xref: /onnv-gate/usr/src/common/openssl/ssl/t1_enc.c (revision 5434:d0b14f9f9750)
10Sstevel@tonic-gate /* ssl/t1_enc.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate /* ====================================================================
590Sstevel@tonic-gate  * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
620Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
630Sstevel@tonic-gate  * are met:
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
660Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
690Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
700Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
710Sstevel@tonic-gate  *    distribution.
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
740Sstevel@tonic-gate  *    software must display the following acknowledgment:
750Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
760Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
790Sstevel@tonic-gate  *    endorse or promote products derived from this software without
800Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
810Sstevel@tonic-gate  *    openssl-core@openssl.org.
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
840Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
850Sstevel@tonic-gate  *    permission of the OpenSSL Project.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
880Sstevel@tonic-gate  *    acknowledgment:
890Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
900Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
930Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
940Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
950Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
960Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
970Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
980Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
990Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1000Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1010Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1020Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1030Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
1040Sstevel@tonic-gate  * ====================================================================
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
1070Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
1080Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate #include <stdio.h>
1130Sstevel@tonic-gate #include "ssl_locl.h"
1140Sstevel@tonic-gate #include <openssl/comp.h>
1150Sstevel@tonic-gate #include <openssl/evp.h>
1160Sstevel@tonic-gate #include <openssl/hmac.h>
1170Sstevel@tonic-gate #include <openssl/md5.h>
1180Sstevel@tonic-gate 
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)1190Sstevel@tonic-gate static void tls1_P_hash(const EVP_MD *md, const unsigned char *sec,
1200Sstevel@tonic-gate 			int sec_len, unsigned char *seed, int seed_len,
1210Sstevel@tonic-gate 			unsigned char *out, int olen)
1220Sstevel@tonic-gate 	{
1230Sstevel@tonic-gate 	int chunk,n;
1240Sstevel@tonic-gate 	unsigned int j;
1250Sstevel@tonic-gate 	HMAC_CTX ctx;
1260Sstevel@tonic-gate 	HMAC_CTX ctx_tmp;
1270Sstevel@tonic-gate 	unsigned char A1[EVP_MAX_MD_SIZE];
1280Sstevel@tonic-gate 	unsigned int A1_len;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	chunk=EVP_MD_size(md);
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	HMAC_CTX_init(&ctx);
1330Sstevel@tonic-gate 	HMAC_CTX_init(&ctx_tmp);
1340Sstevel@tonic-gate 	HMAC_Init_ex(&ctx,sec,sec_len,md, NULL);
1350Sstevel@tonic-gate 	HMAC_Init_ex(&ctx_tmp,sec,sec_len,md, NULL);
1360Sstevel@tonic-gate 	HMAC_Update(&ctx,seed,seed_len);
1370Sstevel@tonic-gate 	HMAC_Final(&ctx,A1,&A1_len);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	n=0;
1400Sstevel@tonic-gate 	for (;;)
1410Sstevel@tonic-gate 		{
1420Sstevel@tonic-gate 		HMAC_Init_ex(&ctx,NULL,0,NULL,NULL); /* re-init */
1430Sstevel@tonic-gate 		HMAC_Init_ex(&ctx_tmp,NULL,0,NULL,NULL); /* re-init */
1440Sstevel@tonic-gate 		HMAC_Update(&ctx,A1,A1_len);
1450Sstevel@tonic-gate 		HMAC_Update(&ctx_tmp,A1,A1_len);
1460Sstevel@tonic-gate 		HMAC_Update(&ctx,seed,seed_len);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 		if (olen > chunk)
1490Sstevel@tonic-gate 			{
1500Sstevel@tonic-gate 			HMAC_Final(&ctx,out,&j);
1510Sstevel@tonic-gate 			out+=j;
1520Sstevel@tonic-gate 			olen-=j;
1530Sstevel@tonic-gate 			HMAC_Final(&ctx_tmp,A1,&A1_len); /* calc the next A1 value */
1540Sstevel@tonic-gate 			}
1550Sstevel@tonic-gate 		else	/* last one */
1560Sstevel@tonic-gate 			{
1570Sstevel@tonic-gate 			HMAC_Final(&ctx,A1,&A1_len);
1580Sstevel@tonic-gate 			memcpy(out,A1,olen);
1590Sstevel@tonic-gate 			break;
1600Sstevel@tonic-gate 			}
1610Sstevel@tonic-gate 		}
1620Sstevel@tonic-gate 	HMAC_CTX_cleanup(&ctx);
1630Sstevel@tonic-gate 	HMAC_CTX_cleanup(&ctx_tmp);
1640Sstevel@tonic-gate 	OPENSSL_cleanse(A1,sizeof(A1));
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 
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)1670Sstevel@tonic-gate static void tls1_PRF(const EVP_MD *md5, const EVP_MD *sha1,
1680Sstevel@tonic-gate 		     unsigned char *label, int label_len,
1690Sstevel@tonic-gate 		     const unsigned char *sec, int slen, unsigned char *out1,
1700Sstevel@tonic-gate 		     unsigned char *out2, int olen)
1710Sstevel@tonic-gate 	{
1720Sstevel@tonic-gate 	int len,i;
1730Sstevel@tonic-gate 	const unsigned char *S1,*S2;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	len=slen/2;
1760Sstevel@tonic-gate 	S1=sec;
1770Sstevel@tonic-gate 	S2= &(sec[len]);
1780Sstevel@tonic-gate 	len+=(slen&1); /* add for odd, make longer */
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	tls1_P_hash(md5 ,S1,len,label,label_len,out1,olen);
1820Sstevel@tonic-gate 	tls1_P_hash(sha1,S2,len,label,label_len,out2,olen);
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	for (i=0; i<olen; i++)
1850Sstevel@tonic-gate 		out1[i]^=out2[i];
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 
tls1_generate_key_block(SSL * s,unsigned char * km,unsigned char * tmp,int num)1880Sstevel@tonic-gate static void tls1_generate_key_block(SSL *s, unsigned char *km,
1890Sstevel@tonic-gate 	     unsigned char *tmp, int num)
1900Sstevel@tonic-gate 	{
1910Sstevel@tonic-gate 	unsigned char *p;
1920Sstevel@tonic-gate 	unsigned char buf[SSL3_RANDOM_SIZE*2+
1930Sstevel@tonic-gate 		TLS_MD_MAX_CONST_SIZE];
1940Sstevel@tonic-gate 	p=buf;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	memcpy(p,TLS_MD_KEY_EXPANSION_CONST,
1970Sstevel@tonic-gate 		TLS_MD_KEY_EXPANSION_CONST_SIZE);
1980Sstevel@tonic-gate 	p+=TLS_MD_KEY_EXPANSION_CONST_SIZE;
1990Sstevel@tonic-gate 	memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
2000Sstevel@tonic-gate 	p+=SSL3_RANDOM_SIZE;
2010Sstevel@tonic-gate 	memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
2020Sstevel@tonic-gate 	p+=SSL3_RANDOM_SIZE;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(p-buf),
2050Sstevel@tonic-gate 		 s->session->master_key,s->session->master_key_length,
2060Sstevel@tonic-gate 		 km,tmp,num);
2070Sstevel@tonic-gate #ifdef KSSL_DEBUG
2080Sstevel@tonic-gate 	printf("tls1_generate_key_block() ==> %d byte master_key =\n\t",
2090Sstevel@tonic-gate                 s->session->master_key_length);
2100Sstevel@tonic-gate 	{
2110Sstevel@tonic-gate         int i;
2120Sstevel@tonic-gate         for (i=0; i < s->session->master_key_length; i++)
2130Sstevel@tonic-gate                 {
2140Sstevel@tonic-gate                 printf("%02X", s->session->master_key[i]);
2150Sstevel@tonic-gate                 }
2160Sstevel@tonic-gate         printf("\n");  }
2170Sstevel@tonic-gate #endif    /* KSSL_DEBUG */
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 
tls1_change_cipher_state(SSL * s,int which)2200Sstevel@tonic-gate int tls1_change_cipher_state(SSL *s, int which)
2210Sstevel@tonic-gate 	{
2220Sstevel@tonic-gate 	static const unsigned char empty[]="";
2230Sstevel@tonic-gate 	unsigned char *p,*key_block,*mac_secret;
2240Sstevel@tonic-gate 	unsigned char *exp_label,buf[TLS_MD_MAX_CONST_SIZE+
2250Sstevel@tonic-gate 		SSL3_RANDOM_SIZE*2];
2260Sstevel@tonic-gate 	unsigned char tmp1[EVP_MAX_KEY_LENGTH];
2270Sstevel@tonic-gate 	unsigned char tmp2[EVP_MAX_KEY_LENGTH];
2280Sstevel@tonic-gate 	unsigned char iv1[EVP_MAX_IV_LENGTH*2];
2290Sstevel@tonic-gate 	unsigned char iv2[EVP_MAX_IV_LENGTH*2];
2300Sstevel@tonic-gate 	unsigned char *ms,*key,*iv,*er1,*er2;
2310Sstevel@tonic-gate 	int client_write;
2320Sstevel@tonic-gate 	EVP_CIPHER_CTX *dd;
2330Sstevel@tonic-gate 	const EVP_CIPHER *c;
2342139Sjp161948 #ifndef OPENSSL_NO_COMP
2350Sstevel@tonic-gate 	const SSL_COMP *comp;
2362139Sjp161948 #endif
2370Sstevel@tonic-gate 	const EVP_MD *m;
2380Sstevel@tonic-gate 	int is_export,n,i,j,k,exp_label_len,cl;
2390Sstevel@tonic-gate 	int reuse_dd = 0;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	is_export=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher);
2420Sstevel@tonic-gate 	c=s->s3->tmp.new_sym_enc;
2430Sstevel@tonic-gate 	m=s->s3->tmp.new_hash;
2442139Sjp161948 #ifndef OPENSSL_NO_COMP
2450Sstevel@tonic-gate 	comp=s->s3->tmp.new_compression;
2462139Sjp161948 #endif
2470Sstevel@tonic-gate 	key_block=s->s3->tmp.key_block;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate #ifdef KSSL_DEBUG
2500Sstevel@tonic-gate 	printf("tls1_change_cipher_state(which= %d) w/\n", which);
2510Sstevel@tonic-gate 	printf("\talg= %ld, comp= %p\n", s->s3->tmp.new_cipher->algorithms,
2520Sstevel@tonic-gate                 comp);
2530Sstevel@tonic-gate 	printf("\tevp_cipher == %p ==? &d_cbc_ede_cipher3\n", c);
2540Sstevel@tonic-gate 	printf("\tevp_cipher: nid, blksz= %d, %d, keylen=%d, ivlen=%d\n",
2550Sstevel@tonic-gate                 c->nid,c->block_size,c->key_len,c->iv_len);
2560Sstevel@tonic-gate 	printf("\tkey_block: len= %d, data= ", s->s3->tmp.key_block_length);
2570Sstevel@tonic-gate 	{
2580Sstevel@tonic-gate         int i;
2590Sstevel@tonic-gate         for (i=0; i<s->s3->tmp.key_block_length; i++)
2600Sstevel@tonic-gate 		printf("%02x", key_block[i]);  printf("\n");
2610Sstevel@tonic-gate         }
2620Sstevel@tonic-gate #endif	/* KSSL_DEBUG */
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	if (which & SSL3_CC_READ)
2650Sstevel@tonic-gate 		{
2660Sstevel@tonic-gate 		if (s->enc_read_ctx != NULL)
2670Sstevel@tonic-gate 			reuse_dd = 1;
2680Sstevel@tonic-gate 		else if ((s->enc_read_ctx=OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL)
2690Sstevel@tonic-gate 			goto err;
2700Sstevel@tonic-gate 		dd= s->enc_read_ctx;
2710Sstevel@tonic-gate 		s->read_hash=m;
2722139Sjp161948 #ifndef OPENSSL_NO_COMP
2730Sstevel@tonic-gate 		if (s->expand != NULL)
2740Sstevel@tonic-gate 			{
2750Sstevel@tonic-gate 			COMP_CTX_free(s->expand);
2760Sstevel@tonic-gate 			s->expand=NULL;
2770Sstevel@tonic-gate 			}
2780Sstevel@tonic-gate 		if (comp != NULL)
2790Sstevel@tonic-gate 			{
2800Sstevel@tonic-gate 			s->expand=COMP_CTX_new(comp->method);
2810Sstevel@tonic-gate 			if (s->expand == NULL)
2820Sstevel@tonic-gate 				{
2830Sstevel@tonic-gate 				SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,SSL_R_COMPRESSION_LIBRARY_ERROR);
2840Sstevel@tonic-gate 				goto err2;
2850Sstevel@tonic-gate 				}
2860Sstevel@tonic-gate 			if (s->s3->rrec.comp == NULL)
2870Sstevel@tonic-gate 				s->s3->rrec.comp=(unsigned char *)
2880Sstevel@tonic-gate 					OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
2890Sstevel@tonic-gate 			if (s->s3->rrec.comp == NULL)
2900Sstevel@tonic-gate 				goto err;
2910Sstevel@tonic-gate 			}
2922139Sjp161948 #endif
2932139Sjp161948 		/* this is done by dtls1_reset_seq_numbers for DTLS1_VERSION */
2942139Sjp161948  		if (s->version != DTLS1_VERSION)
2952139Sjp161948 			memset(&(s->s3->read_sequence[0]),0,8);
2960Sstevel@tonic-gate 		mac_secret= &(s->s3->read_mac_secret[0]);
2970Sstevel@tonic-gate 		}
2980Sstevel@tonic-gate 	else
2990Sstevel@tonic-gate 		{
3000Sstevel@tonic-gate 		if (s->enc_write_ctx != NULL)
3010Sstevel@tonic-gate 			reuse_dd = 1;
3020Sstevel@tonic-gate 		else if ((s->enc_write_ctx=OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL)
3030Sstevel@tonic-gate 			goto err;
3040Sstevel@tonic-gate 		if ((s->enc_write_ctx == NULL) &&
3050Sstevel@tonic-gate 			((s->enc_write_ctx=(EVP_CIPHER_CTX *)
3060Sstevel@tonic-gate 			OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL))
3070Sstevel@tonic-gate 			goto err;
3080Sstevel@tonic-gate 		dd= s->enc_write_ctx;
3090Sstevel@tonic-gate 		s->write_hash=m;
3102139Sjp161948 #ifndef OPENSSL_NO_COMP
3110Sstevel@tonic-gate 		if (s->compress != NULL)
3120Sstevel@tonic-gate 			{
3130Sstevel@tonic-gate 			COMP_CTX_free(s->compress);
3140Sstevel@tonic-gate 			s->compress=NULL;
3150Sstevel@tonic-gate 			}
3160Sstevel@tonic-gate 		if (comp != NULL)
3170Sstevel@tonic-gate 			{
3180Sstevel@tonic-gate 			s->compress=COMP_CTX_new(comp->method);
3190Sstevel@tonic-gate 			if (s->compress == NULL)
3200Sstevel@tonic-gate 				{
3210Sstevel@tonic-gate 				SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,SSL_R_COMPRESSION_LIBRARY_ERROR);
3220Sstevel@tonic-gate 				goto err2;
3230Sstevel@tonic-gate 				}
3240Sstevel@tonic-gate 			}
3252139Sjp161948 #endif
3262139Sjp161948 		/* this is done by dtls1_reset_seq_numbers for DTLS1_VERSION */
3272139Sjp161948  		if (s->version != DTLS1_VERSION)
3282139Sjp161948 			memset(&(s->s3->write_sequence[0]),0,8);
3290Sstevel@tonic-gate 		mac_secret= &(s->s3->write_mac_secret[0]);
3300Sstevel@tonic-gate 		}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	if (reuse_dd)
3330Sstevel@tonic-gate 		EVP_CIPHER_CTX_cleanup(dd);
3340Sstevel@tonic-gate 	EVP_CIPHER_CTX_init(dd);
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	p=s->s3->tmp.key_block;
3370Sstevel@tonic-gate 	i=EVP_MD_size(m);
3380Sstevel@tonic-gate 	cl=EVP_CIPHER_key_length(c);
3390Sstevel@tonic-gate 	j=is_export ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ?
3400Sstevel@tonic-gate 	               cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl;
3410Sstevel@tonic-gate 	/* Was j=(exp)?5:EVP_CIPHER_key_length(c); */
3420Sstevel@tonic-gate 	k=EVP_CIPHER_iv_length(c);
3430Sstevel@tonic-gate 	er1= &(s->s3->client_random[0]);
3440Sstevel@tonic-gate 	er2= &(s->s3->server_random[0]);
3450Sstevel@tonic-gate 	if (	(which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
3460Sstevel@tonic-gate 		(which == SSL3_CHANGE_CIPHER_SERVER_READ))
3470Sstevel@tonic-gate 		{
3480Sstevel@tonic-gate 		ms=  &(p[ 0]); n=i+i;
3490Sstevel@tonic-gate 		key= &(p[ n]); n+=j+j;
3500Sstevel@tonic-gate 		iv=  &(p[ n]); n+=k+k;
3510Sstevel@tonic-gate 		exp_label=(unsigned char *)TLS_MD_CLIENT_WRITE_KEY_CONST;
3520Sstevel@tonic-gate 		exp_label_len=TLS_MD_CLIENT_WRITE_KEY_CONST_SIZE;
3530Sstevel@tonic-gate 		client_write=1;
3540Sstevel@tonic-gate 		}
3550Sstevel@tonic-gate 	else
3560Sstevel@tonic-gate 		{
3570Sstevel@tonic-gate 		n=i;
3580Sstevel@tonic-gate 		ms=  &(p[ n]); n+=i+j;
3590Sstevel@tonic-gate 		key= &(p[ n]); n+=j+k;
3600Sstevel@tonic-gate 		iv=  &(p[ n]); n+=k;
3610Sstevel@tonic-gate 		exp_label=(unsigned char *)TLS_MD_SERVER_WRITE_KEY_CONST;
3620Sstevel@tonic-gate 		exp_label_len=TLS_MD_SERVER_WRITE_KEY_CONST_SIZE;
3630Sstevel@tonic-gate 		client_write=0;
3640Sstevel@tonic-gate 		}
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	if (n > s->s3->tmp.key_block_length)
3670Sstevel@tonic-gate 		{
3680Sstevel@tonic-gate 		SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,ERR_R_INTERNAL_ERROR);
3690Sstevel@tonic-gate 		goto err2;
3700Sstevel@tonic-gate 		}
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	memcpy(mac_secret,ms,i);
3730Sstevel@tonic-gate #ifdef TLS_DEBUG
3740Sstevel@tonic-gate printf("which = %04X\nmac key=",which);
3750Sstevel@tonic-gate { int z; for (z=0; z<i; z++) printf("%02X%c",ms[z],((z+1)%16)?' ':'\n'); }
3760Sstevel@tonic-gate #endif
3770Sstevel@tonic-gate 	if (is_export)
3780Sstevel@tonic-gate 		{
3790Sstevel@tonic-gate 		/* In here I set both the read and write key/iv to the
3800Sstevel@tonic-gate 		 * same value since only the correct one will be used :-).
3810Sstevel@tonic-gate 		 */
3820Sstevel@tonic-gate 		p=buf;
3830Sstevel@tonic-gate 		memcpy(p,exp_label,exp_label_len);
3840Sstevel@tonic-gate 		p+=exp_label_len;
3850Sstevel@tonic-gate 		memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
3860Sstevel@tonic-gate 		p+=SSL3_RANDOM_SIZE;
3870Sstevel@tonic-gate 		memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
3880Sstevel@tonic-gate 		p+=SSL3_RANDOM_SIZE;
3890Sstevel@tonic-gate 		tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(p-buf),key,j,
3900Sstevel@tonic-gate 			 tmp1,tmp2,EVP_CIPHER_key_length(c));
3910Sstevel@tonic-gate 		key=tmp1;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 		if (k > 0)
3940Sstevel@tonic-gate 			{
3950Sstevel@tonic-gate 			p=buf;
3960Sstevel@tonic-gate 			memcpy(p,TLS_MD_IV_BLOCK_CONST,
3970Sstevel@tonic-gate 				TLS_MD_IV_BLOCK_CONST_SIZE);
3980Sstevel@tonic-gate 			p+=TLS_MD_IV_BLOCK_CONST_SIZE;
3990Sstevel@tonic-gate 			memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
4000Sstevel@tonic-gate 			p+=SSL3_RANDOM_SIZE;
4010Sstevel@tonic-gate 			memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
4020Sstevel@tonic-gate 			p+=SSL3_RANDOM_SIZE;
4030Sstevel@tonic-gate 			tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,p-buf,empty,0,
4040Sstevel@tonic-gate 				 iv1,iv2,k*2);
4050Sstevel@tonic-gate 			if (client_write)
4060Sstevel@tonic-gate 				iv=iv1;
4070Sstevel@tonic-gate 			else
4080Sstevel@tonic-gate 				iv= &(iv1[k]);
4090Sstevel@tonic-gate 			}
4100Sstevel@tonic-gate 		}
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	s->session->key_arg_length=0;
4130Sstevel@tonic-gate #ifdef KSSL_DEBUG
4140Sstevel@tonic-gate 	{
4150Sstevel@tonic-gate         int i;
4160Sstevel@tonic-gate 	printf("EVP_CipherInit_ex(dd,c,key=,iv=,which)\n");
4170Sstevel@tonic-gate 	printf("\tkey= "); for (i=0; i<c->key_len; i++) printf("%02x", key[i]);
4180Sstevel@tonic-gate 	printf("\n");
4190Sstevel@tonic-gate 	printf("\t iv= "); for (i=0; i<c->iv_len; i++) printf("%02x", iv[i]);
4200Sstevel@tonic-gate 	printf("\n");
4210Sstevel@tonic-gate 	}
4220Sstevel@tonic-gate #endif	/* KSSL_DEBUG */
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	EVP_CipherInit_ex(dd,c,NULL,key,iv,(which & SSL3_CC_WRITE));
4250Sstevel@tonic-gate #ifdef TLS_DEBUG
4260Sstevel@tonic-gate printf("which = %04X\nkey=",which);
4270Sstevel@tonic-gate { int z; for (z=0; z<EVP_CIPHER_key_length(c); z++) printf("%02X%c",key[z],((z+1)%16)?' ':'\n'); }
4280Sstevel@tonic-gate printf("\niv=");
4290Sstevel@tonic-gate { int z; for (z=0; z<k; z++) printf("%02X%c",iv[z],((z+1)%16)?' ':'\n'); }
4300Sstevel@tonic-gate printf("\n");
4310Sstevel@tonic-gate #endif
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	OPENSSL_cleanse(tmp1,sizeof(tmp1));
4340Sstevel@tonic-gate 	OPENSSL_cleanse(tmp2,sizeof(tmp1));
4350Sstevel@tonic-gate 	OPENSSL_cleanse(iv1,sizeof(iv1));
4360Sstevel@tonic-gate 	OPENSSL_cleanse(iv2,sizeof(iv2));
4370Sstevel@tonic-gate 	return(1);
4380Sstevel@tonic-gate err:
4390Sstevel@tonic-gate 	SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,ERR_R_MALLOC_FAILURE);
4400Sstevel@tonic-gate err2:
4410Sstevel@tonic-gate 	return(0);
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate 
tls1_setup_key_block(SSL * s)4440Sstevel@tonic-gate int tls1_setup_key_block(SSL *s)
4450Sstevel@tonic-gate 	{
4460Sstevel@tonic-gate 	unsigned char *p1,*p2;
4470Sstevel@tonic-gate 	const EVP_CIPHER *c;
4480Sstevel@tonic-gate 	const EVP_MD *hash;
4490Sstevel@tonic-gate 	int num;
4500Sstevel@tonic-gate 	SSL_COMP *comp;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate #ifdef KSSL_DEBUG
4530Sstevel@tonic-gate 	printf ("tls1_setup_key_block()\n");
4540Sstevel@tonic-gate #endif	/* KSSL_DEBUG */
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	if (s->s3->tmp.key_block_length != 0)
4570Sstevel@tonic-gate 		return(1);
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	if (!ssl_cipher_get_evp(s->session,&c,&hash,&comp))
4600Sstevel@tonic-gate 		{
4610Sstevel@tonic-gate 		SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
4620Sstevel@tonic-gate 		return(0);
4630Sstevel@tonic-gate 		}
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	s->s3->tmp.new_sym_enc=c;
4660Sstevel@tonic-gate 	s->s3->tmp.new_hash=hash;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	num=EVP_CIPHER_key_length(c)+EVP_MD_size(hash)+EVP_CIPHER_iv_length(c);
4690Sstevel@tonic-gate 	num*=2;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	ssl3_cleanup_key_block(s);
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	if ((p1=(unsigned char *)OPENSSL_malloc(num)) == NULL)
4740Sstevel@tonic-gate 		goto err;
4750Sstevel@tonic-gate 	if ((p2=(unsigned char *)OPENSSL_malloc(num)) == NULL)
4760Sstevel@tonic-gate 		goto err;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	s->s3->tmp.key_block_length=num;
4790Sstevel@tonic-gate 	s->s3->tmp.key_block=p1;
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate #ifdef TLS_DEBUG
4830Sstevel@tonic-gate printf("client random\n");
4840Sstevel@tonic-gate { int z; for (z=0; z<SSL3_RANDOM_SIZE; z++) printf("%02X%c",s->s3->client_random[z],((z+1)%16)?' ':'\n'); }
4850Sstevel@tonic-gate printf("server random\n");
4860Sstevel@tonic-gate { int z; for (z=0; z<SSL3_RANDOM_SIZE; z++) printf("%02X%c",s->s3->server_random[z],((z+1)%16)?' ':'\n'); }
4870Sstevel@tonic-gate printf("pre-master\n");
4880Sstevel@tonic-gate { int z; for (z=0; z<s->session->master_key_length; z++) printf("%02X%c",s->session->master_key[z],((z+1)%16)?' ':'\n'); }
4890Sstevel@tonic-gate #endif
4900Sstevel@tonic-gate 	tls1_generate_key_block(s,p1,p2,num);
4910Sstevel@tonic-gate 	OPENSSL_cleanse(p2,num);
4920Sstevel@tonic-gate 	OPENSSL_free(p2);
4930Sstevel@tonic-gate #ifdef TLS_DEBUG
4940Sstevel@tonic-gate printf("\nkey block\n");
4950Sstevel@tonic-gate { int z; for (z=0; z<num; z++) printf("%02X%c",p1[z],((z+1)%16)?' ':'\n'); }
4960Sstevel@tonic-gate #endif
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
4990Sstevel@tonic-gate 		{
5000Sstevel@tonic-gate 		/* enable vulnerability countermeasure for CBC ciphers with
5010Sstevel@tonic-gate 		 * known-IV problem (http://www.openssl.org/~bodo/tls-cbc.txt)
5020Sstevel@tonic-gate 		 */
5030Sstevel@tonic-gate 		s->s3->need_empty_fragments = 1;
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 		if (s->session->cipher != NULL)
5060Sstevel@tonic-gate 			{
5070Sstevel@tonic-gate 			if ((s->session->cipher->algorithms & SSL_ENC_MASK) == SSL_eNULL)
5080Sstevel@tonic-gate 				s->s3->need_empty_fragments = 0;
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate #ifndef OPENSSL_NO_RC4
5110Sstevel@tonic-gate 			if ((s->session->cipher->algorithms & SSL_ENC_MASK) == SSL_RC4)
5120Sstevel@tonic-gate 				s->s3->need_empty_fragments = 0;
5130Sstevel@tonic-gate #endif
5140Sstevel@tonic-gate 			}
5150Sstevel@tonic-gate 		}
5162139Sjp161948 
5170Sstevel@tonic-gate 	return(1);
5180Sstevel@tonic-gate err:
5190Sstevel@tonic-gate 	SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,ERR_R_MALLOC_FAILURE);
5200Sstevel@tonic-gate 	return(0);
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 
tls1_enc(SSL * s,int send)5230Sstevel@tonic-gate int tls1_enc(SSL *s, int send)
5240Sstevel@tonic-gate 	{
5250Sstevel@tonic-gate 	SSL3_RECORD *rec;
5260Sstevel@tonic-gate 	EVP_CIPHER_CTX *ds;
5270Sstevel@tonic-gate 	unsigned long l;
5280Sstevel@tonic-gate 	int bs,i,ii,j,k,n=0;
5290Sstevel@tonic-gate 	const EVP_CIPHER *enc;
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	if (send)
5320Sstevel@tonic-gate 		{
5330Sstevel@tonic-gate 		if (s->write_hash != NULL)
5340Sstevel@tonic-gate 			n=EVP_MD_size(s->write_hash);
5350Sstevel@tonic-gate 		ds=s->enc_write_ctx;
5360Sstevel@tonic-gate 		rec= &(s->s3->wrec);
5370Sstevel@tonic-gate 		if (s->enc_write_ctx == NULL)
5380Sstevel@tonic-gate 			enc=NULL;
5390Sstevel@tonic-gate 		else
5400Sstevel@tonic-gate 			enc=EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
5410Sstevel@tonic-gate 		}
5420Sstevel@tonic-gate 	else
5430Sstevel@tonic-gate 		{
5440Sstevel@tonic-gate 		if (s->read_hash != NULL)
5450Sstevel@tonic-gate 			n=EVP_MD_size(s->read_hash);
5460Sstevel@tonic-gate 		ds=s->enc_read_ctx;
5470Sstevel@tonic-gate 		rec= &(s->s3->rrec);
5480Sstevel@tonic-gate 		if (s->enc_read_ctx == NULL)
5490Sstevel@tonic-gate 			enc=NULL;
5500Sstevel@tonic-gate 		else
5510Sstevel@tonic-gate 			enc=EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
5520Sstevel@tonic-gate 		}
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate #ifdef KSSL_DEBUG
5550Sstevel@tonic-gate 	printf("tls1_enc(%d)\n", send);
5560Sstevel@tonic-gate #endif    /* KSSL_DEBUG */
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	if ((s->session == NULL) || (ds == NULL) ||
5590Sstevel@tonic-gate 		(enc == NULL))
5600Sstevel@tonic-gate 		{
5610Sstevel@tonic-gate 		memmove(rec->data,rec->input,rec->length);
5620Sstevel@tonic-gate 		rec->input=rec->data;
5630Sstevel@tonic-gate 		}
5640Sstevel@tonic-gate 	else
5650Sstevel@tonic-gate 		{
5660Sstevel@tonic-gate 		l=rec->length;
5670Sstevel@tonic-gate 		bs=EVP_CIPHER_block_size(ds->cipher);
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 		if ((bs != 1) && send)
5700Sstevel@tonic-gate 			{
5710Sstevel@tonic-gate 			i=bs-((int)l%bs);
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 			/* Add weird padding of upto 256 bytes */
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 			/* we need to add 'i' padding bytes of value j */
5760Sstevel@tonic-gate 			j=i-1;
5770Sstevel@tonic-gate 			if (s->options & SSL_OP_TLS_BLOCK_PADDING_BUG)
5780Sstevel@tonic-gate 				{
5790Sstevel@tonic-gate 				if (s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG)
5800Sstevel@tonic-gate 					j++;
5810Sstevel@tonic-gate 				}
5820Sstevel@tonic-gate 			for (k=(int)l; k<(int)(l+i); k++)
5830Sstevel@tonic-gate 				rec->input[k]=j;
5840Sstevel@tonic-gate 			l+=i;
5850Sstevel@tonic-gate 			rec->length+=i;
5860Sstevel@tonic-gate 			}
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate #ifdef KSSL_DEBUG
5890Sstevel@tonic-gate 		{
5900Sstevel@tonic-gate                 unsigned long ui;
5910Sstevel@tonic-gate 		printf("EVP_Cipher(ds=%p,rec->data=%p,rec->input=%p,l=%ld) ==>\n",
5920Sstevel@tonic-gate                         ds,rec->data,rec->input,l);
5930Sstevel@tonic-gate 		printf("\tEVP_CIPHER_CTX: %d buf_len, %d key_len [%d %d], %d iv_len\n",
5940Sstevel@tonic-gate                         ds->buf_len, ds->cipher->key_len,
5950Sstevel@tonic-gate                         DES_KEY_SZ, DES_SCHEDULE_SZ,
5960Sstevel@tonic-gate                         ds->cipher->iv_len);
5970Sstevel@tonic-gate 		printf("\t\tIV: ");
5980Sstevel@tonic-gate 		for (i=0; i<ds->cipher->iv_len; i++) printf("%02X", ds->iv[i]);
5990Sstevel@tonic-gate 		printf("\n");
6000Sstevel@tonic-gate 		printf("\trec->input=");
6010Sstevel@tonic-gate 		for (ui=0; ui<l; ui++) printf(" %02x", rec->input[ui]);
6020Sstevel@tonic-gate 		printf("\n");
6030Sstevel@tonic-gate 		}
6040Sstevel@tonic-gate #endif	/* KSSL_DEBUG */
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 		if (!send)
6070Sstevel@tonic-gate 			{
6080Sstevel@tonic-gate 			if (l == 0 || l%bs != 0)
6090Sstevel@tonic-gate 				{
6100Sstevel@tonic-gate 				SSLerr(SSL_F_TLS1_ENC,SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
6110Sstevel@tonic-gate 				ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECRYPTION_FAILED);
6120Sstevel@tonic-gate 				return 0;
6130Sstevel@tonic-gate 				}
6140Sstevel@tonic-gate 			}
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 		EVP_Cipher(ds,rec->data,rec->input,l);
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate #ifdef KSSL_DEBUG
6190Sstevel@tonic-gate 		{
6200Sstevel@tonic-gate                 unsigned long i;
6210Sstevel@tonic-gate                 printf("\trec->data=");
6220Sstevel@tonic-gate 		for (i=0; i<l; i++)
6230Sstevel@tonic-gate                         printf(" %02x", rec->data[i]);  printf("\n");
6240Sstevel@tonic-gate                 }
6250Sstevel@tonic-gate #endif	/* KSSL_DEBUG */
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 		if ((bs != 1) && !send)
6280Sstevel@tonic-gate 			{
6290Sstevel@tonic-gate 			ii=i=rec->data[l-1]; /* padding_length */
6300Sstevel@tonic-gate 			i++;
6310Sstevel@tonic-gate 			if (s->options&SSL_OP_TLS_BLOCK_PADDING_BUG)
6320Sstevel@tonic-gate 				{
6330Sstevel@tonic-gate 				/* First packet is even in size, so check */
6340Sstevel@tonic-gate 				if ((memcmp(s->s3->read_sequence,
6350Sstevel@tonic-gate 					"\0\0\0\0\0\0\0\0",8) == 0) && !(ii & 1))
6360Sstevel@tonic-gate 					s->s3->flags|=TLS1_FLAGS_TLS_PADDING_BUG;
6370Sstevel@tonic-gate 				if (s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG)
6380Sstevel@tonic-gate 					i--;
6390Sstevel@tonic-gate 				}
6400Sstevel@tonic-gate 			/* TLS 1.0 does not bound the number of padding bytes by the block size.
6410Sstevel@tonic-gate 			 * All of them must have value 'padding_length'. */
6420Sstevel@tonic-gate 			if (i > (int)rec->length)
6430Sstevel@tonic-gate 				{
6440Sstevel@tonic-gate 				/* Incorrect padding. SSLerr() and ssl3_alert are done
6450Sstevel@tonic-gate 				 * by caller: we don't want to reveal whether this is
6460Sstevel@tonic-gate 				 * a decryption error or a MAC verification failure
6470Sstevel@tonic-gate 				 * (see http://www.openssl.org/~bodo/tls-cbc.txt) */
6480Sstevel@tonic-gate 				return -1;
6490Sstevel@tonic-gate 				}
6500Sstevel@tonic-gate 			for (j=(int)(l-i); j<(int)l; j++)
6510Sstevel@tonic-gate 				{
6520Sstevel@tonic-gate 				if (rec->data[j] != ii)
6530Sstevel@tonic-gate 					{
6540Sstevel@tonic-gate 					/* Incorrect padding */
6550Sstevel@tonic-gate 					return -1;
6560Sstevel@tonic-gate 					}
6570Sstevel@tonic-gate 				}
6580Sstevel@tonic-gate 			rec->length-=i;
6590Sstevel@tonic-gate 			}
6600Sstevel@tonic-gate 		}
6610Sstevel@tonic-gate 	return(1);
6620Sstevel@tonic-gate 	}
6630Sstevel@tonic-gate 
tls1_cert_verify_mac(SSL * s,EVP_MD_CTX * in_ctx,unsigned char * out)6640Sstevel@tonic-gate int tls1_cert_verify_mac(SSL *s, EVP_MD_CTX *in_ctx, unsigned char *out)
6650Sstevel@tonic-gate 	{
6660Sstevel@tonic-gate 	unsigned int ret;
6670Sstevel@tonic-gate 	EVP_MD_CTX ctx;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	EVP_MD_CTX_init(&ctx);
6700Sstevel@tonic-gate 	EVP_MD_CTX_copy_ex(&ctx,in_ctx);
6710Sstevel@tonic-gate 	EVP_DigestFinal_ex(&ctx,out,&ret);
6720Sstevel@tonic-gate 	EVP_MD_CTX_cleanup(&ctx);
6730Sstevel@tonic-gate 	return((int)ret);
6740Sstevel@tonic-gate 	}
6750Sstevel@tonic-gate 
tls1_final_finish_mac(SSL * s,EVP_MD_CTX * in1_ctx,EVP_MD_CTX * in2_ctx,const char * str,int slen,unsigned char * out)6760Sstevel@tonic-gate int tls1_final_finish_mac(SSL *s, EVP_MD_CTX *in1_ctx, EVP_MD_CTX *in2_ctx,
6770Sstevel@tonic-gate 	     const char *str, int slen, unsigned char *out)
6780Sstevel@tonic-gate 	{
6790Sstevel@tonic-gate 	unsigned int i;
6800Sstevel@tonic-gate 	EVP_MD_CTX ctx;
6810Sstevel@tonic-gate 	unsigned char buf[TLS_MD_MAX_CONST_SIZE+MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
6820Sstevel@tonic-gate 	unsigned char *q,buf2[12];
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	q=buf;
6850Sstevel@tonic-gate 	memcpy(q,str,slen);
6860Sstevel@tonic-gate 	q+=slen;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	EVP_MD_CTX_init(&ctx);
6890Sstevel@tonic-gate 	EVP_MD_CTX_copy_ex(&ctx,in1_ctx);
6900Sstevel@tonic-gate 	EVP_DigestFinal_ex(&ctx,q,&i);
6910Sstevel@tonic-gate 	q+=i;
6920Sstevel@tonic-gate 	EVP_MD_CTX_copy_ex(&ctx,in2_ctx);
6930Sstevel@tonic-gate 	EVP_DigestFinal_ex(&ctx,q,&i);
6940Sstevel@tonic-gate 	q+=i;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(q-buf),
6970Sstevel@tonic-gate 		s->session->master_key,s->session->master_key_length,
6980Sstevel@tonic-gate 		out,buf2,sizeof buf2);
6990Sstevel@tonic-gate 	EVP_MD_CTX_cleanup(&ctx);
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	return sizeof buf2;
7020Sstevel@tonic-gate 	}
7030Sstevel@tonic-gate 
tls1_mac(SSL * ssl,unsigned char * md,int send)7040Sstevel@tonic-gate int tls1_mac(SSL *ssl, unsigned char *md, int send)
7050Sstevel@tonic-gate 	{
7060Sstevel@tonic-gate 	SSL3_RECORD *rec;
7070Sstevel@tonic-gate 	unsigned char *mac_sec,*seq;
7080Sstevel@tonic-gate 	const EVP_MD *hash;
7090Sstevel@tonic-gate 	unsigned int md_size;
7100Sstevel@tonic-gate 	int i;
7110Sstevel@tonic-gate 	HMAC_CTX hmac;
7120Sstevel@tonic-gate 	unsigned char buf[5];
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	if (send)
7150Sstevel@tonic-gate 		{
7160Sstevel@tonic-gate 		rec= &(ssl->s3->wrec);
7170Sstevel@tonic-gate 		mac_sec= &(ssl->s3->write_mac_secret[0]);
7180Sstevel@tonic-gate 		seq= &(ssl->s3->write_sequence[0]);
7190Sstevel@tonic-gate 		hash=ssl->write_hash;
7200Sstevel@tonic-gate 		}
7210Sstevel@tonic-gate 	else
7220Sstevel@tonic-gate 		{
7230Sstevel@tonic-gate 		rec= &(ssl->s3->rrec);
7240Sstevel@tonic-gate 		mac_sec= &(ssl->s3->read_mac_secret[0]);
7250Sstevel@tonic-gate 		seq= &(ssl->s3->read_sequence[0]);
7260Sstevel@tonic-gate 		hash=ssl->read_hash;
7270Sstevel@tonic-gate 		}
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	md_size=EVP_MD_size(hash);
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	buf[0]=rec->type;
732*5434Sjp161948 	if (ssl->version == DTLS1_VERSION && ssl->client_version == DTLS1_BAD_VER)
733*5434Sjp161948 		{
734*5434Sjp161948 		buf[1]=TLS1_VERSION_MAJOR;
735*5434Sjp161948 		buf[2]=TLS1_VERSION_MINOR;
736*5434Sjp161948 		}
737*5434Sjp161948 	else	{
738*5434Sjp161948 		buf[1]=(unsigned char)(ssl->version>>8);
739*5434Sjp161948 		buf[2]=(unsigned char)(ssl->version);
740*5434Sjp161948 		}
741*5434Sjp161948 
7420Sstevel@tonic-gate 	buf[3]=rec->length>>8;
7430Sstevel@tonic-gate 	buf[4]=rec->length&0xff;
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	/* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
7460Sstevel@tonic-gate 	HMAC_CTX_init(&hmac);
7470Sstevel@tonic-gate 	HMAC_Init_ex(&hmac,mac_sec,EVP_MD_size(hash),hash,NULL);
748*5434Sjp161948 
749*5434Sjp161948 	if (ssl->version == DTLS1_VERSION && ssl->client_version != DTLS1_BAD_VER)
750*5434Sjp161948 		{
751*5434Sjp161948 		unsigned char dtlsseq[8],*p=dtlsseq;
752*5434Sjp161948 
753*5434Sjp161948 		s2n(send?ssl->d1->w_epoch:ssl->d1->r_epoch, p);
754*5434Sjp161948 		memcpy (p,&seq[2],6);
755*5434Sjp161948 
756*5434Sjp161948 		HMAC_Update(&hmac,dtlsseq,8);
757*5434Sjp161948 		}
758*5434Sjp161948 	else
759*5434Sjp161948 		HMAC_Update(&hmac,seq,8);
760*5434Sjp161948 
7610Sstevel@tonic-gate 	HMAC_Update(&hmac,buf,5);
7620Sstevel@tonic-gate 	HMAC_Update(&hmac,rec->input,rec->length);
7630Sstevel@tonic-gate 	HMAC_Final(&hmac,md,&md_size);
7640Sstevel@tonic-gate 	HMAC_CTX_cleanup(&hmac);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate #ifdef TLS_DEBUG
7670Sstevel@tonic-gate printf("sec=");
7680Sstevel@tonic-gate {unsigned int z; for (z=0; z<md_size; z++) printf("%02X ",mac_sec[z]); printf("\n"); }
7690Sstevel@tonic-gate printf("seq=");
7700Sstevel@tonic-gate {int z; for (z=0; z<8; z++) printf("%02X ",seq[z]); printf("\n"); }
7710Sstevel@tonic-gate printf("buf=");
7720Sstevel@tonic-gate {int z; for (z=0; z<5; z++) printf("%02X ",buf[z]); printf("\n"); }
7730Sstevel@tonic-gate printf("rec=");
7740Sstevel@tonic-gate {unsigned int z; for (z=0; z<rec->length; z++) printf("%02X ",buf[z]); printf("\n"); }
7750Sstevel@tonic-gate #endif
7760Sstevel@tonic-gate 
777*5434Sjp161948 	if ( SSL_version(ssl) != DTLS1_VERSION)
778*5434Sjp161948 		{
7792139Sjp161948 		for (i=7; i>=0; i--)
7802139Sjp161948 			{
7812139Sjp161948 			++seq[i];
7822139Sjp161948 			if (seq[i] != 0) break;
7832139Sjp161948 			}
7840Sstevel@tonic-gate 		}
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate #ifdef TLS_DEBUG
7870Sstevel@tonic-gate {unsigned int z; for (z=0; z<md_size; z++) printf("%02X ",md[z]); printf("\n"); }
7880Sstevel@tonic-gate #endif
7890Sstevel@tonic-gate 	return(md_size);
7900Sstevel@tonic-gate 	}
7910Sstevel@tonic-gate 
tls1_generate_master_secret(SSL * s,unsigned char * out,unsigned char * p,int len)7920Sstevel@tonic-gate int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
7930Sstevel@tonic-gate 	     int len)
7940Sstevel@tonic-gate 	{
7950Sstevel@tonic-gate 	unsigned char buf[SSL3_RANDOM_SIZE*2+TLS_MD_MASTER_SECRET_CONST_SIZE];
7960Sstevel@tonic-gate 	unsigned char buff[SSL_MAX_MASTER_KEY_LENGTH];
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate #ifdef KSSL_DEBUG
7990Sstevel@tonic-gate 	printf ("tls1_generate_master_secret(%p,%p, %p, %d)\n", s,out, p,len);
8000Sstevel@tonic-gate #endif	/* KSSL_DEBUG */
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	/* Setup the stuff to munge */
8030Sstevel@tonic-gate 	memcpy(buf,TLS_MD_MASTER_SECRET_CONST,
8040Sstevel@tonic-gate 		TLS_MD_MASTER_SECRET_CONST_SIZE);
8050Sstevel@tonic-gate 	memcpy(&(buf[TLS_MD_MASTER_SECRET_CONST_SIZE]),
8060Sstevel@tonic-gate 		s->s3->client_random,SSL3_RANDOM_SIZE);
8070Sstevel@tonic-gate 	memcpy(&(buf[SSL3_RANDOM_SIZE+TLS_MD_MASTER_SECRET_CONST_SIZE]),
8080Sstevel@tonic-gate 		s->s3->server_random,SSL3_RANDOM_SIZE);
8090Sstevel@tonic-gate 	tls1_PRF(s->ctx->md5,s->ctx->sha1,
8100Sstevel@tonic-gate 		buf,TLS_MD_MASTER_SECRET_CONST_SIZE+SSL3_RANDOM_SIZE*2,p,len,
8110Sstevel@tonic-gate 		s->session->master_key,buff,sizeof buff);
8120Sstevel@tonic-gate #ifdef KSSL_DEBUG
8130Sstevel@tonic-gate 	printf ("tls1_generate_master_secret() complete\n");
8140Sstevel@tonic-gate #endif	/* KSSL_DEBUG */
8150Sstevel@tonic-gate 	return(SSL3_MASTER_SECRET_SIZE);
8160Sstevel@tonic-gate 	}
8170Sstevel@tonic-gate 
tls1_alert_code(int code)8180Sstevel@tonic-gate int tls1_alert_code(int code)
8190Sstevel@tonic-gate 	{
8200Sstevel@tonic-gate 	switch (code)
8210Sstevel@tonic-gate 		{
8220Sstevel@tonic-gate 	case SSL_AD_CLOSE_NOTIFY:	return(SSL3_AD_CLOSE_NOTIFY);
8230Sstevel@tonic-gate 	case SSL_AD_UNEXPECTED_MESSAGE:	return(SSL3_AD_UNEXPECTED_MESSAGE);
8240Sstevel@tonic-gate 	case SSL_AD_BAD_RECORD_MAC:	return(SSL3_AD_BAD_RECORD_MAC);
8250Sstevel@tonic-gate 	case SSL_AD_DECRYPTION_FAILED:	return(TLS1_AD_DECRYPTION_FAILED);
8260Sstevel@tonic-gate 	case SSL_AD_RECORD_OVERFLOW:	return(TLS1_AD_RECORD_OVERFLOW);
8270Sstevel@tonic-gate 	case SSL_AD_DECOMPRESSION_FAILURE:return(SSL3_AD_DECOMPRESSION_FAILURE);
8280Sstevel@tonic-gate 	case SSL_AD_HANDSHAKE_FAILURE:	return(SSL3_AD_HANDSHAKE_FAILURE);
8290Sstevel@tonic-gate 	case SSL_AD_NO_CERTIFICATE:	return(-1);
8300Sstevel@tonic-gate 	case SSL_AD_BAD_CERTIFICATE:	return(SSL3_AD_BAD_CERTIFICATE);
8310Sstevel@tonic-gate 	case SSL_AD_UNSUPPORTED_CERTIFICATE:return(SSL3_AD_UNSUPPORTED_CERTIFICATE);
8320Sstevel@tonic-gate 	case SSL_AD_CERTIFICATE_REVOKED:return(SSL3_AD_CERTIFICATE_REVOKED);
8330Sstevel@tonic-gate 	case SSL_AD_CERTIFICATE_EXPIRED:return(SSL3_AD_CERTIFICATE_EXPIRED);
8340Sstevel@tonic-gate 	case SSL_AD_CERTIFICATE_UNKNOWN:return(SSL3_AD_CERTIFICATE_UNKNOWN);
8350Sstevel@tonic-gate 	case SSL_AD_ILLEGAL_PARAMETER:	return(SSL3_AD_ILLEGAL_PARAMETER);
8360Sstevel@tonic-gate 	case SSL_AD_UNKNOWN_CA:		return(TLS1_AD_UNKNOWN_CA);
8370Sstevel@tonic-gate 	case SSL_AD_ACCESS_DENIED:	return(TLS1_AD_ACCESS_DENIED);
8380Sstevel@tonic-gate 	case SSL_AD_DECODE_ERROR:	return(TLS1_AD_DECODE_ERROR);
8390Sstevel@tonic-gate 	case SSL_AD_DECRYPT_ERROR:	return(TLS1_AD_DECRYPT_ERROR);
8400Sstevel@tonic-gate 	case SSL_AD_EXPORT_RESTRICTION:	return(TLS1_AD_EXPORT_RESTRICTION);
8410Sstevel@tonic-gate 	case SSL_AD_PROTOCOL_VERSION:	return(TLS1_AD_PROTOCOL_VERSION);
8420Sstevel@tonic-gate 	case SSL_AD_INSUFFICIENT_SECURITY:return(TLS1_AD_INSUFFICIENT_SECURITY);
8430Sstevel@tonic-gate 	case SSL_AD_INTERNAL_ERROR:	return(TLS1_AD_INTERNAL_ERROR);
8440Sstevel@tonic-gate 	case SSL_AD_USER_CANCELLED:	return(TLS1_AD_USER_CANCELLED);
8450Sstevel@tonic-gate 	case SSL_AD_NO_RENEGOTIATION:	return(TLS1_AD_NO_RENEGOTIATION);
8462139Sjp161948 	case DTLS1_AD_MISSING_HANDSHAKE_MESSAGE: return
8472139Sjp161948 					  (DTLS1_AD_MISSING_HANDSHAKE_MESSAGE);
8480Sstevel@tonic-gate 	default:			return(-1);
8490Sstevel@tonic-gate 		}
8500Sstevel@tonic-gate 	}
8510Sstevel@tonic-gate 
852