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