| 1 |
// Copyright (c) 2014, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_PKCS7_H |
| 5 |
#define OPENSSL_HEADER_PKCS7_H |
| 6 |
|
| 7 |
#include <openssl/asn1.h> |
| 8 |
#include <openssl/base.h> |
| 9 |
|
| 10 |
#include <openssl/stack.h> |
| 11 |
|
| 12 |
#if defined(__cplusplus) |
| 13 |
extern "C" { |
| 14 |
#endif |
| 15 |
|
| 16 |
|
| 17 |
// PKCS#7. |
| 18 |
// |
| 19 |
// This library contains functions for extracting information from PKCS#7 |
| 20 |
// structures (RFC 2315). |
| 21 |
|
| 22 |
DECLARE_STACK_OF(CRYPTO_BUFFER) |
| 23 |
DECLARE_STACK_OF(X509) |
| 24 |
DECLARE_STACK_OF(X509_CRL) |
| 25 |
|
| 26 |
// PKCS7_get_raw_certificates parses a PKCS#7, SignedData structure from |cbs| |
| 27 |
// and appends the included certificates to |out_certs|. It returns one on |
| 28 |
// success and zero on error. |cbs| is advanced passed the structure. |
| 29 |
// |
| 30 |
// Note that a SignedData structure may contain no certificates, in which case |
| 31 |
// this function succeeds but does not append any certificates. Additionally, |
| 32 |
// certificates in SignedData structures are unordered. Callers should not |
| 33 |
// assume a particular order in |*out_certs| and may need to search for matches |
| 34 |
// or run path-building algorithms. |
| 35 |
OPENSSL_EXPORT int PKCS7_get_raw_certificates( |
| 36 |
STACK_OF(CRYPTO_BUFFER) *out_certs, CBS *cbs, CRYPTO_BUFFER_POOL *pool); |
| 37 |
|
| 38 |
// PKCS7_get_certificates behaves like |PKCS7_get_raw_certificates| but parses |
| 39 |
// them into |X509| objects. |
| 40 |
OPENSSL_EXPORT int PKCS7_get_certificates(STACK_OF(X509) *out_certs, CBS *cbs); |
| 41 |
|
| 42 |
// PKCS7_bundle_raw_certificates appends a PKCS#7, SignedData structure |
| 43 |
// containing |certs| to |out|. It returns one on success and zero on error. |
| 44 |
// Note that certificates in SignedData structures are unordered. The order in |
| 45 |
// |certs| will not be preserved. |
| 46 |
OPENSSL_EXPORT int PKCS7_bundle_raw_certificates( |
| 47 |
CBB *out, const STACK_OF(CRYPTO_BUFFER) *certs); |
| 48 |
|
| 49 |
// PKCS7_bundle_certificates behaves like |PKCS7_bundle_raw_certificates| but |
| 50 |
// takes |X509| objects as input. |
| 51 |
OPENSSL_EXPORT int PKCS7_bundle_certificates(CBB *out, |
| 52 |
const STACK_OF(X509) *certs); |
| 53 |
|
| 54 |
// PKCS7_get_CRLs parses a PKCS#7, SignedData structure from |cbs| and appends |
| 55 |
// the included CRLs to |out_crls|. It returns one on success and zero on error. |
| 56 |
// |cbs| is advanced passed the structure. |
| 57 |
// |
| 58 |
// Note that a SignedData structure may contain no CRLs, in which case this |
| 59 |
// function succeeds but does not append any CRLs. Additionally, CRLs in |
| 60 |
// SignedData structures are unordered. Callers should not assume an order in |
| 61 |
// |*out_crls| and may need to search for matches. |
| 62 |
OPENSSL_EXPORT int PKCS7_get_CRLs(STACK_OF(X509_CRL) *out_crls, CBS *cbs); |
| 63 |
|
| 64 |
// PKCS7_bundle_CRLs appends a PKCS#7, SignedData structure containing |
| 65 |
// |crls| to |out|. It returns one on success and zero on error. Note that CRLs |
| 66 |
// in SignedData structures are unordered. The order in |crls| will not be |
| 67 |
// preserved. |
| 68 |
OPENSSL_EXPORT int PKCS7_bundle_CRLs(CBB *out, const STACK_OF(X509_CRL) *crls); |
| 69 |
|
| 70 |
// PKCS7_get_PEM_certificates reads a PEM-encoded, PKCS#7, SignedData structure |
| 71 |
// from |pem_bio| and appends the included certificates to |out_certs|. It |
| 72 |
// returns one on success and zero on error. |
| 73 |
// |
| 74 |
// Note that a SignedData structure may contain no certificates, in which case |
| 75 |
// this function succeeds but does not append any certificates. Additionally, |
| 76 |
// certificates in SignedData structures are unordered. Callers should not |
| 77 |
// assume a particular order in |*out_certs| and may need to search for matches |
| 78 |
// or run path-building algorithms. |
| 79 |
OPENSSL_EXPORT int PKCS7_get_PEM_certificates(STACK_OF(X509) *out_certs, |
| 80 |
BIO *pem_bio); |
| 81 |
|
| 82 |
// PKCS7_get_PEM_CRLs reads a PEM-encoded, PKCS#7, SignedData structure from |
| 83 |
// |pem_bio| and appends the included CRLs to |out_crls|. It returns one on |
| 84 |
// success and zero on error. |
| 85 |
// |
| 86 |
// Note that a SignedData structure may contain no CRLs, in which case this |
| 87 |
// function succeeds but does not append any CRLs. Additionally, CRLs in |
| 88 |
// SignedData structures are unordered. Callers should not assume an order in |
| 89 |
// |*out_crls| and may need to search for matches. |
| 90 |
OPENSSL_EXPORT int PKCS7_get_PEM_CRLs(STACK_OF(X509_CRL) *out_crls, |
| 91 |
BIO *pem_bio); |
| 92 |
|
| 93 |
// d2i_PKCS7_bio behaves like |d2i_PKCS7| but reads the input from |bio|. If |
| 94 |
// the length of the object is indefinite the full contents of |bio| are read. |
| 95 |
// |
| 96 |
// If the function fails then some unknown amount of data may have been read |
| 97 |
// from |bio|. |
| 98 |
OPENSSL_EXPORT PKCS7 *d2i_PKCS7_bio(BIO *bio, PKCS7 **out); |
| 99 |
|
| 100 |
// i2d_PKCS7_bio writes |p7| to |bio|. It returns one on success and zero on |
| 101 |
// error. |
| 102 |
OPENSSL_EXPORT int i2d_PKCS7_bio(BIO *bio, const PKCS7 *p7); |
| 103 |
|
| 104 |
// PKCS7_type_is_data returns 1 if |p7| is of type data |
| 105 |
OPENSSL_EXPORT int PKCS7_type_is_data(const PKCS7 *p7); |
| 106 |
|
| 107 |
// PKCS7_type_is_digest returns 1 if |p7| is of type digest |
| 108 |
OPENSSL_EXPORT int PKCS7_type_is_digest(const PKCS7 *p7); |
| 109 |
|
| 110 |
// PKCS7_type_is_encrypted returns 1 if |p7| is of type encrypted |
| 111 |
OPENSSL_EXPORT int PKCS7_type_is_encrypted(const PKCS7 *p7); |
| 112 |
|
| 113 |
// PKCS7_type_is_enveloped returns 1 if |p7| is of type enveloped |
| 114 |
OPENSSL_EXPORT int PKCS7_type_is_enveloped(const PKCS7 *p7); |
| 115 |
|
| 116 |
// PKCS7_type_is_signed returns 1 if |p7| is of type signed |
| 117 |
OPENSSL_EXPORT int PKCS7_type_is_signed(const PKCS7 *p7); |
| 118 |
|
| 119 |
// PKCS7_type_is_signedAndEnveloped returns 1 if |p7| is of type |
| 120 |
// signedAndEnveloped |
| 121 |
OPENSSL_EXPORT int PKCS7_type_is_signedAndEnveloped(const PKCS7 *p7); |
| 122 |
|
| 123 |
|
| 124 |
// Deprecated functions. |
| 125 |
// |
| 126 |
// These functions are a compatibility layer over a subset of OpenSSL's PKCS#7 |
| 127 |
// API. It intentionally does not implement the whole thing, only the minimum |
| 128 |
// needed to build cryptography.io and CRuby. |
| 129 |
|
| 130 |
// ASN.1 defined here https://datatracker.ietf.org/doc/html/rfc2315#section-7 |
| 131 |
// |
| 132 |
// ContentInfo ::= SEQUENCE { |
| 133 |
// contentType ContentType, |
| 134 |
// content |
| 135 |
// [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL } |
| 136 |
// |
| 137 |
// ContentType ::= OBJECT IDENTIFIER |
| 138 |
struct pkcs7_st { |
| 139 |
// Unlike OpenSSL, the following fields are immutable. They filled in when the |
| 140 |
// object is parsed and ignored in serialization. |
| 141 |
ASN1_OBJECT *type; |
| 142 |
union { |
| 143 |
char *ptr; |
| 144 |
ASN1_OCTET_STRING *data; |
| 145 |
PKCS7_SIGNED *sign; |
| 146 |
PKCS7_ENVELOPE *enveloped; |
| 147 |
PKCS7_SIGN_ENVELOPE *signed_and_enveloped; |
| 148 |
PKCS7_DIGEST *digest; |
| 149 |
PKCS7_ENCRYPT *encrypted; |
| 150 |
// Other things provided by the user. Not specified in the RFC. |
| 151 |
ASN1_TYPE *other; |
| 152 |
} d; |
| 153 |
}; |
| 154 |
|
| 155 |
// ASN.1 defined here https://datatracker.ietf.org/doc/html/rfc2315#section-9.1 |
| 156 |
// |
| 157 |
// SignedData ::= SEQUENCE { |
| 158 |
// version Version, |
| 159 |
// digestAlgorithms DigestAlgorithmIdentifiers, |
| 160 |
// contentInfo ContentInfo, |
| 161 |
// certificates |
| 162 |
// [0] IMPLICIT ExtendedCertificatesAndCertificates |
| 163 |
// OPTIONAL, |
| 164 |
// crls |
| 165 |
// [1] IMPLICIT CertificateRevocationLists OPTIONAL, |
| 166 |
// signerInfos SignerInfos } |
| 167 |
// |
| 168 |
// DigestAlgorithmIdentifiers ::= |
| 169 |
// |
| 170 |
// SET OF DigestAlgorithmIdentifier |
| 171 |
// |
| 172 |
// SignerInfos ::= SET OF SignerInfo |
| 173 |
struct pkcs7_signed_st { |
| 174 |
ASN1_INTEGER *version; |
| 175 |
STACK_OF(X509_ALGOR) *md_algs; |
| 176 |
PKCS7 *contents; |
| 177 |
STACK_OF(X509) *cert; |
| 178 |
STACK_OF(X509_CRL) *crl; |
| 179 |
STACK_OF(PKCS7_SIGNER_INFO) *signer_info; |
| 180 |
}; |
| 181 |
|
| 182 |
// ASN.1 defined here https://datatracker.ietf.org/doc/html/rfc2315#section-9.2 |
| 183 |
// |
| 184 |
// SignerInfo ::= SEQUENCE { |
| 185 |
// version Version, |
| 186 |
// issuerAndSerialNumber IssuerAndSerialNumber, |
| 187 |
// digestAlgorithm DigestAlgorithmIdentifier, |
| 188 |
// authenticatedAttributes |
| 189 |
// [0] IMPLICIT Attributes OPTIONAL, |
| 190 |
// digestEncryptionAlgorithm |
| 191 |
// DigestEncryptionAlgorithmIdentifier, |
| 192 |
// encryptedDigest EncryptedDigest, |
| 193 |
// unauthenticatedAttributes |
| 194 |
// [1] IMPLICIT Attributes OPTIONAL } |
| 195 |
// |
| 196 |
// EncryptedDigest ::= OCTET STRING |
| 197 |
struct pkcs7_signer_info_st { |
| 198 |
ASN1_INTEGER *version; |
| 199 |
PKCS7_ISSUER_AND_SERIAL *issuer_and_serial; |
| 200 |
X509_ALGOR *digest_alg; |
| 201 |
STACK_OF(X509_ATTRIBUTE) *auth_attr; |
| 202 |
X509_ALGOR *digest_enc_alg; |
| 203 |
ASN1_OCTET_STRING *enc_digest; |
| 204 |
STACK_OF(X509_ATTRIBUTE) *unauth_attr; |
| 205 |
EVP_PKEY *pkey; // NOTE: |pkey| is not serialized. |
| 206 |
}; |
| 207 |
|
| 208 |
// ASN.1 defined here https://datatracker.ietf.org/doc/html/rfc2315#section-11.1 |
| 209 |
// |
| 210 |
// SignedAndEnvelopedData ::= SEQUENCE { |
| 211 |
// version Version, |
| 212 |
// recipientInfos RecipientInfos, |
| 213 |
// digestAlgorithms DigestAlgorithmIdentifiers, |
| 214 |
// encryptedContentInfo EncryptedContentInfo, |
| 215 |
// certificates |
| 216 |
// [0] IMPLICIT ExtendedCertificatesAndCertificates |
| 217 |
// OPTIONAL, |
| 218 |
// crls |
| 219 |
// [1] IMPLICIT CertificateRevocationLists OPTIONAL, |
| 220 |
// signerInfos SignerInfos } |
| 221 |
struct pkcs7_sign_envelope_st { |
| 222 |
ASN1_INTEGER *version; |
| 223 |
STACK_OF(PKCS7_RECIP_INFO) *recipientinfo; |
| 224 |
STACK_OF(X509_ALGOR) *md_algs; |
| 225 |
PKCS7_ENC_CONTENT *enc_data; |
| 226 |
STACK_OF(X509) *cert; |
| 227 |
STACK_OF(X509_CRL) *crl; |
| 228 |
STACK_OF(PKCS7_SIGNER_INFO) *signer_info; |
| 229 |
}; |
| 230 |
|
| 231 |
// ASN.1 defined here https://datatracker.ietf.org/doc/html/rfc2315#section-10.1 |
| 232 |
// |
| 233 |
// EnvelopedData ::= SEQUENCE { |
| 234 |
// version Version, |
| 235 |
// recipientInfos RecipientInfos, |
| 236 |
// encryptedContentInfo EncryptedContentInfo } |
| 237 |
// |
| 238 |
// RecipientInfos ::= SET OF RecipientInfo |
| 239 |
struct pkcs7_envelope_st { |
| 240 |
ASN1_INTEGER *version; |
| 241 |
PKCS7_ENC_CONTENT *enc_data; |
| 242 |
STACK_OF(PKCS7_RECIP_INFO) *recipientinfo; |
| 243 |
}; |
| 244 |
|
| 245 |
// ASN.1 defined here https://datatracker.ietf.org/doc/html/rfc2315#section-10.2 |
| 246 |
// |
| 247 |
// RecipientInfo ::= SEQUENCE { |
| 248 |
// version Version, |
| 249 |
// issuerAndSerialNumber IssuerAndSerialNumber, |
| 250 |
// keyEncryptionAlgorithm |
| 251 |
// |
| 252 |
// KeyEncryptionAlgorithmIdentifier, |
| 253 |
// encryptedKey EncryptedKey } |
| 254 |
// |
| 255 |
// EncryptedKey ::= OCTET STRING |
| 256 |
struct pkcs7_recip_info_st { |
| 257 |
ASN1_INTEGER *version; |
| 258 |
PKCS7_ISSUER_AND_SERIAL *issuer_and_serial; |
| 259 |
X509_ALGOR *key_enc_algor; |
| 260 |
ASN1_OCTET_STRING *enc_key; |
| 261 |
X509 *cert; // NOTE: |cert| is not serialized |
| 262 |
}; |
| 263 |
|
| 264 |
// ASN.1 defined here https://datatracker.ietf.org/doc/html/rfc2315#section-6.7 |
| 265 |
// |
| 266 |
// IssuerAndSerialNumber ::= SEQUENCE { |
| 267 |
// issuer Name, |
| 268 |
// serialNumber CertificateSerialNumber } |
| 269 |
struct pkcs7_issuer_and_serial_st { |
| 270 |
X509_NAME *issuer; |
| 271 |
ASN1_INTEGER *serial; |
| 272 |
}; |
| 273 |
|
| 274 |
// Only declare ASN1 functions or define stacks publibly if needed by supported |
| 275 |
// projects that depend on them. |
| 276 |
DECLARE_ASN1_FUNCTIONS(PKCS7) |
| 277 |
DECLARE_ASN1_FUNCTIONS(PKCS7_RECIP_INFO) |
| 278 |
DECLARE_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO) |
| 279 |
|
| 280 |
DEFINE_STACK_OF(PKCS7_RECIP_INFO) |
| 281 |
DEFINE_STACK_OF(PKCS7_SIGNER_INFO) |
| 282 |
|
| 283 |
// PKCS7_dup returns a newly allocated copy of |p7| without deep-copying |
| 284 |
// internal references. |
| 285 |
OPENSSL_EXPORT OPENSSL_DEPRECATED PKCS7 *PKCS7_dup(PKCS7 *p7); |
| 286 |
|
| 287 |
// PKCS7_get_signed_attribute returns a pointer to the first signed attribute |
| 288 |
// from |si| with NID |nid| if one is present, else NULL. |
| 289 |
OPENSSL_EXPORT OPENSSL_DEPRECATED ASN1_TYPE *PKCS7_get_signed_attribute( |
| 290 |
const PKCS7_SIGNER_INFO *si, int nid); |
| 291 |
|
| 292 |
// PKCS7_get_signer_info returns |p7|'s attached PKCS7_SIGNER_INFO if present |
| 293 |
// and |p7| is of a relevant type, else NULL. This function only pertains to |
| 294 |
// signedData and signedAndEnvelopedData. |
| 295 |
OPENSSL_EXPORT OPENSSL_DEPRECATED STACK_OF(PKCS7_SIGNER_INFO) * |
| 296 |
PKCS7_get_signer_info(PKCS7 *p7); |
| 297 |
|
| 298 |
// PKCS7_RECIP_INFO_set attaches |x509| to |p7i| and increments |x509|'s |
| 299 |
// reference count. It returns 1 on success and 0 on failure or if |x509|'s |
| 300 |
// public key not usable for encryption. |
| 301 |
OPENSSL_EXPORT OPENSSL_DEPRECATED int PKCS7_RECIP_INFO_set( |
| 302 |
PKCS7_RECIP_INFO *p7i, X509 *x509); |
| 303 |
|
| 304 |
// PKCS7_SIGNER_INFO_set attaches the other parameters to |p7i|, returning 1 on |
| 305 |
// success and 0 on error or if specified parameters are inapplicable to |
| 306 |
// signing. Only EC, DH, and RSA |pkey|s are supported. |pkey|'s reference |
| 307 |
// count is incremented, but neither |x509|'s nor |dgst|'s is. |
| 308 |
OPENSSL_EXPORT OPENSSL_DEPRECATED int PKCS7_SIGNER_INFO_set( |
| 309 |
PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey, const EVP_MD *dgst); |
| 310 |
|
| 311 |
// PKCS7_add_certificate adds |x509| to |p7|'s certificate stack, incrementing |
| 312 |
// |x509|'s reference count. It returns 1 on success and 0 on failure or if |
| 313 |
// |p7| isn't of an applicable type: signedData and signedAndEnvelopedData. |
| 314 |
OPENSSL_EXPORT OPENSSL_DEPRECATED int PKCS7_add_certificate(PKCS7 *p7, |
| 315 |
X509 *x509); |
| 316 |
|
| 317 |
// PKCS7_add_crl adds |x509| to |p7|'s CRL stack, incrementing |x509|'s |
| 318 |
// reference count. It returns 1 on success and 0 on failure or if |p7| isn't |
| 319 |
// of an applicable type: signedData and signedAndEnvelopedData. |
| 320 |
OPENSSL_EXPORT OPENSSL_DEPRECATED int PKCS7_add_crl(PKCS7 *p7, X509_CRL *x509); |
| 321 |
|
| 322 |
// PKCS7_add_recipient_info adds |ri| to |p7|, returning 1 on succes or 0 if |
| 323 |
// |p7| is of an inapplicable type: envelopedData and signedAndEnvelopedData. |
| 324 |
OPENSSL_EXPORT OPENSSL_DEPRECATED int PKCS7_add_recipient_info( |
| 325 |
PKCS7 *p7, PKCS7_RECIP_INFO *ri); |
| 326 |
|
| 327 |
// PKCS7_add_signer adds |p7i| to |p7|, returning 1 on succes or 0 if |
| 328 |
// |p7| is of an inapplicable type: signedData and signedAndEnvelopedData. |
| 329 |
OPENSSL_EXPORT OPENSSL_DEPRECATED int PKCS7_add_signer(PKCS7 *p7, |
| 330 |
PKCS7_SIGNER_INFO *p7i); |
| 331 |
|
| 332 |
// PKCS7_content_new allocates a new PKCS7 and adds it to |p7| as content. It |
| 333 |
// returns 1 on success and 0 on failure. |
| 334 |
OPENSSL_EXPORT OPENSSL_DEPRECATED int PKCS7_content_new(PKCS7 *p7, int nid); |
| 335 |
|
| 336 |
// PKCS7_set_content sets |p7_data| as content on |p7| for applicable types of |
| 337 |
// |p7|. It frees any existing content on |p7|, returning 1 on success and 0 on |
| 338 |
// failure. |
| 339 |
OPENSSL_EXPORT OPENSSL_DEPRECATED int PKCS7_set_content(PKCS7 *p7, |
| 340 |
PKCS7 *p7_data); |
| 341 |
|
| 342 |
// PKCS7_set_content sets |p7_data| as content on |p7| for applicable types of |
| 343 |
// |p7|: signedData and digestData. |p7_data| may be NULL. It frees any |
| 344 |
// existing content on |p7|, returning 1 on success and 0 on failure. |
| 345 |
OPENSSL_EXPORT OPENSSL_DEPRECATED int PKCS7_set_cipher( |
| 346 |
PKCS7 *p7, const EVP_CIPHER *cipher); |
| 347 |
|
| 348 |
// PKCS7_set_type instantiates |p7| as type |type|. It returns 1 on success and |
| 349 |
// 0 on failure or if |type| is not a valid PKCS7 content type. |
| 350 |
OPENSSL_EXPORT OPENSSL_DEPRECATED int PKCS7_set_type(PKCS7 *p7, int type); |
| 351 |
|
| 352 |
// PKCS7_RECIP_INFO_get0_alg sets |*penc| to |ri|'s key encryption algorithm, |
| 353 |
// if present. Ownership of |*penc| is retained by |ri|. |
| 354 |
OPENSSL_EXPORT OPENSSL_DEPRECATED void PKCS7_RECIP_INFO_get0_alg( |
| 355 |
PKCS7_RECIP_INFO *ri, X509_ALGOR **penc); |
| 356 |
|
| 357 |
// PKCS7_SIGNER_INFO_get0_algs sets all of, if present: |*pk| to |si|'s key, |
| 358 |
// |*pdig| to |si|'s digest angorithm, and |*psig| to |si|'s signature |
| 359 |
// algorithm. Ownership of |*pk|, |*pdig|, and |*psig) is retained by |si|. |
| 360 |
OPENSSL_EXPORT OPENSSL_DEPRECATED void PKCS7_SIGNER_INFO_get0_algs( |
| 361 |
PKCS7_SIGNER_INFO *si, EVP_PKEY **pk, X509_ALGOR **pdig, X509_ALGOR **psig); |
| 362 |
|
| 363 |
|
| 364 |
// Deprecated flags |
| 365 |
// |
| 366 |
// Not all defined flags are acted upon, and the behavior associated with some |
| 367 |
// flags is performed unconditionally. See each |PKCS7_*| for details. |
| 368 |
|
| 369 |
// PKCS7_DETACHED indicates that the PKCS#7 file specifies its data externally. |
| 370 |
#define PKCS7_DETACHED 0x40 |
| 371 |
|
| 372 |
// PKCS7_BINARY disables the default translation to MIME canonical format (as |
| 373 |
// required by the S/MIME specifications). It is assumed in |PKCS7_sign| unless |
| 374 |
// the caller is just bundling certs. |
| 375 |
#define PKCS7_BINARY 0x80 |
| 376 |
|
| 377 |
// PKCS7_NOINTERN disables verification against certificate public keys included |
| 378 |
// in a PKCS7 ContentInfo. If this flag is specified, the caller must supply a |
| 379 |
// stack of certificates to verify against. |
| 380 |
#define PKCS7_NOINTERN 0x10 |
| 381 |
|
| 382 |
// PKCS7_NOATTR disables usage of authenticatedAttributes. It is assumed in |
| 383 |
// |PKCS7_sign| unless the caller is just bundling certs. |
| 384 |
#define PKCS7_NOATTR 0x100 |
| 385 |
|
| 386 |
// PKCS7_NOCERTS excludes the signer's certificate and the extra certs defined |
| 387 |
// from the PKCS7 structure. Using this will fail |PKCS7_sign| unless used as |
| 388 |
// described in |PKCS7_sign|'s documentation. |
| 389 |
#define PKCS7_NOCERTS 0x2 |
| 390 |
|
| 391 |
// PKCS7_NOVERIFY will skip trust chain verification against the trust store. |
| 392 |
// It will still verify signatures against signer infos included in the PKCS7. |
| 393 |
#define PKCS7_NOVERIFY 0x20 |
| 394 |
|
| 395 |
// The following flags are used in OpenSSL, but are ignored by AWS-LC. They are |
| 396 |
// defined here solely for build compatibility. |
| 397 |
#define PKCS7_TEXT 0x1 |
| 398 |
#define PKCS7_NOSIGS 0x4 |
| 399 |
#define PKCS7_NOCHAIN 0x8 |
| 400 |
#define PKCS7_NOSMIMECAP 0x200 |
| 401 |
#define PKCS7_STREAM 0x1000 |
| 402 |
#define PKCS7_PARTIAL 0x4000 |
| 403 |
|
| 404 |
// PKCS7_sign can operate in three modes to provide some backwards |
| 405 |
// compatibility: |
| 406 |
// |
| 407 |
// The first mode assembles |certs| into a PKCS#7 signed data ContentInfo with |
| 408 |
// external data and no signatures. It returns a newly-allocated |PKCS7| on |
| 409 |
// success or NULL on error. |sign_cert| and |pkey| must be NULL. |data| is |
| 410 |
// ignored. |flags| must be equal to |PKCS7_DETACHED|. Additionally, |
| 411 |
// certificates in SignedData structures are unordered. The order of |certs| |
| 412 |
// will not be preserved. |
| 413 |
// |
| 414 |
// The second mode generates a detached RSA SHA-256 signature of |data| using |
| 415 |
// |pkey| and produces a PKCS#7 SignedData structure containing it. |certs| |
| 416 |
// must be NULL and |flags| must be exactly |PKCS7_NOATTR | PKCS7_BINARY | |
| 417 |
// PKCS7_NOCERTS | PKCS7_DETACHED|. |
| 418 |
// |
| 419 |
// The third mode is used for more general signing and does not require the |
| 420 |
// specification of any flags, but does require |sign_cert|, |pkey|, and |data| |
| 421 |
// to be populated. This mode always behaves as if |PKCS7_NOATTR| and |
| 422 |
// |PKCS7_BINARY| are set. It honors the specification (or elision) of |
| 423 |
// |PKCS7_DETACHED|. It does not allow |PKCS7_NOCERTS|. |
| 424 |
// |
| 425 |
// Note this function only implements a subset of the corresponding OpenSSL |
| 426 |
// function. It is provided for backwards compatibility only. |
| 427 |
OPENSSL_EXPORT OPENSSL_DEPRECATED PKCS7 *PKCS7_sign(X509 *sign_cert, |
| 428 |
EVP_PKEY *pkey, |
| 429 |
STACK_OF(X509) *certs, |
| 430 |
BIO *data, int flags); |
| 431 |
|
| 432 |
// PKCS7_verify takes in a |p7| with signed ContentInfo and verifies its |
| 433 |
// signature against |certs| or |store|. If |certs| is specified, this function |
| 434 |
// will attempt to verify |p7|'s signature against those certificates' public |
| 435 |
// keys. If |store| is specified, its contents will be treated as certificate |
| 436 |
// authorities (CAs) for establishing trust of any certificates bundled in |p7|. |
| 437 |
// |
| 438 |
// If |p7| is detached, |indata| must contain the data over which |p7|'s |
| 439 |
// signature was computed. If verification succeeds, the verified content is |
| 440 |
// written to |out| and 1 is returned. On error or verification failure, 0 is |
| 441 |
// returned. |
| 442 |
// |
| 443 |
// Flags: If |PKCS7_NOVERIFY| is specified, trust chain validation is skipped. |
| 444 |
// This function also enforces the behavior of OpenSSL's |PKCS7_NO_DUAL_CONTENT| |
| 445 |
// meaning that |indata| may not be specified if |p7|'s signed data is attached. |
| 446 |
// If |PKCS7_NOINTERN| is set, this function will not verify against certificate |
| 447 |
// public keys included |p7|, instead relying solely on |certs|, which must be |
| 448 |
// specified. |
| 449 |
OPENSSL_EXPORT OPENSSL_DEPRECATED int PKCS7_verify(PKCS7 *p7, |
| 450 |
STACK_OF(X509) *certs, |
| 451 |
X509_STORE *store, |
| 452 |
BIO *indata, BIO *outdata, |
| 453 |
int flags); |
| 454 |
|
| 455 |
// PKCS7_is_detached returns 0 if |p7| has attached content and 1 otherwise. |
| 456 |
OPENSSL_EXPORT OPENSSL_DEPRECATED int PKCS7_is_detached(PKCS7 *p7); |
| 457 |
|
| 458 |
// PKCS7_set_detached frees the attached content of |p7| if |detach| is set to |
| 459 |
// 1. It returns 0 if otherwise or if |p7| is not of type signed. |
| 460 |
// |
| 461 |
// Note: |detach| is intended to be a boolean and MUST be set with either 1 or |
| 462 |
// 0. |
| 463 |
OPENSSL_EXPORT OPENSSL_DEPRECATED int PKCS7_set_detached(PKCS7 *p7, int detach); |
| 464 |
|
| 465 |
// PKCS7_get_detached returns 0 if |p7| has attached content and 1 otherwise. |
| 466 |
OPENSSL_EXPORT OPENSSL_DEPRECATED int PKCS7_get_detached(PKCS7 *p7); |
| 467 |
|
| 468 |
// PKCS7_dataInit creates or initializes a BIO chain for reading data from or |
| 469 |
// writing data to |p7|. If |bio| is non-null, it is added to the chain. |
| 470 |
// Otherwise, a new BIO is allocated and returned to anchor the chain. |
| 471 |
OPENSSL_EXPORT OPENSSL_DEPRECATED BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio); |
| 472 |
|
| 473 |
// PKCS7_dataFinal serializes data written to |bio|'s chain into |p7|. It should |
| 474 |
// only be called on BIO chains created by |PKCS7_dataInit|. |
| 475 |
OPENSSL_EXPORT OPENSSL_DEPRECATED int PKCS7_dataFinal(PKCS7 *p7, BIO *bio); |
| 476 |
|
| 477 |
// PKCS7_set_digest sets |p7|'s digest to |md|. It returns 1 on success and 0 if |
| 478 |
// |p7| is of the wrong content type. |
| 479 |
OPENSSL_EXPORT OPENSSL_DEPRECATED int PKCS7_set_digest(PKCS7 *p7, |
| 480 |
const EVP_MD *md); |
| 481 |
|
| 482 |
// PKCS7_get_recipient_info returns a pointer to a stack containing |p7|'s |
| 483 |
// |PKCS7_RECIP_INFO| or NULL if none are present. |
| 484 |
OPENSSL_EXPORT OPENSSL_DEPRECATED STACK_OF(PKCS7_RECIP_INFO) * |
| 485 |
PKCS7_get_recipient_info(PKCS7 *p7); |
| 486 |
|
| 487 |
// PKCS7_add_recipient allocates a new |PCKS7_RECEPIENT_INFO|, adds |x509| to it |
| 488 |
// and returns that |PCKS7_RECEPIENT_INFO|. |
| 489 |
OPENSSL_EXPORT OPENSSL_DEPRECATED PKCS7_RECIP_INFO *PKCS7_add_recipient( |
| 490 |
PKCS7 *p7, X509 *x509); |
| 491 |
|
| 492 |
// PKCS7_get0_signers retrieves the signer's certificates from p7. It does not |
| 493 |
// check their validity or whether any signatures are valid. The caller owns the |
| 494 |
// returned X509 stack and is responsible for freeing it. |
| 495 |
OPENSSL_EXPORT OPENSSL_DEPRECATED STACK_OF(X509) *PKCS7_get0_signers( |
| 496 |
PKCS7 *p7, STACK_OF(X509) *certs, int flags); |
| 497 |
|
| 498 |
// PKCS7_encrypt encrypts the contents of |in| with |cipher| and adds |certs| as |
| 499 |
// recipient infos and returns an encrypted |PKCS7| or NULL on failed |
| 500 |
// encryption. |flags| is ignored. We only perform key encryption using RSA, so |
| 501 |
// |certs| must use RSA public keys. |
| 502 |
OPENSSL_EXPORT OPENSSL_DEPRECATED PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, |
| 503 |
BIO *in, |
| 504 |
const EVP_CIPHER *cipher, |
| 505 |
int flags); |
| 506 |
|
| 507 |
// PKCS7_decrypt decrypts |p7| with |pkey| and writes the plaintext to |data|. |
| 508 |
// If |cert| is present, it's public key is checked against |pkey| and |p7|'s |
| 509 |
// recipient infos. 1 is returned on success and 0 on failure. |flags| is |
| 510 |
// ignored. |pkey| must be an |EVP_PKEY_RSA|. |
| 511 |
// |
| 512 |
// NOTE: If |p7| was encrypted with a stream cipher, this operation may return 1 |
| 513 |
// even on decryption failure. The reason for this is detailed in RFC 3218 and |
| 514 |
// comments in the |PKCS7_decrypt| source. |
| 515 |
OPENSSL_EXPORT OPENSSL_DEPRECATED int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, |
| 516 |
X509 *cert, BIO *data, |
| 517 |
int flags); |
| 518 |
|
| 519 |
// No-ops |
| 520 |
// |
| 521 |
// These functions do nothing. They're provided solely for build compatibility |
| 522 |
|
| 523 |
// SMIME_read_PKCS7 is a no-op and returns NULL |
| 524 |
OPENSSL_EXPORT OPENSSL_DEPRECATED PKCS7 *SMIME_read_PKCS7(BIO *in, BIO **bcont); |
| 525 |
|
| 526 |
// SMIME_write_PKCS7 is a no-op and returns 0 |
| 527 |
OPENSSL_EXPORT OPENSSL_DEPRECATED int SMIME_write_PKCS7(BIO *out, PKCS7 *p7, |
| 528 |
BIO *data, int flags); |
| 529 |
|
| 530 |
// PKCS7_print_ctx prints "PKCS7 printing is not supported" and returns 1. |
| 531 |
OPENSSL_EXPORT OPENSSL_DEPRECATED int PKCS7_print_ctx(BIO *bio, PKCS7 *pkcs7, |
| 532 |
int indent, |
| 533 |
const ASN1_PCTX *pctx); |
| 534 |
|
| 535 |
#if defined(__cplusplus) |
| 536 |
} // extern C |
| 537 |
|
| 538 |
extern "C++" { |
| 539 |
BSSL_NAMESPACE_BEGIN |
| 540 |
|
| 541 |
BORINGSSL_MAKE_DELETER(PKCS7, PKCS7_free) |
| 542 |
BORINGSSL_MAKE_DELETER(PKCS7_SIGNER_INFO, PKCS7_SIGNER_INFO_free) |
| 543 |
|
| 544 |
BSSL_NAMESPACE_END |
| 545 |
} // extern C++ |
| 546 |
#endif |
| 547 |
|
| 548 |
#define PKCS7_R_BAD_PKCS7_VERSION 100 |
| 549 |
#define PKCS7_R_NOT_PKCS7_SIGNED_DATA 101 |
| 550 |
#define PKCS7_R_NO_CERTIFICATES_INCLUDED 102 |
| 551 |
#define PKCS7_R_NO_CRLS_INCLUDED 103 |
| 552 |
#define PKCS7_R_INVALID_NULL_POINTER 104 |
| 553 |
#define PKCS7_R_NO_CONTENT 105 |
| 554 |
#define PKCS7_R_CIPHER_NOT_INITIALIZED 106 |
| 555 |
#define PKCS7_R_UNSUPPORTED_CONTENT_TYPE 107 |
| 556 |
#define PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST 108 |
| 557 |
#define PKCS7_R_UNABLE_TO_FIND_MEM_BIO 109 |
| 558 |
#define PKCS7_R_WRONG_CONTENT_TYPE 110 |
| 559 |
#define PKCS7_R_CONTENT_AND_DATA_PRESENT 111 |
| 560 |
#define PKCS7_R_NO_SIGNATURES_ON_DATA 112 |
| 561 |
#define PKCS7_R_CERTIFICATE_VERIFY_ERROR 113 |
| 562 |
#define PKCS7_R_SMIME_TEXT_ERROR 114 |
| 563 |
#define PKCS7_R_SIGNATURE_FAILURE 115 |
| 564 |
#define PKCS7_R_NO_SIGNERS 116 |
| 565 |
#define PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND 117 |
| 566 |
#define PKCS7_R_ERROR_SETTING_CIPHER 118 |
| 567 |
#define PKCS7_R_ERROR_ADDING_RECIPIENT 119 |
| 568 |
#define PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE 120 |
| 569 |
#define PKCS7_R_DECRYPT_ERROR 121 |
| 570 |
#define PKCS7_R_PKCS7_DATASIGN 122 |
| 571 |
#define PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER 123 |
| 572 |
#define PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE 124 |
| 573 |
#define PKCS7_R_UNKNOWN_DIGEST_TYPE 125 |
| 574 |
#define PKCS7_R_INVALID_SIGNED_DATA_TYPE 126 |
| 575 |
#define PKCS7_R_UNSUPPORTED_CIPHER_TYPE 127 |
| 576 |
#define PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE 128 |
| 577 |
#define PKCS7_R_DIGEST_FAILURE 129 |
| 578 |
#define PKCS7_R_WRONG_PKCS7_TYPE 130 |
| 579 |
#define PKCS7_R_PKCS7_ADD_SIGNER_ERROR 131 |
| 580 |
#define PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR 132 |
| 581 |
#define PKCS7_R_NO_DEFAULT_DIGEST 133 |
| 582 |
#define PKCS7_R_CERT_MUST_BE_RSA 134 |
| 583 |
#define PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE 135 |
| 584 |
|
| 585 |
#endif // OPENSSL_HEADER_PKCS7_H |