ssl_lib.c revision 276861
155714Skris/*! \file ssl/ssl_lib.c 255714Skris * \brief Version independent SSL functions. 355714Skris */ 455714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 555714Skris * All rights reserved. 655714Skris * 755714Skris * This package is an SSL implementation written 855714Skris * by Eric Young (eay@cryptsoft.com). 955714Skris * The implementation was written so as to conform with Netscapes SSL. 1055714Skris * 1155714Skris * This library is free for commercial and non-commercial use as long as 1255714Skris * the following conditions are aheared to. The following conditions 1355714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1455714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1555714Skris * included with this distribution is covered by the same copyright terms 1655714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1755714Skris * 1855714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1955714Skris * the code are not to be removed. 2055714Skris * If this package is used in a product, Eric Young should be given attribution 2155714Skris * as the author of the parts of the library used. 2255714Skris * This can be in the form of a textual message at program startup or 2355714Skris * in documentation (online or textual) provided with the package. 2455714Skris * 2555714Skris * Redistribution and use in source and binary forms, with or without 2655714Skris * modification, are permitted provided that the following conditions 2755714Skris * are met: 2855714Skris * 1. Redistributions of source code must retain the copyright 2955714Skris * notice, this list of conditions and the following disclaimer. 3055714Skris * 2. Redistributions in binary form must reproduce the above copyright 3155714Skris * notice, this list of conditions and the following disclaimer in the 3255714Skris * documentation and/or other materials provided with the distribution. 3355714Skris * 3. All advertising materials mentioning features or use of this software 3455714Skris * must display the following acknowledgement: 3555714Skris * "This product includes cryptographic software written by 3655714Skris * Eric Young (eay@cryptsoft.com)" 3755714Skris * The word 'cryptographic' can be left out if the rouines from the library 3855714Skris * being used are not cryptographic related :-). 3955714Skris * 4. If you include any Windows specific code (or a derivative thereof) from 4055714Skris * the apps directory (application code) you must include an acknowledgement: 4155714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4255714Skris * 4355714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4455714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4555714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4655714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4755714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4855714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4955714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5055714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5155714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5255714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5355714Skris * SUCH DAMAGE. 5455714Skris * 5555714Skris * The licence and distribution terms for any publically available version or 5655714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5755714Skris * copied and put under another distribution licence 5855714Skris * [including the GNU Public Licence.] 5955714Skris */ 60109998Smarkm/* ==================================================================== 61238405Sjkim * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. 62109998Smarkm * 63109998Smarkm * Redistribution and use in source and binary forms, with or without 64109998Smarkm * modification, are permitted provided that the following conditions 65109998Smarkm * are met: 66109998Smarkm * 67109998Smarkm * 1. Redistributions of source code must retain the above copyright 68109998Smarkm * notice, this list of conditions and the following disclaimer. 69109998Smarkm * 70109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright 71109998Smarkm * notice, this list of conditions and the following disclaimer in 72109998Smarkm * the documentation and/or other materials provided with the 73109998Smarkm * distribution. 74109998Smarkm * 75109998Smarkm * 3. All advertising materials mentioning features or use of this 76109998Smarkm * software must display the following acknowledgment: 77109998Smarkm * "This product includes software developed by the OpenSSL Project 78109998Smarkm * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 79109998Smarkm * 80109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 81109998Smarkm * endorse or promote products derived from this software without 82109998Smarkm * prior written permission. For written permission, please contact 83109998Smarkm * openssl-core@openssl.org. 84109998Smarkm * 85109998Smarkm * 5. Products derived from this software may not be called "OpenSSL" 86109998Smarkm * nor may "OpenSSL" appear in their names without prior written 87109998Smarkm * permission of the OpenSSL Project. 88109998Smarkm * 89109998Smarkm * 6. Redistributions of any form whatsoever must retain the following 90109998Smarkm * acknowledgment: 91109998Smarkm * "This product includes software developed by the OpenSSL Project 92109998Smarkm * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 93109998Smarkm * 94109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 95109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 96109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 97109998Smarkm * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 98109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 99109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 100109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 101109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 102109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 103109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 104109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 105109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE. 106109998Smarkm * ==================================================================== 107109998Smarkm * 108109998Smarkm * This product includes cryptographic software written by Eric Young 109109998Smarkm * (eay@cryptsoft.com). This product includes software written by Tim 110109998Smarkm * Hudson (tjh@cryptsoft.com). 111109998Smarkm * 112109998Smarkm */ 113160814Ssimon/* ==================================================================== 114160814Ssimon * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 115160814Ssimon * ECC cipher suite support in OpenSSL originally developed by 116160814Ssimon * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. 117160814Ssimon */ 118238405Sjkim/* ==================================================================== 119238405Sjkim * Copyright 2005 Nokia. All rights reserved. 120238405Sjkim * 121238405Sjkim * The portions of the attached software ("Contribution") is developed by 122238405Sjkim * Nokia Corporation and is licensed pursuant to the OpenSSL open source 123238405Sjkim * license. 124238405Sjkim * 125238405Sjkim * The Contribution, originally written by Mika Kousa and Pasi Eronen of 126238405Sjkim * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites 127238405Sjkim * support (see RFC 4279) to OpenSSL. 128238405Sjkim * 129238405Sjkim * No patent licenses or other rights except those expressly stated in 130238405Sjkim * the OpenSSL open source license shall be deemed granted or received 131238405Sjkim * expressly, by implication, estoppel, or otherwise. 132238405Sjkim * 133238405Sjkim * No assurances are provided by Nokia that the Contribution does not 134238405Sjkim * infringe the patent or other intellectual property rights of any third 135238405Sjkim * party or that the license provides you with all the necessary rights 136238405Sjkim * to make use of the Contribution. 137238405Sjkim * 138238405Sjkim * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN 139238405Sjkim * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA 140238405Sjkim * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY 141238405Sjkim * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR 142238405Sjkim * OTHERWISE. 143238405Sjkim */ 14455714Skris 145109998Smarkm#ifdef REF_CHECK 146109998Smarkm# include <assert.h> 147109998Smarkm#endif 14855714Skris#include <stdio.h> 149109998Smarkm#include "ssl_locl.h" 150109998Smarkm#include "kssl_lcl.h" 15155714Skris#include <openssl/objects.h> 15255714Skris#include <openssl/lhash.h> 15359191Skris#include <openssl/x509v3.h> 154194206Ssimon#include <openssl/rand.h> 155194206Ssimon#include <openssl/ocsp.h> 156160814Ssimon#ifndef OPENSSL_NO_DH 157160814Ssimon#include <openssl/dh.h> 158160814Ssimon#endif 159194206Ssimon#ifndef OPENSSL_NO_ENGINE 160194206Ssimon#include <openssl/engine.h> 161194206Ssimon#endif 16255714Skris 16359191Skrisconst char *SSL_version_str=OPENSSL_VERSION_TEXT; 16455714Skris 165160814SsimonSSL3_ENC_METHOD ssl3_undef_enc_method={ 16659191Skris /* evil casts, but these functions are only called if there's a library bug */ 16759191Skris (int (*)(SSL *,int))ssl_undefined_function, 16859191Skris (int (*)(SSL *, unsigned char *, int))ssl_undefined_function, 16955714Skris ssl_undefined_function, 17059191Skris (int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function, 17159191Skris (int (*)(SSL*, int))ssl_undefined_function, 172238405Sjkim (int (*)(SSL *, const char*, int, unsigned char *))ssl_undefined_function, 173160814Ssimon 0, /* finish_mac_length */ 174238405Sjkim (int (*)(SSL *, int, unsigned char *))ssl_undefined_function, 175160814Ssimon NULL, /* client_finished_label */ 176160814Ssimon 0, /* client_finished_label_len */ 177160814Ssimon NULL, /* server_finished_label */ 178160814Ssimon 0, /* server_finished_label_len */ 179238405Sjkim (int (*)(int))ssl_undefined_function, 180238405Sjkim (int (*)(SSL *, unsigned char *, size_t, const char *, 181238405Sjkim size_t, const unsigned char *, size_t, 182238405Sjkim int use_context)) ssl_undefined_function, 18355714Skris }; 18455714Skris 18555714Skrisint SSL_clear(SSL *s) 18655714Skris { 18755714Skris 18855714Skris if (s->method == NULL) 18955714Skris { 19055714Skris SSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED); 19155714Skris return(0); 19255714Skris } 19355714Skris 194100928Snectar if (ssl_clear_bad_session(s)) 195100928Snectar { 196100928Snectar SSL_SESSION_free(s->session); 197100928Snectar s->session=NULL; 198100928Snectar } 199100928Snectar 20055714Skris s->error=0; 20155714Skris s->hit=0; 20255714Skris s->shutdown=0; 20355714Skris 20459191Skris#if 0 /* Disabled since version 1.10 of this file (early return not 20559191Skris * needed because SSL_clear is not called when doing renegotiation) */ 20655714Skris /* This is set if we are doing dynamic renegotiation so keep 20755714Skris * the old cipher. It is sort of a SSL_clear_lite :-) */ 208238405Sjkim if (s->renegotiate) return(1); 20959191Skris#else 210238405Sjkim if (s->renegotiate) 21159191Skris { 212109998Smarkm SSLerr(SSL_F_SSL_CLEAR,ERR_R_INTERNAL_ERROR); 21359191Skris return 0; 21459191Skris } 21555714Skris#endif 21655714Skris 21755714Skris s->type=0; 21855714Skris 21955714Skris s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT); 22055714Skris 22155714Skris s->version=s->method->version; 22255714Skris s->client_version=s->version; 22355714Skris s->rwstate=SSL_NOTHING; 22455714Skris s->rstate=SSL_ST_READ_HEADER; 22572613Skris#if 0 22655714Skris s->read_ahead=s->ctx->read_ahead; 22772613Skris#endif 22855714Skris 22955714Skris if (s->init_buf != NULL) 23055714Skris { 23155714Skris BUF_MEM_free(s->init_buf); 23255714Skris s->init_buf=NULL; 23355714Skris } 23455714Skris 23555714Skris ssl_clear_cipher_ctx(s); 236238405Sjkim ssl_clear_hash_ctx(&s->read_hash); 237238405Sjkim ssl_clear_hash_ctx(&s->write_hash); 23855714Skris 23955714Skris s->first_packet=0; 24055714Skris 24155714Skris#if 1 24255714Skris /* Check to see if we were changed into a different method, if 24355714Skris * so, revert back if we are not doing session-id reuse. */ 24489837Skris if (!s->in_handshake && (s->session == NULL) && (s->method != s->ctx->method)) 24555714Skris { 24655714Skris s->method->ssl_free(s); 24755714Skris s->method=s->ctx->method; 24855714Skris if (!s->method->ssl_new(s)) 24955714Skris return(0); 25055714Skris } 25155714Skris else 25255714Skris#endif 25355714Skris s->method->ssl_clear(s); 25455714Skris return(1); 25555714Skris } 25655714Skris 25755714Skris/** Used to change an SSL_CTXs default SSL method type */ 258238405Sjkimint SSL_CTX_set_ssl_version(SSL_CTX *ctx,const SSL_METHOD *meth) 25955714Skris { 26055714Skris STACK_OF(SSL_CIPHER) *sk; 26155714Skris 26255714Skris ctx->method=meth; 26355714Skris 26455714Skris sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list), 265238405Sjkim &(ctx->cipher_list_by_id), 266238405Sjkim meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST); 26755714Skris if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) 26855714Skris { 26955714Skris SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS); 27055714Skris return(0); 27155714Skris } 27255714Skris return(1); 27355714Skris } 27455714Skris 27555714SkrisSSL *SSL_new(SSL_CTX *ctx) 27655714Skris { 27755714Skris SSL *s; 27855714Skris 27955714Skris if (ctx == NULL) 28055714Skris { 28155714Skris SSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX); 28255714Skris return(NULL); 28355714Skris } 28455714Skris if (ctx->method == NULL) 28555714Skris { 28655714Skris SSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION); 28755714Skris return(NULL); 28855714Skris } 28955714Skris 29068651Skris s=(SSL *)OPENSSL_malloc(sizeof(SSL)); 29155714Skris if (s == NULL) goto err; 29255714Skris memset(s,0,sizeof(SSL)); 29355714Skris 294109998Smarkm#ifndef OPENSSL_NO_KRB5 295109998Smarkm s->kssl_ctx = kssl_ctx_new(); 296109998Smarkm#endif /* OPENSSL_NO_KRB5 */ 297109998Smarkm 298109998Smarkm s->options=ctx->options; 299109998Smarkm s->mode=ctx->mode; 300109998Smarkm s->max_cert_list=ctx->max_cert_list; 301109998Smarkm 30255714Skris if (ctx->cert != NULL) 30355714Skris { 30455714Skris /* Earlier library versions used to copy the pointer to 30555714Skris * the CERT, not its contents; only when setting new 30655714Skris * parameters for the per-SSL copy, ssl_cert_new would be 30755714Skris * called (and the direct reference to the per-SSL_CTX 30855714Skris * settings would be lost, but those still were indirectly 30955714Skris * accessed for various purposes, and for that reason they 31055714Skris * used to be known as s->ctx->default_cert). 31155714Skris * Now we don't look at the SSL_CTX's CERT after having 31255714Skris * duplicated it once. */ 31355714Skris 31455714Skris s->cert = ssl_cert_dup(ctx->cert); 31555714Skris if (s->cert == NULL) 31655714Skris goto err; 31755714Skris } 31855714Skris else 31955714Skris s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */ 320109998Smarkm 321109998Smarkm s->read_ahead=ctx->read_ahead; 322109998Smarkm s->msg_callback=ctx->msg_callback; 323109998Smarkm s->msg_callback_arg=ctx->msg_callback_arg; 324109998Smarkm s->verify_mode=ctx->verify_mode; 325160814Ssimon#if 0 326109998Smarkm s->verify_depth=ctx->verify_depth; 327160814Ssimon#endif 32855714Skris s->sid_ctx_length=ctx->sid_ctx_length; 329109998Smarkm OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx); 33055714Skris memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx)); 33155714Skris s->verify_callback=ctx->default_verify_callback; 332109998Smarkm s->generate_session_id=ctx->generate_session_id; 333160814Ssimon 334160814Ssimon s->param = X509_VERIFY_PARAM_new(); 335160814Ssimon if (!s->param) 336160814Ssimon goto err; 337160814Ssimon X509_VERIFY_PARAM_inherit(s->param, ctx->param); 338160814Ssimon#if 0 33959191Skris s->purpose = ctx->purpose; 34059191Skris s->trust = ctx->trust; 341160814Ssimon#endif 342109998Smarkm s->quiet_shutdown=ctx->quiet_shutdown; 343238405Sjkim s->max_send_fragment = ctx->max_send_fragment; 344109998Smarkm 34555714Skris CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX); 34655714Skris s->ctx=ctx; 347194206Ssimon#ifndef OPENSSL_NO_TLSEXT 348194206Ssimon s->tlsext_debug_cb = 0; 349194206Ssimon s->tlsext_debug_arg = NULL; 350194206Ssimon s->tlsext_ticket_expected = 0; 351194206Ssimon s->tlsext_status_type = -1; 352194206Ssimon s->tlsext_status_expected = 0; 353194206Ssimon s->tlsext_ocsp_ids = NULL; 354194206Ssimon s->tlsext_ocsp_exts = NULL; 355194206Ssimon s->tlsext_ocsp_resp = NULL; 356194206Ssimon s->tlsext_ocsp_resplen = -1; 357194206Ssimon CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX); 358194206Ssimon s->initial_ctx=ctx; 359238405Sjkim# ifndef OPENSSL_NO_NEXTPROTONEG 360238405Sjkim s->next_proto_negotiated = NULL; 361238405Sjkim# endif 362194206Ssimon#endif 363238405Sjkim 36455714Skris s->verify_result=X509_V_OK; 36555714Skris 36655714Skris s->method=ctx->method; 36755714Skris 36855714Skris if (!s->method->ssl_new(s)) 36955714Skris goto err; 37055714Skris 37155714Skris s->references=1; 37255714Skris s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1; 373109998Smarkm 37455714Skris SSL_clear(s); 37555714Skris 376109998Smarkm CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data); 37755714Skris 378238405Sjkim#ifndef OPENSSL_NO_PSK 379238405Sjkim s->psk_client_callback=ctx->psk_client_callback; 380238405Sjkim s->psk_server_callback=ctx->psk_server_callback; 381238405Sjkim#endif 382238405Sjkim 38355714Skris return(s); 38455714Skriserr: 38555714Skris if (s != NULL) 386276861Sjkim SSL_free(s); 38755714Skris SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE); 38855714Skris return(NULL); 38955714Skris } 39055714Skris 39155714Skrisint SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx, 39255714Skris unsigned int sid_ctx_len) 39355714Skris { 394109998Smarkm if(sid_ctx_len > sizeof ctx->sid_ctx) 39555714Skris { 39655714Skris SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); 39755714Skris return 0; 39855714Skris } 39955714Skris ctx->sid_ctx_length=sid_ctx_len; 40055714Skris memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len); 40155714Skris 40255714Skris return 1; 40355714Skris } 40455714Skris 40555714Skrisint SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx, 40655714Skris unsigned int sid_ctx_len) 40755714Skris { 40855714Skris if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) 40955714Skris { 41055714Skris SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); 41155714Skris return 0; 41255714Skris } 41355714Skris ssl->sid_ctx_length=sid_ctx_len; 41455714Skris memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len); 41555714Skris 41655714Skris return 1; 41755714Skris } 41855714Skris 419109998Smarkmint SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) 420109998Smarkm { 421109998Smarkm CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); 422109998Smarkm ctx->generate_session_id = cb; 423109998Smarkm CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); 424109998Smarkm return 1; 42559191Skris } 426109998Smarkm 427109998Smarkmint SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) 428109998Smarkm { 429109998Smarkm CRYPTO_w_lock(CRYPTO_LOCK_SSL); 430109998Smarkm ssl->generate_session_id = cb; 431109998Smarkm CRYPTO_w_unlock(CRYPTO_LOCK_SSL); 43259191Skris return 1; 433109998Smarkm } 43459191Skris 435109998Smarkmint SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id, 436109998Smarkm unsigned int id_len) 437109998Smarkm { 438109998Smarkm /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how 439109998Smarkm * we can "construct" a session to give us the desired check - ie. to 440109998Smarkm * find if there's a session in the hash table that would conflict with 441109998Smarkm * any new session built out of this id/id_len and the ssl_version in 442109998Smarkm * use by this SSL. */ 443109998Smarkm SSL_SESSION r, *p; 444109998Smarkm 445109998Smarkm if(id_len > sizeof r.session_id) 44659191Skris return 0; 447109998Smarkm 448109998Smarkm r.ssl_version = ssl->version; 449109998Smarkm r.session_id_length = id_len; 450109998Smarkm memcpy(r.session_id, id, id_len); 451109998Smarkm /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a 452109998Smarkm * callback is calling us to check the uniqueness of a shorter ID, it 453109998Smarkm * must be compared as a padded-out ID because that is what it will be 454109998Smarkm * converted to when the callback has finished choosing it. */ 455109998Smarkm if((r.ssl_version == SSL2_VERSION) && 456109998Smarkm (id_len < SSL2_SSL_SESSION_ID_LENGTH)) 457109998Smarkm { 458109998Smarkm memset(r.session_id + id_len, 0, 459109998Smarkm SSL2_SSL_SESSION_ID_LENGTH - id_len); 460109998Smarkm r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH; 461109998Smarkm } 462109998Smarkm 463109998Smarkm CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); 464238405Sjkim p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r); 465109998Smarkm CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); 466109998Smarkm return (p != NULL); 46759191Skris } 468109998Smarkm 469109998Smarkmint SSL_CTX_set_purpose(SSL_CTX *s, int purpose) 470109998Smarkm { 471160814Ssimon return X509_VERIFY_PARAM_set_purpose(s->param, purpose); 472109998Smarkm } 473109998Smarkm 474109998Smarkmint SSL_set_purpose(SSL *s, int purpose) 475109998Smarkm { 476160814Ssimon return X509_VERIFY_PARAM_set_purpose(s->param, purpose); 477109998Smarkm } 478109998Smarkm 47959191Skrisint SSL_CTX_set_trust(SSL_CTX *s, int trust) 480109998Smarkm { 481160814Ssimon return X509_VERIFY_PARAM_set_trust(s->param, trust); 48259191Skris } 48359191Skris 48459191Skrisint SSL_set_trust(SSL *s, int trust) 485109998Smarkm { 486160814Ssimon return X509_VERIFY_PARAM_set_trust(s->param, trust); 48759191Skris } 48859191Skris 489238405Sjkimint SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) 490238405Sjkim { 491238405Sjkim return X509_VERIFY_PARAM_set1(ctx->param, vpm); 492238405Sjkim } 493238405Sjkim 494238405Sjkimint SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) 495238405Sjkim { 496238405Sjkim return X509_VERIFY_PARAM_set1(ssl->param, vpm); 497238405Sjkim } 498238405Sjkim 49955714Skrisvoid SSL_free(SSL *s) 50055714Skris { 50155714Skris int i; 50255714Skris 50355714Skris if(s == NULL) 50455714Skris return; 50555714Skris 50655714Skris i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL); 50755714Skris#ifdef REF_PRINT 50855714Skris REF_PRINT("SSL",s); 50955714Skris#endif 51055714Skris if (i > 0) return; 51155714Skris#ifdef REF_CHECK 51255714Skris if (i < 0) 51355714Skris { 51455714Skris fprintf(stderr,"SSL_free, bad reference count\n"); 51555714Skris abort(); /* ok */ 51655714Skris } 51755714Skris#endif 51855714Skris 519160814Ssimon if (s->param) 520160814Ssimon X509_VERIFY_PARAM_free(s->param); 521160814Ssimon 522109998Smarkm CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data); 52355714Skris 52455714Skris if (s->bbio != NULL) 52555714Skris { 52655714Skris /* If the buffering BIO is in place, pop it off */ 52755714Skris if (s->bbio == s->wbio) 52855714Skris { 52955714Skris s->wbio=BIO_pop(s->wbio); 53055714Skris } 53155714Skris BIO_free(s->bbio); 53255714Skris s->bbio=NULL; 53355714Skris } 53455714Skris if (s->rbio != NULL) 53555714Skris BIO_free_all(s->rbio); 53655714Skris if ((s->wbio != NULL) && (s->wbio != s->rbio)) 53755714Skris BIO_free_all(s->wbio); 53855714Skris 53955714Skris if (s->init_buf != NULL) BUF_MEM_free(s->init_buf); 54055714Skris 54155714Skris /* add extra stuff */ 54255714Skris if (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list); 54355714Skris if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id); 54455714Skris 54555714Skris /* Make the next call work :-) */ 54655714Skris if (s->session != NULL) 54755714Skris { 54855714Skris ssl_clear_bad_session(s); 54955714Skris SSL_SESSION_free(s->session); 55055714Skris } 55155714Skris 55255714Skris ssl_clear_cipher_ctx(s); 553238405Sjkim ssl_clear_hash_ctx(&s->read_hash); 554238405Sjkim ssl_clear_hash_ctx(&s->write_hash); 55555714Skris 55655714Skris if (s->cert != NULL) ssl_cert_free(s->cert); 55755714Skris /* Free up if allocated */ 55855714Skris 559194206Ssimon#ifndef OPENSSL_NO_TLSEXT 560194206Ssimon if (s->tlsext_hostname) 561194206Ssimon OPENSSL_free(s->tlsext_hostname); 562194206Ssimon if (s->initial_ctx) SSL_CTX_free(s->initial_ctx); 563238405Sjkim#ifndef OPENSSL_NO_EC 564238405Sjkim if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist); 565238405Sjkim if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist); 566238405Sjkim#endif /* OPENSSL_NO_EC */ 567238405Sjkim if (s->tlsext_opaque_prf_input) OPENSSL_free(s->tlsext_opaque_prf_input); 568194206Ssimon if (s->tlsext_ocsp_exts) 569194206Ssimon sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, 570194206Ssimon X509_EXTENSION_free); 571194206Ssimon if (s->tlsext_ocsp_ids) 572194206Ssimon sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free); 573194206Ssimon if (s->tlsext_ocsp_resp) 574194206Ssimon OPENSSL_free(s->tlsext_ocsp_resp); 575194206Ssimon#endif 576238405Sjkim 57755714Skris if (s->client_CA != NULL) 57855714Skris sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free); 57955714Skris 58055714Skris if (s->method != NULL) s->method->ssl_free(s); 58155714Skris 582205128Ssimon if (s->ctx) SSL_CTX_free(s->ctx); 583205128Ssimon 584120631Snectar#ifndef OPENSSL_NO_KRB5 585120631Snectar if (s->kssl_ctx != NULL) 586120631Snectar kssl_ctx_free(s->kssl_ctx); 587120631Snectar#endif /* OPENSSL_NO_KRB5 */ 588120631Snectar 589238405Sjkim#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) 590238405Sjkim if (s->next_proto_negotiated) 591238405Sjkim OPENSSL_free(s->next_proto_negotiated); 592238405Sjkim#endif 593238405Sjkim 594246772Sjkim#ifndef OPENSSL_NO_SRTP 595238405Sjkim if (s->srtp_profiles) 596238405Sjkim sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles); 597246772Sjkim#endif 598238405Sjkim 59968651Skris OPENSSL_free(s); 60055714Skris } 60155714Skris 60255714Skrisvoid SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio) 60355714Skris { 60455714Skris /* If the output buffering BIO is still in place, remove it 60555714Skris */ 60655714Skris if (s->bbio != NULL) 60755714Skris { 60855714Skris if (s->wbio == s->bbio) 60955714Skris { 61055714Skris s->wbio=s->wbio->next_bio; 61155714Skris s->bbio->next_bio=NULL; 61255714Skris } 61355714Skris } 61455714Skris if ((s->rbio != NULL) && (s->rbio != rbio)) 61555714Skris BIO_free_all(s->rbio); 61655714Skris if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio)) 61755714Skris BIO_free_all(s->wbio); 61855714Skris s->rbio=rbio; 61955714Skris s->wbio=wbio; 62055714Skris } 62155714Skris 622160814SsimonBIO *SSL_get_rbio(const SSL *s) 62355714Skris { return(s->rbio); } 62455714Skris 625160814SsimonBIO *SSL_get_wbio(const SSL *s) 62655714Skris { return(s->wbio); } 62755714Skris 628160814Ssimonint SSL_get_fd(const SSL *s) 62955714Skris { 63089837Skris return(SSL_get_rfd(s)); 63189837Skris } 63289837Skris 633160814Ssimonint SSL_get_rfd(const SSL *s) 63489837Skris { 63555714Skris int ret= -1; 63655714Skris BIO *b,*r; 63755714Skris 63855714Skris b=SSL_get_rbio(s); 63955714Skris r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR); 64055714Skris if (r != NULL) 64155714Skris BIO_get_fd(r,&ret); 64255714Skris return(ret); 64355714Skris } 64455714Skris 645160814Ssimonint SSL_get_wfd(const SSL *s) 64689837Skris { 64789837Skris int ret= -1; 64889837Skris BIO *b,*r; 64989837Skris 65089837Skris b=SSL_get_wbio(s); 65189837Skris r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR); 65289837Skris if (r != NULL) 65389837Skris BIO_get_fd(r,&ret); 65489837Skris return(ret); 65589837Skris } 65689837Skris 657109998Smarkm#ifndef OPENSSL_NO_SOCK 65855714Skrisint SSL_set_fd(SSL *s,int fd) 65955714Skris { 66055714Skris int ret=0; 66155714Skris BIO *bio=NULL; 66255714Skris 66355714Skris bio=BIO_new(BIO_s_socket()); 66455714Skris 66555714Skris if (bio == NULL) 66655714Skris { 66755714Skris SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB); 66855714Skris goto err; 66955714Skris } 67055714Skris BIO_set_fd(bio,fd,BIO_NOCLOSE); 67155714Skris SSL_set_bio(s,bio,bio); 67255714Skris ret=1; 67355714Skriserr: 67455714Skris return(ret); 67555714Skris } 67655714Skris 67755714Skrisint SSL_set_wfd(SSL *s,int fd) 67855714Skris { 67955714Skris int ret=0; 68055714Skris BIO *bio=NULL; 68155714Skris 68255714Skris if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET) 68355714Skris || ((int)BIO_get_fd(s->rbio,NULL) != fd)) 68455714Skris { 68555714Skris bio=BIO_new(BIO_s_socket()); 68655714Skris 68755714Skris if (bio == NULL) 68855714Skris { SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; } 68955714Skris BIO_set_fd(bio,fd,BIO_NOCLOSE); 69055714Skris SSL_set_bio(s,SSL_get_rbio(s),bio); 69155714Skris } 69255714Skris else 69355714Skris SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s)); 69455714Skris ret=1; 69555714Skriserr: 69655714Skris return(ret); 69755714Skris } 69855714Skris 69955714Skrisint SSL_set_rfd(SSL *s,int fd) 70055714Skris { 70155714Skris int ret=0; 70255714Skris BIO *bio=NULL; 70355714Skris 70455714Skris if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET) 70555714Skris || ((int)BIO_get_fd(s->wbio,NULL) != fd)) 70655714Skris { 70755714Skris bio=BIO_new(BIO_s_socket()); 70855714Skris 70955714Skris if (bio == NULL) 71055714Skris { 71155714Skris SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB); 71255714Skris goto err; 71355714Skris } 71455714Skris BIO_set_fd(bio,fd,BIO_NOCLOSE); 71555714Skris SSL_set_bio(s,bio,SSL_get_wbio(s)); 71655714Skris } 71755714Skris else 71855714Skris SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s)); 71955714Skris ret=1; 72055714Skriserr: 72155714Skris return(ret); 72255714Skris } 72355714Skris#endif 72455714Skris 72559191Skris 72659191Skris/* return length of latest Finished message we sent, copy to 'buf' */ 727160814Ssimonsize_t SSL_get_finished(const SSL *s, void *buf, size_t count) 72859191Skris { 72959191Skris size_t ret = 0; 73059191Skris 73159191Skris if (s->s3 != NULL) 73259191Skris { 73359191Skris ret = s->s3->tmp.finish_md_len; 73459191Skris if (count > ret) 73559191Skris count = ret; 73659191Skris memcpy(buf, s->s3->tmp.finish_md, count); 73759191Skris } 73859191Skris return ret; 73959191Skris } 74059191Skris 74159191Skris/* return length of latest Finished message we expected, copy to 'buf' */ 742160814Ssimonsize_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) 74359191Skris { 74459191Skris size_t ret = 0; 74559191Skris 74659191Skris if (s->s3 != NULL) 74759191Skris { 74859191Skris ret = s->s3->tmp.peer_finish_md_len; 74959191Skris if (count > ret) 75059191Skris count = ret; 75159191Skris memcpy(buf, s->s3->tmp.peer_finish_md, count); 75259191Skris } 75359191Skris return ret; 75459191Skris } 75559191Skris 75659191Skris 757160814Ssimonint SSL_get_verify_mode(const SSL *s) 75855714Skris { 75955714Skris return(s->verify_mode); 76055714Skris } 76155714Skris 762160814Ssimonint SSL_get_verify_depth(const SSL *s) 76355714Skris { 764160814Ssimon return X509_VERIFY_PARAM_get_depth(s->param); 76555714Skris } 76655714Skris 767160814Ssimonint (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *) 76855714Skris { 76955714Skris return(s->verify_callback); 77055714Skris } 77155714Skris 772160814Ssimonint SSL_CTX_get_verify_mode(const SSL_CTX *ctx) 77355714Skris { 77455714Skris return(ctx->verify_mode); 77555714Skris } 77655714Skris 777160814Ssimonint SSL_CTX_get_verify_depth(const SSL_CTX *ctx) 77855714Skris { 779160814Ssimon return X509_VERIFY_PARAM_get_depth(ctx->param); 78055714Skris } 78155714Skris 782160814Ssimonint (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *) 78355714Skris { 78455714Skris return(ctx->default_verify_callback); 78555714Skris } 78655714Skris 78755714Skrisvoid SSL_set_verify(SSL *s,int mode, 78855714Skris int (*callback)(int ok,X509_STORE_CTX *ctx)) 78955714Skris { 79055714Skris s->verify_mode=mode; 79155714Skris if (callback != NULL) 79255714Skris s->verify_callback=callback; 79355714Skris } 79455714Skris 79555714Skrisvoid SSL_set_verify_depth(SSL *s,int depth) 79655714Skris { 797160814Ssimon X509_VERIFY_PARAM_set_depth(s->param, depth); 79855714Skris } 79955714Skris 80055714Skrisvoid SSL_set_read_ahead(SSL *s,int yes) 80155714Skris { 80255714Skris s->read_ahead=yes; 80355714Skris } 80455714Skris 805160814Ssimonint SSL_get_read_ahead(const SSL *s) 80655714Skris { 80755714Skris return(s->read_ahead); 80855714Skris } 80955714Skris 810160814Ssimonint SSL_pending(const SSL *s) 81155714Skris { 812109998Smarkm /* SSL_pending cannot work properly if read-ahead is enabled 813109998Smarkm * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), 814109998Smarkm * and it is impossible to fix since SSL_pending cannot report 815109998Smarkm * errors that may be observed while scanning the new data. 816109998Smarkm * (Note that SSL_pending() is often used as a boolean value, 817109998Smarkm * so we'd better not return -1.) 818109998Smarkm */ 81955714Skris return(s->method->ssl_pending(s)); 82055714Skris } 82155714Skris 822160814SsimonX509 *SSL_get_peer_certificate(const SSL *s) 82355714Skris { 82455714Skris X509 *r; 82555714Skris 82655714Skris if ((s == NULL) || (s->session == NULL)) 82755714Skris r=NULL; 82855714Skris else 82955714Skris r=s->session->peer; 83055714Skris 83155714Skris if (r == NULL) return(r); 83255714Skris 83355714Skris CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509); 83455714Skris 83555714Skris return(r); 83655714Skris } 83755714Skris 838160814SsimonSTACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s) 83955714Skris { 84055714Skris STACK_OF(X509) *r; 84155714Skris 84255714Skris if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL)) 84355714Skris r=NULL; 84455714Skris else 84555714Skris r=s->session->sess_cert->cert_chain; 84655714Skris 84759191Skris /* If we are a client, cert_chain includes the peer's own 84859191Skris * certificate; if we are a server, it does not. */ 84959191Skris 85055714Skris return(r); 85155714Skris } 85255714Skris 85355714Skris/* Now in theory, since the calling process own 't' it should be safe to 85455714Skris * modify. We need to be able to read f without being hassled */ 855160814Ssimonvoid SSL_copy_session_id(SSL *t,const SSL *f) 85655714Skris { 85755714Skris CERT *tmp; 85855714Skris 85955714Skris /* Do we need to to SSL locking? */ 86055714Skris SSL_set_session(t,SSL_get_session(f)); 86155714Skris 86255714Skris /* what if we are setup as SSLv2 but want to talk SSLv3 or 86355714Skris * vice-versa */ 86455714Skris if (t->method != f->method) 86555714Skris { 86655714Skris t->method->ssl_free(t); /* cleanup current */ 86755714Skris t->method=f->method; /* change method */ 86855714Skris t->method->ssl_new(t); /* setup new */ 86955714Skris } 87055714Skris 87155714Skris tmp=t->cert; 87255714Skris if (f->cert != NULL) 87355714Skris { 87455714Skris CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT); 87555714Skris t->cert=f->cert; 87655714Skris } 87755714Skris else 87855714Skris t->cert=NULL; 87955714Skris if (tmp != NULL) ssl_cert_free(tmp); 88055714Skris SSL_set_session_id_context(t,f->sid_ctx,f->sid_ctx_length); 88155714Skris } 88255714Skris 88355714Skris/* Fix this so it checks all the valid key/cert options */ 884160814Ssimonint SSL_CTX_check_private_key(const SSL_CTX *ctx) 88555714Skris { 88655714Skris if ( (ctx == NULL) || 88755714Skris (ctx->cert == NULL) || 88855714Skris (ctx->cert->key->x509 == NULL)) 88955714Skris { 89055714Skris SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED); 89155714Skris return(0); 89255714Skris } 89355714Skris if (ctx->cert->key->privatekey == NULL) 89455714Skris { 89555714Skris SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED); 89655714Skris return(0); 89755714Skris } 89855714Skris return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey)); 89955714Skris } 90055714Skris 90155714Skris/* Fix this function so that it takes an optional type parameter */ 902160814Ssimonint SSL_check_private_key(const SSL *ssl) 90355714Skris { 90455714Skris if (ssl == NULL) 90555714Skris { 90655714Skris SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER); 90755714Skris return(0); 90855714Skris } 90955714Skris if (ssl->cert == NULL) 91055714Skris { 911238405Sjkim SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED); 91255714Skris return 0; 91355714Skris } 91455714Skris if (ssl->cert->key->x509 == NULL) 91555714Skris { 91655714Skris SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED); 91755714Skris return(0); 91855714Skris } 91955714Skris if (ssl->cert->key->privatekey == NULL) 92055714Skris { 92155714Skris SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED); 92255714Skris return(0); 92355714Skris } 92455714Skris return(X509_check_private_key(ssl->cert->key->x509, 92555714Skris ssl->cert->key->privatekey)); 92655714Skris } 92755714Skris 92855714Skrisint SSL_accept(SSL *s) 92955714Skris { 93055714Skris if (s->handshake_func == 0) 93155714Skris /* Not properly initialized yet */ 93255714Skris SSL_set_accept_state(s); 93355714Skris 93455714Skris return(s->method->ssl_accept(s)); 93555714Skris } 93655714Skris 93755714Skrisint SSL_connect(SSL *s) 93855714Skris { 93955714Skris if (s->handshake_func == 0) 94055714Skris /* Not properly initialized yet */ 94155714Skris SSL_set_connect_state(s); 94255714Skris 94355714Skris return(s->method->ssl_connect(s)); 94455714Skris } 94555714Skris 946160814Ssimonlong SSL_get_default_timeout(const SSL *s) 94755714Skris { 94855714Skris return(s->method->get_timeout()); 94955714Skris } 95055714Skris 95176866Skrisint SSL_read(SSL *s,void *buf,int num) 95255714Skris { 95355714Skris if (s->handshake_func == 0) 95455714Skris { 95555714Skris SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED); 95655714Skris return -1; 95755714Skris } 95855714Skris 95955714Skris if (s->shutdown & SSL_RECEIVED_SHUTDOWN) 96055714Skris { 96155714Skris s->rwstate=SSL_NOTHING; 96255714Skris return(0); 96355714Skris } 96455714Skris return(s->method->ssl_read(s,buf,num)); 96555714Skris } 96655714Skris 96776866Skrisint SSL_peek(SSL *s,void *buf,int num) 96855714Skris { 96976866Skris if (s->handshake_func == 0) 97076866Skris { 971160814Ssimon SSLerr(SSL_F_SSL_PEEK, SSL_R_UNINITIALIZED); 97276866Skris return -1; 97376866Skris } 97476866Skris 97555714Skris if (s->shutdown & SSL_RECEIVED_SHUTDOWN) 97655714Skris { 97755714Skris return(0); 97855714Skris } 97955714Skris return(s->method->ssl_peek(s,buf,num)); 98055714Skris } 98155714Skris 98276866Skrisint SSL_write(SSL *s,const void *buf,int num) 98355714Skris { 98455714Skris if (s->handshake_func == 0) 98555714Skris { 98655714Skris SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED); 98755714Skris return -1; 98855714Skris } 98955714Skris 99055714Skris if (s->shutdown & SSL_SENT_SHUTDOWN) 99155714Skris { 99255714Skris s->rwstate=SSL_NOTHING; 99355714Skris SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN); 99455714Skris return(-1); 99555714Skris } 99655714Skris return(s->method->ssl_write(s,buf,num)); 99755714Skris } 99855714Skris 99955714Skrisint SSL_shutdown(SSL *s) 100055714Skris { 100155714Skris /* Note that this function behaves differently from what one might 100255714Skris * expect. Return values are 0 for no success (yet), 100355714Skris * 1 for success; but calling it once is usually not enough, 100455714Skris * even if blocking I/O is used (see ssl3_shutdown). 100555714Skris */ 100655714Skris 100755714Skris if (s->handshake_func == 0) 100855714Skris { 100955714Skris SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED); 101055714Skris return -1; 101155714Skris } 101255714Skris 101355714Skris if ((s != NULL) && !SSL_in_init(s)) 101455714Skris return(s->method->ssl_shutdown(s)); 101555714Skris else 101655714Skris return(1); 101755714Skris } 101855714Skris 101955714Skrisint SSL_renegotiate(SSL *s) 102055714Skris { 1021238405Sjkim if (s->renegotiate == 0) 1022238405Sjkim s->renegotiate=1; 1023238405Sjkim 1024238405Sjkim s->new_session=1; 1025238405Sjkim 102655714Skris return(s->method->ssl_renegotiate(s)); 102755714Skris } 102855714Skris 1029238405Sjkimint SSL_renegotiate_abbreviated(SSL *s) 1030238405Sjkim { 1031238405Sjkim if (s->renegotiate == 0) 1032238405Sjkim s->renegotiate=1; 1033238405Sjkim 1034238405Sjkim s->new_session=0; 1035238405Sjkim 1036238405Sjkim return(s->method->ssl_renegotiate(s)); 1037238405Sjkim } 1038238405Sjkim 1039109998Smarkmint SSL_renegotiate_pending(SSL *s) 104055714Skris { 1041109998Smarkm /* becomes true when negotiation is requested; 1042109998Smarkm * false again once a handshake has finished */ 1043238405Sjkim return (s->renegotiate != 0); 1044109998Smarkm } 1045109998Smarkm 1046109998Smarkmlong SSL_ctrl(SSL *s,int cmd,long larg,void *parg) 1047109998Smarkm { 104855714Skris long l; 104955714Skris 105055714Skris switch (cmd) 105155714Skris { 105255714Skris case SSL_CTRL_GET_READ_AHEAD: 105355714Skris return(s->read_ahead); 105455714Skris case SSL_CTRL_SET_READ_AHEAD: 105555714Skris l=s->read_ahead; 105655714Skris s->read_ahead=larg; 105755714Skris return(l); 1058109998Smarkm 1059109998Smarkm case SSL_CTRL_SET_MSG_CALLBACK_ARG: 1060109998Smarkm s->msg_callback_arg = parg; 1061109998Smarkm return 1; 1062109998Smarkm 106355714Skris case SSL_CTRL_OPTIONS: 106455714Skris return(s->options|=larg); 1065205128Ssimon case SSL_CTRL_CLEAR_OPTIONS: 1066205128Ssimon return(s->options&=~larg); 106755714Skris case SSL_CTRL_MODE: 106855714Skris return(s->mode|=larg); 1069205128Ssimon case SSL_CTRL_CLEAR_MODE: 1070205128Ssimon return(s->mode &=~larg); 1071109998Smarkm case SSL_CTRL_GET_MAX_CERT_LIST: 1072109998Smarkm return(s->max_cert_list); 1073109998Smarkm case SSL_CTRL_SET_MAX_CERT_LIST: 1074109998Smarkm l=s->max_cert_list; 1075109998Smarkm s->max_cert_list=larg; 1076109998Smarkm return(l); 1077238405Sjkim case SSL_CTRL_SET_MAX_SEND_FRAGMENT: 1078238405Sjkim if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) 1079238405Sjkim return 0; 1080238405Sjkim s->max_send_fragment = larg; 1081238405Sjkim return 1; 1082205128Ssimon case SSL_CTRL_GET_RI_SUPPORT: 1083205128Ssimon if (s->s3) 1084205128Ssimon return s->s3->send_connection_binding; 1085205128Ssimon else return 0; 108655714Skris default: 108755714Skris return(s->method->ssl_ctrl(s,cmd,larg,parg)); 108855714Skris } 108955714Skris } 109055714Skris 1091160814Ssimonlong SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) 109259191Skris { 109359191Skris switch(cmd) 109459191Skris { 1095109998Smarkm case SSL_CTRL_SET_MSG_CALLBACK: 1096109998Smarkm s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp); 1097109998Smarkm return 1; 1098109998Smarkm 109959191Skris default: 110059191Skris return(s->method->ssl_callback_ctrl(s,cmd,fp)); 110159191Skris } 110259191Skris } 110359191Skris 1104238405SjkimLHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) 110559191Skris { 110659191Skris return ctx->sessions; 110759191Skris } 110859191Skris 1109109998Smarkmlong SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg) 111055714Skris { 111155714Skris long l; 111255714Skris 111355714Skris switch (cmd) 111455714Skris { 111555714Skris case SSL_CTRL_GET_READ_AHEAD: 111655714Skris return(ctx->read_ahead); 111755714Skris case SSL_CTRL_SET_READ_AHEAD: 111855714Skris l=ctx->read_ahead; 111955714Skris ctx->read_ahead=larg; 112055714Skris return(l); 1121109998Smarkm 1122109998Smarkm case SSL_CTRL_SET_MSG_CALLBACK_ARG: 1123109998Smarkm ctx->msg_callback_arg = parg; 1124109998Smarkm return 1; 112555714Skris 1126109998Smarkm case SSL_CTRL_GET_MAX_CERT_LIST: 1127109998Smarkm return(ctx->max_cert_list); 1128109998Smarkm case SSL_CTRL_SET_MAX_CERT_LIST: 1129109998Smarkm l=ctx->max_cert_list; 1130109998Smarkm ctx->max_cert_list=larg; 1131109998Smarkm return(l); 1132109998Smarkm 113355714Skris case SSL_CTRL_SET_SESS_CACHE_SIZE: 113455714Skris l=ctx->session_cache_size; 113555714Skris ctx->session_cache_size=larg; 113655714Skris return(l); 113755714Skris case SSL_CTRL_GET_SESS_CACHE_SIZE: 113855714Skris return(ctx->session_cache_size); 113955714Skris case SSL_CTRL_SET_SESS_CACHE_MODE: 114055714Skris l=ctx->session_cache_mode; 114155714Skris ctx->session_cache_mode=larg; 114255714Skris return(l); 114355714Skris case SSL_CTRL_GET_SESS_CACHE_MODE: 114455714Skris return(ctx->session_cache_mode); 114555714Skris 114655714Skris case SSL_CTRL_SESS_NUMBER: 1147238405Sjkim return(lh_SSL_SESSION_num_items(ctx->sessions)); 114855714Skris case SSL_CTRL_SESS_CONNECT: 114955714Skris return(ctx->stats.sess_connect); 115055714Skris case SSL_CTRL_SESS_CONNECT_GOOD: 115155714Skris return(ctx->stats.sess_connect_good); 115255714Skris case SSL_CTRL_SESS_CONNECT_RENEGOTIATE: 115355714Skris return(ctx->stats.sess_connect_renegotiate); 115455714Skris case SSL_CTRL_SESS_ACCEPT: 115555714Skris return(ctx->stats.sess_accept); 115655714Skris case SSL_CTRL_SESS_ACCEPT_GOOD: 115755714Skris return(ctx->stats.sess_accept_good); 115855714Skris case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE: 115955714Skris return(ctx->stats.sess_accept_renegotiate); 116055714Skris case SSL_CTRL_SESS_HIT: 116155714Skris return(ctx->stats.sess_hit); 116255714Skris case SSL_CTRL_SESS_CB_HIT: 116355714Skris return(ctx->stats.sess_cb_hit); 116455714Skris case SSL_CTRL_SESS_MISSES: 116555714Skris return(ctx->stats.sess_miss); 116655714Skris case SSL_CTRL_SESS_TIMEOUTS: 116755714Skris return(ctx->stats.sess_timeout); 116855714Skris case SSL_CTRL_SESS_CACHE_FULL: 116955714Skris return(ctx->stats.sess_cache_full); 117055714Skris case SSL_CTRL_OPTIONS: 117155714Skris return(ctx->options|=larg); 1172205128Ssimon case SSL_CTRL_CLEAR_OPTIONS: 1173205128Ssimon return(ctx->options&=~larg); 117455714Skris case SSL_CTRL_MODE: 117555714Skris return(ctx->mode|=larg); 1176205128Ssimon case SSL_CTRL_CLEAR_MODE: 1177205128Ssimon return(ctx->mode&=~larg); 1178238405Sjkim case SSL_CTRL_SET_MAX_SEND_FRAGMENT: 1179238405Sjkim if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) 1180238405Sjkim return 0; 1181238405Sjkim ctx->max_send_fragment = larg; 1182238405Sjkim return 1; 118355714Skris default: 118455714Skris return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg)); 118555714Skris } 118655714Skris } 118755714Skris 1188160814Ssimonlong SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) 118959191Skris { 119059191Skris switch(cmd) 119159191Skris { 1192109998Smarkm case SSL_CTRL_SET_MSG_CALLBACK: 1193109998Smarkm ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp); 1194109998Smarkm return 1; 1195109998Smarkm 119659191Skris default: 119759191Skris return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp)); 119859191Skris } 119959191Skris } 120059191Skris 120168651Skrisint ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b) 120255714Skris { 120355714Skris long l; 120455714Skris 120555714Skris l=a->id-b->id; 120655714Skris if (l == 0L) 120755714Skris return(0); 120855714Skris else 120955714Skris return((l > 0)?1:-1); 121055714Skris } 121155714Skris 121268651Skrisint ssl_cipher_ptr_id_cmp(const SSL_CIPHER * const *ap, 121368651Skris const SSL_CIPHER * const *bp) 121455714Skris { 121555714Skris long l; 121655714Skris 121755714Skris l=(*ap)->id-(*bp)->id; 121855714Skris if (l == 0L) 121955714Skris return(0); 122055714Skris else 122155714Skris return((l > 0)?1:-1); 122255714Skris } 122355714Skris 122455714Skris/** return a STACK of the ciphers available for the SSL and in order of 122555714Skris * preference */ 1226160814SsimonSTACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) 122755714Skris { 1228111147Snectar if (s != NULL) 122955714Skris { 1230111147Snectar if (s->cipher_list != NULL) 1231111147Snectar { 1232111147Snectar return(s->cipher_list); 1233111147Snectar } 1234111147Snectar else if ((s->ctx != NULL) && 1235111147Snectar (s->ctx->cipher_list != NULL)) 1236111147Snectar { 1237111147Snectar return(s->ctx->cipher_list); 1238111147Snectar } 123955714Skris } 124055714Skris return(NULL); 124155714Skris } 124255714Skris 124355714Skris/** return a STACK of the ciphers available for the SSL and in order of 124455714Skris * algorithm id */ 124555714SkrisSTACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) 124655714Skris { 1247111147Snectar if (s != NULL) 124855714Skris { 1249111147Snectar if (s->cipher_list_by_id != NULL) 1250111147Snectar { 1251111147Snectar return(s->cipher_list_by_id); 1252111147Snectar } 1253111147Snectar else if ((s->ctx != NULL) && 1254111147Snectar (s->ctx->cipher_list_by_id != NULL)) 1255111147Snectar { 1256111147Snectar return(s->ctx->cipher_list_by_id); 1257111147Snectar } 125855714Skris } 125955714Skris return(NULL); 126055714Skris } 126155714Skris 126255714Skris/** The old interface to get the same thing as SSL_get_ciphers() */ 1263160814Ssimonconst char *SSL_get_cipher_list(const SSL *s,int n) 126455714Skris { 126555714Skris SSL_CIPHER *c; 126655714Skris STACK_OF(SSL_CIPHER) *sk; 126755714Skris 126855714Skris if (s == NULL) return(NULL); 126955714Skris sk=SSL_get_ciphers(s); 127055714Skris if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n)) 127155714Skris return(NULL); 127255714Skris c=sk_SSL_CIPHER_value(sk,n); 127355714Skris if (c == NULL) return(NULL); 127455714Skris return(c->name); 127555714Skris } 127655714Skris 127759191Skris/** specify the ciphers to be used by default by the SSL_CTX */ 127859191Skrisint SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) 127955714Skris { 128055714Skris STACK_OF(SSL_CIPHER) *sk; 128155714Skris 128255714Skris sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list, 128355714Skris &ctx->cipher_list_by_id,str); 1284160814Ssimon /* ssl_create_cipher_list may return an empty stack if it 1285160814Ssimon * was unable to find a cipher matching the given rule string 1286160814Ssimon * (for example if the rule string specifies a cipher which 1287238405Sjkim * has been disabled). This is not an error as far as 1288238405Sjkim * ssl_create_cipher_list is concerned, and hence 1289160814Ssimon * ctx->cipher_list and ctx->cipher_list_by_id has been 1290160814Ssimon * updated. */ 1291160814Ssimon if (sk == NULL) 1292160814Ssimon return 0; 1293160814Ssimon else if (sk_SSL_CIPHER_num(sk) == 0) 1294160814Ssimon { 1295160814Ssimon SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH); 1296160814Ssimon return 0; 1297160814Ssimon } 1298160814Ssimon return 1; 129955714Skris } 130055714Skris 130155714Skris/** specify the ciphers to be used by the SSL */ 130259191Skrisint SSL_set_cipher_list(SSL *s,const char *str) 130355714Skris { 130455714Skris STACK_OF(SSL_CIPHER) *sk; 130555714Skris 130655714Skris sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list, 130755714Skris &s->cipher_list_by_id,str); 1308160814Ssimon /* see comment in SSL_CTX_set_cipher_list */ 1309160814Ssimon if (sk == NULL) 1310160814Ssimon return 0; 1311160814Ssimon else if (sk_SSL_CIPHER_num(sk) == 0) 1312160814Ssimon { 1313160814Ssimon SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH); 1314160814Ssimon return 0; 1315160814Ssimon } 1316160814Ssimon return 1; 131755714Skris } 131855714Skris 131955714Skris/* works well for SSLv2, not so good for SSLv3 */ 1320160814Ssimonchar *SSL_get_shared_ciphers(const SSL *s,char *buf,int len) 132155714Skris { 132255714Skris char *p; 132355714Skris STACK_OF(SSL_CIPHER) *sk; 132455714Skris SSL_CIPHER *c; 132555714Skris int i; 132655714Skris 132755714Skris if ((s->session == NULL) || (s->session->ciphers == NULL) || 132855714Skris (len < 2)) 132955714Skris return(NULL); 133055714Skris 133155714Skris p=buf; 133255714Skris sk=s->session->ciphers; 1333267256Sjkim 1334267256Sjkim if (sk_SSL_CIPHER_num(sk) == 0) 1335267256Sjkim return NULL; 1336267256Sjkim 133755714Skris for (i=0; i<sk_SSL_CIPHER_num(sk); i++) 133855714Skris { 1339172429Ssimon int n; 1340172429Ssimon 134155714Skris c=sk_SSL_CIPHER_value(sk,i); 1342172429Ssimon n=strlen(c->name); 1343172429Ssimon if (n+1 > len) 134455714Skris { 1345172429Ssimon if (p != buf) 1346172429Ssimon --p; 1347172429Ssimon *p='\0'; 1348172429Ssimon return buf; 134955714Skris } 1350172429Ssimon strcpy(p,c->name); 1351172429Ssimon p+=n; 135255714Skris *(p++)=':'; 1353172429Ssimon len-=n+1; 135455714Skris } 135555714Skris p[-1]='\0'; 135655714Skris return(buf); 135755714Skris } 135855714Skris 1359160814Ssimonint ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p, 1360238405Sjkim int (*put_cb)(const SSL_CIPHER *, unsigned char *)) 136155714Skris { 136255714Skris int i,j=0; 136355714Skris SSL_CIPHER *c; 136455714Skris unsigned char *q; 1365109998Smarkm#ifndef OPENSSL_NO_KRB5 1366238405Sjkim int nokrb5 = !kssl_tgt_is_available(s->kssl_ctx); 1367109998Smarkm#endif /* OPENSSL_NO_KRB5 */ 136855714Skris 136955714Skris if (sk == NULL) return(0); 137055714Skris q=p; 1371273144Sjkim if (put_cb == NULL) 1372273144Sjkim put_cb = s->method->put_cipher_by_char; 137355714Skris 137455714Skris for (i=0; i<sk_SSL_CIPHER_num(sk); i++) 137555714Skris { 137655714Skris c=sk_SSL_CIPHER_value(sk,i); 1377238405Sjkim /* Skip TLS v1.2 only ciphersuites if lower than v1.2 */ 1378238405Sjkim if ((c->algorithm_ssl & SSL_TLSV1_2) && 1379238405Sjkim (TLS1_get_client_version(s) < TLS1_2_VERSION)) 1380238405Sjkim continue; 1381109998Smarkm#ifndef OPENSSL_NO_KRB5 1382238405Sjkim if (((c->algorithm_mkey & SSL_kKRB5) || (c->algorithm_auth & SSL_aKRB5)) && 1383238405Sjkim nokrb5) 1384238405Sjkim continue; 1385238405Sjkim#endif /* OPENSSL_NO_KRB5 */ 1386238405Sjkim#ifndef OPENSSL_NO_PSK 1387238405Sjkim /* with PSK there must be client callback set */ 1388238405Sjkim if (((c->algorithm_mkey & SSL_kPSK) || (c->algorithm_auth & SSL_aPSK)) && 1389238405Sjkim s->psk_client_callback == NULL) 1390238405Sjkim continue; 1391238405Sjkim#endif /* OPENSSL_NO_PSK */ 1392269682Sjkim#ifndef OPENSSL_NO_SRP 1393269682Sjkim if (((c->algorithm_mkey & SSL_kSRP) || (c->algorithm_auth & SSL_aSRP)) && 1394269682Sjkim !(s->srp_ctx.srp_Mask & SSL_kSRP)) 1395269682Sjkim continue; 1396269682Sjkim#endif /* OPENSSL_NO_SRP */ 1397273144Sjkim j = put_cb(c,p); 139855714Skris p+=j; 139955714Skris } 1400273144Sjkim /* If p == q, no ciphers; caller indicates an error. 1401273144Sjkim * Otherwise, add applicable SCSVs. */ 1402273144Sjkim if (p != q) 1403205128Ssimon { 1404273144Sjkim if (!s->renegotiate) 1405205128Ssimon { 1406273144Sjkim static SSL_CIPHER scsv = 1407273144Sjkim { 1408273144Sjkim 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 1409273144Sjkim }; 1410273144Sjkim j = put_cb(&scsv,p); 1411273144Sjkim p+=j; 1412205128Ssimon#ifdef OPENSSL_RI_DEBUG 1413273144Sjkim fprintf(stderr, "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n"); 1414205128Ssimon#endif 1415273144Sjkim } 1416205128Ssimon 1417273144Sjkim if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) 1418273144Sjkim { 1419273144Sjkim static SSL_CIPHER scsv = 1420273144Sjkim { 1421273144Sjkim 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 1422273144Sjkim }; 1423273144Sjkim j = put_cb(&scsv,p); 1424273144Sjkim p+=j; 1425273144Sjkim } 1426273144Sjkim } 1427273144Sjkim 142855714Skris return(p-q); 142955714Skris } 143055714Skris 143155714SkrisSTACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num, 143255714Skris STACK_OF(SSL_CIPHER) **skp) 143355714Skris { 1434238405Sjkim const SSL_CIPHER *c; 143555714Skris STACK_OF(SSL_CIPHER) *sk; 143655714Skris int i,n; 1437273144Sjkim 1438205128Ssimon if (s->s3) 1439205128Ssimon s->s3->send_connection_binding = 0; 144055714Skris 144155714Skris n=ssl_put_cipher_by_char(s,NULL,NULL); 1442273144Sjkim if (n == 0 || (num%n) != 0) 144355714Skris { 144455714Skris SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); 144555714Skris return(NULL); 144655714Skris } 144755714Skris if ((skp == NULL) || (*skp == NULL)) 144868651Skris sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */ 144955714Skris else 145055714Skris { 145155714Skris sk= *skp; 145255714Skris sk_SSL_CIPHER_zero(sk); 145355714Skris } 145455714Skris 145555714Skris for (i=0; i<num; i+=n) 145655714Skris { 1457273144Sjkim /* Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV */ 1458205128Ssimon if (s->s3 && (n != 3 || !p[0]) && 1459205128Ssimon (p[n-2] == ((SSL3_CK_SCSV >> 8) & 0xff)) && 1460205128Ssimon (p[n-1] == (SSL3_CK_SCSV & 0xff))) 1461205128Ssimon { 1462205128Ssimon /* SCSV fatal if renegotiating */ 1463238405Sjkim if (s->renegotiate) 1464205128Ssimon { 1465205128Ssimon SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING); 1466205128Ssimon ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); 1467205128Ssimon goto err; 1468205128Ssimon } 1469205128Ssimon s->s3->send_connection_binding = 1; 1470205128Ssimon p += n; 1471205128Ssimon#ifdef OPENSSL_RI_DEBUG 1472205128Ssimon fprintf(stderr, "SCSV received by server\n"); 1473205128Ssimon#endif 1474205128Ssimon continue; 1475205128Ssimon } 1476205128Ssimon 1477273144Sjkim /* Check for TLS_FALLBACK_SCSV */ 1478273144Sjkim if ((n != 3 || !p[0]) && 1479273144Sjkim (p[n-2] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) && 1480273144Sjkim (p[n-1] == (SSL3_CK_FALLBACK_SCSV & 0xff))) 1481273144Sjkim { 1482273144Sjkim /* The SCSV indicates that the client previously tried a higher version. 1483273144Sjkim * Fail if the current version is an unexpected downgrade. */ 1484273144Sjkim if (!SSL_ctrl(s, SSL_CTRL_CHECK_PROTO_VERSION, 0, NULL)) 1485273144Sjkim { 1486273144Sjkim SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_INAPPROPRIATE_FALLBACK); 1487273144Sjkim if (s->s3) 1488273144Sjkim ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INAPPROPRIATE_FALLBACK); 1489273144Sjkim goto err; 1490273144Sjkim } 1491276861Sjkim p += n; 1492273144Sjkim continue; 1493273144Sjkim } 1494273144Sjkim 149555714Skris c=ssl_get_cipher_by_char(s,p); 149655714Skris p+=n; 149755714Skris if (c != NULL) 149855714Skris { 149955714Skris if (!sk_SSL_CIPHER_push(sk,c)) 150055714Skris { 150155714Skris SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE); 150255714Skris goto err; 150355714Skris } 150455714Skris } 150555714Skris } 150655714Skris 150755714Skris if (skp != NULL) 150855714Skris *skp=sk; 150955714Skris return(sk); 151055714Skriserr: 151155714Skris if ((skp == NULL) || (*skp == NULL)) 151255714Skris sk_SSL_CIPHER_free(sk); 151355714Skris return(NULL); 151455714Skris } 151555714Skris 1516238405Sjkim 1517194206Ssimon#ifndef OPENSSL_NO_TLSEXT 1518194206Ssimon/** return a servername extension value if provided in Client Hello, or NULL. 1519194206Ssimon * So far, only host_name types are defined (RFC 3546). 1520194206Ssimon */ 1521194206Ssimon 1522194206Ssimonconst char *SSL_get_servername(const SSL *s, const int type) 1523194206Ssimon { 1524194206Ssimon if (type != TLSEXT_NAMETYPE_host_name) 1525194206Ssimon return NULL; 1526194206Ssimon 1527194206Ssimon return s->session && !s->tlsext_hostname ? 1528194206Ssimon s->session->tlsext_hostname : 1529194206Ssimon s->tlsext_hostname; 1530194206Ssimon } 1531194206Ssimon 1532194206Ssimonint SSL_get_servername_type(const SSL *s) 1533194206Ssimon { 1534194206Ssimon if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) 1535194206Ssimon return TLSEXT_NAMETYPE_host_name; 1536194206Ssimon return -1; 1537194206Ssimon } 1538238405Sjkim 1539238405Sjkim# ifndef OPENSSL_NO_NEXTPROTONEG 1540238405Sjkim/* SSL_select_next_proto implements the standard protocol selection. It is 1541238405Sjkim * expected that this function is called from the callback set by 1542238405Sjkim * SSL_CTX_set_next_proto_select_cb. 1543238405Sjkim * 1544238405Sjkim * The protocol data is assumed to be a vector of 8-bit, length prefixed byte 1545238405Sjkim * strings. The length byte itself is not included in the length. A byte 1546238405Sjkim * string of length 0 is invalid. No byte string may be truncated. 1547238405Sjkim * 1548238405Sjkim * The current, but experimental algorithm for selecting the protocol is: 1549238405Sjkim * 1550238405Sjkim * 1) If the server doesn't support NPN then this is indicated to the 1551238405Sjkim * callback. In this case, the client application has to abort the connection 1552238405Sjkim * or have a default application level protocol. 1553238405Sjkim * 1554238405Sjkim * 2) If the server supports NPN, but advertises an empty list then the 1555238405Sjkim * client selects the first protcol in its list, but indicates via the 1556238405Sjkim * API that this fallback case was enacted. 1557238405Sjkim * 1558238405Sjkim * 3) Otherwise, the client finds the first protocol in the server's list 1559238405Sjkim * that it supports and selects this protocol. This is because it's 1560238405Sjkim * assumed that the server has better information about which protocol 1561238405Sjkim * a client should use. 1562238405Sjkim * 1563238405Sjkim * 4) If the client doesn't support any of the server's advertised 1564238405Sjkim * protocols, then this is treated the same as case 2. 1565238405Sjkim * 1566238405Sjkim * It returns either 1567238405Sjkim * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or 1568238405Sjkim * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached. 1569238405Sjkim */ 1570238405Sjkimint 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) 1571238405Sjkim { 1572238405Sjkim unsigned int i, j; 1573238405Sjkim const unsigned char *result; 1574238405Sjkim int status = OPENSSL_NPN_UNSUPPORTED; 1575238405Sjkim 1576238405Sjkim /* For each protocol in server preference order, see if we support it. */ 1577238405Sjkim for (i = 0; i < server_len; ) 1578238405Sjkim { 1579238405Sjkim for (j = 0; j < client_len; ) 1580238405Sjkim { 1581238405Sjkim if (server[i] == client[j] && 1582238405Sjkim memcmp(&server[i+1], &client[j+1], server[i]) == 0) 1583238405Sjkim { 1584238405Sjkim /* We found a match */ 1585238405Sjkim result = &server[i]; 1586238405Sjkim status = OPENSSL_NPN_NEGOTIATED; 1587238405Sjkim goto found; 1588238405Sjkim } 1589238405Sjkim j += client[j]; 1590238405Sjkim j++; 1591238405Sjkim } 1592238405Sjkim i += server[i]; 1593238405Sjkim i++; 1594238405Sjkim } 1595238405Sjkim 1596238405Sjkim /* There's no overlap between our protocols and the server's list. */ 1597238405Sjkim result = client; 1598238405Sjkim status = OPENSSL_NPN_NO_OVERLAP; 1599238405Sjkim 1600238405Sjkim found: 1601238405Sjkim *out = (unsigned char *) result + 1; 1602238405Sjkim *outlen = result[0]; 1603238405Sjkim return status; 1604238405Sjkim } 1605238405Sjkim 1606238405Sjkim/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's 1607238405Sjkim * requested protocol for this connection and returns 0. If the client didn't 1608238405Sjkim * request any protocol, then *data is set to NULL. 1609238405Sjkim * 1610238405Sjkim * Note that the client can request any protocol it chooses. The value returned 1611238405Sjkim * from this function need not be a member of the list of supported protocols 1612238405Sjkim * provided by the callback. 1613238405Sjkim */ 1614238405Sjkimvoid SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data, unsigned *len) 1615238405Sjkim { 1616238405Sjkim *data = s->next_proto_negotiated; 1617238405Sjkim if (!*data) { 1618238405Sjkim *len = 0; 1619238405Sjkim } else { 1620238405Sjkim *len = s->next_proto_negotiated_len; 1621238405Sjkim } 1622238405Sjkim} 1623238405Sjkim 1624238405Sjkim/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a 1625238405Sjkim * TLS server needs a list of supported protocols for Next Protocol 1626238405Sjkim * Negotiation. The returned list must be in wire format. The list is returned 1627238405Sjkim * by setting |out| to point to it and |outlen| to its length. This memory will 1628238405Sjkim * not be modified, but one should assume that the SSL* keeps a reference to 1629238405Sjkim * it. 1630238405Sjkim * 1631238405Sjkim * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no 1632238405Sjkim * such extension will be included in the ServerHello. */ 1633238405Sjkimvoid 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) 1634238405Sjkim { 1635238405Sjkim ctx->next_protos_advertised_cb = cb; 1636238405Sjkim ctx->next_protos_advertised_cb_arg = arg; 1637238405Sjkim } 1638238405Sjkim 1639238405Sjkim/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a 1640238405Sjkim * client needs to select a protocol from the server's provided list. |out| 1641238405Sjkim * must be set to point to the selected protocol (which may be within |in|). 1642238405Sjkim * The length of the protocol name must be written into |outlen|. The server's 1643238405Sjkim * advertised protocols are provided in |in| and |inlen|. The callback can 1644238405Sjkim * assume that |in| is syntactically valid. 1645238405Sjkim * 1646238405Sjkim * The client must select a protocol. It is fatal to the connection if this 1647238405Sjkim * callback returns a value other than SSL_TLSEXT_ERR_OK. 1648238405Sjkim */ 1649238405Sjkimvoid 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) 1650238405Sjkim { 1651238405Sjkim ctx->next_proto_select_cb = cb; 1652238405Sjkim ctx->next_proto_select_cb_arg = arg; 1653238405Sjkim } 1654238405Sjkim# endif 1655194206Ssimon#endif 1656194206Ssimon 1657238405Sjkimint SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen, 1658238405Sjkim const char *label, size_t llen, const unsigned char *p, size_t plen, 1659238405Sjkim int use_context) 166055714Skris { 1661238405Sjkim if (s->version < TLS1_VERSION) 1662238405Sjkim return -1; 1663238405Sjkim 1664238405Sjkim return s->method->ssl3_enc->export_keying_material(s, out, olen, label, 1665238405Sjkim llen, p, plen, 1666238405Sjkim use_context); 1667238405Sjkim } 1668238405Sjkim 1669238405Sjkimstatic unsigned long ssl_session_hash(const SSL_SESSION *a) 1670238405Sjkim { 167155714Skris unsigned long l; 167255714Skris 167355714Skris l=(unsigned long) 167455714Skris ((unsigned int) a->session_id[0] )| 167555714Skris ((unsigned int) a->session_id[1]<< 8L)| 167655714Skris ((unsigned long)a->session_id[2]<<16L)| 167755714Skris ((unsigned long)a->session_id[3]<<24L); 167855714Skris return(l); 167955714Skris } 168055714Skris 1681109998Smarkm/* NB: If this function (or indeed the hash function which uses a sort of 1682109998Smarkm * coarser function than this one) is changed, ensure 1683109998Smarkm * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being 1684109998Smarkm * able to construct an SSL_SESSION that will collide with any existing session 1685109998Smarkm * with a matching session ID. */ 1686238405Sjkimstatic int ssl_session_cmp(const SSL_SESSION *a,const SSL_SESSION *b) 168755714Skris { 168855714Skris if (a->ssl_version != b->ssl_version) 168955714Skris return(1); 169055714Skris if (a->session_id_length != b->session_id_length) 169155714Skris return(1); 169255714Skris return(memcmp(a->session_id,b->session_id,a->session_id_length)); 169355714Skris } 169455714Skris 1695109998Smarkm/* These wrapper functions should remain rather than redeclaring 1696109998Smarkm * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each 1697109998Smarkm * variable. The reason is that the functions aren't static, they're exposed via 1698109998Smarkm * ssl.h. */ 1699238405Sjkimstatic IMPLEMENT_LHASH_HASH_FN(ssl_session, SSL_SESSION) 1700238405Sjkimstatic IMPLEMENT_LHASH_COMP_FN(ssl_session, SSL_SESSION) 1701109998Smarkm 1702238405SjkimSSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) 170355714Skris { 170455714Skris SSL_CTX *ret=NULL; 1705238405Sjkim 170655714Skris if (meth == NULL) 170755714Skris { 170855714Skris SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED); 170955714Skris return(NULL); 171055714Skris } 171155714Skris 1712194206Ssimon#ifdef OPENSSL_FIPS 1713194206Ssimon if (FIPS_mode() && (meth->version < TLS1_VERSION)) 1714194206Ssimon { 1715194206Ssimon SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE); 1716194206Ssimon return NULL; 1717194206Ssimon } 1718194206Ssimon#endif 1719194206Ssimon 172055714Skris if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) 172155714Skris { 172255714Skris SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS); 172355714Skris goto err; 172455714Skris } 172568651Skris ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX)); 172655714Skris if (ret == NULL) 172755714Skris goto err; 172855714Skris 172955714Skris memset(ret,0,sizeof(SSL_CTX)); 173055714Skris 173155714Skris ret->method=meth; 173255714Skris 173355714Skris ret->cert_store=NULL; 173455714Skris ret->session_cache_mode=SSL_SESS_CACHE_SERVER; 173555714Skris ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; 173655714Skris ret->session_cache_head=NULL; 173755714Skris ret->session_cache_tail=NULL; 173855714Skris 173955714Skris /* We take the system default */ 174055714Skris ret->session_timeout=meth->get_timeout(); 174155714Skris 1742109998Smarkm ret->new_session_cb=0; 1743109998Smarkm ret->remove_session_cb=0; 1744109998Smarkm ret->get_session_cb=0; 1745109998Smarkm ret->generate_session_id=0; 174655714Skris 174755714Skris memset((char *)&ret->stats,0,sizeof(ret->stats)); 174855714Skris 174955714Skris ret->references=1; 175055714Skris ret->quiet_shutdown=0; 175155714Skris 175255714Skris/* ret->cipher=NULL;*/ 175355714Skris/* ret->s2->challenge=NULL; 175455714Skris ret->master_key=NULL; 175555714Skris ret->key_arg=NULL; 175655714Skris ret->s2->conn_id=NULL; */ 175755714Skris 175855714Skris ret->info_callback=NULL; 175955714Skris 1760109998Smarkm ret->app_verify_callback=0; 176155714Skris ret->app_verify_arg=NULL; 176255714Skris 1763109998Smarkm ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT; 176455714Skris ret->read_ahead=0; 1765109998Smarkm ret->msg_callback=0; 1766109998Smarkm ret->msg_callback_arg=NULL; 176755714Skris ret->verify_mode=SSL_VERIFY_NONE; 1768160814Ssimon#if 0 176955714Skris ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */ 1770160814Ssimon#endif 1771109998Smarkm ret->sid_ctx_length=0; 177255714Skris ret->default_verify_callback=NULL; 177355714Skris if ((ret->cert=ssl_cert_new()) == NULL) 177455714Skris goto err; 177555714Skris 1776109998Smarkm ret->default_passwd_callback=0; 177755714Skris ret->default_passwd_callback_userdata=NULL; 1778109998Smarkm ret->client_cert_cb=0; 1779160814Ssimon ret->app_gen_cookie_cb=0; 1780160814Ssimon ret->app_verify_cookie_cb=0; 178155714Skris 1782238405Sjkim ret->sessions=lh_SSL_SESSION_new(); 178355714Skris if (ret->sessions == NULL) goto err; 178455714Skris ret->cert_store=X509_STORE_new(); 178555714Skris if (ret->cert_store == NULL) goto err; 178655714Skris 178755714Skris ssl_create_cipher_list(ret->method, 178855714Skris &ret->cipher_list,&ret->cipher_list_by_id, 1789238405Sjkim meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST); 179055714Skris if (ret->cipher_list == NULL 179155714Skris || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) 179255714Skris { 179355714Skris SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS); 179455714Skris goto err2; 179555714Skris } 179655714Skris 1797160814Ssimon ret->param = X509_VERIFY_PARAM_new(); 1798160814Ssimon if (!ret->param) 1799160814Ssimon goto err; 1800160814Ssimon 180155714Skris if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL) 180255714Skris { 180355714Skris SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES); 180455714Skris goto err2; 180555714Skris } 180655714Skris if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL) 180755714Skris { 180855714Skris SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES); 180955714Skris goto err2; 181055714Skris } 181155714Skris if ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL) 181255714Skris { 181355714Skris SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES); 181455714Skris goto err2; 181555714Skris } 181655714Skris 181755714Skris if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL) 181855714Skris goto err; 181955714Skris 1820109998Smarkm CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data); 182155714Skris 182255714Skris ret->extra_certs=NULL; 1823261037Sjkim /* No compression for DTLS */ 1824261037Sjkim if (meth->version != DTLS1_VERSION) 1825261037Sjkim ret->comp_methods=SSL_COMP_get_compression_methods(); 182655714Skris 1827238405Sjkim ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; 1828238405Sjkim 1829194206Ssimon#ifndef OPENSSL_NO_TLSEXT 1830194206Ssimon ret->tlsext_servername_callback = 0; 1831194206Ssimon ret->tlsext_servername_arg = NULL; 1832194206Ssimon /* Setup RFC4507 ticket keys */ 1833194206Ssimon if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0) 1834194206Ssimon || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0) 1835194206Ssimon || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0)) 1836194206Ssimon ret->options |= SSL_OP_NO_TICKET; 1837194206Ssimon 1838194206Ssimon ret->tlsext_status_cb = 0; 1839194206Ssimon ret->tlsext_status_arg = NULL; 1840194206Ssimon 1841238405Sjkim# ifndef OPENSSL_NO_NEXTPROTONEG 1842238405Sjkim ret->next_protos_advertised_cb = 0; 1843238405Sjkim ret->next_proto_select_cb = 0; 1844238405Sjkim# endif 1845194206Ssimon#endif 1846238405Sjkim#ifndef OPENSSL_NO_PSK 1847238405Sjkim ret->psk_identity_hint=NULL; 1848238405Sjkim ret->psk_client_callback=NULL; 1849238405Sjkim ret->psk_server_callback=NULL; 1850238405Sjkim#endif 1851238405Sjkim#ifndef OPENSSL_NO_SRP 1852238405Sjkim SSL_CTX_SRP_CTX_init(ret); 1853238405Sjkim#endif 1854238405Sjkim#ifndef OPENSSL_NO_BUF_FREELISTS 1855238405Sjkim ret->freelist_max_len = SSL_MAX_BUF_FREELIST_LEN_DEFAULT; 1856238405Sjkim ret->rbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST)); 1857238405Sjkim if (!ret->rbuf_freelist) 1858238405Sjkim goto err; 1859238405Sjkim ret->rbuf_freelist->chunklen = 0; 1860238405Sjkim ret->rbuf_freelist->len = 0; 1861238405Sjkim ret->rbuf_freelist->head = NULL; 1862238405Sjkim ret->wbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST)); 1863238405Sjkim if (!ret->wbuf_freelist) 1864238405Sjkim { 1865238405Sjkim OPENSSL_free(ret->rbuf_freelist); 1866238405Sjkim goto err; 1867238405Sjkim } 1868238405Sjkim ret->wbuf_freelist->chunklen = 0; 1869238405Sjkim ret->wbuf_freelist->len = 0; 1870238405Sjkim ret->wbuf_freelist->head = NULL; 1871238405Sjkim#endif 1872194206Ssimon#ifndef OPENSSL_NO_ENGINE 1873194206Ssimon ret->client_cert_engine = NULL; 1874194206Ssimon#ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO 1875194206Ssimon#define eng_strx(x) #x 1876194206Ssimon#define eng_str(x) eng_strx(x) 1877194206Ssimon /* Use specific client engine automatically... ignore errors */ 1878194206Ssimon { 1879194206Ssimon ENGINE *eng; 1880194206Ssimon eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO)); 1881194206Ssimon if (!eng) 1882194206Ssimon { 1883194206Ssimon ERR_clear_error(); 1884194206Ssimon ENGINE_load_builtin_engines(); 1885194206Ssimon eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO)); 1886194206Ssimon } 1887194206Ssimon if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng)) 1888194206Ssimon ERR_clear_error(); 1889194206Ssimon } 1890194206Ssimon#endif 1891194206Ssimon#endif 1892205128Ssimon /* Default is to connect to non-RI servers. When RI is more widely 1893205128Ssimon * deployed might change this. 1894205128Ssimon */ 1895205128Ssimon ret->options |= SSL_OP_LEGACY_SERVER_CONNECT; 1896194206Ssimon 189755714Skris return(ret); 189855714Skriserr: 189955714Skris SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE); 190055714Skriserr2: 190155714Skris if (ret != NULL) SSL_CTX_free(ret); 190255714Skris return(NULL); 190355714Skris } 190455714Skris 1905109998Smarkm#if 0 190655714Skrisstatic void SSL_COMP_free(SSL_COMP *comp) 190768651Skris { OPENSSL_free(comp); } 1908109998Smarkm#endif 190955714Skris 1910238405Sjkim#ifndef OPENSSL_NO_BUF_FREELISTS 1911238405Sjkimstatic void 1912238405Sjkimssl_buf_freelist_free(SSL3_BUF_FREELIST *list) 1913238405Sjkim { 1914238405Sjkim SSL3_BUF_FREELIST_ENTRY *ent, *next; 1915238405Sjkim for (ent = list->head; ent; ent = next) 1916238405Sjkim { 1917238405Sjkim next = ent->next; 1918238405Sjkim OPENSSL_free(ent); 1919238405Sjkim } 1920238405Sjkim OPENSSL_free(list); 1921238405Sjkim } 1922238405Sjkim#endif 1923238405Sjkim 192455714Skrisvoid SSL_CTX_free(SSL_CTX *a) 192555714Skris { 192655714Skris int i; 192755714Skris 192855714Skris if (a == NULL) return; 192955714Skris 193055714Skris i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX); 193155714Skris#ifdef REF_PRINT 193255714Skris REF_PRINT("SSL_CTX",a); 193355714Skris#endif 193455714Skris if (i > 0) return; 193555714Skris#ifdef REF_CHECK 193655714Skris if (i < 0) 193755714Skris { 193855714Skris fprintf(stderr,"SSL_CTX_free, bad reference count\n"); 193955714Skris abort(); /* ok */ 194055714Skris } 194155714Skris#endif 194255714Skris 1943160814Ssimon if (a->param) 1944160814Ssimon X509_VERIFY_PARAM_free(a->param); 1945160814Ssimon 1946109998Smarkm /* 1947109998Smarkm * Free internal session cache. However: the remove_cb() may reference 1948109998Smarkm * the ex_data of SSL_CTX, thus the ex_data store can only be removed 1949109998Smarkm * after the sessions were flushed. 1950109998Smarkm * As the ex_data handling routines might also touch the session cache, 1951109998Smarkm * the most secure solution seems to be: empty (flush) the cache, then 1952109998Smarkm * free ex_data, then finally free the cache. 1953109998Smarkm * (See ticket [openssl.org #212].) 1954109998Smarkm */ 195555714Skris if (a->sessions != NULL) 195655714Skris SSL_CTX_flush_sessions(a,0); 1957109998Smarkm 1958109998Smarkm CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data); 1959109998Smarkm 1960109998Smarkm if (a->sessions != NULL) 1961238405Sjkim lh_SSL_SESSION_free(a->sessions); 1962109998Smarkm 196355714Skris if (a->cert_store != NULL) 196455714Skris X509_STORE_free(a->cert_store); 196555714Skris if (a->cipher_list != NULL) 196655714Skris sk_SSL_CIPHER_free(a->cipher_list); 196755714Skris if (a->cipher_list_by_id != NULL) 196855714Skris sk_SSL_CIPHER_free(a->cipher_list_by_id); 196955714Skris if (a->cert != NULL) 197055714Skris ssl_cert_free(a->cert); 197155714Skris if (a->client_CA != NULL) 197255714Skris sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free); 197355714Skris if (a->extra_certs != NULL) 197455714Skris sk_X509_pop_free(a->extra_certs,X509_free); 1975109998Smarkm#if 0 /* This should never be done, since it removes a global database */ 197655714Skris if (a->comp_methods != NULL) 197755714Skris sk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free); 1978109998Smarkm#else 1979109998Smarkm a->comp_methods = NULL; 1980109998Smarkm#endif 1981238405Sjkim 1982246772Sjkim#ifndef OPENSSL_NO_SRTP 1983238405Sjkim if (a->srtp_profiles) 1984238405Sjkim sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles); 1985246772Sjkim#endif 1986238405Sjkim 1987238405Sjkim#ifndef OPENSSL_NO_PSK 1988238405Sjkim if (a->psk_identity_hint) 1989238405Sjkim OPENSSL_free(a->psk_identity_hint); 1990238405Sjkim#endif 1991238405Sjkim#ifndef OPENSSL_NO_SRP 1992238405Sjkim SSL_CTX_SRP_CTX_free(a); 1993238405Sjkim#endif 1994194206Ssimon#ifndef OPENSSL_NO_ENGINE 1995194206Ssimon if (a->client_cert_engine) 1996194206Ssimon ENGINE_finish(a->client_cert_engine); 1997194206Ssimon#endif 1998238405Sjkim 1999238405Sjkim#ifndef OPENSSL_NO_BUF_FREELISTS 2000238405Sjkim if (a->wbuf_freelist) 2001238405Sjkim ssl_buf_freelist_free(a->wbuf_freelist); 2002238405Sjkim if (a->rbuf_freelist) 2003238405Sjkim ssl_buf_freelist_free(a->rbuf_freelist); 2004238405Sjkim#endif 2005238405Sjkim 200668651Skris OPENSSL_free(a); 200755714Skris } 200855714Skris 200955714Skrisvoid SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) 201055714Skris { 201155714Skris ctx->default_passwd_callback=cb; 201255714Skris } 201355714Skris 201455714Skrisvoid SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u) 201555714Skris { 201655714Skris ctx->default_passwd_callback_userdata=u; 201755714Skris } 201855714Skris 2019109998Smarkmvoid SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg) 202055714Skris { 202155714Skris ctx->app_verify_callback=cb; 2022109998Smarkm ctx->app_verify_arg=arg; 202355714Skris } 202455714Skris 202555714Skrisvoid SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *)) 202655714Skris { 202755714Skris ctx->verify_mode=mode; 202855714Skris ctx->default_verify_callback=cb; 202955714Skris } 203055714Skris 203155714Skrisvoid SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth) 203255714Skris { 2033160814Ssimon X509_VERIFY_PARAM_set_depth(ctx->param, depth); 203455714Skris } 203555714Skris 2036238405Sjkimvoid ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher) 203755714Skris { 203855714Skris CERT_PKEY *cpk; 203955714Skris int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign; 204055714Skris int rsa_enc_export,dh_rsa_export,dh_dsa_export; 204155714Skris int rsa_tmp_export,dh_tmp_export,kl; 2042238405Sjkim unsigned long mask_k,mask_a,emask_k,emask_a; 2043160814Ssimon int have_ecc_cert, ecdh_ok, ecdsa_ok, ecc_pkey_size; 2044160814Ssimon#ifndef OPENSSL_NO_ECDH 2045160814Ssimon int have_ecdh_tmp; 2046160814Ssimon#endif 2047160814Ssimon X509 *x = NULL; 2048160814Ssimon EVP_PKEY *ecc_pkey = NULL; 2049238405Sjkim int signature_nid = 0, pk_nid = 0, md_nid = 0; 205055714Skris 205155714Skris if (c == NULL) return; 205255714Skris 205355714Skris kl=SSL_C_EXPORT_PKEYLENGTH(cipher); 205455714Skris 2055109998Smarkm#ifndef OPENSSL_NO_RSA 205655714Skris rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL); 205755714Skris rsa_tmp_export=(c->rsa_tmp_cb != NULL || 205855714Skris (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl)); 205955714Skris#else 206055714Skris rsa_tmp=rsa_tmp_export=0; 206155714Skris#endif 2062109998Smarkm#ifndef OPENSSL_NO_DH 206355714Skris dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL); 206455714Skris dh_tmp_export=(c->dh_tmp_cb != NULL || 206555714Skris (dh_tmp && DH_size(c->dh_tmp)*8 <= kl)); 206655714Skris#else 206755714Skris dh_tmp=dh_tmp_export=0; 206855714Skris#endif 206955714Skris 2070160814Ssimon#ifndef OPENSSL_NO_ECDH 2071160814Ssimon have_ecdh_tmp=(c->ecdh_tmp != NULL || c->ecdh_tmp_cb != NULL); 2072160814Ssimon#endif 207355714Skris cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]); 207455714Skris rsa_enc= (cpk->x509 != NULL && cpk->privatekey != NULL); 207555714Skris rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl); 207655714Skris cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]); 207755714Skris rsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL); 207855714Skris cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]); 207955714Skris dsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL); 208055714Skris cpk= &(c->pkeys[SSL_PKEY_DH_RSA]); 208155714Skris dh_rsa= (cpk->x509 != NULL && cpk->privatekey != NULL); 208255714Skris dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl); 208355714Skris cpk= &(c->pkeys[SSL_PKEY_DH_DSA]); 208455714Skris/* FIX THIS EAY EAY EAY */ 208555714Skris dh_dsa= (cpk->x509 != NULL && cpk->privatekey != NULL); 208655714Skris dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl); 2087160814Ssimon cpk= &(c->pkeys[SSL_PKEY_ECC]); 2088160814Ssimon have_ecc_cert= (cpk->x509 != NULL && cpk->privatekey != NULL); 2089238405Sjkim mask_k=0; 2090238405Sjkim mask_a=0; 2091238405Sjkim emask_k=0; 2092238405Sjkim emask_a=0; 209355714Skris 2094238405Sjkim 2095238405Sjkim 209655714Skris#ifdef CIPHER_DEBUG 2097276861Sjkim fprintf(stderr,"rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n", 2098238405Sjkim rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp, 209955714Skris rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa); 210055714Skris#endif 2101238405Sjkim 2102238405Sjkim cpk = &(c->pkeys[SSL_PKEY_GOST01]); 2103238405Sjkim if (cpk->x509 != NULL && cpk->privatekey !=NULL) { 2104238405Sjkim mask_k |= SSL_kGOST; 2105238405Sjkim mask_a |= SSL_aGOST01; 2106238405Sjkim } 2107238405Sjkim cpk = &(c->pkeys[SSL_PKEY_GOST94]); 2108238405Sjkim if (cpk->x509 != NULL && cpk->privatekey !=NULL) { 2109238405Sjkim mask_k |= SSL_kGOST; 2110238405Sjkim mask_a |= SSL_aGOST94; 2111238405Sjkim } 211255714Skris 211355714Skris if (rsa_enc || (rsa_tmp && rsa_sign)) 2114238405Sjkim mask_k|=SSL_kRSA; 211555714Skris if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc))) 2116238405Sjkim emask_k|=SSL_kRSA; 211755714Skris 211855714Skris#if 0 211955714Skris /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */ 2120238405Sjkim if ( (dh_tmp || dh_rsa || dh_dsa) && 212155714Skris (rsa_enc || rsa_sign || dsa_sign)) 2122238405Sjkim mask_k|=SSL_kEDH; 212355714Skris if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) && 212455714Skris (rsa_enc || rsa_sign || dsa_sign)) 2125238405Sjkim emask_k|=SSL_kEDH; 212655714Skris#endif 212755714Skris 2128238405Sjkim if (dh_tmp_export) 2129238405Sjkim emask_k|=SSL_kEDH; 213055714Skris 213155714Skris if (dh_tmp) 2132238405Sjkim mask_k|=SSL_kEDH; 213355714Skris 2134238405Sjkim if (dh_rsa) mask_k|=SSL_kDHr; 2135238405Sjkim if (dh_rsa_export) emask_k|=SSL_kDHr; 213655714Skris 2137238405Sjkim if (dh_dsa) mask_k|=SSL_kDHd; 2138238405Sjkim if (dh_dsa_export) emask_k|=SSL_kDHd; 213955714Skris 214055714Skris if (rsa_enc || rsa_sign) 214155714Skris { 2142238405Sjkim mask_a|=SSL_aRSA; 2143238405Sjkim emask_a|=SSL_aRSA; 214455714Skris } 214555714Skris 214655714Skris if (dsa_sign) 214755714Skris { 2148238405Sjkim mask_a|=SSL_aDSS; 2149238405Sjkim emask_a|=SSL_aDSS; 215055714Skris } 215155714Skris 2152238405Sjkim mask_a|=SSL_aNULL; 2153238405Sjkim emask_a|=SSL_aNULL; 215455714Skris 2155109998Smarkm#ifndef OPENSSL_NO_KRB5 2156238405Sjkim mask_k|=SSL_kKRB5; 2157238405Sjkim mask_a|=SSL_aKRB5; 2158238405Sjkim emask_k|=SSL_kKRB5; 2159238405Sjkim emask_a|=SSL_aKRB5; 2160109998Smarkm#endif 2161109998Smarkm 2162160814Ssimon /* An ECC certificate may be usable for ECDH and/or 2163160814Ssimon * ECDSA cipher suites depending on the key usage extension. 2164160814Ssimon */ 2165160814Ssimon if (have_ecc_cert) 2166160814Ssimon { 2167238405Sjkim /* This call populates extension flags (ex_flags) */ 2168160814Ssimon x = (c->pkeys[SSL_PKEY_ECC]).x509; 2169160814Ssimon X509_check_purpose(x, -1, 0); 2170160814Ssimon ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ? 2171160814Ssimon (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1; 2172160814Ssimon ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ? 2173160814Ssimon (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1; 2174160814Ssimon ecc_pkey = X509_get_pubkey(x); 2175238405Sjkim ecc_pkey_size = (ecc_pkey != NULL) ? 2176160814Ssimon EVP_PKEY_bits(ecc_pkey) : 0; 2177160814Ssimon EVP_PKEY_free(ecc_pkey); 2178160814Ssimon if ((x->sig_alg) && (x->sig_alg->algorithm)) 2179238405Sjkim { 2180160814Ssimon signature_nid = OBJ_obj2nid(x->sig_alg->algorithm); 2181238405Sjkim OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid); 2182238405Sjkim } 2183160814Ssimon#ifndef OPENSSL_NO_ECDH 2184160814Ssimon if (ecdh_ok) 2185160814Ssimon { 2186238405Sjkim 2187238405Sjkim if (pk_nid == NID_rsaEncryption || pk_nid == NID_rsa) 2188160814Ssimon { 2189238405Sjkim mask_k|=SSL_kECDHr; 2190238405Sjkim mask_a|=SSL_aECDH; 2191160814Ssimon if (ecc_pkey_size <= 163) 2192238405Sjkim { 2193238405Sjkim emask_k|=SSL_kECDHr; 2194238405Sjkim emask_a|=SSL_aECDH; 2195238405Sjkim } 2196160814Ssimon } 2197238405Sjkim 2198238405Sjkim if (pk_nid == NID_X9_62_id_ecPublicKey) 2199160814Ssimon { 2200238405Sjkim mask_k|=SSL_kECDHe; 2201238405Sjkim mask_a|=SSL_aECDH; 2202160814Ssimon if (ecc_pkey_size <= 163) 2203238405Sjkim { 2204238405Sjkim emask_k|=SSL_kECDHe; 2205238405Sjkim emask_a|=SSL_aECDH; 2206238405Sjkim } 2207160814Ssimon } 2208160814Ssimon } 2209160814Ssimon#endif 2210160814Ssimon#ifndef OPENSSL_NO_ECDSA 2211160814Ssimon if (ecdsa_ok) 2212160814Ssimon { 2213238405Sjkim mask_a|=SSL_aECDSA; 2214238405Sjkim emask_a|=SSL_aECDSA; 2215160814Ssimon } 2216160814Ssimon#endif 2217160814Ssimon } 2218160814Ssimon 2219160814Ssimon#ifndef OPENSSL_NO_ECDH 2220160814Ssimon if (have_ecdh_tmp) 2221160814Ssimon { 2222238405Sjkim mask_k|=SSL_kEECDH; 2223238405Sjkim emask_k|=SSL_kEECDH; 2224160814Ssimon } 2225160814Ssimon#endif 2226238405Sjkim 2227238405Sjkim#ifndef OPENSSL_NO_PSK 2228238405Sjkim mask_k |= SSL_kPSK; 2229238405Sjkim mask_a |= SSL_aPSK; 2230238405Sjkim emask_k |= SSL_kPSK; 2231238405Sjkim emask_a |= SSL_aPSK; 2232238405Sjkim#endif 2233238405Sjkim 2234238405Sjkim c->mask_k=mask_k; 2235238405Sjkim c->mask_a=mask_a; 2236238405Sjkim c->export_mask_k=emask_k; 2237238405Sjkim c->export_mask_a=emask_a; 223855714Skris c->valid=1; 223955714Skris } 224055714Skris 2241160814Ssimon/* This handy macro borrowed from crypto/x509v3/v3_purp.c */ 2242160814Ssimon#define ku_reject(x, usage) \ 2243160814Ssimon (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage))) 2244160814Ssimon 2245238405Sjkim#ifndef OPENSSL_NO_EC 2246238405Sjkim 2247238405Sjkimint ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) 2248160814Ssimon { 2249238405Sjkim unsigned long alg_k, alg_a; 2250160814Ssimon EVP_PKEY *pkey = NULL; 2251160814Ssimon int keysize = 0; 2252238405Sjkim int signature_nid = 0, md_nid = 0, pk_nid = 0; 2253238405Sjkim const SSL_CIPHER *cs = s->s3->tmp.new_cipher; 2254160814Ssimon 2255238405Sjkim alg_k = cs->algorithm_mkey; 2256238405Sjkim alg_a = cs->algorithm_auth; 2257238405Sjkim 2258160814Ssimon if (SSL_C_IS_EXPORT(cs)) 2259160814Ssimon { 2260160814Ssimon /* ECDH key length in export ciphers must be <= 163 bits */ 2261160814Ssimon pkey = X509_get_pubkey(x); 2262160814Ssimon if (pkey == NULL) return 0; 2263160814Ssimon keysize = EVP_PKEY_bits(pkey); 2264160814Ssimon EVP_PKEY_free(pkey); 2265160814Ssimon if (keysize > 163) return 0; 2266160814Ssimon } 2267160814Ssimon 2268160814Ssimon /* This call populates the ex_flags field correctly */ 2269160814Ssimon X509_check_purpose(x, -1, 0); 2270160814Ssimon if ((x->sig_alg) && (x->sig_alg->algorithm)) 2271238405Sjkim { 2272160814Ssimon signature_nid = OBJ_obj2nid(x->sig_alg->algorithm); 2273238405Sjkim OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid); 2274238405Sjkim } 2275238405Sjkim if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr) 2276160814Ssimon { 2277160814Ssimon /* key usage, if present, must allow key agreement */ 2278160814Ssimon if (ku_reject(x, X509v3_KU_KEY_AGREEMENT)) 2279160814Ssimon { 2280238405Sjkim SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT); 2281160814Ssimon return 0; 2282160814Ssimon } 2283238405Sjkim if ((alg_k & SSL_kECDHe) && TLS1_get_version(s) < TLS1_2_VERSION) 2284160814Ssimon { 2285160814Ssimon /* signature alg must be ECDSA */ 2286238405Sjkim if (pk_nid != NID_X9_62_id_ecPublicKey) 2287160814Ssimon { 2288238405Sjkim SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE); 2289160814Ssimon return 0; 2290160814Ssimon } 2291160814Ssimon } 2292238405Sjkim if ((alg_k & SSL_kECDHr) && TLS1_get_version(s) < TLS1_2_VERSION) 2293160814Ssimon { 2294160814Ssimon /* signature alg must be RSA */ 2295238405Sjkim 2296238405Sjkim if (pk_nid != NID_rsaEncryption && pk_nid != NID_rsa) 2297160814Ssimon { 2298238405Sjkim SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE); 2299160814Ssimon return 0; 2300160814Ssimon } 2301160814Ssimon } 2302238405Sjkim } 2303238405Sjkim if (alg_a & SSL_aECDSA) 2304160814Ssimon { 2305160814Ssimon /* key usage, if present, must allow signing */ 2306160814Ssimon if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) 2307160814Ssimon { 2308238405Sjkim SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_SIGNING); 2309160814Ssimon return 0; 2310160814Ssimon } 2311160814Ssimon } 2312160814Ssimon 2313160814Ssimon return 1; /* all checks are ok */ 2314160814Ssimon } 2315160814Ssimon 2316238405Sjkim#endif 2317238405Sjkim 231855714Skris/* THIS NEEDS CLEANING UP */ 2319246772SjkimCERT_PKEY *ssl_get_server_send_pkey(const SSL *s) 232055714Skris { 2321238405Sjkim unsigned long alg_k,alg_a; 232255714Skris CERT *c; 2323215697Ssimon int i; 232455714Skris 232555714Skris c=s->cert; 232655714Skris ssl_set_cert_masks(c, s->s3->tmp.new_cipher); 2327238405Sjkim 2328238405Sjkim alg_k = s->s3->tmp.new_cipher->algorithm_mkey; 2329238405Sjkim alg_a = s->s3->tmp.new_cipher->algorithm_auth; 233055714Skris 2331238405Sjkim if (alg_k & (SSL_kECDHr|SSL_kECDHe)) 2332160814Ssimon { 2333238405Sjkim /* we don't need to look at SSL_kEECDH 2334160814Ssimon * since no certificate is needed for 2335160814Ssimon * anon ECDH and for authenticated 2336238405Sjkim * EECDH, the check for the auth 2337160814Ssimon * algorithm will set i correctly 2338160814Ssimon * NOTE: For ECDH-RSA, we need an ECC 2339238405Sjkim * not an RSA cert but for EECDH-RSA 2340160814Ssimon * we need an RSA cert. Placing the 2341160814Ssimon * checks for SSL_kECDH before RSA 2342160814Ssimon * checks ensures the correct cert is chosen. 2343160814Ssimon */ 2344160814Ssimon i=SSL_PKEY_ECC; 2345160814Ssimon } 2346238405Sjkim else if (alg_a & SSL_aECDSA) 2347160814Ssimon { 2348160814Ssimon i=SSL_PKEY_ECC; 2349160814Ssimon } 2350238405Sjkim else if (alg_k & SSL_kDHr) 235155714Skris i=SSL_PKEY_DH_RSA; 2352238405Sjkim else if (alg_k & SSL_kDHd) 235355714Skris i=SSL_PKEY_DH_DSA; 2354238405Sjkim else if (alg_a & SSL_aDSS) 235555714Skris i=SSL_PKEY_DSA_SIGN; 2356238405Sjkim else if (alg_a & SSL_aRSA) 235755714Skris { 235855714Skris if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL) 235955714Skris i=SSL_PKEY_RSA_SIGN; 236055714Skris else 236155714Skris i=SSL_PKEY_RSA_ENC; 236255714Skris } 2363238405Sjkim else if (alg_a & SSL_aKRB5) 2364109998Smarkm { 2365109998Smarkm /* VRS something else here? */ 2366109998Smarkm return(NULL); 2367109998Smarkm } 2368238405Sjkim else if (alg_a & SSL_aGOST94) 2369238405Sjkim i=SSL_PKEY_GOST94; 2370238405Sjkim else if (alg_a & SSL_aGOST01) 2371238405Sjkim i=SSL_PKEY_GOST01; 2372238405Sjkim else /* if (alg_a & SSL_aNULL) */ 237355714Skris { 2374246772Sjkim SSLerr(SSL_F_SSL_GET_SERVER_SEND_PKEY,ERR_R_INTERNAL_ERROR); 237555714Skris return(NULL); 237655714Skris } 2377160814Ssimon 2378246772Sjkim return c->pkeys + i; 237955714Skris } 238055714Skris 2381246772SjkimX509 *ssl_get_server_send_cert(const SSL *s) 2382246772Sjkim { 2383246772Sjkim CERT_PKEY *cpk; 2384246772Sjkim cpk = ssl_get_server_send_pkey(s); 2385246772Sjkim if (!cpk) 2386246772Sjkim return NULL; 2387246772Sjkim return cpk->x509; 2388246772Sjkim } 2389246772Sjkim 2390238405SjkimEVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher, const EVP_MD **pmd) 239155714Skris { 2392238405Sjkim unsigned long alg_a; 239355714Skris CERT *c; 2394238405Sjkim int idx = -1; 239555714Skris 2396238405Sjkim alg_a = cipher->algorithm_auth; 239755714Skris c=s->cert; 239855714Skris 2399238405Sjkim if ((alg_a & SSL_aDSS) && 240055714Skris (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL)) 2401238405Sjkim idx = SSL_PKEY_DSA_SIGN; 2402238405Sjkim else if (alg_a & SSL_aRSA) 240355714Skris { 240455714Skris if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) 2405238405Sjkim idx = SSL_PKEY_RSA_SIGN; 240655714Skris else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) 2407238405Sjkim idx = SSL_PKEY_RSA_ENC; 240855714Skris } 2409238405Sjkim else if ((alg_a & SSL_aECDSA) && 2410160814Ssimon (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) 2411238405Sjkim idx = SSL_PKEY_ECC; 2412238405Sjkim if (idx == -1) 241355714Skris { 2414109998Smarkm SSLerr(SSL_F_SSL_GET_SIGN_PKEY,ERR_R_INTERNAL_ERROR); 241555714Skris return(NULL); 241655714Skris } 2417238405Sjkim if (pmd) 2418238405Sjkim *pmd = c->pkeys[idx].digest; 2419238405Sjkim return c->pkeys[idx].privatekey; 242055714Skris } 242155714Skris 242255714Skrisvoid ssl_update_cache(SSL *s,int mode) 242355714Skris { 242455714Skris int i; 242555714Skris 242655714Skris /* If the session_id_length is 0, we are not supposed to cache it, 242755714Skris * and it would be rather hard to do anyway :-) */ 242855714Skris if (s->session->session_id_length == 0) return; 242955714Skris 2430238405Sjkim i=s->session_ctx->session_cache_mode; 2431100928Snectar if ((i & mode) && (!s->hit) 2432109998Smarkm && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) 2433238405Sjkim || SSL_CTX_add_session(s->session_ctx,s->session)) 2434238405Sjkim && (s->session_ctx->new_session_cb != NULL)) 243555714Skris { 243655714Skris CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION); 2437238405Sjkim if (!s->session_ctx->new_session_cb(s,s->session)) 243855714Skris SSL_SESSION_free(s->session); 243955714Skris } 244055714Skris 244155714Skris /* auto flush every 255 connections */ 244255714Skris if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && 244355714Skris ((i & mode) == mode)) 244455714Skris { 244555714Skris if ( (((mode & SSL_SESS_CACHE_CLIENT) 2446238405Sjkim ?s->session_ctx->stats.sess_connect_good 2447238405Sjkim :s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff) 244855714Skris { 2449238405Sjkim SSL_CTX_flush_sessions(s->session_ctx,(unsigned long)time(NULL)); 245055714Skris } 245155714Skris } 245255714Skris } 245355714Skris 2454238405Sjkimconst SSL_METHOD *SSL_get_ssl_method(SSL *s) 245555714Skris { 245655714Skris return(s->method); 245755714Skris } 245855714Skris 2459238405Sjkimint SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth) 246055714Skris { 246155714Skris int conn= -1; 246255714Skris int ret=1; 246355714Skris 246455714Skris if (s->method != meth) 246555714Skris { 246655714Skris if (s->handshake_func != NULL) 246755714Skris conn=(s->handshake_func == s->method->ssl_connect); 246855714Skris 246955714Skris if (s->method->version == meth->version) 247055714Skris s->method=meth; 247155714Skris else 247255714Skris { 247355714Skris s->method->ssl_free(s); 247455714Skris s->method=meth; 247555714Skris ret=s->method->ssl_new(s); 247655714Skris } 247755714Skris 247855714Skris if (conn == 1) 247955714Skris s->handshake_func=meth->ssl_connect; 248055714Skris else if (conn == 0) 248155714Skris s->handshake_func=meth->ssl_accept; 248255714Skris } 248355714Skris return(ret); 248455714Skris } 248555714Skris 2486160814Ssimonint SSL_get_error(const SSL *s,int i) 248755714Skris { 248855714Skris int reason; 248955714Skris unsigned long l; 249055714Skris BIO *bio; 249155714Skris 249255714Skris if (i > 0) return(SSL_ERROR_NONE); 249355714Skris 249455714Skris /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake 249555714Skris * etc, where we do encode the error */ 249655714Skris if ((l=ERR_peek_error()) != 0) 249755714Skris { 249855714Skris if (ERR_GET_LIB(l) == ERR_LIB_SYS) 249955714Skris return(SSL_ERROR_SYSCALL); 250055714Skris else 250155714Skris return(SSL_ERROR_SSL); 250255714Skris } 250355714Skris 250455714Skris if ((i < 0) && SSL_want_read(s)) 250555714Skris { 250655714Skris bio=SSL_get_rbio(s); 250755714Skris if (BIO_should_read(bio)) 250855714Skris return(SSL_ERROR_WANT_READ); 250955714Skris else if (BIO_should_write(bio)) 251055714Skris /* This one doesn't make too much sense ... We never try 251155714Skris * to write to the rbio, and an application program where 251255714Skris * rbio and wbio are separate couldn't even know what it 251355714Skris * should wait for. 251455714Skris * However if we ever set s->rwstate incorrectly 251555714Skris * (so that we have SSL_want_read(s) instead of 251655714Skris * SSL_want_write(s)) and rbio and wbio *are* the same, 251755714Skris * this test works around that bug; so it might be safer 251855714Skris * to keep it. */ 251955714Skris return(SSL_ERROR_WANT_WRITE); 252055714Skris else if (BIO_should_io_special(bio)) 252155714Skris { 252255714Skris reason=BIO_get_retry_reason(bio); 252355714Skris if (reason == BIO_RR_CONNECT) 252455714Skris return(SSL_ERROR_WANT_CONNECT); 2525109998Smarkm else if (reason == BIO_RR_ACCEPT) 2526109998Smarkm return(SSL_ERROR_WANT_ACCEPT); 252755714Skris else 252855714Skris return(SSL_ERROR_SYSCALL); /* unknown */ 252955714Skris } 253055714Skris } 253155714Skris 253255714Skris if ((i < 0) && SSL_want_write(s)) 253355714Skris { 253455714Skris bio=SSL_get_wbio(s); 253555714Skris if (BIO_should_write(bio)) 253655714Skris return(SSL_ERROR_WANT_WRITE); 253755714Skris else if (BIO_should_read(bio)) 253855714Skris /* See above (SSL_want_read(s) with BIO_should_write(bio)) */ 253955714Skris return(SSL_ERROR_WANT_READ); 254055714Skris else if (BIO_should_io_special(bio)) 254155714Skris { 254255714Skris reason=BIO_get_retry_reason(bio); 254355714Skris if (reason == BIO_RR_CONNECT) 254455714Skris return(SSL_ERROR_WANT_CONNECT); 2545109998Smarkm else if (reason == BIO_RR_ACCEPT) 2546109998Smarkm return(SSL_ERROR_WANT_ACCEPT); 254755714Skris else 254855714Skris return(SSL_ERROR_SYSCALL); 254955714Skris } 255055714Skris } 255155714Skris if ((i < 0) && SSL_want_x509_lookup(s)) 255255714Skris { 255355714Skris return(SSL_ERROR_WANT_X509_LOOKUP); 255455714Skris } 255555714Skris 255655714Skris if (i == 0) 255755714Skris { 255855714Skris if (s->version == SSL2_VERSION) 255955714Skris { 256055714Skris /* assume it is the socket being closed */ 256155714Skris return(SSL_ERROR_ZERO_RETURN); 256255714Skris } 256355714Skris else 256455714Skris { 256555714Skris if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) && 256655714Skris (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) 256755714Skris return(SSL_ERROR_ZERO_RETURN); 256855714Skris } 256955714Skris } 257055714Skris return(SSL_ERROR_SYSCALL); 257155714Skris } 257255714Skris 257355714Skrisint SSL_do_handshake(SSL *s) 257455714Skris { 257555714Skris int ret=1; 257655714Skris 257755714Skris if (s->handshake_func == NULL) 257855714Skris { 257955714Skris SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET); 258055714Skris return(-1); 258155714Skris } 258255714Skris 258355714Skris s->method->ssl_renegotiate_check(s); 258455714Skris 258555714Skris if (SSL_in_init(s) || SSL_in_before(s)) 258655714Skris { 258755714Skris ret=s->handshake_func(s); 258855714Skris } 258955714Skris return(ret); 259055714Skris } 259155714Skris 259255714Skris/* For the next 2 functions, SSL_clear() sets shutdown and so 259355714Skris * one of these calls will reset it */ 259455714Skrisvoid SSL_set_accept_state(SSL *s) 259555714Skris { 259655714Skris s->server=1; 259755714Skris s->shutdown=0; 259855714Skris s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE; 259955714Skris s->handshake_func=s->method->ssl_accept; 260055714Skris /* clear the current cipher */ 260155714Skris ssl_clear_cipher_ctx(s); 2602238405Sjkim ssl_clear_hash_ctx(&s->read_hash); 2603238405Sjkim ssl_clear_hash_ctx(&s->write_hash); 260455714Skris } 260555714Skris 260655714Skrisvoid SSL_set_connect_state(SSL *s) 260755714Skris { 260855714Skris s->server=0; 260955714Skris s->shutdown=0; 261055714Skris s->state=SSL_ST_CONNECT|SSL_ST_BEFORE; 261155714Skris s->handshake_func=s->method->ssl_connect; 261255714Skris /* clear the current cipher */ 261355714Skris ssl_clear_cipher_ctx(s); 2614238405Sjkim ssl_clear_hash_ctx(&s->read_hash); 2615238405Sjkim ssl_clear_hash_ctx(&s->write_hash); 261655714Skris } 261755714Skris 261855714Skrisint ssl_undefined_function(SSL *s) 261955714Skris { 262055714Skris SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 262155714Skris return(0); 262255714Skris } 262355714Skris 2624160814Ssimonint ssl_undefined_void_function(void) 2625160814Ssimon { 2626160814Ssimon SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2627160814Ssimon return(0); 2628160814Ssimon } 2629160814Ssimon 2630160814Ssimonint ssl_undefined_const_function(const SSL *s) 2631160814Ssimon { 2632160814Ssimon SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2633160814Ssimon return(0); 2634160814Ssimon } 2635160814Ssimon 263655714SkrisSSL_METHOD *ssl_bad_method(int ver) 263755714Skris { 263855714Skris SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 263955714Skris return(NULL); 264055714Skris } 264155714Skris 2642160814Ssimonconst char *SSL_get_version(const SSL *s) 264355714Skris { 2644238405Sjkim if (s->version == TLS1_2_VERSION) 2645238405Sjkim return("TLSv1.2"); 2646238405Sjkim else if (s->version == TLS1_1_VERSION) 2647238405Sjkim return("TLSv1.1"); 2648246772Sjkim else if (s->version == TLS1_VERSION) 264955714Skris return("TLSv1"); 265055714Skris else if (s->version == SSL3_VERSION) 265155714Skris return("SSLv3"); 265255714Skris else if (s->version == SSL2_VERSION) 265355714Skris return("SSLv2"); 265455714Skris else 265555714Skris return("unknown"); 265655714Skris } 265755714Skris 265855714SkrisSSL *SSL_dup(SSL *s) 265955714Skris { 266055714Skris STACK_OF(X509_NAME) *sk; 266155714Skris X509_NAME *xn; 266255714Skris SSL *ret; 266355714Skris int i; 2664238405Sjkim 266555714Skris if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL) 266655714Skris return(NULL); 2667109998Smarkm 2668109998Smarkm ret->version = s->version; 2669109998Smarkm ret->type = s->type; 2670109998Smarkm ret->method = s->method; 2671109998Smarkm 267255714Skris if (s->session != NULL) 267355714Skris { 267455714Skris /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */ 267555714Skris SSL_copy_session_id(ret,s); 267655714Skris } 267755714Skris else 267855714Skris { 267955714Skris /* No session has been established yet, so we have to expect 268055714Skris * that s->cert or ret->cert will be changed later -- 268155714Skris * they should not both point to the same object, 268255714Skris * and thus we can't use SSL_copy_session_id. */ 268355714Skris 2684111147Snectar ret->method->ssl_free(ret); 268555714Skris ret->method = s->method; 268655714Skris ret->method->ssl_new(ret); 268755714Skris 268855714Skris if (s->cert != NULL) 268955714Skris { 269076866Skris if (ret->cert != NULL) 269176866Skris { 269276866Skris ssl_cert_free(ret->cert); 269376866Skris } 269455714Skris ret->cert = ssl_cert_dup(s->cert); 269555714Skris if (ret->cert == NULL) 269655714Skris goto err; 269755714Skris } 269855714Skris 269955714Skris SSL_set_session_id_context(ret, 270055714Skris s->sid_ctx, s->sid_ctx_length); 270155714Skris } 270255714Skris 2703109998Smarkm ret->options=s->options; 2704109998Smarkm ret->mode=s->mode; 2705109998Smarkm SSL_set_max_cert_list(ret,SSL_get_max_cert_list(s)); 270655714Skris SSL_set_read_ahead(ret,SSL_get_read_ahead(s)); 2707109998Smarkm ret->msg_callback = s->msg_callback; 2708109998Smarkm ret->msg_callback_arg = s->msg_callback_arg; 270955714Skris SSL_set_verify(ret,SSL_get_verify_mode(s), 271055714Skris SSL_get_verify_callback(s)); 271155714Skris SSL_set_verify_depth(ret,SSL_get_verify_depth(s)); 2712109998Smarkm ret->generate_session_id = s->generate_session_id; 271355714Skris 271455714Skris SSL_set_info_callback(ret,SSL_get_info_callback(s)); 271555714Skris 271655714Skris ret->debug=s->debug; 271755714Skris 271855714Skris /* copy app data, a little dangerous perhaps */ 2719109998Smarkm if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data)) 272055714Skris goto err; 272155714Skris 272255714Skris /* setup rbio, and wbio */ 272355714Skris if (s->rbio != NULL) 272455714Skris { 272555714Skris if (!BIO_dup_state(s->rbio,(char *)&ret->rbio)) 272655714Skris goto err; 272755714Skris } 272855714Skris if (s->wbio != NULL) 272955714Skris { 273055714Skris if (s->wbio != s->rbio) 273155714Skris { 273255714Skris if (!BIO_dup_state(s->wbio,(char *)&ret->wbio)) 273355714Skris goto err; 273455714Skris } 273555714Skris else 273655714Skris ret->wbio=ret->rbio; 273755714Skris } 2738109998Smarkm ret->rwstate = s->rwstate; 2739109998Smarkm ret->in_handshake = s->in_handshake; 2740109998Smarkm ret->handshake_func = s->handshake_func; 2741109998Smarkm ret->server = s->server; 2742238405Sjkim ret->renegotiate = s->renegotiate; 2743109998Smarkm ret->new_session = s->new_session; 2744109998Smarkm ret->quiet_shutdown = s->quiet_shutdown; 2745109998Smarkm ret->shutdown=s->shutdown; 2746109998Smarkm ret->state=s->state; /* SSL_dup does not really work at any state, though */ 2747109998Smarkm ret->rstate=s->rstate; 2748109998Smarkm ret->init_num = 0; /* would have to copy ret->init_buf, ret->init_msg, ret->init_num, ret->init_off */ 2749109998Smarkm ret->hit=s->hit; 275055714Skris 2751160814Ssimon X509_VERIFY_PARAM_inherit(ret->param, s->param); 2752160814Ssimon 275355714Skris /* dup the cipher_list and cipher_list_by_id stacks */ 275455714Skris if (s->cipher_list != NULL) 275555714Skris { 275655714Skris if ((ret->cipher_list=sk_SSL_CIPHER_dup(s->cipher_list)) == NULL) 275755714Skris goto err; 275855714Skris } 275955714Skris if (s->cipher_list_by_id != NULL) 276055714Skris if ((ret->cipher_list_by_id=sk_SSL_CIPHER_dup(s->cipher_list_by_id)) 276155714Skris == NULL) 276255714Skris goto err; 276355714Skris 276455714Skris /* Dup the client_CA list */ 276555714Skris if (s->client_CA != NULL) 276655714Skris { 276755714Skris if ((sk=sk_X509_NAME_dup(s->client_CA)) == NULL) goto err; 276855714Skris ret->client_CA=sk; 276955714Skris for (i=0; i<sk_X509_NAME_num(sk); i++) 277055714Skris { 277155714Skris xn=sk_X509_NAME_value(sk,i); 277255714Skris if (sk_X509_NAME_set(sk,i,X509_NAME_dup(xn)) == NULL) 277355714Skris { 277455714Skris X509_NAME_free(xn); 277555714Skris goto err; 277655714Skris } 277755714Skris } 277855714Skris } 277955714Skris 278055714Skris if (0) 278155714Skris { 278255714Skriserr: 278355714Skris if (ret != NULL) SSL_free(ret); 278455714Skris ret=NULL; 278555714Skris } 278655714Skris return(ret); 278755714Skris } 278855714Skris 278955714Skrisvoid ssl_clear_cipher_ctx(SSL *s) 279055714Skris { 279155714Skris if (s->enc_read_ctx != NULL) 279255714Skris { 279355714Skris EVP_CIPHER_CTX_cleanup(s->enc_read_ctx); 279468651Skris OPENSSL_free(s->enc_read_ctx); 279555714Skris s->enc_read_ctx=NULL; 279655714Skris } 279755714Skris if (s->enc_write_ctx != NULL) 279855714Skris { 279955714Skris EVP_CIPHER_CTX_cleanup(s->enc_write_ctx); 280068651Skris OPENSSL_free(s->enc_write_ctx); 280155714Skris s->enc_write_ctx=NULL; 280255714Skris } 2803160814Ssimon#ifndef OPENSSL_NO_COMP 280455714Skris if (s->expand != NULL) 280555714Skris { 280655714Skris COMP_CTX_free(s->expand); 280755714Skris s->expand=NULL; 280855714Skris } 280955714Skris if (s->compress != NULL) 281055714Skris { 281155714Skris COMP_CTX_free(s->compress); 281255714Skris s->compress=NULL; 281355714Skris } 2814160814Ssimon#endif 281555714Skris } 281655714Skris 281755714Skris/* Fix this function so that it takes an optional type parameter */ 2818160814SsimonX509 *SSL_get_certificate(const SSL *s) 281955714Skris { 2820261037Sjkim if (s->cert != NULL) 282155714Skris return(s->cert->key->x509); 282255714Skris else 282355714Skris return(NULL); 282455714Skris } 282555714Skris 282655714Skris/* Fix this function so that it takes an optional type parameter */ 282755714SkrisEVP_PKEY *SSL_get_privatekey(SSL *s) 282855714Skris { 282955714Skris if (s->cert != NULL) 283055714Skris return(s->cert->key->privatekey); 283155714Skris else 283255714Skris return(NULL); 283355714Skris } 283455714Skris 2835238405Sjkimconst SSL_CIPHER *SSL_get_current_cipher(const SSL *s) 283655714Skris { 283755714Skris if ((s->session != NULL) && (s->session->cipher != NULL)) 283855714Skris return(s->session->cipher); 283955714Skris return(NULL); 284055714Skris } 2841160814Ssimon#ifdef OPENSSL_NO_COMP 2842160814Ssimonconst void *SSL_get_current_compression(SSL *s) 2843160814Ssimon { 2844160814Ssimon return NULL; 2845160814Ssimon } 2846160814Ssimonconst void *SSL_get_current_expansion(SSL *s) 2847160814Ssimon { 2848160814Ssimon return NULL; 2849160814Ssimon } 2850160814Ssimon#else 285155714Skris 2852160814Ssimonconst COMP_METHOD *SSL_get_current_compression(SSL *s) 2853160814Ssimon { 2854160814Ssimon if (s->compress != NULL) 2855160814Ssimon return(s->compress->meth); 2856160814Ssimon return(NULL); 2857160814Ssimon } 2858160814Ssimon 2859160814Ssimonconst COMP_METHOD *SSL_get_current_expansion(SSL *s) 2860160814Ssimon { 2861160814Ssimon if (s->expand != NULL) 2862160814Ssimon return(s->expand->meth); 2863160814Ssimon return(NULL); 2864160814Ssimon } 2865160814Ssimon#endif 2866160814Ssimon 286755714Skrisint ssl_init_wbio_buffer(SSL *s,int push) 286855714Skris { 286955714Skris BIO *bbio; 287055714Skris 287155714Skris if (s->bbio == NULL) 287255714Skris { 287355714Skris bbio=BIO_new(BIO_f_buffer()); 287455714Skris if (bbio == NULL) return(0); 287555714Skris s->bbio=bbio; 287655714Skris } 287755714Skris else 287855714Skris { 287955714Skris bbio=s->bbio; 288055714Skris if (s->bbio == s->wbio) 288155714Skris s->wbio=BIO_pop(s->wbio); 288255714Skris } 288355714Skris (void)BIO_reset(bbio); 288455714Skris/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */ 288555714Skris if (!BIO_set_read_buffer_size(bbio,1)) 288655714Skris { 288755714Skris SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB); 288855714Skris return(0); 288955714Skris } 289055714Skris if (push) 289155714Skris { 289255714Skris if (s->wbio != bbio) 289355714Skris s->wbio=BIO_push(bbio,s->wbio); 289455714Skris } 289555714Skris else 289655714Skris { 289755714Skris if (s->wbio == bbio) 289855714Skris s->wbio=BIO_pop(bbio); 289955714Skris } 290055714Skris return(1); 290155714Skris } 290255714Skris 290355714Skrisvoid ssl_free_wbio_buffer(SSL *s) 290455714Skris { 290555714Skris if (s->bbio == NULL) return; 290655714Skris 290755714Skris if (s->bbio == s->wbio) 290855714Skris { 290955714Skris /* remove buffering */ 291068651Skris s->wbio=BIO_pop(s->wbio); 291168651Skris#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */ 291268651Skris assert(s->wbio != NULL); 2913238405Sjkim#endif 291468651Skris } 291555714Skris BIO_free(s->bbio); 291655714Skris s->bbio=NULL; 291755714Skris } 291855714Skris 291955714Skrisvoid SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode) 292055714Skris { 292155714Skris ctx->quiet_shutdown=mode; 292255714Skris } 292355714Skris 2924160814Ssimonint SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) 292555714Skris { 292655714Skris return(ctx->quiet_shutdown); 292755714Skris } 292855714Skris 292955714Skrisvoid SSL_set_quiet_shutdown(SSL *s,int mode) 293055714Skris { 293155714Skris s->quiet_shutdown=mode; 293255714Skris } 293355714Skris 2934160814Ssimonint SSL_get_quiet_shutdown(const SSL *s) 293555714Skris { 293655714Skris return(s->quiet_shutdown); 293755714Skris } 293855714Skris 293955714Skrisvoid SSL_set_shutdown(SSL *s,int mode) 294055714Skris { 294155714Skris s->shutdown=mode; 294255714Skris } 294355714Skris 2944160814Ssimonint SSL_get_shutdown(const SSL *s) 294555714Skris { 294655714Skris return(s->shutdown); 294755714Skris } 294855714Skris 2949160814Ssimonint SSL_version(const SSL *s) 295055714Skris { 295155714Skris return(s->version); 295255714Skris } 295355714Skris 2954160814SsimonSSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) 295555714Skris { 295655714Skris return(ssl->ctx); 295755714Skris } 295855714Skris 2959194206SsimonSSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx) 2960194206Ssimon { 2961273144Sjkim CERT *ocert = ssl->cert; 2962194206Ssimon if (ssl->ctx == ctx) 2963194206Ssimon return ssl->ctx; 2964194206Ssimon#ifndef OPENSSL_NO_TLSEXT 2965194206Ssimon if (ctx == NULL) 2966194206Ssimon ctx = ssl->initial_ctx; 2967194206Ssimon#endif 2968194206Ssimon ssl->cert = ssl_cert_dup(ctx->cert); 2969273144Sjkim if (ocert != NULL) 2970273144Sjkim { 2971273144Sjkim int i; 2972273144Sjkim /* Copy negotiated digests from original */ 2973273144Sjkim for (i = 0; i < SSL_PKEY_NUM; i++) 2974273144Sjkim { 2975273144Sjkim CERT_PKEY *cpk = ocert->pkeys + i; 2976273144Sjkim CERT_PKEY *rpk = ssl->cert->pkeys + i; 2977273144Sjkim rpk->digest = cpk->digest; 2978273144Sjkim } 2979273144Sjkim ssl_cert_free(ocert); 2980273144Sjkim } 2981276861Sjkim 2982276861Sjkim /* 2983276861Sjkim * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH), 2984276861Sjkim * so setter APIs must prevent invalid lengths from entering the system. 2985276861Sjkim */ 2986276861Sjkim OPENSSL_assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx)); 2987276861Sjkim 2988276861Sjkim /* 2989276861Sjkim * If the session ID context matches that of the parent SSL_CTX, 2990276861Sjkim * inherit it from the new SSL_CTX as well. If however the context does 2991276861Sjkim * not match (i.e., it was set per-ssl with SSL_set_session_id_context), 2992276861Sjkim * leave it unchanged. 2993276861Sjkim */ 2994276861Sjkim if ((ssl->ctx != NULL) && 2995276861Sjkim (ssl->sid_ctx_length == ssl->ctx->sid_ctx_length) && 2996276861Sjkim (memcmp(ssl->sid_ctx, ssl->ctx->sid_ctx, ssl->sid_ctx_length) == 0)) 2997276861Sjkim { 2998276861Sjkim ssl->sid_ctx_length = ctx->sid_ctx_length; 2999276861Sjkim memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx)); 3000276861Sjkim } 3001276861Sjkim 3002194206Ssimon CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX); 3003194206Ssimon if (ssl->ctx != NULL) 3004194206Ssimon SSL_CTX_free(ssl->ctx); /* decrement reference count */ 3005194206Ssimon ssl->ctx = ctx; 3006276861Sjkim 3007194206Ssimon return(ssl->ctx); 3008194206Ssimon } 3009194206Ssimon 3010109998Smarkm#ifndef OPENSSL_NO_STDIO 301155714Skrisint SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) 301255714Skris { 301355714Skris return(X509_STORE_set_default_paths(ctx->cert_store)); 301455714Skris } 301555714Skris 301655714Skrisint SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile, 301755714Skris const char *CApath) 301855714Skris { 3019160814Ssimon return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath)); 302055714Skris } 302155714Skris#endif 302255714Skris 3023109998Smarkmvoid SSL_set_info_callback(SSL *ssl, 3024167612Ssimon void (*cb)(const SSL *ssl,int type,int val)) 302555714Skris { 302655714Skris ssl->info_callback=cb; 302755714Skris } 302855714Skris 3029160814Ssimon/* One compiler (Diab DCC) doesn't like argument names in returned 3030160814Ssimon function pointer. */ 3031238405Sjkimvoid (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/) 303255714Skris { 3033109998Smarkm return ssl->info_callback; 303455714Skris } 303555714Skris 3036160814Ssimonint SSL_state(const SSL *ssl) 303755714Skris { 303855714Skris return(ssl->state); 303955714Skris } 304055714Skris 3041238405Sjkimvoid SSL_set_state(SSL *ssl, int state) 3042238405Sjkim { 3043238405Sjkim ssl->state = state; 3044238405Sjkim } 3045238405Sjkim 304655714Skrisvoid SSL_set_verify_result(SSL *ssl,long arg) 304755714Skris { 304855714Skris ssl->verify_result=arg; 304955714Skris } 305055714Skris 3051160814Ssimonlong SSL_get_verify_result(const SSL *ssl) 305255714Skris { 305355714Skris return(ssl->verify_result); 305455714Skris } 305555714Skris 305659191Skrisint SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func, 305759191Skris CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func) 305855714Skris { 3059109998Smarkm return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, 3060109998Smarkm new_func, dup_func, free_func); 306155714Skris } 306255714Skris 306355714Skrisint SSL_set_ex_data(SSL *s,int idx,void *arg) 306455714Skris { 306555714Skris return(CRYPTO_set_ex_data(&s->ex_data,idx,arg)); 306655714Skris } 306755714Skris 3068160814Ssimonvoid *SSL_get_ex_data(const SSL *s,int idx) 306955714Skris { 307055714Skris return(CRYPTO_get_ex_data(&s->ex_data,idx)); 307155714Skris } 307255714Skris 307359191Skrisint SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func, 307459191Skris CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func) 307555714Skris { 3076109998Smarkm return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, 3077109998Smarkm new_func, dup_func, free_func); 307855714Skris } 307955714Skris 308055714Skrisint SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg) 308155714Skris { 308255714Skris return(CRYPTO_set_ex_data(&s->ex_data,idx,arg)); 308355714Skris } 308455714Skris 3085160814Ssimonvoid *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx) 308655714Skris { 308755714Skris return(CRYPTO_get_ex_data(&s->ex_data,idx)); 308855714Skris } 308955714Skris 309055714Skrisint ssl_ok(SSL *s) 309155714Skris { 309255714Skris return(1); 309355714Skris } 309455714Skris 3095160814SsimonX509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) 309655714Skris { 309755714Skris return(ctx->cert_store); 309855714Skris } 309955714Skris 310055714Skrisvoid SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store) 310155714Skris { 310255714Skris if (ctx->cert_store != NULL) 310355714Skris X509_STORE_free(ctx->cert_store); 310455714Skris ctx->cert_store=store; 310555714Skris } 310655714Skris 3107160814Ssimonint SSL_want(const SSL *s) 310855714Skris { 310955714Skris return(s->rwstate); 311055714Skris } 311155714Skris 311255714Skris/*! 311355714Skris * \brief Set the callback for generating temporary RSA keys. 311455714Skris * \param ctx the SSL context. 311555714Skris * \param cb the callback 311655714Skris */ 311755714Skris 3118109998Smarkm#ifndef OPENSSL_NO_RSA 311955714Skrisvoid SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl, 312055714Skris int is_export, 312155714Skris int keylength)) 312259191Skris { 3123160814Ssimon SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); 312459191Skris } 312555714Skris 312659191Skrisvoid SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl, 312759191Skris int is_export, 312859191Skris int keylength)) 312959191Skris { 3130160814Ssimon SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); 313159191Skris } 313255714Skris#endif 313355714Skris 313455714Skris#ifdef DOXYGEN 313555714Skris/*! 313655714Skris * \brief The RSA temporary key callback function. 313755714Skris * \param ssl the SSL session. 313855714Skris * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite. 313955714Skris * \param keylength if \c is_export is \c TRUE, then \c keylength is the size 314055714Skris * of the required key in bits. 314155714Skris * \return the temporary RSA key. 314255714Skris * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback 314355714Skris */ 314455714Skris 314555714SkrisRSA *cb(SSL *ssl,int is_export,int keylength) 314655714Skris {} 314755714Skris#endif 314855714Skris 314955714Skris/*! 315055714Skris * \brief Set the callback for generating temporary DH keys. 315155714Skris * \param ctx the SSL context. 315255714Skris * \param dh the callback 315355714Skris */ 315455714Skris 3155109998Smarkm#ifndef OPENSSL_NO_DH 315655714Skrisvoid SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export, 3157238405Sjkim int keylength)) 3158109998Smarkm { 3159160814Ssimon SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); 3160109998Smarkm } 316155714Skris 316255714Skrisvoid SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export, 3163238405Sjkim int keylength)) 3164109998Smarkm { 3165160814Ssimon SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); 3166109998Smarkm } 316755714Skris#endif 316855714Skris 3169160814Ssimon#ifndef OPENSSL_NO_ECDH 3170160814Ssimonvoid SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export, 3171238405Sjkim int keylength)) 3172160814Ssimon { 3173160814Ssimon SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh); 3174160814Ssimon } 3175109998Smarkm 3176160814Ssimonvoid SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export, 3177238405Sjkim int keylength)) 3178160814Ssimon { 3179160814Ssimon SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh); 3180160814Ssimon } 3181160814Ssimon#endif 3182160814Ssimon 3183238405Sjkim#ifndef OPENSSL_NO_PSK 3184238405Sjkimint SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) 3185238405Sjkim { 3186238405Sjkim if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) 3187238405Sjkim { 3188238405Sjkim SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG); 3189238405Sjkim return 0; 3190238405Sjkim } 3191238405Sjkim if (ctx->psk_identity_hint != NULL) 3192238405Sjkim OPENSSL_free(ctx->psk_identity_hint); 3193238405Sjkim if (identity_hint != NULL) 3194238405Sjkim { 3195238405Sjkim ctx->psk_identity_hint = BUF_strdup(identity_hint); 3196238405Sjkim if (ctx->psk_identity_hint == NULL) 3197238405Sjkim return 0; 3198238405Sjkim } 3199238405Sjkim else 3200238405Sjkim ctx->psk_identity_hint = NULL; 3201238405Sjkim return 1; 3202238405Sjkim } 3203160814Ssimon 3204238405Sjkimint SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) 3205238405Sjkim { 3206238405Sjkim if (s == NULL) 3207238405Sjkim return 0; 3208238405Sjkim 3209238405Sjkim if (s->session == NULL) 3210238405Sjkim return 1; /* session not created yet, ignored */ 3211238405Sjkim 3212238405Sjkim if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) 3213238405Sjkim { 3214238405Sjkim SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG); 3215238405Sjkim return 0; 3216238405Sjkim } 3217238405Sjkim if (s->session->psk_identity_hint != NULL) 3218238405Sjkim OPENSSL_free(s->session->psk_identity_hint); 3219238405Sjkim if (identity_hint != NULL) 3220238405Sjkim { 3221238405Sjkim s->session->psk_identity_hint = BUF_strdup(identity_hint); 3222238405Sjkim if (s->session->psk_identity_hint == NULL) 3223238405Sjkim return 0; 3224238405Sjkim } 3225238405Sjkim else 3226238405Sjkim s->session->psk_identity_hint = NULL; 3227238405Sjkim return 1; 3228238405Sjkim } 3229238405Sjkim 3230238405Sjkimconst char *SSL_get_psk_identity_hint(const SSL *s) 3231238405Sjkim { 3232238405Sjkim if (s == NULL || s->session == NULL) 3233238405Sjkim return NULL; 3234238405Sjkim return(s->session->psk_identity_hint); 3235238405Sjkim } 3236238405Sjkim 3237238405Sjkimconst char *SSL_get_psk_identity(const SSL *s) 3238238405Sjkim { 3239238405Sjkim if (s == NULL || s->session == NULL) 3240238405Sjkim return NULL; 3241238405Sjkim return(s->session->psk_identity); 3242238405Sjkim } 3243238405Sjkim 3244238405Sjkimvoid SSL_set_psk_client_callback(SSL *s, 3245238405Sjkim unsigned int (*cb)(SSL *ssl, const char *hint, 3246238405Sjkim char *identity, unsigned int max_identity_len, unsigned char *psk, 3247238405Sjkim unsigned int max_psk_len)) 3248238405Sjkim { 3249238405Sjkim s->psk_client_callback = cb; 3250238405Sjkim } 3251238405Sjkim 3252238405Sjkimvoid SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, 3253238405Sjkim unsigned int (*cb)(SSL *ssl, const char *hint, 3254238405Sjkim char *identity, unsigned int max_identity_len, unsigned char *psk, 3255238405Sjkim unsigned int max_psk_len)) 3256238405Sjkim { 3257238405Sjkim ctx->psk_client_callback = cb; 3258238405Sjkim } 3259238405Sjkim 3260238405Sjkimvoid SSL_set_psk_server_callback(SSL *s, 3261238405Sjkim unsigned int (*cb)(SSL *ssl, const char *identity, 3262238405Sjkim unsigned char *psk, unsigned int max_psk_len)) 3263238405Sjkim { 3264238405Sjkim s->psk_server_callback = cb; 3265238405Sjkim } 3266238405Sjkim 3267238405Sjkimvoid SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, 3268238405Sjkim unsigned int (*cb)(SSL *ssl, const char *identity, 3269238405Sjkim unsigned char *psk, unsigned int max_psk_len)) 3270238405Sjkim { 3271238405Sjkim ctx->psk_server_callback = cb; 3272238405Sjkim } 3273238405Sjkim#endif 3274238405Sjkim 3275109998Smarkmvoid 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)) 3276109998Smarkm { 3277160814Ssimon SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); 3278109998Smarkm } 3279109998Smarkmvoid 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)) 3280109998Smarkm { 3281160814Ssimon SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); 3282109998Smarkm } 3283109998Smarkm 3284238405Sjkim/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer 3285238405Sjkim * vairable, freeing EVP_MD_CTX previously stored in that variable, if 3286238405Sjkim * any. If EVP_MD pointer is passed, initializes ctx with this md 3287238405Sjkim * Returns newly allocated ctx; 3288238405Sjkim */ 3289109998Smarkm 3290238405SjkimEVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md) 3291238405Sjkim{ 3292238405Sjkim ssl_clear_hash_ctx(hash); 3293238405Sjkim *hash = EVP_MD_CTX_create(); 3294238405Sjkim if (md) EVP_DigestInit_ex(*hash,md,NULL); 3295238405Sjkim return *hash; 3296238405Sjkim} 3297238405Sjkimvoid ssl_clear_hash_ctx(EVP_MD_CTX **hash) 3298238405Sjkim{ 3299109998Smarkm 3300238405Sjkim if (*hash) EVP_MD_CTX_destroy(*hash); 3301238405Sjkim *hash=NULL; 3302238405Sjkim} 3303238405Sjkim 3304238405Sjkimvoid SSL_set_debug(SSL *s, int debug) 3305238405Sjkim { 3306238405Sjkim s->debug = debug; 3307238405Sjkim } 3308238405Sjkim 3309238405Sjkimint SSL_cache_hit(SSL *s) 3310238405Sjkim { 3311238405Sjkim return s->hit; 3312238405Sjkim } 3313238405Sjkim 3314109998Smarkm#if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16) 331555714Skris#include "../crypto/bio/bss_file.c" 331655714Skris#endif 331755714Skris 331855714SkrisIMPLEMENT_STACK_OF(SSL_CIPHER) 331955714SkrisIMPLEMENT_STACK_OF(SSL_COMP) 3320238405SjkimIMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, 3321238405Sjkim ssl_cipher_id); 3322