blob: 099d5ef8219a898bf4605fdaa00e39e70898bdfa [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
David Benjamin991d31b2020-06-19 11:38:20 -04007 *
Adam Langley95c29f32014-06-20 12:00:00 -07008 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
David Benjamin991d31b2020-06-19 11:38:20 -040014 *
Adam Langley95c29f32014-06-20 12:00:00 -070015 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
David Benjamin991d31b2020-06-19 11:38:20 -040021 *
Adam Langley95c29f32014-06-20 12:00:00 -070022 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
David Benjamin991d31b2020-06-19 11:38:20 -040036 * 4. If you include any Windows specific code (or a derivative thereof) from
Adam Langley95c29f32014-06-20 12:00:00 -070037 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
David Benjamin991d31b2020-06-19 11:38:20 -040039 *
Adam Langley95c29f32014-06-20 12:00:00 -070040 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
David Benjamin991d31b2020-06-19 11:38:20 -040051 *
Adam Langley95c29f32014-06-20 12:00:00 -070052 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
David Benjamin991d31b2020-06-19 11:38:20 -040059 * ECDH support in OpenSSL originally developed by
Adam Langley95c29f32014-06-20 12:00:00 -070060 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
61 */
62
David Benjamina1c79222023-01-02 09:55:09 -080063#ifndef OPENSSL_HEADER_X509_H
64#define OPENSSL_HEADER_X509_H
Adam Langley95c29f32014-06-20 12:00:00 -070065
David Benjamin991d31b2020-06-19 11:38:20 -040066#include <openssl/base.h>
David Benjamin58906ea2023-11-20 23:59:49 -050067
68#include <time.h>
69
70#include <openssl/asn1.h>
Adam Langley95c29f32014-06-20 12:00:00 -070071#include <openssl/bio.h>
72#include <openssl/cipher.h>
David Benjamin58906ea2023-11-20 23:59:49 -050073#include <openssl/conf.h>
Adam Langley95c29f32014-06-20 12:00:00 -070074#include <openssl/dh.h>
75#include <openssl/dsa.h>
David Benjamin991d31b2020-06-19 11:38:20 -040076#include <openssl/ec.h>
Adam Langley95c29f32014-06-20 12:00:00 -070077#include <openssl/ecdh.h>
78#include <openssl/ecdsa.h>
79#include <openssl/evp.h>
David Benjamin58906ea2023-11-20 23:59:49 -050080#include <openssl/lhash.h>
David Benjamin98193672016-03-25 18:07:11 -040081#include <openssl/obj.h>
David Benjamin6fdea2a2017-04-15 18:40:41 -040082#include <openssl/pkcs7.h>
Adam Langley48983312016-10-28 11:42:45 -070083#include <openssl/pool.h>
Adam Langley95c29f32014-06-20 12:00:00 -070084#include <openssl/rsa.h>
85#include <openssl/sha.h>
86#include <openssl/stack.h>
Adam Langley0da323a2015-05-15 12:49:30 -070087#include <openssl/thread.h>
David Benjamin58906ea2023-11-20 23:59:49 -050088#include <openssl/x509v3_errors.h> // IWYU pragma: export
Adam Langley95c29f32014-06-20 12:00:00 -070089
David Benjamin8a5ec722021-10-15 13:17:40 -040090#if defined(__cplusplus)
Adam Langley95c29f32014-06-20 12:00:00 -070091extern "C" {
92#endif
93
94
David Benjamin991d31b2020-06-19 11:38:20 -040095// Legacy X.509 library.
96//
97// This header is part of OpenSSL's X.509 implementation. It is retained for
David Benjamin4363bdd2022-04-06 15:19:16 -040098// compatibility but should not be used by new code. The functions are difficult
99// to use correctly, and have buggy or non-standard behaviors. They are thus
100// particularly prone to behavior changes and API removals, as BoringSSL
101// iterates on these issues.
102//
103// In the future, a replacement library will be available. Meanwhile, minimize
David Benjamin991d31b2020-06-19 11:38:20 -0400104// dependencies on this header where possible.
David Benjamin4363bdd2022-04-06 15:19:16 -0400105//
106// TODO(https://crbug.com/boringssl/426): Documentation for this library is
107// still in progress. Some functions have not yet been documented, and some
108// functions have not yet been grouped into sections.
David Benjamin5e61d532017-01-05 21:01:49 -0500109
110
David Benjamin4363bdd2022-04-06 15:19:16 -0400111// Certificates.
112//
113// An |X509| object represents an X.509 certificate, defined in RFC 5280.
114//
115// Although an |X509| is a mutable object, mutating an |X509| can give incorrect
116// results. Callers typically obtain |X509|s by parsing some input with
117// |d2i_X509|, etc. Such objects carry information such as the serialized
118// TBSCertificate and decoded extensions, which will become inconsistent when
119// mutated.
120//
121// Instead, mutation functions should only be used when issuing new
122// certificates, as described in a later section.
Adam Langley95c29f32014-06-20 12:00:00 -0700123
David Benjamin4363bdd2022-04-06 15:19:16 -0400124DEFINE_STACK_OF(X509)
Adam Langley95c29f32014-06-20 12:00:00 -0700125
David Benjaminfaac6232023-12-16 21:07:46 -0500126// X509 is an |ASN1_ITEM| whose ASN.1 type is X.509 Certificate (RFC 5280) and C
127// type is |X509*|.
128DECLARE_ASN1_ITEM(X509)
129
David Benjamin4363bdd2022-04-06 15:19:16 -0400130// X509_up_ref adds one to the reference count of |x509| and returns one.
131OPENSSL_EXPORT int X509_up_ref(X509 *x509);
132
133// X509_chain_up_ref returns a newly-allocated |STACK_OF(X509)| containing a
134// shallow copy of |chain|, or NULL on error. That is, the return value has the
135// same contents as |chain|, and each |X509|'s reference count is incremented by
136// one.
137OPENSSL_EXPORT STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain);
138
David Benjaminb8596422022-07-10 01:08:07 -0400139// X509_dup returns a newly-allocated copy of |x509|, or NULL on error. This
140// function works by serializing the structure, so auxiliary properties (see
141// |i2d_X509_AUX|) are not preserved. Additionally, if |x509| is incomplete,
142// this function may fail.
143//
144// TODO(https://crbug.com/boringssl/407): This function should be const and
145// thread-safe but is currently neither in some cases, notably if |crl| was
146// mutated.
147OPENSSL_EXPORT X509 *X509_dup(X509 *x509);
148
David Benjamin4363bdd2022-04-06 15:19:16 -0400149// X509_free decrements |x509|'s reference count and, if zero, releases memory
150// associated with |x509|.
151OPENSSL_EXPORT void X509_free(X509 *x509);
152
153// d2i_X509 parses up to |len| bytes from |*inp| as a DER-encoded X.509
David Benjamin19721cd2023-01-16 16:19:03 -0500154// Certificate (RFC 5280), as described in |d2i_SAMPLE|.
David Benjamin4363bdd2022-04-06 15:19:16 -0400155OPENSSL_EXPORT X509 *d2i_X509(X509 **out, const uint8_t **inp, long len);
156
157// X509_parse_from_buffer parses an X.509 structure from |buf| and returns a
158// fresh X509 or NULL on error. There must not be any trailing data in |buf|.
159// The returned structure (if any) holds a reference to |buf| rather than
160// copying parts of it as a normal |d2i_X509| call would do.
161OPENSSL_EXPORT X509 *X509_parse_from_buffer(CRYPTO_BUFFER *buf);
162
163// i2d_X509 marshals |x509| as a DER-encoded X.509 Certificate (RFC 5280), as
164// described in |i2d_SAMPLE|.
165//
166// TODO(https://crbug.com/boringssl/407): This function should be const and
167// thread-safe but is currently neither in some cases, notably if |x509| was
168// mutated.
169OPENSSL_EXPORT int i2d_X509(X509 *x509, uint8_t **outp);
170
171// X509_VERSION_* are X.509 version numbers. Note the numerical values of all
172// defined X.509 versions are one less than the named version.
173#define X509_VERSION_1 0
174#define X509_VERSION_2 1
175#define X509_VERSION_3 2
176
177// X509_get_version returns the numerical value of |x509|'s version, which will
178// be one of the |X509_VERSION_*| constants.
179OPENSSL_EXPORT long X509_get_version(const X509 *x509);
180
181// X509_get0_serialNumber returns |x509|'s serial number.
182OPENSSL_EXPORT const ASN1_INTEGER *X509_get0_serialNumber(const X509 *x509);
183
184// X509_get0_notBefore returns |x509|'s notBefore time.
185OPENSSL_EXPORT const ASN1_TIME *X509_get0_notBefore(const X509 *x509);
186
187// X509_get0_notAfter returns |x509|'s notAfter time.
188OPENSSL_EXPORT const ASN1_TIME *X509_get0_notAfter(const X509 *x509);
189
190// X509_get_issuer_name returns |x509|'s issuer.
191OPENSSL_EXPORT X509_NAME *X509_get_issuer_name(const X509 *x509);
192
193// X509_get_subject_name returns |x509|'s subject.
194OPENSSL_EXPORT X509_NAME *X509_get_subject_name(const X509 *x509);
195
196// X509_get_X509_PUBKEY returns the public key of |x509|. Note this function is
197// not const-correct for legacy reasons. Callers should not modify the returned
198// object.
199OPENSSL_EXPORT X509_PUBKEY *X509_get_X509_PUBKEY(const X509 *x509);
200
David Benjamin5b3dc492023-12-21 19:19:16 -0500201// X509_get0_pubkey returns |x509|'s public key as an |EVP_PKEY|, or NULL if the
202// public key was unsupported or could not be decoded. The |EVP_PKEY| is cached
203// in |x509|, so callers must not mutate the result.
204OPENSSL_EXPORT EVP_PKEY *X509_get0_pubkey(const X509 *x509);
205
206// X509_get_pubkey behaves like |X509_get0_pubkey| but increments the reference
207// count on the |EVP_PKEY|. The caller must release the result with
208// |EVP_PKEY_free| when done. The |EVP_PKEY| is cached in |x509|, so callers
209// must not mutate the result.
210OPENSSL_EXPORT EVP_PKEY *X509_get_pubkey(const X509 *x509);
David Benjamin4363bdd2022-04-06 15:19:16 -0400211
212// X509_get0_pubkey_bitstr returns the BIT STRING portion of |x509|'s public
213// key. Note this does not contain the AlgorithmIdentifier portion.
214//
215// WARNING: This function returns a non-const pointer for OpenSSL compatibility,
216// but the caller must not modify the resulting object. Doing so will break
217// internal invariants in |x509|.
218OPENSSL_EXPORT ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x509);
219
David Benjamin2ba76342023-11-19 10:20:04 -0500220// X509_check_private_key returns one if |x509|'s public key matches |pkey| and
221// zero otherwise.
David Benjaminc1c7f092023-12-21 19:27:31 -0500222OPENSSL_EXPORT int X509_check_private_key(const X509 *x509,
223 const EVP_PKEY *pkey);
David Benjamin2ba76342023-11-19 10:20:04 -0500224
David Benjamin4363bdd2022-04-06 15:19:16 -0400225// X509_get0_uids sets |*out_issuer_uid| to a non-owning pointer to the
226// issuerUID field of |x509|, or NULL if |x509| has no issuerUID. It similarly
227// outputs |x509|'s subjectUID field to |*out_subject_uid|.
228//
229// Callers may pass NULL to either |out_issuer_uid| or |out_subject_uid| to
230// ignore the corresponding field.
231OPENSSL_EXPORT void X509_get0_uids(const X509 *x509,
232 const ASN1_BIT_STRING **out_issuer_uid,
233 const ASN1_BIT_STRING **out_subject_uid);
234
David Benjamin314c2522023-11-27 23:26:09 -0500235// The following bits are returned from |X509_get_extension_flags|.
236
237// EXFLAG_BCONS indicates the certificate has a basic constraints extension.
238#define EXFLAG_BCONS 0x1
239// EXFLAG_KUSAGE indicates the certifcate has a key usage extension.
240#define EXFLAG_KUSAGE 0x2
241// EXFLAG_XKUSAGE indicates the certifcate has an extended key usage extension.
242#define EXFLAG_XKUSAGE 0x4
243// EXFLAG_NSCERT indicates the certificate has a legacy Netscape certificate
244// type extension.
245#define EXFLAG_NSCERT 0x8
246// EXFLAG_CA indicates the certificate has a basic constraints extension with
247// the CA bit set.
248#define EXFLAG_CA 0x10
249// EXFLAG_SI indicates the certificate is self-issued, i.e. its subject and
250// issuer names match.
251#define EXFLAG_SI 0x20
252// EXFLAG_V1 indicates an X.509v1 certificate.
253#define EXFLAG_V1 0x40
254// EXFLAG_INVALID indicates an error processing some extension. The certificate
255// should not be accepted. Note the lack of this bit does not imply all
256// extensions are valid, only those used to compute extension flags.
257#define EXFLAG_INVALID 0x80
258// EXFLAG_SET is an internal bit that indicates extension flags were computed.
259#define EXFLAG_SET 0x100
260// EXFLAG_CRITICAL indicates an unsupported critical extension. The certificate
261// should not be accepted.
262#define EXFLAG_CRITICAL 0x200
263// EXFLAG_SS indicates the certificate is likely self-signed. That is, if it is
264// self-issued, its authority key identifer (if any) matches itself, and its key
265// usage extension (if any) allows certificate signatures. The signature itself
266// is not checked in computing this bit.
267#define EXFLAG_SS 0x2000
268
269// X509_get_extension_flags decodes a set of extensions from |x509| and returns
270// a collection of |EXFLAG_*| bits which reflect |x509|. If there was an error
271// in computing this bitmask, the result will include the |EXFLAG_INVALID| bit.
272OPENSSL_EXPORT uint32_t X509_get_extension_flags(X509 *x509);
273
David Benjamindd8ffe12023-11-12 12:39:39 -0500274// X509_get_pathlen returns path length constraint from the basic constraints
275// extension in |x509|. (See RFC 5280, section 4.2.1.9.) It returns -1 if the
276// constraint is not present, or if some extension in |x509| was invalid.
277//
278// TODO(crbug.com/boringssl/381): Decoding an |X509| object will not check for
279// invalid extensions. To detect the error case, call
David Benjamin2a88b4b2023-12-25 16:16:32 -0500280// |X509_get_extension_flags| and check the |EXFLAG_INVALID| bit.
David Benjamindd8ffe12023-11-12 12:39:39 -0500281OPENSSL_EXPORT long X509_get_pathlen(X509 *x509);
282
David Benjamin314c2522023-11-27 23:26:09 -0500283// X509v3_KU_* are key usage bits returned from |X509_get_key_usage|.
284#define X509v3_KU_DIGITAL_SIGNATURE 0x0080
285#define X509v3_KU_NON_REPUDIATION 0x0040
286#define X509v3_KU_KEY_ENCIPHERMENT 0x0020
287#define X509v3_KU_DATA_ENCIPHERMENT 0x0010
288#define X509v3_KU_KEY_AGREEMENT 0x0008
289#define X509v3_KU_KEY_CERT_SIGN 0x0004
290#define X509v3_KU_CRL_SIGN 0x0002
291#define X509v3_KU_ENCIPHER_ONLY 0x0001
292#define X509v3_KU_DECIPHER_ONLY 0x8000
293
294// X509_get_key_usage returns a bitmask of key usages (see Section 4.2.1.3 of
295// RFC 5280) which |x509| is valid for. This function only reports the first 16
296// bits, in a little-endian byte order, but big-endian bit order. That is, bits
297// 0 though 7 are reported at 1<<7 through 1<<0, and bits 8 through 15 are
298// reported at 1<<15 through 1<<8.
299//
300// Instead of depending on this bit order, callers should compare against the
301// |X509v3_KU_*| constants.
302//
303// If |x509| has no key usage extension, all key usages are valid and this
304// function returns |UINT32_MAX|. If there was an error processing |x509|'s
305// extensions, or if the first 16 bits in the key usage extension were all zero,
306// this function returns zero.
307OPENSSL_EXPORT uint32_t X509_get_key_usage(X509 *x509);
308
309// XKU_* are extended key usage bits returned from
310// |X509_get_extended_key_usage|.
311#define XKU_SSL_SERVER 0x1
312#define XKU_SSL_CLIENT 0x2
313#define XKU_SMIME 0x4
314#define XKU_CODE_SIGN 0x8
315#define XKU_SGC 0x10
316#define XKU_OCSP_SIGN 0x20
317#define XKU_TIMESTAMP 0x40
318#define XKU_DVCS 0x80
319#define XKU_ANYEKU 0x100
320
321// X509_get_extended_key_usage returns a bitmask of extended key usages (see
322// Section 4.2.1.12 of RFC 5280) which |x509| is valid for. The result will be
323// a combination of |XKU_*| constants. If checking an extended key usage not
324// defined above, callers should extract the extended key usage extension
325// separately, e.g. via |X509_get_ext_d2i|.
326//
327// If |x509| has no extended key usage extension, all extended key usages are
328// valid and this function returns |UINT32_MAX|. If there was an error
329// processing |x509|'s extensions, or if |x509|'s extended key usage extension
330// contained no recognized usages, this function returns zero.
331OPENSSL_EXPORT uint32_t X509_get_extended_key_usage(X509 *x509);
332
333// X509_get0_subject_key_id returns |x509|'s subject key identifier, if present.
334// (See RFC 5280, section 4.2.1.2.) It returns NULL if the extension is not
335// present or if some extension in |x509| was invalid.
336//
337// TODO(crbug.com/boringssl/381): Decoding an |X509| object will not check for
338// invalid extensions. To detect the error case, call
David Benjamin2a88b4b2023-12-25 16:16:32 -0500339// |X509_get_extension_flags| and check the |EXFLAG_INVALID| bit.
David Benjamin314c2522023-11-27 23:26:09 -0500340OPENSSL_EXPORT const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x509);
341
342// X509_get0_authority_key_id returns keyIdentifier of |x509|'s authority key
343// identifier, if the extension and field are present. (See RFC 5280,
344// section 4.2.1.1.) It returns NULL if the extension is not present, if it is
345// present but lacks a keyIdentifier field, or if some extension in |x509| was
346// invalid.
347//
348// TODO(crbug.com/boringssl/381): Decoding an |X509| object will not check for
349// invalid extensions. To detect the error case, call
David Benjamin2a88b4b2023-12-25 16:16:32 -0500350// |X509_get_extension_flags| and check the |EXFLAG_INVALID| bit.
David Benjamin314c2522023-11-27 23:26:09 -0500351OPENSSL_EXPORT const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x509);
352
353DEFINE_STACK_OF(GENERAL_NAME)
354typedef STACK_OF(GENERAL_NAME) GENERAL_NAMES;
355
356// X509_get0_authority_issuer returns the authorityCertIssuer of |x509|'s
357// authority key identifier, if the extension and field are present. (See
358// RFC 5280, section 4.2.1.1.) It returns NULL if the extension is not present,
359// if it is present but lacks a authorityCertIssuer field, or if some extension
360// in |x509| was invalid.
361//
362// TODO(crbug.com/boringssl/381): Decoding an |X509| object will not check for
363// invalid extensions. To detect the error case, call
David Benjamin2a88b4b2023-12-25 16:16:32 -0500364// |X509_get_extension_flags| and check the |EXFLAG_INVALID| bit.
David Benjamin314c2522023-11-27 23:26:09 -0500365OPENSSL_EXPORT const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x509);
366
367// X509_get0_authority_serial returns the authorityCertSerialNumber of |x509|'s
368// authority key identifier, if the extension and field are present. (See
369// RFC 5280, section 4.2.1.1.) It returns NULL if the extension is not present,
370// if it is present but lacks a authorityCertSerialNumber field, or if some
371// extension in |x509| was invalid.
372//
373// TODO(crbug.com/boringssl/381): Decoding an |X509| object will not check for
374// invalid extensions. To detect the error case, call
David Benjamin2a88b4b2023-12-25 16:16:32 -0500375// |X509_get_extension_flags| and check the |EXFLAG_INVALID| bit.
David Benjamin314c2522023-11-27 23:26:09 -0500376OPENSSL_EXPORT const ASN1_INTEGER *X509_get0_authority_serial(X509 *x509);
377
David Benjamin4363bdd2022-04-06 15:19:16 -0400378// X509_get0_extensions returns |x509|'s extension list, or NULL if |x509| omits
379// it.
380OPENSSL_EXPORT const STACK_OF(X509_EXTENSION) *X509_get0_extensions(
381 const X509 *x509);
382
David Benjamin557b80f2022-07-10 19:12:12 -0400383// X509_get_ext_count returns the number of extensions in |x|.
384OPENSSL_EXPORT int X509_get_ext_count(const X509 *x);
385
386// X509_get_ext_by_NID behaves like |X509v3_get_ext_by_NID| but searches for
387// extensions in |x|.
388OPENSSL_EXPORT int X509_get_ext_by_NID(const X509 *x, int nid, int lastpos);
389
390// X509_get_ext_by_OBJ behaves like |X509v3_get_ext_by_OBJ| but searches for
391// extensions in |x|.
392OPENSSL_EXPORT int X509_get_ext_by_OBJ(const X509 *x, const ASN1_OBJECT *obj,
393 int lastpos);
394
395// X509_get_ext_by_critical behaves like |X509v3_get_ext_by_critical| but
396// searches for extensions in |x|.
397OPENSSL_EXPORT int X509_get_ext_by_critical(const X509 *x, int crit,
398 int lastpos);
399
400// X509_get_ext returns the extension in |x| at index |loc|, or NULL if |loc| is
401// out of bounds. This function returns a non-const pointer for OpenSSL
402// compatibility, but callers should not mutate the result.
403OPENSSL_EXPORT X509_EXTENSION *X509_get_ext(const X509 *x, int loc);
404
David Benjamindd8ffe12023-11-12 12:39:39 -0500405// X509_get_ext_d2i behaves like |X509V3_get_d2i| but looks for the extension in
406// |x509|'s extension list.
407//
408// WARNING: This function is difficult to use correctly. See the documentation
409// for |X509V3_get_d2i| for details.
410OPENSSL_EXPORT void *X509_get_ext_d2i(const X509 *x509, int nid,
411 int *out_critical, int *out_idx);
412
David Benjamin4363bdd2022-04-06 15:19:16 -0400413// X509_get0_tbs_sigalg returns the signature algorithm in |x509|'s
414// TBSCertificate. For the outer signature algorithm, see |X509_get0_signature|.
415//
416// Certificates with mismatched signature algorithms will successfully parse,
417// but they will be rejected when verifying.
418OPENSSL_EXPORT const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x509);
419
420// X509_get0_signature sets |*out_sig| and |*out_alg| to the signature and
421// signature algorithm of |x509|, respectively. Either output pointer may be
422// NULL to ignore the value.
423//
424// This function outputs the outer signature algorithm. For the one in the
425// TBSCertificate, see |X509_get0_tbs_sigalg|. Certificates with mismatched
426// signature algorithms will successfully parse, but they will be rejected when
427// verifying.
428OPENSSL_EXPORT void X509_get0_signature(const ASN1_BIT_STRING **out_sig,
429 const X509_ALGOR **out_alg,
430 const X509 *x509);
431
432// X509_get_signature_nid returns the NID corresponding to |x509|'s signature
433// algorithm, or |NID_undef| if the signature algorithm does not correspond to
434// a known NID.
435OPENSSL_EXPORT int X509_get_signature_nid(const X509 *x509);
436
437// i2d_X509_tbs serializes the TBSCertificate portion of |x509|, as described in
438// |i2d_SAMPLE|.
439//
440// This function preserves the original encoding of the TBSCertificate and may
441// not reflect modifications made to |x509|. It may be used to manually verify
442// the signature of an existing certificate. To generate certificates, use
443// |i2d_re_X509_tbs| instead.
444OPENSSL_EXPORT int i2d_X509_tbs(X509 *x509, unsigned char **outp);
445
David Benjamin787713b2023-04-19 10:28:13 -0400446// X509_verify checks that |x509| has a valid signature by |pkey|. It returns
447// one if the signature is valid and zero otherwise. Note this function only
448// checks the signature itself and does not perform a full certificate
449// validation.
450OPENSSL_EXPORT int X509_verify(X509 *x509, EVP_PKEY *pkey);
451
David Benjamin314c2522023-11-27 23:26:09 -0500452// X509_get1_email returns a newly-allocated list of NUL-terminated strings
453// containing all email addresses in |x509|'s subject and all rfc822name names
454// in |x509|'s subject alternative names. Email addresses which contain embedded
455// NUL bytes are skipped.
456//
457// On error, or if there are no such email addresses, it returns NULL. When
458// done, the caller must release the result with |X509_email_free|.
459OPENSSL_EXPORT STACK_OF(OPENSSL_STRING) *X509_get1_email(const X509 *x509);
460
461// X509_get1_ocsp returns a newly-allocated list of NUL-terminated strings
462// containing all OCSP URIs in |x509|. That is, it collects all URI
463// AccessDescriptions with an accessMethod of id-ad-ocsp in |x509|'s authority
464// information access extension. URIs which contain embedded NUL bytes are
465// skipped.
466//
467// On error, or if there are no such URIs, it returns NULL. When done, the
468// caller must release the result with |X509_email_free|.
469OPENSSL_EXPORT STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(const X509 *x509);
470
471// X509_email_free releases memory associated with |sk|, including |sk| itself.
472// Each |OPENSSL_STRING| in |sk| must be a NUL-terminated string allocated with
473// |OPENSSL_malloc|. If |sk| is NULL, no action is taken.
474OPENSSL_EXPORT void X509_email_free(STACK_OF(OPENSSL_STRING) *sk);
475
David Benjamin4363bdd2022-04-06 15:19:16 -0400476
477// Issuing certificates.
478//
479// An |X509| object may also represent an incomplete certificate. Callers may
480// construct empty |X509| objects, fill in fields individually, and finally sign
481// the result. The following functions may be used for this purpose.
482
483// X509_new returns a newly-allocated, empty |X509| object, or NULL on error.
484// This produces an incomplete certificate which may be filled in to issue a new
485// certificate.
486OPENSSL_EXPORT X509 *X509_new(void);
487
488// X509_set_version sets |x509|'s version to |version|, which should be one of
489// the |X509V_VERSION_*| constants. It returns one on success and zero on error.
490//
491// If unsure, use |X509_VERSION_3|.
492OPENSSL_EXPORT int X509_set_version(X509 *x509, long version);
493
494// X509_set_serialNumber sets |x509|'s serial number to |serial|. It returns one
495// on success and zero on error.
496OPENSSL_EXPORT int X509_set_serialNumber(X509 *x509,
497 const ASN1_INTEGER *serial);
498
499// X509_set1_notBefore sets |x509|'s notBefore time to |tm|. It returns one on
500// success and zero on error.
501OPENSSL_EXPORT int X509_set1_notBefore(X509 *x509, const ASN1_TIME *tm);
502
503// X509_set1_notAfter sets |x509|'s notAfter time to |tm|. it returns one on
504// success and zero on error.
505OPENSSL_EXPORT int X509_set1_notAfter(X509 *x509, const ASN1_TIME *tm);
506
507// X509_getm_notBefore returns a mutable pointer to |x509|'s notBefore time.
508OPENSSL_EXPORT ASN1_TIME *X509_getm_notBefore(X509 *x509);
509
510// X509_getm_notAfter returns a mutable pointer to |x509|'s notAfter time.
511OPENSSL_EXPORT ASN1_TIME *X509_getm_notAfter(X509 *x);
512
513// X509_set_issuer_name sets |x509|'s issuer to a copy of |name|. It returns one
514// on success and zero on error.
515OPENSSL_EXPORT int X509_set_issuer_name(X509 *x509, X509_NAME *name);
516
517// X509_set_subject_name sets |x509|'s subject to a copy of |name|. It returns
518// one on success and zero on error.
519OPENSSL_EXPORT int X509_set_subject_name(X509 *x509, X509_NAME *name);
520
521// X509_set_pubkey sets |x509|'s public key to |pkey|. It returns one on success
522// and zero on error. This function does not take ownership of |pkey| and
523// internally copies and updates reference counts as needed.
524OPENSSL_EXPORT int X509_set_pubkey(X509 *x509, EVP_PKEY *pkey);
525
David Benjamin557b80f2022-07-10 19:12:12 -0400526// X509_delete_ext removes the extension in |x| at index |loc| and returns the
527// removed extension, or NULL if |loc| was out of bounds. If non-NULL, the
528// caller must release the result with |X509_EXTENSION_free|.
529OPENSSL_EXPORT X509_EXTENSION *X509_delete_ext(X509 *x, int loc);
530
531// X509_add_ext adds a copy of |ex| to |x|. It returns one on success and zero
532// on failure. The caller retains ownership of |ex| and can release it
533// independently of |x|.
534//
535// The new extension is inserted at index |loc|, shifting extensions to the
536// right. If |loc| is -1 or out of bounds, the new extension is appended to the
537// list.
538OPENSSL_EXPORT int X509_add_ext(X509 *x, const X509_EXTENSION *ex, int loc);
539
David Benjamindd8ffe12023-11-12 12:39:39 -0500540// X509_add1_ext_i2d behaves like |X509V3_add1_i2d| but adds the extension to
541// |x|'s extension list.
542//
543// WARNING: This function may return zero or -1 on error. The caller must also
544// ensure |value|'s type matches |nid|. See the documentation for
545// |X509V3_add1_i2d| for details.
546OPENSSL_EXPORT int X509_add1_ext_i2d(X509 *x, int nid, void *value, int crit,
547 unsigned long flags);
548
David Benjamin4363bdd2022-04-06 15:19:16 -0400549// X509_sign signs |x509| with |pkey| and replaces the signature algorithm and
David Benjamin0c7527b2023-04-25 21:45:00 -0400550// signature fields. It returns the length of the signature on success and zero
551// on error. This function uses digest algorithm |md|, or |pkey|'s default if
552// NULL. Other signing parameters use |pkey|'s defaults. To customize them, use
553// |X509_sign_ctx|.
David Benjamin4363bdd2022-04-06 15:19:16 -0400554OPENSSL_EXPORT int X509_sign(X509 *x509, EVP_PKEY *pkey, const EVP_MD *md);
555
556// X509_sign_ctx signs |x509| with |ctx| and replaces the signature algorithm
David Benjamin0c7527b2023-04-25 21:45:00 -0400557// and signature fields. It returns the length of the signature on success and
558// zero on error. The signature algorithm and parameters come from |ctx|, which
559// must have been initialized with |EVP_DigestSignInit|. The caller should
560// configure the corresponding |EVP_PKEY_CTX| before calling this function.
David Benjamin5bef6ec2023-11-13 09:04:43 -0500561//
562// On success or failure, this function mutates |ctx| and resets it to the empty
563// state. Caller should not rely on its contents after the function returns.
David Benjamin4363bdd2022-04-06 15:19:16 -0400564OPENSSL_EXPORT int X509_sign_ctx(X509 *x509, EVP_MD_CTX *ctx);
565
566// i2d_re_X509_tbs serializes the TBSCertificate portion of |x509|, as described
567// in |i2d_SAMPLE|.
568//
569// This function re-encodes the TBSCertificate and may not reflect |x509|'s
570// original encoding. It may be used to manually generate a signature for a new
571// certificate. To verify certificates, use |i2d_X509_tbs| instead.
572OPENSSL_EXPORT int i2d_re_X509_tbs(X509 *x509, unsigned char **outp);
573
574// X509_set1_signature_algo sets |x509|'s signature algorithm to |algo| and
575// returns one on success or zero on error. It updates both the signature field
576// of the TBSCertificate structure, and the signatureAlgorithm field of the
577// Certificate.
578OPENSSL_EXPORT int X509_set1_signature_algo(X509 *x509, const X509_ALGOR *algo);
579
580// X509_set1_signature_value sets |x509|'s signature to a copy of the |sig_len|
581// bytes pointed by |sig|. It returns one on success and zero on error.
582//
583// Due to a specification error, X.509 certificates store signatures in ASN.1
584// BIT STRINGs, but signature algorithms return byte strings rather than bit
585// strings. This function creates a BIT STRING containing a whole number of
586// bytes, with the bit order matching the DER encoding. This matches the
587// encoding used by all X.509 signature algorithms.
588OPENSSL_EXPORT int X509_set1_signature_value(X509 *x509, const uint8_t *sig,
589 size_t sig_len);
590
591
592// Auxiliary certificate properties.
593//
594// |X509| objects optionally maintain auxiliary properties. These are not part
595// of the certificates themselves, and thus are not covered by signatures or
596// preserved by the standard serialization. They are used as inputs or outputs
597// to other functions in this library.
598
599// i2d_X509_AUX marshals |x509| as a DER-encoded X.509 Certificate (RFC 5280),
600// followed optionally by a separate, OpenSSL-specific structure with auxiliary
601// properties. It behaves as described in |i2d_SAMPLE|.
602//
603// Unlike similarly-named functions, this function does not output a single
604// ASN.1 element. Directly embedding the output in a larger ASN.1 structure will
605// not behave correctly.
David Benjamin3ef8cbc2023-12-16 21:36:59 -0500606//
607// TODO(crbug.com/boringssl/407): |x509| should be const.
608OPENSSL_EXPORT int i2d_X509_AUX(X509 *x509, uint8_t **outp);
David Benjamin4363bdd2022-04-06 15:19:16 -0400609
610// d2i_X509_AUX parses up to |length| bytes from |*inp| as a DER-encoded X.509
611// Certificate (RFC 5280), followed optionally by a separate, OpenSSL-specific
David Benjamin19721cd2023-01-16 16:19:03 -0500612// structure with auxiliary properties. It behaves as described in |d2i_SAMPLE|.
David Benjamin4363bdd2022-04-06 15:19:16 -0400613//
614// Some auxiliary properties affect trust decisions, so this function should not
615// be used with untrusted input.
616//
617// Unlike similarly-named functions, this function does not parse a single
618// ASN.1 element. Trying to parse data directly embedded in a larger ASN.1
619// structure will not behave correctly.
David Benjamin3ef8cbc2023-12-16 21:36:59 -0500620OPENSSL_EXPORT X509 *d2i_X509_AUX(X509 **x509, const uint8_t **inp,
David Benjamin4363bdd2022-04-06 15:19:16 -0400621 long length);
622
623// X509_alias_set1 sets |x509|'s alias to |len| bytes from |name|. If |name| is
624// NULL, the alias is cleared instead. Aliases are not part of the certificate
625// itself and will not be serialized by |i2d_X509|.
David Benjamin3ef8cbc2023-12-16 21:36:59 -0500626OPENSSL_EXPORT int X509_alias_set1(X509 *x509, const uint8_t *name,
David Benjamina4385192023-03-25 01:26:49 -0400627 ossl_ssize_t len);
David Benjamin4363bdd2022-04-06 15:19:16 -0400628
629// X509_keyid_set1 sets |x509|'s key ID to |len| bytes from |id|. If |id| is
630// NULL, the key ID is cleared instead. Key IDs are not part of the certificate
631// itself and will not be serialized by |i2d_X509|.
David Benjamin3ef8cbc2023-12-16 21:36:59 -0500632OPENSSL_EXPORT int X509_keyid_set1(X509 *x509, const uint8_t *id,
David Benjamina4385192023-03-25 01:26:49 -0400633 ossl_ssize_t len);
David Benjamin4363bdd2022-04-06 15:19:16 -0400634
635// X509_alias_get0 looks up |x509|'s alias. If found, it sets |*out_len| to the
636// alias's length and returns a pointer to a buffer containing the contents. If
637// not found, it outputs the empty string by returning NULL and setting
638// |*out_len| to zero.
639//
640// If |x509| was parsed from a PKCS#12 structure (see
641// |PKCS12_get_key_and_certs|), the alias will reflect the friendlyName
642// attribute (RFC 2985).
643//
644// WARNING: In OpenSSL, this function did not set |*out_len| when the alias was
645// missing. Callers that target both OpenSSL and BoringSSL should set the value
646// to zero before calling this function.
David Benjamin3ef8cbc2023-12-16 21:36:59 -0500647OPENSSL_EXPORT const uint8_t *X509_alias_get0(const X509 *x509, int *out_len);
David Benjamin4363bdd2022-04-06 15:19:16 -0400648
649// X509_keyid_get0 looks up |x509|'s key ID. If found, it sets |*out_len| to the
650// key ID's length and returns a pointer to a buffer containing the contents. If
651// not found, it outputs the empty string by returning NULL and setting
652// |*out_len| to zero.
653//
654// WARNING: In OpenSSL, this function did not set |*out_len| when the alias was
655// missing. Callers that target both OpenSSL and BoringSSL should set the value
656// to zero before calling this function.
David Benjamin3ef8cbc2023-12-16 21:36:59 -0500657OPENSSL_EXPORT const uint8_t *X509_keyid_get0(const X509 *x509, int *out_len);
David Benjamin4363bdd2022-04-06 15:19:16 -0400658
David Benjamin240b73a2023-11-12 12:13:03 -0500659// X509_add1_trust_object configures |x509| as a valid trust anchor for |obj|.
660// It returns one on success and zero on error. |obj| should be a certificate
661// usage OID associated with an |X509_TRUST| object.
662OPENSSL_EXPORT int X509_add1_trust_object(X509 *x509, const ASN1_OBJECT *obj);
663
664// X509_add1_reject_object configures |x509| as distrusted for |obj|. It returns
665// one on success and zero on error. |obj| should be a certificate usage OID
666// associated with an |X509_TRUST| object.
667OPENSSL_EXPORT int X509_add1_reject_object(X509 *x509, const ASN1_OBJECT *obj);
668
David Benjamin09febb32023-11-21 00:14:22 -0500669// X509_trust_clear clears the list of OIDs for which |x509| is trusted. See
David Benjamin240b73a2023-11-12 12:13:03 -0500670// also |X509_add1_trust_object|.
671OPENSSL_EXPORT void X509_trust_clear(X509 *x509);
672
673// X509_reject_clear clears the list of OIDs for which |x509| is distrusted. See
674// also |X509_add1_reject_object|.
675OPENSSL_EXPORT void X509_reject_clear(X509 *x509);
676
David Benjamin4363bdd2022-04-06 15:19:16 -0400677
678// Certificate revocation lists.
679//
680// An |X509_CRL| object represents an X.509 certificate revocation list (CRL),
David Benjamindd8ffe12023-11-12 12:39:39 -0500681// defined in RFC 5280. A CRL is a signed list of certificates, the
682// revokedCertificates field, which are no longer considered valid. Each entry
683// of this list is represented with an |X509_REVOKED| object, documented in the
684// "CRL entries" section below.
David Benjamin4363bdd2022-04-06 15:19:16 -0400685//
David Benjamindd8ffe12023-11-12 12:39:39 -0500686// Although an |X509_CRL| is a mutable object, mutating an |X509_CRL| or its
687// |X509_REVOKED|s can give incorrect results. Callers typically obtain
688// |X509_CRL|s by parsing some input with |d2i_X509_CRL|, etc. Such objects
689// carry information such as the serialized TBSCertList and decoded extensions,
690// which will become inconsistent when mutated.
David Benjamin4363bdd2022-04-06 15:19:16 -0400691//
692// Instead, mutation functions should only be used when issuing new CRLs, as
693// described in a later section.
694
695DEFINE_STACK_OF(X509_CRL)
David Benjamindd8ffe12023-11-12 12:39:39 -0500696DEFINE_STACK_OF(X509_REVOKED)
David Benjamin4363bdd2022-04-06 15:19:16 -0400697
David Benjamin4363bdd2022-04-06 15:19:16 -0400698// X509_CRL_up_ref adds one to the reference count of |crl| and returns one.
699OPENSSL_EXPORT int X509_CRL_up_ref(X509_CRL *crl);
700
David Benjaminb8596422022-07-10 01:08:07 -0400701// X509_CRL_dup returns a newly-allocated copy of |crl|, or NULL on error. This
702// function works by serializing the structure, so if |crl| is incomplete, it
703// may fail.
704//
705// TODO(https://crbug.com/boringssl/407): This function should be const and
706// thread-safe but is currently neither in some cases, notably if |crl| was
707// mutated.
708OPENSSL_EXPORT X509_CRL *X509_CRL_dup(X509_CRL *crl);
709
David Benjamin4363bdd2022-04-06 15:19:16 -0400710// X509_CRL_free decrements |crl|'s reference count and, if zero, releases
711// memory associated with |crl|.
712OPENSSL_EXPORT void X509_CRL_free(X509_CRL *crl);
713
714// d2i_X509_CRL parses up to |len| bytes from |*inp| as a DER-encoded X.509
David Benjamin19721cd2023-01-16 16:19:03 -0500715// CertificateList (RFC 5280), as described in |d2i_SAMPLE|.
David Benjamin4363bdd2022-04-06 15:19:16 -0400716OPENSSL_EXPORT X509_CRL *d2i_X509_CRL(X509_CRL **out, const uint8_t **inp,
717 long len);
718
719// i2d_X509_CRL marshals |crl| as a X.509 CertificateList (RFC 5280), as
720// described in |i2d_SAMPLE|.
721//
722// TODO(https://crbug.com/boringssl/407): This function should be const and
723// thread-safe but is currently neither in some cases, notably if |crl| was
724// mutated.
725OPENSSL_EXPORT int i2d_X509_CRL(X509_CRL *crl, uint8_t **outp);
726
727#define X509_CRL_VERSION_1 0
728#define X509_CRL_VERSION_2 1
729
730// X509_CRL_get_version returns the numerical value of |crl|'s version, which
731// will be one of the |X509_CRL_VERSION_*| constants.
732OPENSSL_EXPORT long X509_CRL_get_version(const X509_CRL *crl);
733
David Benjamin884ae3a2022-07-08 18:18:12 -0400734// X509_CRL_get0_lastUpdate returns |crl|'s thisUpdate time. The OpenSSL API
735// refers to this field as lastUpdate.
David Benjamin4363bdd2022-04-06 15:19:16 -0400736OPENSSL_EXPORT const ASN1_TIME *X509_CRL_get0_lastUpdate(const X509_CRL *crl);
737
738// X509_CRL_get0_nextUpdate returns |crl|'s nextUpdate time, or NULL if |crl|
739// has none.
740OPENSSL_EXPORT const ASN1_TIME *X509_CRL_get0_nextUpdate(const X509_CRL *crl);
741
742// X509_CRL_get_issuer returns |crl|'s issuer name. Note this function is not
743// const-correct for legacy reasons.
744OPENSSL_EXPORT X509_NAME *X509_CRL_get_issuer(const X509_CRL *crl);
745
David Benjamindd8ffe12023-11-12 12:39:39 -0500746// X509_CRL_get0_by_serial finds the entry in |crl| whose serial number is
David Benjamin9177d652023-11-12 14:05:24 -0500747// |serial|. If found, it sets |*out| to the entry and returns one. If not
748// found, it returns zero.
David Benjamindd8ffe12023-11-12 12:39:39 -0500749//
750// On success, |*out| continues to be owned by |crl|. It is an error to free or
751// otherwise modify |*out|.
752//
753// TODO(crbug.com/boringssl/600): Ideally |crl| would be const. It is broadly
754// thread-safe, but changes the order of entries in |crl|. It cannot be called
755// concurrently with |i2d_X509_CRL|.
David Benjamindd8ffe12023-11-12 12:39:39 -0500756OPENSSL_EXPORT int X509_CRL_get0_by_serial(X509_CRL *crl, X509_REVOKED **out,
757 const ASN1_INTEGER *serial);
758
759// X509_CRL_get0_by_cert behaves like |X509_CRL_get0_by_serial|, except it looks
760// for the entry that matches |x509|.
761OPENSSL_EXPORT int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **out,
762 X509 *x509);
763
David Benjamin4363bdd2022-04-06 15:19:16 -0400764// X509_CRL_get_REVOKED returns the list of revoked certificates in |crl|, or
765// NULL if |crl| omits it.
766//
767// TOOD(davidben): This function was originally a macro, without clear const
768// semantics. It should take a const input and give const output, but the latter
769// would break existing callers. For now, we match upstream.
770OPENSSL_EXPORT STACK_OF(X509_REVOKED) *X509_CRL_get_REVOKED(X509_CRL *crl);
771
772// X509_CRL_get0_extensions returns |crl|'s extension list, or NULL if |crl|
David Benjamindd8ffe12023-11-12 12:39:39 -0500773// omits it. A CRL can have extensions on individual entries, which is
774// |X509_REVOKED_get0_extensions|, or on the overall CRL, which is this
775// function.
David Benjamin4363bdd2022-04-06 15:19:16 -0400776OPENSSL_EXPORT const STACK_OF(X509_EXTENSION) *X509_CRL_get0_extensions(
777 const X509_CRL *crl);
778
David Benjamin557b80f2022-07-10 19:12:12 -0400779// X509_CRL_get_ext_count returns the number of extensions in |x|.
780OPENSSL_EXPORT int X509_CRL_get_ext_count(const X509_CRL *x);
781
782// X509_CRL_get_ext_by_NID behaves like |X509v3_get_ext_by_NID| but searches for
783// extensions in |x|.
784OPENSSL_EXPORT int X509_CRL_get_ext_by_NID(const X509_CRL *x, int nid,
785 int lastpos);
786
787// X509_CRL_get_ext_by_OBJ behaves like |X509v3_get_ext_by_OBJ| but searches for
788// extensions in |x|.
789OPENSSL_EXPORT int X509_CRL_get_ext_by_OBJ(const X509_CRL *x,
790 const ASN1_OBJECT *obj, int lastpos);
791
792// X509_CRL_get_ext_by_critical behaves like |X509v3_get_ext_by_critical| but
793// searches for extensions in |x|.
794OPENSSL_EXPORT int X509_CRL_get_ext_by_critical(const X509_CRL *x, int crit,
795 int lastpos);
796
797// X509_CRL_get_ext returns the extension in |x| at index |loc|, or NULL if
798// |loc| is out of bounds. This function returns a non-const pointer for OpenSSL
799// compatibility, but callers should not mutate the result.
800OPENSSL_EXPORT X509_EXTENSION *X509_CRL_get_ext(const X509_CRL *x, int loc);
801
David Benjamindd8ffe12023-11-12 12:39:39 -0500802// X509_CRL_get_ext_d2i behaves like |X509V3_get_d2i| but looks for the
803// extension in |crl|'s extension list.
804//
805// WARNING: This function is difficult to use correctly. See the documentation
806// for |X509V3_get_d2i| for details.
807OPENSSL_EXPORT void *X509_CRL_get_ext_d2i(const X509_CRL *crl, int nid,
808 int *out_critical, int *out_idx);
809
David Benjamin4363bdd2022-04-06 15:19:16 -0400810// X509_CRL_get0_signature sets |*out_sig| and |*out_alg| to the signature and
811// signature algorithm of |crl|, respectively. Either output pointer may be NULL
812// to ignore the value.
813//
814// This function outputs the outer signature algorithm, not the one in the
815// TBSCertList. CRLs with mismatched signature algorithms will successfully
816// parse, but they will be rejected when verifying.
817OPENSSL_EXPORT void X509_CRL_get0_signature(const X509_CRL *crl,
818 const ASN1_BIT_STRING **out_sig,
819 const X509_ALGOR **out_alg);
820
821// X509_CRL_get_signature_nid returns the NID corresponding to |crl|'s signature
822// algorithm, or |NID_undef| if the signature algorithm does not correspond to
823// a known NID.
824OPENSSL_EXPORT int X509_CRL_get_signature_nid(const X509_CRL *crl);
825
826// i2d_X509_CRL_tbs serializes the TBSCertList portion of |crl|, as described in
827// |i2d_SAMPLE|.
828//
829// This function preserves the original encoding of the TBSCertList and may not
830// reflect modifications made to |crl|. It may be used to manually verify the
831// signature of an existing CRL. To generate CRLs, use |i2d_re_X509_CRL_tbs|
832// instead.
833OPENSSL_EXPORT int i2d_X509_CRL_tbs(X509_CRL *crl, unsigned char **outp);
834
David Benjamin787713b2023-04-19 10:28:13 -0400835// X509_CRL_verify checks that |crl| has a valid signature by |pkey|. It returns
836// one if the signature is valid and zero otherwise.
837OPENSSL_EXPORT int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *pkey);
838
David Benjamin4363bdd2022-04-06 15:19:16 -0400839
840// Issuing certificate revocation lists.
841//
842// An |X509_CRL| object may also represent an incomplete CRL. Callers may
843// construct empty |X509_CRL| objects, fill in fields individually, and finally
844// sign the result. The following functions may be used for this purpose.
845
846// X509_CRL_new returns a newly-allocated, empty |X509_CRL| object, or NULL on
847// error. This object may be filled in and then signed to construct a CRL.
848OPENSSL_EXPORT X509_CRL *X509_CRL_new(void);
849
850// X509_CRL_set_version sets |crl|'s version to |version|, which should be one
851// of the |X509_CRL_VERSION_*| constants. It returns one on success and zero on
852// error.
853//
854// If unsure, use |X509_CRL_VERSION_2|. Note that, unlike certificates, CRL
855// versions are only defined up to v2. Callers should not use |X509_VERSION_3|.
856OPENSSL_EXPORT int X509_CRL_set_version(X509_CRL *crl, long version);
857
858// X509_CRL_set_issuer_name sets |crl|'s issuer to a copy of |name|. It returns
859// one on success and zero on error.
860OPENSSL_EXPORT int X509_CRL_set_issuer_name(X509_CRL *crl, X509_NAME *name);
861
David Benjamin884ae3a2022-07-08 18:18:12 -0400862// X509_CRL_set1_lastUpdate sets |crl|'s thisUpdate time to |tm|. It returns one
863// on success and zero on error. The OpenSSL API refers to this field as
864// lastUpdate.
David Benjamin4363bdd2022-04-06 15:19:16 -0400865OPENSSL_EXPORT int X509_CRL_set1_lastUpdate(X509_CRL *crl, const ASN1_TIME *tm);
866
867// X509_CRL_set1_nextUpdate sets |crl|'s nextUpdate time to |tm|. It returns one
868// on success and zero on error.
869OPENSSL_EXPORT int X509_CRL_set1_nextUpdate(X509_CRL *crl, const ASN1_TIME *tm);
870
David Benjamindd8ffe12023-11-12 12:39:39 -0500871// X509_CRL_add0_revoked adds |rev| to |crl|. On success, it takes ownership of
872// |rev| and returns one. On error, it returns zero. If this function fails, the
873// caller retains ownership of |rev| and must release it when done.
874OPENSSL_EXPORT int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev);
875
876// X509_CRL_sort sorts the entries in |crl| by serial number. It returns one on
877// success and zero on error.
878OPENSSL_EXPORT int X509_CRL_sort(X509_CRL *crl);
879
David Benjamin557b80f2022-07-10 19:12:12 -0400880// X509_CRL_delete_ext removes the extension in |x| at index |loc| and returns
881// the removed extension, or NULL if |loc| was out of bounds. If non-NULL, the
882// caller must release the result with |X509_EXTENSION_free|.
883OPENSSL_EXPORT X509_EXTENSION *X509_CRL_delete_ext(X509_CRL *x, int loc);
884
885// X509_CRL_add_ext adds a copy of |ex| to |x|. It returns one on success and
886// zero on failure. The caller retains ownership of |ex| and can release it
887// independently of |x|.
888//
889// The new extension is inserted at index |loc|, shifting extensions to the
890// right. If |loc| is -1 or out of bounds, the new extension is appended to the
891// list.
892OPENSSL_EXPORT int X509_CRL_add_ext(X509_CRL *x, const X509_EXTENSION *ex,
893 int loc);
894
David Benjamindd8ffe12023-11-12 12:39:39 -0500895// X509_CRL_add1_ext_i2d behaves like |X509V3_add1_i2d| but adds the extension
896// to |x|'s extension list.
897//
898// WARNING: This function may return zero or -1 on error. The caller must also
899// ensure |value|'s type matches |nid|. See the documentation for
900// |X509V3_add1_i2d| for details.
901OPENSSL_EXPORT int X509_CRL_add1_ext_i2d(X509_CRL *x, int nid, void *value,
902 int crit, unsigned long flags);
903
David Benjamin4363bdd2022-04-06 15:19:16 -0400904// X509_CRL_sign signs |crl| with |pkey| and replaces the signature algorithm
David Benjamin0c7527b2023-04-25 21:45:00 -0400905// and signature fields. It returns the length of the signature on success and
906// zero on error. This function uses digest algorithm |md|, or |pkey|'s default
907// if NULL. Other signing parameters use |pkey|'s defaults. To customize them,
908// use |X509_CRL_sign_ctx|.
David Benjamin4363bdd2022-04-06 15:19:16 -0400909OPENSSL_EXPORT int X509_CRL_sign(X509_CRL *crl, EVP_PKEY *pkey,
910 const EVP_MD *md);
911
912// X509_CRL_sign_ctx signs |crl| with |ctx| and replaces the signature algorithm
David Benjamin0c7527b2023-04-25 21:45:00 -0400913// and signature fields. It returns the length of the signature on success and
914// zero on error. The signature algorithm and parameters come from |ctx|, which
915// must have been initialized with |EVP_DigestSignInit|. The caller should
916// configure the corresponding |EVP_PKEY_CTX| before calling this function.
David Benjamin5bef6ec2023-11-13 09:04:43 -0500917//
918// On success or failure, this function mutates |ctx| and resets it to the empty
919// state. Caller should not rely on its contents after the function returns.
David Benjamin4363bdd2022-04-06 15:19:16 -0400920OPENSSL_EXPORT int X509_CRL_sign_ctx(X509_CRL *crl, EVP_MD_CTX *ctx);
921
922// i2d_re_X509_CRL_tbs serializes the TBSCertList portion of |crl|, as described
923// in |i2d_SAMPLE|.
924//
925// This function re-encodes the TBSCertList and may not reflect |crl|'s original
926// encoding. It may be used to manually generate a signature for a new CRL. To
927// verify CRLs, use |i2d_X509_CRL_tbs| instead.
928OPENSSL_EXPORT int i2d_re_X509_CRL_tbs(X509_CRL *crl, unsigned char **outp);
929
930// X509_CRL_set1_signature_algo sets |crl|'s signature algorithm to |algo| and
931// returns one on success or zero on error. It updates both the signature field
932// of the TBSCertList structure, and the signatureAlgorithm field of the CRL.
933OPENSSL_EXPORT int X509_CRL_set1_signature_algo(X509_CRL *crl,
934 const X509_ALGOR *algo);
935
936// X509_CRL_set1_signature_value sets |crl|'s signature to a copy of the
937// |sig_len| bytes pointed by |sig|. It returns one on success and zero on
938// error.
939//
940// Due to a specification error, X.509 CRLs store signatures in ASN.1 BIT
941// STRINGs, but signature algorithms return byte strings rather than bit
942// strings. This function creates a BIT STRING containing a whole number of
943// bytes, with the bit order matching the DER encoding. This matches the
944// encoding used by all X.509 signature algorithms.
945OPENSSL_EXPORT int X509_CRL_set1_signature_value(X509_CRL *crl,
946 const uint8_t *sig,
947 size_t sig_len);
948
949
David Benjamindd8ffe12023-11-12 12:39:39 -0500950// CRL entries.
951//
952// Each entry of a CRL is represented as an |X509_REVOKED| object, which
953// describes a revoked certificate by serial number.
954//
955// When an |X509_REVOKED| is obtained from an |X509_CRL| object, it is an error
956// to mutate the object. Doing so may break |X509_CRL|'s and cause the library
957// to behave incorrectly.
958
David Benjamindd8ffe12023-11-12 12:39:39 -0500959// X509_REVOKED_new returns a newly-allocated, empty |X509_REVOKED| object, or
960// NULL on allocation error.
961OPENSSL_EXPORT X509_REVOKED *X509_REVOKED_new(void);
962
963// X509_REVOKED_free releases memory associated with |rev|.
964OPENSSL_EXPORT void X509_REVOKED_free(X509_REVOKED *rev);
965
966// d2i_X509_REVOKED parses up to |len| bytes from |*inp| as a DER-encoded X.509
967// CRL entry, as described in |d2i_SAMPLE|.
968OPENSSL_EXPORT X509_REVOKED *d2i_X509_REVOKED(X509_REVOKED **out,
969 const uint8_t **inp, long len);
970
971// i2d_X509_REVOKED marshals |alg| as a DER-encoded X.509 CRL entry, as
972// described in |i2d_SAMPLE|.
973OPENSSL_EXPORT int i2d_X509_REVOKED(const X509_REVOKED *alg, uint8_t **outp);
974
975// X509_REVOKED_dup returns a newly-allocated copy of |rev|, or NULL on error.
976// This function works by serializing the structure, so if |rev| is incomplete,
977// it may fail.
978OPENSSL_EXPORT X509_REVOKED *X509_REVOKED_dup(const X509_REVOKED *rev);
979
980// X509_REVOKED_get0_serialNumber returns the serial number of the certificate
981// revoked by |revoked|.
982OPENSSL_EXPORT const ASN1_INTEGER *X509_REVOKED_get0_serialNumber(
983 const X509_REVOKED *revoked);
984
985// X509_REVOKED_set_serialNumber sets |revoked|'s serial number to |serial|. It
986// returns one on success or zero on error.
987OPENSSL_EXPORT int X509_REVOKED_set_serialNumber(X509_REVOKED *revoked,
988 const ASN1_INTEGER *serial);
989
990// X509_REVOKED_get0_revocationDate returns the revocation time of the
991// certificate revoked by |revoked|.
992OPENSSL_EXPORT const ASN1_TIME *X509_REVOKED_get0_revocationDate(
993 const X509_REVOKED *revoked);
994
995// X509_REVOKED_set_revocationDate sets |revoked|'s revocation time to |tm|. It
996// returns one on success or zero on error.
997OPENSSL_EXPORT int X509_REVOKED_set_revocationDate(X509_REVOKED *revoked,
998 const ASN1_TIME *tm);
999
1000// X509_REVOKED_get0_extensions returns |r|'s extensions list, or NULL if |r|
1001// omits it. A CRL can have extensions on individual entries, which is this
1002// function, or on the overall CRL, which is |X509_CRL_get0_extensions|.
1003OPENSSL_EXPORT const STACK_OF(X509_EXTENSION) *X509_REVOKED_get0_extensions(
1004 const X509_REVOKED *r);
1005
1006 // X509_REVOKED_get_ext_count returns the number of extensions in |x|.
1007OPENSSL_EXPORT int X509_REVOKED_get_ext_count(const X509_REVOKED *x);
1008
1009// X509_REVOKED_get_ext_by_NID behaves like |X509v3_get_ext_by_NID| but searches
1010// for extensions in |x|.
1011OPENSSL_EXPORT int X509_REVOKED_get_ext_by_NID(const X509_REVOKED *x, int nid,
1012 int lastpos);
1013
1014// X509_REVOKED_get_ext_by_OBJ behaves like |X509v3_get_ext_by_OBJ| but searches
1015// for extensions in |x|.
1016OPENSSL_EXPORT int X509_REVOKED_get_ext_by_OBJ(const X509_REVOKED *x,
1017 const ASN1_OBJECT *obj,
1018 int lastpos);
1019
1020// X509_REVOKED_get_ext_by_critical behaves like |X509v3_get_ext_by_critical|
1021// but searches for extensions in |x|.
1022OPENSSL_EXPORT int X509_REVOKED_get_ext_by_critical(const X509_REVOKED *x,
1023 int crit, int lastpos);
1024
1025// X509_REVOKED_get_ext returns the extension in |x| at index |loc|, or NULL if
1026// |loc| is out of bounds. This function returns a non-const pointer for OpenSSL
1027// compatibility, but callers should not mutate the result.
1028OPENSSL_EXPORT X509_EXTENSION *X509_REVOKED_get_ext(const X509_REVOKED *x,
1029 int loc);
1030
1031// X509_REVOKED_delete_ext removes the extension in |x| at index |loc| and
1032// returns the removed extension, or NULL if |loc| was out of bounds. If
1033// non-NULL, the caller must release the result with |X509_EXTENSION_free|.
1034OPENSSL_EXPORT X509_EXTENSION *X509_REVOKED_delete_ext(X509_REVOKED *x,
1035 int loc);
1036
1037// X509_REVOKED_add_ext adds a copy of |ex| to |x|. It returns one on success
1038// and zero on failure. The caller retains ownership of |ex| and can release it
1039// independently of |x|.
1040//
1041// The new extension is inserted at index |loc|, shifting extensions to the
1042// right. If |loc| is -1 or out of bounds, the new extension is appended to the
1043// list.
1044OPENSSL_EXPORT int X509_REVOKED_add_ext(X509_REVOKED *x,
1045 const X509_EXTENSION *ex, int loc);
1046
1047// X509_REVOKED_get_ext_d2i behaves like |X509V3_get_d2i| but looks for the
1048// extension in |revoked|'s extension list.
1049//
1050// WARNING: This function is difficult to use correctly. See the documentation
1051// for |X509V3_get_d2i| for details.
1052OPENSSL_EXPORT void *X509_REVOKED_get_ext_d2i(const X509_REVOKED *revoked,
1053 int nid, int *out_critical,
1054 int *out_idx);
1055
1056// X509_REVOKED_add1_ext_i2d behaves like |X509V3_add1_i2d| but adds the
1057// extension to |x|'s extension list.
1058//
1059// WARNING: This function may return zero or -1 on error. The caller must also
1060// ensure |value|'s type matches |nid|. See the documentation for
1061// |X509V3_add1_i2d| for details.
1062OPENSSL_EXPORT int X509_REVOKED_add1_ext_i2d(X509_REVOKED *x, int nid,
1063 void *value, int crit,
1064 unsigned long flags);
1065
1066
David Benjamin4363bdd2022-04-06 15:19:16 -04001067// Certificate requests.
1068//
1069// An |X509_REQ| represents a PKCS #10 certificate request (RFC 2986). These are
1070// also referred to as certificate signing requests or CSRs. CSRs are a common
1071// format used to request a certificate from a CA.
1072//
1073// Although an |X509_REQ| is a mutable object, mutating an |X509_REQ| can give
1074// incorrect results. Callers typically obtain |X509_REQ|s by parsing some input
1075// with |d2i_X509_REQ|, etc. Such objects carry information such as the
1076// serialized CertificationRequestInfo, which will become inconsistent when
1077// mutated.
1078//
1079// Instead, mutation functions should only be used when issuing new CRLs, as
1080// described in a later section.
1081
David Benjaminb8596422022-07-10 01:08:07 -04001082// X509_REQ_dup returns a newly-allocated copy of |req|, or NULL on error. This
1083// function works by serializing the structure, so if |req| is incomplete, it
1084// may fail.
1085//
1086// TODO(https://crbug.com/boringssl/407): This function should be const and
1087// thread-safe but is currently neither in some cases, notably if |req| was
1088// mutated.
1089OPENSSL_EXPORT X509_REQ *X509_REQ_dup(X509_REQ *req);
1090
David Benjamin4363bdd2022-04-06 15:19:16 -04001091// X509_REQ_free releases memory associated with |req|.
1092OPENSSL_EXPORT void X509_REQ_free(X509_REQ *req);
1093
1094// d2i_X509_REQ parses up to |len| bytes from |*inp| as a DER-encoded
David Benjamin19721cd2023-01-16 16:19:03 -05001095// CertificateRequest (RFC 2986), as described in |d2i_SAMPLE|.
David Benjamin4363bdd2022-04-06 15:19:16 -04001096OPENSSL_EXPORT X509_REQ *d2i_X509_REQ(X509_REQ **out, const uint8_t **inp,
1097 long len);
1098
1099// i2d_X509_REQ marshals |req| as a CertificateRequest (RFC 2986), as described
1100// in |i2d_SAMPLE|.
1101//
1102// TODO(https://crbug.com/boringssl/407): This function should be const and
1103// thread-safe but is currently neither in some cases, notably if |req| was
1104// mutated.
1105OPENSSL_EXPORT int i2d_X509_REQ(X509_REQ *req, uint8_t **outp);
1106
David Benjamin4363bdd2022-04-06 15:19:16 -04001107// X509_REQ_VERSION_1 is the version constant for |X509_REQ| objects. No other
1108// versions are defined.
1109#define X509_REQ_VERSION_1 0
1110
1111// X509_REQ_get_version returns the numerical value of |req|'s version. This
1112// will always be |X509_REQ_VERSION_1| for valid CSRs. For compatibility,
1113// |d2i_X509_REQ| also accepts some invalid version numbers, in which case this
1114// function may return other values.
1115OPENSSL_EXPORT long X509_REQ_get_version(const X509_REQ *req);
1116
1117// X509_REQ_get_subject_name returns |req|'s subject name. Note this function is
1118// not const-correct for legacy reasons.
1119OPENSSL_EXPORT X509_NAME *X509_REQ_get_subject_name(const X509_REQ *req);
1120
David Benjamin5b3dc492023-12-21 19:19:16 -05001121// X509_REQ_get0_pubkey returns |req|'s public key as an |EVP_PKEY|, or NULL if
1122// the public key was unsupported or could not be decoded. The |EVP_PKEY| is
1123// cached in |req|, so callers must not mutate the result.
1124OPENSSL_EXPORT EVP_PKEY *X509_REQ_get0_pubkey(const X509_REQ *req);
1125
1126// X509_REQ_get_pubkey behaves like |X509_REQ_get0_pubkey| but increments the
1127// reference count on the |EVP_PKEY|. The caller must release the result with
1128// |EVP_PKEY_free| when done. The |EVP_PKEY| is cached in |req|, so callers must
1129// not mutate the result.
1130OPENSSL_EXPORT EVP_PKEY *X509_REQ_get_pubkey(const X509_REQ *req);
David Benjamin4363bdd2022-04-06 15:19:16 -04001131
David Benjamin2ba76342023-11-19 10:20:04 -05001132// X509_REQ_check_private_key returns one if |req|'s public key matches |pkey|
1133// and zero otherwise.
David Benjaminc1c7f092023-12-21 19:27:31 -05001134OPENSSL_EXPORT int X509_REQ_check_private_key(const X509_REQ *req,
David Benjamin2ba76342023-11-19 10:20:04 -05001135 const EVP_PKEY *pkey);
1136
David Benjamin787713b2023-04-19 10:28:13 -04001137// X509_REQ_get_attr_count returns the number of attributes in |req|.
1138OPENSSL_EXPORT int X509_REQ_get_attr_count(const X509_REQ *req);
1139
1140// X509_REQ_get_attr returns the attribute at index |loc| in |req|, or NULL if
1141// out of bounds.
1142OPENSSL_EXPORT X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc);
1143
1144// X509_REQ_get_attr_by_NID returns the index of the attribute in |req| of type
1145// |nid|, or a negative number if not found. If found, callers can use
1146// |X509_REQ_get_attr| to look up the attribute by index.
1147//
1148// If |lastpos| is non-negative, it begins searching at |lastpos| + 1. Callers
1149// can thus loop over all matching attributes by first passing -1 and then
1150// passing the previously-returned value until no match is returned.
1151OPENSSL_EXPORT int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid,
1152 int lastpos);
1153
1154// X509_REQ_get_attr_by_OBJ behaves like |X509_REQ_get_attr_by_NID| but looks
1155// for attributes of type |obj|.
1156OPENSSL_EXPORT int X509_REQ_get_attr_by_OBJ(const X509_REQ *req,
1157 const ASN1_OBJECT *obj,
1158 int lastpos);
1159
1160// X509_REQ_extension_nid returns one if |nid| is a supported CSR attribute type
1161// for carrying extensions and zero otherwise. The supported types are
1162// |NID_ext_req| (pkcs-9-at-extensionRequest from RFC 2985) and |NID_ms_ext_req|
1163// (a Microsoft szOID_CERT_EXTENSIONS variant).
1164OPENSSL_EXPORT int X509_REQ_extension_nid(int nid);
1165
David Benjamin314c2522023-11-27 23:26:09 -05001166// X509_REQ_get_extensions decodes the most preferred list of requested
1167// extensions in |req| and returns a newly-allocated |STACK_OF(X509_EXTENSION)|
1168// containing the result. It returns NULL on error, or if |req| did not request
1169// extensions.
David Benjamin787713b2023-04-19 10:28:13 -04001170//
1171// CSRs do not store extensions directly. Instead there are attribute types
1172// which are defined to hold extensions. See |X509_REQ_extension_nid|. This
1173// function supports both pkcs-9-at-extensionRequest from RFC 2985 and the
1174// Microsoft szOID_CERT_EXTENSIONS variant. If both are present,
1175// pkcs-9-at-extensionRequest is preferred.
David Benjamin314c2522023-11-27 23:26:09 -05001176OPENSSL_EXPORT STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(
1177 const X509_REQ *req);
David Benjamin787713b2023-04-19 10:28:13 -04001178
David Benjamin4363bdd2022-04-06 15:19:16 -04001179// X509_REQ_get0_signature sets |*out_sig| and |*out_alg| to the signature and
1180// signature algorithm of |req|, respectively. Either output pointer may be NULL
1181// to ignore the value.
1182OPENSSL_EXPORT void X509_REQ_get0_signature(const X509_REQ *req,
1183 const ASN1_BIT_STRING **out_sig,
1184 const X509_ALGOR **out_alg);
1185
1186// X509_REQ_get_signature_nid returns the NID corresponding to |req|'s signature
1187// algorithm, or |NID_undef| if the signature algorithm does not correspond to
1188// a known NID.
1189OPENSSL_EXPORT int X509_REQ_get_signature_nid(const X509_REQ *req);
1190
David Benjamin787713b2023-04-19 10:28:13 -04001191// X509_REQ_verify checks that |req| has a valid signature by |pkey|. It returns
1192// one if the signature is valid and zero otherwise.
1193OPENSSL_EXPORT int X509_REQ_verify(X509_REQ *req, EVP_PKEY *pkey);
1194
David Benjamin314c2522023-11-27 23:26:09 -05001195// X509_REQ_get1_email returns a newly-allocated list of NUL-terminated strings
1196// containing all email addresses in |req|'s subject and all rfc822name names
1197// in |req|'s subject alternative names. The subject alternative names extension
1198// is extracted from the result of |X509_REQ_get_extensions|. Email addresses
1199// which contain embedded NUL bytes are skipped.
1200//
1201// On error, or if there are no such email addresses, it returns NULL. When
1202// done, the caller must release the result with |X509_email_free|.
1203OPENSSL_EXPORT STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(
1204 const X509_REQ *req);
1205
David Benjamin4363bdd2022-04-06 15:19:16 -04001206
1207// Issuing certificate requests.
1208//
1209// An |X509_REQ| object may also represent an incomplete CSR. Callers may
1210// construct empty |X509_REQ| objects, fill in fields individually, and finally
1211// sign the result. The following functions may be used for this purpose.
1212
1213// X509_REQ_new returns a newly-allocated, empty |X509_REQ| object, or NULL on
1214// error. This object may be filled in and then signed to construct a CSR.
1215OPENSSL_EXPORT X509_REQ *X509_REQ_new(void);
1216
1217// X509_REQ_set_version sets |req|'s version to |version|, which should be
1218// |X509_REQ_VERSION_1|. It returns one on success and zero on error.
1219//
1220// The only defined CSR version is |X509_REQ_VERSION_1|, so there is no need to
1221// call this function.
1222OPENSSL_EXPORT int X509_REQ_set_version(X509_REQ *req, long version);
1223
1224// X509_REQ_set_subject_name sets |req|'s subject to a copy of |name|. It
1225// returns one on success and zero on error.
1226OPENSSL_EXPORT int X509_REQ_set_subject_name(X509_REQ *req, X509_NAME *name);
1227
1228// X509_REQ_set_pubkey sets |req|'s public key to |pkey|. It returns one on
1229// success and zero on error. This function does not take ownership of |pkey|
1230// and internally copies and updates reference counts as needed.
1231OPENSSL_EXPORT int X509_REQ_set_pubkey(X509_REQ *req, EVP_PKEY *pkey);
1232
David Benjamin787713b2023-04-19 10:28:13 -04001233// X509_REQ_delete_attr removes the attribute at index |loc| in |req|. It
1234// returns the removed attribute to the caller, or NULL if |loc| was out of
1235// bounds. If non-NULL, the caller must release the result with
1236// |X509_ATTRIBUTE_free| when done. It is also safe, but not necessary, to call
1237// |X509_ATTRIBUTE_free| if the result is NULL.
1238OPENSSL_EXPORT X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc);
1239
1240// X509_REQ_add1_attr appends a copy of |attr| to |req|'s list of attributes. It
1241// returns one on success and zero on error.
David Benjamin437ef4d2023-04-19 10:41:02 -04001242OPENSSL_EXPORT int X509_REQ_add1_attr(X509_REQ *req,
1243 const X509_ATTRIBUTE *attr);
David Benjamin787713b2023-04-19 10:28:13 -04001244
1245// X509_REQ_add1_attr_by_OBJ appends a new attribute to |req| with type |obj|.
1246// It returns one on success and zero on error. The value is determined by
1247// |X509_ATTRIBUTE_set1_data|.
1248//
1249// WARNING: The interpretation of |attrtype|, |data|, and |len| is complex and
1250// error-prone. See |X509_ATTRIBUTE_set1_data| for details.
1251OPENSSL_EXPORT int X509_REQ_add1_attr_by_OBJ(X509_REQ *req,
1252 const ASN1_OBJECT *obj,
1253 int attrtype,
1254 const unsigned char *data,
1255 int len);
1256
1257// X509_REQ_add1_attr_by_NID behaves like |X509_REQ_add1_attr_by_OBJ| except the
1258// attribute type is determined by |nid|.
1259OPENSSL_EXPORT int X509_REQ_add1_attr_by_NID(X509_REQ *req, int nid,
1260 int attrtype,
1261 const unsigned char *data,
1262 int len);
1263
1264// X509_REQ_add1_attr_by_txt behaves like |X509_REQ_add1_attr_by_OBJ| except the
1265// attribute type is determined by calling |OBJ_txt2obj| with |attrname|.
1266OPENSSL_EXPORT int X509_REQ_add1_attr_by_txt(X509_REQ *req,
1267 const char *attrname, int attrtype,
1268 const unsigned char *data,
1269 int len);
1270
1271// X509_REQ_add_extensions_nid adds an attribute to |req| of type |nid|, to
1272// request the certificate extensions in |exts|. It returns one on success and
1273// zero on error. |nid| should be |NID_ext_req| or |NID_ms_ext_req|.
1274OPENSSL_EXPORT int X509_REQ_add_extensions_nid(
1275 X509_REQ *req, const STACK_OF(X509_EXTENSION) *exts, int nid);
1276
1277// X509_REQ_add_extensions behaves like |X509_REQ_add_extensions_nid|, using the
1278// standard |NID_ext_req| for the attribute type.
1279OPENSSL_EXPORT int X509_REQ_add_extensions(
1280 X509_REQ *req, const STACK_OF(X509_EXTENSION) *exts);
1281
David Benjamin4363bdd2022-04-06 15:19:16 -04001282// X509_REQ_sign signs |req| with |pkey| and replaces the signature algorithm
David Benjamin0c7527b2023-04-25 21:45:00 -04001283// and signature fields. It returns the length of the signature on success and
1284// zero on error. This function uses digest algorithm |md|, or |pkey|'s default
1285// if NULL. Other signing parameters use |pkey|'s defaults. To customize them,
1286// use |X509_REQ_sign_ctx|.
David Benjamin4363bdd2022-04-06 15:19:16 -04001287OPENSSL_EXPORT int X509_REQ_sign(X509_REQ *req, EVP_PKEY *pkey,
1288 const EVP_MD *md);
1289
1290// X509_REQ_sign_ctx signs |req| with |ctx| and replaces the signature algorithm
David Benjamin0c7527b2023-04-25 21:45:00 -04001291// and signature fields. It returns the length of the signature on success and
1292// zero on error. The signature algorithm and parameters come from |ctx|, which
1293// must have been initialized with |EVP_DigestSignInit|. The caller should
1294// configure the corresponding |EVP_PKEY_CTX| before calling this function.
David Benjamin5bef6ec2023-11-13 09:04:43 -05001295//
1296// On success or failure, this function mutates |ctx| and resets it to the empty
1297// state. Caller should not rely on its contents after the function returns.
David Benjamin4363bdd2022-04-06 15:19:16 -04001298OPENSSL_EXPORT int X509_REQ_sign_ctx(X509_REQ *req, EVP_MD_CTX *ctx);
1299
1300// i2d_re_X509_REQ_tbs serializes the CertificationRequestInfo (see RFC 2986)
1301// portion of |req|, as described in |i2d_SAMPLE|.
1302//
1303// This function re-encodes the CertificationRequestInfo and may not reflect
1304// |req|'s original encoding. It may be used to manually generate a signature
1305// for a new certificate request.
1306OPENSSL_EXPORT int i2d_re_X509_REQ_tbs(X509_REQ *req, uint8_t **outp);
1307
1308// X509_REQ_set1_signature_algo sets |req|'s signature algorithm to |algo| and
1309// returns one on success or zero on error.
1310OPENSSL_EXPORT int X509_REQ_set1_signature_algo(X509_REQ *req,
1311 const X509_ALGOR *algo);
1312
1313// X509_REQ_set1_signature_value sets |req|'s signature to a copy of the
1314// |sig_len| bytes pointed by |sig|. It returns one on success and zero on
1315// error.
1316//
1317// Due to a specification error, PKCS#10 certificate requests store signatures
1318// in ASN.1 BIT STRINGs, but signature algorithms return byte strings rather
1319// than bit strings. This function creates a BIT STRING containing a whole
1320// number of bytes, with the bit order matching the DER encoding. This matches
1321// the encoding used by all X.509 signature algorithms.
1322OPENSSL_EXPORT int X509_REQ_set1_signature_value(X509_REQ *req,
1323 const uint8_t *sig,
1324 size_t sig_len);
1325
1326
David Benjaminb6f47e82022-07-09 00:57:54 -04001327// Names.
1328//
1329// An |X509_NAME| represents an X.509 Name structure (RFC 5280). X.509 names are
1330// a complex, hierarchical structure over a collection of attributes. Each name
1331// is sequence of relative distinguished names (RDNs), decreasing in
1332// specificity. For example, the first RDN may specify the country, while the
1333// next RDN may specify a locality. Each RDN is, itself, a set of attributes.
1334// Having more than one attribute in an RDN is uncommon, but possible. Within an
1335// RDN, attributes have the same level in specificity. Attribute types are
1336// OBJECT IDENTIFIERs. This determines the ASN.1 type of the value, which is
1337// commonly a string but may be other types.
1338//
1339// The |X509_NAME| representation flattens this two-level structure into a
1340// single list of attributes. Each attribute is stored in an |X509_NAME_ENTRY|,
1341// with also maintains the index of the RDN it is part of, accessible via
1342// |X509_NAME_ENTRY_set|. This can be used to recover the two-level structure.
1343//
1344// X.509 names are largely vestigial. Historically, DNS names were parsed out of
1345// the subject's common name attribute, but this is deprecated and has since
1346// moved to the subject alternative name extension. In modern usage, X.509 names
1347// are primarily opaque identifiers to link a certificate with its issuer.
1348
1349DEFINE_STACK_OF(X509_NAME_ENTRY)
1350DEFINE_STACK_OF(X509_NAME)
1351
1352// X509_NAME is an |ASN1_ITEM| whose ASN.1 type is X.509 Name (RFC 5280) and C
1353// type is |X509_NAME*|.
1354DECLARE_ASN1_ITEM(X509_NAME)
1355
David Benjamine89d99a2023-12-06 16:23:50 -05001356// X509_NAME_new returns a new, empty |X509_NAME|, or NULL on error.
David Benjaminb6f47e82022-07-09 00:57:54 -04001357OPENSSL_EXPORT X509_NAME *X509_NAME_new(void);
1358
1359// X509_NAME_free releases memory associated with |name|.
1360OPENSSL_EXPORT void X509_NAME_free(X509_NAME *name);
1361
1362// d2i_X509_NAME parses up to |len| bytes from |*inp| as a DER-encoded X.509
David Benjamin19721cd2023-01-16 16:19:03 -05001363// Name (RFC 5280), as described in |d2i_SAMPLE|.
David Benjaminb6f47e82022-07-09 00:57:54 -04001364OPENSSL_EXPORT X509_NAME *d2i_X509_NAME(X509_NAME **out, const uint8_t **inp,
1365 long len);
1366
1367// i2d_X509_NAME marshals |in| as a DER-encoded X.509 Name (RFC 5280), as
1368// described in |i2d_SAMPLE|.
1369//
1370// TODO(https://crbug.com/boringssl/407): This function should be const and
1371// thread-safe but is currently neither in some cases, notably if |in| was
1372// mutated.
1373OPENSSL_EXPORT int i2d_X509_NAME(X509_NAME *in, uint8_t **outp);
1374
1375// X509_NAME_dup returns a newly-allocated copy of |name|, or NULL on error.
1376//
1377// TODO(https://crbug.com/boringssl/407): This function should be const and
1378// thread-safe but is currently neither in some cases, notably if |name| was
1379// mutated.
1380OPENSSL_EXPORT X509_NAME *X509_NAME_dup(X509_NAME *name);
1381
David Benjamina697bcb2023-11-12 19:56:19 -05001382// X509_NAME_cmp compares |a| and |b|'s canonicalized forms. It returns zero if
1383// they are equal, one if |a| sorts after |b|, -1 if |b| sorts after |a|, and -2
1384// on error.
1385//
1386// TODO(https://crbug.com/boringssl/407): This function is const, but it is not
1387// always thread-safe, notably if |name| was mutated.
1388//
1389// TODO(https://crbug.com/boringssl/355): The -2 return is very inconvenient to
1390// pass to a sorting function. Can we make this infallible? In the meantime,
1391// prefer to use this function only for equality checks rather than comparisons.
1392// Although even the library itself passes this to a sorting function.
1393OPENSSL_EXPORT int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b);
1394
David Benjamin2a88b4b2023-12-25 16:16:32 -05001395// X509_NAME_get0_der marshals |name| as a DER-encoded X.509 Name (RFC 5280). On
1396// success, it returns one and sets |*out_der| and |*out_der_len| to a buffer
1397// containing the result. Otherwise, it returns zero. |*out_der| is owned by
1398// |name| and must not be freed by the caller. It is invalidated after |name| is
1399// mutated or freed.
David Benjaminb6f47e82022-07-09 00:57:54 -04001400//
1401// Avoid this function and prefer |i2d_X509_NAME|. It is one of the reasons
David Benjamin2a88b4b2023-12-25 16:16:32 -05001402// |X509_NAME| functions, including this one, are not consistently thread-safe
1403// or const-correct. Depending on the resolution of
1404// https://crbug.com/boringssl/407, this function may be removed or cause poor
1405// performance.
David Benjaminb6f47e82022-07-09 00:57:54 -04001406OPENSSL_EXPORT int X509_NAME_get0_der(X509_NAME *name, const uint8_t **out_der,
1407 size_t *out_der_len);
1408
1409// X509_NAME_set makes a copy of |name|. On success, it frees |*xn|, sets |*xn|
1410// to the copy, and returns one. Otherwise, it returns zero.
1411//
1412// TODO(https://crbug.com/boringssl/407): This function should be const and
1413// thread-safe but is currently neither in some cases, notably if |name| was
1414// mutated.
1415OPENSSL_EXPORT int X509_NAME_set(X509_NAME **xn, X509_NAME *name);
1416
1417// X509_NAME_entry_count returns the number of entries in |name|.
1418OPENSSL_EXPORT int X509_NAME_entry_count(const X509_NAME *name);
1419
1420// X509_NAME_get_index_by_NID returns the zero-based index of the first
1421// attribute in |name| with type |nid|, or -1 if there is none. |nid| should be
1422// one of the |NID_*| constants. If |lastpos| is non-negative, it begins
1423// searching at |lastpos+1|. To search all attributes, pass in -1, not zero.
1424//
1425// Indices from this function refer to |X509_NAME|'s flattened representation.
1426OPENSSL_EXPORT int X509_NAME_get_index_by_NID(const X509_NAME *name, int nid,
1427 int lastpos);
1428
1429// X509_NAME_get_index_by_OBJ behaves like |X509_NAME_get_index_by_NID| but
1430// looks for attributes with type |obj|.
1431OPENSSL_EXPORT int X509_NAME_get_index_by_OBJ(const X509_NAME *name,
1432 const ASN1_OBJECT *obj,
1433 int lastpos);
1434
1435// X509_NAME_get_entry returns the attribute in |name| at index |loc|, or NULL
1436// if |loc| is out of range. |loc| is interpreted using |X509_NAME|'s flattened
1437// representation. This function returns a non-const pointer for OpenSSL
1438// compatibility, but callers should not mutate the result. Doing so will break
1439// internal invariants in the library.
1440OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_get_entry(const X509_NAME *name,
1441 int loc);
1442
1443// X509_NAME_delete_entry removes and returns the attribute in |name| at index
1444// |loc|, or NULL if |loc| is out of range. |loc| is interpreted using
1445// |X509_NAME|'s flattened representation. If the attribute is found, the caller
1446// is responsible for releasing the result with |X509_NAME_ENTRY_free|.
1447//
1448// This function will internally update RDN indices (see |X509_NAME_ENTRY_set|)
1449// so they continue to be consecutive.
1450OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name,
1451 int loc);
1452
1453// X509_NAME_add_entry adds a copy of |entry| to |name| and returns one on
1454// success or zero on error. If |loc| is -1, the entry is appended to |name|.
1455// Otherwise, it is inserted at index |loc|. If |set| is -1, the entry is added
1456// to the previous entry's RDN. If it is 0, the entry becomes a singleton RDN.
1457// If 1, it is added to next entry's RDN.
1458//
1459// This function will internally update RDN indices (see |X509_NAME_ENTRY_set|)
1460// so they continue to be consecutive.
1461OPENSSL_EXPORT int X509_NAME_add_entry(X509_NAME *name,
1462 const X509_NAME_ENTRY *entry, int loc,
1463 int set);
1464
1465// X509_NAME_add_entry_by_OBJ adds a new entry to |name| and returns one on
1466// success or zero on error. The entry's attribute type is |obj|. The entry's
1467// attribute value is determined by |type|, |bytes|, and |len|, as in
1468// |X509_NAME_ENTRY_set_data|. The entry's position is determined by |loc| and
David Benjamina028a5e2023-02-06 12:22:07 -05001469// |set| as in |X509_NAME_add_entry|.
David Benjaminb6f47e82022-07-09 00:57:54 -04001470OPENSSL_EXPORT int X509_NAME_add_entry_by_OBJ(X509_NAME *name,
1471 const ASN1_OBJECT *obj, int type,
David Benjamin32b51302023-03-25 01:42:18 -04001472 const uint8_t *bytes,
1473 ossl_ssize_t len, int loc,
1474 int set);
David Benjaminb6f47e82022-07-09 00:57:54 -04001475
1476// X509_NAME_add_entry_by_NID behaves like |X509_NAME_add_entry_by_OBJ| but sets
1477// the entry's attribute type to |nid|, which should be one of the |NID_*|
1478// constants.
1479OPENSSL_EXPORT int X509_NAME_add_entry_by_NID(X509_NAME *name, int nid,
1480 int type, const uint8_t *bytes,
David Benjamin32b51302023-03-25 01:42:18 -04001481 ossl_ssize_t len, int loc,
1482 int set);
David Benjaminb6f47e82022-07-09 00:57:54 -04001483
1484// X509_NAME_add_entry_by_txt behaves like |X509_NAME_add_entry_by_OBJ| but sets
1485// the entry's attribute type to |field|, which is passed to |OBJ_txt2obj|.
1486OPENSSL_EXPORT int X509_NAME_add_entry_by_txt(X509_NAME *name,
1487 const char *field, int type,
David Benjamin32b51302023-03-25 01:42:18 -04001488 const uint8_t *bytes,
1489 ossl_ssize_t len, int loc,
1490 int set);
David Benjaminb6f47e82022-07-09 00:57:54 -04001491
David Benjamine89d99a2023-12-06 16:23:50 -05001492// X509_NAME_ENTRY_new returns a new, empty |X509_NAME_ENTRY|, or NULL on error.
David Benjaminb6f47e82022-07-09 00:57:54 -04001493OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_new(void);
1494
1495// X509_NAME_ENTRY_free releases memory associated with |entry|.
1496OPENSSL_EXPORT void X509_NAME_ENTRY_free(X509_NAME_ENTRY *entry);
1497
David Benjaminb6f47e82022-07-09 00:57:54 -04001498// X509_NAME_ENTRY_dup returns a newly-allocated copy of |entry|, or NULL on
1499// error.
1500OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_dup(
1501 const X509_NAME_ENTRY *entry);
1502
1503// X509_NAME_ENTRY_get_object returns |entry|'s attribute type. This function
1504// returns a non-const pointer for OpenSSL compatibility, but callers should not
1505// mutate the result. Doing so will break internal invariants in the library.
1506OPENSSL_EXPORT ASN1_OBJECT *X509_NAME_ENTRY_get_object(
1507 const X509_NAME_ENTRY *entry);
1508
1509// X509_NAME_ENTRY_set_object sets |entry|'s attribute type to |obj|. It returns
1510// one on success and zero on error.
1511OPENSSL_EXPORT int X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *entry,
1512 const ASN1_OBJECT *obj);
1513
1514// X509_NAME_ENTRY_get_data returns |entry|'s attribute value, represented as an
1515// |ASN1_STRING|. This value may have any ASN.1 type, so callers must check the
1516// type before interpreting the contents. This function returns a non-const
1517// pointer for OpenSSL compatibility, but callers should not mutate the result.
1518// Doing so will break internal invariants in the library.
1519//
1520// TODO(https://crbug.com/boringssl/412): Although the spec says any ASN.1 type
1521// is allowed, we currently only allow an ad-hoc set of types. Additionally, it
1522// is unclear if some types can even be represented by this function.
1523OPENSSL_EXPORT ASN1_STRING *X509_NAME_ENTRY_get_data(
1524 const X509_NAME_ENTRY *entry);
1525
1526// X509_NAME_ENTRY_set_data sets |entry|'s value to |len| bytes from |bytes|. It
1527// returns one on success and zero on error. If |len| is -1, |bytes| must be a
1528// NUL-terminated C string and the length is determined by |strlen|. |bytes| is
1529// converted to an ASN.1 type as follows:
1530//
1531// If |type| is a |MBSTRING_*| constant, the value is an ASN.1 string. The
1532// string is determined by decoding |bytes| in the encoding specified by |type|,
1533// and then re-encoding it in a form appropriate for |entry|'s attribute type.
1534// See |ASN1_STRING_set_by_NID| for details.
1535//
1536// Otherwise, the value is an |ASN1_STRING| with type |type| and value |bytes|.
1537// See |ASN1_STRING| for how to format ASN.1 types as an |ASN1_STRING|. If
1538// |type| is |V_ASN1_UNDEF| the previous |ASN1_STRING| type is reused.
1539OPENSSL_EXPORT int X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *entry, int type,
David Benjamin32b51302023-03-25 01:42:18 -04001540 const uint8_t *bytes,
1541 ossl_ssize_t len);
David Benjaminb6f47e82022-07-09 00:57:54 -04001542
1543// X509_NAME_ENTRY_set returns the zero-based index of the RDN which contains
1544// |entry|. Consecutive entries with the same index are part of the same RDN.
1545OPENSSL_EXPORT int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *entry);
1546
1547// X509_NAME_ENTRY_create_by_OBJ creates a new |X509_NAME_ENTRY| with attribute
1548// type |obj|. The attribute value is determined from |type|, |bytes|, and |len|
1549// as in |X509_NAME_ENTRY_set_data|. It returns the |X509_NAME_ENTRY| on success
1550// and NULL on error.
1551//
1552// If |out| is non-NULL and |*out| is NULL, it additionally sets |*out| to the
1553// result on success. If both |out| and |*out| are non-NULL, it updates the
1554// object at |*out| instead of allocating a new one.
1555OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(
1556 X509_NAME_ENTRY **out, const ASN1_OBJECT *obj, int type,
David Benjamin32b51302023-03-25 01:42:18 -04001557 const uint8_t *bytes, ossl_ssize_t len);
David Benjaminb6f47e82022-07-09 00:57:54 -04001558
1559// X509_NAME_ENTRY_create_by_NID behaves like |X509_NAME_ENTRY_create_by_OBJ|
1560// except the attribute type is |nid|, which should be one of the |NID_*|
1561// constants.
1562OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(
David Benjamin32b51302023-03-25 01:42:18 -04001563 X509_NAME_ENTRY **out, int nid, int type, const uint8_t *bytes,
1564 ossl_ssize_t len);
David Benjaminb6f47e82022-07-09 00:57:54 -04001565
1566// X509_NAME_ENTRY_create_by_txt behaves like |X509_NAME_ENTRY_create_by_OBJ|
1567// except the attribute type is |field|, which is passed to |OBJ_txt2obj|.
1568OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_txt(
1569 X509_NAME_ENTRY **out, const char *field, int type, const uint8_t *bytes,
David Benjamin32b51302023-03-25 01:42:18 -04001570 ossl_ssize_t len);
David Benjaminb6f47e82022-07-09 00:57:54 -04001571
1572
David Benjamin4e400352023-11-12 19:21:55 -05001573// Public keys.
1574//
1575// X.509 encodes public keys as SubjectPublicKeyInfo (RFC 5280), sometimes
1576// referred to as SPKI. These are represented in this library by |X509_PUBKEY|.
1577
David Benjamin4e400352023-11-12 19:21:55 -05001578// X509_PUBKEY_new returns a newly-allocated, empty |X509_PUBKEY| object, or
1579// NULL on error.
1580OPENSSL_EXPORT X509_PUBKEY *X509_PUBKEY_new(void);
1581
1582// X509_PUBKEY_free releases memory associated with |key|.
1583OPENSSL_EXPORT void X509_PUBKEY_free(X509_PUBKEY *key);
1584
1585// d2i_X509_PUBKEY parses up to |len| bytes from |*inp| as a DER-encoded
1586// SubjectPublicKeyInfo, as described in |d2i_SAMPLE|.
1587OPENSSL_EXPORT X509_PUBKEY *d2i_X509_PUBKEY(X509_PUBKEY **out,
1588 const uint8_t **inp, long len);
1589
1590// i2d_X509_PUBKEY marshals |key| as a DER-encoded SubjectPublicKeyInfo, as
1591// described in |i2d_SAMPLE|.
1592OPENSSL_EXPORT int i2d_X509_PUBKEY(const X509_PUBKEY *key, uint8_t **outp);
1593
1594// X509_PUBKEY_set serializes |pkey| into a newly-allocated |X509_PUBKEY|
1595// structure. On success, it frees |*x| if non-NULL, then sets |*x| to the new
1596// object, and returns one. Otherwise, it returns zero.
1597OPENSSL_EXPORT int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey);
1598
David Benjamin5b3dc492023-12-21 19:19:16 -05001599// X509_PUBKEY_get0 returns |key| as an |EVP_PKEY|, or NULL if |key| either
1600// could not be parsed or is an unrecognized algorithm. The |EVP_PKEY| is cached
1601// in |key|, so callers must not mutate the result.
1602OPENSSL_EXPORT EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key);
1603
1604// X509_PUBKEY_get behaves like |X509_PUBKEY_get0| but increments the reference
1605// count on the |EVP_PKEY|. The caller must release the result with
1606// |EVP_PKEY_free| when done. The |EVP_PKEY| is cached in |key|, so callers must
1607// not mutate the result.
1608OPENSSL_EXPORT EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key);
David Benjamin4e400352023-11-12 19:21:55 -05001609
1610// X509_PUBKEY_set0_param sets |pub| to a key with AlgorithmIdentifier
1611// determined by |obj|, |param_type|, and |param_value|, and an encoded
1612// public key of |key|. On success, it gives |pub| ownership of all the other
1613// parameters and returns one. Otherwise, it returns zero. |key| must have been
1614// allocated by |OPENSSL_malloc|. |obj| and, if applicable, |param_value| must
1615// not be freed after a successful call, and must have been allocated in a
1616// manner compatible with |ASN1_OBJECT_free| or |ASN1_STRING_free|.
1617//
1618// |obj|, |param_type|, and |param_value| are interpreted as in
1619// |X509_ALGOR_set0|. See |X509_ALGOR_set0| for details.
1620OPENSSL_EXPORT int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *obj,
1621 int param_type, void *param_value,
1622 uint8_t *key, int key_len);
1623
1624// X509_PUBKEY_get0_param outputs fields of |pub| and returns one. If |out_obj|
1625// is not NULL, it sets |*out_obj| to AlgorithmIdentifier's OID. If |out_key|
1626// is not NULL, it sets |*out_key| and |*out_key_len| to the encoded public key.
1627// If |out_alg| is not NULL, it sets |*out_alg| to the AlgorithmIdentifier.
1628//
1629// All pointers outputted by this function are internal to |pub| and must not be
1630// freed by the caller. Additionally, although some outputs are non-const,
1631// callers must not mutate the resulting objects.
1632//
1633// Note: X.509 SubjectPublicKeyInfo structures store the encoded public key as a
1634// BIT STRING. |*out_key| and |*out_key_len| will silently pad the key with zero
1635// bits if |pub| did not contain a whole number of bytes. Use
1636// |X509_PUBKEY_get0_public_key| to preserve this information.
1637OPENSSL_EXPORT int X509_PUBKEY_get0_param(ASN1_OBJECT **out_obj,
1638 const uint8_t **out_key,
1639 int *out_key_len,
1640 X509_ALGOR **out_alg,
1641 X509_PUBKEY *pub);
1642
1643// X509_PUBKEY_get0_public_key returns |pub|'s encoded public key.
1644OPENSSL_EXPORT const ASN1_BIT_STRING *X509_PUBKEY_get0_public_key(
1645 const X509_PUBKEY *pub);
1646
1647
David Benjamin557b80f2022-07-10 19:12:12 -04001648// Extensions.
1649//
1650// X.509 certificates and CRLs may contain a list of extensions (RFC 5280).
1651// Extensions have a type, specified by an object identifier (|ASN1_OBJECT|) and
1652// a byte string value, which should a DER-encoded structure whose type is
1653// determined by the extension type. This library represents extensions with the
1654// |X509_EXTENSION| type.
1655
David Benjamin538b2a62023-12-18 13:33:36 -05001656// X509_EXTENSION is an |ASN1_ITEM| whose ASN.1 type is X.509 Extension (RFC
1657// 5280) and C type is |X509_EXTENSION*|.
1658DECLARE_ASN1_ITEM(X509_EXTENSION)
1659
David Benjamin557b80f2022-07-10 19:12:12 -04001660// X509_EXTENSION_new returns a newly-allocated, empty |X509_EXTENSION| object
1661// or NULL on error.
1662OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_new(void);
1663
1664// X509_EXTENSION_free releases memory associated with |ex|.
1665OPENSSL_EXPORT void X509_EXTENSION_free(X509_EXTENSION *ex);
1666
1667// d2i_X509_EXTENSION parses up to |len| bytes from |*inp| as a DER-encoded
David Benjamin19721cd2023-01-16 16:19:03 -05001668// X.509 Extension (RFC 5280), as described in |d2i_SAMPLE|.
David Benjamin557b80f2022-07-10 19:12:12 -04001669OPENSSL_EXPORT X509_EXTENSION *d2i_X509_EXTENSION(X509_EXTENSION **out,
1670 const uint8_t **inp,
1671 long len);
1672
David Benjamin2e13e362023-03-23 00:22:54 -04001673// i2d_X509_EXTENSION marshals |ex| as a DER-encoded X.509 Extension (RFC
David Benjamin557b80f2022-07-10 19:12:12 -04001674// 5280), as described in |i2d_SAMPLE|.
David Benjamin2e13e362023-03-23 00:22:54 -04001675OPENSSL_EXPORT int i2d_X509_EXTENSION(const X509_EXTENSION *ex, uint8_t **outp);
David Benjamin557b80f2022-07-10 19:12:12 -04001676
1677// X509_EXTENSION_dup returns a newly-allocated copy of |ex|, or NULL on error.
1678// This function works by serializing the structure, so if |ex| is incomplete,
1679// it may fail.
1680OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_dup(const X509_EXTENSION *ex);
1681
1682// X509_EXTENSION_create_by_NID creates a new |X509_EXTENSION| with type |nid|,
1683// value |data|, and critical bit |crit|. It returns an |X509_EXTENSION| on
1684// success, and NULL on error. |nid| should be a |NID_*| constant.
1685//
1686// If |ex| and |*ex| are both non-NULL, |*ex| is used to hold the result,
1687// otherwise a new object is allocated. If |ex| is non-NULL and |*ex| is NULL,
1688// the function sets |*ex| to point to the newly allocated result, in addition
1689// to returning the result.
1690OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_create_by_NID(
1691 X509_EXTENSION **ex, int nid, int crit, const ASN1_OCTET_STRING *data);
1692
1693// X509_EXTENSION_create_by_OBJ behaves like |X509_EXTENSION_create_by_NID|, but
1694// the extension type is determined by an |ASN1_OBJECT|.
1695OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_create_by_OBJ(
1696 X509_EXTENSION **ex, const ASN1_OBJECT *obj, int crit,
1697 const ASN1_OCTET_STRING *data);
1698
1699// X509_EXTENSION_get_object returns |ex|'s extension type. This function
1700// returns a non-const pointer for OpenSSL compatibility, but callers should not
1701// mutate the result.
1702OPENSSL_EXPORT ASN1_OBJECT *X509_EXTENSION_get_object(const X509_EXTENSION *ex);
1703
1704// X509_EXTENSION_get_data returns |ne|'s extension value. This function returns
1705// a non-const pointer for OpenSSL compatibility, but callers should not mutate
1706// the result.
1707OPENSSL_EXPORT ASN1_OCTET_STRING *X509_EXTENSION_get_data(
1708 const X509_EXTENSION *ne);
1709
1710// X509_EXTENSION_get_critical returns one if |ex| is critical and zero
1711// otherwise.
1712OPENSSL_EXPORT int X509_EXTENSION_get_critical(const X509_EXTENSION *ex);
1713
1714// X509_EXTENSION_set_object sets |ex|'s extension type to |obj|. It returns one
1715// on success and zero on error.
1716OPENSSL_EXPORT int X509_EXTENSION_set_object(X509_EXTENSION *ex,
1717 const ASN1_OBJECT *obj);
1718
1719// X509_EXTENSION_set_critical sets |ex| to critical if |crit| is non-zero and
1720// to non-critical if |crit| is zero.
1721OPENSSL_EXPORT int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit);
1722
1723// X509_EXTENSION_set_data set's |ex|'s extension value to a copy of |data|. It
1724// returns one on success and zero on error.
1725OPENSSL_EXPORT int X509_EXTENSION_set_data(X509_EXTENSION *ex,
1726 const ASN1_OCTET_STRING *data);
1727
1728
1729// Extension lists.
1730//
1731// The following functions manipulate lists of extensions. Most of them have
1732// corresponding functions on the containing |X509|, |X509_CRL|, or
1733// |X509_REVOKED|.
1734
1735DEFINE_STACK_OF(X509_EXTENSION)
1736typedef STACK_OF(X509_EXTENSION) X509_EXTENSIONS;
1737
David Benjamin557b80f2022-07-10 19:12:12 -04001738// d2i_X509_EXTENSIONS parses up to |len| bytes from |*inp| as a DER-encoded
David Benjamin19721cd2023-01-16 16:19:03 -05001739// SEQUENCE OF Extension (RFC 5280), as described in |d2i_SAMPLE|.
David Benjamin557b80f2022-07-10 19:12:12 -04001740OPENSSL_EXPORT X509_EXTENSIONS *d2i_X509_EXTENSIONS(X509_EXTENSIONS **out,
1741 const uint8_t **inp,
1742 long len);
1743
1744// i2d_X509_EXTENSIONS marshals |alg| as a DER-encoded SEQUENCE OF Extension
1745// (RFC 5280), as described in |i2d_SAMPLE|.
1746OPENSSL_EXPORT int i2d_X509_EXTENSIONS(const X509_EXTENSIONS *alg,
1747 uint8_t **outp);
1748
1749// X509v3_get_ext_count returns the number of extensions in |x|.
1750OPENSSL_EXPORT int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x);
1751
1752// X509v3_get_ext_by_NID returns the index of the first extension in |x| with
1753// type |nid|, or a negative number if not found. If found, callers can use
1754// |X509v3_get_ext| to look up the extension by index.
1755//
1756// If |lastpos| is non-negative, it begins searching at |lastpos| + 1. Callers
1757// can thus loop over all matching extensions by first passing -1 and then
1758// passing the previously-returned value until no match is returned.
1759OPENSSL_EXPORT int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x,
1760 int nid, int lastpos);
1761
1762// X509v3_get_ext_by_OBJ behaves like |X509v3_get_ext_by_NID| but looks for
1763// extensions matching |obj|.
1764OPENSSL_EXPORT int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *x,
1765 const ASN1_OBJECT *obj, int lastpos);
1766
1767// X509v3_get_ext_by_critical returns the index of the first extension in |x|
1768// whose critical bit matches |crit|, or a negative number if no such extension
1769// was found.
1770//
1771// If |lastpos| is non-negative, it begins searching at |lastpos| + 1. Callers
1772// can thus loop over all matching extensions by first passing -1 and then
1773// passing the previously-returned value until no match is returned.
1774OPENSSL_EXPORT int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *x,
1775 int crit, int lastpos);
1776
1777// X509v3_get_ext returns the extension in |x| at index |loc|, or NULL if |loc|
1778// is out of bounds. This function returns a non-const pointer for OpenSSL
1779// compatibility, but callers should not mutate the result.
1780OPENSSL_EXPORT X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x,
1781 int loc);
1782
1783// X509v3_delete_ext removes the extension in |x| at index |loc| and returns the
1784// removed extension, or NULL if |loc| was out of bounds. If an extension was
1785// returned, the caller must release it with |X509_EXTENSION_free|.
1786OPENSSL_EXPORT X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x,
1787 int loc);
1788
1789// X509v3_add_ext adds a copy of |ex| to the extension list in |*x|. If |*x| is
1790// NULL, it allocates a new |STACK_OF(X509_EXTENSION)| to hold the copy and sets
1791// |*x| to the new list. It returns |*x| on success and NULL on error. The
1792// caller retains ownership of |ex| and can release it independently of |*x|.
1793//
1794// The new extension is inserted at index |loc|, shifting extensions to the
1795// right. If |loc| is -1 or out of bounds, the new extension is appended to the
1796// list.
1797OPENSSL_EXPORT STACK_OF(X509_EXTENSION) *X509v3_add_ext(
1798 STACK_OF(X509_EXTENSION) **x, const X509_EXTENSION *ex, int loc);
1799
1800
David Benjamine89d99a2023-12-06 16:23:50 -05001801// General names.
1802//
1803// A |GENERAL_NAME| represents an X.509 GeneralName structure, defined in RFC
1804// 5280, Section 4.2.1.6. General names are distinct from names (|X509_NAME|). A
1805// general name is a CHOICE type which may contain one of several name types,
1806// most commonly a DNS name or an IP address. General names most commonly appear
1807// in the subject alternative name (SAN) extension, though they are also used in
1808// other extensions.
1809//
1810// Many extensions contain a SEQUENCE OF GeneralName, or GeneralNames, so
1811// |STACK_OF(GENERAL_NAME)| is defined and aliased to |GENERAL_NAMES|.
1812
1813typedef struct otherName_st {
1814 ASN1_OBJECT *type_id;
1815 ASN1_TYPE *value;
1816} OTHERNAME;
1817
1818typedef struct EDIPartyName_st {
1819 ASN1_STRING *nameAssigner;
1820 ASN1_STRING *partyName;
1821} EDIPARTYNAME;
1822
1823// GEN_* are constants for the |type| field of |GENERAL_NAME|, defined below.
1824#define GEN_OTHERNAME 0
1825#define GEN_EMAIL 1
1826#define GEN_DNS 2
1827#define GEN_X400 3
1828#define GEN_DIRNAME 4
1829#define GEN_EDIPARTY 5
1830#define GEN_URI 6
1831#define GEN_IPADD 7
1832#define GEN_RID 8
1833
1834// A |GENERAL_NAME_st|, aka |GENERAL_NAME|, represents an X.509 GeneralName. The
1835// |type| field determines which member of |d| is active. A |GENERAL_NAME| may
1836// also be empty, in which case |type| is -1 and |d| is NULL. Empty
1837// |GENERAL_NAME|s are invalid and will never be returned from the parser, but
1838// may be created temporarily, e.g. by |GENERAL_NAME_new|.
1839struct GENERAL_NAME_st {
1840 int type;
1841 union {
1842 char *ptr;
1843 OTHERNAME *otherName;
1844 ASN1_IA5STRING *rfc822Name;
1845 ASN1_IA5STRING *dNSName;
1846 ASN1_STRING *x400Address;
1847 X509_NAME *directoryName;
1848 EDIPARTYNAME *ediPartyName;
1849 ASN1_IA5STRING *uniformResourceIdentifier;
1850 ASN1_OCTET_STRING *iPAddress;
1851 ASN1_OBJECT *registeredID;
1852
1853 // Old names
1854 ASN1_OCTET_STRING *ip; // iPAddress
1855 X509_NAME *dirn; // dirn
1856 ASN1_IA5STRING *ia5; // rfc822Name, dNSName, uniformResourceIdentifier
1857 ASN1_OBJECT *rid; // registeredID
1858 } d;
1859} /* GENERAL_NAME */;
1860
1861// GENERAL_NAME_new returns a new, empty |GENERAL_NAME|, or NULL on error.
1862OPENSSL_EXPORT GENERAL_NAME *GENERAL_NAME_new(void);
1863
1864// GENERAL_NAME_free releases memory associated with |gen|.
1865OPENSSL_EXPORT void GENERAL_NAME_free(GENERAL_NAME *gen);
1866
1867// d2i_GENERAL_NAME parses up to |len| bytes from |*inp| as a DER-encoded X.509
1868// GeneralName (RFC 5280), as described in |d2i_SAMPLE|.
1869OPENSSL_EXPORT GENERAL_NAME *d2i_GENERAL_NAME(GENERAL_NAME **out,
1870 const uint8_t **inp, long len);
1871
1872// i2d_GENERAL_NAME marshals |in| as a DER-encoded X.509 GeneralName (RFC 5280),
1873// as described in |i2d_SAMPLE|.
1874//
1875// TODO(https://crbug.com/boringssl/407): This function should be const and
1876// thread-safe but is currently neither in some cases, notably if |in| is an
1877// directoryName and the |X509_NAME| has been modified.
1878OPENSSL_EXPORT int i2d_GENERAL_NAME(GENERAL_NAME *in, uint8_t **outp);
1879
1880// GENERAL_NAME_dup returns a newly-allocated copy of |gen|, or NULL on error.
1881// This function works by serializing the structure, so it will fail if |gen| is
1882// empty.
1883//
1884// TODO(https://crbug.com/boringssl/407): This function should be const and
1885// thread-safe but is currently neither in some cases, notably if |gen| is an
1886// directoryName and the |X509_NAME| has been modified.
1887OPENSSL_EXPORT GENERAL_NAME *GENERAL_NAME_dup(GENERAL_NAME *gen);
1888
1889// GENERAL_NAMES_new returns a new, empty |GENERAL_NAMES|, or NULL on error.
1890OPENSSL_EXPORT GENERAL_NAMES *GENERAL_NAMES_new(void);
1891
1892// GENERAL_NAMES_free releases memory associated with |gens|.
1893OPENSSL_EXPORT void GENERAL_NAMES_free(GENERAL_NAMES *gens);
1894
1895// d2i_GENERAL_NAMES parses up to |len| bytes from |*inp| as a DER-encoded
1896// SEQUENCE OF GeneralName, as described in |d2i_SAMPLE|.
1897OPENSSL_EXPORT GENERAL_NAMES *d2i_GENERAL_NAMES(GENERAL_NAMES **out,
1898 const uint8_t **inp, long len);
1899
1900// i2d_GENERAL_NAMES marshals |in| as a DER-encoded SEQUENCE OF GeneralName, as
1901// described in |i2d_SAMPLE|.
1902//
1903// TODO(https://crbug.com/boringssl/407): This function should be const and
1904// thread-safe but is currently neither in some cases, notably if some element
1905// of |in| is an directoryName and the |X509_NAME| has been modified.
1906OPENSSL_EXPORT int i2d_GENERAL_NAMES(GENERAL_NAMES *in, uint8_t **outp);
1907
1908// OTHERNAME_new returns a new, empty |OTHERNAME|, or NULL on error.
1909OPENSSL_EXPORT OTHERNAME *OTHERNAME_new(void);
1910
1911// OTHERNAME_free releases memory associated with |name|.
1912OPENSSL_EXPORT void OTHERNAME_free(OTHERNAME *name);
1913
1914// EDIPARTYNAME_new returns a new, empty |EDIPARTYNAME|, or NULL on error.
1915// EDIPartyName is rarely used in practice, so callers are unlikely to need this
1916// function.
1917OPENSSL_EXPORT EDIPARTYNAME *EDIPARTYNAME_new(void);
1918
1919// EDIPARTYNAME_free releases memory associated with |name|. EDIPartyName is
1920// rarely used in practice, so callers are unlikely to need this function.
1921OPENSSL_EXPORT void EDIPARTYNAME_free(EDIPARTYNAME *name);
1922
1923// GENERAL_NAME_set0_value set |gen|'s type and value to |type| and |value|.
1924// |type| must be a |GEN_*| constant and |value| must be an object of the
1925// corresponding type. |gen| takes ownership of |value|, so |value| must have
1926// been an allocated object.
1927//
1928// WARNING: |gen| must be empty (typically as returned from |GENERAL_NAME_new|)
1929// before calling this function. If |gen| already contained a value, the
1930// previous contents will be leaked.
1931OPENSSL_EXPORT void GENERAL_NAME_set0_value(GENERAL_NAME *gen, int type,
1932 void *value);
1933
1934// GENERAL_NAME_get0_value returns the in-memory representation of |gen|'s
1935// contents and, |out_type| is not NULL, sets |*out_type| to the type of |gen|,
1936// which will be a |GEN_*| constant. If |gen| is incomplete, the return value
1937// will be NULL and the type will be -1.
1938//
1939// WARNING: Casting the result of this function to the wrong type is a
1940// potentially exploitable memory error. Callers must check |gen|'s type, either
1941// via |*out_type| or checking |gen->type| directly, before inspecting the
1942// result.
1943//
1944// WARNING: This function is not const-correct. The return value should be
1945// const. Callers shoudl not mutate the returned object.
1946OPENSSL_EXPORT void *GENERAL_NAME_get0_value(const GENERAL_NAME *gen,
1947 int *out_type);
1948
1949// GENERAL_NAME_set0_othername sets |gen| to be an OtherName with type |oid| and
1950// value |value|. On success, it returns one and takes ownership of |oid| and
1951// |value|, which must be created in a way compatible with |ASN1_OBJECT_free|
1952// and |ASN1_TYPE_free|, respectively. On allocation failure, it returns zero.
1953// In the failure case, the caller retains ownership of |oid| and |value| and
1954// must release them when done.
1955//
1956// WARNING: |gen| must be empty (typically as returned from |GENERAL_NAME_new|)
1957// before calling this function. If |gen| already contained a value, the
1958// previously contents will be leaked.
1959OPENSSL_EXPORT int GENERAL_NAME_set0_othername(GENERAL_NAME *gen,
1960 ASN1_OBJECT *oid,
1961 ASN1_TYPE *value);
1962
1963// GENERAL_NAME_get0_otherName, if |gen| is an OtherName, sets |*out_oid| and
1964// |*out_value| to the OtherName's type-id and value, respectively, and returns
1965// one. If |gen| is not an OtherName, it returns zero and leaves |*out_oid| and
1966// |*out_value| unmodified. Either of |out_oid| or |out_value| may be NULL to
1967// ignore the value.
1968//
1969// WARNING: This function is not const-correct. |out_oid| and |out_value| are
1970// not const, but callers should not mutate the resulting objects.
1971OPENSSL_EXPORT int GENERAL_NAME_get0_otherName(const GENERAL_NAME *gen,
1972 ASN1_OBJECT **out_oid,
1973 ASN1_TYPE **out_value);
1974
1975
David Benjamin4363bdd2022-04-06 15:19:16 -04001976// Algorithm identifiers.
1977//
1978// An |X509_ALGOR| represents an AlgorithmIdentifier structure, used in X.509
1979// to represent signature algorithms and public key algorithms.
1980
1981DEFINE_STACK_OF(X509_ALGOR)
David Benjamin28d72522021-08-08 17:01:10 -04001982
David Benjamin28127932022-06-25 21:30:08 -04001983// X509_ALGOR is an |ASN1_ITEM| whose ASN.1 type is AlgorithmIdentifier and C
1984// type is |X509_ALGOR*|.
1985DECLARE_ASN1_ITEM(X509_ALGOR)
1986
1987// X509_ALGOR_new returns a newly-allocated, empty |X509_ALGOR| object, or NULL
1988// on error.
1989OPENSSL_EXPORT X509_ALGOR *X509_ALGOR_new(void);
1990
David Benjaminb8596422022-07-10 01:08:07 -04001991// X509_ALGOR_dup returns a newly-allocated copy of |alg|, or NULL on error.
1992// This function works by serializing the structure, so if |alg| is incomplete,
1993// it may fail.
1994OPENSSL_EXPORT X509_ALGOR *X509_ALGOR_dup(const X509_ALGOR *alg);
1995
David Benjamin28127932022-06-25 21:30:08 -04001996// X509_ALGOR_free releases memory associated with |alg|.
1997OPENSSL_EXPORT void X509_ALGOR_free(X509_ALGOR *alg);
1998
1999// d2i_X509_ALGOR parses up to |len| bytes from |*inp| as a DER-encoded
David Benjamin19721cd2023-01-16 16:19:03 -05002000// AlgorithmIdentifier, as described in |d2i_SAMPLE|.
David Benjamin28127932022-06-25 21:30:08 -04002001OPENSSL_EXPORT X509_ALGOR *d2i_X509_ALGOR(X509_ALGOR **out, const uint8_t **inp,
2002 long len);
2003
2004// i2d_X509_ALGOR marshals |alg| as a DER-encoded AlgorithmIdentifier, as
2005// described in |i2d_SAMPLE|.
2006OPENSSL_EXPORT int i2d_X509_ALGOR(const X509_ALGOR *alg, uint8_t **outp);
David Benjamin28d72522021-08-08 17:01:10 -04002007
David Benjamin4363bdd2022-04-06 15:19:16 -04002008// X509_ALGOR_set0 sets |alg| to an AlgorithmIdentifier with algorithm |obj| and
2009// parameter determined by |param_type| and |param_value|. It returns one on
2010// success and zero on error. This function takes ownership of |obj| and
2011// |param_value| on success.
2012//
2013// If |param_type| is |V_ASN1_UNDEF|, the parameter is omitted. If |param_type|
2014// is zero, the parameter is left unchanged. Otherwise, |param_type| and
2015// |param_value| are interpreted as in |ASN1_TYPE_set|.
2016//
2017// Note omitting the parameter (|V_ASN1_UNDEF|) and encoding an explicit NULL
2018// value (|V_ASN1_NULL|) are different. Some algorithms require one and some the
2019// other. Consult the relevant specification before calling this function. The
2020// correct parameter for an RSASSA-PKCS1-v1_5 signature is |V_ASN1_NULL|. The
2021// correct one for an ECDSA or Ed25519 signature is |V_ASN1_UNDEF|.
2022OPENSSL_EXPORT int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *obj,
2023 int param_type, void *param_value);
2024
2025// X509_ALGOR_get0 sets |*out_obj| to the |alg|'s algorithm. If |alg|'s
2026// parameter is omitted, it sets |*out_param_type| and |*out_param_value| to
2027// |V_ASN1_UNDEF| and NULL. Otherwise, it sets |*out_param_type| and
2028// |*out_param_value| to the parameter, using the same representation as
2029// |ASN1_TYPE_set0|. See |ASN1_TYPE_set0| and |ASN1_TYPE| for details.
2030//
2031// Callers that require the parameter in serialized form should, after checking
2032// for |V_ASN1_UNDEF|, use |ASN1_TYPE_set1| and |d2i_ASN1_TYPE|, rather than
2033// inspecting |*out_param_value|.
2034//
2035// Each of |out_obj|, |out_param_type|, and |out_param_value| may be NULL to
2036// ignore the output. If |out_param_type| is NULL, |out_param_value| is ignored.
2037//
2038// WARNING: If |*out_param_type| is set to |V_ASN1_UNDEF|, OpenSSL and older
2039// revisions of BoringSSL leave |*out_param_value| unset rather than setting it
2040// to NULL. Callers that support both OpenSSL and BoringSSL should not assume
2041// |*out_param_value| is uniformly initialized.
2042OPENSSL_EXPORT void X509_ALGOR_get0(const ASN1_OBJECT **out_obj,
2043 int *out_param_type,
2044 const void **out_param_value,
2045 const X509_ALGOR *alg);
2046
2047// X509_ALGOR_set_md sets |alg| to the hash function |md|. Note this
2048// AlgorithmIdentifier represents the hash function itself, not a signature
2049// algorithm that uses |md|.
2050OPENSSL_EXPORT void X509_ALGOR_set_md(X509_ALGOR *alg, const EVP_MD *md);
2051
2052// X509_ALGOR_cmp returns zero if |a| and |b| are equal, and some non-zero value
2053// otherwise. Note this function can only be used for equality checks, not an
2054// ordering.
2055OPENSSL_EXPORT int X509_ALGOR_cmp(const X509_ALGOR *a, const X509_ALGOR *b);
2056
2057
David Benjamin787713b2023-04-19 10:28:13 -04002058// Attributes.
2059//
2060// Unlike certificates and CRLs, CSRs use a separate Attribute structure (RFC
2061// 2985, RFC 2986) for extensibility. This is represented by the library as
2062// |X509_ATTRIBUTE|.
2063
2064DEFINE_STACK_OF(X509_ATTRIBUTE)
2065
David Benjamin787713b2023-04-19 10:28:13 -04002066// X509_ATTRIBUTE_new returns a newly-allocated, empty |X509_ATTRIBUTE| object,
2067// or NULL on error. |X509_ATTRIBUTE_set1_*| may be used to finish initializing
2068// it.
2069OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_new(void);
2070
2071// X509_ATTRIBUTE_dup returns a newly-allocated copy of |attr|, or NULL on
2072// error. This function works by serializing the structure, so if |attr| is
2073// incomplete, it may fail.
2074OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_dup(const X509_ATTRIBUTE *attr);
2075
2076// X509_ATTRIBUTE_free releases memory associated with |attr|.
2077OPENSSL_EXPORT void X509_ATTRIBUTE_free(X509_ATTRIBUTE *attr);
2078
2079// d2i_X509_ATTRIBUTE parses up to |len| bytes from |*inp| as a DER-encoded
2080// Attribute (RFC 2986), as described in |d2i_SAMPLE|.
2081OPENSSL_EXPORT X509_ATTRIBUTE *d2i_X509_ATTRIBUTE(X509_ATTRIBUTE **out,
2082 const uint8_t **inp,
2083 long len);
2084
2085// i2d_X509_ATTRIBUTE marshals |alg| as a DER-encoded Attribute (RFC 2986), as
2086// described in |i2d_SAMPLE|.
2087OPENSSL_EXPORT int i2d_X509_ATTRIBUTE(const X509_ATTRIBUTE *alg,
2088 uint8_t **outp);
2089
2090// X509_ATTRIBUTE_create returns a newly-allocated |X509_ATTRIBUTE|, or NULL on
2091// error. The attribute has type |nid| and contains a single value determined by
2092// |attrtype| and |value|, which are interpreted as in |ASN1_TYPE_set|. Note
2093// this function takes ownership of |value|.
2094OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int attrtype,
2095 void *value);
2096
2097// X509_ATTRIBUTE_create_by_NID returns a newly-allocated |X509_ATTRIBUTE| of
2098// type |nid|, or NULL on error. The value is determined as in
2099// |X509_ATTRIBUTE_set1_data|.
2100//
2101// If |attr| is non-NULL, the resulting |X509_ATTRIBUTE| is also written to
2102// |*attr|. If |*attr| was non-NULL when the function was called, |*attr| is
2103// reused instead of creating a new object.
2104//
2105// WARNING: The interpretation of |attrtype|, |data|, and |len| is complex and
2106// error-prone. See |X509_ATTRIBUTE_set1_data| for details.
2107//
2108// WARNING: The object reuse form is deprecated and may be removed in the
2109// future. It also currently incorrectly appends to the reused object's value
2110// set rather than overwriting it.
2111OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(
2112 X509_ATTRIBUTE **attr, int nid, int attrtype, const void *data, int len);
2113
2114// X509_ATTRIBUTE_create_by_OBJ behaves like |X509_ATTRIBUTE_create_by_NID|
2115// except the attribute's type is determined by |obj|.
2116OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(
2117 X509_ATTRIBUTE **attr, const ASN1_OBJECT *obj, int attrtype,
2118 const void *data, int len);
2119
2120// X509_ATTRIBUTE_create_by_txt behaves like |X509_ATTRIBUTE_create_by_NID|
2121// except the attribute's type is determined by calling |OBJ_txt2obj| with
2122// |attrname|.
2123OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(
2124 X509_ATTRIBUTE **attr, const char *attrname, int type,
2125 const unsigned char *bytes, int len);
2126
2127// X509_ATTRIBUTE_set1_object sets |attr|'s type to |obj|. It returns one on
2128// success and zero on error.
2129OPENSSL_EXPORT int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr,
2130 const ASN1_OBJECT *obj);
2131
2132// X509_ATTRIBUTE_set1_data appends a value to |attr|'s value set and returns
2133// one on success or zero on error. The value is determined as follows:
2134//
David Benjamin23d58422023-12-13 10:33:17 -05002135// If |attrtype| is zero, this function returns one and does nothing. This form
2136// may be used when calling |X509_ATTRIBUTE_create_by_*| to create an attribute
2137// with an empty value set. Such attributes are invalid, but OpenSSL supports
2138// creating them.
2139//
2140// Otherwise, if |attrtype| is a |MBSTRING_*| constant, the value is an ASN.1
2141// string. The string is determined by decoding |len| bytes from |data| in the
2142// encoding specified by |attrtype|, and then re-encoding it in a form
2143// appropriate for |attr|'s type. If |len| is -1, |strlen(data)| is used
2144// instead. See |ASN1_STRING_set_by_NID| for details.
David Benjamin787713b2023-04-19 10:28:13 -04002145//
2146// Otherwise, if |len| is not -1, the value is an ASN.1 string. |attrtype| is an
2147// |ASN1_STRING| type value and the |len| bytes from |data| are copied as the
2148// type-specific representation of |ASN1_STRING|. See |ASN1_STRING| for details.
2149//
David Benjamin787713b2023-04-19 10:28:13 -04002150// Otherwise, if |len| is -1, the value is constructed by passing |attrtype| and
2151// |data| to |ASN1_TYPE_set1|. That is, |attrtype| is an |ASN1_TYPE| type value,
2152// and |data| is cast to the corresponding pointer type.
2153//
2154// WARNING: Despite the name, this function appends to |attr|'s value set,
2155// rather than overwriting it. To overwrite the value set, create a new
2156// |X509_ATTRIBUTE| with |X509_ATTRIBUTE_new|.
2157//
2158// WARNING: If using the |MBSTRING_*| form, pass a length rather than relying on
2159// |strlen|. In particular, |strlen| will not behave correctly if the input is
2160// |MBSTRING_BMP| or |MBSTRING_UNIV|.
2161//
2162// WARNING: This function currently misinterprets |V_ASN1_OTHER| as an
2163// |MBSTRING_*| constant. This matches OpenSSL but means it is impossible to
2164// construct a value with a non-universal tag.
2165OPENSSL_EXPORT int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,
2166 const void *data, int len);
2167
2168// X509_ATTRIBUTE_get0_data returns the |idx|th value of |attr| in a
2169// type-specific representation to |attrtype|, or NULL if out of bounds or the
2170// type does not match. |attrtype| is one of the type values in |ASN1_TYPE|. On
2171// match, the return value uses the same representation as |ASN1_TYPE_set0|. See
2172// |ASN1_TYPE| for details.
2173OPENSSL_EXPORT void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx,
2174 int attrtype, void *unused);
2175
2176// X509_ATTRIBUTE_count returns the number of values in |attr|.
2177OPENSSL_EXPORT int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr);
2178
2179// X509_ATTRIBUTE_get0_object returns the type of |attr|.
2180OPENSSL_EXPORT ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr);
2181
2182// X509_ATTRIBUTE_get0_type returns the |idx|th value in |attr|, or NULL if out
2183// of bounds. Note this function returns one of |attr|'s values, not the type.
2184OPENSSL_EXPORT ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr,
2185 int idx);
2186
David Benjamin787713b2023-04-19 10:28:13 -04002187
David Benjamind206f3d2023-04-20 00:39:19 -04002188// SignedPublicKeyAndChallenge structures.
2189//
2190// The SignedPublicKeyAndChallenge (SPKAC) is a legacy structure to request
2191// certificates, primarily in the legacy <keygen> HTML tag. An SPKAC structure
2192// is represented by a |NETSCAPE_SPKI| structure.
2193//
2194// The structure is described in
2195// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/keygen
2196
2197// A Netscape_spki_st, or |NETSCAPE_SPKI|, represents a
2198// SignedPublicKeyAndChallenge structure. Although this structure contains a
2199// |spkac| field of type |NETSCAPE_SPKAC|, these are misnamed. The SPKAC is the
2200// entire structure, not the signed portion.
2201struct Netscape_spki_st {
2202 NETSCAPE_SPKAC *spkac;
2203 X509_ALGOR *sig_algor;
2204 ASN1_BIT_STRING *signature;
2205} /* NETSCAPE_SPKI */;
2206
David Benjamind206f3d2023-04-20 00:39:19 -04002207// NETSCAPE_SPKI_new returns a newly-allocated, empty |NETSCAPE_SPKI| object, or
2208// NULL on error.
2209OPENSSL_EXPORT NETSCAPE_SPKI *NETSCAPE_SPKI_new(void);
2210
2211// NETSCAPE_SPKI_free releases memory associated with |spki|.
2212OPENSSL_EXPORT void NETSCAPE_SPKI_free(NETSCAPE_SPKI *spki);
2213
2214// d2i_NETSCAPE_SPKI parses up to |len| bytes from |*inp| as a DER-encoded
2215// SignedPublicKeyAndChallenge structure, as described in |d2i_SAMPLE|.
2216OPENSSL_EXPORT NETSCAPE_SPKI *d2i_NETSCAPE_SPKI(NETSCAPE_SPKI **out,
2217 const uint8_t **inp, long len);
2218
2219// i2d_NETSCAPE_SPKI marshals |spki| as a DER-encoded
2220// SignedPublicKeyAndChallenge structure, as described in |i2d_SAMPLE|.
2221OPENSSL_EXPORT int i2d_NETSCAPE_SPKI(const NETSCAPE_SPKI *spki, uint8_t **outp);
2222
2223// NETSCAPE_SPKI_verify checks that |spki| has a valid signature by |pkey|. It
2224// returns one if the signature is valid and zero otherwise.
2225OPENSSL_EXPORT int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *spki, EVP_PKEY *pkey);
2226
2227// NETSCAPE_SPKI_b64_decode decodes |len| bytes from |str| as a base64-encoded
2228// SignedPublicKeyAndChallenge structure. It returns a newly-allocated
2229// |NETSCAPE_SPKI| structure with the result, or NULL on error. If |len| is 0 or
2230// negative, the length is calculated with |strlen| and |str| must be a
2231// NUL-terminated C string.
2232OPENSSL_EXPORT NETSCAPE_SPKI *NETSCAPE_SPKI_b64_decode(const char *str,
2233 ossl_ssize_t len);
2234
2235// NETSCAPE_SPKI_b64_encode encodes |spki| as a base64-encoded
2236// SignedPublicKeyAndChallenge structure. It returns a newly-allocated
2237// NUL-terminated C string with the result, or NULL on error. The caller must
2238// release the memory with |OPENSSL_free| when done.
2239OPENSSL_EXPORT char *NETSCAPE_SPKI_b64_encode(NETSCAPE_SPKI *spki);
2240
2241// NETSCAPE_SPKI_get_pubkey decodes and returns the public key in |spki| as an
2242// |EVP_PKEY|, or NULL on error. The caller takes ownership of the resulting
2243// pointer and must call |EVP_PKEY_free| when done.
David Benjamin5b3dc492023-12-21 19:19:16 -05002244OPENSSL_EXPORT EVP_PKEY *NETSCAPE_SPKI_get_pubkey(const NETSCAPE_SPKI *spki);
David Benjamind206f3d2023-04-20 00:39:19 -04002245
2246// NETSCAPE_SPKI_set_pubkey sets |spki|'s public key to |pkey|. It returns one
2247// on success or zero on error. This function does not take ownership of |pkey|,
2248// so the caller may continue to manage its lifetime independently of |spki|.
2249OPENSSL_EXPORT int NETSCAPE_SPKI_set_pubkey(NETSCAPE_SPKI *spki,
2250 EVP_PKEY *pkey);
2251
2252// NETSCAPE_SPKI_sign signs |spki| with |pkey| and replaces the signature
2253// algorithm and signature fields. It returns the length of the signature on
2254// success and zero on error. This function uses digest algorithm |md|, or
2255// |pkey|'s default if NULL. Other signing parameters use |pkey|'s defaults.
2256OPENSSL_EXPORT int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *spki, EVP_PKEY *pkey,
2257 const EVP_MD *md);
2258
2259// A Netscape_spkac_st, or |NETSCAPE_SPKAC|, represents a PublicKeyAndChallenge
2260// structure. This type is misnamed. The full SPKAC includes the signature,
2261// which is represented with the |NETSCAPE_SPKI| type.
2262struct Netscape_spkac_st {
2263 X509_PUBKEY *pubkey;
2264 ASN1_IA5STRING *challenge;
2265} /* NETSCAPE_SPKAC */;
2266
David Benjamind206f3d2023-04-20 00:39:19 -04002267// NETSCAPE_SPKAC_new returns a newly-allocated, empty |NETSCAPE_SPKAC| object,
2268// or NULL on error.
2269OPENSSL_EXPORT NETSCAPE_SPKAC *NETSCAPE_SPKAC_new(void);
2270
2271// NETSCAPE_SPKAC_free releases memory associated with |spkac|.
2272OPENSSL_EXPORT void NETSCAPE_SPKAC_free(NETSCAPE_SPKAC *spkac);
2273
2274// d2i_NETSCAPE_SPKAC parses up to |len| bytes from |*inp| as a DER-encoded
2275// PublicKeyAndChallenge structure, as described in |d2i_SAMPLE|.
2276OPENSSL_EXPORT NETSCAPE_SPKAC *d2i_NETSCAPE_SPKAC(NETSCAPE_SPKAC **out,
2277 const uint8_t **inp,
2278 long len);
2279
2280// i2d_NETSCAPE_SPKAC marshals |spkac| as a DER-encoded PublicKeyAndChallenge
2281// structure, as described in |i2d_SAMPLE|.
2282OPENSSL_EXPORT int i2d_NETSCAPE_SPKAC(const NETSCAPE_SPKAC *spkac,
2283 uint8_t **outp);
2284
2285
David Benjamin5d1c6122023-11-12 11:33:40 -05002286// RSASSA-PSS Parameters.
2287//
2288// In X.509, RSASSA-PSS signatures and keys use a complex parameter structure,
2289// defined in RFC 4055. The following functions are provided for compatibility
2290// with some OpenSSL APIs relating to this. Use of RSASSA-PSS in X.509 is
2291// discouraged. The parameters structure is very complex, and it takes more
2292// bytes to merely encode parameters than an entire P-256 ECDSA signature.
2293
David Benjamin09febb32023-11-21 00:14:22 -05002294// An rsa_pss_params_st, aka |RSA_PSS_PARAMS|, represents a parsed
2295// RSASSA-PSS-params structure, as defined in (RFC 4055).
David Benjamin5d1c6122023-11-12 11:33:40 -05002296struct rsa_pss_params_st {
2297 X509_ALGOR *hashAlgorithm;
2298 X509_ALGOR *maskGenAlgorithm;
2299 ASN1_INTEGER *saltLength;
2300 ASN1_INTEGER *trailerField;
2301 // OpenSSL caches the MGF hash on |RSA_PSS_PARAMS| in some cases. None of the
2302 // cases apply to BoringSSL, so this is always NULL, but Node expects the
2303 // field to be present.
2304 X509_ALGOR *maskHash;
2305} /* RSA_PSS_PARAMS */;
2306
2307// RSA_PSS_PARAMS is an |ASN1_ITEM| whose ASN.1 type is RSASSA-PSS-params (RFC
2308// 4055) and C type is |RSA_PSS_PARAMS*|.
2309DECLARE_ASN1_ITEM(RSA_PSS_PARAMS)
2310
2311// RSA_PSS_PARAMS_new returns a new, empty |RSA_PSS_PARAMS|, or NULL on error.
2312OPENSSL_EXPORT RSA_PSS_PARAMS *RSA_PSS_PARAMS_new(void);
2313
2314// RSA_PSS_PARAMS_free releases memory associated with |params|.
2315OPENSSL_EXPORT void RSA_PSS_PARAMS_free(RSA_PSS_PARAMS *params);
2316
2317// d2i_RSA_PSS_PARAMS parses up to |len| bytes from |*inp| as a DER-encoded
2318// RSASSA-PSS-params (RFC 4055), as described in |d2i_SAMPLE|.
2319OPENSSL_EXPORT RSA_PSS_PARAMS *d2i_RSA_PSS_PARAMS(RSA_PSS_PARAMS **out,
2320 const uint8_t **inp,
2321 long len);
2322
2323// i2d_RSA_PSS_PARAMS marshals |in| as a DER-encoded RSASSA-PSS-params (RFC
2324// 4055), as described in |i2d_SAMPLE|.
2325OPENSSL_EXPORT int i2d_RSA_PSS_PARAMS(const RSA_PSS_PARAMS *in, uint8_t **outp);
2326
2327
David Benjamin2a63b902023-11-12 19:42:41 -05002328// PKCS#8 private keys.
2329//
2330// The |PKCS8_PRIV_KEY_INFO| type represents a PKCS#8 PrivateKeyInfo (RFC 5208)
2331// structure. This is analogous to SubjectPublicKeyInfo and uses the same
2332// AlgorithmIdentifiers, but carries private keys and is not part of X.509
2333// itself.
2334//
2335// TODO(davidben): Do these functions really belong in this header?
2336
David Benjamin2a63b902023-11-12 19:42:41 -05002337// PKCS8_PRIV_KEY_INFO_new returns a newly-allocated, empty
2338// |PKCS8_PRIV_KEY_INFO| object, or NULL on error.
2339OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *PKCS8_PRIV_KEY_INFO_new(void);
2340
2341// PKCS8_PRIV_KEY_INFO_free releases memory associated with |key|.
2342OPENSSL_EXPORT void PKCS8_PRIV_KEY_INFO_free(PKCS8_PRIV_KEY_INFO *key);
2343
2344// d2i_PKCS8_PRIV_KEY_INFO parses up to |len| bytes from |*inp| as a DER-encoded
2345// PrivateKeyInfo, as described in |d2i_SAMPLE|.
2346OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO(
2347 PKCS8_PRIV_KEY_INFO **out, const uint8_t **inp, long len);
2348
2349// i2d_PKCS8_PRIV_KEY_INFO marshals |key| as a DER-encoded PrivateKeyInfo, as
2350// described in |i2d_SAMPLE|.
2351OPENSSL_EXPORT int i2d_PKCS8_PRIV_KEY_INFO(const PKCS8_PRIV_KEY_INFO *key,
2352 uint8_t **outp);
2353
2354// EVP_PKCS82PKEY returns |p8| as a newly-allocated |EVP_PKEY|, or NULL if the
2355// key was unsupported or could not be decoded. The caller must release the
2356// result with |EVP_PKEY_free| when done.
2357//
2358// Use |EVP_parse_private_key| instead.
2359OPENSSL_EXPORT EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8);
2360
2361// EVP_PKEY2PKCS8 encodes |pkey| as a PKCS#8 PrivateKeyInfo (RFC 5208),
2362// represented as a newly-allocated |PKCS8_PRIV_KEY_INFO|, or NULL on error. The
2363// caller must release the result with |PKCS8_PRIV_KEY_INFO_free| when done.
2364//
2365// Use |EVP_marshal_private_key| instead.
2366OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(const EVP_PKEY *pkey);
2367
2368
2369// Algorithm and octet string pairs.
2370//
2371// The |X509_SIG| type represents an ASN.1 SEQUENCE type of an
2372// AlgorithmIdentifier and an OCTET STRING. Although named |X509_SIG|, there is
2373// no type in X.509 which matches this format. The two common types which do are
2374// DigestInfo (RFC 2315 and RFC 8017), and EncryptedPrivateKeyInfo (RFC 5208).
2375
David Benjamin2a63b902023-11-12 19:42:41 -05002376// X509_SIG_new returns a newly-allocated, empty |X509_SIG| object, or NULL on
2377// error.
2378OPENSSL_EXPORT X509_SIG *X509_SIG_new(void);
2379
2380// X509_SIG_free releases memory associated with |key|.
2381OPENSSL_EXPORT void X509_SIG_free(X509_SIG *key);
2382
2383// d2i_X509_SIG parses up to |len| bytes from |*inp| as a DER-encoded algorithm
2384// and octet string pair, as described in |d2i_SAMPLE|.
2385OPENSSL_EXPORT X509_SIG *d2i_X509_SIG(X509_SIG **out, const uint8_t **inp,
2386 long len);
2387
2388// i2d_X509_SIG marshals |sig| as a DER-encoded algorithm
2389// and octet string pair, as described in |i2d_SAMPLE|.
2390OPENSSL_EXPORT int i2d_X509_SIG(const X509_SIG *sig, uint8_t **outp);
2391
2392// X509_SIG_get0 sets |*out_alg| and |*out_digest| to non-owning pointers to
2393// |sig|'s algorithm and digest fields, respectively. Either |out_alg| and
2394// |out_digest| may be NULL to skip those fields.
2395OPENSSL_EXPORT void X509_SIG_get0(const X509_SIG *sig,
2396 const X509_ALGOR **out_alg,
2397 const ASN1_OCTET_STRING **out_digest);
2398
2399// X509_SIG_getm behaves like |X509_SIG_get0| but returns mutable pointers.
2400OPENSSL_EXPORT void X509_SIG_getm(X509_SIG *sig, X509_ALGOR **out_alg,
2401 ASN1_OCTET_STRING **out_digest);
2402
2403
David Benjamin4363bdd2022-04-06 15:19:16 -04002404// Printing functions.
2405//
2406// The following functions output human-readable representations of
2407// X.509-related structures. They should only be used for debugging or logging
David Benjamind9f209b2023-04-19 01:08:04 -04002408// and not parsed programmatically. In many cases, the outputs are ambiguous, so
2409// attempting to parse them can lead to string injection vulnerabilities.
2410
David Benjamin2d7adae2023-11-21 00:11:09 -05002411// The following flags control |X509_print_ex| and |X509_REQ_print_ex|. These
2412// flags co-exist with |X509V3_EXT_*|, so avoid collisions when adding new ones.
David Benjamind9f209b2023-04-19 01:08:04 -04002413
2414// X509_FLAG_COMPAT disables all flags. It additionally causes names to be
2415// printed with a 16-byte indent.
2416#define X509_FLAG_COMPAT 0
2417
2418// X509_FLAG_NO_HEADER skips a header identifying the type of object printed.
2419#define X509_FLAG_NO_HEADER 1L
2420
2421// X509_FLAG_NO_VERSION skips printing the X.509 version number.
2422#define X509_FLAG_NO_VERSION (1L << 1)
2423
2424// X509_FLAG_NO_SERIAL skips printing the serial number. It is ignored in
2425// |X509_REQ_print_fp|.
2426#define X509_FLAG_NO_SERIAL (1L << 2)
2427
2428// X509_FLAG_NO_SIGNAME skips printing the signature algorithm in the
2429// TBSCertificate. It is ignored in |X509_REQ_print_fp|.
2430#define X509_FLAG_NO_SIGNAME (1L << 3)
2431
2432// X509_FLAG_NO_ISSUER skips printing the issuer.
2433#define X509_FLAG_NO_ISSUER (1L << 4)
2434
David Benjamind206f3d2023-04-20 00:39:19 -04002435// X509_FLAG_NO_VALIDITY skips printing the notBefore and notAfter times. It is
David Benjamind9f209b2023-04-19 01:08:04 -04002436// ignored in |X509_REQ_print_fp|.
2437#define X509_FLAG_NO_VALIDITY (1L << 5)
2438
David Benjamind206f3d2023-04-20 00:39:19 -04002439// X509_FLAG_NO_SUBJECT skips printing the subject.
David Benjamind9f209b2023-04-19 01:08:04 -04002440#define X509_FLAG_NO_SUBJECT (1L << 6)
2441
2442// X509_FLAG_NO_PUBKEY skips printing the public key.
2443#define X509_FLAG_NO_PUBKEY (1L << 7)
2444
2445// X509_FLAG_NO_EXTENSIONS skips printing the extension list. It is ignored in
2446// |X509_REQ_print_fp|. CSRs instead have attributes, which is controlled by
2447// |X509_FLAG_NO_ATTRIBUTES|.
2448#define X509_FLAG_NO_EXTENSIONS (1L << 8)
2449
2450// X509_FLAG_NO_SIGDUMP skips printing the signature and outer signature
2451// algorithm.
2452#define X509_FLAG_NO_SIGDUMP (1L << 9)
2453
2454// X509_FLAG_NO_AUX skips printing auxiliary properties. (See |d2i_X509_AUX| and
2455// related functions.)
2456#define X509_FLAG_NO_AUX (1L << 10)
2457
2458// X509_FLAG_NO_ATTRIBUTES skips printing CSR attributes. It does nothing for
2459// certificates and CRLs.
2460#define X509_FLAG_NO_ATTRIBUTES (1L << 11)
2461
2462// X509_FLAG_NO_IDS skips printing the issuerUniqueID and subjectUniqueID in a
2463// certificate. It is ignored in |X509_REQ_print_fp|.
2464#define X509_FLAG_NO_IDS (1L << 12)
2465
David Benjamin2d7adae2023-11-21 00:11:09 -05002466// The following flags control |X509_print_ex|, |X509_REQ_print_ex|,
2467// |X509V3_EXT_print|, and |X509V3_extensions_print|. These flags coexist with
2468// |X509_FLAG_*|, so avoid collisions when adding new ones.
2469
2470// X509V3_EXT_UNKNOWN_MASK is a mask that determines how unknown extensions are
2471// processed.
2472#define X509V3_EXT_UNKNOWN_MASK (0xfL << 16)
2473
2474// X509V3_EXT_DEFAULT causes unknown extensions or syntax errors to return
2475// failure.
2476#define X509V3_EXT_DEFAULT 0
2477
2478// X509V3_EXT_ERROR_UNKNOWN causes unknown extensions or syntax errors to print
2479// as "<Not Supported>" or "<Parse Error>", respectively.
2480#define X509V3_EXT_ERROR_UNKNOWN (1L << 16)
2481
2482// X509V3_EXT_PARSE_UNKNOWN is deprecated and behaves like
2483// |X509V3_EXT_DUMP_UNKNOWN|.
2484#define X509V3_EXT_PARSE_UNKNOWN (2L << 16)
2485
2486// X509V3_EXT_DUMP_UNKNOWN causes unknown extensions to be displayed as a
2487// hexdump.
2488#define X509V3_EXT_DUMP_UNKNOWN (3L << 16)
2489
David Benjamind9f209b2023-04-19 01:08:04 -04002490// X509_print_ex writes a human-readable representation of |x| to |bp|. It
2491// returns one on success and zero on error. |nmflags| is the flags parameter
2492// for |X509_NAME_print_ex| when printing the subject and issuer. |cflag| should
David Benjamin2d7adae2023-11-21 00:11:09 -05002493// be some combination of the |X509_FLAG_*| and |X509V3_EXT_*| constants.
David Benjamind9f209b2023-04-19 01:08:04 -04002494OPENSSL_EXPORT int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflag,
2495 unsigned long cflag);
2496
2497// X509_print_ex_fp behaves like |X509_print_ex| but writes to |fp|.
2498OPENSSL_EXPORT int X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag,
2499 unsigned long cflag);
2500
2501// X509_print calls |X509_print_ex| with |XN_FLAG_COMPAT| and |X509_FLAG_COMPAT|
2502// flags.
2503OPENSSL_EXPORT int X509_print(BIO *bp, X509 *x);
2504
2505// X509_print_fp behaves like |X509_print| but writes to |fp|.
2506OPENSSL_EXPORT int X509_print_fp(FILE *fp, X509 *x);
2507
2508// X509_CRL_print writes a human-readable representation of |x| to |bp|. It
2509// returns one on success and zero on error.
2510OPENSSL_EXPORT int X509_CRL_print(BIO *bp, X509_CRL *x);
2511
2512// X509_CRL_print_fp behaves like |X509_CRL_print| but writes to |fp|.
2513OPENSSL_EXPORT int X509_CRL_print_fp(FILE *fp, X509_CRL *x);
2514
2515// X509_REQ_print_ex writes a human-readable representation of |x| to |bp|. It
2516// returns one on success and zero on error. |nmflags| is the flags parameter
2517// for |X509_NAME_print_ex|, when printing the subject. |cflag| should be some
David Benjamin2d7adae2023-11-21 00:11:09 -05002518// combination of the |X509_FLAG_*| and |X509V3_EXT_*| constants.
David Benjamind9f209b2023-04-19 01:08:04 -04002519OPENSSL_EXPORT int X509_REQ_print_ex(BIO *bp, X509_REQ *x, unsigned long nmflag,
2520 unsigned long cflag);
2521
2522// X509_REQ_print calls |X509_REQ_print_ex| with |XN_FLAG_COMPAT| and
2523// |X509_FLAG_COMPAT| flags.
2524OPENSSL_EXPORT int X509_REQ_print(BIO *bp, X509_REQ *req);
2525
2526// X509_REQ_print_fp behaves like |X509_REQ_print| but writes to |fp|.
2527OPENSSL_EXPORT int X509_REQ_print_fp(FILE *fp, X509_REQ *req);
2528
2529// The following flags are control |X509_NAME_print_ex|. They must not collide
2530// with |ASN1_STRFLGS_*|.
2531//
2532// TODO(davidben): This is far, far too many options and most of them are
2533// useless. Trim this down.
2534
2535// XN_FLAG_COMPAT prints with |X509_NAME_print|'s format and return value
2536// convention.
2537#define XN_FLAG_COMPAT 0
2538
2539// XN_FLAG_SEP_MASK determines the separators to use between attributes.
2540#define XN_FLAG_SEP_MASK (0xf << 16)
2541
2542// XN_FLAG_SEP_COMMA_PLUS separates RDNs with "," and attributes within an RDN
2543// with "+", as in RFC 2253.
2544#define XN_FLAG_SEP_COMMA_PLUS (1 << 16)
2545
2546// XN_FLAG_SEP_CPLUS_SPC behaves like |XN_FLAG_SEP_COMMA_PLUS| but adds spaces
2547// between the separators.
2548#define XN_FLAG_SEP_CPLUS_SPC (2 << 16)
2549
2550// XN_FLAG_SEP_SPLUS_SPC separates RDNs with "; " and attributes within an RDN
2551// with " + ".
2552#define XN_FLAG_SEP_SPLUS_SPC (3 << 16)
2553
2554// XN_FLAG_SEP_MULTILINE prints each attribute on one line.
2555#define XN_FLAG_SEP_MULTILINE (4 << 16)
2556
2557// XN_FLAG_DN_REV prints RDNs in reverse, from least significant to most
2558// significant, as RFC 2253.
2559#define XN_FLAG_DN_REV (1 << 20)
2560
2561// XN_FLAG_FN_MASK determines how attribute types are displayed.
2562#define XN_FLAG_FN_MASK (0x3 << 21)
2563
2564// XN_FLAG_FN_SN uses the attribute type's short name, when available.
2565#define XN_FLAG_FN_SN 0
2566
David Benjamind9f209b2023-04-19 01:08:04 -04002567// XN_FLAG_SPC_EQ wraps the "=" operator with spaces when printing attributes.
2568#define XN_FLAG_SPC_EQ (1 << 23)
2569
2570// XN_FLAG_DUMP_UNKNOWN_FIELDS causes unknown attribute types to be printed in
2571// hex, as in RFC 2253.
2572#define XN_FLAG_DUMP_UNKNOWN_FIELDS (1 << 24)
2573
David Benjamind9f209b2023-04-19 01:08:04 -04002574// XN_FLAG_RFC2253 prints like RFC 2253.
2575#define XN_FLAG_RFC2253 \
2576 (ASN1_STRFLGS_RFC2253 | XN_FLAG_SEP_COMMA_PLUS | XN_FLAG_DN_REV | \
2577 XN_FLAG_FN_SN | XN_FLAG_DUMP_UNKNOWN_FIELDS)
2578
2579// XN_FLAG_ONELINE prints a one-line representation of the name.
2580#define XN_FLAG_ONELINE \
2581 (ASN1_STRFLGS_RFC2253 | ASN1_STRFLGS_ESC_QUOTE | XN_FLAG_SEP_CPLUS_SPC | \
2582 XN_FLAG_SPC_EQ | XN_FLAG_FN_SN)
2583
David Benjamind9f209b2023-04-19 01:08:04 -04002584// X509_NAME_print_ex writes a human-readable representation of |nm| to |out|.
2585// Each line of output is indented by |indent| spaces. It returns the number of
2586// bytes written on success, and -1 on error. If |out| is NULL, it returns the
2587// number of bytes it would have written but does not write anything. |flags|
2588// should be some combination of |XN_FLAG_*| and |ASN1_STRFLGS_*| values and
2589// determines the output. If unsure, use |XN_FLAG_RFC2253|.
2590//
2591// If |flags| is |XN_FLAG_COMPAT|, or zero, this function calls
2592// |X509_NAME_print| instead. In that case, it returns one on success, rather
2593// than the output length.
2594OPENSSL_EXPORT int X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent,
2595 unsigned long flags);
2596
2597// X509_NAME_print prints a human-readable representation of |name| to |bp|. It
2598// returns one on success and zero on error. |obase| is ignored.
2599//
2600// This function outputs a legacy format that does not correctly handle string
2601// encodings and other cases. Prefer |X509_NAME_print_ex| if printing a name for
2602// debugging purposes.
2603OPENSSL_EXPORT int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase);
2604
2605// X509_NAME_oneline writes a human-readable representation to |name| to a
2606// buffer as a NUL-terminated C string.
2607//
2608// If |buf| is NULL, returns a newly-allocated buffer containing the result on
2609// success, or NULL on error. The buffer must be released with |OPENSSL_free|
2610// when done.
2611//
2612// If |buf| is non-NULL, at most |size| bytes of output are written to |buf|
2613// instead. |size| includes the trailing NUL. The function then returns |buf| on
2614// success or NULL on error. If the output does not fit in |size| bytes, the
2615// output is silently truncated at an attribute boundary.
2616//
2617// This function outputs a legacy format that does not correctly handle string
2618// encodings and other cases. Prefer |X509_NAME_print_ex| if printing a name for
2619// debugging purposes.
2620OPENSSL_EXPORT char *X509_NAME_oneline(const X509_NAME *name, char *buf, int size);
2621
2622// X509_NAME_print_ex_fp behaves like |X509_NAME_print_ex| but writes to |fp|.
2623OPENSSL_EXPORT int X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm,
2624 int indent, unsigned long flags);
David Benjamin4363bdd2022-04-06 15:19:16 -04002625
2626// X509_signature_dump writes a human-readable representation of |sig| to |bio|,
2627// indented with |indent| spaces. It returns one on success and zero on error.
2628OPENSSL_EXPORT int X509_signature_dump(BIO *bio, const ASN1_STRING *sig,
2629 int indent);
2630
2631// X509_signature_print writes a human-readable representation of |alg| and
2632// |sig| to |bio|. It returns one on success and zero on error.
2633OPENSSL_EXPORT int X509_signature_print(BIO *bio, const X509_ALGOR *alg,
2634 const ASN1_STRING *sig);
2635
David Benjamin2d7adae2023-11-21 00:11:09 -05002636// X509V3_EXT_print prints a human-readable representation of |ext| to out. It
2637// returns one on success and zero on error. The output is indented by |indent|
2638// spaces. |flag| is one of the |X509V3_EXT_*| constants and controls printing
2639// of unknown extensions and syntax errors.
2640//
2641// WARNING: Although some applications programmatically parse the output of this
2642// function to process X.509 extensions, this is not safe. In many cases, the
2643// outputs are ambiguous to attempting to parse them can lead to string
2644// injection vulnerabilities. These functions should only be used for debugging
2645// or logging.
2646OPENSSL_EXPORT int X509V3_EXT_print(BIO *out, const X509_EXTENSION *ext,
2647 unsigned long flag, int indent);
2648
2649// X509V3_EXT_print_fp behaves like |X509V3_EXT_print| but writes to a |FILE|
2650// instead of a |BIO|.
2651OPENSSL_EXPORT int X509V3_EXT_print_fp(FILE *out, const X509_EXTENSION *ext,
2652 int flag, int indent);
2653
2654// X509V3_extensions_print prints |title|, followed by a human-readable
2655// representation of |exts| to |out|. It returns one on success and zero on
2656// error. The output is indented by |indent| spaces. |flag| is one of the
2657// |X509V3_EXT_*| constants and controls printing of unknown extensions and
2658// syntax errors.
2659OPENSSL_EXPORT int X509V3_extensions_print(BIO *out, const char *title,
2660 const STACK_OF(X509_EXTENSION) *exts,
2661 unsigned long flag, int indent);
2662
2663// GENERAL_NAME_print prints a human-readable representation of |gen| to |out|.
2664// It returns one on success and zero on error.
2665//
2666// TODO(davidben): Actually, it just returns one and doesn't check for I/O or
2667// allocation errors. But it should return zero on error.
2668OPENSSL_EXPORT int GENERAL_NAME_print(BIO *out, const GENERAL_NAME *gen);
2669
David Benjamin4363bdd2022-04-06 15:19:16 -04002670
David Benjamin918b8bc2022-07-09 15:33:19 -04002671// Convenience functions.
2672
2673// X509_pubkey_digest hashes the contents of the BIT STRING in |x509|'s
2674// subjectPublicKeyInfo field with |md| and writes the result to |out|.
2675// |EVP_MD_CTX_size| bytes are written, which is at most |EVP_MAX_MD_SIZE|. If
2676// |out_len| is not NULL, |*out_len| is set to the number of bytes written. This
2677// function returns one on success and zero on error.
2678//
2679// This hash omits the BIT STRING tag, length, and number of unused bits. It
2680// also omits the AlgorithmIdentifier which describes the key type. It
2681// corresponds to the OCSP KeyHash definition and is not suitable for other
2682// purposes.
2683OPENSSL_EXPORT int X509_pubkey_digest(const X509 *x509, const EVP_MD *md,
2684 uint8_t *out, unsigned *out_len);
2685
2686// X509_digest hashes |x509|'s DER encoding with |md| and writes the result to
2687// |out|. |EVP_MD_CTX_size| bytes are written, which is at most
2688// |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number
2689// of bytes written. This function returns one on success and zero on error.
2690// Note this digest covers the entire certificate, not just the signed portion.
2691OPENSSL_EXPORT int X509_digest(const X509 *x509, const EVP_MD *md, uint8_t *out,
2692 unsigned *out_len);
2693
2694// X509_CRL_digest hashes |crl|'s DER encoding with |md| and writes the result
2695// to |out|. |EVP_MD_CTX_size| bytes are written, which is at most
2696// |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number
2697// of bytes written. This function returns one on success and zero on error.
2698// Note this digest covers the entire CRL, not just the signed portion.
2699OPENSSL_EXPORT int X509_CRL_digest(const X509_CRL *crl, const EVP_MD *md,
2700 uint8_t *out, unsigned *out_len);
2701
2702// X509_REQ_digest hashes |req|'s DER encoding with |md| and writes the result
2703// to |out|. |EVP_MD_CTX_size| bytes are written, which is at most
2704// |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number
2705// of bytes written. This function returns one on success and zero on error.
2706// Note this digest covers the entire certificate request, not just the signed
2707// portion.
2708OPENSSL_EXPORT int X509_REQ_digest(const X509_REQ *req, const EVP_MD *md,
2709 uint8_t *out, unsigned *out_len);
2710
2711// X509_NAME_digest hashes |name|'s DER encoding with |md| and writes the result
2712// to |out|. |EVP_MD_CTX_size| bytes are written, which is at most
2713// |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number
2714// of bytes written. This function returns one on success and zero on error.
2715OPENSSL_EXPORT int X509_NAME_digest(const X509_NAME *name, const EVP_MD *md,
2716 uint8_t *out, unsigned *out_len);
2717
2718// The following functions behave like the corresponding unsuffixed |d2i_*|
2719// functions, but read the result from |bp| instead. Callers using these
2720// functions with memory |BIO|s to parse structures already in memory should use
2721// |d2i_*| instead.
2722OPENSSL_EXPORT X509 *d2i_X509_bio(BIO *bp, X509 **x509);
2723OPENSSL_EXPORT X509_CRL *d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl);
2724OPENSSL_EXPORT X509_REQ *d2i_X509_REQ_bio(BIO *bp, X509_REQ **req);
2725OPENSSL_EXPORT RSA *d2i_RSAPrivateKey_bio(BIO *bp, RSA **rsa);
2726OPENSSL_EXPORT RSA *d2i_RSAPublicKey_bio(BIO *bp, RSA **rsa);
2727OPENSSL_EXPORT RSA *d2i_RSA_PUBKEY_bio(BIO *bp, RSA **rsa);
2728OPENSSL_EXPORT DSA *d2i_DSA_PUBKEY_bio(BIO *bp, DSA **dsa);
2729OPENSSL_EXPORT DSA *d2i_DSAPrivateKey_bio(BIO *bp, DSA **dsa);
2730OPENSSL_EXPORT EC_KEY *d2i_EC_PUBKEY_bio(BIO *bp, EC_KEY **eckey);
2731OPENSSL_EXPORT EC_KEY *d2i_ECPrivateKey_bio(BIO *bp, EC_KEY **eckey);
2732OPENSSL_EXPORT X509_SIG *d2i_PKCS8_bio(BIO *bp, X509_SIG **p8);
2733OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_bio(
2734 BIO *bp, PKCS8_PRIV_KEY_INFO **p8inf);
2735OPENSSL_EXPORT EVP_PKEY *d2i_PUBKEY_bio(BIO *bp, EVP_PKEY **a);
2736OPENSSL_EXPORT DH *d2i_DHparams_bio(BIO *bp, DH **dh);
2737
2738// d2i_PrivateKey_bio behaves like |d2i_AutoPrivateKey|, but reads from |bp|
2739// instead.
2740OPENSSL_EXPORT EVP_PKEY *d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a);
2741
2742// The following functions behave like the corresponding unsuffixed |i2d_*|
2743// functions, but write the result to |bp|. They return one on success and zero
2744// on error. Callers using them with memory |BIO|s to encode structures to
2745// memory should use |i2d_*| directly instead.
2746OPENSSL_EXPORT int i2d_X509_bio(BIO *bp, X509 *x509);
2747OPENSSL_EXPORT int i2d_X509_CRL_bio(BIO *bp, X509_CRL *crl);
2748OPENSSL_EXPORT int i2d_X509_REQ_bio(BIO *bp, X509_REQ *req);
2749OPENSSL_EXPORT int i2d_RSAPrivateKey_bio(BIO *bp, RSA *rsa);
2750OPENSSL_EXPORT int i2d_RSAPublicKey_bio(BIO *bp, RSA *rsa);
2751OPENSSL_EXPORT int i2d_RSA_PUBKEY_bio(BIO *bp, RSA *rsa);
2752OPENSSL_EXPORT int i2d_DSA_PUBKEY_bio(BIO *bp, DSA *dsa);
2753OPENSSL_EXPORT int i2d_DSAPrivateKey_bio(BIO *bp, DSA *dsa);
2754OPENSSL_EXPORT int i2d_EC_PUBKEY_bio(BIO *bp, EC_KEY *eckey);
2755OPENSSL_EXPORT int i2d_ECPrivateKey_bio(BIO *bp, EC_KEY *eckey);
2756OPENSSL_EXPORT int i2d_PKCS8_bio(BIO *bp, X509_SIG *p8);
2757OPENSSL_EXPORT int i2d_PKCS8_PRIV_KEY_INFO_bio(BIO *bp,
2758 PKCS8_PRIV_KEY_INFO *p8inf);
2759OPENSSL_EXPORT int i2d_PrivateKey_bio(BIO *bp, EVP_PKEY *pkey);
2760OPENSSL_EXPORT int i2d_PUBKEY_bio(BIO *bp, EVP_PKEY *pkey);
2761OPENSSL_EXPORT int i2d_DHparams_bio(BIO *bp, const DH *dh);
2762
2763// i2d_PKCS8PrivateKeyInfo_bio encodes |key| as a PKCS#8 PrivateKeyInfo
2764// structure (see |EVP_marshal_private_key|) and writes the result to |bp|. It
2765// returns one on success and zero on error.
2766OPENSSL_EXPORT int i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, EVP_PKEY *key);
2767
2768// The following functions behave like the corresponding |d2i_*_bio| functions,
2769// but read from |fp| instead.
2770OPENSSL_EXPORT X509 *d2i_X509_fp(FILE *fp, X509 **x509);
2771OPENSSL_EXPORT X509_CRL *d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl);
2772OPENSSL_EXPORT X509_REQ *d2i_X509_REQ_fp(FILE *fp, X509_REQ **req);
2773OPENSSL_EXPORT RSA *d2i_RSAPrivateKey_fp(FILE *fp, RSA **rsa);
2774OPENSSL_EXPORT RSA *d2i_RSAPublicKey_fp(FILE *fp, RSA **rsa);
2775OPENSSL_EXPORT RSA *d2i_RSA_PUBKEY_fp(FILE *fp, RSA **rsa);
2776OPENSSL_EXPORT DSA *d2i_DSA_PUBKEY_fp(FILE *fp, DSA **dsa);
2777OPENSSL_EXPORT DSA *d2i_DSAPrivateKey_fp(FILE *fp, DSA **dsa);
2778OPENSSL_EXPORT EC_KEY *d2i_EC_PUBKEY_fp(FILE *fp, EC_KEY **eckey);
2779OPENSSL_EXPORT EC_KEY *d2i_ECPrivateKey_fp(FILE *fp, EC_KEY **eckey);
2780OPENSSL_EXPORT X509_SIG *d2i_PKCS8_fp(FILE *fp, X509_SIG **p8);
2781OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_fp(
2782 FILE *fp, PKCS8_PRIV_KEY_INFO **p8inf);
2783OPENSSL_EXPORT EVP_PKEY *d2i_PrivateKey_fp(FILE *fp, EVP_PKEY **a);
2784OPENSSL_EXPORT EVP_PKEY *d2i_PUBKEY_fp(FILE *fp, EVP_PKEY **a);
2785
2786// The following functions behave like the corresponding |i2d_*_bio| functions,
2787// but write to |fp| instead.
2788OPENSSL_EXPORT int i2d_X509_fp(FILE *fp, X509 *x509);
2789OPENSSL_EXPORT int i2d_X509_CRL_fp(FILE *fp, X509_CRL *crl);
2790OPENSSL_EXPORT int i2d_X509_REQ_fp(FILE *fp, X509_REQ *req);
2791OPENSSL_EXPORT int i2d_RSAPrivateKey_fp(FILE *fp, RSA *rsa);
2792OPENSSL_EXPORT int i2d_RSAPublicKey_fp(FILE *fp, RSA *rsa);
2793OPENSSL_EXPORT int i2d_RSA_PUBKEY_fp(FILE *fp, RSA *rsa);
2794OPENSSL_EXPORT int i2d_DSA_PUBKEY_fp(FILE *fp, DSA *dsa);
2795OPENSSL_EXPORT int i2d_DSAPrivateKey_fp(FILE *fp, DSA *dsa);
2796OPENSSL_EXPORT int i2d_EC_PUBKEY_fp(FILE *fp, EC_KEY *eckey);
2797OPENSSL_EXPORT int i2d_ECPrivateKey_fp(FILE *fp, EC_KEY *eckey);
2798OPENSSL_EXPORT int i2d_PKCS8_fp(FILE *fp, X509_SIG *p8);
2799OPENSSL_EXPORT int i2d_PKCS8_PRIV_KEY_INFO_fp(FILE *fp,
2800 PKCS8_PRIV_KEY_INFO *p8inf);
2801OPENSSL_EXPORT int i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, EVP_PKEY *key);
2802OPENSSL_EXPORT int i2d_PrivateKey_fp(FILE *fp, EVP_PKEY *pkey);
2803OPENSSL_EXPORT int i2d_PUBKEY_fp(FILE *fp, EVP_PKEY *pkey);
2804
David Benjaminbbd9f182022-09-09 11:57:14 -04002805// X509_find_by_issuer_and_serial returns the first |X509| in |sk| whose issuer
2806// and serial are |name| and |serial|, respectively. If no match is found, it
2807// returns NULL.
2808OPENSSL_EXPORT X509 *X509_find_by_issuer_and_serial(const STACK_OF(X509) *sk,
2809 X509_NAME *name,
2810 const ASN1_INTEGER *serial);
2811
2812// X509_find_by_subject returns the first |X509| in |sk| whose subject is
2813// |name|. If no match is found, it returns NULL.
2814OPENSSL_EXPORT X509 *X509_find_by_subject(const STACK_OF(X509) *sk,
2815 X509_NAME *name);
2816
David Benjamin58a40942023-04-19 10:43:55 -04002817// X509_cmp_time compares |s| against |*t|. On success, it returns a negative
2818// number if |s| <= |*t| and a positive number if |s| > |*t|. On error, it
2819// returns zero. If |t| is NULL, it uses the current time instead of |*t|.
2820//
2821// WARNING: Unlike most comparison functions, this function returns zero on
2822// error, not equality.
David Benjamin899c1a72023-07-05 21:56:55 -04002823OPENSSL_EXPORT int X509_cmp_time(const ASN1_TIME *s, const time_t *t);
David Benjamin58a40942023-04-19 10:43:55 -04002824
2825// X509_cmp_time_posix compares |s| against |t|. On success, it returns a
2826// negative number if |s| <= |t| and a positive number if |s| > |t|. On error,
2827// it returns zero.
2828//
2829// WARNING: Unlike most comparison functions, this function returns zero on
2830// error, not equality.
2831OPENSSL_EXPORT int X509_cmp_time_posix(const ASN1_TIME *s, int64_t t);
2832
2833// X509_cmp_current_time behaves like |X509_cmp_time| but compares |s| against
2834// the current time.
2835OPENSSL_EXPORT int X509_cmp_current_time(const ASN1_TIME *s);
2836
2837// X509_time_adj calls |X509_time_adj_ex| with |offset_day| equal to zero.
2838OPENSSL_EXPORT ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec,
David Benjamin899c1a72023-07-05 21:56:55 -04002839 const time_t *t);
David Benjamin58a40942023-04-19 10:43:55 -04002840
2841// X509_time_adj_ex behaves like |ASN1_TIME_adj|, but adds an offset to |*t|. If
2842// |t| is NULL, it uses the current time instead of |*t|.
2843OPENSSL_EXPORT ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s, int offset_day,
David Benjamin899c1a72023-07-05 21:56:55 -04002844 long offset_sec, const time_t *t);
David Benjamin58a40942023-04-19 10:43:55 -04002845
2846// X509_gmtime_adj behaves like |X509_time_adj_ex| but adds |offset_sec| to the
2847// current time.
2848OPENSSL_EXPORT ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long offset_sec);
2849
David Benjamina697bcb2023-11-12 19:56:19 -05002850// X509_issuer_name_cmp behaves like |X509_NAME_cmp|, but compares |a| and |b|'s
2851// issuer names.
2852OPENSSL_EXPORT int X509_issuer_name_cmp(const X509 *a, const X509 *b);
2853
2854// X509_subject_name_cmp behaves like |X509_NAME_cmp|, but compares |a| and
2855// |b|'s subject names.
2856OPENSSL_EXPORT int X509_subject_name_cmp(const X509 *a, const X509 *b);
2857
2858// X509_CRL_cmp behaves like |X509_NAME_cmp|, but compares |a| and |b|'s
2859// issuer names.
2860//
2861// WARNING: This function is misnamed. It does not compare other parts of the
2862// CRL, only the issuer fields using |X509_NAME_cmp|.
2863OPENSSL_EXPORT int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b);
2864
David Benjaminc77d0f82023-12-06 23:07:11 -05002865// X509_issuer_name_hash returns the hash of |x509|'s issuer name with
2866// |X509_NAME_hash|.
2867OPENSSL_EXPORT uint32_t X509_issuer_name_hash(X509 *x509);
2868
2869// X509_subject_name_hash returns the hash of |x509|'s subject name with
2870// |X509_NAME_hash|.
2871OPENSSL_EXPORT uint32_t X509_subject_name_hash(X509 *x509);
2872
2873// X509_issuer_name_hash returns the hash of |x509|'s issuer name with
2874// |X509_NAME_hash_old|.
2875OPENSSL_EXPORT uint32_t X509_issuer_name_hash_old(X509 *x509);
2876
2877// X509_usjbect_name_hash returns the hash of |x509|'s usjbect name with
2878// |X509_NAME_hash_old|.
2879OPENSSL_EXPORT uint32_t X509_subject_name_hash_old(X509 *x509);
2880
David Benjamin918b8bc2022-07-09 15:33:19 -04002881
David Benjamin4363bdd2022-04-06 15:19:16 -04002882// ex_data functions.
2883//
2884// See |ex_data.h| for details.
2885
2886OPENSSL_EXPORT int X509_get_ex_new_index(long argl, void *argp,
2887 CRYPTO_EX_unused *unused,
2888 CRYPTO_EX_dup *dup_unused,
2889 CRYPTO_EX_free *free_func);
2890OPENSSL_EXPORT int X509_set_ex_data(X509 *r, int idx, void *arg);
2891OPENSSL_EXPORT void *X509_get_ex_data(X509 *r, int idx);
2892
2893OPENSSL_EXPORT int X509_STORE_CTX_get_ex_new_index(long argl, void *argp,
2894 CRYPTO_EX_unused *unused,
2895 CRYPTO_EX_dup *dup_unused,
2896 CRYPTO_EX_free *free_func);
2897OPENSSL_EXPORT int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx,
2898 void *data);
2899OPENSSL_EXPORT void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx);
2900
2901
David Benjamin5bef6ec2023-11-13 09:04:43 -05002902// Hashing and signing ASN.1 structures.
2903
2904// ASN1_digest serializes |data| with |i2d| and then hashes the result with
2905// |type|. On success, it returns one, writes the digest to |md|, and sets
2906// |*len| to the digest length if non-NULL. On error, it returns zero.
2907//
2908// |EVP_MD_CTX_size| bytes are written, which is at most |EVP_MAX_MD_SIZE|. The
2909// buffer must have sufficient space for this output.
2910OPENSSL_EXPORT int ASN1_digest(i2d_of_void *i2d, const EVP_MD *type, char *data,
2911 unsigned char *md, unsigned int *len);
2912
2913// ASN1_item_digest serializes |data| with |it| and then hashes the result with
2914// |type|. On success, it returns one, writes the digest to |md|, and sets
2915// |*len| to the digest length if non-NULL. On error, it returns zero.
2916//
2917// |EVP_MD_CTX_size| bytes are written, which is at most |EVP_MAX_MD_SIZE|. The
2918// buffer must have sufficient space for this output.
2919//
2920// WARNING: |data| must be a pointer with the same type as |it|'s corresponding
2921// C type. Using the wrong type is a potentially exploitable memory error.
2922OPENSSL_EXPORT int ASN1_item_digest(const ASN1_ITEM *it, const EVP_MD *type,
2923 void *data, unsigned char *md,
2924 unsigned int *len);
2925
2926// ASN1_item_verify serializes |data| with |it| and then verifies |signature| is
2927// a valid signature for the result with |algor1| and |pkey|. It returns one on
2928// success and zero on error. The signature and algorithm are interpreted as in
2929// X.509.
2930//
2931// WARNING: |data| must be a pointer with the same type as |it|'s corresponding
2932// C type. Using the wrong type is a potentially exploitable memory error.
2933OPENSSL_EXPORT int ASN1_item_verify(const ASN1_ITEM *it,
2934 const X509_ALGOR *algor1,
2935 const ASN1_BIT_STRING *signature,
2936 void *data, EVP_PKEY *pkey);
2937
2938// ASN1_item_sign serializes |data| with |it| and then signs the result with
2939// the private key |pkey|. It returns the length of the signature on success and
2940// zero on error. On success, it writes the signature to |signature| and the
2941// signature algorithm to each of |algor1| and |algor2|. Either of |algor1| or
2942// |algor2| may be NULL to ignore them. This function uses digest algorithm
2943// |md|, or |pkey|'s default if NULL. Other signing parameters use |pkey|'s
2944// defaults. To customize them, use |ASN1_item_sign_ctx|.
2945//
2946// WARNING: |data| must be a pointer with the same type as |it|'s corresponding
2947// C type. Using the wrong type is a potentially exploitable memory error.
2948OPENSSL_EXPORT int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1,
2949 X509_ALGOR *algor2,
2950 ASN1_BIT_STRING *signature, void *data,
2951 EVP_PKEY *pkey, const EVP_MD *type);
2952
2953// ASN1_item_sign_ctx behaves like |ASN1_item_sign| except the signature is
2954// signed with |ctx|, |ctx|, which must have been initialized with
2955// |EVP_DigestSignInit|. The caller should configure the corresponding
2956// |EVP_PKEY_CTX| with any additional parameters before calling this function.
2957//
2958// On success or failure, this function mutates |ctx| and resets it to the empty
2959// state. Caller should not rely on its contents after the function returns.
2960//
2961// WARNING: |data| must be a pointer with the same type as |it|'s corresponding
2962// C type. Using the wrong type is a potentially exploitable memory error.
2963OPENSSL_EXPORT int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1,
2964 X509_ALGOR *algor2,
2965 ASN1_BIT_STRING *signature, void *asn,
2966 EVP_MD_CTX *ctx);
2967
2968
David Benjamin9e404812023-12-06 22:33:21 -05002969// Verification internals.
2970//
2971// The following functions expose portions of certificate validation. They are
2972// exported for compatibility with existing callers, or to support some obscure
2973// use cases. Most callers, however, will not need these functions and should
2974// instead use |X509_STORE_CTX| APIs.
2975
2976// X509_supported_extension returns one if |ex| is a critical X.509 certificate
2977// extension, supported by |X509_verify_cert|, and zero otherwise.
2978//
2979// Note this function only reports certificate extensions (as opposed to CRL or
2980// CRL extensions), and only extensions that are expected to be marked critical.
2981// Additionally, |X509_verify_cert| checks for unsupported critical extensions
2982// internally, so most callers will not need to call this function separately.
2983OPENSSL_EXPORT int X509_supported_extension(const X509_EXTENSION *ex);
2984
2985// X509_check_ca returns one if |x509| may be considered a CA certificate,
2986// according to basic constraints and key usage extensions. Otherwise, it
2987// returns zero. If |x509| is an X509v1 certificate, and thus has no extensions,
2988// it is considered eligible.
2989//
2990// This function returning one does not indicate that |x509| is trusted, only
2991// that it is eligible to be a CA.
2992//
2993// TODO(crbug.com/boringssl/407): |x509| should be const.
2994OPENSSL_EXPORT int X509_check_ca(X509 *x509);
2995
2996// X509_check_issued checks if |issuer| and |subject|'s name, authority key
2997// identifier, and key usage fields allow |issuer| to have issued |subject|. It
2998// returns |X509_V_OK| on success and an |X509_V_ERR_*| value otherwise.
2999//
3000// This function does not check the signature on |subject|. Rather, it is
3001// intended to prune the set of possible issuer certificates during
3002// path-building.
3003//
3004// TODO(crbug.com/boringssl/407): Both parameters should be const.
3005OPENSSL_EXPORT int X509_check_issued(X509 *issuer, X509 *subject);
3006
3007// NAME_CONSTRAINTS_check checks if |x509| satisfies name constraints in |nc|.
3008// It returns |X509_V_OK| on success and some |X509_V_ERR_*| constant on error.
3009//
3010// TODO(crbug.com/boringssl/407): Both parameters should be const.
3011OPENSSL_EXPORT int NAME_CONSTRAINTS_check(X509 *x509, NAME_CONSTRAINTS *nc);
3012
3013// X509_check_host checks if |x509| matches the DNS name |chk|. It returns one
3014// on match, zero on mismatch, or a negative number on error. |flags| should be
3015// some combination of |X509_CHECK_FLAG_*| and modifies the behavior. On match,
3016// if |out_peername| is non-NULL, it additionally sets |*out_peername| to a
3017// newly-allocated, NUL-terminated string containing the DNS name or wildcard in
3018// the certificate which matched. The caller must then free |*out_peername| with
3019// |OPENSSL_free| when done.
3020//
3021// By default, both subject alternative names and the subject's common name
3022// attribute are checked. The latter has long been deprecated, so callers should
3023// include |X509_CHECK_FLAG_NEVER_CHECK_SUBJECT| in |flags| to use the standard
3024// behavior. https://crbug.com/boringssl/464 tracks fixing the default.
3025//
3026// This function does not check if |x509| is a trusted certificate, only if,
3027// were it trusted, it would match |chk|.
3028//
3029// WARNING: This function differs from the usual calling convention and may
3030// return either 0 or a negative number on error.
3031//
3032// TODO(davidben): Make the error case also return zero.
3033OPENSSL_EXPORT int X509_check_host(const X509 *x509, const char *chk,
3034 size_t chklen, unsigned int flags,
3035 char **out_peername);
3036
3037// X509_check_email checks if |x509| matches the email address |chk|. It returns
3038// one on match, zero on mismatch, or a negative number on error. |flags| should
3039// be some combination of |X509_CHECK_FLAG_*| and modifies the behavior.
3040//
3041// By default, both subject alternative names and the subject's email address
3042// attribute are checked. The |X509_CHECK_FLAG_NEVER_CHECK_SUBJECT| flag may be
3043// used to change this behavior.
3044//
3045// This function does not check if |x509| is a trusted certificate, only if,
3046// were it trusted, it would match |chk|.
3047//
3048// WARNING: This function differs from the usual calling convention and may
3049// return either 0 or a negative number on error.
3050//
3051// TODO(davidben): Make the error case also return zero.
3052OPENSSL_EXPORT int X509_check_email(const X509 *x509, const char *chk,
3053 size_t chklen, unsigned int flags);
3054
3055// X509_check_ip checks if |x509| matches the IP address |chk|. The IP address
3056// is represented in byte form and should be 4 bytes for an IPv4 address and 16
3057// bytes for an IPv6 address. It returns one on match, zero on mismatch, or a
3058// negative number on error. |flags| should be some combination of
3059// |X509_CHECK_FLAG_*| and modifies the behavior.
3060//
3061// This function does not check if |x509| is a trusted certificate, only if,
3062// were it trusted, it would match |chk|.
3063//
3064// WARNING: This function differs from the usual calling convention and may
3065// return either 0 or a negative number on error.
3066//
3067// TODO(davidben): Make the error case also return zero.
3068OPENSSL_EXPORT int X509_check_ip(const X509 *x509, const uint8_t *chk,
3069 size_t chklen, unsigned int flags);
3070
3071// X509_check_ip_asc behaves like |X509_check_ip| except the IP address is
3072// specified in textual form in |ipasc|.
3073//
3074// WARNING: This function differs from the usual calling convention and may
3075// return either 0 or a negative number on error.
3076//
3077// TODO(davidben): Make the error case also return zero.
3078OPENSSL_EXPORT int X509_check_ip_asc(const X509 *x509, const char *ipasc,
3079 unsigned int flags);
3080
David Benjaminb6694ec2024-01-03 16:40:51 -05003081// X509_STORE_CTX_get1_issuer looks up a candidate trusted issuer for |x509| out
3082// of |ctx|'s |X509_STORE|, based on the criteria in |X509_check_issued|. If one
3083// was found, it returns one and sets |*out_issuer| to the issuer. The caller
3084// must release |*out_issuer| with |X509_free| when done. If none was found, it
3085// returns zero and leaves |*out_issuer| unchanged.
3086//
3087// This function only searches for trusted issuers. It does not consider
3088// untrusted intermediates passed in to |X509_STORE_CTX_init|.
3089//
3090// TODO(crbug.com/boringssl/407): |x509| should be const.
3091OPENSSL_EXPORT int X509_STORE_CTX_get1_issuer(X509 **out_issuer,
3092 X509_STORE_CTX *ctx, X509 *x509);
3093
David Benjamin9e404812023-12-06 22:33:21 -05003094
David Benjamin38c4bf12023-11-29 10:48:37 -05003095// X.509 information.
3096//
3097// |X509_INFO| is the return type for |PEM_X509_INFO_read_bio|, defined in
3098// <openssl/pem.h>. It is used to store a certificate, CRL, or private key. This
3099// type is defined in this header for OpenSSL compatibility.
3100
3101struct private_key_st {
3102 EVP_PKEY *dec_pkey;
3103} /* X509_PKEY */;
3104
3105struct X509_info_st {
3106 X509 *x509;
3107 X509_CRL *crl;
3108 X509_PKEY *x_pkey;
3109
3110 EVP_CIPHER_INFO enc_cipher;
3111 int enc_len;
3112 char *enc_data;
3113} /* X509_INFO */;
3114
3115DEFINE_STACK_OF(X509_INFO)
3116
3117// X509_INFO_free releases memory associated with |info|.
3118OPENSSL_EXPORT void X509_INFO_free(X509_INFO *info);
3119
3120
David Benjamin2d7adae2023-11-21 00:11:09 -05003121// Deprecated config-based extension creation.
3122//
3123// The following functions allow specifying X.509 extensions using OpenSSL's
3124// config file syntax, from the OpenSSL command-line tool. They are retained,
3125// for now, for compatibility with legacy software but may be removed in the
3126// future. Construct the extensions using the typed C APIs instead.
3127//
3128// Callers should especially avoid these functions if passing in non-constant
3129// values. They use ad-hoc, string-based formats which are prone to injection
3130// vulnerabilities. For a CA, this means using them risks misissuance.
3131//
3132// These functions are not safe to use with untrusted inputs. The string formats
3133// may implicitly reference context information and, in OpenSSL (though not
3134// BoringSSL), one even allows reading arbitrary files. Many formats can also
3135// produce far larger outputs than their inputs, so untrusted inputs may lead to
3136// denial-of-service attacks. Finally, the parsers see much less testing and
3137// review than most of the library and may have bugs including memory leaks or
3138// crashes.
3139
3140// v3_ext_ctx, aka |X509V3_CTX|, contains additional context information for
3141// constructing extensions. Some string formats reference additional values in
3142// these objects. It must be initialized with |X509V3_set_ctx| or
3143// |X509V3_set_ctx_test| before use.
3144struct v3_ext_ctx {
3145 int flags;
3146 const X509 *issuer_cert;
3147 const X509 *subject_cert;
3148 const X509_REQ *subject_req;
3149 const X509_CRL *crl;
3150 const CONF *db;
3151};
3152
3153#define X509V3_CTX_TEST 0x1
3154
3155// X509V3_set_ctx initializes |ctx| with the specified objects. Some string
3156// formats will reference fields in these objects. Each object may be NULL to
3157// omit it, in which case those formats cannot be used. |flags| should be zero,
3158// unless called via |X509V3_set_ctx_test|.
3159//
3160// |issuer|, |subject|, |req|, and |crl|, if non-NULL, must outlive |ctx|.
3161OPENSSL_EXPORT void X509V3_set_ctx(X509V3_CTX *ctx, const X509 *issuer,
3162 const X509 *subject, const X509_REQ *req,
3163 const X509_CRL *crl, int flags);
3164
3165// X509V3_set_ctx_test calls |X509V3_set_ctx| without any reference objects and
3166// mocks out some features that use them. The resulting extensions may be
3167// incomplete and should be discarded. This can be used to partially validate
3168// syntax.
3169//
3170// TODO(davidben): Can we remove this?
3171#define X509V3_set_ctx_test(ctx) \
3172 X509V3_set_ctx(ctx, NULL, NULL, NULL, NULL, X509V3_CTX_TEST)
3173
3174// X509V3_set_nconf sets |ctx| to use |conf| as the config database. |ctx| must
3175// have previously been initialized by |X509V3_set_ctx| or
3176// |X509V3_set_ctx_test|. Some string formats will reference sections in |conf|.
3177// |conf| may be NULL, in which case these formats cannot be used. If non-NULL,
3178// |conf| must outlive |ctx|.
3179OPENSSL_EXPORT void X509V3_set_nconf(X509V3_CTX *ctx, const CONF *conf);
3180
3181// X509V3_set_ctx_nodb calls |X509V3_set_nconf| with no config database.
3182#define X509V3_set_ctx_nodb(ctx) X509V3_set_nconf(ctx, NULL)
3183
3184// X509V3_EXT_nconf constructs an extension of type specified by |name|, and
3185// value specified by |value|. It returns a newly-allocated |X509_EXTENSION|
3186// object on success, or NULL on error. |conf| and |ctx| specify additional
3187// information referenced by some formats. Either |conf| or |ctx| may be NULL,
3188// in which case features which use it will be disabled.
3189//
3190// If non-NULL, |ctx| must be initialized with |X509V3_set_ctx| or
3191// |X509V3_set_ctx_test|.
3192//
3193// Both |conf| and |ctx| provide a |CONF| object. When |ctx| is non-NULL, most
3194// features use the |ctx| copy, configured with |X509V3_set_ctx|, but some use
3195// |conf|. Callers should ensure the two match to avoid surprisingly behavior.
3196OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_nconf(const CONF *conf,
3197 const X509V3_CTX *ctx,
3198 const char *name,
3199 const char *value);
3200
3201// X509V3_EXT_nconf_nid behaves like |X509V3_EXT_nconf|, except the extension
3202// type is specified as a NID.
3203OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_nconf_nid(const CONF *conf,
3204 const X509V3_CTX *ctx,
3205 int ext_nid,
3206 const char *value);
3207
3208// X509V3_EXT_conf_nid calls |X509V3_EXT_nconf_nid|. |conf| must be NULL.
3209//
3210// TODO(davidben): This is the only exposed instance of an LHASH in our public
3211// headers. cryptography.io wraps this function so we cannot, yet, replace the
3212// type with a dummy struct.
3213OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_conf_nid(LHASH_OF(CONF_VALUE) *conf,
3214 const X509V3_CTX *ctx,
3215 int ext_nid,
3216 const char *value);
3217
3218// X509V3_EXT_add_nconf_sk looks up the section named |section| in |conf|. For
3219// each |CONF_VALUE| in the section, it constructs an extension as in
3220// |X509V3_EXT_nconf|, taking |name| and |value| from the |CONF_VALUE|. Each new
3221// extension is appended to |*sk|. If |*sk| is non-NULL, and at least one
3222// extension is added, it sets |*sk| to a newly-allocated
3223// |STACK_OF(X509_EXTENSION)|. It returns one on success and zero on error.
3224OPENSSL_EXPORT int X509V3_EXT_add_nconf_sk(const CONF *conf,
3225 const X509V3_CTX *ctx,
3226 const char *section,
3227 STACK_OF(X509_EXTENSION) **sk);
3228
3229// X509V3_EXT_add_nconf adds extensions to |cert| as in
3230// |X509V3_EXT_add_nconf_sk|. It returns one on success and zero on error.
3231OPENSSL_EXPORT int X509V3_EXT_add_nconf(const CONF *conf, const X509V3_CTX *ctx,
3232 const char *section, X509 *cert);
3233
3234// X509V3_EXT_REQ_add_nconf adds extensions to |req| as in
3235// |X509V3_EXT_add_nconf_sk|. It returns one on success and zero on error.
3236OPENSSL_EXPORT int X509V3_EXT_REQ_add_nconf(const CONF *conf,
3237 const X509V3_CTX *ctx,
3238 const char *section, X509_REQ *req);
3239
3240// X509V3_EXT_CRL_add_nconf adds extensions to |crl| as in
3241// |X509V3_EXT_add_nconf_sk|. It returns one on success and zero on error.
3242OPENSSL_EXPORT int X509V3_EXT_CRL_add_nconf(const CONF *conf,
3243 const X509V3_CTX *ctx,
3244 const char *section, X509_CRL *crl);
3245
David Benjamin314c2522023-11-27 23:26:09 -05003246// i2s_ASN1_OCTET_STRING returns a human-readable representation of |oct| as a
3247// newly-allocated, NUL-terminated string, or NULL on error. |method| is
3248// ignored. The caller must release the result with |OPENSSL_free| when done.
3249OPENSSL_EXPORT char *i2s_ASN1_OCTET_STRING(const X509V3_EXT_METHOD *method,
3250 const ASN1_OCTET_STRING *oct);
3251
3252// s2i_ASN1_OCTET_STRING decodes |str| as a hexdecimal byte string, with
3253// optional colon separators between bytes. It returns a newly-allocated
3254// |ASN1_OCTET_STRING| with the result on success, or NULL on error. |method|
3255// and |ctx| are ignored.
3256OPENSSL_EXPORT ASN1_OCTET_STRING *s2i_ASN1_OCTET_STRING(
3257 const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, const char *str);
3258
3259// i2s_ASN1_INTEGER returns a human-readable representation of |aint| as a
3260// newly-allocated, NUL-terminated string, or NULL on error. |method| is
3261// ignored. The caller must release the result with |OPENSSL_free| when done.
3262OPENSSL_EXPORT char *i2s_ASN1_INTEGER(const X509V3_EXT_METHOD *method,
3263 const ASN1_INTEGER *aint);
3264
3265// s2i_ASN1_INTEGER decodes |value| as the ASCII representation of an integer,
3266// and returns a newly-allocated |ASN1_INTEGER| containing the result, or NULL
3267// on error. |method| is ignored. If |value| begins with "0x" or "0X", the input
3268// is decoded in hexadecimal, otherwise decimal.
3269OPENSSL_EXPORT ASN1_INTEGER *s2i_ASN1_INTEGER(const X509V3_EXT_METHOD *method,
3270 const char *value);
3271
3272// i2s_ASN1_ENUMERATED returns a human-readable representation of |aint| as a
3273// newly-allocated, NUL-terminated string, or NULL on error. |method| is
3274// ignored. The caller must release the result with |OPENSSL_free| when done.
3275OPENSSL_EXPORT char *i2s_ASN1_ENUMERATED(const X509V3_EXT_METHOD *method,
3276 const ASN1_ENUMERATED *aint);
3277
3278// X509V3_conf_free releases memory associated with |CONF_VALUE|.
3279OPENSSL_EXPORT void X509V3_conf_free(CONF_VALUE *val);
3280
3281// i2v_GENERAL_NAME serializes |gen| as a |CONF_VALUE|. If |ret| is non-NULL, it
3282// appends the value to |ret| and returns |ret| on success or NULL on error. If
3283// it returns NULL, the caller is still responsible for freeing |ret|. If |ret|
3284// is NULL, it returns a newly-allocated |STACK_OF(CONF_VALUE)| containing the
3285// result. |method| is ignored. When done, the caller should release the result
3286// with |sk_CONF_VALUE_pop_free| and |X509V3_conf_free|.
3287//
3288// Do not use this function. This is an internal implementation detail of the
3289// human-readable print functions. If extracting a SAN list from a certificate,
3290// look at |gen| directly.
3291OPENSSL_EXPORT STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(
3292 const X509V3_EXT_METHOD *method, const GENERAL_NAME *gen,
3293 STACK_OF(CONF_VALUE) *ret);
3294
3295// i2v_GENERAL_NAMES serializes |gen| as a list of |CONF_VALUE|s. If |ret| is
3296// non-NULL, it appends the values to |ret| and returns |ret| on success or NULL
3297// on error. If it returns NULL, the caller is still responsible for freeing
3298// |ret|. If |ret| is NULL, it returns a newly-allocated |STACK_OF(CONF_VALUE)|
3299// containing the results. |method| is ignored.
3300//
3301// Do not use this function. This is an internal implementation detail of the
3302// human-readable print functions. If extracting a SAN list from a certificate,
3303// look at |gen| directly.
3304OPENSSL_EXPORT STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES(
3305 const X509V3_EXT_METHOD *method, const GENERAL_NAMES *gen,
3306 STACK_OF(CONF_VALUE) *extlist);
3307
3308// a2i_IPADDRESS decodes |ipasc| as the textual representation of an IPv4 or
3309// IPv6 address. On success, it returns a newly-allocated |ASN1_OCTET_STRING|
3310// containing the decoded IP address. IPv4 addresses are represented as 4-byte
3311// strings and IPv6 addresses as 16-byte strings. On failure, it returns NULL.
3312OPENSSL_EXPORT ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc);
3313
3314// a2i_IPADDRESS_NC decodes |ipasc| as the textual representation of an IPv4 or
3315// IPv6 address range. On success, it returns a newly-allocated
3316// |ASN1_OCTET_STRING| containing the decoded IP address, followed by the
3317// decoded mask. IPv4 ranges are represented as 8-byte strings and IPv6 ranges
3318// as 32-byte strings. On failure, it returns NULL.
3319//
3320// The text format decoded by this function is not the standard CIDR notiation.
3321// Instead, the mask after the "/" is represented as another IP address. For
3322// example, "192.168.0.0/16" would be written "192.168.0.0/255.255.0.0".
3323OPENSSL_EXPORT ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc);
3324
David Benjamin2d7adae2023-11-21 00:11:09 -05003325
David Benjamin4363bdd2022-04-06 15:19:16 -04003326// Deprecated functions.
3327
3328// X509_get_notBefore returns |x509|'s notBefore time. Note this function is not
3329// const-correct for legacy reasons. Use |X509_get0_notBefore| or
3330// |X509_getm_notBefore| instead.
3331OPENSSL_EXPORT ASN1_TIME *X509_get_notBefore(const X509 *x509);
3332
3333// X509_get_notAfter returns |x509|'s notAfter time. Note this function is not
3334// const-correct for legacy reasons. Use |X509_get0_notAfter| or
3335// |X509_getm_notAfter| instead.
3336OPENSSL_EXPORT ASN1_TIME *X509_get_notAfter(const X509 *x509);
3337
3338// X509_set_notBefore calls |X509_set1_notBefore|. Use |X509_set1_notBefore|
3339// instead.
3340OPENSSL_EXPORT int X509_set_notBefore(X509 *x509, const ASN1_TIME *tm);
3341
3342// X509_set_notAfter calls |X509_set1_notAfter|. Use |X509_set1_notAfter|
3343// instead.
3344OPENSSL_EXPORT int X509_set_notAfter(X509 *x509, const ASN1_TIME *tm);
3345
David Benjamin884ae3a2022-07-08 18:18:12 -04003346// X509_CRL_get_lastUpdate returns a mutable pointer to |crl|'s thisUpdate time.
3347// The OpenSSL API refers to this field as lastUpdate.
3348//
David Benjamin4363bdd2022-04-06 15:19:16 -04003349// Use |X509_CRL_get0_lastUpdate| or |X509_CRL_set1_lastUpdate| instead.
3350OPENSSL_EXPORT ASN1_TIME *X509_CRL_get_lastUpdate(X509_CRL *crl);
3351
3352// X509_CRL_get_nextUpdate returns a mutable pointer to |crl|'s nextUpdate time,
3353// or NULL if |crl| has none. Use |X509_CRL_get0_nextUpdate| or
3354// |X509_CRL_set1_nextUpdate| instead.
3355OPENSSL_EXPORT ASN1_TIME *X509_CRL_get_nextUpdate(X509_CRL *crl);
3356
3357// X509_extract_key is a legacy alias to |X509_get_pubkey|. Use
3358// |X509_get_pubkey| instead.
3359#define X509_extract_key(x) X509_get_pubkey(x)
3360
3361// X509_REQ_extract_key is a legacy alias for |X509_REQ_get_pubkey|.
3362#define X509_REQ_extract_key(a) X509_REQ_get_pubkey(a)
3363
3364// X509_name_cmp is a legacy alias for |X509_NAME_cmp|.
3365#define X509_name_cmp(a, b) X509_NAME_cmp((a), (b))
3366
3367// The following symbols are deprecated aliases to |X509_CRL_set1_*|.
3368#define X509_CRL_set_lastUpdate X509_CRL_set1_lastUpdate
3369#define X509_CRL_set_nextUpdate X509_CRL_set1_nextUpdate
3370
3371// X509_get_serialNumber returns a mutable pointer to |x509|'s serial number.
3372// Prefer |X509_get0_serialNumber|.
3373OPENSSL_EXPORT ASN1_INTEGER *X509_get_serialNumber(X509 *x509);
3374
David Benjaminb6f47e82022-07-09 00:57:54 -04003375// X509_NAME_get_text_by_OBJ finds the first attribute with type |obj| in
Bob Beck3763efb2023-03-29 11:17:46 -06003376// |name|. If found, it writes the value's UTF-8 representation to |buf|.
3377// followed by a NUL byte, and returns the number of bytes in the output,
3378// excluding the NUL byte. This is unlike OpenSSL which returns the raw
3379// ASN1_STRING data. The UTF-8 encoding of the |ASN1_STRING| may not contain a 0
3380// codepoint.
David Benjaminb6f47e82022-07-09 00:57:54 -04003381//
Bob Beck3763efb2023-03-29 11:17:46 -06003382// This function writes at most |len| bytes, including the NUL byte. If |buf|
3383// is NULL, it writes nothing and returns the number of bytes in the
3384// output, excluding the NUL byte that would be required for the full UTF-8
3385// output.
David Benjaminb6f47e82022-07-09 00:57:54 -04003386//
Bob Beck3763efb2023-03-29 11:17:46 -06003387// This function may return -1 if an error occurs for any reason, including the
3388// value not being a recognized string type, |len| being of insufficient size to
3389// hold the full UTF-8 encoding and NUL byte, memory allocation failures, an
3390// object with type |obj| not existing in |name|, or if the UTF-8 encoding of
3391// the string contains a zero byte.
David Benjaminb6f47e82022-07-09 00:57:54 -04003392OPENSSL_EXPORT int X509_NAME_get_text_by_OBJ(const X509_NAME *name,
3393 const ASN1_OBJECT *obj, char *buf,
3394 int len);
3395
3396// X509_NAME_get_text_by_NID behaves like |X509_NAME_get_text_by_OBJ| except it
3397// finds an attribute of type |nid|, which should be one of the |NID_*|
3398// constants.
3399OPENSSL_EXPORT int X509_NAME_get_text_by_NID(const X509_NAME *name, int nid,
3400 char *buf, int len);
3401
David Benjamina4851dd2023-11-12 15:51:49 -05003402// X509_STORE_CTX_get0_parent_ctx returns NULL.
3403OPENSSL_EXPORT X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(
David Benjamin04b2d612023-12-25 17:12:27 -05003404 const X509_STORE_CTX *ctx);
David Benjamina4851dd2023-11-12 15:51:49 -05003405
David Benjaminc2b7df52023-11-18 19:31:24 -05003406// X509_OBJECT_free_contents sets |obj| to the empty object, freeing any values
3407// that were previously there.
3408//
3409// TODO(davidben): Unexport this function after rust-openssl is fixed to no
3410// longer call it.
3411OPENSSL_EXPORT void X509_OBJECT_free_contents(X509_OBJECT *obj);
3412
David Benjamind9b81bb2023-11-18 19:48:21 -05003413// X509_LOOKUP_free releases memory associated with |ctx|. This function should
3414// never be used outside the library. No function in the public API hands
3415// ownership of an |X509_LOOKUP| to the caller.
3416//
3417// TODO(davidben): Unexport this function after rust-openssl is fixed to no
3418// longer call it.
3419OPENSSL_EXPORT void X509_LOOKUP_free(X509_LOOKUP *ctx);
3420
David Benjamin2ba76342023-11-19 10:20:04 -05003421// X509_STORE_CTX_cleanup resets |ctx| to the empty state.
3422//
3423// This function is a remnant of when |X509_STORE_CTX| was stack-allocated and
3424// should not be used. If releasing |ctx|, call |X509_STORE_CTX_free|. If
3425// reusing |ctx| for a new verification, release the old one and create a new
3426// one.
3427OPENSSL_EXPORT void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx);
3428
David Benjamin2d7adae2023-11-21 00:11:09 -05003429// X509V3_add_standard_extensions returns one.
3430OPENSSL_EXPORT int X509V3_add_standard_extensions(void);
3431
David Benjamin33a5e942023-11-22 02:55:41 -05003432// The following symbols are legacy aliases for |X509_STORE_CTX| functions.
3433#define X509_STORE_get_by_subject X509_STORE_CTX_get_by_subject
3434#define X509_STORE_get1_certs X509_STORE_CTX_get1_certs
3435#define X509_STORE_get1_crls X509_STORE_CTX_get1_crls
3436
David Benjamine9539952024-01-05 17:57:14 -05003437// X509_STORE_CTX_get_chain is a legacy alias for |X509_STORE_CTX_get0_chain|.
David Benjamin04b2d612023-12-25 17:12:27 -05003438OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get_chain(
3439 const X509_STORE_CTX *ctx);
David Benjamine9539952024-01-05 17:57:14 -05003440
3441// X509_STORE_CTX_trusted_stack is a deprecated alias for
3442// |X509_STORE_CTX_set0_trusted_stack|.
3443OPENSSL_EXPORT void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx,
3444 STACK_OF(X509) *sk);
3445
3446typedef int (*X509_STORE_CTX_verify_cb)(int, X509_STORE_CTX *);
3447
3448// X509_STORE_CTX_set_verify_cb configures a callback function for |ctx| that is
3449// called multiple times during |X509_verify_cert|. The callback returns zero to
3450// fail verification and one to proceed. Typically, it will return |ok|, which
3451// preserves the default behavior. Returning one when |ok| is zero will proceed
3452// past some error. The callback may inspect |ctx| and the error queue to
3453// attempt to determine the current stage of certificate verification, but this
3454// is often unreliable. When synthesizing an error, callbacks should use
3455// |X509_STORE_CTX_set_error| to set a corresponding error.
3456//
3457// WARNING: Do not use this function. It is extremely fragile and unpredictable.
3458// This callback exposes implementation details of certificate verification,
3459// which change as the library evolves. Attempting to use it for security checks
3460// can introduce vulnerabilities if making incorrect assumptions about when the
3461// callback is called. Some errors, when suppressed, may implicitly suppress
3462// other errors due to internal implementation details. Additionally, overriding
3463// |ok| may leave |ctx| in an inconsistent state and break invariants.
3464//
3465// Instead, customize certificate verification by configuring options on the
3466// |X509_STORE_CTX| before verification, or applying additional checks after
3467// |X509_verify_cert| completes successfully.
3468OPENSSL_EXPORT void X509_STORE_CTX_set_verify_cb(
3469 X509_STORE_CTX *ctx, int (*verify_cb)(int ok, X509_STORE_CTX *ctx));
3470
3471// X509_STORE_set_verify_cb acts like |X509_STORE_CTX_set_verify_cb| but sets
3472// the verify callback for any |X509_STORE_CTX| created from this |X509_STORE|
3473//
3474// Do not use this function. See |X509_STORE_CTX_set_verify_cb| for details.
3475OPENSSL_EXPORT void X509_STORE_set_verify_cb(
3476 X509_STORE *store, X509_STORE_CTX_verify_cb verify_cb);
3477
3478// X509_STORE_set_verify_cb_func is a deprecated alias for
3479// |X509_STORE_set_verify_cb|.
3480#define X509_STORE_set_verify_cb_func(store, func) \
3481 X509_STORE_set_verify_cb((store), (func))
3482
3483typedef int (*X509_STORE_CTX_get_crl_fn)(X509_STORE_CTX *ctx, X509_CRL **crl,
3484 X509 *x);
3485typedef int (*X509_STORE_CTX_check_crl_fn)(X509_STORE_CTX *ctx, X509_CRL *crl);
3486
3487// X509_STORE_set_get_crl override's |store|'s logic for looking up CRLs.
3488//
3489// Do not use this function. It is temporarily retained to support one caller
3490// and will be removed after that caller is fixed. It is not possible for
3491// external callers to correctly implement this callback. The real
3492// implementation sets some inaccessible internal state on |X509_STORE_CTX|.
3493OPENSSL_EXPORT void X509_STORE_set_get_crl(X509_STORE *store,
3494 X509_STORE_CTX_get_crl_fn get_crl);
3495
3496// X509_STORE_set_check_crl override's |store|'s logic for checking CRL
3497// validity.
3498//
3499// Do not use this function. It is temporarily retained to support one caller
3500// and will be removed after that caller is fixed. It is not possible for
3501// external callers to correctly implement this callback. The real
3502// implementation relies some inaccessible internal state on |X509_STORE_CTX|.
3503OPENSSL_EXPORT void X509_STORE_set_check_crl(
3504 X509_STORE *store, X509_STORE_CTX_check_crl_fn check_crl);
3505
3506// X509_STORE_CTX_set_chain configures |ctx| to use |sk| for untrusted
3507// intermediate certificates to use in verification. This function is redundant
3508// with the |chain| parameter of |X509_STORE_CTX_init|. Use the parameter
3509// instead.
3510//
3511// WARNING: Despite the similar name, this function is unrelated to
3512// |X509_STORE_CTX_get0_chain|.
3513//
3514// WARNING: This function saves a pointer to |sk| without copying or
3515// incrementing reference counts. |sk| must outlive |ctx| and may not be mutated
3516// for the duration of the certificate verification.
3517OPENSSL_EXPORT void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx,
3518 STACK_OF(X509) *sk);
3519
David Benjamin4363bdd2022-04-06 15:19:16 -04003520
3521// Private structures.
3522
3523struct X509_algor_st {
3524 ASN1_OBJECT *algorithm;
3525 ASN1_TYPE *parameter;
3526} /* X509_ALGOR */;
3527
3528
3529// Functions below this point have not yet been organized into sections.
3530
David Benjamin991d31b2020-06-19 11:38:20 -04003531// This stuff is certificate "auxiliary info"
3532// it contains details which are useful in certificate
3533// stores and databases. When used this is tagged onto
3534// the end of the certificate itself
Adam Langley95c29f32014-06-20 12:00:00 -07003535
David Benjamin01f8a8c2017-04-15 18:12:55 -04003536DECLARE_STACK_OF(DIST_POINT)
David Benjamin01f8a8c2017-04-15 18:12:55 -04003537
David Benjamin991d31b2020-06-19 11:38:20 -04003538// This is used for a table of trust checking functions
Adam Langley95c29f32014-06-20 12:00:00 -07003539
David Benjamin43bd18f2015-08-04 10:45:18 -04003540struct x509_trust_st {
David Benjamin991d31b2020-06-19 11:38:20 -04003541 int trust;
3542 int flags;
David Benjamin6099ab92023-12-02 17:20:35 -05003543 int (*check_trust)(const X509_TRUST *, X509 *, int);
David Benjamin991d31b2020-06-19 11:38:20 -04003544 char *name;
3545 int arg1;
3546 void *arg2;
David Benjamin43bd18f2015-08-04 10:45:18 -04003547} /* X509_TRUST */;
Adam Langley95c29f32014-06-20 12:00:00 -07003548
David Benjamin01f8a8c2017-04-15 18:12:55 -04003549DEFINE_STACK_OF(X509_TRUST)
Adam Langley95c29f32014-06-20 12:00:00 -07003550
David Benjamin991d31b2020-06-19 11:38:20 -04003551// standard trust ids
Adam Langley95c29f32014-06-20 12:00:00 -07003552
David Benjamin991d31b2020-06-19 11:38:20 -04003553#define X509_TRUST_DEFAULT (-1) // Only valid in purpose settings
Adam Langley95c29f32014-06-20 12:00:00 -07003554
David Benjamin991d31b2020-06-19 11:38:20 -04003555#define X509_TRUST_COMPAT 1
3556#define X509_TRUST_SSL_CLIENT 2
3557#define X509_TRUST_SSL_SERVER 3
3558#define X509_TRUST_EMAIL 4
3559#define X509_TRUST_OBJECT_SIGN 5
David Benjamin991d31b2020-06-19 11:38:20 -04003560#define X509_TRUST_TSA 8
Adam Langley95c29f32014-06-20 12:00:00 -07003561
David Benjamin991d31b2020-06-19 11:38:20 -04003562// check_trust return codes
Adam Langley95c29f32014-06-20 12:00:00 -07003563
David Benjamin991d31b2020-06-19 11:38:20 -04003564#define X509_TRUST_TRUSTED 1
3565#define X509_TRUST_REJECTED 2
3566#define X509_TRUST_UNTRUSTED 3
Adam Langley95c29f32014-06-20 12:00:00 -07003567
David Benjamin4ef5de02020-08-25 20:00:44 -04003568// X509_verify_cert_error_string returns |err| as a human-readable string, where
3569// |err| should be one of the |X509_V_*| values. If |err| is unknown, it returns
3570// a default description.
David Benjamin4ef5de02020-08-25 20:00:44 -04003571OPENSSL_EXPORT const char *X509_verify_cert_error_string(long err);
Adam Langley95c29f32014-06-20 12:00:00 -07003572
David Benjamin991d31b2020-06-19 11:38:20 -04003573OPENSSL_EXPORT const char *X509_get_default_cert_area(void);
3574OPENSSL_EXPORT const char *X509_get_default_cert_dir(void);
3575OPENSSL_EXPORT const char *X509_get_default_cert_file(void);
3576OPENSSL_EXPORT const char *X509_get_default_cert_dir_env(void);
3577OPENSSL_EXPORT const char *X509_get_default_cert_file_env(void);
3578OPENSSL_EXPORT const char *X509_get_default_private_dir(void);
Adam Langley95c29f32014-06-20 12:00:00 -07003579
David Benjamind0f14f32022-03-08 15:55:42 -05003580
David Benjamind0f14f32022-03-08 15:55:42 -05003581OPENSSL_EXPORT int X509_TRUST_set(int *t, int trust);
3582
David Benjamin991d31b2020-06-19 11:38:20 -04003583OPENSSL_EXPORT int X509_cmp(const X509 *a, const X509 *b);
David Benjaminc77d0f82023-12-06 23:07:11 -05003584
3585// X509_NAME_hash returns a hash of |name|, or zero on error. This is the new
3586// hash used by |X509_LOOKUP_hash_dir|.
3587//
3588// TODO(https://crbug.com/boringssl/407): This should be const and thread-safe
3589// but currently is neither, notably if |name| was modified from its parsed
3590// value.
3591OPENSSL_EXPORT uint32_t X509_NAME_hash(X509_NAME *name);
3592
3593// X509_NAME_hash_old returns a hash of |name|, or zero on error. This is the
3594// legacy hash used by |X509_LOOKUP_hash_dir|, which is still supported for
3595// compatibility.
3596//
3597// TODO(https://crbug.com/boringssl/407): This should be const and thread-safe
3598// but currently is neither, notably if |name| was modified from its parsed
3599// value.
3600OPENSSL_EXPORT uint32_t X509_NAME_hash_old(X509_NAME *name);
Adam Langley95c29f32014-06-20 12:00:00 -07003601
David Benjamin991d31b2020-06-19 11:38:20 -04003602OPENSSL_EXPORT int X509_CRL_match(const X509_CRL *a, const X509_CRL *b);
Adam Langley95c29f32014-06-20 12:00:00 -07003603
David Benjamin8ab77ee2023-12-21 22:34:59 -05003604// X509_verify_cert performs certifice verification with |ctx|, which must have
3605// been initialized with |X509_STORE_CTX_init|. It returns one on success and
3606// zero on error. On success, |X509_STORE_CTX_get0_chain| or
3607// |X509_STORE_CTX_get1_chain| may be used to return the verified certificate
3608// chain. On error, |X509_STORE_CTX_get_error| may be used to return additional
3609// error information.
David Benjamin991d31b2020-06-19 11:38:20 -04003610OPENSSL_EXPORT int X509_verify_cert(X509_STORE_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07003611
Adam Langleyeb7d2ed2014-07-30 16:02:14 -07003612OPENSSL_EXPORT int X509_check_trust(X509 *x, int id, int flags);
3613OPENSSL_EXPORT int X509_TRUST_get_count(void);
David Benjamin6099ab92023-12-02 17:20:35 -05003614OPENSSL_EXPORT const X509_TRUST *X509_TRUST_get0(int idx);
Adam Langleyeb7d2ed2014-07-30 16:02:14 -07003615OPENSSL_EXPORT int X509_TRUST_get_by_id(int id);
David Benjamin125a38f2020-08-20 14:54:02 -04003616OPENSSL_EXPORT int X509_TRUST_get_flags(const X509_TRUST *xp);
3617OPENSSL_EXPORT char *X509_TRUST_get0_name(const X509_TRUST *xp);
3618OPENSSL_EXPORT int X509_TRUST_get_trust(const X509_TRUST *xp);
Adam Langley95c29f32014-06-20 12:00:00 -07003619
David Benjamin17727c62016-01-30 14:58:52 -05003620
David Benjaminc31a8a62021-10-03 22:47:13 -04003621/*
3622SSL_CTX -> X509_STORE
3623 -> X509_LOOKUP
3624 ->X509_LOOKUP_METHOD
3625 -> X509_LOOKUP
3626 ->X509_LOOKUP_METHOD
3627
3628SSL -> X509_STORE_CTX
3629 ->X509_STORE
3630
3631The X509_STORE holds the tables etc for verification stuff.
3632A X509_STORE_CTX is used while validating a single certificate.
3633The X509_STORE has X509_LOOKUPs for looking up certs.
3634The X509_STORE then calls a function to actually verify the
3635certificate chain.
3636*/
3637
David Benjaminc2b7df52023-11-18 19:31:24 -05003638#define X509_LU_NONE 0
David Benjaminc31a8a62021-10-03 22:47:13 -04003639#define X509_LU_X509 1
3640#define X509_LU_CRL 2
3641#define X509_LU_PKEY 3
3642
David Benjaminc31a8a62021-10-03 22:47:13 -04003643DEFINE_STACK_OF(X509_OBJECT)
David Benjaminc31a8a62021-10-03 22:47:13 -04003644
David Benjaminc31a8a62021-10-03 22:47:13 -04003645
David Benjaminb251d812023-12-11 01:18:00 -05003646// X509_STORE_set_depth configures |store| to, by default, limit certificate
3647// chains to |depth| intermediate certificates. This count excludes both the
3648// target certificate and the trust anchor (root certificate).
David Benjaminc31a8a62021-10-03 22:47:13 -04003649OPENSSL_EXPORT int X509_STORE_set_depth(X509_STORE *store, int depth);
3650
David Benjaminb251d812023-12-11 01:18:00 -05003651// X509_STORE_CTX_set_depth configures |ctx| to, by default, limit certificate
3652// chains to |depth| intermediate certificates. This count excludes both the
3653// target certificate and the trust anchor (root certificate).
David Benjaminc31a8a62021-10-03 22:47:13 -04003654OPENSSL_EXPORT void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth);
3655
3656#define X509_STORE_CTX_set_app_data(ctx, data) \
3657 X509_STORE_CTX_set_ex_data(ctx, 0, data)
3658#define X509_STORE_CTX_get_app_data(ctx) X509_STORE_CTX_get_ex_data(ctx, 0)
3659
3660#define X509_L_FILE_LOAD 1
3661#define X509_L_ADD_DIR 2
3662
David Benjamin2ff409e2023-11-22 01:32:03 -05003663// The following constants are used to specify the format of files in an
3664// |X509_LOOKUP|.
3665#define X509_FILETYPE_PEM 1
3666#define X509_FILETYPE_ASN1 2
3667#define X509_FILETYPE_DEFAULT 3
David Benjaminc31a8a62021-10-03 22:47:13 -04003668
David Benjamin2ff409e2023-11-22 01:32:03 -05003669// X509_LOOKUP_load_file configures |lookup| to load information from the file
3670// at |path|. It returns one on success and zero on error. |type| should be one
3671// of the |X509_FILETYPE_*| constants to determine if the contents are PEM or
3672// DER. If |type| is |X509_FILETYPE_DEFAULT|, |path| is ignored and instead some
3673// default system path is used.
3674OPENSSL_EXPORT int X509_LOOKUP_load_file(X509_LOOKUP *lookup, const char *path,
3675 int type);
3676
3677// X509_LOOKUP_add_dir configures |lookup| to load information from the
3678// directory at |path|. It returns one on success and zero on error. |type|
3679// should be one of the |X509_FILETYPE_*| constants to determine if the contents
3680// are PEM or DER. If |type| is |X509_FILETYPE_DEFAULT|, |path| is ignored and
3681// instead some default system path is used.
3682OPENSSL_EXPORT int X509_LOOKUP_add_dir(X509_LOOKUP *lookup, const char *path,
3683 int type);
David Benjaminc31a8a62021-10-03 22:47:13 -04003684
3685#define X509_V_OK 0
3686#define X509_V_ERR_UNSPECIFIED 1
3687
3688#define X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT 2
3689#define X509_V_ERR_UNABLE_TO_GET_CRL 3
3690#define X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE 4
3691#define X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE 5
3692#define X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY 6
3693#define X509_V_ERR_CERT_SIGNATURE_FAILURE 7
3694#define X509_V_ERR_CRL_SIGNATURE_FAILURE 8
3695#define X509_V_ERR_CERT_NOT_YET_VALID 9
3696#define X509_V_ERR_CERT_HAS_EXPIRED 10
3697#define X509_V_ERR_CRL_NOT_YET_VALID 11
3698#define X509_V_ERR_CRL_HAS_EXPIRED 12
3699#define X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD 13
3700#define X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD 14
3701#define X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD 15
3702#define X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD 16
3703#define X509_V_ERR_OUT_OF_MEM 17
3704#define X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT 18
3705#define X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN 19
3706#define X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY 20
3707#define X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE 21
3708#define X509_V_ERR_CERT_CHAIN_TOO_LONG 22
3709#define X509_V_ERR_CERT_REVOKED 23
3710#define X509_V_ERR_INVALID_CA 24
3711#define X509_V_ERR_PATH_LENGTH_EXCEEDED 25
3712#define X509_V_ERR_INVALID_PURPOSE 26
3713#define X509_V_ERR_CERT_UNTRUSTED 27
3714#define X509_V_ERR_CERT_REJECTED 28
3715// These are 'informational' when looking for issuer cert
3716#define X509_V_ERR_SUBJECT_ISSUER_MISMATCH 29
3717#define X509_V_ERR_AKID_SKID_MISMATCH 30
3718#define X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH 31
3719#define X509_V_ERR_KEYUSAGE_NO_CERTSIGN 32
3720
3721#define X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER 33
3722#define X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION 34
3723#define X509_V_ERR_KEYUSAGE_NO_CRL_SIGN 35
3724#define X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION 36
3725#define X509_V_ERR_INVALID_NON_CA 37
3726#define X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED 38
3727#define X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE 39
3728#define X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED 40
3729
3730#define X509_V_ERR_INVALID_EXTENSION 41
3731#define X509_V_ERR_INVALID_POLICY_EXTENSION 42
3732#define X509_V_ERR_NO_EXPLICIT_POLICY 43
3733#define X509_V_ERR_DIFFERENT_CRL_SCOPE 44
3734#define X509_V_ERR_UNSUPPORTED_EXTENSION_FEATURE 45
3735
3736#define X509_V_ERR_UNNESTED_RESOURCE 46
3737
3738#define X509_V_ERR_PERMITTED_VIOLATION 47
3739#define X509_V_ERR_EXCLUDED_VIOLATION 48
3740#define X509_V_ERR_SUBTREE_MINMAX 49
3741#define X509_V_ERR_APPLICATION_VERIFICATION 50
3742#define X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE 51
3743#define X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX 52
3744#define X509_V_ERR_UNSUPPORTED_NAME_SYNTAX 53
3745#define X509_V_ERR_CRL_PATH_VALIDATION_ERROR 54
3746
David Benjaminc31a8a62021-10-03 22:47:13 -04003747// Host, email and IP check errors
3748#define X509_V_ERR_HOSTNAME_MISMATCH 62
3749#define X509_V_ERR_EMAIL_MISMATCH 63
3750#define X509_V_ERR_IP_ADDRESS_MISMATCH 64
3751
3752// Caller error
3753#define X509_V_ERR_INVALID_CALL 65
3754// Issuer lookup error
3755#define X509_V_ERR_STORE_LOOKUP 66
3756
3757#define X509_V_ERR_NAME_CONSTRAINTS_WITHOUT_SANS 67
3758
3759// Certificate verify flags
3760
3761// Send issuer+subject checks to verify_cb
3762#define X509_V_FLAG_CB_ISSUER_CHECK 0x1
3763// Use check time instead of current time
3764#define X509_V_FLAG_USE_CHECK_TIME 0x2
3765// Lookup CRLs
3766#define X509_V_FLAG_CRL_CHECK 0x4
3767// Lookup CRLs for whole chain
3768#define X509_V_FLAG_CRL_CHECK_ALL 0x8
3769// Ignore unhandled critical extensions
3770#define X509_V_FLAG_IGNORE_CRITICAL 0x10
3771// Does nothing as its functionality has been enabled by default.
3772#define X509_V_FLAG_X509_STRICT 0x00
Bob Beck503ba982023-02-14 11:08:56 -07003773// This flag does nothing as proxy certificate support has been removed.
David Benjaminc31a8a62021-10-03 22:47:13 -04003774#define X509_V_FLAG_ALLOW_PROXY_CERTS 0x40
David Benjamin28226f52023-03-29 14:04:44 +09003775// Does nothing as its functionality has been enabled by default.
David Benjaminc31a8a62021-10-03 22:47:13 -04003776#define X509_V_FLAG_POLICY_CHECK 0x80
3777// Policy variable require-explicit-policy
3778#define X509_V_FLAG_EXPLICIT_POLICY 0x100
3779// Policy variable inhibit-any-policy
3780#define X509_V_FLAG_INHIBIT_ANY 0x200
3781// Policy variable inhibit-policy-mapping
3782#define X509_V_FLAG_INHIBIT_MAP 0x400
David Benjaminb2e57a12024-01-03 13:45:15 -05003783// Does nothing
David Benjaminc31a8a62021-10-03 22:47:13 -04003784#define X509_V_FLAG_NOTIFY_POLICY 0x800
David Benjaminf8614992023-11-12 13:43:33 -05003785// Causes all verifications to fail. Extended CRL features have been removed.
David Benjaminc31a8a62021-10-03 22:47:13 -04003786#define X509_V_FLAG_EXTENDED_CRL_SUPPORT 0x1000
David Benjaminf8614992023-11-12 13:43:33 -05003787// Causes all verifications to fail. Delta CRL support has been removed.
David Benjaminc31a8a62021-10-03 22:47:13 -04003788#define X509_V_FLAG_USE_DELTAS 0x2000
3789// Check selfsigned CA signature
3790#define X509_V_FLAG_CHECK_SS_SIGNATURE 0x4000
3791// Use trusted store first
3792#define X509_V_FLAG_TRUSTED_FIRST 0x8000
David Benjaminc31a8a62021-10-03 22:47:13 -04003793
3794// Allow partial chains if at least one certificate is in trusted store
3795#define X509_V_FLAG_PARTIAL_CHAIN 0x80000
3796
3797// If the initial chain is not trusted, do not attempt to build an alternative
3798// chain. Alternate chain checking was introduced in 1.0.2b. Setting this flag
3799// will force the behaviour to match that of previous versions.
3800#define X509_V_FLAG_NO_ALT_CHAINS 0x100000
3801
David Benjamineccd1032022-08-12 15:12:29 -04003802// X509_V_FLAG_NO_CHECK_TIME disables all time checks in certificate
3803// verification.
3804#define X509_V_FLAG_NO_CHECK_TIME 0x200000
3805
David Benjaminc2b7df52023-11-18 19:31:24 -05003806// X509_OBJECT_new returns a newly-allocated, empty |X509_OBJECT| or NULL on
3807// error.
3808OPENSSL_EXPORT X509_OBJECT *X509_OBJECT_new(void);
3809
3810// X509_OBJECT_free releases memory associated with |obj|.
3811OPENSSL_EXPORT void X509_OBJECT_free(X509_OBJECT *obj);
3812
3813// X509_OBJECT_get_type returns the type of |obj|, which will be one of the
3814// |X509_LU_*| constants.
3815OPENSSL_EXPORT int X509_OBJECT_get_type(const X509_OBJECT *obj);
3816
3817// X509_OBJECT_get0_X509 returns |obj| as a certificate, or NULL if |obj| is not
3818// a certificate.
3819OPENSSL_EXPORT X509 *X509_OBJECT_get0_X509(const X509_OBJECT *obj);
3820
David Benjamin2ba76342023-11-19 10:20:04 -05003821// X509_STORE_new returns a newly-allocated |X509_STORE|, or NULL on error.
David Benjaminc31a8a62021-10-03 22:47:13 -04003822OPENSSL_EXPORT X509_STORE *X509_STORE_new(void);
David Benjamin2ba76342023-11-19 10:20:04 -05003823
3824// X509_STORE_up_ref adds one to the reference count of |store| and returns one.
David Benjaminc31a8a62021-10-03 22:47:13 -04003825OPENSSL_EXPORT int X509_STORE_up_ref(X509_STORE *store);
David Benjamin2ba76342023-11-19 10:20:04 -05003826
3827// X509_STORE_free releases memory associated with |store|.
3828OPENSSL_EXPORT void X509_STORE_free(X509_STORE *store);
David Benjaminc31a8a62021-10-03 22:47:13 -04003829
3830OPENSSL_EXPORT STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *st);
David Benjamin33a5e942023-11-22 02:55:41 -05003831OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *st,
3832 X509_NAME *nm);
3833OPENSSL_EXPORT STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls(X509_STORE_CTX *st,
3834 X509_NAME *nm);
David Benjamin2ba76342023-11-19 10:20:04 -05003835
David Benjamin51ae9582023-11-25 12:06:49 -05003836// X509_STORE_set_flags enables all values in |flags| in |store|'s verification
David Benjamin2a88b4b2023-12-25 16:16:32 -05003837// flags. |flags| should be a combination of |X509_V_FLAG_*| constants.
David Benjamin2ba76342023-11-19 10:20:04 -05003838//
David Benjamin51ae9582023-11-25 12:06:49 -05003839// WARNING: These flags will be combined with default flags when copied to an
3840// |X509_STORE_CTX|. This means it is impossible to unset those defaults from
3841// the |X509_STORE|. See discussion in |X509_STORE_get0_param|.
3842OPENSSL_EXPORT int X509_STORE_set_flags(X509_STORE *store, unsigned long flags);
3843
3844OPENSSL_EXPORT int X509_STORE_set_purpose(X509_STORE *store, int purpose);
3845OPENSSL_EXPORT int X509_STORE_set_trust(X509_STORE *store, int trust);
3846
3847// |X509_STORE_set1_param| copies verification parameters from |param| as in
3848// |X509_VERIFY_PARAM_set1|. It returns one on success and zero on error.
3849OPENSSL_EXPORT int X509_STORE_set1_param(X509_STORE *store,
3850 const X509_VERIFY_PARAM *param);
3851
3852// X509_STORE_get0_param returns |store|'s verification parameters. This object
3853// is mutable and may be modified by the caller. For an individual certificate
3854// verification operation, |X509_STORE_CTX_init| initializes the
3855// |X509_STORE_CTX|'s parameters with these parameters.
3856//
3857// WARNING: |X509_STORE_CTX_init| applies some default parameters (as in
3858// |X509_VERIFY_PARAM_inherit|) after copying |store|'s parameters. This means
3859// it is impossible to leave some parameters unset at |store|. They must be
3860// explicitly unset after creating the |X509_STORE_CTX|.
3861//
3862// As of writing these late defaults are a depth limit (see
3863// |X509_VERIFY_PARAM_set_depth|) and the |X509_V_FLAG_TRUSTED_FIRST| flag. This
David Benjaminb251d812023-12-11 01:18:00 -05003864// warning does not apply if the parameters were set in |store|.
David Benjamin51ae9582023-11-25 12:06:49 -05003865//
3866// TODO(crbug.com/boringssl/441): This behavior is very surprising. Can we
David Benjaminb251d812023-12-11 01:18:00 -05003867// remove this notion of late defaults? The unsettable value at |X509_STORE| is
3868// -1, which rejects everything but explicitly-trusted self-signed certificates.
3869// |X509_V_FLAG_TRUSTED_FIRST| is mostly a workaround for poor path-building.
David Benjamin2ba76342023-11-19 10:20:04 -05003870OPENSSL_EXPORT X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *store);
David Benjaminc31a8a62021-10-03 22:47:13 -04003871
David Benjamine6ce79a2023-11-25 01:14:12 -05003872// X509_STORE_CTX_new returns a newly-allocated, empty |X509_STORE_CTX|, or NULL
3873// on error.
David Benjaminc31a8a62021-10-03 22:47:13 -04003874OPENSSL_EXPORT X509_STORE_CTX *X509_STORE_CTX_new(void);
3875
David Benjamin2ba76342023-11-19 10:20:04 -05003876// X509_STORE_CTX_free releases memory associated with |ctx|.
David Benjaminc31a8a62021-10-03 22:47:13 -04003877OPENSSL_EXPORT void X509_STORE_CTX_free(X509_STORE_CTX *ctx);
David Benjamin2ba76342023-11-19 10:20:04 -05003878
David Benjamin51ae9582023-11-25 12:06:49 -05003879// X509_STORE_CTX_init initializes |ctx| to verify |x509|, using trusted
3880// certificates and parameters in |store|. It returns one on success and zero on
3881// error. |chain| is a list of untrusted intermediate certificates to use in
3882// verification.
3883//
3884// |ctx| stores pointers to |store|, |x509|, and |chain|. Each of these objects
3885// must outlive |ctx| and may not be mutated for the duration of the certificate
3886// verification.
David Benjaminc31a8a62021-10-03 22:47:13 -04003887OPENSSL_EXPORT int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store,
3888 X509 *x509, STACK_OF(X509) *chain);
David Benjamin2135ac62022-08-12 15:33:43 -04003889
3890// X509_STORE_CTX_set0_trusted_stack configures |ctx| to trust the certificates
David Benjamind1831d72023-12-20 12:55:23 -05003891// in |sk|. |sk| must remain valid for the duration of |ctx|. Calling this
3892// function causes |ctx| to ignore any certificates configured in the
3893// |X509_STORE|.
David Benjamin2135ac62022-08-12 15:33:43 -04003894//
3895// WARNING: This function differs from most |set0| functions in that it does not
3896// take ownership of its input. The caller is required to ensure the lifetimes
3897// are consistent.
3898OPENSSL_EXPORT void X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX *ctx,
3899 STACK_OF(X509) *sk);
3900
David Benjamin2ba76342023-11-19 10:20:04 -05003901// X509_STORE_CTX_get0_store returns the |X509_STORE| that |ctx| uses.
David Benjamin04b2d612023-12-25 17:12:27 -05003902OPENSSL_EXPORT X509_STORE *X509_STORE_CTX_get0_store(const X509_STORE_CTX *ctx);
David Benjamin2ba76342023-11-19 10:20:04 -05003903
3904// X509_STORE_CTX_get0_cert returns the leaf certificate that |ctx| is
3905// verifying.
David Benjamin04b2d612023-12-25 17:12:27 -05003906OPENSSL_EXPORT X509 *X509_STORE_CTX_get0_cert(const X509_STORE_CTX *ctx);
David Benjaminc31a8a62021-10-03 22:47:13 -04003907
3908OPENSSL_EXPORT X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *v,
David Benjamin5a827022023-11-22 01:36:12 -05003909 const X509_LOOKUP_METHOD *m);
David Benjaminc31a8a62021-10-03 22:47:13 -04003910
David Benjamin5a827022023-11-22 01:36:12 -05003911OPENSSL_EXPORT const X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void);
3912OPENSSL_EXPORT const X509_LOOKUP_METHOD *X509_LOOKUP_file(void);
David Benjaminc31a8a62021-10-03 22:47:13 -04003913
3914OPENSSL_EXPORT int X509_STORE_add_cert(X509_STORE *ctx, X509 *x);
3915OPENSSL_EXPORT int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x);
3916
David Benjamin33a5e942023-11-22 02:55:41 -05003917OPENSSL_EXPORT int X509_STORE_CTX_get_by_subject(X509_STORE_CTX *vs, int type,
3918 X509_NAME *name,
3919 X509_OBJECT *ret);
David Benjaminc31a8a62021-10-03 22:47:13 -04003920
3921OPENSSL_EXPORT int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc,
3922 long argl, char **ret);
3923
David Benjaminc31a8a62021-10-03 22:47:13 -04003924OPENSSL_EXPORT int X509_load_cert_file(X509_LOOKUP *ctx, const char *file,
3925 int type);
3926OPENSSL_EXPORT int X509_load_crl_file(X509_LOOKUP *ctx, const char *file,
3927 int type);
3928OPENSSL_EXPORT int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file,
3929 int type);
David Benjaminc31a8a62021-10-03 22:47:13 -04003930
David Benjaminc31a8a62021-10-03 22:47:13 -04003931OPENSSL_EXPORT int X509_STORE_load_locations(X509_STORE *ctx, const char *file,
3932 const char *dir);
3933OPENSSL_EXPORT int X509_STORE_set_default_paths(X509_STORE *ctx);
David Benjamin8ab77ee2023-12-21 22:34:59 -05003934
3935// X509_STORE_CTX_get_error, after |X509_verify_cert| returns, returns
3936// |X509_V_OK| if verification succeeded or an |X509_V_ERR_*| describing why
3937// verification failed. This will be consistent with |X509_verify_cert|'s return
3938// value, unless the caller used the deprecated verification callback (see
3939// |X509_STORE_CTX_set_verify_cb|) in a way that breaks |ctx|'s invariants.
3940//
3941// If called during the deprecated verification callback when |ok| is zero, it
3942// returns the current error under consideration.
David Benjamin04b2d612023-12-25 17:12:27 -05003943OPENSSL_EXPORT int X509_STORE_CTX_get_error(const X509_STORE_CTX *ctx);
David Benjamin8ab77ee2023-12-21 22:34:59 -05003944
3945// X509_STORE_CTX_set_error sets |ctx|'s error to |err|, which should be
3946// |X509_V_OK| or an |X509_V_ERR_*| constant. It is not expected to be called in
3947// typical |X509_STORE_CTX| usage, but may be used in callback APIs where
3948// applications synthesize |X509_STORE_CTX| error conditions. See also
3949// |X509_STORE_CTX_set_verify_cb| and |SSL_CTX_set_cert_verify_callback|.
3950OPENSSL_EXPORT void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err);
3951
3952// X509_STORE_CTX_get_error_depth returns the depth at which the error returned
3953// by |X509_STORE_CTX_get_error| occured. This is zero-indexed integer into the
3954// certificate chain. Zero indicates the target certificate, one its issuer, and
3955// so on.
David Benjamin04b2d612023-12-25 17:12:27 -05003956OPENSSL_EXPORT int X509_STORE_CTX_get_error_depth(const X509_STORE_CTX *ctx);
David Benjamin8ab77ee2023-12-21 22:34:59 -05003957
David Benjamin04b2d612023-12-25 17:12:27 -05003958OPENSSL_EXPORT X509 *X509_STORE_CTX_get_current_cert(const X509_STORE_CTX *ctx);
3959OPENSSL_EXPORT X509_CRL *X509_STORE_CTX_get0_current_crl(
3960 const X509_STORE_CTX *ctx);
David Benjamin8ab77ee2023-12-21 22:34:59 -05003961
David Benjamin8ab77ee2023-12-21 22:34:59 -05003962// X509_STORE_CTX_get0_chain, after a successful |X509_verify_cert| call,
3963// returns the verified certificate chain. The chain begins with the leaf and
3964// ends with trust anchor.
3965//
3966// At other points, such as after a failed verification or during the deprecated
3967// verification callback, it returns the partial chain built so far. Callers
3968// should avoid relying on this as this exposes unstable library implementation
3969// details.
David Benjamin04b2d612023-12-25 17:12:27 -05003970OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get0_chain(
3971 const X509_STORE_CTX *ctx);
David Benjamin8ab77ee2023-12-21 22:34:59 -05003972
3973// X509_STORE_CTX_get1_chain behaves like |X509_STORE_CTX_get0_chain| but
3974// returns a newly-allocated |STACK_OF(X509)| containing the completed chain,
3975// with each certificate's reference count incremented. Callers must free the
3976// result with |sk_X509_pop_free| and |X509_free| when done.
David Benjamin04b2d612023-12-25 17:12:27 -05003977OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get1_chain(
3978 const X509_STORE_CTX *ctx);
David Benjamin8ab77ee2023-12-21 22:34:59 -05003979
David Benjaminc31a8a62021-10-03 22:47:13 -04003980OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get0_untrusted(
David Benjamin04b2d612023-12-25 17:12:27 -05003981 const X509_STORE_CTX *ctx);
David Benjaminc31a8a62021-10-03 22:47:13 -04003982OPENSSL_EXPORT void X509_STORE_CTX_set0_crls(X509_STORE_CTX *c,
3983 STACK_OF(X509_CRL) *sk);
3984OPENSSL_EXPORT int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose);
3985OPENSSL_EXPORT int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust);
3986OPENSSL_EXPORT int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx,
3987 int def_purpose, int purpose,
3988 int trust);
David Benjamin51ae9582023-11-25 12:06:49 -05003989
3990// X509_STORE_CTX_set_flags enables all values in |flags| in |ctx|'s
David Benjamin2a88b4b2023-12-25 16:16:32 -05003991// verification flags. |flags| should be a combination of |X509_V_FLAG_*|
3992// constants.
David Benjaminc31a8a62021-10-03 22:47:13 -04003993OPENSSL_EXPORT void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx,
3994 unsigned long flags);
David Benjamin2ba76342023-11-19 10:20:04 -05003995
3996// X509_STORE_CTX_set_time configures certificate verification to use |t|
3997// instead of the current time. |flags| is ignored and should be zero.
David Benjaminc31a8a62021-10-03 22:47:13 -04003998OPENSSL_EXPORT void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx,
3999 unsigned long flags, time_t t);
David Benjamin2ba76342023-11-19 10:20:04 -05004000
4001// X509_STORE_CTX_set_time_posix configures certificate verification to use |t|
4002// instead of the current time. |t| is interpreted as a POSIX timestamp in
4003// seconds. |flags| is ignored and should be zero.
Bob Beck6e20b772023-02-08 11:32:19 -07004004OPENSSL_EXPORT void X509_STORE_CTX_set_time_posix(X509_STORE_CTX *ctx,
4005 unsigned long flags,
4006 int64_t t);
Bob Beck706846d2023-04-19 17:43:48 -06004007
David Benjamin2ba76342023-11-19 10:20:04 -05004008// X509_STORE_CTX_get0_param returns |ctx|'s verification parameters. This
4009// object is mutable and may be modified by the caller.
David Benjaminc31a8a62021-10-03 22:47:13 -04004010OPENSSL_EXPORT X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(
4011 X509_STORE_CTX *ctx);
David Benjamin2ba76342023-11-19 10:20:04 -05004012
4013// X509_STORE_CTX_set0_param returns |ctx|'s verification parameters to |param|
4014// and takes ownership of |param|. After this function returns, the caller
4015// should not free |param|.
4016//
David Benjamin51ae9582023-11-25 12:06:49 -05004017// WARNING: This function discards any values which were previously applied in
4018// |ctx|, including the "default" parameters applied late in
4019// |X509_STORE_CTX_init|. These late defaults are not applied to parameters
4020// created standalone by |X509_VERIFY_PARAM_new|.
4021//
4022// TODO(crbug.com/boringssl/441): This behavior is very surprising. Should we
4023// re-apply the late defaults in |param|, or somehow avoid this notion of late
4024// defaults altogether?
David Benjaminc31a8a62021-10-03 22:47:13 -04004025OPENSSL_EXPORT void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx,
4026 X509_VERIFY_PARAM *param);
David Benjamin2ba76342023-11-19 10:20:04 -05004027
David Benjamin51ae9582023-11-25 12:06:49 -05004028// X509_STORE_CTX_set_default looks up the set of parameters named |name| and
4029// applies those default verification parameters for |ctx|. As in
4030// |X509_VERIFY_PARAM_inherit|, only unset parameters are changed. This function
4031// returns one on success and zero on error.
David Benjaminc31a8a62021-10-03 22:47:13 -04004032OPENSSL_EXPORT int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx,
4033 const char *name);
4034
4035// X509_VERIFY_PARAM functions
4036
David Benjamin2ba76342023-11-19 10:20:04 -05004037// X509_VERIFY_PARAM_new returns a newly-allocated |X509_VERIFY_PARAM|, or NULL
4038// on error.
David Benjaminc31a8a62021-10-03 22:47:13 -04004039OPENSSL_EXPORT X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void);
David Benjamin2ba76342023-11-19 10:20:04 -05004040
4041// X509_VERIFY_PARAM_free releases memory associated with |param|.
David Benjaminc31a8a62021-10-03 22:47:13 -04004042OPENSSL_EXPORT void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param);
David Benjamin2ba76342023-11-19 10:20:04 -05004043
David Benjamin51ae9582023-11-25 12:06:49 -05004044// X509_VERIFY_PARAM_inherit applies |from| as the default values for |to|. That
4045// is, for each parameter that is unset in |to|, it copies the value in |from|.
4046// This function returns one on success and zero on error.
David Benjaminc31a8a62021-10-03 22:47:13 -04004047OPENSSL_EXPORT int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *to,
4048 const X509_VERIFY_PARAM *from);
David Benjamin51ae9582023-11-25 12:06:49 -05004049
4050// X509_VERIFY_PARAM_set1 copies parameters from |from| to |to|. If a parameter
4051// is unset in |from|, the existing value in |to| is preserved. This function
4052// returns one on success and zero on error.
David Benjaminc31a8a62021-10-03 22:47:13 -04004053OPENSSL_EXPORT int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to,
4054 const X509_VERIFY_PARAM *from);
David Benjamin51ae9582023-11-25 12:06:49 -05004055
4056// X509_VERIFY_PARAM_set_flags enables all values in |flags| in |param|'s
4057// verification flags and returns one. |flags| should be a combination of
4058// |X509_V_FLAG_*| constants.
David Benjaminc31a8a62021-10-03 22:47:13 -04004059OPENSSL_EXPORT int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param,
4060 unsigned long flags);
David Benjamin51ae9582023-11-25 12:06:49 -05004061
4062// X509_VERIFY_PARAM_clear_flags disables all values in |flags| in |param|'s
4063// verification flags and returns one. |flags| should be a combination of
4064// |X509_V_FLAG_*| constants.
David Benjaminc31a8a62021-10-03 22:47:13 -04004065OPENSSL_EXPORT int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param,
4066 unsigned long flags);
David Benjamin51ae9582023-11-25 12:06:49 -05004067
4068// X509_VERIFY_PARAM_get_flags returns |param|'s verification flags.
David Benjaminc31a8a62021-10-03 22:47:13 -04004069OPENSSL_EXPORT unsigned long X509_VERIFY_PARAM_get_flags(
David Benjamin51ae9582023-11-25 12:06:49 -05004070 const X509_VERIFY_PARAM *param);
4071
David Benjaminc31a8a62021-10-03 22:47:13 -04004072OPENSSL_EXPORT int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param,
4073 int purpose);
4074OPENSSL_EXPORT int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param,
4075 int trust);
David Benjaminb251d812023-12-11 01:18:00 -05004076
4077// X509_VERIFY_PARAM_set_depth configures |param| to limit certificate chains to
4078// |depth| intermediate certificates. This count excludes both the target
4079// certificate and the trust anchor (root certificate).
David Benjaminc31a8a62021-10-03 22:47:13 -04004080OPENSSL_EXPORT void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param,
4081 int depth);
David Benjamin2ba76342023-11-19 10:20:04 -05004082
4083// X509_VERIFY_PARAM_set_time configures certificate verification to use |t|
4084// instead of the current time.
David Benjaminc31a8a62021-10-03 22:47:13 -04004085OPENSSL_EXPORT void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param,
4086 time_t t);
David Benjamin2ba76342023-11-19 10:20:04 -05004087
4088// X509_VERIFY_PARAM_set_time_posix configures certificate verification to use
4089// |t| instead of the current time. |t| is interpreted as a POSIX timestamp in
4090// seconds.
Bob Beck6e20b772023-02-08 11:32:19 -07004091OPENSSL_EXPORT void X509_VERIFY_PARAM_set_time_posix(X509_VERIFY_PARAM *param,
4092 int64_t t);
David Benjamin2ba76342023-11-19 10:20:04 -05004093
4094// X509_VERIFY_PARAM_add0_policy adds |policy| to the user-initial-policy-set
4095// (see Section 6.1.1 of RFC 5280). On success, it takes ownership of
4096// |policy| and returns one. Otherwise, it returns zero and the caller retains
4097// owneship of |policy|.
David Benjaminc31a8a62021-10-03 22:47:13 -04004098OPENSSL_EXPORT int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param,
4099 ASN1_OBJECT *policy);
David Benjamin2ba76342023-11-19 10:20:04 -05004100
4101// X509_VERIFY_PARAM_set1_policies sets the user-initial-policy-set (see
4102// Section 6.1.1 of RFC 5280) to a copy of |policies|. It returns one on success
4103// and zero on error.
David Benjaminc31a8a62021-10-03 22:47:13 -04004104OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_policies(
David Benjamin42ba95f2022-12-13 14:17:45 -05004105 X509_VERIFY_PARAM *param, const STACK_OF(ASN1_OBJECT) *policies);
David Benjaminc31a8a62021-10-03 22:47:13 -04004106
David Benjamin2ba76342023-11-19 10:20:04 -05004107// X509_VERIFY_PARAM_set1_host configures |param| to check for the DNS name
4108// specified by |name|. It returns one on success and zero on error.
David Benjamin9e404812023-12-06 22:33:21 -05004109//
4110// By default, both subject alternative names and the subject's common name
4111// attribute are checked. The latter has long been deprecated, so callers should
4112// call |X509_VERIFY_PARAM_set_hostflags| with
4113// |X509_CHECK_FLAG_NEVER_CHECK_SUBJECT| to use the standard behavior.
4114// https://crbug.com/boringssl/464 tracks fixing the default.
David Benjaminc31a8a62021-10-03 22:47:13 -04004115OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param,
4116 const char *name,
David Benjamin2ba76342023-11-19 10:20:04 -05004117 size_t name_len);
4118
4119// X509_VERIFY_PARAM_add1_host adds |name| to the list of names checked by
4120// |param|. If any configured DNS name matches the certificate, verification
4121// succeeds. It returns one on success and zero on error.
David Benjamin9e404812023-12-06 22:33:21 -05004122//
4123// By default, both subject alternative names and the subject's common name
4124// attribute are checked. The latter has long been deprecated, so callers should
4125// call |X509_VERIFY_PARAM_set_hostflags| with
4126// |X509_CHECK_FLAG_NEVER_CHECK_SUBJECT| to use the standard behavior.
4127// https://crbug.com/boringssl/464 tracks fixing the default.
David Benjaminc31a8a62021-10-03 22:47:13 -04004128OPENSSL_EXPORT int X509_VERIFY_PARAM_add1_host(X509_VERIFY_PARAM *param,
4129 const char *name,
David Benjamin2ba76342023-11-19 10:20:04 -05004130 size_t name_len);
4131
4132// X509_VERIFY_PARAM_set_hostflags sets the name-checking flags on |param| to
4133// |flags|. |flags| should be a combination of |X509_CHECK_FLAG_*| constants.
David Benjaminc31a8a62021-10-03 22:47:13 -04004134OPENSSL_EXPORT void X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param,
4135 unsigned int flags);
David Benjamin2ba76342023-11-19 10:20:04 -05004136
David Benjamin2ba76342023-11-19 10:20:04 -05004137// X509_VERIFY_PARAM_set1_email configures |param| to check for the email
4138// address specified by |email|. It returns one on success and zero on error.
David Benjamin9e404812023-12-06 22:33:21 -05004139//
4140// By default, both subject alternative names and the subject's email address
4141// attribute are checked. The |X509_CHECK_FLAG_NEVER_CHECK_SUBJECT| flag may be
4142// used to change this behavior.
David Benjaminc31a8a62021-10-03 22:47:13 -04004143OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *param,
4144 const char *email,
David Benjamin2ba76342023-11-19 10:20:04 -05004145 size_t email_len);
4146
4147// X509_VERIFY_PARAM_set1_ip configures |param| to check for the IP address
4148// specified by |ip|. It returns one on success and zero on error. The IP
4149// address is specified in its binary representation. |ip_len| must be 4 for an
4150// IPv4 address and 16 for an IPv6 address.
David Benjaminc31a8a62021-10-03 22:47:13 -04004151OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *param,
David Benjamin2ba76342023-11-19 10:20:04 -05004152 const uint8_t *ip, size_t ip_len);
4153
4154// X509_VERIFY_PARAM_set1_ip_asc decodes |ipasc| as the ASCII representation of
4155// an IPv4 or IPv6 address, and configures |param| to check for it. It returns
4156// one on success and zero on error.
David Benjaminc31a8a62021-10-03 22:47:13 -04004157OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param,
4158 const char *ipasc);
4159
David Benjaminb251d812023-12-11 01:18:00 -05004160// X509_VERIFY_PARAM_get_depth returns the maximum depth configured in |param|.
4161// See |X509_VERIFY_PARAM_set_depth|.
David Benjaminc31a8a62021-10-03 22:47:13 -04004162OPENSSL_EXPORT int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param);
David Benjaminc31a8a62021-10-03 22:47:13 -04004163
David Benjamin58906ea2023-11-20 23:59:49 -05004164typedef void *(*X509V3_EXT_NEW)(void);
4165typedef void (*X509V3_EXT_FREE)(void *);
4166typedef void *(*X509V3_EXT_D2I)(void *, const unsigned char **, long);
4167typedef int (*X509V3_EXT_I2D)(void *, unsigned char **);
4168typedef STACK_OF(CONF_VALUE) *(*X509V3_EXT_I2V)(const X509V3_EXT_METHOD *method,
4169 void *ext,
4170 STACK_OF(CONF_VALUE) *extlist);
4171typedef void *(*X509V3_EXT_V2I)(const X509V3_EXT_METHOD *method,
4172 const X509V3_CTX *ctx,
4173 const STACK_OF(CONF_VALUE) *values);
4174typedef char *(*X509V3_EXT_I2S)(const X509V3_EXT_METHOD *method, void *ext);
4175typedef void *(*X509V3_EXT_S2I)(const X509V3_EXT_METHOD *method,
4176 const X509V3_CTX *ctx, const char *str);
4177typedef int (*X509V3_EXT_I2R)(const X509V3_EXT_METHOD *method, void *ext,
4178 BIO *out, int indent);
4179typedef void *(*X509V3_EXT_R2I)(const X509V3_EXT_METHOD *method,
4180 const X509V3_CTX *ctx, const char *str);
4181
4182// V3 extension structure
4183
4184struct v3_ext_method {
4185 int ext_nid;
4186 int ext_flags;
4187
4188 // it determines how values of this extension are allocated, released, parsed,
4189 // and marshalled. This must be non-NULL.
4190 ASN1_ITEM_EXP *it;
4191
4192 // The following functions are ignored in favor of |it|. They are retained in
4193 // the struct only for source compatibility with existing struct definitions.
4194 X509V3_EXT_NEW ext_new;
4195 X509V3_EXT_FREE ext_free;
4196 X509V3_EXT_D2I d2i;
4197 X509V3_EXT_I2D i2d;
4198
4199 // The following pair is used for string extensions
4200 X509V3_EXT_I2S i2s;
4201 X509V3_EXT_S2I s2i;
4202
4203 // The following pair is used for multi-valued extensions
4204 X509V3_EXT_I2V i2v;
4205 X509V3_EXT_V2I v2i;
4206
4207 // The following are used for raw extensions
4208 X509V3_EXT_I2R i2r;
4209 X509V3_EXT_R2I r2i;
4210
4211 void *usr_data; // Any extension specific data
4212};
4213
4214DEFINE_STACK_OF(X509V3_EXT_METHOD)
4215
4216// ext_flags values
4217#define X509V3_EXT_CTX_DEP 0x2
4218#define X509V3_EXT_MULTILINE 0x4
4219
4220struct BASIC_CONSTRAINTS_st {
4221 int ca;
4222 ASN1_INTEGER *pathlen;
4223};
4224
David Benjamin58906ea2023-11-20 23:59:49 -05004225typedef struct ACCESS_DESCRIPTION_st {
4226 ASN1_OBJECT *method;
4227 GENERAL_NAME *location;
4228} ACCESS_DESCRIPTION;
4229
4230DEFINE_STACK_OF(ACCESS_DESCRIPTION)
4231
4232typedef STACK_OF(ACCESS_DESCRIPTION) AUTHORITY_INFO_ACCESS;
4233
4234typedef STACK_OF(ASN1_OBJECT) EXTENDED_KEY_USAGE;
4235
4236typedef struct DIST_POINT_NAME_st {
4237 int type;
4238 union {
4239 GENERAL_NAMES *fullname;
4240 STACK_OF(X509_NAME_ENTRY) *relativename;
4241 } name;
4242 // If relativename then this contains the full distribution point name
4243 X509_NAME *dpname;
4244} DIST_POINT_NAME;
4245// All existing reasons
4246#define CRLDP_ALL_REASONS 0x807f
4247
4248#define CRL_REASON_NONE (-1)
4249#define CRL_REASON_UNSPECIFIED 0
4250#define CRL_REASON_KEY_COMPROMISE 1
4251#define CRL_REASON_CA_COMPROMISE 2
4252#define CRL_REASON_AFFILIATION_CHANGED 3
4253#define CRL_REASON_SUPERSEDED 4
4254#define CRL_REASON_CESSATION_OF_OPERATION 5
4255#define CRL_REASON_CERTIFICATE_HOLD 6
4256#define CRL_REASON_REMOVE_FROM_CRL 8
4257#define CRL_REASON_PRIVILEGE_WITHDRAWN 9
4258#define CRL_REASON_AA_COMPROMISE 10
4259
4260struct DIST_POINT_st {
4261 DIST_POINT_NAME *distpoint;
4262 ASN1_BIT_STRING *reasons;
4263 GENERAL_NAMES *CRLissuer;
4264};
4265
4266typedef STACK_OF(DIST_POINT) CRL_DIST_POINTS;
4267
4268DEFINE_STACK_OF(DIST_POINT)
4269
4270struct AUTHORITY_KEYID_st {
4271 ASN1_OCTET_STRING *keyid;
4272 GENERAL_NAMES *issuer;
4273 ASN1_INTEGER *serial;
4274};
4275
4276typedef struct NOTICEREF_st {
4277 ASN1_STRING *organization;
4278 STACK_OF(ASN1_INTEGER) *noticenos;
4279} NOTICEREF;
4280
4281typedef struct USERNOTICE_st {
4282 NOTICEREF *noticeref;
4283 ASN1_STRING *exptext;
4284} USERNOTICE;
4285
4286typedef struct POLICYQUALINFO_st {
4287 ASN1_OBJECT *pqualid;
4288 union {
4289 ASN1_IA5STRING *cpsuri;
4290 USERNOTICE *usernotice;
4291 ASN1_TYPE *other;
4292 } d;
4293} POLICYQUALINFO;
4294
4295DEFINE_STACK_OF(POLICYQUALINFO)
4296
4297typedef struct POLICYINFO_st {
4298 ASN1_OBJECT *policyid;
4299 STACK_OF(POLICYQUALINFO) *qualifiers;
4300} POLICYINFO;
4301
4302typedef STACK_OF(POLICYINFO) CERTIFICATEPOLICIES;
4303
4304DEFINE_STACK_OF(POLICYINFO)
4305
4306typedef struct POLICY_MAPPING_st {
4307 ASN1_OBJECT *issuerDomainPolicy;
4308 ASN1_OBJECT *subjectDomainPolicy;
4309} POLICY_MAPPING;
4310
4311DEFINE_STACK_OF(POLICY_MAPPING)
4312
4313typedef STACK_OF(POLICY_MAPPING) POLICY_MAPPINGS;
4314
4315typedef struct GENERAL_SUBTREE_st {
4316 GENERAL_NAME *base;
4317 ASN1_INTEGER *minimum;
4318 ASN1_INTEGER *maximum;
4319} GENERAL_SUBTREE;
4320
4321DEFINE_STACK_OF(GENERAL_SUBTREE)
4322
4323struct NAME_CONSTRAINTS_st {
4324 STACK_OF(GENERAL_SUBTREE) *permittedSubtrees;
4325 STACK_OF(GENERAL_SUBTREE) *excludedSubtrees;
4326};
4327
4328typedef struct POLICY_CONSTRAINTS_st {
4329 ASN1_INTEGER *requireExplicitPolicy;
4330 ASN1_INTEGER *inhibitPolicyMapping;
4331} POLICY_CONSTRAINTS;
4332
4333struct ISSUING_DIST_POINT_st {
4334 DIST_POINT_NAME *distpoint;
4335 ASN1_BOOLEAN onlyuser;
4336 ASN1_BOOLEAN onlyCA;
4337 ASN1_BIT_STRING *onlysomereasons;
4338 ASN1_BOOLEAN indirectCRL;
4339 ASN1_BOOLEAN onlyattr;
4340};
4341
4342// X509_PURPOSE stuff
4343
David Benjamin58906ea2023-11-20 23:59:49 -05004344#define NS_SSL_CLIENT 0x80
4345#define NS_SSL_SERVER 0x40
4346#define NS_SMIME 0x20
4347#define NS_OBJSIGN 0x10
4348#define NS_SSL_CA 0x04
4349#define NS_SMIME_CA 0x02
4350#define NS_OBJSIGN_CA 0x01
4351#define NS_ANY_CA (NS_SSL_CA | NS_SMIME_CA | NS_OBJSIGN_CA)
4352
David Benjamin58906ea2023-11-20 23:59:49 -05004353typedef struct x509_purpose_st {
4354 int purpose;
4355 int trust; // Default trust ID
4356 int flags;
4357 int (*check_purpose)(const struct x509_purpose_st *, const X509 *, int);
4358 char *name;
4359 char *sname;
4360 void *usr_data;
4361} X509_PURPOSE;
4362
4363#define X509_PURPOSE_SSL_CLIENT 1
4364#define X509_PURPOSE_SSL_SERVER 2
4365#define X509_PURPOSE_NS_SSL_SERVER 3
4366#define X509_PURPOSE_SMIME_SIGN 4
4367#define X509_PURPOSE_SMIME_ENCRYPT 5
4368#define X509_PURPOSE_CRL_SIGN 6
4369#define X509_PURPOSE_ANY 7
4370#define X509_PURPOSE_OCSP_HELPER 8
4371#define X509_PURPOSE_TIMESTAMP_SIGN 9
4372
David Benjamin58906ea2023-11-20 23:59:49 -05004373DEFINE_STACK_OF(X509_PURPOSE)
4374
4375DECLARE_ASN1_FUNCTIONS_const(BASIC_CONSTRAINTS)
4376
4377// TODO(https://crbug.com/boringssl/407): This is not const because it contains
4378// an |X509_NAME|.
4379DECLARE_ASN1_FUNCTIONS(AUTHORITY_KEYID)
4380
David Benjamin58906ea2023-11-20 23:59:49 -05004381DECLARE_ASN1_FUNCTIONS_const(EXTENDED_KEY_USAGE)
David Benjamin58906ea2023-11-20 23:59:49 -05004382
4383DECLARE_ASN1_FUNCTIONS_const(CERTIFICATEPOLICIES)
4384DECLARE_ASN1_FUNCTIONS_const(POLICYINFO)
4385DECLARE_ASN1_FUNCTIONS_const(POLICYQUALINFO)
4386DECLARE_ASN1_FUNCTIONS_const(USERNOTICE)
4387DECLARE_ASN1_FUNCTIONS_const(NOTICEREF)
4388
4389// TODO(https://crbug.com/boringssl/407): This is not const because it contains
4390// an |X509_NAME|.
4391DECLARE_ASN1_FUNCTIONS(CRL_DIST_POINTS)
4392// TODO(https://crbug.com/boringssl/407): This is not const because it contains
4393// an |X509_NAME|.
4394DECLARE_ASN1_FUNCTIONS(DIST_POINT)
4395// TODO(https://crbug.com/boringssl/407): This is not const because it contains
4396// an |X509_NAME|.
4397DECLARE_ASN1_FUNCTIONS(DIST_POINT_NAME)
4398// TODO(https://crbug.com/boringssl/407): This is not const because it contains
4399// an |X509_NAME|.
4400DECLARE_ASN1_FUNCTIONS(ISSUING_DIST_POINT)
4401
4402OPENSSL_EXPORT int DIST_POINT_set_dpname(DIST_POINT_NAME *dpn,
4403 X509_NAME *iname);
4404
David Benjamin58906ea2023-11-20 23:59:49 -05004405// TODO(https://crbug.com/boringssl/407): This is not const because it contains
4406// an |X509_NAME|.
4407DECLARE_ASN1_FUNCTIONS(ACCESS_DESCRIPTION)
4408// TODO(https://crbug.com/boringssl/407): This is not const because it contains
4409// an |X509_NAME|.
4410DECLARE_ASN1_FUNCTIONS(AUTHORITY_INFO_ACCESS)
4411
4412DECLARE_ASN1_ITEM(POLICY_MAPPING)
4413DECLARE_ASN1_ALLOC_FUNCTIONS(POLICY_MAPPING)
4414DECLARE_ASN1_ITEM(POLICY_MAPPINGS)
4415
4416DECLARE_ASN1_ITEM(GENERAL_SUBTREE)
4417DECLARE_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE)
4418
4419DECLARE_ASN1_ITEM(NAME_CONSTRAINTS)
4420DECLARE_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS)
4421
4422DECLARE_ASN1_ALLOC_FUNCTIONS(POLICY_CONSTRAINTS)
4423DECLARE_ASN1_ITEM(POLICY_CONSTRAINTS)
4424
David Benjamin58906ea2023-11-20 23:59:49 -05004425// X509V3_EXT_add registers |ext| as a custom extension for the extension type
4426// |ext->ext_nid|. |ext| must be valid for the remainder of the address space's
4427// lifetime. It returns one on success and zero on error.
4428//
4429// WARNING: This function modifies global state. If other code in the same
4430// address space also registers an extension with type |ext->ext_nid|, the two
4431// registrations will conflict. Which registration takes effect is undefined. If
4432// the two registrations use incompatible in-memory representations, code
4433// expecting the other registration will then cast a type to the wrong type,
4434// resulting in a potentially exploitable memory error. This conflict can also
4435// occur if BoringSSL later adds support for |ext->ext_nid|, with a different
4436// in-memory representation than the one expected by |ext|.
4437//
4438// This function, additionally, is not thread-safe and cannot be called
4439// concurrently with any other BoringSSL function.
4440//
4441// As a result, it is impossible to safely use this function. Registering a
4442// custom extension has no impact on certificate verification so, instead,
4443// callers should simply handle the custom extension with the byte-based
4444// |X509_EXTENSION| APIs directly. Registering |ext| with the library has little
4445// practical value.
4446OPENSSL_EXPORT OPENSSL_DEPRECATED int X509V3_EXT_add(X509V3_EXT_METHOD *ext);
4447
4448// X509V3_EXT_add_alias registers a custom extension with NID |nid_to|. The
4449// corresponding ASN.1 type is copied from |nid_from|. It returns one on success
4450// and zero on error.
4451//
4452// WARNING: Do not use this function. See |X509V3_EXT_add|.
4453OPENSSL_EXPORT OPENSSL_DEPRECATED int X509V3_EXT_add_alias(int nid_to,
4454 int nid_from);
4455
4456OPENSSL_EXPORT const X509V3_EXT_METHOD *X509V3_EXT_get(
4457 const X509_EXTENSION *ext);
4458OPENSSL_EXPORT const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid);
David Benjamin58906ea2023-11-20 23:59:49 -05004459
4460// X509V3_EXT_d2i decodes |ext| and returns a pointer to a newly-allocated
4461// structure, with type dependent on the type of the extension. It returns NULL
4462// if |ext| is an unsupported extension or if there was a syntax error in the
4463// extension. The caller should cast the return value to the expected type and
4464// free the structure when done.
4465//
4466// WARNING: Casting the return value to the wrong type is a potentially
4467// exploitable memory error, so callers must not use this function before
4468// checking |ext| is of a known type.
4469OPENSSL_EXPORT void *X509V3_EXT_d2i(const X509_EXTENSION *ext);
4470
4471// X509V3_get_d2i finds and decodes the extension in |extensions| of type |nid|.
4472// If found, it decodes it and returns a newly-allocated structure, with type
4473// dependent on |nid|. If the extension is not found or on error, it returns
4474// NULL. The caller may distinguish these cases using the |out_critical| value.
4475//
4476// If |out_critical| is not NULL, this function sets |*out_critical| to one if
4477// the extension is found and critical, zero if it is found and not critical, -1
4478// if it is not found, and -2 if there is an invalid duplicate extension. Note
4479// this function may set |*out_critical| to one or zero and still return NULL if
4480// the extension is found but has a syntax error.
4481//
4482// If |out_idx| is not NULL, this function looks for the first occurrence of the
4483// extension after |*out_idx|. It then sets |*out_idx| to the index of the
4484// extension, or -1 if not found. If |out_idx| is non-NULL, duplicate extensions
4485// are not treated as an error. Callers, however, should not rely on this
4486// behavior as it may be removed in the future. Duplicate extensions are
4487// forbidden in RFC 5280.
4488//
4489// WARNING: This function is difficult to use correctly. Callers should pass a
4490// non-NULL |out_critical| and check both the return value and |*out_critical|
4491// to handle errors. If the return value is NULL and |*out_critical| is not -1,
4492// there was an error. Otherwise, the function succeeded and but may return NULL
4493// for a missing extension. Callers should pass NULL to |out_idx| so that
4494// duplicate extensions are handled correctly.
4495//
4496// Additionally, casting the return value to the wrong type is a potentially
4497// exploitable memory error, so callers must ensure the cast and |nid| match.
4498OPENSSL_EXPORT void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *extensions,
4499 int nid, int *out_critical, int *out_idx);
4500
4501// X509V3_EXT_free casts |ext_data| into the type that corresponds to |nid| and
4502// releases memory associated with it. It returns one on success and zero if
4503// |nid| is not a known extension.
4504//
4505// WARNING: Casting |ext_data| to the wrong type is a potentially exploitable
4506// memory error, so callers must ensure |ext_data|'s type matches |nid|.
4507//
4508// TODO(davidben): OpenSSL upstream no longer exposes this function. Remove it?
4509OPENSSL_EXPORT int X509V3_EXT_free(int nid, void *ext_data);
4510
4511// X509V3_EXT_i2d casts |ext_struc| into the type that corresponds to
4512// |ext_nid|, serializes it, and returns a newly-allocated |X509_EXTENSION|
4513// object containing the serialization, or NULL on error. The |X509_EXTENSION|
4514// has OID |ext_nid| and is critical if |crit| is one.
4515//
4516// WARNING: Casting |ext_struc| to the wrong type is a potentially exploitable
4517// memory error, so callers must ensure |ext_struct|'s type matches |ext_nid|.
4518OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit,
4519 void *ext_struc);
4520
4521// The following constants control the behavior of |X509V3_add1_i2d| and related
4522// functions.
4523
4524// X509V3_ADD_OP_MASK can be ANDed with the flags to determine how duplicate
4525// extensions are processed.
4526#define X509V3_ADD_OP_MASK 0xfL
4527
4528// X509V3_ADD_DEFAULT causes the function to fail if the extension was already
4529// present.
4530#define X509V3_ADD_DEFAULT 0L
4531
4532// X509V3_ADD_APPEND causes the function to unconditionally appended the new
4533// extension to to the extensions list, even if there is a duplicate.
4534#define X509V3_ADD_APPEND 1L
4535
4536// X509V3_ADD_REPLACE causes the function to replace the existing extension, or
4537// append if it is not present.
4538#define X509V3_ADD_REPLACE 2L
4539
4540// X509V3_ADD_REPLACE causes the function to replace the existing extension and
4541// fail if it is not present.
4542#define X509V3_ADD_REPLACE_EXISTING 3L
4543
4544// X509V3_ADD_KEEP_EXISTING causes the function to succeed without replacing the
4545// extension if already present.
4546#define X509V3_ADD_KEEP_EXISTING 4L
4547
4548// X509V3_ADD_DELETE causes the function to remove the matching extension. No
4549// new extension is added. If there is no matching extension, the function
4550// fails. The |value| parameter is ignored in this mode.
4551#define X509V3_ADD_DELETE 5L
4552
4553// X509V3_ADD_SILENT may be ORed into one of the values above to indicate the
4554// function should not add to the error queue on duplicate or missing extension.
4555// The function will continue to return zero in those cases, and it will
4556// continue to return -1 and add to the error queue on other errors.
4557#define X509V3_ADD_SILENT 0x10
4558
4559// X509V3_add1_i2d casts |value| to the type that corresponds to |nid|,
4560// serializes it, and appends it to the extension list in |*x|. If |*x| is NULL,
4561// it will set |*x| to a newly-allocated |STACK_OF(X509_EXTENSION)| as needed.
4562// The |crit| parameter determines whether the new extension is critical.
4563// |flags| may be some combination of the |X509V3_ADD_*| constants to control
4564// the function's behavior on duplicate extension.
4565//
4566// This function returns one on success, zero if the operation failed due to a
4567// missing or duplicate extension, and -1 on other errors.
4568//
4569// WARNING: Casting |value| to the wrong type is a potentially exploitable
4570// memory error, so callers must ensure |value|'s type matches |nid|.
4571OPENSSL_EXPORT int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid,
4572 void *value, int crit, unsigned long flags);
4573
David Benjamin58906ea2023-11-20 23:59:49 -05004574OPENSSL_EXPORT int X509_check_purpose(X509 *x, int id, int ca);
David Benjamin7ff31d32023-11-25 00:38:01 -05004575
David Benjamin58906ea2023-11-20 23:59:49 -05004576OPENSSL_EXPORT int X509_PURPOSE_set(int *p, int purpose);
David Benjamin58906ea2023-11-20 23:59:49 -05004577
David Benjamin58906ea2023-11-20 23:59:49 -05004578OPENSSL_EXPORT int X509_PURPOSE_get_count(void);
David Benjamin6099ab92023-12-02 17:20:35 -05004579OPENSSL_EXPORT const X509_PURPOSE *X509_PURPOSE_get0(int idx);
David Benjamin58906ea2023-11-20 23:59:49 -05004580OPENSSL_EXPORT int X509_PURPOSE_get_by_sname(const char *sname);
4581OPENSSL_EXPORT int X509_PURPOSE_get_by_id(int id);
David Benjamin58906ea2023-11-20 23:59:49 -05004582OPENSSL_EXPORT char *X509_PURPOSE_get0_name(const X509_PURPOSE *xp);
4583OPENSSL_EXPORT char *X509_PURPOSE_get0_sname(const X509_PURPOSE *xp);
4584OPENSSL_EXPORT int X509_PURPOSE_get_trust(const X509_PURPOSE *xp);
David Benjamin58906ea2023-11-20 23:59:49 -05004585OPENSSL_EXPORT int X509_PURPOSE_get_id(const X509_PURPOSE *);
4586
David Benjamin58906ea2023-11-20 23:59:49 -05004587// Flags for X509_check_* functions
4588
4589// Deprecated: this flag does nothing
4590#define X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT 0
4591// Disable wildcard matching for dnsName fields and common name.
4592#define X509_CHECK_FLAG_NO_WILDCARDS 0x2
4593// X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS does nothing, but is necessary in
4594// OpenSSL to enable standard wildcard matching. In BoringSSL, this behavior is
4595// always enabled.
4596#define X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS 0
4597// Deprecated: this flag does nothing
4598#define X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS 0
4599// Deprecated: this flag does nothing
4600#define X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS 0
4601// Skip the subject common name fallback if subjectAltNames is missing.
4602#define X509_CHECK_FLAG_NEVER_CHECK_SUBJECT 0x20
4603
Adam Langley95c29f32014-06-20 12:00:00 -07004604
David Benjamin8a5ec722021-10-15 13:17:40 -04004605#if defined(__cplusplus)
4606} // extern C
David Benjamin4492a612017-08-02 17:16:31 -04004607#endif
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004608
David Benjamin4492a612017-08-02 17:16:31 -04004609#if !defined(BORINGSSL_NO_CXX)
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004610extern "C++" {
4611
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07004612BSSL_NAMESPACE_BEGIN
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004613
David Benjamin58906ea2023-11-20 23:59:49 -05004614BORINGSSL_MAKE_DELETER(ACCESS_DESCRIPTION, ACCESS_DESCRIPTION_free)
4615BORINGSSL_MAKE_DELETER(AUTHORITY_KEYID, AUTHORITY_KEYID_free)
4616BORINGSSL_MAKE_DELETER(BASIC_CONSTRAINTS, BASIC_CONSTRAINTS_free)
4617// TODO(davidben): Move this to conf.h and rename to CONF_VALUE_free.
4618BORINGSSL_MAKE_DELETER(CONF_VALUE, X509V3_conf_free)
4619BORINGSSL_MAKE_DELETER(DIST_POINT, DIST_POINT_free)
4620BORINGSSL_MAKE_DELETER(GENERAL_NAME, GENERAL_NAME_free)
4621BORINGSSL_MAKE_DELETER(GENERAL_SUBTREE, GENERAL_SUBTREE_free)
4622BORINGSSL_MAKE_DELETER(NAME_CONSTRAINTS, NAME_CONSTRAINTS_free)
Matthew Braithwaite7358fab2016-09-26 14:14:43 -07004623BORINGSSL_MAKE_DELETER(NETSCAPE_SPKI, NETSCAPE_SPKI_free)
David Benjamin58906ea2023-11-20 23:59:49 -05004624BORINGSSL_MAKE_DELETER(POLICY_MAPPING, POLICY_MAPPING_free)
4625BORINGSSL_MAKE_DELETER(POLICYINFO, POLICYINFO_free)
David Benjamin441efad2018-03-21 23:25:19 -04004626BORINGSSL_MAKE_DELETER(RSA_PSS_PARAMS, RSA_PSS_PARAMS_free)
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004627BORINGSSL_MAKE_DELETER(X509, X509_free)
David Benjamin2908dd12018-06-29 17:46:42 -04004628BORINGSSL_MAKE_UP_REF(X509, X509_up_ref)
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004629BORINGSSL_MAKE_DELETER(X509_ALGOR, X509_ALGOR_free)
David Benjaminab7811e2021-04-20 14:26:36 -04004630BORINGSSL_MAKE_DELETER(X509_ATTRIBUTE, X509_ATTRIBUTE_free)
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004631BORINGSSL_MAKE_DELETER(X509_CRL, X509_CRL_free)
David Benjamin2908dd12018-06-29 17:46:42 -04004632BORINGSSL_MAKE_UP_REF(X509_CRL, X509_CRL_up_ref)
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004633BORINGSSL_MAKE_DELETER(X509_EXTENSION, X509_EXTENSION_free)
David Benjamin38c4bf12023-11-29 10:48:37 -05004634BORINGSSL_MAKE_DELETER(X509_INFO, X509_INFO_free)
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004635BORINGSSL_MAKE_DELETER(X509_LOOKUP, X509_LOOKUP_free)
4636BORINGSSL_MAKE_DELETER(X509_NAME, X509_NAME_free)
4637BORINGSSL_MAKE_DELETER(X509_NAME_ENTRY, X509_NAME_ENTRY_free)
David Benjamin2762b352017-08-30 13:07:26 -04004638BORINGSSL_MAKE_DELETER(X509_PUBKEY, X509_PUBKEY_free)
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004639BORINGSSL_MAKE_DELETER(X509_REQ, X509_REQ_free)
4640BORINGSSL_MAKE_DELETER(X509_REVOKED, X509_REVOKED_free)
4641BORINGSSL_MAKE_DELETER(X509_SIG, X509_SIG_free)
4642BORINGSSL_MAKE_DELETER(X509_STORE, X509_STORE_free)
David Benjaminba423c92021-06-15 16:26:58 -04004643BORINGSSL_MAKE_UP_REF(X509_STORE, X509_STORE_up_ref)
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004644BORINGSSL_MAKE_DELETER(X509_STORE_CTX, X509_STORE_CTX_free)
4645BORINGSSL_MAKE_DELETER(X509_VERIFY_PARAM, X509_VERIFY_PARAM_free)
4646
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07004647BSSL_NAMESPACE_END
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004648
David Benjamin991d31b2020-06-19 11:38:20 -04004649} // extern C++
4650#endif // !BORINGSSL_NO_CXX
Adam Langley95c29f32014-06-20 12:00:00 -07004651
David Benjamin689be0f2015-02-11 15:55:26 -05004652#define X509_R_AKID_MISMATCH 100
4653#define X509_R_BAD_PKCS7_VERSION 101
4654#define X509_R_BAD_X509_FILETYPE 102
4655#define X509_R_BASE64_DECODE_ERROR 103
4656#define X509_R_CANT_CHECK_DH_KEY 104
4657#define X509_R_CERT_ALREADY_IN_HASH_TABLE 105
4658#define X509_R_CRL_ALREADY_DELTA 106
4659#define X509_R_CRL_VERIFY_FAILURE 107
4660#define X509_R_IDP_MISMATCH 108
4661#define X509_R_INVALID_BIT_STRING_BITS_LEFT 109
4662#define X509_R_INVALID_DIRECTORY 110
4663#define X509_R_INVALID_FIELD_NAME 111
David Benjamina2f2bc32016-03-14 17:13:54 -04004664#define X509_R_INVALID_PSS_PARAMETERS 112
4665#define X509_R_INVALID_TRUST 113
4666#define X509_R_ISSUER_MISMATCH 114
4667#define X509_R_KEY_TYPE_MISMATCH 115
4668#define X509_R_KEY_VALUES_MISMATCH 116
4669#define X509_R_LOADING_CERT_DIR 117
4670#define X509_R_LOADING_DEFAULTS 118
4671#define X509_R_NEWER_CRL_NOT_NEWER 119
4672#define X509_R_NOT_PKCS7_SIGNED_DATA 120
4673#define X509_R_NO_CERTIFICATES_INCLUDED 121
4674#define X509_R_NO_CERT_SET_FOR_US_TO_VERIFY 122
4675#define X509_R_NO_CRLS_INCLUDED 123
4676#define X509_R_NO_CRL_NUMBER 124
4677#define X509_R_PUBLIC_KEY_DECODE_ERROR 125
4678#define X509_R_PUBLIC_KEY_ENCODE_ERROR 126
4679#define X509_R_SHOULD_RETRY 127
4680#define X509_R_UNKNOWN_KEY_TYPE 128
4681#define X509_R_UNKNOWN_NID 129
4682#define X509_R_UNKNOWN_PURPOSE_ID 130
4683#define X509_R_UNKNOWN_TRUST_ID 131
4684#define X509_R_UNSUPPORTED_ALGORITHM 132
4685#define X509_R_WRONG_LOOKUP_TYPE 133
4686#define X509_R_WRONG_TYPE 134
David Benjamin52a3bf22016-05-03 07:50:44 -04004687#define X509_R_NAME_TOO_LONG 135
David Benjamind69d94e2017-03-29 16:08:50 -05004688#define X509_R_INVALID_PARAMETER 136
David Benjamin371305f2018-09-18 13:14:53 -07004689#define X509_R_SIGNATURE_ALGORITHM_MISMATCH 137
David Benjaminfd86eaa2020-06-17 15:13:16 -04004690#define X509_R_DELTA_CRL_WITHOUT_CRL_NUMBER 138
David Benjamindd86e752020-06-17 21:17:42 -04004691#define X509_R_INVALID_FIELD_FOR_VERSION 139
4692#define X509_R_INVALID_VERSION 140
Adam Langleyf7975702022-01-12 16:23:11 -08004693#define X509_R_NO_CERTIFICATE_FOUND 141
4694#define X509_R_NO_CERTIFICATE_OR_CRL_FOUND 142
4695#define X509_R_NO_CRL_FOUND 143
David Benjamin029d0e72022-12-26 19:38:40 -05004696#define X509_R_INVALID_POLICY_EXTENSION 144
Adam Langley95c29f32014-06-20 12:00:00 -07004697
David Benjamina1c79222023-01-02 09:55:09 -08004698#endif // OPENSSL_HEADER_X509_H