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