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