1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* PKCS#7 crypto data parser internal definitions
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
8 #ifndef _PKCS7_PARSER_H
9 #define _PKCS7_PARSER_H
11 #include <linux/oid_registry.h>
12 #include <crypto/pkcs7.h>
13 #include <crypto/x509_parser.h>
14 #if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
15 #include <mbedtls/pkcs7.h>
16 #include <library/x509_internal.h>
17 #include <mbedtls/asn1.h>
18 #include <mbedtls/oid.h>
20 #include <linux/printk.h>
22 #define kenter(FMT, ...) \
23 pr_devel("==> %s("FMT")\n", __func__, ##__VA_ARGS__)
24 #define kleave(FMT, ...) \
25 pr_devel("<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
27 /* Backup the parsed MedTLS context that we need */
28 #if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
29 struct pkcs7_mbedtls_ctx {
33 struct pkcs7_sinfo_mbedtls_ctx {
35 void *content_data_digest;
40 * MbedTLS integration Notes:
42 * MbedTLS PKCS#7 library does not originally support parsing MicroSoft
43 * Authentication Code which is used for verifying the PE image digest.
45 * 1. Authenticated Attributes (authenticatedAttributes)
46 * MbedTLS assumes unauthenticatedAttributes and authenticatedAttributes
48 * See MbedTLS function 'pkcs7_get_signer_info' for details.
50 * 2. MicroSoft Authentication Code (mscode)
51 * MbedTLS only supports Content Data type defined as 1.2.840.113549.1.7.1
52 * (MBEDTLS_OID_PKCS7_DATA, aka OID_data).
53 * 1.3.6.1.4.1.311.2.1.4 (MicroSoft Authentication Code, aka
54 * OID_msIndirectData) is not supported.
55 * See MbedTLS function 'pkcs7_get_content_info_type' for details.
57 * But the EFI loader assumes that a PKCS#7 message with an EFI image always
58 * contains MicroSoft Authentication Code as Content Data (msg->data is NOT
59 * NULL), see function 'efi_signature_verify'.
61 * MbedTLS patch "0002-support-MicroSoft-authentication-code-in-PKCS7-lib.patch"
62 * is to support both above features by parsing the Content Data and
63 * Authenticate Attributes from a given PKCS#7 message.
65 * Other fields we don't need to populate from MbedTLS, which are used
66 * internally by pkcs7_verify:
67 * 'signer', 'unsupported_crypto', 'blacklisted'
68 * 'sig->digest' is used internally by pkcs7_digest to calculate the hash of
69 * Content Data or Authenticate Attributes.
71 struct pkcs7_signed_info {
72 #if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
73 struct pkcs7_sinfo_mbedtls_ctx *mbedtls_ctx;
75 struct pkcs7_signed_info *next;
76 struct x509_certificate *signer; /* Signing certificate (in msg->certs) */
78 bool unsupported_crypto; /* T if not usable due to missing crypto */
81 /* Message digest - the digest of the Content Data (or NULL) */
82 const void *msgdigest;
83 unsigned msgdigest_len;
85 /* Authenticated Attribute data (or NULL) */
86 unsigned authattrs_len;
87 const void *authattrs;
89 #define sinfo_has_content_type 0
90 #define sinfo_has_signing_time 1
91 #define sinfo_has_message_digest 2
92 #define sinfo_has_smime_caps 3
93 #define sinfo_has_ms_opus_info 4
94 #define sinfo_has_ms_statement_type 5
95 time64_t signing_time;
99 * This contains the generated digest of _either_ the Content Data or
100 * the Authenticated Attributes [RFC2315 9.3]. If the latter, one of
101 * the attributes contains the digest of the the Content Data within
104 * THis also contains the issuing cert serial number and issuer's name
105 * [PKCS#7 or CMS ver 1] or issuing cert's SKID [CMS ver 3].
107 struct public_key_signature *sig;
110 struct pkcs7_message {
111 #if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
112 struct pkcs7_mbedtls_ctx *mbedtls_ctx;
114 struct x509_certificate *certs; /* Certificate list */
115 struct x509_certificate *crl; /* Revocation list */
116 struct pkcs7_signed_info *signed_infos;
117 u8 version; /* Version of cert (1 -> PKCS#7 or CMS; 3 -> CMS) */
118 bool have_authattrs; /* T if have authattrs */
120 /* Content Data (or NULL) */
121 enum OID data_type; /* Type of Data */
122 size_t data_len; /* Length of Data */
123 size_t data_hdrlen; /* Length of Data ASN.1 header */
124 const void *data; /* Content Data (or 0) */
126 #endif /* _PKCS7_PARSER_H */