• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.]
56  */
57 /* ====================================================================
58  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
59  * ECDH support in OpenSSL originally developed by
60  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
61  */
62 
63 #ifndef OPENSSL_HEADER_X509_H
64 #define OPENSSL_HEADER_X509_H
65 
66 #include <openssl/asn1.h>
67 #include <openssl/base.h>
68 #include <openssl/bio.h>
69 #include <openssl/cipher.h>
70 #include <openssl/dh.h>
71 #include <openssl/dsa.h>
72 #include <openssl/ec.h>
73 #include <openssl/ecdh.h>
74 #include <openssl/ecdsa.h>
75 #include <openssl/evp.h>
76 #include <openssl/obj.h>
77 #include <openssl/pkcs7.h>
78 #include <openssl/pool.h>
79 #include <openssl/rsa.h>
80 #include <openssl/sha.h>
81 #include <openssl/stack.h>
82 #include <openssl/thread.h>
83 #include <time.h>
84 
85 #if defined(__cplusplus)
86 extern "C" {
87 #endif
88 
89 
90 // Legacy X.509 library.
91 //
92 // This header is part of OpenSSL's X.509 implementation. It is retained for
93 // compatibility but should not be used by new code. The functions are difficult
94 // to use correctly, and have buggy or non-standard behaviors. They are thus
95 // particularly prone to behavior changes and API removals, as BoringSSL
96 // iterates on these issues.
97 //
98 // In the future, a replacement library will be available. Meanwhile, minimize
99 // dependencies on this header where possible.
100 //
101 // TODO(https://crbug.com/boringssl/426): Documentation for this library is
102 // still in progress. Some functions have not yet been documented, and some
103 // functions have not yet been grouped into sections.
104 
105 
106 // Certificates.
107 //
108 // An |X509| object represents an X.509 certificate, defined in RFC 5280.
109 //
110 // Although an |X509| is a mutable object, mutating an |X509| can give incorrect
111 // results. Callers typically obtain |X509|s by parsing some input with
112 // |d2i_X509|, etc. Such objects carry information such as the serialized
113 // TBSCertificate and decoded extensions, which will become inconsistent when
114 // mutated.
115 //
116 // Instead, mutation functions should only be used when issuing new
117 // certificates, as described in a later section.
118 
119 DEFINE_STACK_OF(X509)
120 
121 // X509 is an |ASN1_ITEM| whose ASN.1 type is X.509 Certificate (RFC 5280) and C
122 // type is |X509*|.
123 DECLARE_ASN1_ITEM(X509)
124 
125 // X509_up_ref adds one to the reference count of |x509| and returns one.
126 OPENSSL_EXPORT int X509_up_ref(X509 *x509);
127 
128 // X509_chain_up_ref returns a newly-allocated |STACK_OF(X509)| containing a
129 // shallow copy of |chain|, or NULL on error. That is, the return value has the
130 // same contents as |chain|, and each |X509|'s reference count is incremented by
131 // one.
132 OPENSSL_EXPORT STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain);
133 
134 // X509_dup returns a newly-allocated copy of |x509|, or NULL on error. This
135 // function works by serializing the structure, so auxiliary properties (see
136 // |i2d_X509_AUX|) are not preserved. Additionally, if |x509| is incomplete,
137 // this function may fail.
138 //
139 // TODO(https://crbug.com/boringssl/407): This function should be const and
140 // thread-safe but is currently neither in some cases, notably if |crl| was
141 // mutated.
142 OPENSSL_EXPORT X509 *X509_dup(X509 *x509);
143 
144 // X509_free decrements |x509|'s reference count and, if zero, releases memory
145 // associated with |x509|.
146 OPENSSL_EXPORT void X509_free(X509 *x509);
147 
148 // d2i_X509 parses up to |len| bytes from |*inp| as a DER-encoded X.509
149 // Certificate (RFC 5280), as described in |d2i_SAMPLE|.
150 OPENSSL_EXPORT X509 *d2i_X509(X509 **out, const uint8_t **inp, long len);
151 
152 // X509_parse_from_buffer parses an X.509 structure from |buf| and returns a
153 // fresh X509 or NULL on error. There must not be any trailing data in |buf|.
154 // The returned structure (if any) holds a reference to |buf| rather than
155 // copying parts of it as a normal |d2i_X509| call would do.
156 OPENSSL_EXPORT X509 *X509_parse_from_buffer(CRYPTO_BUFFER *buf);
157 
158 // i2d_X509 marshals |x509| as a DER-encoded X.509 Certificate (RFC 5280), as
159 // described in |i2d_SAMPLE|.
160 //
161 // TODO(https://crbug.com/boringssl/407): This function should be const and
162 // thread-safe but is currently neither in some cases, notably if |x509| was
163 // mutated.
164 OPENSSL_EXPORT int i2d_X509(X509 *x509, uint8_t **outp);
165 
166 // X509_VERSION_* are X.509 version numbers. Note the numerical values of all
167 // defined X.509 versions are one less than the named version.
168 #define X509_VERSION_1 0
169 #define X509_VERSION_2 1
170 #define X509_VERSION_3 2
171 
172 // X509_get_version returns the numerical value of |x509|'s version, which will
173 // be one of the |X509_VERSION_*| constants.
174 OPENSSL_EXPORT long X509_get_version(const X509 *x509);
175 
176 // X509_get0_serialNumber returns |x509|'s serial number.
177 OPENSSL_EXPORT const ASN1_INTEGER *X509_get0_serialNumber(const X509 *x509);
178 
179 // X509_get0_notBefore returns |x509|'s notBefore time.
180 OPENSSL_EXPORT const ASN1_TIME *X509_get0_notBefore(const X509 *x509);
181 
182 // X509_get0_notAfter returns |x509|'s notAfter time.
183 OPENSSL_EXPORT const ASN1_TIME *X509_get0_notAfter(const X509 *x509);
184 
185 // X509_get_issuer_name returns |x509|'s issuer.
186 OPENSSL_EXPORT X509_NAME *X509_get_issuer_name(const X509 *x509);
187 
188 // X509_get_subject_name returns |x509|'s subject.
189 OPENSSL_EXPORT X509_NAME *X509_get_subject_name(const X509 *x509);
190 
191 // X509_get_X509_PUBKEY returns the public key of |x509|. Note this function is
192 // not const-correct for legacy reasons. Callers should not modify the returned
193 // object.
194 OPENSSL_EXPORT X509_PUBKEY *X509_get_X509_PUBKEY(const X509 *x509);
195 
196 // X509_get_pubkey returns |x509|'s public key as an |EVP_PKEY|, or NULL if the
197 // public key was unsupported or could not be decoded. This function returns a
198 // reference to the |EVP_PKEY|. The caller must release the result with
199 // |EVP_PKEY_free| when done.
200 OPENSSL_EXPORT EVP_PKEY *X509_get_pubkey(X509 *x509);
201 
202 // X509_get0_pubkey_bitstr returns the BIT STRING portion of |x509|'s public
203 // key. Note this does not contain the AlgorithmIdentifier portion.
204 //
205 // WARNING: This function returns a non-const pointer for OpenSSL compatibility,
206 // but the caller must not modify the resulting object. Doing so will break
207 // internal invariants in |x509|.
208 OPENSSL_EXPORT ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x509);
209 
210 // X509_get0_uids sets |*out_issuer_uid| to a non-owning pointer to the
211 // issuerUID field of |x509|, or NULL if |x509| has no issuerUID. It similarly
212 // outputs |x509|'s subjectUID field to |*out_subject_uid|.
213 //
214 // Callers may pass NULL to either |out_issuer_uid| or |out_subject_uid| to
215 // ignore the corresponding field.
216 OPENSSL_EXPORT void X509_get0_uids(const X509 *x509,
217                                    const ASN1_BIT_STRING **out_issuer_uid,
218                                    const ASN1_BIT_STRING **out_subject_uid);
219 
220 // X509_get0_extensions returns |x509|'s extension list, or NULL if |x509| omits
221 // it.
222 OPENSSL_EXPORT const STACK_OF(X509_EXTENSION) *X509_get0_extensions(
223     const X509 *x509);
224 
225 // X509_get_ext_count returns the number of extensions in |x|.
226 OPENSSL_EXPORT int X509_get_ext_count(const X509 *x);
227 
228 // X509_get_ext_by_NID behaves like |X509v3_get_ext_by_NID| but searches for
229 // extensions in |x|.
230 OPENSSL_EXPORT int X509_get_ext_by_NID(const X509 *x, int nid, int lastpos);
231 
232 // X509_get_ext_by_OBJ behaves like |X509v3_get_ext_by_OBJ| but searches for
233 // extensions in |x|.
234 OPENSSL_EXPORT int X509_get_ext_by_OBJ(const X509 *x, const ASN1_OBJECT *obj,
235                                        int lastpos);
236 
237 // X509_get_ext_by_critical behaves like |X509v3_get_ext_by_critical| but
238 // searches for extensions in |x|.
239 OPENSSL_EXPORT int X509_get_ext_by_critical(const X509 *x, int crit,
240                                             int lastpos);
241 
242 // X509_get_ext returns the extension in |x| at index |loc|, or NULL if |loc| is
243 // out of bounds. This function returns a non-const pointer for OpenSSL
244 // compatibility, but callers should not mutate the result.
245 OPENSSL_EXPORT X509_EXTENSION *X509_get_ext(const X509 *x, int loc);
246 
247 // X509_get0_tbs_sigalg returns the signature algorithm in |x509|'s
248 // TBSCertificate. For the outer signature algorithm, see |X509_get0_signature|.
249 //
250 // Certificates with mismatched signature algorithms will successfully parse,
251 // but they will be rejected when verifying.
252 OPENSSL_EXPORT const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x509);
253 
254 // X509_get0_signature sets |*out_sig| and |*out_alg| to the signature and
255 // signature algorithm of |x509|, respectively. Either output pointer may be
256 // NULL to ignore the value.
257 //
258 // This function outputs the outer signature algorithm. For the one in the
259 // TBSCertificate, see |X509_get0_tbs_sigalg|. Certificates with mismatched
260 // signature algorithms will successfully parse, but they will be rejected when
261 // verifying.
262 OPENSSL_EXPORT void X509_get0_signature(const ASN1_BIT_STRING **out_sig,
263                                         const X509_ALGOR **out_alg,
264                                         const X509 *x509);
265 
266 // X509_get_signature_nid returns the NID corresponding to |x509|'s signature
267 // algorithm, or |NID_undef| if the signature algorithm does not correspond to
268 // a known NID.
269 OPENSSL_EXPORT int X509_get_signature_nid(const X509 *x509);
270 
271 // i2d_X509_tbs serializes the TBSCertificate portion of |x509|, as described in
272 // |i2d_SAMPLE|.
273 //
274 // This function preserves the original encoding of the TBSCertificate and may
275 // not reflect modifications made to |x509|. It may be used to manually verify
276 // the signature of an existing certificate. To generate certificates, use
277 // |i2d_re_X509_tbs| instead.
278 OPENSSL_EXPORT int i2d_X509_tbs(X509 *x509, unsigned char **outp);
279 
280 // X509_verify checks that |x509| has a valid signature by |pkey|. It returns
281 // one if the signature is valid and zero otherwise. Note this function only
282 // checks the signature itself and does not perform a full certificate
283 // validation.
284 OPENSSL_EXPORT int X509_verify(X509 *x509, EVP_PKEY *pkey);
285 
286 
287 // Issuing certificates.
288 //
289 // An |X509| object may also represent an incomplete certificate. Callers may
290 // construct empty |X509| objects, fill in fields individually, and finally sign
291 // the result. The following functions may be used for this purpose.
292 
293 // X509_new returns a newly-allocated, empty |X509| object, or NULL on error.
294 // This produces an incomplete certificate which may be filled in to issue a new
295 // certificate.
296 OPENSSL_EXPORT X509 *X509_new(void);
297 
298 // X509_set_version sets |x509|'s version to |version|, which should be one of
299 // the |X509V_VERSION_*| constants. It returns one on success and zero on error.
300 //
301 // If unsure, use |X509_VERSION_3|.
302 OPENSSL_EXPORT int X509_set_version(X509 *x509, long version);
303 
304 // X509_set_serialNumber sets |x509|'s serial number to |serial|. It returns one
305 // on success and zero on error.
306 OPENSSL_EXPORT int X509_set_serialNumber(X509 *x509,
307                                          const ASN1_INTEGER *serial);
308 
309 // X509_set1_notBefore sets |x509|'s notBefore time to |tm|. It returns one on
310 // success and zero on error.
311 OPENSSL_EXPORT int X509_set1_notBefore(X509 *x509, const ASN1_TIME *tm);
312 
313 // X509_set1_notAfter sets |x509|'s notAfter time to |tm|. it returns one on
314 // success and zero on error.
315 OPENSSL_EXPORT int X509_set1_notAfter(X509 *x509, const ASN1_TIME *tm);
316 
317 // X509_getm_notBefore returns a mutable pointer to |x509|'s notBefore time.
318 OPENSSL_EXPORT ASN1_TIME *X509_getm_notBefore(X509 *x509);
319 
320 // X509_getm_notAfter returns a mutable pointer to |x509|'s notAfter time.
321 OPENSSL_EXPORT ASN1_TIME *X509_getm_notAfter(X509 *x);
322 
323 // X509_set_issuer_name sets |x509|'s issuer to a copy of |name|. It returns one
324 // on success and zero on error.
325 OPENSSL_EXPORT int X509_set_issuer_name(X509 *x509, X509_NAME *name);
326 
327 // X509_set_subject_name sets |x509|'s subject to a copy of |name|. It returns
328 // one on success and zero on error.
329 OPENSSL_EXPORT int X509_set_subject_name(X509 *x509, X509_NAME *name);
330 
331 // X509_set_pubkey sets |x509|'s public key to |pkey|. It returns one on success
332 // and zero on error. This function does not take ownership of |pkey| and
333 // internally copies and updates reference counts as needed.
334 OPENSSL_EXPORT int X509_set_pubkey(X509 *x509, EVP_PKEY *pkey);
335 
336 // X509_delete_ext removes the extension in |x| at index |loc| and returns the
337 // removed extension, or NULL if |loc| was out of bounds. If non-NULL, the
338 // caller must release the result with |X509_EXTENSION_free|.
339 OPENSSL_EXPORT X509_EXTENSION *X509_delete_ext(X509 *x, int loc);
340 
341 // X509_add_ext adds a copy of |ex| to |x|. It returns one on success and zero
342 // on failure. The caller retains ownership of |ex| and can release it
343 // independently of |x|.
344 //
345 // The new extension is inserted at index |loc|, shifting extensions to the
346 // right. If |loc| is -1 or out of bounds, the new extension is appended to the
347 // list.
348 OPENSSL_EXPORT int X509_add_ext(X509 *x, const X509_EXTENSION *ex, int loc);
349 
350 // X509_sign signs |x509| with |pkey| and replaces the signature algorithm and
351 // signature fields. It returns one on success and zero on error. This function
352 // uses digest algorithm |md|, or |pkey|'s default if NULL. Other signing
353 // parameters use |pkey|'s defaults. To customize them, use |X509_sign_ctx|.
354 OPENSSL_EXPORT int X509_sign(X509 *x509, EVP_PKEY *pkey, const EVP_MD *md);
355 
356 // X509_sign_ctx signs |x509| with |ctx| and replaces the signature algorithm
357 // and signature fields. It returns one on success and zero on error. The
358 // signature algorithm and parameters come from |ctx|, which must have been
359 // initialized with |EVP_DigestSignInit|. The caller should configure the
360 // corresponding |EVP_PKEY_CTX| before calling this function.
361 OPENSSL_EXPORT int X509_sign_ctx(X509 *x509, EVP_MD_CTX *ctx);
362 
363 // i2d_re_X509_tbs serializes the TBSCertificate portion of |x509|, as described
364 // in |i2d_SAMPLE|.
365 //
366 // This function re-encodes the TBSCertificate and may not reflect |x509|'s
367 // original encoding. It may be used to manually generate a signature for a new
368 // certificate. To verify certificates, use |i2d_X509_tbs| instead.
369 OPENSSL_EXPORT int i2d_re_X509_tbs(X509 *x509, unsigned char **outp);
370 
371 // X509_set1_signature_algo sets |x509|'s signature algorithm to |algo| and
372 // returns one on success or zero on error. It updates both the signature field
373 // of the TBSCertificate structure, and the signatureAlgorithm field of the
374 // Certificate.
375 OPENSSL_EXPORT int X509_set1_signature_algo(X509 *x509, const X509_ALGOR *algo);
376 
377 // X509_set1_signature_value sets |x509|'s signature to a copy of the |sig_len|
378 // bytes pointed by |sig|. It returns one on success and zero on error.
379 //
380 // Due to a specification error, X.509 certificates store signatures in ASN.1
381 // BIT STRINGs, but signature algorithms return byte strings rather than bit
382 // strings. This function creates a BIT STRING containing a whole number of
383 // bytes, with the bit order matching the DER encoding. This matches the
384 // encoding used by all X.509 signature algorithms.
385 OPENSSL_EXPORT int X509_set1_signature_value(X509 *x509, const uint8_t *sig,
386                                              size_t sig_len);
387 
388 
389 // Auxiliary certificate properties.
390 //
391 // |X509| objects optionally maintain auxiliary properties. These are not part
392 // of the certificates themselves, and thus are not covered by signatures or
393 // preserved by the standard serialization. They are used as inputs or outputs
394 // to other functions in this library.
395 
396 // i2d_X509_AUX marshals |x509| as a DER-encoded X.509 Certificate (RFC 5280),
397 // followed optionally by a separate, OpenSSL-specific structure with auxiliary
398 // properties. It behaves as described in |i2d_SAMPLE|.
399 //
400 // Unlike similarly-named functions, this function does not output a single
401 // ASN.1 element. Directly embedding the output in a larger ASN.1 structure will
402 // not behave correctly.
403 OPENSSL_EXPORT int i2d_X509_AUX(X509 *x509, unsigned char **outp);
404 
405 // d2i_X509_AUX parses up to |length| bytes from |*inp| as a DER-encoded X.509
406 // Certificate (RFC 5280), followed optionally by a separate, OpenSSL-specific
407 // structure with auxiliary properties. It behaves as described in |d2i_SAMPLE|.
408 //
409 // Some auxiliary properties affect trust decisions, so this function should not
410 // be used with untrusted input.
411 //
412 // Unlike similarly-named functions, this function does not parse a single
413 // ASN.1 element. Trying to parse data directly embedded in a larger ASN.1
414 // structure will not behave correctly.
415 OPENSSL_EXPORT X509 *d2i_X509_AUX(X509 **x509, const unsigned char **inp,
416                                   long length);
417 
418 // X509_alias_set1 sets |x509|'s alias to |len| bytes from |name|. If |name| is
419 // NULL, the alias is cleared instead. Aliases are not part of the certificate
420 // itself and will not be serialized by |i2d_X509|.
421 OPENSSL_EXPORT int X509_alias_set1(X509 *x509, const unsigned char *name,
422                                    ossl_ssize_t len);
423 
424 // X509_keyid_set1 sets |x509|'s key ID to |len| bytes from |id|. If |id| is
425 // NULL, the key ID is cleared instead. Key IDs are not part of the certificate
426 // itself and will not be serialized by |i2d_X509|.
427 OPENSSL_EXPORT int X509_keyid_set1(X509 *x509, const unsigned char *id,
428                                    ossl_ssize_t len);
429 
430 // X509_alias_get0 looks up |x509|'s alias. If found, it sets |*out_len| to the
431 // alias's length and returns a pointer to a buffer containing the contents. If
432 // not found, it outputs the empty string by returning NULL and setting
433 // |*out_len| to zero.
434 //
435 // If |x509| was parsed from a PKCS#12 structure (see
436 // |PKCS12_get_key_and_certs|), the alias will reflect the friendlyName
437 // attribute (RFC 2985).
438 //
439 // WARNING: In OpenSSL, this function did not set |*out_len| when the alias was
440 // missing. Callers that target both OpenSSL and BoringSSL should set the value
441 // to zero before calling this function.
442 OPENSSL_EXPORT unsigned char *X509_alias_get0(X509 *x509, int *out_len);
443 
444 // X509_keyid_get0 looks up |x509|'s key ID. If found, it sets |*out_len| to the
445 // key ID's length and returns a pointer to a buffer containing the contents. If
446 // not found, it outputs the empty string by returning NULL and setting
447 // |*out_len| to zero.
448 //
449 // WARNING: In OpenSSL, this function did not set |*out_len| when the alias was
450 // missing. Callers that target both OpenSSL and BoringSSL should set the value
451 // to zero before calling this function.
452 OPENSSL_EXPORT unsigned char *X509_keyid_get0(X509 *x509, int *out_len);
453 
454 
455 // Certificate revocation lists.
456 //
457 // An |X509_CRL| object represents an X.509 certificate revocation list (CRL),
458 // defined in RFC 5280. A CRL is a signed list of certificates which are no
459 // longer considered valid.
460 //
461 // Although an |X509_CRL| is a mutable object, mutating an |X509_CRL| can give
462 // incorrect results. Callers typically obtain |X509_CRL|s by parsing some input
463 // with |d2i_X509_CRL|, etc. Such objects carry information such as the
464 // serialized TBSCertList and decoded extensions, which will become inconsistent
465 // when mutated.
466 //
467 // Instead, mutation functions should only be used when issuing new CRLs, as
468 // described in a later section.
469 
470 DEFINE_STACK_OF(X509_CRL)
471 
472 // X509_CRL is an |ASN1_ITEM| whose ASN.1 type is X.509 CertificateList (RFC
473 // 5280) and C type is |X509_CRL*|.
474 DECLARE_ASN1_ITEM(X509_CRL)
475 
476 // X509_CRL_up_ref adds one to the reference count of |crl| and returns one.
477 OPENSSL_EXPORT int X509_CRL_up_ref(X509_CRL *crl);
478 
479 // X509_CRL_dup returns a newly-allocated copy of |crl|, or NULL on error. This
480 // function works by serializing the structure, so if |crl| is incomplete, it
481 // may fail.
482 //
483 // TODO(https://crbug.com/boringssl/407): This function should be const and
484 // thread-safe but is currently neither in some cases, notably if |crl| was
485 // mutated.
486 OPENSSL_EXPORT X509_CRL *X509_CRL_dup(X509_CRL *crl);
487 
488 // X509_CRL_free decrements |crl|'s reference count and, if zero, releases
489 // memory associated with |crl|.
490 OPENSSL_EXPORT void X509_CRL_free(X509_CRL *crl);
491 
492 // d2i_X509_CRL parses up to |len| bytes from |*inp| as a DER-encoded X.509
493 // CertificateList (RFC 5280), as described in |d2i_SAMPLE|.
494 OPENSSL_EXPORT X509_CRL *d2i_X509_CRL(X509_CRL **out, const uint8_t **inp,
495                                       long len);
496 
497 // i2d_X509_CRL marshals |crl| as a X.509 CertificateList (RFC 5280), as
498 // described in |i2d_SAMPLE|.
499 //
500 // TODO(https://crbug.com/boringssl/407): This function should be const and
501 // thread-safe but is currently neither in some cases, notably if |crl| was
502 // mutated.
503 OPENSSL_EXPORT int i2d_X509_CRL(X509_CRL *crl, uint8_t **outp);
504 
505 #define X509_CRL_VERSION_1 0
506 #define X509_CRL_VERSION_2 1
507 
508 // X509_CRL_get_version returns the numerical value of |crl|'s version, which
509 // will be one of the |X509_CRL_VERSION_*| constants.
510 OPENSSL_EXPORT long X509_CRL_get_version(const X509_CRL *crl);
511 
512 // X509_CRL_get0_lastUpdate returns |crl|'s thisUpdate time. The OpenSSL API
513 // refers to this field as lastUpdate.
514 OPENSSL_EXPORT const ASN1_TIME *X509_CRL_get0_lastUpdate(const X509_CRL *crl);
515 
516 // X509_CRL_get0_nextUpdate returns |crl|'s nextUpdate time, or NULL if |crl|
517 // has none.
518 OPENSSL_EXPORT const ASN1_TIME *X509_CRL_get0_nextUpdate(const X509_CRL *crl);
519 
520 // X509_CRL_get_issuer returns |crl|'s issuer name. Note this function is not
521 // const-correct for legacy reasons.
522 OPENSSL_EXPORT X509_NAME *X509_CRL_get_issuer(const X509_CRL *crl);
523 
524 // X509_CRL_get_REVOKED returns the list of revoked certificates in |crl|, or
525 // NULL if |crl| omits it.
526 //
527 // TOOD(davidben): This function was originally a macro, without clear const
528 // semantics. It should take a const input and give const output, but the latter
529 // would break existing callers. For now, we match upstream.
530 OPENSSL_EXPORT STACK_OF(X509_REVOKED) *X509_CRL_get_REVOKED(X509_CRL *crl);
531 
532 // X509_CRL_get0_extensions returns |crl|'s extension list, or NULL if |crl|
533 // omits it.
534 OPENSSL_EXPORT const STACK_OF(X509_EXTENSION) *X509_CRL_get0_extensions(
535     const X509_CRL *crl);
536 
537 // X509_CRL_get_ext_count returns the number of extensions in |x|.
538 OPENSSL_EXPORT int X509_CRL_get_ext_count(const X509_CRL *x);
539 
540 // X509_CRL_get_ext_by_NID behaves like |X509v3_get_ext_by_NID| but searches for
541 // extensions in |x|.
542 OPENSSL_EXPORT int X509_CRL_get_ext_by_NID(const X509_CRL *x, int nid,
543                                            int lastpos);
544 
545 // X509_CRL_get_ext_by_OBJ behaves like |X509v3_get_ext_by_OBJ| but searches for
546 // extensions in |x|.
547 OPENSSL_EXPORT int X509_CRL_get_ext_by_OBJ(const X509_CRL *x,
548                                            const ASN1_OBJECT *obj, int lastpos);
549 
550 // X509_CRL_get_ext_by_critical behaves like |X509v3_get_ext_by_critical| but
551 // searches for extensions in |x|.
552 OPENSSL_EXPORT int X509_CRL_get_ext_by_critical(const X509_CRL *x, int crit,
553                                                 int lastpos);
554 
555 // X509_CRL_get_ext returns the extension in |x| at index |loc|, or NULL if
556 // |loc| is out of bounds. This function returns a non-const pointer for OpenSSL
557 // compatibility, but callers should not mutate the result.
558 OPENSSL_EXPORT X509_EXTENSION *X509_CRL_get_ext(const X509_CRL *x, int loc);
559 
560 // X509_CRL_get0_signature sets |*out_sig| and |*out_alg| to the signature and
561 // signature algorithm of |crl|, respectively. Either output pointer may be NULL
562 // to ignore the value.
563 //
564 // This function outputs the outer signature algorithm, not the one in the
565 // TBSCertList. CRLs with mismatched signature algorithms will successfully
566 // parse, but they will be rejected when verifying.
567 OPENSSL_EXPORT void X509_CRL_get0_signature(const X509_CRL *crl,
568                                             const ASN1_BIT_STRING **out_sig,
569                                             const X509_ALGOR **out_alg);
570 
571 // X509_CRL_get_signature_nid returns the NID corresponding to |crl|'s signature
572 // algorithm, or |NID_undef| if the signature algorithm does not correspond to
573 // a known NID.
574 OPENSSL_EXPORT int X509_CRL_get_signature_nid(const X509_CRL *crl);
575 
576 // i2d_X509_CRL_tbs serializes the TBSCertList portion of |crl|, as described in
577 // |i2d_SAMPLE|.
578 //
579 // This function preserves the original encoding of the TBSCertList and may not
580 // reflect modifications made to |crl|. It may be used to manually verify the
581 // signature of an existing CRL. To generate CRLs, use |i2d_re_X509_CRL_tbs|
582 // instead.
583 OPENSSL_EXPORT int i2d_X509_CRL_tbs(X509_CRL *crl, unsigned char **outp);
584 
585 // X509_CRL_verify checks that |crl| has a valid signature by |pkey|. It returns
586 // one if the signature is valid and zero otherwise.
587 OPENSSL_EXPORT int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *pkey);
588 
589 
590 // Issuing certificate revocation lists.
591 //
592 // An |X509_CRL| object may also represent an incomplete CRL. Callers may
593 // construct empty |X509_CRL| objects, fill in fields individually, and finally
594 // sign the result. The following functions may be used for this purpose.
595 
596 // X509_CRL_new returns a newly-allocated, empty |X509_CRL| object, or NULL on
597 // error. This object may be filled in and then signed to construct a CRL.
598 OPENSSL_EXPORT X509_CRL *X509_CRL_new(void);
599 
600 // X509_CRL_set_version sets |crl|'s version to |version|, which should be one
601 // of the |X509_CRL_VERSION_*| constants. It returns one on success and zero on
602 // error.
603 //
604 // If unsure, use |X509_CRL_VERSION_2|. Note that, unlike certificates, CRL
605 // versions are only defined up to v2. Callers should not use |X509_VERSION_3|.
606 OPENSSL_EXPORT int X509_CRL_set_version(X509_CRL *crl, long version);
607 
608 // X509_CRL_set_issuer_name sets |crl|'s issuer to a copy of |name|. It returns
609 // one on success and zero on error.
610 OPENSSL_EXPORT int X509_CRL_set_issuer_name(X509_CRL *crl, X509_NAME *name);
611 
612 // X509_CRL_set1_lastUpdate sets |crl|'s thisUpdate time to |tm|. It returns one
613 // on success and zero on error. The OpenSSL API refers to this field as
614 // lastUpdate.
615 OPENSSL_EXPORT int X509_CRL_set1_lastUpdate(X509_CRL *crl, const ASN1_TIME *tm);
616 
617 // X509_CRL_set1_nextUpdate sets |crl|'s nextUpdate time to |tm|. It returns one
618 // on success and zero on error.
619 OPENSSL_EXPORT int X509_CRL_set1_nextUpdate(X509_CRL *crl, const ASN1_TIME *tm);
620 
621 // X509_CRL_delete_ext removes the extension in |x| at index |loc| and returns
622 // the removed extension, or NULL if |loc| was out of bounds. If non-NULL, the
623 // caller must release the result with |X509_EXTENSION_free|.
624 OPENSSL_EXPORT X509_EXTENSION *X509_CRL_delete_ext(X509_CRL *x, int loc);
625 
626 // X509_CRL_add_ext adds a copy of |ex| to |x|. It returns one on success and
627 // zero on failure. The caller retains ownership of |ex| and can release it
628 // independently of |x|.
629 //
630 // The new extension is inserted at index |loc|, shifting extensions to the
631 // right. If |loc| is -1 or out of bounds, the new extension is appended to the
632 // list.
633 OPENSSL_EXPORT int X509_CRL_add_ext(X509_CRL *x, const X509_EXTENSION *ex,
634                                     int loc);
635 
636 // X509_CRL_sign signs |crl| with |pkey| and replaces the signature algorithm
637 // and signature fields. It returns one on success and zero on error. This
638 // function uses digest algorithm |md|, or |pkey|'s default if NULL. Other
639 // signing parameters use |pkey|'s defaults. To customize them, use
640 // |X509_CRL_sign_ctx|.
641 OPENSSL_EXPORT int X509_CRL_sign(X509_CRL *crl, EVP_PKEY *pkey,
642                                  const EVP_MD *md);
643 
644 // X509_CRL_sign_ctx signs |crl| with |ctx| and replaces the signature algorithm
645 // and signature fields. It returns one on success and zero on error. The
646 // signature algorithm and parameters come from |ctx|, which must have been
647 // initialized with |EVP_DigestSignInit|. The caller should configure the
648 // corresponding |EVP_PKEY_CTX| before calling this function.
649 OPENSSL_EXPORT int X509_CRL_sign_ctx(X509_CRL *crl, EVP_MD_CTX *ctx);
650 
651 // i2d_re_X509_CRL_tbs serializes the TBSCertList portion of |crl|, as described
652 // in |i2d_SAMPLE|.
653 //
654 // This function re-encodes the TBSCertList and may not reflect |crl|'s original
655 // encoding. It may be used to manually generate a signature for a new CRL. To
656 // verify CRLs, use |i2d_X509_CRL_tbs| instead.
657 OPENSSL_EXPORT int i2d_re_X509_CRL_tbs(X509_CRL *crl, unsigned char **outp);
658 
659 // X509_CRL_set1_signature_algo sets |crl|'s signature algorithm to |algo| and
660 // returns one on success or zero on error. It updates both the signature field
661 // of the TBSCertList structure, and the signatureAlgorithm field of the CRL.
662 OPENSSL_EXPORT int X509_CRL_set1_signature_algo(X509_CRL *crl,
663                                                 const X509_ALGOR *algo);
664 
665 // X509_CRL_set1_signature_value sets |crl|'s signature to a copy of the
666 // |sig_len| bytes pointed by |sig|. It returns one on success and zero on
667 // error.
668 //
669 // Due to a specification error, X.509 CRLs store signatures in ASN.1 BIT
670 // STRINGs, but signature algorithms return byte strings rather than bit
671 // strings. This function creates a BIT STRING containing a whole number of
672 // bytes, with the bit order matching the DER encoding. This matches the
673 // encoding used by all X.509 signature algorithms.
674 OPENSSL_EXPORT int X509_CRL_set1_signature_value(X509_CRL *crl,
675                                                  const uint8_t *sig,
676                                                  size_t sig_len);
677 
678 
679 // Certificate requests.
680 //
681 // An |X509_REQ| represents a PKCS #10 certificate request (RFC 2986). These are
682 // also referred to as certificate signing requests or CSRs. CSRs are a common
683 // format used to request a certificate from a CA.
684 //
685 // Although an |X509_REQ| is a mutable object, mutating an |X509_REQ| can give
686 // incorrect results. Callers typically obtain |X509_REQ|s by parsing some input
687 // with |d2i_X509_REQ|, etc. Such objects carry information such as the
688 // serialized CertificationRequestInfo, which will become inconsistent when
689 // mutated.
690 //
691 // Instead, mutation functions should only be used when issuing new CRLs, as
692 // described in a later section.
693 
694 // X509_REQ is an |ASN1_ITEM| whose ASN.1 type is CertificateRequest (RFC 2986)
695 // and C type is |X509_REQ*|.
696 DECLARE_ASN1_ITEM(X509_REQ)
697 
698 // X509_REQ_dup returns a newly-allocated copy of |req|, or NULL on error. This
699 // function works by serializing the structure, so if |req| is incomplete, it
700 // may fail.
701 //
702 // TODO(https://crbug.com/boringssl/407): This function should be const and
703 // thread-safe but is currently neither in some cases, notably if |req| was
704 // mutated.
705 OPENSSL_EXPORT X509_REQ *X509_REQ_dup(X509_REQ *req);
706 
707 // X509_REQ_free releases memory associated with |req|.
708 OPENSSL_EXPORT void X509_REQ_free(X509_REQ *req);
709 
710 // d2i_X509_REQ parses up to |len| bytes from |*inp| as a DER-encoded
711 // CertificateRequest (RFC 2986), as described in |d2i_SAMPLE|.
712 OPENSSL_EXPORT X509_REQ *d2i_X509_REQ(X509_REQ **out, const uint8_t **inp,
713                                       long len);
714 
715 // i2d_X509_REQ marshals |req| as a CertificateRequest (RFC 2986), as described
716 // in |i2d_SAMPLE|.
717 //
718 // TODO(https://crbug.com/boringssl/407): This function should be const and
719 // thread-safe but is currently neither in some cases, notably if |req| was
720 // mutated.
721 OPENSSL_EXPORT int i2d_X509_REQ(X509_REQ *req, uint8_t **outp);
722 
723 // X509_REQ_VERSION_1 is the version constant for |X509_REQ| objects. No other
724 // versions are defined.
725 #define X509_REQ_VERSION_1 0
726 
727 // X509_REQ_get_version returns the numerical value of |req|'s version. This
728 // will always be |X509_REQ_VERSION_1| for valid CSRs. For compatibility,
729 // |d2i_X509_REQ| also accepts some invalid version numbers, in which case this
730 // function may return other values.
731 OPENSSL_EXPORT long X509_REQ_get_version(const X509_REQ *req);
732 
733 // X509_REQ_get_subject_name returns |req|'s subject name. Note this function is
734 // not const-correct for legacy reasons.
735 OPENSSL_EXPORT X509_NAME *X509_REQ_get_subject_name(const X509_REQ *req);
736 
737 // X509_REQ_get_pubkey returns |req|'s public key as an |EVP_PKEY|, or NULL if
738 // the public key was unsupported or could not be decoded. This function returns
739 // a reference to the |EVP_PKEY|. The caller must release the result with
740 // |EVP_PKEY_free| when done.
741 OPENSSL_EXPORT EVP_PKEY *X509_REQ_get_pubkey(X509_REQ *req);
742 
743 // X509_REQ_get_attr_count returns the number of attributes in |req|.
744 OPENSSL_EXPORT int X509_REQ_get_attr_count(const X509_REQ *req);
745 
746 // X509_REQ_get_attr returns the attribute at index |loc| in |req|, or NULL if
747 // out of bounds.
748 OPENSSL_EXPORT X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc);
749 
750 // X509_REQ_get_attr_by_NID returns the index of the attribute in |req| of type
751 // |nid|, or a negative number if not found. If found, callers can use
752 // |X509_REQ_get_attr| to look up the attribute by index.
753 //
754 // If |lastpos| is non-negative, it begins searching at |lastpos| + 1. Callers
755 // can thus loop over all matching attributes by first passing -1 and then
756 // passing the previously-returned value until no match is returned.
757 OPENSSL_EXPORT int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid,
758                                             int lastpos);
759 
760 // X509_REQ_get_attr_by_OBJ behaves like |X509_REQ_get_attr_by_NID| but looks
761 // for attributes of type |obj|.
762 OPENSSL_EXPORT int X509_REQ_get_attr_by_OBJ(const X509_REQ *req,
763                                             const ASN1_OBJECT *obj,
764                                             int lastpos);
765 
766 // X509_REQ_extension_nid returns one if |nid| is a supported CSR attribute type
767 // for carrying extensions and zero otherwise. The supported types are
768 // |NID_ext_req| (pkcs-9-at-extensionRequest from RFC 2985) and |NID_ms_ext_req|
769 // (a Microsoft szOID_CERT_EXTENSIONS variant).
770 OPENSSL_EXPORT int X509_REQ_extension_nid(int nid);
771 
772 // X509_REQ_get_extensions decodes the list of requested extensions in |req| and
773 // returns a newly-allocated |STACK_OF(X509_EXTENSION)| containing the result.
774 // It returns NULL on error, or if |req| did not request extensions.
775 //
776 // CSRs do not store extensions directly. Instead there are attribute types
777 // which are defined to hold extensions. See |X509_REQ_extension_nid|. This
778 // function supports both pkcs-9-at-extensionRequest from RFC 2985 and the
779 // Microsoft szOID_CERT_EXTENSIONS variant. If both are present,
780 // pkcs-9-at-extensionRequest is preferred.
781 OPENSSL_EXPORT STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(X509_REQ *req);
782 
783 // X509_REQ_get0_signature sets |*out_sig| and |*out_alg| to the signature and
784 // signature algorithm of |req|, respectively. Either output pointer may be NULL
785 // to ignore the value.
786 OPENSSL_EXPORT void X509_REQ_get0_signature(const X509_REQ *req,
787                                             const ASN1_BIT_STRING **out_sig,
788                                             const X509_ALGOR **out_alg);
789 
790 // X509_REQ_get_signature_nid returns the NID corresponding to |req|'s signature
791 // algorithm, or |NID_undef| if the signature algorithm does not correspond to
792 // a known NID.
793 OPENSSL_EXPORT int X509_REQ_get_signature_nid(const X509_REQ *req);
794 
795 // X509_REQ_verify checks that |req| has a valid signature by |pkey|. It returns
796 // one if the signature is valid and zero otherwise.
797 OPENSSL_EXPORT int X509_REQ_verify(X509_REQ *req, EVP_PKEY *pkey);
798 
799 
800 // Issuing certificate requests.
801 //
802 // An |X509_REQ| object may also represent an incomplete CSR. Callers may
803 // construct empty |X509_REQ| objects, fill in fields individually, and finally
804 // sign the result. The following functions may be used for this purpose.
805 
806 // X509_REQ_new returns a newly-allocated, empty |X509_REQ| object, or NULL on
807 // error. This object may be filled in and then signed to construct a CSR.
808 OPENSSL_EXPORT X509_REQ *X509_REQ_new(void);
809 
810 // X509_REQ_set_version sets |req|'s version to |version|, which should be
811 // |X509_REQ_VERSION_1|. It returns one on success and zero on error.
812 //
813 // The only defined CSR version is |X509_REQ_VERSION_1|, so there is no need to
814 // call this function.
815 OPENSSL_EXPORT int X509_REQ_set_version(X509_REQ *req, long version);
816 
817 // X509_REQ_set_subject_name sets |req|'s subject to a copy of |name|. It
818 // returns one on success and zero on error.
819 OPENSSL_EXPORT int X509_REQ_set_subject_name(X509_REQ *req, X509_NAME *name);
820 
821 // X509_REQ_set_pubkey sets |req|'s public key to |pkey|. It returns one on
822 // success and zero on error. This function does not take ownership of |pkey|
823 // and internally copies and updates reference counts as needed.
824 OPENSSL_EXPORT int X509_REQ_set_pubkey(X509_REQ *req, EVP_PKEY *pkey);
825 
826 // X509_REQ_delete_attr removes the attribute at index |loc| in |req|. It
827 // returns the removed attribute to the caller, or NULL if |loc| was out of
828 // bounds. If non-NULL, the caller must release the result with
829 // |X509_ATTRIBUTE_free| when done. It is also safe, but not necessary, to call
830 // |X509_ATTRIBUTE_free| if the result is NULL.
831 OPENSSL_EXPORT X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc);
832 
833 // X509_REQ_add1_attr appends a copy of |attr| to |req|'s list of attributes. It
834 // returns one on success and zero on error.
835 OPENSSL_EXPORT int X509_REQ_add1_attr(X509_REQ *req,
836                                       const X509_ATTRIBUTE *attr);
837 
838 // X509_REQ_add1_attr_by_OBJ appends a new attribute to |req| with type |obj|.
839 // It returns one on success and zero on error. The value is determined by
840 // |X509_ATTRIBUTE_set1_data|.
841 //
842 // WARNING: The interpretation of |attrtype|, |data|, and |len| is complex and
843 // error-prone. See |X509_ATTRIBUTE_set1_data| for details.
844 OPENSSL_EXPORT int X509_REQ_add1_attr_by_OBJ(X509_REQ *req,
845                                              const ASN1_OBJECT *obj,
846                                              int attrtype,
847                                              const unsigned char *data,
848                                              int len);
849 
850 // X509_REQ_add1_attr_by_NID behaves like |X509_REQ_add1_attr_by_OBJ| except the
851 // attribute type is determined by |nid|.
852 OPENSSL_EXPORT int X509_REQ_add1_attr_by_NID(X509_REQ *req, int nid,
853                                              int attrtype,
854                                              const unsigned char *data,
855                                              int len);
856 
857 // X509_REQ_add1_attr_by_txt behaves like |X509_REQ_add1_attr_by_OBJ| except the
858 // attribute type is determined by calling |OBJ_txt2obj| with |attrname|.
859 OPENSSL_EXPORT int X509_REQ_add1_attr_by_txt(X509_REQ *req,
860                                              const char *attrname, int attrtype,
861                                              const unsigned char *data,
862                                              int len);
863 
864 // X509_REQ_add_extensions_nid adds an attribute to |req| of type |nid|, to
865 // request the certificate extensions in |exts|. It returns one on success and
866 // zero on error. |nid| should be |NID_ext_req| or |NID_ms_ext_req|.
867 OPENSSL_EXPORT int X509_REQ_add_extensions_nid(
868     X509_REQ *req, const STACK_OF(X509_EXTENSION) *exts, int nid);
869 
870 // X509_REQ_add_extensions behaves like |X509_REQ_add_extensions_nid|, using the
871 // standard |NID_ext_req| for the attribute type.
872 OPENSSL_EXPORT int X509_REQ_add_extensions(
873     X509_REQ *req, const STACK_OF(X509_EXTENSION) *exts);
874 
875 // X509_REQ_sign signs |req| with |pkey| and replaces the signature algorithm
876 // and signature fields. It returns one on success and zero on error. This
877 // function uses digest algorithm |md|, or |pkey|'s default if NULL. Other
878 // signing parameters use |pkey|'s defaults. To customize them, use
879 // |X509_REQ_sign_ctx|.
880 OPENSSL_EXPORT int X509_REQ_sign(X509_REQ *req, EVP_PKEY *pkey,
881                                  const EVP_MD *md);
882 
883 // X509_REQ_sign_ctx signs |req| with |ctx| and replaces the signature algorithm
884 // and signature fields. It returns one on success and zero on error. The
885 // signature algorithm and parameters come from |ctx|, which must have been
886 // initialized with |EVP_DigestSignInit|. The caller should configure the
887 // corresponding |EVP_PKEY_CTX| before calling this function.
888 OPENSSL_EXPORT int X509_REQ_sign_ctx(X509_REQ *req, EVP_MD_CTX *ctx);
889 
890 // i2d_re_X509_REQ_tbs serializes the CertificationRequestInfo (see RFC 2986)
891 // portion of |req|, as described in |i2d_SAMPLE|.
892 //
893 // This function re-encodes the CertificationRequestInfo and may not reflect
894 // |req|'s original encoding. It may be used to manually generate a signature
895 // for a new certificate request.
896 OPENSSL_EXPORT int i2d_re_X509_REQ_tbs(X509_REQ *req, uint8_t **outp);
897 
898 // X509_REQ_set1_signature_algo sets |req|'s signature algorithm to |algo| and
899 // returns one on success or zero on error.
900 OPENSSL_EXPORT int X509_REQ_set1_signature_algo(X509_REQ *req,
901                                                 const X509_ALGOR *algo);
902 
903 // X509_REQ_set1_signature_value sets |req|'s signature to a copy of the
904 // |sig_len| bytes pointed by |sig|. It returns one on success and zero on
905 // error.
906 //
907 // Due to a specification error, PKCS#10 certificate requests store signatures
908 // in ASN.1 BIT STRINGs, but signature algorithms return byte strings rather
909 // than bit strings. This function creates a BIT STRING containing a whole
910 // number of bytes, with the bit order matching the DER encoding. This matches
911 // the encoding used by all X.509 signature algorithms.
912 OPENSSL_EXPORT int X509_REQ_set1_signature_value(X509_REQ *req,
913                                                  const uint8_t *sig,
914                                                  size_t sig_len);
915 
916 
917 // Names.
918 //
919 // An |X509_NAME| represents an X.509 Name structure (RFC 5280). X.509 names are
920 // a complex, hierarchical structure over a collection of attributes. Each name
921 // is sequence of relative distinguished names (RDNs), decreasing in
922 // specificity. For example, the first RDN may specify the country, while the
923 // next RDN may specify a locality. Each RDN is, itself, a set of attributes.
924 // Having more than one attribute in an RDN is uncommon, but possible. Within an
925 // RDN, attributes have the same level in specificity. Attribute types are
926 // OBJECT IDENTIFIERs. This determines the ASN.1 type of the value, which is
927 // commonly a string but may be other types.
928 //
929 // The |X509_NAME| representation flattens this two-level structure into a
930 // single list of attributes. Each attribute is stored in an |X509_NAME_ENTRY|,
931 // with also maintains the index of the RDN it is part of, accessible via
932 // |X509_NAME_ENTRY_set|. This can be used to recover the two-level structure.
933 //
934 // X.509 names are largely vestigial. Historically, DNS names were parsed out of
935 // the subject's common name attribute, but this is deprecated and has since
936 // moved to the subject alternative name extension. In modern usage, X.509 names
937 // are primarily opaque identifiers to link a certificate with its issuer.
938 
939 DEFINE_STACK_OF(X509_NAME_ENTRY)
940 DEFINE_STACK_OF(X509_NAME)
941 
942 // X509_NAME is an |ASN1_ITEM| whose ASN.1 type is X.509 Name (RFC 5280) and C
943 // type is |X509_NAME*|.
944 DECLARE_ASN1_ITEM(X509_NAME)
945 
946 // X509_NAME_new returns a new, empty |X509_NAME_new|, or NULL on
947 // error.
948 OPENSSL_EXPORT X509_NAME *X509_NAME_new(void);
949 
950 // X509_NAME_free releases memory associated with |name|.
951 OPENSSL_EXPORT void X509_NAME_free(X509_NAME *name);
952 
953 // d2i_X509_NAME parses up to |len| bytes from |*inp| as a DER-encoded X.509
954 // Name (RFC 5280), as described in |d2i_SAMPLE|.
955 OPENSSL_EXPORT X509_NAME *d2i_X509_NAME(X509_NAME **out, const uint8_t **inp,
956                                         long len);
957 
958 // i2d_X509_NAME marshals |in| as a DER-encoded X.509 Name (RFC 5280), as
959 // described in |i2d_SAMPLE|.
960 //
961 // TODO(https://crbug.com/boringssl/407): This function should be const and
962 // thread-safe but is currently neither in some cases, notably if |in| was
963 // mutated.
964 OPENSSL_EXPORT int i2d_X509_NAME(X509_NAME *in, uint8_t **outp);
965 
966 // X509_NAME_dup returns a newly-allocated copy of |name|, or NULL on error.
967 //
968 // TODO(https://crbug.com/boringssl/407): This function should be const and
969 // thread-safe but is currently neither in some cases, notably if |name| was
970 // mutated.
971 OPENSSL_EXPORT X509_NAME *X509_NAME_dup(X509_NAME *name);
972 
973 // X509_NAME_get0_der sets |*out_der| and |*out_der_len|
974 //
975 // Avoid this function and prefer |i2d_X509_NAME|. It is one of the reasons
976 // these functions are not consistently thread-safe or const-correct. Depending
977 // on the resolution of https://crbug.com/boringssl/407, this function may be
978 // removed or cause poor performance.
979 OPENSSL_EXPORT int X509_NAME_get0_der(X509_NAME *name, const uint8_t **out_der,
980                                       size_t *out_der_len);
981 
982 // X509_NAME_set makes a copy of |name|. On success, it frees |*xn|, sets |*xn|
983 // to the copy, and returns one. Otherwise, it returns zero.
984 //
985 // TODO(https://crbug.com/boringssl/407): This function should be const and
986 // thread-safe but is currently neither in some cases, notably if |name| was
987 // mutated.
988 OPENSSL_EXPORT int X509_NAME_set(X509_NAME **xn, X509_NAME *name);
989 
990 // X509_NAME_entry_count returns the number of entries in |name|.
991 OPENSSL_EXPORT int X509_NAME_entry_count(const X509_NAME *name);
992 
993 // X509_NAME_get_index_by_NID returns the zero-based index of the first
994 // attribute in |name| with type |nid|, or -1 if there is none. |nid| should be
995 // one of the |NID_*| constants. If |lastpos| is non-negative, it begins
996 // searching at |lastpos+1|. To search all attributes, pass in -1, not zero.
997 //
998 // Indices from this function refer to |X509_NAME|'s flattened representation.
999 OPENSSL_EXPORT int X509_NAME_get_index_by_NID(const X509_NAME *name, int nid,
1000                                               int lastpos);
1001 
1002 // X509_NAME_get_index_by_OBJ behaves like |X509_NAME_get_index_by_NID| but
1003 // looks for attributes with type |obj|.
1004 OPENSSL_EXPORT int X509_NAME_get_index_by_OBJ(const X509_NAME *name,
1005                                               const ASN1_OBJECT *obj,
1006                                               int lastpos);
1007 
1008 // X509_NAME_get_entry returns the attribute in |name| at index |loc|, or NULL
1009 // if |loc| is out of range. |loc| is interpreted using |X509_NAME|'s flattened
1010 // representation. This function returns a non-const pointer for OpenSSL
1011 // compatibility, but callers should not mutate the result. Doing so will break
1012 // internal invariants in the library.
1013 OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_get_entry(const X509_NAME *name,
1014                                                     int loc);
1015 
1016 // X509_NAME_delete_entry removes and returns the attribute in |name| at index
1017 // |loc|, or NULL if |loc| is out of range. |loc| is interpreted using
1018 // |X509_NAME|'s flattened representation. If the attribute is found, the caller
1019 // is responsible for releasing the result with |X509_NAME_ENTRY_free|.
1020 //
1021 // This function will internally update RDN indices (see |X509_NAME_ENTRY_set|)
1022 // so they continue to be consecutive.
1023 OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name,
1024                                                        int loc);
1025 
1026 // X509_NAME_add_entry adds a copy of |entry| to |name| and returns one on
1027 // success or zero on error. If |loc| is -1, the entry is appended to |name|.
1028 // Otherwise, it is inserted at index |loc|. If |set| is -1, the entry is added
1029 // to the previous entry's RDN. If it is 0, the entry becomes a singleton RDN.
1030 // If 1, it is added to next entry's RDN.
1031 //
1032 // This function will internally update RDN indices (see |X509_NAME_ENTRY_set|)
1033 // so they continue to be consecutive.
1034 OPENSSL_EXPORT int X509_NAME_add_entry(X509_NAME *name,
1035                                        const X509_NAME_ENTRY *entry, int loc,
1036                                        int set);
1037 
1038 // X509_NAME_add_entry_by_OBJ adds a new entry to |name| and returns one on
1039 // success or zero on error. The entry's attribute type is |obj|. The entry's
1040 // attribute value is determined by |type|, |bytes|, and |len|, as in
1041 // |X509_NAME_ENTRY_set_data|. The entry's position is determined by |loc| and
1042 // |set| as in |X509_NAME_add_entry|.
1043 OPENSSL_EXPORT int X509_NAME_add_entry_by_OBJ(X509_NAME *name,
1044                                               const ASN1_OBJECT *obj, int type,
1045                                               const uint8_t *bytes,
1046                                               ossl_ssize_t len, int loc,
1047                                               int set);
1048 
1049 // X509_NAME_add_entry_by_NID behaves like |X509_NAME_add_entry_by_OBJ| but sets
1050 // the entry's attribute type to |nid|, which should be one of the |NID_*|
1051 // constants.
1052 OPENSSL_EXPORT int X509_NAME_add_entry_by_NID(X509_NAME *name, int nid,
1053                                               int type, const uint8_t *bytes,
1054                                               ossl_ssize_t len, int loc,
1055                                               int set);
1056 
1057 // X509_NAME_add_entry_by_txt behaves like |X509_NAME_add_entry_by_OBJ| but sets
1058 // the entry's attribute type to |field|, which is passed to |OBJ_txt2obj|.
1059 OPENSSL_EXPORT int X509_NAME_add_entry_by_txt(X509_NAME *name,
1060                                               const char *field, int type,
1061                                               const uint8_t *bytes,
1062                                               ossl_ssize_t len, int loc,
1063                                               int set);
1064 
1065 // X509_NAME_ENTRY is an |ASN1_ITEM| whose ASN.1 type is AttributeTypeAndValue
1066 // (RFC 5280) and C type is |X509_NAME_ENTRY*|.
1067 DECLARE_ASN1_ITEM(X509_NAME_ENTRY)
1068 
1069 // X509_NAME_ENTRY_new returns a new, empty |X509_NAME_ENTRY_new|, or NULL on
1070 // error.
1071 OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_new(void);
1072 
1073 // X509_NAME_ENTRY_free releases memory associated with |entry|.
1074 OPENSSL_EXPORT void X509_NAME_ENTRY_free(X509_NAME_ENTRY *entry);
1075 
1076 // d2i_X509_NAME_ENTRY parses up to |len| bytes from |*inp| as a DER-encoded
1077 // AttributeTypeAndValue (RFC 5280), as described in |d2i_SAMPLE|.
1078 OPENSSL_EXPORT X509_NAME_ENTRY *d2i_X509_NAME_ENTRY(X509_NAME_ENTRY **out,
1079                                                     const uint8_t **inp,
1080                                                     long len);
1081 
1082 // i2d_X509_NAME_ENTRY marshals |in| as a DER-encoded AttributeTypeAndValue (RFC
1083 // 5280), as described in |i2d_SAMPLE|.
1084 OPENSSL_EXPORT int i2d_X509_NAME_ENTRY(const X509_NAME_ENTRY *in,
1085                                        uint8_t **outp);
1086 
1087 // X509_NAME_ENTRY_dup returns a newly-allocated copy of |entry|, or NULL on
1088 // error.
1089 OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_dup(
1090     const X509_NAME_ENTRY *entry);
1091 
1092 // X509_NAME_ENTRY_get_object returns |entry|'s attribute type. This function
1093 // returns a non-const pointer for OpenSSL compatibility, but callers should not
1094 // mutate the result. Doing so will break internal invariants in the library.
1095 OPENSSL_EXPORT ASN1_OBJECT *X509_NAME_ENTRY_get_object(
1096     const X509_NAME_ENTRY *entry);
1097 
1098 // X509_NAME_ENTRY_set_object sets |entry|'s attribute type to |obj|. It returns
1099 // one on success and zero on error.
1100 OPENSSL_EXPORT int X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *entry,
1101                                               const ASN1_OBJECT *obj);
1102 
1103 // X509_NAME_ENTRY_get_data returns |entry|'s attribute value, represented as an
1104 // |ASN1_STRING|. This value may have any ASN.1 type, so callers must check the
1105 // type before interpreting the contents. This function returns a non-const
1106 // pointer for OpenSSL compatibility, but callers should not mutate the result.
1107 // Doing so will break internal invariants in the library.
1108 //
1109 // TODO(https://crbug.com/boringssl/412): Although the spec says any ASN.1 type
1110 // is allowed, we currently only allow an ad-hoc set of types. Additionally, it
1111 // is unclear if some types can even be represented by this function.
1112 OPENSSL_EXPORT ASN1_STRING *X509_NAME_ENTRY_get_data(
1113     const X509_NAME_ENTRY *entry);
1114 
1115 // X509_NAME_ENTRY_set_data sets |entry|'s value to |len| bytes from |bytes|. It
1116 // returns one on success and zero on error. If |len| is -1, |bytes| must be a
1117 // NUL-terminated C string and the length is determined by |strlen|. |bytes| is
1118 // converted to an ASN.1 type as follows:
1119 //
1120 // If |type| is a |MBSTRING_*| constant, the value is an ASN.1 string. The
1121 // string is determined by decoding |bytes| in the encoding specified by |type|,
1122 // and then re-encoding it in a form appropriate for |entry|'s attribute type.
1123 // See |ASN1_STRING_set_by_NID| for details.
1124 //
1125 // Otherwise, the value is an |ASN1_STRING| with type |type| and value |bytes|.
1126 // See |ASN1_STRING| for how to format ASN.1 types as an |ASN1_STRING|. If
1127 // |type| is |V_ASN1_UNDEF| the previous |ASN1_STRING| type is reused.
1128 OPENSSL_EXPORT int X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *entry, int type,
1129                                             const uint8_t *bytes,
1130                                             ossl_ssize_t len);
1131 
1132 // X509_NAME_ENTRY_set returns the zero-based index of the RDN which contains
1133 // |entry|. Consecutive entries with the same index are part of the same RDN.
1134 OPENSSL_EXPORT int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *entry);
1135 
1136 // X509_NAME_ENTRY_create_by_OBJ creates a new |X509_NAME_ENTRY| with attribute
1137 // type |obj|. The attribute value is determined from |type|, |bytes|, and |len|
1138 // as in |X509_NAME_ENTRY_set_data|. It returns the |X509_NAME_ENTRY| on success
1139 // and NULL on error.
1140 //
1141 // If |out| is non-NULL and |*out| is NULL, it additionally sets |*out| to the
1142 // result on success. If both |out| and |*out| are non-NULL, it updates the
1143 // object at |*out| instead of allocating a new one.
1144 OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(
1145     X509_NAME_ENTRY **out, const ASN1_OBJECT *obj, int type,
1146     const uint8_t *bytes, ossl_ssize_t len);
1147 
1148 // X509_NAME_ENTRY_create_by_NID behaves like |X509_NAME_ENTRY_create_by_OBJ|
1149 // except the attribute type is |nid|, which should be one of the |NID_*|
1150 // constants.
1151 OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(
1152     X509_NAME_ENTRY **out, int nid, int type, const uint8_t *bytes,
1153     ossl_ssize_t len);
1154 
1155 // X509_NAME_ENTRY_create_by_txt behaves like |X509_NAME_ENTRY_create_by_OBJ|
1156 // except the attribute type is |field|, which is passed to |OBJ_txt2obj|.
1157 OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_txt(
1158     X509_NAME_ENTRY **out, const char *field, int type, const uint8_t *bytes,
1159     ossl_ssize_t len);
1160 
1161 
1162 // Extensions.
1163 //
1164 // X.509 certificates and CRLs may contain a list of extensions (RFC 5280).
1165 // Extensions have a type, specified by an object identifier (|ASN1_OBJECT|) and
1166 // a byte string value, which should a DER-encoded structure whose type is
1167 // determined by the extension type. This library represents extensions with the
1168 // |X509_EXTENSION| type.
1169 
1170 // X509_EXTENSION is an |ASN1_ITEM| whose ASN.1 type is X.509 Extension (RFC
1171 // 5280) and C type is |X509_EXTENSION*|.
1172 DECLARE_ASN1_ITEM(X509_EXTENSION)
1173 
1174 // X509_EXTENSION_new returns a newly-allocated, empty |X509_EXTENSION| object
1175 // or NULL on error.
1176 OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_new(void);
1177 
1178 // X509_EXTENSION_free releases memory associated with |ex|.
1179 OPENSSL_EXPORT void X509_EXTENSION_free(X509_EXTENSION *ex);
1180 
1181 // d2i_X509_EXTENSION parses up to |len| bytes from |*inp| as a DER-encoded
1182 // X.509 Extension (RFC 5280), as described in |d2i_SAMPLE|.
1183 OPENSSL_EXPORT X509_EXTENSION *d2i_X509_EXTENSION(X509_EXTENSION **out,
1184                                                   const uint8_t **inp,
1185                                                   long len);
1186 
1187 // i2d_X509_EXTENSION marshals |ex| as a DER-encoded X.509 Extension (RFC
1188 // 5280), as described in |i2d_SAMPLE|.
1189 OPENSSL_EXPORT int i2d_X509_EXTENSION(const X509_EXTENSION *ex, uint8_t **outp);
1190 
1191 // X509_EXTENSION_dup returns a newly-allocated copy of |ex|, or NULL on error.
1192 // This function works by serializing the structure, so if |ex| is incomplete,
1193 // it may fail.
1194 OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_dup(const X509_EXTENSION *ex);
1195 
1196 // X509_EXTENSION_create_by_NID creates a new |X509_EXTENSION| with type |nid|,
1197 // value |data|, and critical bit |crit|. It returns an |X509_EXTENSION| on
1198 // success, and NULL on error. |nid| should be a |NID_*| constant.
1199 //
1200 // If |ex| and |*ex| are both non-NULL, |*ex| is used to hold the result,
1201 // otherwise a new object is allocated. If |ex| is non-NULL and |*ex| is NULL,
1202 // the function sets |*ex| to point to the newly allocated result, in addition
1203 // to returning the result.
1204 OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_create_by_NID(
1205     X509_EXTENSION **ex, int nid, int crit, const ASN1_OCTET_STRING *data);
1206 
1207 // X509_EXTENSION_create_by_OBJ behaves like |X509_EXTENSION_create_by_NID|, but
1208 // the extension type is determined by an |ASN1_OBJECT|.
1209 OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_create_by_OBJ(
1210     X509_EXTENSION **ex, const ASN1_OBJECT *obj, int crit,
1211     const ASN1_OCTET_STRING *data);
1212 
1213 // X509_EXTENSION_get_object returns |ex|'s extension type. This function
1214 // returns a non-const pointer for OpenSSL compatibility, but callers should not
1215 // mutate the result.
1216 OPENSSL_EXPORT ASN1_OBJECT *X509_EXTENSION_get_object(const X509_EXTENSION *ex);
1217 
1218 // X509_EXTENSION_get_data returns |ne|'s extension value. This function returns
1219 // a non-const pointer for OpenSSL compatibility, but callers should not mutate
1220 // the result.
1221 OPENSSL_EXPORT ASN1_OCTET_STRING *X509_EXTENSION_get_data(
1222     const X509_EXTENSION *ne);
1223 
1224 // X509_EXTENSION_get_critical returns one if |ex| is critical and zero
1225 // otherwise.
1226 OPENSSL_EXPORT int X509_EXTENSION_get_critical(const X509_EXTENSION *ex);
1227 
1228 // X509_EXTENSION_set_object sets |ex|'s extension type to |obj|. It returns one
1229 // on success and zero on error.
1230 OPENSSL_EXPORT int X509_EXTENSION_set_object(X509_EXTENSION *ex,
1231                                              const ASN1_OBJECT *obj);
1232 
1233 // X509_EXTENSION_set_critical sets |ex| to critical if |crit| is non-zero and
1234 // to non-critical if |crit| is zero.
1235 OPENSSL_EXPORT int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit);
1236 
1237 // X509_EXTENSION_set_data set's |ex|'s extension value to a copy of |data|. It
1238 // returns one on success and zero on error.
1239 OPENSSL_EXPORT int X509_EXTENSION_set_data(X509_EXTENSION *ex,
1240                                            const ASN1_OCTET_STRING *data);
1241 
1242 
1243 // Extension lists.
1244 //
1245 // The following functions manipulate lists of extensions. Most of them have
1246 // corresponding functions on the containing |X509|, |X509_CRL|, or
1247 // |X509_REVOKED|.
1248 
1249 DEFINE_STACK_OF(X509_EXTENSION)
1250 typedef STACK_OF(X509_EXTENSION) X509_EXTENSIONS;
1251 
1252 // X509_EXTENSIONS is an |ASN1_ITEM| whose ASN.1 type is SEQUENCE of Extension
1253 // (RFC 5280) and C type is |STACK_OF(X509_EXTENSION)*|.
1254 DECLARE_ASN1_ITEM(X509_EXTENSIONS)
1255 
1256 // d2i_X509_EXTENSIONS parses up to |len| bytes from |*inp| as a DER-encoded
1257 // SEQUENCE OF Extension (RFC 5280), as described in |d2i_SAMPLE|.
1258 OPENSSL_EXPORT X509_EXTENSIONS *d2i_X509_EXTENSIONS(X509_EXTENSIONS **out,
1259                                                     const uint8_t **inp,
1260                                                     long len);
1261 
1262 // i2d_X509_EXTENSIONS marshals |alg| as a DER-encoded SEQUENCE OF Extension
1263 // (RFC 5280), as described in |i2d_SAMPLE|.
1264 OPENSSL_EXPORT int i2d_X509_EXTENSIONS(const X509_EXTENSIONS *alg,
1265                                        uint8_t **outp);
1266 
1267 // X509v3_get_ext_count returns the number of extensions in |x|.
1268 OPENSSL_EXPORT int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x);
1269 
1270 // X509v3_get_ext_by_NID returns the index of the first extension in |x| with
1271 // type |nid|, or a negative number if not found. If found, callers can use
1272 // |X509v3_get_ext| to look up the extension by index.
1273 //
1274 // If |lastpos| is non-negative, it begins searching at |lastpos| + 1. Callers
1275 // can thus loop over all matching extensions by first passing -1 and then
1276 // passing the previously-returned value until no match is returned.
1277 OPENSSL_EXPORT int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x,
1278                                          int nid, int lastpos);
1279 
1280 // X509v3_get_ext_by_OBJ behaves like |X509v3_get_ext_by_NID| but looks for
1281 // extensions matching |obj|.
1282 OPENSSL_EXPORT int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *x,
1283                                          const ASN1_OBJECT *obj, int lastpos);
1284 
1285 // X509v3_get_ext_by_critical returns the index of the first extension in |x|
1286 // whose critical bit matches |crit|, or a negative number if no such extension
1287 // was found.
1288 //
1289 // If |lastpos| is non-negative, it begins searching at |lastpos| + 1. Callers
1290 // can thus loop over all matching extensions by first passing -1 and then
1291 // passing the previously-returned value until no match is returned.
1292 OPENSSL_EXPORT int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *x,
1293                                               int crit, int lastpos);
1294 
1295 // X509v3_get_ext returns the extension in |x| at index |loc|, or NULL if |loc|
1296 // is out of bounds. This function returns a non-const pointer for OpenSSL
1297 // compatibility, but callers should not mutate the result.
1298 OPENSSL_EXPORT X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x,
1299                                               int loc);
1300 
1301 // X509v3_delete_ext removes the extension in |x| at index |loc| and returns the
1302 // removed extension, or NULL if |loc| was out of bounds. If an extension was
1303 // returned, the caller must release it with |X509_EXTENSION_free|.
1304 OPENSSL_EXPORT X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x,
1305                                                  int loc);
1306 
1307 // X509v3_add_ext adds a copy of |ex| to the extension list in |*x|. If |*x| is
1308 // NULL, it allocates a new |STACK_OF(X509_EXTENSION)| to hold the copy and sets
1309 // |*x| to the new list. It returns |*x| on success and NULL on error. The
1310 // caller retains ownership of |ex| and can release it independently of |*x|.
1311 //
1312 // The new extension is inserted at index |loc|, shifting extensions to the
1313 // right. If |loc| is -1 or out of bounds, the new extension is appended to the
1314 // list.
1315 OPENSSL_EXPORT STACK_OF(X509_EXTENSION) *X509v3_add_ext(
1316     STACK_OF(X509_EXTENSION) **x, const X509_EXTENSION *ex, int loc);
1317 
1318 
1319 // Algorithm identifiers.
1320 //
1321 // An |X509_ALGOR| represents an AlgorithmIdentifier structure, used in X.509
1322 // to represent signature algorithms and public key algorithms.
1323 
1324 DEFINE_STACK_OF(X509_ALGOR)
1325 
1326 // X509_ALGOR is an |ASN1_ITEM| whose ASN.1 type is AlgorithmIdentifier and C
1327 // type is |X509_ALGOR*|.
1328 DECLARE_ASN1_ITEM(X509_ALGOR)
1329 
1330 // X509_ALGOR_new returns a newly-allocated, empty |X509_ALGOR| object, or NULL
1331 // on error.
1332 OPENSSL_EXPORT X509_ALGOR *X509_ALGOR_new(void);
1333 
1334 // X509_ALGOR_dup returns a newly-allocated copy of |alg|, or NULL on error.
1335 // This function works by serializing the structure, so if |alg| is incomplete,
1336 // it may fail.
1337 OPENSSL_EXPORT X509_ALGOR *X509_ALGOR_dup(const X509_ALGOR *alg);
1338 
1339 // X509_ALGOR_free releases memory associated with |alg|.
1340 OPENSSL_EXPORT void X509_ALGOR_free(X509_ALGOR *alg);
1341 
1342 // d2i_X509_ALGOR parses up to |len| bytes from |*inp| as a DER-encoded
1343 // AlgorithmIdentifier, as described in |d2i_SAMPLE|.
1344 OPENSSL_EXPORT X509_ALGOR *d2i_X509_ALGOR(X509_ALGOR **out, const uint8_t **inp,
1345                                           long len);
1346 
1347 // i2d_X509_ALGOR marshals |alg| as a DER-encoded AlgorithmIdentifier, as
1348 // described in |i2d_SAMPLE|.
1349 OPENSSL_EXPORT int i2d_X509_ALGOR(const X509_ALGOR *alg, uint8_t **outp);
1350 
1351 // X509_ALGOR_set0 sets |alg| to an AlgorithmIdentifier with algorithm |obj| and
1352 // parameter determined by |param_type| and |param_value|. It returns one on
1353 // success and zero on error. This function takes ownership of |obj| and
1354 // |param_value| on success.
1355 //
1356 // If |param_type| is |V_ASN1_UNDEF|, the parameter is omitted. If |param_type|
1357 // is zero, the parameter is left unchanged. Otherwise, |param_type| and
1358 // |param_value| are interpreted as in |ASN1_TYPE_set|.
1359 //
1360 // Note omitting the parameter (|V_ASN1_UNDEF|) and encoding an explicit NULL
1361 // value (|V_ASN1_NULL|) are different. Some algorithms require one and some the
1362 // other. Consult the relevant specification before calling this function. The
1363 // correct parameter for an RSASSA-PKCS1-v1_5 signature is |V_ASN1_NULL|. The
1364 // correct one for an ECDSA or Ed25519 signature is |V_ASN1_UNDEF|.
1365 OPENSSL_EXPORT int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *obj,
1366                                    int param_type, void *param_value);
1367 
1368 // X509_ALGOR_get0 sets |*out_obj| to the |alg|'s algorithm. If |alg|'s
1369 // parameter is omitted, it sets |*out_param_type| and |*out_param_value| to
1370 // |V_ASN1_UNDEF| and NULL. Otherwise, it sets |*out_param_type| and
1371 // |*out_param_value| to the parameter, using the same representation as
1372 // |ASN1_TYPE_set0|. See |ASN1_TYPE_set0| and |ASN1_TYPE| for details.
1373 //
1374 // Callers that require the parameter in serialized form should, after checking
1375 // for |V_ASN1_UNDEF|, use |ASN1_TYPE_set1| and |d2i_ASN1_TYPE|, rather than
1376 // inspecting |*out_param_value|.
1377 //
1378 // Each of |out_obj|, |out_param_type|, and |out_param_value| may be NULL to
1379 // ignore the output. If |out_param_type| is NULL, |out_param_value| is ignored.
1380 //
1381 // WARNING: If |*out_param_type| is set to |V_ASN1_UNDEF|, OpenSSL and older
1382 // revisions of BoringSSL leave |*out_param_value| unset rather than setting it
1383 // to NULL. Callers that support both OpenSSL and BoringSSL should not assume
1384 // |*out_param_value| is uniformly initialized.
1385 OPENSSL_EXPORT void X509_ALGOR_get0(const ASN1_OBJECT **out_obj,
1386                                     int *out_param_type,
1387                                     const void **out_param_value,
1388                                     const X509_ALGOR *alg);
1389 
1390 // X509_ALGOR_set_md sets |alg| to the hash function |md|. Note this
1391 // AlgorithmIdentifier represents the hash function itself, not a signature
1392 // algorithm that uses |md|.
1393 OPENSSL_EXPORT void X509_ALGOR_set_md(X509_ALGOR *alg, const EVP_MD *md);
1394 
1395 // X509_ALGOR_cmp returns zero if |a| and |b| are equal, and some non-zero value
1396 // otherwise. Note this function can only be used for equality checks, not an
1397 // ordering.
1398 OPENSSL_EXPORT int X509_ALGOR_cmp(const X509_ALGOR *a, const X509_ALGOR *b);
1399 
1400 
1401 // Attributes.
1402 //
1403 // Unlike certificates and CRLs, CSRs use a separate Attribute structure (RFC
1404 // 2985, RFC 2986) for extensibility. This is represented by the library as
1405 // |X509_ATTRIBUTE|.
1406 
1407 DEFINE_STACK_OF(X509_ATTRIBUTE)
1408 
1409 // X509_ATTRIBUTE is an |ASN1_ITEM| whose ASN.1 type is Attribute (RFC 2986) and
1410 // C type is |X509_ATTRIBUTE*|.
1411 DECLARE_ASN1_ITEM(X509_ATTRIBUTE)
1412 
1413 // X509_ATTRIBUTE_new returns a newly-allocated, empty |X509_ATTRIBUTE| object,
1414 // or NULL on error. |X509_ATTRIBUTE_set1_*| may be used to finish initializing
1415 // it.
1416 OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_new(void);
1417 
1418 // X509_ATTRIBUTE_dup returns a newly-allocated copy of |attr|, or NULL on
1419 // error. This function works by serializing the structure, so if |attr| is
1420 // incomplete, it may fail.
1421 OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_dup(const X509_ATTRIBUTE *attr);
1422 
1423 // X509_ATTRIBUTE_free releases memory associated with |attr|.
1424 OPENSSL_EXPORT void X509_ATTRIBUTE_free(X509_ATTRIBUTE *attr);
1425 
1426 // d2i_X509_ATTRIBUTE parses up to |len| bytes from |*inp| as a DER-encoded
1427 // Attribute (RFC 2986), as described in |d2i_SAMPLE|.
1428 OPENSSL_EXPORT X509_ATTRIBUTE *d2i_X509_ATTRIBUTE(X509_ATTRIBUTE **out,
1429                                                   const uint8_t **inp,
1430                                                   long len);
1431 
1432 // i2d_X509_ATTRIBUTE marshals |alg| as a DER-encoded Attribute (RFC 2986), as
1433 // described in |i2d_SAMPLE|.
1434 OPENSSL_EXPORT int i2d_X509_ATTRIBUTE(const X509_ATTRIBUTE *alg,
1435                                       uint8_t **outp);
1436 
1437 // X509_ATTRIBUTE_create returns a newly-allocated |X509_ATTRIBUTE|, or NULL on
1438 // error. The attribute has type |nid| and contains a single value determined by
1439 // |attrtype| and |value|, which are interpreted as in |ASN1_TYPE_set|. Note
1440 // this function takes ownership of |value|.
1441 OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int attrtype,
1442                                                      void *value);
1443 
1444 // X509_ATTRIBUTE_create_by_NID returns a newly-allocated |X509_ATTRIBUTE| of
1445 // type |nid|, or NULL on error. The value is determined as in
1446 // |X509_ATTRIBUTE_set1_data|.
1447 //
1448 // If |attr| is non-NULL, the resulting |X509_ATTRIBUTE| is also written to
1449 // |*attr|. If |*attr| was non-NULL when the function was called, |*attr| is
1450 // reused instead of creating a new object.
1451 //
1452 // WARNING: The interpretation of |attrtype|, |data|, and |len| is complex and
1453 // error-prone. See |X509_ATTRIBUTE_set1_data| for details.
1454 //
1455 // WARNING: The object reuse form is deprecated and may be removed in the
1456 // future. It also currently incorrectly appends to the reused object's value
1457 // set rather than overwriting it.
1458 OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(
1459     X509_ATTRIBUTE **attr, int nid, int attrtype, const void *data, int len);
1460 
1461 // X509_ATTRIBUTE_create_by_OBJ behaves like |X509_ATTRIBUTE_create_by_NID|
1462 // except the attribute's type is determined by |obj|.
1463 OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(
1464     X509_ATTRIBUTE **attr, const ASN1_OBJECT *obj, int attrtype,
1465     const void *data, int len);
1466 
1467 // X509_ATTRIBUTE_create_by_txt behaves like |X509_ATTRIBUTE_create_by_NID|
1468 // except the attribute's type is determined by calling |OBJ_txt2obj| with
1469 // |attrname|.
1470 OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(
1471     X509_ATTRIBUTE **attr, const char *attrname, int type,
1472     const unsigned char *bytes, int len);
1473 
1474 // X509_ATTRIBUTE_set1_object sets |attr|'s type to |obj|. It returns one on
1475 // success and zero on error.
1476 OPENSSL_EXPORT int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr,
1477                                               const ASN1_OBJECT *obj);
1478 
1479 // X509_ATTRIBUTE_set1_data appends a value to |attr|'s value set and returns
1480 // one on success or zero on error. The value is determined as follows:
1481 //
1482 // If |attrtype| is a |MBSTRING_*| constant, the value is an ASN.1 string. The
1483 // string is determined by decoding |len| bytes from |data| in the encoding
1484 // specified by |attrtype|, and then re-encoding it in a form appropriate for
1485 // |attr|'s type. If |len| is -1, |strlen(data)| is used instead. See
1486 // |ASN1_STRING_set_by_NID| for details.
1487 //
1488 // Otherwise, if |len| is not -1, the value is an ASN.1 string. |attrtype| is an
1489 // |ASN1_STRING| type value and the |len| bytes from |data| are copied as the
1490 // type-specific representation of |ASN1_STRING|. See |ASN1_STRING| for details.
1491 //
1492 // WARNING: If this form is used to construct a negative INTEGER or ENUMERATED,
1493 // |attrtype| includes the |V_ASN1_NEG| flag for |ASN1_STRING|, but the function
1494 // forgets to clear the flag for |ASN1_TYPE|. This matches OpenSSL but is
1495 // probably a bug. For now, do not use this form with negative values.
1496 //
1497 // Otherwise, if |len| is -1, the value is constructed by passing |attrtype| and
1498 // |data| to |ASN1_TYPE_set1|. That is, |attrtype| is an |ASN1_TYPE| type value,
1499 // and |data| is cast to the corresponding pointer type.
1500 //
1501 // WARNING: Despite the name, this function appends to |attr|'s value set,
1502 // rather than overwriting it. To overwrite the value set, create a new
1503 // |X509_ATTRIBUTE| with |X509_ATTRIBUTE_new|.
1504 //
1505 // WARNING: If using the |MBSTRING_*| form, pass a length rather than relying on
1506 // |strlen|. In particular, |strlen| will not behave correctly if the input is
1507 // |MBSTRING_BMP| or |MBSTRING_UNIV|.
1508 //
1509 // WARNING: This function currently misinterprets |V_ASN1_OTHER| as an
1510 // |MBSTRING_*| constant. This matches OpenSSL but means it is impossible to
1511 // construct a value with a non-universal tag.
1512 OPENSSL_EXPORT int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,
1513                                             const void *data, int len);
1514 
1515 // X509_ATTRIBUTE_get0_data returns the |idx|th value of |attr| in a
1516 // type-specific representation to |attrtype|, or NULL if out of bounds or the
1517 // type does not match. |attrtype| is one of the type values in |ASN1_TYPE|. On
1518 // match, the return value uses the same representation as |ASN1_TYPE_set0|. See
1519 // |ASN1_TYPE| for details.
1520 OPENSSL_EXPORT void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx,
1521                                               int attrtype, void *unused);
1522 
1523 // X509_ATTRIBUTE_count returns the number of values in |attr|.
1524 OPENSSL_EXPORT int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr);
1525 
1526 // X509_ATTRIBUTE_get0_object returns the type of |attr|.
1527 OPENSSL_EXPORT ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr);
1528 
1529 // X509_ATTRIBUTE_get0_type returns the |idx|th value in |attr|, or NULL if out
1530 // of bounds. Note this function returns one of |attr|'s values, not the type.
1531 OPENSSL_EXPORT ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr,
1532                                                    int idx);
1533 
1534 
1535 // Printing functions.
1536 //
1537 // The following functions output human-readable representations of
1538 // X.509-related structures. They should only be used for debugging or logging
1539 // and not parsed programmatically. In many cases, the outputs are ambiguous, so
1540 // attempting to parse them can lead to string injection vulnerabilities.
1541 
1542 // The following flags control |X509_print_ex| and |X509_REQ_print_ex|.
1543 
1544 // X509_FLAG_COMPAT disables all flags. It additionally causes names to be
1545 // printed with a 16-byte indent.
1546 #define X509_FLAG_COMPAT 0
1547 
1548 // X509_FLAG_NO_HEADER skips a header identifying the type of object printed.
1549 #define X509_FLAG_NO_HEADER 1L
1550 
1551 // X509_FLAG_NO_VERSION skips printing the X.509 version number.
1552 #define X509_FLAG_NO_VERSION (1L << 1)
1553 
1554 // X509_FLAG_NO_SERIAL skips printing the serial number. It is ignored in
1555 // |X509_REQ_print_fp|.
1556 #define X509_FLAG_NO_SERIAL (1L << 2)
1557 
1558 // X509_FLAG_NO_SIGNAME skips printing the signature algorithm in the
1559 // TBSCertificate. It is ignored in |X509_REQ_print_fp|.
1560 #define X509_FLAG_NO_SIGNAME (1L << 3)
1561 
1562 // X509_FLAG_NO_ISSUER skips printing the issuer.
1563 #define X509_FLAG_NO_ISSUER (1L << 4)
1564 
1565 // X509_FLAG_NO_ISSUER skips printing the notBefore and notAfter times. It is
1566 // ignored in |X509_REQ_print_fp|.
1567 #define X509_FLAG_NO_VALIDITY (1L << 5)
1568 
1569 // X509_FLAG_NO_ISSUER skips printing the subject.
1570 #define X509_FLAG_NO_SUBJECT (1L << 6)
1571 
1572 // X509_FLAG_NO_PUBKEY skips printing the public key.
1573 #define X509_FLAG_NO_PUBKEY (1L << 7)
1574 
1575 // X509_FLAG_NO_EXTENSIONS skips printing the extension list. It is ignored in
1576 // |X509_REQ_print_fp|. CSRs instead have attributes, which is controlled by
1577 // |X509_FLAG_NO_ATTRIBUTES|.
1578 #define X509_FLAG_NO_EXTENSIONS (1L << 8)
1579 
1580 // X509_FLAG_NO_SIGDUMP skips printing the signature and outer signature
1581 // algorithm.
1582 #define X509_FLAG_NO_SIGDUMP (1L << 9)
1583 
1584 // X509_FLAG_NO_AUX skips printing auxiliary properties. (See |d2i_X509_AUX| and
1585 // related functions.)
1586 #define X509_FLAG_NO_AUX (1L << 10)
1587 
1588 // X509_FLAG_NO_ATTRIBUTES skips printing CSR attributes. It does nothing for
1589 // certificates and CRLs.
1590 #define X509_FLAG_NO_ATTRIBUTES (1L << 11)
1591 
1592 // X509_FLAG_NO_IDS skips printing the issuerUniqueID and subjectUniqueID in a
1593 // certificate. It is ignored in |X509_REQ_print_fp|.
1594 #define X509_FLAG_NO_IDS (1L << 12)
1595 
1596 // X509_print_ex writes a human-readable representation of |x| to |bp|. It
1597 // returns one on success and zero on error. |nmflags| is the flags parameter
1598 // for |X509_NAME_print_ex| when printing the subject and issuer. |cflag| should
1599 // be some combination of the |X509_FLAG_*| constants.
1600 OPENSSL_EXPORT int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflag,
1601                                  unsigned long cflag);
1602 
1603 // X509_print_ex_fp behaves like |X509_print_ex| but writes to |fp|.
1604 OPENSSL_EXPORT int X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag,
1605                                     unsigned long cflag);
1606 
1607 // X509_print calls |X509_print_ex| with |XN_FLAG_COMPAT| and |X509_FLAG_COMPAT|
1608 // flags.
1609 OPENSSL_EXPORT int X509_print(BIO *bp, X509 *x);
1610 
1611 // X509_print_fp behaves like |X509_print| but writes to |fp|.
1612 OPENSSL_EXPORT int X509_print_fp(FILE *fp, X509 *x);
1613 
1614 // X509_CRL_print writes a human-readable representation of |x| to |bp|. It
1615 // returns one on success and zero on error.
1616 OPENSSL_EXPORT int X509_CRL_print(BIO *bp, X509_CRL *x);
1617 
1618 // X509_CRL_print_fp behaves like |X509_CRL_print| but writes to |fp|.
1619 OPENSSL_EXPORT int X509_CRL_print_fp(FILE *fp, X509_CRL *x);
1620 
1621 // X509_REQ_print_ex writes a human-readable representation of |x| to |bp|. It
1622 // returns one on success and zero on error. |nmflags| is the flags parameter
1623 // for |X509_NAME_print_ex|, when printing the subject. |cflag| should be some
1624 // combination of the |X509_FLAG_*| constants.
1625 OPENSSL_EXPORT int X509_REQ_print_ex(BIO *bp, X509_REQ *x, unsigned long nmflag,
1626                                      unsigned long cflag);
1627 
1628 // X509_REQ_print calls |X509_REQ_print_ex| with |XN_FLAG_COMPAT| and
1629 // |X509_FLAG_COMPAT| flags.
1630 OPENSSL_EXPORT int X509_REQ_print(BIO *bp, X509_REQ *req);
1631 
1632 // X509_REQ_print_fp behaves like |X509_REQ_print| but writes to |fp|.
1633 OPENSSL_EXPORT int X509_REQ_print_fp(FILE *fp, X509_REQ *req);
1634 
1635 // The following flags are control |X509_NAME_print_ex|. They must not collide
1636 // with |ASN1_STRFLGS_*|.
1637 //
1638 // TODO(davidben): This is far, far too many options and most of them are
1639 // useless. Trim this down.
1640 
1641 // XN_FLAG_COMPAT prints with |X509_NAME_print|'s format and return value
1642 // convention.
1643 #define XN_FLAG_COMPAT 0
1644 
1645 // XN_FLAG_SEP_MASK determines the separators to use between attributes.
1646 #define XN_FLAG_SEP_MASK (0xf << 16)
1647 
1648 // XN_FLAG_SEP_COMMA_PLUS separates RDNs with "," and attributes within an RDN
1649 // with "+", as in RFC 2253.
1650 #define XN_FLAG_SEP_COMMA_PLUS (1 << 16)
1651 
1652 // XN_FLAG_SEP_CPLUS_SPC behaves like |XN_FLAG_SEP_COMMA_PLUS| but adds spaces
1653 // between the separators.
1654 #define XN_FLAG_SEP_CPLUS_SPC (2 << 16)
1655 
1656 // XN_FLAG_SEP_SPLUS_SPC separates RDNs with "; " and attributes within an RDN
1657 // with " + ".
1658 #define XN_FLAG_SEP_SPLUS_SPC (3 << 16)
1659 
1660 // XN_FLAG_SEP_MULTILINE prints each attribute on one line.
1661 #define XN_FLAG_SEP_MULTILINE (4 << 16)
1662 
1663 // XN_FLAG_DN_REV prints RDNs in reverse, from least significant to most
1664 // significant, as RFC 2253.
1665 #define XN_FLAG_DN_REV (1 << 20)
1666 
1667 // XN_FLAG_FN_MASK determines how attribute types are displayed.
1668 #define XN_FLAG_FN_MASK (0x3 << 21)
1669 
1670 // XN_FLAG_FN_SN uses the attribute type's short name, when available.
1671 #define XN_FLAG_FN_SN 0
1672 
1673 // XN_FLAG_SPC_EQ wraps the "=" operator with spaces when printing attributes.
1674 #define XN_FLAG_SPC_EQ (1 << 23)
1675 
1676 // XN_FLAG_DUMP_UNKNOWN_FIELDS causes unknown attribute types to be printed in
1677 // hex, as in RFC 2253.
1678 #define XN_FLAG_DUMP_UNKNOWN_FIELDS (1 << 24)
1679 
1680 // XN_FLAG_RFC2253 prints like RFC 2253.
1681 #define XN_FLAG_RFC2253                                             \
1682   (ASN1_STRFLGS_RFC2253 | XN_FLAG_SEP_COMMA_PLUS | XN_FLAG_DN_REV | \
1683    XN_FLAG_FN_SN | XN_FLAG_DUMP_UNKNOWN_FIELDS)
1684 
1685 // XN_FLAG_ONELINE prints a one-line representation of the name.
1686 #define XN_FLAG_ONELINE                                                    \
1687   (ASN1_STRFLGS_RFC2253 | ASN1_STRFLGS_ESC_QUOTE | XN_FLAG_SEP_CPLUS_SPC | \
1688    XN_FLAG_SPC_EQ | XN_FLAG_FN_SN)
1689 
1690 // X509_NAME_print_ex writes a human-readable representation of |nm| to |out|.
1691 // Each line of output is indented by |indent| spaces. It returns the number of
1692 // bytes written on success, and -1 on error. If |out| is NULL, it returns the
1693 // number of bytes it would have written but does not write anything. |flags|
1694 // should be some combination of |XN_FLAG_*| and |ASN1_STRFLGS_*| values and
1695 // determines the output. If unsure, use |XN_FLAG_RFC2253|.
1696 //
1697 // If |flags| is |XN_FLAG_COMPAT|, or zero, this function calls
1698 // |X509_NAME_print| instead. In that case, it returns one on success, rather
1699 // than the output length.
1700 OPENSSL_EXPORT int X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent,
1701                                       unsigned long flags);
1702 
1703 // X509_NAME_print prints a human-readable representation of |name| to |bp|. It
1704 // returns one on success and zero on error. |obase| is ignored.
1705 //
1706 // This function outputs a legacy format that does not correctly handle string
1707 // encodings and other cases. Prefer |X509_NAME_print_ex| if printing a name for
1708 // debugging purposes.
1709 OPENSSL_EXPORT int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase);
1710 
1711 // X509_NAME_oneline writes a human-readable representation to |name| to a
1712 // buffer as a NUL-terminated C string.
1713 //
1714 // If |buf| is NULL, returns a newly-allocated buffer containing the result on
1715 // success, or NULL on error. The buffer must be released with |OPENSSL_free|
1716 // when done.
1717 //
1718 // If |buf| is non-NULL, at most |size| bytes of output are written to |buf|
1719 // instead. |size| includes the trailing NUL. The function then returns |buf| on
1720 // success or NULL on error. If the output does not fit in |size| bytes, the
1721 // output is silently truncated at an attribute boundary.
1722 //
1723 // This function outputs a legacy format that does not correctly handle string
1724 // encodings and other cases. Prefer |X509_NAME_print_ex| if printing a name for
1725 // debugging purposes.
1726 OPENSSL_EXPORT char *X509_NAME_oneline(const X509_NAME *name, char *buf, int size);
1727 
1728 // X509_NAME_print_ex_fp behaves like |X509_NAME_print_ex| but writes to |fp|.
1729 OPENSSL_EXPORT int X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm,
1730                                          int indent, unsigned long flags);
1731 
1732 // X509_signature_dump writes a human-readable representation of |sig| to |bio|,
1733 // indented with |indent| spaces. It returns one on success and zero on error.
1734 OPENSSL_EXPORT int X509_signature_dump(BIO *bio, const ASN1_STRING *sig,
1735                                        int indent);
1736 
1737 // X509_signature_print writes a human-readable representation of |alg| and
1738 // |sig| to |bio|. It returns one on success and zero on error.
1739 OPENSSL_EXPORT int X509_signature_print(BIO *bio, const X509_ALGOR *alg,
1740                                         const ASN1_STRING *sig);
1741 
1742 
1743 // Convenience functions.
1744 
1745 // X509_pubkey_digest hashes the contents of the BIT STRING in |x509|'s
1746 // subjectPublicKeyInfo field with |md| and writes the result to |out|.
1747 // |EVP_MD_CTX_size| bytes are written, which is at most |EVP_MAX_MD_SIZE|. If
1748 // |out_len| is not NULL, |*out_len| is set to the number of bytes written. This
1749 // function returns one on success and zero on error.
1750 //
1751 // This hash omits the BIT STRING tag, length, and number of unused bits. It
1752 // also omits the AlgorithmIdentifier which describes the key type. It
1753 // corresponds to the OCSP KeyHash definition and is not suitable for other
1754 // purposes.
1755 OPENSSL_EXPORT int X509_pubkey_digest(const X509 *x509, const EVP_MD *md,
1756                                       uint8_t *out, unsigned *out_len);
1757 
1758 // X509_digest hashes |x509|'s DER encoding with |md| and writes the result to
1759 // |out|. |EVP_MD_CTX_size| bytes are written, which is at most
1760 // |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number
1761 // of bytes written. This function returns one on success and zero on error.
1762 // Note this digest covers the entire certificate, not just the signed portion.
1763 OPENSSL_EXPORT int X509_digest(const X509 *x509, const EVP_MD *md, uint8_t *out,
1764                                unsigned *out_len);
1765 
1766 // X509_CRL_digest hashes |crl|'s DER encoding with |md| and writes the result
1767 // to |out|. |EVP_MD_CTX_size| bytes are written, which is at most
1768 // |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number
1769 // of bytes written. This function returns one on success and zero on error.
1770 // Note this digest covers the entire CRL, not just the signed portion.
1771 OPENSSL_EXPORT int X509_CRL_digest(const X509_CRL *crl, const EVP_MD *md,
1772                                    uint8_t *out, unsigned *out_len);
1773 
1774 // X509_REQ_digest hashes |req|'s DER encoding with |md| and writes the result
1775 // to |out|. |EVP_MD_CTX_size| bytes are written, which is at most
1776 // |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number
1777 // of bytes written. This function returns one on success and zero on error.
1778 // Note this digest covers the entire certificate request, not just the signed
1779 // portion.
1780 OPENSSL_EXPORT int X509_REQ_digest(const X509_REQ *req, const EVP_MD *md,
1781                                    uint8_t *out, unsigned *out_len);
1782 
1783 // X509_NAME_digest hashes |name|'s DER encoding with |md| and writes the result
1784 // to |out|. |EVP_MD_CTX_size| bytes are written, which is at most
1785 // |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number
1786 // of bytes written. This function returns one on success and zero on error.
1787 OPENSSL_EXPORT int X509_NAME_digest(const X509_NAME *name, const EVP_MD *md,
1788                                     uint8_t *out, unsigned *out_len);
1789 
1790 // The following functions behave like the corresponding unsuffixed |d2i_*|
1791 // functions, but read the result from |bp| instead. Callers using these
1792 // functions with memory |BIO|s to parse structures already in memory should use
1793 // |d2i_*| instead.
1794 OPENSSL_EXPORT X509 *d2i_X509_bio(BIO *bp, X509 **x509);
1795 OPENSSL_EXPORT X509_CRL *d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl);
1796 OPENSSL_EXPORT X509_REQ *d2i_X509_REQ_bio(BIO *bp, X509_REQ **req);
1797 OPENSSL_EXPORT RSA *d2i_RSAPrivateKey_bio(BIO *bp, RSA **rsa);
1798 OPENSSL_EXPORT RSA *d2i_RSAPublicKey_bio(BIO *bp, RSA **rsa);
1799 OPENSSL_EXPORT RSA *d2i_RSA_PUBKEY_bio(BIO *bp, RSA **rsa);
1800 OPENSSL_EXPORT DSA *d2i_DSA_PUBKEY_bio(BIO *bp, DSA **dsa);
1801 OPENSSL_EXPORT DSA *d2i_DSAPrivateKey_bio(BIO *bp, DSA **dsa);
1802 OPENSSL_EXPORT EC_KEY *d2i_EC_PUBKEY_bio(BIO *bp, EC_KEY **eckey);
1803 OPENSSL_EXPORT EC_KEY *d2i_ECPrivateKey_bio(BIO *bp, EC_KEY **eckey);
1804 OPENSSL_EXPORT X509_SIG *d2i_PKCS8_bio(BIO *bp, X509_SIG **p8);
1805 OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_bio(
1806     BIO *bp, PKCS8_PRIV_KEY_INFO **p8inf);
1807 OPENSSL_EXPORT EVP_PKEY *d2i_PUBKEY_bio(BIO *bp, EVP_PKEY **a);
1808 OPENSSL_EXPORT DH *d2i_DHparams_bio(BIO *bp, DH **dh);
1809 
1810 // d2i_PrivateKey_bio behaves like |d2i_AutoPrivateKey|, but reads from |bp|
1811 // instead.
1812 OPENSSL_EXPORT EVP_PKEY *d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a);
1813 
1814 // The following functions behave like the corresponding unsuffixed |i2d_*|
1815 // functions, but write the result to |bp|. They return one on success and zero
1816 // on error. Callers using them with memory |BIO|s to encode structures to
1817 // memory should use |i2d_*| directly instead.
1818 OPENSSL_EXPORT int i2d_X509_bio(BIO *bp, X509 *x509);
1819 OPENSSL_EXPORT int i2d_X509_CRL_bio(BIO *bp, X509_CRL *crl);
1820 OPENSSL_EXPORT int i2d_X509_REQ_bio(BIO *bp, X509_REQ *req);
1821 OPENSSL_EXPORT int i2d_RSAPrivateKey_bio(BIO *bp, RSA *rsa);
1822 OPENSSL_EXPORT int i2d_RSAPublicKey_bio(BIO *bp, RSA *rsa);
1823 OPENSSL_EXPORT int i2d_RSA_PUBKEY_bio(BIO *bp, RSA *rsa);
1824 OPENSSL_EXPORT int i2d_DSA_PUBKEY_bio(BIO *bp, DSA *dsa);
1825 OPENSSL_EXPORT int i2d_DSAPrivateKey_bio(BIO *bp, DSA *dsa);
1826 OPENSSL_EXPORT int i2d_EC_PUBKEY_bio(BIO *bp, EC_KEY *eckey);
1827 OPENSSL_EXPORT int i2d_ECPrivateKey_bio(BIO *bp, EC_KEY *eckey);
1828 OPENSSL_EXPORT int i2d_PKCS8_bio(BIO *bp, X509_SIG *p8);
1829 OPENSSL_EXPORT int i2d_PKCS8_PRIV_KEY_INFO_bio(BIO *bp,
1830                                                PKCS8_PRIV_KEY_INFO *p8inf);
1831 OPENSSL_EXPORT int i2d_PrivateKey_bio(BIO *bp, EVP_PKEY *pkey);
1832 OPENSSL_EXPORT int i2d_PUBKEY_bio(BIO *bp, EVP_PKEY *pkey);
1833 OPENSSL_EXPORT int i2d_DHparams_bio(BIO *bp, const DH *dh);
1834 
1835 // i2d_PKCS8PrivateKeyInfo_bio encodes |key| as a PKCS#8 PrivateKeyInfo
1836 // structure (see |EVP_marshal_private_key|) and writes the result to |bp|. It
1837 // returns one on success and zero on error.
1838 OPENSSL_EXPORT int i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, EVP_PKEY *key);
1839 
1840 // The following functions behave like the corresponding |d2i_*_bio| functions,
1841 // but read from |fp| instead.
1842 OPENSSL_EXPORT X509 *d2i_X509_fp(FILE *fp, X509 **x509);
1843 OPENSSL_EXPORT X509_CRL *d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl);
1844 OPENSSL_EXPORT X509_REQ *d2i_X509_REQ_fp(FILE *fp, X509_REQ **req);
1845 OPENSSL_EXPORT RSA *d2i_RSAPrivateKey_fp(FILE *fp, RSA **rsa);
1846 OPENSSL_EXPORT RSA *d2i_RSAPublicKey_fp(FILE *fp, RSA **rsa);
1847 OPENSSL_EXPORT RSA *d2i_RSA_PUBKEY_fp(FILE *fp, RSA **rsa);
1848 OPENSSL_EXPORT DSA *d2i_DSA_PUBKEY_fp(FILE *fp, DSA **dsa);
1849 OPENSSL_EXPORT DSA *d2i_DSAPrivateKey_fp(FILE *fp, DSA **dsa);
1850 OPENSSL_EXPORT EC_KEY *d2i_EC_PUBKEY_fp(FILE *fp, EC_KEY **eckey);
1851 OPENSSL_EXPORT EC_KEY *d2i_ECPrivateKey_fp(FILE *fp, EC_KEY **eckey);
1852 OPENSSL_EXPORT X509_SIG *d2i_PKCS8_fp(FILE *fp, X509_SIG **p8);
1853 OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_fp(
1854     FILE *fp, PKCS8_PRIV_KEY_INFO **p8inf);
1855 OPENSSL_EXPORT EVP_PKEY *d2i_PrivateKey_fp(FILE *fp, EVP_PKEY **a);
1856 OPENSSL_EXPORT EVP_PKEY *d2i_PUBKEY_fp(FILE *fp, EVP_PKEY **a);
1857 
1858 // The following functions behave like the corresponding |i2d_*_bio| functions,
1859 // but write to |fp| instead.
1860 OPENSSL_EXPORT int i2d_X509_fp(FILE *fp, X509 *x509);
1861 OPENSSL_EXPORT int i2d_X509_CRL_fp(FILE *fp, X509_CRL *crl);
1862 OPENSSL_EXPORT int i2d_X509_REQ_fp(FILE *fp, X509_REQ *req);
1863 OPENSSL_EXPORT int i2d_RSAPrivateKey_fp(FILE *fp, RSA *rsa);
1864 OPENSSL_EXPORT int i2d_RSAPublicKey_fp(FILE *fp, RSA *rsa);
1865 OPENSSL_EXPORT int i2d_RSA_PUBKEY_fp(FILE *fp, RSA *rsa);
1866 OPENSSL_EXPORT int i2d_DSA_PUBKEY_fp(FILE *fp, DSA *dsa);
1867 OPENSSL_EXPORT int i2d_DSAPrivateKey_fp(FILE *fp, DSA *dsa);
1868 OPENSSL_EXPORT int i2d_EC_PUBKEY_fp(FILE *fp, EC_KEY *eckey);
1869 OPENSSL_EXPORT int i2d_ECPrivateKey_fp(FILE *fp, EC_KEY *eckey);
1870 OPENSSL_EXPORT int i2d_PKCS8_fp(FILE *fp, X509_SIG *p8);
1871 OPENSSL_EXPORT int i2d_PKCS8_PRIV_KEY_INFO_fp(FILE *fp,
1872                                               PKCS8_PRIV_KEY_INFO *p8inf);
1873 OPENSSL_EXPORT int i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, EVP_PKEY *key);
1874 OPENSSL_EXPORT int i2d_PrivateKey_fp(FILE *fp, EVP_PKEY *pkey);
1875 OPENSSL_EXPORT int i2d_PUBKEY_fp(FILE *fp, EVP_PKEY *pkey);
1876 
1877 // X509_find_by_issuer_and_serial returns the first |X509| in |sk| whose issuer
1878 // and serial are |name| and |serial|, respectively. If no match is found, it
1879 // returns NULL.
1880 OPENSSL_EXPORT X509 *X509_find_by_issuer_and_serial(const STACK_OF(X509) *sk,
1881                                                     X509_NAME *name,
1882                                                     const ASN1_INTEGER *serial);
1883 
1884 // X509_find_by_subject returns the first |X509| in |sk| whose subject is
1885 // |name|. If no match is found, it returns NULL.
1886 OPENSSL_EXPORT X509 *X509_find_by_subject(const STACK_OF(X509) *sk,
1887                                           X509_NAME *name);
1888 
1889 // X509_cmp_time compares |s| against |*t|. On success, it returns a negative
1890 // number if |s| <= |*t| and a positive number if |s| > |*t|. On error, it
1891 // returns zero. If |t| is NULL, it uses the current time instead of |*t|.
1892 //
1893 // WARNING: Unlike most comparison functions, this function returns zero on
1894 // error, not equality.
1895 OPENSSL_EXPORT int X509_cmp_time(const ASN1_TIME *s, time_t *t);
1896 
1897 // X509_cmp_time_posix compares |s| against |t|. On success, it returns a
1898 // negative number if |s| <= |t| and a positive number if |s| > |t|. On error,
1899 // it returns zero.
1900 //
1901 // WARNING: Unlike most comparison functions, this function returns zero on
1902 // error, not equality.
1903 OPENSSL_EXPORT int X509_cmp_time_posix(const ASN1_TIME *s, int64_t t);
1904 
1905 // X509_cmp_current_time behaves like |X509_cmp_time| but compares |s| against
1906 // the current time.
1907 OPENSSL_EXPORT int X509_cmp_current_time(const ASN1_TIME *s);
1908 
1909 // X509_time_adj calls |X509_time_adj_ex| with |offset_day| equal to zero.
1910 OPENSSL_EXPORT ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec,
1911                                         time_t *t);
1912 
1913 // X509_time_adj_ex behaves like |ASN1_TIME_adj|, but adds an offset to |*t|. If
1914 // |t| is NULL, it uses the current time instead of |*t|.
1915 OPENSSL_EXPORT ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s, int offset_day,
1916                                            long offset_sec, time_t *t);
1917 
1918 // X509_gmtime_adj behaves like |X509_time_adj_ex| but adds |offset_sec| to the
1919 // current time.
1920 OPENSSL_EXPORT ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long offset_sec);
1921 
1922 
1923 // ex_data functions.
1924 //
1925 // See |ex_data.h| for details.
1926 
1927 OPENSSL_EXPORT int X509_get_ex_new_index(long argl, void *argp,
1928                                          CRYPTO_EX_unused *unused,
1929                                          CRYPTO_EX_dup *dup_unused,
1930                                          CRYPTO_EX_free *free_func);
1931 OPENSSL_EXPORT int X509_set_ex_data(X509 *r, int idx, void *arg);
1932 OPENSSL_EXPORT void *X509_get_ex_data(X509 *r, int idx);
1933 
1934 OPENSSL_EXPORT int X509_STORE_CTX_get_ex_new_index(long argl, void *argp,
1935                                                    CRYPTO_EX_unused *unused,
1936                                                    CRYPTO_EX_dup *dup_unused,
1937                                                    CRYPTO_EX_free *free_func);
1938 OPENSSL_EXPORT int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx,
1939                                               void *data);
1940 OPENSSL_EXPORT void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx);
1941 
1942 
1943 // Deprecated functions.
1944 
1945 // X509_get_notBefore returns |x509|'s notBefore time. Note this function is not
1946 // const-correct for legacy reasons. Use |X509_get0_notBefore| or
1947 // |X509_getm_notBefore| instead.
1948 OPENSSL_EXPORT ASN1_TIME *X509_get_notBefore(const X509 *x509);
1949 
1950 // X509_get_notAfter returns |x509|'s notAfter time. Note this function is not
1951 // const-correct for legacy reasons. Use |X509_get0_notAfter| or
1952 // |X509_getm_notAfter| instead.
1953 OPENSSL_EXPORT ASN1_TIME *X509_get_notAfter(const X509 *x509);
1954 
1955 // X509_set_notBefore calls |X509_set1_notBefore|. Use |X509_set1_notBefore|
1956 // instead.
1957 OPENSSL_EXPORT int X509_set_notBefore(X509 *x509, const ASN1_TIME *tm);
1958 
1959 // X509_set_notAfter calls |X509_set1_notAfter|. Use |X509_set1_notAfter|
1960 // instead.
1961 OPENSSL_EXPORT int X509_set_notAfter(X509 *x509, const ASN1_TIME *tm);
1962 
1963 // X509_CRL_get_lastUpdate returns a mutable pointer to |crl|'s thisUpdate time.
1964 // The OpenSSL API refers to this field as lastUpdate.
1965 //
1966 // Use |X509_CRL_get0_lastUpdate| or |X509_CRL_set1_lastUpdate| instead.
1967 OPENSSL_EXPORT ASN1_TIME *X509_CRL_get_lastUpdate(X509_CRL *crl);
1968 
1969 // X509_CRL_get_nextUpdate returns a mutable pointer to |crl|'s nextUpdate time,
1970 // or NULL if |crl| has none. Use |X509_CRL_get0_nextUpdate| or
1971 // |X509_CRL_set1_nextUpdate| instead.
1972 OPENSSL_EXPORT ASN1_TIME *X509_CRL_get_nextUpdate(X509_CRL *crl);
1973 
1974 // X509_extract_key is a legacy alias to |X509_get_pubkey|. Use
1975 // |X509_get_pubkey| instead.
1976 #define X509_extract_key(x) X509_get_pubkey(x)
1977 
1978 // X509_REQ_extract_key is a legacy alias for |X509_REQ_get_pubkey|.
1979 #define X509_REQ_extract_key(a) X509_REQ_get_pubkey(a)
1980 
1981 // X509_name_cmp is a legacy alias for |X509_NAME_cmp|.
1982 #define X509_name_cmp(a, b) X509_NAME_cmp((a), (b))
1983 
1984 // The following symbols are deprecated aliases to |X509_CRL_set1_*|.
1985 #define X509_CRL_set_lastUpdate X509_CRL_set1_lastUpdate
1986 #define X509_CRL_set_nextUpdate X509_CRL_set1_nextUpdate
1987 
1988 // X509_get_serialNumber returns a mutable pointer to |x509|'s serial number.
1989 // Prefer |X509_get0_serialNumber|.
1990 OPENSSL_EXPORT ASN1_INTEGER *X509_get_serialNumber(X509 *x509);
1991 
1992 // X509_NAME_get_text_by_OBJ finds the first attribute with type |obj| in
1993 // |name|. If found, it ignores the value's ASN.1 type, writes the raw
1994 // |ASN1_STRING| representation to |buf|, followed by a NUL byte, and
1995 // returns the number of bytes in output, excluding the NUL byte.
1996 //
1997 // This function writes at most |len| bytes, including the NUL byte. If |len| is
1998 // not large enough, it silently truncates the output to fit. If |buf| is NULL,
1999 // it instead writes enough and returns the number of bytes in the output,
2000 // excluding the NUL byte.
2001 //
2002 // WARNING: Do not use this function. It does not return enough information for
2003 // the caller to correctly interpret its output. The attribute value may be of
2004 // any type, including one of several ASN.1 string encodings, but this function
2005 // only outputs the raw |ASN1_STRING| representation. See
2006 // https://crbug.com/boringssl/436.
2007 OPENSSL_EXPORT int X509_NAME_get_text_by_OBJ(const X509_NAME *name,
2008                                              const ASN1_OBJECT *obj, char *buf,
2009                                              int len);
2010 
2011 // X509_NAME_get_text_by_NID behaves like |X509_NAME_get_text_by_OBJ| except it
2012 // finds an attribute of type |nid|, which should be one of the |NID_*|
2013 // constants.
2014 OPENSSL_EXPORT int X509_NAME_get_text_by_NID(const X509_NAME *name, int nid,
2015                                              char *buf, int len);
2016 
2017 
2018 // Private structures.
2019 
2020 struct X509_algor_st {
2021   ASN1_OBJECT *algorithm;
2022   ASN1_TYPE *parameter;
2023 } /* X509_ALGOR */;
2024 
2025 
2026 // Functions below this point have not yet been organized into sections.
2027 
2028 #define X509_FILETYPE_PEM 1
2029 #define X509_FILETYPE_ASN1 2
2030 #define X509_FILETYPE_DEFAULT 3
2031 
2032 #define X509v3_KU_DIGITAL_SIGNATURE 0x0080
2033 #define X509v3_KU_NON_REPUDIATION 0x0040
2034 #define X509v3_KU_KEY_ENCIPHERMENT 0x0020
2035 #define X509v3_KU_DATA_ENCIPHERMENT 0x0010
2036 #define X509v3_KU_KEY_AGREEMENT 0x0008
2037 #define X509v3_KU_KEY_CERT_SIGN 0x0004
2038 #define X509v3_KU_CRL_SIGN 0x0002
2039 #define X509v3_KU_ENCIPHER_ONLY 0x0001
2040 #define X509v3_KU_DECIPHER_ONLY 0x8000
2041 #define X509v3_KU_UNDEF 0xffff
2042 
2043 // This stuff is certificate "auxiliary info"
2044 // it contains details which are useful in certificate
2045 // stores and databases. When used this is tagged onto
2046 // the end of the certificate itself
2047 
2048 DECLARE_STACK_OF(DIST_POINT)
2049 DECLARE_STACK_OF(GENERAL_NAME)
2050 
2051 // This is used for a table of trust checking functions
2052 
2053 struct x509_trust_st {
2054   int trust;
2055   int flags;
2056   int (*check_trust)(struct x509_trust_st *, X509 *, int);
2057   char *name;
2058   int arg1;
2059   void *arg2;
2060 } /* X509_TRUST */;
2061 
2062 DEFINE_STACK_OF(X509_TRUST)
2063 
2064 // standard trust ids
2065 
2066 #define X509_TRUST_DEFAULT (-1)  // Only valid in purpose settings
2067 
2068 #define X509_TRUST_COMPAT 1
2069 #define X509_TRUST_SSL_CLIENT 2
2070 #define X509_TRUST_SSL_SERVER 3
2071 #define X509_TRUST_EMAIL 4
2072 #define X509_TRUST_OBJECT_SIGN 5
2073 #define X509_TRUST_OCSP_SIGN 6
2074 #define X509_TRUST_OCSP_REQUEST 7
2075 #define X509_TRUST_TSA 8
2076 
2077 // Keep these up to date!
2078 #define X509_TRUST_MIN 1
2079 #define X509_TRUST_MAX 8
2080 
2081 
2082 // trust_flags values
2083 #define X509_TRUST_DYNAMIC 1
2084 #define X509_TRUST_DYNAMIC_NAME 2
2085 
2086 // check_trust return codes
2087 
2088 #define X509_TRUST_TRUSTED 1
2089 #define X509_TRUST_REJECTED 2
2090 #define X509_TRUST_UNTRUSTED 3
2091 
2092 DEFINE_STACK_OF(X509_REVOKED)
2093 
2094 DECLARE_STACK_OF(GENERAL_NAMES)
2095 
2096 struct private_key_st {
2097   int version;
2098   // The PKCS#8 data types
2099   X509_ALGOR *enc_algor;
2100   ASN1_OCTET_STRING *enc_pkey;  // encrypted pub key
2101 
2102   // When decrypted, the following will not be NULL
2103   EVP_PKEY *dec_pkey;
2104 
2105   // used to encrypt and decrypt
2106   int key_length;
2107   char *key_data;
2108   int key_free;  // true if we should auto free key_data
2109 
2110   // expanded version of 'enc_algor'
2111   EVP_CIPHER_INFO cipher;
2112 } /* X509_PKEY */;
2113 
2114 struct X509_info_st {
2115   X509 *x509;
2116   X509_CRL *crl;
2117   X509_PKEY *x_pkey;
2118 
2119   EVP_CIPHER_INFO enc_cipher;
2120   int enc_len;
2121   char *enc_data;
2122 
2123 } /* X509_INFO */;
2124 
2125 DEFINE_STACK_OF(X509_INFO)
2126 
2127 // The next 2 structures and their 8 routines were sent to me by
2128 // Pat Richard <patr@x509.com> and are used to manipulate
2129 // Netscapes spki structures - useful if you are writing a CA web page
2130 struct Netscape_spkac_st {
2131   X509_PUBKEY *pubkey;
2132   ASN1_IA5STRING *challenge;  // challenge sent in atlas >= PR2
2133 } /* NETSCAPE_SPKAC */;
2134 
2135 struct Netscape_spki_st {
2136   NETSCAPE_SPKAC *spkac;  // signed public key and challenge
2137   X509_ALGOR *sig_algor;
2138   ASN1_BIT_STRING *signature;
2139 } /* NETSCAPE_SPKI */;
2140 
2141 // X509_get_pathlen returns path length constraint from the basic constraints
2142 // extension in |x509|. (See RFC 5280, section 4.2.1.9.) It returns -1 if the
2143 // constraint is not present, or if some extension in |x509| was invalid.
2144 //
2145 // Note that decoding an |X509| object will not check for invalid extensions. To
2146 // detect the error case, call |X509_get_extensions_flags| and check the
2147 // |EXFLAG_INVALID| bit.
2148 OPENSSL_EXPORT long X509_get_pathlen(X509 *x509);
2149 
2150 // X509_SIG_get0 sets |*out_alg| and |*out_digest| to non-owning pointers to
2151 // |sig|'s algorithm and digest fields, respectively. Either |out_alg| and
2152 // |out_digest| may be NULL to skip those fields.
2153 OPENSSL_EXPORT void X509_SIG_get0(const X509_SIG *sig,
2154                                   const X509_ALGOR **out_alg,
2155                                   const ASN1_OCTET_STRING **out_digest);
2156 
2157 // X509_SIG_getm behaves like |X509_SIG_get0| but returns mutable pointers.
2158 OPENSSL_EXPORT void X509_SIG_getm(X509_SIG *sig, X509_ALGOR **out_alg,
2159                                   ASN1_OCTET_STRING **out_digest);
2160 
2161 // X509_verify_cert_error_string returns |err| as a human-readable string, where
2162 // |err| should be one of the |X509_V_*| values. If |err| is unknown, it returns
2163 // a default description.
2164 OPENSSL_EXPORT const char *X509_verify_cert_error_string(long err);
2165 
2166 // NETSCAPE_SPKI_verify checks that |spki| has a valid signature by |pkey|. It
2167 // returns one if the signature is valid and zero otherwise.
2168 OPENSSL_EXPORT int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *spki, EVP_PKEY *pkey);
2169 
2170 // NETSCAPE_SPKI_b64_decode decodes |len| bytes from |str| as a base64-encoded
2171 // Netscape signed public key and challenge (SPKAC) structure. It returns a
2172 // newly-allocated |NETSCAPE_SPKI| structure with the result, or NULL on error.
2173 // If |len| is 0 or negative, the length is calculated with |strlen| and |str|
2174 // must be a NUL-terminated C string.
2175 OPENSSL_EXPORT NETSCAPE_SPKI *NETSCAPE_SPKI_b64_decode(const char *str,
2176                                                        ossl_ssize_t len);
2177 
2178 // NETSCAPE_SPKI_b64_encode encodes |spki| as a base64-encoded Netscape signed
2179 // public key and challenge (SPKAC) structure. It returns a newly-allocated
2180 // NUL-terminated C string with the result, or NULL on error. The caller must
2181 // release the memory with |OPENSSL_free| when done.
2182 OPENSSL_EXPORT char *NETSCAPE_SPKI_b64_encode(NETSCAPE_SPKI *spki);
2183 
2184 // NETSCAPE_SPKI_get_pubkey decodes and returns the public key in |spki| as an
2185 // |EVP_PKEY|, or NULL on error. The caller takes ownership of the resulting
2186 // pointer and must call |EVP_PKEY_free| when done.
2187 OPENSSL_EXPORT EVP_PKEY *NETSCAPE_SPKI_get_pubkey(NETSCAPE_SPKI *spki);
2188 
2189 // NETSCAPE_SPKI_set_pubkey sets |spki|'s public key to |pkey|. It returns one
2190 // on success or zero on error. This function does not take ownership of |pkey|,
2191 // so the caller may continue to manage its lifetime independently of |spki|.
2192 OPENSSL_EXPORT int NETSCAPE_SPKI_set_pubkey(NETSCAPE_SPKI *spki,
2193                                             EVP_PKEY *pkey);
2194 
2195 // NETSCAPE_SPKI_sign signs |spki| with |pkey| and replaces the signature
2196 // algorithm and signature fields. It returns one on success and zero on error.
2197 // This function uses digest algorithm |md|, or |pkey|'s default if NULL. Other
2198 // signing parameters use |pkey|'s defaults.
2199 OPENSSL_EXPORT int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *spki, EVP_PKEY *pkey,
2200                                       const EVP_MD *md);
2201 
2202 // X509_REVOKED_dup returns a newly-allocated copy of |rev|, or NULL on error.
2203 // This function works by serializing the structure, so if |rev| is incomplete,
2204 // it may fail.
2205 OPENSSL_EXPORT X509_REVOKED *X509_REVOKED_dup(const X509_REVOKED *rev);
2206 
2207 OPENSSL_EXPORT const char *X509_get_default_cert_area(void);
2208 OPENSSL_EXPORT const char *X509_get_default_cert_dir(void);
2209 OPENSSL_EXPORT const char *X509_get_default_cert_file(void);
2210 OPENSSL_EXPORT const char *X509_get_default_cert_dir_env(void);
2211 OPENSSL_EXPORT const char *X509_get_default_cert_file_env(void);
2212 OPENSSL_EXPORT const char *X509_get_default_private_dir(void);
2213 
2214 DECLARE_ASN1_FUNCTIONS_const(X509_PUBKEY)
2215 
2216 // X509_PUBKEY_set serializes |pkey| into a newly-allocated |X509_PUBKEY|
2217 // structure. On success, it frees |*x|, sets |*x| to the new object, and
2218 // returns one. Otherwise, it returns zero.
2219 OPENSSL_EXPORT int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey);
2220 
2221 // X509_PUBKEY_get decodes the public key in |key| and returns an |EVP_PKEY| on
2222 // success, or NULL on error. The caller must release the result with
2223 // |EVP_PKEY_free| when done. The |EVP_PKEY| is cached in |key|, so callers must
2224 // not mutate the result.
2225 OPENSSL_EXPORT EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key);
2226 
2227 DECLARE_ASN1_FUNCTIONS_const(X509_SIG)
2228 
2229 OPENSSL_EXPORT int X509_add1_trust_object(X509 *x, ASN1_OBJECT *obj);
2230 OPENSSL_EXPORT int X509_add1_reject_object(X509 *x, ASN1_OBJECT *obj);
2231 OPENSSL_EXPORT void X509_trust_clear(X509 *x);
2232 OPENSSL_EXPORT void X509_reject_clear(X509 *x);
2233 
2234 
2235 OPENSSL_EXPORT int X509_TRUST_set(int *t, int trust);
2236 
2237 DECLARE_ASN1_FUNCTIONS_const(X509_REVOKED)
2238 
2239 OPENSSL_EXPORT int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev);
2240 OPENSSL_EXPORT int X509_CRL_get0_by_serial(X509_CRL *crl, X509_REVOKED **ret,
2241                                            ASN1_INTEGER *serial);
2242 OPENSSL_EXPORT int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret,
2243                                          X509 *x);
2244 
2245 OPENSSL_EXPORT X509_PKEY *X509_PKEY_new(void);
2246 OPENSSL_EXPORT void X509_PKEY_free(X509_PKEY *a);
2247 
2248 DECLARE_ASN1_FUNCTIONS_const(NETSCAPE_SPKI)
2249 DECLARE_ASN1_FUNCTIONS_const(NETSCAPE_SPKAC)
2250 
2251 OPENSSL_EXPORT X509_INFO *X509_INFO_new(void);
2252 OPENSSL_EXPORT void X509_INFO_free(X509_INFO *a);
2253 
2254 OPENSSL_EXPORT int ASN1_digest(i2d_of_void *i2d, const EVP_MD *type, char *data,
2255                                unsigned char *md, unsigned int *len);
2256 
2257 OPENSSL_EXPORT int ASN1_item_digest(const ASN1_ITEM *it, const EVP_MD *type,
2258                                     void *data, unsigned char *md,
2259                                     unsigned int *len);
2260 
2261 OPENSSL_EXPORT int ASN1_item_verify(const ASN1_ITEM *it,
2262                                     const X509_ALGOR *algor1,
2263                                     const ASN1_BIT_STRING *signature,
2264                                     void *data, EVP_PKEY *pkey);
2265 
2266 OPENSSL_EXPORT int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1,
2267                                   X509_ALGOR *algor2,
2268                                   ASN1_BIT_STRING *signature, void *data,
2269                                   EVP_PKEY *pkey, const EVP_MD *type);
2270 OPENSSL_EXPORT int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1,
2271                                       X509_ALGOR *algor2,
2272                                       ASN1_BIT_STRING *signature, void *asn,
2273                                       EVP_MD_CTX *ctx);
2274 
2275 OPENSSL_EXPORT int X509_CRL_sort(X509_CRL *crl);
2276 
2277 // X509_REVOKED_get0_serialNumber returns the serial number of the certificate
2278 // revoked by |revoked|.
2279 OPENSSL_EXPORT const ASN1_INTEGER *X509_REVOKED_get0_serialNumber(
2280     const X509_REVOKED *revoked);
2281 
2282 // X509_REVOKED_set_serialNumber sets |revoked|'s serial number to |serial|. It
2283 // returns one on success or zero on error.
2284 OPENSSL_EXPORT int X509_REVOKED_set_serialNumber(X509_REVOKED *revoked,
2285                                                  const ASN1_INTEGER *serial);
2286 
2287 // X509_REVOKED_get0_revocationDate returns the revocation time of the
2288 // certificate revoked by |revoked|.
2289 OPENSSL_EXPORT const ASN1_TIME *X509_REVOKED_get0_revocationDate(
2290     const X509_REVOKED *revoked);
2291 
2292 // X509_REVOKED_set_revocationDate sets |revoked|'s revocation time to |tm|. It
2293 // returns one on success or zero on error.
2294 OPENSSL_EXPORT int X509_REVOKED_set_revocationDate(X509_REVOKED *revoked,
2295                                                    const ASN1_TIME *tm);
2296 
2297 // X509_REVOKED_get0_extensions returns |r|'s extensions list, or NULL if |r|
2298 // omits it.
2299 OPENSSL_EXPORT const STACK_OF(X509_EXTENSION) *X509_REVOKED_get0_extensions(
2300     const X509_REVOKED *r);
2301 
2302 OPENSSL_EXPORT X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer,
2303                                        EVP_PKEY *skey, const EVP_MD *md,
2304                                        unsigned int flags);
2305 
2306 OPENSSL_EXPORT int X509_REQ_check_private_key(X509_REQ *x509, EVP_PKEY *pkey);
2307 
2308 OPENSSL_EXPORT int X509_check_private_key(X509 *x509, const EVP_PKEY *pkey);
2309 
2310 OPENSSL_EXPORT int X509_issuer_name_cmp(const X509 *a, const X509 *b);
2311 OPENSSL_EXPORT unsigned long X509_issuer_name_hash(X509 *a);
2312 
2313 OPENSSL_EXPORT int X509_subject_name_cmp(const X509 *a, const X509 *b);
2314 OPENSSL_EXPORT unsigned long X509_subject_name_hash(X509 *x);
2315 
2316 OPENSSL_EXPORT unsigned long X509_issuer_name_hash_old(X509 *a);
2317 OPENSSL_EXPORT unsigned long X509_subject_name_hash_old(X509 *x);
2318 
2319 OPENSSL_EXPORT int X509_cmp(const X509 *a, const X509 *b);
2320 OPENSSL_EXPORT int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b);
2321 OPENSSL_EXPORT unsigned long X509_NAME_hash(X509_NAME *x);
2322 OPENSSL_EXPORT unsigned long X509_NAME_hash_old(X509_NAME *x);
2323 
2324 OPENSSL_EXPORT int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b);
2325 OPENSSL_EXPORT int X509_CRL_match(const X509_CRL *a, const X509_CRL *b);
2326 
2327 // X509_get_ext_d2i behaves like |X509V3_get_d2i| but looks for the extension in
2328 // |x509|'s extension list.
2329 //
2330 // WARNING: This function is difficult to use correctly. See the documentation
2331 // for |X509V3_get_d2i| for details.
2332 OPENSSL_EXPORT void *X509_get_ext_d2i(const X509 *x509, int nid,
2333                                       int *out_critical, int *out_idx);
2334 
2335 // X509_add1_ext_i2d behaves like |X509V3_add1_i2d| but adds the extension to
2336 // |x|'s extension list.
2337 //
2338 // WARNING: This function may return zero or -1 on error. The caller must also
2339 // ensure |value|'s type matches |nid|. See the documentation for
2340 // |X509V3_add1_i2d| for details.
2341 OPENSSL_EXPORT int X509_add1_ext_i2d(X509 *x, int nid, void *value, int crit,
2342                                      unsigned long flags);
2343 
2344 // X509_CRL_get_ext_d2i behaves like |X509V3_get_d2i| but looks for the
2345 // extension in |crl|'s extension list.
2346 //
2347 // WARNING: This function is difficult to use correctly. See the documentation
2348 // for |X509V3_get_d2i| for details.
2349 OPENSSL_EXPORT void *X509_CRL_get_ext_d2i(const X509_CRL *crl, int nid,
2350                                           int *out_critical, int *out_idx);
2351 
2352 // X509_CRL_add1_ext_i2d behaves like |X509V3_add1_i2d| but adds the extension
2353 // to |x|'s extension list.
2354 //
2355 // WARNING: This function may return zero or -1 on error. The caller must also
2356 // ensure |value|'s type matches |nid|. See the documentation for
2357 // |X509V3_add1_i2d| for details.
2358 OPENSSL_EXPORT int X509_CRL_add1_ext_i2d(X509_CRL *x, int nid, void *value,
2359                                          int crit, unsigned long flags);
2360 
2361 // X509_REVOKED_get_ext_count returns the number of extensions in |x|.
2362 OPENSSL_EXPORT int X509_REVOKED_get_ext_count(const X509_REVOKED *x);
2363 
2364 // X509_REVOKED_get_ext_by_NID behaves like |X509v3_get_ext_by_NID| but searches
2365 // for extensions in |x|.
2366 OPENSSL_EXPORT int X509_REVOKED_get_ext_by_NID(const X509_REVOKED *x, int nid,
2367                                                int lastpos);
2368 
2369 // X509_REVOKED_get_ext_by_OBJ behaves like |X509v3_get_ext_by_OBJ| but searches
2370 // for extensions in |x|.
2371 OPENSSL_EXPORT int X509_REVOKED_get_ext_by_OBJ(const X509_REVOKED *x,
2372                                                const ASN1_OBJECT *obj,
2373                                                int lastpos);
2374 
2375 // X509_REVOKED_get_ext_by_critical behaves like |X509v3_get_ext_by_critical|
2376 // but searches for extensions in |x|.
2377 OPENSSL_EXPORT int X509_REVOKED_get_ext_by_critical(const X509_REVOKED *x,
2378                                                     int crit, int lastpos);
2379 
2380 // X509_REVOKED_get_ext returns the extension in |x| at index |loc|, or NULL if
2381 // |loc| is out of bounds. This function returns a non-const pointer for OpenSSL
2382 // compatibility, but callers should not mutate the result.
2383 OPENSSL_EXPORT X509_EXTENSION *X509_REVOKED_get_ext(const X509_REVOKED *x,
2384                                                     int loc);
2385 
2386 // X509_REVOKED_delete_ext removes the extension in |x| at index |loc| and
2387 // returns the removed extension, or NULL if |loc| was out of bounds. If
2388 // non-NULL, the caller must release the result with |X509_EXTENSION_free|.
2389 OPENSSL_EXPORT X509_EXTENSION *X509_REVOKED_delete_ext(X509_REVOKED *x,
2390                                                        int loc);
2391 
2392 // X509_REVOKED_add_ext adds a copy of |ex| to |x|. It returns one on success
2393 // and zero on failure. The caller retains ownership of |ex| and can release it
2394 // independently of |x|.
2395 //
2396 // The new extension is inserted at index |loc|, shifting extensions to the
2397 // right. If |loc| is -1 or out of bounds, the new extension is appended to the
2398 // list.
2399 OPENSSL_EXPORT int X509_REVOKED_add_ext(X509_REVOKED *x,
2400                                         const X509_EXTENSION *ex, int loc);
2401 
2402 // X509_REVOKED_get_ext_d2i behaves like |X509V3_get_d2i| but looks for the
2403 // extension in |revoked|'s extension list.
2404 //
2405 // WARNING: This function is difficult to use correctly. See the documentation
2406 // for |X509V3_get_d2i| for details.
2407 OPENSSL_EXPORT void *X509_REVOKED_get_ext_d2i(const X509_REVOKED *revoked,
2408                                               int nid, int *out_critical,
2409                                               int *out_idx);
2410 
2411 // X509_REVOKED_add1_ext_i2d behaves like |X509V3_add1_i2d| but adds the
2412 // extension to |x|'s extension list.
2413 //
2414 // WARNING: This function may return zero or -1 on error. The caller must also
2415 // ensure |value|'s type matches |nid|. See the documentation for
2416 // |X509V3_add1_i2d| for details.
2417 OPENSSL_EXPORT int X509_REVOKED_add1_ext_i2d(X509_REVOKED *x, int nid,
2418                                              void *value, int crit,
2419                                              unsigned long flags);
2420 
2421 OPENSSL_EXPORT int X509_verify_cert(X509_STORE_CTX *ctx);
2422 
2423 // PKCS#8 utilities
2424 
2425 DECLARE_ASN1_FUNCTIONS_const(PKCS8_PRIV_KEY_INFO)
2426 
2427 // EVP_PKCS82PKEY returns |p8| as a newly-allocated |EVP_PKEY|, or NULL if the
2428 // key was unsupported or could not be decoded. If non-NULL, the caller must
2429 // release the result with |EVP_PKEY_free| when done.
2430 //
2431 // Use |EVP_parse_private_key| instead.
2432 OPENSSL_EXPORT EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8);
2433 
2434 // EVP_PKEY2PKCS8 encodes |pkey| as a PKCS#8 PrivateKeyInfo (RFC 5208),
2435 // represented as a newly-allocated |PKCS8_PRIV_KEY_INFO|, or NULL on error. The
2436 // caller must release the result with |PKCS8_PRIV_KEY_INFO_free| when done.
2437 //
2438 // Use |EVP_marshal_private_key| instead.
2439 OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(const EVP_PKEY *pkey);
2440 
2441 // X509_PUBKEY_set0_param sets |pub| to a key with AlgorithmIdentifier
2442 // determined by |obj|, |param_type|, and |param_value|, and an encoded
2443 // public key of |key|. On success, it takes ownership of all its parameters and
2444 // returns one. Otherwise, it returns zero. |key| must have been allocated by
2445 // |OPENSSL_malloc|.
2446 //
2447 // |obj|, |param_type|, and |param_value| are interpreted as in
2448 // |X509_ALGOR_set0|. See |X509_ALGOR_set0| for details.
2449 OPENSSL_EXPORT int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *obj,
2450                                           int param_type, void *param_value,
2451                                           uint8_t *key, int key_len);
2452 
2453 // X509_PUBKEY_get0_param outputs fields of |pub| and returns one. If |out_obj|
2454 // is not NULL, it sets |*out_obj| to AlgorithmIdentifier's OID. If |out_key|
2455 // is not NULL, it sets |*out_key| and |*out_key_len| to the encoded public key.
2456 // If |out_alg| is not NULL, it sets |*out_alg| to the AlgorithmIdentifier.
2457 //
2458 // Note: X.509 SubjectPublicKeyInfo structures store the encoded public key as a
2459 // BIT STRING. |*out_key| and |*out_key_len| will silently pad the key with zero
2460 // bits if |pub| did not contain a whole number of bytes. Use
2461 // |X509_PUBKEY_get0_public_key| to preserve this information.
2462 OPENSSL_EXPORT int X509_PUBKEY_get0_param(ASN1_OBJECT **out_obj,
2463                                           const uint8_t **out_key,
2464                                           int *out_key_len,
2465                                           X509_ALGOR **out_alg,
2466                                           X509_PUBKEY *pub);
2467 
2468 // X509_PUBKEY_get0_public_key returns |pub|'s encoded public key.
2469 OPENSSL_EXPORT const ASN1_BIT_STRING *X509_PUBKEY_get0_public_key(
2470     const X509_PUBKEY *pub);
2471 
2472 OPENSSL_EXPORT int X509_check_trust(X509 *x, int id, int flags);
2473 OPENSSL_EXPORT int X509_TRUST_get_count(void);
2474 OPENSSL_EXPORT X509_TRUST *X509_TRUST_get0(int idx);
2475 OPENSSL_EXPORT int X509_TRUST_get_by_id(int id);
2476 OPENSSL_EXPORT int X509_TRUST_add(int id, int flags,
2477                                   int (*ck)(X509_TRUST *, X509 *, int),
2478                                   char *name, int arg1, void *arg2);
2479 OPENSSL_EXPORT void X509_TRUST_cleanup(void);
2480 OPENSSL_EXPORT int X509_TRUST_get_flags(const X509_TRUST *xp);
2481 OPENSSL_EXPORT char *X509_TRUST_get0_name(const X509_TRUST *xp);
2482 OPENSSL_EXPORT int X509_TRUST_get_trust(const X509_TRUST *xp);
2483 
2484 
2485 struct rsa_pss_params_st {
2486   X509_ALGOR *hashAlgorithm;
2487   X509_ALGOR *maskGenAlgorithm;
2488   ASN1_INTEGER *saltLength;
2489   ASN1_INTEGER *trailerField;
2490   // OpenSSL caches the MGF hash on |RSA_PSS_PARAMS| in some cases. None of the
2491   // cases apply to BoringSSL, so this is always NULL, but Node expects the
2492   // field to be present.
2493   X509_ALGOR *maskHash;
2494 } /* RSA_PSS_PARAMS */;
2495 
2496 DECLARE_ASN1_FUNCTIONS_const(RSA_PSS_PARAMS)
2497 
2498 /*
2499 SSL_CTX -> X509_STORE
2500                 -> X509_LOOKUP
2501                         ->X509_LOOKUP_METHOD
2502                 -> X509_LOOKUP
2503                         ->X509_LOOKUP_METHOD
2504 
2505 SSL	-> X509_STORE_CTX
2506                 ->X509_STORE
2507 
2508 The X509_STORE holds the tables etc for verification stuff.
2509 A X509_STORE_CTX is used while validating a single certificate.
2510 The X509_STORE has X509_LOOKUPs for looking up certs.
2511 The X509_STORE then calls a function to actually verify the
2512 certificate chain.
2513 */
2514 
2515 #define X509_LU_X509 1
2516 #define X509_LU_CRL 2
2517 #define X509_LU_PKEY 3
2518 
2519 DEFINE_STACK_OF(X509_LOOKUP)
2520 DEFINE_STACK_OF(X509_OBJECT)
2521 DEFINE_STACK_OF(X509_VERIFY_PARAM)
2522 
2523 typedef int (*X509_STORE_CTX_verify_cb)(int, X509_STORE_CTX *);
2524 typedef int (*X509_STORE_CTX_verify_fn)(X509_STORE_CTX *);
2525 typedef int (*X509_STORE_CTX_get_issuer_fn)(X509 **issuer, X509_STORE_CTX *ctx,
2526                                             X509 *x);
2527 typedef int (*X509_STORE_CTX_check_issued_fn)(X509_STORE_CTX *ctx, X509 *x,
2528                                               X509 *issuer);
2529 typedef int (*X509_STORE_CTX_check_revocation_fn)(X509_STORE_CTX *ctx);
2530 typedef int (*X509_STORE_CTX_get_crl_fn)(X509_STORE_CTX *ctx, X509_CRL **crl,
2531                                          X509 *x);
2532 typedef int (*X509_STORE_CTX_check_crl_fn)(X509_STORE_CTX *ctx, X509_CRL *crl);
2533 typedef int (*X509_STORE_CTX_cert_crl_fn)(X509_STORE_CTX *ctx, X509_CRL *crl,
2534                                           X509 *x);
2535 typedef int (*X509_STORE_CTX_check_policy_fn)(X509_STORE_CTX *ctx);
2536 typedef STACK_OF(X509) *(*X509_STORE_CTX_lookup_certs_fn)(X509_STORE_CTX *ctx,
2537                                                           X509_NAME *nm);
2538 typedef STACK_OF(X509_CRL) *(*X509_STORE_CTX_lookup_crls_fn)(
2539     X509_STORE_CTX *ctx, X509_NAME *nm);
2540 typedef int (*X509_STORE_CTX_cleanup_fn)(X509_STORE_CTX *ctx);
2541 
2542 OPENSSL_EXPORT int X509_STORE_set_depth(X509_STORE *store, int depth);
2543 
2544 OPENSSL_EXPORT void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth);
2545 
2546 #define X509_STORE_CTX_set_app_data(ctx, data) \
2547   X509_STORE_CTX_set_ex_data(ctx, 0, data)
2548 #define X509_STORE_CTX_get_app_data(ctx) X509_STORE_CTX_get_ex_data(ctx, 0)
2549 
2550 #define X509_L_FILE_LOAD 1
2551 #define X509_L_ADD_DIR 2
2552 
2553 #define X509_LOOKUP_load_file(x, name, type) \
2554   X509_LOOKUP_ctrl((x), X509_L_FILE_LOAD, (name), (long)(type), NULL)
2555 
2556 #define X509_LOOKUP_add_dir(x, name, type) \
2557   X509_LOOKUP_ctrl((x), X509_L_ADD_DIR, (name), (long)(type), NULL)
2558 
2559 #define X509_V_OK 0
2560 #define X509_V_ERR_UNSPECIFIED 1
2561 
2562 #define X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT 2
2563 #define X509_V_ERR_UNABLE_TO_GET_CRL 3
2564 #define X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE 4
2565 #define X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE 5
2566 #define X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY 6
2567 #define X509_V_ERR_CERT_SIGNATURE_FAILURE 7
2568 #define X509_V_ERR_CRL_SIGNATURE_FAILURE 8
2569 #define X509_V_ERR_CERT_NOT_YET_VALID 9
2570 #define X509_V_ERR_CERT_HAS_EXPIRED 10
2571 #define X509_V_ERR_CRL_NOT_YET_VALID 11
2572 #define X509_V_ERR_CRL_HAS_EXPIRED 12
2573 #define X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD 13
2574 #define X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD 14
2575 #define X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD 15
2576 #define X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD 16
2577 #define X509_V_ERR_OUT_OF_MEM 17
2578 #define X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT 18
2579 #define X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN 19
2580 #define X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY 20
2581 #define X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE 21
2582 #define X509_V_ERR_CERT_CHAIN_TOO_LONG 22
2583 #define X509_V_ERR_CERT_REVOKED 23
2584 #define X509_V_ERR_INVALID_CA 24
2585 #define X509_V_ERR_PATH_LENGTH_EXCEEDED 25
2586 #define X509_V_ERR_INVALID_PURPOSE 26
2587 #define X509_V_ERR_CERT_UNTRUSTED 27
2588 #define X509_V_ERR_CERT_REJECTED 28
2589 // These are 'informational' when looking for issuer cert
2590 #define X509_V_ERR_SUBJECT_ISSUER_MISMATCH 29
2591 #define X509_V_ERR_AKID_SKID_MISMATCH 30
2592 #define X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH 31
2593 #define X509_V_ERR_KEYUSAGE_NO_CERTSIGN 32
2594 
2595 #define X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER 33
2596 #define X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION 34
2597 #define X509_V_ERR_KEYUSAGE_NO_CRL_SIGN 35
2598 #define X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION 36
2599 #define X509_V_ERR_INVALID_NON_CA 37
2600 #define X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED 38
2601 #define X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE 39
2602 #define X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED 40
2603 
2604 #define X509_V_ERR_INVALID_EXTENSION 41
2605 #define X509_V_ERR_INVALID_POLICY_EXTENSION 42
2606 #define X509_V_ERR_NO_EXPLICIT_POLICY 43
2607 #define X509_V_ERR_DIFFERENT_CRL_SCOPE 44
2608 #define X509_V_ERR_UNSUPPORTED_EXTENSION_FEATURE 45
2609 
2610 #define X509_V_ERR_UNNESTED_RESOURCE 46
2611 
2612 #define X509_V_ERR_PERMITTED_VIOLATION 47
2613 #define X509_V_ERR_EXCLUDED_VIOLATION 48
2614 #define X509_V_ERR_SUBTREE_MINMAX 49
2615 #define X509_V_ERR_APPLICATION_VERIFICATION 50
2616 #define X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE 51
2617 #define X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX 52
2618 #define X509_V_ERR_UNSUPPORTED_NAME_SYNTAX 53
2619 #define X509_V_ERR_CRL_PATH_VALIDATION_ERROR 54
2620 
2621 // Host, email and IP check errors
2622 #define X509_V_ERR_HOSTNAME_MISMATCH 62
2623 #define X509_V_ERR_EMAIL_MISMATCH 63
2624 #define X509_V_ERR_IP_ADDRESS_MISMATCH 64
2625 
2626 // Caller error
2627 #define X509_V_ERR_INVALID_CALL 65
2628 // Issuer lookup error
2629 #define X509_V_ERR_STORE_LOOKUP 66
2630 
2631 #define X509_V_ERR_NAME_CONSTRAINTS_WITHOUT_SANS 67
2632 
2633 // Certificate verify flags
2634 
2635 // Send issuer+subject checks to verify_cb
2636 #define X509_V_FLAG_CB_ISSUER_CHECK 0x1
2637 // Use check time instead of current time
2638 #define X509_V_FLAG_USE_CHECK_TIME 0x2
2639 // Lookup CRLs
2640 #define X509_V_FLAG_CRL_CHECK 0x4
2641 // Lookup CRLs for whole chain
2642 #define X509_V_FLAG_CRL_CHECK_ALL 0x8
2643 // Ignore unhandled critical extensions
2644 #define X509_V_FLAG_IGNORE_CRITICAL 0x10
2645 // Does nothing as its functionality has been enabled by default.
2646 #define X509_V_FLAG_X509_STRICT 0x00
2647 // This flag does nothing as proxy certificate support has been removed.
2648 #define X509_V_FLAG_ALLOW_PROXY_CERTS 0x40
2649 // Does nothing as its functionality has been enabled by default.
2650 #define X509_V_FLAG_POLICY_CHECK 0x80
2651 // Policy variable require-explicit-policy
2652 #define X509_V_FLAG_EXPLICIT_POLICY 0x100
2653 // Policy variable inhibit-any-policy
2654 #define X509_V_FLAG_INHIBIT_ANY 0x200
2655 // Policy variable inhibit-policy-mapping
2656 #define X509_V_FLAG_INHIBIT_MAP 0x400
2657 // Notify callback that policy is OK
2658 #define X509_V_FLAG_NOTIFY_POLICY 0x800
2659 // Extended CRL features such as indirect CRLs, alternate CRL signing keys
2660 #define X509_V_FLAG_EXTENDED_CRL_SUPPORT 0x1000
2661 // Delta CRL support
2662 #define X509_V_FLAG_USE_DELTAS 0x2000
2663 // Check selfsigned CA signature
2664 #define X509_V_FLAG_CHECK_SS_SIGNATURE 0x4000
2665 // Use trusted store first
2666 #define X509_V_FLAG_TRUSTED_FIRST 0x8000
2667 
2668 // Allow partial chains if at least one certificate is in trusted store
2669 #define X509_V_FLAG_PARTIAL_CHAIN 0x80000
2670 
2671 // If the initial chain is not trusted, do not attempt to build an alternative
2672 // chain. Alternate chain checking was introduced in 1.0.2b. Setting this flag
2673 // will force the behaviour to match that of previous versions.
2674 #define X509_V_FLAG_NO_ALT_CHAINS 0x100000
2675 
2676 // X509_V_FLAG_NO_CHECK_TIME disables all time checks in certificate
2677 // verification.
2678 #define X509_V_FLAG_NO_CHECK_TIME 0x200000
2679 
2680 #define X509_VP_FLAG_DEFAULT 0x1
2681 #define X509_VP_FLAG_OVERWRITE 0x2
2682 #define X509_VP_FLAG_RESET_FLAGS 0x4
2683 #define X509_VP_FLAG_LOCKED 0x8
2684 #define X509_VP_FLAG_ONCE 0x10
2685 
2686 OPENSSL_EXPORT int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h,
2687                                               int type, X509_NAME *name);
2688 OPENSSL_EXPORT X509_OBJECT *X509_OBJECT_retrieve_by_subject(
2689     STACK_OF(X509_OBJECT) *h, int type, X509_NAME *name);
2690 OPENSSL_EXPORT X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h,
2691                                                        X509_OBJECT *x);
2692 OPENSSL_EXPORT int X509_OBJECT_up_ref_count(X509_OBJECT *a);
2693 OPENSSL_EXPORT void X509_OBJECT_free_contents(X509_OBJECT *a);
2694 OPENSSL_EXPORT int X509_OBJECT_get_type(const X509_OBJECT *a);
2695 OPENSSL_EXPORT X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a);
2696 OPENSSL_EXPORT X509_STORE *X509_STORE_new(void);
2697 OPENSSL_EXPORT int X509_STORE_up_ref(X509_STORE *store);
2698 OPENSSL_EXPORT void X509_STORE_free(X509_STORE *v);
2699 
2700 OPENSSL_EXPORT STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *st);
2701 OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_get1_certs(X509_STORE_CTX *st,
2702                                                      X509_NAME *nm);
2703 OPENSSL_EXPORT STACK_OF(X509_CRL) *X509_STORE_get1_crls(X509_STORE_CTX *st,
2704                                                         X509_NAME *nm);
2705 OPENSSL_EXPORT int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags);
2706 OPENSSL_EXPORT int X509_STORE_set_purpose(X509_STORE *ctx, int purpose);
2707 OPENSSL_EXPORT int X509_STORE_set_trust(X509_STORE *ctx, int trust);
2708 OPENSSL_EXPORT int X509_STORE_set1_param(X509_STORE *ctx,
2709                                          X509_VERIFY_PARAM *pm);
2710 OPENSSL_EXPORT X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *ctx);
2711 
2712 OPENSSL_EXPORT void X509_STORE_set_verify(X509_STORE *ctx,
2713                                           X509_STORE_CTX_verify_fn verify);
2714 #define X509_STORE_set_verify_func(ctx, func) \
2715   X509_STORE_set_verify((ctx), (func))
2716 OPENSSL_EXPORT void X509_STORE_CTX_set_verify(X509_STORE_CTX *ctx,
2717                                               X509_STORE_CTX_verify_fn verify);
2718 OPENSSL_EXPORT X509_STORE_CTX_verify_fn X509_STORE_get_verify(X509_STORE *ctx);
2719 OPENSSL_EXPORT void X509_STORE_set_verify_cb(
2720     X509_STORE *ctx, X509_STORE_CTX_verify_cb verify_cb);
2721 #define X509_STORE_set_verify_cb_func(ctx, func) \
2722   X509_STORE_set_verify_cb((ctx), (func))
2723 OPENSSL_EXPORT X509_STORE_CTX_verify_cb
2724 X509_STORE_get_verify_cb(X509_STORE *ctx);
2725 OPENSSL_EXPORT void X509_STORE_set_get_issuer(
2726     X509_STORE *ctx, X509_STORE_CTX_get_issuer_fn get_issuer);
2727 OPENSSL_EXPORT X509_STORE_CTX_get_issuer_fn
2728 X509_STORE_get_get_issuer(X509_STORE *ctx);
2729 OPENSSL_EXPORT void X509_STORE_set_check_issued(
2730     X509_STORE *ctx, X509_STORE_CTX_check_issued_fn check_issued);
2731 OPENSSL_EXPORT X509_STORE_CTX_check_issued_fn
2732 X509_STORE_get_check_issued(X509_STORE *ctx);
2733 OPENSSL_EXPORT void X509_STORE_set_check_revocation(
2734     X509_STORE *ctx, X509_STORE_CTX_check_revocation_fn check_revocation);
2735 OPENSSL_EXPORT X509_STORE_CTX_check_revocation_fn
2736 X509_STORE_get_check_revocation(X509_STORE *ctx);
2737 OPENSSL_EXPORT void X509_STORE_set_get_crl(X509_STORE *ctx,
2738                                            X509_STORE_CTX_get_crl_fn get_crl);
2739 OPENSSL_EXPORT X509_STORE_CTX_get_crl_fn
2740 X509_STORE_get_get_crl(X509_STORE *ctx);
2741 OPENSSL_EXPORT void X509_STORE_set_check_crl(
2742     X509_STORE *ctx, X509_STORE_CTX_check_crl_fn check_crl);
2743 OPENSSL_EXPORT X509_STORE_CTX_check_crl_fn
2744 X509_STORE_get_check_crl(X509_STORE *ctx);
2745 OPENSSL_EXPORT void X509_STORE_set_cert_crl(
2746     X509_STORE *ctx, X509_STORE_CTX_cert_crl_fn cert_crl);
2747 OPENSSL_EXPORT X509_STORE_CTX_cert_crl_fn
2748 X509_STORE_get_cert_crl(X509_STORE *ctx);
2749 OPENSSL_EXPORT void X509_STORE_set_lookup_certs(
2750     X509_STORE *ctx, X509_STORE_CTX_lookup_certs_fn lookup_certs);
2751 OPENSSL_EXPORT X509_STORE_CTX_lookup_certs_fn
2752 X509_STORE_get_lookup_certs(X509_STORE *ctx);
2753 OPENSSL_EXPORT void X509_STORE_set_lookup_crls(
2754     X509_STORE *ctx, X509_STORE_CTX_lookup_crls_fn lookup_crls);
2755 #define X509_STORE_set_lookup_crls_cb(ctx, func) \
2756   X509_STORE_set_lookup_crls((ctx), (func))
2757 OPENSSL_EXPORT X509_STORE_CTX_lookup_crls_fn
2758 X509_STORE_get_lookup_crls(X509_STORE *ctx);
2759 OPENSSL_EXPORT void X509_STORE_set_cleanup(X509_STORE *ctx,
2760                                            X509_STORE_CTX_cleanup_fn cleanup);
2761 OPENSSL_EXPORT X509_STORE_CTX_cleanup_fn
2762 X509_STORE_get_cleanup(X509_STORE *ctx);
2763 
2764 OPENSSL_EXPORT X509_STORE_CTX *X509_STORE_CTX_new(void);
2765 
2766 OPENSSL_EXPORT int X509_STORE_CTX_get1_issuer(X509 **issuer,
2767                                               X509_STORE_CTX *ctx, X509 *x);
2768 
2769 OPENSSL_EXPORT void X509_STORE_CTX_zero(X509_STORE_CTX *ctx);
2770 OPENSSL_EXPORT void X509_STORE_CTX_free(X509_STORE_CTX *ctx);
2771 OPENSSL_EXPORT int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store,
2772                                        X509 *x509, STACK_OF(X509) *chain);
2773 
2774 // X509_STORE_CTX_set0_trusted_stack configures |ctx| to trust the certificates
2775 // in |sk|. |sk| must remain valid for the duration of |ctx|.
2776 //
2777 // WARNING: This function differs from most |set0| functions in that it does not
2778 // take ownership of its input. The caller is required to ensure the lifetimes
2779 // are consistent.
2780 OPENSSL_EXPORT void X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX *ctx,
2781                                                       STACK_OF(X509) *sk);
2782 
2783 // X509_STORE_CTX_trusted_stack is a deprecated alias for
2784 // |X509_STORE_CTX_set0_trusted_stack|.
2785 OPENSSL_EXPORT void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx,
2786                                                  STACK_OF(X509) *sk);
2787 
2788 OPENSSL_EXPORT void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx);
2789 
2790 OPENSSL_EXPORT X509_STORE *X509_STORE_CTX_get0_store(X509_STORE_CTX *ctx);
2791 OPENSSL_EXPORT X509 *X509_STORE_CTX_get0_cert(X509_STORE_CTX *ctx);
2792 
2793 OPENSSL_EXPORT X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *v,
2794                                                   X509_LOOKUP_METHOD *m);
2795 
2796 OPENSSL_EXPORT X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void);
2797 OPENSSL_EXPORT X509_LOOKUP_METHOD *X509_LOOKUP_file(void);
2798 
2799 OPENSSL_EXPORT int X509_STORE_add_cert(X509_STORE *ctx, X509 *x);
2800 OPENSSL_EXPORT int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x);
2801 
2802 OPENSSL_EXPORT int X509_STORE_get_by_subject(X509_STORE_CTX *vs, int type,
2803                                              X509_NAME *name, X509_OBJECT *ret);
2804 
2805 OPENSSL_EXPORT int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc,
2806                                     long argl, char **ret);
2807 
2808 #ifndef OPENSSL_NO_STDIO
2809 OPENSSL_EXPORT int X509_load_cert_file(X509_LOOKUP *ctx, const char *file,
2810                                        int type);
2811 OPENSSL_EXPORT int X509_load_crl_file(X509_LOOKUP *ctx, const char *file,
2812                                       int type);
2813 OPENSSL_EXPORT int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file,
2814                                            int type);
2815 #endif
2816 
2817 OPENSSL_EXPORT X509_LOOKUP *X509_LOOKUP_new(X509_LOOKUP_METHOD *method);
2818 OPENSSL_EXPORT void X509_LOOKUP_free(X509_LOOKUP *ctx);
2819 OPENSSL_EXPORT int X509_LOOKUP_init(X509_LOOKUP *ctx);
2820 OPENSSL_EXPORT int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type,
2821                                           X509_NAME *name, X509_OBJECT *ret);
2822 OPENSSL_EXPORT int X509_LOOKUP_shutdown(X509_LOOKUP *ctx);
2823 
2824 #ifndef OPENSSL_NO_STDIO
2825 OPENSSL_EXPORT int X509_STORE_load_locations(X509_STORE *ctx, const char *file,
2826                                              const char *dir);
2827 OPENSSL_EXPORT int X509_STORE_set_default_paths(X509_STORE *ctx);
2828 #endif
2829 OPENSSL_EXPORT int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx);
2830 OPENSSL_EXPORT void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int s);
2831 OPENSSL_EXPORT int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx);
2832 OPENSSL_EXPORT X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx);
2833 OPENSSL_EXPORT X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx);
2834 OPENSSL_EXPORT X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx);
2835 OPENSSL_EXPORT X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(
2836     X509_STORE_CTX *ctx);
2837 OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx);
2838 OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get0_chain(X509_STORE_CTX *ctx);
2839 OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx);
2840 OPENSSL_EXPORT void X509_STORE_CTX_set_cert(X509_STORE_CTX *c, X509 *x);
2841 OPENSSL_EXPORT void X509_STORE_CTX_set_chain(X509_STORE_CTX *c,
2842                                              STACK_OF(X509) *sk);
2843 OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get0_untrusted(
2844     X509_STORE_CTX *ctx);
2845 OPENSSL_EXPORT void X509_STORE_CTX_set0_crls(X509_STORE_CTX *c,
2846                                              STACK_OF(X509_CRL) *sk);
2847 OPENSSL_EXPORT int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose);
2848 OPENSSL_EXPORT int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust);
2849 OPENSSL_EXPORT int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx,
2850                                                   int def_purpose, int purpose,
2851                                                   int trust);
2852 OPENSSL_EXPORT void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx,
2853                                              unsigned long flags);
2854 OPENSSL_EXPORT void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx,
2855                                             unsigned long flags, time_t t);
2856 OPENSSL_EXPORT void X509_STORE_CTX_set_time_posix(X509_STORE_CTX *ctx,
2857                                                   unsigned long flags,
2858                                                   int64_t t);
2859 OPENSSL_EXPORT void X509_STORE_CTX_set_verify_cb(
2860     X509_STORE_CTX *ctx, int (*verify_cb)(int, X509_STORE_CTX *));
2861 
2862 OPENSSL_EXPORT X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(
2863     X509_STORE_CTX *ctx);
2864 OPENSSL_EXPORT void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx,
2865                                               X509_VERIFY_PARAM *param);
2866 OPENSSL_EXPORT int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx,
2867                                               const char *name);
2868 
2869 // X509_VERIFY_PARAM functions
2870 
2871 OPENSSL_EXPORT X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void);
2872 OPENSSL_EXPORT void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param);
2873 OPENSSL_EXPORT int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *to,
2874                                              const X509_VERIFY_PARAM *from);
2875 OPENSSL_EXPORT int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to,
2876                                           const X509_VERIFY_PARAM *from);
2877 OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param,
2878                                                const char *name);
2879 OPENSSL_EXPORT int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param,
2880                                                unsigned long flags);
2881 OPENSSL_EXPORT int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param,
2882                                                  unsigned long flags);
2883 OPENSSL_EXPORT unsigned long X509_VERIFY_PARAM_get_flags(
2884     X509_VERIFY_PARAM *param);
2885 OPENSSL_EXPORT int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param,
2886                                                  int purpose);
2887 OPENSSL_EXPORT int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param,
2888                                                int trust);
2889 OPENSSL_EXPORT void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param,
2890                                                 int depth);
2891 OPENSSL_EXPORT void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param,
2892                                                time_t t);
2893 OPENSSL_EXPORT void X509_VERIFY_PARAM_set_time_posix(X509_VERIFY_PARAM *param,
2894                                                      int64_t t);
2895 OPENSSL_EXPORT int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param,
2896                                                  ASN1_OBJECT *policy);
2897 OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_policies(
2898     X509_VERIFY_PARAM *param, const STACK_OF(ASN1_OBJECT) *policies);
2899 
2900 OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param,
2901                                                const char *name,
2902                                                size_t namelen);
2903 OPENSSL_EXPORT int X509_VERIFY_PARAM_add1_host(X509_VERIFY_PARAM *param,
2904                                                const char *name,
2905                                                size_t namelen);
2906 OPENSSL_EXPORT void X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param,
2907                                                     unsigned int flags);
2908 OPENSSL_EXPORT char *X509_VERIFY_PARAM_get0_peername(X509_VERIFY_PARAM *);
2909 OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *param,
2910                                                 const char *email,
2911                                                 size_t emaillen);
2912 OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *param,
2913                                              const unsigned char *ip,
2914                                              size_t iplen);
2915 OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param,
2916                                                  const char *ipasc);
2917 
2918 OPENSSL_EXPORT int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param);
2919 OPENSSL_EXPORT const char *X509_VERIFY_PARAM_get0_name(
2920     const X509_VERIFY_PARAM *param);
2921 
2922 OPENSSL_EXPORT const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(
2923     const char *name);
2924 
2925 
2926 #if defined(__cplusplus)
2927 }  // extern C
2928 #endif
2929 
2930 #if !defined(BORINGSSL_NO_CXX)
2931 extern "C++" {
2932 
2933 BSSL_NAMESPACE_BEGIN
2934 
2935 BORINGSSL_MAKE_DELETER(NETSCAPE_SPKI, NETSCAPE_SPKI_free)
2936 BORINGSSL_MAKE_DELETER(RSA_PSS_PARAMS, RSA_PSS_PARAMS_free)
2937 BORINGSSL_MAKE_DELETER(X509, X509_free)
2938 BORINGSSL_MAKE_UP_REF(X509, X509_up_ref)
2939 BORINGSSL_MAKE_DELETER(X509_ALGOR, X509_ALGOR_free)
2940 BORINGSSL_MAKE_DELETER(X509_ATTRIBUTE, X509_ATTRIBUTE_free)
2941 BORINGSSL_MAKE_DELETER(X509_CRL, X509_CRL_free)
2942 BORINGSSL_MAKE_UP_REF(X509_CRL, X509_CRL_up_ref)
2943 BORINGSSL_MAKE_DELETER(X509_EXTENSION, X509_EXTENSION_free)
2944 BORINGSSL_MAKE_DELETER(X509_INFO, X509_INFO_free)
2945 BORINGSSL_MAKE_DELETER(X509_LOOKUP, X509_LOOKUP_free)
2946 BORINGSSL_MAKE_DELETER(X509_NAME, X509_NAME_free)
2947 BORINGSSL_MAKE_DELETER(X509_NAME_ENTRY, X509_NAME_ENTRY_free)
2948 BORINGSSL_MAKE_DELETER(X509_PKEY, X509_PKEY_free)
2949 BORINGSSL_MAKE_DELETER(X509_PUBKEY, X509_PUBKEY_free)
2950 BORINGSSL_MAKE_DELETER(X509_REQ, X509_REQ_free)
2951 BORINGSSL_MAKE_DELETER(X509_REVOKED, X509_REVOKED_free)
2952 BORINGSSL_MAKE_DELETER(X509_SIG, X509_SIG_free)
2953 BORINGSSL_MAKE_DELETER(X509_STORE, X509_STORE_free)
2954 BORINGSSL_MAKE_UP_REF(X509_STORE, X509_STORE_up_ref)
2955 BORINGSSL_MAKE_DELETER(X509_STORE_CTX, X509_STORE_CTX_free)
2956 BORINGSSL_MAKE_DELETER(X509_VERIFY_PARAM, X509_VERIFY_PARAM_free)
2957 
2958 BSSL_NAMESPACE_END
2959 
2960 }  // extern C++
2961 #endif  // !BORINGSSL_NO_CXX
2962 
2963 #define X509_R_AKID_MISMATCH 100
2964 #define X509_R_BAD_PKCS7_VERSION 101
2965 #define X509_R_BAD_X509_FILETYPE 102
2966 #define X509_R_BASE64_DECODE_ERROR 103
2967 #define X509_R_CANT_CHECK_DH_KEY 104
2968 #define X509_R_CERT_ALREADY_IN_HASH_TABLE 105
2969 #define X509_R_CRL_ALREADY_DELTA 106
2970 #define X509_R_CRL_VERIFY_FAILURE 107
2971 #define X509_R_IDP_MISMATCH 108
2972 #define X509_R_INVALID_BIT_STRING_BITS_LEFT 109
2973 #define X509_R_INVALID_DIRECTORY 110
2974 #define X509_R_INVALID_FIELD_NAME 111
2975 #define X509_R_INVALID_PSS_PARAMETERS 112
2976 #define X509_R_INVALID_TRUST 113
2977 #define X509_R_ISSUER_MISMATCH 114
2978 #define X509_R_KEY_TYPE_MISMATCH 115
2979 #define X509_R_KEY_VALUES_MISMATCH 116
2980 #define X509_R_LOADING_CERT_DIR 117
2981 #define X509_R_LOADING_DEFAULTS 118
2982 #define X509_R_NEWER_CRL_NOT_NEWER 119
2983 #define X509_R_NOT_PKCS7_SIGNED_DATA 120
2984 #define X509_R_NO_CERTIFICATES_INCLUDED 121
2985 #define X509_R_NO_CERT_SET_FOR_US_TO_VERIFY 122
2986 #define X509_R_NO_CRLS_INCLUDED 123
2987 #define X509_R_NO_CRL_NUMBER 124
2988 #define X509_R_PUBLIC_KEY_DECODE_ERROR 125
2989 #define X509_R_PUBLIC_KEY_ENCODE_ERROR 126
2990 #define X509_R_SHOULD_RETRY 127
2991 #define X509_R_UNKNOWN_KEY_TYPE 128
2992 #define X509_R_UNKNOWN_NID 129
2993 #define X509_R_UNKNOWN_PURPOSE_ID 130
2994 #define X509_R_UNKNOWN_TRUST_ID 131
2995 #define X509_R_UNSUPPORTED_ALGORITHM 132
2996 #define X509_R_WRONG_LOOKUP_TYPE 133
2997 #define X509_R_WRONG_TYPE 134
2998 #define X509_R_NAME_TOO_LONG 135
2999 #define X509_R_INVALID_PARAMETER 136
3000 #define X509_R_SIGNATURE_ALGORITHM_MISMATCH 137
3001 #define X509_R_DELTA_CRL_WITHOUT_CRL_NUMBER 138
3002 #define X509_R_INVALID_FIELD_FOR_VERSION 139
3003 #define X509_R_INVALID_VERSION 140
3004 #define X509_R_NO_CERTIFICATE_FOUND 141
3005 #define X509_R_NO_CERTIFICATE_OR_CRL_FOUND 142
3006 #define X509_R_NO_CRL_FOUND 143
3007 #define X509_R_INVALID_POLICY_EXTENSION 144
3008 
3009 #endif  // OPENSSL_HEADER_X509_H
3010