pkcs7_verify.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /* Verify the signature on a PKCS#7 message.
  2. *
  3. * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #define pr_fmt(fmt) "PKCS7: "fmt
  12. #include <linux/kernel.h>
  13. #include <linux/export.h>
  14. #include <linux/slab.h>
  15. #include <linux/err.h>
  16. #include <linux/asn1.h>
  17. #include <crypto/hash.h>
  18. #include <crypto/public_key.h>
  19. #include "pkcs7_parser.h"
  20. /*
  21. * Digest the relevant parts of the PKCS#7 data
  22. */
  23. static int pkcs7_digest(struct pkcs7_message *pkcs7,
  24. struct pkcs7_signed_info *sinfo)
  25. {
  26. struct public_key_signature *sig = sinfo->sig;
  27. struct crypto_shash *tfm;
  28. struct shash_desc *desc;
  29. size_t desc_size;
  30. int ret;
  31. kenter(",%u,%s", sinfo->index, sinfo->sig->hash_algo);
  32. if (!sinfo->sig->hash_algo)
  33. return -ENOPKG;
  34. /* Allocate the hashing algorithm we're going to need and find out how
  35. * big the hash operational data will be.
  36. */
  37. tfm = crypto_alloc_shash(sinfo->sig->hash_algo, 0, 0);
  38. if (IS_ERR(tfm))
  39. return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);
  40. desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
  41. sig->digest_size = crypto_shash_digestsize(tfm);
  42. ret = -ENOMEM;
  43. sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);
  44. if (!sig->digest)
  45. goto error_no_desc;
  46. desc = kzalloc(desc_size, GFP_KERNEL);
  47. if (!desc)
  48. goto error_no_desc;
  49. desc->tfm = tfm;
  50. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  51. /* Digest the message [RFC2315 9.3] */
  52. ret = crypto_shash_digest(desc, pkcs7->data, pkcs7->data_len,
  53. sig->digest);
  54. if (ret < 0)
  55. goto error;
  56. pr_devel("MsgDigest = [%*ph]\n", 8, sig->digest);
  57. /* However, if there are authenticated attributes, there must be a
  58. * message digest attribute amongst them which corresponds to the
  59. * digest we just calculated.
  60. */
  61. if (sinfo->authattrs) {
  62. u8 tag;
  63. if (!sinfo->msgdigest) {
  64. pr_warn("Sig %u: No messageDigest\n", sinfo->index);
  65. ret = -EKEYREJECTED;
  66. goto error;
  67. }
  68. if (sinfo->msgdigest_len != sig->digest_size) {
  69. pr_debug("Sig %u: Invalid digest size (%u)\n",
  70. sinfo->index, sinfo->msgdigest_len);
  71. ret = -EBADMSG;
  72. goto error;
  73. }
  74. if (memcmp(sig->digest, sinfo->msgdigest,
  75. sinfo->msgdigest_len) != 0) {
  76. pr_debug("Sig %u: Message digest doesn't match\n",
  77. sinfo->index);
  78. ret = -EKEYREJECTED;
  79. goto error;
  80. }
  81. /* We then calculate anew, using the authenticated attributes
  82. * as the contents of the digest instead. Note that we need to
  83. * convert the attributes from a CONT.0 into a SET before we
  84. * hash it.
  85. */
  86. memset(sig->digest, 0, sig->digest_size);
  87. ret = crypto_shash_init(desc);
  88. if (ret < 0)
  89. goto error;
  90. tag = ASN1_CONS_BIT | ASN1_SET;
  91. ret = crypto_shash_update(desc, &tag, 1);
  92. if (ret < 0)
  93. goto error;
  94. ret = crypto_shash_finup(desc, sinfo->authattrs,
  95. sinfo->authattrs_len, sig->digest);
  96. if (ret < 0)
  97. goto error;
  98. pr_devel("AADigest = [%*ph]\n", 8, sig->digest);
  99. }
  100. error:
  101. kfree(desc);
  102. error_no_desc:
  103. crypto_free_shash(tfm);
  104. kleave(" = %d", ret);
  105. return ret;
  106. }
  107. /*
  108. * Find the key (X.509 certificate) to use to verify a PKCS#7 message. PKCS#7
  109. * uses the issuer's name and the issuing certificate serial number for
  110. * matching purposes. These must match the certificate issuer's name (not
  111. * subject's name) and the certificate serial number [RFC 2315 6.7].
  112. */
  113. static int pkcs7_find_key(struct pkcs7_message *pkcs7,
  114. struct pkcs7_signed_info *sinfo)
  115. {
  116. struct x509_certificate *x509;
  117. unsigned certix = 1;
  118. kenter("%u", sinfo->index);
  119. for (x509 = pkcs7->certs; x509; x509 = x509->next, certix++) {
  120. /* I'm _assuming_ that the generator of the PKCS#7 message will
  121. * encode the fields from the X.509 cert in the same way in the
  122. * PKCS#7 message - but I can't be 100% sure of that. It's
  123. * possible this will need element-by-element comparison.
  124. */
  125. if (!asymmetric_key_id_same(x509->id, sinfo->sig->auth_ids[0]))
  126. continue;
  127. pr_devel("Sig %u: Found cert serial match X.509[%u]\n",
  128. sinfo->index, certix);
  129. if (strcmp(x509->pub->pkey_algo, sinfo->sig->pkey_algo) != 0) {
  130. pr_warn("Sig %u: X.509 algo and PKCS#7 sig algo don't match\n",
  131. sinfo->index);
  132. continue;
  133. }
  134. sinfo->signer = x509;
  135. return 0;
  136. }
  137. /* The relevant X.509 cert isn't found here, but it might be found in
  138. * the trust keyring.
  139. */
  140. pr_debug("Sig %u: Issuing X.509 cert not found (#%*phN)\n",
  141. sinfo->index,
  142. sinfo->sig->auth_ids[0]->len, sinfo->sig->auth_ids[0]->data);
  143. return 0;
  144. }
  145. /*
  146. * Verify the internal certificate chain as best we can.
  147. */
  148. static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,
  149. struct pkcs7_signed_info *sinfo)
  150. {
  151. struct public_key_signature *sig;
  152. struct x509_certificate *x509 = sinfo->signer, *p;
  153. struct asymmetric_key_id *auth;
  154. int ret;
  155. kenter("");
  156. for (p = pkcs7->certs; p; p = p->next)
  157. p->seen = false;
  158. for (;;) {
  159. pr_debug("verify %s: %*phN\n",
  160. x509->subject,
  161. x509->raw_serial_size, x509->raw_serial);
  162. x509->seen = true;
  163. if (x509->blacklisted) {
  164. /* If this cert is blacklisted, then mark everything
  165. * that depends on this as blacklisted too.
  166. */
  167. sinfo->blacklisted = true;
  168. for (p = sinfo->signer; p != x509; p = p->signer)
  169. p->blacklisted = true;
  170. pr_debug("- blacklisted\n");
  171. return 0;
  172. }
  173. if (x509->unsupported_key)
  174. goto unsupported_crypto_in_x509;
  175. pr_debug("- issuer %s\n", x509->issuer);
  176. sig = x509->sig;
  177. if (sig->auth_ids[0])
  178. pr_debug("- authkeyid.id %*phN\n",
  179. sig->auth_ids[0]->len, sig->auth_ids[0]->data);
  180. if (sig->auth_ids[1])
  181. pr_debug("- authkeyid.skid %*phN\n",
  182. sig->auth_ids[1]->len, sig->auth_ids[1]->data);
  183. if (x509->self_signed) {
  184. /* If there's no authority certificate specified, then
  185. * the certificate must be self-signed and is the root
  186. * of the chain. Likewise if the cert is its own
  187. * authority.
  188. */
  189. if (x509->unsupported_sig)
  190. goto unsupported_crypto_in_x509;
  191. x509->signer = x509;
  192. pr_debug("- self-signed\n");
  193. return 0;
  194. }
  195. /* Look through the X.509 certificates in the PKCS#7 message's
  196. * list to see if the next one is there.
  197. */
  198. auth = sig->auth_ids[0];
  199. if (auth) {
  200. pr_debug("- want %*phN\n", auth->len, auth->data);
  201. for (p = pkcs7->certs; p; p = p->next) {
  202. pr_debug("- cmp [%u] %*phN\n",
  203. p->index, p->id->len, p->id->data);
  204. if (asymmetric_key_id_same(p->id, auth))
  205. goto found_issuer_check_skid;
  206. }
  207. } else if (sig->auth_ids[1]) {
  208. auth = sig->auth_ids[1];
  209. pr_debug("- want %*phN\n", auth->len, auth->data);
  210. for (p = pkcs7->certs; p; p = p->next) {
  211. if (!p->skid)
  212. continue;
  213. pr_debug("- cmp [%u] %*phN\n",
  214. p->index, p->skid->len, p->skid->data);
  215. if (asymmetric_key_id_same(p->skid, auth))
  216. goto found_issuer;
  217. }
  218. }
  219. /* We didn't find the root of this chain */
  220. pr_debug("- top\n");
  221. return 0;
  222. found_issuer_check_skid:
  223. /* We matched issuer + serialNumber, but if there's an
  224. * authKeyId.keyId, that must match the CA subjKeyId also.
  225. */
  226. if (sig->auth_ids[1] &&
  227. !asymmetric_key_id_same(p->skid, sig->auth_ids[1])) {
  228. pr_warn("Sig %u: X.509 chain contains auth-skid nonmatch (%u->%u)\n",
  229. sinfo->index, x509->index, p->index);
  230. return -EKEYREJECTED;
  231. }
  232. found_issuer:
  233. pr_debug("- subject %s\n", p->subject);
  234. if (p->seen) {
  235. pr_warn("Sig %u: X.509 chain contains loop\n",
  236. sinfo->index);
  237. return 0;
  238. }
  239. ret = public_key_verify_signature(p->pub, x509->sig);
  240. if (ret < 0)
  241. return ret;
  242. x509->signer = p;
  243. if (x509 == p) {
  244. pr_debug("- self-signed\n");
  245. return 0;
  246. }
  247. x509 = p;
  248. might_sleep();
  249. }
  250. unsupported_crypto_in_x509:
  251. /* Just prune the certificate chain at this point if we lack some
  252. * crypto module to go further. Note, however, we don't want to set
  253. * sinfo->unsupported_crypto as the signed info block may still be
  254. * validatable against an X.509 cert lower in the chain that we have a
  255. * trusted copy of.
  256. */
  257. return 0;
  258. }
  259. /*
  260. * Verify one signed information block from a PKCS#7 message.
  261. */
  262. static int pkcs7_verify_one(struct pkcs7_message *pkcs7,
  263. struct pkcs7_signed_info *sinfo)
  264. {
  265. int ret;
  266. kenter(",%u", sinfo->index);
  267. /* First of all, digest the data in the PKCS#7 message and the
  268. * signed information block
  269. */
  270. ret = pkcs7_digest(pkcs7, sinfo);
  271. if (ret < 0)
  272. return ret;
  273. /* Find the key for the signature if there is one */
  274. ret = pkcs7_find_key(pkcs7, sinfo);
  275. if (ret < 0)
  276. return ret;
  277. if (!sinfo->signer)
  278. return 0;
  279. pr_devel("Using X.509[%u] for sig %u\n",
  280. sinfo->signer->index, sinfo->index);
  281. /* Check that the PKCS#7 signing time is valid according to the X.509
  282. * certificate. We can't, however, check against the system clock
  283. * since that may not have been set yet and may be wrong.
  284. */
  285. if (test_bit(sinfo_has_signing_time, &sinfo->aa_set)) {
  286. if (sinfo->signing_time < sinfo->signer->valid_from ||
  287. sinfo->signing_time > sinfo->signer->valid_to) {
  288. pr_warn("Message signed outside of X.509 validity window\n");
  289. return -EKEYREJECTED;
  290. }
  291. }
  292. /* Verify the PKCS#7 binary against the key */
  293. ret = public_key_verify_signature(sinfo->signer->pub, sinfo->sig);
  294. if (ret < 0)
  295. return ret;
  296. pr_devel("Verified signature %u\n", sinfo->index);
  297. /* Verify the internal certificate chain */
  298. return pkcs7_verify_sig_chain(pkcs7, sinfo);
  299. }
  300. /**
  301. * pkcs7_verify - Verify a PKCS#7 message
  302. * @pkcs7: The PKCS#7 message to be verified
  303. * @usage: The use to which the key is being put
  304. *
  305. * Verify a PKCS#7 message is internally consistent - that is, the data digest
  306. * matches the digest in the AuthAttrs and any signature in the message or one
  307. * of the X.509 certificates it carries that matches another X.509 cert in the
  308. * message can be verified.
  309. *
  310. * This does not look to match the contents of the PKCS#7 message against any
  311. * external public keys.
  312. *
  313. * Returns, in order of descending priority:
  314. *
  315. * (*) -EKEYREJECTED if a key was selected that had a usage restriction at
  316. * odds with the specified usage, or:
  317. *
  318. * (*) -EKEYREJECTED if a signature failed to match for which we found an
  319. * appropriate X.509 certificate, or:
  320. *
  321. * (*) -EBADMSG if some part of the message was invalid, or:
  322. *
  323. * (*) 0 if a signature chain passed verification, or:
  324. *
  325. * (*) -EKEYREJECTED if a blacklisted key was encountered, or:
  326. *
  327. * (*) -ENOPKG if none of the signature chains are verifiable because suitable
  328. * crypto modules couldn't be found.
  329. */
  330. int pkcs7_verify(struct pkcs7_message *pkcs7,
  331. enum key_being_used_for usage)
  332. {
  333. struct pkcs7_signed_info *sinfo;
  334. int actual_ret = -ENOPKG;
  335. int ret;
  336. kenter("");
  337. switch (usage) {
  338. case VERIFYING_MODULE_SIGNATURE:
  339. if (pkcs7->data_type != OID_data) {
  340. pr_warn("Invalid module sig (not pkcs7-data)\n");
  341. return -EKEYREJECTED;
  342. }
  343. if (pkcs7->have_authattrs) {
  344. pr_warn("Invalid module sig (has authattrs)\n");
  345. return -EKEYREJECTED;
  346. }
  347. break;
  348. case VERIFYING_FIRMWARE_SIGNATURE:
  349. if (pkcs7->data_type != OID_data) {
  350. pr_warn("Invalid firmware sig (not pkcs7-data)\n");
  351. return -EKEYREJECTED;
  352. }
  353. if (!pkcs7->have_authattrs) {
  354. pr_warn("Invalid firmware sig (missing authattrs)\n");
  355. return -EKEYREJECTED;
  356. }
  357. break;
  358. case VERIFYING_KEXEC_PE_SIGNATURE:
  359. if (pkcs7->data_type != OID_msIndirectData) {
  360. pr_warn("Invalid kexec sig (not Authenticode)\n");
  361. return -EKEYREJECTED;
  362. }
  363. /* Authattr presence checked in parser */
  364. break;
  365. case VERIFYING_UNSPECIFIED_SIGNATURE:
  366. if (pkcs7->data_type != OID_data) {
  367. pr_warn("Invalid unspecified sig (not pkcs7-data)\n");
  368. return -EKEYREJECTED;
  369. }
  370. break;
  371. default:
  372. return -EINVAL;
  373. }
  374. for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {
  375. ret = pkcs7_verify_one(pkcs7, sinfo);
  376. if (sinfo->blacklisted) {
  377. if (actual_ret == -ENOPKG)
  378. actual_ret = -EKEYREJECTED;
  379. continue;
  380. }
  381. if (ret < 0) {
  382. if (ret == -ENOPKG) {
  383. sinfo->unsupported_crypto = true;
  384. continue;
  385. }
  386. kleave(" = %d", ret);
  387. return ret;
  388. }
  389. actual_ret = 0;
  390. }
  391. kleave(" = %d", actual_ret);
  392. return actual_ret;
  393. }
  394. EXPORT_SYMBOL_GPL(pkcs7_verify);
  395. /**
  396. * pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message
  397. * @pkcs7: The PKCS#7 message
  398. * @data: The data to be verified
  399. * @datalen: The amount of data
  400. *
  401. * Supply the detached data needed to verify a PKCS#7 message. Note that no
  402. * attempt to retain/pin the data is made. That is left to the caller. The
  403. * data will not be modified by pkcs7_verify() and will not be freed when the
  404. * PKCS#7 message is freed.
  405. *
  406. * Returns -EINVAL if data is already supplied in the message, 0 otherwise.
  407. */
  408. int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,
  409. const void *data, size_t datalen)
  410. {
  411. if (pkcs7->data) {
  412. pr_debug("Data already supplied\n");
  413. return -EINVAL;
  414. }
  415. pkcs7->data = data;
  416. pkcs7->data_len = datalen;
  417. return 0;
  418. }