1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define pr_fmt(fmt) "PKCS7: "fmt
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/export.h>
12 #include <linux/slab.h>
13 #include <linux/err.h>
14 #include <linux/oid_registry.h>
15 #include <crypto/public_key.h>
16 #include "pkcs7_parser.h"
17 #include "pkcs7.asn1.h"
19 MODULE_DESCRIPTION("PKCS#7 parser");
20 MODULE_AUTHOR("Red Hat, Inc.");
21 MODULE_LICENSE("GPL");
23 struct pkcs7_parse_context
{
24 struct pkcs7_message
*msg
; /* Message being constructed */
25 struct pkcs7_signed_info
*sinfo
; /* SignedInfo being constructed */
26 struct pkcs7_signed_info
**ppsinfo
;
27 struct x509_certificate
*certs
; /* Certificate cache */
28 struct x509_certificate
**ppcerts
;
29 unsigned long data
; /* Start of data */
30 enum OID last_oid
; /* Last OID encountered */
33 const void *raw_serial
;
34 unsigned raw_serial_size
;
35 unsigned raw_issuer_size
;
36 const void *raw_issuer
;
38 unsigned raw_skid_size
;
43 * Free a signed information block.
45 static void pkcs7_free_signed_info(struct pkcs7_signed_info
*sinfo
)
48 public_key_signature_free(sinfo
->sig
);
54 * pkcs7_free_message - Free a PKCS#7 message
55 * @pkcs7: The PKCS#7 message to free
57 void pkcs7_free_message(struct pkcs7_message
*pkcs7
)
59 struct x509_certificate
*cert
;
60 struct pkcs7_signed_info
*sinfo
;
63 while (pkcs7
->certs
) {
65 pkcs7
->certs
= cert
->next
;
66 x509_free_certificate(cert
);
70 pkcs7
->crl
= cert
->next
;
71 x509_free_certificate(cert
);
73 while (pkcs7
->signed_infos
) {
74 sinfo
= pkcs7
->signed_infos
;
75 pkcs7
->signed_infos
= sinfo
->next
;
76 pkcs7_free_signed_info(sinfo
);
81 EXPORT_SYMBOL_GPL(pkcs7_free_message
);
84 * Check authenticatedAttributes are provided or not provided consistently.
86 static int pkcs7_check_authattrs(struct pkcs7_message
*msg
)
88 struct pkcs7_signed_info
*sinfo
;
91 sinfo
= msg
->signed_infos
;
95 if (sinfo
->authattrs
) {
97 msg
->have_authattrs
= true;
100 for (sinfo
= sinfo
->next
; sinfo
; sinfo
= sinfo
->next
)
101 if (!!sinfo
->authattrs
!= want
)
106 pr_warn("Inconsistently supplied authAttrs\n");
111 * pkcs7_parse_message - Parse a PKCS#7 message
112 * @data: The raw binary ASN.1 encoded message to be parsed
113 * @datalen: The size of the encoded message
115 struct pkcs7_message
*pkcs7_parse_message(const void *data
, size_t datalen
)
117 struct pkcs7_parse_context
*ctx
;
118 struct pkcs7_message
*msg
= ERR_PTR(-ENOMEM
);
121 ctx
= kzalloc(sizeof(struct pkcs7_parse_context
), GFP_KERNEL
);
124 ctx
->msg
= kzalloc(sizeof(struct pkcs7_message
), GFP_KERNEL
);
127 ctx
->sinfo
= kzalloc(sizeof(struct pkcs7_signed_info
), GFP_KERNEL
);
130 ctx
->sinfo
->sig
= kzalloc(sizeof(struct public_key_signature
),
132 if (!ctx
->sinfo
->sig
)
135 ctx
->data
= (unsigned long)data
;
136 ctx
->ppcerts
= &ctx
->certs
;
137 ctx
->ppsinfo
= &ctx
->msg
->signed_infos
;
139 /* Attempt to decode the signature */
140 ret
= asn1_ber_decoder(&pkcs7_decoder
, ctx
, data
, datalen
);
146 ret
= pkcs7_check_authattrs(ctx
->msg
);
157 struct x509_certificate
*cert
= ctx
->certs
;
158 ctx
->certs
= cert
->next
;
159 x509_free_certificate(cert
);
162 pkcs7_free_signed_info(ctx
->sinfo
);
164 pkcs7_free_message(ctx
->msg
);
170 EXPORT_SYMBOL_GPL(pkcs7_parse_message
);
173 * pkcs7_get_content_data - Get access to the PKCS#7 content
174 * @pkcs7: The preparsed PKCS#7 message to access
175 * @_data: Place to return a pointer to the data
176 * @_data_len: Place to return the data length
177 * @_headerlen: Size of ASN.1 header not included in _data
179 * Get access to the data content of the PKCS#7 message. The size of the
180 * header of the ASN.1 object that contains it is also provided and can be used
181 * to adjust *_data and *_data_len to get the entire object.
183 * Returns -ENODATA if the data object was missing from the message.
185 int pkcs7_get_content_data(const struct pkcs7_message
*pkcs7
,
186 const void **_data
, size_t *_data_len
,
192 *_data
= pkcs7
->data
;
193 *_data_len
= pkcs7
->data_len
;
195 *_headerlen
= pkcs7
->data_hdrlen
;
198 EXPORT_SYMBOL_GPL(pkcs7_get_content_data
);
201 * Note an OID when we find one for later processing when we know how
204 int pkcs7_note_OID(void *context
, size_t hdrlen
,
206 const void *value
, size_t vlen
)
208 struct pkcs7_parse_context
*ctx
= context
;
210 ctx
->last_oid
= look_up_OID(value
, vlen
);
211 if (ctx
->last_oid
== OID__NR
) {
213 sprint_oid(value
, vlen
, buffer
, sizeof(buffer
));
214 printk("PKCS7: Unknown OID: [%lu] %s\n",
215 (unsigned long)value
- ctx
->data
, buffer
);
221 * Note the digest algorithm for the signature.
223 int pkcs7_sig_note_digest_algo(void *context
, size_t hdrlen
,
225 const void *value
, size_t vlen
)
227 struct pkcs7_parse_context
*ctx
= context
;
229 switch (ctx
->last_oid
) {
231 ctx
->sinfo
->sig
->hash_algo
= "md4";
234 ctx
->sinfo
->sig
->hash_algo
= "md5";
237 ctx
->sinfo
->sig
->hash_algo
= "sha1";
240 ctx
->sinfo
->sig
->hash_algo
= "sha256";
243 ctx
->sinfo
->sig
->hash_algo
= "sha384";
246 ctx
->sinfo
->sig
->hash_algo
= "sha512";
249 ctx
->sinfo
->sig
->hash_algo
= "sha224";
252 printk("Unsupported digest algo: %u\n", ctx
->last_oid
);
259 * Note the public key algorithm for the signature.
261 int pkcs7_sig_note_pkey_algo(void *context
, size_t hdrlen
,
263 const void *value
, size_t vlen
)
265 struct pkcs7_parse_context
*ctx
= context
;
267 switch (ctx
->last_oid
) {
268 case OID_rsaEncryption
:
269 ctx
->sinfo
->sig
->pkey_algo
= "rsa";
270 ctx
->sinfo
->sig
->encoding
= "pkcs1";
273 printk("Unsupported pkey algo: %u\n", ctx
->last_oid
);
280 * We only support signed data [RFC2315 sec 9].
282 int pkcs7_check_content_type(void *context
, size_t hdrlen
,
284 const void *value
, size_t vlen
)
286 struct pkcs7_parse_context
*ctx
= context
;
288 if (ctx
->last_oid
!= OID_signed_data
) {
289 pr_warn("Only support pkcs7_signedData type\n");
297 * Note the SignedData version
299 int pkcs7_note_signeddata_version(void *context
, size_t hdrlen
,
301 const void *value
, size_t vlen
)
303 struct pkcs7_parse_context
*ctx
= context
;
309 ctx
->msg
->version
= version
= *(const u8
*)value
;
312 /* PKCS#7 SignedData [RFC2315 sec 9.1]
313 * CMS ver 1 SignedData [RFC5652 sec 5.1]
317 /* CMS ver 3 SignedData [RFC2315 sec 5.1] */
326 pr_warn("Unsupported SignedData version\n");
331 * Note the SignerInfo version
333 int pkcs7_note_signerinfo_version(void *context
, size_t hdrlen
,
335 const void *value
, size_t vlen
)
337 struct pkcs7_parse_context
*ctx
= context
;
343 version
= *(const u8
*)value
;
346 /* PKCS#7 SignerInfo [RFC2315 sec 9.2]
347 * CMS ver 1 SignerInfo [RFC5652 sec 5.3]
349 if (ctx
->msg
->version
!= 1)
350 goto version_mismatch
;
351 ctx
->expect_skid
= false;
354 /* CMS ver 3 SignerInfo [RFC2315 sec 5.3] */
355 if (ctx
->msg
->version
== 1)
356 goto version_mismatch
;
357 ctx
->expect_skid
= true;
366 pr_warn("Unsupported SignerInfo version\n");
369 pr_warn("SignedData-SignerInfo version mismatch\n");
374 * Extract a certificate and store it in the context.
376 int pkcs7_extract_cert(void *context
, size_t hdrlen
,
378 const void *value
, size_t vlen
)
380 struct pkcs7_parse_context
*ctx
= context
;
381 struct x509_certificate
*x509
;
383 if (tag
!= ((ASN1_UNIV
<< 6) | ASN1_CONS_BIT
| ASN1_SEQ
)) {
384 pr_debug("Cert began with tag %02x at %lu\n",
385 tag
, (unsigned long)ctx
- ctx
->data
);
389 /* We have to correct for the header so that the X.509 parser can start
390 * from the beginning. Note that since X.509 stipulates DER, there
391 * probably shouldn't be an EOC trailer - but it is in PKCS#7 (which
397 if (((u8
*)value
)[1] == 0x80)
398 vlen
+= 2; /* Indefinite length - there should be an EOC */
400 x509
= x509_cert_parse(value
, vlen
);
402 return PTR_ERR(x509
);
404 x509
->index
= ++ctx
->x509_index
;
405 pr_debug("Got cert %u for %s\n", x509
->index
, x509
->subject
);
406 pr_debug("- fingerprint %*phN\n", x509
->id
->len
, x509
->id
->data
);
408 *ctx
->ppcerts
= x509
;
409 ctx
->ppcerts
= &x509
->next
;
414 * Save the certificate list
416 int pkcs7_note_certificate_list(void *context
, size_t hdrlen
,
418 const void *value
, size_t vlen
)
420 struct pkcs7_parse_context
*ctx
= context
;
422 pr_devel("Got cert list (%02x)\n", tag
);
424 *ctx
->ppcerts
= ctx
->msg
->certs
;
425 ctx
->msg
->certs
= ctx
->certs
;
427 ctx
->ppcerts
= &ctx
->certs
;
432 * Note the content type.
434 int pkcs7_note_content(void *context
, size_t hdrlen
,
436 const void *value
, size_t vlen
)
438 struct pkcs7_parse_context
*ctx
= context
;
440 if (ctx
->last_oid
!= OID_data
&&
441 ctx
->last_oid
!= OID_msIndirectData
) {
442 pr_warn("Unsupported data type %d\n", ctx
->last_oid
);
446 ctx
->msg
->data_type
= ctx
->last_oid
;
451 * Extract the data from the message and store that and its content type OID in
454 int pkcs7_note_data(void *context
, size_t hdrlen
,
456 const void *value
, size_t vlen
)
458 struct pkcs7_parse_context
*ctx
= context
;
460 pr_debug("Got data\n");
462 ctx
->msg
->data
= value
;
463 ctx
->msg
->data_len
= vlen
;
464 ctx
->msg
->data_hdrlen
= hdrlen
;
469 * Parse authenticated attributes.
471 int pkcs7_sig_note_authenticated_attr(void *context
, size_t hdrlen
,
473 const void *value
, size_t vlen
)
475 struct pkcs7_parse_context
*ctx
= context
;
476 struct pkcs7_signed_info
*sinfo
= ctx
->sinfo
;
477 enum OID content_type
;
479 pr_devel("AuthAttr: %02x %zu [%*ph]\n", tag
, vlen
, (unsigned)vlen
, value
);
481 switch (ctx
->last_oid
) {
482 case OID_contentType
:
483 if (__test_and_set_bit(sinfo_has_content_type
, &sinfo
->aa_set
))
485 content_type
= look_up_OID(value
, vlen
);
486 if (content_type
!= ctx
->msg
->data_type
) {
487 pr_warn("Mismatch between global data type (%d) and sinfo %u (%d)\n",
488 ctx
->msg
->data_type
, sinfo
->index
,
494 case OID_signingTime
:
495 if (__test_and_set_bit(sinfo_has_signing_time
, &sinfo
->aa_set
))
497 /* Should we check that the signing time is consistent
498 * with the signer's X.509 cert?
500 return x509_decode_time(&sinfo
->signing_time
,
501 hdrlen
, tag
, value
, vlen
);
503 case OID_messageDigest
:
504 if (__test_and_set_bit(sinfo_has_message_digest
, &sinfo
->aa_set
))
508 sinfo
->msgdigest
= value
;
509 sinfo
->msgdigest_len
= vlen
;
512 case OID_smimeCapabilites
:
513 if (__test_and_set_bit(sinfo_has_smime_caps
, &sinfo
->aa_set
))
515 if (ctx
->msg
->data_type
!= OID_msIndirectData
) {
516 pr_warn("S/MIME Caps only allowed with Authenticode\n");
517 return -EKEYREJECTED
;
521 /* Microsoft SpOpusInfo seems to be contain cont[0] 16-bit BE
522 * char URLs and cont[1] 8-bit char URLs.
524 * Microsoft StatementType seems to contain a list of OIDs that
525 * are also used as extendedKeyUsage types in X.509 certs.
527 case OID_msSpOpusInfo
:
528 if (__test_and_set_bit(sinfo_has_ms_opus_info
, &sinfo
->aa_set
))
530 goto authenticode_check
;
531 case OID_msStatementType
:
532 if (__test_and_set_bit(sinfo_has_ms_statement_type
, &sinfo
->aa_set
))
535 if (ctx
->msg
->data_type
!= OID_msIndirectData
) {
536 pr_warn("Authenticode AuthAttrs only allowed with Authenticode\n");
537 return -EKEYREJECTED
;
539 /* I'm not sure how to validate these */
546 /* We permit max one item per AuthenticatedAttribute and no repeats */
547 pr_warn("Repeated/multivalue AuthAttrs not permitted\n");
548 return -EKEYREJECTED
;
552 * Note the set of auth attributes for digestion purposes [RFC2315 sec 9.3]
554 int pkcs7_sig_note_set_of_authattrs(void *context
, size_t hdrlen
,
556 const void *value
, size_t vlen
)
558 struct pkcs7_parse_context
*ctx
= context
;
559 struct pkcs7_signed_info
*sinfo
= ctx
->sinfo
;
561 if (!test_bit(sinfo_has_content_type
, &sinfo
->aa_set
) ||
562 !test_bit(sinfo_has_message_digest
, &sinfo
->aa_set
)) {
563 pr_warn("Missing required AuthAttr\n");
567 if (ctx
->msg
->data_type
!= OID_msIndirectData
&&
568 test_bit(sinfo_has_ms_opus_info
, &sinfo
->aa_set
)) {
569 pr_warn("Unexpected Authenticode AuthAttr\n");
573 /* We need to switch the 'CONT 0' to a 'SET OF' when we digest */
574 sinfo
->authattrs
= value
- (hdrlen
- 1);
575 sinfo
->authattrs_len
= vlen
+ (hdrlen
- 1);
580 * Note the issuing certificate serial number
582 int pkcs7_sig_note_serial(void *context
, size_t hdrlen
,
584 const void *value
, size_t vlen
)
586 struct pkcs7_parse_context
*ctx
= context
;
587 ctx
->raw_serial
= value
;
588 ctx
->raw_serial_size
= vlen
;
593 * Note the issuer's name
595 int pkcs7_sig_note_issuer(void *context
, size_t hdrlen
,
597 const void *value
, size_t vlen
)
599 struct pkcs7_parse_context
*ctx
= context
;
600 ctx
->raw_issuer
= value
;
601 ctx
->raw_issuer_size
= vlen
;
606 * Note the issuing cert's subjectKeyIdentifier
608 int pkcs7_sig_note_skid(void *context
, size_t hdrlen
,
610 const void *value
, size_t vlen
)
612 struct pkcs7_parse_context
*ctx
= context
;
614 pr_devel("SKID: %02x %zu [%*ph]\n", tag
, vlen
, (unsigned)vlen
, value
);
616 ctx
->raw_skid
= value
;
617 ctx
->raw_skid_size
= vlen
;
622 * Note the signature data
624 int pkcs7_sig_note_signature(void *context
, size_t hdrlen
,
626 const void *value
, size_t vlen
)
628 struct pkcs7_parse_context
*ctx
= context
;
630 ctx
->sinfo
->sig
->s
= kmemdup(value
, vlen
, GFP_KERNEL
);
631 if (!ctx
->sinfo
->sig
->s
)
634 ctx
->sinfo
->sig
->s_size
= vlen
;
639 * Note a signature information block
641 int pkcs7_note_signed_info(void *context
, size_t hdrlen
,
643 const void *value
, size_t vlen
)
645 struct pkcs7_parse_context
*ctx
= context
;
646 struct pkcs7_signed_info
*sinfo
= ctx
->sinfo
;
647 struct asymmetric_key_id
*kid
;
649 if (ctx
->msg
->data_type
== OID_msIndirectData
&& !sinfo
->authattrs
) {
650 pr_warn("Authenticode requires AuthAttrs\n");
654 /* Generate cert issuer + serial number key ID */
655 if (!ctx
->expect_skid
) {
656 kid
= asymmetric_key_generate_id(ctx
->raw_serial
,
657 ctx
->raw_serial_size
,
659 ctx
->raw_issuer_size
);
661 kid
= asymmetric_key_generate_id(ctx
->raw_skid
,
668 pr_devel("SINFO KID: %u [%*phN]\n", kid
->len
, kid
->len
, kid
->data
);
670 sinfo
->sig
->auth_ids
[0] = kid
;
671 sinfo
->index
= ++ctx
->sinfo_index
;
672 *ctx
->ppsinfo
= sinfo
;
673 ctx
->ppsinfo
= &sinfo
->next
;
674 ctx
->sinfo
= kzalloc(sizeof(struct pkcs7_signed_info
), GFP_KERNEL
);
677 ctx
->sinfo
->sig
= kzalloc(sizeof(struct public_key_signature
),
679 if (!ctx
->sinfo
->sig
)