2000db2f23ba3d2a0e36fa4d51efcf72622edc32
[folly.git] / folly / io / async / ssl / OpenSSLPtrTypes.h
1 /*
2  * Copyright 2016 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 #include <openssl/evp.h>
21 #include <openssl/rsa.h>
22 #include <openssl/ssl.h>
23 #ifndef OPENSSL_NO_EC
24 #include <openssl/ec.h>
25 #endif
26 #include <folly/Memory.h>
27 #include <openssl/bio.h>
28 #include <openssl/x509.h>
29
30 namespace folly {
31 namespace ssl {
32
33 // X509
34 using X509Deleter = folly::static_function_deleter<X509, &X509_free>;
35 using X509UniquePtr = std::unique_ptr<X509, X509Deleter>;
36 using X509StoreCtxDeleter =
37     folly::static_function_deleter<X509_STORE_CTX, &X509_STORE_CTX_free>;
38 using X509StoreCtxUniquePtr =
39     std::unique_ptr<X509_STORE_CTX, X509StoreCtxDeleter>;
40
41 // EVP
42 using EvpPkeyDel = folly::static_function_deleter<EVP_PKEY, &EVP_PKEY_free>;
43 using EvpPkeyUniquePtr = std::unique_ptr<EVP_PKEY, EvpPkeyDel>;
44 using EvpPkeySharedPtr = std::shared_ptr<EVP_PKEY>;
45
46 // No EVP_PKEY_CTX <= 0.9.8b
47 #if OPENSSL_VERSION_NUMBER >= 0x10000002L
48 using EvpPkeyCtxDeleter =
49     folly::static_function_deleter<EVP_PKEY_CTX, &EVP_PKEY_CTX_free>;
50 using EvpPkeyCtxUniquePtr = std::unique_ptr<EVP_PKEY_CTX, EvpPkeyCtxDeleter>;
51 #else
52 struct EVP_PKEY_CTX;
53 #endif
54 using EvpMdCtxDeleter =
55     folly::static_function_deleter<EVP_MD_CTX, &EVP_MD_CTX_destroy>;
56 using EvpMdCtxUniquePtr = std::unique_ptr<EVP_MD_CTX, EvpMdCtxDeleter>;
57
58 // BIO
59 using BioDeleter = folly::static_function_deleter<BIO, &BIO_vfree>;
60 using BioUniquePtr = std::unique_ptr<BIO, BioDeleter>;
61 using BioChainDeleter = folly::static_function_deleter<BIO, &BIO_free_all>;
62 using BioChainUniquePtr = std::unique_ptr<BIO, BioChainDeleter>;
63 inline void BIO_free_fb(BIO* bio) { CHECK_EQ(1, BIO_free(bio)); }
64 using BioDeleterFb = folly::static_function_deleter<BIO, &BIO_free_fb>;
65 using BioUniquePtrFb = std::unique_ptr<BIO, BioDeleterFb>;
66
67 // RSA and EC
68 using RsaDeleter = folly::static_function_deleter<RSA, &RSA_free>;
69 using RsaUniquePtr = std::unique_ptr<RSA, RsaDeleter>;
70 #ifndef OPENSSL_NO_EC
71 using EcKeyDeleter = folly::static_function_deleter<EC_KEY, &EC_KEY_free>;
72 using EcKeyUniquePtr = std::unique_ptr<EC_KEY, EcKeyDeleter>;
73 #endif
74
75 // SSL and SSL_CTX
76 using SSLDeleter = folly::static_function_deleter<SSL, &SSL_free>;
77 using SSLUniquePtr = std::unique_ptr<SSL, SSLDeleter>;
78 }
79 }