blob: e6298b7a945a97b21e233104d49d1e801be1c2d4 [file] [log] [blame]
David Howells2e3fadb2014-07-01 16:40:19 +01001/* PKCS#7 parser
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
12#define pr_fmt(fmt) "PKCS7: "fmt
13#include <linux/kernel.h>
14#include <linux/export.h>
15#include <linux/slab.h>
16#include <linux/err.h>
17#include <linux/oid_registry.h>
18#include "public_key.h"
19#include "pkcs7_parser.h"
20#include "pkcs7-asn1.h"
21
22struct pkcs7_parse_context {
23 struct pkcs7_message *msg; /* Message being constructed */
24 struct pkcs7_signed_info *sinfo; /* SignedInfo being constructed */
25 struct pkcs7_signed_info **ppsinfo;
26 struct x509_certificate *certs; /* Certificate cache */
27 struct x509_certificate **ppcerts;
28 unsigned long data; /* Start of data */
29 enum OID last_oid; /* Last OID encountered */
30 unsigned x509_index;
31 unsigned sinfo_index;
David Howells46963b72014-09-16 17:36:13 +010032 const void *raw_serial;
33 unsigned raw_serial_size;
34 unsigned raw_issuer_size;
35 const void *raw_issuer;
David Howells60d65ca2015-07-20 21:16:33 +010036 const void *raw_skid;
37 unsigned raw_skid_size;
38 bool expect_skid;
David Howells2e3fadb2014-07-01 16:40:19 +010039};
40
David Howells3cd09202014-09-16 17:29:03 +010041/*
42 * Free a signed information block.
43 */
44static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo)
45{
46 if (sinfo) {
47 mpi_free(sinfo->sig.mpi[0]);
48 kfree(sinfo->sig.digest);
David Howells46963b72014-09-16 17:36:13 +010049 kfree(sinfo->signing_cert_id);
David Howells3cd09202014-09-16 17:29:03 +010050 kfree(sinfo);
51 }
52}
53
David Howells2e3fadb2014-07-01 16:40:19 +010054/**
55 * pkcs7_free_message - Free a PKCS#7 message
56 * @pkcs7: The PKCS#7 message to free
57 */
58void pkcs7_free_message(struct pkcs7_message *pkcs7)
59{
60 struct x509_certificate *cert;
61 struct pkcs7_signed_info *sinfo;
62
63 if (pkcs7) {
64 while (pkcs7->certs) {
65 cert = pkcs7->certs;
66 pkcs7->certs = cert->next;
67 x509_free_certificate(cert);
68 }
69 while (pkcs7->crl) {
70 cert = pkcs7->crl;
71 pkcs7->crl = cert->next;
72 x509_free_certificate(cert);
73 }
74 while (pkcs7->signed_infos) {
75 sinfo = pkcs7->signed_infos;
76 pkcs7->signed_infos = sinfo->next;
David Howells3cd09202014-09-16 17:29:03 +010077 pkcs7_free_signed_info(sinfo);
David Howells2e3fadb2014-07-01 16:40:19 +010078 }
79 kfree(pkcs7);
80 }
81}
82EXPORT_SYMBOL_GPL(pkcs7_free_message);
83
David Howells99db4432015-08-05 15:22:27 +010084/*
85 * Check authenticatedAttributes are provided or not provided consistently.
86 */
87static int pkcs7_check_authattrs(struct pkcs7_message *msg)
88{
89 struct pkcs7_signed_info *sinfo;
90 bool want;
91
92 sinfo = msg->signed_infos;
93 if (sinfo->authattrs) {
94 want = true;
95 msg->have_authattrs = true;
96 }
97
98 for (sinfo = sinfo->next; sinfo; sinfo = sinfo->next)
99 if (!!sinfo->authattrs != want)
100 goto inconsistent;
101 return 0;
102
103inconsistent:
104 pr_warn("Inconsistently supplied authAttrs\n");
105 return -EINVAL;
106}
107
David Howells2e3fadb2014-07-01 16:40:19 +0100108/**
109 * pkcs7_parse_message - Parse a PKCS#7 message
110 * @data: The raw binary ASN.1 encoded message to be parsed
111 * @datalen: The size of the encoded message
112 */
113struct pkcs7_message *pkcs7_parse_message(const void *data, size_t datalen)
114{
115 struct pkcs7_parse_context *ctx;
David Howellscecf5d2e2014-09-16 17:29:03 +0100116 struct pkcs7_message *msg = ERR_PTR(-ENOMEM);
117 int ret;
David Howells2e3fadb2014-07-01 16:40:19 +0100118
David Howells2e3fadb2014-07-01 16:40:19 +0100119 ctx = kzalloc(sizeof(struct pkcs7_parse_context), GFP_KERNEL);
120 if (!ctx)
David Howellscecf5d2e2014-09-16 17:29:03 +0100121 goto out_no_ctx;
122 ctx->msg = kzalloc(sizeof(struct pkcs7_message), GFP_KERNEL);
123 if (!ctx->msg)
124 goto out_no_msg;
David Howells2e3fadb2014-07-01 16:40:19 +0100125 ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
126 if (!ctx->sinfo)
David Howellscecf5d2e2014-09-16 17:29:03 +0100127 goto out_no_sinfo;
David Howells2e3fadb2014-07-01 16:40:19 +0100128
David Howells2e3fadb2014-07-01 16:40:19 +0100129 ctx->data = (unsigned long)data;
130 ctx->ppcerts = &ctx->certs;
131 ctx->ppsinfo = &ctx->msg->signed_infos;
132
133 /* Attempt to decode the signature */
134 ret = asn1_ber_decoder(&pkcs7_decoder, ctx, data, datalen);
David Howellscecf5d2e2014-09-16 17:29:03 +0100135 if (ret < 0) {
136 msg = ERR_PTR(ret);
137 goto out;
138 }
David Howells2e3fadb2014-07-01 16:40:19 +0100139
David Howells99db4432015-08-05 15:22:27 +0100140 ret = pkcs7_check_authattrs(ctx->msg);
141 if (ret < 0)
142 goto out;
143
David Howellscecf5d2e2014-09-16 17:29:03 +0100144 msg = ctx->msg;
145 ctx->msg = NULL;
146
147out:
David Howells2e3fadb2014-07-01 16:40:19 +0100148 while (ctx->certs) {
149 struct x509_certificate *cert = ctx->certs;
150 ctx->certs = cert->next;
151 x509_free_certificate(cert);
152 }
David Howells3cd09202014-09-16 17:29:03 +0100153 pkcs7_free_signed_info(ctx->sinfo);
David Howellscecf5d2e2014-09-16 17:29:03 +0100154out_no_sinfo:
155 pkcs7_free_message(ctx->msg);
156out_no_msg:
David Howells2e3fadb2014-07-01 16:40:19 +0100157 kfree(ctx);
David Howellscecf5d2e2014-09-16 17:29:03 +0100158out_no_ctx:
David Howells2e3fadb2014-07-01 16:40:19 +0100159 return msg;
David Howells2e3fadb2014-07-01 16:40:19 +0100160}
161EXPORT_SYMBOL_GPL(pkcs7_parse_message);
162
163/**
164 * pkcs7_get_content_data - Get access to the PKCS#7 content
165 * @pkcs7: The preparsed PKCS#7 message to access
166 * @_data: Place to return a pointer to the data
167 * @_data_len: Place to return the data length
168 * @want_wrapper: True if the ASN.1 object header should be included in the data
169 *
170 * Get access to the data content of the PKCS#7 message, including, optionally,
171 * the header of the ASN.1 object that contains it. Returns -ENODATA if the
172 * data object was missing from the message.
173 */
174int pkcs7_get_content_data(const struct pkcs7_message *pkcs7,
175 const void **_data, size_t *_data_len,
176 bool want_wrapper)
177{
178 size_t wrapper;
179
180 if (!pkcs7->data)
181 return -ENODATA;
182
183 wrapper = want_wrapper ? pkcs7->data_hdrlen : 0;
184 *_data = pkcs7->data - wrapper;
185 *_data_len = pkcs7->data_len + wrapper;
186 return 0;
187}
188EXPORT_SYMBOL_GPL(pkcs7_get_content_data);
189
190/*
191 * Note an OID when we find one for later processing when we know how
192 * to interpret it.
193 */
194int pkcs7_note_OID(void *context, size_t hdrlen,
195 unsigned char tag,
196 const void *value, size_t vlen)
197{
198 struct pkcs7_parse_context *ctx = context;
199
200 ctx->last_oid = look_up_OID(value, vlen);
201 if (ctx->last_oid == OID__NR) {
202 char buffer[50];
203 sprint_oid(value, vlen, buffer, sizeof(buffer));
204 printk("PKCS7: Unknown OID: [%lu] %s\n",
205 (unsigned long)value - ctx->data, buffer);
206 }
207 return 0;
208}
209
210/*
211 * Note the digest algorithm for the signature.
212 */
213int pkcs7_sig_note_digest_algo(void *context, size_t hdrlen,
214 unsigned char tag,
215 const void *value, size_t vlen)
216{
217 struct pkcs7_parse_context *ctx = context;
218
219 switch (ctx->last_oid) {
220 case OID_md4:
221 ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_MD4;
222 break;
223 case OID_md5:
224 ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_MD5;
225 break;
226 case OID_sha1:
227 ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_SHA1;
228 break;
229 case OID_sha256:
230 ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_SHA256;
231 break;
232 default:
233 printk("Unsupported digest algo: %u\n", ctx->last_oid);
234 return -ENOPKG;
235 }
236 return 0;
237}
238
239/*
240 * Note the public key algorithm for the signature.
241 */
242int pkcs7_sig_note_pkey_algo(void *context, size_t hdrlen,
243 unsigned char tag,
244 const void *value, size_t vlen)
245{
246 struct pkcs7_parse_context *ctx = context;
247
248 switch (ctx->last_oid) {
249 case OID_rsaEncryption:
250 ctx->sinfo->sig.pkey_algo = PKEY_ALGO_RSA;
251 break;
252 default:
253 printk("Unsupported pkey algo: %u\n", ctx->last_oid);
254 return -ENOPKG;
255 }
256 return 0;
257}
258
259/*
David Howells2c7fd362015-07-20 21:16:31 +0100260 * We only support signed data [RFC2315 sec 9].
261 */
262int pkcs7_check_content_type(void *context, size_t hdrlen,
263 unsigned char tag,
264 const void *value, size_t vlen)
265{
266 struct pkcs7_parse_context *ctx = context;
267
268 if (ctx->last_oid != OID_signed_data) {
269 pr_warn("Only support pkcs7_signedData type\n");
270 return -EINVAL;
271 }
272
273 return 0;
274}
275
276/*
277 * Note the SignedData version
278 */
279int pkcs7_note_signeddata_version(void *context, size_t hdrlen,
280 unsigned char tag,
281 const void *value, size_t vlen)
282{
David Howells60d65ca2015-07-20 21:16:33 +0100283 struct pkcs7_parse_context *ctx = context;
David Howells2c7fd362015-07-20 21:16:31 +0100284 unsigned version;
285
286 if (vlen != 1)
287 goto unsupported;
288
David Howells60d65ca2015-07-20 21:16:33 +0100289 ctx->msg->version = version = *(const u8 *)value;
David Howells2c7fd362015-07-20 21:16:31 +0100290 switch (version) {
291 case 1:
David Howells60d65ca2015-07-20 21:16:33 +0100292 /* PKCS#7 SignedData [RFC2315 sec 9.1]
293 * CMS ver 1 SignedData [RFC5652 sec 5.1]
294 */
295 break;
296 case 3:
297 /* CMS ver 3 SignedData [RFC2315 sec 5.1] */
David Howells2c7fd362015-07-20 21:16:31 +0100298 break;
299 default:
300 goto unsupported;
301 }
302
303 return 0;
304
305unsupported:
306 pr_warn("Unsupported SignedData version\n");
307 return -EINVAL;
308}
309
310/*
311 * Note the SignerInfo version
312 */
313int pkcs7_note_signerinfo_version(void *context, size_t hdrlen,
314 unsigned char tag,
315 const void *value, size_t vlen)
316{
David Howells60d65ca2015-07-20 21:16:33 +0100317 struct pkcs7_parse_context *ctx = context;
David Howells2c7fd362015-07-20 21:16:31 +0100318 unsigned version;
319
320 if (vlen != 1)
321 goto unsupported;
322
323 version = *(const u8 *)value;
324 switch (version) {
325 case 1:
David Howells60d65ca2015-07-20 21:16:33 +0100326 /* PKCS#7 SignerInfo [RFC2315 sec 9.2]
327 * CMS ver 1 SignerInfo [RFC5652 sec 5.3]
328 */
329 if (ctx->msg->version != 1)
330 goto version_mismatch;
331 ctx->expect_skid = false;
332 break;
333 case 3:
334 /* CMS ver 3 SignerInfo [RFC2315 sec 5.3] */
335 if (ctx->msg->version == 1)
336 goto version_mismatch;
337 ctx->expect_skid = true;
David Howells2c7fd362015-07-20 21:16:31 +0100338 break;
339 default:
340 goto unsupported;
341 }
342
343 return 0;
344
345unsupported:
346 pr_warn("Unsupported SignerInfo version\n");
347 return -EINVAL;
David Howells60d65ca2015-07-20 21:16:33 +0100348version_mismatch:
349 pr_warn("SignedData-SignerInfo version mismatch\n");
350 return -EBADMSG;
David Howells2c7fd362015-07-20 21:16:31 +0100351}
352
353/*
David Howells2e3fadb2014-07-01 16:40:19 +0100354 * Extract a certificate and store it in the context.
355 */
356int pkcs7_extract_cert(void *context, size_t hdrlen,
357 unsigned char tag,
358 const void *value, size_t vlen)
359{
360 struct pkcs7_parse_context *ctx = context;
361 struct x509_certificate *x509;
362
363 if (tag != ((ASN1_UNIV << 6) | ASN1_CONS_BIT | ASN1_SEQ)) {
364 pr_debug("Cert began with tag %02x at %lu\n",
365 tag, (unsigned long)ctx - ctx->data);
366 return -EBADMSG;
367 }
368
369 /* We have to correct for the header so that the X.509 parser can start
370 * from the beginning. Note that since X.509 stipulates DER, there
371 * probably shouldn't be an EOC trailer - but it is in PKCS#7 (which
372 * stipulates BER).
373 */
374 value -= hdrlen;
375 vlen += hdrlen;
376
377 if (((u8*)value)[1] == 0x80)
378 vlen += 2; /* Indefinite length - there should be an EOC */
379
380 x509 = x509_cert_parse(value, vlen);
381 if (IS_ERR(x509))
382 return PTR_ERR(x509);
383
David Howells2e3fadb2014-07-01 16:40:19 +0100384 x509->index = ++ctx->x509_index;
David Howells46963b72014-09-16 17:36:13 +0100385 pr_debug("Got cert %u for %s\n", x509->index, x509->subject);
386 pr_debug("- fingerprint %*phN\n", x509->id->len, x509->id->data);
387
David Howells2e3fadb2014-07-01 16:40:19 +0100388 *ctx->ppcerts = x509;
389 ctx->ppcerts = &x509->next;
390 return 0;
391}
392
393/*
394 * Save the certificate list
395 */
396int pkcs7_note_certificate_list(void *context, size_t hdrlen,
397 unsigned char tag,
398 const void *value, size_t vlen)
399{
400 struct pkcs7_parse_context *ctx = context;
401
402 pr_devel("Got cert list (%02x)\n", tag);
403
404 *ctx->ppcerts = ctx->msg->certs;
405 ctx->msg->certs = ctx->certs;
406 ctx->certs = NULL;
407 ctx->ppcerts = &ctx->certs;
408 return 0;
409}
410
411/*
David Howells99db4432015-08-05 15:22:27 +0100412 * Note the content type.
413 */
414int pkcs7_note_content(void *context, size_t hdrlen,
415 unsigned char tag,
416 const void *value, size_t vlen)
417{
418 struct pkcs7_parse_context *ctx = context;
419
420 if (ctx->last_oid != OID_data &&
421 ctx->last_oid != OID_msIndirectData) {
422 pr_warn("Unsupported data type %d\n", ctx->last_oid);
423 return -EINVAL;
424 }
425
426 ctx->msg->data_type = ctx->last_oid;
427 return 0;
428}
429
430/*
David Howells2e3fadb2014-07-01 16:40:19 +0100431 * Extract the data from the message and store that and its content type OID in
432 * the context.
433 */
434int pkcs7_note_data(void *context, size_t hdrlen,
435 unsigned char tag,
436 const void *value, size_t vlen)
437{
438 struct pkcs7_parse_context *ctx = context;
439
440 pr_debug("Got data\n");
441
442 ctx->msg->data = value;
443 ctx->msg->data_len = vlen;
444 ctx->msg->data_hdrlen = hdrlen;
David Howells2e3fadb2014-07-01 16:40:19 +0100445 return 0;
446}
447
448/*
David Howells99db4432015-08-05 15:22:27 +0100449 * Parse authenticated attributes.
David Howells2e3fadb2014-07-01 16:40:19 +0100450 */
451int pkcs7_sig_note_authenticated_attr(void *context, size_t hdrlen,
452 unsigned char tag,
453 const void *value, size_t vlen)
454{
455 struct pkcs7_parse_context *ctx = context;
David Howells99db4432015-08-05 15:22:27 +0100456 struct pkcs7_signed_info *sinfo = ctx->sinfo;
457 enum OID content_type;
David Howells2e3fadb2014-07-01 16:40:19 +0100458
459 pr_devel("AuthAttr: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
460
461 switch (ctx->last_oid) {
David Howells99db4432015-08-05 15:22:27 +0100462 case OID_contentType:
463 if (__test_and_set_bit(sinfo_has_content_type, &sinfo->aa_set))
464 goto repeated;
465 content_type = look_up_OID(value, vlen);
466 if (content_type != ctx->msg->data_type) {
467 pr_warn("Mismatch between global data type (%d) and sinfo %u (%d)\n",
468 ctx->msg->data_type, sinfo->index,
469 content_type);
470 return -EBADMSG;
471 }
472 return 0;
473
474 case OID_signingTime:
475 if (__test_and_set_bit(sinfo_has_signing_time, &sinfo->aa_set))
476 goto repeated;
477 /* Should we check that the signing time is consistent
478 * with the signer's X.509 cert?
479 */
480 return x509_decode_time(&sinfo->signing_time,
481 hdrlen, tag, value, vlen);
482
David Howells2e3fadb2014-07-01 16:40:19 +0100483 case OID_messageDigest:
David Howells99db4432015-08-05 15:22:27 +0100484 if (__test_and_set_bit(sinfo_has_message_digest, &sinfo->aa_set))
485 goto repeated;
David Howells2e3fadb2014-07-01 16:40:19 +0100486 if (tag != ASN1_OTS)
487 return -EBADMSG;
David Howells99db4432015-08-05 15:22:27 +0100488 sinfo->msgdigest = value;
489 sinfo->msgdigest_len = vlen;
490 return 0;
491
492 case OID_smimeCapabilites:
493 if (__test_and_set_bit(sinfo_has_smime_caps, &sinfo->aa_set))
494 goto repeated;
495 if (ctx->msg->data_type != OID_msIndirectData) {
496 pr_warn("S/MIME Caps only allowed with Authenticode\n");
497 return -EKEYREJECTED;
498 }
499 return 0;
500
501 /* Microsoft SpOpusInfo seems to be contain cont[0] 16-bit BE
502 * char URLs and cont[1] 8-bit char URLs.
503 *
504 * Microsoft StatementType seems to contain a list of OIDs that
505 * are also used as extendedKeyUsage types in X.509 certs.
506 */
507 case OID_msSpOpusInfo:
508 if (__test_and_set_bit(sinfo_has_ms_opus_info, &sinfo->aa_set))
509 goto repeated;
510 goto authenticode_check;
511 case OID_msStatementType:
512 if (__test_and_set_bit(sinfo_has_ms_statement_type, &sinfo->aa_set))
513 goto repeated;
514 authenticode_check:
515 if (ctx->msg->data_type != OID_msIndirectData) {
516 pr_warn("Authenticode AuthAttrs only allowed with Authenticode\n");
517 return -EKEYREJECTED;
518 }
519 /* I'm not sure how to validate these */
David Howells2e3fadb2014-07-01 16:40:19 +0100520 return 0;
521 default:
522 return 0;
523 }
David Howells99db4432015-08-05 15:22:27 +0100524
525repeated:
526 /* We permit max one item per AuthenticatedAttribute and no repeats */
527 pr_warn("Repeated/multivalue AuthAttrs not permitted\n");
528 return -EKEYREJECTED;
David Howells2e3fadb2014-07-01 16:40:19 +0100529}
530
531/*
David Howells2c7fd362015-07-20 21:16:31 +0100532 * Note the set of auth attributes for digestion purposes [RFC2315 sec 9.3]
David Howells2e3fadb2014-07-01 16:40:19 +0100533 */
534int pkcs7_sig_note_set_of_authattrs(void *context, size_t hdrlen,
535 unsigned char tag,
536 const void *value, size_t vlen)
537{
538 struct pkcs7_parse_context *ctx = context;
David Howells99db4432015-08-05 15:22:27 +0100539 struct pkcs7_signed_info *sinfo = ctx->sinfo;
540
541 if (!test_bit(sinfo_has_content_type, &sinfo->aa_set) ||
542 !test_bit(sinfo_has_message_digest, &sinfo->aa_set) ||
543 (ctx->msg->data_type == OID_msIndirectData &&
544 !test_bit(sinfo_has_ms_opus_info, &sinfo->aa_set))) {
545 pr_warn("Missing required AuthAttr\n");
546 return -EBADMSG;
547 }
548
549 if (ctx->msg->data_type != OID_msIndirectData &&
550 test_bit(sinfo_has_ms_opus_info, &sinfo->aa_set)) {
551 pr_warn("Unexpected Authenticode AuthAttr\n");
552 return -EBADMSG;
553 }
David Howells2e3fadb2014-07-01 16:40:19 +0100554
555 /* We need to switch the 'CONT 0' to a 'SET OF' when we digest */
David Howells99db4432015-08-05 15:22:27 +0100556 sinfo->authattrs = value - (hdrlen - 1);
557 sinfo->authattrs_len = vlen + (hdrlen - 1);
David Howells2e3fadb2014-07-01 16:40:19 +0100558 return 0;
559}
560
561/*
562 * Note the issuing certificate serial number
563 */
564int pkcs7_sig_note_serial(void *context, size_t hdrlen,
565 unsigned char tag,
566 const void *value, size_t vlen)
567{
568 struct pkcs7_parse_context *ctx = context;
David Howells46963b72014-09-16 17:36:13 +0100569 ctx->raw_serial = value;
570 ctx->raw_serial_size = vlen;
David Howells2e3fadb2014-07-01 16:40:19 +0100571 return 0;
572}
573
574/*
575 * Note the issuer's name
576 */
577int pkcs7_sig_note_issuer(void *context, size_t hdrlen,
578 unsigned char tag,
579 const void *value, size_t vlen)
580{
581 struct pkcs7_parse_context *ctx = context;
David Howells46963b72014-09-16 17:36:13 +0100582 ctx->raw_issuer = value;
583 ctx->raw_issuer_size = vlen;
David Howells2e3fadb2014-07-01 16:40:19 +0100584 return 0;
585}
586
587/*
David Howells60d65ca2015-07-20 21:16:33 +0100588 * Note the issuing cert's subjectKeyIdentifier
589 */
590int pkcs7_sig_note_skid(void *context, size_t hdrlen,
591 unsigned char tag,
592 const void *value, size_t vlen)
593{
594 struct pkcs7_parse_context *ctx = context;
595
596 pr_devel("SKID: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
597
598 ctx->raw_skid = value;
599 ctx->raw_skid_size = vlen;
600 return 0;
601}
602
603/*
David Howells2e3fadb2014-07-01 16:40:19 +0100604 * Note the signature data
605 */
606int pkcs7_sig_note_signature(void *context, size_t hdrlen,
607 unsigned char tag,
608 const void *value, size_t vlen)
609{
610 struct pkcs7_parse_context *ctx = context;
611 MPI mpi;
612
613 BUG_ON(ctx->sinfo->sig.pkey_algo != PKEY_ALGO_RSA);
614
615 mpi = mpi_read_raw_data(value, vlen);
616 if (!mpi)
617 return -ENOMEM;
618
619 ctx->sinfo->sig.mpi[0] = mpi;
620 ctx->sinfo->sig.nr_mpi = 1;
621 return 0;
622}
623
624/*
625 * Note a signature information block
626 */
627int pkcs7_note_signed_info(void *context, size_t hdrlen,
628 unsigned char tag,
629 const void *value, size_t vlen)
630{
631 struct pkcs7_parse_context *ctx = context;
David Howells46963b72014-09-16 17:36:13 +0100632 struct pkcs7_signed_info *sinfo = ctx->sinfo;
633 struct asymmetric_key_id *kid;
David Howells2e3fadb2014-07-01 16:40:19 +0100634
David Howells99db4432015-08-05 15:22:27 +0100635 if (ctx->msg->data_type == OID_msIndirectData && !sinfo->authattrs) {
636 pr_warn("Authenticode requires AuthAttrs\n");
637 return -EBADMSG;
638 }
639
David Howells46963b72014-09-16 17:36:13 +0100640 /* Generate cert issuer + serial number key ID */
David Howells60d65ca2015-07-20 21:16:33 +0100641 if (!ctx->expect_skid) {
642 kid = asymmetric_key_generate_id(ctx->raw_serial,
643 ctx->raw_serial_size,
644 ctx->raw_issuer,
645 ctx->raw_issuer_size);
646 } else {
647 kid = asymmetric_key_generate_id(ctx->raw_skid,
648 ctx->raw_skid_size,
649 "", 0);
650 }
David Howells46963b72014-09-16 17:36:13 +0100651 if (IS_ERR(kid))
652 return PTR_ERR(kid);
653
David Howells60d65ca2015-07-20 21:16:33 +0100654 pr_devel("SINFO KID: %u [%*phN]\n", kid->len, kid->len, kid->data);
655
David Howells46963b72014-09-16 17:36:13 +0100656 sinfo->signing_cert_id = kid;
657 sinfo->index = ++ctx->sinfo_index;
658 *ctx->ppsinfo = sinfo;
659 ctx->ppsinfo = &sinfo->next;
David Howells2e3fadb2014-07-01 16:40:19 +0100660 ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
661 if (!ctx->sinfo)
662 return -ENOMEM;
663 return 0;
664}