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_1_1_1_API 61 TLS_DEFAULT_CIPHERSUITES 62 #else // !OPENSSL_1_1_1_API 63 "" 64 #endif // !OPENSSL_1_1_1_API 65 ; 66 67 constexpr auto NGHTTP2_TLS_MIN_VERSION = TLS1_VERSION; 68 #ifdef TLS1_3_VERSION 69 constexpr auto NGHTTP2_TLS_MAX_VERSION = TLS1_3_VERSION; 70 #else // !TLS1_3_VERSION 71 constexpr auto NGHTTP2_TLS_MAX_VERSION = TLS1_2_VERSION; 72 #endif // !TLS1_3_VERSION 73 74 const char *get_tls_protocol(SSL *ssl); 75 76 struct TLSSessionInfo { 77 const char *cipher; 78 const char *protocol; 79 const uint8_t *session_id; 80 bool session_reused; 81 size_t session_id_length; 82 }; 83 84 TLSSessionInfo *get_tls_session_info(TLSSessionInfo *tls_info, SSL *ssl); 85 86 // Returns true iff the negotiated protocol is TLSv1.2. 87 bool check_http2_tls_version(SSL *ssl); 88 89 // Returns true iff the negotiated cipher suite is in HTTP/2 cipher 90 // black list. 91 bool check_http2_cipher_black_list(SSL *ssl); 92 93 // Returns true if SSL/TLS requirement for HTTP/2 is fulfilled. 94 // To fulfill the requirement, the following 2 terms must be hold: 95 // 96 // 1. The negotiated protocol must be TLSv1.2. 97 // 2. The negotiated cipher cuite is not listed in the black list 98 // described in RFC 7540. 99 bool check_http2_requirement(SSL *ssl); 100 101 // Initializes OpenSSL library 102 void libssl_init(); 103 104 // Sets TLS min and max versions to |ssl_ctx|. This function returns 105 // 0 if it succeeds, or -1. 106 int ssl_ctx_set_proto_versions(SSL_CTX *ssl_ctx, int min, int max); 107 108 } // namespace tls 109 110 } // namespace nghttp2 111 112 #endif // TLS_H 113