1 /* Copyright (c) 2015, Google Inc. 2 * 3 * Permission to use, copy, modify, and/or distribute this software for any 4 * purpose with or without fee is hereby granted, provided that the above 5 * copyright notice and this permission notice appear in all copies. 6 * 7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ 14 15 #ifndef OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H 16 #define OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H 17 18 #include <stdint.h> 19 #include <stdio.h> 20 21 #include <openssl/bio.h> 22 #include <openssl/bn.h> 23 #include <openssl/cmac.h> 24 #include <openssl/dh.h> 25 #include <openssl/ec.h> 26 #include <openssl/ec_key.h> 27 #include <openssl/ecdsa.h> 28 #include <openssl/evp.h> 29 #include <openssl/hmac.h> 30 #include <openssl/mem.h> 31 #include <openssl/pkcs8.h> 32 #include <openssl/rsa.h> 33 #include <openssl/stack.h> 34 #include <openssl/x509.h> 35 36 #include "stl_compat.h" 37 38 39 template<typename T, void (*func)(T*)> 40 struct OpenSSLDeleter { operatorOpenSSLDeleter41 void operator()(T *obj) { 42 func(obj); 43 } 44 }; 45 46 template<typename StackType, typename T, void (*func)(T*)> 47 struct OpenSSLStackDeleter { operatorOpenSSLStackDeleter48 void operator()(StackType *obj) { 49 sk_pop_free(reinterpret_cast<_STACK*>(obj), 50 reinterpret_cast<void (*)(void *)>(func)); 51 } 52 }; 53 54 template<typename T> 55 struct OpenSSLFree { operatorOpenSSLFree56 void operator()(T *buf) { 57 OPENSSL_free(buf); 58 } 59 }; 60 61 struct FileCloser { operatorFileCloser62 void operator()(FILE *file) { 63 fclose(file); 64 } 65 }; 66 67 template<typename T, void (*func)(T*)> 68 using ScopedOpenSSLType = bssl::unique_ptr<T, OpenSSLDeleter<T, func>>; 69 70 template<typename StackType, typename T, void (*func)(T*)> 71 using ScopedOpenSSLStack = 72 bssl::unique_ptr<StackType, OpenSSLStackDeleter<StackType, T, func>>; 73 74 template<typename T, typename CleanupRet, void (*init_func)(T*), 75 CleanupRet (*cleanup_func)(T*)> 76 class ScopedOpenSSLContext { 77 public: ScopedOpenSSLContext()78 ScopedOpenSSLContext() { 79 init_func(&ctx_); 80 } ~ScopedOpenSSLContext()81 ~ScopedOpenSSLContext() { 82 cleanup_func(&ctx_); 83 } 84 get()85 T *get() { return &ctx_; } get()86 const T *get() const { return &ctx_; } 87 Reset()88 void Reset() { 89 cleanup_func(&ctx_); 90 init_func(&ctx_); 91 } 92 93 private: 94 T ctx_; 95 }; 96 97 using ScopedBIO = ScopedOpenSSLType<BIO, BIO_vfree>; 98 using ScopedBIGNUM = ScopedOpenSSLType<BIGNUM, BN_free>; 99 using ScopedBN_CTX = ScopedOpenSSLType<BN_CTX, BN_CTX_free>; 100 using ScopedBN_MONT_CTX = ScopedOpenSSLType<BN_MONT_CTX, BN_MONT_CTX_free>; 101 using ScopedCMAC_CTX = ScopedOpenSSLType<CMAC_CTX, CMAC_CTX_free>; 102 using ScopedDH = ScopedOpenSSLType<DH, DH_free>; 103 using ScopedECDSA_SIG = ScopedOpenSSLType<ECDSA_SIG, ECDSA_SIG_free>; 104 using ScopedEC_GROUP = ScopedOpenSSLType<EC_GROUP, EC_GROUP_free>; 105 using ScopedEC_KEY = ScopedOpenSSLType<EC_KEY, EC_KEY_free>; 106 using ScopedEC_POINT = ScopedOpenSSLType<EC_POINT, EC_POINT_free>; 107 using ScopedEVP_PKEY = ScopedOpenSSLType<EVP_PKEY, EVP_PKEY_free>; 108 using ScopedEVP_PKEY_CTX = ScopedOpenSSLType<EVP_PKEY_CTX, EVP_PKEY_CTX_free>; 109 using ScopedPKCS8_PRIV_KEY_INFO = ScopedOpenSSLType<PKCS8_PRIV_KEY_INFO, 110 PKCS8_PRIV_KEY_INFO_free>; 111 using ScopedPKCS12 = ScopedOpenSSLType<PKCS12, PKCS12_free>; 112 using ScopedRSA = ScopedOpenSSLType<RSA, RSA_free>; 113 using ScopedX509 = ScopedOpenSSLType<X509, X509_free>; 114 using ScopedX509_ALGOR = ScopedOpenSSLType<X509_ALGOR, X509_ALGOR_free>; 115 116 using ScopedX509Stack = ScopedOpenSSLStack<STACK_OF(X509), X509, X509_free>; 117 118 using ScopedEVP_CIPHER_CTX = ScopedOpenSSLContext<EVP_CIPHER_CTX, int, 119 EVP_CIPHER_CTX_init, 120 EVP_CIPHER_CTX_cleanup>; 121 using ScopedEVP_MD_CTX = ScopedOpenSSLContext<EVP_MD_CTX, int, EVP_MD_CTX_init, 122 EVP_MD_CTX_cleanup>; 123 using ScopedHMAC_CTX = ScopedOpenSSLContext<HMAC_CTX, void, HMAC_CTX_init, 124 HMAC_CTX_cleanup>; 125 126 using ScopedOpenSSLBytes = bssl::unique_ptr<uint8_t, OpenSSLFree<uint8_t>>; 127 using ScopedOpenSSLString = bssl::unique_ptr<char, OpenSSLFree<char>>; 128 129 using ScopedFILE = bssl::unique_ptr<FILE, FileCloser>; 130 131 #endif // OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H 132