xref: /onnv-gate/usr/src/common/openssl/ssl/d1_enc.c (revision 5434:d0b14f9f9750)
12139Sjp161948 /* ssl/d1_enc.c */
22139Sjp161948 /*
32139Sjp161948  * DTLS implementation written by Nagendra Modadugu
42139Sjp161948  * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
52139Sjp161948  */
62139Sjp161948 /* ====================================================================
72139Sjp161948  * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
82139Sjp161948  *
92139Sjp161948  * Redistribution and use in source and binary forms, with or without
102139Sjp161948  * modification, are permitted provided that the following conditions
112139Sjp161948  * are met:
122139Sjp161948  *
132139Sjp161948  * 1. Redistributions of source code must retain the above copyright
142139Sjp161948  *    notice, this list of conditions and the following disclaimer.
152139Sjp161948  *
162139Sjp161948  * 2. Redistributions in binary form must reproduce the above copyright
172139Sjp161948  *    notice, this list of conditions and the following disclaimer in
182139Sjp161948  *    the documentation and/or other materials provided with the
192139Sjp161948  *    distribution.
202139Sjp161948  *
212139Sjp161948  * 3. All advertising materials mentioning features or use of this
222139Sjp161948  *    software must display the following acknowledgment:
232139Sjp161948  *    "This product includes software developed by the OpenSSL Project
242139Sjp161948  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
252139Sjp161948  *
262139Sjp161948  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
272139Sjp161948  *    endorse or promote products derived from this software without
282139Sjp161948  *    prior written permission. For written permission, please contact
292139Sjp161948  *    openssl-core@openssl.org.
302139Sjp161948  *
312139Sjp161948  * 5. Products derived from this software may not be called "OpenSSL"
322139Sjp161948  *    nor may "OpenSSL" appear in their names without prior written
332139Sjp161948  *    permission of the OpenSSL Project.
342139Sjp161948  *
352139Sjp161948  * 6. Redistributions of any form whatsoever must retain the following
362139Sjp161948  *    acknowledgment:
372139Sjp161948  *    "This product includes software developed by the OpenSSL Project
382139Sjp161948  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
392139Sjp161948  *
402139Sjp161948  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
412139Sjp161948  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
422139Sjp161948  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
432139Sjp161948  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
442139Sjp161948  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
452139Sjp161948  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
462139Sjp161948  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
472139Sjp161948  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
482139Sjp161948  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
492139Sjp161948  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
502139Sjp161948  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
512139Sjp161948  * OF THE POSSIBILITY OF SUCH DAMAGE.
522139Sjp161948  * ====================================================================
532139Sjp161948  *
542139Sjp161948  * This product includes cryptographic software written by Eric Young
552139Sjp161948  * (eay@cryptsoft.com).  This product includes software written by Tim
562139Sjp161948  * Hudson (tjh@cryptsoft.com).
572139Sjp161948  *
582139Sjp161948  */
592139Sjp161948 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
602139Sjp161948  * All rights reserved.
612139Sjp161948  *
622139Sjp161948  * This package is an SSL implementation written
632139Sjp161948  * by Eric Young (eay@cryptsoft.com).
642139Sjp161948  * The implementation was written so as to conform with Netscapes SSL.
652139Sjp161948  *
662139Sjp161948  * This library is free for commercial and non-commercial use as long as
672139Sjp161948  * the following conditions are aheared to.  The following conditions
682139Sjp161948  * apply to all code found in this distribution, be it the RC4, RSA,
692139Sjp161948  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
702139Sjp161948  * included with this distribution is covered by the same copyright terms
712139Sjp161948  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
722139Sjp161948  *
732139Sjp161948  * Copyright remains Eric Young's, and as such any Copyright notices in
742139Sjp161948  * the code are not to be removed.
752139Sjp161948  * If this package is used in a product, Eric Young should be given attribution
762139Sjp161948  * as the author of the parts of the library used.
772139Sjp161948  * This can be in the form of a textual message at program startup or
782139Sjp161948  * in documentation (online or textual) provided with the package.
792139Sjp161948  *
802139Sjp161948  * Redistribution and use in source and binary forms, with or without
812139Sjp161948  * modification, are permitted provided that the following conditions
822139Sjp161948  * are met:
832139Sjp161948  * 1. Redistributions of source code must retain the copyright
842139Sjp161948  *    notice, this list of conditions and the following disclaimer.
852139Sjp161948  * 2. Redistributions in binary form must reproduce the above copyright
862139Sjp161948  *    notice, this list of conditions and the following disclaimer in the
872139Sjp161948  *    documentation and/or other materials provided with the distribution.
882139Sjp161948  * 3. All advertising materials mentioning features or use of this software
892139Sjp161948  *    must display the following acknowledgement:
902139Sjp161948  *    "This product includes cryptographic software written by
912139Sjp161948  *     Eric Young (eay@cryptsoft.com)"
922139Sjp161948  *    The word 'cryptographic' can be left out if the rouines from the library
932139Sjp161948  *    being used are not cryptographic related :-).
942139Sjp161948  * 4. If you include any Windows specific code (or a derivative thereof) from
952139Sjp161948  *    the apps directory (application code) you must include an acknowledgement:
962139Sjp161948  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
972139Sjp161948  *
982139Sjp161948  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
992139Sjp161948  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1002139Sjp161948  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1012139Sjp161948  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1022139Sjp161948  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1032139Sjp161948  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1042139Sjp161948  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1052139Sjp161948  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1062139Sjp161948  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1072139Sjp161948  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1082139Sjp161948  * SUCH DAMAGE.
1092139Sjp161948  *
1102139Sjp161948  * The licence and distribution terms for any publically available version or
1112139Sjp161948  * derivative of this code cannot be changed.  i.e. this code cannot simply be
1122139Sjp161948  * copied and put under another distribution licence
1132139Sjp161948  * [including the GNU Public Licence.]
1142139Sjp161948  */
1152139Sjp161948 
1162139Sjp161948 #include <stdio.h>
1172139Sjp161948 #include "ssl_locl.h"
1182139Sjp161948 #include <openssl/comp.h>
1192139Sjp161948 #include <openssl/evp.h>
1202139Sjp161948 #include <openssl/hmac.h>
1212139Sjp161948 #include <openssl/md5.h>
1222139Sjp161948 #include <openssl/rand.h>
1232139Sjp161948 
1242139Sjp161948 
dtls1_enc(SSL * s,int send)1252139Sjp161948 int dtls1_enc(SSL *s, int send)
1262139Sjp161948 	{
1272139Sjp161948 	SSL3_RECORD *rec;
1282139Sjp161948 	EVP_CIPHER_CTX *ds;
1292139Sjp161948 	unsigned long l;
1302139Sjp161948 	int bs,i,ii,j,k,n=0;
1312139Sjp161948 	const EVP_CIPHER *enc;
1322139Sjp161948 
1332139Sjp161948 	if (send)
1342139Sjp161948 		{
1352139Sjp161948 		if (s->write_hash != NULL)
1362139Sjp161948 			n=EVP_MD_size(s->write_hash);
1372139Sjp161948 		ds=s->enc_write_ctx;
1382139Sjp161948 		rec= &(s->s3->wrec);
1392139Sjp161948 		if (s->enc_write_ctx == NULL)
1402139Sjp161948 			enc=NULL;
1412139Sjp161948 		else
1422139Sjp161948 			{
1432139Sjp161948 			enc=EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
1442139Sjp161948 			if ( rec->data != rec->input)
1452139Sjp161948 				/* we can't write into the input stream */
1462139Sjp161948 				fprintf(stderr, "%s:%d: rec->data != rec->input\n",
1472139Sjp161948 					__FILE__, __LINE__);
1482139Sjp161948 			else if ( EVP_CIPHER_block_size(ds->cipher) > 1)
149*5434Sjp161948 				{
150*5434Sjp161948 				if (!RAND_bytes(rec->input, EVP_CIPHER_block_size(ds->cipher)))
151*5434Sjp161948 					return -1;
152*5434Sjp161948 				}
1532139Sjp161948 			}
1542139Sjp161948 		}
1552139Sjp161948 	else
1562139Sjp161948 		{
1572139Sjp161948 		if (s->read_hash != NULL)
1582139Sjp161948 			n=EVP_MD_size(s->read_hash);
1592139Sjp161948 		ds=s->enc_read_ctx;
1602139Sjp161948 		rec= &(s->s3->rrec);
1612139Sjp161948 		if (s->enc_read_ctx == NULL)
1622139Sjp161948 			enc=NULL;
1632139Sjp161948 		else
1642139Sjp161948 			enc=EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
1652139Sjp161948 		}
1662139Sjp161948 
1672139Sjp161948 #ifdef KSSL_DEBUG
1682139Sjp161948 	printf("dtls1_enc(%d)\n", send);
1692139Sjp161948 #endif    /* KSSL_DEBUG */
1702139Sjp161948 
1712139Sjp161948 	if ((s->session == NULL) || (ds == NULL) ||
1722139Sjp161948 		(enc == NULL))
1732139Sjp161948 		{
1742139Sjp161948 		memmove(rec->data,rec->input,rec->length);
1752139Sjp161948 		rec->input=rec->data;
1762139Sjp161948 		}
1772139Sjp161948 	else
1782139Sjp161948 		{
1792139Sjp161948 		l=rec->length;
1802139Sjp161948 		bs=EVP_CIPHER_block_size(ds->cipher);
1812139Sjp161948 
1822139Sjp161948 		if ((bs != 1) && send)
1832139Sjp161948 			{
1842139Sjp161948 			i=bs-((int)l%bs);
1852139Sjp161948 
1862139Sjp161948 			/* Add weird padding of upto 256 bytes */
1872139Sjp161948 
1882139Sjp161948 			/* we need to add 'i' padding bytes of value j */
1892139Sjp161948 			j=i-1;
1902139Sjp161948 			if (s->options & SSL_OP_TLS_BLOCK_PADDING_BUG)
1912139Sjp161948 				{
1922139Sjp161948 				if (s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG)
1932139Sjp161948 					j++;
1942139Sjp161948 				}
1952139Sjp161948 			for (k=(int)l; k<(int)(l+i); k++)
1962139Sjp161948 				rec->input[k]=j;
1972139Sjp161948 			l+=i;
1982139Sjp161948 			rec->length+=i;
1992139Sjp161948 			}
2002139Sjp161948 
2012139Sjp161948 #ifdef KSSL_DEBUG
2022139Sjp161948 		{
2032139Sjp161948                 unsigned long ui;
2042139Sjp161948 		printf("EVP_Cipher(ds=%p,rec->data=%p,rec->input=%p,l=%ld) ==>\n",
2052139Sjp161948                         ds,rec->data,rec->input,l);
2062139Sjp161948 		printf("\tEVP_CIPHER_CTX: %d buf_len, %d key_len [%d %d], %d iv_len\n",
2072139Sjp161948                         ds->buf_len, ds->cipher->key_len,
2082139Sjp161948                         DES_KEY_SZ, DES_SCHEDULE_SZ,
2092139Sjp161948                         ds->cipher->iv_len);
2102139Sjp161948 		printf("\t\tIV: ");
2112139Sjp161948 		for (i=0; i<ds->cipher->iv_len; i++) printf("%02X", ds->iv[i]);
2122139Sjp161948 		printf("\n");
2132139Sjp161948 		printf("\trec->input=");
2142139Sjp161948 		for (ui=0; ui<l; ui++) printf(" %02x", rec->input[ui]);
2152139Sjp161948 		printf("\n");
2162139Sjp161948 		}
2172139Sjp161948 #endif	/* KSSL_DEBUG */
2182139Sjp161948 
2192139Sjp161948 		if (!send)
2202139Sjp161948 			{
2212139Sjp161948 			if (l == 0 || l%bs != 0)
2222139Sjp161948 				{
2232139Sjp161948 				SSLerr(SSL_F_DTLS1_ENC,SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
2242139Sjp161948 				ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECRYPTION_FAILED);
2252139Sjp161948 				return 0;
2262139Sjp161948 				}
2272139Sjp161948 			}
2282139Sjp161948 
2292139Sjp161948 		EVP_Cipher(ds,rec->data,rec->input,l);
2302139Sjp161948 
2312139Sjp161948 #ifdef KSSL_DEBUG
2322139Sjp161948 		{
2332139Sjp161948                 unsigned long i;
2342139Sjp161948                 printf("\trec->data=");
2352139Sjp161948 		for (i=0; i<l; i++)
2362139Sjp161948                         printf(" %02x", rec->data[i]);  printf("\n");
2372139Sjp161948                 }
2382139Sjp161948 #endif	/* KSSL_DEBUG */
2392139Sjp161948 
2402139Sjp161948 		if ((bs != 1) && !send)
2412139Sjp161948 			{
2422139Sjp161948 			ii=i=rec->data[l-1]; /* padding_length */
2432139Sjp161948 			i++;
2442139Sjp161948 			if (s->options&SSL_OP_TLS_BLOCK_PADDING_BUG)
2452139Sjp161948 				{
2462139Sjp161948 				/* First packet is even in size, so check */
2472139Sjp161948 				if ((memcmp(s->s3->read_sequence,
2482139Sjp161948 					"\0\0\0\0\0\0\0\0",8) == 0) && !(ii & 1))
2492139Sjp161948 					s->s3->flags|=TLS1_FLAGS_TLS_PADDING_BUG;
2502139Sjp161948 				if (s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG)
2512139Sjp161948 					i--;
2522139Sjp161948 				}
2532139Sjp161948 			/* TLS 1.0 does not bound the number of padding bytes by the block size.
2542139Sjp161948 			 * All of them must have value 'padding_length'. */
2552139Sjp161948 			if (i > (int)rec->length)
2562139Sjp161948 				{
2572139Sjp161948 				/* Incorrect padding. SSLerr() and ssl3_alert are done
2582139Sjp161948 				 * by caller: we don't want to reveal whether this is
2592139Sjp161948 				 * a decryption error or a MAC verification failure
2602139Sjp161948 				 * (see http://www.openssl.org/~bodo/tls-cbc.txt)
2612139Sjp161948 				 */
2622139Sjp161948 				return -1;
2632139Sjp161948 				}
2642139Sjp161948 			for (j=(int)(l-i); j<(int)l; j++)
2652139Sjp161948 				{
2662139Sjp161948 				if (rec->data[j] != ii)
2672139Sjp161948 					{
2682139Sjp161948 					/* Incorrect padding */
2692139Sjp161948 					return -1;
2702139Sjp161948 					}
2712139Sjp161948 				}
2722139Sjp161948 			rec->length-=i;
2732139Sjp161948 
2742139Sjp161948 			rec->data += bs;    /* skip the implicit IV */
2752139Sjp161948 			rec->input += bs;
2762139Sjp161948 			rec->length -= bs;
2772139Sjp161948 			}
2782139Sjp161948 		}
2792139Sjp161948 	return(1);
2802139Sjp161948 	}
2812139Sjp161948 
282