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