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