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