1*2139Sjp161948 /* ssl/d1_enc.c */ 2*2139Sjp161948 /* 3*2139Sjp161948 * DTLS implementation written by Nagendra Modadugu 4*2139Sjp161948 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. 5*2139Sjp161948 */ 6*2139Sjp161948 /* ==================================================================== 7*2139Sjp161948 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. 8*2139Sjp161948 * 9*2139Sjp161948 * Redistribution and use in source and binary forms, with or without 10*2139Sjp161948 * modification, are permitted provided that the following conditions 11*2139Sjp161948 * are met: 12*2139Sjp161948 * 13*2139Sjp161948 * 1. Redistributions of source code must retain the above copyright 14*2139Sjp161948 * notice, this list of conditions and the following disclaimer. 15*2139Sjp161948 * 16*2139Sjp161948 * 2. Redistributions in binary form must reproduce the above copyright 17*2139Sjp161948 * notice, this list of conditions and the following disclaimer in 18*2139Sjp161948 * the documentation and/or other materials provided with the 19*2139Sjp161948 * distribution. 20*2139Sjp161948 * 21*2139Sjp161948 * 3. All advertising materials mentioning features or use of this 22*2139Sjp161948 * software must display the following acknowledgment: 23*2139Sjp161948 * "This product includes software developed by the OpenSSL Project 24*2139Sjp161948 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 25*2139Sjp161948 * 26*2139Sjp161948 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27*2139Sjp161948 * endorse or promote products derived from this software without 28*2139Sjp161948 * prior written permission. For written permission, please contact 29*2139Sjp161948 * openssl-core@openssl.org. 30*2139Sjp161948 * 31*2139Sjp161948 * 5. Products derived from this software may not be called "OpenSSL" 32*2139Sjp161948 * nor may "OpenSSL" appear in their names without prior written 33*2139Sjp161948 * permission of the OpenSSL Project. 34*2139Sjp161948 * 35*2139Sjp161948 * 6. Redistributions of any form whatsoever must retain the following 36*2139Sjp161948 * acknowledgment: 37*2139Sjp161948 * "This product includes software developed by the OpenSSL Project 38*2139Sjp161948 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 39*2139Sjp161948 * 40*2139Sjp161948 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41*2139Sjp161948 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42*2139Sjp161948 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43*2139Sjp161948 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44*2139Sjp161948 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45*2139Sjp161948 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46*2139Sjp161948 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47*2139Sjp161948 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48*2139Sjp161948 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49*2139Sjp161948 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50*2139Sjp161948 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51*2139Sjp161948 * OF THE POSSIBILITY OF SUCH DAMAGE. 52*2139Sjp161948 * ==================================================================== 53*2139Sjp161948 * 54*2139Sjp161948 * This product includes cryptographic software written by Eric Young 55*2139Sjp161948 * (eay@cryptsoft.com). This product includes software written by Tim 56*2139Sjp161948 * Hudson (tjh@cryptsoft.com). 57*2139Sjp161948 * 58*2139Sjp161948 */ 59*2139Sjp161948 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 60*2139Sjp161948 * All rights reserved. 61*2139Sjp161948 * 62*2139Sjp161948 * This package is an SSL implementation written 63*2139Sjp161948 * by Eric Young (eay@cryptsoft.com). 64*2139Sjp161948 * The implementation was written so as to conform with Netscapes SSL. 65*2139Sjp161948 * 66*2139Sjp161948 * This library is free for commercial and non-commercial use as long as 67*2139Sjp161948 * the following conditions are aheared to. The following conditions 68*2139Sjp161948 * apply to all code found in this distribution, be it the RC4, RSA, 69*2139Sjp161948 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 70*2139Sjp161948 * included with this distribution is covered by the same copyright terms 71*2139Sjp161948 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 72*2139Sjp161948 * 73*2139Sjp161948 * Copyright remains Eric Young's, and as such any Copyright notices in 74*2139Sjp161948 * the code are not to be removed. 75*2139Sjp161948 * If this package is used in a product, Eric Young should be given attribution 76*2139Sjp161948 * as the author of the parts of the library used. 77*2139Sjp161948 * This can be in the form of a textual message at program startup or 78*2139Sjp161948 * in documentation (online or textual) provided with the package. 79*2139Sjp161948 * 80*2139Sjp161948 * Redistribution and use in source and binary forms, with or without 81*2139Sjp161948 * modification, are permitted provided that the following conditions 82*2139Sjp161948 * are met: 83*2139Sjp161948 * 1. Redistributions of source code must retain the copyright 84*2139Sjp161948 * notice, this list of conditions and the following disclaimer. 85*2139Sjp161948 * 2. Redistributions in binary form must reproduce the above copyright 86*2139Sjp161948 * notice, this list of conditions and the following disclaimer in the 87*2139Sjp161948 * documentation and/or other materials provided with the distribution. 88*2139Sjp161948 * 3. All advertising materials mentioning features or use of this software 89*2139Sjp161948 * must display the following acknowledgement: 90*2139Sjp161948 * "This product includes cryptographic software written by 91*2139Sjp161948 * Eric Young (eay@cryptsoft.com)" 92*2139Sjp161948 * The word 'cryptographic' can be left out if the rouines from the library 93*2139Sjp161948 * being used are not cryptographic related :-). 94*2139Sjp161948 * 4. If you include any Windows specific code (or a derivative thereof) from 95*2139Sjp161948 * the apps directory (application code) you must include an acknowledgement: 96*2139Sjp161948 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 97*2139Sjp161948 * 98*2139Sjp161948 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 99*2139Sjp161948 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 100*2139Sjp161948 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 101*2139Sjp161948 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 102*2139Sjp161948 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 103*2139Sjp161948 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 104*2139Sjp161948 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 105*2139Sjp161948 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 106*2139Sjp161948 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 107*2139Sjp161948 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 108*2139Sjp161948 * SUCH DAMAGE. 109*2139Sjp161948 * 110*2139Sjp161948 * The licence and distribution terms for any publically available version or 111*2139Sjp161948 * derivative of this code cannot be changed. i.e. this code cannot simply be 112*2139Sjp161948 * copied and put under another distribution licence 113*2139Sjp161948 * [including the GNU Public Licence.] 114*2139Sjp161948 */ 115*2139Sjp161948 116*2139Sjp161948 #include <stdio.h> 117*2139Sjp161948 #include "ssl_locl.h" 118*2139Sjp161948 #include <openssl/comp.h> 119*2139Sjp161948 #include <openssl/evp.h> 120*2139Sjp161948 #include <openssl/hmac.h> 121*2139Sjp161948 #include <openssl/md5.h> 122*2139Sjp161948 #include <openssl/rand.h> 123*2139Sjp161948 124*2139Sjp161948 125*2139Sjp161948 int dtls1_enc(SSL *s, int send) 126*2139Sjp161948 { 127*2139Sjp161948 SSL3_RECORD *rec; 128*2139Sjp161948 EVP_CIPHER_CTX *ds; 129*2139Sjp161948 unsigned long l; 130*2139Sjp161948 int bs,i,ii,j,k,n=0; 131*2139Sjp161948 const EVP_CIPHER *enc; 132*2139Sjp161948 133*2139Sjp161948 if (send) 134*2139Sjp161948 { 135*2139Sjp161948 if (s->write_hash != NULL) 136*2139Sjp161948 n=EVP_MD_size(s->write_hash); 137*2139Sjp161948 ds=s->enc_write_ctx; 138*2139Sjp161948 rec= &(s->s3->wrec); 139*2139Sjp161948 if (s->enc_write_ctx == NULL) 140*2139Sjp161948 enc=NULL; 141*2139Sjp161948 else 142*2139Sjp161948 { 143*2139Sjp161948 enc=EVP_CIPHER_CTX_cipher(s->enc_write_ctx); 144*2139Sjp161948 if ( rec->data != rec->input) 145*2139Sjp161948 /* we can't write into the input stream */ 146*2139Sjp161948 fprintf(stderr, "%s:%d: rec->data != rec->input\n", 147*2139Sjp161948 __FILE__, __LINE__); 148*2139Sjp161948 else if ( EVP_CIPHER_block_size(ds->cipher) > 1) 149*2139Sjp161948 RAND_bytes(rec->input, EVP_CIPHER_block_size(ds->cipher)); 150*2139Sjp161948 } 151*2139Sjp161948 } 152*2139Sjp161948 else 153*2139Sjp161948 { 154*2139Sjp161948 if (s->read_hash != NULL) 155*2139Sjp161948 n=EVP_MD_size(s->read_hash); 156*2139Sjp161948 ds=s->enc_read_ctx; 157*2139Sjp161948 rec= &(s->s3->rrec); 158*2139Sjp161948 if (s->enc_read_ctx == NULL) 159*2139Sjp161948 enc=NULL; 160*2139Sjp161948 else 161*2139Sjp161948 enc=EVP_CIPHER_CTX_cipher(s->enc_read_ctx); 162*2139Sjp161948 } 163*2139Sjp161948 164*2139Sjp161948 #ifdef KSSL_DEBUG 165*2139Sjp161948 printf("dtls1_enc(%d)\n", send); 166*2139Sjp161948 #endif /* KSSL_DEBUG */ 167*2139Sjp161948 168*2139Sjp161948 if ((s->session == NULL) || (ds == NULL) || 169*2139Sjp161948 (enc == NULL)) 170*2139Sjp161948 { 171*2139Sjp161948 memmove(rec->data,rec->input,rec->length); 172*2139Sjp161948 rec->input=rec->data; 173*2139Sjp161948 } 174*2139Sjp161948 else 175*2139Sjp161948 { 176*2139Sjp161948 l=rec->length; 177*2139Sjp161948 bs=EVP_CIPHER_block_size(ds->cipher); 178*2139Sjp161948 179*2139Sjp161948 if ((bs != 1) && send) 180*2139Sjp161948 { 181*2139Sjp161948 i=bs-((int)l%bs); 182*2139Sjp161948 183*2139Sjp161948 /* Add weird padding of upto 256 bytes */ 184*2139Sjp161948 185*2139Sjp161948 /* we need to add 'i' padding bytes of value j */ 186*2139Sjp161948 j=i-1; 187*2139Sjp161948 if (s->options & SSL_OP_TLS_BLOCK_PADDING_BUG) 188*2139Sjp161948 { 189*2139Sjp161948 if (s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG) 190*2139Sjp161948 j++; 191*2139Sjp161948 } 192*2139Sjp161948 for (k=(int)l; k<(int)(l+i); k++) 193*2139Sjp161948 rec->input[k]=j; 194*2139Sjp161948 l+=i; 195*2139Sjp161948 rec->length+=i; 196*2139Sjp161948 } 197*2139Sjp161948 198*2139Sjp161948 #ifdef KSSL_DEBUG 199*2139Sjp161948 { 200*2139Sjp161948 unsigned long ui; 201*2139Sjp161948 printf("EVP_Cipher(ds=%p,rec->data=%p,rec->input=%p,l=%ld) ==>\n", 202*2139Sjp161948 ds,rec->data,rec->input,l); 203*2139Sjp161948 printf("\tEVP_CIPHER_CTX: %d buf_len, %d key_len [%d %d], %d iv_len\n", 204*2139Sjp161948 ds->buf_len, ds->cipher->key_len, 205*2139Sjp161948 DES_KEY_SZ, DES_SCHEDULE_SZ, 206*2139Sjp161948 ds->cipher->iv_len); 207*2139Sjp161948 printf("\t\tIV: "); 208*2139Sjp161948 for (i=0; i<ds->cipher->iv_len; i++) printf("%02X", ds->iv[i]); 209*2139Sjp161948 printf("\n"); 210*2139Sjp161948 printf("\trec->input="); 211*2139Sjp161948 for (ui=0; ui<l; ui++) printf(" %02x", rec->input[ui]); 212*2139Sjp161948 printf("\n"); 213*2139Sjp161948 } 214*2139Sjp161948 #endif /* KSSL_DEBUG */ 215*2139Sjp161948 216*2139Sjp161948 if (!send) 217*2139Sjp161948 { 218*2139Sjp161948 if (l == 0 || l%bs != 0) 219*2139Sjp161948 { 220*2139Sjp161948 SSLerr(SSL_F_DTLS1_ENC,SSL_R_BLOCK_CIPHER_PAD_IS_WRONG); 221*2139Sjp161948 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECRYPTION_FAILED); 222*2139Sjp161948 return 0; 223*2139Sjp161948 } 224*2139Sjp161948 } 225*2139Sjp161948 226*2139Sjp161948 EVP_Cipher(ds,rec->data,rec->input,l); 227*2139Sjp161948 228*2139Sjp161948 #ifdef KSSL_DEBUG 229*2139Sjp161948 { 230*2139Sjp161948 unsigned long i; 231*2139Sjp161948 printf("\trec->data="); 232*2139Sjp161948 for (i=0; i<l; i++) 233*2139Sjp161948 printf(" %02x", rec->data[i]); printf("\n"); 234*2139Sjp161948 } 235*2139Sjp161948 #endif /* KSSL_DEBUG */ 236*2139Sjp161948 237*2139Sjp161948 if ((bs != 1) && !send) 238*2139Sjp161948 { 239*2139Sjp161948 ii=i=rec->data[l-1]; /* padding_length */ 240*2139Sjp161948 i++; 241*2139Sjp161948 if (s->options&SSL_OP_TLS_BLOCK_PADDING_BUG) 242*2139Sjp161948 { 243*2139Sjp161948 /* First packet is even in size, so check */ 244*2139Sjp161948 if ((memcmp(s->s3->read_sequence, 245*2139Sjp161948 "\0\0\0\0\0\0\0\0",8) == 0) && !(ii & 1)) 246*2139Sjp161948 s->s3->flags|=TLS1_FLAGS_TLS_PADDING_BUG; 247*2139Sjp161948 if (s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG) 248*2139Sjp161948 i--; 249*2139Sjp161948 } 250*2139Sjp161948 /* TLS 1.0 does not bound the number of padding bytes by the block size. 251*2139Sjp161948 * All of them must have value 'padding_length'. */ 252*2139Sjp161948 if (i > (int)rec->length) 253*2139Sjp161948 { 254*2139Sjp161948 /* Incorrect padding. SSLerr() and ssl3_alert are done 255*2139Sjp161948 * by caller: we don't want to reveal whether this is 256*2139Sjp161948 * a decryption error or a MAC verification failure 257*2139Sjp161948 * (see http://www.openssl.org/~bodo/tls-cbc.txt) 258*2139Sjp161948 */ 259*2139Sjp161948 return -1; 260*2139Sjp161948 } 261*2139Sjp161948 for (j=(int)(l-i); j<(int)l; j++) 262*2139Sjp161948 { 263*2139Sjp161948 if (rec->data[j] != ii) 264*2139Sjp161948 { 265*2139Sjp161948 /* Incorrect padding */ 266*2139Sjp161948 return -1; 267*2139Sjp161948 } 268*2139Sjp161948 } 269*2139Sjp161948 rec->length-=i; 270*2139Sjp161948 271*2139Sjp161948 rec->data += bs; /* skip the implicit IV */ 272*2139Sjp161948 rec->input += bs; 273*2139Sjp161948 rec->length -= bs; 274*2139Sjp161948 } 275*2139Sjp161948 } 276*2139Sjp161948 return(1); 277*2139Sjp161948 } 278*2139Sjp161948 279