2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 // This class attempts to "unify" the OpenSSL libcrypto/libssl APIs between
19 // OpenSSL 1.0.2, 1.1.0 (and some earlier versions) and BoringSSL. The general
20 // idea is to provide namespaced wrapper methods for versions which do not
21 // which already exist in BoringSSL and 1.1.0, but there are few APIs such as
22 // SSL_CTX_set1_sigalgs_list and so on which exist in 1.0.2 but were removed
28 // This must come before the OpenSSL includes.
29 #include <folly/portability/Windows.h>
31 #include <openssl/dh.h>
32 #include <openssl/evp.h>
33 #include <openssl/ssl.h>
34 #include <openssl/x509.h>
40 // BoringSSL doesn't have notion of versioning although it defines
41 // OPENSSL_VERSION_NUMBER to maintain compatibility. The following variables are
42 // intended to be specific to OpenSSL.
43 #if !defined(OPENSSL_IS_BORINGSSL)
44 #define FOLLY_OPENSSL_IS_100 \
45 (OPENSSL_VERSION_NUMBER >= 0x10000003L && \
46 OPENSSL_VERSION_NUMBER < 0x1000105fL)
47 #define FOLLY_OPENSSL_IS_101 \
48 (OPENSSL_VERSION_NUMBER >= 0x1000105fL && \
49 OPENSSL_VERSION_NUMBER < 0x1000200fL)
50 #define FOLLY_OPENSSL_IS_102 \
51 (OPENSSL_VERSION_NUMBER >= 0x1000200fL && \
52 OPENSSL_VERSION_NUMBER < 0x10100000L)
53 #define FOLLY_OPENSSL_IS_110 (OPENSSL_VERSION_NUMBER >= 0x10100000L)
54 #endif // !defined(OPENSSL_IS_BORINGSSL)
56 // BoringSSL and OpenSSL 1.0.2 later with TLS extension support ALPN.
57 #if defined(OPENSSL_IS_BORINGSSL) || \
58 (OPENSSL_VERSION_NUMBER >= 0x1000200fL && \
59 !defined(OPENSSL_NO_TLSEXT))
60 #define FOLLY_OPENSSL_HAS_ALPN 1
62 #define FOLLY_OPENSSL_HAS_ALPN 0
65 // BoringSSL and OpenSSL 0.9.8f later with TLS extension support SNI.
66 #if defined(OPENSSL_IS_BORINGSSL) || \
67 (OPENSSL_VERSION_NUMBER >= 0x00908070L && \
68 !defined(OPENSSL_NO_TLSEXT))
69 #define FOLLY_OPENSSL_HAS_SNI 1
71 #define FOLLY_OPENSSL_HAS_SNI 0
74 #if FOLLY_OPENSSL_IS_110
75 ////////////////////////////////////////////////////////////////////////////////
76 // APIs needed in 1.1.0 only
77 ////////////////////////////////////////////////////////////////////////////////
80 ////////////////////////////////////////////////////////////////////////////////
81 // APIs needed in BoringSSL and OpenSSL != 1.1.0 (1.0.2, 1.0.1, 1.0.0...)
82 ////////////////////////////////////////////////////////////////////////////////
83 void BIO_meth_free(BIO_METHOD* biom);
84 int BIO_meth_set_read(BIO_METHOD* biom, int (*read)(BIO*, char*, int));
85 int BIO_meth_set_write(BIO_METHOD* biom, int (*write)(BIO*, const char*, int));
86 void EVP_MD_CTX_free(EVP_MD_CTX* ctx);
87 const char* SSL_SESSION_get0_hostname(const SSL_SESSION* s);
89 EVP_MD_CTX* EVP_MD_CTX_new(void);
90 void EVP_MD_CTX_free(EVP_MD_CTX* ctx);
92 HMAC_CTX* HMAC_CTX_new(void);
93 void HMAC_CTX_free(HMAC_CTX* ctx);
95 unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION* s);
96 int SSL_SESSION_has_ticket(const SSL_SESSION*);
97 int DH_set0_pqg(DH* dh, BIGNUM* p, BIGNUM* q, BIGNUM* g);
99 #ifdef OPENSSL_IS_BORINGSSL
100 ////////////////////////////////////////////////////////////////////////////////
101 // APIs needed in BoringSSL only
102 ////////////////////////////////////////////////////////////////////////////////
104 int SSL_CTX_set1_sigalgs_list(SSL_CTX* ctx, const char* sigalgs_list);
105 int TLS1_get_client_version(SSL* s);
107 #elif FOLLY_OPENSSL_IS_102 || FOLLY_OPENSSL_IS_101 || FOLLY_OPENSSL_IS_100
108 ////////////////////////////////////////////////////////////////////////////////
109 // APIs needed in 1.0.2 and 1.0.1/1.0.0 (both deprecated)
110 ////////////////////////////////////////////////////////////////////////////////
112 int SSL_CTX_up_ref(SSL_CTX* session);
113 int SSL_SESSION_up_ref(SSL_SESSION* session);
114 int X509_up_ref(X509* x);
116 #if FOLLY_OPENSSL_IS_101 || FOLLY_OPENSSL_IS_100
117 ////////////////////////////////////////////////////////////////////////////////
118 // APIs needed in 1.0.1/1.0.0 (both deprecated)
119 ////////////////////////////////////////////////////////////////////////////////
120 int X509_get_signature_nid(X509* cert);
124 #if FOLLY_OPENSSL_IS_100
125 ////////////////////////////////////////////////////////////////////////////////
126 // APIs needed only in 1.0.0 only (deprecated)
127 ////////////////////////////////////////////////////////////////////////////////
129 uint32_t SSL_CIPHER_get_id(const SSL_CIPHER*);
130 int TLS1_get_client_version(const SSL*);
134 #warning Compiling with unsupported OpenSSL version
136 #endif // !(OPENSSL_IS_BORINGSSL || FOLLY_OPENSSL_IS_101 ||
137 // FOLLY_OPENSSL_IS_102 || FOLLY_OPENSSL_IS_100)
139 #endif // !FOLLY_OPENSSL_IS_110