diff options
Diffstat (limited to 'src/lib/libssl/d1_clnt.c')
-rw-r--r-- | src/lib/libssl/d1_clnt.c | 451 |
1 files changed, 415 insertions, 36 deletions
diff --git a/src/lib/libssl/d1_clnt.c b/src/lib/libssl/d1_clnt.c index 49c6760d19..5bc9eb6603 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-2005 The OpenSSL Project. All rights reserved. | 7 | * Copyright (c) 1999-2007 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,22 +115,25 @@ | |||
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 | ||
118 | #include "kssl_lcl.h" | 119 | #include "kssl_lcl.h" |
120 | #endif | ||
119 | #include <openssl/buffer.h> | 121 | #include <openssl/buffer.h> |
120 | #include <openssl/rand.h> | 122 | #include <openssl/rand.h> |
121 | #include <openssl/objects.h> | 123 | #include <openssl/objects.h> |
122 | #include <openssl/evp.h> | 124 | #include <openssl/evp.h> |
123 | #include <openssl/md5.h> | 125 | #include <openssl/md5.h> |
126 | #include <openssl/bn.h> | ||
124 | #ifndef OPENSSL_NO_DH | 127 | #ifndef OPENSSL_NO_DH |
125 | #include <openssl/dh.h> | 128 | #include <openssl/dh.h> |
126 | #endif | 129 | #endif |
127 | 130 | ||
128 | static SSL_METHOD *dtls1_get_client_method(int ver); | 131 | static const SSL_METHOD *dtls1_get_client_method(int ver); |
129 | static int dtls1_get_hello_verify(SSL *s); | 132 | static int dtls1_get_hello_verify(SSL *s); |
130 | 133 | ||
131 | static SSL_METHOD *dtls1_get_client_method(int ver) | 134 | static const SSL_METHOD *dtls1_get_client_method(int ver) |
132 | { | 135 | { |
133 | if (ver == DTLS1_VERSION) | 136 | if (ver == DTLS1_VERSION || ver == DTLS1_BAD_VER) |
134 | return(DTLSv1_client_method()); | 137 | return(DTLSv1_client_method()); |
135 | else | 138 | else |
136 | return(NULL); | 139 | return(NULL); |
@@ -144,8 +147,7 @@ IMPLEMENT_dtls1_meth_func(DTLSv1_client_method, | |||
144 | int dtls1_connect(SSL *s) | 147 | int dtls1_connect(SSL *s) |
145 | { | 148 | { |
146 | BUF_MEM *buf=NULL; | 149 | BUF_MEM *buf=NULL; |
147 | unsigned long Time=(unsigned long)time(NULL),l; | 150 | unsigned long Time=(unsigned long)time(NULL); |
148 | long num1; | ||
149 | void (*cb)(const SSL *ssl,int type,int val)=NULL; | 151 | void (*cb)(const SSL *ssl,int type,int val)=NULL; |
150 | int ret= -1; | 152 | int ret= -1; |
151 | int new_state,state,skip=0;; | 153 | int new_state,state,skip=0;; |
@@ -181,7 +183,8 @@ int dtls1_connect(SSL *s) | |||
181 | s->server=0; | 183 | s->server=0; |
182 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); | 184 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); |
183 | 185 | ||
184 | if ((s->version & 0xff00 ) != (DTLS1_VERSION & 0xff00)) | 186 | if ((s->version & 0xff00 ) != (DTLS1_VERSION & 0xff00) && |
187 | (s->version & 0xff00 ) != (DTLS1_BAD_VER & 0xff00)) | ||
185 | { | 188 | { |
186 | SSLerr(SSL_F_DTLS1_CONNECT, ERR_R_INTERNAL_ERROR); | 189 | SSLerr(SSL_F_DTLS1_CONNECT, ERR_R_INTERNAL_ERROR); |
187 | ret = -1; | 190 | ret = -1; |
@@ -219,6 +222,8 @@ int dtls1_connect(SSL *s) | |||
219 | s->init_num=0; | 222 | s->init_num=0; |
220 | /* mark client_random uninitialized */ | 223 | /* mark client_random uninitialized */ |
221 | memset(s->s3->client_random,0,sizeof(s->s3->client_random)); | 224 | memset(s->s3->client_random,0,sizeof(s->s3->client_random)); |
225 | s->d1->send_cookie = 0; | ||
226 | s->hit = 0; | ||
222 | break; | 227 | break; |
223 | 228 | ||
224 | case SSL3_ST_CW_CLNT_HELLO_A: | 229 | case SSL3_ST_CW_CLNT_HELLO_A: |
@@ -229,6 +234,7 @@ int dtls1_connect(SSL *s) | |||
229 | /* every DTLS ClientHello resets Finished MAC */ | 234 | /* every DTLS ClientHello resets Finished MAC */ |
230 | ssl3_init_finished_mac(s); | 235 | ssl3_init_finished_mac(s); |
231 | 236 | ||
237 | dtls1_start_timer(s); | ||
232 | ret=dtls1_client_hello(s); | 238 | ret=dtls1_client_hello(s); |
233 | if (ret <= 0) goto end; | 239 | if (ret <= 0) goto end; |
234 | 240 | ||
@@ -254,6 +260,7 @@ int dtls1_connect(SSL *s) | |||
254 | if (ret <= 0) goto end; | 260 | if (ret <= 0) goto end; |
255 | else | 261 | else |
256 | { | 262 | { |
263 | dtls1_stop_timer(s); | ||
257 | if (s->hit) | 264 | if (s->hit) |
258 | s->state=SSL3_ST_CR_FINISHED_A; | 265 | s->state=SSL3_ST_CR_FINISHED_A; |
259 | else | 266 | else |
@@ -268,6 +275,7 @@ int dtls1_connect(SSL *s) | |||
268 | ret = dtls1_get_hello_verify(s); | 275 | ret = dtls1_get_hello_verify(s); |
269 | if ( ret <= 0) | 276 | if ( ret <= 0) |
270 | goto end; | 277 | goto end; |
278 | dtls1_stop_timer(s); | ||
271 | if ( s->d1->send_cookie) /* start again, with a cookie */ | 279 | if ( s->d1->send_cookie) /* start again, with a cookie */ |
272 | s->state=SSL3_ST_CW_CLNT_HELLO_A; | 280 | s->state=SSL3_ST_CW_CLNT_HELLO_A; |
273 | else | 281 | else |
@@ -277,15 +285,44 @@ int dtls1_connect(SSL *s) | |||
277 | 285 | ||
278 | case SSL3_ST_CR_CERT_A: | 286 | case SSL3_ST_CR_CERT_A: |
279 | case SSL3_ST_CR_CERT_B: | 287 | case SSL3_ST_CR_CERT_B: |
280 | /* Check if it is anon DH */ | 288 | #ifndef OPENSSL_NO_TLSEXT |
281 | if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) | 289 | ret=ssl3_check_finished(s); |
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)) | ||
282 | { | 305 | { |
283 | ret=ssl3_get_server_certificate(s); | 306 | ret=ssl3_get_server_certificate(s); |
284 | if (ret <= 0) goto end; | 307 | 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 | ||
285 | } | 320 | } |
286 | else | 321 | else |
287 | skip=1; | 322 | skip=1; |
323 | |||
288 | s->state=SSL3_ST_CR_KEY_EXCH_A; | 324 | s->state=SSL3_ST_CR_KEY_EXCH_A; |
325 | #endif | ||
289 | s->init_num=0; | 326 | s->init_num=0; |
290 | break; | 327 | break; |
291 | 328 | ||
@@ -329,6 +366,7 @@ int dtls1_connect(SSL *s) | |||
329 | case SSL3_ST_CW_CERT_B: | 366 | case SSL3_ST_CW_CERT_B: |
330 | case SSL3_ST_CW_CERT_C: | 367 | case SSL3_ST_CW_CERT_C: |
331 | case SSL3_ST_CW_CERT_D: | 368 | case SSL3_ST_CW_CERT_D: |
369 | dtls1_start_timer(s); | ||
332 | ret=dtls1_send_client_certificate(s); | 370 | ret=dtls1_send_client_certificate(s); |
333 | if (ret <= 0) goto end; | 371 | if (ret <= 0) goto end; |
334 | s->state=SSL3_ST_CW_KEY_EXCH_A; | 372 | s->state=SSL3_ST_CW_KEY_EXCH_A; |
@@ -337,9 +375,9 @@ int dtls1_connect(SSL *s) | |||
337 | 375 | ||
338 | case SSL3_ST_CW_KEY_EXCH_A: | 376 | case SSL3_ST_CW_KEY_EXCH_A: |
339 | case SSL3_ST_CW_KEY_EXCH_B: | 377 | case SSL3_ST_CW_KEY_EXCH_B: |
378 | dtls1_start_timer(s); | ||
340 | ret=dtls1_send_client_key_exchange(s); | 379 | ret=dtls1_send_client_key_exchange(s); |
341 | if (ret <= 0) goto end; | 380 | if (ret <= 0) goto end; |
342 | l=s->s3->tmp.new_cipher->algorithms; | ||
343 | /* EAY EAY EAY need to check for DH fix cert | 381 | /* EAY EAY EAY need to check for DH fix cert |
344 | * sent back */ | 382 | * sent back */ |
345 | /* For TLS, cert_req is set to 2, so a cert chain | 383 | /* For TLS, cert_req is set to 2, so a cert chain |
@@ -359,6 +397,7 @@ int dtls1_connect(SSL *s) | |||
359 | 397 | ||
360 | case SSL3_ST_CW_CERT_VRFY_A: | 398 | case SSL3_ST_CW_CERT_VRFY_A: |
361 | case SSL3_ST_CW_CERT_VRFY_B: | 399 | case SSL3_ST_CW_CERT_VRFY_B: |
400 | dtls1_start_timer(s); | ||
362 | ret=dtls1_send_client_verify(s); | 401 | ret=dtls1_send_client_verify(s); |
363 | if (ret <= 0) goto end; | 402 | if (ret <= 0) goto end; |
364 | s->state=SSL3_ST_CW_CHANGE_A; | 403 | s->state=SSL3_ST_CW_CHANGE_A; |
@@ -368,6 +407,7 @@ int dtls1_connect(SSL *s) | |||
368 | 407 | ||
369 | case SSL3_ST_CW_CHANGE_A: | 408 | case SSL3_ST_CW_CHANGE_A: |
370 | case SSL3_ST_CW_CHANGE_B: | 409 | case SSL3_ST_CW_CHANGE_B: |
410 | dtls1_start_timer(s); | ||
371 | ret=dtls1_send_change_cipher_spec(s, | 411 | ret=dtls1_send_change_cipher_spec(s, |
372 | SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B); | 412 | SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B); |
373 | if (ret <= 0) goto end; | 413 | if (ret <= 0) goto end; |
@@ -402,6 +442,7 @@ int dtls1_connect(SSL *s) | |||
402 | 442 | ||
403 | case SSL3_ST_CW_FINISHED_A: | 443 | case SSL3_ST_CW_FINISHED_A: |
404 | case SSL3_ST_CW_FINISHED_B: | 444 | case SSL3_ST_CW_FINISHED_B: |
445 | dtls1_start_timer(s); | ||
405 | ret=dtls1_send_finished(s, | 446 | ret=dtls1_send_finished(s, |
406 | SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B, | 447 | SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B, |
407 | s->method->ssl3_enc->client_finished_label, | 448 | s->method->ssl3_enc->client_finished_label, |
@@ -423,20 +464,43 @@ int dtls1_connect(SSL *s) | |||
423 | } | 464 | } |
424 | else | 465 | else |
425 | { | 466 | { |
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 | |||
426 | s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A; | 474 | s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A; |
427 | } | 475 | } |
428 | s->init_num=0; | 476 | s->init_num=0; |
429 | /* mark client_random uninitialized */ | ||
430 | memset (s->s3->client_random,0,sizeof(s->s3->client_random)); | ||
431 | |||
432 | break; | 477 | break; |
433 | 478 | ||
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 | |||
488 | case SSL3_ST_CR_CERT_STATUS_A: | ||
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 | |||
434 | case SSL3_ST_CR_FINISHED_A: | 497 | case SSL3_ST_CR_FINISHED_A: |
435 | case SSL3_ST_CR_FINISHED_B: | 498 | case SSL3_ST_CR_FINISHED_B: |
436 | 499 | s->d1->change_cipher_spec_ok = 1; | |
437 | ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A, | 500 | ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A, |
438 | SSL3_ST_CR_FINISHED_B); | 501 | SSL3_ST_CR_FINISHED_B); |
439 | if (ret <= 0) goto end; | 502 | if (ret <= 0) goto end; |
503 | dtls1_stop_timer(s); | ||
440 | 504 | ||
441 | if (s->hit) | 505 | if (s->hit) |
442 | s->state=SSL3_ST_CW_CHANGE_A; | 506 | s->state=SSL3_ST_CW_CHANGE_A; |
@@ -446,16 +510,13 @@ int dtls1_connect(SSL *s) | |||
446 | break; | 510 | break; |
447 | 511 | ||
448 | case SSL3_ST_CW_FLUSH: | 512 | case SSL3_ST_CW_FLUSH: |
449 | /* number of bytes to be flushed */ | 513 | s->rwstate=SSL_WRITING; |
450 | num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL); | 514 | if (BIO_flush(s->wbio) <= 0) |
451 | if (num1 > 0) | ||
452 | { | 515 | { |
453 | s->rwstate=SSL_WRITING; | 516 | ret= -1; |
454 | num1=BIO_flush(s->wbio); | 517 | goto end; |
455 | if (num1 <= 0) { ret= -1; goto end; } | ||
456 | s->rwstate=SSL_NOTHING; | ||
457 | } | 518 | } |
458 | 519 | s->rwstate=SSL_NOTHING; | |
459 | s->state=s->s3->tmp.next_state; | 520 | s->state=s->s3->tmp.next_state; |
460 | break; | 521 | break; |
461 | 522 | ||
@@ -492,6 +553,7 @@ int dtls1_connect(SSL *s) | |||
492 | 553 | ||
493 | /* done with handshaking */ | 554 | /* done with handshaking */ |
494 | s->d1->handshake_read_seq = 0; | 555 | s->d1->handshake_read_seq = 0; |
556 | s->d1->next_handshake_write_seq = 0; | ||
495 | goto end; | 557 | goto end; |
496 | /* break; */ | 558 | /* break; */ |
497 | 559 | ||
@@ -541,8 +603,14 @@ int dtls1_client_hello(SSL *s) | |||
541 | buf=(unsigned char *)s->init_buf->data; | 603 | buf=(unsigned char *)s->init_buf->data; |
542 | if (s->state == SSL3_ST_CW_CLNT_HELLO_A) | 604 | if (s->state == SSL3_ST_CW_CLNT_HELLO_A) |
543 | { | 605 | { |
606 | SSL_SESSION *sess = s->session; | ||
544 | if ((s->session == NULL) || | 607 | if ((s->session == NULL) || |
545 | (s->session->ssl_version != s->version) || | 608 | (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 | ||
546 | (s->session->not_resumable)) | 614 | (s->session->not_resumable)) |
547 | { | 615 | { |
548 | if (!ssl_get_new_session(s,0)) | 616 | if (!ssl_get_new_session(s,0)) |
@@ -551,6 +619,7 @@ int dtls1_client_hello(SSL *s) | |||
551 | /* else use the pre-loaded session */ | 619 | /* else use the pre-loaded session */ |
552 | 620 | ||
553 | p=s->s3->client_random; | 621 | p=s->s3->client_random; |
622 | |||
554 | /* if client_random is initialized, reuse it, we are | 623 | /* if client_random is initialized, reuse it, we are |
555 | * required to use same upon reply to HelloVerify */ | 624 | * required to use same upon reply to HelloVerify */ |
556 | for (i=0;p[i]=='\0' && i<sizeof(s->s3->client_random);i++) ; | 625 | for (i=0;p[i]=='\0' && i<sizeof(s->s3->client_random);i++) ; |
@@ -558,7 +627,7 @@ int dtls1_client_hello(SSL *s) | |||
558 | { | 627 | { |
559 | Time=(unsigned long)time(NULL); /* Time */ | 628 | Time=(unsigned long)time(NULL); /* Time */ |
560 | l2n(Time,p); | 629 | l2n(Time,p); |
561 | RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4); | 630 | RAND_pseudo_bytes(p,sizeof(s->s3->client_random)-4); |
562 | } | 631 | } |
563 | 632 | ||
564 | /* Do the message type and length last */ | 633 | /* Do the message type and length last */ |
@@ -621,7 +690,15 @@ int dtls1_client_hello(SSL *s) | |||
621 | *(p++)=comp->id; | 690 | *(p++)=comp->id; |
622 | } | 691 | } |
623 | *(p++)=0; /* Add the NULL method */ | 692 | *(p++)=0; /* Add the NULL method */ |
624 | 693 | ||
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 | |||
625 | l=(p-d); | 702 | l=(p-d); |
626 | d=buf; | 703 | d=buf; |
627 | 704 | ||
@@ -697,7 +774,7 @@ int dtls1_send_client_key_exchange(SSL *s) | |||
697 | { | 774 | { |
698 | unsigned char *p,*d; | 775 | unsigned char *p,*d; |
699 | int n; | 776 | int n; |
700 | unsigned long l; | 777 | unsigned long alg_k; |
701 | #ifndef OPENSSL_NO_RSA | 778 | #ifndef OPENSSL_NO_RSA |
702 | unsigned char *q; | 779 | unsigned char *q; |
703 | EVP_PKEY *pkey=NULL; | 780 | EVP_PKEY *pkey=NULL; |
@@ -705,18 +782,26 @@ int dtls1_send_client_key_exchange(SSL *s) | |||
705 | #ifndef OPENSSL_NO_KRB5 | 782 | #ifndef OPENSSL_NO_KRB5 |
706 | KSSL_ERR kssl_err; | 783 | KSSL_ERR kssl_err; |
707 | #endif /* OPENSSL_NO_KRB5 */ | 784 | #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 | ||
708 | 793 | ||
709 | if (s->state == SSL3_ST_CW_KEY_EXCH_A) | 794 | if (s->state == SSL3_ST_CW_KEY_EXCH_A) |
710 | { | 795 | { |
711 | d=(unsigned char *)s->init_buf->data; | 796 | d=(unsigned char *)s->init_buf->data; |
712 | p= &(d[DTLS1_HM_HEADER_LENGTH]); | 797 | p= &(d[DTLS1_HM_HEADER_LENGTH]); |
713 | 798 | ||
714 | l=s->s3->tmp.new_cipher->algorithms; | 799 | alg_k=s->s3->tmp.new_cipher->algorithm_mkey; |
715 | 800 | ||
716 | /* Fool emacs indentation */ | 801 | /* Fool emacs indentation */ |
717 | if (0) {} | 802 | if (0) {} |
718 | #ifndef OPENSSL_NO_RSA | 803 | #ifndef OPENSSL_NO_RSA |
719 | else if (l & SSL_kRSA) | 804 | else if (alg_k & SSL_kRSA) |
720 | { | 805 | { |
721 | RSA *rsa; | 806 | RSA *rsa; |
722 | unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; | 807 | unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; |
@@ -775,7 +860,7 @@ int dtls1_send_client_key_exchange(SSL *s) | |||
775 | } | 860 | } |
776 | #endif | 861 | #endif |
777 | #ifndef OPENSSL_NO_KRB5 | 862 | #ifndef OPENSSL_NO_KRB5 |
778 | else if (l & SSL_kKRB5) | 863 | else if (alg_k & SSL_kKRB5) |
779 | { | 864 | { |
780 | krb5_error_code krb5rc; | 865 | krb5_error_code krb5rc; |
781 | KSSL_CTX *kssl_ctx = s->kssl_ctx; | 866 | KSSL_CTX *kssl_ctx = s->kssl_ctx; |
@@ -783,7 +868,7 @@ int dtls1_send_client_key_exchange(SSL *s) | |||
783 | krb5_data *enc_ticket; | 868 | krb5_data *enc_ticket; |
784 | krb5_data authenticator, *authp = NULL; | 869 | krb5_data authenticator, *authp = NULL; |
785 | EVP_CIPHER_CTX ciph_ctx; | 870 | EVP_CIPHER_CTX ciph_ctx; |
786 | EVP_CIPHER *enc = NULL; | 871 | const EVP_CIPHER *enc = NULL; |
787 | unsigned char iv[EVP_MAX_IV_LENGTH]; | 872 | unsigned char iv[EVP_MAX_IV_LENGTH]; |
788 | unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; | 873 | unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; |
789 | unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH | 874 | unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH |
@@ -794,7 +879,7 @@ int dtls1_send_client_key_exchange(SSL *s) | |||
794 | 879 | ||
795 | #ifdef KSSL_DEBUG | 880 | #ifdef KSSL_DEBUG |
796 | printf("ssl3_send_client_key_exchange(%lx & %lx)\n", | 881 | printf("ssl3_send_client_key_exchange(%lx & %lx)\n", |
797 | l, SSL_kKRB5); | 882 | alg_k, SSL_kKRB5); |
798 | #endif /* KSSL_DEBUG */ | 883 | #endif /* KSSL_DEBUG */ |
799 | 884 | ||
800 | authp = NULL; | 885 | authp = NULL; |
@@ -884,7 +969,7 @@ int dtls1_send_client_key_exchange(SSL *s) | |||
884 | sizeof tmp_buf); | 969 | sizeof tmp_buf); |
885 | EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl); | 970 | EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl); |
886 | outl += padl; | 971 | outl += padl; |
887 | if (outl > sizeof epms) | 972 | if (outl > (int)sizeof epms) |
888 | { | 973 | { |
889 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | 974 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
890 | goto err; | 975 | goto err; |
@@ -907,7 +992,7 @@ int dtls1_send_client_key_exchange(SSL *s) | |||
907 | } | 992 | } |
908 | #endif | 993 | #endif |
909 | #ifndef OPENSSL_NO_DH | 994 | #ifndef OPENSSL_NO_DH |
910 | else if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) | 995 | else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) |
911 | { | 996 | { |
912 | DH *dh_srvr,*dh_clnt; | 997 | DH *dh_srvr,*dh_clnt; |
913 | 998 | ||
@@ -962,6 +1047,274 @@ int dtls1_send_client_key_exchange(SSL *s) | |||
962 | /* perhaps clean things up a bit EAY EAY EAY EAY*/ | 1047 | /* perhaps clean things up a bit EAY EAY EAY EAY*/ |
963 | } | 1048 | } |
964 | #endif | 1049 | #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 | ||
965 | else | 1318 | else |
966 | { | 1319 | { |
967 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); | 1320 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); |
@@ -990,6 +1343,13 @@ int dtls1_send_client_key_exchange(SSL *s) | |||
990 | /* SSL3_ST_CW_KEY_EXCH_B */ | 1343 | /* SSL3_ST_CW_KEY_EXCH_B */ |
991 | return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); | 1344 | return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); |
992 | err: | 1345 | 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 | ||
993 | return(-1); | 1353 | return(-1); |
994 | } | 1354 | } |
995 | 1355 | ||
@@ -1002,7 +1362,7 @@ int dtls1_send_client_verify(SSL *s) | |||
1002 | unsigned u=0; | 1362 | unsigned u=0; |
1003 | #endif | 1363 | #endif |
1004 | unsigned long n; | 1364 | unsigned long n; |
1005 | #ifndef OPENSSL_NO_DSA | 1365 | #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA) |
1006 | int j; | 1366 | int j; |
1007 | #endif | 1367 | #endif |
1008 | 1368 | ||
@@ -1012,14 +1372,16 @@ int dtls1_send_client_verify(SSL *s) | |||
1012 | p= &(d[DTLS1_HM_HEADER_LENGTH]); | 1372 | p= &(d[DTLS1_HM_HEADER_LENGTH]); |
1013 | pkey=s->cert->key->privatekey; | 1373 | pkey=s->cert->key->privatekey; |
1014 | 1374 | ||
1015 | s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2), | 1375 | s->method->ssl3_enc->cert_verify_mac(s, |
1376 | NID_sha1, | ||
1016 | &(data[MD5_DIGEST_LENGTH])); | 1377 | &(data[MD5_DIGEST_LENGTH])); |
1017 | 1378 | ||
1018 | #ifndef OPENSSL_NO_RSA | 1379 | #ifndef OPENSSL_NO_RSA |
1019 | if (pkey->type == EVP_PKEY_RSA) | 1380 | if (pkey->type == EVP_PKEY_RSA) |
1020 | { | 1381 | { |
1021 | s->method->ssl3_enc->cert_verify_mac(s, | 1382 | s->method->ssl3_enc->cert_verify_mac(s, |
1022 | &(s->s3->finish_dgst1),&(data[0])); | 1383 | NID_md5, |
1384 | &(data[0])); | ||
1023 | if (RSA_sign(NID_md5_sha1, data, | 1385 | if (RSA_sign(NID_md5_sha1, data, |
1024 | MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, | 1386 | MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, |
1025 | &(p[2]), &u, pkey->pkey.rsa) <= 0 ) | 1387 | &(p[2]), &u, pkey->pkey.rsa) <= 0 ) |
@@ -1048,6 +1410,23 @@ int dtls1_send_client_verify(SSL *s) | |||
1048 | } | 1410 | } |
1049 | else | 1411 | else |
1050 | #endif | 1412 | #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 | ||
1051 | { | 1430 | { |
1052 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR); | 1431 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR); |
1053 | goto err; | 1432 | goto err; |