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