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