scorpiox-bot-plugin-webhook
←pkcs7.hc
252 lines · 10 KBHEADBlameDownload
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 * - The RFC doesn't put any constrain on the hash algorithm to be used, but
35 * this implementation by default rejects weak hash algorithms (i.e. RIPEMD160,
36 * MD5, SHA-1, SHA-224, SHA3-224). In general accepted hash algorithms
37 * are the ones belonging to ::mbedtls_x509_crt_profile_default.
38 * #MBEDTLS_PKCS7_ALLOW_WEAK_SIGNATURES can be enabled to accept all
39 * supported hash algorithms.
40 */
41
42#ifndef MBEDTLS_PKCS7_H
43#define MBEDTLS_PKCS7_H
44
45#include "mbedtls/private_access.h"
46
47#include "mbedtls/build_info.h"
48
49#include "mbedtls/asn1.h"
50#include "mbedtls/x509_crt.h"
51
52/**
53 * \name PKCS #7 Module Error codes
54 * \{
55 */
56#define MBEDTLS_ERR_PKCS7_INVALID_FORMAT -0x5300 /**< The format is invalid, e.g. different type expected. */
57#define MBEDTLS_ERR_PKCS7_FEATURE_UNAVAILABLE -0x5380 /**< Unavailable feature, e.g. anything other than signed data. */
58#define MBEDTLS_ERR_PKCS7_INVALID_VERSION -0x5400 /**< The PKCS #7 version element is invalid or cannot be parsed. */
59#define MBEDTLS_ERR_PKCS7_INVALID_CONTENT_INFO -0x5480 /**< The PKCS #7 content info is invalid or cannot be parsed. */
60#define MBEDTLS_ERR_PKCS7_INVALID_ALG -0x5500 /**< The algorithm tag or value is invalid or cannot be parsed. */
61#define MBEDTLS_ERR_PKCS7_INVALID_CERT -0x5580 /**< The certificate tag or value is invalid or cannot be parsed. */
62#define MBEDTLS_ERR_PKCS7_INVALID_SIGNATURE -0x5600 /**< Error parsing the signature */
63#define MBEDTLS_ERR_PKCS7_INVALID_SIGNER_INFO -0x5680 /**< Error parsing the signer's info */
64#define MBEDTLS_ERR_PKCS7_BAD_INPUT_DATA -0x5700 /**< Input invalid. */
65#define MBEDTLS_ERR_PKCS7_ALLOC_FAILED -0x5780 /**< Allocation of memory failed. */
66#define MBEDTLS_ERR_PKCS7_VERIFY_FAIL -0x5800 /**< Verification Failed */
67#define MBEDTLS_ERR_PKCS7_CERT_DATE_INVALID -0x5880 /**< The PKCS #7 date issued/expired dates are invalid */
68/* \} name */
69
70/**
71 * \name PKCS #7 Supported Version
72 * \{
73 */
74#define MBEDTLS_PKCS7_SUPPORTED_VERSION 0x01
75/* \} name */
76
77#ifdef __cplusplus
78extern "C" {
79#endif
80
81/**
82 * Type-length-value structure that allows for ASN.1 using DER.
83 */
84typedef mbedtls_asn1_buf mbedtls_pkcs7_buf;
85
86/**
87 * Container for ASN.1 named information objects.
88 * It allows for Relative Distinguished Names (e.g. cn=localhost,ou=code,etc.).
89 */
90typedef mbedtls_asn1_named_data mbedtls_pkcs7_name;
91
92/**
93 * Container for a sequence of ASN.1 items
94 */
95typedef mbedtls_asn1_sequence mbedtls_pkcs7_sequence;
96
97/**
98 * PKCS #7 types
99 */
100typedef enum {
101 MBEDTLS_PKCS7_NONE=0,
102 MBEDTLS_PKCS7_DATA,
103 MBEDTLS_PKCS7_SIGNED_DATA,
104 MBEDTLS_PKCS7_ENVELOPED_DATA,
105 MBEDTLS_PKCS7_SIGNED_AND_ENVELOPED_DATA,
106 MBEDTLS_PKCS7_DIGESTED_DATA,
107 MBEDTLS_PKCS7_ENCRYPTED_DATA,
108}
109mbedtls_pkcs7_type;
110
111/**
112 * Structure holding PKCS #7 signer info
113 */
114typedef struct mbedtls_pkcs7_signer_info {
115 int MBEDTLS_PRIVATE(version);
116 mbedtls_x509_buf MBEDTLS_PRIVATE(serial);
117 mbedtls_x509_name MBEDTLS_PRIVATE(issuer);
118 mbedtls_x509_buf MBEDTLS_PRIVATE(issuer_raw);
119 mbedtls_x509_buf MBEDTLS_PRIVATE(alg_identifier);
120 mbedtls_x509_buf MBEDTLS_PRIVATE(sig_alg_identifier);
121 mbedtls_x509_buf MBEDTLS_PRIVATE(sig);
122 struct mbedtls_pkcs7_signer_info *MBEDTLS_PRIVATE(next);
123}
124mbedtls_pkcs7_signer_info;
125
126/**
127 * Structure holding the signed data section
128 */
129typedef struct mbedtls_pkcs7_signed_data {
130 int MBEDTLS_PRIVATE(version);
131 mbedtls_pkcs7_buf MBEDTLS_PRIVATE(digest_alg_identifiers);
132 int MBEDTLS_PRIVATE(no_of_certs);
133 mbedtls_x509_crt MBEDTLS_PRIVATE(certs);
134 int MBEDTLS_PRIVATE(no_of_crls);
135 mbedtls_x509_crl MBEDTLS_PRIVATE(crl);
136 int MBEDTLS_PRIVATE(no_of_signers);
137 mbedtls_pkcs7_signer_info MBEDTLS_PRIVATE(signers);
138}
139mbedtls_pkcs7_signed_data;
140
141/**
142 * Structure holding PKCS #7 structure, only signed data for now
143 */
144typedef struct mbedtls_pkcs7 {
145 mbedtls_pkcs7_buf MBEDTLS_PRIVATE(raw);
146 mbedtls_pkcs7_signed_data MBEDTLS_PRIVATE(signed_data);
147}
148mbedtls_pkcs7;
149
150/**
151 * \brief Initialize mbedtls_pkcs7 structure.
152 *
153 * \param pkcs7 mbedtls_pkcs7 structure.
154 */
155void mbedtls_pkcs7_init(mbedtls_pkcs7 *pkcs7);
156
157/**
158 * \brief Parse a single DER formatted PKCS #7 detached signature.
159 *
160 * \param pkcs7 The mbedtls_pkcs7 structure to be filled by the parser.
161 * \param buf The buffer holding only the DER encoded PKCS #7 content.
162 * \param buflen The size in bytes of \p buf. The size must be exactly the
163 * length of the DER encoded PKCS #7 content.
164 *
165 * \note This function makes an internal copy of the PKCS #7 buffer
166 * \p buf. In particular, \p buf may be destroyed or reused
167 * after this call returns.
168 * \note Signatures with internal data are not supported.
169 *
170 * \return The \c mbedtls_pkcs7_type of \p buf, if successful.
171 * \return A negative error code on failure.
172 */
173int mbedtls_pkcs7_parse_der(mbedtls_pkcs7 *pkcs7, const unsigned char *buf,
174 const size_t buflen);
175
176/**
177 * \brief Verification of PKCS #7 signature against a caller-supplied
178 * certificate.
179 *
180 * For each signer in the PKCS structure, this function computes
181 * a signature over the supplied data, using the supplied
182 * certificate and the same digest algorithm as specified by the
183 * signer. It then compares this signature against the
184 * signer's signature; verification succeeds if any comparison
185 * matches.
186 *
187 * This function does not use the certificates held within the
188 * PKCS #7 structure itself, and does not check that the
189 * certificate is signed by a trusted certification authority.
190 *
191 * \param pkcs7 mbedtls_pkcs7 structure containing signature.
192 * \param cert Certificate containing key to verify signature.
193 * \param data Plain data on which signature has to be verified.
194 * \param datalen Length of the data.
195 *
196 * \note This function internally calculates the hash on the supplied
197 * plain data for signature verification.
198 *
199 * \note For limitation on the supported hash algorithms, please refer
200 * to the note at the top of this document.
201 *
202 * \return 0 if the signature verifies, or a negative error code on failure.
203 */
204int mbedtls_pkcs7_signed_data_verify(mbedtls_pkcs7 *pkcs7,
205 const mbedtls_x509_crt *cert,
206 const unsigned char *data,
207 size_t datalen);
208
209/**
210 * \brief Verification of PKCS #7 signature against a caller-supplied
211 * certificate.
212 *
213 * For each signer in the PKCS structure, this function
214 * validates a signature over the supplied hash, using the
215 * supplied certificate and the same digest algorithm as
216 * specified by the signer. Verification succeeds if any
217 * signature is good.
218 *
219 * This function does not use the certificates held within the
220 * PKCS #7 structure itself, and does not check that the
221 * certificate is signed by a trusted certification authority.
222 *
223 * \param pkcs7 PKCS #7 structure containing signature.
224 * \param cert Certificate containing key to verify signature.
225 * \param hash Hash of the plain data on which signature has to be verified.
226 * \param hashlen Length of the hash.
227 *
228 * \note This function is different from mbedtls_pkcs7_signed_data_verify()
229 * in that it is directly passed the hash of the data.
230 *
231 * \note For limitation on the supported hash algorithms, please refer
232 * to the note at the top of this document.
233 *
234 * \return 0 if the signature verifies, or a negative error code on failure.
235 */
236int mbedtls_pkcs7_signed_hash_verify(mbedtls_pkcs7 *pkcs7,
237 const mbedtls_x509_crt *cert,
238 const unsigned char *hash, size_t hashlen);
239
240/**
241 * \brief Unallocate all PKCS #7 data and zeroize the memory.
242 * It doesn't free \p pkcs7 itself. This should be done by the caller.
243 *
244 * \param pkcs7 mbedtls_pkcs7 structure to free.
245 */
246void mbedtls_pkcs7_free(mbedtls_pkcs7 *pkcs7);
247
248#ifdef __cplusplus
249}
250#endif
251
252#endif /* pkcs7.h */