1
/* Copyright (c) 2015, Google Inc.
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.
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. */
15
#ifndef OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H
16
#define OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H
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>
36
#include "stl_compat.h"
39
template<typename T, void (*func)(T*)>
40
struct OpenSSLDeleter {
41
void operator()(T *obj) {
46
template<typename StackType, typename T, void (*func)(T*)>
47
struct OpenSSLStackDeleter {
48
void operator()(StackType *obj) {
49
sk_pop_free(reinterpret_cast<_STACK*>(obj),
50
reinterpret_cast<void (*)(void *)>(func));
56
void operator()(T *buf) {
62
void operator()(FILE *file) {
67
template<typename T, void (*func)(T*)>
68
using ScopedOpenSSLType = bssl::unique_ptr<T, OpenSSLDeleter<T, func>>;
70
template<typename StackType, typename T, void (*func)(T*)>
71
using ScopedOpenSSLStack =
72
bssl::unique_ptr<StackType, OpenSSLStackDeleter<StackType, T, func>>;
74
template<typename T, typename CleanupRet, void (*init_func)(T*),
75
CleanupRet (*cleanup_func)(T*)>
76
class ScopedOpenSSLContext {
78
ScopedOpenSSLContext() {
81
~ScopedOpenSSLContext() {
85
T *get() { return &ctx_; }
86
const T *get() const { return &ctx_; }
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>;
116
using ScopedX509Stack = ScopedOpenSSLStack<STACK_OF(X509), X509, X509_free>;
118
using ScopedEVP_CIPHER_CTX = ScopedOpenSSLContext<EVP_CIPHER_CTX, int,
120
EVP_CIPHER_CTX_cleanup>;
121
using ScopedEVP_MD_CTX = ScopedOpenSSLContext<EVP_MD_CTX, int, EVP_MD_CTX_init,
123
using ScopedHMAC_CTX = ScopedOpenSSLContext<HMAC_CTX, void, HMAC_CTX_init,
126
using ScopedOpenSSLBytes = bssl::unique_ptr<uint8_t, OpenSSLFree<uint8_t>>;
127
using ScopedOpenSSLString = bssl::unique_ptr<char, OpenSSLFree<char>>;
129
using ScopedFILE = bssl::unique_ptr<FILE, FileCloser>;
131
#endif // OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H