• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <string_view>
32 
33 #include "ssl_compat.h"
34 
35 using namespace std::literals;
36 
37 #ifdef NGHTTP2_OPENSSL_IS_WOLFSSL
38 #  include <wolfssl/options.h>
39 #  include <wolfssl/openssl/ssl.h>
40 #else // !NGHTTP2_OPENSSL_IS_WOLFSSL
41 #  include <openssl/ssl.h>
42 #endif // !NGHTTP2_OPENSSL_IS_WOLFSSL
43 
44 namespace nghttp2 {
45 
46 namespace tls {
47 
48 // Recommended general purpose "Intermediate compatibility" cipher
49 // suites for TLSv1.2 by mozilla.
50 //
51 // https://wiki.mozilla.org/Security/Server_Side_TLS
52 constexpr auto DEFAULT_CIPHER_LIST =
53   "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-"
54   "AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-"
55   "POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-"
56   "AES256-GCM-SHA384"sv;
57 
58 // Recommended general purpose "Modern compatibility" cipher suites
59 // for TLSv1.3 by mozilla.
60 //
61 // https://wiki.mozilla.org/Security/Server_Side_TLS
62 constexpr auto DEFAULT_TLS13_CIPHER_LIST =
63 #if defined(NGHTTP2_GENUINE_OPENSSL) ||                                        \
64   defined(NGHTTP2_OPENSSL_IS_LIBRESSL) || defined(NGHTTP2_OPENSSL_IS_WOLFSSL)
65   "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:"
66   "TLS_CHACHA20_POLY1305_SHA256"sv
67 #else  // !NGHTTP2_GENUINE_OPENSSL && !NGHTTP2_OPENSSL_IS_LIBRESSL
68   ""
69 #endif // !NGHTTP2_GENUINE_OPENSSL && !NGHTTP2_OPENSSL_IS_LIBRESSL
70   ;
71 
72 constexpr auto NGHTTP2_TLS_MIN_VERSION = TLS1_VERSION;
73 #ifdef TLS1_3_VERSION
74 constexpr auto NGHTTP2_TLS_MAX_VERSION = TLS1_3_VERSION;
75 #else  // !TLS1_3_VERSION
76 constexpr auto NGHTTP2_TLS_MAX_VERSION = TLS1_2_VERSION;
77 #endif // !TLS1_3_VERSION
78 
79 const char *get_tls_protocol(SSL *ssl);
80 
81 struct TLSSessionInfo {
82   const char *cipher;
83   const char *protocol;
84   const uint8_t *session_id;
85   bool session_reused;
86   size_t session_id_length;
87 };
88 
89 TLSSessionInfo *get_tls_session_info(TLSSessionInfo *tls_info, SSL *ssl);
90 
91 // Returns true iff the negotiated protocol is TLSv1.2.
92 bool check_http2_tls_version(SSL *ssl);
93 
94 // Returns true iff the negotiated cipher suite is in HTTP/2 cipher
95 // block list.
96 bool check_http2_cipher_block_list(SSL *ssl);
97 
98 // Returns true if SSL/TLS requirement for HTTP/2 is fulfilled.
99 // To fulfill the requirement, the following 2 terms must be hold:
100 //
101 // 1. The negotiated protocol must be TLSv1.2.
102 // 2. The negotiated cipher cuite is not listed in the block list
103 //    described in RFC 7540.
104 bool check_http2_requirement(SSL *ssl);
105 
106 // Sets TLS min and max versions to |ssl_ctx|.  This function returns
107 // 0 if it succeeds, or -1.
108 int ssl_ctx_set_proto_versions(SSL_CTX *ssl_ctx, int min, int max);
109 
110 constexpr uint16_t CERTIFICATE_COMPRESSION_ALGO_BROTLI = 2;
111 
112 #if defined(NGHTTP2_OPENSSL_IS_BORINGSSL) && defined(HAVE_LIBBROTLI)
113 int cert_compress(SSL *ssl, CBB *out, const uint8_t *in, size_t in_len);
114 
115 int cert_decompress(SSL *ssl, CRYPTO_BUFFER **out, size_t uncompressed_len,
116                     const uint8_t *in, size_t in_len);
117 #endif // NGHTTP2_OPENSSL_IS_BORINGSSL && HAVE_LIBBROTLI
118 
119 // Setup keylog callback.  It returns 0 if it succeeds, or -1.
120 int setup_keylog_callback(SSL_CTX *ssl_ctx);
121 
122 } // namespace tls
123 
124 } // namespace nghttp2
125 
126 #endif // TLS_H
127