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 * [email protected]. 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 * ([email protected]). This product includes software written by Tim 51 * Hudson ([email protected]). */ 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 32 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 #else 201 #define OPENSSL_UNUSED 202 #endif 203 204 // C and C++ handle inline functions differently. In C++, an inline function is 205 // defined in just the header file, potentially emitted in multiple compilation 206 // units (in cases the compiler did not inline), but each copy must be identical 207 // to satsify ODR. In C, a non-static inline must be manually emitted in exactly 208 // one compilation unit with a separate extern inline declaration. 209 // 210 // In both languages, exported inline functions referencing file-local symbols 211 // are problematic. C forbids this altogether (though GCC and Clang seem not to 212 // enforce it). It works in C++, but ODR requires the definitions be identical, 213 // including all names in the definitions resolving to the "same entity". In 214 // practice, this is unlikely to be a problem, but an inline function that 215 // returns a pointer to a file-local symbol 216 // could compile oddly. 217 // 218 // Historically, we used static inline in headers. However, to satisfy ODR, use 219 // plain inline in C++, to allow inline consumer functions to call our header 220 // functions. Plain inline would also work better with C99 inline, but that is 221 // not used much in practice, extern inline is tedious, and there are conflicts 222 // with the old gnu89 model: 223 // https://stackoverflow.com/questions/216510/extern-inline 224 #if defined(__cplusplus) 225 #define OPENSSL_INLINE inline 226 #else 227 // Add OPENSSL_UNUSED so that, should an inline function be emitted via macro 228 // (e.g. a |STACK_OF(T)| implementation) in a source file without tripping 229 // clang's -Wunused-function. 230 #define OPENSSL_INLINE static inline OPENSSL_UNUSED 231 #endif 232 233 #if defined(__cplusplus) 234 // enums can be predeclared, but only in C++ and only if given an explicit type. 235 // C doesn't support setting an explicit type for enums thus a #define is used 236 // to do this only for C++. However, the ABI type between C and C++ need to have 237 // equal sizes, which is confirmed in a unittest. 238 #define BORINGSSL_ENUM_INT : int 239 enum ssl_early_data_reason_t BORINGSSL_ENUM_INT; 240 enum ssl_encryption_level_t BORINGSSL_ENUM_INT; 241 enum ssl_private_key_result_t BORINGSSL_ENUM_INT; 242 enum ssl_renegotiate_mode_t BORINGSSL_ENUM_INT; 243 enum ssl_select_cert_result_t BORINGSSL_ENUM_INT; 244 enum ssl_select_cert_result_t BORINGSSL_ENUM_INT; 245 enum ssl_ticket_aead_result_t BORINGSSL_ENUM_INT; 246 enum ssl_verify_result_t BORINGSSL_ENUM_INT; 247 #else 248 #define BORINGSSL_ENUM_INT 249 #endif 250 251 // ossl_ssize_t is a signed type which is large enough to fit the size of any 252 // valid memory allocation. We prefer using |size_t|, but sometimes we need a 253 // signed type for OpenSSL API compatibility. This type can be used in such 254 // cases to avoid overflow. 255 // 256 // Not all |size_t| values fit in |ossl_ssize_t|, but all |size_t| values that 257 // are sizes of or indices into C objects, can be converted without overflow. 258 typedef ptrdiff_t ossl_ssize_t; 259 260 // CBS_ASN1_TAG is the type used by |CBS| and |CBB| for ASN.1 tags. See that 261 // header for details. This type is defined in base.h as a forward declaration. 262 typedef uint32_t CBS_ASN1_TAG; 263 264 // CRYPTO_THREADID is a dummy value. 265 typedef int CRYPTO_THREADID; 266 267 // An |ASN1_NULL| is an opaque type. asn1.h represents the ASN.1 NULL value as 268 // an opaque, non-NULL |ASN1_NULL*| pointer. 269 typedef struct asn1_null_st ASN1_NULL; 270 271 typedef int ASN1_BOOLEAN; 272 typedef struct ASN1_ITEM_st ASN1_ITEM; 273 typedef struct asn1_object_st ASN1_OBJECT; 274 typedef struct asn1_pctx_st ASN1_PCTX; 275 typedef struct asn1_string_st ASN1_BIT_STRING; 276 typedef struct asn1_string_st ASN1_BMPSTRING; 277 typedef struct asn1_string_st ASN1_ENUMERATED; 278 typedef struct asn1_string_st ASN1_GENERALIZEDTIME; 279 typedef struct asn1_string_st ASN1_GENERALSTRING; 280 typedef struct asn1_string_st ASN1_IA5STRING; 281 typedef struct asn1_string_st ASN1_INTEGER; 282 typedef struct asn1_string_st ASN1_OCTET_STRING; 283 typedef struct asn1_string_st ASN1_PRINTABLESTRING; 284 typedef struct asn1_string_st ASN1_STRING; 285 typedef struct asn1_string_st ASN1_T61STRING; 286 typedef struct asn1_string_st ASN1_TIME; 287 typedef struct asn1_string_st ASN1_UNIVERSALSTRING; 288 typedef struct asn1_string_st ASN1_UTCTIME; 289 typedef struct asn1_string_st ASN1_UTF8STRING; 290 typedef struct asn1_string_st ASN1_VISIBLESTRING; 291 typedef struct asn1_type_st ASN1_TYPE; 292 typedef struct AUTHORITY_KEYID_st AUTHORITY_KEYID; 293 typedef struct BASIC_CONSTRAINTS_st BASIC_CONSTRAINTS; 294 typedef struct DIST_POINT_st DIST_POINT; 295 typedef struct DSA_SIG_st DSA_SIG; 296 typedef struct GENERAL_NAME_st GENERAL_NAME; 297 typedef struct ISSUING_DIST_POINT_st ISSUING_DIST_POINT; 298 typedef struct NAME_CONSTRAINTS_st NAME_CONSTRAINTS; 299 typedef struct Netscape_spkac_st NETSCAPE_SPKAC; 300 typedef struct Netscape_spki_st NETSCAPE_SPKI; 301 typedef struct RIPEMD160state_st RIPEMD160_CTX; 302 typedef struct X509_VERIFY_PARAM_st X509_VERIFY_PARAM; 303 typedef struct X509_algor_st X509_ALGOR; 304 typedef struct X509_crl_st X509_CRL; 305 typedef struct X509_extension_st X509_EXTENSION; 306 typedef struct X509_info_st X509_INFO; 307 typedef struct X509_name_entry_st X509_NAME_ENTRY; 308 typedef struct X509_name_st X509_NAME; 309 typedef struct X509_pubkey_st X509_PUBKEY; 310 typedef struct X509_req_st X509_REQ; 311 typedef struct X509_sig_st X509_SIG; 312 typedef struct bignum_ctx BN_CTX; 313 typedef struct bignum_st BIGNUM; 314 typedef struct bio_method_st BIO_METHOD; 315 typedef struct bio_st BIO; 316 typedef struct blake2b_state_st BLAKE2B_CTX; 317 typedef struct bn_gencb_st BN_GENCB; 318 typedef struct bn_mont_ctx_st BN_MONT_CTX; 319 typedef struct buf_mem_st BUF_MEM; 320 typedef struct cbb_st CBB; 321 typedef struct cbs_st CBS; 322 typedef struct cmac_ctx_st CMAC_CTX; 323 typedef struct conf_st CONF; 324 typedef struct conf_value_st CONF_VALUE; 325 typedef struct crypto_buffer_pool_st CRYPTO_BUFFER_POOL; 326 typedef struct crypto_buffer_st CRYPTO_BUFFER; 327 typedef struct ctr_drbg_state_st CTR_DRBG_STATE; 328 typedef struct dh_st DH; 329 typedef struct dsa_st DSA; 330 typedef struct ec_group_st EC_GROUP; 331 typedef struct ec_key_st EC_KEY; 332 typedef struct ec_point_st EC_POINT; 333 typedef struct ecdsa_method_st ECDSA_METHOD; 334 typedef struct ecdsa_sig_st ECDSA_SIG; 335 typedef struct engine_st ENGINE; 336 typedef struct env_md_ctx_st EVP_MD_CTX; 337 typedef struct env_md_st EVP_MD; 338 typedef struct evp_aead_st EVP_AEAD; 339 typedef struct evp_aead_ctx_st EVP_AEAD_CTX; 340 typedef struct evp_cipher_ctx_st EVP_CIPHER_CTX; 341 typedef struct evp_cipher_st EVP_CIPHER; 342 typedef struct evp_encode_ctx_st EVP_ENCODE_CTX; 343 typedef struct evp_hpke_aead_st EVP_HPKE_AEAD; 344 typedef struct evp_hpke_ctx_st EVP_HPKE_CTX; 345 typedef struct evp_hpke_kdf_st EVP_HPKE_KDF; 346 typedef struct evp_hpke_kem_st EVP_HPKE_KEM; 347 typedef struct evp_hpke_key_st EVP_HPKE_KEY; 348 typedef struct evp_pkey_ctx_st EVP_PKEY_CTX; 349 typedef struct evp_pkey_st EVP_PKEY; 350 typedef struct hmac_ctx_st HMAC_CTX; 351 typedef struct md4_state_st MD4_CTX; 352 typedef struct md5_state_st MD5_CTX; 353 typedef struct ossl_init_settings_st OPENSSL_INIT_SETTINGS; 354 typedef struct pkcs12_st PKCS12; 355 typedef struct pkcs8_priv_key_info_st PKCS8_PRIV_KEY_INFO; 356 typedef struct private_key_st X509_PKEY; 357 typedef struct rand_meth_st RAND_METHOD; 358 typedef struct rc4_key_st RC4_KEY; 359 typedef struct rsa_meth_st RSA_METHOD; 360 typedef struct rsa_pss_params_st RSA_PSS_PARAMS; 361 typedef struct rsa_st RSA; 362 typedef struct sha256_state_st SHA256_CTX; 363 typedef struct sha512_state_st SHA512_CTX; 364 typedef struct sha_state_st SHA_CTX; 365 typedef struct spake2_ctx_st SPAKE2_CTX; 366 typedef struct srtp_protection_profile_st SRTP_PROTECTION_PROFILE; 367 typedef struct ssl_cipher_st SSL_CIPHER; 368 typedef struct ssl_credential_st SSL_CREDENTIAL; 369 typedef struct ssl_ctx_st SSL_CTX; 370 typedef struct ssl_early_callback_ctx SSL_CLIENT_HELLO; 371 typedef struct ssl_ech_keys_st SSL_ECH_KEYS; 372 typedef struct ssl_method_st SSL_METHOD; 373 typedef struct ssl_private_key_method_st SSL_PRIVATE_KEY_METHOD; 374 typedef struct ssl_quic_method_st SSL_QUIC_METHOD; 375 typedef struct ssl_session_st SSL_SESSION; 376 typedef struct ssl_st SSL; 377 typedef struct ssl_ticket_aead_method_st SSL_TICKET_AEAD_METHOD; 378 typedef struct st_ERR_FNS ERR_FNS; 379 typedef struct trust_token_st TRUST_TOKEN; 380 typedef struct trust_token_client_st TRUST_TOKEN_CLIENT; 381 typedef struct trust_token_issuer_st TRUST_TOKEN_ISSUER; 382 typedef struct trust_token_method_st TRUST_TOKEN_METHOD; 383 typedef struct v3_ext_ctx X509V3_CTX; 384 typedef struct v3_ext_method X509V3_EXT_METHOD; 385 typedef struct x509_attributes_st X509_ATTRIBUTE; 386 typedef struct x509_lookup_st X509_LOOKUP; 387 typedef struct x509_lookup_method_st X509_LOOKUP_METHOD; 388 typedef struct x509_object_st X509_OBJECT; 389 typedef struct x509_purpose_st X509_PURPOSE; 390 typedef struct x509_revoked_st X509_REVOKED; 391 typedef struct x509_st X509; 392 typedef struct x509_store_ctx_st X509_STORE_CTX; 393 typedef struct x509_store_st X509_STORE; 394 395 typedef void *OPENSSL_BLOCK; 396 397 // BSSL_CHECK aborts if |condition| is not true. 398 #define BSSL_CHECK(condition) \ 399 do { \ 400 if (!(condition)) { \ 401 abort(); \ 402 } \ 403 } while (0); 404 405 #if defined(__cplusplus) 406 } // extern C 407 #elif !defined(BORINGSSL_NO_CXX) 408 #define BORINGSSL_NO_CXX 409 #endif 410 411 #if defined(BORINGSSL_PREFIX) 412 #define BSSL_NAMESPACE_BEGIN \ 413 namespace bssl { \ 414 inline namespace BORINGSSL_PREFIX { 415 #define BSSL_NAMESPACE_END \ 416 } \ 417 } 418 #else 419 #define BSSL_NAMESPACE_BEGIN namespace bssl { 420 #define BSSL_NAMESPACE_END } 421 #endif 422 423 // MSVC doesn't set __cplusplus to 201103 to indicate C++11 support (see 424 // https://connect.microsoft.com/VisualStudio/feedback/details/763051/a-value-of-predefined-macro-cplusplus-is-still-199711l) 425 // so MSVC is just assumed to support C++11. 426 #if !defined(BORINGSSL_NO_CXX) && __cplusplus < 201103L && !defined(_MSC_VER) 427 #define BORINGSSL_NO_CXX 428 #endif 429 430 #if !defined(BORINGSSL_NO_CXX) 431 432 extern "C++" { 433 434 #include <memory> 435 436 // STLPort, used by some Android consumers, not have std::unique_ptr. 437 #if defined(_STLPORT_VERSION) 438 #define BORINGSSL_NO_CXX 439 #endif 440 441 } // extern C++ 442 #endif // !BORINGSSL_NO_CXX 443 444 #if defined(BORINGSSL_NO_CXX) 445 446 #define BORINGSSL_MAKE_DELETER(type, deleter) 447 #define BORINGSSL_MAKE_UP_REF(type, up_ref_func) 448 449 #else 450 451 extern "C++" { 452 453 BSSL_NAMESPACE_BEGIN 454 455 namespace internal { 456 457 // The Enable parameter is ignored and only exists so specializations can use 458 // SFINAE. 459 template <typename T, typename Enable = void> 460 struct DeleterImpl {}; 461 462 struct Deleter { 463 template <typename T> operatorDeleter464 void operator()(T *ptr) { 465 // Rather than specialize Deleter for each type, we specialize 466 // DeleterImpl. This allows bssl::UniquePtr<T> to be used while only 467 // including base.h as long as the destructor is not emitted. This matches 468 // std::unique_ptr's behavior on forward-declared types. 469 // 470 // DeleterImpl itself is specialized in the corresponding module's header 471 // and must be included to release an object. If not included, the compiler 472 // will error that DeleterImpl<T> does not have a method Free. 473 DeleterImpl<T>::Free(ptr); 474 } 475 }; 476 477 template <typename T, typename CleanupRet, void (*init)(T *), 478 CleanupRet (*cleanup)(T *)> 479 class StackAllocated { 480 public: StackAllocated()481 StackAllocated() { init(&ctx_); } ~StackAllocated()482 ~StackAllocated() { cleanup(&ctx_); } 483 484 StackAllocated(const StackAllocated &) = delete; 485 StackAllocated& operator=(const StackAllocated &) = delete; 486 get()487 T *get() { return &ctx_; } get()488 const T *get() const { return &ctx_; } 489 490 T *operator->() { return &ctx_; } 491 const T *operator->() const { return &ctx_; } 492 Reset()493 void Reset() { 494 cleanup(&ctx_); 495 init(&ctx_); 496 } 497 498 private: 499 T ctx_; 500 }; 501 502 template <typename T, typename CleanupRet, void (*init)(T *), 503 CleanupRet (*cleanup)(T *), void (*move)(T *, T *)> 504 class StackAllocatedMovable { 505 public: StackAllocatedMovable()506 StackAllocatedMovable() { init(&ctx_); } ~StackAllocatedMovable()507 ~StackAllocatedMovable() { cleanup(&ctx_); } 508 StackAllocatedMovable(StackAllocatedMovable && other)509 StackAllocatedMovable(StackAllocatedMovable &&other) { 510 init(&ctx_); 511 move(&ctx_, &other.ctx_); 512 } 513 StackAllocatedMovable &operator=(StackAllocatedMovable &&other) { 514 move(&ctx_, &other.ctx_); 515 return *this; 516 } 517 get()518 T *get() { return &ctx_; } get()519 const T *get() const { return &ctx_; } 520 521 T *operator->() { return &ctx_; } 522 const T *operator->() const { return &ctx_; } 523 Reset()524 void Reset() { 525 cleanup(&ctx_); 526 init(&ctx_); 527 } 528 529 private: 530 T ctx_; 531 }; 532 533 } // namespace internal 534 535 #define BORINGSSL_MAKE_DELETER(type, deleter) \ 536 namespace internal { \ 537 template <> \ 538 struct DeleterImpl<type> { \ 539 static void Free(type *ptr) { deleter(ptr); } \ 540 }; \ 541 } 542 543 // Holds ownership of heap-allocated BoringSSL structures. Sample usage: 544 // bssl::UniquePtr<RSA> rsa(RSA_new()); 545 // bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem())); 546 template <typename T> 547 using UniquePtr = std::unique_ptr<T, internal::Deleter>; 548 549 #define BORINGSSL_MAKE_UP_REF(type, up_ref_func) \ 550 inline UniquePtr<type> UpRef(type *v) { \ 551 if (v != nullptr) { \ 552 up_ref_func(v); \ 553 } \ 554 return UniquePtr<type>(v); \ 555 } \ 556 \ 557 inline UniquePtr<type> UpRef(const UniquePtr<type> &ptr) { \ 558 return UpRef(ptr.get()); \ 559 } 560 561 BSSL_NAMESPACE_END 562 563 } // extern C++ 564 565 #endif // !BORINGSSL_NO_CXX 566 567 #endif // OPENSSL_HEADER_BASE_H 568