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