| 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/ec.h> | |
| 161 #include <openssl/ecdsa.h> | |
| 162 #include <openssl/evp.h> | |
| 163 #include <openssl/hmac.h> | |
| 164 #include <openssl/sha.h> | |
| 165 #include <openssl/x509.h> | |
| 166 #ifndef OPENSSL_NO_DH | |
| 167 #include <openssl/dh.h> | |
| 168 #endif | |
| 169 #include <openssl/bn.h> | |
| 170 #ifndef OPENSSL_NO_KRB5 | |
| 171 #include <openssl/krb5_asn.h> | |
| 172 #endif | |
| 173 #include <openssl/md5.h> | |
| 174 | |
| 175 static const SSL_METHOD *ssl3_get_server_method(int ver); | |
| 176 | |
| 177 static const SSL_METHOD *ssl3_get_server_method(int ver) | |
| 178 { | |
| 179 if (ver == SSL3_VERSION) | |
| 180 return(SSLv3_server_method()); | |
| 181 else | |
| 182 return(NULL); | |
| 183 } | |
| 184 | |
| 185 #ifndef OPENSSL_NO_SRP | |
| 186 static int ssl_check_srp_ext_ClientHello(SSL *s, int *al) | |
| 187 { | |
| 188 int ret = SSL_ERROR_NONE; | |
| 189 | |
| 190 *al = SSL_AD_UNRECOGNIZED_NAME; | |
| 191 | |
| 192 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) && | |
| 193 (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) | |
| 194 { | |
| 195 if(s->srp_ctx.login == NULL) | |
| 196 { | |
| 197 /* RFC 5054 says SHOULD reject, | |
| 198 we do so if There is no srp login name */ | |
| 199 ret = SSL3_AL_FATAL; | |
| 200 *al = SSL_AD_UNKNOWN_PSK_IDENTITY; | |
| 201 } | |
| 202 else | |
| 203 { | |
| 204 ret = SSL_srp_server_param_with_username(s,al); | |
| 205 } | |
| 206 } | |
| 207 return ret; | |
| 208 } | |
| 209 #endif | |
| 210 | |
| 211 IMPLEMENT_ssl3_meth_func(SSLv3_server_method, | |
| 212 ssl3_accept, | |
| 213 ssl_undefined_function, | |
| 214 ssl3_get_server_method) | |
| 215 | |
| 216 int ssl3_accept(SSL *s) | |
| 217 { | |
| 218 BUF_MEM *buf; | |
| 219 unsigned long alg_k,Time=(unsigned long)time(NULL); | |
| 220 void (*cb)(const SSL *ssl,int type,int val)=NULL; | |
| 221 int ret= -1; | |
| 222 int new_state,state,skip=0; | |
| 223 | |
| 224 RAND_add(&Time,sizeof(Time),0); | |
| 225 ERR_clear_error(); | |
| 226 clear_sys_error(); | |
| 227 | |
| 228 if (s->info_callback != NULL) | |
| 229 cb=s->info_callback; | |
| 230 else if (s->ctx->info_callback != NULL) | |
| 231 cb=s->ctx->info_callback; | |
| 232 | |
| 233 /* init things to blank */ | |
| 234 s->in_handshake++; | |
| 235 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); | |
| 236 | |
| 237 if (s->cert == NULL) | |
| 238 { | |
| 239 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET); | |
| 240 return(-1); | |
| 241 } | |
| 242 | |
| 243 #ifndef OPENSSL_NO_HEARTBEATS | |
| 244 /* If we're awaiting a HeartbeatResponse, pretend we | |
| 245 * already got and don't await it anymore, because | |
| 246 * Heartbeats don't make sense during handshakes anyway. | |
| 247 */ | |
| 248 if (s->tlsext_hb_pending) | |
| 249 { | |
| 250 s->tlsext_hb_pending = 0; | |
| 251 s->tlsext_hb_seq++; | |
| 252 } | |
| 253 #endif | |
| 254 | |
| 255 for (;;) | |
| 256 { | |
| 257 state=s->state; | |
| 258 | |
| 259 switch (s->state) | |
| 260 { | |
| 261 case SSL_ST_RENEGOTIATE: | |
| 262 s->renegotiate=1; | |
| 263 /* s->state=SSL_ST_ACCEPT; */ | |
| 264 | |
| 265 case SSL_ST_BEFORE: | |
| 266 case SSL_ST_ACCEPT: | |
| 267 case SSL_ST_BEFORE|SSL_ST_ACCEPT: | |
| 268 case SSL_ST_OK|SSL_ST_ACCEPT: | |
| 269 | |
| 270 s->server=1; | |
| 271 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); | |
| 272 | |
| 273 if ((s->version>>8) != 3) | |
| 274 { | |
| 275 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR); | |
| 276 return -1; | |
| 277 } | |
| 278 s->type=SSL_ST_ACCEPT; | |
| 279 | |
| 280 if (s->init_buf == NULL) | |
| 281 { | |
| 282 if ((buf=BUF_MEM_new()) == NULL) | |
| 283 { | |
| 284 ret= -1; | |
| 285 goto end; | |
| 286 } | |
| 287 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) | |
| 288 { | |
| 289 ret= -1; | |
| 290 goto end; | |
| 291 } | |
| 292 s->init_buf=buf; | |
| 293 } | |
| 294 | |
| 295 if (!ssl3_setup_buffers(s)) | |
| 296 { | |
| 297 ret= -1; | |
| 298 goto end; | |
| 299 } | |
| 300 | |
| 301 s->init_num=0; | |
| 302 s->s3->flags &= ~SSL3_FLAGS_SGC_RESTART_DONE; | |
| 303 | |
| 304 if (s->state != SSL_ST_RENEGOTIATE) | |
| 305 { | |
| 306 /* Ok, we now need to push on a buffering BIO so
that | |
| 307 * the output is sent in a way that TCP likes :-
) | |
| 308 */ | |
| 309 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto
end; } | |
| 310 | |
| 311 ssl3_init_finished_mac(s); | |
| 312 s->state=SSL3_ST_SR_CLNT_HELLO_A; | |
| 313 s->ctx->stats.sess_accept++; | |
| 314 } | |
| 315 else if (!s->s3->send_connection_binding && | |
| 316 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGO
TIATION)) | |
| 317 { | |
| 318 /* Server attempting to renegotiate with | |
| 319 * client that doesn't support secure | |
| 320 * renegotiation. | |
| 321 */ | |
| 322 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_UNSAFE_LEGACY_RE
NEGOTIATION_DISABLED); | |
| 323 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE
_FAILURE); | |
| 324 ret = -1; | |
| 325 goto end; | |
| 326 } | |
| 327 else | |
| 328 { | |
| 329 /* s->state == SSL_ST_RENEGOTIATE, | |
| 330 * we will just send a HelloRequest */ | |
| 331 s->ctx->stats.sess_accept_renegotiate++; | |
| 332 s->state=SSL3_ST_SW_HELLO_REQ_A; | |
| 333 } | |
| 334 break; | |
| 335 | |
| 336 case SSL3_ST_SW_HELLO_REQ_A: | |
| 337 case SSL3_ST_SW_HELLO_REQ_B: | |
| 338 | |
| 339 s->shutdown=0; | |
| 340 ret=ssl3_send_hello_request(s); | |
| 341 if (ret <= 0) goto end; | |
| 342 s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C; | |
| 343 s->state=SSL3_ST_SW_FLUSH; | |
| 344 s->init_num=0; | |
| 345 | |
| 346 ssl3_init_finished_mac(s); | |
| 347 break; | |
| 348 | |
| 349 case SSL3_ST_SW_HELLO_REQ_C: | |
| 350 s->state=SSL_ST_OK; | |
| 351 break; | |
| 352 | |
| 353 case SSL3_ST_SR_CLNT_HELLO_A: | |
| 354 case SSL3_ST_SR_CLNT_HELLO_B: | |
| 355 case SSL3_ST_SR_CLNT_HELLO_C: | |
| 356 | |
| 357 s->shutdown=0; | |
| 358 if (s->rwstate != SSL_X509_LOOKUP) | |
| 359 { | |
| 360 ret=ssl3_get_client_hello(s); | |
| 361 if (ret <= 0) goto end; | |
| 362 } | |
| 363 #ifndef OPENSSL_NO_SRP | |
| 364 { | |
| 365 int al; | |
| 366 if ((ret = ssl_check_srp_ext_ClientHello(s,&al)) < 0) | |
| 367 { | |
| 368 /* callback indicates firther work to be
done */ | |
| 369 s->rwstate=SSL_X509_LOOKUP; | |
| 370 goto end; | |
| 371 } | |
| 372 if (ret != SSL_ERROR_NONE) | |
| 373 { | |
| 374 ssl3_send_alert(s,SSL3_AL_FATAL,al); | |
| 375 /* This is not really an error but the only mean
s to | |
| 376 for a client to detect whether srp is support
ed. */ | |
| 377 if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY) | |
| 378 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_CLIENTHEL
LO_TLSEXT); | |
| 379 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
| |
| 380 ret= -1; | |
| 381 goto end; | |
| 382 } | |
| 383 } | |
| 384 #endif | |
| 385 | |
| 386 s->renegotiate = 2; | |
| 387 s->state=SSL3_ST_SW_SRVR_HELLO_A; | |
| 388 s->init_num=0; | |
| 389 break; | |
| 390 | |
| 391 case SSL3_ST_SW_SRVR_HELLO_A: | |
| 392 case SSL3_ST_SW_SRVR_HELLO_B: | |
| 393 ret=ssl3_send_server_hello(s); | |
| 394 if (ret <= 0) goto end; | |
| 395 #ifndef OPENSSL_NO_TLSEXT | |
| 396 if (s->hit) | |
| 397 { | |
| 398 if (s->tlsext_ticket_expected) | |
| 399 s->state=SSL3_ST_SW_SESSION_TICKET_A; | |
| 400 else | |
| 401 s->state=SSL3_ST_SW_CHANGE_A; | |
| 402 } | |
| 403 #else | |
| 404 if (s->hit) | |
| 405 s->state=SSL3_ST_SW_CHANGE_A; | |
| 406 #endif | |
| 407 else | |
| 408 s->state=SSL3_ST_SW_CERT_A; | |
| 409 s->init_num=0; | |
| 410 break; | |
| 411 | |
| 412 case SSL3_ST_SW_CERT_A: | |
| 413 case SSL3_ST_SW_CERT_B: | |
| 414 /* Check if it is anon DH or anon ECDH, */ | |
| 415 /* normal PSK or KRB5 or SRP */ | |
| 416 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) | |
| 417 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL
_kPSK) | |
| 418 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL
_aKRB5)) | |
| 419 { | |
| 420 ret=ssl3_send_server_certificate(s); | |
| 421 if (ret <= 0) goto end; | |
| 422 #ifndef OPENSSL_NO_TLSEXT | |
| 423 if (s->tlsext_status_expected) | |
| 424 s->state=SSL3_ST_SW_CERT_STATUS_A; | |
| 425 else | |
| 426 s->state=SSL3_ST_SW_KEY_EXCH_A; | |
| 427 } | |
| 428 else | |
| 429 { | |
| 430 skip = 1; | |
| 431 s->state=SSL3_ST_SW_KEY_EXCH_A; | |
| 432 } | |
| 433 #else | |
| 434 } | |
| 435 else | |
| 436 skip=1; | |
| 437 | |
| 438 s->state=SSL3_ST_SW_KEY_EXCH_A; | |
| 439 #endif | |
| 440 s->init_num=0; | |
| 441 break; | |
| 442 | |
| 443 case SSL3_ST_SW_KEY_EXCH_A: | |
| 444 case SSL3_ST_SW_KEY_EXCH_B: | |
| 445 alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | |
| 446 | |
| 447 /* clear this, it may get reset by | |
| 448 * send_server_key_exchange */ | |
| 449 if ((s->options & SSL_OP_EPHEMERAL_RSA) | |
| 450 #ifndef OPENSSL_NO_KRB5 | |
| 451 && !(alg_k & SSL_kKRB5) | |
| 452 #endif /* OPENSSL_NO_KRB5 */ | |
| 453 ) | |
| 454 /* option SSL_OP_EPHEMERAL_RSA sends temporary R
SA key | |
| 455 * even when forbidden by protocol specs | |
| 456 * (handshake may fail as clients are not requir
ed to | |
| 457 * be able to handle this) */ | |
| 458 s->s3->tmp.use_rsa_tmp=1; | |
| 459 else | |
| 460 s->s3->tmp.use_rsa_tmp=0; | |
| 461 | |
| 462 | |
| 463 /* only send if a DH key exchange, fortezza or | |
| 464 * RSA but we have a sign only certificate | |
| 465 * | |
| 466 * PSK: may send PSK identity hints | |
| 467 * | |
| 468 * For ECC ciphersuites, we send a serverKeyExchange | |
| 469 * message only if the cipher suite is either | |
| 470 * ECDH-anon or ECDHE. In other cases, the | |
| 471 * server certificate contains the server's | |
| 472 * public key for key exchange. | |
| 473 */ | |
| 474 if (s->s3->tmp.use_rsa_tmp | |
| 475 /* PSK: send ServerKeyExchange if PSK identity | |
| 476 * hint if provided */ | |
| 477 #ifndef OPENSSL_NO_PSK | |
| 478 || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint) | |
| 479 #endif | |
| 480 #ifndef OPENSSL_NO_SRP | |
| 481 /* SRP: send ServerKeyExchange */ | |
| 482 || (alg_k & SSL_kSRP) | |
| 483 #endif | |
| 484 || (alg_k & (SSL_kDHr|SSL_kDHd|SSL_kEDH)) | |
| 485 || (alg_k & SSL_kEECDH) | |
| 486 || ((alg_k & SSL_kRSA) | |
| 487 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey
== NULL | |
| 488 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) | |
| 489 && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY
_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher) | |
| 490 ) | |
| 491 ) | |
| 492 ) | |
| 493 ) | |
| 494 { | |
| 495 ret=ssl3_send_server_key_exchange(s); | |
| 496 if (ret <= 0) goto end; | |
| 497 } | |
| 498 else | |
| 499 skip=1; | |
| 500 | |
| 501 s->state=SSL3_ST_SW_CERT_REQ_A; | |
| 502 s->init_num=0; | |
| 503 break; | |
| 504 | |
| 505 case SSL3_ST_SW_CERT_REQ_A: | |
| 506 case SSL3_ST_SW_CERT_REQ_B: | |
| 507 if (/* don't request cert unless asked for it: */ | |
| 508 !(s->verify_mode & SSL_VERIFY_PEER) || | |
| 509 /* if SSL_VERIFY_CLIENT_ONCE is set, | |
| 510 * don't request cert during re-negotiation: */ | |
| 511 ((s->session->peer != NULL) && | |
| 512 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || | |
| 513 /* never request cert in anonymous ciphersuites | |
| 514 * (see section "Certificate request" in SSL 3 d
rafts | |
| 515 * and in RFC 2246): */ | |
| 516 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aN
ULL) && | |
| 517 /* ... except when the application insists on v
erification | |
| 518 * (against the specs, but s3_clnt.c accepts th
is for SSL 3) */ | |
| 519 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_C
ERT)) || | |
| 520 /* never request cert in Kerberos ciphersuites
*/ | |
| 521 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKR
B5) | |
| 522 /* With normal PSK Certificates and | |
| 523 * Certificate Requests are omitted */ | |
| 524 || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_
kPSK)) | |
| 525 { | |
| 526 /* no cert request */ | |
| 527 skip=1; | |
| 528 s->s3->tmp.cert_request=0; | |
| 529 s->state=SSL3_ST_SW_SRVR_DONE_A; | |
| 530 if (s->s3->handshake_buffer) | |
| 531 if (!ssl3_digest_cached_records(s)) | |
| 532 return -1; | |
| 533 } | |
| 534 else | |
| 535 { | |
| 536 s->s3->tmp.cert_request=1; | |
| 537 ret=ssl3_send_certificate_request(s); | |
| 538 if (ret <= 0) goto end; | |
| 539 #ifndef NETSCAPE_HANG_BUG | |
| 540 s->state=SSL3_ST_SW_SRVR_DONE_A; | |
| 541 #else | |
| 542 s->state=SSL3_ST_SW_FLUSH; | |
| 543 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; | |
| 544 #endif | |
| 545 s->init_num=0; | |
| 546 } | |
| 547 break; | |
| 548 | |
| 549 case SSL3_ST_SW_SRVR_DONE_A: | |
| 550 case SSL3_ST_SW_SRVR_DONE_B: | |
| 551 ret=ssl3_send_server_done(s); | |
| 552 if (ret <= 0) goto end; | |
| 553 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; | |
| 554 s->state=SSL3_ST_SW_FLUSH; | |
| 555 s->init_num=0; | |
| 556 break; | |
| 557 | |
| 558 case SSL3_ST_SW_FLUSH: | |
| 559 | |
| 560 /* This code originally checked to see if | |
| 561 * any data was pending using BIO_CTRL_INFO | |
| 562 * and then flushed. This caused problems | |
| 563 * as documented in PR#1939. The proposed | |
| 564 * fix doesn't completely resolve this issue | |
| 565 * as buggy implementations of BIO_CTRL_PENDING | |
| 566 * still exist. So instead we just flush | |
| 567 * unconditionally. | |
| 568 */ | |
| 569 | |
| 570 s->rwstate=SSL_WRITING; | |
| 571 if (BIO_flush(s->wbio) <= 0) | |
| 572 { | |
| 573 ret= -1; | |
| 574 goto end; | |
| 575 } | |
| 576 s->rwstate=SSL_NOTHING; | |
| 577 | |
| 578 s->state=s->s3->tmp.next_state; | |
| 579 break; | |
| 580 | |
| 581 case SSL3_ST_SR_CERT_A: | |
| 582 case SSL3_ST_SR_CERT_B: | |
| 583 /* Check for second client hello (MS SGC) */ | |
| 584 ret = ssl3_check_client_hello(s); | |
| 585 if (ret <= 0) | |
| 586 goto end; | |
| 587 if (ret == 2) | |
| 588 s->state = SSL3_ST_SR_CLNT_HELLO_C; | |
| 589 else { | |
| 590 if (s->s3->tmp.cert_request) | |
| 591 { | |
| 592 ret=ssl3_get_client_certificate(s); | |
| 593 if (ret <= 0) goto end; | |
| 594 } | |
| 595 s->init_num=0; | |
| 596 s->state=SSL3_ST_SR_KEY_EXCH_A; | |
| 597 } | |
| 598 break; | |
| 599 | |
| 600 case SSL3_ST_SR_KEY_EXCH_A: | |
| 601 case SSL3_ST_SR_KEY_EXCH_B: | |
| 602 ret=ssl3_get_client_key_exchange(s); | |
| 603 if (ret <= 0) | |
| 604 goto end; | |
| 605 if (ret == 2) | |
| 606 { | |
| 607 /* For the ECDH ciphersuites when | |
| 608 * the client sends its ECDH pub key in | |
| 609 * a certificate, the CertificateVerify | |
| 610 * message is not sent. | |
| 611 * Also for GOST ciphersuites when | |
| 612 * the client uses its key from the certificate | |
| 613 * for key exchange. | |
| 614 */ | |
| 615 s->init_num = 0; | |
| 616 s->state=SSL3_ST_SR_POST_CLIENT_CERT; | |
| 617 } | |
| 618 else if (TLS1_get_version(s) >= TLS1_2_VERSION) | |
| 619 { | |
| 620 s->state=SSL3_ST_SR_CERT_VRFY_A; | |
| 621 s->init_num=0; | |
| 622 if (!s->session->peer) | |
| 623 break; | |
| 624 /* For TLS v1.2 freeze the handshake buffer | |
| 625 * at this point and digest cached records. | |
| 626 */ | |
| 627 if (!s->s3->handshake_buffer) | |
| 628 { | |
| 629 SSLerr(SSL_F_SSL3_ACCEPT,ERR_R_INTERNAL_
ERROR); | |
| 630 return -1; | |
| 631 } | |
| 632 s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE; | |
| 633 if (!ssl3_digest_cached_records(s)) | |
| 634 return -1; | |
| 635 } | |
| 636 else | |
| 637 { | |
| 638 int offset=0; | |
| 639 int dgst_num; | |
| 640 | |
| 641 s->state=SSL3_ST_SR_CERT_VRFY_A; | |
| 642 s->init_num=0; | |
| 643 | |
| 644 /* We need to get hashes here so if there is | |
| 645 * a client cert, it can be verified | |
| 646 * FIXME - digest processing for CertificateVeri
fy | |
| 647 * should be generalized. But it is next step | |
| 648 */ | |
| 649 if (s->s3->handshake_buffer) | |
| 650 if (!ssl3_digest_cached_records(s)) | |
| 651 return -1; | |
| 652 for (dgst_num=0; dgst_num<SSL_MAX_DIGEST;dgst_nu
m++) | |
| 653 if (s->s3->handshake_dgst[dgst_num]) | |
| 654 { | |
| 655 int dgst_size; | |
| 656 | |
| 657 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])); | |
| 658 dgst_size=EVP_MD_CTX_size(s->s3-
>handshake_dgst[dgst_num]); | |
| 659 if (dgst_size < 0) | |
| 660 { | |
| 661 ret = -1; | |
| 662 goto end; | |
| 663 } | |
| 664 offset+=dgst_size; | |
| 665 } | |
| 666 } | |
| 667 break; | |
| 668 | |
| 669 case SSL3_ST_SR_CERT_VRFY_A: | |
| 670 case SSL3_ST_SR_CERT_VRFY_B: | |
| 671 | |
| 672 s->s3->flags |= SSL3_FLAGS_CCS_OK; | |
| 673 /* we should decide if we expected this one */ | |
| 674 ret=ssl3_get_cert_verify(s); | |
| 675 if (ret <= 0) goto end; | |
| 676 | |
| 677 s->state=SSL3_ST_SR_POST_CLIENT_CERT; | |
| 678 s->init_num=0; | |
| 679 break; | |
| 680 | |
| 681 case SSL3_ST_SR_POST_CLIENT_CERT: { | |
| 682 char next_proto_neg = 0; | |
| 683 char channel_id = 0; | |
| 684 #if !defined(OPENSSL_NO_TLSEXT) | |
| 685 # if !defined(OPENSSL_NO_NEXTPROTONEG) | |
| 686 next_proto_neg = s->s3->next_proto_neg_seen; | |
| 687 # endif | |
| 688 channel_id = s->s3->tlsext_channel_id_valid; | |
| 689 #endif | |
| 690 s->s3->flags |= SSL3_FLAGS_CCS_OK; | |
| 691 | |
| 692 if (next_proto_neg) | |
| 693 s->state=SSL3_ST_SR_NEXT_PROTO_A; | |
| 694 else if (channel_id) | |
| 695 s->state=SSL3_ST_SR_CHANNEL_ID_A; | |
| 696 else | |
| 697 s->state=SSL3_ST_SR_FINISHED_A; | |
| 698 break; | |
| 699 } | |
| 700 | |
| 701 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) | |
| 702 case SSL3_ST_SR_NEXT_PROTO_A: | |
| 703 case SSL3_ST_SR_NEXT_PROTO_B: | |
| 704 ret=ssl3_get_next_proto(s); | |
| 705 if (ret <= 0) goto end; | |
| 706 s->init_num = 0; | |
| 707 if (s->s3->tlsext_channel_id_valid) | |
| 708 s->state=SSL3_ST_SR_CHANNEL_ID_A; | |
| 709 else | |
| 710 s->state=SSL3_ST_SR_FINISHED_A; | |
| 711 break; | |
| 712 #endif | |
| 713 | |
| 714 #if !defined(OPENSSL_NO_TLSEXT) | |
| 715 case SSL3_ST_SR_CHANNEL_ID_A: | |
| 716 case SSL3_ST_SR_CHANNEL_ID_B: | |
| 717 ret=ssl3_get_channel_id(s); | |
| 718 if (ret <= 0) goto end; | |
| 719 s->init_num = 0; | |
| 720 s->state=SSL3_ST_SR_FINISHED_A; | |
| 721 break; | |
| 722 #endif | |
| 723 | |
| 724 case SSL3_ST_SR_FINISHED_A: | |
| 725 case SSL3_ST_SR_FINISHED_B: | |
| 726 s->s3->flags |= SSL3_FLAGS_CCS_OK; | |
| 727 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A, | |
| 728 SSL3_ST_SR_FINISHED_B); | |
| 729 if (ret <= 0) goto end; | |
| 730 if (s->hit) | |
| 731 s->state=SSL_ST_OK; | |
| 732 #ifndef OPENSSL_NO_TLSEXT | |
| 733 else if (s->tlsext_ticket_expected) | |
| 734 s->state=SSL3_ST_SW_SESSION_TICKET_A; | |
| 735 #endif | |
| 736 else | |
| 737 s->state=SSL3_ST_SW_CHANGE_A; | |
| 738 s->init_num=0; | |
| 739 break; | |
| 740 | |
| 741 #ifndef OPENSSL_NO_TLSEXT | |
| 742 case SSL3_ST_SW_SESSION_TICKET_A: | |
| 743 case SSL3_ST_SW_SESSION_TICKET_B: | |
| 744 ret=ssl3_send_newsession_ticket(s); | |
| 745 if (ret <= 0) goto end; | |
| 746 s->state=SSL3_ST_SW_CHANGE_A; | |
| 747 s->init_num=0; | |
| 748 break; | |
| 749 | |
| 750 case SSL3_ST_SW_CERT_STATUS_A: | |
| 751 case SSL3_ST_SW_CERT_STATUS_B: | |
| 752 ret=ssl3_send_cert_status(s); | |
| 753 if (ret <= 0) goto end; | |
| 754 s->state=SSL3_ST_SW_KEY_EXCH_A; | |
| 755 s->init_num=0; | |
| 756 break; | |
| 757 | |
| 758 #endif | |
| 759 | |
| 760 case SSL3_ST_SW_CHANGE_A: | |
| 761 case SSL3_ST_SW_CHANGE_B: | |
| 762 | |
| 763 s->session->cipher=s->s3->tmp.new_cipher; | |
| 764 if (!s->method->ssl3_enc->setup_key_block(s)) | |
| 765 { ret= -1; goto end; } | |
| 766 | |
| 767 ret=ssl3_send_change_cipher_spec(s, | |
| 768 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B); | |
| 769 | |
| 770 if (ret <= 0) goto end; | |
| 771 s->state=SSL3_ST_SW_FINISHED_A; | |
| 772 s->init_num=0; | |
| 773 | |
| 774 if (!s->method->ssl3_enc->change_cipher_state(s, | |
| 775 SSL3_CHANGE_CIPHER_SERVER_WRITE)) | |
| 776 { | |
| 777 ret= -1; | |
| 778 goto end; | |
| 779 } | |
| 780 | |
| 781 break; | |
| 782 | |
| 783 case SSL3_ST_SW_FINISHED_A: | |
| 784 case SSL3_ST_SW_FINISHED_B: | |
| 785 ret=ssl3_send_finished(s, | |
| 786 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B, | |
| 787 s->method->ssl3_enc->server_finished_label, | |
| 788 s->method->ssl3_enc->server_finished_label_len); | |
| 789 if (ret <= 0) goto end; | |
| 790 s->state=SSL3_ST_SW_FLUSH; | |
| 791 if (s->hit) | |
| 792 s->s3->tmp.next_state=SSL3_ST_SR_POST_CLIENT_CER
T; | |
| 793 else | |
| 794 s->s3->tmp.next_state=SSL_ST_OK; | |
| 795 s->init_num=0; | |
| 796 break; | |
| 797 | |
| 798 case SSL_ST_OK: | |
| 799 /* clean a few things up */ | |
| 800 ssl3_cleanup_key_block(s); | |
| 801 | |
| 802 BUF_MEM_free(s->init_buf); | |
| 803 s->init_buf=NULL; | |
| 804 | |
| 805 /* remove buffering on output */ | |
| 806 ssl_free_wbio_buffer(s); | |
| 807 | |
| 808 s->init_num=0; | |
| 809 | |
| 810 if (s->renegotiate == 2) /* skipped if we just sent a He
lloRequest */ | |
| 811 { | |
| 812 s->renegotiate=0; | |
| 813 s->new_session=0; | |
| 814 | |
| 815 ssl_update_cache(s,SSL_SESS_CACHE_SERVER); | |
| 816 | |
| 817 s->ctx->stats.sess_accept_good++; | |
| 818 /* s->server=1; */ | |
| 819 s->handshake_func=ssl3_accept; | |
| 820 | |
| 821 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); | |
| 822 } | |
| 823 | |
| 824 ret = 1; | |
| 825 goto end; | |
| 826 /* break; */ | |
| 827 | |
| 828 default: | |
| 829 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE); | |
| 830 ret= -1; | |
| 831 goto end; | |
| 832 /* break; */ | |
| 833 } | |
| 834 | |
| 835 if (!s->s3->tmp.reuse_message && !skip) | |
| 836 { | |
| 837 if (s->debug) | |
| 838 { | |
| 839 if ((ret=BIO_flush(s->wbio)) <= 0) | |
| 840 goto end; | |
| 841 } | |
| 842 | |
| 843 | |
| 844 if ((cb != NULL) && (s->state != state)) | |
| 845 { | |
| 846 new_state=s->state; | |
| 847 s->state=state; | |
| 848 cb(s,SSL_CB_ACCEPT_LOOP,1); | |
| 849 s->state=new_state; | |
| 850 } | |
| 851 } | |
| 852 skip=0; | |
| 853 } | |
| 854 end: | |
| 855 /* BIO_flush(s->wbio); */ | |
| 856 | |
| 857 s->in_handshake--; | |
| 858 if (cb != NULL) | |
| 859 cb(s,SSL_CB_ACCEPT_EXIT,ret); | |
| 860 return(ret); | |
| 861 } | |
| 862 | |
| 863 int ssl3_send_hello_request(SSL *s) | |
| 864 { | |
| 865 unsigned char *p; | |
| 866 | |
| 867 if (s->state == SSL3_ST_SW_HELLO_REQ_A) | |
| 868 { | |
| 869 p=(unsigned char *)s->init_buf->data; | |
| 870 *(p++)=SSL3_MT_HELLO_REQUEST; | |
| 871 *(p++)=0; | |
| 872 *(p++)=0; | |
| 873 *(p++)=0; | |
| 874 | |
| 875 s->state=SSL3_ST_SW_HELLO_REQ_B; | |
| 876 /* number of bytes to write */ | |
| 877 s->init_num=4; | |
| 878 s->init_off=0; | |
| 879 } | |
| 880 | |
| 881 /* SSL3_ST_SW_HELLO_REQ_B */ | |
| 882 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | |
| 883 } | |
| 884 | |
| 885 int ssl3_check_client_hello(SSL *s) | |
| 886 { | |
| 887 int ok; | |
| 888 long n; | |
| 889 | |
| 890 /* this function is called when we really expect a Certificate message, | |
| 891 * so permit appropriate message length */ | |
| 892 n=s->method->ssl_get_message(s, | |
| 893 SSL3_ST_SR_CERT_A, | |
| 894 SSL3_ST_SR_CERT_B, | |
| 895 -1, | |
| 896 s->max_cert_list, | |
| 897 &ok); | |
| 898 if (!ok) return((int)n); | |
| 899 s->s3->tmp.reuse_message = 1; | |
| 900 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO) | |
| 901 { | |
| 902 /* We only allow the client to restart the handshake once per | |
| 903 * negotiation. */ | |
| 904 if (s->s3->flags & SSL3_FLAGS_SGC_RESTART_DONE) | |
| 905 { | |
| 906 SSLerr(SSL_F_SSL3_CHECK_CLIENT_HELLO, SSL_R_MULTIPLE_SGC
_RESTARTS); | |
| 907 return -1; | |
| 908 } | |
| 909 /* Throw away what we have done so far in the current handshake, | |
| 910 * which will now be aborted. (A full SSL_clear would be too muc
h.) */ | |
| 911 #ifndef OPENSSL_NO_DH | |
| 912 if (s->s3->tmp.dh != NULL) | |
| 913 { | |
| 914 DH_free(s->s3->tmp.dh); | |
| 915 s->s3->tmp.dh = NULL; | |
| 916 } | |
| 917 #endif | |
| 918 #ifndef OPENSSL_NO_ECDH | |
| 919 if (s->s3->tmp.ecdh != NULL) | |
| 920 { | |
| 921 EC_KEY_free(s->s3->tmp.ecdh); | |
| 922 s->s3->tmp.ecdh = NULL; | |
| 923 } | |
| 924 #endif | |
| 925 s->s3->flags |= SSL3_FLAGS_SGC_RESTART_DONE; | |
| 926 return 2; | |
| 927 } | |
| 928 return 1; | |
| 929 } | |
| 930 | |
| 931 int ssl3_get_client_hello(SSL *s) | |
| 932 { | |
| 933 int i,j,ok,al,ret= -1; | |
| 934 unsigned int cookie_len; | |
| 935 long n; | |
| 936 unsigned long id; | |
| 937 unsigned char *p,*d,*q; | |
| 938 SSL_CIPHER *c; | |
| 939 #ifndef OPENSSL_NO_COMP | |
| 940 SSL_COMP *comp=NULL; | |
| 941 #endif | |
| 942 STACK_OF(SSL_CIPHER) *ciphers=NULL; | |
| 943 | |
| 944 /* We do this so that we will respond with our native type. | |
| 945 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1, | |
| 946 * This down switching should be handled by a different method. | |
| 947 * If we are SSLv3, we will respond with SSLv3, even if prompted with | |
| 948 * TLSv1. | |
| 949 */ | |
| 950 if (s->state == SSL3_ST_SR_CLNT_HELLO_A | |
| 951 ) | |
| 952 { | |
| 953 s->state=SSL3_ST_SR_CLNT_HELLO_B; | |
| 954 } | |
| 955 s->first_packet=1; | |
| 956 n=s->method->ssl_get_message(s, | |
| 957 SSL3_ST_SR_CLNT_HELLO_B, | |
| 958 SSL3_ST_SR_CLNT_HELLO_C, | |
| 959 SSL3_MT_CLIENT_HELLO, | |
| 960 SSL3_RT_MAX_PLAIN_LENGTH, | |
| 961 &ok); | |
| 962 | |
| 963 if (!ok) return((int)n); | |
| 964 s->first_packet=0; | |
| 965 d=p=(unsigned char *)s->init_msg; | |
| 966 | |
| 967 /* use version from inside client hello, not from record header | |
| 968 * (may differ: see RFC 2246, Appendix E, second paragraph) */ | |
| 969 s->client_version=(((int)p[0])<<8)|(int)p[1]; | |
| 970 p+=2; | |
| 971 | |
| 972 if ((s->version == DTLS1_VERSION && s->client_version > s->version) || | |
| 973 (s->version != DTLS1_VERSION && s->client_version < s->version)) | |
| 974 { | |
| 975 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER); | |
| 976 if ((s->client_version>>8) == SSL3_VERSION_MAJOR) | |
| 977 { | |
| 978 /* similar to ssl3_get_record, send alert using remote v
ersion number */ | |
| 979 s->version = s->client_version; | |
| 980 } | |
| 981 al = SSL_AD_PROTOCOL_VERSION; | |
| 982 goto f_err; | |
| 983 } | |
| 984 | |
| 985 /* If we require cookies and this ClientHello doesn't | |
| 986 * contain one, just return since we do not want to | |
| 987 * allocate any memory yet. So check cookie length... | |
| 988 */ | |
| 989 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) | |
| 990 { | |
| 991 unsigned int session_length, cookie_length; | |
| 992 | |
| 993 session_length = *(p + SSL3_RANDOM_SIZE); | |
| 994 cookie_length = *(p + SSL3_RANDOM_SIZE + session_length + 1); | |
| 995 | |
| 996 if (cookie_length == 0) | |
| 997 return 1; | |
| 998 } | |
| 999 | |
| 1000 /* load the client random */ | |
| 1001 memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE); | |
| 1002 p+=SSL3_RANDOM_SIZE; | |
| 1003 | |
| 1004 /* get the session-id */ | |
| 1005 j= *(p++); | |
| 1006 | |
| 1007 s->hit=0; | |
| 1008 /* Versions before 0.9.7 always allow clients to resume sessions in rene
gotiation. | |
| 1009 * 0.9.7 and later allow this by default, but optionally ignore resumpti
on requests | |
| 1010 * with flag SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new f
lag rather | |
| 1011 * than a change to default behavior so that applications relying on thi
s for security | |
| 1012 * won't even compile against older library versions). | |
| 1013 * | |
| 1014 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
request | |
| 1015 * renegotiation but not a new session (s->new_session remains unset): f
or servers, | |
| 1016 * this essentially just means that the SSL_OP_NO_SESSION_RESUMPTION_ON_
RENEGOTIATION | |
| 1017 * setting will be ignored. | |
| 1018 */ | |
| 1019 if ((s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_REN
EGOTIATION))) | |
| 1020 { | |
| 1021 if (!s->session_creation_enabled) | |
| 1022 { | |
| 1023 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE
); | |
| 1024 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_SESSION_MAY_NOT
_BE_CREATED); | |
| 1025 goto err; | |
| 1026 } | |
| 1027 if (!ssl_get_new_session(s,1)) | |
| 1028 goto err; | |
| 1029 } | |
| 1030 else | |
| 1031 { | |
| 1032 i=ssl_get_prev_session(s, p, j, d + n); | |
| 1033 if (i == 1) | |
| 1034 { /* previous session */ | |
| 1035 s->hit=1; | |
| 1036 } | |
| 1037 else if (i == -1) | |
| 1038 goto err; | |
| 1039 else /* i == 0 */ | |
| 1040 { | |
| 1041 if (!s->session_creation_enabled) | |
| 1042 { | |
| 1043 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE
_FAILURE); | |
| 1044 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_SESSION
_MAY_NOT_BE_CREATED); | |
| 1045 goto err; | |
| 1046 } | |
| 1047 if (!ssl_get_new_session(s,1)) | |
| 1048 goto err; | |
| 1049 } | |
| 1050 } | |
| 1051 | |
| 1052 p+=j; | |
| 1053 | |
| 1054 if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) | |
| 1055 { | |
| 1056 /* cookie stuff */ | |
| 1057 cookie_len = *(p++); | |
| 1058 | |
| 1059 /* | |
| 1060 * The ClientHello may contain a cookie even if the | |
| 1061 * HelloVerify message has not been sent--make sure that it | |
| 1062 * does not cause an overflow. | |
| 1063 */ | |
| 1064 if ( cookie_len > sizeof(s->d1->rcvd_cookie)) | |
| 1065 { | |
| 1066 /* too much data */ | |
| 1067 al = SSL_AD_DECODE_ERROR; | |
| 1068 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATC
H); | |
| 1069 goto f_err; | |
| 1070 } | |
| 1071 | |
| 1072 /* verify the cookie if appropriate option is set. */ | |
| 1073 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && | |
| 1074 cookie_len > 0) | |
| 1075 { | |
| 1076 memcpy(s->d1->rcvd_cookie, p, cookie_len); | |
| 1077 | |
| 1078 if ( s->ctx->app_verify_cookie_cb != NULL) | |
| 1079 { | |
| 1080 if ( s->ctx->app_verify_cookie_cb(s, s->d1->rcvd
_cookie, | |
| 1081 cookie_len) == 0) | |
| 1082 { | |
| 1083 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 1084 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, | |
| 1085 SSL_R_COOKIE_MISMATCH); | |
| 1086 goto f_err; | |
| 1087 } | |
| 1088 /* else cookie verification succeeded */ | |
| 1089 } | |
| 1090 else if ( memcmp(s->d1->rcvd_cookie, s->d1->cookie, | |
| 1091 s->d1->cookie_len) != 0) /* de
fault verification */ | |
| 1092 { | |
| 1093 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 1094 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, | |
| 1095 SSL_R_COOKIE_MISMATCH); | |
| 1096 goto f_err; | |
| 1097 } | |
| 1098 | |
| 1099 ret = 2; | |
| 1100 } | |
| 1101 | |
| 1102 p += cookie_len; | |
| 1103 } | |
| 1104 | |
| 1105 n2s(p,i); | |
| 1106 if ((i == 0) && (j != 0)) | |
| 1107 { | |
| 1108 /* we need a cipher if we are not resuming a session */ | |
| 1109 al=SSL_AD_ILLEGAL_PARAMETER; | |
| 1110 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED); | |
| 1111 goto f_err; | |
| 1112 } | |
| 1113 if ((p+i) >= (d+n)) | |
| 1114 { | |
| 1115 /* not enough data */ | |
| 1116 al=SSL_AD_DECODE_ERROR; | |
| 1117 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH); | |
| 1118 goto f_err; | |
| 1119 } | |
| 1120 if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers)) | |
| 1121 == NULL)) | |
| 1122 { | |
| 1123 goto err; | |
| 1124 } | |
| 1125 p+=i; | |
| 1126 | |
| 1127 /* If it is a hit, check that the cipher is in the list */ | |
| 1128 if ((s->hit) && (i > 0)) | |
| 1129 { | |
| 1130 j=0; | |
| 1131 id=s->session->cipher->id; | |
| 1132 | |
| 1133 #ifdef CIPHER_DEBUG | |
| 1134 printf("client sent %d ciphers\n",sk_num(ciphers)); | |
| 1135 #endif | |
| 1136 for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++) | |
| 1137 { | |
| 1138 c=sk_SSL_CIPHER_value(ciphers,i); | |
| 1139 #ifdef CIPHER_DEBUG | |
| 1140 printf("client [%2d of %2d]:%s\n", | |
| 1141 i,sk_num(ciphers),SSL_CIPHER_get_name(c)); | |
| 1142 #endif | |
| 1143 if (c->id == id) | |
| 1144 { | |
| 1145 j=1; | |
| 1146 break; | |
| 1147 } | |
| 1148 } | |
| 1149 /* Disabled because it can be used in a ciphersuite downgrade | |
| 1150 * attack: CVE-2010-4180. | |
| 1151 */ | |
| 1152 #if 0 | |
| 1153 if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_
BUG) && (sk_SSL_CIPHER_num(ciphers) == 1)) | |
| 1154 { | |
| 1155 /* Special case as client bug workaround: the previously
used cipher may | |
| 1156 * not be in the current list, the client instead might
be trying to | |
| 1157 * continue using a cipher that before wasn't chosen due
to server | |
| 1158 * preferences. We'll have to reject the connection if
the cipher is not | |
| 1159 * enabled, though. */ | |
| 1160 c = sk_SSL_CIPHER_value(ciphers, 0); | |
| 1161 if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0) | |
| 1162 { | |
| 1163 s->session->cipher = c; | |
| 1164 j = 1; | |
| 1165 } | |
| 1166 } | |
| 1167 #endif | |
| 1168 if (j == 0) | |
| 1169 { | |
| 1170 /* we need to have the cipher in the cipher | |
| 1171 * list if we are asked to reuse it */ | |
| 1172 al=SSL_AD_ILLEGAL_PARAMETER; | |
| 1173 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER
_MISSING); | |
| 1174 goto f_err; | |
| 1175 } | |
| 1176 } | |
| 1177 | |
| 1178 /* compression */ | |
| 1179 i= *(p++); | |
| 1180 if ((p+i) > (d+n)) | |
| 1181 { | |
| 1182 /* not enough data */ | |
| 1183 al=SSL_AD_DECODE_ERROR; | |
| 1184 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH); | |
| 1185 goto f_err; | |
| 1186 } | |
| 1187 q=p; | |
| 1188 for (j=0; j<i; j++) | |
| 1189 { | |
| 1190 if (p[j] == 0) break; | |
| 1191 } | |
| 1192 | |
| 1193 p+=i; | |
| 1194 if (j >= i) | |
| 1195 { | |
| 1196 /* no compress */ | |
| 1197 al=SSL_AD_DECODE_ERROR; | |
| 1198 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIE
D); | |
| 1199 goto f_err; | |
| 1200 } | |
| 1201 | |
| 1202 #ifndef OPENSSL_NO_TLSEXT | |
| 1203 /* TLS extensions*/ | |
| 1204 if (s->version >= SSL3_VERSION) | |
| 1205 { | |
| 1206 if (!ssl_parse_clienthello_tlsext(s,&p,d,n, &al)) | |
| 1207 { | |
| 1208 /* 'al' set by ssl_parse_clienthello_tlsext */ | |
| 1209 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_PARSE_TLSEXT); | |
| 1210 goto f_err; | |
| 1211 } | |
| 1212 } | |
| 1213 if (ssl_check_clienthello_tlsext_early(s) <= 0) { | |
| 1214 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLS
EXT); | |
| 1215 goto err; | |
| 1216 } | |
| 1217 | |
| 1218 /* Check if we want to use external pre-shared secret for this | |
| 1219 * handshake for not reused session only. We need to generate | |
| 1220 * server_random before calling tls_session_secret_cb in order to allow | |
| 1221 * SessionTicket processing to use it in key derivation. */ | |
| 1222 { | |
| 1223 unsigned long Time; | |
| 1224 unsigned char *pos; | |
| 1225 Time=(unsigned long)time(NULL); /* Time */ | |
| 1226 pos=s->s3->server_random; | |
| 1227 l2n(Time,pos); | |
| 1228 if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0) | |
| 1229 { | |
| 1230 al=SSL_AD_INTERNAL_ERROR; | |
| 1231 goto f_err; | |
| 1232 } | |
| 1233 } | |
| 1234 | |
| 1235 if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) | |
| 1236 { | |
| 1237 SSL_CIPHER *pref_cipher=NULL; | |
| 1238 | |
| 1239 s->session->master_key_length=sizeof(s->session->master_key); | |
| 1240 if(s->tls_session_secret_cb(s, s->session->master_key, &s->sessi
on->master_key_length, | |
| 1241 ciphers, &pref_cipher, s->tls_session_secret_cb_arg)) | |
| 1242 { | |
| 1243 s->hit=1; | |
| 1244 s->session->ciphers=ciphers; | |
| 1245 s->session->verify_result=X509_V_OK; | |
| 1246 | |
| 1247 ciphers=NULL; | |
| 1248 | |
| 1249 /* check if some cipher was preferred by call back */ | |
| 1250 pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_ciph
er(s, s->session->ciphers, SSL_get_ciphers(s)); | |
| 1251 if (pref_cipher == NULL) | |
| 1252 { | |
| 1253 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 1254 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHAR
ED_CIPHER); | |
| 1255 goto f_err; | |
| 1256 } | |
| 1257 | |
| 1258 s->session->cipher=pref_cipher; | |
| 1259 | |
| 1260 if (s->cipher_list) | |
| 1261 sk_SSL_CIPHER_free(s->cipher_list); | |
| 1262 | |
| 1263 if (s->cipher_list_by_id) | |
| 1264 sk_SSL_CIPHER_free(s->cipher_list_by_id); | |
| 1265 | |
| 1266 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers); | |
| 1267 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->cip
hers); | |
| 1268 } | |
| 1269 } | |
| 1270 #endif | |
| 1271 | |
| 1272 /* Worst case, we will use the NULL compression, but if we have other | |
| 1273 * options, we will now look for them. We have i-1 compression | |
| 1274 * algorithms from the client, starting at q. */ | |
| 1275 s->s3->tmp.new_compression=NULL; | |
| 1276 #ifndef OPENSSL_NO_COMP | |
| 1277 /* This only happens if we have a cache hit */ | |
| 1278 if (s->session->compress_meth != 0) | |
| 1279 { | |
| 1280 int m, comp_id = s->session->compress_meth; | |
| 1281 /* Perform sanity checks on resumed compression algorithm */ | |
| 1282 /* Can't disable compression */ | |
| 1283 if (s->options & SSL_OP_NO_COMPRESSION) | |
| 1284 { | |
| 1285 al=SSL_AD_INTERNAL_ERROR; | |
| 1286 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INCONSISTENT_CO
MPRESSION); | |
| 1287 goto f_err; | |
| 1288 } | |
| 1289 /* Look for resumed compression method */ | |
| 1290 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) | |
| 1291 { | |
| 1292 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m); | |
| 1293 if (comp_id == comp->id) | |
| 1294 { | |
| 1295 s->s3->tmp.new_compression=comp; | |
| 1296 break; | |
| 1297 } | |
| 1298 } | |
| 1299 if (s->s3->tmp.new_compression == NULL) | |
| 1300 { | |
| 1301 al=SSL_AD_INTERNAL_ERROR; | |
| 1302 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INVALID_COMPRES
SION_ALGORITHM); | |
| 1303 goto f_err; | |
| 1304 } | |
| 1305 /* Look for resumed method in compression list */ | |
| 1306 for (m = 0; m < i; m++) | |
| 1307 { | |
| 1308 if (q[m] == comp_id) | |
| 1309 break; | |
| 1310 } | |
| 1311 if (m >= i) | |
| 1312 { | |
| 1313 al=SSL_AD_ILLEGAL_PARAMETER; | |
| 1314 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_COMPRE
SSSION_ALGORITHM_MISSING); | |
| 1315 goto f_err; | |
| 1316 } | |
| 1317 } | |
| 1318 else if (s->hit) | |
| 1319 comp = NULL; | |
| 1320 else if (!(s->options & SSL_OP_NO_COMPRESSION) && s->ctx->comp_methods) | |
| 1321 { /* See if we have a match */ | |
| 1322 int m,nn,o,v,done=0; | |
| 1323 | |
| 1324 nn=sk_SSL_COMP_num(s->ctx->comp_methods); | |
| 1325 for (m=0; m<nn; m++) | |
| 1326 { | |
| 1327 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m); | |
| 1328 v=comp->id; | |
| 1329 for (o=0; o<i; o++) | |
| 1330 { | |
| 1331 if (v == q[o]) | |
| 1332 { | |
| 1333 done=1; | |
| 1334 break; | |
| 1335 } | |
| 1336 } | |
| 1337 if (done) break; | |
| 1338 } | |
| 1339 if (done) | |
| 1340 s->s3->tmp.new_compression=comp; | |
| 1341 else | |
| 1342 comp=NULL; | |
| 1343 } | |
| 1344 #else | |
| 1345 /* If compression is disabled we'd better not try to resume a session | |
| 1346 * using compression. | |
| 1347 */ | |
| 1348 if (s->session->compress_meth != 0) | |
| 1349 { | |
| 1350 al=SSL_AD_INTERNAL_ERROR; | |
| 1351 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INCONSISTENT_COMPRESSIO
N); | |
| 1352 goto f_err; | |
| 1353 } | |
| 1354 #endif | |
| 1355 | |
| 1356 /* Given s->session->ciphers and SSL_get_ciphers, we must | |
| 1357 * pick a cipher */ | |
| 1358 | |
| 1359 if (!s->hit) | |
| 1360 { | |
| 1361 #ifdef OPENSSL_NO_COMP | |
| 1362 s->session->compress_meth=0; | |
| 1363 #else | |
| 1364 s->session->compress_meth=(comp == NULL)?0:comp->id; | |
| 1365 #endif | |
| 1366 if (s->session->ciphers != NULL) | |
| 1367 sk_SSL_CIPHER_free(s->session->ciphers); | |
| 1368 s->session->ciphers=ciphers; | |
| 1369 if (ciphers == NULL) | |
| 1370 { | |
| 1371 al=SSL_AD_ILLEGAL_PARAMETER; | |
| 1372 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASS
ED); | |
| 1373 goto f_err; | |
| 1374 } | |
| 1375 ciphers=NULL; | |
| 1376 c=ssl3_choose_cipher(s,s->session->ciphers, | |
| 1377 SSL_get_ciphers(s)); | |
| 1378 | |
| 1379 if (c == NULL) | |
| 1380 { | |
| 1381 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 1382 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHE
R); | |
| 1383 goto f_err; | |
| 1384 } | |
| 1385 s->s3->tmp.new_cipher=c; | |
| 1386 } | |
| 1387 else | |
| 1388 { | |
| 1389 /* Session-id reuse */ | |
| 1390 #ifdef REUSE_CIPHER_BUG | |
| 1391 STACK_OF(SSL_CIPHER) *sk; | |
| 1392 SSL_CIPHER *nc=NULL; | |
| 1393 SSL_CIPHER *ec=NULL; | |
| 1394 | |
| 1395 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG) | |
| 1396 { | |
| 1397 sk=s->session->ciphers; | |
| 1398 for (i=0; i<sk_SSL_CIPHER_num(sk); i++) | |
| 1399 { | |
| 1400 c=sk_SSL_CIPHER_value(sk,i); | |
| 1401 if (c->algorithm_enc & SSL_eNULL) | |
| 1402 nc=c; | |
| 1403 if (SSL_C_IS_EXPORT(c)) | |
| 1404 ec=c; | |
| 1405 } | |
| 1406 if (nc != NULL) | |
| 1407 s->s3->tmp.new_cipher=nc; | |
| 1408 else if (ec != NULL) | |
| 1409 s->s3->tmp.new_cipher=ec; | |
| 1410 else | |
| 1411 s->s3->tmp.new_cipher=s->session->cipher; | |
| 1412 } | |
| 1413 else | |
| 1414 #endif | |
| 1415 s->s3->tmp.new_cipher=s->session->cipher; | |
| 1416 } | |
| 1417 | |
| 1418 if (TLS1_get_version(s) < TLS1_2_VERSION || !(s->verify_mode & SSL_VERIF
Y_PEER)) | |
| 1419 { | |
| 1420 if (!ssl3_digest_cached_records(s)) | |
| 1421 { | |
| 1422 al = SSL_AD_INTERNAL_ERROR; | |
| 1423 goto f_err; | |
| 1424 } | |
| 1425 } | |
| 1426 | |
| 1427 /* we now have the following setup. | |
| 1428 * client_random | |
| 1429 * cipher_list - our prefered list of ciphers | |
| 1430 * ciphers - the clients prefered list of ciphers | |
| 1431 * compression - basically ignored right now | |
| 1432 * ssl version is set - sslv3 | |
| 1433 * s->session - The ssl session has been setup. | |
| 1434 * s->hit - session reuse flag | |
| 1435 * s->tmp.new_cipher - the new cipher to use. | |
| 1436 */ | |
| 1437 | |
| 1438 /* Handles TLS extensions that we couldn't check earlier */ | |
| 1439 if (s->version >= SSL3_VERSION) | |
| 1440 { | |
| 1441 if (ssl_check_clienthello_tlsext_late(s) <= 0) | |
| 1442 { | |
| 1443 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TL
SEXT); | |
| 1444 goto err; | |
| 1445 } | |
| 1446 } | |
| 1447 | |
| 1448 if (ret < 0) ret=1; | |
| 1449 if (0) | |
| 1450 { | |
| 1451 f_err: | |
| 1452 ssl3_send_alert(s,SSL3_AL_FATAL,al); | |
| 1453 } | |
| 1454 err: | |
| 1455 if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers); | |
| 1456 return(ret); | |
| 1457 } | |
| 1458 | |
| 1459 int ssl3_send_server_hello(SSL *s) | |
| 1460 { | |
| 1461 unsigned char *buf; | |
| 1462 unsigned char *p,*d; | |
| 1463 int i,sl; | |
| 1464 unsigned long l; | |
| 1465 #ifdef OPENSSL_NO_TLSEXT | |
| 1466 unsigned long Time; | |
| 1467 #endif | |
| 1468 | |
| 1469 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) | |
| 1470 { | |
| 1471 buf=(unsigned char *)s->init_buf->data; | |
| 1472 #ifdef OPENSSL_NO_TLSEXT | |
| 1473 p=s->s3->server_random; | |
| 1474 /* Generate server_random if it was not needed previously */ | |
| 1475 Time=(unsigned long)time(NULL); /* Time */ | |
| 1476 l2n(Time,p); | |
| 1477 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0) | |
| 1478 return -1; | |
| 1479 #endif | |
| 1480 /* Do the message type and length last */ | |
| 1481 d=p= &(buf[4]); | |
| 1482 | |
| 1483 *(p++)=s->version>>8; | |
| 1484 *(p++)=s->version&0xff; | |
| 1485 | |
| 1486 /* Random stuff */ | |
| 1487 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); | |
| 1488 p+=SSL3_RANDOM_SIZE; | |
| 1489 | |
| 1490 /* There are several cases for the session ID to send | |
| 1491 * back in the server hello: | |
| 1492 * - For session reuse from the session cache, | |
| 1493 * we send back the old session ID. | |
| 1494 * - If stateless session reuse (using a session ticket) | |
| 1495 * is successful, we send back the client's "session ID" | |
| 1496 * (which doesn't actually identify the session). | |
| 1497 * - If it is a new session, we send back the new | |
| 1498 * session ID. | |
| 1499 * - However, if we want the new session to be single-use, | |
| 1500 * we send back a 0-length session ID. | |
| 1501 * s->hit is non-zero in either case of session reuse, | |
| 1502 * so the following won't overwrite an ID that we're supposed | |
| 1503 * to send back. | |
| 1504 */ | |
| 1505 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) | |
| 1506 && !s->hit) | |
| 1507 s->session->session_id_length=0; | |
| 1508 | |
| 1509 sl=s->session->session_id_length; | |
| 1510 if (sl > (int)sizeof(s->session->session_id)) | |
| 1511 { | |
| 1512 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERRO
R); | |
| 1513 return -1; | |
| 1514 } | |
| 1515 *(p++)=sl; | |
| 1516 memcpy(p,s->session->session_id,sl); | |
| 1517 p+=sl; | |
| 1518 | |
| 1519 /* put the cipher */ | |
| 1520 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p); | |
| 1521 p+=i; | |
| 1522 | |
| 1523 /* put the compression method */ | |
| 1524 #ifdef OPENSSL_NO_COMP | |
| 1525 *(p++)=0; | |
| 1526 #else | |
| 1527 if (s->s3->tmp.new_compression == NULL) | |
| 1528 *(p++)=0; | |
| 1529 else | |
| 1530 *(p++)=s->s3->tmp.new_compression->id; | |
| 1531 #endif | |
| 1532 #ifndef OPENSSL_NO_TLSEXT | |
| 1533 if (ssl_prepare_serverhello_tlsext(s) <= 0) | |
| 1534 { | |
| 1535 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,SSL_R_SERVERHELLO_TL
SEXT); | |
| 1536 return -1; | |
| 1537 } | |
| 1538 if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_
LENGTH)) == NULL) | |
| 1539 { | |
| 1540 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,ERR_R_INTERNAL_ERROR
); | |
| 1541 return -1; | |
| 1542 } | |
| 1543 #endif | |
| 1544 /* do the header */ | |
| 1545 l=(p-d); | |
| 1546 d=buf; | |
| 1547 *(d++)=SSL3_MT_SERVER_HELLO; | |
| 1548 l2n3(l,d); | |
| 1549 | |
| 1550 s->state=SSL3_ST_SW_SRVR_HELLO_B; | |
| 1551 /* number of bytes to write */ | |
| 1552 s->init_num=p-buf; | |
| 1553 s->init_off=0; | |
| 1554 } | |
| 1555 | |
| 1556 /* SSL3_ST_SW_SRVR_HELLO_B */ | |
| 1557 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | |
| 1558 } | |
| 1559 | |
| 1560 int ssl3_send_server_done(SSL *s) | |
| 1561 { | |
| 1562 unsigned char *p; | |
| 1563 | |
| 1564 if (s->state == SSL3_ST_SW_SRVR_DONE_A) | |
| 1565 { | |
| 1566 p=(unsigned char *)s->init_buf->data; | |
| 1567 | |
| 1568 /* do the header */ | |
| 1569 *(p++)=SSL3_MT_SERVER_DONE; | |
| 1570 *(p++)=0; | |
| 1571 *(p++)=0; | |
| 1572 *(p++)=0; | |
| 1573 | |
| 1574 s->state=SSL3_ST_SW_SRVR_DONE_B; | |
| 1575 /* number of bytes to write */ | |
| 1576 s->init_num=4; | |
| 1577 s->init_off=0; | |
| 1578 } | |
| 1579 | |
| 1580 /* SSL3_ST_SW_SRVR_DONE_B */ | |
| 1581 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | |
| 1582 } | |
| 1583 | |
| 1584 int ssl3_send_server_key_exchange(SSL *s) | |
| 1585 { | |
| 1586 #ifndef OPENSSL_NO_RSA | |
| 1587 unsigned char *q; | |
| 1588 int j,num; | |
| 1589 RSA *rsa; | |
| 1590 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; | |
| 1591 unsigned int u; | |
| 1592 #endif | |
| 1593 #ifndef OPENSSL_NO_DH | |
| 1594 DH *dh=NULL,*dhp; | |
| 1595 #endif | |
| 1596 #ifndef OPENSSL_NO_ECDH | |
| 1597 EC_KEY *ecdh=NULL, *ecdhp; | |
| 1598 unsigned char *encodedPoint = NULL; | |
| 1599 int encodedlen = 0; | |
| 1600 int curve_id = 0; | |
| 1601 BN_CTX *bn_ctx = NULL; | |
| 1602 #endif | |
| 1603 EVP_PKEY *pkey; | |
| 1604 const EVP_MD *md = NULL; | |
| 1605 unsigned char *p,*d; | |
| 1606 int al,i; | |
| 1607 unsigned long type; | |
| 1608 int n; | |
| 1609 CERT *cert; | |
| 1610 BIGNUM *r[4]; | |
| 1611 int nr[4],kn; | |
| 1612 BUF_MEM *buf; | |
| 1613 EVP_MD_CTX md_ctx; | |
| 1614 | |
| 1615 EVP_MD_CTX_init(&md_ctx); | |
| 1616 if (s->state == SSL3_ST_SW_KEY_EXCH_A) | |
| 1617 { | |
| 1618 type=s->s3->tmp.new_cipher->algorithm_mkey; | |
| 1619 cert=s->cert; | |
| 1620 | |
| 1621 buf=s->init_buf; | |
| 1622 | |
| 1623 r[0]=r[1]=r[2]=r[3]=NULL; | |
| 1624 n=0; | |
| 1625 #ifndef OPENSSL_NO_RSA | |
| 1626 if (type & SSL_kRSA) | |
| 1627 { | |
| 1628 rsa=cert->rsa_tmp; | |
| 1629 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) | |
| 1630 { | |
| 1631 rsa=s->cert->rsa_tmp_cb(s, | |
| 1632 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), | |
| 1633 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cip
her)); | |
| 1634 if(rsa == NULL) | |
| 1635 { | |
| 1636 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 1637 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHAN
GE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY); | |
| 1638 goto f_err; | |
| 1639 } | |
| 1640 RSA_up_ref(rsa); | |
| 1641 cert->rsa_tmp=rsa; | |
| 1642 } | |
| 1643 if (rsa == NULL) | |
| 1644 { | |
| 1645 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 1646 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R
_MISSING_TMP_RSA_KEY); | |
| 1647 goto f_err; | |
| 1648 } | |
| 1649 r[0]=rsa->n; | |
| 1650 r[1]=rsa->e; | |
| 1651 s->s3->tmp.use_rsa_tmp=1; | |
| 1652 } | |
| 1653 else | |
| 1654 #endif | |
| 1655 #ifndef OPENSSL_NO_DH | |
| 1656 if (type & SSL_kEDH) | |
| 1657 { | |
| 1658 dhp=cert->dh_tmp; | |
| 1659 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL)) | |
| 1660 dhp=s->cert->dh_tmp_cb(s, | |
| 1661 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), | |
| 1662 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cip
her)); | |
| 1663 if (dhp == NULL) | |
| 1664 { | |
| 1665 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 1666 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R
_MISSING_TMP_DH_KEY); | |
| 1667 goto f_err; | |
| 1668 } | |
| 1669 | |
| 1670 if (s->s3->tmp.dh != NULL) | |
| 1671 { | |
| 1672 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_
R_INTERNAL_ERROR); | |
| 1673 goto err; | |
| 1674 } | |
| 1675 | |
| 1676 if ((dh=DHparams_dup(dhp)) == NULL) | |
| 1677 { | |
| 1678 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R
_DH_LIB); | |
| 1679 goto err; | |
| 1680 } | |
| 1681 | |
| 1682 s->s3->tmp.dh=dh; | |
| 1683 if ((dhp->pub_key == NULL || | |
| 1684 dhp->priv_key == NULL || | |
| 1685 (s->options & SSL_OP_SINGLE_DH_USE))) | |
| 1686 { | |
| 1687 if(!DH_generate_key(dh)) | |
| 1688 { | |
| 1689 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | |
| 1690 ERR_R_DH_LIB); | |
| 1691 goto err; | |
| 1692 } | |
| 1693 } | |
| 1694 else | |
| 1695 { | |
| 1696 dh->pub_key=BN_dup(dhp->pub_key); | |
| 1697 dh->priv_key=BN_dup(dhp->priv_key); | |
| 1698 if ((dh->pub_key == NULL) || | |
| 1699 (dh->priv_key == NULL)) | |
| 1700 { | |
| 1701 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHAN
GE,ERR_R_DH_LIB); | |
| 1702 goto err; | |
| 1703 } | |
| 1704 } | |
| 1705 r[0]=dh->p; | |
| 1706 r[1]=dh->g; | |
| 1707 r[2]=dh->pub_key; | |
| 1708 } | |
| 1709 else | |
| 1710 #endif | |
| 1711 #ifndef OPENSSL_NO_ECDH | |
| 1712 if (type & SSL_kEECDH) | |
| 1713 { | |
| 1714 const EC_GROUP *group; | |
| 1715 | |
| 1716 ecdhp=cert->ecdh_tmp; | |
| 1717 if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL)) | |
| 1718 { | |
| 1719 ecdhp=s->cert->ecdh_tmp_cb(s, | |
| 1720 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), | |
| 1721 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cip
her)); | |
| 1722 } | |
| 1723 if (ecdhp == NULL) | |
| 1724 { | |
| 1725 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 1726 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R
_MISSING_TMP_ECDH_KEY); | |
| 1727 goto f_err; | |
| 1728 } | |
| 1729 | |
| 1730 if (s->s3->tmp.ecdh != NULL) | |
| 1731 { | |
| 1732 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_
R_INTERNAL_ERROR); | |
| 1733 goto err; | |
| 1734 } | |
| 1735 | |
| 1736 /* Duplicate the ECDH structure. */ | |
| 1737 if (ecdhp == NULL) | |
| 1738 { | |
| 1739 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R
_ECDH_LIB); | |
| 1740 goto err; | |
| 1741 } | |
| 1742 if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) | |
| 1743 { | |
| 1744 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R
_ECDH_LIB); | |
| 1745 goto err; | |
| 1746 } | |
| 1747 | |
| 1748 s->s3->tmp.ecdh=ecdh; | |
| 1749 if ((EC_KEY_get0_public_key(ecdh) == NULL) || | |
| 1750 (EC_KEY_get0_private_key(ecdh) == NULL) || | |
| 1751 (s->options & SSL_OP_SINGLE_ECDH_USE)) | |
| 1752 { | |
| 1753 if(!EC_KEY_generate_key(ecdh)) | |
| 1754 { | |
| 1755 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,E
RR_R_ECDH_LIB); | |
| 1756 goto err; | |
| 1757 } | |
| 1758 } | |
| 1759 | |
| 1760 if (((group = EC_KEY_get0_group(ecdh)) == NULL) || | |
| 1761 (EC_KEY_get0_public_key(ecdh) == NULL) || | |
| 1762 (EC_KEY_get0_private_key(ecdh) == NULL)) | |
| 1763 { | |
| 1764 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R
_ECDH_LIB); | |
| 1765 goto err; | |
| 1766 } | |
| 1767 | |
| 1768 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && | |
| 1769 (EC_GROUP_get_degree(group) > 163)) | |
| 1770 { | |
| 1771 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R
_ECGROUP_TOO_LARGE_FOR_CIPHER); | |
| 1772 goto err; | |
| 1773 } | |
| 1774 | |
| 1775 /* XXX: For now, we only support ephemeral ECDH | |
| 1776 * keys over named (not generic) curves. For | |
| 1777 * supported named curves, curve_id is non-zero. | |
| 1778 */ | |
| 1779 if ((curve_id = | |
| 1780 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group))
) | |
| 1781 == 0) | |
| 1782 { | |
| 1783 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R
_UNSUPPORTED_ELLIPTIC_CURVE); | |
| 1784 goto err; | |
| 1785 } | |
| 1786 | |
| 1787 /* Encode the public key. | |
| 1788 * First check the size of encoding and | |
| 1789 * allocate memory accordingly. | |
| 1790 */ | |
| 1791 encodedlen = EC_POINT_point2oct(group, | |
| 1792 EC_KEY_get0_public_key(ecdh), | |
| 1793 POINT_CONVERSION_UNCOMPRESSED, | |
| 1794 NULL, 0, NULL); | |
| 1795 | |
| 1796 encodedPoint = (unsigned char *) | |
| 1797 OPENSSL_malloc(encodedlen*sizeof(unsigned char)); | |
| 1798 bn_ctx = BN_CTX_new(); | |
| 1799 if ((encodedPoint == NULL) || (bn_ctx == NULL)) | |
| 1800 { | |
| 1801 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R
_MALLOC_FAILURE); | |
| 1802 goto err; | |
| 1803 } | |
| 1804 | |
| 1805 | |
| 1806 encodedlen = EC_POINT_point2oct(group, | |
| 1807 EC_KEY_get0_public_key(ecdh), | |
| 1808 POINT_CONVERSION_UNCOMPRESSED, | |
| 1809 encodedPoint, encodedlen, bn_ctx); | |
| 1810 | |
| 1811 if (encodedlen == 0) | |
| 1812 { | |
| 1813 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R
_ECDH_LIB); | |
| 1814 goto err; | |
| 1815 } | |
| 1816 | |
| 1817 BN_CTX_free(bn_ctx); bn_ctx=NULL; | |
| 1818 | |
| 1819 /* XXX: For now, we only support named (not | |
| 1820 * generic) curves in ECDH ephemeral key exchanges. | |
| 1821 * In this situation, we need four additional bytes | |
| 1822 * to encode the entire ServerECDHParams | |
| 1823 * structure. | |
| 1824 */ | |
| 1825 n = 4 + encodedlen; | |
| 1826 | |
| 1827 /* We'll generate the serverKeyExchange message | |
| 1828 * explicitly so we can set these to NULLs | |
| 1829 */ | |
| 1830 r[0]=NULL; | |
| 1831 r[1]=NULL; | |
| 1832 r[2]=NULL; | |
| 1833 r[3]=NULL; | |
| 1834 } | |
| 1835 else | |
| 1836 #endif /* !OPENSSL_NO_ECDH */ | |
| 1837 #ifndef OPENSSL_NO_PSK | |
| 1838 if (type & SSL_kPSK) | |
| 1839 { | |
| 1840 /* reserve size for record length and PSK identi
ty hint*/ | |
| 1841 n+=2+strlen(s->ctx->psk_identity_hint); | |
| 1842 } | |
| 1843 else | |
| 1844 #endif /* !OPENSSL_NO_PSK */ | |
| 1845 #ifndef OPENSSL_NO_SRP | |
| 1846 if (type & SSL_kSRP) | |
| 1847 { | |
| 1848 if ((s->srp_ctx.N == NULL) || | |
| 1849 (s->srp_ctx.g == NULL) || | |
| 1850 (s->srp_ctx.s == NULL) || | |
| 1851 (s->srp_ctx.B == NULL)) | |
| 1852 { | |
| 1853 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R
_MISSING_SRP_PARAM); | |
| 1854 goto err; | |
| 1855 } | |
| 1856 r[0]=s->srp_ctx.N; | |
| 1857 r[1]=s->srp_ctx.g; | |
| 1858 r[2]=s->srp_ctx.s; | |
| 1859 r[3]=s->srp_ctx.B; | |
| 1860 } | |
| 1861 else | |
| 1862 #endif | |
| 1863 { | |
| 1864 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 1865 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN
_KEY_EXCHANGE_TYPE); | |
| 1866 goto f_err; | |
| 1867 } | |
| 1868 for (i=0; r[i] != NULL && i<4; i++) | |
| 1869 { | |
| 1870 nr[i]=BN_num_bytes(r[i]); | |
| 1871 #ifndef OPENSSL_NO_SRP | |
| 1872 if ((i == 2) && (type & SSL_kSRP)) | |
| 1873 n+=1+nr[i]; | |
| 1874 else | |
| 1875 #endif | |
| 1876 n+=2+nr[i]; | |
| 1877 } | |
| 1878 | |
| 1879 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) | |
| 1880 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) | |
| 1881 { | |
| 1882 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher,&md)
) | |
| 1883 == NULL) | |
| 1884 { | |
| 1885 al=SSL_AD_DECODE_ERROR; | |
| 1886 goto f_err; | |
| 1887 } | |
| 1888 kn=EVP_PKEY_size(pkey); | |
| 1889 } | |
| 1890 else | |
| 1891 { | |
| 1892 pkey=NULL; | |
| 1893 kn=0; | |
| 1894 } | |
| 1895 | |
| 1896 if (!BUF_MEM_grow_clean(buf,n+4+kn)) | |
| 1897 { | |
| 1898 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF); | |
| 1899 goto err; | |
| 1900 } | |
| 1901 d=(unsigned char *)s->init_buf->data; | |
| 1902 p= &(d[4]); | |
| 1903 | |
| 1904 for (i=0; r[i] != NULL && i<4; i++) | |
| 1905 { | |
| 1906 #ifndef OPENSSL_NO_SRP | |
| 1907 if ((i == 2) && (type & SSL_kSRP)) | |
| 1908 { | |
| 1909 *p = nr[i]; | |
| 1910 p++; | |
| 1911 } | |
| 1912 else | |
| 1913 #endif | |
| 1914 s2n(nr[i],p); | |
| 1915 BN_bn2bin(r[i],p); | |
| 1916 p+=nr[i]; | |
| 1917 } | |
| 1918 | |
| 1919 #ifndef OPENSSL_NO_ECDH | |
| 1920 if (type & SSL_kEECDH) | |
| 1921 { | |
| 1922 /* XXX: For now, we only support named (not generic) cur
ves. | |
| 1923 * In this situation, the serverKeyExchange message has: | |
| 1924 * [1 byte CurveType], [2 byte CurveName] | |
| 1925 * [1 byte length of encoded point], followed by | |
| 1926 * the actual encoded point itself | |
| 1927 */ | |
| 1928 *p = NAMED_CURVE_TYPE; | |
| 1929 p += 1; | |
| 1930 *p = 0; | |
| 1931 p += 1; | |
| 1932 *p = curve_id; | |
| 1933 p += 1; | |
| 1934 *p = encodedlen; | |
| 1935 p += 1; | |
| 1936 memcpy((unsigned char*)p, | |
| 1937 (unsigned char *)encodedPoint, | |
| 1938 encodedlen); | |
| 1939 OPENSSL_free(encodedPoint); | |
| 1940 encodedPoint = NULL; | |
| 1941 p += encodedlen; | |
| 1942 } | |
| 1943 #endif | |
| 1944 | |
| 1945 #ifndef OPENSSL_NO_PSK | |
| 1946 if (type & SSL_kPSK) | |
| 1947 { | |
| 1948 /* copy PSK identity hint */ | |
| 1949 s2n(strlen(s->ctx->psk_identity_hint), p); | |
| 1950 strncpy((char *)p, s->ctx->psk_identity_hint, strlen(s->
ctx->psk_identity_hint)); | |
| 1951 p+=strlen(s->ctx->psk_identity_hint); | |
| 1952 } | |
| 1953 #endif | |
| 1954 | |
| 1955 /* not anonymous */ | |
| 1956 if (pkey != NULL) | |
| 1957 { | |
| 1958 /* n is the length of the params, they start at &(d[4]) | |
| 1959 * and p points to the space at the end. */ | |
| 1960 #ifndef OPENSSL_NO_RSA | |
| 1961 if (pkey->type == EVP_PKEY_RSA | |
| 1962 && TLS1_get_version(s) < TLS1_2_VERSION) | |
| 1963 { | |
| 1964 q=md_buf; | |
| 1965 j=0; | |
| 1966 for (num=2; num > 0; num--) | |
| 1967 { | |
| 1968 EVP_MD_CTX_set_flags(&md_ctx, | |
| 1969 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); | |
| 1970 EVP_DigestInit_ex(&md_ctx,(num == 2) | |
| 1971 ?s->ctx->md5:s->ctx->sha1, NULL)
; | |
| 1972 EVP_DigestUpdate(&md_ctx,&(s->s3->client
_random[0]),SSL3_RANDOM_SIZE); | |
| 1973 EVP_DigestUpdate(&md_ctx,&(s->s3->server
_random[0]),SSL3_RANDOM_SIZE); | |
| 1974 EVP_DigestUpdate(&md_ctx,&(d[4]),n); | |
| 1975 EVP_DigestFinal_ex(&md_ctx,q, | |
| 1976 (unsigned int *)&i); | |
| 1977 q+=i; | |
| 1978 j+=i; | |
| 1979 } | |
| 1980 if (RSA_sign(NID_md5_sha1, md_buf, j, | |
| 1981 &(p[2]), &u, pkey->pkey.rsa) <= 0) | |
| 1982 { | |
| 1983 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHAN
GE,ERR_LIB_RSA); | |
| 1984 goto err; | |
| 1985 } | |
| 1986 s2n(u,p); | |
| 1987 n+=u+2; | |
| 1988 } | |
| 1989 else | |
| 1990 #endif | |
| 1991 if (md) | |
| 1992 { | |
| 1993 /* For TLS1.2 and later send signature | |
| 1994 * algorithm */ | |
| 1995 if (TLS1_get_version(s) >= TLS1_2_VERSION) | |
| 1996 { | |
| 1997 if (!tls12_get_sigandhash(p, pkey, md)) | |
| 1998 { | |
| 1999 /* Should never happen */ | |
| 2000 al=SSL_AD_INTERNAL_ERROR; | |
| 2001 SSLerr(SSL_F_SSL3_SEND_SERVER_KE
Y_EXCHANGE,ERR_R_INTERNAL_ERROR); | |
| 2002 goto f_err; | |
| 2003 } | |
| 2004 p+=2; | |
| 2005 } | |
| 2006 #ifdef SSL_DEBUG | |
| 2007 fprintf(stderr, "Using hash %s\n", | |
| 2008 EVP_MD_name(md)); | |
| 2009 #endif | |
| 2010 EVP_SignInit_ex(&md_ctx, md, NULL); | |
| 2011 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]
),SSL3_RANDOM_SIZE); | |
| 2012 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]
),SSL3_RANDOM_SIZE); | |
| 2013 EVP_SignUpdate(&md_ctx,&(d[4]),n); | |
| 2014 if (!EVP_SignFinal(&md_ctx,&(p[2]), | |
| 2015 (unsigned int *)&i,pkey)) | |
| 2016 { | |
| 2017 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHAN
GE,ERR_LIB_EVP); | |
| 2018 goto err; | |
| 2019 } | |
| 2020 s2n(i,p); | |
| 2021 n+=i+2; | |
| 2022 if (TLS1_get_version(s) >= TLS1_2_VERSION) | |
| 2023 n+= 2; | |
| 2024 } | |
| 2025 else | |
| 2026 { | |
| 2027 /* Is this error check actually needed? */ | |
| 2028 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 2029 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R
_UNKNOWN_PKEY_TYPE); | |
| 2030 goto f_err; | |
| 2031 } | |
| 2032 } | |
| 2033 | |
| 2034 *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE; | |
| 2035 l2n3(n,d); | |
| 2036 | |
| 2037 /* we should now have things packed up, so lets send | |
| 2038 * it off */ | |
| 2039 s->init_num=n+4; | |
| 2040 s->init_off=0; | |
| 2041 } | |
| 2042 | |
| 2043 s->state = SSL3_ST_SW_KEY_EXCH_B; | |
| 2044 EVP_MD_CTX_cleanup(&md_ctx); | |
| 2045 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | |
| 2046 f_err: | |
| 2047 ssl3_send_alert(s,SSL3_AL_FATAL,al); | |
| 2048 err: | |
| 2049 #ifndef OPENSSL_NO_ECDH | |
| 2050 if (encodedPoint != NULL) OPENSSL_free(encodedPoint); | |
| 2051 BN_CTX_free(bn_ctx); | |
| 2052 #endif | |
| 2053 EVP_MD_CTX_cleanup(&md_ctx); | |
| 2054 return(-1); | |
| 2055 } | |
| 2056 | |
| 2057 int ssl3_send_certificate_request(SSL *s) | |
| 2058 { | |
| 2059 unsigned char *p,*d; | |
| 2060 int i,j,nl,off,n; | |
| 2061 STACK_OF(X509_NAME) *sk=NULL; | |
| 2062 X509_NAME *name; | |
| 2063 BUF_MEM *buf; | |
| 2064 | |
| 2065 if (s->state == SSL3_ST_SW_CERT_REQ_A) | |
| 2066 { | |
| 2067 buf=s->init_buf; | |
| 2068 | |
| 2069 d=p=(unsigned char *)&(buf->data[4]); | |
| 2070 | |
| 2071 /* get the list of acceptable cert types */ | |
| 2072 p++; | |
| 2073 n=ssl3_get_req_cert_type(s,p); | |
| 2074 d[0]=n; | |
| 2075 p+=n; | |
| 2076 n++; | |
| 2077 | |
| 2078 if (TLS1_get_version(s) >= TLS1_2_VERSION) | |
| 2079 { | |
| 2080 nl = tls12_get_req_sig_algs(s, p + 2); | |
| 2081 s2n(nl, p); | |
| 2082 p += nl + 2; | |
| 2083 n += nl + 2; | |
| 2084 } | |
| 2085 | |
| 2086 off=n; | |
| 2087 p+=2; | |
| 2088 n+=2; | |
| 2089 | |
| 2090 sk=SSL_get_client_CA_list(s); | |
| 2091 nl=0; | |
| 2092 if (sk != NULL) | |
| 2093 { | |
| 2094 for (i=0; i<sk_X509_NAME_num(sk); i++) | |
| 2095 { | |
| 2096 name=sk_X509_NAME_value(sk,i); | |
| 2097 j=i2d_X509_NAME(name,NULL); | |
| 2098 if (!BUF_MEM_grow_clean(buf,4+n+j+2)) | |
| 2099 { | |
| 2100 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUE
ST,ERR_R_BUF_LIB); | |
| 2101 goto err; | |
| 2102 } | |
| 2103 p=(unsigned char *)&(buf->data[4+n]); | |
| 2104 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) | |
| 2105 { | |
| 2106 s2n(j,p); | |
| 2107 i2d_X509_NAME(name,&p); | |
| 2108 n+=2+j; | |
| 2109 nl+=2+j; | |
| 2110 } | |
| 2111 else | |
| 2112 { | |
| 2113 d=p; | |
| 2114 i2d_X509_NAME(name,&p); | |
| 2115 j-=2; s2n(j,d); j+=2; | |
| 2116 n+=j; | |
| 2117 nl+=j; | |
| 2118 } | |
| 2119 } | |
| 2120 } | |
| 2121 /* else no CA names */ | |
| 2122 p=(unsigned char *)&(buf->data[4+off]); | |
| 2123 s2n(nl,p); | |
| 2124 | |
| 2125 d=(unsigned char *)buf->data; | |
| 2126 *(d++)=SSL3_MT_CERTIFICATE_REQUEST; | |
| 2127 l2n3(n,d); | |
| 2128 | |
| 2129 /* we should now have things packed up, so lets send | |
| 2130 * it off */ | |
| 2131 | |
| 2132 s->init_num=n+4; | |
| 2133 s->init_off=0; | |
| 2134 #ifdef NETSCAPE_HANG_BUG | |
| 2135 p=(unsigned char *)s->init_buf->data + s->init_num; | |
| 2136 | |
| 2137 /* do the header */ | |
| 2138 *(p++)=SSL3_MT_SERVER_DONE; | |
| 2139 *(p++)=0; | |
| 2140 *(p++)=0; | |
| 2141 *(p++)=0; | |
| 2142 s->init_num += 4; | |
| 2143 #endif | |
| 2144 | |
| 2145 s->state = SSL3_ST_SW_CERT_REQ_B; | |
| 2146 } | |
| 2147 | |
| 2148 /* SSL3_ST_SW_CERT_REQ_B */ | |
| 2149 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | |
| 2150 err: | |
| 2151 return(-1); | |
| 2152 } | |
| 2153 | |
| 2154 int ssl3_get_client_key_exchange(SSL *s) | |
| 2155 { | |
| 2156 int i,al,ok; | |
| 2157 long n; | |
| 2158 unsigned long alg_k; | |
| 2159 unsigned char *p; | |
| 2160 #ifndef OPENSSL_NO_RSA | |
| 2161 RSA *rsa=NULL; | |
| 2162 EVP_PKEY *pkey=NULL; | |
| 2163 #endif | |
| 2164 #ifndef OPENSSL_NO_DH | |
| 2165 BIGNUM *pub=NULL; | |
| 2166 DH *dh_srvr; | |
| 2167 #endif | |
| 2168 #ifndef OPENSSL_NO_KRB5 | |
| 2169 KSSL_ERR kssl_err; | |
| 2170 #endif /* OPENSSL_NO_KRB5 */ | |
| 2171 | |
| 2172 #ifndef OPENSSL_NO_ECDH | |
| 2173 EC_KEY *srvr_ecdh = NULL; | |
| 2174 EVP_PKEY *clnt_pub_pkey = NULL; | |
| 2175 EC_POINT *clnt_ecpoint = NULL; | |
| 2176 BN_CTX *bn_ctx = NULL; | |
| 2177 #endif | |
| 2178 | |
| 2179 n=s->method->ssl_get_message(s, | |
| 2180 SSL3_ST_SR_KEY_EXCH_A, | |
| 2181 SSL3_ST_SR_KEY_EXCH_B, | |
| 2182 SSL3_MT_CLIENT_KEY_EXCHANGE, | |
| 2183 2048, /* ??? */ | |
| 2184 &ok); | |
| 2185 | |
| 2186 if (!ok) return((int)n); | |
| 2187 p=(unsigned char *)s->init_msg; | |
| 2188 | |
| 2189 alg_k=s->s3->tmp.new_cipher->algorithm_mkey; | |
| 2190 | |
| 2191 #ifndef OPENSSL_NO_RSA | |
| 2192 if (alg_k & SSL_kRSA) | |
| 2193 { | |
| 2194 /* FIX THIS UP EAY EAY EAY EAY */ | |
| 2195 if (s->s3->tmp.use_rsa_tmp) | |
| 2196 { | |
| 2197 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL)) | |
| 2198 rsa=s->cert->rsa_tmp; | |
| 2199 /* Don't do a callback because rsa_tmp should | |
| 2200 * be sent already */ | |
| 2201 if (rsa == NULL) | |
| 2202 { | |
| 2203 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 2204 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_
MISSING_TMP_RSA_PKEY); | |
| 2205 goto f_err; | |
| 2206 | |
| 2207 } | |
| 2208 } | |
| 2209 else | |
| 2210 { | |
| 2211 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey; | |
| 2212 if ( (pkey == NULL) || | |
| 2213 (pkey->type != EVP_PKEY_RSA) || | |
| 2214 (pkey->pkey.rsa == NULL)) | |
| 2215 { | |
| 2216 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 2217 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_
MISSING_RSA_CERTIFICATE); | |
| 2218 goto f_err; | |
| 2219 } | |
| 2220 rsa=pkey->pkey.rsa; | |
| 2221 } | |
| 2222 | |
| 2223 /* TLS and [incidentally] DTLS{0xFEFF} */ | |
| 2224 if (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER) | |
| 2225 { | |
| 2226 n2s(p,i); | |
| 2227 if (n != i+2) | |
| 2228 { | |
| 2229 if (!(s->options & SSL_OP_TLS_D5_BUG)) | |
| 2230 { | |
| 2231 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANG
E,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); | |
| 2232 goto err; | |
| 2233 } | |
| 2234 else | |
| 2235 p-=2; | |
| 2236 } | |
| 2237 else | |
| 2238 n=i; | |
| 2239 } | |
| 2240 | |
| 2241 i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING); | |
| 2242 | |
| 2243 al = -1; | |
| 2244 | |
| 2245 if (i != SSL_MAX_MASTER_KEY_LENGTH) | |
| 2246 { | |
| 2247 al=SSL_AD_DECODE_ERROR; | |
| 2248 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_R
SA_DECRYPT); */ | |
| 2249 } | |
| 2250 | |
| 2251 if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] ==
(s->client_version & 0xff)))) | |
| 2252 { | |
| 2253 /* The premaster secret must contain the same version nu
mber as the | |
| 2254 * ClientHello to detect version rollback attacks (stran
gely, the | |
| 2255 * protocol does not offer such protection for DH cipher
suites). | |
| 2256 * However, buggy clients exist that send the negotiated
protocol | |
| 2257 * version instead if the server does not support the re
quested | |
| 2258 * protocol version. | |
| 2259 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clie
nts. */ | |
| 2260 if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) && | |
| 2261 (p[0] == (s->version>>8)) && (p[1] == (s->versio
n & 0xff)))) | |
| 2262 { | |
| 2263 al=SSL_AD_DECODE_ERROR; | |
| 2264 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL
_R_BAD_PROTOCOL_VERSION_NUMBER); */ | |
| 2265 | |
| 2266 /* The Klima-Pokorny-Rosa extension of Bleichenb
acher's attack | |
| 2267 * (http://eprint.iacr.org/2003/052/) exploits t
he version | |
| 2268 * number check as a "bad version oracle" -- an
alert would | |
| 2269 * reveal that the plaintext corresponding to so
me ciphertext | |
| 2270 * made up by the adversary is properly formatte
d except | |
| 2271 * that the version number is wrong. To avoid s
uch attacks, | |
| 2272 * we should treat this just like any other decr
yption error. */ | |
| 2273 } | |
| 2274 } | |
| 2275 | |
| 2276 if (al != -1) | |
| 2277 { | |
| 2278 /* Some decryption failure -- use random value instead a
s countermeasure | |
| 2279 * against Bleichenbacher's attack on PKCS #1 v1.5 RSA p
adding | |
| 2280 * (see RFC 2246, section 7.4.7.1). */ | |
| 2281 ERR_clear_error(); | |
| 2282 i = SSL_MAX_MASTER_KEY_LENGTH; | |
| 2283 p[0] = s->client_version >> 8; | |
| 2284 p[1] = s->client_version & 0xff; | |
| 2285 if (RAND_pseudo_bytes(p+2, i-2) <= 0) /* should be RAND_
bytes, but we cannot work around a failure */ | |
| 2286 goto err; | |
| 2287 } | |
| 2288 | |
| 2289 s->session->master_key_length= | |
| 2290 s->method->ssl3_enc->generate_master_secret(s, | |
| 2291 s->session->master_key, | |
| 2292 p,i); | |
| 2293 OPENSSL_cleanse(p,i); | |
| 2294 } | |
| 2295 else | |
| 2296 #endif | |
| 2297 #ifndef OPENSSL_NO_DH | |
| 2298 if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) | |
| 2299 { | |
| 2300 n2s(p,i); | |
| 2301 if (n != i+2) | |
| 2302 { | |
| 2303 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) | |
| 2304 { | |
| 2305 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_
DH_PUBLIC_VALUE_LENGTH_IS_WRONG); | |
| 2306 goto err; | |
| 2307 } | |
| 2308 else | |
| 2309 { | |
| 2310 p-=2; | |
| 2311 i=(int)n; | |
| 2312 } | |
| 2313 } | |
| 2314 | |
| 2315 if (n == 0L) /* the parameters are in the cert */ | |
| 2316 { | |
| 2317 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 2318 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_T
O_DECODE_DH_CERTS); | |
| 2319 goto f_err; | |
| 2320 } | |
| 2321 else | |
| 2322 { | |
| 2323 if (s->s3->tmp.dh == NULL) | |
| 2324 { | |
| 2325 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 2326 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_
MISSING_TMP_DH_KEY); | |
| 2327 goto f_err; | |
| 2328 } | |
| 2329 else | |
| 2330 dh_srvr=s->s3->tmp.dh; | |
| 2331 } | |
| 2332 | |
| 2333 pub=BN_bin2bn(p,i,NULL); | |
| 2334 if (pub == NULL) | |
| 2335 { | |
| 2336 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB); | |
| 2337 goto err; | |
| 2338 } | |
| 2339 | |
| 2340 i=DH_compute_key(p,pub,dh_srvr); | |
| 2341 | |
| 2342 if (i <= 0) | |
| 2343 { | |
| 2344 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); | |
| 2345 BN_clear_free(pub); | |
| 2346 goto err; | |
| 2347 } | |
| 2348 | |
| 2349 DH_free(s->s3->tmp.dh); | |
| 2350 s->s3->tmp.dh=NULL; | |
| 2351 | |
| 2352 BN_clear_free(pub); | |
| 2353 pub=NULL; | |
| 2354 s->session->master_key_length= | |
| 2355 s->method->ssl3_enc->generate_master_secret(s, | |
| 2356 s->session->master_key,p,i); | |
| 2357 OPENSSL_cleanse(p,i); | |
| 2358 } | |
| 2359 else | |
| 2360 #endif | |
| 2361 #ifndef OPENSSL_NO_KRB5 | |
| 2362 if (alg_k & SSL_kKRB5) | |
| 2363 { | |
| 2364 krb5_error_code krb5rc; | |
| 2365 krb5_data enc_ticket; | |
| 2366 krb5_data authenticator; | |
| 2367 krb5_data enc_pms; | |
| 2368 KSSL_CTX *kssl_ctx = s->kssl_ctx; | |
| 2369 EVP_CIPHER_CTX ciph_ctx; | |
| 2370 const EVP_CIPHER *enc = NULL; | |
| 2371 unsigned char iv[EVP_MAX_IV_LENGTH]; | |
| 2372 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH | |
| 2373 + EVP_MAX_BLOCK_LENGTH]; | |
| 2374 int padl, outl; | |
| 2375 krb5_timestamp authtime = 0; | |
| 2376 krb5_ticket_times ttimes; | |
| 2377 | |
| 2378 EVP_CIPHER_CTX_init(&ciph_ctx); | |
| 2379 | |
| 2380 if (!kssl_ctx) kssl_ctx = kssl_ctx_new(); | |
| 2381 | |
| 2382 n2s(p,i); | |
| 2383 enc_ticket.length = i; | |
| 2384 | |
| 2385 if (n < (long)(enc_ticket.length + 6)) | |
| 2386 { | |
| 2387 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2388 SSL_R_DATA_LENGTH_TOO_LONG); | |
| 2389 goto err; | |
| 2390 } | |
| 2391 | |
| 2392 enc_ticket.data = (char *)p; | |
| 2393 p+=enc_ticket.length; | |
| 2394 | |
| 2395 n2s(p,i); | |
| 2396 authenticator.length = i; | |
| 2397 | |
| 2398 if (n < (long)(enc_ticket.length + authenticator.length + 6)) | |
| 2399 { | |
| 2400 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2401 SSL_R_DATA_LENGTH_TOO_LONG); | |
| 2402 goto err; | |
| 2403 } | |
| 2404 | |
| 2405 authenticator.data = (char *)p; | |
| 2406 p+=authenticator.length; | |
| 2407 | |
| 2408 n2s(p,i); | |
| 2409 enc_pms.length = i; | |
| 2410 enc_pms.data = (char *)p; | |
| 2411 p+=enc_pms.length; | |
| 2412 | |
| 2413 /* Note that the length is checked again below, | |
| 2414 ** after decryption | |
| 2415 */ | |
| 2416 if(enc_pms.length > sizeof pms) | |
| 2417 { | |
| 2418 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2419 SSL_R_DATA_LENGTH_TOO_LONG); | |
| 2420 goto err; | |
| 2421 } | |
| 2422 | |
| 2423 if (n != (long)(enc_ticket.length + authenticator.length + | |
| 2424 enc_pms.length + 6)) | |
| 2425 { | |
| 2426 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2427 SSL_R_DATA_LENGTH_TOO_LONG); | |
| 2428 goto err; | |
| 2429 } | |
| 2430 | |
| 2431 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes, | |
| 2432 &kssl_err)) != 0) | |
| 2433 { | |
| 2434 #ifdef KSSL_DEBUG | |
| 2435 printf("kssl_sget_tkt rtn %d [%d]\n", | |
| 2436 krb5rc, kssl_err.reason); | |
| 2437 if (kssl_err.text) | |
| 2438 printf("kssl_err text= %s\n", kssl_err.text); | |
| 2439 #endif /* KSSL_DEBUG */ | |
| 2440 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2441 kssl_err.reason); | |
| 2442 goto err; | |
| 2443 } | |
| 2444 | |
| 2445 /* Note: no authenticator is not considered an error, | |
| 2446 ** but will return authtime == 0. | |
| 2447 */ | |
| 2448 if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator, | |
| 2449 &authtime, &kssl_err)) != 0) | |
| 2450 { | |
| 2451 #ifdef KSSL_DEBUG | |
| 2452 printf("kssl_check_authent rtn %d [%d]\n", | |
| 2453 krb5rc, kssl_err.reason); | |
| 2454 if (kssl_err.text) | |
| 2455 printf("kssl_err text= %s\n", kssl_err.text); | |
| 2456 #endif /* KSSL_DEBUG */ | |
| 2457 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2458 kssl_err.reason); | |
| 2459 goto err; | |
| 2460 } | |
| 2461 | |
| 2462 if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0) | |
| 2463 { | |
| 2464 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc); | |
| 2465 goto err; | |
| 2466 } | |
| 2467 | |
| 2468 #ifdef KSSL_DEBUG | |
| 2469 kssl_ctx_show(kssl_ctx); | |
| 2470 #endif /* KSSL_DEBUG */ | |
| 2471 | |
| 2472 enc = kssl_map_enc(kssl_ctx->enctype); | |
| 2473 if (enc == NULL) | |
| 2474 goto err; | |
| 2475 | |
| 2476 memset(iv, 0, sizeof iv); /* per RFC 1510 */ | |
| 2477 | |
| 2478 if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv)) | |
| 2479 { | |
| 2480 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2481 SSL_R_DECRYPTION_FAILED); | |
| 2482 goto err; | |
| 2483 } | |
| 2484 if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl, | |
| 2485 (unsigned char *)enc_pms.data, enc_pms.l
ength)) | |
| 2486 { | |
| 2487 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2488 SSL_R_DECRYPTION_FAILED); | |
| 2489 goto err; | |
| 2490 } | |
| 2491 if (outl > SSL_MAX_MASTER_KEY_LENGTH) | |
| 2492 { | |
| 2493 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2494 SSL_R_DATA_LENGTH_TOO_LONG); | |
| 2495 goto err; | |
| 2496 } | |
| 2497 if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl)) | |
| 2498 { | |
| 2499 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2500 SSL_R_DECRYPTION_FAILED); | |
| 2501 goto err; | |
| 2502 } | |
| 2503 outl += padl; | |
| 2504 if (outl > SSL_MAX_MASTER_KEY_LENGTH) | |
| 2505 { | |
| 2506 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2507 SSL_R_DATA_LENGTH_TOO_LONG); | |
| 2508 goto err; | |
| 2509 } | |
| 2510 if (!((pms[0] == (s->client_version>>8)) && (pms[1] == (s->clien
t_version & 0xff)))) | |
| 2511 { | |
| 2512 /* The premaster secret must contain the same version number
as the | |
| 2513 * ClientHello to detect version rollback attacks (strangely
, the | |
| 2514 * protocol does not offer such protection for DH ciphersuit
es). | |
| 2515 * However, buggy clients exist that send random bytes inste
ad of | |
| 2516 * the protocol version. | |
| 2517 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients.
| |
| 2518 * (Perhaps we should have a separate BUG value for the Kerb
eros cipher) | |
| 2519 */ | |
| 2520 if (!(s->options & SSL_OP_TLS_ROLLBACK_BUG)) | |
| 2521 { | |
| 2522 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2523 SSL_AD_DECODE_ERROR); | |
| 2524 goto err; | |
| 2525 } | |
| 2526 } | |
| 2527 | |
| 2528 EVP_CIPHER_CTX_cleanup(&ciph_ctx); | |
| 2529 | |
| 2530 s->session->master_key_length= | |
| 2531 s->method->ssl3_enc->generate_master_secret(s, | |
| 2532 s->session->master_key, pms, outl); | |
| 2533 | |
| 2534 if (kssl_ctx->client_princ) | |
| 2535 { | |
| 2536 size_t len = strlen(kssl_ctx->client_princ); | |
| 2537 if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH ) | |
| 2538 { | |
| 2539 s->session->krb5_client_princ_len = len; | |
| 2540 memcpy(s->session->krb5_client_princ,kssl_ctx->c
lient_princ,len); | |
| 2541 } | |
| 2542 } | |
| 2543 | |
| 2544 | |
| 2545 /* Was doing kssl_ctx_free() here, | |
| 2546 ** but it caused problems for apache. | |
| 2547 ** kssl_ctx = kssl_ctx_free(kssl_ctx); | |
| 2548 ** if (s->kssl_ctx) s->kssl_ctx = NULL; | |
| 2549 */ | |
| 2550 } | |
| 2551 else | |
| 2552 #endif /* OPENSSL_NO_KRB5 */ | |
| 2553 | |
| 2554 #ifndef OPENSSL_NO_ECDH | |
| 2555 if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) | |
| 2556 { | |
| 2557 int ret = 1; | |
| 2558 int field_size = 0; | |
| 2559 const EC_KEY *tkey; | |
| 2560 const EC_GROUP *group; | |
| 2561 const BIGNUM *priv_key; | |
| 2562 | |
| 2563 /* initialize structures for server's ECDH key pair */ | |
| 2564 if ((srvr_ecdh = EC_KEY_new()) == NULL) | |
| 2565 { | |
| 2566 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2567 ERR_R_MALLOC_FAILURE); | |
| 2568 goto err; | |
| 2569 } | |
| 2570 | |
| 2571 /* Let's get server private key and group information */ | |
| 2572 if (alg_k & (SSL_kECDHr|SSL_kECDHe)) | |
| 2573 { | |
| 2574 /* use the certificate */ | |
| 2575 tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec; | |
| 2576 } | |
| 2577 else | |
| 2578 { | |
| 2579 /* use the ephermeral values we saved when | |
| 2580 * generating the ServerKeyExchange msg. | |
| 2581 */ | |
| 2582 tkey = s->s3->tmp.ecdh; | |
| 2583 } | |
| 2584 | |
| 2585 group = EC_KEY_get0_group(tkey); | |
| 2586 priv_key = EC_KEY_get0_private_key(tkey); | |
| 2587 | |
| 2588 if (!EC_KEY_set_group(srvr_ecdh, group) || | |
| 2589 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) | |
| 2590 { | |
| 2591 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2592 ERR_R_EC_LIB); | |
| 2593 goto err; | |
| 2594 } | |
| 2595 | |
| 2596 /* Let's get client's public key */ | |
| 2597 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) | |
| 2598 { | |
| 2599 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2600 ERR_R_MALLOC_FAILURE); | |
| 2601 goto err; | |
| 2602 } | |
| 2603 | |
| 2604 if (n == 0L) | |
| 2605 { | |
| 2606 /* Client Publickey was in Client Certificate */ | |
| 2607 | |
| 2608 if (alg_k & SSL_kEECDH) | |
| 2609 { | |
| 2610 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 2611 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R
_MISSING_TMP_ECDH_KEY); | |
| 2612 goto f_err; | |
| 2613 } | |
| 2614 if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer)) | |
| 2615 == NULL) || | |
| 2616 (clnt_pub_pkey->type != EVP_PKEY_EC)) | |
| 2617 { | |
| 2618 /* XXX: For now, we do not support client | |
| 2619 * authentication using ECDH certificates | |
| 2620 * so this branch (n == 0L) of the code is | |
| 2621 * never executed. When that support is | |
| 2622 * added, we ought to ensure the key | |
| 2623 * received in the certificate is | |
| 2624 * authorized for key agreement. | |
| 2625 * ECDH_compute_key implicitly checks that | |
| 2626 * the two ECDH shares are for the same | |
| 2627 * group. | |
| 2628 */ | |
| 2629 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 2630 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2631 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS); | |
| 2632 goto f_err; | |
| 2633 } | |
| 2634 | |
| 2635 if (EC_POINT_copy(clnt_ecpoint, | |
| 2636 EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec)) == 0
) | |
| 2637 { | |
| 2638 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2639 ERR_R_EC_LIB); | |
| 2640 goto err; | |
| 2641 } | |
| 2642 ret = 2; /* Skip certificate verify processing */ | |
| 2643 } | |
| 2644 else | |
| 2645 { | |
| 2646 /* Get client's public key from encoded point | |
| 2647 * in the ClientKeyExchange message. | |
| 2648 */ | |
| 2649 if ((bn_ctx = BN_CTX_new()) == NULL) | |
| 2650 { | |
| 2651 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2652 ERR_R_MALLOC_FAILURE); | |
| 2653 goto err; | |
| 2654 } | |
| 2655 | |
| 2656 /* Get encoded point length */ | |
| 2657 i = *p; | |
| 2658 p += 1; | |
| 2659 if (n != 1 + i) | |
| 2660 { | |
| 2661 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2662 ERR_R_EC_LIB); | |
| 2663 goto err; | |
| 2664 } | |
| 2665 if (EC_POINT_oct2point(group, | |
| 2666 clnt_ecpoint, p, i, bn_ctx) == 0) | |
| 2667 { | |
| 2668 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2669 ERR_R_EC_LIB); | |
| 2670 goto err; | |
| 2671 } | |
| 2672 /* p is pointing to somewhere in the buffer | |
| 2673 * currently, so set it to the start | |
| 2674 */ | |
| 2675 p=(unsigned char *)s->init_buf->data; | |
| 2676 } | |
| 2677 | |
| 2678 /* Compute the shared pre-master secret */ | |
| 2679 field_size = EC_GROUP_get_degree(group); | |
| 2680 if (field_size <= 0) | |
| 2681 { | |
| 2682 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2683 ERR_R_ECDH_LIB); | |
| 2684 goto err; | |
| 2685 } | |
| 2686 i = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecd
h, NULL); | |
| 2687 if (i <= 0) | |
| 2688 { | |
| 2689 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2690 ERR_R_ECDH_LIB); | |
| 2691 goto err; | |
| 2692 } | |
| 2693 | |
| 2694 EVP_PKEY_free(clnt_pub_pkey); | |
| 2695 EC_POINT_free(clnt_ecpoint); | |
| 2696 EC_KEY_free(srvr_ecdh); | |
| 2697 BN_CTX_free(bn_ctx); | |
| 2698 EC_KEY_free(s->s3->tmp.ecdh); | |
| 2699 s->s3->tmp.ecdh = NULL; | |
| 2700 | |
| 2701 /* Compute the master secret */ | |
| 2702 s->session->master_key_length = s->method->ssl3_enc-> \ | |
| 2703 generate_master_secret(s, s->session->master_key, p, i); | |
| 2704 | |
| 2705 OPENSSL_cleanse(p, i); | |
| 2706 return (ret); | |
| 2707 } | |
| 2708 else | |
| 2709 #endif | |
| 2710 #ifndef OPENSSL_NO_PSK | |
| 2711 if (alg_k & SSL_kPSK) | |
| 2712 { | |
| 2713 unsigned char *t = NULL; | |
| 2714 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4]; | |
| 2715 unsigned int pre_ms_len = 0, psk_len = 0; | |
| 2716 int psk_err = 1; | |
| 2717 char tmp_id[PSK_MAX_IDENTITY_LEN+1]; | |
| 2718 | |
| 2719 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 2720 | |
| 2721 n2s(p,i); | |
| 2722 if (n != i+2) | |
| 2723 { | |
| 2724 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2725 SSL_R_LENGTH_MISMATCH); | |
| 2726 goto psk_err; | |
| 2727 } | |
| 2728 if (i > PSK_MAX_IDENTITY_LEN) | |
| 2729 { | |
| 2730 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2731 SSL_R_DATA_LENGTH_TOO_LONG); | |
| 2732 goto psk_err; | |
| 2733 } | |
| 2734 if (s->psk_server_callback == NULL) | |
| 2735 { | |
| 2736 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2737 SSL_R_PSK_NO_SERVER_CB); | |
| 2738 goto psk_err; | |
| 2739 } | |
| 2740 | |
| 2741 /* Create guaranteed NULL-terminated identity | |
| 2742 * string for the callback */ | |
| 2743 memcpy(tmp_id, p, i); | |
| 2744 memset(tmp_id+i, 0, PSK_MAX_IDENTITY_LEN+1-i); | |
| 2745 psk_len = s->psk_server_callback(s, tmp_id, | |
| 2746 psk_or_pre_ms, sizeof(psk_or_pre_ms)); | |
| 2747 OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN+1); | |
| 2748 | |
| 2749 if (psk_len > PSK_MAX_PSK_LEN) | |
| 2750 { | |
| 2751 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2752 ERR_R_INTERNAL_ERROR); | |
| 2753 goto psk_err; | |
| 2754 } | |
| 2755 else if (psk_len == 0) | |
| 2756 { | |
| 2757 /* PSK related to the given identity not found *
/ | |
| 2758 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2759 SSL_R_PSK_IDENTITY_NOT_FOUND); | |
| 2760 al=SSL_AD_UNKNOWN_PSK_IDENTITY; | |
| 2761 goto psk_err; | |
| 2762 } | |
| 2763 | |
| 2764 /* create PSK pre_master_secret */ | |
| 2765 pre_ms_len=2+psk_len+2+psk_len; | |
| 2766 t = psk_or_pre_ms; | |
| 2767 memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len)
; | |
| 2768 s2n(psk_len, t); | |
| 2769 memset(t, 0, psk_len); | |
| 2770 t+=psk_len; | |
| 2771 s2n(psk_len, t); | |
| 2772 | |
| 2773 if (s->session->psk_identity != NULL) | |
| 2774 OPENSSL_free(s->session->psk_identity); | |
| 2775 s->session->psk_identity = BUF_strdup((char *)p); | |
| 2776 if (s->session->psk_identity == NULL) | |
| 2777 { | |
| 2778 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2779 ERR_R_MALLOC_FAILURE); | |
| 2780 goto psk_err; | |
| 2781 } | |
| 2782 | |
| 2783 if (s->session->psk_identity_hint != NULL) | |
| 2784 OPENSSL_free(s->session->psk_identity_hint); | |
| 2785 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_i
dentity_hint); | |
| 2786 if (s->ctx->psk_identity_hint != NULL && | |
| 2787 s->session->psk_identity_hint == NULL) | |
| 2788 { | |
| 2789 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2790 ERR_R_MALLOC_FAILURE); | |
| 2791 goto psk_err; | |
| 2792 } | |
| 2793 | |
| 2794 s->session->master_key_length= | |
| 2795 s->method->ssl3_enc->generate_master_secret(s, | |
| 2796 s->session->master_key, psk_or_pre_ms, p
re_ms_len); | |
| 2797 psk_err = 0; | |
| 2798 psk_err: | |
| 2799 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms)); | |
| 2800 if (psk_err != 0) | |
| 2801 goto f_err; | |
| 2802 } | |
| 2803 else | |
| 2804 #endif | |
| 2805 #ifndef OPENSSL_NO_SRP | |
| 2806 if (alg_k & SSL_kSRP) | |
| 2807 { | |
| 2808 int param_len; | |
| 2809 | |
| 2810 n2s(p,i); | |
| 2811 param_len=i+2; | |
| 2812 if (param_len > n) | |
| 2813 { | |
| 2814 al=SSL_AD_DECODE_ERROR; | |
| 2815 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_
BAD_SRP_A_LENGTH); | |
| 2816 goto f_err; | |
| 2817 } | |
| 2818 if (!(s->srp_ctx.A=BN_bin2bn(p,i,NULL))) | |
| 2819 { | |
| 2820 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_
BN_LIB); | |
| 2821 goto err; | |
| 2822 } | |
| 2823 if (s->session->srp_username != NULL) | |
| 2824 OPENSSL_free(s->session->srp_username); | |
| 2825 s->session->srp_username = BUF_strdup(s->srp_ctx.login); | |
| 2826 if (s->session->srp_username == NULL) | |
| 2827 { | |
| 2828 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2829 ERR_R_MALLOC_FAILURE); | |
| 2830 goto err; | |
| 2831 } | |
| 2832 | |
| 2833 if ((s->session->master_key_length = SRP_generate_server
_master_secret(s,s->session->master_key))<0) | |
| 2834 { | |
| 2835 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_
INTERNAL_ERROR); | |
| 2836 goto err; | |
| 2837 } | |
| 2838 | |
| 2839 p+=i; | |
| 2840 } | |
| 2841 else | |
| 2842 #endif /* OPENSSL_NO_SRP */ | |
| 2843 if (alg_k & SSL_kGOST) | |
| 2844 { | |
| 2845 int ret = 0; | |
| 2846 EVP_PKEY_CTX *pkey_ctx; | |
| 2847 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL; | |
| 2848 unsigned char premaster_secret[32], *start; | |
| 2849 size_t outlen=32, inlen; | |
| 2850 unsigned long alg_a; | |
| 2851 | |
| 2852 /* Get our certificate private key*/ | |
| 2853 alg_a = s->s3->tmp.new_cipher->algorithm_auth; | |
| 2854 if (alg_a & SSL_aGOST94) | |
| 2855 pk = s->cert->pkeys[SSL_PKEY_GOST94].privatekey; | |
| 2856 else if (alg_a & SSL_aGOST01) | |
| 2857 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey; | |
| 2858 | |
| 2859 pkey_ctx = EVP_PKEY_CTX_new(pk,NULL); | |
| 2860 EVP_PKEY_decrypt_init(pkey_ctx); | |
| 2861 /* If client certificate is present and is of the same t
ype, maybe | |
| 2862 * use it for key exchange. Don't mind errors from | |
| 2863 * EVP_PKEY_derive_set_peer, because it is completely va
lid to use | |
| 2864 * a client certificate for authorization only. */ | |
| 2865 client_pub_pkey = X509_get_pubkey(s->session->peer); | |
| 2866 if (client_pub_pkey) | |
| 2867 { | |
| 2868 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pu
b_pkey) <= 0) | |
| 2869 ERR_clear_error(); | |
| 2870 } | |
| 2871 /* Decrypt session key */ | |
| 2872 if ((*p!=( V_ASN1_SEQUENCE| V_ASN1_CONSTRUCTED))) | |
| 2873 { | |
| 2874 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_
DECRYPTION_FAILED); | |
| 2875 goto gerr; | |
| 2876 } | |
| 2877 if (p[1] == 0x81) | |
| 2878 { | |
| 2879 start = p+3; | |
| 2880 inlen = p[2]; | |
| 2881 } | |
| 2882 else if (p[1] < 0x80) | |
| 2883 { | |
| 2884 start = p+2; | |
| 2885 inlen = p[1]; | |
| 2886 } | |
| 2887 else | |
| 2888 { | |
| 2889 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_
DECRYPTION_FAILED); | |
| 2890 goto gerr; | |
| 2891 } | |
| 2892 if (EVP_PKEY_decrypt(pkey_ctx,premaster_secret,&outlen,s
tart,inlen) <=0) | |
| 2893 | |
| 2894 { | |
| 2895 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_
DECRYPTION_FAILED); | |
| 2896 goto gerr; | |
| 2897 } | |
| 2898 /* Generate master secret */ | |
| 2899 s->session->master_key_length= | |
| 2900 s->method->ssl3_enc->generate_master_secret(s, | |
| 2901 s->session->master_key,premaster_secret,
32); | |
| 2902 /* Check if pubkey from client certificate was used */ | |
| 2903 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PE
ER_KEY, 2, NULL) > 0) | |
| 2904 ret = 2; | |
| 2905 else | |
| 2906 ret = 1; | |
| 2907 gerr: | |
| 2908 EVP_PKEY_free(client_pub_pkey); | |
| 2909 EVP_PKEY_CTX_free(pkey_ctx); | |
| 2910 if (ret) | |
| 2911 return ret; | |
| 2912 else | |
| 2913 goto err; | |
| 2914 } | |
| 2915 else | |
| 2916 { | |
| 2917 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 2918 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | |
| 2919 SSL_R_UNKNOWN_CIPHER_TYPE); | |
| 2920 goto f_err; | |
| 2921 } | |
| 2922 | |
| 2923 return(1); | |
| 2924 f_err: | |
| 2925 ssl3_send_alert(s,SSL3_AL_FATAL,al); | |
| 2926 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_E
CDH) || defined(OPENSSL_NO_SRP) | |
| 2927 err: | |
| 2928 #endif | |
| 2929 #ifndef OPENSSL_NO_ECDH | |
| 2930 EVP_PKEY_free(clnt_pub_pkey); | |
| 2931 EC_POINT_free(clnt_ecpoint); | |
| 2932 if (srvr_ecdh != NULL) | |
| 2933 EC_KEY_free(srvr_ecdh); | |
| 2934 BN_CTX_free(bn_ctx); | |
| 2935 #endif | |
| 2936 return(-1); | |
| 2937 } | |
| 2938 | |
| 2939 int ssl3_get_cert_verify(SSL *s) | |
| 2940 { | |
| 2941 EVP_PKEY *pkey=NULL; | |
| 2942 unsigned char *p; | |
| 2943 int al,ok,ret=0; | |
| 2944 long n; | |
| 2945 int type=0,i,j; | |
| 2946 X509 *peer; | |
| 2947 const EVP_MD *md = NULL; | |
| 2948 EVP_MD_CTX mctx; | |
| 2949 EVP_MD_CTX_init(&mctx); | |
| 2950 | |
| 2951 n=s->method->ssl_get_message(s, | |
| 2952 SSL3_ST_SR_CERT_VRFY_A, | |
| 2953 SSL3_ST_SR_CERT_VRFY_B, | |
| 2954 -1, | |
| 2955 516, /* Enough for 4096 bit RSA key with TLS v1.2 */ | |
| 2956 &ok); | |
| 2957 | |
| 2958 if (!ok) return((int)n); | |
| 2959 | |
| 2960 if (s->session->peer != NULL) | |
| 2961 { | |
| 2962 peer=s->session->peer; | |
| 2963 pkey=X509_get_pubkey(peer); | |
| 2964 type=X509_certificate_type(peer,pkey); | |
| 2965 } | |
| 2966 else | |
| 2967 { | |
| 2968 peer=NULL; | |
| 2969 pkey=NULL; | |
| 2970 } | |
| 2971 | |
| 2972 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY) | |
| 2973 { | |
| 2974 s->s3->tmp.reuse_message=1; | |
| 2975 if ((peer != NULL) && (type & EVP_PKT_SIGN)) | |
| 2976 { | |
| 2977 al=SSL_AD_UNEXPECTED_MESSAGE; | |
| 2978 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_M
ESSAGE); | |
| 2979 goto f_err; | |
| 2980 } | |
| 2981 ret=1; | |
| 2982 goto end; | |
| 2983 } | |
| 2984 | |
| 2985 if (peer == NULL) | |
| 2986 { | |
| 2987 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED)
; | |
| 2988 al=SSL_AD_UNEXPECTED_MESSAGE; | |
| 2989 goto f_err; | |
| 2990 } | |
| 2991 | |
| 2992 if (!(type & EVP_PKT_SIGN)) | |
| 2993 { | |
| 2994 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNIN
G_CERTIFICATE); | |
| 2995 al=SSL_AD_ILLEGAL_PARAMETER; | |
| 2996 goto f_err; | |
| 2997 } | |
| 2998 | |
| 2999 if (s->s3->change_cipher_spec) | |
| 3000 { | |
| 3001 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY); | |
| 3002 al=SSL_AD_UNEXPECTED_MESSAGE; | |
| 3003 goto f_err; | |
| 3004 } | |
| 3005 | |
| 3006 /* we now have a signature that we need to verify */ | |
| 3007 p=(unsigned char *)s->init_msg; | |
| 3008 /* Check for broken implementations of GOST ciphersuites */ | |
| 3009 /* If key is GOST and n is exactly 64, it is bare | |
| 3010 * signature without length field */ | |
| 3011 if (n==64 && (pkey->type==NID_id_GostR3410_94 || | |
| 3012 pkey->type == NID_id_GostR3410_2001) ) | |
| 3013 { | |
| 3014 i=64; | |
| 3015 } | |
| 3016 else | |
| 3017 { | |
| 3018 if (TLS1_get_version(s) >= TLS1_2_VERSION) | |
| 3019 { | |
| 3020 int sigalg = tls12_get_sigid(pkey); | |
| 3021 /* Should never happen */ | |
| 3022 if (sigalg == -1) | |
| 3023 { | |
| 3024 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL
_ERROR); | |
| 3025 al=SSL_AD_INTERNAL_ERROR; | |
| 3026 goto f_err; | |
| 3027 } | |
| 3028 /* Check key type is consistent with signature */ | |
| 3029 if (sigalg != (int)p[1]) | |
| 3030 { | |
| 3031 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SI
GNATURE_TYPE); | |
| 3032 al=SSL_AD_DECODE_ERROR; | |
| 3033 goto f_err; | |
| 3034 } | |
| 3035 md = tls12_get_hash(p[0]); | |
| 3036 if (md == NULL) | |
| 3037 { | |
| 3038 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_UNKNOWN_
DIGEST); | |
| 3039 al=SSL_AD_DECODE_ERROR; | |
| 3040 goto f_err; | |
| 3041 } | |
| 3042 #ifdef SSL_DEBUG | |
| 3043 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md)); | |
| 3044 #endif | |
| 3045 p += 2; | |
| 3046 n -= 2; | |
| 3047 } | |
| 3048 n2s(p,i); | |
| 3049 n-=2; | |
| 3050 if (i > n) | |
| 3051 { | |
| 3052 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH)
; | |
| 3053 al=SSL_AD_DECODE_ERROR; | |
| 3054 goto f_err; | |
| 3055 } | |
| 3056 } | |
| 3057 j=EVP_PKEY_size(pkey); | |
| 3058 if ((i > j) || (n > j) || (n <= 0)) | |
| 3059 { | |
| 3060 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE); | |
| 3061 al=SSL_AD_DECODE_ERROR; | |
| 3062 goto f_err; | |
| 3063 } | |
| 3064 | |
| 3065 if (TLS1_get_version(s) >= TLS1_2_VERSION) | |
| 3066 { | |
| 3067 long hdatalen = 0; | |
| 3068 void *hdata; | |
| 3069 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata); | |
| 3070 if (hdatalen <= 0) | |
| 3071 { | |
| 3072 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR)
; | |
| 3073 al=SSL_AD_INTERNAL_ERROR; | |
| 3074 goto f_err; | |
| 3075 } | |
| 3076 #ifdef SSL_DEBUG | |
| 3077 fprintf(stderr, "Using TLS 1.2 with client verify alg %s\n", | |
| 3078 EVP_MD_name(md)); | |
| 3079 #endif | |
| 3080 if (!EVP_VerifyInit_ex(&mctx, md, NULL) | |
| 3081 || !EVP_VerifyUpdate(&mctx, hdata, hdatalen)) | |
| 3082 { | |
| 3083 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_EVP_LIB); | |
| 3084 al=SSL_AD_INTERNAL_ERROR; | |
| 3085 goto f_err; | |
| 3086 } | |
| 3087 | |
| 3088 if (EVP_VerifyFinal(&mctx, p , i, pkey) <= 0) | |
| 3089 { | |
| 3090 al=SSL_AD_DECRYPT_ERROR; | |
| 3091 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_SIGNATURE); | |
| 3092 goto f_err; | |
| 3093 } | |
| 3094 } | |
| 3095 else | |
| 3096 #ifndef OPENSSL_NO_RSA | |
| 3097 if (pkey->type == EVP_PKEY_RSA) | |
| 3098 { | |
| 3099 i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md, | |
| 3100 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i, | |
| 3101 pkey->pkey.rsa); | |
| 3102 if (i < 0) | |
| 3103 { | |
| 3104 al=SSL_AD_DECRYPT_ERROR; | |
| 3105 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT)
; | |
| 3106 goto f_err; | |
| 3107 } | |
| 3108 if (i == 0) | |
| 3109 { | |
| 3110 al=SSL_AD_DECRYPT_ERROR; | |
| 3111 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATUR
E); | |
| 3112 goto f_err; | |
| 3113 } | |
| 3114 } | |
| 3115 else | |
| 3116 #endif | |
| 3117 #ifndef OPENSSL_NO_DSA | |
| 3118 if (pkey->type == EVP_PKEY_DSA) | |
| 3119 { | |
| 3120 j=DSA_verify(pkey->save_type, | |
| 3121 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]), | |
| 3122 SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa); | |
| 3123 if (j <= 0) | |
| 3124 { | |
| 3125 /* bad signature */ | |
| 3126 al=SSL_AD_DECRYPT_ERROR; | |
| 3127 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATUR
E); | |
| 3128 goto f_err; | |
| 3129 } | |
| 3130 } | |
| 3131 else | |
| 3132 #endif | |
| 3133 #ifndef OPENSSL_NO_ECDSA | |
| 3134 if (pkey->type == EVP_PKEY_EC) | |
| 3135 { | |
| 3136 j=ECDSA_verify(pkey->save_type, | |
| 3137 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]), | |
| 3138 SHA_DIGEST_LENGTH,p,i,pkey->pkey.ec); | |
| 3139 if (j <= 0) | |
| 3140 { | |
| 3141 /* bad signature */ | |
| 3142 al=SSL_AD_DECRYPT_ERROR; | |
| 3143 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | |
| 3144 SSL_R_BAD_ECDSA_SIGNATURE); | |
| 3145 goto f_err; | |
| 3146 } | |
| 3147 } | |
| 3148 else | |
| 3149 #endif | |
| 3150 if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_
2001) | |
| 3151 { unsigned char signature[64]; | |
| 3152 int idx; | |
| 3153 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey,NULL); | |
| 3154 EVP_PKEY_verify_init(pctx); | |
| 3155 if (i!=64) { | |
| 3156 fprintf(stderr,"GOST signature length is %d",i); | |
| 3157 } | |
| 3158 for (idx=0;idx<64;idx++) { | |
| 3159 signature[63-idx]=p[idx]; | |
| 3160 } | |
| 3161 j=EVP_PKEY_verify(pctx,signature,64,s->s3->tmp.cert_veri
fy_md,32); | |
| 3162 EVP_PKEY_CTX_free(pctx); | |
| 3163 if (j<=0) | |
| 3164 { | |
| 3165 al=SSL_AD_DECRYPT_ERROR; | |
| 3166 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | |
| 3167 SSL_R_BAD_ECDSA_SIGNATURE); | |
| 3168 goto f_err; | |
| 3169 } | |
| 3170 } | |
| 3171 else | |
| 3172 { | |
| 3173 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR); | |
| 3174 al=SSL_AD_UNSUPPORTED_CERTIFICATE; | |
| 3175 goto f_err; | |
| 3176 } | |
| 3177 | |
| 3178 | |
| 3179 ret=1; | |
| 3180 if (0) | |
| 3181 { | |
| 3182 f_err: | |
| 3183 ssl3_send_alert(s,SSL3_AL_FATAL,al); | |
| 3184 } | |
| 3185 end: | |
| 3186 if (s->s3->handshake_buffer) | |
| 3187 { | |
| 3188 BIO_free(s->s3->handshake_buffer); | |
| 3189 s->s3->handshake_buffer = NULL; | |
| 3190 s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE; | |
| 3191 } | |
| 3192 EVP_MD_CTX_cleanup(&mctx); | |
| 3193 EVP_PKEY_free(pkey); | |
| 3194 return(ret); | |
| 3195 } | |
| 3196 | |
| 3197 int ssl3_get_client_certificate(SSL *s) | |
| 3198 { | |
| 3199 int i,ok,al,ret= -1; | |
| 3200 X509 *x=NULL; | |
| 3201 unsigned long l,nc,llen,n; | |
| 3202 const unsigned char *p,*q; | |
| 3203 unsigned char *d; | |
| 3204 STACK_OF(X509) *sk=NULL; | |
| 3205 | |
| 3206 n=s->method->ssl_get_message(s, | |
| 3207 SSL3_ST_SR_CERT_A, | |
| 3208 SSL3_ST_SR_CERT_B, | |
| 3209 -1, | |
| 3210 s->max_cert_list, | |
| 3211 &ok); | |
| 3212 | |
| 3213 if (!ok) return((int)n); | |
| 3214 | |
| 3215 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) | |
| 3216 { | |
| 3217 if ( (s->verify_mode & SSL_VERIFY_PEER) && | |
| 3218 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) | |
| 3219 { | |
| 3220 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_
NOT_RETURN_A_CERTIFICATE); | |
| 3221 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 3222 goto f_err; | |
| 3223 } | |
| 3224 /* If tls asked for a client cert, the client must return a 0 li
st */ | |
| 3225 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) | |
| 3226 { | |
| 3227 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_
DID_NOT_RESPOND_WITH_CERTIFICATE_LIST); | |
| 3228 al=SSL_AD_UNEXPECTED_MESSAGE; | |
| 3229 goto f_err; | |
| 3230 } | |
| 3231 s->s3->tmp.reuse_message=1; | |
| 3232 return(1); | |
| 3233 } | |
| 3234 | |
| 3235 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) | |
| 3236 { | |
| 3237 al=SSL_AD_UNEXPECTED_MESSAGE; | |
| 3238 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYP
E); | |
| 3239 goto f_err; | |
| 3240 } | |
| 3241 p=d=(unsigned char *)s->init_msg; | |
| 3242 | |
| 3243 if ((sk=sk_X509_new_null()) == NULL) | |
| 3244 { | |
| 3245 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE); | |
| 3246 goto err; | |
| 3247 } | |
| 3248 | |
| 3249 n2l3(p,llen); | |
| 3250 if (llen+3 != n) | |
| 3251 { | |
| 3252 al=SSL_AD_DECODE_ERROR; | |
| 3253 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH); | |
| 3254 goto f_err; | |
| 3255 } | |
| 3256 for (nc=0; nc<llen; ) | |
| 3257 { | |
| 3258 n2l3(p,l); | |
| 3259 if ((l+nc+3) > llen) | |
| 3260 { | |
| 3261 al=SSL_AD_DECODE_ERROR; | |
| 3262 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENG
TH_MISMATCH); | |
| 3263 goto f_err; | |
| 3264 } | |
| 3265 | |
| 3266 q=p; | |
| 3267 x=d2i_X509(NULL,&p,l); | |
| 3268 if (x == NULL) | |
| 3269 { | |
| 3270 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB)
; | |
| 3271 goto err; | |
| 3272 } | |
| 3273 if (p != (q+l)) | |
| 3274 { | |
| 3275 al=SSL_AD_DECODE_ERROR; | |
| 3276 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENG
TH_MISMATCH); | |
| 3277 goto f_err; | |
| 3278 } | |
| 3279 if (!sk_X509_push(sk,x)) | |
| 3280 { | |
| 3281 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FA
ILURE); | |
| 3282 goto err; | |
| 3283 } | |
| 3284 x=NULL; | |
| 3285 nc+=l+3; | |
| 3286 } | |
| 3287 | |
| 3288 if (sk_X509_num(sk) <= 0) | |
| 3289 { | |
| 3290 /* TLS does not mind 0 certs returned */ | |
| 3291 if (s->version == SSL3_VERSION) | |
| 3292 { | |
| 3293 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 3294 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIF
ICATES_RETURNED); | |
| 3295 goto f_err; | |
| 3296 } | |
| 3297 /* Fail for TLS only if we required a certificate */ | |
| 3298 else if ((s->verify_mode & SSL_VERIFY_PEER) && | |
| 3299 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) | |
| 3300 { | |
| 3301 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_
NOT_RETURN_A_CERTIFICATE); | |
| 3302 al=SSL_AD_HANDSHAKE_FAILURE; | |
| 3303 goto f_err; | |
| 3304 } | |
| 3305 /* No client certificate so digest cached records */ | |
| 3306 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) | |
| 3307 { | |
| 3308 al=SSL_AD_INTERNAL_ERROR; | |
| 3309 goto f_err; | |
| 3310 } | |
| 3311 } | |
| 3312 else | |
| 3313 { | |
| 3314 i=ssl_verify_cert_chain(s,sk); | |
| 3315 if (i <= 0) | |
| 3316 { | |
| 3317 al=ssl_verify_alarm_type(s->verify_result); | |
| 3318 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIF
ICATE_RETURNED); | |
| 3319 goto f_err; | |
| 3320 } | |
| 3321 } | |
| 3322 | |
| 3323 if (s->session->peer != NULL) /* This should not be needed */ | |
| 3324 X509_free(s->session->peer); | |
| 3325 s->session->peer=sk_X509_shift(sk); | |
| 3326 s->session->verify_result = s->verify_result; | |
| 3327 | |
| 3328 /* With the current implementation, sess_cert will always be NULL | |
| 3329 * when we arrive here. */ | |
| 3330 if (s->session->sess_cert == NULL) | |
| 3331 { | |
| 3332 s->session->sess_cert = ssl_sess_cert_new(); | |
| 3333 if (s->session->sess_cert == NULL) | |
| 3334 { | |
| 3335 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_F
AILURE); | |
| 3336 goto err; | |
| 3337 } | |
| 3338 } | |
| 3339 if (s->session->sess_cert->cert_chain != NULL) | |
| 3340 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free); | |
| 3341 s->session->sess_cert->cert_chain=sk; | |
| 3342 /* Inconsistency alert: cert_chain does *not* include the | |
| 3343 * peer's own certificate, while we do include it in s3_clnt.c */ | |
| 3344 | |
| 3345 sk=NULL; | |
| 3346 | |
| 3347 ret=1; | |
| 3348 if (0) | |
| 3349 { | |
| 3350 f_err: | |
| 3351 ssl3_send_alert(s,SSL3_AL_FATAL,al); | |
| 3352 } | |
| 3353 err: | |
| 3354 if (x != NULL) X509_free(x); | |
| 3355 if (sk != NULL) sk_X509_pop_free(sk,X509_free); | |
| 3356 return(ret); | |
| 3357 } | |
| 3358 | |
| 3359 int ssl3_send_server_certificate(SSL *s) | |
| 3360 { | |
| 3361 unsigned long l; | |
| 3362 X509 *x; | |
| 3363 | |
| 3364 if (s->state == SSL3_ST_SW_CERT_A) | |
| 3365 { | |
| 3366 x=ssl_get_server_send_cert(s); | |
| 3367 if (x == NULL) | |
| 3368 { | |
| 3369 /* VRS: allow null cert if auth == KRB5 */ | |
| 3370 if ((s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5)
|| | |
| 3371 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5)) | |
| 3372 { | |
| 3373 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_
INTERNAL_ERROR); | |
| 3374 return(0); | |
| 3375 } | |
| 3376 } | |
| 3377 | |
| 3378 l=ssl3_output_cert_chain(s,x); | |
| 3379 s->state=SSL3_ST_SW_CERT_B; | |
| 3380 s->init_num=(int)l; | |
| 3381 s->init_off=0; | |
| 3382 } | |
| 3383 | |
| 3384 /* SSL3_ST_SW_CERT_B */ | |
| 3385 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | |
| 3386 } | |
| 3387 | |
| 3388 #ifndef OPENSSL_NO_TLSEXT | |
| 3389 /* send a new session ticket (not necessarily for a new session) */ | |
| 3390 int ssl3_send_newsession_ticket(SSL *s) | |
| 3391 { | |
| 3392 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) | |
| 3393 { | |
| 3394 unsigned char *p, *senc, *macstart; | |
| 3395 const unsigned char *const_p; | |
| 3396 int len, slen_full, slen; | |
| 3397 SSL_SESSION *sess; | |
| 3398 unsigned int hlen; | |
| 3399 EVP_CIPHER_CTX ctx; | |
| 3400 HMAC_CTX hctx; | |
| 3401 SSL_CTX *tctx = s->initial_ctx; | |
| 3402 unsigned char iv[EVP_MAX_IV_LENGTH]; | |
| 3403 unsigned char key_name[16]; | |
| 3404 | |
| 3405 /* get session encoding length */ | |
| 3406 slen_full = i2d_SSL_SESSION(s->session, NULL); | |
| 3407 /* Some length values are 16 bits, so forget it if session is | |
| 3408 * too long | |
| 3409 */ | |
| 3410 if (slen_full > 0xFF00) | |
| 3411 return -1; | |
| 3412 senc = OPENSSL_malloc(slen_full); | |
| 3413 if (!senc) | |
| 3414 return -1; | |
| 3415 p = senc; | |
| 3416 i2d_SSL_SESSION(s->session, &p); | |
| 3417 | |
| 3418 /* create a fresh copy (not shared with other threads) to clean
up */ | |
| 3419 const_p = senc; | |
| 3420 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full); | |
| 3421 if (sess == NULL) | |
| 3422 { | |
| 3423 OPENSSL_free(senc); | |
| 3424 return -1; | |
| 3425 } | |
| 3426 sess->session_id_length = 0; /* ID is irrelevant for the ticket
*/ | |
| 3427 | |
| 3428 slen = i2d_SSL_SESSION(sess, NULL); | |
| 3429 if (slen > slen_full) /* shouldn't ever happen */ | |
| 3430 { | |
| 3431 OPENSSL_free(senc); | |
| 3432 return -1; | |
| 3433 } | |
| 3434 p = senc; | |
| 3435 i2d_SSL_SESSION(sess, &p); | |
| 3436 SSL_SESSION_free(sess); | |
| 3437 | |
| 3438 /* Grow buffer if need be: the length calculation is as | |
| 3439 * follows 1 (size of message name) + 3 (message length | |
| 3440 * bytes) + 4 (ticket lifetime hint) + 2 (ticket length) + | |
| 3441 * 16 (key name) + max_iv_len (iv length) + | |
| 3442 * session_length + max_enc_block_size (max encrypted session | |
| 3443 * length) + max_md_size (HMAC). | |
| 3444 */ | |
| 3445 if (!BUF_MEM_grow(s->init_buf, | |
| 3446 26 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + | |
| 3447 EVP_MAX_MD_SIZE + slen)) | |
| 3448 return -1; | |
| 3449 | |
| 3450 p=(unsigned char *)s->init_buf->data; | |
| 3451 /* do the header */ | |
| 3452 *(p++)=SSL3_MT_NEWSESSION_TICKET; | |
| 3453 /* Skip message length for now */ | |
| 3454 p += 3; | |
| 3455 EVP_CIPHER_CTX_init(&ctx); | |
| 3456 HMAC_CTX_init(&hctx); | |
| 3457 /* Initialize HMAC and cipher contexts. If callback present | |
| 3458 * it does all the work otherwise use generated values | |
| 3459 * from parent ctx. | |
| 3460 */ | |
| 3461 if (tctx->tlsext_ticket_key_cb) | |
| 3462 { | |
| 3463 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, | |
| 3464 &hctx, 1) < 0) | |
| 3465 { | |
| 3466 OPENSSL_free(senc); | |
| 3467 return -1; | |
| 3468 } | |
| 3469 } | |
| 3470 else | |
| 3471 { | |
| 3472 RAND_pseudo_bytes(iv, 16); | |
| 3473 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, | |
| 3474 tctx->tlsext_tick_aes_key, iv); | |
| 3475 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, | |
| 3476 tlsext_tick_md(), NULL); | |
| 3477 memcpy(key_name, tctx->tlsext_tick_key_name, 16); | |
| 3478 } | |
| 3479 | |
| 3480 /* Ticket lifetime hint (advisory only): | |
| 3481 * We leave this unspecified for resumed session (for simplicity
), | |
| 3482 * and guess that tickets for new sessions will live as long | |
| 3483 * as their sessions. */ | |
| 3484 l2n(s->hit ? 0 : s->session->timeout, p); | |
| 3485 | |
| 3486 /* Skip ticket length for now */ | |
| 3487 p += 2; | |
| 3488 /* Output key name */ | |
| 3489 macstart = p; | |
| 3490 memcpy(p, key_name, 16); | |
| 3491 p += 16; | |
| 3492 /* output IV */ | |
| 3493 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx)); | |
| 3494 p += EVP_CIPHER_CTX_iv_length(&ctx); | |
| 3495 /* Encrypt session data */ | |
| 3496 EVP_EncryptUpdate(&ctx, p, &len, senc, slen); | |
| 3497 p += len; | |
| 3498 EVP_EncryptFinal(&ctx, p, &len); | |
| 3499 p += len; | |
| 3500 EVP_CIPHER_CTX_cleanup(&ctx); | |
| 3501 | |
| 3502 HMAC_Update(&hctx, macstart, p - macstart); | |
| 3503 HMAC_Final(&hctx, p, &hlen); | |
| 3504 HMAC_CTX_cleanup(&hctx); | |
| 3505 | |
| 3506 p += hlen; | |
| 3507 /* Now write out lengths: p points to end of data written */ | |
| 3508 /* Total length */ | |
| 3509 len = p - (unsigned char *)s->init_buf->data; | |
| 3510 p=(unsigned char *)s->init_buf->data + 1; | |
| 3511 l2n3(len - 4, p); /* Message length */ | |
| 3512 p += 4; | |
| 3513 s2n(len - 10, p); /* Ticket length */ | |
| 3514 | |
| 3515 /* number of bytes to write */ | |
| 3516 s->init_num= len; | |
| 3517 s->state=SSL3_ST_SW_SESSION_TICKET_B; | |
| 3518 s->init_off=0; | |
| 3519 OPENSSL_free(senc); | |
| 3520 } | |
| 3521 | |
| 3522 /* SSL3_ST_SW_SESSION_TICKET_B */ | |
| 3523 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | |
| 3524 } | |
| 3525 | |
| 3526 int ssl3_send_cert_status(SSL *s) | |
| 3527 { | |
| 3528 if (s->state == SSL3_ST_SW_CERT_STATUS_A) | |
| 3529 { | |
| 3530 unsigned char *p; | |
| 3531 /* Grow buffer if need be: the length calculation is as | |
| 3532 * follows 1 (message type) + 3 (message length) + | |
| 3533 * 1 (ocsp response type) + 3 (ocsp response length) | |
| 3534 * + (ocsp response) | |
| 3535 */ | |
| 3536 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen)) | |
| 3537 return -1; | |
| 3538 | |
| 3539 p=(unsigned char *)s->init_buf->data; | |
| 3540 | |
| 3541 /* do the header */ | |
| 3542 *(p++)=SSL3_MT_CERTIFICATE_STATUS; | |
| 3543 /* message length */ | |
| 3544 l2n3(s->tlsext_ocsp_resplen + 4, p); | |
| 3545 /* status type */ | |
| 3546 *(p++)= s->tlsext_status_type; | |
| 3547 /* length of OCSP response */ | |
| 3548 l2n3(s->tlsext_ocsp_resplen, p); | |
| 3549 /* actual response */ | |
| 3550 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen); | |
| 3551 /* number of bytes to write */ | |
| 3552 s->init_num = 8 + s->tlsext_ocsp_resplen; | |
| 3553 s->state=SSL3_ST_SW_CERT_STATUS_B; | |
| 3554 s->init_off = 0; | |
| 3555 } | |
| 3556 | |
| 3557 /* SSL3_ST_SW_CERT_STATUS_B */ | |
| 3558 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | |
| 3559 } | |
| 3560 | |
| 3561 # ifndef OPENSSL_NO_NEXTPROTONEG | |
| 3562 /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It | |
| 3563 * sets the next_proto member in s if found */ | |
| 3564 int ssl3_get_next_proto(SSL *s) | |
| 3565 { | |
| 3566 int ok; | |
| 3567 int proto_len, padding_len; | |
| 3568 long n; | |
| 3569 const unsigned char *p; | |
| 3570 | |
| 3571 /* Clients cannot send a NextProtocol message if we didn't see the | |
| 3572 * extension in their ClientHello */ | |
| 3573 if (!s->s3->next_proto_neg_seen) | |
| 3574 { | |
| 3575 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,SSL_R_GOT_NEXT_PROTO_WITHOUT_EX
TENSION); | |
| 3576 return -1; | |
| 3577 } | |
| 3578 | |
| 3579 n=s->method->ssl_get_message(s, | |
| 3580 SSL3_ST_SR_NEXT_PROTO_A, | |
| 3581 SSL3_ST_SR_NEXT_PROTO_B, | |
| 3582 SSL3_MT_NEXT_PROTO, | |
| 3583 514, /* See the payload format below */ | |
| 3584 &ok); | |
| 3585 | |
| 3586 if (!ok) | |
| 3587 return((int)n); | |
| 3588 | |
| 3589 /* s->state doesn't reflect whether ChangeCipherSpec has been received | |
| 3590 * in this handshake, but s->s3->change_cipher_spec does (will be reset | |
| 3591 * by ssl3_get_finished). */ | |
| 3592 if (!s->s3->change_cipher_spec) | |
| 3593 { | |
| 3594 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,SSL_R_GOT_NEXT_PROTO_BEFORE_A_C
CS); | |
| 3595 return -1; | |
| 3596 } | |
| 3597 | |
| 3598 if (n < 2) | |
| 3599 return 0; /* The body must be > 1 bytes long */ | |
| 3600 | |
| 3601 p=(unsigned char *)s->init_msg; | |
| 3602 | |
| 3603 /* The payload looks like: | |
| 3604 * uint8 proto_len; | |
| 3605 * uint8 proto[proto_len]; | |
| 3606 * uint8 padding_len; | |
| 3607 * uint8 padding[padding_len]; | |
| 3608 */ | |
| 3609 proto_len = p[0]; | |
| 3610 if (proto_len + 2 > s->init_num) | |
| 3611 return 0; | |
| 3612 padding_len = p[proto_len + 1]; | |
| 3613 if (proto_len + padding_len + 2 != s->init_num) | |
| 3614 return 0; | |
| 3615 | |
| 3616 s->next_proto_negotiated = OPENSSL_malloc(proto_len); | |
| 3617 if (!s->next_proto_negotiated) | |
| 3618 { | |
| 3619 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,ERR_R_MALLOC_FAILURE); | |
| 3620 return 0; | |
| 3621 } | |
| 3622 memcpy(s->next_proto_negotiated, p + 1, proto_len); | |
| 3623 s->next_proto_negotiated_len = proto_len; | |
| 3624 | |
| 3625 return 1; | |
| 3626 } | |
| 3627 # endif | |
| 3628 | |
| 3629 /* ssl3_get_channel_id reads and verifies a ClientID handshake message. */ | |
| 3630 int ssl3_get_channel_id(SSL *s) | |
| 3631 { | |
| 3632 int ret = -1, ok; | |
| 3633 long n; | |
| 3634 const unsigned char *p; | |
| 3635 unsigned short extension_type, extension_len; | |
| 3636 EC_GROUP* p256 = NULL; | |
| 3637 EC_KEY* key = NULL; | |
| 3638 EC_POINT* point = NULL; | |
| 3639 ECDSA_SIG sig; | |
| 3640 BIGNUM x, y; | |
| 3641 | |
| 3642 if (s->state == SSL3_ST_SR_CHANNEL_ID_A && s->init_num == 0) | |
| 3643 { | |
| 3644 /* The first time that we're called we take the current | |
| 3645 * handshake hash and store it. */ | |
| 3646 EVP_MD_CTX md_ctx; | |
| 3647 unsigned int len; | |
| 3648 | |
| 3649 EVP_MD_CTX_init(&md_ctx); | |
| 3650 EVP_DigestInit_ex(&md_ctx, EVP_sha256(), NULL); | |
| 3651 if (!tls1_channel_id_hash(&md_ctx, s)) | |
| 3652 return -1; | |
| 3653 len = sizeof(s->s3->tlsext_channel_id); | |
| 3654 EVP_DigestFinal(&md_ctx, s->s3->tlsext_channel_id, &len); | |
| 3655 EVP_MD_CTX_cleanup(&md_ctx); | |
| 3656 } | |
| 3657 | |
| 3658 n = s->method->ssl_get_message(s, | |
| 3659 SSL3_ST_SR_CHANNEL_ID_A, | |
| 3660 SSL3_ST_SR_CHANNEL_ID_B, | |
| 3661 SSL3_MT_ENCRYPTED_EXTENSIONS, | |
| 3662 2 + 2 + TLSEXT_CHANNEL_ID_SIZE, | |
| 3663 &ok); | |
| 3664 | |
| 3665 if (!ok) | |
| 3666 return((int)n); | |
| 3667 | |
| 3668 ssl3_finish_mac(s, (unsigned char*)s->init_buf->data, s->init_num + 4); | |
| 3669 | |
| 3670 /* s->state doesn't reflect whether ChangeCipherSpec has been received | |
| 3671 * in this handshake, but s->s3->change_cipher_spec does (will be reset | |
| 3672 * by ssl3_get_finished). */ | |
| 3673 if (!s->s3->change_cipher_spec) | |
| 3674 { | |
| 3675 SSLerr(SSL_F_SSL3_GET_CHANNEL_ID,SSL_R_GOT_CHANNEL_ID_BEFORE_A_C
CS); | |
| 3676 return -1; | |
| 3677 } | |
| 3678 | |
| 3679 if (n != 2 + 2 + TLSEXT_CHANNEL_ID_SIZE) | |
| 3680 { | |
| 3681 SSLerr(SSL_F_SSL3_GET_CHANNEL_ID,SSL_R_INVALID_MESSAGE); | |
| 3682 return -1; | |
| 3683 } | |
| 3684 | |
| 3685 p = (unsigned char *)s->init_msg; | |
| 3686 | |
| 3687 /* The payload looks like: | |
| 3688 * uint16 extension_type | |
| 3689 * uint16 extension_len; | |
| 3690 * uint8 x[32]; | |
| 3691 * uint8 y[32]; | |
| 3692 * uint8 r[32]; | |
| 3693 * uint8 s[32]; | |
| 3694 */ | |
| 3695 n2s(p, extension_type); | |
| 3696 n2s(p, extension_len); | |
| 3697 | |
| 3698 if (extension_type != TLSEXT_TYPE_channel_id || | |
| 3699 extension_len != TLSEXT_CHANNEL_ID_SIZE) | |
| 3700 { | |
| 3701 SSLerr(SSL_F_SSL3_GET_CHANNEL_ID,SSL_R_INVALID_MESSAGE); | |
| 3702 return -1; | |
| 3703 } | |
| 3704 | |
| 3705 p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); | |
| 3706 if (!p256) | |
| 3707 { | |
| 3708 SSLerr(SSL_F_SSL3_GET_CHANNEL_ID,SSL_R_NO_P256_SUPPORT); | |
| 3709 return -1; | |
| 3710 } | |
| 3711 | |
| 3712 BN_init(&x); | |
| 3713 BN_init(&y); | |
| 3714 sig.r = BN_new(); | |
| 3715 sig.s = BN_new(); | |
| 3716 | |
| 3717 if (BN_bin2bn(p + 0, 32, &x) == NULL || | |
| 3718 BN_bin2bn(p + 32, 32, &y) == NULL || | |
| 3719 BN_bin2bn(p + 64, 32, sig.r) == NULL || | |
| 3720 BN_bin2bn(p + 96, 32, sig.s) == NULL) | |
| 3721 goto err; | |
| 3722 | |
| 3723 point = EC_POINT_new(p256); | |
| 3724 if (!point || | |
| 3725 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) | |
| 3726 goto err; | |
| 3727 | |
| 3728 key = EC_KEY_new(); | |
| 3729 if (!key || | |
| 3730 !EC_KEY_set_group(key, p256) || | |
| 3731 !EC_KEY_set_public_key(key, point)) | |
| 3732 goto err; | |
| 3733 | |
| 3734 /* We stored the handshake hash in |tlsext_channel_id| the first time | |
| 3735 * that we were called. */ | |
| 3736 switch (ECDSA_do_verify(s->s3->tlsext_channel_id, SHA256_DIGEST_LENGTH,
&sig, key)) { | |
| 3737 case 1: | |
| 3738 break; | |
| 3739 case 0: | |
| 3740 SSLerr(SSL_F_SSL3_GET_CHANNEL_ID,SSL_R_CHANNEL_ID_SIGNATURE_INVA
LID); | |
| 3741 s->s3->tlsext_channel_id_valid = 0; | |
| 3742 goto err; | |
| 3743 default: | |
| 3744 s->s3->tlsext_channel_id_valid = 0; | |
| 3745 goto err; | |
| 3746 } | |
| 3747 | |
| 3748 memcpy(s->s3->tlsext_channel_id, p, 64); | |
| 3749 ret = 1; | |
| 3750 | |
| 3751 err: | |
| 3752 BN_free(&x); | |
| 3753 BN_free(&y); | |
| 3754 BN_free(sig.r); | |
| 3755 BN_free(sig.s); | |
| 3756 if (key) | |
| 3757 EC_KEY_free(key); | |
| 3758 if (point) | |
| 3759 EC_POINT_free(point); | |
| 3760 if (p256) | |
| 3761 EC_GROUP_free(p256); | |
| 3762 return ret; | |
| 3763 } | |
| 3764 #endif | |
| OLD | NEW |