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