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