pkcs7.h
Go to the documentation of this file.
1 /* Copyright (c) 2014, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #ifndef OPENSSL_HEADER_PKCS7_H
16 #define OPENSSL_HEADER_PKCS7_H
17 
18 #include <openssl/base.h>
19 
20 #include <openssl/stack.h>
21 
22 #if defined(__cplusplus)
23 extern "C" {
24 #endif
25 
26 
27 // PKCS#7.
28 //
29 // This library contains functions for extracting information from PKCS#7
30 // structures (RFC 2315).
31 
35 
36 // PKCS7_get_raw_certificates parses a PKCS#7, SignedData structure from |cbs|
37 // and appends the included certificates to |out_certs|. It returns one on
38 // success and zero on error. |cbs| is advanced passed the structure.
39 //
40 // Note that a SignedData structure may contain no certificates, in which case
41 // this function succeeds but does not append any certificates. Additionally,
42 // certificates in SignedData structures are unordered. Callers should not
43 // assume a particular order in |*out_certs| and may need to search for matches
44 // or run path-building algorithms.
47 
48 // PKCS7_get_certificates behaves like |PKCS7_get_raw_certificates| but parses
49 // them into |X509| objects.
51 
52 // PKCS7_bundle_raw_certificates appends a PKCS#7, SignedData structure
53 // containing |certs| to |out|. It returns one on success and zero on error.
54 // Note that certificates in SignedData structures are unordered. The order in
55 // |certs| will not be preserved.
57  CBB *out, const STACK_OF(CRYPTO_BUFFER) *certs);
58 
59 // PKCS7_bundle_certificates behaves like |PKCS7_bundle_raw_certificates| but
60 // takes |X509| objects as input.
62  CBB *out, const STACK_OF(X509) *certs);
63 
64 // PKCS7_get_CRLs parses a PKCS#7, SignedData structure from |cbs| and appends
65 // the included CRLs to |out_crls|. It returns one on success and zero on error.
66 // |cbs| is advanced passed the structure.
67 //
68 // Note that a SignedData structure may contain no CRLs, in which case this
69 // function succeeds but does not append any CRLs. Additionally, CRLs in
70 // SignedData structures are unordered. Callers should not assume an order in
71 // |*out_crls| and may need to search for matches.
73 
74 // PKCS7_bundle_CRLs appends a PKCS#7, SignedData structure containing
75 // |crls| to |out|. It returns one on success and zero on error. Note that CRLs
76 // in SignedData structures are unordered. The order in |crls| will not be
77 // preserved.
79 
80 // PKCS7_get_PEM_certificates reads a PEM-encoded, PKCS#7, SignedData structure
81 // from |pem_bio| and appends the included certificates to |out_certs|. It
82 // returns one on success and zero on error.
83 //
84 // Note that a SignedData structure may contain no certificates, in which case
85 // this function succeeds but does not append any certificates. Additionally,
86 // certificates in SignedData structures are unordered. Callers should not
87 // assume a particular order in |*out_certs| and may need to search for matches
88 // or run path-building algorithms.
90  BIO *pem_bio);
91 
92 // PKCS7_get_PEM_CRLs reads a PEM-encoded, PKCS#7, SignedData structure from
93 // |pem_bio| and appends the included CRLs to |out_crls|. It returns one on
94 // success and zero on error.
95 //
96 // Note that a SignedData structure may contain no CRLs, in which case this
97 // function succeeds but does not append any CRLs. Additionally, CRLs in
98 // SignedData structures are unordered. Callers should not assume an order in
99 // |*out_crls| and may need to search for matches.
101  BIO *pem_bio);
102 
103 
104 // Deprecated functions.
105 //
106 // These functions are a compatibility layer over a subset of OpenSSL's PKCS#7
107 // API. It intentionally does not implement the whole thing, only the minimum
108 // needed to build cryptography.io.
109 
110 typedef struct {
111  STACK_OF(X509) *cert;
112  STACK_OF(X509_CRL) *crl;
113 } PKCS7_SIGNED;
114 
115 typedef struct {
116  STACK_OF(X509) *cert;
117  STACK_OF(X509_CRL) *crl;
119 
120 typedef void PKCS7_ENVELOPE;
121 typedef void PKCS7_DIGEST;
122 typedef void PKCS7_ENCRYPT;
123 typedef void PKCS7_SIGNER_INFO;
124 
125 typedef struct {
127  size_t ber_len;
128 
129  // Unlike OpenSSL, the following fields are immutable. They filled in when the
130  // object is parsed and ignored in serialization.
132  union {
133  char *ptr;
141  } d;
142 } PKCS7;
143 
144 // d2i_PKCS7 parses a BER-encoded, PKCS#7 signed data ContentInfo structure from
145 // |len| bytes at |*inp|, as described in |d2i_SAMPLE|.
147  size_t len);
148 
149 // d2i_PKCS7_bio behaves like |d2i_PKCS7| but reads the input from |bio|. If
150 // the length of the object is indefinite the full contents of |bio| are read.
151 //
152 // If the function fails then some unknown amount of data may have been read
153 // from |bio|.
155 
156 // i2d_PKCS7 marshals |p7| as a DER-encoded PKCS#7 ContentInfo structure, as
157 // described in |i2d_SAMPLE|.
158 OPENSSL_EXPORT int i2d_PKCS7(const PKCS7 *p7, uint8_t **out);
159 
160 // i2d_PKCS7_bio writes |p7| to |bio|. It returns one on success and zero on
161 // error.
162 OPENSSL_EXPORT int i2d_PKCS7_bio(BIO *bio, const PKCS7 *p7);
163 
164 // PKCS7_free releases memory associated with |p7|.
166 
167 // PKCS7_type_is_data returns zero.
169 
170 // PKCS7_type_is_digest returns zero.
172 
173 // PKCS7_type_is_encrypted returns zero.
175 
176 // PKCS7_type_is_enveloped returns zero.
178 
179 // PKCS7_type_is_signed returns one. (We only supporte signed data
180 // ContentInfos.)
182 
183 // PKCS7_type_is_signedAndEnveloped returns zero.
185 
186 // PKCS7_DETACHED indicates that the PKCS#7 file specifies its data externally.
187 #define PKCS7_DETACHED 0x40
188 
189 // The following flags cause |PKCS7_sign| to fail.
190 #define PKCS7_TEXT 0x1
191 #define PKCS7_NOCERTS 0x2
192 #define PKCS7_NOSIGS 0x4
193 #define PKCS7_NOCHAIN 0x8
194 #define PKCS7_NOINTERN 0x10
195 #define PKCS7_NOVERIFY 0x20
196 #define PKCS7_BINARY 0x80
197 #define PKCS7_NOATTR 0x100
198 #define PKCS7_NOSMIMECAP 0x200
199 #define PKCS7_STREAM 0x1000
200 #define PKCS7_PARTIAL 0x4000
201 
202 // PKCS7_sign can operate in two modes to provide some backwards compatibility:
203 //
204 // The first mode assembles |certs| into a PKCS#7 signed data ContentInfo with
205 // external data and no signatures. It returns a newly-allocated |PKCS7| on
206 // success or NULL on error. |sign_cert| and |pkey| must be NULL. |data| is
207 // ignored. |flags| must be equal to |PKCS7_DETACHED|. Additionally,
208 // certificates in SignedData structures are unordered. The order of |certs|
209 // will not be preserved.
210 //
211 // The second mode generates a detached RSA SHA-256 signature of |data| using
212 // |pkey| and produces a PKCS#7 SignedData structure containing it. |certs|
213 // must be NULL and |flags| must be exactly |PKCS7_NOATTR | PKCS7_BINARY |
214 // PKCS7_NOCERTS | PKCS7_DETACHED|.
215 //
216 // Note this function only implements a subset of the corresponding OpenSSL
217 // function. It is provided for backwards compatibility only.
218 OPENSSL_EXPORT PKCS7 *PKCS7_sign(X509 *sign_cert, EVP_PKEY *pkey,
219  STACK_OF(X509) *certs, BIO *data, int flags);
220 
221 
222 #if defined(__cplusplus)
223 } // extern C
224 
225 extern "C++" {
227 
229 
231 } // extern C++
232 #endif
233 
234 #define PKCS7_R_BAD_PKCS7_VERSION 100
235 #define PKCS7_R_NOT_PKCS7_SIGNED_DATA 101
236 #define PKCS7_R_NO_CERTIFICATES_INCLUDED 102
237 #define PKCS7_R_NO_CRLS_INCLUDED 103
238 
239 #endif // OPENSSL_HEADER_PKCS7_H
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
PKCS7::digest
PKCS7_DIGEST * digest
Definition: pkcs7.h:138
PKCS7_ENCRYPT
void PKCS7_ENCRYPT
Definition: pkcs7.h:122
cbs_st
Definition: bytestring.h:39
PKCS7_DIGEST
void PKCS7_DIGEST
Definition: pkcs7.h:121
bio_st
Definition: bio.h:822
PKCS7_free
OPENSSL_EXPORT void PKCS7_free(PKCS7 *p7)
Definition: pkcs7_x509.c:346
PKCS7_type_is_signedAndEnveloped
OPENSSL_EXPORT int PKCS7_type_is_signedAndEnveloped(const PKCS7 *p7)
Definition: pkcs7_x509.c:368
PKCS7_type_is_enveloped
OPENSSL_EXPORT int PKCS7_type_is_enveloped(const PKCS7 *p7)
Definition: pkcs7_x509.c:366
regen-readme.inp
inp
Definition: regen-readme.py:11
PKCS7_bundle_raw_certificates
OPENSSL_EXPORT int PKCS7_bundle_raw_certificates(CBB *out, const STACK_OF(CRYPTO_BUFFER) *certs)
Definition: pkcs7.c:156
PKCS7::ptr
char * ptr
Definition: pkcs7.h:133
i2d_PKCS7
OPENSSL_EXPORT int i2d_PKCS7(const PKCS7 *p7, uint8_t **out)
Definition: pkcs7_x509.c:318
PKCS7::other
ASN1_TYPE * other
Definition: pkcs7.h:140
PKCS7_SIGNER_INFO
void PKCS7_SIGNER_INFO
Definition: pkcs7.h:123
crypto_buffer_st
Definition: third_party/boringssl-with-bazel/src/crypto/pool/internal.h:31
PKCS7_SIGNED
Definition: pkcs7.h:110
cbs
const CBS * cbs
Definition: third_party/boringssl-with-bazel/src/crypto/trust_token/internal.h:107
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
base.h
asn1_object_st
Definition: third_party/boringssl-with-bazel/src/crypto/asn1/internal.h:102
PKCS7::type
ASN1_OBJECT * type
Definition: pkcs7.h:131
PKCS7::ber_bytes
uint8_t * ber_bytes
Definition: pkcs7.h:126
PKCS7_type_is_data
OPENSSL_EXPORT int PKCS7_type_is_data(const PKCS7 *p7)
Definition: pkcs7_x509.c:363
PKCS7_SIGN_ENVELOPE
Definition: pkcs7.h:115
PKCS7::encrypted
PKCS7_ENCRYPT * encrypted
Definition: pkcs7.h:139
evp_pkey_st
Definition: evp.h:1046
STACK_OF
#define STACK_OF(type)
Definition: stack.h:125
PKCS7::data
ASN1_OCTET_STRING * data
Definition: pkcs7.h:134
PKCS7::enveloped
PKCS7_ENVELOPE * enveloped
Definition: pkcs7.h:136
PKCS7_get_certificates
OPENSSL_EXPORT int PKCS7_get_certificates(STACK_OF(X509) *out_certs, CBS *cbs)
Definition: pkcs7_x509.c:33
PKCS7_get_CRLs
OPENSSL_EXPORT int PKCS7_get_CRLs(STACK_OF(X509_CRL) *out_crls, CBS *cbs)
Definition: pkcs7_x509.c:66
X509_crl_st
Definition: third_party/boringssl-with-bazel/src/crypto/x509/internal.h:195
BSSL_NAMESPACE_END
#define BSSL_NAMESPACE_END
Definition: base.h:480
PKCS7_get_PEM_CRLs
OPENSSL_EXPORT int PKCS7_get_PEM_CRLs(STACK_OF(X509_CRL) *out_crls, BIO *pem_bio)
Definition: pkcs7_x509.c:151
PKCS7_bundle_CRLs
OPENSSL_EXPORT int PKCS7_bundle_CRLs(CBB *out, const STACK_OF(X509_CRL) *crls)
Definition: pkcs7_x509.c:233
PKCS7_get_raw_certificates
OPENSSL_EXPORT int PKCS7_get_raw_certificates(STACK_OF(CRYPTO_BUFFER) *out_certs, CBS *cbs, CRYPTO_BUFFER_POOL *pool)
Definition: pkcs7.c:86
PKCS7_type_is_digest
OPENSSL_EXPORT int PKCS7_type_is_digest(const PKCS7 *p7)
Definition: pkcs7_x509.c:364
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
d
static const fe d
Definition: curve25519_tables.h:19
DECLARE_STACK_OF
#define DECLARE_STACK_OF(type)
Definition: stack.h:127
PKCS7_get_PEM_certificates
OPENSSL_EXPORT int PKCS7_get_PEM_certificates(STACK_OF(X509) *out_certs, BIO *pem_bio)
Definition: pkcs7_x509.c:129
PKCS7::signed_and_enveloped
PKCS7_SIGN_ENVELOPE * signed_and_enveloped
Definition: pkcs7.h:137
BSSL_NAMESPACE_BEGIN
Definition: trust_token_test.cc:45
x509_st
Definition: third_party/boringssl-with-bazel/src/crypto/x509/internal.h:139
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
PKCS7::ber_len
size_t ber_len
Definition: pkcs7.h:127
PKCS7::sign
PKCS7_SIGNED * sign
Definition: pkcs7.h:135
i2d_PKCS7_bio
OPENSSL_EXPORT int i2d_PKCS7_bio(BIO *bio, const PKCS7 *p7)
Definition: pkcs7_x509.c:342
OPENSSL_EXPORT
#define OPENSSL_EXPORT
Definition: base.h:222
BORINGSSL_MAKE_DELETER
#define BORINGSSL_MAKE_DELETER(type, deleter)
Definition: base.h:506
d2i_PKCS7
OPENSSL_EXPORT PKCS7 * d2i_PKCS7(PKCS7 **out, const uint8_t **inp, size_t len)
Definition: pkcs7_x509.c:282
crypto_buffer_pool_st
Definition: third_party/boringssl-with-bazel/src/crypto/pool/internal.h:39
pool
InternalDescriptorPool * pool
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:807
PKCS7_sign
OPENSSL_EXPORT PKCS7 * PKCS7_sign(X509 *sign_cert, EVP_PKEY *pkey, STACK_OF(X509) *certs, BIO *data, int flags)
Definition: pkcs7_x509.c:472
PKCS7_type_is_encrypted
OPENSSL_EXPORT int PKCS7_type_is_encrypted(const PKCS7 *p7)
Definition: pkcs7_x509.c:365
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
PKCS7_ENVELOPE
void PKCS7_ENVELOPE
Definition: pkcs7.h:120
asn1_type_st
Definition: asn1.h:1481
PKCS7
Definition: pkcs7.h:125
PKCS7_bundle_certificates
OPENSSL_EXPORT int PKCS7_bundle_certificates(CBB *out, const STACK_OF(X509) *certs)
Definition: pkcs7_x509.c:200
PKCS7_type_is_signed
OPENSSL_EXPORT int PKCS7_type_is_signed(const PKCS7 *p7)
Definition: pkcs7_x509.c:367
d2i_PKCS7_bio
OPENSSL_EXPORT PKCS7 * d2i_PKCS7_bio(BIO *bio, PKCS7 **out)
Definition: pkcs7_x509.c:298
asn1_string_st
Definition: asn1.h:543
stack.h
cbb_st
Definition: bytestring.h:375


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:52