pkcs7_verify.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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_init(desc);
  53. if (ret < 0)
  54. goto error;
  55. ret = crypto_shash_finup(desc, pkcs7->data, pkcs7->data_len,
  56. sig->digest);
  57. if (ret < 0)
  58. goto error;
  59. pr_devel("MsgDigest = [%*ph]\n", 8, sig->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 != 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(sig->digest, sinfo->msgdigest,
  78. sinfo->msgdigest_len) != 0) {
  79. pr_debug("Sig %u: Message digest doesn't match\n",
  80. sinfo->index);
  81. ret = -EKEYREJECTED;
  82. goto error;
  83. }
  84. /* We then calculate anew, using the authenticated attributes
  85. * as the contents of the digest instead. Note that we need to
  86. * convert the attributes from a CONT.0 into a SET before we
  87. * hash it.
  88. */
  89. memset(sig->digest, 0, sig->digest_size);
  90. ret = crypto_shash_init(desc);
  91. if (ret < 0)
  92. goto error;
  93. tag = ASN1_CONS_BIT | ASN1_SET;
  94. ret = crypto_shash_update(desc, &tag, 1);
  95. if (ret < 0)
  96. goto error;
  97. ret = crypto_shash_finup(desc, sinfo->authattrs,
  98. sinfo->authattrs_len, sig->digest);
  99. if (ret < 0)
  100. goto error;
  101. pr_devel("AADigest = [%*ph]\n", 8, sig->digest);
  102. }
  103. error:
  104. kfree(desc);
  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->sig->auth_ids[0]))
  129. continue;
  130. pr_devel("Sig %u: Found cert serial match X.509[%u]\n",
  131. sinfo->index, certix);
  132. if (strcmp(x509->pub->pkey_algo, sinfo->sig->pkey_algo) != 0) {
  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->sig->auth_ids[0]->len, sinfo->sig->auth_ids[0]->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 public_key_signature *sig;
  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. if (x509->unsupported_key)
  167. goto unsupported_crypto_in_x509;
  168. pr_debug("- issuer %s\n", x509->issuer);
  169. sig = x509->sig;
  170. if (sig->auth_ids[0])
  171. pr_debug("- authkeyid.id %*phN\n",
  172. sig->auth_ids[0]->len, sig->auth_ids[0]->data);
  173. if (sig->auth_ids[1])
  174. pr_debug("- authkeyid.skid %*phN\n",
  175. sig->auth_ids[1]->len, sig->auth_ids[1]->data);
  176. if (x509->self_signed) {
  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. if (x509->unsupported_sig)
  183. goto unsupported_crypto_in_x509;
  184. x509->signer = x509;
  185. pr_debug("- self-signed\n");
  186. return 0;
  187. }
  188. /* Look through the X.509 certificates in the PKCS#7 message's
  189. * list to see if the next one is there.
  190. */
  191. auth = sig->auth_ids[0];
  192. if (auth) {
  193. pr_debug("- want %*phN\n", auth->len, auth->data);
  194. for (p = pkcs7->certs; p; p = p->next) {
  195. pr_debug("- cmp [%u] %*phN\n",
  196. p->index, p->id->len, p->id->data);
  197. if (asymmetric_key_id_same(p->id, auth))
  198. goto found_issuer_check_skid;
  199. }
  200. } else if (sig->auth_ids[1]) {
  201. auth = sig->auth_ids[1];
  202. pr_debug("- want %*phN\n", auth->len, auth->data);
  203. for (p = pkcs7->certs; p; p = p->next) {
  204. if (!p->skid)
  205. continue;
  206. pr_debug("- cmp [%u] %*phN\n",
  207. p->index, p->skid->len, p->skid->data);
  208. if (asymmetric_key_id_same(p->skid, auth))
  209. goto found_issuer;
  210. }
  211. }
  212. /* We didn't find the root of this chain */
  213. pr_debug("- top\n");
  214. return 0;
  215. found_issuer_check_skid:
  216. /* We matched issuer + serialNumber, but if there's an
  217. * authKeyId.keyId, that must match the CA subjKeyId also.
  218. */
  219. if (sig->auth_ids[1] &&
  220. !asymmetric_key_id_same(p->skid, sig->auth_ids[1])) {
  221. pr_warn("Sig %u: X.509 chain contains auth-skid nonmatch (%u->%u)\n",
  222. sinfo->index, x509->index, p->index);
  223. return -EKEYREJECTED;
  224. }
  225. found_issuer:
  226. pr_debug("- subject %s\n", p->subject);
  227. if (p->seen) {
  228. pr_warn("Sig %u: X.509 chain contains loop\n",
  229. sinfo->index);
  230. return 0;
  231. }
  232. ret = public_key_verify_signature(p->pub, x509->sig);
  233. if (ret < 0)
  234. return ret;
  235. x509->signer = p;
  236. if (x509 == p) {
  237. pr_debug("- self-signed\n");
  238. return 0;
  239. }
  240. x509 = p;
  241. might_sleep();
  242. }
  243. unsupported_crypto_in_x509:
  244. /* Just prune the certificate chain at this point if we lack some
  245. * crypto module to go further. Note, however, we don't want to set
  246. * sinfo->unsupported_crypto as the signed info block may still be
  247. * validatable against an X.509 cert lower in the chain that we have a
  248. * trusted copy of.
  249. */
  250. return 0;
  251. }
  252. /*
  253. * Verify one signed information block from a PKCS#7 message.
  254. */
  255. static int pkcs7_verify_one(struct pkcs7_message *pkcs7,
  256. struct pkcs7_signed_info *sinfo)
  257. {
  258. int ret;
  259. kenter(",%u", sinfo->index);
  260. /* First of all, digest the data in the PKCS#7 message and the
  261. * signed information block
  262. */
  263. ret = pkcs7_digest(pkcs7, sinfo);
  264. if (ret < 0)
  265. return ret;
  266. /* Find the key for the signature if there is one */
  267. ret = pkcs7_find_key(pkcs7, sinfo);
  268. if (ret < 0)
  269. return ret;
  270. if (!sinfo->signer)
  271. return 0;
  272. pr_devel("Using X.509[%u] for sig %u\n",
  273. sinfo->signer->index, sinfo->index);
  274. /* Check that the PKCS#7 signing time is valid according to the X.509
  275. * certificate. We can't, however, check against the system clock
  276. * since that may not have been set yet and may be wrong.
  277. */
  278. if (test_bit(sinfo_has_signing_time, &sinfo->aa_set)) {
  279. if (sinfo->signing_time < sinfo->signer->valid_from ||
  280. sinfo->signing_time > sinfo->signer->valid_to) {
  281. pr_warn("Message signed outside of X.509 validity window\n");
  282. return -EKEYREJECTED;
  283. }
  284. }
  285. /* Verify the PKCS#7 binary against the key */
  286. ret = public_key_verify_signature(sinfo->signer->pub, sinfo->sig);
  287. if (ret < 0)
  288. return ret;
  289. pr_devel("Verified signature %u\n", sinfo->index);
  290. /* Verify the internal certificate chain */
  291. return pkcs7_verify_sig_chain(pkcs7, sinfo);
  292. }
  293. /**
  294. * pkcs7_verify - Verify a PKCS#7 message
  295. * @pkcs7: The PKCS#7 message to be verified
  296. * @usage: The use to which the key is being put
  297. *
  298. * Verify a PKCS#7 message is internally consistent - that is, the data digest
  299. * matches the digest in the AuthAttrs and any signature in the message or one
  300. * of the X.509 certificates it carries that matches another X.509 cert in the
  301. * message can be verified.
  302. *
  303. * This does not look to match the contents of the PKCS#7 message against any
  304. * external public keys.
  305. *
  306. * Returns, in order of descending priority:
  307. *
  308. * (*) -EKEYREJECTED if a key was selected that had a usage restriction at
  309. * odds with the specified usage, or:
  310. *
  311. * (*) -EKEYREJECTED if a signature failed to match for which we found an
  312. * appropriate X.509 certificate, or:
  313. *
  314. * (*) -EBADMSG if some part of the message was invalid, or:
  315. *
  316. * (*) -ENOPKG if none of the signature chains are verifiable because suitable
  317. * crypto modules couldn't be found, or:
  318. *
  319. * (*) 0 if all the signature chains that don't incur -ENOPKG can be verified
  320. * (note that a signature chain may be of zero length), or:
  321. */
  322. int pkcs7_verify(struct pkcs7_message *pkcs7,
  323. enum key_being_used_for usage)
  324. {
  325. struct pkcs7_signed_info *sinfo;
  326. int enopkg = -ENOPKG;
  327. int ret;
  328. kenter("");
  329. switch (usage) {
  330. case VERIFYING_MODULE_SIGNATURE:
  331. if (pkcs7->data_type != OID_data) {
  332. pr_warn("Invalid module sig (not pkcs7-data)\n");
  333. return -EKEYREJECTED;
  334. }
  335. if (pkcs7->have_authattrs) {
  336. pr_warn("Invalid module sig (has authattrs)\n");
  337. return -EKEYREJECTED;
  338. }
  339. break;
  340. case VERIFYING_FIRMWARE_SIGNATURE:
  341. if (pkcs7->data_type != OID_data) {
  342. pr_warn("Invalid firmware sig (not pkcs7-data)\n");
  343. return -EKEYREJECTED;
  344. }
  345. if (!pkcs7->have_authattrs) {
  346. pr_warn("Invalid firmware sig (missing authattrs)\n");
  347. return -EKEYREJECTED;
  348. }
  349. break;
  350. case VERIFYING_KEXEC_PE_SIGNATURE:
  351. if (pkcs7->data_type != OID_msIndirectData) {
  352. pr_warn("Invalid kexec sig (not Authenticode)\n");
  353. return -EKEYREJECTED;
  354. }
  355. /* Authattr presence checked in parser */
  356. break;
  357. case VERIFYING_UNSPECIFIED_SIGNATURE:
  358. if (pkcs7->data_type != OID_data) {
  359. pr_warn("Invalid unspecified sig (not pkcs7-data)\n");
  360. return -EKEYREJECTED;
  361. }
  362. break;
  363. default:
  364. return -EINVAL;
  365. }
  366. for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {
  367. ret = pkcs7_verify_one(pkcs7, sinfo);
  368. if (ret < 0) {
  369. if (ret == -ENOPKG) {
  370. sinfo->unsupported_crypto = true;
  371. continue;
  372. }
  373. kleave(" = %d", ret);
  374. return ret;
  375. }
  376. enopkg = 0;
  377. }
  378. kleave(" = %d", enopkg);
  379. return enopkg;
  380. }
  381. EXPORT_SYMBOL_GPL(pkcs7_verify);
  382. /**
  383. * pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message
  384. * @pkcs7: The PKCS#7 message
  385. * @data: The data to be verified
  386. * @datalen: The amount of data
  387. *
  388. * Supply the detached data needed to verify a PKCS#7 message. Note that no
  389. * attempt to retain/pin the data is made. That is left to the caller. The
  390. * data will not be modified by pkcs7_verify() and will not be freed when the
  391. * PKCS#7 message is freed.
  392. *
  393. * Returns -EINVAL if data is already supplied in the message, 0 otherwise.
  394. */
  395. int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,
  396. const void *data, size_t datalen)
  397. {
  398. if (pkcs7->data) {
  399. pr_debug("Data already supplied\n");
  400. return -EINVAL;
  401. }
  402. pkcs7->data = data;
  403. pkcs7->data_len = datalen;
  404. return 0;
  405. }