summaryrefslogtreecommitdiff
path: root/src/lib/libssl/t1_enc.c
diff options
context:
space:
mode:
authorbeck <>1999-09-29 04:37:45 +0000
committerbeck <>1999-09-29 04:37:45 +0000
commitde8f24ea083384bb66b32ec105dc4743c5663cdf (patch)
tree1412176ae62a3cab2cf2b0b92150fcbceaac6092 /src/lib/libssl/t1_enc.c
parentcb929d29896bcb87c2a97417fbd03e50078fc178 (diff)
downloadopenbsd-de8f24ea083384bb66b32ec105dc4743c5663cdf.tar.gz
openbsd-de8f24ea083384bb66b32ec105dc4743c5663cdf.tar.bz2
openbsd-de8f24ea083384bb66b32ec105dc4743c5663cdf.zip
OpenSSL 0.9.4 merge
Diffstat (limited to 'src/lib/libssl/t1_enc.c')
-rw-r--r--src/lib/libssl/t1_enc.c188
1 files changed, 93 insertions, 95 deletions
diff --git a/src/lib/libssl/t1_enc.c b/src/lib/libssl/t1_enc.c
index fbdd3bffb5..914b743498 100644
--- a/src/lib/libssl/t1_enc.c
+++ b/src/lib/libssl/t1_enc.c
@@ -57,18 +57,16 @@
57 */ 57 */
58 58
59#include <stdio.h> 59#include <stdio.h>
60#include "evp.h" 60#include <openssl/comp.h>
61#include "hmac.h" 61#include <openssl/md5.h>
62#include <openssl/sha.h>
63#include <openssl/evp.h>
64#include <openssl/hmac.h>
62#include "ssl_locl.h" 65#include "ssl_locl.h"
63 66
64static void tls1_P_hash(md,sec,sec_len,seed,seed_len,out,olen) 67static void tls1_P_hash(const EVP_MD *md, const unsigned char *sec,
65EVP_MD *md; 68 int sec_len, unsigned char *seed, int seed_len,
66unsigned char *sec; 69 unsigned char *out, int olen)
67int sec_len;
68unsigned char *seed;
69int seed_len;
70unsigned char *out;
71int olen;
72 { 70 {
73 int chunk,n; 71 int chunk,n;
74 unsigned int j; 72 unsigned int j;
@@ -110,19 +108,13 @@ int olen;
110 memset(A1,0,sizeof(A1)); 108 memset(A1,0,sizeof(A1));
111 } 109 }
112 110
113static void tls1_PRF(md5,sha1,label,label_len,sec,slen,out1,out2,olen) 111static void tls1_PRF(const EVP_MD *md5, const EVP_MD *sha1,
114EVP_MD *md5; 112 unsigned char *label, int label_len,
115EVP_MD *sha1; 113 const unsigned char *sec, int slen, unsigned char *out1,
116unsigned char *label; 114 unsigned char *out2, int olen)
117int label_len;
118unsigned char *sec;
119int slen;
120unsigned char *out1;
121unsigned char *out2;
122int olen;
123 { 115 {
124 int len,i; 116 int len,i;
125 unsigned char *S1,*S2; 117 const unsigned char *S1,*S2;
126 118
127 len=slen/2; 119 len=slen/2;
128 S1=sec; 120 S1=sec;
@@ -137,10 +129,8 @@ int olen;
137 out1[i]^=out2[i]; 129 out1[i]^=out2[i];
138 } 130 }
139 131
140static void tls1_generate_key_block(s,km,tmp,num) 132static void tls1_generate_key_block(SSL *s, unsigned char *km,
141SSL *s; 133 unsigned char *tmp, int num)
142unsigned char *km,*tmp;
143int num;
144 { 134 {
145 unsigned char *p; 135 unsigned char *p;
146 unsigned char buf[SSL3_RANDOM_SIZE*2+ 136 unsigned char buf[SSL3_RANDOM_SIZE*2+
@@ -155,15 +145,14 @@ int num;
155 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE); 145 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
156 p+=SSL3_RANDOM_SIZE; 146 p+=SSL3_RANDOM_SIZE;
157 147
158 tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,p-buf, 148 tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(p-buf),
159 s->session->master_key,s->session->master_key_length, 149 s->session->master_key,s->session->master_key_length,
160 km,tmp,num); 150 km,tmp,num);
161 } 151 }
162 152
163int tls1_change_cipher_state(s,which) 153int tls1_change_cipher_state(SSL *s, int which)
164SSL *s;
165int which;
166 { 154 {
155 static const unsigned char empty[]="";
167 unsigned char *p,*key_block,*mac_secret; 156 unsigned char *p,*key_block,*mac_secret;
168 unsigned char *exp_label,buf[TLS_MD_MAX_CONST_SIZE+ 157 unsigned char *exp_label,buf[TLS_MD_MAX_CONST_SIZE+
169 SSL3_RANDOM_SIZE*2]; 158 SSL3_RANDOM_SIZE*2];
@@ -174,12 +163,12 @@ int which;
174 unsigned char *ms,*key,*iv,*er1,*er2; 163 unsigned char *ms,*key,*iv,*er1,*er2;
175 int client_write; 164 int client_write;
176 EVP_CIPHER_CTX *dd; 165 EVP_CIPHER_CTX *dd;
177 EVP_CIPHER *c; 166 const EVP_CIPHER *c;
178 SSL_COMPRESSION *comp; 167 const SSL_COMP *comp;
179 EVP_MD *m; 168 const EVP_MD *m;
180 int exp,n,i,j,k,exp_label_len; 169 int _exp,n,i,j,k,exp_label_len,cl;
181 170
182 exp=(s->s3->tmp.new_cipher->algorithms & SSL_EXPORT)?1:0; 171 _exp=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher);
183 c=s->s3->tmp.new_sym_enc; 172 c=s->s3->tmp.new_sym_enc;
184 m=s->s3->tmp.new_hash; 173 m=s->s3->tmp.new_hash;
185 comp=s->s3->tmp.new_compression; 174 comp=s->s3->tmp.new_compression;
@@ -193,7 +182,25 @@ int which;
193 goto err; 182 goto err;
194 dd= s->enc_read_ctx; 183 dd= s->enc_read_ctx;
195 s->read_hash=m; 184 s->read_hash=m;
196 s->read_compression=comp; 185 if (s->expand != NULL)
186 {
187 COMP_CTX_free(s->expand);
188 s->expand=NULL;
189 }
190 if (comp != NULL)
191 {
192 s->expand=COMP_CTX_new(comp->method);
193 if (s->expand == NULL)
194 {
195 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,SSL_R_COMPRESSION_LIBRARY_ERROR);
196 goto err2;
197 }
198 if (s->s3->rrec.comp == NULL)
199 s->s3->rrec.comp=(unsigned char *)
200 Malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
201 if (s->s3->rrec.comp == NULL)
202 goto err;
203 }
197 memset(&(s->s3->read_sequence[0]),0,8); 204 memset(&(s->s3->read_sequence[0]),0,8);
198 mac_secret= &(s->s3->read_mac_secret[0]); 205 mac_secret= &(s->s3->read_mac_secret[0]);
199 } 206 }
@@ -205,7 +212,20 @@ int which;
205 goto err; 212 goto err;
206 dd= s->enc_write_ctx; 213 dd= s->enc_write_ctx;
207 s->write_hash=m; 214 s->write_hash=m;
208 s->write_compression=comp; 215 if (s->compress != NULL)
216 {
217 COMP_CTX_free(s->compress);
218 s->compress=NULL;
219 }
220 if (comp != NULL)
221 {
222 s->compress=COMP_CTX_new(comp->method);
223 if (s->compress == NULL)
224 {
225 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,SSL_R_COMPRESSION_LIBRARY_ERROR);
226 goto err2;
227 }
228 }
209 memset(&(s->s3->write_sequence[0]),0,8); 229 memset(&(s->s3->write_sequence[0]),0,8);
210 mac_secret= &(s->s3->write_mac_secret[0]); 230 mac_secret= &(s->s3->write_mac_secret[0]);
211 } 231 }
@@ -214,7 +234,10 @@ int which;
214 234
215 p=s->s3->tmp.key_block; 235 p=s->s3->tmp.key_block;
216 i=EVP_MD_size(m); 236 i=EVP_MD_size(m);
217 j=(exp)?5:EVP_CIPHER_key_length(c); 237 cl=EVP_CIPHER_key_length(c);
238 j=_exp ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ?
239 cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl;
240 /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */
218 k=EVP_CIPHER_iv_length(c); 241 k=EVP_CIPHER_iv_length(c);
219 er1= &(s->s3->client_random[0]); 242 er1= &(s->s3->client_random[0]);
220 er2= &(s->s3->server_random[0]); 243 er2= &(s->s3->server_random[0]);
@@ -250,7 +273,7 @@ int which;
250printf("which = %04X\nmac key=",which); 273printf("which = %04X\nmac key=",which);
251{ int z; for (z=0; z<i; z++) printf("%02X%c",ms[z],((z+1)%16)?' ':'\n'); } 274{ int z; for (z=0; z<i; z++) printf("%02X%c",ms[z],((z+1)%16)?' ':'\n'); }
252#endif 275#endif
253 if (exp) 276 if (_exp)
254 { 277 {
255 /* In here I set both the read and write key/iv to the 278 /* In here I set both the read and write key/iv to the
256 * same value since only the correct one will be used :-). 279 * same value since only the correct one will be used :-).
@@ -262,8 +285,8 @@ printf("which = %04X\nmac key=",which);
262 p+=SSL3_RANDOM_SIZE; 285 p+=SSL3_RANDOM_SIZE;
263 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); 286 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
264 p+=SSL3_RANDOM_SIZE; 287 p+=SSL3_RANDOM_SIZE;
265 tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,p-buf,key,j, 288 tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(p-buf),key,j,
266 tmp1,tmp2,EVP_CIPHER_key_length(c)); 289 tmp1,tmp2,EVP_CIPHER_key_length(c));
267 key=tmp1; 290 key=tmp1;
268 291
269 if (k > 0) 292 if (k > 0)
@@ -276,8 +299,8 @@ printf("which = %04X\nmac key=",which);
276 p+=SSL3_RANDOM_SIZE; 299 p+=SSL3_RANDOM_SIZE;
277 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); 300 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
278 p+=SSL3_RANDOM_SIZE; 301 p+=SSL3_RANDOM_SIZE;
279 tls1_PRF(s->ctx->md5,s->ctx->sha1, 302 tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,p-buf,empty,0,
280 buf,p-buf,"",0,iv1,iv2,k*2); 303 iv1,iv2,k*2);
281 if (client_write) 304 if (client_write)
282 iv=iv1; 305 iv=iv1;
283 else 306 else
@@ -307,18 +330,18 @@ err2:
307 return(0); 330 return(0);
308 } 331 }
309 332
310int tls1_setup_key_block(s) 333int tls1_setup_key_block(SSL *s)
311SSL *s;
312 { 334 {
313 unsigned char *p1,*p2; 335 unsigned char *p1,*p2;
314 EVP_CIPHER *c; 336 const EVP_CIPHER *c;
315 EVP_MD *hash; 337 const EVP_MD *hash;
316 int num,exp; 338 int num;
339 SSL_COMP *comp;
317 340
318 if (s->s3->tmp.key_block_length != 0) 341 if (s->s3->tmp.key_block_length != 0)
319 return(1); 342 return(1);
320 343
321 if (!ssl_cipher_get_evp(s->session->cipher,&c,&hash)) 344 if (!ssl_cipher_get_evp(s->session,&c,&hash,&comp))
322 { 345 {
323 SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,SSL_R_CIPHER_OR_HASH_UNAVAILABLE); 346 SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
324 return(0); 347 return(0);
@@ -327,8 +350,6 @@ SSL *s;
327 s->s3->tmp.new_sym_enc=c; 350 s->s3->tmp.new_sym_enc=c;
328 s->s3->tmp.new_hash=hash; 351 s->s3->tmp.new_hash=hash;
329 352
330 exp=(s->session->cipher->algorithms & SSL_EXPORT)?1:0;
331
332 num=EVP_CIPHER_key_length(c)+EVP_MD_size(hash)+EVP_CIPHER_iv_length(c); 353 num=EVP_CIPHER_key_length(c)+EVP_MD_size(hash)+EVP_CIPHER_iv_length(c);
333 num*=2; 354 num*=2;
334 355
@@ -365,16 +386,13 @@ err:
365 return(0); 386 return(0);
366 } 387 }
367 388
368int tls1_enc(s,send) 389int tls1_enc(SSL *s, int send)
369SSL *s;
370int send;
371 { 390 {
372 SSL3_RECORD *rec; 391 SSL3_RECORD *rec;
373 EVP_CIPHER_CTX *ds; 392 EVP_CIPHER_CTX *ds;
374 unsigned long l; 393 unsigned long l;
375 int bs,i,ii,j,k,n=0; 394 int bs,i,ii,j,k,n=0;
376 EVP_CIPHER *enc; 395 const EVP_CIPHER *enc;
377 SSL_COMPRESSION *comp;
378 396
379 if (send) 397 if (send)
380 { 398 {
@@ -383,12 +401,9 @@ int send;
383 ds=s->enc_write_ctx; 401 ds=s->enc_write_ctx;
384 rec= &(s->s3->wrec); 402 rec= &(s->s3->wrec);
385 if (s->enc_write_ctx == NULL) 403 if (s->enc_write_ctx == NULL)
386 { enc=NULL; comp=NULL; } 404 enc=NULL;
387 else 405 else
388 {
389 enc=EVP_CIPHER_CTX_cipher(s->enc_write_ctx); 406 enc=EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
390 comp=s->write_compression;
391 }
392 } 407 }
393 else 408 else
394 { 409 {
@@ -397,16 +412,13 @@ int send;
397 ds=s->enc_read_ctx; 412 ds=s->enc_read_ctx;
398 rec= &(s->s3->rrec); 413 rec= &(s->s3->rrec);
399 if (s->enc_read_ctx == NULL) 414 if (s->enc_read_ctx == NULL)
400 { enc=NULL; comp=NULL; } 415 enc=NULL;
401 else 416 else
402 {
403 enc=EVP_CIPHER_CTX_cipher(s->enc_read_ctx); 417 enc=EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
404 comp=s->read_compression;
405 }
406 } 418 }
407 419
408 if ((s->session == NULL) || (ds == NULL) || 420 if ((s->session == NULL) || (ds == NULL) ||
409 ((enc == NULL) && (comp == NULL))) 421 (enc == NULL))
410 { 422 {
411 memcpy(rec->data,rec->input,rec->length); 423 memcpy(rec->data,rec->input,rec->length);
412 rec->input=rec->data; 424 rec->input=rec->data;
@@ -471,25 +483,18 @@ int send;
471 return(1); 483 return(1);
472 } 484 }
473 485
474int tls1_cert_verify_mac(s,in_ctx,out) 486int tls1_cert_verify_mac(SSL *s, EVP_MD_CTX *in_ctx, unsigned char *out)
475SSL *s;
476EVP_MD_CTX *in_ctx;
477unsigned char *out;
478 { 487 {
479 unsigned int ret; 488 unsigned int ret;
480 EVP_MD_CTX ctx; 489 EVP_MD_CTX ctx;
481 490
482 memcpy(&ctx,in_ctx,sizeof(EVP_MD_CTX)); 491 EVP_MD_CTX_copy(&ctx,in_ctx);
483 EVP_DigestFinal(&ctx,out,&ret); 492 EVP_DigestFinal(&ctx,out,&ret);
484 return((int)ret); 493 return((int)ret);
485 } 494 }
486 495
487int tls1_final_finish_mac(s,in1_ctx,in2_ctx,str,slen,out) 496int tls1_final_finish_mac(SSL *s, EVP_MD_CTX *in1_ctx, EVP_MD_CTX *in2_ctx,
488SSL *s; 497 unsigned char *str, int slen, unsigned char *out)
489EVP_MD_CTX *in1_ctx,*in2_ctx;
490unsigned char *str;
491int slen;
492unsigned char *out;
493 { 498 {
494 unsigned int i; 499 unsigned int i;
495 EVP_MD_CTX ctx; 500 EVP_MD_CTX ctx;
@@ -500,14 +505,14 @@ unsigned char *out;
500 memcpy(q,str,slen); 505 memcpy(q,str,slen);
501 q+=slen; 506 q+=slen;
502 507
503 memcpy(&ctx,in1_ctx,sizeof(EVP_MD_CTX)); 508 EVP_MD_CTX_copy(&ctx,in1_ctx);
504 EVP_DigestFinal(&ctx,q,&i); 509 EVP_DigestFinal(&ctx,q,&i);
505 q+=i; 510 q+=i;
506 memcpy(&ctx,in2_ctx,sizeof(EVP_MD_CTX)); 511 EVP_MD_CTX_copy(&ctx,in2_ctx);
507 EVP_DigestFinal(&ctx,q,&i); 512 EVP_DigestFinal(&ctx,q,&i);
508 q+=i; 513 q+=i;
509 514
510 tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,q-buf, 515 tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(q-buf),
511 s->session->master_key,s->session->master_key_length, 516 s->session->master_key,s->session->master_key_length,
512 out,buf2,12); 517 out,buf2,12);
513 memset(&ctx,0,sizeof(EVP_MD_CTX)); 518 memset(&ctx,0,sizeof(EVP_MD_CTX));
@@ -515,14 +520,11 @@ unsigned char *out;
515 return((int)12); 520 return((int)12);
516 } 521 }
517 522
518int tls1_mac(ssl,md,send) 523int tls1_mac(SSL *ssl, unsigned char *md, int send)
519SSL *ssl;
520unsigned char *md;
521int send;
522 { 524 {
523 SSL3_RECORD *rec; 525 SSL3_RECORD *rec;
524 unsigned char *mac_sec,*seq; 526 unsigned char *mac_sec,*seq;
525 EVP_MD *hash; 527 const EVP_MD *hash;
526 unsigned int md_size; 528 unsigned int md_size;
527 int i; 529 int i;
528 HMAC_CTX hmac; 530 HMAC_CTX hmac;
@@ -560,29 +562,26 @@ int send;
560 562
561#ifdef TLS_DEBUG 563#ifdef TLS_DEBUG
562printf("sec="); 564printf("sec=");
563{int z; for (z=0; z<md_size; z++) printf("%02X ",mac_sec[z]); printf("\n"); } 565{unsigned int z; for (z=0; z<md_size; z++) printf("%02X ",mac_sec[z]); printf("\n"); }
564printf("seq="); 566printf("seq=");
565{int z; for (z=0; z<8; z++) printf("%02X ",seq[z]); printf("\n"); } 567{int z; for (z=0; z<8; z++) printf("%02X ",seq[z]); printf("\n"); }
566printf("buf="); 568printf("buf=");
567{int z; for (z=0; z<5; z++) printf("%02X ",buf[z]); printf("\n"); } 569{int z; for (z=0; z<5; z++) printf("%02X ",buf[z]); printf("\n"); }
568printf("rec="); 570printf("rec=");
569{int z; for (z=0; z<rec->length; z++) printf("%02X ",buf[z]); printf("\n"); } 571{unsigned int z; for (z=0; z<rec->length; z++) printf("%02X ",buf[z]); printf("\n"); }
570#endif 572#endif
571 573
572 for (i=7; i>=0; i--) 574 for (i=7; i>=0; i--)
573 if (++seq[i]) break; 575 if (++seq[i]) break;
574 576
575#ifdef TLS_DEBUG 577#ifdef TLS_DEBUG
576{int z; for (z=0; z<md_size; z++) printf("%02X ",md[z]); printf("\n"); } 578{unsigned int z; for (z=0; z<md_size; z++) printf("%02X ",md[z]); printf("\n"); }
577#endif 579#endif
578 return(md_size); 580 return(md_size);
579 } 581 }
580 582
581int tls1_generate_master_secret(s,out,p,len) 583int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
582SSL *s; 584 int len)
583unsigned char *out;
584unsigned char *p;
585int len;
586 { 585 {
587 unsigned char buf[SSL3_RANDOM_SIZE*2+TLS_MD_MASTER_SECRET_CONST_SIZE]; 586 unsigned char buf[SSL3_RANDOM_SIZE*2+TLS_MD_MASTER_SECRET_CONST_SIZE];
588 unsigned char buff[SSL_MAX_MASTER_KEY_LENGTH]; 587 unsigned char buff[SSL_MAX_MASTER_KEY_LENGTH];
@@ -600,8 +599,7 @@ int len;
600 return(SSL3_MASTER_SECRET_SIZE); 599 return(SSL3_MASTER_SECRET_SIZE);
601 } 600 }
602 601
603int tls1_alert_code(code) 602int tls1_alert_code(int code)
604int code;
605 { 603 {
606 switch (code) 604 switch (code)
607 { 605 {