ssl_lib.c revision 1.48
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->info_callback = NULL; 1769 1770 ret->app_verify_callback = 0; 1771 ret->app_verify_arg = NULL; 1772 1773 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT; 1774 ret->read_ahead = 0; 1775 ret->msg_callback = 0; 1776 ret->msg_callback_arg = NULL; 1777 ret->verify_mode = SSL_VERIFY_NONE; 1778#if 0 1779 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */ 1780#endif 1781 ret->sid_ctx_length = 0; 1782 ret->default_verify_callback = NULL; 1783 if ((ret->cert = ssl_cert_new()) == NULL) 1784 goto err; 1785 1786 ret->default_passwd_callback = 0; 1787 ret->default_passwd_callback_userdata = NULL; 1788 ret->client_cert_cb = 0; 1789 ret->app_gen_cookie_cb = 0; 1790 ret->app_verify_cookie_cb = 0; 1791 1792 ret->sessions = lh_SSL_SESSION_new(); 1793 if (ret->sessions == NULL) 1794 goto err; 1795 ret->cert_store = X509_STORE_new(); 1796 if (ret->cert_store == NULL) 1797 goto err; 1798 1799 ssl_create_cipher_list(ret->method, &ret->cipher_list, 1800 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST); 1801 if (ret->cipher_list == NULL || 1802 sk_SSL_CIPHER_num(ret->cipher_list) <= 0) { 1803 SSLerr(SSL_F_SSL_CTX_NEW, 1804 SSL_R_LIBRARY_HAS_NO_CIPHERS); 1805 goto err2; 1806 } 1807 1808 ret->param = X509_VERIFY_PARAM_new(); 1809 if (!ret->param) 1810 goto err; 1811 1812 if ((ret->md5 = EVP_get_digestbyname("ssl3-md5")) == NULL) { 1813 SSLerr(SSL_F_SSL_CTX_NEW, 1814 SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES); 1815 goto err2; 1816 } 1817 if ((ret->sha1 = EVP_get_digestbyname("ssl3-sha1")) == NULL) { 1818 SSLerr(SSL_F_SSL_CTX_NEW, 1819 SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES); 1820 goto err2; 1821 } 1822 1823 if ((ret->client_CA = sk_X509_NAME_new_null()) == NULL) 1824 goto err; 1825 1826 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data); 1827 1828 ret->extra_certs = NULL; 1829 /* No compression for DTLS */ 1830 if (meth->version != DTLS1_VERSION) 1831 ret->comp_methods = SSL_COMP_get_compression_methods(); 1832 1833 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; 1834 1835#ifndef OPENSSL_NO_TLSEXT 1836 ret->tlsext_servername_callback = 0; 1837 ret->tlsext_servername_arg = NULL; 1838 /* Setup RFC4507 ticket keys */ 1839 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0) 1840 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0) 1841 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0)) 1842 ret->options |= SSL_OP_NO_TICKET; 1843 1844 ret->tlsext_status_cb = 0; 1845 ret->tlsext_status_arg = NULL; 1846 1847# ifndef OPENSSL_NO_NEXTPROTONEG 1848 ret->next_protos_advertised_cb = 0; 1849 ret->next_proto_select_cb = 0; 1850# endif 1851#endif 1852#ifndef OPENSSL_NO_PSK 1853 ret->psk_identity_hint = NULL; 1854 ret->psk_client_callback = NULL; 1855 ret->psk_server_callback = NULL; 1856#endif 1857#ifndef OPENSSL_NO_ENGINE 1858 ret->client_cert_engine = NULL; 1859#ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO 1860#define eng_strx(x) #x 1861#define eng_str(x) eng_strx(x) 1862 /* Use specific client engine automatically... ignore errors */ 1863 { 1864 ENGINE *eng; 1865 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO)); 1866 if (!eng) { 1867 ERR_clear_error(); 1868 ENGINE_load_builtin_engines(); 1869 eng = ENGINE_by_id(eng_str( 1870 OPENSSL_SSL_CLIENT_ENGINE_AUTO)); 1871 } 1872 if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng)) 1873 ERR_clear_error(); 1874 } 1875#endif 1876#endif 1877 /* 1878 * Default is to connect to non-RI servers. When RI is more widely 1879 * deployed might change this. 1880 */ 1881 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT; 1882 1883 return (ret); 1884 err: 1885 SSLerr(SSL_F_SSL_CTX_NEW, 1886 ERR_R_MALLOC_FAILURE); 1887 err2: 1888 if (ret != NULL) 1889 SSL_CTX_free(ret); 1890 return (NULL); 1891} 1892 1893#if 0 1894static void 1895SSL_COMP_free(SSL_COMP *comp) 1896 { free(comp); 1897} 1898#endif 1899 1900void 1901SSL_CTX_free(SSL_CTX *a) 1902{ 1903 int i; 1904 1905 if (a == NULL) 1906 return; 1907 1908 i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX); 1909 if (i > 0) 1910 return; 1911 1912 if (a->param) 1913 X509_VERIFY_PARAM_free(a->param); 1914 1915 /* 1916 * Free internal session cache. However: the remove_cb() may reference 1917 * the ex_data of SSL_CTX, thus the ex_data store can only be removed 1918 * after the sessions were flushed. 1919 * As the ex_data handling routines might also touch the session cache, 1920 * the most secure solution seems to be: empty (flush) the cache, then 1921 * free ex_data, then finally free the cache. 1922 * (See ticket [openssl.org #212].) 1923 */ 1924 if (a->sessions != NULL) 1925 SSL_CTX_flush_sessions(a, 0); 1926 1927 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data); 1928 1929 if (a->sessions != NULL) 1930 lh_SSL_SESSION_free(a->sessions); 1931 1932 if (a->cert_store != NULL) 1933 X509_STORE_free(a->cert_store); 1934 if (a->cipher_list != NULL) 1935 sk_SSL_CIPHER_free(a->cipher_list); 1936 if (a->cipher_list_by_id != NULL) 1937 sk_SSL_CIPHER_free(a->cipher_list_by_id); 1938 if (a->cert != NULL) 1939 ssl_cert_free(a->cert); 1940 if (a->client_CA != NULL) 1941 sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free); 1942 if (a->extra_certs != NULL) 1943 sk_X509_pop_free(a->extra_certs, X509_free); 1944#if 0 /* This should never be done, since it removes a global database */ 1945 if (a->comp_methods != NULL) 1946 sk_SSL_COMP_pop_free(a->comp_methods, SSL_COMP_free); 1947#else 1948 a->comp_methods = NULL; 1949#endif 1950 1951#ifndef OPENSSL_NO_SRTP 1952 if (a->srtp_profiles) 1953 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles); 1954#endif 1955 1956#ifndef OPENSSL_NO_PSK 1957 if (a->psk_identity_hint) 1958 free(a->psk_identity_hint); 1959#endif 1960#ifndef OPENSSL_NO_ENGINE 1961 if (a->client_cert_engine) 1962 ENGINE_finish(a->client_cert_engine); 1963#endif 1964 1965 free(a); 1966} 1967 1968void 1969SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) 1970{ 1971 ctx->default_passwd_callback = cb; 1972} 1973 1974void 1975SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) 1976{ 1977 ctx->default_passwd_callback_userdata = u; 1978} 1979 1980void 1981SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *, 1982 void *), void *arg) 1983{ 1984 ctx->app_verify_callback = cb; 1985 ctx->app_verify_arg = arg; 1986} 1987 1988void 1989SSL_CTX_set_verify(SSL_CTX *ctx, int mode, int (*cb)(int, X509_STORE_CTX *)) 1990{ 1991 ctx->verify_mode = mode; 1992 ctx->default_verify_callback = cb; 1993} 1994 1995void 1996SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) 1997{ 1998 X509_VERIFY_PARAM_set_depth(ctx->param, depth); 1999} 2000 2001void 2002ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher) 2003{ 2004 CERT_PKEY *cpk; 2005 int rsa_enc, rsa_tmp, rsa_sign, dh_tmp, dh_rsa, dh_dsa, dsa_sign; 2006 int rsa_enc_export, dh_rsa_export, dh_dsa_export; 2007 int rsa_tmp_export, dh_tmp_export, kl; 2008 unsigned long mask_k, mask_a, emask_k, emask_a; 2009 int have_ecc_cert, ecdh_ok, ecdsa_ok, ecc_pkey_size; 2010#ifndef OPENSSL_NO_ECDH 2011 int have_ecdh_tmp; 2012#endif 2013 X509 *x = NULL; 2014 EVP_PKEY *ecc_pkey = NULL; 2015 int signature_nid = 0, pk_nid = 0, md_nid = 0; 2016 2017 if (c == NULL) 2018 return; 2019 2020 kl = SSL_C_EXPORT_PKEYLENGTH(cipher); 2021 2022 rsa_tmp = (c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL); 2023 rsa_tmp_export = (c->rsa_tmp_cb != NULL || 2024 (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl)); 2025#ifndef OPENSSL_NO_DH 2026 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL); 2027 dh_tmp_export = (c->dh_tmp_cb != NULL || 2028 (dh_tmp && DH_size(c->dh_tmp)*8 <= kl)); 2029#else 2030 dh_tmp = dh_tmp_export = 0; 2031#endif 2032 2033#ifndef OPENSSL_NO_ECDH 2034 have_ecdh_tmp = (c->ecdh_tmp != NULL || c->ecdh_tmp_cb != NULL); 2035#endif 2036 cpk = &(c->pkeys[SSL_PKEY_RSA_ENC]); 2037 rsa_enc = (cpk->x509 != NULL && cpk->privatekey != NULL); 2038 rsa_enc_export = (rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl); 2039 cpk = &(c->pkeys[SSL_PKEY_RSA_SIGN]); 2040 rsa_sign = (cpk->x509 != NULL && cpk->privatekey != NULL); 2041 cpk = &(c->pkeys[SSL_PKEY_DSA_SIGN]); 2042 dsa_sign = (cpk->x509 != NULL && cpk->privatekey != NULL); 2043 cpk = &(c->pkeys[SSL_PKEY_DH_RSA]); 2044 dh_rsa = (cpk->x509 != NULL && cpk->privatekey != NULL); 2045 dh_rsa_export = (dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl); 2046 cpk = &(c->pkeys[SSL_PKEY_DH_DSA]); 2047/* FIX THIS EAY EAY EAY */ 2048 dh_dsa = (cpk->x509 != NULL && cpk->privatekey != NULL); 2049 dh_dsa_export = (dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl); 2050 cpk = &(c->pkeys[SSL_PKEY_ECC]); 2051 have_ecc_cert = (cpk->x509 != NULL && cpk->privatekey != NULL); 2052 mask_k = 0; 2053 mask_a = 0; 2054 emask_k = 0; 2055 emask_a = 0; 2056 2057 2058 2059 2060 cpk = &(c->pkeys[SSL_PKEY_GOST01]); 2061 if (cpk->x509 != NULL && cpk->privatekey !=NULL) { 2062 mask_k |= SSL_kGOST; 2063 mask_a |= SSL_aGOST01; 2064 } 2065 cpk = &(c->pkeys[SSL_PKEY_GOST94]); 2066 if (cpk->x509 != NULL && cpk->privatekey !=NULL) { 2067 mask_k |= SSL_kGOST; 2068 mask_a |= SSL_aGOST94; 2069 } 2070 2071 if (rsa_enc || (rsa_tmp && rsa_sign)) 2072 mask_k|=SSL_kRSA; 2073 if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc))) 2074 emask_k|=SSL_kRSA; 2075 2076#if 0 2077 /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */ 2078 if ((dh_tmp || dh_rsa || dh_dsa) && 2079 (rsa_enc || rsa_sign || dsa_sign)) 2080 mask_k|=SSL_kEDH; 2081 if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) && 2082 (rsa_enc || rsa_sign || dsa_sign)) 2083 emask_k|=SSL_kEDH; 2084#endif 2085 2086 if (dh_tmp_export) 2087 emask_k|=SSL_kEDH; 2088 2089 if (dh_tmp) 2090 mask_k|=SSL_kEDH; 2091 2092 if (dh_rsa) 2093 mask_k|=SSL_kDHr; 2094 if (dh_rsa_export) 2095 emask_k|=SSL_kDHr; 2096 2097 if (dh_dsa) 2098 mask_k|=SSL_kDHd; 2099 if (dh_dsa_export) 2100 emask_k|=SSL_kDHd; 2101 2102 if (rsa_enc || rsa_sign) { 2103 mask_a|=SSL_aRSA; 2104 emask_a|=SSL_aRSA; 2105 } 2106 2107 if (dsa_sign) { 2108 mask_a|=SSL_aDSS; 2109 emask_a|=SSL_aDSS; 2110 } 2111 2112 mask_a|=SSL_aNULL; 2113 emask_a|=SSL_aNULL; 2114 2115 2116 /* 2117 * An ECC certificate may be usable for ECDH and/or 2118 * ECDSA cipher suites depending on the key usage extension. 2119 */ 2120 if (have_ecc_cert) { 2121 /* This call populates extension flags (ex_flags) */ 2122 x = (c->pkeys[SSL_PKEY_ECC]).x509; 2123 X509_check_purpose(x, -1, 0); 2124 ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ? 2125 (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1; 2126 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ? 2127 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1; 2128 ecc_pkey = X509_get_pubkey(x); 2129 ecc_pkey_size = (ecc_pkey != NULL) ? 2130 EVP_PKEY_bits(ecc_pkey) : 0; 2131 EVP_PKEY_free(ecc_pkey); 2132 if ((x->sig_alg) && (x->sig_alg->algorithm)) { 2133 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm); 2134 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid); 2135 } 2136#ifndef OPENSSL_NO_ECDH 2137 if (ecdh_ok) { 2138 2139 if (pk_nid == NID_rsaEncryption || pk_nid == NID_rsa) { 2140 mask_k|=SSL_kECDHr; 2141 mask_a|=SSL_aECDH; 2142 if (ecc_pkey_size <= 163) { 2143 emask_k|=SSL_kECDHr; 2144 emask_a|=SSL_aECDH; 2145 } 2146 } 2147 2148 if (pk_nid == NID_X9_62_id_ecPublicKey) { 2149 mask_k|=SSL_kECDHe; 2150 mask_a|=SSL_aECDH; 2151 if (ecc_pkey_size <= 163) { 2152 emask_k|=SSL_kECDHe; 2153 emask_a|=SSL_aECDH; 2154 } 2155 } 2156 } 2157#endif 2158#ifndef OPENSSL_NO_ECDSA 2159 if (ecdsa_ok) { 2160 mask_a|=SSL_aECDSA; 2161 emask_a|=SSL_aECDSA; 2162 } 2163#endif 2164 } 2165 2166#ifndef OPENSSL_NO_ECDH 2167 if (have_ecdh_tmp) { 2168 mask_k|=SSL_kEECDH; 2169 emask_k|=SSL_kEECDH; 2170 } 2171#endif 2172 2173#ifndef OPENSSL_NO_PSK 2174 mask_k |= SSL_kPSK; 2175 mask_a |= SSL_aPSK; 2176 emask_k |= SSL_kPSK; 2177 emask_a |= SSL_aPSK; 2178#endif 2179 2180 c->mask_k = mask_k; 2181 c->mask_a = mask_a; 2182 c->export_mask_k = emask_k; 2183 c->export_mask_a = emask_a; 2184 c->valid = 1; 2185} 2186 2187/* This handy macro borrowed from crypto/x509v3/v3_purp.c */ 2188#define ku_reject(x, usage) \ 2189 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage))) 2190 2191#ifndef OPENSSL_NO_EC 2192 2193int 2194ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) 2195{ 2196 unsigned long alg_k, alg_a; 2197 EVP_PKEY *pkey = NULL; 2198 int keysize = 0; 2199 int signature_nid = 0, md_nid = 0, pk_nid = 0; 2200 const SSL_CIPHER *cs = s->s3->tmp.new_cipher; 2201 2202 alg_k = cs->algorithm_mkey; 2203 alg_a = cs->algorithm_auth; 2204 2205 if (SSL_C_IS_EXPORT(cs)) { 2206 /* ECDH key length in export ciphers must be <= 163 bits */ 2207 pkey = X509_get_pubkey(x); 2208 if (pkey == NULL) 2209 return (0); 2210 keysize = EVP_PKEY_bits(pkey); 2211 EVP_PKEY_free(pkey); 2212 if (keysize > 163) 2213 return (0); 2214 } 2215 2216 /* This call populates the ex_flags field correctly */ 2217 X509_check_purpose(x, -1, 0); 2218 if ((x->sig_alg) && (x->sig_alg->algorithm)) { 2219 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm); 2220 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid); 2221 } 2222 if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr) { 2223 /* key usage, if present, must allow key agreement */ 2224 if (ku_reject(x, X509v3_KU_KEY_AGREEMENT)) { 2225 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, 2226 SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT); 2227 return (0); 2228 } 2229 if ((alg_k & SSL_kECDHe) && TLS1_get_version(s) < 2230 TLS1_2_VERSION) { 2231 /* signature alg must be ECDSA */ 2232 if (pk_nid != NID_X9_62_id_ecPublicKey) { 2233 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, 2234 SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE); 2235 return (0); 2236 } 2237 } 2238 if ((alg_k & SSL_kECDHr) && TLS1_get_version(s) < 2239 TLS1_2_VERSION) { 2240 /* signature alg must be RSA */ 2241 if (pk_nid != NID_rsaEncryption && pk_nid != NID_rsa) { 2242 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, 2243 SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE); 2244 return (0); 2245 } 2246 } 2247 } 2248 if (alg_a & SSL_aECDSA) { 2249 /* key usage, if present, must allow signing */ 2250 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) { 2251 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, 2252 SSL_R_ECC_CERT_NOT_FOR_SIGNING); 2253 return (0); 2254 } 2255 } 2256 2257 return (1); 2258 /* all checks are ok */ 2259} 2260 2261#endif 2262 2263/* THIS NEEDS CLEANING UP */ 2264CERT_PKEY * 2265ssl_get_server_send_pkey(const SSL *s) 2266{ 2267 unsigned long alg_k, alg_a; 2268 CERT *c; 2269 int i; 2270 2271 c = s->cert; 2272 ssl_set_cert_masks(c, s->s3->tmp.new_cipher); 2273 2274 alg_k = s->s3->tmp.new_cipher->algorithm_mkey; 2275 alg_a = s->s3->tmp.new_cipher->algorithm_auth; 2276 2277 if (alg_k & (SSL_kECDHr|SSL_kECDHe)) { 2278 /* 2279 * We don't need to look at SSL_kEECDH 2280 * since no certificate is needed for 2281 * anon ECDH and for authenticated 2282 * EECDH, the check for the auth 2283 * algorithm will set i correctly 2284 * NOTE: For ECDH-RSA, we need an ECC 2285 * not an RSA cert but for EECDH-RSA 2286 * we need an RSA cert. Placing the 2287 * checks for SSL_kECDH before RSA 2288 * checks ensures the correct cert is chosen. 2289 */ 2290 i = SSL_PKEY_ECC; 2291 } else if (alg_a & SSL_aECDSA) { 2292 i = SSL_PKEY_ECC; 2293 } else if (alg_k & SSL_kDHr) 2294 i = SSL_PKEY_DH_RSA; 2295 else if (alg_k & SSL_kDHd) 2296 i = SSL_PKEY_DH_DSA; 2297 else if (alg_a & SSL_aDSS) 2298 i = SSL_PKEY_DSA_SIGN; 2299 else if (alg_a & SSL_aRSA) { 2300 if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL) 2301 i = SSL_PKEY_RSA_SIGN; 2302 else 2303 i = SSL_PKEY_RSA_ENC; 2304 } else if (alg_a & SSL_aKRB5) { 2305 /* VRS something else here? */ 2306 return (NULL); 2307 } else if (alg_a & SSL_aGOST94) 2308 i = SSL_PKEY_GOST94; 2309 else if (alg_a & SSL_aGOST01) 2310 i = SSL_PKEY_GOST01; 2311 else { /* if (alg_a & SSL_aNULL) */ 2312 SSLerr(SSL_F_SSL_GET_SERVER_SEND_PKEY, 2313 ERR_R_INTERNAL_ERROR); 2314 return (NULL); 2315 } 2316 2317 return (c->pkeys + i); 2318} 2319 2320X509 * 2321ssl_get_server_send_cert(const SSL *s) 2322{ 2323 CERT_PKEY *cpk; 2324 2325 cpk = ssl_get_server_send_pkey(s); 2326 if (!cpk) 2327 return (NULL); 2328 return (cpk->x509); 2329} 2330 2331EVP_PKEY * 2332ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher, const EVP_MD **pmd) 2333{ 2334 unsigned long alg_a; 2335 CERT *c; 2336 int idx = -1; 2337 2338 alg_a = cipher->algorithm_auth; 2339 c = s->cert; 2340 2341 if ((alg_a & SSL_aDSS) && 2342 (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL)) 2343 idx = SSL_PKEY_DSA_SIGN; 2344 else if (alg_a & SSL_aRSA) { 2345 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) 2346 idx = SSL_PKEY_RSA_SIGN; 2347 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) 2348 idx = SSL_PKEY_RSA_ENC; 2349 } else if ((alg_a & SSL_aECDSA) && 2350 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) 2351 idx = SSL_PKEY_ECC; 2352 if (idx == -1) { 2353 SSLerr(SSL_F_SSL_GET_SIGN_PKEY, 2354 ERR_R_INTERNAL_ERROR); 2355 return (NULL); 2356 } 2357 if (pmd) 2358 *pmd = c->pkeys[idx].digest; 2359 return (c->pkeys[idx].privatekey); 2360} 2361 2362void 2363ssl_update_cache(SSL *s, int mode) 2364{ 2365 int i; 2366 2367 /* 2368 * If the session_id_length is 0, we are not supposed to cache it, 2369 * and it would be rather hard to do anyway :-) 2370 */ 2371 if (s->session->session_id_length == 0) 2372 return; 2373 2374 i = s->session_ctx->session_cache_mode; 2375 if ((i & mode) && (!s->hit) && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) 2376 || SSL_CTX_add_session(s->session_ctx, s->session)) 2377 && (s->session_ctx->new_session_cb != NULL)) { 2378 CRYPTO_add(&s->session->references, 1, CRYPTO_LOCK_SSL_SESSION); 2379 if (!s->session_ctx->new_session_cb(s, s->session)) 2380 SSL_SESSION_free(s->session); 2381 } 2382 2383 /* auto flush every 255 connections */ 2384 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && 2385 ((i & mode) == mode)) { 2386 if ((((mode & SSL_SESS_CACHE_CLIENT) ? 2387 s->session_ctx->stats.sess_connect_good : 2388 s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff) { 2389 SSL_CTX_flush_sessions(s->session_ctx, time(NULL)); 2390 } 2391 } 2392} 2393 2394const SSL_METHOD * 2395SSL_get_ssl_method(SSL *s) 2396{ 2397 return (s->method); 2398} 2399 2400int 2401SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth) 2402{ 2403 int conn = -1; 2404 int ret = 1; 2405 2406 if (s->method != meth) { 2407 if (s->handshake_func != NULL) 2408 conn = (s->handshake_func == s->method->ssl_connect); 2409 2410 if (s->method->version == meth->version) 2411 s->method = meth; 2412 else { 2413 s->method->ssl_free(s); 2414 s->method = meth; 2415 ret = s->method->ssl_new(s); 2416 } 2417 2418 if (conn == 1) 2419 s->handshake_func = meth->ssl_connect; 2420 else if (conn == 0) 2421 s->handshake_func = meth->ssl_accept; 2422 } 2423 return (ret); 2424} 2425 2426int 2427SSL_get_error(const SSL *s, int i) 2428{ 2429 int reason; 2430 unsigned long l; 2431 BIO *bio; 2432 2433 if (i > 0) 2434 return (SSL_ERROR_NONE); 2435 2436 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake 2437 * etc, where we do encode the error */ 2438 if ((l = ERR_peek_error()) != 0) { 2439 if (ERR_GET_LIB(l) == ERR_LIB_SYS) 2440 return (SSL_ERROR_SYSCALL); 2441 else 2442 return (SSL_ERROR_SSL); 2443 } 2444 2445 if ((i < 0) && SSL_want_read(s)) { 2446 bio = SSL_get_rbio(s); 2447 if (BIO_should_read(bio)) 2448 return (SSL_ERROR_WANT_READ); 2449 else if (BIO_should_write(bio)) 2450 /* 2451 * This one doesn't make too much sense... We never 2452 * try to write to the rbio, and an application 2453 * program where rbio and wbio are separate couldn't 2454 * even know what it should wait for. However if we 2455 * ever set s->rwstate incorrectly (so that we have 2456 * SSL_want_read(s) instead of SSL_want_write(s)) 2457 * and rbio and wbio *are* the same, this test works 2458 * around that bug; so it might be safer to keep it. 2459 */ 2460 return (SSL_ERROR_WANT_WRITE); 2461 else if (BIO_should_io_special(bio)) { 2462 reason = BIO_get_retry_reason(bio); 2463 if (reason == BIO_RR_CONNECT) 2464 return (SSL_ERROR_WANT_CONNECT); 2465 else if (reason == BIO_RR_ACCEPT) 2466 return (SSL_ERROR_WANT_ACCEPT); 2467 else 2468 return (SSL_ERROR_SYSCALL); /* unknown */ 2469 } 2470 } 2471 2472 if ((i < 0) && SSL_want_write(s)) { 2473 bio = SSL_get_wbio(s); 2474 if (BIO_should_write(bio)) 2475 return (SSL_ERROR_WANT_WRITE); 2476 else if (BIO_should_read(bio)) { 2477 /* 2478 * See above (SSL_want_read(s) with 2479 * BIO_should_write(bio)) 2480 */ 2481 return (SSL_ERROR_WANT_READ); 2482 } else if (BIO_should_io_special(bio)) { 2483 reason = BIO_get_retry_reason(bio); 2484 if (reason == BIO_RR_CONNECT) 2485 return (SSL_ERROR_WANT_CONNECT); 2486 else if (reason == BIO_RR_ACCEPT) 2487 return (SSL_ERROR_WANT_ACCEPT); 2488 else 2489 return (SSL_ERROR_SYSCALL); 2490 } 2491 } 2492 if ((i < 0) && SSL_want_x509_lookup(s)) { 2493 return (SSL_ERROR_WANT_X509_LOOKUP); 2494 } 2495 2496 if (i == 0) { 2497 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) && 2498 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) 2499 return (SSL_ERROR_ZERO_RETURN); 2500 } 2501 return (SSL_ERROR_SYSCALL); 2502} 2503 2504int 2505SSL_do_handshake(SSL *s) 2506{ 2507 int ret = 1; 2508 2509 if (s->handshake_func == NULL) { 2510 SSLerr(SSL_F_SSL_DO_HANDSHAKE, 2511 SSL_R_CONNECTION_TYPE_NOT_SET); 2512 return (-1); 2513 } 2514 2515 s->method->ssl_renegotiate_check(s); 2516 2517 if (SSL_in_init(s) || SSL_in_before(s)) { 2518 ret = s->handshake_func(s); 2519 } 2520 return (ret); 2521} 2522 2523/* 2524 * For the next 2 functions, SSL_clear() sets shutdown and so 2525 * one of these calls will reset it 2526 */ 2527void 2528SSL_set_accept_state(SSL *s) 2529{ 2530 s->server = 1; 2531 s->shutdown = 0; 2532 s->state = SSL_ST_ACCEPT|SSL_ST_BEFORE; 2533 s->handshake_func = s->method->ssl_accept; 2534 /* clear the current cipher */ 2535 ssl_clear_cipher_ctx(s); 2536 ssl_clear_hash_ctx(&s->read_hash); 2537 ssl_clear_hash_ctx(&s->write_hash); 2538} 2539 2540void 2541SSL_set_connect_state(SSL *s) 2542{ 2543 s->server = 0; 2544 s->shutdown = 0; 2545 s->state = SSL_ST_CONNECT|SSL_ST_BEFORE; 2546 s->handshake_func = s->method->ssl_connect; 2547 /* clear the current cipher */ 2548 ssl_clear_cipher_ctx(s); 2549 ssl_clear_hash_ctx(&s->read_hash); 2550 ssl_clear_hash_ctx(&s->write_hash); 2551} 2552 2553int 2554ssl_undefined_function(SSL *s) 2555{ 2556 SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION, 2557 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2558 return (0); 2559} 2560 2561int 2562ssl_undefined_void_function(void) 2563{ 2564 SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION, 2565 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2566 return (0); 2567} 2568 2569int 2570ssl_undefined_const_function(const SSL *s) 2571{ 2572 SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION, 2573 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2574 return (0); 2575} 2576 2577SSL_METHOD * 2578ssl_bad_method(int ver) 2579{ 2580 SSLerr(SSL_F_SSL_BAD_METHOD, 2581 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2582 return (NULL); 2583} 2584 2585const char * 2586SSL_get_version(const SSL *s) 2587{ 2588 if (s->version == TLS1_2_VERSION) 2589 return ("TLSv1.2"); 2590 else if (s->version == TLS1_1_VERSION) 2591 return ("TLSv1.1"); 2592 else if (s->version == TLS1_VERSION) 2593 return ("TLSv1"); 2594 else if (s->version == SSL3_VERSION) 2595 return ("SSLv3"); 2596 else 2597 return ("unknown"); 2598} 2599 2600SSL * 2601SSL_dup(SSL *s) 2602{ 2603 STACK_OF(X509_NAME) *sk; 2604 X509_NAME *xn; 2605 SSL *ret; 2606 int i; 2607 2608 if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL) 2609 return (NULL); 2610 2611 ret->version = s->version; 2612 ret->type = s->type; 2613 ret->method = s->method; 2614 2615 if (s->session != NULL) { 2616 /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */ 2617 SSL_copy_session_id(ret, s); 2618 } else { 2619 /* 2620 * No session has been established yet, so we have to expect 2621 * that s->cert or ret->cert will be changed later -- 2622 * they should not both point to the same object, 2623 * and thus we can't use SSL_copy_session_id. 2624 */ 2625 2626 ret->method->ssl_free(ret); 2627 ret->method = s->method; 2628 ret->method->ssl_new(ret); 2629 2630 if (s->cert != NULL) { 2631 if (ret->cert != NULL) { 2632 ssl_cert_free(ret->cert); 2633 } 2634 ret->cert = ssl_cert_dup(s->cert); 2635 if (ret->cert == NULL) 2636 goto err; 2637 } 2638 2639 SSL_set_session_id_context(ret, 2640 s->sid_ctx, s->sid_ctx_length); 2641 } 2642 2643 ret->options = s->options; 2644 ret->mode = s->mode; 2645 SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s)); 2646 SSL_set_read_ahead(ret, SSL_get_read_ahead(s)); 2647 ret->msg_callback = s->msg_callback; 2648 ret->msg_callback_arg = s->msg_callback_arg; 2649 SSL_set_verify(ret, SSL_get_verify_mode(s), 2650 SSL_get_verify_callback(s)); 2651 SSL_set_verify_depth(ret, SSL_get_verify_depth(s)); 2652 ret->generate_session_id = s->generate_session_id; 2653 2654 SSL_set_info_callback(ret, SSL_get_info_callback(s)); 2655 2656 ret->debug = s->debug; 2657 2658 /* copy app data, a little dangerous perhaps */ 2659 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, 2660 &ret->ex_data, &s->ex_data)) 2661 goto err; 2662 2663 /* setup rbio, and wbio */ 2664 if (s->rbio != NULL) { 2665 if (!BIO_dup_state(s->rbio,(char *)&ret->rbio)) 2666 goto err; 2667 } 2668 if (s->wbio != NULL) { 2669 if (s->wbio != s->rbio) { 2670 if (!BIO_dup_state(s->wbio,(char *)&ret->wbio)) 2671 goto err; 2672 } else 2673 ret->wbio = ret->rbio; 2674 } 2675 ret->rwstate = s->rwstate; 2676 ret->in_handshake = s->in_handshake; 2677 ret->handshake_func = s->handshake_func; 2678 ret->server = s->server; 2679 ret->renegotiate = s->renegotiate; 2680 ret->new_session = s->new_session; 2681 ret->quiet_shutdown = s->quiet_shutdown; 2682 ret->shutdown = s->shutdown; 2683 /* SSL_dup does not really work at any state, though */ 2684 ret->state=s->state; 2685 ret->rstate = s->rstate; 2686 2687 /* 2688 * Would have to copy ret->init_buf, ret->init_msg, ret->init_num, 2689 * ret->init_off 2690 */ 2691 ret->init_num = 0; 2692 2693 ret->hit = s->hit; 2694 2695 X509_VERIFY_PARAM_inherit(ret->param, s->param); 2696 2697 /* dup the cipher_list and cipher_list_by_id stacks */ 2698 if (s->cipher_list != NULL) { 2699 if ((ret->cipher_list = 2700 sk_SSL_CIPHER_dup(s->cipher_list)) == NULL) 2701 goto err; 2702 } 2703 if (s->cipher_list_by_id != NULL) { 2704 if ((ret->cipher_list_by_id = 2705 sk_SSL_CIPHER_dup(s->cipher_list_by_id)) == NULL) 2706 goto err; 2707 } 2708 2709 /* Dup the client_CA list */ 2710 if (s->client_CA != NULL) { 2711 if ((sk = sk_X509_NAME_dup(s->client_CA)) == NULL) goto err; 2712 ret->client_CA = sk; 2713 for (i = 0; i < sk_X509_NAME_num(sk); i++) { 2714 xn = sk_X509_NAME_value(sk, i); 2715 if (sk_X509_NAME_set(sk, i, 2716 X509_NAME_dup(xn)) == NULL) { 2717 X509_NAME_free(xn); 2718 goto err; 2719 } 2720 } 2721 } 2722 2723 if (0) { 2724 err: 2725 if (ret != NULL) 2726 SSL_free(ret); 2727 ret = NULL; 2728 } 2729 return (ret); 2730} 2731 2732void 2733ssl_clear_cipher_ctx(SSL *s) 2734{ 2735 if (s->enc_read_ctx != NULL) { 2736 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx); 2737 free(s->enc_read_ctx); 2738 s->enc_read_ctx = NULL; 2739 } 2740 if (s->enc_write_ctx != NULL) { 2741 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx); 2742 free(s->enc_write_ctx); 2743 s->enc_write_ctx = NULL; 2744 } 2745#ifndef OPENSSL_NO_COMP 2746 if (s->expand != NULL) { 2747 COMP_CTX_free(s->expand); 2748 s->expand = NULL; 2749 } 2750 if (s->compress != NULL) { 2751 COMP_CTX_free(s->compress); 2752 s->compress = NULL; 2753 } 2754#endif 2755} 2756 2757/* Fix this function so that it takes an optional type parameter */ 2758X509 * 2759SSL_get_certificate(const SSL *s) 2760{ 2761 if (s->cert != NULL) 2762 return (s->cert->key->x509); 2763 else 2764 return (NULL); 2765} 2766 2767/* Fix this function so that it takes an optional type parameter */ 2768EVP_PKEY * 2769SSL_get_privatekey(SSL *s) 2770{ 2771 if (s->cert != NULL) 2772 return (s->cert->key->privatekey); 2773 else 2774 return (NULL); 2775} 2776 2777const SSL_CIPHER * 2778SSL_get_current_cipher(const SSL *s) 2779{ 2780 if ((s->session != NULL) && (s->session->cipher != NULL)) 2781 return (s->session->cipher); 2782 return (NULL); 2783} 2784#ifdef OPENSSL_NO_COMP 2785const void * 2786SSL_get_current_compression(SSL *s) 2787{ 2788 return (NULL); 2789} 2790 2791const void * 2792SSL_get_current_expansion(SSL *s) 2793{ 2794 return (NULL); 2795} 2796#else 2797 2798const COMP_METHOD * 2799SSL_get_current_compression(SSL *s) 2800{ 2801 if (s->compress != NULL) 2802 return (s->compress->meth); 2803 return (NULL); 2804} 2805 2806const COMP_METHOD * 2807SSL_get_current_expansion(SSL *s) 2808{ 2809 if (s->expand != NULL) 2810 return (s->expand->meth); 2811 return (NULL); 2812} 2813#endif 2814 2815int 2816ssl_init_wbio_buffer(SSL *s, int push) 2817{ 2818 BIO *bbio; 2819 2820 if (s->bbio == NULL) { 2821 bbio = BIO_new(BIO_f_buffer()); 2822 if (bbio == NULL) 2823 return (0); 2824 s->bbio = bbio; 2825 } else { 2826 bbio = s->bbio; 2827 if (s->bbio == s->wbio) 2828 s->wbio = BIO_pop(s->wbio); 2829 } 2830 (void)BIO_reset(bbio); 2831/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */ 2832 if (!BIO_set_read_buffer_size(bbio, 1)) { 2833 SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER, 2834 ERR_R_BUF_LIB); 2835 return (0); 2836 } 2837 if (push) { 2838 if (s->wbio != bbio) 2839 s->wbio = BIO_push(bbio, s->wbio); 2840 } else { 2841 if (s->wbio == bbio) 2842 s->wbio = BIO_pop(bbio); 2843 } 2844 return (1); 2845} 2846 2847void 2848ssl_free_wbio_buffer(SSL *s) 2849{ 2850 if (s->bbio == NULL) 2851 return; 2852 2853 if (s->bbio == s->wbio) { 2854 /* remove buffering */ 2855 s->wbio = BIO_pop(s->wbio); 2856 } 2857 BIO_free(s->bbio); 2858 s->bbio = NULL; 2859} 2860 2861void 2862SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) 2863{ 2864 ctx->quiet_shutdown = mode; 2865} 2866 2867int 2868SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) 2869{ 2870 return (ctx->quiet_shutdown); 2871} 2872 2873void 2874SSL_set_quiet_shutdown(SSL *s, int mode) 2875{ 2876 s->quiet_shutdown = mode; 2877} 2878 2879int 2880SSL_get_quiet_shutdown(const SSL *s) 2881{ 2882 return (s->quiet_shutdown); 2883} 2884 2885void 2886SSL_set_shutdown(SSL *s, int mode) 2887{ 2888 s->shutdown = mode; 2889} 2890 2891int 2892SSL_get_shutdown(const SSL *s) 2893{ 2894 return (s->shutdown); 2895} 2896 2897int 2898SSL_version(const SSL *s) 2899{ 2900 return (s->version); 2901} 2902 2903SSL_CTX * 2904SSL_get_SSL_CTX(const SSL *ssl) 2905{ 2906 return (ssl->ctx); 2907} 2908 2909SSL_CTX * 2910SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx) 2911{ 2912 if (ssl->ctx == ctx) 2913 return (ssl->ctx); 2914#ifndef OPENSSL_NO_TLSEXT 2915 if (ctx == NULL) 2916 ctx = ssl->initial_ctx; 2917#endif 2918 if (ssl->cert != NULL) 2919 ssl_cert_free(ssl->cert); 2920 ssl->cert = ssl_cert_dup(ctx->cert); 2921 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX); 2922 if (ssl->ctx != NULL) 2923 SSL_CTX_free(ssl->ctx); /* decrement reference count */ 2924 ssl->ctx = ctx; 2925 return (ssl->ctx); 2926} 2927 2928#ifndef OPENSSL_NO_STDIO 2929int 2930SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) 2931{ 2932 return (X509_STORE_set_default_paths(ctx->cert_store)); 2933} 2934 2935int 2936SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile, 2937 const char *CApath) 2938{ 2939 return (X509_STORE_load_locations(ctx->cert_store, CAfile, CApath)); 2940} 2941#endif 2942 2943void 2944SSL_set_info_callback(SSL *ssl, 2945 void (*cb)(const SSL *ssl, int type, int val)) 2946{ 2947 ssl->info_callback = cb; 2948} 2949 2950void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type, int val) 2951{ 2952 return (ssl->info_callback); 2953} 2954 2955int 2956SSL_state(const SSL *ssl) 2957{ 2958 return (ssl->state); 2959} 2960 2961void 2962SSL_set_state(SSL *ssl, int state) 2963{ 2964 ssl->state = state; 2965} 2966 2967void 2968SSL_set_verify_result(SSL *ssl, long arg) 2969{ 2970 ssl->verify_result = arg; 2971} 2972 2973long 2974SSL_get_verify_result(const SSL *ssl) 2975{ 2976 return (ssl->verify_result); 2977} 2978 2979int 2980SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 2981 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 2982{ 2983 return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, 2984 new_func, dup_func, free_func)); 2985} 2986 2987int 2988SSL_set_ex_data(SSL *s, int idx, void *arg) 2989{ 2990 return (CRYPTO_set_ex_data(&s->ex_data, idx, arg)); 2991} 2992 2993void * 2994SSL_get_ex_data(const SSL *s, int idx) 2995{ 2996 return (CRYPTO_get_ex_data(&s->ex_data, idx)); 2997} 2998 2999int 3000SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 3001 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 3002{ 3003 return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, 3004 new_func, dup_func, free_func)); 3005} 3006 3007int 3008SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) 3009{ 3010 return (CRYPTO_set_ex_data(&s->ex_data, idx, arg)); 3011} 3012 3013void * 3014SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) 3015{ 3016 return (CRYPTO_get_ex_data(&s->ex_data, idx)); 3017} 3018 3019int 3020ssl_ok(SSL *s) 3021{ 3022 return (1); 3023} 3024 3025X509_STORE * 3026SSL_CTX_get_cert_store(const SSL_CTX *ctx) 3027{ 3028 return (ctx->cert_store); 3029} 3030 3031void 3032SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) 3033{ 3034 if (ctx->cert_store != NULL) 3035 X509_STORE_free(ctx->cert_store); 3036 ctx->cert_store = store; 3037} 3038 3039int 3040SSL_want(const SSL *s) 3041{ 3042 return (s->rwstate); 3043} 3044 3045/*! 3046 * \brief Set the callback for generating temporary RSA keys. 3047 * \param ctx the SSL context. 3048 * \param cb the callback 3049 */ 3050 3051void 3052SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, RSA *(*cb)(SSL *ssl, 3053 int is_export, 3054int keylength)) 3055{ 3056 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); 3057} 3058 3059void 3060SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, 3061 int is_export, 3062int keylength)) 3063{ 3064 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); 3065} 3066 3067#ifdef DOXYGEN 3068/*! 3069 * \brief The RSA temporary key callback function. 3070 * \param ssl the SSL session. 3071 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite. 3072 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size 3073 * of the required key in bits. 3074 * \return the temporary RSA key. 3075 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback 3076 */ 3077 3078RSA * 3079cb(SSL *ssl, int is_export, int keylength) 3080{} 3081#endif 3082 3083/*! 3084 * \brief Set the callback for generating temporary DH keys. 3085 * \param ctx the SSL context. 3086 * \param dh the callback 3087 */ 3088 3089#ifndef OPENSSL_NO_DH 3090void 3091SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, DH *(*dh)(SSL *ssl, int is_export, 3092 int keylength)) 3093{ 3094 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); 3095} 3096 3097void 3098SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export, 3099 int keylength)) 3100{ 3101 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); 3102} 3103#endif 3104 3105#ifndef OPENSSL_NO_ECDH 3106void 3107SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx, EC_KEY *(*ecdh)(SSL *ssl, 3108 int is_export, int keylength)) 3109{ 3110 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, 3111 (void (*)(void))ecdh); 3112} 3113 3114void 3115SSL_set_tmp_ecdh_callback(SSL *ssl, EC_KEY *(*ecdh)(SSL *ssl, int is_export, 3116 int keylength)) 3117{ 3118 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh); 3119} 3120#endif 3121 3122#ifndef OPENSSL_NO_PSK 3123int 3124SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) 3125{ 3126 if (identity_hint != NULL && strlen(identity_hint) > 3127 PSK_MAX_IDENTITY_LEN) { 3128 SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT, 3129 SSL_R_DATA_LENGTH_TOO_LONG); 3130 return (0); 3131 } 3132 if (ctx->psk_identity_hint != NULL) 3133 free(ctx->psk_identity_hint); 3134 if (identity_hint != NULL) { 3135 ctx->psk_identity_hint = BUF_strdup(identity_hint); 3136 if (ctx->psk_identity_hint == NULL) 3137 return (0); 3138 } else 3139 ctx->psk_identity_hint = NULL; 3140 return (1); 3141} 3142 3143int 3144SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) 3145{ 3146 if (s == NULL) 3147 return (0); 3148 3149 if (s->session == NULL) 3150 return (1); /* session not created yet, ignored */ 3151 3152 if (identity_hint != NULL && strlen(identity_hint) > 3153 PSK_MAX_IDENTITY_LEN) { 3154 SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, 3155 SSL_R_DATA_LENGTH_TOO_LONG); 3156 return (0); 3157 } 3158 if (s->session->psk_identity_hint != NULL) 3159 free(s->session->psk_identity_hint); 3160 if (identity_hint != NULL) { 3161 s->session->psk_identity_hint = BUF_strdup(identity_hint); 3162 if (s->session->psk_identity_hint == NULL) 3163 return (0); 3164 } else 3165 s->session->psk_identity_hint = NULL; 3166 return (1); 3167} 3168 3169const char * 3170SSL_get_psk_identity_hint(const SSL *s) 3171{ 3172 if (s == NULL || s->session == NULL) 3173 return (NULL); 3174 return (s->session->psk_identity_hint); 3175} 3176 3177const char * 3178SSL_get_psk_identity(const SSL *s) 3179{ 3180 if (s == NULL || s->session == NULL) 3181 return (NULL); 3182 return (s->session->psk_identity); 3183} 3184 3185void 3186SSL_set_psk_client_callback(SSL *s, 3187 unsigned int (*cb)(SSL *ssl, const char *hint, 3188 char *identity, unsigned int max_identity_len, unsigned char *psk, 3189 unsigned int max_psk_len)) 3190{ 3191 s->psk_client_callback = cb; 3192} 3193 3194void 3195SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, 3196 unsigned int (*cb)(SSL *ssl, const char *hint, 3197 char *identity, unsigned int max_identity_len, unsigned char *psk, 3198 unsigned int max_psk_len)) 3199{ 3200 ctx->psk_client_callback = cb; 3201} 3202 3203void 3204SSL_set_psk_server_callback(SSL *s, 3205 unsigned int (*cb)(SSL *ssl, const char *identity, 3206 unsigned char *psk, unsigned int max_psk_len)) 3207{ 3208 s->psk_server_callback = cb; 3209} 3210 3211void 3212SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, 3213 unsigned int (*cb)(SSL *ssl, const char *identity, 3214 unsigned char *psk, unsigned int max_psk_len)) 3215{ 3216 ctx->psk_server_callback = cb; 3217} 3218#endif 3219 3220void 3221SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version, 3222 int content_type, const void *buf, size_t len, SSL *ssl, void *arg)) 3223{ 3224 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, 3225 (void (*)(void))cb); 3226} 3227 3228void 3229SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version, 3230 int content_type, const void *buf, size_t len, SSL *ssl, void *arg)) 3231{ 3232 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); 3233} 3234 3235/* 3236 * Allocates new EVP_MD_CTX and sets pointer to it into given pointer 3237 * vairable, freeing EVP_MD_CTX previously stored in that variable, if 3238 * any. If EVP_MD pointer is passed, initializes ctx with this md 3239 * Returns newly allocated ctx; 3240 */ 3241EVP_MD_CTX * 3242ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md) 3243{ 3244 ssl_clear_hash_ctx(hash); 3245 *hash = EVP_MD_CTX_create(); 3246 if (md) 3247 EVP_DigestInit_ex(*hash, md, NULL); 3248 return (*hash); 3249} 3250 3251void 3252ssl_clear_hash_ctx(EVP_MD_CTX **hash) 3253{ 3254 if (*hash) 3255 EVP_MD_CTX_destroy(*hash); 3256 *hash = NULL; 3257} 3258 3259void 3260SSL_set_debug(SSL *s, int debug) 3261{ 3262 s->debug = debug; 3263} 3264 3265int 3266SSL_cache_hit(SSL *s) 3267{ 3268 return (s->hit); 3269} 3270 3271IMPLEMENT_STACK_OF(SSL_CIPHER) 3272IMPLEMENT_STACK_OF(SSL_COMP) 3273IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, 3274 ssl_cipher_id); 3275