3 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
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 <crypto/public_key.h>
19 #include "pkcs7_parser.h"
20 #include "pkcs7-asn1.h"
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 */
32 const void *raw_serial
;
33 unsigned raw_serial_size
;
34 unsigned raw_issuer_size
;
35 const void *raw_issuer
;
37 unsigned raw_skid_size
;
42 * Free a signed information block.
44 static void pkcs7_free_signed_info(struct pkcs7_signed_info
*sinfo
)
47 public_key_signature_free(sinfo
->sig
);
53 * pkcs7_free_message - Free a PKCS#7 message
54 * @pkcs7: The PKCS#7 message to free
56 void pkcs7_free_message(struct pkcs7_message
*pkcs7
)
58 struct x509_certificate
*cert
;
59 struct pkcs7_signed_info
*sinfo
;
62 while (pkcs7
->certs
) {
64 pkcs7
->certs
= cert
->next
;
65 x509_free_certificate(cert
);
69 pkcs7
->crl
= cert
->next
;
70 x509_free_certificate(cert
);
72 while (pkcs7
->signed_infos
) {
73 sinfo
= pkcs7
->signed_infos
;
74 pkcs7
->signed_infos
= sinfo
->next
;
75 pkcs7_free_signed_info(sinfo
);
80 EXPORT_SYMBOL_GPL(pkcs7_free_message
);
83 * Check authenticatedAttributes are provided or not provided consistently.
85 static int pkcs7_check_authattrs(struct pkcs7_message
*msg
)
87 struct pkcs7_signed_info
*sinfo
;
90 sinfo
= msg
->signed_infos
;
94 if (sinfo
->authattrs
) {
96 msg
->have_authattrs
= true;
99 for (sinfo
= sinfo
->next
; sinfo
; sinfo
= sinfo
->next
)
100 if (!!sinfo
->authattrs
!= want
)
105 pr_warn("Inconsistently supplied authAttrs\n");
110 * pkcs7_parse_message - Parse a PKCS#7 message
111 * @data: The raw binary ASN.1 encoded message to be parsed
112 * @datalen: The size of the encoded message
114 struct pkcs7_message
*pkcs7_parse_message(const void *data
, size_t datalen
)
116 struct pkcs7_parse_context
*ctx
;
117 struct pkcs7_message
*msg
= ERR_PTR(-ENOMEM
);
120 ctx
= kzalloc(sizeof(struct pkcs7_parse_context
), GFP_KERNEL
);
123 ctx
->msg
= kzalloc(sizeof(struct pkcs7_message
), GFP_KERNEL
);
126 ctx
->sinfo
= kzalloc(sizeof(struct pkcs7_signed_info
), GFP_KERNEL
);
129 ctx
->sinfo
->sig
= kzalloc(sizeof(struct public_key_signature
),
131 if (!ctx
->sinfo
->sig
)
134 ctx
->data
= (unsigned long)data
;
135 ctx
->ppcerts
= &ctx
->certs
;
136 ctx
->ppsinfo
= &ctx
->msg
->signed_infos
;
138 /* Attempt to decode the signature */
139 ret
= asn1_ber_decoder(&pkcs7_decoder
, ctx
, data
, datalen
);
145 ret
= pkcs7_check_authattrs(ctx
->msg
);
154 struct x509_certificate
*cert
= ctx
->certs
;
155 ctx
->certs
= cert
->next
;
156 x509_free_certificate(cert
);
159 pkcs7_free_signed_info(ctx
->sinfo
);
161 pkcs7_free_message(ctx
->msg
);
167 EXPORT_SYMBOL_GPL(pkcs7_parse_message
);
170 * pkcs7_get_content_data - Get access to the PKCS#7 content
171 * @pkcs7: The preparsed PKCS#7 message to access
172 * @_data: Place to return a pointer to the data
173 * @_data_len: Place to return the data length
174 * @_headerlen: Size of ASN.1 header not included in _data
176 * Get access to the data content of the PKCS#7 message. The size of the
177 * header of the ASN.1 object that contains it is also provided and can be used
178 * to adjust *_data and *_data_len to get the entire object.
180 * Returns -ENODATA if the data object was missing from the message.
182 int pkcs7_get_content_data(const struct pkcs7_message
*pkcs7
,
183 const void **_data
, size_t *_data_len
,
189 *_data
= pkcs7
->data
;
190 *_data_len
= pkcs7
->data_len
;
192 *_headerlen
= pkcs7
->data_hdrlen
;
195 EXPORT_SYMBOL_GPL(pkcs7_get_content_data
);
198 * Note an OID when we find one for later processing when we know how
201 int pkcs7_note_OID(void *context
, size_t hdrlen
,
203 const void *value
, size_t vlen
)
205 struct pkcs7_parse_context
*ctx
= context
;
207 ctx
->last_oid
= look_up_OID(value
, vlen
);
208 if (ctx
->last_oid
== OID__NR
) {
210 sprint_oid(value
, vlen
, buffer
, sizeof(buffer
));
211 printk("PKCS7: Unknown OID: [%lu] %s\n",
212 (unsigned long)value
- ctx
->data
, buffer
);
218 * Note the digest algorithm for the signature.
220 int pkcs7_sig_note_digest_algo(void *context
, size_t hdrlen
,
222 const void *value
, size_t vlen
)
224 struct pkcs7_parse_context
*ctx
= context
;
226 switch (ctx
->last_oid
) {
228 ctx
->sinfo
->sig
->hash_algo
= "md4";
231 ctx
->sinfo
->sig
->hash_algo
= "md5";
234 ctx
->sinfo
->sig
->hash_algo
= "sha1";
237 ctx
->sinfo
->sig
->hash_algo
= "sha256";
240 ctx
->sinfo
->sig
->hash_algo
= "sha384";
243 ctx
->sinfo
->sig
->hash_algo
= "sha512";
246 ctx
->sinfo
->sig
->hash_algo
= "sha224";
249 printk("Unsupported digest algo: %u\n", ctx
->last_oid
);
256 * Note the public key algorithm for the signature.
258 int pkcs7_sig_note_pkey_algo(void *context
, size_t hdrlen
,
260 const void *value
, size_t vlen
)
262 struct pkcs7_parse_context
*ctx
= context
;
264 switch (ctx
->last_oid
) {
265 case OID_rsaEncryption
:
266 ctx
->sinfo
->sig
->pkey_algo
= "rsa";
269 printk("Unsupported pkey algo: %u\n", ctx
->last_oid
);
276 * We only support signed data [RFC2315 sec 9].
278 int pkcs7_check_content_type(void *context
, size_t hdrlen
,
280 const void *value
, size_t vlen
)
282 struct pkcs7_parse_context
*ctx
= context
;
284 if (ctx
->last_oid
!= OID_signed_data
) {
285 pr_warn("Only support pkcs7_signedData type\n");
293 * Note the SignedData version
295 int pkcs7_note_signeddata_version(void *context
, size_t hdrlen
,
297 const void *value
, size_t vlen
)
299 struct pkcs7_parse_context
*ctx
= context
;
305 ctx
->msg
->version
= version
= *(const u8
*)value
;
308 /* PKCS#7 SignedData [RFC2315 sec 9.1]
309 * CMS ver 1 SignedData [RFC5652 sec 5.1]
313 /* CMS ver 3 SignedData [RFC2315 sec 5.1] */
322 pr_warn("Unsupported SignedData version\n");
327 * Note the SignerInfo version
329 int pkcs7_note_signerinfo_version(void *context
, size_t hdrlen
,
331 const void *value
, size_t vlen
)
333 struct pkcs7_parse_context
*ctx
= context
;
339 version
= *(const u8
*)value
;
342 /* PKCS#7 SignerInfo [RFC2315 sec 9.2]
343 * CMS ver 1 SignerInfo [RFC5652 sec 5.3]
345 if (ctx
->msg
->version
!= 1)
346 goto version_mismatch
;
347 ctx
->expect_skid
= false;
350 /* CMS ver 3 SignerInfo [RFC2315 sec 5.3] */
351 if (ctx
->msg
->version
== 1)
352 goto version_mismatch
;
353 ctx
->expect_skid
= true;
362 pr_warn("Unsupported SignerInfo version\n");
365 pr_warn("SignedData-SignerInfo version mismatch\n");
370 * Extract a certificate and store it in the context.
372 int pkcs7_extract_cert(void *context
, size_t hdrlen
,
374 const void *value
, size_t vlen
)
376 struct pkcs7_parse_context
*ctx
= context
;
377 struct x509_certificate
*x509
;
379 if (tag
!= ((ASN1_UNIV
<< 6) | ASN1_CONS_BIT
| ASN1_SEQ
)) {
380 pr_debug("Cert began with tag %02x at %lu\n",
381 tag
, (unsigned long)ctx
- ctx
->data
);
385 /* We have to correct for the header so that the X.509 parser can start
386 * from the beginning. Note that since X.509 stipulates DER, there
387 * probably shouldn't be an EOC trailer - but it is in PKCS#7 (which
393 if (((u8
*)value
)[1] == 0x80)
394 vlen
+= 2; /* Indefinite length - there should be an EOC */
396 x509
= x509_cert_parse(value
, vlen
);
398 return PTR_ERR(x509
);
400 x509
->index
= ++ctx
->x509_index
;
401 pr_debug("Got cert %u for %s\n", x509
->index
, x509
->subject
);
402 pr_debug("- fingerprint %*phN\n", x509
->id
->len
, x509
->id
->data
);
404 *ctx
->ppcerts
= x509
;
405 ctx
->ppcerts
= &x509
->next
;
410 * Save the certificate list
412 int pkcs7_note_certificate_list(void *context
, size_t hdrlen
,
414 const void *value
, size_t vlen
)
416 struct pkcs7_parse_context
*ctx
= context
;
418 pr_devel("Got cert list (%02x)\n", tag
);
420 *ctx
->ppcerts
= ctx
->msg
->certs
;
421 ctx
->msg
->certs
= ctx
->certs
;
423 ctx
->ppcerts
= &ctx
->certs
;
428 * Note the content type.
430 int pkcs7_note_content(void *context
, size_t hdrlen
,
432 const void *value
, size_t vlen
)
434 struct pkcs7_parse_context
*ctx
= context
;
436 if (ctx
->last_oid
!= OID_data
&&
437 ctx
->last_oid
!= OID_msIndirectData
) {
438 pr_warn("Unsupported data type %d\n", ctx
->last_oid
);
442 ctx
->msg
->data_type
= ctx
->last_oid
;
447 * Extract the data from the message and store that and its content type OID in
450 int pkcs7_note_data(void *context
, size_t hdrlen
,
452 const void *value
, size_t vlen
)
454 struct pkcs7_parse_context
*ctx
= context
;
456 pr_debug("Got data\n");
458 ctx
->msg
->data
= value
;
459 ctx
->msg
->data_len
= vlen
;
460 ctx
->msg
->data_hdrlen
= hdrlen
;
465 * Parse authenticated attributes.
467 int pkcs7_sig_note_authenticated_attr(void *context
, size_t hdrlen
,
469 const void *value
, size_t vlen
)
471 struct pkcs7_parse_context
*ctx
= context
;
472 struct pkcs7_signed_info
*sinfo
= ctx
->sinfo
;
473 enum OID content_type
;
475 pr_devel("AuthAttr: %02x %zu [%*ph]\n", tag
, vlen
, (unsigned)vlen
, value
);
477 switch (ctx
->last_oid
) {
478 case OID_contentType
:
479 if (__test_and_set_bit(sinfo_has_content_type
, &sinfo
->aa_set
))
481 content_type
= look_up_OID(value
, vlen
);
482 if (content_type
!= ctx
->msg
->data_type
) {
483 pr_warn("Mismatch between global data type (%d) and sinfo %u (%d)\n",
484 ctx
->msg
->data_type
, sinfo
->index
,
490 case OID_signingTime
:
491 if (__test_and_set_bit(sinfo_has_signing_time
, &sinfo
->aa_set
))
493 /* Should we check that the signing time is consistent
494 * with the signer's X.509 cert?
496 return x509_decode_time(&sinfo
->signing_time
,
497 hdrlen
, tag
, value
, vlen
);
499 case OID_messageDigest
:
500 if (__test_and_set_bit(sinfo_has_message_digest
, &sinfo
->aa_set
))
504 sinfo
->msgdigest
= value
;
505 sinfo
->msgdigest_len
= vlen
;
508 case OID_smimeCapabilites
:
509 if (__test_and_set_bit(sinfo_has_smime_caps
, &sinfo
->aa_set
))
511 if (ctx
->msg
->data_type
!= OID_msIndirectData
) {
512 pr_warn("S/MIME Caps only allowed with Authenticode\n");
513 return -EKEYREJECTED
;
517 /* Microsoft SpOpusInfo seems to be contain cont[0] 16-bit BE
518 * char URLs and cont[1] 8-bit char URLs.
520 * Microsoft StatementType seems to contain a list of OIDs that
521 * are also used as extendedKeyUsage types in X.509 certs.
523 case OID_msSpOpusInfo
:
524 if (__test_and_set_bit(sinfo_has_ms_opus_info
, &sinfo
->aa_set
))
526 goto authenticode_check
;
527 case OID_msStatementType
:
528 if (__test_and_set_bit(sinfo_has_ms_statement_type
, &sinfo
->aa_set
))
531 if (ctx
->msg
->data_type
!= OID_msIndirectData
) {
532 pr_warn("Authenticode AuthAttrs only allowed with Authenticode\n");
533 return -EKEYREJECTED
;
535 /* I'm not sure how to validate these */
542 /* We permit max one item per AuthenticatedAttribute and no repeats */
543 pr_warn("Repeated/multivalue AuthAttrs not permitted\n");
544 return -EKEYREJECTED
;
548 * Note the set of auth attributes for digestion purposes [RFC2315 sec 9.3]
550 int pkcs7_sig_note_set_of_authattrs(void *context
, size_t hdrlen
,
552 const void *value
, size_t vlen
)
554 struct pkcs7_parse_context
*ctx
= context
;
555 struct pkcs7_signed_info
*sinfo
= ctx
->sinfo
;
557 if (!test_bit(sinfo_has_content_type
, &sinfo
->aa_set
) ||
558 !test_bit(sinfo_has_message_digest
, &sinfo
->aa_set
)) {
559 pr_warn("Missing required AuthAttr\n");
563 if (ctx
->msg
->data_type
!= OID_msIndirectData
&&
564 test_bit(sinfo_has_ms_opus_info
, &sinfo
->aa_set
)) {
565 pr_warn("Unexpected Authenticode AuthAttr\n");
569 /* We need to switch the 'CONT 0' to a 'SET OF' when we digest */
570 sinfo
->authattrs
= value
- (hdrlen
- 1);
571 sinfo
->authattrs_len
= vlen
+ (hdrlen
- 1);
576 * Note the issuing certificate serial number
578 int pkcs7_sig_note_serial(void *context
, size_t hdrlen
,
580 const void *value
, size_t vlen
)
582 struct pkcs7_parse_context
*ctx
= context
;
583 ctx
->raw_serial
= value
;
584 ctx
->raw_serial_size
= vlen
;
589 * Note the issuer's name
591 int pkcs7_sig_note_issuer(void *context
, size_t hdrlen
,
593 const void *value
, size_t vlen
)
595 struct pkcs7_parse_context
*ctx
= context
;
596 ctx
->raw_issuer
= value
;
597 ctx
->raw_issuer_size
= vlen
;
602 * Note the issuing cert's subjectKeyIdentifier
604 int pkcs7_sig_note_skid(void *context
, size_t hdrlen
,
606 const void *value
, size_t vlen
)
608 struct pkcs7_parse_context
*ctx
= context
;
610 pr_devel("SKID: %02x %zu [%*ph]\n", tag
, vlen
, (unsigned)vlen
, value
);
612 ctx
->raw_skid
= value
;
613 ctx
->raw_skid_size
= vlen
;
618 * Note the signature data
620 int pkcs7_sig_note_signature(void *context
, size_t hdrlen
,
622 const void *value
, size_t vlen
)
624 struct pkcs7_parse_context
*ctx
= context
;
626 ctx
->sinfo
->sig
->s
= kmemdup(value
, vlen
, GFP_KERNEL
);
627 if (!ctx
->sinfo
->sig
->s
)
630 ctx
->sinfo
->sig
->s_size
= vlen
;
635 * Note a signature information block
637 int pkcs7_note_signed_info(void *context
, size_t hdrlen
,
639 const void *value
, size_t vlen
)
641 struct pkcs7_parse_context
*ctx
= context
;
642 struct pkcs7_signed_info
*sinfo
= ctx
->sinfo
;
643 struct asymmetric_key_id
*kid
;
645 if (ctx
->msg
->data_type
== OID_msIndirectData
&& !sinfo
->authattrs
) {
646 pr_warn("Authenticode requires AuthAttrs\n");
650 /* Generate cert issuer + serial number key ID */
651 if (!ctx
->expect_skid
) {
652 kid
= asymmetric_key_generate_id(ctx
->raw_serial
,
653 ctx
->raw_serial_size
,
655 ctx
->raw_issuer_size
);
657 kid
= asymmetric_key_generate_id(ctx
->raw_skid
,
664 pr_devel("SINFO KID: %u [%*phN]\n", kid
->len
, kid
->len
, kid
->data
);
666 sinfo
->sig
->auth_ids
[0] = kid
;
667 sinfo
->index
= ++ctx
->sinfo_index
;
668 *ctx
->ppsinfo
= sinfo
;
669 ctx
->ppsinfo
= &sinfo
->next
;
670 ctx
->sinfo
= kzalloc(sizeof(struct pkcs7_signed_info
), GFP_KERNEL
);
673 ctx
->sinfo
->sig
= kzalloc(sizeof(struct public_key_signature
),
675 if (!ctx
->sinfo
->sig
)