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