blob: 87ffe214b826fdabfb2fb5b1421a0de2a42688cc [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* ====================================================================
2 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
Adam Langley428fb3a2018-07-09 17:03:57 -07009 * notice, this list of conditions and the following disclaimer.
Adam Langley95c29f32014-06-20 12:00:00 -070010 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
20 *
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * openssl-core@openssl.org.
25 *
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
29 *
30 * 6. Redistributions of any form whatsoever must retain the following
31 * acknowledgment:
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This product includes cryptographic software written by Eric Young
50 * (eay@cryptsoft.com). This product includes software written by Tim
51 * Hudson (tjh@cryptsoft.com). */
52
53#ifndef OPENSSL_HEADER_BASE_H
54#define OPENSSL_HEADER_BASE_H
55
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070056
David Benjamin4512b792017-08-18 19:21:50 -040057// This file should be the first included by all BoringSSL headers.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070058
Brian Smith054e6822015-03-27 21:12:01 -100059#include <stddef.h>
Adam Langley95c29f32014-06-20 12:00:00 -070060#include <stdint.h>
Adam Langley95c29f32014-06-20 12:00:00 -070061#include <sys/types.h>
62
David Benjamin8404bdb2016-10-10 14:41:44 -040063#if defined(__MINGW32__)
David Benjamin4512b792017-08-18 19:21:50 -040064// stdio.h is needed on MinGW for __MINGW_PRINTF_FORMAT.
David Benjamin8404bdb2016-10-10 14:41:44 -040065#include <stdio.h>
66#endif
67
David Benjamin7c3ce512018-09-30 18:50:26 -050068#if defined(__APPLE__)
69#include <TargetConditionals.h>
70#endif
71
David Benjamin4512b792017-08-18 19:21:50 -040072// Include a BoringSSL-only header so consumers including this header without
73// setting up include paths do not accidentally pick up the system
74// opensslconf.h.
David Benjamind403be92017-04-12 16:28:25 -040075#include <openssl/is_boringssl.h>
David Benjamine593fed2016-02-25 11:39:59 -050076#include <openssl/opensslconf.h>
David Benjamina905bbb2023-06-12 18:11:13 -040077#include <openssl/target.h> // IWYU pragma: export
Piotr Sikora987b8f12014-07-14 19:21:44 -070078
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070079#if defined(BORINGSSL_PREFIX)
80#include <boringssl_prefix_symbols.h>
81#endif
82
Adam Langleyb2cb0ec2014-09-02 14:28:49 -070083#if defined(__cplusplus)
84extern "C" {
85#endif
86
87
Adam Langley30eda1d2014-06-24 11:15:12 -070088#if defined(__APPLE__)
David Benjamin40633ac2019-08-30 16:00:33 -040089// Note |TARGET_OS_MAC| is set for all Apple OS variants. |TARGET_OS_OSX|
90// targets macOS specifically.
91#if defined(TARGET_OS_OSX) && TARGET_OS_OSX
92#define OPENSSL_MACOS
93#endif
David Benjamin13fd6272018-09-28 10:12:35 -050094#if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
95#define OPENSSL_IOS
96#endif
Adam Langley30eda1d2014-06-24 11:15:12 -070097#endif
98
Adam Langley95c29f32014-06-20 12:00:00 -070099#define OPENSSL_IS_BORINGSSL
David Benjamin49e9f672020-09-16 12:29:13 -0400100#define OPENSSL_VERSION_NUMBER 0x1010107f
Matt Braithwaite6454a4c2015-07-31 15:57:07 -0700101#define SSLEAY_VERSION_NUMBER OPENSSL_VERSION_NUMBER
Adam Langley95c29f32014-06-20 12:00:00 -0700102
David Benjamin4512b792017-08-18 19:21:50 -0400103// BORINGSSL_API_VERSION is a positive integer that increments as BoringSSL
104// changes over time. The value itself is not meaningful. It will be incremented
105// whenever is convenient to coordinate an API change with consumers. This will
106// not denote any special point in development.
107//
108// A consumer may use this symbol in the preprocessor to temporarily build
109// against multiple revisions of BoringSSL at the same time. It is not
110// recommended to do so for longer than is necessary.
David Benjamin0ffd3652023-07-23 13:18:31 -0700111#define BORINGSSL_API_VERSION 25
David Benjamin9d908ba2016-05-05 18:54:33 -0400112
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700113#if defined(BORINGSSL_SHARED_LIBRARY)
Adam Langley95c29f32014-06-20 12:00:00 -0700114
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700115#if defined(OPENSSL_WINDOWS)
116
117#if defined(BORINGSSL_IMPLEMENTATION)
118#define OPENSSL_EXPORT __declspec(dllexport)
119#else
120#define OPENSSL_EXPORT __declspec(dllimport)
121#endif
122
David Benjamin4512b792017-08-18 19:21:50 -0400123#else // defined(OPENSSL_WINDOWS)
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700124
125#if defined(BORINGSSL_IMPLEMENTATION)
126#define OPENSSL_EXPORT __attribute__((visibility("default")))
127#else
128#define OPENSSL_EXPORT
129#endif
130
David Benjamin4512b792017-08-18 19:21:50 -0400131#endif // defined(OPENSSL_WINDOWS)
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700132
David Benjamin4512b792017-08-18 19:21:50 -0400133#else // defined(BORINGSSL_SHARED_LIBRARY)
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700134
135#define OPENSSL_EXPORT
136
David Benjamin4512b792017-08-18 19:21:50 -0400137#endif // defined(BORINGSSL_SHARED_LIBRARY)
Adam Langley95c29f32014-06-20 12:00:00 -0700138
Bob Beckac6d5582023-03-28 14:23:03 -0600139#if defined(_MSC_VER)
140
141// OPENSSL_DEPRECATED is used to mark a function as deprecated. Use
142// of any functions so marked in caller code will produce a warning.
143// OPENSSL_BEGIN_ALLOW_DEPRECATED and OPENSSL_END_ALLOW_DEPRECATED
144// can be used to suppress the warning in regions of caller code.
145#define OPENSSL_DEPRECATED __declspec(deprecated)
146#define OPENSSL_BEGIN_ALLOW_DEPRECATED \
147 __pragma(warning(push)) __pragma(warning(disable : 4996))
148#define OPENSSL_END_ALLOW_DEPRECATED __pragma(warning(pop))
149
150#elif defined(__GNUC__) || defined(__clang__)
151
152#define OPENSSL_DEPRECATED __attribute__((__deprecated__))
153#define OPENSSL_BEGIN_ALLOW_DEPRECATED \
154 _Pragma("GCC diagnostic push") \
155 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
156#define OPENSSL_END_ALLOW_DEPRECATED _Pragma("GCC diagnostic pop")
157
158#else
159
160#define OPENSSL_DEPRECATED
161#define OPENSSL_BEGIN_ALLOW_DEPRECATED
162#define OPENSSL_END_ALLOW_DEPRECATED
163
164#endif
165
Brian Smith061332f2016-01-17 09:30:42 -1000166
David Benjamin02afbd32017-10-05 15:04:08 -0400167#if defined(__GNUC__) || defined(__clang__)
David Benjamin4512b792017-08-18 19:21:50 -0400168// MinGW has two different printf implementations. Ensure the format macro
169// matches the selected implementation. See
170// https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/.
David Benjamin8404bdb2016-10-10 14:41:44 -0400171#if defined(__MINGW_PRINTF_FORMAT)
Brian Smith061332f2016-01-17 09:30:42 -1000172#define OPENSSL_PRINTF_FORMAT_FUNC(string_index, first_to_check) \
David Benjamin8404bdb2016-10-10 14:41:44 -0400173 __attribute__( \
174 (__format__(__MINGW_PRINTF_FORMAT, string_index, first_to_check)))
175#else
176#define OPENSSL_PRINTF_FORMAT_FUNC(string_index, first_to_check) \
177 __attribute__((__format__(__printf__, string_index, first_to_check)))
178#endif
Brian Smith061332f2016-01-17 09:30:42 -1000179#else
180#define OPENSSL_PRINTF_FORMAT_FUNC(string_index, first_to_check)
181#endif
182
David Benjamin4512b792017-08-18 19:21:50 -0400183// OPENSSL_MSVC_PRAGMA emits a pragma on MSVC and nothing on other compilers.
David Benjamina353cdb2016-06-09 16:48:33 -0400184#if defined(_MSC_VER)
185#define OPENSSL_MSVC_PRAGMA(arg) __pragma(arg)
186#else
187#define OPENSSL_MSVC_PRAGMA(arg)
188#endif
189
David Benjamin806e18c2017-05-24 18:04:18 -0400190#if defined(__GNUC__) || defined(__clang__)
David Benjamin01f8a8c2017-04-15 18:12:55 -0400191#define OPENSSL_UNUSED __attribute__((unused))
192#else
193#define OPENSSL_UNUSED
194#endif
195
David Benjamina9436132018-09-23 18:36:01 -0500196// C and C++ handle inline functions differently. In C++, an inline function is
197// defined in just the header file, potentially emitted in multiple compilation
198// units (in cases the compiler did not inline), but each copy must be identical
199// to satsify ODR. In C, a non-static inline must be manually emitted in exactly
200// one compilation unit with a separate extern inline declaration.
201//
202// In both languages, exported inline functions referencing file-local symbols
203// are problematic. C forbids this altogether (though GCC and Clang seem not to
204// enforce it). It works in C++, but ODR requires the definitions be identical,
205// including all names in the definitions resolving to the "same entity". In
206// practice, this is unlikely to be a problem, but an inline function that
207// returns a pointer to a file-local symbol
208// could compile oddly.
209//
210// Historically, we used static inline in headers. However, to satisfy ODR, use
211// plain inline in C++, to allow inline consumer functions to call our header
212// functions. Plain inline would also work better with C99 inline, but that is
213// not used much in practice, extern inline is tedious, and there are conflicts
214// with the old gnu89 model:
215// https://stackoverflow.com/questions/216510/extern-inline
216#if defined(__cplusplus)
217#define OPENSSL_INLINE inline
218#else
219// Add OPENSSL_UNUSED so that, should an inline function be emitted via macro
220// (e.g. a |STACK_OF(T)| implementation) in a source file without tripping
221// clang's -Wunused-function.
222#define OPENSSL_INLINE static inline OPENSSL_UNUSED
223#endif
224
Adam Langley7540cc22019-04-18 09:56:13 -0700225#if defined(__cplusplus)
226// enums can be predeclared, but only in C++ and only if given an explicit type.
227// C doesn't support setting an explicit type for enums thus a #define is used
228// to do this only for C++. However, the ABI type between C and C++ need to have
229// equal sizes, which is confirmed in a unittest.
230#define BORINGSSL_ENUM_INT : int
David Benjamin64770122019-05-04 11:00:04 -0500231enum ssl_early_data_reason_t BORINGSSL_ENUM_INT;
Adam Langley7540cc22019-04-18 09:56:13 -0700232enum ssl_encryption_level_t BORINGSSL_ENUM_INT;
David Benjamin64770122019-05-04 11:00:04 -0500233enum ssl_private_key_result_t BORINGSSL_ENUM_INT;
Adam Langley7540cc22019-04-18 09:56:13 -0700234enum ssl_renegotiate_mode_t BORINGSSL_ENUM_INT;
235enum ssl_select_cert_result_t BORINGSSL_ENUM_INT;
236enum ssl_select_cert_result_t BORINGSSL_ENUM_INT;
David Benjamin64770122019-05-04 11:00:04 -0500237enum ssl_ticket_aead_result_t BORINGSSL_ENUM_INT;
238enum ssl_verify_result_t BORINGSSL_ENUM_INT;
Adam Langley7540cc22019-04-18 09:56:13 -0700239#else
240#define BORINGSSL_ENUM_INT
241#endif
242
David Benjamindd81bf72022-10-18 21:41:30 -0400243// ossl_ssize_t is a signed type which is large enough to fit the size of any
244// valid memory allocation. We prefer using |size_t|, but sometimes we need a
245// signed type for OpenSSL API compatibility. This type can be used in such
246// cases to avoid overflow.
247//
248// Not all |size_t| values fit in |ossl_ssize_t|, but all |size_t| values that
249// are sizes of or indices into C objects, can be converted without overflow.
250typedef ptrdiff_t ossl_ssize_t;
251
David Benjamina1dffbf2022-10-25 16:29:43 -0400252// CBS_ASN1_TAG is the type used by |CBS| and |CBB| for ASN.1 tags. See that
253// header for details. This type is defined in base.h as a forward declaration.
254typedef uint32_t CBS_ASN1_TAG;
255
David Benjamin4512b792017-08-18 19:21:50 -0400256// CRYPTO_THREADID is a dummy value.
David Benjaminf0df86a2015-04-20 11:32:12 -0400257typedef int CRYPTO_THREADID;
258
David Benjamina406ad72021-10-04 19:21:01 -0400259// An |ASN1_NULL| is an opaque type. asn1.h represents the ASN.1 NULL value as
260// an opaque, non-NULL |ASN1_NULL*| pointer.
261typedef struct asn1_null_st ASN1_NULL;
262
Adam Langley95c29f32014-06-20 12:00:00 -0700263typedef int ASN1_BOOLEAN;
Adam Langley95c29f32014-06-20 12:00:00 -0700264typedef struct ASN1_ITEM_st ASN1_ITEM;
265typedef struct asn1_object_st ASN1_OBJECT;
266typedef struct asn1_pctx_st ASN1_PCTX;
267typedef struct asn1_string_st ASN1_BIT_STRING;
268typedef struct asn1_string_st ASN1_BMPSTRING;
269typedef struct asn1_string_st ASN1_ENUMERATED;
270typedef struct asn1_string_st ASN1_GENERALIZEDTIME;
271typedef struct asn1_string_st ASN1_GENERALSTRING;
272typedef struct asn1_string_st ASN1_IA5STRING;
273typedef struct asn1_string_st ASN1_INTEGER;
274typedef struct asn1_string_st ASN1_OCTET_STRING;
275typedef struct asn1_string_st ASN1_PRINTABLESTRING;
276typedef struct asn1_string_st ASN1_STRING;
277typedef struct asn1_string_st ASN1_T61STRING;
278typedef struct asn1_string_st ASN1_TIME;
279typedef struct asn1_string_st ASN1_UNIVERSALSTRING;
280typedef struct asn1_string_st ASN1_UTCTIME;
281typedef struct asn1_string_st ASN1_UTF8STRING;
282typedef struct asn1_string_st ASN1_VISIBLESTRING;
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700283typedef struct asn1_type_st ASN1_TYPE;
Adam Langley95c29f32014-06-20 12:00:00 -0700284typedef struct AUTHORITY_KEYID_st AUTHORITY_KEYID;
Matt Braithwaiteb9afd512016-07-19 12:41:35 -0700285typedef struct BASIC_CONSTRAINTS_st BASIC_CONSTRAINTS;
Adam Langley95c29f32014-06-20 12:00:00 -0700286typedef struct DIST_POINT_st DIST_POINT;
Matt Braithwaiteb9afd512016-07-19 12:41:35 -0700287typedef struct DSA_SIG_st DSA_SIG;
Adam Langley95c29f32014-06-20 12:00:00 -0700288typedef struct ISSUING_DIST_POINT_st ISSUING_DIST_POINT;
289typedef struct NAME_CONSTRAINTS_st NAME_CONSTRAINTS;
Matt Braithwaite16f774f2015-08-03 14:14:14 -0700290typedef struct Netscape_spkac_st NETSCAPE_SPKAC;
291typedef struct Netscape_spki_st NETSCAPE_SPKI;
Adam Langleyff452c12016-03-08 14:17:02 -0800292typedef struct RIPEMD160state_st RIPEMD160_CTX;
Matt Braithwaiteb9afd512016-07-19 12:41:35 -0700293typedef struct X509_VERIFY_PARAM_st X509_VERIFY_PARAM;
Adam Langley95c29f32014-06-20 12:00:00 -0700294typedef struct X509_algor_st X509_ALGOR;
295typedef struct X509_crl_st X509_CRL;
Matt Braithwaite16f774f2015-08-03 14:14:14 -0700296typedef struct X509_extension_st X509_EXTENSION;
297typedef struct X509_info_st X509_INFO;
298typedef struct X509_name_entry_st X509_NAME_ENTRY;
299typedef struct X509_name_st X509_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -0700300typedef struct X509_pubkey_st X509_PUBKEY;
Matt Braithwaite16f774f2015-08-03 14:14:14 -0700301typedef struct X509_req_st X509_REQ;
302typedef struct X509_sig_st X509_SIG;
Adam Langley95c29f32014-06-20 12:00:00 -0700303typedef struct bignum_ctx BN_CTX;
304typedef struct bignum_st BIGNUM;
305typedef struct bio_method_st BIO_METHOD;
306typedef struct bio_st BIO;
Adam Langleye1333452021-01-21 11:56:52 -0800307typedef struct blake2b_state_st BLAKE2B_CTX;
Adam Langley95c29f32014-06-20 12:00:00 -0700308typedef struct bn_gencb_st BN_GENCB;
309typedef struct bn_mont_ctx_st BN_MONT_CTX;
310typedef struct buf_mem_st BUF_MEM;
311typedef struct cbb_st CBB;
312typedef struct cbs_st CBS;
Adam Langley0d107e12015-05-05 16:36:32 -0700313typedef struct cmac_ctx_st CMAC_CTX;
Adam Langley95c29f32014-06-20 12:00:00 -0700314typedef struct conf_st CONF;
Brian Smith054e6822015-03-27 21:12:01 -1000315typedef struct conf_value_st CONF_VALUE;
Adam Langley9ef99d52016-10-25 17:33:49 -0700316typedef struct crypto_buffer_pool_st CRYPTO_BUFFER_POOL;
317typedef struct crypto_buffer_st CRYPTO_BUFFER;
Adam Langley24c01862022-05-06 16:14:47 -0700318typedef struct ctr_drbg_state_st CTR_DRBG_STATE;
Adam Langley95c29f32014-06-20 12:00:00 -0700319typedef struct dh_st DH;
Adam Langley95c29f32014-06-20 12:00:00 -0700320typedef struct dsa_st DSA;
Matt Braithwaiteb9afd512016-07-19 12:41:35 -0700321typedef struct ec_group_st EC_GROUP;
Adam Langley95c29f32014-06-20 12:00:00 -0700322typedef struct ec_key_st EC_KEY;
Matt Braithwaiteb9afd512016-07-19 12:41:35 -0700323typedef struct ec_point_st EC_POINT;
Adam Langley95c29f32014-06-20 12:00:00 -0700324typedef struct ecdsa_method_st ECDSA_METHOD;
325typedef struct ecdsa_sig_st ECDSA_SIG;
326typedef struct engine_st ENGINE;
327typedef struct env_md_ctx_st EVP_MD_CTX;
328typedef struct env_md_st EVP_MD;
Adam Langleyfd772a52014-06-20 12:00:00 -0700329typedef struct evp_aead_st EVP_AEAD;
Adam Langleyce2a3532022-05-13 11:00:24 -0700330typedef struct evp_aead_ctx_st EVP_AEAD_CTX;
Adam Langley95c29f32014-06-20 12:00:00 -0700331typedef struct evp_cipher_ctx_st EVP_CIPHER_CTX;
332typedef struct evp_cipher_st EVP_CIPHER;
David Benjamin97a33932015-09-19 15:17:34 -0400333typedef struct evp_encode_ctx_st EVP_ENCODE_CTX;
David Benjamin070a6c32021-05-05 15:39:27 -0400334typedef struct evp_hpke_aead_st EVP_HPKE_AEAD;
335typedef struct evp_hpke_ctx_st EVP_HPKE_CTX;
336typedef struct evp_hpke_kdf_st EVP_HPKE_KDF;
337typedef struct evp_hpke_kem_st EVP_HPKE_KEM;
338typedef struct evp_hpke_key_st EVP_HPKE_KEY;
Adam Langley95c29f32014-06-20 12:00:00 -0700339typedef struct evp_pkey_ctx_st EVP_PKEY_CTX;
Adam Langley95c29f32014-06-20 12:00:00 -0700340typedef struct evp_pkey_st EVP_PKEY;
341typedef struct hmac_ctx_st HMAC_CTX;
Adam Langleyc9eb7ea2014-08-22 11:06:14 -0700342typedef struct md4_state_st MD4_CTX;
Adam Langley95c29f32014-06-20 12:00:00 -0700343typedef struct md5_state_st MD5_CTX;
David Benjamin81f030b2016-08-12 14:48:19 -0400344typedef struct ossl_init_settings_st OPENSSL_INIT_SETTINGS;
Adam Langley5127db32014-09-19 10:49:56 -0700345typedef struct pkcs12_st PKCS12;
Matt Braithwaite16f774f2015-08-03 14:14:14 -0700346typedef struct pkcs8_priv_key_info_st PKCS8_PRIV_KEY_INFO;
347typedef struct private_key_st X509_PKEY;
Adam Langley95c29f32014-06-20 12:00:00 -0700348typedef struct rand_meth_st RAND_METHOD;
Adam Langley3f92d212015-02-20 15:32:52 -0800349typedef struct rc4_key_st RC4_KEY;
Adam Langley95c29f32014-06-20 12:00:00 -0700350typedef struct rsa_meth_st RSA_METHOD;
Shelley Vohr0446b592021-09-09 11:25:24 +0200351typedef struct rsa_pss_params_st RSA_PSS_PARAMS;
Adam Langley95c29f32014-06-20 12:00:00 -0700352typedef struct rsa_st RSA;
353typedef struct sha256_state_st SHA256_CTX;
354typedef struct sha512_state_st SHA512_CTX;
355typedef struct sha_state_st SHA_CTX;
Arnar Birgissonf27459e2016-02-09 18:09:00 -0800356typedef struct spake2_ctx_st SPAKE2_CTX;
David Benjamincfd65b62015-09-14 01:54:44 -0400357typedef struct srtp_protection_profile_st SRTP_PROTECTION_PROFILE;
David Benjamin79c117a2015-09-13 13:53:19 -0400358typedef struct ssl_cipher_st SSL_CIPHER;
Adam Langley95c29f32014-06-20 12:00:00 -0700359typedef struct ssl_ctx_st SSL_CTX;
David Benjamin45c8be92021-10-07 16:15:01 -0400360typedef struct ssl_early_callback_ctx SSL_CLIENT_HELLO;
David Benjaminc3b373b2021-06-06 13:04:26 -0400361typedef struct ssl_ech_keys_st SSL_ECH_KEYS;
David Benjamine39ac8f2017-07-20 12:22:21 -0400362typedef struct ssl_method_st SSL_METHOD;
Adam Langleyd04ca952017-02-28 11:26:51 -0800363typedef struct ssl_private_key_method_st SSL_PRIVATE_KEY_METHOD;
Steven Valdezc8e0f902018-07-14 11:23:01 -0400364typedef struct ssl_quic_method_st SSL_QUIC_METHOD;
David Benjamina6b8cdf2015-09-13 14:07:33 -0400365typedef struct ssl_session_st SSL_SESSION;
David Benjamine39ac8f2017-07-20 12:22:21 -0400366typedef struct ssl_st SSL;
Adam Langley4c341d02017-03-08 19:33:21 -0800367typedef struct ssl_ticket_aead_method_st SSL_TICKET_AEAD_METHOD;
Adam Langley95c29f32014-06-20 12:00:00 -0700368typedef struct st_ERR_FNS ERR_FNS;
Steven Valdez538a1242019-12-16 12:12:31 -0500369typedef struct trust_token_st TRUST_TOKEN;
370typedef struct trust_token_client_st TRUST_TOKEN_CLIENT;
371typedef struct trust_token_issuer_st TRUST_TOKEN_ISSUER;
David Benjamin239634d2020-04-29 11:17:51 -0400372typedef struct trust_token_method_st TRUST_TOKEN_METHOD;
Adam Langley95c29f32014-06-20 12:00:00 -0700373typedef struct v3_ext_ctx X509V3_CTX;
Matt Braithwaite16f774f2015-08-03 14:14:14 -0700374typedef struct x509_attributes_st X509_ATTRIBUTE;
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700375typedef struct x509_lookup_st X509_LOOKUP;
David Benjamindddb60e2021-08-11 01:23:09 -0400376typedef struct x509_lookup_method_st X509_LOOKUP_METHOD;
377typedef struct x509_object_st X509_OBJECT;
Adam Langley95c29f32014-06-20 12:00:00 -0700378typedef struct x509_revoked_st X509_REVOKED;
379typedef struct x509_st X509;
380typedef struct x509_store_ctx_st X509_STORE_CTX;
381typedef struct x509_store_st X509_STORE;
Matt Braithwaite16f774f2015-08-03 14:14:14 -0700382typedef struct x509_trust_st X509_TRUST;
383
Adam Langley95c29f32014-06-20 12:00:00 -0700384typedef void *OPENSSL_BLOCK;
385
386
Adam Langleyb2cb0ec2014-09-02 14:28:49 -0700387#if defined(__cplusplus)
David Benjamin4512b792017-08-18 19:21:50 -0400388} // extern C
David Benjaminebb4a372017-07-20 12:50:11 -0400389#elif !defined(BORINGSSL_NO_CXX)
390#define BORINGSSL_NO_CXX
391#endif
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700392
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700393#if defined(BORINGSSL_PREFIX)
394#define BSSL_NAMESPACE_BEGIN \
395 namespace bssl { \
396 inline namespace BORINGSSL_PREFIX {
397#define BSSL_NAMESPACE_END \
398 } \
399 }
400#else
401#define BSSL_NAMESPACE_BEGIN namespace bssl {
402#define BSSL_NAMESPACE_END }
403#endif
404
David Benjamin689019f2018-09-11 01:18:50 -0500405// MSVC doesn't set __cplusplus to 201103 to indicate C++11 support (see
406// https://connect.microsoft.com/VisualStudio/feedback/details/763051/a-value-of-predefined-macro-cplusplus-is-still-199711l)
407// so MSVC is just assumed to support C++11.
408#if !defined(BORINGSSL_NO_CXX) && __cplusplus < 201103L && !defined(_MSC_VER)
409#define BORINGSSL_NO_CXX
410#endif
411
412#if !defined(BORINGSSL_NO_CXX)
413
David Benjamin3e5619d2016-09-07 17:28:59 -0400414extern "C++" {
415
416#include <memory>
417
418// STLPort, used by some Android consumers, not have std::unique_ptr.
419#if defined(_STLPORT_VERSION)
420#define BORINGSSL_NO_CXX
421#endif
422
423} // extern C++
424#endif // !BORINGSSL_NO_CXX
425
David Benjaminf0e935d2016-09-06 18:10:19 -0400426#if defined(BORINGSSL_NO_CXX)
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700427
428#define BORINGSSL_MAKE_DELETER(type, deleter)
Adam Langley428fb3a2018-07-09 17:03:57 -0700429#define BORINGSSL_MAKE_UP_REF(type, up_ref_func)
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700430
431#else
432
David Benjaminf0e935d2016-09-06 18:10:19 -0400433extern "C++" {
434
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700435BSSL_NAMESPACE_BEGIN
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700436
437namespace internal {
438
David Benjamincfc11c22017-07-18 22:45:18 -0400439// The Enable parameter is ignored and only exists so specializations can use
440// SFINAE.
441template <typename T, typename Enable = void>
David Benjamin47092032016-09-09 14:54:10 -0400442struct DeleterImpl {};
443
David Benjamin47092032016-09-09 14:54:10 -0400444struct Deleter {
David Benjamin971b3302023-01-25 15:59:39 -0500445 template <typename T>
David Benjamin47092032016-09-09 14:54:10 -0400446 void operator()(T *ptr) {
447 // Rather than specialize Deleter for each type, we specialize
448 // DeleterImpl. This allows bssl::UniquePtr<T> to be used while only
449 // including base.h as long as the destructor is not emitted. This matches
450 // std::unique_ptr's behavior on forward-declared types.
451 //
452 // DeleterImpl itself is specialized in the corresponding module's header
453 // and must be included to release an object. If not included, the compiler
454 // will error that DeleterImpl<T> does not have a method Free.
455 DeleterImpl<T>::Free(ptr);
456 }
457};
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700458
David Benjaminf0e935d2016-09-06 18:10:19 -0400459template <typename T, typename CleanupRet, void (*init)(T *),
460 CleanupRet (*cleanup)(T *)>
461class StackAllocated {
462 public:
463 StackAllocated() { init(&ctx_); }
464 ~StackAllocated() { cleanup(&ctx_); }
465
David Benjamine9109cb2021-05-22 17:20:53 -0400466 StackAllocated(const StackAllocated &) = delete;
467 StackAllocated& operator=(const StackAllocated &) = delete;
468
469 T *get() { return &ctx_; }
470 const T *get() const { return &ctx_; }
471
472 T *operator->() { return &ctx_; }
473 const T *operator->() const { return &ctx_; }
474
475 void Reset() {
476 cleanup(&ctx_);
477 init(&ctx_);
478 }
479
480 private:
481 T ctx_;
482};
483
484template <typename T, typename CleanupRet, void (*init)(T *),
485 CleanupRet (*cleanup)(T *), void (*move)(T *, T *)>
486class StackAllocatedMovable {
487 public:
488 StackAllocatedMovable() { init(&ctx_); }
489 ~StackAllocatedMovable() { cleanup(&ctx_); }
490
491 StackAllocatedMovable(StackAllocatedMovable &&other) {
492 init(&ctx_);
493 move(&ctx_, &other.ctx_);
494 }
495 StackAllocatedMovable &operator=(StackAllocatedMovable &&other) {
496 move(&ctx_, &other.ctx_);
497 return *this;
498 }
Matthew Braithwaite1b0bd282016-09-27 13:25:32 -0700499
David Benjaminf0e935d2016-09-06 18:10:19 -0400500 T *get() { return &ctx_; }
501 const T *get() const { return &ctx_; }
502
David Benjamin4492a612017-08-02 17:16:31 -0400503 T *operator->() { return &ctx_; }
504 const T *operator->() const { return &ctx_; }
505
David Benjaminf0e935d2016-09-06 18:10:19 -0400506 void Reset() {
507 cleanup(&ctx_);
508 init(&ctx_);
509 }
510
511 private:
512 T ctx_;
513};
514
515} // namespace internal
516
David Benjamin47092032016-09-09 14:54:10 -0400517#define BORINGSSL_MAKE_DELETER(type, deleter) \
518 namespace internal { \
519 template <> \
520 struct DeleterImpl<type> { \
521 static void Free(type *ptr) { deleter(ptr); } \
522 }; \
David Benjaminf0e935d2016-09-06 18:10:19 -0400523 }
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700524
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700525// Holds ownership of heap-allocated BoringSSL structures. Sample usage:
David Benjamin8ee0d142017-05-02 09:54:05 -0400526// bssl::UniquePtr<RSA> rsa(RSA_new());
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700527// bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
528template <typename T>
David Benjamin971b3302023-01-25 15:59:39 -0500529using UniquePtr = std::unique_ptr<T, internal::Deleter>;
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700530
David Benjamina9436132018-09-23 18:36:01 -0500531#define BORINGSSL_MAKE_UP_REF(type, up_ref_func) \
532 inline UniquePtr<type> UpRef(type *v) { \
533 if (v != nullptr) { \
534 up_ref_func(v); \
535 } \
536 return UniquePtr<type>(v); \
537 } \
538 \
539 inline UniquePtr<type> UpRef(const UniquePtr<type> &ptr) { \
540 return UpRef(ptr.get()); \
David Benjamin2908dd12018-06-29 17:46:42 -0400541 }
542
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700543BSSL_NAMESPACE_END
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700544
David Benjamin4512b792017-08-18 19:21:50 -0400545} // extern C++
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700546
David Benjaminf0e935d2016-09-06 18:10:19 -0400547#endif // !BORINGSSL_NO_CXX
548
David Benjamin4512b792017-08-18 19:21:50 -0400549#endif // OPENSSL_HEADER_BASE_H