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