ssl_lib.c revision 238405
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 600238405Sjkim if (s->srtp_profiles) 601238405Sjkim sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles); 602238405Sjkim 60368651Skris OPENSSL_free(s); 60455714Skris } 60555714Skris 60655714Skrisvoid SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio) 60755714Skris { 60855714Skris /* If the output buffering BIO is still in place, remove it 60955714Skris */ 61055714Skris if (s->bbio != NULL) 61155714Skris { 61255714Skris if (s->wbio == s->bbio) 61355714Skris { 61455714Skris s->wbio=s->wbio->next_bio; 61555714Skris s->bbio->next_bio=NULL; 61655714Skris } 61755714Skris } 61855714Skris if ((s->rbio != NULL) && (s->rbio != rbio)) 61955714Skris BIO_free_all(s->rbio); 62055714Skris if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio)) 62155714Skris BIO_free_all(s->wbio); 62255714Skris s->rbio=rbio; 62355714Skris s->wbio=wbio; 62455714Skris } 62555714Skris 626160814SsimonBIO *SSL_get_rbio(const SSL *s) 62755714Skris { return(s->rbio); } 62855714Skris 629160814SsimonBIO *SSL_get_wbio(const SSL *s) 63055714Skris { return(s->wbio); } 63155714Skris 632160814Ssimonint SSL_get_fd(const SSL *s) 63355714Skris { 63489837Skris return(SSL_get_rfd(s)); 63589837Skris } 63689837Skris 637160814Ssimonint SSL_get_rfd(const SSL *s) 63889837Skris { 63955714Skris int ret= -1; 64055714Skris BIO *b,*r; 64155714Skris 64255714Skris b=SSL_get_rbio(s); 64355714Skris r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR); 64455714Skris if (r != NULL) 64555714Skris BIO_get_fd(r,&ret); 64655714Skris return(ret); 64755714Skris } 64855714Skris 649160814Ssimonint SSL_get_wfd(const SSL *s) 65089837Skris { 65189837Skris int ret= -1; 65289837Skris BIO *b,*r; 65389837Skris 65489837Skris b=SSL_get_wbio(s); 65589837Skris r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR); 65689837Skris if (r != NULL) 65789837Skris BIO_get_fd(r,&ret); 65889837Skris return(ret); 65989837Skris } 66089837Skris 661109998Smarkm#ifndef OPENSSL_NO_SOCK 66255714Skrisint SSL_set_fd(SSL *s,int fd) 66355714Skris { 66455714Skris int ret=0; 66555714Skris BIO *bio=NULL; 66655714Skris 66755714Skris bio=BIO_new(BIO_s_socket()); 66855714Skris 66955714Skris if (bio == NULL) 67055714Skris { 67155714Skris SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB); 67255714Skris goto err; 67355714Skris } 67455714Skris BIO_set_fd(bio,fd,BIO_NOCLOSE); 67555714Skris SSL_set_bio(s,bio,bio); 67655714Skris ret=1; 67755714Skriserr: 67855714Skris return(ret); 67955714Skris } 68055714Skris 68155714Skrisint SSL_set_wfd(SSL *s,int fd) 68255714Skris { 68355714Skris int ret=0; 68455714Skris BIO *bio=NULL; 68555714Skris 68655714Skris if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET) 68755714Skris || ((int)BIO_get_fd(s->rbio,NULL) != fd)) 68855714Skris { 68955714Skris bio=BIO_new(BIO_s_socket()); 69055714Skris 69155714Skris if (bio == NULL) 69255714Skris { SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; } 69355714Skris BIO_set_fd(bio,fd,BIO_NOCLOSE); 69455714Skris SSL_set_bio(s,SSL_get_rbio(s),bio); 69555714Skris } 69655714Skris else 69755714Skris SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s)); 69855714Skris ret=1; 69955714Skriserr: 70055714Skris return(ret); 70155714Skris } 70255714Skris 70355714Skrisint SSL_set_rfd(SSL *s,int fd) 70455714Skris { 70555714Skris int ret=0; 70655714Skris BIO *bio=NULL; 70755714Skris 70855714Skris if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET) 70955714Skris || ((int)BIO_get_fd(s->wbio,NULL) != fd)) 71055714Skris { 71155714Skris bio=BIO_new(BIO_s_socket()); 71255714Skris 71355714Skris if (bio == NULL) 71455714Skris { 71555714Skris SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB); 71655714Skris goto err; 71755714Skris } 71855714Skris BIO_set_fd(bio,fd,BIO_NOCLOSE); 71955714Skris SSL_set_bio(s,bio,SSL_get_wbio(s)); 72055714Skris } 72155714Skris else 72255714Skris SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s)); 72355714Skris ret=1; 72455714Skriserr: 72555714Skris return(ret); 72655714Skris } 72755714Skris#endif 72855714Skris 72959191Skris 73059191Skris/* return length of latest Finished message we sent, copy to 'buf' */ 731160814Ssimonsize_t SSL_get_finished(const SSL *s, void *buf, size_t count) 73259191Skris { 73359191Skris size_t ret = 0; 73459191Skris 73559191Skris if (s->s3 != NULL) 73659191Skris { 73759191Skris ret = s->s3->tmp.finish_md_len; 73859191Skris if (count > ret) 73959191Skris count = ret; 74059191Skris memcpy(buf, s->s3->tmp.finish_md, count); 74159191Skris } 74259191Skris return ret; 74359191Skris } 74459191Skris 74559191Skris/* return length of latest Finished message we expected, copy to 'buf' */ 746160814Ssimonsize_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) 74759191Skris { 74859191Skris size_t ret = 0; 74959191Skris 75059191Skris if (s->s3 != NULL) 75159191Skris { 75259191Skris ret = s->s3->tmp.peer_finish_md_len; 75359191Skris if (count > ret) 75459191Skris count = ret; 75559191Skris memcpy(buf, s->s3->tmp.peer_finish_md, count); 75659191Skris } 75759191Skris return ret; 75859191Skris } 75959191Skris 76059191Skris 761160814Ssimonint SSL_get_verify_mode(const SSL *s) 76255714Skris { 76355714Skris return(s->verify_mode); 76455714Skris } 76555714Skris 766160814Ssimonint SSL_get_verify_depth(const SSL *s) 76755714Skris { 768160814Ssimon return X509_VERIFY_PARAM_get_depth(s->param); 76955714Skris } 77055714Skris 771160814Ssimonint (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *) 77255714Skris { 77355714Skris return(s->verify_callback); 77455714Skris } 77555714Skris 776160814Ssimonint SSL_CTX_get_verify_mode(const SSL_CTX *ctx) 77755714Skris { 77855714Skris return(ctx->verify_mode); 77955714Skris } 78055714Skris 781160814Ssimonint SSL_CTX_get_verify_depth(const SSL_CTX *ctx) 78255714Skris { 783160814Ssimon return X509_VERIFY_PARAM_get_depth(ctx->param); 78455714Skris } 78555714Skris 786160814Ssimonint (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *) 78755714Skris { 78855714Skris return(ctx->default_verify_callback); 78955714Skris } 79055714Skris 79155714Skrisvoid SSL_set_verify(SSL *s,int mode, 79255714Skris int (*callback)(int ok,X509_STORE_CTX *ctx)) 79355714Skris { 79455714Skris s->verify_mode=mode; 79555714Skris if (callback != NULL) 79655714Skris s->verify_callback=callback; 79755714Skris } 79855714Skris 79955714Skrisvoid SSL_set_verify_depth(SSL *s,int depth) 80055714Skris { 801160814Ssimon X509_VERIFY_PARAM_set_depth(s->param, depth); 80255714Skris } 80355714Skris 80455714Skrisvoid SSL_set_read_ahead(SSL *s,int yes) 80555714Skris { 80655714Skris s->read_ahead=yes; 80755714Skris } 80855714Skris 809160814Ssimonint SSL_get_read_ahead(const SSL *s) 81055714Skris { 81155714Skris return(s->read_ahead); 81255714Skris } 81355714Skris 814160814Ssimonint SSL_pending(const SSL *s) 81555714Skris { 816109998Smarkm /* SSL_pending cannot work properly if read-ahead is enabled 817109998Smarkm * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), 818109998Smarkm * and it is impossible to fix since SSL_pending cannot report 819109998Smarkm * errors that may be observed while scanning the new data. 820109998Smarkm * (Note that SSL_pending() is often used as a boolean value, 821109998Smarkm * so we'd better not return -1.) 822109998Smarkm */ 82355714Skris return(s->method->ssl_pending(s)); 82455714Skris } 82555714Skris 826160814SsimonX509 *SSL_get_peer_certificate(const SSL *s) 82755714Skris { 82855714Skris X509 *r; 82955714Skris 83055714Skris if ((s == NULL) || (s->session == NULL)) 83155714Skris r=NULL; 83255714Skris else 83355714Skris r=s->session->peer; 83455714Skris 83555714Skris if (r == NULL) return(r); 83655714Skris 83755714Skris CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509); 83855714Skris 83955714Skris return(r); 84055714Skris } 84155714Skris 842160814SsimonSTACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s) 84355714Skris { 84455714Skris STACK_OF(X509) *r; 84555714Skris 84655714Skris if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL)) 84755714Skris r=NULL; 84855714Skris else 84955714Skris r=s->session->sess_cert->cert_chain; 85055714Skris 85159191Skris /* If we are a client, cert_chain includes the peer's own 85259191Skris * certificate; if we are a server, it does not. */ 85359191Skris 85455714Skris return(r); 85555714Skris } 85655714Skris 85755714Skris/* Now in theory, since the calling process own 't' it should be safe to 85855714Skris * modify. We need to be able to read f without being hassled */ 859160814Ssimonvoid SSL_copy_session_id(SSL *t,const SSL *f) 86055714Skris { 86155714Skris CERT *tmp; 86255714Skris 86355714Skris /* Do we need to to SSL locking? */ 86455714Skris SSL_set_session(t,SSL_get_session(f)); 86555714Skris 86655714Skris /* what if we are setup as SSLv2 but want to talk SSLv3 or 86755714Skris * vice-versa */ 86855714Skris if (t->method != f->method) 86955714Skris { 87055714Skris t->method->ssl_free(t); /* cleanup current */ 87155714Skris t->method=f->method; /* change method */ 87255714Skris t->method->ssl_new(t); /* setup new */ 87355714Skris } 87455714Skris 87555714Skris tmp=t->cert; 87655714Skris if (f->cert != NULL) 87755714Skris { 87855714Skris CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT); 87955714Skris t->cert=f->cert; 88055714Skris } 88155714Skris else 88255714Skris t->cert=NULL; 88355714Skris if (tmp != NULL) ssl_cert_free(tmp); 88455714Skris SSL_set_session_id_context(t,f->sid_ctx,f->sid_ctx_length); 88555714Skris } 88655714Skris 88755714Skris/* Fix this so it checks all the valid key/cert options */ 888160814Ssimonint SSL_CTX_check_private_key(const SSL_CTX *ctx) 88955714Skris { 89055714Skris if ( (ctx == NULL) || 89155714Skris (ctx->cert == NULL) || 89255714Skris (ctx->cert->key->x509 == NULL)) 89355714Skris { 89455714Skris SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED); 89555714Skris return(0); 89655714Skris } 89755714Skris if (ctx->cert->key->privatekey == NULL) 89855714Skris { 89955714Skris SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED); 90055714Skris return(0); 90155714Skris } 90255714Skris return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey)); 90355714Skris } 90455714Skris 90555714Skris/* Fix this function so that it takes an optional type parameter */ 906160814Ssimonint SSL_check_private_key(const SSL *ssl) 90755714Skris { 90855714Skris if (ssl == NULL) 90955714Skris { 91055714Skris SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER); 91155714Skris return(0); 91255714Skris } 91355714Skris if (ssl->cert == NULL) 91455714Skris { 915238405Sjkim SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED); 91655714Skris return 0; 91755714Skris } 91855714Skris if (ssl->cert->key->x509 == NULL) 91955714Skris { 92055714Skris SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED); 92155714Skris return(0); 92255714Skris } 92355714Skris if (ssl->cert->key->privatekey == NULL) 92455714Skris { 92555714Skris SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED); 92655714Skris return(0); 92755714Skris } 92855714Skris return(X509_check_private_key(ssl->cert->key->x509, 92955714Skris ssl->cert->key->privatekey)); 93055714Skris } 93155714Skris 93255714Skrisint SSL_accept(SSL *s) 93355714Skris { 93455714Skris if (s->handshake_func == 0) 93555714Skris /* Not properly initialized yet */ 93655714Skris SSL_set_accept_state(s); 93755714Skris 93855714Skris return(s->method->ssl_accept(s)); 93955714Skris } 94055714Skris 94155714Skrisint SSL_connect(SSL *s) 94255714Skris { 94355714Skris if (s->handshake_func == 0) 94455714Skris /* Not properly initialized yet */ 94555714Skris SSL_set_connect_state(s); 94655714Skris 94755714Skris return(s->method->ssl_connect(s)); 94855714Skris } 94955714Skris 950160814Ssimonlong SSL_get_default_timeout(const SSL *s) 95155714Skris { 95255714Skris return(s->method->get_timeout()); 95355714Skris } 95455714Skris 95576866Skrisint SSL_read(SSL *s,void *buf,int num) 95655714Skris { 95755714Skris if (s->handshake_func == 0) 95855714Skris { 95955714Skris SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED); 96055714Skris return -1; 96155714Skris } 96255714Skris 96355714Skris if (s->shutdown & SSL_RECEIVED_SHUTDOWN) 96455714Skris { 96555714Skris s->rwstate=SSL_NOTHING; 96655714Skris return(0); 96755714Skris } 96855714Skris return(s->method->ssl_read(s,buf,num)); 96955714Skris } 97055714Skris 97176866Skrisint SSL_peek(SSL *s,void *buf,int num) 97255714Skris { 97376866Skris if (s->handshake_func == 0) 97476866Skris { 975160814Ssimon SSLerr(SSL_F_SSL_PEEK, SSL_R_UNINITIALIZED); 97676866Skris return -1; 97776866Skris } 97876866Skris 97955714Skris if (s->shutdown & SSL_RECEIVED_SHUTDOWN) 98055714Skris { 98155714Skris return(0); 98255714Skris } 98355714Skris return(s->method->ssl_peek(s,buf,num)); 98455714Skris } 98555714Skris 98676866Skrisint SSL_write(SSL *s,const void *buf,int num) 98755714Skris { 98855714Skris if (s->handshake_func == 0) 98955714Skris { 99055714Skris SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED); 99155714Skris return -1; 99255714Skris } 99355714Skris 99455714Skris if (s->shutdown & SSL_SENT_SHUTDOWN) 99555714Skris { 99655714Skris s->rwstate=SSL_NOTHING; 99755714Skris SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN); 99855714Skris return(-1); 99955714Skris } 100055714Skris return(s->method->ssl_write(s,buf,num)); 100155714Skris } 100255714Skris 100355714Skrisint SSL_shutdown(SSL *s) 100455714Skris { 100555714Skris /* Note that this function behaves differently from what one might 100655714Skris * expect. Return values are 0 for no success (yet), 100755714Skris * 1 for success; but calling it once is usually not enough, 100855714Skris * even if blocking I/O is used (see ssl3_shutdown). 100955714Skris */ 101055714Skris 101155714Skris if (s->handshake_func == 0) 101255714Skris { 101355714Skris SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED); 101455714Skris return -1; 101555714Skris } 101655714Skris 101755714Skris if ((s != NULL) && !SSL_in_init(s)) 101855714Skris return(s->method->ssl_shutdown(s)); 101955714Skris else 102055714Skris return(1); 102155714Skris } 102255714Skris 102355714Skrisint SSL_renegotiate(SSL *s) 102455714Skris { 1025238405Sjkim if (s->renegotiate == 0) 1026238405Sjkim s->renegotiate=1; 1027238405Sjkim 1028238405Sjkim s->new_session=1; 1029238405Sjkim 103055714Skris return(s->method->ssl_renegotiate(s)); 103155714Skris } 103255714Skris 1033238405Sjkimint SSL_renegotiate_abbreviated(SSL *s) 1034238405Sjkim { 1035238405Sjkim if (s->renegotiate == 0) 1036238405Sjkim s->renegotiate=1; 1037238405Sjkim 1038238405Sjkim s->new_session=0; 1039238405Sjkim 1040238405Sjkim return(s->method->ssl_renegotiate(s)); 1041238405Sjkim } 1042238405Sjkim 1043109998Smarkmint SSL_renegotiate_pending(SSL *s) 104455714Skris { 1045109998Smarkm /* becomes true when negotiation is requested; 1046109998Smarkm * false again once a handshake has finished */ 1047238405Sjkim return (s->renegotiate != 0); 1048109998Smarkm } 1049109998Smarkm 1050109998Smarkmlong SSL_ctrl(SSL *s,int cmd,long larg,void *parg) 1051109998Smarkm { 105255714Skris long l; 105355714Skris 105455714Skris switch (cmd) 105555714Skris { 105655714Skris case SSL_CTRL_GET_READ_AHEAD: 105755714Skris return(s->read_ahead); 105855714Skris case SSL_CTRL_SET_READ_AHEAD: 105955714Skris l=s->read_ahead; 106055714Skris s->read_ahead=larg; 106155714Skris return(l); 1062109998Smarkm 1063109998Smarkm case SSL_CTRL_SET_MSG_CALLBACK_ARG: 1064109998Smarkm s->msg_callback_arg = parg; 1065109998Smarkm return 1; 1066109998Smarkm 106755714Skris case SSL_CTRL_OPTIONS: 106855714Skris return(s->options|=larg); 1069205128Ssimon case SSL_CTRL_CLEAR_OPTIONS: 1070205128Ssimon return(s->options&=~larg); 107155714Skris case SSL_CTRL_MODE: 107255714Skris return(s->mode|=larg); 1073205128Ssimon case SSL_CTRL_CLEAR_MODE: 1074205128Ssimon return(s->mode &=~larg); 1075109998Smarkm case SSL_CTRL_GET_MAX_CERT_LIST: 1076109998Smarkm return(s->max_cert_list); 1077109998Smarkm case SSL_CTRL_SET_MAX_CERT_LIST: 1078109998Smarkm l=s->max_cert_list; 1079109998Smarkm s->max_cert_list=larg; 1080109998Smarkm return(l); 1081160814Ssimon case SSL_CTRL_SET_MTU: 1082237657Sjkim#ifndef OPENSSL_NO_DTLS1 1083237657Sjkim if (larg < (long)dtls1_min_mtu()) 1084237657Sjkim return 0; 1085237657Sjkim#endif 1086237657Sjkim 1087205128Ssimon if (SSL_version(s) == DTLS1_VERSION || 1088205128Ssimon SSL_version(s) == DTLS1_BAD_VER) 1089160814Ssimon { 1090160814Ssimon s->d1->mtu = larg; 1091160814Ssimon return larg; 1092160814Ssimon } 1093160814Ssimon return 0; 1094238405Sjkim case SSL_CTRL_SET_MAX_SEND_FRAGMENT: 1095238405Sjkim if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) 1096238405Sjkim return 0; 1097238405Sjkim s->max_send_fragment = larg; 1098238405Sjkim return 1; 1099205128Ssimon case SSL_CTRL_GET_RI_SUPPORT: 1100205128Ssimon if (s->s3) 1101205128Ssimon return s->s3->send_connection_binding; 1102205128Ssimon else return 0; 110355714Skris default: 110455714Skris return(s->method->ssl_ctrl(s,cmd,larg,parg)); 110555714Skris } 110655714Skris } 110755714Skris 1108160814Ssimonlong SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) 110959191Skris { 111059191Skris switch(cmd) 111159191Skris { 1112109998Smarkm case SSL_CTRL_SET_MSG_CALLBACK: 1113109998Smarkm s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp); 1114109998Smarkm return 1; 1115109998Smarkm 111659191Skris default: 111759191Skris return(s->method->ssl_callback_ctrl(s,cmd,fp)); 111859191Skris } 111959191Skris } 112059191Skris 1121238405SjkimLHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) 112259191Skris { 112359191Skris return ctx->sessions; 112459191Skris } 112559191Skris 1126109998Smarkmlong SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg) 112755714Skris { 112855714Skris long l; 112955714Skris 113055714Skris switch (cmd) 113155714Skris { 113255714Skris case SSL_CTRL_GET_READ_AHEAD: 113355714Skris return(ctx->read_ahead); 113455714Skris case SSL_CTRL_SET_READ_AHEAD: 113555714Skris l=ctx->read_ahead; 113655714Skris ctx->read_ahead=larg; 113755714Skris return(l); 1138109998Smarkm 1139109998Smarkm case SSL_CTRL_SET_MSG_CALLBACK_ARG: 1140109998Smarkm ctx->msg_callback_arg = parg; 1141109998Smarkm return 1; 114255714Skris 1143109998Smarkm case SSL_CTRL_GET_MAX_CERT_LIST: 1144109998Smarkm return(ctx->max_cert_list); 1145109998Smarkm case SSL_CTRL_SET_MAX_CERT_LIST: 1146109998Smarkm l=ctx->max_cert_list; 1147109998Smarkm ctx->max_cert_list=larg; 1148109998Smarkm return(l); 1149109998Smarkm 115055714Skris case SSL_CTRL_SET_SESS_CACHE_SIZE: 115155714Skris l=ctx->session_cache_size; 115255714Skris ctx->session_cache_size=larg; 115355714Skris return(l); 115455714Skris case SSL_CTRL_GET_SESS_CACHE_SIZE: 115555714Skris return(ctx->session_cache_size); 115655714Skris case SSL_CTRL_SET_SESS_CACHE_MODE: 115755714Skris l=ctx->session_cache_mode; 115855714Skris ctx->session_cache_mode=larg; 115955714Skris return(l); 116055714Skris case SSL_CTRL_GET_SESS_CACHE_MODE: 116155714Skris return(ctx->session_cache_mode); 116255714Skris 116355714Skris case SSL_CTRL_SESS_NUMBER: 1164238405Sjkim return(lh_SSL_SESSION_num_items(ctx->sessions)); 116555714Skris case SSL_CTRL_SESS_CONNECT: 116655714Skris return(ctx->stats.sess_connect); 116755714Skris case SSL_CTRL_SESS_CONNECT_GOOD: 116855714Skris return(ctx->stats.sess_connect_good); 116955714Skris case SSL_CTRL_SESS_CONNECT_RENEGOTIATE: 117055714Skris return(ctx->stats.sess_connect_renegotiate); 117155714Skris case SSL_CTRL_SESS_ACCEPT: 117255714Skris return(ctx->stats.sess_accept); 117355714Skris case SSL_CTRL_SESS_ACCEPT_GOOD: 117455714Skris return(ctx->stats.sess_accept_good); 117555714Skris case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE: 117655714Skris return(ctx->stats.sess_accept_renegotiate); 117755714Skris case SSL_CTRL_SESS_HIT: 117855714Skris return(ctx->stats.sess_hit); 117955714Skris case SSL_CTRL_SESS_CB_HIT: 118055714Skris return(ctx->stats.sess_cb_hit); 118155714Skris case SSL_CTRL_SESS_MISSES: 118255714Skris return(ctx->stats.sess_miss); 118355714Skris case SSL_CTRL_SESS_TIMEOUTS: 118455714Skris return(ctx->stats.sess_timeout); 118555714Skris case SSL_CTRL_SESS_CACHE_FULL: 118655714Skris return(ctx->stats.sess_cache_full); 118755714Skris case SSL_CTRL_OPTIONS: 118855714Skris return(ctx->options|=larg); 1189205128Ssimon case SSL_CTRL_CLEAR_OPTIONS: 1190205128Ssimon return(ctx->options&=~larg); 119155714Skris case SSL_CTRL_MODE: 119255714Skris return(ctx->mode|=larg); 1193205128Ssimon case SSL_CTRL_CLEAR_MODE: 1194205128Ssimon return(ctx->mode&=~larg); 1195238405Sjkim case SSL_CTRL_SET_MAX_SEND_FRAGMENT: 1196238405Sjkim if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) 1197238405Sjkim return 0; 1198238405Sjkim ctx->max_send_fragment = larg; 1199238405Sjkim return 1; 120055714Skris default: 120155714Skris return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg)); 120255714Skris } 120355714Skris } 120455714Skris 1205160814Ssimonlong SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) 120659191Skris { 120759191Skris switch(cmd) 120859191Skris { 1209109998Smarkm case SSL_CTRL_SET_MSG_CALLBACK: 1210109998Smarkm ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp); 1211109998Smarkm return 1; 1212109998Smarkm 121359191Skris default: 121459191Skris return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp)); 121559191Skris } 121659191Skris } 121759191Skris 121868651Skrisint ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b) 121955714Skris { 122055714Skris long l; 122155714Skris 122255714Skris l=a->id-b->id; 122355714Skris if (l == 0L) 122455714Skris return(0); 122555714Skris else 122655714Skris return((l > 0)?1:-1); 122755714Skris } 122855714Skris 122968651Skrisint ssl_cipher_ptr_id_cmp(const SSL_CIPHER * const *ap, 123068651Skris const SSL_CIPHER * const *bp) 123155714Skris { 123255714Skris long l; 123355714Skris 123455714Skris l=(*ap)->id-(*bp)->id; 123555714Skris if (l == 0L) 123655714Skris return(0); 123755714Skris else 123855714Skris return((l > 0)?1:-1); 123955714Skris } 124055714Skris 124155714Skris/** return a STACK of the ciphers available for the SSL and in order of 124255714Skris * preference */ 1243160814SsimonSTACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) 124455714Skris { 1245111147Snectar if (s != NULL) 124655714Skris { 1247111147Snectar if (s->cipher_list != NULL) 1248111147Snectar { 1249111147Snectar return(s->cipher_list); 1250111147Snectar } 1251111147Snectar else if ((s->ctx != NULL) && 1252111147Snectar (s->ctx->cipher_list != NULL)) 1253111147Snectar { 1254111147Snectar return(s->ctx->cipher_list); 1255111147Snectar } 125655714Skris } 125755714Skris return(NULL); 125855714Skris } 125955714Skris 126055714Skris/** return a STACK of the ciphers available for the SSL and in order of 126155714Skris * algorithm id */ 126255714SkrisSTACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) 126355714Skris { 1264111147Snectar if (s != NULL) 126555714Skris { 1266111147Snectar if (s->cipher_list_by_id != NULL) 1267111147Snectar { 1268111147Snectar return(s->cipher_list_by_id); 1269111147Snectar } 1270111147Snectar else if ((s->ctx != NULL) && 1271111147Snectar (s->ctx->cipher_list_by_id != NULL)) 1272111147Snectar { 1273111147Snectar return(s->ctx->cipher_list_by_id); 1274111147Snectar } 127555714Skris } 127655714Skris return(NULL); 127755714Skris } 127855714Skris 127955714Skris/** The old interface to get the same thing as SSL_get_ciphers() */ 1280160814Ssimonconst char *SSL_get_cipher_list(const SSL *s,int n) 128155714Skris { 128255714Skris SSL_CIPHER *c; 128355714Skris STACK_OF(SSL_CIPHER) *sk; 128455714Skris 128555714Skris if (s == NULL) return(NULL); 128655714Skris sk=SSL_get_ciphers(s); 128755714Skris if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n)) 128855714Skris return(NULL); 128955714Skris c=sk_SSL_CIPHER_value(sk,n); 129055714Skris if (c == NULL) return(NULL); 129155714Skris return(c->name); 129255714Skris } 129355714Skris 129459191Skris/** specify the ciphers to be used by default by the SSL_CTX */ 129559191Skrisint SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) 129655714Skris { 129755714Skris STACK_OF(SSL_CIPHER) *sk; 129855714Skris 129955714Skris sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list, 130055714Skris &ctx->cipher_list_by_id,str); 1301160814Ssimon /* ssl_create_cipher_list may return an empty stack if it 1302160814Ssimon * was unable to find a cipher matching the given rule string 1303160814Ssimon * (for example if the rule string specifies a cipher which 1304238405Sjkim * has been disabled). This is not an error as far as 1305238405Sjkim * ssl_create_cipher_list is concerned, and hence 1306160814Ssimon * ctx->cipher_list and ctx->cipher_list_by_id has been 1307160814Ssimon * updated. */ 1308160814Ssimon if (sk == NULL) 1309160814Ssimon return 0; 1310160814Ssimon else if (sk_SSL_CIPHER_num(sk) == 0) 1311160814Ssimon { 1312160814Ssimon SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH); 1313160814Ssimon return 0; 1314160814Ssimon } 1315160814Ssimon return 1; 131655714Skris } 131755714Skris 131855714Skris/** specify the ciphers to be used by the SSL */ 131959191Skrisint SSL_set_cipher_list(SSL *s,const char *str) 132055714Skris { 132155714Skris STACK_OF(SSL_CIPHER) *sk; 132255714Skris 132355714Skris sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list, 132455714Skris &s->cipher_list_by_id,str); 1325160814Ssimon /* see comment in SSL_CTX_set_cipher_list */ 1326160814Ssimon if (sk == NULL) 1327160814Ssimon return 0; 1328160814Ssimon else if (sk_SSL_CIPHER_num(sk) == 0) 1329160814Ssimon { 1330160814Ssimon SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH); 1331160814Ssimon return 0; 1332160814Ssimon } 1333160814Ssimon return 1; 133455714Skris } 133555714Skris 133655714Skris/* works well for SSLv2, not so good for SSLv3 */ 1337160814Ssimonchar *SSL_get_shared_ciphers(const SSL *s,char *buf,int len) 133855714Skris { 133955714Skris char *p; 134055714Skris STACK_OF(SSL_CIPHER) *sk; 134155714Skris SSL_CIPHER *c; 134255714Skris int i; 134355714Skris 134455714Skris if ((s->session == NULL) || (s->session->ciphers == NULL) || 134555714Skris (len < 2)) 134655714Skris return(NULL); 134755714Skris 134855714Skris p=buf; 134955714Skris sk=s->session->ciphers; 135055714Skris for (i=0; i<sk_SSL_CIPHER_num(sk); i++) 135155714Skris { 1352172429Ssimon int n; 1353172429Ssimon 135455714Skris c=sk_SSL_CIPHER_value(sk,i); 1355172429Ssimon n=strlen(c->name); 1356172429Ssimon if (n+1 > len) 135755714Skris { 1358172429Ssimon if (p != buf) 1359172429Ssimon --p; 1360172429Ssimon *p='\0'; 1361172429Ssimon return buf; 136255714Skris } 1363172429Ssimon strcpy(p,c->name); 1364172429Ssimon p+=n; 136555714Skris *(p++)=':'; 1366172429Ssimon len-=n+1; 136755714Skris } 136855714Skris p[-1]='\0'; 136955714Skris return(buf); 137055714Skris } 137155714Skris 1372160814Ssimonint ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p, 1373238405Sjkim int (*put_cb)(const SSL_CIPHER *, unsigned char *)) 137455714Skris { 137555714Skris int i,j=0; 137655714Skris SSL_CIPHER *c; 137755714Skris unsigned char *q; 1378109998Smarkm#ifndef OPENSSL_NO_KRB5 1379238405Sjkim int nokrb5 = !kssl_tgt_is_available(s->kssl_ctx); 1380109998Smarkm#endif /* OPENSSL_NO_KRB5 */ 138155714Skris 138255714Skris if (sk == NULL) return(0); 138355714Skris q=p; 138455714Skris 138555714Skris for (i=0; i<sk_SSL_CIPHER_num(sk); i++) 138655714Skris { 138755714Skris c=sk_SSL_CIPHER_value(sk,i); 1388238405Sjkim /* Skip TLS v1.2 only ciphersuites if lower than v1.2 */ 1389238405Sjkim if ((c->algorithm_ssl & SSL_TLSV1_2) && 1390238405Sjkim (TLS1_get_client_version(s) < TLS1_2_VERSION)) 1391238405Sjkim continue; 1392109998Smarkm#ifndef OPENSSL_NO_KRB5 1393238405Sjkim if (((c->algorithm_mkey & SSL_kKRB5) || (c->algorithm_auth & SSL_aKRB5)) && 1394238405Sjkim nokrb5) 1395238405Sjkim continue; 1396238405Sjkim#endif /* OPENSSL_NO_KRB5 */ 1397238405Sjkim#ifndef OPENSSL_NO_PSK 1398238405Sjkim /* with PSK there must be client callback set */ 1399238405Sjkim if (((c->algorithm_mkey & SSL_kPSK) || (c->algorithm_auth & SSL_aPSK)) && 1400238405Sjkim s->psk_client_callback == NULL) 1401238405Sjkim continue; 1402238405Sjkim#endif /* OPENSSL_NO_PSK */ 1403160814Ssimon j = put_cb ? put_cb(c,p) : ssl_put_cipher_by_char(s,c,p); 140455714Skris p+=j; 140555714Skris } 1406205128Ssimon /* If p == q, no ciphers and caller indicates an error. Otherwise 1407205128Ssimon * add SCSV if not renegotiating. 1408205128Ssimon */ 1409238405Sjkim if (p != q && !s->renegotiate) 1410205128Ssimon { 1411205128Ssimon static SSL_CIPHER scsv = 1412205128Ssimon { 1413238405Sjkim 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 1414205128Ssimon }; 1415205128Ssimon j = put_cb ? put_cb(&scsv,p) : ssl_put_cipher_by_char(s,&scsv,p); 1416205128Ssimon p+=j; 1417205128Ssimon#ifdef OPENSSL_RI_DEBUG 1418205128Ssimon fprintf(stderr, "SCSV sent by client\n"); 1419205128Ssimon#endif 1420205128Ssimon } 1421205128Ssimon 142255714Skris return(p-q); 142355714Skris } 142455714Skris 142555714SkrisSTACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num, 142655714Skris STACK_OF(SSL_CIPHER) **skp) 142755714Skris { 1428238405Sjkim const SSL_CIPHER *c; 142955714Skris STACK_OF(SSL_CIPHER) *sk; 143055714Skris int i,n; 1431205128Ssimon if (s->s3) 1432205128Ssimon s->s3->send_connection_binding = 0; 143355714Skris 143455714Skris n=ssl_put_cipher_by_char(s,NULL,NULL); 143555714Skris if ((num%n) != 0) 143655714Skris { 143755714Skris SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); 143855714Skris return(NULL); 143955714Skris } 144055714Skris if ((skp == NULL) || (*skp == NULL)) 144168651Skris sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */ 144255714Skris else 144355714Skris { 144455714Skris sk= *skp; 144555714Skris sk_SSL_CIPHER_zero(sk); 144655714Skris } 144755714Skris 144855714Skris for (i=0; i<num; i+=n) 144955714Skris { 1450205128Ssimon /* Check for SCSV */ 1451205128Ssimon if (s->s3 && (n != 3 || !p[0]) && 1452205128Ssimon (p[n-2] == ((SSL3_CK_SCSV >> 8) & 0xff)) && 1453205128Ssimon (p[n-1] == (SSL3_CK_SCSV & 0xff))) 1454205128Ssimon { 1455205128Ssimon /* SCSV fatal if renegotiating */ 1456238405Sjkim if (s->renegotiate) 1457205128Ssimon { 1458205128Ssimon SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING); 1459205128Ssimon ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); 1460205128Ssimon goto err; 1461205128Ssimon } 1462205128Ssimon s->s3->send_connection_binding = 1; 1463205128Ssimon p += n; 1464205128Ssimon#ifdef OPENSSL_RI_DEBUG 1465205128Ssimon fprintf(stderr, "SCSV received by server\n"); 1466205128Ssimon#endif 1467205128Ssimon continue; 1468205128Ssimon } 1469205128Ssimon 147055714Skris c=ssl_get_cipher_by_char(s,p); 147155714Skris p+=n; 147255714Skris if (c != NULL) 147355714Skris { 147455714Skris if (!sk_SSL_CIPHER_push(sk,c)) 147555714Skris { 147655714Skris SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE); 147755714Skris goto err; 147855714Skris } 147955714Skris } 148055714Skris } 148155714Skris 148255714Skris if (skp != NULL) 148355714Skris *skp=sk; 148455714Skris return(sk); 148555714Skriserr: 148655714Skris if ((skp == NULL) || (*skp == NULL)) 148755714Skris sk_SSL_CIPHER_free(sk); 148855714Skris return(NULL); 148955714Skris } 149055714Skris 1491238405Sjkim 1492194206Ssimon#ifndef OPENSSL_NO_TLSEXT 1493194206Ssimon/** return a servername extension value if provided in Client Hello, or NULL. 1494194206Ssimon * So far, only host_name types are defined (RFC 3546). 1495194206Ssimon */ 1496194206Ssimon 1497194206Ssimonconst char *SSL_get_servername(const SSL *s, const int type) 1498194206Ssimon { 1499194206Ssimon if (type != TLSEXT_NAMETYPE_host_name) 1500194206Ssimon return NULL; 1501194206Ssimon 1502194206Ssimon return s->session && !s->tlsext_hostname ? 1503194206Ssimon s->session->tlsext_hostname : 1504194206Ssimon s->tlsext_hostname; 1505194206Ssimon } 1506194206Ssimon 1507194206Ssimonint SSL_get_servername_type(const SSL *s) 1508194206Ssimon { 1509194206Ssimon if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) 1510194206Ssimon return TLSEXT_NAMETYPE_host_name; 1511194206Ssimon return -1; 1512194206Ssimon } 1513238405Sjkim 1514238405Sjkim# ifndef OPENSSL_NO_NEXTPROTONEG 1515238405Sjkim/* SSL_select_next_proto implements the standard protocol selection. It is 1516238405Sjkim * expected that this function is called from the callback set by 1517238405Sjkim * SSL_CTX_set_next_proto_select_cb. 1518238405Sjkim * 1519238405Sjkim * The protocol data is assumed to be a vector of 8-bit, length prefixed byte 1520238405Sjkim * strings. The length byte itself is not included in the length. A byte 1521238405Sjkim * string of length 0 is invalid. No byte string may be truncated. 1522238405Sjkim * 1523238405Sjkim * The current, but experimental algorithm for selecting the protocol is: 1524238405Sjkim * 1525238405Sjkim * 1) If the server doesn't support NPN then this is indicated to the 1526238405Sjkim * callback. In this case, the client application has to abort the connection 1527238405Sjkim * or have a default application level protocol. 1528238405Sjkim * 1529238405Sjkim * 2) If the server supports NPN, but advertises an empty list then the 1530238405Sjkim * client selects the first protcol in its list, but indicates via the 1531238405Sjkim * API that this fallback case was enacted. 1532238405Sjkim * 1533238405Sjkim * 3) Otherwise, the client finds the first protocol in the server's list 1534238405Sjkim * that it supports and selects this protocol. This is because it's 1535238405Sjkim * assumed that the server has better information about which protocol 1536238405Sjkim * a client should use. 1537238405Sjkim * 1538238405Sjkim * 4) If the client doesn't support any of the server's advertised 1539238405Sjkim * protocols, then this is treated the same as case 2. 1540238405Sjkim * 1541238405Sjkim * It returns either 1542238405Sjkim * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or 1543238405Sjkim * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached. 1544238405Sjkim */ 1545238405Sjkimint 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) 1546238405Sjkim { 1547238405Sjkim unsigned int i, j; 1548238405Sjkim const unsigned char *result; 1549238405Sjkim int status = OPENSSL_NPN_UNSUPPORTED; 1550238405Sjkim 1551238405Sjkim /* For each protocol in server preference order, see if we support it. */ 1552238405Sjkim for (i = 0; i < server_len; ) 1553238405Sjkim { 1554238405Sjkim for (j = 0; j < client_len; ) 1555238405Sjkim { 1556238405Sjkim if (server[i] == client[j] && 1557238405Sjkim memcmp(&server[i+1], &client[j+1], server[i]) == 0) 1558238405Sjkim { 1559238405Sjkim /* We found a match */ 1560238405Sjkim result = &server[i]; 1561238405Sjkim status = OPENSSL_NPN_NEGOTIATED; 1562238405Sjkim goto found; 1563238405Sjkim } 1564238405Sjkim j += client[j]; 1565238405Sjkim j++; 1566238405Sjkim } 1567238405Sjkim i += server[i]; 1568238405Sjkim i++; 1569238405Sjkim } 1570238405Sjkim 1571238405Sjkim /* There's no overlap between our protocols and the server's list. */ 1572238405Sjkim result = client; 1573238405Sjkim status = OPENSSL_NPN_NO_OVERLAP; 1574238405Sjkim 1575238405Sjkim found: 1576238405Sjkim *out = (unsigned char *) result + 1; 1577238405Sjkim *outlen = result[0]; 1578238405Sjkim return status; 1579238405Sjkim } 1580238405Sjkim 1581238405Sjkim/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's 1582238405Sjkim * requested protocol for this connection and returns 0. If the client didn't 1583238405Sjkim * request any protocol, then *data is set to NULL. 1584238405Sjkim * 1585238405Sjkim * Note that the client can request any protocol it chooses. The value returned 1586238405Sjkim * from this function need not be a member of the list of supported protocols 1587238405Sjkim * provided by the callback. 1588238405Sjkim */ 1589238405Sjkimvoid SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data, unsigned *len) 1590238405Sjkim { 1591238405Sjkim *data = s->next_proto_negotiated; 1592238405Sjkim if (!*data) { 1593238405Sjkim *len = 0; 1594238405Sjkim } else { 1595238405Sjkim *len = s->next_proto_negotiated_len; 1596238405Sjkim } 1597238405Sjkim} 1598238405Sjkim 1599238405Sjkim/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a 1600238405Sjkim * TLS server needs a list of supported protocols for Next Protocol 1601238405Sjkim * Negotiation. The returned list must be in wire format. The list is returned 1602238405Sjkim * by setting |out| to point to it and |outlen| to its length. This memory will 1603238405Sjkim * not be modified, but one should assume that the SSL* keeps a reference to 1604238405Sjkim * it. 1605238405Sjkim * 1606238405Sjkim * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no 1607238405Sjkim * such extension will be included in the ServerHello. */ 1608238405Sjkimvoid 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) 1609238405Sjkim { 1610238405Sjkim ctx->next_protos_advertised_cb = cb; 1611238405Sjkim ctx->next_protos_advertised_cb_arg = arg; 1612238405Sjkim } 1613238405Sjkim 1614238405Sjkim/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a 1615238405Sjkim * client needs to select a protocol from the server's provided list. |out| 1616238405Sjkim * must be set to point to the selected protocol (which may be within |in|). 1617238405Sjkim * The length of the protocol name must be written into |outlen|. The server's 1618238405Sjkim * advertised protocols are provided in |in| and |inlen|. The callback can 1619238405Sjkim * assume that |in| is syntactically valid. 1620238405Sjkim * 1621238405Sjkim * The client must select a protocol. It is fatal to the connection if this 1622238405Sjkim * callback returns a value other than SSL_TLSEXT_ERR_OK. 1623238405Sjkim */ 1624238405Sjkimvoid 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) 1625238405Sjkim { 1626238405Sjkim ctx->next_proto_select_cb = cb; 1627238405Sjkim ctx->next_proto_select_cb_arg = arg; 1628238405Sjkim } 1629238405Sjkim# endif 1630194206Ssimon#endif 1631194206Ssimon 1632238405Sjkimint SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen, 1633238405Sjkim const char *label, size_t llen, const unsigned char *p, size_t plen, 1634238405Sjkim int use_context) 163555714Skris { 1636238405Sjkim if (s->version < TLS1_VERSION) 1637238405Sjkim return -1; 1638238405Sjkim 1639238405Sjkim return s->method->ssl3_enc->export_keying_material(s, out, olen, label, 1640238405Sjkim llen, p, plen, 1641238405Sjkim use_context); 1642238405Sjkim } 1643238405Sjkim 1644238405Sjkimstatic unsigned long ssl_session_hash(const SSL_SESSION *a) 1645238405Sjkim { 164655714Skris unsigned long l; 164755714Skris 164855714Skris l=(unsigned long) 164955714Skris ((unsigned int) a->session_id[0] )| 165055714Skris ((unsigned int) a->session_id[1]<< 8L)| 165155714Skris ((unsigned long)a->session_id[2]<<16L)| 165255714Skris ((unsigned long)a->session_id[3]<<24L); 165355714Skris return(l); 165455714Skris } 165555714Skris 1656109998Smarkm/* NB: If this function (or indeed the hash function which uses a sort of 1657109998Smarkm * coarser function than this one) is changed, ensure 1658109998Smarkm * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being 1659109998Smarkm * able to construct an SSL_SESSION that will collide with any existing session 1660109998Smarkm * with a matching session ID. */ 1661238405Sjkimstatic int ssl_session_cmp(const SSL_SESSION *a,const SSL_SESSION *b) 166255714Skris { 166355714Skris if (a->ssl_version != b->ssl_version) 166455714Skris return(1); 166555714Skris if (a->session_id_length != b->session_id_length) 166655714Skris return(1); 166755714Skris return(memcmp(a->session_id,b->session_id,a->session_id_length)); 166855714Skris } 166955714Skris 1670109998Smarkm/* These wrapper functions should remain rather than redeclaring 1671109998Smarkm * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each 1672109998Smarkm * variable. The reason is that the functions aren't static, they're exposed via 1673109998Smarkm * ssl.h. */ 1674238405Sjkimstatic IMPLEMENT_LHASH_HASH_FN(ssl_session, SSL_SESSION) 1675238405Sjkimstatic IMPLEMENT_LHASH_COMP_FN(ssl_session, SSL_SESSION) 1676109998Smarkm 1677238405SjkimSSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) 167855714Skris { 167955714Skris SSL_CTX *ret=NULL; 1680238405Sjkim 168155714Skris if (meth == NULL) 168255714Skris { 168355714Skris SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED); 168455714Skris return(NULL); 168555714Skris } 168655714Skris 1687194206Ssimon#ifdef OPENSSL_FIPS 1688194206Ssimon if (FIPS_mode() && (meth->version < TLS1_VERSION)) 1689194206Ssimon { 1690194206Ssimon SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE); 1691194206Ssimon return NULL; 1692194206Ssimon } 1693194206Ssimon#endif 1694194206Ssimon 169555714Skris if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) 169655714Skris { 169755714Skris SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS); 169855714Skris goto err; 169955714Skris } 170068651Skris ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX)); 170155714Skris if (ret == NULL) 170255714Skris goto err; 170355714Skris 170455714Skris memset(ret,0,sizeof(SSL_CTX)); 170555714Skris 170655714Skris ret->method=meth; 170755714Skris 170855714Skris ret->cert_store=NULL; 170955714Skris ret->session_cache_mode=SSL_SESS_CACHE_SERVER; 171055714Skris ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; 171155714Skris ret->session_cache_head=NULL; 171255714Skris ret->session_cache_tail=NULL; 171355714Skris 171455714Skris /* We take the system default */ 171555714Skris ret->session_timeout=meth->get_timeout(); 171655714Skris 1717109998Smarkm ret->new_session_cb=0; 1718109998Smarkm ret->remove_session_cb=0; 1719109998Smarkm ret->get_session_cb=0; 1720109998Smarkm ret->generate_session_id=0; 172155714Skris 172255714Skris memset((char *)&ret->stats,0,sizeof(ret->stats)); 172355714Skris 172455714Skris ret->references=1; 172555714Skris ret->quiet_shutdown=0; 172655714Skris 172755714Skris/* ret->cipher=NULL;*/ 172855714Skris/* ret->s2->challenge=NULL; 172955714Skris ret->master_key=NULL; 173055714Skris ret->key_arg=NULL; 173155714Skris ret->s2->conn_id=NULL; */ 173255714Skris 173355714Skris ret->info_callback=NULL; 173455714Skris 1735109998Smarkm ret->app_verify_callback=0; 173655714Skris ret->app_verify_arg=NULL; 173755714Skris 1738109998Smarkm ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT; 173955714Skris ret->read_ahead=0; 1740109998Smarkm ret->msg_callback=0; 1741109998Smarkm ret->msg_callback_arg=NULL; 174255714Skris ret->verify_mode=SSL_VERIFY_NONE; 1743160814Ssimon#if 0 174455714Skris ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */ 1745160814Ssimon#endif 1746109998Smarkm ret->sid_ctx_length=0; 174755714Skris ret->default_verify_callback=NULL; 174855714Skris if ((ret->cert=ssl_cert_new()) == NULL) 174955714Skris goto err; 175055714Skris 1751109998Smarkm ret->default_passwd_callback=0; 175255714Skris ret->default_passwd_callback_userdata=NULL; 1753109998Smarkm ret->client_cert_cb=0; 1754160814Ssimon ret->app_gen_cookie_cb=0; 1755160814Ssimon ret->app_verify_cookie_cb=0; 175655714Skris 1757238405Sjkim ret->sessions=lh_SSL_SESSION_new(); 175855714Skris if (ret->sessions == NULL) goto err; 175955714Skris ret->cert_store=X509_STORE_new(); 176055714Skris if (ret->cert_store == NULL) goto err; 176155714Skris 176255714Skris ssl_create_cipher_list(ret->method, 176355714Skris &ret->cipher_list,&ret->cipher_list_by_id, 1764238405Sjkim meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST); 176555714Skris if (ret->cipher_list == NULL 176655714Skris || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) 176755714Skris { 176855714Skris SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS); 176955714Skris goto err2; 177055714Skris } 177155714Skris 1772160814Ssimon ret->param = X509_VERIFY_PARAM_new(); 1773160814Ssimon if (!ret->param) 1774160814Ssimon goto err; 1775160814Ssimon 177655714Skris if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL) 177755714Skris { 177855714Skris SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES); 177955714Skris goto err2; 178055714Skris } 178155714Skris if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL) 178255714Skris { 178355714Skris SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES); 178455714Skris goto err2; 178555714Skris } 178655714Skris if ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL) 178755714Skris { 178855714Skris SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES); 178955714Skris goto err2; 179055714Skris } 179155714Skris 179255714Skris if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL) 179355714Skris goto err; 179455714Skris 1795109998Smarkm CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data); 179655714Skris 179755714Skris ret->extra_certs=NULL; 179855714Skris ret->comp_methods=SSL_COMP_get_compression_methods(); 179955714Skris 1800238405Sjkim ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; 1801238405Sjkim 1802194206Ssimon#ifndef OPENSSL_NO_TLSEXT 1803194206Ssimon ret->tlsext_servername_callback = 0; 1804194206Ssimon ret->tlsext_servername_arg = NULL; 1805194206Ssimon /* Setup RFC4507 ticket keys */ 1806194206Ssimon if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0) 1807194206Ssimon || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0) 1808194206Ssimon || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0)) 1809194206Ssimon ret->options |= SSL_OP_NO_TICKET; 1810194206Ssimon 1811194206Ssimon ret->tlsext_status_cb = 0; 1812194206Ssimon ret->tlsext_status_arg = NULL; 1813194206Ssimon 1814238405Sjkim# ifndef OPENSSL_NO_NEXTPROTONEG 1815238405Sjkim ret->next_protos_advertised_cb = 0; 1816238405Sjkim ret->next_proto_select_cb = 0; 1817238405Sjkim# endif 1818194206Ssimon#endif 1819238405Sjkim#ifndef OPENSSL_NO_PSK 1820238405Sjkim ret->psk_identity_hint=NULL; 1821238405Sjkim ret->psk_client_callback=NULL; 1822238405Sjkim ret->psk_server_callback=NULL; 1823238405Sjkim#endif 1824238405Sjkim#ifndef OPENSSL_NO_SRP 1825238405Sjkim SSL_CTX_SRP_CTX_init(ret); 1826238405Sjkim#endif 1827238405Sjkim#ifndef OPENSSL_NO_BUF_FREELISTS 1828238405Sjkim ret->freelist_max_len = SSL_MAX_BUF_FREELIST_LEN_DEFAULT; 1829238405Sjkim ret->rbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST)); 1830238405Sjkim if (!ret->rbuf_freelist) 1831238405Sjkim goto err; 1832238405Sjkim ret->rbuf_freelist->chunklen = 0; 1833238405Sjkim ret->rbuf_freelist->len = 0; 1834238405Sjkim ret->rbuf_freelist->head = NULL; 1835238405Sjkim ret->wbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST)); 1836238405Sjkim if (!ret->wbuf_freelist) 1837238405Sjkim { 1838238405Sjkim OPENSSL_free(ret->rbuf_freelist); 1839238405Sjkim goto err; 1840238405Sjkim } 1841238405Sjkim ret->wbuf_freelist->chunklen = 0; 1842238405Sjkim ret->wbuf_freelist->len = 0; 1843238405Sjkim ret->wbuf_freelist->head = NULL; 1844238405Sjkim#endif 1845194206Ssimon#ifndef OPENSSL_NO_ENGINE 1846194206Ssimon ret->client_cert_engine = NULL; 1847194206Ssimon#ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO 1848194206Ssimon#define eng_strx(x) #x 1849194206Ssimon#define eng_str(x) eng_strx(x) 1850194206Ssimon /* Use specific client engine automatically... ignore errors */ 1851194206Ssimon { 1852194206Ssimon ENGINE *eng; 1853194206Ssimon eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO)); 1854194206Ssimon if (!eng) 1855194206Ssimon { 1856194206Ssimon ERR_clear_error(); 1857194206Ssimon ENGINE_load_builtin_engines(); 1858194206Ssimon eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO)); 1859194206Ssimon } 1860194206Ssimon if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng)) 1861194206Ssimon ERR_clear_error(); 1862194206Ssimon } 1863194206Ssimon#endif 1864194206Ssimon#endif 1865205128Ssimon /* Default is to connect to non-RI servers. When RI is more widely 1866205128Ssimon * deployed might change this. 1867205128Ssimon */ 1868205128Ssimon ret->options |= SSL_OP_LEGACY_SERVER_CONNECT; 1869194206Ssimon 187055714Skris return(ret); 187155714Skriserr: 187255714Skris SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE); 187355714Skriserr2: 187455714Skris if (ret != NULL) SSL_CTX_free(ret); 187555714Skris return(NULL); 187655714Skris } 187755714Skris 1878109998Smarkm#if 0 187955714Skrisstatic void SSL_COMP_free(SSL_COMP *comp) 188068651Skris { OPENSSL_free(comp); } 1881109998Smarkm#endif 188255714Skris 1883238405Sjkim#ifndef OPENSSL_NO_BUF_FREELISTS 1884238405Sjkimstatic void 1885238405Sjkimssl_buf_freelist_free(SSL3_BUF_FREELIST *list) 1886238405Sjkim { 1887238405Sjkim SSL3_BUF_FREELIST_ENTRY *ent, *next; 1888238405Sjkim for (ent = list->head; ent; ent = next) 1889238405Sjkim { 1890238405Sjkim next = ent->next; 1891238405Sjkim OPENSSL_free(ent); 1892238405Sjkim } 1893238405Sjkim OPENSSL_free(list); 1894238405Sjkim } 1895238405Sjkim#endif 1896238405Sjkim 189755714Skrisvoid SSL_CTX_free(SSL_CTX *a) 189855714Skris { 189955714Skris int i; 190055714Skris 190155714Skris if (a == NULL) return; 190255714Skris 190355714Skris i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX); 190455714Skris#ifdef REF_PRINT 190555714Skris REF_PRINT("SSL_CTX",a); 190655714Skris#endif 190755714Skris if (i > 0) return; 190855714Skris#ifdef REF_CHECK 190955714Skris if (i < 0) 191055714Skris { 191155714Skris fprintf(stderr,"SSL_CTX_free, bad reference count\n"); 191255714Skris abort(); /* ok */ 191355714Skris } 191455714Skris#endif 191555714Skris 1916160814Ssimon if (a->param) 1917160814Ssimon X509_VERIFY_PARAM_free(a->param); 1918160814Ssimon 1919109998Smarkm /* 1920109998Smarkm * Free internal session cache. However: the remove_cb() may reference 1921109998Smarkm * the ex_data of SSL_CTX, thus the ex_data store can only be removed 1922109998Smarkm * after the sessions were flushed. 1923109998Smarkm * As the ex_data handling routines might also touch the session cache, 1924109998Smarkm * the most secure solution seems to be: empty (flush) the cache, then 1925109998Smarkm * free ex_data, then finally free the cache. 1926109998Smarkm * (See ticket [openssl.org #212].) 1927109998Smarkm */ 192855714Skris if (a->sessions != NULL) 192955714Skris SSL_CTX_flush_sessions(a,0); 1930109998Smarkm 1931109998Smarkm CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data); 1932109998Smarkm 1933109998Smarkm if (a->sessions != NULL) 1934238405Sjkim lh_SSL_SESSION_free(a->sessions); 1935109998Smarkm 193655714Skris if (a->cert_store != NULL) 193755714Skris X509_STORE_free(a->cert_store); 193855714Skris if (a->cipher_list != NULL) 193955714Skris sk_SSL_CIPHER_free(a->cipher_list); 194055714Skris if (a->cipher_list_by_id != NULL) 194155714Skris sk_SSL_CIPHER_free(a->cipher_list_by_id); 194255714Skris if (a->cert != NULL) 194355714Skris ssl_cert_free(a->cert); 194455714Skris if (a->client_CA != NULL) 194555714Skris sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free); 194655714Skris if (a->extra_certs != NULL) 194755714Skris sk_X509_pop_free(a->extra_certs,X509_free); 1948109998Smarkm#if 0 /* This should never be done, since it removes a global database */ 194955714Skris if (a->comp_methods != NULL) 195055714Skris sk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free); 1951109998Smarkm#else 1952109998Smarkm a->comp_methods = NULL; 1953109998Smarkm#endif 1954238405Sjkim 1955238405Sjkim if (a->srtp_profiles) 1956238405Sjkim sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles); 1957238405Sjkim 1958238405Sjkim#ifndef OPENSSL_NO_PSK 1959238405Sjkim if (a->psk_identity_hint) 1960238405Sjkim OPENSSL_free(a->psk_identity_hint); 1961238405Sjkim#endif 1962238405Sjkim#ifndef OPENSSL_NO_SRP 1963238405Sjkim SSL_CTX_SRP_CTX_free(a); 1964238405Sjkim#endif 1965194206Ssimon#ifndef OPENSSL_NO_ENGINE 1966194206Ssimon if (a->client_cert_engine) 1967194206Ssimon ENGINE_finish(a->client_cert_engine); 1968194206Ssimon#endif 1969238405Sjkim 1970238405Sjkim#ifndef OPENSSL_NO_BUF_FREELISTS 1971238405Sjkim if (a->wbuf_freelist) 1972238405Sjkim ssl_buf_freelist_free(a->wbuf_freelist); 1973238405Sjkim if (a->rbuf_freelist) 1974238405Sjkim ssl_buf_freelist_free(a->rbuf_freelist); 1975238405Sjkim#endif 1976238405Sjkim 197768651Skris OPENSSL_free(a); 197855714Skris } 197955714Skris 198055714Skrisvoid SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) 198155714Skris { 198255714Skris ctx->default_passwd_callback=cb; 198355714Skris } 198455714Skris 198555714Skrisvoid SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u) 198655714Skris { 198755714Skris ctx->default_passwd_callback_userdata=u; 198855714Skris } 198955714Skris 1990109998Smarkmvoid SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg) 199155714Skris { 199255714Skris ctx->app_verify_callback=cb; 1993109998Smarkm ctx->app_verify_arg=arg; 199455714Skris } 199555714Skris 199655714Skrisvoid SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *)) 199755714Skris { 199855714Skris ctx->verify_mode=mode; 199955714Skris ctx->default_verify_callback=cb; 200055714Skris } 200155714Skris 200255714Skrisvoid SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth) 200355714Skris { 2004160814Ssimon X509_VERIFY_PARAM_set_depth(ctx->param, depth); 200555714Skris } 200655714Skris 2007238405Sjkimvoid ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher) 200855714Skris { 200955714Skris CERT_PKEY *cpk; 201055714Skris int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign; 201155714Skris int rsa_enc_export,dh_rsa_export,dh_dsa_export; 201255714Skris int rsa_tmp_export,dh_tmp_export,kl; 2013238405Sjkim unsigned long mask_k,mask_a,emask_k,emask_a; 2014160814Ssimon int have_ecc_cert, ecdh_ok, ecdsa_ok, ecc_pkey_size; 2015160814Ssimon#ifndef OPENSSL_NO_ECDH 2016160814Ssimon int have_ecdh_tmp; 2017160814Ssimon#endif 2018160814Ssimon X509 *x = NULL; 2019160814Ssimon EVP_PKEY *ecc_pkey = NULL; 2020238405Sjkim int signature_nid = 0, pk_nid = 0, md_nid = 0; 202155714Skris 202255714Skris if (c == NULL) return; 202355714Skris 202455714Skris kl=SSL_C_EXPORT_PKEYLENGTH(cipher); 202555714Skris 2026109998Smarkm#ifndef OPENSSL_NO_RSA 202755714Skris rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL); 202855714Skris rsa_tmp_export=(c->rsa_tmp_cb != NULL || 202955714Skris (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl)); 203055714Skris#else 203155714Skris rsa_tmp=rsa_tmp_export=0; 203255714Skris#endif 2033109998Smarkm#ifndef OPENSSL_NO_DH 203455714Skris dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL); 203555714Skris dh_tmp_export=(c->dh_tmp_cb != NULL || 203655714Skris (dh_tmp && DH_size(c->dh_tmp)*8 <= kl)); 203755714Skris#else 203855714Skris dh_tmp=dh_tmp_export=0; 203955714Skris#endif 204055714Skris 2041160814Ssimon#ifndef OPENSSL_NO_ECDH 2042160814Ssimon have_ecdh_tmp=(c->ecdh_tmp != NULL || c->ecdh_tmp_cb != NULL); 2043160814Ssimon#endif 204455714Skris cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]); 204555714Skris rsa_enc= (cpk->x509 != NULL && cpk->privatekey != NULL); 204655714Skris rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl); 204755714Skris cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]); 204855714Skris rsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL); 204955714Skris cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]); 205055714Skris dsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL); 205155714Skris cpk= &(c->pkeys[SSL_PKEY_DH_RSA]); 205255714Skris dh_rsa= (cpk->x509 != NULL && cpk->privatekey != NULL); 205355714Skris dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl); 205455714Skris cpk= &(c->pkeys[SSL_PKEY_DH_DSA]); 205555714Skris/* FIX THIS EAY EAY EAY */ 205655714Skris dh_dsa= (cpk->x509 != NULL && cpk->privatekey != NULL); 205755714Skris dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl); 2058160814Ssimon cpk= &(c->pkeys[SSL_PKEY_ECC]); 2059160814Ssimon have_ecc_cert= (cpk->x509 != NULL && cpk->privatekey != NULL); 2060238405Sjkim mask_k=0; 2061238405Sjkim mask_a=0; 2062238405Sjkim emask_k=0; 2063238405Sjkim emask_a=0; 206455714Skris 2065238405Sjkim 2066238405Sjkim 206755714Skris#ifdef CIPHER_DEBUG 2068238405Sjkim printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n", 2069238405Sjkim rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp, 207055714Skris rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa); 207155714Skris#endif 2072238405Sjkim 2073238405Sjkim cpk = &(c->pkeys[SSL_PKEY_GOST01]); 2074238405Sjkim if (cpk->x509 != NULL && cpk->privatekey !=NULL) { 2075238405Sjkim mask_k |= SSL_kGOST; 2076238405Sjkim mask_a |= SSL_aGOST01; 2077238405Sjkim } 2078238405Sjkim cpk = &(c->pkeys[SSL_PKEY_GOST94]); 2079238405Sjkim if (cpk->x509 != NULL && cpk->privatekey !=NULL) { 2080238405Sjkim mask_k |= SSL_kGOST; 2081238405Sjkim mask_a |= SSL_aGOST94; 2082238405Sjkim } 208355714Skris 208455714Skris if (rsa_enc || (rsa_tmp && rsa_sign)) 2085238405Sjkim mask_k|=SSL_kRSA; 208655714Skris if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc))) 2087238405Sjkim emask_k|=SSL_kRSA; 208855714Skris 208955714Skris#if 0 209055714Skris /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */ 2091238405Sjkim if ( (dh_tmp || dh_rsa || dh_dsa) && 209255714Skris (rsa_enc || rsa_sign || dsa_sign)) 2093238405Sjkim mask_k|=SSL_kEDH; 209455714Skris if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) && 209555714Skris (rsa_enc || rsa_sign || dsa_sign)) 2096238405Sjkim emask_k|=SSL_kEDH; 209755714Skris#endif 209855714Skris 2099238405Sjkim if (dh_tmp_export) 2100238405Sjkim emask_k|=SSL_kEDH; 210155714Skris 210255714Skris if (dh_tmp) 2103238405Sjkim mask_k|=SSL_kEDH; 210455714Skris 2105238405Sjkim if (dh_rsa) mask_k|=SSL_kDHr; 2106238405Sjkim if (dh_rsa_export) emask_k|=SSL_kDHr; 210755714Skris 2108238405Sjkim if (dh_dsa) mask_k|=SSL_kDHd; 2109238405Sjkim if (dh_dsa_export) emask_k|=SSL_kDHd; 211055714Skris 211155714Skris if (rsa_enc || rsa_sign) 211255714Skris { 2113238405Sjkim mask_a|=SSL_aRSA; 2114238405Sjkim emask_a|=SSL_aRSA; 211555714Skris } 211655714Skris 211755714Skris if (dsa_sign) 211855714Skris { 2119238405Sjkim mask_a|=SSL_aDSS; 2120238405Sjkim emask_a|=SSL_aDSS; 212155714Skris } 212255714Skris 2123238405Sjkim mask_a|=SSL_aNULL; 2124238405Sjkim emask_a|=SSL_aNULL; 212555714Skris 2126109998Smarkm#ifndef OPENSSL_NO_KRB5 2127238405Sjkim mask_k|=SSL_kKRB5; 2128238405Sjkim mask_a|=SSL_aKRB5; 2129238405Sjkim emask_k|=SSL_kKRB5; 2130238405Sjkim emask_a|=SSL_aKRB5; 2131109998Smarkm#endif 2132109998Smarkm 2133160814Ssimon /* An ECC certificate may be usable for ECDH and/or 2134160814Ssimon * ECDSA cipher suites depending on the key usage extension. 2135160814Ssimon */ 2136160814Ssimon if (have_ecc_cert) 2137160814Ssimon { 2138238405Sjkim /* This call populates extension flags (ex_flags) */ 2139160814Ssimon x = (c->pkeys[SSL_PKEY_ECC]).x509; 2140160814Ssimon X509_check_purpose(x, -1, 0); 2141160814Ssimon ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ? 2142160814Ssimon (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1; 2143160814Ssimon ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ? 2144160814Ssimon (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1; 2145160814Ssimon ecc_pkey = X509_get_pubkey(x); 2146238405Sjkim ecc_pkey_size = (ecc_pkey != NULL) ? 2147160814Ssimon EVP_PKEY_bits(ecc_pkey) : 0; 2148160814Ssimon EVP_PKEY_free(ecc_pkey); 2149160814Ssimon if ((x->sig_alg) && (x->sig_alg->algorithm)) 2150238405Sjkim { 2151160814Ssimon signature_nid = OBJ_obj2nid(x->sig_alg->algorithm); 2152238405Sjkim OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid); 2153238405Sjkim } 2154160814Ssimon#ifndef OPENSSL_NO_ECDH 2155160814Ssimon if (ecdh_ok) 2156160814Ssimon { 2157238405Sjkim 2158238405Sjkim if (pk_nid == NID_rsaEncryption || pk_nid == NID_rsa) 2159160814Ssimon { 2160238405Sjkim mask_k|=SSL_kECDHr; 2161238405Sjkim mask_a|=SSL_aECDH; 2162160814Ssimon if (ecc_pkey_size <= 163) 2163238405Sjkim { 2164238405Sjkim emask_k|=SSL_kECDHr; 2165238405Sjkim emask_a|=SSL_aECDH; 2166238405Sjkim } 2167160814Ssimon } 2168238405Sjkim 2169238405Sjkim if (pk_nid == NID_X9_62_id_ecPublicKey) 2170160814Ssimon { 2171238405Sjkim mask_k|=SSL_kECDHe; 2172238405Sjkim mask_a|=SSL_aECDH; 2173160814Ssimon if (ecc_pkey_size <= 163) 2174238405Sjkim { 2175238405Sjkim emask_k|=SSL_kECDHe; 2176238405Sjkim emask_a|=SSL_aECDH; 2177238405Sjkim } 2178160814Ssimon } 2179160814Ssimon } 2180160814Ssimon#endif 2181160814Ssimon#ifndef OPENSSL_NO_ECDSA 2182160814Ssimon if (ecdsa_ok) 2183160814Ssimon { 2184238405Sjkim mask_a|=SSL_aECDSA; 2185238405Sjkim emask_a|=SSL_aECDSA; 2186160814Ssimon } 2187160814Ssimon#endif 2188160814Ssimon } 2189160814Ssimon 2190160814Ssimon#ifndef OPENSSL_NO_ECDH 2191160814Ssimon if (have_ecdh_tmp) 2192160814Ssimon { 2193238405Sjkim mask_k|=SSL_kEECDH; 2194238405Sjkim emask_k|=SSL_kEECDH; 2195160814Ssimon } 2196160814Ssimon#endif 2197238405Sjkim 2198238405Sjkim#ifndef OPENSSL_NO_PSK 2199238405Sjkim mask_k |= SSL_kPSK; 2200238405Sjkim mask_a |= SSL_aPSK; 2201238405Sjkim emask_k |= SSL_kPSK; 2202238405Sjkim emask_a |= SSL_aPSK; 2203238405Sjkim#endif 2204238405Sjkim 2205238405Sjkim c->mask_k=mask_k; 2206238405Sjkim c->mask_a=mask_a; 2207238405Sjkim c->export_mask_k=emask_k; 2208238405Sjkim c->export_mask_a=emask_a; 220955714Skris c->valid=1; 221055714Skris } 221155714Skris 2212160814Ssimon/* This handy macro borrowed from crypto/x509v3/v3_purp.c */ 2213160814Ssimon#define ku_reject(x, usage) \ 2214160814Ssimon (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage))) 2215160814Ssimon 2216238405Sjkim#ifndef OPENSSL_NO_EC 2217238405Sjkim 2218238405Sjkimint ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) 2219160814Ssimon { 2220238405Sjkim unsigned long alg_k, alg_a; 2221160814Ssimon EVP_PKEY *pkey = NULL; 2222160814Ssimon int keysize = 0; 2223238405Sjkim int signature_nid = 0, md_nid = 0, pk_nid = 0; 2224238405Sjkim const SSL_CIPHER *cs = s->s3->tmp.new_cipher; 2225160814Ssimon 2226238405Sjkim alg_k = cs->algorithm_mkey; 2227238405Sjkim alg_a = cs->algorithm_auth; 2228238405Sjkim 2229160814Ssimon if (SSL_C_IS_EXPORT(cs)) 2230160814Ssimon { 2231160814Ssimon /* ECDH key length in export ciphers must be <= 163 bits */ 2232160814Ssimon pkey = X509_get_pubkey(x); 2233160814Ssimon if (pkey == NULL) return 0; 2234160814Ssimon keysize = EVP_PKEY_bits(pkey); 2235160814Ssimon EVP_PKEY_free(pkey); 2236160814Ssimon if (keysize > 163) return 0; 2237160814Ssimon } 2238160814Ssimon 2239160814Ssimon /* This call populates the ex_flags field correctly */ 2240160814Ssimon X509_check_purpose(x, -1, 0); 2241160814Ssimon if ((x->sig_alg) && (x->sig_alg->algorithm)) 2242238405Sjkim { 2243160814Ssimon signature_nid = OBJ_obj2nid(x->sig_alg->algorithm); 2244238405Sjkim OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid); 2245238405Sjkim } 2246238405Sjkim if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr) 2247160814Ssimon { 2248160814Ssimon /* key usage, if present, must allow key agreement */ 2249160814Ssimon if (ku_reject(x, X509v3_KU_KEY_AGREEMENT)) 2250160814Ssimon { 2251238405Sjkim SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT); 2252160814Ssimon return 0; 2253160814Ssimon } 2254238405Sjkim if ((alg_k & SSL_kECDHe) && TLS1_get_version(s) < TLS1_2_VERSION) 2255160814Ssimon { 2256160814Ssimon /* signature alg must be ECDSA */ 2257238405Sjkim if (pk_nid != NID_X9_62_id_ecPublicKey) 2258160814Ssimon { 2259238405Sjkim SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE); 2260160814Ssimon return 0; 2261160814Ssimon } 2262160814Ssimon } 2263238405Sjkim if ((alg_k & SSL_kECDHr) && TLS1_get_version(s) < TLS1_2_VERSION) 2264160814Ssimon { 2265160814Ssimon /* signature alg must be RSA */ 2266238405Sjkim 2267238405Sjkim if (pk_nid != NID_rsaEncryption && pk_nid != NID_rsa) 2268160814Ssimon { 2269238405Sjkim SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE); 2270160814Ssimon return 0; 2271160814Ssimon } 2272160814Ssimon } 2273238405Sjkim } 2274238405Sjkim if (alg_a & SSL_aECDSA) 2275160814Ssimon { 2276160814Ssimon /* key usage, if present, must allow signing */ 2277160814Ssimon if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) 2278160814Ssimon { 2279238405Sjkim SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_SIGNING); 2280160814Ssimon return 0; 2281160814Ssimon } 2282160814Ssimon } 2283160814Ssimon 2284160814Ssimon return 1; /* all checks are ok */ 2285160814Ssimon } 2286160814Ssimon 2287238405Sjkim#endif 2288238405Sjkim 228955714Skris/* THIS NEEDS CLEANING UP */ 229055714SkrisX509 *ssl_get_server_send_cert(SSL *s) 229155714Skris { 2292238405Sjkim unsigned long alg_k,alg_a; 229355714Skris CERT *c; 2294215697Ssimon int i; 229555714Skris 229655714Skris c=s->cert; 229755714Skris ssl_set_cert_masks(c, s->s3->tmp.new_cipher); 2298238405Sjkim 2299238405Sjkim alg_k = s->s3->tmp.new_cipher->algorithm_mkey; 2300238405Sjkim alg_a = s->s3->tmp.new_cipher->algorithm_auth; 230155714Skris 2302238405Sjkim if (alg_k & (SSL_kECDHr|SSL_kECDHe)) 2303160814Ssimon { 2304238405Sjkim /* we don't need to look at SSL_kEECDH 2305160814Ssimon * since no certificate is needed for 2306160814Ssimon * anon ECDH and for authenticated 2307238405Sjkim * EECDH, the check for the auth 2308160814Ssimon * algorithm will set i correctly 2309160814Ssimon * NOTE: For ECDH-RSA, we need an ECC 2310238405Sjkim * not an RSA cert but for EECDH-RSA 2311160814Ssimon * we need an RSA cert. Placing the 2312160814Ssimon * checks for SSL_kECDH before RSA 2313160814Ssimon * checks ensures the correct cert is chosen. 2314160814Ssimon */ 2315160814Ssimon i=SSL_PKEY_ECC; 2316160814Ssimon } 2317238405Sjkim else if (alg_a & SSL_aECDSA) 2318160814Ssimon { 2319160814Ssimon i=SSL_PKEY_ECC; 2320160814Ssimon } 2321238405Sjkim else if (alg_k & SSL_kDHr) 232255714Skris i=SSL_PKEY_DH_RSA; 2323238405Sjkim else if (alg_k & SSL_kDHd) 232455714Skris i=SSL_PKEY_DH_DSA; 2325238405Sjkim else if (alg_a & SSL_aDSS) 232655714Skris i=SSL_PKEY_DSA_SIGN; 2327238405Sjkim else if (alg_a & SSL_aRSA) 232855714Skris { 232955714Skris if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL) 233055714Skris i=SSL_PKEY_RSA_SIGN; 233155714Skris else 233255714Skris i=SSL_PKEY_RSA_ENC; 233355714Skris } 2334238405Sjkim else if (alg_a & SSL_aKRB5) 2335109998Smarkm { 2336109998Smarkm /* VRS something else here? */ 2337109998Smarkm return(NULL); 2338109998Smarkm } 2339238405Sjkim else if (alg_a & SSL_aGOST94) 2340238405Sjkim i=SSL_PKEY_GOST94; 2341238405Sjkim else if (alg_a & SSL_aGOST01) 2342238405Sjkim i=SSL_PKEY_GOST01; 2343238405Sjkim else /* if (alg_a & SSL_aNULL) */ 234455714Skris { 2345109998Smarkm SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT,ERR_R_INTERNAL_ERROR); 234655714Skris return(NULL); 234755714Skris } 234855714Skris if (c->pkeys[i].x509 == NULL) return(NULL); 2349160814Ssimon 235055714Skris return(c->pkeys[i].x509); 235155714Skris } 235255714Skris 2353238405SjkimEVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher, const EVP_MD **pmd) 235455714Skris { 2355238405Sjkim unsigned long alg_a; 235655714Skris CERT *c; 2357238405Sjkim int idx = -1; 235855714Skris 2359238405Sjkim alg_a = cipher->algorithm_auth; 236055714Skris c=s->cert; 236155714Skris 2362238405Sjkim if ((alg_a & SSL_aDSS) && 236355714Skris (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL)) 2364238405Sjkim idx = SSL_PKEY_DSA_SIGN; 2365238405Sjkim else if (alg_a & SSL_aRSA) 236655714Skris { 236755714Skris if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) 2368238405Sjkim idx = SSL_PKEY_RSA_SIGN; 236955714Skris else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) 2370238405Sjkim idx = SSL_PKEY_RSA_ENC; 237155714Skris } 2372238405Sjkim else if ((alg_a & SSL_aECDSA) && 2373160814Ssimon (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) 2374238405Sjkim idx = SSL_PKEY_ECC; 2375238405Sjkim if (idx == -1) 237655714Skris { 2377109998Smarkm SSLerr(SSL_F_SSL_GET_SIGN_PKEY,ERR_R_INTERNAL_ERROR); 237855714Skris return(NULL); 237955714Skris } 2380238405Sjkim if (pmd) 2381238405Sjkim *pmd = c->pkeys[idx].digest; 2382238405Sjkim return c->pkeys[idx].privatekey; 238355714Skris } 238455714Skris 238555714Skrisvoid ssl_update_cache(SSL *s,int mode) 238655714Skris { 238755714Skris int i; 238855714Skris 238955714Skris /* If the session_id_length is 0, we are not supposed to cache it, 239055714Skris * and it would be rather hard to do anyway :-) */ 239155714Skris if (s->session->session_id_length == 0) return; 239255714Skris 2393238405Sjkim i=s->session_ctx->session_cache_mode; 2394100928Snectar if ((i & mode) && (!s->hit) 2395109998Smarkm && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) 2396238405Sjkim || SSL_CTX_add_session(s->session_ctx,s->session)) 2397238405Sjkim && (s->session_ctx->new_session_cb != NULL)) 239855714Skris { 239955714Skris CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION); 2400238405Sjkim if (!s->session_ctx->new_session_cb(s,s->session)) 240155714Skris SSL_SESSION_free(s->session); 240255714Skris } 240355714Skris 240455714Skris /* auto flush every 255 connections */ 240555714Skris if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && 240655714Skris ((i & mode) == mode)) 240755714Skris { 240855714Skris if ( (((mode & SSL_SESS_CACHE_CLIENT) 2409238405Sjkim ?s->session_ctx->stats.sess_connect_good 2410238405Sjkim :s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff) 241155714Skris { 2412238405Sjkim SSL_CTX_flush_sessions(s->session_ctx,(unsigned long)time(NULL)); 241355714Skris } 241455714Skris } 241555714Skris } 241655714Skris 2417238405Sjkimconst SSL_METHOD *SSL_get_ssl_method(SSL *s) 241855714Skris { 241955714Skris return(s->method); 242055714Skris } 242155714Skris 2422238405Sjkimint SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth) 242355714Skris { 242455714Skris int conn= -1; 242555714Skris int ret=1; 242655714Skris 242755714Skris if (s->method != meth) 242855714Skris { 242955714Skris if (s->handshake_func != NULL) 243055714Skris conn=(s->handshake_func == s->method->ssl_connect); 243155714Skris 243255714Skris if (s->method->version == meth->version) 243355714Skris s->method=meth; 243455714Skris else 243555714Skris { 243655714Skris s->method->ssl_free(s); 243755714Skris s->method=meth; 243855714Skris ret=s->method->ssl_new(s); 243955714Skris } 244055714Skris 244155714Skris if (conn == 1) 244255714Skris s->handshake_func=meth->ssl_connect; 244355714Skris else if (conn == 0) 244455714Skris s->handshake_func=meth->ssl_accept; 244555714Skris } 244655714Skris return(ret); 244755714Skris } 244855714Skris 2449160814Ssimonint SSL_get_error(const SSL *s,int i) 245055714Skris { 245155714Skris int reason; 245255714Skris unsigned long l; 245355714Skris BIO *bio; 245455714Skris 245555714Skris if (i > 0) return(SSL_ERROR_NONE); 245655714Skris 245755714Skris /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake 245855714Skris * etc, where we do encode the error */ 245955714Skris if ((l=ERR_peek_error()) != 0) 246055714Skris { 246155714Skris if (ERR_GET_LIB(l) == ERR_LIB_SYS) 246255714Skris return(SSL_ERROR_SYSCALL); 246355714Skris else 246455714Skris return(SSL_ERROR_SSL); 246555714Skris } 246655714Skris 246755714Skris if ((i < 0) && SSL_want_read(s)) 246855714Skris { 246955714Skris bio=SSL_get_rbio(s); 247055714Skris if (BIO_should_read(bio)) 247155714Skris return(SSL_ERROR_WANT_READ); 247255714Skris else if (BIO_should_write(bio)) 247355714Skris /* This one doesn't make too much sense ... We never try 247455714Skris * to write to the rbio, and an application program where 247555714Skris * rbio and wbio are separate couldn't even know what it 247655714Skris * should wait for. 247755714Skris * However if we ever set s->rwstate incorrectly 247855714Skris * (so that we have SSL_want_read(s) instead of 247955714Skris * SSL_want_write(s)) and rbio and wbio *are* the same, 248055714Skris * this test works around that bug; so it might be safer 248155714Skris * to keep it. */ 248255714Skris return(SSL_ERROR_WANT_WRITE); 248355714Skris else if (BIO_should_io_special(bio)) 248455714Skris { 248555714Skris reason=BIO_get_retry_reason(bio); 248655714Skris if (reason == BIO_RR_CONNECT) 248755714Skris return(SSL_ERROR_WANT_CONNECT); 2488109998Smarkm else if (reason == BIO_RR_ACCEPT) 2489109998Smarkm return(SSL_ERROR_WANT_ACCEPT); 249055714Skris else 249155714Skris return(SSL_ERROR_SYSCALL); /* unknown */ 249255714Skris } 249355714Skris } 249455714Skris 249555714Skris if ((i < 0) && SSL_want_write(s)) 249655714Skris { 249755714Skris bio=SSL_get_wbio(s); 249855714Skris if (BIO_should_write(bio)) 249955714Skris return(SSL_ERROR_WANT_WRITE); 250055714Skris else if (BIO_should_read(bio)) 250155714Skris /* See above (SSL_want_read(s) with BIO_should_write(bio)) */ 250255714Skris return(SSL_ERROR_WANT_READ); 250355714Skris else if (BIO_should_io_special(bio)) 250455714Skris { 250555714Skris reason=BIO_get_retry_reason(bio); 250655714Skris if (reason == BIO_RR_CONNECT) 250755714Skris return(SSL_ERROR_WANT_CONNECT); 2508109998Smarkm else if (reason == BIO_RR_ACCEPT) 2509109998Smarkm return(SSL_ERROR_WANT_ACCEPT); 251055714Skris else 251155714Skris return(SSL_ERROR_SYSCALL); 251255714Skris } 251355714Skris } 251455714Skris if ((i < 0) && SSL_want_x509_lookup(s)) 251555714Skris { 251655714Skris return(SSL_ERROR_WANT_X509_LOOKUP); 251755714Skris } 251855714Skris 251955714Skris if (i == 0) 252055714Skris { 252155714Skris if (s->version == SSL2_VERSION) 252255714Skris { 252355714Skris /* assume it is the socket being closed */ 252455714Skris return(SSL_ERROR_ZERO_RETURN); 252555714Skris } 252655714Skris else 252755714Skris { 252855714Skris if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) && 252955714Skris (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) 253055714Skris return(SSL_ERROR_ZERO_RETURN); 253155714Skris } 253255714Skris } 253355714Skris return(SSL_ERROR_SYSCALL); 253455714Skris } 253555714Skris 253655714Skrisint SSL_do_handshake(SSL *s) 253755714Skris { 253855714Skris int ret=1; 253955714Skris 254055714Skris if (s->handshake_func == NULL) 254155714Skris { 254255714Skris SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET); 254355714Skris return(-1); 254455714Skris } 254555714Skris 254655714Skris s->method->ssl_renegotiate_check(s); 254755714Skris 254855714Skris if (SSL_in_init(s) || SSL_in_before(s)) 254955714Skris { 255055714Skris ret=s->handshake_func(s); 255155714Skris } 255255714Skris return(ret); 255355714Skris } 255455714Skris 255555714Skris/* For the next 2 functions, SSL_clear() sets shutdown and so 255655714Skris * one of these calls will reset it */ 255755714Skrisvoid SSL_set_accept_state(SSL *s) 255855714Skris { 255955714Skris s->server=1; 256055714Skris s->shutdown=0; 256155714Skris s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE; 256255714Skris s->handshake_func=s->method->ssl_accept; 256355714Skris /* clear the current cipher */ 256455714Skris ssl_clear_cipher_ctx(s); 2565238405Sjkim ssl_clear_hash_ctx(&s->read_hash); 2566238405Sjkim ssl_clear_hash_ctx(&s->write_hash); 256755714Skris } 256855714Skris 256955714Skrisvoid SSL_set_connect_state(SSL *s) 257055714Skris { 257155714Skris s->server=0; 257255714Skris s->shutdown=0; 257355714Skris s->state=SSL_ST_CONNECT|SSL_ST_BEFORE; 257455714Skris s->handshake_func=s->method->ssl_connect; 257555714Skris /* clear the current cipher */ 257655714Skris ssl_clear_cipher_ctx(s); 2577238405Sjkim ssl_clear_hash_ctx(&s->read_hash); 2578238405Sjkim ssl_clear_hash_ctx(&s->write_hash); 257955714Skris } 258055714Skris 258155714Skrisint ssl_undefined_function(SSL *s) 258255714Skris { 258355714Skris SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 258455714Skris return(0); 258555714Skris } 258655714Skris 2587160814Ssimonint ssl_undefined_void_function(void) 2588160814Ssimon { 2589160814Ssimon SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2590160814Ssimon return(0); 2591160814Ssimon } 2592160814Ssimon 2593160814Ssimonint ssl_undefined_const_function(const SSL *s) 2594160814Ssimon { 2595160814Ssimon SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2596160814Ssimon return(0); 2597160814Ssimon } 2598160814Ssimon 259955714SkrisSSL_METHOD *ssl_bad_method(int ver) 260055714Skris { 260155714Skris SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 260255714Skris return(NULL); 260355714Skris } 260455714Skris 2605160814Ssimonconst char *SSL_get_version(const SSL *s) 260655714Skris { 2607238405Sjkim if (s->version == TLS1_2_VERSION) 2608238405Sjkim return("TLSv1.2"); 2609238405Sjkim else if (s->version == TLS1_1_VERSION) 2610238405Sjkim return("TLSv1.1"); 261155714Skris if (s->version == TLS1_VERSION) 261255714Skris return("TLSv1"); 261355714Skris else if (s->version == SSL3_VERSION) 261455714Skris return("SSLv3"); 261555714Skris else if (s->version == SSL2_VERSION) 261655714Skris return("SSLv2"); 261755714Skris else 261855714Skris return("unknown"); 261955714Skris } 262055714Skris 262155714SkrisSSL *SSL_dup(SSL *s) 262255714Skris { 262355714Skris STACK_OF(X509_NAME) *sk; 262455714Skris X509_NAME *xn; 262555714Skris SSL *ret; 262655714Skris int i; 2627238405Sjkim 262855714Skris if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL) 262955714Skris return(NULL); 2630109998Smarkm 2631109998Smarkm ret->version = s->version; 2632109998Smarkm ret->type = s->type; 2633109998Smarkm ret->method = s->method; 2634109998Smarkm 263555714Skris if (s->session != NULL) 263655714Skris { 263755714Skris /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */ 263855714Skris SSL_copy_session_id(ret,s); 263955714Skris } 264055714Skris else 264155714Skris { 264255714Skris /* No session has been established yet, so we have to expect 264355714Skris * that s->cert or ret->cert will be changed later -- 264455714Skris * they should not both point to the same object, 264555714Skris * and thus we can't use SSL_copy_session_id. */ 264655714Skris 2647111147Snectar ret->method->ssl_free(ret); 264855714Skris ret->method = s->method; 264955714Skris ret->method->ssl_new(ret); 265055714Skris 265155714Skris if (s->cert != NULL) 265255714Skris { 265376866Skris if (ret->cert != NULL) 265476866Skris { 265576866Skris ssl_cert_free(ret->cert); 265676866Skris } 265755714Skris ret->cert = ssl_cert_dup(s->cert); 265855714Skris if (ret->cert == NULL) 265955714Skris goto err; 266055714Skris } 266155714Skris 266255714Skris SSL_set_session_id_context(ret, 266355714Skris s->sid_ctx, s->sid_ctx_length); 266455714Skris } 266555714Skris 2666109998Smarkm ret->options=s->options; 2667109998Smarkm ret->mode=s->mode; 2668109998Smarkm SSL_set_max_cert_list(ret,SSL_get_max_cert_list(s)); 266955714Skris SSL_set_read_ahead(ret,SSL_get_read_ahead(s)); 2670109998Smarkm ret->msg_callback = s->msg_callback; 2671109998Smarkm ret->msg_callback_arg = s->msg_callback_arg; 267255714Skris SSL_set_verify(ret,SSL_get_verify_mode(s), 267355714Skris SSL_get_verify_callback(s)); 267455714Skris SSL_set_verify_depth(ret,SSL_get_verify_depth(s)); 2675109998Smarkm ret->generate_session_id = s->generate_session_id; 267655714Skris 267755714Skris SSL_set_info_callback(ret,SSL_get_info_callback(s)); 267855714Skris 267955714Skris ret->debug=s->debug; 268055714Skris 268155714Skris /* copy app data, a little dangerous perhaps */ 2682109998Smarkm if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data)) 268355714Skris goto err; 268455714Skris 268555714Skris /* setup rbio, and wbio */ 268655714Skris if (s->rbio != NULL) 268755714Skris { 268855714Skris if (!BIO_dup_state(s->rbio,(char *)&ret->rbio)) 268955714Skris goto err; 269055714Skris } 269155714Skris if (s->wbio != NULL) 269255714Skris { 269355714Skris if (s->wbio != s->rbio) 269455714Skris { 269555714Skris if (!BIO_dup_state(s->wbio,(char *)&ret->wbio)) 269655714Skris goto err; 269755714Skris } 269855714Skris else 269955714Skris ret->wbio=ret->rbio; 270055714Skris } 2701109998Smarkm ret->rwstate = s->rwstate; 2702109998Smarkm ret->in_handshake = s->in_handshake; 2703109998Smarkm ret->handshake_func = s->handshake_func; 2704109998Smarkm ret->server = s->server; 2705238405Sjkim ret->renegotiate = s->renegotiate; 2706109998Smarkm ret->new_session = s->new_session; 2707109998Smarkm ret->quiet_shutdown = s->quiet_shutdown; 2708109998Smarkm ret->shutdown=s->shutdown; 2709109998Smarkm ret->state=s->state; /* SSL_dup does not really work at any state, though */ 2710109998Smarkm ret->rstate=s->rstate; 2711109998Smarkm ret->init_num = 0; /* would have to copy ret->init_buf, ret->init_msg, ret->init_num, ret->init_off */ 2712109998Smarkm ret->hit=s->hit; 271355714Skris 2714160814Ssimon X509_VERIFY_PARAM_inherit(ret->param, s->param); 2715160814Ssimon 271655714Skris /* dup the cipher_list and cipher_list_by_id stacks */ 271755714Skris if (s->cipher_list != NULL) 271855714Skris { 271955714Skris if ((ret->cipher_list=sk_SSL_CIPHER_dup(s->cipher_list)) == NULL) 272055714Skris goto err; 272155714Skris } 272255714Skris if (s->cipher_list_by_id != NULL) 272355714Skris if ((ret->cipher_list_by_id=sk_SSL_CIPHER_dup(s->cipher_list_by_id)) 272455714Skris == NULL) 272555714Skris goto err; 272655714Skris 272755714Skris /* Dup the client_CA list */ 272855714Skris if (s->client_CA != NULL) 272955714Skris { 273055714Skris if ((sk=sk_X509_NAME_dup(s->client_CA)) == NULL) goto err; 273155714Skris ret->client_CA=sk; 273255714Skris for (i=0; i<sk_X509_NAME_num(sk); i++) 273355714Skris { 273455714Skris xn=sk_X509_NAME_value(sk,i); 273555714Skris if (sk_X509_NAME_set(sk,i,X509_NAME_dup(xn)) == NULL) 273655714Skris { 273755714Skris X509_NAME_free(xn); 273855714Skris goto err; 273955714Skris } 274055714Skris } 274155714Skris } 274255714Skris 274355714Skris if (0) 274455714Skris { 274555714Skriserr: 274655714Skris if (ret != NULL) SSL_free(ret); 274755714Skris ret=NULL; 274855714Skris } 274955714Skris return(ret); 275055714Skris } 275155714Skris 275255714Skrisvoid ssl_clear_cipher_ctx(SSL *s) 275355714Skris { 275455714Skris if (s->enc_read_ctx != NULL) 275555714Skris { 275655714Skris EVP_CIPHER_CTX_cleanup(s->enc_read_ctx); 275768651Skris OPENSSL_free(s->enc_read_ctx); 275855714Skris s->enc_read_ctx=NULL; 275955714Skris } 276055714Skris if (s->enc_write_ctx != NULL) 276155714Skris { 276255714Skris EVP_CIPHER_CTX_cleanup(s->enc_write_ctx); 276368651Skris OPENSSL_free(s->enc_write_ctx); 276455714Skris s->enc_write_ctx=NULL; 276555714Skris } 2766160814Ssimon#ifndef OPENSSL_NO_COMP 276755714Skris if (s->expand != NULL) 276855714Skris { 276955714Skris COMP_CTX_free(s->expand); 277055714Skris s->expand=NULL; 277155714Skris } 277255714Skris if (s->compress != NULL) 277355714Skris { 277455714Skris COMP_CTX_free(s->compress); 277555714Skris s->compress=NULL; 277655714Skris } 2777160814Ssimon#endif 277855714Skris } 277955714Skris 278055714Skris/* Fix this function so that it takes an optional type parameter */ 2781160814SsimonX509 *SSL_get_certificate(const SSL *s) 278255714Skris { 278355714Skris if (s->cert != NULL) 278455714Skris return(s->cert->key->x509); 278555714Skris else 278655714Skris return(NULL); 278755714Skris } 278855714Skris 278955714Skris/* Fix this function so that it takes an optional type parameter */ 279055714SkrisEVP_PKEY *SSL_get_privatekey(SSL *s) 279155714Skris { 279255714Skris if (s->cert != NULL) 279355714Skris return(s->cert->key->privatekey); 279455714Skris else 279555714Skris return(NULL); 279655714Skris } 279755714Skris 2798238405Sjkimconst SSL_CIPHER *SSL_get_current_cipher(const SSL *s) 279955714Skris { 280055714Skris if ((s->session != NULL) && (s->session->cipher != NULL)) 280155714Skris return(s->session->cipher); 280255714Skris return(NULL); 280355714Skris } 2804160814Ssimon#ifdef OPENSSL_NO_COMP 2805160814Ssimonconst void *SSL_get_current_compression(SSL *s) 2806160814Ssimon { 2807160814Ssimon return NULL; 2808160814Ssimon } 2809160814Ssimonconst void *SSL_get_current_expansion(SSL *s) 2810160814Ssimon { 2811160814Ssimon return NULL; 2812160814Ssimon } 2813160814Ssimon#else 281455714Skris 2815160814Ssimonconst COMP_METHOD *SSL_get_current_compression(SSL *s) 2816160814Ssimon { 2817160814Ssimon if (s->compress != NULL) 2818160814Ssimon return(s->compress->meth); 2819160814Ssimon return(NULL); 2820160814Ssimon } 2821160814Ssimon 2822160814Ssimonconst COMP_METHOD *SSL_get_current_expansion(SSL *s) 2823160814Ssimon { 2824160814Ssimon if (s->expand != NULL) 2825160814Ssimon return(s->expand->meth); 2826160814Ssimon return(NULL); 2827160814Ssimon } 2828160814Ssimon#endif 2829160814Ssimon 283055714Skrisint ssl_init_wbio_buffer(SSL *s,int push) 283155714Skris { 283255714Skris BIO *bbio; 283355714Skris 283455714Skris if (s->bbio == NULL) 283555714Skris { 283655714Skris bbio=BIO_new(BIO_f_buffer()); 283755714Skris if (bbio == NULL) return(0); 283855714Skris s->bbio=bbio; 283955714Skris } 284055714Skris else 284155714Skris { 284255714Skris bbio=s->bbio; 284355714Skris if (s->bbio == s->wbio) 284455714Skris s->wbio=BIO_pop(s->wbio); 284555714Skris } 284655714Skris (void)BIO_reset(bbio); 284755714Skris/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */ 284855714Skris if (!BIO_set_read_buffer_size(bbio,1)) 284955714Skris { 285055714Skris SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB); 285155714Skris return(0); 285255714Skris } 285355714Skris if (push) 285455714Skris { 285555714Skris if (s->wbio != bbio) 285655714Skris s->wbio=BIO_push(bbio,s->wbio); 285755714Skris } 285855714Skris else 285955714Skris { 286055714Skris if (s->wbio == bbio) 286155714Skris s->wbio=BIO_pop(bbio); 286255714Skris } 286355714Skris return(1); 286455714Skris } 286555714Skris 286655714Skrisvoid ssl_free_wbio_buffer(SSL *s) 286755714Skris { 286855714Skris if (s->bbio == NULL) return; 286955714Skris 287055714Skris if (s->bbio == s->wbio) 287155714Skris { 287255714Skris /* remove buffering */ 287368651Skris s->wbio=BIO_pop(s->wbio); 287468651Skris#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */ 287568651Skris assert(s->wbio != NULL); 2876238405Sjkim#endif 287768651Skris } 287855714Skris BIO_free(s->bbio); 287955714Skris s->bbio=NULL; 288055714Skris } 288155714Skris 288255714Skrisvoid SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode) 288355714Skris { 288455714Skris ctx->quiet_shutdown=mode; 288555714Skris } 288655714Skris 2887160814Ssimonint SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) 288855714Skris { 288955714Skris return(ctx->quiet_shutdown); 289055714Skris } 289155714Skris 289255714Skrisvoid SSL_set_quiet_shutdown(SSL *s,int mode) 289355714Skris { 289455714Skris s->quiet_shutdown=mode; 289555714Skris } 289655714Skris 2897160814Ssimonint SSL_get_quiet_shutdown(const SSL *s) 289855714Skris { 289955714Skris return(s->quiet_shutdown); 290055714Skris } 290155714Skris 290255714Skrisvoid SSL_set_shutdown(SSL *s,int mode) 290355714Skris { 290455714Skris s->shutdown=mode; 290555714Skris } 290655714Skris 2907160814Ssimonint SSL_get_shutdown(const SSL *s) 290855714Skris { 290955714Skris return(s->shutdown); 291055714Skris } 291155714Skris 2912160814Ssimonint SSL_version(const SSL *s) 291355714Skris { 291455714Skris return(s->version); 291555714Skris } 291655714Skris 2917160814SsimonSSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) 291855714Skris { 291955714Skris return(ssl->ctx); 292055714Skris } 292155714Skris 2922194206SsimonSSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx) 2923194206Ssimon { 2924194206Ssimon if (ssl->ctx == ctx) 2925194206Ssimon return ssl->ctx; 2926194206Ssimon#ifndef OPENSSL_NO_TLSEXT 2927194206Ssimon if (ctx == NULL) 2928194206Ssimon ctx = ssl->initial_ctx; 2929194206Ssimon#endif 2930194206Ssimon if (ssl->cert != NULL) 2931194206Ssimon ssl_cert_free(ssl->cert); 2932194206Ssimon ssl->cert = ssl_cert_dup(ctx->cert); 2933194206Ssimon CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX); 2934194206Ssimon if (ssl->ctx != NULL) 2935194206Ssimon SSL_CTX_free(ssl->ctx); /* decrement reference count */ 2936194206Ssimon ssl->ctx = ctx; 2937194206Ssimon return(ssl->ctx); 2938194206Ssimon } 2939194206Ssimon 2940109998Smarkm#ifndef OPENSSL_NO_STDIO 294155714Skrisint SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) 294255714Skris { 294355714Skris return(X509_STORE_set_default_paths(ctx->cert_store)); 294455714Skris } 294555714Skris 294655714Skrisint SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile, 294755714Skris const char *CApath) 294855714Skris { 2949160814Ssimon return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath)); 295055714Skris } 295155714Skris#endif 295255714Skris 2953109998Smarkmvoid SSL_set_info_callback(SSL *ssl, 2954167612Ssimon void (*cb)(const SSL *ssl,int type,int val)) 295555714Skris { 295655714Skris ssl->info_callback=cb; 295755714Skris } 295855714Skris 2959160814Ssimon/* One compiler (Diab DCC) doesn't like argument names in returned 2960160814Ssimon function pointer. */ 2961238405Sjkimvoid (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/) 296255714Skris { 2963109998Smarkm return ssl->info_callback; 296455714Skris } 296555714Skris 2966160814Ssimonint SSL_state(const SSL *ssl) 296755714Skris { 296855714Skris return(ssl->state); 296955714Skris } 297055714Skris 2971238405Sjkimvoid SSL_set_state(SSL *ssl, int state) 2972238405Sjkim { 2973238405Sjkim ssl->state = state; 2974238405Sjkim } 2975238405Sjkim 297655714Skrisvoid SSL_set_verify_result(SSL *ssl,long arg) 297755714Skris { 297855714Skris ssl->verify_result=arg; 297955714Skris } 298055714Skris 2981160814Ssimonlong SSL_get_verify_result(const SSL *ssl) 298255714Skris { 298355714Skris return(ssl->verify_result); 298455714Skris } 298555714Skris 298659191Skrisint SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func, 298759191Skris CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func) 298855714Skris { 2989109998Smarkm return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, 2990109998Smarkm new_func, dup_func, free_func); 299155714Skris } 299255714Skris 299355714Skrisint SSL_set_ex_data(SSL *s,int idx,void *arg) 299455714Skris { 299555714Skris return(CRYPTO_set_ex_data(&s->ex_data,idx,arg)); 299655714Skris } 299755714Skris 2998160814Ssimonvoid *SSL_get_ex_data(const SSL *s,int idx) 299955714Skris { 300055714Skris return(CRYPTO_get_ex_data(&s->ex_data,idx)); 300155714Skris } 300255714Skris 300359191Skrisint SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func, 300459191Skris CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func) 300555714Skris { 3006109998Smarkm return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, 3007109998Smarkm new_func, dup_func, free_func); 300855714Skris } 300955714Skris 301055714Skrisint SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg) 301155714Skris { 301255714Skris return(CRYPTO_set_ex_data(&s->ex_data,idx,arg)); 301355714Skris } 301455714Skris 3015160814Ssimonvoid *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx) 301655714Skris { 301755714Skris return(CRYPTO_get_ex_data(&s->ex_data,idx)); 301855714Skris } 301955714Skris 302055714Skrisint ssl_ok(SSL *s) 302155714Skris { 302255714Skris return(1); 302355714Skris } 302455714Skris 3025160814SsimonX509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) 302655714Skris { 302755714Skris return(ctx->cert_store); 302855714Skris } 302955714Skris 303055714Skrisvoid SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store) 303155714Skris { 303255714Skris if (ctx->cert_store != NULL) 303355714Skris X509_STORE_free(ctx->cert_store); 303455714Skris ctx->cert_store=store; 303555714Skris } 303655714Skris 3037160814Ssimonint SSL_want(const SSL *s) 303855714Skris { 303955714Skris return(s->rwstate); 304055714Skris } 304155714Skris 304255714Skris/*! 304355714Skris * \brief Set the callback for generating temporary RSA keys. 304455714Skris * \param ctx the SSL context. 304555714Skris * \param cb the callback 304655714Skris */ 304755714Skris 3048109998Smarkm#ifndef OPENSSL_NO_RSA 304955714Skrisvoid SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl, 305055714Skris int is_export, 305155714Skris int keylength)) 305259191Skris { 3053160814Ssimon SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); 305459191Skris } 305555714Skris 305659191Skrisvoid SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl, 305759191Skris int is_export, 305859191Skris int keylength)) 305959191Skris { 3060160814Ssimon SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); 306159191Skris } 306255714Skris#endif 306355714Skris 306455714Skris#ifdef DOXYGEN 306555714Skris/*! 306655714Skris * \brief The RSA temporary key callback function. 306755714Skris * \param ssl the SSL session. 306855714Skris * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite. 306955714Skris * \param keylength if \c is_export is \c TRUE, then \c keylength is the size 307055714Skris * of the required key in bits. 307155714Skris * \return the temporary RSA key. 307255714Skris * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback 307355714Skris */ 307455714Skris 307555714SkrisRSA *cb(SSL *ssl,int is_export,int keylength) 307655714Skris {} 307755714Skris#endif 307855714Skris 307955714Skris/*! 308055714Skris * \brief Set the callback for generating temporary DH keys. 308155714Skris * \param ctx the SSL context. 308255714Skris * \param dh the callback 308355714Skris */ 308455714Skris 3085109998Smarkm#ifndef OPENSSL_NO_DH 308655714Skrisvoid SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export, 3087238405Sjkim int keylength)) 3088109998Smarkm { 3089160814Ssimon SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); 3090109998Smarkm } 309155714Skris 309255714Skrisvoid SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export, 3093238405Sjkim int keylength)) 3094109998Smarkm { 3095160814Ssimon SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); 3096109998Smarkm } 309755714Skris#endif 309855714Skris 3099160814Ssimon#ifndef OPENSSL_NO_ECDH 3100160814Ssimonvoid SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export, 3101238405Sjkim int keylength)) 3102160814Ssimon { 3103160814Ssimon SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh); 3104160814Ssimon } 3105109998Smarkm 3106160814Ssimonvoid SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export, 3107238405Sjkim int keylength)) 3108160814Ssimon { 3109160814Ssimon SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh); 3110160814Ssimon } 3111160814Ssimon#endif 3112160814Ssimon 3113238405Sjkim#ifndef OPENSSL_NO_PSK 3114238405Sjkimint SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) 3115238405Sjkim { 3116238405Sjkim if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) 3117238405Sjkim { 3118238405Sjkim SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG); 3119238405Sjkim return 0; 3120238405Sjkim } 3121238405Sjkim if (ctx->psk_identity_hint != NULL) 3122238405Sjkim OPENSSL_free(ctx->psk_identity_hint); 3123238405Sjkim if (identity_hint != NULL) 3124238405Sjkim { 3125238405Sjkim ctx->psk_identity_hint = BUF_strdup(identity_hint); 3126238405Sjkim if (ctx->psk_identity_hint == NULL) 3127238405Sjkim return 0; 3128238405Sjkim } 3129238405Sjkim else 3130238405Sjkim ctx->psk_identity_hint = NULL; 3131238405Sjkim return 1; 3132238405Sjkim } 3133160814Ssimon 3134238405Sjkimint SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) 3135238405Sjkim { 3136238405Sjkim if (s == NULL) 3137238405Sjkim return 0; 3138238405Sjkim 3139238405Sjkim if (s->session == NULL) 3140238405Sjkim return 1; /* session not created yet, ignored */ 3141238405Sjkim 3142238405Sjkim if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) 3143238405Sjkim { 3144238405Sjkim SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG); 3145238405Sjkim return 0; 3146238405Sjkim } 3147238405Sjkim if (s->session->psk_identity_hint != NULL) 3148238405Sjkim OPENSSL_free(s->session->psk_identity_hint); 3149238405Sjkim if (identity_hint != NULL) 3150238405Sjkim { 3151238405Sjkim s->session->psk_identity_hint = BUF_strdup(identity_hint); 3152238405Sjkim if (s->session->psk_identity_hint == NULL) 3153238405Sjkim return 0; 3154238405Sjkim } 3155238405Sjkim else 3156238405Sjkim s->session->psk_identity_hint = NULL; 3157238405Sjkim return 1; 3158238405Sjkim } 3159238405Sjkim 3160238405Sjkimconst char *SSL_get_psk_identity_hint(const SSL *s) 3161238405Sjkim { 3162238405Sjkim if (s == NULL || s->session == NULL) 3163238405Sjkim return NULL; 3164238405Sjkim return(s->session->psk_identity_hint); 3165238405Sjkim } 3166238405Sjkim 3167238405Sjkimconst char *SSL_get_psk_identity(const SSL *s) 3168238405Sjkim { 3169238405Sjkim if (s == NULL || s->session == NULL) 3170238405Sjkim return NULL; 3171238405Sjkim return(s->session->psk_identity); 3172238405Sjkim } 3173238405Sjkim 3174238405Sjkimvoid SSL_set_psk_client_callback(SSL *s, 3175238405Sjkim unsigned int (*cb)(SSL *ssl, const char *hint, 3176238405Sjkim char *identity, unsigned int max_identity_len, unsigned char *psk, 3177238405Sjkim unsigned int max_psk_len)) 3178238405Sjkim { 3179238405Sjkim s->psk_client_callback = cb; 3180238405Sjkim } 3181238405Sjkim 3182238405Sjkimvoid SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, 3183238405Sjkim unsigned int (*cb)(SSL *ssl, const char *hint, 3184238405Sjkim char *identity, unsigned int max_identity_len, unsigned char *psk, 3185238405Sjkim unsigned int max_psk_len)) 3186238405Sjkim { 3187238405Sjkim ctx->psk_client_callback = cb; 3188238405Sjkim } 3189238405Sjkim 3190238405Sjkimvoid SSL_set_psk_server_callback(SSL *s, 3191238405Sjkim unsigned int (*cb)(SSL *ssl, const char *identity, 3192238405Sjkim unsigned char *psk, unsigned int max_psk_len)) 3193238405Sjkim { 3194238405Sjkim s->psk_server_callback = cb; 3195238405Sjkim } 3196238405Sjkim 3197238405Sjkimvoid SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, 3198238405Sjkim unsigned int (*cb)(SSL *ssl, const char *identity, 3199238405Sjkim unsigned char *psk, unsigned int max_psk_len)) 3200238405Sjkim { 3201238405Sjkim ctx->psk_server_callback = cb; 3202238405Sjkim } 3203238405Sjkim#endif 3204238405Sjkim 3205109998Smarkmvoid 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)) 3206109998Smarkm { 3207160814Ssimon SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); 3208109998Smarkm } 3209109998Smarkmvoid 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)) 3210109998Smarkm { 3211160814Ssimon SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); 3212109998Smarkm } 3213109998Smarkm 3214238405Sjkim/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer 3215238405Sjkim * vairable, freeing EVP_MD_CTX previously stored in that variable, if 3216238405Sjkim * any. If EVP_MD pointer is passed, initializes ctx with this md 3217238405Sjkim * Returns newly allocated ctx; 3218238405Sjkim */ 3219109998Smarkm 3220238405SjkimEVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md) 3221238405Sjkim{ 3222238405Sjkim ssl_clear_hash_ctx(hash); 3223238405Sjkim *hash = EVP_MD_CTX_create(); 3224238405Sjkim if (md) EVP_DigestInit_ex(*hash,md,NULL); 3225238405Sjkim return *hash; 3226238405Sjkim} 3227238405Sjkimvoid ssl_clear_hash_ctx(EVP_MD_CTX **hash) 3228238405Sjkim{ 3229109998Smarkm 3230238405Sjkim if (*hash) EVP_MD_CTX_destroy(*hash); 3231238405Sjkim *hash=NULL; 3232238405Sjkim} 3233238405Sjkim 3234238405Sjkimvoid SSL_set_debug(SSL *s, int debug) 3235238405Sjkim { 3236238405Sjkim s->debug = debug; 3237238405Sjkim } 3238238405Sjkim 3239238405Sjkimint SSL_cache_hit(SSL *s) 3240238405Sjkim { 3241238405Sjkim return s->hit; 3242238405Sjkim } 3243238405Sjkim 3244109998Smarkm#if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16) 324555714Skris#include "../crypto/bio/bss_file.c" 324655714Skris#endif 324755714Skris 324855714SkrisIMPLEMENT_STACK_OF(SSL_CIPHER) 324955714SkrisIMPLEMENT_STACK_OF(SSL_COMP) 3250238405SjkimIMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, 3251238405Sjkim ssl_cipher_id); 3252