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