pkcs7_verify.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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->msgdigest) {
  64. u8 tag;
  65. if (sinfo->msgdigest_len != sinfo->sig.digest_size) {
  66. pr_debug("Sig %u: Invalid digest size (%u)\n",
  67. sinfo->index, sinfo->msgdigest_len);
  68. ret = -EBADMSG;
  69. goto error;
  70. }
  71. if (memcmp(digest, sinfo->msgdigest, sinfo->msgdigest_len) != 0) {
  72. pr_debug("Sig %u: Message digest doesn't match\n",
  73. sinfo->index);
  74. ret = -EKEYREJECTED;
  75. goto error;
  76. }
  77. /* We then calculate anew, using the authenticated attributes
  78. * as the contents of the digest instead. Note that we need to
  79. * convert the attributes from a CONT.0 into a SET before we
  80. * hash it.
  81. */
  82. memset(digest, 0, sinfo->sig.digest_size);
  83. ret = crypto_shash_init(desc);
  84. if (ret < 0)
  85. goto error;
  86. tag = ASN1_CONS_BIT | ASN1_SET;
  87. ret = crypto_shash_update(desc, &tag, 1);
  88. if (ret < 0)
  89. goto error;
  90. ret = crypto_shash_finup(desc, sinfo->authattrs,
  91. sinfo->authattrs_len, digest);
  92. if (ret < 0)
  93. goto error;
  94. pr_devel("AADigest = [%*ph]\n", 8, digest);
  95. }
  96. sinfo->sig.digest = digest;
  97. digest = NULL;
  98. error:
  99. kfree(digest);
  100. error_no_desc:
  101. crypto_free_shash(tfm);
  102. kleave(" = %d", ret);
  103. return ret;
  104. }
  105. /*
  106. * Find the key (X.509 certificate) to use to verify a PKCS#7 message. PKCS#7
  107. * uses the issuer's name and the issuing certificate serial number for
  108. * matching purposes. These must match the certificate issuer's name (not
  109. * subject's name) and the certificate serial number [RFC 2315 6.7].
  110. */
  111. static int pkcs7_find_key(struct pkcs7_message *pkcs7,
  112. struct pkcs7_signed_info *sinfo)
  113. {
  114. struct x509_certificate *x509;
  115. unsigned certix = 1;
  116. kenter("%u", sinfo->index);
  117. for (x509 = pkcs7->certs; x509; x509 = x509->next, certix++) {
  118. /* I'm _assuming_ that the generator of the PKCS#7 message will
  119. * encode the fields from the X.509 cert in the same way in the
  120. * PKCS#7 message - but I can't be 100% sure of that. It's
  121. * possible this will need element-by-element comparison.
  122. */
  123. if (!asymmetric_key_id_same(x509->id, sinfo->signing_cert_id))
  124. continue;
  125. pr_devel("Sig %u: Found cert serial match X.509[%u]\n",
  126. sinfo->index, certix);
  127. if (x509->pub->pkey_algo != sinfo->sig.pkey_algo) {
  128. pr_warn("Sig %u: X.509 algo and PKCS#7 sig algo don't match\n",
  129. sinfo->index);
  130. continue;
  131. }
  132. sinfo->signer = x509;
  133. return 0;
  134. }
  135. /* The relevant X.509 cert isn't found here, but it might be found in
  136. * the trust keyring.
  137. */
  138. pr_debug("Sig %u: Issuing X.509 cert not found (#%*phN)\n",
  139. sinfo->index,
  140. sinfo->signing_cert_id->len, sinfo->signing_cert_id->data);
  141. return 0;
  142. }
  143. /*
  144. * Verify the internal certificate chain as best we can.
  145. */
  146. static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,
  147. struct pkcs7_signed_info *sinfo)
  148. {
  149. struct x509_certificate *x509 = sinfo->signer, *p;
  150. int ret;
  151. kenter("");
  152. for (p = pkcs7->certs; p; p = p->next)
  153. p->seen = false;
  154. for (;;) {
  155. pr_debug("verify %s: %*phN\n",
  156. x509->subject,
  157. x509->raw_serial_size, x509->raw_serial);
  158. x509->seen = true;
  159. ret = x509_get_sig_params(x509);
  160. if (ret < 0)
  161. goto maybe_missing_crypto_in_x509;
  162. pr_debug("- issuer %s\n", x509->issuer);
  163. if (x509->authority)
  164. pr_debug("- authkeyid %*phN\n",
  165. x509->authority->len, x509->authority->data);
  166. if (!x509->authority ||
  167. strcmp(x509->subject, x509->issuer) == 0) {
  168. /* If there's no authority certificate specified, then
  169. * the certificate must be self-signed and is the root
  170. * of the chain. Likewise if the cert is its own
  171. * authority.
  172. */
  173. pr_debug("- no auth?\n");
  174. if (x509->raw_subject_size != x509->raw_issuer_size ||
  175. memcmp(x509->raw_subject, x509->raw_issuer,
  176. x509->raw_issuer_size) != 0)
  177. return 0;
  178. ret = x509_check_signature(x509->pub, x509);
  179. if (ret < 0)
  180. goto maybe_missing_crypto_in_x509;
  181. x509->signer = x509;
  182. pr_debug("- self-signed\n");
  183. return 0;
  184. }
  185. /* Look through the X.509 certificates in the PKCS#7 message's
  186. * list to see if the next one is there.
  187. */
  188. pr_debug("- want %*phN\n",
  189. x509->authority->len, x509->authority->data);
  190. for (p = pkcs7->certs; p; p = p->next) {
  191. if (!p->skid)
  192. continue;
  193. pr_debug("- cmp [%u] %*phN\n",
  194. p->index, p->skid->len, p->skid->data);
  195. if (asymmetric_key_id_same(p->skid, x509->authority))
  196. goto found_issuer;
  197. }
  198. /* We didn't find the root of this chain */
  199. pr_debug("- top\n");
  200. return 0;
  201. found_issuer:
  202. pr_debug("- subject %s\n", p->subject);
  203. if (p->seen) {
  204. pr_warn("Sig %u: X.509 chain contains loop\n",
  205. sinfo->index);
  206. return 0;
  207. }
  208. ret = x509_check_signature(p->pub, x509);
  209. if (ret < 0)
  210. return ret;
  211. x509->signer = p;
  212. if (x509 == p) {
  213. pr_debug("- self-signed\n");
  214. return 0;
  215. }
  216. x509 = p;
  217. might_sleep();
  218. }
  219. maybe_missing_crypto_in_x509:
  220. /* Just prune the certificate chain at this point if we lack some
  221. * crypto module to go further. Note, however, we don't want to set
  222. * sinfo->missing_crypto as the signed info block may still be
  223. * validatable against an X.509 cert lower in the chain that we have a
  224. * trusted copy of.
  225. */
  226. if (ret == -ENOPKG)
  227. return 0;
  228. return ret;
  229. }
  230. /*
  231. * Verify one signed information block from a PKCS#7 message.
  232. */
  233. static int pkcs7_verify_one(struct pkcs7_message *pkcs7,
  234. struct pkcs7_signed_info *sinfo)
  235. {
  236. int ret;
  237. kenter(",%u", sinfo->index);
  238. /* First of all, digest the data in the PKCS#7 message and the
  239. * signed information block
  240. */
  241. ret = pkcs7_digest(pkcs7, sinfo);
  242. if (ret < 0)
  243. return ret;
  244. /* Find the key for the signature if there is one */
  245. ret = pkcs7_find_key(pkcs7, sinfo);
  246. if (ret < 0)
  247. return ret;
  248. if (!sinfo->signer)
  249. return 0;
  250. pr_devel("Using X.509[%u] for sig %u\n",
  251. sinfo->signer->index, sinfo->index);
  252. /* Verify the PKCS#7 binary against the key */
  253. ret = public_key_verify_signature(sinfo->signer->pub, &sinfo->sig);
  254. if (ret < 0)
  255. return ret;
  256. pr_devel("Verified signature %u\n", sinfo->index);
  257. /* Verify the internal certificate chain */
  258. return pkcs7_verify_sig_chain(pkcs7, sinfo);
  259. }
  260. /**
  261. * pkcs7_verify - Verify a PKCS#7 message
  262. * @pkcs7: The PKCS#7 message to be verified
  263. *
  264. * Verify a PKCS#7 message is internally consistent - that is, the data digest
  265. * matches the digest in the AuthAttrs and any signature in the message or one
  266. * of the X.509 certificates it carries that matches another X.509 cert in the
  267. * message can be verified.
  268. *
  269. * This does not look to match the contents of the PKCS#7 message against any
  270. * external public keys.
  271. *
  272. * Returns, in order of descending priority:
  273. *
  274. * (*) -EKEYREJECTED if a signature failed to match for which we found an
  275. * appropriate X.509 certificate, or:
  276. *
  277. * (*) -EBADMSG if some part of the message was invalid, or:
  278. *
  279. * (*) -ENOPKG if none of the signature chains are verifiable because suitable
  280. * crypto modules couldn't be found, or:
  281. *
  282. * (*) 0 if all the signature chains that don't incur -ENOPKG can be verified
  283. * (note that a signature chain may be of zero length), or:
  284. */
  285. int pkcs7_verify(struct pkcs7_message *pkcs7)
  286. {
  287. struct pkcs7_signed_info *sinfo;
  288. struct x509_certificate *x509;
  289. int enopkg = -ENOPKG;
  290. int ret, n;
  291. kenter("");
  292. for (n = 0, x509 = pkcs7->certs; x509; x509 = x509->next, n++) {
  293. ret = x509_get_sig_params(x509);
  294. if (ret < 0)
  295. return ret;
  296. pr_debug("X.509[%u] %*phN\n",
  297. n, x509->authority->len, x509->authority->data);
  298. }
  299. for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {
  300. ret = pkcs7_verify_one(pkcs7, sinfo);
  301. if (ret < 0) {
  302. if (ret == -ENOPKG) {
  303. sinfo->unsupported_crypto = true;
  304. continue;
  305. }
  306. kleave(" = %d", ret);
  307. return ret;
  308. }
  309. enopkg = 0;
  310. }
  311. kleave(" = %d", enopkg);
  312. return enopkg;
  313. }
  314. EXPORT_SYMBOL_GPL(pkcs7_verify);