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