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