Support building with OpenSSL 1.1.0 and BoringSSL
[folly.git] / folly / portability / OpenSSL.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 //
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
23 // in BoringSSL
24 //
25
26 #pragma once
27
28 // This must come before the OpenSSL includes.
29 #include <folly/portability/Windows.h>
30
31 #include <openssl/dh.h>
32 #include <openssl/evp.h>
33 #include <openssl/ssl.h>
34 #include <openssl/x509.h>
35 #include <cstdint>
36
37 namespace folly {
38 namespace ssl {
39
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)
55
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
61 #else
62 #define FOLLY_OPENSSL_HAS_ALPN 0
63 #endif
64
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
70 #else
71 #define FOLLY_OPENSSL_HAS_SNI 0
72 #endif
73
74 #if FOLLY_OPENSSL_IS_110
75 ////////////////////////////////////////////////////////////////////////////////
76 // APIs needed in 1.1.0 only
77 ////////////////////////////////////////////////////////////////////////////////
78
79 #else
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);
88
89 EVP_MD_CTX* EVP_MD_CTX_new(void);
90 void EVP_MD_CTX_free(EVP_MD_CTX* ctx);
91
92 HMAC_CTX* HMAC_CTX_new(void);
93 void HMAC_CTX_free(HMAC_CTX* ctx);
94
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);
98
99 #ifdef OPENSSL_IS_BORINGSSL
100 ////////////////////////////////////////////////////////////////////////////////
101 // APIs needed in BoringSSL only
102 ////////////////////////////////////////////////////////////////////////////////
103
104 int SSL_CTX_set1_sigalgs_list(SSL_CTX* ctx, const char* sigalgs_list);
105 int TLS1_get_client_version(SSL* s);
106
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 ////////////////////////////////////////////////////////////////////////////////
111
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);
115
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);
121
122 #endif
123
124 #if FOLLY_OPENSSL_IS_100
125 ////////////////////////////////////////////////////////////////////////////////
126 // APIs needed only in 1.0.0 only (deprecated)
127 ////////////////////////////////////////////////////////////////////////////////
128
129 uint32_t SSL_CIPHER_get_id(const SSL_CIPHER*);
130 int TLS1_get_client_version(const SSL*);
131
132 #endif
133 #else
134 #warning Compiling with unsupported OpenSSL version
135
136 #endif // !(OPENSSL_IS_BORINGSSL || FOLLY_OPENSSL_IS_101 ||
137 // FOLLY_OPENSSL_IS_102 || FOLLY_OPENSSL_IS_100)
138
139 #endif // !FOLLY_OPENSSL_IS_110
140
141 } // ssl
142 } // folly