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