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