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