1 /** 2 * \file pkcs7.h 3 * 4 * \brief PKCS #7 generic defines and structures 5 * https://tools.ietf.org/html/rfc2315 6 */ 7 /* 8 * Copyright The Mbed TLS Contributors 9 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 10 */ 11 12 /** 13 * Note: For the time being, this implementation of the PKCS #7 cryptographic 14 * message syntax is a partial implementation of RFC 2315. 15 * Differences include: 16 * - The RFC specifies 6 different content types. The only type currently 17 * supported in Mbed TLS is the signed-data content type. 18 * - The only supported PKCS #7 Signed Data syntax version is version 1 19 * - The RFC specifies support for BER. This implementation is limited to 20 * DER only. 21 * - The RFC specifies that multiple digest algorithms can be specified 22 * in the Signed Data type. Only one digest algorithm is supported in Mbed TLS. 23 * - The RFC specifies the Signed Data type can contain multiple X.509 or PKCS #6 extended 24 * certificates. In Mbed TLS, this list can only contain 0 or 1 certificates 25 * and they must be in X.509 format. 26 * - The RFC specifies the Signed Data type can contain 27 * certificate-revocation lists (CRLs). This implementation has no support 28 * for CRLs so it is assumed to be an empty list. 29 * - The RFC allows for SignerInfo structure to optionally contain 30 * unauthenticatedAttributes and authenticatedAttributes. In Mbed TLS it is 31 * assumed these fields are empty. 32 * - The RFC allows for the signed Data type to contain contentInfo. This 33 * implementation assumes the type is DATA and the content is empty. 34 */ 35 36 #ifndef MBEDTLS_PKCS7_H 37 #define MBEDTLS_PKCS7_H 38 39 #include "mbedtls/private_access.h" 40 41 #include "mbedtls/build_info.h" 42 43 #include "mbedtls/asn1.h" 44 #include "mbedtls/x509.h" 45 #include "mbedtls/x509_crt.h" 46 47 /** 48 * \name PKCS #7 Module Error codes 49 * \{ 50 */ 51 #define MBEDTLS_ERR_PKCS7_INVALID_FORMAT -0x5300 /**< The format is invalid, e.g. different type expected. */ 52 #define MBEDTLS_ERR_PKCS7_FEATURE_UNAVAILABLE -0x5380 /**< Unavailable feature, e.g. anything other than signed data. */ 53 #define MBEDTLS_ERR_PKCS7_INVALID_VERSION -0x5400 /**< The PKCS #7 version element is invalid or cannot be parsed. */ 54 #define MBEDTLS_ERR_PKCS7_INVALID_CONTENT_INFO -0x5480 /**< The PKCS #7 content info is invalid or cannot be parsed. */ 55 #define MBEDTLS_ERR_PKCS7_INVALID_ALG -0x5500 /**< The algorithm tag or value is invalid or cannot be parsed. */ 56 #define MBEDTLS_ERR_PKCS7_INVALID_CERT -0x5580 /**< The certificate tag or value is invalid or cannot be parsed. */ 57 #define MBEDTLS_ERR_PKCS7_INVALID_SIGNATURE -0x5600 /**< Error parsing the signature */ 58 #define MBEDTLS_ERR_PKCS7_INVALID_SIGNER_INFO -0x5680 /**< Error parsing the signer's info */ 59 #define MBEDTLS_ERR_PKCS7_BAD_INPUT_DATA -0x5700 /**< Input invalid. */ 60 #define MBEDTLS_ERR_PKCS7_ALLOC_FAILED -0x5780 /**< Allocation of memory failed. */ 61 #define MBEDTLS_ERR_PKCS7_VERIFY_FAIL -0x5800 /**< Verification Failed */ 62 #define MBEDTLS_ERR_PKCS7_CERT_DATE_INVALID -0x5880 /**< The PKCS #7 date issued/expired dates are invalid */ 63 /* \} name */ 64 65 /** 66 * \name PKCS #7 Supported Version 67 * \{ 68 */ 69 #define MBEDTLS_PKCS7_SUPPORTED_VERSION 0x01 70 /* \} name */ 71 72 #ifdef __cplusplus 73 extern "C" { 74 #endif 75 76 /** 77 * Type-length-value structure that allows for ASN.1 using DER. 78 */ 79 typedef mbedtls_asn1_buf mbedtls_pkcs7_buf; 80 81 /** 82 * Container for ASN.1 named information objects. 83 * It allows for Relative Distinguished Names (e.g. cn=localhost,ou=code,etc.). 84 */ 85 typedef mbedtls_asn1_named_data mbedtls_pkcs7_name; 86 87 /** 88 * Container for a sequence of ASN.1 items 89 */ 90 typedef mbedtls_asn1_sequence mbedtls_pkcs7_sequence; 91 92 /** 93 * PKCS #7 types 94 */ 95 typedef enum { 96 MBEDTLS_PKCS7_NONE=0, 97 MBEDTLS_PKCS7_DATA, 98 MBEDTLS_PKCS7_SIGNED_DATA, 99 MBEDTLS_PKCS7_ENVELOPED_DATA, 100 MBEDTLS_PKCS7_SIGNED_AND_ENVELOPED_DATA, 101 MBEDTLS_PKCS7_DIGESTED_DATA, 102 MBEDTLS_PKCS7_ENCRYPTED_DATA, 103 } 104 mbedtls_pkcs7_type; 105 106 /** 107 * Structure holding PKCS #7 signer info 108 */ 109 typedef struct mbedtls_pkcs7_signer_info { 110 int MBEDTLS_PRIVATE(version); 111 mbedtls_x509_buf MBEDTLS_PRIVATE(serial); 112 mbedtls_x509_name MBEDTLS_PRIVATE(issuer); 113 mbedtls_x509_buf MBEDTLS_PRIVATE(issuer_raw); 114 mbedtls_x509_buf MBEDTLS_PRIVATE(alg_identifier); 115 mbedtls_x509_buf MBEDTLS_PRIVATE(sig_alg_identifier); 116 mbedtls_x509_buf MBEDTLS_PRIVATE(sig); 117 struct mbedtls_pkcs7_signer_info *MBEDTLS_PRIVATE(next); 118 } 119 mbedtls_pkcs7_signer_info; 120 121 /** 122 * Structure holding the signed data section 123 */ 124 typedef struct mbedtls_pkcs7_signed_data { 125 int MBEDTLS_PRIVATE(version); 126 mbedtls_pkcs7_buf MBEDTLS_PRIVATE(digest_alg_identifiers); 127 int MBEDTLS_PRIVATE(no_of_certs); 128 mbedtls_x509_crt MBEDTLS_PRIVATE(certs); 129 int MBEDTLS_PRIVATE(no_of_crls); 130 mbedtls_x509_crl MBEDTLS_PRIVATE(crl); 131 int MBEDTLS_PRIVATE(no_of_signers); 132 mbedtls_pkcs7_signer_info MBEDTLS_PRIVATE(signers); 133 } 134 mbedtls_pkcs7_signed_data; 135 136 /** 137 * Structure holding PKCS #7 structure, only signed data for now 138 */ 139 typedef struct mbedtls_pkcs7 { 140 mbedtls_pkcs7_buf MBEDTLS_PRIVATE(raw); 141 mbedtls_pkcs7_signed_data MBEDTLS_PRIVATE(signed_data); 142 } 143 mbedtls_pkcs7; 144 145 /** 146 * \brief Initialize mbedtls_pkcs7 structure. 147 * 148 * \param pkcs7 mbedtls_pkcs7 structure. 149 */ 150 void mbedtls_pkcs7_init(mbedtls_pkcs7 *pkcs7); 151 152 /** 153 * \brief Parse a single DER formatted PKCS #7 detached signature. 154 * 155 * \param pkcs7 The mbedtls_pkcs7 structure to be filled by the parser. 156 * \param buf The buffer holding only the DER encoded PKCS #7 content. 157 * \param buflen The size in bytes of \p buf. The size must be exactly the 158 * length of the DER encoded PKCS #7 content. 159 * 160 * \note This function makes an internal copy of the PKCS #7 buffer 161 * \p buf. In particular, \p buf may be destroyed or reused 162 * after this call returns. 163 * \note Signatures with internal data are not supported. 164 * 165 * \return The \c mbedtls_pkcs7_type of \p buf, if successful. 166 * \return A negative error code on failure. 167 */ 168 int mbedtls_pkcs7_parse_der(mbedtls_pkcs7 *pkcs7, const unsigned char *buf, 169 const size_t buflen); 170 171 /** 172 * \brief Verification of PKCS #7 signature against a caller-supplied 173 * certificate. 174 * 175 * For each signer in the PKCS structure, this function computes 176 * a signature over the supplied data, using the supplied 177 * certificate and the same digest algorithm as specified by the 178 * signer. It then compares this signature against the 179 * signer's signature; verification succeeds if any comparison 180 * matches. 181 * 182 * This function does not use the certificates held within the 183 * PKCS #7 structure itself, and does not check that the 184 * certificate is signed by a trusted certification authority. 185 * 186 * \param pkcs7 mbedtls_pkcs7 structure containing signature. 187 * \param cert Certificate containing key to verify signature. 188 * \param data Plain data on which signature has to be verified. 189 * \param datalen Length of the data. 190 * 191 * \note This function internally calculates the hash on the supplied 192 * plain data for signature verification. 193 * 194 * \return 0 if the signature verifies, or a negative error code on failure. 195 */ 196 int mbedtls_pkcs7_signed_data_verify(mbedtls_pkcs7 *pkcs7, 197 const mbedtls_x509_crt *cert, 198 const unsigned char *data, 199 size_t datalen); 200 201 /** 202 * \brief Verification of PKCS #7 signature against a caller-supplied 203 * certificate. 204 * 205 * For each signer in the PKCS structure, this function 206 * validates a signature over the supplied hash, using the 207 * supplied certificate and the same digest algorithm as 208 * specified by the signer. Verification succeeds if any 209 * signature is good. 210 * 211 * This function does not use the certificates held within the 212 * PKCS #7 structure itself, and does not check that the 213 * certificate is signed by a trusted certification authority. 214 * 215 * \param pkcs7 PKCS #7 structure containing signature. 216 * \param cert Certificate containing key to verify signature. 217 * \param hash Hash of the plain data on which signature has to be verified. 218 * \param hashlen Length of the hash. 219 * 220 * \note This function is different from mbedtls_pkcs7_signed_data_verify() 221 * in that it is directly passed the hash of the data. 222 * 223 * \return 0 if the signature verifies, or a negative error code on failure. 224 */ 225 int mbedtls_pkcs7_signed_hash_verify(mbedtls_pkcs7 *pkcs7, 226 const mbedtls_x509_crt *cert, 227 const unsigned char *hash, size_t hashlen); 228 229 /** 230 * \brief Unallocate all PKCS #7 data and zeroize the memory. 231 * It doesn't free \p pkcs7 itself. This should be done by the caller. 232 * 233 * \param pkcs7 mbedtls_pkcs7 structure to free. 234 */ 235 void mbedtls_pkcs7_free(mbedtls_pkcs7 *pkcs7); 236 237 #ifdef __cplusplus 238 } 239 #endif 240 241 #endif /* pkcs7.h */ 242