pkcs7_parser.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* PKCS#7 parser
  3. *
  4. * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #define pr_fmt(fmt) "PKCS7: "fmt
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/export.h>
  11. #include <linux/slab.h>
  12. #include <linux/err.h>
  13. #include <linux/oid_registry.h>
  14. #include <crypto/public_key.h>
  15. #include "pkcs7_parser.h"
  16. #include "pkcs7.asn1.h"
  17. MODULE_DESCRIPTION("PKCS#7 parser");
  18. MODULE_AUTHOR("Red Hat, Inc.");
  19. MODULE_LICENSE("GPL");
  20. struct pkcs7_parse_context {
  21. struct pkcs7_message *msg; /* Message being constructed */
  22. struct pkcs7_signed_info *sinfo; /* SignedInfo being constructed */
  23. struct pkcs7_signed_info **ppsinfo;
  24. struct x509_certificate *certs; /* Certificate cache */
  25. struct x509_certificate **ppcerts;
  26. unsigned long data; /* Start of data */
  27. enum OID last_oid; /* Last OID encountered */
  28. unsigned x509_index;
  29. unsigned sinfo_index;
  30. const void *raw_serial;
  31. unsigned raw_serial_size;
  32. unsigned raw_issuer_size;
  33. const void *raw_issuer;
  34. const void *raw_skid;
  35. unsigned raw_skid_size;
  36. bool expect_skid;
  37. };
  38. /*
  39. * Free a signed information block.
  40. */
  41. static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo)
  42. {
  43. if (sinfo) {
  44. public_key_signature_free(sinfo->sig);
  45. kfree(sinfo);
  46. }
  47. }
  48. /**
  49. * pkcs7_free_message - Free a PKCS#7 message
  50. * @pkcs7: The PKCS#7 message to free
  51. */
  52. void pkcs7_free_message(struct pkcs7_message *pkcs7)
  53. {
  54. struct x509_certificate *cert;
  55. struct pkcs7_signed_info *sinfo;
  56. if (pkcs7) {
  57. while (pkcs7->certs) {
  58. cert = pkcs7->certs;
  59. pkcs7->certs = cert->next;
  60. x509_free_certificate(cert);
  61. }
  62. while (pkcs7->crl) {
  63. cert = pkcs7->crl;
  64. pkcs7->crl = cert->next;
  65. x509_free_certificate(cert);
  66. }
  67. while (pkcs7->signed_infos) {
  68. sinfo = pkcs7->signed_infos;
  69. pkcs7->signed_infos = sinfo->next;
  70. pkcs7_free_signed_info(sinfo);
  71. }
  72. kfree(pkcs7);
  73. }
  74. }
  75. EXPORT_SYMBOL_GPL(pkcs7_free_message);
  76. /*
  77. * Check authenticatedAttributes are provided or not provided consistently.
  78. */
  79. static int pkcs7_check_authattrs(struct pkcs7_message *msg)
  80. {
  81. struct pkcs7_signed_info *sinfo;
  82. bool want = false;
  83. sinfo = msg->signed_infos;
  84. if (!sinfo)
  85. goto inconsistent;
  86. if (sinfo->authattrs) {
  87. want = true;
  88. msg->have_authattrs = true;
  89. }
  90. for (sinfo = sinfo->next; sinfo; sinfo = sinfo->next)
  91. if (!!sinfo->authattrs != want)
  92. goto inconsistent;
  93. return 0;
  94. inconsistent:
  95. pr_warn("Inconsistently supplied authAttrs\n");
  96. return -EINVAL;
  97. }
  98. /**
  99. * pkcs7_parse_message - Parse a PKCS#7 message
  100. * @data: The raw binary ASN.1 encoded message to be parsed
  101. * @datalen: The size of the encoded message
  102. */
  103. struct pkcs7_message *pkcs7_parse_message(const void *data, size_t datalen)
  104. {
  105. struct pkcs7_parse_context *ctx;
  106. struct pkcs7_message *msg = ERR_PTR(-ENOMEM);
  107. int ret;
  108. ctx = kzalloc(sizeof(struct pkcs7_parse_context), GFP_KERNEL);
  109. if (!ctx)
  110. goto out_no_ctx;
  111. ctx->msg = kzalloc(sizeof(struct pkcs7_message), GFP_KERNEL);
  112. if (!ctx->msg)
  113. goto out_no_msg;
  114. ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
  115. if (!ctx->sinfo)
  116. goto out_no_sinfo;
  117. ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature),
  118. GFP_KERNEL);
  119. if (!ctx->sinfo->sig)
  120. goto out_no_sig;
  121. ctx->data = (unsigned long)data;
  122. ctx->ppcerts = &ctx->certs;
  123. ctx->ppsinfo = &ctx->msg->signed_infos;
  124. /* Attempt to decode the signature */
  125. ret = asn1_ber_decoder(&pkcs7_decoder, ctx, data, datalen);
  126. if (ret < 0) {
  127. msg = ERR_PTR(ret);
  128. goto out;
  129. }
  130. ret = pkcs7_check_authattrs(ctx->msg);
  131. if (ret < 0) {
  132. msg = ERR_PTR(ret);
  133. goto out;
  134. }
  135. msg = ctx->msg;
  136. ctx->msg = NULL;
  137. out:
  138. while (ctx->certs) {
  139. struct x509_certificate *cert = ctx->certs;
  140. ctx->certs = cert->next;
  141. x509_free_certificate(cert);
  142. }
  143. out_no_sig:
  144. pkcs7_free_signed_info(ctx->sinfo);
  145. out_no_sinfo:
  146. pkcs7_free_message(ctx->msg);
  147. out_no_msg:
  148. kfree(ctx);
  149. out_no_ctx:
  150. return msg;
  151. }
  152. EXPORT_SYMBOL_GPL(pkcs7_parse_message);
  153. /**
  154. * pkcs7_get_content_data - Get access to the PKCS#7 content
  155. * @pkcs7: The preparsed PKCS#7 message to access
  156. * @_data: Place to return a pointer to the data
  157. * @_data_len: Place to return the data length
  158. * @_headerlen: Size of ASN.1 header not included in _data
  159. *
  160. * Get access to the data content of the PKCS#7 message. The size of the
  161. * header of the ASN.1 object that contains it is also provided and can be used
  162. * to adjust *_data and *_data_len to get the entire object.
  163. *
  164. * Returns -ENODATA if the data object was missing from the message.
  165. */
  166. int pkcs7_get_content_data(const struct pkcs7_message *pkcs7,
  167. const void **_data, size_t *_data_len,
  168. size_t *_headerlen)
  169. {
  170. if (!pkcs7->data)
  171. return -ENODATA;
  172. *_data = pkcs7->data;
  173. *_data_len = pkcs7->data_len;
  174. if (_headerlen)
  175. *_headerlen = pkcs7->data_hdrlen;
  176. return 0;
  177. }
  178. EXPORT_SYMBOL_GPL(pkcs7_get_content_data);
  179. /*
  180. * Note an OID when we find one for later processing when we know how
  181. * to interpret it.
  182. */
  183. int pkcs7_note_OID(void *context, size_t hdrlen,
  184. unsigned char tag,
  185. const void *value, size_t vlen)
  186. {
  187. struct pkcs7_parse_context *ctx = context;
  188. ctx->last_oid = look_up_OID(value, vlen);
  189. if (ctx->last_oid == OID__NR) {
  190. char buffer[50];
  191. sprint_oid(value, vlen, buffer, sizeof(buffer));
  192. printk("PKCS7: Unknown OID: [%lu] %s\n",
  193. (unsigned long)value - ctx->data, buffer);
  194. }
  195. return 0;
  196. }
  197. /*
  198. * Note the digest algorithm for the signature.
  199. */
  200. int pkcs7_sig_note_digest_algo(void *context, size_t hdrlen,
  201. unsigned char tag,
  202. const void *value, size_t vlen)
  203. {
  204. struct pkcs7_parse_context *ctx = context;
  205. switch (ctx->last_oid) {
  206. case OID_sha1:
  207. ctx->sinfo->sig->hash_algo = "sha1";
  208. break;
  209. case OID_sha256:
  210. ctx->sinfo->sig->hash_algo = "sha256";
  211. break;
  212. case OID_sha384:
  213. ctx->sinfo->sig->hash_algo = "sha384";
  214. break;
  215. case OID_sha512:
  216. ctx->sinfo->sig->hash_algo = "sha512";
  217. break;
  218. case OID_sha224:
  219. ctx->sinfo->sig->hash_algo = "sha224";
  220. break;
  221. case OID_sm3:
  222. ctx->sinfo->sig->hash_algo = "sm3";
  223. break;
  224. case OID_gost2012Digest256:
  225. ctx->sinfo->sig->hash_algo = "streebog256";
  226. break;
  227. case OID_gost2012Digest512:
  228. ctx->sinfo->sig->hash_algo = "streebog512";
  229. break;
  230. case OID_sha3_256:
  231. ctx->sinfo->sig->hash_algo = "sha3-256";
  232. break;
  233. case OID_sha3_384:
  234. ctx->sinfo->sig->hash_algo = "sha3-384";
  235. break;
  236. case OID_sha3_512:
  237. ctx->sinfo->sig->hash_algo = "sha3-512";
  238. break;
  239. default:
  240. printk("Unsupported digest algo: %u\n", ctx->last_oid);
  241. return -ENOPKG;
  242. }
  243. return 0;
  244. }
  245. /*
  246. * Note the public key algorithm for the signature.
  247. */
  248. int pkcs7_sig_note_pkey_algo(void *context, size_t hdrlen,
  249. unsigned char tag,
  250. const void *value, size_t vlen)
  251. {
  252. struct pkcs7_parse_context *ctx = context;
  253. switch (ctx->last_oid) {
  254. case OID_rsaEncryption:
  255. ctx->sinfo->sig->pkey_algo = "rsa";
  256. ctx->sinfo->sig->encoding = "pkcs1";
  257. break;
  258. case OID_id_ecdsa_with_sha1:
  259. case OID_id_ecdsa_with_sha224:
  260. case OID_id_ecdsa_with_sha256:
  261. case OID_id_ecdsa_with_sha384:
  262. case OID_id_ecdsa_with_sha512:
  263. case OID_id_ecdsa_with_sha3_256:
  264. case OID_id_ecdsa_with_sha3_384:
  265. case OID_id_ecdsa_with_sha3_512:
  266. ctx->sinfo->sig->pkey_algo = "ecdsa";
  267. ctx->sinfo->sig->encoding = "x962";
  268. break;
  269. case OID_gost2012PKey256:
  270. case OID_gost2012PKey512:
  271. ctx->sinfo->sig->pkey_algo = "ecrdsa";
  272. ctx->sinfo->sig->encoding = "raw";
  273. break;
  274. default:
  275. printk("Unsupported pkey algo: %u\n", ctx->last_oid);
  276. return -ENOPKG;
  277. }
  278. return 0;
  279. }
  280. /*
  281. * We only support signed data [RFC2315 sec 9].
  282. */
  283. int pkcs7_check_content_type(void *context, size_t hdrlen,
  284. unsigned char tag,
  285. const void *value, size_t vlen)
  286. {
  287. struct pkcs7_parse_context *ctx = context;
  288. if (ctx->last_oid != OID_signed_data) {
  289. pr_warn("Only support pkcs7_signedData type\n");
  290. return -EINVAL;
  291. }
  292. return 0;
  293. }
  294. /*
  295. * Note the SignedData version
  296. */
  297. int pkcs7_note_signeddata_version(void *context, size_t hdrlen,
  298. unsigned char tag,
  299. const void *value, size_t vlen)
  300. {
  301. struct pkcs7_parse_context *ctx = context;
  302. unsigned version;
  303. if (vlen != 1)
  304. goto unsupported;
  305. ctx->msg->version = version = *(const u8 *)value;
  306. switch (version) {
  307. case 1:
  308. /* PKCS#7 SignedData [RFC2315 sec 9.1]
  309. * CMS ver 1 SignedData [RFC5652 sec 5.1]
  310. */
  311. break;
  312. case 3:
  313. /* CMS ver 3 SignedData [RFC2315 sec 5.1] */
  314. break;
  315. default:
  316. goto unsupported;
  317. }
  318. return 0;
  319. unsupported:
  320. pr_warn("Unsupported SignedData version\n");
  321. return -EINVAL;
  322. }
  323. /*
  324. * Note the SignerInfo version
  325. */
  326. int pkcs7_note_signerinfo_version(void *context, size_t hdrlen,
  327. unsigned char tag,
  328. const void *value, size_t vlen)
  329. {
  330. struct pkcs7_parse_context *ctx = context;
  331. unsigned version;
  332. if (vlen != 1)
  333. goto unsupported;
  334. version = *(const u8 *)value;
  335. switch (version) {
  336. case 1:
  337. /* PKCS#7 SignerInfo [RFC2315 sec 9.2]
  338. * CMS ver 1 SignerInfo [RFC5652 sec 5.3]
  339. */
  340. if (ctx->msg->version != 1)
  341. goto version_mismatch;
  342. ctx->expect_skid = false;
  343. break;
  344. case 3:
  345. /* CMS ver 3 SignerInfo [RFC2315 sec 5.3] */
  346. if (ctx->msg->version == 1)
  347. goto version_mismatch;
  348. ctx->expect_skid = true;
  349. break;
  350. default:
  351. goto unsupported;
  352. }
  353. return 0;
  354. unsupported:
  355. pr_warn("Unsupported SignerInfo version\n");
  356. return -EINVAL;
  357. version_mismatch:
  358. pr_warn("SignedData-SignerInfo version mismatch\n");
  359. return -EBADMSG;
  360. }
  361. /*
  362. * Extract a certificate and store it in the context.
  363. */
  364. int pkcs7_extract_cert(void *context, size_t hdrlen,
  365. unsigned char tag,
  366. const void *value, size_t vlen)
  367. {
  368. struct pkcs7_parse_context *ctx = context;
  369. struct x509_certificate *x509;
  370. if (tag != ((ASN1_UNIV << 6) | ASN1_CONS_BIT | ASN1_SEQ)) {
  371. pr_debug("Cert began with tag %02x at %lu\n",
  372. tag, (unsigned long)ctx - ctx->data);
  373. return -EBADMSG;
  374. }
  375. /* We have to correct for the header so that the X.509 parser can start
  376. * from the beginning. Note that since X.509 stipulates DER, there
  377. * probably shouldn't be an EOC trailer - but it is in PKCS#7 (which
  378. * stipulates BER).
  379. */
  380. value -= hdrlen;
  381. vlen += hdrlen;
  382. if (((u8*)value)[1] == 0x80)
  383. vlen += 2; /* Indefinite length - there should be an EOC */
  384. x509 = x509_cert_parse(value, vlen);
  385. if (IS_ERR(x509))
  386. return PTR_ERR(x509);
  387. x509->index = ++ctx->x509_index;
  388. pr_debug("Got cert %u for %s\n", x509->index, x509->subject);
  389. pr_debug("- fingerprint %*phN\n", x509->id->len, x509->id->data);
  390. *ctx->ppcerts = x509;
  391. ctx->ppcerts = &x509->next;
  392. return 0;
  393. }
  394. /*
  395. * Save the certificate list
  396. */
  397. int pkcs7_note_certificate_list(void *context, size_t hdrlen,
  398. unsigned char tag,
  399. const void *value, size_t vlen)
  400. {
  401. struct pkcs7_parse_context *ctx = context;
  402. pr_devel("Got cert list (%02x)\n", tag);
  403. *ctx->ppcerts = ctx->msg->certs;
  404. ctx->msg->certs = ctx->certs;
  405. ctx->certs = NULL;
  406. ctx->ppcerts = &ctx->certs;
  407. return 0;
  408. }
  409. /*
  410. * Note the content type.
  411. */
  412. int pkcs7_note_content(void *context, size_t hdrlen,
  413. unsigned char tag,
  414. const void *value, size_t vlen)
  415. {
  416. struct pkcs7_parse_context *ctx = context;
  417. if (ctx->last_oid != OID_data &&
  418. ctx->last_oid != OID_msIndirectData) {
  419. pr_warn("Unsupported data type %d\n", ctx->last_oid);
  420. return -EINVAL;
  421. }
  422. ctx->msg->data_type = ctx->last_oid;
  423. return 0;
  424. }
  425. /*
  426. * Extract the data from the message and store that and its content type OID in
  427. * the context.
  428. */
  429. int pkcs7_note_data(void *context, size_t hdrlen,
  430. unsigned char tag,
  431. const void *value, size_t vlen)
  432. {
  433. struct pkcs7_parse_context *ctx = context;
  434. pr_debug("Got data\n");
  435. ctx->msg->data = value;
  436. ctx->msg->data_len = vlen;
  437. ctx->msg->data_hdrlen = hdrlen;
  438. return 0;
  439. }
  440. /*
  441. * Parse authenticated attributes.
  442. */
  443. int pkcs7_sig_note_authenticated_attr(void *context, size_t hdrlen,
  444. unsigned char tag,
  445. const void *value, size_t vlen)
  446. {
  447. struct pkcs7_parse_context *ctx = context;
  448. struct pkcs7_signed_info *sinfo = ctx->sinfo;
  449. enum OID content_type;
  450. pr_devel("AuthAttr: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
  451. switch (ctx->last_oid) {
  452. case OID_contentType:
  453. if (__test_and_set_bit(sinfo_has_content_type, &sinfo->aa_set))
  454. goto repeated;
  455. content_type = look_up_OID(value, vlen);
  456. if (content_type != ctx->msg->data_type) {
  457. pr_warn("Mismatch between global data type (%d) and sinfo %u (%d)\n",
  458. ctx->msg->data_type, sinfo->index,
  459. content_type);
  460. return -EBADMSG;
  461. }
  462. return 0;
  463. case OID_signingTime:
  464. if (__test_and_set_bit(sinfo_has_signing_time, &sinfo->aa_set))
  465. goto repeated;
  466. /* Should we check that the signing time is consistent
  467. * with the signer's X.509 cert?
  468. */
  469. return x509_decode_time(&sinfo->signing_time,
  470. hdrlen, tag, value, vlen);
  471. case OID_messageDigest:
  472. if (__test_and_set_bit(sinfo_has_message_digest, &sinfo->aa_set))
  473. goto repeated;
  474. if (tag != ASN1_OTS)
  475. return -EBADMSG;
  476. sinfo->msgdigest = value;
  477. sinfo->msgdigest_len = vlen;
  478. return 0;
  479. case OID_smimeCapabilites:
  480. if (__test_and_set_bit(sinfo_has_smime_caps, &sinfo->aa_set))
  481. goto repeated;
  482. if (ctx->msg->data_type != OID_msIndirectData) {
  483. pr_warn("S/MIME Caps only allowed with Authenticode\n");
  484. return -EKEYREJECTED;
  485. }
  486. return 0;
  487. /* Microsoft SpOpusInfo seems to be contain cont[0] 16-bit BE
  488. * char URLs and cont[1] 8-bit char URLs.
  489. *
  490. * Microsoft StatementType seems to contain a list of OIDs that
  491. * are also used as extendedKeyUsage types in X.509 certs.
  492. */
  493. case OID_msSpOpusInfo:
  494. if (__test_and_set_bit(sinfo_has_ms_opus_info, &sinfo->aa_set))
  495. goto repeated;
  496. goto authenticode_check;
  497. case OID_msStatementType:
  498. if (__test_and_set_bit(sinfo_has_ms_statement_type, &sinfo->aa_set))
  499. goto repeated;
  500. authenticode_check:
  501. if (ctx->msg->data_type != OID_msIndirectData) {
  502. pr_warn("Authenticode AuthAttrs only allowed with Authenticode\n");
  503. return -EKEYREJECTED;
  504. }
  505. /* I'm not sure how to validate these */
  506. return 0;
  507. default:
  508. return 0;
  509. }
  510. repeated:
  511. /* We permit max one item per AuthenticatedAttribute and no repeats */
  512. pr_warn("Repeated/multivalue AuthAttrs not permitted\n");
  513. return -EKEYREJECTED;
  514. }
  515. /*
  516. * Note the set of auth attributes for digestion purposes [RFC2315 sec 9.3]
  517. */
  518. int pkcs7_sig_note_set_of_authattrs(void *context, size_t hdrlen,
  519. unsigned char tag,
  520. const void *value, size_t vlen)
  521. {
  522. struct pkcs7_parse_context *ctx = context;
  523. struct pkcs7_signed_info *sinfo = ctx->sinfo;
  524. if (!test_bit(sinfo_has_content_type, &sinfo->aa_set) ||
  525. !test_bit(sinfo_has_message_digest, &sinfo->aa_set)) {
  526. pr_warn("Missing required AuthAttr\n");
  527. return -EBADMSG;
  528. }
  529. if (ctx->msg->data_type != OID_msIndirectData &&
  530. test_bit(sinfo_has_ms_opus_info, &sinfo->aa_set)) {
  531. pr_warn("Unexpected Authenticode AuthAttr\n");
  532. return -EBADMSG;
  533. }
  534. /* We need to switch the 'CONT 0' to a 'SET OF' when we digest */
  535. sinfo->authattrs = value - (hdrlen - 1);
  536. sinfo->authattrs_len = vlen + (hdrlen - 1);
  537. return 0;
  538. }
  539. /*
  540. * Note the issuing certificate serial number
  541. */
  542. int pkcs7_sig_note_serial(void *context, size_t hdrlen,
  543. unsigned char tag,
  544. const void *value, size_t vlen)
  545. {
  546. struct pkcs7_parse_context *ctx = context;
  547. ctx->raw_serial = value;
  548. ctx->raw_serial_size = vlen;
  549. return 0;
  550. }
  551. /*
  552. * Note the issuer's name
  553. */
  554. int pkcs7_sig_note_issuer(void *context, size_t hdrlen,
  555. unsigned char tag,
  556. const void *value, size_t vlen)
  557. {
  558. struct pkcs7_parse_context *ctx = context;
  559. ctx->raw_issuer = value;
  560. ctx->raw_issuer_size = vlen;
  561. return 0;
  562. }
  563. /*
  564. * Note the issuing cert's subjectKeyIdentifier
  565. */
  566. int pkcs7_sig_note_skid(void *context, size_t hdrlen,
  567. unsigned char tag,
  568. const void *value, size_t vlen)
  569. {
  570. struct pkcs7_parse_context *ctx = context;
  571. pr_devel("SKID: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
  572. ctx->raw_skid = value;
  573. ctx->raw_skid_size = vlen;
  574. return 0;
  575. }
  576. /*
  577. * Note the signature data
  578. */
  579. int pkcs7_sig_note_signature(void *context, size_t hdrlen,
  580. unsigned char tag,
  581. const void *value, size_t vlen)
  582. {
  583. struct pkcs7_parse_context *ctx = context;
  584. ctx->sinfo->sig->s = kmemdup(value, vlen, GFP_KERNEL);
  585. if (!ctx->sinfo->sig->s)
  586. return -ENOMEM;
  587. ctx->sinfo->sig->s_size = vlen;
  588. return 0;
  589. }
  590. /*
  591. * Note a signature information block
  592. */
  593. int pkcs7_note_signed_info(void *context, size_t hdrlen,
  594. unsigned char tag,
  595. const void *value, size_t vlen)
  596. {
  597. struct pkcs7_parse_context *ctx = context;
  598. struct pkcs7_signed_info *sinfo = ctx->sinfo;
  599. struct asymmetric_key_id *kid;
  600. if (ctx->msg->data_type == OID_msIndirectData && !sinfo->authattrs) {
  601. pr_warn("Authenticode requires AuthAttrs\n");
  602. return -EBADMSG;
  603. }
  604. /* Generate cert issuer + serial number key ID */
  605. if (!ctx->expect_skid) {
  606. kid = asymmetric_key_generate_id(ctx->raw_serial,
  607. ctx->raw_serial_size,
  608. ctx->raw_issuer,
  609. ctx->raw_issuer_size);
  610. } else {
  611. kid = asymmetric_key_generate_id(ctx->raw_skid,
  612. ctx->raw_skid_size,
  613. "", 0);
  614. }
  615. if (IS_ERR(kid))
  616. return PTR_ERR(kid);
  617. pr_devel("SINFO KID: %u [%*phN]\n", kid->len, kid->len, kid->data);
  618. sinfo->sig->auth_ids[0] = kid;
  619. sinfo->index = ++ctx->sinfo_index;
  620. *ctx->ppsinfo = sinfo;
  621. ctx->ppsinfo = &sinfo->next;
  622. ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
  623. if (!ctx->sinfo)
  624. return -ENOMEM;
  625. ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature),
  626. GFP_KERNEL);
  627. if (!ctx->sinfo->sig)
  628. return -ENOMEM;
  629. return 0;
  630. }