pkcs7_verify.c 13 KB

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