ssl_lib.c revision 1.229
1/* $OpenBSD: ssl_lib.c,v 1.229 2020/09/16 07:25:15 schwarze 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 if (ctx == NULL) 1340 return NULL; 1341 return ctx->cipher_list; 1342} 1343 1344/* Specify the ciphers to be used by default by the SSL_CTX. */ 1345int 1346SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) 1347{ 1348 STACK_OF(SSL_CIPHER) *ciphers; 1349 1350 /* 1351 * ssl_create_cipher_list may return an empty stack if it was unable to 1352 * find a cipher matching the given rule string (for example if the 1353 * rule string specifies a cipher which has been disabled). This is not 1354 * an error as far as ssl_create_cipher_list is concerned, and hence 1355 * ctx->cipher_list has been updated. 1356 */ 1357 ciphers = ssl_create_cipher_list(ctx->method, &ctx->cipher_list, 1358 ctx->internal->cipher_list_tls13, str); 1359 if (ciphers == NULL) { 1360 return (0); 1361 } else if (sk_SSL_CIPHER_num(ciphers) == 0) { 1362 SSLerrorx(SSL_R_NO_CIPHER_MATCH); 1363 return (0); 1364 } 1365 return (1); 1366} 1367 1368int 1369SSL_CTX_set_ciphersuites(SSL_CTX *ctx, const char *str) 1370{ 1371 if (!ssl_parse_ciphersuites(&ctx->internal->cipher_list_tls13, str)) { 1372 SSLerrorx(SSL_R_NO_CIPHER_MATCH); 1373 return 0; 1374 } 1375 if (!ssl_merge_cipherlists(ctx->cipher_list, 1376 ctx->internal->cipher_list_tls13, &ctx->cipher_list)) 1377 return 0; 1378 1379 return 1; 1380} 1381 1382/* Specify the ciphers to be used by the SSL. */ 1383int 1384SSL_set_cipher_list(SSL *s, const char *str) 1385{ 1386 STACK_OF(SSL_CIPHER) *ciphers, *ciphers_tls13; 1387 1388 if ((ciphers_tls13 = s->internal->cipher_list_tls13) == NULL) 1389 ciphers_tls13 = s->ctx->internal->cipher_list_tls13; 1390 1391 /* See comment in SSL_CTX_set_cipher_list. */ 1392 ciphers = ssl_create_cipher_list(s->ctx->method, &s->cipher_list, 1393 ciphers_tls13, str); 1394 if (ciphers == NULL) { 1395 return (0); 1396 } else if (sk_SSL_CIPHER_num(ciphers) == 0) { 1397 SSLerror(s, SSL_R_NO_CIPHER_MATCH); 1398 return (0); 1399 } 1400 return (1); 1401} 1402 1403int 1404SSL_set_ciphersuites(SSL *s, const char *str) 1405{ 1406 STACK_OF(SSL_CIPHER) *ciphers; 1407 1408 if ((ciphers = s->cipher_list) == NULL) 1409 ciphers = s->ctx->cipher_list; 1410 1411 if (!ssl_parse_ciphersuites(&s->internal->cipher_list_tls13, str)) { 1412 SSLerrorx(SSL_R_NO_CIPHER_MATCH); 1413 return (0); 1414 } 1415 if (!ssl_merge_cipherlists(ciphers, s->internal->cipher_list_tls13, 1416 &s->cipher_list)) 1417 return 0; 1418 1419 return 1; 1420} 1421 1422char * 1423SSL_get_shared_ciphers(const SSL *s, char *buf, int len) 1424{ 1425 STACK_OF(SSL_CIPHER) *ciphers; 1426 const SSL_CIPHER *cipher; 1427 size_t curlen = 0; 1428 char *end; 1429 int i; 1430 1431 if (s->session == NULL || s->session->ciphers == NULL || len < 2) 1432 return (NULL); 1433 1434 ciphers = s->session->ciphers; 1435 if (sk_SSL_CIPHER_num(ciphers) == 0) 1436 return (NULL); 1437 1438 buf[0] = '\0'; 1439 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { 1440 cipher = sk_SSL_CIPHER_value(ciphers, i); 1441 end = buf + curlen; 1442 if (strlcat(buf, cipher->name, len) >= len || 1443 (curlen = strlcat(buf, ":", len)) >= len) { 1444 /* remove truncated cipher from list */ 1445 *end = '\0'; 1446 break; 1447 } 1448 } 1449 /* remove trailing colon */ 1450 if ((end = strrchr(buf, ':')) != NULL) 1451 *end = '\0'; 1452 return (buf); 1453} 1454 1455/* 1456 * Return a servername extension value if provided in Client Hello, or NULL. 1457 * So far, only host_name types are defined (RFC 3546). 1458 */ 1459const char * 1460SSL_get_servername(const SSL *s, const int type) 1461{ 1462 if (type != TLSEXT_NAMETYPE_host_name) 1463 return (NULL); 1464 1465 return (s->session && !s->tlsext_hostname ? 1466 s->session->tlsext_hostname : 1467 s->tlsext_hostname); 1468} 1469 1470int 1471SSL_get_servername_type(const SSL *s) 1472{ 1473 if (s->session && 1474 (!s->tlsext_hostname ? 1475 s->session->tlsext_hostname : s->tlsext_hostname)) 1476 return (TLSEXT_NAMETYPE_host_name); 1477 return (-1); 1478} 1479 1480/* 1481 * SSL_select_next_proto implements standard protocol selection. It is 1482 * expected that this function is called from the callback set by 1483 * SSL_CTX_set_alpn_select_cb. 1484 * 1485 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte 1486 * strings. The length byte itself is not included in the length. A byte 1487 * string of length 0 is invalid. No byte string may be truncated. 1488 * 1489 * It returns either: 1490 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or 1491 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached. 1492 */ 1493int 1494SSL_select_next_proto(unsigned char **out, unsigned char *outlen, 1495 const unsigned char *server, unsigned int server_len, 1496 const unsigned char *client, unsigned int client_len) 1497{ 1498 unsigned int i, j; 1499 const unsigned char *result; 1500 int status = OPENSSL_NPN_UNSUPPORTED; 1501 1502 /* 1503 * For each protocol in server preference order, 1504 * see if we support it. 1505 */ 1506 for (i = 0; i < server_len; ) { 1507 for (j = 0; j < client_len; ) { 1508 if (server[i] == client[j] && 1509 memcmp(&server[i + 1], 1510 &client[j + 1], server[i]) == 0) { 1511 /* We found a match */ 1512 result = &server[i]; 1513 status = OPENSSL_NPN_NEGOTIATED; 1514 goto found; 1515 } 1516 j += client[j]; 1517 j++; 1518 } 1519 i += server[i]; 1520 i++; 1521 } 1522 1523 /* There's no overlap between our protocols and the server's list. */ 1524 result = client; 1525 status = OPENSSL_NPN_NO_OVERLAP; 1526 1527found: 1528 *out = (unsigned char *) result + 1; 1529 *outlen = result[0]; 1530 return (status); 1531} 1532 1533/* SSL_get0_next_proto_negotiated is deprecated. */ 1534void 1535SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data, 1536 unsigned int *len) 1537{ 1538 *data = NULL; 1539 *len = 0; 1540} 1541 1542/* SSL_CTX_set_next_protos_advertised_cb is deprecated. */ 1543void 1544SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *ctx, int (*cb) (SSL *ssl, 1545 const unsigned char **out, unsigned int *outlen, void *arg), void *arg) 1546{ 1547} 1548 1549/* SSL_CTX_set_next_proto_select_cb is deprecated. */ 1550void 1551SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx, int (*cb) (SSL *s, 1552 unsigned char **out, unsigned char *outlen, const unsigned char *in, 1553 unsigned int inlen, void *arg), void *arg) 1554{ 1555} 1556 1557/* 1558 * SSL_CTX_set_alpn_protos sets the ALPN protocol list to the specified 1559 * protocols, which must be in wire-format (i.e. a series of non-empty, 1560 * 8-bit length-prefixed strings). Returns 0 on success. 1561 */ 1562int 1563SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos, 1564 unsigned int protos_len) 1565{ 1566 int failed = 1; 1567 1568 if (protos == NULL || protos_len == 0) 1569 goto err; 1570 1571 free(ctx->internal->alpn_client_proto_list); 1572 ctx->internal->alpn_client_proto_list = NULL; 1573 ctx->internal->alpn_client_proto_list_len = 0; 1574 1575 if ((ctx->internal->alpn_client_proto_list = malloc(protos_len)) 1576 == NULL) 1577 goto err; 1578 ctx->internal->alpn_client_proto_list_len = protos_len; 1579 1580 memcpy(ctx->internal->alpn_client_proto_list, protos, protos_len); 1581 1582 failed = 0; 1583 1584 err: 1585 /* NOTE: Return values are the reverse of what you expect. */ 1586 return (failed); 1587} 1588 1589/* 1590 * SSL_set_alpn_protos sets the ALPN protocol list to the specified 1591 * protocols, which must be in wire-format (i.e. a series of non-empty, 1592 * 8-bit length-prefixed strings). Returns 0 on success. 1593 */ 1594int 1595SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos, 1596 unsigned int protos_len) 1597{ 1598 int failed = 1; 1599 1600 if (protos == NULL || protos_len == 0) 1601 goto err; 1602 1603 free(ssl->internal->alpn_client_proto_list); 1604 ssl->internal->alpn_client_proto_list = NULL; 1605 ssl->internal->alpn_client_proto_list_len = 0; 1606 1607 if ((ssl->internal->alpn_client_proto_list = malloc(protos_len)) 1608 == NULL) 1609 goto err; 1610 ssl->internal->alpn_client_proto_list_len = protos_len; 1611 1612 memcpy(ssl->internal->alpn_client_proto_list, protos, protos_len); 1613 1614 failed = 0; 1615 1616 err: 1617 /* NOTE: Return values are the reverse of what you expect. */ 1618 return (failed); 1619} 1620 1621/* 1622 * SSL_CTX_set_alpn_select_cb sets a callback function that is called during 1623 * ClientHello processing in order to select an ALPN protocol from the 1624 * client's list of offered protocols. 1625 */ 1626void 1627SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx, 1628 int (*cb) (SSL *ssl, const unsigned char **out, unsigned char *outlen, 1629 const unsigned char *in, unsigned int inlen, void *arg), void *arg) 1630{ 1631 ctx->internal->alpn_select_cb = cb; 1632 ctx->internal->alpn_select_cb_arg = arg; 1633} 1634 1635/* 1636 * SSL_get0_alpn_selected gets the selected ALPN protocol (if any). On return 1637 * it sets data to point to len bytes of protocol name (not including the 1638 * leading length-prefix byte). If the server didn't respond with* a negotiated 1639 * protocol then len will be zero. 1640 */ 1641void 1642SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data, 1643 unsigned int *len) 1644{ 1645 *data = NULL; 1646 *len = 0; 1647 1648 *data = ssl->s3->internal->alpn_selected; 1649 *len = ssl->s3->internal->alpn_selected_len; 1650} 1651 1652int 1653SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen, 1654 const char *label, size_t llen, const unsigned char *p, size_t plen, 1655 int use_context) 1656{ 1657 return (tls1_export_keying_material(s, out, olen, 1658 label, llen, p, plen, use_context)); 1659} 1660 1661static unsigned long 1662ssl_session_hash(const SSL_SESSION *a) 1663{ 1664 unsigned long l; 1665 1666 l = (unsigned long) 1667 ((unsigned int) a->session_id[0] )| 1668 ((unsigned int) a->session_id[1]<< 8L)| 1669 ((unsigned long)a->session_id[2]<<16L)| 1670 ((unsigned long)a->session_id[3]<<24L); 1671 return (l); 1672} 1673 1674/* 1675 * NB: If this function (or indeed the hash function which uses a sort of 1676 * coarser function than this one) is changed, ensure 1677 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being 1678 * able to construct an SSL_SESSION that will collide with any existing session 1679 * with a matching session ID. 1680 */ 1681static int 1682ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) 1683{ 1684 if (a->ssl_version != b->ssl_version) 1685 return (1); 1686 if (a->session_id_length != b->session_id_length) 1687 return (1); 1688 if (timingsafe_memcmp(a->session_id, b->session_id, a->session_id_length) != 0) 1689 return (1); 1690 return (0); 1691} 1692 1693/* 1694 * These wrapper functions should remain rather than redeclaring 1695 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each 1696 * variable. The reason is that the functions aren't static, they're exposed via 1697 * ssl.h. 1698 */ 1699static unsigned long 1700ssl_session_LHASH_HASH(const void *arg) 1701{ 1702 const SSL_SESSION *a = arg; 1703 1704 return ssl_session_hash(a); 1705} 1706 1707static int 1708ssl_session_LHASH_COMP(const void *arg1, const void *arg2) 1709{ 1710 const SSL_SESSION *a = arg1; 1711 const SSL_SESSION *b = arg2; 1712 1713 return ssl_session_cmp(a, b); 1714} 1715 1716SSL_CTX * 1717SSL_CTX_new(const SSL_METHOD *meth) 1718{ 1719 SSL_CTX *ret; 1720 1721 if (!OPENSSL_init_ssl(0, NULL)) { 1722 SSLerrorx(SSL_R_LIBRARY_BUG); 1723 return (NULL); 1724 } 1725 1726 if (meth == NULL) { 1727 SSLerrorx(SSL_R_NULL_SSL_METHOD_PASSED); 1728 return (NULL); 1729 } 1730 1731 if ((ret = calloc(1, sizeof(*ret))) == NULL) { 1732 SSLerrorx(ERR_R_MALLOC_FAILURE); 1733 return (NULL); 1734 } 1735 if ((ret->internal = calloc(1, sizeof(*ret->internal))) == NULL) { 1736 free(ret); 1737 SSLerrorx(ERR_R_MALLOC_FAILURE); 1738 return (NULL); 1739 } 1740 1741 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) { 1742 SSLerrorx(SSL_R_X509_VERIFICATION_SETUP_PROBLEMS); 1743 goto err; 1744 } 1745 1746 ret->method = meth; 1747 ret->internal->min_version = meth->internal->min_version; 1748 ret->internal->max_version = meth->internal->max_version; 1749 ret->internal->mode = SSL_MODE_AUTO_RETRY; 1750 1751 ret->cert_store = NULL; 1752 ret->internal->session_cache_mode = SSL_SESS_CACHE_SERVER; 1753 ret->internal->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; 1754 ret->internal->session_cache_head = NULL; 1755 ret->internal->session_cache_tail = NULL; 1756 1757 /* We take the system default */ 1758 ret->session_timeout = ssl_get_default_timeout(); 1759 1760 ret->internal->new_session_cb = 0; 1761 ret->internal->remove_session_cb = 0; 1762 ret->internal->get_session_cb = 0; 1763 ret->internal->generate_session_id = 0; 1764 1765 memset((char *)&ret->internal->stats, 0, sizeof(ret->internal->stats)); 1766 1767 ret->references = 1; 1768 ret->internal->quiet_shutdown = 0; 1769 1770 ret->internal->info_callback = NULL; 1771 1772 ret->internal->app_verify_callback = 0; 1773 ret->internal->app_verify_arg = NULL; 1774 1775 ret->internal->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT; 1776 ret->internal->read_ahead = 0; 1777 ret->internal->msg_callback = 0; 1778 ret->internal->msg_callback_arg = NULL; 1779 ret->verify_mode = SSL_VERIFY_NONE; 1780 ret->sid_ctx_length = 0; 1781 ret->internal->default_verify_callback = NULL; 1782 1783 if ((ret->internal->cert = ssl_cert_new()) == NULL) 1784 goto err; 1785 1786 ret->default_passwd_callback = 0; 1787 ret->default_passwd_callback_userdata = NULL; 1788 ret->internal->client_cert_cb = 0; 1789 ret->internal->app_gen_cookie_cb = 0; 1790 ret->internal->app_verify_cookie_cb = 0; 1791 1792 ret->internal->sessions = lh_SSL_SESSION_new(); 1793 if (ret->internal->sessions == NULL) 1794 goto err; 1795 ret->cert_store = X509_STORE_new(); 1796 if (ret->cert_store == NULL) 1797 goto err; 1798 1799 ssl_create_cipher_list(ret->method, &ret->cipher_list, 1800 NULL, SSL_DEFAULT_CIPHER_LIST); 1801 if (ret->cipher_list == NULL || 1802 sk_SSL_CIPHER_num(ret->cipher_list) <= 0) { 1803 SSLerrorx(SSL_R_LIBRARY_HAS_NO_CIPHERS); 1804 goto err2; 1805 } 1806 1807 ret->param = X509_VERIFY_PARAM_new(); 1808 if (!ret->param) 1809 goto err; 1810 1811 if ((ret->internal->client_CA = sk_X509_NAME_new_null()) == NULL) 1812 goto err; 1813 1814 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->internal->ex_data); 1815 1816 ret->extra_certs = NULL; 1817 1818 ret->internal->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; 1819 1820 ret->internal->tlsext_servername_callback = 0; 1821 ret->internal->tlsext_servername_arg = NULL; 1822 1823 /* Setup RFC4507 ticket keys */ 1824 arc4random_buf(ret->internal->tlsext_tick_key_name, 16); 1825 arc4random_buf(ret->internal->tlsext_tick_hmac_key, 16); 1826 arc4random_buf(ret->internal->tlsext_tick_aes_key, 16); 1827 1828 ret->internal->tlsext_status_cb = 0; 1829 ret->internal->tlsext_status_arg = NULL; 1830 1831#ifndef OPENSSL_NO_ENGINE 1832 ret->internal->client_cert_engine = NULL; 1833#ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO 1834#define eng_strx(x) #x 1835#define eng_str(x) eng_strx(x) 1836 /* Use specific client engine automatically... ignore errors */ 1837 { 1838 ENGINE *eng; 1839 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO)); 1840 if (!eng) { 1841 ERR_clear_error(); 1842 ENGINE_load_builtin_engines(); 1843 eng = ENGINE_by_id(eng_str( 1844 OPENSSL_SSL_CLIENT_ENGINE_AUTO)); 1845 } 1846 if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng)) 1847 ERR_clear_error(); 1848 } 1849#endif 1850#endif 1851 /* 1852 * Default is to connect to non-RI servers. When RI is more widely 1853 * deployed might change this. 1854 */ 1855 ret->internal->options |= SSL_OP_LEGACY_SERVER_CONNECT; 1856 1857 return (ret); 1858err: 1859 SSLerrorx(ERR_R_MALLOC_FAILURE); 1860err2: 1861 SSL_CTX_free(ret); 1862 return (NULL); 1863} 1864 1865void 1866SSL_CTX_free(SSL_CTX *ctx) 1867{ 1868 int i; 1869 1870 if (ctx == NULL) 1871 return; 1872 1873 i = CRYPTO_add(&ctx->references, -1, CRYPTO_LOCK_SSL_CTX); 1874 if (i > 0) 1875 return; 1876 1877 X509_VERIFY_PARAM_free(ctx->param); 1878 1879 /* 1880 * Free internal session cache. However: the remove_cb() may reference 1881 * the ex_data of SSL_CTX, thus the ex_data store can only be removed 1882 * after the sessions were flushed. 1883 * As the ex_data handling routines might also touch the session cache, 1884 * the most secure solution seems to be: empty (flush) the cache, then 1885 * free ex_data, then finally free the cache. 1886 * (See ticket [openssl.org #212].) 1887 */ 1888 if (ctx->internal->sessions != NULL) 1889 SSL_CTX_flush_sessions(ctx, 0); 1890 1891 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ctx, &ctx->internal->ex_data); 1892 1893 lh_SSL_SESSION_free(ctx->internal->sessions); 1894 1895 X509_STORE_free(ctx->cert_store); 1896 sk_SSL_CIPHER_free(ctx->cipher_list); 1897 sk_SSL_CIPHER_free(ctx->internal->cipher_list_tls13); 1898 ssl_cert_free(ctx->internal->cert); 1899 sk_X509_NAME_pop_free(ctx->internal->client_CA, X509_NAME_free); 1900 sk_X509_pop_free(ctx->extra_certs, X509_free); 1901 1902#ifndef OPENSSL_NO_SRTP 1903 if (ctx->internal->srtp_profiles) 1904 sk_SRTP_PROTECTION_PROFILE_free(ctx->internal->srtp_profiles); 1905#endif 1906 1907#ifndef OPENSSL_NO_ENGINE 1908 ENGINE_finish(ctx->internal->client_cert_engine); 1909#endif 1910 1911 free(ctx->internal->tlsext_ecpointformatlist); 1912 free(ctx->internal->tlsext_supportedgroups); 1913 1914 free(ctx->internal->alpn_client_proto_list); 1915 1916 free(ctx->internal); 1917 free(ctx); 1918} 1919 1920int 1921SSL_CTX_up_ref(SSL_CTX *ctx) 1922{ 1923 int refs = CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX); 1924 return ((refs > 1) ? 1 : 0); 1925} 1926 1927pem_password_cb * 1928SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx) 1929{ 1930 return (ctx->default_passwd_callback); 1931} 1932 1933void 1934SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) 1935{ 1936 ctx->default_passwd_callback = cb; 1937} 1938 1939void * 1940SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx) 1941{ 1942 return ctx->default_passwd_callback_userdata; 1943} 1944 1945void 1946SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) 1947{ 1948 ctx->default_passwd_callback_userdata = u; 1949} 1950 1951void 1952SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, 1953 int (*cb)(X509_STORE_CTX *, void *), void *arg) 1954{ 1955 ctx->internal->app_verify_callback = cb; 1956 ctx->internal->app_verify_arg = arg; 1957} 1958 1959void 1960SSL_CTX_set_verify(SSL_CTX *ctx, int mode, int (*cb)(int, X509_STORE_CTX *)) 1961{ 1962 ctx->verify_mode = mode; 1963 ctx->internal->default_verify_callback = cb; 1964} 1965 1966void 1967SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) 1968{ 1969 X509_VERIFY_PARAM_set_depth(ctx->param, depth); 1970} 1971 1972static int 1973ssl_cert_can_sign(X509 *x) 1974{ 1975 /* This call populates extension flags (ex_flags). */ 1976 X509_check_purpose(x, -1, 0); 1977 1978 /* Key usage, if present, must allow signing. */ 1979 return ((x->ex_flags & EXFLAG_KUSAGE) == 0 || 1980 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)); 1981} 1982 1983void 1984ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher) 1985{ 1986 unsigned long mask_a, mask_k; 1987 CERT_PKEY *cpk; 1988 1989 if (c == NULL) 1990 return; 1991 1992 mask_a = SSL_aNULL | SSL_aTLS1_3; 1993 mask_k = SSL_kECDHE | SSL_kTLS1_3; 1994 1995 if (c->dh_tmp != NULL || c->dh_tmp_cb != NULL || c->dh_tmp_auto != 0) 1996 mask_k |= SSL_kDHE; 1997 1998 cpk = &(c->pkeys[SSL_PKEY_ECC]); 1999 if (cpk->x509 != NULL && cpk->privatekey != NULL) { 2000 if (ssl_cert_can_sign(cpk->x509)) 2001 mask_a |= SSL_aECDSA; 2002 } 2003 2004 cpk = &(c->pkeys[SSL_PKEY_GOST01]); 2005 if (cpk->x509 != NULL && cpk->privatekey != NULL) { 2006 mask_k |= SSL_kGOST; 2007 mask_a |= SSL_aGOST01; 2008 } 2009 2010 cpk = &(c->pkeys[SSL_PKEY_RSA]); 2011 if (cpk->x509 != NULL && cpk->privatekey != NULL) { 2012 mask_a |= SSL_aRSA; 2013 mask_k |= SSL_kRSA; 2014 } 2015 2016 c->mask_k = mask_k; 2017 c->mask_a = mask_a; 2018 c->valid = 1; 2019} 2020 2021/* See if this handshake is using an ECC cipher suite. */ 2022int 2023ssl_using_ecc_cipher(SSL *s) 2024{ 2025 unsigned long alg_a, alg_k; 2026 2027 alg_a = S3I(s)->hs.new_cipher->algorithm_auth; 2028 alg_k = S3I(s)->hs.new_cipher->algorithm_mkey; 2029 2030 return SSI(s)->tlsext_ecpointformatlist != NULL && 2031 SSI(s)->tlsext_ecpointformatlist_length > 0 && 2032 ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)); 2033} 2034 2035int 2036ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) 2037{ 2038 const SSL_CIPHER *cs = S3I(s)->hs.new_cipher; 2039 unsigned long alg_a; 2040 2041 alg_a = cs->algorithm_auth; 2042 2043 if (alg_a & SSL_aECDSA) { 2044 /* This call populates extension flags (ex_flags). */ 2045 X509_check_purpose(x, -1, 0); 2046 2047 /* Key usage, if present, must allow signing. */ 2048 if ((x->ex_flags & EXFLAG_KUSAGE) && 2049 ((x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) == 0)) { 2050 SSLerror(s, SSL_R_ECC_CERT_NOT_FOR_SIGNING); 2051 return (0); 2052 } 2053 } 2054 2055 return (1); 2056} 2057 2058CERT_PKEY * 2059ssl_get_server_send_pkey(const SSL *s) 2060{ 2061 unsigned long alg_a; 2062 CERT *c; 2063 int i; 2064 2065 c = s->cert; 2066 ssl_set_cert_masks(c, S3I(s)->hs.new_cipher); 2067 2068 alg_a = S3I(s)->hs.new_cipher->algorithm_auth; 2069 2070 if (alg_a & SSL_aECDSA) { 2071 i = SSL_PKEY_ECC; 2072 } else if (alg_a & SSL_aRSA) { 2073 i = SSL_PKEY_RSA; 2074 } else if (alg_a & SSL_aGOST01) { 2075 i = SSL_PKEY_GOST01; 2076 } else { /* if (alg_a & SSL_aNULL) */ 2077 SSLerror(s, ERR_R_INTERNAL_ERROR); 2078 return (NULL); 2079 } 2080 2081 return (c->pkeys + i); 2082} 2083 2084EVP_PKEY * 2085ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher, const EVP_MD **pmd, 2086 const struct ssl_sigalg **sap) 2087{ 2088 const struct ssl_sigalg *sigalg = NULL; 2089 EVP_PKEY *pkey = NULL; 2090 unsigned long alg_a; 2091 CERT *c; 2092 int idx = -1; 2093 2094 alg_a = cipher->algorithm_auth; 2095 c = s->cert; 2096 2097 if (alg_a & SSL_aRSA) { 2098 idx = SSL_PKEY_RSA; 2099 } else if ((alg_a & SSL_aECDSA) && 2100 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) 2101 idx = SSL_PKEY_ECC; 2102 if (idx == -1) { 2103 SSLerror(s, ERR_R_INTERNAL_ERROR); 2104 return (NULL); 2105 } 2106 2107 pkey = c->pkeys[idx].privatekey; 2108 if ((sigalg = ssl_sigalg_select(s, pkey)) == NULL) { 2109 SSLerror(s, SSL_R_SIGNATURE_ALGORITHMS_ERROR); 2110 return (NULL); 2111 } 2112 *pmd = sigalg->md(); 2113 *sap = sigalg; 2114 2115 return (pkey); 2116} 2117 2118DH * 2119ssl_get_auto_dh(SSL *s) 2120{ 2121 CERT_PKEY *cpk; 2122 int keylen; 2123 DH *dhp; 2124 2125 if (s->cert->dh_tmp_auto == 2) { 2126 keylen = 1024; 2127 } else if (S3I(s)->hs.new_cipher->algorithm_auth & SSL_aNULL) { 2128 keylen = 1024; 2129 if (S3I(s)->hs.new_cipher->strength_bits == 256) 2130 keylen = 3072; 2131 } else { 2132 if ((cpk = ssl_get_server_send_pkey(s)) == NULL) 2133 return (NULL); 2134 if (cpk->privatekey == NULL || cpk->privatekey->pkey.dh == NULL) 2135 return (NULL); 2136 keylen = EVP_PKEY_bits(cpk->privatekey); 2137 } 2138 2139 if ((dhp = DH_new()) == NULL) 2140 return (NULL); 2141 2142 dhp->g = BN_new(); 2143 if (dhp->g != NULL) 2144 BN_set_word(dhp->g, 2); 2145 2146 if (keylen >= 8192) 2147 dhp->p = get_rfc3526_prime_8192(NULL); 2148 else if (keylen >= 4096) 2149 dhp->p = get_rfc3526_prime_4096(NULL); 2150 else if (keylen >= 3072) 2151 dhp->p = get_rfc3526_prime_3072(NULL); 2152 else if (keylen >= 2048) 2153 dhp->p = get_rfc3526_prime_2048(NULL); 2154 else if (keylen >= 1536) 2155 dhp->p = get_rfc3526_prime_1536(NULL); 2156 else 2157 dhp->p = get_rfc2409_prime_1024(NULL); 2158 2159 if (dhp->p == NULL || dhp->g == NULL) { 2160 DH_free(dhp); 2161 return (NULL); 2162 } 2163 return (dhp); 2164} 2165 2166void 2167ssl_update_cache(SSL *s, int mode) 2168{ 2169 int i; 2170 2171 /* 2172 * If the session_id_length is 0, we are not supposed to cache it, 2173 * and it would be rather hard to do anyway :-) 2174 */ 2175 if (s->session->session_id_length == 0) 2176 return; 2177 2178 i = s->session_ctx->internal->session_cache_mode; 2179 if ((i & mode) && (!s->internal->hit) && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) 2180 || SSL_CTX_add_session(s->session_ctx, s->session)) 2181 && (s->session_ctx->internal->new_session_cb != NULL)) { 2182 CRYPTO_add(&s->session->references, 1, CRYPTO_LOCK_SSL_SESSION); 2183 if (!s->session_ctx->internal->new_session_cb(s, s->session)) 2184 SSL_SESSION_free(s->session); 2185 } 2186 2187 /* auto flush every 255 connections */ 2188 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && 2189 ((i & mode) == mode)) { 2190 if ((((mode & SSL_SESS_CACHE_CLIENT) ? 2191 s->session_ctx->internal->stats.sess_connect_good : 2192 s->session_ctx->internal->stats.sess_accept_good) & 0xff) == 0xff) { 2193 SSL_CTX_flush_sessions(s->session_ctx, time(NULL)); 2194 } 2195 } 2196} 2197 2198const SSL_METHOD * 2199SSL_get_ssl_method(SSL *s) 2200{ 2201 return (s->method); 2202} 2203 2204int 2205SSL_set_ssl_method(SSL *s, const SSL_METHOD *method) 2206{ 2207 int (*handshake_func)(SSL *) = NULL; 2208 int ret = 1; 2209 2210 if (s->method == method) 2211 return (ret); 2212 2213 if (s->internal->handshake_func == s->method->internal->ssl_connect) 2214 handshake_func = method->internal->ssl_connect; 2215 else if (s->internal->handshake_func == s->method->internal->ssl_accept) 2216 handshake_func = method->internal->ssl_accept; 2217 2218 if (s->method->internal->version == method->internal->version) { 2219 s->method = method; 2220 } else { 2221 s->method->internal->ssl_free(s); 2222 s->method = method; 2223 ret = s->method->internal->ssl_new(s); 2224 } 2225 s->internal->handshake_func = handshake_func; 2226 2227 return (ret); 2228} 2229 2230int 2231SSL_get_error(const SSL *s, int i) 2232{ 2233 int reason; 2234 unsigned long l; 2235 BIO *bio; 2236 2237 if (i > 0) 2238 return (SSL_ERROR_NONE); 2239 2240 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake 2241 * etc, where we do encode the error */ 2242 if ((l = ERR_peek_error()) != 0) { 2243 if (ERR_GET_LIB(l) == ERR_LIB_SYS) 2244 return (SSL_ERROR_SYSCALL); 2245 else 2246 return (SSL_ERROR_SSL); 2247 } 2248 2249 if ((i < 0) && SSL_want_read(s)) { 2250 bio = SSL_get_rbio(s); 2251 if (BIO_should_read(bio)) { 2252 return (SSL_ERROR_WANT_READ); 2253 } else if (BIO_should_write(bio)) { 2254 /* 2255 * This one doesn't make too much sense... We never 2256 * try to write to the rbio, and an application 2257 * program where rbio and wbio are separate couldn't 2258 * even know what it should wait for. However if we 2259 * ever set s->internal->rwstate incorrectly (so that we have 2260 * SSL_want_read(s) instead of SSL_want_write(s)) 2261 * and rbio and wbio *are* the same, this test works 2262 * around that bug; so it might be safer to keep it. 2263 */ 2264 return (SSL_ERROR_WANT_WRITE); 2265 } else if (BIO_should_io_special(bio)) { 2266 reason = BIO_get_retry_reason(bio); 2267 if (reason == BIO_RR_CONNECT) 2268 return (SSL_ERROR_WANT_CONNECT); 2269 else if (reason == BIO_RR_ACCEPT) 2270 return (SSL_ERROR_WANT_ACCEPT); 2271 else 2272 return (SSL_ERROR_SYSCALL); /* unknown */ 2273 } 2274 } 2275 2276 if ((i < 0) && SSL_want_write(s)) { 2277 bio = SSL_get_wbio(s); 2278 if (BIO_should_write(bio)) { 2279 return (SSL_ERROR_WANT_WRITE); 2280 } else if (BIO_should_read(bio)) { 2281 /* 2282 * See above (SSL_want_read(s) with 2283 * BIO_should_write(bio)) 2284 */ 2285 return (SSL_ERROR_WANT_READ); 2286 } else if (BIO_should_io_special(bio)) { 2287 reason = BIO_get_retry_reason(bio); 2288 if (reason == BIO_RR_CONNECT) 2289 return (SSL_ERROR_WANT_CONNECT); 2290 else if (reason == BIO_RR_ACCEPT) 2291 return (SSL_ERROR_WANT_ACCEPT); 2292 else 2293 return (SSL_ERROR_SYSCALL); 2294 } 2295 } 2296 if ((i < 0) && SSL_want_x509_lookup(s)) { 2297 return (SSL_ERROR_WANT_X509_LOOKUP); 2298 } 2299 2300 if (i == 0) { 2301 if ((s->internal->shutdown & SSL_RECEIVED_SHUTDOWN) && 2302 (S3I(s)->warn_alert == SSL_AD_CLOSE_NOTIFY)) 2303 return (SSL_ERROR_ZERO_RETURN); 2304 } 2305 return (SSL_ERROR_SYSCALL); 2306} 2307 2308int 2309SSL_do_handshake(SSL *s) 2310{ 2311 int ret = 1; 2312 2313 if (s->internal->handshake_func == NULL) { 2314 SSLerror(s, SSL_R_CONNECTION_TYPE_NOT_SET); 2315 return (-1); 2316 } 2317 2318 s->method->internal->ssl_renegotiate_check(s); 2319 2320 if (SSL_in_init(s) || SSL_in_before(s)) { 2321 ret = s->internal->handshake_func(s); 2322 } 2323 return (ret); 2324} 2325 2326/* 2327 * For the next 2 functions, SSL_clear() sets shutdown and so 2328 * one of these calls will reset it 2329 */ 2330void 2331SSL_set_accept_state(SSL *s) 2332{ 2333 s->server = 1; 2334 s->internal->shutdown = 0; 2335 S3I(s)->hs.state = SSL_ST_ACCEPT|SSL_ST_BEFORE; 2336 s->internal->handshake_func = s->method->internal->ssl_accept; 2337 ssl_clear_cipher_state(s); 2338} 2339 2340void 2341SSL_set_connect_state(SSL *s) 2342{ 2343 s->server = 0; 2344 s->internal->shutdown = 0; 2345 S3I(s)->hs.state = SSL_ST_CONNECT|SSL_ST_BEFORE; 2346 s->internal->handshake_func = s->method->internal->ssl_connect; 2347 ssl_clear_cipher_state(s); 2348} 2349 2350int 2351ssl_undefined_function(SSL *s) 2352{ 2353 SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2354 return (0); 2355} 2356 2357int 2358ssl_undefined_void_function(void) 2359{ 2360 SSLerrorx(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2361 return (0); 2362} 2363 2364int 2365ssl_undefined_const_function(const SSL *s) 2366{ 2367 SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2368 return (0); 2369} 2370 2371const char * 2372ssl_version_string(int ver) 2373{ 2374 switch (ver) { 2375 case DTLS1_VERSION: 2376 return (SSL_TXT_DTLS1); 2377 case TLS1_VERSION: 2378 return (SSL_TXT_TLSV1); 2379 case TLS1_1_VERSION: 2380 return (SSL_TXT_TLSV1_1); 2381 case TLS1_2_VERSION: 2382 return (SSL_TXT_TLSV1_2); 2383 case TLS1_3_VERSION: 2384 return (SSL_TXT_TLSV1_3); 2385 default: 2386 return ("unknown"); 2387 } 2388} 2389 2390const char * 2391SSL_get_version(const SSL *s) 2392{ 2393 return ssl_version_string(s->version); 2394} 2395 2396SSL * 2397SSL_dup(SSL *s) 2398{ 2399 STACK_OF(X509_NAME) *sk; 2400 X509_NAME *xn; 2401 SSL *ret; 2402 int i; 2403 2404 if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL) 2405 goto err; 2406 2407 ret->version = s->version; 2408 ret->internal->type = s->internal->type; 2409 ret->method = s->method; 2410 2411 if (s->session != NULL) { 2412 if (!SSL_copy_session_id(ret, s)) 2413 goto err; 2414 } else { 2415 /* 2416 * No session has been established yet, so we have to expect 2417 * that s->cert or ret->cert will be changed later -- 2418 * they should not both point to the same object, 2419 * and thus we can't use SSL_copy_session_id. 2420 */ 2421 2422 ret->method->internal->ssl_free(ret); 2423 ret->method = s->method; 2424 ret->method->internal->ssl_new(ret); 2425 2426 ssl_cert_free(ret->cert); 2427 if ((ret->cert = ssl_cert_dup(s->cert)) == NULL) 2428 goto err; 2429 2430 if (!SSL_set_session_id_context(ret, s->sid_ctx, 2431 s->sid_ctx_length)) 2432 goto err; 2433 } 2434 2435 ret->internal->options = s->internal->options; 2436 ret->internal->mode = s->internal->mode; 2437 SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s)); 2438 SSL_set_read_ahead(ret, SSL_get_read_ahead(s)); 2439 ret->internal->msg_callback = s->internal->msg_callback; 2440 ret->internal->msg_callback_arg = s->internal->msg_callback_arg; 2441 SSL_set_verify(ret, SSL_get_verify_mode(s), 2442 SSL_get_verify_callback(s)); 2443 SSL_set_verify_depth(ret, SSL_get_verify_depth(s)); 2444 ret->internal->generate_session_id = s->internal->generate_session_id; 2445 2446 SSL_set_info_callback(ret, SSL_get_info_callback(s)); 2447 2448 ret->internal->debug = s->internal->debug; 2449 2450 /* copy app data, a little dangerous perhaps */ 2451 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, 2452 &ret->internal->ex_data, &s->internal->ex_data)) 2453 goto err; 2454 2455 /* setup rbio, and wbio */ 2456 if (s->rbio != NULL) { 2457 if (!BIO_dup_state(s->rbio,(char *)&ret->rbio)) 2458 goto err; 2459 } 2460 if (s->wbio != NULL) { 2461 if (s->wbio != s->rbio) { 2462 if (!BIO_dup_state(s->wbio,(char *)&ret->wbio)) 2463 goto err; 2464 } else 2465 ret->wbio = ret->rbio; 2466 } 2467 ret->internal->rwstate = s->internal->rwstate; 2468 ret->internal->in_handshake = s->internal->in_handshake; 2469 ret->internal->handshake_func = s->internal->handshake_func; 2470 ret->server = s->server; 2471 ret->internal->renegotiate = s->internal->renegotiate; 2472 ret->internal->new_session = s->internal->new_session; 2473 ret->internal->quiet_shutdown = s->internal->quiet_shutdown; 2474 ret->internal->shutdown = s->internal->shutdown; 2475 /* SSL_dup does not really work at any state, though */ 2476 S3I(ret)->hs.state = S3I(s)->hs.state; 2477 ret->internal->rstate = s->internal->rstate; 2478 2479 /* 2480 * Would have to copy ret->init_buf, ret->init_msg, ret->init_num, 2481 * ret->init_off 2482 */ 2483 ret->internal->init_num = 0; 2484 2485 ret->internal->hit = s->internal->hit; 2486 2487 X509_VERIFY_PARAM_inherit(ret->param, s->param); 2488 2489 if (s->cipher_list != NULL) { 2490 if ((ret->cipher_list = 2491 sk_SSL_CIPHER_dup(s->cipher_list)) == NULL) 2492 goto err; 2493 } 2494 if (s->internal->cipher_list_tls13 != NULL) { 2495 if ((ret->internal->cipher_list_tls13 = 2496 sk_SSL_CIPHER_dup(s->internal->cipher_list_tls13)) == NULL) 2497 goto err; 2498 } 2499 2500 /* Dup the client_CA list */ 2501 if (s->internal->client_CA != NULL) { 2502 if ((sk = sk_X509_NAME_dup(s->internal->client_CA)) == NULL) goto err; 2503 ret->internal->client_CA = sk; 2504 for (i = 0; i < sk_X509_NAME_num(sk); i++) { 2505 xn = sk_X509_NAME_value(sk, i); 2506 if (sk_X509_NAME_set(sk, i, 2507 X509_NAME_dup(xn)) == NULL) { 2508 X509_NAME_free(xn); 2509 goto err; 2510 } 2511 } 2512 } 2513 2514 return ret; 2515 err: 2516 SSL_free(ret); 2517 return NULL; 2518} 2519 2520void 2521ssl_clear_cipher_state(SSL *s) 2522{ 2523 ssl_clear_cipher_read_state(s); 2524 ssl_clear_cipher_write_state(s); 2525} 2526 2527void 2528ssl_clear_cipher_read_state(SSL *s) 2529{ 2530 EVP_CIPHER_CTX_free(s->enc_read_ctx); 2531 s->enc_read_ctx = NULL; 2532 EVP_MD_CTX_free(s->read_hash); 2533 s->read_hash = NULL; 2534 2535 tls12_record_layer_clear_read_state(s->internal->rl); 2536 tls12_record_layer_set_read_seq_num(s->internal->rl, 2537 S3I(s)->read_sequence); 2538 2539 if (s->internal->aead_read_ctx != NULL) { 2540 EVP_AEAD_CTX_cleanup(&s->internal->aead_read_ctx->ctx); 2541 free(s->internal->aead_read_ctx); 2542 s->internal->aead_read_ctx = NULL; 2543 } 2544} 2545 2546void 2547ssl_clear_cipher_write_state(SSL *s) 2548{ 2549 EVP_CIPHER_CTX_free(s->internal->enc_write_ctx); 2550 s->internal->enc_write_ctx = NULL; 2551 EVP_MD_CTX_free(s->internal->write_hash); 2552 s->internal->write_hash = NULL; 2553 2554 tls12_record_layer_clear_write_state(s->internal->rl); 2555 tls12_record_layer_set_write_seq_num(s->internal->rl, 2556 S3I(s)->write_sequence); 2557 2558 if (s->internal->aead_write_ctx != NULL) { 2559 EVP_AEAD_CTX_cleanup(&s->internal->aead_write_ctx->ctx); 2560 free(s->internal->aead_write_ctx); 2561 s->internal->aead_write_ctx = NULL; 2562 } 2563} 2564 2565/* Fix this function so that it takes an optional type parameter */ 2566X509 * 2567SSL_get_certificate(const SSL *s) 2568{ 2569 return (s->cert->key->x509); 2570} 2571 2572/* Fix this function so that it takes an optional type parameter */ 2573EVP_PKEY * 2574SSL_get_privatekey(const SSL *s) 2575{ 2576 return (s->cert->key->privatekey); 2577} 2578 2579const SSL_CIPHER * 2580SSL_get_current_cipher(const SSL *s) 2581{ 2582 if ((s->session != NULL) && (s->session->cipher != NULL)) 2583 return (s->session->cipher); 2584 return (NULL); 2585} 2586const void * 2587SSL_get_current_compression(SSL *s) 2588{ 2589 return (NULL); 2590} 2591 2592const void * 2593SSL_get_current_expansion(SSL *s) 2594{ 2595 return (NULL); 2596} 2597 2598size_t 2599SSL_get_client_random(const SSL *s, unsigned char *out, size_t max_out) 2600{ 2601 size_t len = sizeof(s->s3->client_random); 2602 2603 if (out == NULL) 2604 return len; 2605 2606 if (len > max_out) 2607 len = max_out; 2608 2609 memcpy(out, s->s3->client_random, len); 2610 2611 return len; 2612} 2613 2614size_t 2615SSL_get_server_random(const SSL *s, unsigned char *out, size_t max_out) 2616{ 2617 size_t len = sizeof(s->s3->server_random); 2618 2619 if (out == NULL) 2620 return len; 2621 2622 if (len > max_out) 2623 len = max_out; 2624 2625 memcpy(out, s->s3->server_random, len); 2626 2627 return len; 2628} 2629 2630int 2631ssl_init_wbio_buffer(SSL *s, int push) 2632{ 2633 BIO *bbio; 2634 2635 if (s->bbio == NULL) { 2636 bbio = BIO_new(BIO_f_buffer()); 2637 if (bbio == NULL) 2638 return (0); 2639 s->bbio = bbio; 2640 } else { 2641 bbio = s->bbio; 2642 if (s->bbio == s->wbio) 2643 s->wbio = BIO_pop(s->wbio); 2644 } 2645 (void)BIO_reset(bbio); 2646/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */ 2647 if (!BIO_set_read_buffer_size(bbio, 1)) { 2648 SSLerror(s, ERR_R_BUF_LIB); 2649 return (0); 2650 } 2651 if (push) { 2652 if (s->wbio != bbio) 2653 s->wbio = BIO_push(bbio, s->wbio); 2654 } else { 2655 if (s->wbio == bbio) 2656 s->wbio = BIO_pop(bbio); 2657 } 2658 return (1); 2659} 2660 2661void 2662ssl_free_wbio_buffer(SSL *s) 2663{ 2664 if (s == NULL) 2665 return; 2666 2667 if (s->bbio == NULL) 2668 return; 2669 2670 if (s->bbio == s->wbio) { 2671 /* remove buffering */ 2672 s->wbio = BIO_pop(s->wbio); 2673 } 2674 BIO_free(s->bbio); 2675 s->bbio = NULL; 2676} 2677 2678void 2679SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) 2680{ 2681 ctx->internal->quiet_shutdown = mode; 2682} 2683 2684int 2685SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) 2686{ 2687 return (ctx->internal->quiet_shutdown); 2688} 2689 2690void 2691SSL_set_quiet_shutdown(SSL *s, int mode) 2692{ 2693 s->internal->quiet_shutdown = mode; 2694} 2695 2696int 2697SSL_get_quiet_shutdown(const SSL *s) 2698{ 2699 return (s->internal->quiet_shutdown); 2700} 2701 2702void 2703SSL_set_shutdown(SSL *s, int mode) 2704{ 2705 s->internal->shutdown = mode; 2706} 2707 2708int 2709SSL_get_shutdown(const SSL *s) 2710{ 2711 return (s->internal->shutdown); 2712} 2713 2714int 2715SSL_version(const SSL *s) 2716{ 2717 return (s->version); 2718} 2719 2720SSL_CTX * 2721SSL_get_SSL_CTX(const SSL *ssl) 2722{ 2723 return (ssl->ctx); 2724} 2725 2726SSL_CTX * 2727SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx) 2728{ 2729 if (ssl->ctx == ctx) 2730 return (ssl->ctx); 2731 if (ctx == NULL) 2732 ctx = ssl->initial_ctx; 2733 2734 ssl_cert_free(ssl->cert); 2735 ssl->cert = ssl_cert_dup(ctx->internal->cert); 2736 2737 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX); 2738 SSL_CTX_free(ssl->ctx); /* decrement reference count */ 2739 ssl->ctx = ctx; 2740 return (ssl->ctx); 2741} 2742 2743int 2744SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) 2745{ 2746 return (X509_STORE_set_default_paths(ctx->cert_store)); 2747} 2748 2749int 2750SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile, 2751 const char *CApath) 2752{ 2753 return (X509_STORE_load_locations(ctx->cert_store, CAfile, CApath)); 2754} 2755 2756int 2757SSL_CTX_load_verify_mem(SSL_CTX *ctx, void *buf, int len) 2758{ 2759 return (X509_STORE_load_mem(ctx->cert_store, buf, len)); 2760} 2761 2762void 2763SSL_set_info_callback(SSL *ssl, void (*cb)(const SSL *ssl, int type, int val)) 2764{ 2765 ssl->internal->info_callback = cb; 2766} 2767 2768void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type, int val) 2769{ 2770 return (ssl->internal->info_callback); 2771} 2772 2773int 2774SSL_state(const SSL *ssl) 2775{ 2776 return (S3I(ssl)->hs.state); 2777} 2778 2779void 2780SSL_set_state(SSL *ssl, int state) 2781{ 2782 S3I(ssl)->hs.state = state; 2783} 2784 2785void 2786SSL_set_verify_result(SSL *ssl, long arg) 2787{ 2788 ssl->verify_result = arg; 2789} 2790 2791long 2792SSL_get_verify_result(const SSL *ssl) 2793{ 2794 return (ssl->verify_result); 2795} 2796 2797int 2798SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 2799 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 2800{ 2801 return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, 2802 new_func, dup_func, free_func)); 2803} 2804 2805int 2806SSL_set_ex_data(SSL *s, int idx, void *arg) 2807{ 2808 return (CRYPTO_set_ex_data(&s->internal->ex_data, idx, arg)); 2809} 2810 2811void * 2812SSL_get_ex_data(const SSL *s, int idx) 2813{ 2814 return (CRYPTO_get_ex_data(&s->internal->ex_data, idx)); 2815} 2816 2817int 2818SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 2819 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 2820{ 2821 return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, 2822 new_func, dup_func, free_func)); 2823} 2824 2825int 2826SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) 2827{ 2828 return (CRYPTO_set_ex_data(&s->internal->ex_data, idx, arg)); 2829} 2830 2831void * 2832SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) 2833{ 2834 return (CRYPTO_get_ex_data(&s->internal->ex_data, idx)); 2835} 2836 2837int 2838ssl_ok(SSL *s) 2839{ 2840 return (1); 2841} 2842 2843X509_STORE * 2844SSL_CTX_get_cert_store(const SSL_CTX *ctx) 2845{ 2846 return (ctx->cert_store); 2847} 2848 2849void 2850SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) 2851{ 2852 X509_STORE_free(ctx->cert_store); 2853 ctx->cert_store = store; 2854} 2855 2856X509 * 2857SSL_CTX_get0_certificate(const SSL_CTX *ctx) 2858{ 2859 if (ctx->internal->cert == NULL) 2860 return NULL; 2861 2862 return ctx->internal->cert->key->x509; 2863} 2864 2865int 2866SSL_want(const SSL *s) 2867{ 2868 return (s->internal->rwstate); 2869} 2870 2871void 2872SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, RSA *(*cb)(SSL *ssl, int is_export, 2873 int keylength)) 2874{ 2875 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); 2876} 2877 2878void 2879SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export, 2880 int keylength)) 2881{ 2882 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); 2883} 2884 2885void 2886SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, DH *(*dh)(SSL *ssl, int is_export, 2887 int keylength)) 2888{ 2889 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); 2890} 2891 2892void 2893SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export, 2894 int keylength)) 2895{ 2896 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); 2897} 2898 2899void 2900SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx, EC_KEY *(*ecdh)(SSL *ssl, 2901 int is_export, int keylength)) 2902{ 2903 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, 2904 (void (*)(void))ecdh); 2905} 2906 2907void 2908SSL_set_tmp_ecdh_callback(SSL *ssl, EC_KEY *(*ecdh)(SSL *ssl, int is_export, 2909 int keylength)) 2910{ 2911 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh); 2912} 2913 2914 2915void 2916SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version, 2917 int content_type, const void *buf, size_t len, SSL *ssl, void *arg)) 2918{ 2919 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, 2920 (void (*)(void))cb); 2921} 2922 2923void 2924SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version, 2925 int content_type, const void *buf, size_t len, SSL *ssl, void *arg)) 2926{ 2927 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); 2928} 2929 2930void 2931SSL_set_debug(SSL *s, int debug) 2932{ 2933 s->internal->debug = debug; 2934} 2935 2936int 2937SSL_cache_hit(SSL *s) 2938{ 2939 return (s->internal->hit); 2940} 2941 2942int 2943SSL_CTX_get_min_proto_version(SSL_CTX *ctx) 2944{ 2945 return ctx->internal->min_version; 2946} 2947 2948int 2949SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version) 2950{ 2951 return ssl_version_set_min(ctx->method, version, 2952 ctx->internal->max_version, &ctx->internal->min_version); 2953} 2954 2955int 2956SSL_CTX_get_max_proto_version(SSL_CTX *ctx) 2957{ 2958 return ctx->internal->max_version; 2959} 2960 2961int 2962SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version) 2963{ 2964 return ssl_version_set_max(ctx->method, version, 2965 ctx->internal->min_version, &ctx->internal->max_version); 2966} 2967 2968int 2969SSL_get_min_proto_version(SSL *ssl) 2970{ 2971 return ssl->internal->min_version; 2972} 2973 2974int 2975SSL_set_min_proto_version(SSL *ssl, uint16_t version) 2976{ 2977 return ssl_version_set_min(ssl->method, version, 2978 ssl->internal->max_version, &ssl->internal->min_version); 2979} 2980int 2981SSL_get_max_proto_version(SSL *ssl) 2982{ 2983 return ssl->internal->max_version; 2984} 2985 2986int 2987SSL_set_max_proto_version(SSL *ssl, uint16_t version) 2988{ 2989 return ssl_version_set_max(ssl->method, version, 2990 ssl->internal->min_version, &ssl->internal->max_version); 2991} 2992 2993static int 2994ssl_cipher_id_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_) 2995{ 2996 SSL_CIPHER const *a = a_; 2997 SSL_CIPHER const *b = b_; 2998 return ssl_cipher_id_cmp(a, b); 2999} 3000 3001SSL_CIPHER * 3002OBJ_bsearch_ssl_cipher_id(SSL_CIPHER *key, SSL_CIPHER const *base, int num) 3003{ 3004 return (SSL_CIPHER *)OBJ_bsearch_(key, base, num, sizeof(SSL_CIPHER), 3005 ssl_cipher_id_cmp_BSEARCH_CMP_FN); 3006} 3007