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