Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/include/openssl/x509.h

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected])
2 // Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
3 //
4 // ECDH support in OpenSSL originally developed by
5 // SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
6 //
7 // SPDX-License-Identifier: Apache-2.0
8
9 #ifndef OPENSSL_HEADER_X509_H
10 #define OPENSSL_HEADER_X509_H
11
12 #include <openssl/base.h>
13
14 #include <time.h>
15
16 #include <openssl/asn1.h>
17 #include <openssl/bio.h>
18 #include <openssl/cipher.h>
19 #include <openssl/conf.h>
20 #include <openssl/dh.h>
21 #include <openssl/dsa.h>
22 #include <openssl/ec.h>
23 #include <openssl/ecdh.h>
24 #include <openssl/ecdsa.h>
25 #include <openssl/evp.h>
26 #include <openssl/lhash.h>
27 #include <openssl/obj.h>
28 #include <openssl/pkcs7.h>
29 #include <openssl/pool.h>
30 #include <openssl/rsa.h>
31 #include <openssl/sha.h>
32 #include <openssl/stack.h>
33 #include <openssl/thread.h>
34 #include <openssl/x509v3_errors.h> // IWYU pragma: export
35
36 #if defined(__cplusplus)
37 extern "C" {
38 #endif
39
40
41 // Legacy X.509 library.
42 //
43 // This header is part of OpenSSL's X.509 implementation. It is retained for
44 // compatibility but should not be used by new code. The functions are difficult
45 // to use correctly, and have buggy or non-standard behaviors. They are thus
46 // particularly prone to behavior changes and API removals, as BoringSSL
47 // iterates on these issues.
48 //
49 // In the future, a replacement library will be available. Meanwhile, minimize
50 // dependencies on this header where possible.
51
52
53 // Certificates.
54 //
55 // An |X509| object represents an X.509 certificate, defined in RFC 5280.
56 //
57 // Although an |X509| is a mutable object, mutating an |X509| can give incorrect
58 // results. Callers typically obtain |X509|s by parsing some input with
59 // |d2i_X509|, etc. Such objects carry information such as the serialized
60 // TBSCertificate and decoded extensions, which will become inconsistent when
61 // mutated.
62 //
63 // Instead, mutation functions should only be used when issuing new
64 // certificates, as described in a later section.
65
66 DEFINE_STACK_OF(X509)
67
68 // X509 is an |ASN1_ITEM| whose ASN.1 type is X.509 Certificate (RFC 5280) and C
69 // type is |X509*|.
70 DECLARE_ASN1_ITEM(X509)
71
72 // X509_up_ref adds one to the reference count of |x509| and returns one.
73 OPENSSL_EXPORT int X509_up_ref(X509 *x509);
74
75 // X509_chain_up_ref returns a newly-allocated |STACK_OF(X509)| containing a
76 // shallow copy of |chain|, or NULL on error. That is, the return value has the
77 // same contents as |chain|, and each |X509|'s reference count is incremented by
78 // one.
79 OPENSSL_EXPORT STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain);
80
81 // X509_dup returns a newly-allocated copy of |x509|, or NULL on error. This
82 // function works by serializing the structure, so auxiliary properties (see
83 // |i2d_X509_AUX|) are not preserved. Additionally, if |x509| is incomplete,
84 // this function may fail.
85 //
86 // TODO(https://crbug.com/boringssl/407): This function should be const and
87 // thread-safe but is currently neither in some cases, notably if |crl| was
88 // mutated.
89 OPENSSL_EXPORT X509 *X509_dup(X509 *x509);
90
91 // X509_free decrements |x509|'s reference count and, if zero, releases memory
92 // associated with |x509|.
93 OPENSSL_EXPORT void X509_free(X509 *x509);
94
95 // d2i_X509 parses up to |len| bytes from |*inp| as a DER-encoded X.509
96 // Certificate (RFC 5280), as described in |d2i_SAMPLE_with_reuse|.
97 OPENSSL_EXPORT X509 *d2i_X509(X509 **out, const uint8_t **inp, long len);
98
99 // X509_parse_from_buffer parses an X.509 structure from |buf| and returns a
100 // fresh X509 or NULL on error. There must not be any trailing data in |buf|.
101 // The returned structure (if any) holds a reference to |buf| rather than
102 // copying parts of it as a normal |d2i_X509| call would do.
103 OPENSSL_EXPORT X509 *X509_parse_from_buffer(CRYPTO_BUFFER *buf);
104
105 // i2d_X509 marshals |x509| as a DER-encoded X.509 Certificate (RFC 5280), as
106 // described in |i2d_SAMPLE|.
107 //
108 // TODO(https://crbug.com/boringssl/407): This function should be const and
109 // thread-safe but is currently neither in some cases, notably if |x509| was
110 // mutated.
111 OPENSSL_EXPORT int i2d_X509(X509 *x509, uint8_t **outp);
112
113 // X509_VERSION_* are X.509 version numbers. Note the numerical values of all
114 // defined X.509 versions are one less than the named version.
115 #define X509_VERSION_1 0
116 #define X509_VERSION_2 1
117 #define X509_VERSION_3 2
118
119 // X509_get_version returns the numerical value of |x509|'s version, which will
120 // be one of the |X509_VERSION_*| constants.
121 OPENSSL_EXPORT long X509_get_version(const X509 *x509);
122
123 // X509_get0_serialNumber returns |x509|'s serial number.
124 OPENSSL_EXPORT const ASN1_INTEGER *X509_get0_serialNumber(const X509 *x509);
125
126 // X509_get0_notBefore returns |x509|'s notBefore time.
127 OPENSSL_EXPORT const ASN1_TIME *X509_get0_notBefore(const X509 *x509);
128
129 // X509_get0_notAfter returns |x509|'s notAfter time.
130 OPENSSL_EXPORT const ASN1_TIME *X509_get0_notAfter(const X509 *x509);
131
132 // X509_get_issuer_name returns |x509|'s issuer.
133 OPENSSL_EXPORT X509_NAME *X509_get_issuer_name(const X509 *x509);
134
135 // X509_get_subject_name returns |x509|'s subject.
136 OPENSSL_EXPORT X509_NAME *X509_get_subject_name(const X509 *x509);
137
138 // X509_get_X509_PUBKEY returns the public key of |x509|. Note this function is
139 // not const-correct for legacy reasons. Callers should not modify the returned
140 // object.
141 OPENSSL_EXPORT X509_PUBKEY *X509_get_X509_PUBKEY(const X509 *x509);
142
143 // X509_get0_pubkey returns |x509|'s public key as an |EVP_PKEY|, or NULL if the
144 // public key was unsupported or could not be decoded. The |EVP_PKEY| is cached
145 // in |x509|, so callers must not mutate the result.
146 OPENSSL_EXPORT EVP_PKEY *X509_get0_pubkey(const X509 *x509);
147
148 // X509_get_pubkey behaves like |X509_get0_pubkey| but increments the reference
149 // count on the |EVP_PKEY|. The caller must release the result with
150 // |EVP_PKEY_free| when done. The |EVP_PKEY| is cached in |x509|, so callers
151 // must not mutate the result.
152 OPENSSL_EXPORT EVP_PKEY *X509_get_pubkey(const X509 *x509);
153
154 // X509_get0_pubkey_bitstr returns the BIT STRING portion of |x509|'s public
155 // key. Note this does not contain the AlgorithmIdentifier portion.
156 //
157 // WARNING: This function returns a non-const pointer for OpenSSL compatibility,
158 // but the caller must not modify the resulting object. Doing so will break
159 // internal invariants in |x509|.
160 OPENSSL_EXPORT ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x509);
161
162 // X509_check_private_key returns one if |x509|'s public key matches |pkey| and
163 // zero otherwise.
164 OPENSSL_EXPORT int X509_check_private_key(const X509 *x509,
165 const EVP_PKEY *pkey);
166
167 // X509_get0_uids sets |*out_issuer_uid| to a non-owning pointer to the
168 // issuerUID field of |x509|, or NULL if |x509| has no issuerUID. It similarly
169 // outputs |x509|'s subjectUID field to |*out_subject_uid|.
170 //
171 // Callers may pass NULL to either |out_issuer_uid| or |out_subject_uid| to
172 // ignore the corresponding field.
173 OPENSSL_EXPORT void X509_get0_uids(const X509 *x509,
174 const ASN1_BIT_STRING **out_issuer_uid,
175 const ASN1_BIT_STRING **out_subject_uid);
176
177 // The following bits are returned from |X509_get_extension_flags|.
178
179 // EXFLAG_BCONS indicates the certificate has a basic constraints extension.
180 #define EXFLAG_BCONS 0x1
181 // EXFLAG_KUSAGE indicates the certifcate has a key usage extension.
182 #define EXFLAG_KUSAGE 0x2
183 // EXFLAG_XKUSAGE indicates the certifcate has an extended key usage extension.
184 #define EXFLAG_XKUSAGE 0x4
185 // EXFLAG_NSCERT indicates the certificate has a legacy Netscape certificate
186 // type extension.
187 #define EXFLAG_NSCERT 0x8
188 // EXFLAG_CA indicates the certificate has a basic constraints extension with
189 // the CA bit set.
190 #define EXFLAG_CA 0x10
191 // EXFLAG_SI indicates the certificate is self-issued, i.e. its subject and
192 // issuer names match.
193 #define EXFLAG_SI 0x20
194 // EXFLAG_V1 indicates an X.509v1 certificate.
195 #define EXFLAG_V1 0x40
196 // EXFLAG_INVALID indicates an error processing some extension. The certificate
197 // should not be accepted. Note the lack of this bit does not imply all
198 // extensions are valid, only those used to compute extension flags.
199 #define EXFLAG_INVALID 0x80
200 // EXFLAG_SET is an internal bit that indicates extension flags were computed.
201 #define EXFLAG_SET 0x100
202 // EXFLAG_CRITICAL indicates an unsupported critical extension. The certificate
203 // should not be accepted.
204 #define EXFLAG_CRITICAL 0x200
205 // EXFLAG_SS indicates the certificate is likely self-signed. That is, if it is
206 // self-issued, its authority key identifier (if any) matches itself, and its
207 // key usage extension (if any) allows certificate signatures. The signature
208 // itself is not checked in computing this bit.
209 #define EXFLAG_SS 0x2000
210
211 // X509_get_extension_flags decodes a set of extensions from |x509| and returns
212 // a collection of |EXFLAG_*| bits which reflect |x509|. If there was an error
213 // in computing this bitmask, the result will include the |EXFLAG_INVALID| bit.
214 OPENSSL_EXPORT uint32_t X509_get_extension_flags(X509 *x509);
215
216 // X509_get_pathlen returns path length constraint from the basic constraints
217 // extension in |x509|. (See RFC 5280, section 4.2.1.9.) It returns -1 if the
218 // constraint is not present, or if some extension in |x509| was invalid.
219 //
220 // TODO(crbug.com/boringssl/381): Decoding an |X509| object will not check for
221 // invalid extensions. To detect the error case, call
222 // |X509_get_extension_flags| and check the |EXFLAG_INVALID| bit.
223 OPENSSL_EXPORT long X509_get_pathlen(X509 *x509);
224
225 // X509v3_KU_* are key usage bits returned from |X509_get_key_usage|.
226 #define X509v3_KU_DIGITAL_SIGNATURE 0x0080
227 #define X509v3_KU_NON_REPUDIATION 0x0040
228 #define X509v3_KU_KEY_ENCIPHERMENT 0x0020
229 #define X509v3_KU_DATA_ENCIPHERMENT 0x0010
230 #define X509v3_KU_KEY_AGREEMENT 0x0008
231 #define X509v3_KU_KEY_CERT_SIGN 0x0004
232 #define X509v3_KU_CRL_SIGN 0x0002
233 #define X509v3_KU_ENCIPHER_ONLY 0x0001
234 #define X509v3_KU_DECIPHER_ONLY 0x8000
235
236 // X509_get_key_usage returns a bitmask of key usages (see Section 4.2.1.3 of
237 // RFC 5280) which |x509| is valid for. This function only reports the first 16
238 // bits, in a little-endian byte order, but big-endian bit order. That is, bits
239 // 0 though 7 are reported at 1<<7 through 1<<0, and bits 8 through 15 are
240 // reported at 1<<15 through 1<<8.
241 //
242 // Instead of depending on this bit order, callers should compare against the
243 // |X509v3_KU_*| constants.
244 //
245 // If |x509| has no key usage extension, all key usages are valid and this
246 // function returns |UINT32_MAX|. If there was an error processing |x509|'s
247 // extensions, or if the first 16 bits in the key usage extension were all zero,
248 // this function returns zero.
249 OPENSSL_EXPORT uint32_t X509_get_key_usage(X509 *x509);
250
251 // XKU_* are extended key usage bits returned from
252 // |X509_get_extended_key_usage|.
253 #define XKU_SSL_SERVER 0x1
254 #define XKU_SSL_CLIENT 0x2
255 #define XKU_SMIME 0x4
256 #define XKU_CODE_SIGN 0x8
257 #define XKU_SGC 0x10
258 #define XKU_OCSP_SIGN 0x20
259 #define XKU_TIMESTAMP 0x40
260 #define XKU_DVCS 0x80
261 #define XKU_ANYEKU 0x100
262
263 // X509_get_extended_key_usage returns a bitmask of extended key usages (see
264 // Section 4.2.1.12 of RFC 5280) which |x509| is valid for. The result will be
265 // a combination of |XKU_*| constants. If checking an extended key usage not
266 // defined above, callers should extract the extended key usage extension
267 // separately, e.g. via |X509_get_ext_d2i|.
268 //
269 // If |x509| has no extended key usage extension, all extended key usages are
270 // valid and this function returns |UINT32_MAX|. If there was an error
271 // processing |x509|'s extensions, or if |x509|'s extended key usage extension
272 // contained no recognized usages, this function returns zero.
273 OPENSSL_EXPORT uint32_t X509_get_extended_key_usage(X509 *x509);
274
275 // X509_get0_subject_key_id returns |x509|'s subject key identifier, if present.
276 // (See RFC 5280, section 4.2.1.2.) It returns NULL if the extension is not
277 // present or if some extension in |x509| was invalid.
278 //
279 // TODO(crbug.com/boringssl/381): Decoding an |X509| object will not check for
280 // invalid extensions. To detect the error case, call
281 // |X509_get_extension_flags| and check the |EXFLAG_INVALID| bit.
282 OPENSSL_EXPORT const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x509);
283
284 // X509_get0_authority_key_id returns keyIdentifier of |x509|'s authority key
285 // identifier, if the extension and field are present. (See RFC 5280,
286 // section 4.2.1.1.) It returns NULL if the extension is not present, if it is
287 // present but lacks a keyIdentifier field, or if some extension in |x509| was
288 // invalid.
289 //
290 // TODO(crbug.com/boringssl/381): Decoding an |X509| object will not check for
291 // invalid extensions. To detect the error case, call
292 // |X509_get_extension_flags| and check the |EXFLAG_INVALID| bit.
293 OPENSSL_EXPORT const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x509);
294
295 DEFINE_STACK_OF(GENERAL_NAME)
296 typedef STACK_OF(GENERAL_NAME) GENERAL_NAMES;
297
298 // X509_get0_authority_issuer returns the authorityCertIssuer of |x509|'s
299 // authority key identifier, if the extension and field are present. (See
300 // RFC 5280, section 4.2.1.1.) It returns NULL if the extension is not present,
301 // if it is present but lacks a authorityCertIssuer field, or if some extension
302 // in |x509| was invalid.
303 //
304 // TODO(crbug.com/boringssl/381): Decoding an |X509| object will not check for
305 // invalid extensions. To detect the error case, call
306 // |X509_get_extension_flags| and check the |EXFLAG_INVALID| bit.
307 OPENSSL_EXPORT const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x509);
308
309 // X509_get0_authority_serial returns the authorityCertSerialNumber of |x509|'s
310 // authority key identifier, if the extension and field are present. (See
311 // RFC 5280, section 4.2.1.1.) It returns NULL if the extension is not present,
312 // if it is present but lacks a authorityCertSerialNumber field, or if some
313 // extension in |x509| was invalid.
314 //
315 // TODO(crbug.com/boringssl/381): Decoding an |X509| object will not check for
316 // invalid extensions. To detect the error case, call
317 // |X509_get_extension_flags| and check the |EXFLAG_INVALID| bit.
318 OPENSSL_EXPORT const ASN1_INTEGER *X509_get0_authority_serial(X509 *x509);
319
320 // X509_get0_extensions returns |x509|'s extension list, or NULL if |x509| omits
321 // it.
322 OPENSSL_EXPORT const STACK_OF(X509_EXTENSION) *X509_get0_extensions(
323 const X509 *x509);
324
325 // X509_get_ext_count returns the number of extensions in |x|.
326 OPENSSL_EXPORT int X509_get_ext_count(const X509 *x);
327
328 // X509_get_ext_by_NID behaves like |X509v3_get_ext_by_NID| but searches for
329 // extensions in |x|.
330 OPENSSL_EXPORT int X509_get_ext_by_NID(const X509 *x, int nid, int lastpos);
331
332 // X509_get_ext_by_OBJ behaves like |X509v3_get_ext_by_OBJ| but searches for
333 // extensions in |x|.
334 OPENSSL_EXPORT int X509_get_ext_by_OBJ(const X509 *x, const ASN1_OBJECT *obj,
335 int lastpos);
336
337 // X509_get_ext_by_critical behaves like |X509v3_get_ext_by_critical| but
338 // searches for extensions in |x|.
339 OPENSSL_EXPORT int X509_get_ext_by_critical(const X509 *x, int crit,
340 int lastpos);
341
342 // X509_get_ext returns the extension in |x| at index |loc|, or NULL if |loc| is
343 // out of bounds. This function returns a non-const pointer for OpenSSL
344 // compatibility, but callers should not mutate the result.
345 OPENSSL_EXPORT X509_EXTENSION *X509_get_ext(const X509 *x, int loc);
346
347 // X509_get_ext_d2i behaves like |X509V3_get_d2i| but looks for the extension in
348 // |x509|'s extension list.
349 //
350 // WARNING: This function is difficult to use correctly. See the documentation
351 // for |X509V3_get_d2i| for details.
352 OPENSSL_EXPORT void *X509_get_ext_d2i(const X509 *x509, int nid,
353 int *out_critical, int *out_idx);
354
355 // X509_get0_tbs_sigalg returns the signature algorithm in |x509|'s
356 // TBSCertificate. For the outer signature algorithm, see |X509_get0_signature|.
357 //
358 // Certificates with mismatched signature algorithms will successfully parse,
359 // but they will be rejected when verifying.
360 OPENSSL_EXPORT const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x509);
361
362 // X509_SIG_INFO_* are flags for |X509_get_signature_info|.
363 // X509_SIG_INFO_VALID means that the signature info is valid.
364 #define X509_SIG_INFO_VALID 0x1
365 // X509_SIG_INFO_TLS means that the signature is suitable for TLS use.
366 #define X509_SIG_INFO_TLS 0x2
367
368 // X509_get_signature_info retrieves information about the signature of |x509|.
369 // The NID of the signing digest is written to |digest_nid|, the public key
370 // algorithm to |pubkey_nid|, the effective security bits to |sec_bits|, and
371 // flag details to |flags|. Parameters other than |x509| can be set to NULL if
372 // the information is not required. It is an error to pass a null pointer to
373 // |x509|.
374 OPENSSL_EXPORT int X509_get_signature_info(X509 *x509, int *digest_nid,
375 int *pubkey_nid, int *sec_bits,
376 uint32_t *flags);
377
378 // X509_get0_signature sets |*out_sig| and |*out_alg| to the signature and
379 // signature algorithm of |x509|, respectively. Either output pointer may be
380 // NULL to ignore the value.
381 //
382 // This function outputs the outer signature algorithm. For the one in the
383 // TBSCertificate, see |X509_get0_tbs_sigalg|. Certificates with mismatched
384 // signature algorithms will successfully parse, but they will be rejected when
385 // verifying.
386 OPENSSL_EXPORT void X509_get0_signature(const ASN1_BIT_STRING **out_sig,
387 const X509_ALGOR **out_alg,
388 const X509 *x509);
389
390 // X509_get_signature_nid returns the NID corresponding to |x509|'s signature
391 // algorithm, or |NID_undef| if the signature algorithm does not correspond to
392 // a known NID.
393 OPENSSL_EXPORT int X509_get_signature_nid(const X509 *x509);
394
395 // i2d_X509_tbs serializes the TBSCertificate portion of |x509|, as described in
396 // |i2d_SAMPLE|.
397 //
398 // This function preserves the original encoding of the TBSCertificate and may
399 // not reflect modifications made to |x509|. It may be used to manually verify
400 // the signature of an existing certificate. To generate certificates, use
401 // |i2d_re_X509_tbs| instead.
402 OPENSSL_EXPORT int i2d_X509_tbs(X509 *x509, unsigned char **outp);
403
404 // X509_verify checks that |x509| has a valid signature by |pkey|. It returns
405 // one if the signature is valid and zero otherwise. Note this function only
406 // checks the signature itself and does not perform a full certificate
407 // validation.
408 OPENSSL_EXPORT int X509_verify(X509 *x509, EVP_PKEY *pkey);
409
410 // X509_get1_email returns a newly-allocated list of NUL-terminated strings
411 // containing all email addresses in |x509|'s subject and all rfc822name names
412 // in |x509|'s subject alternative names. Email addresses which contain embedded
413 // NUL bytes are skipped.
414 //
415 // On error, or if there are no such email addresses, it returns NULL. When
416 // done, the caller must release the result with |X509_email_free|.
417 OPENSSL_EXPORT STACK_OF(OPENSSL_STRING) *X509_get1_email(const X509 *x509);
418
419 // X509_get1_ocsp returns a newly-allocated list of NUL-terminated strings
420 // containing all OCSP URIs in |x509|. That is, it collects all URI
421 // AccessDescriptions with an accessMethod of id-ad-ocsp in |x509|'s authority
422 // information access extension. URIs which contain embedded NUL bytes are
423 // skipped.
424 //
425 // On error, or if there are no such URIs, it returns NULL. When done, the
426 // caller must release the result with |X509_email_free|.
427 OPENSSL_EXPORT STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(const X509 *x509);
428
429 // X509_email_free releases memory associated with |sk|, including |sk| itself.
430 // Each |OPENSSL_STRING| in |sk| must be a NUL-terminated string allocated with
431 // |OPENSSL_malloc|. If |sk| is NULL, no action is taken.
432 OPENSSL_EXPORT void X509_email_free(STACK_OF(OPENSSL_STRING) *sk);
433
434 // X509_cmp compares |a| and |b| and returns zero if they are equal, a negative
435 // number if |b| sorts after |a| and a negative number if |a| sorts after |b|.
436 // The sort order implemented by this function is arbitrary and does not
437 // reflect properties of the certificate such as expiry. Applications should not
438 // rely on the order itself.
439 //
440 // TODO(https://crbug.com/boringssl/355): This function works by comparing a
441 // cached hash of the encoded certificate. If |a| or |b| could not be
442 // serialized, the current behavior is to compare all unencodable certificates
443 // as equal. This function should only be used with |X509| objects that were
444 // parsed from bytes and never mutated.
445 //
446 // TODO(https://crbug.com/boringssl/407): This function is const, but it is not
447 // always thread-safe, notably if |a| and |b| were mutated.
448 OPENSSL_EXPORT int X509_cmp(const X509 *a, const X509 *b);
449
450
451 // Issuing certificates.
452 //
453 // An |X509| object may also represent an incomplete certificate. Callers may
454 // construct empty |X509| objects, fill in fields individually, and finally sign
455 // the result. The following functions may be used for this purpose.
456
457 // X509_new returns a newly-allocated, empty |X509| object, or NULL on error.
458 // This produces an incomplete certificate which may be filled in to issue a new
459 // certificate.
460 OPENSSL_EXPORT X509 *X509_new(void);
461
462 // X509_set_version sets |x509|'s version to |version|, which should be one of
463 // the |X509V_VERSION_*| constants. It returns one on success and zero on error.
464 //
465 // If unsure, use |X509_VERSION_3|.
466 OPENSSL_EXPORT int X509_set_version(X509 *x509, long version);
467
468 // X509_set_serialNumber sets |x509|'s serial number to |serial|. It returns one
469 // on success and zero on error.
470 OPENSSL_EXPORT int X509_set_serialNumber(X509 *x509,
471 const ASN1_INTEGER *serial);
472
473 // X509_set1_notBefore sets |x509|'s notBefore time to |tm|. It returns one on
474 // success and zero on error.
475 OPENSSL_EXPORT int X509_set1_notBefore(X509 *x509, const ASN1_TIME *tm);
476
477 // X509_set1_notAfter sets |x509|'s notAfter time to |tm|. it returns one on
478 // success and zero on error.
479 OPENSSL_EXPORT int X509_set1_notAfter(X509 *x509, const ASN1_TIME *tm);
480
481 // X509_getm_notBefore returns a mutable pointer to |x509|'s notBefore time.
482 OPENSSL_EXPORT ASN1_TIME *X509_getm_notBefore(X509 *x509);
483
484 // X509_getm_notAfter returns a mutable pointer to |x509|'s notAfter time.
485 OPENSSL_EXPORT ASN1_TIME *X509_getm_notAfter(X509 *x);
486
487 // X509_set_issuer_name sets |x509|'s issuer to a copy of |name|. It returns one
488 // on success and zero on error.
489 OPENSSL_EXPORT int X509_set_issuer_name(X509 *x509, X509_NAME *name);
490
491 // X509_set_subject_name sets |x509|'s subject to a copy of |name|. It returns
492 // one on success and zero on error.
493 OPENSSL_EXPORT int X509_set_subject_name(X509 *x509, X509_NAME *name);
494
495 // X509_set_pubkey sets |x509|'s public key to |pkey|. It returns one on success
496 // and zero on error. This function does not take ownership of |pkey| and
497 // internally copies and updates reference counts as needed.
498 OPENSSL_EXPORT int X509_set_pubkey(X509 *x509, EVP_PKEY *pkey);
499
500 // X509_delete_ext removes the extension in |x| at index |loc| and returns the
501 // removed extension, or NULL if |loc| was out of bounds. If non-NULL, the
502 // caller must release the result with |X509_EXTENSION_free|.
503 OPENSSL_EXPORT X509_EXTENSION *X509_delete_ext(X509 *x, int loc);
504
505 // X509_add_ext adds a copy of |ex| to |x|. It returns one on success and zero
506 // on failure. The caller retains ownership of |ex| and can release it
507 // independently of |x|.
508 //
509 // The new extension is inserted at index |loc|, shifting extensions to the
510 // right. If |loc| is -1 or out of bounds, the new extension is appended to the
511 // list.
512 OPENSSL_EXPORT int X509_add_ext(X509 *x, const X509_EXTENSION *ex, int loc);
513
514 // X509_add1_ext_i2d behaves like |X509V3_add1_i2d| but adds the extension to
515 // |x|'s extension list.
516 //
517 // WARNING: This function may return zero or -1 on error. The caller must also
518 // ensure |value|'s type matches |nid|. See the documentation for
519 // |X509V3_add1_i2d| for details.
520 OPENSSL_EXPORT int X509_add1_ext_i2d(X509 *x, int nid, void *value, int crit,
521 unsigned long flags);
522
523 // X509_sign signs |x509| with |pkey| and replaces the signature algorithm and
524 // signature fields. It returns the length of the signature on success and zero
525 // on error. This function uses digest algorithm |md|, or |pkey|'s default if
526 // NULL. Other signing parameters use |pkey|'s defaults. To customize them, use
527 // |X509_sign_ctx|.
528 OPENSSL_EXPORT int X509_sign(X509 *x509, EVP_PKEY *pkey, const EVP_MD *md);
529
530 // X509_sign_ctx signs |x509| with |ctx| and replaces the signature algorithm
531 // and signature fields. It returns the length of the signature on success and
532 // zero on error. The signature algorithm and parameters come from |ctx|, which
533 // must have been initialized with |EVP_DigestSignInit|. The caller should
534 // configure the corresponding |EVP_PKEY_CTX| before calling this function.
535 //
536 // On success or failure, this function mutates |ctx| and resets it to the empty
537 // state. Caller should not rely on its contents after the function returns.
538 OPENSSL_EXPORT int X509_sign_ctx(X509 *x509, EVP_MD_CTX *ctx);
539
540 // i2d_re_X509_tbs serializes the TBSCertificate portion of |x509|, as described
541 // in |i2d_SAMPLE|.
542 //
543 // This function re-encodes the TBSCertificate and may not reflect |x509|'s
544 // original encoding. It may be used to manually generate a signature for a new
545 // certificate. To verify certificates, use |i2d_X509_tbs| instead.
546 OPENSSL_EXPORT int i2d_re_X509_tbs(X509 *x509, unsigned char **outp);
547
548 // X509_set1_signature_algo sets |x509|'s signature algorithm to |algo| and
549 // returns one on success or zero on error. It updates both the signature field
550 // of the TBSCertificate structure, and the signatureAlgorithm field of the
551 // Certificate.
552 OPENSSL_EXPORT int X509_set1_signature_algo(X509 *x509, const X509_ALGOR *algo);
553
554 // X509_set1_signature_value sets |x509|'s signature to a copy of the |sig_len|
555 // bytes pointed by |sig|. It returns one on success and zero on error.
556 //
557 // Due to a specification error, X.509 certificates store signatures in ASN.1
558 // BIT STRINGs, but signature algorithms return byte strings rather than bit
559 // strings. This function creates a BIT STRING containing a whole number of
560 // bytes, with the bit order matching the DER encoding. This matches the
561 // encoding used by all X.509 signature algorithms.
562 OPENSSL_EXPORT int X509_set1_signature_value(X509 *x509, const uint8_t *sig,
563 size_t sig_len);
564
565
566 // Auxiliary certificate properties.
567 //
568 // |X509| objects optionally maintain auxiliary properties. These are not part
569 // of the certificates themselves, and thus are not covered by signatures or
570 // preserved by the standard serialization. They are used as inputs or outputs
571 // to other functions in this library.
572
573 // i2d_X509_AUX marshals |x509| as a DER-encoded X.509 Certificate (RFC 5280),
574 // followed optionally by a separate, OpenSSL-specific structure with auxiliary
575 // properties. It behaves as described in |i2d_SAMPLE|.
576 //
577 // Unlike similarly-named functions, this function does not output a single
578 // ASN.1 element. Directly embedding the output in a larger ASN.1 structure will
579 // not behave correctly.
580 //
581 // TODO(crbug.com/boringssl/407): |x509| should be const.
582 OPENSSL_EXPORT int i2d_X509_AUX(X509 *x509, uint8_t **outp);
583
584 // d2i_X509_AUX parses up to |length| bytes from |*inp| as a DER-encoded X.509
585 // Certificate (RFC 5280), followed optionally by a separate, OpenSSL-specific
586 // structure with auxiliary properties. It behaves as described in
587 // |d2i_SAMPLE_with_reuse|.
588 //
589 // WARNING: Passing untrusted input to this function allows an attacker to
590 // control auxiliary properties. This can allow unexpected influence over the
591 // application if the certificate is used in a context that reads auxiliary
592 // properties. This includes PKCS#12 serialization, trusted certificates in
593 // |X509_STORE|, and callers of |X509_alias_get0| or |X509_keyid_get0|.
594 //
595 // Unlike similarly-named functions, this function does not parse a single
596 // ASN.1 element. Trying to parse data directly embedded in a larger ASN.1
597 // structure will not behave correctly.
598 OPENSSL_EXPORT X509 *d2i_X509_AUX(X509 **x509, const uint8_t **inp,
599 long length);
600
601 // X509_alias_set1 sets |x509|'s alias to |len| bytes from |name|. If |name| is
602 // NULL, the alias is cleared instead. Aliases are not part of the certificate
603 // itself and will not be serialized by |i2d_X509|. If |x509| is serialized in
604 // a PKCS#12 structure, the friendlyName attribute (RFC 2985) will contain this
605 // alias.
606 OPENSSL_EXPORT int X509_alias_set1(X509 *x509, const uint8_t *name,
607 ossl_ssize_t len);
608
609 // X509_keyid_set1 sets |x509|'s key ID to |len| bytes from |id|. If |id| is
610 // NULL, the key ID is cleared instead. Key IDs are not part of the certificate
611 // itself and will not be serialized by |i2d_X509|.
612 OPENSSL_EXPORT int X509_keyid_set1(X509 *x509, const uint8_t *id,
613 ossl_ssize_t len);
614
615 // X509_alias_get0 looks up |x509|'s alias. If found, it sets |*out_len| to the
616 // alias's length and returns a pointer to a buffer containing the contents. If
617 // not found, it outputs the empty string by returning NULL and setting
618 // |*out_len| to zero.
619 //
620 // If |x509| was parsed from a PKCS#12 structure (see
621 // |PKCS12_get_key_and_certs|), the alias will reflect the friendlyName
622 // attribute (RFC 2985).
623 //
624 // WARNING: In OpenSSL, this function did not set |*out_len| when the alias was
625 // missing. Callers that target both OpenSSL and BoringSSL should set the value
626 // to zero before calling this function.
627 OPENSSL_EXPORT const uint8_t *X509_alias_get0(const X509 *x509, int *out_len);
628
629 // X509_keyid_get0 looks up |x509|'s key ID. If found, it sets |*out_len| to the
630 // key ID's length and returns a pointer to a buffer containing the contents. If
631 // not found, it outputs the empty string by returning NULL and setting
632 // |*out_len| to zero.
633 //
634 // WARNING: In OpenSSL, this function did not set |*out_len| when the alias was
635 // missing. Callers that target both OpenSSL and BoringSSL should set the value
636 // to zero before calling this function.
637 OPENSSL_EXPORT const uint8_t *X509_keyid_get0(const X509 *x509, int *out_len);
638
639 // X509_add1_trust_object configures |x509| as a valid trust anchor for |obj|.
640 // It returns one on success and zero on error. |obj| should be a certificate
641 // usage OID associated with an |X509_TRUST| object.
642 //
643 // See |X509_VERIFY_PARAM_set_trust| for details on how this value is evaluated.
644 // Note this only takes effect if |x509| was configured as a trusted certificate
645 // via |X509_STORE|.
646 OPENSSL_EXPORT int X509_add1_trust_object(X509 *x509, const ASN1_OBJECT *obj);
647
648 // X509_add1_reject_object configures |x509| as distrusted for |obj|. It returns
649 // one on success and zero on error. |obj| should be a certificate usage OID
650 // associated with an |X509_TRUST| object.
651 //
652 // See |X509_VERIFY_PARAM_set_trust| for details on how this value is evaluated.
653 // Note this only takes effect if |x509| was configured as a trusted certificate
654 // via |X509_STORE|.
655 OPENSSL_EXPORT int X509_add1_reject_object(X509 *x509, const ASN1_OBJECT *obj);
656
657 // X509_trust_clear clears the list of OIDs for which |x509| is trusted. See
658 // also |X509_add1_trust_object|.
659 OPENSSL_EXPORT void X509_trust_clear(X509 *x509);
660
661 // X509_reject_clear clears the list of OIDs for which |x509| is distrusted. See
662 // also |X509_add1_reject_object|.
663 OPENSSL_EXPORT void X509_reject_clear(X509 *x509);
664
665
666 // Certificate revocation lists.
667 //
668 // An |X509_CRL| object represents an X.509 certificate revocation list (CRL),
669 // defined in RFC 5280. A CRL is a signed list of certificates, the
670 // revokedCertificates field, which are no longer considered valid. Each entry
671 // of this list is represented with an |X509_REVOKED| object, documented in the
672 // "CRL entries" section below.
673 //
674 // Although an |X509_CRL| is a mutable object, mutating an |X509_CRL| or its
675 // |X509_REVOKED|s can give incorrect results. Callers typically obtain
676 // |X509_CRL|s by parsing some input with |d2i_X509_CRL|, etc. Such objects
677 // carry information such as the serialized TBSCertList and decoded extensions,
678 // which will become inconsistent when mutated.
679 //
680 // Instead, mutation functions should only be used when issuing new CRLs, as
681 // described in a later section.
682
683 DEFINE_STACK_OF(X509_CRL)
684 DEFINE_STACK_OF(X509_REVOKED)
685
686 // X509_CRL is an |ASN1_ITEM| whose ASN.1 type is X.509 CertificateList (RFC
687 // 5280) and C type is |X509_CRL*|.
688 DECLARE_ASN1_ITEM(X509_CRL)
689
690 // X509_CRL_up_ref adds one to the reference count of |crl| and returns one.
691 OPENSSL_EXPORT int X509_CRL_up_ref(X509_CRL *crl);
692
693 // X509_CRL_dup returns a newly-allocated copy of |crl|, or NULL on error. This
694 // function works by serializing the structure, so if |crl| is incomplete, it
695 // may fail.
696 //
697 // TODO(https://crbug.com/boringssl/407): This function should be const and
698 // thread-safe but is currently neither in some cases, notably if |crl| was
699 // mutated.
700 OPENSSL_EXPORT X509_CRL *X509_CRL_dup(X509_CRL *crl);
701
702 // X509_CRL_free decrements |crl|'s reference count and, if zero, releases
703 // memory associated with |crl|.
704 OPENSSL_EXPORT void X509_CRL_free(X509_CRL *crl);
705
706 // d2i_X509_CRL parses up to |len| bytes from |*inp| as a DER-encoded X.509
707 // CertificateList (RFC 5280), as described in |d2i_SAMPLE_with_reuse|.
708 OPENSSL_EXPORT X509_CRL *d2i_X509_CRL(X509_CRL **out, const uint8_t **inp,
709 long len);
710
711 // i2d_X509_CRL marshals |crl| as a X.509 CertificateList (RFC 5280), as
712 // described in |i2d_SAMPLE|.
713 //
714 // TODO(https://crbug.com/boringssl/407): This function should be const and
715 // thread-safe but is currently neither in some cases, notably if |crl| was
716 // mutated.
717 OPENSSL_EXPORT int i2d_X509_CRL(X509_CRL *crl, uint8_t **outp);
718
719 // X509_CRL_match compares |a| and |b| and returns zero if they are equal, a
720 // negative number if |b| sorts after |a| and a negative number if |a| sorts
721 // after |b|. The sort order implemented by this function is arbitrary and does
722 // not reflect properties of the CRL such as expiry. Applications should not
723 // rely on the order itself.
724 //
725 // TODO(https://crbug.com/boringssl/355): This function works by comparing a
726 // cached hash of the encoded CRL. This cached hash is computed when the CRL is
727 // parsed, but not when mutating or issuing CRLs. This function should only be
728 // used with |X509_CRL| objects that were parsed from bytes and never mutated.
729 OPENSSL_EXPORT int X509_CRL_match(const X509_CRL *a, const X509_CRL *b);
730
731 #define X509_CRL_VERSION_1 0
732 #define X509_CRL_VERSION_2 1
733
734 // X509_CRL_get_version returns the numerical value of |crl|'s version, which
735 // will be one of the |X509_CRL_VERSION_*| constants.
736 OPENSSL_EXPORT long X509_CRL_get_version(const X509_CRL *crl);
737
738 // X509_CRL_get0_lastUpdate returns |crl|'s thisUpdate time. The OpenSSL API
739 // refers to this field as lastUpdate.
740 OPENSSL_EXPORT const ASN1_TIME *X509_CRL_get0_lastUpdate(const X509_CRL *crl);
741
742 // X509_CRL_get0_nextUpdate returns |crl|'s nextUpdate time, or NULL if |crl|
743 // has none.
744 OPENSSL_EXPORT const ASN1_TIME *X509_CRL_get0_nextUpdate(const X509_CRL *crl);
745
746 // X509_CRL_get_issuer returns |crl|'s issuer name. Note this function is not
747 // const-correct for legacy reasons.
748 OPENSSL_EXPORT X509_NAME *X509_CRL_get_issuer(const X509_CRL *crl);
749
750 // X509_CRL_get0_by_serial finds the entry in |crl| whose serial number is
751 // |serial|. If found, it sets |*out| to the entry and returns one. If not
752 // found, it returns zero.
753 //
754 // On success, |*out| continues to be owned by |crl|. It is an error to free or
755 // otherwise modify |*out|.
756 //
757 // TODO(crbug.com/boringssl/600): Ideally |crl| would be const. It is broadly
758 // thread-safe, but changes the order of entries in |crl|. It cannot be called
759 // concurrently with |i2d_X509_CRL|.
760 OPENSSL_EXPORT int X509_CRL_get0_by_serial(X509_CRL *crl, X509_REVOKED **out,
761 const ASN1_INTEGER *serial);
762
763 // X509_CRL_get0_by_cert behaves like |X509_CRL_get0_by_serial|, except it looks
764 // for the entry that matches |x509|.
765 OPENSSL_EXPORT int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **out,
766 X509 *x509);
767
768 // X509_CRL_get_REVOKED returns the list of revoked certificates in |crl|, or
769 // NULL if |crl| omits it.
770 //
771 // TOOD(davidben): This function was originally a macro, without clear const
772 // semantics. It should take a const input and give const output, but the latter
773 // would break existing callers. For now, we match upstream.
774 OPENSSL_EXPORT STACK_OF(X509_REVOKED) *X509_CRL_get_REVOKED(X509_CRL *crl);
775
776 // X509_CRL_get0_extensions returns |crl|'s extension list, or NULL if |crl|
777 // omits it. A CRL can have extensions on individual entries, which is
778 // |X509_REVOKED_get0_extensions|, or on the overall CRL, which is this
779 // function.
780 OPENSSL_EXPORT const STACK_OF(X509_EXTENSION) *X509_CRL_get0_extensions(
781 const X509_CRL *crl);
782
783 // X509_CRL_get_ext_count returns the number of extensions in |x|.
784 OPENSSL_EXPORT int X509_CRL_get_ext_count(const X509_CRL *x);
785
786 // X509_CRL_get_ext_by_NID behaves like |X509v3_get_ext_by_NID| but searches for
787 // extensions in |x|.
788 OPENSSL_EXPORT int X509_CRL_get_ext_by_NID(const X509_CRL *x, int nid,
789 int lastpos);
790
791 // X509_CRL_get_ext_by_OBJ behaves like |X509v3_get_ext_by_OBJ| but searches for
792 // extensions in |x|.
793 OPENSSL_EXPORT int X509_CRL_get_ext_by_OBJ(const X509_CRL *x,
794 const ASN1_OBJECT *obj, int lastpos);
795
796 // X509_CRL_get_ext_by_critical behaves like |X509v3_get_ext_by_critical| but
797 // searches for extensions in |x|.
798 OPENSSL_EXPORT int X509_CRL_get_ext_by_critical(const X509_CRL *x, int crit,
799 int lastpos);
800
801 // X509_CRL_get_ext returns the extension in |x| at index |loc|, or NULL if
802 // |loc| is out of bounds. This function returns a non-const pointer for OpenSSL
803 // compatibility, but callers should not mutate the result.
804 OPENSSL_EXPORT X509_EXTENSION *X509_CRL_get_ext(const X509_CRL *x, int loc);
805
806 // X509_CRL_get_ext_d2i behaves like |X509V3_get_d2i| but looks for the
807 // extension in |crl|'s extension list.
808 //
809 // WARNING: This function is difficult to use correctly. See the documentation
810 // for |X509V3_get_d2i| for details.
811 OPENSSL_EXPORT void *X509_CRL_get_ext_d2i(const X509_CRL *crl, int nid,
812 int *out_critical, int *out_idx);
813
814 // X509_CRL_get0_signature sets |*out_sig| and |*out_alg| to the signature and
815 // signature algorithm of |crl|, respectively. Either output pointer may be NULL
816 // to ignore the value.
817 //
818 // This function outputs the outer signature algorithm, not the one in the
819 // TBSCertList. CRLs with mismatched signature algorithms will successfully
820 // parse, but they will be rejected when verifying.
821 OPENSSL_EXPORT void X509_CRL_get0_signature(const X509_CRL *crl,
822 const ASN1_BIT_STRING **out_sig,
823 const X509_ALGOR **out_alg);
824
825 // X509_CRL_get_signature_nid returns the NID corresponding to |crl|'s signature
826 // algorithm, or |NID_undef| if the signature algorithm does not correspond to
827 // a known NID.
828 OPENSSL_EXPORT int X509_CRL_get_signature_nid(const X509_CRL *crl);
829
830 // i2d_X509_CRL_tbs serializes the TBSCertList portion of |crl|, as described in
831 // |i2d_SAMPLE|.
832 //
833 // This function preserves the original encoding of the TBSCertList and may not
834 // reflect modifications made to |crl|. It may be used to manually verify the
835 // signature of an existing CRL. To generate CRLs, use |i2d_re_X509_CRL_tbs|
836 // instead.
837 OPENSSL_EXPORT int i2d_X509_CRL_tbs(X509_CRL *crl, unsigned char **outp);
838
839 // X509_CRL_verify checks that |crl| has a valid signature by |pkey|. It returns
840 // one if the signature is valid and zero otherwise.
841 OPENSSL_EXPORT int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *pkey);
842
843
844 // Issuing certificate revocation lists.
845 //
846 // An |X509_CRL| object may also represent an incomplete CRL. Callers may
847 // construct empty |X509_CRL| objects, fill in fields individually, and finally
848 // sign the result. The following functions may be used for this purpose.
849
850 // X509_CRL_new returns a newly-allocated, empty |X509_CRL| object, or NULL on
851 // error. This object may be filled in and then signed to construct a CRL.
852 OPENSSL_EXPORT X509_CRL *X509_CRL_new(void);
853
854 // X509_CRL_set_version sets |crl|'s version to |version|, which should be one
855 // of the |X509_CRL_VERSION_*| constants. It returns one on success and zero on
856 // error.
857 //
858 // If unsure, use |X509_CRL_VERSION_2|. Note that, unlike certificates, CRL
859 // versions are only defined up to v2. Callers should not use |X509_VERSION_3|.
860 OPENSSL_EXPORT int X509_CRL_set_version(X509_CRL *crl, long version);
861
862 // X509_CRL_set_issuer_name sets |crl|'s issuer to a copy of |name|. It returns
863 // one on success and zero on error.
864 OPENSSL_EXPORT int X509_CRL_set_issuer_name(X509_CRL *crl, X509_NAME *name);
865
866 // X509_CRL_set1_lastUpdate sets |crl|'s thisUpdate time to |tm|. It returns one
867 // on success and zero on error. The OpenSSL API refers to this field as
868 // lastUpdate.
869 OPENSSL_EXPORT int X509_CRL_set1_lastUpdate(X509_CRL *crl, const ASN1_TIME *tm);
870
871 // X509_CRL_set1_nextUpdate sets |crl|'s nextUpdate time to |tm|. It returns one
872 // on success and zero on error.
873 OPENSSL_EXPORT int X509_CRL_set1_nextUpdate(X509_CRL *crl, const ASN1_TIME *tm);
874
875 // X509_CRL_add0_revoked adds |rev| to |crl|. On success, it takes ownership of
876 // |rev| and returns one. On error, it returns zero. If this function fails, the
877 // caller retains ownership of |rev| and must release it when done.
878 OPENSSL_EXPORT int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev);
879
880 // X509_CRL_sort sorts the entries in |crl| by serial number. It returns one on
881 // success and zero on error.
882 OPENSSL_EXPORT int X509_CRL_sort(X509_CRL *crl);
883
884 // X509_CRL_delete_ext removes the extension in |x| at index |loc| and returns
885 // the removed extension, or NULL if |loc| was out of bounds. If non-NULL, the
886 // caller must release the result with |X509_EXTENSION_free|.
887 OPENSSL_EXPORT X509_EXTENSION *X509_CRL_delete_ext(X509_CRL *x, int loc);
888
889 // X509_CRL_add_ext adds a copy of |ex| to |x|. It returns one on success and
890 // zero on failure. The caller retains ownership of |ex| and can release it
891 // independently of |x|.
892 //
893 // The new extension is inserted at index |loc|, shifting extensions to the
894 // right. If |loc| is -1 or out of bounds, the new extension is appended to the
895 // list.
896 OPENSSL_EXPORT int X509_CRL_add_ext(X509_CRL *x, const X509_EXTENSION *ex,
897 int loc);
898
899 // X509_CRL_add1_ext_i2d behaves like |X509V3_add1_i2d| but adds the extension
900 // to |x|'s extension list.
901 //
902 // WARNING: This function may return zero or -1 on error. The caller must also
903 // ensure |value|'s type matches |nid|. See the documentation for
904 // |X509V3_add1_i2d| for details.
905 OPENSSL_EXPORT int X509_CRL_add1_ext_i2d(X509_CRL *x, int nid, void *value,
906 int crit, unsigned long flags);
907
908 // X509_CRL_sign signs |crl| with |pkey| and replaces the signature algorithm
909 // and signature fields. It returns the length of the signature on success and
910 // zero on error. This function uses digest algorithm |md|, or |pkey|'s default
911 // if NULL. Other signing parameters use |pkey|'s defaults. To customize them,
912 // use |X509_CRL_sign_ctx|.
913 OPENSSL_EXPORT int X509_CRL_sign(X509_CRL *crl, EVP_PKEY *pkey,
914 const EVP_MD *md);
915
916 // X509_CRL_sign_ctx signs |crl| with |ctx| and replaces the signature algorithm
917 // and signature fields. It returns the length of the signature on success and
918 // zero on error. The signature algorithm and parameters come from |ctx|, which
919 // must have been initialized with |EVP_DigestSignInit|. The caller should
920 // configure the corresponding |EVP_PKEY_CTX| before calling this function.
921 //
922 // On success or failure, this function mutates |ctx| and resets it to the empty
923 // state. Caller should not rely on its contents after the function returns.
924 OPENSSL_EXPORT int X509_CRL_sign_ctx(X509_CRL *crl, EVP_MD_CTX *ctx);
925
926 // i2d_re_X509_CRL_tbs serializes the TBSCertList portion of |crl|, as described
927 // in |i2d_SAMPLE|.
928 //
929 // This function re-encodes the TBSCertList and may not reflect |crl|'s original
930 // encoding. It may be used to manually generate a signature for a new CRL. To
931 // verify CRLs, use |i2d_X509_CRL_tbs| instead.
932 OPENSSL_EXPORT int i2d_re_X509_CRL_tbs(X509_CRL *crl, unsigned char **outp);
933
934 // X509_CRL_set1_signature_algo sets |crl|'s signature algorithm to |algo| and
935 // returns one on success or zero on error. It updates both the signature field
936 // of the TBSCertList structure, and the signatureAlgorithm field of the CRL.
937 OPENSSL_EXPORT int X509_CRL_set1_signature_algo(X509_CRL *crl,
938 const X509_ALGOR *algo);
939
940 // X509_CRL_set1_signature_value sets |crl|'s signature to a copy of the
941 // |sig_len| bytes pointed by |sig|. It returns one on success and zero on
942 // error.
943 //
944 // Due to a specification error, X.509 CRLs store signatures in ASN.1 BIT
945 // STRINGs, but signature algorithms return byte strings rather than bit
946 // strings. This function creates a BIT STRING containing a whole number of
947 // bytes, with the bit order matching the DER encoding. This matches the
948 // encoding used by all X.509 signature algorithms.
949 OPENSSL_EXPORT int X509_CRL_set1_signature_value(X509_CRL *crl,
950 const uint8_t *sig,
951 size_t sig_len);
952
953 // X509_CRL_http_nbio calls |OCSP_REQ_CTX_nbio_d2i| to exchange the request
954 // via http. On success, it parses the response as a DER-encoded |X509_CRL|
955 // ASN.1 structure.
956 OPENSSL_EXPORT int X509_CRL_http_nbio(OCSP_REQ_CTX *rctx, X509_CRL **pcrl);
957
958
959 // CRL entries.
960 //
961 // Each entry of a CRL is represented as an |X509_REVOKED| object, which
962 // describes a revoked certificate by serial number.
963 //
964 // When an |X509_REVOKED| is obtained from an |X509_CRL| object, it is an error
965 // to mutate the object. Doing so may break |X509_CRL|'s and cause the library
966 // to behave incorrectly.
967
968 // X509_REVOKED is an |ASN1_ITEM| whose ASN.1 type is an element of the
969 // revokedCertificates field of TBSCertList (RFC 5280) and C type is
970 // |X509_REVOKED*|.
971 DECLARE_ASN1_ITEM(X509_REVOKED)
972
973 // X509_REVOKED_new returns a newly-allocated, empty |X509_REVOKED| object, or
974 // NULL on allocation error.
975 OPENSSL_EXPORT X509_REVOKED *X509_REVOKED_new(void);
976
977 // X509_REVOKED_free releases memory associated with |rev|.
978 OPENSSL_EXPORT void X509_REVOKED_free(X509_REVOKED *rev);
979
980 // d2i_X509_REVOKED parses up to |len| bytes from |*inp| as a DER-encoded X.509
981 // CRL entry, as described in |d2i_SAMPLE|.
982 OPENSSL_EXPORT X509_REVOKED *d2i_X509_REVOKED(X509_REVOKED **out,
983 const uint8_t **inp, long len);
984
985 // i2d_X509_REVOKED marshals |alg| as a DER-encoded X.509 CRL entry, as
986 // described in |i2d_SAMPLE|.
987 OPENSSL_EXPORT int i2d_X509_REVOKED(const X509_REVOKED *alg, uint8_t **outp);
988
989 // X509_REVOKED_dup returns a newly-allocated copy of |rev|, or NULL on error.
990 // This function works by serializing the structure, so if |rev| is incomplete,
991 // it may fail.
992 OPENSSL_EXPORT X509_REVOKED *X509_REVOKED_dup(const X509_REVOKED *rev);
993
994 // X509_REVOKED_get0_serialNumber returns the serial number of the certificate
995 // revoked by |revoked|.
996 OPENSSL_EXPORT const ASN1_INTEGER *X509_REVOKED_get0_serialNumber(
997 const X509_REVOKED *revoked);
998
999 // X509_REVOKED_set_serialNumber sets |revoked|'s serial number to |serial|. It
1000 // returns one on success or zero on error.
1001 OPENSSL_EXPORT int X509_REVOKED_set_serialNumber(X509_REVOKED *revoked,
1002 const ASN1_INTEGER *serial);
1003
1004 // X509_REVOKED_get0_revocationDate returns the revocation time of the
1005 // certificate revoked by |revoked|.
1006 OPENSSL_EXPORT const ASN1_TIME *X509_REVOKED_get0_revocationDate(
1007 const X509_REVOKED *revoked);
1008
1009 // X509_REVOKED_set_revocationDate sets |revoked|'s revocation time to |tm|. It
1010 // returns one on success or zero on error.
1011 OPENSSL_EXPORT int X509_REVOKED_set_revocationDate(X509_REVOKED *revoked,
1012 const ASN1_TIME *tm);
1013
1014 // X509_REVOKED_get0_extensions returns |r|'s extensions list, or NULL if |r|
1015 // omits it. A CRL can have extensions on individual entries, which is this
1016 // function, or on the overall CRL, which is |X509_CRL_get0_extensions|.
1017 OPENSSL_EXPORT const STACK_OF(X509_EXTENSION) *X509_REVOKED_get0_extensions(
1018 const X509_REVOKED *r);
1019
1020 // X509_REVOKED_get_ext_count returns the number of extensions in |x|.
1021 OPENSSL_EXPORT int X509_REVOKED_get_ext_count(const X509_REVOKED *x);
1022
1023 // X509_REVOKED_get_ext_by_NID behaves like |X509v3_get_ext_by_NID| but searches
1024 // for extensions in |x|.
1025 OPENSSL_EXPORT int X509_REVOKED_get_ext_by_NID(const X509_REVOKED *x, int nid,
1026 int lastpos);
1027
1028 // X509_REVOKED_get_ext_by_OBJ behaves like |X509v3_get_ext_by_OBJ| but searches
1029 // for extensions in |x|.
1030 OPENSSL_EXPORT int X509_REVOKED_get_ext_by_OBJ(const X509_REVOKED *x,
1031 const ASN1_OBJECT *obj,
1032 int lastpos);
1033
1034 // X509_REVOKED_get_ext_by_critical behaves like |X509v3_get_ext_by_critical|
1035 // but searches for extensions in |x|.
1036 OPENSSL_EXPORT int X509_REVOKED_get_ext_by_critical(const X509_REVOKED *x,
1037 int crit, int lastpos);
1038
1039 // X509_REVOKED_get_ext returns the extension in |x| at index |loc|, or NULL if
1040 // |loc| is out of bounds. This function returns a non-const pointer for OpenSSL
1041 // compatibility, but callers should not mutate the result.
1042 OPENSSL_EXPORT X509_EXTENSION *X509_REVOKED_get_ext(const X509_REVOKED *x,
1043 int loc);
1044
1045 // X509_REVOKED_delete_ext removes the extension in |x| at index |loc| and
1046 // returns the removed extension, or NULL if |loc| was out of bounds. If
1047 // non-NULL, the caller must release the result with |X509_EXTENSION_free|.
1048 OPENSSL_EXPORT X509_EXTENSION *X509_REVOKED_delete_ext(X509_REVOKED *x,
1049 int loc);
1050
1051 // X509_REVOKED_add_ext adds a copy of |ex| to |x|. It returns one on success
1052 // and zero on failure. The caller retains ownership of |ex| and can release it
1053 // independently of |x|.
1054 //
1055 // The new extension is inserted at index |loc|, shifting extensions to the
1056 // right. If |loc| is -1 or out of bounds, the new extension is appended to the
1057 // list.
1058 OPENSSL_EXPORT int X509_REVOKED_add_ext(X509_REVOKED *x,
1059 const X509_EXTENSION *ex, int loc);
1060
1061 // X509_REVOKED_get_ext_d2i behaves like |X509V3_get_d2i| but looks for the
1062 // extension in |revoked|'s extension list.
1063 //
1064 // WARNING: This function is difficult to use correctly. See the documentation
1065 // for |X509V3_get_d2i| for details.
1066 OPENSSL_EXPORT void *X509_REVOKED_get_ext_d2i(const X509_REVOKED *revoked,
1067 int nid, int *out_critical,
1068 int *out_idx);
1069
1070 // X509_REVOKED_add1_ext_i2d behaves like |X509V3_add1_i2d| but adds the
1071 // extension to |x|'s extension list.
1072 //
1073 // WARNING: This function may return zero or -1 on error. The caller must also
1074 // ensure |value|'s type matches |nid|. See the documentation for
1075 // |X509V3_add1_i2d| for details.
1076 OPENSSL_EXPORT int X509_REVOKED_add1_ext_i2d(X509_REVOKED *x, int nid,
1077 void *value, int crit,
1078 unsigned long flags);
1079
1080
1081 // Certificate requests.
1082 //
1083 // An |X509_REQ| represents a PKCS #10 certificate request (RFC 2986). These are
1084 // also referred to as certificate signing requests or CSRs. CSRs are a common
1085 // format used to request a certificate from a CA.
1086 //
1087 // Although an |X509_REQ| is a mutable object, mutating an |X509_REQ| can give
1088 // incorrect results. Callers typically obtain |X509_REQ|s by parsing some input
1089 // with |d2i_X509_REQ|, etc. Such objects carry information such as the
1090 // serialized CertificationRequestInfo, which will become inconsistent when
1091 // mutated.
1092 //
1093 // Instead, mutation functions should only be used when issuing new CRLs, as
1094 // described in a later section.
1095
1096 // X509_REQ is an |ASN1_ITEM| whose ASN.1 type is CertificateRequest (RFC 2986)
1097 // and C type is |X509_REQ*|.
1098 DECLARE_ASN1_ITEM(X509_REQ)
1099
1100 // X509_REQ_dup returns a newly-allocated copy of |req|, or NULL on error. This
1101 // function works by serializing the structure, so if |req| is incomplete, it
1102 // may fail.
1103 //
1104 // TODO(https://crbug.com/boringssl/407): This function should be const and
1105 // thread-safe but is currently neither in some cases, notably if |req| was
1106 // mutated.
1107 OPENSSL_EXPORT X509_REQ *X509_REQ_dup(X509_REQ *req);
1108
1109 // X509_REQ_free releases memory associated with |req|.
1110 OPENSSL_EXPORT void X509_REQ_free(X509_REQ *req);
1111
1112 // d2i_X509_REQ parses up to |len| bytes from |*inp| as a DER-encoded
1113 // CertificateRequest (RFC 2986), as described in |d2i_SAMPLE_with_reuse|.
1114 OPENSSL_EXPORT X509_REQ *d2i_X509_REQ(X509_REQ **out, const uint8_t **inp,
1115 long len);
1116
1117 // i2d_X509_REQ marshals |req| as a CertificateRequest (RFC 2986), as described
1118 // in |i2d_SAMPLE|.
1119 //
1120 // TODO(https://crbug.com/boringssl/407): This function should be const and
1121 // thread-safe but is currently neither in some cases, notably if |req| was
1122 // mutated.
1123 OPENSSL_EXPORT int i2d_X509_REQ(X509_REQ *req, uint8_t **outp);
1124
1125 // X509_REQ_VERSION_1 is the version constant for |X509_REQ| objects. No other
1126 // versions are defined.
1127 #define X509_REQ_VERSION_1 0
1128
1129 // X509_REQ_get_version returns the numerical value of |req|'s version. This
1130 // will always be |X509_REQ_VERSION_1| for valid CSRs. For compatibility,
1131 // |d2i_X509_REQ| also accepts some invalid version numbers, in which case this
1132 // function may return other values.
1133 OPENSSL_EXPORT long X509_REQ_get_version(const X509_REQ *req);
1134
1135 // X509_REQ_get_subject_name returns |req|'s subject name. Note this function is
1136 // not const-correct for legacy reasons.
1137 OPENSSL_EXPORT X509_NAME *X509_REQ_get_subject_name(const X509_REQ *req);
1138
1139 // X509_REQ_get0_pubkey returns |req|'s public key as an |EVP_PKEY|, or NULL if
1140 // the public key was unsupported or could not be decoded. The |EVP_PKEY| is
1141 // cached in |req|, so callers must not mutate the result.
1142 OPENSSL_EXPORT EVP_PKEY *X509_REQ_get0_pubkey(const X509_REQ *req);
1143
1144 // X509_REQ_get_pubkey behaves like |X509_REQ_get0_pubkey| but increments the
1145 // reference count on the |EVP_PKEY|. The caller must release the result with
1146 // |EVP_PKEY_free| when done. The |EVP_PKEY| is cached in |req|, so callers must
1147 // not mutate the result.
1148 OPENSSL_EXPORT EVP_PKEY *X509_REQ_get_pubkey(const X509_REQ *req);
1149
1150 // X509_REQ_check_private_key returns one if |req|'s public key matches |pkey|
1151 // and zero otherwise.
1152 OPENSSL_EXPORT int X509_REQ_check_private_key(const X509_REQ *req,
1153 const EVP_PKEY *pkey);
1154
1155 // X509_REQ_get_attr_count returns the number of attributes in |req|.
1156 OPENSSL_EXPORT int X509_REQ_get_attr_count(const X509_REQ *req);
1157
1158 // X509_REQ_get_attr returns the attribute at index |loc| in |req|, or NULL if
1159 // out of bounds.
1160 OPENSSL_EXPORT X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc);
1161
1162 // X509_REQ_get_attr_by_NID returns the index of the attribute in |req| of type
1163 // |nid|, or a negative number if not found. If found, callers can use
1164 // |X509_REQ_get_attr| to look up the attribute by index.
1165 //
1166 // If |lastpos| is non-negative, it begins searching at |lastpos| + 1. Callers
1167 // can thus loop over all matching attributes by first passing -1 and then
1168 // passing the previously-returned value until no match is returned.
1169 OPENSSL_EXPORT int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid,
1170 int lastpos);
1171
1172 // X509_REQ_get_attr_by_OBJ behaves like |X509_REQ_get_attr_by_NID| but looks
1173 // for attributes of type |obj|.
1174 OPENSSL_EXPORT int X509_REQ_get_attr_by_OBJ(const X509_REQ *req,
1175 const ASN1_OBJECT *obj,
1176 int lastpos);
1177
1178 // X509_REQ_extension_nid returns one if |nid| is a supported CSR attribute type
1179 // for carrying extensions and zero otherwise. The supported types are
1180 // |NID_ext_req| (pkcs-9-at-extensionRequest from RFC 2985) and |NID_ms_ext_req|
1181 // (a Microsoft szOID_CERT_EXTENSIONS variant).
1182 OPENSSL_EXPORT int X509_REQ_extension_nid(int nid);
1183
1184 // X509_REQ_get_extensions decodes the most preferred list of requested
1185 // extensions in |req| and returns a newly-allocated |STACK_OF(X509_EXTENSION)|
1186 // containing the result. It returns NULL on error, or if |req| did not request
1187 // extensions.
1188 //
1189 // CSRs do not store extensions directly. Instead there are attribute types
1190 // which are defined to hold extensions. See |X509_REQ_extension_nid|. This
1191 // function supports both pkcs-9-at-extensionRequest from RFC 2985 and the
1192 // Microsoft szOID_CERT_EXTENSIONS variant. If both are present,
1193 // pkcs-9-at-extensionRequest is preferred.
1194 OPENSSL_EXPORT STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(
1195 const X509_REQ *req);
1196
1197 // X509_REQ_get0_signature sets |*out_sig| and |*out_alg| to the signature and
1198 // signature algorithm of |req|, respectively. Either output pointer may be NULL
1199 // to ignore the value.
1200 OPENSSL_EXPORT void X509_REQ_get0_signature(const X509_REQ *req,
1201 const ASN1_BIT_STRING **out_sig,
1202 const X509_ALGOR **out_alg);
1203
1204 // X509_REQ_get_signature_nid returns the NID corresponding to |req|'s signature
1205 // algorithm, or |NID_undef| if the signature algorithm does not correspond to
1206 // a known NID.
1207 OPENSSL_EXPORT int X509_REQ_get_signature_nid(const X509_REQ *req);
1208
1209 // X509_REQ_verify checks that |req| has a valid signature by |pkey|. It returns
1210 // one if the signature is valid and zero otherwise.
1211 OPENSSL_EXPORT int X509_REQ_verify(X509_REQ *req, EVP_PKEY *pkey);
1212
1213 // X509_REQ_get1_email returns a newly-allocated list of NUL-terminated strings
1214 // containing all email addresses in |req|'s subject and all rfc822name names
1215 // in |req|'s subject alternative names. The subject alternative names extension
1216 // is extracted from the result of |X509_REQ_get_extensions|. Email addresses
1217 // which contain embedded NUL bytes are skipped.
1218 //
1219 // On error, or if there are no such email addresses, it returns NULL. When
1220 // done, the caller must release the result with |X509_email_free|.
1221 OPENSSL_EXPORT STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(
1222 const X509_REQ *req);
1223
1224
1225 // Issuing certificate requests.
1226 //
1227 // An |X509_REQ| object may also represent an incomplete CSR. Callers may
1228 // construct empty |X509_REQ| objects, fill in fields individually, and finally
1229 // sign the result. The following functions may be used for this purpose.
1230
1231 // X509_REQ_new returns a newly-allocated, empty |X509_REQ| object, or NULL on
1232 // error. This object may be filled in and then signed to construct a CSR.
1233 OPENSSL_EXPORT X509_REQ *X509_REQ_new(void);
1234
1235 // X509_REQ_set_version sets |req|'s version to |version|, which should be
1236 // |X509_REQ_VERSION_1|. It returns one on success and zero on error.
1237 //
1238 // The only defined CSR version is |X509_REQ_VERSION_1|, so there is no need to
1239 // call this function.
1240 OPENSSL_EXPORT int X509_REQ_set_version(X509_REQ *req, long version);
1241
1242 // X509_REQ_set_subject_name sets |req|'s subject to a copy of |name|. It
1243 // returns one on success and zero on error.
1244 OPENSSL_EXPORT int X509_REQ_set_subject_name(X509_REQ *req, X509_NAME *name);
1245
1246 // X509_REQ_set_pubkey sets |req|'s public key to |pkey|. It returns one on
1247 // success and zero on error. This function does not take ownership of |pkey|
1248 // and internally copies and updates reference counts as needed.
1249 OPENSSL_EXPORT int X509_REQ_set_pubkey(X509_REQ *req, EVP_PKEY *pkey);
1250
1251 // X509_REQ_delete_attr removes the attribute at index |loc| in |req|. It
1252 // returns the removed attribute to the caller, or NULL if |loc| was out of
1253 // bounds. If non-NULL, the caller must release the result with
1254 // |X509_ATTRIBUTE_free| when done. It is also safe, but not necessary, to call
1255 // |X509_ATTRIBUTE_free| if the result is NULL.
1256 OPENSSL_EXPORT X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc);
1257
1258 // X509_REQ_add1_attr appends a copy of |attr| to |req|'s list of attributes. It
1259 // returns one on success and zero on error.
1260 OPENSSL_EXPORT int X509_REQ_add1_attr(X509_REQ *req,
1261 const X509_ATTRIBUTE *attr);
1262
1263 // X509_REQ_add1_attr_by_OBJ appends a new attribute to |req| with type |obj|.
1264 // It returns one on success and zero on error. The value is determined by
1265 // |X509_ATTRIBUTE_set1_data|.
1266 //
1267 // WARNING: The interpretation of |attrtype|, |data|, and |len| is complex and
1268 // error-prone. See |X509_ATTRIBUTE_set1_data| for details.
1269 OPENSSL_EXPORT int X509_REQ_add1_attr_by_OBJ(X509_REQ *req,
1270 const ASN1_OBJECT *obj,
1271 int attrtype,
1272 const unsigned char *data,
1273 int len);
1274
1275 // X509_REQ_add1_attr_by_NID behaves like |X509_REQ_add1_attr_by_OBJ| except the
1276 // attribute type is determined by |nid|.
1277 OPENSSL_EXPORT int X509_REQ_add1_attr_by_NID(X509_REQ *req, int nid,
1278 int attrtype,
1279 const unsigned char *data,
1280 int len);
1281
1282 // X509_REQ_add1_attr_by_txt behaves like |X509_REQ_add1_attr_by_OBJ| except the
1283 // attribute type is determined by calling |OBJ_txt2obj| with |attrname|.
1284 OPENSSL_EXPORT int X509_REQ_add1_attr_by_txt(X509_REQ *req,
1285 const char *attrname, int attrtype,
1286 const unsigned char *data,
1287 int len);
1288
1289 // X509_REQ_add_extensions_nid adds an attribute to |req| of type |nid|, to
1290 // request the certificate extensions in |exts|. It returns one on success and
1291 // zero on error. |nid| should be |NID_ext_req| or |NID_ms_ext_req|.
1292 OPENSSL_EXPORT int X509_REQ_add_extensions_nid(
1293 X509_REQ *req, const STACK_OF(X509_EXTENSION) *exts, int nid);
1294
1295 // X509_REQ_add_extensions behaves like |X509_REQ_add_extensions_nid|, using the
1296 // standard |NID_ext_req| for the attribute type.
1297 OPENSSL_EXPORT int X509_REQ_add_extensions(
1298 X509_REQ *req, const STACK_OF(X509_EXTENSION) *exts);
1299
1300 // X509_REQ_sign signs |req| with |pkey| and replaces the signature algorithm
1301 // and signature fields. It returns the length of the signature on success and
1302 // zero on error. This function uses digest algorithm |md|, or |pkey|'s default
1303 // if NULL. Other signing parameters use |pkey|'s defaults. To customize them,
1304 // use |X509_REQ_sign_ctx|.
1305 OPENSSL_EXPORT int X509_REQ_sign(X509_REQ *req, EVP_PKEY *pkey,
1306 const EVP_MD *md);
1307
1308 // X509_REQ_sign_ctx signs |req| with |ctx| and replaces the signature algorithm
1309 // and signature fields. It returns the length of the signature on success and
1310 // zero on error. The signature algorithm and parameters come from |ctx|, which
1311 // must have been initialized with |EVP_DigestSignInit|. The caller should
1312 // configure the corresponding |EVP_PKEY_CTX| before calling this function.
1313 //
1314 // On success or failure, this function mutates |ctx| and resets it to the empty
1315 // state. Caller should not rely on its contents after the function returns.
1316 OPENSSL_EXPORT int X509_REQ_sign_ctx(X509_REQ *req, EVP_MD_CTX *ctx);
1317
1318 // i2d_re_X509_REQ_tbs serializes the CertificationRequestInfo (see RFC 2986)
1319 // portion of |req|, as described in |i2d_SAMPLE|.
1320 //
1321 // This function re-encodes the CertificationRequestInfo and may not reflect
1322 // |req|'s original encoding. It may be used to manually generate a signature
1323 // for a new certificate request.
1324 OPENSSL_EXPORT int i2d_re_X509_REQ_tbs(X509_REQ *req, uint8_t **outp);
1325
1326 // X509_REQ_set1_signature_algo sets |req|'s signature algorithm to |algo| and
1327 // returns one on success or zero on error.
1328 OPENSSL_EXPORT int X509_REQ_set1_signature_algo(X509_REQ *req,
1329 const X509_ALGOR *algo);
1330
1331 // X509_REQ_set1_signature_value sets |req|'s signature to a copy of the
1332 // |sig_len| bytes pointed by |sig|. It returns one on success and zero on
1333 // error.
1334 //
1335 // Due to a specification error, PKCS#10 certificate requests store signatures
1336 // in ASN.1 BIT STRINGs, but signature algorithms return byte strings rather
1337 // than bit strings. This function creates a BIT STRING containing a whole
1338 // number of bytes, with the bit order matching the DER encoding. This matches
1339 // the encoding used by all X.509 signature algorithms.
1340 OPENSSL_EXPORT int X509_REQ_set1_signature_value(X509_REQ *req,
1341 const uint8_t *sig,
1342 size_t sig_len);
1343
1344
1345 // Names.
1346 //
1347 // An |X509_NAME| represents an X.509 Name structure (RFC 5280). X.509 names are
1348 // a complex, hierarchical structure over a collection of attributes. Each name
1349 // is sequence of relative distinguished names (RDNs), decreasing in
1350 // specificity. For example, the first RDN may specify the country, while the
1351 // next RDN may specify a locality. Each RDN is, itself, a set of attributes.
1352 // Having more than one attribute in an RDN is uncommon, but possible. Within an
1353 // RDN, attributes have the same level in specificity. Attribute types are
1354 // OBJECT IDENTIFIERs. This determines the ASN.1 type of the value, which is
1355 // commonly a string but may be other types.
1356 //
1357 // The |X509_NAME| representation flattens this two-level structure into a
1358 // single list of attributes. Each attribute is stored in an |X509_NAME_ENTRY|,
1359 // with also maintains the index of the RDN it is part of, accessible via
1360 // |X509_NAME_ENTRY_set|. This can be used to recover the two-level structure.
1361 //
1362 // X.509 names are largely vestigial. Historically, DNS names were parsed out of
1363 // the subject's common name attribute, but this is deprecated and has since
1364 // moved to the subject alternative name extension. In modern usage, X.509 names
1365 // are primarily opaque identifiers to link a certificate with its issuer.
1366
1367 DEFINE_STACK_OF(X509_NAME_ENTRY)
1368 DEFINE_STACK_OF(X509_NAME)
1369
1370 // X509_NAME is an |ASN1_ITEM| whose ASN.1 type is X.509 Name (RFC 5280) and C
1371 // type is |X509_NAME*|.
1372 DECLARE_ASN1_ITEM(X509_NAME)
1373
1374 // X509_NAME_new returns a new, empty |X509_NAME|, or NULL on error.
1375 OPENSSL_EXPORT X509_NAME *X509_NAME_new(void);
1376
1377 // X509_NAME_free releases memory associated with |name|.
1378 OPENSSL_EXPORT void X509_NAME_free(X509_NAME *name);
1379
1380 // d2i_X509_NAME parses up to |len| bytes from |*inp| as a DER-encoded X.509
1381 // Name (RFC 5280), as described in |d2i_SAMPLE_with_reuse|.
1382 OPENSSL_EXPORT X509_NAME *d2i_X509_NAME(X509_NAME **out, const uint8_t **inp,
1383 long len);
1384
1385 // i2d_X509_NAME marshals |in| as a DER-encoded X.509 Name (RFC 5280), as
1386 // described in |i2d_SAMPLE|.
1387 //
1388 // TODO(https://crbug.com/boringssl/407): This function should be const and
1389 // thread-safe but is currently neither in some cases, notably if |in| was
1390 // mutated.
1391 OPENSSL_EXPORT int i2d_X509_NAME(X509_NAME *in, uint8_t **outp);
1392
1393 // X509_NAME_dup returns a newly-allocated copy of |name|, or NULL on error.
1394 //
1395 // TODO(https://crbug.com/boringssl/407): This function should be const and
1396 // thread-safe but is currently neither in some cases, notably if |name| was
1397 // mutated.
1398 OPENSSL_EXPORT X509_NAME *X509_NAME_dup(X509_NAME *name);
1399
1400 // X509_NAME_cmp compares |a| and |b|'s canonicalized forms. It returns zero if
1401 // they are equal, one if |a| sorts after |b|, -1 if |b| sorts after |a|, and -2
1402 // on error.
1403 //
1404 // TODO(https://crbug.com/boringssl/407): This function is const, but it is not
1405 // always thread-safe, notably if |name| was mutated.
1406 //
1407 // TODO(https://crbug.com/boringssl/355): The -2 return is very inconvenient to
1408 // pass to a sorting function. Can we make this infallible? In the meantime,
1409 // prefer to use this function only for equality checks rather than comparisons.
1410 // Although even the library itself passes this to a sorting function.
1411 OPENSSL_EXPORT int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b);
1412
1413 // X509_NAME_get0_der marshals |name| as a DER-encoded X.509 Name (RFC 5280). On
1414 // success, it returns one and sets |*out_der| and |*out_der_len| to a buffer
1415 // containing the result. Otherwise, it returns zero. |*out_der| is owned by
1416 // |name| and must not be freed by the caller. It is invalidated after |name| is
1417 // mutated or freed.
1418 //
1419 // Avoid this function and prefer |i2d_X509_NAME|. It is one of the reasons
1420 // |X509_NAME| functions, including this one, are not consistently thread-safe
1421 // or const-correct. Depending on the resolution of
1422 // https://crbug.com/boringssl/407, this function may be removed or cause poor
1423 // performance.
1424 OPENSSL_EXPORT int X509_NAME_get0_der(X509_NAME *name, const uint8_t **out_der,
1425 size_t *out_der_len);
1426
1427 // X509_NAME_set makes a copy of |name|. On success, it frees |*xn|, sets |*xn|
1428 // to the copy, and returns one. Otherwise, it returns zero.
1429 //
1430 // TODO(https://crbug.com/boringssl/407): This function should be const and
1431 // thread-safe but is currently neither in some cases, notably if |name| was
1432 // mutated.
1433 OPENSSL_EXPORT int X509_NAME_set(X509_NAME **xn, X509_NAME *name);
1434
1435 // X509_NAME_entry_count returns the number of entries in |name|.
1436 OPENSSL_EXPORT int X509_NAME_entry_count(const X509_NAME *name);
1437
1438 // X509_NAME_get_index_by_NID returns the zero-based index of the first
1439 // attribute in |name| with type |nid|, or -1 if there is none. |nid| should be
1440 // one of the |NID_*| constants. If |lastpos| is non-negative, it begins
1441 // searching at |lastpos+1|. To search all attributes, pass in -1, not zero.
1442 //
1443 // Indices from this function refer to |X509_NAME|'s flattened representation.
1444 OPENSSL_EXPORT int X509_NAME_get_index_by_NID(const X509_NAME *name, int nid,
1445 int lastpos);
1446
1447 // X509_NAME_get_index_by_OBJ behaves like |X509_NAME_get_index_by_NID| but
1448 // looks for attributes with type |obj|.
1449 OPENSSL_EXPORT int X509_NAME_get_index_by_OBJ(const X509_NAME *name,
1450 const ASN1_OBJECT *obj,
1451 int lastpos);
1452
1453 // X509_NAME_get_entry returns the attribute in |name| at index |loc|, or NULL
1454 // if |loc| is out of range. |loc| is interpreted using |X509_NAME|'s flattened
1455 // representation. This function returns a non-const pointer for OpenSSL
1456 // compatibility, but callers should not mutate the result. Doing so will break
1457 // internal invariants in the library.
1458 OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_get_entry(const X509_NAME *name,
1459 int loc);
1460
1461 // X509_NAME_delete_entry removes and returns the attribute in |name| at index
1462 // |loc|, or NULL if |loc| is out of range. |loc| is interpreted using
1463 // |X509_NAME|'s flattened representation. If the attribute is found, the caller
1464 // is responsible for releasing the result with |X509_NAME_ENTRY_free|.
1465 //
1466 // This function will internally update RDN indices (see |X509_NAME_ENTRY_set|)
1467 // so they continue to be consecutive.
1468 OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name,
1469 int loc);
1470
1471 // X509_NAME_add_entry adds a copy of |entry| to |name| and returns one on
1472 // success or zero on error. If |loc| is -1, the entry is appended to |name|.
1473 // Otherwise, it is inserted at index |loc|. If |set| is -1, the entry is added
1474 // to the previous entry's RDN. If it is 0, the entry becomes a singleton RDN.
1475 // If 1, it is added to next entry's RDN.
1476 //
1477 // This function will internally update RDN indices (see |X509_NAME_ENTRY_set|)
1478 // so they continue to be consecutive.
1479 OPENSSL_EXPORT int X509_NAME_add_entry(X509_NAME *name,
1480 const X509_NAME_ENTRY *entry, int loc,
1481 int set);
1482
1483 // X509_NAME_add_entry_by_OBJ adds a new entry to |name| and returns one on
1484 // success or zero on error. The entry's attribute type is |obj|. The entry's
1485 // attribute value is determined by |type|, |bytes|, and |len|, as in
1486 // |X509_NAME_ENTRY_set_data|. The entry's position is determined by |loc| and
1487 // |set| as in |X509_NAME_add_entry|.
1488 OPENSSL_EXPORT int X509_NAME_add_entry_by_OBJ(X509_NAME *name,
1489 const ASN1_OBJECT *obj, int type,
1490 const uint8_t *bytes,
1491 ossl_ssize_t len, int loc,
1492 int set);
1493
1494 // X509_NAME_add_entry_by_NID behaves like |X509_NAME_add_entry_by_OBJ| but sets
1495 // the entry's attribute type to |nid|, which should be one of the |NID_*|
1496 // constants.
1497 OPENSSL_EXPORT int X509_NAME_add_entry_by_NID(X509_NAME *name, int nid,
1498 int type, const uint8_t *bytes,
1499 ossl_ssize_t len, int loc,
1500 int set);
1501
1502 // X509_NAME_add_entry_by_txt behaves like |X509_NAME_add_entry_by_OBJ| but sets
1503 // the entry's attribute type to |field|, which is passed to |OBJ_txt2obj|.
1504 OPENSSL_EXPORT int X509_NAME_add_entry_by_txt(X509_NAME *name,
1505 const char *field, int type,
1506 const uint8_t *bytes,
1507 ossl_ssize_t len, int loc,
1508 int set);
1509
1510 // X509_NAME_ENTRY is an |ASN1_ITEM| whose ASN.1 type is AttributeTypeAndValue
1511 // (RFC 5280) and C type is |X509_NAME_ENTRY*|.
1512 DECLARE_ASN1_ITEM(X509_NAME_ENTRY)
1513
1514 // X509_NAME_ENTRY_new returns a new, empty |X509_NAME_ENTRY|, or NULL on error.
1515 OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_new(void);
1516
1517 // X509_NAME_ENTRY_free releases memory associated with |entry|.
1518 OPENSSL_EXPORT void X509_NAME_ENTRY_free(X509_NAME_ENTRY *entry);
1519
1520 // d2i_X509_NAME_ENTRY parses up to |len| bytes from |*inp| as a DER-encoded
1521 // AttributeTypeAndValue (RFC 5280), as described in |d2i_SAMPLE_with_reuse|.
1522 OPENSSL_EXPORT X509_NAME_ENTRY *d2i_X509_NAME_ENTRY(X509_NAME_ENTRY **out,
1523 const uint8_t **inp,
1524 long len);
1525
1526 // i2d_X509_NAME_ENTRY marshals |in| as a DER-encoded AttributeTypeAndValue (RFC
1527 // 5280), as described in |i2d_SAMPLE|.
1528 OPENSSL_EXPORT int i2d_X509_NAME_ENTRY(const X509_NAME_ENTRY *in,
1529 uint8_t **outp);
1530
1531 // X509_NAME_ENTRY_dup returns a newly-allocated copy of |entry|, or NULL on
1532 // error.
1533 OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_dup(
1534 const X509_NAME_ENTRY *entry);
1535
1536 // X509_NAME_ENTRY_get_object returns |entry|'s attribute type. This function
1537 // returns a non-const pointer for OpenSSL compatibility, but callers should not
1538 // mutate the result. Doing so will break internal invariants in the library.
1539 OPENSSL_EXPORT ASN1_OBJECT *X509_NAME_ENTRY_get_object(
1540 const X509_NAME_ENTRY *entry);
1541
1542 // X509_NAME_ENTRY_set_object sets |entry|'s attribute type to |obj|. It returns
1543 // one on success and zero on error.
1544 OPENSSL_EXPORT int X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *entry,
1545 const ASN1_OBJECT *obj);
1546
1547 // X509_NAME_ENTRY_get_data returns |entry|'s attribute value, represented as an
1548 // |ASN1_STRING|. This value may have any ASN.1 type, so callers must check the
1549 // type before interpreting the contents. This function returns a non-const
1550 // pointer for OpenSSL compatibility, but callers should not mutate the result.
1551 // Doing so will break internal invariants in the library.
1552 //
1553 // TODO(https://crbug.com/boringssl/412): Although the spec says any ASN.1 type
1554 // is allowed, we currently only allow an ad-hoc set of types. Additionally, it
1555 // is unclear if some types can even be represented by this function.
1556 OPENSSL_EXPORT ASN1_STRING *X509_NAME_ENTRY_get_data(
1557 const X509_NAME_ENTRY *entry);
1558
1559 // X509_NAME_ENTRY_set_data sets |entry|'s value to |len| bytes from |bytes|. It
1560 // returns one on success and zero on error. If |len| is -1, |bytes| must be a
1561 // NUL-terminated C string and the length is determined by |strlen|. |bytes| is
1562 // converted to an ASN.1 type as follows:
1563 //
1564 // If |type| is a |MBSTRING_*| constant, the value is an ASN.1 string. The
1565 // string is determined by decoding |bytes| in the encoding specified by |type|,
1566 // and then re-encoding it in a form appropriate for |entry|'s attribute type.
1567 // See |ASN1_STRING_set_by_NID| for details.
1568 //
1569 // Otherwise, the value is an |ASN1_STRING| with type |type| and value |bytes|.
1570 // See |ASN1_STRING| for how to format ASN.1 types as an |ASN1_STRING|. If
1571 // |type| is |V_ASN1_UNDEF| the previous |ASN1_STRING| type is reused.
1572 OPENSSL_EXPORT int X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *entry, int type,
1573 const uint8_t *bytes,
1574 ossl_ssize_t len);
1575
1576 // X509_NAME_ENTRY_set returns the zero-based index of the RDN which contains
1577 // |entry|. Consecutive entries with the same index are part of the same RDN.
1578 OPENSSL_EXPORT int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *entry);
1579
1580 // X509_NAME_ENTRY_create_by_OBJ creates a new |X509_NAME_ENTRY| with attribute
1581 // type |obj|. The attribute value is determined from |type|, |bytes|, and |len|
1582 // as in |X509_NAME_ENTRY_set_data|. It returns the |X509_NAME_ENTRY| on success
1583 // and NULL on error.
1584 //
1585 // If |out| is non-NULL and |*out| is NULL, it additionally sets |*out| to the
1586 // result on success. If both |out| and |*out| are non-NULL, it updates the
1587 // object at |*out| instead of allocating a new one.
1588 OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(
1589 X509_NAME_ENTRY **out, const ASN1_OBJECT *obj, int type,
1590 const uint8_t *bytes, ossl_ssize_t len);
1591
1592 // X509_NAME_ENTRY_create_by_NID behaves like |X509_NAME_ENTRY_create_by_OBJ|
1593 // except the attribute type is |nid|, which should be one of the |NID_*|
1594 // constants.
1595 OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(
1596 X509_NAME_ENTRY **out, int nid, int type, const uint8_t *bytes,
1597 ossl_ssize_t len);
1598
1599 // X509_NAME_ENTRY_create_by_txt behaves like |X509_NAME_ENTRY_create_by_OBJ|
1600 // except the attribute type is |field|, which is passed to |OBJ_txt2obj|.
1601 OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_txt(
1602 X509_NAME_ENTRY **out, const char *field, int type, const uint8_t *bytes,
1603 ossl_ssize_t len);
1604
1605
1606 // Public keys.
1607 //
1608 // X.509 encodes public keys as SubjectPublicKeyInfo (RFC 5280), sometimes
1609 // referred to as SPKI. These are represented in this library by |X509_PUBKEY|.
1610
1611 // X509_PUBKEY is an |ASN1_ITEM| whose ASN.1 type is SubjectPublicKeyInfo and C
1612 // type is |X509_PUBKEY*|.
1613 DECLARE_ASN1_ITEM(X509_PUBKEY)
1614
1615 // X509_PUBKEY_new returns a newly-allocated, empty |X509_PUBKEY| object, or
1616 // NULL on error.
1617 OPENSSL_EXPORT X509_PUBKEY *X509_PUBKEY_new(void);
1618
1619 // X509_PUBKEY_free releases memory associated with |key|.
1620 OPENSSL_EXPORT void X509_PUBKEY_free(X509_PUBKEY *key);
1621
1622 // d2i_X509_PUBKEY parses up to |len| bytes from |*inp| as a DER-encoded
1623 // SubjectPublicKeyInfo, as described in |d2i_SAMPLE|.
1624 OPENSSL_EXPORT X509_PUBKEY *d2i_X509_PUBKEY(X509_PUBKEY **out,
1625 const uint8_t **inp, long len);
1626
1627 // i2d_X509_PUBKEY marshals |key| as a DER-encoded SubjectPublicKeyInfo, as
1628 // described in |i2d_SAMPLE|.
1629 OPENSSL_EXPORT int i2d_X509_PUBKEY(const X509_PUBKEY *key, uint8_t **outp);
1630
1631 // X509_PUBKEY_set serializes |pkey| into a newly-allocated |X509_PUBKEY|
1632 // structure. On success, it frees |*x| if non-NULL, then sets |*x| to the new
1633 // object, and returns one. Otherwise, it returns zero.
1634 OPENSSL_EXPORT int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey);
1635
1636 // X509_PUBKEY_get0 returns |key| as an |EVP_PKEY|, or NULL if |key| either
1637 // could not be parsed or is an unrecognized algorithm. The |EVP_PKEY| is cached
1638 // in |key|, so callers must not mutate the result.
1639 OPENSSL_EXPORT EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key);
1640
1641 // X509_PUBKEY_get behaves like |X509_PUBKEY_get0| but increments the reference
1642 // count on the |EVP_PKEY|. The caller must release the result with
1643 // |EVP_PKEY_free| when done. The |EVP_PKEY| is cached in |key|, so callers must
1644 // not mutate the result.
1645 OPENSSL_EXPORT EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key);
1646
1647 // X509_PUBKEY_set0_param sets |pub| to a key with AlgorithmIdentifier
1648 // determined by |obj|, |param_type|, and |param_value|, and an encoded
1649 // public key of |key|. On success, it gives |pub| ownership of all the other
1650 // parameters and returns one. Otherwise, it returns zero. |key| must have been
1651 // allocated by |OPENSSL_malloc|. |obj| and, if applicable, |param_value| must
1652 // not be freed after a successful call, and must have been allocated in a
1653 // manner compatible with |ASN1_OBJECT_free| or |ASN1_STRING_free|.
1654 //
1655 // |obj|, |param_type|, and |param_value| are interpreted as in
1656 // |X509_ALGOR_set0|. See |X509_ALGOR_set0| for details.
1657 OPENSSL_EXPORT int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *obj,
1658 int param_type, void *param_value,
1659 uint8_t *key, int key_len);
1660
1661 // X509_PUBKEY_get0_param outputs fields of |pub| and returns one. If |out_obj|
1662 // is not NULL, it sets |*out_obj| to AlgorithmIdentifier's OID. If |out_key|
1663 // is not NULL, it sets |*out_key| and |*out_key_len| to the encoded public key.
1664 // If |out_alg| is not NULL, it sets |*out_alg| to the AlgorithmIdentifier.
1665 //
1666 // All pointers outputted by this function are internal to |pub| and must not be
1667 // freed by the caller. Additionally, although some outputs are non-const,
1668 // callers must not mutate the resulting objects.
1669 //
1670 // Note: X.509 SubjectPublicKeyInfo structures store the encoded public key as a
1671 // BIT STRING. |*out_key| and |*out_key_len| will silently pad the key with zero
1672 // bits if |pub| did not contain a whole number of bytes. Use
1673 // |X509_PUBKEY_get0_public_key| to preserve this information.
1674 OPENSSL_EXPORT int X509_PUBKEY_get0_param(ASN1_OBJECT **out_obj,
1675 const uint8_t **out_key,
1676 int *out_key_len,
1677 X509_ALGOR **out_alg,
1678 X509_PUBKEY *pub);
1679
1680 // X509_PUBKEY_get0_public_key returns |pub|'s encoded public key.
1681 OPENSSL_EXPORT const ASN1_BIT_STRING *X509_PUBKEY_get0_public_key(
1682 const X509_PUBKEY *pub);
1683
1684
1685 // Extensions.
1686 //
1687 // X.509 certificates and CRLs may contain a list of extensions (RFC 5280).
1688 // Extensions have a type, specified by an object identifier (|ASN1_OBJECT|) and
1689 // a byte string value, which should a DER-encoded structure whose type is
1690 // determined by the extension type. This library represents extensions with the
1691 // |X509_EXTENSION| type.
1692
1693 // X509_EXTENSION is an |ASN1_ITEM| whose ASN.1 type is X.509 Extension (RFC
1694 // 5280) and C type is |X509_EXTENSION*|.
1695 DECLARE_ASN1_ITEM(X509_EXTENSION)
1696
1697 // X509_EXTENSION_new returns a newly-allocated, empty |X509_EXTENSION| object
1698 // or NULL on error.
1699 OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_new(void);
1700
1701 // X509_EXTENSION_free releases memory associated with |ex|.
1702 OPENSSL_EXPORT void X509_EXTENSION_free(X509_EXTENSION *ex);
1703
1704 // d2i_X509_EXTENSION parses up to |len| bytes from |*inp| as a DER-encoded
1705 // X.509 Extension (RFC 5280), as described in |d2i_SAMPLE_with_reuse|.
1706 OPENSSL_EXPORT X509_EXTENSION *d2i_X509_EXTENSION(X509_EXTENSION **out,
1707 const uint8_t **inp,
1708 long len);
1709
1710 // i2d_X509_EXTENSION marshals |ex| as a DER-encoded X.509 Extension (RFC
1711 // 5280), as described in |i2d_SAMPLE|.
1712 OPENSSL_EXPORT int i2d_X509_EXTENSION(const X509_EXTENSION *ex, uint8_t **outp);
1713
1714 // X509_EXTENSION_dup returns a newly-allocated copy of |ex|, or NULL on error.
1715 // This function works by serializing the structure, so if |ex| is incomplete,
1716 // it may fail.
1717 OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_dup(const X509_EXTENSION *ex);
1718
1719 // X509_EXTENSION_create_by_NID creates a new |X509_EXTENSION| with type |nid|,
1720 // value |data|, and critical bit |crit|. It returns an |X509_EXTENSION| on
1721 // success, and NULL on error. |nid| should be a |NID_*| constant.
1722 //
1723 // If |ex| and |*ex| are both non-NULL, |*ex| is used to hold the result,
1724 // otherwise a new object is allocated. If |ex| is non-NULL and |*ex| is NULL,
1725 // the function sets |*ex| to point to the newly allocated result, in addition
1726 // to returning the result.
1727 OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_create_by_NID(
1728 X509_EXTENSION **ex, int nid, int crit, const ASN1_OCTET_STRING *data);
1729
1730 // X509_EXTENSION_create_by_OBJ behaves like |X509_EXTENSION_create_by_NID|, but
1731 // the extension type is determined by an |ASN1_OBJECT|.
1732 OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_create_by_OBJ(
1733 X509_EXTENSION **ex, const ASN1_OBJECT *obj, int crit,
1734 const ASN1_OCTET_STRING *data);
1735
1736 // X509_EXTENSION_get_object returns |ex|'s extension type. This function
1737 // returns a non-const pointer for OpenSSL compatibility, but callers should not
1738 // mutate the result.
1739 OPENSSL_EXPORT ASN1_OBJECT *X509_EXTENSION_get_object(const X509_EXTENSION *ex);
1740
1741 // X509_EXTENSION_get_data returns |ne|'s extension value. This function returns
1742 // a non-const pointer for OpenSSL compatibility, but callers should not mutate
1743 // the result.
1744 OPENSSL_EXPORT ASN1_OCTET_STRING *X509_EXTENSION_get_data(
1745 const X509_EXTENSION *ne);
1746
1747 // X509_EXTENSION_get_critical returns one if |ex| is critical and zero
1748 // otherwise.
1749 OPENSSL_EXPORT int X509_EXTENSION_get_critical(const X509_EXTENSION *ex);
1750
1751 // X509_EXTENSION_set_object sets |ex|'s extension type to |obj|. It returns one
1752 // on success and zero on error.
1753 OPENSSL_EXPORT int X509_EXTENSION_set_object(X509_EXTENSION *ex,
1754 const ASN1_OBJECT *obj);
1755
1756 // X509_EXTENSION_set_critical sets |ex| to critical if |crit| is non-zero and
1757 // to non-critical if |crit| is zero.
1758 OPENSSL_EXPORT int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit);
1759
1760 // X509_EXTENSION_set_data set's |ex|'s extension value to a copy of |data|. It
1761 // returns one on success and zero on error.
1762 OPENSSL_EXPORT int X509_EXTENSION_set_data(X509_EXTENSION *ex,
1763 const ASN1_OCTET_STRING *data);
1764
1765
1766 // Extension lists.
1767 //
1768 // The following functions manipulate lists of extensions. Most of them have
1769 // corresponding functions on the containing |X509|, |X509_CRL|, or
1770 // |X509_REVOKED|.
1771
1772 DEFINE_STACK_OF(X509_EXTENSION)
1773 typedef STACK_OF(X509_EXTENSION) X509_EXTENSIONS;
1774
1775 // X509_EXTENSIONS is an |ASN1_ITEM| whose ASN.1 type is SEQUENCE of Extension
1776 // (RFC 5280) and C type is |STACK_OF(X509_EXTENSION)*|.
1777 DECLARE_ASN1_ITEM(X509_EXTENSIONS)
1778
1779 // d2i_X509_EXTENSIONS parses up to |len| bytes from |*inp| as a DER-encoded
1780 // SEQUENCE OF Extension (RFC 5280), as described in |d2i_SAMPLE_with_reuse|.
1781 OPENSSL_EXPORT X509_EXTENSIONS *d2i_X509_EXTENSIONS(X509_EXTENSIONS **out,
1782 const uint8_t **inp,
1783 long len);
1784
1785 // i2d_X509_EXTENSIONS marshals |alg| as a DER-encoded SEQUENCE OF Extension
1786 // (RFC 5280), as described in |i2d_SAMPLE|.
1787 OPENSSL_EXPORT int i2d_X509_EXTENSIONS(const X509_EXTENSIONS *alg,
1788 uint8_t **outp);
1789
1790 // X509v3_get_ext_count returns the number of extensions in |x|.
1791 OPENSSL_EXPORT int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x);
1792
1793 // X509v3_get_ext_by_NID returns the index of the first extension in |x| with
1794 // type |nid|, or a negative number if not found. If found, callers can use
1795 // |X509v3_get_ext| to look up the extension by index.
1796 //
1797 // If |lastpos| is non-negative, it begins searching at |lastpos| + 1. Callers
1798 // can thus loop over all matching extensions by first passing -1 and then
1799 // passing the previously-returned value until no match is returned.
1800 OPENSSL_EXPORT int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x,
1801 int nid, int lastpos);
1802
1803 // X509v3_get_ext_by_OBJ behaves like |X509v3_get_ext_by_NID| but looks for
1804 // extensions matching |obj|.
1805 OPENSSL_EXPORT int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *x,
1806 const ASN1_OBJECT *obj, int lastpos);
1807
1808 // X509v3_get_ext_by_critical returns the index of the first extension in |x|
1809 // whose critical bit matches |crit|, or a negative number if no such extension
1810 // was found.
1811 //
1812 // If |lastpos| is non-negative, it begins searching at |lastpos| + 1. Callers
1813 // can thus loop over all matching extensions by first passing -1 and then
1814 // passing the previously-returned value until no match is returned.
1815 OPENSSL_EXPORT int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *x,
1816 int crit, int lastpos);
1817
1818 // X509v3_get_ext returns the extension in |x| at index |loc|, or NULL if |loc|
1819 // is out of bounds. This function returns a non-const pointer for OpenSSL
1820 // compatibility, but callers should not mutate the result.
1821 OPENSSL_EXPORT X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x,
1822 int loc);
1823
1824 // X509v3_delete_ext removes the extension in |x| at index |loc| and returns the
1825 // removed extension, or NULL if |loc| was out of bounds. If an extension was
1826 // returned, the caller must release it with |X509_EXTENSION_free|.
1827 OPENSSL_EXPORT X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x,
1828 int loc);
1829
1830 // X509v3_add_ext adds a copy of |ex| to the extension list in |*x|. If |*x| is
1831 // NULL, it allocates a new |STACK_OF(X509_EXTENSION)| to hold the copy and sets
1832 // |*x| to the new list. It returns |*x| on success and NULL on error. The
1833 // caller retains ownership of |ex| and can release it independently of |*x|.
1834 //
1835 // The new extension is inserted at index |loc|, shifting extensions to the
1836 // right. If |loc| is -1 or out of bounds, the new extension is appended to the
1837 // list.
1838 OPENSSL_EXPORT STACK_OF(X509_EXTENSION) *X509v3_add_ext(
1839 STACK_OF(X509_EXTENSION) **x, const X509_EXTENSION *ex, int loc);
1840
1841
1842 // Built-in extensions.
1843 //
1844 // Several functions in the library encode and decode extension values into a
1845 // C structure to that extension. The following extensions are supported:
1846 //
1847 // - |NID_authority_key_identifier| with type |AUTHORITY_KEYID|
1848 // - |NID_basic_constraints| with type |BASIC_CONSTRAINTS|
1849 // - |NID_certificate_issuer| with type |GENERAL_NAMES|
1850 // - |NID_certificate_policies| with type |CERTIFICATEPOLICIES|
1851 // - |NID_crl_distribution_points| with type |CRL_DIST_POINTS|
1852 // - |NID_crl_number| with type |ASN1_INTEGER|
1853 // - |NID_crl_reason| with type |ASN1_ENUMERATED|
1854 // - |NID_delta_crl| with type |ASN1_INTEGER|
1855 // - |NID_ext_key_usage| with type |EXTENDED_KEY_USAGE|
1856 // - |NID_freshest_crl| with type |ISSUING_DIST_POINT|
1857 // - |NID_id_pkix_OCSP_noCheck| with type |ASN1_NULL|
1858 // - |NID_info_access| with type |AUTHORITY_INFO_ACCESS|
1859 // - |NID_inhibit_any_policy| with type |ASN1_INTEGER|
1860 // - |NID_invalidity_date| with type |ASN1_GENERALIZEDTIME|
1861 // - |NID_issuer_alt_name| with type |GENERAL_NAMES|
1862 // - |NID_issuing_distribution_point| with type |ISSUING_DIST_POINT|
1863 // - |NID_key_usage| with type |ASN1_BIT_STRING|
1864 // - |NID_name_constraints| with type |NAME_CONSTRAINTS|
1865 // - |NID_netscape_base_url| with type |ASN1_IA5STRING|
1866 // - |NID_netscape_ca_policy_url| with type |ASN1_IA5STRING|
1867 // - |NID_netscape_ca_revocation_url| with type |ASN1_IA5STRING|
1868 // - |NID_netscape_cert_type| with type |ASN1_BIT_STRING|
1869 // - |NID_netscape_comment| with type |ASN1_IA5STRING|
1870 // - |NID_netscape_renewal_url| with type |ASN1_IA5STRING|
1871 // - |NID_netscape_revocation_url| with type |ASN1_IA5STRING|
1872 // - |NID_netscape_ssl_server_name| with type |ASN1_IA5STRING|
1873 // - |NID_policy_constraints| with type |POLICY_CONSTRAINTS|
1874 // - |NID_policy_mappings| with type |POLICY_MAPPINGS|
1875 // - |NID_sinfo_access| with type |AUTHORITY_INFO_ACCESS|
1876 // - |NID_subject_alt_name| with type |GENERAL_NAMES|
1877 // - |NID_subject_key_identifier| with type |ASN1_OCTET_STRING|
1878 //
1879 // If an extension does not appear in this list, e.g. for a custom extension,
1880 // callers can instead use functions such as |X509_get_ext_by_OBJ|,
1881 // |X509_EXTENSION_get_data|, and |X509_EXTENSION_create_by_OBJ| to inspect or
1882 // create extensions directly. Although the |X509V3_EXT_METHOD| mechanism allows
1883 // registering custom extensions, doing so is deprecated and may result in
1884 // threading or memory errors.
1885
1886 // X509V3_EXT_d2i decodes |ext| and returns a pointer to a newly-allocated
1887 // structure, with type dependent on the type of the extension. It returns NULL
1888 // if |ext| is an unsupported extension or if there was a syntax error in the
1889 // extension. The caller should cast the return value to the expected type and
1890 // free the structure when done.
1891 //
1892 // WARNING: Casting the return value to the wrong type is a potentially
1893 // exploitable memory error, so callers must not use this function before
1894 // checking |ext| is of a known type. See the list at the top of this section
1895 // for the correct types.
1896 OPENSSL_EXPORT void *X509V3_EXT_d2i(const X509_EXTENSION *ext);
1897
1898 // X509V3_get_d2i finds and decodes the extension in |extensions| of type |nid|.
1899 // If found, it decodes it and returns a newly-allocated structure, with type
1900 // dependent on |nid|. If the extension is not found or on error, it returns
1901 // NULL. The caller may distinguish these cases using the |out_critical| value.
1902 //
1903 // If |out_critical| is not NULL, this function sets |*out_critical| to one if
1904 // the extension is found and critical, zero if it is found and not critical, -1
1905 // if it is not found, and -2 if there is an invalid duplicate extension. Note
1906 // this function may set |*out_critical| to one or zero and still return NULL if
1907 // the extension is found but has a syntax error.
1908 //
1909 // If |out_idx| is not NULL, this function looks for the first occurrence of the
1910 // extension after |*out_idx|. It then sets |*out_idx| to the index of the
1911 // extension, or -1 if not found. If |out_idx| is non-NULL, duplicate extensions
1912 // are not treated as an error. Callers, however, should not rely on this
1913 // behavior as it may be removed in the future. Duplicate extensions are
1914 // forbidden in RFC 5280.
1915 //
1916 // WARNING: This function is difficult to use correctly. Callers should pass a
1917 // non-NULL |out_critical| and check both the return value and |*out_critical|
1918 // to handle errors. If the return value is NULL and |*out_critical| is not -1,
1919 // there was an error. Otherwise, the function succeeded and but may return NULL
1920 // for a missing extension. Callers should pass NULL to |out_idx| so that
1921 // duplicate extensions are handled correctly.
1922 //
1923 // Additionally, casting the return value to the wrong type is a potentially
1924 // exploitable memory error, so callers must ensure the cast and |nid| match.
1925 // See the list at the top of this section for the correct types.
1926 OPENSSL_EXPORT void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *extensions,
1927 int nid, int *out_critical, int *out_idx);
1928
1929 // X509V3_EXT_free casts |ext_data| into the type that corresponds to |nid| and
1930 // releases memory associated with it. It returns one on success and zero if
1931 // |nid| is not a known extension.
1932 //
1933 // WARNING: Casting |ext_data| to the wrong type is a potentially exploitable
1934 // memory error, so callers must ensure |ext_data|'s type matches |nid|. See the
1935 // list at the top of this section for the correct types.
1936 //
1937 // TODO(davidben): OpenSSL upstream no longer exposes this function. Remove it?
1938 OPENSSL_EXPORT int X509V3_EXT_free(int nid, void *ext_data);
1939
1940 // X509V3_EXT_i2d casts |ext_struc| into the type that corresponds to
1941 // |ext_nid|, serializes it, and returns a newly-allocated |X509_EXTENSION|
1942 // object containing the serialization, or NULL on error. The |X509_EXTENSION|
1943 // has OID |ext_nid| and is critical if |crit| is one.
1944 //
1945 // WARNING: Casting |ext_struc| to the wrong type is a potentially exploitable
1946 // memory error, so callers must ensure |ext_struct|'s type matches |ext_nid|.
1947 // See the list at the top of this section for the correct types.
1948 OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit,
1949 void *ext_struc);
1950
1951 // The following constants control the behavior of |X509V3_add1_i2d| and related
1952 // functions.
1953
1954 // X509V3_ADD_OP_MASK can be ANDed with the flags to determine how duplicate
1955 // extensions are processed.
1956 #define X509V3_ADD_OP_MASK 0xfL
1957
1958 // X509V3_ADD_DEFAULT causes the function to fail if the extension was already
1959 // present.
1960 #define X509V3_ADD_DEFAULT 0L
1961
1962 // X509V3_ADD_APPEND causes the function to unconditionally appended the new
1963 // extension to to the extensions list, even if there is a duplicate.
1964 #define X509V3_ADD_APPEND 1L
1965
1966 // X509V3_ADD_REPLACE causes the function to replace the existing extension, or
1967 // append if it is not present.
1968 #define X509V3_ADD_REPLACE 2L
1969
1970 // X509V3_ADD_REPLACE_EXISTING causes the function to replace the existing
1971 // extension and fail if it is not present.
1972 #define X509V3_ADD_REPLACE_EXISTING 3L
1973
1974 // X509V3_ADD_KEEP_EXISTING causes the function to succeed without replacing the
1975 // extension if already present.
1976 #define X509V3_ADD_KEEP_EXISTING 4L
1977
1978 // X509V3_ADD_DELETE causes the function to remove the matching extension. No
1979 // new extension is added. If there is no matching extension, the function
1980 // fails. The |value| parameter is ignored in this mode.
1981 #define X509V3_ADD_DELETE 5L
1982
1983 // X509V3_ADD_SILENT may be ORed into one of the values above to indicate the
1984 // function should not add to the error queue on duplicate or missing extension.
1985 // The function will continue to return zero in those cases, and it will
1986 // continue to return -1 and add to the error queue on other errors.
1987 #define X509V3_ADD_SILENT 0x10
1988
1989 // X509V3_add1_i2d casts |value| to the type that corresponds to |nid|,
1990 // serializes it, and appends it to the extension list in |*x|. If |*x| is NULL,
1991 // it will set |*x| to a newly-allocated |STACK_OF(X509_EXTENSION)| as needed.
1992 // The |crit| parameter determines whether the new extension is critical.
1993 // |flags| may be some combination of the |X509V3_ADD_*| constants to control
1994 // the function's behavior on duplicate extension.
1995 //
1996 // This function returns one on success, zero if the operation failed due to a
1997 // missing or duplicate extension, and -1 on other errors.
1998 //
1999 // WARNING: Casting |value| to the wrong type is a potentially exploitable
2000 // memory error, so callers must ensure |value|'s type matches |nid|. See the
2001 // list at the top of this section for the correct types.
2002 OPENSSL_EXPORT int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid,
2003 void *value, int crit, unsigned long flags);
2004
2005
2006 // General names.
2007 //
2008 // A |GENERAL_NAME| represents an X.509 GeneralName structure, defined in RFC
2009 // 5280, Section 4.2.1.6. General names are distinct from names (|X509_NAME|). A
2010 // general name is a CHOICE type which may contain one of several name types,
2011 // most commonly a DNS name or an IP address. General names most commonly appear
2012 // in the subject alternative name (SAN) extension, though they are also used in
2013 // other extensions.
2014 //
2015 // Many extensions contain a SEQUENCE OF GeneralName, or GeneralNames, so
2016 // |STACK_OF(GENERAL_NAME)| is defined and aliased to |GENERAL_NAMES|.
2017
2018 typedef struct otherName_st {
2019 ASN1_OBJECT *type_id;
2020 ASN1_TYPE *value;
2021 } OTHERNAME;
2022
2023 typedef struct EDIPartyName_st {
2024 ASN1_STRING *nameAssigner;
2025 ASN1_STRING *partyName;
2026 } EDIPARTYNAME;
2027
2028 // GEN_* are constants for the |type| field of |GENERAL_NAME|, defined below.
2029 #define GEN_OTHERNAME 0
2030 #define GEN_EMAIL 1
2031 #define GEN_DNS 2
2032 #define GEN_X400 3
2033 #define GEN_DIRNAME 4
2034 #define GEN_EDIPARTY 5
2035 #define GEN_URI 6
2036 #define GEN_IPADD 7
2037 #define GEN_RID 8
2038
2039 // A GENERAL_NAME_st, aka |GENERAL_NAME|, represents an X.509 GeneralName. The
2040 // |type| field determines which member of |d| is active. A |GENERAL_NAME| may
2041 // also be empty, in which case |type| is -1 and |d| is NULL. Empty
2042 // |GENERAL_NAME|s are invalid and will never be returned from the parser, but
2043 // may be created temporarily, e.g. by |GENERAL_NAME_new|.
2044 struct GENERAL_NAME_st {
2045 int type;
2046 union {
2047 char *ptr;
2048 OTHERNAME *otherName;
2049 ASN1_IA5STRING *rfc822Name;
2050 ASN1_IA5STRING *dNSName;
2051 ASN1_STRING *x400Address;
2052 X509_NAME *directoryName;
2053 EDIPARTYNAME *ediPartyName;
2054 ASN1_IA5STRING *uniformResourceIdentifier;
2055 ASN1_OCTET_STRING *iPAddress;
2056 ASN1_OBJECT *registeredID;
2057
2058 // Old names
2059 ASN1_OCTET_STRING *ip; // iPAddress
2060 X509_NAME *dirn; // dirn
2061 ASN1_IA5STRING *ia5; // rfc822Name, dNSName, uniformResourceIdentifier
2062 ASN1_OBJECT *rid; // registeredID
2063 } d;
2064 } /* GENERAL_NAME */;
2065
2066 // GENERAL_NAME_new returns a new, empty |GENERAL_NAME|, or NULL on error.
2067 OPENSSL_EXPORT GENERAL_NAME *GENERAL_NAME_new(void);
2068
2069 // GENERAL_NAME_free releases memory associated with |gen|.
2070 OPENSSL_EXPORT void GENERAL_NAME_free(GENERAL_NAME *gen);
2071
2072 // d2i_GENERAL_NAME parses up to |len| bytes from |*inp| as a DER-encoded X.509
2073 // GeneralName (RFC 5280), as described in |d2i_SAMPLE|.
2074 OPENSSL_EXPORT GENERAL_NAME *d2i_GENERAL_NAME(GENERAL_NAME **out,
2075 const uint8_t **inp, long len);
2076
2077 // i2d_GENERAL_NAME marshals |in| as a DER-encoded X.509 GeneralName (RFC 5280),
2078 // as described in |i2d_SAMPLE|.
2079 //
2080 // TODO(https://crbug.com/boringssl/407): This function should be const and
2081 // thread-safe but is currently neither in some cases, notably if |in| is an
2082 // directoryName and the |X509_NAME| has been modified.
2083 OPENSSL_EXPORT int i2d_GENERAL_NAME(GENERAL_NAME *in, uint8_t **outp);
2084
2085 // GENERAL_NAME_dup returns a newly-allocated copy of |gen|, or NULL on error.
2086 // This function works by serializing the structure, so it will fail if |gen| is
2087 // empty.
2088 //
2089 // TODO(https://crbug.com/boringssl/407): This function should be const and
2090 // thread-safe but is currently neither in some cases, notably if |gen| is an
2091 // directoryName and the |X509_NAME| has been modified.
2092 OPENSSL_EXPORT GENERAL_NAME *GENERAL_NAME_dup(GENERAL_NAME *gen);
2093
2094 // GENERAL_NAMES_new returns a new, empty |GENERAL_NAMES|, or NULL on error.
2095 OPENSSL_EXPORT GENERAL_NAMES *GENERAL_NAMES_new(void);
2096
2097 // GENERAL_NAMES_free releases memory associated with |gens|.
2098 OPENSSL_EXPORT void GENERAL_NAMES_free(GENERAL_NAMES *gens);
2099
2100 // d2i_GENERAL_NAMES parses up to |len| bytes from |*inp| as a DER-encoded
2101 // SEQUENCE OF GeneralName, as described in |d2i_SAMPLE|.
2102 OPENSSL_EXPORT GENERAL_NAMES *d2i_GENERAL_NAMES(GENERAL_NAMES **out,
2103 const uint8_t **inp, long len);
2104
2105 // i2d_GENERAL_NAMES marshals |in| as a DER-encoded SEQUENCE OF GeneralName, as
2106 // described in |i2d_SAMPLE|.
2107 //
2108 // TODO(https://crbug.com/boringssl/407): This function should be const and
2109 // thread-safe but is currently neither in some cases, notably if some element
2110 // of |in| is an directoryName and the |X509_NAME| has been modified.
2111 OPENSSL_EXPORT int i2d_GENERAL_NAMES(GENERAL_NAMES *in, uint8_t **outp);
2112
2113 // OTHERNAME_new returns a new, empty |OTHERNAME|, or NULL on error.
2114 OPENSSL_EXPORT OTHERNAME *OTHERNAME_new(void);
2115
2116 // OTHERNAME_free releases memory associated with |name|.
2117 OPENSSL_EXPORT void OTHERNAME_free(OTHERNAME *name);
2118
2119 // EDIPARTYNAME_new returns a new, empty |EDIPARTYNAME|, or NULL on error.
2120 // EDIPartyName is rarely used in practice, so callers are unlikely to need this
2121 // function.
2122 OPENSSL_EXPORT EDIPARTYNAME *EDIPARTYNAME_new(void);
2123
2124 // EDIPARTYNAME_free releases memory associated with |name|. EDIPartyName is
2125 // rarely used in practice, so callers are unlikely to need this function.
2126 OPENSSL_EXPORT void EDIPARTYNAME_free(EDIPARTYNAME *name);
2127
2128 // GENERAL_NAME_set0_value set |gen|'s type and value to |type| and |value|.
2129 // |type| must be a |GEN_*| constant and |value| must be an object of the
2130 // corresponding type. |gen| takes ownership of |value|, so |value| must have
2131 // been an allocated object.
2132 //
2133 // WARNING: |gen| must be empty (typically as returned from |GENERAL_NAME_new|)
2134 // before calling this function. If |gen| already contained a value, the
2135 // previous contents will be leaked.
2136 OPENSSL_EXPORT void GENERAL_NAME_set0_value(GENERAL_NAME *gen, int type,
2137 void *value);
2138
2139 // GENERAL_NAME_get0_value returns the in-memory representation of |gen|'s
2140 // contents and, |out_type| is not NULL, sets |*out_type| to the type of |gen|,
2141 // which will be a |GEN_*| constant. If |gen| is incomplete, the return value
2142 // will be NULL and the type will be -1.
2143 //
2144 // WARNING: Casting the result of this function to the wrong type is a
2145 // potentially exploitable memory error. Callers must check |gen|'s type, either
2146 // via |*out_type| or checking |gen->type| directly, before inspecting the
2147 // result.
2148 //
2149 // WARNING: This function is not const-correct. The return value should be
2150 // const. Callers shoudl not mutate the returned object.
2151 OPENSSL_EXPORT void *GENERAL_NAME_get0_value(const GENERAL_NAME *gen,
2152 int *out_type);
2153
2154 // GENERAL_NAME_set0_othername sets |gen| to be an OtherName with type |oid| and
2155 // value |value|. On success, it returns one and takes ownership of |oid| and
2156 // |value|, which must be created in a way compatible with |ASN1_OBJECT_free|
2157 // and |ASN1_TYPE_free|, respectively. On allocation failure, it returns zero.
2158 // In the failure case, the caller retains ownership of |oid| and |value| and
2159 // must release them when done.
2160 //
2161 // WARNING: |gen| must be empty (typically as returned from |GENERAL_NAME_new|)
2162 // before calling this function. If |gen| already contained a value, the
2163 // previously contents will be leaked.
2164 OPENSSL_EXPORT int GENERAL_NAME_set0_othername(GENERAL_NAME *gen,
2165 ASN1_OBJECT *oid,
2166 ASN1_TYPE *value);
2167
2168 // GENERAL_NAME_get0_otherName, if |gen| is an OtherName, sets |*out_oid| and
2169 // |*out_value| to the OtherName's type-id and value, respectively, and returns
2170 // one. If |gen| is not an OtherName, it returns zero and leaves |*out_oid| and
2171 // |*out_value| unmodified. Either of |out_oid| or |out_value| may be NULL to
2172 // ignore the value.
2173 //
2174 // WARNING: This function is not const-correct. |out_oid| and |out_value| are
2175 // not const, but callers should not mutate the resulting objects.
2176 OPENSSL_EXPORT int GENERAL_NAME_get0_otherName(const GENERAL_NAME *gen,
2177 ASN1_OBJECT **out_oid,
2178 ASN1_TYPE **out_value);
2179
2180
2181 // Algorithm identifiers.
2182 //
2183 // An |X509_ALGOR| represents an AlgorithmIdentifier structure, used in X.509
2184 // to represent signature algorithms and public key algorithms.
2185
2186 DEFINE_STACK_OF(X509_ALGOR)
2187
2188 // X509_ALGOR is an |ASN1_ITEM| whose ASN.1 type is AlgorithmIdentifier and C
2189 // type is |X509_ALGOR*|.
2190 DECLARE_ASN1_ITEM(X509_ALGOR)
2191
2192 // X509_ALGOR_new returns a newly-allocated, empty |X509_ALGOR| object, or NULL
2193 // on error.
2194 OPENSSL_EXPORT X509_ALGOR *X509_ALGOR_new(void);
2195
2196 // X509_ALGOR_dup returns a newly-allocated copy of |alg|, or NULL on error.
2197 // This function works by serializing the structure, so if |alg| is incomplete,
2198 // it may fail.
2199 OPENSSL_EXPORT X509_ALGOR *X509_ALGOR_dup(const X509_ALGOR *alg);
2200
2201 // X509_ALGOR_free releases memory associated with |alg|.
2202 OPENSSL_EXPORT void X509_ALGOR_free(X509_ALGOR *alg);
2203
2204 // d2i_X509_ALGOR parses up to |len| bytes from |*inp| as a DER-encoded
2205 // AlgorithmIdentifier, as described in |d2i_SAMPLE_with_reuse|.
2206 OPENSSL_EXPORT X509_ALGOR *d2i_X509_ALGOR(X509_ALGOR **out, const uint8_t **inp,
2207 long len);
2208
2209 // i2d_X509_ALGOR marshals |alg| as a DER-encoded AlgorithmIdentifier, as
2210 // described in |i2d_SAMPLE|.
2211 OPENSSL_EXPORT int i2d_X509_ALGOR(const X509_ALGOR *alg, uint8_t **outp);
2212
2213 // X509_ALGOR_set0 sets |alg| to an AlgorithmIdentifier with algorithm |obj| and
2214 // parameter determined by |param_type| and |param_value|. It returns one on
2215 // success and zero on error. This function takes ownership of |obj| and
2216 // |param_value| on success.
2217 //
2218 // If |param_type| is |V_ASN1_UNDEF|, the parameter is omitted. If |param_type|
2219 // is zero, the parameter is left unchanged. Otherwise, |param_type| and
2220 // |param_value| are interpreted as in |ASN1_TYPE_set|.
2221 //
2222 // Note omitting the parameter (|V_ASN1_UNDEF|) and encoding an explicit NULL
2223 // value (|V_ASN1_NULL|) are different. Some algorithms require one and some the
2224 // other. Consult the relevant specification before calling this function. The
2225 // correct parameter for an RSASSA-PKCS1-v1_5 signature is |V_ASN1_NULL|. The
2226 // correct one for an ECDSA or Ed25519 signature is |V_ASN1_UNDEF|.
2227 OPENSSL_EXPORT int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *obj,
2228 int param_type, void *param_value);
2229
2230 // X509_ALGOR_get0 sets |*out_obj| to the |alg|'s algorithm. If |alg|'s
2231 // parameter is omitted, it sets |*out_param_type| and |*out_param_value| to
2232 // |V_ASN1_UNDEF| and NULL. Otherwise, it sets |*out_param_type| and
2233 // |*out_param_value| to the parameter, using the same representation as
2234 // |ASN1_TYPE_set0|. See |ASN1_TYPE_set0| and |ASN1_TYPE| for details.
2235 //
2236 // Callers that require the parameter in serialized form should, after checking
2237 // for |V_ASN1_UNDEF|, use |ASN1_TYPE_set1| and |d2i_ASN1_TYPE|, rather than
2238 // inspecting |*out_param_value|.
2239 //
2240 // Each of |out_obj|, |out_param_type|, and |out_param_value| may be NULL to
2241 // ignore the output. If |out_param_type| is NULL, |out_param_value| is ignored.
2242 //
2243 // WARNING: If |*out_param_type| is set to |V_ASN1_UNDEF|, OpenSSL and older
2244 // revisions of BoringSSL leave |*out_param_value| unset rather than setting it
2245 // to NULL. Callers that support both OpenSSL and BoringSSL should not assume
2246 // |*out_param_value| is uniformly initialized.
2247 OPENSSL_EXPORT void X509_ALGOR_get0(const ASN1_OBJECT **out_obj,
2248 int *out_param_type,
2249 const void **out_param_value,
2250 const X509_ALGOR *alg);
2251
2252 // X509_ALGOR_set_md sets |alg| to the hash function |md|. Note this
2253 // AlgorithmIdentifier represents the hash function itself, not a signature
2254 // algorithm that uses |md|. It returns one on success and zero on error.
2255 //
2256 // Due to historical specification mistakes (see Section 2.1 of RFC 4055), the
2257 // parameters field is sometimes omitted and sometimes a NULL value. When used
2258 // in RSASSA-PSS and RSAES-OAEP, it should be a NULL value. In other contexts,
2259 // the parameters should be omitted. This function assumes the caller is
2260 // constructing a RSASSA-PSS or RSAES-OAEP AlgorithmIdentifier and includes a
2261 // NULL parameter. This differs from OpenSSL's behavior.
2262 //
2263 // TODO(davidben): Rename this function, or perhaps just add a bespoke API for
2264 // constructing PSS and move on.
2265 OPENSSL_EXPORT int X509_ALGOR_set_md(X509_ALGOR *alg, const EVP_MD *md);
2266
2267 // X509_ALGOR_cmp returns zero if |a| and |b| are equal, and some non-zero value
2268 // otherwise. Note this function can only be used for equality checks, not an
2269 // ordering.
2270 OPENSSL_EXPORT int X509_ALGOR_cmp(const X509_ALGOR *a, const X509_ALGOR *b);
2271
2272
2273 // Attributes.
2274 //
2275 // Unlike certificates and CRLs, CSRs use a separate Attribute structure (RFC
2276 // 2985, RFC 2986) for extensibility. This is represented by the library as
2277 // |X509_ATTRIBUTE|.
2278
2279 DEFINE_STACK_OF(X509_ATTRIBUTE)
2280
2281 // X509_ATTRIBUTE is an |ASN1_ITEM| whose ASN.1 type is Attribute (RFC 2986) and
2282 // C type is |X509_ATTRIBUTE*|.
2283 DECLARE_ASN1_ITEM(X509_ATTRIBUTE)
2284
2285 // X509_ATTRIBUTE_new returns a newly-allocated, empty |X509_ATTRIBUTE| object,
2286 // or NULL on error. |X509_ATTRIBUTE_set1_*| may be used to finish initializing
2287 // it.
2288 OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_new(void);
2289
2290 // X509_ATTRIBUTE_dup returns a newly-allocated copy of |attr|, or NULL on
2291 // error. This function works by serializing the structure, so if |attr| is
2292 // incomplete, it may fail.
2293 OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_dup(const X509_ATTRIBUTE *attr);
2294
2295 // X509_ATTRIBUTE_free releases memory associated with |attr|.
2296 OPENSSL_EXPORT void X509_ATTRIBUTE_free(X509_ATTRIBUTE *attr);
2297
2298 // d2i_X509_ATTRIBUTE parses up to |len| bytes from |*inp| as a DER-encoded
2299 // Attribute (RFC 2986), as described in |d2i_SAMPLE|.
2300 OPENSSL_EXPORT X509_ATTRIBUTE *d2i_X509_ATTRIBUTE(X509_ATTRIBUTE **out,
2301 const uint8_t **inp,
2302 long len);
2303
2304 // i2d_X509_ATTRIBUTE marshals |alg| as a DER-encoded Attribute (RFC 2986), as
2305 // described in |i2d_SAMPLE|.
2306 OPENSSL_EXPORT int i2d_X509_ATTRIBUTE(const X509_ATTRIBUTE *alg,
2307 uint8_t **outp);
2308
2309 // X509_ATTRIBUTE_create returns a newly-allocated |X509_ATTRIBUTE|, or NULL on
2310 // error. The attribute has type |nid| and contains a single value determined by
2311 // |attrtype| and |value|, which are interpreted as in |ASN1_TYPE_set|. Note
2312 // this function takes ownership of |value|.
2313 OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int attrtype,
2314 void *value);
2315
2316 // X509_ATTRIBUTE_create_by_NID returns a newly-allocated |X509_ATTRIBUTE| of
2317 // type |nid|, or NULL on error. The value is determined as in
2318 // |X509_ATTRIBUTE_set1_data|.
2319 //
2320 // If |attr| is non-NULL, the resulting |X509_ATTRIBUTE| is also written to
2321 // |*attr|. If |*attr| was non-NULL when the function was called, |*attr| is
2322 // reused instead of creating a new object.
2323 //
2324 // WARNING: The interpretation of |attrtype|, |data|, and |len| is complex and
2325 // error-prone. See |X509_ATTRIBUTE_set1_data| for details.
2326 //
2327 // WARNING: The object reuse form is deprecated and may be removed in the
2328 // future. It also currently incorrectly appends to the reused object's value
2329 // set rather than overwriting it.
2330 OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(
2331 X509_ATTRIBUTE **attr, int nid, int attrtype, const void *data, int len);
2332
2333 // X509_ATTRIBUTE_create_by_OBJ behaves like |X509_ATTRIBUTE_create_by_NID|
2334 // except the attribute's type is determined by |obj|.
2335 OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(
2336 X509_ATTRIBUTE **attr, const ASN1_OBJECT *obj, int attrtype,
2337 const void *data, int len);
2338
2339 // X509_ATTRIBUTE_create_by_txt behaves like |X509_ATTRIBUTE_create_by_NID|
2340 // except the attribute's type is determined by calling |OBJ_txt2obj| with
2341 // |attrname|.
2342 OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(
2343 X509_ATTRIBUTE **attr, const char *attrname, int type,
2344 const unsigned char *bytes, int len);
2345
2346 // X509_ATTRIBUTE_set1_object sets |attr|'s type to |obj|. It returns one on
2347 // success and zero on error.
2348 OPENSSL_EXPORT int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr,
2349 const ASN1_OBJECT *obj);
2350
2351 // X509_ATTRIBUTE_set1_data appends a value to |attr|'s value set and returns
2352 // one on success or zero on error. The value is determined as follows:
2353 //
2354 // If |attrtype| is zero, this function returns one and does nothing. This form
2355 // may be used when calling |X509_ATTRIBUTE_create_by_*| to create an attribute
2356 // with an empty value set. Such attributes are invalid, but OpenSSL supports
2357 // creating them.
2358 //
2359 // Otherwise, if |attrtype| is a |MBSTRING_*| constant, the value is an ASN.1
2360 // string. The string is determined by decoding |len| bytes from |data| in the
2361 // encoding specified by |attrtype|, and then re-encoding it in a form
2362 // appropriate for |attr|'s type. If |len| is -1, |strlen(data)| is used
2363 // instead. See |ASN1_STRING_set_by_NID| for details.
2364 //
2365 // Otherwise, if |len| is not -1, the value is an ASN.1 string. |attrtype| is an
2366 // |ASN1_STRING| type value and the |len| bytes from |data| are copied as the
2367 // type-specific representation of |ASN1_STRING|. See |ASN1_STRING| for details.
2368 //
2369 // Otherwise, if |len| is -1, the value is constructed by passing |attrtype| and
2370 // |data| to |ASN1_TYPE_set1|. That is, |attrtype| is an |ASN1_TYPE| type value,
2371 // and |data| is cast to the corresponding pointer type.
2372 //
2373 // WARNING: Despite the name, this function appends to |attr|'s value set,
2374 // rather than overwriting it. To overwrite the value set, create a new
2375 // |X509_ATTRIBUTE| with |X509_ATTRIBUTE_new|.
2376 //
2377 // WARNING: If using the |MBSTRING_*| form, pass a length rather than relying on
2378 // |strlen|. In particular, |strlen| will not behave correctly if the input is
2379 // |MBSTRING_BMP| or |MBSTRING_UNIV|.
2380 //
2381 // WARNING: This function currently misinterprets |V_ASN1_OTHER| as an
2382 // |MBSTRING_*| constant. This matches OpenSSL but means it is impossible to
2383 // construct a value with a non-universal tag.
2384 OPENSSL_EXPORT int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,
2385 const void *data, int len);
2386
2387 // X509_ATTRIBUTE_get0_data returns the |idx|th value of |attr| in a
2388 // type-specific representation to |attrtype|, or NULL if out of bounds or the
2389 // type does not match. |attrtype| is one of the type values in |ASN1_TYPE|. On
2390 // match, the return value uses the same representation as |ASN1_TYPE_set0|. See
2391 // |ASN1_TYPE| for details.
2392 OPENSSL_EXPORT void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx,
2393 int attrtype, void *unused);
2394
2395 // X509_ATTRIBUTE_count returns the number of values in |attr| or 0 if |attr|
2396 // is NULL.
2397 OPENSSL_EXPORT int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr);
2398
2399 // X509_ATTRIBUTE_get0_object returns the type of |attr|.
2400 OPENSSL_EXPORT ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr);
2401
2402 // X509_ATTRIBUTE_get0_type returns the |idx|th value in |attr|, or NULL if out
2403 // of bounds. Note this function returns one of |attr|'s values, not the type.
2404 OPENSSL_EXPORT ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr,
2405 int idx);
2406
2407
2408 // Certificate stores.
2409 //
2410 // An |X509_STORE| contains trusted certificates, CRLs, and verification
2411 // parameters that are shared between multiple certificate verifications.
2412 //
2413 // Certificates in an |X509_STORE| are referred to as "trusted certificates",
2414 // but an individual certificate verification may not necessarily treat every
2415 // trusted certificate as a trust anchor. See |X509_VERIFY_PARAM_set_trust| for
2416 // details.
2417 //
2418 // WARNING: Although a trusted certificate which fails the
2419 // |X509_VERIFY_PARAM_set_trust| check is functionally an untrusted
2420 // intermediate certificate, callers should not rely on this to configure
2421 // untrusted intermediates in an |X509_STORE|. The trust check is complex, so
2422 // this risks inadvertently treating it as a trust anchor. Instead, configure
2423 // untrusted intermediates with the |chain| parameter of |X509_STORE_CTX_init|.
2424 //
2425 // Certificates in |X509_STORE| may be specified in several ways:
2426 // - Added by |X509_STORE_add_cert|.
2427 // - Returned by an |X509_LOOKUP| added by |X509_STORE_add_lookup|.
2428 //
2429 // |X509_STORE|s are reference-counted and may be shared by certificate
2430 // verifications running concurrently on multiple threads. However, an
2431 // |X509_STORE|'s verification parameters may not be modified concurrently with
2432 // certificate verification or other operations. Unless otherwise documented,
2433 // functions which take const pointer may be used concurrently, while
2434 // functions which take a non-const pointer may not. Callers that wish to modify
2435 // verification parameters in a shared |X509_STORE| should instead modify
2436 // |X509_STORE_CTX|s individually.
2437
2438 // X509_STORE_new returns a newly-allocated |X509_STORE|, or NULL on error.
2439 OPENSSL_EXPORT X509_STORE *X509_STORE_new(void);
2440
2441 // X509_STORE_up_ref adds one to the reference count of |store| and returns one.
2442 // Although |store| is not const, this function's use of |store| is thread-safe.
2443 OPENSSL_EXPORT int X509_STORE_up_ref(X509_STORE *store);
2444
2445 // X509_STORE_free releases memory associated with |store|.
2446 OPENSSL_EXPORT void X509_STORE_free(X509_STORE *store);
2447
2448 // X509_STORE_add_cert adds |x509| to |store| as a trusted certificate. It
2449 // returns one on success and zero on error. This function internally increments
2450 // |x509|'s reference count, so the caller retains ownership of |x509|.
2451 //
2452 // Certificates configured by this function are still subject to the checks
2453 // described in |X509_VERIFY_PARAM_set_trust|.
2454 //
2455 // Although |store| is not const, this function's use of |store| is thread-safe.
2456 // However, if this function is called concurrently with |X509_verify_cert|, it
2457 // is a race condition whether |x509| is available for issuer lookups.
2458 // Moreover, the result may differ for each issuer lookup performed by a single
2459 // |X509_verify_cert| call.
2460 OPENSSL_EXPORT int X509_STORE_add_cert(X509_STORE *store, X509 *x509);
2461
2462 // X509_STORE_add_crl adds |crl| to |store|. It returns one on success and zero
2463 // on error. This function internally increments |crl|'s reference count, so the
2464 // caller retains ownership of |crl|. CRLs added in this way are candidates for
2465 // CRL lookup when |X509_V_FLAG_CRL_CHECK| is set.
2466 //
2467 // Although |store| is not const, this function's use of |store| is thread-safe.
2468 // However, if this function is called concurrently with |X509_verify_cert|, it
2469 // is a race condition whether |crl| is available for CRL checks. Moreover, the
2470 // result may differ for each CRL check performed by a single
2471 // |X509_verify_cert| call.
2472 //
2473 // Note there are no supported APIs to remove CRLs from |store| once inserted.
2474 // To vary the set of CRLs over time, callers should either create a new
2475 // |X509_STORE| or configure CRLs on a per-verification basis with
2476 // |X509_STORE_CTX_set0_crls|.
2477 OPENSSL_EXPORT int X509_STORE_add_crl(X509_STORE *store, X509_CRL *crl);
2478
2479 // X509_STORE_get0_param returns |store|'s verification parameters. This object
2480 // is mutable and may be modified by the caller. For an individual certificate
2481 // verification operation, |X509_STORE_CTX_init| initializes the
2482 // |X509_STORE_CTX|'s parameters with these parameters.
2483 //
2484 // WARNING: |X509_STORE_CTX_init| applies some default parameters (as in
2485 // |X509_VERIFY_PARAM_inherit|) after copying |store|'s parameters. This means
2486 // it is impossible to leave some parameters unset at |store|. They must be
2487 // explicitly unset after creating the |X509_STORE_CTX|.
2488 //
2489 // As of writing these late defaults are a depth limit (see
2490 // |X509_VERIFY_PARAM_set_depth|) and the |X509_V_FLAG_TRUSTED_FIRST| flag. This
2491 // warning does not apply if the parameters were set in |store|.
2492 //
2493 // TODO(crbug.com/boringssl/441): This behavior is very surprising. Can we
2494 // remove this notion of late defaults? The unsettable value at |X509_STORE| is
2495 // -1, which rejects everything but explicitly-trusted self-signed certificates.
2496 // |X509_V_FLAG_TRUSTED_FIRST| is mostly a workaround for poor path-building.
2497 OPENSSL_EXPORT X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *store);
2498
2499 // X509_STORE_set1_param copies verification parameters from |param| as in
2500 // |X509_VERIFY_PARAM_set1|. It returns one on success and zero on error.
2501 OPENSSL_EXPORT int X509_STORE_set1_param(X509_STORE *store,
2502 const X509_VERIFY_PARAM *param);
2503
2504 // X509_STORE_set_flags enables all values in |flags| in |store|'s verification
2505 // flags. |flags| should be a combination of |X509_V_FLAG_*| constants.
2506 //
2507 // WARNING: These flags will be combined with default flags when copied to an
2508 // |X509_STORE_CTX|. This means it is impossible to unset those defaults from
2509 // the |X509_STORE|. See discussion in |X509_STORE_get0_param|.
2510 OPENSSL_EXPORT int X509_STORE_set_flags(X509_STORE *store, unsigned long flags);
2511
2512 // X509_STORE_set_depth configures |store| to, by default, limit certificate
2513 // chains to |depth| intermediate certificates. This count excludes both the
2514 // target certificate and the trust anchor (root certificate).
2515 OPENSSL_EXPORT int X509_STORE_set_depth(X509_STORE *store, int depth);
2516
2517 // X509_STORE_set_purpose configures the purpose check for |store|. See
2518 // |X509_VERIFY_PARAM_set_purpose| for details.
2519 OPENSSL_EXPORT int X509_STORE_set_purpose(X509_STORE *store, int purpose);
2520
2521 // X509_STORE_set_trust configures the trust check for |store|. See
2522 // |X509_VERIFY_PARAM_set_trust| for details.
2523 OPENSSL_EXPORT int X509_STORE_set_trust(X509_STORE *store, int trust);
2524
2525 // The following constants indicate the type of an |X509_OBJECT|.
2526 #define X509_LU_NONE 0
2527 #define X509_LU_X509 1
2528 #define X509_LU_CRL 2
2529 #define X509_LU_PKEY 3
2530
2531 DEFINE_STACK_OF(X509_OBJECT)
2532
2533 // X509_OBJECT_new returns a newly-allocated, empty |X509_OBJECT| or NULL on
2534 // error.
2535 OPENSSL_EXPORT X509_OBJECT *X509_OBJECT_new(void);
2536
2537 // X509_OBJECT_free releases memory associated with |obj|.
2538 OPENSSL_EXPORT void X509_OBJECT_free(X509_OBJECT *obj);
2539
2540 // X509_OBJECT_get_type returns the type of |obj|, which will be one of the
2541 // |X509_LU_*| constants.
2542 OPENSSL_EXPORT int X509_OBJECT_get_type(const X509_OBJECT *obj);
2543
2544 // X509_OBJECT_get0_X509 returns |obj| as a certificate, or NULL if |obj| is not
2545 // a certificate.
2546 OPENSSL_EXPORT X509 *X509_OBJECT_get0_X509(const X509_OBJECT *obj);
2547
2548 typedef STACK_OF(X509_CRL) *(*X509_STORE_CTX_lookup_crls_fn)(
2549 X509_STORE_CTX *ctx, X509_NAME *nm);
2550
2551 OPENSSL_EXPORT X509_STORE_CTX_lookup_crls_fn
2552 X509_STORE_get_lookup_crls(X509_STORE *ctx);
2553
2554 OPENSSL_EXPORT void X509_STORE_set_lookup_crls(
2555 X509_STORE *ctx, X509_STORE_CTX_lookup_crls_fn lookup_crls);
2556
2557 #define X509_STORE_set_lookup_crls_cb(ctx, func) \
2558 X509_STORE_set_lookup_crls((ctx), (func))
2559
2560 // Certificate verification.
2561 //
2562 // An |X509_STORE_CTX| object represents a single certificate verification
2563 // operation. To verify a certificate chain, callers construct an
2564 // |X509_STORE_CTX|, initialize it with |X509_STORE_CTX_init|, configure extra
2565 // parameters with |X509_STORE_CTX_get0_param|, and call |X509_verify_cert|.
2566
2567 // X509_STORE_CTX_new returns a newly-allocated, empty |X509_STORE_CTX|, or NULL
2568 // on error.
2569 OPENSSL_EXPORT X509_STORE_CTX *X509_STORE_CTX_new(void);
2570
2571 // X509_STORE_CTX_free releases memory associated with |ctx|.
2572 OPENSSL_EXPORT void X509_STORE_CTX_free(X509_STORE_CTX *ctx);
2573
2574 // X509_STORE_CTX_init initializes |ctx| to verify |x509|, using trusted
2575 // certificates and parameters in |store|. It returns one on success and zero on
2576 // error. |chain| is a list of untrusted intermediate certificates to use in
2577 // verification.
2578 //
2579 // |ctx| stores pointers to |store|, |x509|, and |chain|. Each of these objects
2580 // must outlive |ctx| and may not be mutated for the duration of the certificate
2581 // verification.
2582 OPENSSL_EXPORT int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store,
2583 X509 *x509, STACK_OF(X509) *chain);
2584
2585 // X509_verify_cert attempts to discover and validate a certificate chain based
2586 // on parameters in |ctx|. |ctx| usually includes a target certificate to be
2587 // verified, a set of certificates serving as trust anchors, a list of
2588 // non-trusted certificates that may be helpful for chain construction, flags,
2589 // and various other optional components such as callback functions. |ctx|
2590 // must have been initialized with |X509_STORE_CTX_init|. A certificate chain
2591 // is built up starting from the target certificate and ending
2592 // in a trust anchor. The chain is built up iteratively, looking up in turn a
2593 // certificate with suitable key usage that matches as an issuer of the current
2594 // "subject" certificate.
2595 //
2596 // NOTE:
2597 // 1. Applications rarely call this function directly, but it is used
2598 // internally for certificate validation.
2599 // 2. |X509_verify_cert| and other related functions call
2600 // |sk_X509_OBJECT_sort| internally, which rearranges the certificate
2601 // ordering. There will be cases where two certs have an identical
2602 // |subject| and |X509_OBJECT_cmp| will return 0, but one is a valid cert
2603 // and the other is invalid.
2604 // Due to https://github.com/openssl/openssl/issues/18708, certificate
2605 // verification could fail if an invalid cert is checked before the valid
2606 // cert. What we do with sorting behavior when certs are identical is
2607 // considered "unstable" and certain sorting expectations shouldn't be
2608 // depended on.
2609 OPENSSL_EXPORT int X509_verify_cert(X509_STORE_CTX *ctx);
2610
2611 // X509_STORE_CTX_get0_chain, after a successful |X509_verify_cert| call,
2612 // returns the verified certificate chain. The chain begins with the leaf and
2613 // ends with trust anchor.
2614 //
2615 // At other points, such as after a failed verification or during the deprecated
2616 // verification callback, it returns the partial chain built so far. Callers
2617 // should avoid relying on this as this exposes unstable library implementation
2618 // details.
2619 OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get0_chain(X509_STORE_CTX *ctx);
2620
2621 // X509_STORE_CTX_get1_chain behaves like |X509_STORE_CTX_get0_chain| but
2622 // returns a newly-allocated |STACK_OF(X509)| containing the completed chain,
2623 // with each certificate's reference count incremented. Callers must free the
2624 // result with |sk_X509_pop_free| and |X509_free| when done.
2625 OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx);
2626
2627 OPENSSL_EXPORT void X509_STORE_CTX_set_cert(X509_STORE_CTX *c, X509 *x);
2628
2629
2630 // The following values are possible outputs of |X509_STORE_CTX_get_error|.
2631 #define X509_V_OK 0
2632 #define X509_V_ERR_UNSPECIFIED 1
2633 #define X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT 2
2634 #define X509_V_ERR_UNABLE_TO_GET_CRL 3
2635 #define X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE 4
2636 #define X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE 5
2637 #define X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY 6
2638 #define X509_V_ERR_CERT_SIGNATURE_FAILURE 7
2639 #define X509_V_ERR_CRL_SIGNATURE_FAILURE 8
2640 #define X509_V_ERR_CERT_NOT_YET_VALID 9
2641 #define X509_V_ERR_CERT_HAS_EXPIRED 10
2642 #define X509_V_ERR_CRL_NOT_YET_VALID 11
2643 #define X509_V_ERR_CRL_HAS_EXPIRED 12
2644 #define X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD 13
2645 #define X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD 14
2646 #define X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD 15
2647 #define X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD 16
2648 #define X509_V_ERR_OUT_OF_MEM 17
2649 #define X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT 18
2650 #define X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN 19
2651 #define X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY 20
2652 #define X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE 21
2653 #define X509_V_ERR_CERT_CHAIN_TOO_LONG 22
2654 #define X509_V_ERR_CERT_REVOKED 23
2655 #define X509_V_ERR_INVALID_CA 24
2656 #define X509_V_ERR_PATH_LENGTH_EXCEEDED 25
2657 #define X509_V_ERR_INVALID_PURPOSE 26
2658 #define X509_V_ERR_CERT_UNTRUSTED 27
2659 #define X509_V_ERR_CERT_REJECTED 28
2660 #define X509_V_ERR_SUBJECT_ISSUER_MISMATCH 29
2661 #define X509_V_ERR_AKID_SKID_MISMATCH 30
2662 #define X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH 31
2663 #define X509_V_ERR_KEYUSAGE_NO_CERTSIGN 32
2664 #define X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER 33
2665 #define X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION 34
2666 #define X509_V_ERR_KEYUSAGE_NO_CRL_SIGN 35
2667 #define X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION 36
2668 #define X509_V_ERR_INVALID_NON_CA 37
2669 #define X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED 38
2670 #define X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE 39
2671 #define X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED 40
2672 #define X509_V_ERR_INVALID_EXTENSION 41
2673 #define X509_V_ERR_INVALID_POLICY_EXTENSION 42
2674 #define X509_V_ERR_NO_EXPLICIT_POLICY 43
2675 #define X509_V_ERR_DIFFERENT_CRL_SCOPE 44
2676 #define X509_V_ERR_UNSUPPORTED_EXTENSION_FEATURE 45
2677 #define X509_V_ERR_UNNESTED_RESOURCE 46
2678 #define X509_V_ERR_PERMITTED_VIOLATION 47
2679 #define X509_V_ERR_EXCLUDED_VIOLATION 48
2680 #define X509_V_ERR_SUBTREE_MINMAX 49
2681 #define X509_V_ERR_APPLICATION_VERIFICATION 50
2682 #define X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE 51
2683 #define X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX 52
2684 #define X509_V_ERR_UNSUPPORTED_NAME_SYNTAX 53
2685 #define X509_V_ERR_CRL_PATH_VALIDATION_ERROR 54
2686 #define X509_V_ERR_HOSTNAME_MISMATCH 62
2687 #define X509_V_ERR_EMAIL_MISMATCH 63
2688 #define X509_V_ERR_IP_ADDRESS_MISMATCH 64
2689 #define X509_V_ERR_INVALID_CALL 65
2690 #define X509_V_ERR_STORE_LOOKUP 66
2691 #define X509_V_ERR_NAME_CONSTRAINTS_WITHOUT_SANS 67
2692 // The following error codes are related to security levels in OpenSSL and are
2693 // unused in AWS-LC. See |SSL_CTX_set_security_level|.
2694 #define X509_V_ERR_EE_KEY_TOO_SMALL 68
2695 #define X509_V_ERR_CA_KEY_TOO_SMALL 69
2696 #define X509_V_ERR_CA_MD_TOO_WEAK 70
2697 #define X509_V_UNABLE_TO_GET_CERTS_PUBLIC_KEY 71
2698 #define X509_V_ERR_EC_KEY_EXPLICIT_PARAMS 72
2699
2700 // X509_STORE_CTX_get_error, after |X509_verify_cert| returns, returns
2701 // |X509_V_OK| if verification succeeded or an |X509_V_ERR_*| describing why
2702 // verification failed. This will be consistent with |X509_verify_cert|'s return
2703 // value, unless the caller used the deprecated verification callback (see
2704 // |X509_STORE_CTX_set_verify_cb|) in a way that breaks |ctx|'s invariants.
2705 //
2706 // If called during the deprecated verification callback when |ok| is zero, it
2707 // returns the current error under consideration.
2708 OPENSSL_EXPORT int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx);
2709
2710 // X509_STORE_CTX_set_error sets |ctx|'s error to |err|, which should be
2711 // |X509_V_OK| or an |X509_V_ERR_*| constant. It is not expected to be called in
2712 // typical |X509_STORE_CTX| usage, but may be used in callback APIs where
2713 // applications synthesize |X509_STORE_CTX| error conditions. See also
2714 // |X509_STORE_CTX_set_verify_cb| and |SSL_CTX_set_cert_verify_callback|.
2715 OPENSSL_EXPORT void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err);
2716
2717 // X509_verify_cert_error_string returns |err| as a human-readable string, where
2718 // |err| should be one of the |X509_V_*| values. If |err| is unknown, it returns
2719 // a default description.
2720 OPENSSL_EXPORT const char *X509_verify_cert_error_string(long err);
2721
2722 // X509_STORE_CTX_get_error_depth returns the depth at which the error returned
2723 // by |X509_STORE_CTX_get_error| occured. This is zero-indexed integer into the
2724 // certificate chain. Zero indicates the target certificate, one its issuer, and
2725 // so on.
2726 OPENSSL_EXPORT int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx);
2727
2728 // X509_STORE_CTX_get_current_cert returns the certificate which caused the
2729 // error returned by |X509_STORE_CTX_get_error|.
2730 OPENSSL_EXPORT X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx);
2731
2732 OPENSSL_EXPORT X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx);
2733
2734 // X509_STORE_CTX_get0_current_crl returns the CRL which caused the error
2735 // returned by |X509_STORE_CTX_get_error|.
2736 OPENSSL_EXPORT X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx);
2737
2738 // X509_STORE_CTX_get0_store returns the |X509_STORE| that |ctx| uses.
2739 OPENSSL_EXPORT X509_STORE *X509_STORE_CTX_get0_store(X509_STORE_CTX *ctx);
2740
2741 // X509_STORE_CTX_get0_cert returns the leaf certificate that |ctx| is
2742 // verifying.
2743 OPENSSL_EXPORT X509 *X509_STORE_CTX_get0_cert(X509_STORE_CTX *ctx);
2744
2745 // X509_STORE_CTX_get0_untrusted returns the stack of untrusted intermediates
2746 // used by |ctx| for certificate verification.
2747 OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get0_untrusted(
2748 X509_STORE_CTX *ctx);
2749
2750 // X509_STORE_CTX_set0_trusted_stack configures |ctx| to trust the certificates
2751 // in |sk|. |sk| must remain valid for the duration of |ctx|. Calling this
2752 // function causes |ctx| to ignore any certificates configured in the
2753 // |X509_STORE|. Certificates in |sk| are still subject to the check described
2754 // in |X509_VERIFY_PARAM_set_trust|.
2755 //
2756 // WARNING: This function differs from most |set0| functions in that it does not
2757 // take ownership of its input. The caller is required to ensure the lifetimes
2758 // are consistent.
2759 OPENSSL_EXPORT void X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX *ctx,
2760 STACK_OF(X509) *sk);
2761
2762 // X509_STORE_CTX_set0_crls configures |ctx| to consider the CRLs in |sk| as
2763 // candidates for CRL lookup. |sk| must remain valid for the duration of |ctx|.
2764 // These CRLs are considered in addition to CRLs found in |X509_STORE|.
2765 //
2766 // WARNING: This function differs from most |set0| functions in that it does not
2767 // take ownership of its input. The caller is required to ensure the lifetimes
2768 // are consistent.
2769 OPENSSL_EXPORT void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx,
2770 STACK_OF(X509_CRL) *sk);
2771
2772 // X509_STORE_CTX_set_default looks up the set of parameters named |name| and
2773 // applies those default verification parameters for |ctx|. As in
2774 // |X509_VERIFY_PARAM_inherit|, only unset parameters are changed. This function
2775 // returns one on success and zero on error.
2776 //
2777 // The supported values of |name| are:
2778 // - "default" is an internal value which configures some late defaults. See the
2779 // discussion in |X509_STORE_get0_param|.
2780 // - "pkcs7" configures default trust and purpose checks for PKCS#7 signatures.
2781 // - "smime_sign" configures trust and purpose checks for S/MIME signatures.
2782 // - "ssl_client" configures trust and purpose checks for TLS clients.
2783 // - "ssl_server" configures trust and purpose checks for TLS servers.
2784 //
2785 // TODO(crbug.com/boringssl/441): Make "default" a no-op.
2786 OPENSSL_EXPORT int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx,
2787 const char *name);
2788
2789 // X509_STORE_CTX_get0_param returns |ctx|'s verification parameters. This
2790 // object is mutable and may be modified by the caller.
2791 OPENSSL_EXPORT X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(
2792 X509_STORE_CTX *ctx);
2793
2794 // X509_STORE_CTX_set0_param returns |ctx|'s verification parameters to |param|
2795 // and takes ownership of |param|. After this function returns, the caller
2796 // should not free |param|.
2797 //
2798 // WARNING: This function discards any values which were previously applied in
2799 // |ctx|, including the "default" parameters applied late in
2800 // |X509_STORE_CTX_init|. These late defaults are not applied to parameters
2801 // created standalone by |X509_VERIFY_PARAM_new|.
2802 //
2803 // TODO(crbug.com/boringssl/441): This behavior is very surprising. Should we
2804 // re-apply the late defaults in |param|, or somehow avoid this notion of late
2805 // defaults altogether?
2806 OPENSSL_EXPORT void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx,
2807 X509_VERIFY_PARAM *param);
2808
2809 // X509_STORE_CTX_set_flags enables all values in |flags| in |ctx|'s
2810 // verification flags. |flags| should be a combination of |X509_V_FLAG_*|
2811 // constants.
2812 OPENSSL_EXPORT void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx,
2813 unsigned long flags);
2814
2815 // X509_STORE_CTX_set_time configures certificate verification to use |t|
2816 // instead of the current time. |flags| is ignored and should be zero.
2817 OPENSSL_EXPORT void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx,
2818 unsigned long flags, time_t t);
2819
2820 // X509_STORE_CTX_set_time_posix configures certificate verification to use |t|
2821 // instead of the current time. |t| is interpreted as a POSIX timestamp in
2822 // seconds. |flags| is ignored and should be zero.
2823 OPENSSL_EXPORT void X509_STORE_CTX_set_time_posix(X509_STORE_CTX *ctx,
2824 unsigned long flags,
2825 int64_t t);
2826
2827 // X509_STORE_CTX_set_depth configures |ctx| to, by default, limit certificate
2828 // chains to |depth| intermediate certificates. This count excludes both the
2829 // target certificate and the trust anchor (root certificate).
2830 OPENSSL_EXPORT void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth);
2831
2832 // X509_STORE_CTX_set_purpose simultaneously configures |ctx|'s purpose and
2833 // trust checks, if unset. It returns one on success and zero if |purpose| is
2834 // not a valid purpose value. |purpose| should be an |X509_PURPOSE_*| constant.
2835 // If so, it configures |ctx| with a purpose check of |purpose| and a trust
2836 // check of |purpose|'s corresponding trust value. If either the purpose or
2837 // trust check had already been specified for |ctx|, that corresponding
2838 // modification is silently dropped.
2839 //
2840 // See |X509_VERIFY_PARAM_set_purpose| and |X509_VERIFY_PARAM_set_trust| for
2841 // details on the purpose and trust checks, respectively.
2842 //
2843 // If |purpose| is |X509_PURPOSE_ANY|, this function returns an error because it
2844 // has no corresponding |X509_TRUST_*| value. It is not possible to set
2845 // |X509_PURPOSE_ANY| with this function, only |X509_VERIFY_PARAM_set_purpose|.
2846 //
2847 // WARNING: Unlike similarly named functions in this header, this function
2848 // silently does not behave the same as |X509_VERIFY_PARAM_set_purpose|. Callers
2849 // may use |X509_VERIFY_PARAM_set_purpose| with |X509_STORE_CTX_get0_param| to
2850 // avoid this difference.
2851 OPENSSL_EXPORT int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose);
2852
2853 // X509_STORE_CTX_set_trust configures |ctx|'s trust check, if unset. It returns
2854 // one on success and zero if |trust| is not a valid trust value. |trust| should
2855 // be an |X509_TRUST_*| constant. If so, it configures |ctx| with a trust check
2856 // of |trust|. If the trust check had already been specified for |ctx|, it
2857 // silently does nothing.
2858 //
2859 // See |X509_VERIFY_PARAM_set_trust| for details on the purpose and trust check.
2860 //
2861 // WARNING: Unlike similarly named functions in this header, this function
2862 // does not behave the same as |X509_VERIFY_PARAM_set_trust|. Callers may use
2863 // |X509_VERIFY_PARAM_set_trust| with |X509_STORE_CTX_get0_param| to avoid this
2864 // difference.
2865 OPENSSL_EXPORT int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust);
2866
2867 // X509_STORE_CTX_add_custom_crit_oid adds |oid| to the list of "known" critical
2868 // extension OIDs in |ctx|. Typical OpenSSL/AWS-LC behavior returns an error if
2869 // there are any unknown critical extensions present within the certificates
2870 // being validated. This function lets users specify custom OIDs of any critical
2871 // extensions that are within the certificates being validated, that they wish
2872 // to allow.
2873 //
2874 // To properly consume this feature, the callback mechanism with
2875 // |X509_STORE_CTX_set_verify_crit_oids| must be set. See its specific
2876 // documentation for more details.
2877 OPENSSL_EXPORT int X509_STORE_CTX_add_custom_crit_oid(X509_STORE_CTX *ctx,
2878 ASN1_OBJECT *oid);
2879
2880 // X509_STORE_CTX_verify_crit_oids is the callback signature for
2881 // |X509_STORE_CTX_set_verify_crit_oids|. |ctx| is the context being used,
2882 // |x509| represents the current certificate being validated, and |oids|
2883 // is a stack of |ASN1_OBJECT|s representing unknown critical extension
2884 // OIDs that were found in |x509| and match those previously registered via
2885 // |X509_STORE_CTX_add_custom_crit_oid|.
2886 typedef int (*X509_STORE_CTX_verify_crit_oids_cb)(X509_STORE_CTX *ctx,
2887 X509 *x509,
2888 STACK_OF(ASN1_OBJECT) *oids);
2889
2890 // X509_STORE_CTX_set_verify_crit_oids sets the |verify_crit_oids| callback
2891 // function for |ctx|. Consumers should be performing additional validation
2892 // against the custom extension oids after or during the handshake with
2893 // |X509_STORE_CTX_set_verify_crit_oids|. This callback forces users to validate
2894 // their custom OIDs when processing unknown custom critical extensions.
2895 OPENSSL_EXPORT void X509_STORE_CTX_set_verify_crit_oids(
2896 X509_STORE_CTX *ctx,
2897 X509_STORE_CTX_verify_crit_oids_cb verify_custom_crit_oids);
2898
2899 // Verification parameters
2900 //
2901 // An |X509_VERIFY_PARAM| contains a set of parameters for certificate
2902 // verification.
2903
2904 // X509_VERIFY_PARAM_new returns a newly-allocated |X509_VERIFY_PARAM|, or NULL
2905 // on error.
2906 OPENSSL_EXPORT X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void);
2907
2908 // X509_VERIFY_PARAM_free releases memory associated with |param|.
2909 OPENSSL_EXPORT void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param);
2910
2911 // X509_VERIFY_PARAM_inherit applies |from| as the default values for |to|. That
2912 // is, for each parameter that is unset in |to|, it copies the value in |from|.
2913 // This function returns one on success and zero on error.
2914 OPENSSL_EXPORT int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *to,
2915 const X509_VERIFY_PARAM *from);
2916
2917 // X509_VERIFY_PARAM_set1 copies parameters from |from| to |to|. If a parameter
2918 // is unset in |from|, the existing value in |to| is preserved. This function
2919 // returns one on success and zero on error.
2920 OPENSSL_EXPORT int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to,
2921 const X509_VERIFY_PARAM *from);
2922
2923 // X509_V_FLAG_* are flags for |X509_VERIFY_PARAM_set_flags| and
2924 // |X509_VERIFY_PARAM_clear_flags|.
2925
2926 // X509_V_FLAG_CB_ISSUER_CHECK causes the deprecated verify callback (see
2927 // |X509_STORE_CTX_set_verify_cb|) to be called for errors while matching
2928 // subject and issuer certificates.
2929 #define X509_V_FLAG_CB_ISSUER_CHECK 0x1
2930 // X509_V_FLAG_USE_CHECK_TIME is an internal flag used to track whether
2931 // |X509_STORE_CTX_set_time| has been used. If cleared, the system time is
2932 // restored.
2933 #define X509_V_FLAG_USE_CHECK_TIME 0x2
2934 // X509_V_FLAG_CRL_CHECK enables CRL lookup and checking for the leaf.
2935 #define X509_V_FLAG_CRL_CHECK 0x4
2936 // X509_V_FLAG_CRL_CHECK_ALL enables CRL lookup and checking for the entire
2937 // certificate chain. |X509_V_FLAG_CRL_CHECK| must be set for this flag to take
2938 // effect.
2939 #define X509_V_FLAG_CRL_CHECK_ALL 0x8
2940 // X509_V_FLAG_IGNORE_CRITICAL ignores unhandled critical extensions. Do not use
2941 // this option. Critical extensions ensure the verifier does not bypass
2942 // unrecognized security restrictions in certificates.
2943 #define X509_V_FLAG_IGNORE_CRITICAL 0x10
2944 // X509_V_FLAG_X509_STRICT does nothing. Its functionality has been enabled by
2945 // default.
2946 #define X509_V_FLAG_X509_STRICT 0x00
2947 // X509_V_FLAG_ALLOW_PROXY_CERTS does nothing. Proxy certificate support has
2948 // been removed.
2949 #define X509_V_FLAG_ALLOW_PROXY_CERTS 0x40
2950 // X509_V_FLAG_POLICY_CHECK does nothing. Policy checking is always enabled.
2951 #define X509_V_FLAG_POLICY_CHECK 0x80
2952 // X509_V_FLAG_EXPLICIT_POLICY requires some policy OID to be asserted by the
2953 // final certificate chain. See initial-explicit-policy from RFC 5280,
2954 // section 6.1.1.
2955 #define X509_V_FLAG_EXPLICIT_POLICY 0x100
2956 // X509_V_FLAG_INHIBIT_ANY inhibits the anyPolicy OID. See
2957 // initial-any-policy-inhibit from RFC 5280, section 6.1.1.
2958 #define X509_V_FLAG_INHIBIT_ANY 0x200
2959 // X509_V_FLAG_INHIBIT_MAP inhibits policy mapping. See
2960 // initial-policy-mapping-inhibit from RFC 5280, section 6.1.1.
2961 #define X509_V_FLAG_INHIBIT_MAP 0x400
2962 // X509_V_FLAG_NOTIFY_POLICY does nothing. Its functionality has been removed.
2963 #define X509_V_FLAG_NOTIFY_POLICY 0x800
2964 // X509_V_FLAG_EXTENDED_CRL_SUPPORT causes all verifications to fail. Extended
2965 // CRL features have been removed.
2966 #define X509_V_FLAG_EXTENDED_CRL_SUPPORT 0x1000
2967 // X509_V_FLAG_USE_DELTAS causes all verifications to fail. Delta CRL support
2968 // has been removed.
2969 #define X509_V_FLAG_USE_DELTAS 0x2000
2970 // X509_V_FLAG_CHECK_SS_SIGNATURE checks the redundant signature on self-signed
2971 // trust anchors. This check provides no security benefit and only wastes CPU.
2972 #define X509_V_FLAG_CHECK_SS_SIGNATURE 0x4000
2973 // X509_V_FLAG_TRUSTED_FIRST, during path-building, checks for a match in the
2974 // trust store before considering an untrusted intermediate. This flag is
2975 // enabled by default.
2976 #define X509_V_FLAG_TRUSTED_FIRST 0x8000
2977 // X509_V_FLAG_PARTIAL_CHAIN treats all trusted certificates as trust anchors,
2978 // independent of the |X509_VERIFY_PARAM_set_trust| setting.
2979 #define X509_V_FLAG_PARTIAL_CHAIN 0x80000
2980 // X509_V_FLAG_NO_ALT_CHAINS disables building alternative chains if the initial
2981 // one was rejected.
2982 #define X509_V_FLAG_NO_ALT_CHAINS 0x100000
2983 // X509_V_FLAG_NO_CHECK_TIME disables all time checks in certificate
2984 // verification.
2985 #define X509_V_FLAG_NO_CHECK_TIME 0x200000
2986
2987 // X509_VERIFY_PARAM_set_flags enables all values in |flags| in |param|'s
2988 // verification flags and returns one. |flags| should be a combination of
2989 // |X509_V_FLAG_*| constants.
2990 OPENSSL_EXPORT int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param,
2991 unsigned long flags);
2992
2993 // X509_VERIFY_PARAM_clear_flags disables all values in |flags| in |param|'s
2994 // verification flags and returns one. |flags| should be a combination of
2995 // |X509_V_FLAG_*| constants.
2996 OPENSSL_EXPORT int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param,
2997 unsigned long flags);
2998
2999 // X509_VERIFY_PARAM_get_flags returns |param|'s verification flags.
3000 OPENSSL_EXPORT unsigned long X509_VERIFY_PARAM_get_flags(
3001 const X509_VERIFY_PARAM *param);
3002
3003 // X509_VERIFY_PARAM_set_depth configures |param| to limit certificate chains to
3004 // |depth| intermediate certificates. This count excludes both the target
3005 // certificate and the trust anchor (root certificate).
3006 OPENSSL_EXPORT void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param,
3007 int depth);
3008
3009 // X509_VERIFY_PARAM_get_depth returns the maximum depth configured in |param|.
3010 // See |X509_VERIFY_PARAM_set_depth|.
3011 OPENSSL_EXPORT int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param);
3012
3013 // X509_VERIFY_PARAM_set_time configures certificate verification to use |t|
3014 // instead of the current time.
3015 OPENSSL_EXPORT void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param,
3016 time_t t);
3017
3018 // X509_VERIFY_PARAM_set_time_posix configures certificate verification to use
3019 // |t| instead of the current time. |t| is interpreted as a POSIX timestamp in
3020 // seconds.
3021 OPENSSL_EXPORT void X509_VERIFY_PARAM_set_time_posix(X509_VERIFY_PARAM *param,
3022 int64_t t);
3023
3024 // X509_VERIFY_PARAM_add0_policy adds |policy| to the user-initial-policy-set
3025 // (see Section 6.1.1 of RFC 5280). On success, it takes ownership of
3026 // |policy| and returns one. Otherwise, it returns zero and the caller retains
3027 // owneship of |policy|.
3028 OPENSSL_EXPORT int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param,
3029 ASN1_OBJECT *policy);
3030
3031 // X509_VERIFY_PARAM_set1_policies sets the user-initial-policy-set (see
3032 // Section 6.1.1 of RFC 5280) to a copy of |policies|. It returns one on success
3033 // and zero on error.
3034 OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_policies(
3035 X509_VERIFY_PARAM *param, const STACK_OF(ASN1_OBJECT) *policies);
3036
3037 // X509_VERIFY_PARAM_set1_host configures |param| to check for the DNS name
3038 // specified by |name| and clears any previously specified hostname. If |name|
3039 // is NULL or empty, the list of hostnames is cleared and name checks are not
3040 // performed on the peer certificate. It returns one on success and zero on
3041 // error.
3042 // |namelen| should be set to the length of |name|. It may be zero if |name| is
3043 // NUL-terminated, but this is only maintained for backwards compatibility with
3044 // OpenSSL.
3045 //
3046 // By default, both subject alternative names and the subject's common name
3047 // attribute are checked. The latter has long been deprecated, so callers should
3048 // call |X509_VERIFY_PARAM_set_hostflags| with
3049 // |X509_CHECK_FLAG_NEVER_CHECK_SUBJECT| to use the standard behavior.
3050 // https://crbug.com/boringssl/464 tracks fixing the default.
3051 OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param,
3052 const char *name,
3053 size_t name_len);
3054
3055 // X509_VERIFY_PARAM_add1_host adds |name| to the list of names checked by
3056 // |param|. If any configured DNS name matches the certificate, verification
3057 // succeeds. Any previous names set via |X509_VERIFY_PARAM_set1_host| or
3058 // |X509_VERIFY_PARAM_add1_host| are retained, no change is made if |name| is
3059 // NULL or empty. When multiple names are configured, the peer is considered
3060 // verified when any name matches. It returns one on success and zero on error.
3061 // |namelen| should be set to the length of |name|. It may be zero if |name| is
3062 // NUL-terminated, but this is only maintained for backwards compatibility with
3063 // OpenSSL.
3064 //
3065 // By default, both subject alternative names and the subject's common name
3066 // attribute are checked. The latter has long been deprecated, so callers should
3067 // call |X509_VERIFY_PARAM_set_hostflags| with
3068 // |X509_CHECK_FLAG_NEVER_CHECK_SUBJECT| to use the standard behavior.
3069 // https://crbug.com/boringssl/464 tracks fixing the default.
3070 OPENSSL_EXPORT int X509_VERIFY_PARAM_add1_host(X509_VERIFY_PARAM *param,
3071 const char *name,
3072 size_t name_len);
3073
3074 // X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT enables always checking the subject name
3075 // for host match even if subject alt names are present.
3076 #define X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT 0x1
3077
3078 // X509_CHECK_FLAG_NO_WILDCARDS disables wildcard matching for DNS names.
3079 #define X509_CHECK_FLAG_NO_WILDCARDS 0x2
3080
3081 // X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS constrains host name patterns passed
3082 // to |X509_check_host| starting with '.' to only match a single label /
3083 // subdomain.
3084 //
3085 // For example, by default the host name '.example.com' would match a
3086 // certificate DNS name like 'www.example.com' and 'www.foo.example.com'.
3087 // Setting this flag would result in the same host name only matching
3088 // 'www.example.com' but not 'www.foo.example.com'.
3089 #define X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS 0x10
3090
3091 // X509_CHECK_FLAG_NEVER_CHECK_SUBJECT disables the subject fallback, normally
3092 // enabled when subjectAltNames is missing.
3093 #define X509_CHECK_FLAG_NEVER_CHECK_SUBJECT 0x20
3094
3095 // X509_VERIFY_PARAM_set_hostflags sets the name-checking flags on |param| to
3096 // |flags|. |flags| should be a combination of |X509_CHECK_FLAG_*| constants.
3097 OPENSSL_EXPORT void X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param,
3098 unsigned int flags);
3099
3100 // X509_VERIFY_PARAM_get_hostflags returns |param|'s name-checking flags.
3101 OPENSSL_EXPORT unsigned int X509_VERIFY_PARAM_get_hostflags(
3102 const X509_VERIFY_PARAM *param);
3103
3104
3105 // X509_VERIFY_PARAM_set1_email configures |param| to check for the email
3106 // address specified by |email|. It returns one on success and zero on error.
3107 // |emaillen| should be set to the length of |email|. It may be zero if |email|
3108 // is NUL-terminated, but this is only maintained for backwards compatibility
3109 // with OpenSSL.
3110 //
3111 // By default, both subject alternative names and the subject's email address
3112 // attribute are checked. The |X509_CHECK_FLAG_NEVER_CHECK_SUBJECT| flag may be
3113 // used to change this behavior.
3114 OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *param,
3115 const char *email,
3116 size_t email_len);
3117
3118 // X509_VERIFY_PARAM_set1_ip configures |param| to check for the IP address
3119 // specified by |ip|. It returns one on success and zero on error. The IP
3120 // address is specified in its binary representation. |ip_len| must be 4 for an
3121 // IPv4 address and 16 for an IPv6 address.
3122 // NOTE:|iplen| MUST be set to the length of |ip|.
3123 OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *param,
3124 const uint8_t *ip, size_t ip_len);
3125
3126 // X509_VERIFY_PARAM_set1_ip_asc decodes |ipasc| as the ASCII representation of
3127 // an IPv4 or IPv6 address, and configures |param| to check for it. It returns
3128 // one on success and zero on error.
3129 // |ipasc| MUST be a NUL-terminal ASCII string: dotted decimal quad for IPv4 and
3130 // colon-separated hexadecimal for IPv6. The condensed "::" notation is
3131 // supported for IPv6 addresses.
3132 OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param,
3133 const char *ipasc);
3134
3135 // X509_PURPOSE_SSL_CLIENT validates TLS client certificates. It checks for the
3136 // id-kp-clientAuth EKU and one of digitalSignature or keyAgreement key usages.
3137 // The TLS library is expected to check for the key usage specific to the
3138 // negotiated TLS parameters.
3139 #define X509_PURPOSE_SSL_CLIENT 1
3140 // X509_PURPOSE_SSL_SERVER validates TLS server certificates. It checks for the
3141 // id-kp-clientAuth EKU and one of digitalSignature, keyAgreement, or
3142 // keyEncipherment key usages. The TLS library is expected to check for the key
3143 // usage specific to the negotiated TLS parameters.
3144 #define X509_PURPOSE_SSL_SERVER 2
3145 // X509_PURPOSE_NS_SSL_SERVER is a legacy mode. It behaves like
3146 // |X509_PURPOSE_SSL_SERVER|, but only accepts the keyEncipherment key usage,
3147 // used by SSL 2.0 and RSA key exchange. Do not use this.
3148 #define X509_PURPOSE_NS_SSL_SERVER 3
3149 // X509_PURPOSE_SMIME_SIGN validates S/MIME signing certificates. It checks for
3150 // the id-kp-emailProtection EKU and one of digitalSignature or nonRepudiation
3151 // key usages.
3152 #define X509_PURPOSE_SMIME_SIGN 4
3153 // X509_PURPOSE_SMIME_ENCRYPT validates S/MIME encryption certificates. It
3154 // checks for the id-kp-emailProtection EKU and keyEncipherment key usage.
3155 #define X509_PURPOSE_SMIME_ENCRYPT 5
3156 // X509_PURPOSE_CRL_SIGN validates indirect CRL signers. It checks for the
3157 // cRLSign key usage. BoringSSL does not support indirect CRLs and does not use
3158 // this mode.
3159 #define X509_PURPOSE_CRL_SIGN 6
3160 // X509_PURPOSE_ANY performs no EKU or key usage checks. Such checks are the
3161 // responsibility of the caller.
3162 #define X509_PURPOSE_ANY 7
3163 // X509_PURPOSE_OCSP_HELPER performs no EKU or key usage checks. It was
3164 // historically used in OpenSSL's OCSP implementation, which left those checks
3165 // to the OCSP implementation itself.
3166 #define X509_PURPOSE_OCSP_HELPER 8
3167 // X509_PURPOSE_TIMESTAMP_SIGN validates Time Stamping Authority (RFC 3161)
3168 // certificates. It checks for the id-kp-timeStamping EKU and one of
3169 // digitalSignature or nonRepudiation key usages. It additionally checks that
3170 // the EKU extension is critical and that no other EKUs or key usages are
3171 // asserted.
3172 #define X509_PURPOSE_TIMESTAMP_SIGN 9
3173
3174 // X509_VERIFY_PARAM_set_purpose configures |param| to validate certificates for
3175 // a specified purpose. It returns one on success and zero if |purpose| is not a
3176 // valid purpose type. |purpose| should be one of the |X509_PURPOSE_*| values.
3177 //
3178 // This option controls checking the extended key usage (EKU) and key usage
3179 // extensions. These extensions specify how a certificate's public key may be
3180 // used and are important to avoid cross-protocol attacks, particularly in PKIs
3181 // that may issue certificates for multiple protocols, or for protocols that use
3182 // keys in multiple ways. If not configured, these security checks are the
3183 // caller's responsibility.
3184 //
3185 // This library applies the EKU checks to all untrusted intermediates. Although
3186 // not defined in RFC 5280, this matches widely-deployed practice. It also does
3187 // not accept anyExtendedKeyUsage.
3188 //
3189 // Many purpose values have a corresponding trust value, which is not configured
3190 // by this function. See |X509_VERIFY_PARAM_set_trust| for details. Callers
3191 // that wish to configure both should either call both functions, or use
3192 // |X509_STORE_CTX_set_purpose|.
3193 //
3194 // It is currently not possible to configure custom EKU OIDs or key usage bits.
3195 // Contact the BoringSSL maintainers if your application needs to do so. OpenSSL
3196 // had an |X509_PURPOSE_add| API, but it was not thread-safe and relied on
3197 // global mutable state, so we removed it.
3198 //
3199 // TODO(davidben): This function additionally configures checking the legacy
3200 // Netscape certificate type extension. Remove this.
3201 OPENSSL_EXPORT int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param,
3202 int purpose);
3203
3204 // X509_TRUST_COMPAT evaluates trust using only the self-signed fallback. Trust
3205 // and distrust OIDs are ignored.
3206 #define X509_TRUST_COMPAT 1
3207 // X509_TRUST_SSL_CLIENT evaluates trust with the |NID_client_auth| OID, for
3208 // validating TLS client certificates.
3209 #define X509_TRUST_SSL_CLIENT 2
3210 // X509_TRUST_SSL_SERVER evaluates trust with the |NID_server_auth| OID, for
3211 // validating TLS server certificates.
3212 #define X509_TRUST_SSL_SERVER 3
3213 // X509_TRUST_EMAIL evaluates trust with the |NID_email_protect| OID, for
3214 // validating S/MIME email certificates.
3215 #define X509_TRUST_EMAIL 4
3216 // X509_TRUST_OBJECT_SIGN evaluates trust with the |NID_code_sign| OID, for
3217 // validating code signing certificates.
3218 #define X509_TRUST_OBJECT_SIGN 5
3219 // X509_TRUST_OCSP_SIGN does nothing. It's unused in OpenSSL and AWS-LC.
3220 #define X509_TRUST_OCSP_SIGN 6
3221 // X509_TRUST_OCSP_REQUEST does nothing. It's unused in OpenSSL and AWS-LC.
3222 #define X509_TRUST_OCSP_REQUEST 7
3223 // X509_TRUST_TSA evaluates trust with the |NID_time_stamp| OID, for validating
3224 // Time Stamping Authority (RFC 3161) certificates.
3225 #define X509_TRUST_TSA 8
3226
3227 // X509_VERIFY_PARAM_set_trust configures which certificates from |X509_STORE|
3228 // are trust anchors. It returns one on success and zero if |trust| is not a
3229 // valid trust value. |trust| should be one of the |X509_TRUST_*| constants.
3230 // This function allows applications to vary trust anchors when the same set of
3231 // trusted certificates is used in multiple contexts.
3232 //
3233 // Two properties determine whether a certificate is a trust anchor:
3234 //
3235 // - Whether it is trusted or distrusted for some OID, via auxiliary information
3236 // configured by |X509_add1_trust_object| or |X509_add1_reject_object|.
3237 //
3238 // - Whether it is "self-signed". That is, whether |X509_get_extension_flags|
3239 // includes |EXFLAG_SS|. The signature itself is not checked.
3240 //
3241 // When this function is called, |trust| determines the OID to check in the
3242 // first case. If the certificate is not explicitly trusted or distrusted for
3243 // any OID, it is trusted if self-signed instead.
3244 //
3245 // If unset, the default behavior is to check for the |NID_anyExtendedKeyUsage|
3246 // OID. If the certificate is not explicitly trusted or distrusted for this OID,
3247 // it is trusted if self-signed instead. Note this slightly differs from the
3248 // above.
3249 //
3250 // If the |X509_V_FLAG_PARTIAL_CHAIN| is set, every certificate from
3251 // |X509_STORE| is a trust anchor, unless it was explicitly distrusted for the
3252 // OID.
3253 //
3254 // It is currently not possible to configure custom trust OIDs. Contact the
3255 // BoringSSL maintainers if your application needs to do so. OpenSSL had an
3256 // |X509_TRUST_add| API, but it was not thread-safe and relied on global mutable
3257 // state, so we removed it.
3258 OPENSSL_EXPORT int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param,
3259 int trust);
3260
3261 // X509_VERIFY_PARAM_enable_ec_key_explicit_params enables X.509 subject public
3262 // keys to contain elliptic curve keys with explicit parameters. By default
3263 // AWS-LC rejects validation of certificate chains containing public keys
3264 // with explicit EC parameters. Returns 1 on success, or 0 on failure.
3265 OPENSSL_EXPORT int X509_VERIFY_PARAM_enable_ec_key_explicit_params(
3266 X509_VERIFY_PARAM *param);
3267
3268 // X509_VERIFY_PARAM_disable_ec_key_explicit_params disables X.509 subject
3269 // public keys to contain elliptic curve keys with explicit parameters. By
3270 // default AWS-LC rejects validation of certificate chains containing public
3271 // keys with explicit EC parameters. Returns 1 on success, or 0 on failure.
3272 OPENSSL_EXPORT int X509_VERIFY_PARAM_disable_ec_key_explicit_params(
3273 X509_VERIFY_PARAM *param);
3274
3275 // Filesystem-based certificate stores.
3276 //
3277 // An |X509_STORE| may be configured to get its contents from the filesystem.
3278 // This is done by adding |X509_LOOKUP| structures to the |X509_STORE| with
3279 // |X509_STORE_add_lookup| and then configuring the |X509_LOOKUP| with paths.
3280 //
3281 // Most cases can use |X509_STORE_load_locations|, which configures the same
3282 // thing but is simpler to use.
3283
3284 // X509_STORE_load_locations configures |store| to load data from filepaths
3285 // |file| and |dir|. It returns one on success and zero on error. Either of
3286 // |file| or |dir| may be NULL, but at least one must be non-NULL.
3287 //
3288 // If |file| is non-NULL, it loads CRLs and trusted certificates in PEM format
3289 // from the file at |file|, and them to |store|, as in |X509_load_cert_crl_file|
3290 // with |X509_FILETYPE_PEM|.
3291 //
3292 // If |dir| is non-NULL, it configures |store| to load CRLs and trusted
3293 // certificates from the directory at |dir| in PEM format, as in
3294 // |X509_LOOKUP_add_dir| with |X509_FILETYPE_PEM|.
3295 OPENSSL_EXPORT int X509_STORE_load_locations(X509_STORE *store,
3296 const char *file, const char *dir);
3297
3298 // X509_STORE_add_lookup returns an |X509_LOOKUP| associated with |store| with
3299 // type |method|, or NULL on error. The result is owned by |store|, so callers
3300 // are not expected to free it. This may be used with |X509_LOOKUP_add_dir| or
3301 // |X509_LOOKUP_load_file|, depending on |method|, to configure |store|.
3302 //
3303 // A single |X509_LOOKUP| may be configured with multiple paths, and an
3304 // |X509_STORE| only contains one |X509_LOOKUP| of each type, so there is no
3305 // need to call this function multiple times for a single type. Calling it
3306 // multiple times will return the previous |X509_LOOKUP| of that type.
3307 OPENSSL_EXPORT X509_LOOKUP *X509_STORE_add_lookup(
3308 X509_STORE *store, const X509_LOOKUP_METHOD *method);
3309
3310 // X509_LOOKUP_hash_dir creates |X509_LOOKUP|s that may be used with
3311 // |X509_LOOKUP_add_dir|.
3312 OPENSSL_EXPORT const X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void);
3313
3314 // X509_LOOKUP_file creates |X509_LOOKUP|s that may be used with
3315 // |X509_LOOKUP_load_file|.
3316 //
3317 // Although this is modeled as an |X509_LOOKUP|, this function is redundant. It
3318 // has the same effect as loading a certificate or CRL from the filesystem, in
3319 // the caller's desired format, and then adding it with |X509_STORE_add_cert|
3320 // and |X509_STORE_add_crl|.
3321 OPENSSL_EXPORT const X509_LOOKUP_METHOD *X509_LOOKUP_file(void);
3322
3323 // The following constants are used to specify the format of files in an
3324 // |X509_LOOKUP|.
3325 #define X509_FILETYPE_PEM 1
3326 #define X509_FILETYPE_ASN1 2
3327 #define X509_FILETYPE_DEFAULT 3
3328
3329 // X509_LOOKUP_load_file calls |X509_load_cert_crl_file|. |lookup| must have
3330 // been constructed with |X509_LOOKUP_file|.
3331 //
3332 // If |type| is |X509_FILETYPE_DEFAULT|, it ignores |file| and instead uses some
3333 // default system path with |X509_FILETYPE_PEM|. See also
3334 // |X509_STORE_set_default_paths|.
3335 OPENSSL_EXPORT int X509_LOOKUP_load_file(X509_LOOKUP *lookup, const char *file,
3336 int type);
3337
3338 // X509_LOOKUP_add_dir configures |lookup| to load CRLs and trusted certificates
3339 // from the directories in |path|. It returns one on success and zero on error.
3340 // |lookup| must have been constructed with |X509_LOOKUP_hash_dir|.
3341 //
3342 // WARNING: |path| is interpreted as a colon-separated (semicolon-separated on
3343 // Windows) list of paths. It is not possible to configure a path containing the
3344 // separator character. https://crbug.com/boringssl/691 tracks removing this
3345 // behavior.
3346 //
3347 // |type| should be one of the |X509_FILETYPE_*| constants and determines the
3348 // format of the files. If |type| is |X509_FILETYPE_DEFAULT|, |path| is ignored
3349 // and some default system path is used with |X509_FILETYPE_PEM|. See also
3350 // |X509_STORE_set_default_paths|.
3351 //
3352 // Trusted certificates should be named HASH.N and CRLs should be
3353 // named HASH.rN. HASH is |X509_NAME_hash| of the certificate subject and CRL
3354 // issuer, respectively, in hexadecimal. N is in decimal and counts hash
3355 // collisions consecutively, starting from zero. For example, "002c0b4f.0" and
3356 // "002c0b4f.r0".
3357 //
3358 // WARNING: Objects from |path| are loaded on demand, but cached in memory on
3359 // the |X509_STORE|. If a CA is removed from the directory, existing
3360 // |X509_STORE|s will continue to trust it. Cache entries are not evicted for
3361 // the lifetime of the |X509_STORE|.
3362 //
3363 // WARNING: This mechanism is also not well-suited for CRL updates.
3364 // |X509_STORE|s rely on this cache and never load the same CRL file twice. CRL
3365 // updates must use a new file, with an incremented suffix, to be reflected in
3366 // existing |X509_STORE|s. However, this means each CRL update will use
3367 // additional storage and memory. Instead, configure inputs that vary per
3368 // verification, such as CRLs, on each |X509_STORE_CTX| separately, using
3369 // functions like |X509_STORE_CTX_set0_crl|.
3370 OPENSSL_EXPORT int X509_LOOKUP_add_dir(X509_LOOKUP *lookup, const char *path,
3371 int type);
3372
3373 // X509_L_* are commands for |X509_LOOKUP_ctrl|.
3374 #define X509_L_FILE_LOAD 1
3375 #define X509_L_ADD_DIR 2
3376
3377 // X509_LOOKUP_ctrl implements commands on |lookup|. |cmd| specifies the
3378 // command. The other arguments specify the operation in a command-specific way.
3379 // Use |X509_LOOKUP_load_file| or |X509_LOOKUP_add_dir| instead.
3380 OPENSSL_EXPORT int X509_LOOKUP_ctrl(X509_LOOKUP *lookup, int cmd,
3381 const char *argc, long argl, char **ret);
3382
3383 // X509_load_cert_file loads trusted certificates from |file| and adds them to
3384 // |lookup|'s |X509_STORE|. It returns one on success and zero on error.
3385 //
3386 // If |type| is |X509_FILETYPE_ASN1|, it loads a single DER-encoded certificate.
3387 // If |type| is |X509_FILETYPE_PEM|, it loads a sequence of PEM-encoded
3388 // certificates. |type| may not be |X509_FILETYPE_DEFAULT|.
3389 OPENSSL_EXPORT int X509_load_cert_file(X509_LOOKUP *lookup, const char *file,
3390 int type);
3391
3392 // X509_load_crl_file loads CRLs from |file| and add them it to |lookup|'s
3393 // |X509_STORE|. It returns one on success and zero on error.
3394 //
3395 // If |type| is |X509_FILETYPE_ASN1|, it loads a single DER-encoded CRL. If
3396 // |type| is |X509_FILETYPE_PEM|, it loads a sequence of PEM-encoded CRLs.
3397 // |type| may not be |X509_FILETYPE_DEFAULT|.
3398 OPENSSL_EXPORT int X509_load_crl_file(X509_LOOKUP *lookup, const char *file,
3399 int type);
3400
3401 // X509_load_cert_crl_file loads CRLs and trusted certificates from |file| and
3402 // adds them to |lookup|'s |X509_STORE|. It returns one on success and zero on
3403 // error.
3404 //
3405 // If |type| is |X509_FILETYPE_ASN1|, it loads a single DER-encoded certificate.
3406 // This function cannot be used to load a DER-encoded CRL. If |type| is
3407 // |X509_FILETYPE_PEM|, it loads a sequence of PEM-encoded certificates and
3408 // CRLs. |type| may not be |X509_FILETYPE_DEFAULT|.
3409 OPENSSL_EXPORT int X509_load_cert_crl_file(X509_LOOKUP *lookup,
3410 const char *file, int type);
3411
3412 // X509_NAME_hash returns a hash of |name|, or zero on error. This is the new
3413 // hash used by |X509_LOOKUP_add_dir|.
3414 //
3415 // This hash is specific to the |X509_LOOKUP_add_dir| filesystem format and is
3416 // not suitable for general-purpose X.509 name processing. It is very short, so
3417 // there will be hash collisions. It also depends on an OpenSSL-specific
3418 // canonicalization process.
3419 //
3420 // TODO(https://crbug.com/boringssl/407): This should be const and thread-safe
3421 // but currently is neither, notably if |name| was modified from its parsed
3422 // value.
3423 OPENSSL_EXPORT uint32_t X509_NAME_hash(X509_NAME *name);
3424
3425 // X509_NAME_hash_old returns a hash of |name|, or zero on error. This is the
3426 // legacy hash used by |X509_LOOKUP_add_dir|, which is still supported for
3427 // compatibility.
3428 //
3429 // This hash is specific to the |X509_LOOKUP_add_dir| filesystem format and is
3430 // not suitable for general-purpose X.509 name processing. It is very short, so
3431 // there will be hash collisions.
3432 //
3433 // TODO(https://crbug.com/boringssl/407): This should be const and thread-safe
3434 // but currently is neither, notably if |name| was modified from its parsed
3435 // value.
3436 OPENSSL_EXPORT uint32_t X509_NAME_hash_old(X509_NAME *name);
3437
3438 // X509_STORE_set_default_paths configures |store| to read from some "default"
3439 // filesystem paths. It returns one on success and zero on error. The filesystem
3440 // paths are determined by a combination of hardcoded paths and the SSL_CERT_DIR
3441 // and SSL_CERT_FILE environment variables.
3442 //
3443 // Using this function is not recommended. In OpenSSL, these defaults are
3444 // determined by OpenSSL's install prefix. There is no corresponding concept for
3445 // BoringSSL. Future versions of BoringSSL may change or remove this
3446 // functionality.
3447 OPENSSL_EXPORT int X509_STORE_set_default_paths(X509_STORE *store);
3448
3449 // The following functions return filesystem paths used to determine the above
3450 // "default" paths, when the corresponding environment variables are not set.
3451 //
3452 // Using these functions is not recommended. In OpenSSL, these defaults are
3453 // determined by OpenSSL's install prefix. There is no corresponding concept for
3454 // BoringSSL. Future versions of BoringSSL may change or remove this
3455 // functionality.
3456 OPENSSL_EXPORT const char *X509_get_default_cert_area(void);
3457 OPENSSL_EXPORT const char *X509_get_default_cert_dir(void);
3458 OPENSSL_EXPORT const char *X509_get_default_cert_file(void);
3459 OPENSSL_EXPORT const char *X509_get_default_private_dir(void);
3460
3461 // X509_get_default_cert_dir_env returns "SSL_CERT_DIR", an environment variable
3462 // used to determine the above "default" paths.
3463 OPENSSL_EXPORT const char *X509_get_default_cert_dir_env(void);
3464
3465 // X509_get_default_cert_file_env returns "SSL_CERT_FILE", an environment
3466 // variable used to determine the above "default" paths.
3467 OPENSSL_EXPORT const char *X509_get_default_cert_file_env(void);
3468
3469
3470 // SignedPublicKeyAndChallenge structures.
3471 //
3472 // The SignedPublicKeyAndChallenge (SPKAC) is a legacy structure to request
3473 // certificates, primarily in the legacy <keygen> HTML tag. An SPKAC structure
3474 // is represented by a |NETSCAPE_SPKI| structure.
3475 //
3476 // The structure is described in
3477 // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/keygen
3478
3479 // A Netscape_spki_st, or |NETSCAPE_SPKI|, represents a
3480 // SignedPublicKeyAndChallenge structure. Although this structure contains a
3481 // |spkac| field of type |NETSCAPE_SPKAC|, these are misnamed. The SPKAC is the
3482 // entire structure, not the signed portion.
3483 struct Netscape_spki_st {
3484 NETSCAPE_SPKAC *spkac;
3485 X509_ALGOR *sig_algor;
3486 ASN1_BIT_STRING *signature;
3487 } /* NETSCAPE_SPKI */;
3488
3489 // NETSCAPE_SPKI is an |ASN1_ITEM| whose ASN.1 type is
3490 // SignedPublicKeyAndChallenge and C type is |NETSCAPE_SPKI*|.
3491 DECLARE_ASN1_ITEM(NETSCAPE_SPKI)
3492
3493 // NETSCAPE_SPKI_new returns a newly-allocated, empty |NETSCAPE_SPKI| object, or
3494 // NULL on error.
3495 OPENSSL_EXPORT NETSCAPE_SPKI *NETSCAPE_SPKI_new(void);
3496
3497 // NETSCAPE_SPKI_free releases memory associated with |spki|.
3498 OPENSSL_EXPORT void NETSCAPE_SPKI_free(NETSCAPE_SPKI *spki);
3499
3500 // d2i_NETSCAPE_SPKI parses up to |len| bytes from |*inp| as a DER-encoded
3501 // SignedPublicKeyAndChallenge structure, as described in |d2i_SAMPLE|.
3502 OPENSSL_EXPORT NETSCAPE_SPKI *d2i_NETSCAPE_SPKI(NETSCAPE_SPKI **out,
3503 const uint8_t **inp, long len);
3504
3505 // i2d_NETSCAPE_SPKI marshals |spki| as a DER-encoded
3506 // SignedPublicKeyAndChallenge structure, as described in |i2d_SAMPLE|.
3507 OPENSSL_EXPORT int i2d_NETSCAPE_SPKI(const NETSCAPE_SPKI *spki, uint8_t **outp);
3508
3509 // NETSCAPE_SPKI_verify checks that |spki| has a valid signature by |pkey|. It
3510 // returns one if the signature is valid and zero otherwise.
3511 OPENSSL_EXPORT int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *spki, EVP_PKEY *pkey);
3512
3513 // NETSCAPE_SPKI_b64_decode decodes |len| bytes from |str| as a base64-encoded
3514 // SignedPublicKeyAndChallenge structure. It returns a newly-allocated
3515 // |NETSCAPE_SPKI| structure with the result, or NULL on error. If |len| is 0 or
3516 // negative, the length is calculated with |strlen| and |str| must be a
3517 // NUL-terminated C string.
3518 OPENSSL_EXPORT NETSCAPE_SPKI *NETSCAPE_SPKI_b64_decode(const char *str,
3519 ossl_ssize_t len);
3520
3521 // NETSCAPE_SPKI_b64_encode encodes |spki| as a base64-encoded
3522 // SignedPublicKeyAndChallenge structure. It returns a newly-allocated
3523 // NUL-terminated C string with the result, or NULL on error. The caller must
3524 // release the memory with |OPENSSL_free| when done.
3525 OPENSSL_EXPORT char *NETSCAPE_SPKI_b64_encode(NETSCAPE_SPKI *spki);
3526
3527 // NETSCAPE_SPKI_get_pubkey decodes and returns the public key in |spki| as an
3528 // |EVP_PKEY|, or NULL on error. The caller takes ownership of the resulting
3529 // pointer and must call |EVP_PKEY_free| when done.
3530 OPENSSL_EXPORT EVP_PKEY *NETSCAPE_SPKI_get_pubkey(const NETSCAPE_SPKI *spki);
3531
3532 // NETSCAPE_SPKI_set_pubkey sets |spki|'s public key to |pkey|. It returns one
3533 // on success or zero on error. This function does not take ownership of |pkey|,
3534 // so the caller may continue to manage its lifetime independently of |spki|.
3535 OPENSSL_EXPORT int NETSCAPE_SPKI_set_pubkey(NETSCAPE_SPKI *spki,
3536 EVP_PKEY *pkey);
3537
3538 // NETSCAPE_SPKI_sign signs |spki| with |pkey| and replaces the signature
3539 // algorithm and signature fields. It returns the length of the signature on
3540 // success and zero on error. This function uses digest algorithm |md|, or
3541 // |pkey|'s default if NULL. Other signing parameters use |pkey|'s defaults.
3542 OPENSSL_EXPORT int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *spki, EVP_PKEY *pkey,
3543 const EVP_MD *md);
3544
3545 // A Netscape_spkac_st, or |NETSCAPE_SPKAC|, represents a PublicKeyAndChallenge
3546 // structure. This type is misnamed. The full SPKAC includes the signature,
3547 // which is represented with the |NETSCAPE_SPKI| type.
3548 struct Netscape_spkac_st {
3549 X509_PUBKEY *pubkey;
3550 ASN1_IA5STRING *challenge;
3551 } /* NETSCAPE_SPKAC */;
3552
3553 // NETSCAPE_SPKAC is an |ASN1_ITEM| whose ASN.1 type is PublicKeyAndChallenge
3554 // and C type is |NETSCAPE_SPKAC*|.
3555 DECLARE_ASN1_ITEM(NETSCAPE_SPKAC)
3556
3557 // NETSCAPE_SPKAC_new returns a newly-allocated, empty |NETSCAPE_SPKAC| object,
3558 // or NULL on error.
3559 OPENSSL_EXPORT NETSCAPE_SPKAC *NETSCAPE_SPKAC_new(void);
3560
3561 // NETSCAPE_SPKAC_free releases memory associated with |spkac|.
3562 OPENSSL_EXPORT void NETSCAPE_SPKAC_free(NETSCAPE_SPKAC *spkac);
3563
3564 // d2i_NETSCAPE_SPKAC parses up to |len| bytes from |*inp| as a DER-encoded
3565 // PublicKeyAndChallenge structure, as described in |d2i_SAMPLE|.
3566 OPENSSL_EXPORT NETSCAPE_SPKAC *d2i_NETSCAPE_SPKAC(NETSCAPE_SPKAC **out,
3567 const uint8_t **inp,
3568 long len);
3569
3570 // i2d_NETSCAPE_SPKAC marshals |spkac| as a DER-encoded PublicKeyAndChallenge
3571 // structure, as described in |i2d_SAMPLE|.
3572 OPENSSL_EXPORT int i2d_NETSCAPE_SPKAC(const NETSCAPE_SPKAC *spkac,
3573 uint8_t **outp);
3574
3575 // NETSCAPE_SPKI_print prints out the contents of |spki| to |out|.
3576 OPENSSL_EXPORT int NETSCAPE_SPKI_print(BIO *out, NETSCAPE_SPKI *spki);
3577
3578
3579 // RSASSA-PSS Parameters.
3580 //
3581 // In X.509, RSASSA-PSS signatures and keys use a complex parameter structure,
3582 // defined in RFC 4055. The following functions are provided for compatibility
3583 // with some OpenSSL APIs relating to this. Use of RSASSA-PSS in X.509 is
3584 // discouraged. The parameters structure is very complex, and it takes more
3585 // bytes to merely encode parameters than an entire P-256 ECDSA signature.
3586
3587 // An rsa_pss_params_st, aka |RSA_PSS_PARAMS|, represents a parsed
3588 // RSASSA-PSS-params structure, as defined in (RFC 4055).
3589 struct rsa_pss_params_st {
3590 X509_ALGOR *hashAlgorithm;
3591 X509_ALGOR *maskGenAlgorithm;
3592 ASN1_INTEGER *saltLength;
3593 ASN1_INTEGER *trailerField;
3594 // OpenSSL caches the MGF hash on |RSA_PSS_PARAMS| in some cases. None of the
3595 // cases apply to BoringSSL, so this is always NULL, but Node expects the
3596 // field to be present.
3597 X509_ALGOR *maskHash;
3598 } /* RSA_PSS_PARAMS */;
3599
3600 // RSA_PSS_PARAMS is an |ASN1_ITEM| whose ASN.1 type is RSASSA-PSS-params (RFC
3601 // 4055) and C type is |RSA_PSS_PARAMS*|.
3602 DECLARE_ASN1_ITEM(RSA_PSS_PARAMS)
3603
3604 // RSA_PSS_PARAMS_new returns a new, empty |RSA_PSS_PARAMS|, or NULL on error.
3605 OPENSSL_EXPORT RSA_PSS_PARAMS *RSA_PSS_PARAMS_new(void);
3606
3607 // RSA_PSS_PARAMS_free releases memory associated with |params|.
3608 OPENSSL_EXPORT void RSA_PSS_PARAMS_free(RSA_PSS_PARAMS *params);
3609
3610 // d2i_RSA_PSS_PARAMS parses up to |len| bytes from |*inp| as a DER-encoded
3611 // RSASSA-PSS-params (RFC 4055), as described in |d2i_SAMPLE|.
3612 OPENSSL_EXPORT RSA_PSS_PARAMS *d2i_RSA_PSS_PARAMS(RSA_PSS_PARAMS **out,
3613 const uint8_t **inp,
3614 long len);
3615
3616 // i2d_RSA_PSS_PARAMS marshals |in| as a DER-encoded RSASSA-PSS-params (RFC
3617 // 4055), as described in |i2d_SAMPLE|.
3618 OPENSSL_EXPORT int i2d_RSA_PSS_PARAMS(const RSA_PSS_PARAMS *in, uint8_t **outp);
3619
3620
3621 // PKCS#8 private keys.
3622 //
3623 // The |PKCS8_PRIV_KEY_INFO| type represents a PKCS#8 PrivateKeyInfo (RFC 5208)
3624 // structure. This is analogous to SubjectPublicKeyInfo and uses the same
3625 // AlgorithmIdentifiers, but carries private keys and is not part of X.509
3626 // itself.
3627 //
3628 // TODO(davidben): Do these functions really belong in this header?
3629
3630 // PKCS8_PRIV_KEY_INFO is an |ASN1_ITEM| whose ASN.1 type is PrivateKeyInfo and
3631 // C type is |PKCS8_PRIV_KEY_INFO*|.
3632 DECLARE_ASN1_ITEM(PKCS8_PRIV_KEY_INFO)
3633
3634 // PKCS8_PRIV_KEY_INFO_new returns a newly-allocated, empty
3635 // |PKCS8_PRIV_KEY_INFO| object, or NULL on error.
3636 OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *PKCS8_PRIV_KEY_INFO_new(void);
3637
3638 // PKCS8_PRIV_KEY_INFO_free releases memory associated with |key|.
3639 OPENSSL_EXPORT void PKCS8_PRIV_KEY_INFO_free(PKCS8_PRIV_KEY_INFO *key);
3640
3641 // d2i_PKCS8_PRIV_KEY_INFO parses up to |len| bytes from |*inp| as a DER-encoded
3642 // PrivateKeyInfo, as described in |d2i_SAMPLE|.
3643 OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO(
3644 PKCS8_PRIV_KEY_INFO **out, const uint8_t **inp, long len);
3645
3646 // i2d_PKCS8_PRIV_KEY_INFO marshals |key| as a DER-encoded PrivateKeyInfo, as
3647 // described in |i2d_SAMPLE|.
3648 OPENSSL_EXPORT int i2d_PKCS8_PRIV_KEY_INFO(const PKCS8_PRIV_KEY_INFO *key,
3649 uint8_t **outp);
3650
3651 // EVP_PKCS82PKEY returns |p8| as a newly-allocated |EVP_PKEY|, or NULL if the
3652 // key was unsupported or could not be decoded. The caller must release the
3653 // result with |EVP_PKEY_free| when done.
3654 //
3655 // Use |EVP_parse_private_key| instead.
3656 OPENSSL_EXPORT EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8);
3657
3658 // EVP_PKEY2PKCS8 encodes |pkey| as a PKCS#8 PrivateKeyInfo (RFC 5208),
3659 // represented as a newly-allocated |PKCS8_PRIV_KEY_INFO|, or NULL on error. The
3660 // caller must release the result with |PKCS8_PRIV_KEY_INFO_free| when done.
3661 //
3662 // Use |EVP_marshal_private_key| instead.
3663 OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(const EVP_PKEY *pkey);
3664
3665
3666 // Algorithm and octet string pairs.
3667 //
3668 // The |X509_SIG| type represents an ASN.1 SEQUENCE type of an
3669 // AlgorithmIdentifier and an OCTET STRING. Although named |X509_SIG|, there is
3670 // no type in X.509 which matches this format. The two common types which do are
3671 // DigestInfo (RFC 2315 and RFC 8017), and EncryptedPrivateKeyInfo (RFC 5208).
3672
3673 // X509_SIG is an |ASN1_ITEM| whose ASN.1 type is the SEQUENCE described above
3674 // and C type is |X509_SIG*|.
3675 DECLARE_ASN1_ITEM(X509_SIG)
3676
3677 // X509_SIG_new returns a newly-allocated, empty |X509_SIG| object, or NULL on
3678 // error.
3679 OPENSSL_EXPORT X509_SIG *X509_SIG_new(void);
3680
3681 // X509_SIG_free releases memory associated with |key|.
3682 OPENSSL_EXPORT void X509_SIG_free(X509_SIG *key);
3683
3684 // d2i_X509_SIG parses up to |len| bytes from |*inp| as a DER-encoded algorithm
3685 // and octet string pair, as described in |d2i_SAMPLE|.
3686 OPENSSL_EXPORT X509_SIG *d2i_X509_SIG(X509_SIG **out, const uint8_t **inp,
3687 long len);
3688
3689 // i2d_X509_SIG marshals |sig| as a DER-encoded algorithm
3690 // and octet string pair, as described in |i2d_SAMPLE|.
3691 OPENSSL_EXPORT int i2d_X509_SIG(const X509_SIG *sig, uint8_t **outp);
3692
3693 // X509_SIG_get0 sets |*out_alg| and |*out_digest| to non-owning pointers to
3694 // |sig|'s algorithm and digest fields, respectively. Either |out_alg| and
3695 // |out_digest| may be NULL to skip those fields.
3696 OPENSSL_EXPORT void X509_SIG_get0(const X509_SIG *sig,
3697 const X509_ALGOR **out_alg,
3698 const ASN1_OCTET_STRING **out_digest);
3699
3700 // X509_SIG_getm behaves like |X509_SIG_get0| but returns mutable pointers.
3701 OPENSSL_EXPORT void X509_SIG_getm(X509_SIG *sig, X509_ALGOR **out_alg,
3702 ASN1_OCTET_STRING **out_digest);
3703
3704
3705 // Printing functions.
3706 //
3707 // The following functions output human-readable representations of
3708 // X.509-related structures. They should only be used for debugging or logging
3709 // and not parsed programmatically. In many cases, the outputs are ambiguous, so
3710 // attempting to parse them can lead to string injection vulnerabilities.
3711
3712 // The following flags control |X509_print_ex| and |X509_REQ_print_ex|. These
3713 // flags co-exist with |X509V3_EXT_*|, so avoid collisions when adding new ones.
3714
3715 // X509_FLAG_COMPAT disables all flags. It additionally causes names to be
3716 // printed with a 16-byte indent.
3717 #define X509_FLAG_COMPAT 0
3718
3719 // X509_FLAG_NO_HEADER skips a header identifying the type of object printed.
3720 #define X509_FLAG_NO_HEADER 1L
3721
3722 // X509_FLAG_NO_VERSION skips printing the X.509 version number.
3723 #define X509_FLAG_NO_VERSION (1L << 1)
3724
3725 // X509_FLAG_NO_SERIAL skips printing the serial number. It is ignored in
3726 // |X509_REQ_print_fp|.
3727 #define X509_FLAG_NO_SERIAL (1L << 2)
3728
3729 // X509_FLAG_NO_SIGNAME skips printing the signature algorithm in the
3730 // TBSCertificate. It is ignored in |X509_REQ_print_fp|.
3731 #define X509_FLAG_NO_SIGNAME (1L << 3)
3732
3733 // X509_FLAG_NO_ISSUER skips printing the issuer.
3734 #define X509_FLAG_NO_ISSUER (1L << 4)
3735
3736 // X509_FLAG_NO_VALIDITY skips printing the notBefore and notAfter times. It is
3737 // ignored in |X509_REQ_print_fp|.
3738 #define X509_FLAG_NO_VALIDITY (1L << 5)
3739
3740 // X509_FLAG_NO_SUBJECT skips printing the subject.
3741 #define X509_FLAG_NO_SUBJECT (1L << 6)
3742
3743 // X509_FLAG_NO_PUBKEY skips printing the public key.
3744 #define X509_FLAG_NO_PUBKEY (1L << 7)
3745
3746 // X509_FLAG_NO_EXTENSIONS skips printing the extension list. It is ignored in
3747 // |X509_REQ_print_fp|. CSRs instead have attributes, which is controlled by
3748 // |X509_FLAG_NO_ATTRIBUTES|.
3749 #define X509_FLAG_NO_EXTENSIONS (1L << 8)
3750
3751 // X509_FLAG_NO_SIGDUMP skips printing the signature and outer signature
3752 // algorithm.
3753 #define X509_FLAG_NO_SIGDUMP (1L << 9)
3754
3755 // X509_FLAG_NO_AUX skips printing auxiliary properties. (See |d2i_X509_AUX| and
3756 // related functions.)
3757 #define X509_FLAG_NO_AUX (1L << 10)
3758
3759 // X509_FLAG_NO_ATTRIBUTES skips printing CSR attributes. It does nothing for
3760 // certificates and CRLs.
3761 #define X509_FLAG_NO_ATTRIBUTES (1L << 11)
3762
3763 // X509_FLAG_NO_IDS skips printing the issuerUniqueID and subjectUniqueID in a
3764 // certificate. It is ignored in |X509_REQ_print_fp|.
3765 #define X509_FLAG_NO_IDS (1L << 12)
3766
3767 // The following flags control |X509_print_ex|, |X509_REQ_print_ex|,
3768 // |X509V3_EXT_print|, and |X509V3_extensions_print|. These flags coexist with
3769 // |X509_FLAG_*|, so avoid collisions when adding new ones.
3770
3771 // X509V3_EXT_UNKNOWN_MASK is a mask that determines how unknown extensions are
3772 // processed.
3773 #define X509V3_EXT_UNKNOWN_MASK (0xfL << 16)
3774
3775 // X509V3_EXT_DEFAULT causes unknown extensions or syntax errors to return
3776 // failure.
3777 #define X509V3_EXT_DEFAULT 0
3778
3779 // X509V3_EXT_ERROR_UNKNOWN causes unknown extensions or syntax errors to print
3780 // as "<Not Supported>" or "<Parse Error>", respectively.
3781 #define X509V3_EXT_ERROR_UNKNOWN (1L << 16)
3782
3783 // X509V3_EXT_PARSE_UNKNOWN is deprecated and behaves like
3784 // |X509V3_EXT_DUMP_UNKNOWN|.
3785 #define X509V3_EXT_PARSE_UNKNOWN (2L << 16)
3786
3787 // X509V3_EXT_DUMP_UNKNOWN causes unknown extensions to be displayed as a
3788 // hexdump.
3789 #define X509V3_EXT_DUMP_UNKNOWN (3L << 16)
3790
3791 // X509_print_ex writes a human-readable representation of |x| to |bp|. It
3792 // returns one on success and zero on error. |nmflags| is the flags parameter
3793 // for |X509_NAME_print_ex| when printing the subject and issuer. |cflag| should
3794 // be some combination of the |X509_FLAG_*| and |X509V3_EXT_*| constants.
3795 OPENSSL_EXPORT int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflag,
3796 unsigned long cflag);
3797
3798 // X509_print_ex_fp behaves like |X509_print_ex| but writes to |fp|.
3799 OPENSSL_EXPORT int X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag,
3800 unsigned long cflag);
3801
3802 // X509_print calls |X509_print_ex| with |XN_FLAG_COMPAT| and |X509_FLAG_COMPAT|
3803 // flags.
3804 OPENSSL_EXPORT int X509_print(BIO *bp, X509 *x);
3805
3806 // X509_print_fp behaves like |X509_print| but writes to |fp|.
3807 OPENSSL_EXPORT int X509_print_fp(FILE *fp, X509 *x);
3808
3809 // X509_CRL_print writes a human-readable representation of |x| to |bp|. It
3810 // returns one on success and zero on error.
3811 OPENSSL_EXPORT int X509_CRL_print(BIO *bp, X509_CRL *x);
3812
3813 // X509_CRL_print_fp behaves like |X509_CRL_print| but writes to |fp|.
3814 OPENSSL_EXPORT int X509_CRL_print_fp(FILE *fp, X509_CRL *x);
3815
3816 // X509_REQ_print_ex writes a human-readable representation of |x| to |bp|. It
3817 // returns one on success and zero on error. |nmflags| is the flags parameter
3818 // for |X509_NAME_print_ex|, when printing the subject. |cflag| should be some
3819 // combination of the |X509_FLAG_*| and |X509V3_EXT_*| constants.
3820 OPENSSL_EXPORT int X509_REQ_print_ex(BIO *bp, X509_REQ *x, unsigned long nmflag,
3821 unsigned long cflag);
3822
3823 // X509_REQ_print calls |X509_REQ_print_ex| with |XN_FLAG_COMPAT| and
3824 // |X509_FLAG_COMPAT| flags.
3825 OPENSSL_EXPORT int X509_REQ_print(BIO *bp, X509_REQ *req);
3826
3827 // X509_REQ_print_fp behaves like |X509_REQ_print| but writes to |fp|.
3828 OPENSSL_EXPORT int X509_REQ_print_fp(FILE *fp, X509_REQ *req);
3829
3830 // The following flags are control |X509_NAME_print_ex|. They must not collide
3831 // with |ASN1_STRFLGS_*|.
3832 //
3833 // TODO(davidben): This is far, far too many options and most of them are
3834 // useless. Trim this down.
3835
3836 // XN_FLAG_COMPAT prints with |X509_NAME_print|'s format and return value
3837 // convention.
3838 #define XN_FLAG_COMPAT 0ul
3839
3840 // XN_FLAG_SEP_MASK determines the separators to use between attributes.
3841 #define XN_FLAG_SEP_MASK (0xful << 16)
3842
3843 // XN_FLAG_SEP_COMMA_PLUS separates RDNs with "," and attributes within an RDN
3844 // with "+", as in RFC 2253.
3845 #define XN_FLAG_SEP_COMMA_PLUS (1ul << 16)
3846
3847 // XN_FLAG_SEP_CPLUS_SPC behaves like |XN_FLAG_SEP_COMMA_PLUS| but adds spaces
3848 // between the separators.
3849 #define XN_FLAG_SEP_CPLUS_SPC (2ul << 16)
3850
3851 // XN_FLAG_SEP_SPLUS_SPC separates RDNs with "; " and attributes within an RDN
3852 // with " + ".
3853 #define XN_FLAG_SEP_SPLUS_SPC (3ul << 16)
3854
3855 // XN_FLAG_SEP_MULTILINE prints each attribute on one line.
3856 #define XN_FLAG_SEP_MULTILINE (4ul << 16)
3857
3858 // XN_FLAG_DN_REV prints RDNs in reverse, from least significant to most
3859 // significant, as RFC 2253.
3860 #define XN_FLAG_DN_REV (1ul << 20)
3861
3862 // XN_FLAG_FN_MASK determines how attribute types are displayed.
3863 #define XN_FLAG_FN_MASK (0x3ul << 21)
3864
3865 // XN_FLAG_FN_SN uses the attribute type's short name, when available.
3866 #define XN_FLAG_FN_SN 0ul
3867
3868 // XN_FLAG_FN_LN uses the attribute type's long name, when available.
3869 #define XN_FLAG_FN_LN (1 << 21)
3870
3871 // XN_FLAG_FN_OID always prints attribute types as OIDs.
3872 #define XN_FLAG_FN_OID (2 << 21)
3873
3874 // XN_FLAG_FN_NONE skips printing field names.
3875 #define XN_FLAG_FN_NONE (3 << 21)
3876
3877 // XN_FLAG_SPC_EQ wraps the "=" operator with spaces when printing attributes.
3878 #define XN_FLAG_SPC_EQ (1ul << 23)
3879
3880 // XN_FLAG_DUMP_UNKNOWN_FIELDS causes unknown attribute types to be printed in
3881 // hex, as in RFC 2253.
3882 #define XN_FLAG_DUMP_UNKNOWN_FIELDS (1ul << 24)
3883
3884 // XN_FLAG_FN_ALIGN aligns attribute names to 10 characters if using short
3885 // names, and 25 characters if using long names.
3886 #define XN_FLAG_FN_ALIGN (1 << 25)
3887
3888 // XN_FLAG_RFC2253 prints like RFC 2253.
3889 #define XN_FLAG_RFC2253 \
3890 (ASN1_STRFLGS_RFC2253 | XN_FLAG_SEP_COMMA_PLUS | XN_FLAG_DN_REV | \
3891 XN_FLAG_FN_SN | XN_FLAG_DUMP_UNKNOWN_FIELDS)
3892
3893 // XN_FLAG_ONELINE prints a one-line representation of the name.
3894 #define XN_FLAG_ONELINE \
3895 (ASN1_STRFLGS_RFC2253 | ASN1_STRFLGS_ESC_QUOTE | XN_FLAG_SEP_CPLUS_SPC | \
3896 XN_FLAG_SPC_EQ | XN_FLAG_FN_SN)
3897
3898 // XN_FLAG_MULTILINE prints a multi-line representation of the name.
3899 #define XN_FLAG_MULTILINE \
3900 (ASN1_STRFLGS_ESC_CTRL | ASN1_STRFLGS_ESC_MSB | XN_FLAG_SEP_MULTILINE | \
3901 XN_FLAG_SPC_EQ | XN_FLAG_FN_LN | XN_FLAG_FN_ALIGN)
3902
3903 // X509_NAME_print_ex writes a human-readable representation of |nm| to |out|.
3904 // Each line of output is indented by |indent| spaces. It returns the number of
3905 // bytes written on success, and -1 on error. If |out| is NULL, it returns the
3906 // number of bytes it would have written but does not write anything. |flags|
3907 // should be some combination of |XN_FLAG_*| and |ASN1_STRFLGS_*| values and
3908 // determines the output. If unsure, use |XN_FLAG_RFC2253|.
3909 //
3910 // If |flags| is |XN_FLAG_COMPAT|, or zero, this function calls
3911 // |X509_NAME_print| instead. In that case, it returns one on success, rather
3912 // than the output length.
3913 OPENSSL_EXPORT int X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent,
3914 unsigned long flags);
3915
3916 // X509_NAME_print prints a human-readable representation of |name| to |bp|. It
3917 // returns one on success and zero on error. |obase| is ignored.
3918 //
3919 // This function outputs a legacy format that does not correctly handle string
3920 // encodings and other cases. Prefer |X509_NAME_print_ex| if printing a name for
3921 // debugging purposes.
3922 OPENSSL_EXPORT int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase);
3923
3924 // X509_NAME_oneline writes a human-readable representation to |name| to a
3925 // buffer as a NUL-terminated C string.
3926 //
3927 // If |buf| is NULL, returns a newly-allocated buffer containing the result on
3928 // success, or NULL on error. The buffer must be released with |OPENSSL_free|
3929 // when done.
3930 //
3931 // If |buf| is non-NULL, at most |size| bytes of output are written to |buf|
3932 // instead. |size| includes the trailing NUL. The function then returns |buf| on
3933 // success or NULL on error. If the output does not fit in |size| bytes, the
3934 // output is silently truncated at an attribute boundary.
3935 //
3936 // This function outputs a legacy format that does not correctly handle string
3937 // encodings and other cases. Prefer |X509_NAME_print_ex| if printing a name for
3938 // debugging purposes.
3939 OPENSSL_EXPORT char *X509_NAME_oneline(const X509_NAME *name, char *buf,
3940 int size);
3941
3942 // X509_NAME_print_ex_fp behaves like |X509_NAME_print_ex| but writes to |fp|.
3943 OPENSSL_EXPORT int X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm,
3944 int indent, unsigned long flags);
3945
3946 // X509_signature_dump writes a human-readable representation of |sig| to |bio|,
3947 // indented with |indent| spaces. It returns one on success and zero on error.
3948 OPENSSL_EXPORT int X509_signature_dump(BIO *bio, const ASN1_STRING *sig,
3949 int indent);
3950
3951 // X509_signature_print writes a human-readable representation of |alg| and
3952 // |sig| to |bio|. It returns one on success and zero on error.
3953 OPENSSL_EXPORT int X509_signature_print(BIO *bio, const X509_ALGOR *alg,
3954 const ASN1_STRING *sig);
3955
3956 // X509V3_EXT_print prints a human-readable representation of |ext| to out. It
3957 // returns one on success and zero on error. The output is indented by |indent|
3958 // spaces. |flag| is one of the |X509V3_EXT_*| constants and controls printing
3959 // of unknown extensions and syntax errors.
3960 //
3961 // WARNING: Although some applications programmatically parse the output of this
3962 // function to process X.509 extensions, this is not safe. In many cases, the
3963 // outputs are ambiguous to attempting to parse them can lead to string
3964 // injection vulnerabilities. These functions should only be used for debugging
3965 // or logging.
3966 OPENSSL_EXPORT int X509V3_EXT_print(BIO *out, const X509_EXTENSION *ext,
3967 unsigned long flag, int indent);
3968
3969 // X509V3_EXT_print_fp behaves like |X509V3_EXT_print| but writes to a |FILE|
3970 // instead of a |BIO|.
3971 OPENSSL_EXPORT int X509V3_EXT_print_fp(FILE *out, const X509_EXTENSION *ext,
3972 int flag, int indent);
3973
3974 // X509V3_extensions_print prints |title|, followed by a human-readable
3975 // representation of |exts| to |out|. It returns one on success and zero on
3976 // error. The output is indented by |indent| spaces. |flag| is one of the
3977 // |X509V3_EXT_*| constants and controls printing of unknown extensions and
3978 // syntax errors.
3979 OPENSSL_EXPORT int X509V3_extensions_print(BIO *out, const char *title,
3980 const STACK_OF(X509_EXTENSION) *exts,
3981 unsigned long flag, int indent);
3982
3983 // GENERAL_NAME_print prints a human-readable representation of |gen| to |out|.
3984 // It returns one on success and zero on error.
3985 //
3986 // TODO(davidben): Actually, it just returns one and doesn't check for I/O or
3987 // allocation errors. But it should return zero on error.
3988 OPENSSL_EXPORT int GENERAL_NAME_print(BIO *out, const GENERAL_NAME *gen);
3989
3990
3991 // Convenience functions.
3992
3993 // X509_pubkey_digest hashes the contents of the BIT STRING in |x509|'s
3994 // subjectPublicKeyInfo field with |md| and writes the result to |out|.
3995 // |EVP_MD_CTX_size| bytes are written, which is at most |EVP_MAX_MD_SIZE|. If
3996 // |out_len| is not NULL, |*out_len| is set to the number of bytes written. This
3997 // function returns one on success and zero on error.
3998 //
3999 // This hash omits the BIT STRING tag, length, and number of unused bits. It
4000 // also omits the AlgorithmIdentifier which describes the key type. It
4001 // corresponds to the OCSP KeyHash definition and is not suitable for other
4002 // purposes.
4003 OPENSSL_EXPORT int X509_pubkey_digest(const X509 *x509, const EVP_MD *md,
4004 uint8_t *out, unsigned *out_len);
4005
4006 // X509_digest hashes |x509|'s DER encoding with |md| and writes the result to
4007 // |out|. |EVP_MD_CTX_size| bytes are written, which is at most
4008 // |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number
4009 // of bytes written. This function returns one on success and zero on error.
4010 // Note this digest covers the entire certificate, not just the signed portion.
4011 OPENSSL_EXPORT int X509_digest(const X509 *x509, const EVP_MD *md, uint8_t *out,
4012 unsigned *out_len);
4013
4014 // X509_CRL_digest hashes |crl|'s DER encoding with |md| and writes the result
4015 // to |out|. |EVP_MD_CTX_size| bytes are written, which is at most
4016 // |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number
4017 // of bytes written. This function returns one on success and zero on error.
4018 // Note this digest covers the entire CRL, not just the signed portion.
4019 OPENSSL_EXPORT int X509_CRL_digest(const X509_CRL *crl, const EVP_MD *md,
4020 uint8_t *out, unsigned *out_len);
4021
4022 // X509_REQ_digest hashes |req|'s DER encoding with |md| and writes the result
4023 // to |out|. |EVP_MD_CTX_size| bytes are written, which is at most
4024 // |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number
4025 // of bytes written. This function returns one on success and zero on error.
4026 // Note this digest covers the entire certificate request, not just the signed
4027 // portion.
4028 OPENSSL_EXPORT int X509_REQ_digest(const X509_REQ *req, const EVP_MD *md,
4029 uint8_t *out, unsigned *out_len);
4030
4031 // X509_NAME_digest hashes |name|'s DER encoding with |md| and writes the result
4032 // to |out|. |EVP_MD_CTX_size| bytes are written, which is at most
4033 // |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number
4034 // of bytes written. This function returns one on success and zero on error.
4035 OPENSSL_EXPORT int X509_NAME_digest(const X509_NAME *name, const EVP_MD *md,
4036 uint8_t *out, unsigned *out_len);
4037
4038 // The following functions behave like the corresponding unsuffixed |d2i_*|
4039 // functions, but read the result from |bp| instead. Callers using these
4040 // functions with memory |BIO|s to parse structures already in memory should use
4041 // |d2i_*| instead.
4042 OPENSSL_EXPORT X509 *d2i_X509_bio(BIO *bp, X509 **x509);
4043 OPENSSL_EXPORT X509_CRL *d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl);
4044 OPENSSL_EXPORT X509_REQ *d2i_X509_REQ_bio(BIO *bp, X509_REQ **req);
4045 OPENSSL_EXPORT RSA *d2i_RSAPrivateKey_bio(BIO *bp, RSA **rsa);
4046 OPENSSL_EXPORT RSA *d2i_RSAPublicKey_bio(BIO *bp, RSA **rsa);
4047 OPENSSL_EXPORT RSA *d2i_RSA_PUBKEY_bio(BIO *bp, RSA **rsa);
4048 OPENSSL_EXPORT DSA *d2i_DSA_PUBKEY_bio(BIO *bp, DSA **dsa);
4049 OPENSSL_EXPORT DSA *d2i_DSAPrivateKey_bio(BIO *bp, DSA **dsa);
4050 OPENSSL_EXPORT EC_KEY *d2i_EC_PUBKEY_bio(BIO *bp, EC_KEY **eckey);
4051 OPENSSL_EXPORT EC_KEY *d2i_ECPrivateKey_bio(BIO *bp, EC_KEY **eckey);
4052 OPENSSL_EXPORT X509_SIG *d2i_PKCS8_bio(BIO *bp, X509_SIG **p8);
4053 OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_bio(
4054 BIO *bp, PKCS8_PRIV_KEY_INFO **p8inf);
4055 OPENSSL_EXPORT EVP_PKEY *d2i_PUBKEY_bio(BIO *bp, EVP_PKEY **a);
4056 OPENSSL_EXPORT DH *d2i_DHparams_bio(BIO *bp, DH **dh);
4057
4058 // d2i_PrivateKey_bio behaves like |d2i_AutoPrivateKey|, but reads from |bp|
4059 // instead.
4060 OPENSSL_EXPORT EVP_PKEY *d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a);
4061
4062 // The following functions behave like the corresponding unsuffixed |i2d_*|
4063 // functions, but write the result to |bp|. They return one on success and zero
4064 // on error. Callers using them with memory |BIO|s to encode structures to
4065 // memory should use |i2d_*| directly instead.
4066 OPENSSL_EXPORT int i2d_X509_bio(BIO *bp, X509 *x509);
4067 OPENSSL_EXPORT int i2d_X509_CRL_bio(BIO *bp, X509_CRL *crl);
4068 OPENSSL_EXPORT int i2d_X509_REQ_bio(BIO *bp, X509_REQ *req);
4069 OPENSSL_EXPORT int i2d_RSAPrivateKey_bio(BIO *bp, RSA *rsa);
4070 OPENSSL_EXPORT int i2d_RSAPublicKey_bio(BIO *bp, RSA *rsa);
4071 OPENSSL_EXPORT int i2d_RSA_PUBKEY_bio(BIO *bp, RSA *rsa);
4072 OPENSSL_EXPORT int i2d_DSA_PUBKEY_bio(BIO *bp, DSA *dsa);
4073 OPENSSL_EXPORT int i2d_DSAPrivateKey_bio(BIO *bp, DSA *dsa);
4074 OPENSSL_EXPORT int i2d_EC_PUBKEY_bio(BIO *bp, EC_KEY *eckey);
4075 OPENSSL_EXPORT int i2d_ECPrivateKey_bio(BIO *bp, EC_KEY *eckey);
4076 OPENSSL_EXPORT int i2d_PKCS8_bio(BIO *bp, X509_SIG *p8);
4077 OPENSSL_EXPORT int i2d_PKCS8_PRIV_KEY_INFO_bio(BIO *bp,
4078 PKCS8_PRIV_KEY_INFO *p8inf);
4079 OPENSSL_EXPORT int i2d_PrivateKey_bio(BIO *bp, EVP_PKEY *pkey);
4080 OPENSSL_EXPORT int i2d_PUBKEY_bio(BIO *bp, EVP_PKEY *pkey);
4081 OPENSSL_EXPORT int i2d_DHparams_bio(BIO *bp, const DH *dh);
4082
4083 // i2d_PKCS8PrivateKeyInfo_bio encodes |key| as a PKCS#8 PrivateKeyInfo
4084 // structure (see |EVP_marshal_private_key|) and writes the result to |bp|. It
4085 // returns one on success and zero on error.
4086 OPENSSL_EXPORT int i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, EVP_PKEY *key);
4087
4088 // The following functions behave like the corresponding |d2i_*_bio| functions,
4089 // but read from |fp| instead.
4090 OPENSSL_EXPORT X509 *d2i_X509_fp(FILE *fp, X509 **x509);
4091 OPENSSL_EXPORT X509_CRL *d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl);
4092 OPENSSL_EXPORT X509_REQ *d2i_X509_REQ_fp(FILE *fp, X509_REQ **req);
4093 OPENSSL_EXPORT RSA *d2i_RSAPrivateKey_fp(FILE *fp, RSA **rsa);
4094 OPENSSL_EXPORT RSA *d2i_RSAPublicKey_fp(FILE *fp, RSA **rsa);
4095 OPENSSL_EXPORT RSA *d2i_RSA_PUBKEY_fp(FILE *fp, RSA **rsa);
4096 OPENSSL_EXPORT DSA *d2i_DSA_PUBKEY_fp(FILE *fp, DSA **dsa);
4097 OPENSSL_EXPORT DSA *d2i_DSAPrivateKey_fp(FILE *fp, DSA **dsa);
4098 OPENSSL_EXPORT EC_KEY *d2i_EC_PUBKEY_fp(FILE *fp, EC_KEY **eckey);
4099 OPENSSL_EXPORT EC_KEY *d2i_ECPrivateKey_fp(FILE *fp, EC_KEY **eckey);
4100 OPENSSL_EXPORT X509_SIG *d2i_PKCS8_fp(FILE *fp, X509_SIG **p8);
4101 OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_fp(
4102 FILE *fp, PKCS8_PRIV_KEY_INFO **p8inf);
4103 OPENSSL_EXPORT EVP_PKEY *d2i_PrivateKey_fp(FILE *fp, EVP_PKEY **a);
4104 OPENSSL_EXPORT EVP_PKEY *d2i_PUBKEY_fp(FILE *fp, EVP_PKEY **a);
4105
4106 // The following functions behave like the corresponding |i2d_*_bio| functions,
4107 // but write to |fp| instead.
4108 OPENSSL_EXPORT int i2d_X509_fp(FILE *fp, X509 *x509);
4109 OPENSSL_EXPORT int i2d_X509_CRL_fp(FILE *fp, X509_CRL *crl);
4110 OPENSSL_EXPORT int i2d_X509_REQ_fp(FILE *fp, X509_REQ *req);
4111 OPENSSL_EXPORT int i2d_RSAPrivateKey_fp(FILE *fp, RSA *rsa);
4112 OPENSSL_EXPORT int i2d_RSAPublicKey_fp(FILE *fp, RSA *rsa);
4113 OPENSSL_EXPORT int i2d_RSA_PUBKEY_fp(FILE *fp, RSA *rsa);
4114 OPENSSL_EXPORT int i2d_DSA_PUBKEY_fp(FILE *fp, DSA *dsa);
4115 OPENSSL_EXPORT int i2d_DSAPrivateKey_fp(FILE *fp, DSA *dsa);
4116 OPENSSL_EXPORT int i2d_EC_PUBKEY_fp(FILE *fp, EC_KEY *eckey);
4117 OPENSSL_EXPORT int i2d_ECPrivateKey_fp(FILE *fp, EC_KEY *eckey);
4118 OPENSSL_EXPORT int i2d_PKCS8_fp(FILE *fp, X509_SIG *p8);
4119 OPENSSL_EXPORT int i2d_PKCS8_PRIV_KEY_INFO_fp(FILE *fp,
4120 PKCS8_PRIV_KEY_INFO *p8inf);
4121 OPENSSL_EXPORT int i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, EVP_PKEY *key);
4122 OPENSSL_EXPORT int i2d_PrivateKey_fp(FILE *fp, EVP_PKEY *pkey);
4123 OPENSSL_EXPORT int i2d_PUBKEY_fp(FILE *fp, EVP_PKEY *pkey);
4124
4125 // X509_find_by_issuer_and_serial returns the first |X509| in |sk| whose issuer
4126 // and serial are |name| and |serial|, respectively. If no match is found, it
4127 // returns NULL.
4128 OPENSSL_EXPORT X509 *X509_find_by_issuer_and_serial(const STACK_OF(X509) *sk,
4129 X509_NAME *name,
4130 const ASN1_INTEGER *serial);
4131
4132 // X509_find_by_subject returns the first |X509| in |sk| whose subject is
4133 // |name|. If no match is found, it returns NULL.
4134 OPENSSL_EXPORT X509 *X509_find_by_subject(const STACK_OF(X509) *sk,
4135 X509_NAME *name);
4136
4137 // X509_cmp_time compares |s| against |*t|. On success, it returns a negative
4138 // number if |s| <= |*t| and a positive number if |s| > |*t|. On error, it
4139 // returns zero. If |t| is NULL, it uses the current time instead of |*t|.
4140 //
4141 // WARNING: Unlike most comparison functions, this function returns zero on
4142 // error, not equality.
4143 OPENSSL_EXPORT int X509_cmp_time(const ASN1_TIME *s, const time_t *t);
4144
4145 // X509_cmp_time_posix compares |s| against |t|. On success, it returns a
4146 // negative number if |s| <= |t| and a positive number if |s| > |t|. On error,
4147 // it returns zero.
4148 //
4149 // WARNING: Unlike most comparison functions, this function returns zero on
4150 // error, not equality.
4151 OPENSSL_EXPORT int X509_cmp_time_posix(const ASN1_TIME *s, int64_t t);
4152
4153 // X509_cmp_current_time behaves like |X509_cmp_time| but compares |s| against
4154 // the current time.
4155 OPENSSL_EXPORT int X509_cmp_current_time(const ASN1_TIME *s);
4156
4157 // X509_time_adj calls |X509_time_adj_ex| with |offset_day| equal to zero.
4158 OPENSSL_EXPORT ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec,
4159 const time_t *t);
4160
4161 // X509_time_adj_ex behaves like |ASN1_TIME_adj|, but adds an offset to |*t|. If
4162 // |t| is NULL, it uses the current time instead of |*t|.
4163 OPENSSL_EXPORT ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s, int offset_day,
4164 long offset_sec, const time_t *t);
4165
4166 // X509_gmtime_adj behaves like |X509_time_adj_ex| but adds |offset_sec| to the
4167 // current time.
4168 OPENSSL_EXPORT ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long offset_sec);
4169
4170 // X509_issuer_name_cmp behaves like |X509_NAME_cmp|, but compares |a| and |b|'s
4171 // issuer names.
4172 OPENSSL_EXPORT int X509_issuer_name_cmp(const X509 *a, const X509 *b);
4173
4174 // X509_subject_name_cmp behaves like |X509_NAME_cmp|, but compares |a| and
4175 // |b|'s subject names.
4176 OPENSSL_EXPORT int X509_subject_name_cmp(const X509 *a, const X509 *b);
4177
4178 // X509_CRL_cmp behaves like |X509_NAME_cmp|, but compares |a| and |b|'s
4179 // issuer names.
4180 //
4181 // WARNING: This function is misnamed. It does not compare other parts of the
4182 // CRL, only the issuer fields using |X509_NAME_cmp|.
4183 OPENSSL_EXPORT int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b);
4184
4185 // X509_issuer_name_hash returns the hash of |x509|'s issuer name with
4186 // |X509_NAME_hash|.
4187 //
4188 // This hash is specific to the |X509_LOOKUP_add_dir| filesystem format and is
4189 // not suitable for general-purpose X.509 name processing. It is very short, so
4190 // there will be hash collisions. It also depends on an OpenSSL-specific
4191 // canonicalization process.
4192 OPENSSL_EXPORT uint32_t X509_issuer_name_hash(X509 *x509);
4193
4194 // X509_subject_name_hash returns the hash of |x509|'s subject name with
4195 // |X509_NAME_hash|.
4196 //
4197 // This hash is specific to the |X509_LOOKUP_add_dir| filesystem format and is
4198 // not suitable for general-purpose X.509 name processing. It is very short, so
4199 // there will be hash collisions. It also depends on an OpenSSL-specific
4200 // canonicalization process.
4201 OPENSSL_EXPORT uint32_t X509_subject_name_hash(X509 *x509);
4202
4203 // X509_issuer_name_hash_old returns the hash of |x509|'s issuer name with
4204 // |X509_NAME_hash_old|.
4205 //
4206 // This hash is specific to the |X509_LOOKUP_add_dir| filesystem format and is
4207 // not suitable for general-purpose X.509 name processing. It is very short, so
4208 // there will be hash collisions.
4209 OPENSSL_EXPORT uint32_t X509_issuer_name_hash_old(X509 *x509);
4210
4211 // X509_subject_name_hash_old returns the hash of |x509|'s usjbect name with
4212 // |X509_NAME_hash_old|.
4213 //
4214 // This hash is specific to the |X509_LOOKUP_add_dir| filesystem format and is
4215 // not suitable for general-purpose X.509 name processing. It is very short, so
4216 // there will be hash collisions.
4217 OPENSSL_EXPORT uint32_t X509_subject_name_hash_old(X509 *x509);
4218
4219
4220 // ex_data functions.
4221 //
4222 // See |ex_data.h| for details.
4223
4224 OPENSSL_EXPORT int X509_get_ex_new_index(long argl, void *argp,
4225 CRYPTO_EX_unused *unused,
4226 CRYPTO_EX_dup *dup_unused,
4227 CRYPTO_EX_free *free_func);
4228 OPENSSL_EXPORT int X509_set_ex_data(X509 *r, int idx, void *arg);
4229 OPENSSL_EXPORT void *X509_get_ex_data(X509 *r, int idx);
4230
4231 OPENSSL_EXPORT int X509_STORE_CTX_get_ex_new_index(long argl, void *argp,
4232 CRYPTO_EX_unused *unused,
4233 CRYPTO_EX_dup *dup_unused,
4234 CRYPTO_EX_free *free_func);
4235 OPENSSL_EXPORT int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx,
4236 void *data);
4237 OPENSSL_EXPORT void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx);
4238
4239 OPENSSL_EXPORT int X509_STORE_get_ex_new_index(long argl, void *argp,
4240 CRYPTO_EX_unused *unused,
4241 CRYPTO_EX_dup *dup_unused,
4242 CRYPTO_EX_free *free_func);
4243 OPENSSL_EXPORT int X509_STORE_set_ex_data(X509_STORE *ctx, int idx, void *data);
4244 OPENSSL_EXPORT void *X509_STORE_get_ex_data(X509_STORE *ctx, int idx);
4245
4246 #define X509_STORE_CTX_set_app_data(ctx, data) \
4247 X509_STORE_CTX_set_ex_data(ctx, 0, data)
4248 #define X509_STORE_CTX_get_app_data(ctx) X509_STORE_CTX_get_ex_data(ctx, 0)
4249
4250 // Hashing and signing ASN.1 structures.
4251
4252 // ASN1_digest serializes |data| with |i2d| and then hashes the result with
4253 // |type|. On success, it returns one, writes the digest to |md|, and sets
4254 // |*len| to the digest length if non-NULL. On error, it returns zero.
4255 //
4256 // |EVP_MD_CTX_size| bytes are written, which is at most |EVP_MAX_MD_SIZE|. The
4257 // buffer must have sufficient space for this output.
4258 OPENSSL_EXPORT int ASN1_digest(i2d_of_void *i2d, const EVP_MD *type, char *data,
4259 unsigned char *md, unsigned int *len);
4260
4261 // ASN1_item_digest serializes |data| with |it| and then hashes the result with
4262 // |type|. On success, it returns one, writes the digest to |md|, and sets
4263 // |*len| to the digest length if non-NULL. On error, it returns zero.
4264 //
4265 // |EVP_MD_CTX_size| bytes are written, which is at most |EVP_MAX_MD_SIZE|. The
4266 // buffer must have sufficient space for this output.
4267 //
4268 // WARNING: |data| must be a pointer with the same type as |it|'s corresponding
4269 // C type. Using the wrong type is a potentially exploitable memory error.
4270 OPENSSL_EXPORT int ASN1_item_digest(const ASN1_ITEM *it, const EVP_MD *type,
4271 void *data, unsigned char *md,
4272 unsigned int *len);
4273
4274 // ASN1_item_verify serializes |data| with |it| and then verifies |signature| is
4275 // a valid signature for the result with |algor1| and |pkey|. It returns one on
4276 // success and zero on error. The signature and algorithm are interpreted as in
4277 // X.509.
4278 //
4279 // WARNING: |data| must be a pointer with the same type as |it|'s corresponding
4280 // C type. Using the wrong type is a potentially exploitable memory error.
4281 OPENSSL_EXPORT int ASN1_item_verify(const ASN1_ITEM *it,
4282 const X509_ALGOR *algor1,
4283 const ASN1_BIT_STRING *signature,
4284 void *data, EVP_PKEY *pkey);
4285
4286 // ASN1_item_sign serializes |data| with |it| and then signs the result with
4287 // the private key |pkey|. It returns the length of the signature on success and
4288 // zero on error. On success, it writes the signature to |signature| and the
4289 // signature algorithm to each of |algor1| and |algor2|. Either of |algor1| or
4290 // |algor2| may be NULL to ignore them. This function uses digest algorithm
4291 // |md|, or |pkey|'s default if NULL. Other signing parameters use |pkey|'s
4292 // defaults. To customize them, use |ASN1_item_sign_ctx|.
4293 //
4294 // WARNING: |data| must be a pointer with the same type as |it|'s corresponding
4295 // C type. Using the wrong type is a potentially exploitable memory error.
4296 OPENSSL_EXPORT int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1,
4297 X509_ALGOR *algor2,
4298 ASN1_BIT_STRING *signature, void *data,
4299 EVP_PKEY *pkey, const EVP_MD *type);
4300
4301 // ASN1_item_sign_ctx behaves like |ASN1_item_sign| except the signature is
4302 // signed with |ctx|, |ctx|, which must have been initialized with
4303 // |EVP_DigestSignInit|. The caller should configure the corresponding
4304 // |EVP_PKEY_CTX| with any additional parameters before calling this function.
4305 //
4306 // On success or failure, this function mutates |ctx| and resets it to the empty
4307 // state. Caller should not rely on its contents after the function returns.
4308 //
4309 // WARNING: |data| must be a pointer with the same type as |it|'s corresponding
4310 // C type. Using the wrong type is a potentially exploitable memory error.
4311 OPENSSL_EXPORT int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1,
4312 X509_ALGOR *algor2,
4313 ASN1_BIT_STRING *signature, void *asn,
4314 EVP_MD_CTX *ctx);
4315
4316
4317 // Verification internals.
4318 //
4319 // The following functions expose portions of certificate validation. They are
4320 // exported for compatibility with existing callers, or to support some obscure
4321 // use cases. Most callers, however, will not need these functions and should
4322 // instead use |X509_STORE_CTX| APIs.
4323
4324 // X509_supported_extension returns one if |ex| is a critical X.509 certificate
4325 // extension, supported by |X509_verify_cert|, and zero otherwise.
4326 //
4327 // Note this function only reports certificate extensions (as opposed to CRL or
4328 // CRL extensions), and only extensions that are expected to be marked critical.
4329 // Additionally, |X509_verify_cert| checks for unsupported critical extensions
4330 // internally, so most callers will not need to call this function separately.
4331 OPENSSL_EXPORT int X509_supported_extension(const X509_EXTENSION *ex);
4332
4333 // X509_check_ca returns one if |x509| may be considered a CA certificate,
4334 // according to basic constraints and key usage extensions. Otherwise, it
4335 // returns zero. If |x509| is an X509v1 certificate, and thus has no extensions,
4336 // it is considered eligible.
4337 //
4338 // This function returning one does not indicate that |x509| is trusted, only
4339 // that it is eligible to be a CA.
4340 //
4341 // TODO(crbug.com/boringssl/407): |x509| should be const.
4342 OPENSSL_EXPORT int X509_check_ca(X509 *x509);
4343
4344 // X509_check_issued checks if |issuer| and |subject|'s name, authority key
4345 // identifier, and key usage fields allow |issuer| to have issued |subject|. It
4346 // returns |X509_V_OK| on success and an |X509_V_ERR_*| value otherwise.
4347 //
4348 // This function does not check the signature on |subject|. Rather, it is
4349 // intended to prune the set of possible issuer certificates during
4350 // path-building.
4351 //
4352 // TODO(crbug.com/boringssl/407): Both parameters should be const.
4353 OPENSSL_EXPORT int X509_check_issued(X509 *issuer, X509 *subject);
4354
4355 // NAME_CONSTRAINTS_check checks if |x509| satisfies name constraints in |nc|.
4356 // It returns |X509_V_OK| on success and some |X509_V_ERR_*| constant on error.
4357 //
4358 // TODO(crbug.com/boringssl/407): Both parameters should be const.
4359 OPENSSL_EXPORT int NAME_CONSTRAINTS_check(X509 *x509, NAME_CONSTRAINTS *nc);
4360
4361 // X509_check_host checks if |x509| matches the DNS name |chk|. It returns one
4362 // on match, zero on mismatch, or a negative number on error. |flags| should be
4363 // some combination of |X509_CHECK_FLAG_*| and modifies the behavior. On match,
4364 // if |out_peername| is non-NULL, it additionally sets |*out_peername| to a
4365 // newly-allocated, NUL-terminated string containing the DNS name or wildcard in
4366 // the certificate which matched. The caller must then free |*out_peername| with
4367 // |OPENSSL_free| when done.
4368 //
4369 // By default, both subject alternative names and the subject's common name
4370 // attribute are checked. The latter has long been deprecated, so callers should
4371 // include |X509_CHECK_FLAG_NEVER_CHECK_SUBJECT| in |flags| to use the standard
4372 // behavior. https://crbug.com/boringssl/464 tracks fixing the default.
4373 //
4374 // This function does not check if |x509| is a trusted certificate, only if,
4375 // were it trusted, it would match |chk|.
4376 //
4377 // WARNING: This function differs from the usual calling convention and may
4378 // return either 0 or a negative number on error.
4379 //
4380 // TODO(davidben): Make the error case also return zero.
4381 OPENSSL_EXPORT int X509_check_host(const X509 *x509, const char *chk,
4382 size_t chklen, unsigned int flags,
4383 char **out_peername);
4384
4385 // X509_check_email checks if |x509| matches the email address |chk|. It returns
4386 // one on match, zero on mismatch, or a negative number on error. |flags| should
4387 // be some combination of |X509_CHECK_FLAG_*| and modifies the behavior.
4388 //
4389 // By default, both subject alternative names and the subject's email address
4390 // attribute are checked. The |X509_CHECK_FLAG_NEVER_CHECK_SUBJECT| flag may be
4391 // used to change this behavior.
4392 //
4393 // This function does not check if |x509| is a trusted certificate, only if,
4394 // were it trusted, it would match |chk|.
4395 //
4396 // WARNING: This function differs from the usual calling convention and may
4397 // return either 0 or a negative number on error.
4398 //
4399 // TODO(davidben): Make the error case also return zero.
4400 OPENSSL_EXPORT int X509_check_email(const X509 *x509, const char *chk,
4401 size_t chklen, unsigned int flags);
4402
4403 // X509_check_ip checks if |x509| matches the IP address |chk|. The IP address
4404 // is represented in byte form and should be 4 bytes for an IPv4 address and 16
4405 // bytes for an IPv6 address. It returns one on match, zero on mismatch, or a
4406 // negative number on error. |flags| should be some combination of
4407 // |X509_CHECK_FLAG_*| and modifies the behavior.
4408 //
4409 // This function does not check if |x509| is a trusted certificate, only if,
4410 // were it trusted, it would match |chk|.
4411 //
4412 // WARNING: This function differs from the usual calling convention and may
4413 // return either 0 or a negative number on error.
4414 //
4415 // TODO(davidben): Make the error case also return zero.
4416 OPENSSL_EXPORT int X509_check_ip(const X509 *x509, const uint8_t *chk,
4417 size_t chklen, unsigned int flags);
4418
4419 // X509_check_ip_asc behaves like |X509_check_ip| except the IP address is
4420 // specified in textual form in |ipasc|.
4421 //
4422 // WARNING: This function differs from the usual calling convention and may
4423 // return either 0 or a negative number on error.
4424 //
4425 // TODO(davidben): Make the error case also return zero.
4426 OPENSSL_EXPORT int X509_check_ip_asc(const X509 *x509, const char *ipasc,
4427 unsigned int flags);
4428
4429 // X509_STORE_CTX_get1_issuer looks up a candidate trusted issuer for |x509| out
4430 // of |ctx|'s |X509_STORE|, based on the criteria in |X509_check_issued|. If one
4431 // was found, it returns one and sets |*out_issuer| to the issuer. The caller
4432 // must release |*out_issuer| with |X509_free| when done. If none was found, it
4433 // returns zero and leaves |*out_issuer| unchanged.
4434 //
4435 // This function only searches for trusted issuers. It does not consider
4436 // untrusted intermediates passed in to |X509_STORE_CTX_init|.
4437 //
4438 // TODO(crbug.com/boringssl/407): |x509| should be const.
4439 OPENSSL_EXPORT int X509_STORE_CTX_get1_issuer(X509 **out_issuer,
4440 X509_STORE_CTX *ctx, X509 *x509);
4441
4442 // X509_check_purpose performs checks if |x509|'s basic constraints, key usage,
4443 // and extended key usage extensions for the specified purpose. |purpose| should
4444 // be one of |X509_PURPOSE_*| constants. See |X509_VERIFY_PARAM_set_purpose| for
4445 // details. It returns one if |x509|'s extensions are consistent with |purpose|
4446 // and zero otherwise. If |ca| is non-zero, |x509| is checked as a CA
4447 // certificate. Otherwise, it is checked as an end-entity certificate.
4448 //
4449 // If |purpose| is -1, this function performs no purpose checks, but it parses
4450 // some extensions in |x509| and may return zero on syntax error. Historically,
4451 // callers primarily used this function to trigger this parsing, but this is no
4452 // longer necessary. Functions acting on |X509| will internally parse as needed.
4453 OPENSSL_EXPORT int X509_check_purpose(X509 *x509, int purpose, int ca);
4454
4455 #define X509_TRUST_TRUSTED 1
4456 #define X509_TRUST_REJECTED 2
4457 #define X509_TRUST_UNTRUSTED 3
4458
4459 // X509_check_trust checks if |x509| is a valid trust anchor for trust type
4460 // |id|. See |X509_VERIFY_PARAM_set_trust| for details. It returns
4461 // |X509_TRUST_TRUSTED| if |x509| is a trust anchor, |X509_TRUST_REJECTED| if it
4462 // was distrusted, and |X509_TRUST_UNTRUSTED| otherwise. |id| should be one of
4463 // the |X509_TRUST_*| constants, or zero to indicate the default behavior.
4464 // |flags| should be zero and is ignored.
4465 OPENSSL_EXPORT int X509_check_trust(X509 *x509, int id, int flags);
4466
4467 // X.509 information.
4468 //
4469 // |X509_INFO| is the return type for |PEM_X509_INFO_read_bio|, defined in
4470 // <openssl/pem.h>. It is used to store a certificate, CRL, or private key. This
4471 // type is defined in this header for OpenSSL compatibility.
4472
4473 struct private_key_st {
4474 EVP_PKEY *dec_pkey;
4475 } /* X509_PKEY */;
4476
4477 struct X509_info_st {
4478 X509 *x509;
4479 X509_CRL *crl;
4480 X509_PKEY *x_pkey;
4481
4482 EVP_CIPHER_INFO enc_cipher;
4483 int enc_len;
4484 char *enc_data;
4485 } /* X509_INFO */;
4486
4487 DEFINE_STACK_OF(X509_INFO)
4488
4489 // X509_INFO_free releases memory associated with |info|.
4490 OPENSSL_EXPORT void X509_INFO_free(X509_INFO *info);
4491
4492
4493 // Deprecated custom extension registration.
4494 //
4495 // The following functions allow callers to register custom extensions for use
4496 // with |X509V3_EXT_d2i| and related functions. This mechanism is deprecated and
4497 // will be removed in the future. As discussed in |X509V3_EXT_add|, it is not
4498 // possible to safely register a custom extension without risking race
4499 // conditions and memory errors when linked with other users of BoringSSL.
4500 //
4501 // Moreover, it is not necessary to register a custom extension to process
4502 // extensions unknown to BoringSSL. Registration does not impact certificate
4503 // verification. Caller should instead use functions such as
4504 // |ASN1_OBJECT_create|, |X509_get_ext_by_OBJ|, |X509_EXTENSION_get_data|, and
4505 // |X509_EXTENSION_create_by_OBJ| to inspect or create extensions directly.
4506
4507 // The following function pointer types are used in |X509V3_EXT_METHOD|.
4508 typedef void *(*X509V3_EXT_NEW)(void);
4509 typedef void (*X509V3_EXT_FREE)(void *ext);
4510 typedef void *(*X509V3_EXT_D2I)(void *ext, const uint8_t **inp, long len);
4511 typedef int (*X509V3_EXT_I2D)(void *ext, uint8_t **outp);
4512 typedef STACK_OF(CONF_VALUE) *(*X509V3_EXT_I2V)(const X509V3_EXT_METHOD *method,
4513 void *ext,
4514 STACK_OF(CONF_VALUE) *extlist);
4515 typedef void *(*X509V3_EXT_V2I)(const X509V3_EXT_METHOD *method,
4516 const X509V3_CTX *ctx,
4517 const STACK_OF(CONF_VALUE) *values);
4518 typedef char *(*X509V3_EXT_I2S)(const X509V3_EXT_METHOD *method, void *ext);
4519 typedef void *(*X509V3_EXT_S2I)(const X509V3_EXT_METHOD *method,
4520 const X509V3_CTX *ctx, const char *str);
4521 typedef int (*X509V3_EXT_I2R)(const X509V3_EXT_METHOD *method, void *ext,
4522 BIO *out, int indent);
4523 typedef void *(*X509V3_EXT_R2I)(const X509V3_EXT_METHOD *method,
4524 const X509V3_CTX *ctx, const char *str);
4525
4526 // A v3_ext_method, aka |X509V3_EXT_METHOD|, is a deprecated type which defines
4527 // a custom extension.
4528 struct v3_ext_method {
4529 // ext_nid is the NID of the extension.
4530 int ext_nid;
4531
4532 // ext_flags is a combination of |X509V3_EXT_*| constants.
4533 int ext_flags;
4534
4535 // it determines how values of this extension are allocated, released, parsed,
4536 // and marshalled. This must be non-NULL.
4537 ASN1_ITEM_EXP *it;
4538
4539 // The following functions are ignored in favor of |it|. They are retained in
4540 // the struct only for source compatibility with existing struct definitions.
4541 X509V3_EXT_NEW ext_new;
4542 X509V3_EXT_FREE ext_free;
4543 X509V3_EXT_D2I d2i;
4544 X509V3_EXT_I2D i2d;
4545
4546 // The following functions are used for string extensions.
4547 X509V3_EXT_I2S i2s;
4548 X509V3_EXT_S2I s2i;
4549
4550 // The following functions are used for multi-valued extensions.
4551 X509V3_EXT_I2V i2v;
4552 X509V3_EXT_V2I v2i;
4553
4554 // The following functions are used for "raw" extensions, which implement
4555 // custom printing behavior.
4556 X509V3_EXT_I2R i2r;
4557 X509V3_EXT_R2I r2i;
4558
4559 void *usr_data; // Any extension specific data
4560 } /* X509V3_EXT_METHOD */;
4561
4562 // X509V3_EXT_MULTILINE causes the result of an |X509V3_EXT_METHOD|'s |i2v|
4563 // function to be printed on separate lines, rather than separated by commas.
4564 #define X509V3_EXT_MULTILINE 0x4
4565
4566 // X509V3_EXT_get returns the |X509V3_EXT_METHOD| corresponding to |ext|'s
4567 // extension type, or NULL if none was registered.
4568 OPENSSL_EXPORT const X509V3_EXT_METHOD *X509V3_EXT_get(
4569 const X509_EXTENSION *ext);
4570
4571 // X509V3_EXT_get_nid returns the |X509V3_EXT_METHOD| corresponding to |nid|, or
4572 // NULL if none was registered.
4573 OPENSSL_EXPORT const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid);
4574
4575 // X509V3_EXT_add registers |ext| as a custom extension for the extension type
4576 // |ext->ext_nid|. |ext| must be valid for the remainder of the address space's
4577 // lifetime. It returns one on success and zero on error.
4578 //
4579 // WARNING: This function modifies global state. If other code in the same
4580 // address space also registers an extension with type |ext->ext_nid|, the two
4581 // registrations will conflict. Which registration takes effect is undefined. If
4582 // the two registrations use incompatible in-memory representations, code
4583 // expecting the other registration will then cast a type to the wrong type,
4584 // resulting in a potentially exploitable memory error. This conflict can also
4585 // occur if BoringSSL later adds support for |ext->ext_nid|, with a different
4586 // in-memory representation than the one expected by |ext|.
4587 //
4588 // This function, additionally, is not thread-safe and cannot be called
4589 // concurrently with any other BoringSSL function.
4590 //
4591 // As a result, it is impossible to safely use this function. Registering a
4592 // custom extension has no impact on certificate verification so, instead,
4593 // callers should simply handle the custom extension with the byte-based
4594 // |X509_EXTENSION| APIs directly. Registering |ext| with the library has little
4595 // practical value.
4596 OPENSSL_EXPORT OPENSSL_DEPRECATED int X509V3_EXT_add(X509V3_EXT_METHOD *ext);
4597
4598 // X509V3_EXT_add_alias registers a custom extension with NID |nid_to|. The
4599 // corresponding ASN.1 type is copied from |nid_from|. It returns one on success
4600 // and zero on error.
4601 //
4602 // WARNING: Do not use this function. See |X509V3_EXT_add|.
4603 OPENSSL_EXPORT OPENSSL_DEPRECATED int X509V3_EXT_add_alias(int nid_to,
4604 int nid_from);
4605
4606
4607 // Deprecated config-based extension creation.
4608 //
4609 // The following functions allow specifying X.509 extensions using OpenSSL's
4610 // config file syntax, from the OpenSSL command-line tool. They are retained,
4611 // for now, for compatibility with legacy software but may be removed in the
4612 // future. Construct the extensions using the typed C APIs instead.
4613 //
4614 // Callers should especially avoid these functions if passing in non-constant
4615 // values. They use ad-hoc, string-based formats which are prone to injection
4616 // vulnerabilities. For a CA, this means using them risks misissuance.
4617 //
4618 // These functions are not safe to use with untrusted inputs. The string formats
4619 // may implicitly reference context information and, in OpenSSL (though not
4620 // BoringSSL), one even allows reading arbitrary files. Many formats can also
4621 // produce far larger outputs than their inputs, so untrusted inputs may lead to
4622 // denial-of-service attacks. Finally, the parsers see much less testing and
4623 // review than most of the library and may have bugs including memory leaks or
4624 // crashes.
4625
4626 // v3_ext_ctx, aka |X509V3_CTX|, contains additional context information for
4627 // constructing extensions. Some string formats reference additional values in
4628 // these objects. It must be initialized with |X509V3_set_ctx| or
4629 // |X509V3_set_ctx_test| before use.
4630 struct v3_ext_ctx {
4631 int flags;
4632 const X509 *issuer_cert;
4633 const X509 *subject_cert;
4634 const X509_REQ *subject_req;
4635 const X509_CRL *crl;
4636 const CONF *db;
4637 };
4638
4639 #define X509V3_CTX_TEST 0x1
4640 #define X509V3_CTX_REPLACE 0x2
4641
4642 // X509V3_set_ctx initializes |ctx| with the specified objects. Some string
4643 // formats will reference fields in these objects. Each object may be NULL to
4644 // omit it, in which case those formats cannot be used. |flags| should be zero,
4645 // unless called via |X509V3_set_ctx_test|.
4646 //
4647 // |issuer|, |subject|, |req|, and |crl|, if non-NULL, must outlive |ctx|.
4648 OPENSSL_EXPORT void X509V3_set_ctx(X509V3_CTX *ctx, const X509 *issuer,
4649 const X509 *subject, const X509_REQ *req,
4650 const X509_CRL *crl, int flags);
4651
4652 // X509V3_set_ctx_test calls |X509V3_set_ctx| without any reference objects and
4653 // mocks out some features that use them. The resulting extensions may be
4654 // incomplete and should be discarded. This can be used to partially validate
4655 // syntax.
4656 //
4657 // TODO(davidben): Can we remove this?
4658 #define X509V3_set_ctx_test(ctx) \
4659 X509V3_set_ctx(ctx, NULL, NULL, NULL, NULL, X509V3_CTX_TEST)
4660
4661 // X509V3_set_nconf sets |ctx| to use |conf| as the config database. |ctx| must
4662 // have previously been initialized by |X509V3_set_ctx| or
4663 // |X509V3_set_ctx_test|. Some string formats will reference sections in |conf|.
4664 // |conf| may be NULL, in which case these formats cannot be used. If non-NULL,
4665 // |conf| must outlive |ctx|.
4666 OPENSSL_EXPORT void X509V3_set_nconf(X509V3_CTX *ctx, const CONF *conf);
4667
4668 // X509V3_set_ctx_nodb calls |X509V3_set_nconf| with no config database.
4669 #define X509V3_set_ctx_nodb(ctx) X509V3_set_nconf(ctx, NULL)
4670
4671 // X509V3_EXT_nconf constructs an extension of type specified by |name|, and
4672 // value specified by |value|. It returns a newly-allocated |X509_EXTENSION|
4673 // object on success, or NULL on error. |conf| and |ctx| specify additional
4674 // information referenced by some formats. Either |conf| or |ctx| may be NULL,
4675 // in which case features which use it will be disabled.
4676 //
4677 // If non-NULL, |ctx| must be initialized with |X509V3_set_ctx| or
4678 // |X509V3_set_ctx_test|.
4679 //
4680 // Both |conf| and |ctx| provide a |CONF| object. When |ctx| is non-NULL, most
4681 // features use the |ctx| copy, configured with |X509V3_set_ctx|, but some use
4682 // |conf|. Callers should ensure the two match to avoid surprisingly behavior.
4683 OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_nconf(const CONF *conf,
4684 const X509V3_CTX *ctx,
4685 const char *name,
4686 const char *value);
4687
4688 // X509V3_EXT_nconf_nid behaves like |X509V3_EXT_nconf|, except the extension
4689 // type is specified as a NID.
4690 OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_nconf_nid(const CONF *conf,
4691 const X509V3_CTX *ctx,
4692 int ext_nid,
4693 const char *value);
4694
4695 // X509V3_EXT_conf_nid calls |X509V3_EXT_nconf_nid|. |conf| must be NULL.
4696 //
4697 // TODO(davidben): This is the only exposed instance of an LHASH in our public
4698 // headers. cryptography.io wraps this function so we cannot, yet, replace the
4699 // type with a dummy struct.
4700 OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_conf_nid(LHASH_OF(CONF_VALUE) *conf,
4701 const X509V3_CTX *ctx,
4702 int ext_nid,
4703 const char *value);
4704
4705 // X509V3_EXT_add_nconf_sk looks up the section named |section| in |conf|. For
4706 // each |CONF_VALUE| in the section, it constructs an extension as in
4707 // |X509V3_EXT_nconf|, taking |name| and |value| from the |CONF_VALUE|. Each new
4708 // extension is appended to |*sk|. If |*sk| is non-NULL, and at least one
4709 // extension is added, it sets |*sk| to a newly-allocated
4710 // |STACK_OF(X509_EXTENSION)|. It returns one on success and zero on error.
4711 OPENSSL_EXPORT int X509V3_EXT_add_nconf_sk(const CONF *conf,
4712 const X509V3_CTX *ctx,
4713 const char *section,
4714 STACK_OF(X509_EXTENSION) **sk);
4715
4716 // X509V3_EXT_add_nconf adds extensions to |cert| as in
4717 // |X509V3_EXT_add_nconf_sk|. It returns one on success and zero on error.
4718 OPENSSL_EXPORT int X509V3_EXT_add_nconf(const CONF *conf, const X509V3_CTX *ctx,
4719 const char *section, X509 *cert);
4720
4721 // X509V3_EXT_REQ_add_nconf adds extensions to |req| as in
4722 // |X509V3_EXT_add_nconf_sk|. It returns one on success and zero on error.
4723 OPENSSL_EXPORT int X509V3_EXT_REQ_add_nconf(const CONF *conf,
4724 const X509V3_CTX *ctx,
4725 const char *section, X509_REQ *req);
4726
4727 // X509V3_EXT_CRL_add_nconf adds extensions to |crl| as in
4728 // |X509V3_EXT_add_nconf_sk|. It returns one on success and zero on error.
4729 OPENSSL_EXPORT int X509V3_EXT_CRL_add_nconf(const CONF *conf,
4730 const X509V3_CTX *ctx,
4731 const char *section, X509_CRL *crl);
4732
4733 // X509V3_EXT_conf calls |X509V3_EXT_nconf|. |conf| must be NULL.
4734 //
4735 // NOTE: This is only provided for compatibility. See |X509V3_EXT_nconf|
4736 // instead.
4737 OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_conf(LHASH_OF(CONF_VALUE) *conf,
4738 X509V3_CTX *ctx,
4739 const char *name,
4740 const char *value);
4741
4742 // i2s_ASN1_OCTET_STRING returns a human-readable representation of |oct| as a
4743 // newly-allocated, NUL-terminated string, or NULL on error. |method| is
4744 // ignored. The caller must release the result with |OPENSSL_free| when done.
4745 OPENSSL_EXPORT char *i2s_ASN1_OCTET_STRING(const X509V3_EXT_METHOD *method,
4746 const ASN1_OCTET_STRING *oct);
4747
4748 // s2i_ASN1_OCTET_STRING decodes |str| as a hexdecimal byte string, with
4749 // optional colon separators between bytes. It returns a newly-allocated
4750 // |ASN1_OCTET_STRING| with the result on success, or NULL on error. |method|
4751 // and |ctx| are ignored.
4752 OPENSSL_EXPORT ASN1_OCTET_STRING *s2i_ASN1_OCTET_STRING(
4753 const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, const char *str);
4754
4755 // i2s_ASN1_INTEGER returns a human-readable representation of |aint| as a
4756 // newly-allocated, NUL-terminated string, or NULL on error. |method| is
4757 // ignored. The caller must release the result with |OPENSSL_free| when done.
4758 OPENSSL_EXPORT char *i2s_ASN1_INTEGER(const X509V3_EXT_METHOD *method,
4759 const ASN1_INTEGER *aint);
4760
4761 // s2i_ASN1_INTEGER decodes |value| as the ASCII representation of an integer,
4762 // and returns a newly-allocated |ASN1_INTEGER| containing the result, or NULL
4763 // on error. |method| is ignored. If |value| begins with "0x" or "0X", the input
4764 // is decoded in hexadecimal, otherwise decimal.
4765 OPENSSL_EXPORT ASN1_INTEGER *s2i_ASN1_INTEGER(const X509V3_EXT_METHOD *method,
4766 const char *value);
4767
4768 // i2s_ASN1_ENUMERATED returns a human-readable representation of |aint| as a
4769 // newly-allocated, NUL-terminated string, or NULL on error. |method| is
4770 // ignored. The caller must release the result with |OPENSSL_free| when done.
4771 OPENSSL_EXPORT char *i2s_ASN1_ENUMERATED(const X509V3_EXT_METHOD *method,
4772 const ASN1_ENUMERATED *aint);
4773
4774 // X509V3_conf_free releases memory associated with |CONF_VALUE|.
4775 OPENSSL_EXPORT void X509V3_conf_free(CONF_VALUE *val);
4776
4777 // i2v_GENERAL_NAME serializes |gen| as a |CONF_VALUE|. If |ret| is non-NULL, it
4778 // appends the value to |ret| and returns |ret| on success or NULL on error. If
4779 // it returns NULL, the caller is still responsible for freeing |ret|. If |ret|
4780 // is NULL, it returns a newly-allocated |STACK_OF(CONF_VALUE)| containing the
4781 // result. |method| is ignored. When done, the caller should release the result
4782 // with |sk_CONF_VALUE_pop_free| and |X509V3_conf_free|.
4783 //
4784 // Do not use this function. This is an internal implementation detail of the
4785 // human-readable print functions. If extracting a SAN list from a certificate,
4786 // look at |gen| directly.
4787 OPENSSL_EXPORT STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(
4788 const X509V3_EXT_METHOD *method, const GENERAL_NAME *gen,
4789 STACK_OF(CONF_VALUE) *ret);
4790
4791 // i2v_GENERAL_NAMES serializes |gen| as a list of |CONF_VALUE|s. If |ret| is
4792 // non-NULL, it appends the values to |ret| and returns |ret| on success or NULL
4793 // on error. If it returns NULL, the caller is still responsible for freeing
4794 // |ret|. If |ret| is NULL, it returns a newly-allocated |STACK_OF(CONF_VALUE)|
4795 // containing the results. |method| is ignored.
4796 //
4797 // Do not use this function. This is an internal implementation detail of the
4798 // human-readable print functions. If extracting a SAN list from a certificate,
4799 // look at |gen| directly.
4800 OPENSSL_EXPORT STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES(
4801 const X509V3_EXT_METHOD *method, const GENERAL_NAMES *gen,
4802 STACK_OF(CONF_VALUE) *extlist);
4803
4804 // a2i_IPADDRESS decodes |ipasc| as the textual representation of an IPv4 or
4805 // IPv6 address. On success, it returns a newly-allocated |ASN1_OCTET_STRING|
4806 // containing the decoded IP address. IPv4 addresses are represented as 4-byte
4807 // strings and IPv6 addresses as 16-byte strings. On failure, it returns NULL.
4808 OPENSSL_EXPORT ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc);
4809
4810 // a2i_IPADDRESS_NC decodes |ipasc| as the textual representation of an IPv4 or
4811 // IPv6 address range. On success, it returns a newly-allocated
4812 // |ASN1_OCTET_STRING| containing the decoded IP address, followed by the
4813 // decoded mask. IPv4 ranges are represented as 8-byte strings and IPv6 ranges
4814 // as 32-byte strings. On failure, it returns NULL.
4815 //
4816 // The text format decoded by this function is not the standard CIDR notiation.
4817 // Instead, the mask after the "/" is represented as another IP address. For
4818 // example, "192.168.0.0/16" would be written "192.168.0.0/255.255.0.0".
4819 OPENSSL_EXPORT ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc);
4820
4821
4822 // Deprecated functions.
4823
4824 // X509_get_notBefore returns |x509|'s notBefore time. Note this function is not
4825 // const-correct for legacy reasons. Use |X509_get0_notBefore| or
4826 // |X509_getm_notBefore| instead.
4827 OPENSSL_EXPORT ASN1_TIME *X509_get_notBefore(const X509 *x509);
4828
4829 // X509_get_notAfter returns |x509|'s notAfter time. Note this function is not
4830 // const-correct for legacy reasons. Use |X509_get0_notAfter| or
4831 // |X509_getm_notAfter| instead.
4832 OPENSSL_EXPORT ASN1_TIME *X509_get_notAfter(const X509 *x509);
4833
4834 // X509_set_notBefore calls |X509_set1_notBefore|. Use |X509_set1_notBefore|
4835 // instead.
4836 OPENSSL_EXPORT int X509_set_notBefore(X509 *x509, const ASN1_TIME *tm);
4837
4838 // X509_set_notAfter calls |X509_set1_notAfter|. Use |X509_set1_notAfter|
4839 // instead.
4840 OPENSSL_EXPORT int X509_set_notAfter(X509 *x509, const ASN1_TIME *tm);
4841
4842 // X509_CRL_get_lastUpdate returns a mutable pointer to |crl|'s thisUpdate time.
4843 // The OpenSSL API refers to this field as lastUpdate.
4844 //
4845 // Use |X509_CRL_get0_lastUpdate| or |X509_CRL_set1_lastUpdate| instead.
4846 OPENSSL_EXPORT ASN1_TIME *X509_CRL_get_lastUpdate(X509_CRL *crl);
4847
4848 // X509_CRL_get_nextUpdate returns a mutable pointer to |crl|'s nextUpdate time,
4849 // or NULL if |crl| has none. Use |X509_CRL_get0_nextUpdate| or
4850 // |X509_CRL_set1_nextUpdate| instead.
4851 OPENSSL_EXPORT ASN1_TIME *X509_CRL_get_nextUpdate(X509_CRL *crl);
4852
4853 // X509_extract_key is a legacy alias to |X509_get_pubkey|. Use
4854 // |X509_get_pubkey| instead.
4855 #define X509_extract_key(x) X509_get_pubkey(x)
4856
4857 // X509_REQ_extract_key is a legacy alias for |X509_REQ_get_pubkey|.
4858 #define X509_REQ_extract_key(a) X509_REQ_get_pubkey(a)
4859
4860 // X509_name_cmp is a legacy alias for |X509_NAME_cmp|.
4861 #define X509_name_cmp(a, b) X509_NAME_cmp((a), (b))
4862
4863 // The following symbols are deprecated aliases to |X509_CRL_set1_*|.
4864 #define X509_CRL_set_lastUpdate X509_CRL_set1_lastUpdate
4865 #define X509_CRL_set_nextUpdate X509_CRL_set1_nextUpdate
4866
4867 // X509_get_serialNumber returns a mutable pointer to |x509|'s serial number.
4868 // Prefer |X509_get0_serialNumber|.
4869 OPENSSL_EXPORT ASN1_INTEGER *X509_get_serialNumber(X509 *x509);
4870
4871 // X509_NAME_get_text_by_OBJ finds the first attribute with type |obj| in
4872 // |name|. If found, it writes the value's UTF-8 representation to |buf|.
4873 // followed by a NUL byte, and returns the number of bytes in the output,
4874 // excluding the NUL byte. This is unlike OpenSSL which returns the raw
4875 // ASN1_STRING data. The UTF-8 encoding of the |ASN1_STRING| may not contain a 0
4876 // codepoint.
4877 //
4878 // This function writes at most |len| bytes, including the NUL byte. If |buf|
4879 // is NULL, it writes nothing and returns the number of bytes in the
4880 // output, excluding the NUL byte that would be required for the full UTF-8
4881 // output.
4882 //
4883 // This function may return -1 if an error occurs for any reason, including the
4884 // value not being a recognized string type, |len| being of insufficient size to
4885 // hold the full UTF-8 encoding and NUL byte, memory allocation failures, an
4886 // object with type |obj| not existing in |name|, or if the UTF-8 encoding of
4887 // the string contains a zero byte.
4888 OPENSSL_EXPORT int X509_NAME_get_text_by_OBJ(const X509_NAME *name,
4889 const ASN1_OBJECT *obj, char *buf,
4890 int len);
4891
4892 // X509_NAME_get_text_by_NID behaves like |X509_NAME_get_text_by_OBJ| except it
4893 // finds an attribute of type |nid|, which should be one of the |NID_*|
4894 // constants.
4895 OPENSSL_EXPORT int X509_NAME_get_text_by_NID(const X509_NAME *name, int nid,
4896 char *buf, int len);
4897
4898 // X509_STORE_CTX_get0_parent_ctx returns NULL.
4899 OPENSSL_EXPORT X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(
4900 X509_STORE_CTX *ctx);
4901
4902 // X509_LOOKUP_free releases memory associated with |ctx|. This function should
4903 // never be used outside the library. No function in the public API hands
4904 // ownership of an |X509_LOOKUP| to the caller.
4905 //
4906 // TODO(davidben): Unexport this function after rust-openssl is fixed to no
4907 // longer call it.
4908 OPENSSL_EXPORT void X509_LOOKUP_free(X509_LOOKUP *ctx);
4909
4910 // X509_STORE_CTX_cleanup resets |ctx| to the empty state.
4911 //
4912 // This function is a remnant of when |X509_STORE_CTX| was stack-allocated and
4913 // should not be used. If releasing |ctx|, call |X509_STORE_CTX_free|. If
4914 // reusing |ctx| for a new verification, release the old one and create a new
4915 // one.
4916 OPENSSL_EXPORT void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx);
4917
4918 // X509V3_add_standard_extensions returns one.
4919 OPENSSL_EXPORT int X509V3_add_standard_extensions(void);
4920
4921 OPENSSL_EXPORT STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line);
4922
4923 // The following symbols are legacy aliases for |X509_STORE_CTX| functions.
4924 #define X509_STORE_get1_certs X509_STORE_CTX_get1_certs
4925 #define X509_STORE_get1_crls X509_STORE_CTX_get1_crls
4926
4927 // X509_STORE_CTX_get_chain is a legacy alias for |X509_STORE_CTX_get0_chain|.
4928 OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx);
4929
4930 // X509_STORE_CTX_trusted_stack is a deprecated alias for
4931 // |X509_STORE_CTX_set0_trusted_stack|.
4932 OPENSSL_EXPORT void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx,
4933 STACK_OF(X509) *sk);
4934
4935 typedef int (*X509_STORE_CTX_verify_cb)(int, X509_STORE_CTX *);
4936
4937 // X509_STORE_CTX_set_verify_cb configures a callback function for |ctx| that is
4938 // called multiple times during |X509_verify_cert|. The callback returns zero to
4939 // fail verification and one to proceed. Typically, it will return |ok|, which
4940 // preserves the default behavior. Returning one when |ok| is zero will proceed
4941 // past some error. The callback may inspect |ctx| and the error queue to
4942 // attempt to determine the current stage of certificate verification, but this
4943 // is often unreliable. When synthesizing an error, callbacks should use
4944 // |X509_STORE_CTX_set_error| to set a corresponding error.
4945 //
4946 // WARNING: Do not use this function. It is extremely fragile and unpredictable.
4947 // This callback exposes implementation details of certificate verification,
4948 // which change as the library evolves. Attempting to use it for security checks
4949 // can introduce vulnerabilities if making incorrect assumptions about when the
4950 // callback is called. Some errors, when suppressed, may implicitly suppress
4951 // other errors due to internal implementation details. Additionally, overriding
4952 // |ok| may leave |ctx| in an inconsistent state and break invariants.
4953 //
4954 // Instead, customize certificate verification by configuring options on the
4955 // |X509_STORE_CTX| before verification, or applying additional checks after
4956 // |X509_verify_cert| completes successfully.
4957 OPENSSL_EXPORT void X509_STORE_CTX_set_verify_cb(
4958 X509_STORE_CTX *ctx, int (*verify_cb)(int ok, X509_STORE_CTX *ctx));
4959
4960 OPENSSL_EXPORT X509_STORE_CTX_verify_cb
4961 X509_STORE_get_verify_cb(X509_STORE *ctx);
4962
4963 // X509_STORE_set_verify_cb acts like |X509_STORE_CTX_set_verify_cb| but sets
4964 // the verify callback for any |X509_STORE_CTX| created from this |X509_STORE|
4965 //
4966 // Do not use this function. See |X509_STORE_CTX_set_verify_cb| for details.
4967 OPENSSL_EXPORT void X509_STORE_set_verify_cb(
4968 X509_STORE *store, X509_STORE_CTX_verify_cb verify_cb);
4969
4970 // X509_STORE_set_verify_cb_func is a deprecated alias for
4971 // |X509_STORE_set_verify_cb|.
4972 #define X509_STORE_set_verify_cb_func(store, func) \
4973 X509_STORE_set_verify_cb((store), (func))
4974
4975 typedef int (*X509_STORE_CTX_get_crl_fn)(X509_STORE_CTX *ctx, X509_CRL **crl,
4976 X509 *x);
4977 typedef int (*X509_STORE_CTX_check_crl_fn)(X509_STORE_CTX *ctx, X509_CRL *crl);
4978
4979 // X509_STORE_set_get_crl override's |store|'s logic for looking up CRLs.
4980 //
4981 // Do not use this function. It is temporarily retained to support one caller
4982 // and will be removed after that caller is fixed. It is not possible for
4983 // external callers to correctly implement this callback. The real
4984 // implementation sets some inaccessible internal state on |X509_STORE_CTX|.
4985 OPENSSL_EXPORT void X509_STORE_set_get_crl(X509_STORE *store,
4986 X509_STORE_CTX_get_crl_fn get_crl);
4987
4988 // X509_STORE_set_check_crl override's |store|'s logic for checking CRL
4989 // validity.
4990 //
4991 // Do not use this function. It is temporarily retained to support one caller
4992 // and will be removed after that caller is fixed. It is not possible for
4993 // external callers to correctly implement this callback. The real
4994 // implementation relies some inaccessible internal state on |X509_STORE_CTX|.
4995 OPENSSL_EXPORT void X509_STORE_set_check_crl(
4996 X509_STORE *store, X509_STORE_CTX_check_crl_fn check_crl);
4997
4998 // X509_STORE_CTX_set_chain configures |ctx| to use |sk| for untrusted
4999 // intermediate certificates to use in verification. This function is redundant
5000 // with the |chain| parameter of |X509_STORE_CTX_init|. Use the parameter
5001 // instead.
5002 //
5003 // WARNING: Despite the similar name, this function is unrelated to
5004 // |X509_STORE_CTX_get0_chain|.
5005 //
5006 // WARNING: This function saves a pointer to |sk| without copying or
5007 // incrementing reference counts. |sk| must outlive |ctx| and may not be mutated
5008 // for the duration of the certificate verification.
5009 OPENSSL_EXPORT void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx,
5010 STACK_OF(X509) *sk);
5011
5012 // X509_STORE_CTX_set0_untrusted is an alias for |X509_STORE_CTX_set_chain|.
5013 OPENSSL_EXPORT void X509_STORE_CTX_set0_untrusted(X509_STORE_CTX *ctx,
5014 STACK_OF(X509) *sk);
5015
5016 // The following flags do nothing. The corresponding non-standard options have
5017 // been removed.
5018 #define X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS 0
5019
5020 // X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS does nothing, but is necessary in
5021 // OpenSSL to enable standard wildcard matching. In AWS-LC, this behavior is
5022 // always enabled.
5023 #define X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS 0
5024
5025 // X509_PURPOSE stuff
5026
5027 #define NS_SSL_CLIENT 0x80
5028 #define NS_SSL_SERVER 0x40
5029 #define NS_SMIME 0x20
5030 #define NS_OBJSIGN 0x10
5031 #define NS_SSL_CA 0x04
5032 #define NS_SMIME_CA 0x02
5033 #define NS_OBJSIGN_CA 0x01
5034 #define NS_ANY_CA (NS_SSL_CA | NS_SMIME_CA | NS_OBJSIGN_CA)
5035
5036 typedef struct x509_purpose_st {
5037 int purpose;
5038 int trust; // Default trust ID
5039 int flags;
5040 int (*check_purpose)(const struct x509_purpose_st *, const X509 *, int);
5041 char *name;
5042 char *sname;
5043 void *usr_data;
5044 } X509_PURPOSE;
5045
5046 DEFINE_STACK_OF(X509_PURPOSE)
5047
5048 // X509_STORE_get0_objects returns a non-owning pointer of |store|'s internal
5049 // object list. Although this function is not const, callers must not modify
5050 // the result of this function.
5051 //
5052 // WARNING: This function is not thread-safe. If |store| is shared across
5053 // multiple threads, callers cannot safely inspect the result of this function,
5054 // because another thread may have concurrently added to it. In particular,
5055 // |X509_LOOKUP_add_dir| treats this list as a cache and may add to it in the
5056 // course of certificate verification. This API additionally prevents fixing
5057 // some quadratic worst-case behavior in |X509_STORE| and may be removed in the
5058 // future. Use |X509_STORE_get1_objects| instead.
5059 OPENSSL_EXPORT STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(
5060 X509_STORE *store);
5061
5062 // X509_PURPOSE_get_by_sname returns the |X509_PURPOSE_*| constant corresponding
5063 // a short name |sname|, or -1 if |sname| was not recognized.
5064 //
5065 // Use |X509_PURPOSE_*| constants directly instead. The short names used by this
5066 // function look like "sslserver" or "smimeencrypt", so they do not make
5067 // especially good APIs.
5068 //
5069 // This function differs from OpenSSL, which returns an "index" to be passed to
5070 // |X509_PURPOSE_get0|, followed by |X509_PURPOSE_get_id|, to finally obtain an
5071 // |X509_PURPOSE_*| value suitable for use with |X509_VERIFY_PARAM_set_purpose|.
5072 OPENSSL_EXPORT int X509_PURPOSE_get_by_sname(const char *sname);
5073
5074 // X509_PURPOSE_get0 returns the |X509_PURPOSE| object corresponding to |id|,
5075 // which should be one of the |X509_PURPOSE_*| constants, or NULL if none
5076 // exists.
5077 //
5078 // This function differs from OpenSSL, which takes an "index", returned from
5079 // |X509_PURPOSE_get_by_sname|. In BoringSSL, indices and |X509_PURPOSE_*| IDs
5080 // are the same.
5081 OPENSSL_EXPORT const X509_PURPOSE *X509_PURPOSE_get0(int id);
5082
5083 // X509_PURPOSE_get_id returns |purpose|'s ID. This will be one of the
5084 // |X509_PURPOSE_*| constants.
5085 OPENSSL_EXPORT int X509_PURPOSE_get_id(const X509_PURPOSE *purpose);
5086
5087
5088 // Private structures.
5089
5090 struct X509_algor_st {
5091 ASN1_OBJECT *algorithm;
5092 ASN1_TYPE *parameter;
5093 } /* X509_ALGOR */;
5094
5095
5096 // Underdocumented functions.
5097 //
5098 // TODO(https://crbug.com/boringssl/426): Functions below this point have not
5099 // yet been documented or organized into sections.
5100
5101 DECLARE_STACK_OF(DIST_POINT)
5102
5103 // This is used for a table of trust checking functions
5104
5105 struct x509_trust_st {
5106 int trust;
5107 int flags;
5108 int (*check_trust)(const X509_TRUST *, X509 *);
5109 char *name;
5110 int arg1;
5111 void *arg2;
5112 } /* X509_TRUST */;
5113
5114 DEFINE_STACK_OF(X509_TRUST)
5115
5116 OPENSSL_EXPORT int X509_TRUST_set(int *t, int trust);
5117 OPENSSL_EXPORT int X509_TRUST_get_count(void);
5118 OPENSSL_EXPORT const X509_TRUST *X509_TRUST_get0(int idx);
5119 OPENSSL_EXPORT int X509_TRUST_get_by_id(int id);
5120 OPENSSL_EXPORT int X509_TRUST_get_flags(const X509_TRUST *xp);
5121 OPENSSL_EXPORT char *X509_TRUST_get0_name(const X509_TRUST *xp);
5122 OPENSSL_EXPORT int X509_TRUST_get_trust(const X509_TRUST *xp);
5123 // X509_TRUST_cleanup intentionally does nothing.
5124 // This function is maintained only for compatibility with applications
5125 // that consume OpenSSL APIs. AWS-LC does not support the related
5126 // static trust settings functions which were also deprecated in OpenSSL 1.1.0.
5127 OPENSSL_EXPORT OPENSSL_DEPRECATED void X509_TRUST_cleanup(void);
5128
5129 #define X509_LU_NONE 0
5130 #define X509_LU_X509 1
5131 #define X509_LU_CRL 2
5132 #define X509_LU_PKEY 3
5133
5134 // Internal use: mask of policy related options (hidden)
5135
5136 #define X509_V_FLAG_POLICY_MASK \
5137 (X509_V_FLAG_POLICY_CHECK | X509_V_FLAG_EXPLICIT_POLICY | \
5138 X509_V_FLAG_INHIBIT_ANY | X509_V_FLAG_INHIBIT_MAP)
5139
5140 // X509_OBJECT_get0_X509_CRL returns the |X509_CRL| associated with |a|
5141 OPENSSL_EXPORT X509_CRL *X509_OBJECT_get0_X509_CRL(const X509_OBJECT *a);
5142
5143 // X509_OBJECT_set1_X509 sets |obj| on |a| and uprefs |obj|. As with other set1
5144 // methods, |a| does not take ownership of |obj|; the caller is responsible for
5145 // managing freeing |obj| when appropriate.
5146 OPENSSL_EXPORT int X509_OBJECT_set1_X509(X509_OBJECT *a, X509 *obj);
5147
5148 // X509_OBJECT_set1_X509_CRL sets CRL |obj| on |a| and uprefs |obj|. As with
5149 // other set1 methods, |a| does not take ownership of |obj|; the caller is
5150 // responsible for managing freeing |obj| when appropriate.
5151 OPENSSL_EXPORT int X509_OBJECT_set1_X509_CRL(X509_OBJECT *a, X509_CRL *obj);
5152
5153 // X509_STORE_lock takes a write lock on |v|. return 1 on success, 0 on failure.
5154 //
5155 // Avoid operations on the X509_STORE or a X509_STORE_CTX containing it while
5156 // it is locked; many |X509_STORE_*| functions take this lock internally which
5157 // will cause a deadlock when called on a locked store.
5158 OPENSSL_EXPORT int X509_STORE_lock(X509_STORE *v);
5159
5160 // X509_STORE_unlock releases a lock on |v|. return 1 on success, 0 on failure
5161 OPENSSL_EXPORT int X509_STORE_unlock(X509_STORE *v);
5162
5163 OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *st,
5164 X509_NAME *nm);
5165 OPENSSL_EXPORT STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls(X509_STORE_CTX *st,
5166 X509_NAME *nm);
5167
5168
5169 // X509_STORE_get_by_subject is an alias to |X509_STORE_CTX_get_by_subject| in
5170 // OpenSSL 1.1.1.
5171 #define X509_STORE_get_by_subject X509_STORE_CTX_get_by_subject
5172
5173 // X509_STORE_CTX_get_by_subject tries to find an object of a given type, which
5174 // may be |X509_LU_X509| or |X509_LU_CRL|, and the subject name from the store
5175 // in |vs|. If found and |ret| is not NULL, it increments the reference count
5176 // and stores the object in |ret|.
5177 OPENSSL_EXPORT int X509_STORE_CTX_get_by_subject(X509_STORE_CTX *vs, int type,
5178 X509_NAME *name,
5179 X509_OBJECT *ret);
5180
5181 // A BASIC_CONSTRAINTS_st, aka |BASIC_CONSTRAINTS| represents an
5182 // BasicConstraints structure (RFC 5280).
5183 struct BASIC_CONSTRAINTS_st {
5184 ASN1_BOOLEAN ca;
5185 ASN1_INTEGER *pathlen;
5186 } /* BASIC_CONSTRAINTS */;
5187
5188 // An ACCESS_DESCRIPTION represents an AccessDescription structure (RFC 5280).
5189 typedef struct ACCESS_DESCRIPTION_st {
5190 ASN1_OBJECT *method;
5191 GENERAL_NAME *location;
5192 } ACCESS_DESCRIPTION;
5193
5194 DEFINE_STACK_OF(ACCESS_DESCRIPTION)
5195
5196 typedef STACK_OF(ACCESS_DESCRIPTION) AUTHORITY_INFO_ACCESS;
5197
5198 typedef STACK_OF(ASN1_OBJECT) EXTENDED_KEY_USAGE;
5199
5200 // A DIST_POINT_NAME represents a DistributionPointName structure (RFC 5280).
5201 // The |name| field contains the CHOICE value and is determined by |type|. If
5202 // |type| is zero, |name| must be a |fullname|. If |type| is one, |name| must be
5203 // a |relativename|.
5204 //
5205 // |type| and |name| must be kept consistent. An inconsistency will result in a
5206 // potentially exploitable memory error.
5207 typedef struct DIST_POINT_NAME_st {
5208 int type;
5209 union {
5210 GENERAL_NAMES *fullname;
5211 STACK_OF(X509_NAME_ENTRY) *relativename;
5212 } name;
5213 // If relativename then this contains the full distribution point name
5214 X509_NAME *dpname;
5215 } DIST_POINT_NAME;
5216
5217 // A DIST_POINT_st, aka |DIST_POINT|, represents a DistributionPoint structure
5218 // (RFC 5280).
5219 struct DIST_POINT_st {
5220 DIST_POINT_NAME *distpoint;
5221 ASN1_BIT_STRING *reasons;
5222 GENERAL_NAMES *CRLissuer;
5223 } /* DIST_POINT */;
5224
5225 typedef STACK_OF(DIST_POINT) CRL_DIST_POINTS;
5226
5227 DEFINE_STACK_OF(DIST_POINT)
5228
5229 // A AUTHORITY_KEYID_st, aka |AUTHORITY_KEYID|, represents an
5230 // AuthorityKeyIdentifier structure (RFC 5280).
5231 struct AUTHORITY_KEYID_st {
5232 ASN1_OCTET_STRING *keyid;
5233 GENERAL_NAMES *issuer;
5234 ASN1_INTEGER *serial;
5235 } /* AUTHORITY_KEYID */;
5236
5237 // A NOTICEREF represents a NoticeReference structure (RFC 5280).
5238 typedef struct NOTICEREF_st {
5239 ASN1_STRING *organization;
5240 STACK_OF(ASN1_INTEGER) *noticenos;
5241 } NOTICEREF;
5242
5243 // A USERNOTICE represents a UserNotice structure (RFC 5280).
5244 typedef struct USERNOTICE_st {
5245 NOTICEREF *noticeref;
5246 ASN1_STRING *exptext;
5247 } USERNOTICE;
5248
5249 // A POLICYQUALINFO represents a PolicyQualifierInfo structure (RFC 5280). |d|
5250 // contains the qualifier field of the PolicyQualifierInfo. Its type is
5251 // determined by |pqualid|. If |pqualid| is |NID_id_qt_cps|, |d| must be
5252 // |cpsuri|. If |pqualid| is |NID_id_qt_unotice|, |d| must be |usernotice|.
5253 // Otherwise, |d| must be |other|.
5254 //
5255 // |pqualid| and |d| must be kept consistent. An inconsistency will result in a
5256 // potentially exploitable memory error.
5257 typedef struct POLICYQUALINFO_st {
5258 ASN1_OBJECT *pqualid;
5259 union {
5260 ASN1_IA5STRING *cpsuri;
5261 USERNOTICE *usernotice;
5262 ASN1_TYPE *other;
5263 } d;
5264 } POLICYQUALINFO;
5265
5266 DEFINE_STACK_OF(POLICYQUALINFO)
5267
5268 // A POLICYINFO represents a PolicyInformation structure (RFC 5280).
5269 typedef struct POLICYINFO_st {
5270 ASN1_OBJECT *policyid;
5271 STACK_OF(POLICYQUALINFO) *qualifiers;
5272 } POLICYINFO;
5273
5274 typedef STACK_OF(POLICYINFO) CERTIFICATEPOLICIES;
5275
5276 DEFINE_STACK_OF(POLICYINFO)
5277
5278 // A POLICY_MAPPING represents an individual element of a PolicyMappings
5279 // structure (RFC 5280).
5280 typedef struct POLICY_MAPPING_st {
5281 ASN1_OBJECT *issuerDomainPolicy;
5282 ASN1_OBJECT *subjectDomainPolicy;
5283 } POLICY_MAPPING;
5284
5285 DEFINE_STACK_OF(POLICY_MAPPING)
5286
5287 typedef STACK_OF(POLICY_MAPPING) POLICY_MAPPINGS;
5288
5289 // A GENERAL_SUBTREE represents a GeneralSubtree structure (RFC 5280).
5290 typedef struct GENERAL_SUBTREE_st {
5291 GENERAL_NAME *base;
5292 ASN1_INTEGER *minimum;
5293 ASN1_INTEGER *maximum;
5294 } GENERAL_SUBTREE;
5295
5296 DEFINE_STACK_OF(GENERAL_SUBTREE)
5297
5298 // A NAME_CONSTRAINTS_st, aka |NAME_CONSTRAINTS|, represents a NameConstraints
5299 // structure (RFC 5280).
5300 struct NAME_CONSTRAINTS_st {
5301 STACK_OF(GENERAL_SUBTREE) *permittedSubtrees;
5302 STACK_OF(GENERAL_SUBTREE) *excludedSubtrees;
5303 } /* NAME_CONSTRAINTS */;
5304
5305 // A POLICY_CONSTRAINTS represents a PolicyConstraints structure (RFC 5280).
5306 typedef struct POLICY_CONSTRAINTS_st {
5307 ASN1_INTEGER *requireExplicitPolicy;
5308 ASN1_INTEGER *inhibitPolicyMapping;
5309 } POLICY_CONSTRAINTS;
5310
5311 // A ISSUING_DIST_POINT_st, aka |ISSUING_DIST_POINT|, represents a
5312 // IssuingDistributionPoint structure (RFC 5280).
5313 struct ISSUING_DIST_POINT_st {
5314 DIST_POINT_NAME *distpoint;
5315 ASN1_BOOLEAN onlyuser;
5316 ASN1_BOOLEAN onlyCA;
5317 ASN1_BIT_STRING *onlysomereasons;
5318 ASN1_BOOLEAN indirectCRL;
5319 ASN1_BOOLEAN onlyattr;
5320 } /* ISSUING_DIST_POINT */;
5321
5322
5323 // BASIC_CONSTRAINTS is an |ASN1_ITEM| whose ASN.1 type is BasicConstraints (RFC
5324 // 5280) and C type is |BASIC_CONSTRAINTS*|.
5325 DECLARE_ASN1_ITEM(BASIC_CONSTRAINTS)
5326
5327 // BASIC_CONSTRAINTS_new returns a newly-allocated, empty |BASIC_CONSTRAINTS|
5328 // object, or NULL on error.
5329 OPENSSL_EXPORT BASIC_CONSTRAINTS *BASIC_CONSTRAINTS_new(void);
5330
5331 // BASIC_CONSTRAINTS_free releases memory associated with |bcons|.
5332 OPENSSL_EXPORT void BASIC_CONSTRAINTS_free(BASIC_CONSTRAINTS *bcons);
5333
5334 // d2i_BASIC_CONSTRAINTS parses up to |len| bytes from |*inp| as a DER-encoded
5335 // BasicConstraints (RFC 5280), as described in |d2i_SAMPLE|.
5336 OPENSSL_EXPORT BASIC_CONSTRAINTS *d2i_BASIC_CONSTRAINTS(BASIC_CONSTRAINTS **out,
5337 const uint8_t **inp,
5338 long len);
5339
5340 // i2d_BASIC_CONSTRAINTS marshals |bcons| as a DER-encoded BasicConstraints (RFC
5341 // 5280), as described in |i2d_SAMPLE|.
5342 OPENSSL_EXPORT int i2d_BASIC_CONSTRAINTS(const BASIC_CONSTRAINTS *bcons,
5343 uint8_t **outp);
5344
5345 // AUTHORITY_KEYID is an |ASN1_ITEM| whose ASN.1 type is AuthorityKeyIdentifier
5346 // (RFC 5280) and C type is |AUTHORITY_KEYID*|.
5347 DECLARE_ASN1_ITEM(AUTHORITY_KEYID)
5348
5349 // AUTHORITY_KEYID_new returns a newly-allocated, empty |AUTHORITY_KEYID|
5350 // object, or NULL on error.
5351 OPENSSL_EXPORT AUTHORITY_KEYID *AUTHORITY_KEYID_new(void);
5352
5353 // AUTHORITY_KEYID_free releases memory associated with |akid|.
5354 OPENSSL_EXPORT void AUTHORITY_KEYID_free(AUTHORITY_KEYID *akid);
5355
5356 // d2i_AUTHORITY_KEYID parses up to |len| bytes from |*inp| as a DER-encoded
5357 // AuthorityKeyIdentifier (RFC 5280), as described in |d2i_SAMPLE|.
5358 OPENSSL_EXPORT AUTHORITY_KEYID *d2i_AUTHORITY_KEYID(AUTHORITY_KEYID **out,
5359 const uint8_t **inp,
5360 long len);
5361
5362 // i2d_AUTHORITY_KEYID marshals |akid| as a DER-encoded AuthorityKeyIdentifier
5363 // (RFC 5280), as described in |i2d_SAMPLE|.
5364 //
5365 // TODO(https://crbug.com/boringssl/407): |akid| is not const because it
5366 // contains an |X509_NAME|.
5367 OPENSSL_EXPORT int i2d_AUTHORITY_KEYID(AUTHORITY_KEYID *akid, uint8_t **outp);
5368
5369 // EXTENDED_KEY_USAGE is an |ASN1_ITEM| whose ASN.1 type is ExtKeyUsageSyntax
5370 // (RFC 5280) and C type is |STACK_OF(ASN1_OBJECT)*|, or |EXTENDED_KEY_USAGE*|.
5371 DECLARE_ASN1_ITEM(EXTENDED_KEY_USAGE)
5372
5373 // EXTENDED_KEY_USAGE_new returns a newly-allocated, empty |EXTENDED_KEY_USAGE|
5374 // object, or NULL on error.
5375 OPENSSL_EXPORT EXTENDED_KEY_USAGE *EXTENDED_KEY_USAGE_new(void);
5376
5377 // EXTENDED_KEY_USAGE_free releases memory associated with |eku|.
5378 OPENSSL_EXPORT void EXTENDED_KEY_USAGE_free(EXTENDED_KEY_USAGE *eku);
5379
5380 // d2i_EXTENDED_KEY_USAGE parses up to |len| bytes from |*inp| as a DER-encoded
5381 // ExtKeyUsageSyntax (RFC 5280), as described in |d2i_SAMPLE|.
5382 OPENSSL_EXPORT EXTENDED_KEY_USAGE *d2i_EXTENDED_KEY_USAGE(
5383 EXTENDED_KEY_USAGE **out, const uint8_t **inp, long len);
5384
5385 // i2d_EXTENDED_KEY_USAGE marshals |eku| as a DER-encoded ExtKeyUsageSyntax (RFC
5386 // 5280), as described in |i2d_SAMPLE|.
5387 OPENSSL_EXPORT int i2d_EXTENDED_KEY_USAGE(const EXTENDED_KEY_USAGE *eku,
5388 uint8_t **outp);
5389
5390 // CERTIFICATEPOLICIES is an |ASN1_ITEM| whose ASN.1 type is CertificatePolicies
5391 // (RFC 5280) and C type is |STACK_OF(POLICYINFO)*|, or |CERTIFICATEPOLICIES*|.
5392 DECLARE_ASN1_ITEM(CERTIFICATEPOLICIES)
5393
5394 // CERTIFICATEPOLICIES_new returns a newly-allocated, empty
5395 // |CERTIFICATEPOLICIES| object, or NULL on error.
5396 OPENSSL_EXPORT CERTIFICATEPOLICIES *CERTIFICATEPOLICIES_new(void);
5397
5398 // CERTIFICATEPOLICIES_free releases memory associated with |policies|.
5399 OPENSSL_EXPORT void CERTIFICATEPOLICIES_free(CERTIFICATEPOLICIES *policies);
5400
5401 // d2i_CERTIFICATEPOLICIES parses up to |len| bytes from |*inp| as a DER-encoded
5402 // CertificatePolicies (RFC 5280), as described in |d2i_SAMPLE|.
5403 OPENSSL_EXPORT CERTIFICATEPOLICIES *d2i_CERTIFICATEPOLICIES(
5404 CERTIFICATEPOLICIES **out, const uint8_t **inp, long len);
5405
5406 // i2d_CERTIFICATEPOLICIES marshals |policies| as a DER-encoded
5407 // CertificatePolicies (RFC 5280), as described in |i2d_SAMPLE|.
5408 OPENSSL_EXPORT int i2d_CERTIFICATEPOLICIES(const CERTIFICATEPOLICIES *policies,
5409 uint8_t **outp);
5410
5411 // POLICYINFO_new returns a newly-allocated, empty |POLICYINFO| object, or NULL
5412 // on error.
5413 OPENSSL_EXPORT POLICYINFO *POLICYINFO_new(void);
5414
5415 // POLICYINFO_free releases memory associated with |info|.
5416 OPENSSL_EXPORT void POLICYINFO_free(POLICYINFO *info);
5417
5418 // POLICYQUALINFO_new returns a newly-allocated, empty |POLICYQUALINFO| object,
5419 // or NULL on error.
5420 OPENSSL_EXPORT POLICYQUALINFO *POLICYQUALINFO_new(void);
5421
5422 // POLICYQUALINFO_free releases memory associated with |info|.
5423 OPENSSL_EXPORT void POLICYQUALINFO_free(POLICYQUALINFO *info);
5424
5425 // USERNOTICE_new returns a newly-allocated, empty |USERNOTICE| object, or NULL
5426 // on error.
5427 OPENSSL_EXPORT USERNOTICE *USERNOTICE_new(void);
5428
5429 // USERNOTICE_free releases memory associated with |notice|.
5430 OPENSSL_EXPORT void USERNOTICE_free(USERNOTICE *notice);
5431
5432 // NOTICEREF_new returns a newly-allocated, empty |NOTICEREF| object, or NULL
5433 // on error.
5434 OPENSSL_EXPORT NOTICEREF *NOTICEREF_new(void);
5435
5436 // NOTICEREF_free releases memory associated with |ref|.
5437 OPENSSL_EXPORT void NOTICEREF_free(NOTICEREF *ref);
5438
5439 // CRL_DIST_POINTS is an |ASN1_ITEM| whose ASN.1 type is CRLDistributionPoints
5440 // (RFC 5280) and C type is |CRL_DIST_POINTS*|.
5441 DECLARE_ASN1_ITEM(CRL_DIST_POINTS)
5442
5443 // CRL_DIST_POINTS_new returns a newly-allocated, empty |CRL_DIST_POINTS|
5444 // object, or NULL on error.
5445 OPENSSL_EXPORT CRL_DIST_POINTS *CRL_DIST_POINTS_new(void);
5446
5447 // CRL_DIST_POINTS_free releases memory associated with |crldp|.
5448 OPENSSL_EXPORT void CRL_DIST_POINTS_free(CRL_DIST_POINTS *crldp);
5449
5450 // d2i_CRL_DIST_POINTS parses up to |len| bytes from |*inp| as a DER-encoded
5451 // CRLDistributionPoints (RFC 5280), as described in |d2i_SAMPLE|.
5452 OPENSSL_EXPORT CRL_DIST_POINTS *d2i_CRL_DIST_POINTS(CRL_DIST_POINTS **out,
5453 const uint8_t **inp,
5454 long len);
5455
5456 // i2d_CRL_DIST_POINTS marshals |crldp| as a DER-encoded CRLDistributionPoints
5457 // (RFC 5280), as described in |i2d_SAMPLE|.
5458 //
5459 // TODO(https://crbug.com/boringssl/407): |crldp| is not const because it
5460 // contains an |X509_NAME|.
5461 OPENSSL_EXPORT int i2d_CRL_DIST_POINTS(CRL_DIST_POINTS *crldp, uint8_t **outp);
5462
5463 // DIST_POINT_new returns a newly-allocated, empty |DIST_POINT| object, or NULL
5464 // on error.
5465 OPENSSL_EXPORT DIST_POINT *DIST_POINT_new(void);
5466
5467 // DIST_POINT_free releases memory associated with |dp|.
5468 OPENSSL_EXPORT void DIST_POINT_free(DIST_POINT *dp);
5469
5470 // DIST_POINT_NAME_new returns a newly-allocated, empty |DIST_POINT_NAME|
5471 // object, or NULL on error.
5472 OPENSSL_EXPORT DIST_POINT_NAME *DIST_POINT_NAME_new(void);
5473
5474 // DIST_POINT_NAME_free releases memory associated with |name|.
5475 OPENSSL_EXPORT void DIST_POINT_NAME_free(DIST_POINT_NAME *name);
5476
5477 // ISSUING_DIST_POINT is an |ASN1_ITEM| whose ASN.1 type is
5478 // IssuingDistributionPoint (RFC 5280) and C type is |ISSUING_DIST_POINT*|.
5479 DECLARE_ASN1_ITEM(ISSUING_DIST_POINT)
5480
5481 // ISSUING_DIST_POINT_new returns a newly-allocated, empty |ISSUING_DIST_POINT|
5482 // object, or NULL on error.
5483 OPENSSL_EXPORT ISSUING_DIST_POINT *ISSUING_DIST_POINT_new(void);
5484
5485 // ISSUING_DIST_POINT_free releases memory associated with |idp|.
5486 OPENSSL_EXPORT void ISSUING_DIST_POINT_free(ISSUING_DIST_POINT *idp);
5487
5488 // d2i_ISSUING_DIST_POINT parses up to |len| bytes from |*inp| as a DER-encoded
5489 // IssuingDistributionPoint (RFC 5280), as described in |d2i_SAMPLE|.
5490 OPENSSL_EXPORT ISSUING_DIST_POINT *d2i_ISSUING_DIST_POINT(
5491 ISSUING_DIST_POINT **out, const uint8_t **inp, long len);
5492
5493 // i2d_ISSUING_DIST_POINT marshals |idp| as a DER-encoded
5494 // IssuingDistributionPoint (RFC 5280), as described in |i2d_SAMPLE|.
5495 //
5496 // TODO(https://crbug.com/boringssl/407): |idp| is not const because it
5497 // contains an |X509_NAME|.
5498 OPENSSL_EXPORT int i2d_ISSUING_DIST_POINT(ISSUING_DIST_POINT *idp,
5499 uint8_t **outp);
5500
5501 // ACCESS_DESCRIPTION_new returns a newly-allocated, empty |ACCESS_DESCRIPTION|
5502 // object, or NULL on error.
5503 OPENSSL_EXPORT ACCESS_DESCRIPTION *ACCESS_DESCRIPTION_new(void);
5504
5505 // ACCESS_DESCRIPTION_free releases memory associated with |desc|.
5506 OPENSSL_EXPORT void ACCESS_DESCRIPTION_free(ACCESS_DESCRIPTION *desc);
5507
5508 // AUTHORITY_INFO_ACCESS is an |ASN1_ITEM| whose ASN.1 type is
5509 // AuthorityInfoAccessSyntax (RFC 5280) and C type is
5510 // |STACK_OF(ACCESS_DESCRIPTION)*|, or |AUTHORITY_INFO_ACCESS*|.
5511 DECLARE_ASN1_ITEM(AUTHORITY_INFO_ACCESS)
5512
5513 // AUTHORITY_INFO_ACCESS_new returns a newly-allocated, empty
5514 // |AUTHORITY_INFO_ACCESS| object, or NULL on error.
5515 OPENSSL_EXPORT AUTHORITY_INFO_ACCESS *AUTHORITY_INFO_ACCESS_new(void);
5516
5517 // AUTHORITY_INFO_ACCESS_free releases memory associated with |aia|.
5518 OPENSSL_EXPORT void AUTHORITY_INFO_ACCESS_free(AUTHORITY_INFO_ACCESS *aia);
5519
5520 // d2i_AUTHORITY_INFO_ACCESS parses up to |len| bytes from |*inp| as a
5521 // DER-encoded AuthorityInfoAccessSyntax (RFC 5280), as described in
5522 // |d2i_SAMPLE|.
5523 OPENSSL_EXPORT AUTHORITY_INFO_ACCESS *d2i_AUTHORITY_INFO_ACCESS(
5524 AUTHORITY_INFO_ACCESS **out, const uint8_t **inp, long len);
5525
5526 // i2d_AUTHORITY_INFO_ACCESS marshals |aia| as a DER-encoded
5527 // AuthorityInfoAccessSyntax (RFC 5280), as described in |i2d_SAMPLE|.
5528 //
5529 // TODO(https://crbug.com/boringssl/407): |aia| is not const because it
5530 // contains an |X509_NAME|.
5531 OPENSSL_EXPORT int i2d_AUTHORITY_INFO_ACCESS(AUTHORITY_INFO_ACCESS *aia,
5532 uint8_t **outp);
5533
5534 // POLICY_MAPPING_new returns a newly-allocated, empty |POLICY_MAPPING| object,
5535 // or NULL on error.
5536 OPENSSL_EXPORT POLICY_MAPPING *POLICY_MAPPING_new(void);
5537
5538 // POLICY_MAPPING_free releases memory associated with |mapping|.
5539 OPENSSL_EXPORT void POLICY_MAPPING_free(POLICY_MAPPING *mapping);
5540
5541 // POLICY_MAPPINGS is an |ASN1_ITEM| whose ASN.1 type is PolicyMappings (RFC
5542 // 5280) and C type is |STACK_OF(POLICY_MAPPING)*|, or |POLICY_MAPPINGS*|.
5543 DECLARE_ASN1_ITEM(POLICY_MAPPINGS)
5544
5545 // GENERAL_SUBTREE_new returns a newly-allocated, empty |GENERAL_SUBTREE|
5546 // object, or NULL on error.
5547 OPENSSL_EXPORT GENERAL_SUBTREE *GENERAL_SUBTREE_new(void);
5548
5549 // GENERAL_SUBTREE_free releases memory associated with |subtree|.
5550 OPENSSL_EXPORT void GENERAL_SUBTREE_free(GENERAL_SUBTREE *subtree);
5551
5552 // NAME_CONSTRAINTS is an |ASN1_ITEM| whose ASN.1 type is NameConstraints (RFC
5553 // 5280) and C type is |NAME_CONSTRAINTS*|.
5554 DECLARE_ASN1_ITEM(NAME_CONSTRAINTS)
5555
5556 // NAME_CONSTRAINTS_new returns a newly-allocated, empty |NAME_CONSTRAINTS|
5557 // object, or NULL on error.
5558 OPENSSL_EXPORT NAME_CONSTRAINTS *NAME_CONSTRAINTS_new(void);
5559
5560 // NAME_CONSTRAINTS_free releases memory associated with |ncons|.
5561 OPENSSL_EXPORT void NAME_CONSTRAINTS_free(NAME_CONSTRAINTS *ncons);
5562
5563 // POLICY_CONSTRAINTS_new returns a newly-allocated, empty |POLICY_CONSTRAINTS|
5564 // object, or NULL on error.
5565 OPENSSL_EXPORT POLICY_CONSTRAINTS *POLICY_CONSTRAINTS_new(void);
5566
5567 // POLICY_CONSTRAINTS_free releases memory associated with |pcons|.
5568 OPENSSL_EXPORT void POLICY_CONSTRAINTS_free(POLICY_CONSTRAINTS *pcons);
5569
5570 // POLICY_CONSTRAINTS is an |ASN1_ITEM| whose ASN.1 type is PolicyConstraints
5571 // (RFC 5280) and C type is |POLICY_CONSTRAINTS*|.
5572 DECLARE_ASN1_ITEM(POLICY_CONSTRAINTS)
5573
5574
5575 OPENSSL_EXPORT int X509_PURPOSE_set(int *p, int purpose);
5576
5577 OPENSSL_EXPORT int X509_PURPOSE_get_count(void);
5578 OPENSSL_EXPORT int X509_PURPOSE_get_by_id(int id);
5579 OPENSSL_EXPORT char *X509_PURPOSE_get0_name(const X509_PURPOSE *xp);
5580 OPENSSL_EXPORT char *X509_PURPOSE_get0_sname(const X509_PURPOSE *xp);
5581 OPENSSL_EXPORT int X509_PURPOSE_get_trust(const X509_PURPOSE *xp);
5582
5583
5584 #if defined(__cplusplus)
5585 } // extern C
5586 #endif
5587
5588 #if !defined(BORINGSSL_NO_CXX)
5589 extern "C++" {
5590
5591 BSSL_NAMESPACE_BEGIN
5592
5593 BORINGSSL_MAKE_DELETER(ACCESS_DESCRIPTION, ACCESS_DESCRIPTION_free)
5594 BORINGSSL_MAKE_DELETER(AUTHORITY_KEYID, AUTHORITY_KEYID_free)
5595 BORINGSSL_MAKE_DELETER(BASIC_CONSTRAINTS, BASIC_CONSTRAINTS_free)
5596 // TODO(davidben): Move this to conf.h and rename to CONF_VALUE_free.
5597 BORINGSSL_MAKE_DELETER(CONF_VALUE, X509V3_conf_free)
5598 BORINGSSL_MAKE_DELETER(DIST_POINT, DIST_POINT_free)
5599 BORINGSSL_MAKE_DELETER(GENERAL_NAME, GENERAL_NAME_free)
5600 BORINGSSL_MAKE_DELETER(GENERAL_SUBTREE, GENERAL_SUBTREE_free)
5601 BORINGSSL_MAKE_DELETER(NAME_CONSTRAINTS, NAME_CONSTRAINTS_free)
5602 BORINGSSL_MAKE_DELETER(NETSCAPE_SPKI, NETSCAPE_SPKI_free)
5603 BORINGSSL_MAKE_DELETER(POLICY_MAPPING, POLICY_MAPPING_free)
5604 BORINGSSL_MAKE_DELETER(POLICYINFO, POLICYINFO_free)
5605 BORINGSSL_MAKE_DELETER(RSA_PSS_PARAMS, RSA_PSS_PARAMS_free)
5606 BORINGSSL_MAKE_DELETER(X509, X509_free)
5607 BORINGSSL_MAKE_UP_REF(X509, X509_up_ref)
5608 BORINGSSL_MAKE_DELETER(X509_ALGOR, X509_ALGOR_free)
5609 BORINGSSL_MAKE_DELETER(X509_ATTRIBUTE, X509_ATTRIBUTE_free)
5610 BORINGSSL_MAKE_DELETER(X509_CRL, X509_CRL_free)
5611 BORINGSSL_MAKE_UP_REF(X509_CRL, X509_CRL_up_ref)
5612 BORINGSSL_MAKE_DELETER(X509_EXTENSION, X509_EXTENSION_free)
5613 BORINGSSL_MAKE_DELETER(X509_INFO, X509_INFO_free)
5614 BORINGSSL_MAKE_DELETER(X509_LOOKUP, X509_LOOKUP_free)
5615 BORINGSSL_MAKE_DELETER(X509_NAME, X509_NAME_free)
5616 BORINGSSL_MAKE_DELETER(X509_NAME_ENTRY, X509_NAME_ENTRY_free)
5617 BORINGSSL_MAKE_DELETER(X509_OBJECT, X509_OBJECT_free)
5618 BORINGSSL_MAKE_DELETER(X509_PUBKEY, X509_PUBKEY_free)
5619 BORINGSSL_MAKE_DELETER(X509_REQ, X509_REQ_free)
5620 BORINGSSL_MAKE_DELETER(X509_REVOKED, X509_REVOKED_free)
5621 BORINGSSL_MAKE_DELETER(X509_SIG, X509_SIG_free)
5622 BORINGSSL_MAKE_DELETER(X509_STORE, X509_STORE_free)
5623 BORINGSSL_MAKE_UP_REF(X509_STORE, X509_STORE_up_ref)
5624 BORINGSSL_MAKE_DELETER(X509_STORE_CTX, X509_STORE_CTX_free)
5625 BORINGSSL_MAKE_DELETER(X509_VERIFY_PARAM, X509_VERIFY_PARAM_free)
5626
5627 BSSL_NAMESPACE_END
5628
5629 } // extern C++
5630 #endif // !BORINGSSL_NO_CXX
5631
5632 #define X509_R_AKID_MISMATCH 100
5633 #define X509_R_BAD_PKCS7_VERSION 101
5634 #define X509_R_BAD_X509_FILETYPE 102
5635 #define X509_R_BASE64_DECODE_ERROR 103
5636 #define X509_R_CANT_CHECK_DH_KEY 104
5637 #define X509_R_CERT_ALREADY_IN_HASH_TABLE 105
5638 #define X509_R_CRL_ALREADY_DELTA 106
5639 #define X509_R_CRL_VERIFY_FAILURE 107
5640 #define X509_R_IDP_MISMATCH 108
5641 #define X509_R_INVALID_BIT_STRING_BITS_LEFT 109
5642 #define X509_R_INVALID_DIRECTORY 110
5643 #define X509_R_INVALID_FIELD_NAME 111
5644 #define X509_R_INVALID_PSS_PARAMETERS 112
5645 #define X509_R_INVALID_TRUST 113
5646 #define X509_R_ISSUER_MISMATCH 114
5647 #define X509_R_KEY_TYPE_MISMATCH 115
5648 #define X509_R_KEY_VALUES_MISMATCH 116
5649 #define X509_R_LOADING_CERT_DIR 117
5650 #define X509_R_LOADING_DEFAULTS 118
5651 #define X509_R_NEWER_CRL_NOT_NEWER 119
5652 #define X509_R_NOT_PKCS7_SIGNED_DATA 120
5653 #define X509_R_NO_CERTIFICATES_INCLUDED 121
5654 #define X509_R_NO_CERT_SET_FOR_US_TO_VERIFY 122
5655 #define X509_R_NO_CRLS_INCLUDED 123
5656 #define X509_R_NO_CRL_NUMBER 124
5657 #define X509_R_PUBLIC_KEY_DECODE_ERROR 125
5658 #define X509_R_PUBLIC_KEY_ENCODE_ERROR 126
5659 #define X509_R_SHOULD_RETRY 127
5660 #define X509_R_UNKNOWN_KEY_TYPE 128
5661 #define X509_R_UNKNOWN_NID 129
5662 #define X509_R_UNKNOWN_PURPOSE_ID 130
5663 #define X509_R_UNKNOWN_TRUST_ID 131
5664 #define X509_R_UNSUPPORTED_ALGORITHM 132
5665 #define X509_R_WRONG_LOOKUP_TYPE 133
5666 #define X509_R_WRONG_TYPE 134
5667 #define X509_R_NAME_TOO_LONG 135
5668 #define X509_R_INVALID_PARAMETER 136
5669 #define X509_R_SIGNATURE_ALGORITHM_MISMATCH 137
5670 #define X509_R_DELTA_CRL_WITHOUT_CRL_NUMBER 138
5671 #define X509_R_INVALID_FIELD_FOR_VERSION 139
5672 #define X509_R_INVALID_VERSION 140
5673 #define X509_R_NO_CERTIFICATE_FOUND 141
5674 #define X509_R_NO_CERTIFICATE_OR_CRL_FOUND 142
5675 #define X509_R_NO_CRL_FOUND 143
5676 #define X509_R_INVALID_POLICY_EXTENSION 144
5677 #define X509_R_UNKNOWN_SIGID_ALGS 145
5678
5679 #endif // OPENSSL_HEADER_X509_H