1 /* $OpenBSD: ssl_lib.c,v 1.329 2024/08/03 04:50:27 tb Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 /* ==================================================================== 59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. 60 * 61 * Redistribution and use in source and binary forms, with or without 62 * modification, are permitted provided that the following conditions 63 * are met: 64 * 65 * 1. Redistributions of source code must retain the above copyright 66 * notice, this list of conditions and the following disclaimer. 67 * 68 * 2. Redistributions in binary form must reproduce the above copyright 69 * notice, this list of conditions and the following disclaimer in 70 * the documentation and/or other materials provided with the 71 * distribution. 72 * 73 * 3. All advertising materials mentioning features or use of this 74 * software must display the following acknowledgment: 75 * "This product includes software developed by the OpenSSL Project 76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 77 * 78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 79 * endorse or promote products derived from this software without 80 * prior written permission. For written permission, please contact 81 * openssl-core@openssl.org. 82 * 83 * 5. Products derived from this software may not be called "OpenSSL" 84 * nor may "OpenSSL" appear in their names without prior written 85 * permission of the OpenSSL Project. 86 * 87 * 6. Redistributions of any form whatsoever must retain the following 88 * acknowledgment: 89 * "This product includes software developed by the OpenSSL Project 90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 91 * 92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 103 * OF THE POSSIBILITY OF SUCH DAMAGE. 104 * ==================================================================== 105 * 106 * This product includes cryptographic software written by Eric Young 107 * (eay@cryptsoft.com). This product includes software written by Tim 108 * Hudson (tjh@cryptsoft.com). 109 * 110 */ 111 /* ==================================================================== 112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 113 * ECC cipher suite support in OpenSSL originally developed by 114 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. 115 */ 116 /* ==================================================================== 117 * Copyright 2005 Nokia. All rights reserved. 118 * 119 * The portions of the attached software ("Contribution") is developed by 120 * Nokia Corporation and is licensed pursuant to the OpenSSL open source 121 * license. 122 * 123 * The Contribution, originally written by Mika Kousa and Pasi Eronen of 124 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites 125 * support (see RFC 4279) to OpenSSL. 126 * 127 * No patent licenses or other rights except those expressly stated in 128 * the OpenSSL open source license shall be deemed granted or received 129 * expressly, by implication, estoppel, or otherwise. 130 * 131 * No assurances are provided by Nokia that the Contribution does not 132 * infringe the patent or other intellectual property rights of any third 133 * party or that the license provides you with all the necessary rights 134 * to make use of the Contribution. 135 * 136 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN 137 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA 138 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY 139 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR 140 * OTHERWISE. 141 */ 142 143 #include <arpa/inet.h> 144 #include <sys/socket.h> 145 #include <netinet/in.h> 146 147 #include <limits.h> 148 #include <stdio.h> 149 150 #include <openssl/dh.h> 151 #include <openssl/lhash.h> 152 #include <openssl/objects.h> 153 #include <openssl/ocsp.h> 154 #include <openssl/opensslconf.h> 155 #include <openssl/x509v3.h> 156 157 #include "bytestring.h" 158 #include "dtls_local.h" 159 #include "ssl_local.h" 160 #include "ssl_sigalgs.h" 161 #include "ssl_tlsext.h" 162 #include "tls12_internal.h" 163 164 int 165 SSL_clear(SSL *s) 166 { 167 if (s->method == NULL) { 168 SSLerror(s, SSL_R_NO_METHOD_SPECIFIED); 169 return (0); 170 } 171 172 if (ssl_clear_bad_session(s)) { 173 SSL_SESSION_free(s->session); 174 s->session = NULL; 175 } 176 177 s->error = 0; 178 s->hit = 0; 179 s->shutdown = 0; 180 181 if (s->renegotiate) { 182 SSLerror(s, ERR_R_INTERNAL_ERROR); 183 return (0); 184 } 185 186 s->version = s->method->version; 187 s->client_version = s->version; 188 s->rwstate = SSL_NOTHING; 189 s->rstate = SSL_ST_READ_HEADER; 190 191 tls13_ctx_free(s->tls13); 192 s->tls13 = NULL; 193 194 ssl3_release_init_buffer(s); 195 196 ssl_clear_cipher_state(s); 197 198 s->first_packet = 0; 199 200 /* 201 * Check to see if we were changed into a different method, if 202 * so, revert back if we are not doing session-id reuse. 203 */ 204 if (!s->in_handshake && (s->session == NULL) && 205 (s->method != s->ctx->method)) { 206 s->method->ssl_free(s); 207 s->method = s->ctx->method; 208 if (!s->method->ssl_new(s)) 209 return (0); 210 } else 211 s->method->ssl_clear(s); 212 213 return (1); 214 } 215 LSSL_ALIAS(SSL_clear); 216 217 /* Used to change an SSL_CTXs default SSL method type */ 218 int 219 SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth) 220 { 221 STACK_OF(SSL_CIPHER) *ciphers; 222 223 ctx->method = meth; 224 225 ciphers = ssl_create_cipher_list(ctx->method, &ctx->cipher_list, 226 ctx->cipher_list_tls13, SSL_DEFAULT_CIPHER_LIST, 227 ctx->cert); 228 if (ciphers == NULL || sk_SSL_CIPHER_num(ciphers) <= 0) { 229 SSLerrorx(SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS); 230 return (0); 231 } 232 return (1); 233 } 234 LSSL_ALIAS(SSL_CTX_set_ssl_version); 235 236 SSL * 237 SSL_new(SSL_CTX *ctx) 238 { 239 SSL *s; 240 CBS cbs; 241 242 if (ctx == NULL) { 243 SSLerrorx(SSL_R_NULL_SSL_CTX); 244 return (NULL); 245 } 246 if (ctx->method == NULL) { 247 SSLerrorx(SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION); 248 return (NULL); 249 } 250 251 if ((s = calloc(1, sizeof(*s))) == NULL) 252 goto err; 253 254 if ((s->rl = tls12_record_layer_new()) == NULL) 255 goto err; 256 257 s->min_tls_version = ctx->min_tls_version; 258 s->max_tls_version = ctx->max_tls_version; 259 s->min_proto_version = ctx->min_proto_version; 260 s->max_proto_version = ctx->max_proto_version; 261 262 s->options = ctx->options; 263 s->mode = ctx->mode; 264 s->max_cert_list = ctx->max_cert_list; 265 s->num_tickets = ctx->num_tickets; 266 267 if ((s->cert = ssl_cert_dup(ctx->cert)) == NULL) 268 goto err; 269 270 s->read_ahead = ctx->read_ahead; 271 s->msg_callback = ctx->msg_callback; 272 s->msg_callback_arg = ctx->msg_callback_arg; 273 s->verify_mode = ctx->verify_mode; 274 s->sid_ctx_length = ctx->sid_ctx_length; 275 OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx); 276 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx)); 277 s->verify_callback = ctx->default_verify_callback; 278 s->generate_session_id = ctx->generate_session_id; 279 280 s->param = X509_VERIFY_PARAM_new(); 281 if (!s->param) 282 goto err; 283 X509_VERIFY_PARAM_inherit(s->param, ctx->param); 284 s->quiet_shutdown = ctx->quiet_shutdown; 285 s->max_send_fragment = ctx->max_send_fragment; 286 287 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX); 288 s->ctx = ctx; 289 s->tlsext_debug_cb = NULL; 290 s->tlsext_debug_arg = NULL; 291 s->tlsext_ticket_expected = 0; 292 s->tlsext_status_type = -1; 293 s->tlsext_status_expected = 0; 294 s->tlsext_ocsp_ids = NULL; 295 s->tlsext_ocsp_exts = NULL; 296 s->tlsext_ocsp_resp = NULL; 297 s->tlsext_ocsp_resp_len = 0; 298 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX); 299 s->initial_ctx = ctx; 300 301 if (!tlsext_randomize_build_order(s)) 302 goto err; 303 304 if (ctx->tlsext_ecpointformatlist != NULL) { 305 s->tlsext_ecpointformatlist = 306 calloc(ctx->tlsext_ecpointformatlist_length, 307 sizeof(ctx->tlsext_ecpointformatlist[0])); 308 if (s->tlsext_ecpointformatlist == NULL) 309 goto err; 310 memcpy(s->tlsext_ecpointformatlist, 311 ctx->tlsext_ecpointformatlist, 312 ctx->tlsext_ecpointformatlist_length * 313 sizeof(ctx->tlsext_ecpointformatlist[0])); 314 s->tlsext_ecpointformatlist_length = 315 ctx->tlsext_ecpointformatlist_length; 316 } 317 if (ctx->tlsext_supportedgroups != NULL) { 318 s->tlsext_supportedgroups = 319 calloc(ctx->tlsext_supportedgroups_length, 320 sizeof(ctx->tlsext_supportedgroups[0])); 321 if (s->tlsext_supportedgroups == NULL) 322 goto err; 323 memcpy(s->tlsext_supportedgroups, 324 ctx->tlsext_supportedgroups, 325 ctx->tlsext_supportedgroups_length * 326 sizeof(ctx->tlsext_supportedgroups[0])); 327 s->tlsext_supportedgroups_length = 328 ctx->tlsext_supportedgroups_length; 329 } 330 331 CBS_init(&cbs, ctx->alpn_client_proto_list, 332 ctx->alpn_client_proto_list_len); 333 if (!CBS_stow(&cbs, &s->alpn_client_proto_list, 334 &s->alpn_client_proto_list_len)) 335 goto err; 336 337 s->verify_result = X509_V_OK; 338 339 s->method = ctx->method; 340 s->quic_method = ctx->quic_method; 341 342 if (!s->method->ssl_new(s)) 343 goto err; 344 345 s->references = 1; 346 s->server = ctx->method->server; 347 348 SSL_clear(s); 349 350 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data); 351 352 return (s); 353 354 err: 355 SSL_free(s); 356 SSLerrorx(ERR_R_MALLOC_FAILURE); 357 return (NULL); 358 } 359 LSSL_ALIAS(SSL_new); 360 361 int 362 SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx, 363 unsigned int sid_ctx_len) 364 { 365 if (sid_ctx_len > sizeof ctx->sid_ctx) { 366 SSLerrorx(SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); 367 return (0); 368 } 369 ctx->sid_ctx_length = sid_ctx_len; 370 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len); 371 372 return (1); 373 } 374 LSSL_ALIAS(SSL_CTX_set_session_id_context); 375 376 int 377 SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx, 378 unsigned int sid_ctx_len) 379 { 380 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) { 381 SSLerror(ssl, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); 382 return (0); 383 } 384 ssl->sid_ctx_length = sid_ctx_len; 385 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len); 386 387 return (1); 388 } 389 LSSL_ALIAS(SSL_set_session_id_context); 390 391 int 392 SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) 393 { 394 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); 395 ctx->generate_session_id = cb; 396 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); 397 return (1); 398 } 399 LSSL_ALIAS(SSL_CTX_set_generate_session_id); 400 401 int 402 SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) 403 { 404 CRYPTO_w_lock(CRYPTO_LOCK_SSL); 405 ssl->generate_session_id = cb; 406 CRYPTO_w_unlock(CRYPTO_LOCK_SSL); 407 return (1); 408 } 409 LSSL_ALIAS(SSL_set_generate_session_id); 410 411 int 412 SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id, 413 unsigned int id_len) 414 { 415 /* 416 * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp 417 * shows how we can "construct" a session to give us the desired 418 * check - ie. to find if there's a session in the hash table 419 * that would conflict with any new session built out of this 420 * id/id_len and the ssl_version in use by this SSL. 421 */ 422 SSL_SESSION r, *p; 423 424 if (id_len > sizeof r.session_id) 425 return (0); 426 427 r.ssl_version = ssl->version; 428 r.session_id_length = id_len; 429 memcpy(r.session_id, id, id_len); 430 431 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); 432 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r); 433 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); 434 return (p != NULL); 435 } 436 LSSL_ALIAS(SSL_has_matching_session_id); 437 438 int 439 SSL_CTX_set_purpose(SSL_CTX *s, int purpose) 440 { 441 return (X509_VERIFY_PARAM_set_purpose(s->param, purpose)); 442 } 443 LSSL_ALIAS(SSL_CTX_set_purpose); 444 445 int 446 SSL_set_purpose(SSL *s, int purpose) 447 { 448 return (X509_VERIFY_PARAM_set_purpose(s->param, purpose)); 449 } 450 LSSL_ALIAS(SSL_set_purpose); 451 452 int 453 SSL_CTX_set_trust(SSL_CTX *s, int trust) 454 { 455 return (X509_VERIFY_PARAM_set_trust(s->param, trust)); 456 } 457 LSSL_ALIAS(SSL_CTX_set_trust); 458 459 int 460 SSL_set_trust(SSL *s, int trust) 461 { 462 return (X509_VERIFY_PARAM_set_trust(s->param, trust)); 463 } 464 LSSL_ALIAS(SSL_set_trust); 465 466 int 467 SSL_set1_host(SSL *s, const char *hostname) 468 { 469 struct in_addr ina; 470 struct in6_addr in6a; 471 472 if (hostname != NULL && *hostname != '\0' && 473 (inet_pton(AF_INET, hostname, &ina) == 1 || 474 inet_pton(AF_INET6, hostname, &in6a) == 1)) 475 return X509_VERIFY_PARAM_set1_ip_asc(s->param, hostname); 476 else 477 return X509_VERIFY_PARAM_set1_host(s->param, hostname, 0); 478 } 479 LSSL_ALIAS(SSL_set1_host); 480 481 void 482 SSL_set_hostflags(SSL *s, unsigned int flags) 483 { 484 X509_VERIFY_PARAM_set_hostflags(s->param, flags); 485 } 486 LSSL_ALIAS(SSL_set_hostflags); 487 488 const char * 489 SSL_get0_peername(SSL *s) 490 { 491 return X509_VERIFY_PARAM_get0_peername(s->param); 492 } 493 LSSL_ALIAS(SSL_get0_peername); 494 495 X509_VERIFY_PARAM * 496 SSL_CTX_get0_param(SSL_CTX *ctx) 497 { 498 return (ctx->param); 499 } 500 LSSL_ALIAS(SSL_CTX_get0_param); 501 502 int 503 SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) 504 { 505 return (X509_VERIFY_PARAM_set1(ctx->param, vpm)); 506 } 507 LSSL_ALIAS(SSL_CTX_set1_param); 508 509 X509_VERIFY_PARAM * 510 SSL_get0_param(SSL *ssl) 511 { 512 return (ssl->param); 513 } 514 LSSL_ALIAS(SSL_get0_param); 515 516 int 517 SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) 518 { 519 return (X509_VERIFY_PARAM_set1(ssl->param, vpm)); 520 } 521 LSSL_ALIAS(SSL_set1_param); 522 523 void 524 SSL_free(SSL *s) 525 { 526 int i; 527 528 if (s == NULL) 529 return; 530 531 i = CRYPTO_add(&s->references, -1, CRYPTO_LOCK_SSL); 532 if (i > 0) 533 return; 534 535 X509_VERIFY_PARAM_free(s->param); 536 537 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data); 538 539 if (s->bbio != NULL) { 540 /* If the buffering BIO is in place, pop it off */ 541 if (s->bbio == s->wbio) { 542 s->wbio = BIO_pop(s->wbio); 543 } 544 BIO_free(s->bbio); 545 s->bbio = NULL; 546 } 547 548 if (s->rbio != s->wbio) 549 BIO_free_all(s->rbio); 550 BIO_free_all(s->wbio); 551 552 tls13_ctx_free(s->tls13); 553 554 ssl3_release_init_buffer(s); 555 556 sk_SSL_CIPHER_free(s->cipher_list); 557 sk_SSL_CIPHER_free(s->cipher_list_tls13); 558 559 /* Make the next call work :-) */ 560 if (s->session != NULL) { 561 ssl_clear_bad_session(s); 562 SSL_SESSION_free(s->session); 563 } 564 565 ssl_clear_cipher_state(s); 566 567 ssl_cert_free(s->cert); 568 569 free(s->tlsext_build_order); 570 571 free(s->tlsext_hostname); 572 SSL_CTX_free(s->initial_ctx); 573 574 free(s->tlsext_ecpointformatlist); 575 free(s->tlsext_supportedgroups); 576 577 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, 578 X509_EXTENSION_free); 579 sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free); 580 free(s->tlsext_ocsp_resp); 581 582 sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free); 583 584 if (s->method != NULL) 585 s->method->ssl_free(s); 586 587 SSL_CTX_free(s->ctx); 588 589 free(s->alpn_client_proto_list); 590 591 free(s->quic_transport_params); 592 593 #ifndef OPENSSL_NO_SRTP 594 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles); 595 #endif 596 597 tls12_record_layer_free(s->rl); 598 599 free(s); 600 } 601 LSSL_ALIAS(SSL_free); 602 603 int 604 SSL_up_ref(SSL *s) 605 { 606 return CRYPTO_add(&s->references, 1, CRYPTO_LOCK_SSL) > 1; 607 } 608 LSSL_ALIAS(SSL_up_ref); 609 610 void 611 SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) 612 { 613 /* If the output buffering BIO is still in place, remove it */ 614 if (s->bbio != NULL) { 615 if (s->wbio == s->bbio) { 616 s->wbio = BIO_next(s->wbio); 617 BIO_set_next(s->bbio, NULL); 618 } 619 } 620 621 if (s->rbio != rbio && s->rbio != s->wbio) 622 BIO_free_all(s->rbio); 623 if (s->wbio != wbio) 624 BIO_free_all(s->wbio); 625 s->rbio = rbio; 626 s->wbio = wbio; 627 } 628 LSSL_ALIAS(SSL_set_bio); 629 630 BIO * 631 SSL_get_rbio(const SSL *s) 632 { 633 return (s->rbio); 634 } 635 LSSL_ALIAS(SSL_get_rbio); 636 637 void 638 SSL_set0_rbio(SSL *s, BIO *rbio) 639 { 640 BIO_free_all(s->rbio); 641 s->rbio = rbio; 642 } 643 LSSL_ALIAS(SSL_set0_rbio); 644 645 BIO * 646 SSL_get_wbio(const SSL *s) 647 { 648 return (s->wbio); 649 } 650 LSSL_ALIAS(SSL_get_wbio); 651 652 int 653 SSL_get_fd(const SSL *s) 654 { 655 return (SSL_get_rfd(s)); 656 } 657 LSSL_ALIAS(SSL_get_fd); 658 659 int 660 SSL_get_rfd(const SSL *s) 661 { 662 int ret = -1; 663 BIO *b, *r; 664 665 b = SSL_get_rbio(s); 666 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR); 667 if (r != NULL) 668 BIO_get_fd(r, &ret); 669 return (ret); 670 } 671 LSSL_ALIAS(SSL_get_rfd); 672 673 int 674 SSL_get_wfd(const SSL *s) 675 { 676 int ret = -1; 677 BIO *b, *r; 678 679 b = SSL_get_wbio(s); 680 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR); 681 if (r != NULL) 682 BIO_get_fd(r, &ret); 683 return (ret); 684 } 685 LSSL_ALIAS(SSL_get_wfd); 686 687 int 688 SSL_set_fd(SSL *s, int fd) 689 { 690 int ret = 0; 691 BIO *bio = NULL; 692 693 bio = BIO_new(BIO_s_socket()); 694 695 if (bio == NULL) { 696 SSLerror(s, ERR_R_BUF_LIB); 697 goto err; 698 } 699 BIO_set_fd(bio, fd, BIO_NOCLOSE); 700 SSL_set_bio(s, bio, bio); 701 ret = 1; 702 err: 703 return (ret); 704 } 705 LSSL_ALIAS(SSL_set_fd); 706 707 int 708 SSL_set_wfd(SSL *s, int fd) 709 { 710 int ret = 0; 711 BIO *bio = NULL; 712 713 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET) 714 || ((int)BIO_get_fd(s->rbio, NULL) != fd)) { 715 bio = BIO_new(BIO_s_socket()); 716 717 if (bio == NULL) { 718 SSLerror(s, ERR_R_BUF_LIB); 719 goto err; 720 } 721 BIO_set_fd(bio, fd, BIO_NOCLOSE); 722 SSL_set_bio(s, SSL_get_rbio(s), bio); 723 } else 724 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s)); 725 ret = 1; 726 err: 727 return (ret); 728 } 729 LSSL_ALIAS(SSL_set_wfd); 730 731 int 732 SSL_set_rfd(SSL *s, int fd) 733 { 734 int ret = 0; 735 BIO *bio = NULL; 736 737 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET) 738 || ((int)BIO_get_fd(s->wbio, NULL) != fd)) { 739 bio = BIO_new(BIO_s_socket()); 740 741 if (bio == NULL) { 742 SSLerror(s, ERR_R_BUF_LIB); 743 goto err; 744 } 745 BIO_set_fd(bio, fd, BIO_NOCLOSE); 746 SSL_set_bio(s, bio, SSL_get_wbio(s)); 747 } else 748 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s)); 749 ret = 1; 750 err: 751 return (ret); 752 } 753 LSSL_ALIAS(SSL_set_rfd); 754 755 756 /* return length of latest Finished message we sent, copy to 'buf' */ 757 size_t 758 SSL_get_finished(const SSL *s, void *buf, size_t count) 759 { 760 size_t ret; 761 762 ret = s->s3->hs.finished_len; 763 if (count > ret) 764 count = ret; 765 memcpy(buf, s->s3->hs.finished, count); 766 return (ret); 767 } 768 LSSL_ALIAS(SSL_get_finished); 769 770 /* return length of latest Finished message we expected, copy to 'buf' */ 771 size_t 772 SSL_get_peer_finished(const SSL *s, void *buf, size_t count) 773 { 774 size_t ret; 775 776 ret = s->s3->hs.peer_finished_len; 777 if (count > ret) 778 count = ret; 779 memcpy(buf, s->s3->hs.peer_finished, count); 780 return (ret); 781 } 782 LSSL_ALIAS(SSL_get_peer_finished); 783 784 785 int 786 SSL_get_verify_mode(const SSL *s) 787 { 788 return (s->verify_mode); 789 } 790 LSSL_ALIAS(SSL_get_verify_mode); 791 792 int 793 SSL_get_verify_depth(const SSL *s) 794 { 795 return (X509_VERIFY_PARAM_get_depth(s->param)); 796 } 797 LSSL_ALIAS(SSL_get_verify_depth); 798 799 int 800 (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) 801 { 802 return (s->verify_callback); 803 } 804 LSSL_ALIAS(SSL_get_verify_callback); 805 806 void 807 SSL_CTX_set_keylog_callback(SSL_CTX *ctx, SSL_CTX_keylog_cb_func cb) 808 { 809 ctx->keylog_callback = cb; 810 } 811 LSSL_ALIAS(SSL_CTX_set_keylog_callback); 812 813 SSL_CTX_keylog_cb_func 814 SSL_CTX_get_keylog_callback(const SSL_CTX *ctx) 815 { 816 return (ctx->keylog_callback); 817 } 818 LSSL_ALIAS(SSL_CTX_get_keylog_callback); 819 820 int 821 SSL_set_num_tickets(SSL *s, size_t num_tickets) 822 { 823 s->num_tickets = num_tickets; 824 825 return 1; 826 } 827 LSSL_ALIAS(SSL_set_num_tickets); 828 829 size_t 830 SSL_get_num_tickets(const SSL *s) 831 { 832 return s->num_tickets; 833 } 834 LSSL_ALIAS(SSL_get_num_tickets); 835 836 int 837 SSL_CTX_set_num_tickets(SSL_CTX *ctx, size_t num_tickets) 838 { 839 ctx->num_tickets = num_tickets; 840 841 return 1; 842 } 843 LSSL_ALIAS(SSL_CTX_set_num_tickets); 844 845 size_t 846 SSL_CTX_get_num_tickets(const SSL_CTX *ctx) 847 { 848 return ctx->num_tickets; 849 } 850 LSSL_ALIAS(SSL_CTX_get_num_tickets); 851 852 int 853 SSL_CTX_get_verify_mode(const SSL_CTX *ctx) 854 { 855 return (ctx->verify_mode); 856 } 857 LSSL_ALIAS(SSL_CTX_get_verify_mode); 858 859 int 860 SSL_CTX_get_verify_depth(const SSL_CTX *ctx) 861 { 862 return (X509_VERIFY_PARAM_get_depth(ctx->param)); 863 } 864 LSSL_ALIAS(SSL_CTX_get_verify_depth); 865 866 int 867 (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) 868 { 869 return (ctx->default_verify_callback); 870 } 871 LSSL_ALIAS(SSL_CTX_get_verify_callback); 872 873 void 874 SSL_set_verify(SSL *s, int mode, 875 int (*callback)(int ok, X509_STORE_CTX *ctx)) 876 { 877 s->verify_mode = mode; 878 if (callback != NULL) 879 s->verify_callback = callback; 880 } 881 LSSL_ALIAS(SSL_set_verify); 882 883 void 884 SSL_set_verify_depth(SSL *s, int depth) 885 { 886 X509_VERIFY_PARAM_set_depth(s->param, depth); 887 } 888 LSSL_ALIAS(SSL_set_verify_depth); 889 890 void 891 SSL_set_read_ahead(SSL *s, int yes) 892 { 893 s->read_ahead = yes; 894 } 895 LSSL_ALIAS(SSL_set_read_ahead); 896 897 int 898 SSL_get_read_ahead(const SSL *s) 899 { 900 return (s->read_ahead); 901 } 902 LSSL_ALIAS(SSL_get_read_ahead); 903 904 int 905 SSL_pending(const SSL *s) 906 { 907 return (s->method->ssl_pending(s)); 908 } 909 LSSL_ALIAS(SSL_pending); 910 911 X509 * 912 SSL_get_peer_certificate(const SSL *s) 913 { 914 X509 *cert; 915 916 if (s == NULL || s->session == NULL) 917 return NULL; 918 919 if ((cert = s->session->peer_cert) == NULL) 920 return NULL; 921 922 X509_up_ref(cert); 923 924 return cert; 925 } 926 LSSL_ALIAS(SSL_get_peer_certificate); 927 928 STACK_OF(X509) * 929 SSL_get_peer_cert_chain(const SSL *s) 930 { 931 if (s == NULL) 932 return NULL; 933 934 /* 935 * Achtung! Due to API inconsistency, a client includes the peer's leaf 936 * certificate in the peer certificate chain, while a server does not. 937 */ 938 if (!s->server) 939 return s->s3->hs.peer_certs; 940 941 return s->s3->hs.peer_certs_no_leaf; 942 } 943 LSSL_ALIAS(SSL_get_peer_cert_chain); 944 945 STACK_OF(X509) * 946 SSL_get0_verified_chain(const SSL *s) 947 { 948 if (s->s3 == NULL) 949 return NULL; 950 return s->s3->hs.verified_chain; 951 } 952 LSSL_ALIAS(SSL_get0_verified_chain); 953 954 /* 955 * Now in theory, since the calling process own 't' it should be safe to 956 * modify. We need to be able to read f without being hassled 957 */ 958 int 959 SSL_copy_session_id(SSL *t, const SSL *f) 960 { 961 SSL_CERT *tmp; 962 963 /* Do we need to do SSL locking? */ 964 if (!SSL_set_session(t, SSL_get_session(f))) 965 return 0; 966 967 /* What if we are set up for one protocol but want to talk another? */ 968 if (t->method != f->method) { 969 t->method->ssl_free(t); 970 t->method = f->method; 971 if (!t->method->ssl_new(t)) 972 return 0; 973 } 974 975 tmp = t->cert; 976 if (f->cert != NULL) { 977 CRYPTO_add(&f->cert->references, 1, CRYPTO_LOCK_SSL_CERT); 978 t->cert = f->cert; 979 } else 980 t->cert = NULL; 981 ssl_cert_free(tmp); 982 983 if (!SSL_set_session_id_context(t, f->sid_ctx, f->sid_ctx_length)) 984 return 0; 985 986 return 1; 987 } 988 LSSL_ALIAS(SSL_copy_session_id); 989 990 /* Fix this so it checks all the valid key/cert options */ 991 int 992 SSL_CTX_check_private_key(const SSL_CTX *ctx) 993 { 994 if ((ctx == NULL) || (ctx->cert == NULL) || 995 (ctx->cert->key->x509 == NULL)) { 996 SSLerrorx(SSL_R_NO_CERTIFICATE_ASSIGNED); 997 return (0); 998 } 999 if (ctx->cert->key->privatekey == NULL) { 1000 SSLerrorx(SSL_R_NO_PRIVATE_KEY_ASSIGNED); 1001 return (0); 1002 } 1003 return (X509_check_private_key(ctx->cert->key->x509, 1004 ctx->cert->key->privatekey)); 1005 } 1006 LSSL_ALIAS(SSL_CTX_check_private_key); 1007 1008 /* Fix this function so that it takes an optional type parameter */ 1009 int 1010 SSL_check_private_key(const SSL *ssl) 1011 { 1012 if (ssl == NULL) { 1013 SSLerrorx(ERR_R_PASSED_NULL_PARAMETER); 1014 return (0); 1015 } 1016 if (ssl->cert == NULL) { 1017 SSLerror(ssl, SSL_R_NO_CERTIFICATE_ASSIGNED); 1018 return (0); 1019 } 1020 if (ssl->cert->key->x509 == NULL) { 1021 SSLerror(ssl, SSL_R_NO_CERTIFICATE_ASSIGNED); 1022 return (0); 1023 } 1024 if (ssl->cert->key->privatekey == NULL) { 1025 SSLerror(ssl, SSL_R_NO_PRIVATE_KEY_ASSIGNED); 1026 return (0); 1027 } 1028 return (X509_check_private_key(ssl->cert->key->x509, 1029 ssl->cert->key->privatekey)); 1030 } 1031 LSSL_ALIAS(SSL_check_private_key); 1032 1033 int 1034 SSL_accept(SSL *s) 1035 { 1036 if (s->handshake_func == NULL) 1037 SSL_set_accept_state(s); /* Not properly initialized yet */ 1038 1039 return (s->method->ssl_accept(s)); 1040 } 1041 LSSL_ALIAS(SSL_accept); 1042 1043 int 1044 SSL_connect(SSL *s) 1045 { 1046 if (s->handshake_func == NULL) 1047 SSL_set_connect_state(s); /* Not properly initialized yet */ 1048 1049 return (s->method->ssl_connect(s)); 1050 } 1051 LSSL_ALIAS(SSL_connect); 1052 1053 int 1054 SSL_is_dtls(const SSL *s) 1055 { 1056 return s->method->dtls; 1057 } 1058 LSSL_ALIAS(SSL_is_dtls); 1059 1060 int 1061 SSL_is_server(const SSL *s) 1062 { 1063 return s->server; 1064 } 1065 LSSL_ALIAS(SSL_is_server); 1066 1067 static long 1068 ssl_get_default_timeout(void) 1069 { 1070 /* 1071 * 2 hours, the 24 hours mentioned in the TLSv1 spec 1072 * is way too long for http, the cache would over fill. 1073 */ 1074 return (2 * 60 * 60); 1075 } 1076 1077 long 1078 SSL_get_default_timeout(const SSL *s) 1079 { 1080 return (ssl_get_default_timeout()); 1081 } 1082 LSSL_ALIAS(SSL_get_default_timeout); 1083 1084 int 1085 SSL_read(SSL *s, void *buf, int num) 1086 { 1087 if (num < 0) { 1088 SSLerror(s, SSL_R_BAD_LENGTH); 1089 return -1; 1090 } 1091 1092 if (SSL_is_quic(s)) { 1093 SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 1094 return (-1); 1095 } 1096 1097 if (s->handshake_func == NULL) { 1098 SSLerror(s, SSL_R_UNINITIALIZED); 1099 return (-1); 1100 } 1101 1102 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { 1103 s->rwstate = SSL_NOTHING; 1104 return (0); 1105 } 1106 return ssl3_read(s, buf, num); 1107 } 1108 LSSL_ALIAS(SSL_read); 1109 1110 int 1111 SSL_read_ex(SSL *s, void *buf, size_t num, size_t *bytes_read) 1112 { 1113 int ret; 1114 1115 /* We simply don't bother supporting enormous reads */ 1116 if (num > INT_MAX) { 1117 SSLerror(s, SSL_R_BAD_LENGTH); 1118 return 0; 1119 } 1120 1121 ret = SSL_read(s, buf, (int)num); 1122 if (ret < 0) 1123 ret = 0; 1124 *bytes_read = ret; 1125 1126 return ret > 0; 1127 } 1128 LSSL_ALIAS(SSL_read_ex); 1129 1130 int 1131 SSL_peek(SSL *s, void *buf, int num) 1132 { 1133 if (num < 0) { 1134 SSLerror(s, SSL_R_BAD_LENGTH); 1135 return -1; 1136 } 1137 1138 if (SSL_is_quic(s)) { 1139 SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 1140 return (-1); 1141 } 1142 1143 if (s->handshake_func == NULL) { 1144 SSLerror(s, SSL_R_UNINITIALIZED); 1145 return (-1); 1146 } 1147 1148 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { 1149 return (0); 1150 } 1151 return ssl3_peek(s, buf, num); 1152 } 1153 LSSL_ALIAS(SSL_peek); 1154 1155 int 1156 SSL_peek_ex(SSL *s, void *buf, size_t num, size_t *bytes_peeked) 1157 { 1158 int ret; 1159 1160 /* We simply don't bother supporting enormous peeks */ 1161 if (num > INT_MAX) { 1162 SSLerror(s, SSL_R_BAD_LENGTH); 1163 return 0; 1164 } 1165 1166 ret = SSL_peek(s, buf, (int)num); 1167 if (ret < 0) 1168 ret = 0; 1169 *bytes_peeked = ret; 1170 1171 return ret > 0; 1172 } 1173 LSSL_ALIAS(SSL_peek_ex); 1174 1175 int 1176 SSL_write(SSL *s, const void *buf, int num) 1177 { 1178 if (num < 0) { 1179 SSLerror(s, SSL_R_BAD_LENGTH); 1180 return -1; 1181 } 1182 1183 if (SSL_is_quic(s)) { 1184 SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 1185 return (-1); 1186 } 1187 1188 if (s->handshake_func == NULL) { 1189 SSLerror(s, SSL_R_UNINITIALIZED); 1190 return (-1); 1191 } 1192 1193 if (s->shutdown & SSL_SENT_SHUTDOWN) { 1194 s->rwstate = SSL_NOTHING; 1195 SSLerror(s, SSL_R_PROTOCOL_IS_SHUTDOWN); 1196 return (-1); 1197 } 1198 return ssl3_write(s, buf, num); 1199 } 1200 LSSL_ALIAS(SSL_write); 1201 1202 int 1203 SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *bytes_written) 1204 { 1205 int ret; 1206 1207 /* We simply don't bother supporting enormous writes */ 1208 if (num > INT_MAX) { 1209 SSLerror(s, SSL_R_BAD_LENGTH); 1210 return 0; 1211 } 1212 1213 if (num == 0) { 1214 /* This API is special */ 1215 bytes_written = 0; 1216 return 1; 1217 } 1218 1219 ret = SSL_write(s, buf, (int)num); 1220 if (ret < 0) 1221 ret = 0; 1222 *bytes_written = ret; 1223 1224 return ret > 0; 1225 } 1226 LSSL_ALIAS(SSL_write_ex); 1227 1228 uint32_t 1229 SSL_CTX_get_max_early_data(const SSL_CTX *ctx) 1230 { 1231 return 0; 1232 } 1233 LSSL_ALIAS(SSL_CTX_get_max_early_data); 1234 1235 int 1236 SSL_CTX_set_max_early_data(SSL_CTX *ctx, uint32_t max_early_data) 1237 { 1238 return 1; 1239 } 1240 LSSL_ALIAS(SSL_CTX_set_max_early_data); 1241 1242 uint32_t 1243 SSL_get_max_early_data(const SSL *s) 1244 { 1245 return 0; 1246 } 1247 LSSL_ALIAS(SSL_get_max_early_data); 1248 1249 int 1250 SSL_set_max_early_data(SSL *s, uint32_t max_early_data) 1251 { 1252 return 1; 1253 } 1254 LSSL_ALIAS(SSL_set_max_early_data); 1255 1256 int 1257 SSL_get_early_data_status(const SSL *s) 1258 { 1259 return SSL_EARLY_DATA_REJECTED; 1260 } 1261 LSSL_ALIAS(SSL_get_early_data_status); 1262 1263 int 1264 SSL_read_early_data(SSL *s, void *buf, size_t num, size_t *readbytes) 1265 { 1266 *readbytes = 0; 1267 1268 if (!s->server) { 1269 SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 1270 return SSL_READ_EARLY_DATA_ERROR; 1271 } 1272 1273 return SSL_READ_EARLY_DATA_FINISH; 1274 } 1275 LSSL_ALIAS(SSL_read_early_data); 1276 1277 int 1278 SSL_write_early_data(SSL *s, const void *buf, size_t num, size_t *written) 1279 { 1280 *written = 0; 1281 SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 1282 return 0; 1283 } 1284 LSSL_ALIAS(SSL_write_early_data); 1285 1286 int 1287 SSL_shutdown(SSL *s) 1288 { 1289 /* 1290 * Note that this function behaves differently from what one might 1291 * expect. Return values are 0 for no success (yet), 1292 * 1 for success; but calling it once is usually not enough, 1293 * even if blocking I/O is used (see ssl3_shutdown). 1294 */ 1295 1296 if (s->handshake_func == NULL) { 1297 SSLerror(s, SSL_R_UNINITIALIZED); 1298 return (-1); 1299 } 1300 1301 if (s != NULL && !SSL_in_init(s)) 1302 return (s->method->ssl_shutdown(s)); 1303 1304 return (1); 1305 } 1306 LSSL_ALIAS(SSL_shutdown); 1307 1308 int 1309 SSL_renegotiate(SSL *s) 1310 { 1311 if (s->renegotiate == 0) 1312 s->renegotiate = 1; 1313 1314 s->new_session = 1; 1315 1316 return (s->method->ssl_renegotiate(s)); 1317 } 1318 LSSL_ALIAS(SSL_renegotiate); 1319 1320 int 1321 SSL_renegotiate_abbreviated(SSL *s) 1322 { 1323 if (s->renegotiate == 0) 1324 s->renegotiate = 1; 1325 1326 s->new_session = 0; 1327 1328 return (s->method->ssl_renegotiate(s)); 1329 } 1330 LSSL_ALIAS(SSL_renegotiate_abbreviated); 1331 1332 int 1333 SSL_renegotiate_pending(SSL *s) 1334 { 1335 /* 1336 * Becomes true when negotiation is requested; 1337 * false again once a handshake has finished. 1338 */ 1339 return (s->renegotiate != 0); 1340 } 1341 LSSL_ALIAS(SSL_renegotiate_pending); 1342 1343 long 1344 SSL_ctrl(SSL *s, int cmd, long larg, void *parg) 1345 { 1346 long l; 1347 1348 switch (cmd) { 1349 case SSL_CTRL_GET_READ_AHEAD: 1350 return (s->read_ahead); 1351 case SSL_CTRL_SET_READ_AHEAD: 1352 l = s->read_ahead; 1353 s->read_ahead = larg; 1354 return (l); 1355 1356 case SSL_CTRL_SET_MSG_CALLBACK_ARG: 1357 s->msg_callback_arg = parg; 1358 return (1); 1359 1360 case SSL_CTRL_OPTIONS: 1361 return (s->options|=larg); 1362 case SSL_CTRL_CLEAR_OPTIONS: 1363 return (s->options&=~larg); 1364 case SSL_CTRL_MODE: 1365 return (s->mode|=larg); 1366 case SSL_CTRL_CLEAR_MODE: 1367 return (s->mode &=~larg); 1368 case SSL_CTRL_GET_MAX_CERT_LIST: 1369 return (s->max_cert_list); 1370 case SSL_CTRL_SET_MAX_CERT_LIST: 1371 l = s->max_cert_list; 1372 s->max_cert_list = larg; 1373 return (l); 1374 case SSL_CTRL_SET_MTU: 1375 #ifndef OPENSSL_NO_DTLS1 1376 if (larg < (long)dtls1_min_mtu()) 1377 return (0); 1378 #endif 1379 if (SSL_is_dtls(s)) { 1380 s->d1->mtu = larg; 1381 return (larg); 1382 } 1383 return (0); 1384 case SSL_CTRL_SET_MAX_SEND_FRAGMENT: 1385 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) 1386 return (0); 1387 s->max_send_fragment = larg; 1388 return (1); 1389 case SSL_CTRL_GET_RI_SUPPORT: 1390 if (s->s3) 1391 return (s->s3->send_connection_binding); 1392 else return (0); 1393 default: 1394 if (SSL_is_dtls(s)) 1395 return dtls1_ctrl(s, cmd, larg, parg); 1396 return ssl3_ctrl(s, cmd, larg, parg); 1397 } 1398 } 1399 LSSL_ALIAS(SSL_ctrl); 1400 1401 long 1402 SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) 1403 { 1404 switch (cmd) { 1405 case SSL_CTRL_SET_MSG_CALLBACK: 1406 s->msg_callback = (ssl_msg_callback_fn *)(fp); 1407 return (1); 1408 1409 default: 1410 return (ssl3_callback_ctrl(s, cmd, fp)); 1411 } 1412 } 1413 LSSL_ALIAS(SSL_callback_ctrl); 1414 1415 struct lhash_st_SSL_SESSION * 1416 SSL_CTX_sessions(SSL_CTX *ctx) 1417 { 1418 return (ctx->sessions); 1419 } 1420 LSSL_ALIAS(SSL_CTX_sessions); 1421 1422 long 1423 SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) 1424 { 1425 long l; 1426 1427 switch (cmd) { 1428 case SSL_CTRL_GET_READ_AHEAD: 1429 return (ctx->read_ahead); 1430 case SSL_CTRL_SET_READ_AHEAD: 1431 l = ctx->read_ahead; 1432 ctx->read_ahead = larg; 1433 return (l); 1434 1435 case SSL_CTRL_SET_MSG_CALLBACK_ARG: 1436 ctx->msg_callback_arg = parg; 1437 return (1); 1438 1439 case SSL_CTRL_GET_MAX_CERT_LIST: 1440 return (ctx->max_cert_list); 1441 case SSL_CTRL_SET_MAX_CERT_LIST: 1442 l = ctx->max_cert_list; 1443 ctx->max_cert_list = larg; 1444 return (l); 1445 1446 case SSL_CTRL_SET_SESS_CACHE_SIZE: 1447 l = ctx->session_cache_size; 1448 ctx->session_cache_size = larg; 1449 return (l); 1450 case SSL_CTRL_GET_SESS_CACHE_SIZE: 1451 return (ctx->session_cache_size); 1452 case SSL_CTRL_SET_SESS_CACHE_MODE: 1453 l = ctx->session_cache_mode; 1454 ctx->session_cache_mode = larg; 1455 return (l); 1456 case SSL_CTRL_GET_SESS_CACHE_MODE: 1457 return (ctx->session_cache_mode); 1458 1459 case SSL_CTRL_SESS_NUMBER: 1460 return (lh_SSL_SESSION_num_items(ctx->sessions)); 1461 case SSL_CTRL_SESS_CONNECT: 1462 return (ctx->stats.sess_connect); 1463 case SSL_CTRL_SESS_CONNECT_GOOD: 1464 return (ctx->stats.sess_connect_good); 1465 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE: 1466 return (ctx->stats.sess_connect_renegotiate); 1467 case SSL_CTRL_SESS_ACCEPT: 1468 return (ctx->stats.sess_accept); 1469 case SSL_CTRL_SESS_ACCEPT_GOOD: 1470 return (ctx->stats.sess_accept_good); 1471 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE: 1472 return (ctx->stats.sess_accept_renegotiate); 1473 case SSL_CTRL_SESS_HIT: 1474 return (ctx->stats.sess_hit); 1475 case SSL_CTRL_SESS_CB_HIT: 1476 return (ctx->stats.sess_cb_hit); 1477 case SSL_CTRL_SESS_MISSES: 1478 return (ctx->stats.sess_miss); 1479 case SSL_CTRL_SESS_TIMEOUTS: 1480 return (ctx->stats.sess_timeout); 1481 case SSL_CTRL_SESS_CACHE_FULL: 1482 return (ctx->stats.sess_cache_full); 1483 case SSL_CTRL_OPTIONS: 1484 return (ctx->options|=larg); 1485 case SSL_CTRL_CLEAR_OPTIONS: 1486 return (ctx->options&=~larg); 1487 case SSL_CTRL_MODE: 1488 return (ctx->mode|=larg); 1489 case SSL_CTRL_CLEAR_MODE: 1490 return (ctx->mode&=~larg); 1491 case SSL_CTRL_SET_MAX_SEND_FRAGMENT: 1492 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) 1493 return (0); 1494 ctx->max_send_fragment = larg; 1495 return (1); 1496 default: 1497 return (ssl3_ctx_ctrl(ctx, cmd, larg, parg)); 1498 } 1499 } 1500 LSSL_ALIAS(SSL_CTX_ctrl); 1501 1502 long 1503 SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) 1504 { 1505 switch (cmd) { 1506 case SSL_CTRL_SET_MSG_CALLBACK: 1507 ctx->msg_callback = (ssl_msg_callback_fn *)fp; 1508 return (1); 1509 1510 default: 1511 return (ssl3_ctx_callback_ctrl(ctx, cmd, fp)); 1512 } 1513 } 1514 LSSL_ALIAS(SSL_CTX_callback_ctrl); 1515 1516 STACK_OF(SSL_CIPHER) * 1517 SSL_get_ciphers(const SSL *s) 1518 { 1519 if (s == NULL) 1520 return (NULL); 1521 if (s->cipher_list != NULL) 1522 return (s->cipher_list); 1523 1524 return (s->ctx->cipher_list); 1525 } 1526 LSSL_ALIAS(SSL_get_ciphers); 1527 1528 STACK_OF(SSL_CIPHER) * 1529 SSL_get_client_ciphers(const SSL *s) 1530 { 1531 if (s == NULL || !s->server) 1532 return NULL; 1533 return s->s3->hs.client_ciphers; 1534 } 1535 LSSL_ALIAS(SSL_get_client_ciphers); 1536 1537 STACK_OF(SSL_CIPHER) * 1538 SSL_get1_supported_ciphers(SSL *s) 1539 { 1540 STACK_OF(SSL_CIPHER) *supported_ciphers = NULL, *ciphers; 1541 SSL_CIPHER *cipher; 1542 uint16_t min_vers, max_vers; 1543 int i; 1544 1545 if (s == NULL) 1546 return NULL; 1547 if (!ssl_supported_tls_version_range(s, &min_vers, &max_vers)) 1548 return NULL; 1549 if ((ciphers = SSL_get_ciphers(s)) == NULL) 1550 return NULL; 1551 if ((supported_ciphers = sk_SSL_CIPHER_new_null()) == NULL) 1552 return NULL; 1553 1554 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { 1555 if ((cipher = sk_SSL_CIPHER_value(ciphers, i)) == NULL) 1556 goto err; 1557 if (!ssl_cipher_allowed_in_tls_version_range(cipher, min_vers, 1558 max_vers)) 1559 continue; 1560 if (!ssl_security_supported_cipher(s, cipher)) 1561 continue; 1562 if (!sk_SSL_CIPHER_push(supported_ciphers, cipher)) 1563 goto err; 1564 } 1565 1566 if (sk_SSL_CIPHER_num(supported_ciphers) > 0) 1567 return supported_ciphers; 1568 1569 err: 1570 sk_SSL_CIPHER_free(supported_ciphers); 1571 return NULL; 1572 } 1573 LSSL_ALIAS(SSL_get1_supported_ciphers); 1574 1575 /* See if we have any ECC cipher suites. */ 1576 int 1577 ssl_has_ecc_ciphers(SSL *s) 1578 { 1579 STACK_OF(SSL_CIPHER) *ciphers; 1580 unsigned long alg_k, alg_a; 1581 SSL_CIPHER *cipher; 1582 int i; 1583 1584 if ((ciphers = SSL_get_ciphers(s)) == NULL) 1585 return 0; 1586 1587 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { 1588 cipher = sk_SSL_CIPHER_value(ciphers, i); 1589 1590 alg_k = cipher->algorithm_mkey; 1591 alg_a = cipher->algorithm_auth; 1592 1593 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) 1594 return 1; 1595 } 1596 1597 return 0; 1598 } 1599 1600 /* The old interface to get the same thing as SSL_get_ciphers(). */ 1601 const char * 1602 SSL_get_cipher_list(const SSL *s, int n) 1603 { 1604 STACK_OF(SSL_CIPHER) *ciphers; 1605 const SSL_CIPHER *cipher; 1606 1607 if ((ciphers = SSL_get_ciphers(s)) == NULL) 1608 return (NULL); 1609 if ((cipher = sk_SSL_CIPHER_value(ciphers, n)) == NULL) 1610 return (NULL); 1611 1612 return (cipher->name); 1613 } 1614 LSSL_ALIAS(SSL_get_cipher_list); 1615 1616 STACK_OF(SSL_CIPHER) * 1617 SSL_CTX_get_ciphers(const SSL_CTX *ctx) 1618 { 1619 if (ctx == NULL) 1620 return NULL; 1621 return ctx->cipher_list; 1622 } 1623 LSSL_ALIAS(SSL_CTX_get_ciphers); 1624 1625 /* Specify the ciphers to be used by default by the SSL_CTX. */ 1626 int 1627 SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) 1628 { 1629 STACK_OF(SSL_CIPHER) *ciphers; 1630 1631 /* 1632 * ssl_create_cipher_list may return an empty stack if it was unable to 1633 * find a cipher matching the given rule string (for example if the 1634 * rule string specifies a cipher which has been disabled). This is not 1635 * an error as far as ssl_create_cipher_list is concerned, and hence 1636 * ctx->cipher_list has been updated. 1637 */ 1638 ciphers = ssl_create_cipher_list(ctx->method, &ctx->cipher_list, 1639 ctx->cipher_list_tls13, str, ctx->cert); 1640 if (ciphers == NULL) { 1641 return (0); 1642 } else if (sk_SSL_CIPHER_num(ciphers) == 0) { 1643 SSLerrorx(SSL_R_NO_CIPHER_MATCH); 1644 return (0); 1645 } 1646 return (1); 1647 } 1648 LSSL_ALIAS(SSL_CTX_set_cipher_list); 1649 1650 int 1651 SSL_CTX_set_ciphersuites(SSL_CTX *ctx, const char *str) 1652 { 1653 if (!ssl_parse_ciphersuites(&ctx->cipher_list_tls13, str)) { 1654 SSLerrorx(SSL_R_NO_CIPHER_MATCH); 1655 return 0; 1656 } 1657 if (!ssl_merge_cipherlists(ctx->cipher_list, 1658 ctx->cipher_list_tls13, &ctx->cipher_list)) 1659 return 0; 1660 1661 return 1; 1662 } 1663 LSSL_ALIAS(SSL_CTX_set_ciphersuites); 1664 1665 /* Specify the ciphers to be used by the SSL. */ 1666 int 1667 SSL_set_cipher_list(SSL *s, const char *str) 1668 { 1669 STACK_OF(SSL_CIPHER) *ciphers, *ciphers_tls13; 1670 1671 if ((ciphers_tls13 = s->cipher_list_tls13) == NULL) 1672 ciphers_tls13 = s->ctx->cipher_list_tls13; 1673 1674 /* See comment in SSL_CTX_set_cipher_list. */ 1675 ciphers = ssl_create_cipher_list(s->ctx->method, &s->cipher_list, 1676 ciphers_tls13, str, s->cert); 1677 if (ciphers == NULL) { 1678 return (0); 1679 } else if (sk_SSL_CIPHER_num(ciphers) == 0) { 1680 SSLerror(s, SSL_R_NO_CIPHER_MATCH); 1681 return (0); 1682 } 1683 return (1); 1684 } 1685 LSSL_ALIAS(SSL_set_cipher_list); 1686 1687 int 1688 SSL_set_ciphersuites(SSL *s, const char *str) 1689 { 1690 STACK_OF(SSL_CIPHER) *ciphers; 1691 1692 if ((ciphers = s->cipher_list) == NULL) 1693 ciphers = s->ctx->cipher_list; 1694 1695 if (!ssl_parse_ciphersuites(&s->cipher_list_tls13, str)) { 1696 SSLerrorx(SSL_R_NO_CIPHER_MATCH); 1697 return (0); 1698 } 1699 if (!ssl_merge_cipherlists(ciphers, s->cipher_list_tls13, 1700 &s->cipher_list)) 1701 return 0; 1702 1703 return 1; 1704 } 1705 LSSL_ALIAS(SSL_set_ciphersuites); 1706 1707 char * 1708 SSL_get_shared_ciphers(const SSL *s, char *buf, int len) 1709 { 1710 STACK_OF(SSL_CIPHER) *client_ciphers, *server_ciphers; 1711 const SSL_CIPHER *cipher; 1712 size_t curlen = 0; 1713 char *end; 1714 int i; 1715 1716 if (!s->server || len < 2) 1717 return NULL; 1718 1719 if ((client_ciphers = s->s3->hs.client_ciphers) == NULL) 1720 return NULL; 1721 if ((server_ciphers = SSL_get_ciphers(s)) == NULL) 1722 return NULL; 1723 if (sk_SSL_CIPHER_num(client_ciphers) == 0 || 1724 sk_SSL_CIPHER_num(server_ciphers) == 0) 1725 return NULL; 1726 1727 buf[0] = '\0'; 1728 for (i = 0; i < sk_SSL_CIPHER_num(client_ciphers); i++) { 1729 cipher = sk_SSL_CIPHER_value(client_ciphers, i); 1730 1731 if (sk_SSL_CIPHER_find(server_ciphers, cipher) < 0) 1732 continue; 1733 1734 end = buf + curlen; 1735 if (strlcat(buf, cipher->name, len) >= len || 1736 (curlen = strlcat(buf, ":", len)) >= len) { 1737 /* remove truncated cipher from list */ 1738 *end = '\0'; 1739 break; 1740 } 1741 } 1742 /* remove trailing colon */ 1743 if ((end = strrchr(buf, ':')) != NULL) 1744 *end = '\0'; 1745 return buf; 1746 } 1747 LSSL_ALIAS(SSL_get_shared_ciphers); 1748 1749 /* 1750 * Return a servername extension value if provided in Client Hello, or NULL. 1751 * So far, only host_name types are defined (RFC 3546). 1752 */ 1753 const char * 1754 SSL_get_servername(const SSL *s, const int type) 1755 { 1756 if (type != TLSEXT_NAMETYPE_host_name) 1757 return (NULL); 1758 1759 return (s->session && !s->tlsext_hostname ? 1760 s->session->tlsext_hostname : 1761 s->tlsext_hostname); 1762 } 1763 LSSL_ALIAS(SSL_get_servername); 1764 1765 int 1766 SSL_get_servername_type(const SSL *s) 1767 { 1768 if (s->session && 1769 (!s->tlsext_hostname ? 1770 s->session->tlsext_hostname : s->tlsext_hostname)) 1771 return (TLSEXT_NAMETYPE_host_name); 1772 return (-1); 1773 } 1774 LSSL_ALIAS(SSL_get_servername_type); 1775 1776 /* 1777 * SSL_select_next_proto implements standard protocol selection. It is 1778 * expected that this function is called from the callback set by 1779 * SSL_CTX_set_alpn_select_cb. 1780 * 1781 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte 1782 * strings. The length byte itself is not included in the length. A byte 1783 * string of length 0 is invalid. No byte string may be truncated. 1784 * 1785 * It returns either: 1786 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or 1787 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached. 1788 * 1789 * XXX - the out argument points into server_list or client_list and should 1790 * therefore really be const. We can't fix that without breaking the callers. 1791 */ 1792 int 1793 SSL_select_next_proto(unsigned char **out, unsigned char *outlen, 1794 const unsigned char *peer_list, unsigned int peer_list_len, 1795 const unsigned char *supported_list, unsigned int supported_list_len) 1796 { 1797 CBS peer, peer_proto, supported, supported_proto; 1798 1799 *out = NULL; 1800 *outlen = 0; 1801 1802 /* First check that the supported list is well-formed. */ 1803 CBS_init(&supported, supported_list, supported_list_len); 1804 if (!tlsext_alpn_check_format(&supported)) 1805 goto err; 1806 1807 /* 1808 * Use first supported protocol as fallback. This is one way of doing 1809 * NPN's "opportunistic" protocol selection (see security considerations 1810 * in draft-agl-tls-nextprotoneg-04), and it is the documented behavior 1811 * of this API. For ALPN it's the callback's responsibility to fail on 1812 * OPENSSL_NPN_NO_OVERLAP. 1813 */ 1814 1815 if (!CBS_get_u8_length_prefixed(&supported, &supported_proto)) 1816 goto err; 1817 1818 *out = (unsigned char *)CBS_data(&supported_proto); 1819 *outlen = CBS_len(&supported_proto); 1820 1821 /* Now check that the peer list is well-formed. */ 1822 CBS_init(&peer, peer_list, peer_list_len); 1823 if (!tlsext_alpn_check_format(&peer)) 1824 goto err; 1825 1826 /* 1827 * Walk the peer list and select the first protocol that appears in 1828 * the supported list. Thus we honor peer preference rather than local 1829 * preference contrary to a SHOULD in RFC 7301, section 3.2. 1830 */ 1831 while (CBS_len(&peer) > 0) { 1832 if (!CBS_get_u8_length_prefixed(&peer, &peer_proto)) 1833 goto err; 1834 1835 CBS_init(&supported, supported_list, supported_list_len); 1836 1837 while (CBS_len(&supported) > 0) { 1838 if (!CBS_get_u8_length_prefixed(&supported, 1839 &supported_proto)) 1840 goto err; 1841 1842 if (CBS_mem_equal(&supported_proto, 1843 CBS_data(&peer_proto), CBS_len(&peer_proto))) { 1844 *out = (unsigned char *)CBS_data(&peer_proto); 1845 *outlen = CBS_len(&peer_proto); 1846 1847 return OPENSSL_NPN_NEGOTIATED; 1848 } 1849 } 1850 } 1851 1852 err: 1853 return OPENSSL_NPN_NO_OVERLAP; 1854 } 1855 LSSL_ALIAS(SSL_select_next_proto); 1856 1857 /* SSL_get0_next_proto_negotiated is deprecated. */ 1858 void 1859 SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data, 1860 unsigned int *len) 1861 { 1862 *data = NULL; 1863 *len = 0; 1864 } 1865 LSSL_ALIAS(SSL_get0_next_proto_negotiated); 1866 1867 /* SSL_CTX_set_next_protos_advertised_cb is deprecated. */ 1868 void 1869 SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *ctx, int (*cb) (SSL *ssl, 1870 const unsigned char **out, unsigned int *outlen, void *arg), void *arg) 1871 { 1872 } 1873 LSSL_ALIAS(SSL_CTX_set_next_protos_advertised_cb); 1874 1875 /* SSL_CTX_set_next_proto_select_cb is deprecated. */ 1876 void 1877 SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx, int (*cb) (SSL *s, 1878 unsigned char **out, unsigned char *outlen, const unsigned char *in, 1879 unsigned int inlen, void *arg), void *arg) 1880 { 1881 } 1882 LSSL_ALIAS(SSL_CTX_set_next_proto_select_cb); 1883 1884 /* 1885 * SSL_CTX_set_alpn_protos sets the ALPN protocol list to the specified 1886 * protocols, which must be in wire-format (i.e. a series of non-empty, 1887 * 8-bit length-prefixed strings). Returns 0 on success. 1888 */ 1889 int 1890 SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos, 1891 unsigned int protos_len) 1892 { 1893 CBS cbs; 1894 int failed = 1; 1895 1896 if (protos == NULL) 1897 protos_len = 0; 1898 1899 CBS_init(&cbs, protos, protos_len); 1900 1901 if (protos_len > 0) { 1902 if (!tlsext_alpn_check_format(&cbs)) 1903 goto err; 1904 } 1905 1906 if (!CBS_stow(&cbs, &ctx->alpn_client_proto_list, 1907 &ctx->alpn_client_proto_list_len)) 1908 goto err; 1909 1910 failed = 0; 1911 1912 err: 1913 /* NOTE: Return values are the reverse of what you expect. */ 1914 return failed; 1915 } 1916 LSSL_ALIAS(SSL_CTX_set_alpn_protos); 1917 1918 /* 1919 * SSL_set_alpn_protos sets the ALPN protocol list to the specified 1920 * protocols, which must be in wire-format (i.e. a series of non-empty, 1921 * 8-bit length-prefixed strings). Returns 0 on success. 1922 */ 1923 int 1924 SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos, 1925 unsigned int protos_len) 1926 { 1927 CBS cbs; 1928 int failed = 1; 1929 1930 if (protos == NULL) 1931 protos_len = 0; 1932 1933 CBS_init(&cbs, protos, protos_len); 1934 1935 if (protos_len > 0) { 1936 if (!tlsext_alpn_check_format(&cbs)) 1937 goto err; 1938 } 1939 1940 if (!CBS_stow(&cbs, &ssl->alpn_client_proto_list, 1941 &ssl->alpn_client_proto_list_len)) 1942 goto err; 1943 1944 failed = 0; 1945 1946 err: 1947 /* NOTE: Return values are the reverse of what you expect. */ 1948 return failed; 1949 } 1950 LSSL_ALIAS(SSL_set_alpn_protos); 1951 1952 /* 1953 * SSL_CTX_set_alpn_select_cb sets a callback function that is called during 1954 * ClientHello processing in order to select an ALPN protocol from the 1955 * client's list of offered protocols. 1956 */ 1957 void 1958 SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx, 1959 int (*cb) (SSL *ssl, const unsigned char **out, unsigned char *outlen, 1960 const unsigned char *in, unsigned int inlen, void *arg), void *arg) 1961 { 1962 ctx->alpn_select_cb = cb; 1963 ctx->alpn_select_cb_arg = arg; 1964 } 1965 LSSL_ALIAS(SSL_CTX_set_alpn_select_cb); 1966 1967 /* 1968 * SSL_get0_alpn_selected gets the selected ALPN protocol (if any). On return 1969 * it sets data to point to len bytes of protocol name (not including the 1970 * leading length-prefix byte). If the server didn't respond with* a negotiated 1971 * protocol then len will be zero. 1972 */ 1973 void 1974 SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data, 1975 unsigned int *len) 1976 { 1977 *data = ssl->s3->alpn_selected; 1978 *len = ssl->s3->alpn_selected_len; 1979 } 1980 LSSL_ALIAS(SSL_get0_alpn_selected); 1981 1982 void 1983 SSL_set_psk_use_session_callback(SSL *s, SSL_psk_use_session_cb_func cb) 1984 { 1985 return; 1986 } 1987 LSSL_ALIAS(SSL_set_psk_use_session_callback); 1988 1989 int 1990 SSL_export_keying_material(SSL *s, unsigned char *out, size_t out_len, 1991 const char *label, size_t label_len, const unsigned char *context, 1992 size_t context_len, int use_context) 1993 { 1994 if (s->tls13 != NULL && s->version == TLS1_3_VERSION) { 1995 if (!use_context) { 1996 context = NULL; 1997 context_len = 0; 1998 } 1999 return tls13_exporter(s->tls13, label, label_len, context, 2000 context_len, out, out_len); 2001 } 2002 2003 return tls12_exporter(s, label, label_len, context, context_len, 2004 use_context, out, out_len); 2005 } 2006 LSSL_ALIAS(SSL_export_keying_material); 2007 2008 static unsigned long 2009 ssl_session_hash(const SSL_SESSION *a) 2010 { 2011 unsigned long l; 2012 2013 l = (unsigned long) 2014 ((unsigned int) a->session_id[0] )| 2015 ((unsigned int) a->session_id[1]<< 8L)| 2016 ((unsigned long)a->session_id[2]<<16L)| 2017 ((unsigned long)a->session_id[3]<<24L); 2018 return (l); 2019 } 2020 2021 /* 2022 * NB: If this function (or indeed the hash function which uses a sort of 2023 * coarser function than this one) is changed, ensure 2024 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being 2025 * able to construct an SSL_SESSION that will collide with any existing session 2026 * with a matching session ID. 2027 */ 2028 static int 2029 ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) 2030 { 2031 if (a->ssl_version != b->ssl_version) 2032 return (1); 2033 if (a->session_id_length != b->session_id_length) 2034 return (1); 2035 if (timingsafe_memcmp(a->session_id, b->session_id, a->session_id_length) != 0) 2036 return (1); 2037 return (0); 2038 } 2039 2040 /* 2041 * These wrapper functions should remain rather than redeclaring 2042 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each 2043 * variable. The reason is that the functions aren't static, they're exposed via 2044 * ssl.h. 2045 */ 2046 static unsigned long 2047 ssl_session_LHASH_HASH(const void *arg) 2048 { 2049 const SSL_SESSION *a = arg; 2050 2051 return ssl_session_hash(a); 2052 } 2053 2054 static int 2055 ssl_session_LHASH_COMP(const void *arg1, const void *arg2) 2056 { 2057 const SSL_SESSION *a = arg1; 2058 const SSL_SESSION *b = arg2; 2059 2060 return ssl_session_cmp(a, b); 2061 } 2062 2063 SSL_CTX * 2064 SSL_CTX_new(const SSL_METHOD *meth) 2065 { 2066 SSL_CTX *ret; 2067 2068 if (!OPENSSL_init_ssl(0, NULL)) { 2069 SSLerrorx(SSL_R_LIBRARY_BUG); 2070 return (NULL); 2071 } 2072 2073 if (meth == NULL) { 2074 SSLerrorx(SSL_R_NULL_SSL_METHOD_PASSED); 2075 return (NULL); 2076 } 2077 2078 if ((ret = calloc(1, sizeof(*ret))) == NULL) { 2079 SSLerrorx(ERR_R_MALLOC_FAILURE); 2080 return (NULL); 2081 } 2082 2083 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) { 2084 SSLerrorx(SSL_R_X509_VERIFICATION_SETUP_PROBLEMS); 2085 goto err; 2086 } 2087 2088 ret->method = meth; 2089 ret->min_tls_version = meth->min_tls_version; 2090 ret->max_tls_version = meth->max_tls_version; 2091 ret->min_proto_version = 0; 2092 ret->max_proto_version = 0; 2093 ret->mode = SSL_MODE_AUTO_RETRY; 2094 2095 ret->cert_store = NULL; 2096 ret->session_cache_mode = SSL_SESS_CACHE_SERVER; 2097 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; 2098 ret->session_cache_head = NULL; 2099 ret->session_cache_tail = NULL; 2100 2101 /* We take the system default */ 2102 ret->session_timeout = ssl_get_default_timeout(); 2103 2104 ret->new_session_cb = NULL; 2105 ret->remove_session_cb = NULL; 2106 ret->get_session_cb = NULL; 2107 ret->generate_session_id = NULL; 2108 2109 memset((char *)&ret->stats, 0, sizeof(ret->stats)); 2110 2111 ret->references = 1; 2112 ret->quiet_shutdown = 0; 2113 2114 ret->info_callback = NULL; 2115 2116 ret->app_verify_callback = NULL; 2117 ret->app_verify_arg = NULL; 2118 2119 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT; 2120 ret->read_ahead = 0; 2121 ret->msg_callback = NULL; 2122 ret->msg_callback_arg = NULL; 2123 ret->verify_mode = SSL_VERIFY_NONE; 2124 ret->sid_ctx_length = 0; 2125 ret->default_verify_callback = NULL; 2126 2127 if ((ret->cert = ssl_cert_new()) == NULL) 2128 goto err; 2129 2130 ret->default_passwd_callback = NULL; 2131 ret->default_passwd_callback_userdata = NULL; 2132 ret->client_cert_cb = NULL; 2133 ret->app_gen_cookie_cb = NULL; 2134 ret->app_verify_cookie_cb = NULL; 2135 2136 ret->sessions = lh_SSL_SESSION_new(); 2137 if (ret->sessions == NULL) 2138 goto err; 2139 ret->cert_store = X509_STORE_new(); 2140 if (ret->cert_store == NULL) 2141 goto err; 2142 2143 ssl_create_cipher_list(ret->method, &ret->cipher_list, 2144 NULL, SSL_DEFAULT_CIPHER_LIST, ret->cert); 2145 if (ret->cipher_list == NULL || 2146 sk_SSL_CIPHER_num(ret->cipher_list) <= 0) { 2147 SSLerrorx(SSL_R_LIBRARY_HAS_NO_CIPHERS); 2148 goto err2; 2149 } 2150 2151 ret->param = X509_VERIFY_PARAM_new(); 2152 if (!ret->param) 2153 goto err; 2154 2155 if ((ret->client_CA = sk_X509_NAME_new_null()) == NULL) 2156 goto err; 2157 2158 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data); 2159 2160 ret->extra_certs = NULL; 2161 2162 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; 2163 2164 ret->tlsext_servername_callback = 0; 2165 ret->tlsext_servername_arg = NULL; 2166 2167 /* Setup RFC4507 ticket keys */ 2168 arc4random_buf(ret->tlsext_tick_key_name, 16); 2169 arc4random_buf(ret->tlsext_tick_hmac_key, 16); 2170 arc4random_buf(ret->tlsext_tick_aes_key, 16); 2171 2172 ret->tlsext_status_cb = 0; 2173 ret->tlsext_status_arg = NULL; 2174 2175 /* 2176 * Default is to connect to non-RI servers. When RI is more widely 2177 * deployed might change this. 2178 */ 2179 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT; 2180 2181 return (ret); 2182 err: 2183 SSLerrorx(ERR_R_MALLOC_FAILURE); 2184 err2: 2185 SSL_CTX_free(ret); 2186 return (NULL); 2187 } 2188 LSSL_ALIAS(SSL_CTX_new); 2189 2190 void 2191 SSL_CTX_free(SSL_CTX *ctx) 2192 { 2193 int i; 2194 2195 if (ctx == NULL) 2196 return; 2197 2198 i = CRYPTO_add(&ctx->references, -1, CRYPTO_LOCK_SSL_CTX); 2199 if (i > 0) 2200 return; 2201 2202 X509_VERIFY_PARAM_free(ctx->param); 2203 2204 /* 2205 * Free internal session cache. However: the remove_cb() may reference 2206 * the ex_data of SSL_CTX, thus the ex_data store can only be removed 2207 * after the sessions were flushed. 2208 * As the ex_data handling routines might also touch the session cache, 2209 * the most secure solution seems to be: empty (flush) the cache, then 2210 * free ex_data, then finally free the cache. 2211 * (See ticket [openssl.org #212].) 2212 */ 2213 if (ctx->sessions != NULL) 2214 SSL_CTX_flush_sessions(ctx, 0); 2215 2216 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ctx, &ctx->ex_data); 2217 2218 lh_SSL_SESSION_free(ctx->sessions); 2219 2220 X509_STORE_free(ctx->cert_store); 2221 sk_SSL_CIPHER_free(ctx->cipher_list); 2222 sk_SSL_CIPHER_free(ctx->cipher_list_tls13); 2223 ssl_cert_free(ctx->cert); 2224 sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free); 2225 sk_X509_pop_free(ctx->extra_certs, X509_free); 2226 2227 #ifndef OPENSSL_NO_SRTP 2228 if (ctx->srtp_profiles) 2229 sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles); 2230 #endif 2231 2232 free(ctx->tlsext_ecpointformatlist); 2233 free(ctx->tlsext_supportedgroups); 2234 2235 free(ctx->alpn_client_proto_list); 2236 2237 free(ctx); 2238 } 2239 LSSL_ALIAS(SSL_CTX_free); 2240 2241 int 2242 SSL_CTX_up_ref(SSL_CTX *ctx) 2243 { 2244 return CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX) > 1; 2245 } 2246 LSSL_ALIAS(SSL_CTX_up_ref); 2247 2248 pem_password_cb * 2249 SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx) 2250 { 2251 return (ctx->default_passwd_callback); 2252 } 2253 LSSL_ALIAS(SSL_CTX_get_default_passwd_cb); 2254 2255 void 2256 SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) 2257 { 2258 ctx->default_passwd_callback = cb; 2259 } 2260 LSSL_ALIAS(SSL_CTX_set_default_passwd_cb); 2261 2262 void * 2263 SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx) 2264 { 2265 return ctx->default_passwd_callback_userdata; 2266 } 2267 LSSL_ALIAS(SSL_CTX_get_default_passwd_cb_userdata); 2268 2269 void 2270 SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) 2271 { 2272 ctx->default_passwd_callback_userdata = u; 2273 } 2274 LSSL_ALIAS(SSL_CTX_set_default_passwd_cb_userdata); 2275 2276 void 2277 SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, 2278 int (*cb)(X509_STORE_CTX *, void *), void *arg) 2279 { 2280 ctx->app_verify_callback = cb; 2281 ctx->app_verify_arg = arg; 2282 } 2283 LSSL_ALIAS(SSL_CTX_set_cert_verify_callback); 2284 2285 void 2286 SSL_CTX_set_verify(SSL_CTX *ctx, int mode, int (*cb)(int, X509_STORE_CTX *)) 2287 { 2288 ctx->verify_mode = mode; 2289 ctx->default_verify_callback = cb; 2290 } 2291 LSSL_ALIAS(SSL_CTX_set_verify); 2292 2293 void 2294 SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) 2295 { 2296 X509_VERIFY_PARAM_set_depth(ctx->param, depth); 2297 } 2298 LSSL_ALIAS(SSL_CTX_set_verify_depth); 2299 2300 void 2301 ssl_set_cert_masks(SSL_CERT *c, const SSL_CIPHER *cipher) 2302 { 2303 unsigned long mask_a, mask_k; 2304 SSL_CERT_PKEY *cpk; 2305 2306 if (c == NULL) 2307 return; 2308 2309 mask_a = SSL_aNULL | SSL_aTLS1_3; 2310 mask_k = SSL_kECDHE | SSL_kTLS1_3; 2311 2312 if (c->dhe_params != NULL || c->dhe_params_cb != NULL || 2313 c->dhe_params_auto != 0) 2314 mask_k |= SSL_kDHE; 2315 2316 cpk = &(c->pkeys[SSL_PKEY_ECC]); 2317 if (cpk->x509 != NULL && cpk->privatekey != NULL) { 2318 /* Key usage, if present, must allow signing. */ 2319 if (X509_get_key_usage(cpk->x509) & X509v3_KU_DIGITAL_SIGNATURE) 2320 mask_a |= SSL_aECDSA; 2321 } 2322 2323 cpk = &(c->pkeys[SSL_PKEY_RSA]); 2324 if (cpk->x509 != NULL && cpk->privatekey != NULL) { 2325 mask_a |= SSL_aRSA; 2326 mask_k |= SSL_kRSA; 2327 } 2328 2329 c->mask_k = mask_k; 2330 c->mask_a = mask_a; 2331 c->valid = 1; 2332 } 2333 2334 /* See if this handshake is using an ECC cipher suite. */ 2335 int 2336 ssl_using_ecc_cipher(SSL *s) 2337 { 2338 unsigned long alg_a, alg_k; 2339 2340 alg_a = s->s3->hs.cipher->algorithm_auth; 2341 alg_k = s->s3->hs.cipher->algorithm_mkey; 2342 2343 return s->session->tlsext_ecpointformatlist != NULL && 2344 s->session->tlsext_ecpointformatlist_length > 0 && 2345 ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)); 2346 } 2347 2348 int 2349 ssl_check_srvr_ecc_cert_and_alg(SSL *s, X509 *x) 2350 { 2351 const SSL_CIPHER *cs = s->s3->hs.cipher; 2352 unsigned long alg_a; 2353 2354 alg_a = cs->algorithm_auth; 2355 2356 if (alg_a & SSL_aECDSA) { 2357 /* Key usage, if present, must allow signing. */ 2358 if (!(X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE)) { 2359 SSLerror(s, SSL_R_ECC_CERT_NOT_FOR_SIGNING); 2360 return (0); 2361 } 2362 } 2363 2364 return (1); 2365 } 2366 2367 SSL_CERT_PKEY * 2368 ssl_get_server_send_pkey(const SSL *s) 2369 { 2370 unsigned long alg_a; 2371 SSL_CERT *c; 2372 int i; 2373 2374 c = s->cert; 2375 ssl_set_cert_masks(c, s->s3->hs.cipher); 2376 2377 alg_a = s->s3->hs.cipher->algorithm_auth; 2378 2379 if (alg_a & SSL_aECDSA) { 2380 i = SSL_PKEY_ECC; 2381 } else if (alg_a & SSL_aRSA) { 2382 i = SSL_PKEY_RSA; 2383 } else { /* if (alg_a & SSL_aNULL) */ 2384 SSLerror(s, ERR_R_INTERNAL_ERROR); 2385 return (NULL); 2386 } 2387 2388 return (c->pkeys + i); 2389 } 2390 2391 EVP_PKEY * 2392 ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher, const EVP_MD **pmd, 2393 const struct ssl_sigalg **sap) 2394 { 2395 const struct ssl_sigalg *sigalg = NULL; 2396 EVP_PKEY *pkey = NULL; 2397 unsigned long alg_a; 2398 SSL_CERT *c; 2399 int idx = -1; 2400 2401 alg_a = cipher->algorithm_auth; 2402 c = s->cert; 2403 2404 if (alg_a & SSL_aRSA) { 2405 idx = SSL_PKEY_RSA; 2406 } else if ((alg_a & SSL_aECDSA) && 2407 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) 2408 idx = SSL_PKEY_ECC; 2409 if (idx == -1) { 2410 SSLerror(s, ERR_R_INTERNAL_ERROR); 2411 return (NULL); 2412 } 2413 2414 pkey = c->pkeys[idx].privatekey; 2415 if ((sigalg = ssl_sigalg_select(s, pkey)) == NULL) { 2416 SSLerror(s, SSL_R_SIGNATURE_ALGORITHMS_ERROR); 2417 return (NULL); 2418 } 2419 *pmd = sigalg->md(); 2420 *sap = sigalg; 2421 2422 return (pkey); 2423 } 2424 2425 size_t 2426 ssl_dhe_params_auto_key_bits(SSL *s) 2427 { 2428 SSL_CERT_PKEY *cpk; 2429 int key_bits; 2430 2431 if (s->cert->dhe_params_auto == 2) { 2432 key_bits = 1024; 2433 } else if (s->s3->hs.cipher->algorithm_auth & SSL_aNULL) { 2434 key_bits = 1024; 2435 if (s->s3->hs.cipher->strength_bits == 256) 2436 key_bits = 3072; 2437 } else { 2438 if ((cpk = ssl_get_server_send_pkey(s)) == NULL) 2439 return 0; 2440 if (cpk->privatekey == NULL || 2441 EVP_PKEY_get0_RSA(cpk->privatekey) == NULL) 2442 return 0; 2443 if ((key_bits = EVP_PKEY_bits(cpk->privatekey)) <= 0) 2444 return 0; 2445 } 2446 2447 return key_bits; 2448 } 2449 2450 static int 2451 ssl_should_update_external_cache(SSL *s, int mode) 2452 { 2453 int cache_mode; 2454 2455 cache_mode = s->session_ctx->session_cache_mode; 2456 2457 /* Don't cache if mode says not to */ 2458 if ((cache_mode & mode) == 0) 2459 return 0; 2460 2461 /* if it is not already cached, cache it */ 2462 if (!s->hit) 2463 return 1; 2464 2465 /* If it's TLS 1.3, do it to match OpenSSL */ 2466 if (s->s3->hs.negotiated_tls_version >= TLS1_3_VERSION) 2467 return 1; 2468 2469 return 0; 2470 } 2471 2472 static int 2473 ssl_should_update_internal_cache(SSL *s, int mode) 2474 { 2475 int cache_mode; 2476 2477 cache_mode = s->session_ctx->session_cache_mode; 2478 2479 /* Don't cache if mode says not to */ 2480 if ((cache_mode & mode) == 0) 2481 return 0; 2482 2483 /* If it is already cached, don't cache it again */ 2484 if (s->hit) 2485 return 0; 2486 2487 if ((cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) != 0) 2488 return 0; 2489 2490 /* If we are lesser than TLS 1.3, Cache it. */ 2491 if (s->s3->hs.negotiated_tls_version < TLS1_3_VERSION) 2492 return 1; 2493 2494 /* Below this we consider TLS 1.3 or later */ 2495 2496 /* If it's not a server, add it? OpenSSL does this. */ 2497 if (!s->server) 2498 return 1; 2499 2500 /* XXX if we support early data / PSK need to add */ 2501 2502 /* 2503 * If we have the remove session callback, we will want 2504 * to know about this even if it's a stateless ticket 2505 * from 1.3 so we can know when it is removed. 2506 */ 2507 if (s->session_ctx->remove_session_cb != NULL) 2508 return 1; 2509 2510 /* If we have set OP_NO_TICKET, cache it. */ 2511 if ((s->options & SSL_OP_NO_TICKET) != 0) 2512 return 1; 2513 2514 /* Otherwise do not cache */ 2515 return 0; 2516 } 2517 2518 void 2519 ssl_update_cache(SSL *s, int mode) 2520 { 2521 int cache_mode, do_callback; 2522 2523 if (s->session->session_id_length == 0) 2524 return; 2525 2526 cache_mode = s->session_ctx->session_cache_mode; 2527 do_callback = ssl_should_update_external_cache(s, mode); 2528 2529 if (ssl_should_update_internal_cache(s, mode)) { 2530 /* 2531 * XXX should we fail if the add to the internal cache 2532 * fails? OpenSSL doesn't care.. 2533 */ 2534 (void) SSL_CTX_add_session(s->session_ctx, s->session); 2535 } 2536 2537 /* 2538 * Update the "external cache" by calling the new session 2539 * callback if present, even with TLS 1.3 without early data 2540 * "because some application just want to know about the 2541 * creation of a session and aren't doing a full cache". 2542 * Apparently, if they are doing a full cache, they'll have 2543 * some fun, but we endeavour to give application writers the 2544 * same glorious experience they expect from OpenSSL which 2545 * does it this way. 2546 */ 2547 if (do_callback && s->session_ctx->new_session_cb != NULL) { 2548 CRYPTO_add(&s->session->references, 1, CRYPTO_LOCK_SSL_SESSION); 2549 if (!s->session_ctx->new_session_cb(s, s->session)) 2550 SSL_SESSION_free(s->session); 2551 } 2552 2553 /* Auto flush every 255 connections. */ 2554 if (!(cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) && 2555 (cache_mode & mode) != 0) { 2556 int connections; 2557 if (mode & SSL_SESS_CACHE_CLIENT) 2558 connections = s->session_ctx->stats.sess_connect_good; 2559 else 2560 connections = s->session_ctx->stats.sess_accept_good; 2561 if ((connections & 0xff) == 0xff) 2562 SSL_CTX_flush_sessions(s->session_ctx, time(NULL)); 2563 } 2564 } 2565 2566 const SSL_METHOD * 2567 SSL_get_ssl_method(SSL *s) 2568 { 2569 return (s->method); 2570 } 2571 LSSL_ALIAS(SSL_get_ssl_method); 2572 2573 int 2574 SSL_set_ssl_method(SSL *s, const SSL_METHOD *method) 2575 { 2576 int (*handshake_func)(SSL *) = NULL; 2577 int ret = 1; 2578 2579 if (s->method == method) 2580 return (ret); 2581 2582 if (s->handshake_func == s->method->ssl_connect) 2583 handshake_func = method->ssl_connect; 2584 else if (s->handshake_func == s->method->ssl_accept) 2585 handshake_func = method->ssl_accept; 2586 2587 if (s->method->version == method->version) { 2588 s->method = method; 2589 } else { 2590 s->method->ssl_free(s); 2591 s->method = method; 2592 ret = s->method->ssl_new(s); 2593 } 2594 s->handshake_func = handshake_func; 2595 2596 return (ret); 2597 } 2598 LSSL_ALIAS(SSL_set_ssl_method); 2599 2600 int 2601 SSL_get_error(const SSL *s, int i) 2602 { 2603 unsigned long l; 2604 int reason; 2605 BIO *bio; 2606 2607 if (i > 0) 2608 return (SSL_ERROR_NONE); 2609 2610 /* 2611 * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake 2612 * etc, where we do encode the error. 2613 */ 2614 if ((l = ERR_peek_error()) != 0) { 2615 if (ERR_GET_LIB(l) == ERR_LIB_SYS) 2616 return (SSL_ERROR_SYSCALL); 2617 else 2618 return (SSL_ERROR_SSL); 2619 } 2620 2621 if (SSL_want_read(s)) { 2622 bio = SSL_get_rbio(s); 2623 if (BIO_should_read(bio)) { 2624 return (SSL_ERROR_WANT_READ); 2625 } else if (BIO_should_write(bio)) { 2626 /* 2627 * This one doesn't make too much sense... We never 2628 * try to write to the rbio, and an application 2629 * program where rbio and wbio are separate couldn't 2630 * even know what it should wait for. However if we 2631 * ever set s->rwstate incorrectly (so that we have 2632 * SSL_want_read(s) instead of SSL_want_write(s)) 2633 * and rbio and wbio *are* the same, this test works 2634 * around that bug; so it might be safer to keep it. 2635 */ 2636 return (SSL_ERROR_WANT_WRITE); 2637 } else if (BIO_should_io_special(bio)) { 2638 reason = BIO_get_retry_reason(bio); 2639 if (reason == BIO_RR_CONNECT) 2640 return (SSL_ERROR_WANT_CONNECT); 2641 else if (reason == BIO_RR_ACCEPT) 2642 return (SSL_ERROR_WANT_ACCEPT); 2643 else 2644 return (SSL_ERROR_SYSCALL); /* unknown */ 2645 } 2646 } 2647 2648 if (SSL_want_write(s)) { 2649 bio = SSL_get_wbio(s); 2650 if (BIO_should_write(bio)) { 2651 return (SSL_ERROR_WANT_WRITE); 2652 } else if (BIO_should_read(bio)) { 2653 /* 2654 * See above (SSL_want_read(s) with 2655 * BIO_should_write(bio)) 2656 */ 2657 return (SSL_ERROR_WANT_READ); 2658 } else if (BIO_should_io_special(bio)) { 2659 reason = BIO_get_retry_reason(bio); 2660 if (reason == BIO_RR_CONNECT) 2661 return (SSL_ERROR_WANT_CONNECT); 2662 else if (reason == BIO_RR_ACCEPT) 2663 return (SSL_ERROR_WANT_ACCEPT); 2664 else 2665 return (SSL_ERROR_SYSCALL); 2666 } 2667 } 2668 2669 if (SSL_want_x509_lookup(s)) 2670 return (SSL_ERROR_WANT_X509_LOOKUP); 2671 2672 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) && 2673 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) 2674 return (SSL_ERROR_ZERO_RETURN); 2675 2676 return (SSL_ERROR_SYSCALL); 2677 } 2678 LSSL_ALIAS(SSL_get_error); 2679 2680 int 2681 SSL_CTX_set_quic_method(SSL_CTX *ctx, const SSL_QUIC_METHOD *quic_method) 2682 { 2683 if (ctx->method->dtls) 2684 return 0; 2685 2686 ctx->quic_method = quic_method; 2687 2688 return 1; 2689 } 2690 LSSL_ALIAS(SSL_CTX_set_quic_method); 2691 2692 int 2693 SSL_set_quic_method(SSL *ssl, const SSL_QUIC_METHOD *quic_method) 2694 { 2695 if (ssl->method->dtls) 2696 return 0; 2697 2698 ssl->quic_method = quic_method; 2699 2700 return 1; 2701 } 2702 LSSL_ALIAS(SSL_set_quic_method); 2703 2704 size_t 2705 SSL_quic_max_handshake_flight_len(const SSL *ssl, 2706 enum ssl_encryption_level_t level) 2707 { 2708 size_t flight_len; 2709 2710 /* Limit flights to 16K when there are no large certificate messages. */ 2711 flight_len = 16384; 2712 2713 switch (level) { 2714 case ssl_encryption_initial: 2715 return flight_len; 2716 2717 case ssl_encryption_early_data: 2718 /* QUIC does not send EndOfEarlyData. */ 2719 return 0; 2720 2721 case ssl_encryption_handshake: 2722 if (ssl->server) { 2723 /* 2724 * Servers may receive Certificate message if configured 2725 * to request client certificates. 2726 */ 2727 if ((SSL_get_verify_mode(ssl) & SSL_VERIFY_PEER) != 0 && 2728 ssl->max_cert_list > flight_len) 2729 flight_len = ssl->max_cert_list; 2730 } else { 2731 /* 2732 * Clients may receive both Certificate message and a 2733 * CertificateRequest message. 2734 */ 2735 if (ssl->max_cert_list * 2 > flight_len) 2736 flight_len = ssl->max_cert_list * 2; 2737 } 2738 return flight_len; 2739 case ssl_encryption_application: 2740 /* 2741 * Note there is not actually a bound on the number of 2742 * NewSessionTickets one may send in a row. This level may need 2743 * more involved flow control. 2744 */ 2745 return flight_len; 2746 } 2747 2748 return 0; 2749 } 2750 LSSL_ALIAS(SSL_quic_max_handshake_flight_len); 2751 2752 enum ssl_encryption_level_t 2753 SSL_quic_read_level(const SSL *ssl) 2754 { 2755 return ssl->s3->hs.tls13.quic_read_level; 2756 } 2757 LSSL_ALIAS(SSL_quic_read_level); 2758 2759 enum ssl_encryption_level_t 2760 SSL_quic_write_level(const SSL *ssl) 2761 { 2762 return ssl->s3->hs.tls13.quic_write_level; 2763 } 2764 LSSL_ALIAS(SSL_quic_write_level); 2765 2766 int 2767 SSL_provide_quic_data(SSL *ssl, enum ssl_encryption_level_t level, 2768 const uint8_t *data, size_t len) 2769 { 2770 if (!SSL_is_quic(ssl)) { 2771 SSLerror(ssl, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2772 return 0; 2773 } 2774 2775 if (level != SSL_quic_read_level(ssl)) { 2776 SSLerror(ssl, SSL_R_WRONG_ENCRYPTION_LEVEL_RECEIVED); 2777 return 0; 2778 } 2779 2780 if (ssl->s3->hs.tls13.quic_read_buffer == NULL) { 2781 ssl->s3->hs.tls13.quic_read_buffer = tls_buffer_new(0); 2782 if (ssl->s3->hs.tls13.quic_read_buffer == NULL) { 2783 SSLerror(ssl, ERR_R_MALLOC_FAILURE); 2784 return 0; 2785 } 2786 } 2787 2788 /* XXX - note that this does not currently downsize. */ 2789 tls_buffer_set_capacity_limit(ssl->s3->hs.tls13.quic_read_buffer, 2790 SSL_quic_max_handshake_flight_len(ssl, level)); 2791 2792 /* 2793 * XXX - an append that fails due to exceeding capacity should set 2794 * SSL_R_EXCESSIVE_MESSAGE_SIZE. 2795 */ 2796 return tls_buffer_append(ssl->s3->hs.tls13.quic_read_buffer, data, len); 2797 } 2798 LSSL_ALIAS(SSL_provide_quic_data); 2799 2800 int 2801 SSL_process_quic_post_handshake(SSL *ssl) 2802 { 2803 /* XXX - this needs to run PHH received. */ 2804 return 1; 2805 } 2806 LSSL_ALIAS(SSL_process_quic_post_handshake); 2807 2808 int 2809 SSL_do_handshake(SSL *s) 2810 { 2811 if (s->handshake_func == NULL) { 2812 SSLerror(s, SSL_R_CONNECTION_TYPE_NOT_SET); 2813 return (-1); 2814 } 2815 2816 s->method->ssl_renegotiate_check(s); 2817 2818 if (!SSL_in_init(s) && !SSL_in_before(s)) 2819 return 1; 2820 2821 return s->handshake_func(s); 2822 } 2823 LSSL_ALIAS(SSL_do_handshake); 2824 2825 /* 2826 * For the next 2 functions, SSL_clear() sets shutdown and so 2827 * one of these calls will reset it 2828 */ 2829 void 2830 SSL_set_accept_state(SSL *s) 2831 { 2832 s->server = 1; 2833 s->shutdown = 0; 2834 s->s3->hs.state = SSL_ST_ACCEPT|SSL_ST_BEFORE; 2835 s->handshake_func = s->method->ssl_accept; 2836 ssl_clear_cipher_state(s); 2837 } 2838 LSSL_ALIAS(SSL_set_accept_state); 2839 2840 void 2841 SSL_set_connect_state(SSL *s) 2842 { 2843 s->server = 0; 2844 s->shutdown = 0; 2845 s->s3->hs.state = SSL_ST_CONNECT|SSL_ST_BEFORE; 2846 s->handshake_func = s->method->ssl_connect; 2847 ssl_clear_cipher_state(s); 2848 } 2849 LSSL_ALIAS(SSL_set_connect_state); 2850 2851 int 2852 ssl_undefined_function(SSL *s) 2853 { 2854 SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2855 return (0); 2856 } 2857 2858 int 2859 ssl_undefined_void_function(void) 2860 { 2861 SSLerrorx(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2862 return (0); 2863 } 2864 2865 int 2866 ssl_undefined_const_function(const SSL *s) 2867 { 2868 SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2869 return (0); 2870 } 2871 2872 const char * 2873 ssl_version_string(int ver) 2874 { 2875 switch (ver) { 2876 case TLS1_VERSION: 2877 return (SSL_TXT_TLSV1); 2878 case TLS1_1_VERSION: 2879 return (SSL_TXT_TLSV1_1); 2880 case TLS1_2_VERSION: 2881 return (SSL_TXT_TLSV1_2); 2882 case TLS1_3_VERSION: 2883 return (SSL_TXT_TLSV1_3); 2884 case DTLS1_VERSION: 2885 return (SSL_TXT_DTLS1); 2886 case DTLS1_2_VERSION: 2887 return (SSL_TXT_DTLS1_2); 2888 default: 2889 return ("unknown"); 2890 } 2891 } 2892 2893 const char * 2894 SSL_get_version(const SSL *s) 2895 { 2896 return ssl_version_string(s->version); 2897 } 2898 LSSL_ALIAS(SSL_get_version); 2899 2900 SSL * 2901 SSL_dup(SSL *s) 2902 { 2903 STACK_OF(X509_NAME) *sk; 2904 X509_NAME *xn; 2905 SSL *ret; 2906 int i; 2907 2908 if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL) 2909 goto err; 2910 2911 ret->version = s->version; 2912 ret->method = s->method; 2913 2914 if (s->session != NULL) { 2915 if (!SSL_copy_session_id(ret, s)) 2916 goto err; 2917 } else { 2918 /* 2919 * No session has been established yet, so we have to expect 2920 * that s->cert or ret->cert will be changed later -- 2921 * they should not both point to the same object, 2922 * and thus we can't use SSL_copy_session_id. 2923 */ 2924 2925 ret->method->ssl_free(ret); 2926 ret->method = s->method; 2927 ret->method->ssl_new(ret); 2928 2929 ssl_cert_free(ret->cert); 2930 if ((ret->cert = ssl_cert_dup(s->cert)) == NULL) 2931 goto err; 2932 2933 if (!SSL_set_session_id_context(ret, s->sid_ctx, 2934 s->sid_ctx_length)) 2935 goto err; 2936 } 2937 2938 ret->options = s->options; 2939 ret->mode = s->mode; 2940 SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s)); 2941 SSL_set_read_ahead(ret, SSL_get_read_ahead(s)); 2942 ret->msg_callback = s->msg_callback; 2943 ret->msg_callback_arg = s->msg_callback_arg; 2944 SSL_set_verify(ret, SSL_get_verify_mode(s), 2945 SSL_get_verify_callback(s)); 2946 SSL_set_verify_depth(ret, SSL_get_verify_depth(s)); 2947 ret->generate_session_id = s->generate_session_id; 2948 2949 SSL_set_info_callback(ret, SSL_get_info_callback(s)); 2950 2951 /* copy app data, a little dangerous perhaps */ 2952 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, 2953 &ret->ex_data, &s->ex_data)) 2954 goto err; 2955 2956 /* setup rbio, and wbio */ 2957 if (s->rbio != NULL) { 2958 if (!BIO_dup_state(s->rbio,(char *)&ret->rbio)) 2959 goto err; 2960 } 2961 if (s->wbio != NULL) { 2962 if (s->wbio != s->rbio) { 2963 if (!BIO_dup_state(s->wbio,(char *)&ret->wbio)) 2964 goto err; 2965 } else 2966 ret->wbio = ret->rbio; 2967 } 2968 ret->rwstate = s->rwstate; 2969 ret->in_handshake = s->in_handshake; 2970 ret->handshake_func = s->handshake_func; 2971 ret->server = s->server; 2972 ret->renegotiate = s->renegotiate; 2973 ret->new_session = s->new_session; 2974 ret->quiet_shutdown = s->quiet_shutdown; 2975 ret->shutdown = s->shutdown; 2976 /* SSL_dup does not really work at any state, though */ 2977 ret->s3->hs.state = s->s3->hs.state; 2978 ret->rstate = s->rstate; 2979 2980 /* 2981 * Would have to copy ret->init_buf, ret->init_msg, ret->init_num, 2982 * ret->init_off 2983 */ 2984 ret->init_num = 0; 2985 2986 ret->hit = s->hit; 2987 2988 X509_VERIFY_PARAM_inherit(ret->param, s->param); 2989 2990 if (s->cipher_list != NULL) { 2991 if ((ret->cipher_list = 2992 sk_SSL_CIPHER_dup(s->cipher_list)) == NULL) 2993 goto err; 2994 } 2995 if (s->cipher_list_tls13 != NULL) { 2996 if ((ret->cipher_list_tls13 = 2997 sk_SSL_CIPHER_dup(s->cipher_list_tls13)) == NULL) 2998 goto err; 2999 } 3000 3001 /* Dup the client_CA list */ 3002 if (s->client_CA != NULL) { 3003 if ((sk = sk_X509_NAME_dup(s->client_CA)) == NULL) goto err; 3004 ret->client_CA = sk; 3005 for (i = 0; i < sk_X509_NAME_num(sk); i++) { 3006 xn = sk_X509_NAME_value(sk, i); 3007 if (sk_X509_NAME_set(sk, i, 3008 X509_NAME_dup(xn)) == NULL) { 3009 X509_NAME_free(xn); 3010 goto err; 3011 } 3012 } 3013 } 3014 3015 return ret; 3016 err: 3017 SSL_free(ret); 3018 return NULL; 3019 } 3020 LSSL_ALIAS(SSL_dup); 3021 3022 void 3023 ssl_clear_cipher_state(SSL *s) 3024 { 3025 tls12_record_layer_clear_read_state(s->rl); 3026 tls12_record_layer_clear_write_state(s->rl); 3027 } 3028 3029 void 3030 ssl_info_callback(const SSL *s, int type, int value) 3031 { 3032 ssl_info_callback_fn *cb; 3033 3034 if ((cb = s->info_callback) == NULL) 3035 cb = s->ctx->info_callback; 3036 if (cb != NULL) 3037 cb(s, type, value); 3038 } 3039 3040 void 3041 ssl_msg_callback(SSL *s, int is_write, int content_type, 3042 const void *msg_buf, size_t msg_len) 3043 { 3044 if (s->msg_callback == NULL) 3045 return; 3046 3047 s->msg_callback(is_write, s->version, content_type, 3048 msg_buf, msg_len, s, s->msg_callback_arg); 3049 } 3050 3051 void 3052 ssl_msg_callback_cbs(SSL *s, int is_write, int content_type, CBS *cbs) 3053 { 3054 ssl_msg_callback(s, is_write, content_type, CBS_data(cbs), CBS_len(cbs)); 3055 } 3056 3057 /* Fix this function so that it takes an optional type parameter */ 3058 X509 * 3059 SSL_get_certificate(const SSL *s) 3060 { 3061 return (s->cert->key->x509); 3062 } 3063 LSSL_ALIAS(SSL_get_certificate); 3064 3065 /* Fix this function so that it takes an optional type parameter */ 3066 EVP_PKEY * 3067 SSL_get_privatekey(const SSL *s) 3068 { 3069 return (s->cert->key->privatekey); 3070 } 3071 LSSL_ALIAS(SSL_get_privatekey); 3072 3073 const SSL_CIPHER * 3074 SSL_get_current_cipher(const SSL *s) 3075 { 3076 return s->s3->hs.cipher; 3077 } 3078 LSSL_ALIAS(SSL_get_current_cipher); 3079 3080 const void * 3081 SSL_get_current_compression(SSL *s) 3082 { 3083 return (NULL); 3084 } 3085 LSSL_ALIAS(SSL_get_current_compression); 3086 3087 const void * 3088 SSL_get_current_expansion(SSL *s) 3089 { 3090 return (NULL); 3091 } 3092 LSSL_ALIAS(SSL_get_current_expansion); 3093 3094 size_t 3095 SSL_get_client_random(const SSL *s, unsigned char *out, size_t max_out) 3096 { 3097 size_t len = sizeof(s->s3->client_random); 3098 3099 if (out == NULL) 3100 return len; 3101 3102 if (len > max_out) 3103 len = max_out; 3104 3105 memcpy(out, s->s3->client_random, len); 3106 3107 return len; 3108 } 3109 LSSL_ALIAS(SSL_get_client_random); 3110 3111 size_t 3112 SSL_get_server_random(const SSL *s, unsigned char *out, size_t max_out) 3113 { 3114 size_t len = sizeof(s->s3->server_random); 3115 3116 if (out == NULL) 3117 return len; 3118 3119 if (len > max_out) 3120 len = max_out; 3121 3122 memcpy(out, s->s3->server_random, len); 3123 3124 return len; 3125 } 3126 LSSL_ALIAS(SSL_get_server_random); 3127 3128 int 3129 ssl_init_wbio_buffer(SSL *s, int push) 3130 { 3131 BIO *bbio; 3132 3133 if (s->bbio == NULL) { 3134 bbio = BIO_new(BIO_f_buffer()); 3135 if (bbio == NULL) 3136 return (0); 3137 s->bbio = bbio; 3138 } else { 3139 bbio = s->bbio; 3140 if (s->bbio == s->wbio) 3141 s->wbio = BIO_pop(s->wbio); 3142 } 3143 (void)BIO_reset(bbio); 3144 /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */ 3145 if (!BIO_set_read_buffer_size(bbio, 1)) { 3146 SSLerror(s, ERR_R_BUF_LIB); 3147 return (0); 3148 } 3149 if (push) { 3150 if (s->wbio != bbio) 3151 s->wbio = BIO_push(bbio, s->wbio); 3152 } else { 3153 if (s->wbio == bbio) 3154 s->wbio = BIO_pop(bbio); 3155 } 3156 return (1); 3157 } 3158 3159 void 3160 ssl_free_wbio_buffer(SSL *s) 3161 { 3162 if (s == NULL) 3163 return; 3164 3165 if (s->bbio == NULL) 3166 return; 3167 3168 if (s->bbio == s->wbio) { 3169 /* remove buffering */ 3170 s->wbio = BIO_pop(s->wbio); 3171 } 3172 BIO_free(s->bbio); 3173 s->bbio = NULL; 3174 } 3175 3176 void 3177 SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) 3178 { 3179 ctx->quiet_shutdown = mode; 3180 } 3181 LSSL_ALIAS(SSL_CTX_set_quiet_shutdown); 3182 3183 int 3184 SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) 3185 { 3186 return (ctx->quiet_shutdown); 3187 } 3188 LSSL_ALIAS(SSL_CTX_get_quiet_shutdown); 3189 3190 void 3191 SSL_set_quiet_shutdown(SSL *s, int mode) 3192 { 3193 s->quiet_shutdown = mode; 3194 } 3195 LSSL_ALIAS(SSL_set_quiet_shutdown); 3196 3197 int 3198 SSL_get_quiet_shutdown(const SSL *s) 3199 { 3200 return (s->quiet_shutdown); 3201 } 3202 LSSL_ALIAS(SSL_get_quiet_shutdown); 3203 3204 void 3205 SSL_set_shutdown(SSL *s, int mode) 3206 { 3207 s->shutdown = mode; 3208 } 3209 LSSL_ALIAS(SSL_set_shutdown); 3210 3211 int 3212 SSL_get_shutdown(const SSL *s) 3213 { 3214 return (s->shutdown); 3215 } 3216 LSSL_ALIAS(SSL_get_shutdown); 3217 3218 int 3219 SSL_version(const SSL *s) 3220 { 3221 return (s->version); 3222 } 3223 LSSL_ALIAS(SSL_version); 3224 3225 SSL_CTX * 3226 SSL_get_SSL_CTX(const SSL *ssl) 3227 { 3228 return (ssl->ctx); 3229 } 3230 LSSL_ALIAS(SSL_get_SSL_CTX); 3231 3232 SSL_CTX * 3233 SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx) 3234 { 3235 SSL_CERT *new_cert; 3236 3237 if (ctx == NULL) 3238 ctx = ssl->initial_ctx; 3239 if (ssl->ctx == ctx) 3240 return (ssl->ctx); 3241 3242 if ((new_cert = ssl_cert_dup(ctx->cert)) == NULL) 3243 return NULL; 3244 ssl_cert_free(ssl->cert); 3245 ssl->cert = new_cert; 3246 3247 SSL_CTX_up_ref(ctx); 3248 SSL_CTX_free(ssl->ctx); /* decrement reference count */ 3249 ssl->ctx = ctx; 3250 3251 return (ssl->ctx); 3252 } 3253 LSSL_ALIAS(SSL_set_SSL_CTX); 3254 3255 int 3256 SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) 3257 { 3258 return (X509_STORE_set_default_paths(ctx->cert_store)); 3259 } 3260 LSSL_ALIAS(SSL_CTX_set_default_verify_paths); 3261 3262 int 3263 SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile, 3264 const char *CApath) 3265 { 3266 return (X509_STORE_load_locations(ctx->cert_store, CAfile, CApath)); 3267 } 3268 LSSL_ALIAS(SSL_CTX_load_verify_locations); 3269 3270 int 3271 SSL_CTX_load_verify_mem(SSL_CTX *ctx, void *buf, int len) 3272 { 3273 return (X509_STORE_load_mem(ctx->cert_store, buf, len)); 3274 } 3275 LSSL_ALIAS(SSL_CTX_load_verify_mem); 3276 3277 void 3278 SSL_set_info_callback(SSL *ssl, void (*cb)(const SSL *ssl, int type, int val)) 3279 { 3280 ssl->info_callback = cb; 3281 } 3282 LSSL_ALIAS(SSL_set_info_callback); 3283 3284 void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type, int val) 3285 { 3286 return (ssl->info_callback); 3287 } 3288 LSSL_ALIAS(SSL_get_info_callback); 3289 3290 int 3291 SSL_state(const SSL *ssl) 3292 { 3293 return (ssl->s3->hs.state); 3294 } 3295 LSSL_ALIAS(SSL_state); 3296 3297 void 3298 SSL_set_state(SSL *ssl, int state) 3299 { 3300 ssl->s3->hs.state = state; 3301 } 3302 LSSL_ALIAS(SSL_set_state); 3303 3304 void 3305 SSL_set_verify_result(SSL *ssl, long arg) 3306 { 3307 ssl->verify_result = arg; 3308 } 3309 LSSL_ALIAS(SSL_set_verify_result); 3310 3311 long 3312 SSL_get_verify_result(const SSL *ssl) 3313 { 3314 return (ssl->verify_result); 3315 } 3316 LSSL_ALIAS(SSL_get_verify_result); 3317 3318 int 3319 SSL_verify_client_post_handshake(SSL *ssl) 3320 { 3321 return 0; 3322 } 3323 LSSL_ALIAS(SSL_verify_client_post_handshake); 3324 3325 void 3326 SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val) 3327 { 3328 return; 3329 } 3330 LSSL_ALIAS(SSL_CTX_set_post_handshake_auth); 3331 3332 void 3333 SSL_set_post_handshake_auth(SSL *ssl, int val) 3334 { 3335 return; 3336 } 3337 LSSL_ALIAS(SSL_set_post_handshake_auth); 3338 3339 int 3340 SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 3341 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 3342 { 3343 return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, 3344 new_func, dup_func, free_func)); 3345 } 3346 LSSL_ALIAS(SSL_get_ex_new_index); 3347 3348 int 3349 SSL_set_ex_data(SSL *s, int idx, void *arg) 3350 { 3351 return (CRYPTO_set_ex_data(&s->ex_data, idx, arg)); 3352 } 3353 LSSL_ALIAS(SSL_set_ex_data); 3354 3355 void * 3356 SSL_get_ex_data(const SSL *s, int idx) 3357 { 3358 return (CRYPTO_get_ex_data(&s->ex_data, idx)); 3359 } 3360 LSSL_ALIAS(SSL_get_ex_data); 3361 3362 int 3363 SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 3364 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 3365 { 3366 return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, 3367 new_func, dup_func, free_func)); 3368 } 3369 LSSL_ALIAS(SSL_CTX_get_ex_new_index); 3370 3371 int 3372 SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) 3373 { 3374 return (CRYPTO_set_ex_data(&s->ex_data, idx, arg)); 3375 } 3376 LSSL_ALIAS(SSL_CTX_set_ex_data); 3377 3378 void * 3379 SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) 3380 { 3381 return (CRYPTO_get_ex_data(&s->ex_data, idx)); 3382 } 3383 LSSL_ALIAS(SSL_CTX_get_ex_data); 3384 3385 int 3386 ssl_ok(SSL *s) 3387 { 3388 return (1); 3389 } 3390 3391 X509_STORE * 3392 SSL_CTX_get_cert_store(const SSL_CTX *ctx) 3393 { 3394 return (ctx->cert_store); 3395 } 3396 LSSL_ALIAS(SSL_CTX_get_cert_store); 3397 3398 void 3399 SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) 3400 { 3401 X509_STORE_free(ctx->cert_store); 3402 ctx->cert_store = store; 3403 } 3404 LSSL_ALIAS(SSL_CTX_set_cert_store); 3405 3406 void 3407 SSL_CTX_set1_cert_store(SSL_CTX *ctx, X509_STORE *store) 3408 { 3409 if (store != NULL) 3410 X509_STORE_up_ref(store); 3411 3412 SSL_CTX_set_cert_store(ctx, store); 3413 } 3414 LSSL_ALIAS(SSL_CTX_set1_cert_store); 3415 3416 X509 * 3417 SSL_CTX_get0_certificate(const SSL_CTX *ctx) 3418 { 3419 if (ctx->cert == NULL) 3420 return NULL; 3421 3422 return ctx->cert->key->x509; 3423 } 3424 LSSL_ALIAS(SSL_CTX_get0_certificate); 3425 3426 EVP_PKEY * 3427 SSL_CTX_get0_privatekey(const SSL_CTX *ctx) 3428 { 3429 if (ctx->cert == NULL) 3430 return NULL; 3431 3432 return ctx->cert->key->privatekey; 3433 } 3434 LSSL_ALIAS(SSL_CTX_get0_privatekey); 3435 3436 int 3437 SSL_want(const SSL *s) 3438 { 3439 return (s->rwstate); 3440 } 3441 LSSL_ALIAS(SSL_want); 3442 3443 void 3444 SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, RSA *(*cb)(SSL *ssl, int is_export, 3445 int keylength)) 3446 { 3447 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); 3448 } 3449 LSSL_ALIAS(SSL_CTX_set_tmp_rsa_callback); 3450 3451 void 3452 SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export, 3453 int keylength)) 3454 { 3455 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); 3456 } 3457 LSSL_ALIAS(SSL_set_tmp_rsa_callback); 3458 3459 void 3460 SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, DH *(*dh)(SSL *ssl, int is_export, 3461 int keylength)) 3462 { 3463 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); 3464 } 3465 LSSL_ALIAS(SSL_CTX_set_tmp_dh_callback); 3466 3467 void 3468 SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export, 3469 int keylength)) 3470 { 3471 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); 3472 } 3473 LSSL_ALIAS(SSL_set_tmp_dh_callback); 3474 3475 void 3476 SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx, EC_KEY *(*ecdh)(SSL *ssl, 3477 int is_export, int keylength)) 3478 { 3479 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, 3480 (void (*)(void))ecdh); 3481 } 3482 LSSL_ALIAS(SSL_CTX_set_tmp_ecdh_callback); 3483 3484 void 3485 SSL_set_tmp_ecdh_callback(SSL *ssl, EC_KEY *(*ecdh)(SSL *ssl, int is_export, 3486 int keylength)) 3487 { 3488 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh); 3489 } 3490 LSSL_ALIAS(SSL_set_tmp_ecdh_callback); 3491 3492 3493 void 3494 SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version, 3495 int content_type, const void *buf, size_t len, SSL *ssl, void *arg)) 3496 { 3497 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, 3498 (void (*)(void))cb); 3499 } 3500 LSSL_ALIAS(SSL_CTX_set_msg_callback); 3501 3502 void 3503 SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version, 3504 int content_type, const void *buf, size_t len, SSL *ssl, void *arg)) 3505 { 3506 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); 3507 } 3508 LSSL_ALIAS(SSL_set_msg_callback); 3509 3510 int 3511 SSL_cache_hit(SSL *s) 3512 { 3513 return (s->hit); 3514 } 3515 LSSL_ALIAS(SSL_cache_hit); 3516 3517 int 3518 SSL_CTX_get_min_proto_version(SSL_CTX *ctx) 3519 { 3520 return ctx->min_proto_version; 3521 } 3522 LSSL_ALIAS(SSL_CTX_get_min_proto_version); 3523 3524 int 3525 SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version) 3526 { 3527 return ssl_version_set_min(ctx->method, version, 3528 ctx->max_tls_version, &ctx->min_tls_version, 3529 &ctx->min_proto_version); 3530 } 3531 LSSL_ALIAS(SSL_CTX_set_min_proto_version); 3532 3533 int 3534 SSL_CTX_get_max_proto_version(SSL_CTX *ctx) 3535 { 3536 return ctx->max_proto_version; 3537 } 3538 LSSL_ALIAS(SSL_CTX_get_max_proto_version); 3539 3540 int 3541 SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version) 3542 { 3543 return ssl_version_set_max(ctx->method, version, 3544 ctx->min_tls_version, &ctx->max_tls_version, 3545 &ctx->max_proto_version); 3546 } 3547 LSSL_ALIAS(SSL_CTX_set_max_proto_version); 3548 3549 int 3550 SSL_get_min_proto_version(SSL *ssl) 3551 { 3552 return ssl->min_proto_version; 3553 } 3554 LSSL_ALIAS(SSL_get_min_proto_version); 3555 3556 int 3557 SSL_set_min_proto_version(SSL *ssl, uint16_t version) 3558 { 3559 return ssl_version_set_min(ssl->method, version, 3560 ssl->max_tls_version, &ssl->min_tls_version, 3561 &ssl->min_proto_version); 3562 } 3563 LSSL_ALIAS(SSL_set_min_proto_version); 3564 int 3565 SSL_get_max_proto_version(SSL *ssl) 3566 { 3567 return ssl->max_proto_version; 3568 } 3569 LSSL_ALIAS(SSL_get_max_proto_version); 3570 3571 int 3572 SSL_set_max_proto_version(SSL *ssl, uint16_t version) 3573 { 3574 return ssl_version_set_max(ssl->method, version, 3575 ssl->min_tls_version, &ssl->max_tls_version, 3576 &ssl->max_proto_version); 3577 } 3578 LSSL_ALIAS(SSL_set_max_proto_version); 3579 3580 const SSL_METHOD * 3581 SSL_CTX_get_ssl_method(const SSL_CTX *ctx) 3582 { 3583 return ctx->method; 3584 } 3585 LSSL_ALIAS(SSL_CTX_get_ssl_method); 3586 3587 int 3588 SSL_CTX_get_security_level(const SSL_CTX *ctx) 3589 { 3590 return ctx->cert->security_level; 3591 } 3592 LSSL_ALIAS(SSL_CTX_get_security_level); 3593 3594 void 3595 SSL_CTX_set_security_level(SSL_CTX *ctx, int level) 3596 { 3597 ctx->cert->security_level = level; 3598 } 3599 LSSL_ALIAS(SSL_CTX_set_security_level); 3600 3601 int 3602 SSL_get_security_level(const SSL *ssl) 3603 { 3604 return ssl->cert->security_level; 3605 } 3606 LSSL_ALIAS(SSL_get_security_level); 3607 3608 void 3609 SSL_set_security_level(SSL *ssl, int level) 3610 { 3611 ssl->cert->security_level = level; 3612 } 3613 LSSL_ALIAS(SSL_set_security_level); 3614 3615 int 3616 SSL_is_quic(const SSL *ssl) 3617 { 3618 return ssl->quic_method != NULL; 3619 } 3620 LSSL_ALIAS(SSL_is_quic); 3621 3622 int 3623 SSL_set_quic_transport_params(SSL *ssl, const uint8_t *params, 3624 size_t params_len) 3625 { 3626 freezero(ssl->quic_transport_params, 3627 ssl->quic_transport_params_len); 3628 ssl->quic_transport_params = NULL; 3629 ssl->quic_transport_params_len = 0; 3630 3631 if ((ssl->quic_transport_params = malloc(params_len)) == NULL) 3632 return 0; 3633 3634 memcpy(ssl->quic_transport_params, params, params_len); 3635 ssl->quic_transport_params_len = params_len; 3636 3637 return 1; 3638 } 3639 LSSL_ALIAS(SSL_set_quic_transport_params); 3640 3641 void 3642 SSL_get_peer_quic_transport_params(const SSL *ssl, const uint8_t **out_params, 3643 size_t *out_params_len) 3644 { 3645 *out_params = ssl->s3->peer_quic_transport_params; 3646 *out_params_len = ssl->s3->peer_quic_transport_params_len; 3647 } 3648 LSSL_ALIAS(SSL_get_peer_quic_transport_params); 3649 3650 void 3651 SSL_set_quic_use_legacy_codepoint(SSL *ssl, int use_legacy) 3652 { 3653 /* Not supported. */ 3654 } 3655 LSSL_ALIAS(SSL_set_quic_use_legacy_codepoint); 3656