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