1296341Sdelphij/* 2296341Sdelphij * ! \file ssl/ssl_lib.c \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. 10296341Sdelphij * 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). 17296341Sdelphij * 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. 24296341Sdelphij * 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 :-). 39296341Sdelphij * 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)" 42296341Sdelphij * 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. 54296341Sdelphij * 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 68296341Sdelphij * 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. 115296341Sdelphij * 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 146296341Sdelphij# 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 157296341Sdelphij# include <openssl/dh.h> 158160814Ssimon#endif 159194206Ssimon#ifndef OPENSSL_NO_ENGINE 160296341Sdelphij# include <openssl/engine.h> 161194206Ssimon#endif 16255714Skris 163296341Sdelphijconst char *SSL_version_str = OPENSSL_VERSION_TEXT; 16455714Skris 165296341SdelphijSSL3_ENC_METHOD ssl3_undef_enc_method = { 166296341Sdelphij /* 167296341Sdelphij * evil casts, but these functions are only called if there's a library 168296341Sdelphij * bug 169296341Sdelphij */ 170296341Sdelphij (int (*)(SSL *, int))ssl_undefined_function, 171296341Sdelphij (int (*)(SSL *, unsigned char *, int))ssl_undefined_function, 172296341Sdelphij ssl_undefined_function, 173296341Sdelphij (int (*)(SSL *, unsigned char *, unsigned char *, int)) 174296341Sdelphij ssl_undefined_function, 175296341Sdelphij (int (*)(SSL *, int))ssl_undefined_function, 176296341Sdelphij (int (*)(SSL *, const char *, int, unsigned char *)) 177296341Sdelphij ssl_undefined_function, 178296341Sdelphij 0, /* finish_mac_length */ 179296341Sdelphij (int (*)(SSL *, int, unsigned char *))ssl_undefined_function, 180296341Sdelphij NULL, /* client_finished_label */ 181296341Sdelphij 0, /* client_finished_label_len */ 182296341Sdelphij NULL, /* server_finished_label */ 183296341Sdelphij 0, /* server_finished_label_len */ 184296341Sdelphij (int (*)(int))ssl_undefined_function, 185296341Sdelphij (int (*)(SSL *, unsigned char *, size_t, const char *, 186296341Sdelphij size_t, const unsigned char *, size_t, 187296341Sdelphij int use_context))ssl_undefined_function, 188296341Sdelphij}; 18955714Skris 19055714Skrisint SSL_clear(SSL *s) 191296341Sdelphij{ 19255714Skris 193296341Sdelphij if (s->method == NULL) { 194296341Sdelphij SSLerr(SSL_F_SSL_CLEAR, SSL_R_NO_METHOD_SPECIFIED); 195296341Sdelphij return (0); 196296341Sdelphij } 19755714Skris 198296341Sdelphij if (ssl_clear_bad_session(s)) { 199296341Sdelphij SSL_SESSION_free(s->session); 200296341Sdelphij s->session = NULL; 201296341Sdelphij } 202100928Snectar 203296341Sdelphij s->error = 0; 204296341Sdelphij s->hit = 0; 205296341Sdelphij s->shutdown = 0; 20655714Skris 207296341Sdelphij#if 0 208296341Sdelphij /* 209296341Sdelphij * Disabled since version 1.10 of this file (early return not 210296341Sdelphij * needed because SSL_clear is not called when doing renegotiation) 211296341Sdelphij */ 212296341Sdelphij /* 213296341Sdelphij * This is set if we are doing dynamic renegotiation so keep 214296341Sdelphij * the old cipher. It is sort of a SSL_clear_lite :-) 215296341Sdelphij */ 216296341Sdelphij if (s->renegotiate) 217296341Sdelphij return (1); 21859191Skris#else 219296341Sdelphij if (s->renegotiate) { 220296341Sdelphij SSLerr(SSL_F_SSL_CLEAR, ERR_R_INTERNAL_ERROR); 221296341Sdelphij return 0; 222296341Sdelphij } 22355714Skris#endif 22455714Skris 225296341Sdelphij s->type = 0; 22655714Skris 227296341Sdelphij s->state = SSL_ST_BEFORE | ((s->server) ? SSL_ST_ACCEPT : SSL_ST_CONNECT); 22855714Skris 229296341Sdelphij s->version = s->method->version; 230296341Sdelphij s->client_version = s->version; 231296341Sdelphij s->rwstate = SSL_NOTHING; 232296341Sdelphij s->rstate = SSL_ST_READ_HEADER; 23372613Skris#if 0 234296341Sdelphij s->read_ahead = s->ctx->read_ahead; 23572613Skris#endif 23655714Skris 237296341Sdelphij if (s->init_buf != NULL) { 238296341Sdelphij BUF_MEM_free(s->init_buf); 239296341Sdelphij s->init_buf = NULL; 240296341Sdelphij } 24155714Skris 242296341Sdelphij ssl_clear_cipher_ctx(s); 243296341Sdelphij ssl_clear_hash_ctx(&s->read_hash); 244296341Sdelphij ssl_clear_hash_ctx(&s->write_hash); 24555714Skris 246296341Sdelphij s->first_packet = 0; 24755714Skris 24855714Skris#if 1 249296341Sdelphij /* 250296341Sdelphij * Check to see if we were changed into a different method, if so, revert 251296341Sdelphij * back if we are not doing session-id reuse. 252296341Sdelphij */ 253296341Sdelphij if (!s->in_handshake && (s->session == NULL) 254296341Sdelphij && (s->method != s->ctx->method)) { 255296341Sdelphij s->method->ssl_free(s); 256296341Sdelphij s->method = s->ctx->method; 257296341Sdelphij if (!s->method->ssl_new(s)) 258296341Sdelphij return (0); 259296341Sdelphij } else 26055714Skris#endif 261296341Sdelphij s->method->ssl_clear(s); 262296341Sdelphij return (1); 263296341Sdelphij} 26455714Skris 26555714Skris/** Used to change an SSL_CTXs default SSL method type */ 266296341Sdelphijint SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth) 267296341Sdelphij{ 268296341Sdelphij STACK_OF(SSL_CIPHER) *sk; 26955714Skris 270296341Sdelphij ctx->method = meth; 27155714Skris 272296341Sdelphij sk = ssl_create_cipher_list(ctx->method, &(ctx->cipher_list), 273296341Sdelphij &(ctx->cipher_list_by_id), 274296341Sdelphij meth->version == 275296341Sdelphij SSL2_VERSION ? "SSLv2" : 276296341Sdelphij SSL_DEFAULT_CIPHER_LIST); 277296341Sdelphij if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) { 278296341Sdelphij SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION, 279296341Sdelphij SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS); 280296341Sdelphij return (0); 281296341Sdelphij } 282296341Sdelphij return (1); 283296341Sdelphij} 28455714Skris 28555714SkrisSSL *SSL_new(SSL_CTX *ctx) 286296341Sdelphij{ 287296341Sdelphij SSL *s; 28855714Skris 289296341Sdelphij if (ctx == NULL) { 290296341Sdelphij SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX); 291296341Sdelphij return (NULL); 292296341Sdelphij } 293296341Sdelphij if (ctx->method == NULL) { 294296341Sdelphij SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION); 295296341Sdelphij return (NULL); 296296341Sdelphij } 29755714Skris 298296341Sdelphij s = (SSL *)OPENSSL_malloc(sizeof(SSL)); 299296341Sdelphij if (s == NULL) 300296341Sdelphij goto err; 301296341Sdelphij memset(s, 0, sizeof(SSL)); 30255714Skris 303296341Sdelphij#ifndef OPENSSL_NO_KRB5 304296341Sdelphij s->kssl_ctx = kssl_ctx_new(); 305296341Sdelphij#endif /* OPENSSL_NO_KRB5 */ 306109998Smarkm 307296341Sdelphij s->options = ctx->options; 308296341Sdelphij s->mode = ctx->mode; 309296341Sdelphij s->max_cert_list = ctx->max_cert_list; 310109998Smarkm 311296341Sdelphij if (ctx->cert != NULL) { 312296341Sdelphij /* 313296341Sdelphij * Earlier library versions used to copy the pointer to the CERT, not 314296341Sdelphij * its contents; only when setting new parameters for the per-SSL 315296341Sdelphij * copy, ssl_cert_new would be called (and the direct reference to 316296341Sdelphij * the per-SSL_CTX settings would be lost, but those still were 317296341Sdelphij * indirectly accessed for various purposes, and for that reason they 318296341Sdelphij * used to be known as s->ctx->default_cert). Now we don't look at the 319296341Sdelphij * SSL_CTX's CERT after having duplicated it once. 320296341Sdelphij */ 32155714Skris 322296341Sdelphij s->cert = ssl_cert_dup(ctx->cert); 323296341Sdelphij if (s->cert == NULL) 324296341Sdelphij goto err; 325296341Sdelphij } else 326296341Sdelphij s->cert = NULL; /* Cannot really happen (see SSL_CTX_new) */ 327109998Smarkm 328296341Sdelphij s->read_ahead = ctx->read_ahead; 329296341Sdelphij s->msg_callback = ctx->msg_callback; 330296341Sdelphij s->msg_callback_arg = ctx->msg_callback_arg; 331296341Sdelphij s->verify_mode = ctx->verify_mode; 332160814Ssimon#if 0 333296341Sdelphij s->verify_depth = ctx->verify_depth; 334160814Ssimon#endif 335296341Sdelphij s->sid_ctx_length = ctx->sid_ctx_length; 336296341Sdelphij OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx); 337296341Sdelphij memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx)); 338296341Sdelphij s->verify_callback = ctx->default_verify_callback; 339296341Sdelphij s->generate_session_id = ctx->generate_session_id; 340160814Ssimon 341296341Sdelphij s->param = X509_VERIFY_PARAM_new(); 342296341Sdelphij if (!s->param) 343296341Sdelphij goto err; 344296341Sdelphij X509_VERIFY_PARAM_inherit(s->param, ctx->param); 345160814Ssimon#if 0 346296341Sdelphij s->purpose = ctx->purpose; 347296341Sdelphij s->trust = ctx->trust; 348160814Ssimon#endif 349296341Sdelphij s->quiet_shutdown = ctx->quiet_shutdown; 350296341Sdelphij s->max_send_fragment = ctx->max_send_fragment; 351109998Smarkm 352296341Sdelphij CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX); 353296341Sdelphij s->ctx = ctx; 354194206Ssimon#ifndef OPENSSL_NO_TLSEXT 355296341Sdelphij s->tlsext_debug_cb = 0; 356296341Sdelphij s->tlsext_debug_arg = NULL; 357296341Sdelphij s->tlsext_ticket_expected = 0; 358296341Sdelphij s->tlsext_status_type = -1; 359296341Sdelphij s->tlsext_status_expected = 0; 360296341Sdelphij s->tlsext_ocsp_ids = NULL; 361296341Sdelphij s->tlsext_ocsp_exts = NULL; 362296341Sdelphij s->tlsext_ocsp_resp = NULL; 363296341Sdelphij s->tlsext_ocsp_resplen = -1; 364296341Sdelphij CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX); 365296341Sdelphij s->initial_ctx = ctx; 366238405Sjkim# ifndef OPENSSL_NO_NEXTPROTONEG 367296341Sdelphij s->next_proto_negotiated = NULL; 368238405Sjkim# endif 369194206Ssimon#endif 370238405Sjkim 371296341Sdelphij s->verify_result = X509_V_OK; 37255714Skris 373296341Sdelphij s->method = ctx->method; 37455714Skris 375296341Sdelphij if (!s->method->ssl_new(s)) 376296341Sdelphij goto err; 37755714Skris 378296341Sdelphij s->references = 1; 379296341Sdelphij s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1; 380109998Smarkm 381296341Sdelphij SSL_clear(s); 38255714Skris 383296341Sdelphij CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data); 38455714Skris 385238405Sjkim#ifndef OPENSSL_NO_PSK 386296341Sdelphij s->psk_client_callback = ctx->psk_client_callback; 387296341Sdelphij s->psk_server_callback = ctx->psk_server_callback; 388238405Sjkim#endif 389238405Sjkim 390296341Sdelphij return (s); 391296341Sdelphij err: 392296341Sdelphij if (s != NULL) 393296341Sdelphij SSL_free(s); 394296341Sdelphij SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE); 395296341Sdelphij return (NULL); 396296341Sdelphij} 39755714Skris 398296341Sdelphijint SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx, 399296341Sdelphij unsigned int sid_ctx_len) 400296341Sdelphij{ 401296341Sdelphij if (sid_ctx_len > sizeof ctx->sid_ctx) { 402296341Sdelphij SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT, 403296341Sdelphij SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); 404296341Sdelphij return 0; 405296341Sdelphij } 406296341Sdelphij ctx->sid_ctx_length = sid_ctx_len; 407296341Sdelphij memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len); 40855714Skris 40955714Skris return 1; 410296341Sdelphij} 411296341Sdelphij 412296341Sdelphijint SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx, 413296341Sdelphij unsigned int sid_ctx_len) 414296341Sdelphij{ 415296341Sdelphij if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) { 416296341Sdelphij SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT, 417296341Sdelphij SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); 418296341Sdelphij return 0; 41955714Skris } 420296341Sdelphij ssl->sid_ctx_length = sid_ctx_len; 421296341Sdelphij memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len); 42255714Skris 42355714Skris return 1; 424296341Sdelphij} 42555714Skris 426109998Smarkmint SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) 427296341Sdelphij{ 428296341Sdelphij CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); 429296341Sdelphij ctx->generate_session_id = cb; 430296341Sdelphij CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); 431296341Sdelphij return 1; 432296341Sdelphij} 433109998Smarkm 434109998Smarkmint SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) 435296341Sdelphij{ 436296341Sdelphij CRYPTO_w_lock(CRYPTO_LOCK_SSL); 437296341Sdelphij ssl->generate_session_id = cb; 438296341Sdelphij CRYPTO_w_unlock(CRYPTO_LOCK_SSL); 439296341Sdelphij return 1; 440296341Sdelphij} 44159191Skris 442109998Smarkmint SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id, 443296341Sdelphij unsigned int id_len) 444296341Sdelphij{ 445296341Sdelphij /* 446296341Sdelphij * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how 447296341Sdelphij * we can "construct" a session to give us the desired check - ie. to 448296341Sdelphij * find if there's a session in the hash table that would conflict with 449296341Sdelphij * any new session built out of this id/id_len and the ssl_version in use 450296341Sdelphij * by this SSL. 451296341Sdelphij */ 452296341Sdelphij SSL_SESSION r, *p; 453109998Smarkm 454296341Sdelphij if (id_len > sizeof r.session_id) 455296341Sdelphij return 0; 456109998Smarkm 457296341Sdelphij r.ssl_version = ssl->version; 458296341Sdelphij r.session_id_length = id_len; 459296341Sdelphij memcpy(r.session_id, id, id_len); 460296341Sdelphij /* 461296341Sdelphij * NB: SSLv2 always uses a fixed 16-byte session ID, so even if a 462296341Sdelphij * callback is calling us to check the uniqueness of a shorter ID, it 463296341Sdelphij * must be compared as a padded-out ID because that is what it will be 464296341Sdelphij * converted to when the callback has finished choosing it. 465296341Sdelphij */ 466296341Sdelphij if ((r.ssl_version == SSL2_VERSION) && 467296341Sdelphij (id_len < SSL2_SSL_SESSION_ID_LENGTH)) { 468296341Sdelphij memset(r.session_id + id_len, 0, SSL2_SSL_SESSION_ID_LENGTH - id_len); 469296341Sdelphij r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH; 470296341Sdelphij } 471109998Smarkm 472296341Sdelphij CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); 473296341Sdelphij p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r); 474296341Sdelphij CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); 475296341Sdelphij return (p != NULL); 476296341Sdelphij} 477109998Smarkm 478109998Smarkmint SSL_CTX_set_purpose(SSL_CTX *s, int purpose) 479296341Sdelphij{ 480296341Sdelphij return X509_VERIFY_PARAM_set_purpose(s->param, purpose); 481296341Sdelphij} 482109998Smarkm 483109998Smarkmint SSL_set_purpose(SSL *s, int purpose) 484296341Sdelphij{ 485296341Sdelphij return X509_VERIFY_PARAM_set_purpose(s->param, purpose); 486296341Sdelphij} 487109998Smarkm 48859191Skrisint SSL_CTX_set_trust(SSL_CTX *s, int trust) 489296341Sdelphij{ 490296341Sdelphij return X509_VERIFY_PARAM_set_trust(s->param, trust); 491296341Sdelphij} 49259191Skris 49359191Skrisint SSL_set_trust(SSL *s, int trust) 494296341Sdelphij{ 495296341Sdelphij return X509_VERIFY_PARAM_set_trust(s->param, trust); 496296341Sdelphij} 49759191Skris 498238405Sjkimint SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) 499296341Sdelphij{ 500296341Sdelphij return X509_VERIFY_PARAM_set1(ctx->param, vpm); 501296341Sdelphij} 502238405Sjkim 503238405Sjkimint SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) 504296341Sdelphij{ 505296341Sdelphij return X509_VERIFY_PARAM_set1(ssl->param, vpm); 506296341Sdelphij} 507238405Sjkim 50855714Skrisvoid SSL_free(SSL *s) 509296341Sdelphij{ 510296341Sdelphij int i; 51155714Skris 512296341Sdelphij if (s == NULL) 513296341Sdelphij return; 51455714Skris 515296341Sdelphij i = CRYPTO_add(&s->references, -1, CRYPTO_LOCK_SSL); 51655714Skris#ifdef REF_PRINT 517296341Sdelphij REF_PRINT("SSL", s); 51855714Skris#endif 519296341Sdelphij if (i > 0) 520296341Sdelphij return; 52155714Skris#ifdef REF_CHECK 522296341Sdelphij if (i < 0) { 523296341Sdelphij fprintf(stderr, "SSL_free, bad reference count\n"); 524296341Sdelphij abort(); /* ok */ 525296341Sdelphij } 52655714Skris#endif 52755714Skris 528296341Sdelphij if (s->param) 529296341Sdelphij X509_VERIFY_PARAM_free(s->param); 530160814Ssimon 531296341Sdelphij CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data); 53255714Skris 533296341Sdelphij if (s->bbio != NULL) { 534296341Sdelphij /* If the buffering BIO is in place, pop it off */ 535296341Sdelphij if (s->bbio == s->wbio) { 536296341Sdelphij s->wbio = BIO_pop(s->wbio); 537296341Sdelphij } 538296341Sdelphij BIO_free(s->bbio); 539296341Sdelphij s->bbio = NULL; 540296341Sdelphij } 541296341Sdelphij if (s->rbio != NULL) 542296341Sdelphij BIO_free_all(s->rbio); 543296341Sdelphij if ((s->wbio != NULL) && (s->wbio != s->rbio)) 544296341Sdelphij BIO_free_all(s->wbio); 54555714Skris 546296341Sdelphij if (s->init_buf != NULL) 547296341Sdelphij BUF_MEM_free(s->init_buf); 54855714Skris 549296341Sdelphij /* add extra stuff */ 550296341Sdelphij if (s->cipher_list != NULL) 551296341Sdelphij sk_SSL_CIPHER_free(s->cipher_list); 552296341Sdelphij if (s->cipher_list_by_id != NULL) 553296341Sdelphij sk_SSL_CIPHER_free(s->cipher_list_by_id); 55455714Skris 555296341Sdelphij /* Make the next call work :-) */ 556296341Sdelphij if (s->session != NULL) { 557296341Sdelphij ssl_clear_bad_session(s); 558296341Sdelphij SSL_SESSION_free(s->session); 559296341Sdelphij } 56055714Skris 561296341Sdelphij ssl_clear_cipher_ctx(s); 562296341Sdelphij ssl_clear_hash_ctx(&s->read_hash); 563296341Sdelphij ssl_clear_hash_ctx(&s->write_hash); 56455714Skris 565296341Sdelphij if (s->cert != NULL) 566296341Sdelphij ssl_cert_free(s->cert); 567296341Sdelphij /* Free up if allocated */ 56855714Skris 569194206Ssimon#ifndef OPENSSL_NO_TLSEXT 570296341Sdelphij if (s->tlsext_hostname) 571296341Sdelphij OPENSSL_free(s->tlsext_hostname); 572296341Sdelphij if (s->initial_ctx) 573296341Sdelphij SSL_CTX_free(s->initial_ctx); 574296341Sdelphij# ifndef OPENSSL_NO_EC 575296341Sdelphij if (s->tlsext_ecpointformatlist) 576296341Sdelphij OPENSSL_free(s->tlsext_ecpointformatlist); 577296341Sdelphij if (s->tlsext_ellipticcurvelist) 578296341Sdelphij OPENSSL_free(s->tlsext_ellipticcurvelist); 579296341Sdelphij# endif /* OPENSSL_NO_EC */ 580296341Sdelphij if (s->tlsext_opaque_prf_input) 581296341Sdelphij OPENSSL_free(s->tlsext_opaque_prf_input); 582296341Sdelphij if (s->tlsext_ocsp_exts) 583296341Sdelphij sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, X509_EXTENSION_free); 584296341Sdelphij if (s->tlsext_ocsp_ids) 585296341Sdelphij sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free); 586296341Sdelphij if (s->tlsext_ocsp_resp) 587296341Sdelphij OPENSSL_free(s->tlsext_ocsp_resp); 588194206Ssimon#endif 589238405Sjkim 590296341Sdelphij if (s->client_CA != NULL) 591296341Sdelphij sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free); 59255714Skris 593296341Sdelphij if (s->method != NULL) 594296341Sdelphij s->method->ssl_free(s); 59555714Skris 596296341Sdelphij if (s->ctx) 597296341Sdelphij SSL_CTX_free(s->ctx); 598205128Ssimon 599296341Sdelphij#ifndef OPENSSL_NO_KRB5 600296341Sdelphij if (s->kssl_ctx != NULL) 601296341Sdelphij kssl_ctx_free(s->kssl_ctx); 602296341Sdelphij#endif /* OPENSSL_NO_KRB5 */ 603120631Snectar 604238405Sjkim#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) 605296341Sdelphij if (s->next_proto_negotiated) 606296341Sdelphij OPENSSL_free(s->next_proto_negotiated); 607238405Sjkim#endif 608238405Sjkim 609246772Sjkim#ifndef OPENSSL_NO_SRTP 610296341Sdelphij if (s->srtp_profiles) 611296341Sdelphij sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles); 612246772Sjkim#endif 613238405Sjkim 614296341Sdelphij OPENSSL_free(s); 615296341Sdelphij} 61655714Skris 617296341Sdelphijvoid SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) 618296341Sdelphij{ 619296341Sdelphij /* 620296341Sdelphij * If the output buffering BIO is still in place, remove it 621296341Sdelphij */ 622296341Sdelphij if (s->bbio != NULL) { 623296341Sdelphij if (s->wbio == s->bbio) { 624296341Sdelphij s->wbio = s->wbio->next_bio; 625296341Sdelphij s->bbio->next_bio = NULL; 626296341Sdelphij } 627296341Sdelphij } 628296341Sdelphij if ((s->rbio != NULL) && (s->rbio != rbio)) 629296341Sdelphij BIO_free_all(s->rbio); 630296341Sdelphij if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio)) 631296341Sdelphij BIO_free_all(s->wbio); 632296341Sdelphij s->rbio = rbio; 633296341Sdelphij s->wbio = wbio; 634296341Sdelphij} 63555714Skris 636160814SsimonBIO *SSL_get_rbio(const SSL *s) 637296341Sdelphij{ 638296341Sdelphij return (s->rbio); 639296341Sdelphij} 64055714Skris 641160814SsimonBIO *SSL_get_wbio(const SSL *s) 642296341Sdelphij{ 643296341Sdelphij return (s->wbio); 644296341Sdelphij} 64555714Skris 646160814Ssimonint SSL_get_fd(const SSL *s) 647296341Sdelphij{ 648296341Sdelphij return (SSL_get_rfd(s)); 649296341Sdelphij} 65089837Skris 651160814Ssimonint SSL_get_rfd(const SSL *s) 652296341Sdelphij{ 653296341Sdelphij int ret = -1; 654296341Sdelphij BIO *b, *r; 65555714Skris 656296341Sdelphij b = SSL_get_rbio(s); 657296341Sdelphij r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR); 658296341Sdelphij if (r != NULL) 659296341Sdelphij BIO_get_fd(r, &ret); 660296341Sdelphij return (ret); 661296341Sdelphij} 66255714Skris 663160814Ssimonint SSL_get_wfd(const SSL *s) 664296341Sdelphij{ 665296341Sdelphij int ret = -1; 666296341Sdelphij BIO *b, *r; 66789837Skris 668296341Sdelphij b = SSL_get_wbio(s); 669296341Sdelphij r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR); 670296341Sdelphij if (r != NULL) 671296341Sdelphij BIO_get_fd(r, &ret); 672296341Sdelphij return (ret); 673296341Sdelphij} 67489837Skris 675109998Smarkm#ifndef OPENSSL_NO_SOCK 676296341Sdelphijint SSL_set_fd(SSL *s, int fd) 677296341Sdelphij{ 678296341Sdelphij int ret = 0; 679296341Sdelphij BIO *bio = NULL; 68055714Skris 681296341Sdelphij bio = BIO_new(BIO_s_socket()); 68255714Skris 683296341Sdelphij if (bio == NULL) { 684296341Sdelphij SSLerr(SSL_F_SSL_SET_FD, ERR_R_BUF_LIB); 685296341Sdelphij goto err; 686296341Sdelphij } 687296341Sdelphij BIO_set_fd(bio, fd, BIO_NOCLOSE); 688296341Sdelphij SSL_set_bio(s, bio, bio); 689296341Sdelphij ret = 1; 690296341Sdelphij err: 691296341Sdelphij return (ret); 692296341Sdelphij} 69355714Skris 694296341Sdelphijint SSL_set_wfd(SSL *s, int fd) 695296341Sdelphij{ 696296341Sdelphij int ret = 0; 697296341Sdelphij BIO *bio = NULL; 69855714Skris 699296341Sdelphij if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET) 700296341Sdelphij || ((int)BIO_get_fd(s->rbio, NULL) != fd)) { 701296341Sdelphij bio = BIO_new(BIO_s_socket()); 70255714Skris 703296341Sdelphij if (bio == NULL) { 704296341Sdelphij SSLerr(SSL_F_SSL_SET_WFD, ERR_R_BUF_LIB); 705296341Sdelphij goto err; 706296341Sdelphij } 707296341Sdelphij BIO_set_fd(bio, fd, BIO_NOCLOSE); 708296341Sdelphij SSL_set_bio(s, SSL_get_rbio(s), bio); 709296341Sdelphij } else 710296341Sdelphij SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s)); 711296341Sdelphij ret = 1; 712296341Sdelphij err: 713296341Sdelphij return (ret); 714296341Sdelphij} 71555714Skris 716296341Sdelphijint SSL_set_rfd(SSL *s, int fd) 717296341Sdelphij{ 718296341Sdelphij int ret = 0; 719296341Sdelphij BIO *bio = NULL; 72055714Skris 721296341Sdelphij if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET) 722296341Sdelphij || ((int)BIO_get_fd(s->wbio, NULL) != fd)) { 723296341Sdelphij bio = BIO_new(BIO_s_socket()); 72455714Skris 725296341Sdelphij if (bio == NULL) { 726296341Sdelphij SSLerr(SSL_F_SSL_SET_RFD, ERR_R_BUF_LIB); 727296341Sdelphij goto err; 728296341Sdelphij } 729296341Sdelphij BIO_set_fd(bio, fd, BIO_NOCLOSE); 730296341Sdelphij SSL_set_bio(s, bio, SSL_get_wbio(s)); 731296341Sdelphij } else 732296341Sdelphij SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s)); 733296341Sdelphij ret = 1; 734296341Sdelphij err: 735296341Sdelphij return (ret); 736296341Sdelphij} 73755714Skris#endif 73855714Skris 73959191Skris/* return length of latest Finished message we sent, copy to 'buf' */ 740160814Ssimonsize_t SSL_get_finished(const SSL *s, void *buf, size_t count) 741296341Sdelphij{ 742296341Sdelphij size_t ret = 0; 74359191Skris 744296341Sdelphij if (s->s3 != NULL) { 745296341Sdelphij ret = s->s3->tmp.finish_md_len; 746296341Sdelphij if (count > ret) 747296341Sdelphij count = ret; 748296341Sdelphij memcpy(buf, s->s3->tmp.finish_md, count); 749296341Sdelphij } 750296341Sdelphij return ret; 751296341Sdelphij} 752296341Sdelphij 75359191Skris/* return length of latest Finished message we expected, copy to 'buf' */ 754160814Ssimonsize_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) 755296341Sdelphij{ 756296341Sdelphij size_t ret = 0; 75759191Skris 758296341Sdelphij if (s->s3 != NULL) { 759296341Sdelphij ret = s->s3->tmp.peer_finish_md_len; 760296341Sdelphij if (count > ret) 761296341Sdelphij count = ret; 762296341Sdelphij memcpy(buf, s->s3->tmp.peer_finish_md, count); 763296341Sdelphij } 764296341Sdelphij return ret; 765296341Sdelphij} 76659191Skris 767160814Ssimonint SSL_get_verify_mode(const SSL *s) 768296341Sdelphij{ 769296341Sdelphij return (s->verify_mode); 770296341Sdelphij} 77155714Skris 772160814Ssimonint SSL_get_verify_depth(const SSL *s) 773296341Sdelphij{ 774296341Sdelphij return X509_VERIFY_PARAM_get_depth(s->param); 775296341Sdelphij} 77655714Skris 777296341Sdelphijint (*SSL_get_verify_callback(const SSL *s)) (int, X509_STORE_CTX *) { 778296341Sdelphij return (s->verify_callback); 779296341Sdelphij} 78055714Skris 781160814Ssimonint SSL_CTX_get_verify_mode(const SSL_CTX *ctx) 782296341Sdelphij{ 783296341Sdelphij return (ctx->verify_mode); 784296341Sdelphij} 78555714Skris 786160814Ssimonint SSL_CTX_get_verify_depth(const SSL_CTX *ctx) 787296341Sdelphij{ 788296341Sdelphij return X509_VERIFY_PARAM_get_depth(ctx->param); 789296341Sdelphij} 79055714Skris 791296341Sdelphijint (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx)) (int, X509_STORE_CTX *) { 792296341Sdelphij return (ctx->default_verify_callback); 793296341Sdelphij} 79455714Skris 795296341Sdelphijvoid SSL_set_verify(SSL *s, int mode, 796296341Sdelphij int (*callback) (int ok, X509_STORE_CTX *ctx)) 797296341Sdelphij{ 798296341Sdelphij s->verify_mode = mode; 799296341Sdelphij if (callback != NULL) 800296341Sdelphij s->verify_callback = callback; 801296341Sdelphij} 80255714Skris 803296341Sdelphijvoid SSL_set_verify_depth(SSL *s, int depth) 804296341Sdelphij{ 805296341Sdelphij X509_VERIFY_PARAM_set_depth(s->param, depth); 806296341Sdelphij} 80755714Skris 808296341Sdelphijvoid SSL_set_read_ahead(SSL *s, int yes) 809296341Sdelphij{ 810296341Sdelphij s->read_ahead = yes; 811296341Sdelphij} 81255714Skris 813160814Ssimonint SSL_get_read_ahead(const SSL *s) 814296341Sdelphij{ 815296341Sdelphij return (s->read_ahead); 816296341Sdelphij} 81755714Skris 818160814Ssimonint SSL_pending(const SSL *s) 819296341Sdelphij{ 820296341Sdelphij /* 821296341Sdelphij * SSL_pending cannot work properly if read-ahead is enabled 822296341Sdelphij * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is 823296341Sdelphij * impossible to fix since SSL_pending cannot report errors that may be 824296341Sdelphij * observed while scanning the new data. (Note that SSL_pending() is 825296341Sdelphij * often used as a boolean value, so we'd better not return -1.) 826296341Sdelphij */ 827296341Sdelphij return (s->method->ssl_pending(s)); 828296341Sdelphij} 82955714Skris 830160814SsimonX509 *SSL_get_peer_certificate(const SSL *s) 831296341Sdelphij{ 832296341Sdelphij X509 *r; 83355714Skris 834296341Sdelphij if ((s == NULL) || (s->session == NULL)) 835296341Sdelphij r = NULL; 836296341Sdelphij else 837296341Sdelphij r = s->session->peer; 83855714Skris 839296341Sdelphij if (r == NULL) 840296341Sdelphij return (r); 84155714Skris 842296341Sdelphij CRYPTO_add(&r->references, 1, CRYPTO_LOCK_X509); 84355714Skris 844296341Sdelphij return (r); 845296341Sdelphij} 846296341Sdelphij 847160814SsimonSTACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s) 848296341Sdelphij{ 849296341Sdelphij STACK_OF(X509) *r; 85055714Skris 851296341Sdelphij if ((s == NULL) || (s->session == NULL) 852296341Sdelphij || (s->session->sess_cert == NULL)) 853296341Sdelphij r = NULL; 854296341Sdelphij else 855296341Sdelphij r = s->session->sess_cert->cert_chain; 85655714Skris 857296341Sdelphij /* 858296341Sdelphij * If we are a client, cert_chain includes the peer's own certificate; if 859296341Sdelphij * we are a server, it does not. 860296341Sdelphij */ 86155714Skris 862296341Sdelphij return (r); 863296341Sdelphij} 86455714Skris 865296341Sdelphij/* 866296341Sdelphij * Now in theory, since the calling process own 't' it should be safe to 867296341Sdelphij * modify. We need to be able to read f without being hassled 868296341Sdelphij */ 869296341Sdelphijvoid SSL_copy_session_id(SSL *t, const SSL *f) 870296341Sdelphij{ 871296341Sdelphij CERT *tmp; 87255714Skris 873296341Sdelphij /* Do we need to to SSL locking? */ 874296341Sdelphij SSL_set_session(t, SSL_get_session(f)); 87555714Skris 876296341Sdelphij /* 877296341Sdelphij * what if we are setup as SSLv2 but want to talk SSLv3 or vice-versa 878296341Sdelphij */ 879296341Sdelphij if (t->method != f->method) { 880296341Sdelphij t->method->ssl_free(t); /* cleanup current */ 881296341Sdelphij t->method = f->method; /* change method */ 882296341Sdelphij t->method->ssl_new(t); /* setup new */ 883296341Sdelphij } 884296341Sdelphij 885296341Sdelphij tmp = t->cert; 886296341Sdelphij if (f->cert != NULL) { 887296341Sdelphij CRYPTO_add(&f->cert->references, 1, CRYPTO_LOCK_SSL_CERT); 888296341Sdelphij t->cert = f->cert; 889296341Sdelphij } else 890296341Sdelphij t->cert = NULL; 891296341Sdelphij if (tmp != NULL) 892296341Sdelphij ssl_cert_free(tmp); 893296341Sdelphij SSL_set_session_id_context(t, f->sid_ctx, f->sid_ctx_length); 894296341Sdelphij} 895296341Sdelphij 89655714Skris/* Fix this so it checks all the valid key/cert options */ 897160814Ssimonint SSL_CTX_check_private_key(const SSL_CTX *ctx) 898296341Sdelphij{ 899296341Sdelphij if ((ctx == NULL) || 900296341Sdelphij (ctx->cert == NULL) || (ctx->cert->key->x509 == NULL)) { 901296341Sdelphij SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY, 902296341Sdelphij SSL_R_NO_CERTIFICATE_ASSIGNED); 903296341Sdelphij return (0); 904296341Sdelphij } 905296341Sdelphij if (ctx->cert->key->privatekey == NULL) { 906296341Sdelphij SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY, 907296341Sdelphij SSL_R_NO_PRIVATE_KEY_ASSIGNED); 908296341Sdelphij return (0); 909296341Sdelphij } 910296341Sdelphij return (X509_check_private_key 911296341Sdelphij (ctx->cert->key->x509, ctx->cert->key->privatekey)); 912296341Sdelphij} 91355714Skris 91455714Skris/* Fix this function so that it takes an optional type parameter */ 915160814Ssimonint SSL_check_private_key(const SSL *ssl) 916296341Sdelphij{ 917296341Sdelphij if (ssl == NULL) { 918296341Sdelphij SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, ERR_R_PASSED_NULL_PARAMETER); 919296341Sdelphij return (0); 920296341Sdelphij } 921296341Sdelphij if (ssl->cert == NULL) { 922296341Sdelphij SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_CERTIFICATE_ASSIGNED); 923296341Sdelphij return 0; 924296341Sdelphij } 925296341Sdelphij if (ssl->cert->key->x509 == NULL) { 926296341Sdelphij SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_CERTIFICATE_ASSIGNED); 927296341Sdelphij return (0); 928296341Sdelphij } 929296341Sdelphij if (ssl->cert->key->privatekey == NULL) { 930296341Sdelphij SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_PRIVATE_KEY_ASSIGNED); 931296341Sdelphij return (0); 932296341Sdelphij } 933296341Sdelphij return (X509_check_private_key(ssl->cert->key->x509, 934296341Sdelphij ssl->cert->key->privatekey)); 935296341Sdelphij} 93655714Skris 93755714Skrisint SSL_accept(SSL *s) 938296341Sdelphij{ 939296341Sdelphij if (s->handshake_func == 0) 940296341Sdelphij /* Not properly initialized yet */ 941296341Sdelphij SSL_set_accept_state(s); 94255714Skris 943296341Sdelphij return (s->method->ssl_accept(s)); 944296341Sdelphij} 94555714Skris 94655714Skrisint SSL_connect(SSL *s) 947296341Sdelphij{ 948296341Sdelphij if (s->handshake_func == 0) 949296341Sdelphij /* Not properly initialized yet */ 950296341Sdelphij SSL_set_connect_state(s); 95155714Skris 952296341Sdelphij return (s->method->ssl_connect(s)); 953296341Sdelphij} 95455714Skris 955160814Ssimonlong SSL_get_default_timeout(const SSL *s) 956296341Sdelphij{ 957296341Sdelphij return (s->method->get_timeout()); 958296341Sdelphij} 95955714Skris 960296341Sdelphijint SSL_read(SSL *s, void *buf, int num) 961296341Sdelphij{ 962296341Sdelphij if (s->handshake_func == 0) { 963296341Sdelphij SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED); 964296341Sdelphij return -1; 965296341Sdelphij } 96655714Skris 967296341Sdelphij if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { 968296341Sdelphij s->rwstate = SSL_NOTHING; 969296341Sdelphij return (0); 970296341Sdelphij } 971296341Sdelphij return (s->method->ssl_read(s, buf, num)); 972296341Sdelphij} 97355714Skris 974296341Sdelphijint SSL_peek(SSL *s, void *buf, int num) 975296341Sdelphij{ 976296341Sdelphij if (s->handshake_func == 0) { 977296341Sdelphij SSLerr(SSL_F_SSL_PEEK, SSL_R_UNINITIALIZED); 978296341Sdelphij return -1; 979296341Sdelphij } 98076866Skris 981296341Sdelphij if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { 982296341Sdelphij return (0); 983296341Sdelphij } 984296341Sdelphij return (s->method->ssl_peek(s, buf, num)); 985296341Sdelphij} 98655714Skris 987296341Sdelphijint SSL_write(SSL *s, const void *buf, int num) 988296341Sdelphij{ 989296341Sdelphij if (s->handshake_func == 0) { 990296341Sdelphij SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED); 991296341Sdelphij return -1; 992296341Sdelphij } 99355714Skris 994296341Sdelphij if (s->shutdown & SSL_SENT_SHUTDOWN) { 995296341Sdelphij s->rwstate = SSL_NOTHING; 996296341Sdelphij SSLerr(SSL_F_SSL_WRITE, SSL_R_PROTOCOL_IS_SHUTDOWN); 997296341Sdelphij return (-1); 998296341Sdelphij } 999296341Sdelphij return (s->method->ssl_write(s, buf, num)); 1000296341Sdelphij} 100155714Skris 100255714Skrisint SSL_shutdown(SSL *s) 1003296341Sdelphij{ 1004296341Sdelphij /* 1005296341Sdelphij * Note that this function behaves differently from what one might 1006296341Sdelphij * expect. Return values are 0 for no success (yet), 1 for success; but 1007296341Sdelphij * calling it once is usually not enough, even if blocking I/O is used 1008296341Sdelphij * (see ssl3_shutdown). 1009296341Sdelphij */ 101055714Skris 1011296341Sdelphij if (s->handshake_func == 0) { 1012296341Sdelphij SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED); 1013296341Sdelphij return -1; 1014296341Sdelphij } 101555714Skris 1016296341Sdelphij if ((s != NULL) && !SSL_in_init(s)) 1017296341Sdelphij return (s->method->ssl_shutdown(s)); 1018296341Sdelphij else 1019296341Sdelphij return (1); 1020296341Sdelphij} 102155714Skris 102255714Skrisint SSL_renegotiate(SSL *s) 1023296341Sdelphij{ 1024296341Sdelphij if (s->renegotiate == 0) 1025296341Sdelphij s->renegotiate = 1; 1026238405Sjkim 1027296341Sdelphij s->new_session = 1; 1028238405Sjkim 1029296341Sdelphij return (s->method->ssl_renegotiate(s)); 1030296341Sdelphij} 103155714Skris 1032238405Sjkimint SSL_renegotiate_abbreviated(SSL *s) 1033296341Sdelphij{ 1034296341Sdelphij if (s->renegotiate == 0) 1035296341Sdelphij s->renegotiate = 1; 1036238405Sjkim 1037296341Sdelphij s->new_session = 0; 1038238405Sjkim 1039296341Sdelphij return (s->method->ssl_renegotiate(s)); 1040296341Sdelphij} 1041238405Sjkim 1042109998Smarkmint SSL_renegotiate_pending(SSL *s) 1043296341Sdelphij{ 1044296341Sdelphij /* 1045296341Sdelphij * becomes true when negotiation is requested; false again once a 1046296341Sdelphij * handshake has finished 1047296341Sdelphij */ 1048296341Sdelphij return (s->renegotiate != 0); 1049296341Sdelphij} 1050109998Smarkm 1051296341Sdelphijlong SSL_ctrl(SSL *s, int cmd, long larg, void *parg) 1052296341Sdelphij{ 1053296341Sdelphij long l; 105455714Skris 1055296341Sdelphij switch (cmd) { 1056296341Sdelphij case SSL_CTRL_GET_READ_AHEAD: 1057296341Sdelphij return (s->read_ahead); 1058296341Sdelphij case SSL_CTRL_SET_READ_AHEAD: 1059296341Sdelphij l = s->read_ahead; 1060296341Sdelphij s->read_ahead = larg; 1061296341Sdelphij return (l); 1062109998Smarkm 1063296341Sdelphij case SSL_CTRL_SET_MSG_CALLBACK_ARG: 1064296341Sdelphij s->msg_callback_arg = parg; 1065296341Sdelphij return 1; 1066109998Smarkm 1067296341Sdelphij case SSL_CTRL_OPTIONS: 1068296341Sdelphij return (s->options |= larg); 1069296341Sdelphij case SSL_CTRL_CLEAR_OPTIONS: 1070296341Sdelphij return (s->options &= ~larg); 1071296341Sdelphij case SSL_CTRL_MODE: 1072296341Sdelphij return (s->mode |= larg); 1073296341Sdelphij case SSL_CTRL_CLEAR_MODE: 1074296341Sdelphij return (s->mode &= ~larg); 1075296341Sdelphij case SSL_CTRL_GET_MAX_CERT_LIST: 1076296341Sdelphij return (s->max_cert_list); 1077296341Sdelphij case SSL_CTRL_SET_MAX_CERT_LIST: 1078296341Sdelphij l = s->max_cert_list; 1079296341Sdelphij s->max_cert_list = larg; 1080296341Sdelphij return (l); 1081296341Sdelphij case SSL_CTRL_SET_MAX_SEND_FRAGMENT: 1082296341Sdelphij if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) 1083296341Sdelphij return 0; 1084296341Sdelphij s->max_send_fragment = larg; 1085296341Sdelphij return 1; 1086296341Sdelphij case SSL_CTRL_GET_RI_SUPPORT: 1087296341Sdelphij if (s->s3) 1088296341Sdelphij return s->s3->send_connection_binding; 1089296341Sdelphij else 1090296341Sdelphij return 0; 1091296341Sdelphij default: 1092296341Sdelphij return (s->method->ssl_ctrl(s, cmd, larg, parg)); 1093296341Sdelphij } 1094296341Sdelphij} 109555714Skris 1096296341Sdelphijlong SSL_callback_ctrl(SSL *s, int cmd, void (*fp) (void)) 1097296341Sdelphij{ 1098296341Sdelphij switch (cmd) { 1099296341Sdelphij case SSL_CTRL_SET_MSG_CALLBACK: 1100296341Sdelphij s->msg_callback = (void (*) 1101296341Sdelphij (int write_p, int version, int content_type, 1102296341Sdelphij const void *buf, size_t len, SSL *ssl, 1103296341Sdelphij void *arg))(fp); 1104296341Sdelphij return 1; 110559191Skris 1106296341Sdelphij default: 1107296341Sdelphij return (s->method->ssl_callback_ctrl(s, cmd, fp)); 1108296341Sdelphij } 1109296341Sdelphij} 1110296341Sdelphij 1111238405SjkimLHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) 1112296341Sdelphij{ 1113296341Sdelphij return ctx->sessions; 1114296341Sdelphij} 111559191Skris 1116296341Sdelphijlong SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) 1117296341Sdelphij{ 1118296341Sdelphij long l; 111955714Skris 1120296341Sdelphij switch (cmd) { 1121296341Sdelphij case SSL_CTRL_GET_READ_AHEAD: 1122296341Sdelphij return (ctx->read_ahead); 1123296341Sdelphij case SSL_CTRL_SET_READ_AHEAD: 1124296341Sdelphij l = ctx->read_ahead; 1125296341Sdelphij ctx->read_ahead = larg; 1126296341Sdelphij return (l); 112755714Skris 1128296341Sdelphij case SSL_CTRL_SET_MSG_CALLBACK_ARG: 1129296341Sdelphij ctx->msg_callback_arg = parg; 1130296341Sdelphij return 1; 1131109998Smarkm 1132296341Sdelphij case SSL_CTRL_GET_MAX_CERT_LIST: 1133296341Sdelphij return (ctx->max_cert_list); 1134296341Sdelphij case SSL_CTRL_SET_MAX_CERT_LIST: 1135296341Sdelphij l = ctx->max_cert_list; 1136296341Sdelphij ctx->max_cert_list = larg; 1137296341Sdelphij return (l); 113855714Skris 1139296341Sdelphij case SSL_CTRL_SET_SESS_CACHE_SIZE: 1140296341Sdelphij l = ctx->session_cache_size; 1141296341Sdelphij ctx->session_cache_size = larg; 1142296341Sdelphij return (l); 1143296341Sdelphij case SSL_CTRL_GET_SESS_CACHE_SIZE: 1144296341Sdelphij return (ctx->session_cache_size); 1145296341Sdelphij case SSL_CTRL_SET_SESS_CACHE_MODE: 1146296341Sdelphij l = ctx->session_cache_mode; 1147296341Sdelphij ctx->session_cache_mode = larg; 1148296341Sdelphij return (l); 1149296341Sdelphij case SSL_CTRL_GET_SESS_CACHE_MODE: 1150296341Sdelphij return (ctx->session_cache_mode); 115155714Skris 1152296341Sdelphij case SSL_CTRL_SESS_NUMBER: 1153296341Sdelphij return (lh_SSL_SESSION_num_items(ctx->sessions)); 1154296341Sdelphij case SSL_CTRL_SESS_CONNECT: 1155296341Sdelphij return (ctx->stats.sess_connect); 1156296341Sdelphij case SSL_CTRL_SESS_CONNECT_GOOD: 1157296341Sdelphij return (ctx->stats.sess_connect_good); 1158296341Sdelphij case SSL_CTRL_SESS_CONNECT_RENEGOTIATE: 1159296341Sdelphij return (ctx->stats.sess_connect_renegotiate); 1160296341Sdelphij case SSL_CTRL_SESS_ACCEPT: 1161296341Sdelphij return (ctx->stats.sess_accept); 1162296341Sdelphij case SSL_CTRL_SESS_ACCEPT_GOOD: 1163296341Sdelphij return (ctx->stats.sess_accept_good); 1164296341Sdelphij case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE: 1165296341Sdelphij return (ctx->stats.sess_accept_renegotiate); 1166296341Sdelphij case SSL_CTRL_SESS_HIT: 1167296341Sdelphij return (ctx->stats.sess_hit); 1168296341Sdelphij case SSL_CTRL_SESS_CB_HIT: 1169296341Sdelphij return (ctx->stats.sess_cb_hit); 1170296341Sdelphij case SSL_CTRL_SESS_MISSES: 1171296341Sdelphij return (ctx->stats.sess_miss); 1172296341Sdelphij case SSL_CTRL_SESS_TIMEOUTS: 1173296341Sdelphij return (ctx->stats.sess_timeout); 1174296341Sdelphij case SSL_CTRL_SESS_CACHE_FULL: 1175296341Sdelphij return (ctx->stats.sess_cache_full); 1176296341Sdelphij case SSL_CTRL_OPTIONS: 1177296341Sdelphij return (ctx->options |= larg); 1178296341Sdelphij case SSL_CTRL_CLEAR_OPTIONS: 1179296341Sdelphij return (ctx->options &= ~larg); 1180296341Sdelphij case SSL_CTRL_MODE: 1181296341Sdelphij return (ctx->mode |= larg); 1182296341Sdelphij case SSL_CTRL_CLEAR_MODE: 1183296341Sdelphij return (ctx->mode &= ~larg); 1184296341Sdelphij case SSL_CTRL_SET_MAX_SEND_FRAGMENT: 1185296341Sdelphij if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) 1186296341Sdelphij return 0; 1187296341Sdelphij ctx->max_send_fragment = larg; 1188296341Sdelphij return 1; 1189296341Sdelphij default: 1190296341Sdelphij return (ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg)); 1191296341Sdelphij } 1192296341Sdelphij} 1193109998Smarkm 1194296341Sdelphijlong SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void)) 1195296341Sdelphij{ 1196296341Sdelphij switch (cmd) { 1197296341Sdelphij case SSL_CTRL_SET_MSG_CALLBACK: 1198296341Sdelphij ctx->msg_callback = (void (*) 1199296341Sdelphij (int write_p, int version, int content_type, 1200296341Sdelphij const void *buf, size_t len, SSL *ssl, 1201296341Sdelphij void *arg))(fp); 1202296341Sdelphij return 1; 120359191Skris 1204296341Sdelphij default: 1205296341Sdelphij return (ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp)); 1206296341Sdelphij } 1207296341Sdelphij} 1208296341Sdelphij 120968651Skrisint ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b) 1210296341Sdelphij{ 1211296341Sdelphij long l; 121255714Skris 1213296341Sdelphij l = a->id - b->id; 1214296341Sdelphij if (l == 0L) 1215296341Sdelphij return (0); 1216296341Sdelphij else 1217296341Sdelphij return ((l > 0) ? 1 : -1); 1218296341Sdelphij} 121955714Skris 1220296341Sdelphijint ssl_cipher_ptr_id_cmp(const SSL_CIPHER *const *ap, 1221296341Sdelphij const SSL_CIPHER *const *bp) 1222296341Sdelphij{ 1223296341Sdelphij long l; 122455714Skris 1225296341Sdelphij l = (*ap)->id - (*bp)->id; 1226296341Sdelphij if (l == 0L) 1227296341Sdelphij return (0); 1228296341Sdelphij else 1229296341Sdelphij return ((l > 0) ? 1 : -1); 1230296341Sdelphij} 123155714Skris 123255714Skris/** return a STACK of the ciphers available for the SSL and in order of 123355714Skris * preference */ 1234160814SsimonSTACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) 1235296341Sdelphij{ 1236296341Sdelphij if (s != NULL) { 1237296341Sdelphij if (s->cipher_list != NULL) { 1238296341Sdelphij return (s->cipher_list); 1239296341Sdelphij } else if ((s->ctx != NULL) && (s->ctx->cipher_list != NULL)) { 1240296341Sdelphij return (s->ctx->cipher_list); 1241296341Sdelphij } 1242296341Sdelphij } 1243296341Sdelphij return (NULL); 1244296341Sdelphij} 124555714Skris 124655714Skris/** return a STACK of the ciphers available for the SSL and in order of 124755714Skris * algorithm id */ 124855714SkrisSTACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) 1249296341Sdelphij{ 1250296341Sdelphij if (s != NULL) { 1251296341Sdelphij if (s->cipher_list_by_id != NULL) { 1252296341Sdelphij return (s->cipher_list_by_id); 1253296341Sdelphij } else if ((s->ctx != NULL) && (s->ctx->cipher_list_by_id != NULL)) { 1254296341Sdelphij return (s->ctx->cipher_list_by_id); 1255296341Sdelphij } 1256296341Sdelphij } 1257296341Sdelphij return (NULL); 1258296341Sdelphij} 125955714Skris 126055714Skris/** The old interface to get the same thing as SSL_get_ciphers() */ 1261296341Sdelphijconst char *SSL_get_cipher_list(const SSL *s, int n) 1262296341Sdelphij{ 1263296341Sdelphij SSL_CIPHER *c; 1264296341Sdelphij STACK_OF(SSL_CIPHER) *sk; 126555714Skris 1266296341Sdelphij if (s == NULL) 1267296341Sdelphij return (NULL); 1268296341Sdelphij sk = SSL_get_ciphers(s); 1269296341Sdelphij if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n)) 1270296341Sdelphij return (NULL); 1271296341Sdelphij c = sk_SSL_CIPHER_value(sk, n); 1272296341Sdelphij if (c == NULL) 1273296341Sdelphij return (NULL); 1274296341Sdelphij return (c->name); 1275296341Sdelphij} 127655714Skris 127759191Skris/** specify the ciphers to be used by default by the SSL_CTX */ 127859191Skrisint SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) 1279296341Sdelphij{ 1280296341Sdelphij STACK_OF(SSL_CIPHER) *sk; 128155714Skris 1282296341Sdelphij sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list, 1283296341Sdelphij &ctx->cipher_list_by_id, str); 1284296341Sdelphij /* 1285296341Sdelphij * ssl_create_cipher_list may return an empty stack if it was unable to 1286296341Sdelphij * find a cipher matching the given rule string (for example if the rule 1287296341Sdelphij * string specifies a cipher which has been disabled). This is not an 1288296341Sdelphij * error as far as ssl_create_cipher_list is concerned, and hence 1289296341Sdelphij * ctx->cipher_list and ctx->cipher_list_by_id has been updated. 1290296341Sdelphij */ 1291296341Sdelphij if (sk == NULL) 1292296341Sdelphij return 0; 1293296341Sdelphij else if (sk_SSL_CIPHER_num(sk) == 0) { 1294296341Sdelphij SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH); 1295296341Sdelphij return 0; 1296296341Sdelphij } 1297296341Sdelphij return 1; 1298296341Sdelphij} 1299296341Sdelphij 130055714Skris/** specify the ciphers to be used by the SSL */ 1301296341Sdelphijint SSL_set_cipher_list(SSL *s, const char *str) 1302296341Sdelphij{ 1303296341Sdelphij STACK_OF(SSL_CIPHER) *sk; 130455714Skris 1305296341Sdelphij sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list, 1306296341Sdelphij &s->cipher_list_by_id, str); 1307296341Sdelphij /* see comment in SSL_CTX_set_cipher_list */ 1308296341Sdelphij if (sk == NULL) 1309296341Sdelphij return 0; 1310296341Sdelphij else if (sk_SSL_CIPHER_num(sk) == 0) { 1311296341Sdelphij SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH); 1312296341Sdelphij return 0; 1313296341Sdelphij } 1314296341Sdelphij return 1; 1315296341Sdelphij} 1316296341Sdelphij 131755714Skris/* works well for SSLv2, not so good for SSLv3 */ 1318296341Sdelphijchar *SSL_get_shared_ciphers(const SSL *s, char *buf, int len) 1319296341Sdelphij{ 1320296341Sdelphij char *p; 1321296341Sdelphij STACK_OF(SSL_CIPHER) *sk; 1322296341Sdelphij SSL_CIPHER *c; 1323296341Sdelphij int i; 132455714Skris 1325296341Sdelphij if ((s->session == NULL) || (s->session->ciphers == NULL) || (len < 2)) 1326296341Sdelphij return (NULL); 132755714Skris 1328296341Sdelphij p = buf; 1329296341Sdelphij sk = s->session->ciphers; 1330267258Sjkim 1331296341Sdelphij if (sk_SSL_CIPHER_num(sk) == 0) 1332296341Sdelphij return NULL; 1333267258Sjkim 1334296341Sdelphij for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { 1335296341Sdelphij int n; 1336172429Ssimon 1337296341Sdelphij c = sk_SSL_CIPHER_value(sk, i); 1338296341Sdelphij n = strlen(c->name); 1339296341Sdelphij if (n + 1 > len) { 1340296341Sdelphij if (p != buf) 1341296341Sdelphij --p; 1342296341Sdelphij *p = '\0'; 1343296341Sdelphij return buf; 1344296341Sdelphij } 1345296341Sdelphij strcpy(p, c->name); 1346296341Sdelphij p += n; 1347296341Sdelphij *(p++) = ':'; 1348296341Sdelphij len -= n + 1; 1349296341Sdelphij } 1350296341Sdelphij p[-1] = '\0'; 1351296341Sdelphij return (buf); 1352296341Sdelphij} 135355714Skris 1354296341Sdelphijint ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, 1355296341Sdelphij unsigned char *p, 1356296341Sdelphij int (*put_cb) (const SSL_CIPHER *, 1357296341Sdelphij unsigned char *)) 1358296341Sdelphij{ 1359296341Sdelphij int i, j = 0; 1360296341Sdelphij SSL_CIPHER *c; 1361296341Sdelphij unsigned char *q; 1362109998Smarkm#ifndef OPENSSL_NO_KRB5 1363296341Sdelphij int nokrb5 = !kssl_tgt_is_available(s->kssl_ctx); 1364296341Sdelphij#endif /* OPENSSL_NO_KRB5 */ 136555714Skris 1366296341Sdelphij if (sk == NULL) 1367296341Sdelphij return (0); 1368296341Sdelphij q = p; 1369296341Sdelphij if (put_cb == NULL) 1370296341Sdelphij put_cb = s->method->put_cipher_by_char; 137155714Skris 1372296341Sdelphij for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { 1373296341Sdelphij c = sk_SSL_CIPHER_value(sk, i); 1374296341Sdelphij /* Skip TLS v1.2 only ciphersuites if lower than v1.2 */ 1375296341Sdelphij if ((c->algorithm_ssl & SSL_TLSV1_2) && 1376296341Sdelphij (TLS1_get_client_version(s) < TLS1_2_VERSION)) 1377296341Sdelphij continue; 1378109998Smarkm#ifndef OPENSSL_NO_KRB5 1379296341Sdelphij if (((c->algorithm_mkey & SSL_kKRB5) 1380296341Sdelphij || (c->algorithm_auth & SSL_aKRB5)) && nokrb5) 1381296341Sdelphij continue; 1382296341Sdelphij#endif /* OPENSSL_NO_KRB5 */ 1383238405Sjkim#ifndef OPENSSL_NO_PSK 1384296341Sdelphij /* with PSK there must be client callback set */ 1385296341Sdelphij if (((c->algorithm_mkey & SSL_kPSK) || (c->algorithm_auth & SSL_aPSK)) 1386296341Sdelphij && s->psk_client_callback == NULL) 1387296341Sdelphij continue; 1388296341Sdelphij#endif /* OPENSSL_NO_PSK */ 1389269686Sjkim#ifndef OPENSSL_NO_SRP 1390296341Sdelphij if (((c->algorithm_mkey & SSL_kSRP) || (c->algorithm_auth & SSL_aSRP)) 1391296341Sdelphij && !(s->srp_ctx.srp_Mask & SSL_kSRP)) 1392296341Sdelphij continue; 1393296341Sdelphij#endif /* OPENSSL_NO_SRP */ 1394296341Sdelphij j = put_cb(c, p); 1395296341Sdelphij p += j; 1396296341Sdelphij } 1397296341Sdelphij /* 1398296341Sdelphij * If p == q, no ciphers; caller indicates an error. Otherwise, add 1399296341Sdelphij * applicable SCSVs. 1400296341Sdelphij */ 1401296341Sdelphij if (p != q) { 1402296341Sdelphij if (!s->renegotiate) { 1403296341Sdelphij static SSL_CIPHER scsv = { 1404296341Sdelphij 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 1405296341Sdelphij }; 1406296341Sdelphij j = put_cb(&scsv, p); 1407296341Sdelphij p += j; 1408205128Ssimon#ifdef OPENSSL_RI_DEBUG 1409296341Sdelphij fprintf(stderr, 1410296341Sdelphij "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n"); 1411205128Ssimon#endif 1412296341Sdelphij } 1413205128Ssimon 1414296341Sdelphij if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) { 1415296341Sdelphij static SSL_CIPHER scsv = { 1416296341Sdelphij 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 1417296341Sdelphij }; 1418296341Sdelphij j = put_cb(&scsv, p); 1419296341Sdelphij p += j; 1420296341Sdelphij } 1421296341Sdelphij } 1422273399Sdelphij 1423296341Sdelphij return (p - q); 1424296341Sdelphij} 142555714Skris 1426296341SdelphijSTACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, unsigned char *p, 1427296341Sdelphij int num, 1428296341Sdelphij STACK_OF(SSL_CIPHER) **skp) 1429296341Sdelphij{ 1430296341Sdelphij const SSL_CIPHER *c; 1431296341Sdelphij STACK_OF(SSL_CIPHER) *sk; 1432296341Sdelphij int i, n; 1433273399Sdelphij 1434296341Sdelphij if (s->s3) 1435296341Sdelphij s->s3->send_connection_binding = 0; 143655714Skris 1437296341Sdelphij n = ssl_put_cipher_by_char(s, NULL, NULL); 1438296341Sdelphij if (n == 0 || (num % n) != 0) { 1439296341Sdelphij SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, 1440296341Sdelphij SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); 1441296341Sdelphij return (NULL); 1442296341Sdelphij } 1443296341Sdelphij if ((skp == NULL) || (*skp == NULL)) { 1444296341Sdelphij sk = sk_SSL_CIPHER_new_null(); /* change perhaps later */ 1445296341Sdelphij if(sk == NULL) { 1446296341Sdelphij SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE); 1447296341Sdelphij return NULL; 1448296341Sdelphij } 1449296341Sdelphij } else { 1450296341Sdelphij sk = *skp; 1451296341Sdelphij sk_SSL_CIPHER_zero(sk); 1452296341Sdelphij } 145355714Skris 1454296341Sdelphij for (i = 0; i < num; i += n) { 1455296341Sdelphij /* Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV */ 1456296341Sdelphij if (s->s3 && (n != 3 || !p[0]) && 1457296341Sdelphij (p[n - 2] == ((SSL3_CK_SCSV >> 8) & 0xff)) && 1458296341Sdelphij (p[n - 1] == (SSL3_CK_SCSV & 0xff))) { 1459296341Sdelphij /* SCSV fatal if renegotiating */ 1460296341Sdelphij if (s->renegotiate) { 1461296341Sdelphij SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, 1462296341Sdelphij SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING); 1463296341Sdelphij ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 1464296341Sdelphij goto err; 1465296341Sdelphij } 1466296341Sdelphij s->s3->send_connection_binding = 1; 1467296341Sdelphij p += n; 1468205128Ssimon#ifdef OPENSSL_RI_DEBUG 1469296341Sdelphij fprintf(stderr, "SCSV received by server\n"); 1470205128Ssimon#endif 1471296341Sdelphij continue; 1472296341Sdelphij } 1473205128Ssimon 1474296341Sdelphij /* Check for TLS_FALLBACK_SCSV */ 1475296341Sdelphij if ((n != 3 || !p[0]) && 1476296341Sdelphij (p[n - 2] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) && 1477296341Sdelphij (p[n - 1] == (SSL3_CK_FALLBACK_SCSV & 0xff))) { 1478296341Sdelphij /* 1479296341Sdelphij * The SCSV indicates that the client previously tried a higher 1480296341Sdelphij * version. Fail if the current version is an unexpected 1481296341Sdelphij * downgrade. 1482296341Sdelphij */ 1483296341Sdelphij if (!SSL_ctrl(s, SSL_CTRL_CHECK_PROTO_VERSION, 0, NULL)) { 1484296341Sdelphij SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, 1485296341Sdelphij SSL_R_INAPPROPRIATE_FALLBACK); 1486296341Sdelphij if (s->s3) 1487296341Sdelphij ssl3_send_alert(s, SSL3_AL_FATAL, 1488296341Sdelphij SSL_AD_INAPPROPRIATE_FALLBACK); 1489296341Sdelphij goto err; 1490296341Sdelphij } 1491296341Sdelphij p += n; 1492296341Sdelphij continue; 1493296341Sdelphij } 1494273399Sdelphij 1495296341Sdelphij c = ssl_get_cipher_by_char(s, p); 1496296341Sdelphij p += n; 1497296341Sdelphij if (c != NULL) { 1498296341Sdelphij if (!sk_SSL_CIPHER_push(sk, c)) { 1499296341Sdelphij SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE); 1500296341Sdelphij goto err; 1501296341Sdelphij } 1502296341Sdelphij } 1503296341Sdelphij } 150455714Skris 1505296341Sdelphij if (skp != NULL) 1506296341Sdelphij *skp = sk; 1507296341Sdelphij return (sk); 1508296341Sdelphij err: 1509296341Sdelphij if ((skp == NULL) || (*skp == NULL)) 1510296341Sdelphij sk_SSL_CIPHER_free(sk); 1511296341Sdelphij return (NULL); 1512296341Sdelphij} 151355714Skris 1514194206Ssimon#ifndef OPENSSL_NO_TLSEXT 1515194206Ssimon/** return a servername extension value if provided in Client Hello, or NULL. 1516194206Ssimon * So far, only host_name types are defined (RFC 3546). 1517194206Ssimon */ 1518194206Ssimon 1519194206Ssimonconst char *SSL_get_servername(const SSL *s, const int type) 1520296341Sdelphij{ 1521296341Sdelphij if (type != TLSEXT_NAMETYPE_host_name) 1522296341Sdelphij return NULL; 1523194206Ssimon 1524296341Sdelphij return s->session && !s->tlsext_hostname ? 1525296341Sdelphij s->session->tlsext_hostname : s->tlsext_hostname; 1526296341Sdelphij} 1527194206Ssimon 1528194206Ssimonint SSL_get_servername_type(const SSL *s) 1529296341Sdelphij{ 1530296341Sdelphij if (s->session 1531296341Sdelphij && (!s->tlsext_hostname ? s->session-> 1532296341Sdelphij tlsext_hostname : s->tlsext_hostname)) 1533296341Sdelphij return TLSEXT_NAMETYPE_host_name; 1534296341Sdelphij return -1; 1535296341Sdelphij} 1536238405Sjkim 1537238405Sjkim# ifndef OPENSSL_NO_NEXTPROTONEG 1538296341Sdelphij/* 1539296341Sdelphij * SSL_select_next_proto implements the standard protocol selection. It is 1540238405Sjkim * expected that this function is called from the callback set by 1541296341Sdelphij * SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a 1542296341Sdelphij * vector of 8-bit, length prefixed byte strings. The length byte itself is 1543296341Sdelphij * not included in the length. A byte string of length 0 is invalid. No byte 1544296341Sdelphij * string may be truncated. The current, but experimental algorithm for 1545296341Sdelphij * selecting the protocol is: 1) If the server doesn't support NPN then this 1546296341Sdelphij * is indicated to the callback. In this case, the client application has to 1547296341Sdelphij * abort the connection or have a default application level protocol. 2) If 1548296341Sdelphij * the server supports NPN, but advertises an empty list then the client 1549296341Sdelphij * selects the first protcol in its list, but indicates via the API that this 1550296341Sdelphij * fallback case was enacted. 3) Otherwise, the client finds the first 1551296341Sdelphij * protocol in the server's list that it supports and selects this protocol. 1552296341Sdelphij * This is because it's assumed that the server has better information about 1553296341Sdelphij * which protocol a client should use. 4) If the client doesn't support any 1554296341Sdelphij * of the server's advertised protocols, then this is treated the same as 1555296341Sdelphij * case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was 1556296341Sdelphij * found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached. 1557238405Sjkim */ 1558296341Sdelphijint SSL_select_next_proto(unsigned char **out, unsigned char *outlen, 1559296341Sdelphij const unsigned char *server, 1560296341Sdelphij unsigned int server_len, 1561296341Sdelphij const unsigned char *client, 1562296341Sdelphij unsigned int client_len) 1563296341Sdelphij{ 1564296341Sdelphij unsigned int i, j; 1565296341Sdelphij const unsigned char *result; 1566296341Sdelphij int status = OPENSSL_NPN_UNSUPPORTED; 1567238405Sjkim 1568296341Sdelphij /* 1569296341Sdelphij * For each protocol in server preference order, see if we support it. 1570296341Sdelphij */ 1571296341Sdelphij for (i = 0; i < server_len;) { 1572296341Sdelphij for (j = 0; j < client_len;) { 1573296341Sdelphij if (server[i] == client[j] && 1574296341Sdelphij memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) { 1575296341Sdelphij /* We found a match */ 1576296341Sdelphij result = &server[i]; 1577296341Sdelphij status = OPENSSL_NPN_NEGOTIATED; 1578296341Sdelphij goto found; 1579296341Sdelphij } 1580296341Sdelphij j += client[j]; 1581296341Sdelphij j++; 1582296341Sdelphij } 1583296341Sdelphij i += server[i]; 1584296341Sdelphij i++; 1585296341Sdelphij } 1586238405Sjkim 1587296341Sdelphij /* There's no overlap between our protocols and the server's list. */ 1588296341Sdelphij result = client; 1589296341Sdelphij status = OPENSSL_NPN_NO_OVERLAP; 1590238405Sjkim 1591296341Sdelphij found: 1592296341Sdelphij *out = (unsigned char *)result + 1; 1593296341Sdelphij *outlen = result[0]; 1594296341Sdelphij return status; 1595296341Sdelphij} 1596238405Sjkim 1597296341Sdelphij/* 1598296341Sdelphij * SSL_get0_next_proto_negotiated sets *data and *len to point to the 1599296341Sdelphij * client's requested protocol for this connection and returns 0. If the 1600296341Sdelphij * client didn't request any protocol, then *data is set to NULL. Note that 1601296341Sdelphij * the client can request any protocol it chooses. The value returned from 1602296341Sdelphij * this function need not be a member of the list of supported protocols 1603238405Sjkim * provided by the callback. 1604238405Sjkim */ 1605296341Sdelphijvoid SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data, 1606296341Sdelphij unsigned *len) 1607296341Sdelphij{ 1608296341Sdelphij *data = s->next_proto_negotiated; 1609296341Sdelphij if (!*data) { 1610296341Sdelphij *len = 0; 1611296341Sdelphij } else { 1612296341Sdelphij *len = s->next_proto_negotiated_len; 1613296341Sdelphij } 1614238405Sjkim} 1615238405Sjkim 1616296341Sdelphij/* 1617296341Sdelphij * SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when 1618296341Sdelphij * a TLS server needs a list of supported protocols for Next Protocol 1619296341Sdelphij * Negotiation. The returned list must be in wire format. The list is 1620296341Sdelphij * returned by setting |out| to point to it and |outlen| to its length. This 1621296341Sdelphij * memory will not be modified, but one should assume that the SSL* keeps a 1622296341Sdelphij * reference to it. The callback should return SSL_TLSEXT_ERR_OK if it 1623296341Sdelphij * wishes to advertise. Otherwise, no such extension will be included in the 1624296341Sdelphij * ServerHello. 1625296341Sdelphij */ 1626296341Sdelphijvoid SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *ctx, 1627296341Sdelphij int (*cb) (SSL *ssl, 1628296341Sdelphij const unsigned char 1629296341Sdelphij **out, 1630296341Sdelphij unsigned int *outlen, 1631296341Sdelphij void *arg), void *arg) 1632296341Sdelphij{ 1633296341Sdelphij ctx->next_protos_advertised_cb = cb; 1634296341Sdelphij ctx->next_protos_advertised_cb_arg = arg; 1635296341Sdelphij} 1636238405Sjkim 1637296341Sdelphij/* 1638296341Sdelphij * SSL_CTX_set_next_proto_select_cb sets a callback that is called when a 1639238405Sjkim * client needs to select a protocol from the server's provided list. |out| 1640238405Sjkim * must be set to point to the selected protocol (which may be within |in|). 1641296341Sdelphij * The length of the protocol name must be written into |outlen|. The 1642296341Sdelphij * server's advertised protocols are provided in |in| and |inlen|. The 1643296341Sdelphij * callback can assume that |in| is syntactically valid. The client must 1644296341Sdelphij * select a protocol. It is fatal to the connection if this callback returns 1645296341Sdelphij * a value other than SSL_TLSEXT_ERR_OK. 1646238405Sjkim */ 1647296341Sdelphijvoid SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx, 1648296341Sdelphij int (*cb) (SSL *s, unsigned char **out, 1649296341Sdelphij unsigned char *outlen, 1650296341Sdelphij const unsigned char *in, 1651296341Sdelphij unsigned int inlen, 1652296341Sdelphij void *arg), void *arg) 1653296341Sdelphij{ 1654296341Sdelphij ctx->next_proto_select_cb = cb; 1655296341Sdelphij ctx->next_proto_select_cb_arg = arg; 1656296341Sdelphij} 1657238405Sjkim# endif 1658194206Ssimon#endif 1659194206Ssimon 1660238405Sjkimint SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen, 1661296341Sdelphij const char *label, size_t llen, 1662296341Sdelphij const unsigned char *p, size_t plen, 1663296341Sdelphij int use_context) 1664296341Sdelphij{ 1665296341Sdelphij if (s->version < TLS1_VERSION) 1666296341Sdelphij return -1; 1667238405Sjkim 1668296341Sdelphij return s->method->ssl3_enc->export_keying_material(s, out, olen, label, 1669296341Sdelphij llen, p, plen, 1670296341Sdelphij use_context); 1671296341Sdelphij} 1672238405Sjkim 1673238405Sjkimstatic unsigned long ssl_session_hash(const SSL_SESSION *a) 1674296341Sdelphij{ 1675296341Sdelphij unsigned long l; 167655714Skris 1677296341Sdelphij l = (unsigned long) 1678296341Sdelphij ((unsigned int)a->session_id[0]) | 1679296341Sdelphij ((unsigned int)a->session_id[1] << 8L) | 1680296341Sdelphij ((unsigned long)a->session_id[2] << 16L) | 1681296341Sdelphij ((unsigned long)a->session_id[3] << 24L); 1682296341Sdelphij return (l); 1683296341Sdelphij} 168455714Skris 1685296341Sdelphij/* 1686296341Sdelphij * NB: If this function (or indeed the hash function which uses a sort of 1687109998Smarkm * coarser function than this one) is changed, ensure 1688296341Sdelphij * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on 1689296341Sdelphij * being able to construct an SSL_SESSION that will collide with any existing 1690296341Sdelphij * session with a matching session ID. 1691296341Sdelphij */ 1692296341Sdelphijstatic int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) 1693296341Sdelphij{ 1694296341Sdelphij if (a->ssl_version != b->ssl_version) 1695296341Sdelphij return (1); 1696296341Sdelphij if (a->session_id_length != b->session_id_length) 1697296341Sdelphij return (1); 1698296341Sdelphij return (memcmp(a->session_id, b->session_id, a->session_id_length)); 1699296341Sdelphij} 170055714Skris 1701296341Sdelphij/* 1702296341Sdelphij * These wrapper functions should remain rather than redeclaring 1703109998Smarkm * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each 1704296341Sdelphij * variable. The reason is that the functions aren't static, they're exposed 1705296341Sdelphij * via ssl.h. 1706296341Sdelphij */ 1707238405Sjkimstatic IMPLEMENT_LHASH_HASH_FN(ssl_session, SSL_SESSION) 1708238405Sjkimstatic IMPLEMENT_LHASH_COMP_FN(ssl_session, SSL_SESSION) 1709109998Smarkm 1710238405SjkimSSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) 1711296341Sdelphij{ 1712296341Sdelphij SSL_CTX *ret = NULL; 1713238405Sjkim 1714296341Sdelphij if (meth == NULL) { 1715296341Sdelphij SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_NULL_SSL_METHOD_PASSED); 1716296341Sdelphij return (NULL); 1717296341Sdelphij } 1718194206Ssimon#ifdef OPENSSL_FIPS 1719296341Sdelphij if (FIPS_mode() && (meth->version < TLS1_VERSION)) { 1720296341Sdelphij SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE); 1721296341Sdelphij return NULL; 1722296341Sdelphij } 1723194206Ssimon#endif 1724194206Ssimon 1725296341Sdelphij if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) { 1726296341Sdelphij SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS); 1727296341Sdelphij goto err; 1728296341Sdelphij } 1729296341Sdelphij ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX)); 1730296341Sdelphij if (ret == NULL) 1731296341Sdelphij goto err; 173255714Skris 1733296341Sdelphij memset(ret, 0, sizeof(SSL_CTX)); 173455714Skris 1735296341Sdelphij ret->method = meth; 173655714Skris 1737296341Sdelphij ret->cert_store = NULL; 1738296341Sdelphij ret->session_cache_mode = SSL_SESS_CACHE_SERVER; 1739296341Sdelphij ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; 1740296341Sdelphij ret->session_cache_head = NULL; 1741296341Sdelphij ret->session_cache_tail = NULL; 174255714Skris 1743296341Sdelphij /* We take the system default */ 1744296341Sdelphij ret->session_timeout = meth->get_timeout(); 174555714Skris 1746296341Sdelphij ret->new_session_cb = 0; 1747296341Sdelphij ret->remove_session_cb = 0; 1748296341Sdelphij ret->get_session_cb = 0; 1749296341Sdelphij ret->generate_session_id = 0; 175055714Skris 1751296341Sdelphij memset((char *)&ret->stats, 0, sizeof(ret->stats)); 175255714Skris 1753296341Sdelphij ret->references = 1; 1754296341Sdelphij ret->quiet_shutdown = 0; 175555714Skris 1756296341Sdelphij/* ret->cipher=NULL;*/ 1757296341Sdelphij/*- 1758296341Sdelphij ret->s2->challenge=NULL; 1759296341Sdelphij ret->master_key=NULL; 1760296341Sdelphij ret->key_arg=NULL; 1761296341Sdelphij ret->s2->conn_id=NULL; */ 176255714Skris 1763296341Sdelphij ret->info_callback = NULL; 176455714Skris 1765296341Sdelphij ret->app_verify_callback = 0; 1766296341Sdelphij ret->app_verify_arg = NULL; 176755714Skris 1768296341Sdelphij ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT; 1769296341Sdelphij ret->read_ahead = 0; 1770296341Sdelphij ret->msg_callback = 0; 1771296341Sdelphij ret->msg_callback_arg = NULL; 1772296341Sdelphij ret->verify_mode = SSL_VERIFY_NONE; 1773160814Ssimon#if 0 1774296341Sdelphij ret->verify_depth = -1; /* Don't impose a limit (but x509_lu.c does) */ 1775160814Ssimon#endif 1776296341Sdelphij ret->sid_ctx_length = 0; 1777296341Sdelphij ret->default_verify_callback = NULL; 1778296341Sdelphij if ((ret->cert = ssl_cert_new()) == NULL) 1779296341Sdelphij goto err; 178055714Skris 1781296341Sdelphij ret->default_passwd_callback = 0; 1782296341Sdelphij ret->default_passwd_callback_userdata = NULL; 1783296341Sdelphij ret->client_cert_cb = 0; 1784296341Sdelphij ret->app_gen_cookie_cb = 0; 1785296341Sdelphij ret->app_verify_cookie_cb = 0; 178655714Skris 1787296341Sdelphij ret->sessions = lh_SSL_SESSION_new(); 1788296341Sdelphij if (ret->sessions == NULL) 1789296341Sdelphij goto err; 1790296341Sdelphij ret->cert_store = X509_STORE_new(); 1791296341Sdelphij if (ret->cert_store == NULL) 1792296341Sdelphij goto err; 179355714Skris 1794296341Sdelphij ssl_create_cipher_list(ret->method, 1795296341Sdelphij &ret->cipher_list, &ret->cipher_list_by_id, 1796296341Sdelphij meth->version == 1797296341Sdelphij SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST); 1798296341Sdelphij if (ret->cipher_list == NULL || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) { 1799296341Sdelphij SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_LIBRARY_HAS_NO_CIPHERS); 1800296341Sdelphij goto err2; 1801296341Sdelphij } 180255714Skris 1803296341Sdelphij ret->param = X509_VERIFY_PARAM_new(); 1804296341Sdelphij if (!ret->param) 1805296341Sdelphij goto err; 1806160814Ssimon 1807296341Sdelphij if ((ret->rsa_md5 = EVP_get_digestbyname("ssl2-md5")) == NULL) { 1808296341Sdelphij SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES); 1809296341Sdelphij goto err2; 1810296341Sdelphij } 1811296341Sdelphij if ((ret->md5 = EVP_get_digestbyname("ssl3-md5")) == NULL) { 1812296341Sdelphij SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES); 1813296341Sdelphij goto err2; 1814296341Sdelphij } 1815296341Sdelphij if ((ret->sha1 = EVP_get_digestbyname("ssl3-sha1")) == NULL) { 1816296341Sdelphij SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES); 1817296341Sdelphij goto err2; 1818296341Sdelphij } 181955714Skris 1820296341Sdelphij if ((ret->client_CA = sk_X509_NAME_new_null()) == NULL) 1821296341Sdelphij goto err; 182255714Skris 1823296341Sdelphij CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data); 182455714Skris 1825296341Sdelphij ret->extra_certs = NULL; 1826296341Sdelphij /* No compression for DTLS */ 1827296341Sdelphij if (meth->version != DTLS1_VERSION) 1828296341Sdelphij ret->comp_methods = SSL_COMP_get_compression_methods(); 182955714Skris 1830296341Sdelphij ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; 1831238405Sjkim 1832194206Ssimon#ifndef OPENSSL_NO_TLSEXT 1833296341Sdelphij ret->tlsext_servername_callback = 0; 1834296341Sdelphij ret->tlsext_servername_arg = NULL; 1835296341Sdelphij /* Setup RFC4507 ticket keys */ 1836296341Sdelphij if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0) 1837296341Sdelphij || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0) 1838296341Sdelphij || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0)) 1839296341Sdelphij ret->options |= SSL_OP_NO_TICKET; 1840194206Ssimon 1841296341Sdelphij ret->tlsext_status_cb = 0; 1842296341Sdelphij ret->tlsext_status_arg = NULL; 1843194206Ssimon 1844238405Sjkim# ifndef OPENSSL_NO_NEXTPROTONEG 1845296341Sdelphij ret->next_protos_advertised_cb = 0; 1846296341Sdelphij ret->next_proto_select_cb = 0; 1847238405Sjkim# endif 1848194206Ssimon#endif 1849238405Sjkim#ifndef OPENSSL_NO_PSK 1850296341Sdelphij ret->psk_identity_hint = NULL; 1851296341Sdelphij ret->psk_client_callback = NULL; 1852296341Sdelphij ret->psk_server_callback = NULL; 1853238405Sjkim#endif 1854238405Sjkim#ifndef OPENSSL_NO_SRP 1855296341Sdelphij SSL_CTX_SRP_CTX_init(ret); 1856238405Sjkim#endif 1857238405Sjkim#ifndef OPENSSL_NO_BUF_FREELISTS 1858296341Sdelphij ret->freelist_max_len = SSL_MAX_BUF_FREELIST_LEN_DEFAULT; 1859296341Sdelphij ret->rbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST)); 1860296341Sdelphij if (!ret->rbuf_freelist) 1861296341Sdelphij goto err; 1862296341Sdelphij ret->rbuf_freelist->chunklen = 0; 1863296341Sdelphij ret->rbuf_freelist->len = 0; 1864296341Sdelphij ret->rbuf_freelist->head = NULL; 1865296341Sdelphij ret->wbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST)); 1866296341Sdelphij if (!ret->wbuf_freelist) { 1867296341Sdelphij OPENSSL_free(ret->rbuf_freelist); 1868296341Sdelphij goto err; 1869296341Sdelphij } 1870296341Sdelphij ret->wbuf_freelist->chunklen = 0; 1871296341Sdelphij ret->wbuf_freelist->len = 0; 1872296341Sdelphij ret->wbuf_freelist->head = NULL; 1873238405Sjkim#endif 1874194206Ssimon#ifndef OPENSSL_NO_ENGINE 1875296341Sdelphij ret->client_cert_engine = NULL; 1876296341Sdelphij# ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO 1877296341Sdelphij# define eng_strx(x) #x 1878296341Sdelphij# define eng_str(x) eng_strx(x) 1879296341Sdelphij /* Use specific client engine automatically... ignore errors */ 1880296341Sdelphij { 1881296341Sdelphij ENGINE *eng; 1882296341Sdelphij eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO)); 1883296341Sdelphij if (!eng) { 1884296341Sdelphij ERR_clear_error(); 1885296341Sdelphij ENGINE_load_builtin_engines(); 1886296341Sdelphij eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO)); 1887296341Sdelphij } 1888296341Sdelphij if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng)) 1889296341Sdelphij ERR_clear_error(); 1890296341Sdelphij } 1891296341Sdelphij# endif 1892194206Ssimon#endif 1893296341Sdelphij /* 1894296341Sdelphij * Default is to connect to non-RI servers. When RI is more widely 1895296341Sdelphij * deployed might change this. 1896296341Sdelphij */ 1897296341Sdelphij ret->options |= SSL_OP_LEGACY_SERVER_CONNECT; 1898194206Ssimon 1899296341Sdelphij /* 1900296341Sdelphij * Disable SSLv2 by default, callers that want to enable SSLv2 will have to 1901296341Sdelphij * explicitly clear this option via either of SSL_CTX_clear_options() or 1902296341Sdelphij * SSL_clear_options(). 1903296341Sdelphij */ 1904296341Sdelphij ret->options |= SSL_OP_NO_SSLv2; 190555714Skris 1906296341Sdelphij return (ret); 1907296341Sdelphij err: 1908296341Sdelphij SSLerr(SSL_F_SSL_CTX_NEW, ERR_R_MALLOC_FAILURE); 1909296341Sdelphij err2: 1910296341Sdelphij if (ret != NULL) 1911296341Sdelphij SSL_CTX_free(ret); 1912296341Sdelphij return (NULL); 1913296341Sdelphij} 1914296341Sdelphij 1915109998Smarkm#if 0 191655714Skrisstatic void SSL_COMP_free(SSL_COMP *comp) 1917296341Sdelphij{ 1918296341Sdelphij OPENSSL_free(comp); 1919296341Sdelphij} 1920109998Smarkm#endif 192155714Skris 1922238405Sjkim#ifndef OPENSSL_NO_BUF_FREELISTS 1923296341Sdelphijstatic void ssl_buf_freelist_free(SSL3_BUF_FREELIST *list) 1924296341Sdelphij{ 1925296341Sdelphij SSL3_BUF_FREELIST_ENTRY *ent, *next; 1926296341Sdelphij for (ent = list->head; ent; ent = next) { 1927296341Sdelphij next = ent->next; 1928296341Sdelphij OPENSSL_free(ent); 1929296341Sdelphij } 1930296341Sdelphij OPENSSL_free(list); 1931296341Sdelphij} 1932238405Sjkim#endif 1933238405Sjkim 193455714Skrisvoid SSL_CTX_free(SSL_CTX *a) 1935296341Sdelphij{ 1936296341Sdelphij int i; 193755714Skris 1938296341Sdelphij if (a == NULL) 1939296341Sdelphij return; 194055714Skris 1941296341Sdelphij i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX); 194255714Skris#ifdef REF_PRINT 1943296341Sdelphij REF_PRINT("SSL_CTX", a); 194455714Skris#endif 1945296341Sdelphij if (i > 0) 1946296341Sdelphij return; 194755714Skris#ifdef REF_CHECK 1948296341Sdelphij if (i < 0) { 1949296341Sdelphij fprintf(stderr, "SSL_CTX_free, bad reference count\n"); 1950296341Sdelphij abort(); /* ok */ 1951296341Sdelphij } 195255714Skris#endif 195355714Skris 1954296341Sdelphij if (a->param) 1955296341Sdelphij X509_VERIFY_PARAM_free(a->param); 1956160814Ssimon 1957296341Sdelphij /* 1958296341Sdelphij * Free internal session cache. However: the remove_cb() may reference 1959296341Sdelphij * the ex_data of SSL_CTX, thus the ex_data store can only be removed 1960296341Sdelphij * after the sessions were flushed. 1961296341Sdelphij * As the ex_data handling routines might also touch the session cache, 1962296341Sdelphij * the most secure solution seems to be: empty (flush) the cache, then 1963296341Sdelphij * free ex_data, then finally free the cache. 1964296341Sdelphij * (See ticket [openssl.org #212].) 1965296341Sdelphij */ 1966296341Sdelphij if (a->sessions != NULL) 1967296341Sdelphij SSL_CTX_flush_sessions(a, 0); 1968109998Smarkm 1969296341Sdelphij CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data); 1970109998Smarkm 1971296341Sdelphij if (a->sessions != NULL) 1972296341Sdelphij lh_SSL_SESSION_free(a->sessions); 1973109998Smarkm 1974296341Sdelphij if (a->cert_store != NULL) 1975296341Sdelphij X509_STORE_free(a->cert_store); 1976296341Sdelphij if (a->cipher_list != NULL) 1977296341Sdelphij sk_SSL_CIPHER_free(a->cipher_list); 1978296341Sdelphij if (a->cipher_list_by_id != NULL) 1979296341Sdelphij sk_SSL_CIPHER_free(a->cipher_list_by_id); 1980296341Sdelphij if (a->cert != NULL) 1981296341Sdelphij ssl_cert_free(a->cert); 1982296341Sdelphij if (a->client_CA != NULL) 1983296341Sdelphij sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free); 1984296341Sdelphij if (a->extra_certs != NULL) 1985296341Sdelphij sk_X509_pop_free(a->extra_certs, X509_free); 1986296341Sdelphij#if 0 /* This should never be done, since it 1987296341Sdelphij * removes a global database */ 1988296341Sdelphij if (a->comp_methods != NULL) 1989296341Sdelphij sk_SSL_COMP_pop_free(a->comp_methods, SSL_COMP_free); 1990109998Smarkm#else 1991296341Sdelphij a->comp_methods = NULL; 1992109998Smarkm#endif 1993238405Sjkim 1994246772Sjkim#ifndef OPENSSL_NO_SRTP 1995296341Sdelphij if (a->srtp_profiles) 1996296341Sdelphij sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles); 1997246772Sjkim#endif 1998238405Sjkim 1999238405Sjkim#ifndef OPENSSL_NO_PSK 2000296341Sdelphij if (a->psk_identity_hint) 2001296341Sdelphij OPENSSL_free(a->psk_identity_hint); 2002238405Sjkim#endif 2003238405Sjkim#ifndef OPENSSL_NO_SRP 2004296341Sdelphij SSL_CTX_SRP_CTX_free(a); 2005238405Sjkim#endif 2006194206Ssimon#ifndef OPENSSL_NO_ENGINE 2007296341Sdelphij if (a->client_cert_engine) 2008296341Sdelphij ENGINE_finish(a->client_cert_engine); 2009194206Ssimon#endif 2010238405Sjkim 2011238405Sjkim#ifndef OPENSSL_NO_BUF_FREELISTS 2012296341Sdelphij if (a->wbuf_freelist) 2013296341Sdelphij ssl_buf_freelist_free(a->wbuf_freelist); 2014296341Sdelphij if (a->rbuf_freelist) 2015296341Sdelphij ssl_buf_freelist_free(a->rbuf_freelist); 2016238405Sjkim#endif 2017238405Sjkim 2018296341Sdelphij OPENSSL_free(a); 2019296341Sdelphij} 202055714Skris 202155714Skrisvoid SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) 2022296341Sdelphij{ 2023296341Sdelphij ctx->default_passwd_callback = cb; 2024296341Sdelphij} 202555714Skris 2026296341Sdelphijvoid SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) 2027296341Sdelphij{ 2028296341Sdelphij ctx->default_passwd_callback_userdata = u; 2029296341Sdelphij} 203055714Skris 2031296341Sdelphijvoid SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, 2032296341Sdelphij int (*cb) (X509_STORE_CTX *, void *), 2033296341Sdelphij void *arg) 2034296341Sdelphij{ 2035296341Sdelphij ctx->app_verify_callback = cb; 2036296341Sdelphij ctx->app_verify_arg = arg; 2037296341Sdelphij} 203855714Skris 2039296341Sdelphijvoid SSL_CTX_set_verify(SSL_CTX *ctx, int mode, 2040296341Sdelphij int (*cb) (int, X509_STORE_CTX *)) 2041296341Sdelphij{ 2042296341Sdelphij ctx->verify_mode = mode; 2043296341Sdelphij ctx->default_verify_callback = cb; 2044296341Sdelphij} 204555714Skris 2046296341Sdelphijvoid SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) 2047296341Sdelphij{ 2048296341Sdelphij X509_VERIFY_PARAM_set_depth(ctx->param, depth); 2049296341Sdelphij} 205055714Skris 2051238405Sjkimvoid ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher) 2052296341Sdelphij{ 2053296341Sdelphij CERT_PKEY *cpk; 2054296341Sdelphij int rsa_enc, rsa_tmp, rsa_sign, dh_tmp, dh_rsa, dh_dsa, dsa_sign; 2055296341Sdelphij int rsa_enc_export, dh_rsa_export, dh_dsa_export; 2056296341Sdelphij int rsa_tmp_export, dh_tmp_export, kl; 2057296341Sdelphij unsigned long mask_k, mask_a, emask_k, emask_a; 2058296341Sdelphij#ifndef OPENSSL_NO_ECDSA 2059296341Sdelphij int have_ecc_cert, ecdsa_ok, ecc_pkey_size; 2060296341Sdelphij#endif 2061160814Ssimon#ifndef OPENSSL_NO_ECDH 2062296341Sdelphij int have_ecdh_tmp, ecdh_ok; 2063160814Ssimon#endif 2064296341Sdelphij#ifndef OPENSSL_NO_EC 2065296341Sdelphij X509 *x = NULL; 2066296341Sdelphij EVP_PKEY *ecc_pkey = NULL; 2067296341Sdelphij int signature_nid = 0, pk_nid = 0, md_nid = 0; 2068296341Sdelphij#endif 2069296341Sdelphij if (c == NULL) 2070296341Sdelphij return; 207155714Skris 2072296341Sdelphij kl = SSL_C_EXPORT_PKEYLENGTH(cipher); 207355714Skris 2074109998Smarkm#ifndef OPENSSL_NO_RSA 2075296341Sdelphij rsa_tmp = (c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL); 2076296341Sdelphij rsa_tmp_export = (c->rsa_tmp_cb != NULL || 2077296341Sdelphij (rsa_tmp && RSA_size(c->rsa_tmp) * 8 <= kl)); 207855714Skris#else 2079296341Sdelphij rsa_tmp = rsa_tmp_export = 0; 208055714Skris#endif 2081109998Smarkm#ifndef OPENSSL_NO_DH 2082296341Sdelphij dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL); 2083296341Sdelphij dh_tmp_export = (c->dh_tmp_cb != NULL || 2084296341Sdelphij (dh_tmp && DH_size(c->dh_tmp) * 8 <= kl)); 208555714Skris#else 2086296341Sdelphij dh_tmp = dh_tmp_export = 0; 208755714Skris#endif 208855714Skris 2089160814Ssimon#ifndef OPENSSL_NO_ECDH 2090296341Sdelphij have_ecdh_tmp = (c->ecdh_tmp != NULL || c->ecdh_tmp_cb != NULL); 2091160814Ssimon#endif 2092296341Sdelphij cpk = &(c->pkeys[SSL_PKEY_RSA_ENC]); 2093296341Sdelphij rsa_enc = (cpk->x509 != NULL && cpk->privatekey != NULL); 2094296341Sdelphij rsa_enc_export = (rsa_enc && EVP_PKEY_size(cpk->privatekey) * 8 <= kl); 2095296341Sdelphij cpk = &(c->pkeys[SSL_PKEY_RSA_SIGN]); 2096296341Sdelphij rsa_sign = (cpk->x509 != NULL && cpk->privatekey != NULL); 2097296341Sdelphij cpk = &(c->pkeys[SSL_PKEY_DSA_SIGN]); 2098296341Sdelphij dsa_sign = (cpk->x509 != NULL && cpk->privatekey != NULL); 2099296341Sdelphij cpk = &(c->pkeys[SSL_PKEY_DH_RSA]); 2100296341Sdelphij dh_rsa = (cpk->x509 != NULL && cpk->privatekey != NULL); 2101296341Sdelphij dh_rsa_export = (dh_rsa && EVP_PKEY_size(cpk->privatekey) * 8 <= kl); 2102296341Sdelphij cpk = &(c->pkeys[SSL_PKEY_DH_DSA]); 210355714Skris/* FIX THIS EAY EAY EAY */ 2104296341Sdelphij dh_dsa = (cpk->x509 != NULL && cpk->privatekey != NULL); 2105296341Sdelphij dh_dsa_export = (dh_dsa && EVP_PKEY_size(cpk->privatekey) * 8 <= kl); 2106296341Sdelphij cpk = &(c->pkeys[SSL_PKEY_ECC]); 2107296341Sdelphij#ifndef OPENSSL_NO_EC 2108296341Sdelphij have_ecc_cert = (cpk->x509 != NULL && cpk->privatekey != NULL); 2109296341Sdelphij#endif 2110296341Sdelphij mask_k = 0; 2111296341Sdelphij mask_a = 0; 2112296341Sdelphij emask_k = 0; 2113296341Sdelphij emask_a = 0; 211455714Skris 211555714Skris#ifdef CIPHER_DEBUG 2116296341Sdelphij fprintf(stderr, 2117296341Sdelphij "rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n", 2118296341Sdelphij rsa_tmp, rsa_tmp_export, dh_tmp, have_ecdh_tmp, rsa_enc, 2119296341Sdelphij rsa_enc_export, rsa_sign, dsa_sign, dh_rsa, dh_dsa); 212055714Skris#endif 212155714Skris 2122296341Sdelphij cpk = &(c->pkeys[SSL_PKEY_GOST01]); 2123296341Sdelphij if (cpk->x509 != NULL && cpk->privatekey != NULL) { 2124296341Sdelphij mask_k |= SSL_kGOST; 2125296341Sdelphij mask_a |= SSL_aGOST01; 2126296341Sdelphij } 2127296341Sdelphij cpk = &(c->pkeys[SSL_PKEY_GOST94]); 2128296341Sdelphij if (cpk->x509 != NULL && cpk->privatekey != NULL) { 2129296341Sdelphij mask_k |= SSL_kGOST; 2130296341Sdelphij mask_a |= SSL_aGOST94; 2131296341Sdelphij } 213255714Skris 2133296341Sdelphij if (rsa_enc || (rsa_tmp && rsa_sign)) 2134296341Sdelphij mask_k |= SSL_kRSA; 2135296341Sdelphij if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc))) 2136296341Sdelphij emask_k |= SSL_kRSA; 2137296341Sdelphij 213855714Skris#if 0 2139296341Sdelphij /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */ 2140296341Sdelphij if ((dh_tmp || dh_rsa || dh_dsa) && (rsa_enc || rsa_sign || dsa_sign)) 2141296341Sdelphij mask_k |= SSL_kEDH; 2142296341Sdelphij if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) && 2143296341Sdelphij (rsa_enc || rsa_sign || dsa_sign)) 2144296341Sdelphij emask_k |= SSL_kEDH; 214555714Skris#endif 214655714Skris 2147296341Sdelphij if (dh_tmp_export) 2148296341Sdelphij emask_k |= SSL_kEDH; 214955714Skris 2150296341Sdelphij if (dh_tmp) 2151296341Sdelphij mask_k |= SSL_kEDH; 215255714Skris 2153296341Sdelphij if (dh_rsa) 2154296341Sdelphij mask_k |= SSL_kDHr; 2155296341Sdelphij if (dh_rsa_export) 2156296341Sdelphij emask_k |= SSL_kDHr; 215755714Skris 2158296341Sdelphij if (dh_dsa) 2159296341Sdelphij mask_k |= SSL_kDHd; 2160296341Sdelphij if (dh_dsa_export) 2161296341Sdelphij emask_k |= SSL_kDHd; 216255714Skris 2163296341Sdelphij if (rsa_enc || rsa_sign) { 2164296341Sdelphij mask_a |= SSL_aRSA; 2165296341Sdelphij emask_a |= SSL_aRSA; 2166296341Sdelphij } 216755714Skris 2168296341Sdelphij if (dsa_sign) { 2169296341Sdelphij mask_a |= SSL_aDSS; 2170296341Sdelphij emask_a |= SSL_aDSS; 2171296341Sdelphij } 217255714Skris 2173296341Sdelphij mask_a |= SSL_aNULL; 2174296341Sdelphij emask_a |= SSL_aNULL; 217555714Skris 2176109998Smarkm#ifndef OPENSSL_NO_KRB5 2177296341Sdelphij mask_k |= SSL_kKRB5; 2178296341Sdelphij mask_a |= SSL_aKRB5; 2179296341Sdelphij emask_k |= SSL_kKRB5; 2180296341Sdelphij emask_a |= SSL_aKRB5; 2181109998Smarkm#endif 2182109998Smarkm 2183296341Sdelphij /* 2184296341Sdelphij * An ECC certificate may be usable for ECDH and/or ECDSA cipher suites 2185296341Sdelphij * depending on the key usage extension. 2186296341Sdelphij */ 2187296341Sdelphij#ifndef OPENSSL_NO_EC 2188296341Sdelphij if (have_ecc_cert) { 2189296341Sdelphij /* This call populates extension flags (ex_flags) */ 2190296341Sdelphij x = (c->pkeys[SSL_PKEY_ECC]).x509; 2191296341Sdelphij X509_check_purpose(x, -1, 0); 2192296341Sdelphij ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ? 2193296341Sdelphij (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1; 2194296341Sdelphij ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ? 2195296341Sdelphij (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1; 2196296341Sdelphij ecc_pkey = X509_get_pubkey(x); 2197296341Sdelphij ecc_pkey_size = (ecc_pkey != NULL) ? EVP_PKEY_bits(ecc_pkey) : 0; 2198296341Sdelphij EVP_PKEY_free(ecc_pkey); 2199296341Sdelphij if ((x->sig_alg) && (x->sig_alg->algorithm)) { 2200296341Sdelphij signature_nid = OBJ_obj2nid(x->sig_alg->algorithm); 2201296341Sdelphij OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid); 2202296341Sdelphij } 2203160814Ssimon#ifndef OPENSSL_NO_ECDH 2204296341Sdelphij if (ecdh_ok) { 2205238405Sjkim 2206296341Sdelphij if (pk_nid == NID_rsaEncryption || pk_nid == NID_rsa) { 2207296341Sdelphij mask_k |= SSL_kECDHr; 2208296341Sdelphij mask_a |= SSL_aECDH; 2209296341Sdelphij if (ecc_pkey_size <= 163) { 2210296341Sdelphij emask_k |= SSL_kECDHr; 2211296341Sdelphij emask_a |= SSL_aECDH; 2212296341Sdelphij } 2213296341Sdelphij } 2214238405Sjkim 2215296341Sdelphij if (pk_nid == NID_X9_62_id_ecPublicKey) { 2216296341Sdelphij mask_k |= SSL_kECDHe; 2217296341Sdelphij mask_a |= SSL_aECDH; 2218296341Sdelphij if (ecc_pkey_size <= 163) { 2219296341Sdelphij emask_k |= SSL_kECDHe; 2220296341Sdelphij emask_a |= SSL_aECDH; 2221296341Sdelphij } 2222296341Sdelphij } 2223296341Sdelphij } 2224160814Ssimon#endif 2225160814Ssimon#ifndef OPENSSL_NO_ECDSA 2226296341Sdelphij if (ecdsa_ok) { 2227296341Sdelphij mask_a |= SSL_aECDSA; 2228296341Sdelphij emask_a |= SSL_aECDSA; 2229296341Sdelphij } 2230160814Ssimon#endif 2231296341Sdelphij } 2232296341Sdelphij#endif 2233160814Ssimon#ifndef OPENSSL_NO_ECDH 2234296341Sdelphij if (have_ecdh_tmp) { 2235296341Sdelphij mask_k |= SSL_kEECDH; 2236296341Sdelphij emask_k |= SSL_kEECDH; 2237296341Sdelphij } 2238160814Ssimon#endif 2239238405Sjkim 2240238405Sjkim#ifndef OPENSSL_NO_PSK 2241296341Sdelphij mask_k |= SSL_kPSK; 2242296341Sdelphij mask_a |= SSL_aPSK; 2243296341Sdelphij emask_k |= SSL_kPSK; 2244296341Sdelphij emask_a |= SSL_aPSK; 2245238405Sjkim#endif 2246238405Sjkim 2247296341Sdelphij c->mask_k = mask_k; 2248296341Sdelphij c->mask_a = mask_a; 2249296341Sdelphij c->export_mask_k = emask_k; 2250296341Sdelphij c->export_mask_a = emask_a; 2251296341Sdelphij c->valid = 1; 2252296341Sdelphij} 225355714Skris 2254160814Ssimon/* This handy macro borrowed from crypto/x509v3/v3_purp.c */ 2255160814Ssimon#define ku_reject(x, usage) \ 2256296341Sdelphij (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage))) 2257160814Ssimon 2258238405Sjkim#ifndef OPENSSL_NO_EC 2259238405Sjkim 2260238405Sjkimint ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) 2261296341Sdelphij{ 2262296341Sdelphij unsigned long alg_k, alg_a; 2263296341Sdelphij EVP_PKEY *pkey = NULL; 2264296341Sdelphij int keysize = 0; 2265296341Sdelphij int signature_nid = 0, md_nid = 0, pk_nid = 0; 2266296341Sdelphij const SSL_CIPHER *cs = s->s3->tmp.new_cipher; 2267160814Ssimon 2268296341Sdelphij alg_k = cs->algorithm_mkey; 2269296341Sdelphij alg_a = cs->algorithm_auth; 2270238405Sjkim 2271296341Sdelphij if (SSL_C_IS_EXPORT(cs)) { 2272296341Sdelphij /* ECDH key length in export ciphers must be <= 163 bits */ 2273296341Sdelphij pkey = X509_get_pubkey(x); 2274296341Sdelphij if (pkey == NULL) 2275296341Sdelphij return 0; 2276296341Sdelphij keysize = EVP_PKEY_bits(pkey); 2277296341Sdelphij EVP_PKEY_free(pkey); 2278296341Sdelphij if (keysize > 163) 2279296341Sdelphij return 0; 2280296341Sdelphij } 2281160814Ssimon 2282296341Sdelphij /* This call populates the ex_flags field correctly */ 2283296341Sdelphij X509_check_purpose(x, -1, 0); 2284296341Sdelphij if ((x->sig_alg) && (x->sig_alg->algorithm)) { 2285296341Sdelphij signature_nid = OBJ_obj2nid(x->sig_alg->algorithm); 2286296341Sdelphij OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid); 2287296341Sdelphij } 2288296341Sdelphij if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr) { 2289296341Sdelphij /* key usage, if present, must allow key agreement */ 2290296341Sdelphij if (ku_reject(x, X509v3_KU_KEY_AGREEMENT)) { 2291296341Sdelphij SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, 2292296341Sdelphij SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT); 2293296341Sdelphij return 0; 2294296341Sdelphij } 2295296341Sdelphij if ((alg_k & SSL_kECDHe) && TLS1_get_version(s) < TLS1_2_VERSION) { 2296296341Sdelphij /* signature alg must be ECDSA */ 2297296341Sdelphij if (pk_nid != NID_X9_62_id_ecPublicKey) { 2298296341Sdelphij SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, 2299296341Sdelphij SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE); 2300296341Sdelphij return 0; 2301296341Sdelphij } 2302296341Sdelphij } 2303296341Sdelphij if ((alg_k & SSL_kECDHr) && TLS1_get_version(s) < TLS1_2_VERSION) { 2304296341Sdelphij /* signature alg must be RSA */ 2305238405Sjkim 2306296341Sdelphij if (pk_nid != NID_rsaEncryption && pk_nid != NID_rsa) { 2307296341Sdelphij SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, 2308296341Sdelphij SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE); 2309296341Sdelphij return 0; 2310296341Sdelphij } 2311296341Sdelphij } 2312296341Sdelphij } 2313296341Sdelphij if (alg_a & SSL_aECDSA) { 2314296341Sdelphij /* key usage, if present, must allow signing */ 2315296341Sdelphij if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) { 2316296341Sdelphij SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, 2317296341Sdelphij SSL_R_ECC_CERT_NOT_FOR_SIGNING); 2318296341Sdelphij return 0; 2319296341Sdelphij } 2320296341Sdelphij } 2321160814Ssimon 2322296341Sdelphij return 1; /* all checks are ok */ 2323296341Sdelphij} 2324160814Ssimon 2325238405Sjkim#endif 2326238405Sjkim 232755714Skris/* THIS NEEDS CLEANING UP */ 2328246772SjkimCERT_PKEY *ssl_get_server_send_pkey(const SSL *s) 2329296341Sdelphij{ 2330296341Sdelphij unsigned long alg_k, alg_a; 2331296341Sdelphij CERT *c; 2332296341Sdelphij int i; 233355714Skris 2334296341Sdelphij c = s->cert; 2335296341Sdelphij ssl_set_cert_masks(c, s->s3->tmp.new_cipher); 233655714Skris 2337296341Sdelphij alg_k = s->s3->tmp.new_cipher->algorithm_mkey; 2338296341Sdelphij alg_a = s->s3->tmp.new_cipher->algorithm_auth; 2339160814Ssimon 2340296341Sdelphij if (alg_k & (SSL_kECDHr | SSL_kECDHe)) { 2341296341Sdelphij /* 2342296341Sdelphij * we don't need to look at SSL_kEECDH since no certificate is needed 2343296341Sdelphij * for anon ECDH and for authenticated EECDH, the check for the auth 2344296341Sdelphij * algorithm will set i correctly NOTE: For ECDH-RSA, we need an ECC 2345296341Sdelphij * not an RSA cert but for EECDH-RSA we need an RSA cert. Placing the 2346296341Sdelphij * checks for SSL_kECDH before RSA checks ensures the correct cert is 2347296341Sdelphij * chosen. 2348296341Sdelphij */ 2349296341Sdelphij i = SSL_PKEY_ECC; 2350296341Sdelphij } else if (alg_a & SSL_aECDSA) { 2351296341Sdelphij i = SSL_PKEY_ECC; 2352296341Sdelphij } else if (alg_k & SSL_kDHr) 2353296341Sdelphij i = SSL_PKEY_DH_RSA; 2354296341Sdelphij else if (alg_k & SSL_kDHd) 2355296341Sdelphij i = SSL_PKEY_DH_DSA; 2356296341Sdelphij else if (alg_a & SSL_aDSS) 2357296341Sdelphij i = SSL_PKEY_DSA_SIGN; 2358296341Sdelphij else if (alg_a & SSL_aRSA) { 2359296341Sdelphij if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL) 2360296341Sdelphij i = SSL_PKEY_RSA_SIGN; 2361296341Sdelphij else 2362296341Sdelphij i = SSL_PKEY_RSA_ENC; 2363296341Sdelphij } else if (alg_a & SSL_aKRB5) { 2364296341Sdelphij /* VRS something else here? */ 2365296341Sdelphij return (NULL); 2366296341Sdelphij } else if (alg_a & SSL_aGOST94) 2367296341Sdelphij i = SSL_PKEY_GOST94; 2368296341Sdelphij else if (alg_a & SSL_aGOST01) 2369296341Sdelphij i = SSL_PKEY_GOST01; 2370296341Sdelphij else { /* if (alg_a & SSL_aNULL) */ 237155714Skris 2372296341Sdelphij SSLerr(SSL_F_SSL_GET_SERVER_SEND_PKEY, ERR_R_INTERNAL_ERROR); 2373296341Sdelphij return (NULL); 2374296341Sdelphij } 2375296341Sdelphij 2376296341Sdelphij return c->pkeys + i; 2377296341Sdelphij} 2378296341Sdelphij 2379246772SjkimX509 *ssl_get_server_send_cert(const SSL *s) 2380296341Sdelphij{ 2381296341Sdelphij CERT_PKEY *cpk; 2382296341Sdelphij cpk = ssl_get_server_send_pkey(s); 2383296341Sdelphij if (!cpk) 2384296341Sdelphij return NULL; 2385296341Sdelphij return cpk->x509; 2386296341Sdelphij} 2387246772Sjkim 2388296341SdelphijEVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher, 2389296341Sdelphij const EVP_MD **pmd) 2390296341Sdelphij{ 2391296341Sdelphij unsigned long alg_a; 2392296341Sdelphij CERT *c; 2393296341Sdelphij int idx = -1; 239455714Skris 2395296341Sdelphij alg_a = cipher->algorithm_auth; 2396296341Sdelphij c = s->cert; 239755714Skris 2398296341Sdelphij if ((alg_a & SSL_aDSS) && 2399296341Sdelphij (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL)) 2400296341Sdelphij idx = SSL_PKEY_DSA_SIGN; 2401296341Sdelphij else if (alg_a & SSL_aRSA) { 2402296341Sdelphij if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) 2403296341Sdelphij idx = SSL_PKEY_RSA_SIGN; 2404296341Sdelphij else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) 2405296341Sdelphij idx = SSL_PKEY_RSA_ENC; 2406296341Sdelphij } else if ((alg_a & SSL_aECDSA) && 2407296341Sdelphij (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) 2408296341Sdelphij idx = SSL_PKEY_ECC; 2409296341Sdelphij if (idx == -1) { 2410296341Sdelphij SSLerr(SSL_F_SSL_GET_SIGN_PKEY, ERR_R_INTERNAL_ERROR); 2411296341Sdelphij return (NULL); 2412296341Sdelphij } 2413296341Sdelphij if (pmd) 2414296341Sdelphij *pmd = c->pkeys[idx].digest; 2415296341Sdelphij return c->pkeys[idx].privatekey; 2416296341Sdelphij} 241755714Skris 2418296341Sdelphijvoid ssl_update_cache(SSL *s, int mode) 2419296341Sdelphij{ 2420296341Sdelphij int i; 242155714Skris 2422296341Sdelphij /* 2423296341Sdelphij * If the session_id_length is 0, we are not supposed to cache it, and it 2424296341Sdelphij * would be rather hard to do anyway :-) 2425296341Sdelphij */ 2426296341Sdelphij if (s->session->session_id_length == 0) 2427296341Sdelphij return; 242855714Skris 2429296341Sdelphij i = s->session_ctx->session_cache_mode; 2430296341Sdelphij if ((i & mode) && (!s->hit) 2431296341Sdelphij && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) 2432296341Sdelphij || SSL_CTX_add_session(s->session_ctx, s->session)) 2433296341Sdelphij && (s->session_ctx->new_session_cb != NULL)) { 2434296341Sdelphij CRYPTO_add(&s->session->references, 1, CRYPTO_LOCK_SSL_SESSION); 2435296341Sdelphij if (!s->session_ctx->new_session_cb(s, s->session)) 2436296341Sdelphij SSL_SESSION_free(s->session); 2437296341Sdelphij } 243855714Skris 2439296341Sdelphij /* auto flush every 255 connections */ 2440296341Sdelphij if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) { 2441296341Sdelphij if ((((mode & SSL_SESS_CACHE_CLIENT) 2442296341Sdelphij ? s->session_ctx->stats.sess_connect_good 2443296341Sdelphij : s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff) { 2444296341Sdelphij SSL_CTX_flush_sessions(s->session_ctx, (unsigned long)time(NULL)); 2445296341Sdelphij } 2446296341Sdelphij } 2447296341Sdelphij} 244855714Skris 2449238405Sjkimconst SSL_METHOD *SSL_get_ssl_method(SSL *s) 2450296341Sdelphij{ 2451296341Sdelphij return (s->method); 2452296341Sdelphij} 245355714Skris 2454238405Sjkimint SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth) 2455296341Sdelphij{ 2456296341Sdelphij int conn = -1; 2457296341Sdelphij int ret = 1; 245855714Skris 2459296341Sdelphij if (s->method != meth) { 2460296341Sdelphij if (s->handshake_func != NULL) 2461296341Sdelphij conn = (s->handshake_func == s->method->ssl_connect); 246255714Skris 2463296341Sdelphij if (s->method->version == meth->version) 2464296341Sdelphij s->method = meth; 2465296341Sdelphij else { 2466296341Sdelphij s->method->ssl_free(s); 2467296341Sdelphij s->method = meth; 2468296341Sdelphij ret = s->method->ssl_new(s); 2469296341Sdelphij } 247055714Skris 2471296341Sdelphij if (conn == 1) 2472296341Sdelphij s->handshake_func = meth->ssl_connect; 2473296341Sdelphij else if (conn == 0) 2474296341Sdelphij s->handshake_func = meth->ssl_accept; 2475296341Sdelphij } 2476296341Sdelphij return (ret); 2477296341Sdelphij} 247855714Skris 2479296341Sdelphijint SSL_get_error(const SSL *s, int i) 2480296341Sdelphij{ 2481296341Sdelphij int reason; 2482296341Sdelphij unsigned long l; 2483296341Sdelphij BIO *bio; 248455714Skris 2485296341Sdelphij if (i > 0) 2486296341Sdelphij return (SSL_ERROR_NONE); 248755714Skris 2488296341Sdelphij /* 2489296341Sdelphij * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc, 2490296341Sdelphij * where we do encode the error 2491296341Sdelphij */ 2492296341Sdelphij if ((l = ERR_peek_error()) != 0) { 2493296341Sdelphij if (ERR_GET_LIB(l) == ERR_LIB_SYS) 2494296341Sdelphij return (SSL_ERROR_SYSCALL); 2495296341Sdelphij else 2496296341Sdelphij return (SSL_ERROR_SSL); 2497296341Sdelphij } 249855714Skris 2499296341Sdelphij if ((i < 0) && SSL_want_read(s)) { 2500296341Sdelphij bio = SSL_get_rbio(s); 2501296341Sdelphij if (BIO_should_read(bio)) 2502296341Sdelphij return (SSL_ERROR_WANT_READ); 2503296341Sdelphij else if (BIO_should_write(bio)) 2504296341Sdelphij /* 2505296341Sdelphij * This one doesn't make too much sense ... We never try to write 2506296341Sdelphij * to the rbio, and an application program where rbio and wbio 2507296341Sdelphij * are separate couldn't even know what it should wait for. 2508296341Sdelphij * However if we ever set s->rwstate incorrectly (so that we have 2509296341Sdelphij * SSL_want_read(s) instead of SSL_want_write(s)) and rbio and 2510296341Sdelphij * wbio *are* the same, this test works around that bug; so it 2511296341Sdelphij * might be safer to keep it. 2512296341Sdelphij */ 2513296341Sdelphij return (SSL_ERROR_WANT_WRITE); 2514296341Sdelphij else if (BIO_should_io_special(bio)) { 2515296341Sdelphij reason = BIO_get_retry_reason(bio); 2516296341Sdelphij if (reason == BIO_RR_CONNECT) 2517296341Sdelphij return (SSL_ERROR_WANT_CONNECT); 2518296341Sdelphij else if (reason == BIO_RR_ACCEPT) 2519296341Sdelphij return (SSL_ERROR_WANT_ACCEPT); 2520296341Sdelphij else 2521296341Sdelphij return (SSL_ERROR_SYSCALL); /* unknown */ 2522296341Sdelphij } 2523296341Sdelphij } 252455714Skris 2525296341Sdelphij if ((i < 0) && SSL_want_write(s)) { 2526296341Sdelphij bio = SSL_get_wbio(s); 2527296341Sdelphij if (BIO_should_write(bio)) 2528296341Sdelphij return (SSL_ERROR_WANT_WRITE); 2529296341Sdelphij else if (BIO_should_read(bio)) 2530296341Sdelphij /* 2531296341Sdelphij * See above (SSL_want_read(s) with BIO_should_write(bio)) 2532296341Sdelphij */ 2533296341Sdelphij return (SSL_ERROR_WANT_READ); 2534296341Sdelphij else if (BIO_should_io_special(bio)) { 2535296341Sdelphij reason = BIO_get_retry_reason(bio); 2536296341Sdelphij if (reason == BIO_RR_CONNECT) 2537296341Sdelphij return (SSL_ERROR_WANT_CONNECT); 2538296341Sdelphij else if (reason == BIO_RR_ACCEPT) 2539296341Sdelphij return (SSL_ERROR_WANT_ACCEPT); 2540296341Sdelphij else 2541296341Sdelphij return (SSL_ERROR_SYSCALL); 2542296341Sdelphij } 2543296341Sdelphij } 2544296341Sdelphij if ((i < 0) && SSL_want_x509_lookup(s)) { 2545296341Sdelphij return (SSL_ERROR_WANT_X509_LOOKUP); 2546296341Sdelphij } 254755714Skris 2548296341Sdelphij if (i == 0) { 2549296341Sdelphij if (s->version == SSL2_VERSION) { 2550296341Sdelphij /* assume it is the socket being closed */ 2551296341Sdelphij return (SSL_ERROR_ZERO_RETURN); 2552296341Sdelphij } else { 2553296341Sdelphij if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) && 2554296341Sdelphij (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) 2555296341Sdelphij return (SSL_ERROR_ZERO_RETURN); 2556296341Sdelphij } 2557296341Sdelphij } 2558296341Sdelphij return (SSL_ERROR_SYSCALL); 2559296341Sdelphij} 256055714Skris 256155714Skrisint SSL_do_handshake(SSL *s) 2562296341Sdelphij{ 2563296341Sdelphij int ret = 1; 256455714Skris 2565296341Sdelphij if (s->handshake_func == NULL) { 2566296341Sdelphij SSLerr(SSL_F_SSL_DO_HANDSHAKE, SSL_R_CONNECTION_TYPE_NOT_SET); 2567296341Sdelphij return (-1); 2568296341Sdelphij } 256955714Skris 2570296341Sdelphij s->method->ssl_renegotiate_check(s); 257155714Skris 2572296341Sdelphij if (SSL_in_init(s) || SSL_in_before(s)) { 2573296341Sdelphij ret = s->handshake_func(s); 2574296341Sdelphij } 2575296341Sdelphij return (ret); 2576296341Sdelphij} 257755714Skris 2578296341Sdelphij/* 2579296341Sdelphij * For the next 2 functions, SSL_clear() sets shutdown and so one of these 2580296341Sdelphij * calls will reset it 2581296341Sdelphij */ 258255714Skrisvoid SSL_set_accept_state(SSL *s) 2583296341Sdelphij{ 2584296341Sdelphij s->server = 1; 2585296341Sdelphij s->shutdown = 0; 2586296341Sdelphij s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE; 2587296341Sdelphij s->handshake_func = s->method->ssl_accept; 2588296341Sdelphij /* clear the current cipher */ 2589296341Sdelphij ssl_clear_cipher_ctx(s); 2590296341Sdelphij ssl_clear_hash_ctx(&s->read_hash); 2591296341Sdelphij ssl_clear_hash_ctx(&s->write_hash); 2592296341Sdelphij} 259355714Skris 259455714Skrisvoid SSL_set_connect_state(SSL *s) 2595296341Sdelphij{ 2596296341Sdelphij s->server = 0; 2597296341Sdelphij s->shutdown = 0; 2598296341Sdelphij s->state = SSL_ST_CONNECT | SSL_ST_BEFORE; 2599296341Sdelphij s->handshake_func = s->method->ssl_connect; 2600296341Sdelphij /* clear the current cipher */ 2601296341Sdelphij ssl_clear_cipher_ctx(s); 2602296341Sdelphij ssl_clear_hash_ctx(&s->read_hash); 2603296341Sdelphij ssl_clear_hash_ctx(&s->write_hash); 2604296341Sdelphij} 260555714Skris 260655714Skrisint ssl_undefined_function(SSL *s) 2607296341Sdelphij{ 2608296341Sdelphij SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2609296341Sdelphij return (0); 2610296341Sdelphij} 261155714Skris 2612160814Ssimonint ssl_undefined_void_function(void) 2613296341Sdelphij{ 2614296341Sdelphij SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION, 2615296341Sdelphij ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2616296341Sdelphij return (0); 2617296341Sdelphij} 2618160814Ssimon 2619160814Ssimonint ssl_undefined_const_function(const SSL *s) 2620296341Sdelphij{ 2621296341Sdelphij SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION, 2622296341Sdelphij ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2623296341Sdelphij return (0); 2624296341Sdelphij} 2625160814Ssimon 262655714SkrisSSL_METHOD *ssl_bad_method(int ver) 2627296341Sdelphij{ 2628296341Sdelphij SSLerr(SSL_F_SSL_BAD_METHOD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 2629296341Sdelphij return (NULL); 2630296341Sdelphij} 263155714Skris 2632160814Ssimonconst char *SSL_get_version(const SSL *s) 2633296341Sdelphij{ 2634296341Sdelphij if (s->version == TLS1_2_VERSION) 2635296341Sdelphij return ("TLSv1.2"); 2636296341Sdelphij else if (s->version == TLS1_1_VERSION) 2637296341Sdelphij return ("TLSv1.1"); 2638296341Sdelphij else if (s->version == TLS1_VERSION) 2639296341Sdelphij return ("TLSv1"); 2640296341Sdelphij else if (s->version == SSL3_VERSION) 2641296341Sdelphij return ("SSLv3"); 2642296341Sdelphij else if (s->version == SSL2_VERSION) 2643296341Sdelphij return ("SSLv2"); 2644296341Sdelphij else 2645296341Sdelphij return ("unknown"); 2646296341Sdelphij} 264755714Skris 264855714SkrisSSL *SSL_dup(SSL *s) 2649296341Sdelphij{ 2650296341Sdelphij STACK_OF(X509_NAME) *sk; 2651296341Sdelphij X509_NAME *xn; 2652296341Sdelphij SSL *ret; 2653296341Sdelphij int i; 2654109998Smarkm 2655296341Sdelphij if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL) 2656296341Sdelphij return (NULL); 2657109998Smarkm 2658296341Sdelphij ret->version = s->version; 2659296341Sdelphij ret->type = s->type; 2660296341Sdelphij ret->method = s->method; 266155714Skris 2662296341Sdelphij if (s->session != NULL) { 2663296341Sdelphij /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */ 2664296341Sdelphij SSL_copy_session_id(ret, s); 2665296341Sdelphij } else { 2666296341Sdelphij /* 2667296341Sdelphij * No session has been established yet, so we have to expect that 2668296341Sdelphij * s->cert or ret->cert will be changed later -- they should not both 2669296341Sdelphij * point to the same object, and thus we can't use 2670296341Sdelphij * SSL_copy_session_id. 2671296341Sdelphij */ 267255714Skris 2673296341Sdelphij ret->method->ssl_free(ret); 2674296341Sdelphij ret->method = s->method; 2675296341Sdelphij ret->method->ssl_new(ret); 267655714Skris 2677296341Sdelphij if (s->cert != NULL) { 2678296341Sdelphij if (ret->cert != NULL) { 2679296341Sdelphij ssl_cert_free(ret->cert); 2680296341Sdelphij } 2681296341Sdelphij ret->cert = ssl_cert_dup(s->cert); 2682296341Sdelphij if (ret->cert == NULL) 2683296341Sdelphij goto err; 2684296341Sdelphij } 268555714Skris 2686296341Sdelphij SSL_set_session_id_context(ret, s->sid_ctx, s->sid_ctx_length); 2687296341Sdelphij } 268855714Skris 2689296341Sdelphij ret->options = s->options; 2690296341Sdelphij ret->mode = s->mode; 2691296341Sdelphij SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s)); 2692296341Sdelphij SSL_set_read_ahead(ret, SSL_get_read_ahead(s)); 2693296341Sdelphij ret->msg_callback = s->msg_callback; 2694296341Sdelphij ret->msg_callback_arg = s->msg_callback_arg; 2695296341Sdelphij SSL_set_verify(ret, SSL_get_verify_mode(s), SSL_get_verify_callback(s)); 2696296341Sdelphij SSL_set_verify_depth(ret, SSL_get_verify_depth(s)); 2697296341Sdelphij ret->generate_session_id = s->generate_session_id; 269855714Skris 2699296341Sdelphij SSL_set_info_callback(ret, SSL_get_info_callback(s)); 270055714Skris 2701296341Sdelphij ret->debug = s->debug; 2702160814Ssimon 2703296341Sdelphij /* copy app data, a little dangerous perhaps */ 2704296341Sdelphij if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data)) 2705296341Sdelphij goto err; 270655714Skris 2707296341Sdelphij /* setup rbio, and wbio */ 2708296341Sdelphij if (s->rbio != NULL) { 2709296341Sdelphij if (!BIO_dup_state(s->rbio, (char *)&ret->rbio)) 2710296341Sdelphij goto err; 2711296341Sdelphij } 2712296341Sdelphij if (s->wbio != NULL) { 2713296341Sdelphij if (s->wbio != s->rbio) { 2714296341Sdelphij if (!BIO_dup_state(s->wbio, (char *)&ret->wbio)) 2715296341Sdelphij goto err; 2716296341Sdelphij } else 2717296341Sdelphij ret->wbio = ret->rbio; 2718296341Sdelphij } 2719296341Sdelphij ret->rwstate = s->rwstate; 2720296341Sdelphij ret->in_handshake = s->in_handshake; 2721296341Sdelphij ret->handshake_func = s->handshake_func; 2722296341Sdelphij ret->server = s->server; 2723296341Sdelphij ret->renegotiate = s->renegotiate; 2724296341Sdelphij ret->new_session = s->new_session; 2725296341Sdelphij ret->quiet_shutdown = s->quiet_shutdown; 2726296341Sdelphij ret->shutdown = s->shutdown; 2727296341Sdelphij ret->state = s->state; /* SSL_dup does not really work at any state, 2728296341Sdelphij * though */ 2729296341Sdelphij ret->rstate = s->rstate; 2730296341Sdelphij ret->init_num = 0; /* would have to copy ret->init_buf, 2731296341Sdelphij * ret->init_msg, ret->init_num, 2732296341Sdelphij * ret->init_off */ 2733296341Sdelphij ret->hit = s->hit; 273455714Skris 2735296341Sdelphij X509_VERIFY_PARAM_inherit(ret->param, s->param); 273655714Skris 2737296341Sdelphij /* dup the cipher_list and cipher_list_by_id stacks */ 2738296341Sdelphij if (s->cipher_list != NULL) { 2739296341Sdelphij if ((ret->cipher_list = sk_SSL_CIPHER_dup(s->cipher_list)) == NULL) 2740296341Sdelphij goto err; 2741296341Sdelphij } 2742296341Sdelphij if (s->cipher_list_by_id != NULL) 2743296341Sdelphij if ((ret->cipher_list_by_id = sk_SSL_CIPHER_dup(s->cipher_list_by_id)) 2744296341Sdelphij == NULL) 2745296341Sdelphij goto err; 2746296341Sdelphij 2747296341Sdelphij /* Dup the client_CA list */ 2748296341Sdelphij if (s->client_CA != NULL) { 2749296341Sdelphij if ((sk = sk_X509_NAME_dup(s->client_CA)) == NULL) 2750296341Sdelphij goto err; 2751296341Sdelphij ret->client_CA = sk; 2752296341Sdelphij for (i = 0; i < sk_X509_NAME_num(sk); i++) { 2753296341Sdelphij xn = sk_X509_NAME_value(sk, i); 2754296341Sdelphij if (sk_X509_NAME_set(sk, i, X509_NAME_dup(xn)) == NULL) { 2755296341Sdelphij X509_NAME_free(xn); 2756296341Sdelphij goto err; 2757296341Sdelphij } 2758296341Sdelphij } 2759296341Sdelphij } 2760296341Sdelphij 2761296341Sdelphij if (0) { 2762296341Sdelphij err: 2763296341Sdelphij if (ret != NULL) 2764296341Sdelphij SSL_free(ret); 2765296341Sdelphij ret = NULL; 2766296341Sdelphij } 2767296341Sdelphij return (ret); 2768296341Sdelphij} 2769296341Sdelphij 277055714Skrisvoid ssl_clear_cipher_ctx(SSL *s) 2771296341Sdelphij{ 2772296341Sdelphij if (s->enc_read_ctx != NULL) { 2773296341Sdelphij EVP_CIPHER_CTX_cleanup(s->enc_read_ctx); 2774296341Sdelphij OPENSSL_free(s->enc_read_ctx); 2775296341Sdelphij s->enc_read_ctx = NULL; 2776296341Sdelphij } 2777296341Sdelphij if (s->enc_write_ctx != NULL) { 2778296341Sdelphij EVP_CIPHER_CTX_cleanup(s->enc_write_ctx); 2779296341Sdelphij OPENSSL_free(s->enc_write_ctx); 2780296341Sdelphij s->enc_write_ctx = NULL; 2781296341Sdelphij } 2782160814Ssimon#ifndef OPENSSL_NO_COMP 2783296341Sdelphij if (s->expand != NULL) { 2784296341Sdelphij COMP_CTX_free(s->expand); 2785296341Sdelphij s->expand = NULL; 2786296341Sdelphij } 2787296341Sdelphij if (s->compress != NULL) { 2788296341Sdelphij COMP_CTX_free(s->compress); 2789296341Sdelphij s->compress = NULL; 2790296341Sdelphij } 2791160814Ssimon#endif 2792296341Sdelphij} 279355714Skris 279455714Skris/* Fix this function so that it takes an optional type parameter */ 2795160814SsimonX509 *SSL_get_certificate(const SSL *s) 2796296341Sdelphij{ 2797296341Sdelphij if (s->cert != NULL) 2798296341Sdelphij return (s->cert->key->x509); 2799296341Sdelphij else 2800296341Sdelphij return (NULL); 2801296341Sdelphij} 280255714Skris 280355714Skris/* Fix this function so that it takes an optional type parameter */ 280455714SkrisEVP_PKEY *SSL_get_privatekey(SSL *s) 2805296341Sdelphij{ 2806296341Sdelphij if (s->cert != NULL) 2807296341Sdelphij return (s->cert->key->privatekey); 2808296341Sdelphij else 2809296341Sdelphij return (NULL); 2810296341Sdelphij} 281155714Skris 2812238405Sjkimconst SSL_CIPHER *SSL_get_current_cipher(const SSL *s) 2813296341Sdelphij{ 2814296341Sdelphij if ((s->session != NULL) && (s->session->cipher != NULL)) 2815296341Sdelphij return (s->session->cipher); 2816296341Sdelphij return (NULL); 2817296341Sdelphij} 2818296341Sdelphij 2819160814Ssimon#ifdef OPENSSL_NO_COMP 2820160814Ssimonconst void *SSL_get_current_compression(SSL *s) 2821296341Sdelphij{ 2822296341Sdelphij return NULL; 2823296341Sdelphij} 2824296341Sdelphij 2825160814Ssimonconst void *SSL_get_current_expansion(SSL *s) 2826296341Sdelphij{ 2827296341Sdelphij return NULL; 2828296341Sdelphij} 2829160814Ssimon#else 283055714Skris 2831160814Ssimonconst COMP_METHOD *SSL_get_current_compression(SSL *s) 2832296341Sdelphij{ 2833296341Sdelphij if (s->compress != NULL) 2834296341Sdelphij return (s->compress->meth); 2835296341Sdelphij return (NULL); 2836296341Sdelphij} 2837160814Ssimon 2838160814Ssimonconst COMP_METHOD *SSL_get_current_expansion(SSL *s) 2839296341Sdelphij{ 2840296341Sdelphij if (s->expand != NULL) 2841296341Sdelphij return (s->expand->meth); 2842296341Sdelphij return (NULL); 2843296341Sdelphij} 2844160814Ssimon#endif 2845160814Ssimon 2846296341Sdelphijint ssl_init_wbio_buffer(SSL *s, int push) 2847296341Sdelphij{ 2848296341Sdelphij BIO *bbio; 284955714Skris 2850296341Sdelphij if (s->bbio == NULL) { 2851296341Sdelphij bbio = BIO_new(BIO_f_buffer()); 2852296341Sdelphij if (bbio == NULL) 2853296341Sdelphij return (0); 2854296341Sdelphij s->bbio = bbio; 2855296341Sdelphij } else { 2856296341Sdelphij bbio = s->bbio; 2857296341Sdelphij if (s->bbio == s->wbio) 2858296341Sdelphij s->wbio = BIO_pop(s->wbio); 2859296341Sdelphij } 2860296341Sdelphij (void)BIO_reset(bbio); 2861296341Sdelphij/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */ 2862296341Sdelphij if (!BIO_set_read_buffer_size(bbio, 1)) { 2863296341Sdelphij SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER, ERR_R_BUF_LIB); 2864296341Sdelphij return (0); 2865296341Sdelphij } 2866296341Sdelphij if (push) { 2867296341Sdelphij if (s->wbio != bbio) 2868296341Sdelphij s->wbio = BIO_push(bbio, s->wbio); 2869296341Sdelphij } else { 2870296341Sdelphij if (s->wbio == bbio) 2871296341Sdelphij s->wbio = BIO_pop(bbio); 2872296341Sdelphij } 2873296341Sdelphij return (1); 2874296341Sdelphij} 287555714Skris 287655714Skrisvoid ssl_free_wbio_buffer(SSL *s) 2877296341Sdelphij{ 2878296341Sdelphij if (s->bbio == NULL) 2879296341Sdelphij return; 288055714Skris 2881296341Sdelphij if (s->bbio == s->wbio) { 2882296341Sdelphij /* remove buffering */ 2883296341Sdelphij s->wbio = BIO_pop(s->wbio); 2884296341Sdelphij#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids 2885296341Sdelphij * adding one more preprocessor symbol */ 2886296341Sdelphij assert(s->wbio != NULL); 2887238405Sjkim#endif 2888296341Sdelphij } 2889296341Sdelphij BIO_free(s->bbio); 2890296341Sdelphij s->bbio = NULL; 2891296341Sdelphij} 289255714Skris 2893296341Sdelphijvoid SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) 2894296341Sdelphij{ 2895296341Sdelphij ctx->quiet_shutdown = mode; 2896296341Sdelphij} 2897296341Sdelphij 2898160814Ssimonint SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) 2899296341Sdelphij{ 2900296341Sdelphij return (ctx->quiet_shutdown); 2901296341Sdelphij} 290255714Skris 2903296341Sdelphijvoid SSL_set_quiet_shutdown(SSL *s, int mode) 2904296341Sdelphij{ 2905296341Sdelphij s->quiet_shutdown = mode; 2906296341Sdelphij} 290755714Skris 2908160814Ssimonint SSL_get_quiet_shutdown(const SSL *s) 2909296341Sdelphij{ 2910296341Sdelphij return (s->quiet_shutdown); 2911296341Sdelphij} 291255714Skris 2913296341Sdelphijvoid SSL_set_shutdown(SSL *s, int mode) 2914296341Sdelphij{ 2915296341Sdelphij s->shutdown = mode; 2916296341Sdelphij} 291755714Skris 2918160814Ssimonint SSL_get_shutdown(const SSL *s) 2919296341Sdelphij{ 2920296341Sdelphij return (s->shutdown); 2921296341Sdelphij} 292255714Skris 2923160814Ssimonint SSL_version(const SSL *s) 2924296341Sdelphij{ 2925296341Sdelphij return (s->version); 2926296341Sdelphij} 292755714Skris 2928160814SsimonSSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) 2929296341Sdelphij{ 2930296341Sdelphij return (ssl->ctx); 2931296341Sdelphij} 293255714Skris 2933296341SdelphijSSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) 2934296341Sdelphij{ 2935296341Sdelphij CERT *ocert = ssl->cert; 2936296341Sdelphij if (ssl->ctx == ctx) 2937296341Sdelphij return ssl->ctx; 2938194206Ssimon#ifndef OPENSSL_NO_TLSEXT 2939296341Sdelphij if (ctx == NULL) 2940296341Sdelphij ctx = ssl->initial_ctx; 2941194206Ssimon#endif 2942296341Sdelphij ssl->cert = ssl_cert_dup(ctx->cert); 2943296341Sdelphij if (ocert != NULL) { 2944296341Sdelphij int i; 2945296341Sdelphij /* Copy negotiated digests from original */ 2946296341Sdelphij for (i = 0; i < SSL_PKEY_NUM; i++) { 2947296341Sdelphij CERT_PKEY *cpk = ocert->pkeys + i; 2948296341Sdelphij CERT_PKEY *rpk = ssl->cert->pkeys + i; 2949296341Sdelphij rpk->digest = cpk->digest; 2950296341Sdelphij } 2951296341Sdelphij ssl_cert_free(ocert); 2952296341Sdelphij } 2953277195Sdelphij 2954296341Sdelphij /* 2955296341Sdelphij * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH), 2956296341Sdelphij * so setter APIs must prevent invalid lengths from entering the system. 2957296341Sdelphij */ 2958296341Sdelphij OPENSSL_assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx)); 2959277195Sdelphij 2960296341Sdelphij /* 2961296341Sdelphij * If the session ID context matches that of the parent SSL_CTX, 2962296341Sdelphij * inherit it from the new SSL_CTX as well. If however the context does 2963296341Sdelphij * not match (i.e., it was set per-ssl with SSL_set_session_id_context), 2964296341Sdelphij * leave it unchanged. 2965296341Sdelphij */ 2966296341Sdelphij if ((ssl->ctx != NULL) && 2967296341Sdelphij (ssl->sid_ctx_length == ssl->ctx->sid_ctx_length) && 2968296341Sdelphij (memcmp(ssl->sid_ctx, ssl->ctx->sid_ctx, ssl->sid_ctx_length) == 0)) { 2969296341Sdelphij ssl->sid_ctx_length = ctx->sid_ctx_length; 2970296341Sdelphij memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx)); 2971296341Sdelphij } 2972277195Sdelphij 2973296341Sdelphij CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX); 2974296341Sdelphij if (ssl->ctx != NULL) 2975296341Sdelphij SSL_CTX_free(ssl->ctx); /* decrement reference count */ 2976296341Sdelphij ssl->ctx = ctx; 2977277195Sdelphij 2978296341Sdelphij return (ssl->ctx); 2979296341Sdelphij} 2980194206Ssimon 2981109998Smarkm#ifndef OPENSSL_NO_STDIO 298255714Skrisint SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) 2983296341Sdelphij{ 2984296341Sdelphij return (X509_STORE_set_default_paths(ctx->cert_store)); 2985296341Sdelphij} 298655714Skris 298755714Skrisint SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile, 2988296341Sdelphij const char *CApath) 2989296341Sdelphij{ 2990296341Sdelphij return (X509_STORE_load_locations(ctx->cert_store, CAfile, CApath)); 2991296341Sdelphij} 299255714Skris#endif 299355714Skris 2994109998Smarkmvoid SSL_set_info_callback(SSL *ssl, 2995296341Sdelphij void (*cb) (const SSL *ssl, int type, int val)) 2996296341Sdelphij{ 2997296341Sdelphij ssl->info_callback = cb; 2998296341Sdelphij} 299955714Skris 3000296341Sdelphij/* 3001296341Sdelphij * One compiler (Diab DCC) doesn't like argument names in returned function 3002296341Sdelphij * pointer. 3003296341Sdelphij */ 3004296341Sdelphijvoid (*SSL_get_info_callback(const SSL *ssl)) (const SSL * /* ssl */ , 3005296341Sdelphij int /* type */ , 3006296341Sdelphij int /* val */ ) { 3007296341Sdelphij return ssl->info_callback; 3008296341Sdelphij} 300955714Skris 3010160814Ssimonint SSL_state(const SSL *ssl) 3011296341Sdelphij{ 3012296341Sdelphij return (ssl->state); 3013296341Sdelphij} 301455714Skris 3015238405Sjkimvoid SSL_set_state(SSL *ssl, int state) 3016296341Sdelphij{ 3017296341Sdelphij ssl->state = state; 3018296341Sdelphij} 3019238405Sjkim 3020296341Sdelphijvoid SSL_set_verify_result(SSL *ssl, long arg) 3021296341Sdelphij{ 3022296341Sdelphij ssl->verify_result = arg; 3023296341Sdelphij} 302455714Skris 3025160814Ssimonlong SSL_get_verify_result(const SSL *ssl) 3026296341Sdelphij{ 3027296341Sdelphij return (ssl->verify_result); 3028296341Sdelphij} 302955714Skris 3030296341Sdelphijint SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 3031296341Sdelphij CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 3032296341Sdelphij{ 3033296341Sdelphij return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, 3034296341Sdelphij new_func, dup_func, free_func); 3035296341Sdelphij} 303655714Skris 3037296341Sdelphijint SSL_set_ex_data(SSL *s, int idx, void *arg) 3038296341Sdelphij{ 3039296341Sdelphij return (CRYPTO_set_ex_data(&s->ex_data, idx, arg)); 3040296341Sdelphij} 304155714Skris 3042296341Sdelphijvoid *SSL_get_ex_data(const SSL *s, int idx) 3043296341Sdelphij{ 3044296341Sdelphij return (CRYPTO_get_ex_data(&s->ex_data, idx)); 3045296341Sdelphij} 304655714Skris 3047296341Sdelphijint SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 3048296341Sdelphij CRYPTO_EX_dup *dup_func, 3049296341Sdelphij CRYPTO_EX_free *free_func) 3050296341Sdelphij{ 3051296341Sdelphij return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, 3052296341Sdelphij new_func, dup_func, free_func); 3053296341Sdelphij} 305455714Skris 3055296341Sdelphijint SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) 3056296341Sdelphij{ 3057296341Sdelphij return (CRYPTO_set_ex_data(&s->ex_data, idx, arg)); 3058296341Sdelphij} 305955714Skris 3060296341Sdelphijvoid *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) 3061296341Sdelphij{ 3062296341Sdelphij return (CRYPTO_get_ex_data(&s->ex_data, idx)); 3063296341Sdelphij} 306455714Skris 306555714Skrisint ssl_ok(SSL *s) 3066296341Sdelphij{ 3067296341Sdelphij return (1); 3068296341Sdelphij} 306955714Skris 3070160814SsimonX509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) 3071296341Sdelphij{ 3072296341Sdelphij return (ctx->cert_store); 3073296341Sdelphij} 307455714Skris 3075296341Sdelphijvoid SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) 3076296341Sdelphij{ 3077296341Sdelphij if (ctx->cert_store != NULL) 3078296341Sdelphij X509_STORE_free(ctx->cert_store); 3079296341Sdelphij ctx->cert_store = store; 3080296341Sdelphij} 308155714Skris 3082160814Ssimonint SSL_want(const SSL *s) 3083296341Sdelphij{ 3084296341Sdelphij return (s->rwstate); 3085296341Sdelphij} 308655714Skris 3087296341Sdelphij/** 308855714Skris * \brief Set the callback for generating temporary RSA keys. 308955714Skris * \param ctx the SSL context. 309055714Skris * \param cb the callback 309155714Skris */ 309255714Skris 3093109998Smarkm#ifndef OPENSSL_NO_RSA 3094296341Sdelphijvoid SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, RSA *(*cb) (SSL *ssl, 3095296341Sdelphij int is_export, 3096296341Sdelphij int keylength)) 3097296341Sdelphij{ 3098296341Sdelphij SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb); 3099296341Sdelphij} 310055714Skris 3101296341Sdelphijvoid SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb) (SSL *ssl, 3102296341Sdelphij int is_export, 3103296341Sdelphij int keylength)) 3104296341Sdelphij{ 3105296341Sdelphij SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb); 3106296341Sdelphij} 310755714Skris#endif 310855714Skris 310955714Skris#ifdef DOXYGEN 3110296341Sdelphij/** 311155714Skris * \brief The RSA temporary key callback function. 311255714Skris * \param ssl the SSL session. 311355714Skris * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite. 311455714Skris * \param keylength if \c is_export is \c TRUE, then \c keylength is the size 311555714Skris * of the required key in bits. 311655714Skris * \return the temporary RSA key. 311755714Skris * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback 311855714Skris */ 311955714Skris 3120296341SdelphijRSA *cb(SSL *ssl, int is_export, int keylength) 3121296341Sdelphij{ 3122296341Sdelphij} 312355714Skris#endif 312455714Skris 3125296341Sdelphij/** 312655714Skris * \brief Set the callback for generating temporary DH keys. 312755714Skris * \param ctx the SSL context. 312855714Skris * \param dh the callback 312955714Skris */ 313055714Skris 3131109998Smarkm#ifndef OPENSSL_NO_DH 3132296341Sdelphijvoid SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, 3133296341Sdelphij DH *(*dh) (SSL *ssl, int is_export, 3134296341Sdelphij int keylength)) 3135296341Sdelphij{ 3136296341Sdelphij SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh); 3137296341Sdelphij} 313855714Skris 3139296341Sdelphijvoid SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh) (SSL *ssl, int is_export, 3140296341Sdelphij int keylength)) 3141296341Sdelphij{ 3142296341Sdelphij SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh); 3143296341Sdelphij} 314455714Skris#endif 314555714Skris 3146160814Ssimon#ifndef OPENSSL_NO_ECDH 3147296341Sdelphijvoid SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx, 3148296341Sdelphij EC_KEY *(*ecdh) (SSL *ssl, int is_export, 3149296341Sdelphij int keylength)) 3150296341Sdelphij{ 3151296341Sdelphij SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, 3152296341Sdelphij (void (*)(void))ecdh); 3153296341Sdelphij} 3154109998Smarkm 3155296341Sdelphijvoid SSL_set_tmp_ecdh_callback(SSL *ssl, 3156296341Sdelphij EC_KEY *(*ecdh) (SSL *ssl, int is_export, 3157296341Sdelphij int keylength)) 3158296341Sdelphij{ 3159296341Sdelphij SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh); 3160296341Sdelphij} 3161160814Ssimon#endif 3162160814Ssimon 3163238405Sjkim#ifndef OPENSSL_NO_PSK 3164238405Sjkimint SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) 3165296341Sdelphij{ 3166296341Sdelphij if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) { 3167296341Sdelphij SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT, 3168296341Sdelphij SSL_R_DATA_LENGTH_TOO_LONG); 3169296341Sdelphij return 0; 3170296341Sdelphij } 3171296341Sdelphij if (ctx->psk_identity_hint != NULL) 3172296341Sdelphij OPENSSL_free(ctx->psk_identity_hint); 3173296341Sdelphij if (identity_hint != NULL) { 3174296341Sdelphij ctx->psk_identity_hint = BUF_strdup(identity_hint); 3175296341Sdelphij if (ctx->psk_identity_hint == NULL) 3176296341Sdelphij return 0; 3177296341Sdelphij } else 3178296341Sdelphij ctx->psk_identity_hint = NULL; 3179296341Sdelphij return 1; 3180296341Sdelphij} 3181160814Ssimon 3182238405Sjkimint SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) 3183296341Sdelphij{ 3184296341Sdelphij if (s == NULL) 3185296341Sdelphij return 0; 3186238405Sjkim 3187296341Sdelphij if (s->session == NULL) 3188296341Sdelphij return 1; /* session not created yet, ignored */ 3189238405Sjkim 3190296341Sdelphij if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) { 3191296341Sdelphij SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG); 3192296341Sdelphij return 0; 3193296341Sdelphij } 3194296341Sdelphij if (s->session->psk_identity_hint != NULL) 3195296341Sdelphij OPENSSL_free(s->session->psk_identity_hint); 3196296341Sdelphij if (identity_hint != NULL) { 3197296341Sdelphij s->session->psk_identity_hint = BUF_strdup(identity_hint); 3198296341Sdelphij if (s->session->psk_identity_hint == NULL) 3199296341Sdelphij return 0; 3200296341Sdelphij } else 3201296341Sdelphij s->session->psk_identity_hint = NULL; 3202296341Sdelphij return 1; 3203296341Sdelphij} 3204238405Sjkim 3205238405Sjkimconst char *SSL_get_psk_identity_hint(const SSL *s) 3206296341Sdelphij{ 3207296341Sdelphij if (s == NULL || s->session == NULL) 3208296341Sdelphij return NULL; 3209296341Sdelphij return (s->session->psk_identity_hint); 3210296341Sdelphij} 3211238405Sjkim 3212238405Sjkimconst char *SSL_get_psk_identity(const SSL *s) 3213296341Sdelphij{ 3214296341Sdelphij if (s == NULL || s->session == NULL) 3215296341Sdelphij return NULL; 3216296341Sdelphij return (s->session->psk_identity); 3217296341Sdelphij} 3218238405Sjkim 3219238405Sjkimvoid SSL_set_psk_client_callback(SSL *s, 3220296341Sdelphij unsigned int (*cb) (SSL *ssl, 3221296341Sdelphij const char *hint, 3222296341Sdelphij char *identity, 3223296341Sdelphij unsigned int 3224296341Sdelphij max_identity_len, 3225296341Sdelphij unsigned char *psk, 3226296341Sdelphij unsigned int 3227296341Sdelphij max_psk_len)) 3228296341Sdelphij{ 3229296341Sdelphij s->psk_client_callback = cb; 3230296341Sdelphij} 3231238405Sjkim 3232238405Sjkimvoid SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, 3233296341Sdelphij unsigned int (*cb) (SSL *ssl, 3234296341Sdelphij const char *hint, 3235296341Sdelphij char *identity, 3236296341Sdelphij unsigned int 3237296341Sdelphij max_identity_len, 3238296341Sdelphij unsigned char *psk, 3239296341Sdelphij unsigned int 3240296341Sdelphij max_psk_len)) 3241296341Sdelphij{ 3242296341Sdelphij ctx->psk_client_callback = cb; 3243296341Sdelphij} 3244238405Sjkim 3245238405Sjkimvoid SSL_set_psk_server_callback(SSL *s, 3246296341Sdelphij unsigned int (*cb) (SSL *ssl, 3247296341Sdelphij const char *identity, 3248296341Sdelphij unsigned char *psk, 3249296341Sdelphij unsigned int 3250296341Sdelphij max_psk_len)) 3251296341Sdelphij{ 3252296341Sdelphij s->psk_server_callback = cb; 3253296341Sdelphij} 3254238405Sjkim 3255238405Sjkimvoid SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, 3256296341Sdelphij unsigned int (*cb) (SSL *ssl, 3257296341Sdelphij const char *identity, 3258296341Sdelphij unsigned char *psk, 3259296341Sdelphij unsigned int 3260296341Sdelphij max_psk_len)) 3261296341Sdelphij{ 3262296341Sdelphij ctx->psk_server_callback = cb; 3263296341Sdelphij} 3264238405Sjkim#endif 3265238405Sjkim 3266296341Sdelphijvoid SSL_CTX_set_msg_callback(SSL_CTX *ctx, 3267296341Sdelphij void (*cb) (int write_p, int version, 3268296341Sdelphij int content_type, const void *buf, 3269296341Sdelphij size_t len, SSL *ssl, void *arg)) 3270296341Sdelphij{ 3271296341Sdelphij SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); 3272296341Sdelphij} 3273109998Smarkm 3274296341Sdelphijvoid SSL_set_msg_callback(SSL *ssl, 3275296341Sdelphij void (*cb) (int write_p, int version, 3276296341Sdelphij int content_type, const void *buf, 3277296341Sdelphij size_t len, SSL *ssl, void *arg)) 3278296341Sdelphij{ 3279296341Sdelphij SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); 3280296341Sdelphij} 3281296341Sdelphij 3282296341Sdelphij/* 3283296341Sdelphij * Allocates new EVP_MD_CTX and sets pointer to it into given pointer 3284296341Sdelphij * vairable, freeing EVP_MD_CTX previously stored in that variable, if any. 3285296341Sdelphij * If EVP_MD pointer is passed, initializes ctx with this md Returns newly 3286296341Sdelphij * allocated ctx; 3287238405Sjkim */ 3288109998Smarkm 3289296341SdelphijEVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md) 3290238405Sjkim{ 3291296341Sdelphij ssl_clear_hash_ctx(hash); 3292296341Sdelphij *hash = EVP_MD_CTX_create(); 3293296341Sdelphij if (md) 3294296341Sdelphij EVP_DigestInit_ex(*hash, md, NULL); 3295296341Sdelphij return *hash; 3296238405Sjkim} 3297296341Sdelphij 3298296341Sdelphijvoid ssl_clear_hash_ctx(EVP_MD_CTX **hash) 3299238405Sjkim{ 3300109998Smarkm 3301296341Sdelphij if (*hash) 3302296341Sdelphij EVP_MD_CTX_destroy(*hash); 3303296341Sdelphij *hash = NULL; 3304238405Sjkim} 3305238405Sjkim 3306238405Sjkimvoid SSL_set_debug(SSL *s, int debug) 3307296341Sdelphij{ 3308296341Sdelphij s->debug = debug; 3309296341Sdelphij} 3310238405Sjkim 3311238405Sjkimint SSL_cache_hit(SSL *s) 3312296341Sdelphij{ 3313296341Sdelphij return s->hit; 3314296341Sdelphij} 3315238405Sjkim 3316109998Smarkm#if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16) 3317296341Sdelphij# include "../crypto/bio/bss_file.c" 331855714Skris#endif 331955714Skris 332055714SkrisIMPLEMENT_STACK_OF(SSL_CIPHER) 332155714SkrisIMPLEMENT_STACK_OF(SSL_COMP) 3322296341SdelphijIMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id); 3323