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 9 * notice, this list of conditions and the following disclaimer. 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 56 57 // This file should be the first included by all BoringSSL headers. 58 59 #include <stddef.h> 60 #include <stdint.h> 61 #include <stdlib.h> 62 #include <sys/types.h> 63 64 #if defined(__MINGW32__) 65 // stdio.h is needed on MinGW for __MINGW_PRINTF_FORMAT. 66 #include <stdio.h> 67 #endif 68 69 #if defined(__APPLE__) 70 #include <TargetConditionals.h> 71 #endif 72 73 // Include a BoringSSL-only header so consumers including this header without 74 // setting up include paths do not accidentally pick up the system 75 // opensslconf.h. 76 #include <openssl/is_boringssl.h> 77 #include <openssl/opensslconf.h> 78 #include <openssl/target.h> // IWYU pragma: export 79 80 #if defined(BORINGSSL_PREFIX) 81 #include <boringssl_prefix_symbols.h> 82 #endif 83 84 #if defined(__cplusplus) 85 extern "C" { 86 #endif 87 88 89 #if defined(__APPLE__) 90 // Note |TARGET_OS_MAC| is set for all Apple OS variants. |TARGET_OS_OSX| 91 // targets macOS specifically. 92 #if defined(TARGET_OS_OSX) && TARGET_OS_OSX 93 #define OPENSSL_MACOS 94 #endif 95 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE 96 #define OPENSSL_IOS 97 #endif 98 #endif 99 100 #define OPENSSL_IS_BORINGSSL 101 #define OPENSSL_VERSION_NUMBER 0x1010107f 102 #define SSLEAY_VERSION_NUMBER OPENSSL_VERSION_NUMBER 103 104 // BORINGSSL_API_VERSION is a positive integer that increments as BoringSSL 105 // changes over time. The value itself is not meaningful. It will be incremented 106 // whenever is convenient to coordinate an API change with consumers. This will 107 // not denote any special point in development. 108 // 109 // A consumer may use this symbol in the preprocessor to temporarily build 110 // against multiple revisions of BoringSSL at the same time. It is not 111 // recommended to do so for longer than is necessary. 112 #define BORINGSSL_API_VERSION 33 113 114 #if defined(BORINGSSL_SHARED_LIBRARY) 115 116 #if defined(OPENSSL_WINDOWS) 117 118 #if defined(BORINGSSL_IMPLEMENTATION) 119 #define OPENSSL_EXPORT __declspec(dllexport) 120 #else 121 #define OPENSSL_EXPORT __declspec(dllimport) 122 #endif 123 124 #else // defined(OPENSSL_WINDOWS) 125 126 #if defined(BORINGSSL_IMPLEMENTATION) 127 #define OPENSSL_EXPORT __attribute__((visibility("default"))) 128 #else 129 #define OPENSSL_EXPORT 130 #endif 131 132 #endif // defined(OPENSSL_WINDOWS) 133 134 #else // defined(BORINGSSL_SHARED_LIBRARY) 135 136 #define OPENSSL_EXPORT 137 138 #endif // defined(BORINGSSL_SHARED_LIBRARY) 139 140 #if defined(_MSC_VER) 141 142 // OPENSSL_DEPRECATED is used to mark a function as deprecated. Use 143 // of any functions so marked in caller code will produce a warning. 144 // OPENSSL_BEGIN_ALLOW_DEPRECATED and OPENSSL_END_ALLOW_DEPRECATED 145 // can be used to suppress the warning in regions of caller code. 146 #define OPENSSL_DEPRECATED __declspec(deprecated) 147 #define OPENSSL_BEGIN_ALLOW_DEPRECATED \ 148 __pragma(warning(push)) __pragma(warning(disable : 4996)) 149 #define OPENSSL_END_ALLOW_DEPRECATED __pragma(warning(pop)) 150 151 #elif defined(__GNUC__) || defined(__clang__) 152 153 #define OPENSSL_DEPRECATED __attribute__((__deprecated__)) 154 #define OPENSSL_BEGIN_ALLOW_DEPRECATED \ 155 _Pragma("GCC diagnostic push") \ 156 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") 157 #define OPENSSL_END_ALLOW_DEPRECATED _Pragma("GCC diagnostic pop") 158 159 #else 160 161 #define OPENSSL_DEPRECATED 162 #define OPENSSL_BEGIN_ALLOW_DEPRECATED 163 #define OPENSSL_END_ALLOW_DEPRECATED 164 165 #endif 166 167 168 #if defined(__GNUC__) || defined(__clang__) 169 // MinGW has two different printf implementations. Ensure the format macro 170 // matches the selected implementation. See 171 // https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/. 172 #if defined(__MINGW_PRINTF_FORMAT) 173 #define OPENSSL_PRINTF_FORMAT_FUNC(string_index, first_to_check) \ 174 __attribute__(( \ 175 __format__(__MINGW_PRINTF_FORMAT, string_index, first_to_check))) 176 #else 177 #define OPENSSL_PRINTF_FORMAT_FUNC(string_index, first_to_check) \ 178 __attribute__((__format__(__printf__, string_index, first_to_check))) 179 #endif 180 #else 181 #define OPENSSL_PRINTF_FORMAT_FUNC(string_index, first_to_check) 182 #endif 183 184 // OPENSSL_CLANG_PRAGMA emits a pragma on clang and nothing on other compilers. 185 #if defined(__clang__) 186 #define OPENSSL_CLANG_PRAGMA(arg) _Pragma(arg) 187 #else 188 #define OPENSSL_CLANG_PRAGMA(arg) 189 #endif 190 191 // OPENSSL_MSVC_PRAGMA emits a pragma on MSVC and nothing on other compilers. 192 #if defined(_MSC_VER) 193 #define OPENSSL_MSVC_PRAGMA(arg) __pragma(arg) 194 #else 195 #define OPENSSL_MSVC_PRAGMA(arg) 196 #endif 197 198 #if defined(__GNUC__) || defined(__clang__) 199 #define OPENSSL_UNUSED __attribute__((unused)) 200 #elif defined(_MSC_VER) 201 // __pragma wants to be on a separate line. The following is what it takes to 202 // stop clang-format from messing with that. 203 // clang-format off 204 #define OPENSSL_UNUSED __pragma(warning(suppress : 4505)) \ 205 /* */ 206 // clang-format on 207 #else 208 #define OPENSSL_UNUSED 209 #endif 210 211 // C and C++ handle inline functions differently. In C++, an inline function is 212 // defined in just the header file, potentially emitted in multiple compilation 213 // units (in cases the compiler did not inline), but each copy must be identical 214 // to satsify ODR. In C, a non-static inline must be manually emitted in exactly 215 // one compilation unit with a separate extern inline declaration. 216 // 217 // In both languages, exported inline functions referencing file-local symbols 218 // are problematic. C forbids this altogether (though GCC and Clang seem not to 219 // enforce it). It works in C++, but ODR requires the definitions be identical, 220 // including all names in the definitions resolving to the "same entity". In 221 // practice, this is unlikely to be a problem, but an inline function that 222 // returns a pointer to a file-local symbol 223 // could compile oddly. 224 // 225 // Historically, we used static inline in headers. However, to satisfy ODR, use 226 // plain inline in C++, to allow inline consumer functions to call our header 227 // functions. Plain inline would also work better with C99 inline, but that is 228 // not used much in practice, extern inline is tedious, and there are conflicts 229 // with the old gnu89 model: 230 // https://stackoverflow.com/questions/216510/extern-inline 231 #if defined(__cplusplus) 232 #define OPENSSL_INLINE inline 233 #else 234 // Add OPENSSL_UNUSED so that, should an inline function be emitted via macro 235 // (e.g. a |STACK_OF(T)| implementation) in a source file without tripping 236 // clang's -Wunused-function. 237 #define OPENSSL_INLINE static inline OPENSSL_UNUSED 238 #endif 239 240 #if defined(__cplusplus) 241 // enums can be predeclared, but only in C++ and only if given an explicit type. 242 // C doesn't support setting an explicit type for enums thus a #define is used 243 // to do this only for C++. However, the ABI type between C and C++ need to have 244 // equal sizes, which is confirmed in a unittest. 245 #define BORINGSSL_ENUM_INT : int 246 enum ssl_early_data_reason_t BORINGSSL_ENUM_INT; 247 enum ssl_encryption_level_t BORINGSSL_ENUM_INT; 248 enum ssl_private_key_result_t BORINGSSL_ENUM_INT; 249 enum ssl_renegotiate_mode_t BORINGSSL_ENUM_INT; 250 enum ssl_select_cert_result_t BORINGSSL_ENUM_INT; 251 enum ssl_select_cert_result_t BORINGSSL_ENUM_INT; 252 enum ssl_ticket_aead_result_t BORINGSSL_ENUM_INT; 253 enum ssl_verify_result_t BORINGSSL_ENUM_INT; 254 #else 255 #define BORINGSSL_ENUM_INT 256 #endif 257 258 // ossl_ssize_t is a signed type which is large enough to fit the size of any 259 // valid memory allocation. We prefer using |size_t|, but sometimes we need a 260 // signed type for OpenSSL API compatibility. This type can be used in such 261 // cases to avoid overflow. 262 // 263 // Not all |size_t| values fit in |ossl_ssize_t|, but all |size_t| values that 264 // are sizes of or indices into C objects, can be converted without overflow. 265 typedef ptrdiff_t ossl_ssize_t; 266 267 // CBS_ASN1_TAG is the type used by |CBS| and |CBB| for ASN.1 tags. See that 268 // header for details. This type is defined in base.h as a forward declaration. 269 typedef uint32_t CBS_ASN1_TAG; 270 271 // CRYPTO_THREADID is a dummy value. 272 typedef int CRYPTO_THREADID; 273 274 // An |ASN1_NULL| is an opaque type. asn1.h represents the ASN.1 NULL value as 275 // an opaque, non-NULL |ASN1_NULL*| pointer. 276 typedef struct asn1_null_st ASN1_NULL; 277 278 typedef int ASN1_BOOLEAN; 279 typedef struct ASN1_ITEM_st ASN1_ITEM; 280 typedef struct asn1_object_st ASN1_OBJECT; 281 typedef struct asn1_pctx_st ASN1_PCTX; 282 typedef struct asn1_string_st ASN1_BIT_STRING; 283 typedef struct asn1_string_st ASN1_BMPSTRING; 284 typedef struct asn1_string_st ASN1_ENUMERATED; 285 typedef struct asn1_string_st ASN1_GENERALIZEDTIME; 286 typedef struct asn1_string_st ASN1_GENERALSTRING; 287 typedef struct asn1_string_st ASN1_IA5STRING; 288 typedef struct asn1_string_st ASN1_INTEGER; 289 typedef struct asn1_string_st ASN1_OCTET_STRING; 290 typedef struct asn1_string_st ASN1_PRINTABLESTRING; 291 typedef struct asn1_string_st ASN1_STRING; 292 typedef struct asn1_string_st ASN1_T61STRING; 293 typedef struct asn1_string_st ASN1_TIME; 294 typedef struct asn1_string_st ASN1_UNIVERSALSTRING; 295 typedef struct asn1_string_st ASN1_UTCTIME; 296 typedef struct asn1_string_st ASN1_UTF8STRING; 297 typedef struct asn1_string_st ASN1_VISIBLESTRING; 298 typedef struct asn1_type_st ASN1_TYPE; 299 typedef struct AUTHORITY_KEYID_st AUTHORITY_KEYID; 300 typedef struct BASIC_CONSTRAINTS_st BASIC_CONSTRAINTS; 301 typedef struct DIST_POINT_st DIST_POINT; 302 typedef struct DSA_SIG_st DSA_SIG; 303 typedef struct GENERAL_NAME_st GENERAL_NAME; 304 typedef struct ISSUING_DIST_POINT_st ISSUING_DIST_POINT; 305 typedef struct NAME_CONSTRAINTS_st NAME_CONSTRAINTS; 306 typedef struct Netscape_spkac_st NETSCAPE_SPKAC; 307 typedef struct Netscape_spki_st NETSCAPE_SPKI; 308 typedef struct RIPEMD160state_st RIPEMD160_CTX; 309 typedef struct X509_VERIFY_PARAM_st X509_VERIFY_PARAM; 310 typedef struct X509_algor_st X509_ALGOR; 311 typedef struct X509_crl_st X509_CRL; 312 typedef struct X509_extension_st X509_EXTENSION; 313 typedef struct X509_info_st X509_INFO; 314 typedef struct X509_name_entry_st X509_NAME_ENTRY; 315 typedef struct X509_name_st X509_NAME; 316 typedef struct X509_pubkey_st X509_PUBKEY; 317 typedef struct X509_req_st X509_REQ; 318 typedef struct X509_sig_st X509_SIG; 319 typedef struct bignum_ctx BN_CTX; 320 typedef struct bignum_st BIGNUM; 321 typedef struct bio_method_st BIO_METHOD; 322 typedef struct bio_st BIO; 323 typedef struct blake2b_state_st BLAKE2B_CTX; 324 typedef struct bn_gencb_st BN_GENCB; 325 typedef struct bn_mont_ctx_st BN_MONT_CTX; 326 typedef struct buf_mem_st BUF_MEM; 327 typedef struct cbb_st CBB; 328 typedef struct cbs_st CBS; 329 typedef struct cmac_ctx_st CMAC_CTX; 330 typedef struct conf_st CONF; 331 typedef struct conf_value_st CONF_VALUE; 332 typedef struct crypto_buffer_pool_st CRYPTO_BUFFER_POOL; 333 typedef struct crypto_buffer_st CRYPTO_BUFFER; 334 typedef struct ctr_drbg_state_st CTR_DRBG_STATE; 335 typedef struct dh_st DH; 336 typedef struct dsa_st DSA; 337 typedef struct ec_group_st EC_GROUP; 338 typedef struct ec_key_st EC_KEY; 339 typedef struct ec_point_st EC_POINT; 340 typedef struct ecdsa_method_st ECDSA_METHOD; 341 typedef struct ecdsa_sig_st ECDSA_SIG; 342 typedef struct engine_st ENGINE; 343 typedef struct env_md_ctx_st EVP_MD_CTX; 344 typedef struct env_md_st EVP_MD; 345 typedef struct evp_aead_st EVP_AEAD; 346 typedef struct evp_aead_ctx_st EVP_AEAD_CTX; 347 typedef struct evp_cipher_ctx_st EVP_CIPHER_CTX; 348 typedef struct evp_cipher_st EVP_CIPHER; 349 typedef struct evp_encode_ctx_st EVP_ENCODE_CTX; 350 typedef struct evp_hpke_aead_st EVP_HPKE_AEAD; 351 typedef struct evp_hpke_ctx_st EVP_HPKE_CTX; 352 typedef struct evp_hpke_kdf_st EVP_HPKE_KDF; 353 typedef struct evp_hpke_kem_st EVP_HPKE_KEM; 354 typedef struct evp_hpke_key_st EVP_HPKE_KEY; 355 typedef struct evp_pkey_ctx_st EVP_PKEY_CTX; 356 typedef struct evp_pkey_st EVP_PKEY; 357 typedef struct hmac_ctx_st HMAC_CTX; 358 typedef struct md4_state_st MD4_CTX; 359 typedef struct md5_state_st MD5_CTX; 360 typedef struct ossl_init_settings_st OPENSSL_INIT_SETTINGS; 361 typedef struct pkcs12_st PKCS12; 362 typedef struct pkcs8_priv_key_info_st PKCS8_PRIV_KEY_INFO; 363 typedef struct private_key_st X509_PKEY; 364 typedef struct rand_meth_st RAND_METHOD; 365 typedef struct rc4_key_st RC4_KEY; 366 typedef struct rsa_meth_st RSA_METHOD; 367 typedef struct rsa_pss_params_st RSA_PSS_PARAMS; 368 typedef struct rsa_st RSA; 369 typedef struct sha256_state_st SHA256_CTX; 370 typedef struct sha512_state_st SHA512_CTX; 371 typedef struct sha_state_st SHA_CTX; 372 typedef struct spake2_ctx_st SPAKE2_CTX; 373 typedef struct srtp_protection_profile_st SRTP_PROTECTION_PROFILE; 374 typedef struct ssl_cipher_st SSL_CIPHER; 375 typedef struct ssl_credential_st SSL_CREDENTIAL; 376 typedef struct ssl_ctx_st SSL_CTX; 377 typedef struct ssl_early_callback_ctx SSL_CLIENT_HELLO; 378 typedef struct ssl_ech_keys_st SSL_ECH_KEYS; 379 typedef struct ssl_method_st SSL_METHOD; 380 typedef struct ssl_private_key_method_st SSL_PRIVATE_KEY_METHOD; 381 typedef struct ssl_quic_method_st SSL_QUIC_METHOD; 382 typedef struct ssl_session_st SSL_SESSION; 383 typedef struct ssl_st SSL; 384 typedef struct ssl_ticket_aead_method_st SSL_TICKET_AEAD_METHOD; 385 typedef struct st_ERR_FNS ERR_FNS; 386 typedef struct trust_token_st TRUST_TOKEN; 387 typedef struct trust_token_client_st TRUST_TOKEN_CLIENT; 388 typedef struct trust_token_issuer_st TRUST_TOKEN_ISSUER; 389 typedef struct trust_token_method_st TRUST_TOKEN_METHOD; 390 typedef struct v3_ext_ctx X509V3_CTX; 391 typedef struct v3_ext_method X509V3_EXT_METHOD; 392 typedef struct x509_attributes_st X509_ATTRIBUTE; 393 typedef struct x509_lookup_st X509_LOOKUP; 394 typedef struct x509_lookup_method_st X509_LOOKUP_METHOD; 395 typedef struct x509_object_st X509_OBJECT; 396 typedef struct x509_purpose_st X509_PURPOSE; 397 typedef struct x509_revoked_st X509_REVOKED; 398 typedef struct x509_st X509; 399 typedef struct x509_store_ctx_st X509_STORE_CTX; 400 typedef struct x509_store_st X509_STORE; 401 402 typedef void *OPENSSL_BLOCK; 403 404 // BSSL_CHECK aborts if |condition| is not true. 405 #define BSSL_CHECK(condition) \ 406 do { \ 407 if (!(condition)) { \ 408 abort(); \ 409 } \ 410 } while (0); 411 412 #if defined(__cplusplus) 413 } // extern C 414 #elif !defined(BORINGSSL_NO_CXX) 415 #define BORINGSSL_NO_CXX 416 #endif 417 418 #if defined(BORINGSSL_PREFIX) 419 #define BSSL_NAMESPACE_BEGIN \ 420 namespace bssl { \ 421 inline namespace BORINGSSL_PREFIX { 422 #define BSSL_NAMESPACE_END \ 423 } \ 424 } 425 #else 426 #define BSSL_NAMESPACE_BEGIN namespace bssl { 427 #define BSSL_NAMESPACE_END } 428 #endif 429 430 // MSVC doesn't set __cplusplus to 201103 to indicate C++11 support (see 431 // https://connect.microsoft.com/VisualStudio/feedback/details/763051/a-value-of-predefined-macro-cplusplus-is-still-199711l) 432 // so MSVC is just assumed to support C++11. 433 #if !defined(BORINGSSL_NO_CXX) && __cplusplus < 201103L && !defined(_MSC_VER) 434 #define BORINGSSL_NO_CXX 435 #endif 436 437 #if !defined(BORINGSSL_NO_CXX) 438 439 extern "C++" { 440 441 #include <memory> 442 443 // STLPort, used by some Android consumers, not have std::unique_ptr. 444 #if defined(_STLPORT_VERSION) 445 #define BORINGSSL_NO_CXX 446 #endif 447 448 } // extern C++ 449 #endif // !BORINGSSL_NO_CXX 450 451 #if defined(BORINGSSL_NO_CXX) 452 453 #define BORINGSSL_MAKE_DELETER(type, deleter) 454 #define BORINGSSL_MAKE_UP_REF(type, up_ref_func) 455 456 #else 457 458 extern "C++" { 459 460 BSSL_NAMESPACE_BEGIN 461 462 namespace internal { 463 464 // The Enable parameter is ignored and only exists so specializations can use 465 // SFINAE. 466 template <typename T, typename Enable = void> 467 struct DeleterImpl {}; 468 469 struct Deleter { 470 template <typename T> operatorDeleter471 void operator()(T *ptr) { 472 // Rather than specialize Deleter for each type, we specialize 473 // DeleterImpl. This allows bssl::UniquePtr<T> to be used while only 474 // including base.h as long as the destructor is not emitted. This matches 475 // std::unique_ptr's behavior on forward-declared types. 476 // 477 // DeleterImpl itself is specialized in the corresponding module's header 478 // and must be included to release an object. If not included, the compiler 479 // will error that DeleterImpl<T> does not have a method Free. 480 DeleterImpl<T>::Free(ptr); 481 } 482 }; 483 484 template <typename T, typename CleanupRet, void (*init)(T *), 485 CleanupRet (*cleanup)(T *)> 486 class StackAllocated { 487 public: StackAllocated()488 StackAllocated() { init(&ctx_); } ~StackAllocated()489 ~StackAllocated() { cleanup(&ctx_); } 490 491 StackAllocated(const StackAllocated &) = delete; 492 StackAllocated &operator=(const StackAllocated &) = delete; 493 get()494 T *get() { return &ctx_; } get()495 const T *get() const { return &ctx_; } 496 497 T *operator->() { return &ctx_; } 498 const T *operator->() const { return &ctx_; } 499 Reset()500 void Reset() { 501 cleanup(&ctx_); 502 init(&ctx_); 503 } 504 505 private: 506 T ctx_; 507 }; 508 509 template <typename T, typename CleanupRet, void (*init)(T *), 510 CleanupRet (*cleanup)(T *), void (*move)(T *, T *)> 511 class StackAllocatedMovable { 512 public: StackAllocatedMovable()513 StackAllocatedMovable() { init(&ctx_); } ~StackAllocatedMovable()514 ~StackAllocatedMovable() { cleanup(&ctx_); } 515 StackAllocatedMovable(StackAllocatedMovable && other)516 StackAllocatedMovable(StackAllocatedMovable &&other) { 517 init(&ctx_); 518 move(&ctx_, &other.ctx_); 519 } 520 StackAllocatedMovable &operator=(StackAllocatedMovable &&other) { 521 move(&ctx_, &other.ctx_); 522 return *this; 523 } 524 get()525 T *get() { return &ctx_; } get()526 const T *get() const { return &ctx_; } 527 528 T *operator->() { return &ctx_; } 529 const T *operator->() const { return &ctx_; } 530 Reset()531 void Reset() { 532 cleanup(&ctx_); 533 init(&ctx_); 534 } 535 536 private: 537 T ctx_; 538 }; 539 540 } // namespace internal 541 542 #define BORINGSSL_MAKE_DELETER(type, deleter) \ 543 namespace internal { \ 544 template <> \ 545 struct DeleterImpl<type> { \ 546 static void Free(type *ptr) { deleter(ptr); } \ 547 }; \ 548 } 549 550 // Holds ownership of heap-allocated BoringSSL structures. Sample usage: 551 // bssl::UniquePtr<RSA> rsa(RSA_new()); 552 // bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem())); 553 template <typename T> 554 using UniquePtr = std::unique_ptr<T, internal::Deleter>; 555 556 #define BORINGSSL_MAKE_UP_REF(type, up_ref_func) \ 557 inline UniquePtr<type> UpRef(type *v) { \ 558 if (v != nullptr) { \ 559 up_ref_func(v); \ 560 } \ 561 return UniquePtr<type>(v); \ 562 } \ 563 \ 564 inline UniquePtr<type> UpRef(const UniquePtr<type> &ptr) { \ 565 return UpRef(ptr.get()); \ 566 } 567 568 BSSL_NAMESPACE_END 569 570 } // extern C++ 571 572 #endif // !BORINGSSL_NO_CXX 573 574 #endif // OPENSSL_HEADER_BASE_H 575