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