blob: 0f134162cef4b5f89c016db315df1b52de18ef16 [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>
David Howells1e684d32017-11-15 16:38:45 +000014#include <linux/module.h>
David Howells2e3fadb2014-07-01 16:40:19 +010015#include <linux/export.h>
16#include <linux/slab.h>
17#include <linux/err.h>
18#include <linux/oid_registry.h>
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -080019#include <crypto/public_key.h>
David Howells2e3fadb2014-07-01 16:40:19 +010020#include "pkcs7_parser.h"
Masahiro Yamada4fa8bc92018-03-23 22:04:37 +090021#include "pkcs7.asn1.h"
David Howells2e3fadb2014-07-01 16:40:19 +010022
David Howells1e684d32017-11-15 16:38:45 +000023MODULE_DESCRIPTION("PKCS#7 parser");
24MODULE_AUTHOR("Red Hat, Inc.");
25MODULE_LICENSE("GPL");
26
David Howells2e3fadb2014-07-01 16:40:19 +010027struct pkcs7_parse_context {
28 struct pkcs7_message *msg; /* Message being constructed */
29 struct pkcs7_signed_info *sinfo; /* SignedInfo being constructed */
30 struct pkcs7_signed_info **ppsinfo;
31 struct x509_certificate *certs; /* Certificate cache */
32 struct x509_certificate **ppcerts;
33 unsigned long data; /* Start of data */
34 enum OID last_oid; /* Last OID encountered */
35 unsigned x509_index;
36 unsigned sinfo_index;
David Howells46963b72014-09-16 17:36:13 +010037 const void *raw_serial;
38 unsigned raw_serial_size;
39 unsigned raw_issuer_size;
40 const void *raw_issuer;
David Howells60d65ca2015-07-20 21:16:33 +010041 const void *raw_skid;
42 unsigned raw_skid_size;
43 bool expect_skid;
David Howells2e3fadb2014-07-01 16:40:19 +010044};
45
David Howells3cd09202014-09-16 17:29:03 +010046/*
47 * Free a signed information block.
48 */
49static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo)
50{
51 if (sinfo) {
David Howells566a1172016-04-06 16:13:33 +010052 public_key_signature_free(sinfo->sig);
David Howells3cd09202014-09-16 17:29:03 +010053 kfree(sinfo);
54 }
55}
56
David Howells2e3fadb2014-07-01 16:40:19 +010057/**
58 * pkcs7_free_message - Free a PKCS#7 message
59 * @pkcs7: The PKCS#7 message to free
60 */
61void pkcs7_free_message(struct pkcs7_message *pkcs7)
62{
63 struct x509_certificate *cert;
64 struct pkcs7_signed_info *sinfo;
65
66 if (pkcs7) {
67 while (pkcs7->certs) {
68 cert = pkcs7->certs;
69 pkcs7->certs = cert->next;
70 x509_free_certificate(cert);
71 }
72 while (pkcs7->crl) {
73 cert = pkcs7->crl;
74 pkcs7->crl = cert->next;
75 x509_free_certificate(cert);
76 }
77 while (pkcs7->signed_infos) {
78 sinfo = pkcs7->signed_infos;
79 pkcs7->signed_infos = sinfo->next;
David Howells3cd09202014-09-16 17:29:03 +010080 pkcs7_free_signed_info(sinfo);
David Howells2e3fadb2014-07-01 16:40:19 +010081 }
82 kfree(pkcs7);
83 }
84}
85EXPORT_SYMBOL_GPL(pkcs7_free_message);
86
David Howells99db4432015-08-05 15:22:27 +010087/*
88 * Check authenticatedAttributes are provided or not provided consistently.
89 */
90static int pkcs7_check_authattrs(struct pkcs7_message *msg)
91{
92 struct pkcs7_signed_info *sinfo;
Colin Ian King06aae5922016-02-27 12:45:26 +000093 bool want = false;
David Howells99db4432015-08-05 15:22:27 +010094
95 sinfo = msg->signed_infos;
Eric Sesterhenn68a1fdb2017-10-08 20:02:32 +020096 if (!sinfo)
97 goto inconsistent;
98
David Howells99db4432015-08-05 15:22:27 +010099 if (sinfo->authattrs) {
100 want = true;
101 msg->have_authattrs = true;
102 }
103
104 for (sinfo = sinfo->next; sinfo; sinfo = sinfo->next)
105 if (!!sinfo->authattrs != want)
106 goto inconsistent;
107 return 0;
108
109inconsistent:
110 pr_warn("Inconsistently supplied authAttrs\n");
111 return -EINVAL;
112}
113
David Howells2e3fadb2014-07-01 16:40:19 +0100114/**
115 * pkcs7_parse_message - Parse a PKCS#7 message
116 * @data: The raw binary ASN.1 encoded message to be parsed
117 * @datalen: The size of the encoded message
118 */
119struct pkcs7_message *pkcs7_parse_message(const void *data, size_t datalen)
120{
121 struct pkcs7_parse_context *ctx;
David Howellscecf5d2e2014-09-16 17:29:03 +0100122 struct pkcs7_message *msg = ERR_PTR(-ENOMEM);
123 int ret;
David Howells2e3fadb2014-07-01 16:40:19 +0100124
David Howells2e3fadb2014-07-01 16:40:19 +0100125 ctx = kzalloc(sizeof(struct pkcs7_parse_context), GFP_KERNEL);
126 if (!ctx)
David Howellscecf5d2e2014-09-16 17:29:03 +0100127 goto out_no_ctx;
128 ctx->msg = kzalloc(sizeof(struct pkcs7_message), GFP_KERNEL);
129 if (!ctx->msg)
130 goto out_no_msg;
David Howells2e3fadb2014-07-01 16:40:19 +0100131 ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
132 if (!ctx->sinfo)
David Howellscecf5d2e2014-09-16 17:29:03 +0100133 goto out_no_sinfo;
David Howells566a1172016-04-06 16:13:33 +0100134 ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature),
135 GFP_KERNEL);
136 if (!ctx->sinfo->sig)
137 goto out_no_sig;
David Howells2e3fadb2014-07-01 16:40:19 +0100138
David Howells2e3fadb2014-07-01 16:40:19 +0100139 ctx->data = (unsigned long)data;
140 ctx->ppcerts = &ctx->certs;
141 ctx->ppsinfo = &ctx->msg->signed_infos;
142
143 /* Attempt to decode the signature */
144 ret = asn1_ber_decoder(&pkcs7_decoder, ctx, data, datalen);
David Howellscecf5d2e2014-09-16 17:29:03 +0100145 if (ret < 0) {
146 msg = ERR_PTR(ret);
147 goto out;
148 }
David Howells2e3fadb2014-07-01 16:40:19 +0100149
David Howells99db4432015-08-05 15:22:27 +0100150 ret = pkcs7_check_authattrs(ctx->msg);
Eric Biggers8ecb5062017-12-08 15:13:28 +0000151 if (ret < 0) {
152 msg = ERR_PTR(ret);
David Howells99db4432015-08-05 15:22:27 +0100153 goto out;
Eric Biggers8ecb5062017-12-08 15:13:28 +0000154 }
David Howells99db4432015-08-05 15:22:27 +0100155
David Howellscecf5d2e2014-09-16 17:29:03 +0100156 msg = ctx->msg;
157 ctx->msg = NULL;
158
159out:
David Howells2e3fadb2014-07-01 16:40:19 +0100160 while (ctx->certs) {
161 struct x509_certificate *cert = ctx->certs;
162 ctx->certs = cert->next;
163 x509_free_certificate(cert);
164 }
David Howells566a1172016-04-06 16:13:33 +0100165out_no_sig:
David Howells3cd09202014-09-16 17:29:03 +0100166 pkcs7_free_signed_info(ctx->sinfo);
David Howellscecf5d2e2014-09-16 17:29:03 +0100167out_no_sinfo:
168 pkcs7_free_message(ctx->msg);
169out_no_msg:
David Howells2e3fadb2014-07-01 16:40:19 +0100170 kfree(ctx);
David Howellscecf5d2e2014-09-16 17:29:03 +0100171out_no_ctx:
David Howells2e3fadb2014-07-01 16:40:19 +0100172 return msg;
David Howells2e3fadb2014-07-01 16:40:19 +0100173}
174EXPORT_SYMBOL_GPL(pkcs7_parse_message);
175
176/**
177 * pkcs7_get_content_data - Get access to the PKCS#7 content
178 * @pkcs7: The preparsed PKCS#7 message to access
179 * @_data: Place to return a pointer to the data
180 * @_data_len: Place to return the data length
David Howellse68503b2016-04-06 16:14:24 +0100181 * @_headerlen: Size of ASN.1 header not included in _data
David Howells2e3fadb2014-07-01 16:40:19 +0100182 *
David Howellse68503b2016-04-06 16:14:24 +0100183 * Get access to the data content of the PKCS#7 message. The size of the
184 * header of the ASN.1 object that contains it is also provided and can be used
185 * to adjust *_data and *_data_len to get the entire object.
186 *
187 * Returns -ENODATA if the data object was missing from the message.
David Howells2e3fadb2014-07-01 16:40:19 +0100188 */
189int pkcs7_get_content_data(const struct pkcs7_message *pkcs7,
190 const void **_data, size_t *_data_len,
David Howellse68503b2016-04-06 16:14:24 +0100191 size_t *_headerlen)
David Howells2e3fadb2014-07-01 16:40:19 +0100192{
David Howells2e3fadb2014-07-01 16:40:19 +0100193 if (!pkcs7->data)
194 return -ENODATA;
195
David Howellse68503b2016-04-06 16:14:24 +0100196 *_data = pkcs7->data;
197 *_data_len = pkcs7->data_len;
198 if (_headerlen)
199 *_headerlen = pkcs7->data_hdrlen;
David Howells2e3fadb2014-07-01 16:40:19 +0100200 return 0;
201}
202EXPORT_SYMBOL_GPL(pkcs7_get_content_data);
203
204/*
205 * Note an OID when we find one for later processing when we know how
206 * to interpret it.
207 */
208int pkcs7_note_OID(void *context, size_t hdrlen,
209 unsigned char tag,
210 const void *value, size_t vlen)
211{
212 struct pkcs7_parse_context *ctx = context;
213
214 ctx->last_oid = look_up_OID(value, vlen);
215 if (ctx->last_oid == OID__NR) {
216 char buffer[50];
217 sprint_oid(value, vlen, buffer, sizeof(buffer));
218 printk("PKCS7: Unknown OID: [%lu] %s\n",
219 (unsigned long)value - ctx->data, buffer);
220 }
221 return 0;
222}
223
224/*
225 * Note the digest algorithm for the signature.
226 */
227int pkcs7_sig_note_digest_algo(void *context, size_t hdrlen,
228 unsigned char tag,
229 const void *value, size_t vlen)
230{
231 struct pkcs7_parse_context *ctx = context;
232
233 switch (ctx->last_oid) {
234 case OID_md4:
David Howells566a1172016-04-06 16:13:33 +0100235 ctx->sinfo->sig->hash_algo = "md4";
David Howells2e3fadb2014-07-01 16:40:19 +0100236 break;
237 case OID_md5:
David Howells566a1172016-04-06 16:13:33 +0100238 ctx->sinfo->sig->hash_algo = "md5";
David Howells2e3fadb2014-07-01 16:40:19 +0100239 break;
240 case OID_sha1:
David Howells566a1172016-04-06 16:13:33 +0100241 ctx->sinfo->sig->hash_algo = "sha1";
David Howells2e3fadb2014-07-01 16:40:19 +0100242 break;
243 case OID_sha256:
David Howells566a1172016-04-06 16:13:33 +0100244 ctx->sinfo->sig->hash_algo = "sha256";
David Howells2e3fadb2014-07-01 16:40:19 +0100245 break;
David Howells07f081f2015-08-30 16:59:57 +0100246 case OID_sha384:
David Howells566a1172016-04-06 16:13:33 +0100247 ctx->sinfo->sig->hash_algo = "sha384";
David Howells07f081f2015-08-30 16:59:57 +0100248 break;
249 case OID_sha512:
David Howells566a1172016-04-06 16:13:33 +0100250 ctx->sinfo->sig->hash_algo = "sha512";
David Howells07f081f2015-08-30 16:59:57 +0100251 break;
252 case OID_sha224:
David Howells566a1172016-04-06 16:13:33 +0100253 ctx->sinfo->sig->hash_algo = "sha224";
254 break;
David Howells2e3fadb2014-07-01 16:40:19 +0100255 default:
256 printk("Unsupported digest algo: %u\n", ctx->last_oid);
257 return -ENOPKG;
258 }
259 return 0;
260}
261
262/*
263 * Note the public key algorithm for the signature.
264 */
265int pkcs7_sig_note_pkey_algo(void *context, size_t hdrlen,
266 unsigned char tag,
267 const void *value, size_t vlen)
268{
269 struct pkcs7_parse_context *ctx = context;
270
271 switch (ctx->last_oid) {
272 case OID_rsaEncryption:
David Howells566a1172016-04-06 16:13:33 +0100273 ctx->sinfo->sig->pkey_algo = "rsa";
David Howells2e3fadb2014-07-01 16:40:19 +0100274 break;
275 default:
276 printk("Unsupported pkey algo: %u\n", ctx->last_oid);
277 return -ENOPKG;
278 }
279 return 0;
280}
281
282/*
David Howells2c7fd362015-07-20 21:16:31 +0100283 * We only support signed data [RFC2315 sec 9].
284 */
285int pkcs7_check_content_type(void *context, size_t hdrlen,
286 unsigned char tag,
287 const void *value, size_t vlen)
288{
289 struct pkcs7_parse_context *ctx = context;
290
291 if (ctx->last_oid != OID_signed_data) {
292 pr_warn("Only support pkcs7_signedData type\n");
293 return -EINVAL;
294 }
295
296 return 0;
297}
298
299/*
300 * Note the SignedData version
301 */
302int pkcs7_note_signeddata_version(void *context, size_t hdrlen,
303 unsigned char tag,
304 const void *value, size_t vlen)
305{
David Howells60d65ca2015-07-20 21:16:33 +0100306 struct pkcs7_parse_context *ctx = context;
David Howells2c7fd362015-07-20 21:16:31 +0100307 unsigned version;
308
309 if (vlen != 1)
310 goto unsupported;
311
David Howells60d65ca2015-07-20 21:16:33 +0100312 ctx->msg->version = version = *(const u8 *)value;
David Howells2c7fd362015-07-20 21:16:31 +0100313 switch (version) {
314 case 1:
David Howells60d65ca2015-07-20 21:16:33 +0100315 /* PKCS#7 SignedData [RFC2315 sec 9.1]
316 * CMS ver 1 SignedData [RFC5652 sec 5.1]
317 */
318 break;
319 case 3:
320 /* CMS ver 3 SignedData [RFC2315 sec 5.1] */
David Howells2c7fd362015-07-20 21:16:31 +0100321 break;
322 default:
323 goto unsupported;
324 }
325
326 return 0;
327
328unsupported:
329 pr_warn("Unsupported SignedData version\n");
330 return -EINVAL;
331}
332
333/*
334 * Note the SignerInfo version
335 */
336int pkcs7_note_signerinfo_version(void *context, size_t hdrlen,
337 unsigned char tag,
338 const void *value, size_t vlen)
339{
David Howells60d65ca2015-07-20 21:16:33 +0100340 struct pkcs7_parse_context *ctx = context;
David Howells2c7fd362015-07-20 21:16:31 +0100341 unsigned version;
342
343 if (vlen != 1)
344 goto unsupported;
345
346 version = *(const u8 *)value;
347 switch (version) {
348 case 1:
David Howells60d65ca2015-07-20 21:16:33 +0100349 /* PKCS#7 SignerInfo [RFC2315 sec 9.2]
350 * CMS ver 1 SignerInfo [RFC5652 sec 5.3]
351 */
352 if (ctx->msg->version != 1)
353 goto version_mismatch;
354 ctx->expect_skid = false;
355 break;
356 case 3:
357 /* CMS ver 3 SignerInfo [RFC2315 sec 5.3] */
358 if (ctx->msg->version == 1)
359 goto version_mismatch;
360 ctx->expect_skid = true;
David Howells2c7fd362015-07-20 21:16:31 +0100361 break;
362 default:
363 goto unsupported;
364 }
365
366 return 0;
367
368unsupported:
369 pr_warn("Unsupported SignerInfo version\n");
370 return -EINVAL;
David Howells60d65ca2015-07-20 21:16:33 +0100371version_mismatch:
372 pr_warn("SignedData-SignerInfo version mismatch\n");
373 return -EBADMSG;
David Howells2c7fd362015-07-20 21:16:31 +0100374}
375
376/*
David Howells2e3fadb2014-07-01 16:40:19 +0100377 * Extract a certificate and store it in the context.
378 */
379int pkcs7_extract_cert(void *context, size_t hdrlen,
380 unsigned char tag,
381 const void *value, size_t vlen)
382{
383 struct pkcs7_parse_context *ctx = context;
384 struct x509_certificate *x509;
385
386 if (tag != ((ASN1_UNIV << 6) | ASN1_CONS_BIT | ASN1_SEQ)) {
387 pr_debug("Cert began with tag %02x at %lu\n",
388 tag, (unsigned long)ctx - ctx->data);
389 return -EBADMSG;
390 }
391
392 /* We have to correct for the header so that the X.509 parser can start
393 * from the beginning. Note that since X.509 stipulates DER, there
394 * probably shouldn't be an EOC trailer - but it is in PKCS#7 (which
395 * stipulates BER).
396 */
397 value -= hdrlen;
398 vlen += hdrlen;
399
400 if (((u8*)value)[1] == 0x80)
401 vlen += 2; /* Indefinite length - there should be an EOC */
402
403 x509 = x509_cert_parse(value, vlen);
404 if (IS_ERR(x509))
405 return PTR_ERR(x509);
406
David Howells2e3fadb2014-07-01 16:40:19 +0100407 x509->index = ++ctx->x509_index;
David Howells46963b72014-09-16 17:36:13 +0100408 pr_debug("Got cert %u for %s\n", x509->index, x509->subject);
409 pr_debug("- fingerprint %*phN\n", x509->id->len, x509->id->data);
410
David Howells2e3fadb2014-07-01 16:40:19 +0100411 *ctx->ppcerts = x509;
412 ctx->ppcerts = &x509->next;
413 return 0;
414}
415
416/*
417 * Save the certificate list
418 */
419int pkcs7_note_certificate_list(void *context, size_t hdrlen,
420 unsigned char tag,
421 const void *value, size_t vlen)
422{
423 struct pkcs7_parse_context *ctx = context;
424
425 pr_devel("Got cert list (%02x)\n", tag);
426
427 *ctx->ppcerts = ctx->msg->certs;
428 ctx->msg->certs = ctx->certs;
429 ctx->certs = NULL;
430 ctx->ppcerts = &ctx->certs;
431 return 0;
432}
433
434/*
David Howells99db4432015-08-05 15:22:27 +0100435 * Note the content type.
436 */
437int pkcs7_note_content(void *context, size_t hdrlen,
438 unsigned char tag,
439 const void *value, size_t vlen)
440{
441 struct pkcs7_parse_context *ctx = context;
442
443 if (ctx->last_oid != OID_data &&
444 ctx->last_oid != OID_msIndirectData) {
445 pr_warn("Unsupported data type %d\n", ctx->last_oid);
446 return -EINVAL;
447 }
448
449 ctx->msg->data_type = ctx->last_oid;
450 return 0;
451}
452
453/*
David Howells2e3fadb2014-07-01 16:40:19 +0100454 * Extract the data from the message and store that and its content type OID in
455 * the context.
456 */
457int pkcs7_note_data(void *context, size_t hdrlen,
458 unsigned char tag,
459 const void *value, size_t vlen)
460{
461 struct pkcs7_parse_context *ctx = context;
462
463 pr_debug("Got data\n");
464
465 ctx->msg->data = value;
466 ctx->msg->data_len = vlen;
467 ctx->msg->data_hdrlen = hdrlen;
David Howells2e3fadb2014-07-01 16:40:19 +0100468 return 0;
469}
470
471/*
David Howells99db4432015-08-05 15:22:27 +0100472 * Parse authenticated attributes.
David Howells2e3fadb2014-07-01 16:40:19 +0100473 */
474int pkcs7_sig_note_authenticated_attr(void *context, size_t hdrlen,
475 unsigned char tag,
476 const void *value, size_t vlen)
477{
478 struct pkcs7_parse_context *ctx = context;
David Howells99db4432015-08-05 15:22:27 +0100479 struct pkcs7_signed_info *sinfo = ctx->sinfo;
480 enum OID content_type;
David Howells2e3fadb2014-07-01 16:40:19 +0100481
482 pr_devel("AuthAttr: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
483
484 switch (ctx->last_oid) {
David Howells99db4432015-08-05 15:22:27 +0100485 case OID_contentType:
486 if (__test_and_set_bit(sinfo_has_content_type, &sinfo->aa_set))
487 goto repeated;
488 content_type = look_up_OID(value, vlen);
489 if (content_type != ctx->msg->data_type) {
490 pr_warn("Mismatch between global data type (%d) and sinfo %u (%d)\n",
491 ctx->msg->data_type, sinfo->index,
492 content_type);
493 return -EBADMSG;
494 }
495 return 0;
496
497 case OID_signingTime:
498 if (__test_and_set_bit(sinfo_has_signing_time, &sinfo->aa_set))
499 goto repeated;
500 /* Should we check that the signing time is consistent
501 * with the signer's X.509 cert?
502 */
503 return x509_decode_time(&sinfo->signing_time,
504 hdrlen, tag, value, vlen);
505
David Howells2e3fadb2014-07-01 16:40:19 +0100506 case OID_messageDigest:
David Howells99db4432015-08-05 15:22:27 +0100507 if (__test_and_set_bit(sinfo_has_message_digest, &sinfo->aa_set))
508 goto repeated;
David Howells2e3fadb2014-07-01 16:40:19 +0100509 if (tag != ASN1_OTS)
510 return -EBADMSG;
David Howells99db4432015-08-05 15:22:27 +0100511 sinfo->msgdigest = value;
512 sinfo->msgdigest_len = vlen;
513 return 0;
514
515 case OID_smimeCapabilites:
516 if (__test_and_set_bit(sinfo_has_smime_caps, &sinfo->aa_set))
517 goto repeated;
518 if (ctx->msg->data_type != OID_msIndirectData) {
519 pr_warn("S/MIME Caps only allowed with Authenticode\n");
520 return -EKEYREJECTED;
521 }
522 return 0;
523
524 /* Microsoft SpOpusInfo seems to be contain cont[0] 16-bit BE
525 * char URLs and cont[1] 8-bit char URLs.
526 *
527 * Microsoft StatementType seems to contain a list of OIDs that
528 * are also used as extendedKeyUsage types in X.509 certs.
529 */
530 case OID_msSpOpusInfo:
531 if (__test_and_set_bit(sinfo_has_ms_opus_info, &sinfo->aa_set))
532 goto repeated;
533 goto authenticode_check;
534 case OID_msStatementType:
535 if (__test_and_set_bit(sinfo_has_ms_statement_type, &sinfo->aa_set))
536 goto repeated;
537 authenticode_check:
538 if (ctx->msg->data_type != OID_msIndirectData) {
539 pr_warn("Authenticode AuthAttrs only allowed with Authenticode\n");
540 return -EKEYREJECTED;
541 }
542 /* I'm not sure how to validate these */
David Howells2e3fadb2014-07-01 16:40:19 +0100543 return 0;
544 default:
545 return 0;
546 }
David Howells99db4432015-08-05 15:22:27 +0100547
548repeated:
549 /* We permit max one item per AuthenticatedAttribute and no repeats */
550 pr_warn("Repeated/multivalue AuthAttrs not permitted\n");
551 return -EKEYREJECTED;
David Howells2e3fadb2014-07-01 16:40:19 +0100552}
553
554/*
David Howells2c7fd362015-07-20 21:16:31 +0100555 * Note the set of auth attributes for digestion purposes [RFC2315 sec 9.3]
David Howells2e3fadb2014-07-01 16:40:19 +0100556 */
557int pkcs7_sig_note_set_of_authattrs(void *context, size_t hdrlen,
558 unsigned char tag,
559 const void *value, size_t vlen)
560{
561 struct pkcs7_parse_context *ctx = context;
David Howells99db4432015-08-05 15:22:27 +0100562 struct pkcs7_signed_info *sinfo = ctx->sinfo;
563
564 if (!test_bit(sinfo_has_content_type, &sinfo->aa_set) ||
Peter Jones7ee70142016-01-18 10:49:58 -0500565 !test_bit(sinfo_has_message_digest, &sinfo->aa_set)) {
David Howells99db4432015-08-05 15:22:27 +0100566 pr_warn("Missing required AuthAttr\n");
567 return -EBADMSG;
568 }
569
570 if (ctx->msg->data_type != OID_msIndirectData &&
571 test_bit(sinfo_has_ms_opus_info, &sinfo->aa_set)) {
572 pr_warn("Unexpected Authenticode AuthAttr\n");
573 return -EBADMSG;
574 }
David Howells2e3fadb2014-07-01 16:40:19 +0100575
576 /* We need to switch the 'CONT 0' to a 'SET OF' when we digest */
David Howells99db4432015-08-05 15:22:27 +0100577 sinfo->authattrs = value - (hdrlen - 1);
578 sinfo->authattrs_len = vlen + (hdrlen - 1);
David Howells2e3fadb2014-07-01 16:40:19 +0100579 return 0;
580}
581
582/*
583 * Note the issuing certificate serial number
584 */
585int pkcs7_sig_note_serial(void *context, size_t hdrlen,
586 unsigned char tag,
587 const void *value, size_t vlen)
588{
589 struct pkcs7_parse_context *ctx = context;
David Howells46963b72014-09-16 17:36:13 +0100590 ctx->raw_serial = value;
591 ctx->raw_serial_size = vlen;
David Howells2e3fadb2014-07-01 16:40:19 +0100592 return 0;
593}
594
595/*
596 * Note the issuer's name
597 */
598int pkcs7_sig_note_issuer(void *context, size_t hdrlen,
599 unsigned char tag,
600 const void *value, size_t vlen)
601{
602 struct pkcs7_parse_context *ctx = context;
David Howells46963b72014-09-16 17:36:13 +0100603 ctx->raw_issuer = value;
604 ctx->raw_issuer_size = vlen;
David Howells2e3fadb2014-07-01 16:40:19 +0100605 return 0;
606}
607
608/*
David Howells60d65ca2015-07-20 21:16:33 +0100609 * Note the issuing cert's subjectKeyIdentifier
610 */
611int pkcs7_sig_note_skid(void *context, size_t hdrlen,
612 unsigned char tag,
613 const void *value, size_t vlen)
614{
615 struct pkcs7_parse_context *ctx = context;
616
617 pr_devel("SKID: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
618
619 ctx->raw_skid = value;
620 ctx->raw_skid_size = vlen;
621 return 0;
622}
623
624/*
David Howells2e3fadb2014-07-01 16:40:19 +0100625 * Note the signature data
626 */
627int pkcs7_sig_note_signature(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 Howells2e3fadb2014-07-01 16:40:19 +0100632
David Howells566a1172016-04-06 16:13:33 +0100633 ctx->sinfo->sig->s = kmemdup(value, vlen, GFP_KERNEL);
634 if (!ctx->sinfo->sig->s)
David Howells2e3fadb2014-07-01 16:40:19 +0100635 return -ENOMEM;
636
David Howells566a1172016-04-06 16:13:33 +0100637 ctx->sinfo->sig->s_size = vlen;
David Howells2e3fadb2014-07-01 16:40:19 +0100638 return 0;
639}
640
641/*
642 * Note a signature information block
643 */
644int pkcs7_note_signed_info(void *context, size_t hdrlen,
645 unsigned char tag,
646 const void *value, size_t vlen)
647{
648 struct pkcs7_parse_context *ctx = context;
David Howells46963b72014-09-16 17:36:13 +0100649 struct pkcs7_signed_info *sinfo = ctx->sinfo;
650 struct asymmetric_key_id *kid;
David Howells2e3fadb2014-07-01 16:40:19 +0100651
David Howells99db4432015-08-05 15:22:27 +0100652 if (ctx->msg->data_type == OID_msIndirectData && !sinfo->authattrs) {
653 pr_warn("Authenticode requires AuthAttrs\n");
654 return -EBADMSG;
655 }
656
David Howells46963b72014-09-16 17:36:13 +0100657 /* Generate cert issuer + serial number key ID */
David Howells60d65ca2015-07-20 21:16:33 +0100658 if (!ctx->expect_skid) {
659 kid = asymmetric_key_generate_id(ctx->raw_serial,
660 ctx->raw_serial_size,
661 ctx->raw_issuer,
662 ctx->raw_issuer_size);
663 } else {
664 kid = asymmetric_key_generate_id(ctx->raw_skid,
665 ctx->raw_skid_size,
666 "", 0);
667 }
David Howells46963b72014-09-16 17:36:13 +0100668 if (IS_ERR(kid))
669 return PTR_ERR(kid);
670
David Howells60d65ca2015-07-20 21:16:33 +0100671 pr_devel("SINFO KID: %u [%*phN]\n", kid->len, kid->len, kid->data);
672
David Howells566a1172016-04-06 16:13:33 +0100673 sinfo->sig->auth_ids[0] = kid;
David Howells46963b72014-09-16 17:36:13 +0100674 sinfo->index = ++ctx->sinfo_index;
675 *ctx->ppsinfo = sinfo;
676 ctx->ppsinfo = &sinfo->next;
David Howells2e3fadb2014-07-01 16:40:19 +0100677 ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
678 if (!ctx->sinfo)
679 return -ENOMEM;
David Howells566a1172016-04-06 16:13:33 +0100680 ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature),
681 GFP_KERNEL);
682 if (!ctx->sinfo->sig)
683 return -ENOMEM;
David Howells2e3fadb2014-07-01 16:40:19 +0100684 return 0;
685}