Add support for OpenSSL 1.0
[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 <openssl/ssl.h>
23 #include <openssl/x509.h>
24 #include <cstdint>
25
26 namespace folly {
27 namespace ssl {
28
29 // BoringSSL doesn't have notion of versioning although it defines
30 // OPENSSL_VERSION_NUMBER to maintain compatibility. The following variables are
31 // intended to be specific to OpenSSL.
32 #if !defined(OPENSSL_IS_BORINGSSL)
33 #define FOLLY_OPENSSL_IS_100                \
34   (OPENSSL_VERSION_NUMBER >= 0x10000003L && \
35    OPENSSL_VERSION_NUMBER < 0x1000105fL)
36 #define FOLLY_OPENSSL_IS_101                \
37   (OPENSSL_VERSION_NUMBER >= 0x1000105fL && \
38    OPENSSL_VERSION_NUMBER < 0x1000200fL)
39 #define FOLLY_OPENSSL_IS_102                \
40   (OPENSSL_VERSION_NUMBER >= 0x1000200fL && \
41    OPENSSL_VERSION_NUMBER < 0x10100000L)
42 #define FOLLY_OPENSSL_IS_110 (OPENSSL_VERSION_NUMBER >= 0x10100000L)
43 #endif // !defined(OPENSSL_IS_BORINGSSL)
44
45 // BoringSSL and OpenSSL 1.0.2 later with TLS extension support ALPN.
46 #if defined(OPENSSL_IS_BORINGSSL) ||          \
47     (OPENSSL_VERSION_NUMBER >= 0x1000200fL && \
48      !defined(OPENSSL_NO_TLSEXT))
49 #define FOLLY_OPENSSL_HAS_ALPN 1
50 #else
51 #define FOLLY_OPENSSL_HAS_ALPN 0
52 #endif
53
54 // BoringSSL and OpenSSL 0.9.8f later with TLS extension support SNI.
55 #if defined(OPENSSL_IS_BORINGSSL) ||          \
56     (OPENSSL_VERSION_NUMBER >= 0x00908070L && \
57      !defined(OPENSSL_NO_TLSEXT))
58 #define FOLLY_OPENSSL_HAS_SNI 1
59 #else
60 #define FOLLY_OPENSSL_HAS_SNI 0
61 #endif
62
63 // This class attempts to "unify" the OpenSSL libssl APIs between OpenSSL 1.0.2,
64 // 1.1.0 and BoringSSL. The general idea is to provide wrapper methods for 1.0.2
65 // which already exist in BoringSSL and 1.1.0, but there are few APIs such as
66 // SSL_CTX_set1_sigalgs_list and so on which exist in 1.0.2 but were removed
67 // in BoringSSL
68
69 #ifdef OPENSSL_IS_BORINGSSL
70
71 int SSL_CTX_set1_sigalgs_list(SSL_CTX* ctx, const char* sigalgs_list);
72 int TLS1_get_client_version(SSL* s);
73 int BIO_meth_set_read(BIO_METHOD* biom, int (*read)(BIO*, char*, int));
74 int BIO_meth_set_write(BIO_METHOD* biom, int (*write)(BIO*, const char*, int));
75
76 #elif FOLLY_OPENSSL_IS_102 || FOLLY_OPENSSL_IS_101 || FOLLY_OPENSSL_IS_100
77
78 #if FOLLY_OPENSSL_IS_100
79
80 uint32_t SSL_CIPHER_get_id(const SSL_CIPHER*);
81 int TLS1_get_client_version(const SSL*);
82
83 #endif
84
85 int SSL_CTX_up_ref(SSL_CTX* session);
86 int SSL_SESSION_up_ref(SSL_SESSION* session);
87 int X509_up_ref(X509* x);
88 int BIO_meth_set_read(BIO_METHOD* biom, int (*read)(BIO*, char*, int));
89 int BIO_meth_set_write(BIO_METHOD* biom, int (*write)(BIO*, const char*, int));
90
91 #elif FOLLY_OPENSSL_IS_110
92
93 #else
94 #warning Compiling with unsupported OpenSSL version
95 #endif
96
97 } // ssl
98 } // folly