pkcs7_verify.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 ([email protected])
  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_warn("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_warn("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. i = match_string(hash_algo_name, HASH_ALGO__LAST,
  122. sinfo->sig->hash_algo);
  123. if (i >= 0)
  124. *hash_algo = i;
  125. return 0;
  126. }
  127. /*
  128. * Find the key (X.509 certificate) to use to verify a PKCS#7 message. PKCS#7
  129. * uses the issuer's name and the issuing certificate serial number for
  130. * matching purposes. These must match the certificate issuer's name (not
  131. * subject's name) and the certificate serial number [RFC 2315 6.7].
  132. */
  133. static int pkcs7_find_key(struct pkcs7_message *pkcs7,
  134. struct pkcs7_signed_info *sinfo)
  135. {
  136. struct x509_certificate *x509;
  137. unsigned certix = 1;
  138. kenter("%u", sinfo->index);
  139. for (x509 = pkcs7->certs; x509; x509 = x509->next, certix++) {
  140. /* I'm _assuming_ that the generator of the PKCS#7 message will
  141. * encode the fields from the X.509 cert in the same way in the
  142. * PKCS#7 message - but I can't be 100% sure of that. It's
  143. * possible this will need element-by-element comparison.
  144. */
  145. if (!asymmetric_key_id_same(x509->id, sinfo->sig->auth_ids[0]))
  146. continue;
  147. pr_devel("Sig %u: Found cert serial match X.509[%u]\n",
  148. sinfo->index, certix);
  149. sinfo->signer = x509;
  150. return 0;
  151. }
  152. /* The relevant X.509 cert isn't found here, but it might be found in
  153. * the trust keyring.
  154. */
  155. pr_debug("Sig %u: Issuing X.509 cert not found (#%*phN)\n",
  156. sinfo->index,
  157. sinfo->sig->auth_ids[0]->len, sinfo->sig->auth_ids[0]->data);
  158. return 0;
  159. }
  160. /*
  161. * Verify the internal certificate chain as best we can.
  162. */
  163. static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,
  164. struct pkcs7_signed_info *sinfo)
  165. {
  166. struct public_key_signature *sig;
  167. struct x509_certificate *x509 = sinfo->signer, *p;
  168. struct asymmetric_key_id *auth;
  169. int ret;
  170. kenter("");
  171. for (p = pkcs7->certs; p; p = p->next)
  172. p->seen = false;
  173. for (;;) {
  174. pr_debug("verify %s: %*phN\n",
  175. x509->subject,
  176. x509->raw_serial_size, x509->raw_serial);
  177. x509->seen = true;
  178. if (x509->blacklisted) {
  179. /* If this cert is blacklisted, then mark everything
  180. * that depends on this as blacklisted too.
  181. */
  182. sinfo->blacklisted = true;
  183. for (p = sinfo->signer; p != x509; p = p->signer)
  184. p->blacklisted = true;
  185. pr_debug("- blacklisted\n");
  186. return 0;
  187. }
  188. pr_debug("- issuer %s\n", x509->issuer);
  189. sig = x509->sig;
  190. if (sig->auth_ids[0])
  191. pr_debug("- authkeyid.id %*phN\n",
  192. sig->auth_ids[0]->len, sig->auth_ids[0]->data);
  193. if (sig->auth_ids[1])
  194. pr_debug("- authkeyid.skid %*phN\n",
  195. sig->auth_ids[1]->len, sig->auth_ids[1]->data);
  196. if (x509->self_signed) {
  197. /* If there's no authority certificate specified, then
  198. * the certificate must be self-signed and is the root
  199. * of the chain. Likewise if the cert is its own
  200. * authority.
  201. */
  202. if (x509->unsupported_sig)
  203. goto unsupported_sig_in_x509;
  204. x509->signer = x509;
  205. pr_debug("- self-signed\n");
  206. return 0;
  207. }
  208. /* Look through the X.509 certificates in the PKCS#7 message's
  209. * list to see if the next one is there.
  210. */
  211. auth = sig->auth_ids[0];
  212. if (auth) {
  213. pr_debug("- want %*phN\n", auth->len, auth->data);
  214. for (p = pkcs7->certs; p; p = p->next) {
  215. pr_debug("- cmp [%u] %*phN\n",
  216. p->index, p->id->len, p->id->data);
  217. if (asymmetric_key_id_same(p->id, auth))
  218. goto found_issuer_check_skid;
  219. }
  220. } else if (sig->auth_ids[1]) {
  221. auth = sig->auth_ids[1];
  222. pr_debug("- want %*phN\n", auth->len, auth->data);
  223. for (p = pkcs7->certs; p; p = p->next) {
  224. if (!p->skid)
  225. continue;
  226. pr_debug("- cmp [%u] %*phN\n",
  227. p->index, p->skid->len, p->skid->data);
  228. if (asymmetric_key_id_same(p->skid, auth))
  229. goto found_issuer;
  230. }
  231. }
  232. /* We didn't find the root of this chain */
  233. pr_debug("- top\n");
  234. return 0;
  235. found_issuer_check_skid:
  236. /* We matched issuer + serialNumber, but if there's an
  237. * authKeyId.keyId, that must match the CA subjKeyId also.
  238. */
  239. if (sig->auth_ids[1] &&
  240. !asymmetric_key_id_same(p->skid, sig->auth_ids[1])) {
  241. pr_warn("Sig %u: X.509 chain contains auth-skid nonmatch (%u->%u)\n",
  242. sinfo->index, x509->index, p->index);
  243. return -EKEYREJECTED;
  244. }
  245. found_issuer:
  246. pr_debug("- subject %s\n", p->subject);
  247. if (p->seen) {
  248. pr_warn("Sig %u: X.509 chain contains loop\n",
  249. sinfo->index);
  250. return 0;
  251. }
  252. ret = public_key_verify_signature(p->pub, x509->sig);
  253. if (ret < 0)
  254. return ret;
  255. x509->signer = p;
  256. if (x509 == p) {
  257. pr_debug("- self-signed\n");
  258. return 0;
  259. }
  260. x509 = p;
  261. might_sleep();
  262. }
  263. unsupported_sig_in_x509:
  264. /* Just prune the certificate chain at this point if we lack some
  265. * crypto module to go further. Note, however, we don't want to set
  266. * sinfo->unsupported_crypto as the signed info block may still be
  267. * validatable against an X.509 cert lower in the chain that we have a
  268. * trusted copy of.
  269. */
  270. return 0;
  271. }
  272. /*
  273. * Verify one signed information block from a PKCS#7 message.
  274. */
  275. static int pkcs7_verify_one(struct pkcs7_message *pkcs7,
  276. struct pkcs7_signed_info *sinfo)
  277. {
  278. int ret;
  279. kenter(",%u", sinfo->index);
  280. /* First of all, digest the data in the PKCS#7 message and the
  281. * signed information block
  282. */
  283. ret = pkcs7_digest(pkcs7, sinfo);
  284. if (ret < 0)
  285. return ret;
  286. /* Find the key for the signature if there is one */
  287. ret = pkcs7_find_key(pkcs7, sinfo);
  288. if (ret < 0)
  289. return ret;
  290. if (!sinfo->signer)
  291. return 0;
  292. pr_devel("Using X.509[%u] for sig %u\n",
  293. sinfo->signer->index, sinfo->index);
  294. /* Check that the PKCS#7 signing time is valid according to the X.509
  295. * certificate. We can't, however, check against the system clock
  296. * since that may not have been set yet and may be wrong.
  297. */
  298. if (test_bit(sinfo_has_signing_time, &sinfo->aa_set)) {
  299. if (sinfo->signing_time < sinfo->signer->valid_from ||
  300. sinfo->signing_time > sinfo->signer->valid_to) {
  301. pr_warn("Message signed outside of X.509 validity window\n");
  302. return -EKEYREJECTED;
  303. }
  304. }
  305. /* Verify the PKCS#7 binary against the key */
  306. ret = public_key_verify_signature(sinfo->signer->pub, sinfo->sig);
  307. if (ret < 0)
  308. return ret;
  309. pr_devel("Verified signature %u\n", sinfo->index);
  310. /* Verify the internal certificate chain */
  311. return pkcs7_verify_sig_chain(pkcs7, sinfo);
  312. }
  313. /**
  314. * pkcs7_verify - Verify a PKCS#7 message
  315. * @pkcs7: The PKCS#7 message to be verified
  316. * @usage: The use to which the key is being put
  317. *
  318. * Verify a PKCS#7 message is internally consistent - that is, the data digest
  319. * matches the digest in the AuthAttrs and any signature in the message or one
  320. * of the X.509 certificates it carries that matches another X.509 cert in the
  321. * message can be verified.
  322. *
  323. * This does not look to match the contents of the PKCS#7 message against any
  324. * external public keys.
  325. *
  326. * Returns, in order of descending priority:
  327. *
  328. * (*) -EKEYREJECTED if a key was selected that had a usage restriction at
  329. * odds with the specified usage, or:
  330. *
  331. * (*) -EKEYREJECTED if a signature failed to match for which we found an
  332. * appropriate X.509 certificate, or:
  333. *
  334. * (*) -EBADMSG if some part of the message was invalid, or:
  335. *
  336. * (*) 0 if a signature chain passed verification, or:
  337. *
  338. * (*) -EKEYREJECTED if a blacklisted key was encountered, or:
  339. *
  340. * (*) -ENOPKG if none of the signature chains are verifiable because suitable
  341. * crypto modules couldn't be found.
  342. */
  343. int pkcs7_verify(struct pkcs7_message *pkcs7,
  344. enum key_being_used_for usage)
  345. {
  346. struct pkcs7_signed_info *sinfo;
  347. int actual_ret = -ENOPKG;
  348. int ret;
  349. kenter("");
  350. switch (usage) {
  351. case VERIFYING_MODULE_SIGNATURE:
  352. if (pkcs7->data_type != OID_data) {
  353. pr_warn("Invalid module sig (not pkcs7-data)\n");
  354. return -EKEYREJECTED;
  355. }
  356. if (pkcs7->have_authattrs) {
  357. pr_warn("Invalid module sig (has authattrs)\n");
  358. return -EKEYREJECTED;
  359. }
  360. break;
  361. case VERIFYING_FIRMWARE_SIGNATURE:
  362. if (pkcs7->data_type != OID_data) {
  363. pr_warn("Invalid firmware sig (not pkcs7-data)\n");
  364. return -EKEYREJECTED;
  365. }
  366. if (!pkcs7->have_authattrs) {
  367. pr_warn("Invalid firmware sig (missing authattrs)\n");
  368. return -EKEYREJECTED;
  369. }
  370. break;
  371. case VERIFYING_KEXEC_PE_SIGNATURE:
  372. if (pkcs7->data_type != OID_msIndirectData) {
  373. pr_warn("Invalid kexec sig (not Authenticode)\n");
  374. return -EKEYREJECTED;
  375. }
  376. /* Authattr presence checked in parser */
  377. break;
  378. case VERIFYING_UNSPECIFIED_SIGNATURE:
  379. if (pkcs7->data_type != OID_data) {
  380. pr_warn("Invalid unspecified sig (not pkcs7-data)\n");
  381. return -EKEYREJECTED;
  382. }
  383. break;
  384. default:
  385. return -EINVAL;
  386. }
  387. for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {
  388. ret = pkcs7_verify_one(pkcs7, sinfo);
  389. if (sinfo->blacklisted) {
  390. if (actual_ret == -ENOPKG)
  391. actual_ret = -EKEYREJECTED;
  392. continue;
  393. }
  394. if (ret < 0) {
  395. if (ret == -ENOPKG) {
  396. sinfo->unsupported_crypto = true;
  397. continue;
  398. }
  399. kleave(" = %d", ret);
  400. return ret;
  401. }
  402. actual_ret = 0;
  403. }
  404. kleave(" = %d", actual_ret);
  405. return actual_ret;
  406. }
  407. EXPORT_SYMBOL_GPL(pkcs7_verify);
  408. /**
  409. * pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message
  410. * @pkcs7: The PKCS#7 message
  411. * @data: The data to be verified
  412. * @datalen: The amount of data
  413. *
  414. * Supply the detached data needed to verify a PKCS#7 message. Note that no
  415. * attempt to retain/pin the data is made. That is left to the caller. The
  416. * data will not be modified by pkcs7_verify() and will not be freed when the
  417. * PKCS#7 message is freed.
  418. *
  419. * Returns -EINVAL if data is already supplied in the message, 0 otherwise.
  420. */
  421. int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,
  422. const void *data, size_t datalen)
  423. {
  424. if (pkcs7->data) {
  425. pr_warn("Data already supplied\n");
  426. return -EINVAL;
  427. }
  428. pkcs7->data = data;
  429. pkcs7->data_len = datalen;
  430. return 0;
  431. }