2 * Copyright 2016-present 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.
21 // This must come before the OpenSSL includes.
22 #include <folly/portability/Windows.h>
24 #include <folly/Portability.h>
26 #include <openssl/opensslv.h>
28 #include <openssl/asn1.h>
29 #include <openssl/bio.h>
30 #include <openssl/crypto.h>
31 #include <openssl/dh.h>
32 #include <openssl/err.h>
33 #include <openssl/evp.h>
34 #include <openssl/hmac.h>
35 #include <openssl/rand.h>
36 #include <openssl/rsa.h>
37 #include <openssl/sha.h>
38 #include <openssl/ssl.h>
39 #include <openssl/tls1.h>
40 #include <openssl/x509.h>
41 #include <openssl/x509v3.h>
44 #include <openssl/ec.h>
45 #include <openssl/ecdsa.h>
48 // BoringSSL doesn't have notion of versioning although it defines
49 // OPENSSL_VERSION_NUMBER to maintain compatibility. The following variables are
50 // intended to be specific to OpenSSL.
51 #if !defined(OPENSSL_IS_BORINGSSL)
52 #define FOLLY_OPENSSL_IS_100 \
53 (OPENSSL_VERSION_NUMBER >= 0x10000003L && \
54 OPENSSL_VERSION_NUMBER < 0x1000105fL)
55 #define FOLLY_OPENSSL_IS_101 \
56 (OPENSSL_VERSION_NUMBER >= 0x1000105fL && \
57 OPENSSL_VERSION_NUMBER < 0x1000200fL)
58 #define FOLLY_OPENSSL_IS_102 \
59 (OPENSSL_VERSION_NUMBER >= 0x1000200fL && \
60 OPENSSL_VERSION_NUMBER < 0x10100000L)
61 #define FOLLY_OPENSSL_IS_110 (OPENSSL_VERSION_NUMBER >= 0x10100000L)
64 #if !OPENSSL_IS_BORINGSSL && !FOLLY_OPENSSL_IS_100 && !FOLLY_OPENSSL_IS_101 && \
65 !FOLLY_OPENSSL_IS_102 && !FOLLY_OPENSSL_IS_110
66 #warning Compiling with unsupported OpenSSL version
69 // BoringSSL and OpenSSL 0.9.8f later with TLS extension support SNI.
70 #if OPENSSL_IS_BORINGSSL || \
71 (OPENSSL_VERSION_NUMBER >= 0x00908070L && !defined(OPENSSL_NO_TLSEXT))
72 #define FOLLY_OPENSSL_HAS_SNI 1
74 #define FOLLY_OPENSSL_HAS_SNI 0
77 // BoringSSL and OpenSSL 1.0.2 later with TLS extension support ALPN.
78 #if OPENSSL_IS_BORINGSSL || \
79 (OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined(OPENSSL_NO_TLSEXT))
80 #define FOLLY_OPENSSL_HAS_ALPN 1
82 #define FOLLY_OPENSSL_HAS_ALPN 0
85 // This attempts to "unify" the OpenSSL libcrypto/libssl APIs between
86 // OpenSSL 1.0.2, 1.1.0 (and some earlier versions) and BoringSSL. The general
87 // idea is to provide namespaced wrapper methods for versions which do not
88 // which already exist in BoringSSL and 1.1.0, but there are few APIs such as
89 // SSL_CTX_set1_sigalgs_list and so on which exist in 1.0.2 but were removed
92 namespace portability {
95 #if OPENSSL_IS_BORINGSSL
96 int SSL_CTX_set1_sigalgs_list(SSL_CTX* ctx, const char* sigalgs_list);
97 int TLS1_get_client_version(SSL* s);
100 #if FOLLY_OPENSSL_IS_100
101 uint32_t SSL_CIPHER_get_id(const SSL_CIPHER*);
102 int TLS1_get_client_version(const SSL*);
105 #if FOLLY_OPENSSL_IS_100 || FOLLY_OPENSSL_IS_101
106 int X509_get_signature_nid(X509* cert);
109 #if FOLLY_OPENSSL_IS_100 || FOLLY_OPENSSL_IS_101 || FOLLY_OPENSSL_IS_102
110 int SSL_CTX_up_ref(SSL_CTX* session);
111 int SSL_SESSION_up_ref(SSL_SESSION* session);
112 int X509_up_ref(X509* x);
113 int EVP_PKEY_up_ref(EVP_PKEY* evp);
119 RSA* EVP_PKEY_get0_RSA(EVP_PKEY* pkey);
120 DSA* EVP_PKEY_get0_DSA(EVP_PKEY* pkey);
121 DH* EVP_PKEY_get0_DH(EVP_PKEY* pkey);
122 EC_KEY* EVP_PKEY_get0_EC_KEY(EVP_PKEY* pkey);
125 #if !FOLLY_OPENSSL_IS_110
126 void BIO_meth_free(BIO_METHOD* biom);
127 int BIO_meth_set_read(BIO_METHOD* biom, int (*read)(BIO*, char*, int));
128 int BIO_meth_set_write(BIO_METHOD* biom, int (*write)(BIO*, const char*, int));
130 const char* SSL_SESSION_get0_hostname(const SSL_SESSION* s);
131 unsigned char* ASN1_STRING_get0_data(const ASN1_STRING* x);
133 EVP_MD_CTX* EVP_MD_CTX_new();
134 void EVP_MD_CTX_free(EVP_MD_CTX* ctx);
136 HMAC_CTX* HMAC_CTX_new();
137 void HMAC_CTX_free(HMAC_CTX* ctx);
139 unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION* s);
140 int SSL_SESSION_has_ticket(const SSL_SESSION* s);
141 int DH_set0_pqg(DH* dh, BIGNUM* p, BIGNUM* q, BIGNUM* g);
147 void DH_get0_key(const DH* dh, const BIGNUM** pub_key, const BIGNUM** priv_key);
156 const BIGNUM** pub_key,
157 const BIGNUM** priv_key);
159 X509* X509_STORE_CTX_get0_cert(X509_STORE_CTX* ctx);
160 STACK_OF(X509) * X509_STORE_CTX_get0_chain(X509_STORE_CTX* ctx);
161 STACK_OF(X509) * X509_STORE_CTX_get0_untrusted(X509_STORE_CTX* ctx);
162 bool RSA_set0_key(RSA* r, BIGNUM* n, BIGNUM* e, BIGNUM* d);
163 void RSA_get0_factors(const RSA* r, const BIGNUM** p, const BIGNUM** q);
164 void RSA_get0_crt_params(
168 const BIGNUM** iqmp);
169 int ECDSA_SIG_set0(ECDSA_SIG* sig, BIGNUM* r, BIGNUM* s);
170 void ECDSA_SIG_get0(const ECDSA_SIG* sig, const BIGNUM** pr, const BIGNUM** ps);
172 using OPENSSL_INIT_SETTINGS = void;
173 int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS* settings);
174 void OPENSSL_cleanup();
176 const ASN1_INTEGER* X509_REVOKED_get0_serialNumber(const X509_REVOKED* r);
177 const ASN1_TIME* X509_REVOKED_get0_revocationDate(const X509_REVOKED* r);
181 #if FOLLY_OPENSSL_IS_110
182 // Note: this was a type and has been fixed upstream, so the next 1.1.0
183 // minor version upgrade will need to remove this
184 #define OPENSSL_lh_new OPENSSL_LH_new
186 // OpenSSL v1.1.0 removed support for SSLv2, and also removed the define that
187 // indicates it isn't supported.
188 #define OPENSSL_NO_SSL2
191 } // namespace portability
195 #if __CLANG_PREREQ(3, 0)
196 FOLLY_GCC_DISABLE_WARNING("-Wheader-hygiene")
198 /* using override */ using namespace folly::portability::ssl;