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