gnunet-android

GNUnet for Android
Log | Files | Refs | README

x509.h (265626B)


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