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