Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* ==================================================================== |
| 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 Langley | 428fb3a | 2018-07-09 17:03:57 -0700 | [diff] [blame] | 9 | * notice, this list of conditions and the following disclaimer. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 10 | * |
| 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 Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 56 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 57 | // This file should be the first included by all BoringSSL headers. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 58 | |
Brian Smith | 054e682 | 2015-03-27 21:12:01 -1000 | [diff] [blame] | 59 | #include <stddef.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 60 | #include <stdint.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 61 | #include <sys/types.h> |
| 62 | |
David Benjamin | 8404bdb | 2016-10-10 14:41:44 -0400 | [diff] [blame] | 63 | #if defined(__MINGW32__) |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 64 | // stdio.h is needed on MinGW for __MINGW_PRINTF_FORMAT. |
David Benjamin | 8404bdb | 2016-10-10 14:41:44 -0400 | [diff] [blame] | 65 | #include <stdio.h> |
| 66 | #endif |
| 67 | |
David Benjamin | 7c3ce51 | 2018-09-30 18:50:26 -0500 | [diff] [blame] | 68 | #if defined(__APPLE__) |
| 69 | #include <TargetConditionals.h> |
| 70 | #endif |
| 71 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 72 | // 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 Benjamin | d403be9 | 2017-04-12 16:28:25 -0400 | [diff] [blame] | 75 | #include <openssl/is_boringssl.h> |
David Benjamin | e593fed | 2016-02-25 11:39:59 -0500 | [diff] [blame] | 76 | #include <openssl/opensslconf.h> |
Piotr Sikora | 987b8f1 | 2014-07-14 19:21:44 -0700 | [diff] [blame] | 77 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 78 | #if defined(BORINGSSL_PREFIX) |
| 79 | #include <boringssl_prefix_symbols.h> |
| 80 | #endif |
| 81 | |
Adam Langley | b2cb0ec | 2014-09-02 14:28:49 -0700 | [diff] [blame] | 82 | #if defined(__cplusplus) |
| 83 | extern "C" { |
| 84 | #endif |
| 85 | |
| 86 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 87 | #if defined(__x86_64) || defined(_M_AMD64) || defined(_M_X64) |
| 88 | #define OPENSSL_64_BIT |
| 89 | #define OPENSSL_X86_64 |
| 90 | #elif defined(__x86) || defined(__i386) || defined(__i386__) || defined(_M_IX86) |
| 91 | #define OPENSSL_32_BIT |
| 92 | #define OPENSSL_X86 |
David Benjamin | c3f4612 | 2020-12-01 17:47:57 -0500 | [diff] [blame] | 93 | #elif defined(__AARCH64EL__) || defined(_M_ARM64) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 94 | #define OPENSSL_64_BIT |
| 95 | #define OPENSSL_AARCH64 |
David Benjamin | c3f4612 | 2020-12-01 17:47:57 -0500 | [diff] [blame] | 96 | #elif defined(__ARMEL__) || defined(_M_ARM) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 97 | #define OPENSSL_32_BIT |
| 98 | #define OPENSSL_ARM |
David Benjamin | c3f4612 | 2020-12-01 17:47:57 -0500 | [diff] [blame] | 99 | #elif defined(__MIPSEL__) && !defined(__LP64__) |
Adam Langley | 09020c2 | 2014-07-10 09:14:56 -0700 | [diff] [blame] | 100 | #define OPENSSL_32_BIT |
| 101 | #define OPENSSL_MIPS |
David Benjamin | c3f4612 | 2020-12-01 17:47:57 -0500 | [diff] [blame] | 102 | #elif defined(__MIPSEL__) && defined(__LP64__) |
Adam Langley | c11e13a | 2015-03-20 16:46:23 -0700 | [diff] [blame] | 103 | #define OPENSSL_64_BIT |
| 104 | #define OPENSSL_MIPS64 |
Adam Langley | 5652262 | 2021-02-25 14:58:53 -0800 | [diff] [blame] | 105 | #elif defined(__riscv) && __SIZEOF_POINTER__ == 8 |
| 106 | #define OPENSSL_64_BIT |
Rebecca Chang Swee Fun | b2d3c10 | 2022-08-24 02:38:49 +0000 | [diff] [blame] | 107 | #define OPENSSL_RISCV64 |
Adam Langley | 5652262 | 2021-02-25 14:58:53 -0800 | [diff] [blame] | 108 | #elif defined(__riscv) && __SIZEOF_POINTER__ == 4 |
| 109 | #define OPENSSL_32_BIT |
Adam Langley | 0113a4f | 2014-07-10 17:07:14 -0700 | [diff] [blame] | 110 | #elif defined(__pnacl__) |
| 111 | #define OPENSSL_32_BIT |
| 112 | #define OPENSSL_PNACL |
Adam Langley | 88e6a05 | 2018-03-20 16:12:56 -0700 | [diff] [blame] | 113 | #elif defined(__wasm__) |
| 114 | #define OPENSSL_32_BIT |
| 115 | #elif defined(__asmjs__) |
| 116 | #define OPENSSL_32_BIT |
Radu Margarint | 17b6a7f | 2016-07-21 15:46:57 -0700 | [diff] [blame] | 117 | #elif defined(__myriad2__) |
| 118 | #define OPENSSL_32_BIT |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 119 | #else |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 120 | // Note BoringSSL only supports standard 32-bit and 64-bit two's-complement, |
| 121 | // little-endian architectures. Functions will not produce the correct answer |
| 122 | // on other systems. Run the crypto_test binary, notably |
| 123 | // crypto/compiler_test.cc, before adding a new architecture. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 124 | #error "Unknown target CPU" |
| 125 | #endif |
| 126 | |
Adam Langley | 30eda1d | 2014-06-24 11:15:12 -0700 | [diff] [blame] | 127 | #if defined(__APPLE__) |
| 128 | #define OPENSSL_APPLE |
David Benjamin | 40633ac | 2019-08-30 16:00:33 -0400 | [diff] [blame] | 129 | // Note |TARGET_OS_MAC| is set for all Apple OS variants. |TARGET_OS_OSX| |
| 130 | // targets macOS specifically. |
| 131 | #if defined(TARGET_OS_OSX) && TARGET_OS_OSX |
| 132 | #define OPENSSL_MACOS |
| 133 | #endif |
David Benjamin | 13fd627 | 2018-09-28 10:12:35 -0500 | [diff] [blame] | 134 | #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE |
| 135 | #define OPENSSL_IOS |
| 136 | #endif |
Adam Langley | 30eda1d | 2014-06-24 11:15:12 -0700 | [diff] [blame] | 137 | #endif |
| 138 | |
Brian Smith | 8a36e53 | 2015-07-31 16:11:31 -0400 | [diff] [blame] | 139 | #if defined(_WIN32) |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 140 | #define OPENSSL_WINDOWS |
| 141 | #endif |
| 142 | |
David Benjamin | 9bf1634 | 2020-09-30 15:24:18 -0400 | [diff] [blame] | 143 | // Trusty isn't Linux but currently defines __linux__. As a workaround, we |
| 144 | // exclude it here. |
| 145 | // TODO(b/169780122): Remove this workaround once Trusty no longer defines it. |
David Benjamin | a03c34c | 2021-09-03 17:13:15 -0400 | [diff] [blame] | 146 | #if defined(__linux__) && !defined(__TRUSTY__) |
Alessandro Ghedini | 5e393fe | 2016-07-09 13:02:18 +0100 | [diff] [blame] | 147 | #define OPENSSL_LINUX |
| 148 | #endif |
| 149 | |
Aaron Green | c80e416 | 2017-01-13 11:49:38 -0800 | [diff] [blame] | 150 | #if defined(__Fuchsia__) |
| 151 | #define OPENSSL_FUCHSIA |
| 152 | #endif |
| 153 | |
David Benjamin | a03c34c | 2021-09-03 17:13:15 -0400 | [diff] [blame] | 154 | #if defined(__TRUSTY__) |
Adam Langley | 65a7e94 | 2015-05-07 18:28:27 -0700 | [diff] [blame] | 155 | #define OPENSSL_TRUSTY |
David Benjamin | 5b33eff | 2018-09-22 16:52:48 -0700 | [diff] [blame] | 156 | #define OPENSSL_NO_THREADS_CORRUPT_MEMORY_AND_LEAK_SECRETS_IF_THREADED |
Adam Langley | 65a7e94 | 2015-05-07 18:28:27 -0700 | [diff] [blame] | 157 | #endif |
| 158 | |
Matthew Braithwaite | 548c276 | 2018-06-22 18:53:05 -0700 | [diff] [blame] | 159 | #if defined(__ANDROID_API__) |
| 160 | #define OPENSSL_ANDROID |
| 161 | #endif |
| 162 | |
Bradley Hess | c953ee4 | 2021-03-15 23:40:48 -0400 | [diff] [blame] | 163 | #if defined(__FreeBSD__) |
| 164 | #define OPENSSL_FREEBSD |
| 165 | #endif |
| 166 | |
Bob Beck | 4540c3c | 2023-03-08 12:40:29 -0700 | [diff] [blame] | 167 | #if defined(__OpenBSD__) |
| 168 | #define OPENSSL_OPENBSD |
| 169 | #endif |
| 170 | |
David Benjamin | 5b33eff | 2018-09-22 16:52:48 -0700 | [diff] [blame] | 171 | // BoringSSL requires platform's locking APIs to make internal global state |
| 172 | // thread-safe, including the PRNG. On some single-threaded embedded platforms, |
| 173 | // locking APIs may not exist, so this dependency may be disabled with the |
| 174 | // following build flag. |
| 175 | // |
| 176 | // IMPORTANT: Doing so means the consumer promises the library will never be |
| 177 | // used in any multi-threaded context. It causes BoringSSL to be globally |
| 178 | // thread-unsafe. Setting it inappropriately will subtly and unpredictably |
| 179 | // corrupt memory and leak secret keys. |
| 180 | // |
| 181 | // Do not set this flag on any platform where threads are possible. BoringSSL |
| 182 | // maintainers will not provide support for any consumers that do so. Changes |
| 183 | // which break such unsupported configurations will not be reverted. |
| 184 | #if !defined(OPENSSL_NO_THREADS_CORRUPT_MEMORY_AND_LEAK_SECRETS_IF_THREADED) |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 185 | #define OPENSSL_THREADS |
| 186 | #endif |
| 187 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 188 | #define OPENSSL_IS_BORINGSSL |
David Benjamin | 49e9f67 | 2020-09-16 12:29:13 -0400 | [diff] [blame] | 189 | #define OPENSSL_VERSION_NUMBER 0x1010107f |
Matt Braithwaite | 6454a4c | 2015-07-31 15:57:07 -0700 | [diff] [blame] | 190 | #define SSLEAY_VERSION_NUMBER OPENSSL_VERSION_NUMBER |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 191 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 192 | // BORINGSSL_API_VERSION is a positive integer that increments as BoringSSL |
| 193 | // changes over time. The value itself is not meaningful. It will be incremented |
| 194 | // whenever is convenient to coordinate an API change with consumers. This will |
| 195 | // not denote any special point in development. |
| 196 | // |
| 197 | // A consumer may use this symbol in the preprocessor to temporarily build |
| 198 | // against multiple revisions of BoringSSL at the same time. It is not |
| 199 | // recommended to do so for longer than is necessary. |
David Benjamin | 556a973f | 2023-06-03 23:32:27 -0400 | [diff] [blame^] | 200 | #define BORINGSSL_API_VERSION 23 |
David Benjamin | 9d908ba | 2016-05-05 18:54:33 -0400 | [diff] [blame] | 201 | |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 202 | #if defined(BORINGSSL_SHARED_LIBRARY) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 203 | |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 204 | #if defined(OPENSSL_WINDOWS) |
| 205 | |
| 206 | #if defined(BORINGSSL_IMPLEMENTATION) |
| 207 | #define OPENSSL_EXPORT __declspec(dllexport) |
| 208 | #else |
| 209 | #define OPENSSL_EXPORT __declspec(dllimport) |
| 210 | #endif |
| 211 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 212 | #else // defined(OPENSSL_WINDOWS) |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 213 | |
| 214 | #if defined(BORINGSSL_IMPLEMENTATION) |
| 215 | #define OPENSSL_EXPORT __attribute__((visibility("default"))) |
| 216 | #else |
| 217 | #define OPENSSL_EXPORT |
| 218 | #endif |
| 219 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 220 | #endif // defined(OPENSSL_WINDOWS) |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 221 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 222 | #else // defined(BORINGSSL_SHARED_LIBRARY) |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 223 | |
| 224 | #define OPENSSL_EXPORT |
| 225 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 226 | #endif // defined(BORINGSSL_SHARED_LIBRARY) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 227 | |
Bob Beck | ac6d558 | 2023-03-28 14:23:03 -0600 | [diff] [blame] | 228 | #if defined(_MSC_VER) |
| 229 | |
| 230 | // OPENSSL_DEPRECATED is used to mark a function as deprecated. Use |
| 231 | // of any functions so marked in caller code will produce a warning. |
| 232 | // OPENSSL_BEGIN_ALLOW_DEPRECATED and OPENSSL_END_ALLOW_DEPRECATED |
| 233 | // can be used to suppress the warning in regions of caller code. |
| 234 | #define OPENSSL_DEPRECATED __declspec(deprecated) |
| 235 | #define OPENSSL_BEGIN_ALLOW_DEPRECATED \ |
| 236 | __pragma(warning(push)) __pragma(warning(disable : 4996)) |
| 237 | #define OPENSSL_END_ALLOW_DEPRECATED __pragma(warning(pop)) |
| 238 | |
| 239 | #elif defined(__GNUC__) || defined(__clang__) |
| 240 | |
| 241 | #define OPENSSL_DEPRECATED __attribute__((__deprecated__)) |
| 242 | #define OPENSSL_BEGIN_ALLOW_DEPRECATED \ |
| 243 | _Pragma("GCC diagnostic push") \ |
| 244 | _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") |
| 245 | #define OPENSSL_END_ALLOW_DEPRECATED _Pragma("GCC diagnostic pop") |
| 246 | |
| 247 | #else |
| 248 | |
| 249 | #define OPENSSL_DEPRECATED |
| 250 | #define OPENSSL_BEGIN_ALLOW_DEPRECATED |
| 251 | #define OPENSSL_END_ALLOW_DEPRECATED |
| 252 | |
| 253 | #endif |
| 254 | |
Brian Smith | 061332f | 2016-01-17 09:30:42 -1000 | [diff] [blame] | 255 | |
David Benjamin | 02afbd3 | 2017-10-05 15:04:08 -0400 | [diff] [blame] | 256 | #if defined(__GNUC__) || defined(__clang__) |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 257 | // MinGW has two different printf implementations. Ensure the format macro |
| 258 | // matches the selected implementation. See |
| 259 | // https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/. |
David Benjamin | 8404bdb | 2016-10-10 14:41:44 -0400 | [diff] [blame] | 260 | #if defined(__MINGW_PRINTF_FORMAT) |
Brian Smith | 061332f | 2016-01-17 09:30:42 -1000 | [diff] [blame] | 261 | #define OPENSSL_PRINTF_FORMAT_FUNC(string_index, first_to_check) \ |
David Benjamin | 8404bdb | 2016-10-10 14:41:44 -0400 | [diff] [blame] | 262 | __attribute__( \ |
| 263 | (__format__(__MINGW_PRINTF_FORMAT, string_index, first_to_check))) |
| 264 | #else |
| 265 | #define OPENSSL_PRINTF_FORMAT_FUNC(string_index, first_to_check) \ |
| 266 | __attribute__((__format__(__printf__, string_index, first_to_check))) |
| 267 | #endif |
Brian Smith | 061332f | 2016-01-17 09:30:42 -1000 | [diff] [blame] | 268 | #else |
| 269 | #define OPENSSL_PRINTF_FORMAT_FUNC(string_index, first_to_check) |
| 270 | #endif |
| 271 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 272 | // OPENSSL_MSVC_PRAGMA emits a pragma on MSVC and nothing on other compilers. |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 273 | #if defined(_MSC_VER) |
| 274 | #define OPENSSL_MSVC_PRAGMA(arg) __pragma(arg) |
| 275 | #else |
| 276 | #define OPENSSL_MSVC_PRAGMA(arg) |
| 277 | #endif |
| 278 | |
David Benjamin | 806e18c | 2017-05-24 18:04:18 -0400 | [diff] [blame] | 279 | #if defined(__GNUC__) || defined(__clang__) |
David Benjamin | 01f8a8c | 2017-04-15 18:12:55 -0400 | [diff] [blame] | 280 | #define OPENSSL_UNUSED __attribute__((unused)) |
| 281 | #else |
| 282 | #define OPENSSL_UNUSED |
| 283 | #endif |
| 284 | |
David Benjamin | a943613 | 2018-09-23 18:36:01 -0500 | [diff] [blame] | 285 | // C and C++ handle inline functions differently. In C++, an inline function is |
| 286 | // defined in just the header file, potentially emitted in multiple compilation |
| 287 | // units (in cases the compiler did not inline), but each copy must be identical |
| 288 | // to satsify ODR. In C, a non-static inline must be manually emitted in exactly |
| 289 | // one compilation unit with a separate extern inline declaration. |
| 290 | // |
| 291 | // In both languages, exported inline functions referencing file-local symbols |
| 292 | // are problematic. C forbids this altogether (though GCC and Clang seem not to |
| 293 | // enforce it). It works in C++, but ODR requires the definitions be identical, |
| 294 | // including all names in the definitions resolving to the "same entity". In |
| 295 | // practice, this is unlikely to be a problem, but an inline function that |
| 296 | // returns a pointer to a file-local symbol |
| 297 | // could compile oddly. |
| 298 | // |
| 299 | // Historically, we used static inline in headers. However, to satisfy ODR, use |
| 300 | // plain inline in C++, to allow inline consumer functions to call our header |
| 301 | // functions. Plain inline would also work better with C99 inline, but that is |
| 302 | // not used much in practice, extern inline is tedious, and there are conflicts |
| 303 | // with the old gnu89 model: |
| 304 | // https://stackoverflow.com/questions/216510/extern-inline |
| 305 | #if defined(__cplusplus) |
| 306 | #define OPENSSL_INLINE inline |
| 307 | #else |
| 308 | // Add OPENSSL_UNUSED so that, should an inline function be emitted via macro |
| 309 | // (e.g. a |STACK_OF(T)| implementation) in a source file without tripping |
| 310 | // clang's -Wunused-function. |
| 311 | #define OPENSSL_INLINE static inline OPENSSL_UNUSED |
| 312 | #endif |
| 313 | |
David Benjamin | 14c7e8d | 2016-11-09 17:57:31 -0500 | [diff] [blame] | 314 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) && \ |
| 315 | !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE) |
| 316 | #define BORINGSSL_UNSAFE_DETERMINISTIC_MODE |
| 317 | #endif |
Brian Smith | 061332f | 2016-01-17 09:30:42 -1000 | [diff] [blame] | 318 | |
Adam Langley | 208e239 | 2017-05-08 12:53:15 -0700 | [diff] [blame] | 319 | #if defined(__has_feature) |
| 320 | #if __has_feature(address_sanitizer) |
| 321 | #define OPENSSL_ASAN |
| 322 | #endif |
Daniel Hirche | 9af1edb | 2018-07-24 10:56:15 +0200 | [diff] [blame] | 323 | #if __has_feature(thread_sanitizer) |
| 324 | #define OPENSSL_TSAN |
| 325 | #endif |
Adam Langley | f64a6ee | 2017-05-17 13:05:50 -0700 | [diff] [blame] | 326 | #if __has_feature(memory_sanitizer) |
| 327 | #define OPENSSL_MSAN |
Adam Langley | e77c27d | 2018-09-07 11:20:23 -0700 | [diff] [blame] | 328 | #define OPENSSL_ASM_INCOMPATIBLE |
Adam Langley | f64a6ee | 2017-05-17 13:05:50 -0700 | [diff] [blame] | 329 | #endif |
Adam Langley | 208e239 | 2017-05-08 12:53:15 -0700 | [diff] [blame] | 330 | #endif |
| 331 | |
Adam Langley | e77c27d | 2018-09-07 11:20:23 -0700 | [diff] [blame] | 332 | #if defined(OPENSSL_ASM_INCOMPATIBLE) |
| 333 | #undef OPENSSL_ASM_INCOMPATIBLE |
| 334 | #if !defined(OPENSSL_NO_ASM) |
| 335 | #define OPENSSL_NO_ASM |
| 336 | #endif |
| 337 | #endif // OPENSSL_ASM_INCOMPATIBLE |
| 338 | |
Adam Langley | 7540cc2 | 2019-04-18 09:56:13 -0700 | [diff] [blame] | 339 | #if defined(__cplusplus) |
| 340 | // enums can be predeclared, but only in C++ and only if given an explicit type. |
| 341 | // C doesn't support setting an explicit type for enums thus a #define is used |
| 342 | // to do this only for C++. However, the ABI type between C and C++ need to have |
| 343 | // equal sizes, which is confirmed in a unittest. |
| 344 | #define BORINGSSL_ENUM_INT : int |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 345 | enum ssl_early_data_reason_t BORINGSSL_ENUM_INT; |
Adam Langley | 7540cc2 | 2019-04-18 09:56:13 -0700 | [diff] [blame] | 346 | enum ssl_encryption_level_t BORINGSSL_ENUM_INT; |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 347 | enum ssl_private_key_result_t BORINGSSL_ENUM_INT; |
Adam Langley | 7540cc2 | 2019-04-18 09:56:13 -0700 | [diff] [blame] | 348 | enum ssl_renegotiate_mode_t BORINGSSL_ENUM_INT; |
| 349 | enum ssl_select_cert_result_t BORINGSSL_ENUM_INT; |
| 350 | enum ssl_select_cert_result_t BORINGSSL_ENUM_INT; |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 351 | enum ssl_ticket_aead_result_t BORINGSSL_ENUM_INT; |
| 352 | enum ssl_verify_result_t BORINGSSL_ENUM_INT; |
Adam Langley | 7540cc2 | 2019-04-18 09:56:13 -0700 | [diff] [blame] | 353 | #else |
| 354 | #define BORINGSSL_ENUM_INT |
| 355 | #endif |
| 356 | |
David Benjamin | dd81bf7 | 2022-10-18 21:41:30 -0400 | [diff] [blame] | 357 | // ossl_ssize_t is a signed type which is large enough to fit the size of any |
| 358 | // valid memory allocation. We prefer using |size_t|, but sometimes we need a |
| 359 | // signed type for OpenSSL API compatibility. This type can be used in such |
| 360 | // cases to avoid overflow. |
| 361 | // |
| 362 | // Not all |size_t| values fit in |ossl_ssize_t|, but all |size_t| values that |
| 363 | // are sizes of or indices into C objects, can be converted without overflow. |
| 364 | typedef ptrdiff_t ossl_ssize_t; |
| 365 | |
David Benjamin | a1dffbf | 2022-10-25 16:29:43 -0400 | [diff] [blame] | 366 | // CBS_ASN1_TAG is the type used by |CBS| and |CBB| for ASN.1 tags. See that |
| 367 | // header for details. This type is defined in base.h as a forward declaration. |
| 368 | typedef uint32_t CBS_ASN1_TAG; |
| 369 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 370 | // CRYPTO_THREADID is a dummy value. |
David Benjamin | f0df86a | 2015-04-20 11:32:12 -0400 | [diff] [blame] | 371 | typedef int CRYPTO_THREADID; |
| 372 | |
David Benjamin | a406ad7 | 2021-10-04 19:21:01 -0400 | [diff] [blame] | 373 | // An |ASN1_NULL| is an opaque type. asn1.h represents the ASN.1 NULL value as |
| 374 | // an opaque, non-NULL |ASN1_NULL*| pointer. |
| 375 | typedef struct asn1_null_st ASN1_NULL; |
| 376 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 377 | typedef int ASN1_BOOLEAN; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 378 | typedef struct ASN1_ITEM_st ASN1_ITEM; |
| 379 | typedef struct asn1_object_st ASN1_OBJECT; |
| 380 | typedef struct asn1_pctx_st ASN1_PCTX; |
| 381 | typedef struct asn1_string_st ASN1_BIT_STRING; |
| 382 | typedef struct asn1_string_st ASN1_BMPSTRING; |
| 383 | typedef struct asn1_string_st ASN1_ENUMERATED; |
| 384 | typedef struct asn1_string_st ASN1_GENERALIZEDTIME; |
| 385 | typedef struct asn1_string_st ASN1_GENERALSTRING; |
| 386 | typedef struct asn1_string_st ASN1_IA5STRING; |
| 387 | typedef struct asn1_string_st ASN1_INTEGER; |
| 388 | typedef struct asn1_string_st ASN1_OCTET_STRING; |
| 389 | typedef struct asn1_string_st ASN1_PRINTABLESTRING; |
| 390 | typedef struct asn1_string_st ASN1_STRING; |
| 391 | typedef struct asn1_string_st ASN1_T61STRING; |
| 392 | typedef struct asn1_string_st ASN1_TIME; |
| 393 | typedef struct asn1_string_st ASN1_UNIVERSALSTRING; |
| 394 | typedef struct asn1_string_st ASN1_UTCTIME; |
| 395 | typedef struct asn1_string_st ASN1_UTF8STRING; |
| 396 | typedef struct asn1_string_st ASN1_VISIBLESTRING; |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 397 | typedef struct asn1_type_st ASN1_TYPE; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 398 | typedef struct AUTHORITY_KEYID_st AUTHORITY_KEYID; |
Matt Braithwaite | b9afd51 | 2016-07-19 12:41:35 -0700 | [diff] [blame] | 399 | typedef struct BASIC_CONSTRAINTS_st BASIC_CONSTRAINTS; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 400 | typedef struct DIST_POINT_st DIST_POINT; |
Matt Braithwaite | b9afd51 | 2016-07-19 12:41:35 -0700 | [diff] [blame] | 401 | typedef struct DSA_SIG_st DSA_SIG; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 402 | typedef struct ISSUING_DIST_POINT_st ISSUING_DIST_POINT; |
| 403 | typedef struct NAME_CONSTRAINTS_st NAME_CONSTRAINTS; |
Matt Braithwaite | 16f774f | 2015-08-03 14:14:14 -0700 | [diff] [blame] | 404 | typedef struct Netscape_spkac_st NETSCAPE_SPKAC; |
| 405 | typedef struct Netscape_spki_st NETSCAPE_SPKI; |
Adam Langley | ff452c1 | 2016-03-08 14:17:02 -0800 | [diff] [blame] | 406 | typedef struct RIPEMD160state_st RIPEMD160_CTX; |
Matt Braithwaite | b9afd51 | 2016-07-19 12:41:35 -0700 | [diff] [blame] | 407 | typedef struct X509_VERIFY_PARAM_st X509_VERIFY_PARAM; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 408 | typedef struct X509_algor_st X509_ALGOR; |
| 409 | typedef struct X509_crl_st X509_CRL; |
Matt Braithwaite | 16f774f | 2015-08-03 14:14:14 -0700 | [diff] [blame] | 410 | typedef struct X509_extension_st X509_EXTENSION; |
| 411 | typedef struct X509_info_st X509_INFO; |
| 412 | typedef struct X509_name_entry_st X509_NAME_ENTRY; |
| 413 | typedef struct X509_name_st X509_NAME; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 414 | typedef struct X509_pubkey_st X509_PUBKEY; |
Matt Braithwaite | 16f774f | 2015-08-03 14:14:14 -0700 | [diff] [blame] | 415 | typedef struct X509_req_st X509_REQ; |
| 416 | typedef struct X509_sig_st X509_SIG; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 417 | typedef struct bignum_ctx BN_CTX; |
| 418 | typedef struct bignum_st BIGNUM; |
| 419 | typedef struct bio_method_st BIO_METHOD; |
| 420 | typedef struct bio_st BIO; |
Adam Langley | e133345 | 2021-01-21 11:56:52 -0800 | [diff] [blame] | 421 | typedef struct blake2b_state_st BLAKE2B_CTX; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 422 | typedef struct bn_gencb_st BN_GENCB; |
| 423 | typedef struct bn_mont_ctx_st BN_MONT_CTX; |
| 424 | typedef struct buf_mem_st BUF_MEM; |
| 425 | typedef struct cbb_st CBB; |
| 426 | typedef struct cbs_st CBS; |
Adam Langley | 0d107e1 | 2015-05-05 16:36:32 -0700 | [diff] [blame] | 427 | typedef struct cmac_ctx_st CMAC_CTX; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 428 | typedef struct conf_st CONF; |
Brian Smith | 054e682 | 2015-03-27 21:12:01 -1000 | [diff] [blame] | 429 | typedef struct conf_value_st CONF_VALUE; |
Adam Langley | 9ef99d5 | 2016-10-25 17:33:49 -0700 | [diff] [blame] | 430 | typedef struct crypto_buffer_pool_st CRYPTO_BUFFER_POOL; |
| 431 | typedef struct crypto_buffer_st CRYPTO_BUFFER; |
Adam Langley | 24c0186 | 2022-05-06 16:14:47 -0700 | [diff] [blame] | 432 | typedef struct ctr_drbg_state_st CTR_DRBG_STATE; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 433 | typedef struct dh_st DH; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 434 | typedef struct dsa_st DSA; |
Matt Braithwaite | b9afd51 | 2016-07-19 12:41:35 -0700 | [diff] [blame] | 435 | typedef struct ec_group_st EC_GROUP; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 436 | typedef struct ec_key_st EC_KEY; |
Matt Braithwaite | b9afd51 | 2016-07-19 12:41:35 -0700 | [diff] [blame] | 437 | typedef struct ec_point_st EC_POINT; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 438 | typedef struct ecdsa_method_st ECDSA_METHOD; |
| 439 | typedef struct ecdsa_sig_st ECDSA_SIG; |
| 440 | typedef struct engine_st ENGINE; |
| 441 | typedef struct env_md_ctx_st EVP_MD_CTX; |
| 442 | typedef struct env_md_st EVP_MD; |
Adam Langley | fd772a5 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 443 | typedef struct evp_aead_st EVP_AEAD; |
Adam Langley | ce2a353 | 2022-05-13 11:00:24 -0700 | [diff] [blame] | 444 | typedef struct evp_aead_ctx_st EVP_AEAD_CTX; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 445 | typedef struct evp_cipher_ctx_st EVP_CIPHER_CTX; |
| 446 | typedef struct evp_cipher_st EVP_CIPHER; |
David Benjamin | 97a3393 | 2015-09-19 15:17:34 -0400 | [diff] [blame] | 447 | typedef struct evp_encode_ctx_st EVP_ENCODE_CTX; |
David Benjamin | 070a6c3 | 2021-05-05 15:39:27 -0400 | [diff] [blame] | 448 | typedef struct evp_hpke_aead_st EVP_HPKE_AEAD; |
| 449 | typedef struct evp_hpke_ctx_st EVP_HPKE_CTX; |
| 450 | typedef struct evp_hpke_kdf_st EVP_HPKE_KDF; |
| 451 | typedef struct evp_hpke_kem_st EVP_HPKE_KEM; |
| 452 | typedef struct evp_hpke_key_st EVP_HPKE_KEY; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 453 | typedef struct evp_pkey_ctx_st EVP_PKEY_CTX; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 454 | typedef struct evp_pkey_st EVP_PKEY; |
| 455 | typedef struct hmac_ctx_st HMAC_CTX; |
Adam Langley | c9eb7ea | 2014-08-22 11:06:14 -0700 | [diff] [blame] | 456 | typedef struct md4_state_st MD4_CTX; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 457 | typedef struct md5_state_st MD5_CTX; |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 458 | typedef struct ossl_init_settings_st OPENSSL_INIT_SETTINGS; |
Adam Langley | 5127db3 | 2014-09-19 10:49:56 -0700 | [diff] [blame] | 459 | typedef struct pkcs12_st PKCS12; |
Matt Braithwaite | 16f774f | 2015-08-03 14:14:14 -0700 | [diff] [blame] | 460 | typedef struct pkcs8_priv_key_info_st PKCS8_PRIV_KEY_INFO; |
| 461 | typedef struct private_key_st X509_PKEY; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 462 | typedef struct rand_meth_st RAND_METHOD; |
Adam Langley | 3f92d21 | 2015-02-20 15:32:52 -0800 | [diff] [blame] | 463 | typedef struct rc4_key_st RC4_KEY; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 464 | typedef struct rsa_meth_st RSA_METHOD; |
Shelley Vohr | 0446b59 | 2021-09-09 11:25:24 +0200 | [diff] [blame] | 465 | typedef struct rsa_pss_params_st RSA_PSS_PARAMS; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 466 | typedef struct rsa_st RSA; |
| 467 | typedef struct sha256_state_st SHA256_CTX; |
| 468 | typedef struct sha512_state_st SHA512_CTX; |
| 469 | typedef struct sha_state_st SHA_CTX; |
Arnar Birgisson | f27459e | 2016-02-09 18:09:00 -0800 | [diff] [blame] | 470 | typedef struct spake2_ctx_st SPAKE2_CTX; |
David Benjamin | cfd65b6 | 2015-09-14 01:54:44 -0400 | [diff] [blame] | 471 | typedef struct srtp_protection_profile_st SRTP_PROTECTION_PROFILE; |
David Benjamin | 79c117a | 2015-09-13 13:53:19 -0400 | [diff] [blame] | 472 | typedef struct ssl_cipher_st SSL_CIPHER; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 473 | typedef struct ssl_ctx_st SSL_CTX; |
David Benjamin | 45c8be9 | 2021-10-07 16:15:01 -0400 | [diff] [blame] | 474 | typedef struct ssl_early_callback_ctx SSL_CLIENT_HELLO; |
David Benjamin | c3b373b | 2021-06-06 13:04:26 -0400 | [diff] [blame] | 475 | typedef struct ssl_ech_keys_st SSL_ECH_KEYS; |
David Benjamin | e39ac8f | 2017-07-20 12:22:21 -0400 | [diff] [blame] | 476 | typedef struct ssl_method_st SSL_METHOD; |
Adam Langley | d04ca95 | 2017-02-28 11:26:51 -0800 | [diff] [blame] | 477 | typedef struct ssl_private_key_method_st SSL_PRIVATE_KEY_METHOD; |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 478 | typedef struct ssl_quic_method_st SSL_QUIC_METHOD; |
David Benjamin | a6b8cdf | 2015-09-13 14:07:33 -0400 | [diff] [blame] | 479 | typedef struct ssl_session_st SSL_SESSION; |
David Benjamin | e39ac8f | 2017-07-20 12:22:21 -0400 | [diff] [blame] | 480 | typedef struct ssl_st SSL; |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 481 | typedef struct ssl_ticket_aead_method_st SSL_TICKET_AEAD_METHOD; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 482 | typedef struct st_ERR_FNS ERR_FNS; |
Steven Valdez | 538a124 | 2019-12-16 12:12:31 -0500 | [diff] [blame] | 483 | typedef struct trust_token_st TRUST_TOKEN; |
| 484 | typedef struct trust_token_client_st TRUST_TOKEN_CLIENT; |
| 485 | typedef struct trust_token_issuer_st TRUST_TOKEN_ISSUER; |
David Benjamin | 239634d | 2020-04-29 11:17:51 -0400 | [diff] [blame] | 486 | typedef struct trust_token_method_st TRUST_TOKEN_METHOD; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 487 | typedef struct v3_ext_ctx X509V3_CTX; |
Matt Braithwaite | 16f774f | 2015-08-03 14:14:14 -0700 | [diff] [blame] | 488 | typedef struct x509_attributes_st X509_ATTRIBUTE; |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 489 | typedef struct x509_lookup_st X509_LOOKUP; |
David Benjamin | dddb60e | 2021-08-11 01:23:09 -0400 | [diff] [blame] | 490 | typedef struct x509_lookup_method_st X509_LOOKUP_METHOD; |
| 491 | typedef struct x509_object_st X509_OBJECT; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 492 | typedef struct x509_revoked_st X509_REVOKED; |
| 493 | typedef struct x509_st X509; |
| 494 | typedef struct x509_store_ctx_st X509_STORE_CTX; |
| 495 | typedef struct x509_store_st X509_STORE; |
Matt Braithwaite | 16f774f | 2015-08-03 14:14:14 -0700 | [diff] [blame] | 496 | typedef struct x509_trust_st X509_TRUST; |
| 497 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 498 | typedef void *OPENSSL_BLOCK; |
| 499 | |
| 500 | |
Adam Langley | b2cb0ec | 2014-09-02 14:28:49 -0700 | [diff] [blame] | 501 | #if defined(__cplusplus) |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 502 | } // extern C |
David Benjamin | ebb4a37 | 2017-07-20 12:50:11 -0400 | [diff] [blame] | 503 | #elif !defined(BORINGSSL_NO_CXX) |
| 504 | #define BORINGSSL_NO_CXX |
| 505 | #endif |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 506 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 507 | #if defined(BORINGSSL_PREFIX) |
| 508 | #define BSSL_NAMESPACE_BEGIN \ |
| 509 | namespace bssl { \ |
| 510 | inline namespace BORINGSSL_PREFIX { |
| 511 | #define BSSL_NAMESPACE_END \ |
| 512 | } \ |
| 513 | } |
| 514 | #else |
| 515 | #define BSSL_NAMESPACE_BEGIN namespace bssl { |
| 516 | #define BSSL_NAMESPACE_END } |
| 517 | #endif |
| 518 | |
David Benjamin | 689019f | 2018-09-11 01:18:50 -0500 | [diff] [blame] | 519 | // MSVC doesn't set __cplusplus to 201103 to indicate C++11 support (see |
| 520 | // https://connect.microsoft.com/VisualStudio/feedback/details/763051/a-value-of-predefined-macro-cplusplus-is-still-199711l) |
| 521 | // so MSVC is just assumed to support C++11. |
| 522 | #if !defined(BORINGSSL_NO_CXX) && __cplusplus < 201103L && !defined(_MSC_VER) |
| 523 | #define BORINGSSL_NO_CXX |
| 524 | #endif |
| 525 | |
| 526 | #if !defined(BORINGSSL_NO_CXX) |
| 527 | |
David Benjamin | 3e5619d | 2016-09-07 17:28:59 -0400 | [diff] [blame] | 528 | extern "C++" { |
| 529 | |
| 530 | #include <memory> |
| 531 | |
| 532 | // STLPort, used by some Android consumers, not have std::unique_ptr. |
| 533 | #if defined(_STLPORT_VERSION) |
| 534 | #define BORINGSSL_NO_CXX |
| 535 | #endif |
| 536 | |
| 537 | } // extern C++ |
| 538 | #endif // !BORINGSSL_NO_CXX |
| 539 | |
David Benjamin | f0e935d | 2016-09-06 18:10:19 -0400 | [diff] [blame] | 540 | #if defined(BORINGSSL_NO_CXX) |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 541 | |
| 542 | #define BORINGSSL_MAKE_DELETER(type, deleter) |
Adam Langley | 428fb3a | 2018-07-09 17:03:57 -0700 | [diff] [blame] | 543 | #define BORINGSSL_MAKE_UP_REF(type, up_ref_func) |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 544 | |
| 545 | #else |
| 546 | |
David Benjamin | f0e935d | 2016-09-06 18:10:19 -0400 | [diff] [blame] | 547 | extern "C++" { |
| 548 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 549 | BSSL_NAMESPACE_BEGIN |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 550 | |
| 551 | namespace internal { |
| 552 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 553 | // The Enable parameter is ignored and only exists so specializations can use |
| 554 | // SFINAE. |
| 555 | template <typename T, typename Enable = void> |
David Benjamin | 4709203 | 2016-09-09 14:54:10 -0400 | [diff] [blame] | 556 | struct DeleterImpl {}; |
| 557 | |
David Benjamin | 4709203 | 2016-09-09 14:54:10 -0400 | [diff] [blame] | 558 | struct Deleter { |
David Benjamin | 971b330 | 2023-01-25 15:59:39 -0500 | [diff] [blame] | 559 | template <typename T> |
David Benjamin | 4709203 | 2016-09-09 14:54:10 -0400 | [diff] [blame] | 560 | void operator()(T *ptr) { |
| 561 | // Rather than specialize Deleter for each type, we specialize |
| 562 | // DeleterImpl. This allows bssl::UniquePtr<T> to be used while only |
| 563 | // including base.h as long as the destructor is not emitted. This matches |
| 564 | // std::unique_ptr's behavior on forward-declared types. |
| 565 | // |
| 566 | // DeleterImpl itself is specialized in the corresponding module's header |
| 567 | // and must be included to release an object. If not included, the compiler |
| 568 | // will error that DeleterImpl<T> does not have a method Free. |
| 569 | DeleterImpl<T>::Free(ptr); |
| 570 | } |
| 571 | }; |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 572 | |
David Benjamin | f0e935d | 2016-09-06 18:10:19 -0400 | [diff] [blame] | 573 | template <typename T, typename CleanupRet, void (*init)(T *), |
| 574 | CleanupRet (*cleanup)(T *)> |
| 575 | class StackAllocated { |
| 576 | public: |
| 577 | StackAllocated() { init(&ctx_); } |
| 578 | ~StackAllocated() { cleanup(&ctx_); } |
| 579 | |
David Benjamin | e9109cb | 2021-05-22 17:20:53 -0400 | [diff] [blame] | 580 | StackAllocated(const StackAllocated &) = delete; |
| 581 | StackAllocated& operator=(const StackAllocated &) = delete; |
| 582 | |
| 583 | T *get() { return &ctx_; } |
| 584 | const T *get() const { return &ctx_; } |
| 585 | |
| 586 | T *operator->() { return &ctx_; } |
| 587 | const T *operator->() const { return &ctx_; } |
| 588 | |
| 589 | void Reset() { |
| 590 | cleanup(&ctx_); |
| 591 | init(&ctx_); |
| 592 | } |
| 593 | |
| 594 | private: |
| 595 | T ctx_; |
| 596 | }; |
| 597 | |
| 598 | template <typename T, typename CleanupRet, void (*init)(T *), |
| 599 | CleanupRet (*cleanup)(T *), void (*move)(T *, T *)> |
| 600 | class StackAllocatedMovable { |
| 601 | public: |
| 602 | StackAllocatedMovable() { init(&ctx_); } |
| 603 | ~StackAllocatedMovable() { cleanup(&ctx_); } |
| 604 | |
| 605 | StackAllocatedMovable(StackAllocatedMovable &&other) { |
| 606 | init(&ctx_); |
| 607 | move(&ctx_, &other.ctx_); |
| 608 | } |
| 609 | StackAllocatedMovable &operator=(StackAllocatedMovable &&other) { |
| 610 | move(&ctx_, &other.ctx_); |
| 611 | return *this; |
| 612 | } |
Matthew Braithwaite | 1b0bd28 | 2016-09-27 13:25:32 -0700 | [diff] [blame] | 613 | |
David Benjamin | f0e935d | 2016-09-06 18:10:19 -0400 | [diff] [blame] | 614 | T *get() { return &ctx_; } |
| 615 | const T *get() const { return &ctx_; } |
| 616 | |
David Benjamin | 4492a61 | 2017-08-02 17:16:31 -0400 | [diff] [blame] | 617 | T *operator->() { return &ctx_; } |
| 618 | const T *operator->() const { return &ctx_; } |
| 619 | |
David Benjamin | f0e935d | 2016-09-06 18:10:19 -0400 | [diff] [blame] | 620 | void Reset() { |
| 621 | cleanup(&ctx_); |
| 622 | init(&ctx_); |
| 623 | } |
| 624 | |
| 625 | private: |
| 626 | T ctx_; |
| 627 | }; |
| 628 | |
| 629 | } // namespace internal |
| 630 | |
David Benjamin | 4709203 | 2016-09-09 14:54:10 -0400 | [diff] [blame] | 631 | #define BORINGSSL_MAKE_DELETER(type, deleter) \ |
| 632 | namespace internal { \ |
| 633 | template <> \ |
| 634 | struct DeleterImpl<type> { \ |
| 635 | static void Free(type *ptr) { deleter(ptr); } \ |
| 636 | }; \ |
David Benjamin | f0e935d | 2016-09-06 18:10:19 -0400 | [diff] [blame] | 637 | } |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 638 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 639 | // Holds ownership of heap-allocated BoringSSL structures. Sample usage: |
David Benjamin | 8ee0d14 | 2017-05-02 09:54:05 -0400 | [diff] [blame] | 640 | // bssl::UniquePtr<RSA> rsa(RSA_new()); |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 641 | // bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem())); |
| 642 | template <typename T> |
David Benjamin | 971b330 | 2023-01-25 15:59:39 -0500 | [diff] [blame] | 643 | using UniquePtr = std::unique_ptr<T, internal::Deleter>; |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 644 | |
David Benjamin | a943613 | 2018-09-23 18:36:01 -0500 | [diff] [blame] | 645 | #define BORINGSSL_MAKE_UP_REF(type, up_ref_func) \ |
| 646 | inline UniquePtr<type> UpRef(type *v) { \ |
| 647 | if (v != nullptr) { \ |
| 648 | up_ref_func(v); \ |
| 649 | } \ |
| 650 | return UniquePtr<type>(v); \ |
| 651 | } \ |
| 652 | \ |
| 653 | inline UniquePtr<type> UpRef(const UniquePtr<type> &ptr) { \ |
| 654 | return UpRef(ptr.get()); \ |
David Benjamin | 2908dd1 | 2018-06-29 17:46:42 -0400 | [diff] [blame] | 655 | } |
| 656 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 657 | BSSL_NAMESPACE_END |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 658 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 659 | } // extern C++ |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 660 | |
David Benjamin | f0e935d | 2016-09-06 18:10:19 -0400 | [diff] [blame] | 661 | #endif // !BORINGSSL_NO_CXX |
| 662 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 663 | #endif // OPENSSL_HEADER_BASE_H |