diff options
Diffstat (limited to 'src/lib/libssl/d1_clnt.c')
-rw-r--r-- | src/lib/libssl/d1_clnt.c | 451 |
1 files changed, 36 insertions, 415 deletions
diff --git a/src/lib/libssl/d1_clnt.c b/src/lib/libssl/d1_clnt.c index 5bc9eb6603..49c6760d19 100644 --- a/src/lib/libssl/d1_clnt.c +++ b/src/lib/libssl/d1_clnt.c | |||
@@ -4,7 +4,7 @@ | |||
4 | * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. | 4 | * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. |
5 | */ | 5 | */ |
6 | /* ==================================================================== | 6 | /* ==================================================================== |
7 | * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved. | 7 | * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. |
8 | * | 8 | * |
9 | * Redistribution and use in source and binary forms, with or without | 9 | * Redistribution and use in source and binary forms, with or without |
10 | * modification, are permitted provided that the following conditions | 10 | * modification, are permitted provided that the following conditions |
@@ -115,25 +115,22 @@ | |||
115 | 115 | ||
116 | #include <stdio.h> | 116 | #include <stdio.h> |
117 | #include "ssl_locl.h" | 117 | #include "ssl_locl.h" |
118 | #ifndef OPENSSL_NO_KRB5 | ||
119 | #include "kssl_lcl.h" | 118 | #include "kssl_lcl.h" |
120 | #endif | ||
121 | #include <openssl/buffer.h> | 119 | #include <openssl/buffer.h> |
122 | #include <openssl/rand.h> | 120 | #include <openssl/rand.h> |
123 | #include <openssl/objects.h> | 121 | #include <openssl/objects.h> |
124 | #include <openssl/evp.h> | 122 | #include <openssl/evp.h> |
125 | #include <openssl/md5.h> | 123 | #include <openssl/md5.h> |
126 | #include <openssl/bn.h> | ||
127 | #ifndef OPENSSL_NO_DH | 124 | #ifndef OPENSSL_NO_DH |
128 | #include <openssl/dh.h> | 125 | #include <openssl/dh.h> |
129 | #endif | 126 | #endif |
130 | 127 | ||
131 | static const SSL_METHOD *dtls1_get_client_method(int ver); | 128 | static SSL_METHOD *dtls1_get_client_method(int ver); |
132 | static int dtls1_get_hello_verify(SSL *s); | 129 | static int dtls1_get_hello_verify(SSL *s); |
133 | 130 | ||
134 | static const SSL_METHOD *dtls1_get_client_method(int ver) | 131 | static SSL_METHOD *dtls1_get_client_method(int ver) |
135 | { | 132 | { |
136 | if (ver == DTLS1_VERSION || ver == DTLS1_BAD_VER) | 133 | if (ver == DTLS1_VERSION) |
137 | return(DTLSv1_client_method()); | 134 | return(DTLSv1_client_method()); |
138 | else | 135 | else |
139 | return(NULL); | 136 | return(NULL); |
@@ -147,7 +144,8 @@ IMPLEMENT_dtls1_meth_func(DTLSv1_client_method, | |||
147 | int dtls1_connect(SSL *s) | 144 | int dtls1_connect(SSL *s) |
148 | { | 145 | { |
149 | BUF_MEM *buf=NULL; | 146 | BUF_MEM *buf=NULL; |
150 | unsigned long Time=(unsigned long)time(NULL); | 147 | unsigned long Time=(unsigned long)time(NULL),l; |
148 | long num1; | ||
151 | void (*cb)(const SSL *ssl,int type,int val)=NULL; | 149 | void (*cb)(const SSL *ssl,int type,int val)=NULL; |
152 | int ret= -1; | 150 | int ret= -1; |
153 | int new_state,state,skip=0;; | 151 | int new_state,state,skip=0;; |
@@ -183,8 +181,7 @@ int dtls1_connect(SSL *s) | |||
183 | s->server=0; | 181 | s->server=0; |
184 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); | 182 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); |
185 | 183 | ||
186 | if ((s->version & 0xff00 ) != (DTLS1_VERSION & 0xff00) && | 184 | if ((s->version & 0xff00 ) != (DTLS1_VERSION & 0xff00)) |
187 | (s->version & 0xff00 ) != (DTLS1_BAD_VER & 0xff00)) | ||
188 | { | 185 | { |
189 | SSLerr(SSL_F_DTLS1_CONNECT, ERR_R_INTERNAL_ERROR); | 186 | SSLerr(SSL_F_DTLS1_CONNECT, ERR_R_INTERNAL_ERROR); |
190 | ret = -1; | 187 | ret = -1; |
@@ -222,8 +219,6 @@ int dtls1_connect(SSL *s) | |||
222 | s->init_num=0; | 219 | s->init_num=0; |
223 | /* mark client_random uninitialized */ | 220 | /* mark client_random uninitialized */ |
224 | memset(s->s3->client_random,0,sizeof(s->s3->client_random)); | 221 | memset(s->s3->client_random,0,sizeof(s->s3->client_random)); |
225 | s->d1->send_cookie = 0; | ||
226 | s->hit = 0; | ||
227 | break; | 222 | break; |
228 | 223 | ||
229 | case SSL3_ST_CW_CLNT_HELLO_A: | 224 | case SSL3_ST_CW_CLNT_HELLO_A: |
@@ -234,7 +229,6 @@ int dtls1_connect(SSL *s) | |||
234 | /* every DTLS ClientHello resets Finished MAC */ | 229 | /* every DTLS ClientHello resets Finished MAC */ |
235 | ssl3_init_finished_mac(s); | 230 | ssl3_init_finished_mac(s); |
236 | 231 | ||
237 | dtls1_start_timer(s); | ||
238 | ret=dtls1_client_hello(s); | 232 | ret=dtls1_client_hello(s); |
239 | if (ret <= 0) goto end; | 233 | if (ret <= 0) goto end; |
240 | 234 | ||
@@ -260,7 +254,6 @@ int dtls1_connect(SSL *s) | |||
260 | if (ret <= 0) goto end; | 254 | if (ret <= 0) goto end; |
261 | else | 255 | else |
262 | { | 256 | { |
263 | dtls1_stop_timer(s); | ||
264 | if (s->hit) | 257 | if (s->hit) |
265 | s->state=SSL3_ST_CR_FINISHED_A; | 258 | s->state=SSL3_ST_CR_FINISHED_A; |
266 | else | 259 | else |
@@ -275,7 +268,6 @@ int dtls1_connect(SSL *s) | |||
275 | ret = dtls1_get_hello_verify(s); | 268 | ret = dtls1_get_hello_verify(s); |
276 | if ( ret <= 0) | 269 | if ( ret <= 0) |
277 | goto end; | 270 | goto end; |
278 | dtls1_stop_timer(s); | ||
279 | if ( s->d1->send_cookie) /* start again, with a cookie */ | 271 | if ( s->d1->send_cookie) /* start again, with a cookie */ |
280 | s->state=SSL3_ST_CW_CLNT_HELLO_A; | 272 | s->state=SSL3_ST_CW_CLNT_HELLO_A; |
281 | else | 273 | else |
@@ -285,44 +277,15 @@ int dtls1_connect(SSL *s) | |||
285 | 277 | ||
286 | case SSL3_ST_CR_CERT_A: | 278 | case SSL3_ST_CR_CERT_A: |
287 | case SSL3_ST_CR_CERT_B: | 279 | case SSL3_ST_CR_CERT_B: |
288 | #ifndef OPENSSL_NO_TLSEXT | 280 | /* Check if it is anon DH */ |
289 | ret=ssl3_check_finished(s); | 281 | if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) |
290 | if (ret <= 0) goto end; | ||
291 | if (ret == 2) | ||
292 | { | ||
293 | s->hit = 1; | ||
294 | if (s->tlsext_ticket_expected) | ||
295 | s->state=SSL3_ST_CR_SESSION_TICKET_A; | ||
296 | else | ||
297 | s->state=SSL3_ST_CR_FINISHED_A; | ||
298 | s->init_num=0; | ||
299 | break; | ||
300 | } | ||
301 | #endif | ||
302 | /* Check if it is anon DH or PSK */ | ||
303 | if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) && | ||
304 | !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) | ||
305 | { | 282 | { |
306 | ret=ssl3_get_server_certificate(s); | 283 | ret=ssl3_get_server_certificate(s); |
307 | if (ret <= 0) goto end; | 284 | if (ret <= 0) goto end; |
308 | #ifndef OPENSSL_NO_TLSEXT | ||
309 | if (s->tlsext_status_expected) | ||
310 | s->state=SSL3_ST_CR_CERT_STATUS_A; | ||
311 | else | ||
312 | s->state=SSL3_ST_CR_KEY_EXCH_A; | ||
313 | } | ||
314 | else | ||
315 | { | ||
316 | skip = 1; | ||
317 | s->state=SSL3_ST_CR_KEY_EXCH_A; | ||
318 | } | ||
319 | #else | ||
320 | } | 285 | } |
321 | else | 286 | else |
322 | skip=1; | 287 | skip=1; |
323 | |||
324 | s->state=SSL3_ST_CR_KEY_EXCH_A; | 288 | s->state=SSL3_ST_CR_KEY_EXCH_A; |
325 | #endif | ||
326 | s->init_num=0; | 289 | s->init_num=0; |
327 | break; | 290 | break; |
328 | 291 | ||
@@ -366,7 +329,6 @@ int dtls1_connect(SSL *s) | |||
366 | case SSL3_ST_CW_CERT_B: | 329 | case SSL3_ST_CW_CERT_B: |
367 | case SSL3_ST_CW_CERT_C: | 330 | case SSL3_ST_CW_CERT_C: |
368 | case SSL3_ST_CW_CERT_D: | 331 | case SSL3_ST_CW_CERT_D: |
369 | dtls1_start_timer(s); | ||
370 | ret=dtls1_send_client_certificate(s); | 332 | ret=dtls1_send_client_certificate(s); |
371 | if (ret <= 0) goto end; | 333 | if (ret <= 0) goto end; |
372 | s->state=SSL3_ST_CW_KEY_EXCH_A; | 334 | s->state=SSL3_ST_CW_KEY_EXCH_A; |
@@ -375,9 +337,9 @@ int dtls1_connect(SSL *s) | |||
375 | 337 | ||
376 | case SSL3_ST_CW_KEY_EXCH_A: | 338 | case SSL3_ST_CW_KEY_EXCH_A: |
377 | case SSL3_ST_CW_KEY_EXCH_B: | 339 | case SSL3_ST_CW_KEY_EXCH_B: |
378 | dtls1_start_timer(s); | ||
379 | ret=dtls1_send_client_key_exchange(s); | 340 | ret=dtls1_send_client_key_exchange(s); |
380 | if (ret <= 0) goto end; | 341 | if (ret <= 0) goto end; |
342 | l=s->s3->tmp.new_cipher->algorithms; | ||
381 | /* EAY EAY EAY need to check for DH fix cert | 343 | /* EAY EAY EAY need to check for DH fix cert |
382 | * sent back */ | 344 | * sent back */ |
383 | /* For TLS, cert_req is set to 2, so a cert chain | 345 | /* For TLS, cert_req is set to 2, so a cert chain |
@@ -397,7 +359,6 @@ int dtls1_connect(SSL *s) | |||
397 | 359 | ||
398 | case SSL3_ST_CW_CERT_VRFY_A: | 360 | case SSL3_ST_CW_CERT_VRFY_A: |
399 | case SSL3_ST_CW_CERT_VRFY_B: | 361 | case SSL3_ST_CW_CERT_VRFY_B: |
400 | dtls1_start_timer(s); | ||
401 | ret=dtls1_send_client_verify(s); | 362 | ret=dtls1_send_client_verify(s); |
402 | if (ret <= 0) goto end; | 363 | if (ret <= 0) goto end; |
403 | s->state=SSL3_ST_CW_CHANGE_A; | 364 | s->state=SSL3_ST_CW_CHANGE_A; |
@@ -407,7 +368,6 @@ int dtls1_connect(SSL *s) | |||
407 | 368 | ||
408 | case SSL3_ST_CW_CHANGE_A: | 369 | case SSL3_ST_CW_CHANGE_A: |
409 | case SSL3_ST_CW_CHANGE_B: | 370 | case SSL3_ST_CW_CHANGE_B: |
410 | dtls1_start_timer(s); | ||
411 | ret=dtls1_send_change_cipher_spec(s, | 371 | ret=dtls1_send_change_cipher_spec(s, |
412 | SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B); | 372 | SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B); |
413 | if (ret <= 0) goto end; | 373 | if (ret <= 0) goto end; |
@@ -442,7 +402,6 @@ int dtls1_connect(SSL *s) | |||
442 | 402 | ||
443 | case SSL3_ST_CW_FINISHED_A: | 403 | case SSL3_ST_CW_FINISHED_A: |
444 | case SSL3_ST_CW_FINISHED_B: | 404 | case SSL3_ST_CW_FINISHED_B: |
445 | dtls1_start_timer(s); | ||
446 | ret=dtls1_send_finished(s, | 405 | ret=dtls1_send_finished(s, |
447 | SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B, | 406 | SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B, |
448 | s->method->ssl3_enc->client_finished_label, | 407 | s->method->ssl3_enc->client_finished_label, |
@@ -464,43 +423,20 @@ int dtls1_connect(SSL *s) | |||
464 | } | 423 | } |
465 | else | 424 | else |
466 | { | 425 | { |
467 | #ifndef OPENSSL_NO_TLSEXT | ||
468 | /* Allow NewSessionTicket if ticket expected */ | ||
469 | if (s->tlsext_ticket_expected) | ||
470 | s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A; | ||
471 | else | ||
472 | #endif | ||
473 | |||
474 | s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A; | 426 | s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A; |
475 | } | 427 | } |
476 | s->init_num=0; | 428 | s->init_num=0; |
477 | break; | 429 | /* mark client_random uninitialized */ |
478 | 430 | memset (s->s3->client_random,0,sizeof(s->s3->client_random)); | |
479 | #ifndef OPENSSL_NO_TLSEXT | ||
480 | case SSL3_ST_CR_SESSION_TICKET_A: | ||
481 | case SSL3_ST_CR_SESSION_TICKET_B: | ||
482 | ret=ssl3_get_new_session_ticket(s); | ||
483 | if (ret <= 0) goto end; | ||
484 | s->state=SSL3_ST_CR_FINISHED_A; | ||
485 | s->init_num=0; | ||
486 | break; | ||
487 | 431 | ||
488 | case SSL3_ST_CR_CERT_STATUS_A: | 432 | break; |
489 | case SSL3_ST_CR_CERT_STATUS_B: | ||
490 | ret=ssl3_get_cert_status(s); | ||
491 | if (ret <= 0) goto end; | ||
492 | s->state=SSL3_ST_CR_KEY_EXCH_A; | ||
493 | s->init_num=0; | ||
494 | break; | ||
495 | #endif | ||
496 | 433 | ||
497 | case SSL3_ST_CR_FINISHED_A: | 434 | case SSL3_ST_CR_FINISHED_A: |
498 | case SSL3_ST_CR_FINISHED_B: | 435 | case SSL3_ST_CR_FINISHED_B: |
499 | s->d1->change_cipher_spec_ok = 1; | 436 | |
500 | ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A, | 437 | ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A, |
501 | SSL3_ST_CR_FINISHED_B); | 438 | SSL3_ST_CR_FINISHED_B); |
502 | if (ret <= 0) goto end; | 439 | if (ret <= 0) goto end; |
503 | dtls1_stop_timer(s); | ||
504 | 440 | ||
505 | if (s->hit) | 441 | if (s->hit) |
506 | s->state=SSL3_ST_CW_CHANGE_A; | 442 | s->state=SSL3_ST_CW_CHANGE_A; |
@@ -510,13 +446,16 @@ int dtls1_connect(SSL *s) | |||
510 | break; | 446 | break; |
511 | 447 | ||
512 | case SSL3_ST_CW_FLUSH: | 448 | case SSL3_ST_CW_FLUSH: |
513 | s->rwstate=SSL_WRITING; | 449 | /* number of bytes to be flushed */ |
514 | if (BIO_flush(s->wbio) <= 0) | 450 | num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL); |
451 | if (num1 > 0) | ||
515 | { | 452 | { |
516 | ret= -1; | 453 | s->rwstate=SSL_WRITING; |
517 | goto end; | 454 | num1=BIO_flush(s->wbio); |
455 | if (num1 <= 0) { ret= -1; goto end; } | ||
456 | s->rwstate=SSL_NOTHING; | ||
518 | } | 457 | } |
519 | s->rwstate=SSL_NOTHING; | 458 | |
520 | s->state=s->s3->tmp.next_state; | 459 | s->state=s->s3->tmp.next_state; |
521 | break; | 460 | break; |
522 | 461 | ||
@@ -553,7 +492,6 @@ int dtls1_connect(SSL *s) | |||
553 | 492 | ||
554 | /* done with handshaking */ | 493 | /* done with handshaking */ |
555 | s->d1->handshake_read_seq = 0; | 494 | s->d1->handshake_read_seq = 0; |
556 | s->d1->next_handshake_write_seq = 0; | ||
557 | goto end; | 495 | goto end; |
558 | /* break; */ | 496 | /* break; */ |
559 | 497 | ||
@@ -603,14 +541,8 @@ int dtls1_client_hello(SSL *s) | |||
603 | buf=(unsigned char *)s->init_buf->data; | 541 | buf=(unsigned char *)s->init_buf->data; |
604 | if (s->state == SSL3_ST_CW_CLNT_HELLO_A) | 542 | if (s->state == SSL3_ST_CW_CLNT_HELLO_A) |
605 | { | 543 | { |
606 | SSL_SESSION *sess = s->session; | ||
607 | if ((s->session == NULL) || | 544 | if ((s->session == NULL) || |
608 | (s->session->ssl_version != s->version) || | 545 | (s->session->ssl_version != s->version) || |
609 | #ifdef OPENSSL_NO_TLSEXT | ||
610 | !sess->session_id_length || | ||
611 | #else | ||
612 | (!sess->session_id_length && !sess->tlsext_tick) || | ||
613 | #endif | ||
614 | (s->session->not_resumable)) | 546 | (s->session->not_resumable)) |
615 | { | 547 | { |
616 | if (!ssl_get_new_session(s,0)) | 548 | if (!ssl_get_new_session(s,0)) |
@@ -619,7 +551,6 @@ int dtls1_client_hello(SSL *s) | |||
619 | /* else use the pre-loaded session */ | 551 | /* else use the pre-loaded session */ |
620 | 552 | ||
621 | p=s->s3->client_random; | 553 | p=s->s3->client_random; |
622 | |||
623 | /* if client_random is initialized, reuse it, we are | 554 | /* if client_random is initialized, reuse it, we are |
624 | * required to use same upon reply to HelloVerify */ | 555 | * required to use same upon reply to HelloVerify */ |
625 | for (i=0;p[i]=='\0' && i<sizeof(s->s3->client_random);i++) ; | 556 | for (i=0;p[i]=='\0' && i<sizeof(s->s3->client_random);i++) ; |
@@ -627,7 +558,7 @@ int dtls1_client_hello(SSL *s) | |||
627 | { | 558 | { |
628 | Time=(unsigned long)time(NULL); /* Time */ | 559 | Time=(unsigned long)time(NULL); /* Time */ |
629 | l2n(Time,p); | 560 | l2n(Time,p); |
630 | RAND_pseudo_bytes(p,sizeof(s->s3->client_random)-4); | 561 | RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4); |
631 | } | 562 | } |
632 | 563 | ||
633 | /* Do the message type and length last */ | 564 | /* Do the message type and length last */ |
@@ -690,15 +621,7 @@ int dtls1_client_hello(SSL *s) | |||
690 | *(p++)=comp->id; | 621 | *(p++)=comp->id; |
691 | } | 622 | } |
692 | *(p++)=0; /* Add the NULL method */ | 623 | *(p++)=0; /* Add the NULL method */ |
693 | 624 | ||
694 | #ifndef OPENSSL_NO_TLSEXT | ||
695 | if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL) | ||
696 | { | ||
697 | SSLerr(SSL_F_DTLS1_CLIENT_HELLO,ERR_R_INTERNAL_ERROR); | ||
698 | goto err; | ||
699 | } | ||
700 | #endif | ||
701 | |||
702 | l=(p-d); | 625 | l=(p-d); |
703 | d=buf; | 626 | d=buf; |
704 | 627 | ||
@@ -774,7 +697,7 @@ int dtls1_send_client_key_exchange(SSL *s) | |||
774 | { | 697 | { |
775 | unsigned char *p,*d; | 698 | unsigned char *p,*d; |
776 | int n; | 699 | int n; |
777 | unsigned long alg_k; | 700 | unsigned long l; |
778 | #ifndef OPENSSL_NO_RSA | 701 | #ifndef OPENSSL_NO_RSA |
779 | unsigned char *q; | 702 | unsigned char *q; |
780 | EVP_PKEY *pkey=NULL; | 703 | EVP_PKEY *pkey=NULL; |
@@ -782,26 +705,18 @@ int dtls1_send_client_key_exchange(SSL *s) | |||
782 | #ifndef OPENSSL_NO_KRB5 | 705 | #ifndef OPENSSL_NO_KRB5 |
783 | KSSL_ERR kssl_err; | 706 | KSSL_ERR kssl_err; |
784 | #endif /* OPENSSL_NO_KRB5 */ | 707 | #endif /* OPENSSL_NO_KRB5 */ |
785 | #ifndef OPENSSL_NO_ECDH | ||
786 | EC_KEY *clnt_ecdh = NULL; | ||
787 | const EC_POINT *srvr_ecpoint = NULL; | ||
788 | EVP_PKEY *srvr_pub_pkey = NULL; | ||
789 | unsigned char *encodedPoint = NULL; | ||
790 | int encoded_pt_len = 0; | ||
791 | BN_CTX * bn_ctx = NULL; | ||
792 | #endif | ||
793 | 708 | ||
794 | if (s->state == SSL3_ST_CW_KEY_EXCH_A) | 709 | if (s->state == SSL3_ST_CW_KEY_EXCH_A) |
795 | { | 710 | { |
796 | d=(unsigned char *)s->init_buf->data; | 711 | d=(unsigned char *)s->init_buf->data; |
797 | p= &(d[DTLS1_HM_HEADER_LENGTH]); | 712 | p= &(d[DTLS1_HM_HEADER_LENGTH]); |
798 | 713 | ||
799 | alg_k=s->s3->tmp.new_cipher->algorithm_mkey; | 714 | l=s->s3->tmp.new_cipher->algorithms; |
800 | 715 | ||
801 | /* Fool emacs indentation */ | 716 | /* Fool emacs indentation */ |
802 | if (0) {} | 717 | if (0) {} |
803 | #ifndef OPENSSL_NO_RSA | 718 | #ifndef OPENSSL_NO_RSA |
804 | else if (alg_k & SSL_kRSA) | 719 | else if (l & SSL_kRSA) |
805 | { | 720 | { |
806 | RSA *rsa; | 721 | RSA *rsa; |
807 | unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; | 722 | unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; |
@@ -860,7 +775,7 @@ int dtls1_send_client_key_exchange(SSL *s) | |||
860 | } | 775 | } |
861 | #endif | 776 | #endif |
862 | #ifndef OPENSSL_NO_KRB5 | 777 | #ifndef OPENSSL_NO_KRB5 |
863 | else if (alg_k & SSL_kKRB5) | 778 | else if (l & SSL_kKRB5) |
864 | { | 779 | { |
865 | krb5_error_code krb5rc; | 780 | krb5_error_code krb5rc; |
866 | KSSL_CTX *kssl_ctx = s->kssl_ctx; | 781 | KSSL_CTX *kssl_ctx = s->kssl_ctx; |
@@ -868,7 +783,7 @@ int dtls1_send_client_key_exchange(SSL *s) | |||
868 | krb5_data *enc_ticket; | 783 | krb5_data *enc_ticket; |
869 | krb5_data authenticator, *authp = NULL; | 784 | krb5_data authenticator, *authp = NULL; |
870 | EVP_CIPHER_CTX ciph_ctx; | 785 | EVP_CIPHER_CTX ciph_ctx; |
871 | const EVP_CIPHER *enc = NULL; | 786 | EVP_CIPHER *enc = NULL; |
872 | unsigned char iv[EVP_MAX_IV_LENGTH]; | 787 | unsigned char iv[EVP_MAX_IV_LENGTH]; |
873 | unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; | 788 | unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; |
874 | unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH | 789 | unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH |
@@ -879,7 +794,7 @@ int dtls1_send_client_key_exchange(SSL *s) | |||
879 | 794 | ||
880 | #ifdef KSSL_DEBUG | 795 | #ifdef KSSL_DEBUG |
881 | printf("ssl3_send_client_key_exchange(%lx & %lx)\n", | 796 | printf("ssl3_send_client_key_exchange(%lx & %lx)\n", |
882 | alg_k, SSL_kKRB5); | 797 | l, SSL_kKRB5); |
883 | #endif /* KSSL_DEBUG */ | 798 | #endif /* KSSL_DEBUG */ |
884 | 799 | ||
885 | authp = NULL; | 800 | authp = NULL; |
@@ -969,7 +884,7 @@ int dtls1_send_client_key_exchange(SSL *s) | |||
969 | sizeof tmp_buf); | 884 | sizeof tmp_buf); |
970 | EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl); | 885 | EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl); |
971 | outl += padl; | 886 | outl += padl; |
972 | if (outl > (int)sizeof epms) | 887 | if (outl > sizeof epms) |
973 | { | 888 | { |
974 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | 889 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
975 | goto err; | 890 | goto err; |
@@ -992,7 +907,7 @@ int dtls1_send_client_key_exchange(SSL *s) | |||
992 | } | 907 | } |
993 | #endif | 908 | #endif |
994 | #ifndef OPENSSL_NO_DH | 909 | #ifndef OPENSSL_NO_DH |
995 | else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) | 910 | else if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) |
996 | { | 911 | { |
997 | DH *dh_srvr,*dh_clnt; | 912 | DH *dh_srvr,*dh_clnt; |
998 | 913 | ||
@@ -1047,274 +962,6 @@ int dtls1_send_client_key_exchange(SSL *s) | |||
1047 | /* perhaps clean things up a bit EAY EAY EAY EAY*/ | 962 | /* perhaps clean things up a bit EAY EAY EAY EAY*/ |
1048 | } | 963 | } |
1049 | #endif | 964 | #endif |
1050 | #ifndef OPENSSL_NO_ECDH | ||
1051 | else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) | ||
1052 | { | ||
1053 | const EC_GROUP *srvr_group = NULL; | ||
1054 | EC_KEY *tkey; | ||
1055 | int ecdh_clnt_cert = 0; | ||
1056 | int field_size = 0; | ||
1057 | |||
1058 | /* Did we send out the client's | ||
1059 | * ECDH share for use in premaster | ||
1060 | * computation as part of client certificate? | ||
1061 | * If so, set ecdh_clnt_cert to 1. | ||
1062 | */ | ||
1063 | if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL)) | ||
1064 | { | ||
1065 | /* XXX: For now, we do not support client | ||
1066 | * authentication using ECDH certificates. | ||
1067 | * To add such support, one needs to add | ||
1068 | * code that checks for appropriate | ||
1069 | * conditions and sets ecdh_clnt_cert to 1. | ||
1070 | * For example, the cert have an ECC | ||
1071 | * key on the same curve as the server's | ||
1072 | * and the key should be authorized for | ||
1073 | * key agreement. | ||
1074 | * | ||
1075 | * One also needs to add code in ssl3_connect | ||
1076 | * to skip sending the certificate verify | ||
1077 | * message. | ||
1078 | * | ||
1079 | * if ((s->cert->key->privatekey != NULL) && | ||
1080 | * (s->cert->key->privatekey->type == | ||
1081 | * EVP_PKEY_EC) && ...) | ||
1082 | * ecdh_clnt_cert = 1; | ||
1083 | */ | ||
1084 | } | ||
1085 | |||
1086 | if (s->session->sess_cert->peer_ecdh_tmp != NULL) | ||
1087 | { | ||
1088 | tkey = s->session->sess_cert->peer_ecdh_tmp; | ||
1089 | } | ||
1090 | else | ||
1091 | { | ||
1092 | /* Get the Server Public Key from Cert */ | ||
1093 | srvr_pub_pkey = X509_get_pubkey(s->session-> \ | ||
1094 | sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); | ||
1095 | if ((srvr_pub_pkey == NULL) || | ||
1096 | (srvr_pub_pkey->type != EVP_PKEY_EC) || | ||
1097 | (srvr_pub_pkey->pkey.ec == NULL)) | ||
1098 | { | ||
1099 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, | ||
1100 | ERR_R_INTERNAL_ERROR); | ||
1101 | goto err; | ||
1102 | } | ||
1103 | |||
1104 | tkey = srvr_pub_pkey->pkey.ec; | ||
1105 | } | ||
1106 | |||
1107 | srvr_group = EC_KEY_get0_group(tkey); | ||
1108 | srvr_ecpoint = EC_KEY_get0_public_key(tkey); | ||
1109 | |||
1110 | if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) | ||
1111 | { | ||
1112 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, | ||
1113 | ERR_R_INTERNAL_ERROR); | ||
1114 | goto err; | ||
1115 | } | ||
1116 | |||
1117 | if ((clnt_ecdh=EC_KEY_new()) == NULL) | ||
1118 | { | ||
1119 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); | ||
1120 | goto err; | ||
1121 | } | ||
1122 | |||
1123 | if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) | ||
1124 | { | ||
1125 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB); | ||
1126 | goto err; | ||
1127 | } | ||
1128 | if (ecdh_clnt_cert) | ||
1129 | { | ||
1130 | /* Reuse key info from our certificate | ||
1131 | * We only need our private key to perform | ||
1132 | * the ECDH computation. | ||
1133 | */ | ||
1134 | const BIGNUM *priv_key; | ||
1135 | tkey = s->cert->key->privatekey->pkey.ec; | ||
1136 | priv_key = EC_KEY_get0_private_key(tkey); | ||
1137 | if (priv_key == NULL) | ||
1138 | { | ||
1139 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); | ||
1140 | goto err; | ||
1141 | } | ||
1142 | if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) | ||
1143 | { | ||
1144 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB); | ||
1145 | goto err; | ||
1146 | } | ||
1147 | } | ||
1148 | else | ||
1149 | { | ||
1150 | /* Generate a new ECDH key pair */ | ||
1151 | if (!(EC_KEY_generate_key(clnt_ecdh))) | ||
1152 | { | ||
1153 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); | ||
1154 | goto err; | ||
1155 | } | ||
1156 | } | ||
1157 | |||
1158 | /* use the 'p' output buffer for the ECDH key, but | ||
1159 | * make sure to clear it out afterwards | ||
1160 | */ | ||
1161 | |||
1162 | field_size = EC_GROUP_get_degree(srvr_group); | ||
1163 | if (field_size <= 0) | ||
1164 | { | ||
1165 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, | ||
1166 | ERR_R_ECDH_LIB); | ||
1167 | goto err; | ||
1168 | } | ||
1169 | n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL); | ||
1170 | if (n <= 0) | ||
1171 | { | ||
1172 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, | ||
1173 | ERR_R_ECDH_LIB); | ||
1174 | goto err; | ||
1175 | } | ||
1176 | |||
1177 | /* generate master key from the result */ | ||
1178 | s->session->master_key_length = s->method->ssl3_enc \ | ||
1179 | -> generate_master_secret(s, | ||
1180 | s->session->master_key, | ||
1181 | p, n); | ||
1182 | |||
1183 | memset(p, 0, n); /* clean up */ | ||
1184 | |||
1185 | if (ecdh_clnt_cert) | ||
1186 | { | ||
1187 | /* Send empty client key exch message */ | ||
1188 | n = 0; | ||
1189 | } | ||
1190 | else | ||
1191 | { | ||
1192 | /* First check the size of encoding and | ||
1193 | * allocate memory accordingly. | ||
1194 | */ | ||
1195 | encoded_pt_len = | ||
1196 | EC_POINT_point2oct(srvr_group, | ||
1197 | EC_KEY_get0_public_key(clnt_ecdh), | ||
1198 | POINT_CONVERSION_UNCOMPRESSED, | ||
1199 | NULL, 0, NULL); | ||
1200 | |||
1201 | encodedPoint = (unsigned char *) | ||
1202 | OPENSSL_malloc(encoded_pt_len * | ||
1203 | sizeof(unsigned char)); | ||
1204 | bn_ctx = BN_CTX_new(); | ||
1205 | if ((encodedPoint == NULL) || | ||
1206 | (bn_ctx == NULL)) | ||
1207 | { | ||
1208 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); | ||
1209 | goto err; | ||
1210 | } | ||
1211 | |||
1212 | /* Encode the public key */ | ||
1213 | n = EC_POINT_point2oct(srvr_group, | ||
1214 | EC_KEY_get0_public_key(clnt_ecdh), | ||
1215 | POINT_CONVERSION_UNCOMPRESSED, | ||
1216 | encodedPoint, encoded_pt_len, bn_ctx); | ||
1217 | |||
1218 | *p = n; /* length of encoded point */ | ||
1219 | /* Encoded point will be copied here */ | ||
1220 | p += 1; | ||
1221 | /* copy the point */ | ||
1222 | memcpy((unsigned char *)p, encodedPoint, n); | ||
1223 | /* increment n to account for length field */ | ||
1224 | n += 1; | ||
1225 | } | ||
1226 | |||
1227 | /* Free allocated memory */ | ||
1228 | BN_CTX_free(bn_ctx); | ||
1229 | if (encodedPoint != NULL) OPENSSL_free(encodedPoint); | ||
1230 | if (clnt_ecdh != NULL) | ||
1231 | EC_KEY_free(clnt_ecdh); | ||
1232 | EVP_PKEY_free(srvr_pub_pkey); | ||
1233 | } | ||
1234 | #endif /* !OPENSSL_NO_ECDH */ | ||
1235 | |||
1236 | #ifndef OPENSSL_NO_PSK | ||
1237 | else if (alg_k & SSL_kPSK) | ||
1238 | { | ||
1239 | char identity[PSK_MAX_IDENTITY_LEN]; | ||
1240 | unsigned char *t = NULL; | ||
1241 | unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4]; | ||
1242 | unsigned int pre_ms_len = 0, psk_len = 0; | ||
1243 | int psk_err = 1; | ||
1244 | |||
1245 | n = 0; | ||
1246 | if (s->psk_client_callback == NULL) | ||
1247 | { | ||
1248 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, | ||
1249 | SSL_R_PSK_NO_CLIENT_CB); | ||
1250 | goto err; | ||
1251 | } | ||
1252 | |||
1253 | psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint, | ||
1254 | identity, PSK_MAX_IDENTITY_LEN, | ||
1255 | psk_or_pre_ms, sizeof(psk_or_pre_ms)); | ||
1256 | if (psk_len > PSK_MAX_PSK_LEN) | ||
1257 | { | ||
1258 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, | ||
1259 | ERR_R_INTERNAL_ERROR); | ||
1260 | goto psk_err; | ||
1261 | } | ||
1262 | else if (psk_len == 0) | ||
1263 | { | ||
1264 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, | ||
1265 | SSL_R_PSK_IDENTITY_NOT_FOUND); | ||
1266 | goto psk_err; | ||
1267 | } | ||
1268 | |||
1269 | /* create PSK pre_master_secret */ | ||
1270 | pre_ms_len = 2+psk_len+2+psk_len; | ||
1271 | t = psk_or_pre_ms; | ||
1272 | memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len); | ||
1273 | s2n(psk_len, t); | ||
1274 | memset(t, 0, psk_len); | ||
1275 | t+=psk_len; | ||
1276 | s2n(psk_len, t); | ||
1277 | |||
1278 | if (s->session->psk_identity_hint != NULL) | ||
1279 | OPENSSL_free(s->session->psk_identity_hint); | ||
1280 | s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint); | ||
1281 | if (s->ctx->psk_identity_hint != NULL && | ||
1282 | s->session->psk_identity_hint == NULL) | ||
1283 | { | ||
1284 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, | ||
1285 | ERR_R_MALLOC_FAILURE); | ||
1286 | goto psk_err; | ||
1287 | } | ||
1288 | |||
1289 | if (s->session->psk_identity != NULL) | ||
1290 | OPENSSL_free(s->session->psk_identity); | ||
1291 | s->session->psk_identity = BUF_strdup(identity); | ||
1292 | if (s->session->psk_identity == NULL) | ||
1293 | { | ||
1294 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, | ||
1295 | ERR_R_MALLOC_FAILURE); | ||
1296 | goto psk_err; | ||
1297 | } | ||
1298 | |||
1299 | s->session->master_key_length = | ||
1300 | s->method->ssl3_enc->generate_master_secret(s, | ||
1301 | s->session->master_key, | ||
1302 | psk_or_pre_ms, pre_ms_len); | ||
1303 | n = strlen(identity); | ||
1304 | s2n(n, p); | ||
1305 | memcpy(p, identity, n); | ||
1306 | n+=2; | ||
1307 | psk_err = 0; | ||
1308 | psk_err: | ||
1309 | OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN); | ||
1310 | OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms)); | ||
1311 | if (psk_err != 0) | ||
1312 | { | ||
1313 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | ||
1314 | goto err; | ||
1315 | } | ||
1316 | } | ||
1317 | #endif | ||
1318 | else | 965 | else |
1319 | { | 966 | { |
1320 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); | 967 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); |
@@ -1343,13 +990,6 @@ int dtls1_send_client_key_exchange(SSL *s) | |||
1343 | /* SSL3_ST_CW_KEY_EXCH_B */ | 990 | /* SSL3_ST_CW_KEY_EXCH_B */ |
1344 | return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); | 991 | return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); |
1345 | err: | 992 | err: |
1346 | #ifndef OPENSSL_NO_ECDH | ||
1347 | BN_CTX_free(bn_ctx); | ||
1348 | if (encodedPoint != NULL) OPENSSL_free(encodedPoint); | ||
1349 | if (clnt_ecdh != NULL) | ||
1350 | EC_KEY_free(clnt_ecdh); | ||
1351 | EVP_PKEY_free(srvr_pub_pkey); | ||
1352 | #endif | ||
1353 | return(-1); | 993 | return(-1); |
1354 | } | 994 | } |
1355 | 995 | ||
@@ -1362,7 +1002,7 @@ int dtls1_send_client_verify(SSL *s) | |||
1362 | unsigned u=0; | 1002 | unsigned u=0; |
1363 | #endif | 1003 | #endif |
1364 | unsigned long n; | 1004 | unsigned long n; |
1365 | #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA) | 1005 | #ifndef OPENSSL_NO_DSA |
1366 | int j; | 1006 | int j; |
1367 | #endif | 1007 | #endif |
1368 | 1008 | ||
@@ -1372,16 +1012,14 @@ int dtls1_send_client_verify(SSL *s) | |||
1372 | p= &(d[DTLS1_HM_HEADER_LENGTH]); | 1012 | p= &(d[DTLS1_HM_HEADER_LENGTH]); |
1373 | pkey=s->cert->key->privatekey; | 1013 | pkey=s->cert->key->privatekey; |
1374 | 1014 | ||
1375 | s->method->ssl3_enc->cert_verify_mac(s, | 1015 | s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2), |
1376 | NID_sha1, | ||
1377 | &(data[MD5_DIGEST_LENGTH])); | 1016 | &(data[MD5_DIGEST_LENGTH])); |
1378 | 1017 | ||
1379 | #ifndef OPENSSL_NO_RSA | 1018 | #ifndef OPENSSL_NO_RSA |
1380 | if (pkey->type == EVP_PKEY_RSA) | 1019 | if (pkey->type == EVP_PKEY_RSA) |
1381 | { | 1020 | { |
1382 | s->method->ssl3_enc->cert_verify_mac(s, | 1021 | s->method->ssl3_enc->cert_verify_mac(s, |
1383 | NID_md5, | 1022 | &(s->s3->finish_dgst1),&(data[0])); |
1384 | &(data[0])); | ||
1385 | if (RSA_sign(NID_md5_sha1, data, | 1023 | if (RSA_sign(NID_md5_sha1, data, |
1386 | MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, | 1024 | MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, |
1387 | &(p[2]), &u, pkey->pkey.rsa) <= 0 ) | 1025 | &(p[2]), &u, pkey->pkey.rsa) <= 0 ) |
@@ -1410,23 +1048,6 @@ int dtls1_send_client_verify(SSL *s) | |||
1410 | } | 1048 | } |
1411 | else | 1049 | else |
1412 | #endif | 1050 | #endif |
1413 | #ifndef OPENSSL_NO_ECDSA | ||
1414 | if (pkey->type == EVP_PKEY_EC) | ||
1415 | { | ||
1416 | if (!ECDSA_sign(pkey->save_type, | ||
1417 | &(data[MD5_DIGEST_LENGTH]), | ||
1418 | SHA_DIGEST_LENGTH,&(p[2]), | ||
1419 | (unsigned int *)&j,pkey->pkey.ec)) | ||
1420 | { | ||
1421 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY, | ||
1422 | ERR_R_ECDSA_LIB); | ||
1423 | goto err; | ||
1424 | } | ||
1425 | s2n(j,p); | ||
1426 | n=j+2; | ||
1427 | } | ||
1428 | else | ||
1429 | #endif | ||
1430 | { | 1051 | { |
1431 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR); | 1052 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR); |
1432 | goto err; | 1053 | goto err; |