Coverage Report

Created: 2023-06-07 07:11

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