ssl_lib.c revision 267258
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) 38655714Skris { 38755714Skris if (s->cert != NULL) 38855714Skris ssl_cert_free(s->cert); 38955714Skris if (s->ctx != NULL) 39055714Skris SSL_CTX_free(s->ctx); /* decrement reference count */ 39168651Skris OPENSSL_free(s); 39255714Skris } 39355714Skris SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE); 39455714Skris return(NULL); 39555714Skris } 39655714Skris 39755714Skrisint SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx, 39855714Skris unsigned int sid_ctx_len) 39955714Skris { 400109998Smarkm if(sid_ctx_len > sizeof ctx->sid_ctx) 40155714Skris { 40255714Skris SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); 40355714Skris return 0; 40455714Skris } 40555714Skris ctx->sid_ctx_length=sid_ctx_len; 40655714Skris memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len); 40755714Skris 40855714Skris return 1; 40955714Skris } 41055714Skris 41155714Skrisint SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx, 41255714Skris unsigned int sid_ctx_len) 41355714Skris { 41455714Skris if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) 41555714Skris { 41655714Skris SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); 41755714Skris return 0; 41855714Skris } 41955714Skris ssl->sid_ctx_length=sid_ctx_len; 42055714Skris memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len); 42155714Skris 42255714Skris return 1; 42355714Skris } 42455714Skris 425109998Smarkmint SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) 426109998Smarkm { 427109998Smarkm CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); 428109998Smarkm ctx->generate_session_id = cb; 429109998Smarkm CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); 430109998Smarkm return 1; 43159191Skris } 432109998Smarkm 433109998Smarkmint SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) 434109998Smarkm { 435109998Smarkm CRYPTO_w_lock(CRYPTO_LOCK_SSL); 436109998Smarkm ssl->generate_session_id = cb; 437109998Smarkm CRYPTO_w_unlock(CRYPTO_LOCK_SSL); 43859191Skris return 1; 439109998Smarkm } 44059191Skris 441109998Smarkmint SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id, 442109998Smarkm unsigned int id_len) 443109998Smarkm { 444109998Smarkm /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how 445109998Smarkm * we can "construct" a session to give us the desired check - ie. to 446109998Smarkm * find if there's a session in the hash table that would conflict with 447109998Smarkm * any new session built out of this id/id_len and the ssl_version in 448109998Smarkm * use by this SSL. */ 449109998Smarkm SSL_SESSION r, *p; 450109998Smarkm 451109998Smarkm if(id_len > sizeof r.session_id) 45259191Skris return 0; 453109998Smarkm 454109998Smarkm r.ssl_version = ssl->version; 455109998Smarkm r.session_id_length = id_len; 456109998Smarkm memcpy(r.session_id, id, id_len); 457109998Smarkm /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a 458109998Smarkm * callback is calling us to check the uniqueness of a shorter ID, it 459109998Smarkm * must be compared as a padded-out ID because that is what it will be 460109998Smarkm * converted to when the callback has finished choosing it. */ 461109998Smarkm if((r.ssl_version == SSL2_VERSION) && 462109998Smarkm (id_len < SSL2_SSL_SESSION_ID_LENGTH)) 463109998Smarkm { 464109998Smarkm memset(r.session_id + id_len, 0, 465109998Smarkm SSL2_SSL_SESSION_ID_LENGTH - id_len); 466109998Smarkm r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH; 467109998Smarkm } 468109998Smarkm 469109998Smarkm CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); 470238405Sjkim p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r); 471109998Smarkm CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); 472109998Smarkm return (p != NULL); 47359191Skris } 474109998Smarkm 475109998Smarkmint SSL_CTX_set_purpose(SSL_CTX *s, int purpose) 476109998Smarkm { 477160814Ssimon return X509_VERIFY_PARAM_set_purpose(s->param, purpose); 478109998Smarkm } 479109998Smarkm 480109998Smarkmint SSL_set_purpose(SSL *s, int purpose) 481109998Smarkm { 482160814Ssimon return X509_VERIFY_PARAM_set_purpose(s->param, purpose); 483109998Smarkm } 484109998Smarkm 48559191Skrisint SSL_CTX_set_trust(SSL_CTX *s, int trust) 486109998Smarkm { 487160814Ssimon return X509_VERIFY_PARAM_set_trust(s->param, trust); 48859191Skris } 48959191Skris 49059191Skrisint SSL_set_trust(SSL *s, int trust) 491109998Smarkm { 492160814Ssimon return X509_VERIFY_PARAM_set_trust(s->param, trust); 49359191Skris } 49459191Skris 495238405Sjkimint SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) 496238405Sjkim { 497238405Sjkim return X509_VERIFY_PARAM_set1(ctx->param, vpm); 498238405Sjkim } 499238405Sjkim 500238405Sjkimint SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) 501238405Sjkim { 502238405Sjkim return X509_VERIFY_PARAM_set1(ssl->param, vpm); 503238405Sjkim } 504238405Sjkim 50555714Skrisvoid SSL_free(SSL *s) 50655714Skris { 50755714Skris int i; 50855714Skris 50955714Skris if(s == NULL) 51055714Skris return; 51155714Skris 51255714Skris i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL); 51355714Skris#ifdef REF_PRINT 51455714Skris REF_PRINT("SSL",s); 51555714Skris#endif 51655714Skris if (i > 0) return; 51755714Skris#ifdef REF_CHECK 51855714Skris if (i < 0) 51955714Skris { 52055714Skris fprintf(stderr,"SSL_free, bad reference count\n"); 52155714Skris abort(); /* ok */ 52255714Skris } 52355714Skris#endif 52455714Skris 525160814Ssimon if (s->param) 526160814Ssimon X509_VERIFY_PARAM_free(s->param); 527160814Ssimon 528109998Smarkm CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data); 52955714Skris 53055714Skris if (s->bbio != NULL) 53155714Skris { 53255714Skris /* If the buffering BIO is in place, pop it off */ 53355714Skris if (s->bbio == s->wbio) 53455714Skris { 53555714Skris s->wbio=BIO_pop(s->wbio); 53655714Skris } 53755714Skris BIO_free(s->bbio); 53855714Skris s->bbio=NULL; 53955714Skris } 54055714Skris if (s->rbio != NULL) 54155714Skris BIO_free_all(s->rbio); 54255714Skris if ((s->wbio != NULL) && (s->wbio != s->rbio)) 54355714Skris BIO_free_all(s->wbio); 54455714Skris 54555714Skris if (s->init_buf != NULL) BUF_MEM_free(s->init_buf); 54655714Skris 54755714Skris /* add extra stuff */ 54855714Skris if (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list); 54955714Skris if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id); 55055714Skris 55155714Skris /* Make the next call work :-) */ 55255714Skris if (s->session != NULL) 55355714Skris { 55455714Skris ssl_clear_bad_session(s); 55555714Skris SSL_SESSION_free(s->session); 55655714Skris } 55755714Skris 55855714Skris ssl_clear_cipher_ctx(s); 559238405Sjkim ssl_clear_hash_ctx(&s->read_hash); 560238405Sjkim ssl_clear_hash_ctx(&s->write_hash); 56155714Skris 56255714Skris if (s->cert != NULL) ssl_cert_free(s->cert); 56355714Skris /* Free up if allocated */ 56455714Skris 565194206Ssimon#ifndef OPENSSL_NO_TLSEXT 566194206Ssimon if (s->tlsext_hostname) 567194206Ssimon OPENSSL_free(s->tlsext_hostname); 568194206Ssimon if (s->initial_ctx) SSL_CTX_free(s->initial_ctx); 569238405Sjkim#ifndef OPENSSL_NO_EC 570238405Sjkim if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist); 571238405Sjkim if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist); 572238405Sjkim#endif /* OPENSSL_NO_EC */ 573238405Sjkim if (s->tlsext_opaque_prf_input) OPENSSL_free(s->tlsext_opaque_prf_input); 574194206Ssimon if (s->tlsext_ocsp_exts) 575194206Ssimon sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, 576194206Ssimon X509_EXTENSION_free); 577194206Ssimon if (s->tlsext_ocsp_ids) 578194206Ssimon sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free); 579194206Ssimon if (s->tlsext_ocsp_resp) 580194206Ssimon OPENSSL_free(s->tlsext_ocsp_resp); 581194206Ssimon#endif 582238405Sjkim 58355714Skris if (s->client_CA != NULL) 58455714Skris sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free); 58555714Skris 58655714Skris if (s->method != NULL) s->method->ssl_free(s); 58755714Skris 588205128Ssimon if (s->ctx) SSL_CTX_free(s->ctx); 589205128Ssimon 590120631Snectar#ifndef OPENSSL_NO_KRB5 591120631Snectar if (s->kssl_ctx != NULL) 592120631Snectar kssl_ctx_free(s->kssl_ctx); 593120631Snectar#endif /* OPENSSL_NO_KRB5 */ 594120631Snectar 595238405Sjkim#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) 596238405Sjkim if (s->next_proto_negotiated) 597238405Sjkim OPENSSL_free(s->next_proto_negotiated); 598238405Sjkim#endif 599238405Sjkim 600246772Sjkim#ifndef OPENSSL_NO_SRTP 601238405Sjkim if (s->srtp_profiles) 602238405Sjkim sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles); 603246772Sjkim#endif 604238405Sjkim 60568651Skris OPENSSL_free(s); 60655714Skris } 60755714Skris 60855714Skrisvoid SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio) 60955714Skris { 61055714Skris /* If the output buffering BIO is still in place, remove it 61155714Skris */ 61255714Skris if (s->bbio != NULL) 61355714Skris { 61455714Skris if (s->wbio == s->bbio) 61555714Skris { 61655714Skris s->wbio=s->wbio->next_bio; 61755714Skris s->bbio->next_bio=NULL; 61855714Skris } 61955714Skris } 62055714Skris if ((s->rbio != NULL) && (s->rbio != rbio)) 62155714Skris BIO_free_all(s->rbio); 62255714Skris if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio)) 62355714Skris BIO_free_all(s->wbio); 62455714Skris s->rbio=rbio; 62555714Skris s->wbio=wbio; 62655714Skris } 62755714Skris 628160814SsimonBIO *SSL_get_rbio(const SSL *s) 62955714Skris { return(s->rbio); } 63055714Skris 631160814SsimonBIO *SSL_get_wbio(const SSL *s) 63255714Skris { return(s->wbio); } 63355714Skris 634160814Ssimonint SSL_get_fd(const SSL *s) 63555714Skris { 63689837Skris return(SSL_get_rfd(s)); 63789837Skris } 63889837Skris 639160814Ssimonint SSL_get_rfd(const SSL *s) 64089837Skris { 64155714Skris int ret= -1; 64255714Skris BIO *b,*r; 64355714Skris 64455714Skris b=SSL_get_rbio(s); 64555714Skris r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR); 64655714Skris if (r != NULL) 64755714Skris BIO_get_fd(r,&ret); 64855714Skris return(ret); 64955714Skris } 65055714Skris 651160814Ssimonint SSL_get_wfd(const SSL *s) 65289837Skris { 65389837Skris int ret= -1; 65489837Skris BIO *b,*r; 65589837Skris 65689837Skris b=SSL_get_wbio(s); 65789837Skris r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR); 65889837Skris if (r != NULL) 65989837Skris BIO_get_fd(r,&ret); 66089837Skris return(ret); 66189837Skris } 66289837Skris 663109998Smarkm#ifndef OPENSSL_NO_SOCK 66455714Skrisint SSL_set_fd(SSL *s,int fd) 66555714Skris { 66655714Skris int ret=0; 66755714Skris BIO *bio=NULL; 66855714Skris 66955714Skris bio=BIO_new(BIO_s_socket()); 67055714Skris 67155714Skris if (bio == NULL) 67255714Skris { 67355714Skris SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB); 67455714Skris goto err; 67555714Skris } 67655714Skris BIO_set_fd(bio,fd,BIO_NOCLOSE); 67755714Skris SSL_set_bio(s,bio,bio); 67855714Skris ret=1; 67955714Skriserr: 68055714Skris return(ret); 68155714Skris } 68255714Skris 68355714Skrisint SSL_set_wfd(SSL *s,int fd) 68455714Skris { 68555714Skris int ret=0; 68655714Skris BIO *bio=NULL; 68755714Skris 68855714Skris if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET) 68955714Skris || ((int)BIO_get_fd(s->rbio,NULL) != fd)) 69055714Skris { 69155714Skris bio=BIO_new(BIO_s_socket()); 69255714Skris 69355714Skris if (bio == NULL) 69455714Skris { SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; } 69555714Skris BIO_set_fd(bio,fd,BIO_NOCLOSE); 69655714Skris SSL_set_bio(s,SSL_get_rbio(s),bio); 69755714Skris } 69855714Skris else 69955714Skris SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s)); 70055714Skris ret=1; 70155714Skriserr: 70255714Skris return(ret); 70355714Skris } 70455714Skris 70555714Skrisint SSL_set_rfd(SSL *s,int fd) 70655714Skris { 70755714Skris int ret=0; 70855714Skris BIO *bio=NULL; 70955714Skris 71055714Skris if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET) 71155714Skris || ((int)BIO_get_fd(s->wbio,NULL) != fd)) 71255714Skris { 71355714Skris bio=BIO_new(BIO_s_socket()); 71455714Skris 71555714Skris if (bio == NULL) 71655714Skris { 71755714Skris SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB); 71855714Skris goto err; 71955714Skris } 72055714Skris BIO_set_fd(bio,fd,BIO_NOCLOSE); 72155714Skris SSL_set_bio(s,bio,SSL_get_wbio(s)); 72255714Skris } 72355714Skris else 72455714Skris SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s)); 72555714Skris ret=1; 72655714Skriserr: 72755714Skris return(ret); 72855714Skris } 72955714Skris#endif 73055714Skris 73159191Skris 73259191Skris/* return length of latest Finished message we sent, copy to 'buf' */ 733160814Ssimonsize_t SSL_get_finished(const SSL *s, void *buf, size_t count) 73459191Skris { 73559191Skris size_t ret = 0; 73659191Skris 73759191Skris if (s->s3 != NULL) 73859191Skris { 73959191Skris ret = s->s3->tmp.finish_md_len; 74059191Skris if (count > ret) 74159191Skris count = ret; 74259191Skris memcpy(buf, s->s3->tmp.finish_md, count); 74359191Skris } 74459191Skris return ret; 74559191Skris } 74659191Skris 74759191Skris/* return length of latest Finished message we expected, copy to 'buf' */ 748160814Ssimonsize_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) 74959191Skris { 75059191Skris size_t ret = 0; 75159191Skris 75259191Skris if (s->s3 != NULL) 75359191Skris { 75459191Skris ret = s->s3->tmp.peer_finish_md_len; 75559191Skris if (count > ret) 75659191Skris count = ret; 75759191Skris memcpy(buf, s->s3->tmp.peer_finish_md, count); 75859191Skris } 75959191Skris return ret; 76059191Skris } 76159191Skris 76259191Skris 763160814Ssimonint SSL_get_verify_mode(const SSL *s) 76455714Skris { 76555714Skris return(s->verify_mode); 76655714Skris } 76755714Skris 768160814Ssimonint SSL_get_verify_depth(const SSL *s) 76955714Skris { 770160814Ssimon return X509_VERIFY_PARAM_get_depth(s->param); 77155714Skris } 77255714Skris 773160814Ssimonint (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *) 77455714Skris { 77555714Skris return(s->verify_callback); 77655714Skris } 77755714Skris 778160814Ssimonint SSL_CTX_get_verify_mode(const SSL_CTX *ctx) 77955714Skris { 78055714Skris return(ctx->verify_mode); 78155714Skris } 78255714Skris 783160814Ssimonint SSL_CTX_get_verify_depth(const SSL_CTX *ctx) 78455714Skris { 785160814Ssimon return X509_VERIFY_PARAM_get_depth(ctx->param); 78655714Skris } 78755714Skris 788160814Ssimonint (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *) 78955714Skris { 79055714Skris return(ctx->default_verify_callback); 79155714Skris } 79255714Skris 79355714Skrisvoid SSL_set_verify(SSL *s,int mode, 79455714Skris int (*callback)(int ok,X509_STORE_CTX *ctx)) 79555714Skris { 79655714Skris s->verify_mode=mode; 79755714Skris if (callback != NULL) 79855714Skris s->verify_callback=callback; 79955714Skris } 80055714Skris 80155714Skrisvoid SSL_set_verify_depth(SSL *s,int depth) 80255714Skris { 803160814Ssimon X509_VERIFY_PARAM_set_depth(s->param, depth); 80455714Skris } 80555714Skris 80655714Skrisvoid SSL_set_read_ahead(SSL *s,int yes) 80755714Skris { 80855714Skris s->read_ahead=yes; 80955714Skris } 81055714Skris 811160814Ssimonint SSL_get_read_ahead(const SSL *s) 81255714Skris { 81355714Skris return(s->read_ahead); 81455714Skris } 81555714Skris 816160814Ssimonint SSL_pending(const SSL *s) 81755714Skris { 818109998Smarkm /* SSL_pending cannot work properly if read-ahead is enabled 819109998Smarkm * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), 820109998Smarkm * and it is impossible to fix since SSL_pending cannot report 821109998Smarkm * errors that may be observed while scanning the new data. 822109998Smarkm * (Note that SSL_pending() is often used as a boolean value, 823109998Smarkm * so we'd better not return -1.) 824109998Smarkm */ 82555714Skris return(s->method->ssl_pending(s)); 82655714Skris } 82755714Skris 828160814SsimonX509 *SSL_get_peer_certificate(const SSL *s) 82955714Skris { 83055714Skris X509 *r; 83155714Skris 83255714Skris if ((s == NULL) || (s->session == NULL)) 83355714Skris r=NULL; 83455714Skris else 83555714Skris r=s->session->peer; 83655714Skris 83755714Skris if (r == NULL) return(r); 83855714Skris 83955714Skris CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509); 84055714Skris 84155714Skris return(r); 84255714Skris } 84355714Skris 844160814SsimonSTACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s) 84555714Skris { 84655714Skris STACK_OF(X509) *r; 84755714Skris 84855714Skris if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL)) 84955714Skris r=NULL; 85055714Skris else 85155714Skris r=s->session->sess_cert->cert_chain; 85255714Skris 85359191Skris /* If we are a client, cert_chain includes the peer's own 85459191Skris * certificate; if we are a server, it does not. */ 85559191Skris 85655714Skris return(r); 85755714Skris } 85855714Skris 85955714Skris/* Now in theory, since the calling process own 't' it should be safe to 86055714Skris * modify. We need to be able to read f without being hassled */ 861160814Ssimonvoid SSL_copy_session_id(SSL *t,const SSL *f) 86255714Skris { 86355714Skris CERT *tmp; 86455714Skris 86555714Skris /* Do we need to to SSL locking? */ 86655714Skris SSL_set_session(t,SSL_get_session(f)); 86755714Skris 86855714Skris /* what if we are setup as SSLv2 but want to talk SSLv3 or 86955714Skris * vice-versa */ 87055714Skris if (t->method != f->method) 87155714Skris { 87255714Skris t->method->ssl_free(t); /* cleanup current */ 87355714Skris t->method=f->method; /* change method */ 87455714Skris t->method->ssl_new(t); /* setup new */ 87555714Skris } 87655714Skris 87755714Skris tmp=t->cert; 87855714Skris if (f->cert != NULL) 87955714Skris { 88055714Skris CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT); 88155714Skris t->cert=f->cert; 88255714Skris } 88355714Skris else 88455714Skris t->cert=NULL; 88555714Skris if (tmp != NULL) ssl_cert_free(tmp); 88655714Skris SSL_set_session_id_context(t,f->sid_ctx,f->sid_ctx_length); 88755714Skris } 88855714Skris 88955714Skris/* Fix this so it checks all the valid key/cert options */ 890160814Ssimonint SSL_CTX_check_private_key(const SSL_CTX *ctx) 89155714Skris { 89255714Skris if ( (ctx == NULL) || 89355714Skris (ctx->cert == NULL) || 89455714Skris (ctx->cert->key->x509 == NULL)) 89555714Skris { 89655714Skris SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED); 89755714Skris return(0); 89855714Skris } 89955714Skris if (ctx->cert->key->privatekey == NULL) 90055714Skris { 90155714Skris SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED); 90255714Skris return(0); 90355714Skris } 90455714Skris return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey)); 90555714Skris } 90655714Skris 90755714Skris/* Fix this function so that it takes an optional type parameter */ 908160814Ssimonint SSL_check_private_key(const SSL *ssl) 90955714Skris { 91055714Skris if (ssl == NULL) 91155714Skris { 91255714Skris SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER); 91355714Skris return(0); 91455714Skris } 91555714Skris if (ssl->cert == NULL) 91655714Skris { 917238405Sjkim SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED); 91855714Skris return 0; 91955714Skris } 92055714Skris if (ssl->cert->key->x509 == NULL) 92155714Skris { 92255714Skris SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED); 92355714Skris return(0); 92455714Skris } 92555714Skris if (ssl->cert->key->privatekey == NULL) 92655714Skris { 92755714Skris SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED); 92855714Skris return(0); 92955714Skris } 93055714Skris return(X509_check_private_key(ssl->cert->key->x509, 93155714Skris ssl->cert->key->privatekey)); 93255714Skris } 93355714Skris 93455714Skrisint SSL_accept(SSL *s) 93555714Skris { 93655714Skris if (s->handshake_func == 0) 93755714Skris /* Not properly initialized yet */ 93855714Skris SSL_set_accept_state(s); 93955714Skris 94055714Skris return(s->method->ssl_accept(s)); 94155714Skris } 94255714Skris 94355714Skrisint SSL_connect(SSL *s) 94455714Skris { 94555714Skris if (s->handshake_func == 0) 94655714Skris /* Not properly initialized yet */ 94755714Skris SSL_set_connect_state(s); 94855714Skris 94955714Skris return(s->method->ssl_connect(s)); 95055714Skris } 95155714Skris 952160814Ssimonlong SSL_get_default_timeout(const SSL *s) 95355714Skris { 95455714Skris return(s->method->get_timeout()); 95555714Skris } 95655714Skris 95776866Skrisint SSL_read(SSL *s,void *buf,int num) 95855714Skris { 95955714Skris if (s->handshake_func == 0) 96055714Skris { 96155714Skris SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED); 96255714Skris return -1; 96355714Skris } 96455714Skris 96555714Skris if (s->shutdown & SSL_RECEIVED_SHUTDOWN) 96655714Skris { 96755714Skris s->rwstate=SSL_NOTHING; 96855714Skris return(0); 96955714Skris } 97055714Skris return(s->method->ssl_read(s,buf,num)); 97155714Skris } 97255714Skris 97376866Skrisint SSL_peek(SSL *s,void *buf,int num) 97455714Skris { 97576866Skris if (s->handshake_func == 0) 97676866Skris { 977160814Ssimon SSLerr(SSL_F_SSL_PEEK, SSL_R_UNINITIALIZED); 97876866Skris return -1; 97976866Skris } 98076866Skris 98155714Skris if (s->shutdown & SSL_RECEIVED_SHUTDOWN) 98255714Skris { 98355714Skris return(0); 98455714Skris } 98555714Skris return(s->method->ssl_peek(s,buf,num)); 98655714Skris } 98755714Skris 98876866Skrisint SSL_write(SSL *s,const void *buf,int num) 98955714Skris { 99055714Skris if (s->handshake_func == 0) 99155714Skris { 99255714Skris SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED); 99355714Skris return -1; 99455714Skris } 99555714Skris 99655714Skris if (s->shutdown & SSL_SENT_SHUTDOWN) 99755714Skris { 99855714Skris s->rwstate=SSL_NOTHING; 99955714Skris SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN); 100055714Skris return(-1); 100155714Skris } 100255714Skris return(s->method->ssl_write(s,buf,num)); 100355714Skris } 100455714Skris 100555714Skrisint SSL_shutdown(SSL *s) 100655714Skris { 100755714Skris /* Note that this function behaves differently from what one might 100855714Skris * expect. Return values are 0 for no success (yet), 100955714Skris * 1 for success; but calling it once is usually not enough, 101055714Skris * even if blocking I/O is used (see ssl3_shutdown). 101155714Skris */ 101255714Skris 101355714Skris if (s->handshake_func == 0) 101455714Skris { 101555714Skris SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED); 101655714Skris return -1; 101755714Skris } 101855714Skris 101955714Skris if ((s != NULL) && !SSL_in_init(s)) 102055714Skris return(s->method->ssl_shutdown(s)); 102155714Skris else 102255714Skris return(1); 102355714Skris } 102455714Skris 102555714Skrisint SSL_renegotiate(SSL *s) 102655714Skris { 1027238405Sjkim if (s->renegotiate == 0) 1028238405Sjkim s->renegotiate=1; 1029238405Sjkim 1030238405Sjkim s->new_session=1; 1031238405Sjkim 103255714Skris return(s->method->ssl_renegotiate(s)); 103355714Skris } 103455714Skris 1035238405Sjkimint SSL_renegotiate_abbreviated(SSL *s) 1036238405Sjkim { 1037238405Sjkim if (s->renegotiate == 0) 1038238405Sjkim s->renegotiate=1; 1039238405Sjkim 1040238405Sjkim s->new_session=0; 1041238405Sjkim 1042238405Sjkim return(s->method->ssl_renegotiate(s)); 1043238405Sjkim } 1044238405Sjkim 1045109998Smarkmint SSL_renegotiate_pending(SSL *s) 104655714Skris { 1047109998Smarkm /* becomes true when negotiation is requested; 1048109998Smarkm * false again once a handshake has finished */ 1049238405Sjkim return (s->renegotiate != 0); 1050109998Smarkm } 1051109998Smarkm 1052109998Smarkmlong SSL_ctrl(SSL *s,int cmd,long larg,void *parg) 1053109998Smarkm { 105455714Skris long l; 105555714Skris 105655714Skris switch (cmd) 105755714Skris { 105855714Skris case SSL_CTRL_GET_READ_AHEAD: 105955714Skris return(s->read_ahead); 106055714Skris case SSL_CTRL_SET_READ_AHEAD: 106155714Skris l=s->read_ahead; 106255714Skris s->read_ahead=larg; 106355714Skris return(l); 1064109998Smarkm 1065109998Smarkm case SSL_CTRL_SET_MSG_CALLBACK_ARG: 1066109998Smarkm s->msg_callback_arg = parg; 1067109998Smarkm return 1; 1068109998Smarkm 106955714Skris case SSL_CTRL_OPTIONS: 107055714Skris return(s->options|=larg); 1071205128Ssimon case SSL_CTRL_CLEAR_OPTIONS: 1072205128Ssimon return(s->options&=~larg); 107355714Skris case SSL_CTRL_MODE: 107455714Skris return(s->mode|=larg); 1075205128Ssimon case SSL_CTRL_CLEAR_MODE: 1076205128Ssimon return(s->mode &=~larg); 1077109998Smarkm case SSL_CTRL_GET_MAX_CERT_LIST: 1078109998Smarkm return(s->max_cert_list); 1079109998Smarkm case SSL_CTRL_SET_MAX_CERT_LIST: 1080109998Smarkm l=s->max_cert_list; 1081109998Smarkm s->max_cert_list=larg; 1082109998Smarkm return(l); 1083160814Ssimon case SSL_CTRL_SET_MTU: 1084237657Sjkim#ifndef OPENSSL_NO_DTLS1 1085237657Sjkim if (larg < (long)dtls1_min_mtu()) 1086237657Sjkim return 0; 1087237657Sjkim#endif 1088237657Sjkim 1089205128Ssimon if (SSL_version(s) == DTLS1_VERSION || 1090205128Ssimon SSL_version(s) == DTLS1_BAD_VER) 1091160814Ssimon { 1092160814Ssimon s->d1->mtu = larg; 1093160814Ssimon return larg; 1094160814Ssimon } 1095160814Ssimon return 0; 1096238405Sjkim case SSL_CTRL_SET_MAX_SEND_FRAGMENT: 1097238405Sjkim if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) 1098238405Sjkim return 0; 1099238405Sjkim s->max_send_fragment = larg; 1100238405Sjkim return 1; 1101205128Ssimon case SSL_CTRL_GET_RI_SUPPORT: 1102205128Ssimon if (s->s3) 1103205128Ssimon return s->s3->send_connection_binding; 1104205128Ssimon else return 0; 110555714Skris default: 110655714Skris return(s->method->ssl_ctrl(s,cmd,larg,parg)); 110755714Skris } 110855714Skris } 110955714Skris 1110160814Ssimonlong SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) 111159191Skris { 111259191Skris switch(cmd) 111359191Skris { 1114109998Smarkm case SSL_CTRL_SET_MSG_CALLBACK: 1115109998Smarkm s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp); 1116109998Smarkm return 1; 1117109998Smarkm 111859191Skris default: 111959191Skris return(s->method->ssl_callback_ctrl(s,cmd,fp)); 112059191Skris } 112159191Skris } 112259191Skris 1123238405SjkimLHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) 112459191Skris { 112559191Skris return ctx->sessions; 112659191Skris } 112759191Skris 1128109998Smarkmlong SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg) 112955714Skris { 113055714Skris long l; 113155714Skris 113255714Skris switch (cmd) 113355714Skris { 113455714Skris case SSL_CTRL_GET_READ_AHEAD: 113555714Skris return(ctx->read_ahead); 113655714Skris case SSL_CTRL_SET_READ_AHEAD: 113755714Skris l=ctx->read_ahead; 113855714Skris ctx->read_ahead=larg; 113955714Skris return(l); 1140109998Smarkm 1141109998Smarkm case SSL_CTRL_SET_MSG_CALLBACK_ARG: 1142109998Smarkm ctx->msg_callback_arg = parg; 1143109998Smarkm return 1; 114455714Skris 1145109998Smarkm case SSL_CTRL_GET_MAX_CERT_LIST: 1146109998Smarkm return(ctx->max_cert_list); 1147109998Smarkm case SSL_CTRL_SET_MAX_CERT_LIST: 1148109998Smarkm l=ctx->max_cert_list; 1149109998Smarkm ctx->max_cert_list=larg; 1150109998Smarkm return(l); 1151109998Smarkm 115255714Skris case SSL_CTRL_SET_SESS_CACHE_SIZE: 115355714Skris l=ctx->session_cache_size; 115455714Skris ctx->session_cache_size=larg; 115555714Skris return(l); 115655714Skris case SSL_CTRL_GET_SESS_CACHE_SIZE: 115755714Skris return(ctx->session_cache_size); 115855714Skris case SSL_CTRL_SET_SESS_CACHE_MODE: 115955714Skris l=ctx->session_cache_mode; 116055714Skris ctx->session_cache_mode=larg; 116155714Skris return(l); 116255714Skris case SSL_CTRL_GET_SESS_CACHE_MODE: 116355714Skris return(ctx->session_cache_mode); 116455714Skris 116555714Skris case SSL_CTRL_SESS_NUMBER: 1166238405Sjkim return(lh_SSL_SESSION_num_items(ctx->sessions)); 116755714Skris case SSL_CTRL_SESS_CONNECT: 116855714Skris return(ctx->stats.sess_connect); 116955714Skris case SSL_CTRL_SESS_CONNECT_GOOD: 117055714Skris return(ctx->stats.sess_connect_good); 117155714Skris case SSL_CTRL_SESS_CONNECT_RENEGOTIATE: 117255714Skris return(ctx->stats.sess_connect_renegotiate); 117355714Skris case SSL_CTRL_SESS_ACCEPT: 117455714Skris return(ctx->stats.sess_accept); 117555714Skris case SSL_CTRL_SESS_ACCEPT_GOOD: 117655714Skris return(ctx->stats.sess_accept_good); 117755714Skris case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE: 117855714Skris return(ctx->stats.sess_accept_renegotiate); 117955714Skris case SSL_CTRL_SESS_HIT: 118055714Skris return(ctx->stats.sess_hit); 118155714Skris case SSL_CTRL_SESS_CB_HIT: 118255714Skris return(ctx->stats.sess_cb_hit); 118355714Skris case SSL_CTRL_SESS_MISSES: 118455714Skris return(ctx->stats.sess_miss); 118555714Skris case SSL_CTRL_SESS_TIMEOUTS: 118655714Skris return(ctx->stats.sess_timeout); 118755714Skris case SSL_CTRL_SESS_CACHE_FULL: 118855714Skris return(ctx->stats.sess_cache_full); 118955714Skris case SSL_CTRL_OPTIONS: 119055714Skris return(ctx->options|=larg); 1191205128Ssimon case SSL_CTRL_CLEAR_OPTIONS: 1192205128Ssimon return(ctx->options&=~larg); 119355714Skris case SSL_CTRL_MODE: 119455714Skris return(ctx->mode|=larg); 1195205128Ssimon case SSL_CTRL_CLEAR_MODE: 1196205128Ssimon return(ctx->mode&=~larg); 1197238405Sjkim case SSL_CTRL_SET_MAX_SEND_FRAGMENT: 1198238405Sjkim if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) 1199238405Sjkim return 0; 1200238405Sjkim ctx->max_send_fragment = larg; 1201238405Sjkim return 1; 120255714Skris default: 120355714Skris return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg)); 120455714Skris } 120555714Skris } 120655714Skris 1207160814Ssimonlong SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) 120859191Skris { 120959191Skris switch(cmd) 121059191Skris { 1211109998Smarkm case SSL_CTRL_SET_MSG_CALLBACK: 1212109998Smarkm ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp); 1213109998Smarkm return 1; 1214109998Smarkm 121559191Skris default: 121659191Skris return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp)); 121759191Skris } 121859191Skris } 121959191Skris 122068651Skrisint ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b) 122155714Skris { 122255714Skris long l; 122355714Skris 122455714Skris l=a->id-b->id; 122555714Skris if (l == 0L) 122655714Skris return(0); 122755714Skris else 122855714Skris return((l > 0)?1:-1); 122955714Skris } 123055714Skris 123168651Skrisint ssl_cipher_ptr_id_cmp(const SSL_CIPHER * const *ap, 123268651Skris const SSL_CIPHER * const *bp) 123355714Skris { 123455714Skris long l; 123555714Skris 123655714Skris l=(*ap)->id-(*bp)->id; 123755714Skris if (l == 0L) 123855714Skris return(0); 123955714Skris else 124055714Skris return((l > 0)?1:-1); 124155714Skris } 124255714Skris 124355714Skris/** return a STACK of the ciphers available for the SSL and in order of 124455714Skris * preference */ 1245160814SsimonSTACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) 124655714Skris { 1247111147Snectar if (s != NULL) 124855714Skris { 1249111147Snectar if (s->cipher_list != NULL) 1250111147Snectar { 1251111147Snectar return(s->cipher_list); 1252111147Snectar } 1253111147Snectar else if ((s->ctx != NULL) && 1254111147Snectar (s->ctx->cipher_list != NULL)) 1255111147Snectar { 1256111147Snectar return(s->ctx->cipher_list); 1257111147Snectar } 125855714Skris } 125955714Skris return(NULL); 126055714Skris } 126155714Skris 126255714Skris/** return a STACK of the ciphers available for the SSL and in order of 126355714Skris * algorithm id */ 126455714SkrisSTACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) 126555714Skris { 1266111147Snectar if (s != NULL) 126755714Skris { 1268111147Snectar if (s->cipher_list_by_id != NULL) 1269111147Snectar { 1270111147Snectar return(s->cipher_list_by_id); 1271111147Snectar } 1272111147Snectar else if ((s->ctx != NULL) && 1273111147Snectar (s->ctx->cipher_list_by_id != NULL)) 1274111147Snectar { 1275111147Snectar return(s->ctx->cipher_list_by_id); 1276111147Snectar } 127755714Skris } 127855714Skris return(NULL); 127955714Skris } 128055714Skris 128155714Skris/** The old interface to get the same thing as SSL_get_ciphers() */ 1282160814Ssimonconst char *SSL_get_cipher_list(const SSL *s,int n) 128355714Skris { 128455714Skris SSL_CIPHER *c; 128555714Skris STACK_OF(SSL_CIPHER) *sk; 128655714Skris 128755714Skris if (s == NULL) return(NULL); 128855714Skris sk=SSL_get_ciphers(s); 128955714Skris if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n)) 129055714Skris return(NULL); 129155714Skris c=sk_SSL_CIPHER_value(sk,n); 129255714Skris if (c == NULL) return(NULL); 129355714Skris return(c->name); 129455714Skris } 129555714Skris 129659191Skris/** specify the ciphers to be used by default by the SSL_CTX */ 129759191Skrisint SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) 129855714Skris { 129955714Skris STACK_OF(SSL_CIPHER) *sk; 130055714Skris 130155714Skris sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list, 130255714Skris &ctx->cipher_list_by_id,str); 1303160814Ssimon /* ssl_create_cipher_list may return an empty stack if it 1304160814Ssimon * was unable to find a cipher matching the given rule string 1305160814Ssimon * (for example if the rule string specifies a cipher which 1306238405Sjkim * has been disabled). This is not an error as far as 1307238405Sjkim * ssl_create_cipher_list is concerned, and hence 1308160814Ssimon * ctx->cipher_list and ctx->cipher_list_by_id has been 1309160814Ssimon * updated. */ 1310160814Ssimon if (sk == NULL) 1311160814Ssimon return 0; 1312160814Ssimon else if (sk_SSL_CIPHER_num(sk) == 0) 1313160814Ssimon { 1314160814Ssimon SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH); 1315160814Ssimon return 0; 1316160814Ssimon } 1317160814Ssimon return 1; 131855714Skris } 131955714Skris 132055714Skris/** specify the ciphers to be used by the SSL */ 132159191Skrisint SSL_set_cipher_list(SSL *s,const char *str) 132255714Skris { 132355714Skris STACK_OF(SSL_CIPHER) *sk; 132455714Skris 132555714Skris sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list, 132655714Skris &s->cipher_list_by_id,str); 1327160814Ssimon /* see comment in SSL_CTX_set_cipher_list */ 1328160814Ssimon if (sk == NULL) 1329160814Ssimon return 0; 1330160814Ssimon else if (sk_SSL_CIPHER_num(sk) == 0) 1331160814Ssimon { 1332160814Ssimon SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH); 1333160814Ssimon return 0; 1334160814Ssimon } 1335160814Ssimon return 1; 133655714Skris } 133755714Skris 133855714Skris/* works well for SSLv2, not so good for SSLv3 */ 1339160814Ssimonchar *SSL_get_shared_ciphers(const SSL *s,char *buf,int len) 134055714Skris { 134155714Skris char *p; 134255714Skris STACK_OF(SSL_CIPHER) *sk; 134355714Skris SSL_CIPHER *c; 134455714Skris int i; 134555714Skris 134655714Skris if ((s->session == NULL) || (s->session->ciphers == NULL) || 134755714Skris (len < 2)) 134855714Skris return(NULL); 134955714Skris 135055714Skris p=buf; 135155714Skris sk=s->session->ciphers; 1352267258Sjkim 1353267258Sjkim if (sk_SSL_CIPHER_num(sk) == 0) 1354267258Sjkim return NULL; 1355267258Sjkim 135655714Skris for (i=0; i<sk_SSL_CIPHER_num(sk); i++) 135755714Skris { 1358172429Ssimon int n; 1359172429Ssimon 136055714Skris c=sk_SSL_CIPHER_value(sk,i); 1361172429Ssimon n=strlen(c->name); 1362172429Ssimon if (n+1 > len) 136355714Skris { 1364172429Ssimon if (p != buf) 1365172429Ssimon --p; 1366172429Ssimon *p='\0'; 1367172429Ssimon return buf; 136855714Skris } 1369172429Ssimon strcpy(p,c->name); 1370172429Ssimon p+=n; 137155714Skris *(p++)=':'; 1372172429Ssimon len-=n+1; 137355714Skris } 137455714Skris p[-1]='\0'; 137555714Skris return(buf); 137655714Skris } 137755714Skris 1378160814Ssimonint ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p, 1379238405Sjkim int (*put_cb)(const SSL_CIPHER *, unsigned char *)) 138055714Skris { 138155714Skris int i,j=0; 138255714Skris SSL_CIPHER *c; 138355714Skris unsigned char *q; 1384109998Smarkm#ifndef OPENSSL_NO_KRB5 1385238405Sjkim int nokrb5 = !kssl_tgt_is_available(s->kssl_ctx); 1386109998Smarkm#endif /* OPENSSL_NO_KRB5 */ 138755714Skris 138855714Skris if (sk == NULL) return(0); 138955714Skris q=p; 139055714Skris 139155714Skris for (i=0; i<sk_SSL_CIPHER_num(sk); i++) 139255714Skris { 139355714Skris c=sk_SSL_CIPHER_value(sk,i); 1394238405Sjkim /* Skip TLS v1.2 only ciphersuites if lower than v1.2 */ 1395238405Sjkim if ((c->algorithm_ssl & SSL_TLSV1_2) && 1396238405Sjkim (TLS1_get_client_version(s) < TLS1_2_VERSION)) 1397238405Sjkim continue; 1398109998Smarkm#ifndef OPENSSL_NO_KRB5 1399238405Sjkim if (((c->algorithm_mkey & SSL_kKRB5) || (c->algorithm_auth & SSL_aKRB5)) && 1400238405Sjkim nokrb5) 1401238405Sjkim continue; 1402238405Sjkim#endif /* OPENSSL_NO_KRB5 */ 1403238405Sjkim#ifndef OPENSSL_NO_PSK 1404238405Sjkim /* with PSK there must be client callback set */ 1405238405Sjkim if (((c->algorithm_mkey & SSL_kPSK) || (c->algorithm_auth & SSL_aPSK)) && 1406238405Sjkim s->psk_client_callback == NULL) 1407238405Sjkim continue; 1408238405Sjkim#endif /* OPENSSL_NO_PSK */ 1409160814Ssimon j = put_cb ? put_cb(c,p) : ssl_put_cipher_by_char(s,c,p); 141055714Skris p+=j; 141155714Skris } 1412205128Ssimon /* If p == q, no ciphers and caller indicates an error. Otherwise 1413205128Ssimon * add SCSV if not renegotiating. 1414205128Ssimon */ 1415238405Sjkim if (p != q && !s->renegotiate) 1416205128Ssimon { 1417205128Ssimon static SSL_CIPHER scsv = 1418205128Ssimon { 1419238405Sjkim 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 1420205128Ssimon }; 1421205128Ssimon j = put_cb ? put_cb(&scsv,p) : ssl_put_cipher_by_char(s,&scsv,p); 1422205128Ssimon p+=j; 1423205128Ssimon#ifdef OPENSSL_RI_DEBUG 1424205128Ssimon fprintf(stderr, "SCSV sent by client\n"); 1425205128Ssimon#endif 1426205128Ssimon } 1427205128Ssimon 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; 1437205128Ssimon if (s->s3) 1438205128Ssimon s->s3->send_connection_binding = 0; 143955714Skris 144055714Skris n=ssl_put_cipher_by_char(s,NULL,NULL); 144155714Skris if ((num%n) != 0) 144255714Skris { 144355714Skris SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); 144455714Skris return(NULL); 144555714Skris } 144655714Skris if ((skp == NULL) || (*skp == NULL)) 144768651Skris sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */ 144855714Skris else 144955714Skris { 145055714Skris sk= *skp; 145155714Skris sk_SSL_CIPHER_zero(sk); 145255714Skris } 145355714Skris 145455714Skris for (i=0; i<num; i+=n) 145555714Skris { 1456205128Ssimon /* Check for SCSV */ 1457205128Ssimon if (s->s3 && (n != 3 || !p[0]) && 1458205128Ssimon (p[n-2] == ((SSL3_CK_SCSV >> 8) & 0xff)) && 1459205128Ssimon (p[n-1] == (SSL3_CK_SCSV & 0xff))) 1460205128Ssimon { 1461205128Ssimon /* SCSV fatal if renegotiating */ 1462238405Sjkim if (s->renegotiate) 1463205128Ssimon { 1464205128Ssimon SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING); 1465205128Ssimon ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); 1466205128Ssimon goto err; 1467205128Ssimon } 1468205128Ssimon s->s3->send_connection_binding = 1; 1469205128Ssimon p += n; 1470205128Ssimon#ifdef OPENSSL_RI_DEBUG 1471205128Ssimon fprintf(stderr, "SCSV received by server\n"); 1472205128Ssimon#endif 1473205128Ssimon continue; 1474205128Ssimon } 1475205128Ssimon 147655714Skris c=ssl_get_cipher_by_char(s,p); 147755714Skris p+=n; 147855714Skris if (c != NULL) 147955714Skris { 148055714Skris if (!sk_SSL_CIPHER_push(sk,c)) 148155714Skris { 148255714Skris SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE); 148355714Skris goto err; 148455714Skris } 148555714Skris } 148655714Skris } 148755714Skris 148855714Skris if (skp != NULL) 148955714Skris *skp=sk; 149055714Skris return(sk); 149155714Skriserr: 149255714Skris if ((skp == NULL) || (*skp == NULL)) 149355714Skris sk_SSL_CIPHER_free(sk); 149455714Skris return(NULL); 149555714Skris } 149655714Skris 1497238405Sjkim 1498194206Ssimon#ifndef OPENSSL_NO_TLSEXT 1499194206Ssimon/** return a servername extension value if provided in Client Hello, or NULL. 1500194206Ssimon * So far, only host_name types are defined (RFC 3546). 1501194206Ssimon */ 1502194206Ssimon 1503194206Ssimonconst char *SSL_get_servername(const SSL *s, const int type) 1504194206Ssimon { 1505194206Ssimon if (type != TLSEXT_NAMETYPE_host_name) 1506194206Ssimon return NULL; 1507194206Ssimon 1508194206Ssimon return s->session && !s->tlsext_hostname ? 1509194206Ssimon s->session->tlsext_hostname : 1510194206Ssimon s->tlsext_hostname; 1511194206Ssimon } 1512194206Ssimon 1513194206Ssimonint SSL_get_servername_type(const SSL *s) 1514194206Ssimon { 1515194206Ssimon if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) 1516194206Ssimon return TLSEXT_NAMETYPE_host_name; 1517194206Ssimon return -1; 1518194206Ssimon } 1519238405Sjkim 1520238405Sjkim# ifndef OPENSSL_NO_NEXTPROTONEG 1521238405Sjkim/* SSL_select_next_proto implements the standard protocol selection. It is 1522238405Sjkim * expected that this function is called from the callback set by 1523238405Sjkim * SSL_CTX_set_next_proto_select_cb. 1524238405Sjkim * 1525238405Sjkim * The protocol data is assumed to be a vector of 8-bit, length prefixed byte 1526238405Sjkim * strings. The length byte itself is not included in the length. A byte 1527238405Sjkim * string of length 0 is invalid. No byte string may be truncated. 1528238405Sjkim * 1529238405Sjkim * The current, but experimental algorithm for selecting the protocol is: 1530238405Sjkim * 1531238405Sjkim * 1) If the server doesn't support NPN then this is indicated to the 1532238405Sjkim * callback. In this case, the client application has to abort the connection 1533238405Sjkim * or have a default application level protocol. 1534238405Sjkim * 1535238405Sjkim * 2) If the server supports NPN, but advertises an empty list then the 1536238405Sjkim * client selects the first protcol in its list, but indicates via the 1537238405Sjkim * API that this fallback case was enacted. 1538238405Sjkim * 1539238405Sjkim * 3) Otherwise, the client finds the first protocol in the server's list 1540238405Sjkim * that it supports and selects this protocol. This is because it's 1541238405Sjkim * assumed that the server has better information about which protocol 1542238405Sjkim * a client should use. 1543238405Sjkim * 1544238405Sjkim * 4) If the client doesn't support any of the server's advertised 1545238405Sjkim * protocols, then this is treated the same as case 2. 1546238405Sjkim * 1547238405Sjkim * It returns either 1548238405Sjkim * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or 1549238405Sjkim * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached. 1550238405Sjkim */ 1551238405Sjkimint 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) 1552238405Sjkim { 1553238405Sjkim unsigned int i, j; 1554238405Sjkim const unsigned char *result; 1555238405Sjkim int status = OPENSSL_NPN_UNSUPPORTED; 1556238405Sjkim 1557238405Sjkim /* For each protocol in server preference order, see if we support it. */ 1558238405Sjkim for (i = 0; i < server_len; ) 1559238405Sjkim { 1560238405Sjkim for (j = 0; j < client_len; ) 1561238405Sjkim { 1562238405Sjkim if (server[i] == client[j] && 1563238405Sjkim memcmp(&server[i+1], &client[j+1], server[i]) == 0) 1564238405Sjkim { 1565238405Sjkim /* We found a match */ 1566238405Sjkim result = &server[i]; 1567238405Sjkim status = OPENSSL_NPN_NEGOTIATED; 1568238405Sjkim goto found; 1569238405Sjkim } 1570238405Sjkim j += client[j]; 1571238405Sjkim j++; 1572238405Sjkim } 1573238405Sjkim i += server[i]; 1574238405Sjkim i++; 1575238405Sjkim } 1576238405Sjkim 1577238405Sjkim /* There's no overlap between our protocols and the server's list. */ 1578238405Sjkim result = client; 1579238405Sjkim status = OPENSSL_NPN_NO_OVERLAP; 1580238405Sjkim 1581238405Sjkim found: 1582238405Sjkim *out = (unsigned char *) result + 1; 1583238405Sjkim *outlen = result[0]; 1584238405Sjkim return status; 1585238405Sjkim } 1586238405Sjkim 1587238405Sjkim/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's 1588238405Sjkim * requested protocol for this connection and returns 0. If the client didn't 1589238405Sjkim * request any protocol, then *data is set to NULL. 1590238405Sjkim * 1591238405Sjkim * Note that the client can request any protocol it chooses. The value returned 1592238405Sjkim * from this function need not be a member of the list of supported protocols 1593238405Sjkim * provided by the callback. 1594238405Sjkim */ 1595238405Sjkimvoid SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data, unsigned *len) 1596238405Sjkim { 1597238405Sjkim *data = s->next_proto_negotiated; 1598238405Sjkim if (!*data) { 1599238405Sjkim *len = 0; 1600238405Sjkim } else { 1601238405Sjkim *len = s->next_proto_negotiated_len; 1602238405Sjkim } 1603238405Sjkim} 1604238405Sjkim 1605238405Sjkim/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a 1606238405Sjkim * TLS server needs a list of supported protocols for Next Protocol 1607238405Sjkim * Negotiation. The returned list must be in wire format. The list is returned 1608238405Sjkim * by setting |out| to point to it and |outlen| to its length. This memory will 1609238405Sjkim * not be modified, but one should assume that the SSL* keeps a reference to 1610238405Sjkim * it. 1611238405Sjkim * 1612238405Sjkim * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no 1613238405Sjkim * such extension will be included in the ServerHello. */ 1614238405Sjkimvoid 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) 1615238405Sjkim { 1616238405Sjkim ctx->next_protos_advertised_cb = cb; 1617238405Sjkim ctx->next_protos_advertised_cb_arg = arg; 1618238405Sjkim } 1619238405Sjkim 1620238405Sjkim/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a 1621238405Sjkim * client needs to select a protocol from the server's provided list. |out| 1622238405Sjkim * must be set to point to the selected protocol (which may be within |in|). 1623238405Sjkim * The length of the protocol name must be written into |outlen|. The server's 1624238405Sjkim * advertised protocols are provided in |in| and |inlen|. The callback can 1625238405Sjkim * assume that |in| is syntactically valid. 1626238405Sjkim * 1627238405Sjkim * The client must select a protocol. It is fatal to the connection if this 1628238405Sjkim * callback returns a value other than SSL_TLSEXT_ERR_OK. 1629238405Sjkim */ 1630238405Sjkimvoid 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) 1631238405Sjkim { 1632238405Sjkim ctx->next_proto_select_cb = cb; 1633238405Sjkim ctx->next_proto_select_cb_arg = arg; 1634238405Sjkim } 1635238405Sjkim# endif 1636194206Ssimon#endif 1637194206Ssimon 1638238405Sjkimint SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen, 1639238405Sjkim const char *label, size_t llen, const unsigned char *p, size_t plen, 1640238405Sjkim int use_context) 164155714Skris { 1642238405Sjkim if (s->version < TLS1_VERSION) 1643238405Sjkim return -1; 1644238405Sjkim 1645238405Sjkim return s->method->ssl3_enc->export_keying_material(s, out, olen, label, 1646238405Sjkim llen, p, plen, 1647238405Sjkim use_context); 1648238405Sjkim } 1649238405Sjkim 1650238405Sjkimstatic unsigned long ssl_session_hash(const SSL_SESSION *a) 1651238405Sjkim { 165255714Skris unsigned long l; 165355714Skris 165455714Skris l=(unsigned long) 165555714Skris ((unsigned int) a->session_id[0] )| 165655714Skris ((unsigned int) a->session_id[1]<< 8L)| 165755714Skris ((unsigned long)a->session_id[2]<<16L)| 165855714Skris ((unsigned long)a->session_id[3]<<24L); 165955714Skris return(l); 166055714Skris } 166155714Skris 1662109998Smarkm/* NB: If this function (or indeed the hash function which uses a sort of 1663109998Smarkm * coarser function than this one) is changed, ensure 1664109998Smarkm * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being 1665109998Smarkm * able to construct an SSL_SESSION that will collide with any existing session 1666109998Smarkm * with a matching session ID. */ 1667238405Sjkimstatic int ssl_session_cmp(const SSL_SESSION *a,const SSL_SESSION *b) 166855714Skris { 166955714Skris if (a->ssl_version != b->ssl_version) 167055714Skris return(1); 167155714Skris if (a->session_id_length != b->session_id_length) 167255714Skris return(1); 167355714Skris return(memcmp(a->session_id,b->session_id,a->session_id_length)); 167455714Skris } 167555714Skris 1676109998Smarkm/* These wrapper functions should remain rather than redeclaring 1677109998Smarkm * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each 1678109998Smarkm * variable. The reason is that the functions aren't static, they're exposed via 1679109998Smarkm * ssl.h. */ 1680238405Sjkimstatic IMPLEMENT_LHASH_HASH_FN(ssl_session, SSL_SESSION) 1681238405Sjkimstatic IMPLEMENT_LHASH_COMP_FN(ssl_session, SSL_SESSION) 1682109998Smarkm 1683238405SjkimSSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) 168455714Skris { 168555714Skris SSL_CTX *ret=NULL; 1686238405Sjkim 168755714Skris if (meth == NULL) 168855714Skris { 168955714Skris SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED); 169055714Skris return(NULL); 169155714Skris } 169255714Skris 1693194206Ssimon#ifdef OPENSSL_FIPS 1694194206Ssimon if (FIPS_mode() && (meth->version < TLS1_VERSION)) 1695194206Ssimon { 1696194206Ssimon SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE); 1697194206Ssimon return NULL; 1698194206Ssimon } 1699194206Ssimon#endif 1700194206Ssimon 170155714Skris if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) 170255714Skris { 170355714Skris SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS); 170455714Skris goto err; 170555714Skris } 170668651Skris ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX)); 170755714Skris if (ret == NULL) 170855714Skris goto err; 170955714Skris 171055714Skris memset(ret,0,sizeof(SSL_CTX)); 171155714Skris 171255714Skris ret->method=meth; 171355714Skris 171455714Skris ret->cert_store=NULL; 171555714Skris ret->session_cache_mode=SSL_SESS_CACHE_SERVER; 171655714Skris ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; 171755714Skris ret->session_cache_head=NULL; 171855714Skris ret->session_cache_tail=NULL; 171955714Skris 172055714Skris /* We take the system default */ 172155714Skris ret->session_timeout=meth->get_timeout(); 172255714Skris 1723109998Smarkm ret->new_session_cb=0; 1724109998Smarkm ret->remove_session_cb=0; 1725109998Smarkm ret->get_session_cb=0; 1726109998Smarkm ret->generate_session_id=0; 172755714Skris 172855714Skris memset((char *)&ret->stats,0,sizeof(ret->stats)); 172955714Skris 173055714Skris ret->references=1; 173155714Skris ret->quiet_shutdown=0; 173255714Skris 173355714Skris/* ret->cipher=NULL;*/ 173455714Skris/* ret->s2->challenge=NULL; 173555714Skris ret->master_key=NULL; 173655714Skris ret->key_arg=NULL; 173755714Skris ret->s2->conn_id=NULL; */ 173855714Skris 173955714Skris ret->info_callback=NULL; 174055714Skris 1741109998Smarkm ret->app_verify_callback=0; 174255714Skris ret->app_verify_arg=NULL; 174355714Skris 1744109998Smarkm ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT; 174555714Skris ret->read_ahead=0; 1746109998Smarkm ret->msg_callback=0; 1747109998Smarkm ret->msg_callback_arg=NULL; 174855714Skris ret->verify_mode=SSL_VERIFY_NONE; 1749160814Ssimon#if 0 175055714Skris ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */ 1751160814Ssimon#endif 1752109998Smarkm ret->sid_ctx_length=0; 175355714Skris ret->default_verify_callback=NULL; 175455714Skris if ((ret->cert=ssl_cert_new()) == NULL) 175555714Skris goto err; 175655714Skris 1757109998Smarkm ret->default_passwd_callback=0; 175855714Skris ret->default_passwd_callback_userdata=NULL; 1759109998Smarkm ret->client_cert_cb=0; 1760160814Ssimon ret->app_gen_cookie_cb=0; 1761160814Ssimon ret->app_verify_cookie_cb=0; 176255714Skris 1763238405Sjkim ret->sessions=lh_SSL_SESSION_new(); 176455714Skris if (ret->sessions == NULL) goto err; 176555714Skris ret->cert_store=X509_STORE_new(); 176655714Skris if (ret->cert_store == NULL) goto err; 176755714Skris 176855714Skris ssl_create_cipher_list(ret->method, 176955714Skris &ret->cipher_list,&ret->cipher_list_by_id, 1770238405Sjkim meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST); 177155714Skris if (ret->cipher_list == NULL 177255714Skris || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) 177355714Skris { 177455714Skris SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS); 177555714Skris goto err2; 177655714Skris } 177755714Skris 1778160814Ssimon ret->param = X509_VERIFY_PARAM_new(); 1779160814Ssimon if (!ret->param) 1780160814Ssimon goto err; 1781160814Ssimon 178255714Skris if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL) 178355714Skris { 178455714Skris SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES); 178555714Skris goto err2; 178655714Skris } 178755714Skris if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL) 178855714Skris { 178955714Skris SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES); 179055714Skris goto err2; 179155714Skris } 179255714Skris if ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL) 179355714Skris { 179455714Skris SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES); 179555714Skris goto err2; 179655714Skris } 179755714Skris 179855714Skris if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL) 179955714Skris goto err; 180055714Skris 1801109998Smarkm CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data); 180255714Skris 180355714Skris ret->extra_certs=NULL; 1804264331Sjkim /* No compression for DTLS */ 1805264331Sjkim if (meth->version != DTLS1_VERSION) 1806264331Sjkim ret->comp_methods=SSL_COMP_get_compression_methods(); 180755714Skris 1808238405Sjkim ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; 1809238405Sjkim 1810194206Ssimon#ifndef OPENSSL_NO_TLSEXT 1811194206Ssimon ret->tlsext_servername_callback = 0; 1812194206Ssimon ret->tlsext_servername_arg = NULL; 1813194206Ssimon /* Setup RFC4507 ticket keys */ 1814194206Ssimon if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0) 1815194206Ssimon || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0) 1816194206Ssimon || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0)) 1817194206Ssimon ret->options |= SSL_OP_NO_TICKET; 1818194206Ssimon 1819194206Ssimon ret->tlsext_status_cb = 0; 1820194206Ssimon ret->tlsext_status_arg = NULL; 1821194206Ssimon 1822238405Sjkim# ifndef OPENSSL_NO_NEXTPROTONEG 1823238405Sjkim ret->next_protos_advertised_cb = 0; 1824238405Sjkim ret->next_proto_select_cb = 0; 1825238405Sjkim# endif 1826194206Ssimon#endif 1827238405Sjkim#ifndef OPENSSL_NO_PSK 1828238405Sjkim ret->psk_identity_hint=NULL; 1829238405Sjkim ret->psk_client_callback=NULL; 1830238405Sjkim ret->psk_server_callback=NULL; 1831238405Sjkim#endif 1832238405Sjkim#ifndef OPENSSL_NO_SRP 1833238405Sjkim SSL_CTX_SRP_CTX_init(ret); 1834238405Sjkim#endif 1835238405Sjkim#ifndef OPENSSL_NO_BUF_FREELISTS 1836238405Sjkim ret->freelist_max_len = SSL_MAX_BUF_FREELIST_LEN_DEFAULT; 1837238405Sjkim ret->rbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST)); 1838238405Sjkim if (!ret->rbuf_freelist) 1839238405Sjkim goto err; 1840238405Sjkim ret->rbuf_freelist->chunklen = 0; 1841238405Sjkim ret->rbuf_freelist->len = 0; 1842238405Sjkim ret->rbuf_freelist->head = NULL; 1843238405Sjkim ret->wbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST)); 1844238405Sjkim if (!ret->wbuf_freelist) 1845238405Sjkim { 1846238405Sjkim OPENSSL_free(ret->rbuf_freelist); 1847238405Sjkim goto err; 1848238405Sjkim } 1849238405Sjkim ret->wbuf_freelist->chunklen = 0; 1850238405Sjkim ret->wbuf_freelist->len = 0; 1851238405Sjkim ret->wbuf_freelist->head = NULL; 1852238405Sjkim#endif 1853194206Ssimon#ifndef OPENSSL_NO_ENGINE 1854194206Ssimon ret->client_cert_engine = NULL; 1855194206Ssimon#ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO 1856194206Ssimon#define eng_strx(x) #x 1857194206Ssimon#define eng_str(x) eng_strx(x) 1858194206Ssimon /* Use specific client engine automatically... ignore errors */ 1859194206Ssimon { 1860194206Ssimon ENGINE *eng; 1861194206Ssimon eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO)); 1862194206Ssimon if (!eng) 1863194206Ssimon { 1864194206Ssimon ERR_clear_error(); 1865194206Ssimon ENGINE_load_builtin_engines(); 1866194206Ssimon eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO)); 1867194206Ssimon } 1868194206Ssimon if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng)) 1869194206Ssimon ERR_clear_error(); 1870194206Ssimon } 1871194206Ssimon#endif 1872194206Ssimon#endif 1873205128Ssimon /* Default is to connect to non-RI servers. When RI is more widely 1874205128Ssimon * deployed might change this. 1875205128Ssimon */ 1876205128Ssimon ret->options |= SSL_OP_LEGACY_SERVER_CONNECT; 1877194206Ssimon 187855714Skris return(ret); 187955714Skriserr: 188055714Skris SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE); 188155714Skriserr2: 188255714Skris if (ret != NULL) SSL_CTX_free(ret); 188355714Skris return(NULL); 188455714Skris } 188555714Skris 1886109998Smarkm#if 0 188755714Skrisstatic void SSL_COMP_free(SSL_COMP *comp) 188868651Skris { OPENSSL_free(comp); } 1889109998Smarkm#endif 189055714Skris 1891238405Sjkim#ifndef OPENSSL_NO_BUF_FREELISTS 1892238405Sjkimstatic void 1893238405Sjkimssl_buf_freelist_free(SSL3_BUF_FREELIST *list) 1894238405Sjkim { 1895238405Sjkim SSL3_BUF_FREELIST_ENTRY *ent, *next; 1896238405Sjkim for (ent = list->head; ent; ent = next) 1897238405Sjkim { 1898238405Sjkim next = ent->next; 1899238405Sjkim OPENSSL_free(ent); 1900238405Sjkim } 1901238405Sjkim OPENSSL_free(list); 1902238405Sjkim } 1903238405Sjkim#endif 1904238405Sjkim 190555714Skrisvoid SSL_CTX_free(SSL_CTX *a) 190655714Skris { 190755714Skris int i; 190855714Skris 190955714Skris if (a == NULL) return; 191055714Skris 191155714Skris i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX); 191255714Skris#ifdef REF_PRINT 191355714Skris REF_PRINT("SSL_CTX",a); 191455714Skris#endif 191555714Skris if (i > 0) return; 191655714Skris#ifdef REF_CHECK 191755714Skris if (i < 0) 191855714Skris { 191955714Skris fprintf(stderr,"SSL_CTX_free, bad reference count\n"); 192055714Skris abort(); /* ok */ 192155714Skris } 192255714Skris#endif 192355714Skris 1924160814Ssimon if (a->param) 1925160814Ssimon X509_VERIFY_PARAM_free(a->param); 1926160814Ssimon 1927109998Smarkm /* 1928109998Smarkm * Free internal session cache. However: the remove_cb() may reference 1929109998Smarkm * the ex_data of SSL_CTX, thus the ex_data store can only be removed 1930109998Smarkm * after the sessions were flushed. 1931109998Smarkm * As the ex_data handling routines might also touch the session cache, 1932109998Smarkm * the most secure solution seems to be: empty (flush) the cache, then 1933109998Smarkm * free ex_data, then finally free the cache. 1934109998Smarkm * (See ticket [openssl.org #212].) 1935109998Smarkm */ 193655714Skris if (a->sessions != NULL) 193755714Skris SSL_CTX_flush_sessions(a,0); 1938109998Smarkm 1939109998Smarkm CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data); 1940109998Smarkm 1941109998Smarkm if (a->sessions != NULL) 1942238405Sjkim lh_SSL_SESSION_free(a->sessions); 1943109998Smarkm 194455714Skris if (a->cert_store != NULL) 194555714Skris X509_STORE_free(a->cert_store); 194655714Skris if (a->cipher_list != NULL) 194755714Skris sk_SSL_CIPHER_free(a->cipher_list); 194855714Skris if (a->cipher_list_by_id != NULL) 194955714Skris sk_SSL_CIPHER_free(a->cipher_list_by_id); 195055714Skris if (a->cert != NULL) 195155714Skris ssl_cert_free(a->cert); 195255714Skris if (a->client_CA != NULL) 195355714Skris sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free); 195455714Skris if (a->extra_certs != NULL) 195555714Skris sk_X509_pop_free(a->extra_certs,X509_free); 1956109998Smarkm#if 0 /* This should never be done, since it removes a global database */ 195755714Skris if (a->comp_methods != NULL) 195855714Skris sk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free); 1959109998Smarkm#else 1960109998Smarkm a->comp_methods = NULL; 1961109998Smarkm#endif 1962238405Sjkim 1963246772Sjkim#ifndef OPENSSL_NO_SRTP 1964238405Sjkim if (a->srtp_profiles) 1965238405Sjkim sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles); 1966246772Sjkim#endif 1967238405Sjkim 1968238405Sjkim#ifndef OPENSSL_NO_PSK 1969238405Sjkim if (a->psk_identity_hint) 1970238405Sjkim OPENSSL_free(a->psk_identity_hint); 1971238405Sjkim#endif 1972238405Sjkim#ifndef OPENSSL_NO_SRP 1973238405Sjkim SSL_CTX_SRP_CTX_free(a); 1974238405Sjkim#endif 1975194206Ssimon#ifndef OPENSSL_NO_ENGINE 1976194206Ssimon if (a->client_cert_engine) 1977194206Ssimon ENGINE_finish(a->client_cert_engine); 1978194206Ssimon#endif 1979238405Sjkim 1980238405Sjkim#ifndef OPENSSL_NO_BUF_FREELISTS 1981238405Sjkim if (a->wbuf_freelist) 1982238405Sjkim ssl_buf_freelist_free(a->wbuf_freelist); 1983238405Sjkim if (a->rbuf_freelist) 1984238405Sjkim ssl_buf_freelist_free(a->rbuf_freelist); 1985238405Sjkim#endif 1986238405Sjkim 198768651Skris OPENSSL_free(a); 198855714Skris } 198955714Skris 199055714Skrisvoid SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) 199155714Skris { 199255714Skris ctx->default_passwd_callback=cb; 199355714Skris } 199455714Skris 199555714Skrisvoid SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u) 199655714Skris { 199755714Skris ctx->default_passwd_callback_userdata=u; 199855714Skris } 199955714Skris 2000109998Smarkmvoid SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg) 200155714Skris { 200255714Skris ctx->app_verify_callback=cb; 2003109998Smarkm ctx->app_verify_arg=arg; 200455714Skris } 200555714Skris 200655714Skrisvoid SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *)) 200755714Skris { 200855714Skris ctx->verify_mode=mode; 200955714Skris ctx->default_verify_callback=cb; 201055714Skris } 201155714Skris 201255714Skrisvoid SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth) 201355714Skris { 2014160814Ssimon X509_VERIFY_PARAM_set_depth(ctx->param, depth); 201555714Skris } 201655714Skris 2017238405Sjkimvoid ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher) 201855714Skris { 201955714Skris CERT_PKEY *cpk; 202055714Skris int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign; 202155714Skris int rsa_enc_export,dh_rsa_export,dh_dsa_export; 202255714Skris int rsa_tmp_export,dh_tmp_export,kl; 2023238405Sjkim unsigned long mask_k,mask_a,emask_k,emask_a; 2024160814Ssimon int have_ecc_cert, ecdh_ok, ecdsa_ok, ecc_pkey_size; 2025160814Ssimon#ifndef OPENSSL_NO_ECDH 2026160814Ssimon int have_ecdh_tmp; 2027160814Ssimon#endif 2028160814Ssimon X509 *x = NULL; 2029160814Ssimon EVP_PKEY *ecc_pkey = NULL; 2030238405Sjkim int signature_nid = 0, pk_nid = 0, md_nid = 0; 203155714Skris 203255714Skris if (c == NULL) return; 203355714Skris 203455714Skris kl=SSL_C_EXPORT_PKEYLENGTH(cipher); 203555714Skris 2036109998Smarkm#ifndef OPENSSL_NO_RSA 203755714Skris rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL); 203855714Skris rsa_tmp_export=(c->rsa_tmp_cb != NULL || 203955714Skris (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl)); 204055714Skris#else 204155714Skris rsa_tmp=rsa_tmp_export=0; 204255714Skris#endif 2043109998Smarkm#ifndef OPENSSL_NO_DH 204455714Skris dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL); 204555714Skris dh_tmp_export=(c->dh_tmp_cb != NULL || 204655714Skris (dh_tmp && DH_size(c->dh_tmp)*8 <= kl)); 204755714Skris#else 204855714Skris dh_tmp=dh_tmp_export=0; 204955714Skris#endif 205055714Skris 2051160814Ssimon#ifndef OPENSSL_NO_ECDH 2052160814Ssimon have_ecdh_tmp=(c->ecdh_tmp != NULL || c->ecdh_tmp_cb != NULL); 2053160814Ssimon#endif 205455714Skris cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]); 205555714Skris rsa_enc= (cpk->x509 != NULL && cpk->privatekey != NULL); 205655714Skris rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl); 205755714Skris cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]); 205855714Skris rsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL); 205955714Skris cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]); 206055714Skris dsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL); 206155714Skris cpk= &(c->pkeys[SSL_PKEY_DH_RSA]); 206255714Skris dh_rsa= (cpk->x509 != NULL && cpk->privatekey != NULL); 206355714Skris dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl); 206455714Skris cpk= &(c->pkeys[SSL_PKEY_DH_DSA]); 206555714Skris/* FIX THIS EAY EAY EAY */ 206655714Skris dh_dsa= (cpk->x509 != NULL && cpk->privatekey != NULL); 206755714Skris dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl); 2068160814Ssimon cpk= &(c->pkeys[SSL_PKEY_ECC]); 2069160814Ssimon have_ecc_cert= (cpk->x509 != NULL && cpk->privatekey != NULL); 2070238405Sjkim mask_k=0; 2071238405Sjkim mask_a=0; 2072238405Sjkim emask_k=0; 2073238405Sjkim emask_a=0; 207455714Skris 2075238405Sjkim 2076238405Sjkim 207755714Skris#ifdef CIPHER_DEBUG 2078238405Sjkim printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n", 2079238405Sjkim rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp, 208055714Skris rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa); 208155714Skris#endif 2082238405Sjkim 2083238405Sjkim cpk = &(c->pkeys[SSL_PKEY_GOST01]); 2084238405Sjkim if (cpk->x509 != NULL && cpk->privatekey !=NULL) { 2085238405Sjkim mask_k |= SSL_kGOST; 2086238405Sjkim mask_a |= SSL_aGOST01; 2087238405Sjkim } 2088238405Sjkim cpk = &(c->pkeys[SSL_PKEY_GOST94]); 2089238405Sjkim if (cpk->x509 != NULL && cpk->privatekey !=NULL) { 2090238405Sjkim mask_k |= SSL_kGOST; 2091238405Sjkim mask_a |= SSL_aGOST94; 2092238405Sjkim } 209355714Skris 209455714Skris if (rsa_enc || (rsa_tmp && rsa_sign)) 2095238405Sjkim mask_k|=SSL_kRSA; 209655714Skris if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc))) 2097238405Sjkim emask_k|=SSL_kRSA; 209855714Skris 209955714Skris#if 0 210055714Skris /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */ 2101238405Sjkim if ( (dh_tmp || dh_rsa || dh_dsa) && 210255714Skris (rsa_enc || rsa_sign || dsa_sign)) 2103238405Sjkim mask_k|=SSL_kEDH; 210455714Skris if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) && 210555714Skris (rsa_enc || rsa_sign || dsa_sign)) 2106238405Sjkim emask_k|=SSL_kEDH; 210755714Skris#endif 210855714Skris 2109238405Sjkim if (dh_tmp_export) 2110238405Sjkim emask_k|=SSL_kEDH; 211155714Skris 211255714Skris if (dh_tmp) 2113238405Sjkim mask_k|=SSL_kEDH; 211455714Skris 2115238405Sjkim if (dh_rsa) mask_k|=SSL_kDHr; 2116238405Sjkim if (dh_rsa_export) emask_k|=SSL_kDHr; 211755714Skris 2118238405Sjkim if (dh_dsa) mask_k|=SSL_kDHd; 2119238405Sjkim if (dh_dsa_export) emask_k|=SSL_kDHd; 212055714Skris 212155714Skris if (rsa_enc || rsa_sign) 212255714Skris { 2123238405Sjkim mask_a|=SSL_aRSA; 2124238405Sjkim emask_a|=SSL_aRSA; 212555714Skris } 212655714Skris 212755714Skris if (dsa_sign) 212855714Skris { 2129238405Sjkim mask_a|=SSL_aDSS; 2130238405Sjkim emask_a|=SSL_aDSS; 213155714Skris } 213255714Skris 2133238405Sjkim mask_a|=SSL_aNULL; 2134238405Sjkim emask_a|=SSL_aNULL; 213555714Skris 2136109998Smarkm#ifndef OPENSSL_NO_KRB5 2137238405Sjkim mask_k|=SSL_kKRB5; 2138238405Sjkim mask_a|=SSL_aKRB5; 2139238405Sjkim emask_k|=SSL_kKRB5; 2140238405Sjkim emask_a|=SSL_aKRB5; 2141109998Smarkm#endif 2142109998Smarkm 2143160814Ssimon /* An ECC certificate may be usable for ECDH and/or 2144160814Ssimon * ECDSA cipher suites depending on the key usage extension. 2145160814Ssimon */ 2146160814Ssimon if (have_ecc_cert) 2147160814Ssimon { 2148238405Sjkim /* This call populates extension flags (ex_flags) */ 2149160814Ssimon x = (c->pkeys[SSL_PKEY_ECC]).x509; 2150160814Ssimon X509_check_purpose(x, -1, 0); 2151160814Ssimon ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ? 2152160814Ssimon (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1; 2153160814Ssimon ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ? 2154160814Ssimon (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1; 2155160814Ssimon ecc_pkey = X509_get_pubkey(x); 2156238405Sjkim ecc_pkey_size = (ecc_pkey != NULL) ? 2157160814Ssimon EVP_PKEY_bits(ecc_pkey) : 0; 2158160814Ssimon EVP_PKEY_free(ecc_pkey); 2159160814Ssimon if ((x->sig_alg) && (x->sig_alg->algorithm)) 2160238405Sjkim { 2161160814Ssimon signature_nid = OBJ_obj2nid(x->sig_alg->algorithm); 2162238405Sjkim OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid); 2163238405Sjkim } 2164160814Ssimon#ifndef OPENSSL_NO_ECDH 2165160814Ssimon if (ecdh_ok) 2166160814Ssimon { 2167238405Sjkim 2168238405Sjkim if (pk_nid == NID_rsaEncryption || pk_nid == NID_rsa) 2169160814Ssimon { 2170238405Sjkim mask_k|=SSL_kECDHr; 2171238405Sjkim mask_a|=SSL_aECDH; 2172160814Ssimon if (ecc_pkey_size <= 163) 2173238405Sjkim { 2174238405Sjkim emask_k|=SSL_kECDHr; 2175238405Sjkim emask_a|=SSL_aECDH; 2176238405Sjkim } 2177160814Ssimon } 2178238405Sjkim 2179238405Sjkim if (pk_nid == NID_X9_62_id_ecPublicKey) 2180160814Ssimon { 2181238405Sjkim mask_k|=SSL_kECDHe; 2182238405Sjkim mask_a|=SSL_aECDH; 2183160814Ssimon if (ecc_pkey_size <= 163) 2184238405Sjkim { 2185238405Sjkim emask_k|=SSL_kECDHe; 2186238405Sjkim emask_a|=SSL_aECDH; 2187238405Sjkim } 2188160814Ssimon } 2189160814Ssimon } 2190160814Ssimon#endif 2191160814Ssimon#ifndef OPENSSL_NO_ECDSA 2192160814Ssimon if (ecdsa_ok) 2193160814Ssimon { 2194238405Sjkim mask_a|=SSL_aECDSA; 2195238405Sjkim emask_a|=SSL_aECDSA; 2196160814Ssimon } 2197160814Ssimon#endif 2198160814Ssimon } 2199160814Ssimon 2200160814Ssimon#ifndef OPENSSL_NO_ECDH 2201160814Ssimon if (have_ecdh_tmp) 2202160814Ssimon { 2203238405Sjkim mask_k|=SSL_kEECDH; 2204238405Sjkim emask_k|=SSL_kEECDH; 2205160814Ssimon } 2206160814Ssimon#endif 2207238405Sjkim 2208238405Sjkim#ifndef OPENSSL_NO_PSK 2209238405Sjkim mask_k |= SSL_kPSK; 2210238405Sjkim mask_a |= SSL_aPSK; 2211238405Sjkim emask_k |= SSL_kPSK; 2212238405Sjkim emask_a |= SSL_aPSK; 2213238405Sjkim#endif 2214238405Sjkim 2215238405Sjkim c->mask_k=mask_k; 2216238405Sjkim c->mask_a=mask_a; 2217238405Sjkim c->export_mask_k=emask_k; 2218238405Sjkim c->export_mask_a=emask_a; 221955714Skris c->valid=1; 222055714Skris } 222155714Skris 2222160814Ssimon/* This handy macro borrowed from crypto/x509v3/v3_purp.c */ 2223160814Ssimon#define ku_reject(x, usage) \ 2224160814Ssimon (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage))) 2225160814Ssimon 2226238405Sjkim#ifndef OPENSSL_NO_EC 2227238405Sjkim 2228238405Sjkimint ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) 2229160814Ssimon { 2230238405Sjkim unsigned long alg_k, alg_a; 2231160814Ssimon EVP_PKEY *pkey = NULL; 2232160814Ssimon int keysize = 0; 2233238405Sjkim int signature_nid = 0, md_nid = 0, pk_nid = 0; 2234238405Sjkim const SSL_CIPHER *cs = s->s3->tmp.new_cipher; 2235160814Ssimon 2236238405Sjkim alg_k = cs->algorithm_mkey; 2237238405Sjkim alg_a = cs->algorithm_auth; 2238238405Sjkim 2239160814Ssimon if (SSL_C_IS_EXPORT(cs)) 2240160814Ssimon { 2241160814Ssimon /* ECDH key length in export ciphers must be <= 163 bits */ 2242160814Ssimon pkey = X509_get_pubkey(x); 2243160814Ssimon if (pkey == NULL) return 0; 2244160814Ssimon keysize = EVP_PKEY_bits(pkey); 2245160814Ssimon EVP_PKEY_free(pkey); 2246160814Ssimon if (keysize > 163) return 0; 2247160814Ssimon } 2248160814Ssimon 2249160814Ssimon /* This call populates the ex_flags field correctly */ 2250160814Ssimon X509_check_purpose(x, -1, 0); 2251160814Ssimon if ((x->sig_alg) && (x->sig_alg->algorithm)) 2252238405Sjkim { 2253160814Ssimon signature_nid = OBJ_obj2nid(x->sig_alg->algorithm); 2254238405Sjkim OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid); 2255238405Sjkim } 2256238405Sjkim if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr) 2257160814Ssimon { 2258160814Ssimon /* key usage, if present, must allow key agreement */ 2259160814Ssimon if (ku_reject(x, X509v3_KU_KEY_AGREEMENT)) 2260160814Ssimon { 2261238405Sjkim SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT); 2262160814Ssimon return 0; 2263160814Ssimon } 2264238405Sjkim if ((alg_k & SSL_kECDHe) && TLS1_get_version(s) < TLS1_2_VERSION) 2265160814Ssimon { 2266160814Ssimon /* signature alg must be ECDSA */ 2267238405Sjkim if (pk_nid != NID_X9_62_id_ecPublicKey) 2268160814Ssimon { 2269238405Sjkim SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE); 2270160814Ssimon return 0; 2271160814Ssimon } 2272160814Ssimon } 2273238405Sjkim if ((alg_k & SSL_kECDHr) && TLS1_get_version(s) < TLS1_2_VERSION) 2274160814Ssimon { 2275160814Ssimon /* signature alg must be RSA */ 2276238405Sjkim 2277238405Sjkim if (pk_nid != NID_rsaEncryption && pk_nid != NID_rsa) 2278160814Ssimon { 2279238405Sjkim SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE); 2280160814Ssimon return 0; 2281160814Ssimon } 2282160814Ssimon } 2283238405Sjkim } 2284238405Sjkim if (alg_a & SSL_aECDSA) 2285160814Ssimon { 2286160814Ssimon /* key usage, if present, must allow signing */ 2287160814Ssimon if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) 2288160814Ssimon { 2289238405Sjkim SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_SIGNING); 2290160814Ssimon return 0; 2291160814Ssimon } 2292160814Ssimon } 2293160814Ssimon 2294160814Ssimon return 1; /* all checks are ok */ 2295160814Ssimon } 2296160814Ssimon 2297238405Sjkim#endif 2298238405Sjkim 229955714Skris/* THIS NEEDS CLEANING UP */ 2300246772SjkimCERT_PKEY *ssl_get_server_send_pkey(const SSL *s) 230155714Skris { 2302238405Sjkim unsigned long alg_k,alg_a; 230355714Skris CERT *c; 2304215697Ssimon int i; 230555714Skris 230655714Skris c=s->cert; 230755714Skris ssl_set_cert_masks(c, s->s3->tmp.new_cipher); 2308238405Sjkim 2309238405Sjkim alg_k = s->s3->tmp.new_cipher->algorithm_mkey; 2310238405Sjkim alg_a = s->s3->tmp.new_cipher->algorithm_auth; 231155714Skris 2312238405Sjkim if (alg_k & (SSL_kECDHr|SSL_kECDHe)) 2313160814Ssimon { 2314238405Sjkim /* we don't need to look at SSL_kEECDH 2315160814Ssimon * since no certificate is needed for 2316160814Ssimon * anon ECDH and for authenticated 2317238405Sjkim * EECDH, the check for the auth 2318160814Ssimon * algorithm will set i correctly 2319160814Ssimon * NOTE: For ECDH-RSA, we need an ECC 2320238405Sjkim * not an RSA cert but for EECDH-RSA 2321160814Ssimon * we need an RSA cert. Placing the 2322160814Ssimon * checks for SSL_kECDH before RSA 2323160814Ssimon * checks ensures the correct cert is chosen. 2324160814Ssimon */ 2325160814Ssimon i=SSL_PKEY_ECC; 2326160814Ssimon } 2327238405Sjkim else if (alg_a & SSL_aECDSA) 2328160814Ssimon { 2329160814Ssimon i=SSL_PKEY_ECC; 2330160814Ssimon } 2331238405Sjkim else if (alg_k & SSL_kDHr) 233255714Skris i=SSL_PKEY_DH_RSA; 2333238405Sjkim else if (alg_k & SSL_kDHd) 233455714Skris i=SSL_PKEY_DH_DSA; 2335238405Sjkim else if (alg_a & SSL_aDSS) 233655714Skris i=SSL_PKEY_DSA_SIGN; 2337238405Sjkim else if (alg_a & SSL_aRSA) 233855714Skris { 233955714Skris if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL) 234055714Skris i=SSL_PKEY_RSA_SIGN; 234155714Skris else 234255714Skris i=SSL_PKEY_RSA_ENC; 234355714Skris } 2344238405Sjkim else if (alg_a & SSL_aKRB5) 2345109998Smarkm { 2346109998Smarkm /* VRS something else here? */ 2347109998Smarkm return(NULL); 2348109998Smarkm } 2349238405Sjkim else if (alg_a & SSL_aGOST94) 2350238405Sjkim i=SSL_PKEY_GOST94; 2351238405Sjkim else if (alg_a & SSL_aGOST01) 2352238405Sjkim i=SSL_PKEY_GOST01; 2353238405Sjkim else /* if (alg_a & SSL_aNULL) */ 235455714Skris { 2355246772Sjkim SSLerr(SSL_F_SSL_GET_SERVER_SEND_PKEY,ERR_R_INTERNAL_ERROR); 235655714Skris return(NULL); 235755714Skris } 2358160814Ssimon 2359246772Sjkim return c->pkeys + i; 236055714Skris } 236155714Skris 2362246772SjkimX509 *ssl_get_server_send_cert(const SSL *s) 2363246772Sjkim { 2364246772Sjkim CERT_PKEY *cpk; 2365246772Sjkim cpk = ssl_get_server_send_pkey(s); 2366246772Sjkim if (!cpk) 2367246772Sjkim return NULL; 2368246772Sjkim return cpk->x509; 2369246772Sjkim } 2370246772Sjkim 2371238405SjkimEVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher, const EVP_MD **pmd) 237255714Skris { 2373238405Sjkim unsigned long alg_a; 237455714Skris CERT *c; 2375238405Sjkim int idx = -1; 237655714Skris 2377238405Sjkim alg_a = cipher->algorithm_auth; 237855714Skris c=s->cert; 237955714Skris 2380238405Sjkim if ((alg_a & SSL_aDSS) && 238155714Skris (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL)) 2382238405Sjkim idx = SSL_PKEY_DSA_SIGN; 2383238405Sjkim else if (alg_a & SSL_aRSA) 238455714Skris { 238555714Skris if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) 2386238405Sjkim idx = SSL_PKEY_RSA_SIGN; 238755714Skris else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) 2388238405Sjkim idx = SSL_PKEY_RSA_ENC; 238955714Skris } 2390238405Sjkim else if ((alg_a & SSL_aECDSA) && 2391160814Ssimon (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) 2392238405Sjkim idx = SSL_PKEY_ECC; 2393238405Sjkim if (idx == -1) 239455714Skris { 2395109998Smarkm SSLerr(SSL_F_SSL_GET_SIGN_PKEY,ERR_R_INTERNAL_ERROR); 239655714Skris return(NULL); 239755714Skris } 2398238405Sjkim if (pmd) 2399238405Sjkim *pmd = c->pkeys[idx].digest; 2400238405Sjkim return c->pkeys[idx].privatekey; 240155714Skris } 240255714Skris 240355714Skrisvoid ssl_update_cache(SSL *s,int mode) 240455714Skris { 240555714Skris int i; 240655714Skris 240755714Skris /* If the session_id_length is 0, we are not supposed to cache it, 240855714Skris * and it would be rather hard to do anyway :-) */ 240955714Skris if (s->session->session_id_length == 0) return; 241055714Skris 2411238405Sjkim i=s->session_ctx->session_cache_mode; 2412100928Snectar if ((i & mode) && (!s->hit) 2413109998Smarkm && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) 2414238405Sjkim || SSL_CTX_add_session(s->session_ctx,s->session)) 2415238405Sjkim && (s->session_ctx->new_session_cb != NULL)) 241655714Skris { 241755714Skris CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION); 2418238405Sjkim if (!s->session_ctx->new_session_cb(s,s->session)) 241955714Skris SSL_SESSION_free(s->session); 242055714Skris } 242155714Skris 242255714Skris /* auto flush every 255 connections */ 242355714Skris if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && 242455714Skris ((i & mode) == mode)) 242555714Skris { 242655714Skris if ( (((mode & SSL_SESS_CACHE_CLIENT) 2427238405Sjkim ?s->session_ctx->stats.sess_connect_good 2428238405Sjkim :s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff) 242955714Skris { 2430238405Sjkim SSL_CTX_flush_sessions(s->session_ctx,(unsigned long)time(NULL)); 243155714Skris } 243255714Skris } 243355714Skris } 243455714Skris 2435238405Sjkimconst SSL_METHOD *SSL_get_ssl_method(SSL *s) 243655714Skris { 243755714Skris return(s->method); 243855714Skris } 243955714Skris 2440238405Sjkimint SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth) 244155714Skris { 244255714Skris int conn= -1; 244355714Skris int ret=1; 244455714Skris 244555714Skris if (s->method != meth) 244655714Skris { 244755714Skris if (s->handshake_func != NULL) 244855714Skris conn=(s->handshake_func == s->method->ssl_connect); 244955714Skris 245055714Skris if (s->method->version == meth->version) 245155714Skris s->method=meth; 245255714Skris else 245355714Skris { 245455714Skris s->method->ssl_free(s); 245555714Skris s->method=meth; 245655714Skris ret=s->method->ssl_new(s); 245755714Skris } 245855714Skris 245955714Skris if (conn == 1) 246055714Skris s->handshake_func=meth->ssl_connect; 246155714Skris else if (conn == 0) 246255714Skris s->handshake_func=meth->ssl_accept; 246355714Skris } 246455714Skris return(ret); 246555714Skris } 246655714Skris 2467160814Ssimonint SSL_get_error(const SSL *s,int i) 246855714Skris { 246955714Skris int reason; 247055714Skris unsigned long l; 247155714Skris BIO *bio; 247255714Skris 247355714Skris if (i > 0) return(SSL_ERROR_NONE); 247455714Skris 247555714Skris /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake 247655714Skris * etc, where we do encode the error */ 247755714Skris if ((l=ERR_peek_error()) != 0) 247855714Skris { 247955714Skris if (ERR_GET_LIB(l) == ERR_LIB_SYS) 248055714Skris return(SSL_ERROR_SYSCALL); 248155714Skris else 248255714Skris return(SSL_ERROR_SSL); 248355714Skris } 248455714Skris 248555714Skris if ((i < 0) && SSL_want_read(s)) 248655714Skris { 248755714Skris bio=SSL_get_rbio(s); 248855714Skris if (BIO_should_read(bio)) 248955714Skris return(SSL_ERROR_WANT_READ); 249055714Skris else if (BIO_should_write(bio)) 249155714Skris /* This one doesn't make too much sense ... We never try 249255714Skris * to write to the rbio, and an application program where 249355714Skris * rbio and wbio are separate couldn't even know what it 249455714Skris * should wait for. 249555714Skris * However if we ever set s->rwstate incorrectly 249655714Skris * (so that we have SSL_want_read(s) instead of 249755714Skris * SSL_want_write(s)) and rbio and wbio *are* the same, 249855714Skris * this test works around that bug; so it might be safer 249955714Skris * to keep it. */ 250055714Skris return(SSL_ERROR_WANT_WRITE); 250155714Skris else if (BIO_should_io_special(bio)) 250255714Skris { 250355714Skris reason=BIO_get_retry_reason(bio); 250455714Skris if (reason == BIO_RR_CONNECT) 250555714Skris return(SSL_ERROR_WANT_CONNECT); 2506109998Smarkm else if (reason == BIO_RR_ACCEPT) 2507109998Smarkm return(SSL_ERROR_WANT_ACCEPT); 250855714Skris else 250955714Skris return(SSL_ERROR_SYSCALL); /* unknown */ 251055714Skris } 251155714Skris } 251255714Skris 251355714Skris if ((i < 0) && SSL_want_write(s)) 251455714Skris { 251555714Skris bio=SSL_get_wbio(s); 251655714Skris if (BIO_should_write(bio)) 251755714Skris return(SSL_ERROR_WANT_WRITE); 251855714Skris else if (BIO_should_read(bio)) 251955714Skris /* See above (SSL_want_read(s) with BIO_should_write(bio)) */ 252055714Skris return(SSL_ERROR_WANT_READ); 252155714Skris else if (BIO_should_io_special(bio)) 252255714Skris { 252355714Skris reason=BIO_get_retry_reason(bio); 252455714Skris if (reason == BIO_RR_CONNECT) 252555714Skris return(SSL_ERROR_WANT_CONNECT); 2526109998Smarkm else if (reason == BIO_RR_ACCEPT) 2527109998Smarkm return(SSL_ERROR_WANT_ACCEPT); 252855714Skris else 252955714Skris return(SSL_ERROR_SYSCALL); 253055714Skris } 253155714Skris } 253255714Skris if ((i < 0) && SSL_want_x509_lookup(s)) 253355714Skris { 253455714Skris return(SSL_ERROR_WANT_X509_LOOKUP); 253555714Skris } 253655714Skris 253755714Skris if (i == 0) 253855714Skris { 253955714Skris if (s->version == SSL2_VERSION) 254055714Skris { 254155714Skris /* assume it is the socket being closed */ 254255714Skris return(SSL_ERROR_ZERO_RETURN); 254355714Skris } 254455714Skris else 254555714Skris { 254655714Skris if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) && 254755714Skris (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) 254855714Skris return(SSL_ERROR_ZERO_RETURN); 254955714Skris } 255055714Skris } 255155714Skris return(SSL_ERROR_SYSCALL); 255255714Skris } 255355714Skris 255455714Skrisint SSL_do_handshake(SSL *s) 255555714Skris { 255655714Skris int ret=1; 255755714Skris 255855714Skris if (s->handshake_func == NULL) 255955714Skris { 256055714Skris SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET); 256155714Skris return(-1); 256255714Skris } 256355714Skris 256455714Skris s->method->ssl_renegotiate_check(s); 256555714Skris 256655714Skris if (SSL_in_init(s) || SSL_in_before(s)) 256755714Skris { 256855714Skris ret=s->handshake_func(s); 256955714Skris } 257055714Skris return(ret); 257155714Skris } 257255714Skris 257355714Skris/* For the next 2 functions, SSL_clear() sets shutdown and so 257455714Skris * one of these calls will reset it */ 257555714Skrisvoid SSL_set_accept_state(SSL *s) 257655714Skris { 257755714Skris s->server=1; 257855714Skris s->shutdown=0; 257955714Skris s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE; 258055714Skris s->handshake_func=s->method->ssl_accept; 258155714Skris /* clear the current cipher */ 258255714Skris ssl_clear_cipher_ctx(s); 2583238405Sjkim ssl_clear_hash_ctx(&s->read_hash); 2584238405Sjkim ssl_clear_hash_ctx(&s->write_hash); 258555714Skris } 258655714Skris 258755714Skrisvoid SSL_set_connect_state(SSL *s) 258855714Skris { 258955714Skris s->server=0; 259055714Skris s->shutdown=0; 259155714Skris s->state=SSL_ST_CONNECT|SSL_ST_BEFORE; 259255714Skris s->handshake_func=s->method->ssl_connect; 259355714Skris /* clear the current cipher */ 259455714Skris ssl_clear_cipher_ctx(s); 2595238405Sjkim ssl_clear_hash_ctx(&s->read_hash); 2596238405Sjkim ssl_clear_hash_ctx(&s->write_hash); 259755714Skris } 259855714Skris 259955714Skrisint ssl_undefined_function(SSL *s) 260055714Skris { 260155714Skris SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 260255714Skris return(0); 260355714Skris } 260455714Skris 2605160814Ssimonint ssl_undefined_void_function(void) 2606160814Ssimon { 2607160814Ssimon SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2608160814Ssimon return(0); 2609160814Ssimon } 2610160814Ssimon 2611160814Ssimonint ssl_undefined_const_function(const SSL *s) 2612160814Ssimon { 2613160814Ssimon SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2614160814Ssimon return(0); 2615160814Ssimon } 2616160814Ssimon 261755714SkrisSSL_METHOD *ssl_bad_method(int ver) 261855714Skris { 261955714Skris SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 262055714Skris return(NULL); 262155714Skris } 262255714Skris 2623160814Ssimonconst char *SSL_get_version(const SSL *s) 262455714Skris { 2625238405Sjkim if (s->version == TLS1_2_VERSION) 2626238405Sjkim return("TLSv1.2"); 2627238405Sjkim else if (s->version == TLS1_1_VERSION) 2628238405Sjkim return("TLSv1.1"); 2629246772Sjkim else if (s->version == TLS1_VERSION) 263055714Skris return("TLSv1"); 263155714Skris else if (s->version == SSL3_VERSION) 263255714Skris return("SSLv3"); 263355714Skris else if (s->version == SSL2_VERSION) 263455714Skris return("SSLv2"); 263555714Skris else 263655714Skris return("unknown"); 263755714Skris } 263855714Skris 263955714SkrisSSL *SSL_dup(SSL *s) 264055714Skris { 264155714Skris STACK_OF(X509_NAME) *sk; 264255714Skris X509_NAME *xn; 264355714Skris SSL *ret; 264455714Skris int i; 2645238405Sjkim 264655714Skris if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL) 264755714Skris return(NULL); 2648109998Smarkm 2649109998Smarkm ret->version = s->version; 2650109998Smarkm ret->type = s->type; 2651109998Smarkm ret->method = s->method; 2652109998Smarkm 265355714Skris if (s->session != NULL) 265455714Skris { 265555714Skris /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */ 265655714Skris SSL_copy_session_id(ret,s); 265755714Skris } 265855714Skris else 265955714Skris { 266055714Skris /* No session has been established yet, so we have to expect 266155714Skris * that s->cert or ret->cert will be changed later -- 266255714Skris * they should not both point to the same object, 266355714Skris * and thus we can't use SSL_copy_session_id. */ 266455714Skris 2665111147Snectar ret->method->ssl_free(ret); 266655714Skris ret->method = s->method; 266755714Skris ret->method->ssl_new(ret); 266855714Skris 266955714Skris if (s->cert != NULL) 267055714Skris { 267176866Skris if (ret->cert != NULL) 267276866Skris { 267376866Skris ssl_cert_free(ret->cert); 267476866Skris } 267555714Skris ret->cert = ssl_cert_dup(s->cert); 267655714Skris if (ret->cert == NULL) 267755714Skris goto err; 267855714Skris } 267955714Skris 268055714Skris SSL_set_session_id_context(ret, 268155714Skris s->sid_ctx, s->sid_ctx_length); 268255714Skris } 268355714Skris 2684109998Smarkm ret->options=s->options; 2685109998Smarkm ret->mode=s->mode; 2686109998Smarkm SSL_set_max_cert_list(ret,SSL_get_max_cert_list(s)); 268755714Skris SSL_set_read_ahead(ret,SSL_get_read_ahead(s)); 2688109998Smarkm ret->msg_callback = s->msg_callback; 2689109998Smarkm ret->msg_callback_arg = s->msg_callback_arg; 269055714Skris SSL_set_verify(ret,SSL_get_verify_mode(s), 269155714Skris SSL_get_verify_callback(s)); 269255714Skris SSL_set_verify_depth(ret,SSL_get_verify_depth(s)); 2693109998Smarkm ret->generate_session_id = s->generate_session_id; 269455714Skris 269555714Skris SSL_set_info_callback(ret,SSL_get_info_callback(s)); 269655714Skris 269755714Skris ret->debug=s->debug; 269855714Skris 269955714Skris /* copy app data, a little dangerous perhaps */ 2700109998Smarkm if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data)) 270155714Skris goto err; 270255714Skris 270355714Skris /* setup rbio, and wbio */ 270455714Skris if (s->rbio != NULL) 270555714Skris { 270655714Skris if (!BIO_dup_state(s->rbio,(char *)&ret->rbio)) 270755714Skris goto err; 270855714Skris } 270955714Skris if (s->wbio != NULL) 271055714Skris { 271155714Skris if (s->wbio != s->rbio) 271255714Skris { 271355714Skris if (!BIO_dup_state(s->wbio,(char *)&ret->wbio)) 271455714Skris goto err; 271555714Skris } 271655714Skris else 271755714Skris ret->wbio=ret->rbio; 271855714Skris } 2719109998Smarkm ret->rwstate = s->rwstate; 2720109998Smarkm ret->in_handshake = s->in_handshake; 2721109998Smarkm ret->handshake_func = s->handshake_func; 2722109998Smarkm ret->server = s->server; 2723238405Sjkim ret->renegotiate = s->renegotiate; 2724109998Smarkm ret->new_session = s->new_session; 2725109998Smarkm ret->quiet_shutdown = s->quiet_shutdown; 2726109998Smarkm ret->shutdown=s->shutdown; 2727109998Smarkm ret->state=s->state; /* SSL_dup does not really work at any state, though */ 2728109998Smarkm ret->rstate=s->rstate; 2729109998Smarkm ret->init_num = 0; /* would have to copy ret->init_buf, ret->init_msg, ret->init_num, ret->init_off */ 2730109998Smarkm ret->hit=s->hit; 273155714Skris 2732160814Ssimon X509_VERIFY_PARAM_inherit(ret->param, s->param); 2733160814Ssimon 273455714Skris /* dup the cipher_list and cipher_list_by_id stacks */ 273555714Skris if (s->cipher_list != NULL) 273655714Skris { 273755714Skris if ((ret->cipher_list=sk_SSL_CIPHER_dup(s->cipher_list)) == NULL) 273855714Skris goto err; 273955714Skris } 274055714Skris if (s->cipher_list_by_id != NULL) 274155714Skris if ((ret->cipher_list_by_id=sk_SSL_CIPHER_dup(s->cipher_list_by_id)) 274255714Skris == NULL) 274355714Skris goto err; 274455714Skris 274555714Skris /* Dup the client_CA list */ 274655714Skris if (s->client_CA != NULL) 274755714Skris { 274855714Skris if ((sk=sk_X509_NAME_dup(s->client_CA)) == NULL) goto err; 274955714Skris ret->client_CA=sk; 275055714Skris for (i=0; i<sk_X509_NAME_num(sk); i++) 275155714Skris { 275255714Skris xn=sk_X509_NAME_value(sk,i); 275355714Skris if (sk_X509_NAME_set(sk,i,X509_NAME_dup(xn)) == NULL) 275455714Skris { 275555714Skris X509_NAME_free(xn); 275655714Skris goto err; 275755714Skris } 275855714Skris } 275955714Skris } 276055714Skris 276155714Skris if (0) 276255714Skris { 276355714Skriserr: 276455714Skris if (ret != NULL) SSL_free(ret); 276555714Skris ret=NULL; 276655714Skris } 276755714Skris return(ret); 276855714Skris } 276955714Skris 277055714Skrisvoid ssl_clear_cipher_ctx(SSL *s) 277155714Skris { 277255714Skris if (s->enc_read_ctx != NULL) 277355714Skris { 277455714Skris EVP_CIPHER_CTX_cleanup(s->enc_read_ctx); 277568651Skris OPENSSL_free(s->enc_read_ctx); 277655714Skris s->enc_read_ctx=NULL; 277755714Skris } 277855714Skris if (s->enc_write_ctx != NULL) 277955714Skris { 278055714Skris EVP_CIPHER_CTX_cleanup(s->enc_write_ctx); 278168651Skris OPENSSL_free(s->enc_write_ctx); 278255714Skris s->enc_write_ctx=NULL; 278355714Skris } 2784160814Ssimon#ifndef OPENSSL_NO_COMP 278555714Skris if (s->expand != NULL) 278655714Skris { 278755714Skris COMP_CTX_free(s->expand); 278855714Skris s->expand=NULL; 278955714Skris } 279055714Skris if (s->compress != NULL) 279155714Skris { 279255714Skris COMP_CTX_free(s->compress); 279355714Skris s->compress=NULL; 279455714Skris } 2795160814Ssimon#endif 279655714Skris } 279755714Skris 279855714Skris/* Fix this function so that it takes an optional type parameter */ 2799160814SsimonX509 *SSL_get_certificate(const SSL *s) 280055714Skris { 2801264331Sjkim if (s->cert != NULL) 280255714Skris return(s->cert->key->x509); 280355714Skris else 280455714Skris return(NULL); 280555714Skris } 280655714Skris 280755714Skris/* Fix this function so that it takes an optional type parameter */ 280855714SkrisEVP_PKEY *SSL_get_privatekey(SSL *s) 280955714Skris { 281055714Skris if (s->cert != NULL) 281155714Skris return(s->cert->key->privatekey); 281255714Skris else 281355714Skris return(NULL); 281455714Skris } 281555714Skris 2816238405Sjkimconst SSL_CIPHER *SSL_get_current_cipher(const SSL *s) 281755714Skris { 281855714Skris if ((s->session != NULL) && (s->session->cipher != NULL)) 281955714Skris return(s->session->cipher); 282055714Skris return(NULL); 282155714Skris } 2822160814Ssimon#ifdef OPENSSL_NO_COMP 2823160814Ssimonconst void *SSL_get_current_compression(SSL *s) 2824160814Ssimon { 2825160814Ssimon return NULL; 2826160814Ssimon } 2827160814Ssimonconst void *SSL_get_current_expansion(SSL *s) 2828160814Ssimon { 2829160814Ssimon return NULL; 2830160814Ssimon } 2831160814Ssimon#else 283255714Skris 2833160814Ssimonconst COMP_METHOD *SSL_get_current_compression(SSL *s) 2834160814Ssimon { 2835160814Ssimon if (s->compress != NULL) 2836160814Ssimon return(s->compress->meth); 2837160814Ssimon return(NULL); 2838160814Ssimon } 2839160814Ssimon 2840160814Ssimonconst COMP_METHOD *SSL_get_current_expansion(SSL *s) 2841160814Ssimon { 2842160814Ssimon if (s->expand != NULL) 2843160814Ssimon return(s->expand->meth); 2844160814Ssimon return(NULL); 2845160814Ssimon } 2846160814Ssimon#endif 2847160814Ssimon 284855714Skrisint ssl_init_wbio_buffer(SSL *s,int push) 284955714Skris { 285055714Skris BIO *bbio; 285155714Skris 285255714Skris if (s->bbio == NULL) 285355714Skris { 285455714Skris bbio=BIO_new(BIO_f_buffer()); 285555714Skris if (bbio == NULL) return(0); 285655714Skris s->bbio=bbio; 285755714Skris } 285855714Skris else 285955714Skris { 286055714Skris bbio=s->bbio; 286155714Skris if (s->bbio == s->wbio) 286255714Skris s->wbio=BIO_pop(s->wbio); 286355714Skris } 286455714Skris (void)BIO_reset(bbio); 286555714Skris/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */ 286655714Skris if (!BIO_set_read_buffer_size(bbio,1)) 286755714Skris { 286855714Skris SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB); 286955714Skris return(0); 287055714Skris } 287155714Skris if (push) 287255714Skris { 287355714Skris if (s->wbio != bbio) 287455714Skris s->wbio=BIO_push(bbio,s->wbio); 287555714Skris } 287655714Skris else 287755714Skris { 287855714Skris if (s->wbio == bbio) 287955714Skris s->wbio=BIO_pop(bbio); 288055714Skris } 288155714Skris return(1); 288255714Skris } 288355714Skris 288455714Skrisvoid ssl_free_wbio_buffer(SSL *s) 288555714Skris { 288655714Skris if (s->bbio == NULL) return; 288755714Skris 288855714Skris if (s->bbio == s->wbio) 288955714Skris { 289055714Skris /* remove buffering */ 289168651Skris s->wbio=BIO_pop(s->wbio); 289268651Skris#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */ 289368651Skris assert(s->wbio != NULL); 2894238405Sjkim#endif 289568651Skris } 289655714Skris BIO_free(s->bbio); 289755714Skris s->bbio=NULL; 289855714Skris } 289955714Skris 290055714Skrisvoid SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode) 290155714Skris { 290255714Skris ctx->quiet_shutdown=mode; 290355714Skris } 290455714Skris 2905160814Ssimonint SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) 290655714Skris { 290755714Skris return(ctx->quiet_shutdown); 290855714Skris } 290955714Skris 291055714Skrisvoid SSL_set_quiet_shutdown(SSL *s,int mode) 291155714Skris { 291255714Skris s->quiet_shutdown=mode; 291355714Skris } 291455714Skris 2915160814Ssimonint SSL_get_quiet_shutdown(const SSL *s) 291655714Skris { 291755714Skris return(s->quiet_shutdown); 291855714Skris } 291955714Skris 292055714Skrisvoid SSL_set_shutdown(SSL *s,int mode) 292155714Skris { 292255714Skris s->shutdown=mode; 292355714Skris } 292455714Skris 2925160814Ssimonint SSL_get_shutdown(const SSL *s) 292655714Skris { 292755714Skris return(s->shutdown); 292855714Skris } 292955714Skris 2930160814Ssimonint SSL_version(const SSL *s) 293155714Skris { 293255714Skris return(s->version); 293355714Skris } 293455714Skris 2935160814SsimonSSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) 293655714Skris { 293755714Skris return(ssl->ctx); 293855714Skris } 293955714Skris 2940194206SsimonSSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx) 2941194206Ssimon { 2942194206Ssimon if (ssl->ctx == ctx) 2943194206Ssimon return ssl->ctx; 2944194206Ssimon#ifndef OPENSSL_NO_TLSEXT 2945194206Ssimon if (ctx == NULL) 2946194206Ssimon ctx = ssl->initial_ctx; 2947194206Ssimon#endif 2948194206Ssimon if (ssl->cert != NULL) 2949194206Ssimon ssl_cert_free(ssl->cert); 2950194206Ssimon ssl->cert = ssl_cert_dup(ctx->cert); 2951194206Ssimon CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX); 2952194206Ssimon if (ssl->ctx != NULL) 2953194206Ssimon SSL_CTX_free(ssl->ctx); /* decrement reference count */ 2954194206Ssimon ssl->ctx = ctx; 2955194206Ssimon return(ssl->ctx); 2956194206Ssimon } 2957194206Ssimon 2958109998Smarkm#ifndef OPENSSL_NO_STDIO 295955714Skrisint SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) 296055714Skris { 296155714Skris return(X509_STORE_set_default_paths(ctx->cert_store)); 296255714Skris } 296355714Skris 296455714Skrisint SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile, 296555714Skris const char *CApath) 296655714Skris { 2967160814Ssimon return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath)); 296855714Skris } 296955714Skris#endif 297055714Skris 2971109998Smarkmvoid SSL_set_info_callback(SSL *ssl, 2972167612Ssimon void (*cb)(const SSL *ssl,int type,int val)) 297355714Skris { 297455714Skris ssl->info_callback=cb; 297555714Skris } 297655714Skris 2977160814Ssimon/* One compiler (Diab DCC) doesn't like argument names in returned 2978160814Ssimon function pointer. */ 2979238405Sjkimvoid (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/) 298055714Skris { 2981109998Smarkm return ssl->info_callback; 298255714Skris } 298355714Skris 2984160814Ssimonint SSL_state(const SSL *ssl) 298555714Skris { 298655714Skris return(ssl->state); 298755714Skris } 298855714Skris 2989238405Sjkimvoid SSL_set_state(SSL *ssl, int state) 2990238405Sjkim { 2991238405Sjkim ssl->state = state; 2992238405Sjkim } 2993238405Sjkim 299455714Skrisvoid SSL_set_verify_result(SSL *ssl,long arg) 299555714Skris { 299655714Skris ssl->verify_result=arg; 299755714Skris } 299855714Skris 2999160814Ssimonlong SSL_get_verify_result(const SSL *ssl) 300055714Skris { 300155714Skris return(ssl->verify_result); 300255714Skris } 300355714Skris 300459191Skrisint SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func, 300559191Skris CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func) 300655714Skris { 3007109998Smarkm return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, 3008109998Smarkm new_func, dup_func, free_func); 300955714Skris } 301055714Skris 301155714Skrisint SSL_set_ex_data(SSL *s,int idx,void *arg) 301255714Skris { 301355714Skris return(CRYPTO_set_ex_data(&s->ex_data,idx,arg)); 301455714Skris } 301555714Skris 3016160814Ssimonvoid *SSL_get_ex_data(const SSL *s,int idx) 301755714Skris { 301855714Skris return(CRYPTO_get_ex_data(&s->ex_data,idx)); 301955714Skris } 302055714Skris 302159191Skrisint SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func, 302259191Skris CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func) 302355714Skris { 3024109998Smarkm return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, 3025109998Smarkm new_func, dup_func, free_func); 302655714Skris } 302755714Skris 302855714Skrisint SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg) 302955714Skris { 303055714Skris return(CRYPTO_set_ex_data(&s->ex_data,idx,arg)); 303155714Skris } 303255714Skris 3033160814Ssimonvoid *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx) 303455714Skris { 303555714Skris return(CRYPTO_get_ex_data(&s->ex_data,idx)); 303655714Skris } 303755714Skris 303855714Skrisint ssl_ok(SSL *s) 303955714Skris { 304055714Skris return(1); 304155714Skris } 304255714Skris 3043160814SsimonX509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) 304455714Skris { 304555714Skris return(ctx->cert_store); 304655714Skris } 304755714Skris 304855714Skrisvoid SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store) 304955714Skris { 305055714Skris if (ctx->cert_store != NULL) 305155714Skris X509_STORE_free(ctx->cert_store); 305255714Skris ctx->cert_store=store; 305355714Skris } 305455714Skris 3055160814Ssimonint SSL_want(const SSL *s) 305655714Skris { 305755714Skris return(s->rwstate); 305855714Skris } 305955714Skris 306055714Skris/*! 306155714Skris * \brief Set the callback for generating temporary RSA keys. 306255714Skris * \param ctx the SSL context. 306355714Skris * \param cb the callback 306455714Skris */ 306555714Skris 3066109998Smarkm#ifndef OPENSSL_NO_RSA 306755714Skrisvoid SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl, 306855714Skris int is_export, 306955714Skris int keylength)) 307059191Skris { 3071160814Ssimon SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); 307259191Skris } 307355714Skris 307459191Skrisvoid SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl, 307559191Skris int is_export, 307659191Skris int keylength)) 307759191Skris { 3078160814Ssimon SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); 307959191Skris } 308055714Skris#endif 308155714Skris 308255714Skris#ifdef DOXYGEN 308355714Skris/*! 308455714Skris * \brief The RSA temporary key callback function. 308555714Skris * \param ssl the SSL session. 308655714Skris * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite. 308755714Skris * \param keylength if \c is_export is \c TRUE, then \c keylength is the size 308855714Skris * of the required key in bits. 308955714Skris * \return the temporary RSA key. 309055714Skris * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback 309155714Skris */ 309255714Skris 309355714SkrisRSA *cb(SSL *ssl,int is_export,int keylength) 309455714Skris {} 309555714Skris#endif 309655714Skris 309755714Skris/*! 309855714Skris * \brief Set the callback for generating temporary DH keys. 309955714Skris * \param ctx the SSL context. 310055714Skris * \param dh the callback 310155714Skris */ 310255714Skris 3103109998Smarkm#ifndef OPENSSL_NO_DH 310455714Skrisvoid SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export, 3105238405Sjkim int keylength)) 3106109998Smarkm { 3107160814Ssimon SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); 3108109998Smarkm } 310955714Skris 311055714Skrisvoid SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export, 3111238405Sjkim int keylength)) 3112109998Smarkm { 3113160814Ssimon SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); 3114109998Smarkm } 311555714Skris#endif 311655714Skris 3117160814Ssimon#ifndef OPENSSL_NO_ECDH 3118160814Ssimonvoid SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export, 3119238405Sjkim int keylength)) 3120160814Ssimon { 3121160814Ssimon SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh); 3122160814Ssimon } 3123109998Smarkm 3124160814Ssimonvoid SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export, 3125238405Sjkim int keylength)) 3126160814Ssimon { 3127160814Ssimon SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh); 3128160814Ssimon } 3129160814Ssimon#endif 3130160814Ssimon 3131238405Sjkim#ifndef OPENSSL_NO_PSK 3132238405Sjkimint SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) 3133238405Sjkim { 3134238405Sjkim if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) 3135238405Sjkim { 3136238405Sjkim SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG); 3137238405Sjkim return 0; 3138238405Sjkim } 3139238405Sjkim if (ctx->psk_identity_hint != NULL) 3140238405Sjkim OPENSSL_free(ctx->psk_identity_hint); 3141238405Sjkim if (identity_hint != NULL) 3142238405Sjkim { 3143238405Sjkim ctx->psk_identity_hint = BUF_strdup(identity_hint); 3144238405Sjkim if (ctx->psk_identity_hint == NULL) 3145238405Sjkim return 0; 3146238405Sjkim } 3147238405Sjkim else 3148238405Sjkim ctx->psk_identity_hint = NULL; 3149238405Sjkim return 1; 3150238405Sjkim } 3151160814Ssimon 3152238405Sjkimint SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) 3153238405Sjkim { 3154238405Sjkim if (s == NULL) 3155238405Sjkim return 0; 3156238405Sjkim 3157238405Sjkim if (s->session == NULL) 3158238405Sjkim return 1; /* session not created yet, ignored */ 3159238405Sjkim 3160238405Sjkim if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) 3161238405Sjkim { 3162238405Sjkim SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG); 3163238405Sjkim return 0; 3164238405Sjkim } 3165238405Sjkim if (s->session->psk_identity_hint != NULL) 3166238405Sjkim OPENSSL_free(s->session->psk_identity_hint); 3167238405Sjkim if (identity_hint != NULL) 3168238405Sjkim { 3169238405Sjkim s->session->psk_identity_hint = BUF_strdup(identity_hint); 3170238405Sjkim if (s->session->psk_identity_hint == NULL) 3171238405Sjkim return 0; 3172238405Sjkim } 3173238405Sjkim else 3174238405Sjkim s->session->psk_identity_hint = NULL; 3175238405Sjkim return 1; 3176238405Sjkim } 3177238405Sjkim 3178238405Sjkimconst char *SSL_get_psk_identity_hint(const SSL *s) 3179238405Sjkim { 3180238405Sjkim if (s == NULL || s->session == NULL) 3181238405Sjkim return NULL; 3182238405Sjkim return(s->session->psk_identity_hint); 3183238405Sjkim } 3184238405Sjkim 3185238405Sjkimconst char *SSL_get_psk_identity(const SSL *s) 3186238405Sjkim { 3187238405Sjkim if (s == NULL || s->session == NULL) 3188238405Sjkim return NULL; 3189238405Sjkim return(s->session->psk_identity); 3190238405Sjkim } 3191238405Sjkim 3192238405Sjkimvoid SSL_set_psk_client_callback(SSL *s, 3193238405Sjkim unsigned int (*cb)(SSL *ssl, const char *hint, 3194238405Sjkim char *identity, unsigned int max_identity_len, unsigned char *psk, 3195238405Sjkim unsigned int max_psk_len)) 3196238405Sjkim { 3197238405Sjkim s->psk_client_callback = cb; 3198238405Sjkim } 3199238405Sjkim 3200238405Sjkimvoid SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, 3201238405Sjkim unsigned int (*cb)(SSL *ssl, const char *hint, 3202238405Sjkim char *identity, unsigned int max_identity_len, unsigned char *psk, 3203238405Sjkim unsigned int max_psk_len)) 3204238405Sjkim { 3205238405Sjkim ctx->psk_client_callback = cb; 3206238405Sjkim } 3207238405Sjkim 3208238405Sjkimvoid SSL_set_psk_server_callback(SSL *s, 3209238405Sjkim unsigned int (*cb)(SSL *ssl, const char *identity, 3210238405Sjkim unsigned char *psk, unsigned int max_psk_len)) 3211238405Sjkim { 3212238405Sjkim s->psk_server_callback = cb; 3213238405Sjkim } 3214238405Sjkim 3215238405Sjkimvoid SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, 3216238405Sjkim unsigned int (*cb)(SSL *ssl, const char *identity, 3217238405Sjkim unsigned char *psk, unsigned int max_psk_len)) 3218238405Sjkim { 3219238405Sjkim ctx->psk_server_callback = cb; 3220238405Sjkim } 3221238405Sjkim#endif 3222238405Sjkim 3223109998Smarkmvoid 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)) 3224109998Smarkm { 3225160814Ssimon SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); 3226109998Smarkm } 3227109998Smarkmvoid 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)) 3228109998Smarkm { 3229160814Ssimon SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); 3230109998Smarkm } 3231109998Smarkm 3232238405Sjkim/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer 3233238405Sjkim * vairable, freeing EVP_MD_CTX previously stored in that variable, if 3234238405Sjkim * any. If EVP_MD pointer is passed, initializes ctx with this md 3235238405Sjkim * Returns newly allocated ctx; 3236238405Sjkim */ 3237109998Smarkm 3238238405SjkimEVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md) 3239238405Sjkim{ 3240238405Sjkim ssl_clear_hash_ctx(hash); 3241238405Sjkim *hash = EVP_MD_CTX_create(); 3242238405Sjkim if (md) EVP_DigestInit_ex(*hash,md,NULL); 3243238405Sjkim return *hash; 3244238405Sjkim} 3245238405Sjkimvoid ssl_clear_hash_ctx(EVP_MD_CTX **hash) 3246238405Sjkim{ 3247109998Smarkm 3248238405Sjkim if (*hash) EVP_MD_CTX_destroy(*hash); 3249238405Sjkim *hash=NULL; 3250238405Sjkim} 3251238405Sjkim 3252238405Sjkimvoid SSL_set_debug(SSL *s, int debug) 3253238405Sjkim { 3254238405Sjkim s->debug = debug; 3255238405Sjkim } 3256238405Sjkim 3257238405Sjkimint SSL_cache_hit(SSL *s) 3258238405Sjkim { 3259238405Sjkim return s->hit; 3260238405Sjkim } 3261238405Sjkim 3262109998Smarkm#if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16) 326355714Skris#include "../crypto/bio/bss_file.c" 326455714Skris#endif 326555714Skris 326655714SkrisIMPLEMENT_STACK_OF(SSL_CIPHER) 326755714SkrisIMPLEMENT_STACK_OF(SSL_COMP) 3268238405SjkimIMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, 3269238405Sjkim ssl_cipher_id); 3270