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