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