OLD | NEW |
(Empty) | |
| 1 /* ssl/s3_srvr.c -*- mode:C; c-file-style: "eay" -*- */ |
| 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 * All rights reserved. |
| 4 * |
| 5 * This package is an SSL implementation written |
| 6 * by Eric Young (eay@cryptsoft.com). |
| 7 * The implementation was written so as to conform with Netscapes SSL. |
| 8 * |
| 9 * This library is free for commercial and non-commercial use as long as |
| 10 * the following conditions are aheared to. The following conditions |
| 11 * apply to all code found in this distribution, be it the RC4, RSA, |
| 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 13 * included with this distribution is covered by the same copyright terms |
| 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 15 * |
| 16 * Copyright remains Eric Young's, and as such any Copyright notices in |
| 17 * the code are not to be removed. |
| 18 * If this package is used in a product, Eric Young should be given attribution |
| 19 * as the author of the parts of the library used. |
| 20 * This can be in the form of a textual message at program startup or |
| 21 * in documentation (online or textual) provided with the package. |
| 22 * |
| 23 * Redistribution and use in source and binary forms, with or without |
| 24 * modification, are permitted provided that the following conditions |
| 25 * are met: |
| 26 * 1. Redistributions of source code must retain the copyright |
| 27 * notice, this list of conditions and the following disclaimer. |
| 28 * 2. Redistributions in binary form must reproduce the above copyright |
| 29 * notice, this list of conditions and the following disclaimer in the |
| 30 * documentation and/or other materials provided with the distribution. |
| 31 * 3. All advertising materials mentioning features or use of this software |
| 32 * must display the following acknowledgement: |
| 33 * "This product includes cryptographic software written by |
| 34 * Eric Young (eay@cryptsoft.com)" |
| 35 * The word 'cryptographic' can be left out if the rouines from the library |
| 36 * being used are not cryptographic related :-). |
| 37 * 4. If you include any Windows specific code (or a derivative thereof) from |
| 38 * the apps directory (application code) you must include an acknowledgement: |
| 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 40 * |
| 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 51 * SUCH DAMAGE. |
| 52 * |
| 53 * The licence and distribution terms for any publically available version or |
| 54 * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 55 * copied and put under another distribution licence |
| 56 * [including the GNU Public Licence.] |
| 57 */ |
| 58 /* ==================================================================== |
| 59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 60 * |
| 61 * Redistribution and use in source and binary forms, with or without |
| 62 * modification, are permitted provided that the following conditions |
| 63 * are met: |
| 64 * |
| 65 * 1. Redistributions of source code must retain the above copyright |
| 66 * notice, this list of conditions and the following disclaimer. |
| 67 * |
| 68 * 2. Redistributions in binary form must reproduce the above copyright |
| 69 * notice, this list of conditions and the following disclaimer in |
| 70 * the documentation and/or other materials provided with the |
| 71 * distribution. |
| 72 * |
| 73 * 3. All advertising materials mentioning features or use of this |
| 74 * software must display the following acknowledgment: |
| 75 * "This product includes software developed by the OpenSSL Project |
| 76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 77 * |
| 78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 79 * endorse or promote products derived from this software without |
| 80 * prior written permission. For written permission, please contact |
| 81 * openssl-core@openssl.org. |
| 82 * |
| 83 * 5. Products derived from this software may not be called "OpenSSL" |
| 84 * nor may "OpenSSL" appear in their names without prior written |
| 85 * permission of the OpenSSL Project. |
| 86 * |
| 87 * 6. Redistributions of any form whatsoever must retain the following |
| 88 * acknowledgment: |
| 89 * "This product includes software developed by the OpenSSL Project |
| 90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 91 * |
| 92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 103 * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 104 * ==================================================================== |
| 105 * |
| 106 * This product includes cryptographic software written by Eric Young |
| 107 * (eay@cryptsoft.com). This product includes software written by Tim |
| 108 * Hudson (tjh@cryptsoft.com). |
| 109 * |
| 110 */ |
| 111 /* ==================================================================== |
| 112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 113 * |
| 114 * Portions of the attached software ("Contribution") are developed by |
| 115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
| 116 * |
| 117 * The Contribution is licensed pursuant to the OpenSSL open source |
| 118 * license provided above. |
| 119 * |
| 120 * ECC cipher suite support in OpenSSL originally written by |
| 121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. |
| 122 * |
| 123 */ |
| 124 /* ==================================================================== |
| 125 * Copyright 2005 Nokia. All rights reserved. |
| 126 * |
| 127 * The portions of the attached software ("Contribution") is developed by |
| 128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 129 * license. |
| 130 * |
| 131 * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 132 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 133 * support (see RFC 4279) to OpenSSL. |
| 134 * |
| 135 * No patent licenses or other rights except those expressly stated in |
| 136 * the OpenSSL open source license shall be deemed granted or received |
| 137 * expressly, by implication, estoppel, or otherwise. |
| 138 * |
| 139 * No assurances are provided by Nokia that the Contribution does not |
| 140 * infringe the patent or other intellectual property rights of any third |
| 141 * party or that the license provides you with all the necessary rights |
| 142 * to make use of the Contribution. |
| 143 * |
| 144 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 145 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 146 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 147 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 148 * OTHERWISE. |
| 149 */ |
| 150 |
| 151 #define REUSE_CIPHER_BUG |
| 152 #define NETSCAPE_HANG_BUG |
| 153 |
| 154 #include <stdio.h> |
| 155 #include "ssl_locl.h" |
| 156 #include "kssl_lcl.h" |
| 157 #include <openssl/buffer.h> |
| 158 #include <openssl/rand.h> |
| 159 #include <openssl/objects.h> |
| 160 #include <openssl/evp.h> |
| 161 #include <openssl/hmac.h> |
| 162 #include <openssl/x509.h> |
| 163 #ifndef OPENSSL_NO_DH |
| 164 #include <openssl/dh.h> |
| 165 #endif |
| 166 #include <openssl/bn.h> |
| 167 #ifndef OPENSSL_NO_KRB5 |
| 168 #include <openssl/krb5_asn.h> |
| 169 #endif |
| 170 #include <openssl/md5.h> |
| 171 |
| 172 static const SSL_METHOD *ssl3_get_server_method(int ver); |
| 173 |
| 174 static const SSL_METHOD *ssl3_get_server_method(int ver) |
| 175 { |
| 176 if (ver == SSL3_VERSION) |
| 177 return(SSLv3_server_method()); |
| 178 else |
| 179 return(NULL); |
| 180 } |
| 181 |
| 182 IMPLEMENT_ssl3_meth_func(SSLv3_server_method, |
| 183 ssl3_accept, |
| 184 ssl_undefined_function, |
| 185 ssl3_get_server_method) |
| 186 |
| 187 int ssl3_accept(SSL *s) |
| 188 { |
| 189 BUF_MEM *buf; |
| 190 unsigned long alg_k,Time=(unsigned long)time(NULL); |
| 191 void (*cb)(const SSL *ssl,int type,int val)=NULL; |
| 192 int ret= -1; |
| 193 int new_state,state,skip=0; |
| 194 |
| 195 RAND_add(&Time,sizeof(Time),0); |
| 196 ERR_clear_error(); |
| 197 clear_sys_error(); |
| 198 |
| 199 if (s->info_callback != NULL) |
| 200 cb=s->info_callback; |
| 201 else if (s->ctx->info_callback != NULL) |
| 202 cb=s->ctx->info_callback; |
| 203 |
| 204 /* init things to blank */ |
| 205 s->in_handshake++; |
| 206 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); |
| 207 |
| 208 if (s->cert == NULL) |
| 209 { |
| 210 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET); |
| 211 return(-1); |
| 212 } |
| 213 |
| 214 for (;;) |
| 215 { |
| 216 state=s->state; |
| 217 |
| 218 switch (s->state) |
| 219 { |
| 220 case SSL_ST_RENEGOTIATE: |
| 221 s->new_session=1; |
| 222 /* s->state=SSL_ST_ACCEPT; */ |
| 223 |
| 224 case SSL_ST_BEFORE: |
| 225 case SSL_ST_ACCEPT: |
| 226 case SSL_ST_BEFORE|SSL_ST_ACCEPT: |
| 227 case SSL_ST_OK|SSL_ST_ACCEPT: |
| 228 |
| 229 s->server=1; |
| 230 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); |
| 231 |
| 232 if ((s->version>>8) != 3) |
| 233 { |
| 234 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR); |
| 235 return -1; |
| 236 } |
| 237 s->type=SSL_ST_ACCEPT; |
| 238 |
| 239 if (s->init_buf == NULL) |
| 240 { |
| 241 if ((buf=BUF_MEM_new()) == NULL) |
| 242 { |
| 243 ret= -1; |
| 244 goto end; |
| 245 } |
| 246 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) |
| 247 { |
| 248 ret= -1; |
| 249 goto end; |
| 250 } |
| 251 s->init_buf=buf; |
| 252 } |
| 253 |
| 254 if (!ssl3_setup_buffers(s)) |
| 255 { |
| 256 ret= -1; |
| 257 goto end; |
| 258 } |
| 259 |
| 260 s->init_num=0; |
| 261 s->s3->flags &= ~SSL3_FLAGS_SGC_RESTART_DONE; |
| 262 |
| 263 if (s->state != SSL_ST_RENEGOTIATE) |
| 264 { |
| 265 /* Ok, we now need to push on a buffering BIO so
that |
| 266 * the output is sent in a way that TCP likes :-
) |
| 267 */ |
| 268 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto
end; } |
| 269 |
| 270 ssl3_init_finished_mac(s); |
| 271 s->state=SSL3_ST_SR_CLNT_HELLO_A; |
| 272 s->ctx->stats.sess_accept++; |
| 273 } |
| 274 else if (!s->s3->send_connection_binding && |
| 275 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGO
TIATION)) |
| 276 { |
| 277 /* Server attempting to renegotiate with |
| 278 * client that doesn't support secure |
| 279 * renegotiation. |
| 280 */ |
| 281 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_UNSAFE_LEGACY_RE
NEGOTIATION_DISABLED); |
| 282 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE
_FAILURE); |
| 283 ret = -1; |
| 284 goto end; |
| 285 } |
| 286 else |
| 287 { |
| 288 /* s->state == SSL_ST_RENEGOTIATE, |
| 289 * we will just send a HelloRequest */ |
| 290 s->ctx->stats.sess_accept_renegotiate++; |
| 291 s->state=SSL3_ST_SW_HELLO_REQ_A; |
| 292 } |
| 293 break; |
| 294 |
| 295 case SSL3_ST_SW_HELLO_REQ_A: |
| 296 case SSL3_ST_SW_HELLO_REQ_B: |
| 297 |
| 298 s->shutdown=0; |
| 299 ret=ssl3_send_hello_request(s); |
| 300 if (ret <= 0) goto end; |
| 301 s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C; |
| 302 s->state=SSL3_ST_SW_FLUSH; |
| 303 s->init_num=0; |
| 304 |
| 305 ssl3_init_finished_mac(s); |
| 306 break; |
| 307 |
| 308 case SSL3_ST_SW_HELLO_REQ_C: |
| 309 s->state=SSL_ST_OK; |
| 310 break; |
| 311 |
| 312 case SSL3_ST_SR_CLNT_HELLO_A: |
| 313 case SSL3_ST_SR_CLNT_HELLO_B: |
| 314 case SSL3_ST_SR_CLNT_HELLO_C: |
| 315 |
| 316 s->shutdown=0; |
| 317 ret=ssl3_get_client_hello(s); |
| 318 if (ret <= 0) goto end; |
| 319 |
| 320 s->new_session = 2; |
| 321 s->state=SSL3_ST_SW_SRVR_HELLO_A; |
| 322 s->init_num=0; |
| 323 break; |
| 324 |
| 325 case SSL3_ST_SW_SRVR_HELLO_A: |
| 326 case SSL3_ST_SW_SRVR_HELLO_B: |
| 327 ret=ssl3_send_server_hello(s); |
| 328 if (ret <= 0) goto end; |
| 329 #ifndef OPENSSL_NO_TLSEXT |
| 330 if (s->hit) |
| 331 { |
| 332 if (s->tlsext_ticket_expected) |
| 333 s->state=SSL3_ST_SW_SESSION_TICKET_A; |
| 334 else |
| 335 s->state=SSL3_ST_SW_CHANGE_A; |
| 336 } |
| 337 #else |
| 338 if (s->hit) |
| 339 s->state=SSL3_ST_SW_CHANGE_A; |
| 340 #endif |
| 341 else |
| 342 s->state=SSL3_ST_SW_CERT_A; |
| 343 s->init_num=0; |
| 344 break; |
| 345 |
| 346 case SSL3_ST_SW_CERT_A: |
| 347 case SSL3_ST_SW_CERT_B: |
| 348 /* Check if it is anon DH or anon ECDH, */ |
| 349 /* normal PSK or KRB5 */ |
| 350 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) |
| 351 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL
_kPSK) |
| 352 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL
_aKRB5)) |
| 353 { |
| 354 ret=ssl3_send_server_certificate(s); |
| 355 if (ret <= 0) goto end; |
| 356 #ifndef OPENSSL_NO_TLSEXT |
| 357 if (s->tlsext_status_expected) |
| 358 s->state=SSL3_ST_SW_CERT_STATUS_A; |
| 359 else |
| 360 s->state=SSL3_ST_SW_KEY_EXCH_A; |
| 361 } |
| 362 else |
| 363 { |
| 364 skip = 1; |
| 365 s->state=SSL3_ST_SW_KEY_EXCH_A; |
| 366 } |
| 367 #else |
| 368 } |
| 369 else |
| 370 skip=1; |
| 371 |
| 372 s->state=SSL3_ST_SW_KEY_EXCH_A; |
| 373 #endif |
| 374 s->init_num=0; |
| 375 break; |
| 376 |
| 377 case SSL3_ST_SW_KEY_EXCH_A: |
| 378 case SSL3_ST_SW_KEY_EXCH_B: |
| 379 alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 380 |
| 381 /* clear this, it may get reset by |
| 382 * send_server_key_exchange */ |
| 383 if ((s->options & SSL_OP_EPHEMERAL_RSA) |
| 384 #ifndef OPENSSL_NO_KRB5 |
| 385 && !(alg_k & SSL_kKRB5) |
| 386 #endif /* OPENSSL_NO_KRB5 */ |
| 387 ) |
| 388 /* option SSL_OP_EPHEMERAL_RSA sends temporary R
SA key |
| 389 * even when forbidden by protocol specs |
| 390 * (handshake may fail as clients are not requir
ed to |
| 391 * be able to handle this) */ |
| 392 s->s3->tmp.use_rsa_tmp=1; |
| 393 else |
| 394 s->s3->tmp.use_rsa_tmp=0; |
| 395 |
| 396 |
| 397 /* only send if a DH key exchange, fortezza or |
| 398 * RSA but we have a sign only certificate |
| 399 * |
| 400 * PSK: may send PSK identity hints |
| 401 * |
| 402 * For ECC ciphersuites, we send a serverKeyExchange |
| 403 * message only if the cipher suite is either |
| 404 * ECDH-anon or ECDHE. In other cases, the |
| 405 * server certificate contains the server's |
| 406 * public key for key exchange. |
| 407 */ |
| 408 if (s->s3->tmp.use_rsa_tmp |
| 409 /* PSK: send ServerKeyExchange if PSK identity |
| 410 * hint if provided */ |
| 411 #ifndef OPENSSL_NO_PSK |
| 412 || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint) |
| 413 #endif |
| 414 || (alg_k & (SSL_kDHr|SSL_kDHd|SSL_kEDH)) |
| 415 || (alg_k & SSL_kEECDH) |
| 416 || ((alg_k & SSL_kRSA) |
| 417 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey
== NULL |
| 418 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) |
| 419 && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY
_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher) |
| 420 ) |
| 421 ) |
| 422 ) |
| 423 ) |
| 424 { |
| 425 ret=ssl3_send_server_key_exchange(s); |
| 426 if (ret <= 0) goto end; |
| 427 } |
| 428 else |
| 429 skip=1; |
| 430 |
| 431 s->state=SSL3_ST_SW_CERT_REQ_A; |
| 432 s->init_num=0; |
| 433 break; |
| 434 |
| 435 case SSL3_ST_SW_CERT_REQ_A: |
| 436 case SSL3_ST_SW_CERT_REQ_B: |
| 437 if (/* don't request cert unless asked for it: */ |
| 438 !(s->verify_mode & SSL_VERIFY_PEER) || |
| 439 /* if SSL_VERIFY_CLIENT_ONCE is set, |
| 440 * don't request cert during re-negotiation: */ |
| 441 ((s->session->peer != NULL) && |
| 442 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || |
| 443 /* never request cert in anonymous ciphersuites |
| 444 * (see section "Certificate request" in SSL 3 d
rafts |
| 445 * and in RFC 2246): */ |
| 446 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aN
ULL) && |
| 447 /* ... except when the application insists on v
erification |
| 448 * (against the specs, but s3_clnt.c accepts th
is for SSL 3) */ |
| 449 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_C
ERT)) || |
| 450 /* never request cert in Kerberos ciphersuites
*/ |
| 451 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKR
B5) |
| 452 /* With normal PSK Certificates and |
| 453 * Certificate Requests are omitted */ |
| 454 || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_
kPSK)) |
| 455 { |
| 456 /* no cert request */ |
| 457 skip=1; |
| 458 s->s3->tmp.cert_request=0; |
| 459 s->state=SSL3_ST_SW_SRVR_DONE_A; |
| 460 } |
| 461 else |
| 462 { |
| 463 s->s3->tmp.cert_request=1; |
| 464 ret=ssl3_send_certificate_request(s); |
| 465 if (ret <= 0) goto end; |
| 466 #ifndef NETSCAPE_HANG_BUG |
| 467 s->state=SSL3_ST_SW_SRVR_DONE_A; |
| 468 #else |
| 469 s->state=SSL3_ST_SW_FLUSH; |
| 470 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; |
| 471 #endif |
| 472 s->init_num=0; |
| 473 } |
| 474 break; |
| 475 |
| 476 case SSL3_ST_SW_SRVR_DONE_A: |
| 477 case SSL3_ST_SW_SRVR_DONE_B: |
| 478 ret=ssl3_send_server_done(s); |
| 479 if (ret <= 0) goto end; |
| 480 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; |
| 481 s->state=SSL3_ST_SW_FLUSH; |
| 482 s->init_num=0; |
| 483 break; |
| 484 |
| 485 case SSL3_ST_SW_FLUSH: |
| 486 |
| 487 /* This code originally checked to see if |
| 488 * any data was pending using BIO_CTRL_INFO |
| 489 * and then flushed. This caused problems |
| 490 * as documented in PR#1939. The proposed |
| 491 * fix doesn't completely resolve this issue |
| 492 * as buggy implementations of BIO_CTRL_PENDING |
| 493 * still exist. So instead we just flush |
| 494 * unconditionally. |
| 495 */ |
| 496 |
| 497 s->rwstate=SSL_WRITING; |
| 498 if (BIO_flush(s->wbio) <= 0) |
| 499 { |
| 500 ret= -1; |
| 501 goto end; |
| 502 } |
| 503 s->rwstate=SSL_NOTHING; |
| 504 |
| 505 s->state=s->s3->tmp.next_state; |
| 506 break; |
| 507 |
| 508 case SSL3_ST_SR_CERT_A: |
| 509 case SSL3_ST_SR_CERT_B: |
| 510 /* Check for second client hello (MS SGC) */ |
| 511 ret = ssl3_check_client_hello(s); |
| 512 if (ret <= 0) |
| 513 goto end; |
| 514 if (ret == 2) |
| 515 s->state = SSL3_ST_SR_CLNT_HELLO_C; |
| 516 else { |
| 517 if (s->s3->tmp.cert_request) |
| 518 { |
| 519 ret=ssl3_get_client_certificate(s); |
| 520 if (ret <= 0) goto end; |
| 521 } |
| 522 s->init_num=0; |
| 523 s->state=SSL3_ST_SR_KEY_EXCH_A; |
| 524 } |
| 525 break; |
| 526 |
| 527 case SSL3_ST_SR_KEY_EXCH_A: |
| 528 case SSL3_ST_SR_KEY_EXCH_B: |
| 529 ret=ssl3_get_client_key_exchange(s); |
| 530 if (ret <= 0) |
| 531 goto end; |
| 532 if (ret == 2) |
| 533 { |
| 534 /* For the ECDH ciphersuites when |
| 535 * the client sends its ECDH pub key in |
| 536 * a certificate, the CertificateVerify |
| 537 * message is not sent. |
| 538 * Also for GOST ciphersuites when |
| 539 * the client uses its key from the certificate |
| 540 * for key exchange. |
| 541 */ |
| 542 s->state=SSL3_ST_SR_FINISHED_A; |
| 543 s->init_num = 0; |
| 544 } |
| 545 else |
| 546 { |
| 547 int offset=0; |
| 548 int dgst_num; |
| 549 |
| 550 s->state=SSL3_ST_SR_CERT_VRFY_A; |
| 551 s->init_num=0; |
| 552 |
| 553 /* We need to get hashes here so if there is |
| 554 * a client cert, it can be verified |
| 555 * FIXME - digest processing for CertificateVeri
fy |
| 556 * should be generalized. But it is next step |
| 557 */ |
| 558 if (s->s3->handshake_buffer) |
| 559 if (!ssl3_digest_cached_records(s)) |
| 560 return -1; |
| 561 for (dgst_num=0; dgst_num<SSL_MAX_DIGEST;dgst_nu
m++) |
| 562 if (s->s3->handshake_dgst[dgst_num]) |
| 563 { |
| 564 int dgst_size; |
| 565 |
| 566 s->method->ssl3_enc->cert_verify
_mac(s,EVP_MD_CTX_type(s->s3->handshake_dgst[dgst_num]),&(s->s3->tmp.cert_verify
_md[offset])); |
| 567 dgst_size=EVP_MD_CTX_size(s->s3-
>handshake_dgst[dgst_num]); |
| 568 if (dgst_size < 0) |
| 569 { |
| 570 ret = -1; |
| 571 goto end; |
| 572 } |
| 573 offset+=dgst_size; |
| 574 } |
| 575 } |
| 576 break; |
| 577 |
| 578 case SSL3_ST_SR_CERT_VRFY_A: |
| 579 case SSL3_ST_SR_CERT_VRFY_B: |
| 580 |
| 581 /* we should decide if we expected this one */ |
| 582 ret=ssl3_get_cert_verify(s); |
| 583 if (ret <= 0) goto end; |
| 584 |
| 585 s->state=SSL3_ST_SR_FINISHED_A; |
| 586 s->init_num=0; |
| 587 break; |
| 588 |
| 589 case SSL3_ST_SR_FINISHED_A: |
| 590 case SSL3_ST_SR_FINISHED_B: |
| 591 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A, |
| 592 SSL3_ST_SR_FINISHED_B); |
| 593 if (ret <= 0) goto end; |
| 594 #ifndef OPENSSL_NO_TLSEXT |
| 595 if (s->tlsext_ticket_expected) |
| 596 s->state=SSL3_ST_SW_SESSION_TICKET_A; |
| 597 else if (s->hit) |
| 598 s->state=SSL_ST_OK; |
| 599 #else |
| 600 if (s->hit) |
| 601 s->state=SSL_ST_OK; |
| 602 #endif |
| 603 else |
| 604 s->state=SSL3_ST_SW_CHANGE_A; |
| 605 s->init_num=0; |
| 606 break; |
| 607 |
| 608 #ifndef OPENSSL_NO_TLSEXT |
| 609 case SSL3_ST_SW_SESSION_TICKET_A: |
| 610 case SSL3_ST_SW_SESSION_TICKET_B: |
| 611 ret=ssl3_send_newsession_ticket(s); |
| 612 if (ret <= 0) goto end; |
| 613 s->state=SSL3_ST_SW_CHANGE_A; |
| 614 s->init_num=0; |
| 615 break; |
| 616 |
| 617 case SSL3_ST_SW_CERT_STATUS_A: |
| 618 case SSL3_ST_SW_CERT_STATUS_B: |
| 619 ret=ssl3_send_cert_status(s); |
| 620 if (ret <= 0) goto end; |
| 621 s->state=SSL3_ST_SW_KEY_EXCH_A; |
| 622 s->init_num=0; |
| 623 break; |
| 624 |
| 625 #endif |
| 626 |
| 627 case SSL3_ST_SW_CHANGE_A: |
| 628 case SSL3_ST_SW_CHANGE_B: |
| 629 |
| 630 s->session->cipher=s->s3->tmp.new_cipher; |
| 631 if (!s->method->ssl3_enc->setup_key_block(s)) |
| 632 { ret= -1; goto end; } |
| 633 |
| 634 ret=ssl3_send_change_cipher_spec(s, |
| 635 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B); |
| 636 |
| 637 if (ret <= 0) goto end; |
| 638 s->state=SSL3_ST_SW_FINISHED_A; |
| 639 s->init_num=0; |
| 640 |
| 641 if (!s->method->ssl3_enc->change_cipher_state(s, |
| 642 SSL3_CHANGE_CIPHER_SERVER_WRITE)) |
| 643 { |
| 644 ret= -1; |
| 645 goto end; |
| 646 } |
| 647 |
| 648 break; |
| 649 |
| 650 case SSL3_ST_SW_FINISHED_A: |
| 651 case SSL3_ST_SW_FINISHED_B: |
| 652 ret=ssl3_send_finished(s, |
| 653 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B, |
| 654 s->method->ssl3_enc->server_finished_label, |
| 655 s->method->ssl3_enc->server_finished_label_len); |
| 656 if (ret <= 0) goto end; |
| 657 s->state=SSL3_ST_SW_FLUSH; |
| 658 if (s->hit) |
| 659 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A; |
| 660 else |
| 661 s->s3->tmp.next_state=SSL_ST_OK; |
| 662 s->init_num=0; |
| 663 break; |
| 664 |
| 665 case SSL_ST_OK: |
| 666 /* clean a few things up */ |
| 667 ssl3_cleanup_key_block(s); |
| 668 |
| 669 BUF_MEM_free(s->init_buf); |
| 670 s->init_buf=NULL; |
| 671 |
| 672 /* remove buffering on output */ |
| 673 ssl_free_wbio_buffer(s); |
| 674 |
| 675 s->init_num=0; |
| 676 |
| 677 if (s->new_session == 2) /* skipped if we just sent a He
lloRequest */ |
| 678 { |
| 679 /* actually not necessarily a 'new' session unle
ss |
| 680 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
is set */ |
| 681 |
| 682 s->new_session=0; |
| 683 |
| 684 ssl_update_cache(s,SSL_SESS_CACHE_SERVER); |
| 685 |
| 686 s->ctx->stats.sess_accept_good++; |
| 687 /* s->server=1; */ |
| 688 s->handshake_func=ssl3_accept; |
| 689 |
| 690 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); |
| 691 } |
| 692 |
| 693 ret = 1; |
| 694 goto end; |
| 695 /* break; */ |
| 696 |
| 697 default: |
| 698 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE); |
| 699 ret= -1; |
| 700 goto end; |
| 701 /* break; */ |
| 702 } |
| 703 |
| 704 if (!s->s3->tmp.reuse_message && !skip) |
| 705 { |
| 706 if (s->debug) |
| 707 { |
| 708 if ((ret=BIO_flush(s->wbio)) <= 0) |
| 709 goto end; |
| 710 } |
| 711 |
| 712 |
| 713 if ((cb != NULL) && (s->state != state)) |
| 714 { |
| 715 new_state=s->state; |
| 716 s->state=state; |
| 717 cb(s,SSL_CB_ACCEPT_LOOP,1); |
| 718 s->state=new_state; |
| 719 } |
| 720 } |
| 721 skip=0; |
| 722 } |
| 723 end: |
| 724 /* BIO_flush(s->wbio); */ |
| 725 |
| 726 s->in_handshake--; |
| 727 if (cb != NULL) |
| 728 cb(s,SSL_CB_ACCEPT_EXIT,ret); |
| 729 return(ret); |
| 730 } |
| 731 |
| 732 int ssl3_send_hello_request(SSL *s) |
| 733 { |
| 734 unsigned char *p; |
| 735 |
| 736 if (s->state == SSL3_ST_SW_HELLO_REQ_A) |
| 737 { |
| 738 p=(unsigned char *)s->init_buf->data; |
| 739 *(p++)=SSL3_MT_HELLO_REQUEST; |
| 740 *(p++)=0; |
| 741 *(p++)=0; |
| 742 *(p++)=0; |
| 743 |
| 744 s->state=SSL3_ST_SW_HELLO_REQ_B; |
| 745 /* number of bytes to write */ |
| 746 s->init_num=4; |
| 747 s->init_off=0; |
| 748 } |
| 749 |
| 750 /* SSL3_ST_SW_HELLO_REQ_B */ |
| 751 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 752 } |
| 753 |
| 754 int ssl3_check_client_hello(SSL *s) |
| 755 { |
| 756 int ok; |
| 757 long n; |
| 758 |
| 759 /* We only allow the client to restart the handshake once per |
| 760 * negotiation. */ |
| 761 if (s->s3->flags & SSL3_FLAGS_SGC_RESTART_DONE) |
| 762 { |
| 763 SSLerr(SSL_F_SSL3_CHECK_CLIENT_HELLO, SSL_R_MULTIPLE_SGC_RESTART
S); |
| 764 return -1; |
| 765 } |
| 766 |
| 767 /* this function is called when we really expect a Certificate message, |
| 768 * so permit appropriate message length */ |
| 769 n=s->method->ssl_get_message(s, |
| 770 SSL3_ST_SR_CERT_A, |
| 771 SSL3_ST_SR_CERT_B, |
| 772 -1, |
| 773 s->max_cert_list, |
| 774 &ok); |
| 775 if (!ok) return((int)n); |
| 776 s->s3->tmp.reuse_message = 1; |
| 777 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO) |
| 778 { |
| 779 /* Throw away what we have done so far in the current handshake, |
| 780 * which will now be aborted. (A full SSL_clear would be too muc
h.) */ |
| 781 #ifndef OPENSSL_NO_DH |
| 782 if (s->s3->tmp.dh != NULL) |
| 783 { |
| 784 DH_free(s->s3->tmp.dh); |
| 785 s->s3->tmp.dh = NULL; |
| 786 } |
| 787 #endif |
| 788 #ifndef OPENSSL_NO_ECDH |
| 789 if (s->s3->tmp.ecdh != NULL) |
| 790 { |
| 791 EC_KEY_free(s->s3->tmp.ecdh); |
| 792 s->s3->tmp.ecdh = NULL; |
| 793 } |
| 794 #endif |
| 795 s->s3->flags |= SSL3_FLAGS_SGC_RESTART_DONE; |
| 796 return 2; |
| 797 } |
| 798 return 1; |
| 799 } |
| 800 |
| 801 int ssl3_get_client_hello(SSL *s) |
| 802 { |
| 803 int i,j,ok,al,ret= -1; |
| 804 unsigned int cookie_len; |
| 805 long n; |
| 806 unsigned long id; |
| 807 unsigned char *p,*d,*q; |
| 808 SSL_CIPHER *c; |
| 809 #ifndef OPENSSL_NO_COMP |
| 810 SSL_COMP *comp=NULL; |
| 811 #endif |
| 812 STACK_OF(SSL_CIPHER) *ciphers=NULL; |
| 813 |
| 814 /* We do this so that we will respond with our native type. |
| 815 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1, |
| 816 * This down switching should be handled by a different method. |
| 817 * If we are SSLv3, we will respond with SSLv3, even if prompted with |
| 818 * TLSv1. |
| 819 */ |
| 820 if (s->state == SSL3_ST_SR_CLNT_HELLO_A) |
| 821 { |
| 822 s->state=SSL3_ST_SR_CLNT_HELLO_B; |
| 823 } |
| 824 s->first_packet=1; |
| 825 n=s->method->ssl_get_message(s, |
| 826 SSL3_ST_SR_CLNT_HELLO_B, |
| 827 SSL3_ST_SR_CLNT_HELLO_C, |
| 828 SSL3_MT_CLIENT_HELLO, |
| 829 SSL3_RT_MAX_PLAIN_LENGTH, |
| 830 &ok); |
| 831 |
| 832 if (!ok) return((int)n); |
| 833 s->first_packet=0; |
| 834 d=p=(unsigned char *)s->init_msg; |
| 835 |
| 836 /* use version from inside client hello, not from record header |
| 837 * (may differ: see RFC 2246, Appendix E, second paragraph) */ |
| 838 s->client_version=(((int)p[0])<<8)|(int)p[1]; |
| 839 p+=2; |
| 840 |
| 841 if ((s->version == DTLS1_VERSION && s->client_version > s->version) || |
| 842 (s->version != DTLS1_VERSION && s->client_version < s->version)) |
| 843 { |
| 844 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER); |
| 845 if ((s->client_version>>8) == SSL3_VERSION_MAJOR) |
| 846 { |
| 847 /* similar to ssl3_get_record, send alert using remote v
ersion number */ |
| 848 s->version = s->client_version; |
| 849 } |
| 850 al = SSL_AD_PROTOCOL_VERSION; |
| 851 goto f_err; |
| 852 } |
| 853 |
| 854 /* If we require cookies and this ClientHello doesn't |
| 855 * contain one, just return since we do not want to |
| 856 * allocate any memory yet. So check cookie length... |
| 857 */ |
| 858 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) |
| 859 { |
| 860 unsigned int session_length, cookie_length; |
| 861 |
| 862 session_length = *(p + SSL3_RANDOM_SIZE); |
| 863 cookie_length = *(p + SSL3_RANDOM_SIZE + session_length + 1); |
| 864 |
| 865 if (cookie_length == 0) |
| 866 return 1; |
| 867 } |
| 868 |
| 869 /* load the client random */ |
| 870 memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE); |
| 871 p+=SSL3_RANDOM_SIZE; |
| 872 |
| 873 /* get the session-id */ |
| 874 j= *(p++); |
| 875 |
| 876 s->hit=0; |
| 877 /* Versions before 0.9.7 always allow session reuse during renegotiation |
| 878 * (i.e. when s->new_session is true), option |
| 879 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7. |
| 880 * Maybe this optional behaviour should always have been the default, |
| 881 * but we cannot safely change the default behaviour (or new application
s |
| 882 * might be written that become totally unsecure when compiled with |
| 883 * an earlier library version) |
| 884 */ |
| 885 if ((s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_REN
EGOTIATION))) |
| 886 { |
| 887 if (!s->session_creation_enabled) |
| 888 { |
| 889 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE
); |
| 890 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_SESSION_MAY_NOT
_BE_CREATED); |
| 891 goto err; |
| 892 } |
| 893 if (!ssl_get_new_session(s,1)) |
| 894 goto err; |
| 895 } |
| 896 else |
| 897 { |
| 898 i=ssl_get_prev_session(s, p, j, d + n); |
| 899 if (i == 1) |
| 900 { /* previous session */ |
| 901 s->hit=1; |
| 902 } |
| 903 else if (i == -1) |
| 904 goto err; |
| 905 else /* i == 0 */ |
| 906 { |
| 907 if (!s->session_creation_enabled) |
| 908 { |
| 909 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE
_FAILURE); |
| 910 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_SESSION
_MAY_NOT_BE_CREATED); |
| 911 goto err; |
| 912 } |
| 913 if (!ssl_get_new_session(s,1)) |
| 914 goto err; |
| 915 } |
| 916 } |
| 917 |
| 918 p+=j; |
| 919 |
| 920 if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) |
| 921 { |
| 922 /* cookie stuff */ |
| 923 cookie_len = *(p++); |
| 924 |
| 925 /* |
| 926 * The ClientHello may contain a cookie even if the |
| 927 * HelloVerify message has not been sent--make sure that it |
| 928 * does not cause an overflow. |
| 929 */ |
| 930 if ( cookie_len > sizeof(s->d1->rcvd_cookie)) |
| 931 { |
| 932 /* too much data */ |
| 933 al = SSL_AD_DECODE_ERROR; |
| 934 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATC
H); |
| 935 goto f_err; |
| 936 } |
| 937 |
| 938 /* verify the cookie if appropriate option is set. */ |
| 939 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && |
| 940 cookie_len > 0) |
| 941 { |
| 942 memcpy(s->d1->rcvd_cookie, p, cookie_len); |
| 943 |
| 944 if ( s->ctx->app_verify_cookie_cb != NULL) |
| 945 { |
| 946 if ( s->ctx->app_verify_cookie_cb(s, s->d1->rcvd
_cookie, |
| 947 cookie_len) == 0) |
| 948 { |
| 949 al=SSL_AD_HANDSHAKE_FAILURE; |
| 950 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, |
| 951 SSL_R_COOKIE_MISMATCH); |
| 952 goto f_err; |
| 953 } |
| 954 /* else cookie verification succeeded */ |
| 955 } |
| 956 else if ( memcmp(s->d1->rcvd_cookie, s->d1->cookie, |
| 957 s->d1->cookie_len) != 0) /* de
fault verification */ |
| 958 { |
| 959 al=SSL_AD_HANDSHAKE_FAILURE; |
| 960 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, |
| 961 SSL_R_COOKIE_MISMATCH); |
| 962 goto f_err; |
| 963 } |
| 964 |
| 965 ret = 2; |
| 966 } |
| 967 |
| 968 p += cookie_len; |
| 969 } |
| 970 |
| 971 n2s(p,i); |
| 972 if ((i == 0) && (j != 0)) |
| 973 { |
| 974 /* we need a cipher if we are not resuming a session */ |
| 975 al=SSL_AD_ILLEGAL_PARAMETER; |
| 976 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED); |
| 977 goto f_err; |
| 978 } |
| 979 if ((p+i) >= (d+n)) |
| 980 { |
| 981 /* not enough data */ |
| 982 al=SSL_AD_DECODE_ERROR; |
| 983 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH); |
| 984 goto f_err; |
| 985 } |
| 986 if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers)) |
| 987 == NULL)) |
| 988 { |
| 989 goto err; |
| 990 } |
| 991 p+=i; |
| 992 |
| 993 /* If it is a hit, check that the cipher is in the list */ |
| 994 if ((s->hit) && (i > 0)) |
| 995 { |
| 996 j=0; |
| 997 id=s->session->cipher->id; |
| 998 |
| 999 #ifdef CIPHER_DEBUG |
| 1000 printf("client sent %d ciphers\n",sk_num(ciphers)); |
| 1001 #endif |
| 1002 for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++) |
| 1003 { |
| 1004 c=sk_SSL_CIPHER_value(ciphers,i); |
| 1005 #ifdef CIPHER_DEBUG |
| 1006 printf("client [%2d of %2d]:%s\n", |
| 1007 i,sk_num(ciphers),SSL_CIPHER_get_name(c)); |
| 1008 #endif |
| 1009 if (c->id == id) |
| 1010 { |
| 1011 j=1; |
| 1012 break; |
| 1013 } |
| 1014 } |
| 1015 /* Disabled because it can be used in a ciphersuite downgrade |
| 1016 * attack: CVE-2010-4180. |
| 1017 */ |
| 1018 #if 0 |
| 1019 if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_
BUG) && (sk_SSL_CIPHER_num(ciphers) == 1)) |
| 1020 { |
| 1021 /* Special case as client bug workaround: the previously
used cipher may |
| 1022 * not be in the current list, the client instead might
be trying to |
| 1023 * continue using a cipher that before wasn't chosen due
to server |
| 1024 * preferences. We'll have to reject the connection if
the cipher is not |
| 1025 * enabled, though. */ |
| 1026 c = sk_SSL_CIPHER_value(ciphers, 0); |
| 1027 if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0) |
| 1028 { |
| 1029 s->session->cipher = c; |
| 1030 j = 1; |
| 1031 } |
| 1032 } |
| 1033 #endif |
| 1034 if (j == 0) |
| 1035 { |
| 1036 /* we need to have the cipher in the cipher |
| 1037 * list if we are asked to reuse it */ |
| 1038 al=SSL_AD_ILLEGAL_PARAMETER; |
| 1039 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER
_MISSING); |
| 1040 goto f_err; |
| 1041 } |
| 1042 } |
| 1043 |
| 1044 /* compression */ |
| 1045 i= *(p++); |
| 1046 if ((p+i) > (d+n)) |
| 1047 { |
| 1048 /* not enough data */ |
| 1049 al=SSL_AD_DECODE_ERROR; |
| 1050 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH); |
| 1051 goto f_err; |
| 1052 } |
| 1053 q=p; |
| 1054 for (j=0; j<i; j++) |
| 1055 { |
| 1056 if (p[j] == 0) break; |
| 1057 } |
| 1058 |
| 1059 p+=i; |
| 1060 if (j >= i) |
| 1061 { |
| 1062 /* no compress */ |
| 1063 al=SSL_AD_DECODE_ERROR; |
| 1064 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIE
D); |
| 1065 goto f_err; |
| 1066 } |
| 1067 |
| 1068 #ifndef OPENSSL_NO_TLSEXT |
| 1069 /* TLS extensions*/ |
| 1070 if (s->version >= SSL3_VERSION) |
| 1071 { |
| 1072 if (!ssl_parse_clienthello_tlsext(s,&p,d,n, &al)) |
| 1073 { |
| 1074 /* 'al' set by ssl_parse_clienthello_tlsext */ |
| 1075 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_PARSE_TLSEXT); |
| 1076 goto f_err; |
| 1077 } |
| 1078 } |
| 1079 if (ssl_check_clienthello_tlsext(s) <= 0) { |
| 1080 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLS
EXT); |
| 1081 goto err; |
| 1082 } |
| 1083 |
| 1084 /* Check if we want to use external pre-shared secret for this |
| 1085 * handshake for not reused session only. We need to generate |
| 1086 * server_random before calling tls_session_secret_cb in order to allow |
| 1087 * SessionTicket processing to use it in key derivation. */ |
| 1088 { |
| 1089 unsigned long Time; |
| 1090 unsigned char *pos; |
| 1091 Time=(unsigned long)time(NULL); /* Time */ |
| 1092 pos=s->s3->server_random; |
| 1093 l2n(Time,pos); |
| 1094 if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0) |
| 1095 { |
| 1096 al=SSL_AD_INTERNAL_ERROR; |
| 1097 goto f_err; |
| 1098 } |
| 1099 } |
| 1100 |
| 1101 if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) |
| 1102 { |
| 1103 SSL_CIPHER *pref_cipher=NULL; |
| 1104 |
| 1105 s->session->master_key_length=sizeof(s->session->master_key); |
| 1106 if(s->tls_session_secret_cb(s, s->session->master_key, &s->sessi
on->master_key_length, |
| 1107 ciphers, &pref_cipher, s->tls_session_secret_cb_arg)) |
| 1108 { |
| 1109 s->hit=1; |
| 1110 s->session->ciphers=ciphers; |
| 1111 s->session->verify_result=X509_V_OK; |
| 1112 |
| 1113 ciphers=NULL; |
| 1114 |
| 1115 /* check if some cipher was preferred by call back */ |
| 1116 pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_ciph
er(s, s->session->ciphers, SSL_get_ciphers(s)); |
| 1117 if (pref_cipher == NULL) |
| 1118 { |
| 1119 al=SSL_AD_HANDSHAKE_FAILURE; |
| 1120 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHAR
ED_CIPHER); |
| 1121 goto f_err; |
| 1122 } |
| 1123 |
| 1124 s->session->cipher=pref_cipher; |
| 1125 |
| 1126 if (s->cipher_list) |
| 1127 sk_SSL_CIPHER_free(s->cipher_list); |
| 1128 |
| 1129 if (s->cipher_list_by_id) |
| 1130 sk_SSL_CIPHER_free(s->cipher_list_by_id); |
| 1131 |
| 1132 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers); |
| 1133 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->cip
hers); |
| 1134 } |
| 1135 } |
| 1136 #endif |
| 1137 |
| 1138 /* Worst case, we will use the NULL compression, but if we have other |
| 1139 * options, we will now look for them. We have i-1 compression |
| 1140 * algorithms from the client, starting at q. */ |
| 1141 s->s3->tmp.new_compression=NULL; |
| 1142 #ifndef OPENSSL_NO_COMP |
| 1143 /* This only happens if we have a cache hit */ |
| 1144 if (s->session->compress_meth != 0) |
| 1145 { |
| 1146 int m, comp_id = s->session->compress_meth; |
| 1147 /* Perform sanity checks on resumed compression algorithm */ |
| 1148 /* Can't disable compression */ |
| 1149 if (s->options & SSL_OP_NO_COMPRESSION) |
| 1150 { |
| 1151 al=SSL_AD_INTERNAL_ERROR; |
| 1152 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INCONSISTENT_CO
MPRESSION); |
| 1153 goto f_err; |
| 1154 } |
| 1155 /* Look for resumed compression method */ |
| 1156 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) |
| 1157 { |
| 1158 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m); |
| 1159 if (comp_id == comp->id) |
| 1160 { |
| 1161 s->s3->tmp.new_compression=comp; |
| 1162 break; |
| 1163 } |
| 1164 } |
| 1165 if (s->s3->tmp.new_compression == NULL) |
| 1166 { |
| 1167 al=SSL_AD_INTERNAL_ERROR; |
| 1168 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INVALID_COMPRES
SION_ALGORITHM); |
| 1169 goto f_err; |
| 1170 } |
| 1171 /* Look for resumed method in compression list */ |
| 1172 for (m = 0; m < i; m++) |
| 1173 { |
| 1174 if (q[m] == comp_id) |
| 1175 break; |
| 1176 } |
| 1177 if (m >= i) |
| 1178 { |
| 1179 al=SSL_AD_ILLEGAL_PARAMETER; |
| 1180 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_COMPRE
SSSION_ALGORITHM_MISSING); |
| 1181 goto f_err; |
| 1182 } |
| 1183 } |
| 1184 else if (s->hit) |
| 1185 comp = NULL; |
| 1186 else if (!(s->options & SSL_OP_NO_COMPRESSION) && s->ctx->comp_methods) |
| 1187 { /* See if we have a match */ |
| 1188 int m,nn,o,v,done=0; |
| 1189 |
| 1190 nn=sk_SSL_COMP_num(s->ctx->comp_methods); |
| 1191 for (m=0; m<nn; m++) |
| 1192 { |
| 1193 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m); |
| 1194 v=comp->id; |
| 1195 for (o=0; o<i; o++) |
| 1196 { |
| 1197 if (v == q[o]) |
| 1198 { |
| 1199 done=1; |
| 1200 break; |
| 1201 } |
| 1202 } |
| 1203 if (done) break; |
| 1204 } |
| 1205 if (done) |
| 1206 s->s3->tmp.new_compression=comp; |
| 1207 else |
| 1208 comp=NULL; |
| 1209 } |
| 1210 #else |
| 1211 /* If compression is disabled we'd better not try to resume a session |
| 1212 * using compression. |
| 1213 */ |
| 1214 if (s->session->compress_meth != 0) |
| 1215 { |
| 1216 al=SSL_AD_INTERNAL_ERROR; |
| 1217 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INCONSISTENT_COMPRESSIO
N); |
| 1218 goto f_err; |
| 1219 } |
| 1220 #endif |
| 1221 |
| 1222 /* Given s->session->ciphers and SSL_get_ciphers, we must |
| 1223 * pick a cipher */ |
| 1224 |
| 1225 if (!s->hit) |
| 1226 { |
| 1227 #ifdef OPENSSL_NO_COMP |
| 1228 s->session->compress_meth=0; |
| 1229 #else |
| 1230 s->session->compress_meth=(comp == NULL)?0:comp->id; |
| 1231 #endif |
| 1232 if (s->session->ciphers != NULL) |
| 1233 sk_SSL_CIPHER_free(s->session->ciphers); |
| 1234 s->session->ciphers=ciphers; |
| 1235 if (ciphers == NULL) |
| 1236 { |
| 1237 al=SSL_AD_ILLEGAL_PARAMETER; |
| 1238 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASS
ED); |
| 1239 goto f_err; |
| 1240 } |
| 1241 ciphers=NULL; |
| 1242 c=ssl3_choose_cipher(s,s->session->ciphers, |
| 1243 SSL_get_ciphers(s)); |
| 1244 |
| 1245 if (c == NULL) |
| 1246 { |
| 1247 al=SSL_AD_HANDSHAKE_FAILURE; |
| 1248 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHE
R); |
| 1249 goto f_err; |
| 1250 } |
| 1251 s->s3->tmp.new_cipher=c; |
| 1252 } |
| 1253 else |
| 1254 { |
| 1255 /* Session-id reuse */ |
| 1256 #ifdef REUSE_CIPHER_BUG |
| 1257 STACK_OF(SSL_CIPHER) *sk; |
| 1258 SSL_CIPHER *nc=NULL; |
| 1259 SSL_CIPHER *ec=NULL; |
| 1260 |
| 1261 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG) |
| 1262 { |
| 1263 sk=s->session->ciphers; |
| 1264 for (i=0; i<sk_SSL_CIPHER_num(sk); i++) |
| 1265 { |
| 1266 c=sk_SSL_CIPHER_value(sk,i); |
| 1267 if (c->algorithm_enc & SSL_eNULL) |
| 1268 nc=c; |
| 1269 if (SSL_C_IS_EXPORT(c)) |
| 1270 ec=c; |
| 1271 } |
| 1272 if (nc != NULL) |
| 1273 s->s3->tmp.new_cipher=nc; |
| 1274 else if (ec != NULL) |
| 1275 s->s3->tmp.new_cipher=ec; |
| 1276 else |
| 1277 s->s3->tmp.new_cipher=s->session->cipher; |
| 1278 } |
| 1279 else |
| 1280 #endif |
| 1281 s->s3->tmp.new_cipher=s->session->cipher; |
| 1282 } |
| 1283 |
| 1284 if (!ssl3_digest_cached_records(s)) |
| 1285 goto f_err; |
| 1286 |
| 1287 /* we now have the following setup. |
| 1288 * client_random |
| 1289 * cipher_list - our prefered list of ciphers |
| 1290 * ciphers - the clients prefered list of ciphers |
| 1291 * compression - basically ignored right now |
| 1292 * ssl version is set - sslv3 |
| 1293 * s->session - The ssl session has been setup. |
| 1294 * s->hit - session reuse flag |
| 1295 * s->tmp.new_cipher - the new cipher to use. |
| 1296 */ |
| 1297 |
| 1298 if (ret < 0) ret=1; |
| 1299 if (0) |
| 1300 { |
| 1301 f_err: |
| 1302 ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1303 } |
| 1304 err: |
| 1305 if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers); |
| 1306 return(ret); |
| 1307 } |
| 1308 |
| 1309 int ssl3_send_server_hello(SSL *s) |
| 1310 { |
| 1311 unsigned char *buf; |
| 1312 unsigned char *p,*d; |
| 1313 int i,sl; |
| 1314 unsigned long l; |
| 1315 #ifdef OPENSSL_NO_TLSEXT |
| 1316 unsigned long Time; |
| 1317 #endif |
| 1318 |
| 1319 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) |
| 1320 { |
| 1321 buf=(unsigned char *)s->init_buf->data; |
| 1322 #ifdef OPENSSL_NO_TLSEXT |
| 1323 p=s->s3->server_random; |
| 1324 /* Generate server_random if it was not needed previously */ |
| 1325 Time=(unsigned long)time(NULL); /* Time */ |
| 1326 l2n(Time,p); |
| 1327 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0) |
| 1328 return -1; |
| 1329 #endif |
| 1330 /* Do the message type and length last */ |
| 1331 d=p= &(buf[4]); |
| 1332 |
| 1333 *(p++)=s->version>>8; |
| 1334 *(p++)=s->version&0xff; |
| 1335 |
| 1336 /* Random stuff */ |
| 1337 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); |
| 1338 p+=SSL3_RANDOM_SIZE; |
| 1339 |
| 1340 /* now in theory we have 3 options to sending back the |
| 1341 * session id. If it is a re-use, we send back the |
| 1342 * old session-id, if it is a new session, we send |
| 1343 * back the new session-id or we send back a 0 length |
| 1344 * session-id if we want it to be single use. |
| 1345 * Currently I will not implement the '0' length session-id |
| 1346 * 12-Jan-98 - I'll now support the '0' length stuff. |
| 1347 * |
| 1348 * We also have an additional case where stateless session |
| 1349 * resumption is successful: we always send back the old |
| 1350 * session id. In this case s->hit is non zero: this can |
| 1351 * only happen if stateless session resumption is succesful |
| 1352 * if session caching is disabled so existing functionality |
| 1353 * is unaffected. |
| 1354 */ |
| 1355 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) |
| 1356 && !s->hit) |
| 1357 s->session->session_id_length=0; |
| 1358 |
| 1359 sl=s->session->session_id_length; |
| 1360 if (sl > (int)sizeof(s->session->session_id)) |
| 1361 { |
| 1362 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERRO
R); |
| 1363 return -1; |
| 1364 } |
| 1365 *(p++)=sl; |
| 1366 memcpy(p,s->session->session_id,sl); |
| 1367 p+=sl; |
| 1368 |
| 1369 /* put the cipher */ |
| 1370 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p); |
| 1371 p+=i; |
| 1372 |
| 1373 /* put the compression method */ |
| 1374 #ifdef OPENSSL_NO_COMP |
| 1375 *(p++)=0; |
| 1376 #else |
| 1377 if (s->s3->tmp.new_compression == NULL) |
| 1378 *(p++)=0; |
| 1379 else |
| 1380 *(p++)=s->s3->tmp.new_compression->id; |
| 1381 #endif |
| 1382 #ifndef OPENSSL_NO_TLSEXT |
| 1383 if (ssl_prepare_serverhello_tlsext(s) <= 0) |
| 1384 { |
| 1385 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,SSL_R_SERVERHELLO_TL
SEXT); |
| 1386 return -1; |
| 1387 } |
| 1388 if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_
LENGTH)) == NULL) |
| 1389 { |
| 1390 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,ERR_R_INTERNAL_ERROR
); |
| 1391 return -1; |
| 1392 } |
| 1393 #endif |
| 1394 /* do the header */ |
| 1395 l=(p-d); |
| 1396 d=buf; |
| 1397 *(d++)=SSL3_MT_SERVER_HELLO; |
| 1398 l2n3(l,d); |
| 1399 |
| 1400 s->state=SSL3_ST_SW_SRVR_HELLO_B; |
| 1401 /* number of bytes to write */ |
| 1402 s->init_num=p-buf; |
| 1403 s->init_off=0; |
| 1404 } |
| 1405 |
| 1406 /* SSL3_ST_SW_SRVR_HELLO_B */ |
| 1407 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 1408 } |
| 1409 |
| 1410 int ssl3_send_server_done(SSL *s) |
| 1411 { |
| 1412 unsigned char *p; |
| 1413 |
| 1414 if (s->state == SSL3_ST_SW_SRVR_DONE_A) |
| 1415 { |
| 1416 p=(unsigned char *)s->init_buf->data; |
| 1417 |
| 1418 /* do the header */ |
| 1419 *(p++)=SSL3_MT_SERVER_DONE; |
| 1420 *(p++)=0; |
| 1421 *(p++)=0; |
| 1422 *(p++)=0; |
| 1423 |
| 1424 s->state=SSL3_ST_SW_SRVR_DONE_B; |
| 1425 /* number of bytes to write */ |
| 1426 s->init_num=4; |
| 1427 s->init_off=0; |
| 1428 } |
| 1429 |
| 1430 /* SSL3_ST_SW_SRVR_DONE_B */ |
| 1431 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 1432 } |
| 1433 |
| 1434 int ssl3_send_server_key_exchange(SSL *s) |
| 1435 { |
| 1436 #ifndef OPENSSL_NO_RSA |
| 1437 unsigned char *q; |
| 1438 int j,num; |
| 1439 RSA *rsa; |
| 1440 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; |
| 1441 unsigned int u; |
| 1442 #endif |
| 1443 #ifndef OPENSSL_NO_DH |
| 1444 DH *dh=NULL,*dhp; |
| 1445 #endif |
| 1446 #ifndef OPENSSL_NO_ECDH |
| 1447 EC_KEY *ecdh=NULL, *ecdhp; |
| 1448 unsigned char *encodedPoint = NULL; |
| 1449 int encodedlen = 0; |
| 1450 int curve_id = 0; |
| 1451 BN_CTX *bn_ctx = NULL; |
| 1452 #endif |
| 1453 EVP_PKEY *pkey; |
| 1454 unsigned char *p,*d; |
| 1455 int al,i; |
| 1456 unsigned long type; |
| 1457 int n; |
| 1458 CERT *cert; |
| 1459 BIGNUM *r[4]; |
| 1460 int nr[4],kn; |
| 1461 BUF_MEM *buf; |
| 1462 EVP_MD_CTX md_ctx; |
| 1463 |
| 1464 EVP_MD_CTX_init(&md_ctx); |
| 1465 if (s->state == SSL3_ST_SW_KEY_EXCH_A) |
| 1466 { |
| 1467 type=s->s3->tmp.new_cipher->algorithm_mkey; |
| 1468 cert=s->cert; |
| 1469 |
| 1470 buf=s->init_buf; |
| 1471 |
| 1472 r[0]=r[1]=r[2]=r[3]=NULL; |
| 1473 n=0; |
| 1474 #ifndef OPENSSL_NO_RSA |
| 1475 if (type & SSL_kRSA) |
| 1476 { |
| 1477 rsa=cert->rsa_tmp; |
| 1478 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) |
| 1479 { |
| 1480 rsa=s->cert->rsa_tmp_cb(s, |
| 1481 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), |
| 1482 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cip
her)); |
| 1483 if(rsa == NULL) |
| 1484 { |
| 1485 al=SSL_AD_HANDSHAKE_FAILURE; |
| 1486 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHAN
GE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY); |
| 1487 goto f_err; |
| 1488 } |
| 1489 RSA_up_ref(rsa); |
| 1490 cert->rsa_tmp=rsa; |
| 1491 } |
| 1492 if (rsa == NULL) |
| 1493 { |
| 1494 al=SSL_AD_HANDSHAKE_FAILURE; |
| 1495 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R
_MISSING_TMP_RSA_KEY); |
| 1496 goto f_err; |
| 1497 } |
| 1498 r[0]=rsa->n; |
| 1499 r[1]=rsa->e; |
| 1500 s->s3->tmp.use_rsa_tmp=1; |
| 1501 } |
| 1502 else |
| 1503 #endif |
| 1504 #ifndef OPENSSL_NO_DH |
| 1505 if (type & SSL_kEDH) |
| 1506 { |
| 1507 dhp=cert->dh_tmp; |
| 1508 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL)) |
| 1509 dhp=s->cert->dh_tmp_cb(s, |
| 1510 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), |
| 1511 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cip
her)); |
| 1512 if (dhp == NULL) |
| 1513 { |
| 1514 al=SSL_AD_HANDSHAKE_FAILURE; |
| 1515 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R
_MISSING_TMP_DH_KEY); |
| 1516 goto f_err; |
| 1517 } |
| 1518 |
| 1519 if (s->s3->tmp.dh != NULL) |
| 1520 { |
| 1521 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_
R_INTERNAL_ERROR); |
| 1522 goto err; |
| 1523 } |
| 1524 |
| 1525 if ((dh=DHparams_dup(dhp)) == NULL) |
| 1526 { |
| 1527 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R
_DH_LIB); |
| 1528 goto err; |
| 1529 } |
| 1530 |
| 1531 s->s3->tmp.dh=dh; |
| 1532 if ((dhp->pub_key == NULL || |
| 1533 dhp->priv_key == NULL || |
| 1534 (s->options & SSL_OP_SINGLE_DH_USE))) |
| 1535 { |
| 1536 if(!DH_generate_key(dh)) |
| 1537 { |
| 1538 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, |
| 1539 ERR_R_DH_LIB); |
| 1540 goto err; |
| 1541 } |
| 1542 } |
| 1543 else |
| 1544 { |
| 1545 dh->pub_key=BN_dup(dhp->pub_key); |
| 1546 dh->priv_key=BN_dup(dhp->priv_key); |
| 1547 if ((dh->pub_key == NULL) || |
| 1548 (dh->priv_key == NULL)) |
| 1549 { |
| 1550 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHAN
GE,ERR_R_DH_LIB); |
| 1551 goto err; |
| 1552 } |
| 1553 } |
| 1554 r[0]=dh->p; |
| 1555 r[1]=dh->g; |
| 1556 r[2]=dh->pub_key; |
| 1557 } |
| 1558 else |
| 1559 #endif |
| 1560 #ifndef OPENSSL_NO_ECDH |
| 1561 if (type & SSL_kEECDH) |
| 1562 { |
| 1563 const EC_GROUP *group; |
| 1564 |
| 1565 ecdhp=cert->ecdh_tmp; |
| 1566 if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL)) |
| 1567 { |
| 1568 ecdhp=s->cert->ecdh_tmp_cb(s, |
| 1569 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), |
| 1570 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cip
her)); |
| 1571 } |
| 1572 if (ecdhp == NULL) |
| 1573 { |
| 1574 al=SSL_AD_HANDSHAKE_FAILURE; |
| 1575 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R
_MISSING_TMP_ECDH_KEY); |
| 1576 goto f_err; |
| 1577 } |
| 1578 |
| 1579 if (s->s3->tmp.ecdh != NULL) |
| 1580 { |
| 1581 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_
R_INTERNAL_ERROR); |
| 1582 goto err; |
| 1583 } |
| 1584 |
| 1585 /* Duplicate the ECDH structure. */ |
| 1586 if (ecdhp == NULL) |
| 1587 { |
| 1588 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R
_ECDH_LIB); |
| 1589 goto err; |
| 1590 } |
| 1591 if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) |
| 1592 { |
| 1593 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R
_ECDH_LIB); |
| 1594 goto err; |
| 1595 } |
| 1596 |
| 1597 s->s3->tmp.ecdh=ecdh; |
| 1598 if ((EC_KEY_get0_public_key(ecdh) == NULL) || |
| 1599 (EC_KEY_get0_private_key(ecdh) == NULL) || |
| 1600 (s->options & SSL_OP_SINGLE_ECDH_USE)) |
| 1601 { |
| 1602 if(!EC_KEY_generate_key(ecdh)) |
| 1603 { |
| 1604 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,E
RR_R_ECDH_LIB); |
| 1605 goto err; |
| 1606 } |
| 1607 } |
| 1608 |
| 1609 if (((group = EC_KEY_get0_group(ecdh)) == NULL) || |
| 1610 (EC_KEY_get0_public_key(ecdh) == NULL) || |
| 1611 (EC_KEY_get0_private_key(ecdh) == NULL)) |
| 1612 { |
| 1613 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R
_ECDH_LIB); |
| 1614 goto err; |
| 1615 } |
| 1616 |
| 1617 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && |
| 1618 (EC_GROUP_get_degree(group) > 163)) |
| 1619 { |
| 1620 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R
_ECGROUP_TOO_LARGE_FOR_CIPHER); |
| 1621 goto err; |
| 1622 } |
| 1623 |
| 1624 /* XXX: For now, we only support ephemeral ECDH |
| 1625 * keys over named (not generic) curves. For |
| 1626 * supported named curves, curve_id is non-zero. |
| 1627 */ |
| 1628 if ((curve_id = |
| 1629 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group))
) |
| 1630 == 0) |
| 1631 { |
| 1632 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R
_UNSUPPORTED_ELLIPTIC_CURVE); |
| 1633 goto err; |
| 1634 } |
| 1635 |
| 1636 /* Encode the public key. |
| 1637 * First check the size of encoding and |
| 1638 * allocate memory accordingly. |
| 1639 */ |
| 1640 encodedlen = EC_POINT_point2oct(group, |
| 1641 EC_KEY_get0_public_key(ecdh), |
| 1642 POINT_CONVERSION_UNCOMPRESSED, |
| 1643 NULL, 0, NULL); |
| 1644 |
| 1645 encodedPoint = (unsigned char *) |
| 1646 OPENSSL_malloc(encodedlen*sizeof(unsigned char)); |
| 1647 bn_ctx = BN_CTX_new(); |
| 1648 if ((encodedPoint == NULL) || (bn_ctx == NULL)) |
| 1649 { |
| 1650 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R
_MALLOC_FAILURE); |
| 1651 goto err; |
| 1652 } |
| 1653 |
| 1654 |
| 1655 encodedlen = EC_POINT_point2oct(group, |
| 1656 EC_KEY_get0_public_key(ecdh), |
| 1657 POINT_CONVERSION_UNCOMPRESSED, |
| 1658 encodedPoint, encodedlen, bn_ctx); |
| 1659 |
| 1660 if (encodedlen == 0) |
| 1661 { |
| 1662 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R
_ECDH_LIB); |
| 1663 goto err; |
| 1664 } |
| 1665 |
| 1666 BN_CTX_free(bn_ctx); bn_ctx=NULL; |
| 1667 |
| 1668 /* XXX: For now, we only support named (not |
| 1669 * generic) curves in ECDH ephemeral key exchanges. |
| 1670 * In this situation, we need four additional bytes |
| 1671 * to encode the entire ServerECDHParams |
| 1672 * structure. |
| 1673 */ |
| 1674 n = 4 + encodedlen; |
| 1675 |
| 1676 /* We'll generate the serverKeyExchange message |
| 1677 * explicitly so we can set these to NULLs |
| 1678 */ |
| 1679 r[0]=NULL; |
| 1680 r[1]=NULL; |
| 1681 r[2]=NULL; |
| 1682 r[3]=NULL; |
| 1683 } |
| 1684 else |
| 1685 #endif /* !OPENSSL_NO_ECDH */ |
| 1686 #ifndef OPENSSL_NO_PSK |
| 1687 if (type & SSL_kPSK) |
| 1688 { |
| 1689 /* reserve size for record length and PSK identi
ty hint*/ |
| 1690 n+=2+strlen(s->ctx->psk_identity_hint); |
| 1691 } |
| 1692 else |
| 1693 #endif /* !OPENSSL_NO_PSK */ |
| 1694 { |
| 1695 al=SSL_AD_HANDSHAKE_FAILURE; |
| 1696 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN
_KEY_EXCHANGE_TYPE); |
| 1697 goto f_err; |
| 1698 } |
| 1699 for (i=0; r[i] != NULL; i++) |
| 1700 { |
| 1701 nr[i]=BN_num_bytes(r[i]); |
| 1702 n+=2+nr[i]; |
| 1703 } |
| 1704 |
| 1705 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) |
| 1706 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) |
| 1707 { |
| 1708 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher)) |
| 1709 == NULL) |
| 1710 { |
| 1711 al=SSL_AD_DECODE_ERROR; |
| 1712 goto f_err; |
| 1713 } |
| 1714 kn=EVP_PKEY_size(pkey); |
| 1715 } |
| 1716 else |
| 1717 { |
| 1718 pkey=NULL; |
| 1719 kn=0; |
| 1720 } |
| 1721 |
| 1722 if (!BUF_MEM_grow_clean(buf,n+4+kn)) |
| 1723 { |
| 1724 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF); |
| 1725 goto err; |
| 1726 } |
| 1727 d=(unsigned char *)s->init_buf->data; |
| 1728 p= &(d[4]); |
| 1729 |
| 1730 for (i=0; r[i] != NULL; i++) |
| 1731 { |
| 1732 s2n(nr[i],p); |
| 1733 BN_bn2bin(r[i],p); |
| 1734 p+=nr[i]; |
| 1735 } |
| 1736 |
| 1737 #ifndef OPENSSL_NO_ECDH |
| 1738 if (type & SSL_kEECDH) |
| 1739 { |
| 1740 /* XXX: For now, we only support named (not generic) cur
ves. |
| 1741 * In this situation, the serverKeyExchange message has: |
| 1742 * [1 byte CurveType], [2 byte CurveName] |
| 1743 * [1 byte length of encoded point], followed by |
| 1744 * the actual encoded point itself |
| 1745 */ |
| 1746 *p = NAMED_CURVE_TYPE; |
| 1747 p += 1; |
| 1748 *p = 0; |
| 1749 p += 1; |
| 1750 *p = curve_id; |
| 1751 p += 1; |
| 1752 *p = encodedlen; |
| 1753 p += 1; |
| 1754 memcpy((unsigned char*)p, |
| 1755 (unsigned char *)encodedPoint, |
| 1756 encodedlen); |
| 1757 OPENSSL_free(encodedPoint); |
| 1758 encodedPoint = NULL; |
| 1759 p += encodedlen; |
| 1760 } |
| 1761 #endif |
| 1762 |
| 1763 #ifndef OPENSSL_NO_PSK |
| 1764 if (type & SSL_kPSK) |
| 1765 { |
| 1766 /* copy PSK identity hint */ |
| 1767 s2n(strlen(s->ctx->psk_identity_hint), p); |
| 1768 strncpy((char *)p, s->ctx->psk_identity_hint, strlen(s->
ctx->psk_identity_hint)); |
| 1769 p+=strlen(s->ctx->psk_identity_hint); |
| 1770 } |
| 1771 #endif |
| 1772 |
| 1773 /* not anonymous */ |
| 1774 if (pkey != NULL) |
| 1775 { |
| 1776 /* n is the length of the params, they start at &(d[4]) |
| 1777 * and p points to the space at the end. */ |
| 1778 #ifndef OPENSSL_NO_RSA |
| 1779 if (pkey->type == EVP_PKEY_RSA) |
| 1780 { |
| 1781 q=md_buf; |
| 1782 j=0; |
| 1783 for (num=2; num > 0; num--) |
| 1784 { |
| 1785 EVP_DigestInit_ex(&md_ctx,(num == 2) |
| 1786 ?s->ctx->md5:s->ctx->sha1, NULL)
; |
| 1787 EVP_DigestUpdate(&md_ctx,&(s->s3->client
_random[0]),SSL3_RANDOM_SIZE); |
| 1788 EVP_DigestUpdate(&md_ctx,&(s->s3->server
_random[0]),SSL3_RANDOM_SIZE); |
| 1789 EVP_DigestUpdate(&md_ctx,&(d[4]),n); |
| 1790 EVP_DigestFinal_ex(&md_ctx,q, |
| 1791 (unsigned int *)&i); |
| 1792 q+=i; |
| 1793 j+=i; |
| 1794 } |
| 1795 if (RSA_sign(NID_md5_sha1, md_buf, j, |
| 1796 &(p[2]), &u, pkey->pkey.rsa) <= 0) |
| 1797 { |
| 1798 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHAN
GE,ERR_LIB_RSA); |
| 1799 goto err; |
| 1800 } |
| 1801 s2n(u,p); |
| 1802 n+=u+2; |
| 1803 } |
| 1804 else |
| 1805 #endif |
| 1806 #if !defined(OPENSSL_NO_DSA) |
| 1807 if (pkey->type == EVP_PKEY_DSA) |
| 1808 { |
| 1809 /* lets do DSS */ |
| 1810 EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL); |
| 1811 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]
),SSL3_RANDOM_SIZE); |
| 1812 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]
),SSL3_RANDOM_SIZE); |
| 1813 EVP_SignUpdate(&md_ctx,&(d[4]),n); |
| 1814 if (!EVP_SignFinal(&md_ctx,&(p[2]), |
| 1815 (unsigned int *)&i,pkey)) |
| 1816 { |
| 1817 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHAN
GE,ERR_LIB_DSA); |
| 1818 goto err; |
| 1819 } |
| 1820 s2n(i,p); |
| 1821 n+=i+2; |
| 1822 } |
| 1823 else |
| 1824 #endif |
| 1825 #if !defined(OPENSSL_NO_ECDSA) |
| 1826 if (pkey->type == EVP_PKEY_EC) |
| 1827 { |
| 1828 /* let's do ECDSA */ |
| 1829 EVP_SignInit_ex(&md_ctx,EVP_ecdsa(), NULL); |
| 1830 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]
),SSL3_RANDOM_SIZE); |
| 1831 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]
),SSL3_RANDOM_SIZE); |
| 1832 EVP_SignUpdate(&md_ctx,&(d[4]),n); |
| 1833 if (!EVP_SignFinal(&md_ctx,&(p[2]), |
| 1834 (unsigned int *)&i,pkey)) |
| 1835 { |
| 1836 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHAN
GE,ERR_LIB_ECDSA); |
| 1837 goto err; |
| 1838 } |
| 1839 s2n(i,p); |
| 1840 n+=i+2; |
| 1841 } |
| 1842 else |
| 1843 #endif |
| 1844 { |
| 1845 /* Is this error check actually needed? */ |
| 1846 al=SSL_AD_HANDSHAKE_FAILURE; |
| 1847 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R
_UNKNOWN_PKEY_TYPE); |
| 1848 goto f_err; |
| 1849 } |
| 1850 } |
| 1851 |
| 1852 *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE; |
| 1853 l2n3(n,d); |
| 1854 |
| 1855 /* we should now have things packed up, so lets send |
| 1856 * it off */ |
| 1857 s->init_num=n+4; |
| 1858 s->init_off=0; |
| 1859 } |
| 1860 |
| 1861 s->state = SSL3_ST_SW_KEY_EXCH_B; |
| 1862 EVP_MD_CTX_cleanup(&md_ctx); |
| 1863 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 1864 f_err: |
| 1865 ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1866 err: |
| 1867 #ifndef OPENSSL_NO_ECDH |
| 1868 if (encodedPoint != NULL) OPENSSL_free(encodedPoint); |
| 1869 BN_CTX_free(bn_ctx); |
| 1870 #endif |
| 1871 EVP_MD_CTX_cleanup(&md_ctx); |
| 1872 return(-1); |
| 1873 } |
| 1874 |
| 1875 int ssl3_send_certificate_request(SSL *s) |
| 1876 { |
| 1877 unsigned char *p,*d; |
| 1878 int i,j,nl,off,n; |
| 1879 STACK_OF(X509_NAME) *sk=NULL; |
| 1880 X509_NAME *name; |
| 1881 BUF_MEM *buf; |
| 1882 |
| 1883 if (s->state == SSL3_ST_SW_CERT_REQ_A) |
| 1884 { |
| 1885 buf=s->init_buf; |
| 1886 |
| 1887 d=p=(unsigned char *)&(buf->data[4]); |
| 1888 |
| 1889 /* get the list of acceptable cert types */ |
| 1890 p++; |
| 1891 n=ssl3_get_req_cert_type(s,p); |
| 1892 d[0]=n; |
| 1893 p+=n; |
| 1894 n++; |
| 1895 |
| 1896 off=n; |
| 1897 p+=2; |
| 1898 n+=2; |
| 1899 |
| 1900 sk=SSL_get_client_CA_list(s); |
| 1901 nl=0; |
| 1902 if (sk != NULL) |
| 1903 { |
| 1904 for (i=0; i<sk_X509_NAME_num(sk); i++) |
| 1905 { |
| 1906 name=sk_X509_NAME_value(sk,i); |
| 1907 j=i2d_X509_NAME(name,NULL); |
| 1908 if (!BUF_MEM_grow_clean(buf,4+n+j+2)) |
| 1909 { |
| 1910 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUE
ST,ERR_R_BUF_LIB); |
| 1911 goto err; |
| 1912 } |
| 1913 p=(unsigned char *)&(buf->data[4+n]); |
| 1914 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) |
| 1915 { |
| 1916 s2n(j,p); |
| 1917 i2d_X509_NAME(name,&p); |
| 1918 n+=2+j; |
| 1919 nl+=2+j; |
| 1920 } |
| 1921 else |
| 1922 { |
| 1923 d=p; |
| 1924 i2d_X509_NAME(name,&p); |
| 1925 j-=2; s2n(j,d); j+=2; |
| 1926 n+=j; |
| 1927 nl+=j; |
| 1928 } |
| 1929 } |
| 1930 } |
| 1931 /* else no CA names */ |
| 1932 p=(unsigned char *)&(buf->data[4+off]); |
| 1933 s2n(nl,p); |
| 1934 |
| 1935 d=(unsigned char *)buf->data; |
| 1936 *(d++)=SSL3_MT_CERTIFICATE_REQUEST; |
| 1937 l2n3(n,d); |
| 1938 |
| 1939 /* we should now have things packed up, so lets send |
| 1940 * it off */ |
| 1941 |
| 1942 s->init_num=n+4; |
| 1943 s->init_off=0; |
| 1944 #ifdef NETSCAPE_HANG_BUG |
| 1945 p=(unsigned char *)s->init_buf->data + s->init_num; |
| 1946 |
| 1947 /* do the header */ |
| 1948 *(p++)=SSL3_MT_SERVER_DONE; |
| 1949 *(p++)=0; |
| 1950 *(p++)=0; |
| 1951 *(p++)=0; |
| 1952 s->init_num += 4; |
| 1953 #endif |
| 1954 |
| 1955 s->state = SSL3_ST_SW_CERT_REQ_B; |
| 1956 } |
| 1957 |
| 1958 /* SSL3_ST_SW_CERT_REQ_B */ |
| 1959 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 1960 err: |
| 1961 return(-1); |
| 1962 } |
| 1963 |
| 1964 int ssl3_get_client_key_exchange(SSL *s) |
| 1965 { |
| 1966 int i,al,ok; |
| 1967 long n; |
| 1968 unsigned long alg_k; |
| 1969 unsigned char *p; |
| 1970 #ifndef OPENSSL_NO_RSA |
| 1971 RSA *rsa=NULL; |
| 1972 EVP_PKEY *pkey=NULL; |
| 1973 #endif |
| 1974 #ifndef OPENSSL_NO_DH |
| 1975 BIGNUM *pub=NULL; |
| 1976 DH *dh_srvr; |
| 1977 #endif |
| 1978 #ifndef OPENSSL_NO_KRB5 |
| 1979 KSSL_ERR kssl_err; |
| 1980 #endif /* OPENSSL_NO_KRB5 */ |
| 1981 |
| 1982 #ifndef OPENSSL_NO_ECDH |
| 1983 EC_KEY *srvr_ecdh = NULL; |
| 1984 EVP_PKEY *clnt_pub_pkey = NULL; |
| 1985 EC_POINT *clnt_ecpoint = NULL; |
| 1986 BN_CTX *bn_ctx = NULL; |
| 1987 #endif |
| 1988 |
| 1989 n=s->method->ssl_get_message(s, |
| 1990 SSL3_ST_SR_KEY_EXCH_A, |
| 1991 SSL3_ST_SR_KEY_EXCH_B, |
| 1992 SSL3_MT_CLIENT_KEY_EXCHANGE, |
| 1993 2048, /* ??? */ |
| 1994 &ok); |
| 1995 |
| 1996 if (!ok) return((int)n); |
| 1997 p=(unsigned char *)s->init_msg; |
| 1998 |
| 1999 alg_k=s->s3->tmp.new_cipher->algorithm_mkey; |
| 2000 |
| 2001 #ifndef OPENSSL_NO_RSA |
| 2002 if (alg_k & SSL_kRSA) |
| 2003 { |
| 2004 /* FIX THIS UP EAY EAY EAY EAY */ |
| 2005 if (s->s3->tmp.use_rsa_tmp) |
| 2006 { |
| 2007 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL)) |
| 2008 rsa=s->cert->rsa_tmp; |
| 2009 /* Don't do a callback because rsa_tmp should |
| 2010 * be sent already */ |
| 2011 if (rsa == NULL) |
| 2012 { |
| 2013 al=SSL_AD_HANDSHAKE_FAILURE; |
| 2014 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_
MISSING_TMP_RSA_PKEY); |
| 2015 goto f_err; |
| 2016 |
| 2017 } |
| 2018 } |
| 2019 else |
| 2020 { |
| 2021 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey; |
| 2022 if ( (pkey == NULL) || |
| 2023 (pkey->type != EVP_PKEY_RSA) || |
| 2024 (pkey->pkey.rsa == NULL)) |
| 2025 { |
| 2026 al=SSL_AD_HANDSHAKE_FAILURE; |
| 2027 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_
MISSING_RSA_CERTIFICATE); |
| 2028 goto f_err; |
| 2029 } |
| 2030 rsa=pkey->pkey.rsa; |
| 2031 } |
| 2032 |
| 2033 /* TLS and [incidentally] DTLS{0xFEFF} */ |
| 2034 if (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER) |
| 2035 { |
| 2036 n2s(p,i); |
| 2037 if (n != i+2) |
| 2038 { |
| 2039 if (!(s->options & SSL_OP_TLS_D5_BUG)) |
| 2040 { |
| 2041 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANG
E,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); |
| 2042 goto err; |
| 2043 } |
| 2044 else |
| 2045 p-=2; |
| 2046 } |
| 2047 else |
| 2048 n=i; |
| 2049 } |
| 2050 |
| 2051 i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING); |
| 2052 |
| 2053 al = -1; |
| 2054 |
| 2055 if (i != SSL_MAX_MASTER_KEY_LENGTH) |
| 2056 { |
| 2057 al=SSL_AD_DECODE_ERROR; |
| 2058 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_R
SA_DECRYPT); */ |
| 2059 } |
| 2060 |
| 2061 if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] ==
(s->client_version & 0xff)))) |
| 2062 { |
| 2063 /* The premaster secret must contain the same version nu
mber as the |
| 2064 * ClientHello to detect version rollback attacks (stran
gely, the |
| 2065 * protocol does not offer such protection for DH cipher
suites). |
| 2066 * However, buggy clients exist that send the negotiated
protocol |
| 2067 * version instead if the server does not support the re
quested |
| 2068 * protocol version. |
| 2069 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clie
nts. */ |
| 2070 if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) && |
| 2071 (p[0] == (s->version>>8)) && (p[1] == (s->versio
n & 0xff)))) |
| 2072 { |
| 2073 al=SSL_AD_DECODE_ERROR; |
| 2074 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL
_R_BAD_PROTOCOL_VERSION_NUMBER); */ |
| 2075 |
| 2076 /* The Klima-Pokorny-Rosa extension of Bleichenb
acher's attack |
| 2077 * (http://eprint.iacr.org/2003/052/) exploits t
he version |
| 2078 * number check as a "bad version oracle" -- an
alert would |
| 2079 * reveal that the plaintext corresponding to so
me ciphertext |
| 2080 * made up by the adversary is properly formatte
d except |
| 2081 * that the version number is wrong. To avoid s
uch attacks, |
| 2082 * we should treat this just like any other decr
yption error. */ |
| 2083 } |
| 2084 } |
| 2085 |
| 2086 if (al != -1) |
| 2087 { |
| 2088 /* Some decryption failure -- use random value instead a
s countermeasure |
| 2089 * against Bleichenbacher's attack on PKCS #1 v1.5 RSA p
adding |
| 2090 * (see RFC 2246, section 7.4.7.1). */ |
| 2091 ERR_clear_error(); |
| 2092 i = SSL_MAX_MASTER_KEY_LENGTH; |
| 2093 p[0] = s->client_version >> 8; |
| 2094 p[1] = s->client_version & 0xff; |
| 2095 if (RAND_pseudo_bytes(p+2, i-2) <= 0) /* should be RAND_
bytes, but we cannot work around a failure */ |
| 2096 goto err; |
| 2097 } |
| 2098 |
| 2099 s->session->master_key_length= |
| 2100 s->method->ssl3_enc->generate_master_secret(s, |
| 2101 s->session->master_key, |
| 2102 p,i); |
| 2103 OPENSSL_cleanse(p,i); |
| 2104 } |
| 2105 else |
| 2106 #endif |
| 2107 #ifndef OPENSSL_NO_DH |
| 2108 if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) |
| 2109 { |
| 2110 n2s(p,i); |
| 2111 if (n != i+2) |
| 2112 { |
| 2113 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) |
| 2114 { |
| 2115 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_
DH_PUBLIC_VALUE_LENGTH_IS_WRONG); |
| 2116 goto err; |
| 2117 } |
| 2118 else |
| 2119 { |
| 2120 p-=2; |
| 2121 i=(int)n; |
| 2122 } |
| 2123 } |
| 2124 |
| 2125 if (n == 0L) /* the parameters are in the cert */ |
| 2126 { |
| 2127 al=SSL_AD_HANDSHAKE_FAILURE; |
| 2128 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_T
O_DECODE_DH_CERTS); |
| 2129 goto f_err; |
| 2130 } |
| 2131 else |
| 2132 { |
| 2133 if (s->s3->tmp.dh == NULL) |
| 2134 { |
| 2135 al=SSL_AD_HANDSHAKE_FAILURE; |
| 2136 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_
MISSING_TMP_DH_KEY); |
| 2137 goto f_err; |
| 2138 } |
| 2139 else |
| 2140 dh_srvr=s->s3->tmp.dh; |
| 2141 } |
| 2142 |
| 2143 pub=BN_bin2bn(p,i,NULL); |
| 2144 if (pub == NULL) |
| 2145 { |
| 2146 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB); |
| 2147 goto err; |
| 2148 } |
| 2149 |
| 2150 i=DH_compute_key(p,pub,dh_srvr); |
| 2151 |
| 2152 if (i <= 0) |
| 2153 { |
| 2154 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); |
| 2155 BN_clear_free(pub); |
| 2156 goto err; |
| 2157 } |
| 2158 |
| 2159 DH_free(s->s3->tmp.dh); |
| 2160 s->s3->tmp.dh=NULL; |
| 2161 |
| 2162 BN_clear_free(pub); |
| 2163 pub=NULL; |
| 2164 s->session->master_key_length= |
| 2165 s->method->ssl3_enc->generate_master_secret(s, |
| 2166 s->session->master_key,p,i); |
| 2167 OPENSSL_cleanse(p,i); |
| 2168 } |
| 2169 else |
| 2170 #endif |
| 2171 #ifndef OPENSSL_NO_KRB5 |
| 2172 if (alg_k & SSL_kKRB5) |
| 2173 { |
| 2174 krb5_error_code krb5rc; |
| 2175 krb5_data enc_ticket; |
| 2176 krb5_data authenticator; |
| 2177 krb5_data enc_pms; |
| 2178 KSSL_CTX *kssl_ctx = s->kssl_ctx; |
| 2179 EVP_CIPHER_CTX ciph_ctx; |
| 2180 const EVP_CIPHER *enc = NULL; |
| 2181 unsigned char iv[EVP_MAX_IV_LENGTH]; |
| 2182 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH |
| 2183 + EVP_MAX_BLOCK_LENGTH]; |
| 2184 int padl, outl; |
| 2185 krb5_timestamp authtime = 0; |
| 2186 krb5_ticket_times ttimes; |
| 2187 |
| 2188 EVP_CIPHER_CTX_init(&ciph_ctx); |
| 2189 |
| 2190 if (!kssl_ctx) kssl_ctx = kssl_ctx_new(); |
| 2191 |
| 2192 n2s(p,i); |
| 2193 enc_ticket.length = i; |
| 2194 |
| 2195 if (n < (long)(enc_ticket.length + 6)) |
| 2196 { |
| 2197 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2198 SSL_R_DATA_LENGTH_TOO_LONG); |
| 2199 goto err; |
| 2200 } |
| 2201 |
| 2202 enc_ticket.data = (char *)p; |
| 2203 p+=enc_ticket.length; |
| 2204 |
| 2205 n2s(p,i); |
| 2206 authenticator.length = i; |
| 2207 |
| 2208 if (n < (long)(enc_ticket.length + authenticator.length + 6)) |
| 2209 { |
| 2210 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2211 SSL_R_DATA_LENGTH_TOO_LONG); |
| 2212 goto err; |
| 2213 } |
| 2214 |
| 2215 authenticator.data = (char *)p; |
| 2216 p+=authenticator.length; |
| 2217 |
| 2218 n2s(p,i); |
| 2219 enc_pms.length = i; |
| 2220 enc_pms.data = (char *)p; |
| 2221 p+=enc_pms.length; |
| 2222 |
| 2223 /* Note that the length is checked again below, |
| 2224 ** after decryption |
| 2225 */ |
| 2226 if(enc_pms.length > sizeof pms) |
| 2227 { |
| 2228 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2229 SSL_R_DATA_LENGTH_TOO_LONG); |
| 2230 goto err; |
| 2231 } |
| 2232 |
| 2233 if (n != (long)(enc_ticket.length + authenticator.length + |
| 2234 enc_pms.length + 6)) |
| 2235 { |
| 2236 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2237 SSL_R_DATA_LENGTH_TOO_LONG); |
| 2238 goto err; |
| 2239 } |
| 2240 |
| 2241 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes, |
| 2242 &kssl_err)) != 0) |
| 2243 { |
| 2244 #ifdef KSSL_DEBUG |
| 2245 printf("kssl_sget_tkt rtn %d [%d]\n", |
| 2246 krb5rc, kssl_err.reason); |
| 2247 if (kssl_err.text) |
| 2248 printf("kssl_err text= %s\n", kssl_err.text); |
| 2249 #endif /* KSSL_DEBUG */ |
| 2250 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2251 kssl_err.reason); |
| 2252 goto err; |
| 2253 } |
| 2254 |
| 2255 /* Note: no authenticator is not considered an error, |
| 2256 ** but will return authtime == 0. |
| 2257 */ |
| 2258 if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator, |
| 2259 &authtime, &kssl_err)) != 0) |
| 2260 { |
| 2261 #ifdef KSSL_DEBUG |
| 2262 printf("kssl_check_authent rtn %d [%d]\n", |
| 2263 krb5rc, kssl_err.reason); |
| 2264 if (kssl_err.text) |
| 2265 printf("kssl_err text= %s\n", kssl_err.text); |
| 2266 #endif /* KSSL_DEBUG */ |
| 2267 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2268 kssl_err.reason); |
| 2269 goto err; |
| 2270 } |
| 2271 |
| 2272 if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0) |
| 2273 { |
| 2274 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc); |
| 2275 goto err; |
| 2276 } |
| 2277 |
| 2278 #ifdef KSSL_DEBUG |
| 2279 kssl_ctx_show(kssl_ctx); |
| 2280 #endif /* KSSL_DEBUG */ |
| 2281 |
| 2282 enc = kssl_map_enc(kssl_ctx->enctype); |
| 2283 if (enc == NULL) |
| 2284 goto err; |
| 2285 |
| 2286 memset(iv, 0, sizeof iv); /* per RFC 1510 */ |
| 2287 |
| 2288 if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv)) |
| 2289 { |
| 2290 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2291 SSL_R_DECRYPTION_FAILED); |
| 2292 goto err; |
| 2293 } |
| 2294 if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl, |
| 2295 (unsigned char *)enc_pms.data, enc_pms.l
ength)) |
| 2296 { |
| 2297 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2298 SSL_R_DECRYPTION_FAILED); |
| 2299 goto err; |
| 2300 } |
| 2301 if (outl > SSL_MAX_MASTER_KEY_LENGTH) |
| 2302 { |
| 2303 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2304 SSL_R_DATA_LENGTH_TOO_LONG); |
| 2305 goto err; |
| 2306 } |
| 2307 if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl)) |
| 2308 { |
| 2309 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2310 SSL_R_DECRYPTION_FAILED); |
| 2311 goto err; |
| 2312 } |
| 2313 outl += padl; |
| 2314 if (outl > SSL_MAX_MASTER_KEY_LENGTH) |
| 2315 { |
| 2316 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2317 SSL_R_DATA_LENGTH_TOO_LONG); |
| 2318 goto err; |
| 2319 } |
| 2320 if (!((pms[0] == (s->client_version>>8)) && (pms[1] == (s->clien
t_version & 0xff)))) |
| 2321 { |
| 2322 /* The premaster secret must contain the same version number
as the |
| 2323 * ClientHello to detect version rollback attacks (strangely
, the |
| 2324 * protocol does not offer such protection for DH ciphersuit
es). |
| 2325 * However, buggy clients exist that send random bytes inste
ad of |
| 2326 * the protocol version. |
| 2327 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients.
|
| 2328 * (Perhaps we should have a separate BUG value for the Kerb
eros cipher) |
| 2329 */ |
| 2330 if (!(s->options & SSL_OP_TLS_ROLLBACK_BUG)) |
| 2331 { |
| 2332 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2333 SSL_AD_DECODE_ERROR); |
| 2334 goto err; |
| 2335 } |
| 2336 } |
| 2337 |
| 2338 EVP_CIPHER_CTX_cleanup(&ciph_ctx); |
| 2339 |
| 2340 s->session->master_key_length= |
| 2341 s->method->ssl3_enc->generate_master_secret(s, |
| 2342 s->session->master_key, pms, outl); |
| 2343 |
| 2344 if (kssl_ctx->client_princ) |
| 2345 { |
| 2346 size_t len = strlen(kssl_ctx->client_princ); |
| 2347 if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH ) |
| 2348 { |
| 2349 s->session->krb5_client_princ_len = len; |
| 2350 memcpy(s->session->krb5_client_princ,kssl_ctx->c
lient_princ,len); |
| 2351 } |
| 2352 } |
| 2353 |
| 2354 |
| 2355 /* Was doing kssl_ctx_free() here, |
| 2356 ** but it caused problems for apache. |
| 2357 ** kssl_ctx = kssl_ctx_free(kssl_ctx); |
| 2358 ** if (s->kssl_ctx) s->kssl_ctx = NULL; |
| 2359 */ |
| 2360 } |
| 2361 else |
| 2362 #endif /* OPENSSL_NO_KRB5 */ |
| 2363 |
| 2364 #ifndef OPENSSL_NO_ECDH |
| 2365 if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) |
| 2366 { |
| 2367 int ret = 1; |
| 2368 int field_size = 0; |
| 2369 const EC_KEY *tkey; |
| 2370 const EC_GROUP *group; |
| 2371 const BIGNUM *priv_key; |
| 2372 |
| 2373 /* initialize structures for server's ECDH key pair */ |
| 2374 if ((srvr_ecdh = EC_KEY_new()) == NULL) |
| 2375 { |
| 2376 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2377 ERR_R_MALLOC_FAILURE); |
| 2378 goto err; |
| 2379 } |
| 2380 |
| 2381 /* Let's get server private key and group information */ |
| 2382 if (alg_k & (SSL_kECDHr|SSL_kECDHe)) |
| 2383 { |
| 2384 /* use the certificate */ |
| 2385 tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec; |
| 2386 } |
| 2387 else |
| 2388 { |
| 2389 /* use the ephermeral values we saved when |
| 2390 * generating the ServerKeyExchange msg. |
| 2391 */ |
| 2392 tkey = s->s3->tmp.ecdh; |
| 2393 } |
| 2394 |
| 2395 group = EC_KEY_get0_group(tkey); |
| 2396 priv_key = EC_KEY_get0_private_key(tkey); |
| 2397 |
| 2398 if (!EC_KEY_set_group(srvr_ecdh, group) || |
| 2399 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) |
| 2400 { |
| 2401 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2402 ERR_R_EC_LIB); |
| 2403 goto err; |
| 2404 } |
| 2405 |
| 2406 /* Let's get client's public key */ |
| 2407 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) |
| 2408 { |
| 2409 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2410 ERR_R_MALLOC_FAILURE); |
| 2411 goto err; |
| 2412 } |
| 2413 |
| 2414 if (n == 0L) |
| 2415 { |
| 2416 /* Client Publickey was in Client Certificate */ |
| 2417 |
| 2418 if (alg_k & SSL_kEECDH) |
| 2419 { |
| 2420 al=SSL_AD_HANDSHAKE_FAILURE; |
| 2421 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R
_MISSING_TMP_ECDH_KEY); |
| 2422 goto f_err; |
| 2423 } |
| 2424 if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer)) |
| 2425 == NULL) || |
| 2426 (clnt_pub_pkey->type != EVP_PKEY_EC)) |
| 2427 { |
| 2428 /* XXX: For now, we do not support client |
| 2429 * authentication using ECDH certificates |
| 2430 * so this branch (n == 0L) of the code is |
| 2431 * never executed. When that support is |
| 2432 * added, we ought to ensure the key |
| 2433 * received in the certificate is |
| 2434 * authorized for key agreement. |
| 2435 * ECDH_compute_key implicitly checks that |
| 2436 * the two ECDH shares are for the same |
| 2437 * group. |
| 2438 */ |
| 2439 al=SSL_AD_HANDSHAKE_FAILURE; |
| 2440 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2441 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS); |
| 2442 goto f_err; |
| 2443 } |
| 2444 |
| 2445 if (EC_POINT_copy(clnt_ecpoint, |
| 2446 EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec)) == 0
) |
| 2447 { |
| 2448 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2449 ERR_R_EC_LIB); |
| 2450 goto err; |
| 2451 } |
| 2452 ret = 2; /* Skip certificate verify processing */ |
| 2453 } |
| 2454 else |
| 2455 { |
| 2456 /* Get client's public key from encoded point |
| 2457 * in the ClientKeyExchange message. |
| 2458 */ |
| 2459 if ((bn_ctx = BN_CTX_new()) == NULL) |
| 2460 { |
| 2461 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2462 ERR_R_MALLOC_FAILURE); |
| 2463 goto err; |
| 2464 } |
| 2465 |
| 2466 /* Get encoded point length */ |
| 2467 i = *p; |
| 2468 p += 1; |
| 2469 if (n != 1 + i) |
| 2470 { |
| 2471 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2472 ERR_R_EC_LIB); |
| 2473 goto err; |
| 2474 } |
| 2475 if (EC_POINT_oct2point(group, |
| 2476 clnt_ecpoint, p, i, bn_ctx) == 0) |
| 2477 { |
| 2478 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2479 ERR_R_EC_LIB); |
| 2480 goto err; |
| 2481 } |
| 2482 /* p is pointing to somewhere in the buffer |
| 2483 * currently, so set it to the start |
| 2484 */ |
| 2485 p=(unsigned char *)s->init_buf->data; |
| 2486 } |
| 2487 |
| 2488 /* Compute the shared pre-master secret */ |
| 2489 field_size = EC_GROUP_get_degree(group); |
| 2490 if (field_size <= 0) |
| 2491 { |
| 2492 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2493 ERR_R_ECDH_LIB); |
| 2494 goto err; |
| 2495 } |
| 2496 i = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecd
h, NULL); |
| 2497 if (i <= 0) |
| 2498 { |
| 2499 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2500 ERR_R_ECDH_LIB); |
| 2501 goto err; |
| 2502 } |
| 2503 |
| 2504 EVP_PKEY_free(clnt_pub_pkey); |
| 2505 EC_POINT_free(clnt_ecpoint); |
| 2506 EC_KEY_free(srvr_ecdh); |
| 2507 BN_CTX_free(bn_ctx); |
| 2508 EC_KEY_free(s->s3->tmp.ecdh); |
| 2509 s->s3->tmp.ecdh = NULL; |
| 2510 |
| 2511 /* Compute the master secret */ |
| 2512 s->session->master_key_length = s->method->ssl3_enc-> \ |
| 2513 generate_master_secret(s, s->session->master_key, p, i); |
| 2514 |
| 2515 OPENSSL_cleanse(p, i); |
| 2516 return (ret); |
| 2517 } |
| 2518 else |
| 2519 #endif |
| 2520 #ifndef OPENSSL_NO_PSK |
| 2521 if (alg_k & SSL_kPSK) |
| 2522 { |
| 2523 unsigned char *t = NULL; |
| 2524 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4]; |
| 2525 unsigned int pre_ms_len = 0, psk_len = 0; |
| 2526 int psk_err = 1; |
| 2527 char tmp_id[PSK_MAX_IDENTITY_LEN+1]; |
| 2528 |
| 2529 al=SSL_AD_HANDSHAKE_FAILURE; |
| 2530 |
| 2531 n2s(p,i); |
| 2532 if (n != i+2) |
| 2533 { |
| 2534 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2535 SSL_R_LENGTH_MISMATCH); |
| 2536 goto psk_err; |
| 2537 } |
| 2538 if (i > PSK_MAX_IDENTITY_LEN) |
| 2539 { |
| 2540 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2541 SSL_R_DATA_LENGTH_TOO_LONG); |
| 2542 goto psk_err; |
| 2543 } |
| 2544 if (s->psk_server_callback == NULL) |
| 2545 { |
| 2546 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2547 SSL_R_PSK_NO_SERVER_CB); |
| 2548 goto psk_err; |
| 2549 } |
| 2550 |
| 2551 /* Create guaranteed NULL-terminated identity |
| 2552 * string for the callback */ |
| 2553 memcpy(tmp_id, p, i); |
| 2554 memset(tmp_id+i, 0, PSK_MAX_IDENTITY_LEN+1-i); |
| 2555 psk_len = s->psk_server_callback(s, tmp_id, |
| 2556 psk_or_pre_ms, sizeof(psk_or_pre_ms)); |
| 2557 OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN+1); |
| 2558 |
| 2559 if (psk_len > PSK_MAX_PSK_LEN) |
| 2560 { |
| 2561 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2562 ERR_R_INTERNAL_ERROR); |
| 2563 goto psk_err; |
| 2564 } |
| 2565 else if (psk_len == 0) |
| 2566 { |
| 2567 /* PSK related to the given identity not found *
/ |
| 2568 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2569 SSL_R_PSK_IDENTITY_NOT_FOUND); |
| 2570 al=SSL_AD_UNKNOWN_PSK_IDENTITY; |
| 2571 goto psk_err; |
| 2572 } |
| 2573 |
| 2574 /* create PSK pre_master_secret */ |
| 2575 pre_ms_len=2+psk_len+2+psk_len; |
| 2576 t = psk_or_pre_ms; |
| 2577 memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len)
; |
| 2578 s2n(psk_len, t); |
| 2579 memset(t, 0, psk_len); |
| 2580 t+=psk_len; |
| 2581 s2n(psk_len, t); |
| 2582 |
| 2583 if (s->session->psk_identity != NULL) |
| 2584 OPENSSL_free(s->session->psk_identity); |
| 2585 s->session->psk_identity = BUF_strdup((char *)p); |
| 2586 if (s->session->psk_identity == NULL) |
| 2587 { |
| 2588 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2589 ERR_R_MALLOC_FAILURE); |
| 2590 goto psk_err; |
| 2591 } |
| 2592 |
| 2593 if (s->session->psk_identity_hint != NULL) |
| 2594 OPENSSL_free(s->session->psk_identity_hint); |
| 2595 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_i
dentity_hint); |
| 2596 if (s->ctx->psk_identity_hint != NULL && |
| 2597 s->session->psk_identity_hint == NULL) |
| 2598 { |
| 2599 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2600 ERR_R_MALLOC_FAILURE); |
| 2601 goto psk_err; |
| 2602 } |
| 2603 |
| 2604 s->session->master_key_length= |
| 2605 s->method->ssl3_enc->generate_master_secret(s, |
| 2606 s->session->master_key, psk_or_pre_ms, p
re_ms_len); |
| 2607 psk_err = 0; |
| 2608 psk_err: |
| 2609 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms)); |
| 2610 if (psk_err != 0) |
| 2611 goto f_err; |
| 2612 } |
| 2613 else |
| 2614 #endif |
| 2615 if (alg_k & SSL_kGOST) |
| 2616 { |
| 2617 int ret = 0; |
| 2618 EVP_PKEY_CTX *pkey_ctx; |
| 2619 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL; |
| 2620 unsigned char premaster_secret[32], *start; |
| 2621 size_t outlen=32, inlen; |
| 2622 unsigned long alg_a; |
| 2623 |
| 2624 /* Get our certificate private key*/ |
| 2625 alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
| 2626 if (alg_a & SSL_aGOST94) |
| 2627 pk = s->cert->pkeys[SSL_PKEY_GOST94].privatekey; |
| 2628 else if (alg_a & SSL_aGOST01) |
| 2629 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey; |
| 2630 |
| 2631 pkey_ctx = EVP_PKEY_CTX_new(pk,NULL); |
| 2632 EVP_PKEY_decrypt_init(pkey_ctx); |
| 2633 /* If client certificate is present and is of the same t
ype, maybe |
| 2634 * use it for key exchange. Don't mind errors from |
| 2635 * EVP_PKEY_derive_set_peer, because it is completely va
lid to use |
| 2636 * a client certificate for authorization only. */ |
| 2637 client_pub_pkey = X509_get_pubkey(s->session->peer); |
| 2638 if (client_pub_pkey) |
| 2639 { |
| 2640 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pu
b_pkey) <= 0) |
| 2641 ERR_clear_error(); |
| 2642 } |
| 2643 /* Decrypt session key */ |
| 2644 if ((*p!=( V_ASN1_SEQUENCE| V_ASN1_CONSTRUCTED))) |
| 2645 { |
| 2646 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_
DECRYPTION_FAILED); |
| 2647 goto gerr; |
| 2648 } |
| 2649 if (p[1] == 0x81) |
| 2650 { |
| 2651 start = p+3; |
| 2652 inlen = p[2]; |
| 2653 } |
| 2654 else if (p[1] < 0x80) |
| 2655 { |
| 2656 start = p+2; |
| 2657 inlen = p[1]; |
| 2658 } |
| 2659 else |
| 2660 { |
| 2661 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_
DECRYPTION_FAILED); |
| 2662 goto gerr; |
| 2663 } |
| 2664 if (EVP_PKEY_decrypt(pkey_ctx,premaster_secret,&outlen,s
tart,inlen) <=0) |
| 2665 |
| 2666 { |
| 2667 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_
DECRYPTION_FAILED); |
| 2668 goto gerr; |
| 2669 } |
| 2670 /* Generate master secret */ |
| 2671 s->session->master_key_length= |
| 2672 s->method->ssl3_enc->generate_master_secret(s, |
| 2673 s->session->master_key,premaster_secret,
32); |
| 2674 /* Check if pubkey from client certificate was used */ |
| 2675 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PE
ER_KEY, 2, NULL) > 0) |
| 2676 ret = 2; |
| 2677 else |
| 2678 ret = 1; |
| 2679 gerr: |
| 2680 EVP_PKEY_free(client_pub_pkey); |
| 2681 EVP_PKEY_CTX_free(pkey_ctx); |
| 2682 if (ret) |
| 2683 return ret; |
| 2684 else |
| 2685 goto err; |
| 2686 } |
| 2687 else |
| 2688 { |
| 2689 al=SSL_AD_HANDSHAKE_FAILURE; |
| 2690 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, |
| 2691 SSL_R_UNKNOWN_CIPHER_TYPE); |
| 2692 goto f_err; |
| 2693 } |
| 2694 |
| 2695 return(1); |
| 2696 f_err: |
| 2697 ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 2698 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_E
CDH) |
| 2699 err: |
| 2700 #endif |
| 2701 #ifndef OPENSSL_NO_ECDH |
| 2702 EVP_PKEY_free(clnt_pub_pkey); |
| 2703 EC_POINT_free(clnt_ecpoint); |
| 2704 if (srvr_ecdh != NULL) |
| 2705 EC_KEY_free(srvr_ecdh); |
| 2706 BN_CTX_free(bn_ctx); |
| 2707 #endif |
| 2708 return(-1); |
| 2709 } |
| 2710 |
| 2711 int ssl3_get_cert_verify(SSL *s) |
| 2712 { |
| 2713 EVP_PKEY *pkey=NULL; |
| 2714 unsigned char *p; |
| 2715 int al,ok,ret=0; |
| 2716 long n; |
| 2717 int type=0,i,j; |
| 2718 X509 *peer; |
| 2719 |
| 2720 n=s->method->ssl_get_message(s, |
| 2721 SSL3_ST_SR_CERT_VRFY_A, |
| 2722 SSL3_ST_SR_CERT_VRFY_B, |
| 2723 -1, |
| 2724 514, /* 514? */ |
| 2725 &ok); |
| 2726 |
| 2727 if (!ok) return((int)n); |
| 2728 |
| 2729 if (s->session->peer != NULL) |
| 2730 { |
| 2731 peer=s->session->peer; |
| 2732 pkey=X509_get_pubkey(peer); |
| 2733 type=X509_certificate_type(peer,pkey); |
| 2734 } |
| 2735 else |
| 2736 { |
| 2737 peer=NULL; |
| 2738 pkey=NULL; |
| 2739 } |
| 2740 |
| 2741 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY) |
| 2742 { |
| 2743 s->s3->tmp.reuse_message=1; |
| 2744 if ((peer != NULL) && (type | EVP_PKT_SIGN)) |
| 2745 { |
| 2746 al=SSL_AD_UNEXPECTED_MESSAGE; |
| 2747 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_M
ESSAGE); |
| 2748 goto f_err; |
| 2749 } |
| 2750 ret=1; |
| 2751 goto end; |
| 2752 } |
| 2753 |
| 2754 if (peer == NULL) |
| 2755 { |
| 2756 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED)
; |
| 2757 al=SSL_AD_UNEXPECTED_MESSAGE; |
| 2758 goto f_err; |
| 2759 } |
| 2760 |
| 2761 if (!(type & EVP_PKT_SIGN)) |
| 2762 { |
| 2763 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNIN
G_CERTIFICATE); |
| 2764 al=SSL_AD_ILLEGAL_PARAMETER; |
| 2765 goto f_err; |
| 2766 } |
| 2767 |
| 2768 if (s->s3->change_cipher_spec) |
| 2769 { |
| 2770 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY); |
| 2771 al=SSL_AD_UNEXPECTED_MESSAGE; |
| 2772 goto f_err; |
| 2773 } |
| 2774 |
| 2775 /* we now have a signature that we need to verify */ |
| 2776 p=(unsigned char *)s->init_msg; |
| 2777 /* Check for broken implementations of GOST ciphersuites */ |
| 2778 /* If key is GOST and n is exactly 64, it is bare |
| 2779 * signature without length field */ |
| 2780 if (n==64 && (pkey->type==NID_id_GostR3410_94 || |
| 2781 pkey->type == NID_id_GostR3410_2001) ) |
| 2782 { |
| 2783 i=64; |
| 2784 } |
| 2785 else |
| 2786 { |
| 2787 n2s(p,i); |
| 2788 n-=2; |
| 2789 if (i > n) |
| 2790 { |
| 2791 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH)
; |
| 2792 al=SSL_AD_DECODE_ERROR; |
| 2793 goto f_err; |
| 2794 } |
| 2795 } |
| 2796 j=EVP_PKEY_size(pkey); |
| 2797 if ((i > j) || (n > j) || (n <= 0)) |
| 2798 { |
| 2799 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE); |
| 2800 al=SSL_AD_DECODE_ERROR; |
| 2801 goto f_err; |
| 2802 } |
| 2803 |
| 2804 #ifndef OPENSSL_NO_RSA |
| 2805 if (pkey->type == EVP_PKEY_RSA) |
| 2806 { |
| 2807 i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md, |
| 2808 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i, |
| 2809 pkey->pkey.rsa); |
| 2810 if (i < 0) |
| 2811 { |
| 2812 al=SSL_AD_DECRYPT_ERROR; |
| 2813 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT)
; |
| 2814 goto f_err; |
| 2815 } |
| 2816 if (i == 0) |
| 2817 { |
| 2818 al=SSL_AD_DECRYPT_ERROR; |
| 2819 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATUR
E); |
| 2820 goto f_err; |
| 2821 } |
| 2822 } |
| 2823 else |
| 2824 #endif |
| 2825 #ifndef OPENSSL_NO_DSA |
| 2826 if (pkey->type == EVP_PKEY_DSA) |
| 2827 { |
| 2828 j=DSA_verify(pkey->save_type, |
| 2829 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]), |
| 2830 SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa); |
| 2831 if (j <= 0) |
| 2832 { |
| 2833 /* bad signature */ |
| 2834 al=SSL_AD_DECRYPT_ERROR; |
| 2835 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATUR
E); |
| 2836 goto f_err; |
| 2837 } |
| 2838 } |
| 2839 else |
| 2840 #endif |
| 2841 #ifndef OPENSSL_NO_ECDSA |
| 2842 if (pkey->type == EVP_PKEY_EC) |
| 2843 { |
| 2844 j=ECDSA_verify(pkey->save_type, |
| 2845 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]), |
| 2846 SHA_DIGEST_LENGTH,p,i,pkey->pkey.ec); |
| 2847 if (j <= 0) |
| 2848 { |
| 2849 /* bad signature */ |
| 2850 al=SSL_AD_DECRYPT_ERROR; |
| 2851 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, |
| 2852 SSL_R_BAD_ECDSA_SIGNATURE); |
| 2853 goto f_err; |
| 2854 } |
| 2855 } |
| 2856 else |
| 2857 #endif |
| 2858 if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_
2001) |
| 2859 { unsigned char signature[64]; |
| 2860 int idx; |
| 2861 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey,NULL); |
| 2862 EVP_PKEY_verify_init(pctx); |
| 2863 if (i!=64) { |
| 2864 fprintf(stderr,"GOST signature length is %d",i); |
| 2865 } |
| 2866 for (idx=0;idx<64;idx++) { |
| 2867 signature[63-idx]=p[idx]; |
| 2868 } |
| 2869 j=EVP_PKEY_verify(pctx,signature,64,s->s3->tmp.cert_veri
fy_md,32); |
| 2870 EVP_PKEY_CTX_free(pctx); |
| 2871 if (j<=0) |
| 2872 { |
| 2873 al=SSL_AD_DECRYPT_ERROR; |
| 2874 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, |
| 2875 SSL_R_BAD_ECDSA_SIGNATURE); |
| 2876 goto f_err; |
| 2877 } |
| 2878 } |
| 2879 else |
| 2880 { |
| 2881 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR); |
| 2882 al=SSL_AD_UNSUPPORTED_CERTIFICATE; |
| 2883 goto f_err; |
| 2884 } |
| 2885 |
| 2886 |
| 2887 ret=1; |
| 2888 if (0) |
| 2889 { |
| 2890 f_err: |
| 2891 ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 2892 } |
| 2893 end: |
| 2894 EVP_PKEY_free(pkey); |
| 2895 return(ret); |
| 2896 } |
| 2897 |
| 2898 int ssl3_get_client_certificate(SSL *s) |
| 2899 { |
| 2900 int i,ok,al,ret= -1; |
| 2901 X509 *x=NULL; |
| 2902 unsigned long l,nc,llen,n; |
| 2903 const unsigned char *p,*q; |
| 2904 unsigned char *d; |
| 2905 STACK_OF(X509) *sk=NULL; |
| 2906 |
| 2907 n=s->method->ssl_get_message(s, |
| 2908 SSL3_ST_SR_CERT_A, |
| 2909 SSL3_ST_SR_CERT_B, |
| 2910 -1, |
| 2911 s->max_cert_list, |
| 2912 &ok); |
| 2913 |
| 2914 if (!ok) return((int)n); |
| 2915 |
| 2916 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) |
| 2917 { |
| 2918 if ( (s->verify_mode & SSL_VERIFY_PEER) && |
| 2919 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) |
| 2920 { |
| 2921 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_
NOT_RETURN_A_CERTIFICATE); |
| 2922 al=SSL_AD_HANDSHAKE_FAILURE; |
| 2923 goto f_err; |
| 2924 } |
| 2925 /* If tls asked for a client cert, the client must return a 0 li
st */ |
| 2926 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) |
| 2927 { |
| 2928 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_
DID_NOT_RESPOND_WITH_CERTIFICATE_LIST); |
| 2929 al=SSL_AD_UNEXPECTED_MESSAGE; |
| 2930 goto f_err; |
| 2931 } |
| 2932 s->s3->tmp.reuse_message=1; |
| 2933 return(1); |
| 2934 } |
| 2935 |
| 2936 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) |
| 2937 { |
| 2938 al=SSL_AD_UNEXPECTED_MESSAGE; |
| 2939 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYP
E); |
| 2940 goto f_err; |
| 2941 } |
| 2942 p=d=(unsigned char *)s->init_msg; |
| 2943 |
| 2944 if ((sk=sk_X509_new_null()) == NULL) |
| 2945 { |
| 2946 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE); |
| 2947 goto err; |
| 2948 } |
| 2949 |
| 2950 n2l3(p,llen); |
| 2951 if (llen+3 != n) |
| 2952 { |
| 2953 al=SSL_AD_DECODE_ERROR; |
| 2954 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH); |
| 2955 goto f_err; |
| 2956 } |
| 2957 for (nc=0; nc<llen; ) |
| 2958 { |
| 2959 n2l3(p,l); |
| 2960 if ((l+nc+3) > llen) |
| 2961 { |
| 2962 al=SSL_AD_DECODE_ERROR; |
| 2963 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENG
TH_MISMATCH); |
| 2964 goto f_err; |
| 2965 } |
| 2966 |
| 2967 q=p; |
| 2968 x=d2i_X509(NULL,&p,l); |
| 2969 if (x == NULL) |
| 2970 { |
| 2971 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB)
; |
| 2972 goto err; |
| 2973 } |
| 2974 if (p != (q+l)) |
| 2975 { |
| 2976 al=SSL_AD_DECODE_ERROR; |
| 2977 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENG
TH_MISMATCH); |
| 2978 goto f_err; |
| 2979 } |
| 2980 if (!sk_X509_push(sk,x)) |
| 2981 { |
| 2982 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FA
ILURE); |
| 2983 goto err; |
| 2984 } |
| 2985 x=NULL; |
| 2986 nc+=l+3; |
| 2987 } |
| 2988 |
| 2989 if (sk_X509_num(sk) <= 0) |
| 2990 { |
| 2991 /* TLS does not mind 0 certs returned */ |
| 2992 if (s->version == SSL3_VERSION) |
| 2993 { |
| 2994 al=SSL_AD_HANDSHAKE_FAILURE; |
| 2995 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIF
ICATES_RETURNED); |
| 2996 goto f_err; |
| 2997 } |
| 2998 /* Fail for TLS only if we required a certificate */ |
| 2999 else if ((s->verify_mode & SSL_VERIFY_PEER) && |
| 3000 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) |
| 3001 { |
| 3002 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_
NOT_RETURN_A_CERTIFICATE); |
| 3003 al=SSL_AD_HANDSHAKE_FAILURE; |
| 3004 goto f_err; |
| 3005 } |
| 3006 } |
| 3007 else |
| 3008 { |
| 3009 i=ssl_verify_cert_chain(s,sk); |
| 3010 if (i <= 0) |
| 3011 { |
| 3012 al=ssl_verify_alarm_type(s->verify_result); |
| 3013 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIF
ICATE_RETURNED); |
| 3014 goto f_err; |
| 3015 } |
| 3016 } |
| 3017 |
| 3018 if (s->session->peer != NULL) /* This should not be needed */ |
| 3019 X509_free(s->session->peer); |
| 3020 s->session->peer=sk_X509_shift(sk); |
| 3021 s->session->verify_result = s->verify_result; |
| 3022 |
| 3023 /* With the current implementation, sess_cert will always be NULL |
| 3024 * when we arrive here. */ |
| 3025 if (s->session->sess_cert == NULL) |
| 3026 { |
| 3027 s->session->sess_cert = ssl_sess_cert_new(); |
| 3028 if (s->session->sess_cert == NULL) |
| 3029 { |
| 3030 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_F
AILURE); |
| 3031 goto err; |
| 3032 } |
| 3033 } |
| 3034 if (s->session->sess_cert->cert_chain != NULL) |
| 3035 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free); |
| 3036 s->session->sess_cert->cert_chain=sk; |
| 3037 /* Inconsistency alert: cert_chain does *not* include the |
| 3038 * peer's own certificate, while we do include it in s3_clnt.c */ |
| 3039 |
| 3040 sk=NULL; |
| 3041 |
| 3042 ret=1; |
| 3043 if (0) |
| 3044 { |
| 3045 f_err: |
| 3046 ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 3047 } |
| 3048 err: |
| 3049 if (x != NULL) X509_free(x); |
| 3050 if (sk != NULL) sk_X509_pop_free(sk,X509_free); |
| 3051 return(ret); |
| 3052 } |
| 3053 |
| 3054 int ssl3_send_server_certificate(SSL *s) |
| 3055 { |
| 3056 unsigned long l; |
| 3057 X509 *x; |
| 3058 |
| 3059 if (s->state == SSL3_ST_SW_CERT_A) |
| 3060 { |
| 3061 x=ssl_get_server_send_cert(s); |
| 3062 if (x == NULL) |
| 3063 { |
| 3064 /* VRS: allow null cert if auth == KRB5 */ |
| 3065 if ((s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5)
|| |
| 3066 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5)) |
| 3067 { |
| 3068 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_
INTERNAL_ERROR); |
| 3069 return(0); |
| 3070 } |
| 3071 } |
| 3072 |
| 3073 l=ssl3_output_cert_chain(s,x); |
| 3074 s->state=SSL3_ST_SW_CERT_B; |
| 3075 s->init_num=(int)l; |
| 3076 s->init_off=0; |
| 3077 } |
| 3078 |
| 3079 /* SSL3_ST_SW_CERT_B */ |
| 3080 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 3081 } |
| 3082 #ifndef OPENSSL_NO_TLSEXT |
| 3083 int ssl3_send_newsession_ticket(SSL *s) |
| 3084 { |
| 3085 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) |
| 3086 { |
| 3087 unsigned char *p, *senc, *macstart; |
| 3088 int len, slen; |
| 3089 unsigned int hlen; |
| 3090 EVP_CIPHER_CTX ctx; |
| 3091 HMAC_CTX hctx; |
| 3092 SSL_CTX *tctx = s->initial_ctx; |
| 3093 unsigned char iv[EVP_MAX_IV_LENGTH]; |
| 3094 unsigned char key_name[16]; |
| 3095 |
| 3096 /* get session encoding length */ |
| 3097 slen = i2d_SSL_SESSION(s->session, NULL); |
| 3098 /* Some length values are 16 bits, so forget it if session is |
| 3099 * too long |
| 3100 */ |
| 3101 if (slen > 0xFF00) |
| 3102 return -1; |
| 3103 /* Grow buffer if need be: the length calculation is as |
| 3104 * follows 1 (size of message name) + 3 (message length |
| 3105 * bytes) + 4 (ticket lifetime hint) + 2 (ticket length) + |
| 3106 * 16 (key name) + max_iv_len (iv length) + |
| 3107 * session_length + max_enc_block_size (max encrypted session |
| 3108 * length) + max_md_size (HMAC). |
| 3109 */ |
| 3110 if (!BUF_MEM_grow(s->init_buf, |
| 3111 26 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + |
| 3112 EVP_MAX_MD_SIZE + slen)) |
| 3113 return -1; |
| 3114 senc = OPENSSL_malloc(slen); |
| 3115 if (!senc) |
| 3116 return -1; |
| 3117 p = senc; |
| 3118 i2d_SSL_SESSION(s->session, &p); |
| 3119 |
| 3120 p=(unsigned char *)s->init_buf->data; |
| 3121 /* do the header */ |
| 3122 *(p++)=SSL3_MT_NEWSESSION_TICKET; |
| 3123 /* Skip message length for now */ |
| 3124 p += 3; |
| 3125 EVP_CIPHER_CTX_init(&ctx); |
| 3126 HMAC_CTX_init(&hctx); |
| 3127 /* Initialize HMAC and cipher contexts. If callback present |
| 3128 * it does all the work otherwise use generated values |
| 3129 * from parent ctx. |
| 3130 */ |
| 3131 if (tctx->tlsext_ticket_key_cb) |
| 3132 { |
| 3133 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, |
| 3134 &hctx, 1) < 0) |
| 3135 { |
| 3136 OPENSSL_free(senc); |
| 3137 return -1; |
| 3138 } |
| 3139 } |
| 3140 else |
| 3141 { |
| 3142 RAND_pseudo_bytes(iv, 16); |
| 3143 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, |
| 3144 tctx->tlsext_tick_aes_key, iv); |
| 3145 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, |
| 3146 tlsext_tick_md(), NULL); |
| 3147 memcpy(key_name, tctx->tlsext_tick_key_name, 16); |
| 3148 } |
| 3149 l2n(s->session->tlsext_tick_lifetime_hint, p); |
| 3150 /* Skip ticket length for now */ |
| 3151 p += 2; |
| 3152 /* Output key name */ |
| 3153 macstart = p; |
| 3154 memcpy(p, key_name, 16); |
| 3155 p += 16; |
| 3156 /* output IV */ |
| 3157 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx)); |
| 3158 p += EVP_CIPHER_CTX_iv_length(&ctx); |
| 3159 /* Encrypt session data */ |
| 3160 EVP_EncryptUpdate(&ctx, p, &len, senc, slen); |
| 3161 p += len; |
| 3162 EVP_EncryptFinal(&ctx, p, &len); |
| 3163 p += len; |
| 3164 EVP_CIPHER_CTX_cleanup(&ctx); |
| 3165 |
| 3166 HMAC_Update(&hctx, macstart, p - macstart); |
| 3167 HMAC_Final(&hctx, p, &hlen); |
| 3168 HMAC_CTX_cleanup(&hctx); |
| 3169 |
| 3170 p += hlen; |
| 3171 /* Now write out lengths: p points to end of data written */ |
| 3172 /* Total length */ |
| 3173 len = p - (unsigned char *)s->init_buf->data; |
| 3174 p=(unsigned char *)s->init_buf->data + 1; |
| 3175 l2n3(len - 4, p); /* Message length */ |
| 3176 p += 4; |
| 3177 s2n(len - 10, p); /* Ticket length */ |
| 3178 |
| 3179 /* number of bytes to write */ |
| 3180 s->init_num= len; |
| 3181 s->state=SSL3_ST_SW_SESSION_TICKET_B; |
| 3182 s->init_off=0; |
| 3183 OPENSSL_free(senc); |
| 3184 } |
| 3185 |
| 3186 /* SSL3_ST_SW_SESSION_TICKET_B */ |
| 3187 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 3188 } |
| 3189 |
| 3190 int ssl3_send_cert_status(SSL *s) |
| 3191 { |
| 3192 if (s->state == SSL3_ST_SW_CERT_STATUS_A) |
| 3193 { |
| 3194 unsigned char *p; |
| 3195 /* Grow buffer if need be: the length calculation is as |
| 3196 * follows 1 (message type) + 3 (message length) + |
| 3197 * 1 (ocsp response type) + 3 (ocsp response length) |
| 3198 * + (ocsp response) |
| 3199 */ |
| 3200 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen)) |
| 3201 return -1; |
| 3202 |
| 3203 p=(unsigned char *)s->init_buf->data; |
| 3204 |
| 3205 /* do the header */ |
| 3206 *(p++)=SSL3_MT_CERTIFICATE_STATUS; |
| 3207 /* message length */ |
| 3208 l2n3(s->tlsext_ocsp_resplen + 4, p); |
| 3209 /* status type */ |
| 3210 *(p++)= s->tlsext_status_type; |
| 3211 /* length of OCSP response */ |
| 3212 l2n3(s->tlsext_ocsp_resplen, p); |
| 3213 /* actual response */ |
| 3214 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen); |
| 3215 /* number of bytes to write */ |
| 3216 s->init_num = 8 + s->tlsext_ocsp_resplen; |
| 3217 s->state=SSL3_ST_SW_CERT_STATUS_B; |
| 3218 s->init_off = 0; |
| 3219 } |
| 3220 |
| 3221 /* SSL3_ST_SW_CERT_STATUS_B */ |
| 3222 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 3223 } |
| 3224 #endif |
OLD | NEW |