Integrate BoringSSL shim
[openssl.git] / test / ossl_shim / crypto / scoped_types.h
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 <memory>
22
23 #include <openssl/asn1.h>
24 #include <openssl/bio.h>
25 #include <openssl/bn.h>
26 #include <openssl/crypto.h>
27 #include <openssl/cmac.h>
28 #include <openssl/dh.h>
29 #include <openssl/ecdsa.h>
30 #include <openssl/ec.h>
31 #include <openssl/evp.h>
32 #include <openssl/pkcs12.h>
33 #include <openssl/rsa.h>
34 #include <openssl/stack.h>
35 #include <openssl/x509.h>
36
37
38 template<typename T, void (*func)(T*)>
39 struct OpenSSLDeleter {
40   void operator()(T *obj) {
41     func(obj);
42   }
43 };
44
45 template<typename StackType, typename T, void (*func)(T*)>
46 struct OpenSSLStackDeleter {
47   void operator()(StackType *obj) {
48     sk_pop_free(reinterpret_cast<_STACK*>(obj),
49                 reinterpret_cast<void (*)(void *)>(func));
50   }
51 };
52
53 template<typename T>
54 struct OpenSSLFree {
55   void operator()(T *buf) {
56     OPENSSL_free(buf);
57   }
58 };
59
60 struct FileCloser {
61   void operator()(FILE *file) {
62     fclose(file);
63   }
64 };
65
66 template<typename T, void (*func)(T*)>
67 using ScopedOpenSSLType = std::unique_ptr<T, OpenSSLDeleter<T, func>>;
68
69 template<typename StackType, typename T, void (*func)(T*)>
70 using ScopedOpenSSLStack =
71     std::unique_ptr<StackType, OpenSSLStackDeleter<StackType, T, func>>;
72
73 using ScopedASN1_TYPE = ScopedOpenSSLType<ASN1_TYPE, ASN1_TYPE_free>;
74 using ScopedBIO = ScopedOpenSSLType<BIO, BIO_vfree>;
75 using ScopedBIGNUM = ScopedOpenSSLType<BIGNUM, BN_free>;
76 using ScopedBN_CTX = ScopedOpenSSLType<BN_CTX, BN_CTX_free>;
77 using ScopedBN_MONT_CTX = ScopedOpenSSLType<BN_MONT_CTX, BN_MONT_CTX_free>;
78 using ScopedCMAC_CTX = ScopedOpenSSLType<CMAC_CTX, CMAC_CTX_free>;
79 using ScopedDH = ScopedOpenSSLType<DH, DH_free>;
80 using ScopedECDSA_SIG = ScopedOpenSSLType<ECDSA_SIG, ECDSA_SIG_free>;
81 using ScopedEC_GROUP = ScopedOpenSSLType<EC_GROUP, EC_GROUP_free>;
82 using ScopedEC_KEY = ScopedOpenSSLType<EC_KEY, EC_KEY_free>;
83 using ScopedEC_POINT = ScopedOpenSSLType<EC_POINT, EC_POINT_free>;
84 using ScopedEVP_PKEY = ScopedOpenSSLType<EVP_PKEY, EVP_PKEY_free>;
85 using ScopedEVP_PKEY_CTX = ScopedOpenSSLType<EVP_PKEY_CTX, EVP_PKEY_CTX_free>;
86 using ScopedPKCS8_PRIV_KEY_INFO = ScopedOpenSSLType<PKCS8_PRIV_KEY_INFO,
87                                                     PKCS8_PRIV_KEY_INFO_free>;
88 using ScopedPKCS12 = ScopedOpenSSLType<PKCS12, PKCS12_free>;
89 using ScopedRSA = ScopedOpenSSLType<RSA, RSA_free>;
90 using ScopedX509 = ScopedOpenSSLType<X509, X509_free>;
91 using ScopedX509_ALGOR = ScopedOpenSSLType<X509_ALGOR, X509_ALGOR_free>;
92 using ScopedX509_SIG = ScopedOpenSSLType<X509_SIG, X509_SIG_free>;
93 using ScopedX509_STORE_CTX = ScopedOpenSSLType<X509_STORE_CTX, X509_STORE_CTX_free>;
94
95 using ScopedX509Stack = ScopedOpenSSLStack<STACK_OF(X509), X509, X509_free>;
96
97 using ScopedOpenSSLBytes = std::unique_ptr<uint8_t, OpenSSLFree<uint8_t>>;
98 using ScopedOpenSSLString = std::unique_ptr<char, OpenSSLFree<char>>;
99
100 using ScopedFILE = std::unique_ptr<FILE, FileCloser>;
101
102 #endif  // OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H