⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ssl_lib.c

📁 mediastreamer2是开源的网络传输媒体流的库
💻 C
📖 第 1 页 / 共 5 页
字号:
/*! \file ssl/ssl_lib.c *  \brief Version independent SSL functions. *//* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. *  * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to.  The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code.  The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). *  * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: *    "This product includes cryptographic software written by *     Eric Young (eay@cryptsoft.com)" *    The word 'cryptographic' can be left out if the rouines from the library *    being used are not cryptographic related :-). * 4. If you include any Windows specific code (or a derivative thereof) from  *    the apps directory (application code) you must include an acknowledgement: *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" *  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. *  * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed.  i.e. this code cannot simply be * copied and put under another distribution licence * [including the GNU Public Licence.] *//* ==================================================================== * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer.  * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. All advertising materials mentioning features or use of this *    software must display the following acknowledgment: *    "This product includes software developed by the OpenSSL Project *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)" * * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to *    endorse or promote products derived from this software without *    prior written permission. For written permission, please contact *    openssl-core@openssl.org. * * 5. Products derived from this software may not be called "OpenSSL" *    nor may "OpenSSL" appear in their names without prior written *    permission of the OpenSSL Project. * * 6. Redistributions of any form whatsoever must retain the following *    acknowledgment: *    "This product includes software developed by the OpenSSL Project *    for use in the OpenSSL Toolkit (http://www.openssl.org/)" * * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This product includes cryptographic software written by Eric Young * (eay@cryptsoft.com).  This product includes software written by Tim * Hudson (tjh@cryptsoft.com). * *//* ==================================================================== * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. * ECC cipher suite support in OpenSSL originally developed by  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */#ifdef REF_CHECK#  include <assert.h>#endif#include <stdio.h>#include "ssl_locl.h"#include "kssl_lcl.h"#include <openssl/objects.h>#include <openssl/lhash.h>#include <openssl/x509v3.h>#ifndef OPENSSL_NO_DH#include <openssl/dh.h>#endifconst char *SSL_version_str=OPENSSL_VERSION_TEXT;SSL3_ENC_METHOD ssl3_undef_enc_method={	/* evil casts, but these functions are only called if there's a library bug */	(int (*)(SSL *,int))ssl_undefined_function,	(int (*)(SSL *, unsigned char *, int))ssl_undefined_function,	ssl_undefined_function,	(int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function,	(int (*)(SSL*, int))ssl_undefined_function,	(int (*)(SSL *, EVP_MD_CTX *, EVP_MD_CTX *, const char*, int, unsigned char *))ssl_undefined_function,	0,	/* finish_mac_length */	(int (*)(SSL *, EVP_MD_CTX *, unsigned char *))ssl_undefined_function,	NULL,	/* client_finished_label */	0,	/* client_finished_label_len */	NULL,	/* server_finished_label */	0,	/* server_finished_label_len */	(int (*)(int))ssl_undefined_function	};int SSL_clear(SSL *s)	{	if (s->method == NULL)		{		SSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);		return(0);		}	if (ssl_clear_bad_session(s))		{		SSL_SESSION_free(s->session);		s->session=NULL;		}	s->error=0;	s->hit=0;	s->shutdown=0;#if 0 /* Disabled since version 1.10 of this file (early return not       * needed because SSL_clear is not called when doing renegotiation) */	/* This is set if we are doing dynamic renegotiation so keep	 * the old cipher.  It is sort of a SSL_clear_lite :-) */	if (s->new_session) return(1);#else	if (s->new_session)		{		SSLerr(SSL_F_SSL_CLEAR,ERR_R_INTERNAL_ERROR);		return 0;		}#endif	s->type=0;	s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);	s->version=s->method->version;	s->client_version=s->version;	s->rwstate=SSL_NOTHING;	s->rstate=SSL_ST_READ_HEADER;#if 0	s->read_ahead=s->ctx->read_ahead;#endif	if (s->init_buf != NULL)		{		BUF_MEM_free(s->init_buf);		s->init_buf=NULL;		}	ssl_clear_cipher_ctx(s);	s->first_packet=0;#if 1	/* Check to see if we were changed into a different method, if	 * so, revert back if we are not doing session-id reuse. */	if (!s->in_handshake && (s->session == NULL) && (s->method != s->ctx->method))		{		s->method->ssl_free(s);		s->method=s->ctx->method;		if (!s->method->ssl_new(s))			return(0);		}	else#endif		s->method->ssl_clear(s);	return(1);	}/** Used to change an SSL_CTXs default SSL method type */int SSL_CTX_set_ssl_version(SSL_CTX *ctx,SSL_METHOD *meth)	{	STACK_OF(SSL_CIPHER) *sk;	ctx->method=meth;	sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list),		&(ctx->cipher_list_by_id),SSL_DEFAULT_CIPHER_LIST);	if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))		{		SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);		return(0);		}	return(1);	}SSL *SSL_new(SSL_CTX *ctx)	{	SSL *s;	if (ctx == NULL)		{		SSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);		return(NULL);		}	if (ctx->method == NULL)		{		SSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);		return(NULL);		}	s=(SSL *)OPENSSL_malloc(sizeof(SSL));	if (s == NULL) goto err;	memset(s,0,sizeof(SSL));#ifndef	OPENSSL_NO_KRB5	s->kssl_ctx = kssl_ctx_new();#endif	/* OPENSSL_NO_KRB5 */	s->options=ctx->options;	s->mode=ctx->mode;	s->max_cert_list=ctx->max_cert_list;	if (ctx->cert != NULL)		{		/* Earlier library versions used to copy the pointer to		 * the CERT, not its contents; only when setting new		 * parameters for the per-SSL copy, ssl_cert_new would be		 * called (and the direct reference to the per-SSL_CTX		 * settings would be lost, but those still were indirectly		 * accessed for various purposes, and for that reason they		 * used to be known as s->ctx->default_cert).		 * Now we don't look at the SSL_CTX's CERT after having		 * duplicated it once. */		s->cert = ssl_cert_dup(ctx->cert);		if (s->cert == NULL)			goto err;		}	else		s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */	s->read_ahead=ctx->read_ahead;	s->msg_callback=ctx->msg_callback;	s->msg_callback_arg=ctx->msg_callback_arg;	s->verify_mode=ctx->verify_mode;#if 0	s->verify_depth=ctx->verify_depth;#endif	s->sid_ctx_length=ctx->sid_ctx_length;	OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);	memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));	s->verify_callback=ctx->default_verify_callback;	s->generate_session_id=ctx->generate_session_id;	s->param = X509_VERIFY_PARAM_new();	if (!s->param)		goto err;	X509_VERIFY_PARAM_inherit(s->param, ctx->param);#if 0	s->purpose = ctx->purpose;	s->trust = ctx->trust;#endif	s->quiet_shutdown=ctx->quiet_shutdown;	CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);	s->ctx=ctx;	s->verify_result=X509_V_OK;	s->method=ctx->method;	if (!s->method->ssl_new(s))		goto err;	s->references=1;	s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;	SSL_clear(s);	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);	return(s);err:	if (s != NULL)		{		if (s->cert != NULL)			ssl_cert_free(s->cert);		if (s->ctx != NULL)			SSL_CTX_free(s->ctx); /* decrement reference count */		OPENSSL_free(s);		}	SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);	return(NULL);	}int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,				   unsigned int sid_ctx_len)    {    if(sid_ctx_len > sizeof ctx->sid_ctx)	{	SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);	return 0;	}    ctx->sid_ctx_length=sid_ctx_len;    memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);    return 1;    }int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,			       unsigned int sid_ctx_len)    {    if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)	{	SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);	return 0;	}    ssl->sid_ctx_length=sid_ctx_len;    memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);    return 1;    }int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)	{	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);	ctx->generate_session_id = cb;	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);	return 1;	}int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)	{	CRYPTO_w_lock(CRYPTO_LOCK_SSL);	ssl->generate_session_id = cb;	CRYPTO_w_unlock(CRYPTO_LOCK_SSL);	return 1;	}int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,				unsigned int id_len)	{	/* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how	 * we can "construct" a session to give us the desired check - ie. to	 * find if there's a session in the hash table that would conflict with	 * any new session built out of this id/id_len and the ssl_version in	 * use by this SSL. */	SSL_SESSION r, *p;	if(id_len > sizeof r.session_id)		return 0;	r.ssl_version = ssl->version;	r.session_id_length = id_len;	memcpy(r.session_id, id, id_len);	/* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a	 * callback is calling us to check the uniqueness of a shorter ID, it	 * must be compared as a padded-out ID because that is what it will be	 * converted to when the callback has finished choosing it. */	if((r.ssl_version == SSL2_VERSION) &&			(id_len < SSL2_SSL_SESSION_ID_LENGTH))		{		memset(r.session_id + id_len, 0,			SSL2_SSL_SESSION_ID_LENGTH - id_len);		r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH;		}	CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);	p = (SSL_SESSION *)lh_retrieve(ssl->ctx->sessions, &r);	CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);	return (p != NULL);	}int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)	{	return X509_VERIFY_PARAM_set_purpose(s->param, purpose);	}int SSL_set_purpose(SSL *s, int purpose)	{	return X509_VERIFY_PARAM_set_purpose(s->param, purpose);	}int SSL_CTX_set_trust(SSL_CTX *s, int trust)	{	return X509_VERIFY_PARAM_set_trust(s->param, trust);	}int SSL_set_trust(SSL *s, int trust)	{	return X509_VERIFY_PARAM_set_trust(s->param, trust);	}void SSL_free(SSL *s)	{	int i;	if(s == NULL)	    return;	i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);#ifdef REF_PRINT	REF_PRINT("SSL",s);#endif	if (i > 0) return;#ifdef REF_CHECK	if (i < 0)		{		fprintf(stderr,"SSL_free, bad reference count\n");		abort(); /* ok */		}#endif	if (s->param)		X509_VERIFY_PARAM_free(s->param);	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);	if (s->bbio != NULL)		{		/* If the buffering BIO is in place, pop it off */		if (s->bbio == s->wbio)			{			s->wbio=BIO_pop(s->wbio);			}		BIO_free(s->bbio);		s->bbio=NULL;		}	if (s->rbio != NULL)		BIO_free_all(s->rbio);	if ((s->wbio != NULL) && (s->wbio != s->rbio))		BIO_free_all(s->wbio);	if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);	/* add extra stuff */	if (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list);	if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);	/* Make the next call work :-) */	if (s->session != NULL)		{		ssl_clear_bad_session(s);		SSL_SESSION_free(s->session);		}	ssl_clear_cipher_ctx(s);	if (s->cert != NULL) ssl_cert_free(s->cert);	/* Free up if allocated */	if (s->ctx) SSL_CTX_free(s->ctx);	if (s->client_CA != NULL)		sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);	if (s->method != NULL) s->method->ssl_free(s);#ifndef	OPENSSL_NO_KRB5	if (s->kssl_ctx != NULL)		kssl_ctx_free(s->kssl_ctx);#endif	/* OPENSSL_NO_KRB5 */	OPENSSL_free(s);	}void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)	{	/* If the output buffering BIO is still in place, remove it	 */	if (s->bbio != NULL)		{		if (s->wbio == s->bbio)			{			s->wbio=s->wbio->next_bio;			s->bbio->next_bio=NULL;			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -