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