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