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