ssl_lib.c revision 1.303
1/* $OpenBSD: ssl_lib.c,v 1.303 2022/08/21 19:32:38 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 167const char *SSL_version_str = OPENSSL_VERSION_TEXT; 168 169int 170SSL_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 */ 222int 223SSL_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 239SSL * 240SSL_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 362int 363SSL_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 376int 377SSL_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 390int 391SSL_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 399int 400SSL_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 408int 409SSL_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 434int 435SSL_CTX_set_purpose(SSL_CTX *s, int purpose) 436{ 437 return (X509_VERIFY_PARAM_set_purpose(s->param, purpose)); 438} 439 440int 441SSL_set_purpose(SSL *s, int purpose) 442{ 443 return (X509_VERIFY_PARAM_set_purpose(s->param, purpose)); 444} 445 446int 447SSL_CTX_set_trust(SSL_CTX *s, int trust) 448{ 449 return (X509_VERIFY_PARAM_set_trust(s->param, trust)); 450} 451 452int 453SSL_set_trust(SSL *s, int trust) 454{ 455 return (X509_VERIFY_PARAM_set_trust(s->param, trust)); 456} 457 458int 459SSL_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 472void 473SSL_set_hostflags(SSL *s, unsigned int flags) 474{ 475 X509_VERIFY_PARAM_set_hostflags(s->param, flags); 476} 477 478const char * 479SSL_get0_peername(SSL *s) 480{ 481 return X509_VERIFY_PARAM_get0_peername(s->param); 482} 483 484X509_VERIFY_PARAM * 485SSL_CTX_get0_param(SSL_CTX *ctx) 486{ 487 return (ctx->param); 488} 489 490int 491SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) 492{ 493 return (X509_VERIFY_PARAM_set1(ctx->param, vpm)); 494} 495 496X509_VERIFY_PARAM * 497SSL_get0_param(SSL *ssl) 498{ 499 return (ssl->param); 500} 501 502int 503SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) 504{ 505 return (X509_VERIFY_PARAM_set1(ssl->param, vpm)); 506} 507 508void 509SSL_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 586int 587SSL_up_ref(SSL *s) 588{ 589 int refs = CRYPTO_add(&s->references, 1, CRYPTO_LOCK_SSL); 590 return (refs > 1) ? 1 : 0; 591} 592 593void 594SSL_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 612BIO * 613SSL_get_rbio(const SSL *s) 614{ 615 return (s->rbio); 616} 617 618void 619SSL_set0_rbio(SSL *s, BIO *rbio) 620{ 621 BIO_free_all(s->rbio); 622 s->rbio = rbio; 623} 624 625BIO * 626SSL_get_wbio(const SSL *s) 627{ 628 return (s->wbio); 629} 630 631int 632SSL_get_fd(const SSL *s) 633{ 634 return (SSL_get_rfd(s)); 635} 636 637int 638SSL_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 650int 651SSL_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 663int 664SSL_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 682int 683SSL_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 705int 706SSL_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' */ 730size_t 731SSL_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' */ 743size_t 744SSL_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 756int 757SSL_get_verify_mode(const SSL *s) 758{ 759 return (s->verify_mode); 760} 761 762int 763SSL_get_verify_depth(const SSL *s) 764{ 765 return (X509_VERIFY_PARAM_get_depth(s->param)); 766} 767 768int 769(*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) 770{ 771 return (s->internal->verify_callback); 772} 773 774void 775SSL_CTX_set_keylog_callback(SSL_CTX *ctx, SSL_CTX_keylog_cb_func cb) 776{ 777 ctx->internal->keylog_callback = cb; 778} 779 780SSL_CTX_keylog_cb_func 781SSL_CTX_get_keylog_callback(const SSL_CTX *ctx) 782{ 783 return (ctx->internal->keylog_callback); 784} 785 786int 787SSL_set_num_tickets(SSL *s, size_t num_tickets) 788{ 789 s->internal->num_tickets = num_tickets; 790 791 return 1; 792} 793 794size_t 795SSL_get_num_tickets(const SSL *s) 796{ 797 return s->internal->num_tickets; 798} 799 800int 801SSL_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 808size_t 809SSL_CTX_get_num_tickets(const SSL_CTX *ctx) 810{ 811 return ctx->internal->num_tickets; 812} 813 814int 815SSL_CTX_get_verify_mode(const SSL_CTX *ctx) 816{ 817 return (ctx->verify_mode); 818} 819 820int 821SSL_CTX_get_verify_depth(const SSL_CTX *ctx) 822{ 823 return (X509_VERIFY_PARAM_get_depth(ctx->param)); 824} 825 826int 827(*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) 828{ 829 return (ctx->internal->default_verify_callback); 830} 831 832void 833SSL_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 841void 842SSL_set_verify_depth(SSL *s, int depth) 843{ 844 X509_VERIFY_PARAM_set_depth(s->param, depth); 845} 846 847void 848SSL_set_read_ahead(SSL *s, int yes) 849{ 850 s->internal->read_ahead = yes; 851} 852 853int 854SSL_get_read_ahead(const SSL *s) 855{ 856 return (s->internal->read_ahead); 857} 858 859int 860SSL_pending(const SSL *s) 861{ 862 return (s->method->ssl_pending(s)); 863} 864 865X509 * 866SSL_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 881STACK_OF(X509) * 882SSL_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 897STACK_OF(X509) * 898SSL_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 */ 907int 908SSL_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 */ 939int 940SSL_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 */ 956int 957SSL_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 979int 980SSL_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 988int 989SSL_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 997int 998SSL_is_dtls(const SSL *s) 999{ 1000 return s->method->dtls; 1001} 1002 1003int 1004SSL_is_server(const SSL *s) 1005{ 1006 return s->server; 1007} 1008 1009static long 1010ssl_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 1019long 1020SSL_get_default_timeout(const SSL *s) 1021{ 1022 return (ssl_get_default_timeout()); 1023} 1024 1025int 1026SSL_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 1050int 1051SSL_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 1069int 1070SSL_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 1093int 1094SSL_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 1112int 1113SSL_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 1138int 1139SSL_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 1163uint32_t 1164SSL_CTX_get_max_early_data(const SSL_CTX *ctx) 1165{ 1166 return 0; 1167} 1168 1169int 1170SSL_CTX_set_max_early_data(SSL_CTX *ctx, uint32_t max_early_data) 1171{ 1172 return 1; 1173} 1174 1175uint32_t 1176SSL_get_max_early_data(const SSL *s) 1177{ 1178 return 0; 1179} 1180 1181int 1182SSL_set_max_early_data(SSL *s, uint32_t max_early_data) 1183{ 1184 return 1; 1185} 1186 1187int 1188SSL_get_early_data_status(const SSL *s) 1189{ 1190 return SSL_EARLY_DATA_REJECTED; 1191} 1192 1193int 1194SSL_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 1206int 1207SSL_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 1214int 1215SSL_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 1235int 1236SSL_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 1246int 1247SSL_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 1257int 1258SSL_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 1267long 1268SSL_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 1324long 1325SSL_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 1337struct lhash_st_SSL_SESSION * 1338SSL_CTX_sessions(SSL_CTX *ctx) 1339{ 1340 return (ctx->internal->sessions); 1341} 1342 1343long 1344SSL_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 1422long 1423SSL_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 1435int 1436ssl_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 1447STACK_OF(SSL_CIPHER) * 1448SSL_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 1458STACK_OF(SSL_CIPHER) * 1459SSL_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 1466STACK_OF(SSL_CIPHER) * 1467SSL_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. */ 1504int 1505ssl_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(). */ 1529const char * 1530SSL_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 1543STACK_OF(SSL_CIPHER) * 1544SSL_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. */ 1552int 1553SSL_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 1575int 1576SSL_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. */ 1590int 1591SSL_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 1610int 1611SSL_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 1629char * 1630SSL_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 */ 1674const char * 1675SSL_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 1685int 1686SSL_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 */ 1708int 1709SSL_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. */ 1749void 1750SSL_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. */ 1758void 1759SSL_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. */ 1765void 1766SSL_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 */ 1777int 1778SSL_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 */ 1810int 1811SSL_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 */ 1843void 1844SSL_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 */ 1858void 1859SSL_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 1866void 1867SSL_set_psk_use_session_callback(SSL *s, SSL_psk_use_session_cb_func cb) 1868{ 1869 return; 1870} 1871 1872int 1873SSL_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 1890static unsigned long 1891ssl_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 */ 1910static int 1911ssl_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 */ 1928static unsigned long 1929ssl_session_LHASH_HASH(const void *arg) 1930{ 1931 const SSL_SESSION *a = arg; 1932 1933 return ssl_session_hash(a); 1934} 1935 1936static int 1937ssl_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 1945SSL_CTX * 1946SSL_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 2096void 2097SSL_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 2151int 2152SSL_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 2158pem_password_cb * 2159SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx) 2160{ 2161 return (ctx->default_passwd_callback); 2162} 2163 2164void 2165SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) 2166{ 2167 ctx->default_passwd_callback = cb; 2168} 2169 2170void * 2171SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx) 2172{ 2173 return ctx->default_passwd_callback_userdata; 2174} 2175 2176void 2177SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) 2178{ 2179 ctx->default_passwd_callback_userdata = u; 2180} 2181 2182void 2183SSL_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 2190void 2191SSL_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 2197void 2198SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) 2199{ 2200 X509_VERIFY_PARAM_set_depth(ctx->param, depth); 2201} 2202 2203void 2204ssl_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. */ 2244int 2245ssl_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 2257int 2258ssl_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 2276SSL_CERT_PKEY * 2277ssl_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 2302EVP_PKEY * 2303ssl_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 2336size_t 2337ssl_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 2361static int 2362ssl_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 2383static int 2384ssl_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 2429void 2430ssl_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 2477const SSL_METHOD * 2478SSL_get_ssl_method(SSL *s) 2479{ 2480 return (s->method); 2481} 2482 2483int 2484SSL_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 2509int 2510SSL_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 2588int 2589SSL_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 2599int 2600SSL_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 2610int 2611SSL_do_handshake(SSL *s) 2612{ 2613 if (s->internal->handshake_func == NULL) { 2614 SSLerror(s, SSL_R_CONNECTION_TYPE_NOT_SET); 2615 return (-1); 2616 } 2617 2618 s->method->ssl_renegotiate_check(s); 2619 2620 if (!SSL_in_init(s) && !SSL_in_before(s)) 2621 return 1; 2622 2623 return s->internal->handshake_func(s); 2624} 2625 2626/* 2627 * For the next 2 functions, SSL_clear() sets shutdown and so 2628 * one of these calls will reset it 2629 */ 2630void 2631SSL_set_accept_state(SSL *s) 2632{ 2633 s->server = 1; 2634 s->internal->shutdown = 0; 2635 s->s3->hs.state = SSL_ST_ACCEPT|SSL_ST_BEFORE; 2636 s->internal->handshake_func = s->method->ssl_accept; 2637 ssl_clear_cipher_state(s); 2638} 2639 2640void 2641SSL_set_connect_state(SSL *s) 2642{ 2643 s->server = 0; 2644 s->internal->shutdown = 0; 2645 s->s3->hs.state = SSL_ST_CONNECT|SSL_ST_BEFORE; 2646 s->internal->handshake_func = s->method->ssl_connect; 2647 ssl_clear_cipher_state(s); 2648} 2649 2650int 2651ssl_undefined_function(SSL *s) 2652{ 2653 SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2654 return (0); 2655} 2656 2657int 2658ssl_undefined_void_function(void) 2659{ 2660 SSLerrorx(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2661 return (0); 2662} 2663 2664int 2665ssl_undefined_const_function(const SSL *s) 2666{ 2667 SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2668 return (0); 2669} 2670 2671const char * 2672ssl_version_string(int ver) 2673{ 2674 switch (ver) { 2675 case TLS1_VERSION: 2676 return (SSL_TXT_TLSV1); 2677 case TLS1_1_VERSION: 2678 return (SSL_TXT_TLSV1_1); 2679 case TLS1_2_VERSION: 2680 return (SSL_TXT_TLSV1_2); 2681 case TLS1_3_VERSION: 2682 return (SSL_TXT_TLSV1_3); 2683 case DTLS1_VERSION: 2684 return (SSL_TXT_DTLS1); 2685 case DTLS1_2_VERSION: 2686 return (SSL_TXT_DTLS1_2); 2687 default: 2688 return ("unknown"); 2689 } 2690} 2691 2692const char * 2693SSL_get_version(const SSL *s) 2694{ 2695 return ssl_version_string(s->version); 2696} 2697 2698SSL * 2699SSL_dup(SSL *s) 2700{ 2701 STACK_OF(X509_NAME) *sk; 2702 X509_NAME *xn; 2703 SSL *ret; 2704 int i; 2705 2706 if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL) 2707 goto err; 2708 2709 ret->version = s->version; 2710 ret->method = s->method; 2711 2712 if (s->session != NULL) { 2713 if (!SSL_copy_session_id(ret, s)) 2714 goto err; 2715 } else { 2716 /* 2717 * No session has been established yet, so we have to expect 2718 * that s->cert or ret->cert will be changed later -- 2719 * they should not both point to the same object, 2720 * and thus we can't use SSL_copy_session_id. 2721 */ 2722 2723 ret->method->ssl_free(ret); 2724 ret->method = s->method; 2725 ret->method->ssl_new(ret); 2726 2727 ssl_cert_free(ret->cert); 2728 if ((ret->cert = ssl_cert_dup(s->cert)) == NULL) 2729 goto err; 2730 2731 if (!SSL_set_session_id_context(ret, s->sid_ctx, 2732 s->sid_ctx_length)) 2733 goto err; 2734 } 2735 2736 ret->internal->options = s->internal->options; 2737 ret->internal->mode = s->internal->mode; 2738 SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s)); 2739 SSL_set_read_ahead(ret, SSL_get_read_ahead(s)); 2740 ret->internal->msg_callback = s->internal->msg_callback; 2741 ret->internal->msg_callback_arg = s->internal->msg_callback_arg; 2742 SSL_set_verify(ret, SSL_get_verify_mode(s), 2743 SSL_get_verify_callback(s)); 2744 SSL_set_verify_depth(ret, SSL_get_verify_depth(s)); 2745 ret->internal->generate_session_id = s->internal->generate_session_id; 2746 2747 SSL_set_info_callback(ret, SSL_get_info_callback(s)); 2748 2749 ret->internal->debug = s->internal->debug; 2750 2751 /* copy app data, a little dangerous perhaps */ 2752 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, 2753 &ret->internal->ex_data, &s->internal->ex_data)) 2754 goto err; 2755 2756 /* setup rbio, and wbio */ 2757 if (s->rbio != NULL) { 2758 if (!BIO_dup_state(s->rbio,(char *)&ret->rbio)) 2759 goto err; 2760 } 2761 if (s->wbio != NULL) { 2762 if (s->wbio != s->rbio) { 2763 if (!BIO_dup_state(s->wbio,(char *)&ret->wbio)) 2764 goto err; 2765 } else 2766 ret->wbio = ret->rbio; 2767 } 2768 ret->internal->rwstate = s->internal->rwstate; 2769 ret->internal->in_handshake = s->internal->in_handshake; 2770 ret->internal->handshake_func = s->internal->handshake_func; 2771 ret->server = s->server; 2772 ret->internal->renegotiate = s->internal->renegotiate; 2773 ret->internal->new_session = s->internal->new_session; 2774 ret->internal->quiet_shutdown = s->internal->quiet_shutdown; 2775 ret->internal->shutdown = s->internal->shutdown; 2776 /* SSL_dup does not really work at any state, though */ 2777 ret->s3->hs.state = s->s3->hs.state; 2778 ret->internal->rstate = s->internal->rstate; 2779 2780 /* 2781 * Would have to copy ret->init_buf, ret->init_msg, ret->init_num, 2782 * ret->init_off 2783 */ 2784 ret->internal->init_num = 0; 2785 2786 ret->internal->hit = s->internal->hit; 2787 2788 X509_VERIFY_PARAM_inherit(ret->param, s->param); 2789 2790 if (s->cipher_list != NULL) { 2791 if ((ret->cipher_list = 2792 sk_SSL_CIPHER_dup(s->cipher_list)) == NULL) 2793 goto err; 2794 } 2795 if (s->internal->cipher_list_tls13 != NULL) { 2796 if ((ret->internal->cipher_list_tls13 = 2797 sk_SSL_CIPHER_dup(s->internal->cipher_list_tls13)) == NULL) 2798 goto err; 2799 } 2800 2801 /* Dup the client_CA list */ 2802 if (s->internal->client_CA != NULL) { 2803 if ((sk = sk_X509_NAME_dup(s->internal->client_CA)) == NULL) goto err; 2804 ret->internal->client_CA = sk; 2805 for (i = 0; i < sk_X509_NAME_num(sk); i++) { 2806 xn = sk_X509_NAME_value(sk, i); 2807 if (sk_X509_NAME_set(sk, i, 2808 X509_NAME_dup(xn)) == NULL) { 2809 X509_NAME_free(xn); 2810 goto err; 2811 } 2812 } 2813 } 2814 2815 return ret; 2816 err: 2817 SSL_free(ret); 2818 return NULL; 2819} 2820 2821void 2822ssl_clear_cipher_state(SSL *s) 2823{ 2824 tls12_record_layer_clear_read_state(s->internal->rl); 2825 tls12_record_layer_clear_write_state(s->internal->rl); 2826} 2827 2828void 2829ssl_info_callback(const SSL *s, int type, int value) 2830{ 2831 ssl_info_callback_fn *cb; 2832 2833 if ((cb = s->internal->info_callback) == NULL) 2834 cb = s->ctx->internal->info_callback; 2835 if (cb != NULL) 2836 cb(s, type, value); 2837} 2838 2839void 2840ssl_msg_callback(SSL *s, int is_write, int content_type, 2841 const void *msg_buf, size_t msg_len) 2842{ 2843 if (s->internal->msg_callback != NULL) 2844 s->internal->msg_callback(is_write, s->version, content_type, 2845 msg_buf, msg_len, s, s->internal->msg_callback_arg); 2846} 2847 2848/* Fix this function so that it takes an optional type parameter */ 2849X509 * 2850SSL_get_certificate(const SSL *s) 2851{ 2852 return (s->cert->key->x509); 2853} 2854 2855/* Fix this function so that it takes an optional type parameter */ 2856EVP_PKEY * 2857SSL_get_privatekey(const SSL *s) 2858{ 2859 return (s->cert->key->privatekey); 2860} 2861 2862const SSL_CIPHER * 2863SSL_get_current_cipher(const SSL *s) 2864{ 2865 if ((s->session != NULL) && (s->session->cipher != NULL)) 2866 return (s->session->cipher); 2867 return (NULL); 2868} 2869const void * 2870SSL_get_current_compression(SSL *s) 2871{ 2872 return (NULL); 2873} 2874 2875const void * 2876SSL_get_current_expansion(SSL *s) 2877{ 2878 return (NULL); 2879} 2880 2881size_t 2882SSL_get_client_random(const SSL *s, unsigned char *out, size_t max_out) 2883{ 2884 size_t len = sizeof(s->s3->client_random); 2885 2886 if (out == NULL) 2887 return len; 2888 2889 if (len > max_out) 2890 len = max_out; 2891 2892 memcpy(out, s->s3->client_random, len); 2893 2894 return len; 2895} 2896 2897size_t 2898SSL_get_server_random(const SSL *s, unsigned char *out, size_t max_out) 2899{ 2900 size_t len = sizeof(s->s3->server_random); 2901 2902 if (out == NULL) 2903 return len; 2904 2905 if (len > max_out) 2906 len = max_out; 2907 2908 memcpy(out, s->s3->server_random, len); 2909 2910 return len; 2911} 2912 2913int 2914ssl_init_wbio_buffer(SSL *s, int push) 2915{ 2916 BIO *bbio; 2917 2918 if (s->bbio == NULL) { 2919 bbio = BIO_new(BIO_f_buffer()); 2920 if (bbio == NULL) 2921 return (0); 2922 s->bbio = bbio; 2923 } else { 2924 bbio = s->bbio; 2925 if (s->bbio == s->wbio) 2926 s->wbio = BIO_pop(s->wbio); 2927 } 2928 (void)BIO_reset(bbio); 2929/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */ 2930 if (!BIO_set_read_buffer_size(bbio, 1)) { 2931 SSLerror(s, ERR_R_BUF_LIB); 2932 return (0); 2933 } 2934 if (push) { 2935 if (s->wbio != bbio) 2936 s->wbio = BIO_push(bbio, s->wbio); 2937 } else { 2938 if (s->wbio == bbio) 2939 s->wbio = BIO_pop(bbio); 2940 } 2941 return (1); 2942} 2943 2944void 2945ssl_free_wbio_buffer(SSL *s) 2946{ 2947 if (s == NULL) 2948 return; 2949 2950 if (s->bbio == NULL) 2951 return; 2952 2953 if (s->bbio == s->wbio) { 2954 /* remove buffering */ 2955 s->wbio = BIO_pop(s->wbio); 2956 } 2957 BIO_free(s->bbio); 2958 s->bbio = NULL; 2959} 2960 2961void 2962SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) 2963{ 2964 ctx->internal->quiet_shutdown = mode; 2965} 2966 2967int 2968SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) 2969{ 2970 return (ctx->internal->quiet_shutdown); 2971} 2972 2973void 2974SSL_set_quiet_shutdown(SSL *s, int mode) 2975{ 2976 s->internal->quiet_shutdown = mode; 2977} 2978 2979int 2980SSL_get_quiet_shutdown(const SSL *s) 2981{ 2982 return (s->internal->quiet_shutdown); 2983} 2984 2985void 2986SSL_set_shutdown(SSL *s, int mode) 2987{ 2988 s->internal->shutdown = mode; 2989} 2990 2991int 2992SSL_get_shutdown(const SSL *s) 2993{ 2994 return (s->internal->shutdown); 2995} 2996 2997int 2998SSL_version(const SSL *s) 2999{ 3000 return (s->version); 3001} 3002 3003SSL_CTX * 3004SSL_get_SSL_CTX(const SSL *ssl) 3005{ 3006 return (ssl->ctx); 3007} 3008 3009SSL_CTX * 3010SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx) 3011{ 3012 SSL_CERT *new_cert; 3013 3014 if (ctx == NULL) 3015 ctx = ssl->initial_ctx; 3016 if (ssl->ctx == ctx) 3017 return (ssl->ctx); 3018 3019 if ((new_cert = ssl_cert_dup(ctx->internal->cert)) == NULL) 3020 return NULL; 3021 ssl_cert_free(ssl->cert); 3022 ssl->cert = new_cert; 3023 3024 SSL_CTX_up_ref(ctx); 3025 SSL_CTX_free(ssl->ctx); /* decrement reference count */ 3026 ssl->ctx = ctx; 3027 3028 return (ssl->ctx); 3029} 3030 3031int 3032SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) 3033{ 3034 return (X509_STORE_set_default_paths(ctx->cert_store)); 3035} 3036 3037int 3038SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile, 3039 const char *CApath) 3040{ 3041 return (X509_STORE_load_locations(ctx->cert_store, CAfile, CApath)); 3042} 3043 3044int 3045SSL_CTX_load_verify_mem(SSL_CTX *ctx, void *buf, int len) 3046{ 3047 return (X509_STORE_load_mem(ctx->cert_store, buf, len)); 3048} 3049 3050void 3051SSL_set_info_callback(SSL *ssl, void (*cb)(const SSL *ssl, int type, int val)) 3052{ 3053 ssl->internal->info_callback = cb; 3054} 3055 3056void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type, int val) 3057{ 3058 return (ssl->internal->info_callback); 3059} 3060 3061int 3062SSL_state(const SSL *ssl) 3063{ 3064 return (ssl->s3->hs.state); 3065} 3066 3067void 3068SSL_set_state(SSL *ssl, int state) 3069{ 3070 ssl->s3->hs.state = state; 3071} 3072 3073void 3074SSL_set_verify_result(SSL *ssl, long arg) 3075{ 3076 ssl->verify_result = arg; 3077} 3078 3079long 3080SSL_get_verify_result(const SSL *ssl) 3081{ 3082 return (ssl->verify_result); 3083} 3084 3085int 3086SSL_verify_client_post_handshake(SSL *ssl) 3087{ 3088 return 0; 3089} 3090 3091void 3092SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val) 3093{ 3094 return; 3095} 3096 3097void 3098SSL_set_post_handshake_auth(SSL *ssl, int val) 3099{ 3100 return; 3101} 3102 3103int 3104SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 3105 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 3106{ 3107 return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, 3108 new_func, dup_func, free_func)); 3109} 3110 3111int 3112SSL_set_ex_data(SSL *s, int idx, void *arg) 3113{ 3114 return (CRYPTO_set_ex_data(&s->internal->ex_data, idx, arg)); 3115} 3116 3117void * 3118SSL_get_ex_data(const SSL *s, int idx) 3119{ 3120 return (CRYPTO_get_ex_data(&s->internal->ex_data, idx)); 3121} 3122 3123int 3124SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 3125 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 3126{ 3127 return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, 3128 new_func, dup_func, free_func)); 3129} 3130 3131int 3132SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) 3133{ 3134 return (CRYPTO_set_ex_data(&s->internal->ex_data, idx, arg)); 3135} 3136 3137void * 3138SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) 3139{ 3140 return (CRYPTO_get_ex_data(&s->internal->ex_data, idx)); 3141} 3142 3143int 3144ssl_ok(SSL *s) 3145{ 3146 return (1); 3147} 3148 3149X509_STORE * 3150SSL_CTX_get_cert_store(const SSL_CTX *ctx) 3151{ 3152 return (ctx->cert_store); 3153} 3154 3155void 3156SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) 3157{ 3158 X509_STORE_free(ctx->cert_store); 3159 ctx->cert_store = store; 3160} 3161 3162X509 * 3163SSL_CTX_get0_certificate(const SSL_CTX *ctx) 3164{ 3165 if (ctx->internal->cert == NULL) 3166 return NULL; 3167 3168 return ctx->internal->cert->key->x509; 3169} 3170 3171EVP_PKEY * 3172SSL_CTX_get0_privatekey(const SSL_CTX *ctx) 3173{ 3174 if (ctx->internal->cert == NULL) 3175 return NULL; 3176 3177 return ctx->internal->cert->key->privatekey; 3178} 3179 3180int 3181SSL_want(const SSL *s) 3182{ 3183 return (s->internal->rwstate); 3184} 3185 3186void 3187SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, RSA *(*cb)(SSL *ssl, int is_export, 3188 int keylength)) 3189{ 3190 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); 3191} 3192 3193void 3194SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export, 3195 int keylength)) 3196{ 3197 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); 3198} 3199 3200void 3201SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, DH *(*dh)(SSL *ssl, int is_export, 3202 int keylength)) 3203{ 3204 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); 3205} 3206 3207void 3208SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export, 3209 int keylength)) 3210{ 3211 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); 3212} 3213 3214void 3215SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx, EC_KEY *(*ecdh)(SSL *ssl, 3216 int is_export, int keylength)) 3217{ 3218 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, 3219 (void (*)(void))ecdh); 3220} 3221 3222void 3223SSL_set_tmp_ecdh_callback(SSL *ssl, EC_KEY *(*ecdh)(SSL *ssl, int is_export, 3224 int keylength)) 3225{ 3226 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh); 3227} 3228 3229 3230void 3231SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version, 3232 int content_type, const void *buf, size_t len, SSL *ssl, void *arg)) 3233{ 3234 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, 3235 (void (*)(void))cb); 3236} 3237 3238void 3239SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version, 3240 int content_type, const void *buf, size_t len, SSL *ssl, void *arg)) 3241{ 3242 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); 3243} 3244 3245void 3246SSL_set_debug(SSL *s, int debug) 3247{ 3248 s->internal->debug = debug; 3249} 3250 3251int 3252SSL_cache_hit(SSL *s) 3253{ 3254 return (s->internal->hit); 3255} 3256 3257int 3258SSL_CTX_get_min_proto_version(SSL_CTX *ctx) 3259{ 3260 return ctx->internal->min_proto_version; 3261} 3262 3263int 3264SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version) 3265{ 3266 return ssl_version_set_min(ctx->method, version, 3267 ctx->internal->max_tls_version, &ctx->internal->min_tls_version, 3268 &ctx->internal->min_proto_version); 3269} 3270 3271int 3272SSL_CTX_get_max_proto_version(SSL_CTX *ctx) 3273{ 3274 return ctx->internal->max_proto_version; 3275} 3276 3277int 3278SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version) 3279{ 3280 return ssl_version_set_max(ctx->method, version, 3281 ctx->internal->min_tls_version, &ctx->internal->max_tls_version, 3282 &ctx->internal->max_proto_version); 3283} 3284 3285int 3286SSL_get_min_proto_version(SSL *ssl) 3287{ 3288 return ssl->internal->min_proto_version; 3289} 3290 3291int 3292SSL_set_min_proto_version(SSL *ssl, uint16_t version) 3293{ 3294 return ssl_version_set_min(ssl->method, version, 3295 ssl->internal->max_tls_version, &ssl->internal->min_tls_version, 3296 &ssl->internal->min_proto_version); 3297} 3298int 3299SSL_get_max_proto_version(SSL *ssl) 3300{ 3301 return ssl->internal->max_proto_version; 3302} 3303 3304int 3305SSL_set_max_proto_version(SSL *ssl, uint16_t version) 3306{ 3307 return ssl_version_set_max(ssl->method, version, 3308 ssl->internal->min_tls_version, &ssl->internal->max_tls_version, 3309 &ssl->internal->max_proto_version); 3310} 3311 3312const SSL_METHOD * 3313SSL_CTX_get_ssl_method(const SSL_CTX *ctx) 3314{ 3315 return ctx->method; 3316} 3317 3318int 3319SSL_CTX_get_security_level(const SSL_CTX *ctx) 3320{ 3321 return ctx->internal->cert->security_level; 3322} 3323 3324void 3325SSL_CTX_set_security_level(SSL_CTX *ctx, int level) 3326{ 3327 ctx->internal->cert->security_level = level; 3328} 3329 3330int 3331SSL_get_security_level(const SSL *ssl) 3332{ 3333 return ssl->cert->security_level; 3334} 3335 3336void 3337SSL_set_security_level(SSL *ssl, int level) 3338{ 3339 ssl->cert->security_level = level; 3340} 3341 3342int 3343SSL_is_quic(const SSL *ssl) 3344{ 3345 return ssl->quic_method != NULL; 3346} 3347 3348int 3349SSL_set_quic_transport_params(SSL *ssl, const uint8_t *params, 3350 size_t params_len) 3351{ 3352 freezero(ssl->internal->quic_transport_params, 3353 ssl->internal->quic_transport_params_len); 3354 ssl->internal->quic_transport_params = NULL; 3355 ssl->internal->quic_transport_params_len = 0; 3356 3357 if ((ssl->internal->quic_transport_params = malloc(params_len)) == NULL) 3358 return 0; 3359 3360 memcpy(ssl->internal->quic_transport_params, params, params_len); 3361 ssl->internal->quic_transport_params_len = params_len; 3362 3363 return 1; 3364} 3365 3366void 3367SSL_get_peer_quic_transport_params(const SSL *ssl, const uint8_t **out_params, 3368 size_t *out_params_len) 3369{ 3370 *out_params = ssl->s3->peer_quic_transport_params; 3371 *out_params_len = ssl->s3->peer_quic_transport_params_len; 3372} 3373 3374static int 3375ssl_cipher_id_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_) 3376{ 3377 SSL_CIPHER const *a = a_; 3378 SSL_CIPHER const *b = b_; 3379 return ssl_cipher_id_cmp(a, b); 3380} 3381 3382SSL_CIPHER * 3383OBJ_bsearch_ssl_cipher_id(SSL_CIPHER *key, SSL_CIPHER const *base, int num) 3384{ 3385 return (SSL_CIPHER *)OBJ_bsearch_(key, base, num, sizeof(SSL_CIPHER), 3386 ssl_cipher_id_cmp_BSEARCH_CMP_FN); 3387} 3388