pkcs7_parser.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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 ([email protected])
  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_md4:
  207. ctx->sinfo->sig->hash_algo = "md4";
  208. break;
  209. case OID_md5:
  210. ctx->sinfo->sig->hash_algo = "md5";
  211. break;
  212. case OID_sha1:
  213. ctx->sinfo->sig->hash_algo = "sha1";
  214. break;
  215. case OID_sha256:
  216. ctx->sinfo->sig->hash_algo = "sha256";
  217. break;
  218. case OID_sha384:
  219. ctx->sinfo->sig->hash_algo = "sha384";
  220. break;
  221. case OID_sha512:
  222. ctx->sinfo->sig->hash_algo = "sha512";
  223. break;
  224. case OID_sha224:
  225. ctx->sinfo->sig->hash_algo = "sha224";
  226. break;
  227. case OID_sm3:
  228. ctx->sinfo->sig->hash_algo = "sm3";
  229. break;
  230. case OID_gost2012Digest256:
  231. ctx->sinfo->sig->hash_algo = "streebog256";
  232. break;
  233. case OID_gost2012Digest512:
  234. ctx->sinfo->sig->hash_algo = "streebog512";
  235. break;
  236. default:
  237. printk("Unsupported digest algo: %u\n", ctx->last_oid);
  238. return -ENOPKG;
  239. }
  240. return 0;
  241. }
  242. /*
  243. * Note the public key algorithm for the signature.
  244. */
  245. int pkcs7_sig_note_pkey_algo(void *context, size_t hdrlen,
  246. unsigned char tag,
  247. const void *value, size_t vlen)
  248. {
  249. struct pkcs7_parse_context *ctx = context;
  250. switch (ctx->last_oid) {
  251. case OID_rsaEncryption:
  252. ctx->sinfo->sig->pkey_algo = "rsa";
  253. ctx->sinfo->sig->encoding = "pkcs1";
  254. break;
  255. case OID_id_ecdsa_with_sha1:
  256. case OID_id_ecdsa_with_sha224:
  257. case OID_id_ecdsa_with_sha256:
  258. case OID_id_ecdsa_with_sha384:
  259. case OID_id_ecdsa_with_sha512:
  260. ctx->sinfo->sig->pkey_algo = "ecdsa";
  261. ctx->sinfo->sig->encoding = "x962";
  262. break;
  263. case OID_SM2_with_SM3:
  264. ctx->sinfo->sig->pkey_algo = "sm2";
  265. ctx->sinfo->sig->encoding = "raw";
  266. break;
  267. case OID_gost2012PKey256:
  268. case OID_gost2012PKey512:
  269. ctx->sinfo->sig->pkey_algo = "ecrdsa";
  270. ctx->sinfo->sig->encoding = "raw";
  271. break;
  272. default:
  273. printk("Unsupported pkey algo: %u\n", ctx->last_oid);
  274. return -ENOPKG;
  275. }
  276. return 0;
  277. }
  278. /*
  279. * We only support signed data [RFC2315 sec 9].
  280. */
  281. int pkcs7_check_content_type(void *context, size_t hdrlen,
  282. unsigned char tag,
  283. const void *value, size_t vlen)
  284. {
  285. struct pkcs7_parse_context *ctx = context;
  286. if (ctx->last_oid != OID_signed_data) {
  287. pr_warn("Only support pkcs7_signedData type\n");
  288. return -EINVAL;
  289. }
  290. return 0;
  291. }
  292. /*
  293. * Note the SignedData version
  294. */
  295. int pkcs7_note_signeddata_version(void *context, size_t hdrlen,
  296. unsigned char tag,
  297. const void *value, size_t vlen)
  298. {
  299. struct pkcs7_parse_context *ctx = context;
  300. unsigned version;
  301. if (vlen != 1)
  302. goto unsupported;
  303. ctx->msg->version = version = *(const u8 *)value;
  304. switch (version) {
  305. case 1:
  306. /* PKCS#7 SignedData [RFC2315 sec 9.1]
  307. * CMS ver 1 SignedData [RFC5652 sec 5.1]
  308. */
  309. break;
  310. case 3:
  311. /* CMS ver 3 SignedData [RFC2315 sec 5.1] */
  312. break;
  313. default:
  314. goto unsupported;
  315. }
  316. return 0;
  317. unsupported:
  318. pr_warn("Unsupported SignedData version\n");
  319. return -EINVAL;
  320. }
  321. /*
  322. * Note the SignerInfo version
  323. */
  324. int pkcs7_note_signerinfo_version(void *context, size_t hdrlen,
  325. unsigned char tag,
  326. const void *value, size_t vlen)
  327. {
  328. struct pkcs7_parse_context *ctx = context;
  329. unsigned version;
  330. if (vlen != 1)
  331. goto unsupported;
  332. version = *(const u8 *)value;
  333. switch (version) {
  334. case 1:
  335. /* PKCS#7 SignerInfo [RFC2315 sec 9.2]
  336. * CMS ver 1 SignerInfo [RFC5652 sec 5.3]
  337. */
  338. if (ctx->msg->version != 1)
  339. goto version_mismatch;
  340. ctx->expect_skid = false;
  341. break;
  342. case 3:
  343. /* CMS ver 3 SignerInfo [RFC2315 sec 5.3] */
  344. if (ctx->msg->version == 1)
  345. goto version_mismatch;
  346. ctx->expect_skid = true;
  347. break;
  348. default:
  349. goto unsupported;
  350. }
  351. return 0;
  352. unsupported:
  353. pr_warn("Unsupported SignerInfo version\n");
  354. return -EINVAL;
  355. version_mismatch:
  356. pr_warn("SignedData-SignerInfo version mismatch\n");
  357. return -EBADMSG;
  358. }
  359. /*
  360. * Extract a certificate and store it in the context.
  361. */
  362. int pkcs7_extract_cert(void *context, size_t hdrlen,
  363. unsigned char tag,
  364. const void *value, size_t vlen)
  365. {
  366. struct pkcs7_parse_context *ctx = context;
  367. struct x509_certificate *x509;
  368. if (tag != ((ASN1_UNIV << 6) | ASN1_CONS_BIT | ASN1_SEQ)) {
  369. pr_debug("Cert began with tag %02x at %lu\n",
  370. tag, (unsigned long)ctx - ctx->data);
  371. return -EBADMSG;
  372. }
  373. /* We have to correct for the header so that the X.509 parser can start
  374. * from the beginning. Note that since X.509 stipulates DER, there
  375. * probably shouldn't be an EOC trailer - but it is in PKCS#7 (which
  376. * stipulates BER).
  377. */
  378. value -= hdrlen;
  379. vlen += hdrlen;
  380. if (((u8*)value)[1] == 0x80)
  381. vlen += 2; /* Indefinite length - there should be an EOC */
  382. x509 = x509_cert_parse(value, vlen);
  383. if (IS_ERR(x509))
  384. return PTR_ERR(x509);
  385. x509->index = ++ctx->x509_index;
  386. pr_debug("Got cert %u for %s\n", x509->index, x509->subject);
  387. pr_debug("- fingerprint %*phN\n", x509->id->len, x509->id->data);
  388. *ctx->ppcerts = x509;
  389. ctx->ppcerts = &x509->next;
  390. return 0;
  391. }
  392. /*
  393. * Save the certificate list
  394. */
  395. int pkcs7_note_certificate_list(void *context, size_t hdrlen,
  396. unsigned char tag,
  397. const void *value, size_t vlen)
  398. {
  399. struct pkcs7_parse_context *ctx = context;
  400. pr_devel("Got cert list (%02x)\n", tag);
  401. *ctx->ppcerts = ctx->msg->certs;
  402. ctx->msg->certs = ctx->certs;
  403. ctx->certs = NULL;
  404. ctx->ppcerts = &ctx->certs;
  405. return 0;
  406. }
  407. /*
  408. * Note the content type.
  409. */
  410. int pkcs7_note_content(void *context, size_t hdrlen,
  411. unsigned char tag,
  412. const void *value, size_t vlen)
  413. {
  414. struct pkcs7_parse_context *ctx = context;
  415. if (ctx->last_oid != OID_data &&
  416. ctx->last_oid != OID_msIndirectData) {
  417. pr_warn("Unsupported data type %d\n", ctx->last_oid);
  418. return -EINVAL;
  419. }
  420. ctx->msg->data_type = ctx->last_oid;
  421. return 0;
  422. }
  423. /*
  424. * Extract the data from the message and store that and its content type OID in
  425. * the context.
  426. */
  427. int pkcs7_note_data(void *context, size_t hdrlen,
  428. unsigned char tag,
  429. const void *value, size_t vlen)
  430. {
  431. struct pkcs7_parse_context *ctx = context;
  432. pr_debug("Got data\n");
  433. ctx->msg->data = value;
  434. ctx->msg->data_len = vlen;
  435. ctx->msg->data_hdrlen = hdrlen;
  436. return 0;
  437. }
  438. /*
  439. * Parse authenticated attributes.
  440. */
  441. int pkcs7_sig_note_authenticated_attr(void *context, size_t hdrlen,
  442. unsigned char tag,
  443. const void *value, size_t vlen)
  444. {
  445. struct pkcs7_parse_context *ctx = context;
  446. struct pkcs7_signed_info *sinfo = ctx->sinfo;
  447. enum OID content_type;
  448. pr_devel("AuthAttr: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
  449. switch (ctx->last_oid) {
  450. case OID_contentType:
  451. if (__test_and_set_bit(sinfo_has_content_type, &sinfo->aa_set))
  452. goto repeated;
  453. content_type = look_up_OID(value, vlen);
  454. if (content_type != ctx->msg->data_type) {
  455. pr_warn("Mismatch between global data type (%d) and sinfo %u (%d)\n",
  456. ctx->msg->data_type, sinfo->index,
  457. content_type);
  458. return -EBADMSG;
  459. }
  460. return 0;
  461. case OID_signingTime:
  462. if (__test_and_set_bit(sinfo_has_signing_time, &sinfo->aa_set))
  463. goto repeated;
  464. /* Should we check that the signing time is consistent
  465. * with the signer's X.509 cert?
  466. */
  467. return x509_decode_time(&sinfo->signing_time,
  468. hdrlen, tag, value, vlen);
  469. case OID_messageDigest:
  470. if (__test_and_set_bit(sinfo_has_message_digest, &sinfo->aa_set))
  471. goto repeated;
  472. if (tag != ASN1_OTS)
  473. return -EBADMSG;
  474. sinfo->msgdigest = value;
  475. sinfo->msgdigest_len = vlen;
  476. return 0;
  477. case OID_smimeCapabilites:
  478. if (__test_and_set_bit(sinfo_has_smime_caps, &sinfo->aa_set))
  479. goto repeated;
  480. if (ctx->msg->data_type != OID_msIndirectData) {
  481. pr_warn("S/MIME Caps only allowed with Authenticode\n");
  482. return -EKEYREJECTED;
  483. }
  484. return 0;
  485. /* Microsoft SpOpusInfo seems to be contain cont[0] 16-bit BE
  486. * char URLs and cont[1] 8-bit char URLs.
  487. *
  488. * Microsoft StatementType seems to contain a list of OIDs that
  489. * are also used as extendedKeyUsage types in X.509 certs.
  490. */
  491. case OID_msSpOpusInfo:
  492. if (__test_and_set_bit(sinfo_has_ms_opus_info, &sinfo->aa_set))
  493. goto repeated;
  494. goto authenticode_check;
  495. case OID_msStatementType:
  496. if (__test_and_set_bit(sinfo_has_ms_statement_type, &sinfo->aa_set))
  497. goto repeated;
  498. authenticode_check:
  499. if (ctx->msg->data_type != OID_msIndirectData) {
  500. pr_warn("Authenticode AuthAttrs only allowed with Authenticode\n");
  501. return -EKEYREJECTED;
  502. }
  503. /* I'm not sure how to validate these */
  504. return 0;
  505. default:
  506. return 0;
  507. }
  508. repeated:
  509. /* We permit max one item per AuthenticatedAttribute and no repeats */
  510. pr_warn("Repeated/multivalue AuthAttrs not permitted\n");
  511. return -EKEYREJECTED;
  512. }
  513. /*
  514. * Note the set of auth attributes for digestion purposes [RFC2315 sec 9.3]
  515. */
  516. int pkcs7_sig_note_set_of_authattrs(void *context, size_t hdrlen,
  517. unsigned char tag,
  518. const void *value, size_t vlen)
  519. {
  520. struct pkcs7_parse_context *ctx = context;
  521. struct pkcs7_signed_info *sinfo = ctx->sinfo;
  522. if (!test_bit(sinfo_has_content_type, &sinfo->aa_set) ||
  523. !test_bit(sinfo_has_message_digest, &sinfo->aa_set)) {
  524. pr_warn("Missing required AuthAttr\n");
  525. return -EBADMSG;
  526. }
  527. if (ctx->msg->data_type != OID_msIndirectData &&
  528. test_bit(sinfo_has_ms_opus_info, &sinfo->aa_set)) {
  529. pr_warn("Unexpected Authenticode AuthAttr\n");
  530. return -EBADMSG;
  531. }
  532. /* We need to switch the 'CONT 0' to a 'SET OF' when we digest */
  533. sinfo->authattrs = value - (hdrlen - 1);
  534. sinfo->authattrs_len = vlen + (hdrlen - 1);
  535. return 0;
  536. }
  537. /*
  538. * Note the issuing certificate serial number
  539. */
  540. int pkcs7_sig_note_serial(void *context, size_t hdrlen,
  541. unsigned char tag,
  542. const void *value, size_t vlen)
  543. {
  544. struct pkcs7_parse_context *ctx = context;
  545. ctx->raw_serial = value;
  546. ctx->raw_serial_size = vlen;
  547. return 0;
  548. }
  549. /*
  550. * Note the issuer's name
  551. */
  552. int pkcs7_sig_note_issuer(void *context, size_t hdrlen,
  553. unsigned char tag,
  554. const void *value, size_t vlen)
  555. {
  556. struct pkcs7_parse_context *ctx = context;
  557. ctx->raw_issuer = value;
  558. ctx->raw_issuer_size = vlen;
  559. return 0;
  560. }
  561. /*
  562. * Note the issuing cert's subjectKeyIdentifier
  563. */
  564. int pkcs7_sig_note_skid(void *context, size_t hdrlen,
  565. unsigned char tag,
  566. const void *value, size_t vlen)
  567. {
  568. struct pkcs7_parse_context *ctx = context;
  569. pr_devel("SKID: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
  570. ctx->raw_skid = value;
  571. ctx->raw_skid_size = vlen;
  572. return 0;
  573. }
  574. /*
  575. * Note the signature data
  576. */
  577. int pkcs7_sig_note_signature(void *context, size_t hdrlen,
  578. unsigned char tag,
  579. const void *value, size_t vlen)
  580. {
  581. struct pkcs7_parse_context *ctx = context;
  582. ctx->sinfo->sig->s = kmemdup(value, vlen, GFP_KERNEL);
  583. if (!ctx->sinfo->sig->s)
  584. return -ENOMEM;
  585. ctx->sinfo->sig->s_size = vlen;
  586. return 0;
  587. }
  588. /*
  589. * Note a signature information block
  590. */
  591. int pkcs7_note_signed_info(void *context, size_t hdrlen,
  592. unsigned char tag,
  593. const void *value, size_t vlen)
  594. {
  595. struct pkcs7_parse_context *ctx = context;
  596. struct pkcs7_signed_info *sinfo = ctx->sinfo;
  597. struct asymmetric_key_id *kid;
  598. if (ctx->msg->data_type == OID_msIndirectData && !sinfo->authattrs) {
  599. pr_warn("Authenticode requires AuthAttrs\n");
  600. return -EBADMSG;
  601. }
  602. /* Generate cert issuer + serial number key ID */
  603. if (!ctx->expect_skid) {
  604. kid = asymmetric_key_generate_id(ctx->raw_serial,
  605. ctx->raw_serial_size,
  606. ctx->raw_issuer,
  607. ctx->raw_issuer_size);
  608. } else {
  609. kid = asymmetric_key_generate_id(ctx->raw_skid,
  610. ctx->raw_skid_size,
  611. "", 0);
  612. }
  613. if (IS_ERR(kid))
  614. return PTR_ERR(kid);
  615. pr_devel("SINFO KID: %u [%*phN]\n", kid->len, kid->len, kid->data);
  616. sinfo->sig->auth_ids[0] = kid;
  617. sinfo->index = ++ctx->sinfo_index;
  618. *ctx->ppsinfo = sinfo;
  619. ctx->ppsinfo = &sinfo->next;
  620. ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
  621. if (!ctx->sinfo)
  622. return -ENOMEM;
  623. ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature),
  624. GFP_KERNEL);
  625. if (!ctx->sinfo->sig)
  626. return -ENOMEM;
  627. return 0;
  628. }