Imported Upstream version 1.46.0
[platform/upstream/nghttp2.git] / src / tls.h
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2012 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #ifndef TLS_H
26 #define TLS_H
27
28 #include "nghttp2_config.h"
29
30 #include <cinttypes>
31
32 #include <openssl/ssl.h>
33
34 #include "ssl_compat.h"
35
36 namespace nghttp2 {
37
38 namespace tls {
39
40 // Acquire OpenSSL global lock to share SSL_CTX across multiple
41 // threads. The constructor acquires lock and destructor unlocks.
42 class LibsslGlobalLock {
43 public:
44   LibsslGlobalLock();
45   LibsslGlobalLock(const LibsslGlobalLock &) = delete;
46   LibsslGlobalLock &operator=(const LibsslGlobalLock &) = delete;
47 };
48
49 // Recommended general purpose "Modern compatibility" cipher suites by
50 // mozilla.
51 //
52 // https://wiki.mozilla.org/Security/Server_Side_TLS
53 constexpr char DEFAULT_CIPHER_LIST[] =
54     "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-"
55     "CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-"
56     "SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-"
57     "AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256";
58
59 constexpr char DEFAULT_TLS13_CIPHER_LIST[] =
60 #if OPENSSL_3_0_0_API
61     "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256"
62 #elif OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL)
63     TLS_DEFAULT_CIPHERSUITES
64 #else  // !OPENSSL_3_0_0_API && !(OPENSSL_1_1_1_API &&
65        // !defined(OPENSSL_IS_BORINGSSL))
66     ""
67 #endif // !OPENSSL_3_0_0_API && !(OPENSSL_1_1_1_API &&
68        // !defined(OPENSSL_IS_BORINGSSL))
69     ;
70
71 constexpr auto NGHTTP2_TLS_MIN_VERSION = TLS1_VERSION;
72 #ifdef TLS1_3_VERSION
73 constexpr auto NGHTTP2_TLS_MAX_VERSION = TLS1_3_VERSION;
74 #else  // !TLS1_3_VERSION
75 constexpr auto NGHTTP2_TLS_MAX_VERSION = TLS1_2_VERSION;
76 #endif // !TLS1_3_VERSION
77
78 const char *get_tls_protocol(SSL *ssl);
79
80 struct TLSSessionInfo {
81   const char *cipher;
82   const char *protocol;
83   const uint8_t *session_id;
84   bool session_reused;
85   size_t session_id_length;
86 };
87
88 TLSSessionInfo *get_tls_session_info(TLSSessionInfo *tls_info, SSL *ssl);
89
90 // Returns true iff the negotiated protocol is TLSv1.2.
91 bool check_http2_tls_version(SSL *ssl);
92
93 // Returns true iff the negotiated cipher suite is in HTTP/2 cipher
94 // block list.
95 bool check_http2_cipher_block_list(SSL *ssl);
96
97 // Returns true if SSL/TLS requirement for HTTP/2 is fulfilled.
98 // To fulfill the requirement, the following 2 terms must be hold:
99 //
100 // 1. The negotiated protocol must be TLSv1.2.
101 // 2. The negotiated cipher cuite is not listed in the block list
102 //    described in RFC 7540.
103 bool check_http2_requirement(SSL *ssl);
104
105 // Initializes OpenSSL library
106 void libssl_init();
107
108 // Sets TLS min and max versions to |ssl_ctx|.  This function returns
109 // 0 if it succeeds, or -1.
110 int ssl_ctx_set_proto_versions(SSL_CTX *ssl_ctx, int min, int max);
111
112 } // namespace tls
113
114 } // namespace nghttp2
115
116 #endif // TLS_H