7349273c384504988902291fc28dfd1c5e4af1d6
[openssl.git] / test / ossl_shim / include / openssl / base.h
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 /* Needed for BORINGSSL_MAKE_DELETER */
57 # include <openssl/bio.h>
58 # include <openssl/evp.h>
59 # include <openssl/dh.h>
60 # include <openssl/x509.h>
61 # include <openssl/ssl.h>
62
63 # define OPENSSL_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
64
65 extern "C++" {
66
67 #include <memory>
68
69 namespace bssl {
70
71 namespace internal {
72
73 template <typename T>
74 struct DeleterImpl {};
75
76 template <typename T>
77 struct Deleter {
78   void operator()(T *ptr) {
79     // Rather than specialize Deleter for each type, we specialize
80     // DeleterImpl. This allows bssl::UniquePtr<T> to be used while only
81     // including base.h as long as the destructor is not emitted. This matches
82     // std::unique_ptr's behavior on forward-declared types.
83     //
84     // DeleterImpl itself is specialized in the corresponding module's header
85     // and must be included to release an object. If not included, the compiler
86     // will error that DeleterImpl<T> does not have a method Free.
87     DeleterImpl<T>::Free(ptr);
88   }
89 };
90
91 template <typename T, typename CleanupRet, void (*init)(T *),
92           CleanupRet (*cleanup)(T *)>
93 class StackAllocated {
94  public:
95   StackAllocated() { init(&ctx_); }
96   ~StackAllocated() { cleanup(&ctx_); }
97
98   StackAllocated(const StackAllocated<T, CleanupRet, init, cleanup> &) = delete;
99   T& operator=(const StackAllocated<T, CleanupRet, init, cleanup> &) = delete;
100
101   T *get() { return &ctx_; }
102   const T *get() const { return &ctx_; }
103
104   void Reset() {
105     cleanup(&ctx_);
106     init(&ctx_);
107   }
108
109  private:
110   T ctx_;
111 };
112
113 }  // namespace internal
114
115 #define BORINGSSL_MAKE_DELETER(type, deleter)     \
116   namespace internal {                            \
117   template <>                                     \
118   struct DeleterImpl<type> {                      \
119     static void Free(type *ptr) { deleter(ptr); } \
120   };                                              \
121   }
122
123 // This makes a unique_ptr to STACK_OF(type) that owns all elements on the
124 // stack, i.e. it uses sk_pop_free() to clean up.
125 #define BORINGSSL_MAKE_STACK_DELETER(type, deleter) \
126   namespace internal {                              \
127   template <>                                       \
128   struct DeleterImpl<STACK_OF(type)> {              \
129     static void Free(STACK_OF(type) *ptr) {         \
130       sk_##type##_pop_free(ptr, deleter);           \
131     }                                               \
132   };                                                \
133   }
134
135 // Holds ownership of heap-allocated BoringSSL structures. Sample usage:
136 //   bssl::UniquePtr<BIO> rsa(RSA_new());
137 //   bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
138 template <typename T>
139 using UniquePtr = std::unique_ptr<T, internal::Deleter<T>>;
140
141 BORINGSSL_MAKE_DELETER(BIO, BIO_free)
142 BORINGSSL_MAKE_DELETER(EVP_PKEY, EVP_PKEY_free)
143 BORINGSSL_MAKE_DELETER(DH, DH_free)
144 BORINGSSL_MAKE_DELETER(X509, X509_free)
145 BORINGSSL_MAKE_DELETER(SSL, SSL_free)
146 BORINGSSL_MAKE_DELETER(SSL_CTX, SSL_CTX_free)
147 BORINGSSL_MAKE_DELETER(SSL_SESSION, SSL_SESSION_free)
148
149 }  // namespace bssl
150
151 }  /* extern C++ */
152
153
154 #endif  /* OPENSSL_HEADER_BASE_H */