OLD | NEW |
(Empty) | |
| 1 /* ssl/s3_clnt.c */ |
| 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 #include <stdio.h> |
| 152 #include "ssl_locl.h" |
| 153 #include "kssl_lcl.h" |
| 154 #include <openssl/buffer.h> |
| 155 #include <openssl/rand.h> |
| 156 #include <openssl/objects.h> |
| 157 #include <openssl/evp.h> |
| 158 #include <openssl/md5.h> |
| 159 #ifndef OPENSSL_NO_DH |
| 160 #include <openssl/dh.h> |
| 161 #endif |
| 162 #include <openssl/bn.h> |
| 163 #ifndef OPENSSL_NO_ENGINE |
| 164 #include <openssl/engine.h> |
| 165 #endif |
| 166 |
| 167 static const SSL_METHOD *ssl3_get_client_method(int ver); |
| 168 static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b); |
| 169 |
| 170 static const SSL_METHOD *ssl3_get_client_method(int ver) |
| 171 { |
| 172 if (ver == SSL3_VERSION) |
| 173 return(SSLv3_client_method()); |
| 174 else |
| 175 return(NULL); |
| 176 } |
| 177 |
| 178 IMPLEMENT_ssl3_meth_func(SSLv3_client_method, |
| 179 ssl_undefined_function, |
| 180 ssl3_connect, |
| 181 ssl3_get_client_method) |
| 182 |
| 183 int ssl3_connect(SSL *s) |
| 184 { |
| 185 BUF_MEM *buf=NULL; |
| 186 unsigned long Time=(unsigned long)time(NULL); |
| 187 void (*cb)(const SSL *ssl,int type,int val)=NULL; |
| 188 int ret= -1; |
| 189 int new_state,state,skip=0; |
| 190 |
| 191 RAND_add(&Time,sizeof(Time),0); |
| 192 ERR_clear_error(); |
| 193 clear_sys_error(); |
| 194 |
| 195 if (s->info_callback != NULL) |
| 196 cb=s->info_callback; |
| 197 else if (s->ctx->info_callback != NULL) |
| 198 cb=s->ctx->info_callback; |
| 199 |
| 200 s->in_handshake++; |
| 201 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); |
| 202 #if 0 /* Send app data in separate packet, otherwise, some particular site |
| 203 * (only one site so far) closes the socket. |
| 204 * Note: there is a very small chance that two TCP packets |
| 205 * could be arriving at server combined into a single TCP packet, |
| 206 * then trigger that site to break. We haven't encounter that though. |
| 207 */ |
| 208 if (SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) |
| 209 { |
| 210 /* Send app data along with CCS/Finished */ |
| 211 s->s3->flags |= SSL3_FLAGS_DELAY_CLIENT_FINISHED; |
| 212 } |
| 213 #endif |
| 214 |
| 215 for (;;) |
| 216 { |
| 217 state=s->state; |
| 218 |
| 219 switch(s->state) |
| 220 { |
| 221 case SSL_ST_RENEGOTIATE: |
| 222 s->new_session=1; |
| 223 s->state=SSL_ST_CONNECT; |
| 224 s->ctx->stats.sess_connect_renegotiate++; |
| 225 /* break */ |
| 226 case SSL_ST_BEFORE: |
| 227 case SSL_ST_CONNECT: |
| 228 case SSL_ST_BEFORE|SSL_ST_CONNECT: |
| 229 case SSL_ST_OK|SSL_ST_CONNECT: |
| 230 |
| 231 s->server=0; |
| 232 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); |
| 233 |
| 234 if ((s->version & 0xff00 ) != 0x0300) |
| 235 { |
| 236 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR)
; |
| 237 ret = -1; |
| 238 goto end; |
| 239 } |
| 240 |
| 241 /* s->version=SSL3_VERSION; */ |
| 242 s->type=SSL_ST_CONNECT; |
| 243 |
| 244 if (s->init_buf == NULL) |
| 245 { |
| 246 if ((buf=BUF_MEM_new()) == NULL) |
| 247 { |
| 248 ret= -1; |
| 249 goto end; |
| 250 } |
| 251 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) |
| 252 { |
| 253 ret= -1; |
| 254 goto end; |
| 255 } |
| 256 s->init_buf=buf; |
| 257 buf=NULL; |
| 258 } |
| 259 |
| 260 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; } |
| 261 |
| 262 /* setup buffing BIO */ |
| 263 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; } |
| 264 |
| 265 /* don't push the buffering BIO quite yet */ |
| 266 |
| 267 ssl3_init_finished_mac(s); |
| 268 |
| 269 s->state=SSL3_ST_CW_CLNT_HELLO_A; |
| 270 s->ctx->stats.sess_connect++; |
| 271 s->init_num=0; |
| 272 break; |
| 273 |
| 274 case SSL3_ST_CW_CLNT_HELLO_A: |
| 275 case SSL3_ST_CW_CLNT_HELLO_B: |
| 276 |
| 277 s->shutdown=0; |
| 278 ret=ssl3_client_hello(s); |
| 279 if (ret <= 0) goto end; |
| 280 s->state=SSL3_ST_CR_SRVR_HELLO_A; |
| 281 s->init_num=0; |
| 282 |
| 283 /* turn on buffering for the next lot of output */ |
| 284 if (s->bbio != s->wbio) |
| 285 s->wbio=BIO_push(s->bbio,s->wbio); |
| 286 |
| 287 break; |
| 288 |
| 289 case SSL3_ST_CR_SRVR_HELLO_A: |
| 290 case SSL3_ST_CR_SRVR_HELLO_B: |
| 291 ret=ssl3_get_server_hello(s); |
| 292 if (ret <= 0) goto end; |
| 293 |
| 294 if (s->hit) |
| 295 s->state=SSL3_ST_CR_FINISHED_A; |
| 296 else |
| 297 s->state=SSL3_ST_CR_CERT_A; |
| 298 s->init_num=0; |
| 299 break; |
| 300 |
| 301 case SSL3_ST_CR_CERT_A: |
| 302 case SSL3_ST_CR_CERT_B: |
| 303 #ifndef OPENSSL_NO_TLSEXT |
| 304 ret=ssl3_check_finished(s); |
| 305 if (ret <= 0) goto end; |
| 306 if (ret == 2) |
| 307 { |
| 308 s->hit = 1; |
| 309 if (s->tlsext_ticket_expected) |
| 310 s->state=SSL3_ST_CR_SESSION_TICKET_A; |
| 311 else |
| 312 s->state=SSL3_ST_CR_FINISHED_A; |
| 313 s->init_num=0; |
| 314 break; |
| 315 } |
| 316 #endif |
| 317 /* Check if it is anon DH/ECDH */ |
| 318 /* or PSK */ |
| 319 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
&& |
| 320 !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) |
| 321 { |
| 322 ret=ssl3_get_server_certificate(s); |
| 323 if (ret <= 0) goto end; |
| 324 #ifndef OPENSSL_NO_TLSEXT |
| 325 if (s->tlsext_status_expected) |
| 326 s->state=SSL3_ST_CR_CERT_STATUS_A; |
| 327 else |
| 328 s->state=SSL3_ST_CR_KEY_EXCH_A; |
| 329 } |
| 330 else |
| 331 { |
| 332 skip = 1; |
| 333 s->state=SSL3_ST_CR_KEY_EXCH_A; |
| 334 } |
| 335 #else |
| 336 } |
| 337 else |
| 338 skip=1; |
| 339 |
| 340 s->state=SSL3_ST_CR_KEY_EXCH_A; |
| 341 #endif |
| 342 s->init_num=0; |
| 343 break; |
| 344 |
| 345 case SSL3_ST_CR_KEY_EXCH_A: |
| 346 case SSL3_ST_CR_KEY_EXCH_B: |
| 347 ret=ssl3_get_key_exchange(s); |
| 348 if (ret <= 0) goto end; |
| 349 s->state=SSL3_ST_CR_CERT_REQ_A; |
| 350 s->init_num=0; |
| 351 |
| 352 /* at this point we check that we have the |
| 353 * required stuff from the server */ |
| 354 if (!ssl3_check_cert_and_algorithm(s)) |
| 355 { |
| 356 ret= -1; |
| 357 goto end; |
| 358 } |
| 359 break; |
| 360 |
| 361 case SSL3_ST_CR_CERT_REQ_A: |
| 362 case SSL3_ST_CR_CERT_REQ_B: |
| 363 ret=ssl3_get_certificate_request(s); |
| 364 if (ret <= 0) goto end; |
| 365 s->state=SSL3_ST_CR_SRVR_DONE_A; |
| 366 s->init_num=0; |
| 367 break; |
| 368 |
| 369 case SSL3_ST_CR_SRVR_DONE_A: |
| 370 case SSL3_ST_CR_SRVR_DONE_B: |
| 371 ret=ssl3_get_server_done(s); |
| 372 if (ret <= 0) goto end; |
| 373 if (s->s3->tmp.cert_req) |
| 374 s->state=SSL3_ST_CW_CERT_A; |
| 375 else |
| 376 s->state=SSL3_ST_CW_KEY_EXCH_A; |
| 377 s->init_num=0; |
| 378 |
| 379 break; |
| 380 |
| 381 case SSL3_ST_CW_CERT_A: |
| 382 case SSL3_ST_CW_CERT_B: |
| 383 case SSL3_ST_CW_CERT_C: |
| 384 case SSL3_ST_CW_CERT_D: |
| 385 ret=ssl3_send_client_certificate(s); |
| 386 if (ret <= 0) goto end; |
| 387 s->state=SSL3_ST_CW_KEY_EXCH_A; |
| 388 s->init_num=0; |
| 389 break; |
| 390 |
| 391 case SSL3_ST_CW_KEY_EXCH_A: |
| 392 case SSL3_ST_CW_KEY_EXCH_B: |
| 393 ret=ssl3_send_client_key_exchange(s); |
| 394 if (ret <= 0) goto end; |
| 395 /* EAY EAY EAY need to check for DH fix cert |
| 396 * sent back */ |
| 397 /* For TLS, cert_req is set to 2, so a cert chain |
| 398 * of nothing is sent, but no verify packet is sent */ |
| 399 /* XXX: For now, we do not support client |
| 400 * authentication in ECDH cipher suites with |
| 401 * ECDH (rather than ECDSA) certificates. |
| 402 * We need to skip the certificate verify |
| 403 * message when client's ECDH public key is sent |
| 404 * inside the client certificate. |
| 405 */ |
| 406 if (s->s3->tmp.cert_req == 1) |
| 407 { |
| 408 s->state=SSL3_ST_CW_CERT_VRFY_A; |
| 409 } |
| 410 else |
| 411 { |
| 412 s->state=SSL3_ST_CW_CHANGE_A; |
| 413 s->s3->change_cipher_spec=0; |
| 414 } |
| 415 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) |
| 416 { |
| 417 s->state=SSL3_ST_CW_CHANGE_A; |
| 418 s->s3->change_cipher_spec=0; |
| 419 } |
| 420 |
| 421 s->init_num=0; |
| 422 break; |
| 423 |
| 424 case SSL3_ST_CW_CERT_VRFY_A: |
| 425 case SSL3_ST_CW_CERT_VRFY_B: |
| 426 ret=ssl3_send_client_verify(s); |
| 427 if (ret <= 0) goto end; |
| 428 s->state=SSL3_ST_CW_CHANGE_A; |
| 429 s->init_num=0; |
| 430 s->s3->change_cipher_spec=0; |
| 431 break; |
| 432 |
| 433 case SSL3_ST_CW_CHANGE_A: |
| 434 case SSL3_ST_CW_CHANGE_B: |
| 435 ret=ssl3_send_change_cipher_spec(s, |
| 436 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B); |
| 437 if (ret <= 0) goto end; |
| 438 s->state=SSL3_ST_CW_FINISHED_A; |
| 439 s->init_num=0; |
| 440 |
| 441 s->session->cipher=s->s3->tmp.new_cipher; |
| 442 #ifdef OPENSSL_NO_COMP |
| 443 s->session->compress_meth=0; |
| 444 #else |
| 445 if (s->s3->tmp.new_compression == NULL) |
| 446 s->session->compress_meth=0; |
| 447 else |
| 448 s->session->compress_meth= |
| 449 s->s3->tmp.new_compression->id; |
| 450 #endif |
| 451 if (!s->method->ssl3_enc->setup_key_block(s)) |
| 452 { |
| 453 ret= -1; |
| 454 goto end; |
| 455 } |
| 456 |
| 457 if (!s->method->ssl3_enc->change_cipher_state(s, |
| 458 SSL3_CHANGE_CIPHER_CLIENT_WRITE)) |
| 459 { |
| 460 ret= -1; |
| 461 goto end; |
| 462 } |
| 463 |
| 464 break; |
| 465 |
| 466 case SSL3_ST_CW_FINISHED_A: |
| 467 case SSL3_ST_CW_FINISHED_B: |
| 468 ret=ssl3_send_finished(s, |
| 469 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B, |
| 470 s->method->ssl3_enc->client_finished_label, |
| 471 s->method->ssl3_enc->client_finished_label_len); |
| 472 if (ret <= 0) goto end; |
| 473 s->state=SSL3_ST_CW_FLUSH; |
| 474 |
| 475 /* clear flags */ |
| 476 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER; |
| 477 if (s->hit) |
| 478 { |
| 479 s->s3->tmp.next_state=SSL_ST_OK; |
| 480 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINIS
HED) |
| 481 { |
| 482 s->state=SSL_ST_OK; |
| 483 s->s3->flags|=SSL3_FLAGS_POP_BUFFER; |
| 484 s->s3->delay_buf_pop_ret=0; |
| 485 } |
| 486 } |
| 487 else |
| 488 { |
| 489 if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHR
OUGH) && SSL_get_cipher_bits(s, NULL) >= 128 |
| 490 && s->s3->previous_server_finished_len == 0
/* no cutthrough on renegotiation (would complicate the state machine) */ |
| 491 ) |
| 492 { |
| 493 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIE
NT_FINISHED) |
| 494 { |
| 495 s->state=SSL3_ST_CUTTHROUGH_COMP
LETE; |
| 496 s->s3->flags|=SSL3_FLAGS_POP_BUF
FER; |
| 497 s->s3->delay_buf_pop_ret=0; |
| 498 } |
| 499 else |
| 500 { |
| 501 s->s3->tmp.next_state=SSL3_ST_CU
TTHROUGH_COMPLETE; |
| 502 } |
| 503 } |
| 504 else |
| 505 { |
| 506 #ifndef OPENSSL_NO_TLSEXT |
| 507 /* Allow NewSessionTicket if ticket expe
cted */ |
| 508 if (s->tlsext_ticket_expected) |
| 509 s->s3->tmp.next_state=SSL3_ST_CR
_SESSION_TICKET_A; |
| 510 else |
| 511 #endif |
| 512 s->s3->tmp.next_state=SSL3_ST_CR
_FINISHED_A; |
| 513 } |
| 514 } |
| 515 s->init_num=0; |
| 516 break; |
| 517 |
| 518 #ifndef OPENSSL_NO_TLSEXT |
| 519 case SSL3_ST_CR_SESSION_TICKET_A: |
| 520 case SSL3_ST_CR_SESSION_TICKET_B: |
| 521 ret=ssl3_get_new_session_ticket(s); |
| 522 if (ret <= 0) goto end; |
| 523 s->state=SSL3_ST_CR_FINISHED_A; |
| 524 s->init_num=0; |
| 525 break; |
| 526 |
| 527 case SSL3_ST_CR_CERT_STATUS_A: |
| 528 case SSL3_ST_CR_CERT_STATUS_B: |
| 529 ret=ssl3_get_cert_status(s); |
| 530 if (ret <= 0) goto end; |
| 531 s->state=SSL3_ST_CR_KEY_EXCH_A; |
| 532 s->init_num=0; |
| 533 break; |
| 534 #endif |
| 535 |
| 536 case SSL3_ST_CR_FINISHED_A: |
| 537 case SSL3_ST_CR_FINISHED_B: |
| 538 |
| 539 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A, |
| 540 SSL3_ST_CR_FINISHED_B); |
| 541 if (ret <= 0) goto end; |
| 542 |
| 543 if (s->hit) |
| 544 s->state=SSL3_ST_CW_CHANGE_A; |
| 545 else |
| 546 s->state=SSL_ST_OK; |
| 547 s->init_num=0; |
| 548 break; |
| 549 |
| 550 case SSL3_ST_CW_FLUSH: |
| 551 s->rwstate=SSL_WRITING; |
| 552 if (BIO_flush(s->wbio) <= 0) |
| 553 { |
| 554 ret= -1; |
| 555 goto end; |
| 556 } |
| 557 s->rwstate=SSL_NOTHING; |
| 558 s->state=s->s3->tmp.next_state; |
| 559 break; |
| 560 |
| 561 case SSL3_ST_CUTTHROUGH_COMPLETE: |
| 562 #ifndef OPENSSL_NO_TLSEXT |
| 563 /* Allow NewSessionTicket if ticket expected */ |
| 564 if (s->tlsext_ticket_expected) |
| 565 s->state=SSL3_ST_CR_SESSION_TICKET_A; |
| 566 else |
| 567 #endif |
| 568 s->state=SSL3_ST_CR_FINISHED_A; |
| 569 |
| 570 /* SSL_write() will take care of flushing buffered data
if |
| 571 * DELAY_CLIENT_FINISHED is set. |
| 572 */ |
| 573 if (!(s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)) |
| 574 ssl_free_wbio_buffer(s); |
| 575 ret = 1; |
| 576 goto end; |
| 577 /* break; */ |
| 578 |
| 579 case SSL_ST_OK: |
| 580 /* clean a few things up */ |
| 581 ssl3_cleanup_key_block(s); |
| 582 |
| 583 if (s->init_buf != NULL) |
| 584 { |
| 585 BUF_MEM_free(s->init_buf); |
| 586 s->init_buf=NULL; |
| 587 } |
| 588 |
| 589 /* If we are not 'joining' the last two packets, |
| 590 * remove the buffering now */ |
| 591 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER)) |
| 592 ssl_free_wbio_buffer(s); |
| 593 /* else do it later in ssl3_write */ |
| 594 |
| 595 s->init_num=0; |
| 596 s->new_session=0; |
| 597 |
| 598 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT); |
| 599 if (s->hit) s->ctx->stats.sess_hit++; |
| 600 |
| 601 ret=1; |
| 602 /* s->server=0; */ |
| 603 s->handshake_func=ssl3_connect; |
| 604 s->ctx->stats.sess_connect_good++; |
| 605 |
| 606 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); |
| 607 |
| 608 goto end; |
| 609 /* break; */ |
| 610 |
| 611 default: |
| 612 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE); |
| 613 ret= -1; |
| 614 goto end; |
| 615 /* break; */ |
| 616 } |
| 617 |
| 618 /* did we do anything */ |
| 619 if (!s->s3->tmp.reuse_message && !skip) |
| 620 { |
| 621 if (s->debug) |
| 622 { |
| 623 if ((ret=BIO_flush(s->wbio)) <= 0) |
| 624 goto end; |
| 625 } |
| 626 |
| 627 if ((cb != NULL) && (s->state != state)) |
| 628 { |
| 629 new_state=s->state; |
| 630 s->state=state; |
| 631 cb(s,SSL_CB_CONNECT_LOOP,1); |
| 632 s->state=new_state; |
| 633 } |
| 634 } |
| 635 skip=0; |
| 636 } |
| 637 end: |
| 638 s->in_handshake--; |
| 639 if (buf != NULL) |
| 640 BUF_MEM_free(buf); |
| 641 if (cb != NULL) |
| 642 cb(s,SSL_CB_CONNECT_EXIT,ret); |
| 643 return(ret); |
| 644 } |
| 645 |
| 646 |
| 647 int ssl3_client_hello(SSL *s) |
| 648 { |
| 649 unsigned char *buf; |
| 650 unsigned char *p,*d; |
| 651 int i; |
| 652 unsigned long Time,l; |
| 653 #ifndef OPENSSL_NO_COMP |
| 654 int j; |
| 655 SSL_COMP *comp; |
| 656 #endif |
| 657 |
| 658 buf=(unsigned char *)s->init_buf->data; |
| 659 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) |
| 660 { |
| 661 SSL_SESSION *sess = s->session; |
| 662 if ((sess == NULL) || |
| 663 (sess->ssl_version != s->version) || |
| 664 #ifdef OPENSSL_NO_TLSEXT |
| 665 !sess->session_id_length || |
| 666 #else |
| 667 (!sess->session_id_length && !sess->tlsext_tick) || |
| 668 #endif |
| 669 (sess->not_resumable)) |
| 670 { |
| 671 if (!s->session_creation_enabled) |
| 672 { |
| 673 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE
_FAILURE); |
| 674 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_SESSION_MAY
_NOT_BE_CREATED); |
| 675 goto err; |
| 676 } |
| 677 if (!ssl_get_new_session(s,0)) |
| 678 goto err; |
| 679 } |
| 680 /* else use the pre-loaded session */ |
| 681 |
| 682 p=s->s3->client_random; |
| 683 Time=(unsigned long)time(NULL); /* Time */ |
| 684 l2n(Time,p); |
| 685 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0) |
| 686 goto err; |
| 687 |
| 688 /* Do the message type and length last */ |
| 689 d=p= &(buf[4]); |
| 690 |
| 691 *(p++)=s->version>>8; |
| 692 *(p++)=s->version&0xff; |
| 693 s->client_version=s->version; |
| 694 |
| 695 /* Random stuff */ |
| 696 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE); |
| 697 p+=SSL3_RANDOM_SIZE; |
| 698 |
| 699 /* Session ID */ |
| 700 if (s->new_session) |
| 701 i=0; |
| 702 else |
| 703 i=s->session->session_id_length; |
| 704 *(p++)=i; |
| 705 if (i != 0) |
| 706 { |
| 707 if (i > (int)sizeof(s->session->session_id)) |
| 708 { |
| 709 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_E
RROR); |
| 710 goto err; |
| 711 } |
| 712 memcpy(p,s->session->session_id,i); |
| 713 p+=i; |
| 714 } |
| 715 |
| 716 /* Ciphers supported */ |
| 717 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0); |
| 718 if (i == 0) |
| 719 { |
| 720 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABL
E); |
| 721 goto err; |
| 722 } |
| 723 s2n(i,p); |
| 724 p+=i; |
| 725 |
| 726 /* COMPRESSION */ |
| 727 #ifdef OPENSSL_NO_COMP |
| 728 *(p++)=1; |
| 729 #else |
| 730 |
| 731 if ((s->options & SSL_OP_NO_COMPRESSION) |
| 732 || !s->ctx->comp_methods) |
| 733 j=0; |
| 734 else |
| 735 j=sk_SSL_COMP_num(s->ctx->comp_methods); |
| 736 *(p++)=1+j; |
| 737 for (i=0; i<j; i++) |
| 738 { |
| 739 comp=sk_SSL_COMP_value(s->ctx->comp_methods,i); |
| 740 *(p++)=comp->id; |
| 741 } |
| 742 #endif |
| 743 *(p++)=0; /* Add the NULL method */ |
| 744 |
| 745 #ifndef OPENSSL_NO_TLSEXT |
| 746 /* TLS extensions*/ |
| 747 if (ssl_prepare_clienthello_tlsext(s) <= 0) |
| 748 { |
| 749 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT)
; |
| 750 goto err; |
| 751 } |
| 752 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_
LENGTH)) == NULL) |
| 753 { |
| 754 SSLerr(SSL_F_SSL3_CLIENT_HELLO,ERR_R_INTERNAL_ERROR); |
| 755 goto err; |
| 756 } |
| 757 #endif |
| 758 |
| 759 l=(p-d); |
| 760 d=buf; |
| 761 *(d++)=SSL3_MT_CLIENT_HELLO; |
| 762 l2n3(l,d); |
| 763 |
| 764 s->state=SSL3_ST_CW_CLNT_HELLO_B; |
| 765 /* number of bytes to write */ |
| 766 s->init_num=p-buf; |
| 767 s->init_off=0; |
| 768 } |
| 769 |
| 770 /* SSL3_ST_CW_CLNT_HELLO_B */ |
| 771 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 772 err: |
| 773 return(-1); |
| 774 } |
| 775 |
| 776 int ssl3_get_server_hello(SSL *s) |
| 777 { |
| 778 STACK_OF(SSL_CIPHER) *sk; |
| 779 const SSL_CIPHER *c; |
| 780 unsigned char *p,*d; |
| 781 int i,al,ok; |
| 782 unsigned int j; |
| 783 long n; |
| 784 #ifndef OPENSSL_NO_COMP |
| 785 SSL_COMP *comp; |
| 786 #endif |
| 787 |
| 788 n=s->method->ssl_get_message(s, |
| 789 SSL3_ST_CR_SRVR_HELLO_A, |
| 790 SSL3_ST_CR_SRVR_HELLO_B, |
| 791 -1, |
| 792 20000, /* ?? */ |
| 793 &ok); |
| 794 |
| 795 if (!ok) return((int)n); |
| 796 |
| 797 if ( SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER) |
| 798 { |
| 799 if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) |
| 800 { |
| 801 if ( s->d1->send_cookie == 0) |
| 802 { |
| 803 s->s3->tmp.reuse_message = 1; |
| 804 return 1; |
| 805 } |
| 806 else /* already sent a cookie */ |
| 807 { |
| 808 al=SSL_AD_UNEXPECTED_MESSAGE; |
| 809 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MES
SAGE_TYPE); |
| 810 goto f_err; |
| 811 } |
| 812 } |
| 813 } |
| 814 |
| 815 if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) |
| 816 { |
| 817 al=SSL_AD_UNEXPECTED_MESSAGE; |
| 818 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE); |
| 819 goto f_err; |
| 820 } |
| 821 |
| 822 d=p=(unsigned char *)s->init_msg; |
| 823 |
| 824 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff))) |
| 825 { |
| 826 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION); |
| 827 s->version=(s->version&0xff00)|p[1]; |
| 828 al=SSL_AD_PROTOCOL_VERSION; |
| 829 goto f_err; |
| 830 } |
| 831 p+=2; |
| 832 |
| 833 /* load the server hello data */ |
| 834 /* load the server random */ |
| 835 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE); |
| 836 p+=SSL3_RANDOM_SIZE; |
| 837 |
| 838 /* get the session-id */ |
| 839 j= *(p++); |
| 840 |
| 841 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) |
| 842 { |
| 843 al=SSL_AD_ILLEGAL_PARAMETER; |
| 844 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_LON
G); |
| 845 goto f_err; |
| 846 } |
| 847 |
| 848 #ifndef OPENSSL_NO_TLSEXT |
| 849 /* check if we want to resume the session based on external pre-shared s
ecret */ |
| 850 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb) |
| 851 { |
| 852 SSL_CIPHER *pref_cipher=NULL; |
| 853 s->session->master_key_length=sizeof(s->session->master_key); |
| 854 if (s->tls_session_secret_cb(s, s->session->master_key, |
| 855 &s->session->master_key_length, |
| 856 NULL, &pref_cipher, |
| 857 s->tls_session_secret_cb_arg)) |
| 858 { |
| 859 s->session->cipher = pref_cipher ? |
| 860 pref_cipher : ssl_get_cipher_by_char(s, p+j); |
| 861 } |
| 862 } |
| 863 #endif /* OPENSSL_NO_TLSEXT */ |
| 864 |
| 865 if (j != 0 && j == s->session->session_id_length |
| 866 && memcmp(p,s->session->session_id,j) == 0) |
| 867 { |
| 868 if(s->sid_ctx_length != s->session->sid_ctx_length |
| 869 || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length)) |
| 870 { |
| 871 /* actually a client application bug */ |
| 872 al=SSL_AD_ILLEGAL_PARAMETER; |
| 873 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSIO
N_IN_DIFFERENT_CONTEXT); |
| 874 goto f_err; |
| 875 } |
| 876 s->hit=1; |
| 877 } |
| 878 else /* a miss or crap from the other end */ |
| 879 { |
| 880 /* If we were trying for session-id reuse, make a new |
| 881 * SSL_SESSION so we don't stuff up other people */ |
| 882 s->hit=0; |
| 883 if (s->session->session_id_length > 0) |
| 884 { |
| 885 if (!s->session_creation_enabled) |
| 886 { |
| 887 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE
_FAILURE); |
| 888 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SESSION
_MAY_NOT_BE_CREATED); |
| 889 goto err; |
| 890 } |
| 891 if (!ssl_get_new_session(s,0)) |
| 892 { |
| 893 al=SSL_AD_INTERNAL_ERROR; |
| 894 goto f_err; |
| 895 } |
| 896 } |
| 897 s->session->session_id_length=j; |
| 898 memcpy(s->session->session_id,p,j); /* j could be 0 */ |
| 899 } |
| 900 p+=j; |
| 901 c=ssl_get_cipher_by_char(s,p); |
| 902 if (c == NULL) |
| 903 { |
| 904 /* unknown cipher */ |
| 905 al=SSL_AD_ILLEGAL_PARAMETER; |
| 906 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED
); |
| 907 goto f_err; |
| 908 } |
| 909 p+=ssl_put_cipher_by_char(s,NULL,NULL); |
| 910 |
| 911 sk=ssl_get_ciphers_by_id(s); |
| 912 i=sk_SSL_CIPHER_find(sk,c); |
| 913 if (i < 0) |
| 914 { |
| 915 /* we did not say we would use this cipher */ |
| 916 al=SSL_AD_ILLEGAL_PARAMETER; |
| 917 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED); |
| 918 goto f_err; |
| 919 } |
| 920 |
| 921 /* Depending on the session caching (internal/external), the cipher |
| 922 and/or cipher_id values may not be set. Make sure that |
| 923 cipher_id is set and use it for comparison. */ |
| 924 if (s->session->cipher) |
| 925 s->session->cipher_id = s->session->cipher->id; |
| 926 if (s->hit && (s->session->cipher_id != c->id)) |
| 927 { |
| 928 /* Workaround is now obsolete */ |
| 929 #if 0 |
| 930 if (!(s->options & |
| 931 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)) |
| 932 #endif |
| 933 { |
| 934 al=SSL_AD_ILLEGAL_PARAMETER; |
| 935 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIP
HER_NOT_RETURNED); |
| 936 goto f_err; |
| 937 } |
| 938 } |
| 939 s->s3->tmp.new_cipher=c; |
| 940 if (!ssl3_digest_cached_records(s)) |
| 941 goto f_err; |
| 942 |
| 943 /* lets get the compression algorithm */ |
| 944 /* COMPRESSION */ |
| 945 #ifdef OPENSSL_NO_COMP |
| 946 if (*(p++) != 0) |
| 947 { |
| 948 al=SSL_AD_ILLEGAL_PARAMETER; |
| 949 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION
_ALGORITHM); |
| 950 goto f_err; |
| 951 } |
| 952 /* If compression is disabled we'd better not try to resume a session |
| 953 * using compression. |
| 954 */ |
| 955 if (s->session->compress_meth != 0) |
| 956 { |
| 957 al=SSL_AD_INTERNAL_ERROR; |
| 958 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_INCONSISTENT_COMPRESSIO
N); |
| 959 goto f_err; |
| 960 } |
| 961 #else |
| 962 j= *(p++); |
| 963 if (s->hit && j != s->session->compress_meth) |
| 964 { |
| 965 al=SSL_AD_ILLEGAL_PARAMETER; |
| 966 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_COMPRESSION
_ALGORITHM_NOT_RETURNED); |
| 967 goto f_err; |
| 968 } |
| 969 if (j == 0) |
| 970 comp=NULL; |
| 971 else if (s->options & SSL_OP_NO_COMPRESSION) |
| 972 { |
| 973 al=SSL_AD_ILLEGAL_PARAMETER; |
| 974 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_COMPRESSION_DISABLED); |
| 975 goto f_err; |
| 976 } |
| 977 else |
| 978 comp=ssl3_comp_find(s->ctx->comp_methods,j); |
| 979 |
| 980 if ((j != 0) && (comp == NULL)) |
| 981 { |
| 982 al=SSL_AD_ILLEGAL_PARAMETER; |
| 983 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION
_ALGORITHM); |
| 984 goto f_err; |
| 985 } |
| 986 else |
| 987 { |
| 988 s->s3->tmp.new_compression=comp; |
| 989 } |
| 990 #endif |
| 991 |
| 992 #ifndef OPENSSL_NO_TLSEXT |
| 993 /* TLS extensions*/ |
| 994 if (s->version >= SSL3_VERSION) |
| 995 { |
| 996 if (!ssl_parse_serverhello_tlsext(s,&p,d,n, &al)) |
| 997 { |
| 998 /* 'al' set by ssl_parse_serverhello_tlsext */ |
| 999 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_PARSE_TLSEXT); |
| 1000 goto f_err; |
| 1001 } |
| 1002 if (ssl_check_serverhello_tlsext(s) <= 0) |
| 1003 { |
| 1004 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SERVERHELLO_TLS
EXT); |
| 1005 goto err; |
| 1006 } |
| 1007 } |
| 1008 #endif |
| 1009 |
| 1010 if (p != (d+n)) |
| 1011 { |
| 1012 /* wrong packet length */ |
| 1013 al=SSL_AD_DECODE_ERROR; |
| 1014 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH); |
| 1015 goto f_err; |
| 1016 } |
| 1017 |
| 1018 return(1); |
| 1019 f_err: |
| 1020 ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1021 err: |
| 1022 return(-1); |
| 1023 } |
| 1024 |
| 1025 int ssl3_get_server_certificate(SSL *s) |
| 1026 { |
| 1027 int al,i,ok,ret= -1; |
| 1028 unsigned long n,nc,llen,l; |
| 1029 X509 *x=NULL; |
| 1030 const unsigned char *q,*p; |
| 1031 unsigned char *d; |
| 1032 STACK_OF(X509) *sk=NULL; |
| 1033 SESS_CERT *sc; |
| 1034 EVP_PKEY *pkey=NULL; |
| 1035 int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */ |
| 1036 |
| 1037 n=s->method->ssl_get_message(s, |
| 1038 SSL3_ST_CR_CERT_A, |
| 1039 SSL3_ST_CR_CERT_B, |
| 1040 -1, |
| 1041 s->max_cert_list, |
| 1042 &ok); |
| 1043 |
| 1044 if (!ok) return((int)n); |
| 1045 |
| 1046 if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) || |
| 1047 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) && |
| 1048 (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) |
| 1049 { |
| 1050 s->s3->tmp.reuse_message=1; |
| 1051 return(1); |
| 1052 } |
| 1053 |
| 1054 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) |
| 1055 { |
| 1056 al=SSL_AD_UNEXPECTED_MESSAGE; |
| 1057 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE)
; |
| 1058 goto f_err; |
| 1059 } |
| 1060 p=d=(unsigned char *)s->init_msg; |
| 1061 |
| 1062 if ((sk=sk_X509_new_null()) == NULL) |
| 1063 { |
| 1064 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE); |
| 1065 goto err; |
| 1066 } |
| 1067 |
| 1068 n2l3(p,llen); |
| 1069 if (llen+3 != n) |
| 1070 { |
| 1071 al=SSL_AD_DECODE_ERROR; |
| 1072 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH); |
| 1073 goto f_err; |
| 1074 } |
| 1075 for (nc=0; nc<llen; ) |
| 1076 { |
| 1077 n2l3(p,l); |
| 1078 if ((l+nc+3) > llen) |
| 1079 { |
| 1080 al=SSL_AD_DECODE_ERROR; |
| 1081 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENG
TH_MISMATCH); |
| 1082 goto f_err; |
| 1083 } |
| 1084 |
| 1085 q=p; |
| 1086 x=d2i_X509(NULL,&q,l); |
| 1087 if (x == NULL) |
| 1088 { |
| 1089 al=SSL_AD_BAD_CERTIFICATE; |
| 1090 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB)
; |
| 1091 goto f_err; |
| 1092 } |
| 1093 if (q != (p+l)) |
| 1094 { |
| 1095 al=SSL_AD_DECODE_ERROR; |
| 1096 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENG
TH_MISMATCH); |
| 1097 goto f_err; |
| 1098 } |
| 1099 if (!sk_X509_push(sk,x)) |
| 1100 { |
| 1101 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FA
ILURE); |
| 1102 goto err; |
| 1103 } |
| 1104 x=NULL; |
| 1105 nc+=l+3; |
| 1106 p=q; |
| 1107 } |
| 1108 |
| 1109 i=ssl_verify_cert_chain(s,sk); |
| 1110 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0) |
| 1111 #ifndef OPENSSL_NO_KRB5 |
| 1112 && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) && |
| 1113 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)) |
| 1114 #endif /* OPENSSL_NO_KRB5 */ |
| 1115 ) |
| 1116 { |
| 1117 al=ssl_verify_alarm_type(s->verify_result); |
| 1118 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIF
Y_FAILED); |
| 1119 goto f_err; |
| 1120 } |
| 1121 ERR_clear_error(); /* but we keep s->verify_result */ |
| 1122 |
| 1123 sc=ssl_sess_cert_new(); |
| 1124 if (sc == NULL) goto err; |
| 1125 |
| 1126 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert); |
| 1127 s->session->sess_cert=sc; |
| 1128 |
| 1129 sc->cert_chain=sk; |
| 1130 /* Inconsistency alert: cert_chain does include the peer's |
| 1131 * certificate, which we don't include in s3_srvr.c */ |
| 1132 x=sk_X509_value(sk,0); |
| 1133 sk=NULL; |
| 1134 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/ |
| 1135 |
| 1136 pkey=X509_get_pubkey(x); |
| 1137 |
| 1138 /* VRS: allow null cert if auth == KRB5 */ |
| 1139 need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) && |
| 1140 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)) |
| 1141 ? 0 : 1; |
| 1142 |
| 1143 #ifdef KSSL_DEBUG |
| 1144 printf("pkey,x = %p, %p\n", pkey,x); |
| 1145 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey)); |
| 1146 printf("cipher, alg, nc = %s, %lx, %lx, %d\n", s->s3->tmp.new_cipher->na
me, |
| 1147 s->s3->tmp.new_cipher->algorithm_mkey, s->s3->tmp.new_cipher->al
gorithm_auth, need_cert); |
| 1148 #endif /* KSSL_DEBUG */ |
| 1149 |
| 1150 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) |
| 1151 { |
| 1152 x=NULL; |
| 1153 al=SSL3_AL_FATAL; |
| 1154 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, |
| 1155 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); |
| 1156 goto f_err; |
| 1157 } |
| 1158 |
| 1159 i=ssl_cert_type(x,pkey); |
| 1160 if (need_cert && i < 0) |
| 1161 { |
| 1162 x=NULL; |
| 1163 al=SSL3_AL_FATAL; |
| 1164 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, |
| 1165 SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
| 1166 goto f_err; |
| 1167 } |
| 1168 |
| 1169 if (need_cert) |
| 1170 { |
| 1171 sc->peer_cert_type=i; |
| 1172 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509); |
| 1173 /* Why would the following ever happen? |
| 1174 * We just created sc a couple of lines ago. */ |
| 1175 if (sc->peer_pkeys[i].x509 != NULL) |
| 1176 X509_free(sc->peer_pkeys[i].x509); |
| 1177 sc->peer_pkeys[i].x509=x; |
| 1178 sc->peer_key= &(sc->peer_pkeys[i]); |
| 1179 |
| 1180 if (s->session->peer != NULL) |
| 1181 X509_free(s->session->peer); |
| 1182 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509); |
| 1183 s->session->peer=x; |
| 1184 } |
| 1185 else |
| 1186 { |
| 1187 sc->peer_cert_type=i; |
| 1188 sc->peer_key= NULL; |
| 1189 |
| 1190 if (s->session->peer != NULL) |
| 1191 X509_free(s->session->peer); |
| 1192 s->session->peer=NULL; |
| 1193 } |
| 1194 s->session->verify_result = s->verify_result; |
| 1195 |
| 1196 x=NULL; |
| 1197 ret=1; |
| 1198 |
| 1199 if (0) |
| 1200 { |
| 1201 f_err: |
| 1202 ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1203 } |
| 1204 err: |
| 1205 EVP_PKEY_free(pkey); |
| 1206 X509_free(x); |
| 1207 sk_X509_pop_free(sk,X509_free); |
| 1208 return(ret); |
| 1209 } |
| 1210 |
| 1211 int ssl3_get_key_exchange(SSL *s) |
| 1212 { |
| 1213 #ifndef OPENSSL_NO_RSA |
| 1214 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2]; |
| 1215 #endif |
| 1216 EVP_MD_CTX md_ctx; |
| 1217 unsigned char *param,*p; |
| 1218 int al,i,j,param_len,ok; |
| 1219 long n,alg_k,alg_a; |
| 1220 EVP_PKEY *pkey=NULL; |
| 1221 #ifndef OPENSSL_NO_RSA |
| 1222 RSA *rsa=NULL; |
| 1223 #endif |
| 1224 #ifndef OPENSSL_NO_DH |
| 1225 DH *dh=NULL; |
| 1226 #endif |
| 1227 #ifndef OPENSSL_NO_ECDH |
| 1228 EC_KEY *ecdh = NULL; |
| 1229 BN_CTX *bn_ctx = NULL; |
| 1230 EC_POINT *srvr_ecpoint = NULL; |
| 1231 int curve_nid = 0; |
| 1232 int encoded_pt_len = 0; |
| 1233 #endif |
| 1234 |
| 1235 /* use same message size as in ssl3_get_certificate_request() |
| 1236 * as ServerKeyExchange message may be skipped */ |
| 1237 n=s->method->ssl_get_message(s, |
| 1238 SSL3_ST_CR_KEY_EXCH_A, |
| 1239 SSL3_ST_CR_KEY_EXCH_B, |
| 1240 -1, |
| 1241 s->max_cert_list, |
| 1242 &ok); |
| 1243 if (!ok) return((int)n); |
| 1244 |
| 1245 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) |
| 1246 { |
| 1247 #ifndef OPENSSL_NO_PSK |
| 1248 /* In plain PSK ciphersuite, ServerKeyExchange can be |
| 1249 omitted if no identity hint is sent. Set |
| 1250 session->sess_cert anyway to avoid problems |
| 1251 later.*/ |
| 1252 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK) |
| 1253 { |
| 1254 s->session->sess_cert=ssl_sess_cert_new(); |
| 1255 if (s->ctx->psk_identity_hint) |
| 1256 OPENSSL_free(s->ctx->psk_identity_hint); |
| 1257 s->ctx->psk_identity_hint = NULL; |
| 1258 } |
| 1259 #endif |
| 1260 s->s3->tmp.reuse_message=1; |
| 1261 return(1); |
| 1262 } |
| 1263 |
| 1264 param=p=(unsigned char *)s->init_msg; |
| 1265 if (s->session->sess_cert != NULL) |
| 1266 { |
| 1267 #ifndef OPENSSL_NO_RSA |
| 1268 if (s->session->sess_cert->peer_rsa_tmp != NULL) |
| 1269 { |
| 1270 RSA_free(s->session->sess_cert->peer_rsa_tmp); |
| 1271 s->session->sess_cert->peer_rsa_tmp=NULL; |
| 1272 } |
| 1273 #endif |
| 1274 #ifndef OPENSSL_NO_DH |
| 1275 if (s->session->sess_cert->peer_dh_tmp) |
| 1276 { |
| 1277 DH_free(s->session->sess_cert->peer_dh_tmp); |
| 1278 s->session->sess_cert->peer_dh_tmp=NULL; |
| 1279 } |
| 1280 #endif |
| 1281 #ifndef OPENSSL_NO_ECDH |
| 1282 if (s->session->sess_cert->peer_ecdh_tmp) |
| 1283 { |
| 1284 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp); |
| 1285 s->session->sess_cert->peer_ecdh_tmp=NULL; |
| 1286 } |
| 1287 #endif |
| 1288 } |
| 1289 else |
| 1290 { |
| 1291 s->session->sess_cert=ssl_sess_cert_new(); |
| 1292 } |
| 1293 |
| 1294 param_len=0; |
| 1295 alg_k=s->s3->tmp.new_cipher->algorithm_mkey; |
| 1296 alg_a=s->s3->tmp.new_cipher->algorithm_auth; |
| 1297 EVP_MD_CTX_init(&md_ctx); |
| 1298 |
| 1299 #ifndef OPENSSL_NO_PSK |
| 1300 if (alg_k & SSL_kPSK) |
| 1301 { |
| 1302 char tmp_id_hint[PSK_MAX_IDENTITY_LEN+1]; |
| 1303 |
| 1304 al=SSL_AD_HANDSHAKE_FAILURE; |
| 1305 n2s(p,i); |
| 1306 param_len=i+2; |
| 1307 /* Store PSK identity hint for later use, hint is used |
| 1308 * in ssl3_send_client_key_exchange. Assume that the |
| 1309 * maximum length of a PSK identity hint can be as |
| 1310 * long as the maximum length of a PSK identity. */ |
| 1311 if (i > PSK_MAX_IDENTITY_LEN) |
| 1312 { |
| 1313 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, |
| 1314 SSL_R_DATA_LENGTH_TOO_LONG); |
| 1315 goto f_err; |
| 1316 } |
| 1317 if (param_len > n) |
| 1318 { |
| 1319 al=SSL_AD_DECODE_ERROR; |
| 1320 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, |
| 1321 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH); |
| 1322 goto f_err; |
| 1323 } |
| 1324 /* If received PSK identity hint contains NULL |
| 1325 * characters, the hint is truncated from the first |
| 1326 * NULL. p may not be ending with NULL, so create a |
| 1327 * NULL-terminated string. */ |
| 1328 memcpy(tmp_id_hint, p, i); |
| 1329 memset(tmp_id_hint+i, 0, PSK_MAX_IDENTITY_LEN+1-i); |
| 1330 if (s->ctx->psk_identity_hint != NULL) |
| 1331 OPENSSL_free(s->ctx->psk_identity_hint); |
| 1332 s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint); |
| 1333 if (s->ctx->psk_identity_hint == NULL) |
| 1334 { |
| 1335 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE
); |
| 1336 goto f_err; |
| 1337 } |
| 1338 |
| 1339 p+=i; |
| 1340 n-=param_len; |
| 1341 } |
| 1342 else |
| 1343 #endif /* !OPENSSL_NO_PSK */ |
| 1344 #ifndef OPENSSL_NO_RSA |
| 1345 if (alg_k & SSL_kRSA) |
| 1346 { |
| 1347 if ((rsa=RSA_new()) == NULL) |
| 1348 { |
| 1349 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE)
; |
| 1350 goto err; |
| 1351 } |
| 1352 n2s(p,i); |
| 1353 param_len=i+2; |
| 1354 if (param_len > n) |
| 1355 { |
| 1356 al=SSL_AD_DECODE_ERROR; |
| 1357 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS
_LENGTH); |
| 1358 goto f_err; |
| 1359 } |
| 1360 if (!(rsa->n=BN_bin2bn(p,i,rsa->n))) |
| 1361 { |
| 1362 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); |
| 1363 goto err; |
| 1364 } |
| 1365 p+=i; |
| 1366 |
| 1367 n2s(p,i); |
| 1368 param_len+=i+2; |
| 1369 if (param_len > n) |
| 1370 { |
| 1371 al=SSL_AD_DECODE_ERROR; |
| 1372 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGT
H); |
| 1373 goto f_err; |
| 1374 } |
| 1375 if (!(rsa->e=BN_bin2bn(p,i,rsa->e))) |
| 1376 { |
| 1377 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); |
| 1378 goto err; |
| 1379 } |
| 1380 p+=i; |
| 1381 n-=param_len; |
| 1382 |
| 1383 /* this should be because we are using an export cipher */ |
| 1384 if (alg_a & SSL_aRSA) |
| 1385 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[S
SL_PKEY_RSA_ENC].x509); |
| 1386 else |
| 1387 { |
| 1388 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR)
; |
| 1389 goto err; |
| 1390 } |
| 1391 s->session->sess_cert->peer_rsa_tmp=rsa; |
| 1392 rsa=NULL; |
| 1393 } |
| 1394 #else /* OPENSSL_NO_RSA */ |
| 1395 if (0) |
| 1396 ; |
| 1397 #endif |
| 1398 #ifndef OPENSSL_NO_DH |
| 1399 else if (alg_k & SSL_kEDH) |
| 1400 { |
| 1401 if ((dh=DH_new()) == NULL) |
| 1402 { |
| 1403 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB); |
| 1404 goto err; |
| 1405 } |
| 1406 n2s(p,i); |
| 1407 param_len=i+2; |
| 1408 if (param_len > n) |
| 1409 { |
| 1410 al=SSL_AD_DECODE_ERROR; |
| 1411 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH
); |
| 1412 goto f_err; |
| 1413 } |
| 1414 if (!(dh->p=BN_bin2bn(p,i,NULL))) |
| 1415 { |
| 1416 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); |
| 1417 goto err; |
| 1418 } |
| 1419 p+=i; |
| 1420 |
| 1421 n2s(p,i); |
| 1422 param_len+=i+2; |
| 1423 if (param_len > n) |
| 1424 { |
| 1425 al=SSL_AD_DECODE_ERROR; |
| 1426 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH
); |
| 1427 goto f_err; |
| 1428 } |
| 1429 if (!(dh->g=BN_bin2bn(p,i,NULL))) |
| 1430 { |
| 1431 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); |
| 1432 goto err; |
| 1433 } |
| 1434 p+=i; |
| 1435 |
| 1436 n2s(p,i); |
| 1437 param_len+=i+2; |
| 1438 if (param_len > n) |
| 1439 { |
| 1440 al=SSL_AD_DECODE_ERROR; |
| 1441 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_
LENGTH); |
| 1442 goto f_err; |
| 1443 } |
| 1444 if (!(dh->pub_key=BN_bin2bn(p,i,NULL))) |
| 1445 { |
| 1446 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); |
| 1447 goto err; |
| 1448 } |
| 1449 p+=i; |
| 1450 n-=param_len; |
| 1451 |
| 1452 #ifndef OPENSSL_NO_RSA |
| 1453 if (alg_a & SSL_aRSA) |
| 1454 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[S
SL_PKEY_RSA_ENC].x509); |
| 1455 #else |
| 1456 if (0) |
| 1457 ; |
| 1458 #endif |
| 1459 #ifndef OPENSSL_NO_DSA |
| 1460 else if (alg_a & SSL_aDSS) |
| 1461 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[S
SL_PKEY_DSA_SIGN].x509); |
| 1462 #endif |
| 1463 /* else anonymous DH, so no certificate or pkey. */ |
| 1464 |
| 1465 s->session->sess_cert->peer_dh_tmp=dh; |
| 1466 dh=NULL; |
| 1467 } |
| 1468 else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd)) |
| 1469 { |
| 1470 al=SSL_AD_ILLEGAL_PARAMETER; |
| 1471 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTE
D_CIPHER); |
| 1472 goto f_err; |
| 1473 } |
| 1474 #endif /* !OPENSSL_NO_DH */ |
| 1475 |
| 1476 #ifndef OPENSSL_NO_ECDH |
| 1477 else if (alg_k & SSL_kEECDH) |
| 1478 { |
| 1479 EC_GROUP *ngroup; |
| 1480 const EC_GROUP *group; |
| 1481 |
| 1482 if ((ecdh=EC_KEY_new()) == NULL) |
| 1483 { |
| 1484 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE)
; |
| 1485 goto err; |
| 1486 } |
| 1487 |
| 1488 /* Extract elliptic curve parameters and the |
| 1489 * server's ephemeral ECDH public key. |
| 1490 * Keep accumulating lengths of various components in |
| 1491 * param_len and make sure it never exceeds n. |
| 1492 */ |
| 1493 |
| 1494 /* XXX: For now we only support named (not generic) curves |
| 1495 * and the ECParameters in this case is just three bytes. |
| 1496 */ |
| 1497 param_len=3; |
| 1498 if ((param_len > n) || |
| 1499 (*p != NAMED_CURVE_TYPE) || |
| 1500 ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0)) |
| 1501 { |
| 1502 al=SSL_AD_INTERNAL_ERROR; |
| 1503 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_
ECDH_PARAMETERS); |
| 1504 goto f_err; |
| 1505 } |
| 1506 |
| 1507 ngroup = EC_GROUP_new_by_curve_name(curve_nid); |
| 1508 if (ngroup == NULL) |
| 1509 { |
| 1510 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB); |
| 1511 goto err; |
| 1512 } |
| 1513 if (EC_KEY_set_group(ecdh, ngroup) == 0) |
| 1514 { |
| 1515 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB); |
| 1516 goto err; |
| 1517 } |
| 1518 EC_GROUP_free(ngroup); |
| 1519 |
| 1520 group = EC_KEY_get0_group(ecdh); |
| 1521 |
| 1522 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && |
| 1523 (EC_GROUP_get_degree(group) > 163)) |
| 1524 { |
| 1525 al=SSL_AD_EXPORT_RESTRICTION; |
| 1526 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LAR
GE_FOR_CIPHER); |
| 1527 goto f_err; |
| 1528 } |
| 1529 |
| 1530 p+=3; |
| 1531 |
| 1532 /* Next, get the encoded ECPoint */ |
| 1533 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) || |
| 1534 ((bn_ctx = BN_CTX_new()) == NULL)) |
| 1535 { |
| 1536 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE)
; |
| 1537 goto err; |
| 1538 } |
| 1539 |
| 1540 encoded_pt_len = *p; /* length of encoded point */ |
| 1541 p+=1; |
| 1542 param_len += (1 + encoded_pt_len); |
| 1543 if ((param_len > n) || |
| 1544 (EC_POINT_oct2point(group, srvr_ecpoint, |
| 1545 p, encoded_pt_len, bn_ctx) == 0)) |
| 1546 { |
| 1547 al=SSL_AD_DECODE_ERROR; |
| 1548 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_ECPOINT); |
| 1549 goto f_err; |
| 1550 } |
| 1551 |
| 1552 n-=param_len; |
| 1553 p+=encoded_pt_len; |
| 1554 |
| 1555 /* The ECC/TLS specification does not mention |
| 1556 * the use of DSA to sign ECParameters in the server |
| 1557 * key exchange message. We do support RSA and ECDSA. |
| 1558 */ |
| 1559 if (0) ; |
| 1560 #ifndef OPENSSL_NO_RSA |
| 1561 else if (alg_a & SSL_aRSA) |
| 1562 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[S
SL_PKEY_RSA_ENC].x509); |
| 1563 #endif |
| 1564 #ifndef OPENSSL_NO_ECDSA |
| 1565 else if (alg_a & SSL_aECDSA) |
| 1566 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[S
SL_PKEY_ECC].x509); |
| 1567 #endif |
| 1568 /* else anonymous ECDH, so no certificate or pkey. */ |
| 1569 EC_KEY_set_public_key(ecdh, srvr_ecpoint); |
| 1570 s->session->sess_cert->peer_ecdh_tmp=ecdh; |
| 1571 ecdh=NULL; |
| 1572 BN_CTX_free(bn_ctx); |
| 1573 bn_ctx = NULL; |
| 1574 EC_POINT_free(srvr_ecpoint); |
| 1575 srvr_ecpoint = NULL; |
| 1576 } |
| 1577 else if (alg_k) |
| 1578 { |
| 1579 al=SSL_AD_UNEXPECTED_MESSAGE; |
| 1580 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE); |
| 1581 goto f_err; |
| 1582 } |
| 1583 #endif /* !OPENSSL_NO_ECDH */ |
| 1584 |
| 1585 |
| 1586 /* p points to the next byte, there are 'n' bytes left */ |
| 1587 |
| 1588 /* if it was signed, check the signature */ |
| 1589 if (pkey != NULL) |
| 1590 { |
| 1591 n2s(p,i); |
| 1592 n-=2; |
| 1593 j=EVP_PKEY_size(pkey); |
| 1594 |
| 1595 if ((i != n) || (n > j) || (n <= 0)) |
| 1596 { |
| 1597 /* wrong packet length */ |
| 1598 al=SSL_AD_DECODE_ERROR; |
| 1599 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE
_LENGTH); |
| 1600 goto f_err; |
| 1601 } |
| 1602 |
| 1603 #ifndef OPENSSL_NO_RSA |
| 1604 if (pkey->type == EVP_PKEY_RSA) |
| 1605 { |
| 1606 int num; |
| 1607 |
| 1608 j=0; |
| 1609 q=md_buf; |
| 1610 for (num=2; num > 0; num--) |
| 1611 { |
| 1612 EVP_DigestInit_ex(&md_ctx,(num == 2) |
| 1613 ?s->ctx->md5:s->ctx->sha1, NULL); |
| 1614 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[
0]),SSL3_RANDOM_SIZE); |
| 1615 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[
0]),SSL3_RANDOM_SIZE); |
| 1616 EVP_DigestUpdate(&md_ctx,param,param_len); |
| 1617 EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i)
; |
| 1618 q+=i; |
| 1619 j+=i; |
| 1620 } |
| 1621 i=RSA_verify(NID_md5_sha1, md_buf, j, p, n, |
| 1622 pkey->pkey.rsa); |
| 1623 if (i < 0) |
| 1624 { |
| 1625 al=SSL_AD_DECRYPT_ERROR; |
| 1626 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA
_DECRYPT); |
| 1627 goto f_err; |
| 1628 } |
| 1629 if (i == 0) |
| 1630 { |
| 1631 /* bad signature */ |
| 1632 al=SSL_AD_DECRYPT_ERROR; |
| 1633 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIG
NATURE); |
| 1634 goto f_err; |
| 1635 } |
| 1636 } |
| 1637 else |
| 1638 #endif |
| 1639 #ifndef OPENSSL_NO_DSA |
| 1640 if (pkey->type == EVP_PKEY_DSA) |
| 1641 { |
| 1642 /* lets do DSS */ |
| 1643 EVP_VerifyInit_ex(&md_ctx,EVP_dss1(), NULL); |
| 1644 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3
_RANDOM_SIZE); |
| 1645 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3
_RANDOM_SIZE); |
| 1646 EVP_VerifyUpdate(&md_ctx,param,param_len); |
| 1647 if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0) |
| 1648 { |
| 1649 /* bad signature */ |
| 1650 al=SSL_AD_DECRYPT_ERROR; |
| 1651 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIG
NATURE); |
| 1652 goto f_err; |
| 1653 } |
| 1654 } |
| 1655 else |
| 1656 #endif |
| 1657 #ifndef OPENSSL_NO_ECDSA |
| 1658 if (pkey->type == EVP_PKEY_EC) |
| 1659 { |
| 1660 /* let's do ECDSA */ |
| 1661 EVP_VerifyInit_ex(&md_ctx,EVP_ecdsa(), NULL); |
| 1662 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3
_RANDOM_SIZE); |
| 1663 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3
_RANDOM_SIZE); |
| 1664 EVP_VerifyUpdate(&md_ctx,param,param_len); |
| 1665 if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0) |
| 1666 { |
| 1667 /* bad signature */ |
| 1668 al=SSL_AD_DECRYPT_ERROR; |
| 1669 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIG
NATURE); |
| 1670 goto f_err; |
| 1671 } |
| 1672 } |
| 1673 else |
| 1674 #endif |
| 1675 { |
| 1676 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR)
; |
| 1677 goto err; |
| 1678 } |
| 1679 } |
| 1680 else |
| 1681 { |
| 1682 if (!(alg_a & SSL_aNULL) && !(alg_k & SSL_kPSK)) |
| 1683 /* aNULL or kPSK do not need public keys */ |
| 1684 { |
| 1685 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR)
; |
| 1686 goto err; |
| 1687 } |
| 1688 /* still data left over */ |
| 1689 if (n != 0) |
| 1690 { |
| 1691 al=SSL_AD_DECODE_ERROR; |
| 1692 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_M
ESSAGE); |
| 1693 goto f_err; |
| 1694 } |
| 1695 } |
| 1696 EVP_PKEY_free(pkey); |
| 1697 EVP_MD_CTX_cleanup(&md_ctx); |
| 1698 return(1); |
| 1699 f_err: |
| 1700 ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1701 err: |
| 1702 EVP_PKEY_free(pkey); |
| 1703 #ifndef OPENSSL_NO_RSA |
| 1704 if (rsa != NULL) |
| 1705 RSA_free(rsa); |
| 1706 #endif |
| 1707 #ifndef OPENSSL_NO_DH |
| 1708 if (dh != NULL) |
| 1709 DH_free(dh); |
| 1710 #endif |
| 1711 #ifndef OPENSSL_NO_ECDH |
| 1712 BN_CTX_free(bn_ctx); |
| 1713 EC_POINT_free(srvr_ecpoint); |
| 1714 if (ecdh != NULL) |
| 1715 EC_KEY_free(ecdh); |
| 1716 #endif |
| 1717 EVP_MD_CTX_cleanup(&md_ctx); |
| 1718 return(-1); |
| 1719 } |
| 1720 |
| 1721 int ssl3_get_certificate_request(SSL *s) |
| 1722 { |
| 1723 int ok,ret=0; |
| 1724 unsigned long n,nc,l; |
| 1725 unsigned int llen,ctype_num,i; |
| 1726 X509_NAME *xn=NULL; |
| 1727 const unsigned char *p,*q; |
| 1728 unsigned char *d; |
| 1729 STACK_OF(X509_NAME) *ca_sk=NULL; |
| 1730 |
| 1731 n=s->method->ssl_get_message(s, |
| 1732 SSL3_ST_CR_CERT_REQ_A, |
| 1733 SSL3_ST_CR_CERT_REQ_B, |
| 1734 -1, |
| 1735 s->max_cert_list, |
| 1736 &ok); |
| 1737 |
| 1738 if (!ok) return((int)n); |
| 1739 |
| 1740 s->s3->tmp.cert_req=0; |
| 1741 |
| 1742 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) |
| 1743 { |
| 1744 s->s3->tmp.reuse_message=1; |
| 1745 return(1); |
| 1746 } |
| 1747 |
| 1748 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) |
| 1749 { |
| 1750 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE); |
| 1751 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TY
PE); |
| 1752 goto err; |
| 1753 } |
| 1754 |
| 1755 /* TLS does not like anon-DH with client cert */ |
| 1756 if (s->version > SSL3_VERSION) |
| 1757 { |
| 1758 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) |
| 1759 { |
| 1760 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAG
E); |
| 1761 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIE
NT_CERT_REQ_WITH_ANON_CIPHER); |
| 1762 goto err; |
| 1763 } |
| 1764 } |
| 1765 |
| 1766 p=d=(unsigned char *)s->init_msg; |
| 1767 |
| 1768 if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL) |
| 1769 { |
| 1770 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE); |
| 1771 goto err; |
| 1772 } |
| 1773 |
| 1774 /* get the certificate types */ |
| 1775 ctype_num= *(p++); |
| 1776 if (ctype_num > SSL3_CT_NUMBER) |
| 1777 ctype_num=SSL3_CT_NUMBER; |
| 1778 for (i=0; i<ctype_num; i++) |
| 1779 s->s3->tmp.ctype[i]= p[i]; |
| 1780 p+=ctype_num; |
| 1781 |
| 1782 /* get the CA RDNs */ |
| 1783 n2s(p,llen); |
| 1784 #if 0 |
| 1785 { |
| 1786 FILE *out; |
| 1787 out=fopen("/tmp/vsign.der","w"); |
| 1788 fwrite(p,1,llen,out); |
| 1789 fclose(out); |
| 1790 } |
| 1791 #endif |
| 1792 |
| 1793 if ((llen+ctype_num+2+1) != n) |
| 1794 { |
| 1795 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
| 1796 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH)
; |
| 1797 goto err; |
| 1798 } |
| 1799 |
| 1800 for (nc=0; nc<llen; ) |
| 1801 { |
| 1802 n2s(p,l); |
| 1803 if ((l+nc+2) > llen) |
| 1804 { |
| 1805 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) |
| 1806 goto cont; /* netscape bugs */ |
| 1807 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
| 1808 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TO
O_LONG); |
| 1809 goto err; |
| 1810 } |
| 1811 |
| 1812 q=p; |
| 1813 |
| 1814 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL) |
| 1815 { |
| 1816 /* If netscape tolerance is on, ignore errors */ |
| 1817 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG) |
| 1818 goto cont; |
| 1819 else |
| 1820 { |
| 1821 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ER
ROR); |
| 1822 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_
ASN1_LIB); |
| 1823 goto err; |
| 1824 } |
| 1825 } |
| 1826 |
| 1827 if (q != (p+l)) |
| 1828 { |
| 1829 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
| 1830 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LE
NGTH_MISMATCH); |
| 1831 goto err; |
| 1832 } |
| 1833 if (!sk_X509_NAME_push(ca_sk,xn)) |
| 1834 { |
| 1835 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_F
AILURE); |
| 1836 goto err; |
| 1837 } |
| 1838 |
| 1839 p+=l; |
| 1840 nc+=l+2; |
| 1841 } |
| 1842 |
| 1843 if (0) |
| 1844 { |
| 1845 cont: |
| 1846 ERR_clear_error(); |
| 1847 } |
| 1848 |
| 1849 /* we should setup a certificate to return.... */ |
| 1850 s->s3->tmp.cert_req=1; |
| 1851 s->s3->tmp.ctype_num=ctype_num; |
| 1852 if (s->s3->tmp.ca_names != NULL) |
| 1853 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free); |
| 1854 s->s3->tmp.ca_names=ca_sk; |
| 1855 ca_sk=NULL; |
| 1856 |
| 1857 ret=1; |
| 1858 err: |
| 1859 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free); |
| 1860 return(ret); |
| 1861 } |
| 1862 |
| 1863 static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b) |
| 1864 { |
| 1865 return(X509_NAME_cmp(*a,*b)); |
| 1866 } |
| 1867 #ifndef OPENSSL_NO_TLSEXT |
| 1868 int ssl3_get_new_session_ticket(SSL *s) |
| 1869 { |
| 1870 int ok,al,ret=0, ticklen; |
| 1871 long n; |
| 1872 const unsigned char *p; |
| 1873 unsigned char *d; |
| 1874 |
| 1875 n=s->method->ssl_get_message(s, |
| 1876 SSL3_ST_CR_SESSION_TICKET_A, |
| 1877 SSL3_ST_CR_SESSION_TICKET_B, |
| 1878 -1, |
| 1879 16384, |
| 1880 &ok); |
| 1881 |
| 1882 if (!ok) |
| 1883 return((int)n); |
| 1884 |
| 1885 if (s->s3->tmp.message_type == SSL3_MT_FINISHED) |
| 1886 { |
| 1887 s->s3->tmp.reuse_message=1; |
| 1888 return(1); |
| 1889 } |
| 1890 if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET) |
| 1891 { |
| 1892 al=SSL_AD_UNEXPECTED_MESSAGE; |
| 1893 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_BAD_MESSAGE_TYPE)
; |
| 1894 goto f_err; |
| 1895 } |
| 1896 if (n < 6) |
| 1897 { |
| 1898 /* need at least ticket_lifetime_hint + ticket length */ |
| 1899 al = SSL_AD_DECODE_ERROR; |
| 1900 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH); |
| 1901 goto f_err; |
| 1902 } |
| 1903 |
| 1904 p=d=(unsigned char *)s->init_msg; |
| 1905 n2l(p, s->session->tlsext_tick_lifetime_hint); |
| 1906 n2s(p, ticklen); |
| 1907 /* ticket_lifetime_hint + ticket_length + ticket */ |
| 1908 if (ticklen + 6 != n) |
| 1909 { |
| 1910 al = SSL_AD_DECODE_ERROR; |
| 1911 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH); |
| 1912 goto f_err; |
| 1913 } |
| 1914 if (s->session->tlsext_tick) |
| 1915 { |
| 1916 OPENSSL_free(s->session->tlsext_tick); |
| 1917 s->session->tlsext_ticklen = 0; |
| 1918 } |
| 1919 s->session->tlsext_tick = OPENSSL_malloc(ticklen); |
| 1920 if (!s->session->tlsext_tick) |
| 1921 { |
| 1922 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,ERR_R_MALLOC_FAILURE); |
| 1923 goto err; |
| 1924 } |
| 1925 memcpy(s->session->tlsext_tick, p, ticklen); |
| 1926 s->session->tlsext_ticklen = ticklen; |
| 1927 /* There are two ways to detect a resumed ticket sesion. |
| 1928 * One is to set an appropriate session ID and then the server |
| 1929 * must return a match in ServerHello. This allows the normal |
| 1930 * client session ID matching to work and we know much |
| 1931 * earlier that the ticket has been accepted. |
| 1932 * |
| 1933 * The other way is to set zero length session ID when the |
| 1934 * ticket is presented and rely on the handshake to determine |
| 1935 * session resumption. |
| 1936 * |
| 1937 * We choose the former approach because this fits in with |
| 1938 * assumptions elsewhere in OpenSSL. The session ID is set |
| 1939 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the |
| 1940 * ticket. |
| 1941 */ |
| 1942 EVP_Digest(p, ticklen, |
| 1943 s->session->session_id, &s->session->session_id_length, |
| 1944 #ifndef OPENSSL_NO_SHA256 |
| 1945 EVP_sha256(), NULL); |
| 1946 #else |
| 1947 EVP_sha1(), NULL); |
| 1948 #endif |
| 1949 ret=1; |
| 1950 return(ret); |
| 1951 f_err: |
| 1952 ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1953 err: |
| 1954 return(-1); |
| 1955 } |
| 1956 |
| 1957 int ssl3_get_cert_status(SSL *s) |
| 1958 { |
| 1959 int ok, al; |
| 1960 unsigned long resplen,n; |
| 1961 const unsigned char *p; |
| 1962 |
| 1963 n=s->method->ssl_get_message(s, |
| 1964 SSL3_ST_CR_CERT_STATUS_A, |
| 1965 SSL3_ST_CR_CERT_STATUS_B, |
| 1966 SSL3_MT_CERTIFICATE_STATUS, |
| 1967 16384, |
| 1968 &ok); |
| 1969 |
| 1970 if (!ok) return((int)n); |
| 1971 if (n < 4) |
| 1972 { |
| 1973 /* need at least status type + length */ |
| 1974 al = SSL_AD_DECODE_ERROR; |
| 1975 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH); |
| 1976 goto f_err; |
| 1977 } |
| 1978 p = (unsigned char *)s->init_msg; |
| 1979 if (*p++ != TLSEXT_STATUSTYPE_ocsp) |
| 1980 { |
| 1981 al = SSL_AD_DECODE_ERROR; |
| 1982 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_UNSUPPORTED_STATUS_TYPE)
; |
| 1983 goto f_err; |
| 1984 } |
| 1985 n2l3(p, resplen); |
| 1986 if (resplen + 4 != n) |
| 1987 { |
| 1988 al = SSL_AD_DECODE_ERROR; |
| 1989 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH); |
| 1990 goto f_err; |
| 1991 } |
| 1992 if (s->tlsext_ocsp_resp) |
| 1993 OPENSSL_free(s->tlsext_ocsp_resp); |
| 1994 s->tlsext_ocsp_resp = BUF_memdup(p, resplen); |
| 1995 if (!s->tlsext_ocsp_resp) |
| 1996 { |
| 1997 al = SSL_AD_INTERNAL_ERROR; |
| 1998 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE); |
| 1999 goto f_err; |
| 2000 } |
| 2001 s->tlsext_ocsp_resplen = resplen; |
| 2002 if (s->ctx->tlsext_status_cb) |
| 2003 { |
| 2004 int ret; |
| 2005 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); |
| 2006 if (ret == 0) |
| 2007 { |
| 2008 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; |
| 2009 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_INVALID_STATUS_R
ESPONSE); |
| 2010 goto f_err; |
| 2011 } |
| 2012 if (ret < 0) |
| 2013 { |
| 2014 al = SSL_AD_INTERNAL_ERROR; |
| 2015 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE); |
| 2016 goto f_err; |
| 2017 } |
| 2018 } |
| 2019 return 1; |
| 2020 f_err: |
| 2021 ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 2022 return(-1); |
| 2023 } |
| 2024 #endif |
| 2025 |
| 2026 int ssl3_get_server_done(SSL *s) |
| 2027 { |
| 2028 int ok,ret=0; |
| 2029 long n; |
| 2030 |
| 2031 n=s->method->ssl_get_message(s, |
| 2032 SSL3_ST_CR_SRVR_DONE_A, |
| 2033 SSL3_ST_CR_SRVR_DONE_B, |
| 2034 SSL3_MT_SERVER_DONE, |
| 2035 30, /* should be very small, like 0 :-) */ |
| 2036 &ok); |
| 2037 |
| 2038 if (!ok) return((int)n); |
| 2039 if (n > 0) |
| 2040 { |
| 2041 /* should contain no data */ |
| 2042 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
| 2043 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH); |
| 2044 return -1; |
| 2045 } |
| 2046 ret=1; |
| 2047 return(ret); |
| 2048 } |
| 2049 |
| 2050 |
| 2051 int ssl3_send_client_key_exchange(SSL *s) |
| 2052 { |
| 2053 unsigned char *p,*d; |
| 2054 int n; |
| 2055 unsigned long alg_k; |
| 2056 #ifndef OPENSSL_NO_RSA |
| 2057 unsigned char *q; |
| 2058 EVP_PKEY *pkey=NULL; |
| 2059 #endif |
| 2060 #ifndef OPENSSL_NO_KRB5 |
| 2061 KSSL_ERR kssl_err; |
| 2062 #endif /* OPENSSL_NO_KRB5 */ |
| 2063 #ifndef OPENSSL_NO_ECDH |
| 2064 EC_KEY *clnt_ecdh = NULL; |
| 2065 const EC_POINT *srvr_ecpoint = NULL; |
| 2066 EVP_PKEY *srvr_pub_pkey = NULL; |
| 2067 unsigned char *encodedPoint = NULL; |
| 2068 int encoded_pt_len = 0; |
| 2069 BN_CTX * bn_ctx = NULL; |
| 2070 #endif |
| 2071 |
| 2072 if (s->state == SSL3_ST_CW_KEY_EXCH_A) |
| 2073 { |
| 2074 d=(unsigned char *)s->init_buf->data; |
| 2075 p= &(d[4]); |
| 2076 |
| 2077 alg_k=s->s3->tmp.new_cipher->algorithm_mkey; |
| 2078 |
| 2079 /* Fool emacs indentation */ |
| 2080 if (0) {} |
| 2081 #ifndef OPENSSL_NO_RSA |
| 2082 else if (alg_k & SSL_kRSA) |
| 2083 { |
| 2084 RSA *rsa; |
| 2085 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; |
| 2086 |
| 2087 if (s->session->sess_cert->peer_rsa_tmp != NULL) |
| 2088 rsa=s->session->sess_cert->peer_rsa_tmp; |
| 2089 else |
| 2090 { |
| 2091 pkey=X509_get_pubkey(s->session->sess_cert->peer
_pkeys[SSL_PKEY_RSA_ENC].x509); |
| 2092 if ((pkey == NULL) || |
| 2093 (pkey->type != EVP_PKEY_RSA) || |
| 2094 (pkey->pkey.rsa == NULL)) |
| 2095 { |
| 2096 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHAN
GE,ERR_R_INTERNAL_ERROR); |
| 2097 goto err; |
| 2098 } |
| 2099 rsa=pkey->pkey.rsa; |
| 2100 EVP_PKEY_free(pkey); |
| 2101 } |
| 2102 |
| 2103 tmp_buf[0]=s->client_version>>8; |
| 2104 tmp_buf[1]=s->client_version&0xff; |
| 2105 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0) |
| 2106 goto err; |
| 2107 |
| 2108 s->session->master_key_length=sizeof tmp_buf; |
| 2109 |
| 2110 q=p; |
| 2111 /* Fix buf for TLS and beyond */ |
| 2112 if (s->version > SSL3_VERSION) |
| 2113 p+=2; |
| 2114 n=RSA_public_encrypt(sizeof tmp_buf, |
| 2115 tmp_buf,p,rsa,RSA_PKCS1_PADDING); |
| 2116 #ifdef PKCS1_CHECK |
| 2117 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++; |
| 2118 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70; |
| 2119 #endif |
| 2120 if (n <= 0) |
| 2121 { |
| 2122 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R
_BAD_RSA_ENCRYPT); |
| 2123 goto err; |
| 2124 } |
| 2125 |
| 2126 /* Fix buf for TLS and beyond */ |
| 2127 if (s->version > SSL3_VERSION) |
| 2128 { |
| 2129 s2n(n,q); |
| 2130 n+=2; |
| 2131 } |
| 2132 |
| 2133 s->session->master_key_length= |
| 2134 s->method->ssl3_enc->generate_master_secret(s, |
| 2135 s->session->master_key, |
| 2136 tmp_buf,sizeof tmp_buf); |
| 2137 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf); |
| 2138 } |
| 2139 #endif |
| 2140 #ifndef OPENSSL_NO_KRB5 |
| 2141 else if (alg_k & SSL_kKRB5) |
| 2142 { |
| 2143 krb5_error_code krb5rc; |
| 2144 KSSL_CTX *kssl_ctx = s->kssl_ctx; |
| 2145 /* krb5_data krb5_ap_req; */ |
| 2146 krb5_data *enc_ticket; |
| 2147 krb5_data authenticator, *authp = NULL; |
| 2148 EVP_CIPHER_CTX ciph_ctx; |
| 2149 const EVP_CIPHER *enc = NULL; |
| 2150 unsigned char iv[EVP_MAX_IV_LENGTH]; |
| 2151 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; |
| 2152 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH |
| 2153 + EVP_MAX_IV_LENGTH]; |
| 2154 int padl, outl = sizeof(epms); |
| 2155 |
| 2156 EVP_CIPHER_CTX_init(&ciph_ctx); |
| 2157 |
| 2158 #ifdef KSSL_DEBUG |
| 2159 printf("ssl3_send_client_key_exchange(%lx & %lx)\n", |
| 2160 alg_k, SSL_kKRB5); |
| 2161 #endif /* KSSL_DEBUG */ |
| 2162 |
| 2163 authp = NULL; |
| 2164 #ifdef KRB5SENDAUTH |
| 2165 if (KRB5SENDAUTH) authp = &authenticator; |
| 2166 #endif /* KRB5SENDAUTH */ |
| 2167 |
| 2168 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, |
| 2169 &kssl_err); |
| 2170 enc = kssl_map_enc(kssl_ctx->enctype); |
| 2171 if (enc == NULL) |
| 2172 goto err; |
| 2173 #ifdef KSSL_DEBUG |
| 2174 { |
| 2175 printf("kssl_cget_tkt rtn %d\n", krb5rc); |
| 2176 if (krb5rc && kssl_err.text) |
| 2177 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text); |
| 2178 } |
| 2179 #endif /* KSSL_DEBUG */ |
| 2180 |
| 2181 if (krb5rc) |
| 2182 { |
| 2183 ssl3_send_alert(s,SSL3_AL_FATAL, |
| 2184 SSL_AD_HANDSHAKE_FAILURE); |
| 2185 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2186 kssl_err.reason); |
| 2187 goto err; |
| 2188 } |
| 2189 |
| 2190 /* 20010406 VRS - Earlier versions used KRB5 AP_REQ |
| 2191 ** in place of RFC 2712 KerberosWrapper, as in: |
| 2192 ** |
| 2193 ** Send ticket (copy to *p, set n = length) |
| 2194 ** n = krb5_ap_req.length; |
| 2195 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length); |
| 2196 ** if (krb5_ap_req.data) |
| 2197 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req); |
| 2198 ** |
| 2199 ** Now using real RFC 2712 KerberosWrapper |
| 2200 ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>) |
| 2201 ** Note: 2712 "opaque" types are here replaced |
| 2202 ** with a 2-byte length followed by the value. |
| 2203 ** Example: |
| 2204 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms |
| 2205 ** Where "xx xx" = length bytes. Shown here with |
| 2206 ** optional authenticator omitted. |
| 2207 */ |
| 2208 |
| 2209 /* KerberosWrapper.Ticket */ |
| 2210 s2n(enc_ticket->length,p); |
| 2211 memcpy(p, enc_ticket->data, enc_ticket->length); |
| 2212 p+= enc_ticket->length; |
| 2213 n = enc_ticket->length + 2; |
| 2214 |
| 2215 /* KerberosWrapper.Authenticator */ |
| 2216 if (authp && authp->length) |
| 2217 { |
| 2218 s2n(authp->length,p); |
| 2219 memcpy(p, authp->data, authp->length); |
| 2220 p+= authp->length; |
| 2221 n+= authp->length + 2; |
| 2222 |
| 2223 free(authp->data); |
| 2224 authp->data = NULL; |
| 2225 authp->length = 0; |
| 2226 } |
| 2227 else |
| 2228 { |
| 2229 s2n(0,p);/* null authenticator length */ |
| 2230 n+=2; |
| 2231 } |
| 2232 |
| 2233 tmp_buf[0]=s->client_version>>8; |
| 2234 tmp_buf[1]=s->client_version&0xff; |
| 2235 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0) |
| 2236 goto err; |
| 2237 |
| 2238 /* 20010420 VRS. Tried it this way; failed. |
| 2239 ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL); |
| 2240 ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx, |
| 2241 ** kssl_ctx->length); |
| 2242 ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv); |
| 2243 */ |
| 2244 |
| 2245 memset(iv, 0, sizeof iv); /* per RFC 1510 */ |
| 2246 EVP_EncryptInit_ex(&ciph_ctx,enc, NULL, |
| 2247 kssl_ctx->key,iv); |
| 2248 EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf, |
| 2249 sizeof tmp_buf); |
| 2250 EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl); |
| 2251 outl += padl; |
| 2252 if (outl > (int)sizeof epms) |
| 2253 { |
| 2254 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_
R_INTERNAL_ERROR); |
| 2255 goto err; |
| 2256 } |
| 2257 EVP_CIPHER_CTX_cleanup(&ciph_ctx); |
| 2258 |
| 2259 /* KerberosWrapper.EncryptedPreMasterSecret */ |
| 2260 s2n(outl,p); |
| 2261 memcpy(p, epms, outl); |
| 2262 p+=outl; |
| 2263 n+=outl + 2; |
| 2264 |
| 2265 s->session->master_key_length= |
| 2266 s->method->ssl3_enc->generate_master_secret(s, |
| 2267 s->session->master_key, |
| 2268 tmp_buf, sizeof tmp_buf); |
| 2269 |
| 2270 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf); |
| 2271 OPENSSL_cleanse(epms, outl); |
| 2272 } |
| 2273 #endif |
| 2274 #ifndef OPENSSL_NO_DH |
| 2275 else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) |
| 2276 { |
| 2277 DH *dh_srvr,*dh_clnt; |
| 2278 |
| 2279 if (s->session->sess_cert == NULL) |
| 2280 { |
| 2281 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTE
D_MESSAGE); |
| 2282 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R
_UNEXPECTED_MESSAGE); |
| 2283 goto err; |
| 2284 } |
| 2285 |
| 2286 if (s->session->sess_cert->peer_dh_tmp != NULL) |
| 2287 dh_srvr=s->session->sess_cert->peer_dh_tmp; |
| 2288 else |
| 2289 { |
| 2290 /* we get them from the cert */ |
| 2291 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE
_FAILURE); |
| 2292 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R
_UNABLE_TO_FIND_DH_PARAMETERS); |
| 2293 goto err; |
| 2294 } |
| 2295 |
| 2296 /* generate a new random key */ |
| 2297 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL) |
| 2298 { |
| 2299 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R
_DH_LIB); |
| 2300 goto err; |
| 2301 } |
| 2302 if (!DH_generate_key(dh_clnt)) |
| 2303 { |
| 2304 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R
_DH_LIB); |
| 2305 DH_free(dh_clnt); |
| 2306 goto err; |
| 2307 } |
| 2308 |
| 2309 /* use the 'p' output buffer for the DH key, but |
| 2310 * make sure to clear it out afterwards */ |
| 2311 |
| 2312 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt); |
| 2313 |
| 2314 if (n <= 0) |
| 2315 { |
| 2316 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R
_DH_LIB); |
| 2317 DH_free(dh_clnt); |
| 2318 goto err; |
| 2319 } |
| 2320 |
| 2321 /* generate master key from the result */ |
| 2322 s->session->master_key_length= |
| 2323 s->method->ssl3_enc->generate_master_secret(s, |
| 2324 s->session->master_key,p,n); |
| 2325 /* clean up */ |
| 2326 memset(p,0,n); |
| 2327 |
| 2328 /* send off the data */ |
| 2329 n=BN_num_bytes(dh_clnt->pub_key); |
| 2330 s2n(n,p); |
| 2331 BN_bn2bin(dh_clnt->pub_key,p); |
| 2332 n+=2; |
| 2333 |
| 2334 DH_free(dh_clnt); |
| 2335 |
| 2336 /* perhaps clean things up a bit EAY EAY EAY EAY*/ |
| 2337 } |
| 2338 #endif |
| 2339 |
| 2340 #ifndef OPENSSL_NO_ECDH |
| 2341 else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) |
| 2342 { |
| 2343 const EC_GROUP *srvr_group = NULL; |
| 2344 EC_KEY *tkey; |
| 2345 int ecdh_clnt_cert = 0; |
| 2346 int field_size = 0; |
| 2347 |
| 2348 /* Did we send out the client's |
| 2349 * ECDH share for use in premaster |
| 2350 * computation as part of client certificate? |
| 2351 * If so, set ecdh_clnt_cert to 1. |
| 2352 */ |
| 2353 if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NUL
L)) |
| 2354 { |
| 2355 /* XXX: For now, we do not support client |
| 2356 * authentication using ECDH certificates. |
| 2357 * To add such support, one needs to add |
| 2358 * code that checks for appropriate |
| 2359 * conditions and sets ecdh_clnt_cert to 1. |
| 2360 * For example, the cert have an ECC |
| 2361 * key on the same curve as the server's |
| 2362 * and the key should be authorized for |
| 2363 * key agreement. |
| 2364 * |
| 2365 * One also needs to add code in ssl3_connect |
| 2366 * to skip sending the certificate verify |
| 2367 * message. |
| 2368 * |
| 2369 * if ((s->cert->key->privatekey != NULL) && |
| 2370 * (s->cert->key->privatekey->type == |
| 2371 * EVP_PKEY_EC) && ...) |
| 2372 * ecdh_clnt_cert = 1; |
| 2373 */ |
| 2374 } |
| 2375 |
| 2376 if (s->session->sess_cert->peer_ecdh_tmp != NULL) |
| 2377 { |
| 2378 tkey = s->session->sess_cert->peer_ecdh_tmp; |
| 2379 } |
| 2380 else |
| 2381 { |
| 2382 /* Get the Server Public Key from Cert */ |
| 2383 srvr_pub_pkey = X509_get_pubkey(s->session-> \ |
| 2384 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); |
| 2385 if ((srvr_pub_pkey == NULL) || |
| 2386 (srvr_pub_pkey->type != EVP_PKEY_EC) || |
| 2387 (srvr_pub_pkey->pkey.ec == NULL)) |
| 2388 { |
| 2389 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHAN
GE, |
| 2390 ERR_R_INTERNAL_ERROR); |
| 2391 goto err; |
| 2392 } |
| 2393 |
| 2394 tkey = srvr_pub_pkey->pkey.ec; |
| 2395 } |
| 2396 |
| 2397 srvr_group = EC_KEY_get0_group(tkey); |
| 2398 srvr_ecpoint = EC_KEY_get0_public_key(tkey); |
| 2399 |
| 2400 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) |
| 2401 { |
| 2402 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2403 ERR_R_INTERNAL_ERROR); |
| 2404 goto err; |
| 2405 } |
| 2406 |
| 2407 if ((clnt_ecdh=EC_KEY_new()) == NULL) |
| 2408 { |
| 2409 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R
_MALLOC_FAILURE); |
| 2410 goto err; |
| 2411 } |
| 2412 |
| 2413 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) |
| 2414 { |
| 2415 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R
_EC_LIB); |
| 2416 goto err; |
| 2417 } |
| 2418 if (ecdh_clnt_cert) |
| 2419 { |
| 2420 /* Reuse key info from our certificate |
| 2421 * We only need our private key to perform |
| 2422 * the ECDH computation. |
| 2423 */ |
| 2424 const BIGNUM *priv_key; |
| 2425 tkey = s->cert->key->privatekey->pkey.ec; |
| 2426 priv_key = EC_KEY_get0_private_key(tkey); |
| 2427 if (priv_key == NULL) |
| 2428 { |
| 2429 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHAN
GE,ERR_R_MALLOC_FAILURE); |
| 2430 goto err; |
| 2431 } |
| 2432 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)
) |
| 2433 { |
| 2434 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHAN
GE,ERR_R_EC_LIB); |
| 2435 goto err; |
| 2436 } |
| 2437 } |
| 2438 else |
| 2439 { |
| 2440 /* Generate a new ECDH key pair */ |
| 2441 if (!(EC_KEY_generate_key(clnt_ecdh))) |
| 2442 { |
| 2443 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHAN
GE, ERR_R_ECDH_LIB); |
| 2444 goto err; |
| 2445 } |
| 2446 } |
| 2447 |
| 2448 /* use the 'p' output buffer for the ECDH key, but |
| 2449 * make sure to clear it out afterwards |
| 2450 */ |
| 2451 |
| 2452 field_size = EC_GROUP_get_degree(srvr_group); |
| 2453 if (field_size <= 0) |
| 2454 { |
| 2455 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2456 ERR_R_ECDH_LIB); |
| 2457 goto err; |
| 2458 } |
| 2459 n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, cl
nt_ecdh, NULL); |
| 2460 if (n <= 0) |
| 2461 { |
| 2462 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2463 ERR_R_ECDH_LIB); |
| 2464 goto err; |
| 2465 } |
| 2466 |
| 2467 /* generate master key from the result */ |
| 2468 s->session->master_key_length = s->method->ssl3_enc \ |
| 2469 -> generate_master_secret(s, |
| 2470 s->session->master_key, |
| 2471 p, n); |
| 2472 |
| 2473 memset(p, 0, n); /* clean up */ |
| 2474 |
| 2475 if (ecdh_clnt_cert) |
| 2476 { |
| 2477 /* Send empty client key exch message */ |
| 2478 n = 0; |
| 2479 } |
| 2480 else |
| 2481 { |
| 2482 /* First check the size of encoding and |
| 2483 * allocate memory accordingly. |
| 2484 */ |
| 2485 encoded_pt_len = |
| 2486 EC_POINT_point2oct(srvr_group, |
| 2487 EC_KEY_get0_public_key(clnt_ecdh), |
| 2488 POINT_CONVERSION_UNCOMPRESSED, |
| 2489 NULL, 0, NULL); |
| 2490 |
| 2491 encodedPoint = (unsigned char *) |
| 2492 OPENSSL_malloc(encoded_pt_len * |
| 2493 sizeof(unsigned char)); |
| 2494 bn_ctx = BN_CTX_new(); |
| 2495 if ((encodedPoint == NULL) || |
| 2496 (bn_ctx == NULL)) |
| 2497 { |
| 2498 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHAN
GE,ERR_R_MALLOC_FAILURE); |
| 2499 goto err; |
| 2500 } |
| 2501 |
| 2502 /* Encode the public key */ |
| 2503 n = EC_POINT_point2oct(srvr_group, |
| 2504 EC_KEY_get0_public_key(clnt_ecdh), |
| 2505 POINT_CONVERSION_UNCOMPRESSED, |
| 2506 encodedPoint, encoded_pt_len, bn_ctx); |
| 2507 |
| 2508 *p = n; /* length of encoded point */ |
| 2509 /* Encoded point will be copied here */ |
| 2510 p += 1; |
| 2511 /* copy the point */ |
| 2512 memcpy((unsigned char *)p, encodedPoint, n); |
| 2513 /* increment n to account for length field */ |
| 2514 n += 1; |
| 2515 } |
| 2516 |
| 2517 /* Free allocated memory */ |
| 2518 BN_CTX_free(bn_ctx); |
| 2519 if (encodedPoint != NULL) OPENSSL_free(encodedPoint); |
| 2520 if (clnt_ecdh != NULL) |
| 2521 EC_KEY_free(clnt_ecdh); |
| 2522 EVP_PKEY_free(srvr_pub_pkey); |
| 2523 } |
| 2524 #endif /* !OPENSSL_NO_ECDH */ |
| 2525 else if (alg_k & SSL_kGOST) |
| 2526 { |
| 2527 /* GOST key exchange message creation */ |
| 2528 EVP_PKEY_CTX *pkey_ctx; |
| 2529 X509 *peer_cert; |
| 2530 size_t msglen; |
| 2531 unsigned int md_len; |
| 2532 int keytype; |
| 2533 unsigned char premaster_secret[32],shared_ukm[32], tmp[2
56]; |
| 2534 EVP_MD_CTX *ukm_hash; |
| 2535 EVP_PKEY *pub_key; |
| 2536 |
| 2537 /* Get server sertificate PKEY and create ctx from it */ |
| 2538 peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL
_PKEY_GOST01)].x509; |
| 2539 if (!peer_cert) |
| 2540 peer_cert=s->session->sess_cert->peer_pkeys[(key
type=SSL_PKEY_GOST94)].x509; |
| 2541 if (!peer_cert) { |
| 2542 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHAN
GE,SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER); |
| 2543 goto err; |
| 2544 } |
| 2545 |
| 2546 pkey_ctx=EVP_PKEY_CTX_new(pub_key=X509_get_pubkey(peer_c
ert),NULL); |
| 2547 /* If we have send a certificate, and certificate key |
| 2548 |
| 2549 * parameters match those of server certificate, use |
| 2550 * certificate key for key exchange |
| 2551 */ |
| 2552 |
| 2553 /* Otherwise, generate ephemeral key pair */ |
| 2554 |
| 2555 EVP_PKEY_encrypt_init(pkey_ctx); |
| 2556 /* Generate session key */ |
| 2557 RAND_bytes(premaster_secret,32); |
| 2558 /* If we have client certificate, use its secret as peer
key */ |
| 2559 if (s->s3->tmp.cert_req && s->cert->key->privatekey) { |
| 2560 if (EVP_PKEY_derive_set_peer(pkey_ctx,s->cert->k
ey->privatekey) <=0) { |
| 2561 /* If there was an error - just ignore i
t. Ephemeral key |
| 2562 * would be used |
| 2563 */ |
| 2564 ERR_clear_error(); |
| 2565 } |
| 2566 } |
| 2567 /* Compute shared IV and store it in algorithm-specific |
| 2568 * context data */ |
| 2569 ukm_hash = EVP_MD_CTX_create(); |
| 2570 EVP_DigestInit(ukm_hash,EVP_get_digestbynid(NID_id_GostR
3411_94)); |
| 2571 EVP_DigestUpdate(ukm_hash,s->s3->client_random,SSL3_RAND
OM_SIZE); |
| 2572 EVP_DigestUpdate(ukm_hash,s->s3->server_random,SSL3_RAND
OM_SIZE); |
| 2573 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len); |
| 2574 EVP_MD_CTX_destroy(ukm_hash); |
| 2575 if (EVP_PKEY_CTX_ctrl(pkey_ctx,-1,EVP_PKEY_OP_ENCRYPT,EV
P_PKEY_CTRL_SET_IV, |
| 2576 8,shared_ukm)<0) { |
| 2577 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHAN
GE, |
| 2578 SSL_R_LIBRARY_BUG); |
| 2579 goto err; |
| 2580 } |
| 2581 /* Make GOST keytransport blob message */ |
| 2582 /*Encapsulate it into sequence */ |
| 2583 *(p++)=V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED; |
| 2584 msglen=255; |
| 2585 if (EVP_PKEY_encrypt(pkey_ctx,tmp,&msglen,premaster_secr
et,32)<0) { |
| 2586 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2587 SSL_R_LIBRARY_BUG); |
| 2588 goto err; |
| 2589 } |
| 2590 if (msglen >= 0x80) |
| 2591 { |
| 2592 *(p++)=0x81; |
| 2593 *(p++)= msglen & 0xff; |
| 2594 n=msglen+3; |
| 2595 } |
| 2596 else |
| 2597 { |
| 2598 *(p++)= msglen & 0xff; |
| 2599 n=msglen+2; |
| 2600 } |
| 2601 memcpy(p, tmp, msglen); |
| 2602 /* Check if pubkey from client certificate was used */ |
| 2603 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PE
ER_KEY, 2, NULL) > 0) |
| 2604 { |
| 2605 /* Set flag "skip certificate verify" */ |
| 2606 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY; |
| 2607 } |
| 2608 EVP_PKEY_CTX_free(pkey_ctx); |
| 2609 s->session->master_key_length= |
| 2610 s->method->ssl3_enc->generate_master_secret(s, |
| 2611 s->session->master_key,premaster_secret,
32); |
| 2612 EVP_PKEY_free(pub_key); |
| 2613 |
| 2614 } |
| 2615 #ifndef OPENSSL_NO_PSK |
| 2616 else if (alg_k & SSL_kPSK) |
| 2617 { |
| 2618 char identity[PSK_MAX_IDENTITY_LEN]; |
| 2619 unsigned char *t = NULL; |
| 2620 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4]; |
| 2621 unsigned int pre_ms_len = 0, psk_len = 0; |
| 2622 int psk_err = 1; |
| 2623 |
| 2624 n = 0; |
| 2625 if (s->psk_client_callback == NULL) |
| 2626 { |
| 2627 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2628 SSL_R_PSK_NO_CLIENT_CB); |
| 2629 goto err; |
| 2630 } |
| 2631 |
| 2632 psk_len = s->psk_client_callback(s, s->ctx->psk_identity
_hint, |
| 2633 identity, PSK_MAX_IDENTITY_LEN, |
| 2634 psk_or_pre_ms, sizeof(psk_or_pre_ms)); |
| 2635 if (psk_len > PSK_MAX_PSK_LEN) |
| 2636 { |
| 2637 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2638 ERR_R_INTERNAL_ERROR); |
| 2639 goto psk_err; |
| 2640 } |
| 2641 else if (psk_len == 0) |
| 2642 { |
| 2643 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2644 SSL_R_PSK_IDENTITY_NOT_FOUND); |
| 2645 goto psk_err; |
| 2646 } |
| 2647 |
| 2648 /* create PSK pre_master_secret */ |
| 2649 pre_ms_len = 2+psk_len+2+psk_len; |
| 2650 t = psk_or_pre_ms; |
| 2651 memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len)
; |
| 2652 s2n(psk_len, t); |
| 2653 memset(t, 0, psk_len); |
| 2654 t+=psk_len; |
| 2655 s2n(psk_len, t); |
| 2656 |
| 2657 if (s->session->psk_identity_hint != NULL) |
| 2658 OPENSSL_free(s->session->psk_identity_hint); |
| 2659 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_i
dentity_hint); |
| 2660 if (s->ctx->psk_identity_hint != NULL && |
| 2661 s->session->psk_identity_hint == NULL) |
| 2662 { |
| 2663 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2664 ERR_R_MALLOC_FAILURE); |
| 2665 goto psk_err; |
| 2666 } |
| 2667 |
| 2668 if (s->session->psk_identity != NULL) |
| 2669 OPENSSL_free(s->session->psk_identity); |
| 2670 s->session->psk_identity = BUF_strdup(identity); |
| 2671 if (s->session->psk_identity == NULL) |
| 2672 { |
| 2673 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2674 ERR_R_MALLOC_FAILURE); |
| 2675 goto psk_err; |
| 2676 } |
| 2677 |
| 2678 s->session->master_key_length = |
| 2679 s->method->ssl3_enc->generate_master_secret(s, |
| 2680 s->session->master_key, |
| 2681 psk_or_pre_ms, pre_ms_len); |
| 2682 n = strlen(identity); |
| 2683 s2n(n, p); |
| 2684 memcpy(p, identity, n); |
| 2685 n+=2; |
| 2686 psk_err = 0; |
| 2687 psk_err: |
| 2688 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN); |
| 2689 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms)); |
| 2690 if (psk_err != 0) |
| 2691 { |
| 2692 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHA
KE_FAILURE); |
| 2693 goto err; |
| 2694 } |
| 2695 } |
| 2696 #endif |
| 2697 else |
| 2698 { |
| 2699 ssl3_send_alert(s, SSL3_AL_FATAL, |
| 2700 SSL_AD_HANDSHAKE_FAILURE); |
| 2701 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2702 ERR_R_INTERNAL_ERROR); |
| 2703 goto err; |
| 2704 } |
| 2705 |
| 2706 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE; |
| 2707 l2n3(n,d); |
| 2708 |
| 2709 s->state=SSL3_ST_CW_KEY_EXCH_B; |
| 2710 /* number of bytes to write */ |
| 2711 s->init_num=n+4; |
| 2712 s->init_off=0; |
| 2713 } |
| 2714 |
| 2715 /* SSL3_ST_CW_KEY_EXCH_B */ |
| 2716 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 2717 err: |
| 2718 #ifndef OPENSSL_NO_ECDH |
| 2719 BN_CTX_free(bn_ctx); |
| 2720 if (encodedPoint != NULL) OPENSSL_free(encodedPoint); |
| 2721 if (clnt_ecdh != NULL) |
| 2722 EC_KEY_free(clnt_ecdh); |
| 2723 EVP_PKEY_free(srvr_pub_pkey); |
| 2724 #endif |
| 2725 return(-1); |
| 2726 } |
| 2727 |
| 2728 int ssl3_send_client_verify(SSL *s) |
| 2729 { |
| 2730 unsigned char *p,*d; |
| 2731 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; |
| 2732 EVP_PKEY *pkey; |
| 2733 EVP_PKEY_CTX *pctx=NULL; |
| 2734 #ifndef OPENSSL_NO_RSA |
| 2735 unsigned u=0; |
| 2736 #endif |
| 2737 unsigned long n; |
| 2738 int j; |
| 2739 |
| 2740 if (s->state == SSL3_ST_CW_CERT_VRFY_A) |
| 2741 { |
| 2742 d=(unsigned char *)s->init_buf->data; |
| 2743 p= &(d[4]); |
| 2744 pkey=s->cert->key->privatekey; |
| 2745 /* Create context from key and test if sha1 is allowed as digest */ |
| 2746 pctx = EVP_PKEY_CTX_new(pkey,NULL); |
| 2747 EVP_PKEY_sign_init(pctx); |
| 2748 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1())>0) |
| 2749 { |
| 2750 s->method->ssl3_enc->cert_verify_mac(s, |
| 2751 NID_sha1, |
| 2752 &(data[MD5_DIGEST_LENGTH])); |
| 2753 } |
| 2754 else |
| 2755 { |
| 2756 ERR_clear_error(); |
| 2757 } |
| 2758 #ifndef OPENSSL_NO_RSA |
| 2759 if (pkey->type == EVP_PKEY_RSA) |
| 2760 { |
| 2761 s->method->ssl3_enc->cert_verify_mac(s, |
| 2762 NID_md5, |
| 2763 &(data[0])); |
| 2764 if (RSA_sign(NID_md5_sha1, data, |
| 2765 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, |
| 2766 &(p[2]), &u, pkey->pkey.rsa) <= 0 ) |
| 2767 { |
| 2768 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_L
IB); |
| 2769 goto err; |
| 2770 } |
| 2771 s2n(u,p); |
| 2772 n=u+2; |
| 2773 } |
| 2774 else |
| 2775 #endif |
| 2776 #ifndef OPENSSL_NO_DSA |
| 2777 if (pkey->type == EVP_PKEY_DSA) |
| 2778 { |
| 2779 if (!DSA_sign(pkey->save_type, |
| 2780 &(data[MD5_DIGEST_LENGTH]), |
| 2781 SHA_DIGEST_LENGTH,&(p[2]), |
| 2782 (unsigned int *)&j,pkey->pkey.dsa)) |
| 2783 { |
| 2784 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_L
IB); |
| 2785 goto err; |
| 2786 } |
| 2787 s2n(j,p); |
| 2788 n=j+2; |
| 2789 } |
| 2790 else |
| 2791 #endif |
| 2792 #ifndef OPENSSL_NO_ECDSA |
| 2793 if (pkey->type == EVP_PKEY_EC) |
| 2794 { |
| 2795 if (!ECDSA_sign(pkey->save_type, |
| 2796 &(data[MD5_DIGEST_LENGTH]), |
| 2797 SHA_DIGEST_LENGTH,&(p[2]), |
| 2798 (unsigned int *)&j,pkey->pkey.ec)) |
| 2799 { |
| 2800 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, |
| 2801 ERR_R_ECDSA_LIB); |
| 2802 goto err; |
| 2803 } |
| 2804 s2n(j,p); |
| 2805 n=j+2; |
| 2806 } |
| 2807 else |
| 2808 #endif |
| 2809 if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_Go
stR3410_2001) |
| 2810 { |
| 2811 unsigned char signbuf[64]; |
| 2812 int i; |
| 2813 size_t sigsize=64; |
| 2814 s->method->ssl3_enc->cert_verify_mac(s, |
| 2815 NID_id_GostR3411_94, |
| 2816 data); |
| 2817 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) { |
| 2818 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, |
| 2819 ERR_R_INTERNAL_ERROR); |
| 2820 goto err; |
| 2821 } |
| 2822 for (i=63,j=0; i>=0; j++, i--) { |
| 2823 p[2+j]=signbuf[i]; |
| 2824 } |
| 2825 s2n(j,p); |
| 2826 n=j+2; |
| 2827 } |
| 2828 else |
| 2829 { |
| 2830 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERRO
R); |
| 2831 goto err; |
| 2832 } |
| 2833 *(d++)=SSL3_MT_CERTIFICATE_VERIFY; |
| 2834 l2n3(n,d); |
| 2835 |
| 2836 s->state=SSL3_ST_CW_CERT_VRFY_B; |
| 2837 s->init_num=(int)n+4; |
| 2838 s->init_off=0; |
| 2839 } |
| 2840 EVP_PKEY_CTX_free(pctx); |
| 2841 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 2842 err: |
| 2843 EVP_PKEY_CTX_free(pctx); |
| 2844 return(-1); |
| 2845 } |
| 2846 |
| 2847 int ssl3_send_client_certificate(SSL *s) |
| 2848 { |
| 2849 X509 *x509=NULL; |
| 2850 EVP_PKEY *pkey=NULL; |
| 2851 int i; |
| 2852 unsigned long l; |
| 2853 |
| 2854 if (s->state == SSL3_ST_CW_CERT_A) |
| 2855 { |
| 2856 if ((s->cert == NULL) || |
| 2857 (s->cert->key->x509 == NULL) || |
| 2858 (s->cert->key->privatekey == NULL)) |
| 2859 s->state=SSL3_ST_CW_CERT_B; |
| 2860 else |
| 2861 s->state=SSL3_ST_CW_CERT_C; |
| 2862 } |
| 2863 |
| 2864 /* We need to get a client cert */ |
| 2865 if (s->state == SSL3_ST_CW_CERT_B) |
| 2866 { |
| 2867 /* If we get an error, we need to |
| 2868 * ssl->rwstate=SSL_X509_LOOKUP; return(-1); |
| 2869 * We then get retied later */ |
| 2870 i=0; |
| 2871 i = ssl_do_client_cert_cb(s, &x509, &pkey); |
| 2872 if (i < 0) |
| 2873 { |
| 2874 s->rwstate=SSL_X509_LOOKUP; |
| 2875 return(-1); |
| 2876 } |
| 2877 s->rwstate=SSL_NOTHING; |
| 2878 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) |
| 2879 { |
| 2880 s->state=SSL3_ST_CW_CERT_B; |
| 2881 if ( !SSL_use_certificate(s,x509) || |
| 2882 !SSL_use_PrivateKey(s,pkey)) |
| 2883 i=0; |
| 2884 } |
| 2885 else if (i == 1) |
| 2886 { |
| 2887 i=0; |
| 2888 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA
_RETURNED_BY_CALLBACK); |
| 2889 } |
| 2890 |
| 2891 if (x509 != NULL) X509_free(x509); |
| 2892 if (pkey != NULL) EVP_PKEY_free(pkey); |
| 2893 if (i == 0) |
| 2894 { |
| 2895 if (s->version == SSL3_VERSION) |
| 2896 { |
| 2897 s->s3->tmp.cert_req=0; |
| 2898 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERT
IFICATE); |
| 2899 return(1); |
| 2900 } |
| 2901 else |
| 2902 { |
| 2903 s->s3->tmp.cert_req=2; |
| 2904 } |
| 2905 } |
| 2906 |
| 2907 /* Ok, we have a cert */ |
| 2908 s->state=SSL3_ST_CW_CERT_C; |
| 2909 } |
| 2910 |
| 2911 if (s->state == SSL3_ST_CW_CERT_C) |
| 2912 { |
| 2913 s->state=SSL3_ST_CW_CERT_D; |
| 2914 l=ssl3_output_cert_chain(s, |
| 2915 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509); |
| 2916 s->init_num=(int)l; |
| 2917 s->init_off=0; |
| 2918 } |
| 2919 /* SSL3_ST_CW_CERT_D */ |
| 2920 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 2921 } |
| 2922 |
| 2923 #define has_bits(i,m) (((i)&(m)) == (m)) |
| 2924 |
| 2925 int ssl3_check_cert_and_algorithm(SSL *s) |
| 2926 { |
| 2927 int i,idx; |
| 2928 long alg_k,alg_a; |
| 2929 EVP_PKEY *pkey=NULL; |
| 2930 SESS_CERT *sc; |
| 2931 #ifndef OPENSSL_NO_RSA |
| 2932 RSA *rsa; |
| 2933 #endif |
| 2934 #ifndef OPENSSL_NO_DH |
| 2935 DH *dh; |
| 2936 #endif |
| 2937 |
| 2938 alg_k=s->s3->tmp.new_cipher->algorithm_mkey; |
| 2939 alg_a=s->s3->tmp.new_cipher->algorithm_auth; |
| 2940 |
| 2941 /* we don't have a certificate */ |
| 2942 if ((alg_a & (SSL_aDH|SSL_aNULL|SSL_aKRB5)) || (alg_k & SSL_kPSK)) |
| 2943 return(1); |
| 2944 |
| 2945 sc=s->session->sess_cert; |
| 2946 if (sc == NULL) |
| 2947 { |
| 2948 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR)
; |
| 2949 goto err; |
| 2950 } |
| 2951 |
| 2952 #ifndef OPENSSL_NO_RSA |
| 2953 rsa=s->session->sess_cert->peer_rsa_tmp; |
| 2954 #endif |
| 2955 #ifndef OPENSSL_NO_DH |
| 2956 dh=s->session->sess_cert->peer_dh_tmp; |
| 2957 #endif |
| 2958 |
| 2959 /* This is the passed certificate */ |
| 2960 |
| 2961 idx=sc->peer_cert_type; |
| 2962 #ifndef OPENSSL_NO_ECDH |
| 2963 if (idx == SSL_PKEY_ECC) |
| 2964 { |
| 2965 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, |
| 2966 s->s3->tmp.new_cipher) == 0) |
| 2967 { /* check failed */ |
| 2968 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_BAD_ECC
_CERT); |
| 2969 goto f_err; |
| 2970 } |
| 2971 else |
| 2972 { |
| 2973 return 1; |
| 2974 } |
| 2975 } |
| 2976 #endif |
| 2977 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509); |
| 2978 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey); |
| 2979 EVP_PKEY_free(pkey); |
| 2980 |
| 2981 |
| 2982 /* Check that we have a certificate if we require one */ |
| 2983 if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN)) |
| 2984 { |
| 2985 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIG
NING_CERT); |
| 2986 goto f_err; |
| 2987 } |
| 2988 #ifndef OPENSSL_NO_DSA |
| 2989 else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN)) |
| 2990 { |
| 2991 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIG
NING_CERT); |
| 2992 goto f_err; |
| 2993 } |
| 2994 #endif |
| 2995 #ifndef OPENSSL_NO_RSA |
| 2996 if ((alg_k & SSL_kRSA) && |
| 2997 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL))) |
| 2998 { |
| 2999 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENC
RYPTING_CERT); |
| 3000 goto f_err; |
| 3001 } |
| 3002 #endif |
| 3003 #ifndef OPENSSL_NO_DH |
| 3004 if ((alg_k & SSL_kEDH) && |
| 3005 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL))) |
| 3006 { |
| 3007 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY)
; |
| 3008 goto f_err; |
| 3009 } |
| 3010 else if ((alg_k & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA)) |
| 3011 { |
| 3012 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_
CERT); |
| 3013 goto f_err; |
| 3014 } |
| 3015 #ifndef OPENSSL_NO_DSA |
| 3016 else if ((alg_k & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA)) |
| 3017 { |
| 3018 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_
CERT); |
| 3019 goto f_err; |
| 3020 } |
| 3021 #endif |
| 3022 #endif |
| 3023 |
| 3024 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP)) |
| 3025 { |
| 3026 #ifndef OPENSSL_NO_RSA |
| 3027 if (alg_k & SSL_kRSA) |
| 3028 { |
| 3029 if (rsa == NULL |
| 3030 || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->
tmp.new_cipher)) |
| 3031 { |
| 3032 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R
_MISSING_EXPORT_TMP_RSA_KEY); |
| 3033 goto f_err; |
| 3034 } |
| 3035 } |
| 3036 else |
| 3037 #endif |
| 3038 #ifndef OPENSSL_NO_DH |
| 3039 if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) |
| 3040 { |
| 3041 if (dh == NULL |
| 3042 || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3
->tmp.new_cipher)) |
| 3043 { |
| 3044 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R
_MISSING_EXPORT_TMP_DH_KEY); |
| 3045 goto f_err; |
| 3046 } |
| 3047 } |
| 3048 else |
| 3049 #endif |
| 3050 { |
| 3051 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN
_KEY_EXCHANGE_TYPE); |
| 3052 goto f_err; |
| 3053 } |
| 3054 } |
| 3055 return(1); |
| 3056 f_err: |
| 3057 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); |
| 3058 err: |
| 3059 return(0); |
| 3060 } |
| 3061 |
| 3062 /* Check to see if handshake is full or resumed. Usually this is just a |
| 3063 * case of checking to see if a cache hit has occurred. In the case of |
| 3064 * session tickets we have to check the next message to be sure. |
| 3065 */ |
| 3066 |
| 3067 #ifndef OPENSSL_NO_TLSEXT |
| 3068 int ssl3_check_finished(SSL *s) |
| 3069 { |
| 3070 int ok; |
| 3071 long n; |
| 3072 /* If we have no ticket it cannot be a resumed session. */ |
| 3073 if (!s->session->tlsext_tick) |
| 3074 return 1; |
| 3075 /* this function is called when we really expect a Certificate |
| 3076 * message, so permit appropriate message length */ |
| 3077 n=s->method->ssl_get_message(s, |
| 3078 SSL3_ST_CR_CERT_A, |
| 3079 SSL3_ST_CR_CERT_B, |
| 3080 -1, |
| 3081 s->max_cert_list, |
| 3082 &ok); |
| 3083 if (!ok) return((int)n); |
| 3084 s->s3->tmp.reuse_message = 1; |
| 3085 if ((s->s3->tmp.message_type == SSL3_MT_FINISHED) |
| 3086 || (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET)) |
| 3087 return 2; |
| 3088 |
| 3089 return 1; |
| 3090 } |
| 3091 #endif |
| 3092 |
| 3093 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) |
| 3094 { |
| 3095 int i = 0; |
| 3096 #ifndef OPENSSL_NO_ENGINE |
| 3097 if (s->ctx->client_cert_engine) |
| 3098 { |
| 3099 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s, |
| 3100 SSL_get_client_CA_list(s), |
| 3101 px509, ppkey, NULL, NULL, NULL); |
| 3102 if (i != 0) |
| 3103 return i; |
| 3104 } |
| 3105 #endif |
| 3106 if (s->ctx->client_cert_cb) |
| 3107 i = s->ctx->client_cert_cb(s,px509,ppkey); |
| 3108 return i; |
| 3109 } |
OLD | NEW |