7948c62af2ac0e07bab68b811cb504b863b26255
[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 using X509VerifyParamDeleter =
41     folly::static_function_deleter<X509_VERIFY_PARAM, &X509_VERIFY_PARAM_free>;
42 using X509VerifyParam =
43     std::unique_ptr<X509_VERIFY_PARAM, X509VerifyParamDeleter>;
44
45 // EVP
46 using EvpPkeyDel = folly::static_function_deleter<EVP_PKEY, &EVP_PKEY_free>;
47 using EvpPkeyUniquePtr = std::unique_ptr<EVP_PKEY, EvpPkeyDel>;
48 using EvpPkeySharedPtr = std::shared_ptr<EVP_PKEY>;
49
50 // No EVP_PKEY_CTX <= 0.9.8b
51 #if OPENSSL_VERSION_NUMBER >= 0x10000002L
52 using EvpPkeyCtxDeleter =
53     folly::static_function_deleter<EVP_PKEY_CTX, &EVP_PKEY_CTX_free>;
54 using EvpPkeyCtxUniquePtr = std::unique_ptr<EVP_PKEY_CTX, EvpPkeyCtxDeleter>;
55 #else
56 struct EVP_PKEY_CTX;
57 #endif
58 using EvpMdCtxDeleter =
59     folly::static_function_deleter<EVP_MD_CTX, &EVP_MD_CTX_destroy>;
60 using EvpMdCtxUniquePtr = std::unique_ptr<EVP_MD_CTX, EvpMdCtxDeleter>;
61
62 // BIO
63 using BioDeleter = folly::static_function_deleter<BIO, &BIO_vfree>;
64 using BioUniquePtr = std::unique_ptr<BIO, BioDeleter>;
65 using BioChainDeleter = folly::static_function_deleter<BIO, &BIO_free_all>;
66 using BioChainUniquePtr = std::unique_ptr<BIO, BioChainDeleter>;
67 inline void BIO_free_fb(BIO* bio) { CHECK_EQ(1, BIO_free(bio)); }
68 using BioDeleterFb = folly::static_function_deleter<BIO, &BIO_free_fb>;
69 using BioUniquePtrFb = std::unique_ptr<BIO, BioDeleterFb>;
70
71 // RSA and EC
72 using RsaDeleter = folly::static_function_deleter<RSA, &RSA_free>;
73 using RsaUniquePtr = std::unique_ptr<RSA, RsaDeleter>;
74 #ifndef OPENSSL_NO_EC
75 using EcKeyDeleter = folly::static_function_deleter<EC_KEY, &EC_KEY_free>;
76 using EcKeyUniquePtr = std::unique_ptr<EC_KEY, EcKeyDeleter>;
77 #endif
78
79 // SSL and SSL_CTX
80 using SSLDeleter = folly::static_function_deleter<SSL, &SSL_free>;
81 using SSLUniquePtr = std::unique_ptr<SSL, SSLDeleter>;
82 }
83 }