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