pkcs7_verify.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 "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 crypto_shash *tfm;
  27. struct shash_desc *desc;
  28. size_t digest_size, desc_size;
  29. void *digest;
  30. int ret;
  31. kenter(",%u,%u", sinfo->index, sinfo->sig.pkey_hash_algo);
  32. if (sinfo->sig.pkey_hash_algo >= PKEY_HASH__LAST ||
  33. !hash_algo_name[sinfo->sig.pkey_hash_algo])
  34. return -ENOPKG;
  35. /* Allocate the hashing algorithm we're going to need and find out how
  36. * big the hash operational data will be.
  37. */
  38. tfm = crypto_alloc_shash(hash_algo_name[sinfo->sig.pkey_hash_algo],
  39. 0, 0);
  40. if (IS_ERR(tfm))
  41. return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);
  42. desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
  43. sinfo->sig.digest_size = digest_size = crypto_shash_digestsize(tfm);
  44. ret = -ENOMEM;
  45. digest = kzalloc(ALIGN(digest_size, __alignof__(*desc)) + desc_size,
  46. GFP_KERNEL);
  47. if (!digest)
  48. goto error_no_desc;
  49. desc = PTR_ALIGN(digest + digest_size, __alignof__(*desc));
  50. desc->tfm = tfm;
  51. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  52. /* Digest the message [RFC2315 9.3] */
  53. ret = crypto_shash_init(desc);
  54. if (ret < 0)
  55. goto error;
  56. ret = crypto_shash_finup(desc, pkcs7->data, pkcs7->data_len, digest);
  57. if (ret < 0)
  58. goto error;
  59. pr_devel("MsgDigest = [%*ph]\n", 8, digest);
  60. /* However, if there are authenticated attributes, there must be a
  61. * message digest attribute amongst them which corresponds to the
  62. * digest we just calculated.
  63. */
  64. if (sinfo->authattrs) {
  65. u8 tag;
  66. if (!sinfo->msgdigest) {
  67. pr_warn("Sig %u: No messageDigest\n", sinfo->index);
  68. ret = -EKEYREJECTED;
  69. goto error;
  70. }
  71. if (sinfo->msgdigest_len != sinfo->sig.digest_size) {
  72. pr_debug("Sig %u: Invalid digest size (%u)\n",
  73. sinfo->index, sinfo->msgdigest_len);
  74. ret = -EBADMSG;
  75. goto error;
  76. }
  77. if (memcmp(digest, sinfo->msgdigest, sinfo->msgdigest_len) != 0) {
  78. pr_debug("Sig %u: Message digest doesn't match\n",
  79. sinfo->index);
  80. ret = -EKEYREJECTED;
  81. goto error;
  82. }
  83. /* We then calculate anew, using the authenticated attributes
  84. * as the contents of the digest instead. Note that we need to
  85. * convert the attributes from a CONT.0 into a SET before we
  86. * hash it.
  87. */
  88. memset(digest, 0, sinfo->sig.digest_size);
  89. ret = crypto_shash_init(desc);
  90. if (ret < 0)
  91. goto error;
  92. tag = ASN1_CONS_BIT | ASN1_SET;
  93. ret = crypto_shash_update(desc, &tag, 1);
  94. if (ret < 0)
  95. goto error;
  96. ret = crypto_shash_finup(desc, sinfo->authattrs,
  97. sinfo->authattrs_len, digest);
  98. if (ret < 0)
  99. goto error;
  100. pr_devel("AADigest = [%*ph]\n", 8, digest);
  101. }
  102. sinfo->sig.digest = digest;
  103. digest = NULL;
  104. error:
  105. kfree(digest);
  106. error_no_desc:
  107. crypto_free_shash(tfm);
  108. kleave(" = %d", ret);
  109. return ret;
  110. }
  111. /*
  112. * Find the key (X.509 certificate) to use to verify a PKCS#7 message. PKCS#7
  113. * uses the issuer's name and the issuing certificate serial number for
  114. * matching purposes. These must match the certificate issuer's name (not
  115. * subject's name) and the certificate serial number [RFC 2315 6.7].
  116. */
  117. static int pkcs7_find_key(struct pkcs7_message *pkcs7,
  118. struct pkcs7_signed_info *sinfo)
  119. {
  120. struct x509_certificate *x509;
  121. unsigned certix = 1;
  122. kenter("%u", sinfo->index);
  123. for (x509 = pkcs7->certs; x509; x509 = x509->next, certix++) {
  124. /* I'm _assuming_ that the generator of the PKCS#7 message will
  125. * encode the fields from the X.509 cert in the same way in the
  126. * PKCS#7 message - but I can't be 100% sure of that. It's
  127. * possible this will need element-by-element comparison.
  128. */
  129. if (!asymmetric_key_id_same(x509->id, sinfo->signing_cert_id))
  130. continue;
  131. pr_devel("Sig %u: Found cert serial match X.509[%u]\n",
  132. sinfo->index, certix);
  133. if (x509->pub->pkey_algo != sinfo->sig.pkey_algo) {
  134. pr_warn("Sig %u: X.509 algo and PKCS#7 sig algo don't match\n",
  135. sinfo->index);
  136. continue;
  137. }
  138. sinfo->signer = x509;
  139. return 0;
  140. }
  141. /* The relevant X.509 cert isn't found here, but it might be found in
  142. * the trust keyring.
  143. */
  144. pr_debug("Sig %u: Issuing X.509 cert not found (#%*phN)\n",
  145. sinfo->index,
  146. sinfo->signing_cert_id->len, sinfo->signing_cert_id->data);
  147. return 0;
  148. }
  149. /*
  150. * Verify the internal certificate chain as best we can.
  151. */
  152. static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,
  153. struct pkcs7_signed_info *sinfo)
  154. {
  155. struct x509_certificate *x509 = sinfo->signer, *p;
  156. struct asymmetric_key_id *auth;
  157. int ret;
  158. kenter("");
  159. for (p = pkcs7->certs; p; p = p->next)
  160. p->seen = false;
  161. for (;;) {
  162. pr_debug("verify %s: %*phN\n",
  163. x509->subject,
  164. x509->raw_serial_size, x509->raw_serial);
  165. x509->seen = true;
  166. ret = x509_get_sig_params(x509);
  167. if (ret < 0)
  168. goto maybe_missing_crypto_in_x509;
  169. pr_debug("- issuer %s\n", x509->issuer);
  170. if (x509->akid_id)
  171. pr_debug("- authkeyid.id %*phN\n",
  172. x509->akid_id->len, x509->akid_id->data);
  173. if (x509->akid_skid)
  174. pr_debug("- authkeyid.skid %*phN\n",
  175. x509->akid_skid->len, x509->akid_skid->data);
  176. if ((!x509->akid_id && !x509->akid_skid) ||
  177. strcmp(x509->subject, x509->issuer) == 0) {
  178. /* If there's no authority certificate specified, then
  179. * the certificate must be self-signed and is the root
  180. * of the chain. Likewise if the cert is its own
  181. * authority.
  182. */
  183. pr_debug("- no auth?\n");
  184. if (x509->raw_subject_size != x509->raw_issuer_size ||
  185. memcmp(x509->raw_subject, x509->raw_issuer,
  186. x509->raw_issuer_size) != 0)
  187. return 0;
  188. ret = x509_check_signature(x509->pub, x509);
  189. if (ret < 0)
  190. goto maybe_missing_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 = x509->akid_id;
  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 {
  208. auth = x509->akid_skid;
  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 (x509->akid_skid &&
  227. !asymmetric_key_id_same(p->skid, x509->akid_skid)) {
  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 = x509_check_signature(p->pub, x509);
  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. maybe_missing_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->missing_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. if (ret == -ENOPKG)
  258. return 0;
  259. return ret;
  260. }
  261. /*
  262. * Verify one signed information block from a PKCS#7 message.
  263. */
  264. static int pkcs7_verify_one(struct pkcs7_message *pkcs7,
  265. struct pkcs7_signed_info *sinfo)
  266. {
  267. int ret;
  268. kenter(",%u", sinfo->index);
  269. /* First of all, digest the data in the PKCS#7 message and the
  270. * signed information block
  271. */
  272. ret = pkcs7_digest(pkcs7, sinfo);
  273. if (ret < 0)
  274. return ret;
  275. /* Find the key for the signature if there is one */
  276. ret = pkcs7_find_key(pkcs7, sinfo);
  277. if (ret < 0)
  278. return ret;
  279. if (!sinfo->signer)
  280. return 0;
  281. pr_devel("Using X.509[%u] for sig %u\n",
  282. sinfo->signer->index, sinfo->index);
  283. /* Check that the PKCS#7 signing time is valid according to the X.509
  284. * certificate. We can't, however, check against the system clock
  285. * since that may not have been set yet and may be wrong.
  286. */
  287. if (test_bit(sinfo_has_signing_time, &sinfo->aa_set)) {
  288. if (sinfo->signing_time < sinfo->signer->valid_from ||
  289. sinfo->signing_time > sinfo->signer->valid_to) {
  290. pr_warn("Message signed outside of X.509 validity window\n");
  291. return -EKEYREJECTED;
  292. }
  293. }
  294. /* Verify the PKCS#7 binary against the key */
  295. ret = public_key_verify_signature(sinfo->signer->pub, &sinfo->sig);
  296. if (ret < 0)
  297. return ret;
  298. pr_devel("Verified signature %u\n", sinfo->index);
  299. /* Verify the internal certificate chain */
  300. return pkcs7_verify_sig_chain(pkcs7, sinfo);
  301. }
  302. /**
  303. * pkcs7_verify - Verify a PKCS#7 message
  304. * @pkcs7: The PKCS#7 message to be verified
  305. * @usage: The use to which the key is being put
  306. *
  307. * Verify a PKCS#7 message is internally consistent - that is, the data digest
  308. * matches the digest in the AuthAttrs and any signature in the message or one
  309. * of the X.509 certificates it carries that matches another X.509 cert in the
  310. * message can be verified.
  311. *
  312. * This does not look to match the contents of the PKCS#7 message against any
  313. * external public keys.
  314. *
  315. * Returns, in order of descending priority:
  316. *
  317. * (*) -EKEYREJECTED if a key was selected that had a usage restriction at
  318. * odds with the specified usage, or:
  319. *
  320. * (*) -EKEYREJECTED if a signature failed to match for which we found an
  321. * appropriate X.509 certificate, or:
  322. *
  323. * (*) -EBADMSG if some part of the message was invalid, or:
  324. *
  325. * (*) -ENOPKG if none of the signature chains are verifiable because suitable
  326. * crypto modules couldn't be found, or:
  327. *
  328. * (*) 0 if all the signature chains that don't incur -ENOPKG can be verified
  329. * (note that a signature chain may be of zero length), or:
  330. */
  331. int pkcs7_verify(struct pkcs7_message *pkcs7,
  332. enum key_being_used_for usage)
  333. {
  334. struct pkcs7_signed_info *sinfo;
  335. struct x509_certificate *x509;
  336. int enopkg = -ENOPKG;
  337. int ret, n;
  338. kenter("");
  339. switch (usage) {
  340. case VERIFYING_MODULE_SIGNATURE:
  341. if (pkcs7->data_type != OID_data) {
  342. pr_warn("Invalid module sig (not pkcs7-data)\n");
  343. return -EKEYREJECTED;
  344. }
  345. if (pkcs7->have_authattrs) {
  346. pr_warn("Invalid module sig (has authattrs)\n");
  347. return -EKEYREJECTED;
  348. }
  349. break;
  350. case VERIFYING_FIRMWARE_SIGNATURE:
  351. if (pkcs7->data_type != OID_data) {
  352. pr_warn("Invalid firmware sig (not pkcs7-data)\n");
  353. return -EKEYREJECTED;
  354. }
  355. if (!pkcs7->have_authattrs) {
  356. pr_warn("Invalid firmware sig (missing authattrs)\n");
  357. return -EKEYREJECTED;
  358. }
  359. break;
  360. case VERIFYING_KEXEC_PE_SIGNATURE:
  361. if (pkcs7->data_type != OID_msIndirectData) {
  362. pr_warn("Invalid kexec sig (not Authenticode)\n");
  363. return -EKEYREJECTED;
  364. }
  365. /* Authattr presence checked in parser */
  366. break;
  367. case VERIFYING_UNSPECIFIED_SIGNATURE:
  368. if (pkcs7->data_type != OID_data) {
  369. pr_warn("Invalid unspecified sig (not pkcs7-data)\n");
  370. return -EKEYREJECTED;
  371. }
  372. break;
  373. default:
  374. return -EINVAL;
  375. }
  376. for (n = 0, x509 = pkcs7->certs; x509; x509 = x509->next, n++) {
  377. ret = x509_get_sig_params(x509);
  378. if (ret < 0)
  379. return ret;
  380. }
  381. for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {
  382. ret = pkcs7_verify_one(pkcs7, sinfo);
  383. if (ret < 0) {
  384. if (ret == -ENOPKG) {
  385. sinfo->unsupported_crypto = true;
  386. continue;
  387. }
  388. kleave(" = %d", ret);
  389. return ret;
  390. }
  391. enopkg = 0;
  392. }
  393. kleave(" = %d", enopkg);
  394. return enopkg;
  395. }
  396. EXPORT_SYMBOL_GPL(pkcs7_verify);
  397. /**
  398. * pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message
  399. * @pkcs7: The PKCS#7 message
  400. * @data: The data to be verified
  401. * @datalen: The amount of data
  402. *
  403. * Supply the detached data needed to verify a PKCS#7 message. Note that no
  404. * attempt to retain/pin the data is made. That is left to the caller. The
  405. * data will not be modified by pkcs7_verify() and will not be freed when the
  406. * PKCS#7 message is freed.
  407. *
  408. * Returns -EINVAL if data is already supplied in the message, 0 otherwise.
  409. */
  410. int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,
  411. const void *data, size_t datalen)
  412. {
  413. if (pkcs7->data) {
  414. pr_debug("Data already supplied\n");
  415. return -EINVAL;
  416. }
  417. pkcs7->data = data;
  418. pkcs7->data_len = datalen;
  419. return 0;
  420. }