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