diff options
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libssl/d1_srvr.c | 540 |
1 files changed, 472 insertions, 68 deletions
diff --git a/src/lib/libssl/d1_srvr.c b/src/lib/libssl/d1_srvr.c index 0bbf8ae7f3..301ceda7a5 100644 --- a/src/lib/libssl/d1_srvr.c +++ b/src/lib/libssl/d1_srvr.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 |
| @@ -121,14 +121,15 @@ | |||
| 121 | #include <openssl/evp.h> | 121 | #include <openssl/evp.h> |
| 122 | #include <openssl/x509.h> | 122 | #include <openssl/x509.h> |
| 123 | #include <openssl/md5.h> | 123 | #include <openssl/md5.h> |
| 124 | #include <openssl/bn.h> | ||
| 124 | #ifndef OPENSSL_NO_DH | 125 | #ifndef OPENSSL_NO_DH |
| 125 | #include <openssl/dh.h> | 126 | #include <openssl/dh.h> |
| 126 | #endif | 127 | #endif |
| 127 | 128 | ||
| 128 | static SSL_METHOD *dtls1_get_server_method(int ver); | 129 | static const SSL_METHOD *dtls1_get_server_method(int ver); |
| 129 | static int dtls1_send_hello_verify_request(SSL *s); | 130 | static int dtls1_send_hello_verify_request(SSL *s); |
| 130 | 131 | ||
| 131 | static SSL_METHOD *dtls1_get_server_method(int ver) | 132 | static const SSL_METHOD *dtls1_get_server_method(int ver) |
| 132 | { | 133 | { |
| 133 | if (ver == DTLS1_VERSION) | 134 | if (ver == DTLS1_VERSION) |
| 134 | return(DTLSv1_server_method()); | 135 | return(DTLSv1_server_method()); |
| @@ -144,9 +145,9 @@ IMPLEMENT_dtls1_meth_func(DTLSv1_server_method, | |||
| 144 | int dtls1_accept(SSL *s) | 145 | int dtls1_accept(SSL *s) |
| 145 | { | 146 | { |
| 146 | BUF_MEM *buf; | 147 | BUF_MEM *buf; |
| 147 | unsigned long l,Time=(unsigned long)time(NULL); | 148 | unsigned long Time=(unsigned long)time(NULL); |
| 148 | void (*cb)(const SSL *ssl,int type,int val)=NULL; | 149 | void (*cb)(const SSL *ssl,int type,int val)=NULL; |
| 149 | long num1; | 150 | unsigned long alg_k; |
| 150 | int ret= -1; | 151 | int ret= -1; |
| 151 | int new_state,state,skip=0; | 152 | int new_state,state,skip=0; |
| 152 | 153 | ||
| @@ -236,17 +237,13 @@ int dtls1_accept(SSL *s) | |||
| 236 | s->state=SSL3_ST_SW_HELLO_REQ_A; | 237 | s->state=SSL3_ST_SW_HELLO_REQ_A; |
| 237 | } | 238 | } |
| 238 | 239 | ||
| 239 | if ( (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) | ||
| 240 | s->d1->send_cookie = 1; | ||
| 241 | else | ||
| 242 | s->d1->send_cookie = 0; | ||
| 243 | |||
| 244 | break; | 240 | break; |
| 245 | 241 | ||
| 246 | case SSL3_ST_SW_HELLO_REQ_A: | 242 | case SSL3_ST_SW_HELLO_REQ_A: |
| 247 | case SSL3_ST_SW_HELLO_REQ_B: | 243 | case SSL3_ST_SW_HELLO_REQ_B: |
| 248 | 244 | ||
| 249 | s->shutdown=0; | 245 | s->shutdown=0; |
| 246 | dtls1_start_timer(s); | ||
| 250 | ret=dtls1_send_hello_request(s); | 247 | ret=dtls1_send_hello_request(s); |
| 251 | if (ret <= 0) goto end; | 248 | if (ret <= 0) goto end; |
| 252 | s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C; | 249 | s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C; |
| @@ -267,37 +264,58 @@ int dtls1_accept(SSL *s) | |||
| 267 | s->shutdown=0; | 264 | s->shutdown=0; |
| 268 | ret=ssl3_get_client_hello(s); | 265 | ret=ssl3_get_client_hello(s); |
| 269 | if (ret <= 0) goto end; | 266 | if (ret <= 0) goto end; |
| 270 | s->new_session = 2; | 267 | dtls1_stop_timer(s); |
| 271 | 268 | ||
| 272 | if ( s->d1->send_cookie) | 269 | if (ret == 1 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) |
| 273 | s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A; | 270 | s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A; |
| 274 | else | 271 | else |
| 275 | s->state = SSL3_ST_SW_SRVR_HELLO_A; | 272 | s->state = SSL3_ST_SW_SRVR_HELLO_A; |
| 276 | 273 | ||
| 277 | s->init_num=0; | 274 | s->init_num=0; |
| 275 | |||
| 276 | /* If we're just listening, stop here */ | ||
| 277 | if (s->d1->listen && s->state == SSL3_ST_SW_SRVR_HELLO_A) | ||
| 278 | { | ||
| 279 | ret = 2; | ||
| 280 | s->d1->listen = 0; | ||
| 281 | goto end; | ||
| 282 | } | ||
| 283 | |||
| 278 | break; | 284 | break; |
| 279 | 285 | ||
| 280 | case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A: | 286 | case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A: |
| 281 | case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B: | 287 | case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B: |
| 282 | 288 | ||
| 289 | dtls1_start_timer(s); | ||
| 283 | ret = dtls1_send_hello_verify_request(s); | 290 | ret = dtls1_send_hello_verify_request(s); |
| 284 | if ( ret <= 0) goto end; | 291 | if ( ret <= 0) goto end; |
| 285 | s->d1->send_cookie = 0; | ||
| 286 | s->state=SSL3_ST_SW_FLUSH; | 292 | s->state=SSL3_ST_SW_FLUSH; |
| 287 | s->s3->tmp.next_state=SSL3_ST_SR_CLNT_HELLO_A; | 293 | s->s3->tmp.next_state=SSL3_ST_SR_CLNT_HELLO_A; |
| 288 | 294 | ||
| 289 | /* HelloVerifyRequests resets Finished MAC */ | 295 | /* HelloVerifyRequest resets Finished MAC */ |
| 290 | if (s->client_version != DTLS1_BAD_VER) | 296 | if (s->version != DTLS1_BAD_VER) |
| 291 | ssl3_init_finished_mac(s); | 297 | ssl3_init_finished_mac(s); |
| 292 | break; | 298 | break; |
| 293 | 299 | ||
| 294 | case SSL3_ST_SW_SRVR_HELLO_A: | 300 | case SSL3_ST_SW_SRVR_HELLO_A: |
| 295 | case SSL3_ST_SW_SRVR_HELLO_B: | 301 | case SSL3_ST_SW_SRVR_HELLO_B: |
| 302 | s->new_session = 2; | ||
| 303 | dtls1_start_timer(s); | ||
| 296 | ret=dtls1_send_server_hello(s); | 304 | ret=dtls1_send_server_hello(s); |
| 297 | if (ret <= 0) goto end; | 305 | if (ret <= 0) goto end; |
| 298 | 306 | ||
| 307 | #ifndef OPENSSL_NO_TLSEXT | ||
| 299 | if (s->hit) | 308 | if (s->hit) |
| 300 | s->state=SSL3_ST_SW_CHANGE_A; | 309 | { |
| 310 | if (s->tlsext_ticket_expected) | ||
| 311 | s->state=SSL3_ST_SW_SESSION_TICKET_A; | ||
| 312 | else | ||
| 313 | s->state=SSL3_ST_SW_CHANGE_A; | ||
| 314 | } | ||
| 315 | #else | ||
| 316 | if (s->hit) | ||
| 317 | s->state=SSL3_ST_SW_CHANGE_A; | ||
| 318 | #endif | ||
| 301 | else | 319 | else |
| 302 | s->state=SSL3_ST_SW_CERT_A; | 320 | s->state=SSL3_ST_SW_CERT_A; |
| 303 | s->init_num=0; | 321 | s->init_num=0; |
| @@ -305,27 +323,43 @@ int dtls1_accept(SSL *s) | |||
| 305 | 323 | ||
| 306 | case SSL3_ST_SW_CERT_A: | 324 | case SSL3_ST_SW_CERT_A: |
| 307 | case SSL3_ST_SW_CERT_B: | 325 | case SSL3_ST_SW_CERT_B: |
| 308 | /* Check if it is anon DH */ | 326 | /* Check if it is anon DH or normal PSK */ |
| 309 | if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) | 327 | if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) |
| 328 | && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) | ||
| 310 | { | 329 | { |
| 330 | dtls1_start_timer(s); | ||
| 311 | ret=dtls1_send_server_certificate(s); | 331 | ret=dtls1_send_server_certificate(s); |
| 312 | if (ret <= 0) goto end; | 332 | if (ret <= 0) goto end; |
| 333 | #ifndef OPENSSL_NO_TLSEXT | ||
| 334 | if (s->tlsext_status_expected) | ||
| 335 | s->state=SSL3_ST_SW_CERT_STATUS_A; | ||
| 336 | else | ||
| 337 | s->state=SSL3_ST_SW_KEY_EXCH_A; | ||
| 338 | } | ||
| 339 | else | ||
| 340 | { | ||
| 341 | skip = 1; | ||
| 342 | s->state=SSL3_ST_SW_KEY_EXCH_A; | ||
| 343 | } | ||
| 344 | #else | ||
| 313 | } | 345 | } |
| 314 | else | 346 | else |
| 315 | skip=1; | 347 | skip=1; |
| 348 | |||
| 316 | s->state=SSL3_ST_SW_KEY_EXCH_A; | 349 | s->state=SSL3_ST_SW_KEY_EXCH_A; |
| 350 | #endif | ||
| 317 | s->init_num=0; | 351 | s->init_num=0; |
| 318 | break; | 352 | break; |
| 319 | 353 | ||
| 320 | case SSL3_ST_SW_KEY_EXCH_A: | 354 | case SSL3_ST_SW_KEY_EXCH_A: |
| 321 | case SSL3_ST_SW_KEY_EXCH_B: | 355 | case SSL3_ST_SW_KEY_EXCH_B: |
| 322 | l=s->s3->tmp.new_cipher->algorithms; | 356 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 323 | 357 | ||
| 324 | /* clear this, it may get reset by | 358 | /* clear this, it may get reset by |
| 325 | * send_server_key_exchange */ | 359 | * send_server_key_exchange */ |
| 326 | if ((s->options & SSL_OP_EPHEMERAL_RSA) | 360 | if ((s->options & SSL_OP_EPHEMERAL_RSA) |
| 327 | #ifndef OPENSSL_NO_KRB5 | 361 | #ifndef OPENSSL_NO_KRB5 |
| 328 | && !(l & SSL_KRB5) | 362 | && !(alg_k & SSL_kKRB5) |
| 329 | #endif /* OPENSSL_NO_KRB5 */ | 363 | #endif /* OPENSSL_NO_KRB5 */ |
| 330 | ) | 364 | ) |
| 331 | /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key | 365 | /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key |
| @@ -336,11 +370,17 @@ int dtls1_accept(SSL *s) | |||
| 336 | else | 370 | else |
| 337 | s->s3->tmp.use_rsa_tmp=0; | 371 | s->s3->tmp.use_rsa_tmp=0; |
| 338 | 372 | ||
| 339 | /* only send if a DH key exchange, fortezza or | 373 | /* only send if a DH key exchange or |
| 340 | * RSA but we have a sign only certificate */ | 374 | * RSA but we have a sign only certificate */ |
| 341 | if (s->s3->tmp.use_rsa_tmp | 375 | if (s->s3->tmp.use_rsa_tmp |
| 342 | || (l & (SSL_DH|SSL_kFZA)) | 376 | /* PSK: send ServerKeyExchange if PSK identity |
| 343 | || ((l & SSL_kRSA) | 377 | * hint if provided */ |
| 378 | #ifndef OPENSSL_NO_PSK | ||
| 379 | || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint) | ||
| 380 | #endif | ||
| 381 | || (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) | ||
| 382 | || (alg_k & SSL_kEECDH) | ||
| 383 | || ((alg_k & SSL_kRSA) | ||
| 344 | && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL | 384 | && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL |
| 345 | || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) | 385 | || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) |
| 346 | && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher) | 386 | && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher) |
| @@ -349,6 +389,7 @@ int dtls1_accept(SSL *s) | |||
| 349 | ) | 389 | ) |
| 350 | ) | 390 | ) |
| 351 | { | 391 | { |
| 392 | dtls1_start_timer(s); | ||
| 352 | ret=dtls1_send_server_key_exchange(s); | 393 | ret=dtls1_send_server_key_exchange(s); |
| 353 | if (ret <= 0) goto end; | 394 | if (ret <= 0) goto end; |
| 354 | } | 395 | } |
| @@ -370,12 +411,15 @@ int dtls1_accept(SSL *s) | |||
| 370 | /* never request cert in anonymous ciphersuites | 411 | /* never request cert in anonymous ciphersuites |
| 371 | * (see section "Certificate request" in SSL 3 drafts | 412 | * (see section "Certificate request" in SSL 3 drafts |
| 372 | * and in RFC 2246): */ | 413 | * and in RFC 2246): */ |
| 373 | ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) && | 414 | ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) && |
| 374 | /* ... except when the application insists on verification | 415 | /* ... except when the application insists on verification |
| 375 | * (against the specs, but s3_clnt.c accepts this for SSL 3) */ | 416 | * (against the specs, but s3_clnt.c accepts this for SSL 3) */ |
| 376 | !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) || | 417 | !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) || |
| 377 | /* never request cert in Kerberos ciphersuites */ | 418 | /* never request cert in Kerberos ciphersuites */ |
| 378 | (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5)) | 419 | (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) |
| 420 | /* With normal PSK Certificates and | ||
| 421 | * Certificate Requests are omitted */ | ||
| 422 | || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) | ||
| 379 | { | 423 | { |
| 380 | /* no cert request */ | 424 | /* no cert request */ |
| 381 | skip=1; | 425 | skip=1; |
| @@ -385,6 +429,7 @@ int dtls1_accept(SSL *s) | |||
| 385 | else | 429 | else |
| 386 | { | 430 | { |
| 387 | s->s3->tmp.cert_request=1; | 431 | s->s3->tmp.cert_request=1; |
| 432 | dtls1_start_timer(s); | ||
| 388 | ret=dtls1_send_certificate_request(s); | 433 | ret=dtls1_send_certificate_request(s); |
| 389 | if (ret <= 0) goto end; | 434 | if (ret <= 0) goto end; |
| 390 | #ifndef NETSCAPE_HANG_BUG | 435 | #ifndef NETSCAPE_HANG_BUG |
| @@ -399,6 +444,7 @@ int dtls1_accept(SSL *s) | |||
| 399 | 444 | ||
| 400 | case SSL3_ST_SW_SRVR_DONE_A: | 445 | case SSL3_ST_SW_SRVR_DONE_A: |
| 401 | case SSL3_ST_SW_SRVR_DONE_B: | 446 | case SSL3_ST_SW_SRVR_DONE_B: |
| 447 | dtls1_start_timer(s); | ||
| 402 | ret=dtls1_send_server_done(s); | 448 | ret=dtls1_send_server_done(s); |
| 403 | if (ret <= 0) goto end; | 449 | if (ret <= 0) goto end; |
| 404 | s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; | 450 | s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; |
| @@ -407,16 +453,13 @@ int dtls1_accept(SSL *s) | |||
| 407 | break; | 453 | break; |
| 408 | 454 | ||
| 409 | case SSL3_ST_SW_FLUSH: | 455 | case SSL3_ST_SW_FLUSH: |
| 410 | /* number of bytes to be flushed */ | 456 | s->rwstate=SSL_WRITING; |
| 411 | num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL); | 457 | if (BIO_flush(s->wbio) <= 0) |
| 412 | if (num1 > 0) | ||
| 413 | { | 458 | { |
| 414 | s->rwstate=SSL_WRITING; | 459 | ret= -1; |
| 415 | num1=BIO_flush(s->wbio); | 460 | goto end; |
| 416 | if (num1 <= 0) { ret= -1; goto end; } | ||
| 417 | s->rwstate=SSL_NOTHING; | ||
| 418 | } | 461 | } |
| 419 | 462 | s->rwstate=SSL_NOTHING; | |
| 420 | s->state=s->s3->tmp.next_state; | 463 | s->state=s->s3->tmp.next_state; |
| 421 | break; | 464 | break; |
| 422 | 465 | ||
| @@ -426,6 +469,7 @@ int dtls1_accept(SSL *s) | |||
| 426 | ret = ssl3_check_client_hello(s); | 469 | ret = ssl3_check_client_hello(s); |
| 427 | if (ret <= 0) | 470 | if (ret <= 0) |
| 428 | goto end; | 471 | goto end; |
| 472 | dtls1_stop_timer(s); | ||
| 429 | if (ret == 2) | 473 | if (ret == 2) |
| 430 | s->state = SSL3_ST_SR_CLNT_HELLO_C; | 474 | s->state = SSL3_ST_SR_CLNT_HELLO_C; |
| 431 | else { | 475 | else { |
| @@ -433,6 +477,7 @@ int dtls1_accept(SSL *s) | |||
| 433 | * have not asked for it :-) */ | 477 | * have not asked for it :-) */ |
| 434 | ret=ssl3_get_client_certificate(s); | 478 | ret=ssl3_get_client_certificate(s); |
| 435 | if (ret <= 0) goto end; | 479 | if (ret <= 0) goto end; |
| 480 | dtls1_stop_timer(s); | ||
| 436 | s->init_num=0; | 481 | s->init_num=0; |
| 437 | s->state=SSL3_ST_SR_KEY_EXCH_A; | 482 | s->state=SSL3_ST_SR_KEY_EXCH_A; |
| 438 | } | 483 | } |
| @@ -442,26 +487,44 @@ int dtls1_accept(SSL *s) | |||
| 442 | case SSL3_ST_SR_KEY_EXCH_B: | 487 | case SSL3_ST_SR_KEY_EXCH_B: |
| 443 | ret=ssl3_get_client_key_exchange(s); | 488 | ret=ssl3_get_client_key_exchange(s); |
| 444 | if (ret <= 0) goto end; | 489 | if (ret <= 0) goto end; |
| 490 | dtls1_stop_timer(s); | ||
| 445 | s->state=SSL3_ST_SR_CERT_VRFY_A; | 491 | s->state=SSL3_ST_SR_CERT_VRFY_A; |
| 446 | s->init_num=0; | 492 | s->init_num=0; |
| 447 | 493 | ||
| 448 | /* We need to get hashes here so if there is | 494 | if (ret == 2) |
| 449 | * a client cert, it can be verified */ | 495 | { |
| 450 | s->method->ssl3_enc->cert_verify_mac(s, | 496 | /* For the ECDH ciphersuites when |
| 451 | &(s->s3->finish_dgst1), | 497 | * the client sends its ECDH pub key in |
| 452 | &(s->s3->tmp.cert_verify_md[0])); | 498 | * a certificate, the CertificateVerify |
| 453 | s->method->ssl3_enc->cert_verify_mac(s, | 499 | * message is not sent. |
| 454 | &(s->s3->finish_dgst2), | 500 | */ |
| 455 | &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH])); | 501 | s->state=SSL3_ST_SR_FINISHED_A; |
| 502 | s->init_num = 0; | ||
| 503 | } | ||
| 504 | else | ||
| 505 | { | ||
| 506 | s->state=SSL3_ST_SR_CERT_VRFY_A; | ||
| 507 | s->init_num=0; | ||
| 456 | 508 | ||
| 509 | /* We need to get hashes here so if there is | ||
| 510 | * a client cert, it can be verified */ | ||
| 511 | s->method->ssl3_enc->cert_verify_mac(s, | ||
| 512 | NID_md5, | ||
| 513 | &(s->s3->tmp.cert_verify_md[0])); | ||
| 514 | s->method->ssl3_enc->cert_verify_mac(s, | ||
| 515 | NID_sha1, | ||
| 516 | &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH])); | ||
| 517 | } | ||
| 457 | break; | 518 | break; |
| 458 | 519 | ||
| 459 | case SSL3_ST_SR_CERT_VRFY_A: | 520 | case SSL3_ST_SR_CERT_VRFY_A: |
| 460 | case SSL3_ST_SR_CERT_VRFY_B: | 521 | case SSL3_ST_SR_CERT_VRFY_B: |
| 461 | 522 | ||
| 523 | s->d1->change_cipher_spec_ok = 1; | ||
| 462 | /* we should decide if we expected this one */ | 524 | /* we should decide if we expected this one */ |
| 463 | ret=ssl3_get_cert_verify(s); | 525 | ret=ssl3_get_cert_verify(s); |
| 464 | if (ret <= 0) goto end; | 526 | if (ret <= 0) goto end; |
| 527 | dtls1_stop_timer(s); | ||
| 465 | 528 | ||
| 466 | s->state=SSL3_ST_SR_FINISHED_A; | 529 | s->state=SSL3_ST_SR_FINISHED_A; |
| 467 | s->init_num=0; | 530 | s->init_num=0; |
| @@ -469,16 +532,41 @@ int dtls1_accept(SSL *s) | |||
| 469 | 532 | ||
| 470 | case SSL3_ST_SR_FINISHED_A: | 533 | case SSL3_ST_SR_FINISHED_A: |
| 471 | case SSL3_ST_SR_FINISHED_B: | 534 | case SSL3_ST_SR_FINISHED_B: |
| 535 | s->d1->change_cipher_spec_ok = 1; | ||
| 472 | ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A, | 536 | ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A, |
| 473 | SSL3_ST_SR_FINISHED_B); | 537 | SSL3_ST_SR_FINISHED_B); |
| 474 | if (ret <= 0) goto end; | 538 | if (ret <= 0) goto end; |
| 539 | dtls1_stop_timer(s); | ||
| 475 | if (s->hit) | 540 | if (s->hit) |
| 476 | s->state=SSL_ST_OK; | 541 | s->state=SSL_ST_OK; |
| 542 | #ifndef OPENSSL_NO_TLSEXT | ||
| 543 | else if (s->tlsext_ticket_expected) | ||
| 544 | s->state=SSL3_ST_SW_SESSION_TICKET_A; | ||
| 545 | #endif | ||
| 477 | else | 546 | else |
| 478 | s->state=SSL3_ST_SW_CHANGE_A; | 547 | s->state=SSL3_ST_SW_CHANGE_A; |
| 479 | s->init_num=0; | 548 | s->init_num=0; |
| 480 | break; | 549 | break; |
| 481 | 550 | ||
| 551 | #ifndef OPENSSL_NO_TLSEXT | ||
| 552 | case SSL3_ST_SW_SESSION_TICKET_A: | ||
| 553 | case SSL3_ST_SW_SESSION_TICKET_B: | ||
| 554 | ret=dtls1_send_newsession_ticket(s); | ||
| 555 | if (ret <= 0) goto end; | ||
| 556 | s->state=SSL3_ST_SW_CHANGE_A; | ||
| 557 | s->init_num=0; | ||
| 558 | break; | ||
| 559 | |||
| 560 | case SSL3_ST_SW_CERT_STATUS_A: | ||
| 561 | case SSL3_ST_SW_CERT_STATUS_B: | ||
| 562 | ret=ssl3_send_cert_status(s); | ||
| 563 | if (ret <= 0) goto end; | ||
| 564 | s->state=SSL3_ST_SW_KEY_EXCH_A; | ||
| 565 | s->init_num=0; | ||
| 566 | break; | ||
| 567 | |||
| 568 | #endif | ||
| 569 | |||
| 482 | case SSL3_ST_SW_CHANGE_A: | 570 | case SSL3_ST_SW_CHANGE_A: |
| 483 | case SSL3_ST_SW_CHANGE_B: | 571 | case SSL3_ST_SW_CHANGE_B: |
| 484 | 572 | ||
| @@ -554,6 +642,7 @@ int dtls1_accept(SSL *s) | |||
| 554 | s->d1->handshake_read_seq = 0; | 642 | s->d1->handshake_read_seq = 0; |
| 555 | /* next message is server hello */ | 643 | /* next message is server hello */ |
| 556 | s->d1->handshake_write_seq = 0; | 644 | s->d1->handshake_write_seq = 0; |
| 645 | s->d1->next_handshake_write_seq = 0; | ||
| 557 | goto end; | 646 | goto end; |
| 558 | /* break; */ | 647 | /* break; */ |
| 559 | 648 | ||
| @@ -624,22 +713,16 @@ int dtls1_send_hello_verify_request(SSL *s) | |||
| 624 | buf = (unsigned char *)s->init_buf->data; | 713 | buf = (unsigned char *)s->init_buf->data; |
| 625 | 714 | ||
| 626 | msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]); | 715 | msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]); |
| 627 | if (s->client_version == DTLS1_BAD_VER) | 716 | *(p++) = s->version >> 8; |
| 628 | *(p++) = DTLS1_BAD_VER>>8, | 717 | *(p++) = s->version & 0xFF; |
| 629 | *(p++) = DTLS1_BAD_VER&0xff; | ||
| 630 | else | ||
| 631 | *(p++) = s->version >> 8, | ||
| 632 | *(p++) = s->version & 0xFF; | ||
| 633 | 718 | ||
| 634 | if (s->ctx->app_gen_cookie_cb != NULL && | 719 | if (s->ctx->app_gen_cookie_cb == NULL || |
| 635 | s->ctx->app_gen_cookie_cb(s, s->d1->cookie, | 720 | s->ctx->app_gen_cookie_cb(s, s->d1->cookie, |
| 636 | &(s->d1->cookie_len)) == 0) | 721 | &(s->d1->cookie_len)) == 0) |
| 637 | { | 722 | { |
| 638 | SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST,ERR_R_INTERNAL_ERROR); | 723 | SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST,ERR_R_INTERNAL_ERROR); |
| 639 | return 0; | 724 | return 0; |
| 640 | } | 725 | } |
| 641 | /* else the cookie is assumed to have | ||
| 642 | * been initialized by the application */ | ||
| 643 | 726 | ||
| 644 | *(p++) = (unsigned char) s->d1->cookie_len; | 727 | *(p++) = (unsigned char) s->d1->cookie_len; |
| 645 | memcpy(p, s->d1->cookie, s->d1->cookie_len); | 728 | memcpy(p, s->d1->cookie, s->d1->cookie_len); |
| @@ -680,12 +763,8 @@ int dtls1_send_server_hello(SSL *s) | |||
| 680 | /* Do the message type and length last */ | 763 | /* Do the message type and length last */ |
| 681 | d=p= &(buf[DTLS1_HM_HEADER_LENGTH]); | 764 | d=p= &(buf[DTLS1_HM_HEADER_LENGTH]); |
| 682 | 765 | ||
| 683 | if (s->client_version == DTLS1_BAD_VER) | 766 | *(p++)=s->version>>8; |
| 684 | *(p++)=DTLS1_BAD_VER>>8, | 767 | *(p++)=s->version&0xff; |
| 685 | *(p++)=DTLS1_BAD_VER&0xff; | ||
| 686 | else | ||
| 687 | *(p++)=s->version>>8, | ||
| 688 | *(p++)=s->version&0xff; | ||
| 689 | 768 | ||
| 690 | /* Random stuff */ | 769 | /* Random stuff */ |
| 691 | memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); | 770 | memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); |
| @@ -713,6 +792,8 @@ int dtls1_send_server_hello(SSL *s) | |||
| 713 | p+=sl; | 792 | p+=sl; |
| 714 | 793 | ||
| 715 | /* put the cipher */ | 794 | /* put the cipher */ |
| 795 | if (s->s3->tmp.new_cipher == NULL) | ||
| 796 | return -1; | ||
| 716 | i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p); | 797 | i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p); |
| 717 | p+=i; | 798 | p+=i; |
| 718 | 799 | ||
| @@ -726,6 +807,14 @@ int dtls1_send_server_hello(SSL *s) | |||
| 726 | *(p++)=s->s3->tmp.new_compression->id; | 807 | *(p++)=s->s3->tmp.new_compression->id; |
| 727 | #endif | 808 | #endif |
| 728 | 809 | ||
| 810 | #ifndef OPENSSL_NO_TLSEXT | ||
| 811 | if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL) | ||
| 812 | { | ||
| 813 | SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO,ERR_R_INTERNAL_ERROR); | ||
| 814 | return -1; | ||
| 815 | } | ||
| 816 | #endif | ||
| 817 | |||
| 729 | /* do the header */ | 818 | /* do the header */ |
| 730 | l=(p-d); | 819 | l=(p-d); |
| 731 | d=buf; | 820 | d=buf; |
| @@ -781,6 +870,13 @@ int dtls1_send_server_key_exchange(SSL *s) | |||
| 781 | #ifndef OPENSSL_NO_DH | 870 | #ifndef OPENSSL_NO_DH |
| 782 | DH *dh=NULL,*dhp; | 871 | DH *dh=NULL,*dhp; |
| 783 | #endif | 872 | #endif |
| 873 | #ifndef OPENSSL_NO_ECDH | ||
| 874 | EC_KEY *ecdh=NULL, *ecdhp; | ||
| 875 | unsigned char *encodedPoint = NULL; | ||
| 876 | int encodedlen = 0; | ||
| 877 | int curve_id = 0; | ||
| 878 | BN_CTX *bn_ctx = NULL; | ||
| 879 | #endif | ||
| 784 | EVP_PKEY *pkey; | 880 | EVP_PKEY *pkey; |
| 785 | unsigned char *p,*d; | 881 | unsigned char *p,*d; |
| 786 | int al,i; | 882 | int al,i; |
| @@ -795,7 +891,7 @@ int dtls1_send_server_key_exchange(SSL *s) | |||
| 795 | EVP_MD_CTX_init(&md_ctx); | 891 | EVP_MD_CTX_init(&md_ctx); |
| 796 | if (s->state == SSL3_ST_SW_KEY_EXCH_A) | 892 | if (s->state == SSL3_ST_SW_KEY_EXCH_A) |
| 797 | { | 893 | { |
| 798 | type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK; | 894 | type=s->s3->tmp.new_cipher->algorithm_mkey; |
| 799 | cert=s->cert; | 895 | cert=s->cert; |
| 800 | 896 | ||
| 801 | buf=s->init_buf; | 897 | buf=s->init_buf; |
| @@ -889,6 +985,142 @@ int dtls1_send_server_key_exchange(SSL *s) | |||
| 889 | } | 985 | } |
| 890 | else | 986 | else |
| 891 | #endif | 987 | #endif |
| 988 | #ifndef OPENSSL_NO_ECDH | ||
| 989 | if (type & SSL_kEECDH) | ||
| 990 | { | ||
| 991 | const EC_GROUP *group; | ||
| 992 | |||
| 993 | ecdhp=cert->ecdh_tmp; | ||
| 994 | if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL)) | ||
| 995 | { | ||
| 996 | ecdhp=s->cert->ecdh_tmp_cb(s, | ||
| 997 | SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), | ||
| 998 | SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); | ||
| 999 | } | ||
| 1000 | if (ecdhp == NULL) | ||
| 1001 | { | ||
| 1002 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
| 1003 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY); | ||
| 1004 | goto f_err; | ||
| 1005 | } | ||
| 1006 | |||
| 1007 | if (s->s3->tmp.ecdh != NULL) | ||
| 1008 | { | ||
| 1009 | EC_KEY_free(s->s3->tmp.ecdh); | ||
| 1010 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | ||
| 1011 | goto err; | ||
| 1012 | } | ||
| 1013 | |||
| 1014 | /* Duplicate the ECDH structure. */ | ||
| 1015 | if (ecdhp == NULL) | ||
| 1016 | { | ||
| 1017 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); | ||
| 1018 | goto err; | ||
| 1019 | } | ||
| 1020 | if (!EC_KEY_up_ref(ecdhp)) | ||
| 1021 | { | ||
| 1022 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); | ||
| 1023 | goto err; | ||
| 1024 | } | ||
| 1025 | ecdh = ecdhp; | ||
| 1026 | |||
| 1027 | s->s3->tmp.ecdh=ecdh; | ||
| 1028 | if ((EC_KEY_get0_public_key(ecdh) == NULL) || | ||
| 1029 | (EC_KEY_get0_private_key(ecdh) == NULL) || | ||
| 1030 | (s->options & SSL_OP_SINGLE_ECDH_USE)) | ||
| 1031 | { | ||
| 1032 | if(!EC_KEY_generate_key(ecdh)) | ||
| 1033 | { | ||
| 1034 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); | ||
| 1035 | goto err; | ||
| 1036 | } | ||
| 1037 | } | ||
| 1038 | |||
| 1039 | if (((group = EC_KEY_get0_group(ecdh)) == NULL) || | ||
| 1040 | (EC_KEY_get0_public_key(ecdh) == NULL) || | ||
| 1041 | (EC_KEY_get0_private_key(ecdh) == NULL)) | ||
| 1042 | { | ||
| 1043 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); | ||
| 1044 | goto err; | ||
| 1045 | } | ||
| 1046 | |||
| 1047 | if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && | ||
| 1048 | (EC_GROUP_get_degree(group) > 163)) | ||
| 1049 | { | ||
| 1050 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER); | ||
| 1051 | goto err; | ||
| 1052 | } | ||
| 1053 | |||
| 1054 | /* XXX: For now, we only support ephemeral ECDH | ||
| 1055 | * keys over named (not generic) curves. For | ||
| 1056 | * supported named curves, curve_id is non-zero. | ||
| 1057 | */ | ||
| 1058 | if ((curve_id = | ||
| 1059 | tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group))) | ||
| 1060 | == 0) | ||
| 1061 | { | ||
| 1062 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); | ||
| 1063 | goto err; | ||
| 1064 | } | ||
| 1065 | |||
| 1066 | /* Encode the public key. | ||
| 1067 | * First check the size of encoding and | ||
| 1068 | * allocate memory accordingly. | ||
| 1069 | */ | ||
| 1070 | encodedlen = EC_POINT_point2oct(group, | ||
| 1071 | EC_KEY_get0_public_key(ecdh), | ||
| 1072 | POINT_CONVERSION_UNCOMPRESSED, | ||
| 1073 | NULL, 0, NULL); | ||
| 1074 | |||
| 1075 | encodedPoint = (unsigned char *) | ||
| 1076 | OPENSSL_malloc(encodedlen*sizeof(unsigned char)); | ||
| 1077 | bn_ctx = BN_CTX_new(); | ||
| 1078 | if ((encodedPoint == NULL) || (bn_ctx == NULL)) | ||
| 1079 | { | ||
| 1080 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); | ||
| 1081 | goto err; | ||
| 1082 | } | ||
| 1083 | |||
| 1084 | |||
| 1085 | encodedlen = EC_POINT_point2oct(group, | ||
| 1086 | EC_KEY_get0_public_key(ecdh), | ||
| 1087 | POINT_CONVERSION_UNCOMPRESSED, | ||
| 1088 | encodedPoint, encodedlen, bn_ctx); | ||
| 1089 | |||
| 1090 | if (encodedlen == 0) | ||
| 1091 | { | ||
| 1092 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); | ||
| 1093 | goto err; | ||
| 1094 | } | ||
| 1095 | |||
| 1096 | BN_CTX_free(bn_ctx); bn_ctx=NULL; | ||
| 1097 | |||
| 1098 | /* XXX: For now, we only support named (not | ||
| 1099 | * generic) curves in ECDH ephemeral key exchanges. | ||
| 1100 | * In this situation, we need four additional bytes | ||
| 1101 | * to encode the entire ServerECDHParams | ||
| 1102 | * structure. | ||
| 1103 | */ | ||
| 1104 | n = 4 + encodedlen; | ||
| 1105 | |||
| 1106 | /* We'll generate the serverKeyExchange message | ||
| 1107 | * explicitly so we can set these to NULLs | ||
| 1108 | */ | ||
| 1109 | r[0]=NULL; | ||
| 1110 | r[1]=NULL; | ||
| 1111 | r[2]=NULL; | ||
| 1112 | r[3]=NULL; | ||
| 1113 | } | ||
| 1114 | else | ||
| 1115 | #endif /* !OPENSSL_NO_ECDH */ | ||
| 1116 | #ifndef OPENSSL_NO_PSK | ||
| 1117 | if (type & SSL_kPSK) | ||
| 1118 | { | ||
| 1119 | /* reserve size for record length and PSK identity hint*/ | ||
| 1120 | n+=2+strlen(s->ctx->psk_identity_hint); | ||
| 1121 | } | ||
| 1122 | else | ||
| 1123 | #endif /* !OPENSSL_NO_PSK */ | ||
| 892 | { | 1124 | { |
| 893 | al=SSL_AD_HANDSHAKE_FAILURE; | 1125 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 894 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); | 1126 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); |
| @@ -900,7 +1132,8 @@ int dtls1_send_server_key_exchange(SSL *s) | |||
| 900 | n+=2+nr[i]; | 1132 | n+=2+nr[i]; |
| 901 | } | 1133 | } |
| 902 | 1134 | ||
| 903 | if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) | 1135 | if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) |
| 1136 | && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) | ||
| 904 | { | 1137 | { |
| 905 | if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher)) | 1138 | if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher)) |
| 906 | == NULL) | 1139 | == NULL) |
| @@ -931,6 +1164,41 @@ int dtls1_send_server_key_exchange(SSL *s) | |||
| 931 | p+=nr[i]; | 1164 | p+=nr[i]; |
| 932 | } | 1165 | } |
| 933 | 1166 | ||
| 1167 | #ifndef OPENSSL_NO_ECDH | ||
| 1168 | if (type & SSL_kEECDH) | ||
| 1169 | { | ||
| 1170 | /* XXX: For now, we only support named (not generic) curves. | ||
| 1171 | * In this situation, the serverKeyExchange message has: | ||
| 1172 | * [1 byte CurveType], [2 byte CurveName] | ||
| 1173 | * [1 byte length of encoded point], followed by | ||
| 1174 | * the actual encoded point itself | ||
| 1175 | */ | ||
| 1176 | *p = NAMED_CURVE_TYPE; | ||
| 1177 | p += 1; | ||
| 1178 | *p = 0; | ||
| 1179 | p += 1; | ||
| 1180 | *p = curve_id; | ||
| 1181 | p += 1; | ||
| 1182 | *p = encodedlen; | ||
| 1183 | p += 1; | ||
| 1184 | memcpy((unsigned char*)p, | ||
| 1185 | (unsigned char *)encodedPoint, | ||
| 1186 | encodedlen); | ||
| 1187 | OPENSSL_free(encodedPoint); | ||
| 1188 | p += encodedlen; | ||
| 1189 | } | ||
| 1190 | #endif | ||
| 1191 | |||
| 1192 | #ifndef OPENSSL_NO_PSK | ||
| 1193 | if (type & SSL_kPSK) | ||
| 1194 | { | ||
| 1195 | /* copy PSK identity hint */ | ||
| 1196 | s2n(strlen(s->ctx->psk_identity_hint), p); | ||
| 1197 | strncpy((char *)p, s->ctx->psk_identity_hint, strlen(s->ctx->psk_identity_hint)); | ||
| 1198 | p+=strlen(s->ctx->psk_identity_hint); | ||
| 1199 | } | ||
| 1200 | #endif | ||
| 1201 | |||
| 934 | /* not anonymous */ | 1202 | /* not anonymous */ |
| 935 | if (pkey != NULL) | 1203 | if (pkey != NULL) |
| 936 | { | 1204 | { |
| @@ -984,6 +1252,25 @@ int dtls1_send_server_key_exchange(SSL *s) | |||
| 984 | } | 1252 | } |
| 985 | else | 1253 | else |
| 986 | #endif | 1254 | #endif |
| 1255 | #if !defined(OPENSSL_NO_ECDSA) | ||
| 1256 | if (pkey->type == EVP_PKEY_EC) | ||
| 1257 | { | ||
| 1258 | /* let's do ECDSA */ | ||
| 1259 | EVP_SignInit_ex(&md_ctx,EVP_ecdsa(), NULL); | ||
| 1260 | EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); | ||
| 1261 | EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); | ||
| 1262 | EVP_SignUpdate(&md_ctx,&(d[4]),n); | ||
| 1263 | if (!EVP_SignFinal(&md_ctx,&(p[2]), | ||
| 1264 | (unsigned int *)&i,pkey)) | ||
| 1265 | { | ||
| 1266 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_ECDSA); | ||
| 1267 | goto err; | ||
| 1268 | } | ||
| 1269 | s2n(i,p); | ||
| 1270 | n+=i+2; | ||
| 1271 | } | ||
| 1272 | else | ||
| 1273 | #endif | ||
| 987 | { | 1274 | { |
| 988 | /* Is this error check actually needed? */ | 1275 | /* Is this error check actually needed? */ |
| 989 | al=SSL_AD_HANDSHAKE_FAILURE; | 1276 | al=SSL_AD_HANDSHAKE_FAILURE; |
| @@ -1010,6 +1297,10 @@ int dtls1_send_server_key_exchange(SSL *s) | |||
| 1010 | f_err: | 1297 | f_err: |
| 1011 | ssl3_send_alert(s,SSL3_AL_FATAL,al); | 1298 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1012 | err: | 1299 | err: |
| 1300 | #ifndef OPENSSL_NO_ECDH | ||
| 1301 | if (encodedPoint != NULL) OPENSSL_free(encodedPoint); | ||
| 1302 | BN_CTX_free(bn_ctx); | ||
| 1303 | #endif | ||
| 1013 | EVP_MD_CTX_cleanup(&md_ctx); | 1304 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1014 | return(-1); | 1305 | return(-1); |
| 1015 | } | 1306 | } |
| @@ -1123,14 +1414,15 @@ int dtls1_send_server_certificate(SSL *s) | |||
| 1123 | if (s->state == SSL3_ST_SW_CERT_A) | 1414 | if (s->state == SSL3_ST_SW_CERT_A) |
| 1124 | { | 1415 | { |
| 1125 | x=ssl_get_server_send_cert(s); | 1416 | x=ssl_get_server_send_cert(s); |
| 1126 | if (x == NULL && | 1417 | if (x == NULL) |
| 1127 | /* VRS: allow null cert if auth == KRB5 */ | ||
| 1128 | (s->s3->tmp.new_cipher->algorithms | ||
| 1129 | & (SSL_MKEY_MASK|SSL_AUTH_MASK)) | ||
| 1130 | != (SSL_aKRB5|SSL_kKRB5)) | ||
| 1131 | { | 1418 | { |
| 1132 | SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR); | 1419 | /* VRS: allow null cert if auth == KRB5 */ |
| 1133 | return(0); | 1420 | if ((s->s3->tmp.new_cipher->algorithm_mkey != SSL_kKRB5) || |
| 1421 | (s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5)) | ||
| 1422 | { | ||
| 1423 | SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR); | ||
| 1424 | return(0); | ||
| 1425 | } | ||
| 1134 | } | 1426 | } |
| 1135 | 1427 | ||
| 1136 | l=dtls1_output_cert_chain(s,x); | 1428 | l=dtls1_output_cert_chain(s,x); |
| @@ -1145,3 +1437,115 @@ int dtls1_send_server_certificate(SSL *s) | |||
| 1145 | /* SSL3_ST_SW_CERT_B */ | 1437 | /* SSL3_ST_SW_CERT_B */ |
| 1146 | return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); | 1438 | return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); |
| 1147 | } | 1439 | } |
| 1440 | |||
| 1441 | #ifndef OPENSSL_NO_TLSEXT | ||
| 1442 | int dtls1_send_newsession_ticket(SSL *s) | ||
| 1443 | { | ||
| 1444 | if (s->state == SSL3_ST_SW_SESSION_TICKET_A) | ||
| 1445 | { | ||
| 1446 | unsigned char *p, *senc, *macstart; | ||
| 1447 | int len, slen; | ||
| 1448 | unsigned int hlen, msg_len; | ||
| 1449 | EVP_CIPHER_CTX ctx; | ||
| 1450 | HMAC_CTX hctx; | ||
| 1451 | SSL_CTX *tctx = s->initial_ctx; | ||
| 1452 | unsigned char iv[EVP_MAX_IV_LENGTH]; | ||
| 1453 | unsigned char key_name[16]; | ||
| 1454 | |||
| 1455 | /* get session encoding length */ | ||
| 1456 | slen = i2d_SSL_SESSION(s->session, NULL); | ||
| 1457 | /* Some length values are 16 bits, so forget it if session is | ||
| 1458 | * too long | ||
| 1459 | */ | ||
| 1460 | if (slen > 0xFF00) | ||
| 1461 | return -1; | ||
| 1462 | /* Grow buffer if need be: the length calculation is as | ||
| 1463 | * follows 12 (DTLS handshake message header) + | ||
| 1464 | * 4 (ticket lifetime hint) + 2 (ticket length) + | ||
| 1465 | * 16 (key name) + max_iv_len (iv length) + | ||
| 1466 | * session_length + max_enc_block_size (max encrypted session | ||
| 1467 | * length) + max_md_size (HMAC). | ||
| 1468 | */ | ||
| 1469 | if (!BUF_MEM_grow(s->init_buf, | ||
| 1470 | DTLS1_HM_HEADER_LENGTH + 22 + EVP_MAX_IV_LENGTH + | ||
| 1471 | EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen)) | ||
| 1472 | return -1; | ||
| 1473 | senc = OPENSSL_malloc(slen); | ||
| 1474 | if (!senc) | ||
| 1475 | return -1; | ||
| 1476 | p = senc; | ||
| 1477 | i2d_SSL_SESSION(s->session, &p); | ||
| 1478 | |||
| 1479 | p=(unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]); | ||
| 1480 | EVP_CIPHER_CTX_init(&ctx); | ||
| 1481 | HMAC_CTX_init(&hctx); | ||
| 1482 | /* Initialize HMAC and cipher contexts. If callback present | ||
| 1483 | * it does all the work otherwise use generated values | ||
| 1484 | * from parent ctx. | ||
| 1485 | */ | ||
| 1486 | if (tctx->tlsext_ticket_key_cb) | ||
| 1487 | { | ||
| 1488 | if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, | ||
| 1489 | &hctx, 1) < 0) | ||
| 1490 | { | ||
| 1491 | OPENSSL_free(senc); | ||
| 1492 | return -1; | ||
| 1493 | } | ||
| 1494 | } | ||
| 1495 | else | ||
| 1496 | { | ||
| 1497 | RAND_pseudo_bytes(iv, 16); | ||
| 1498 | EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, | ||
| 1499 | tctx->tlsext_tick_aes_key, iv); | ||
| 1500 | HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, | ||
| 1501 | tlsext_tick_md(), NULL); | ||
| 1502 | memcpy(key_name, tctx->tlsext_tick_key_name, 16); | ||
| 1503 | } | ||
| 1504 | l2n(s->session->tlsext_tick_lifetime_hint, p); | ||
| 1505 | /* Skip ticket length for now */ | ||
| 1506 | p += 2; | ||
| 1507 | /* Output key name */ | ||
| 1508 | macstart = p; | ||
| 1509 | memcpy(p, key_name, 16); | ||
| 1510 | p += 16; | ||
| 1511 | /* output IV */ | ||
| 1512 | memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx)); | ||
| 1513 | p += EVP_CIPHER_CTX_iv_length(&ctx); | ||
| 1514 | /* Encrypt session data */ | ||
| 1515 | EVP_EncryptUpdate(&ctx, p, &len, senc, slen); | ||
| 1516 | p += len; | ||
| 1517 | EVP_EncryptFinal(&ctx, p, &len); | ||
| 1518 | p += len; | ||
| 1519 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
| 1520 | |||
| 1521 | HMAC_Update(&hctx, macstart, p - macstart); | ||
| 1522 | HMAC_Final(&hctx, p, &hlen); | ||
| 1523 | HMAC_CTX_cleanup(&hctx); | ||
| 1524 | |||
| 1525 | p += hlen; | ||
| 1526 | /* Now write out lengths: p points to end of data written */ | ||
| 1527 | /* Total length */ | ||
| 1528 | len = p - (unsigned char *)(s->init_buf->data); | ||
| 1529 | /* Ticket length */ | ||
| 1530 | p=(unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]) + 4; | ||
| 1531 | s2n(len - DTLS1_HM_HEADER_LENGTH - 6, p); | ||
| 1532 | |||
| 1533 | /* number of bytes to write */ | ||
| 1534 | s->init_num= len; | ||
| 1535 | s->state=SSL3_ST_SW_SESSION_TICKET_B; | ||
| 1536 | s->init_off=0; | ||
| 1537 | OPENSSL_free(senc); | ||
| 1538 | |||
| 1539 | /* XDTLS: set message header ? */ | ||
| 1540 | msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH; | ||
| 1541 | dtls1_set_message_header(s, (void *)s->init_buf->data, | ||
| 1542 | SSL3_MT_NEWSESSION_TICKET, msg_len, 0, msg_len); | ||
| 1543 | |||
| 1544 | /* buffer the message to handle re-xmits */ | ||
| 1545 | dtls1_buffer_message(s, 0); | ||
| 1546 | } | ||
| 1547 | |||
| 1548 | /* SSL3_ST_SW_SESSION_TICKET_B */ | ||
| 1549 | return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); | ||
| 1550 | } | ||
| 1551 | #endif | ||
