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