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