1 // Copyright 2021 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://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 /////////////////////////////////////////////////////////////////////////////// 16 #ifndef TINK_INTERNAL_SSL_UNIQUE_PTR_H_ 17 #define TINK_INTERNAL_SSL_UNIQUE_PTR_H_ 18 19 #include <memory> 20 21 // Every header in BoringSSL includes base.h, which in turn defines 22 // OPENSSL_IS_BORINGSSL. So we include this common header here to "force" the 23 // definition of OPENSSL_IS_BORINGSSL in case BoringSSL is used. 24 #include "openssl/crypto.h" 25 26 #ifndef OPENSSL_IS_BORINGSSL 27 #include "openssl/bn.h" 28 #include "openssl/cmac.h" 29 #include "openssl/ec.h" 30 #include "openssl/evp.h" 31 #include "openssl/hmac.h" 32 #include "openssl/rsa.h" 33 #endif 34 35 namespace crypto { 36 namespace tink { 37 namespace internal { 38 39 #ifdef OPENSSL_IS_BORINGSSL 40 41 // In this case, simply use BoringSSL's UniquePtr. 42 template <typename T> 43 using SslUniquePtr = ::bssl::UniquePtr<T>; 44 45 #else 46 47 // We define SslUniquePtr similarly to how bssl::UniquePtr<T> is defined, 48 // i.e., as a unique_ptr with custom deleter for each type T. The difference 49 // w.r.t. the BoringSSL equivalent is that we have to define each deleter here 50 // explicitly, while bssl::UniquePtr allows for forward declaration and 51 // later specialization when including specific headers. This is possible in 52 // BoringSSL because each module's header defines the appropriate deleter with 53 // BORINGSSL_MAKE_DELETER, which is not the case for OpenSSL. 54 55 template <typename T> 56 struct Deleter { 57 void operator()(T* ptr); 58 }; 59 60 // Here are all the custom deleters. 61 template <> 62 struct Deleter<BIO> { 63 void operator()(BIO* ptr) { BIO_free(ptr); } 64 }; 65 template <> 66 struct Deleter<EVP_CIPHER_CTX> { 67 void operator()(EVP_CIPHER_CTX* ptr) { EVP_CIPHER_CTX_free(ptr); } 68 }; 69 template <> 70 struct Deleter<BIGNUM> { 71 void operator()(BIGNUM* ptr) { BN_free(ptr); } 72 }; 73 template <> 74 struct Deleter<BN_CTX> { 75 void operator()(BN_CTX* ptr) { BN_CTX_free(ptr); } 76 }; 77 template <> 78 struct Deleter<RSA> { 79 void operator()(RSA* ptr) { RSA_free(ptr); } 80 }; 81 template <> 82 struct Deleter<EC_POINT> { 83 void operator()(EC_POINT* ptr) { EC_POINT_free(ptr); } 84 }; 85 template <> 86 struct Deleter<EC_GROUP> { 87 void operator()(EC_GROUP* ptr) { EC_GROUP_free(ptr); } 88 }; 89 template <> 90 struct Deleter<EC_KEY> { 91 void operator()(EC_KEY* ptr) { EC_KEY_free(ptr); } 92 }; 93 template <> 94 struct Deleter<EVP_PKEY> { 95 void operator()(EVP_PKEY* ptr) { EVP_PKEY_free(ptr); } 96 }; 97 template <> 98 struct Deleter<EVP_PKEY_CTX> { 99 void operator()(EVP_PKEY_CTX* ptr) { EVP_PKEY_CTX_free(ptr); } 100 }; 101 template <> 102 struct Deleter<ECDSA_SIG> { 103 void operator()(ECDSA_SIG* ptr) { ECDSA_SIG_free(ptr); } 104 }; 105 template <> 106 struct Deleter<CMAC_CTX> { 107 void operator()(CMAC_CTX* ptr) { CMAC_CTX_free(ptr); } 108 }; 109 template <> 110 struct Deleter<EVP_MD_CTX> { 111 void operator()(EVP_MD_CTX* ptr) { EVP_MD_CTX_free(ptr); } 112 }; 113 template <> 114 struct Deleter<HMAC_CTX> { 115 void operator()(HMAC_CTX* ptr) { HMAC_CTX_free(ptr); } 116 }; 117 118 template <typename T> 119 using SslUniquePtr = std::unique_ptr<T, Deleter<T> >; 120 121 #endif // OPENSSL_IS_BORINGSSL 122 123 } // namespace internal 124 } // namespace tink 125 } // namespace crypto 126 127 #endif // TINK_INTERNAL_SSL_UNIQUE_PTR_H_ 128