238dd9beab5ed346d6391c45947dec36b48db533
[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
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 #endif
27 #include <openssl/evp.h>
28 #include <openssl/rsa.h>
29 #include <openssl/ssl.h>
30 #include <openssl/x509.h>
31
32 #include <folly/Memory.h>
33
34 namespace folly {
35 namespace ssl {
36
37 // ASN1
38 using ASN1TimeDeleter =
39     folly::static_function_deleter<ASN1_TIME, &ASN1_TIME_free>;
40 using ASN1TimeUniquePtr = std::unique_ptr<ASN1_TIME, ASN1TimeDeleter>;
41
42 // X509
43 using X509Deleter = folly::static_function_deleter<X509, &X509_free>;
44 using X509UniquePtr = std::unique_ptr<X509, X509Deleter>;
45 using X509StoreCtxDeleter =
46     folly::static_function_deleter<X509_STORE_CTX, &X509_STORE_CTX_free>;
47 using X509StoreCtxUniquePtr =
48     std::unique_ptr<X509_STORE_CTX, X509StoreCtxDeleter>;
49 using X509VerifyParamDeleter =
50     folly::static_function_deleter<X509_VERIFY_PARAM, &X509_VERIFY_PARAM_free>;
51 using X509VerifyParam =
52     std::unique_ptr<X509_VERIFY_PARAM, X509VerifyParamDeleter>;
53
54 // EVP
55 using EvpPkeyDel = folly::static_function_deleter<EVP_PKEY, &EVP_PKEY_free>;
56 using EvpPkeyUniquePtr = std::unique_ptr<EVP_PKEY, EvpPkeyDel>;
57 using EvpPkeySharedPtr = std::shared_ptr<EVP_PKEY>;
58
59 // No EVP_PKEY_CTX <= 0.9.8b
60 #if OPENSSL_VERSION_NUMBER >= 0x10000002L
61 using EvpPkeyCtxDeleter =
62     folly::static_function_deleter<EVP_PKEY_CTX, &EVP_PKEY_CTX_free>;
63 using EvpPkeyCtxUniquePtr = std::unique_ptr<EVP_PKEY_CTX, EvpPkeyCtxDeleter>;
64 #else
65 struct EVP_PKEY_CTX;
66 #endif
67 using EvpMdCtxDeleter =
68     folly::static_function_deleter<EVP_MD_CTX, &EVP_MD_CTX_destroy>;
69 using EvpMdCtxUniquePtr = std::unique_ptr<EVP_MD_CTX, EvpMdCtxDeleter>;
70
71 // BIO
72 using BioDeleter = folly::static_function_deleter<BIO, &BIO_vfree>;
73 using BioUniquePtr = std::unique_ptr<BIO, BioDeleter>;
74 using BioChainDeleter = folly::static_function_deleter<BIO, &BIO_free_all>;
75 using BioChainUniquePtr = std::unique_ptr<BIO, BioChainDeleter>;
76 inline void BIO_free_fb(BIO* bio) { CHECK_EQ(1, BIO_free(bio)); }
77 using BioDeleterFb = folly::static_function_deleter<BIO, &BIO_free_fb>;
78 using BioUniquePtrFb = std::unique_ptr<BIO, BioDeleterFb>;
79
80 // RSA and EC
81 using RsaDeleter = folly::static_function_deleter<RSA, &RSA_free>;
82 using RsaUniquePtr = std::unique_ptr<RSA, RsaDeleter>;
83 #ifndef OPENSSL_NO_EC
84 using EcKeyDeleter = folly::static_function_deleter<EC_KEY, &EC_KEY_free>;
85 using EcKeyUniquePtr = std::unique_ptr<EC_KEY, EcKeyDeleter>;
86 using EcGroupDeleter = folly::static_function_deleter<EC_GROUP, &EC_GROUP_free>;
87 using EcGroupUniquePtr = std::unique_ptr<EC_GROUP, EcGroupDeleter>;
88 using EcPointDeleter = folly::static_function_deleter<EC_POINT, &EC_POINT_free>;
89 using EcPointUniquePtr = std::unique_ptr<EC_POINT, EcPointDeleter>;
90 #endif
91
92 // BIGNUMs
93 using BIGNUMDeleter = folly::static_function_deleter<BIGNUM, &BN_clear_free>;
94 using BIGNUMUniquePtr = std::unique_ptr<BIGNUM, BIGNUMDeleter>;
95
96 // SSL and SSL_CTX
97 using SSLDeleter = folly::static_function_deleter<SSL, &SSL_free>;
98 using SSLUniquePtr = std::unique_ptr<SSL, SSLDeleter>;
99 }
100 }