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