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