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