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