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