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