blob: 0c8d5df0c68ffa1435f648fb30049199d0c4bb10 [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 Benjamin4363bdd2022-04-06 15:19:16 -0400126// X509_up_ref adds one to the reference count of |x509| and returns one.
127OPENSSL_EXPORT int X509_up_ref(X509 *x509);
128
129// X509_chain_up_ref returns a newly-allocated |STACK_OF(X509)| containing a
130// shallow copy of |chain|, or NULL on error. That is, the return value has the
131// same contents as |chain|, and each |X509|'s reference count is incremented by
132// one.
133OPENSSL_EXPORT STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain);
134
David Benjaminb8596422022-07-10 01:08:07 -0400135// X509_dup returns a newly-allocated copy of |x509|, or NULL on error. This
136// function works by serializing the structure, so auxiliary properties (see
137// |i2d_X509_AUX|) are not preserved. Additionally, if |x509| is incomplete,
138// this function may fail.
139//
140// TODO(https://crbug.com/boringssl/407): This function should be const and
141// thread-safe but is currently neither in some cases, notably if |crl| was
142// mutated.
143OPENSSL_EXPORT X509 *X509_dup(X509 *x509);
144
David Benjamin4363bdd2022-04-06 15:19:16 -0400145// X509_free decrements |x509|'s reference count and, if zero, releases memory
146// associated with |x509|.
147OPENSSL_EXPORT void X509_free(X509 *x509);
148
149// d2i_X509 parses up to |len| bytes from |*inp| as a DER-encoded X.509
David Benjamin19721cd2023-01-16 16:19:03 -0500150// Certificate (RFC 5280), as described in |d2i_SAMPLE|.
David Benjamin4363bdd2022-04-06 15:19:16 -0400151OPENSSL_EXPORT X509 *d2i_X509(X509 **out, const uint8_t **inp, long len);
152
153// X509_parse_from_buffer parses an X.509 structure from |buf| and returns a
154// fresh X509 or NULL on error. There must not be any trailing data in |buf|.
155// The returned structure (if any) holds a reference to |buf| rather than
156// copying parts of it as a normal |d2i_X509| call would do.
157OPENSSL_EXPORT X509 *X509_parse_from_buffer(CRYPTO_BUFFER *buf);
158
159// i2d_X509 marshals |x509| as a DER-encoded X.509 Certificate (RFC 5280), as
160// described in |i2d_SAMPLE|.
161//
162// TODO(https://crbug.com/boringssl/407): This function should be const and
163// thread-safe but is currently neither in some cases, notably if |x509| was
164// mutated.
165OPENSSL_EXPORT int i2d_X509(X509 *x509, uint8_t **outp);
166
167// X509_VERSION_* are X.509 version numbers. Note the numerical values of all
168// defined X.509 versions are one less than the named version.
169#define X509_VERSION_1 0
170#define X509_VERSION_2 1
171#define X509_VERSION_3 2
172
173// X509_get_version returns the numerical value of |x509|'s version, which will
174// be one of the |X509_VERSION_*| constants.
175OPENSSL_EXPORT long X509_get_version(const X509 *x509);
176
177// X509_get0_serialNumber returns |x509|'s serial number.
178OPENSSL_EXPORT const ASN1_INTEGER *X509_get0_serialNumber(const X509 *x509);
179
180// X509_get0_notBefore returns |x509|'s notBefore time.
181OPENSSL_EXPORT const ASN1_TIME *X509_get0_notBefore(const X509 *x509);
182
183// X509_get0_notAfter returns |x509|'s notAfter time.
184OPENSSL_EXPORT const ASN1_TIME *X509_get0_notAfter(const X509 *x509);
185
186// X509_get_issuer_name returns |x509|'s issuer.
187OPENSSL_EXPORT X509_NAME *X509_get_issuer_name(const X509 *x509);
188
189// X509_get_subject_name returns |x509|'s subject.
190OPENSSL_EXPORT X509_NAME *X509_get_subject_name(const X509 *x509);
191
192// X509_get_X509_PUBKEY returns the public key of |x509|. Note this function is
193// not const-correct for legacy reasons. Callers should not modify the returned
194// object.
195OPENSSL_EXPORT X509_PUBKEY *X509_get_X509_PUBKEY(const X509 *x509);
196
197// X509_get_pubkey returns |x509|'s public key as an |EVP_PKEY|, or NULL if the
198// public key was unsupported or could not be decoded. This function returns a
199// reference to the |EVP_PKEY|. The caller must release the result with
200// |EVP_PKEY_free| when done.
201OPENSSL_EXPORT EVP_PKEY *X509_get_pubkey(X509 *x509);
202
203// X509_get0_pubkey_bitstr returns the BIT STRING portion of |x509|'s public
204// key. Note this does not contain the AlgorithmIdentifier portion.
205//
206// WARNING: This function returns a non-const pointer for OpenSSL compatibility,
207// but the caller must not modify the resulting object. Doing so will break
208// internal invariants in |x509|.
209OPENSSL_EXPORT ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x509);
210
David Benjamin2ba76342023-11-19 10:20:04 -0500211// X509_check_private_key returns one if |x509|'s public key matches |pkey| and
212// zero otherwise.
213OPENSSL_EXPORT int X509_check_private_key(X509 *x509, const EVP_PKEY *pkey);
214
David Benjamin4363bdd2022-04-06 15:19:16 -0400215// X509_get0_uids sets |*out_issuer_uid| to a non-owning pointer to the
216// issuerUID field of |x509|, or NULL if |x509| has no issuerUID. It similarly
217// outputs |x509|'s subjectUID field to |*out_subject_uid|.
218//
219// Callers may pass NULL to either |out_issuer_uid| or |out_subject_uid| to
220// ignore the corresponding field.
221OPENSSL_EXPORT void X509_get0_uids(const X509 *x509,
222 const ASN1_BIT_STRING **out_issuer_uid,
223 const ASN1_BIT_STRING **out_subject_uid);
224
David Benjamindd8ffe12023-11-12 12:39:39 -0500225// X509_get_pathlen returns path length constraint from the basic constraints
226// extension in |x509|. (See RFC 5280, section 4.2.1.9.) It returns -1 if the
227// constraint is not present, or if some extension in |x509| was invalid.
228//
229// TODO(crbug.com/boringssl/381): Decoding an |X509| object will not check for
230// invalid extensions. To detect the error case, call
231// |X509_get_extensions_flags| and check the |EXFLAG_INVALID| bit.
232OPENSSL_EXPORT long X509_get_pathlen(X509 *x509);
233
David Benjamin4363bdd2022-04-06 15:19:16 -0400234// X509_get0_extensions returns |x509|'s extension list, or NULL if |x509| omits
235// it.
236OPENSSL_EXPORT const STACK_OF(X509_EXTENSION) *X509_get0_extensions(
237 const X509 *x509);
238
David Benjamin557b80f2022-07-10 19:12:12 -0400239// X509_get_ext_count returns the number of extensions in |x|.
240OPENSSL_EXPORT int X509_get_ext_count(const X509 *x);
241
242// X509_get_ext_by_NID behaves like |X509v3_get_ext_by_NID| but searches for
243// extensions in |x|.
244OPENSSL_EXPORT int X509_get_ext_by_NID(const X509 *x, int nid, int lastpos);
245
246// X509_get_ext_by_OBJ behaves like |X509v3_get_ext_by_OBJ| but searches for
247// extensions in |x|.
248OPENSSL_EXPORT int X509_get_ext_by_OBJ(const X509 *x, const ASN1_OBJECT *obj,
249 int lastpos);
250
251// X509_get_ext_by_critical behaves like |X509v3_get_ext_by_critical| but
252// searches for extensions in |x|.
253OPENSSL_EXPORT int X509_get_ext_by_critical(const X509 *x, int crit,
254 int lastpos);
255
256// X509_get_ext returns the extension in |x| at index |loc|, or NULL if |loc| is
257// out of bounds. This function returns a non-const pointer for OpenSSL
258// compatibility, but callers should not mutate the result.
259OPENSSL_EXPORT X509_EXTENSION *X509_get_ext(const X509 *x, int loc);
260
David Benjamindd8ffe12023-11-12 12:39:39 -0500261// X509_get_ext_d2i behaves like |X509V3_get_d2i| but looks for the extension in
262// |x509|'s extension list.
263//
264// WARNING: This function is difficult to use correctly. See the documentation
265// for |X509V3_get_d2i| for details.
266OPENSSL_EXPORT void *X509_get_ext_d2i(const X509 *x509, int nid,
267 int *out_critical, int *out_idx);
268
David Benjamin4363bdd2022-04-06 15:19:16 -0400269// X509_get0_tbs_sigalg returns the signature algorithm in |x509|'s
270// TBSCertificate. For the outer signature algorithm, see |X509_get0_signature|.
271//
272// Certificates with mismatched signature algorithms will successfully parse,
273// but they will be rejected when verifying.
274OPENSSL_EXPORT const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x509);
275
276// X509_get0_signature sets |*out_sig| and |*out_alg| to the signature and
277// signature algorithm of |x509|, respectively. Either output pointer may be
278// NULL to ignore the value.
279//
280// This function outputs the outer signature algorithm. For the one in the
281// TBSCertificate, see |X509_get0_tbs_sigalg|. Certificates with mismatched
282// signature algorithms will successfully parse, but they will be rejected when
283// verifying.
284OPENSSL_EXPORT void X509_get0_signature(const ASN1_BIT_STRING **out_sig,
285 const X509_ALGOR **out_alg,
286 const X509 *x509);
287
288// X509_get_signature_nid returns the NID corresponding to |x509|'s signature
289// algorithm, or |NID_undef| if the signature algorithm does not correspond to
290// a known NID.
291OPENSSL_EXPORT int X509_get_signature_nid(const X509 *x509);
292
293// i2d_X509_tbs serializes the TBSCertificate portion of |x509|, as described in
294// |i2d_SAMPLE|.
295//
296// This function preserves the original encoding of the TBSCertificate and may
297// not reflect modifications made to |x509|. It may be used to manually verify
298// the signature of an existing certificate. To generate certificates, use
299// |i2d_re_X509_tbs| instead.
300OPENSSL_EXPORT int i2d_X509_tbs(X509 *x509, unsigned char **outp);
301
David Benjamin787713b2023-04-19 10:28:13 -0400302// X509_verify checks that |x509| has a valid signature by |pkey|. It returns
303// one if the signature is valid and zero otherwise. Note this function only
304// checks the signature itself and does not perform a full certificate
305// validation.
306OPENSSL_EXPORT int X509_verify(X509 *x509, EVP_PKEY *pkey);
307
David Benjamin4363bdd2022-04-06 15:19:16 -0400308
309// Issuing certificates.
310//
311// An |X509| object may also represent an incomplete certificate. Callers may
312// construct empty |X509| objects, fill in fields individually, and finally sign
313// the result. The following functions may be used for this purpose.
314
315// X509_new returns a newly-allocated, empty |X509| object, or NULL on error.
316// This produces an incomplete certificate which may be filled in to issue a new
317// certificate.
318OPENSSL_EXPORT X509 *X509_new(void);
319
320// X509_set_version sets |x509|'s version to |version|, which should be one of
321// the |X509V_VERSION_*| constants. It returns one on success and zero on error.
322//
323// If unsure, use |X509_VERSION_3|.
324OPENSSL_EXPORT int X509_set_version(X509 *x509, long version);
325
326// X509_set_serialNumber sets |x509|'s serial number to |serial|. It returns one
327// on success and zero on error.
328OPENSSL_EXPORT int X509_set_serialNumber(X509 *x509,
329 const ASN1_INTEGER *serial);
330
331// X509_set1_notBefore sets |x509|'s notBefore time to |tm|. It returns one on
332// success and zero on error.
333OPENSSL_EXPORT int X509_set1_notBefore(X509 *x509, const ASN1_TIME *tm);
334
335// X509_set1_notAfter sets |x509|'s notAfter time to |tm|. it returns one on
336// success and zero on error.
337OPENSSL_EXPORT int X509_set1_notAfter(X509 *x509, const ASN1_TIME *tm);
338
339// X509_getm_notBefore returns a mutable pointer to |x509|'s notBefore time.
340OPENSSL_EXPORT ASN1_TIME *X509_getm_notBefore(X509 *x509);
341
342// X509_getm_notAfter returns a mutable pointer to |x509|'s notAfter time.
343OPENSSL_EXPORT ASN1_TIME *X509_getm_notAfter(X509 *x);
344
345// X509_set_issuer_name sets |x509|'s issuer to a copy of |name|. It returns one
346// on success and zero on error.
347OPENSSL_EXPORT int X509_set_issuer_name(X509 *x509, X509_NAME *name);
348
349// X509_set_subject_name sets |x509|'s subject to a copy of |name|. It returns
350// one on success and zero on error.
351OPENSSL_EXPORT int X509_set_subject_name(X509 *x509, X509_NAME *name);
352
353// X509_set_pubkey sets |x509|'s public key to |pkey|. It returns one on success
354// and zero on error. This function does not take ownership of |pkey| and
355// internally copies and updates reference counts as needed.
356OPENSSL_EXPORT int X509_set_pubkey(X509 *x509, EVP_PKEY *pkey);
357
David Benjamin557b80f2022-07-10 19:12:12 -0400358// X509_delete_ext removes the extension in |x| at index |loc| and returns the
359// removed extension, or NULL if |loc| was out of bounds. If non-NULL, the
360// caller must release the result with |X509_EXTENSION_free|.
361OPENSSL_EXPORT X509_EXTENSION *X509_delete_ext(X509 *x, int loc);
362
363// X509_add_ext adds a copy of |ex| to |x|. It returns one on success and zero
364// on failure. The caller retains ownership of |ex| and can release it
365// independently of |x|.
366//
367// The new extension is inserted at index |loc|, shifting extensions to the
368// right. If |loc| is -1 or out of bounds, the new extension is appended to the
369// list.
370OPENSSL_EXPORT int X509_add_ext(X509 *x, const X509_EXTENSION *ex, int loc);
371
David Benjamindd8ffe12023-11-12 12:39:39 -0500372// X509_add1_ext_i2d behaves like |X509V3_add1_i2d| but adds the extension to
373// |x|'s extension list.
374//
375// WARNING: This function may return zero or -1 on error. The caller must also
376// ensure |value|'s type matches |nid|. See the documentation for
377// |X509V3_add1_i2d| for details.
378OPENSSL_EXPORT int X509_add1_ext_i2d(X509 *x, int nid, void *value, int crit,
379 unsigned long flags);
380
David Benjamin4363bdd2022-04-06 15:19:16 -0400381// X509_sign signs |x509| with |pkey| and replaces the signature algorithm and
David Benjamin0c7527b2023-04-25 21:45:00 -0400382// signature fields. It returns the length of the signature on success and zero
383// on error. This function uses digest algorithm |md|, or |pkey|'s default if
384// NULL. Other signing parameters use |pkey|'s defaults. To customize them, use
385// |X509_sign_ctx|.
David Benjamin4363bdd2022-04-06 15:19:16 -0400386OPENSSL_EXPORT int X509_sign(X509 *x509, EVP_PKEY *pkey, const EVP_MD *md);
387
388// X509_sign_ctx signs |x509| with |ctx| and replaces the signature algorithm
David Benjamin0c7527b2023-04-25 21:45:00 -0400389// and signature fields. It returns the length of the signature on success and
390// zero on error. The signature algorithm and parameters come from |ctx|, which
391// must have been initialized with |EVP_DigestSignInit|. The caller should
392// configure the corresponding |EVP_PKEY_CTX| before calling this function.
David Benjamin5bef6ec2023-11-13 09:04:43 -0500393//
394// On success or failure, this function mutates |ctx| and resets it to the empty
395// state. Caller should not rely on its contents after the function returns.
David Benjamin4363bdd2022-04-06 15:19:16 -0400396OPENSSL_EXPORT int X509_sign_ctx(X509 *x509, EVP_MD_CTX *ctx);
397
398// i2d_re_X509_tbs serializes the TBSCertificate portion of |x509|, as described
399// in |i2d_SAMPLE|.
400//
401// This function re-encodes the TBSCertificate and may not reflect |x509|'s
402// original encoding. It may be used to manually generate a signature for a new
403// certificate. To verify certificates, use |i2d_X509_tbs| instead.
404OPENSSL_EXPORT int i2d_re_X509_tbs(X509 *x509, unsigned char **outp);
405
406// X509_set1_signature_algo sets |x509|'s signature algorithm to |algo| and
407// returns one on success or zero on error. It updates both the signature field
408// of the TBSCertificate structure, and the signatureAlgorithm field of the
409// Certificate.
410OPENSSL_EXPORT int X509_set1_signature_algo(X509 *x509, const X509_ALGOR *algo);
411
412// X509_set1_signature_value sets |x509|'s signature to a copy of the |sig_len|
413// bytes pointed by |sig|. It returns one on success and zero on error.
414//
415// Due to a specification error, X.509 certificates store signatures in ASN.1
416// BIT STRINGs, but signature algorithms return byte strings rather than bit
417// strings. This function creates a BIT STRING containing a whole number of
418// bytes, with the bit order matching the DER encoding. This matches the
419// encoding used by all X.509 signature algorithms.
420OPENSSL_EXPORT int X509_set1_signature_value(X509 *x509, const uint8_t *sig,
421 size_t sig_len);
422
423
424// Auxiliary certificate properties.
425//
426// |X509| objects optionally maintain auxiliary properties. These are not part
427// of the certificates themselves, and thus are not covered by signatures or
428// preserved by the standard serialization. They are used as inputs or outputs
429// to other functions in this library.
430
431// i2d_X509_AUX marshals |x509| as a DER-encoded X.509 Certificate (RFC 5280),
432// followed optionally by a separate, OpenSSL-specific structure with auxiliary
433// properties. It behaves as described in |i2d_SAMPLE|.
434//
435// Unlike similarly-named functions, this function does not output a single
436// ASN.1 element. Directly embedding the output in a larger ASN.1 structure will
437// not behave correctly.
438OPENSSL_EXPORT int i2d_X509_AUX(X509 *x509, unsigned char **outp);
439
440// d2i_X509_AUX parses up to |length| bytes from |*inp| as a DER-encoded X.509
441// Certificate (RFC 5280), followed optionally by a separate, OpenSSL-specific
David Benjamin19721cd2023-01-16 16:19:03 -0500442// structure with auxiliary properties. It behaves as described in |d2i_SAMPLE|.
David Benjamin4363bdd2022-04-06 15:19:16 -0400443//
444// Some auxiliary properties affect trust decisions, so this function should not
445// be used with untrusted input.
446//
447// Unlike similarly-named functions, this function does not parse a single
448// ASN.1 element. Trying to parse data directly embedded in a larger ASN.1
449// structure will not behave correctly.
450OPENSSL_EXPORT X509 *d2i_X509_AUX(X509 **x509, const unsigned char **inp,
451 long length);
452
453// X509_alias_set1 sets |x509|'s alias to |len| bytes from |name|. If |name| is
454// NULL, the alias is cleared instead. Aliases are not part of the certificate
455// itself and will not be serialized by |i2d_X509|.
456OPENSSL_EXPORT int X509_alias_set1(X509 *x509, const unsigned char *name,
David Benjamina4385192023-03-25 01:26:49 -0400457 ossl_ssize_t len);
David Benjamin4363bdd2022-04-06 15:19:16 -0400458
459// X509_keyid_set1 sets |x509|'s key ID to |len| bytes from |id|. If |id| is
460// NULL, the key ID is cleared instead. Key IDs are not part of the certificate
461// itself and will not be serialized by |i2d_X509|.
462OPENSSL_EXPORT int X509_keyid_set1(X509 *x509, const unsigned char *id,
David Benjamina4385192023-03-25 01:26:49 -0400463 ossl_ssize_t len);
David Benjamin4363bdd2022-04-06 15:19:16 -0400464
465// X509_alias_get0 looks up |x509|'s alias. If found, it sets |*out_len| to the
466// alias's length and returns a pointer to a buffer containing the contents. If
467// not found, it outputs the empty string by returning NULL and setting
468// |*out_len| to zero.
469//
470// If |x509| was parsed from a PKCS#12 structure (see
471// |PKCS12_get_key_and_certs|), the alias will reflect the friendlyName
472// attribute (RFC 2985).
473//
474// WARNING: In OpenSSL, this function did not set |*out_len| when the alias was
475// missing. Callers that target both OpenSSL and BoringSSL should set the value
476// to zero before calling this function.
477OPENSSL_EXPORT unsigned char *X509_alias_get0(X509 *x509, int *out_len);
478
479// X509_keyid_get0 looks up |x509|'s key ID. If found, it sets |*out_len| to the
480// key ID's length and returns a pointer to a buffer containing the contents. If
481// not found, it outputs the empty string by returning NULL and setting
482// |*out_len| to zero.
483//
484// WARNING: In OpenSSL, this function did not set |*out_len| when the alias was
485// missing. Callers that target both OpenSSL and BoringSSL should set the value
486// to zero before calling this function.
487OPENSSL_EXPORT unsigned char *X509_keyid_get0(X509 *x509, int *out_len);
488
David Benjamin240b73a2023-11-12 12:13:03 -0500489// X509_add1_trust_object configures |x509| as a valid trust anchor for |obj|.
490// It returns one on success and zero on error. |obj| should be a certificate
491// usage OID associated with an |X509_TRUST| object.
492OPENSSL_EXPORT int X509_add1_trust_object(X509 *x509, const ASN1_OBJECT *obj);
493
494// X509_add1_reject_object configures |x509| as distrusted for |obj|. It returns
495// one on success and zero on error. |obj| should be a certificate usage OID
496// associated with an |X509_TRUST| object.
497OPENSSL_EXPORT int X509_add1_reject_object(X509 *x509, const ASN1_OBJECT *obj);
498
David Benjamin09febb32023-11-21 00:14:22 -0500499// X509_trust_clear clears the list of OIDs for which |x509| is trusted. See
David Benjamin240b73a2023-11-12 12:13:03 -0500500// also |X509_add1_trust_object|.
501OPENSSL_EXPORT void X509_trust_clear(X509 *x509);
502
503// X509_reject_clear clears the list of OIDs for which |x509| is distrusted. See
504// also |X509_add1_reject_object|.
505OPENSSL_EXPORT void X509_reject_clear(X509 *x509);
506
David Benjamin4363bdd2022-04-06 15:19:16 -0400507
508// Certificate revocation lists.
509//
510// An |X509_CRL| object represents an X.509 certificate revocation list (CRL),
David Benjamindd8ffe12023-11-12 12:39:39 -0500511// defined in RFC 5280. A CRL is a signed list of certificates, the
512// revokedCertificates field, which are no longer considered valid. Each entry
513// of this list is represented with an |X509_REVOKED| object, documented in the
514// "CRL entries" section below.
David Benjamin4363bdd2022-04-06 15:19:16 -0400515//
David Benjamindd8ffe12023-11-12 12:39:39 -0500516// Although an |X509_CRL| is a mutable object, mutating an |X509_CRL| or its
517// |X509_REVOKED|s can give incorrect results. Callers typically obtain
518// |X509_CRL|s by parsing some input with |d2i_X509_CRL|, etc. Such objects
519// carry information such as the serialized TBSCertList and decoded extensions,
520// which will become inconsistent when mutated.
David Benjamin4363bdd2022-04-06 15:19:16 -0400521//
522// Instead, mutation functions should only be used when issuing new CRLs, as
523// described in a later section.
524
525DEFINE_STACK_OF(X509_CRL)
David Benjamindd8ffe12023-11-12 12:39:39 -0500526DEFINE_STACK_OF(X509_REVOKED)
David Benjamin4363bdd2022-04-06 15:19:16 -0400527
David Benjamin4363bdd2022-04-06 15:19:16 -0400528// X509_CRL_up_ref adds one to the reference count of |crl| and returns one.
529OPENSSL_EXPORT int X509_CRL_up_ref(X509_CRL *crl);
530
David Benjaminb8596422022-07-10 01:08:07 -0400531// X509_CRL_dup returns a newly-allocated copy of |crl|, or NULL on error. This
532// function works by serializing the structure, so if |crl| is incomplete, it
533// may fail.
534//
535// TODO(https://crbug.com/boringssl/407): This function should be const and
536// thread-safe but is currently neither in some cases, notably if |crl| was
537// mutated.
538OPENSSL_EXPORT X509_CRL *X509_CRL_dup(X509_CRL *crl);
539
David Benjamin4363bdd2022-04-06 15:19:16 -0400540// X509_CRL_free decrements |crl|'s reference count and, if zero, releases
541// memory associated with |crl|.
542OPENSSL_EXPORT void X509_CRL_free(X509_CRL *crl);
543
544// d2i_X509_CRL parses up to |len| bytes from |*inp| as a DER-encoded X.509
David Benjamin19721cd2023-01-16 16:19:03 -0500545// CertificateList (RFC 5280), as described in |d2i_SAMPLE|.
David Benjamin4363bdd2022-04-06 15:19:16 -0400546OPENSSL_EXPORT X509_CRL *d2i_X509_CRL(X509_CRL **out, const uint8_t **inp,
547 long len);
548
549// i2d_X509_CRL marshals |crl| as a X.509 CertificateList (RFC 5280), as
550// described in |i2d_SAMPLE|.
551//
552// TODO(https://crbug.com/boringssl/407): This function should be const and
553// thread-safe but is currently neither in some cases, notably if |crl| was
554// mutated.
555OPENSSL_EXPORT int i2d_X509_CRL(X509_CRL *crl, uint8_t **outp);
556
557#define X509_CRL_VERSION_1 0
558#define X509_CRL_VERSION_2 1
559
560// X509_CRL_get_version returns the numerical value of |crl|'s version, which
561// will be one of the |X509_CRL_VERSION_*| constants.
562OPENSSL_EXPORT long X509_CRL_get_version(const X509_CRL *crl);
563
David Benjamin884ae3a2022-07-08 18:18:12 -0400564// X509_CRL_get0_lastUpdate returns |crl|'s thisUpdate time. The OpenSSL API
565// refers to this field as lastUpdate.
David Benjamin4363bdd2022-04-06 15:19:16 -0400566OPENSSL_EXPORT const ASN1_TIME *X509_CRL_get0_lastUpdate(const X509_CRL *crl);
567
568// X509_CRL_get0_nextUpdate returns |crl|'s nextUpdate time, or NULL if |crl|
569// has none.
570OPENSSL_EXPORT const ASN1_TIME *X509_CRL_get0_nextUpdate(const X509_CRL *crl);
571
572// X509_CRL_get_issuer returns |crl|'s issuer name. Note this function is not
573// const-correct for legacy reasons.
574OPENSSL_EXPORT X509_NAME *X509_CRL_get_issuer(const X509_CRL *crl);
575
David Benjamindd8ffe12023-11-12 12:39:39 -0500576// X509_CRL_get0_by_serial finds the entry in |crl| whose serial number is
David Benjamin9177d652023-11-12 14:05:24 -0500577// |serial|. If found, it sets |*out| to the entry and returns one. If not
578// found, it returns zero.
David Benjamindd8ffe12023-11-12 12:39:39 -0500579//
580// On success, |*out| continues to be owned by |crl|. It is an error to free or
581// otherwise modify |*out|.
582//
583// TODO(crbug.com/boringssl/600): Ideally |crl| would be const. It is broadly
584// thread-safe, but changes the order of entries in |crl|. It cannot be called
585// concurrently with |i2d_X509_CRL|.
David Benjamindd8ffe12023-11-12 12:39:39 -0500586OPENSSL_EXPORT int X509_CRL_get0_by_serial(X509_CRL *crl, X509_REVOKED **out,
587 const ASN1_INTEGER *serial);
588
589// X509_CRL_get0_by_cert behaves like |X509_CRL_get0_by_serial|, except it looks
590// for the entry that matches |x509|.
591OPENSSL_EXPORT int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **out,
592 X509 *x509);
593
David Benjamin4363bdd2022-04-06 15:19:16 -0400594// X509_CRL_get_REVOKED returns the list of revoked certificates in |crl|, or
595// NULL if |crl| omits it.
596//
597// TOOD(davidben): This function was originally a macro, without clear const
598// semantics. It should take a const input and give const output, but the latter
599// would break existing callers. For now, we match upstream.
600OPENSSL_EXPORT STACK_OF(X509_REVOKED) *X509_CRL_get_REVOKED(X509_CRL *crl);
601
602// X509_CRL_get0_extensions returns |crl|'s extension list, or NULL if |crl|
David Benjamindd8ffe12023-11-12 12:39:39 -0500603// omits it. A CRL can have extensions on individual entries, which is
604// |X509_REVOKED_get0_extensions|, or on the overall CRL, which is this
605// function.
David Benjamin4363bdd2022-04-06 15:19:16 -0400606OPENSSL_EXPORT const STACK_OF(X509_EXTENSION) *X509_CRL_get0_extensions(
607 const X509_CRL *crl);
608
David Benjamin557b80f2022-07-10 19:12:12 -0400609// X509_CRL_get_ext_count returns the number of extensions in |x|.
610OPENSSL_EXPORT int X509_CRL_get_ext_count(const X509_CRL *x);
611
612// X509_CRL_get_ext_by_NID behaves like |X509v3_get_ext_by_NID| but searches for
613// extensions in |x|.
614OPENSSL_EXPORT int X509_CRL_get_ext_by_NID(const X509_CRL *x, int nid,
615 int lastpos);
616
617// X509_CRL_get_ext_by_OBJ behaves like |X509v3_get_ext_by_OBJ| but searches for
618// extensions in |x|.
619OPENSSL_EXPORT int X509_CRL_get_ext_by_OBJ(const X509_CRL *x,
620 const ASN1_OBJECT *obj, int lastpos);
621
622// X509_CRL_get_ext_by_critical behaves like |X509v3_get_ext_by_critical| but
623// searches for extensions in |x|.
624OPENSSL_EXPORT int X509_CRL_get_ext_by_critical(const X509_CRL *x, int crit,
625 int lastpos);
626
627// X509_CRL_get_ext returns the extension in |x| at index |loc|, or NULL if
628// |loc| is out of bounds. This function returns a non-const pointer for OpenSSL
629// compatibility, but callers should not mutate the result.
630OPENSSL_EXPORT X509_EXTENSION *X509_CRL_get_ext(const X509_CRL *x, int loc);
631
David Benjamindd8ffe12023-11-12 12:39:39 -0500632// X509_CRL_get_ext_d2i behaves like |X509V3_get_d2i| but looks for the
633// extension in |crl|'s extension list.
634//
635// WARNING: This function is difficult to use correctly. See the documentation
636// for |X509V3_get_d2i| for details.
637OPENSSL_EXPORT void *X509_CRL_get_ext_d2i(const X509_CRL *crl, int nid,
638 int *out_critical, int *out_idx);
639
David Benjamin4363bdd2022-04-06 15:19:16 -0400640// X509_CRL_get0_signature sets |*out_sig| and |*out_alg| to the signature and
641// signature algorithm of |crl|, respectively. Either output pointer may be NULL
642// to ignore the value.
643//
644// This function outputs the outer signature algorithm, not the one in the
645// TBSCertList. CRLs with mismatched signature algorithms will successfully
646// parse, but they will be rejected when verifying.
647OPENSSL_EXPORT void X509_CRL_get0_signature(const X509_CRL *crl,
648 const ASN1_BIT_STRING **out_sig,
649 const X509_ALGOR **out_alg);
650
651// X509_CRL_get_signature_nid returns the NID corresponding to |crl|'s signature
652// algorithm, or |NID_undef| if the signature algorithm does not correspond to
653// a known NID.
654OPENSSL_EXPORT int X509_CRL_get_signature_nid(const X509_CRL *crl);
655
656// i2d_X509_CRL_tbs serializes the TBSCertList portion of |crl|, as described in
657// |i2d_SAMPLE|.
658//
659// This function preserves the original encoding of the TBSCertList and may not
660// reflect modifications made to |crl|. It may be used to manually verify the
661// signature of an existing CRL. To generate CRLs, use |i2d_re_X509_CRL_tbs|
662// instead.
663OPENSSL_EXPORT int i2d_X509_CRL_tbs(X509_CRL *crl, unsigned char **outp);
664
David Benjamin787713b2023-04-19 10:28:13 -0400665// X509_CRL_verify checks that |crl| has a valid signature by |pkey|. It returns
666// one if the signature is valid and zero otherwise.
667OPENSSL_EXPORT int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *pkey);
668
David Benjamin4363bdd2022-04-06 15:19:16 -0400669
670// Issuing certificate revocation lists.
671//
672// An |X509_CRL| object may also represent an incomplete CRL. Callers may
673// construct empty |X509_CRL| objects, fill in fields individually, and finally
674// sign the result. The following functions may be used for this purpose.
675
676// X509_CRL_new returns a newly-allocated, empty |X509_CRL| object, or NULL on
677// error. This object may be filled in and then signed to construct a CRL.
678OPENSSL_EXPORT X509_CRL *X509_CRL_new(void);
679
680// X509_CRL_set_version sets |crl|'s version to |version|, which should be one
681// of the |X509_CRL_VERSION_*| constants. It returns one on success and zero on
682// error.
683//
684// If unsure, use |X509_CRL_VERSION_2|. Note that, unlike certificates, CRL
685// versions are only defined up to v2. Callers should not use |X509_VERSION_3|.
686OPENSSL_EXPORT int X509_CRL_set_version(X509_CRL *crl, long version);
687
688// X509_CRL_set_issuer_name sets |crl|'s issuer to a copy of |name|. It returns
689// one on success and zero on error.
690OPENSSL_EXPORT int X509_CRL_set_issuer_name(X509_CRL *crl, X509_NAME *name);
691
David Benjamin884ae3a2022-07-08 18:18:12 -0400692// X509_CRL_set1_lastUpdate sets |crl|'s thisUpdate time to |tm|. It returns one
693// on success and zero on error. The OpenSSL API refers to this field as
694// lastUpdate.
David Benjamin4363bdd2022-04-06 15:19:16 -0400695OPENSSL_EXPORT int X509_CRL_set1_lastUpdate(X509_CRL *crl, const ASN1_TIME *tm);
696
697// X509_CRL_set1_nextUpdate sets |crl|'s nextUpdate time to |tm|. It returns one
698// on success and zero on error.
699OPENSSL_EXPORT int X509_CRL_set1_nextUpdate(X509_CRL *crl, const ASN1_TIME *tm);
700
David Benjamindd8ffe12023-11-12 12:39:39 -0500701// X509_CRL_add0_revoked adds |rev| to |crl|. On success, it takes ownership of
702// |rev| and returns one. On error, it returns zero. If this function fails, the
703// caller retains ownership of |rev| and must release it when done.
704OPENSSL_EXPORT int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev);
705
706// X509_CRL_sort sorts the entries in |crl| by serial number. It returns one on
707// success and zero on error.
708OPENSSL_EXPORT int X509_CRL_sort(X509_CRL *crl);
709
David Benjamin557b80f2022-07-10 19:12:12 -0400710// X509_CRL_delete_ext removes the extension in |x| at index |loc| and returns
711// the removed extension, or NULL if |loc| was out of bounds. If non-NULL, the
712// caller must release the result with |X509_EXTENSION_free|.
713OPENSSL_EXPORT X509_EXTENSION *X509_CRL_delete_ext(X509_CRL *x, int loc);
714
715// X509_CRL_add_ext adds a copy of |ex| to |x|. It returns one on success and
716// zero on failure. The caller retains ownership of |ex| and can release it
717// independently of |x|.
718//
719// The new extension is inserted at index |loc|, shifting extensions to the
720// right. If |loc| is -1 or out of bounds, the new extension is appended to the
721// list.
722OPENSSL_EXPORT int X509_CRL_add_ext(X509_CRL *x, const X509_EXTENSION *ex,
723 int loc);
724
David Benjamindd8ffe12023-11-12 12:39:39 -0500725// X509_CRL_add1_ext_i2d behaves like |X509V3_add1_i2d| but adds the extension
726// to |x|'s extension list.
727//
728// WARNING: This function may return zero or -1 on error. The caller must also
729// ensure |value|'s type matches |nid|. See the documentation for
730// |X509V3_add1_i2d| for details.
731OPENSSL_EXPORT int X509_CRL_add1_ext_i2d(X509_CRL *x, int nid, void *value,
732 int crit, unsigned long flags);
733
David Benjamin4363bdd2022-04-06 15:19:16 -0400734// X509_CRL_sign signs |crl| with |pkey| and replaces the signature algorithm
David Benjamin0c7527b2023-04-25 21:45:00 -0400735// and signature fields. It returns the length of the signature on success and
736// zero on error. This function uses digest algorithm |md|, or |pkey|'s default
737// if NULL. Other signing parameters use |pkey|'s defaults. To customize them,
738// use |X509_CRL_sign_ctx|.
David Benjamin4363bdd2022-04-06 15:19:16 -0400739OPENSSL_EXPORT int X509_CRL_sign(X509_CRL *crl, EVP_PKEY *pkey,
740 const EVP_MD *md);
741
742// X509_CRL_sign_ctx signs |crl| with |ctx| and replaces the signature algorithm
David Benjamin0c7527b2023-04-25 21:45:00 -0400743// and signature fields. It returns the length of the signature on success and
744// zero on error. The signature algorithm and parameters come from |ctx|, which
745// must have been initialized with |EVP_DigestSignInit|. The caller should
746// configure the corresponding |EVP_PKEY_CTX| before calling this function.
David Benjamin5bef6ec2023-11-13 09:04:43 -0500747//
748// On success or failure, this function mutates |ctx| and resets it to the empty
749// state. Caller should not rely on its contents after the function returns.
David Benjamin4363bdd2022-04-06 15:19:16 -0400750OPENSSL_EXPORT int X509_CRL_sign_ctx(X509_CRL *crl, EVP_MD_CTX *ctx);
751
752// i2d_re_X509_CRL_tbs serializes the TBSCertList portion of |crl|, as described
753// in |i2d_SAMPLE|.
754//
755// This function re-encodes the TBSCertList and may not reflect |crl|'s original
756// encoding. It may be used to manually generate a signature for a new CRL. To
757// verify CRLs, use |i2d_X509_CRL_tbs| instead.
758OPENSSL_EXPORT int i2d_re_X509_CRL_tbs(X509_CRL *crl, unsigned char **outp);
759
760// X509_CRL_set1_signature_algo sets |crl|'s signature algorithm to |algo| and
761// returns one on success or zero on error. It updates both the signature field
762// of the TBSCertList structure, and the signatureAlgorithm field of the CRL.
763OPENSSL_EXPORT int X509_CRL_set1_signature_algo(X509_CRL *crl,
764 const X509_ALGOR *algo);
765
766// X509_CRL_set1_signature_value sets |crl|'s signature to a copy of the
767// |sig_len| bytes pointed by |sig|. It returns one on success and zero on
768// error.
769//
770// Due to a specification error, X.509 CRLs store signatures in ASN.1 BIT
771// STRINGs, but signature algorithms return byte strings rather than bit
772// strings. This function creates a BIT STRING containing a whole number of
773// bytes, with the bit order matching the DER encoding. This matches the
774// encoding used by all X.509 signature algorithms.
775OPENSSL_EXPORT int X509_CRL_set1_signature_value(X509_CRL *crl,
776 const uint8_t *sig,
777 size_t sig_len);
778
779
David Benjamindd8ffe12023-11-12 12:39:39 -0500780// CRL entries.
781//
782// Each entry of a CRL is represented as an |X509_REVOKED| object, which
783// describes a revoked certificate by serial number.
784//
785// When an |X509_REVOKED| is obtained from an |X509_CRL| object, it is an error
786// to mutate the object. Doing so may break |X509_CRL|'s and cause the library
787// to behave incorrectly.
788
David Benjamindd8ffe12023-11-12 12:39:39 -0500789// X509_REVOKED_new returns a newly-allocated, empty |X509_REVOKED| object, or
790// NULL on allocation error.
791OPENSSL_EXPORT X509_REVOKED *X509_REVOKED_new(void);
792
793// X509_REVOKED_free releases memory associated with |rev|.
794OPENSSL_EXPORT void X509_REVOKED_free(X509_REVOKED *rev);
795
796// d2i_X509_REVOKED parses up to |len| bytes from |*inp| as a DER-encoded X.509
797// CRL entry, as described in |d2i_SAMPLE|.
798OPENSSL_EXPORT X509_REVOKED *d2i_X509_REVOKED(X509_REVOKED **out,
799 const uint8_t **inp, long len);
800
801// i2d_X509_REVOKED marshals |alg| as a DER-encoded X.509 CRL entry, as
802// described in |i2d_SAMPLE|.
803OPENSSL_EXPORT int i2d_X509_REVOKED(const X509_REVOKED *alg, uint8_t **outp);
804
805// X509_REVOKED_dup returns a newly-allocated copy of |rev|, or NULL on error.
806// This function works by serializing the structure, so if |rev| is incomplete,
807// it may fail.
808OPENSSL_EXPORT X509_REVOKED *X509_REVOKED_dup(const X509_REVOKED *rev);
809
810// X509_REVOKED_get0_serialNumber returns the serial number of the certificate
811// revoked by |revoked|.
812OPENSSL_EXPORT const ASN1_INTEGER *X509_REVOKED_get0_serialNumber(
813 const X509_REVOKED *revoked);
814
815// X509_REVOKED_set_serialNumber sets |revoked|'s serial number to |serial|. It
816// returns one on success or zero on error.
817OPENSSL_EXPORT int X509_REVOKED_set_serialNumber(X509_REVOKED *revoked,
818 const ASN1_INTEGER *serial);
819
820// X509_REVOKED_get0_revocationDate returns the revocation time of the
821// certificate revoked by |revoked|.
822OPENSSL_EXPORT const ASN1_TIME *X509_REVOKED_get0_revocationDate(
823 const X509_REVOKED *revoked);
824
825// X509_REVOKED_set_revocationDate sets |revoked|'s revocation time to |tm|. It
826// returns one on success or zero on error.
827OPENSSL_EXPORT int X509_REVOKED_set_revocationDate(X509_REVOKED *revoked,
828 const ASN1_TIME *tm);
829
830// X509_REVOKED_get0_extensions returns |r|'s extensions list, or NULL if |r|
831// omits it. A CRL can have extensions on individual entries, which is this
832// function, or on the overall CRL, which is |X509_CRL_get0_extensions|.
833OPENSSL_EXPORT const STACK_OF(X509_EXTENSION) *X509_REVOKED_get0_extensions(
834 const X509_REVOKED *r);
835
836 // X509_REVOKED_get_ext_count returns the number of extensions in |x|.
837OPENSSL_EXPORT int X509_REVOKED_get_ext_count(const X509_REVOKED *x);
838
839// X509_REVOKED_get_ext_by_NID behaves like |X509v3_get_ext_by_NID| but searches
840// for extensions in |x|.
841OPENSSL_EXPORT int X509_REVOKED_get_ext_by_NID(const X509_REVOKED *x, int nid,
842 int lastpos);
843
844// X509_REVOKED_get_ext_by_OBJ behaves like |X509v3_get_ext_by_OBJ| but searches
845// for extensions in |x|.
846OPENSSL_EXPORT int X509_REVOKED_get_ext_by_OBJ(const X509_REVOKED *x,
847 const ASN1_OBJECT *obj,
848 int lastpos);
849
850// X509_REVOKED_get_ext_by_critical behaves like |X509v3_get_ext_by_critical|
851// but searches for extensions in |x|.
852OPENSSL_EXPORT int X509_REVOKED_get_ext_by_critical(const X509_REVOKED *x,
853 int crit, int lastpos);
854
855// X509_REVOKED_get_ext returns the extension in |x| at index |loc|, or NULL if
856// |loc| is out of bounds. This function returns a non-const pointer for OpenSSL
857// compatibility, but callers should not mutate the result.
858OPENSSL_EXPORT X509_EXTENSION *X509_REVOKED_get_ext(const X509_REVOKED *x,
859 int loc);
860
861// X509_REVOKED_delete_ext removes the extension in |x| at index |loc| and
862// returns the removed extension, or NULL if |loc| was out of bounds. If
863// non-NULL, the caller must release the result with |X509_EXTENSION_free|.
864OPENSSL_EXPORT X509_EXTENSION *X509_REVOKED_delete_ext(X509_REVOKED *x,
865 int loc);
866
867// X509_REVOKED_add_ext adds a copy of |ex| to |x|. It returns one on success
868// and zero on failure. The caller retains ownership of |ex| and can release it
869// independently of |x|.
870//
871// The new extension is inserted at index |loc|, shifting extensions to the
872// right. If |loc| is -1 or out of bounds, the new extension is appended to the
873// list.
874OPENSSL_EXPORT int X509_REVOKED_add_ext(X509_REVOKED *x,
875 const X509_EXTENSION *ex, int loc);
876
877// X509_REVOKED_get_ext_d2i behaves like |X509V3_get_d2i| but looks for the
878// extension in |revoked|'s extension list.
879//
880// WARNING: This function is difficult to use correctly. See the documentation
881// for |X509V3_get_d2i| for details.
882OPENSSL_EXPORT void *X509_REVOKED_get_ext_d2i(const X509_REVOKED *revoked,
883 int nid, int *out_critical,
884 int *out_idx);
885
886// X509_REVOKED_add1_ext_i2d behaves like |X509V3_add1_i2d| but adds the
887// extension to |x|'s extension list.
888//
889// WARNING: This function may return zero or -1 on error. The caller must also
890// ensure |value|'s type matches |nid|. See the documentation for
891// |X509V3_add1_i2d| for details.
892OPENSSL_EXPORT int X509_REVOKED_add1_ext_i2d(X509_REVOKED *x, int nid,
893 void *value, int crit,
894 unsigned long flags);
895
896
David Benjamin4363bdd2022-04-06 15:19:16 -0400897// Certificate requests.
898//
899// An |X509_REQ| represents a PKCS #10 certificate request (RFC 2986). These are
900// also referred to as certificate signing requests or CSRs. CSRs are a common
901// format used to request a certificate from a CA.
902//
903// Although an |X509_REQ| is a mutable object, mutating an |X509_REQ| can give
904// incorrect results. Callers typically obtain |X509_REQ|s by parsing some input
905// with |d2i_X509_REQ|, etc. Such objects carry information such as the
906// serialized CertificationRequestInfo, which will become inconsistent when
907// mutated.
908//
909// Instead, mutation functions should only be used when issuing new CRLs, as
910// described in a later section.
911
David Benjaminb8596422022-07-10 01:08:07 -0400912// X509_REQ_dup returns a newly-allocated copy of |req|, or NULL on error. This
913// function works by serializing the structure, so if |req| is incomplete, it
914// may fail.
915//
916// TODO(https://crbug.com/boringssl/407): This function should be const and
917// thread-safe but is currently neither in some cases, notably if |req| was
918// mutated.
919OPENSSL_EXPORT X509_REQ *X509_REQ_dup(X509_REQ *req);
920
David Benjamin4363bdd2022-04-06 15:19:16 -0400921// X509_REQ_free releases memory associated with |req|.
922OPENSSL_EXPORT void X509_REQ_free(X509_REQ *req);
923
924// d2i_X509_REQ parses up to |len| bytes from |*inp| as a DER-encoded
David Benjamin19721cd2023-01-16 16:19:03 -0500925// CertificateRequest (RFC 2986), as described in |d2i_SAMPLE|.
David Benjamin4363bdd2022-04-06 15:19:16 -0400926OPENSSL_EXPORT X509_REQ *d2i_X509_REQ(X509_REQ **out, const uint8_t **inp,
927 long len);
928
929// i2d_X509_REQ marshals |req| as a CertificateRequest (RFC 2986), as described
930// in |i2d_SAMPLE|.
931//
932// TODO(https://crbug.com/boringssl/407): This function should be const and
933// thread-safe but is currently neither in some cases, notably if |req| was
934// mutated.
935OPENSSL_EXPORT int i2d_X509_REQ(X509_REQ *req, uint8_t **outp);
936
David Benjamin4363bdd2022-04-06 15:19:16 -0400937// X509_REQ_VERSION_1 is the version constant for |X509_REQ| objects. No other
938// versions are defined.
939#define X509_REQ_VERSION_1 0
940
941// X509_REQ_get_version returns the numerical value of |req|'s version. This
942// will always be |X509_REQ_VERSION_1| for valid CSRs. For compatibility,
943// |d2i_X509_REQ| also accepts some invalid version numbers, in which case this
944// function may return other values.
945OPENSSL_EXPORT long X509_REQ_get_version(const X509_REQ *req);
946
947// X509_REQ_get_subject_name returns |req|'s subject name. Note this function is
948// not const-correct for legacy reasons.
949OPENSSL_EXPORT X509_NAME *X509_REQ_get_subject_name(const X509_REQ *req);
950
951// X509_REQ_get_pubkey returns |req|'s public key as an |EVP_PKEY|, or NULL if
952// the public key was unsupported or could not be decoded. This function returns
953// a reference to the |EVP_PKEY|. The caller must release the result with
954// |EVP_PKEY_free| when done.
955OPENSSL_EXPORT EVP_PKEY *X509_REQ_get_pubkey(X509_REQ *req);
956
David Benjamin2ba76342023-11-19 10:20:04 -0500957// X509_REQ_check_private_key returns one if |req|'s public key matches |pkey|
958// and zero otherwise.
959OPENSSL_EXPORT int X509_REQ_check_private_key(X509_REQ *req,
960 const EVP_PKEY *pkey);
961
David Benjamin787713b2023-04-19 10:28:13 -0400962// X509_REQ_get_attr_count returns the number of attributes in |req|.
963OPENSSL_EXPORT int X509_REQ_get_attr_count(const X509_REQ *req);
964
965// X509_REQ_get_attr returns the attribute at index |loc| in |req|, or NULL if
966// out of bounds.
967OPENSSL_EXPORT X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc);
968
969// X509_REQ_get_attr_by_NID returns the index of the attribute in |req| of type
970// |nid|, or a negative number if not found. If found, callers can use
971// |X509_REQ_get_attr| to look up the attribute by index.
972//
973// If |lastpos| is non-negative, it begins searching at |lastpos| + 1. Callers
974// can thus loop over all matching attributes by first passing -1 and then
975// passing the previously-returned value until no match is returned.
976OPENSSL_EXPORT int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid,
977 int lastpos);
978
979// X509_REQ_get_attr_by_OBJ behaves like |X509_REQ_get_attr_by_NID| but looks
980// for attributes of type |obj|.
981OPENSSL_EXPORT int X509_REQ_get_attr_by_OBJ(const X509_REQ *req,
982 const ASN1_OBJECT *obj,
983 int lastpos);
984
985// X509_REQ_extension_nid returns one if |nid| is a supported CSR attribute type
986// for carrying extensions and zero otherwise. The supported types are
987// |NID_ext_req| (pkcs-9-at-extensionRequest from RFC 2985) and |NID_ms_ext_req|
988// (a Microsoft szOID_CERT_EXTENSIONS variant).
989OPENSSL_EXPORT int X509_REQ_extension_nid(int nid);
990
991// X509_REQ_get_extensions decodes the list of requested extensions in |req| and
992// returns a newly-allocated |STACK_OF(X509_EXTENSION)| containing the result.
993// It returns NULL on error, or if |req| did not request extensions.
994//
995// CSRs do not store extensions directly. Instead there are attribute types
996// which are defined to hold extensions. See |X509_REQ_extension_nid|. This
997// function supports both pkcs-9-at-extensionRequest from RFC 2985 and the
998// Microsoft szOID_CERT_EXTENSIONS variant. If both are present,
999// pkcs-9-at-extensionRequest is preferred.
1000OPENSSL_EXPORT STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(X509_REQ *req);
1001
David Benjamin4363bdd2022-04-06 15:19:16 -04001002// X509_REQ_get0_signature sets |*out_sig| and |*out_alg| to the signature and
1003// signature algorithm of |req|, respectively. Either output pointer may be NULL
1004// to ignore the value.
1005OPENSSL_EXPORT void X509_REQ_get0_signature(const X509_REQ *req,
1006 const ASN1_BIT_STRING **out_sig,
1007 const X509_ALGOR **out_alg);
1008
1009// X509_REQ_get_signature_nid returns the NID corresponding to |req|'s signature
1010// algorithm, or |NID_undef| if the signature algorithm does not correspond to
1011// a known NID.
1012OPENSSL_EXPORT int X509_REQ_get_signature_nid(const X509_REQ *req);
1013
David Benjamin787713b2023-04-19 10:28:13 -04001014// X509_REQ_verify checks that |req| has a valid signature by |pkey|. It returns
1015// one if the signature is valid and zero otherwise.
1016OPENSSL_EXPORT int X509_REQ_verify(X509_REQ *req, EVP_PKEY *pkey);
1017
David Benjamin4363bdd2022-04-06 15:19:16 -04001018
1019// Issuing certificate requests.
1020//
1021// An |X509_REQ| object may also represent an incomplete CSR. Callers may
1022// construct empty |X509_REQ| objects, fill in fields individually, and finally
1023// sign the result. The following functions may be used for this purpose.
1024
1025// X509_REQ_new returns a newly-allocated, empty |X509_REQ| object, or NULL on
1026// error. This object may be filled in and then signed to construct a CSR.
1027OPENSSL_EXPORT X509_REQ *X509_REQ_new(void);
1028
1029// X509_REQ_set_version sets |req|'s version to |version|, which should be
1030// |X509_REQ_VERSION_1|. It returns one on success and zero on error.
1031//
1032// The only defined CSR version is |X509_REQ_VERSION_1|, so there is no need to
1033// call this function.
1034OPENSSL_EXPORT int X509_REQ_set_version(X509_REQ *req, long version);
1035
1036// X509_REQ_set_subject_name sets |req|'s subject to a copy of |name|. It
1037// returns one on success and zero on error.
1038OPENSSL_EXPORT int X509_REQ_set_subject_name(X509_REQ *req, X509_NAME *name);
1039
1040// X509_REQ_set_pubkey sets |req|'s public key to |pkey|. It returns one on
1041// success and zero on error. This function does not take ownership of |pkey|
1042// and internally copies and updates reference counts as needed.
1043OPENSSL_EXPORT int X509_REQ_set_pubkey(X509_REQ *req, EVP_PKEY *pkey);
1044
David Benjamin787713b2023-04-19 10:28:13 -04001045// X509_REQ_delete_attr removes the attribute at index |loc| in |req|. It
1046// returns the removed attribute to the caller, or NULL if |loc| was out of
1047// bounds. If non-NULL, the caller must release the result with
1048// |X509_ATTRIBUTE_free| when done. It is also safe, but not necessary, to call
1049// |X509_ATTRIBUTE_free| if the result is NULL.
1050OPENSSL_EXPORT X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc);
1051
1052// X509_REQ_add1_attr appends a copy of |attr| to |req|'s list of attributes. It
1053// returns one on success and zero on error.
David Benjamin437ef4d2023-04-19 10:41:02 -04001054OPENSSL_EXPORT int X509_REQ_add1_attr(X509_REQ *req,
1055 const X509_ATTRIBUTE *attr);
David Benjamin787713b2023-04-19 10:28:13 -04001056
1057// X509_REQ_add1_attr_by_OBJ appends a new attribute to |req| with type |obj|.
1058// It returns one on success and zero on error. The value is determined by
1059// |X509_ATTRIBUTE_set1_data|.
1060//
1061// WARNING: The interpretation of |attrtype|, |data|, and |len| is complex and
1062// error-prone. See |X509_ATTRIBUTE_set1_data| for details.
1063OPENSSL_EXPORT int X509_REQ_add1_attr_by_OBJ(X509_REQ *req,
1064 const ASN1_OBJECT *obj,
1065 int attrtype,
1066 const unsigned char *data,
1067 int len);
1068
1069// X509_REQ_add1_attr_by_NID behaves like |X509_REQ_add1_attr_by_OBJ| except the
1070// attribute type is determined by |nid|.
1071OPENSSL_EXPORT int X509_REQ_add1_attr_by_NID(X509_REQ *req, int nid,
1072 int attrtype,
1073 const unsigned char *data,
1074 int len);
1075
1076// X509_REQ_add1_attr_by_txt behaves like |X509_REQ_add1_attr_by_OBJ| except the
1077// attribute type is determined by calling |OBJ_txt2obj| with |attrname|.
1078OPENSSL_EXPORT int X509_REQ_add1_attr_by_txt(X509_REQ *req,
1079 const char *attrname, int attrtype,
1080 const unsigned char *data,
1081 int len);
1082
1083// X509_REQ_add_extensions_nid adds an attribute to |req| of type |nid|, to
1084// request the certificate extensions in |exts|. It returns one on success and
1085// zero on error. |nid| should be |NID_ext_req| or |NID_ms_ext_req|.
1086OPENSSL_EXPORT int X509_REQ_add_extensions_nid(
1087 X509_REQ *req, const STACK_OF(X509_EXTENSION) *exts, int nid);
1088
1089// X509_REQ_add_extensions behaves like |X509_REQ_add_extensions_nid|, using the
1090// standard |NID_ext_req| for the attribute type.
1091OPENSSL_EXPORT int X509_REQ_add_extensions(
1092 X509_REQ *req, const STACK_OF(X509_EXTENSION) *exts);
1093
David Benjamin4363bdd2022-04-06 15:19:16 -04001094// X509_REQ_sign signs |req| with |pkey| and replaces the signature algorithm
David Benjamin0c7527b2023-04-25 21:45:00 -04001095// and signature fields. It returns the length of the signature on success and
1096// zero on error. This function uses digest algorithm |md|, or |pkey|'s default
1097// if NULL. Other signing parameters use |pkey|'s defaults. To customize them,
1098// use |X509_REQ_sign_ctx|.
David Benjamin4363bdd2022-04-06 15:19:16 -04001099OPENSSL_EXPORT int X509_REQ_sign(X509_REQ *req, EVP_PKEY *pkey,
1100 const EVP_MD *md);
1101
1102// X509_REQ_sign_ctx signs |req| with |ctx| and replaces the signature algorithm
David Benjamin0c7527b2023-04-25 21:45:00 -04001103// and signature fields. It returns the length of the signature on success and
1104// zero on error. The signature algorithm and parameters come from |ctx|, which
1105// must have been initialized with |EVP_DigestSignInit|. The caller should
1106// configure the corresponding |EVP_PKEY_CTX| before calling this function.
David Benjamin5bef6ec2023-11-13 09:04:43 -05001107//
1108// On success or failure, this function mutates |ctx| and resets it to the empty
1109// state. Caller should not rely on its contents after the function returns.
David Benjamin4363bdd2022-04-06 15:19:16 -04001110OPENSSL_EXPORT int X509_REQ_sign_ctx(X509_REQ *req, EVP_MD_CTX *ctx);
1111
1112// i2d_re_X509_REQ_tbs serializes the CertificationRequestInfo (see RFC 2986)
1113// portion of |req|, as described in |i2d_SAMPLE|.
1114//
1115// This function re-encodes the CertificationRequestInfo and may not reflect
1116// |req|'s original encoding. It may be used to manually generate a signature
1117// for a new certificate request.
1118OPENSSL_EXPORT int i2d_re_X509_REQ_tbs(X509_REQ *req, uint8_t **outp);
1119
1120// X509_REQ_set1_signature_algo sets |req|'s signature algorithm to |algo| and
1121// returns one on success or zero on error.
1122OPENSSL_EXPORT int X509_REQ_set1_signature_algo(X509_REQ *req,
1123 const X509_ALGOR *algo);
1124
1125// X509_REQ_set1_signature_value sets |req|'s signature to a copy of the
1126// |sig_len| bytes pointed by |sig|. It returns one on success and zero on
1127// error.
1128//
1129// Due to a specification error, PKCS#10 certificate requests store signatures
1130// in ASN.1 BIT STRINGs, but signature algorithms return byte strings rather
1131// than bit strings. This function creates a BIT STRING containing a whole
1132// number of bytes, with the bit order matching the DER encoding. This matches
1133// the encoding used by all X.509 signature algorithms.
1134OPENSSL_EXPORT int X509_REQ_set1_signature_value(X509_REQ *req,
1135 const uint8_t *sig,
1136 size_t sig_len);
1137
1138
David Benjaminb6f47e82022-07-09 00:57:54 -04001139// Names.
1140//
1141// An |X509_NAME| represents an X.509 Name structure (RFC 5280). X.509 names are
1142// a complex, hierarchical structure over a collection of attributes. Each name
1143// is sequence of relative distinguished names (RDNs), decreasing in
1144// specificity. For example, the first RDN may specify the country, while the
1145// next RDN may specify a locality. Each RDN is, itself, a set of attributes.
1146// Having more than one attribute in an RDN is uncommon, but possible. Within an
1147// RDN, attributes have the same level in specificity. Attribute types are
1148// OBJECT IDENTIFIERs. This determines the ASN.1 type of the value, which is
1149// commonly a string but may be other types.
1150//
1151// The |X509_NAME| representation flattens this two-level structure into a
1152// single list of attributes. Each attribute is stored in an |X509_NAME_ENTRY|,
1153// with also maintains the index of the RDN it is part of, accessible via
1154// |X509_NAME_ENTRY_set|. This can be used to recover the two-level structure.
1155//
1156// X.509 names are largely vestigial. Historically, DNS names were parsed out of
1157// the subject's common name attribute, but this is deprecated and has since
1158// moved to the subject alternative name extension. In modern usage, X.509 names
1159// are primarily opaque identifiers to link a certificate with its issuer.
1160
1161DEFINE_STACK_OF(X509_NAME_ENTRY)
1162DEFINE_STACK_OF(X509_NAME)
1163
1164// X509_NAME is an |ASN1_ITEM| whose ASN.1 type is X.509 Name (RFC 5280) and C
1165// type is |X509_NAME*|.
1166DECLARE_ASN1_ITEM(X509_NAME)
1167
1168// X509_NAME_new returns a new, empty |X509_NAME_new|, or NULL on
1169// error.
1170OPENSSL_EXPORT X509_NAME *X509_NAME_new(void);
1171
1172// X509_NAME_free releases memory associated with |name|.
1173OPENSSL_EXPORT void X509_NAME_free(X509_NAME *name);
1174
1175// d2i_X509_NAME parses up to |len| bytes from |*inp| as a DER-encoded X.509
David Benjamin19721cd2023-01-16 16:19:03 -05001176// Name (RFC 5280), as described in |d2i_SAMPLE|.
David Benjaminb6f47e82022-07-09 00:57:54 -04001177OPENSSL_EXPORT X509_NAME *d2i_X509_NAME(X509_NAME **out, const uint8_t **inp,
1178 long len);
1179
1180// i2d_X509_NAME marshals |in| as a DER-encoded X.509 Name (RFC 5280), as
1181// described in |i2d_SAMPLE|.
1182//
1183// TODO(https://crbug.com/boringssl/407): This function should be const and
1184// thread-safe but is currently neither in some cases, notably if |in| was
1185// mutated.
1186OPENSSL_EXPORT int i2d_X509_NAME(X509_NAME *in, uint8_t **outp);
1187
1188// X509_NAME_dup returns a newly-allocated copy of |name|, or NULL on error.
1189//
1190// TODO(https://crbug.com/boringssl/407): This function should be const and
1191// thread-safe but is currently neither in some cases, notably if |name| was
1192// mutated.
1193OPENSSL_EXPORT X509_NAME *X509_NAME_dup(X509_NAME *name);
1194
David Benjamina697bcb2023-11-12 19:56:19 -05001195// X509_NAME_cmp compares |a| and |b|'s canonicalized forms. It returns zero if
1196// they are equal, one if |a| sorts after |b|, -1 if |b| sorts after |a|, and -2
1197// on error.
1198//
1199// TODO(https://crbug.com/boringssl/407): This function is const, but it is not
1200// always thread-safe, notably if |name| was mutated.
1201//
1202// TODO(https://crbug.com/boringssl/355): The -2 return is very inconvenient to
1203// pass to a sorting function. Can we make this infallible? In the meantime,
1204// prefer to use this function only for equality checks rather than comparisons.
1205// Although even the library itself passes this to a sorting function.
1206OPENSSL_EXPORT int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b);
1207
David Benjaminb6f47e82022-07-09 00:57:54 -04001208// X509_NAME_get0_der sets |*out_der| and |*out_der_len|
1209//
1210// Avoid this function and prefer |i2d_X509_NAME|. It is one of the reasons
1211// these functions are not consistently thread-safe or const-correct. Depending
1212// on the resolution of https://crbug.com/boringssl/407, this function may be
1213// removed or cause poor performance.
1214OPENSSL_EXPORT int X509_NAME_get0_der(X509_NAME *name, const uint8_t **out_der,
1215 size_t *out_der_len);
1216
1217// X509_NAME_set makes a copy of |name|. On success, it frees |*xn|, sets |*xn|
1218// to the copy, and returns one. Otherwise, it returns zero.
1219//
1220// TODO(https://crbug.com/boringssl/407): This function should be const and
1221// thread-safe but is currently neither in some cases, notably if |name| was
1222// mutated.
1223OPENSSL_EXPORT int X509_NAME_set(X509_NAME **xn, X509_NAME *name);
1224
1225// X509_NAME_entry_count returns the number of entries in |name|.
1226OPENSSL_EXPORT int X509_NAME_entry_count(const X509_NAME *name);
1227
1228// X509_NAME_get_index_by_NID returns the zero-based index of the first
1229// attribute in |name| with type |nid|, or -1 if there is none. |nid| should be
1230// one of the |NID_*| constants. If |lastpos| is non-negative, it begins
1231// searching at |lastpos+1|. To search all attributes, pass in -1, not zero.
1232//
1233// Indices from this function refer to |X509_NAME|'s flattened representation.
1234OPENSSL_EXPORT int X509_NAME_get_index_by_NID(const X509_NAME *name, int nid,
1235 int lastpos);
1236
1237// X509_NAME_get_index_by_OBJ behaves like |X509_NAME_get_index_by_NID| but
1238// looks for attributes with type |obj|.
1239OPENSSL_EXPORT int X509_NAME_get_index_by_OBJ(const X509_NAME *name,
1240 const ASN1_OBJECT *obj,
1241 int lastpos);
1242
1243// X509_NAME_get_entry returns the attribute in |name| at index |loc|, or NULL
1244// if |loc| is out of range. |loc| is interpreted using |X509_NAME|'s flattened
1245// representation. This function returns a non-const pointer for OpenSSL
1246// compatibility, but callers should not mutate the result. Doing so will break
1247// internal invariants in the library.
1248OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_get_entry(const X509_NAME *name,
1249 int loc);
1250
1251// X509_NAME_delete_entry removes and returns the attribute in |name| at index
1252// |loc|, or NULL if |loc| is out of range. |loc| is interpreted using
1253// |X509_NAME|'s flattened representation. If the attribute is found, the caller
1254// is responsible for releasing the result with |X509_NAME_ENTRY_free|.
1255//
1256// This function will internally update RDN indices (see |X509_NAME_ENTRY_set|)
1257// so they continue to be consecutive.
1258OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name,
1259 int loc);
1260
1261// X509_NAME_add_entry adds a copy of |entry| to |name| and returns one on
1262// success or zero on error. If |loc| is -1, the entry is appended to |name|.
1263// Otherwise, it is inserted at index |loc|. If |set| is -1, the entry is added
1264// to the previous entry's RDN. If it is 0, the entry becomes a singleton RDN.
1265// If 1, it is added to next entry's RDN.
1266//
1267// This function will internally update RDN indices (see |X509_NAME_ENTRY_set|)
1268// so they continue to be consecutive.
1269OPENSSL_EXPORT int X509_NAME_add_entry(X509_NAME *name,
1270 const X509_NAME_ENTRY *entry, int loc,
1271 int set);
1272
1273// X509_NAME_add_entry_by_OBJ adds a new entry to |name| and returns one on
1274// success or zero on error. The entry's attribute type is |obj|. The entry's
1275// attribute value is determined by |type|, |bytes|, and |len|, as in
1276// |X509_NAME_ENTRY_set_data|. The entry's position is determined by |loc| and
David Benjamina028a5e2023-02-06 12:22:07 -05001277// |set| as in |X509_NAME_add_entry|.
David Benjaminb6f47e82022-07-09 00:57:54 -04001278OPENSSL_EXPORT int X509_NAME_add_entry_by_OBJ(X509_NAME *name,
1279 const ASN1_OBJECT *obj, int type,
David Benjamin32b51302023-03-25 01:42:18 -04001280 const uint8_t *bytes,
1281 ossl_ssize_t len, int loc,
1282 int set);
David Benjaminb6f47e82022-07-09 00:57:54 -04001283
1284// X509_NAME_add_entry_by_NID behaves like |X509_NAME_add_entry_by_OBJ| but sets
1285// the entry's attribute type to |nid|, which should be one of the |NID_*|
1286// constants.
1287OPENSSL_EXPORT int X509_NAME_add_entry_by_NID(X509_NAME *name, int nid,
1288 int type, const uint8_t *bytes,
David Benjamin32b51302023-03-25 01:42:18 -04001289 ossl_ssize_t len, int loc,
1290 int set);
David Benjaminb6f47e82022-07-09 00:57:54 -04001291
1292// X509_NAME_add_entry_by_txt behaves like |X509_NAME_add_entry_by_OBJ| but sets
1293// the entry's attribute type to |field|, which is passed to |OBJ_txt2obj|.
1294OPENSSL_EXPORT int X509_NAME_add_entry_by_txt(X509_NAME *name,
1295 const char *field, int type,
David Benjamin32b51302023-03-25 01:42:18 -04001296 const uint8_t *bytes,
1297 ossl_ssize_t len, int loc,
1298 int set);
David Benjaminb6f47e82022-07-09 00:57:54 -04001299
David Benjaminb6f47e82022-07-09 00:57:54 -04001300// X509_NAME_ENTRY_new returns a new, empty |X509_NAME_ENTRY_new|, or NULL on
1301// error.
1302OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_new(void);
1303
1304// X509_NAME_ENTRY_free releases memory associated with |entry|.
1305OPENSSL_EXPORT void X509_NAME_ENTRY_free(X509_NAME_ENTRY *entry);
1306
David Benjaminb6f47e82022-07-09 00:57:54 -04001307// X509_NAME_ENTRY_dup returns a newly-allocated copy of |entry|, or NULL on
1308// error.
1309OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_dup(
1310 const X509_NAME_ENTRY *entry);
1311
1312// X509_NAME_ENTRY_get_object returns |entry|'s attribute type. This function
1313// returns a non-const pointer for OpenSSL compatibility, but callers should not
1314// mutate the result. Doing so will break internal invariants in the library.
1315OPENSSL_EXPORT ASN1_OBJECT *X509_NAME_ENTRY_get_object(
1316 const X509_NAME_ENTRY *entry);
1317
1318// X509_NAME_ENTRY_set_object sets |entry|'s attribute type to |obj|. It returns
1319// one on success and zero on error.
1320OPENSSL_EXPORT int X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *entry,
1321 const ASN1_OBJECT *obj);
1322
1323// X509_NAME_ENTRY_get_data returns |entry|'s attribute value, represented as an
1324// |ASN1_STRING|. This value may have any ASN.1 type, so callers must check the
1325// type before interpreting the contents. This function returns a non-const
1326// pointer for OpenSSL compatibility, but callers should not mutate the result.
1327// Doing so will break internal invariants in the library.
1328//
1329// TODO(https://crbug.com/boringssl/412): Although the spec says any ASN.1 type
1330// is allowed, we currently only allow an ad-hoc set of types. Additionally, it
1331// is unclear if some types can even be represented by this function.
1332OPENSSL_EXPORT ASN1_STRING *X509_NAME_ENTRY_get_data(
1333 const X509_NAME_ENTRY *entry);
1334
1335// X509_NAME_ENTRY_set_data sets |entry|'s value to |len| bytes from |bytes|. It
1336// returns one on success and zero on error. If |len| is -1, |bytes| must be a
1337// NUL-terminated C string and the length is determined by |strlen|. |bytes| is
1338// converted to an ASN.1 type as follows:
1339//
1340// If |type| is a |MBSTRING_*| constant, the value is an ASN.1 string. The
1341// string is determined by decoding |bytes| in the encoding specified by |type|,
1342// and then re-encoding it in a form appropriate for |entry|'s attribute type.
1343// See |ASN1_STRING_set_by_NID| for details.
1344//
1345// Otherwise, the value is an |ASN1_STRING| with type |type| and value |bytes|.
1346// See |ASN1_STRING| for how to format ASN.1 types as an |ASN1_STRING|. If
1347// |type| is |V_ASN1_UNDEF| the previous |ASN1_STRING| type is reused.
1348OPENSSL_EXPORT int X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *entry, int type,
David Benjamin32b51302023-03-25 01:42:18 -04001349 const uint8_t *bytes,
1350 ossl_ssize_t len);
David Benjaminb6f47e82022-07-09 00:57:54 -04001351
1352// X509_NAME_ENTRY_set returns the zero-based index of the RDN which contains
1353// |entry|. Consecutive entries with the same index are part of the same RDN.
1354OPENSSL_EXPORT int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *entry);
1355
1356// X509_NAME_ENTRY_create_by_OBJ creates a new |X509_NAME_ENTRY| with attribute
1357// type |obj|. The attribute value is determined from |type|, |bytes|, and |len|
1358// as in |X509_NAME_ENTRY_set_data|. It returns the |X509_NAME_ENTRY| on success
1359// and NULL on error.
1360//
1361// If |out| is non-NULL and |*out| is NULL, it additionally sets |*out| to the
1362// result on success. If both |out| and |*out| are non-NULL, it updates the
1363// object at |*out| instead of allocating a new one.
1364OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(
1365 X509_NAME_ENTRY **out, const ASN1_OBJECT *obj, int type,
David Benjamin32b51302023-03-25 01:42:18 -04001366 const uint8_t *bytes, ossl_ssize_t len);
David Benjaminb6f47e82022-07-09 00:57:54 -04001367
1368// X509_NAME_ENTRY_create_by_NID behaves like |X509_NAME_ENTRY_create_by_OBJ|
1369// except the attribute type is |nid|, which should be one of the |NID_*|
1370// constants.
1371OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(
David Benjamin32b51302023-03-25 01:42:18 -04001372 X509_NAME_ENTRY **out, int nid, int type, const uint8_t *bytes,
1373 ossl_ssize_t len);
David Benjaminb6f47e82022-07-09 00:57:54 -04001374
1375// X509_NAME_ENTRY_create_by_txt behaves like |X509_NAME_ENTRY_create_by_OBJ|
1376// except the attribute type is |field|, which is passed to |OBJ_txt2obj|.
1377OPENSSL_EXPORT X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_txt(
1378 X509_NAME_ENTRY **out, const char *field, int type, const uint8_t *bytes,
David Benjamin32b51302023-03-25 01:42:18 -04001379 ossl_ssize_t len);
David Benjaminb6f47e82022-07-09 00:57:54 -04001380
1381
David Benjamin4e400352023-11-12 19:21:55 -05001382// Public keys.
1383//
1384// X.509 encodes public keys as SubjectPublicKeyInfo (RFC 5280), sometimes
1385// referred to as SPKI. These are represented in this library by |X509_PUBKEY|.
1386
David Benjamin4e400352023-11-12 19:21:55 -05001387// X509_PUBKEY_new returns a newly-allocated, empty |X509_PUBKEY| object, or
1388// NULL on error.
1389OPENSSL_EXPORT X509_PUBKEY *X509_PUBKEY_new(void);
1390
1391// X509_PUBKEY_free releases memory associated with |key|.
1392OPENSSL_EXPORT void X509_PUBKEY_free(X509_PUBKEY *key);
1393
1394// d2i_X509_PUBKEY parses up to |len| bytes from |*inp| as a DER-encoded
1395// SubjectPublicKeyInfo, as described in |d2i_SAMPLE|.
1396OPENSSL_EXPORT X509_PUBKEY *d2i_X509_PUBKEY(X509_PUBKEY **out,
1397 const uint8_t **inp, long len);
1398
1399// i2d_X509_PUBKEY marshals |key| as a DER-encoded SubjectPublicKeyInfo, as
1400// described in |i2d_SAMPLE|.
1401OPENSSL_EXPORT int i2d_X509_PUBKEY(const X509_PUBKEY *key, uint8_t **outp);
1402
1403// X509_PUBKEY_set serializes |pkey| into a newly-allocated |X509_PUBKEY|
1404// structure. On success, it frees |*x| if non-NULL, then sets |*x| to the new
1405// object, and returns one. Otherwise, it returns zero.
1406OPENSSL_EXPORT int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey);
1407
1408// X509_PUBKEY_get decodes the public key in |key| and returns an |EVP_PKEY| on
1409// success, or NULL on error or unrecognized algorithm. The caller must release
1410// the result with |EVP_PKEY_free| when done. The |EVP_PKEY| is cached in |key|,
1411// so callers must not mutate the result.
1412OPENSSL_EXPORT EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key);
1413
1414// X509_PUBKEY_set0_param sets |pub| to a key with AlgorithmIdentifier
1415// determined by |obj|, |param_type|, and |param_value|, and an encoded
1416// public key of |key|. On success, it gives |pub| ownership of all the other
1417// parameters and returns one. Otherwise, it returns zero. |key| must have been
1418// allocated by |OPENSSL_malloc|. |obj| and, if applicable, |param_value| must
1419// not be freed after a successful call, and must have been allocated in a
1420// manner compatible with |ASN1_OBJECT_free| or |ASN1_STRING_free|.
1421//
1422// |obj|, |param_type|, and |param_value| are interpreted as in
1423// |X509_ALGOR_set0|. See |X509_ALGOR_set0| for details.
1424OPENSSL_EXPORT int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *obj,
1425 int param_type, void *param_value,
1426 uint8_t *key, int key_len);
1427
1428// X509_PUBKEY_get0_param outputs fields of |pub| and returns one. If |out_obj|
1429// is not NULL, it sets |*out_obj| to AlgorithmIdentifier's OID. If |out_key|
1430// is not NULL, it sets |*out_key| and |*out_key_len| to the encoded public key.
1431// If |out_alg| is not NULL, it sets |*out_alg| to the AlgorithmIdentifier.
1432//
1433// All pointers outputted by this function are internal to |pub| and must not be
1434// freed by the caller. Additionally, although some outputs are non-const,
1435// callers must not mutate the resulting objects.
1436//
1437// Note: X.509 SubjectPublicKeyInfo structures store the encoded public key as a
1438// BIT STRING. |*out_key| and |*out_key_len| will silently pad the key with zero
1439// bits if |pub| did not contain a whole number of bytes. Use
1440// |X509_PUBKEY_get0_public_key| to preserve this information.
1441OPENSSL_EXPORT int X509_PUBKEY_get0_param(ASN1_OBJECT **out_obj,
1442 const uint8_t **out_key,
1443 int *out_key_len,
1444 X509_ALGOR **out_alg,
1445 X509_PUBKEY *pub);
1446
1447// X509_PUBKEY_get0_public_key returns |pub|'s encoded public key.
1448OPENSSL_EXPORT const ASN1_BIT_STRING *X509_PUBKEY_get0_public_key(
1449 const X509_PUBKEY *pub);
1450
1451
David Benjamin557b80f2022-07-10 19:12:12 -04001452// Extensions.
1453//
1454// X.509 certificates and CRLs may contain a list of extensions (RFC 5280).
1455// Extensions have a type, specified by an object identifier (|ASN1_OBJECT|) and
1456// a byte string value, which should a DER-encoded structure whose type is
1457// determined by the extension type. This library represents extensions with the
1458// |X509_EXTENSION| type.
1459
David Benjamin557b80f2022-07-10 19:12:12 -04001460// X509_EXTENSION_new returns a newly-allocated, empty |X509_EXTENSION| object
1461// or NULL on error.
1462OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_new(void);
1463
1464// X509_EXTENSION_free releases memory associated with |ex|.
1465OPENSSL_EXPORT void X509_EXTENSION_free(X509_EXTENSION *ex);
1466
1467// d2i_X509_EXTENSION parses up to |len| bytes from |*inp| as a DER-encoded
David Benjamin19721cd2023-01-16 16:19:03 -05001468// X.509 Extension (RFC 5280), as described in |d2i_SAMPLE|.
David Benjamin557b80f2022-07-10 19:12:12 -04001469OPENSSL_EXPORT X509_EXTENSION *d2i_X509_EXTENSION(X509_EXTENSION **out,
1470 const uint8_t **inp,
1471 long len);
1472
David Benjamin2e13e362023-03-23 00:22:54 -04001473// i2d_X509_EXTENSION marshals |ex| as a DER-encoded X.509 Extension (RFC
David Benjamin557b80f2022-07-10 19:12:12 -04001474// 5280), as described in |i2d_SAMPLE|.
David Benjamin2e13e362023-03-23 00:22:54 -04001475OPENSSL_EXPORT int i2d_X509_EXTENSION(const X509_EXTENSION *ex, uint8_t **outp);
David Benjamin557b80f2022-07-10 19:12:12 -04001476
1477// X509_EXTENSION_dup returns a newly-allocated copy of |ex|, or NULL on error.
1478// This function works by serializing the structure, so if |ex| is incomplete,
1479// it may fail.
1480OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_dup(const X509_EXTENSION *ex);
1481
1482// X509_EXTENSION_create_by_NID creates a new |X509_EXTENSION| with type |nid|,
1483// value |data|, and critical bit |crit|. It returns an |X509_EXTENSION| on
1484// success, and NULL on error. |nid| should be a |NID_*| constant.
1485//
1486// If |ex| and |*ex| are both non-NULL, |*ex| is used to hold the result,
1487// otherwise a new object is allocated. If |ex| is non-NULL and |*ex| is NULL,
1488// the function sets |*ex| to point to the newly allocated result, in addition
1489// to returning the result.
1490OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_create_by_NID(
1491 X509_EXTENSION **ex, int nid, int crit, const ASN1_OCTET_STRING *data);
1492
1493// X509_EXTENSION_create_by_OBJ behaves like |X509_EXTENSION_create_by_NID|, but
1494// the extension type is determined by an |ASN1_OBJECT|.
1495OPENSSL_EXPORT X509_EXTENSION *X509_EXTENSION_create_by_OBJ(
1496 X509_EXTENSION **ex, const ASN1_OBJECT *obj, int crit,
1497 const ASN1_OCTET_STRING *data);
1498
1499// X509_EXTENSION_get_object returns |ex|'s extension type. This function
1500// returns a non-const pointer for OpenSSL compatibility, but callers should not
1501// mutate the result.
1502OPENSSL_EXPORT ASN1_OBJECT *X509_EXTENSION_get_object(const X509_EXTENSION *ex);
1503
1504// X509_EXTENSION_get_data returns |ne|'s extension value. This function returns
1505// a non-const pointer for OpenSSL compatibility, but callers should not mutate
1506// the result.
1507OPENSSL_EXPORT ASN1_OCTET_STRING *X509_EXTENSION_get_data(
1508 const X509_EXTENSION *ne);
1509
1510// X509_EXTENSION_get_critical returns one if |ex| is critical and zero
1511// otherwise.
1512OPENSSL_EXPORT int X509_EXTENSION_get_critical(const X509_EXTENSION *ex);
1513
1514// X509_EXTENSION_set_object sets |ex|'s extension type to |obj|. It returns one
1515// on success and zero on error.
1516OPENSSL_EXPORT int X509_EXTENSION_set_object(X509_EXTENSION *ex,
1517 const ASN1_OBJECT *obj);
1518
1519// X509_EXTENSION_set_critical sets |ex| to critical if |crit| is non-zero and
1520// to non-critical if |crit| is zero.
1521OPENSSL_EXPORT int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit);
1522
1523// X509_EXTENSION_set_data set's |ex|'s extension value to a copy of |data|. It
1524// returns one on success and zero on error.
1525OPENSSL_EXPORT int X509_EXTENSION_set_data(X509_EXTENSION *ex,
1526 const ASN1_OCTET_STRING *data);
1527
1528
1529// Extension lists.
1530//
1531// The following functions manipulate lists of extensions. Most of them have
1532// corresponding functions on the containing |X509|, |X509_CRL|, or
1533// |X509_REVOKED|.
1534
1535DEFINE_STACK_OF(X509_EXTENSION)
1536typedef STACK_OF(X509_EXTENSION) X509_EXTENSIONS;
1537
David Benjamin557b80f2022-07-10 19:12:12 -04001538// d2i_X509_EXTENSIONS parses up to |len| bytes from |*inp| as a DER-encoded
David Benjamin19721cd2023-01-16 16:19:03 -05001539// SEQUENCE OF Extension (RFC 5280), as described in |d2i_SAMPLE|.
David Benjamin557b80f2022-07-10 19:12:12 -04001540OPENSSL_EXPORT X509_EXTENSIONS *d2i_X509_EXTENSIONS(X509_EXTENSIONS **out,
1541 const uint8_t **inp,
1542 long len);
1543
1544// i2d_X509_EXTENSIONS marshals |alg| as a DER-encoded SEQUENCE OF Extension
1545// (RFC 5280), as described in |i2d_SAMPLE|.
1546OPENSSL_EXPORT int i2d_X509_EXTENSIONS(const X509_EXTENSIONS *alg,
1547 uint8_t **outp);
1548
1549// X509v3_get_ext_count returns the number of extensions in |x|.
1550OPENSSL_EXPORT int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x);
1551
1552// X509v3_get_ext_by_NID returns the index of the first extension in |x| with
1553// type |nid|, or a negative number if not found. If found, callers can use
1554// |X509v3_get_ext| to look up the extension by index.
1555//
1556// If |lastpos| is non-negative, it begins searching at |lastpos| + 1. Callers
1557// can thus loop over all matching extensions by first passing -1 and then
1558// passing the previously-returned value until no match is returned.
1559OPENSSL_EXPORT int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x,
1560 int nid, int lastpos);
1561
1562// X509v3_get_ext_by_OBJ behaves like |X509v3_get_ext_by_NID| but looks for
1563// extensions matching |obj|.
1564OPENSSL_EXPORT int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *x,
1565 const ASN1_OBJECT *obj, int lastpos);
1566
1567// X509v3_get_ext_by_critical returns the index of the first extension in |x|
1568// whose critical bit matches |crit|, or a negative number if no such extension
1569// was found.
1570//
1571// If |lastpos| is non-negative, it begins searching at |lastpos| + 1. Callers
1572// can thus loop over all matching extensions by first passing -1 and then
1573// passing the previously-returned value until no match is returned.
1574OPENSSL_EXPORT int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *x,
1575 int crit, int lastpos);
1576
1577// X509v3_get_ext returns the extension in |x| at index |loc|, or NULL if |loc|
1578// is out of bounds. This function returns a non-const pointer for OpenSSL
1579// compatibility, but callers should not mutate the result.
1580OPENSSL_EXPORT X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x,
1581 int loc);
1582
1583// X509v3_delete_ext removes the extension in |x| at index |loc| and returns the
1584// removed extension, or NULL if |loc| was out of bounds. If an extension was
1585// returned, the caller must release it with |X509_EXTENSION_free|.
1586OPENSSL_EXPORT X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x,
1587 int loc);
1588
1589// X509v3_add_ext adds a copy of |ex| to the extension list in |*x|. If |*x| is
1590// NULL, it allocates a new |STACK_OF(X509_EXTENSION)| to hold the copy and sets
1591// |*x| to the new list. It returns |*x| on success and NULL on error. The
1592// caller retains ownership of |ex| and can release it independently of |*x|.
1593//
1594// The new extension is inserted at index |loc|, shifting extensions to the
1595// right. If |loc| is -1 or out of bounds, the new extension is appended to the
1596// list.
1597OPENSSL_EXPORT STACK_OF(X509_EXTENSION) *X509v3_add_ext(
1598 STACK_OF(X509_EXTENSION) **x, const X509_EXTENSION *ex, int loc);
1599
1600
David Benjamin4363bdd2022-04-06 15:19:16 -04001601// Algorithm identifiers.
1602//
1603// An |X509_ALGOR| represents an AlgorithmIdentifier structure, used in X.509
1604// to represent signature algorithms and public key algorithms.
1605
1606DEFINE_STACK_OF(X509_ALGOR)
David Benjamin28d72522021-08-08 17:01:10 -04001607
David Benjamin28127932022-06-25 21:30:08 -04001608// X509_ALGOR is an |ASN1_ITEM| whose ASN.1 type is AlgorithmIdentifier and C
1609// type is |X509_ALGOR*|.
1610DECLARE_ASN1_ITEM(X509_ALGOR)
1611
1612// X509_ALGOR_new returns a newly-allocated, empty |X509_ALGOR| object, or NULL
1613// on error.
1614OPENSSL_EXPORT X509_ALGOR *X509_ALGOR_new(void);
1615
David Benjaminb8596422022-07-10 01:08:07 -04001616// X509_ALGOR_dup returns a newly-allocated copy of |alg|, or NULL on error.
1617// This function works by serializing the structure, so if |alg| is incomplete,
1618// it may fail.
1619OPENSSL_EXPORT X509_ALGOR *X509_ALGOR_dup(const X509_ALGOR *alg);
1620
David Benjamin28127932022-06-25 21:30:08 -04001621// X509_ALGOR_free releases memory associated with |alg|.
1622OPENSSL_EXPORT void X509_ALGOR_free(X509_ALGOR *alg);
1623
1624// d2i_X509_ALGOR parses up to |len| bytes from |*inp| as a DER-encoded
David Benjamin19721cd2023-01-16 16:19:03 -05001625// AlgorithmIdentifier, as described in |d2i_SAMPLE|.
David Benjamin28127932022-06-25 21:30:08 -04001626OPENSSL_EXPORT X509_ALGOR *d2i_X509_ALGOR(X509_ALGOR **out, const uint8_t **inp,
1627 long len);
1628
1629// i2d_X509_ALGOR marshals |alg| as a DER-encoded AlgorithmIdentifier, as
1630// described in |i2d_SAMPLE|.
1631OPENSSL_EXPORT int i2d_X509_ALGOR(const X509_ALGOR *alg, uint8_t **outp);
David Benjamin28d72522021-08-08 17:01:10 -04001632
David Benjamin4363bdd2022-04-06 15:19:16 -04001633// X509_ALGOR_set0 sets |alg| to an AlgorithmIdentifier with algorithm |obj| and
1634// parameter determined by |param_type| and |param_value|. It returns one on
1635// success and zero on error. This function takes ownership of |obj| and
1636// |param_value| on success.
1637//
1638// If |param_type| is |V_ASN1_UNDEF|, the parameter is omitted. If |param_type|
1639// is zero, the parameter is left unchanged. Otherwise, |param_type| and
1640// |param_value| are interpreted as in |ASN1_TYPE_set|.
1641//
1642// Note omitting the parameter (|V_ASN1_UNDEF|) and encoding an explicit NULL
1643// value (|V_ASN1_NULL|) are different. Some algorithms require one and some the
1644// other. Consult the relevant specification before calling this function. The
1645// correct parameter for an RSASSA-PKCS1-v1_5 signature is |V_ASN1_NULL|. The
1646// correct one for an ECDSA or Ed25519 signature is |V_ASN1_UNDEF|.
1647OPENSSL_EXPORT int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *obj,
1648 int param_type, void *param_value);
1649
1650// X509_ALGOR_get0 sets |*out_obj| to the |alg|'s algorithm. If |alg|'s
1651// parameter is omitted, it sets |*out_param_type| and |*out_param_value| to
1652// |V_ASN1_UNDEF| and NULL. Otherwise, it sets |*out_param_type| and
1653// |*out_param_value| to the parameter, using the same representation as
1654// |ASN1_TYPE_set0|. See |ASN1_TYPE_set0| and |ASN1_TYPE| for details.
1655//
1656// Callers that require the parameter in serialized form should, after checking
1657// for |V_ASN1_UNDEF|, use |ASN1_TYPE_set1| and |d2i_ASN1_TYPE|, rather than
1658// inspecting |*out_param_value|.
1659//
1660// Each of |out_obj|, |out_param_type|, and |out_param_value| may be NULL to
1661// ignore the output. If |out_param_type| is NULL, |out_param_value| is ignored.
1662//
1663// WARNING: If |*out_param_type| is set to |V_ASN1_UNDEF|, OpenSSL and older
1664// revisions of BoringSSL leave |*out_param_value| unset rather than setting it
1665// to NULL. Callers that support both OpenSSL and BoringSSL should not assume
1666// |*out_param_value| is uniformly initialized.
1667OPENSSL_EXPORT void X509_ALGOR_get0(const ASN1_OBJECT **out_obj,
1668 int *out_param_type,
1669 const void **out_param_value,
1670 const X509_ALGOR *alg);
1671
1672// X509_ALGOR_set_md sets |alg| to the hash function |md|. Note this
1673// AlgorithmIdentifier represents the hash function itself, not a signature
1674// algorithm that uses |md|.
1675OPENSSL_EXPORT void X509_ALGOR_set_md(X509_ALGOR *alg, const EVP_MD *md);
1676
1677// X509_ALGOR_cmp returns zero if |a| and |b| are equal, and some non-zero value
1678// otherwise. Note this function can only be used for equality checks, not an
1679// ordering.
1680OPENSSL_EXPORT int X509_ALGOR_cmp(const X509_ALGOR *a, const X509_ALGOR *b);
1681
1682
David Benjamin787713b2023-04-19 10:28:13 -04001683// Attributes.
1684//
1685// Unlike certificates and CRLs, CSRs use a separate Attribute structure (RFC
1686// 2985, RFC 2986) for extensibility. This is represented by the library as
1687// |X509_ATTRIBUTE|.
1688
1689DEFINE_STACK_OF(X509_ATTRIBUTE)
1690
David Benjamin787713b2023-04-19 10:28:13 -04001691// X509_ATTRIBUTE_new returns a newly-allocated, empty |X509_ATTRIBUTE| object,
1692// or NULL on error. |X509_ATTRIBUTE_set1_*| may be used to finish initializing
1693// it.
1694OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_new(void);
1695
1696// X509_ATTRIBUTE_dup returns a newly-allocated copy of |attr|, or NULL on
1697// error. This function works by serializing the structure, so if |attr| is
1698// incomplete, it may fail.
1699OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_dup(const X509_ATTRIBUTE *attr);
1700
1701// X509_ATTRIBUTE_free releases memory associated with |attr|.
1702OPENSSL_EXPORT void X509_ATTRIBUTE_free(X509_ATTRIBUTE *attr);
1703
1704// d2i_X509_ATTRIBUTE parses up to |len| bytes from |*inp| as a DER-encoded
1705// Attribute (RFC 2986), as described in |d2i_SAMPLE|.
1706OPENSSL_EXPORT X509_ATTRIBUTE *d2i_X509_ATTRIBUTE(X509_ATTRIBUTE **out,
1707 const uint8_t **inp,
1708 long len);
1709
1710// i2d_X509_ATTRIBUTE marshals |alg| as a DER-encoded Attribute (RFC 2986), as
1711// described in |i2d_SAMPLE|.
1712OPENSSL_EXPORT int i2d_X509_ATTRIBUTE(const X509_ATTRIBUTE *alg,
1713 uint8_t **outp);
1714
1715// X509_ATTRIBUTE_create returns a newly-allocated |X509_ATTRIBUTE|, or NULL on
1716// error. The attribute has type |nid| and contains a single value determined by
1717// |attrtype| and |value|, which are interpreted as in |ASN1_TYPE_set|. Note
1718// this function takes ownership of |value|.
1719OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int attrtype,
1720 void *value);
1721
1722// X509_ATTRIBUTE_create_by_NID returns a newly-allocated |X509_ATTRIBUTE| of
1723// type |nid|, or NULL on error. The value is determined as in
1724// |X509_ATTRIBUTE_set1_data|.
1725//
1726// If |attr| is non-NULL, the resulting |X509_ATTRIBUTE| is also written to
1727// |*attr|. If |*attr| was non-NULL when the function was called, |*attr| is
1728// reused instead of creating a new object.
1729//
1730// WARNING: The interpretation of |attrtype|, |data|, and |len| is complex and
1731// error-prone. See |X509_ATTRIBUTE_set1_data| for details.
1732//
1733// WARNING: The object reuse form is deprecated and may be removed in the
1734// future. It also currently incorrectly appends to the reused object's value
1735// set rather than overwriting it.
1736OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(
1737 X509_ATTRIBUTE **attr, int nid, int attrtype, const void *data, int len);
1738
1739// X509_ATTRIBUTE_create_by_OBJ behaves like |X509_ATTRIBUTE_create_by_NID|
1740// except the attribute's type is determined by |obj|.
1741OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(
1742 X509_ATTRIBUTE **attr, const ASN1_OBJECT *obj, int attrtype,
1743 const void *data, int len);
1744
1745// X509_ATTRIBUTE_create_by_txt behaves like |X509_ATTRIBUTE_create_by_NID|
1746// except the attribute's type is determined by calling |OBJ_txt2obj| with
1747// |attrname|.
1748OPENSSL_EXPORT X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(
1749 X509_ATTRIBUTE **attr, const char *attrname, int type,
1750 const unsigned char *bytes, int len);
1751
1752// X509_ATTRIBUTE_set1_object sets |attr|'s type to |obj|. It returns one on
1753// success and zero on error.
1754OPENSSL_EXPORT int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr,
1755 const ASN1_OBJECT *obj);
1756
1757// X509_ATTRIBUTE_set1_data appends a value to |attr|'s value set and returns
1758// one on success or zero on error. The value is determined as follows:
1759//
1760// If |attrtype| is a |MBSTRING_*| constant, the value is an ASN.1 string. The
1761// string is determined by decoding |len| bytes from |data| in the encoding
1762// specified by |attrtype|, and then re-encoding it in a form appropriate for
1763// |attr|'s type. If |len| is -1, |strlen(data)| is used instead. See
1764// |ASN1_STRING_set_by_NID| for details.
1765//
1766// Otherwise, if |len| is not -1, the value is an ASN.1 string. |attrtype| is an
1767// |ASN1_STRING| type value and the |len| bytes from |data| are copied as the
1768// type-specific representation of |ASN1_STRING|. See |ASN1_STRING| for details.
1769//
1770// WARNING: If this form is used to construct a negative INTEGER or ENUMERATED,
1771// |attrtype| includes the |V_ASN1_NEG| flag for |ASN1_STRING|, but the function
1772// forgets to clear the flag for |ASN1_TYPE|. This matches OpenSSL but is
1773// probably a bug. For now, do not use this form with negative values.
1774//
1775// Otherwise, if |len| is -1, the value is constructed by passing |attrtype| and
1776// |data| to |ASN1_TYPE_set1|. That is, |attrtype| is an |ASN1_TYPE| type value,
1777// and |data| is cast to the corresponding pointer type.
1778//
1779// WARNING: Despite the name, this function appends to |attr|'s value set,
1780// rather than overwriting it. To overwrite the value set, create a new
1781// |X509_ATTRIBUTE| with |X509_ATTRIBUTE_new|.
1782//
1783// WARNING: If using the |MBSTRING_*| form, pass a length rather than relying on
1784// |strlen|. In particular, |strlen| will not behave correctly if the input is
1785// |MBSTRING_BMP| or |MBSTRING_UNIV|.
1786//
1787// WARNING: This function currently misinterprets |V_ASN1_OTHER| as an
1788// |MBSTRING_*| constant. This matches OpenSSL but means it is impossible to
1789// construct a value with a non-universal tag.
1790OPENSSL_EXPORT int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,
1791 const void *data, int len);
1792
1793// X509_ATTRIBUTE_get0_data returns the |idx|th value of |attr| in a
1794// type-specific representation to |attrtype|, or NULL if out of bounds or the
1795// type does not match. |attrtype| is one of the type values in |ASN1_TYPE|. On
1796// match, the return value uses the same representation as |ASN1_TYPE_set0|. See
1797// |ASN1_TYPE| for details.
1798OPENSSL_EXPORT void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx,
1799 int attrtype, void *unused);
1800
1801// X509_ATTRIBUTE_count returns the number of values in |attr|.
1802OPENSSL_EXPORT int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr);
1803
1804// X509_ATTRIBUTE_get0_object returns the type of |attr|.
1805OPENSSL_EXPORT ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr);
1806
1807// X509_ATTRIBUTE_get0_type returns the |idx|th value in |attr|, or NULL if out
1808// of bounds. Note this function returns one of |attr|'s values, not the type.
1809OPENSSL_EXPORT ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr,
1810 int idx);
1811
David Benjamin787713b2023-04-19 10:28:13 -04001812
David Benjamind206f3d2023-04-20 00:39:19 -04001813// SignedPublicKeyAndChallenge structures.
1814//
1815// The SignedPublicKeyAndChallenge (SPKAC) is a legacy structure to request
1816// certificates, primarily in the legacy <keygen> HTML tag. An SPKAC structure
1817// is represented by a |NETSCAPE_SPKI| structure.
1818//
1819// The structure is described in
1820// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/keygen
1821
1822// A Netscape_spki_st, or |NETSCAPE_SPKI|, represents a
1823// SignedPublicKeyAndChallenge structure. Although this structure contains a
1824// |spkac| field of type |NETSCAPE_SPKAC|, these are misnamed. The SPKAC is the
1825// entire structure, not the signed portion.
1826struct Netscape_spki_st {
1827 NETSCAPE_SPKAC *spkac;
1828 X509_ALGOR *sig_algor;
1829 ASN1_BIT_STRING *signature;
1830} /* NETSCAPE_SPKI */;
1831
David Benjamind206f3d2023-04-20 00:39:19 -04001832// NETSCAPE_SPKI_new returns a newly-allocated, empty |NETSCAPE_SPKI| object, or
1833// NULL on error.
1834OPENSSL_EXPORT NETSCAPE_SPKI *NETSCAPE_SPKI_new(void);
1835
1836// NETSCAPE_SPKI_free releases memory associated with |spki|.
1837OPENSSL_EXPORT void NETSCAPE_SPKI_free(NETSCAPE_SPKI *spki);
1838
1839// d2i_NETSCAPE_SPKI parses up to |len| bytes from |*inp| as a DER-encoded
1840// SignedPublicKeyAndChallenge structure, as described in |d2i_SAMPLE|.
1841OPENSSL_EXPORT NETSCAPE_SPKI *d2i_NETSCAPE_SPKI(NETSCAPE_SPKI **out,
1842 const uint8_t **inp, long len);
1843
1844// i2d_NETSCAPE_SPKI marshals |spki| as a DER-encoded
1845// SignedPublicKeyAndChallenge structure, as described in |i2d_SAMPLE|.
1846OPENSSL_EXPORT int i2d_NETSCAPE_SPKI(const NETSCAPE_SPKI *spki, uint8_t **outp);
1847
1848// NETSCAPE_SPKI_verify checks that |spki| has a valid signature by |pkey|. It
1849// returns one if the signature is valid and zero otherwise.
1850OPENSSL_EXPORT int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *spki, EVP_PKEY *pkey);
1851
1852// NETSCAPE_SPKI_b64_decode decodes |len| bytes from |str| as a base64-encoded
1853// SignedPublicKeyAndChallenge structure. It returns a newly-allocated
1854// |NETSCAPE_SPKI| structure with the result, or NULL on error. If |len| is 0 or
1855// negative, the length is calculated with |strlen| and |str| must be a
1856// NUL-terminated C string.
1857OPENSSL_EXPORT NETSCAPE_SPKI *NETSCAPE_SPKI_b64_decode(const char *str,
1858 ossl_ssize_t len);
1859
1860// NETSCAPE_SPKI_b64_encode encodes |spki| as a base64-encoded
1861// SignedPublicKeyAndChallenge structure. It returns a newly-allocated
1862// NUL-terminated C string with the result, or NULL on error. The caller must
1863// release the memory with |OPENSSL_free| when done.
1864OPENSSL_EXPORT char *NETSCAPE_SPKI_b64_encode(NETSCAPE_SPKI *spki);
1865
1866// NETSCAPE_SPKI_get_pubkey decodes and returns the public key in |spki| as an
1867// |EVP_PKEY|, or NULL on error. The caller takes ownership of the resulting
1868// pointer and must call |EVP_PKEY_free| when done.
1869OPENSSL_EXPORT EVP_PKEY *NETSCAPE_SPKI_get_pubkey(NETSCAPE_SPKI *spki);
1870
1871// NETSCAPE_SPKI_set_pubkey sets |spki|'s public key to |pkey|. It returns one
1872// on success or zero on error. This function does not take ownership of |pkey|,
1873// so the caller may continue to manage its lifetime independently of |spki|.
1874OPENSSL_EXPORT int NETSCAPE_SPKI_set_pubkey(NETSCAPE_SPKI *spki,
1875 EVP_PKEY *pkey);
1876
1877// NETSCAPE_SPKI_sign signs |spki| with |pkey| and replaces the signature
1878// algorithm and signature fields. It returns the length of the signature on
1879// success and zero on error. This function uses digest algorithm |md|, or
1880// |pkey|'s default if NULL. Other signing parameters use |pkey|'s defaults.
1881OPENSSL_EXPORT int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *spki, EVP_PKEY *pkey,
1882 const EVP_MD *md);
1883
1884// A Netscape_spkac_st, or |NETSCAPE_SPKAC|, represents a PublicKeyAndChallenge
1885// structure. This type is misnamed. The full SPKAC includes the signature,
1886// which is represented with the |NETSCAPE_SPKI| type.
1887struct Netscape_spkac_st {
1888 X509_PUBKEY *pubkey;
1889 ASN1_IA5STRING *challenge;
1890} /* NETSCAPE_SPKAC */;
1891
David Benjamind206f3d2023-04-20 00:39:19 -04001892// NETSCAPE_SPKAC_new returns a newly-allocated, empty |NETSCAPE_SPKAC| object,
1893// or NULL on error.
1894OPENSSL_EXPORT NETSCAPE_SPKAC *NETSCAPE_SPKAC_new(void);
1895
1896// NETSCAPE_SPKAC_free releases memory associated with |spkac|.
1897OPENSSL_EXPORT void NETSCAPE_SPKAC_free(NETSCAPE_SPKAC *spkac);
1898
1899// d2i_NETSCAPE_SPKAC parses up to |len| bytes from |*inp| as a DER-encoded
1900// PublicKeyAndChallenge structure, as described in |d2i_SAMPLE|.
1901OPENSSL_EXPORT NETSCAPE_SPKAC *d2i_NETSCAPE_SPKAC(NETSCAPE_SPKAC **out,
1902 const uint8_t **inp,
1903 long len);
1904
1905// i2d_NETSCAPE_SPKAC marshals |spkac| as a DER-encoded PublicKeyAndChallenge
1906// structure, as described in |i2d_SAMPLE|.
1907OPENSSL_EXPORT int i2d_NETSCAPE_SPKAC(const NETSCAPE_SPKAC *spkac,
1908 uint8_t **outp);
1909
1910
David Benjamin5d1c6122023-11-12 11:33:40 -05001911// RSASSA-PSS Parameters.
1912//
1913// In X.509, RSASSA-PSS signatures and keys use a complex parameter structure,
1914// defined in RFC 4055. The following functions are provided for compatibility
1915// with some OpenSSL APIs relating to this. Use of RSASSA-PSS in X.509 is
1916// discouraged. The parameters structure is very complex, and it takes more
1917// bytes to merely encode parameters than an entire P-256 ECDSA signature.
1918
David Benjamin09febb32023-11-21 00:14:22 -05001919// An rsa_pss_params_st, aka |RSA_PSS_PARAMS|, represents a parsed
1920// RSASSA-PSS-params structure, as defined in (RFC 4055).
David Benjamin5d1c6122023-11-12 11:33:40 -05001921struct rsa_pss_params_st {
1922 X509_ALGOR *hashAlgorithm;
1923 X509_ALGOR *maskGenAlgorithm;
1924 ASN1_INTEGER *saltLength;
1925 ASN1_INTEGER *trailerField;
1926 // OpenSSL caches the MGF hash on |RSA_PSS_PARAMS| in some cases. None of the
1927 // cases apply to BoringSSL, so this is always NULL, but Node expects the
1928 // field to be present.
1929 X509_ALGOR *maskHash;
1930} /* RSA_PSS_PARAMS */;
1931
1932// RSA_PSS_PARAMS is an |ASN1_ITEM| whose ASN.1 type is RSASSA-PSS-params (RFC
1933// 4055) and C type is |RSA_PSS_PARAMS*|.
1934DECLARE_ASN1_ITEM(RSA_PSS_PARAMS)
1935
1936// RSA_PSS_PARAMS_new returns a new, empty |RSA_PSS_PARAMS|, or NULL on error.
1937OPENSSL_EXPORT RSA_PSS_PARAMS *RSA_PSS_PARAMS_new(void);
1938
1939// RSA_PSS_PARAMS_free releases memory associated with |params|.
1940OPENSSL_EXPORT void RSA_PSS_PARAMS_free(RSA_PSS_PARAMS *params);
1941
1942// d2i_RSA_PSS_PARAMS parses up to |len| bytes from |*inp| as a DER-encoded
1943// RSASSA-PSS-params (RFC 4055), as described in |d2i_SAMPLE|.
1944OPENSSL_EXPORT RSA_PSS_PARAMS *d2i_RSA_PSS_PARAMS(RSA_PSS_PARAMS **out,
1945 const uint8_t **inp,
1946 long len);
1947
1948// i2d_RSA_PSS_PARAMS marshals |in| as a DER-encoded RSASSA-PSS-params (RFC
1949// 4055), as described in |i2d_SAMPLE|.
1950OPENSSL_EXPORT int i2d_RSA_PSS_PARAMS(const RSA_PSS_PARAMS *in, uint8_t **outp);
1951
1952
David Benjamin2a63b902023-11-12 19:42:41 -05001953// PKCS#8 private keys.
1954//
1955// The |PKCS8_PRIV_KEY_INFO| type represents a PKCS#8 PrivateKeyInfo (RFC 5208)
1956// structure. This is analogous to SubjectPublicKeyInfo and uses the same
1957// AlgorithmIdentifiers, but carries private keys and is not part of X.509
1958// itself.
1959//
1960// TODO(davidben): Do these functions really belong in this header?
1961
David Benjamin2a63b902023-11-12 19:42:41 -05001962// PKCS8_PRIV_KEY_INFO_new returns a newly-allocated, empty
1963// |PKCS8_PRIV_KEY_INFO| object, or NULL on error.
1964OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *PKCS8_PRIV_KEY_INFO_new(void);
1965
1966// PKCS8_PRIV_KEY_INFO_free releases memory associated with |key|.
1967OPENSSL_EXPORT void PKCS8_PRIV_KEY_INFO_free(PKCS8_PRIV_KEY_INFO *key);
1968
1969// d2i_PKCS8_PRIV_KEY_INFO parses up to |len| bytes from |*inp| as a DER-encoded
1970// PrivateKeyInfo, as described in |d2i_SAMPLE|.
1971OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO(
1972 PKCS8_PRIV_KEY_INFO **out, const uint8_t **inp, long len);
1973
1974// i2d_PKCS8_PRIV_KEY_INFO marshals |key| as a DER-encoded PrivateKeyInfo, as
1975// described in |i2d_SAMPLE|.
1976OPENSSL_EXPORT int i2d_PKCS8_PRIV_KEY_INFO(const PKCS8_PRIV_KEY_INFO *key,
1977 uint8_t **outp);
1978
1979// EVP_PKCS82PKEY returns |p8| as a newly-allocated |EVP_PKEY|, or NULL if the
1980// key was unsupported or could not be decoded. The caller must release the
1981// result with |EVP_PKEY_free| when done.
1982//
1983// Use |EVP_parse_private_key| instead.
1984OPENSSL_EXPORT EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8);
1985
1986// EVP_PKEY2PKCS8 encodes |pkey| as a PKCS#8 PrivateKeyInfo (RFC 5208),
1987// represented as a newly-allocated |PKCS8_PRIV_KEY_INFO|, or NULL on error. The
1988// caller must release the result with |PKCS8_PRIV_KEY_INFO_free| when done.
1989//
1990// Use |EVP_marshal_private_key| instead.
1991OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(const EVP_PKEY *pkey);
1992
1993
1994// Algorithm and octet string pairs.
1995//
1996// The |X509_SIG| type represents an ASN.1 SEQUENCE type of an
1997// AlgorithmIdentifier and an OCTET STRING. Although named |X509_SIG|, there is
1998// no type in X.509 which matches this format. The two common types which do are
1999// DigestInfo (RFC 2315 and RFC 8017), and EncryptedPrivateKeyInfo (RFC 5208).
2000
David Benjamin2a63b902023-11-12 19:42:41 -05002001// X509_SIG_new returns a newly-allocated, empty |X509_SIG| object, or NULL on
2002// error.
2003OPENSSL_EXPORT X509_SIG *X509_SIG_new(void);
2004
2005// X509_SIG_free releases memory associated with |key|.
2006OPENSSL_EXPORT void X509_SIG_free(X509_SIG *key);
2007
2008// d2i_X509_SIG parses up to |len| bytes from |*inp| as a DER-encoded algorithm
2009// and octet string pair, as described in |d2i_SAMPLE|.
2010OPENSSL_EXPORT X509_SIG *d2i_X509_SIG(X509_SIG **out, const uint8_t **inp,
2011 long len);
2012
2013// i2d_X509_SIG marshals |sig| as a DER-encoded algorithm
2014// and octet string pair, as described in |i2d_SAMPLE|.
2015OPENSSL_EXPORT int i2d_X509_SIG(const X509_SIG *sig, uint8_t **outp);
2016
2017// X509_SIG_get0 sets |*out_alg| and |*out_digest| to non-owning pointers to
2018// |sig|'s algorithm and digest fields, respectively. Either |out_alg| and
2019// |out_digest| may be NULL to skip those fields.
2020OPENSSL_EXPORT void X509_SIG_get0(const X509_SIG *sig,
2021 const X509_ALGOR **out_alg,
2022 const ASN1_OCTET_STRING **out_digest);
2023
2024// X509_SIG_getm behaves like |X509_SIG_get0| but returns mutable pointers.
2025OPENSSL_EXPORT void X509_SIG_getm(X509_SIG *sig, X509_ALGOR **out_alg,
2026 ASN1_OCTET_STRING **out_digest);
2027
2028
David Benjamin4363bdd2022-04-06 15:19:16 -04002029// Printing functions.
2030//
2031// The following functions output human-readable representations of
2032// X.509-related structures. They should only be used for debugging or logging
David Benjamind9f209b2023-04-19 01:08:04 -04002033// and not parsed programmatically. In many cases, the outputs are ambiguous, so
2034// attempting to parse them can lead to string injection vulnerabilities.
2035
David Benjamin2d7adae2023-11-21 00:11:09 -05002036// The following flags control |X509_print_ex| and |X509_REQ_print_ex|. These
2037// flags co-exist with |X509V3_EXT_*|, so avoid collisions when adding new ones.
David Benjamind9f209b2023-04-19 01:08:04 -04002038
2039// X509_FLAG_COMPAT disables all flags. It additionally causes names to be
2040// printed with a 16-byte indent.
2041#define X509_FLAG_COMPAT 0
2042
2043// X509_FLAG_NO_HEADER skips a header identifying the type of object printed.
2044#define X509_FLAG_NO_HEADER 1L
2045
2046// X509_FLAG_NO_VERSION skips printing the X.509 version number.
2047#define X509_FLAG_NO_VERSION (1L << 1)
2048
2049// X509_FLAG_NO_SERIAL skips printing the serial number. It is ignored in
2050// |X509_REQ_print_fp|.
2051#define X509_FLAG_NO_SERIAL (1L << 2)
2052
2053// X509_FLAG_NO_SIGNAME skips printing the signature algorithm in the
2054// TBSCertificate. It is ignored in |X509_REQ_print_fp|.
2055#define X509_FLAG_NO_SIGNAME (1L << 3)
2056
2057// X509_FLAG_NO_ISSUER skips printing the issuer.
2058#define X509_FLAG_NO_ISSUER (1L << 4)
2059
David Benjamind206f3d2023-04-20 00:39:19 -04002060// X509_FLAG_NO_VALIDITY skips printing the notBefore and notAfter times. It is
David Benjamind9f209b2023-04-19 01:08:04 -04002061// ignored in |X509_REQ_print_fp|.
2062#define X509_FLAG_NO_VALIDITY (1L << 5)
2063
David Benjamind206f3d2023-04-20 00:39:19 -04002064// X509_FLAG_NO_SUBJECT skips printing the subject.
David Benjamind9f209b2023-04-19 01:08:04 -04002065#define X509_FLAG_NO_SUBJECT (1L << 6)
2066
2067// X509_FLAG_NO_PUBKEY skips printing the public key.
2068#define X509_FLAG_NO_PUBKEY (1L << 7)
2069
2070// X509_FLAG_NO_EXTENSIONS skips printing the extension list. It is ignored in
2071// |X509_REQ_print_fp|. CSRs instead have attributes, which is controlled by
2072// |X509_FLAG_NO_ATTRIBUTES|.
2073#define X509_FLAG_NO_EXTENSIONS (1L << 8)
2074
2075// X509_FLAG_NO_SIGDUMP skips printing the signature and outer signature
2076// algorithm.
2077#define X509_FLAG_NO_SIGDUMP (1L << 9)
2078
2079// X509_FLAG_NO_AUX skips printing auxiliary properties. (See |d2i_X509_AUX| and
2080// related functions.)
2081#define X509_FLAG_NO_AUX (1L << 10)
2082
2083// X509_FLAG_NO_ATTRIBUTES skips printing CSR attributes. It does nothing for
2084// certificates and CRLs.
2085#define X509_FLAG_NO_ATTRIBUTES (1L << 11)
2086
2087// X509_FLAG_NO_IDS skips printing the issuerUniqueID and subjectUniqueID in a
2088// certificate. It is ignored in |X509_REQ_print_fp|.
2089#define X509_FLAG_NO_IDS (1L << 12)
2090
David Benjamin2d7adae2023-11-21 00:11:09 -05002091// The following flags control |X509_print_ex|, |X509_REQ_print_ex|,
2092// |X509V3_EXT_print|, and |X509V3_extensions_print|. These flags coexist with
2093// |X509_FLAG_*|, so avoid collisions when adding new ones.
2094
2095// X509V3_EXT_UNKNOWN_MASK is a mask that determines how unknown extensions are
2096// processed.
2097#define X509V3_EXT_UNKNOWN_MASK (0xfL << 16)
2098
2099// X509V3_EXT_DEFAULT causes unknown extensions or syntax errors to return
2100// failure.
2101#define X509V3_EXT_DEFAULT 0
2102
2103// X509V3_EXT_ERROR_UNKNOWN causes unknown extensions or syntax errors to print
2104// as "<Not Supported>" or "<Parse Error>", respectively.
2105#define X509V3_EXT_ERROR_UNKNOWN (1L << 16)
2106
2107// X509V3_EXT_PARSE_UNKNOWN is deprecated and behaves like
2108// |X509V3_EXT_DUMP_UNKNOWN|.
2109#define X509V3_EXT_PARSE_UNKNOWN (2L << 16)
2110
2111// X509V3_EXT_DUMP_UNKNOWN causes unknown extensions to be displayed as a
2112// hexdump.
2113#define X509V3_EXT_DUMP_UNKNOWN (3L << 16)
2114
David Benjamind9f209b2023-04-19 01:08:04 -04002115// X509_print_ex writes a human-readable representation of |x| to |bp|. It
2116// returns one on success and zero on error. |nmflags| is the flags parameter
2117// for |X509_NAME_print_ex| when printing the subject and issuer. |cflag| should
David Benjamin2d7adae2023-11-21 00:11:09 -05002118// be some combination of the |X509_FLAG_*| and |X509V3_EXT_*| constants.
David Benjamind9f209b2023-04-19 01:08:04 -04002119OPENSSL_EXPORT int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflag,
2120 unsigned long cflag);
2121
2122// X509_print_ex_fp behaves like |X509_print_ex| but writes to |fp|.
2123OPENSSL_EXPORT int X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag,
2124 unsigned long cflag);
2125
2126// X509_print calls |X509_print_ex| with |XN_FLAG_COMPAT| and |X509_FLAG_COMPAT|
2127// flags.
2128OPENSSL_EXPORT int X509_print(BIO *bp, X509 *x);
2129
2130// X509_print_fp behaves like |X509_print| but writes to |fp|.
2131OPENSSL_EXPORT int X509_print_fp(FILE *fp, X509 *x);
2132
2133// X509_CRL_print writes a human-readable representation of |x| to |bp|. It
2134// returns one on success and zero on error.
2135OPENSSL_EXPORT int X509_CRL_print(BIO *bp, X509_CRL *x);
2136
2137// X509_CRL_print_fp behaves like |X509_CRL_print| but writes to |fp|.
2138OPENSSL_EXPORT int X509_CRL_print_fp(FILE *fp, X509_CRL *x);
2139
2140// X509_REQ_print_ex writes a human-readable representation of |x| to |bp|. It
2141// returns one on success and zero on error. |nmflags| is the flags parameter
2142// for |X509_NAME_print_ex|, when printing the subject. |cflag| should be some
David Benjamin2d7adae2023-11-21 00:11:09 -05002143// combination of the |X509_FLAG_*| and |X509V3_EXT_*| constants.
David Benjamind9f209b2023-04-19 01:08:04 -04002144OPENSSL_EXPORT int X509_REQ_print_ex(BIO *bp, X509_REQ *x, unsigned long nmflag,
2145 unsigned long cflag);
2146
2147// X509_REQ_print calls |X509_REQ_print_ex| with |XN_FLAG_COMPAT| and
2148// |X509_FLAG_COMPAT| flags.
2149OPENSSL_EXPORT int X509_REQ_print(BIO *bp, X509_REQ *req);
2150
2151// X509_REQ_print_fp behaves like |X509_REQ_print| but writes to |fp|.
2152OPENSSL_EXPORT int X509_REQ_print_fp(FILE *fp, X509_REQ *req);
2153
2154// The following flags are control |X509_NAME_print_ex|. They must not collide
2155// with |ASN1_STRFLGS_*|.
2156//
2157// TODO(davidben): This is far, far too many options and most of them are
2158// useless. Trim this down.
2159
2160// XN_FLAG_COMPAT prints with |X509_NAME_print|'s format and return value
2161// convention.
2162#define XN_FLAG_COMPAT 0
2163
2164// XN_FLAG_SEP_MASK determines the separators to use between attributes.
2165#define XN_FLAG_SEP_MASK (0xf << 16)
2166
2167// XN_FLAG_SEP_COMMA_PLUS separates RDNs with "," and attributes within an RDN
2168// with "+", as in RFC 2253.
2169#define XN_FLAG_SEP_COMMA_PLUS (1 << 16)
2170
2171// XN_FLAG_SEP_CPLUS_SPC behaves like |XN_FLAG_SEP_COMMA_PLUS| but adds spaces
2172// between the separators.
2173#define XN_FLAG_SEP_CPLUS_SPC (2 << 16)
2174
2175// XN_FLAG_SEP_SPLUS_SPC separates RDNs with "; " and attributes within an RDN
2176// with " + ".
2177#define XN_FLAG_SEP_SPLUS_SPC (3 << 16)
2178
2179// XN_FLAG_SEP_MULTILINE prints each attribute on one line.
2180#define XN_FLAG_SEP_MULTILINE (4 << 16)
2181
2182// XN_FLAG_DN_REV prints RDNs in reverse, from least significant to most
2183// significant, as RFC 2253.
2184#define XN_FLAG_DN_REV (1 << 20)
2185
2186// XN_FLAG_FN_MASK determines how attribute types are displayed.
2187#define XN_FLAG_FN_MASK (0x3 << 21)
2188
2189// XN_FLAG_FN_SN uses the attribute type's short name, when available.
2190#define XN_FLAG_FN_SN 0
2191
David Benjamind9f209b2023-04-19 01:08:04 -04002192// XN_FLAG_SPC_EQ wraps the "=" operator with spaces when printing attributes.
2193#define XN_FLAG_SPC_EQ (1 << 23)
2194
2195// XN_FLAG_DUMP_UNKNOWN_FIELDS causes unknown attribute types to be printed in
2196// hex, as in RFC 2253.
2197#define XN_FLAG_DUMP_UNKNOWN_FIELDS (1 << 24)
2198
David Benjamind9f209b2023-04-19 01:08:04 -04002199// XN_FLAG_RFC2253 prints like RFC 2253.
2200#define XN_FLAG_RFC2253 \
2201 (ASN1_STRFLGS_RFC2253 | XN_FLAG_SEP_COMMA_PLUS | XN_FLAG_DN_REV | \
2202 XN_FLAG_FN_SN | XN_FLAG_DUMP_UNKNOWN_FIELDS)
2203
2204// XN_FLAG_ONELINE prints a one-line representation of the name.
2205#define XN_FLAG_ONELINE \
2206 (ASN1_STRFLGS_RFC2253 | ASN1_STRFLGS_ESC_QUOTE | XN_FLAG_SEP_CPLUS_SPC | \
2207 XN_FLAG_SPC_EQ | XN_FLAG_FN_SN)
2208
David Benjamind9f209b2023-04-19 01:08:04 -04002209// X509_NAME_print_ex writes a human-readable representation of |nm| to |out|.
2210// Each line of output is indented by |indent| spaces. It returns the number of
2211// bytes written on success, and -1 on error. If |out| is NULL, it returns the
2212// number of bytes it would have written but does not write anything. |flags|
2213// should be some combination of |XN_FLAG_*| and |ASN1_STRFLGS_*| values and
2214// determines the output. If unsure, use |XN_FLAG_RFC2253|.
2215//
2216// If |flags| is |XN_FLAG_COMPAT|, or zero, this function calls
2217// |X509_NAME_print| instead. In that case, it returns one on success, rather
2218// than the output length.
2219OPENSSL_EXPORT int X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent,
2220 unsigned long flags);
2221
2222// X509_NAME_print prints a human-readable representation of |name| to |bp|. It
2223// returns one on success and zero on error. |obase| is ignored.
2224//
2225// This function outputs a legacy format that does not correctly handle string
2226// encodings and other cases. Prefer |X509_NAME_print_ex| if printing a name for
2227// debugging purposes.
2228OPENSSL_EXPORT int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase);
2229
2230// X509_NAME_oneline writes a human-readable representation to |name| to a
2231// buffer as a NUL-terminated C string.
2232//
2233// If |buf| is NULL, returns a newly-allocated buffer containing the result on
2234// success, or NULL on error. The buffer must be released with |OPENSSL_free|
2235// when done.
2236//
2237// If |buf| is non-NULL, at most |size| bytes of output are written to |buf|
2238// instead. |size| includes the trailing NUL. The function then returns |buf| on
2239// success or NULL on error. If the output does not fit in |size| bytes, the
2240// output is silently truncated at an attribute boundary.
2241//
2242// This function outputs a legacy format that does not correctly handle string
2243// encodings and other cases. Prefer |X509_NAME_print_ex| if printing a name for
2244// debugging purposes.
2245OPENSSL_EXPORT char *X509_NAME_oneline(const X509_NAME *name, char *buf, int size);
2246
2247// X509_NAME_print_ex_fp behaves like |X509_NAME_print_ex| but writes to |fp|.
2248OPENSSL_EXPORT int X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm,
2249 int indent, unsigned long flags);
David Benjamin4363bdd2022-04-06 15:19:16 -04002250
2251// X509_signature_dump writes a human-readable representation of |sig| to |bio|,
2252// indented with |indent| spaces. It returns one on success and zero on error.
2253OPENSSL_EXPORT int X509_signature_dump(BIO *bio, const ASN1_STRING *sig,
2254 int indent);
2255
2256// X509_signature_print writes a human-readable representation of |alg| and
2257// |sig| to |bio|. It returns one on success and zero on error.
2258OPENSSL_EXPORT int X509_signature_print(BIO *bio, const X509_ALGOR *alg,
2259 const ASN1_STRING *sig);
2260
David Benjamin2d7adae2023-11-21 00:11:09 -05002261// X509V3_EXT_print prints a human-readable representation of |ext| to out. It
2262// returns one on success and zero on error. The output is indented by |indent|
2263// spaces. |flag| is one of the |X509V3_EXT_*| constants and controls printing
2264// of unknown extensions and syntax errors.
2265//
2266// WARNING: Although some applications programmatically parse the output of this
2267// function to process X.509 extensions, this is not safe. In many cases, the
2268// outputs are ambiguous to attempting to parse them can lead to string
2269// injection vulnerabilities. These functions should only be used for debugging
2270// or logging.
2271OPENSSL_EXPORT int X509V3_EXT_print(BIO *out, const X509_EXTENSION *ext,
2272 unsigned long flag, int indent);
2273
2274// X509V3_EXT_print_fp behaves like |X509V3_EXT_print| but writes to a |FILE|
2275// instead of a |BIO|.
2276OPENSSL_EXPORT int X509V3_EXT_print_fp(FILE *out, const X509_EXTENSION *ext,
2277 int flag, int indent);
2278
2279// X509V3_extensions_print prints |title|, followed by a human-readable
2280// representation of |exts| to |out|. It returns one on success and zero on
2281// error. The output is indented by |indent| spaces. |flag| is one of the
2282// |X509V3_EXT_*| constants and controls printing of unknown extensions and
2283// syntax errors.
2284OPENSSL_EXPORT int X509V3_extensions_print(BIO *out, const char *title,
2285 const STACK_OF(X509_EXTENSION) *exts,
2286 unsigned long flag, int indent);
2287
2288// GENERAL_NAME_print prints a human-readable representation of |gen| to |out|.
2289// It returns one on success and zero on error.
2290//
2291// TODO(davidben): Actually, it just returns one and doesn't check for I/O or
2292// allocation errors. But it should return zero on error.
2293OPENSSL_EXPORT int GENERAL_NAME_print(BIO *out, const GENERAL_NAME *gen);
2294
David Benjamin4363bdd2022-04-06 15:19:16 -04002295
David Benjamin918b8bc2022-07-09 15:33:19 -04002296// Convenience functions.
2297
2298// X509_pubkey_digest hashes the contents of the BIT STRING in |x509|'s
2299// subjectPublicKeyInfo field with |md| and writes the result to |out|.
2300// |EVP_MD_CTX_size| bytes are written, which is at most |EVP_MAX_MD_SIZE|. If
2301// |out_len| is not NULL, |*out_len| is set to the number of bytes written. This
2302// function returns one on success and zero on error.
2303//
2304// This hash omits the BIT STRING tag, length, and number of unused bits. It
2305// also omits the AlgorithmIdentifier which describes the key type. It
2306// corresponds to the OCSP KeyHash definition and is not suitable for other
2307// purposes.
2308OPENSSL_EXPORT int X509_pubkey_digest(const X509 *x509, const EVP_MD *md,
2309 uint8_t *out, unsigned *out_len);
2310
2311// X509_digest hashes |x509|'s DER encoding with |md| and writes the result to
2312// |out|. |EVP_MD_CTX_size| bytes are written, which is at most
2313// |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number
2314// of bytes written. This function returns one on success and zero on error.
2315// Note this digest covers the entire certificate, not just the signed portion.
2316OPENSSL_EXPORT int X509_digest(const X509 *x509, const EVP_MD *md, uint8_t *out,
2317 unsigned *out_len);
2318
2319// X509_CRL_digest hashes |crl|'s DER encoding with |md| and writes the result
2320// to |out|. |EVP_MD_CTX_size| bytes are written, which is at most
2321// |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number
2322// of bytes written. This function returns one on success and zero on error.
2323// Note this digest covers the entire CRL, not just the signed portion.
2324OPENSSL_EXPORT int X509_CRL_digest(const X509_CRL *crl, const EVP_MD *md,
2325 uint8_t *out, unsigned *out_len);
2326
2327// X509_REQ_digest hashes |req|'s DER encoding with |md| and writes the result
2328// to |out|. |EVP_MD_CTX_size| bytes are written, which is at most
2329// |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number
2330// of bytes written. This function returns one on success and zero on error.
2331// Note this digest covers the entire certificate request, not just the signed
2332// portion.
2333OPENSSL_EXPORT int X509_REQ_digest(const X509_REQ *req, const EVP_MD *md,
2334 uint8_t *out, unsigned *out_len);
2335
2336// X509_NAME_digest hashes |name|'s DER encoding with |md| and writes the result
2337// to |out|. |EVP_MD_CTX_size| bytes are written, which is at most
2338// |EVP_MAX_MD_SIZE|. If |out_len| is not NULL, |*out_len| is set to the number
2339// of bytes written. This function returns one on success and zero on error.
2340OPENSSL_EXPORT int X509_NAME_digest(const X509_NAME *name, const EVP_MD *md,
2341 uint8_t *out, unsigned *out_len);
2342
2343// The following functions behave like the corresponding unsuffixed |d2i_*|
2344// functions, but read the result from |bp| instead. Callers using these
2345// functions with memory |BIO|s to parse structures already in memory should use
2346// |d2i_*| instead.
2347OPENSSL_EXPORT X509 *d2i_X509_bio(BIO *bp, X509 **x509);
2348OPENSSL_EXPORT X509_CRL *d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl);
2349OPENSSL_EXPORT X509_REQ *d2i_X509_REQ_bio(BIO *bp, X509_REQ **req);
2350OPENSSL_EXPORT RSA *d2i_RSAPrivateKey_bio(BIO *bp, RSA **rsa);
2351OPENSSL_EXPORT RSA *d2i_RSAPublicKey_bio(BIO *bp, RSA **rsa);
2352OPENSSL_EXPORT RSA *d2i_RSA_PUBKEY_bio(BIO *bp, RSA **rsa);
2353OPENSSL_EXPORT DSA *d2i_DSA_PUBKEY_bio(BIO *bp, DSA **dsa);
2354OPENSSL_EXPORT DSA *d2i_DSAPrivateKey_bio(BIO *bp, DSA **dsa);
2355OPENSSL_EXPORT EC_KEY *d2i_EC_PUBKEY_bio(BIO *bp, EC_KEY **eckey);
2356OPENSSL_EXPORT EC_KEY *d2i_ECPrivateKey_bio(BIO *bp, EC_KEY **eckey);
2357OPENSSL_EXPORT X509_SIG *d2i_PKCS8_bio(BIO *bp, X509_SIG **p8);
2358OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_bio(
2359 BIO *bp, PKCS8_PRIV_KEY_INFO **p8inf);
2360OPENSSL_EXPORT EVP_PKEY *d2i_PUBKEY_bio(BIO *bp, EVP_PKEY **a);
2361OPENSSL_EXPORT DH *d2i_DHparams_bio(BIO *bp, DH **dh);
2362
2363// d2i_PrivateKey_bio behaves like |d2i_AutoPrivateKey|, but reads from |bp|
2364// instead.
2365OPENSSL_EXPORT EVP_PKEY *d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a);
2366
2367// The following functions behave like the corresponding unsuffixed |i2d_*|
2368// functions, but write the result to |bp|. They return one on success and zero
2369// on error. Callers using them with memory |BIO|s to encode structures to
2370// memory should use |i2d_*| directly instead.
2371OPENSSL_EXPORT int i2d_X509_bio(BIO *bp, X509 *x509);
2372OPENSSL_EXPORT int i2d_X509_CRL_bio(BIO *bp, X509_CRL *crl);
2373OPENSSL_EXPORT int i2d_X509_REQ_bio(BIO *bp, X509_REQ *req);
2374OPENSSL_EXPORT int i2d_RSAPrivateKey_bio(BIO *bp, RSA *rsa);
2375OPENSSL_EXPORT int i2d_RSAPublicKey_bio(BIO *bp, RSA *rsa);
2376OPENSSL_EXPORT int i2d_RSA_PUBKEY_bio(BIO *bp, RSA *rsa);
2377OPENSSL_EXPORT int i2d_DSA_PUBKEY_bio(BIO *bp, DSA *dsa);
2378OPENSSL_EXPORT int i2d_DSAPrivateKey_bio(BIO *bp, DSA *dsa);
2379OPENSSL_EXPORT int i2d_EC_PUBKEY_bio(BIO *bp, EC_KEY *eckey);
2380OPENSSL_EXPORT int i2d_ECPrivateKey_bio(BIO *bp, EC_KEY *eckey);
2381OPENSSL_EXPORT int i2d_PKCS8_bio(BIO *bp, X509_SIG *p8);
2382OPENSSL_EXPORT int i2d_PKCS8_PRIV_KEY_INFO_bio(BIO *bp,
2383 PKCS8_PRIV_KEY_INFO *p8inf);
2384OPENSSL_EXPORT int i2d_PrivateKey_bio(BIO *bp, EVP_PKEY *pkey);
2385OPENSSL_EXPORT int i2d_PUBKEY_bio(BIO *bp, EVP_PKEY *pkey);
2386OPENSSL_EXPORT int i2d_DHparams_bio(BIO *bp, const DH *dh);
2387
2388// i2d_PKCS8PrivateKeyInfo_bio encodes |key| as a PKCS#8 PrivateKeyInfo
2389// structure (see |EVP_marshal_private_key|) and writes the result to |bp|. It
2390// returns one on success and zero on error.
2391OPENSSL_EXPORT int i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, EVP_PKEY *key);
2392
2393// The following functions behave like the corresponding |d2i_*_bio| functions,
2394// but read from |fp| instead.
2395OPENSSL_EXPORT X509 *d2i_X509_fp(FILE *fp, X509 **x509);
2396OPENSSL_EXPORT X509_CRL *d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl);
2397OPENSSL_EXPORT X509_REQ *d2i_X509_REQ_fp(FILE *fp, X509_REQ **req);
2398OPENSSL_EXPORT RSA *d2i_RSAPrivateKey_fp(FILE *fp, RSA **rsa);
2399OPENSSL_EXPORT RSA *d2i_RSAPublicKey_fp(FILE *fp, RSA **rsa);
2400OPENSSL_EXPORT RSA *d2i_RSA_PUBKEY_fp(FILE *fp, RSA **rsa);
2401OPENSSL_EXPORT DSA *d2i_DSA_PUBKEY_fp(FILE *fp, DSA **dsa);
2402OPENSSL_EXPORT DSA *d2i_DSAPrivateKey_fp(FILE *fp, DSA **dsa);
2403OPENSSL_EXPORT EC_KEY *d2i_EC_PUBKEY_fp(FILE *fp, EC_KEY **eckey);
2404OPENSSL_EXPORT EC_KEY *d2i_ECPrivateKey_fp(FILE *fp, EC_KEY **eckey);
2405OPENSSL_EXPORT X509_SIG *d2i_PKCS8_fp(FILE *fp, X509_SIG **p8);
2406OPENSSL_EXPORT PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_fp(
2407 FILE *fp, PKCS8_PRIV_KEY_INFO **p8inf);
2408OPENSSL_EXPORT EVP_PKEY *d2i_PrivateKey_fp(FILE *fp, EVP_PKEY **a);
2409OPENSSL_EXPORT EVP_PKEY *d2i_PUBKEY_fp(FILE *fp, EVP_PKEY **a);
2410
2411// The following functions behave like the corresponding |i2d_*_bio| functions,
2412// but write to |fp| instead.
2413OPENSSL_EXPORT int i2d_X509_fp(FILE *fp, X509 *x509);
2414OPENSSL_EXPORT int i2d_X509_CRL_fp(FILE *fp, X509_CRL *crl);
2415OPENSSL_EXPORT int i2d_X509_REQ_fp(FILE *fp, X509_REQ *req);
2416OPENSSL_EXPORT int i2d_RSAPrivateKey_fp(FILE *fp, RSA *rsa);
2417OPENSSL_EXPORT int i2d_RSAPublicKey_fp(FILE *fp, RSA *rsa);
2418OPENSSL_EXPORT int i2d_RSA_PUBKEY_fp(FILE *fp, RSA *rsa);
2419OPENSSL_EXPORT int i2d_DSA_PUBKEY_fp(FILE *fp, DSA *dsa);
2420OPENSSL_EXPORT int i2d_DSAPrivateKey_fp(FILE *fp, DSA *dsa);
2421OPENSSL_EXPORT int i2d_EC_PUBKEY_fp(FILE *fp, EC_KEY *eckey);
2422OPENSSL_EXPORT int i2d_ECPrivateKey_fp(FILE *fp, EC_KEY *eckey);
2423OPENSSL_EXPORT int i2d_PKCS8_fp(FILE *fp, X509_SIG *p8);
2424OPENSSL_EXPORT int i2d_PKCS8_PRIV_KEY_INFO_fp(FILE *fp,
2425 PKCS8_PRIV_KEY_INFO *p8inf);
2426OPENSSL_EXPORT int i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, EVP_PKEY *key);
2427OPENSSL_EXPORT int i2d_PrivateKey_fp(FILE *fp, EVP_PKEY *pkey);
2428OPENSSL_EXPORT int i2d_PUBKEY_fp(FILE *fp, EVP_PKEY *pkey);
2429
David Benjaminbbd9f182022-09-09 11:57:14 -04002430// X509_find_by_issuer_and_serial returns the first |X509| in |sk| whose issuer
2431// and serial are |name| and |serial|, respectively. If no match is found, it
2432// returns NULL.
2433OPENSSL_EXPORT X509 *X509_find_by_issuer_and_serial(const STACK_OF(X509) *sk,
2434 X509_NAME *name,
2435 const ASN1_INTEGER *serial);
2436
2437// X509_find_by_subject returns the first |X509| in |sk| whose subject is
2438// |name|. If no match is found, it returns NULL.
2439OPENSSL_EXPORT X509 *X509_find_by_subject(const STACK_OF(X509) *sk,
2440 X509_NAME *name);
2441
David Benjamin58a40942023-04-19 10:43:55 -04002442// X509_cmp_time compares |s| against |*t|. On success, it returns a negative
2443// number if |s| <= |*t| and a positive number if |s| > |*t|. On error, it
2444// returns zero. If |t| is NULL, it uses the current time instead of |*t|.
2445//
2446// WARNING: Unlike most comparison functions, this function returns zero on
2447// error, not equality.
David Benjamin899c1a72023-07-05 21:56:55 -04002448OPENSSL_EXPORT int X509_cmp_time(const ASN1_TIME *s, const time_t *t);
David Benjamin58a40942023-04-19 10:43:55 -04002449
2450// X509_cmp_time_posix compares |s| against |t|. On success, it returns a
2451// negative number if |s| <= |t| and a positive number if |s| > |t|. On error,
2452// it returns zero.
2453//
2454// WARNING: Unlike most comparison functions, this function returns zero on
2455// error, not equality.
2456OPENSSL_EXPORT int X509_cmp_time_posix(const ASN1_TIME *s, int64_t t);
2457
2458// X509_cmp_current_time behaves like |X509_cmp_time| but compares |s| against
2459// the current time.
2460OPENSSL_EXPORT int X509_cmp_current_time(const ASN1_TIME *s);
2461
2462// X509_time_adj calls |X509_time_adj_ex| with |offset_day| equal to zero.
2463OPENSSL_EXPORT ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec,
David Benjamin899c1a72023-07-05 21:56:55 -04002464 const time_t *t);
David Benjamin58a40942023-04-19 10:43:55 -04002465
2466// X509_time_adj_ex behaves like |ASN1_TIME_adj|, but adds an offset to |*t|. If
2467// |t| is NULL, it uses the current time instead of |*t|.
2468OPENSSL_EXPORT ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s, int offset_day,
David Benjamin899c1a72023-07-05 21:56:55 -04002469 long offset_sec, const time_t *t);
David Benjamin58a40942023-04-19 10:43:55 -04002470
2471// X509_gmtime_adj behaves like |X509_time_adj_ex| but adds |offset_sec| to the
2472// current time.
2473OPENSSL_EXPORT ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long offset_sec);
2474
David Benjamina697bcb2023-11-12 19:56:19 -05002475// X509_issuer_name_cmp behaves like |X509_NAME_cmp|, but compares |a| and |b|'s
2476// issuer names.
2477OPENSSL_EXPORT int X509_issuer_name_cmp(const X509 *a, const X509 *b);
2478
2479// X509_subject_name_cmp behaves like |X509_NAME_cmp|, but compares |a| and
2480// |b|'s subject names.
2481OPENSSL_EXPORT int X509_subject_name_cmp(const X509 *a, const X509 *b);
2482
2483// X509_CRL_cmp behaves like |X509_NAME_cmp|, but compares |a| and |b|'s
2484// issuer names.
2485//
2486// WARNING: This function is misnamed. It does not compare other parts of the
2487// CRL, only the issuer fields using |X509_NAME_cmp|.
2488OPENSSL_EXPORT int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b);
2489
David Benjamin918b8bc2022-07-09 15:33:19 -04002490
David Benjamin4363bdd2022-04-06 15:19:16 -04002491// ex_data functions.
2492//
2493// See |ex_data.h| for details.
2494
2495OPENSSL_EXPORT int X509_get_ex_new_index(long argl, void *argp,
2496 CRYPTO_EX_unused *unused,
2497 CRYPTO_EX_dup *dup_unused,
2498 CRYPTO_EX_free *free_func);
2499OPENSSL_EXPORT int X509_set_ex_data(X509 *r, int idx, void *arg);
2500OPENSSL_EXPORT void *X509_get_ex_data(X509 *r, int idx);
2501
2502OPENSSL_EXPORT int X509_STORE_CTX_get_ex_new_index(long argl, void *argp,
2503 CRYPTO_EX_unused *unused,
2504 CRYPTO_EX_dup *dup_unused,
2505 CRYPTO_EX_free *free_func);
2506OPENSSL_EXPORT int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx,
2507 void *data);
2508OPENSSL_EXPORT void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx);
2509
2510
David Benjamin5bef6ec2023-11-13 09:04:43 -05002511// Hashing and signing ASN.1 structures.
2512
2513// ASN1_digest serializes |data| with |i2d| and then hashes the result with
2514// |type|. On success, it returns one, writes the digest to |md|, and sets
2515// |*len| to the digest length if non-NULL. On error, it returns zero.
2516//
2517// |EVP_MD_CTX_size| bytes are written, which is at most |EVP_MAX_MD_SIZE|. The
2518// buffer must have sufficient space for this output.
2519OPENSSL_EXPORT int ASN1_digest(i2d_of_void *i2d, const EVP_MD *type, char *data,
2520 unsigned char *md, unsigned int *len);
2521
2522// ASN1_item_digest serializes |data| with |it| and then hashes the result with
2523// |type|. On success, it returns one, writes the digest to |md|, and sets
2524// |*len| to the digest length if non-NULL. On error, it returns zero.
2525//
2526// |EVP_MD_CTX_size| bytes are written, which is at most |EVP_MAX_MD_SIZE|. The
2527// buffer must have sufficient space for this output.
2528//
2529// WARNING: |data| must be a pointer with the same type as |it|'s corresponding
2530// C type. Using the wrong type is a potentially exploitable memory error.
2531OPENSSL_EXPORT int ASN1_item_digest(const ASN1_ITEM *it, const EVP_MD *type,
2532 void *data, unsigned char *md,
2533 unsigned int *len);
2534
2535// ASN1_item_verify serializes |data| with |it| and then verifies |signature| is
2536// a valid signature for the result with |algor1| and |pkey|. It returns one on
2537// success and zero on error. The signature and algorithm are interpreted as in
2538// X.509.
2539//
2540// WARNING: |data| must be a pointer with the same type as |it|'s corresponding
2541// C type. Using the wrong type is a potentially exploitable memory error.
2542OPENSSL_EXPORT int ASN1_item_verify(const ASN1_ITEM *it,
2543 const X509_ALGOR *algor1,
2544 const ASN1_BIT_STRING *signature,
2545 void *data, EVP_PKEY *pkey);
2546
2547// ASN1_item_sign serializes |data| with |it| and then signs the result with
2548// the private key |pkey|. It returns the length of the signature on success and
2549// zero on error. On success, it writes the signature to |signature| and the
2550// signature algorithm to each of |algor1| and |algor2|. Either of |algor1| or
2551// |algor2| may be NULL to ignore them. This function uses digest algorithm
2552// |md|, or |pkey|'s default if NULL. Other signing parameters use |pkey|'s
2553// defaults. To customize them, use |ASN1_item_sign_ctx|.
2554//
2555// WARNING: |data| must be a pointer with the same type as |it|'s corresponding
2556// C type. Using the wrong type is a potentially exploitable memory error.
2557OPENSSL_EXPORT int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1,
2558 X509_ALGOR *algor2,
2559 ASN1_BIT_STRING *signature, void *data,
2560 EVP_PKEY *pkey, const EVP_MD *type);
2561
2562// ASN1_item_sign_ctx behaves like |ASN1_item_sign| except the signature is
2563// signed with |ctx|, |ctx|, which must have been initialized with
2564// |EVP_DigestSignInit|. The caller should configure the corresponding
2565// |EVP_PKEY_CTX| with any additional parameters before calling this function.
2566//
2567// On success or failure, this function mutates |ctx| and resets it to the empty
2568// state. Caller should not rely on its contents after the function returns.
2569//
2570// WARNING: |data| must be a pointer with the same type as |it|'s corresponding
2571// C type. Using the wrong type is a potentially exploitable memory error.
2572OPENSSL_EXPORT int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1,
2573 X509_ALGOR *algor2,
2574 ASN1_BIT_STRING *signature, void *asn,
2575 EVP_MD_CTX *ctx);
2576
2577
David Benjamin38c4bf12023-11-29 10:48:37 -05002578// X.509 information.
2579//
2580// |X509_INFO| is the return type for |PEM_X509_INFO_read_bio|, defined in
2581// <openssl/pem.h>. It is used to store a certificate, CRL, or private key. This
2582// type is defined in this header for OpenSSL compatibility.
2583
2584struct private_key_st {
2585 EVP_PKEY *dec_pkey;
2586} /* X509_PKEY */;
2587
2588struct X509_info_st {
2589 X509 *x509;
2590 X509_CRL *crl;
2591 X509_PKEY *x_pkey;
2592
2593 EVP_CIPHER_INFO enc_cipher;
2594 int enc_len;
2595 char *enc_data;
2596} /* X509_INFO */;
2597
2598DEFINE_STACK_OF(X509_INFO)
2599
2600// X509_INFO_free releases memory associated with |info|.
2601OPENSSL_EXPORT void X509_INFO_free(X509_INFO *info);
2602
2603
David Benjamin2d7adae2023-11-21 00:11:09 -05002604// Deprecated config-based extension creation.
2605//
2606// The following functions allow specifying X.509 extensions using OpenSSL's
2607// config file syntax, from the OpenSSL command-line tool. They are retained,
2608// for now, for compatibility with legacy software but may be removed in the
2609// future. Construct the extensions using the typed C APIs instead.
2610//
2611// Callers should especially avoid these functions if passing in non-constant
2612// values. They use ad-hoc, string-based formats which are prone to injection
2613// vulnerabilities. For a CA, this means using them risks misissuance.
2614//
2615// These functions are not safe to use with untrusted inputs. The string formats
2616// may implicitly reference context information and, in OpenSSL (though not
2617// BoringSSL), one even allows reading arbitrary files. Many formats can also
2618// produce far larger outputs than their inputs, so untrusted inputs may lead to
2619// denial-of-service attacks. Finally, the parsers see much less testing and
2620// review than most of the library and may have bugs including memory leaks or
2621// crashes.
2622
2623// v3_ext_ctx, aka |X509V3_CTX|, contains additional context information for
2624// constructing extensions. Some string formats reference additional values in
2625// these objects. It must be initialized with |X509V3_set_ctx| or
2626// |X509V3_set_ctx_test| before use.
2627struct v3_ext_ctx {
2628 int flags;
2629 const X509 *issuer_cert;
2630 const X509 *subject_cert;
2631 const X509_REQ *subject_req;
2632 const X509_CRL *crl;
2633 const CONF *db;
2634};
2635
2636#define X509V3_CTX_TEST 0x1
2637
2638// X509V3_set_ctx initializes |ctx| with the specified objects. Some string
2639// formats will reference fields in these objects. Each object may be NULL to
2640// omit it, in which case those formats cannot be used. |flags| should be zero,
2641// unless called via |X509V3_set_ctx_test|.
2642//
2643// |issuer|, |subject|, |req|, and |crl|, if non-NULL, must outlive |ctx|.
2644OPENSSL_EXPORT void X509V3_set_ctx(X509V3_CTX *ctx, const X509 *issuer,
2645 const X509 *subject, const X509_REQ *req,
2646 const X509_CRL *crl, int flags);
2647
2648// X509V3_set_ctx_test calls |X509V3_set_ctx| without any reference objects and
2649// mocks out some features that use them. The resulting extensions may be
2650// incomplete and should be discarded. This can be used to partially validate
2651// syntax.
2652//
2653// TODO(davidben): Can we remove this?
2654#define X509V3_set_ctx_test(ctx) \
2655 X509V3_set_ctx(ctx, NULL, NULL, NULL, NULL, X509V3_CTX_TEST)
2656
2657// X509V3_set_nconf sets |ctx| to use |conf| as the config database. |ctx| must
2658// have previously been initialized by |X509V3_set_ctx| or
2659// |X509V3_set_ctx_test|. Some string formats will reference sections in |conf|.
2660// |conf| may be NULL, in which case these formats cannot be used. If non-NULL,
2661// |conf| must outlive |ctx|.
2662OPENSSL_EXPORT void X509V3_set_nconf(X509V3_CTX *ctx, const CONF *conf);
2663
2664// X509V3_set_ctx_nodb calls |X509V3_set_nconf| with no config database.
2665#define X509V3_set_ctx_nodb(ctx) X509V3_set_nconf(ctx, NULL)
2666
2667// X509V3_EXT_nconf constructs an extension of type specified by |name|, and
2668// value specified by |value|. It returns a newly-allocated |X509_EXTENSION|
2669// object on success, or NULL on error. |conf| and |ctx| specify additional
2670// information referenced by some formats. Either |conf| or |ctx| may be NULL,
2671// in which case features which use it will be disabled.
2672//
2673// If non-NULL, |ctx| must be initialized with |X509V3_set_ctx| or
2674// |X509V3_set_ctx_test|.
2675//
2676// Both |conf| and |ctx| provide a |CONF| object. When |ctx| is non-NULL, most
2677// features use the |ctx| copy, configured with |X509V3_set_ctx|, but some use
2678// |conf|. Callers should ensure the two match to avoid surprisingly behavior.
2679OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_nconf(const CONF *conf,
2680 const X509V3_CTX *ctx,
2681 const char *name,
2682 const char *value);
2683
2684// X509V3_EXT_nconf_nid behaves like |X509V3_EXT_nconf|, except the extension
2685// type is specified as a NID.
2686OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_nconf_nid(const CONF *conf,
2687 const X509V3_CTX *ctx,
2688 int ext_nid,
2689 const char *value);
2690
2691// X509V3_EXT_conf_nid calls |X509V3_EXT_nconf_nid|. |conf| must be NULL.
2692//
2693// TODO(davidben): This is the only exposed instance of an LHASH in our public
2694// headers. cryptography.io wraps this function so we cannot, yet, replace the
2695// type with a dummy struct.
2696OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_conf_nid(LHASH_OF(CONF_VALUE) *conf,
2697 const X509V3_CTX *ctx,
2698 int ext_nid,
2699 const char *value);
2700
2701// X509V3_EXT_add_nconf_sk looks up the section named |section| in |conf|. For
2702// each |CONF_VALUE| in the section, it constructs an extension as in
2703// |X509V3_EXT_nconf|, taking |name| and |value| from the |CONF_VALUE|. Each new
2704// extension is appended to |*sk|. If |*sk| is non-NULL, and at least one
2705// extension is added, it sets |*sk| to a newly-allocated
2706// |STACK_OF(X509_EXTENSION)|. It returns one on success and zero on error.
2707OPENSSL_EXPORT int X509V3_EXT_add_nconf_sk(const CONF *conf,
2708 const X509V3_CTX *ctx,
2709 const char *section,
2710 STACK_OF(X509_EXTENSION) **sk);
2711
2712// X509V3_EXT_add_nconf adds extensions to |cert| as in
2713// |X509V3_EXT_add_nconf_sk|. It returns one on success and zero on error.
2714OPENSSL_EXPORT int X509V3_EXT_add_nconf(const CONF *conf, const X509V3_CTX *ctx,
2715 const char *section, X509 *cert);
2716
2717// X509V3_EXT_REQ_add_nconf adds extensions to |req| as in
2718// |X509V3_EXT_add_nconf_sk|. It returns one on success and zero on error.
2719OPENSSL_EXPORT int X509V3_EXT_REQ_add_nconf(const CONF *conf,
2720 const X509V3_CTX *ctx,
2721 const char *section, X509_REQ *req);
2722
2723// X509V3_EXT_CRL_add_nconf adds extensions to |crl| as in
2724// |X509V3_EXT_add_nconf_sk|. It returns one on success and zero on error.
2725OPENSSL_EXPORT int X509V3_EXT_CRL_add_nconf(const CONF *conf,
2726 const X509V3_CTX *ctx,
2727 const char *section, X509_CRL *crl);
2728
2729
David Benjamin4363bdd2022-04-06 15:19:16 -04002730// Deprecated functions.
2731
2732// X509_get_notBefore returns |x509|'s notBefore time. Note this function is not
2733// const-correct for legacy reasons. Use |X509_get0_notBefore| or
2734// |X509_getm_notBefore| instead.
2735OPENSSL_EXPORT ASN1_TIME *X509_get_notBefore(const X509 *x509);
2736
2737// X509_get_notAfter returns |x509|'s notAfter time. Note this function is not
2738// const-correct for legacy reasons. Use |X509_get0_notAfter| or
2739// |X509_getm_notAfter| instead.
2740OPENSSL_EXPORT ASN1_TIME *X509_get_notAfter(const X509 *x509);
2741
2742// X509_set_notBefore calls |X509_set1_notBefore|. Use |X509_set1_notBefore|
2743// instead.
2744OPENSSL_EXPORT int X509_set_notBefore(X509 *x509, const ASN1_TIME *tm);
2745
2746// X509_set_notAfter calls |X509_set1_notAfter|. Use |X509_set1_notAfter|
2747// instead.
2748OPENSSL_EXPORT int X509_set_notAfter(X509 *x509, const ASN1_TIME *tm);
2749
David Benjamin884ae3a2022-07-08 18:18:12 -04002750// X509_CRL_get_lastUpdate returns a mutable pointer to |crl|'s thisUpdate time.
2751// The OpenSSL API refers to this field as lastUpdate.
2752//
David Benjamin4363bdd2022-04-06 15:19:16 -04002753// Use |X509_CRL_get0_lastUpdate| or |X509_CRL_set1_lastUpdate| instead.
2754OPENSSL_EXPORT ASN1_TIME *X509_CRL_get_lastUpdate(X509_CRL *crl);
2755
2756// X509_CRL_get_nextUpdate returns a mutable pointer to |crl|'s nextUpdate time,
2757// or NULL if |crl| has none. Use |X509_CRL_get0_nextUpdate| or
2758// |X509_CRL_set1_nextUpdate| instead.
2759OPENSSL_EXPORT ASN1_TIME *X509_CRL_get_nextUpdate(X509_CRL *crl);
2760
2761// X509_extract_key is a legacy alias to |X509_get_pubkey|. Use
2762// |X509_get_pubkey| instead.
2763#define X509_extract_key(x) X509_get_pubkey(x)
2764
2765// X509_REQ_extract_key is a legacy alias for |X509_REQ_get_pubkey|.
2766#define X509_REQ_extract_key(a) X509_REQ_get_pubkey(a)
2767
2768// X509_name_cmp is a legacy alias for |X509_NAME_cmp|.
2769#define X509_name_cmp(a, b) X509_NAME_cmp((a), (b))
2770
2771// The following symbols are deprecated aliases to |X509_CRL_set1_*|.
2772#define X509_CRL_set_lastUpdate X509_CRL_set1_lastUpdate
2773#define X509_CRL_set_nextUpdate X509_CRL_set1_nextUpdate
2774
2775// X509_get_serialNumber returns a mutable pointer to |x509|'s serial number.
2776// Prefer |X509_get0_serialNumber|.
2777OPENSSL_EXPORT ASN1_INTEGER *X509_get_serialNumber(X509 *x509);
2778
David Benjaminb6f47e82022-07-09 00:57:54 -04002779// X509_NAME_get_text_by_OBJ finds the first attribute with type |obj| in
Bob Beck3763efb2023-03-29 11:17:46 -06002780// |name|. If found, it writes the value's UTF-8 representation to |buf|.
2781// followed by a NUL byte, and returns the number of bytes in the output,
2782// excluding the NUL byte. This is unlike OpenSSL which returns the raw
2783// ASN1_STRING data. The UTF-8 encoding of the |ASN1_STRING| may not contain a 0
2784// codepoint.
David Benjaminb6f47e82022-07-09 00:57:54 -04002785//
Bob Beck3763efb2023-03-29 11:17:46 -06002786// This function writes at most |len| bytes, including the NUL byte. If |buf|
2787// is NULL, it writes nothing and returns the number of bytes in the
2788// output, excluding the NUL byte that would be required for the full UTF-8
2789// output.
David Benjaminb6f47e82022-07-09 00:57:54 -04002790//
Bob Beck3763efb2023-03-29 11:17:46 -06002791// This function may return -1 if an error occurs for any reason, including the
2792// value not being a recognized string type, |len| being of insufficient size to
2793// hold the full UTF-8 encoding and NUL byte, memory allocation failures, an
2794// object with type |obj| not existing in |name|, or if the UTF-8 encoding of
2795// the string contains a zero byte.
David Benjaminb6f47e82022-07-09 00:57:54 -04002796OPENSSL_EXPORT int X509_NAME_get_text_by_OBJ(const X509_NAME *name,
2797 const ASN1_OBJECT *obj, char *buf,
2798 int len);
2799
2800// X509_NAME_get_text_by_NID behaves like |X509_NAME_get_text_by_OBJ| except it
2801// finds an attribute of type |nid|, which should be one of the |NID_*|
2802// constants.
2803OPENSSL_EXPORT int X509_NAME_get_text_by_NID(const X509_NAME *name, int nid,
2804 char *buf, int len);
2805
David Benjamina4851dd2023-11-12 15:51:49 -05002806// X509_STORE_CTX_get0_parent_ctx returns NULL.
2807OPENSSL_EXPORT X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(
2808 X509_STORE_CTX *ctx);
2809
David Benjaminc2b7df52023-11-18 19:31:24 -05002810// X509_OBJECT_free_contents sets |obj| to the empty object, freeing any values
2811// that were previously there.
2812//
2813// TODO(davidben): Unexport this function after rust-openssl is fixed to no
2814// longer call it.
2815OPENSSL_EXPORT void X509_OBJECT_free_contents(X509_OBJECT *obj);
2816
David Benjamind9b81bb2023-11-18 19:48:21 -05002817// X509_LOOKUP_free releases memory associated with |ctx|. This function should
2818// never be used outside the library. No function in the public API hands
2819// ownership of an |X509_LOOKUP| to the caller.
2820//
2821// TODO(davidben): Unexport this function after rust-openssl is fixed to no
2822// longer call it.
2823OPENSSL_EXPORT void X509_LOOKUP_free(X509_LOOKUP *ctx);
2824
David Benjamin2ba76342023-11-19 10:20:04 -05002825// X509_STORE_CTX_cleanup resets |ctx| to the empty state.
2826//
2827// This function is a remnant of when |X509_STORE_CTX| was stack-allocated and
2828// should not be used. If releasing |ctx|, call |X509_STORE_CTX_free|. If
2829// reusing |ctx| for a new verification, release the old one and create a new
2830// one.
2831OPENSSL_EXPORT void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx);
2832
David Benjamin2d7adae2023-11-21 00:11:09 -05002833// X509V3_add_standard_extensions returns one.
2834OPENSSL_EXPORT int X509V3_add_standard_extensions(void);
2835
David Benjamin4363bdd2022-04-06 15:19:16 -04002836
2837// Private structures.
2838
2839struct X509_algor_st {
2840 ASN1_OBJECT *algorithm;
2841 ASN1_TYPE *parameter;
2842} /* X509_ALGOR */;
2843
2844
2845// Functions below this point have not yet been organized into sections.
2846
David Benjamin4363bdd2022-04-06 15:19:16 -04002847#define X509v3_KU_DIGITAL_SIGNATURE 0x0080
2848#define X509v3_KU_NON_REPUDIATION 0x0040
2849#define X509v3_KU_KEY_ENCIPHERMENT 0x0020
2850#define X509v3_KU_DATA_ENCIPHERMENT 0x0010
2851#define X509v3_KU_KEY_AGREEMENT 0x0008
2852#define X509v3_KU_KEY_CERT_SIGN 0x0004
2853#define X509v3_KU_CRL_SIGN 0x0002
2854#define X509v3_KU_ENCIPHER_ONLY 0x0001
2855#define X509v3_KU_DECIPHER_ONLY 0x8000
2856#define X509v3_KU_UNDEF 0xffff
Adam Langley95c29f32014-06-20 12:00:00 -07002857
David Benjamin991d31b2020-06-19 11:38:20 -04002858// This stuff is certificate "auxiliary info"
2859// it contains details which are useful in certificate
2860// stores and databases. When used this is tagged onto
2861// the end of the certificate itself
Adam Langley95c29f32014-06-20 12:00:00 -07002862
David Benjamin01f8a8c2017-04-15 18:12:55 -04002863DECLARE_STACK_OF(DIST_POINT)
2864DECLARE_STACK_OF(GENERAL_NAME)
2865
David Benjamin991d31b2020-06-19 11:38:20 -04002866// This is used for a table of trust checking functions
Adam Langley95c29f32014-06-20 12:00:00 -07002867
David Benjamin43bd18f2015-08-04 10:45:18 -04002868struct x509_trust_st {
David Benjamin991d31b2020-06-19 11:38:20 -04002869 int trust;
2870 int flags;
2871 int (*check_trust)(struct x509_trust_st *, X509 *, int);
2872 char *name;
2873 int arg1;
2874 void *arg2;
David Benjamin43bd18f2015-08-04 10:45:18 -04002875} /* X509_TRUST */;
Adam Langley95c29f32014-06-20 12:00:00 -07002876
David Benjamin01f8a8c2017-04-15 18:12:55 -04002877DEFINE_STACK_OF(X509_TRUST)
Adam Langley95c29f32014-06-20 12:00:00 -07002878
David Benjamin991d31b2020-06-19 11:38:20 -04002879// standard trust ids
Adam Langley95c29f32014-06-20 12:00:00 -07002880
David Benjamin991d31b2020-06-19 11:38:20 -04002881#define X509_TRUST_DEFAULT (-1) // Only valid in purpose settings
Adam Langley95c29f32014-06-20 12:00:00 -07002882
David Benjamin991d31b2020-06-19 11:38:20 -04002883#define X509_TRUST_COMPAT 1
2884#define X509_TRUST_SSL_CLIENT 2
2885#define X509_TRUST_SSL_SERVER 3
2886#define X509_TRUST_EMAIL 4
2887#define X509_TRUST_OBJECT_SIGN 5
2888#define X509_TRUST_OCSP_SIGN 6
2889#define X509_TRUST_OCSP_REQUEST 7
2890#define X509_TRUST_TSA 8
Adam Langley95c29f32014-06-20 12:00:00 -07002891
David Benjamin991d31b2020-06-19 11:38:20 -04002892// Keep these up to date!
2893#define X509_TRUST_MIN 1
2894#define X509_TRUST_MAX 8
Adam Langley95c29f32014-06-20 12:00:00 -07002895
2896
David Benjamin991d31b2020-06-19 11:38:20 -04002897// trust_flags values
2898#define X509_TRUST_DYNAMIC 1
2899#define X509_TRUST_DYNAMIC_NAME 2
Adam Langley95c29f32014-06-20 12:00:00 -07002900
David Benjamin991d31b2020-06-19 11:38:20 -04002901// check_trust return codes
Adam Langley95c29f32014-06-20 12:00:00 -07002902
David Benjamin991d31b2020-06-19 11:38:20 -04002903#define X509_TRUST_TRUSTED 1
2904#define X509_TRUST_REJECTED 2
2905#define X509_TRUST_UNTRUSTED 3
Adam Langley95c29f32014-06-20 12:00:00 -07002906
David Benjamin01f8a8c2017-04-15 18:12:55 -04002907DECLARE_STACK_OF(GENERAL_NAMES)
2908
David Benjamin4ef5de02020-08-25 20:00:44 -04002909// X509_verify_cert_error_string returns |err| as a human-readable string, where
2910// |err| should be one of the |X509_V_*| values. If |err| is unknown, it returns
2911// a default description.
David Benjamin4ef5de02020-08-25 20:00:44 -04002912OPENSSL_EXPORT const char *X509_verify_cert_error_string(long err);
Adam Langley95c29f32014-06-20 12:00:00 -07002913
David Benjamin991d31b2020-06-19 11:38:20 -04002914OPENSSL_EXPORT const char *X509_get_default_cert_area(void);
2915OPENSSL_EXPORT const char *X509_get_default_cert_dir(void);
2916OPENSSL_EXPORT const char *X509_get_default_cert_file(void);
2917OPENSSL_EXPORT const char *X509_get_default_cert_dir_env(void);
2918OPENSSL_EXPORT const char *X509_get_default_cert_file_env(void);
2919OPENSSL_EXPORT const char *X509_get_default_private_dir(void);
Adam Langley95c29f32014-06-20 12:00:00 -07002920
David Benjamind0f14f32022-03-08 15:55:42 -05002921
David Benjamind0f14f32022-03-08 15:55:42 -05002922OPENSSL_EXPORT int X509_TRUST_set(int *t, int trust);
2923
David Benjamin991d31b2020-06-19 11:38:20 -04002924OPENSSL_EXPORT unsigned long X509_issuer_name_hash(X509 *a);
Adam Langley95c29f32014-06-20 12:00:00 -07002925
David Benjamin991d31b2020-06-19 11:38:20 -04002926OPENSSL_EXPORT unsigned long X509_subject_name_hash(X509 *x);
Adam Langley95c29f32014-06-20 12:00:00 -07002927
David Benjamin991d31b2020-06-19 11:38:20 -04002928OPENSSL_EXPORT unsigned long X509_issuer_name_hash_old(X509 *a);
2929OPENSSL_EXPORT unsigned long X509_subject_name_hash_old(X509 *x);
Adam Langley95c29f32014-06-20 12:00:00 -07002930
David Benjamin991d31b2020-06-19 11:38:20 -04002931OPENSSL_EXPORT int X509_cmp(const X509 *a, const X509 *b);
David Benjamin991d31b2020-06-19 11:38:20 -04002932OPENSSL_EXPORT unsigned long X509_NAME_hash(X509_NAME *x);
2933OPENSSL_EXPORT unsigned long X509_NAME_hash_old(X509_NAME *x);
Adam Langley95c29f32014-06-20 12:00:00 -07002934
David Benjamin991d31b2020-06-19 11:38:20 -04002935OPENSSL_EXPORT int X509_CRL_match(const X509_CRL *a, const X509_CRL *b);
Adam Langley95c29f32014-06-20 12:00:00 -07002936
David Benjamin991d31b2020-06-19 11:38:20 -04002937OPENSSL_EXPORT int X509_verify_cert(X509_STORE_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002938
Adam Langleyeb7d2ed2014-07-30 16:02:14 -07002939OPENSSL_EXPORT int X509_check_trust(X509 *x, int id, int flags);
2940OPENSSL_EXPORT int X509_TRUST_get_count(void);
David Benjamin991d31b2020-06-19 11:38:20 -04002941OPENSSL_EXPORT X509_TRUST *X509_TRUST_get0(int idx);
Adam Langleyeb7d2ed2014-07-30 16:02:14 -07002942OPENSSL_EXPORT int X509_TRUST_get_by_id(int id);
David Benjamin991d31b2020-06-19 11:38:20 -04002943OPENSSL_EXPORT int X509_TRUST_add(int id, int flags,
2944 int (*ck)(X509_TRUST *, X509 *, int),
David Benjamincbb96b42023-06-04 12:50:39 -04002945 const char *name, int arg1, void *arg2);
Adam Langleyeb7d2ed2014-07-30 16:02:14 -07002946OPENSSL_EXPORT void X509_TRUST_cleanup(void);
David Benjamin125a38f2020-08-20 14:54:02 -04002947OPENSSL_EXPORT int X509_TRUST_get_flags(const X509_TRUST *xp);
2948OPENSSL_EXPORT char *X509_TRUST_get0_name(const X509_TRUST *xp);
2949OPENSSL_EXPORT int X509_TRUST_get_trust(const X509_TRUST *xp);
Adam Langley95c29f32014-06-20 12:00:00 -07002950
David Benjamin17727c62016-01-30 14:58:52 -05002951
David Benjaminc31a8a62021-10-03 22:47:13 -04002952/*
2953SSL_CTX -> X509_STORE
2954 -> X509_LOOKUP
2955 ->X509_LOOKUP_METHOD
2956 -> X509_LOOKUP
2957 ->X509_LOOKUP_METHOD
2958
2959SSL -> X509_STORE_CTX
2960 ->X509_STORE
2961
2962The X509_STORE holds the tables etc for verification stuff.
2963A X509_STORE_CTX is used while validating a single certificate.
2964The X509_STORE has X509_LOOKUPs for looking up certs.
2965The X509_STORE then calls a function to actually verify the
2966certificate chain.
2967*/
2968
David Benjaminc2b7df52023-11-18 19:31:24 -05002969#define X509_LU_NONE 0
David Benjaminc31a8a62021-10-03 22:47:13 -04002970#define X509_LU_X509 1
2971#define X509_LU_CRL 2
2972#define X509_LU_PKEY 3
2973
2974DEFINE_STACK_OF(X509_LOOKUP)
2975DEFINE_STACK_OF(X509_OBJECT)
2976DEFINE_STACK_OF(X509_VERIFY_PARAM)
2977
2978typedef int (*X509_STORE_CTX_verify_cb)(int, X509_STORE_CTX *);
David Benjaminc31a8a62021-10-03 22:47:13 -04002979typedef int (*X509_STORE_CTX_get_issuer_fn)(X509 **issuer, X509_STORE_CTX *ctx,
2980 X509 *x);
David Benjaminc31a8a62021-10-03 22:47:13 -04002981typedef int (*X509_STORE_CTX_get_crl_fn)(X509_STORE_CTX *ctx, X509_CRL **crl,
2982 X509 *x);
2983typedef int (*X509_STORE_CTX_check_crl_fn)(X509_STORE_CTX *ctx, X509_CRL *crl);
David Benjaminc31a8a62021-10-03 22:47:13 -04002984
2985OPENSSL_EXPORT int X509_STORE_set_depth(X509_STORE *store, int depth);
2986
2987OPENSSL_EXPORT void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth);
2988
2989#define X509_STORE_CTX_set_app_data(ctx, data) \
2990 X509_STORE_CTX_set_ex_data(ctx, 0, data)
2991#define X509_STORE_CTX_get_app_data(ctx) X509_STORE_CTX_get_ex_data(ctx, 0)
2992
2993#define X509_L_FILE_LOAD 1
2994#define X509_L_ADD_DIR 2
2995
David Benjamin2ff409e2023-11-22 01:32:03 -05002996// The following constants are used to specify the format of files in an
2997// |X509_LOOKUP|.
2998#define X509_FILETYPE_PEM 1
2999#define X509_FILETYPE_ASN1 2
3000#define X509_FILETYPE_DEFAULT 3
David Benjaminc31a8a62021-10-03 22:47:13 -04003001
David Benjamin2ff409e2023-11-22 01:32:03 -05003002// X509_LOOKUP_load_file configures |lookup| to load information from the file
3003// at |path|. It returns one on success and zero on error. |type| should be one
3004// of the |X509_FILETYPE_*| constants to determine if the contents are PEM or
3005// DER. If |type| is |X509_FILETYPE_DEFAULT|, |path| is ignored and instead some
3006// default system path is used.
3007OPENSSL_EXPORT int X509_LOOKUP_load_file(X509_LOOKUP *lookup, const char *path,
3008 int type);
3009
3010// X509_LOOKUP_add_dir configures |lookup| to load information from the
3011// directory at |path|. It returns one on success and zero on error. |type|
3012// should be one of the |X509_FILETYPE_*| constants to determine if the contents
3013// are PEM or DER. If |type| is |X509_FILETYPE_DEFAULT|, |path| is ignored and
3014// instead some default system path is used.
3015OPENSSL_EXPORT int X509_LOOKUP_add_dir(X509_LOOKUP *lookup, const char *path,
3016 int type);
David Benjaminc31a8a62021-10-03 22:47:13 -04003017
3018#define X509_V_OK 0
3019#define X509_V_ERR_UNSPECIFIED 1
3020
3021#define X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT 2
3022#define X509_V_ERR_UNABLE_TO_GET_CRL 3
3023#define X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE 4
3024#define X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE 5
3025#define X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY 6
3026#define X509_V_ERR_CERT_SIGNATURE_FAILURE 7
3027#define X509_V_ERR_CRL_SIGNATURE_FAILURE 8
3028#define X509_V_ERR_CERT_NOT_YET_VALID 9
3029#define X509_V_ERR_CERT_HAS_EXPIRED 10
3030#define X509_V_ERR_CRL_NOT_YET_VALID 11
3031#define X509_V_ERR_CRL_HAS_EXPIRED 12
3032#define X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD 13
3033#define X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD 14
3034#define X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD 15
3035#define X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD 16
3036#define X509_V_ERR_OUT_OF_MEM 17
3037#define X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT 18
3038#define X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN 19
3039#define X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY 20
3040#define X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE 21
3041#define X509_V_ERR_CERT_CHAIN_TOO_LONG 22
3042#define X509_V_ERR_CERT_REVOKED 23
3043#define X509_V_ERR_INVALID_CA 24
3044#define X509_V_ERR_PATH_LENGTH_EXCEEDED 25
3045#define X509_V_ERR_INVALID_PURPOSE 26
3046#define X509_V_ERR_CERT_UNTRUSTED 27
3047#define X509_V_ERR_CERT_REJECTED 28
3048// These are 'informational' when looking for issuer cert
3049#define X509_V_ERR_SUBJECT_ISSUER_MISMATCH 29
3050#define X509_V_ERR_AKID_SKID_MISMATCH 30
3051#define X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH 31
3052#define X509_V_ERR_KEYUSAGE_NO_CERTSIGN 32
3053
3054#define X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER 33
3055#define X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION 34
3056#define X509_V_ERR_KEYUSAGE_NO_CRL_SIGN 35
3057#define X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION 36
3058#define X509_V_ERR_INVALID_NON_CA 37
3059#define X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED 38
3060#define X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE 39
3061#define X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED 40
3062
3063#define X509_V_ERR_INVALID_EXTENSION 41
3064#define X509_V_ERR_INVALID_POLICY_EXTENSION 42
3065#define X509_V_ERR_NO_EXPLICIT_POLICY 43
3066#define X509_V_ERR_DIFFERENT_CRL_SCOPE 44
3067#define X509_V_ERR_UNSUPPORTED_EXTENSION_FEATURE 45
3068
3069#define X509_V_ERR_UNNESTED_RESOURCE 46
3070
3071#define X509_V_ERR_PERMITTED_VIOLATION 47
3072#define X509_V_ERR_EXCLUDED_VIOLATION 48
3073#define X509_V_ERR_SUBTREE_MINMAX 49
3074#define X509_V_ERR_APPLICATION_VERIFICATION 50
3075#define X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE 51
3076#define X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX 52
3077#define X509_V_ERR_UNSUPPORTED_NAME_SYNTAX 53
3078#define X509_V_ERR_CRL_PATH_VALIDATION_ERROR 54
3079
David Benjaminc31a8a62021-10-03 22:47:13 -04003080// Host, email and IP check errors
3081#define X509_V_ERR_HOSTNAME_MISMATCH 62
3082#define X509_V_ERR_EMAIL_MISMATCH 63
3083#define X509_V_ERR_IP_ADDRESS_MISMATCH 64
3084
3085// Caller error
3086#define X509_V_ERR_INVALID_CALL 65
3087// Issuer lookup error
3088#define X509_V_ERR_STORE_LOOKUP 66
3089
3090#define X509_V_ERR_NAME_CONSTRAINTS_WITHOUT_SANS 67
3091
3092// Certificate verify flags
3093
3094// Send issuer+subject checks to verify_cb
3095#define X509_V_FLAG_CB_ISSUER_CHECK 0x1
3096// Use check time instead of current time
3097#define X509_V_FLAG_USE_CHECK_TIME 0x2
3098// Lookup CRLs
3099#define X509_V_FLAG_CRL_CHECK 0x4
3100// Lookup CRLs for whole chain
3101#define X509_V_FLAG_CRL_CHECK_ALL 0x8
3102// Ignore unhandled critical extensions
3103#define X509_V_FLAG_IGNORE_CRITICAL 0x10
3104// Does nothing as its functionality has been enabled by default.
3105#define X509_V_FLAG_X509_STRICT 0x00
Bob Beck503ba982023-02-14 11:08:56 -07003106// This flag does nothing as proxy certificate support has been removed.
David Benjaminc31a8a62021-10-03 22:47:13 -04003107#define X509_V_FLAG_ALLOW_PROXY_CERTS 0x40
David Benjamin28226f52023-03-29 14:04:44 +09003108// Does nothing as its functionality has been enabled by default.
David Benjaminc31a8a62021-10-03 22:47:13 -04003109#define X509_V_FLAG_POLICY_CHECK 0x80
3110// Policy variable require-explicit-policy
3111#define X509_V_FLAG_EXPLICIT_POLICY 0x100
3112// Policy variable inhibit-any-policy
3113#define X509_V_FLAG_INHIBIT_ANY 0x200
3114// Policy variable inhibit-policy-mapping
3115#define X509_V_FLAG_INHIBIT_MAP 0x400
3116// Notify callback that policy is OK
3117#define X509_V_FLAG_NOTIFY_POLICY 0x800
David Benjaminf8614992023-11-12 13:43:33 -05003118// Causes all verifications to fail. Extended CRL features have been removed.
David Benjaminc31a8a62021-10-03 22:47:13 -04003119#define X509_V_FLAG_EXTENDED_CRL_SUPPORT 0x1000
David Benjaminf8614992023-11-12 13:43:33 -05003120// Causes all verifications to fail. Delta CRL support has been removed.
David Benjaminc31a8a62021-10-03 22:47:13 -04003121#define X509_V_FLAG_USE_DELTAS 0x2000
3122// Check selfsigned CA signature
3123#define X509_V_FLAG_CHECK_SS_SIGNATURE 0x4000
3124// Use trusted store first
3125#define X509_V_FLAG_TRUSTED_FIRST 0x8000
David Benjaminc31a8a62021-10-03 22:47:13 -04003126
3127// Allow partial chains if at least one certificate is in trusted store
3128#define X509_V_FLAG_PARTIAL_CHAIN 0x80000
3129
3130// If the initial chain is not trusted, do not attempt to build an alternative
3131// chain. Alternate chain checking was introduced in 1.0.2b. Setting this flag
3132// will force the behaviour to match that of previous versions.
3133#define X509_V_FLAG_NO_ALT_CHAINS 0x100000
3134
David Benjamineccd1032022-08-12 15:12:29 -04003135// X509_V_FLAG_NO_CHECK_TIME disables all time checks in certificate
3136// verification.
3137#define X509_V_FLAG_NO_CHECK_TIME 0x200000
3138
David Benjaminc31a8a62021-10-03 22:47:13 -04003139#define X509_VP_FLAG_DEFAULT 0x1
3140#define X509_VP_FLAG_OVERWRITE 0x2
3141#define X509_VP_FLAG_RESET_FLAGS 0x4
3142#define X509_VP_FLAG_LOCKED 0x8
3143#define X509_VP_FLAG_ONCE 0x10
3144
David Benjaminc2b7df52023-11-18 19:31:24 -05003145// X509_OBJECT_new returns a newly-allocated, empty |X509_OBJECT| or NULL on
3146// error.
3147OPENSSL_EXPORT X509_OBJECT *X509_OBJECT_new(void);
3148
3149// X509_OBJECT_free releases memory associated with |obj|.
3150OPENSSL_EXPORT void X509_OBJECT_free(X509_OBJECT *obj);
3151
3152// X509_OBJECT_get_type returns the type of |obj|, which will be one of the
3153// |X509_LU_*| constants.
3154OPENSSL_EXPORT int X509_OBJECT_get_type(const X509_OBJECT *obj);
3155
3156// X509_OBJECT_get0_X509 returns |obj| as a certificate, or NULL if |obj| is not
3157// a certificate.
3158OPENSSL_EXPORT X509 *X509_OBJECT_get0_X509(const X509_OBJECT *obj);
3159
David Benjamin2ba76342023-11-19 10:20:04 -05003160// X509_STORE_new returns a newly-allocated |X509_STORE|, or NULL on error.
David Benjaminc31a8a62021-10-03 22:47:13 -04003161OPENSSL_EXPORT X509_STORE *X509_STORE_new(void);
David Benjamin2ba76342023-11-19 10:20:04 -05003162
3163// X509_STORE_up_ref adds one to the reference count of |store| and returns one.
David Benjaminc31a8a62021-10-03 22:47:13 -04003164OPENSSL_EXPORT int X509_STORE_up_ref(X509_STORE *store);
David Benjamin2ba76342023-11-19 10:20:04 -05003165
3166// X509_STORE_free releases memory associated with |store|.
3167OPENSSL_EXPORT void X509_STORE_free(X509_STORE *store);
David Benjaminc31a8a62021-10-03 22:47:13 -04003168
3169OPENSSL_EXPORT STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *st);
3170OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_get1_certs(X509_STORE_CTX *st,
3171 X509_NAME *nm);
3172OPENSSL_EXPORT STACK_OF(X509_CRL) *X509_STORE_get1_crls(X509_STORE_CTX *st,
3173 X509_NAME *nm);
3174OPENSSL_EXPORT int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags);
3175OPENSSL_EXPORT int X509_STORE_set_purpose(X509_STORE *ctx, int purpose);
3176OPENSSL_EXPORT int X509_STORE_set_trust(X509_STORE *ctx, int trust);
3177OPENSSL_EXPORT int X509_STORE_set1_param(X509_STORE *ctx,
3178 X509_VERIFY_PARAM *pm);
David Benjamin2ba76342023-11-19 10:20:04 -05003179
3180// X509_STORE_get0_param returns |store|'s default verification parameters. This
3181// object is mutable and may be modified by the caller.
3182//
3183// TODO(crbug.com/boringssl/441): Discuss the semantics of this notion of
3184// "default".
3185OPENSSL_EXPORT X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *store);
David Benjaminc31a8a62021-10-03 22:47:13 -04003186
Bob Beck706846d2023-04-19 17:43:48 -06003187// X509_STORE_set_verify_cb acts like |X509_STORE_CTX_set_verify_cb| but sets
3188// the verify callback for any |X509_STORE_CTX| created from this |X509_STORE|
3189//
3190// Do not use this funciton. see |X509_STORE_CTX_set_verify_cb|.
David Benjaminc31a8a62021-10-03 22:47:13 -04003191OPENSSL_EXPORT void X509_STORE_set_verify_cb(
3192 X509_STORE *ctx, X509_STORE_CTX_verify_cb verify_cb);
3193#define X509_STORE_set_verify_cb_func(ctx, func) \
3194 X509_STORE_set_verify_cb((ctx), (func))
3195OPENSSL_EXPORT X509_STORE_CTX_verify_cb
3196X509_STORE_get_verify_cb(X509_STORE *ctx);
3197OPENSSL_EXPORT void X509_STORE_set_get_issuer(
3198 X509_STORE *ctx, X509_STORE_CTX_get_issuer_fn get_issuer);
3199OPENSSL_EXPORT X509_STORE_CTX_get_issuer_fn
3200X509_STORE_get_get_issuer(X509_STORE *ctx);
David Benjaminc31a8a62021-10-03 22:47:13 -04003201OPENSSL_EXPORT void X509_STORE_set_get_crl(X509_STORE *ctx,
3202 X509_STORE_CTX_get_crl_fn get_crl);
3203OPENSSL_EXPORT X509_STORE_CTX_get_crl_fn
3204X509_STORE_get_get_crl(X509_STORE *ctx);
3205OPENSSL_EXPORT void X509_STORE_set_check_crl(
3206 X509_STORE *ctx, X509_STORE_CTX_check_crl_fn check_crl);
3207OPENSSL_EXPORT X509_STORE_CTX_check_crl_fn
3208X509_STORE_get_check_crl(X509_STORE *ctx);
David Benjaminc31a8a62021-10-03 22:47:13 -04003209
3210OPENSSL_EXPORT X509_STORE_CTX *X509_STORE_CTX_new(void);
3211
3212OPENSSL_EXPORT int X509_STORE_CTX_get1_issuer(X509 **issuer,
3213 X509_STORE_CTX *ctx, X509 *x);
3214
David Benjamin2ba76342023-11-19 10:20:04 -05003215// X509_STORE_CTX_free releases memory associated with |ctx|.
David Benjaminc31a8a62021-10-03 22:47:13 -04003216OPENSSL_EXPORT void X509_STORE_CTX_free(X509_STORE_CTX *ctx);
David Benjamin2ba76342023-11-19 10:20:04 -05003217
David Benjaminc31a8a62021-10-03 22:47:13 -04003218OPENSSL_EXPORT int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store,
3219 X509 *x509, STACK_OF(X509) *chain);
David Benjamin2135ac62022-08-12 15:33:43 -04003220
3221// X509_STORE_CTX_set0_trusted_stack configures |ctx| to trust the certificates
3222// in |sk|. |sk| must remain valid for the duration of |ctx|.
3223//
3224// WARNING: This function differs from most |set0| functions in that it does not
3225// take ownership of its input. The caller is required to ensure the lifetimes
3226// are consistent.
3227OPENSSL_EXPORT void X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX *ctx,
3228 STACK_OF(X509) *sk);
3229
3230// X509_STORE_CTX_trusted_stack is a deprecated alias for
3231// |X509_STORE_CTX_set0_trusted_stack|.
David Benjaminc31a8a62021-10-03 22:47:13 -04003232OPENSSL_EXPORT void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx,
3233 STACK_OF(X509) *sk);
David Benjamin2135ac62022-08-12 15:33:43 -04003234
David Benjamin2ba76342023-11-19 10:20:04 -05003235// X509_STORE_CTX_get0_store returns the |X509_STORE| that |ctx| uses.
David Benjaminc31a8a62021-10-03 22:47:13 -04003236OPENSSL_EXPORT X509_STORE *X509_STORE_CTX_get0_store(X509_STORE_CTX *ctx);
David Benjamin2ba76342023-11-19 10:20:04 -05003237
3238// X509_STORE_CTX_get0_cert returns the leaf certificate that |ctx| is
3239// verifying.
David Benjaminc31a8a62021-10-03 22:47:13 -04003240OPENSSL_EXPORT X509 *X509_STORE_CTX_get0_cert(X509_STORE_CTX *ctx);
3241
3242OPENSSL_EXPORT X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *v,
David Benjamin5a827022023-11-22 01:36:12 -05003243 const X509_LOOKUP_METHOD *m);
David Benjaminc31a8a62021-10-03 22:47:13 -04003244
David Benjamin5a827022023-11-22 01:36:12 -05003245OPENSSL_EXPORT const X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void);
3246OPENSSL_EXPORT const X509_LOOKUP_METHOD *X509_LOOKUP_file(void);
David Benjaminc31a8a62021-10-03 22:47:13 -04003247
3248OPENSSL_EXPORT int X509_STORE_add_cert(X509_STORE *ctx, X509 *x);
3249OPENSSL_EXPORT int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x);
3250
3251OPENSSL_EXPORT int X509_STORE_get_by_subject(X509_STORE_CTX *vs, int type,
3252 X509_NAME *name, X509_OBJECT *ret);
3253
3254OPENSSL_EXPORT int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc,
3255 long argl, char **ret);
3256
David Benjaminc31a8a62021-10-03 22:47:13 -04003257OPENSSL_EXPORT int X509_load_cert_file(X509_LOOKUP *ctx, const char *file,
3258 int type);
3259OPENSSL_EXPORT int X509_load_crl_file(X509_LOOKUP *ctx, const char *file,
3260 int type);
3261OPENSSL_EXPORT int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file,
3262 int type);
David Benjaminc31a8a62021-10-03 22:47:13 -04003263
David Benjaminc31a8a62021-10-03 22:47:13 -04003264OPENSSL_EXPORT int X509_STORE_load_locations(X509_STORE *ctx, const char *file,
3265 const char *dir);
3266OPENSSL_EXPORT int X509_STORE_set_default_paths(X509_STORE *ctx);
David Benjaminc31a8a62021-10-03 22:47:13 -04003267OPENSSL_EXPORT int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx);
3268OPENSSL_EXPORT void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int s);
3269OPENSSL_EXPORT int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx);
3270OPENSSL_EXPORT X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx);
3271OPENSSL_EXPORT X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx);
3272OPENSSL_EXPORT X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx);
David Benjaminc31a8a62021-10-03 22:47:13 -04003273OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx);
3274OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get0_chain(X509_STORE_CTX *ctx);
3275OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx);
3276OPENSSL_EXPORT void X509_STORE_CTX_set_cert(X509_STORE_CTX *c, X509 *x);
3277OPENSSL_EXPORT void X509_STORE_CTX_set_chain(X509_STORE_CTX *c,
3278 STACK_OF(X509) *sk);
3279OPENSSL_EXPORT STACK_OF(X509) *X509_STORE_CTX_get0_untrusted(
3280 X509_STORE_CTX *ctx);
3281OPENSSL_EXPORT void X509_STORE_CTX_set0_crls(X509_STORE_CTX *c,
3282 STACK_OF(X509_CRL) *sk);
3283OPENSSL_EXPORT int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose);
3284OPENSSL_EXPORT int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust);
3285OPENSSL_EXPORT int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx,
3286 int def_purpose, int purpose,
3287 int trust);
3288OPENSSL_EXPORT void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx,
3289 unsigned long flags);
David Benjamin2ba76342023-11-19 10:20:04 -05003290
3291// X509_STORE_CTX_set_time configures certificate verification to use |t|
3292// instead of the current time. |flags| is ignored and should be zero.
David Benjaminc31a8a62021-10-03 22:47:13 -04003293OPENSSL_EXPORT void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx,
3294 unsigned long flags, time_t t);
David Benjamin2ba76342023-11-19 10:20:04 -05003295
3296// X509_STORE_CTX_set_time_posix configures certificate verification to use |t|
3297// instead of the current time. |t| is interpreted as a POSIX timestamp in
3298// seconds. |flags| is ignored and should be zero.
Bob Beck6e20b772023-02-08 11:32:19 -07003299OPENSSL_EXPORT void X509_STORE_CTX_set_time_posix(X509_STORE_CTX *ctx,
3300 unsigned long flags,
3301 int64_t t);
Bob Beck706846d2023-04-19 17:43:48 -06003302
3303// X509_STORE_CTX_set_verify_cb configures a callback function for |ctx| that is
3304// called multiple times during |X509_verify_cert|. The callback returns zero to
3305// fail verification and non-zero to proceed. Typically, it will return |ok|,
3306// which preserves the default behavior. Returning one when |ok| is zero will
3307// proceed past some error. The callback may inspect |ctx| and the error queue
3308// to attempt to determine the current stage of certificate verification, but
3309// this is often unreliable.
3310//
3311// WARNING: Do not use this function. It is extremely fragile and unpredictable.
3312// This callback exposes implementation details of certificate verification,
3313// which change as the library evolves. Attempting to use it for security checks
3314// can introduce vulnerabilities if making incorrect assumptions about when the
3315// callback is called. Additionally, overriding |ok| may leave |ctx| in an
3316// inconsistent state and break invariants.
3317//
3318// Instead, customize certificate verification by configuring options on the
3319// |X509_STORE_CTX| before verification, or applying additional checks after
3320// |X509_verify_cert| completes successfully.
David Benjaminc31a8a62021-10-03 22:47:13 -04003321OPENSSL_EXPORT void X509_STORE_CTX_set_verify_cb(
Bob Beck706846d2023-04-19 17:43:48 -06003322 X509_STORE_CTX *ctx, int (*verify_cb)(int ok, X509_STORE_CTX *ctx));
David Benjaminc31a8a62021-10-03 22:47:13 -04003323
David Benjamin2ba76342023-11-19 10:20:04 -05003324// X509_STORE_CTX_get0_param returns |ctx|'s verification parameters. This
3325// object is mutable and may be modified by the caller.
David Benjaminc31a8a62021-10-03 22:47:13 -04003326OPENSSL_EXPORT X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(
3327 X509_STORE_CTX *ctx);
David Benjamin2ba76342023-11-19 10:20:04 -05003328
3329// X509_STORE_CTX_set0_param returns |ctx|'s verification parameters to |param|
3330// and takes ownership of |param|. After this function returns, the caller
3331// should not free |param|.
3332//
3333// TODO(crbug.com/boringssl/441): The bug notes some odd interactions with
3334// the different notions of default. Discuss this.
David Benjaminc31a8a62021-10-03 22:47:13 -04003335OPENSSL_EXPORT void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx,
3336 X509_VERIFY_PARAM *param);
David Benjamin2ba76342023-11-19 10:20:04 -05003337
David Benjaminc31a8a62021-10-03 22:47:13 -04003338OPENSSL_EXPORT int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx,
3339 const char *name);
3340
3341// X509_VERIFY_PARAM functions
3342
David Benjamin2ba76342023-11-19 10:20:04 -05003343// X509_VERIFY_PARAM_new returns a newly-allocated |X509_VERIFY_PARAM|, or NULL
3344// on error.
David Benjaminc31a8a62021-10-03 22:47:13 -04003345OPENSSL_EXPORT X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void);
David Benjamin2ba76342023-11-19 10:20:04 -05003346
3347// X509_VERIFY_PARAM_free releases memory associated with |param|.
David Benjaminc31a8a62021-10-03 22:47:13 -04003348OPENSSL_EXPORT void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param);
David Benjamin2ba76342023-11-19 10:20:04 -05003349
David Benjaminc31a8a62021-10-03 22:47:13 -04003350OPENSSL_EXPORT int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *to,
3351 const X509_VERIFY_PARAM *from);
3352OPENSSL_EXPORT int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to,
3353 const X509_VERIFY_PARAM *from);
David Benjaminc31a8a62021-10-03 22:47:13 -04003354OPENSSL_EXPORT int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param,
3355 unsigned long flags);
3356OPENSSL_EXPORT int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param,
3357 unsigned long flags);
3358OPENSSL_EXPORT unsigned long X509_VERIFY_PARAM_get_flags(
3359 X509_VERIFY_PARAM *param);
3360OPENSSL_EXPORT int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param,
3361 int purpose);
3362OPENSSL_EXPORT int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param,
3363 int trust);
3364OPENSSL_EXPORT void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param,
3365 int depth);
David Benjamin2ba76342023-11-19 10:20:04 -05003366
3367// X509_VERIFY_PARAM_set_time configures certificate verification to use |t|
3368// instead of the current time.
David Benjaminc31a8a62021-10-03 22:47:13 -04003369OPENSSL_EXPORT void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param,
3370 time_t t);
David Benjamin2ba76342023-11-19 10:20:04 -05003371
3372// X509_VERIFY_PARAM_set_time_posix configures certificate verification to use
3373// |t| instead of the current time. |t| is interpreted as a POSIX timestamp in
3374// seconds.
Bob Beck6e20b772023-02-08 11:32:19 -07003375OPENSSL_EXPORT void X509_VERIFY_PARAM_set_time_posix(X509_VERIFY_PARAM *param,
3376 int64_t t);
David Benjamin2ba76342023-11-19 10:20:04 -05003377
3378// X509_VERIFY_PARAM_add0_policy adds |policy| to the user-initial-policy-set
3379// (see Section 6.1.1 of RFC 5280). On success, it takes ownership of
3380// |policy| and returns one. Otherwise, it returns zero and the caller retains
3381// owneship of |policy|.
David Benjaminc31a8a62021-10-03 22:47:13 -04003382OPENSSL_EXPORT int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param,
3383 ASN1_OBJECT *policy);
David Benjamin2ba76342023-11-19 10:20:04 -05003384
3385// X509_VERIFY_PARAM_set1_policies sets the user-initial-policy-set (see
3386// Section 6.1.1 of RFC 5280) to a copy of |policies|. It returns one on success
3387// and zero on error.
David Benjaminc31a8a62021-10-03 22:47:13 -04003388OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_policies(
David Benjamin42ba95f2022-12-13 14:17:45 -05003389 X509_VERIFY_PARAM *param, const STACK_OF(ASN1_OBJECT) *policies);
David Benjaminc31a8a62021-10-03 22:47:13 -04003390
David Benjamin2ba76342023-11-19 10:20:04 -05003391// X509_VERIFY_PARAM_set1_host configures |param| to check for the DNS name
3392// specified by |name|. It returns one on success and zero on error.
David Benjaminc31a8a62021-10-03 22:47:13 -04003393OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param,
3394 const char *name,
David Benjamin2ba76342023-11-19 10:20:04 -05003395 size_t name_len);
3396
3397// X509_VERIFY_PARAM_add1_host adds |name| to the list of names checked by
3398// |param|. If any configured DNS name matches the certificate, verification
3399// succeeds. It returns one on success and zero on error.
David Benjaminc31a8a62021-10-03 22:47:13 -04003400OPENSSL_EXPORT int X509_VERIFY_PARAM_add1_host(X509_VERIFY_PARAM *param,
3401 const char *name,
David Benjamin2ba76342023-11-19 10:20:04 -05003402 size_t name_len);
3403
3404// X509_VERIFY_PARAM_set_hostflags sets the name-checking flags on |param| to
3405// |flags|. |flags| should be a combination of |X509_CHECK_FLAG_*| constants.
David Benjaminc31a8a62021-10-03 22:47:13 -04003406OPENSSL_EXPORT void X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param,
3407 unsigned int flags);
David Benjamin2ba76342023-11-19 10:20:04 -05003408
3409OPENSSL_EXPORT char *X509_VERIFY_PARAM_get0_peername(X509_VERIFY_PARAM *param);
3410
3411// X509_VERIFY_PARAM_set1_email configures |param| to check for the email
3412// address specified by |email|. It returns one on success and zero on error.
David Benjaminc31a8a62021-10-03 22:47:13 -04003413OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *param,
3414 const char *email,
David Benjamin2ba76342023-11-19 10:20:04 -05003415 size_t email_len);
3416
3417// X509_VERIFY_PARAM_set1_ip configures |param| to check for the IP address
3418// specified by |ip|. It returns one on success and zero on error. The IP
3419// address is specified in its binary representation. |ip_len| must be 4 for an
3420// IPv4 address and 16 for an IPv6 address.
David Benjaminc31a8a62021-10-03 22:47:13 -04003421OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *param,
David Benjamin2ba76342023-11-19 10:20:04 -05003422 const uint8_t *ip, size_t ip_len);
3423
3424// X509_VERIFY_PARAM_set1_ip_asc decodes |ipasc| as the ASCII representation of
3425// an IPv4 or IPv6 address, and configures |param| to check for it. It returns
3426// one on success and zero on error.
David Benjaminc31a8a62021-10-03 22:47:13 -04003427OPENSSL_EXPORT int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param,
3428 const char *ipasc);
3429
3430OPENSSL_EXPORT int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param);
David Benjaminc31a8a62021-10-03 22:47:13 -04003431
David Benjamin790f96b2023-11-19 10:28:36 -05003432// X509_VERIFY_PARAM_lookup returns a pre-defined |X509_VERIFY_PARAM| named by
3433// |name|, or NULL if no such name is defined.
David Benjaminc31a8a62021-10-03 22:47:13 -04003434OPENSSL_EXPORT const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(
3435 const char *name);
David Benjaminc31a8a62021-10-03 22:47:13 -04003436
David Benjamin58906ea2023-11-20 23:59:49 -05003437// Forward reference
3438struct v3_ext_method;
3439struct v3_ext_ctx;
3440
3441// Useful typedefs
3442
3443typedef struct v3_ext_method X509V3_EXT_METHOD;
3444
3445typedef void *(*X509V3_EXT_NEW)(void);
3446typedef void (*X509V3_EXT_FREE)(void *);
3447typedef void *(*X509V3_EXT_D2I)(void *, const unsigned char **, long);
3448typedef int (*X509V3_EXT_I2D)(void *, unsigned char **);
3449typedef STACK_OF(CONF_VALUE) *(*X509V3_EXT_I2V)(const X509V3_EXT_METHOD *method,
3450 void *ext,
3451 STACK_OF(CONF_VALUE) *extlist);
3452typedef void *(*X509V3_EXT_V2I)(const X509V3_EXT_METHOD *method,
3453 const X509V3_CTX *ctx,
3454 const STACK_OF(CONF_VALUE) *values);
3455typedef char *(*X509V3_EXT_I2S)(const X509V3_EXT_METHOD *method, void *ext);
3456typedef void *(*X509V3_EXT_S2I)(const X509V3_EXT_METHOD *method,
3457 const X509V3_CTX *ctx, const char *str);
3458typedef int (*X509V3_EXT_I2R)(const X509V3_EXT_METHOD *method, void *ext,
3459 BIO *out, int indent);
3460typedef void *(*X509V3_EXT_R2I)(const X509V3_EXT_METHOD *method,
3461 const X509V3_CTX *ctx, const char *str);
3462
3463// V3 extension structure
3464
3465struct v3_ext_method {
3466 int ext_nid;
3467 int ext_flags;
3468
3469 // it determines how values of this extension are allocated, released, parsed,
3470 // and marshalled. This must be non-NULL.
3471 ASN1_ITEM_EXP *it;
3472
3473 // The following functions are ignored in favor of |it|. They are retained in
3474 // the struct only for source compatibility with existing struct definitions.
3475 X509V3_EXT_NEW ext_new;
3476 X509V3_EXT_FREE ext_free;
3477 X509V3_EXT_D2I d2i;
3478 X509V3_EXT_I2D i2d;
3479
3480 // The following pair is used for string extensions
3481 X509V3_EXT_I2S i2s;
3482 X509V3_EXT_S2I s2i;
3483
3484 // The following pair is used for multi-valued extensions
3485 X509V3_EXT_I2V i2v;
3486 X509V3_EXT_V2I v2i;
3487
3488 // The following are used for raw extensions
3489 X509V3_EXT_I2R i2r;
3490 X509V3_EXT_R2I r2i;
3491
3492 void *usr_data; // Any extension specific data
3493};
3494
3495DEFINE_STACK_OF(X509V3_EXT_METHOD)
3496
3497// ext_flags values
3498#define X509V3_EXT_CTX_DEP 0x2
3499#define X509V3_EXT_MULTILINE 0x4
3500
3501struct BASIC_CONSTRAINTS_st {
3502 int ca;
3503 ASN1_INTEGER *pathlen;
3504};
3505
3506
3507typedef struct otherName_st {
3508 ASN1_OBJECT *type_id;
3509 ASN1_TYPE *value;
3510} OTHERNAME;
3511
3512typedef struct EDIPartyName_st {
3513 ASN1_STRING *nameAssigner;
3514 ASN1_STRING *partyName;
3515} EDIPARTYNAME;
3516
David Benjamin2d7adae2023-11-21 00:11:09 -05003517struct GENERAL_NAME_st {
David Benjamin58906ea2023-11-20 23:59:49 -05003518#define GEN_OTHERNAME 0
3519#define GEN_EMAIL 1
3520#define GEN_DNS 2
3521#define GEN_X400 3
3522#define GEN_DIRNAME 4
3523#define GEN_EDIPARTY 5
3524#define GEN_URI 6
3525#define GEN_IPADD 7
3526#define GEN_RID 8
3527
3528 int type;
3529 union {
3530 char *ptr;
3531 OTHERNAME *otherName; // otherName
3532 ASN1_IA5STRING *rfc822Name;
3533 ASN1_IA5STRING *dNSName;
3534 ASN1_STRING *x400Address;
3535 X509_NAME *directoryName;
3536 EDIPARTYNAME *ediPartyName;
3537 ASN1_IA5STRING *uniformResourceIdentifier;
3538 ASN1_OCTET_STRING *iPAddress;
3539 ASN1_OBJECT *registeredID;
3540
3541 // Old names
3542 ASN1_OCTET_STRING *ip; // iPAddress
3543 X509_NAME *dirn; // dirn
3544 ASN1_IA5STRING *ia5; // rfc822Name, dNSName, uniformResourceIdentifier
3545 ASN1_OBJECT *rid; // registeredID
3546 } d;
David Benjamin2d7adae2023-11-21 00:11:09 -05003547} /* GENERAL_NAME */;
David Benjamin58906ea2023-11-20 23:59:49 -05003548
3549DEFINE_STACK_OF(GENERAL_NAME)
3550
3551typedef STACK_OF(GENERAL_NAME) GENERAL_NAMES;
3552
3553DEFINE_STACK_OF(GENERAL_NAMES)
3554
3555typedef struct ACCESS_DESCRIPTION_st {
3556 ASN1_OBJECT *method;
3557 GENERAL_NAME *location;
3558} ACCESS_DESCRIPTION;
3559
3560DEFINE_STACK_OF(ACCESS_DESCRIPTION)
3561
3562typedef STACK_OF(ACCESS_DESCRIPTION) AUTHORITY_INFO_ACCESS;
3563
3564typedef STACK_OF(ASN1_OBJECT) EXTENDED_KEY_USAGE;
3565
3566typedef struct DIST_POINT_NAME_st {
3567 int type;
3568 union {
3569 GENERAL_NAMES *fullname;
3570 STACK_OF(X509_NAME_ENTRY) *relativename;
3571 } name;
3572 // If relativename then this contains the full distribution point name
3573 X509_NAME *dpname;
3574} DIST_POINT_NAME;
3575// All existing reasons
3576#define CRLDP_ALL_REASONS 0x807f
3577
3578#define CRL_REASON_NONE (-1)
3579#define CRL_REASON_UNSPECIFIED 0
3580#define CRL_REASON_KEY_COMPROMISE 1
3581#define CRL_REASON_CA_COMPROMISE 2
3582#define CRL_REASON_AFFILIATION_CHANGED 3
3583#define CRL_REASON_SUPERSEDED 4
3584#define CRL_REASON_CESSATION_OF_OPERATION 5
3585#define CRL_REASON_CERTIFICATE_HOLD 6
3586#define CRL_REASON_REMOVE_FROM_CRL 8
3587#define CRL_REASON_PRIVILEGE_WITHDRAWN 9
3588#define CRL_REASON_AA_COMPROMISE 10
3589
3590struct DIST_POINT_st {
3591 DIST_POINT_NAME *distpoint;
3592 ASN1_BIT_STRING *reasons;
3593 GENERAL_NAMES *CRLissuer;
3594};
3595
3596typedef STACK_OF(DIST_POINT) CRL_DIST_POINTS;
3597
3598DEFINE_STACK_OF(DIST_POINT)
3599
3600struct AUTHORITY_KEYID_st {
3601 ASN1_OCTET_STRING *keyid;
3602 GENERAL_NAMES *issuer;
3603 ASN1_INTEGER *serial;
3604};
3605
3606typedef struct NOTICEREF_st {
3607 ASN1_STRING *organization;
3608 STACK_OF(ASN1_INTEGER) *noticenos;
3609} NOTICEREF;
3610
3611typedef struct USERNOTICE_st {
3612 NOTICEREF *noticeref;
3613 ASN1_STRING *exptext;
3614} USERNOTICE;
3615
3616typedef struct POLICYQUALINFO_st {
3617 ASN1_OBJECT *pqualid;
3618 union {
3619 ASN1_IA5STRING *cpsuri;
3620 USERNOTICE *usernotice;
3621 ASN1_TYPE *other;
3622 } d;
3623} POLICYQUALINFO;
3624
3625DEFINE_STACK_OF(POLICYQUALINFO)
3626
3627typedef struct POLICYINFO_st {
3628 ASN1_OBJECT *policyid;
3629 STACK_OF(POLICYQUALINFO) *qualifiers;
3630} POLICYINFO;
3631
3632typedef STACK_OF(POLICYINFO) CERTIFICATEPOLICIES;
3633
3634DEFINE_STACK_OF(POLICYINFO)
3635
3636typedef struct POLICY_MAPPING_st {
3637 ASN1_OBJECT *issuerDomainPolicy;
3638 ASN1_OBJECT *subjectDomainPolicy;
3639} POLICY_MAPPING;
3640
3641DEFINE_STACK_OF(POLICY_MAPPING)
3642
3643typedef STACK_OF(POLICY_MAPPING) POLICY_MAPPINGS;
3644
3645typedef struct GENERAL_SUBTREE_st {
3646 GENERAL_NAME *base;
3647 ASN1_INTEGER *minimum;
3648 ASN1_INTEGER *maximum;
3649} GENERAL_SUBTREE;
3650
3651DEFINE_STACK_OF(GENERAL_SUBTREE)
3652
3653struct NAME_CONSTRAINTS_st {
3654 STACK_OF(GENERAL_SUBTREE) *permittedSubtrees;
3655 STACK_OF(GENERAL_SUBTREE) *excludedSubtrees;
3656};
3657
3658typedef struct POLICY_CONSTRAINTS_st {
3659 ASN1_INTEGER *requireExplicitPolicy;
3660 ASN1_INTEGER *inhibitPolicyMapping;
3661} POLICY_CONSTRAINTS;
3662
3663struct ISSUING_DIST_POINT_st {
3664 DIST_POINT_NAME *distpoint;
3665 ASN1_BOOLEAN onlyuser;
3666 ASN1_BOOLEAN onlyCA;
3667 ASN1_BIT_STRING *onlysomereasons;
3668 ASN1_BOOLEAN indirectCRL;
3669 ASN1_BOOLEAN onlyattr;
3670};
3671
3672// X509_PURPOSE stuff
3673
3674#define EXFLAG_BCONS 0x1
3675#define EXFLAG_KUSAGE 0x2
3676#define EXFLAG_XKUSAGE 0x4
3677#define EXFLAG_NSCERT 0x8
3678
3679#define EXFLAG_CA 0x10
3680// Really self issued not necessarily self signed
3681#define EXFLAG_SI 0x20
3682#define EXFLAG_V1 0x40
3683#define EXFLAG_INVALID 0x80
3684#define EXFLAG_SET 0x100
3685#define EXFLAG_CRITICAL 0x200
3686
3687// Self signed
3688#define EXFLAG_SS 0x2000
3689
3690#define KU_DIGITAL_SIGNATURE 0x0080
3691#define KU_NON_REPUDIATION 0x0040
3692#define KU_KEY_ENCIPHERMENT 0x0020
3693#define KU_DATA_ENCIPHERMENT 0x0010
3694#define KU_KEY_AGREEMENT 0x0008
3695#define KU_KEY_CERT_SIGN 0x0004
3696#define KU_CRL_SIGN 0x0002
3697#define KU_ENCIPHER_ONLY 0x0001
3698#define KU_DECIPHER_ONLY 0x8000
3699
3700#define NS_SSL_CLIENT 0x80
3701#define NS_SSL_SERVER 0x40
3702#define NS_SMIME 0x20
3703#define NS_OBJSIGN 0x10
3704#define NS_SSL_CA 0x04
3705#define NS_SMIME_CA 0x02
3706#define NS_OBJSIGN_CA 0x01
3707#define NS_ANY_CA (NS_SSL_CA | NS_SMIME_CA | NS_OBJSIGN_CA)
3708
3709#define XKU_SSL_SERVER 0x1
3710#define XKU_SSL_CLIENT 0x2
3711#define XKU_SMIME 0x4
3712#define XKU_CODE_SIGN 0x8
3713#define XKU_SGC 0x10
3714#define XKU_OCSP_SIGN 0x20
3715#define XKU_TIMESTAMP 0x40
3716#define XKU_DVCS 0x80
3717#define XKU_ANYEKU 0x100
3718
3719#define X509_PURPOSE_DYNAMIC 0x1
3720#define X509_PURPOSE_DYNAMIC_NAME 0x2
3721
3722typedef struct x509_purpose_st {
3723 int purpose;
3724 int trust; // Default trust ID
3725 int flags;
3726 int (*check_purpose)(const struct x509_purpose_st *, const X509 *, int);
3727 char *name;
3728 char *sname;
3729 void *usr_data;
3730} X509_PURPOSE;
3731
3732#define X509_PURPOSE_SSL_CLIENT 1
3733#define X509_PURPOSE_SSL_SERVER 2
3734#define X509_PURPOSE_NS_SSL_SERVER 3
3735#define X509_PURPOSE_SMIME_SIGN 4
3736#define X509_PURPOSE_SMIME_ENCRYPT 5
3737#define X509_PURPOSE_CRL_SIGN 6
3738#define X509_PURPOSE_ANY 7
3739#define X509_PURPOSE_OCSP_HELPER 8
3740#define X509_PURPOSE_TIMESTAMP_SIGN 9
3741
3742#define X509_PURPOSE_MIN 1
3743#define X509_PURPOSE_MAX 9
3744
3745DEFINE_STACK_OF(X509_PURPOSE)
3746
3747DECLARE_ASN1_FUNCTIONS_const(BASIC_CONSTRAINTS)
3748
3749// TODO(https://crbug.com/boringssl/407): This is not const because it contains
3750// an |X509_NAME|.
3751DECLARE_ASN1_FUNCTIONS(AUTHORITY_KEYID)
3752
3753// TODO(https://crbug.com/boringssl/407): This is not const because it contains
3754// an |X509_NAME|.
3755DECLARE_ASN1_FUNCTIONS(GENERAL_NAME)
3756OPENSSL_EXPORT GENERAL_NAME *GENERAL_NAME_dup(GENERAL_NAME *a);
3757
3758// i2v_GENERAL_NAME serializes |gen| as a |CONF_VALUE|. If |ret| is non-NULL, it
3759// appends the value to |ret| and returns |ret| on success or NULL on error. If
3760// it returns NULL, the caller is still responsible for freeing |ret|. If |ret|
3761// is NULL, it returns a newly-allocated |STACK_OF(CONF_VALUE)| containing the
3762// result. |method| is ignored.
3763//
3764// Do not use this function. This is an internal implementation detail of the
3765// human-readable print functions. If extracting a SAN list from a certificate,
3766// look at |gen| directly.
3767OPENSSL_EXPORT STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(
3768 const X509V3_EXT_METHOD *method, const GENERAL_NAME *gen,
3769 STACK_OF(CONF_VALUE) *ret);
3770
David Benjamin58906ea2023-11-20 23:59:49 -05003771// TODO(https://crbug.com/boringssl/407): This is not const because it contains
3772// an |X509_NAME|.
3773DECLARE_ASN1_FUNCTIONS(GENERAL_NAMES)
3774
3775// i2v_GENERAL_NAMES serializes |gen| as a list of |CONF_VALUE|s. If |ret| is
3776// non-NULL, it appends the values to |ret| and returns |ret| on success or NULL
3777// on error. If it returns NULL, the caller is still responsible for freeing
3778// |ret|. If |ret| is NULL, it returns a newly-allocated |STACK_OF(CONF_VALUE)|
3779// containing the results. |method| is ignored.
3780//
3781// Do not use this function. This is an internal implementation detail of the
3782// human-readable print functions. If extracting a SAN list from a certificate,
3783// look at |gen| directly.
3784OPENSSL_EXPORT STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES(
3785 const X509V3_EXT_METHOD *method, const GENERAL_NAMES *gen,
3786 STACK_OF(CONF_VALUE) *extlist);
3787OPENSSL_EXPORT GENERAL_NAMES *v2i_GENERAL_NAMES(
3788 const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx,
3789 const STACK_OF(CONF_VALUE) *nval);
3790
3791DECLARE_ASN1_FUNCTIONS_const(OTHERNAME)
3792DECLARE_ASN1_FUNCTIONS_const(EDIPARTYNAME)
3793OPENSSL_EXPORT void GENERAL_NAME_set0_value(GENERAL_NAME *a, int type,
3794 void *value);
3795OPENSSL_EXPORT void *GENERAL_NAME_get0_value(const GENERAL_NAME *a, int *ptype);
3796OPENSSL_EXPORT int GENERAL_NAME_set0_othername(GENERAL_NAME *gen,
3797 ASN1_OBJECT *oid,
3798 ASN1_TYPE *value);
3799OPENSSL_EXPORT int GENERAL_NAME_get0_otherName(const GENERAL_NAME *gen,
3800 ASN1_OBJECT **poid,
3801 ASN1_TYPE **pvalue);
3802
3803// i2s_ASN1_OCTET_STRING returns a human-readable representation of |oct| as a
3804// newly-allocated, NUL-terminated string, or NULL on error. |method| is
3805// ignored. The caller must release the result with |OPENSSL_free| when done.
3806OPENSSL_EXPORT char *i2s_ASN1_OCTET_STRING(const X509V3_EXT_METHOD *method,
3807 const ASN1_OCTET_STRING *oct);
3808
3809OPENSSL_EXPORT ASN1_OCTET_STRING *s2i_ASN1_OCTET_STRING(
3810 const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, const char *str);
3811
3812DECLARE_ASN1_FUNCTIONS_const(EXTENDED_KEY_USAGE)
3813OPENSSL_EXPORT int i2a_ACCESS_DESCRIPTION(BIO *bp, const ACCESS_DESCRIPTION *a);
3814
3815DECLARE_ASN1_FUNCTIONS_const(CERTIFICATEPOLICIES)
3816DECLARE_ASN1_FUNCTIONS_const(POLICYINFO)
3817DECLARE_ASN1_FUNCTIONS_const(POLICYQUALINFO)
3818DECLARE_ASN1_FUNCTIONS_const(USERNOTICE)
3819DECLARE_ASN1_FUNCTIONS_const(NOTICEREF)
3820
3821// TODO(https://crbug.com/boringssl/407): This is not const because it contains
3822// an |X509_NAME|.
3823DECLARE_ASN1_FUNCTIONS(CRL_DIST_POINTS)
3824// TODO(https://crbug.com/boringssl/407): This is not const because it contains
3825// an |X509_NAME|.
3826DECLARE_ASN1_FUNCTIONS(DIST_POINT)
3827// TODO(https://crbug.com/boringssl/407): This is not const because it contains
3828// an |X509_NAME|.
3829DECLARE_ASN1_FUNCTIONS(DIST_POINT_NAME)
3830// TODO(https://crbug.com/boringssl/407): This is not const because it contains
3831// an |X509_NAME|.
3832DECLARE_ASN1_FUNCTIONS(ISSUING_DIST_POINT)
3833
3834OPENSSL_EXPORT int DIST_POINT_set_dpname(DIST_POINT_NAME *dpn,
3835 X509_NAME *iname);
3836
3837OPENSSL_EXPORT int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc);
3838
3839// TODO(https://crbug.com/boringssl/407): This is not const because it contains
3840// an |X509_NAME|.
3841DECLARE_ASN1_FUNCTIONS(ACCESS_DESCRIPTION)
3842// TODO(https://crbug.com/boringssl/407): This is not const because it contains
3843// an |X509_NAME|.
3844DECLARE_ASN1_FUNCTIONS(AUTHORITY_INFO_ACCESS)
3845
3846DECLARE_ASN1_ITEM(POLICY_MAPPING)
3847DECLARE_ASN1_ALLOC_FUNCTIONS(POLICY_MAPPING)
3848DECLARE_ASN1_ITEM(POLICY_MAPPINGS)
3849
3850DECLARE_ASN1_ITEM(GENERAL_SUBTREE)
3851DECLARE_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE)
3852
3853DECLARE_ASN1_ITEM(NAME_CONSTRAINTS)
3854DECLARE_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS)
3855
3856DECLARE_ASN1_ALLOC_FUNCTIONS(POLICY_CONSTRAINTS)
3857DECLARE_ASN1_ITEM(POLICY_CONSTRAINTS)
3858
3859OPENSSL_EXPORT GENERAL_NAME *a2i_GENERAL_NAME(GENERAL_NAME *out,
3860 const X509V3_EXT_METHOD *method,
3861 const X509V3_CTX *ctx, int gen_type,
3862 const char *value, int is_nc);
3863
3864OPENSSL_EXPORT GENERAL_NAME *v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method,
3865 const X509V3_CTX *ctx,
3866 const CONF_VALUE *cnf);
3867OPENSSL_EXPORT GENERAL_NAME *v2i_GENERAL_NAME_ex(
3868 GENERAL_NAME *out, const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx,
3869 const CONF_VALUE *cnf, int is_nc);
3870OPENSSL_EXPORT void X509V3_conf_free(CONF_VALUE *val);
3871
David Benjamin58906ea2023-11-20 23:59:49 -05003872OPENSSL_EXPORT char *i2s_ASN1_INTEGER(const X509V3_EXT_METHOD *meth,
3873 const ASN1_INTEGER *aint);
3874OPENSSL_EXPORT ASN1_INTEGER *s2i_ASN1_INTEGER(const X509V3_EXT_METHOD *meth,
3875 const char *value);
3876OPENSSL_EXPORT char *i2s_ASN1_ENUMERATED(const X509V3_EXT_METHOD *meth,
3877 const ASN1_ENUMERATED *aint);
3878
3879// X509V3_EXT_add registers |ext| as a custom extension for the extension type
3880// |ext->ext_nid|. |ext| must be valid for the remainder of the address space's
3881// lifetime. It returns one on success and zero on error.
3882//
3883// WARNING: This function modifies global state. If other code in the same
3884// address space also registers an extension with type |ext->ext_nid|, the two
3885// registrations will conflict. Which registration takes effect is undefined. If
3886// the two registrations use incompatible in-memory representations, code
3887// expecting the other registration will then cast a type to the wrong type,
3888// resulting in a potentially exploitable memory error. This conflict can also
3889// occur if BoringSSL later adds support for |ext->ext_nid|, with a different
3890// in-memory representation than the one expected by |ext|.
3891//
3892// This function, additionally, is not thread-safe and cannot be called
3893// concurrently with any other BoringSSL function.
3894//
3895// As a result, it is impossible to safely use this function. Registering a
3896// custom extension has no impact on certificate verification so, instead,
3897// callers should simply handle the custom extension with the byte-based
3898// |X509_EXTENSION| APIs directly. Registering |ext| with the library has little
3899// practical value.
3900OPENSSL_EXPORT OPENSSL_DEPRECATED int X509V3_EXT_add(X509V3_EXT_METHOD *ext);
3901
3902// X509V3_EXT_add_alias registers a custom extension with NID |nid_to|. The
3903// corresponding ASN.1 type is copied from |nid_from|. It returns one on success
3904// and zero on error.
3905//
3906// WARNING: Do not use this function. See |X509V3_EXT_add|.
3907OPENSSL_EXPORT OPENSSL_DEPRECATED int X509V3_EXT_add_alias(int nid_to,
3908 int nid_from);
3909
3910OPENSSL_EXPORT const X509V3_EXT_METHOD *X509V3_EXT_get(
3911 const X509_EXTENSION *ext);
3912OPENSSL_EXPORT const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid);
David Benjamin58906ea2023-11-20 23:59:49 -05003913
3914// X509V3_EXT_d2i decodes |ext| and returns a pointer to a newly-allocated
3915// structure, with type dependent on the type of the extension. It returns NULL
3916// if |ext| is an unsupported extension or if there was a syntax error in the
3917// extension. The caller should cast the return value to the expected type and
3918// free the structure when done.
3919//
3920// WARNING: Casting the return value to the wrong type is a potentially
3921// exploitable memory error, so callers must not use this function before
3922// checking |ext| is of a known type.
3923OPENSSL_EXPORT void *X509V3_EXT_d2i(const X509_EXTENSION *ext);
3924
3925// X509V3_get_d2i finds and decodes the extension in |extensions| of type |nid|.
3926// If found, it decodes it and returns a newly-allocated structure, with type
3927// dependent on |nid|. If the extension is not found or on error, it returns
3928// NULL. The caller may distinguish these cases using the |out_critical| value.
3929//
3930// If |out_critical| is not NULL, this function sets |*out_critical| to one if
3931// the extension is found and critical, zero if it is found and not critical, -1
3932// if it is not found, and -2 if there is an invalid duplicate extension. Note
3933// this function may set |*out_critical| to one or zero and still return NULL if
3934// the extension is found but has a syntax error.
3935//
3936// If |out_idx| is not NULL, this function looks for the first occurrence of the
3937// extension after |*out_idx|. It then sets |*out_idx| to the index of the
3938// extension, or -1 if not found. If |out_idx| is non-NULL, duplicate extensions
3939// are not treated as an error. Callers, however, should not rely on this
3940// behavior as it may be removed in the future. Duplicate extensions are
3941// forbidden in RFC 5280.
3942//
3943// WARNING: This function is difficult to use correctly. Callers should pass a
3944// non-NULL |out_critical| and check both the return value and |*out_critical|
3945// to handle errors. If the return value is NULL and |*out_critical| is not -1,
3946// there was an error. Otherwise, the function succeeded and but may return NULL
3947// for a missing extension. Callers should pass NULL to |out_idx| so that
3948// duplicate extensions are handled correctly.
3949//
3950// Additionally, casting the return value to the wrong type is a potentially
3951// exploitable memory error, so callers must ensure the cast and |nid| match.
3952OPENSSL_EXPORT void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *extensions,
3953 int nid, int *out_critical, int *out_idx);
3954
3955// X509V3_EXT_free casts |ext_data| into the type that corresponds to |nid| and
3956// releases memory associated with it. It returns one on success and zero if
3957// |nid| is not a known extension.
3958//
3959// WARNING: Casting |ext_data| to the wrong type is a potentially exploitable
3960// memory error, so callers must ensure |ext_data|'s type matches |nid|.
3961//
3962// TODO(davidben): OpenSSL upstream no longer exposes this function. Remove it?
3963OPENSSL_EXPORT int X509V3_EXT_free(int nid, void *ext_data);
3964
3965// X509V3_EXT_i2d casts |ext_struc| into the type that corresponds to
3966// |ext_nid|, serializes it, and returns a newly-allocated |X509_EXTENSION|
3967// object containing the serialization, or NULL on error. The |X509_EXTENSION|
3968// has OID |ext_nid| and is critical if |crit| is one.
3969//
3970// WARNING: Casting |ext_struc| to the wrong type is a potentially exploitable
3971// memory error, so callers must ensure |ext_struct|'s type matches |ext_nid|.
3972OPENSSL_EXPORT X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit,
3973 void *ext_struc);
3974
3975// The following constants control the behavior of |X509V3_add1_i2d| and related
3976// functions.
3977
3978// X509V3_ADD_OP_MASK can be ANDed with the flags to determine how duplicate
3979// extensions are processed.
3980#define X509V3_ADD_OP_MASK 0xfL
3981
3982// X509V3_ADD_DEFAULT causes the function to fail if the extension was already
3983// present.
3984#define X509V3_ADD_DEFAULT 0L
3985
3986// X509V3_ADD_APPEND causes the function to unconditionally appended the new
3987// extension to to the extensions list, even if there is a duplicate.
3988#define X509V3_ADD_APPEND 1L
3989
3990// X509V3_ADD_REPLACE causes the function to replace the existing extension, or
3991// append if it is not present.
3992#define X509V3_ADD_REPLACE 2L
3993
3994// X509V3_ADD_REPLACE causes the function to replace the existing extension and
3995// fail if it is not present.
3996#define X509V3_ADD_REPLACE_EXISTING 3L
3997
3998// X509V3_ADD_KEEP_EXISTING causes the function to succeed without replacing the
3999// extension if already present.
4000#define X509V3_ADD_KEEP_EXISTING 4L
4001
4002// X509V3_ADD_DELETE causes the function to remove the matching extension. No
4003// new extension is added. If there is no matching extension, the function
4004// fails. The |value| parameter is ignored in this mode.
4005#define X509V3_ADD_DELETE 5L
4006
4007// X509V3_ADD_SILENT may be ORed into one of the values above to indicate the
4008// function should not add to the error queue on duplicate or missing extension.
4009// The function will continue to return zero in those cases, and it will
4010// continue to return -1 and add to the error queue on other errors.
4011#define X509V3_ADD_SILENT 0x10
4012
4013// X509V3_add1_i2d casts |value| to the type that corresponds to |nid|,
4014// serializes it, and appends it to the extension list in |*x|. If |*x| is NULL,
4015// it will set |*x| to a newly-allocated |STACK_OF(X509_EXTENSION)| as needed.
4016// The |crit| parameter determines whether the new extension is critical.
4017// |flags| may be some combination of the |X509V3_ADD_*| constants to control
4018// the function's behavior on duplicate extension.
4019//
4020// This function returns one on success, zero if the operation failed due to a
4021// missing or duplicate extension, and -1 on other errors.
4022//
4023// WARNING: Casting |value| to the wrong type is a potentially exploitable
4024// memory error, so callers must ensure |value|'s type matches |nid|.
4025OPENSSL_EXPORT int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid,
4026 void *value, int crit, unsigned long flags);
4027
David Benjamin58906ea2023-11-20 23:59:49 -05004028OPENSSL_EXPORT int X509_check_ca(X509 *x);
4029OPENSSL_EXPORT int X509_check_purpose(X509 *x, int id, int ca);
David Benjamin7ff31d32023-11-25 00:38:01 -05004030
4031// X509_supported_extension returns one if |ex| is a critical X.509 certificate
4032// extension, supported by |X509_verify_cert|, and zero otherwise.
4033//
4034// Note this function only reports certificate extensions (as opposed to CRL or
4035// CRL extensions), and only extensions that are expected to be marked critical.
4036// Additionally, |X509_verify_cert| checks for unsupported critical extensions
4037// internally, so most callers will not need to call this function separately.
David Benjamin58906ea2023-11-20 23:59:49 -05004038OPENSSL_EXPORT int X509_supported_extension(const X509_EXTENSION *ex);
David Benjamin7ff31d32023-11-25 00:38:01 -05004039
David Benjamin58906ea2023-11-20 23:59:49 -05004040OPENSSL_EXPORT int X509_PURPOSE_set(int *p, int purpose);
4041OPENSSL_EXPORT int X509_check_issued(X509 *issuer, X509 *subject);
4042OPENSSL_EXPORT int X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid);
4043
4044OPENSSL_EXPORT uint32_t X509_get_extension_flags(X509 *x);
4045OPENSSL_EXPORT uint32_t X509_get_key_usage(X509 *x);
4046OPENSSL_EXPORT uint32_t X509_get_extended_key_usage(X509 *x);
4047
4048// X509_get0_subject_key_id returns |x509|'s subject key identifier, if present.
4049// (See RFC 5280, section 4.2.1.2.) It returns NULL if the extension is not
4050// present or if some extension in |x509| was invalid.
4051//
4052// Note that decoding an |X509| object will not check for invalid extensions. To
4053// detect the error case, call |X509_get_extensions_flags| and check the
4054// |EXFLAG_INVALID| bit.
4055OPENSSL_EXPORT const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x509);
4056
4057// X509_get0_authority_key_id returns keyIdentifier of |x509|'s authority key
4058// identifier, if the extension and field are present. (See RFC 5280,
4059// section 4.2.1.1.) It returns NULL if the extension is not present, if it is
4060// present but lacks a keyIdentifier field, or if some extension in |x509| was
4061// invalid.
4062//
4063// Note that decoding an |X509| object will not check for invalid extensions. To
4064// detect the error case, call |X509_get_extensions_flags| and check the
4065// |EXFLAG_INVALID| bit.
4066OPENSSL_EXPORT const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x509);
4067
4068// X509_get0_authority_issuer returns the authorityCertIssuer of |x509|'s
4069// authority key identifier, if the extension and field are present. (See
4070// RFC 5280, section 4.2.1.1.) It returns NULL if the extension is not present,
4071// if it is present but lacks a authorityCertIssuer field, or if some extension
4072// in |x509| was invalid.
4073//
4074// Note that decoding an |X509| object will not check for invalid extensions. To
4075// detect the error case, call |X509_get_extensions_flags| and check the
4076// |EXFLAG_INVALID| bit.
4077OPENSSL_EXPORT const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x509);
4078
4079// X509_get0_authority_serial returns the authorityCertSerialNumber of |x509|'s
4080// authority key identifier, if the extension and field are present. (See
4081// RFC 5280, section 4.2.1.1.) It returns NULL if the extension is not present,
4082// if it is present but lacks a authorityCertSerialNumber field, or if some
4083// extension in |x509| was invalid.
4084//
4085// Note that decoding an |X509| object will not check for invalid extensions. To
4086// detect the error case, call |X509_get_extensions_flags| and check the
4087// |EXFLAG_INVALID| bit.
4088OPENSSL_EXPORT const ASN1_INTEGER *X509_get0_authority_serial(X509 *x509);
4089
4090OPENSSL_EXPORT int X509_PURPOSE_get_count(void);
4091OPENSSL_EXPORT X509_PURPOSE *X509_PURPOSE_get0(int idx);
4092OPENSSL_EXPORT int X509_PURPOSE_get_by_sname(const char *sname);
4093OPENSSL_EXPORT int X509_PURPOSE_get_by_id(int id);
4094OPENSSL_EXPORT int X509_PURPOSE_add(int id, int trust, int flags,
4095 int (*ck)(const X509_PURPOSE *,
4096 const X509 *, int),
4097 const char *name, const char *sname,
4098 void *arg);
4099OPENSSL_EXPORT char *X509_PURPOSE_get0_name(const X509_PURPOSE *xp);
4100OPENSSL_EXPORT char *X509_PURPOSE_get0_sname(const X509_PURPOSE *xp);
4101OPENSSL_EXPORT int X509_PURPOSE_get_trust(const X509_PURPOSE *xp);
4102OPENSSL_EXPORT void X509_PURPOSE_cleanup(void);
4103OPENSSL_EXPORT int X509_PURPOSE_get_id(const X509_PURPOSE *);
4104
4105OPENSSL_EXPORT STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x);
4106OPENSSL_EXPORT STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x);
4107OPENSSL_EXPORT void X509_email_free(STACK_OF(OPENSSL_STRING) *sk);
4108OPENSSL_EXPORT STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x);
4109// Flags for X509_check_* functions
4110
4111// Deprecated: this flag does nothing
4112#define X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT 0
4113// Disable wildcard matching for dnsName fields and common name.
4114#define X509_CHECK_FLAG_NO_WILDCARDS 0x2
4115// X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS does nothing, but is necessary in
4116// OpenSSL to enable standard wildcard matching. In BoringSSL, this behavior is
4117// always enabled.
4118#define X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS 0
4119// Deprecated: this flag does nothing
4120#define X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS 0
4121// Deprecated: this flag does nothing
4122#define X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS 0
4123// Skip the subject common name fallback if subjectAltNames is missing.
4124#define X509_CHECK_FLAG_NEVER_CHECK_SUBJECT 0x20
4125
4126OPENSSL_EXPORT int X509_check_host(X509 *x, const char *chk, size_t chklen,
4127 unsigned int flags, char **peername);
4128OPENSSL_EXPORT int X509_check_email(X509 *x, const char *chk, size_t chklen,
4129 unsigned int flags);
4130OPENSSL_EXPORT int X509_check_ip(X509 *x, const unsigned char *chk,
4131 size_t chklen, unsigned int flags);
4132OPENSSL_EXPORT int X509_check_ip_asc(X509 *x, const char *ipasc,
4133 unsigned int flags);
4134
4135OPENSSL_EXPORT ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc);
4136OPENSSL_EXPORT ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc);
4137
Adam Langley95c29f32014-06-20 12:00:00 -07004138
David Benjamin8a5ec722021-10-15 13:17:40 -04004139#if defined(__cplusplus)
4140} // extern C
David Benjamin4492a612017-08-02 17:16:31 -04004141#endif
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004142
David Benjamin4492a612017-08-02 17:16:31 -04004143#if !defined(BORINGSSL_NO_CXX)
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004144extern "C++" {
4145
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07004146BSSL_NAMESPACE_BEGIN
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004147
David Benjamin58906ea2023-11-20 23:59:49 -05004148BORINGSSL_MAKE_DELETER(ACCESS_DESCRIPTION, ACCESS_DESCRIPTION_free)
4149BORINGSSL_MAKE_DELETER(AUTHORITY_KEYID, AUTHORITY_KEYID_free)
4150BORINGSSL_MAKE_DELETER(BASIC_CONSTRAINTS, BASIC_CONSTRAINTS_free)
4151// TODO(davidben): Move this to conf.h and rename to CONF_VALUE_free.
4152BORINGSSL_MAKE_DELETER(CONF_VALUE, X509V3_conf_free)
4153BORINGSSL_MAKE_DELETER(DIST_POINT, DIST_POINT_free)
4154BORINGSSL_MAKE_DELETER(GENERAL_NAME, GENERAL_NAME_free)
4155BORINGSSL_MAKE_DELETER(GENERAL_SUBTREE, GENERAL_SUBTREE_free)
4156BORINGSSL_MAKE_DELETER(NAME_CONSTRAINTS, NAME_CONSTRAINTS_free)
Matthew Braithwaite7358fab2016-09-26 14:14:43 -07004157BORINGSSL_MAKE_DELETER(NETSCAPE_SPKI, NETSCAPE_SPKI_free)
David Benjamin58906ea2023-11-20 23:59:49 -05004158BORINGSSL_MAKE_DELETER(POLICY_MAPPING, POLICY_MAPPING_free)
4159BORINGSSL_MAKE_DELETER(POLICYINFO, POLICYINFO_free)
David Benjamin441efad2018-03-21 23:25:19 -04004160BORINGSSL_MAKE_DELETER(RSA_PSS_PARAMS, RSA_PSS_PARAMS_free)
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004161BORINGSSL_MAKE_DELETER(X509, X509_free)
David Benjamin2908dd12018-06-29 17:46:42 -04004162BORINGSSL_MAKE_UP_REF(X509, X509_up_ref)
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004163BORINGSSL_MAKE_DELETER(X509_ALGOR, X509_ALGOR_free)
David Benjaminab7811e2021-04-20 14:26:36 -04004164BORINGSSL_MAKE_DELETER(X509_ATTRIBUTE, X509_ATTRIBUTE_free)
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004165BORINGSSL_MAKE_DELETER(X509_CRL, X509_CRL_free)
David Benjamin2908dd12018-06-29 17:46:42 -04004166BORINGSSL_MAKE_UP_REF(X509_CRL, X509_CRL_up_ref)
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004167BORINGSSL_MAKE_DELETER(X509_EXTENSION, X509_EXTENSION_free)
David Benjamin38c4bf12023-11-29 10:48:37 -05004168BORINGSSL_MAKE_DELETER(X509_INFO, X509_INFO_free)
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004169BORINGSSL_MAKE_DELETER(X509_LOOKUP, X509_LOOKUP_free)
4170BORINGSSL_MAKE_DELETER(X509_NAME, X509_NAME_free)
4171BORINGSSL_MAKE_DELETER(X509_NAME_ENTRY, X509_NAME_ENTRY_free)
David Benjamin2762b352017-08-30 13:07:26 -04004172BORINGSSL_MAKE_DELETER(X509_PUBKEY, X509_PUBKEY_free)
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004173BORINGSSL_MAKE_DELETER(X509_REQ, X509_REQ_free)
4174BORINGSSL_MAKE_DELETER(X509_REVOKED, X509_REVOKED_free)
4175BORINGSSL_MAKE_DELETER(X509_SIG, X509_SIG_free)
4176BORINGSSL_MAKE_DELETER(X509_STORE, X509_STORE_free)
David Benjaminba423c92021-06-15 16:26:58 -04004177BORINGSSL_MAKE_UP_REF(X509_STORE, X509_STORE_up_ref)
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004178BORINGSSL_MAKE_DELETER(X509_STORE_CTX, X509_STORE_CTX_free)
4179BORINGSSL_MAKE_DELETER(X509_VERIFY_PARAM, X509_VERIFY_PARAM_free)
4180
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07004181BSSL_NAMESPACE_END
Matt Braithwaited17d74d2016-08-17 20:10:28 -07004182
David Benjamin991d31b2020-06-19 11:38:20 -04004183} // extern C++
4184#endif // !BORINGSSL_NO_CXX
Adam Langley95c29f32014-06-20 12:00:00 -07004185
David Benjamin689be0f2015-02-11 15:55:26 -05004186#define X509_R_AKID_MISMATCH 100
4187#define X509_R_BAD_PKCS7_VERSION 101
4188#define X509_R_BAD_X509_FILETYPE 102
4189#define X509_R_BASE64_DECODE_ERROR 103
4190#define X509_R_CANT_CHECK_DH_KEY 104
4191#define X509_R_CERT_ALREADY_IN_HASH_TABLE 105
4192#define X509_R_CRL_ALREADY_DELTA 106
4193#define X509_R_CRL_VERIFY_FAILURE 107
4194#define X509_R_IDP_MISMATCH 108
4195#define X509_R_INVALID_BIT_STRING_BITS_LEFT 109
4196#define X509_R_INVALID_DIRECTORY 110
4197#define X509_R_INVALID_FIELD_NAME 111
David Benjamina2f2bc32016-03-14 17:13:54 -04004198#define X509_R_INVALID_PSS_PARAMETERS 112
4199#define X509_R_INVALID_TRUST 113
4200#define X509_R_ISSUER_MISMATCH 114
4201#define X509_R_KEY_TYPE_MISMATCH 115
4202#define X509_R_KEY_VALUES_MISMATCH 116
4203#define X509_R_LOADING_CERT_DIR 117
4204#define X509_R_LOADING_DEFAULTS 118
4205#define X509_R_NEWER_CRL_NOT_NEWER 119
4206#define X509_R_NOT_PKCS7_SIGNED_DATA 120
4207#define X509_R_NO_CERTIFICATES_INCLUDED 121
4208#define X509_R_NO_CERT_SET_FOR_US_TO_VERIFY 122
4209#define X509_R_NO_CRLS_INCLUDED 123
4210#define X509_R_NO_CRL_NUMBER 124
4211#define X509_R_PUBLIC_KEY_DECODE_ERROR 125
4212#define X509_R_PUBLIC_KEY_ENCODE_ERROR 126
4213#define X509_R_SHOULD_RETRY 127
4214#define X509_R_UNKNOWN_KEY_TYPE 128
4215#define X509_R_UNKNOWN_NID 129
4216#define X509_R_UNKNOWN_PURPOSE_ID 130
4217#define X509_R_UNKNOWN_TRUST_ID 131
4218#define X509_R_UNSUPPORTED_ALGORITHM 132
4219#define X509_R_WRONG_LOOKUP_TYPE 133
4220#define X509_R_WRONG_TYPE 134
David Benjamin52a3bf22016-05-03 07:50:44 -04004221#define X509_R_NAME_TOO_LONG 135
David Benjamind69d94e2017-03-29 16:08:50 -05004222#define X509_R_INVALID_PARAMETER 136
David Benjamin371305f2018-09-18 13:14:53 -07004223#define X509_R_SIGNATURE_ALGORITHM_MISMATCH 137
David Benjaminfd86eaa2020-06-17 15:13:16 -04004224#define X509_R_DELTA_CRL_WITHOUT_CRL_NUMBER 138
David Benjamindd86e752020-06-17 21:17:42 -04004225#define X509_R_INVALID_FIELD_FOR_VERSION 139
4226#define X509_R_INVALID_VERSION 140
Adam Langleyf7975702022-01-12 16:23:11 -08004227#define X509_R_NO_CERTIFICATE_FOUND 141
4228#define X509_R_NO_CERTIFICATE_OR_CRL_FOUND 142
4229#define X509_R_NO_CRL_FOUND 143
David Benjamin029d0e72022-12-26 19:38:40 -05004230#define X509_R_INVALID_POLICY_EXTENSION 144
Adam Langley95c29f32014-06-20 12:00:00 -07004231
David Benjamina1c79222023-01-02 09:55:09 -08004232#endif // OPENSSL_HEADER_X509_H