dfc55cc322ec7686fb76440d63db21ac10bf0be2
[folly.git] / folly / io / async / ssl / OpenSSLPtrTypes.h
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <glog/logging.h>
20
21 #include <openssl/asn1.h>
22 #include <openssl/bio.h>
23 #include <openssl/bn.h>
24 #ifndef OPENSSL_NO_EC
25 #include <openssl/ec.h>
26 #include <openssl/ecdsa.h>
27 #endif
28 #include <openssl/evp.h>
29 #include <openssl/hmac.h>
30 #include <openssl/rsa.h>
31 #include <openssl/ssl.h>
32 #include <openssl/x509.h>
33
34 #include <folly/Memory.h>
35 #include <folly/portability/OpenSSL.h>
36
37 namespace folly {
38 namespace ssl {
39
40 // ASN1
41 using ASN1TimeDeleter =
42     folly::static_function_deleter<ASN1_TIME, &ASN1_TIME_free>;
43 using ASN1TimeUniquePtr = std::unique_ptr<ASN1_TIME, ASN1TimeDeleter>;
44
45 // X509
46 using X509Deleter = folly::static_function_deleter<X509, &X509_free>;
47 using X509UniquePtr = std::unique_ptr<X509, X509Deleter>;
48 using X509StoreCtxDeleter =
49     folly::static_function_deleter<X509_STORE_CTX, &X509_STORE_CTX_free>;
50 using X509StoreCtxUniquePtr =
51     std::unique_ptr<X509_STORE_CTX, X509StoreCtxDeleter>;
52 using X509VerifyParamDeleter =
53     folly::static_function_deleter<X509_VERIFY_PARAM, &X509_VERIFY_PARAM_free>;
54 using X509VerifyParam =
55     std::unique_ptr<X509_VERIFY_PARAM, X509VerifyParamDeleter>;
56
57 // EVP
58 using EvpPkeyDel = folly::static_function_deleter<EVP_PKEY, &EVP_PKEY_free>;
59 using EvpPkeyUniquePtr = std::unique_ptr<EVP_PKEY, EvpPkeyDel>;
60 using EvpPkeySharedPtr = std::shared_ptr<EVP_PKEY>;
61
62 // No EVP_PKEY_CTX <= 0.9.8b
63 #if OPENSSL_VERSION_NUMBER >= 0x10000002L
64 using EvpPkeyCtxDeleter =
65     folly::static_function_deleter<EVP_PKEY_CTX, &EVP_PKEY_CTX_free>;
66 using EvpPkeyCtxUniquePtr = std::unique_ptr<EVP_PKEY_CTX, EvpPkeyCtxDeleter>;
67 #else
68 struct EVP_PKEY_CTX;
69 #endif
70
71 using EvpMdCtxDeleter =
72     folly::static_function_deleter<EVP_MD_CTX, &EVP_MD_CTX_free>;
73 using EvpMdCtxUniquePtr = std::unique_ptr<EVP_MD_CTX, EvpMdCtxDeleter>;
74
75 // HMAC
76 using HmacCtxDeleter = folly::static_function_deleter<HMAC_CTX, &HMAC_CTX_free>;
77 using HmacCtxUniquePtr = std::unique_ptr<HMAC_CTX, HmacCtxDeleter>;
78
79 // BIO
80 using BioMethodDeleter =
81     folly::static_function_deleter<BIO_METHOD, &BIO_meth_free>;
82 using BioMethodUniquePtr = std::unique_ptr<BIO_METHOD, BioMethodDeleter>;
83 using BioDeleter = folly::static_function_deleter<BIO, &BIO_vfree>;
84 using BioUniquePtr = std::unique_ptr<BIO, BioDeleter>;
85 using BioChainDeleter = folly::static_function_deleter<BIO, &BIO_free_all>;
86 using BioChainUniquePtr = std::unique_ptr<BIO, BioChainDeleter>;
87 inline void BIO_free_fb(BIO* bio) { CHECK_EQ(1, BIO_free(bio)); }
88 using BioDeleterFb = folly::static_function_deleter<BIO, &BIO_free_fb>;
89 using BioUniquePtrFb = std::unique_ptr<BIO, BioDeleterFb>;
90
91 // RSA and EC
92 using RsaDeleter = folly::static_function_deleter<RSA, &RSA_free>;
93 using RsaUniquePtr = std::unique_ptr<RSA, RsaDeleter>;
94 #ifndef OPENSSL_NO_EC
95 using EcKeyDeleter = folly::static_function_deleter<EC_KEY, &EC_KEY_free>;
96 using EcKeyUniquePtr = std::unique_ptr<EC_KEY, EcKeyDeleter>;
97 using EcGroupDeleter = folly::static_function_deleter<EC_GROUP, &EC_GROUP_free>;
98 using EcGroupUniquePtr = std::unique_ptr<EC_GROUP, EcGroupDeleter>;
99 using EcPointDeleter = folly::static_function_deleter<EC_POINT, &EC_POINT_free>;
100 using EcPointUniquePtr = std::unique_ptr<EC_POINT, EcPointDeleter>;
101 using EcdsaSignDeleter =
102     folly::static_function_deleter<ECDSA_SIG, &ECDSA_SIG_free>;
103 using EcdsaSigUniquePtr = std::unique_ptr<ECDSA_SIG, EcdsaSignDeleter>;
104 #endif
105
106 // BIGNUMs
107 using BIGNUMDeleter = folly::static_function_deleter<BIGNUM, &BN_clear_free>;
108 using BIGNUMUniquePtr = std::unique_ptr<BIGNUM, BIGNUMDeleter>;
109
110 // SSL and SSL_CTX
111 using SSLDeleter = folly::static_function_deleter<SSL, &SSL_free>;
112 using SSLUniquePtr = std::unique_ptr<SSL, SSLDeleter>;
113 }
114 }