Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | |
| 153 | #include <openssl/buf.h> |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame^] | 154 | #include <openssl/bytestring.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 155 | #include <openssl/rand.h> |
| 156 | #include <openssl/obj.h> |
| 157 | #include <openssl/evp.h> |
| 158 | #include <openssl/mem.h> |
| 159 | #include <openssl/md5.h> |
| 160 | #include <openssl/dh.h> |
| 161 | #include <openssl/bn.h> |
| 162 | #include <openssl/engine.h> |
| 163 | #include <openssl/x509.h> |
| 164 | |
| 165 | #include "ssl_locl.h" |
| 166 | #include "../crypto/dh/internal.h" |
| 167 | |
| 168 | static const SSL_METHOD *ssl3_get_client_method(int ver); |
| 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 | void (*cb)(const SSL *ssl,int type,int val)=NULL; |
| 187 | int ret= -1; |
| 188 | int new_state,state,skip=0; |
| 189 | |
| 190 | ERR_clear_error(); |
| 191 | ERR_clear_system_error(); |
| 192 | |
| 193 | if (s->info_callback != NULL) |
| 194 | cb=s->info_callback; |
| 195 | else if (s->ctx->info_callback != NULL) |
| 196 | cb=s->ctx->info_callback; |
| 197 | |
| 198 | s->in_handshake++; |
| 199 | if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); |
| 200 | |
| 201 | #ifndef OPENSSL_NO_HEARTBEATS |
| 202 | /* If we're awaiting a HeartbeatResponse, pretend we |
| 203 | * already got and don't await it anymore, because |
| 204 | * Heartbeats don't make sense during handshakes anyway. |
| 205 | */ |
| 206 | if (s->tlsext_hb_pending) |
| 207 | { |
| 208 | s->tlsext_hb_pending = 0; |
| 209 | s->tlsext_hb_seq++; |
| 210 | } |
| 211 | #endif |
| 212 | |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 213 | if (SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) |
| 214 | { |
| 215 | /* Send app data along with CCS/Finished */ |
| 216 | s->s3->flags |= SSL3_FLAGS_DELAY_CLIENT_FINISHED; |
| 217 | } |
| 218 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 219 | for (;;) |
| 220 | { |
| 221 | state=s->state; |
| 222 | |
| 223 | switch(s->state) |
| 224 | { |
| 225 | case SSL_ST_RENEGOTIATE: |
| 226 | s->renegotiate=1; |
| 227 | s->state=SSL_ST_CONNECT; |
| 228 | s->ctx->stats.sess_connect_renegotiate++; |
| 229 | /* break */ |
| 230 | case SSL_ST_BEFORE: |
| 231 | case SSL_ST_CONNECT: |
| 232 | case SSL_ST_BEFORE|SSL_ST_CONNECT: |
| 233 | case SSL_ST_OK|SSL_ST_CONNECT: |
| 234 | |
| 235 | s->server=0; |
| 236 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); |
| 237 | |
| 238 | if ((s->version & 0xff00 ) != 0x0300) |
| 239 | { |
| 240 | OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR); |
| 241 | ret = -1; |
| 242 | goto end; |
| 243 | } |
| 244 | |
| 245 | /* s->version=SSL3_VERSION; */ |
| 246 | s->type=SSL_ST_CONNECT; |
| 247 | |
| 248 | if (s->init_buf == NULL) |
| 249 | { |
| 250 | if ((buf=BUF_MEM_new()) == NULL) |
| 251 | { |
| 252 | ret= -1; |
| 253 | goto end; |
| 254 | } |
| 255 | if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) |
| 256 | { |
| 257 | ret= -1; |
| 258 | goto end; |
| 259 | } |
| 260 | s->init_buf=buf; |
| 261 | buf=NULL; |
| 262 | } |
| 263 | |
| 264 | if (!ssl3_setup_buffers(s)) { ret= -1; goto end; } |
| 265 | |
| 266 | /* setup buffing BIO */ |
| 267 | if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; } |
| 268 | |
| 269 | /* don't push the buffering BIO quite yet */ |
| 270 | |
| 271 | ssl3_init_finished_mac(s); |
| 272 | |
| 273 | s->state=SSL3_ST_CW_CLNT_HELLO_A; |
| 274 | s->ctx->stats.sess_connect++; |
| 275 | s->init_num=0; |
| 276 | break; |
| 277 | |
| 278 | case SSL3_ST_CW_CLNT_HELLO_A: |
| 279 | case SSL3_ST_CW_CLNT_HELLO_B: |
| 280 | |
| 281 | s->shutdown=0; |
| 282 | ret=ssl3_client_hello(s); |
| 283 | if (ret <= 0) goto end; |
| 284 | s->state=SSL3_ST_CR_SRVR_HELLO_A; |
| 285 | s->init_num=0; |
| 286 | |
| 287 | /* turn on buffering for the next lot of output */ |
| 288 | if (s->bbio != s->wbio) |
| 289 | s->wbio=BIO_push(s->bbio,s->wbio); |
| 290 | |
| 291 | break; |
| 292 | |
| 293 | case SSL3_ST_CR_SRVR_HELLO_A: |
| 294 | case SSL3_ST_CR_SRVR_HELLO_B: |
| 295 | ret=ssl3_get_server_hello(s); |
| 296 | if (ret <= 0) goto end; |
| 297 | |
| 298 | if (s->hit) |
| 299 | { |
| 300 | s->state=SSL3_ST_CR_FINISHED_A; |
| 301 | #ifndef OPENSSL_NO_TLSEXT |
| 302 | if (s->tlsext_ticket_expected) |
| 303 | { |
| 304 | /* receive renewed session ticket */ |
| 305 | s->state=SSL3_ST_CR_SESSION_TICKET_A; |
| 306 | } |
| 307 | #endif |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | #ifndef OPENSSL_NO_TLSEXT |
| 312 | /* The server hello indicated that |
| 313 | * an audit proof would follow. */ |
| 314 | if (s->s3->tlsext_authz_server_promised) |
| 315 | s->state=SSL3_ST_CR_SUPPLEMENTAL_DATA_A; |
| 316 | else |
| 317 | #endif |
| 318 | s->state=SSL3_ST_CR_CERT_A; |
| 319 | } |
| 320 | s->init_num=0; |
| 321 | break; |
| 322 | #ifndef OPENSSL_NO_TLSEXT |
| 323 | case SSL3_ST_CR_SUPPLEMENTAL_DATA_A: |
| 324 | case SSL3_ST_CR_SUPPLEMENTAL_DATA_B: |
| 325 | ret = tls1_get_server_supplemental_data(s); |
| 326 | if (ret <= 0) goto end; |
| 327 | s->state=SSL3_ST_CR_CERT_A; |
| 328 | s->init_num = 0; |
| 329 | break; |
| 330 | #endif |
| 331 | case SSL3_ST_CR_CERT_A: |
| 332 | case SSL3_ST_CR_CERT_B: |
| 333 | #ifndef OPENSSL_NO_TLSEXT |
| 334 | ret=ssl3_check_finished(s); |
| 335 | if (ret <= 0) goto end; |
| 336 | if (ret == 2) |
| 337 | { |
| 338 | s->hit = 1; |
| 339 | if (s->tlsext_ticket_expected) |
| 340 | s->state=SSL3_ST_CR_SESSION_TICKET_A; |
| 341 | else |
| 342 | s->state=SSL3_ST_CR_FINISHED_A; |
| 343 | s->init_num=0; |
| 344 | break; |
| 345 | } |
| 346 | #endif |
| 347 | /* Check if it is anon DH/ECDH */ |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 348 | /* or non-RSA PSK */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 349 | if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) && |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 350 | !((s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) && |
| 351 | !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kRSA))) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 352 | { |
| 353 | ret=ssl3_get_server_certificate(s); |
| 354 | if (ret <= 0) goto end; |
| 355 | #ifndef OPENSSL_NO_TLSEXT |
| 356 | if (s->tlsext_status_expected) |
| 357 | s->state=SSL3_ST_CR_CERT_STATUS_A; |
| 358 | else |
| 359 | s->state=SSL3_ST_CR_KEY_EXCH_A; |
| 360 | } |
| 361 | else |
| 362 | { |
| 363 | skip = 1; |
| 364 | s->state=SSL3_ST_CR_KEY_EXCH_A; |
| 365 | } |
| 366 | #else |
| 367 | } |
| 368 | else |
| 369 | skip=1; |
| 370 | |
| 371 | s->state=SSL3_ST_CR_KEY_EXCH_A; |
| 372 | #endif |
| 373 | s->init_num=0; |
| 374 | break; |
| 375 | |
| 376 | case SSL3_ST_CR_KEY_EXCH_A: |
| 377 | case SSL3_ST_CR_KEY_EXCH_B: |
| 378 | ret=ssl3_get_key_exchange(s); |
| 379 | if (ret <= 0) goto end; |
| 380 | s->state=SSL3_ST_CR_CERT_REQ_A; |
| 381 | s->init_num=0; |
| 382 | |
| 383 | /* at this point we check that we have the |
| 384 | * required stuff from the server */ |
| 385 | if (!ssl3_check_cert_and_algorithm(s)) |
| 386 | { |
| 387 | ret= -1; |
| 388 | goto end; |
| 389 | } |
| 390 | break; |
| 391 | |
| 392 | case SSL3_ST_CR_CERT_REQ_A: |
| 393 | case SSL3_ST_CR_CERT_REQ_B: |
| 394 | ret=ssl3_get_certificate_request(s); |
| 395 | if (ret <= 0) goto end; |
| 396 | s->state=SSL3_ST_CR_SRVR_DONE_A; |
| 397 | s->init_num=0; |
| 398 | break; |
| 399 | |
| 400 | case SSL3_ST_CR_SRVR_DONE_A: |
| 401 | case SSL3_ST_CR_SRVR_DONE_B: |
| 402 | ret=ssl3_get_server_done(s); |
| 403 | if (ret <= 0) goto end; |
| 404 | if (s->s3->tmp.cert_req) |
| 405 | s->state=SSL3_ST_CW_CERT_A; |
| 406 | else |
| 407 | s->state=SSL3_ST_CW_KEY_EXCH_A; |
| 408 | s->init_num=0; |
| 409 | |
| 410 | break; |
| 411 | |
| 412 | case SSL3_ST_CW_CERT_A: |
| 413 | case SSL3_ST_CW_CERT_B: |
| 414 | case SSL3_ST_CW_CERT_C: |
| 415 | case SSL3_ST_CW_CERT_D: |
| 416 | ret=ssl3_send_client_certificate(s); |
| 417 | if (ret <= 0) goto end; |
| 418 | s->state=SSL3_ST_CW_KEY_EXCH_A; |
| 419 | s->init_num=0; |
| 420 | break; |
| 421 | |
| 422 | case SSL3_ST_CW_KEY_EXCH_A: |
| 423 | case SSL3_ST_CW_KEY_EXCH_B: |
| 424 | ret=ssl3_send_client_key_exchange(s); |
| 425 | if (ret <= 0) goto end; |
| 426 | /* EAY EAY EAY need to check for DH fix cert |
| 427 | * sent back */ |
| 428 | /* For TLS, cert_req is set to 2, so a cert chain |
| 429 | * of nothing is sent, but no verify packet is sent */ |
| 430 | /* XXX: For now, we do not support client |
| 431 | * authentication in ECDH cipher suites with |
| 432 | * ECDH (rather than ECDSA) certificates. |
| 433 | * We need to skip the certificate verify |
| 434 | * message when client's ECDH public key is sent |
| 435 | * inside the client certificate. |
| 436 | */ |
| 437 | if (s->s3->tmp.cert_req == 1) |
| 438 | { |
| 439 | s->state=SSL3_ST_CW_CERT_VRFY_A; |
| 440 | } |
| 441 | else |
| 442 | { |
| 443 | s->state=SSL3_ST_CW_CHANGE_A; |
| 444 | s->s3->change_cipher_spec=0; |
| 445 | } |
| 446 | if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) |
| 447 | { |
| 448 | s->state=SSL3_ST_CW_CHANGE_A; |
| 449 | s->s3->change_cipher_spec=0; |
| 450 | } |
| 451 | |
| 452 | s->init_num=0; |
| 453 | break; |
| 454 | |
| 455 | case SSL3_ST_CW_CERT_VRFY_A: |
| 456 | case SSL3_ST_CW_CERT_VRFY_B: |
| 457 | ret=ssl3_send_client_verify(s); |
| 458 | if (ret <= 0) goto end; |
| 459 | s->state=SSL3_ST_CW_CHANGE_A; |
| 460 | s->init_num=0; |
| 461 | s->s3->change_cipher_spec=0; |
| 462 | break; |
| 463 | |
| 464 | case SSL3_ST_CW_CHANGE_A: |
| 465 | case SSL3_ST_CW_CHANGE_B: |
| 466 | ret=ssl3_send_change_cipher_spec(s, |
| 467 | SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B); |
| 468 | if (ret <= 0) goto end; |
| 469 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 470 | s->state=SSL3_ST_CW_FINISHED_A; |
| 471 | #if !defined(OPENSSL_NO_TLSEXT) |
| 472 | if (s->s3->tlsext_channel_id_valid) |
| 473 | s->state=SSL3_ST_CW_CHANNEL_ID_A; |
| 474 | # if !defined(OPENSSL_NO_NEXTPROTONEG) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 475 | if (s->s3->next_proto_neg_seen) |
| 476 | s->state=SSL3_ST_CW_NEXT_PROTO_A; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 477 | # endif |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 478 | #endif |
| 479 | s->init_num=0; |
| 480 | |
| 481 | s->session->cipher=s->s3->tmp.new_cipher; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 482 | if (!s->method->ssl3_enc->setup_key_block(s)) |
| 483 | { |
| 484 | ret= -1; |
| 485 | goto end; |
| 486 | } |
| 487 | |
| 488 | if (!s->method->ssl3_enc->change_cipher_state(s, |
| 489 | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) |
| 490 | { |
| 491 | ret= -1; |
| 492 | goto end; |
| 493 | } |
| 494 | |
| 495 | break; |
| 496 | |
| 497 | #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 498 | case SSL3_ST_CW_NEXT_PROTO_A: |
| 499 | case SSL3_ST_CW_NEXT_PROTO_B: |
| 500 | ret=ssl3_send_next_proto(s); |
| 501 | if (ret <= 0) goto end; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 502 | if (s->s3->tlsext_channel_id_valid) |
| 503 | s->state=SSL3_ST_CW_CHANNEL_ID_A; |
| 504 | else |
| 505 | s->state=SSL3_ST_CW_FINISHED_A; |
| 506 | break; |
| 507 | #endif |
| 508 | |
| 509 | #if !defined(OPENSSL_NO_TLSEXT) |
| 510 | case SSL3_ST_CW_CHANNEL_ID_A: |
| 511 | case SSL3_ST_CW_CHANNEL_ID_B: |
| 512 | ret=ssl3_send_channel_id(s); |
| 513 | if (ret <= 0) goto end; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 514 | s->state=SSL3_ST_CW_FINISHED_A; |
| 515 | break; |
| 516 | #endif |
| 517 | |
| 518 | case SSL3_ST_CW_FINISHED_A: |
| 519 | case SSL3_ST_CW_FINISHED_B: |
| 520 | ret=ssl3_send_finished(s, |
| 521 | SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B, |
| 522 | s->method->ssl3_enc->client_finished_label, |
| 523 | s->method->ssl3_enc->client_finished_label_len); |
| 524 | if (ret <= 0) goto end; |
Adam Langley | a433cbd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 525 | s->s3->flags |= SSL3_FLAGS_CCS_OK; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 526 | s->state=SSL3_ST_CW_FLUSH; |
| 527 | |
| 528 | /* clear flags */ |
| 529 | s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER; |
| 530 | if (s->hit) |
| 531 | { |
| 532 | s->s3->tmp.next_state=SSL_ST_OK; |
| 533 | if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) |
| 534 | { |
| 535 | s->state=SSL_ST_OK; |
| 536 | s->s3->flags|=SSL3_FLAGS_POP_BUFFER; |
| 537 | s->s3->delay_buf_pop_ret=0; |
| 538 | } |
| 539 | } |
| 540 | else |
| 541 | { |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 542 | /* This is a non-resumption handshake. If it |
| 543 | * involves ChannelID, then record the |
| 544 | * handshake hashes at this point in the |
| 545 | * session so that any resumption of this |
| 546 | * session with ChannelID can sign those |
| 547 | * hashes. */ |
| 548 | if (s->s3->tlsext_channel_id_new) |
| 549 | { |
| 550 | ret = tls1_record_handshake_hashes_for_channel_id(s); |
| 551 | if (ret <= 0) |
| 552 | goto end; |
| 553 | } |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 554 | if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) |
| 555 | && ssl3_can_cutthrough(s) |
| 556 | && s->s3->previous_server_finished_len == 0 /* no cutthrough on renegotiation (would complicate the state machine) */ |
| 557 | ) |
| 558 | { |
| 559 | if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) |
| 560 | { |
| 561 | s->state=SSL3_ST_CUTTHROUGH_COMPLETE; |
| 562 | s->s3->flags|=SSL3_FLAGS_POP_BUFFER; |
| 563 | s->s3->delay_buf_pop_ret=0; |
| 564 | } |
| 565 | else |
| 566 | { |
| 567 | s->s3->tmp.next_state=SSL3_ST_CUTTHROUGH_COMPLETE; |
| 568 | } |
| 569 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 570 | else |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 571 | { |
| 572 | #ifndef OPENSSL_NO_TLSEXT |
| 573 | /* Allow NewSessionTicket if ticket expected */ |
| 574 | if (s->tlsext_ticket_expected) |
| 575 | s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A; |
| 576 | else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 577 | #endif |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 578 | s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A; |
| 579 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 580 | } |
| 581 | s->init_num=0; |
| 582 | break; |
| 583 | |
| 584 | #ifndef OPENSSL_NO_TLSEXT |
| 585 | case SSL3_ST_CR_SESSION_TICKET_A: |
| 586 | case SSL3_ST_CR_SESSION_TICKET_B: |
| 587 | ret=ssl3_get_new_session_ticket(s); |
| 588 | if (ret <= 0) goto end; |
| 589 | s->state=SSL3_ST_CR_FINISHED_A; |
| 590 | s->init_num=0; |
| 591 | break; |
| 592 | |
| 593 | case SSL3_ST_CR_CERT_STATUS_A: |
| 594 | case SSL3_ST_CR_CERT_STATUS_B: |
| 595 | ret=ssl3_get_cert_status(s); |
| 596 | if (ret <= 0) goto end; |
| 597 | s->state=SSL3_ST_CR_KEY_EXCH_A; |
| 598 | s->init_num=0; |
| 599 | break; |
| 600 | #endif |
| 601 | |
| 602 | case SSL3_ST_CR_FINISHED_A: |
| 603 | case SSL3_ST_CR_FINISHED_B: |
| 604 | |
Adam Langley | ce7f9ca | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 605 | s->s3->flags |= SSL3_FLAGS_CCS_OK; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 606 | ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A, |
| 607 | SSL3_ST_CR_FINISHED_B); |
| 608 | if (ret <= 0) goto end; |
| 609 | |
| 610 | if (s->hit) |
| 611 | s->state=SSL3_ST_CW_CHANGE_A; |
| 612 | else |
| 613 | s->state=SSL_ST_OK; |
| 614 | s->init_num=0; |
| 615 | break; |
| 616 | |
| 617 | case SSL3_ST_CW_FLUSH: |
| 618 | s->rwstate=SSL_WRITING; |
| 619 | if (BIO_flush(s->wbio) <= 0) |
| 620 | { |
| 621 | ret= -1; |
| 622 | goto end; |
| 623 | } |
| 624 | s->rwstate=SSL_NOTHING; |
| 625 | s->state=s->s3->tmp.next_state; |
| 626 | break; |
| 627 | |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 628 | case SSL3_ST_CUTTHROUGH_COMPLETE: |
| 629 | #ifndef OPENSSL_NO_TLSEXT |
| 630 | /* Allow NewSessionTicket if ticket expected */ |
| 631 | if (s->tlsext_ticket_expected) |
| 632 | s->state=SSL3_ST_CR_SESSION_TICKET_A; |
| 633 | else |
| 634 | #endif |
| 635 | s->state=SSL3_ST_CR_FINISHED_A; |
| 636 | |
| 637 | /* SSL_write() will take care of flushing buffered data if |
| 638 | * DELAY_CLIENT_FINISHED is set. |
| 639 | */ |
| 640 | if (!(s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)) |
| 641 | ssl_free_wbio_buffer(s); |
| 642 | ret = 1; |
| 643 | goto end; |
| 644 | /* break; */ |
| 645 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 646 | case SSL_ST_OK: |
| 647 | /* clean a few things up */ |
| 648 | ssl3_cleanup_key_block(s); |
| 649 | |
| 650 | if (s->init_buf != NULL) |
| 651 | { |
| 652 | BUF_MEM_free(s->init_buf); |
| 653 | s->init_buf=NULL; |
| 654 | } |
| 655 | |
| 656 | /* If we are not 'joining' the last two packets, |
| 657 | * remove the buffering now */ |
| 658 | if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER)) |
| 659 | ssl_free_wbio_buffer(s); |
| 660 | /* else do it later in ssl3_write */ |
| 661 | |
| 662 | s->init_num=0; |
| 663 | s->renegotiate=0; |
| 664 | s->new_session=0; |
| 665 | |
| 666 | ssl_update_cache(s,SSL_SESS_CACHE_CLIENT); |
| 667 | if (s->hit) s->ctx->stats.sess_hit++; |
| 668 | |
| 669 | ret=1; |
| 670 | /* s->server=0; */ |
| 671 | s->handshake_func=ssl3_connect; |
| 672 | s->ctx->stats.sess_connect_good++; |
| 673 | |
| 674 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); |
| 675 | |
| 676 | goto end; |
| 677 | /* break; */ |
| 678 | |
| 679 | default: |
| 680 | OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE); |
| 681 | ret= -1; |
| 682 | goto end; |
| 683 | /* break; */ |
| 684 | } |
| 685 | |
| 686 | /* did we do anything */ |
| 687 | if (!s->s3->tmp.reuse_message && !skip) |
| 688 | { |
| 689 | if (s->debug) |
| 690 | { |
| 691 | if ((ret=BIO_flush(s->wbio)) <= 0) |
| 692 | goto end; |
| 693 | } |
| 694 | |
| 695 | if ((cb != NULL) && (s->state != state)) |
| 696 | { |
| 697 | new_state=s->state; |
| 698 | s->state=state; |
| 699 | cb(s,SSL_CB_CONNECT_LOOP,1); |
| 700 | s->state=new_state; |
| 701 | } |
| 702 | } |
| 703 | skip=0; |
| 704 | } |
| 705 | end: |
| 706 | s->in_handshake--; |
| 707 | if (buf != NULL) |
| 708 | BUF_MEM_free(buf); |
| 709 | if (cb != NULL) |
| 710 | cb(s,SSL_CB_CONNECT_EXIT,ret); |
| 711 | return(ret); |
| 712 | } |
| 713 | |
| 714 | |
| 715 | int ssl3_client_hello(SSL *s) |
| 716 | { |
| 717 | unsigned char *buf; |
| 718 | unsigned char *p,*d; |
| 719 | int i; |
| 720 | unsigned long l; |
| 721 | |
| 722 | buf=(unsigned char *)s->init_buf->data; |
| 723 | if (s->state == SSL3_ST_CW_CLNT_HELLO_A) |
| 724 | { |
| 725 | SSL_SESSION *sess = s->session; |
| 726 | if ((sess == NULL) || |
| 727 | (sess->ssl_version != s->version) || |
| 728 | #ifdef OPENSSL_NO_TLSEXT |
| 729 | !sess->session_id_length || |
| 730 | #else |
| 731 | (!sess->session_id_length && !sess->tlsext_tick) || |
| 732 | #endif |
| 733 | (sess->not_resumable)) |
| 734 | { |
| 735 | if (!ssl_get_new_session(s,0)) |
| 736 | goto err; |
| 737 | } |
| 738 | if (s->method->version == DTLS_ANY_VERSION) |
| 739 | { |
| 740 | /* Determine which DTLS version to use */ |
| 741 | int options = s->options; |
| 742 | /* If DTLS 1.2 disabled correct the version number */ |
| 743 | if (options & SSL_OP_NO_DTLSv1_2) |
| 744 | { |
| 745 | if (tls1_suiteb(s)) |
| 746 | { |
| 747 | OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE); |
| 748 | goto err; |
| 749 | } |
| 750 | /* Disabling all versions is silly: return an |
| 751 | * error. |
| 752 | */ |
| 753 | if (options & SSL_OP_NO_DTLSv1) |
| 754 | { |
| 755 | OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_WRONG_SSL_VERSION); |
| 756 | goto err; |
| 757 | } |
| 758 | /* Update method so we don't use any DTLS 1.2 |
| 759 | * features. |
| 760 | */ |
| 761 | s->method = DTLSv1_client_method(); |
| 762 | s->version = DTLS1_VERSION; |
| 763 | } |
| 764 | else |
| 765 | { |
| 766 | /* We only support one version: update method */ |
| 767 | if (options & SSL_OP_NO_DTLSv1) |
| 768 | s->method = DTLSv1_2_client_method(); |
| 769 | s->version = DTLS1_2_VERSION; |
| 770 | } |
| 771 | s->client_version = s->version; |
| 772 | } |
| 773 | /* else use the pre-loaded session */ |
| 774 | |
| 775 | p=s->s3->client_random; |
| 776 | |
| 777 | /* for DTLS if client_random is initialized, reuse it, we are |
| 778 | * required to use same upon reply to HelloVerify */ |
| 779 | if (SSL_IS_DTLS(s)) |
| 780 | { |
| 781 | size_t idx; |
| 782 | i = 1; |
| 783 | for (idx=0; idx < sizeof(s->s3->client_random); idx++) |
| 784 | { |
| 785 | if (p[idx]) |
| 786 | { |
| 787 | i = 0; |
| 788 | break; |
| 789 | } |
| 790 | } |
| 791 | } |
| 792 | else |
| 793 | i = 1; |
| 794 | |
| 795 | if (i) |
| 796 | ssl_fill_hello_random(s, 0, p, |
| 797 | sizeof(s->s3->client_random)); |
| 798 | |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 799 | /* Do the message type and length last. |
| 800 | * Note: the final argument to ssl_add_clienthello_tlsext below |
| 801 | * depends on the size of this prefix. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 802 | d=p= ssl_handshake_start(s); |
| 803 | |
| 804 | /* version indicates the negotiated version: for example from |
| 805 | * an SSLv2/v3 compatible client hello). The client_version |
| 806 | * field is the maximum version we permit and it is also |
| 807 | * used in RSA encrypted premaster secrets. Some servers can |
| 808 | * choke if we initially report a higher version then |
| 809 | * renegotiate to a lower one in the premaster secret. This |
| 810 | * didn't happen with TLS 1.0 as most servers supported it |
| 811 | * but it can with TLS 1.1 or later if the server only supports |
| 812 | * 1.0. |
| 813 | * |
| 814 | * Possible scenario with previous logic: |
| 815 | * 1. Client hello indicates TLS 1.2 |
| 816 | * 2. Server hello says TLS 1.0 |
| 817 | * 3. RSA encrypted premaster secret uses 1.2. |
| 818 | * 4. Handhaked proceeds using TLS 1.0. |
| 819 | * 5. Server sends hello request to renegotiate. |
| 820 | * 6. Client hello indicates TLS v1.0 as we now |
| 821 | * know that is maximum server supports. |
| 822 | * 7. Server chokes on RSA encrypted premaster secret |
| 823 | * containing version 1.0. |
| 824 | * |
| 825 | * For interoperability it should be OK to always use the |
| 826 | * maximum version we support in client hello and then rely |
| 827 | * on the checking of version to ensure the servers isn't |
| 828 | * being inconsistent: for example initially negotiating with |
| 829 | * TLS 1.0 and renegotiating with TLS 1.2. We do this by using |
| 830 | * client_version in client hello and not resetting it to |
| 831 | * the negotiated version. |
| 832 | */ |
| 833 | #if 0 |
| 834 | *(p++)=s->version>>8; |
| 835 | *(p++)=s->version&0xff; |
| 836 | s->client_version=s->version; |
| 837 | #else |
| 838 | *(p++)=s->client_version>>8; |
| 839 | *(p++)=s->client_version&0xff; |
| 840 | #endif |
| 841 | |
| 842 | /* Random stuff */ |
| 843 | memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE); |
| 844 | p+=SSL3_RANDOM_SIZE; |
| 845 | |
| 846 | /* Session ID */ |
| 847 | if (s->new_session) |
| 848 | i=0; |
| 849 | else |
| 850 | i=s->session->session_id_length; |
| 851 | *(p++)=i; |
| 852 | if (i != 0) |
| 853 | { |
| 854 | if (i > (int)sizeof(s->session->session_id)) |
| 855 | { |
| 856 | OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR); |
| 857 | goto err; |
| 858 | } |
| 859 | memcpy(p,s->session->session_id,i); |
| 860 | p+=i; |
| 861 | } |
| 862 | |
| 863 | /* cookie stuff for DTLS */ |
| 864 | if (SSL_IS_DTLS(s)) |
| 865 | { |
| 866 | if ( s->d1->cookie_len > sizeof(s->d1->cookie)) |
| 867 | { |
| 868 | OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR); |
| 869 | goto err; |
| 870 | } |
| 871 | *(p++) = s->d1->cookie_len; |
| 872 | memcpy(p, s->d1->cookie, s->d1->cookie_len); |
| 873 | p += s->d1->cookie_len; |
| 874 | } |
| 875 | |
| 876 | /* Ciphers supported */ |
| 877 | i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0); |
| 878 | if (i == 0) |
| 879 | { |
| 880 | OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_NO_CIPHERS_AVAILABLE); |
| 881 | goto err; |
| 882 | } |
| 883 | #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH |
| 884 | /* Some servers hang if client hello > 256 bytes |
| 885 | * as hack workaround chop number of supported ciphers |
| 886 | * to keep it well below this if we use TLS v1.2 |
| 887 | */ |
| 888 | if (TLS1_get_version(s) >= TLS1_2_VERSION |
| 889 | && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH) |
| 890 | i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1; |
| 891 | #endif |
| 892 | s2n(i,p); |
| 893 | p+=i; |
| 894 | |
| 895 | /* COMPRESSION */ |
| 896 | *(p++)=1; |
| 897 | *(p++)=0; /* Add the NULL method */ |
| 898 | |
| 899 | #ifndef OPENSSL_NO_TLSEXT |
| 900 | /* TLS extensions*/ |
| 901 | if (ssl_prepare_clienthello_tlsext(s) <= 0) |
| 902 | { |
| 903 | OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_CLIENTHELLO_TLSEXT); |
| 904 | goto err; |
| 905 | } |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 906 | if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH, p-buf)) == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 907 | { |
| 908 | OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR); |
| 909 | goto err; |
| 910 | } |
| 911 | #endif |
| 912 | |
| 913 | l= p-d; |
| 914 | ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l); |
| 915 | s->state=SSL3_ST_CW_CLNT_HELLO_B; |
| 916 | } |
| 917 | |
| 918 | /* SSL3_ST_CW_CLNT_HELLO_B */ |
| 919 | return ssl_do_write(s); |
| 920 | err: |
| 921 | return(-1); |
| 922 | } |
| 923 | |
| 924 | int ssl3_get_server_hello(SSL *s) |
| 925 | { |
| 926 | STACK_OF(SSL_CIPHER) *sk; |
| 927 | const SSL_CIPHER *c; |
| 928 | CERT *ct = s->cert; |
| 929 | unsigned char *p,*d; |
| 930 | int al=SSL_AD_INTERNAL_ERROR,ok; |
| 931 | unsigned int j; |
| 932 | long n; |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame^] | 933 | CBS cbs; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 934 | /* Hello verify request and/or server hello version may not |
| 935 | * match so set first packet if we're negotiating version. |
| 936 | */ |
| 937 | if (SSL_IS_DTLS(s)) |
| 938 | s->first_packet = 1; |
| 939 | |
| 940 | n=s->method->ssl_get_message(s, |
| 941 | SSL3_ST_CR_SRVR_HELLO_A, |
| 942 | SSL3_ST_CR_SRVR_HELLO_B, |
| 943 | -1, |
| 944 | 20000, /* ?? */ |
| 945 | &ok); |
| 946 | |
| 947 | if (!ok) return((int)n); |
| 948 | |
| 949 | if (SSL_IS_DTLS(s)) |
| 950 | { |
| 951 | s->first_packet = 0; |
| 952 | if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) |
| 953 | { |
| 954 | if ( s->d1->send_cookie == 0) |
| 955 | { |
| 956 | s->s3->tmp.reuse_message = 1; |
| 957 | return 1; |
| 958 | } |
| 959 | else /* already sent a cookie */ |
| 960 | { |
| 961 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 962 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_MESSAGE_TYPE); |
| 963 | goto f_err; |
| 964 | } |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) |
| 969 | { |
| 970 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 971 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_MESSAGE_TYPE); |
| 972 | goto f_err; |
| 973 | } |
| 974 | |
| 975 | d=p=(unsigned char *)s->init_msg; |
| 976 | if (s->method->version == DTLS_ANY_VERSION) |
| 977 | { |
| 978 | /* Work out correct protocol version to use */ |
| 979 | int hversion = (p[0] << 8)|p[1]; |
| 980 | int options = s->options; |
| 981 | if (hversion == DTLS1_2_VERSION |
| 982 | && !(options & SSL_OP_NO_DTLSv1_2)) |
| 983 | s->method = DTLSv1_2_client_method(); |
| 984 | else if (tls1_suiteb(s)) |
| 985 | { |
| 986 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE); |
| 987 | s->version = hversion; |
| 988 | al = SSL_AD_PROTOCOL_VERSION; |
| 989 | goto f_err; |
| 990 | } |
| 991 | else if (hversion == DTLS1_VERSION |
| 992 | && !(options & SSL_OP_NO_DTLSv1)) |
| 993 | s->method = DTLSv1_client_method(); |
| 994 | else |
| 995 | { |
| 996 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION); |
| 997 | s->version = hversion; |
| 998 | al = SSL_AD_PROTOCOL_VERSION; |
| 999 | goto f_err; |
| 1000 | } |
| 1001 | s->version = s->client_version = s->method->version; |
| 1002 | } |
| 1003 | |
| 1004 | if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff))) |
| 1005 | { |
| 1006 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION); |
| 1007 | s->version=(s->version&0xff00)|p[1]; |
| 1008 | al=SSL_AD_PROTOCOL_VERSION; |
| 1009 | goto f_err; |
| 1010 | } |
| 1011 | p+=2; |
| 1012 | |
| 1013 | /* load the server hello data */ |
| 1014 | /* load the server random */ |
| 1015 | memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE); |
| 1016 | p+=SSL3_RANDOM_SIZE; |
| 1017 | |
| 1018 | /* get the session-id */ |
| 1019 | j= *(p++); |
| 1020 | |
| 1021 | if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) |
| 1022 | { |
| 1023 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 1024 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_SSL3_SESSION_ID_TOO_LONG); |
| 1025 | goto f_err; |
| 1026 | } |
| 1027 | |
| 1028 | #ifndef OPENSSL_NO_TLSEXT |
| 1029 | /* check if we want to resume the session based on external pre-shared secret */ |
| 1030 | if (s->version >= TLS1_VERSION && s->tls_session_secret_cb) |
| 1031 | { |
| 1032 | SSL_CIPHER *pref_cipher=NULL; |
| 1033 | s->session->master_key_length=sizeof(s->session->master_key); |
| 1034 | if (s->tls_session_secret_cb(s, s->session->master_key, |
| 1035 | &s->session->master_key_length, |
| 1036 | NULL, &pref_cipher, |
| 1037 | s->tls_session_secret_cb_arg)) |
| 1038 | { |
| 1039 | s->session->cipher = pref_cipher ? |
| 1040 | pref_cipher : ssl_get_cipher_by_char(s, p+j); |
Adam Langley | 6acf476 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1041 | s->s3->flags |= SSL3_FLAGS_CCS_OK; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1042 | } |
| 1043 | } |
| 1044 | #endif /* OPENSSL_NO_TLSEXT */ |
| 1045 | |
| 1046 | if (j != 0 && j == s->session->session_id_length |
| 1047 | && memcmp(p,s->session->session_id,j) == 0) |
| 1048 | { |
| 1049 | if(s->sid_ctx_length != s->session->sid_ctx_length |
| 1050 | || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length)) |
| 1051 | { |
| 1052 | /* actually a client application bug */ |
| 1053 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 1054 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
| 1055 | goto f_err; |
| 1056 | } |
Adam Langley | ce7f9ca | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1057 | s->s3->flags |= SSL3_FLAGS_CCS_OK; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1058 | s->hit=1; |
| 1059 | } |
| 1060 | else /* a miss or crap from the other end */ |
| 1061 | { |
| 1062 | /* If we were trying for session-id reuse, make a new |
| 1063 | * SSL_SESSION so we don't stuff up other people */ |
| 1064 | s->hit=0; |
| 1065 | if (s->session->session_id_length > 0) |
| 1066 | { |
| 1067 | if (!ssl_get_new_session(s,0)) |
| 1068 | { |
| 1069 | goto f_err; |
| 1070 | } |
| 1071 | } |
| 1072 | s->session->session_id_length=j; |
| 1073 | memcpy(s->session->session_id,p,j); /* j could be 0 */ |
| 1074 | } |
| 1075 | p+=j; |
| 1076 | c=ssl_get_cipher_by_char(s,p); |
| 1077 | if (c == NULL) |
| 1078 | { |
| 1079 | /* unknown cipher */ |
| 1080 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 1081 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNKNOWN_CIPHER_RETURNED); |
| 1082 | goto f_err; |
| 1083 | } |
| 1084 | /* If it is a disabled cipher we didn't send it in client hello, |
| 1085 | * so return an error. |
| 1086 | */ |
| 1087 | if (c->algorithm_ssl & ct->mask_ssl || |
| 1088 | c->algorithm_mkey & ct->mask_k || |
| 1089 | c->algorithm_auth & ct->mask_a) |
| 1090 | { |
| 1091 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 1092 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED); |
| 1093 | goto f_err; |
| 1094 | } |
| 1095 | p+=ssl_put_cipher_by_char(s,NULL,NULL); |
| 1096 | |
| 1097 | sk=ssl_get_ciphers_by_id(s); |
| 1098 | if (!sk_SSL_CIPHER_find(sk, NULL, c)) |
| 1099 | { |
| 1100 | /* we did not say we would use this cipher */ |
| 1101 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 1102 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED); |
| 1103 | goto f_err; |
| 1104 | } |
| 1105 | |
| 1106 | /* Depending on the session caching (internal/external), the cipher |
| 1107 | and/or cipher_id values may not be set. Make sure that |
| 1108 | cipher_id is set and use it for comparison. */ |
| 1109 | if (s->session->cipher) |
| 1110 | s->session->cipher_id = s->session->cipher->id; |
| 1111 | if (s->hit && (s->session->cipher_id != c->id)) |
| 1112 | { |
| 1113 | /* Workaround is now obsolete */ |
| 1114 | #if 0 |
| 1115 | if (!(s->options & |
| 1116 | SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)) |
| 1117 | #endif |
| 1118 | { |
| 1119 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 1120 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); |
| 1121 | goto f_err; |
| 1122 | } |
| 1123 | } |
| 1124 | s->s3->tmp.new_cipher=c; |
| 1125 | /* Don't digest cached records if no sigalgs: we may need them for |
| 1126 | * client authentication. |
| 1127 | */ |
| 1128 | if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s)) |
| 1129 | goto f_err; |
| 1130 | /* lets get the compression algorithm */ |
| 1131 | /* COMPRESSION */ |
| 1132 | if (*(p++) != 0) |
| 1133 | { |
| 1134 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 1135 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
| 1136 | goto f_err; |
| 1137 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1138 | |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame^] | 1139 | /* TODO(fork): Port the rest of this function to CBS. */ |
| 1140 | CBS_init(&cbs, p, d + n - p); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1141 | #ifndef OPENSSL_NO_TLSEXT |
| 1142 | /* TLS extensions*/ |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame^] | 1143 | if (!ssl_parse_serverhello_tlsext(s, &cbs)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1144 | { |
| 1145 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT); |
| 1146 | goto err; |
| 1147 | } |
| 1148 | #endif |
| 1149 | |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame^] | 1150 | /* There should be nothing left over in the record. */ |
| 1151 | if (CBS_len(&cbs) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1152 | { |
| 1153 | /* wrong packet length */ |
| 1154 | al=SSL_AD_DECODE_ERROR; |
| 1155 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH); |
| 1156 | goto f_err; |
| 1157 | } |
| 1158 | |
| 1159 | return(1); |
| 1160 | f_err: |
| 1161 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1162 | err: |
| 1163 | return(-1); |
| 1164 | } |
| 1165 | |
| 1166 | int ssl3_get_server_certificate(SSL *s) |
| 1167 | { |
| 1168 | int al,i,ok,ret= -1; |
| 1169 | unsigned long n,nc,llen,l; |
| 1170 | X509 *x=NULL; |
| 1171 | const unsigned char *q,*p; |
| 1172 | unsigned char *d; |
| 1173 | STACK_OF(X509) *sk=NULL; |
| 1174 | SESS_CERT *sc; |
| 1175 | EVP_PKEY *pkey=NULL; |
| 1176 | int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */ |
| 1177 | |
| 1178 | n=s->method->ssl_get_message(s, |
| 1179 | SSL3_ST_CR_CERT_A, |
| 1180 | SSL3_ST_CR_CERT_B, |
| 1181 | -1, |
| 1182 | s->max_cert_list, |
| 1183 | &ok); |
| 1184 | |
| 1185 | if (!ok) return((int)n); |
| 1186 | |
| 1187 | if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) || |
| 1188 | ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) && |
| 1189 | (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) |
| 1190 | { |
| 1191 | s->s3->tmp.reuse_message=1; |
| 1192 | return(1); |
| 1193 | } |
| 1194 | |
| 1195 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) |
| 1196 | { |
| 1197 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 1198 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_BAD_MESSAGE_TYPE); |
| 1199 | goto f_err; |
| 1200 | } |
| 1201 | p=d=(unsigned char *)s->init_msg; |
| 1202 | |
| 1203 | if ((sk=sk_X509_new_null()) == NULL) |
| 1204 | { |
| 1205 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE); |
| 1206 | goto err; |
| 1207 | } |
| 1208 | |
| 1209 | n2l3(p,llen); |
| 1210 | if (llen+3 != n) |
| 1211 | { |
| 1212 | al=SSL_AD_DECODE_ERROR; |
| 1213 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH); |
| 1214 | goto f_err; |
| 1215 | } |
| 1216 | for (nc=0; nc<llen; ) |
| 1217 | { |
| 1218 | n2l3(p,l); |
| 1219 | if ((l+nc+3) > llen) |
| 1220 | { |
| 1221 | al=SSL_AD_DECODE_ERROR; |
| 1222 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH); |
| 1223 | goto f_err; |
| 1224 | } |
| 1225 | |
| 1226 | q=p; |
| 1227 | x=d2i_X509(NULL,&q,l); |
| 1228 | if (x == NULL) |
| 1229 | { |
| 1230 | al=SSL_AD_BAD_CERTIFICATE; |
| 1231 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB); |
| 1232 | goto f_err; |
| 1233 | } |
| 1234 | if (q != (p+l)) |
| 1235 | { |
| 1236 | al=SSL_AD_DECODE_ERROR; |
| 1237 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH); |
| 1238 | goto f_err; |
| 1239 | } |
| 1240 | if (!sk_X509_push(sk,x)) |
| 1241 | { |
| 1242 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE); |
| 1243 | goto err; |
| 1244 | } |
| 1245 | x=NULL; |
| 1246 | nc+=l+3; |
| 1247 | p=q; |
| 1248 | } |
| 1249 | |
| 1250 | i=ssl_verify_cert_chain(s,sk); |
| 1251 | if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0) |
| 1252 | ) |
| 1253 | { |
| 1254 | al=ssl_verify_alarm_type(s->verify_result); |
| 1255 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 1256 | goto f_err; |
| 1257 | } |
| 1258 | ERR_clear_error(); /* but we keep s->verify_result */ |
| 1259 | |
| 1260 | sc=ssl_sess_cert_new(); |
| 1261 | if (sc == NULL) goto err; |
| 1262 | |
| 1263 | if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert); |
| 1264 | s->session->sess_cert=sc; |
| 1265 | |
| 1266 | sc->cert_chain=sk; |
| 1267 | /* Inconsistency alert: cert_chain does include the peer's |
| 1268 | * certificate, which we don't include in s3_srvr.c */ |
| 1269 | x=sk_X509_value(sk,0); |
| 1270 | sk=NULL; |
| 1271 | /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/ |
| 1272 | |
| 1273 | pkey=X509_get_pubkey(x); |
| 1274 | |
| 1275 | /* VRS: allow null cert if auth == KRB5 */ |
| 1276 | need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) && |
| 1277 | (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)) |
| 1278 | ? 0 : 1; |
| 1279 | |
| 1280 | #ifdef KSSL_DEBUG |
| 1281 | printf("pkey,x = %p, %p\n", pkey,x); |
| 1282 | printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey)); |
| 1283 | printf("cipher, alg, nc = %s, %lx, %lx, %d\n", s->s3->tmp.new_cipher->name, |
| 1284 | s->s3->tmp.new_cipher->algorithm_mkey, s->s3->tmp.new_cipher->algorithm_auth, need_cert); |
| 1285 | #endif /* KSSL_DEBUG */ |
| 1286 | |
| 1287 | if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) |
| 1288 | { |
| 1289 | x=NULL; |
| 1290 | al=SSL3_AL_FATAL; |
| 1291 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); |
| 1292 | goto f_err; |
| 1293 | } |
| 1294 | |
| 1295 | i=ssl_cert_type(x,pkey); |
| 1296 | if (need_cert && i < 0) |
| 1297 | { |
| 1298 | x=NULL; |
| 1299 | al=SSL3_AL_FATAL; |
| 1300 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
| 1301 | goto f_err; |
| 1302 | } |
| 1303 | |
| 1304 | if (need_cert) |
| 1305 | { |
| 1306 | int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher); |
| 1307 | if (exp_idx >= 0 && i != exp_idx) |
| 1308 | { |
| 1309 | x=NULL; |
| 1310 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 1311 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_WRONG_CERTIFICATE_TYPE); |
| 1312 | goto f_err; |
| 1313 | } |
| 1314 | sc->peer_cert_type=i; |
| 1315 | CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509); |
| 1316 | /* Why would the following ever happen? |
| 1317 | * We just created sc a couple of lines ago. */ |
| 1318 | if (sc->peer_pkeys[i].x509 != NULL) |
| 1319 | X509_free(sc->peer_pkeys[i].x509); |
| 1320 | sc->peer_pkeys[i].x509=x; |
| 1321 | sc->peer_key= &(sc->peer_pkeys[i]); |
| 1322 | |
| 1323 | if (s->session->peer != NULL) |
| 1324 | X509_free(s->session->peer); |
| 1325 | CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509); |
| 1326 | s->session->peer=x; |
| 1327 | } |
| 1328 | else |
| 1329 | { |
| 1330 | sc->peer_cert_type=i; |
| 1331 | sc->peer_key= NULL; |
| 1332 | |
| 1333 | if (s->session->peer != NULL) |
| 1334 | X509_free(s->session->peer); |
| 1335 | s->session->peer=NULL; |
| 1336 | } |
| 1337 | s->session->verify_result = s->verify_result; |
| 1338 | |
| 1339 | x=NULL; |
| 1340 | #ifndef OPENSSL_NO_TLSEXT |
| 1341 | /* Check the audit proof. */ |
| 1342 | if (s->ctx->tlsext_authz_server_audit_proof_cb) |
| 1343 | { |
| 1344 | ret = s->ctx->tlsext_authz_server_audit_proof_cb(s, |
| 1345 | s->ctx->tlsext_authz_server_audit_proof_cb_arg); |
| 1346 | if (ret <= 0) |
| 1347 | { |
| 1348 | al = SSL_AD_BAD_CERTIFICATE; |
| 1349 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_INVALID_AUDIT_PROOF); |
| 1350 | goto f_err; |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | #endif |
| 1355 | ret=1; |
| 1356 | if (0) |
| 1357 | { |
| 1358 | f_err: |
| 1359 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1360 | } |
| 1361 | err: |
| 1362 | EVP_PKEY_free(pkey); |
| 1363 | X509_free(x); |
| 1364 | sk_X509_pop_free(sk,X509_free); |
| 1365 | return(ret); |
| 1366 | } |
| 1367 | |
| 1368 | int ssl3_get_key_exchange(SSL *s) |
| 1369 | { |
| 1370 | #ifndef OPENSSL_NO_RSA |
| 1371 | unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2]; |
| 1372 | #endif |
| 1373 | EVP_MD_CTX md_ctx; |
| 1374 | unsigned char *param,*p; |
| 1375 | int al,i,j,param_len,ok; |
| 1376 | long n,alg_k,alg_a; |
| 1377 | EVP_PKEY *pkey=NULL; |
| 1378 | const EVP_MD *md = NULL; |
| 1379 | #ifndef OPENSSL_NO_RSA |
| 1380 | RSA *rsa=NULL; |
| 1381 | #endif |
| 1382 | #ifndef OPENSSL_NO_DH |
| 1383 | DH *dh=NULL; |
| 1384 | #endif |
| 1385 | #ifndef OPENSSL_NO_ECDH |
| 1386 | EC_KEY *ecdh = NULL; |
| 1387 | BN_CTX *bn_ctx = NULL; |
| 1388 | EC_POINT *srvr_ecpoint = NULL; |
| 1389 | int curve_nid = 0; |
| 1390 | int encoded_pt_len = 0; |
| 1391 | #endif |
| 1392 | |
| 1393 | /* use same message size as in ssl3_get_certificate_request() |
| 1394 | * as ServerKeyExchange message may be skipped */ |
| 1395 | n=s->method->ssl_get_message(s, |
| 1396 | SSL3_ST_CR_KEY_EXCH_A, |
| 1397 | SSL3_ST_CR_KEY_EXCH_B, |
| 1398 | -1, |
| 1399 | s->max_cert_list, |
| 1400 | &ok); |
| 1401 | if (!ok) return((int)n); |
| 1402 | |
| 1403 | if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) |
| 1404 | { |
| 1405 | #ifndef OPENSSL_NO_PSK |
| 1406 | /* In plain PSK ciphersuite, ServerKeyExchange can be |
| 1407 | omitted if no identity hint is sent. Set |
| 1408 | session->sess_cert anyway to avoid problems |
| 1409 | later.*/ |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1410 | if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_aPSK) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1411 | { |
| 1412 | s->session->sess_cert=ssl_sess_cert_new(); |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1413 | if (s->session->psk_identity_hint) |
| 1414 | { |
| 1415 | OPENSSL_free(s->session->psk_identity_hint); |
| 1416 | s->session->psk_identity_hint = NULL; |
| 1417 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1418 | } |
| 1419 | #endif |
| 1420 | s->s3->tmp.reuse_message=1; |
| 1421 | return(1); |
| 1422 | } |
| 1423 | |
| 1424 | param=p=(unsigned char *)s->init_msg; |
| 1425 | if (s->session->sess_cert != NULL) |
| 1426 | { |
| 1427 | #ifndef OPENSSL_NO_RSA |
| 1428 | if (s->session->sess_cert->peer_rsa_tmp != NULL) |
| 1429 | { |
| 1430 | RSA_free(s->session->sess_cert->peer_rsa_tmp); |
| 1431 | s->session->sess_cert->peer_rsa_tmp=NULL; |
| 1432 | } |
| 1433 | #endif |
| 1434 | #ifndef OPENSSL_NO_DH |
| 1435 | if (s->session->sess_cert->peer_dh_tmp) |
| 1436 | { |
| 1437 | DH_free(s->session->sess_cert->peer_dh_tmp); |
| 1438 | s->session->sess_cert->peer_dh_tmp=NULL; |
| 1439 | } |
| 1440 | #endif |
| 1441 | #ifndef OPENSSL_NO_ECDH |
| 1442 | if (s->session->sess_cert->peer_ecdh_tmp) |
| 1443 | { |
| 1444 | EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp); |
| 1445 | s->session->sess_cert->peer_ecdh_tmp=NULL; |
| 1446 | } |
| 1447 | #endif |
| 1448 | } |
| 1449 | else |
| 1450 | { |
| 1451 | s->session->sess_cert=ssl_sess_cert_new(); |
| 1452 | } |
| 1453 | |
| 1454 | param_len=0; |
| 1455 | alg_k=s->s3->tmp.new_cipher->algorithm_mkey; |
| 1456 | alg_a=s->s3->tmp.new_cipher->algorithm_auth; |
| 1457 | EVP_MD_CTX_init(&md_ctx); |
| 1458 | |
| 1459 | #ifndef OPENSSL_NO_PSK |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1460 | if (alg_a & SSL_aPSK) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1461 | { |
| 1462 | char tmp_id_hint[PSK_MAX_IDENTITY_LEN+1]; |
| 1463 | |
| 1464 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1465 | n2s(p,i); |
| 1466 | param_len=i+2; |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1467 | if (s->session->psk_identity_hint) |
| 1468 | { |
| 1469 | OPENSSL_free(s->session->psk_identity_hint); |
| 1470 | s->session->psk_identity_hint = NULL; |
| 1471 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1472 | if (i != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1473 | { |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1474 | /* Store PSK identity hint for later use, hint is used |
| 1475 | * in ssl3_send_client_key_exchange. Assume that the |
| 1476 | * maximum length of a PSK identity hint can be as |
| 1477 | * long as the maximum length of a PSK identity. */ |
| 1478 | if (i > PSK_MAX_IDENTITY_LEN) |
| 1479 | { |
| 1480 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_DATA_LENGTH_TOO_LONG); |
| 1481 | goto f_err; |
| 1482 | } |
| 1483 | if (param_len > n) |
| 1484 | { |
| 1485 | al=SSL_AD_DECODE_ERROR; |
| 1486 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH); |
| 1487 | goto f_err; |
| 1488 | } |
| 1489 | /* If received PSK identity hint contains NULL |
| 1490 | * characters, the hint is truncated from the first |
| 1491 | * NULL. p may not be ending with NULL, so create a |
| 1492 | * NULL-terminated string. */ |
| 1493 | memcpy(tmp_id_hint, p, i); |
| 1494 | memset(tmp_id_hint+i, 0, PSK_MAX_IDENTITY_LEN+1-i); |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1495 | s->session->psk_identity_hint = BUF_strdup(tmp_id_hint); |
| 1496 | if (s->session->psk_identity_hint == NULL) |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1497 | { |
| 1498 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE); |
| 1499 | goto f_err; |
| 1500 | } |
| 1501 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1502 | |
| 1503 | p+=i; |
| 1504 | n-=param_len; |
| 1505 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1506 | #endif /* !OPENSSL_NO_PSK */ |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1507 | |
| 1508 | if (0) {} |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1509 | #ifndef OPENSSL_NO_RSA |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1510 | else if (alg_k & SSL_kRSA) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1511 | { |
| 1512 | if ((rsa=RSA_new()) == NULL) |
| 1513 | { |
| 1514 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE); |
| 1515 | goto err; |
| 1516 | } |
| 1517 | n2s(p,i); |
| 1518 | param_len=i+2; |
| 1519 | if (param_len > n) |
| 1520 | { |
| 1521 | al=SSL_AD_DECODE_ERROR; |
| 1522 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_RSA_MODULUS_LENGTH); |
| 1523 | goto f_err; |
| 1524 | } |
| 1525 | if (!(rsa->n=BN_bin2bn(p,i,rsa->n))) |
| 1526 | { |
| 1527 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB); |
| 1528 | goto err; |
| 1529 | } |
| 1530 | p+=i; |
| 1531 | |
| 1532 | n2s(p,i); |
| 1533 | param_len+=i+2; |
| 1534 | if (param_len > n) |
| 1535 | { |
| 1536 | al=SSL_AD_DECODE_ERROR; |
| 1537 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_RSA_E_LENGTH); |
| 1538 | goto f_err; |
| 1539 | } |
| 1540 | if (!(rsa->e=BN_bin2bn(p,i,rsa->e))) |
| 1541 | { |
| 1542 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB); |
| 1543 | goto err; |
| 1544 | } |
| 1545 | p+=i; |
| 1546 | n-=param_len; |
| 1547 | |
| 1548 | /* this should be because we are using an export cipher */ |
| 1549 | if (alg_a & SSL_aRSA) |
| 1550 | pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); |
| 1551 | else |
| 1552 | { |
| 1553 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_INTERNAL_ERROR); |
| 1554 | goto err; |
| 1555 | } |
| 1556 | s->session->sess_cert->peer_rsa_tmp=rsa; |
| 1557 | rsa=NULL; |
| 1558 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1559 | #endif |
| 1560 | #ifndef OPENSSL_NO_DH |
| 1561 | else if (alg_k & SSL_kEDH) |
| 1562 | { |
| 1563 | if ((dh=DH_new()) == NULL) |
| 1564 | { |
| 1565 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_DH_LIB); |
| 1566 | goto err; |
| 1567 | } |
| 1568 | n2s(p,i); |
| 1569 | param_len=i+2; |
| 1570 | if (param_len > n) |
| 1571 | { |
| 1572 | al=SSL_AD_DECODE_ERROR; |
| 1573 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_DH_P_LENGTH); |
| 1574 | goto f_err; |
| 1575 | } |
| 1576 | if (!(dh->p=BN_bin2bn(p,i,NULL))) |
| 1577 | { |
| 1578 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB); |
| 1579 | goto err; |
| 1580 | } |
| 1581 | p+=i; |
| 1582 | |
| 1583 | n2s(p,i); |
| 1584 | param_len+=i+2; |
| 1585 | if (param_len > n) |
| 1586 | { |
| 1587 | al=SSL_AD_DECODE_ERROR; |
| 1588 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_DH_G_LENGTH); |
| 1589 | goto f_err; |
| 1590 | } |
| 1591 | if (!(dh->g=BN_bin2bn(p,i,NULL))) |
| 1592 | { |
| 1593 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB); |
| 1594 | goto err; |
| 1595 | } |
| 1596 | p+=i; |
| 1597 | |
| 1598 | n2s(p,i); |
| 1599 | param_len+=i+2; |
| 1600 | if (param_len > n) |
| 1601 | { |
| 1602 | al=SSL_AD_DECODE_ERROR; |
| 1603 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_DH_PUB_KEY_LENGTH); |
| 1604 | goto f_err; |
| 1605 | } |
| 1606 | if (!(dh->pub_key=BN_bin2bn(p,i,NULL))) |
| 1607 | { |
| 1608 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_BN_LIB); |
| 1609 | goto err; |
| 1610 | } |
| 1611 | p+=i; |
| 1612 | n-=param_len; |
| 1613 | |
| 1614 | #ifndef OPENSSL_NO_RSA |
| 1615 | if (alg_a & SSL_aRSA) |
| 1616 | pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); |
| 1617 | #else |
| 1618 | if (0) |
| 1619 | ; |
| 1620 | #endif |
| 1621 | #ifndef OPENSSL_NO_DSA |
| 1622 | else if (alg_a & SSL_aDSS) |
| 1623 | pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509); |
| 1624 | #endif |
| 1625 | /* else anonymous DH, so no certificate or pkey. */ |
| 1626 | |
| 1627 | s->session->sess_cert->peer_dh_tmp=dh; |
| 1628 | dh=NULL; |
| 1629 | } |
| 1630 | else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd)) |
| 1631 | { |
| 1632 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 1633 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER); |
| 1634 | goto f_err; |
| 1635 | } |
| 1636 | #endif /* !OPENSSL_NO_DH */ |
| 1637 | |
| 1638 | #ifndef OPENSSL_NO_ECDH |
| 1639 | else if (alg_k & SSL_kEECDH) |
| 1640 | { |
| 1641 | EC_GROUP *ngroup; |
| 1642 | const EC_GROUP *group; |
| 1643 | |
| 1644 | if ((ecdh=EC_KEY_new()) == NULL) |
| 1645 | { |
| 1646 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE); |
| 1647 | goto err; |
| 1648 | } |
| 1649 | |
| 1650 | /* Extract elliptic curve parameters and the |
| 1651 | * server's ephemeral ECDH public key. |
| 1652 | * Keep accumulating lengths of various components in |
| 1653 | * param_len and make sure it never exceeds n. |
| 1654 | */ |
| 1655 | |
| 1656 | /* XXX: For now we only support named (not generic) curves |
| 1657 | * and the ECParameters in this case is just three bytes. |
| 1658 | */ |
| 1659 | param_len=3; |
| 1660 | /* Check curve is one of our prefrences, if not server has |
| 1661 | * sent an invalid curve. |
| 1662 | */ |
| 1663 | if (!tls1_check_curve(s, p, param_len)) |
| 1664 | { |
| 1665 | al=SSL_AD_DECODE_ERROR; |
| 1666 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_WRONG_CURVE); |
| 1667 | goto f_err; |
| 1668 | } |
| 1669 | |
| 1670 | if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) |
| 1671 | { |
| 1672 | al=SSL_AD_INTERNAL_ERROR; |
| 1673 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); |
| 1674 | goto f_err; |
| 1675 | } |
| 1676 | |
| 1677 | ngroup = EC_GROUP_new_by_curve_name(curve_nid); |
| 1678 | if (ngroup == NULL) |
| 1679 | { |
| 1680 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_EC_LIB); |
| 1681 | goto err; |
| 1682 | } |
| 1683 | if (EC_KEY_set_group(ecdh, ngroup) == 0) |
| 1684 | { |
| 1685 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_EC_LIB); |
| 1686 | goto err; |
| 1687 | } |
| 1688 | EC_GROUP_free(ngroup); |
| 1689 | |
| 1690 | group = EC_KEY_get0_group(ecdh); |
| 1691 | |
| 1692 | if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && |
| 1693 | (EC_GROUP_get_degree(group) > 163)) |
| 1694 | { |
| 1695 | al=SSL_AD_EXPORT_RESTRICTION; |
| 1696 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER); |
| 1697 | goto f_err; |
| 1698 | } |
| 1699 | |
| 1700 | p+=3; |
| 1701 | |
| 1702 | /* Next, get the encoded ECPoint */ |
| 1703 | if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) || |
| 1704 | ((bn_ctx = BN_CTX_new()) == NULL)) |
| 1705 | { |
| 1706 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_MALLOC_FAILURE); |
| 1707 | goto err; |
| 1708 | } |
| 1709 | |
| 1710 | encoded_pt_len = *p; /* length of encoded point */ |
| 1711 | p+=1; |
| 1712 | param_len += (1 + encoded_pt_len); |
| 1713 | if ((param_len > n) || |
| 1714 | (EC_POINT_oct2point(group, srvr_ecpoint, |
| 1715 | p, encoded_pt_len, bn_ctx) == 0)) |
| 1716 | { |
| 1717 | al=SSL_AD_DECODE_ERROR; |
| 1718 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_ECPOINT); |
| 1719 | goto f_err; |
| 1720 | } |
| 1721 | |
| 1722 | n-=param_len; |
| 1723 | p+=encoded_pt_len; |
| 1724 | |
| 1725 | /* The ECC/TLS specification does not mention |
| 1726 | * the use of DSA to sign ECParameters in the server |
| 1727 | * key exchange message. We do support RSA and ECDSA. |
| 1728 | */ |
| 1729 | if (0) ; |
| 1730 | #ifndef OPENSSL_NO_RSA |
| 1731 | else if (alg_a & SSL_aRSA) |
| 1732 | pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); |
| 1733 | #endif |
| 1734 | #ifndef OPENSSL_NO_ECDSA |
| 1735 | else if (alg_a & SSL_aECDSA) |
| 1736 | pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); |
| 1737 | #endif |
| 1738 | /* else anonymous ECDH, so no certificate or pkey. */ |
| 1739 | EC_KEY_set_public_key(ecdh, srvr_ecpoint); |
| 1740 | s->session->sess_cert->peer_ecdh_tmp=ecdh; |
| 1741 | ecdh=NULL; |
| 1742 | BN_CTX_free(bn_ctx); |
| 1743 | bn_ctx = NULL; |
| 1744 | EC_POINT_free(srvr_ecpoint); |
| 1745 | srvr_ecpoint = NULL; |
| 1746 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1747 | #endif /* !OPENSSL_NO_ECDH */ |
| 1748 | |
| 1749 | else if (!(alg_k & SSL_kPSK)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1750 | { |
| 1751 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 1752 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_UNEXPECTED_MESSAGE); |
| 1753 | goto f_err; |
| 1754 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1755 | |
| 1756 | /* p points to the next byte, there are 'n' bytes left */ |
| 1757 | |
| 1758 | /* if it was signed, check the signature */ |
| 1759 | if (pkey != NULL) |
| 1760 | { |
| 1761 | if (SSL_USE_SIGALGS(s)) |
| 1762 | { |
| 1763 | int rv = tls12_check_peer_sigalg(&md, s, p, pkey); |
| 1764 | if (rv == -1) |
| 1765 | goto err; |
| 1766 | else if (rv == 0) |
| 1767 | { |
| 1768 | al = SSL_AD_DECODE_ERROR; |
| 1769 | goto f_err; |
| 1770 | } |
| 1771 | #ifdef SSL_DEBUG |
| 1772 | fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md)); |
| 1773 | #endif |
| 1774 | p += 2; |
| 1775 | n -= 2; |
| 1776 | } |
| 1777 | else |
| 1778 | md = EVP_sha1(); |
| 1779 | |
| 1780 | n2s(p,i); |
| 1781 | n-=2; |
| 1782 | j=EVP_PKEY_size(pkey); |
| 1783 | |
| 1784 | if ((i != n) || (n > j) || (n <= 0)) |
| 1785 | { |
| 1786 | /* wrong packet length */ |
| 1787 | al=SSL_AD_DECODE_ERROR; |
| 1788 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_WRONG_SIGNATURE_LENGTH); |
| 1789 | goto f_err; |
| 1790 | } |
| 1791 | |
| 1792 | #ifndef OPENSSL_NO_RSA |
| 1793 | if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) |
| 1794 | { |
| 1795 | int num; |
| 1796 | |
| 1797 | j=0; |
| 1798 | q=md_buf; |
| 1799 | for (num=2; num > 0; num--) |
| 1800 | { |
| 1801 | EVP_DigestInit_ex(&md_ctx,(num == 2) |
| 1802 | ?s->ctx->md5:s->ctx->sha1, NULL); |
| 1803 | EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); |
| 1804 | EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); |
| 1805 | EVP_DigestUpdate(&md_ctx,param,param_len); |
| 1806 | EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i); |
| 1807 | q+=i; |
| 1808 | j+=i; |
| 1809 | } |
| 1810 | i=RSA_verify(NID_md5_sha1, md_buf, j, p, n, |
| 1811 | pkey->pkey.rsa); |
| 1812 | if (i < 0) |
| 1813 | { |
| 1814 | al=SSL_AD_DECRYPT_ERROR; |
| 1815 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_RSA_DECRYPT); |
| 1816 | goto f_err; |
| 1817 | } |
| 1818 | if (i == 0) |
| 1819 | { |
| 1820 | /* bad signature */ |
| 1821 | al=SSL_AD_DECRYPT_ERROR; |
| 1822 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_SIGNATURE); |
| 1823 | goto f_err; |
| 1824 | } |
| 1825 | } |
| 1826 | else |
| 1827 | #endif |
| 1828 | { |
| 1829 | EVP_VerifyInit_ex(&md_ctx, md, NULL); |
| 1830 | EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); |
| 1831 | EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); |
| 1832 | EVP_VerifyUpdate(&md_ctx,param,param_len); |
| 1833 | if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0) |
| 1834 | { |
| 1835 | /* bad signature */ |
| 1836 | al=SSL_AD_DECRYPT_ERROR; |
| 1837 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_BAD_SIGNATURE); |
| 1838 | goto f_err; |
| 1839 | } |
| 1840 | } |
| 1841 | } |
| 1842 | else |
| 1843 | { |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1844 | if (!(alg_a & SSL_aNULL) && |
| 1845 | /* Among PSK ciphers only RSA_PSK needs a public key */ |
| 1846 | !((alg_a & SSL_aPSK) && !(alg_k & SSL_kRSA))) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1847 | { |
| 1848 | /* Might be wrong key type, check it */ |
| 1849 | if (ssl3_check_cert_and_algorithm(s)) |
| 1850 | /* Otherwise this shouldn't happen */ |
| 1851 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, ERR_R_INTERNAL_ERROR); |
| 1852 | goto err; |
| 1853 | } |
| 1854 | /* still data left over */ |
| 1855 | if (n != 0) |
| 1856 | { |
| 1857 | al=SSL_AD_DECODE_ERROR; |
| 1858 | OPENSSL_PUT_ERROR(SSL, ssl3_get_key_exchange, SSL_R_EXTRA_DATA_IN_MESSAGE); |
| 1859 | goto f_err; |
| 1860 | } |
| 1861 | } |
| 1862 | EVP_PKEY_free(pkey); |
| 1863 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1864 | return(1); |
| 1865 | f_err: |
| 1866 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1867 | err: |
| 1868 | EVP_PKEY_free(pkey); |
| 1869 | #ifndef OPENSSL_NO_RSA |
| 1870 | if (rsa != NULL) |
| 1871 | RSA_free(rsa); |
| 1872 | #endif |
| 1873 | #ifndef OPENSSL_NO_DH |
| 1874 | if (dh != NULL) |
| 1875 | DH_free(dh); |
| 1876 | #endif |
| 1877 | #ifndef OPENSSL_NO_ECDH |
| 1878 | BN_CTX_free(bn_ctx); |
| 1879 | EC_POINT_free(srvr_ecpoint); |
| 1880 | if (ecdh != NULL) |
| 1881 | EC_KEY_free(ecdh); |
| 1882 | #endif |
| 1883 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1884 | return(-1); |
| 1885 | } |
| 1886 | |
| 1887 | static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) |
| 1888 | { |
| 1889 | return(X509_NAME_cmp(*a,*b)); |
| 1890 | } |
| 1891 | |
| 1892 | int ssl3_get_certificate_request(SSL *s) |
| 1893 | { |
| 1894 | int ok,ret=0; |
| 1895 | unsigned long n,nc,l; |
| 1896 | unsigned int llen, ctype_num,i; |
| 1897 | X509_NAME *xn=NULL; |
| 1898 | const unsigned char *p,*q; |
| 1899 | unsigned char *d; |
| 1900 | STACK_OF(X509_NAME) *ca_sk=NULL; |
| 1901 | |
| 1902 | n=s->method->ssl_get_message(s, |
| 1903 | SSL3_ST_CR_CERT_REQ_A, |
| 1904 | SSL3_ST_CR_CERT_REQ_B, |
| 1905 | -1, |
| 1906 | s->max_cert_list, |
| 1907 | &ok); |
| 1908 | |
| 1909 | if (!ok) return((int)n); |
| 1910 | |
| 1911 | s->s3->tmp.cert_req=0; |
| 1912 | |
| 1913 | if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) |
| 1914 | { |
| 1915 | s->s3->tmp.reuse_message=1; |
| 1916 | /* If we get here we don't need any cached handshake records |
| 1917 | * as we wont be doing client auth. |
| 1918 | */ |
| 1919 | if (s->s3->handshake_buffer) |
| 1920 | { |
| 1921 | if (!ssl3_digest_cached_records(s)) |
| 1922 | goto err; |
| 1923 | } |
| 1924 | return(1); |
| 1925 | } |
| 1926 | |
| 1927 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) |
| 1928 | { |
| 1929 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE); |
| 1930 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_WRONG_MESSAGE_TYPE); |
| 1931 | goto err; |
| 1932 | } |
| 1933 | |
| 1934 | /* TLS does not like anon-DH with client cert */ |
| 1935 | if (s->version > SSL3_VERSION) |
| 1936 | { |
| 1937 | if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) |
| 1938 | { |
| 1939 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE); |
| 1940 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER); |
| 1941 | goto err; |
| 1942 | } |
| 1943 | } |
| 1944 | |
| 1945 | p=d=(unsigned char *)s->init_msg; |
| 1946 | |
| 1947 | ca_sk = sk_X509_NAME_new(ca_dn_cmp); |
| 1948 | if (ca_sk == NULL) |
| 1949 | { |
| 1950 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE); |
| 1951 | goto err; |
| 1952 | } |
| 1953 | |
| 1954 | /* get the certificate types */ |
| 1955 | ctype_num= *(p++); |
| 1956 | if (s->cert->ctypes) |
| 1957 | { |
| 1958 | OPENSSL_free(s->cert->ctypes); |
| 1959 | s->cert->ctypes = NULL; |
| 1960 | } |
| 1961 | if (ctype_num > SSL3_CT_NUMBER) |
| 1962 | { |
| 1963 | /* If we exceed static buffer copy all to cert structure */ |
| 1964 | s->cert->ctypes = OPENSSL_malloc(ctype_num); |
| 1965 | memcpy(s->cert->ctypes, p, ctype_num); |
| 1966 | s->cert->ctype_num = (size_t)ctype_num; |
| 1967 | ctype_num=SSL3_CT_NUMBER; |
| 1968 | } |
| 1969 | for (i=0; i<ctype_num; i++) |
| 1970 | s->s3->tmp.ctype[i]= p[i]; |
| 1971 | p+=p[-1]; |
| 1972 | if (SSL_USE_SIGALGS(s)) |
| 1973 | { |
| 1974 | n2s(p, llen); |
| 1975 | /* Check we have enough room for signature algorithms and |
| 1976 | * following length value. |
| 1977 | */ |
| 1978 | if ((unsigned long)(p - d + llen + 2) > n) |
| 1979 | { |
| 1980 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
| 1981 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DATA_LENGTH_TOO_LONG); |
| 1982 | goto err; |
| 1983 | } |
| 1984 | /* Clear certificate digests and validity flags */ |
| 1985 | for (i = 0; i < SSL_PKEY_NUM; i++) |
| 1986 | { |
| 1987 | s->cert->pkeys[i].digest = NULL; |
| 1988 | s->cert->pkeys[i].valid_flags = 0; |
| 1989 | } |
| 1990 | if ((llen & 1) || !tls1_process_sigalgs(s, p, llen)) |
| 1991 | { |
| 1992 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
| 1993 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_SIGNATURE_ALGORITHMS_ERROR); |
| 1994 | goto err; |
| 1995 | } |
| 1996 | p += llen; |
| 1997 | } |
| 1998 | |
| 1999 | /* get the CA RDNs */ |
| 2000 | n2s(p,llen); |
| 2001 | #if 0 |
| 2002 | { |
| 2003 | FILE *out; |
| 2004 | out=fopen("/tmp/vsign.der","w"); |
| 2005 | fwrite(p,1,llen,out); |
| 2006 | fclose(out); |
| 2007 | } |
| 2008 | #endif |
| 2009 | |
| 2010 | if ((unsigned long)(p - d + llen) != n) |
| 2011 | { |
| 2012 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
| 2013 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH); |
| 2014 | goto err; |
| 2015 | } |
| 2016 | |
| 2017 | for (nc=0; nc<llen; ) |
| 2018 | { |
| 2019 | n2s(p,l); |
| 2020 | if ((l+nc+2) > llen) |
| 2021 | { |
| 2022 | if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) |
| 2023 | goto cont; /* netscape bugs */ |
| 2024 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
| 2025 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_TOO_LONG); |
| 2026 | goto err; |
| 2027 | } |
| 2028 | |
| 2029 | q=p; |
| 2030 | |
| 2031 | if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL) |
| 2032 | { |
| 2033 | /* If netscape tolerance is on, ignore errors */ |
| 2034 | if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG) |
| 2035 | goto cont; |
| 2036 | else |
| 2037 | { |
| 2038 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
| 2039 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB); |
| 2040 | goto err; |
| 2041 | } |
| 2042 | } |
| 2043 | |
| 2044 | if (q != (p+l)) |
| 2045 | { |
| 2046 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
| 2047 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_LENGTH_MISMATCH); |
| 2048 | goto err; |
| 2049 | } |
| 2050 | if (!sk_X509_NAME_push(ca_sk,xn)) |
| 2051 | { |
| 2052 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE); |
| 2053 | goto err; |
| 2054 | } |
| 2055 | |
| 2056 | p+=l; |
| 2057 | nc+=l+2; |
| 2058 | } |
| 2059 | |
| 2060 | if (0) |
| 2061 | { |
| 2062 | cont: |
| 2063 | ERR_clear_error(); |
| 2064 | } |
| 2065 | |
| 2066 | /* we should setup a certificate to return.... */ |
| 2067 | s->s3->tmp.cert_req=1; |
| 2068 | s->s3->tmp.ctype_num=ctype_num; |
| 2069 | if (s->s3->tmp.ca_names != NULL) |
| 2070 | sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free); |
| 2071 | s->s3->tmp.ca_names=ca_sk; |
| 2072 | ca_sk=NULL; |
| 2073 | |
| 2074 | ret=1; |
| 2075 | err: |
| 2076 | if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free); |
| 2077 | return(ret); |
| 2078 | } |
| 2079 | |
| 2080 | #ifndef OPENSSL_NO_TLSEXT |
| 2081 | int ssl3_get_new_session_ticket(SSL *s) |
| 2082 | { |
| 2083 | int ok,al,ret=0, ticklen; |
| 2084 | long n; |
| 2085 | const unsigned char *p; |
| 2086 | unsigned char *d; |
| 2087 | |
| 2088 | n=s->method->ssl_get_message(s, |
| 2089 | SSL3_ST_CR_SESSION_TICKET_A, |
| 2090 | SSL3_ST_CR_SESSION_TICKET_B, |
| 2091 | -1, |
| 2092 | 16384, |
| 2093 | &ok); |
| 2094 | |
| 2095 | if (!ok) |
| 2096 | return((int)n); |
| 2097 | |
| 2098 | if (s->s3->tmp.message_type == SSL3_MT_FINISHED) |
| 2099 | { |
| 2100 | s->s3->tmp.reuse_message=1; |
| 2101 | return(1); |
| 2102 | } |
| 2103 | if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET) |
| 2104 | { |
| 2105 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 2106 | OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_BAD_MESSAGE_TYPE); |
| 2107 | goto f_err; |
| 2108 | } |
| 2109 | if (n < 6) |
| 2110 | { |
| 2111 | /* need at least ticket_lifetime_hint + ticket length */ |
| 2112 | al = SSL_AD_DECODE_ERROR; |
| 2113 | OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_LENGTH_MISMATCH); |
| 2114 | goto f_err; |
| 2115 | } |
| 2116 | |
| 2117 | p=d=(unsigned char *)s->init_msg; |
| 2118 | n2l(p, s->session->tlsext_tick_lifetime_hint); |
| 2119 | n2s(p, ticklen); |
| 2120 | /* ticket_lifetime_hint + ticket_length + ticket */ |
| 2121 | if (ticklen + 6 != n) |
| 2122 | { |
| 2123 | al = SSL_AD_DECODE_ERROR; |
| 2124 | OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_LENGTH_MISMATCH); |
| 2125 | goto f_err; |
| 2126 | } |
| 2127 | if (s->session->tlsext_tick) |
| 2128 | { |
| 2129 | OPENSSL_free(s->session->tlsext_tick); |
| 2130 | s->session->tlsext_ticklen = 0; |
| 2131 | } |
| 2132 | s->session->tlsext_tick = OPENSSL_malloc(ticklen); |
| 2133 | if (!s->session->tlsext_tick) |
| 2134 | { |
| 2135 | OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE); |
| 2136 | goto err; |
| 2137 | } |
| 2138 | memcpy(s->session->tlsext_tick, p, ticklen); |
| 2139 | s->session->tlsext_ticklen = ticklen; |
| 2140 | /* There are two ways to detect a resumed ticket sesion. |
| 2141 | * One is to set an appropriate session ID and then the server |
| 2142 | * must return a match in ServerHello. This allows the normal |
| 2143 | * client session ID matching to work and we know much |
| 2144 | * earlier that the ticket has been accepted. |
| 2145 | * |
| 2146 | * The other way is to set zero length session ID when the |
| 2147 | * ticket is presented and rely on the handshake to determine |
| 2148 | * session resumption. |
| 2149 | * |
| 2150 | * We choose the former approach because this fits in with |
| 2151 | * assumptions elsewhere in OpenSSL. The session ID is set |
| 2152 | * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the |
| 2153 | * ticket. |
| 2154 | */ |
| 2155 | EVP_Digest(p, ticklen, |
| 2156 | s->session->session_id, &s->session->session_id_length, |
| 2157 | #ifndef OPENSSL_NO_SHA256 |
| 2158 | EVP_sha256(), NULL); |
| 2159 | #else |
| 2160 | EVP_sha1(), NULL); |
| 2161 | #endif |
| 2162 | ret=1; |
| 2163 | return(ret); |
| 2164 | f_err: |
| 2165 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 2166 | err: |
| 2167 | return(-1); |
| 2168 | } |
| 2169 | |
| 2170 | int ssl3_get_cert_status(SSL *s) |
| 2171 | { |
| 2172 | int ok, al; |
| 2173 | unsigned long resplen,n; |
| 2174 | const unsigned char *p; |
| 2175 | |
| 2176 | n=s->method->ssl_get_message(s, |
| 2177 | SSL3_ST_CR_CERT_STATUS_A, |
| 2178 | SSL3_ST_CR_CERT_STATUS_B, |
| 2179 | SSL3_MT_CERTIFICATE_STATUS, |
| 2180 | 16384, |
| 2181 | &ok); |
| 2182 | |
| 2183 | if (!ok) return((int)n); |
| 2184 | if (n < 4) |
| 2185 | { |
| 2186 | /* need at least status type + length */ |
| 2187 | al = SSL_AD_DECODE_ERROR; |
| 2188 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_LENGTH_MISMATCH); |
| 2189 | goto f_err; |
| 2190 | } |
| 2191 | p = (unsigned char *)s->init_msg; |
| 2192 | if (*p++ != TLSEXT_STATUSTYPE_ocsp) |
| 2193 | { |
| 2194 | al = SSL_AD_DECODE_ERROR; |
| 2195 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_UNSUPPORTED_STATUS_TYPE); |
| 2196 | goto f_err; |
| 2197 | } |
| 2198 | n2l3(p, resplen); |
| 2199 | if (resplen + 4 != n) |
| 2200 | { |
| 2201 | al = SSL_AD_DECODE_ERROR; |
| 2202 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_LENGTH_MISMATCH); |
| 2203 | goto f_err; |
| 2204 | } |
| 2205 | if (s->tlsext_ocsp_resp) |
| 2206 | OPENSSL_free(s->tlsext_ocsp_resp); |
| 2207 | s->tlsext_ocsp_resp = BUF_memdup(p, resplen); |
| 2208 | if (!s->tlsext_ocsp_resp) |
| 2209 | { |
| 2210 | al = SSL_AD_INTERNAL_ERROR; |
| 2211 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE); |
| 2212 | goto f_err; |
| 2213 | } |
| 2214 | s->tlsext_ocsp_resplen = resplen; |
| 2215 | if (s->ctx->tlsext_status_cb) |
| 2216 | { |
| 2217 | int ret; |
| 2218 | ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); |
| 2219 | if (ret == 0) |
| 2220 | { |
| 2221 | al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; |
| 2222 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_INVALID_STATUS_RESPONSE); |
| 2223 | goto f_err; |
| 2224 | } |
| 2225 | if (ret < 0) |
| 2226 | { |
| 2227 | al = SSL_AD_INTERNAL_ERROR; |
| 2228 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE); |
| 2229 | goto f_err; |
| 2230 | } |
| 2231 | } |
| 2232 | return 1; |
| 2233 | f_err: |
| 2234 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 2235 | return(-1); |
| 2236 | } |
| 2237 | #endif |
| 2238 | |
| 2239 | int ssl3_get_server_done(SSL *s) |
| 2240 | { |
| 2241 | int ok,ret=0; |
| 2242 | long n; |
| 2243 | |
| 2244 | n=s->method->ssl_get_message(s, |
| 2245 | SSL3_ST_CR_SRVR_DONE_A, |
| 2246 | SSL3_ST_CR_SRVR_DONE_B, |
| 2247 | SSL3_MT_SERVER_DONE, |
| 2248 | 30, /* should be very small, like 0 :-) */ |
| 2249 | &ok); |
| 2250 | |
| 2251 | if (!ok) return((int)n); |
| 2252 | if (n > 0) |
| 2253 | { |
| 2254 | /* should contain no data */ |
| 2255 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
| 2256 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH); |
| 2257 | return -1; |
| 2258 | } |
| 2259 | ret=1; |
| 2260 | return(ret); |
| 2261 | } |
| 2262 | |
| 2263 | |
| 2264 | int ssl3_send_client_key_exchange(SSL *s) |
| 2265 | { |
| 2266 | unsigned char *p; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2267 | int n = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2268 | unsigned long alg_k; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2269 | unsigned long alg_a; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2270 | #ifndef OPENSSL_NO_RSA |
| 2271 | unsigned char *q; |
| 2272 | EVP_PKEY *pkey=NULL; |
| 2273 | #endif |
| 2274 | #ifndef OPENSSL_NO_ECDH |
| 2275 | EC_KEY *clnt_ecdh = NULL; |
| 2276 | const EC_POINT *srvr_ecpoint = NULL; |
| 2277 | EVP_PKEY *srvr_pub_pkey = NULL; |
| 2278 | unsigned char *encodedPoint = NULL; |
| 2279 | int encoded_pt_len = 0; |
| 2280 | BN_CTX * bn_ctx = NULL; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2281 | #ifndef OPENSSL_NO_PSK |
| 2282 | unsigned int psk_len = 0; |
| 2283 | unsigned char psk[PSK_MAX_PSK_LEN]; |
| 2284 | #endif /* OPENSSL_NO_PSK */ |
| 2285 | #endif /* OPENSSL_NO_ECDH */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2286 | |
| 2287 | if (s->state == SSL3_ST_CW_KEY_EXCH_A) |
| 2288 | { |
| 2289 | p = ssl_handshake_start(s); |
| 2290 | |
| 2291 | alg_k=s->s3->tmp.new_cipher->algorithm_mkey; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2292 | alg_a=s->s3->tmp.new_cipher->algorithm_auth; |
| 2293 | |
| 2294 | #ifndef OPENSSL_NO_PSK |
| 2295 | if (alg_a & SSL_aPSK) |
| 2296 | { |
Adam Langley | 01797e3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2297 | char identity[PSK_MAX_IDENTITY_LEN + 1]; |
| 2298 | size_t identity_len; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2299 | unsigned char *t = NULL; |
| 2300 | unsigned char pre_ms[PSK_MAX_PSK_LEN*2+4]; |
| 2301 | unsigned int pre_ms_len = 0; |
| 2302 | int psk_err = 1; |
| 2303 | |
| 2304 | n = 0; |
| 2305 | if (s->psk_client_callback == NULL) |
| 2306 | { |
| 2307 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_NO_CLIENT_CB); |
| 2308 | goto err; |
| 2309 | } |
| 2310 | |
Adam Langley | 01797e3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2311 | memset(identity, 0, sizeof(identity)); |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2312 | psk_len = s->psk_client_callback(s, s->session->psk_identity_hint, |
Adam Langley | 01797e3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2313 | identity, sizeof(identity), psk, sizeof(psk)); |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2314 | if (psk_len > PSK_MAX_PSK_LEN) |
| 2315 | { |
| 2316 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR); |
| 2317 | goto psk_err; |
| 2318 | } |
| 2319 | else if (psk_len == 0) |
| 2320 | { |
| 2321 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_IDENTITY_NOT_FOUND); |
| 2322 | goto psk_err; |
| 2323 | } |
Adam Langley | 01797e3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2324 | identity_len = OPENSSL_strnlen(identity, sizeof(identity)); |
| 2325 | if (identity_len > PSK_MAX_IDENTITY_LEN) |
| 2326 | { |
| 2327 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR); |
| 2328 | goto psk_err; |
| 2329 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2330 | |
| 2331 | if (!(alg_k & SSL_kEECDH)) |
| 2332 | { |
| 2333 | /* Create the shared secret now if we're not using ECDHE-PSK.*/ |
| 2334 | pre_ms_len = 2+psk_len+2+psk_len; |
| 2335 | t = pre_ms; |
| 2336 | s2n(psk_len, t); |
| 2337 | memset(t, 0, psk_len); |
| 2338 | t+=psk_len; |
| 2339 | s2n(psk_len, t); |
| 2340 | memcpy(t, psk, psk_len); |
| 2341 | |
| 2342 | s->session->master_key_length = |
| 2343 | s->method->ssl3_enc->generate_master_secret(s, |
| 2344 | s->session->master_key, |
| 2345 | pre_ms, pre_ms_len); |
Adam Langley | 01797e3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2346 | s2n(identity_len, p); |
| 2347 | memcpy(p, identity, identity_len); |
| 2348 | n = 2 + identity_len; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2349 | } |
| 2350 | |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2351 | if (s->session->psk_identity != NULL) |
| 2352 | OPENSSL_free(s->session->psk_identity); |
| 2353 | s->session->psk_identity = BUF_strdup(identity); |
| 2354 | if (s->session->psk_identity == NULL) |
| 2355 | { |
| 2356 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2357 | goto psk_err; |
| 2358 | } |
| 2359 | psk_err = 0; |
| 2360 | psk_err: |
| 2361 | OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN); |
| 2362 | OPENSSL_cleanse(pre_ms, sizeof(pre_ms)); |
| 2363 | if (psk_err != 0) |
| 2364 | { |
| 2365 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 2366 | goto err; |
| 2367 | } |
| 2368 | } |
| 2369 | #endif |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2370 | |
| 2371 | /* Fool emacs indentation */ |
| 2372 | if (0) {} |
| 2373 | #ifndef OPENSSL_NO_RSA |
| 2374 | else if (alg_k & SSL_kRSA) |
| 2375 | { |
| 2376 | RSA *rsa; |
| 2377 | unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; |
| 2378 | |
| 2379 | if (s->session->sess_cert->peer_rsa_tmp != NULL) |
| 2380 | rsa=s->session->sess_cert->peer_rsa_tmp; |
| 2381 | else |
| 2382 | { |
| 2383 | pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); |
| 2384 | if ((pkey == NULL) || |
| 2385 | (pkey->type != EVP_PKEY_RSA) || |
| 2386 | (pkey->pkey.rsa == NULL)) |
| 2387 | { |
| 2388 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR); |
| 2389 | goto err; |
| 2390 | } |
| 2391 | rsa=pkey->pkey.rsa; |
| 2392 | EVP_PKEY_free(pkey); |
| 2393 | } |
| 2394 | |
| 2395 | tmp_buf[0]=s->client_version>>8; |
| 2396 | tmp_buf[1]=s->client_version&0xff; |
| 2397 | if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0) |
| 2398 | goto err; |
| 2399 | |
| 2400 | s->session->master_key_length=sizeof tmp_buf; |
| 2401 | |
| 2402 | q=p; |
| 2403 | /* Fix buf for TLS and beyond */ |
| 2404 | if (s->version > SSL3_VERSION) |
| 2405 | p+=2; |
| 2406 | n=RSA_public_encrypt(sizeof tmp_buf, |
| 2407 | tmp_buf,p,rsa,RSA_PKCS1_PADDING); |
| 2408 | #ifdef PKCS1_CHECK |
| 2409 | if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++; |
| 2410 | if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70; |
| 2411 | #endif |
| 2412 | if (n <= 0) |
| 2413 | { |
| 2414 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_RSA_ENCRYPT); |
| 2415 | goto err; |
| 2416 | } |
| 2417 | |
| 2418 | /* Fix buf for TLS and beyond */ |
| 2419 | if (s->version > SSL3_VERSION) |
| 2420 | { |
| 2421 | s2n(n,q); |
| 2422 | n+=2; |
| 2423 | } |
| 2424 | |
| 2425 | s->session->master_key_length= |
| 2426 | s->method->ssl3_enc->generate_master_secret(s, |
| 2427 | s->session->master_key, |
| 2428 | tmp_buf,sizeof tmp_buf); |
| 2429 | OPENSSL_cleanse(tmp_buf,sizeof tmp_buf); |
| 2430 | } |
| 2431 | #endif |
| 2432 | #ifndef OPENSSL_NO_DH |
| 2433 | else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) |
| 2434 | { |
| 2435 | DH *dh_srvr,*dh_clnt; |
| 2436 | SESS_CERT *scert = s->session->sess_cert; |
| 2437 | |
| 2438 | if (scert == NULL) |
| 2439 | { |
| 2440 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE); |
| 2441 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE); |
| 2442 | goto err; |
| 2443 | } |
| 2444 | |
| 2445 | if (scert->peer_dh_tmp != NULL) |
| 2446 | dh_srvr=scert->peer_dh_tmp; |
| 2447 | else |
| 2448 | { |
| 2449 | /* we get them from the cert */ |
| 2450 | int idx = scert->peer_cert_type; |
| 2451 | EVP_PKEY *spkey = NULL; |
| 2452 | dh_srvr = NULL; |
| 2453 | if (idx >= 0) |
| 2454 | spkey = X509_get_pubkey( |
| 2455 | scert->peer_pkeys[idx].x509); |
| 2456 | if (spkey) |
| 2457 | { |
| 2458 | dh_srvr = EVP_PKEY_get1_DH(spkey); |
| 2459 | EVP_PKEY_free(spkey); |
| 2460 | } |
| 2461 | if (dh_srvr == NULL) |
| 2462 | { |
| 2463 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR); |
| 2464 | goto err; |
| 2465 | } |
| 2466 | } |
| 2467 | if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) |
| 2468 | { |
| 2469 | /* Use client certificate key */ |
| 2470 | EVP_PKEY *clkey = s->cert->key->privatekey; |
| 2471 | dh_clnt = NULL; |
| 2472 | if (clkey) |
| 2473 | dh_clnt = EVP_PKEY_get1_DH(clkey); |
| 2474 | if (dh_clnt == NULL) |
| 2475 | { |
| 2476 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR); |
| 2477 | goto err; |
| 2478 | } |
| 2479 | } |
| 2480 | else |
| 2481 | { |
| 2482 | /* generate a new random key */ |
| 2483 | if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL) |
| 2484 | { |
| 2485 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB); |
| 2486 | goto err; |
| 2487 | } |
| 2488 | if (!DH_generate_key(dh_clnt)) |
| 2489 | { |
| 2490 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB); |
| 2491 | DH_free(dh_clnt); |
| 2492 | goto err; |
| 2493 | } |
| 2494 | } |
| 2495 | |
| 2496 | /* use the 'p' output buffer for the DH key, but |
| 2497 | * make sure to clear it out afterwards */ |
| 2498 | |
| 2499 | n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt); |
| 2500 | if (scert->peer_dh_tmp == NULL) |
| 2501 | DH_free(dh_srvr); |
| 2502 | |
| 2503 | if (n <= 0) |
| 2504 | { |
| 2505 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB); |
| 2506 | DH_free(dh_clnt); |
| 2507 | goto err; |
| 2508 | } |
| 2509 | |
| 2510 | /* generate master key from the result */ |
| 2511 | s->session->master_key_length= |
| 2512 | s->method->ssl3_enc->generate_master_secret(s, |
| 2513 | s->session->master_key,p,n); |
| 2514 | /* clean up */ |
| 2515 | memset(p,0,n); |
| 2516 | |
| 2517 | if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) |
| 2518 | n = 0; |
| 2519 | else |
| 2520 | { |
| 2521 | /* send off the data */ |
| 2522 | n=BN_num_bytes(dh_clnt->pub_key); |
| 2523 | s2n(n,p); |
| 2524 | BN_bn2bin(dh_clnt->pub_key,p); |
| 2525 | n+=2; |
| 2526 | } |
| 2527 | |
| 2528 | DH_free(dh_clnt); |
| 2529 | |
| 2530 | /* perhaps clean things up a bit EAY EAY EAY EAY*/ |
| 2531 | } |
| 2532 | #endif |
| 2533 | |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2534 | #ifndef OPENSSL_NO_ECDH |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2535 | else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) |
| 2536 | { |
| 2537 | const EC_GROUP *srvr_group = NULL; |
| 2538 | EC_KEY *tkey; |
| 2539 | int ecdh_clnt_cert = 0; |
| 2540 | int field_size = 0; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2541 | #ifndef OPENSSL_NO_PSK |
| 2542 | unsigned char *pre_ms; |
| 2543 | unsigned char *t; |
| 2544 | unsigned int pre_ms_len; |
| 2545 | unsigned int i; |
| 2546 | #endif |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2547 | |
Adam Langley | d06eddd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2548 | if (s->session->sess_cert == NULL) |
| 2549 | { |
| 2550 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE); |
| 2551 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE); |
| 2552 | goto err; |
| 2553 | } |
| 2554 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2555 | /* Did we send out the client's |
| 2556 | * ECDH share for use in premaster |
| 2557 | * computation as part of client certificate? |
| 2558 | * If so, set ecdh_clnt_cert to 1. |
| 2559 | */ |
| 2560 | if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL)) |
| 2561 | { |
| 2562 | /* XXX: For now, we do not support client |
| 2563 | * authentication using ECDH certificates. |
| 2564 | * To add such support, one needs to add |
| 2565 | * code that checks for appropriate |
| 2566 | * conditions and sets ecdh_clnt_cert to 1. |
| 2567 | * For example, the cert have an ECC |
| 2568 | * key on the same curve as the server's |
| 2569 | * and the key should be authorized for |
| 2570 | * key agreement. |
| 2571 | * |
| 2572 | * One also needs to add code in ssl3_connect |
| 2573 | * to skip sending the certificate verify |
| 2574 | * message. |
| 2575 | * |
| 2576 | * if ((s->cert->key->privatekey != NULL) && |
| 2577 | * (s->cert->key->privatekey->type == |
| 2578 | * EVP_PKEY_EC) && ...) |
| 2579 | * ecdh_clnt_cert = 1; |
| 2580 | */ |
| 2581 | } |
| 2582 | |
| 2583 | if (s->session->sess_cert->peer_ecdh_tmp != NULL) |
| 2584 | { |
| 2585 | tkey = s->session->sess_cert->peer_ecdh_tmp; |
| 2586 | } |
| 2587 | else |
| 2588 | { |
| 2589 | /* Get the Server Public Key from Cert */ |
| 2590 | srvr_pub_pkey = X509_get_pubkey(s->session-> \ |
| 2591 | sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); |
| 2592 | if ((srvr_pub_pkey == NULL) || |
| 2593 | (srvr_pub_pkey->type != EVP_PKEY_EC) || |
| 2594 | (srvr_pub_pkey->pkey.ec == NULL)) |
| 2595 | { |
| 2596 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR); |
| 2597 | goto err; |
| 2598 | } |
| 2599 | |
| 2600 | tkey = srvr_pub_pkey->pkey.ec; |
| 2601 | } |
| 2602 | |
| 2603 | srvr_group = EC_KEY_get0_group(tkey); |
| 2604 | srvr_ecpoint = EC_KEY_get0_public_key(tkey); |
| 2605 | |
| 2606 | if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) |
| 2607 | { |
| 2608 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR); |
| 2609 | goto err; |
| 2610 | } |
| 2611 | |
| 2612 | if ((clnt_ecdh=EC_KEY_new()) == NULL) |
| 2613 | { |
| 2614 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2615 | goto err; |
| 2616 | } |
| 2617 | |
| 2618 | if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) |
| 2619 | { |
| 2620 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB); |
| 2621 | goto err; |
| 2622 | } |
| 2623 | if (ecdh_clnt_cert) |
| 2624 | { |
| 2625 | /* Reuse key info from our certificate |
| 2626 | * We only need our private key to perform |
| 2627 | * the ECDH computation. |
| 2628 | */ |
| 2629 | const BIGNUM *priv_key; |
| 2630 | tkey = s->cert->key->privatekey->pkey.ec; |
| 2631 | priv_key = EC_KEY_get0_private_key(tkey); |
| 2632 | if (priv_key == NULL) |
| 2633 | { |
| 2634 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2635 | goto err; |
| 2636 | } |
| 2637 | if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) |
| 2638 | { |
| 2639 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB); |
| 2640 | goto err; |
| 2641 | } |
| 2642 | } |
| 2643 | else |
| 2644 | { |
| 2645 | /* Generate a new ECDH key pair */ |
| 2646 | if (!(EC_KEY_generate_key(clnt_ecdh))) |
| 2647 | { |
| 2648 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB); |
| 2649 | goto err; |
| 2650 | } |
| 2651 | } |
| 2652 | |
| 2653 | /* use the 'p' output buffer for the ECDH key, but |
| 2654 | * make sure to clear it out afterwards |
| 2655 | */ |
| 2656 | |
| 2657 | field_size = EC_GROUP_get_degree(srvr_group); |
| 2658 | if (field_size <= 0) |
| 2659 | { |
| 2660 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB); |
| 2661 | goto err; |
| 2662 | } |
| 2663 | n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL); |
| 2664 | if (n <= 0) |
| 2665 | { |
| 2666 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB); |
| 2667 | goto err; |
| 2668 | } |
| 2669 | |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2670 | #ifndef OPENSSL_NO_PSK |
| 2671 | /* ECDHE PSK ciphersuites from RFC 5489 */ |
| 2672 | if ((alg_a & SSL_aPSK) && psk_len != 0) |
| 2673 | { |
| 2674 | pre_ms_len = 2+psk_len+2+n; |
| 2675 | pre_ms = OPENSSL_malloc(pre_ms_len); |
| 2676 | if (pre_ms == NULL) |
| 2677 | { |
| 2678 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2679 | goto err; |
| 2680 | } |
| 2681 | memset(pre_ms, 0, pre_ms_len); |
| 2682 | t = pre_ms; |
| 2683 | s2n(psk_len, t); |
| 2684 | memcpy(t, psk, psk_len); |
| 2685 | t += psk_len; |
| 2686 | s2n(n, t); |
| 2687 | memcpy(t, p, n); |
| 2688 | s->session->master_key_length = s->method->ssl3_enc \ |
| 2689 | -> generate_master_secret(s, |
| 2690 | s->session->master_key, pre_ms, pre_ms_len); |
| 2691 | OPENSSL_cleanse(pre_ms, pre_ms_len); |
| 2692 | OPENSSL_free(pre_ms); |
| 2693 | } |
| 2694 | #endif /* OPENSSL_NO_PSK */ |
| 2695 | if (!(alg_a & SSL_aPSK)) |
| 2696 | { |
| 2697 | /* generate master key from the result */ |
| 2698 | s->session->master_key_length = s->method->ssl3_enc \ |
| 2699 | -> generate_master_secret(s, |
| 2700 | s->session->master_key, p, n); |
| 2701 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2702 | memset(p, 0, n); /* clean up */ |
| 2703 | |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2704 | if (ecdh_clnt_cert) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2705 | { |
| 2706 | /* Send empty client key exch message */ |
| 2707 | n = 0; |
| 2708 | } |
| 2709 | else |
| 2710 | { |
| 2711 | /* First check the size of encoding and |
| 2712 | * allocate memory accordingly. |
| 2713 | */ |
| 2714 | encoded_pt_len = |
| 2715 | EC_POINT_point2oct(srvr_group, |
| 2716 | EC_KEY_get0_public_key(clnt_ecdh), |
| 2717 | POINT_CONVERSION_UNCOMPRESSED, |
| 2718 | NULL, 0, NULL); |
| 2719 | |
| 2720 | encodedPoint = (unsigned char *) |
| 2721 | OPENSSL_malloc(encoded_pt_len * |
| 2722 | sizeof(unsigned char)); |
| 2723 | bn_ctx = BN_CTX_new(); |
| 2724 | if ((encodedPoint == NULL) || |
| 2725 | (bn_ctx == NULL)) |
| 2726 | { |
| 2727 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2728 | goto err; |
| 2729 | } |
| 2730 | |
| 2731 | /* Encode the public key */ |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2732 | encoded_pt_len = EC_POINT_point2oct(srvr_group, |
| 2733 | EC_KEY_get0_public_key(clnt_ecdh), |
| 2734 | POINT_CONVERSION_UNCOMPRESSED, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2735 | encodedPoint, encoded_pt_len, bn_ctx); |
| 2736 | |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2737 | n = 0; |
| 2738 | #ifndef OPENSSL_NO_PSK |
| 2739 | if ((alg_a & SSL_aPSK) && psk_len != 0) |
| 2740 | { |
| 2741 | i = strlen(s->session->psk_identity); |
| 2742 | s2n(i, p); |
| 2743 | memcpy(p, s->session->psk_identity, i); |
| 2744 | p += i; |
| 2745 | n = i + 2; |
| 2746 | } |
| 2747 | #endif |
| 2748 | |
| 2749 | *p = encoded_pt_len; /* length of encoded point */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2750 | /* Encoded point will be copied here */ |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2751 | p += 1; |
| 2752 | n += 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2753 | /* copy the point */ |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2754 | memcpy((unsigned char *)p, encodedPoint, encoded_pt_len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2755 | /* increment n to account for length field */ |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2756 | n += encoded_pt_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2757 | } |
| 2758 | |
| 2759 | /* Free allocated memory */ |
| 2760 | BN_CTX_free(bn_ctx); |
| 2761 | if (encodedPoint != NULL) OPENSSL_free(encodedPoint); |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2762 | if (clnt_ecdh != NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2763 | EC_KEY_free(clnt_ecdh); |
| 2764 | EVP_PKEY_free(srvr_pub_pkey); |
| 2765 | } |
| 2766 | #endif /* !OPENSSL_NO_ECDH */ |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2767 | else if (alg_k & SSL_kGOST) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2768 | { |
| 2769 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_GOST_NOT_SUPPORTED); |
| 2770 | goto err; |
| 2771 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2772 | else if (!(alg_k & SSL_kPSK) || ((alg_k & SSL_kPSK) && !(alg_a & SSL_aPSK))) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2773 | { |
| 2774 | ssl3_send_alert(s, SSL3_AL_FATAL, |
| 2775 | SSL_AD_HANDSHAKE_FAILURE); |
| 2776 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR); |
| 2777 | goto err; |
| 2778 | } |
| 2779 | |
| 2780 | ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n); |
| 2781 | s->state=SSL3_ST_CW_KEY_EXCH_B; |
| 2782 | } |
| 2783 | |
| 2784 | /* SSL3_ST_CW_KEY_EXCH_B */ |
| 2785 | return ssl_do_write(s); |
| 2786 | err: |
| 2787 | #ifndef OPENSSL_NO_ECDH |
| 2788 | BN_CTX_free(bn_ctx); |
| 2789 | if (encodedPoint != NULL) OPENSSL_free(encodedPoint); |
| 2790 | if (clnt_ecdh != NULL) |
| 2791 | EC_KEY_free(clnt_ecdh); |
| 2792 | EVP_PKEY_free(srvr_pub_pkey); |
| 2793 | #endif |
| 2794 | return(-1); |
| 2795 | } |
| 2796 | |
| 2797 | int ssl3_send_client_verify(SSL *s) |
| 2798 | { |
| 2799 | unsigned char *p; |
| 2800 | unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; |
| 2801 | EVP_PKEY *pkey; |
Adam Langley | aed2306 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2802 | EVP_PKEY_CTX *pctx = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2803 | EVP_MD_CTX mctx; |
Adam Langley | aed2306 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2804 | unsigned signature_length = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2805 | unsigned long n; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2806 | |
| 2807 | EVP_MD_CTX_init(&mctx); |
| 2808 | |
| 2809 | if (s->state == SSL3_ST_CW_CERT_VRFY_A) |
| 2810 | { |
| 2811 | p= ssl_handshake_start(s); |
Adam Langley | aed2306 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2812 | pkey = s->cert->key->privatekey; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2813 | /* For TLS v1.2 send signature algorithm and signature |
| 2814 | * using agreed digest and cached handshake records. |
| 2815 | */ |
| 2816 | if (SSL_USE_SIGALGS(s)) |
| 2817 | { |
| 2818 | long hdatalen = 0; |
| 2819 | char *hdata; |
| 2820 | const EVP_MD *md = s->cert->key->digest; |
| 2821 | hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, |
| 2822 | &hdata); |
| 2823 | if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) |
| 2824 | { |
| 2825 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_INTERNAL_ERROR); |
| 2826 | goto err; |
| 2827 | } |
| 2828 | p += 2; |
| 2829 | #ifdef SSL_DEBUG |
| 2830 | fprintf(stderr, "Using TLS 1.2 with client alg %s\n", |
| 2831 | EVP_MD_name(md)); |
| 2832 | #endif |
| 2833 | if (!EVP_SignInit_ex(&mctx, md, NULL) |
| 2834 | || !EVP_SignUpdate(&mctx, hdata, hdatalen) |
Adam Langley | aed2306 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2835 | || !EVP_SignFinal(&mctx, p + 2, |
| 2836 | &signature_length, pkey)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2837 | { |
| 2838 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB); |
| 2839 | goto err; |
| 2840 | } |
Adam Langley | aed2306 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2841 | s2n(signature_length, p); |
| 2842 | n = signature_length + 4; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2843 | if (!ssl3_digest_cached_records(s)) |
| 2844 | goto err; |
| 2845 | } |
| 2846 | else |
| 2847 | #ifndef OPENSSL_NO_RSA |
| 2848 | if (pkey->type == EVP_PKEY_RSA) |
| 2849 | { |
Adam Langley | aed2306 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2850 | s->method->ssl3_enc->cert_verify_mac(s, NID_md5, data); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2851 | s->method->ssl3_enc->cert_verify_mac(s, |
Adam Langley | aed2306 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2852 | NID_sha1, &data[MD5_DIGEST_LENGTH]); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2853 | if (RSA_sign(NID_md5_sha1, data, |
Adam Langley | aed2306 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2854 | MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, |
| 2855 | &p[2], &signature_length, pkey->pkey.rsa) <= 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2856 | { |
| 2857 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_RSA_LIB); |
| 2858 | goto err; |
| 2859 | } |
Adam Langley | aed2306 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2860 | s2n(signature_length, p); |
| 2861 | n = signature_length + 2; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2862 | } |
| 2863 | else |
| 2864 | #endif |
| 2865 | #ifndef OPENSSL_NO_DSA |
Adam Langley | aed2306 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2866 | if (pkey->type == EVP_PKEY_DSA) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2867 | { |
Adam Langley | aed2306 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2868 | s->method->ssl3_enc->cert_verify_mac(s, NID_sha1, data); |
| 2869 | if (!DSA_sign(pkey->save_type, data, |
| 2870 | SHA_DIGEST_LENGTH, &(p[2]), |
| 2871 | &signature_length, pkey->pkey.dsa)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2872 | { |
| 2873 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_DSA_LIB); |
| 2874 | goto err; |
| 2875 | } |
Adam Langley | aed2306 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2876 | s2n(signature_length, p); |
| 2877 | n = signature_length + 2; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2878 | } |
| 2879 | else |
| 2880 | #endif |
| 2881 | #ifndef OPENSSL_NO_ECDSA |
Adam Langley | aed2306 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2882 | if (pkey->type == EVP_PKEY_EC) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2883 | { |
Adam Langley | aed2306 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2884 | s->method->ssl3_enc->cert_verify_mac(s, NID_sha1, data); |
| 2885 | if (!ECDSA_sign(pkey->save_type, data, |
| 2886 | SHA_DIGEST_LENGTH, &(p[2]), |
| 2887 | &signature_length, pkey->pkey.ec)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2888 | { |
| 2889 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_ECDSA_LIB); |
| 2890 | goto err; |
| 2891 | } |
Adam Langley | aed2306 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2892 | s2n(signature_length, p); |
| 2893 | n = signature_length + 2; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2894 | } |
| 2895 | else |
| 2896 | #endif |
Adam Langley | aed2306 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2897 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2898 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_INTERNAL_ERROR); |
| 2899 | goto err; |
Adam Langley | aed2306 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2900 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2901 | ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n); |
| 2902 | s->state=SSL3_ST_CW_CERT_VRFY_B; |
| 2903 | } |
| 2904 | EVP_MD_CTX_cleanup(&mctx); |
| 2905 | EVP_PKEY_CTX_free(pctx); |
| 2906 | return ssl_do_write(s); |
| 2907 | err: |
| 2908 | EVP_MD_CTX_cleanup(&mctx); |
| 2909 | EVP_PKEY_CTX_free(pctx); |
| 2910 | return(-1); |
| 2911 | } |
| 2912 | |
| 2913 | /* Check a certificate can be used for client authentication. Currently |
| 2914 | * check cert exists, if we have a suitable digest for TLS 1.2 if |
| 2915 | * static DH client certificates can be used and optionally checks |
| 2916 | * suitability for Suite B. |
| 2917 | */ |
| 2918 | static int ssl3_check_client_certificate(SSL *s) |
| 2919 | { |
| 2920 | unsigned long alg_k; |
| 2921 | if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey) |
| 2922 | return 0; |
| 2923 | /* If no suitable signature algorithm can't use certificate */ |
| 2924 | if (SSL_USE_SIGALGS(s) && !s->cert->key->digest) |
| 2925 | return 0; |
| 2926 | /* If strict mode check suitability of chain before using it. |
| 2927 | * This also adjusts suite B digest if necessary. |
| 2928 | */ |
| 2929 | if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT && |
| 2930 | !tls1_check_chain(s, NULL, NULL, NULL, -2)) |
| 2931 | return 0; |
| 2932 | alg_k=s->s3->tmp.new_cipher->algorithm_mkey; |
| 2933 | /* See if we can use client certificate for fixed DH */ |
| 2934 | if (alg_k & (SSL_kDHr|SSL_kDHd)) |
| 2935 | { |
| 2936 | SESS_CERT *scert = s->session->sess_cert; |
| 2937 | int i = scert->peer_cert_type; |
| 2938 | EVP_PKEY *clkey = NULL, *spkey = NULL; |
| 2939 | clkey = s->cert->key->privatekey; |
| 2940 | /* If client key not DH assume it can be used */ |
| 2941 | if (EVP_PKEY_id(clkey) != EVP_PKEY_DH) |
| 2942 | return 1; |
| 2943 | if (i >= 0) |
| 2944 | spkey = X509_get_pubkey(scert->peer_pkeys[i].x509); |
| 2945 | if (spkey) |
| 2946 | { |
| 2947 | /* Compare server and client parameters */ |
| 2948 | i = EVP_PKEY_cmp_parameters(clkey, spkey); |
| 2949 | EVP_PKEY_free(spkey); |
| 2950 | if (i != 1) |
| 2951 | return 0; |
| 2952 | } |
| 2953 | s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY; |
| 2954 | } |
| 2955 | return 1; |
| 2956 | } |
| 2957 | |
| 2958 | int ssl3_send_client_certificate(SSL *s) |
| 2959 | { |
| 2960 | X509 *x509=NULL; |
| 2961 | EVP_PKEY *pkey=NULL; |
| 2962 | int i; |
| 2963 | |
| 2964 | if (s->state == SSL3_ST_CW_CERT_A) |
| 2965 | { |
| 2966 | /* Let cert callback update client certificates if required */ |
| 2967 | if (s->cert->cert_cb) |
| 2968 | { |
| 2969 | i = s->cert->cert_cb(s, s->cert->cert_cb_arg); |
| 2970 | if (i < 0) |
| 2971 | { |
| 2972 | s->rwstate=SSL_X509_LOOKUP; |
| 2973 | return -1; |
| 2974 | } |
| 2975 | if (i == 0) |
| 2976 | { |
| 2977 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INTERNAL_ERROR); |
| 2978 | return 0; |
| 2979 | } |
| 2980 | s->rwstate=SSL_NOTHING; |
| 2981 | } |
| 2982 | if (ssl3_check_client_certificate(s)) |
| 2983 | s->state=SSL3_ST_CW_CERT_C; |
| 2984 | else |
| 2985 | s->state=SSL3_ST_CW_CERT_B; |
| 2986 | } |
| 2987 | |
| 2988 | /* We need to get a client cert */ |
| 2989 | if (s->state == SSL3_ST_CW_CERT_B) |
| 2990 | { |
| 2991 | /* If we get an error, we need to |
| 2992 | * ssl->rwstate=SSL_X509_LOOKUP; return(-1); |
| 2993 | * We then get retied later */ |
| 2994 | i=0; |
| 2995 | i = ssl_do_client_cert_cb(s, &x509, &pkey); |
| 2996 | if (i < 0) |
| 2997 | { |
| 2998 | s->rwstate=SSL_X509_LOOKUP; |
| 2999 | return(-1); |
| 3000 | } |
| 3001 | s->rwstate=SSL_NOTHING; |
| 3002 | if ((i == 1) && (pkey != NULL) && (x509 != NULL)) |
| 3003 | { |
| 3004 | s->state=SSL3_ST_CW_CERT_B; |
| 3005 | if ( !SSL_use_certificate(s,x509) || |
| 3006 | !SSL_use_PrivateKey(s,pkey)) |
| 3007 | i=0; |
| 3008 | } |
| 3009 | else if (i == 1) |
| 3010 | { |
| 3011 | i=0; |
| 3012 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); |
| 3013 | } |
| 3014 | |
| 3015 | if (x509 != NULL) X509_free(x509); |
| 3016 | if (pkey != NULL) EVP_PKEY_free(pkey); |
| 3017 | if (i && !ssl3_check_client_certificate(s)) |
| 3018 | i = 0; |
| 3019 | if (i == 0) |
| 3020 | { |
| 3021 | if (s->version == SSL3_VERSION) |
| 3022 | { |
| 3023 | s->s3->tmp.cert_req=0; |
| 3024 | ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE); |
| 3025 | return(1); |
| 3026 | } |
| 3027 | else |
| 3028 | { |
| 3029 | s->s3->tmp.cert_req=2; |
| 3030 | } |
| 3031 | } |
| 3032 | |
| 3033 | /* Ok, we have a cert */ |
| 3034 | s->state=SSL3_ST_CW_CERT_C; |
| 3035 | } |
| 3036 | |
| 3037 | if (s->state == SSL3_ST_CW_CERT_C) |
| 3038 | { |
| 3039 | s->state=SSL3_ST_CW_CERT_D; |
| 3040 | ssl3_output_cert_chain(s, |
| 3041 | (s->s3->tmp.cert_req == 2)?NULL:s->cert->key); |
| 3042 | } |
| 3043 | /* SSL3_ST_CW_CERT_D */ |
| 3044 | return ssl_do_write(s); |
| 3045 | } |
| 3046 | |
| 3047 | #define has_bits(i,m) (((i)&(m)) == (m)) |
| 3048 | |
| 3049 | int ssl3_check_cert_and_algorithm(SSL *s) |
| 3050 | { |
| 3051 | int i,idx; |
| 3052 | long alg_k,alg_a; |
| 3053 | EVP_PKEY *pkey=NULL; |
| 3054 | SESS_CERT *sc; |
| 3055 | #ifndef OPENSSL_NO_RSA |
| 3056 | RSA *rsa; |
| 3057 | #endif |
| 3058 | #ifndef OPENSSL_NO_DH |
| 3059 | DH *dh; |
| 3060 | #endif |
| 3061 | |
| 3062 | alg_k=s->s3->tmp.new_cipher->algorithm_mkey; |
| 3063 | alg_a=s->s3->tmp.new_cipher->algorithm_auth; |
| 3064 | |
| 3065 | /* we don't have a certificate */ |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3066 | if ((alg_a & (SSL_aNULL|SSL_aKRB5)) || ((alg_a & SSL_aPSK) && !(alg_k & SSL_kRSA))) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3067 | return(1); |
| 3068 | |
| 3069 | sc=s->session->sess_cert; |
| 3070 | if (sc == NULL) |
| 3071 | { |
| 3072 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR); |
| 3073 | goto err; |
| 3074 | } |
| 3075 | |
| 3076 | #ifndef OPENSSL_NO_RSA |
| 3077 | rsa=s->session->sess_cert->peer_rsa_tmp; |
| 3078 | #endif |
| 3079 | #ifndef OPENSSL_NO_DH |
| 3080 | dh=s->session->sess_cert->peer_dh_tmp; |
| 3081 | #endif |
| 3082 | |
| 3083 | /* This is the passed certificate */ |
| 3084 | |
| 3085 | idx=sc->peer_cert_type; |
| 3086 | #ifndef OPENSSL_NO_ECDH |
| 3087 | if (idx == SSL_PKEY_ECC) |
| 3088 | { |
| 3089 | if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, |
| 3090 | s) == 0) |
| 3091 | { /* check failed */ |
| 3092 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_ECC_CERT); |
| 3093 | goto f_err; |
| 3094 | } |
| 3095 | else |
| 3096 | { |
| 3097 | return 1; |
| 3098 | } |
| 3099 | } |
| 3100 | else if (alg_a & SSL_aECDSA) |
| 3101 | { |
| 3102 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_ECDSA_SIGNING_CERT); |
| 3103 | goto f_err; |
| 3104 | } |
| 3105 | else if (alg_k & (SSL_kECDHr|SSL_kECDHe)) |
| 3106 | { |
| 3107 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_ECDH_CERT); |
| 3108 | goto f_err; |
| 3109 | } |
| 3110 | #endif |
| 3111 | pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509); |
| 3112 | i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey); |
| 3113 | EVP_PKEY_free(pkey); |
| 3114 | |
| 3115 | |
| 3116 | /* Check that we have a certificate if we require one */ |
| 3117 | if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN)) |
| 3118 | { |
| 3119 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_RSA_SIGNING_CERT); |
| 3120 | goto f_err; |
| 3121 | } |
| 3122 | #ifndef OPENSSL_NO_DSA |
| 3123 | else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN)) |
| 3124 | { |
| 3125 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DSA_SIGNING_CERT); |
| 3126 | goto f_err; |
| 3127 | } |
| 3128 | #endif |
| 3129 | #ifndef OPENSSL_NO_RSA |
| 3130 | if ((alg_k & SSL_kRSA) && |
| 3131 | !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL))) |
| 3132 | { |
| 3133 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_RSA_ENCRYPTING_CERT); |
| 3134 | goto f_err; |
| 3135 | } |
| 3136 | #endif |
| 3137 | #ifndef OPENSSL_NO_DH |
| 3138 | if ((alg_k & SSL_kEDH) && |
| 3139 | !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL))) |
| 3140 | { |
| 3141 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_KEY); |
| 3142 | goto f_err; |
| 3143 | } |
| 3144 | else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) && |
| 3145 | !has_bits(i,EVP_PK_DH|EVP_PKS_RSA)) |
| 3146 | { |
| 3147 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_RSA_CERT); |
| 3148 | goto f_err; |
| 3149 | } |
| 3150 | #ifndef OPENSSL_NO_DSA |
| 3151 | else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) && |
| 3152 | !has_bits(i,EVP_PK_DH|EVP_PKS_DSA)) |
| 3153 | { |
| 3154 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_DSA_CERT); |
| 3155 | goto f_err; |
| 3156 | } |
| 3157 | #endif |
| 3158 | #endif |
| 3159 | |
| 3160 | if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP)) |
| 3161 | { |
| 3162 | #ifndef OPENSSL_NO_RSA |
| 3163 | if (alg_k & SSL_kRSA) |
| 3164 | { |
| 3165 | if (rsa == NULL |
| 3166 | || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) |
| 3167 | { |
| 3168 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_EXPORT_TMP_RSA_KEY); |
| 3169 | goto f_err; |
| 3170 | } |
| 3171 | } |
| 3172 | else |
| 3173 | #endif |
| 3174 | #ifndef OPENSSL_NO_DH |
| 3175 | if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) |
| 3176 | { |
| 3177 | if (dh == NULL |
| 3178 | || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) |
| 3179 | { |
| 3180 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_EXPORT_TMP_DH_KEY); |
| 3181 | goto f_err; |
| 3182 | } |
| 3183 | } |
| 3184 | else |
| 3185 | #endif |
| 3186 | { |
| 3187 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); |
| 3188 | goto f_err; |
| 3189 | } |
| 3190 | } |
| 3191 | return(1); |
| 3192 | f_err: |
| 3193 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); |
| 3194 | err: |
| 3195 | return(0); |
| 3196 | } |
| 3197 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3198 | #if !defined(OPENSSL_NO_TLSEXT) |
| 3199 | # if !defined(OPENSSL_NO_NEXTPROTONEG) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3200 | int ssl3_send_next_proto(SSL *s) |
| 3201 | { |
| 3202 | unsigned int len, padding_len; |
| 3203 | unsigned char *d; |
| 3204 | |
| 3205 | if (s->state == SSL3_ST_CW_NEXT_PROTO_A) |
| 3206 | { |
| 3207 | len = s->next_proto_negotiated_len; |
| 3208 | padding_len = 32 - ((len + 2) % 32); |
| 3209 | d = (unsigned char *)s->init_buf->data; |
| 3210 | d[4] = len; |
| 3211 | memcpy(d + 5, s->next_proto_negotiated, len); |
| 3212 | d[5 + len] = padding_len; |
| 3213 | memset(d + 6 + len, 0, padding_len); |
| 3214 | *(d++)=SSL3_MT_NEXT_PROTO; |
| 3215 | l2n3(2 + len + padding_len, d); |
| 3216 | s->state = SSL3_ST_CW_NEXT_PROTO_B; |
| 3217 | s->init_num = 4 + 2 + len + padding_len; |
| 3218 | s->init_off = 0; |
| 3219 | } |
| 3220 | |
| 3221 | return ssl3_do_write(s, SSL3_RT_HANDSHAKE); |
| 3222 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3223 | |
| 3224 | # endif /* !OPENSSL_NO_NEXTPROTONEG */ |
| 3225 | |
| 3226 | int ssl3_send_channel_id(SSL *s) |
| 3227 | { |
| 3228 | unsigned char *d; |
| 3229 | int ret = -1, public_key_len; |
| 3230 | EVP_MD_CTX md_ctx; |
| 3231 | size_t sig_len; |
| 3232 | ECDSA_SIG *sig = NULL; |
| 3233 | unsigned char *public_key = NULL, *derp, *der_sig = NULL; |
| 3234 | |
| 3235 | if (s->state != SSL3_ST_CW_CHANNEL_ID_A) |
| 3236 | return ssl3_do_write(s, SSL3_RT_HANDSHAKE); |
| 3237 | |
| 3238 | if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) |
| 3239 | { |
| 3240 | EVP_PKEY *key = NULL; |
| 3241 | s->ctx->channel_id_cb(s, &key); |
| 3242 | if (key != NULL) |
| 3243 | { |
| 3244 | s->tlsext_channel_id_private = key; |
| 3245 | } |
| 3246 | } |
| 3247 | if (!s->tlsext_channel_id_private) |
| 3248 | { |
| 3249 | s->rwstate=SSL_CHANNEL_ID_LOOKUP; |
| 3250 | return (-1); |
| 3251 | } |
| 3252 | s->rwstate=SSL_NOTHING; |
| 3253 | |
| 3254 | d = (unsigned char *)s->init_buf->data; |
| 3255 | *(d++)=SSL3_MT_ENCRYPTED_EXTENSIONS; |
| 3256 | l2n3(2 + 2 + TLSEXT_CHANNEL_ID_SIZE, d); |
| 3257 | if (s->s3->tlsext_channel_id_new) |
| 3258 | s2n(TLSEXT_TYPE_channel_id_new, d); |
| 3259 | else |
| 3260 | s2n(TLSEXT_TYPE_channel_id, d); |
| 3261 | s2n(TLSEXT_CHANNEL_ID_SIZE, d); |
| 3262 | |
| 3263 | EVP_MD_CTX_init(&md_ctx); |
| 3264 | |
| 3265 | public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL); |
| 3266 | if (public_key_len <= 0) |
| 3267 | { |
| 3268 | OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY); |
| 3269 | goto err; |
| 3270 | } |
| 3271 | /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a |
| 3272 | * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y |
| 3273 | * field elements as 32-byte, big-endian numbers. */ |
| 3274 | if (public_key_len != 65) |
| 3275 | { |
| 3276 | OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256); |
| 3277 | goto err; |
| 3278 | } |
| 3279 | public_key = OPENSSL_malloc(public_key_len); |
| 3280 | if (!public_key) |
| 3281 | { |
| 3282 | OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE); |
| 3283 | goto err; |
| 3284 | } |
| 3285 | |
| 3286 | derp = public_key; |
| 3287 | i2d_PublicKey(s->tlsext_channel_id_private, &derp); |
| 3288 | |
| 3289 | if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL, |
| 3290 | s->tlsext_channel_id_private) != 1) |
| 3291 | { |
| 3292 | OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNINIT_FAILED); |
| 3293 | goto err; |
| 3294 | } |
| 3295 | |
| 3296 | if (!tls1_channel_id_hash(&md_ctx, s)) |
| 3297 | goto err; |
| 3298 | |
| 3299 | if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) |
| 3300 | { |
| 3301 | OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED); |
| 3302 | goto err; |
| 3303 | } |
| 3304 | |
| 3305 | der_sig = OPENSSL_malloc(sig_len); |
| 3306 | if (!der_sig) |
| 3307 | { |
| 3308 | OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE); |
| 3309 | goto err; |
| 3310 | } |
| 3311 | |
| 3312 | if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) |
| 3313 | { |
| 3314 | OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED); |
| 3315 | goto err; |
| 3316 | } |
| 3317 | |
| 3318 | derp = der_sig; |
| 3319 | sig = d2i_ECDSA_SIG(NULL, (const unsigned char**) &derp, sig_len); |
| 3320 | if (sig == NULL) |
| 3321 | { |
| 3322 | OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG); |
| 3323 | goto err; |
| 3324 | } |
| 3325 | |
| 3326 | /* The first byte of public_key will be 0x4, denoting an uncompressed key. */ |
| 3327 | memcpy(d, public_key + 1, 64); |
| 3328 | d += 64; |
| 3329 | memset(d, 0, 2 * 32); |
| 3330 | BN_bn2bin(sig->r, d + 32 - BN_num_bytes(sig->r)); |
| 3331 | d += 32; |
| 3332 | BN_bn2bin(sig->s, d + 32 - BN_num_bytes(sig->s)); |
| 3333 | d += 32; |
| 3334 | |
| 3335 | s->state = SSL3_ST_CW_CHANNEL_ID_B; |
| 3336 | s->init_num = 4 + 2 + 2 + TLSEXT_CHANNEL_ID_SIZE; |
| 3337 | s->init_off = 0; |
| 3338 | |
| 3339 | ret = ssl3_do_write(s, SSL3_RT_HANDSHAKE); |
| 3340 | |
| 3341 | err: |
| 3342 | EVP_MD_CTX_cleanup(&md_ctx); |
| 3343 | if (public_key) |
| 3344 | OPENSSL_free(public_key); |
| 3345 | if (der_sig) |
| 3346 | OPENSSL_free(der_sig); |
| 3347 | if (sig) |
| 3348 | ECDSA_SIG_free(sig); |
| 3349 | |
| 3350 | return ret; |
| 3351 | } |
| 3352 | #endif /* !OPENSSL_NO_TLSEXT */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3353 | |
| 3354 | /* Check to see if handshake is full or resumed. Usually this is just a |
| 3355 | * case of checking to see if a cache hit has occurred. In the case of |
| 3356 | * session tickets we have to check the next message to be sure. |
| 3357 | */ |
| 3358 | |
| 3359 | #ifndef OPENSSL_NO_TLSEXT |
| 3360 | int ssl3_check_finished(SSL *s) |
| 3361 | { |
| 3362 | int ok; |
| 3363 | long n; |
| 3364 | /* If we have no ticket it cannot be a resumed session. */ |
| 3365 | if (!s->session->tlsext_tick) |
| 3366 | return 1; |
| 3367 | /* this function is called when we really expect a Certificate |
| 3368 | * message, so permit appropriate message length */ |
| 3369 | n=s->method->ssl_get_message(s, |
| 3370 | SSL3_ST_CR_CERT_A, |
| 3371 | SSL3_ST_CR_CERT_B, |
| 3372 | -1, |
| 3373 | s->max_cert_list, |
| 3374 | &ok); |
| 3375 | if (!ok) return((int)n); |
| 3376 | s->s3->tmp.reuse_message = 1; |
| 3377 | if ((s->s3->tmp.message_type == SSL3_MT_FINISHED) |
| 3378 | || (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET)) |
| 3379 | return 2; |
| 3380 | |
| 3381 | return 1; |
| 3382 | } |
| 3383 | #endif |
| 3384 | |
| 3385 | int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) |
| 3386 | { |
| 3387 | int i = 0; |
| 3388 | /* TODO(fork): remove */ |
| 3389 | #if 0 |
| 3390 | #ifndef OPENSSL_NO_ENGINE |
| 3391 | if (s->ctx->client_cert_engine) |
| 3392 | { |
| 3393 | i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s, |
| 3394 | SSL_get_client_CA_list(s), |
| 3395 | px509, ppkey, NULL, NULL, NULL); |
| 3396 | if (i != 0) |
| 3397 | return i; |
| 3398 | } |
| 3399 | #endif |
| 3400 | #endif |
| 3401 | if (s->ctx->client_cert_cb) |
| 3402 | i = s->ctx->client_cert_cb(s,px509,ppkey); |
| 3403 | return i; |
| 3404 | } |
| 3405 | |
| 3406 | #ifndef OPENSSL_NO_TLSEXT |
| 3407 | int tls1_get_server_supplemental_data(SSL *s) |
| 3408 | { |
| 3409 | int al; |
| 3410 | int ok; |
| 3411 | unsigned long supp_data_len, authz_data_len; |
| 3412 | long n; |
| 3413 | unsigned short supp_data_type, authz_data_type, proof_len; |
| 3414 | const unsigned char *p; |
| 3415 | unsigned char *new_proof; |
| 3416 | |
| 3417 | n=s->method->ssl_get_message(s, |
| 3418 | SSL3_ST_CR_SUPPLEMENTAL_DATA_A, |
| 3419 | SSL3_ST_CR_SUPPLEMENTAL_DATA_B, |
| 3420 | SSL3_MT_SUPPLEMENTAL_DATA, |
| 3421 | /* use default limit */ |
| 3422 | TLSEXT_MAXLEN_supplemental_data, |
| 3423 | &ok); |
| 3424 | |
| 3425 | if (!ok) return((int)n); |
| 3426 | |
| 3427 | p = (unsigned char *)s->init_msg; |
| 3428 | |
| 3429 | /* The message cannot be empty */ |
| 3430 | if (n < 3) |
| 3431 | { |
| 3432 | al = SSL_AD_DECODE_ERROR; |
| 3433 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_LENGTH_MISMATCH); |
| 3434 | goto f_err; |
| 3435 | } |
| 3436 | /* Length of supplemental data */ |
| 3437 | n2l3(p,supp_data_len); |
| 3438 | n -= 3; |
| 3439 | /* We must have at least one supplemental data entry |
| 3440 | * with type (1 byte) and length (2 bytes). */ |
| 3441 | if (supp_data_len != (unsigned long) n || n < 4) |
| 3442 | { |
| 3443 | al = SSL_AD_DECODE_ERROR; |
| 3444 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_LENGTH_MISMATCH); |
| 3445 | goto f_err; |
| 3446 | } |
| 3447 | /* Supplemental data type: must be authz_data */ |
| 3448 | n2s(p,supp_data_type); |
| 3449 | n -= 2; |
| 3450 | if (supp_data_type != TLSEXT_SUPPLEMENTALDATATYPE_authz_data) |
| 3451 | { |
| 3452 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 3453 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNKNOWN_SUPPLEMENTAL_DATA_TYPE); |
| 3454 | goto f_err; |
| 3455 | } |
| 3456 | /* Authz data length */ |
| 3457 | n2s(p, authz_data_len); |
| 3458 | n -= 2; |
| 3459 | if (authz_data_len != (unsigned long) n || n < 1) |
| 3460 | { |
| 3461 | al = SSL_AD_DECODE_ERROR; |
| 3462 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_LENGTH_MISMATCH); |
| 3463 | goto f_err; |
| 3464 | } |
| 3465 | /* Authz data type: must be audit_proof */ |
| 3466 | authz_data_type = *(p++); |
| 3467 | n -= 1; |
| 3468 | if (authz_data_type != TLSEXT_AUTHZDATAFORMAT_audit_proof) |
| 3469 | { |
| 3470 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 3471 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNKNOWN_AUTHZ_DATA_TYPE); |
| 3472 | goto f_err; |
| 3473 | } |
| 3474 | /* We have a proof: read its length */ |
| 3475 | if (n < 2) |
| 3476 | { |
| 3477 | al = SSL_AD_DECODE_ERROR; |
| 3478 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_LENGTH_MISMATCH); |
| 3479 | goto f_err; |
| 3480 | } |
| 3481 | n2s(p, proof_len); |
| 3482 | n -= 2; |
| 3483 | if (proof_len != (unsigned long) n) |
| 3484 | { |
| 3485 | al = SSL_AD_DECODE_ERROR; |
| 3486 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_LENGTH_MISMATCH); |
| 3487 | goto f_err; |
| 3488 | } |
| 3489 | /* Store the proof */ |
| 3490 | new_proof = OPENSSL_realloc(s->session->audit_proof, |
| 3491 | proof_len); |
| 3492 | if (new_proof == NULL) |
| 3493 | { |
| 3494 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 3495 | return 0; |
| 3496 | } |
| 3497 | s->session->audit_proof_length = proof_len; |
| 3498 | s->session->audit_proof = new_proof; |
| 3499 | memcpy(s->session->audit_proof, p, proof_len); |
| 3500 | |
| 3501 | /* Got the proof, but can't verify it yet. */ |
| 3502 | return 1; |
| 3503 | f_err: |
| 3504 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 3505 | return -1; |
| 3506 | } |
| 3507 | #endif |