pkcs7_verify.c 17 KB

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