1 1.1 christos /* $NetBSD: tls.c,v 1.7 2025/05/21 14:48:05 christos Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.1 christos * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 1.1 christos * 6 1.3 christos * SPDX-License-Identifier: MPL-2.0 7 1.3 christos * 8 1.1 christos * This Source Code Form is subject to the terms of the Mozilla Public 9 1.1 christos * License, v. 2.0. If a copy of the MPL was not distributed with this 10 1.1 christos * file, you can obtain one at https://mozilla.org/MPL/2.0/. 11 1.1 christos * 12 1.1 christos * See the COPYRIGHT file distributed with this work for additional 13 1.1 christos * information regarding copyright ownership. 14 1.1 christos */ 15 1.1 christos 16 1.2 christos #include <inttypes.h> 17 1.4 christos #include <netinet/in.h> 18 1.4 christos #include <stdlib.h> 19 1.4 christos #include <string.h> 20 1.4 christos #include <sys/socket.h> 21 1.4 christos #if HAVE_LIBNGHTTP2 22 1.4 christos #include <nghttp2/nghttp2.h> 23 1.4 christos #endif /* HAVE_LIBNGHTTP2 */ 24 1.4 christos #include <arpa/inet.h> 25 1.2 christos 26 1.1 christos #include <openssl/bn.h> 27 1.1 christos #include <openssl/conf.h> 28 1.3 christos #include <openssl/crypto.h> 29 1.4 christos #include <openssl/dh.h> 30 1.1 christos #include <openssl/err.h> 31 1.4 christos #include <openssl/evp.h> 32 1.1 christos #include <openssl/opensslv.h> 33 1.1 christos #include <openssl/rand.h> 34 1.1 christos #include <openssl/rsa.h> 35 1.4 christos #include <openssl/x509_vfy.h> 36 1.4 christos #include <openssl/x509v3.h> 37 1.1 christos 38 1.1 christos #include <isc/atomic.h> 39 1.4 christos #include <isc/ht.h> 40 1.1 christos #include <isc/log.h> 41 1.4 christos #include <isc/magic.h> 42 1.6 christos #include <isc/mem.h> 43 1.1 christos #include <isc/mutex.h> 44 1.1 christos #include <isc/mutexblock.h> 45 1.1 christos #include <isc/once.h> 46 1.4 christos #include <isc/random.h> 47 1.4 christos #include <isc/refcount.h> 48 1.4 christos #include <isc/rwlock.h> 49 1.4 christos #include <isc/sockaddr.h> 50 1.1 christos #include <isc/thread.h> 51 1.4 christos #include <isc/tls.h> 52 1.1 christos #include <isc/util.h> 53 1.1 christos 54 1.1 christos #include "openssl_shim.h" 55 1.1 christos 56 1.4 christos #define COMMON_SSL_OPTIONS \ 57 1.4 christos (SSL_OP_NO_COMPRESSION | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION) 58 1.4 christos 59 1.6 christos static isc_mem_t *isc__tls_mctx = NULL; 60 1.1 christos 61 1.1 christos #if OPENSSL_VERSION_NUMBER < 0x10100000L 62 1.1 christos static isc_mutex_t *locks = NULL; 63 1.1 christos static int nlocks; 64 1.1 christos 65 1.1 christos static void 66 1.1 christos isc__tls_lock_callback(int mode, int type, const char *file, int line) { 67 1.1 christos UNUSED(file); 68 1.1 christos UNUSED(line); 69 1.1 christos if ((mode & CRYPTO_LOCK) != 0) { 70 1.1 christos LOCK(&locks[type]); 71 1.1 christos } else { 72 1.1 christos UNLOCK(&locks[type]); 73 1.1 christos } 74 1.1 christos } 75 1.1 christos 76 1.1 christos static void 77 1.1 christos isc__tls_set_thread_id(CRYPTO_THREADID *id) { 78 1.1 christos CRYPTO_THREADID_set_numeric(id, (unsigned long)isc_thread_self()); 79 1.1 christos } 80 1.1 christos #endif 81 1.1 christos 82 1.6 christos #if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x30000000L 83 1.6 christos /* 84 1.6 christos * This was crippled with LibreSSL, so just skip it: 85 1.6 christos * https://cvsweb.openbsd.org/src/lib/libcrypto/Attic/mem.c 86 1.6 christos */ 87 1.6 christos 88 1.6 christos #if ISC_MEM_TRACKLINES 89 1.6 christos /* 90 1.6 christos * We use the internal isc__mem API here, so we can pass the file and line 91 1.6 christos * arguments passed from OpenSSL >= 1.1.0 to our memory functions for better 92 1.6 christos * tracking of the OpenSSL allocations. Without this, we would always just see 93 1.6 christos * isc__tls_{malloc,realloc,free} in the tracking output, but with this in place 94 1.6 christos * we get to see the places in the OpenSSL code where the allocations happen. 95 1.6 christos */ 96 1.6 christos 97 1.6 christos static void * 98 1.6 christos isc__tls_malloc_ex(size_t size, const char *file, int line) { 99 1.6 christos return isc__mem_allocate(isc__tls_mctx, size, 0, file, 100 1.6 christos (unsigned int)line); 101 1.6 christos } 102 1.6 christos 103 1.6 christos static void * 104 1.6 christos isc__tls_realloc_ex(void *ptr, size_t size, const char *file, int line) { 105 1.6 christos return isc__mem_reallocate(isc__tls_mctx, ptr, size, 0, file, 106 1.6 christos (unsigned int)line); 107 1.6 christos } 108 1.6 christos 109 1.6 christos static void 110 1.6 christos isc__tls_free_ex(void *ptr, const char *file, int line) { 111 1.6 christos if (ptr == NULL) { 112 1.6 christos return; 113 1.6 christos } 114 1.6 christos if (isc__tls_mctx != NULL) { 115 1.6 christos isc__mem_free(isc__tls_mctx, ptr, 0, file, (unsigned int)line); 116 1.6 christos } 117 1.6 christos } 118 1.6 christos 119 1.6 christos #else /* ISC_MEM_TRACKLINES */ 120 1.6 christos 121 1.6 christos static void * 122 1.6 christos isc__tls_malloc_ex(size_t size, const char *file, int line) { 123 1.6 christos UNUSED(file); 124 1.6 christos UNUSED(line); 125 1.6 christos return isc_mem_allocate(isc__tls_mctx, size); 126 1.6 christos } 127 1.6 christos 128 1.6 christos static void * 129 1.6 christos isc__tls_realloc_ex(void *ptr, size_t size, const char *file, int line) { 130 1.6 christos UNUSED(file); 131 1.6 christos UNUSED(line); 132 1.6 christos return isc_mem_reallocate(isc__tls_mctx, ptr, size); 133 1.6 christos } 134 1.6 christos 135 1.1 christos static void 136 1.6 christos isc__tls_free_ex(void *ptr, const char *file, int line) { 137 1.6 christos UNUSED(file); 138 1.6 christos UNUSED(line); 139 1.6 christos if (ptr == NULL) { 140 1.6 christos return; 141 1.6 christos } 142 1.6 christos if (isc__tls_mctx != NULL) { 143 1.6 christos isc__mem_free(isc__tls_mctx, ptr, 0); 144 1.6 christos } 145 1.6 christos } 146 1.6 christos 147 1.6 christos #endif /* ISC_MEM_TRACKLINES */ 148 1.6 christos 149 1.6 christos #endif /* !defined(LIBRESSL_VERSION_NUMBER) */ 150 1.6 christos 151 1.6 christos void 152 1.6 christos isc__tls_initialize(void) { 153 1.6 christos isc_mem_create(&isc__tls_mctx); 154 1.6 christos isc_mem_setname(isc__tls_mctx, "OpenSSL"); 155 1.6 christos isc_mem_setdestroycheck(isc__tls_mctx, false); 156 1.6 christos 157 1.6 christos #if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x30000000L 158 1.6 christos /* 159 1.6 christos * CRYPTO_set_mem_(_ex)_functions() returns 1 on success or 0 on 160 1.6 christos * failure, which means OpenSSL already allocated some memory. There's 161 1.6 christos * nothing we can do about it. 162 1.6 christos */ 163 1.6 christos (void)CRYPTO_set_mem_functions(isc__tls_malloc_ex, isc__tls_realloc_ex, 164 1.6 christos isc__tls_free_ex); 165 1.6 christos #endif /* !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= \ 166 1.6 christos 0x30000000L */ 167 1.1 christos 168 1.1 christos #if OPENSSL_VERSION_NUMBER >= 0x10100000L 169 1.6 christos uint64_t opts = OPENSSL_INIT_ENGINE_ALL_BUILTIN | 170 1.6 christos OPENSSL_INIT_LOAD_CONFIG; 171 1.6 christos #if defined(OPENSSL_INIT_NO_ATEXIT) 172 1.6 christos /* 173 1.6 christos * We call OPENSSL_cleanup() manually, in a correct order, thus disable 174 1.6 christos * the automatic atexit() handler. 175 1.6 christos */ 176 1.6 christos opts |= OPENSSL_INIT_NO_ATEXIT; 177 1.6 christos #endif 178 1.6 christos 179 1.6 christos RUNTIME_CHECK(OPENSSL_init_ssl(opts, NULL) == 1); 180 1.1 christos #else 181 1.1 christos nlocks = CRYPTO_num_locks(); 182 1.6 christos locks = isc_mem_cget(isc__tls_mctx, nlocks, sizeof(locks[0])); 183 1.1 christos isc_mutexblock_init(locks, nlocks); 184 1.1 christos CRYPTO_set_locking_callback(isc__tls_lock_callback); 185 1.1 christos CRYPTO_THREADID_set_callback(isc__tls_set_thread_id); 186 1.1 christos 187 1.1 christos CRYPTO_malloc_init(); 188 1.1 christos ERR_load_crypto_strings(); 189 1.1 christos SSL_load_error_strings(); 190 1.1 christos SSL_library_init(); 191 1.1 christos 192 1.4 christos #if !defined(OPENSSL_NO_ENGINE) && OPENSSL_API_LEVEL < 30000 193 1.1 christos ENGINE_load_builtin_engines(); 194 1.1 christos #endif 195 1.1 christos OpenSSL_add_all_algorithms(); 196 1.1 christos OPENSSL_load_builtin_modules(); 197 1.1 christos 198 1.1 christos CONF_modules_load_file(NULL, NULL, 199 1.1 christos CONF_MFLAGS_DEFAULT_SECTION | 200 1.1 christos CONF_MFLAGS_IGNORE_MISSING_FILE); 201 1.1 christos #endif 202 1.1 christos 203 1.1 christos /* Protect ourselves against unseeded PRNG */ 204 1.1 christos if (RAND_status() != 1) { 205 1.4 christos FATAL_ERROR("OpenSSL pseudorandom number generator " 206 1.1 christos "cannot be initialized (see the `PRNG not " 207 1.1 christos "seeded' message in the OpenSSL FAQ)"); 208 1.1 christos } 209 1.1 christos } 210 1.1 christos 211 1.1 christos void 212 1.6 christos isc__tls_shutdown(void) { 213 1.3 christos #if OPENSSL_VERSION_NUMBER >= 0x10100000L 214 1.3 christos OPENSSL_cleanup(); 215 1.3 christos #else 216 1.1 christos CONF_modules_unload(1); 217 1.1 christos OBJ_cleanup(); 218 1.1 christos EVP_cleanup(); 219 1.4 christos #if !defined(OPENSSL_NO_ENGINE) && OPENSSL_API_LEVEL < 30000 220 1.1 christos ENGINE_cleanup(); 221 1.1 christos #endif 222 1.1 christos CRYPTO_cleanup_all_ex_data(); 223 1.1 christos ERR_remove_thread_state(NULL); 224 1.1 christos RAND_cleanup(); 225 1.1 christos ERR_free_strings(); 226 1.1 christos 227 1.1 christos CRYPTO_set_locking_callback(NULL); 228 1.1 christos 229 1.1 christos if (locks != NULL) { 230 1.1 christos isc_mutexblock_destroy(locks, nlocks); 231 1.6 christos isc_mem_cput(isc__tls_mctx, locks, nlocks, sizeof(locks[0])); 232 1.1 christos locks = NULL; 233 1.1 christos } 234 1.1 christos #endif 235 1.1 christos 236 1.6 christos isc_mem_destroy(&isc__tls_mctx); 237 1.1 christos } 238 1.1 christos 239 1.1 christos void 240 1.6 christos isc__tls_setdestroycheck(bool check) { 241 1.6 christos isc_mem_setdestroycheck(isc__tls_mctx, check); 242 1.1 christos } 243 1.4 christos 244 1.4 christos void 245 1.4 christos isc_tlsctx_free(isc_tlsctx_t **ctxp) { 246 1.4 christos SSL_CTX *ctx = NULL; 247 1.4 christos REQUIRE(ctxp != NULL && *ctxp != NULL); 248 1.4 christos 249 1.4 christos ctx = *ctxp; 250 1.4 christos *ctxp = NULL; 251 1.4 christos 252 1.4 christos SSL_CTX_free(ctx); 253 1.4 christos } 254 1.4 christos 255 1.4 christos void 256 1.4 christos isc_tlsctx_attach(isc_tlsctx_t *src, isc_tlsctx_t **ptarget) { 257 1.4 christos REQUIRE(src != NULL); 258 1.4 christos REQUIRE(ptarget != NULL && *ptarget == NULL); 259 1.4 christos 260 1.4 christos RUNTIME_CHECK(SSL_CTX_up_ref(src) == 1); 261 1.4 christos 262 1.4 christos *ptarget = src; 263 1.4 christos } 264 1.4 christos 265 1.4 christos #if HAVE_SSL_CTX_SET_KEYLOG_CALLBACK 266 1.4 christos /* 267 1.4 christos * Callback invoked by the SSL library whenever a new TLS pre-master secret 268 1.4 christos * needs to be logged. 269 1.4 christos */ 270 1.4 christos static void 271 1.4 christos sslkeylogfile_append(const SSL *ssl, const char *line) { 272 1.4 christos UNUSED(ssl); 273 1.4 christos 274 1.4 christos isc_log_write(isc_lctx, ISC_LOGCATEGORY_SSLKEYLOG, ISC_LOGMODULE_NETMGR, 275 1.4 christos ISC_LOG_INFO, "%s", line); 276 1.4 christos } 277 1.4 christos 278 1.4 christos /* 279 1.4 christos * Enable TLS pre-master secret logging if the SSLKEYLOGFILE environment 280 1.4 christos * variable is set. This needs to be done on a per-context basis as that is 281 1.4 christos * how SSL_CTX_set_keylog_callback() works. 282 1.4 christos */ 283 1.4 christos static void 284 1.4 christos sslkeylogfile_init(isc_tlsctx_t *ctx) { 285 1.4 christos if (getenv("SSLKEYLOGFILE") != NULL) { 286 1.4 christos SSL_CTX_set_keylog_callback(ctx, sslkeylogfile_append); 287 1.4 christos } 288 1.4 christos } 289 1.4 christos #else /* HAVE_SSL_CTX_SET_KEYLOG_CALLBACK */ 290 1.4 christos #define sslkeylogfile_init(ctx) 291 1.4 christos #endif /* HAVE_SSL_CTX_SET_KEYLOG_CALLBACK */ 292 1.4 christos 293 1.4 christos isc_result_t 294 1.4 christos isc_tlsctx_createclient(isc_tlsctx_t **ctxp) { 295 1.4 christos unsigned long err; 296 1.4 christos char errbuf[256]; 297 1.4 christos SSL_CTX *ctx = NULL; 298 1.4 christos const SSL_METHOD *method = NULL; 299 1.4 christos 300 1.4 christos REQUIRE(ctxp != NULL && *ctxp == NULL); 301 1.4 christos 302 1.4 christos method = TLS_client_method(); 303 1.4 christos if (method == NULL) { 304 1.4 christos goto ssl_error; 305 1.4 christos } 306 1.4 christos ctx = SSL_CTX_new(method); 307 1.4 christos if (ctx == NULL) { 308 1.4 christos goto ssl_error; 309 1.4 christos } 310 1.4 christos 311 1.4 christos SSL_CTX_set_options(ctx, COMMON_SSL_OPTIONS); 312 1.4 christos 313 1.4 christos #if HAVE_SSL_CTX_SET_MIN_PROTO_VERSION 314 1.4 christos SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); 315 1.4 christos #else 316 1.4 christos SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | 317 1.4 christos SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1); 318 1.4 christos #endif 319 1.4 christos 320 1.4 christos sslkeylogfile_init(ctx); 321 1.4 christos 322 1.4 christos *ctxp = ctx; 323 1.4 christos 324 1.6 christos return ISC_R_SUCCESS; 325 1.4 christos 326 1.4 christos ssl_error: 327 1.4 christos err = ERR_get_error(); 328 1.4 christos ERR_error_string_n(err, errbuf, sizeof(errbuf)); 329 1.4 christos isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_NETMGR, 330 1.4 christos ISC_LOG_ERROR, "Error initializing TLS context: %s", 331 1.4 christos errbuf); 332 1.4 christos 333 1.6 christos return ISC_R_TLSERROR; 334 1.4 christos } 335 1.4 christos 336 1.4 christos isc_result_t 337 1.4 christos isc_tlsctx_load_certificate(isc_tlsctx_t *ctx, const char *keyfile, 338 1.4 christos const char *certfile) { 339 1.4 christos int rv; 340 1.4 christos REQUIRE(ctx != NULL); 341 1.4 christos REQUIRE(keyfile != NULL); 342 1.4 christos REQUIRE(certfile != NULL); 343 1.4 christos 344 1.4 christos rv = SSL_CTX_use_certificate_chain_file(ctx, certfile); 345 1.4 christos if (rv != 1) { 346 1.6 christos unsigned long err = ERR_peek_last_error(); 347 1.6 christos char errbuf[1024] = { 0 }; 348 1.6 christos ERR_error_string_n(err, errbuf, sizeof(errbuf)); 349 1.6 christos isc_log_write( 350 1.6 christos isc_lctx, ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_NETMGR, 351 1.6 christos ISC_LOG_ERROR, 352 1.6 christos "SSL_CTX_use_certificate_chain_file: '%s' failed: %s", 353 1.6 christos certfile, errbuf); 354 1.6 christos return ISC_R_TLSERROR; 355 1.4 christos } 356 1.4 christos rv = SSL_CTX_use_PrivateKey_file(ctx, keyfile, SSL_FILETYPE_PEM); 357 1.4 christos if (rv != 1) { 358 1.6 christos unsigned long err = ERR_peek_last_error(); 359 1.6 christos char errbuf[1024] = { 0 }; 360 1.6 christos ERR_error_string_n(err, errbuf, sizeof(errbuf)); 361 1.6 christos isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL, 362 1.6 christos ISC_LOGMODULE_NETMGR, ISC_LOG_ERROR, 363 1.6 christos "SSL_CTX_use_PrivateKey_file: '%s' failed: %s", 364 1.6 christos keyfile, errbuf); 365 1.6 christos return ISC_R_TLSERROR; 366 1.4 christos } 367 1.4 christos 368 1.6 christos return ISC_R_SUCCESS; 369 1.4 christos } 370 1.4 christos 371 1.4 christos isc_result_t 372 1.4 christos isc_tlsctx_createserver(const char *keyfile, const char *certfile, 373 1.4 christos isc_tlsctx_t **ctxp) { 374 1.4 christos int rv; 375 1.4 christos unsigned long err; 376 1.4 christos bool ephemeral = (keyfile == NULL && certfile == NULL); 377 1.4 christos X509 *cert = NULL; 378 1.4 christos EVP_PKEY *pkey = NULL; 379 1.4 christos SSL_CTX *ctx = NULL; 380 1.4 christos #if OPENSSL_VERSION_NUMBER < 0x30000000L 381 1.4 christos EC_KEY *eckey = NULL; 382 1.4 christos #else 383 1.4 christos EVP_PKEY_CTX *pkey_ctx = NULL; 384 1.4 christos EVP_PKEY *params_pkey = NULL; 385 1.4 christos #endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */ 386 1.4 christos char errbuf[256]; 387 1.4 christos const SSL_METHOD *method = NULL; 388 1.4 christos 389 1.4 christos REQUIRE(ctxp != NULL && *ctxp == NULL); 390 1.4 christos REQUIRE((keyfile == NULL) == (certfile == NULL)); 391 1.4 christos 392 1.4 christos method = TLS_server_method(); 393 1.4 christos if (method == NULL) { 394 1.4 christos goto ssl_error; 395 1.4 christos } 396 1.4 christos ctx = SSL_CTX_new(method); 397 1.4 christos if (ctx == NULL) { 398 1.4 christos goto ssl_error; 399 1.4 christos } 400 1.4 christos RUNTIME_CHECK(ctx != NULL); 401 1.4 christos 402 1.4 christos SSL_CTX_set_options(ctx, COMMON_SSL_OPTIONS); 403 1.4 christos 404 1.4 christos #if HAVE_SSL_CTX_SET_MIN_PROTO_VERSION 405 1.4 christos SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); 406 1.4 christos #else 407 1.4 christos SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | 408 1.4 christos SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1); 409 1.4 christos #endif 410 1.4 christos 411 1.4 christos if (ephemeral) { 412 1.4 christos const int group_nid = NID_X9_62_prime256v1; 413 1.4 christos 414 1.4 christos #if OPENSSL_VERSION_NUMBER < 0x30000000L 415 1.4 christos eckey = EC_KEY_new_by_curve_name(group_nid); 416 1.4 christos if (eckey == NULL) { 417 1.4 christos goto ssl_error; 418 1.4 christos } 419 1.4 christos 420 1.4 christos /* Generate the key. */ 421 1.4 christos rv = EC_KEY_generate_key(eckey); 422 1.4 christos if (rv != 1) { 423 1.4 christos goto ssl_error; 424 1.4 christos } 425 1.4 christos pkey = EVP_PKEY_new(); 426 1.4 christos if (pkey == NULL) { 427 1.4 christos goto ssl_error; 428 1.4 christos } 429 1.4 christos rv = EVP_PKEY_set1_EC_KEY(pkey, eckey); 430 1.4 christos if (rv != 1) { 431 1.4 christos goto ssl_error; 432 1.4 christos } 433 1.4 christos 434 1.4 christos /* Use a named curve and uncompressed point conversion form. */ 435 1.4 christos #if HAVE_EVP_PKEY_GET0_EC_KEY 436 1.4 christos EC_KEY_set_asn1_flag(EVP_PKEY_get0_EC_KEY(pkey), 437 1.4 christos OPENSSL_EC_NAMED_CURVE); 438 1.4 christos EC_KEY_set_conv_form(EVP_PKEY_get0_EC_KEY(pkey), 439 1.4 christos POINT_CONVERSION_UNCOMPRESSED); 440 1.4 christos #else 441 1.4 christos EC_KEY_set_asn1_flag(pkey->pkey.ec, OPENSSL_EC_NAMED_CURVE); 442 1.4 christos EC_KEY_set_conv_form(pkey->pkey.ec, 443 1.4 christos POINT_CONVERSION_UNCOMPRESSED); 444 1.4 christos #endif /* HAVE_EVP_PKEY_GET0_EC_KEY */ 445 1.4 christos 446 1.4 christos #if defined(SSL_CTX_set_ecdh_auto) 447 1.4 christos /* 448 1.4 christos * Using this macro is required for older versions of OpenSSL to 449 1.4 christos * automatically enable ECDH support. 450 1.4 christos * 451 1.4 christos * On later versions this function is no longer needed and is 452 1.4 christos * deprecated. 453 1.4 christos */ 454 1.4 christos (void)SSL_CTX_set_ecdh_auto(ctx, 1); 455 1.4 christos #endif /* defined(SSL_CTX_set_ecdh_auto) */ 456 1.4 christos 457 1.4 christos /* Cleanup */ 458 1.4 christos EC_KEY_free(eckey); 459 1.4 christos eckey = NULL; 460 1.4 christos #else 461 1.4 christos /* Generate the key's parameters. */ 462 1.4 christos pkey_ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL); 463 1.4 christos if (pkey_ctx == NULL) { 464 1.4 christos goto ssl_error; 465 1.4 christos } 466 1.4 christos rv = EVP_PKEY_paramgen_init(pkey_ctx); 467 1.4 christos if (rv != 1) { 468 1.4 christos goto ssl_error; 469 1.4 christos } 470 1.4 christos rv = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pkey_ctx, 471 1.4 christos group_nid); 472 1.4 christos if (rv != 1) { 473 1.4 christos goto ssl_error; 474 1.4 christos } 475 1.4 christos rv = EVP_PKEY_paramgen(pkey_ctx, ¶ms_pkey); 476 1.4 christos if (rv != 1 || params_pkey == NULL) { 477 1.4 christos goto ssl_error; 478 1.4 christos } 479 1.4 christos EVP_PKEY_CTX_free(pkey_ctx); 480 1.4 christos 481 1.4 christos /* Generate the key. */ 482 1.4 christos pkey_ctx = EVP_PKEY_CTX_new(params_pkey, NULL); 483 1.4 christos if (pkey_ctx == NULL) { 484 1.4 christos goto ssl_error; 485 1.4 christos } 486 1.4 christos rv = EVP_PKEY_keygen_init(pkey_ctx); 487 1.4 christos if (rv != 1) { 488 1.4 christos goto ssl_error; 489 1.4 christos } 490 1.4 christos rv = EVP_PKEY_keygen(pkey_ctx, &pkey); 491 1.4 christos if (rv != 1 || pkey == NULL) { 492 1.4 christos goto ssl_error; 493 1.4 christos } 494 1.4 christos 495 1.4 christos /* Cleanup */ 496 1.4 christos EVP_PKEY_free(params_pkey); 497 1.4 christos params_pkey = NULL; 498 1.4 christos EVP_PKEY_CTX_free(pkey_ctx); 499 1.4 christos pkey_ctx = NULL; 500 1.4 christos #endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */ 501 1.4 christos 502 1.4 christos cert = X509_new(); 503 1.4 christos if (cert == NULL) { 504 1.4 christos goto ssl_error; 505 1.4 christos } 506 1.4 christos 507 1.4 christos ASN1_INTEGER_set(X509_get_serialNumber(cert), 508 1.4 christos (long)isc_random32()); 509 1.4 christos 510 1.4 christos /* 511 1.4 christos * Set the "not before" property 5 minutes into the past to 512 1.4 christos * accommodate with some possible clock skew across systems. 513 1.4 christos */ 514 1.4 christos #if OPENSSL_VERSION_NUMBER < 0x10101000L 515 1.4 christos X509_gmtime_adj(X509_get_notBefore(cert), -300); 516 1.4 christos #else 517 1.4 christos X509_gmtime_adj(X509_getm_notBefore(cert), -300); 518 1.4 christos #endif 519 1.4 christos 520 1.4 christos /* 521 1.4 christos * We set the vailidy for 10 years. 522 1.4 christos */ 523 1.4 christos #if OPENSSL_VERSION_NUMBER < 0x10101000L 524 1.4 christos X509_gmtime_adj(X509_get_notAfter(cert), 3650 * 24 * 3600); 525 1.4 christos #else 526 1.4 christos X509_gmtime_adj(X509_getm_notAfter(cert), 3650 * 24 * 3600); 527 1.4 christos #endif 528 1.4 christos 529 1.4 christos X509_set_pubkey(cert, pkey); 530 1.4 christos 531 1.4 christos X509_NAME *name = X509_get_subject_name(cert); 532 1.4 christos 533 1.4 christos X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, 534 1.4 christos (const unsigned char *)"AQ", -1, -1, 535 1.4 christos 0); 536 1.4 christos X509_NAME_add_entry_by_txt( 537 1.4 christos name, "O", MBSTRING_ASC, 538 1.4 christos (const unsigned char *)"BIND9 ephemeral " 539 1.4 christos "certificate", 540 1.4 christos -1, -1, 0); 541 1.4 christos X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, 542 1.4 christos (const unsigned char *)"bind9.local", 543 1.4 christos -1, -1, 0); 544 1.4 christos 545 1.4 christos X509_set_issuer_name(cert, name); 546 1.4 christos X509_sign(cert, pkey, EVP_sha256()); 547 1.4 christos rv = SSL_CTX_use_certificate(ctx, cert); 548 1.4 christos if (rv != 1) { 549 1.4 christos goto ssl_error; 550 1.4 christos } 551 1.4 christos rv = SSL_CTX_use_PrivateKey(ctx, pkey); 552 1.4 christos if (rv != 1) { 553 1.4 christos goto ssl_error; 554 1.4 christos } 555 1.4 christos 556 1.4 christos X509_free(cert); 557 1.4 christos EVP_PKEY_free(pkey); 558 1.4 christos } else { 559 1.4 christos isc_result_t result; 560 1.4 christos result = isc_tlsctx_load_certificate(ctx, keyfile, certfile); 561 1.4 christos if (result != ISC_R_SUCCESS) { 562 1.4 christos goto ssl_error; 563 1.4 christos } 564 1.4 christos } 565 1.4 christos 566 1.4 christos sslkeylogfile_init(ctx); 567 1.4 christos 568 1.4 christos *ctxp = ctx; 569 1.6 christos return ISC_R_SUCCESS; 570 1.4 christos 571 1.4 christos ssl_error: 572 1.4 christos err = ERR_get_error(); 573 1.4 christos ERR_error_string_n(err, errbuf, sizeof(errbuf)); 574 1.4 christos isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_NETMGR, 575 1.4 christos ISC_LOG_ERROR, "Error initializing TLS context: %s", 576 1.4 christos errbuf); 577 1.4 christos 578 1.4 christos if (ctx != NULL) { 579 1.4 christos SSL_CTX_free(ctx); 580 1.4 christos } 581 1.4 christos if (cert != NULL) { 582 1.4 christos X509_free(cert); 583 1.4 christos } 584 1.4 christos if (pkey != NULL) { 585 1.4 christos EVP_PKEY_free(pkey); 586 1.4 christos } 587 1.4 christos #if OPENSSL_VERSION_NUMBER < 0x30000000L 588 1.4 christos if (eckey != NULL) { 589 1.4 christos EC_KEY_free(eckey); 590 1.4 christos } 591 1.4 christos #else 592 1.4 christos if (params_pkey != NULL) { 593 1.4 christos EVP_PKEY_free(params_pkey); 594 1.4 christos } 595 1.4 christos if (pkey_ctx != NULL) { 596 1.4 christos EVP_PKEY_CTX_free(pkey_ctx); 597 1.4 christos } 598 1.4 christos #endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */ 599 1.4 christos 600 1.6 christos return ISC_R_TLSERROR; 601 1.4 christos } 602 1.4 christos 603 1.4 christos static long 604 1.4 christos get_tls_version_disable_bit(const isc_tls_protocol_version_t tls_ver) { 605 1.4 christos long bit = 0; 606 1.4 christos 607 1.4 christos switch (tls_ver) { 608 1.4 christos case ISC_TLS_PROTO_VER_1_2: 609 1.4 christos #ifdef SSL_OP_NO_TLSv1_2 610 1.4 christos bit = SSL_OP_NO_TLSv1_2; 611 1.4 christos #else 612 1.4 christos bit = 0; 613 1.4 christos #endif 614 1.4 christos break; 615 1.4 christos case ISC_TLS_PROTO_VER_1_3: 616 1.4 christos #ifdef SSL_OP_NO_TLSv1_3 617 1.4 christos bit = SSL_OP_NO_TLSv1_3; 618 1.4 christos #else 619 1.4 christos bit = 0; 620 1.4 christos #endif 621 1.4 christos break; 622 1.4 christos default: 623 1.4 christos UNREACHABLE(); 624 1.4 christos break; 625 1.4 christos }; 626 1.4 christos 627 1.6 christos return bit; 628 1.4 christos } 629 1.4 christos 630 1.4 christos bool 631 1.4 christos isc_tls_protocol_supported(const isc_tls_protocol_version_t tls_ver) { 632 1.6 christos return get_tls_version_disable_bit(tls_ver) != 0; 633 1.4 christos } 634 1.4 christos 635 1.4 christos isc_tls_protocol_version_t 636 1.4 christos isc_tls_protocol_name_to_version(const char *name) { 637 1.4 christos REQUIRE(name != NULL); 638 1.4 christos 639 1.4 christos if (strcasecmp(name, "TLSv1.2") == 0) { 640 1.6 christos return ISC_TLS_PROTO_VER_1_2; 641 1.4 christos } else if (strcasecmp(name, "TLSv1.3") == 0) { 642 1.6 christos return ISC_TLS_PROTO_VER_1_3; 643 1.4 christos } 644 1.4 christos 645 1.6 christos return ISC_TLS_PROTO_VER_UNDEFINED; 646 1.4 christos } 647 1.4 christos 648 1.4 christos void 649 1.4 christos isc_tlsctx_set_protocols(isc_tlsctx_t *ctx, const uint32_t tls_versions) { 650 1.4 christos REQUIRE(ctx != NULL); 651 1.4 christos REQUIRE(tls_versions != 0); 652 1.4 christos long set_options = 0; 653 1.4 christos long clear_options = 0; 654 1.4 christos uint32_t versions = tls_versions; 655 1.4 christos 656 1.4 christos /* 657 1.4 christos * The code below might be initially hard to follow because of the 658 1.4 christos * double negation that OpenSSL enforces. 659 1.4 christos * 660 1.4 christos * Taking into account that OpenSSL provides bits to *disable* 661 1.4 christos * specific protocol versions, like SSL_OP_NO_TLSv1_2, 662 1.4 christos * SSL_OP_NO_TLSv1_3, etc., the code has the following logic: 663 1.4 christos * 664 1.4 christos * If a protocol version is not specified in the bitmask, get the 665 1.4 christos * bit that disables it and add it to the set of TLS options to 666 1.4 christos * set ('set_options'). Otherwise, if a protocol version is set, 667 1.4 christos * add the bit to the set of options to clear ('clear_options'). 668 1.4 christos */ 669 1.4 christos 670 1.4 christos /* TLS protocol versions are defined as powers of two. */ 671 1.4 christos for (uint32_t tls_ver = ISC_TLS_PROTO_VER_1_2; 672 1.4 christos tls_ver < ISC_TLS_PROTO_VER_UNDEFINED; tls_ver <<= 1) 673 1.4 christos { 674 1.4 christos if ((tls_versions & tls_ver) == 0) { 675 1.4 christos set_options |= get_tls_version_disable_bit(tls_ver); 676 1.4 christos } else { 677 1.4 christos /* 678 1.4 christos * Only supported versions should ever be passed to the 679 1.4 christos * function SSL_CTX_clear_options. For example, in order 680 1.4 christos * to enable TLS v1.2, we have to clear 681 1.4 christos * SSL_OP_NO_TLSv1_2. Insist that the configuration file 682 1.4 christos * was verified properly, so we are not trying to enable 683 1.4 christos * an unsupported TLS version. 684 1.4 christos */ 685 1.4 christos INSIST(isc_tls_protocol_supported(tls_ver)); 686 1.4 christos clear_options |= get_tls_version_disable_bit(tls_ver); 687 1.4 christos } 688 1.4 christos versions &= ~(tls_ver); 689 1.4 christos } 690 1.4 christos 691 1.4 christos /* All versions should be processed at this point, thus the value 692 1.4 christos * must equal zero. If it is not, then some garbage has been 693 1.4 christos * passed to the function; this situation is worth 694 1.4 christos * investigation. */ 695 1.4 christos INSIST(versions == 0); 696 1.4 christos 697 1.4 christos (void)SSL_CTX_set_options(ctx, set_options); 698 1.4 christos (void)SSL_CTX_clear_options(ctx, clear_options); 699 1.4 christos } 700 1.4 christos 701 1.4 christos bool 702 1.4 christos isc_tlsctx_load_dhparams(isc_tlsctx_t *ctx, const char *dhparams_file) { 703 1.4 christos REQUIRE(ctx != NULL); 704 1.4 christos REQUIRE(dhparams_file != NULL); 705 1.4 christos REQUIRE(*dhparams_file != '\0'); 706 1.4 christos 707 1.4 christos #if OPENSSL_VERSION_NUMBER < 0x30000000L 708 1.4 christos /* OpenSSL < 3.0 */ 709 1.4 christos DH *dh = NULL; 710 1.4 christos FILE *paramfile; 711 1.4 christos 712 1.4 christos paramfile = fopen(dhparams_file, "r"); 713 1.4 christos 714 1.4 christos if (paramfile) { 715 1.4 christos int check = 0; 716 1.4 christos dh = PEM_read_DHparams(paramfile, NULL, NULL, NULL); 717 1.4 christos fclose(paramfile); 718 1.4 christos 719 1.4 christos if (dh == NULL) { 720 1.6 christos return false; 721 1.4 christos } else if (DH_check(dh, &check) != 1 || check != 0) { 722 1.4 christos DH_free(dh); 723 1.6 christos return false; 724 1.4 christos } 725 1.4 christos } else { 726 1.6 christos return false; 727 1.4 christos } 728 1.4 christos 729 1.4 christos if (SSL_CTX_set_tmp_dh(ctx, dh) != 1) { 730 1.4 christos DH_free(dh); 731 1.6 christos return false; 732 1.4 christos } 733 1.4 christos 734 1.4 christos DH_free(dh); 735 1.4 christos #else 736 1.4 christos /* OpenSSL >= 3.0: low level DH APIs are deprecated in OpenSSL 3.0 */ 737 1.4 christos EVP_PKEY *dh = NULL; 738 1.4 christos BIO *bio = NULL; 739 1.4 christos 740 1.4 christos bio = BIO_new_file(dhparams_file, "r"); 741 1.4 christos if (bio == NULL) { 742 1.6 christos return false; 743 1.4 christos } 744 1.4 christos 745 1.4 christos dh = PEM_read_bio_Parameters(bio, NULL); 746 1.4 christos if (dh == NULL) { 747 1.4 christos BIO_free(bio); 748 1.6 christos return false; 749 1.4 christos } 750 1.4 christos 751 1.4 christos if (SSL_CTX_set0_tmp_dh_pkey(ctx, dh) != 1) { 752 1.4 christos BIO_free(bio); 753 1.4 christos EVP_PKEY_free(dh); 754 1.6 christos return false; 755 1.4 christos } 756 1.4 christos 757 1.4 christos /* No need to call EVP_PKEY_free(dh) as the "dh" is owned by the 758 1.4 christos * SSL context at this point. */ 759 1.4 christos 760 1.4 christos BIO_free(bio); 761 1.4 christos #endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */ 762 1.4 christos 763 1.6 christos return true; 764 1.4 christos } 765 1.4 christos 766 1.4 christos bool 767 1.4 christos isc_tls_cipherlist_valid(const char *cipherlist) { 768 1.4 christos isc_tlsctx_t *tmp_ctx = NULL; 769 1.4 christos const SSL_METHOD *method = NULL; 770 1.4 christos bool result; 771 1.4 christos REQUIRE(cipherlist != NULL); 772 1.4 christos 773 1.4 christos if (*cipherlist == '\0') { 774 1.6 christos return false; 775 1.4 christos } 776 1.4 christos 777 1.4 christos method = TLS_server_method(); 778 1.4 christos if (method == NULL) { 779 1.6 christos return false; 780 1.4 christos } 781 1.4 christos tmp_ctx = SSL_CTX_new(method); 782 1.4 christos if (tmp_ctx == NULL) { 783 1.6 christos return false; 784 1.4 christos } 785 1.4 christos 786 1.4 christos result = SSL_CTX_set_cipher_list(tmp_ctx, cipherlist) == 1; 787 1.4 christos 788 1.4 christos isc_tlsctx_free(&tmp_ctx); 789 1.4 christos 790 1.6 christos return result; 791 1.4 christos } 792 1.4 christos 793 1.4 christos void 794 1.4 christos isc_tlsctx_set_cipherlist(isc_tlsctx_t *ctx, const char *cipherlist) { 795 1.4 christos REQUIRE(ctx != NULL); 796 1.4 christos REQUIRE(cipherlist != NULL); 797 1.4 christos REQUIRE(*cipherlist != '\0'); 798 1.4 christos 799 1.4 christos RUNTIME_CHECK(SSL_CTX_set_cipher_list(ctx, cipherlist) == 1); 800 1.4 christos } 801 1.4 christos 802 1.6 christos bool 803 1.6 christos isc_tls_cipher_suites_valid(const char *cipher_suites) { 804 1.6 christos #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES 805 1.6 christos isc_tlsctx_t *tmp_ctx = NULL; 806 1.6 christos const SSL_METHOD *method = NULL; 807 1.6 christos bool result; 808 1.6 christos REQUIRE(cipher_suites != NULL); 809 1.6 christos 810 1.6 christos if (*cipher_suites == '\0') { 811 1.6 christos return false; 812 1.6 christos } 813 1.6 christos 814 1.6 christos method = TLS_server_method(); 815 1.6 christos if (method == NULL) { 816 1.6 christos return false; 817 1.6 christos } 818 1.6 christos tmp_ctx = SSL_CTX_new(method); 819 1.6 christos if (tmp_ctx == NULL) { 820 1.6 christos return false; 821 1.6 christos } 822 1.6 christos 823 1.6 christos result = SSL_CTX_set_ciphersuites(tmp_ctx, cipher_suites) == 1; 824 1.6 christos 825 1.6 christos isc_tlsctx_free(&tmp_ctx); 826 1.6 christos 827 1.6 christos return result; 828 1.6 christos #else 829 1.6 christos UNUSED(cipher_suites); 830 1.6 christos 831 1.6 christos UNREACHABLE(); 832 1.6 christos #endif 833 1.6 christos } 834 1.6 christos 835 1.6 christos void 836 1.6 christos isc_tlsctx_set_cipher_suites(isc_tlsctx_t *ctx, const char *cipher_suites) { 837 1.6 christos #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES 838 1.6 christos REQUIRE(ctx != NULL); 839 1.6 christos REQUIRE(cipher_suites != NULL); 840 1.6 christos REQUIRE(*cipher_suites != '\0'); 841 1.6 christos 842 1.6 christos RUNTIME_CHECK(SSL_CTX_set_ciphersuites(ctx, cipher_suites) == 1); 843 1.6 christos #else 844 1.6 christos UNUSED(ctx); 845 1.6 christos UNUSED(cipher_suites); 846 1.6 christos 847 1.6 christos UNREACHABLE(); 848 1.6 christos #endif 849 1.6 christos } 850 1.6 christos 851 1.4 christos void 852 1.4 christos isc_tlsctx_prefer_server_ciphers(isc_tlsctx_t *ctx, const bool prefer) { 853 1.4 christos REQUIRE(ctx != NULL); 854 1.4 christos 855 1.4 christos if (prefer) { 856 1.4 christos (void)SSL_CTX_set_options(ctx, SSL_OP_CIPHER_SERVER_PREFERENCE); 857 1.4 christos } else { 858 1.4 christos (void)SSL_CTX_clear_options(ctx, 859 1.4 christos SSL_OP_CIPHER_SERVER_PREFERENCE); 860 1.4 christos } 861 1.4 christos } 862 1.4 christos 863 1.4 christos void 864 1.4 christos isc_tlsctx_session_tickets(isc_tlsctx_t *ctx, const bool use) { 865 1.4 christos REQUIRE(ctx != NULL); 866 1.4 christos 867 1.4 christos if (!use) { 868 1.4 christos (void)SSL_CTX_set_options(ctx, SSL_OP_NO_TICKET); 869 1.4 christos } else { 870 1.4 christos (void)SSL_CTX_clear_options(ctx, SSL_OP_NO_TICKET); 871 1.4 christos } 872 1.4 christos } 873 1.4 christos 874 1.4 christos isc_tls_t * 875 1.4 christos isc_tls_create(isc_tlsctx_t *ctx) { 876 1.4 christos isc_tls_t *newctx = NULL; 877 1.4 christos 878 1.4 christos REQUIRE(ctx != NULL); 879 1.4 christos 880 1.4 christos newctx = SSL_new(ctx); 881 1.4 christos if (newctx == NULL) { 882 1.4 christos char errbuf[256]; 883 1.4 christos unsigned long err = ERR_get_error(); 884 1.4 christos 885 1.4 christos ERR_error_string_n(err, errbuf, sizeof(errbuf)); 886 1.4 christos fprintf(stderr, "%s:SSL_new(%p) -> %s\n", __func__, ctx, 887 1.4 christos errbuf); 888 1.4 christos } 889 1.4 christos 890 1.6 christos return newctx; 891 1.4 christos } 892 1.4 christos 893 1.4 christos void 894 1.4 christos isc_tls_free(isc_tls_t **tlsp) { 895 1.4 christos isc_tls_t *tls = NULL; 896 1.4 christos REQUIRE(tlsp != NULL && *tlsp != NULL); 897 1.4 christos 898 1.4 christos tls = *tlsp; 899 1.4 christos *tlsp = NULL; 900 1.4 christos SSL_free(tls); 901 1.4 christos } 902 1.4 christos 903 1.4 christos const char * 904 1.4 christos isc_tls_verify_peer_result_string(isc_tls_t *tls) { 905 1.4 christos REQUIRE(tls != NULL); 906 1.4 christos 907 1.6 christos return X509_verify_cert_error_string(SSL_get_verify_result(tls)); 908 1.4 christos } 909 1.4 christos 910 1.4 christos #if HAVE_LIBNGHTTP2 911 1.4 christos #ifndef OPENSSL_NO_NEXTPROTONEG 912 1.4 christos /* 913 1.4 christos * NPN TLS extension client callback. 914 1.4 christos */ 915 1.4 christos static int 916 1.4 christos select_next_proto_cb(SSL *ssl, unsigned char **out, unsigned char *outlen, 917 1.4 christos const unsigned char *in, unsigned int inlen, void *arg) { 918 1.4 christos UNUSED(ssl); 919 1.4 christos UNUSED(arg); 920 1.4 christos 921 1.4 christos if (nghttp2_select_next_protocol(out, outlen, in, inlen) <= 0) { 922 1.6 christos return SSL_TLSEXT_ERR_NOACK; 923 1.4 christos } 924 1.6 christos return SSL_TLSEXT_ERR_OK; 925 1.4 christos } 926 1.4 christos #endif /* !OPENSSL_NO_NEXTPROTONEG */ 927 1.4 christos 928 1.4 christos void 929 1.4 christos isc_tlsctx_enable_http2client_alpn(isc_tlsctx_t *ctx) { 930 1.4 christos REQUIRE(ctx != NULL); 931 1.4 christos 932 1.4 christos #ifndef OPENSSL_NO_NEXTPROTONEG 933 1.4 christos SSL_CTX_set_next_proto_select_cb(ctx, select_next_proto_cb, NULL); 934 1.4 christos #endif /* !OPENSSL_NO_NEXTPROTONEG */ 935 1.4 christos 936 1.4 christos #if OPENSSL_VERSION_NUMBER >= 0x10002000L 937 1.4 christos SSL_CTX_set_alpn_protos(ctx, (const unsigned char *)NGHTTP2_PROTO_ALPN, 938 1.4 christos NGHTTP2_PROTO_ALPN_LEN); 939 1.4 christos #endif /* OPENSSL_VERSION_NUMBER >= 0x10002000L */ 940 1.4 christos } 941 1.4 christos 942 1.4 christos #ifndef OPENSSL_NO_NEXTPROTONEG 943 1.4 christos static int 944 1.4 christos next_proto_cb(isc_tls_t *ssl, const unsigned char **data, unsigned int *len, 945 1.4 christos void *arg) { 946 1.4 christos UNUSED(ssl); 947 1.4 christos UNUSED(arg); 948 1.4 christos 949 1.4 christos *data = (const unsigned char *)NGHTTP2_PROTO_ALPN; 950 1.4 christos *len = (unsigned int)NGHTTP2_PROTO_ALPN_LEN; 951 1.6 christos return SSL_TLSEXT_ERR_OK; 952 1.4 christos } 953 1.4 christos #endif /* !OPENSSL_NO_NEXTPROTONEG */ 954 1.4 christos 955 1.4 christos #if OPENSSL_VERSION_NUMBER >= 0x10002000L 956 1.4 christos static int 957 1.4 christos alpn_select_proto_cb(SSL *ssl, const unsigned char **out, unsigned char *outlen, 958 1.4 christos const unsigned char *in, unsigned int inlen, void *arg) { 959 1.4 christos int ret; 960 1.4 christos 961 1.4 christos UNUSED(ssl); 962 1.4 christos UNUSED(arg); 963 1.4 christos 964 1.4 christos ret = nghttp2_select_next_protocol((unsigned char **)(uintptr_t)out, 965 1.4 christos outlen, in, inlen); 966 1.4 christos 967 1.4 christos if (ret != 1) { 968 1.6 christos return SSL_TLSEXT_ERR_NOACK; 969 1.4 christos } 970 1.4 christos 971 1.6 christos return SSL_TLSEXT_ERR_OK; 972 1.4 christos } 973 1.4 christos #endif /* OPENSSL_VERSION_NUMBER >= 0x10002000L */ 974 1.4 christos 975 1.4 christos void 976 1.4 christos isc_tlsctx_enable_http2server_alpn(isc_tlsctx_t *tls) { 977 1.4 christos REQUIRE(tls != NULL); 978 1.4 christos 979 1.4 christos #ifndef OPENSSL_NO_NEXTPROTONEG 980 1.4 christos SSL_CTX_set_next_protos_advertised_cb(tls, next_proto_cb, NULL); 981 1.4 christos #endif // OPENSSL_NO_NEXTPROTONEG 982 1.4 christos #if OPENSSL_VERSION_NUMBER >= 0x10002000L 983 1.4 christos SSL_CTX_set_alpn_select_cb(tls, alpn_select_proto_cb, NULL); 984 1.4 christos #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L 985 1.4 christos } 986 1.4 christos #endif /* HAVE_LIBNGHTTP2 */ 987 1.4 christos 988 1.4 christos void 989 1.4 christos isc_tls_get_selected_alpn(isc_tls_t *tls, const unsigned char **alpn, 990 1.4 christos unsigned int *alpnlen) { 991 1.4 christos REQUIRE(tls != NULL); 992 1.4 christos REQUIRE(alpn != NULL); 993 1.4 christos REQUIRE(alpnlen != NULL); 994 1.4 christos 995 1.4 christos #ifndef OPENSSL_NO_NEXTPROTONEG 996 1.4 christos SSL_get0_next_proto_negotiated(tls, alpn, alpnlen); 997 1.4 christos #endif 998 1.4 christos #if OPENSSL_VERSION_NUMBER >= 0x10002000L 999 1.4 christos if (*alpn == NULL) { 1000 1.4 christos SSL_get0_alpn_selected(tls, alpn, alpnlen); 1001 1.4 christos } 1002 1.4 christos #endif 1003 1.4 christos } 1004 1.4 christos 1005 1.4 christos static bool 1006 1.4 christos protoneg_check_protocol(const uint8_t **pout, uint8_t *pout_len, 1007 1.4 christos const uint8_t *in, size_t in_len, const uint8_t *key, 1008 1.4 christos size_t key_len) { 1009 1.4 christos for (size_t i = 0; i + key_len <= in_len; i += (size_t)(in[i] + 1)) { 1010 1.4 christos if (memcmp(&in[i], key, key_len) == 0) { 1011 1.4 christos *pout = (const uint8_t *)(&in[i + 1]); 1012 1.4 christos *pout_len = in[i]; 1013 1.6 christos return true; 1014 1.4 christos } 1015 1.4 christos } 1016 1.6 christos return false; 1017 1.4 christos } 1018 1.4 christos 1019 1.4 christos /* dot prepended by its length (3 bytes) */ 1020 1.4 christos #define DOT_PROTO_ALPN "\x3" ISC_TLS_DOT_PROTO_ALPN_ID 1021 1.4 christos #define DOT_PROTO_ALPN_LEN (sizeof(DOT_PROTO_ALPN) - 1) 1022 1.4 christos 1023 1.4 christos static bool 1024 1.4 christos dot_select_next_protocol(const uint8_t **pout, uint8_t *pout_len, 1025 1.4 christos const uint8_t *in, size_t in_len) { 1026 1.6 christos return protoneg_check_protocol(pout, pout_len, in, in_len, 1027 1.6 christos (const uint8_t *)DOT_PROTO_ALPN, 1028 1.6 christos DOT_PROTO_ALPN_LEN); 1029 1.4 christos } 1030 1.4 christos 1031 1.4 christos void 1032 1.4 christos isc_tlsctx_enable_dot_client_alpn(isc_tlsctx_t *ctx) { 1033 1.4 christos REQUIRE(ctx != NULL); 1034 1.4 christos 1035 1.4 christos #if OPENSSL_VERSION_NUMBER >= 0x10002000L 1036 1.4 christos SSL_CTX_set_alpn_protos(ctx, (const uint8_t *)DOT_PROTO_ALPN, 1037 1.4 christos DOT_PROTO_ALPN_LEN); 1038 1.4 christos #endif /* OPENSSL_VERSION_NUMBER >= 0x10002000L */ 1039 1.4 christos } 1040 1.4 christos 1041 1.4 christos #if OPENSSL_VERSION_NUMBER >= 0x10002000L 1042 1.4 christos static int 1043 1.4 christos dot_alpn_select_proto_cb(SSL *ssl, const unsigned char **out, 1044 1.4 christos unsigned char *outlen, const unsigned char *in, 1045 1.4 christos unsigned int inlen, void *arg) { 1046 1.4 christos bool ret; 1047 1.4 christos 1048 1.4 christos UNUSED(ssl); 1049 1.4 christos UNUSED(arg); 1050 1.4 christos 1051 1.4 christos ret = dot_select_next_protocol(out, outlen, in, inlen); 1052 1.4 christos 1053 1.4 christos if (!ret) { 1054 1.6 christos return SSL_TLSEXT_ERR_NOACK; 1055 1.4 christos } 1056 1.4 christos 1057 1.6 christos return SSL_TLSEXT_ERR_OK; 1058 1.4 christos } 1059 1.4 christos #endif /* OPENSSL_VERSION_NUMBER >= 0x10002000L */ 1060 1.4 christos 1061 1.4 christos void 1062 1.4 christos isc_tlsctx_enable_dot_server_alpn(isc_tlsctx_t *tls) { 1063 1.4 christos REQUIRE(tls != NULL); 1064 1.4 christos 1065 1.4 christos #if OPENSSL_VERSION_NUMBER >= 0x10002000L 1066 1.4 christos SSL_CTX_set_alpn_select_cb(tls, dot_alpn_select_proto_cb, NULL); 1067 1.4 christos #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L 1068 1.4 christos } 1069 1.4 christos 1070 1.4 christos isc_result_t 1071 1.4 christos isc_tlsctx_enable_peer_verification(isc_tlsctx_t *tlsctx, const bool is_server, 1072 1.4 christos isc_tls_cert_store_t *store, 1073 1.4 christos const char *hostname, 1074 1.4 christos bool hostname_ignore_subject) { 1075 1.4 christos int ret = 0; 1076 1.4 christos REQUIRE(tlsctx != NULL); 1077 1.4 christos REQUIRE(store != NULL); 1078 1.4 christos 1079 1.4 christos /* Set the hostname/IP address. */ 1080 1.4 christos if (!is_server && hostname != NULL && *hostname != '\0') { 1081 1.4 christos struct in6_addr sa6; 1082 1.4 christos struct in_addr sa; 1083 1.4 christos X509_VERIFY_PARAM *param = SSL_CTX_get0_param(tlsctx); 1084 1.4 christos unsigned int hostflags = X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS; 1085 1.4 christos 1086 1.4 christos /* It might be an IP address. */ 1087 1.4 christos if (inet_pton(AF_INET6, hostname, &sa6) == 1 || 1088 1.4 christos inet_pton(AF_INET, hostname, &sa) == 1) 1089 1.4 christos { 1090 1.4 christos ret = X509_VERIFY_PARAM_set1_ip_asc(param, hostname); 1091 1.4 christos } else { 1092 1.4 christos /* It seems that it is a host name. Let's set it. */ 1093 1.4 christos ret = X509_VERIFY_PARAM_set1_host(param, hostname, 0); 1094 1.4 christos } 1095 1.4 christos if (ret != 1) { 1096 1.4 christos ERR_clear_error(); 1097 1.6 christos return ISC_R_FAILURE; 1098 1.4 christos } 1099 1.4 christos 1100 1.4 christos #ifdef X509_CHECK_FLAG_NEVER_CHECK_SUBJECT 1101 1.4 christos /* 1102 1.4 christos * According to the RFC 8310, Section 8.1, Subject field MUST 1103 1.4 christos * NOT be inspected when verifying a hostname when using 1104 1.4 christos * DoT. Only SubjectAltName must be checked instead. That is 1105 1.4 christos * not the case for HTTPS, though. 1106 1.4 christos * 1107 1.4 christos * Unfortunately, some quite old versions of OpenSSL (< 1.1.1) 1108 1.4 christos * might lack the functionality to implement that. It should 1109 1.4 christos * have very little real-world consequences, as most of the 1110 1.4 christos * production-ready certificates issued by real CAs will have 1111 1.4 christos * SubjectAltName set. In such a case, the Subject field is 1112 1.4 christos * ignored. 1113 1.4 christos */ 1114 1.4 christos if (hostname_ignore_subject) { 1115 1.4 christos hostflags |= X509_CHECK_FLAG_NEVER_CHECK_SUBJECT; 1116 1.4 christos } 1117 1.4 christos #else 1118 1.4 christos UNUSED(hostname_ignore_subject); 1119 1.4 christos #endif 1120 1.4 christos X509_VERIFY_PARAM_set_hostflags(param, hostflags); 1121 1.4 christos } 1122 1.4 christos 1123 1.4 christos /* "Attach" the cert store to the context */ 1124 1.4 christos SSL_CTX_set1_cert_store(tlsctx, store); 1125 1.4 christos 1126 1.4 christos /* enable verification */ 1127 1.4 christos if (is_server) { 1128 1.4 christos SSL_CTX_set_verify(tlsctx, 1129 1.4 christos SSL_VERIFY_PEER | 1130 1.4 christos SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 1131 1.4 christos NULL); 1132 1.4 christos } else { 1133 1.4 christos SSL_CTX_set_verify(tlsctx, SSL_VERIFY_PEER, NULL); 1134 1.4 christos } 1135 1.4 christos 1136 1.6 christos return ISC_R_SUCCESS; 1137 1.4 christos } 1138 1.4 christos 1139 1.4 christos isc_result_t 1140 1.4 christos isc_tlsctx_load_client_ca_names(isc_tlsctx_t *ctx, const char *ca_bundle_file) { 1141 1.4 christos STACK_OF(X509_NAME) * cert_names; 1142 1.4 christos REQUIRE(ctx != NULL); 1143 1.4 christos REQUIRE(ca_bundle_file != NULL); 1144 1.4 christos 1145 1.4 christos cert_names = SSL_load_client_CA_file(ca_bundle_file); 1146 1.4 christos if (cert_names == NULL) { 1147 1.4 christos ERR_clear_error(); 1148 1.6 christos return ISC_R_FAILURE; 1149 1.4 christos } 1150 1.4 christos 1151 1.4 christos SSL_CTX_set_client_CA_list(ctx, cert_names); 1152 1.4 christos 1153 1.6 christos return ISC_R_SUCCESS; 1154 1.4 christos } 1155 1.4 christos 1156 1.4 christos isc_result_t 1157 1.4 christos isc_tls_cert_store_create(const char *ca_bundle_filename, 1158 1.4 christos isc_tls_cert_store_t **pstore) { 1159 1.4 christos int ret = 0; 1160 1.4 christos isc_tls_cert_store_t *store = NULL; 1161 1.4 christos REQUIRE(pstore != NULL && *pstore == NULL); 1162 1.4 christos 1163 1.4 christos store = X509_STORE_new(); 1164 1.4 christos if (store == NULL) { 1165 1.4 christos goto error; 1166 1.4 christos } 1167 1.4 christos 1168 1.4 christos /* Let's treat empty string as the default (system wide) store */ 1169 1.4 christos if (ca_bundle_filename != NULL && *ca_bundle_filename == '\0') { 1170 1.4 christos ca_bundle_filename = NULL; 1171 1.4 christos } 1172 1.4 christos 1173 1.4 christos if (ca_bundle_filename == NULL) { 1174 1.4 christos ret = X509_STORE_set_default_paths(store); 1175 1.4 christos } else { 1176 1.4 christos ret = X509_STORE_load_locations(store, ca_bundle_filename, 1177 1.4 christos NULL); 1178 1.4 christos } 1179 1.4 christos 1180 1.4 christos if (ret == 0) { 1181 1.4 christos goto error; 1182 1.4 christos } 1183 1.4 christos 1184 1.4 christos *pstore = store; 1185 1.6 christos return ISC_R_SUCCESS; 1186 1.4 christos 1187 1.4 christos error: 1188 1.4 christos ERR_clear_error(); 1189 1.4 christos if (store != NULL) { 1190 1.4 christos X509_STORE_free(store); 1191 1.4 christos } 1192 1.6 christos return ISC_R_FAILURE; 1193 1.4 christos } 1194 1.4 christos 1195 1.4 christos void 1196 1.4 christos isc_tls_cert_store_free(isc_tls_cert_store_t **pstore) { 1197 1.4 christos isc_tls_cert_store_t *store; 1198 1.4 christos REQUIRE(pstore != NULL && *pstore != NULL); 1199 1.4 christos 1200 1.4 christos store = *pstore; 1201 1.4 christos 1202 1.4 christos X509_STORE_free(store); 1203 1.4 christos 1204 1.4 christos *pstore = NULL; 1205 1.4 christos } 1206 1.4 christos 1207 1.4 christos #define TLSCTX_CACHE_MAGIC ISC_MAGIC('T', 'l', 'S', 'c') 1208 1.4 christos #define VALID_TLSCTX_CACHE(t) ISC_MAGIC_VALID(t, TLSCTX_CACHE_MAGIC) 1209 1.4 christos 1210 1.4 christos #define TLSCTX_CLIENT_SESSION_CACHE_MAGIC ISC_MAGIC('T', 'l', 'C', 'c') 1211 1.4 christos #define VALID_TLSCTX_CLIENT_SESSION_CACHE(t) \ 1212 1.4 christos ISC_MAGIC_VALID(t, TLSCTX_CLIENT_SESSION_CACHE_MAGIC) 1213 1.4 christos 1214 1.4 christos typedef struct isc_tlsctx_cache_entry { 1215 1.4 christos /* 1216 1.4 christos * We need a TLS context entry for each transport on both IPv4 and 1217 1.4 christos * IPv6 in order to avoid cluttering a context-specific 1218 1.4 christos * session-resumption cache. 1219 1.4 christos */ 1220 1.4 christos isc_tlsctx_t *ctx[isc_tlsctx_cache_count - 1][2]; 1221 1.4 christos isc_tlsctx_client_session_cache_t 1222 1.4 christos *client_sess_cache[isc_tlsctx_cache_count - 1][2]; 1223 1.4 christos /* 1224 1.4 christos * One certificate store is enough for all the contexts defined 1225 1.4 christos * above. We need that for peer validation. 1226 1.4 christos */ 1227 1.4 christos isc_tls_cert_store_t *ca_store; 1228 1.4 christos } isc_tlsctx_cache_entry_t; 1229 1.4 christos 1230 1.4 christos struct isc_tlsctx_cache { 1231 1.4 christos uint32_t magic; 1232 1.4 christos isc_refcount_t references; 1233 1.4 christos isc_mem_t *mctx; 1234 1.4 christos 1235 1.4 christos isc_rwlock_t rwlock; 1236 1.4 christos isc_ht_t *data; 1237 1.4 christos }; 1238 1.4 christos 1239 1.4 christos void 1240 1.4 christos isc_tlsctx_cache_create(isc_mem_t *mctx, isc_tlsctx_cache_t **cachep) { 1241 1.4 christos isc_tlsctx_cache_t *nc; 1242 1.4 christos 1243 1.4 christos REQUIRE(cachep != NULL && *cachep == NULL); 1244 1.4 christos nc = isc_mem_get(mctx, sizeof(*nc)); 1245 1.4 christos 1246 1.4 christos *nc = (isc_tlsctx_cache_t){ .magic = TLSCTX_CACHE_MAGIC }; 1247 1.4 christos isc_refcount_init(&nc->references, 1); 1248 1.4 christos isc_mem_attach(mctx, &nc->mctx); 1249 1.4 christos 1250 1.4 christos isc_ht_init(&nc->data, mctx, 5, ISC_HT_CASE_SENSITIVE); 1251 1.6 christos isc_rwlock_init(&nc->rwlock); 1252 1.4 christos 1253 1.4 christos *cachep = nc; 1254 1.4 christos } 1255 1.4 christos 1256 1.4 christos void 1257 1.4 christos isc_tlsctx_cache_attach(isc_tlsctx_cache_t *source, 1258 1.4 christos isc_tlsctx_cache_t **targetp) { 1259 1.4 christos REQUIRE(VALID_TLSCTX_CACHE(source)); 1260 1.4 christos REQUIRE(targetp != NULL && *targetp == NULL); 1261 1.4 christos 1262 1.4 christos isc_refcount_increment(&source->references); 1263 1.4 christos 1264 1.4 christos *targetp = source; 1265 1.4 christos } 1266 1.4 christos 1267 1.4 christos static void 1268 1.4 christos tlsctx_cache_entry_destroy(isc_mem_t *mctx, isc_tlsctx_cache_entry_t *entry) { 1269 1.4 christos size_t i, k; 1270 1.4 christos 1271 1.4 christos for (i = 0; i < (isc_tlsctx_cache_count - 1); i++) { 1272 1.4 christos for (k = 0; k < 2; k++) { 1273 1.4 christos if (entry->ctx[i][k] != NULL) { 1274 1.4 christos isc_tlsctx_free(&entry->ctx[i][k]); 1275 1.4 christos } 1276 1.4 christos 1277 1.4 christos if (entry->client_sess_cache[i][k] != NULL) { 1278 1.4 christos isc_tlsctx_client_session_cache_detach( 1279 1.4 christos &entry->client_sess_cache[i][k]); 1280 1.4 christos } 1281 1.4 christos } 1282 1.4 christos } 1283 1.4 christos if (entry->ca_store != NULL) { 1284 1.4 christos isc_tls_cert_store_free(&entry->ca_store); 1285 1.4 christos } 1286 1.4 christos isc_mem_put(mctx, entry, sizeof(*entry)); 1287 1.4 christos } 1288 1.4 christos 1289 1.4 christos static void 1290 1.4 christos tlsctx_cache_destroy(isc_tlsctx_cache_t *cache) { 1291 1.4 christos isc_ht_iter_t *it = NULL; 1292 1.4 christos isc_result_t result; 1293 1.4 christos 1294 1.4 christos cache->magic = 0; 1295 1.4 christos 1296 1.4 christos isc_refcount_destroy(&cache->references); 1297 1.4 christos 1298 1.4 christos isc_ht_iter_create(cache->data, &it); 1299 1.4 christos for (result = isc_ht_iter_first(it); result == ISC_R_SUCCESS; 1300 1.4 christos result = isc_ht_iter_delcurrent_next(it)) 1301 1.4 christos { 1302 1.4 christos isc_tlsctx_cache_entry_t *entry = NULL; 1303 1.4 christos isc_ht_iter_current(it, (void **)&entry); 1304 1.4 christos tlsctx_cache_entry_destroy(cache->mctx, entry); 1305 1.4 christos } 1306 1.4 christos 1307 1.4 christos isc_ht_iter_destroy(&it); 1308 1.4 christos isc_ht_destroy(&cache->data); 1309 1.4 christos isc_rwlock_destroy(&cache->rwlock); 1310 1.4 christos isc_mem_putanddetach(&cache->mctx, cache, sizeof(*cache)); 1311 1.4 christos } 1312 1.4 christos 1313 1.4 christos void 1314 1.4 christos isc_tlsctx_cache_detach(isc_tlsctx_cache_t **cachep) { 1315 1.4 christos isc_tlsctx_cache_t *cache = NULL; 1316 1.4 christos 1317 1.4 christos REQUIRE(cachep != NULL); 1318 1.4 christos 1319 1.4 christos cache = *cachep; 1320 1.4 christos *cachep = NULL; 1321 1.4 christos 1322 1.4 christos REQUIRE(VALID_TLSCTX_CACHE(cache)); 1323 1.4 christos 1324 1.4 christos if (isc_refcount_decrement(&cache->references) == 1) { 1325 1.4 christos tlsctx_cache_destroy(cache); 1326 1.4 christos } 1327 1.4 christos } 1328 1.4 christos 1329 1.4 christos isc_result_t 1330 1.4 christos isc_tlsctx_cache_add( 1331 1.4 christos isc_tlsctx_cache_t *cache, const char *name, 1332 1.4 christos const isc_tlsctx_cache_transport_t transport, const uint16_t family, 1333 1.4 christos isc_tlsctx_t *ctx, isc_tls_cert_store_t *store, 1334 1.4 christos isc_tlsctx_client_session_cache_t *client_sess_cache, 1335 1.4 christos isc_tlsctx_t **pfound, isc_tls_cert_store_t **pfound_store, 1336 1.4 christos isc_tlsctx_client_session_cache_t **pfound_client_sess_cache) { 1337 1.4 christos isc_result_t result = ISC_R_FAILURE; 1338 1.4 christos size_t name_len, tr_offset; 1339 1.4 christos isc_tlsctx_cache_entry_t *entry = NULL; 1340 1.4 christos bool ipv6; 1341 1.4 christos 1342 1.4 christos REQUIRE(VALID_TLSCTX_CACHE(cache)); 1343 1.4 christos REQUIRE(client_sess_cache == NULL || 1344 1.4 christos VALID_TLSCTX_CLIENT_SESSION_CACHE(client_sess_cache)); 1345 1.4 christos REQUIRE(name != NULL && *name != '\0'); 1346 1.4 christos REQUIRE(transport > isc_tlsctx_cache_none && 1347 1.4 christos transport < isc_tlsctx_cache_count); 1348 1.4 christos REQUIRE(family == AF_INET || family == AF_INET6); 1349 1.4 christos REQUIRE(ctx != NULL); 1350 1.4 christos 1351 1.4 christos tr_offset = (transport - 1); 1352 1.4 christos ipv6 = (family == AF_INET6); 1353 1.4 christos 1354 1.4 christos RWLOCK(&cache->rwlock, isc_rwlocktype_write); 1355 1.4 christos 1356 1.4 christos name_len = strlen(name); 1357 1.4 christos result = isc_ht_find(cache->data, (const uint8_t *)name, name_len, 1358 1.4 christos (void **)&entry); 1359 1.4 christos if (result == ISC_R_SUCCESS && entry->ctx[tr_offset][ipv6] != NULL) { 1360 1.4 christos isc_tlsctx_client_session_cache_t *found_client_sess_cache; 1361 1.4 christos /* The entry exists. */ 1362 1.4 christos if (pfound != NULL) { 1363 1.4 christos INSIST(*pfound == NULL); 1364 1.4 christos *pfound = entry->ctx[tr_offset][ipv6]; 1365 1.4 christos } 1366 1.4 christos 1367 1.4 christos if (pfound_store != NULL && entry->ca_store != NULL) { 1368 1.4 christos INSIST(*pfound_store == NULL); 1369 1.4 christos *pfound_store = entry->ca_store; 1370 1.4 christos } 1371 1.4 christos 1372 1.4 christos found_client_sess_cache = 1373 1.4 christos entry->client_sess_cache[tr_offset][ipv6]; 1374 1.4 christos if (pfound_client_sess_cache != NULL && 1375 1.4 christos found_client_sess_cache != NULL) 1376 1.4 christos { 1377 1.4 christos INSIST(*pfound_client_sess_cache == NULL); 1378 1.4 christos *pfound_client_sess_cache = found_client_sess_cache; 1379 1.4 christos } 1380 1.4 christos result = ISC_R_EXISTS; 1381 1.4 christos } else if (result == ISC_R_SUCCESS && 1382 1.4 christos entry->ctx[tr_offset][ipv6] == NULL) 1383 1.4 christos { 1384 1.4 christos /* 1385 1.4 christos * The hash table entry exists, but is not filled for this 1386 1.4 christos * particular transport/IP type combination. 1387 1.4 christos */ 1388 1.4 christos entry->ctx[tr_offset][ipv6] = ctx; 1389 1.4 christos entry->client_sess_cache[tr_offset][ipv6] = client_sess_cache; 1390 1.4 christos /* 1391 1.4 christos * As the passed certificates store object is supposed 1392 1.4 christos * to be internally managed by the cache object anyway, 1393 1.4 christos * we might destroy the unneeded store object right now. 1394 1.4 christos */ 1395 1.4 christos if (store != NULL && store != entry->ca_store) { 1396 1.4 christos isc_tls_cert_store_free(&store); 1397 1.4 christos } 1398 1.4 christos result = ISC_R_SUCCESS; 1399 1.4 christos } else { 1400 1.4 christos /* 1401 1.4 christos * The hash table entry does not exist, let's create one. 1402 1.4 christos */ 1403 1.4 christos INSIST(result != ISC_R_SUCCESS); 1404 1.4 christos entry = isc_mem_get(cache->mctx, sizeof(*entry)); 1405 1.6 christos *entry = (isc_tlsctx_cache_entry_t){ 1406 1.6 christos .ca_store = store, 1407 1.6 christos }; 1408 1.6 christos 1409 1.4 christos entry->ctx[tr_offset][ipv6] = ctx; 1410 1.4 christos entry->client_sess_cache[tr_offset][ipv6] = client_sess_cache; 1411 1.4 christos RUNTIME_CHECK(isc_ht_add(cache->data, (const uint8_t *)name, 1412 1.4 christos name_len, 1413 1.4 christos (void *)entry) == ISC_R_SUCCESS); 1414 1.4 christos result = ISC_R_SUCCESS; 1415 1.4 christos } 1416 1.4 christos 1417 1.4 christos RWUNLOCK(&cache->rwlock, isc_rwlocktype_write); 1418 1.4 christos 1419 1.6 christos return result; 1420 1.4 christos } 1421 1.4 christos 1422 1.4 christos isc_result_t 1423 1.4 christos isc_tlsctx_cache_find( 1424 1.4 christos isc_tlsctx_cache_t *cache, const char *name, 1425 1.4 christos const isc_tlsctx_cache_transport_t transport, const uint16_t family, 1426 1.4 christos isc_tlsctx_t **pctx, isc_tls_cert_store_t **pstore, 1427 1.4 christos isc_tlsctx_client_session_cache_t **pfound_client_sess_cache) { 1428 1.4 christos isc_result_t result = ISC_R_FAILURE; 1429 1.4 christos size_t tr_offset; 1430 1.4 christos isc_tlsctx_cache_entry_t *entry = NULL; 1431 1.4 christos bool ipv6; 1432 1.4 christos 1433 1.4 christos REQUIRE(VALID_TLSCTX_CACHE(cache)); 1434 1.4 christos REQUIRE(name != NULL && *name != '\0'); 1435 1.4 christos REQUIRE(transport > isc_tlsctx_cache_none && 1436 1.4 christos transport < isc_tlsctx_cache_count); 1437 1.4 christos REQUIRE(family == AF_INET || family == AF_INET6); 1438 1.4 christos REQUIRE(pctx != NULL && *pctx == NULL); 1439 1.4 christos 1440 1.4 christos tr_offset = (transport - 1); 1441 1.4 christos ipv6 = (family == AF_INET6); 1442 1.4 christos 1443 1.4 christos RWLOCK(&cache->rwlock, isc_rwlocktype_read); 1444 1.4 christos 1445 1.4 christos result = isc_ht_find(cache->data, (const uint8_t *)name, strlen(name), 1446 1.4 christos (void **)&entry); 1447 1.4 christos 1448 1.4 christos if (result == ISC_R_SUCCESS && pstore != NULL && 1449 1.4 christos entry->ca_store != NULL) 1450 1.4 christos { 1451 1.4 christos *pstore = entry->ca_store; 1452 1.4 christos } 1453 1.4 christos 1454 1.4 christos if (result == ISC_R_SUCCESS && entry->ctx[tr_offset][ipv6] != NULL) { 1455 1.4 christos isc_tlsctx_client_session_cache_t *found_client_sess_cache = 1456 1.4 christos entry->client_sess_cache[tr_offset][ipv6]; 1457 1.4 christos 1458 1.4 christos *pctx = entry->ctx[tr_offset][ipv6]; 1459 1.4 christos 1460 1.4 christos if (pfound_client_sess_cache != NULL && 1461 1.4 christos found_client_sess_cache != NULL) 1462 1.4 christos { 1463 1.4 christos INSIST(*pfound_client_sess_cache == NULL); 1464 1.4 christos *pfound_client_sess_cache = found_client_sess_cache; 1465 1.4 christos } 1466 1.4 christos } else if (result == ISC_R_SUCCESS && 1467 1.4 christos entry->ctx[tr_offset][ipv6] == NULL) 1468 1.4 christos { 1469 1.4 christos result = ISC_R_NOTFOUND; 1470 1.4 christos } else { 1471 1.4 christos INSIST(result != ISC_R_SUCCESS); 1472 1.4 christos } 1473 1.4 christos 1474 1.4 christos RWUNLOCK(&cache->rwlock, isc_rwlocktype_read); 1475 1.4 christos 1476 1.6 christos return result; 1477 1.4 christos } 1478 1.4 christos 1479 1.4 christos typedef struct client_session_cache_entry client_session_cache_entry_t; 1480 1.4 christos 1481 1.4 christos typedef struct client_session_cache_bucket { 1482 1.4 christos char *bucket_key; 1483 1.4 christos size_t bucket_key_len; 1484 1.4 christos /* Cache entries within the bucket (from the oldest to the newest). */ 1485 1.4 christos ISC_LIST(client_session_cache_entry_t) entries; 1486 1.4 christos } client_session_cache_bucket_t; 1487 1.4 christos 1488 1.4 christos struct client_session_cache_entry { 1489 1.4 christos SSL_SESSION *session; 1490 1.4 christos client_session_cache_bucket_t *bucket; /* "Parent" bucket pointer. */ 1491 1.4 christos ISC_LINK(client_session_cache_entry_t) bucket_link; 1492 1.4 christos ISC_LINK(client_session_cache_entry_t) cache_link; 1493 1.4 christos }; 1494 1.4 christos 1495 1.4 christos struct isc_tlsctx_client_session_cache { 1496 1.4 christos uint32_t magic; 1497 1.4 christos isc_refcount_t references; 1498 1.4 christos isc_mem_t *mctx; 1499 1.4 christos 1500 1.4 christos /* 1501 1.4 christos * We need to keep a reference to the related TLS context in order 1502 1.4 christos * to ensure that it remains valid while the TLS client sessions 1503 1.4 christos * cache object is valid, as every TLS session object 1504 1.4 christos * (SSL_SESSION) is "tied" to a particular context. 1505 1.4 christos */ 1506 1.4 christos isc_tlsctx_t *ctx; 1507 1.4 christos 1508 1.4 christos /* 1509 1.4 christos * The idea is to have one bucket per remote server. Each bucket, 1510 1.4 christos * can maintain multiple TLS sessions to that server, as BIND 1511 1.4 christos * might want to establish multiple TLS connections to the remote 1512 1.4 christos * server at once. 1513 1.4 christos */ 1514 1.4 christos isc_ht_t *buckets; 1515 1.4 christos 1516 1.4 christos /* 1517 1.4 christos * The list of all current entries within the cache maintained in 1518 1.4 christos * LRU-manner, so that the oldest entry might be efficiently 1519 1.4 christos * removed. 1520 1.4 christos */ 1521 1.4 christos ISC_LIST(client_session_cache_entry_t) lru_entries; 1522 1.4 christos /* Number of the entries within the cache. */ 1523 1.4 christos size_t nentries; 1524 1.4 christos /* Maximum number of the entries within the cache. */ 1525 1.4 christos size_t max_entries; 1526 1.4 christos 1527 1.4 christos isc_mutex_t lock; 1528 1.4 christos }; 1529 1.4 christos 1530 1.4 christos void 1531 1.4 christos isc_tlsctx_client_session_cache_create( 1532 1.4 christos isc_mem_t *mctx, isc_tlsctx_t *ctx, const size_t max_entries, 1533 1.4 christos isc_tlsctx_client_session_cache_t **cachep) { 1534 1.4 christos isc_tlsctx_client_session_cache_t *nc; 1535 1.4 christos 1536 1.4 christos REQUIRE(ctx != NULL); 1537 1.4 christos REQUIRE(max_entries > 0); 1538 1.4 christos REQUIRE(cachep != NULL && *cachep == NULL); 1539 1.4 christos 1540 1.4 christos nc = isc_mem_get(mctx, sizeof(*nc)); 1541 1.4 christos 1542 1.4 christos *nc = (isc_tlsctx_client_session_cache_t){ .max_entries = max_entries }; 1543 1.4 christos isc_refcount_init(&nc->references, 1); 1544 1.4 christos isc_mem_attach(mctx, &nc->mctx); 1545 1.4 christos isc_tlsctx_attach(ctx, &nc->ctx); 1546 1.4 christos 1547 1.4 christos isc_ht_init(&nc->buckets, mctx, 5, ISC_HT_CASE_SENSITIVE); 1548 1.4 christos ISC_LIST_INIT(nc->lru_entries); 1549 1.4 christos isc_mutex_init(&nc->lock); 1550 1.4 christos 1551 1.4 christos nc->magic = TLSCTX_CLIENT_SESSION_CACHE_MAGIC; 1552 1.4 christos 1553 1.4 christos *cachep = nc; 1554 1.4 christos } 1555 1.4 christos 1556 1.4 christos void 1557 1.4 christos isc_tlsctx_client_session_cache_attach( 1558 1.4 christos isc_tlsctx_client_session_cache_t *source, 1559 1.4 christos isc_tlsctx_client_session_cache_t **targetp) { 1560 1.4 christos REQUIRE(VALID_TLSCTX_CLIENT_SESSION_CACHE(source)); 1561 1.4 christos REQUIRE(targetp != NULL && *targetp == NULL); 1562 1.4 christos 1563 1.4 christos isc_refcount_increment(&source->references); 1564 1.4 christos 1565 1.4 christos *targetp = source; 1566 1.4 christos } 1567 1.4 christos 1568 1.4 christos static void 1569 1.4 christos client_cache_entry_delete(isc_tlsctx_client_session_cache_t *restrict cache, 1570 1.4 christos client_session_cache_entry_t *restrict entry) { 1571 1.4 christos client_session_cache_bucket_t *restrict bucket = entry->bucket; 1572 1.4 christos 1573 1.4 christos /* Unlink and free the cache entry */ 1574 1.4 christos ISC_LIST_UNLINK(bucket->entries, entry, bucket_link); 1575 1.4 christos ISC_LIST_UNLINK(cache->lru_entries, entry, cache_link); 1576 1.4 christos cache->nentries--; 1577 1.4 christos (void)SSL_SESSION_free(entry->session); 1578 1.4 christos isc_mem_put(cache->mctx, entry, sizeof(*entry)); 1579 1.4 christos 1580 1.4 christos /* The bucket is empty - let's remove it */ 1581 1.4 christos if (ISC_LIST_EMPTY(bucket->entries)) { 1582 1.4 christos RUNTIME_CHECK(isc_ht_delete(cache->buckets, 1583 1.4 christos (const uint8_t *)bucket->bucket_key, 1584 1.4 christos bucket->bucket_key_len) == 1585 1.4 christos ISC_R_SUCCESS); 1586 1.4 christos 1587 1.4 christos isc_mem_free(cache->mctx, bucket->bucket_key); 1588 1.4 christos isc_mem_put(cache->mctx, bucket, sizeof(*bucket)); 1589 1.4 christos } 1590 1.4 christos } 1591 1.4 christos 1592 1.4 christos void 1593 1.4 christos isc_tlsctx_client_session_cache_detach( 1594 1.4 christos isc_tlsctx_client_session_cache_t **cachep) { 1595 1.4 christos isc_tlsctx_client_session_cache_t *cache = NULL; 1596 1.4 christos client_session_cache_entry_t *entry = NULL, *next = NULL; 1597 1.4 christos 1598 1.4 christos REQUIRE(cachep != NULL); 1599 1.4 christos 1600 1.4 christos cache = *cachep; 1601 1.4 christos *cachep = NULL; 1602 1.4 christos 1603 1.4 christos REQUIRE(VALID_TLSCTX_CLIENT_SESSION_CACHE(cache)); 1604 1.4 christos 1605 1.4 christos if (isc_refcount_decrement(&cache->references) != 1) { 1606 1.4 christos return; 1607 1.4 christos } 1608 1.4 christos 1609 1.4 christos cache->magic = 0; 1610 1.4 christos 1611 1.4 christos isc_refcount_destroy(&cache->references); 1612 1.4 christos 1613 1.4 christos entry = ISC_LIST_HEAD(cache->lru_entries); 1614 1.4 christos while (entry != NULL) { 1615 1.4 christos next = ISC_LIST_NEXT(entry, cache_link); 1616 1.4 christos client_cache_entry_delete(cache, entry); 1617 1.4 christos entry = next; 1618 1.4 christos } 1619 1.4 christos 1620 1.4 christos RUNTIME_CHECK(isc_ht_count(cache->buckets) == 0); 1621 1.4 christos isc_ht_destroy(&cache->buckets); 1622 1.4 christos 1623 1.4 christos isc_mutex_destroy(&cache->lock); 1624 1.4 christos isc_tlsctx_free(&cache->ctx); 1625 1.4 christos isc_mem_putanddetach(&cache->mctx, cache, sizeof(*cache)); 1626 1.4 christos } 1627 1.4 christos 1628 1.4 christos static bool 1629 1.4 christos ssl_session_seems_resumable(const SSL_SESSION *sess) { 1630 1.4 christos #ifdef HAVE_SSL_SESSION_IS_RESUMABLE 1631 1.4 christos /* 1632 1.4 christos * If SSL_SESSION_is_resumable() is available, let's use that. It 1633 1.4 christos * is expected to be available on OpenSSL >= 1.1.1 and its modern 1634 1.4 christos * siblings. 1635 1.4 christos */ 1636 1.6 christos return SSL_SESSION_is_resumable(sess) != 0; 1637 1.4 christos #elif (OPENSSL_VERSION_NUMBER >= 0x10100000L) 1638 1.4 christos /* 1639 1.4 christos * Taking into consideration that OpenSSL 1.1.0 uses opaque 1640 1.4 christos * pointers for SSL_SESSION, we cannot implement a replacement for 1641 1.4 christos * SSL_SESSION_is_resumable() manually. Let's use a sensible 1642 1.4 christos * approximation for that, then: if there is an associated session 1643 1.4 christos * ticket or session ID, then, most likely, the session is 1644 1.4 christos * resumable. 1645 1.4 christos */ 1646 1.4 christos unsigned int session_id_len = 0; 1647 1.4 christos (void)SSL_SESSION_get_id(sess, &session_id_len); 1648 1.6 christos return SSL_SESSION_has_ticket(sess) || session_id_len > 0; 1649 1.4 christos #else 1650 1.6 christos return !sess->not_resumable && 1651 1.6 christos (sess->session_id_length > 0 || sess->tlsext_ticklen > 0); 1652 1.4 christos #endif 1653 1.4 christos } 1654 1.4 christos 1655 1.4 christos void 1656 1.4 christos isc_tlsctx_client_session_cache_keep(isc_tlsctx_client_session_cache_t *cache, 1657 1.4 christos char *remote_peer_name, isc_tls_t *tls) { 1658 1.4 christos size_t name_len; 1659 1.4 christos isc_result_t result; 1660 1.4 christos SSL_SESSION *sess; 1661 1.4 christos client_session_cache_bucket_t *restrict bucket = NULL; 1662 1.4 christos client_session_cache_entry_t *restrict entry = NULL; 1663 1.4 christos 1664 1.4 christos REQUIRE(VALID_TLSCTX_CLIENT_SESSION_CACHE(cache)); 1665 1.4 christos REQUIRE(remote_peer_name != NULL && *remote_peer_name != '\0'); 1666 1.4 christos REQUIRE(tls != NULL); 1667 1.4 christos 1668 1.4 christos sess = SSL_get1_session(tls); 1669 1.4 christos if (sess == NULL) { 1670 1.4 christos ERR_clear_error(); 1671 1.4 christos return; 1672 1.4 christos } else if (!ssl_session_seems_resumable(sess)) { 1673 1.4 christos SSL_SESSION_free(sess); 1674 1.4 christos return; 1675 1.4 christos } 1676 1.4 christos 1677 1.5 christos SSL_set_session(tls, NULL); 1678 1.5 christos 1679 1.4 christos isc_mutex_lock(&cache->lock); 1680 1.4 christos 1681 1.4 christos name_len = strlen(remote_peer_name); 1682 1.4 christos result = isc_ht_find(cache->buckets, (const uint8_t *)remote_peer_name, 1683 1.4 christos name_len, (void **)&bucket); 1684 1.4 christos 1685 1.4 christos if (result != ISC_R_SUCCESS) { 1686 1.4 christos /* Let's create a new bucket */ 1687 1.4 christos INSIST(bucket == NULL); 1688 1.4 christos bucket = isc_mem_get(cache->mctx, sizeof(*bucket)); 1689 1.4 christos *bucket = (client_session_cache_bucket_t){ 1690 1.4 christos .bucket_key = isc_mem_strdup(cache->mctx, 1691 1.4 christos remote_peer_name), 1692 1.4 christos .bucket_key_len = name_len 1693 1.4 christos }; 1694 1.4 christos ISC_LIST_INIT(bucket->entries); 1695 1.4 christos RUNTIME_CHECK(isc_ht_add(cache->buckets, 1696 1.4 christos (const uint8_t *)remote_peer_name, 1697 1.4 christos name_len, 1698 1.4 christos (void *)bucket) == ISC_R_SUCCESS); 1699 1.4 christos } 1700 1.4 christos 1701 1.4 christos /* Let's add a new cache entry to the new/found bucket */ 1702 1.4 christos entry = isc_mem_get(cache->mctx, sizeof(*entry)); 1703 1.4 christos *entry = (client_session_cache_entry_t){ .session = sess, 1704 1.4 christos .bucket = bucket }; 1705 1.4 christos ISC_LINK_INIT(entry, bucket_link); 1706 1.4 christos ISC_LINK_INIT(entry, cache_link); 1707 1.4 christos 1708 1.4 christos ISC_LIST_APPEND(bucket->entries, entry, bucket_link); 1709 1.4 christos 1710 1.4 christos ISC_LIST_APPEND(cache->lru_entries, entry, cache_link); 1711 1.4 christos cache->nentries++; 1712 1.4 christos 1713 1.4 christos if (cache->nentries > cache->max_entries) { 1714 1.4 christos /* 1715 1.4 christos * Cache overrun. We need to remove the oldest entry from the 1716 1.4 christos * cache 1717 1.4 christos */ 1718 1.4 christos client_session_cache_entry_t *restrict oldest; 1719 1.4 christos INSIST((cache->nentries - 1) == cache->max_entries); 1720 1.4 christos 1721 1.4 christos oldest = ISC_LIST_HEAD(cache->lru_entries); 1722 1.4 christos client_cache_entry_delete(cache, oldest); 1723 1.4 christos } 1724 1.4 christos 1725 1.4 christos isc_mutex_unlock(&cache->lock); 1726 1.4 christos } 1727 1.4 christos 1728 1.4 christos void 1729 1.4 christos isc_tlsctx_client_session_cache_reuse(isc_tlsctx_client_session_cache_t *cache, 1730 1.4 christos char *remote_peer_name, isc_tls_t *tls) { 1731 1.4 christos client_session_cache_bucket_t *restrict bucket = NULL; 1732 1.4 christos client_session_cache_entry_t *restrict entry; 1733 1.4 christos size_t name_len; 1734 1.4 christos isc_result_t result; 1735 1.4 christos 1736 1.4 christos REQUIRE(VALID_TLSCTX_CLIENT_SESSION_CACHE(cache)); 1737 1.4 christos REQUIRE(remote_peer_name != NULL && *remote_peer_name != '\0'); 1738 1.4 christos REQUIRE(tls != NULL); 1739 1.4 christos 1740 1.4 christos isc_mutex_lock(&cache->lock); 1741 1.4 christos 1742 1.4 christos /* Let's find the bucket */ 1743 1.4 christos name_len = strlen(remote_peer_name); 1744 1.4 christos result = isc_ht_find(cache->buckets, (const uint8_t *)remote_peer_name, 1745 1.4 christos name_len, (void **)&bucket); 1746 1.4 christos 1747 1.4 christos if (result != ISC_R_SUCCESS) { 1748 1.4 christos goto exit; 1749 1.4 christos } 1750 1.4 christos 1751 1.4 christos INSIST(bucket != NULL); 1752 1.4 christos 1753 1.4 christos /* 1754 1.4 christos * If the bucket has been found, let's use the newest session from 1755 1.4 christos * the bucket, as it has the highest chance to be successfully 1756 1.4 christos * resumed. 1757 1.4 christos */ 1758 1.4 christos INSIST(!ISC_LIST_EMPTY(bucket->entries)); 1759 1.4 christos entry = ISC_LIST_TAIL(bucket->entries); 1760 1.4 christos RUNTIME_CHECK(SSL_set_session(tls, entry->session) == 1); 1761 1.4 christos client_cache_entry_delete(cache, entry); 1762 1.4 christos 1763 1.4 christos exit: 1764 1.4 christos isc_mutex_unlock(&cache->lock); 1765 1.4 christos } 1766 1.4 christos 1767 1.4 christos void 1768 1.4 christos isc_tlsctx_client_session_cache_keep_sockaddr( 1769 1.4 christos isc_tlsctx_client_session_cache_t *cache, isc_sockaddr_t *remote_peer, 1770 1.4 christos isc_tls_t *tls) { 1771 1.4 christos char peername[ISC_SOCKADDR_FORMATSIZE] = { 0 }; 1772 1.4 christos 1773 1.4 christos REQUIRE(remote_peer != NULL); 1774 1.4 christos 1775 1.4 christos isc_sockaddr_format(remote_peer, peername, sizeof(peername)); 1776 1.4 christos 1777 1.4 christos isc_tlsctx_client_session_cache_keep(cache, peername, tls); 1778 1.4 christos } 1779 1.4 christos 1780 1.4 christos void 1781 1.4 christos isc_tlsctx_client_session_cache_reuse_sockaddr( 1782 1.4 christos isc_tlsctx_client_session_cache_t *cache, isc_sockaddr_t *remote_peer, 1783 1.4 christos isc_tls_t *tls) { 1784 1.4 christos char peername[ISC_SOCKADDR_FORMATSIZE] = { 0 }; 1785 1.4 christos 1786 1.4 christos REQUIRE(remote_peer != NULL); 1787 1.4 christos 1788 1.4 christos isc_sockaddr_format(remote_peer, peername, sizeof(peername)); 1789 1.4 christos 1790 1.4 christos isc_tlsctx_client_session_cache_reuse(cache, peername, tls); 1791 1.4 christos } 1792 1.4 christos 1793 1.4 christos const isc_tlsctx_t * 1794 1.4 christos isc_tlsctx_client_session_cache_getctx( 1795 1.4 christos isc_tlsctx_client_session_cache_t *cache) { 1796 1.4 christos REQUIRE(VALID_TLSCTX_CLIENT_SESSION_CACHE(cache)); 1797 1.6 christos return cache->ctx; 1798 1.4 christos } 1799 1.4 christos 1800 1.4 christos void 1801 1.4 christos isc_tlsctx_set_random_session_id_context(isc_tlsctx_t *ctx) { 1802 1.4 christos uint8_t session_id_ctx[SSL_MAX_SID_CTX_LENGTH] = { 0 }; 1803 1.4 christos const size_t len = ISC_MIN(20, sizeof(session_id_ctx)); 1804 1.4 christos 1805 1.4 christos REQUIRE(ctx != NULL); 1806 1.4 christos 1807 1.4 christos RUNTIME_CHECK(RAND_bytes(session_id_ctx, len) == 1); 1808 1.4 christos 1809 1.4 christos RUNTIME_CHECK( 1810 1.4 christos SSL_CTX_set_session_id_context(ctx, session_id_ctx, len) == 1); 1811 1.4 christos } 1812 1.7 christos 1813 1.7 christos bool 1814 1.7 christos isc_tls_valid_sni_hostname(const char *hostname) { 1815 1.7 christos struct sockaddr_in sa_v4 = { 0 }; 1816 1.7 christos struct sockaddr_in6 sa_v6 = { 0 }; 1817 1.7 christos int ret = 0; 1818 1.7 christos 1819 1.7 christos if (hostname == NULL) { 1820 1.7 christos return false; 1821 1.7 christos } 1822 1.7 christos 1823 1.7 christos ret = inet_pton(AF_INET, hostname, &sa_v4.sin_addr); 1824 1.7 christos if (ret == 1) { 1825 1.7 christos return false; 1826 1.7 christos } 1827 1.7 christos 1828 1.7 christos ret = inet_pton(AF_INET6, hostname, &sa_v6.sin6_addr); 1829 1.7 christos if (ret == 1) { 1830 1.7 christos return false; 1831 1.7 christos } 1832 1.7 christos 1833 1.7 christos return true; 1834 1.7 christos } 1835