]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/t1_enc.c
Massive constification.
[thirdparty/openssl.git] / ssl / t1_enc.c
CommitLineData
58964a49
RE
1/* ssl/t1_enc.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
413c4f45 60#include "comp.h"
58964a49
RE
61#include "evp.h"
62#include "hmac.h"
63#include "ssl_locl.h"
64
65static void tls1_P_hash(md,sec,sec_len,seed,seed_len,out,olen)
66EVP_MD *md;
67unsigned char *sec;
68int sec_len;
69unsigned char *seed;
70int seed_len;
71unsigned char *out;
72int olen;
73 {
74 int chunk,n;
75 unsigned int j;
76 HMAC_CTX ctx;
77 HMAC_CTX ctx_tmp;
78 unsigned char A1[HMAC_MAX_MD_CBLOCK];
79 unsigned int A1_len;
80
81 chunk=EVP_MD_size(md);
82
83 HMAC_Init(&ctx,sec,sec_len,md);
84 HMAC_Update(&ctx,seed,seed_len);
85 HMAC_Final(&ctx,A1,&A1_len);
86
87 n=0;
88 for (;;)
89 {
90 HMAC_Init(&ctx,NULL,0,NULL); /* re-init */
91 HMAC_Update(&ctx,A1,A1_len);
92 memcpy(&ctx_tmp,&ctx,sizeof(ctx)); /* Copy for A2 */ /* not needed for last one */
93 HMAC_Update(&ctx,seed,seed_len);
94
95 if (olen > chunk)
96 {
97 HMAC_Final(&ctx,out,&j);
98 out+=j;
99 olen-=j;
100 HMAC_Final(&ctx_tmp,A1,&A1_len); /* calc the next A1 value */
101 }
102 else /* last one */
103 {
104 HMAC_Final(&ctx,A1,&A1_len);
105 memcpy(out,A1,olen);
106 break;
107 }
108 }
109 HMAC_cleanup(&ctx);
110 HMAC_cleanup(&ctx_tmp);
111 memset(A1,0,sizeof(A1));
112 }
113
114static void tls1_PRF(md5,sha1,label,label_len,sec,slen,out1,out2,olen)
115EVP_MD *md5;
116EVP_MD *sha1;
117unsigned char *label;
118int label_len;
119unsigned char *sec;
120int slen;
121unsigned char *out1;
122unsigned char *out2;
123int olen;
124 {
125 int len,i;
126 unsigned char *S1,*S2;
127
128 len=slen/2;
129 S1=sec;
130 S2= &(sec[len]);
131 len+=(slen&1); /* add for odd, make longer */
132
133
134 tls1_P_hash(md5 ,S1,len,label,label_len,out1,olen);
135 tls1_P_hash(sha1,S2,len,label,label_len,out2,olen);
136
137 for (i=0; i<olen; i++)
138 out1[i]^=out2[i];
139 }
140
141static void tls1_generate_key_block(s,km,tmp,num)
142SSL *s;
143unsigned char *km,*tmp;
144int num;
145 {
146 unsigned char *p;
147 unsigned char buf[SSL3_RANDOM_SIZE*2+
148 TLS_MD_MAX_CONST_SIZE];
149 p=buf;
150
151 memcpy(p,TLS_MD_KEY_EXPANSION_CONST,
152 TLS_MD_KEY_EXPANSION_CONST_SIZE);
153 p+=TLS_MD_KEY_EXPANSION_CONST_SIZE;
154 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
155 p+=SSL3_RANDOM_SIZE;
156 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
157 p+=SSL3_RANDOM_SIZE;
158
dfeab068 159 tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(p-buf),
58964a49
RE
160 s->session->master_key,s->session->master_key_length,
161 km,tmp,num);
162 }
163
164int tls1_change_cipher_state(s,which)
165SSL *s;
166int which;
167 {
168 unsigned char *p,*key_block,*mac_secret;
169 unsigned char *exp_label,buf[TLS_MD_MAX_CONST_SIZE+
170 SSL3_RANDOM_SIZE*2];
171 unsigned char tmp1[EVP_MAX_KEY_LENGTH];
172 unsigned char tmp2[EVP_MAX_KEY_LENGTH];
173 unsigned char iv1[EVP_MAX_IV_LENGTH*2];
174 unsigned char iv2[EVP_MAX_IV_LENGTH*2];
175 unsigned char *ms,*key,*iv,*er1,*er2;
176 int client_write;
177 EVP_CIPHER_CTX *dd;
e778802f
BL
178 const EVP_CIPHER *c;
179 const SSL_COMP *comp;
180 const EVP_MD *m;
06ab81f9 181 int _exp,n,i,j,k,exp_label_len,cl;
58964a49 182
06ab81f9 183 _exp=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher);
58964a49
RE
184 c=s->s3->tmp.new_sym_enc;
185 m=s->s3->tmp.new_hash;
186 comp=s->s3->tmp.new_compression;
187 key_block=s->s3->tmp.key_block;
188
189 if (which & SSL3_CC_READ)
190 {
191 if ((s->enc_read_ctx == NULL) &&
192 ((s->enc_read_ctx=(EVP_CIPHER_CTX *)
193 Malloc(sizeof(EVP_CIPHER_CTX))) == NULL))
194 goto err;
195 dd= s->enc_read_ctx;
196 s->read_hash=m;
dfeab068
RE
197 if (s->expand != NULL)
198 {
199 COMP_CTX_free(s->expand);
200 s->expand=NULL;
201 }
202 if (comp != NULL)
203 {
413c4f45 204 s->expand=COMP_CTX_new(comp->method);
dfeab068
RE
205 if (s->expand == NULL)
206 {
207 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,SSL_R_COMPRESSION_LIBRARY_ERROR);
208 goto err2;
209 }
413c4f45
MC
210 if (s->s3->rrec.comp == NULL)
211 s->s3->rrec.comp=(unsigned char *)
212 Malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
dfeab068
RE
213 if (s->s3->rrec.comp == NULL)
214 goto err;
215 }
58964a49
RE
216 memset(&(s->s3->read_sequence[0]),0,8);
217 mac_secret= &(s->s3->read_mac_secret[0]);
218 }
219 else
220 {
221 if ((s->enc_write_ctx == NULL) &&
222 ((s->enc_write_ctx=(EVP_CIPHER_CTX *)
223 Malloc(sizeof(EVP_CIPHER_CTX))) == NULL))
224 goto err;
225 dd= s->enc_write_ctx;
226 s->write_hash=m;
dfeab068
RE
227 if (s->compress != NULL)
228 {
229 COMP_CTX_free(s->compress);
230 s->compress=NULL;
231 }
232 if (comp != NULL)
233 {
413c4f45 234 s->compress=COMP_CTX_new(comp->method);
dfeab068
RE
235 if (s->compress == NULL)
236 {
237 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,SSL_R_COMPRESSION_LIBRARY_ERROR);
238 goto err2;
239 }
240 }
58964a49
RE
241 memset(&(s->s3->write_sequence[0]),0,8);
242 mac_secret= &(s->s3->write_mac_secret[0]);
243 }
244
245 EVP_CIPHER_CTX_init(dd);
246
247 p=s->s3->tmp.key_block;
248 i=EVP_MD_size(m);
436d318c 249 cl=EVP_CIPHER_key_length(c);
06ab81f9
BL
250 j=_exp ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ?
251 cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl;
436d318c 252 /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */
58964a49
RE
253 k=EVP_CIPHER_iv_length(c);
254 er1= &(s->s3->client_random[0]);
255 er2= &(s->s3->server_random[0]);
256 if ( (which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
257 (which == SSL3_CHANGE_CIPHER_SERVER_READ))
258 {
259 ms= &(p[ 0]); n=i+i;
260 key= &(p[ n]); n+=j+j;
261 iv= &(p[ n]); n+=k+k;
262 exp_label=(unsigned char *)TLS_MD_CLIENT_WRITE_KEY_CONST;
263 exp_label_len=TLS_MD_CLIENT_WRITE_KEY_CONST_SIZE;
264 client_write=1;
265 }
266 else
267 {
268 n=i;
269 ms= &(p[ n]); n+=i+j;
270 key= &(p[ n]); n+=j+k;
271 iv= &(p[ n]); n+=k;
272 exp_label=(unsigned char *)TLS_MD_SERVER_WRITE_KEY_CONST;
273 exp_label_len=TLS_MD_SERVER_WRITE_KEY_CONST_SIZE;
274 client_write=0;
275 }
276
277 if (n > s->s3->tmp.key_block_length)
278 {
279 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,SSL_R_INTERNAL_ERROR);
280 goto err2;
281 }
282
283 memcpy(mac_secret,ms,i);
284#ifdef TLS_DEBUG
285printf("which = %04X\nmac key=",which);
286{ int z; for (z=0; z<i; z++) printf("%02X%c",ms[z],((z+1)%16)?' ':'\n'); }
287#endif
06ab81f9 288 if (_exp)
58964a49
RE
289 {
290 /* In here I set both the read and write key/iv to the
291 * same value since only the correct one will be used :-).
292 */
293 p=buf;
294 memcpy(p,exp_label,exp_label_len);
295 p+=exp_label_len;
296 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
297 p+=SSL3_RANDOM_SIZE;
298 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
299 p+=SSL3_RANDOM_SIZE;
dfeab068 300 tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(p-buf),key,j,
06ab81f9 301 tmp1,tmp2,EVP_CIPHER_key_length(c));
58964a49
RE
302 key=tmp1;
303
304 if (k > 0)
305 {
306 p=buf;
307 memcpy(p,TLS_MD_IV_BLOCK_CONST,
308 TLS_MD_IV_BLOCK_CONST_SIZE);
309 p+=TLS_MD_IV_BLOCK_CONST_SIZE;
310 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
311 p+=SSL3_RANDOM_SIZE;
312 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
313 p+=SSL3_RANDOM_SIZE;
314 tls1_PRF(s->ctx->md5,s->ctx->sha1,
dfeab068 315 buf,(int)(p-buf),"",0,iv1,iv2,k*2);
58964a49
RE
316 if (client_write)
317 iv=iv1;
318 else
319 iv= &(iv1[k]);
320 }
321 }
322
323 s->session->key_arg_length=0;
324
325 EVP_CipherInit(dd,c,key,iv,(which & SSL3_CC_WRITE));
326#ifdef TLS_DEBUG
327printf("which = %04X\nkey=",which);
328{ int z; for (z=0; z<EVP_CIPHER_key_length(c); z++) printf("%02X%c",key[z],((z+1)%16)?' ':'\n'); }
329printf("\niv=");
330{ int z; for (z=0; z<k; z++) printf("%02X%c",iv[z],((z+1)%16)?' ':'\n'); }
331printf("\n");
332#endif
333
334 memset(tmp1,0,sizeof(tmp1));
335 memset(tmp2,0,sizeof(tmp1));
336 memset(iv1,0,sizeof(iv1));
337 memset(iv2,0,sizeof(iv2));
338 return(1);
339err:
340 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,ERR_R_MALLOC_FAILURE);
341err2:
342 return(0);
343 }
344
345int tls1_setup_key_block(s)
346SSL *s;
347 {
348 unsigned char *p1,*p2;
e778802f
BL
349 const EVP_CIPHER *c;
350 const EVP_MD *hash;
06ab81f9 351 int num;
413c4f45 352 SSL_COMP *comp;
58964a49
RE
353
354 if (s->s3->tmp.key_block_length != 0)
355 return(1);
356
413c4f45 357 if (!ssl_cipher_get_evp(s->session,&c,&hash,&comp))
58964a49
RE
358 {
359 SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
360 return(0);
361 }
362
363 s->s3->tmp.new_sym_enc=c;
364 s->s3->tmp.new_hash=hash;
365
58964a49
RE
366 num=EVP_CIPHER_key_length(c)+EVP_MD_size(hash)+EVP_CIPHER_iv_length(c);
367 num*=2;
368
369 ssl3_cleanup_key_block(s);
370
371 if ((p1=(unsigned char *)Malloc(num)) == NULL)
372 goto err;
373 if ((p2=(unsigned char *)Malloc(num)) == NULL)
374 goto err;
375
376 s->s3->tmp.key_block_length=num;
377 s->s3->tmp.key_block=p1;
378
379
380#ifdef TLS_DEBUG
381printf("client random\n");
382{ int z; for (z=0; z<SSL3_RANDOM_SIZE; z++) printf("%02X%c",s->s3->client_random[z],((z+1)%16)?' ':'\n'); }
383printf("server random\n");
384{ int z; for (z=0; z<SSL3_RANDOM_SIZE; z++) printf("%02X%c",s->s3->server_random[z],((z+1)%16)?' ':'\n'); }
385printf("pre-master\n");
386{ int z; for (z=0; z<s->session->master_key_length; z++) printf("%02X%c",s->session->master_key[z],((z+1)%16)?' ':'\n'); }
387#endif
388 tls1_generate_key_block(s,p1,p2,num);
389 memset(p2,0,num);
390 Free(p2);
391#ifdef TLS_DEBUG
392printf("\nkey block\n");
393{ int z; for (z=0; z<num; z++) printf("%02X%c",p1[z],((z+1)%16)?' ':'\n'); }
394#endif
395
396 return(1);
397err:
398 SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,ERR_R_MALLOC_FAILURE);
399 return(0);
400 }
401
402int tls1_enc(s,send)
403SSL *s;
404int send;
405 {
406 SSL3_RECORD *rec;
407 EVP_CIPHER_CTX *ds;
408 unsigned long l;
409 int bs,i,ii,j,k,n=0;
e778802f 410 const EVP_CIPHER *enc;
58964a49
RE
411
412 if (send)
413 {
414 if (s->write_hash != NULL)
415 n=EVP_MD_size(s->write_hash);
416 ds=s->enc_write_ctx;
417 rec= &(s->s3->wrec);
418 if (s->enc_write_ctx == NULL)
dfeab068 419 enc=NULL;
58964a49 420 else
58964a49 421 enc=EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
58964a49
RE
422 }
423 else
424 {
425 if (s->read_hash != NULL)
426 n=EVP_MD_size(s->read_hash);
427 ds=s->enc_read_ctx;
428 rec= &(s->s3->rrec);
429 if (s->enc_read_ctx == NULL)
dfeab068 430 enc=NULL;
58964a49 431 else
58964a49 432 enc=EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
58964a49
RE
433 }
434
435 if ((s->session == NULL) || (ds == NULL) ||
dfeab068 436 (enc == NULL))
58964a49
RE
437 {
438 memcpy(rec->data,rec->input,rec->length);
439 rec->input=rec->data;
440 }
441 else
442 {
443 l=rec->length;
444 bs=EVP_CIPHER_block_size(ds->cipher);
445
446 if ((bs != 1) && send)
447 {
448 i=bs-((int)l%bs);
449
450 /* Add weird padding of upto 256 bytes */
451
452 /* we need to add 'i' padding bytes of value j */
453 j=i-1;
454 if (s->options & SSL_OP_TLS_BLOCK_PADDING_BUG)
455 {
456 if (s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG)
457 j++;
458 }
459 for (k=(int)l; k<(int)(l+i); k++)
460 rec->input[k]=j;
461 l+=i;
462 rec->length+=i;
463 }
464
465 EVP_Cipher(ds,rec->data,rec->input,l);
466
467 if ((bs != 1) && !send)
468 {
469 ii=i=rec->data[l-1];
470 i++;
471 if (s->options&SSL_OP_TLS_BLOCK_PADDING_BUG)
472 {
473 /* First packet is even in size, so check */
474 if ((memcmp(s->s3->read_sequence,
475 "\0\0\0\0\0\0\0\0",8) == 0) && !(ii & 1))
476 s->s3->flags|=TLS1_FLAGS_TLS_PADDING_BUG;
477 if (s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG)
478 i--;
479 }
480 if (i > (int)rec->length)
481 {
482 SSLerr(SSL_F_TLS1_ENC,SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
483 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECRYPTION_FAILED);
484 return(0);
485 }
486 for (j=(int)(l-i); j<(int)l; j++)
487 {
488 if (rec->data[j] != ii)
489 {
490 SSLerr(SSL_F_TLS1_ENC,SSL_R_DECRYPTION_FAILED);
491 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECRYPTION_FAILED);
492 return(0);
493 }
494 }
495 rec->length-=i;
496 }
497 }
498 return(1);
499 }
500
501int tls1_cert_verify_mac(s,in_ctx,out)
502SSL *s;
503EVP_MD_CTX *in_ctx;
504unsigned char *out;
505 {
506 unsigned int ret;
507 EVP_MD_CTX ctx;
508
413c4f45 509 EVP_MD_CTX_copy(&ctx,in_ctx);
58964a49
RE
510 EVP_DigestFinal(&ctx,out,&ret);
511 return((int)ret);
512 }
513
514int tls1_final_finish_mac(s,in1_ctx,in2_ctx,str,slen,out)
515SSL *s;
516EVP_MD_CTX *in1_ctx,*in2_ctx;
517unsigned char *str;
518int slen;
519unsigned char *out;
520 {
521 unsigned int i;
522 EVP_MD_CTX ctx;
523 unsigned char buf[TLS_MD_MAX_CONST_SIZE+MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
524 unsigned char *q,buf2[12];
525
526 q=buf;
527 memcpy(q,str,slen);
528 q+=slen;
529
413c4f45 530 EVP_MD_CTX_copy(&ctx,in1_ctx);
58964a49
RE
531 EVP_DigestFinal(&ctx,q,&i);
532 q+=i;
413c4f45 533 EVP_MD_CTX_copy(&ctx,in2_ctx);
58964a49
RE
534 EVP_DigestFinal(&ctx,q,&i);
535 q+=i;
536
dfeab068 537 tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(q-buf),
58964a49
RE
538 s->session->master_key,s->session->master_key_length,
539 out,buf2,12);
540 memset(&ctx,0,sizeof(EVP_MD_CTX));
541
542 return((int)12);
543 }
544
545int tls1_mac(ssl,md,send)
546SSL *ssl;
547unsigned char *md;
548int send;
549 {
550 SSL3_RECORD *rec;
551 unsigned char *mac_sec,*seq;
e778802f 552 const EVP_MD *hash;
58964a49
RE
553 unsigned int md_size;
554 int i;
555 HMAC_CTX hmac;
556 unsigned char buf[5];
557
558 if (send)
559 {
560 rec= &(ssl->s3->wrec);
561 mac_sec= &(ssl->s3->write_mac_secret[0]);
562 seq= &(ssl->s3->write_sequence[0]);
563 hash=ssl->write_hash;
564 }
565 else
566 {
567 rec= &(ssl->s3->rrec);
568 mac_sec= &(ssl->s3->read_mac_secret[0]);
569 seq= &(ssl->s3->read_sequence[0]);
570 hash=ssl->read_hash;
571 }
572
573 md_size=EVP_MD_size(hash);
574
575 buf[0]=rec->type;
576 buf[1]=TLS1_VERSION_MAJOR;
577 buf[2]=TLS1_VERSION_MINOR;
578 buf[3]=rec->length>>8;
579 buf[4]=rec->length&0xff;
580
581 /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
582 HMAC_Init(&hmac,mac_sec,EVP_MD_size(hash),hash);
583 HMAC_Update(&hmac,seq,8);
584 HMAC_Update(&hmac,buf,5);
585 HMAC_Update(&hmac,rec->input,rec->length);
586 HMAC_Final(&hmac,md,&md_size);
587
588#ifdef TLS_DEBUG
589printf("sec=");
dfeab068 590{unsigned int z; for (z=0; z<md_size; z++) printf("%02X ",mac_sec[z]); printf("\n"); }
58964a49
RE
591printf("seq=");
592{int z; for (z=0; z<8; z++) printf("%02X ",seq[z]); printf("\n"); }
593printf("buf=");
594{int z; for (z=0; z<5; z++) printf("%02X ",buf[z]); printf("\n"); }
595printf("rec=");
dfeab068 596{unsigned int z; for (z=0; z<rec->length; z++) printf("%02X ",buf[z]); printf("\n"); }
58964a49
RE
597#endif
598
599 for (i=7; i>=0; i--)
600 if (++seq[i]) break;
601
602#ifdef TLS_DEBUG
dfeab068 603{unsigned int z; for (z=0; z<md_size; z++) printf("%02X ",md[z]); printf("\n"); }
58964a49
RE
604#endif
605 return(md_size);
606 }
607
608int tls1_generate_master_secret(s,out,p,len)
609SSL *s;
610unsigned char *out;
611unsigned char *p;
612int len;
613 {
614 unsigned char buf[SSL3_RANDOM_SIZE*2+TLS_MD_MASTER_SECRET_CONST_SIZE];
615 unsigned char buff[SSL_MAX_MASTER_KEY_LENGTH];
616
617 /* Setup the stuff to munge */
618 memcpy(buf,TLS_MD_MASTER_SECRET_CONST,
619 TLS_MD_MASTER_SECRET_CONST_SIZE);
620 memcpy(&(buf[TLS_MD_MASTER_SECRET_CONST_SIZE]),
621 s->s3->client_random,SSL3_RANDOM_SIZE);
622 memcpy(&(buf[SSL3_RANDOM_SIZE+TLS_MD_MASTER_SECRET_CONST_SIZE]),
623 s->s3->server_random,SSL3_RANDOM_SIZE);
624 tls1_PRF(s->ctx->md5,s->ctx->sha1,
625 buf,TLS_MD_MASTER_SECRET_CONST_SIZE+SSL3_RANDOM_SIZE*2,p,len,
626 s->session->master_key,buff,SSL3_MASTER_SECRET_SIZE);
627 return(SSL3_MASTER_SECRET_SIZE);
628 }
629
630int tls1_alert_code(code)
631int code;
632 {
633 switch (code)
634 {
635 case SSL_AD_CLOSE_NOTIFY: return(SSL3_AD_CLOSE_NOTIFY);
636 case SSL_AD_UNEXPECTED_MESSAGE: return(SSL3_AD_UNEXPECTED_MESSAGE);
637 case SSL_AD_BAD_RECORD_MAC: return(SSL3_AD_BAD_RECORD_MAC);
638 case SSL_AD_DECRYPTION_FAILED: return(TLS1_AD_DECRYPTION_FAILED);
639 case SSL_AD_RECORD_OVERFLOW: return(TLS1_AD_RECORD_OVERFLOW);
640 case SSL_AD_DECOMPRESSION_FAILURE:return(SSL3_AD_DECOMPRESSION_FAILURE);
641 case SSL_AD_HANDSHAKE_FAILURE: return(SSL3_AD_HANDSHAKE_FAILURE);
642 case SSL_AD_NO_CERTIFICATE: return(-1);
643 case SSL_AD_BAD_CERTIFICATE: return(SSL3_AD_BAD_CERTIFICATE);
644 case SSL_AD_UNSUPPORTED_CERTIFICATE:return(SSL3_AD_UNSUPPORTED_CERTIFICATE);
645 case SSL_AD_CERTIFICATE_REVOKED:return(SSL3_AD_CERTIFICATE_REVOKED);
646 case SSL_AD_CERTIFICATE_EXPIRED:return(SSL3_AD_CERTIFICATE_EXPIRED);
647 case SSL_AD_CERTIFICATE_UNKNOWN:return(SSL3_AD_CERTIFICATE_UNKNOWN);
648 case SSL_AD_ILLEGAL_PARAMETER: return(SSL3_AD_ILLEGAL_PARAMETER);
649 case SSL_AD_UNKNOWN_CA: return(TLS1_AD_UNKNOWN_CA);
650 case SSL_AD_ACCESS_DENIED: return(TLS1_AD_ACCESS_DENIED);
651 case SSL_AD_DECODE_ERROR: return(TLS1_AD_DECODE_ERROR);
652 case SSL_AD_DECRYPT_ERROR: return(TLS1_AD_DECRYPT_ERROR);
653 case SSL_AD_EXPORT_RESTRICION: return(TLS1_AD_EXPORT_RESTRICION);
654 case SSL_AD_PROTOCOL_VERSION: return(TLS1_AD_PROTOCOL_VERSION);
655 case SSL_AD_INSUFFICIENT_SECURITY:return(TLS1_AD_INSUFFICIENT_SECURITY);
656 case SSL_AD_INTERNAL_ERROR: return(TLS1_AD_INTERNAL_ERROR);
657 case SSL_AD_USER_CANCLED: return(TLS1_AD_USER_CANCLED);
658 case SSL_AD_NO_RENEGOTIATION: return(TLS1_AD_NO_RENEGOTIATION);
659 default: return(-1);
660 }
661 }
662