1/*
2 * Copyright 2016-present 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 <folly/Memory.h>
22#include <folly/portability/OpenSSL.h>
23
24namespace folly {
25namespace ssl {
26
27// helper which translates (DEFINE_SSL_PTR_TYPE(Foo, FOO, FOO_free); into
28// using FooDeleter = folly::static_function_deleter<FOO, &FOO_free>;
29// using FooUniquePtr = std::unique_ptr<FOO, FooDeleter>;
30#define DEFINE_SSL_PTR_TYPE(alias, object, deleter) \
31 using alias##Deleter = folly::static_function_deleter<object, &deleter>; \
32 using alias##UniquePtr = std::unique_ptr<object, alias##Deleter>
33
34// ASN1
35DEFINE_SSL_PTR_TYPE(ASN1Time, ASN1_TIME, ASN1_TIME_free);
36DEFINE_SSL_PTR_TYPE(ASN1Ia5Str, ASN1_IA5STRING, ASN1_IA5STRING_free);
37DEFINE_SSL_PTR_TYPE(ASN1Int, ASN1_INTEGER, ASN1_INTEGER_free);
38DEFINE_SSL_PTR_TYPE(ASN1Obj, ASN1_OBJECT, ASN1_OBJECT_free);
39DEFINE_SSL_PTR_TYPE(ASN1Str, ASN1_STRING, ASN1_STRING_free);
40DEFINE_SSL_PTR_TYPE(ASN1Type, ASN1_TYPE, ASN1_TYPE_free);
41DEFINE_SSL_PTR_TYPE(ASN1UTF8Str, ASN1_UTF8STRING, ASN1_UTF8STRING_free);
42
43// X509
44DEFINE_SSL_PTR_TYPE(X509, X509, X509_free);
45DEFINE_SSL_PTR_TYPE(X509Extension, X509_EXTENSION, X509_EXTENSION_free);
46DEFINE_SSL_PTR_TYPE(X509Store, X509_STORE, X509_STORE_free);
47DEFINE_SSL_PTR_TYPE(X509StoreCtx, X509_STORE_CTX, X509_STORE_CTX_free);
48using X509VerifyParamDeleter =
49 folly::static_function_deleter<X509_VERIFY_PARAM, &X509_VERIFY_PARAM_free>;
50using X509VerifyParam =
51 std::unique_ptr<X509_VERIFY_PARAM, X509VerifyParamDeleter>;
52
53DEFINE_SSL_PTR_TYPE(GeneralName, GENERAL_NAME, GENERAL_NAME_free);
54DEFINE_SSL_PTR_TYPE(GeneralNames, GENERAL_NAMES, GENERAL_NAMES_free);
55DEFINE_SSL_PTR_TYPE(
56 AccessDescription,
57 ACCESS_DESCRIPTION,
58 ACCESS_DESCRIPTION_free);
59DEFINE_SSL_PTR_TYPE(
60 AuthorityInfoAccess,
61 AUTHORITY_INFO_ACCESS,
62 AUTHORITY_INFO_ACCESS_free);
63DEFINE_SSL_PTR_TYPE(DistPointName, DIST_POINT_NAME, DIST_POINT_NAME_free);
64DEFINE_SSL_PTR_TYPE(DistPoint, DIST_POINT, DIST_POINT_free);
65DEFINE_SSL_PTR_TYPE(CrlDistPoints, CRL_DIST_POINTS, CRL_DIST_POINTS_free);
66DEFINE_SSL_PTR_TYPE(X509Crl, X509_CRL, X509_CRL_free);
67DEFINE_SSL_PTR_TYPE(X509Name, X509_NAME, X509_NAME_free);
68DEFINE_SSL_PTR_TYPE(X509Req, X509_REQ, X509_REQ_free);
69DEFINE_SSL_PTR_TYPE(X509Revoked, X509_REVOKED, X509_REVOKED_free);
70
71// EVP
72DEFINE_SSL_PTR_TYPE(EvpPkey, EVP_PKEY, EVP_PKEY_free);
73using EvpPkeySharedPtr = std::shared_ptr<EVP_PKEY>;
74
75// No EVP_PKEY_CTX <= 0.9.8b
76#if OPENSSL_VERSION_NUMBER >= 0x10000002L
77DEFINE_SSL_PTR_TYPE(EvpPkeyCtx, EVP_PKEY_CTX, EVP_PKEY_CTX_free);
78#else
79struct EVP_PKEY_CTX;
80#endif
81
82DEFINE_SSL_PTR_TYPE(EvpMdCtx, EVP_MD_CTX, EVP_MD_CTX_free);
83DEFINE_SSL_PTR_TYPE(EvpCipherCtx, EVP_CIPHER_CTX, EVP_CIPHER_CTX_free);
84
85// HMAC
86DEFINE_SSL_PTR_TYPE(HmacCtx, HMAC_CTX, HMAC_CTX_free);
87
88// BIO
89DEFINE_SSL_PTR_TYPE(BioMethod, BIO_METHOD, BIO_meth_free);
90DEFINE_SSL_PTR_TYPE(Bio, BIO, BIO_vfree);
91DEFINE_SSL_PTR_TYPE(BioChain, BIO, BIO_free_all);
92inline void BIO_free_fb(BIO* bio) {
93 CHECK_EQ(1, BIO_free(bio));
94}
95using BioDeleterFb = folly::static_function_deleter<BIO, &BIO_free_fb>;
96using BioUniquePtrFb = std::unique_ptr<BIO, BioDeleterFb>;
97
98// RSA and EC
99DEFINE_SSL_PTR_TYPE(Rsa, RSA, RSA_free);
100#ifndef OPENSSL_NO_EC
101DEFINE_SSL_PTR_TYPE(EcKey, EC_KEY, EC_KEY_free);
102DEFINE_SSL_PTR_TYPE(EcGroup, EC_GROUP, EC_GROUP_free);
103DEFINE_SSL_PTR_TYPE(EcPoint, EC_POINT, EC_POINT_free);
104DEFINE_SSL_PTR_TYPE(EcdsaSig, ECDSA_SIG, ECDSA_SIG_free);
105#endif
106
107// BIGNUMs
108DEFINE_SSL_PTR_TYPE(BIGNUM, BIGNUM, BN_clear_free);
109DEFINE_SSL_PTR_TYPE(BNCtx, BN_CTX, BN_CTX_free);
110
111// SSL and SSL_CTX
112DEFINE_SSL_PTR_TYPE(SSL, SSL, SSL_free);
113DEFINE_SSL_PTR_TYPE(SSLSession, SSL_SESSION, SSL_SESSION_free);
114
115#undef DEFINE_SSL_PTR_TYPE
116} // namespace ssl
117} // namespace folly
118