ee0bf13f6466c24fb8fb1648184e505d76d1f471
[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 // This needs to be before any OpenSSL includes.
22 #include <folly/portability/OpenSSL.h>
23
24 #include <openssl/asn1.h>
25 #include <openssl/bio.h>
26 #include <openssl/bn.h>
27 #ifndef OPENSSL_NO_EC
28 #include <openssl/ec.h>
29 #include <openssl/ecdsa.h>
30 #endif
31 #include <openssl/evp.h>
32 #include <openssl/hmac.h>
33 #include <openssl/rsa.h>
34 #include <openssl/ssl.h>
35 #include <openssl/x509.h>
36
37 #include <folly/Memory.h>
38
39 namespace folly {
40 namespace ssl {
41
42 // ASN1
43 using ASN1TimeDeleter =
44     folly::static_function_deleter<ASN1_TIME, &ASN1_TIME_free>;
45 using ASN1TimeUniquePtr = std::unique_ptr<ASN1_TIME, ASN1TimeDeleter>;
46
47 // X509
48 using X509Deleter = folly::static_function_deleter<X509, &X509_free>;
49 using X509UniquePtr = std::unique_ptr<X509, X509Deleter>;
50 using X509StoreCtxDeleter =
51     folly::static_function_deleter<X509_STORE_CTX, &X509_STORE_CTX_free>;
52 using X509StoreCtxUniquePtr =
53     std::unique_ptr<X509_STORE_CTX, X509StoreCtxDeleter>;
54 using X509VerifyParamDeleter =
55     folly::static_function_deleter<X509_VERIFY_PARAM, &X509_VERIFY_PARAM_free>;
56 using X509VerifyParam =
57     std::unique_ptr<X509_VERIFY_PARAM, X509VerifyParamDeleter>;
58
59 // EVP
60 using EvpPkeyDel = folly::static_function_deleter<EVP_PKEY, &EVP_PKEY_free>;
61 using EvpPkeyUniquePtr = std::unique_ptr<EVP_PKEY, EvpPkeyDel>;
62 using EvpPkeySharedPtr = std::shared_ptr<EVP_PKEY>;
63
64 // No EVP_PKEY_CTX <= 0.9.8b
65 #if OPENSSL_VERSION_NUMBER >= 0x10000002L
66 using EvpPkeyCtxDeleter =
67     folly::static_function_deleter<EVP_PKEY_CTX, &EVP_PKEY_CTX_free>;
68 using EvpPkeyCtxUniquePtr = std::unique_ptr<EVP_PKEY_CTX, EvpPkeyCtxDeleter>;
69 #else
70 struct EVP_PKEY_CTX;
71 #endif
72
73 using EvpMdCtxDeleter =
74     folly::static_function_deleter<EVP_MD_CTX, &EVP_MD_CTX_free>;
75 using EvpMdCtxUniquePtr = std::unique_ptr<EVP_MD_CTX, EvpMdCtxDeleter>;
76
77 // HMAC
78 using HmacCtxDeleter = folly::static_function_deleter<HMAC_CTX, &HMAC_CTX_free>;
79 using HmacCtxUniquePtr = std::unique_ptr<HMAC_CTX, HmacCtxDeleter>;
80
81 // BIO
82 using BioMethodDeleter =
83     folly::static_function_deleter<BIO_METHOD, &BIO_meth_free>;
84 using BioMethodUniquePtr = std::unique_ptr<BIO_METHOD, BioMethodDeleter>;
85 using BioDeleter = folly::static_function_deleter<BIO, &BIO_vfree>;
86 using BioUniquePtr = std::unique_ptr<BIO, BioDeleter>;
87 using BioChainDeleter = folly::static_function_deleter<BIO, &BIO_free_all>;
88 using BioChainUniquePtr = std::unique_ptr<BIO, BioChainDeleter>;
89 inline void BIO_free_fb(BIO* bio) { CHECK_EQ(1, BIO_free(bio)); }
90 using BioDeleterFb = folly::static_function_deleter<BIO, &BIO_free_fb>;
91 using BioUniquePtrFb = std::unique_ptr<BIO, BioDeleterFb>;
92
93 // RSA and EC
94 using RsaDeleter = folly::static_function_deleter<RSA, &RSA_free>;
95 using RsaUniquePtr = std::unique_ptr<RSA, RsaDeleter>;
96 #ifndef OPENSSL_NO_EC
97 using EcKeyDeleter = folly::static_function_deleter<EC_KEY, &EC_KEY_free>;
98 using EcKeyUniquePtr = std::unique_ptr<EC_KEY, EcKeyDeleter>;
99 using EcGroupDeleter = folly::static_function_deleter<EC_GROUP, &EC_GROUP_free>;
100 using EcGroupUniquePtr = std::unique_ptr<EC_GROUP, EcGroupDeleter>;
101 using EcPointDeleter = folly::static_function_deleter<EC_POINT, &EC_POINT_free>;
102 using EcPointUniquePtr = std::unique_ptr<EC_POINT, EcPointDeleter>;
103 using EcdsaSignDeleter =
104     folly::static_function_deleter<ECDSA_SIG, &ECDSA_SIG_free>;
105 using EcdsaSigUniquePtr = std::unique_ptr<ECDSA_SIG, EcdsaSignDeleter>;
106 #endif
107
108 // BIGNUMs
109 using BIGNUMDeleter = folly::static_function_deleter<BIGNUM, &BN_clear_free>;
110 using BIGNUMUniquePtr = std::unique_ptr<BIGNUM, BIGNUMDeleter>;
111
112 // SSL and SSL_CTX
113 using SSLDeleter = folly::static_function_deleter<SSL, &SSL_free>;
114 using SSLUniquePtr = std::unique_ptr<SSL, SSLDeleter>;
115 }
116 }