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