pkcs7_verify.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Verify the signature on a PKCS#7 message.
  3. *
  4. * Imported from crypto/asymmetric_keys/pkcs7_verify.c of linux 5.7
  5. * with modification marked as __UBOOT__.
  6. *
  7. * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
  8. * Written by David Howells (dhowells@redhat.com)
  9. */
  10. #define pr_fmt(fmt) "PKCS7: "fmt
  11. #ifdef __UBOOT__
  12. #include <image.h>
  13. #include <string.h>
  14. #include <linux/bitops.h>
  15. #include <linux/compat.h>
  16. #include <linux/asn1.h>
  17. #include <u-boot/hash-checksum.h>
  18. #include <crypto/public_key.h>
  19. #include <crypto/pkcs7_parser.h>
  20. #else
  21. #include <linux/kernel.h>
  22. #include <linux/export.h>
  23. #include <linux/slab.h>
  24. #include <linux/err.h>
  25. #include <linux/asn1.h>
  26. #include <crypto/hash.h>
  27. #include <crypto/hash_info.h>
  28. #include <crypto/public_key.h>
  29. #include "pkcs7_parser.h"
  30. #endif
  31. /*
  32. * pkcs7_digest - Digest the relevant parts of the PKCS#7 data
  33. * @pkcs7: PKCS7 Signed Data
  34. * @sinfo: PKCS7 Signed Info
  35. *
  36. * Digest the relevant parts of the PKCS#7 data, @pkcs7, using signature
  37. * information in @sinfo. But if there are authentication attributes,
  38. * i.e. signed image case, the digest must be calculated against
  39. * the authentication attributes.
  40. *
  41. * Return: 0 - on success, non-zero error code - otherwise
  42. */
  43. #ifdef __UBOOT__
  44. static int pkcs7_digest(struct pkcs7_message *pkcs7,
  45. struct pkcs7_signed_info *sinfo)
  46. {
  47. struct public_key_signature *sig = sinfo->sig;
  48. struct image_region regions[2];
  49. int ret = 0;
  50. /*
  51. * [RFC2315 9.3]
  52. * If the authenticated attributes are present,
  53. * the message-digest is calculated on the
  54. * attributes present in the
  55. * authenticatedAttributes field and not just
  56. * the contents field
  57. */
  58. if (!sinfo->authattrs && sig->digest)
  59. return 0;
  60. if (!sinfo->sig->hash_algo)
  61. return -ENOPKG;
  62. if (!strcmp(sinfo->sig->hash_algo, "sha256"))
  63. sig->digest_size = SHA256_SUM_LEN;
  64. else if (!strcmp(sinfo->sig->hash_algo, "sha384"))
  65. sig->digest_size = SHA384_SUM_LEN;
  66. else if (!strcmp(sinfo->sig->hash_algo, "sha512"))
  67. sig->digest_size = SHA512_SUM_LEN;
  68. else if (!strcmp(sinfo->sig->hash_algo, "sha1"))
  69. sig->digest_size = SHA1_SUM_LEN;
  70. else
  71. return -ENOPKG;
  72. /*
  73. * Calculate the hash only if the data is present.
  74. * In case of authenticated variable and capsule,
  75. * the hash has already been calculated on the
  76. * efi_image_regions and populated
  77. */
  78. if (pkcs7->data) {
  79. sig->digest = calloc(1, sig->digest_size);
  80. if (!sig->digest) {
  81. pr_warn("Sig %u: Out of memory\n", sinfo->index);
  82. return -ENOMEM;
  83. }
  84. regions[0].data = pkcs7->data;
  85. regions[0].size = pkcs7->data_len;
  86. /* Digest the message [RFC2315 9.3] */
  87. hash_calculate(sinfo->sig->hash_algo, regions, 1, sig->digest);
  88. }
  89. /* However, if there are authenticated attributes, there must be a
  90. * message digest attribute amongst them which corresponds to the
  91. * digest we just calculated.
  92. */
  93. if (sinfo->authattrs) {
  94. u8 tag;
  95. if (!sinfo->msgdigest) {
  96. pr_warn("Sig %u: No messageDigest\n", sinfo->index);
  97. ret = -EKEYREJECTED;
  98. goto error;
  99. }
  100. if (sinfo->msgdigest_len != sig->digest_size) {
  101. pr_debug("Sig %u: Invalid digest size (%u)\n",
  102. sinfo->index, sinfo->msgdigest_len);
  103. ret = -EBADMSG;
  104. goto error;
  105. }
  106. if (memcmp(sig->digest, sinfo->msgdigest,
  107. sinfo->msgdigest_len) != 0) {
  108. pr_debug("Sig %u: Message digest doesn't match\n",
  109. sinfo->index);
  110. ret = -EKEYREJECTED;
  111. goto error;
  112. }
  113. /* We then calculate anew, using the authenticated attributes
  114. * as the contents of the digest instead. Note that we need to
  115. * convert the attributes from a CONT.0 into a SET before we
  116. * hash it.
  117. */
  118. memset(sig->digest, 0, sig->digest_size);
  119. tag = 0x31;
  120. regions[0].data = &tag;
  121. regions[0].size = 1;
  122. regions[1].data = sinfo->authattrs;
  123. regions[1].size = sinfo->authattrs_len;
  124. hash_calculate(sinfo->sig->hash_algo, regions, 2, sig->digest);
  125. ret = 0;
  126. }
  127. error:
  128. return ret;
  129. }
  130. #else /* !__UBOOT__ */
  131. static int pkcs7_digest(struct pkcs7_message *pkcs7,
  132. struct pkcs7_signed_info *sinfo)
  133. {
  134. struct public_key_signature *sig = sinfo->sig;
  135. struct crypto_shash *tfm;
  136. struct shash_desc *desc;
  137. size_t desc_size;
  138. int ret;
  139. kenter(",%u,%s", sinfo->index, sinfo->sig->hash_algo);
  140. /* The digest was calculated already. */
  141. if (sig->digest)
  142. return 0;
  143. if (!sinfo->sig->hash_algo)
  144. return -ENOPKG;
  145. /* Allocate the hashing algorithm we're going to need and find out how
  146. * big the hash operational data will be.
  147. */
  148. tfm = crypto_alloc_shash(sinfo->sig->hash_algo, 0, 0);
  149. if (IS_ERR(tfm))
  150. return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);
  151. desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
  152. sig->digest_size = crypto_shash_digestsize(tfm);
  153. ret = -ENOMEM;
  154. sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);
  155. if (!sig->digest)
  156. goto error_no_desc;
  157. desc = kzalloc(desc_size, GFP_KERNEL);
  158. if (!desc)
  159. goto error_no_desc;
  160. desc->tfm = tfm;
  161. /* Digest the message [RFC2315 9.3] */
  162. ret = crypto_shash_digest(desc, pkcs7->data, pkcs7->data_len,
  163. sig->digest);
  164. if (ret < 0)
  165. goto error;
  166. pr_devel("MsgDigest = [%*ph]\n", 8, sig->digest);
  167. /* However, if there are authenticated attributes, there must be a
  168. * message digest attribute amongst them which corresponds to the
  169. * digest we just calculated.
  170. */
  171. if (sinfo->authattrs) {
  172. u8 tag;
  173. if (!sinfo->msgdigest) {
  174. pr_warn("Sig %u: No messageDigest\n", sinfo->index);
  175. ret = -EKEYREJECTED;
  176. goto error;
  177. }
  178. if (sinfo->msgdigest_len != sig->digest_size) {
  179. pr_debug("Sig %u: Invalid digest size (%u)\n",
  180. sinfo->index, sinfo->msgdigest_len);
  181. ret = -EBADMSG;
  182. goto error;
  183. }
  184. if (memcmp(sig->digest, sinfo->msgdigest,
  185. sinfo->msgdigest_len) != 0) {
  186. pr_debug("Sig %u: Message digest doesn't match\n",
  187. sinfo->index);
  188. ret = -EKEYREJECTED;
  189. goto error;
  190. }
  191. /* We then calculate anew, using the authenticated attributes
  192. * as the contents of the digest instead. Note that we need to
  193. * convert the attributes from a CONT.0 into a SET before we
  194. * hash it.
  195. */
  196. memset(sig->digest, 0, sig->digest_size);
  197. ret = crypto_shash_init(desc);
  198. if (ret < 0)
  199. goto error;
  200. tag = ASN1_CONS_BIT | ASN1_SET;
  201. ret = crypto_shash_update(desc, &tag, 1);
  202. if (ret < 0)
  203. goto error;
  204. ret = crypto_shash_finup(desc, sinfo->authattrs,
  205. sinfo->authattrs_len, sig->digest);
  206. if (ret < 0)
  207. goto error;
  208. pr_devel("AADigest = [%*ph]\n", 8, sig->digest);
  209. }
  210. error:
  211. kfree(desc);
  212. error_no_desc:
  213. crypto_free_shash(tfm);
  214. kleave(" = %d", ret);
  215. return ret;
  216. }
  217. int pkcs7_get_digest(struct pkcs7_message *pkcs7, const u8 **buf, u32 *len,
  218. enum hash_algo *hash_algo)
  219. {
  220. struct pkcs7_signed_info *sinfo = pkcs7->signed_infos;
  221. int i, ret;
  222. /*
  223. * This function doesn't support messages with more than one signature.
  224. */
  225. if (sinfo == NULL || sinfo->next != NULL)
  226. return -EBADMSG;
  227. ret = pkcs7_digest(pkcs7, sinfo);
  228. if (ret)
  229. return ret;
  230. *buf = sinfo->sig->digest;
  231. *len = sinfo->sig->digest_size;
  232. for (i = 0; i < HASH_ALGO__LAST; i++)
  233. if (!strcmp(hash_algo_name[i], sinfo->sig->hash_algo)) {
  234. *hash_algo = i;
  235. break;
  236. }
  237. return 0;
  238. }
  239. #endif /* !__UBOOT__ */
  240. /*
  241. * Find the key (X.509 certificate) to use to verify a PKCS#7 message. PKCS#7
  242. * uses the issuer's name and the issuing certificate serial number for
  243. * matching purposes. These must match the certificate issuer's name (not
  244. * subject's name) and the certificate serial number [RFC 2315 6.7].
  245. */
  246. static int pkcs7_find_key(struct pkcs7_message *pkcs7,
  247. struct pkcs7_signed_info *sinfo)
  248. {
  249. struct x509_certificate *x509;
  250. unsigned certix = 1;
  251. kenter("%u", sinfo->index);
  252. for (x509 = pkcs7->certs; x509; x509 = x509->next, certix++) {
  253. /* I'm _assuming_ that the generator of the PKCS#7 message will
  254. * encode the fields from the X.509 cert in the same way in the
  255. * PKCS#7 message - but I can't be 100% sure of that. It's
  256. * possible this will need element-by-element comparison.
  257. */
  258. if (!asymmetric_key_id_same(x509->id, sinfo->sig->auth_ids[0]))
  259. continue;
  260. pr_devel("Sig %u: Found cert serial match X.509[%u]\n",
  261. sinfo->index, certix);
  262. if (strcmp(x509->pub->pkey_algo, sinfo->sig->pkey_algo) != 0) {
  263. pr_warn("Sig %u: X.509 algo and PKCS#7 sig algo don't match\n",
  264. sinfo->index);
  265. continue;
  266. }
  267. sinfo->signer = x509;
  268. return 0;
  269. }
  270. /* The relevant X.509 cert isn't found here, but it might be found in
  271. * the trust keyring.
  272. */
  273. pr_debug("Sig %u: Issuing X.509 cert not found (#%*phN)\n",
  274. sinfo->index,
  275. sinfo->sig->auth_ids[0]->len, sinfo->sig->auth_ids[0]->data);
  276. return 0;
  277. }
  278. /*
  279. * pkcs7_verify_sig_chain - Verify the internal certificate chain as best
  280. * as we can.
  281. * @pkcs7: PKCS7 Signed Data
  282. * @sinfo: PKCS7 Signed Info
  283. * @signer: Singer's certificate
  284. *
  285. * Build up and verify the internal certificate chain against a signature
  286. * in @sinfo, using certificates contained in @pkcs7 as best as we can.
  287. * If the chain reaches the end, the last certificate will be returned
  288. * in @signer.
  289. *
  290. * Return: 0 - on success, non-zero error code - otherwise
  291. */
  292. #ifdef __UBOOT__
  293. static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,
  294. struct pkcs7_signed_info *sinfo,
  295. struct x509_certificate **signer)
  296. #else
  297. static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,
  298. struct pkcs7_signed_info *sinfo)
  299. #endif
  300. {
  301. struct public_key_signature *sig;
  302. struct x509_certificate *x509 = sinfo->signer, *p;
  303. struct asymmetric_key_id *auth;
  304. int ret;
  305. kenter("");
  306. *signer = NULL;
  307. for (p = pkcs7->certs; p; p = p->next)
  308. p->seen = false;
  309. for (;;) {
  310. pr_debug("verify %s: %*phN\n",
  311. x509->subject,
  312. x509->raw_serial_size, x509->raw_serial);
  313. x509->seen = true;
  314. if (x509->blacklisted) {
  315. /* If this cert is blacklisted, then mark everything
  316. * that depends on this as blacklisted too.
  317. */
  318. sinfo->blacklisted = true;
  319. for (p = sinfo->signer; p != x509; p = p->signer)
  320. p->blacklisted = true;
  321. pr_debug("- blacklisted\n");
  322. #ifdef __UBOOT__
  323. *signer = x509;
  324. #endif
  325. return 0;
  326. }
  327. if (x509->unsupported_key)
  328. goto unsupported_crypto_in_x509;
  329. pr_debug("- issuer %s\n", x509->issuer);
  330. sig = x509->sig;
  331. if (sig->auth_ids[0])
  332. pr_debug("- authkeyid.id %*phN\n",
  333. sig->auth_ids[0]->len, sig->auth_ids[0]->data);
  334. if (sig->auth_ids[1])
  335. pr_debug("- authkeyid.skid %*phN\n",
  336. sig->auth_ids[1]->len, sig->auth_ids[1]->data);
  337. if (x509->self_signed) {
  338. /* If there's no authority certificate specified, then
  339. * the certificate must be self-signed and is the root
  340. * of the chain. Likewise if the cert is its own
  341. * authority.
  342. */
  343. if (x509->unsupported_sig)
  344. goto unsupported_crypto_in_x509;
  345. x509->signer = x509;
  346. pr_debug("- self-signed\n");
  347. #ifdef __UBOOT__
  348. *signer = x509;
  349. #endif
  350. return 0;
  351. }
  352. /* Look through the X.509 certificates in the PKCS#7 message's
  353. * list to see if the next one is there.
  354. */
  355. auth = sig->auth_ids[0];
  356. if (auth) {
  357. pr_debug("- want %*phN\n", auth->len, auth->data);
  358. for (p = pkcs7->certs; p; p = p->next) {
  359. pr_debug("- cmp [%u] %*phN\n",
  360. p->index, p->id->len, p->id->data);
  361. if (asymmetric_key_id_same(p->id, auth))
  362. goto found_issuer_check_skid;
  363. }
  364. } else if (sig->auth_ids[1]) {
  365. auth = sig->auth_ids[1];
  366. pr_debug("- want %*phN\n", auth->len, auth->data);
  367. for (p = pkcs7->certs; p; p = p->next) {
  368. if (!p->skid)
  369. continue;
  370. pr_debug("- cmp [%u] %*phN\n",
  371. p->index, p->skid->len, p->skid->data);
  372. if (asymmetric_key_id_same(p->skid, auth))
  373. goto found_issuer;
  374. }
  375. }
  376. /* We didn't find the root of this chain */
  377. pr_debug("- top\n");
  378. #ifdef __UBOOT__
  379. *signer = x509;
  380. #endif
  381. return 0;
  382. found_issuer_check_skid:
  383. /* We matched issuer + serialNumber, but if there's an
  384. * authKeyId.keyId, that must match the CA subjKeyId also.
  385. */
  386. if (sig->auth_ids[1] &&
  387. !asymmetric_key_id_same(p->skid, sig->auth_ids[1])) {
  388. pr_warn("Sig %u: X.509 chain contains auth-skid nonmatch (%u->%u)\n",
  389. sinfo->index, x509->index, p->index);
  390. return -EKEYREJECTED;
  391. }
  392. found_issuer:
  393. pr_debug("- subject %s\n", p->subject);
  394. if (p->seen) {
  395. pr_warn("Sig %u: X.509 chain contains loop\n",
  396. sinfo->index);
  397. #ifdef __UBOOT__
  398. *signer = p;
  399. #endif
  400. return 0;
  401. }
  402. ret = public_key_verify_signature(p->pub, x509->sig);
  403. if (ret < 0)
  404. return ret;
  405. x509->signer = p;
  406. if (x509 == p) {
  407. pr_debug("- self-signed\n");
  408. #ifdef __UBOOT__
  409. *signer = p;
  410. #endif
  411. return 0;
  412. }
  413. x509 = p;
  414. #ifndef __UBOOT__
  415. might_sleep();
  416. #endif
  417. }
  418. unsupported_crypto_in_x509:
  419. /* Just prune the certificate chain at this point if we lack some
  420. * crypto module to go further. Note, however, we don't want to set
  421. * sinfo->unsupported_crypto as the signed info block may still be
  422. * validatable against an X.509 cert lower in the chain that we have a
  423. * trusted copy of.
  424. */
  425. return 0;
  426. }
  427. /*
  428. * pkcs7_verify_one - Verify one signed information block from a PKCS#7
  429. * message.
  430. * @pkcs7: PKCS7 Signed Data
  431. * @sinfo: PKCS7 Signed Info
  432. * @signer: Signer's certificate
  433. *
  434. * Verify one signature in @sinfo and follow the certificate chain.
  435. * If the chain reaches the end, the last certificate will be returned
  436. * in @signer.
  437. *
  438. * Return: 0 - on success, non-zero error code - otherwise
  439. */
  440. #ifdef __UBOOT__
  441. int pkcs7_verify_one(struct pkcs7_message *pkcs7,
  442. struct pkcs7_signed_info *sinfo,
  443. struct x509_certificate **signer)
  444. #else
  445. static int pkcs7_verify_one(struct pkcs7_message *pkcs7,
  446. struct pkcs7_signed_info *sinfo)
  447. #endif
  448. {
  449. int ret;
  450. kenter(",%u", sinfo->index);
  451. /* First of all, digest the data in the PKCS#7 message and the
  452. * signed information block
  453. */
  454. ret = pkcs7_digest(pkcs7, sinfo);
  455. if (ret < 0)
  456. return ret;
  457. /* Find the key for the signature if there is one */
  458. ret = pkcs7_find_key(pkcs7, sinfo);
  459. if (ret < 0)
  460. return ret;
  461. if (!sinfo->signer)
  462. return 0;
  463. pr_devel("Using X.509[%u] for sig %u\n",
  464. sinfo->signer->index, sinfo->index);
  465. /* Check that the PKCS#7 signing time is valid according to the X.509
  466. * certificate. We can't, however, check against the system clock
  467. * since that may not have been set yet and may be wrong.
  468. */
  469. if (test_bit(sinfo_has_signing_time, &sinfo->aa_set)) {
  470. if (sinfo->signing_time < sinfo->signer->valid_from ||
  471. sinfo->signing_time > sinfo->signer->valid_to) {
  472. pr_warn("Message signed outside of X.509 validity window\n");
  473. return -EKEYREJECTED;
  474. }
  475. }
  476. /* Verify the PKCS#7 binary against the key */
  477. ret = public_key_verify_signature(sinfo->signer->pub, sinfo->sig);
  478. if (ret < 0)
  479. return ret;
  480. pr_devel("Verified signature %u\n", sinfo->index);
  481. /* Verify the internal certificate chain */
  482. return pkcs7_verify_sig_chain(pkcs7, sinfo, signer);
  483. }
  484. #ifndef __UBOOT__
  485. /**
  486. * pkcs7_verify - Verify a PKCS#7 message
  487. * @pkcs7: The PKCS#7 message to be verified
  488. * @usage: The use to which the key is being put
  489. *
  490. * Verify a PKCS#7 message is internally consistent - that is, the data digest
  491. * matches the digest in the AuthAttrs and any signature in the message or one
  492. * of the X.509 certificates it carries that matches another X.509 cert in the
  493. * message can be verified.
  494. *
  495. * This does not look to match the contents of the PKCS#7 message against any
  496. * external public keys.
  497. *
  498. * Returns, in order of descending priority:
  499. *
  500. * (*) -EKEYREJECTED if a key was selected that had a usage restriction at
  501. * odds with the specified usage, or:
  502. *
  503. * (*) -EKEYREJECTED if a signature failed to match for which we found an
  504. * appropriate X.509 certificate, or:
  505. *
  506. * (*) -EBADMSG if some part of the message was invalid, or:
  507. *
  508. * (*) 0 if a signature chain passed verification, or:
  509. *
  510. * (*) -EKEYREJECTED if a blacklisted key was encountered, or:
  511. *
  512. * (*) -ENOPKG if none of the signature chains are verifiable because suitable
  513. * crypto modules couldn't be found.
  514. */
  515. int pkcs7_verify(struct pkcs7_message *pkcs7,
  516. enum key_being_used_for usage)
  517. {
  518. struct pkcs7_signed_info *sinfo;
  519. int actual_ret = -ENOPKG;
  520. int ret;
  521. kenter("");
  522. switch (usage) {
  523. case VERIFYING_MODULE_SIGNATURE:
  524. if (pkcs7->data_type != OID_data) {
  525. pr_warn("Invalid module sig (not pkcs7-data)\n");
  526. return -EKEYREJECTED;
  527. }
  528. if (pkcs7->have_authattrs) {
  529. pr_warn("Invalid module sig (has authattrs)\n");
  530. return -EKEYREJECTED;
  531. }
  532. break;
  533. case VERIFYING_FIRMWARE_SIGNATURE:
  534. if (pkcs7->data_type != OID_data) {
  535. pr_warn("Invalid firmware sig (not pkcs7-data)\n");
  536. return -EKEYREJECTED;
  537. }
  538. if (!pkcs7->have_authattrs) {
  539. pr_warn("Invalid firmware sig (missing authattrs)\n");
  540. return -EKEYREJECTED;
  541. }
  542. break;
  543. case VERIFYING_KEXEC_PE_SIGNATURE:
  544. if (pkcs7->data_type != OID_msIndirectData) {
  545. pr_warn("Invalid kexec sig (not Authenticode)\n");
  546. return -EKEYREJECTED;
  547. }
  548. /* Authattr presence checked in parser */
  549. break;
  550. case VERIFYING_UNSPECIFIED_SIGNATURE:
  551. if (pkcs7->data_type != OID_data) {
  552. pr_warn("Invalid unspecified sig (not pkcs7-data)\n");
  553. return -EKEYREJECTED;
  554. }
  555. break;
  556. default:
  557. return -EINVAL;
  558. }
  559. for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {
  560. ret = pkcs7_verify_one(pkcs7, sinfo);
  561. if (sinfo->blacklisted) {
  562. if (actual_ret == -ENOPKG)
  563. actual_ret = -EKEYREJECTED;
  564. continue;
  565. }
  566. if (ret < 0) {
  567. if (ret == -ENOPKG) {
  568. sinfo->unsupported_crypto = true;
  569. continue;
  570. }
  571. kleave(" = %d", ret);
  572. return ret;
  573. }
  574. actual_ret = 0;
  575. }
  576. kleave(" = %d", actual_ret);
  577. return actual_ret;
  578. }
  579. EXPORT_SYMBOL_GPL(pkcs7_verify);
  580. /**
  581. * pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message
  582. * @pkcs7: The PKCS#7 message
  583. * @data: The data to be verified
  584. * @datalen: The amount of data
  585. *
  586. * Supply the detached data needed to verify a PKCS#7 message. Note that no
  587. * attempt to retain/pin the data is made. That is left to the caller. The
  588. * data will not be modified by pkcs7_verify() and will not be freed when the
  589. * PKCS#7 message is freed.
  590. *
  591. * Returns -EINVAL if data is already supplied in the message, 0 otherwise.
  592. */
  593. int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,
  594. const void *data, size_t datalen)
  595. {
  596. if (pkcs7->data) {
  597. pr_debug("Data already supplied\n");
  598. return -EINVAL;
  599. }
  600. pkcs7->data = data;
  601. pkcs7->data_len = datalen;
  602. return 0;
  603. }
  604. #endif /* __UBOOT__ */