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

📄 t1_enc.c

📁 mediastreamer2是开源的网络传输媒体流的库
💻 C
📖 第 1 页 / 共 2 页
字号:
/* ssl/t1_enc.c *//* 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-2002 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). * */#include <stdio.h>#include "ssl_locl.h"#include <openssl/comp.h>#include <openssl/evp.h>#include <openssl/hmac.h>#include <openssl/md5.h>static void tls1_P_hash(const EVP_MD *md, const unsigned char *sec,			int sec_len, unsigned char *seed, int seed_len,			unsigned char *out, int olen)	{	int chunk,n;	unsigned int j;	HMAC_CTX ctx;	HMAC_CTX ctx_tmp;	unsigned char A1[EVP_MAX_MD_SIZE];	unsigned int A1_len;		chunk=EVP_MD_size(md);	HMAC_CTX_init(&ctx);	HMAC_CTX_init(&ctx_tmp);	HMAC_Init_ex(&ctx,sec,sec_len,md, NULL);	HMAC_Init_ex(&ctx_tmp,sec,sec_len,md, NULL);	HMAC_Update(&ctx,seed,seed_len);	HMAC_Final(&ctx,A1,&A1_len);	n=0;	for (;;)		{		HMAC_Init_ex(&ctx,NULL,0,NULL,NULL); /* re-init */		HMAC_Init_ex(&ctx_tmp,NULL,0,NULL,NULL); /* re-init */		HMAC_Update(&ctx,A1,A1_len);		HMAC_Update(&ctx_tmp,A1,A1_len);		HMAC_Update(&ctx,seed,seed_len);		if (olen > chunk)			{			HMAC_Final(&ctx,out,&j);			out+=j;			olen-=j;			HMAC_Final(&ctx_tmp,A1,&A1_len); /* calc the next A1 value */			}		else	/* last one */			{			HMAC_Final(&ctx,A1,&A1_len);			memcpy(out,A1,olen);			break;			}		}	HMAC_CTX_cleanup(&ctx);	HMAC_CTX_cleanup(&ctx_tmp);	OPENSSL_cleanse(A1,sizeof(A1));	}static void tls1_PRF(const EVP_MD *md5, const EVP_MD *sha1,		     unsigned char *label, int label_len,		     const unsigned char *sec, int slen, unsigned char *out1,		     unsigned char *out2, int olen)	{	int len,i;	const unsigned char *S1,*S2;	len=slen/2;	S1=sec;	S2= &(sec[len]);	len+=(slen&1); /* add for odd, make longer */		tls1_P_hash(md5 ,S1,len,label,label_len,out1,olen);	tls1_P_hash(sha1,S2,len,label,label_len,out2,olen);	for (i=0; i<olen; i++)		out1[i]^=out2[i];	}static void tls1_generate_key_block(SSL *s, unsigned char *km,	     unsigned char *tmp, int num)	{	unsigned char *p;	unsigned char buf[SSL3_RANDOM_SIZE*2+		TLS_MD_MAX_CONST_SIZE];	p=buf;	memcpy(p,TLS_MD_KEY_EXPANSION_CONST,		TLS_MD_KEY_EXPANSION_CONST_SIZE);	p+=TLS_MD_KEY_EXPANSION_CONST_SIZE;	memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);	p+=SSL3_RANDOM_SIZE;	memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);	p+=SSL3_RANDOM_SIZE;	tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(p-buf),		 s->session->master_key,s->session->master_key_length,		 km,tmp,num);#ifdef KSSL_DEBUG	printf("tls1_generate_key_block() ==> %d byte master_key =\n\t",                s->session->master_key_length);	{        int i;        for (i=0; i < s->session->master_key_length; i++)                {                printf("%02X", s->session->master_key[i]);                }        printf("\n");  }#endif    /* KSSL_DEBUG */	}int tls1_change_cipher_state(SSL *s, int which)	{	static const unsigned char empty[]="";	unsigned char *p,*key_block,*mac_secret;	unsigned char *exp_label,buf[TLS_MD_MAX_CONST_SIZE+		SSL3_RANDOM_SIZE*2];	unsigned char tmp1[EVP_MAX_KEY_LENGTH];	unsigned char tmp2[EVP_MAX_KEY_LENGTH];	unsigned char iv1[EVP_MAX_IV_LENGTH*2];	unsigned char iv2[EVP_MAX_IV_LENGTH*2];	unsigned char *ms,*key,*iv,*er1,*er2;	int client_write;	EVP_CIPHER_CTX *dd;	const EVP_CIPHER *c;#ifndef OPENSSL_NO_COMP	const SSL_COMP *comp;#endif	const EVP_MD *m;	int is_export,n,i,j,k,exp_label_len,cl;	int reuse_dd = 0;	is_export=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher);	c=s->s3->tmp.new_sym_enc;	m=s->s3->tmp.new_hash;#ifndef OPENSSL_NO_COMP	comp=s->s3->tmp.new_compression;#endif	key_block=s->s3->tmp.key_block;#ifdef KSSL_DEBUG	printf("tls1_change_cipher_state(which= %d) w/\n", which);	printf("\talg= %ld, comp= %p\n", s->s3->tmp.new_cipher->algorithms,                comp);	printf("\tevp_cipher == %p ==? &d_cbc_ede_cipher3\n", c);	printf("\tevp_cipher: nid, blksz= %d, %d, keylen=%d, ivlen=%d\n",                c->nid,c->block_size,c->key_len,c->iv_len);	printf("\tkey_block: len= %d, data= ", s->s3->tmp.key_block_length);	{        int i;        for (i=0; i<s->s3->tmp.key_block_length; i++)		printf("%02x", key_block[i]);  printf("\n");        }#endif	/* KSSL_DEBUG */	if (which & SSL3_CC_READ)		{		if (s->enc_read_ctx != NULL)			reuse_dd = 1;		else if ((s->enc_read_ctx=OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL)			goto err;		dd= s->enc_read_ctx;		s->read_hash=m;#ifndef OPENSSL_NO_COMP		if (s->expand != NULL)			{			COMP_CTX_free(s->expand);			s->expand=NULL;			}		if (comp != NULL)			{			s->expand=COMP_CTX_new(comp->method);			if (s->expand == NULL)				{				SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,SSL_R_COMPRESSION_LIBRARY_ERROR);				goto err2;				}			if (s->s3->rrec.comp == NULL)				s->s3->rrec.comp=(unsigned char *)					OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);			if (s->s3->rrec.comp == NULL)				goto err;			}#endif		/* this is done by dtls1_reset_seq_numbers for DTLS1_VERSION */ 		if (s->version != DTLS1_VERSION)			memset(&(s->s3->read_sequence[0]),0,8);		mac_secret= &(s->s3->read_mac_secret[0]);		}	else		{		if (s->enc_write_ctx != NULL)			reuse_dd = 1;		else if ((s->enc_write_ctx=OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL)			goto err;		if ((s->enc_write_ctx == NULL) &&			((s->enc_write_ctx=(EVP_CIPHER_CTX *)			OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL))			goto err;		dd= s->enc_write_ctx;		s->write_hash=m;#ifndef OPENSSL_NO_COMP		if (s->compress != NULL)			{			COMP_CTX_free(s->compress);			s->compress=NULL;			}		if (comp != NULL)			{			s->compress=COMP_CTX_new(comp->method);			if (s->compress == NULL)				{				SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,SSL_R_COMPRESSION_LIBRARY_ERROR);				goto err2;				}			}#endif		/* this is done by dtls1_reset_seq_numbers for DTLS1_VERSION */ 		if (s->version != DTLS1_VERSION)			memset(&(s->s3->write_sequence[0]),0,8);		mac_secret= &(s->s3->write_mac_secret[0]);		}	if (reuse_dd)		EVP_CIPHER_CTX_cleanup(dd);	EVP_CIPHER_CTX_init(dd);	p=s->s3->tmp.key_block;	i=EVP_MD_size(m);	cl=EVP_CIPHER_key_length(c);	j=is_export ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ?	               cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl;	/* Was j=(exp)?5:EVP_CIPHER_key_length(c); */	k=EVP_CIPHER_iv_length(c);	er1= &(s->s3->client_random[0]);	er2= &(s->s3->server_random[0]);	if (	(which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||		(which == SSL3_CHANGE_CIPHER_SERVER_READ))		{		ms=  &(p[ 0]); n=i+i;		key= &(p[ n]); n+=j+j;		iv=  &(p[ n]); n+=k+k;		exp_label=(unsigned char *)TLS_MD_CLIENT_WRITE_KEY_CONST;		exp_label_len=TLS_MD_CLIENT_WRITE_KEY_CONST_SIZE;		client_write=1;		}	else		{		n=i;		ms=  &(p[ n]); n+=i+j;		key= &(p[ n]); n+=j+k;		iv=  &(p[ n]); n+=k;		exp_label=(unsigned char *)TLS_MD_SERVER_WRITE_KEY_CONST;		exp_label_len=TLS_MD_SERVER_WRITE_KEY_CONST_SIZE;		client_write=0;		}	if (n > s->s3->tmp.key_block_length)		{		SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,ERR_R_INTERNAL_ERROR);		goto err2;		}	memcpy(mac_secret,ms,i);#ifdef TLS_DEBUGprintf("which = %04X\nmac key=",which);{ int z; for (z=0; z<i; z++) printf("%02X%c",ms[z],((z+1)%16)?' ':'\n'); }#endif	if (is_export)		{		/* In here I set both the read and write key/iv to the		 * same value since only the correct one will be used :-).		 */		p=buf;		memcpy(p,exp_label,exp_label_len);		p+=exp_label_len;		memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);		p+=SSL3_RANDOM_SIZE;		memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);		p+=SSL3_RANDOM_SIZE;		tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(p-buf),key,j,			 tmp1,tmp2,EVP_CIPHER_key_length(c));		key=tmp1;		if (k > 0)			{			p=buf;			memcpy(p,TLS_MD_IV_BLOCK_CONST,				TLS_MD_IV_BLOCK_CONST_SIZE);			p+=TLS_MD_IV_BLOCK_CONST_SIZE;			memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);			p+=SSL3_RANDOM_SIZE;			memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);			p+=SSL3_RANDOM_SIZE;			tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,p-buf,empty,0,				 iv1,iv2,k*2);			if (client_write)				iv=iv1;			else				iv= &(iv1[k]);			}		}	s->session->key_arg_length=0;#ifdef KSSL_DEBUG	{        int i;	printf("EVP_CipherInit_ex(dd,c,key=,iv=,which)\n");

⌨️ 快捷键说明

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