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