sl@0: /*! \file ssl/ssl_lib.c sl@0: * \brief Version independent SSL functions. sl@0: */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. sl@0: * ECC cipher suite support in OpenSSL originally developed by sl@0: * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: #ifdef REF_CHECK sl@0: # include sl@0: #endif sl@0: #include sl@0: #include "ssl_locl.h" sl@0: #include "kssl_lcl.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #ifndef OPENSSL_NO_DH sl@0: #include sl@0: #endif sl@0: sl@0: #ifdef EMULATOR sl@0: #include "libssl_wsd.h" sl@0: #endif sl@0: sl@0: const char *SSL_version_str=OPENSSL_VERSION_TEXT; sl@0: sl@0: #ifdef EMULATOR sl@0: GET_GLOBAL_VAR_FROM_TLS(ssl3_undef_enc_method,ssl_lib,SSL3_ENC_METHOD) sl@0: sl@0: #define ssl3_undef_enc_method (GET_WSD_VAR_NAME(ssl3_undef_enc_method,ssl_lib,g)()) sl@0: #endif sl@0: sl@0: #ifndef EMULATOR sl@0: SSL3_ENC_METHOD ssl3_undef_enc_method={ sl@0: #else sl@0: const SSL3_ENC_METHOD temp_ssl3_undef_enc_method={ sl@0: #endif sl@0: /* evil casts, but these functions are only called if there's a library bug */ sl@0: (int (*)(SSL *,int))ssl_undefined_function, sl@0: (int (*)(SSL *, unsigned char *, int))ssl_undefined_function, sl@0: ssl_undefined_function, sl@0: (int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function, sl@0: (int (*)(SSL*, int))ssl_undefined_function, sl@0: (int (*)(SSL *, EVP_MD_CTX *, EVP_MD_CTX *, const char*, int, unsigned char *))ssl_undefined_function, sl@0: 0, /* finish_mac_length */ sl@0: (int (*)(SSL *, EVP_MD_CTX *, unsigned char *))ssl_undefined_function, sl@0: NULL, /* client_finished_label */ sl@0: 0, /* client_finished_label_len */ sl@0: NULL, /* server_finished_label */ sl@0: 0, /* server_finished_label_len */ sl@0: (int (*)(int))ssl_undefined_function sl@0: }; sl@0: sl@0: EXPORT_C int SSL_clear(SSL *s) sl@0: { sl@0: sl@0: if (s->method == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED); sl@0: return(0); sl@0: } sl@0: sl@0: if (ssl_clear_bad_session(s)) sl@0: { sl@0: SSL_SESSION_free(s->session); sl@0: s->session=NULL; sl@0: } sl@0: sl@0: s->error=0; sl@0: s->hit=0; sl@0: s->shutdown=0; sl@0: sl@0: #if 0 /* Disabled since version 1.10 of this file (early return not sl@0: * needed because SSL_clear is not called when doing renegotiation) */ sl@0: /* This is set if we are doing dynamic renegotiation so keep sl@0: * the old cipher. It is sort of a SSL_clear_lite :-) */ sl@0: if (s->new_session) return(1); sl@0: #else sl@0: if (s->new_session) sl@0: { sl@0: SSLerr(SSL_F_SSL_CLEAR,ERR_R_INTERNAL_ERROR); sl@0: return 0; sl@0: } sl@0: #endif sl@0: sl@0: s->type=0; sl@0: sl@0: s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT); sl@0: sl@0: s->version=s->method->version; sl@0: s->client_version=s->version; sl@0: s->rwstate=SSL_NOTHING; sl@0: s->rstate=SSL_ST_READ_HEADER; sl@0: #if 0 sl@0: s->read_ahead=s->ctx->read_ahead; sl@0: #endif sl@0: sl@0: if (s->init_buf != NULL) sl@0: { sl@0: BUF_MEM_free(s->init_buf); sl@0: s->init_buf=NULL; sl@0: } sl@0: sl@0: ssl_clear_cipher_ctx(s); sl@0: sl@0: s->first_packet=0; sl@0: sl@0: #if 1 sl@0: /* Check to see if we were changed into a different method, if sl@0: * so, revert back if we are not doing session-id reuse. */ sl@0: if (!s->in_handshake && (s->session == NULL) && (s->method != s->ctx->method)) sl@0: { sl@0: s->method->ssl_free(s); sl@0: s->method=s->ctx->method; sl@0: if (!s->method->ssl_new(s)) sl@0: return(0); sl@0: } sl@0: else sl@0: #endif sl@0: s->method->ssl_clear(s); sl@0: return(1); sl@0: } sl@0: sl@0: /** Used to change an SSL_CTXs default SSL method type */ sl@0: EXPORT_C int SSL_CTX_set_ssl_version(SSL_CTX *ctx,SSL_METHOD *meth) sl@0: { sl@0: STACK_OF(SSL_CIPHER) *sk; sl@0: sl@0: ctx->method=meth; sl@0: sl@0: sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list), sl@0: &(ctx->cipher_list_by_id),SSL_DEFAULT_CIPHER_LIST); sl@0: if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) sl@0: { sl@0: SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS); sl@0: return(0); sl@0: } sl@0: return(1); sl@0: } sl@0: sl@0: EXPORT_C SSL *SSL_new(SSL_CTX *ctx) sl@0: { sl@0: SSL *s; sl@0: sl@0: if (ctx == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX); sl@0: return(NULL); sl@0: } sl@0: if (ctx->method == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION); sl@0: return(NULL); sl@0: } sl@0: sl@0: s=(SSL *)OPENSSL_malloc(sizeof(SSL)); sl@0: if (s == NULL) goto err; sl@0: memset(s,0,sizeof(SSL)); sl@0: sl@0: #ifndef OPENSSL_NO_KRB5 sl@0: s->kssl_ctx = kssl_ctx_new(); sl@0: #endif /* OPENSSL_NO_KRB5 */ sl@0: sl@0: s->options=ctx->options; sl@0: s->mode=ctx->mode; sl@0: s->max_cert_list=ctx->max_cert_list; sl@0: sl@0: if (ctx->cert != NULL) sl@0: { sl@0: /* Earlier library versions used to copy the pointer to sl@0: * the CERT, not its contents; only when setting new sl@0: * parameters for the per-SSL copy, ssl_cert_new would be sl@0: * called (and the direct reference to the per-SSL_CTX sl@0: * settings would be lost, but those still were indirectly sl@0: * accessed for various purposes, and for that reason they sl@0: * used to be known as s->ctx->default_cert). sl@0: * Now we don't look at the SSL_CTX's CERT after having sl@0: * duplicated it once. */ sl@0: sl@0: s->cert = ssl_cert_dup(ctx->cert); sl@0: if (s->cert == NULL) sl@0: goto err; sl@0: } sl@0: else sl@0: s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */ sl@0: sl@0: s->read_ahead=ctx->read_ahead; sl@0: s->msg_callback=ctx->msg_callback; sl@0: s->msg_callback_arg=ctx->msg_callback_arg; sl@0: s->verify_mode=ctx->verify_mode; sl@0: #if 0 sl@0: s->verify_depth=ctx->verify_depth; sl@0: #endif sl@0: s->sid_ctx_length=ctx->sid_ctx_length; sl@0: OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx); sl@0: memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx)); sl@0: s->verify_callback=ctx->default_verify_callback; sl@0: s->generate_session_id=ctx->generate_session_id; sl@0: sl@0: s->param = X509_VERIFY_PARAM_new(); sl@0: if (!s->param) sl@0: goto err; sl@0: X509_VERIFY_PARAM_inherit(s->param, ctx->param); sl@0: #if 0 sl@0: s->purpose = ctx->purpose; sl@0: s->trust = ctx->trust; sl@0: #endif sl@0: s->quiet_shutdown=ctx->quiet_shutdown; sl@0: sl@0: CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX); sl@0: s->ctx=ctx; sl@0: sl@0: s->verify_result=X509_V_OK; sl@0: sl@0: s->method=ctx->method; sl@0: sl@0: if (!s->method->ssl_new(s)) sl@0: goto err; sl@0: sl@0: s->references=1; sl@0: s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1; sl@0: sl@0: SSL_clear(s); sl@0: sl@0: CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data); sl@0: sl@0: return(s); sl@0: err: sl@0: if (s != NULL) sl@0: { sl@0: if (s->cert != NULL) sl@0: ssl_cert_free(s->cert); sl@0: if (s->ctx != NULL) sl@0: SSL_CTX_free(s->ctx); /* decrement reference count */ sl@0: OPENSSL_free(s); sl@0: } sl@0: SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE); sl@0: return(NULL); sl@0: } sl@0: sl@0: EXPORT_C int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx, sl@0: unsigned int sid_ctx_len) sl@0: { sl@0: if(sid_ctx_len > sizeof ctx->sid_ctx) sl@0: { sl@0: SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); sl@0: return 0; sl@0: } sl@0: ctx->sid_ctx_length=sid_ctx_len; sl@0: memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len); sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx, sl@0: unsigned int sid_ctx_len) sl@0: { sl@0: if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) sl@0: { sl@0: SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); sl@0: return 0; sl@0: } sl@0: ssl->sid_ctx_length=sid_ctx_len; sl@0: memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len); sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) sl@0: { sl@0: CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); sl@0: ctx->generate_session_id = cb; sl@0: CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) sl@0: { sl@0: CRYPTO_w_lock(CRYPTO_LOCK_SSL); sl@0: ssl->generate_session_id = cb; sl@0: CRYPTO_w_unlock(CRYPTO_LOCK_SSL); sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id, sl@0: unsigned int id_len) sl@0: { sl@0: /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how sl@0: * we can "construct" a session to give us the desired check - ie. to sl@0: * find if there's a session in the hash table that would conflict with sl@0: * any new session built out of this id/id_len and the ssl_version in sl@0: * use by this SSL. */ sl@0: SSL_SESSION r, *p; sl@0: sl@0: if(id_len > sizeof r.session_id) sl@0: return 0; sl@0: sl@0: r.ssl_version = ssl->version; sl@0: r.session_id_length = id_len; sl@0: memcpy(r.session_id, id, id_len); sl@0: /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a sl@0: * callback is calling us to check the uniqueness of a shorter ID, it sl@0: * must be compared as a padded-out ID because that is what it will be sl@0: * converted to when the callback has finished choosing it. */ sl@0: if((r.ssl_version == SSL2_VERSION) && sl@0: (id_len < SSL2_SSL_SESSION_ID_LENGTH)) sl@0: { sl@0: memset(r.session_id + id_len, 0, sl@0: SSL2_SSL_SESSION_ID_LENGTH - id_len); sl@0: r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH; sl@0: } sl@0: sl@0: CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); sl@0: p = (SSL_SESSION *)lh_retrieve(ssl->ctx->sessions, &r); sl@0: CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); sl@0: return (p != NULL); sl@0: } sl@0: sl@0: EXPORT_C int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) sl@0: { sl@0: return X509_VERIFY_PARAM_set_purpose(s->param, purpose); sl@0: } sl@0: sl@0: EXPORT_C int SSL_set_purpose(SSL *s, int purpose) sl@0: { sl@0: return X509_VERIFY_PARAM_set_purpose(s->param, purpose); sl@0: } sl@0: sl@0: EXPORT_C int SSL_CTX_set_trust(SSL_CTX *s, int trust) sl@0: { sl@0: return X509_VERIFY_PARAM_set_trust(s->param, trust); sl@0: } sl@0: sl@0: EXPORT_C int SSL_set_trust(SSL *s, int trust) sl@0: { sl@0: return X509_VERIFY_PARAM_set_trust(s->param, trust); sl@0: } sl@0: sl@0: EXPORT_C void SSL_free(SSL *s) sl@0: { sl@0: int i; sl@0: sl@0: if(s == NULL) sl@0: return; sl@0: sl@0: i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL); sl@0: #ifdef REF_PRINT sl@0: REF_PRINT("SSL",s); sl@0: #endif sl@0: if (i > 0) return; sl@0: #ifdef REF_CHECK sl@0: if (i < 0) sl@0: { sl@0: fprintf(stderr,"SSL_free, bad reference count\n"); sl@0: abort(); /* ok */ sl@0: } sl@0: #endif sl@0: sl@0: if (s->param) sl@0: X509_VERIFY_PARAM_free(s->param); sl@0: sl@0: CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data); sl@0: sl@0: if (s->bbio != NULL) sl@0: { sl@0: /* If the buffering BIO is in place, pop it off */ sl@0: if (s->bbio == s->wbio) sl@0: { sl@0: s->wbio=BIO_pop(s->wbio); sl@0: } sl@0: BIO_free(s->bbio); sl@0: s->bbio=NULL; sl@0: } sl@0: if (s->rbio != NULL) sl@0: BIO_free_all(s->rbio); sl@0: if ((s->wbio != NULL) && (s->wbio != s->rbio)) sl@0: BIO_free_all(s->wbio); sl@0: sl@0: if (s->init_buf != NULL) BUF_MEM_free(s->init_buf); sl@0: sl@0: /* add extra stuff */ sl@0: if (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list); sl@0: if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id); sl@0: sl@0: /* Make the next call work :-) */ sl@0: if (s->session != NULL) sl@0: { sl@0: ssl_clear_bad_session(s); sl@0: SSL_SESSION_free(s->session); sl@0: } sl@0: sl@0: ssl_clear_cipher_ctx(s); sl@0: sl@0: if (s->cert != NULL) ssl_cert_free(s->cert); sl@0: /* Free up if allocated */ sl@0: sl@0: if (s->ctx) SSL_CTX_free(s->ctx); sl@0: if (s->client_CA != NULL) sl@0: sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free); sl@0: sl@0: if (s->method != NULL) s->method->ssl_free(s); sl@0: sl@0: #ifndef OPENSSL_NO_KRB5 sl@0: if (s->kssl_ctx != NULL) sl@0: kssl_ctx_free(s->kssl_ctx); sl@0: #endif /* OPENSSL_NO_KRB5 */ sl@0: sl@0: OPENSSL_free(s); sl@0: } sl@0: sl@0: EXPORT_C void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio) sl@0: { sl@0: /* If the output buffering BIO is still in place, remove it sl@0: */ sl@0: if (s->bbio != NULL) sl@0: { sl@0: if (s->wbio == s->bbio) sl@0: { sl@0: s->wbio=s->wbio->next_bio; sl@0: s->bbio->next_bio=NULL; sl@0: } sl@0: } sl@0: if ((s->rbio != NULL) && (s->rbio != rbio)) sl@0: BIO_free_all(s->rbio); sl@0: if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio)) sl@0: BIO_free_all(s->wbio); sl@0: s->rbio=rbio; sl@0: s->wbio=wbio; sl@0: } sl@0: sl@0: EXPORT_C BIO *SSL_get_rbio(const SSL *s) sl@0: { return(s->rbio); } sl@0: sl@0: EXPORT_C BIO *SSL_get_wbio(const SSL *s) sl@0: { return(s->wbio); } sl@0: sl@0: EXPORT_C int SSL_get_fd(const SSL *s) sl@0: { sl@0: return(SSL_get_rfd(s)); sl@0: } sl@0: sl@0: EXPORT_C int SSL_get_rfd(const SSL *s) sl@0: { sl@0: int ret= -1; sl@0: BIO *b,*r; sl@0: sl@0: b=SSL_get_rbio(s); sl@0: r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR); sl@0: if (r != NULL) sl@0: BIO_get_fd(r,&ret); sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C int SSL_get_wfd(const SSL *s) sl@0: { sl@0: int ret= -1; sl@0: BIO *b,*r; sl@0: sl@0: b=SSL_get_wbio(s); sl@0: r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR); sl@0: if (r != NULL) sl@0: BIO_get_fd(r,&ret); sl@0: return(ret); sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_SOCK sl@0: EXPORT_C int SSL_set_fd(SSL *s,int fd) sl@0: { sl@0: int ret=0; sl@0: BIO *bio=NULL; sl@0: sl@0: bio=BIO_new(BIO_s_socket()); sl@0: sl@0: if (bio == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB); sl@0: goto err; sl@0: } sl@0: BIO_set_fd(bio,fd,BIO_NOCLOSE); sl@0: SSL_set_bio(s,bio,bio); sl@0: ret=1; sl@0: err: sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C int SSL_set_wfd(SSL *s,int fd) sl@0: { sl@0: int ret=0; sl@0: BIO *bio=NULL; sl@0: sl@0: if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET) sl@0: || ((int)BIO_get_fd(s->rbio,NULL) != fd)) sl@0: { sl@0: bio=BIO_new(BIO_s_socket()); sl@0: sl@0: if (bio == NULL) sl@0: { SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; } sl@0: BIO_set_fd(bio,fd,BIO_NOCLOSE); sl@0: SSL_set_bio(s,SSL_get_rbio(s),bio); sl@0: } sl@0: else sl@0: SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s)); sl@0: ret=1; sl@0: err: sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C int SSL_set_rfd(SSL *s,int fd) sl@0: { sl@0: int ret=0; sl@0: BIO *bio=NULL; sl@0: sl@0: if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET) sl@0: || ((int)BIO_get_fd(s->wbio,NULL) != fd)) sl@0: { sl@0: bio=BIO_new(BIO_s_socket()); sl@0: sl@0: if (bio == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB); sl@0: goto err; sl@0: } sl@0: BIO_set_fd(bio,fd,BIO_NOCLOSE); sl@0: SSL_set_bio(s,bio,SSL_get_wbio(s)); sl@0: } sl@0: else sl@0: SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s)); sl@0: ret=1; sl@0: err: sl@0: return(ret); sl@0: } sl@0: #endif sl@0: sl@0: sl@0: /* return length of latest Finished message we sent, copy to 'buf' */ sl@0: EXPORT_C size_t SSL_get_finished(const SSL *s, void *buf, size_t count) sl@0: { sl@0: size_t ret = 0; sl@0: sl@0: if (s->s3 != NULL) sl@0: { sl@0: ret = s->s3->tmp.finish_md_len; sl@0: if (count > ret) sl@0: count = ret; sl@0: memcpy(buf, s->s3->tmp.finish_md, count); sl@0: } sl@0: return ret; sl@0: } sl@0: sl@0: /* return length of latest Finished message we expected, copy to 'buf' */ sl@0: EXPORT_C size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) sl@0: { sl@0: size_t ret = 0; sl@0: sl@0: if (s->s3 != NULL) sl@0: { sl@0: ret = s->s3->tmp.peer_finish_md_len; sl@0: if (count > ret) sl@0: count = ret; sl@0: memcpy(buf, s->s3->tmp.peer_finish_md, count); sl@0: } sl@0: return ret; sl@0: } sl@0: sl@0: sl@0: EXPORT_C int SSL_get_verify_mode(const SSL *s) sl@0: { sl@0: return(s->verify_mode); sl@0: } sl@0: sl@0: EXPORT_C int SSL_get_verify_depth(const SSL *s) sl@0: { sl@0: return X509_VERIFY_PARAM_get_depth(s->param); sl@0: } sl@0: sl@0: EXPORT_C int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *) sl@0: { sl@0: return(s->verify_callback); sl@0: } sl@0: sl@0: EXPORT_C int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) sl@0: { sl@0: return(ctx->verify_mode); sl@0: } sl@0: sl@0: EXPORT_C int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) sl@0: { sl@0: return X509_VERIFY_PARAM_get_depth(ctx->param); sl@0: } sl@0: sl@0: EXPORT_C int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *) sl@0: { sl@0: return(ctx->default_verify_callback); sl@0: } sl@0: sl@0: EXPORT_C void SSL_set_verify(SSL *s,int mode, sl@0: int (*callback)(int ok,X509_STORE_CTX *ctx)) sl@0: { sl@0: s->verify_mode=mode; sl@0: if (callback != NULL) sl@0: s->verify_callback=callback; sl@0: } sl@0: sl@0: EXPORT_C void SSL_set_verify_depth(SSL *s,int depth) sl@0: { sl@0: X509_VERIFY_PARAM_set_depth(s->param, depth); sl@0: } sl@0: sl@0: EXPORT_C void SSL_set_read_ahead(SSL *s,int yes) sl@0: { sl@0: s->read_ahead=yes; sl@0: } sl@0: sl@0: EXPORT_C int SSL_get_read_ahead(const SSL *s) sl@0: { sl@0: return(s->read_ahead); sl@0: } sl@0: sl@0: EXPORT_C int SSL_pending(const SSL *s) sl@0: { sl@0: /* SSL_pending cannot work properly if read-ahead is enabled sl@0: * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), sl@0: * and it is impossible to fix since SSL_pending cannot report sl@0: * errors that may be observed while scanning the new data. sl@0: * (Note that SSL_pending() is often used as a boolean value, sl@0: * so we'd better not return -1.) sl@0: */ sl@0: return(s->method->ssl_pending(s)); sl@0: } sl@0: sl@0: EXPORT_C X509 *SSL_get_peer_certificate(const SSL *s) sl@0: { sl@0: X509 *r; sl@0: sl@0: if ((s == NULL) || (s->session == NULL)) sl@0: r=NULL; sl@0: else sl@0: r=s->session->peer; sl@0: sl@0: if (r == NULL) return(r); sl@0: sl@0: CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509); sl@0: sl@0: return(r); sl@0: } sl@0: sl@0: EXPORT_C STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s) sl@0: { sl@0: STACK_OF(X509) *r; sl@0: sl@0: if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL)) sl@0: r=NULL; sl@0: else sl@0: r=s->session->sess_cert->cert_chain; sl@0: sl@0: /* If we are a client, cert_chain includes the peer's own sl@0: * certificate; if we are a server, it does not. */ sl@0: sl@0: return(r); sl@0: } sl@0: sl@0: /* Now in theory, since the calling process own 't' it should be safe to sl@0: * modify. We need to be able to read f without being hassled */ sl@0: EXPORT_C void SSL_copy_session_id(SSL *t,const SSL *f) sl@0: { sl@0: CERT *tmp; sl@0: sl@0: /* Do we need to to SSL locking? */ sl@0: SSL_set_session(t,SSL_get_session(f)); sl@0: sl@0: /* what if we are setup as SSLv2 but want to talk SSLv3 or sl@0: * vice-versa */ sl@0: if (t->method != f->method) sl@0: { sl@0: t->method->ssl_free(t); /* cleanup current */ sl@0: t->method=f->method; /* change method */ sl@0: t->method->ssl_new(t); /* setup new */ sl@0: } sl@0: sl@0: tmp=t->cert; sl@0: if (f->cert != NULL) sl@0: { sl@0: CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT); sl@0: t->cert=f->cert; sl@0: } sl@0: else sl@0: t->cert=NULL; sl@0: if (tmp != NULL) ssl_cert_free(tmp); sl@0: SSL_set_session_id_context(t,f->sid_ctx,f->sid_ctx_length); sl@0: } sl@0: sl@0: /* Fix this so it checks all the valid key/cert options */ sl@0: EXPORT_C int SSL_CTX_check_private_key(const SSL_CTX *ctx) sl@0: { sl@0: if ( (ctx == NULL) || sl@0: (ctx->cert == NULL) || sl@0: (ctx->cert->key->x509 == NULL)) sl@0: { sl@0: SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED); sl@0: return(0); sl@0: } sl@0: if (ctx->cert->key->privatekey == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED); sl@0: return(0); sl@0: } sl@0: return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey)); sl@0: } sl@0: sl@0: /* Fix this function so that it takes an optional type parameter */ sl@0: EXPORT_C int SSL_check_private_key(const SSL *ssl) sl@0: { sl@0: if (ssl == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER); sl@0: return(0); sl@0: } sl@0: if (ssl->cert == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED); sl@0: return 0; sl@0: } sl@0: if (ssl->cert->key->x509 == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED); sl@0: return(0); sl@0: } sl@0: if (ssl->cert->key->privatekey == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED); sl@0: return(0); sl@0: } sl@0: return(X509_check_private_key(ssl->cert->key->x509, sl@0: ssl->cert->key->privatekey)); sl@0: } sl@0: sl@0: EXPORT_C int SSL_accept(SSL *s) sl@0: { sl@0: if (s->handshake_func == 0) sl@0: /* Not properly initialized yet */ sl@0: SSL_set_accept_state(s); sl@0: sl@0: return(s->method->ssl_accept(s)); sl@0: } sl@0: sl@0: EXPORT_C int SSL_connect(SSL *s) sl@0: { sl@0: if (s->handshake_func == 0) sl@0: /* Not properly initialized yet */ sl@0: SSL_set_connect_state(s); sl@0: sl@0: return(s->method->ssl_connect(s)); sl@0: } sl@0: sl@0: EXPORT_C long SSL_get_default_timeout(const SSL *s) sl@0: { sl@0: return(s->method->get_timeout()); sl@0: } sl@0: sl@0: EXPORT_C int SSL_read(SSL *s,void *buf,int num) sl@0: { sl@0: if (s->handshake_func == 0) sl@0: { sl@0: SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED); sl@0: return -1; sl@0: } sl@0: sl@0: if (s->shutdown & SSL_RECEIVED_SHUTDOWN) sl@0: { sl@0: s->rwstate=SSL_NOTHING; sl@0: return(0); sl@0: } sl@0: return(s->method->ssl_read(s,buf,num)); sl@0: } sl@0: sl@0: EXPORT_C int SSL_peek(SSL *s,void *buf,int num) sl@0: { sl@0: if (s->handshake_func == 0) sl@0: { sl@0: SSLerr(SSL_F_SSL_PEEK, SSL_R_UNINITIALIZED); sl@0: return -1; sl@0: } sl@0: sl@0: if (s->shutdown & SSL_RECEIVED_SHUTDOWN) sl@0: { sl@0: return(0); sl@0: } sl@0: return(s->method->ssl_peek(s,buf,num)); sl@0: } sl@0: sl@0: EXPORT_C int SSL_write(SSL *s,const void *buf,int num) sl@0: { sl@0: if (s->handshake_func == 0) sl@0: { sl@0: SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED); sl@0: return -1; sl@0: } sl@0: sl@0: if (s->shutdown & SSL_SENT_SHUTDOWN) sl@0: { sl@0: s->rwstate=SSL_NOTHING; sl@0: SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN); sl@0: return(-1); sl@0: } sl@0: return(s->method->ssl_write(s,buf,num)); sl@0: } sl@0: sl@0: EXPORT_C int SSL_shutdown(SSL *s) sl@0: { sl@0: /* Note that this function behaves differently from what one might sl@0: * expect. Return values are 0 for no success (yet), sl@0: * 1 for success; but calling it once is usually not enough, sl@0: * even if blocking I/O is used (see ssl3_shutdown). sl@0: */ sl@0: sl@0: if (s->handshake_func == 0) sl@0: { sl@0: SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED); sl@0: return -1; sl@0: } sl@0: sl@0: if ((s != NULL) && !SSL_in_init(s)) sl@0: return(s->method->ssl_shutdown(s)); sl@0: else sl@0: return(1); sl@0: } sl@0: sl@0: EXPORT_C int SSL_renegotiate(SSL *s) sl@0: { sl@0: if (s->new_session == 0) sl@0: { sl@0: s->new_session=1; sl@0: } sl@0: return(s->method->ssl_renegotiate(s)); sl@0: } sl@0: sl@0: EXPORT_C int SSL_renegotiate_pending(SSL *s) sl@0: { sl@0: /* becomes true when negotiation is requested; sl@0: * false again once a handshake has finished */ sl@0: return (s->new_session != 0); sl@0: } sl@0: sl@0: EXPORT_C long SSL_ctrl(SSL *s,int cmd,long larg,void *parg) sl@0: { sl@0: long l; sl@0: sl@0: switch (cmd) sl@0: { sl@0: case SSL_CTRL_GET_READ_AHEAD: sl@0: return(s->read_ahead); sl@0: case SSL_CTRL_SET_READ_AHEAD: sl@0: l=s->read_ahead; sl@0: s->read_ahead=larg; sl@0: return(l); sl@0: sl@0: case SSL_CTRL_SET_MSG_CALLBACK_ARG: sl@0: s->msg_callback_arg = parg; sl@0: return 1; sl@0: sl@0: case SSL_CTRL_OPTIONS: sl@0: return(s->options|=larg); sl@0: case SSL_CTRL_MODE: sl@0: return(s->mode|=larg); sl@0: case SSL_CTRL_GET_MAX_CERT_LIST: sl@0: return(s->max_cert_list); sl@0: case SSL_CTRL_SET_MAX_CERT_LIST: sl@0: l=s->max_cert_list; sl@0: s->max_cert_list=larg; sl@0: return(l); sl@0: case SSL_CTRL_SET_MTU: sl@0: if (SSL_version(s) == DTLS1_VERSION) sl@0: { sl@0: s->d1->mtu = larg; sl@0: return larg; sl@0: } sl@0: return 0; sl@0: default: sl@0: return(s->method->ssl_ctrl(s,cmd,larg,parg)); sl@0: } sl@0: } sl@0: sl@0: EXPORT_C long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) sl@0: { sl@0: switch(cmd) sl@0: { sl@0: case SSL_CTRL_SET_MSG_CALLBACK: sl@0: s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp); sl@0: return 1; sl@0: sl@0: default: sl@0: return(s->method->ssl_callback_ctrl(s,cmd,fp)); sl@0: } sl@0: } sl@0: sl@0: EXPORT_C struct lhash_st *SSL_CTX_sessions(SSL_CTX *ctx) sl@0: { sl@0: return ctx->sessions; sl@0: } sl@0: sl@0: EXPORT_C long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg) sl@0: { sl@0: long l; sl@0: sl@0: switch (cmd) sl@0: { sl@0: case SSL_CTRL_GET_READ_AHEAD: sl@0: return(ctx->read_ahead); sl@0: case SSL_CTRL_SET_READ_AHEAD: sl@0: l=ctx->read_ahead; sl@0: ctx->read_ahead=larg; sl@0: return(l); sl@0: sl@0: case SSL_CTRL_SET_MSG_CALLBACK_ARG: sl@0: ctx->msg_callback_arg = parg; sl@0: return 1; sl@0: sl@0: case SSL_CTRL_GET_MAX_CERT_LIST: sl@0: return(ctx->max_cert_list); sl@0: case SSL_CTRL_SET_MAX_CERT_LIST: sl@0: l=ctx->max_cert_list; sl@0: ctx->max_cert_list=larg; sl@0: return(l); sl@0: sl@0: case SSL_CTRL_SET_SESS_CACHE_SIZE: sl@0: l=ctx->session_cache_size; sl@0: ctx->session_cache_size=larg; sl@0: return(l); sl@0: case SSL_CTRL_GET_SESS_CACHE_SIZE: sl@0: return(ctx->session_cache_size); sl@0: case SSL_CTRL_SET_SESS_CACHE_MODE: sl@0: l=ctx->session_cache_mode; sl@0: ctx->session_cache_mode=larg; sl@0: return(l); sl@0: case SSL_CTRL_GET_SESS_CACHE_MODE: sl@0: return(ctx->session_cache_mode); sl@0: sl@0: case SSL_CTRL_SESS_NUMBER: sl@0: return(ctx->sessions->num_items); sl@0: case SSL_CTRL_SESS_CONNECT: sl@0: return(ctx->stats.sess_connect); sl@0: case SSL_CTRL_SESS_CONNECT_GOOD: sl@0: return(ctx->stats.sess_connect_good); sl@0: case SSL_CTRL_SESS_CONNECT_RENEGOTIATE: sl@0: return(ctx->stats.sess_connect_renegotiate); sl@0: case SSL_CTRL_SESS_ACCEPT: sl@0: return(ctx->stats.sess_accept); sl@0: case SSL_CTRL_SESS_ACCEPT_GOOD: sl@0: return(ctx->stats.sess_accept_good); sl@0: case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE: sl@0: return(ctx->stats.sess_accept_renegotiate); sl@0: case SSL_CTRL_SESS_HIT: sl@0: return(ctx->stats.sess_hit); sl@0: case SSL_CTRL_SESS_CB_HIT: sl@0: return(ctx->stats.sess_cb_hit); sl@0: case SSL_CTRL_SESS_MISSES: sl@0: return(ctx->stats.sess_miss); sl@0: case SSL_CTRL_SESS_TIMEOUTS: sl@0: return(ctx->stats.sess_timeout); sl@0: case SSL_CTRL_SESS_CACHE_FULL: sl@0: return(ctx->stats.sess_cache_full); sl@0: case SSL_CTRL_OPTIONS: sl@0: return(ctx->options|=larg); sl@0: case SSL_CTRL_MODE: sl@0: return(ctx->mode|=larg); sl@0: default: sl@0: return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg)); sl@0: } sl@0: } sl@0: sl@0: EXPORT_C long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) sl@0: { sl@0: switch(cmd) sl@0: { sl@0: case SSL_CTRL_SET_MSG_CALLBACK: sl@0: ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp); sl@0: return 1; sl@0: sl@0: default: sl@0: return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp)); sl@0: } sl@0: } sl@0: sl@0: int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b) sl@0: { sl@0: long l; sl@0: sl@0: l=a->id-b->id; sl@0: if (l == 0L) sl@0: return(0); sl@0: else sl@0: return((l > 0)?1:-1); sl@0: } sl@0: sl@0: int ssl_cipher_ptr_id_cmp(const SSL_CIPHER * const *ap, sl@0: const SSL_CIPHER * const *bp) sl@0: { sl@0: long l; sl@0: sl@0: l=(*ap)->id-(*bp)->id; sl@0: if (l == 0L) sl@0: return(0); sl@0: else sl@0: return((l > 0)?1:-1); sl@0: } sl@0: sl@0: /** return a STACK of the ciphers available for the SSL and in order of sl@0: * preference */ sl@0: EXPORT_C STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) sl@0: { sl@0: if (s != NULL) sl@0: { sl@0: if (s->cipher_list != NULL) sl@0: { sl@0: return(s->cipher_list); sl@0: } sl@0: else if ((s->ctx != NULL) && sl@0: (s->ctx->cipher_list != NULL)) sl@0: { sl@0: return(s->ctx->cipher_list); sl@0: } sl@0: } sl@0: return(NULL); sl@0: } sl@0: sl@0: /** return a STACK of the ciphers available for the SSL and in order of sl@0: * algorithm id */ sl@0: STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) sl@0: { sl@0: if (s != NULL) sl@0: { sl@0: if (s->cipher_list_by_id != NULL) sl@0: { sl@0: return(s->cipher_list_by_id); sl@0: } sl@0: else if ((s->ctx != NULL) && sl@0: (s->ctx->cipher_list_by_id != NULL)) sl@0: { sl@0: return(s->ctx->cipher_list_by_id); sl@0: } sl@0: } sl@0: return(NULL); sl@0: } sl@0: sl@0: /** The old interface to get the same thing as SSL_get_ciphers() */ sl@0: EXPORT_C const char *SSL_get_cipher_list(const SSL *s,int n) sl@0: { sl@0: SSL_CIPHER *c; sl@0: STACK_OF(SSL_CIPHER) *sk; sl@0: sl@0: if (s == NULL) return(NULL); sl@0: sk=SSL_get_ciphers(s); sl@0: if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n)) sl@0: return(NULL); sl@0: c=sk_SSL_CIPHER_value(sk,n); sl@0: if (c == NULL) return(NULL); sl@0: return(c->name); sl@0: } sl@0: sl@0: /** specify the ciphers to be used by default by the SSL_CTX */ sl@0: EXPORT_C int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) sl@0: { sl@0: STACK_OF(SSL_CIPHER) *sk; sl@0: sl@0: sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list, sl@0: &ctx->cipher_list_by_id,str); sl@0: /* ssl_create_cipher_list may return an empty stack if it sl@0: * was unable to find a cipher matching the given rule string sl@0: * (for example if the rule string specifies a cipher which sl@0: * has been disabled). This is not an error as far as sl@0: * ssl_create_cipher_list is concerned, and hence sl@0: * ctx->cipher_list and ctx->cipher_list_by_id has been sl@0: * updated. */ sl@0: if (sk == NULL) sl@0: return 0; sl@0: else if (sk_SSL_CIPHER_num(sk) == 0) sl@0: { sl@0: SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH); sl@0: return 0; sl@0: } sl@0: return 1; sl@0: } sl@0: sl@0: /** specify the ciphers to be used by the SSL */ sl@0: EXPORT_C int SSL_set_cipher_list(SSL *s,const char *str) sl@0: { sl@0: STACK_OF(SSL_CIPHER) *sk; sl@0: sl@0: sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list, sl@0: &s->cipher_list_by_id,str); sl@0: /* see comment in SSL_CTX_set_cipher_list */ sl@0: if (sk == NULL) sl@0: return 0; sl@0: else if (sk_SSL_CIPHER_num(sk) == 0) sl@0: { sl@0: SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH); sl@0: return 0; sl@0: } sl@0: return 1; sl@0: } sl@0: sl@0: /* works well for SSLv2, not so good for SSLv3 */ sl@0: EXPORT_C char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len) sl@0: { sl@0: char *p; sl@0: STACK_OF(SSL_CIPHER) *sk; sl@0: SSL_CIPHER *c; sl@0: int i; sl@0: sl@0: if ((s->session == NULL) || (s->session->ciphers == NULL) || sl@0: (len < 2)) sl@0: return(NULL); sl@0: sl@0: p=buf; sl@0: sk=s->session->ciphers; sl@0: for (i=0; iname); sl@0: if (n+1 > len) sl@0: { sl@0: if (p != buf) sl@0: --p; sl@0: *p='\0'; sl@0: return buf; sl@0: } sl@0: strcpy(p,c->name); sl@0: p+=n; sl@0: *(p++)=':'; sl@0: len-=n+1; sl@0: } sl@0: p[-1]='\0'; sl@0: return(buf); sl@0: } sl@0: sl@0: int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p, sl@0: int (*put_cb)(const SSL_CIPHER *, unsigned char *)) sl@0: { sl@0: int i,j=0; sl@0: SSL_CIPHER *c; sl@0: unsigned char *q; sl@0: #ifndef OPENSSL_NO_KRB5 sl@0: int nokrb5 = !kssl_tgt_is_available(s->kssl_ctx); sl@0: #endif /* OPENSSL_NO_KRB5 */ sl@0: sl@0: if (sk == NULL) return(0); sl@0: q=p; sl@0: sl@0: for (i=0; ialgorithms & SSL_KRB5) && nokrb5) sl@0: continue; sl@0: #endif /* OPENSSL_NO_KRB5 */ sl@0: sl@0: j = put_cb ? put_cb(c,p) : ssl_put_cipher_by_char(s,c,p); sl@0: p+=j; sl@0: } sl@0: return(p-q); sl@0: } sl@0: sl@0: STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num, sl@0: STACK_OF(SSL_CIPHER) **skp) sl@0: { sl@0: SSL_CIPHER *c; sl@0: STACK_OF(SSL_CIPHER) *sk; sl@0: int i,n; sl@0: sl@0: n=ssl_put_cipher_by_char(s,NULL,NULL); sl@0: if ((num%n) != 0) sl@0: { sl@0: SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); sl@0: return(NULL); sl@0: } sl@0: if ((skp == NULL) || (*skp == NULL)) sl@0: sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */ sl@0: else sl@0: { sl@0: sk= *skp; sl@0: sk_SSL_CIPHER_zero(sk); sl@0: } sl@0: sl@0: for (i=0; isession_id[0] )| sl@0: ((unsigned int) a->session_id[1]<< 8L)| sl@0: ((unsigned long)a->session_id[2]<<16L)| sl@0: ((unsigned long)a->session_id[3]<<24L); sl@0: return(l); sl@0: } sl@0: sl@0: /* NB: If this function (or indeed the hash function which uses a sort of sl@0: * coarser function than this one) is changed, ensure sl@0: * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being sl@0: * able to construct an SSL_SESSION that will collide with any existing session sl@0: * with a matching session ID. */ sl@0: EXPORT_C int SSL_SESSION_cmp(const SSL_SESSION *a,const SSL_SESSION *b) sl@0: { sl@0: if (a->ssl_version != b->ssl_version) sl@0: return(1); sl@0: if (a->session_id_length != b->session_id_length) sl@0: return(1); sl@0: return(memcmp(a->session_id,b->session_id,a->session_id_length)); sl@0: } sl@0: sl@0: /* These wrapper functions should remain rather than redeclaring sl@0: * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each sl@0: * variable. The reason is that the functions aren't static, they're exposed via sl@0: * ssl.h. */ sl@0: static IMPLEMENT_LHASH_HASH_FN(SSL_SESSION_hash, SSL_SESSION *) sl@0: static IMPLEMENT_LHASH_COMP_FN(SSL_SESSION_cmp, SSL_SESSION *) sl@0: sl@0: EXPORT_C SSL_CTX *SSL_CTX_new(SSL_METHOD *meth) sl@0: { sl@0: SSL_CTX *ret=NULL; sl@0: sl@0: if (meth == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED); sl@0: return(NULL); sl@0: } sl@0: sl@0: if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) sl@0: { sl@0: SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS); sl@0: goto err; sl@0: } sl@0: ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX)); sl@0: if (ret == NULL) sl@0: goto err; sl@0: sl@0: memset(ret,0,sizeof(SSL_CTX)); sl@0: sl@0: ret->method=meth; sl@0: sl@0: ret->cert_store=NULL; sl@0: ret->session_cache_mode=SSL_SESS_CACHE_SERVER; sl@0: ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; sl@0: ret->session_cache_head=NULL; sl@0: ret->session_cache_tail=NULL; sl@0: sl@0: /* We take the system default */ sl@0: ret->session_timeout=meth->get_timeout(); sl@0: sl@0: ret->new_session_cb=0; sl@0: ret->remove_session_cb=0; sl@0: ret->get_session_cb=0; sl@0: ret->generate_session_id=0; sl@0: sl@0: memset((char *)&ret->stats,0,sizeof(ret->stats)); sl@0: sl@0: ret->references=1; sl@0: ret->quiet_shutdown=0; sl@0: sl@0: /* ret->cipher=NULL;*/ sl@0: /* ret->s2->challenge=NULL; sl@0: ret->master_key=NULL; sl@0: ret->key_arg=NULL; sl@0: ret->s2->conn_id=NULL; */ sl@0: sl@0: ret->info_callback=NULL; sl@0: sl@0: ret->app_verify_callback=0; sl@0: ret->app_verify_arg=NULL; sl@0: sl@0: ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT; sl@0: ret->read_ahead=0; sl@0: ret->msg_callback=0; sl@0: ret->msg_callback_arg=NULL; sl@0: ret->verify_mode=SSL_VERIFY_NONE; sl@0: #if 0 sl@0: ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */ sl@0: #endif sl@0: ret->sid_ctx_length=0; sl@0: ret->default_verify_callback=NULL; sl@0: if ((ret->cert=ssl_cert_new()) == NULL) sl@0: goto err; sl@0: sl@0: ret->default_passwd_callback=0; sl@0: ret->default_passwd_callback_userdata=NULL; sl@0: ret->client_cert_cb=0; sl@0: ret->app_gen_cookie_cb=0; sl@0: ret->app_verify_cookie_cb=0; sl@0: sl@0: ret->sessions=lh_new(LHASH_HASH_FN(SSL_SESSION_hash), sl@0: LHASH_COMP_FN(SSL_SESSION_cmp)); sl@0: if (ret->sessions == NULL) goto err; sl@0: ret->cert_store=X509_STORE_new(); sl@0: if (ret->cert_store == NULL) goto err; sl@0: sl@0: ssl_create_cipher_list(ret->method, sl@0: &ret->cipher_list,&ret->cipher_list_by_id, sl@0: SSL_DEFAULT_CIPHER_LIST); sl@0: if (ret->cipher_list == NULL sl@0: || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) sl@0: { sl@0: SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS); sl@0: goto err2; sl@0: } sl@0: sl@0: ret->param = X509_VERIFY_PARAM_new(); sl@0: if (!ret->param) sl@0: goto err; sl@0: sl@0: if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES); sl@0: goto err2; sl@0: } sl@0: if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES); sl@0: goto err2; sl@0: } sl@0: if ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES); sl@0: goto err2; sl@0: } sl@0: sl@0: if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL) sl@0: goto err; sl@0: sl@0: CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data); sl@0: sl@0: ret->extra_certs=NULL; sl@0: ret->comp_methods=SSL_COMP_get_compression_methods(); sl@0: sl@0: return(ret); sl@0: err: sl@0: SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE); sl@0: err2: sl@0: if (ret != NULL) SSL_CTX_free(ret); sl@0: return(NULL); sl@0: } sl@0: sl@0: #if 0 sl@0: static void SSL_COMP_free(SSL_COMP *comp) sl@0: { OPENSSL_free(comp); } sl@0: #endif sl@0: sl@0: EXPORT_C void SSL_CTX_free(SSL_CTX *a) sl@0: { sl@0: int i; sl@0: sl@0: if (a == NULL) return; sl@0: sl@0: i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX); sl@0: #ifdef REF_PRINT sl@0: REF_PRINT("SSL_CTX",a); sl@0: #endif sl@0: if (i > 0) return; sl@0: #ifdef REF_CHECK sl@0: if (i < 0) sl@0: { sl@0: fprintf(stderr,"SSL_CTX_free, bad reference count\n"); sl@0: abort(); /* ok */ sl@0: } sl@0: #endif sl@0: sl@0: if (a->param) sl@0: X509_VERIFY_PARAM_free(a->param); sl@0: sl@0: /* sl@0: * Free internal session cache. However: the remove_cb() may reference sl@0: * the ex_data of SSL_CTX, thus the ex_data store can only be removed sl@0: * after the sessions were flushed. sl@0: * As the ex_data handling routines might also touch the session cache, sl@0: * the most secure solution seems to be: empty (flush) the cache, then sl@0: * free ex_data, then finally free the cache. sl@0: * (See ticket [openssl.org #212].) sl@0: */ sl@0: if (a->sessions != NULL) sl@0: SSL_CTX_flush_sessions(a,0); sl@0: sl@0: CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data); sl@0: sl@0: if (a->sessions != NULL) sl@0: lh_free(a->sessions); sl@0: sl@0: if (a->cert_store != NULL) sl@0: X509_STORE_free(a->cert_store); sl@0: if (a->cipher_list != NULL) sl@0: sk_SSL_CIPHER_free(a->cipher_list); sl@0: if (a->cipher_list_by_id != NULL) sl@0: sk_SSL_CIPHER_free(a->cipher_list_by_id); sl@0: if (a->cert != NULL) sl@0: ssl_cert_free(a->cert); sl@0: if (a->client_CA != NULL) sl@0: sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free); sl@0: if (a->extra_certs != NULL) sl@0: sk_X509_pop_free(a->extra_certs,X509_free); sl@0: #if 0 /* This should never be done, since it removes a global database */ sl@0: if (a->comp_methods != NULL) sl@0: sk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free); sl@0: #else sl@0: a->comp_methods = NULL; sl@0: #endif sl@0: OPENSSL_free(a); sl@0: } sl@0: sl@0: EXPORT_C void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) sl@0: { sl@0: ctx->default_passwd_callback=cb; sl@0: } sl@0: sl@0: EXPORT_C void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u) sl@0: { sl@0: ctx->default_passwd_callback_userdata=u; sl@0: } sl@0: sl@0: EXPORT_C void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg) sl@0: { sl@0: ctx->app_verify_callback=cb; sl@0: ctx->app_verify_arg=arg; sl@0: } sl@0: sl@0: EXPORT_C void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *)) sl@0: { sl@0: ctx->verify_mode=mode; sl@0: ctx->default_verify_callback=cb; sl@0: } sl@0: sl@0: EXPORT_C void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth) sl@0: { sl@0: X509_VERIFY_PARAM_set_depth(ctx->param, depth); sl@0: } sl@0: sl@0: void ssl_set_cert_masks(CERT *c, SSL_CIPHER *cipher) sl@0: { sl@0: CERT_PKEY *cpk; sl@0: int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign; sl@0: int rsa_enc_export,dh_rsa_export,dh_dsa_export; sl@0: int rsa_tmp_export,dh_tmp_export,kl; sl@0: unsigned long mask,emask; sl@0: int have_ecc_cert, ecdh_ok, ecdsa_ok, ecc_pkey_size; sl@0: #ifndef OPENSSL_NO_ECDH sl@0: int have_ecdh_tmp; sl@0: #endif sl@0: X509 *x = NULL; sl@0: EVP_PKEY *ecc_pkey = NULL; sl@0: int signature_nid = 0; sl@0: sl@0: if (c == NULL) return; sl@0: sl@0: kl=SSL_C_EXPORT_PKEYLENGTH(cipher); sl@0: sl@0: #ifndef OPENSSL_NO_RSA sl@0: rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL); sl@0: rsa_tmp_export=(c->rsa_tmp_cb != NULL || sl@0: (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl)); sl@0: #else sl@0: rsa_tmp=rsa_tmp_export=0; sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL); sl@0: dh_tmp_export=(c->dh_tmp_cb != NULL || sl@0: (dh_tmp && DH_size(c->dh_tmp)*8 <= kl)); sl@0: #else sl@0: dh_tmp=dh_tmp_export=0; sl@0: #endif sl@0: sl@0: #ifndef OPENSSL_NO_ECDH sl@0: have_ecdh_tmp=(c->ecdh_tmp != NULL || c->ecdh_tmp_cb != NULL); sl@0: #endif sl@0: cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]); sl@0: rsa_enc= (cpk->x509 != NULL && cpk->privatekey != NULL); sl@0: rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl); sl@0: cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]); sl@0: rsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL); sl@0: cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]); sl@0: dsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL); sl@0: cpk= &(c->pkeys[SSL_PKEY_DH_RSA]); sl@0: dh_rsa= (cpk->x509 != NULL && cpk->privatekey != NULL); sl@0: dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl); sl@0: cpk= &(c->pkeys[SSL_PKEY_DH_DSA]); sl@0: /* FIX THIS EAY EAY EAY */ sl@0: dh_dsa= (cpk->x509 != NULL && cpk->privatekey != NULL); sl@0: dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl); sl@0: cpk= &(c->pkeys[SSL_PKEY_ECC]); sl@0: have_ecc_cert= (cpk->x509 != NULL && cpk->privatekey != NULL); sl@0: mask=0; sl@0: emask=0; sl@0: sl@0: #ifdef CIPHER_DEBUG sl@0: printf("rt=%d rte=%d dht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n", sl@0: rsa_tmp,rsa_tmp_export,dh_tmp, sl@0: rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa); sl@0: #endif sl@0: sl@0: if (rsa_enc || (rsa_tmp && rsa_sign)) sl@0: mask|=SSL_kRSA; sl@0: if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc))) sl@0: emask|=SSL_kRSA; sl@0: sl@0: #if 0 sl@0: /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */ sl@0: if ( (dh_tmp || dh_rsa || dh_dsa) && sl@0: (rsa_enc || rsa_sign || dsa_sign)) sl@0: mask|=SSL_kEDH; sl@0: if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) && sl@0: (rsa_enc || rsa_sign || dsa_sign)) sl@0: emask|=SSL_kEDH; sl@0: #endif sl@0: sl@0: if (dh_tmp_export) sl@0: emask|=SSL_kEDH; sl@0: sl@0: if (dh_tmp) sl@0: mask|=SSL_kEDH; sl@0: sl@0: if (dh_rsa) mask|=SSL_kDHr; sl@0: if (dh_rsa_export) emask|=SSL_kDHr; sl@0: sl@0: if (dh_dsa) mask|=SSL_kDHd; sl@0: if (dh_dsa_export) emask|=SSL_kDHd; sl@0: sl@0: if (rsa_enc || rsa_sign) sl@0: { sl@0: mask|=SSL_aRSA; sl@0: emask|=SSL_aRSA; sl@0: } sl@0: sl@0: if (dsa_sign) sl@0: { sl@0: mask|=SSL_aDSS; sl@0: emask|=SSL_aDSS; sl@0: } sl@0: sl@0: mask|=SSL_aNULL; sl@0: emask|=SSL_aNULL; sl@0: sl@0: #ifndef OPENSSL_NO_KRB5 sl@0: mask|=SSL_kKRB5|SSL_aKRB5; sl@0: emask|=SSL_kKRB5|SSL_aKRB5; sl@0: #endif sl@0: sl@0: /* An ECC certificate may be usable for ECDH and/or sl@0: * ECDSA cipher suites depending on the key usage extension. sl@0: */ sl@0: if (have_ecc_cert) sl@0: { sl@0: /* This call populates extension flags (ex_flags) */ sl@0: x = (c->pkeys[SSL_PKEY_ECC]).x509; sl@0: X509_check_purpose(x, -1, 0); sl@0: ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ? sl@0: (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1; sl@0: ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ? sl@0: (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1; sl@0: ecc_pkey = X509_get_pubkey(x); sl@0: ecc_pkey_size = (ecc_pkey != NULL) ? sl@0: EVP_PKEY_bits(ecc_pkey) : 0; sl@0: EVP_PKEY_free(ecc_pkey); sl@0: if ((x->sig_alg) && (x->sig_alg->algorithm)) sl@0: signature_nid = OBJ_obj2nid(x->sig_alg->algorithm); sl@0: #ifndef OPENSSL_NO_ECDH sl@0: if (ecdh_ok) sl@0: { sl@0: if ((signature_nid == NID_md5WithRSAEncryption) || sl@0: (signature_nid == NID_md4WithRSAEncryption) || sl@0: (signature_nid == NID_md2WithRSAEncryption)) sl@0: { sl@0: mask|=SSL_kECDH|SSL_aRSA; sl@0: if (ecc_pkey_size <= 163) sl@0: emask|=SSL_kECDH|SSL_aRSA; sl@0: } sl@0: if (signature_nid == NID_ecdsa_with_SHA1) sl@0: { sl@0: mask|=SSL_kECDH|SSL_aECDSA; sl@0: if (ecc_pkey_size <= 163) sl@0: emask|=SSL_kECDH|SSL_aECDSA; sl@0: } sl@0: } sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDSA sl@0: if (ecdsa_ok) sl@0: { sl@0: mask|=SSL_aECDSA; sl@0: emask|=SSL_aECDSA; sl@0: } sl@0: #endif sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_ECDH sl@0: if (have_ecdh_tmp) sl@0: { sl@0: mask|=SSL_kECDHE; sl@0: emask|=SSL_kECDHE; sl@0: } sl@0: #endif sl@0: c->mask=mask; sl@0: c->export_mask=emask; sl@0: c->valid=1; sl@0: } sl@0: sl@0: /* This handy macro borrowed from crypto/x509v3/v3_purp.c */ sl@0: #define ku_reject(x, usage) \ sl@0: (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage))) sl@0: sl@0: int check_srvr_ecc_cert_and_alg(X509 *x, SSL_CIPHER *cs) sl@0: { sl@0: unsigned long alg = cs->algorithms; sl@0: EVP_PKEY *pkey = NULL; sl@0: int keysize = 0; sl@0: int signature_nid = 0; sl@0: sl@0: if (SSL_C_IS_EXPORT(cs)) sl@0: { sl@0: /* ECDH key length in export ciphers must be <= 163 bits */ sl@0: pkey = X509_get_pubkey(x); sl@0: if (pkey == NULL) return 0; sl@0: keysize = EVP_PKEY_bits(pkey); sl@0: EVP_PKEY_free(pkey); sl@0: if (keysize > 163) return 0; sl@0: } sl@0: sl@0: /* This call populates the ex_flags field correctly */ sl@0: X509_check_purpose(x, -1, 0); sl@0: if ((x->sig_alg) && (x->sig_alg->algorithm)) sl@0: signature_nid = OBJ_obj2nid(x->sig_alg->algorithm); sl@0: if (alg & SSL_kECDH) sl@0: { sl@0: /* key usage, if present, must allow key agreement */ sl@0: if (ku_reject(x, X509v3_KU_KEY_AGREEMENT)) sl@0: { sl@0: return 0; sl@0: } sl@0: if (alg & SSL_aECDSA) sl@0: { sl@0: /* signature alg must be ECDSA */ sl@0: if (signature_nid != NID_ecdsa_with_SHA1) sl@0: { sl@0: return 0; sl@0: } sl@0: } sl@0: if (alg & SSL_aRSA) sl@0: { sl@0: /* signature alg must be RSA */ sl@0: if ((signature_nid != NID_md5WithRSAEncryption) && sl@0: (signature_nid != NID_md4WithRSAEncryption) && sl@0: (signature_nid != NID_md2WithRSAEncryption)) sl@0: { sl@0: return 0; sl@0: } sl@0: } sl@0: } sl@0: else if (alg & SSL_aECDSA) sl@0: { sl@0: /* key usage, if present, must allow signing */ sl@0: if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) sl@0: { sl@0: return 0; sl@0: } sl@0: } sl@0: sl@0: return 1; /* all checks are ok */ sl@0: } sl@0: sl@0: /* THIS NEEDS CLEANING UP */ sl@0: X509 *ssl_get_server_send_cert(SSL *s) sl@0: { sl@0: unsigned long alg,mask,kalg; sl@0: CERT *c; sl@0: int i,is_export; sl@0: sl@0: c=s->cert; sl@0: ssl_set_cert_masks(c, s->s3->tmp.new_cipher); sl@0: alg=s->s3->tmp.new_cipher->algorithms; sl@0: is_export=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher); sl@0: mask=is_export?c->export_mask:c->mask; sl@0: kalg=alg&(SSL_MKEY_MASK|SSL_AUTH_MASK); sl@0: sl@0: if (kalg & SSL_kECDH) sl@0: { sl@0: /* we don't need to look at SSL_kECDHE sl@0: * since no certificate is needed for sl@0: * anon ECDH and for authenticated sl@0: * ECDHE, the check for the auth sl@0: * algorithm will set i correctly sl@0: * NOTE: For ECDH-RSA, we need an ECC sl@0: * not an RSA cert but for ECDHE-RSA sl@0: * we need an RSA cert. Placing the sl@0: * checks for SSL_kECDH before RSA sl@0: * checks ensures the correct cert is chosen. sl@0: */ sl@0: i=SSL_PKEY_ECC; sl@0: } sl@0: else if (kalg & SSL_aECDSA) sl@0: { sl@0: i=SSL_PKEY_ECC; sl@0: } sl@0: else if (kalg & SSL_kDHr) sl@0: i=SSL_PKEY_DH_RSA; sl@0: else if (kalg & SSL_kDHd) sl@0: i=SSL_PKEY_DH_DSA; sl@0: else if (kalg & SSL_aDSS) sl@0: i=SSL_PKEY_DSA_SIGN; sl@0: else if (kalg & SSL_aRSA) sl@0: { sl@0: if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL) sl@0: i=SSL_PKEY_RSA_SIGN; sl@0: else sl@0: i=SSL_PKEY_RSA_ENC; sl@0: } sl@0: else if (kalg & SSL_aKRB5) sl@0: { sl@0: /* VRS something else here? */ sl@0: return(NULL); sl@0: } sl@0: else /* if (kalg & SSL_aNULL) */ sl@0: { sl@0: SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT,ERR_R_INTERNAL_ERROR); sl@0: return(NULL); sl@0: } sl@0: if (c->pkeys[i].x509 == NULL) return(NULL); sl@0: sl@0: return(c->pkeys[i].x509); sl@0: } sl@0: sl@0: EVP_PKEY *ssl_get_sign_pkey(SSL *s,SSL_CIPHER *cipher) sl@0: { sl@0: unsigned long alg; sl@0: CERT *c; sl@0: sl@0: alg=cipher->algorithms; sl@0: c=s->cert; sl@0: sl@0: if ((alg & SSL_aDSS) && sl@0: (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL)) sl@0: return(c->pkeys[SSL_PKEY_DSA_SIGN].privatekey); sl@0: else if (alg & SSL_aRSA) sl@0: { sl@0: if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) sl@0: return(c->pkeys[SSL_PKEY_RSA_SIGN].privatekey); sl@0: else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) sl@0: return(c->pkeys[SSL_PKEY_RSA_ENC].privatekey); sl@0: else sl@0: return(NULL); sl@0: } sl@0: else if ((alg & SSL_aECDSA) && sl@0: (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) sl@0: return(c->pkeys[SSL_PKEY_ECC].privatekey); sl@0: else /* if (alg & SSL_aNULL) */ sl@0: { sl@0: SSLerr(SSL_F_SSL_GET_SIGN_PKEY,ERR_R_INTERNAL_ERROR); sl@0: return(NULL); sl@0: } sl@0: } sl@0: sl@0: void ssl_update_cache(SSL *s,int mode) sl@0: { sl@0: int i; sl@0: sl@0: /* If the session_id_length is 0, we are not supposed to cache it, sl@0: * and it would be rather hard to do anyway :-) */ sl@0: if (s->session->session_id_length == 0) return; sl@0: sl@0: i=s->ctx->session_cache_mode; sl@0: if ((i & mode) && (!s->hit) sl@0: && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) sl@0: || SSL_CTX_add_session(s->ctx,s->session)) sl@0: && (s->ctx->new_session_cb != NULL)) sl@0: { sl@0: CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION); sl@0: if (!s->ctx->new_session_cb(s,s->session)) sl@0: SSL_SESSION_free(s->session); sl@0: } sl@0: sl@0: /* auto flush every 255 connections */ sl@0: if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && sl@0: ((i & mode) == mode)) sl@0: { sl@0: if ( (((mode & SSL_SESS_CACHE_CLIENT) sl@0: ?s->ctx->stats.sess_connect_good sl@0: :s->ctx->stats.sess_accept_good) & 0xff) == 0xff) sl@0: { sl@0: SSL_CTX_flush_sessions(s->ctx,(unsigned long)time(NULL)); sl@0: } sl@0: } sl@0: } sl@0: sl@0: EXPORT_C SSL_METHOD *SSL_get_ssl_method(SSL *s) sl@0: { sl@0: return(s->method); sl@0: } sl@0: sl@0: EXPORT_C int SSL_set_ssl_method(SSL *s,SSL_METHOD *meth) sl@0: { sl@0: int conn= -1; sl@0: int ret=1; sl@0: sl@0: if (s->method != meth) sl@0: { sl@0: if (s->handshake_func != NULL) sl@0: conn=(s->handshake_func == s->method->ssl_connect); sl@0: sl@0: if (s->method->version == meth->version) sl@0: s->method=meth; sl@0: else sl@0: { sl@0: s->method->ssl_free(s); sl@0: s->method=meth; sl@0: ret=s->method->ssl_new(s); sl@0: } sl@0: sl@0: if (conn == 1) sl@0: s->handshake_func=meth->ssl_connect; sl@0: else if (conn == 0) sl@0: s->handshake_func=meth->ssl_accept; sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C int SSL_get_error(const SSL *s,int i) sl@0: { sl@0: int reason; sl@0: unsigned long l; sl@0: BIO *bio; sl@0: sl@0: if (i > 0) return(SSL_ERROR_NONE); sl@0: sl@0: /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake sl@0: * etc, where we do encode the error */ sl@0: if ((l=ERR_peek_error()) != 0) sl@0: { sl@0: if (ERR_GET_LIB(l) == ERR_LIB_SYS) sl@0: return(SSL_ERROR_SYSCALL); sl@0: else sl@0: return(SSL_ERROR_SSL); sl@0: } sl@0: sl@0: if ((i < 0) && SSL_want_read(s)) sl@0: { sl@0: bio=SSL_get_rbio(s); sl@0: if (BIO_should_read(bio)) sl@0: return(SSL_ERROR_WANT_READ); sl@0: else if (BIO_should_write(bio)) sl@0: /* This one doesn't make too much sense ... We never try sl@0: * to write to the rbio, and an application program where sl@0: * rbio and wbio are separate couldn't even know what it sl@0: * should wait for. sl@0: * However if we ever set s->rwstate incorrectly sl@0: * (so that we have SSL_want_read(s) instead of sl@0: * SSL_want_write(s)) and rbio and wbio *are* the same, sl@0: * this test works around that bug; so it might be safer sl@0: * to keep it. */ sl@0: return(SSL_ERROR_WANT_WRITE); sl@0: else if (BIO_should_io_special(bio)) sl@0: { sl@0: reason=BIO_get_retry_reason(bio); sl@0: if (reason == BIO_RR_CONNECT) sl@0: return(SSL_ERROR_WANT_CONNECT); sl@0: else if (reason == BIO_RR_ACCEPT) sl@0: return(SSL_ERROR_WANT_ACCEPT); sl@0: else sl@0: return(SSL_ERROR_SYSCALL); /* unknown */ sl@0: } sl@0: } sl@0: sl@0: if ((i < 0) && SSL_want_write(s)) sl@0: { sl@0: bio=SSL_get_wbio(s); sl@0: if (BIO_should_write(bio)) sl@0: return(SSL_ERROR_WANT_WRITE); sl@0: else if (BIO_should_read(bio)) sl@0: /* See above (SSL_want_read(s) with BIO_should_write(bio)) */ sl@0: return(SSL_ERROR_WANT_READ); sl@0: else if (BIO_should_io_special(bio)) sl@0: { sl@0: reason=BIO_get_retry_reason(bio); sl@0: if (reason == BIO_RR_CONNECT) sl@0: return(SSL_ERROR_WANT_CONNECT); sl@0: else if (reason == BIO_RR_ACCEPT) sl@0: return(SSL_ERROR_WANT_ACCEPT); sl@0: else sl@0: return(SSL_ERROR_SYSCALL); sl@0: } sl@0: } sl@0: if ((i < 0) && SSL_want_x509_lookup(s)) sl@0: { sl@0: return(SSL_ERROR_WANT_X509_LOOKUP); sl@0: } sl@0: sl@0: if (i == 0) sl@0: { sl@0: if (s->version == SSL2_VERSION) sl@0: { sl@0: /* assume it is the socket being closed */ sl@0: return(SSL_ERROR_ZERO_RETURN); sl@0: } sl@0: else sl@0: { sl@0: if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) && sl@0: (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) sl@0: return(SSL_ERROR_ZERO_RETURN); sl@0: } sl@0: } sl@0: return(SSL_ERROR_SYSCALL); sl@0: } sl@0: sl@0: EXPORT_C int SSL_do_handshake(SSL *s) sl@0: { sl@0: int ret=1; sl@0: sl@0: if (s->handshake_func == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET); sl@0: return(-1); sl@0: } sl@0: sl@0: s->method->ssl_renegotiate_check(s); sl@0: sl@0: if (SSL_in_init(s) || SSL_in_before(s)) sl@0: { sl@0: ret=s->handshake_func(s); sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: /* For the next 2 functions, SSL_clear() sets shutdown and so sl@0: * one of these calls will reset it */ sl@0: EXPORT_C void SSL_set_accept_state(SSL *s) sl@0: { sl@0: s->server=1; sl@0: s->shutdown=0; sl@0: s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE; sl@0: s->handshake_func=s->method->ssl_accept; sl@0: /* clear the current cipher */ sl@0: ssl_clear_cipher_ctx(s); sl@0: } sl@0: sl@0: EXPORT_C void SSL_set_connect_state(SSL *s) sl@0: { sl@0: s->server=0; sl@0: s->shutdown=0; sl@0: s->state=SSL_ST_CONNECT|SSL_ST_BEFORE; sl@0: s->handshake_func=s->method->ssl_connect; sl@0: /* clear the current cipher */ sl@0: ssl_clear_cipher_ctx(s); sl@0: } sl@0: sl@0: int ssl_undefined_function(SSL *s) sl@0: { sl@0: SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); sl@0: return(0); sl@0: } sl@0: sl@0: int ssl_undefined_void_function(void) sl@0: { sl@0: SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); sl@0: return(0); sl@0: } sl@0: sl@0: int ssl_undefined_const_function(const SSL *s) sl@0: { sl@0: SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); sl@0: return(0); sl@0: } sl@0: sl@0: SSL_METHOD *ssl_bad_method(int ver) sl@0: { sl@0: SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); sl@0: return(NULL); sl@0: } sl@0: sl@0: EXPORT_C const char *SSL_get_version(const SSL *s) sl@0: { sl@0: if (s->version == TLS1_VERSION) sl@0: return("TLSv1"); sl@0: else if (s->version == SSL3_VERSION) sl@0: return("SSLv3"); sl@0: else if (s->version == SSL2_VERSION) sl@0: return("SSLv2"); sl@0: else sl@0: return("unknown"); sl@0: } sl@0: sl@0: EXPORT_C SSL *SSL_dup(SSL *s) sl@0: { sl@0: STACK_OF(X509_NAME) *sk; sl@0: X509_NAME *xn; sl@0: SSL *ret; sl@0: int i; sl@0: sl@0: if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL) sl@0: return(NULL); sl@0: sl@0: ret->version = s->version; sl@0: ret->type = s->type; sl@0: ret->method = s->method; sl@0: sl@0: if (s->session != NULL) sl@0: { sl@0: /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */ sl@0: SSL_copy_session_id(ret,s); sl@0: } sl@0: else sl@0: { sl@0: /* No session has been established yet, so we have to expect sl@0: * that s->cert or ret->cert will be changed later -- sl@0: * they should not both point to the same object, sl@0: * and thus we can't use SSL_copy_session_id. */ sl@0: sl@0: ret->method->ssl_free(ret); sl@0: ret->method = s->method; sl@0: ret->method->ssl_new(ret); sl@0: sl@0: if (s->cert != NULL) sl@0: { sl@0: if (ret->cert != NULL) sl@0: { sl@0: ssl_cert_free(ret->cert); sl@0: } sl@0: ret->cert = ssl_cert_dup(s->cert); sl@0: if (ret->cert == NULL) sl@0: goto err; sl@0: } sl@0: sl@0: SSL_set_session_id_context(ret, sl@0: s->sid_ctx, s->sid_ctx_length); sl@0: } sl@0: sl@0: ret->options=s->options; sl@0: ret->mode=s->mode; sl@0: SSL_set_max_cert_list(ret,SSL_get_max_cert_list(s)); sl@0: SSL_set_read_ahead(ret,SSL_get_read_ahead(s)); sl@0: ret->msg_callback = s->msg_callback; sl@0: ret->msg_callback_arg = s->msg_callback_arg; sl@0: SSL_set_verify(ret,SSL_get_verify_mode(s), sl@0: SSL_get_verify_callback(s)); sl@0: SSL_set_verify_depth(ret,SSL_get_verify_depth(s)); sl@0: ret->generate_session_id = s->generate_session_id; sl@0: sl@0: SSL_set_info_callback(ret,SSL_get_info_callback(s)); sl@0: sl@0: ret->debug=s->debug; sl@0: sl@0: /* copy app data, a little dangerous perhaps */ sl@0: if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data)) sl@0: goto err; sl@0: sl@0: /* setup rbio, and wbio */ sl@0: if (s->rbio != NULL) sl@0: { sl@0: if (!BIO_dup_state(s->rbio,(char *)&ret->rbio)) sl@0: goto err; sl@0: } sl@0: if (s->wbio != NULL) sl@0: { sl@0: if (s->wbio != s->rbio) sl@0: { sl@0: if (!BIO_dup_state(s->wbio,(char *)&ret->wbio)) sl@0: goto err; sl@0: } sl@0: else sl@0: ret->wbio=ret->rbio; sl@0: } sl@0: ret->rwstate = s->rwstate; sl@0: ret->in_handshake = s->in_handshake; sl@0: ret->handshake_func = s->handshake_func; sl@0: ret->server = s->server; sl@0: ret->new_session = s->new_session; sl@0: ret->quiet_shutdown = s->quiet_shutdown; sl@0: ret->shutdown=s->shutdown; sl@0: ret->state=s->state; /* SSL_dup does not really work at any state, though */ sl@0: ret->rstate=s->rstate; sl@0: ret->init_num = 0; /* would have to copy ret->init_buf, ret->init_msg, ret->init_num, ret->init_off */ sl@0: ret->hit=s->hit; sl@0: sl@0: X509_VERIFY_PARAM_inherit(ret->param, s->param); sl@0: sl@0: /* dup the cipher_list and cipher_list_by_id stacks */ sl@0: if (s->cipher_list != NULL) sl@0: { sl@0: if ((ret->cipher_list=sk_SSL_CIPHER_dup(s->cipher_list)) == NULL) sl@0: goto err; sl@0: } sl@0: if (s->cipher_list_by_id != NULL) sl@0: if ((ret->cipher_list_by_id=sk_SSL_CIPHER_dup(s->cipher_list_by_id)) sl@0: == NULL) sl@0: goto err; sl@0: sl@0: /* Dup the client_CA list */ sl@0: if (s->client_CA != NULL) sl@0: { sl@0: if ((sk=sk_X509_NAME_dup(s->client_CA)) == NULL) goto err; sl@0: ret->client_CA=sk; sl@0: for (i=0; ienc_read_ctx != NULL) sl@0: { sl@0: EVP_CIPHER_CTX_cleanup(s->enc_read_ctx); sl@0: OPENSSL_free(s->enc_read_ctx); sl@0: s->enc_read_ctx=NULL; sl@0: } sl@0: if (s->enc_write_ctx != NULL) sl@0: { sl@0: EVP_CIPHER_CTX_cleanup(s->enc_write_ctx); sl@0: OPENSSL_free(s->enc_write_ctx); sl@0: s->enc_write_ctx=NULL; sl@0: } sl@0: #ifndef OPENSSL_NO_COMP sl@0: if (s->expand != NULL) sl@0: { sl@0: COMP_CTX_free(s->expand); sl@0: s->expand=NULL; sl@0: } sl@0: if (s->compress != NULL) sl@0: { sl@0: COMP_CTX_free(s->compress); sl@0: s->compress=NULL; sl@0: } sl@0: #endif sl@0: } sl@0: sl@0: /* Fix this function so that it takes an optional type parameter */ sl@0: EXPORT_C X509 *SSL_get_certificate(const SSL *s) sl@0: { sl@0: if (s->cert != NULL) sl@0: return(s->cert->key->x509); sl@0: else sl@0: return(NULL); sl@0: } sl@0: sl@0: /* Fix this function so that it takes an optional type parameter */ sl@0: EXPORT_C EVP_PKEY *SSL_get_privatekey(SSL *s) sl@0: { sl@0: if (s->cert != NULL) sl@0: return(s->cert->key->privatekey); sl@0: else sl@0: return(NULL); sl@0: } sl@0: sl@0: EXPORT_C SSL_CIPHER *SSL_get_current_cipher(const SSL *s) sl@0: { sl@0: if ((s->session != NULL) && (s->session->cipher != NULL)) sl@0: return(s->session->cipher); sl@0: return(NULL); sl@0: } sl@0: #ifdef OPENSSL_NO_COMP sl@0: EXPORT_C const void *SSL_get_current_compression(SSL *s) sl@0: { sl@0: return NULL; sl@0: } sl@0: EXPORT_C const void *SSL_get_current_expansion(SSL *s) sl@0: { sl@0: return NULL; sl@0: } sl@0: #else sl@0: sl@0: EXPORT_C const COMP_METHOD *SSL_get_current_compression(SSL *s) sl@0: { sl@0: if (s->compress != NULL) sl@0: return(s->compress->meth); sl@0: return(NULL); sl@0: } sl@0: sl@0: EXPORT_C const COMP_METHOD *SSL_get_current_expansion(SSL *s) sl@0: { sl@0: if (s->expand != NULL) sl@0: return(s->expand->meth); sl@0: return(NULL); sl@0: } sl@0: #endif sl@0: sl@0: int ssl_init_wbio_buffer(SSL *s,int push) sl@0: { sl@0: BIO *bbio; sl@0: sl@0: if (s->bbio == NULL) sl@0: { sl@0: bbio=BIO_new(BIO_f_buffer()); sl@0: if (bbio == NULL) return(0); sl@0: s->bbio=bbio; sl@0: } sl@0: else sl@0: { sl@0: bbio=s->bbio; sl@0: if (s->bbio == s->wbio) sl@0: s->wbio=BIO_pop(s->wbio); sl@0: } sl@0: (void)BIO_reset(bbio); sl@0: /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */ sl@0: if (!BIO_set_read_buffer_size(bbio,1)) sl@0: { sl@0: SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB); sl@0: return(0); sl@0: } sl@0: if (push) sl@0: { sl@0: if (s->wbio != bbio) sl@0: s->wbio=BIO_push(bbio,s->wbio); sl@0: } sl@0: else sl@0: { sl@0: if (s->wbio == bbio) sl@0: s->wbio=BIO_pop(bbio); sl@0: } sl@0: return(1); sl@0: } sl@0: sl@0: void ssl_free_wbio_buffer(SSL *s) sl@0: { sl@0: if (s->bbio == NULL) return; sl@0: sl@0: if (s->bbio == s->wbio) sl@0: { sl@0: /* remove buffering */ sl@0: s->wbio=BIO_pop(s->wbio); sl@0: #ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */ sl@0: assert(s->wbio != NULL); sl@0: #endif sl@0: } sl@0: BIO_free(s->bbio); sl@0: s->bbio=NULL; sl@0: } sl@0: sl@0: EXPORT_C void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode) sl@0: { sl@0: ctx->quiet_shutdown=mode; sl@0: } sl@0: sl@0: EXPORT_C int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) sl@0: { sl@0: return(ctx->quiet_shutdown); sl@0: } sl@0: sl@0: EXPORT_C void SSL_set_quiet_shutdown(SSL *s,int mode) sl@0: { sl@0: s->quiet_shutdown=mode; sl@0: } sl@0: sl@0: EXPORT_C int SSL_get_quiet_shutdown(const SSL *s) sl@0: { sl@0: return(s->quiet_shutdown); sl@0: } sl@0: sl@0: EXPORT_C void SSL_set_shutdown(SSL *s,int mode) sl@0: { sl@0: s->shutdown=mode; sl@0: } sl@0: sl@0: EXPORT_C int SSL_get_shutdown(const SSL *s) sl@0: { sl@0: return(s->shutdown); sl@0: } sl@0: sl@0: EXPORT_C int SSL_version(const SSL *s) sl@0: { sl@0: return(s->version); sl@0: } sl@0: sl@0: EXPORT_C SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) sl@0: { sl@0: return(ssl->ctx); sl@0: } sl@0: sl@0: EXPORT_C SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx) sl@0: { sl@0: if (ssl->ctx == ctx) sl@0: return ssl->ctx; sl@0: sl@0: if (ssl->cert != NULL) sl@0: ssl_cert_free(ssl->cert); sl@0: ssl->cert = ssl_cert_dup(ctx->cert); sl@0: CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX); sl@0: if (ssl->ctx != NULL) sl@0: SSL_CTX_free(ssl->ctx); /* decrement reference count */ sl@0: ssl->ctx = ctx; sl@0: return(ssl->ctx); sl@0: } sl@0: #ifndef OPENSSL_NO_STDIO sl@0: EXPORT_C int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) sl@0: { sl@0: return(X509_STORE_set_default_paths(ctx->cert_store)); sl@0: } sl@0: sl@0: EXPORT_C int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile, sl@0: const char *CApath) sl@0: { sl@0: return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath)); sl@0: } sl@0: #endif sl@0: sl@0: EXPORT_C void SSL_set_info_callback(SSL *ssl, sl@0: void (*cb)(const SSL *ssl,int type,int val)) sl@0: { sl@0: ssl->info_callback=cb; sl@0: } sl@0: sl@0: /* One compiler (Diab DCC) doesn't like argument names in returned sl@0: function pointer. */ sl@0: EXPORT_C void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/) sl@0: { sl@0: return ssl->info_callback; sl@0: } sl@0: sl@0: EXPORT_C int SSL_state(const SSL *ssl) sl@0: { sl@0: return(ssl->state); sl@0: } sl@0: sl@0: EXPORT_C void SSL_set_verify_result(SSL *ssl,long arg) sl@0: { sl@0: ssl->verify_result=arg; sl@0: } sl@0: sl@0: EXPORT_C long SSL_get_verify_result(const SSL *ssl) sl@0: { sl@0: return(ssl->verify_result); sl@0: } sl@0: sl@0: EXPORT_C int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func, sl@0: CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func) sl@0: { sl@0: return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, sl@0: new_func, dup_func, free_func); sl@0: } sl@0: sl@0: EXPORT_C int SSL_set_ex_data(SSL *s,int idx,void *arg) sl@0: { sl@0: return(CRYPTO_set_ex_data(&s->ex_data,idx,arg)); sl@0: } sl@0: sl@0: EXPORT_C void *SSL_get_ex_data(const SSL *s,int idx) sl@0: { sl@0: return(CRYPTO_get_ex_data(&s->ex_data,idx)); sl@0: } sl@0: sl@0: EXPORT_C int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func, sl@0: CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func) sl@0: { sl@0: return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, sl@0: new_func, dup_func, free_func); sl@0: } sl@0: sl@0: EXPORT_C int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg) sl@0: { sl@0: return(CRYPTO_set_ex_data(&s->ex_data,idx,arg)); sl@0: } sl@0: sl@0: EXPORT_C void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx) sl@0: { sl@0: return(CRYPTO_get_ex_data(&s->ex_data,idx)); sl@0: } sl@0: sl@0: int ssl_ok(SSL *s) sl@0: { sl@0: return(1); sl@0: } sl@0: sl@0: EXPORT_C X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) sl@0: { sl@0: return(ctx->cert_store); sl@0: } sl@0: sl@0: EXPORT_C void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store) sl@0: { sl@0: if (ctx->cert_store != NULL) sl@0: X509_STORE_free(ctx->cert_store); sl@0: ctx->cert_store=store; sl@0: } sl@0: sl@0: EXPORT_C int SSL_want(const SSL *s) sl@0: { sl@0: return(s->rwstate); sl@0: } sl@0: sl@0: /*! sl@0: * \brief Set the callback for generating temporary RSA keys. sl@0: * \param ctx the SSL context. sl@0: * \param cb the callback sl@0: */ sl@0: sl@0: #ifndef OPENSSL_NO_RSA sl@0: EXPORT_C void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl, sl@0: int is_export, sl@0: int keylength)) sl@0: { sl@0: SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); sl@0: } sl@0: sl@0: EXPORT_C void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl, sl@0: int is_export, sl@0: int keylength)) sl@0: { sl@0: SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); sl@0: } sl@0: #endif sl@0: sl@0: #ifdef DOXYGEN sl@0: /*! sl@0: * \brief The RSA temporary key callback function. sl@0: * \param ssl the SSL session. sl@0: * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite. sl@0: * \param keylength if \c is_export is \c TRUE, then \c keylength is the size sl@0: * of the required key in bits. sl@0: * \return the temporary RSA key. sl@0: * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback sl@0: */ sl@0: sl@0: RSA *cb(SSL *ssl,int is_export,int keylength) sl@0: {} sl@0: #endif sl@0: sl@0: /*! sl@0: * \brief Set the callback for generating temporary DH keys. sl@0: * \param ctx the SSL context. sl@0: * \param dh the callback sl@0: */ sl@0: sl@0: #ifndef OPENSSL_NO_DH sl@0: EXPORT_C void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export, sl@0: int keylength)) sl@0: { sl@0: SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); sl@0: } sl@0: sl@0: EXPORT_C void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export, sl@0: int keylength)) sl@0: { sl@0: SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); sl@0: } sl@0: #endif sl@0: sl@0: #ifndef OPENSSL_NO_ECDH sl@0: EXPORT_C void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export, sl@0: int keylength)) sl@0: { sl@0: SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh); sl@0: } sl@0: sl@0: EXPORT_C void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export, sl@0: int keylength)) sl@0: { sl@0: SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh); sl@0: } sl@0: #endif sl@0: sl@0: sl@0: EXPORT_C void SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)) sl@0: { sl@0: SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); sl@0: } sl@0: EXPORT_C void SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)) sl@0: { sl@0: SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); sl@0: } sl@0: sl@0: sl@0: sl@0: #if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16) sl@0: #include "../crypto/bio/bss_file.c" sl@0: #endif sl@0: sl@0: IMPLEMENT_STACK_OF(SSL_CIPHER) sl@0: IMPLEMENT_STACK_OF(SSL_COMP)