a75b015bb55f9d2e6bcb03c2c6fc9ce77e963c2b
[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 #pragma once
18
19 // This must come before the OpenSSL includes.
20 #include <folly/portability/Windows.h>
21
22 #include <folly/Portability.h>
23
24 #include <openssl/opensslv.h>
25
26 #include <openssl/asn1.h>
27 #include <openssl/bio.h>
28 #include <openssl/crypto.h>
29 #include <openssl/dh.h>
30 #include <openssl/err.h>
31 #include <openssl/evp.h>
32 #include <openssl/hmac.h>
33 #include <openssl/rand.h>
34 #include <openssl/rsa.h>
35 #include <openssl/sha.h>
36 #include <openssl/ssl.h>
37 #include <openssl/tls1.h>
38 #include <openssl/x509.h>
39 #include <openssl/x509v3.h>
40
41 #ifndef OPENSSL_NO_EC
42 #include <openssl/ec.h>
43 #include <openssl/ecdsa.h>
44 #endif
45
46 // BoringSSL doesn't have notion of versioning although it defines
47 // OPENSSL_VERSION_NUMBER to maintain compatibility. The following variables are
48 // intended to be specific to OpenSSL.
49 #if !defined(OPENSSL_IS_BORINGSSL)
50 # define FOLLY_OPENSSL_IS_100                \
51   (OPENSSL_VERSION_NUMBER >= 0x10000003L && \
52    OPENSSL_VERSION_NUMBER < 0x1000105fL)
53 # define FOLLY_OPENSSL_IS_101                \
54   (OPENSSL_VERSION_NUMBER >= 0x1000105fL && \
55    OPENSSL_VERSION_NUMBER < 0x1000200fL)
56 # define FOLLY_OPENSSL_IS_102                \
57   (OPENSSL_VERSION_NUMBER >= 0x1000200fL && \
58    OPENSSL_VERSION_NUMBER < 0x10100000L)
59 # define FOLLY_OPENSSL_IS_110 (OPENSSL_VERSION_NUMBER >= 0x10100000L)
60 #endif
61
62 #if !OPENSSL_IS_BORINGSSL && !FOLLY_OPENSSL_IS_100 && !FOLLY_OPENSSL_IS_101 \
63     && !FOLLY_OPENSSL_IS_102 && !FOLLY_OPENSSL_IS_110
64 # warning Compiling with unsupported OpenSSL version
65 #endif
66
67 // BoringSSL and OpenSSL 0.9.8f later with TLS extension support SNI.
68 #if OPENSSL_IS_BORINGSSL ||          \
69     (OPENSSL_VERSION_NUMBER >= 0x00908070L && !defined(OPENSSL_NO_TLSEXT))
70 # define FOLLY_OPENSSL_HAS_SNI 1
71 #else
72 # define FOLLY_OPENSSL_HAS_SNI 0
73 #endif
74
75 // BoringSSL and OpenSSL 1.0.2 later with TLS extension support ALPN.
76 #if OPENSSL_IS_BORINGSSL ||          \
77     (OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined(OPENSSL_NO_TLSEXT))
78 # define FOLLY_OPENSSL_HAS_ALPN 1
79 #else
80 # define FOLLY_OPENSSL_HAS_ALPN 0
81 #endif
82
83 // This attempts to "unify" the OpenSSL libcrypto/libssl APIs between
84 // OpenSSL 1.0.2, 1.1.0 (and some earlier versions) and BoringSSL. The general
85 // idea is to provide namespaced wrapper methods for versions which do not
86 // which already exist in BoringSSL and 1.1.0, but there are few APIs such as
87 // SSL_CTX_set1_sigalgs_list and so on which exist in 1.0.2 but were removed
88 // in BoringSSL
89 namespace folly {
90 namespace portability {
91 namespace ssl {
92
93 #if OPENSSL_IS_BORINGSSL
94 int SSL_CTX_set1_sigalgs_list(SSL_CTX* ctx, const char* sigalgs_list);
95 int TLS1_get_client_version(SSL* s);
96 #endif
97
98 #if FOLLY_OPENSSL_IS_100
99 uint32_t SSL_CIPHER_get_id(const SSL_CIPHER*);
100 int TLS1_get_client_version(const SSL*);
101 #endif
102
103 #if FOLLY_OPENSSL_IS_100 || FOLLY_OPENSSL_IS_101
104 int X509_get_signature_nid(X509* cert);
105 #endif
106
107 #if FOLLY_OPENSSL_IS_100 || FOLLY_OPENSSL_IS_101 || FOLLY_OPENSSL_IS_102
108 int SSL_CTX_up_ref(SSL_CTX* session);
109 int SSL_SESSION_up_ref(SSL_SESSION* session);
110 int X509_up_ref(X509* x);
111 #endif
112
113 #if !FOLLY_OPENSSL_IS_110
114 void BIO_meth_free(BIO_METHOD* biom);
115 int BIO_meth_set_read(BIO_METHOD* biom, int (*read)(BIO*, char*, int));
116 int BIO_meth_set_write(BIO_METHOD* biom, int (*write)(BIO*, const char*, int));
117
118 const char* SSL_SESSION_get0_hostname(const SSL_SESSION* s);
119 unsigned char* ASN1_STRING_get0_data(const ASN1_STRING* x);
120
121 EVP_MD_CTX* EVP_MD_CTX_new();
122 void EVP_MD_CTX_free(EVP_MD_CTX* ctx);
123
124 HMAC_CTX* HMAC_CTX_new();
125 void HMAC_CTX_free(HMAC_CTX* ctx);
126
127 unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION* s);
128 int SSL_SESSION_has_ticket(const SSL_SESSION* s);
129 int DH_set0_pqg(DH* dh, BIGNUM* p, BIGNUM* q, BIGNUM* g);
130
131 X509* X509_STORE_CTX_get0_cert(X509_STORE_CTX* ctx);
132 STACK_OF(X509) * X509_STORE_CTX_get0_chain(X509_STORE_CTX* ctx);
133 STACK_OF(X509) * X509_STORE_CTX_get0_untrusted(X509_STORE_CTX* ctx);
134 #endif
135
136 #if FOLLY_OPENSSL_IS_110
137 // Note: this was a type and has been fixed upstream, so the next 1.1.0
138 // minor version upgrade will need to remove this
139 #define OPENSSL_lh_new OPENSSL_LH_new
140 #endif
141
142 }
143 }
144 }
145
146 FOLLY_PUSH_WARNING
147 #if __CLANG_PREREQ(3, 0)
148 FOLLY_GCC_DISABLE_WARNING("-Wheader-hygiene")
149 #endif
150 /* using override */ using namespace folly::portability::ssl;
151 FOLLY_POP_WARNING