Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). |
| 108 | * |
| 109 | */ |
| 110 | /* ==================================================================== |
| 111 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 112 | * |
| 113 | * Portions of the attached software ("Contribution") are developed by |
| 114 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
| 115 | * |
| 116 | * The Contribution is licensed pursuant to the OpenSSL open source |
| 117 | * license provided above. |
| 118 | * |
| 119 | * ECC cipher suite support in OpenSSL originally written by |
| 120 | * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. |
| 121 | * |
| 122 | */ |
| 123 | /* ==================================================================== |
| 124 | * Copyright 2005 Nokia. All rights reserved. |
| 125 | * |
| 126 | * The portions of the attached software ("Contribution") is developed by |
| 127 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 128 | * license. |
| 129 | * |
| 130 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 131 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 132 | * support (see RFC 4279) to OpenSSL. |
| 133 | * |
| 134 | * No patent licenses or other rights except those expressly stated in |
| 135 | * the OpenSSL open source license shall be deemed granted or received |
| 136 | * expressly, by implication, estoppel, or otherwise. |
| 137 | * |
| 138 | * No assurances are provided by Nokia that the Contribution does not |
| 139 | * infringe the patent or other intellectual property rights of any third |
| 140 | * party or that the license provides you with all the necessary rights |
| 141 | * to make use of the Contribution. |
| 142 | * |
| 143 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 144 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 145 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 146 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 147 | * OTHERWISE. */ |
| 148 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 149 | #define NETSCAPE_HANG_BUG |
| 150 | |
David Benjamin | 880b14e | 2014-08-25 22:35:07 -0400 | [diff] [blame] | 151 | #include <assert.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 152 | #include <stdio.h> |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 153 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 154 | |
| 155 | #include <openssl/bn.h> |
| 156 | #include <openssl/buf.h> |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 157 | #include <openssl/bytestring.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 158 | #include <openssl/cipher.h> |
| 159 | #include <openssl/dh.h> |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 160 | #include <openssl/ec.h> |
| 161 | #include <openssl/ecdsa.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 162 | #include <openssl/evp.h> |
| 163 | #include <openssl/hmac.h> |
| 164 | #include <openssl/md5.h> |
| 165 | #include <openssl/mem.h> |
| 166 | #include <openssl/obj.h> |
| 167 | #include <openssl/rand.h> |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 168 | #include <openssl/sha.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 169 | #include <openssl/x509.h> |
| 170 | |
| 171 | #include "ssl_locl.h" |
| 172 | #include "../crypto/dh/internal.h" |
| 173 | |
| 174 | static const SSL_METHOD *ssl3_get_server_method(int ver); |
| 175 | |
| 176 | static const SSL_METHOD *ssl3_get_server_method(int ver) |
| 177 | { |
| 178 | if (ver == SSL3_VERSION) |
| 179 | return(SSLv3_server_method()); |
| 180 | else |
| 181 | return(NULL); |
| 182 | } |
| 183 | |
| 184 | IMPLEMENT_ssl3_meth_func(SSLv3_server_method, |
| 185 | ssl3_accept, |
| 186 | ssl_undefined_function, |
| 187 | ssl3_get_server_method) |
| 188 | |
| 189 | int ssl3_accept(SSL *s) |
| 190 | { |
| 191 | BUF_MEM *buf; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 192 | unsigned long alg_a; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 193 | void (*cb)(const SSL *ssl,int type,int val)=NULL; |
| 194 | int ret= -1; |
| 195 | int new_state,state,skip=0; |
| 196 | |
| 197 | ERR_clear_error(); |
| 198 | ERR_clear_system_error(); |
| 199 | |
| 200 | if (s->info_callback != NULL) |
| 201 | cb=s->info_callback; |
| 202 | else if (s->ctx->info_callback != NULL) |
| 203 | cb=s->ctx->info_callback; |
| 204 | |
| 205 | /* init things to blank */ |
| 206 | s->in_handshake++; |
| 207 | if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); |
| 208 | |
| 209 | if (s->cert == NULL) |
| 210 | { |
| 211 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_NO_CERTIFICATE_SET); |
| 212 | return(-1); |
| 213 | } |
| 214 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 215 | for (;;) |
| 216 | { |
| 217 | state=s->state; |
| 218 | |
| 219 | switch (s->state) |
| 220 | { |
| 221 | case SSL_ST_RENEGOTIATE: |
| 222 | s->renegotiate=1; |
| 223 | /* s->state=SSL_ST_ACCEPT; */ |
| 224 | |
| 225 | case SSL_ST_BEFORE: |
| 226 | case SSL_ST_ACCEPT: |
| 227 | case SSL_ST_BEFORE|SSL_ST_ACCEPT: |
| 228 | case SSL_ST_OK|SSL_ST_ACCEPT: |
| 229 | |
| 230 | s->server=1; |
| 231 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); |
| 232 | |
| 233 | if ((s->version>>8) != 3) |
| 234 | { |
| 235 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR); |
| 236 | return -1; |
| 237 | } |
| 238 | s->type=SSL_ST_ACCEPT; |
| 239 | |
| 240 | if (s->init_buf == NULL) |
| 241 | { |
| 242 | if ((buf=BUF_MEM_new()) == NULL) |
| 243 | { |
| 244 | ret= -1; |
| 245 | goto end; |
| 246 | } |
| 247 | if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) |
| 248 | { |
| 249 | ret= -1; |
| 250 | goto end; |
| 251 | } |
| 252 | s->init_buf=buf; |
| 253 | } |
| 254 | |
| 255 | if (!ssl3_setup_buffers(s)) |
| 256 | { |
| 257 | ret= -1; |
| 258 | goto end; |
| 259 | } |
| 260 | |
| 261 | s->init_num=0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 262 | |
| 263 | if (s->state != SSL_ST_RENEGOTIATE) |
| 264 | { |
| 265 | /* Ok, we now need to push on a buffering BIO so that |
| 266 | * the output is sent in a way that TCP likes :-) |
| 267 | */ |
| 268 | if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; } |
| 269 | |
| 270 | ssl3_init_finished_mac(s); |
| 271 | s->state=SSL3_ST_SR_CLNT_HELLO_A; |
| 272 | s->ctx->stats.sess_accept++; |
| 273 | } |
| 274 | else if (!s->s3->send_connection_binding && |
| 275 | !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) |
| 276 | { |
| 277 | /* Server attempting to renegotiate with |
| 278 | * client that doesn't support secure |
| 279 | * renegotiation. |
| 280 | */ |
| 281 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); |
| 282 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); |
| 283 | ret = -1; |
| 284 | goto end; |
| 285 | } |
| 286 | else |
| 287 | { |
| 288 | /* s->state == SSL_ST_RENEGOTIATE, |
| 289 | * we will just send a HelloRequest */ |
| 290 | s->ctx->stats.sess_accept_renegotiate++; |
| 291 | s->state=SSL3_ST_SW_HELLO_REQ_A; |
| 292 | } |
| 293 | break; |
| 294 | |
| 295 | case SSL3_ST_SW_HELLO_REQ_A: |
| 296 | case SSL3_ST_SW_HELLO_REQ_B: |
| 297 | |
| 298 | s->shutdown=0; |
| 299 | ret=ssl3_send_hello_request(s); |
| 300 | if (ret <= 0) goto end; |
| 301 | s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C; |
| 302 | s->state=SSL3_ST_SW_FLUSH; |
| 303 | s->init_num=0; |
| 304 | |
| 305 | ssl3_init_finished_mac(s); |
| 306 | break; |
| 307 | |
| 308 | case SSL3_ST_SW_HELLO_REQ_C: |
| 309 | s->state=SSL_ST_OK; |
| 310 | break; |
| 311 | |
| 312 | case SSL3_ST_SR_CLNT_HELLO_A: |
| 313 | case SSL3_ST_SR_CLNT_HELLO_B: |
| 314 | case SSL3_ST_SR_CLNT_HELLO_C: |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 315 | case SSL3_ST_SR_CLNT_HELLO_D: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 316 | s->shutdown=0; |
| 317 | ret=ssl3_get_client_hello(s); |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 318 | if (ret == PENDING_SESSION) { |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 319 | s->rwstate = SSL_PENDING_SESSION; |
| 320 | goto end; |
| 321 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 322 | if (ret == CERTIFICATE_SELECTION_PENDING) |
| 323 | { |
| 324 | s->rwstate = SSL_CERTIFICATE_SELECTION_PENDING; |
| 325 | goto end; |
| 326 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 327 | if (ret <= 0) goto end; |
| 328 | s->renegotiate = 2; |
| 329 | s->state=SSL3_ST_SW_SRVR_HELLO_A; |
| 330 | s->init_num=0; |
| 331 | break; |
| 332 | |
| 333 | case SSL3_ST_SW_SRVR_HELLO_A: |
| 334 | case SSL3_ST_SW_SRVR_HELLO_B: |
| 335 | ret=ssl3_send_server_hello(s); |
| 336 | if (ret <= 0) goto end; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 337 | if (s->hit) |
| 338 | { |
| 339 | if (s->tlsext_ticket_expected) |
| 340 | s->state=SSL3_ST_SW_SESSION_TICKET_A; |
| 341 | else |
| 342 | s->state=SSL3_ST_SW_CHANGE_A; |
| 343 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 344 | else |
David Benjamin | 2b0aeec | 2014-07-01 00:39:02 -0400 | [diff] [blame] | 345 | s->state = SSL3_ST_SW_CERT_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 346 | s->init_num = 0; |
| 347 | break; |
| 348 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 349 | case SSL3_ST_SW_CERT_A: |
| 350 | case SSL3_ST_SW_CERT_B: |
David Benjamin | e8f3d66 | 2014-07-12 01:10:19 -0400 | [diff] [blame] | 351 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 352 | { |
| 353 | ret=ssl3_send_server_certificate(s); |
| 354 | if (ret <= 0) goto end; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 355 | if (s->tlsext_status_expected) |
| 356 | s->state=SSL3_ST_SW_CERT_STATUS_A; |
| 357 | else |
| 358 | s->state=SSL3_ST_SW_KEY_EXCH_A; |
| 359 | } |
| 360 | else |
| 361 | { |
| 362 | skip = 1; |
| 363 | s->state=SSL3_ST_SW_KEY_EXCH_A; |
| 364 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 365 | s->init_num=0; |
| 366 | break; |
| 367 | |
| 368 | case SSL3_ST_SW_KEY_EXCH_A: |
| 369 | case SSL3_ST_SW_KEY_EXCH_B: |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 370 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 371 | |
David Benjamin | b9cc33a | 2014-07-15 00:09:48 -0400 | [diff] [blame] | 372 | /* Send a ServerKeyExchange message if: |
| 373 | * - The key exchange is ephemeral or anonymous |
| 374 | * Diffie-Hellman. |
| 375 | * - There is a PSK identity hint. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 376 | * |
David Benjamin | b9cc33a | 2014-07-15 00:09:48 -0400 | [diff] [blame] | 377 | * TODO(davidben): This logic is currently duplicated |
| 378 | * in d1_srvr.c. Fix this. In the meantime, keep them |
| 379 | * in sync. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 380 | */ |
David Benjamin | b9cc33a | 2014-07-15 00:09:48 -0400 | [diff] [blame] | 381 | if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) || |
David Benjamin | 77a942b | 2014-07-15 01:22:50 -0400 | [diff] [blame] | 382 | ((alg_a & SSL_aPSK) && s->session->psk_identity_hint)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 383 | { |
| 384 | ret=ssl3_send_server_key_exchange(s); |
| 385 | if (ret <= 0) goto end; |
| 386 | } |
| 387 | else |
| 388 | skip=1; |
| 389 | |
| 390 | s->state=SSL3_ST_SW_CERT_REQ_A; |
| 391 | s->init_num=0; |
| 392 | break; |
| 393 | |
| 394 | case SSL3_ST_SW_CERT_REQ_A: |
| 395 | case SSL3_ST_SW_CERT_REQ_B: |
| 396 | if (/* don't request cert unless asked for it: */ |
| 397 | !(s->verify_mode & SSL_VERIFY_PEER) || |
Adam Langley | 509e5ed | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 398 | /* Don't request a certificate if an obc was presented */ |
| 399 | ((s->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) && |
| 400 | s->s3->tlsext_channel_id_valid) || |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 401 | /* if SSL_VERIFY_CLIENT_ONCE is set, |
| 402 | * don't request cert during re-negotiation: */ |
| 403 | ((s->session->peer != NULL) && |
| 404 | (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || |
| 405 | /* never request cert in anonymous ciphersuites |
| 406 | * (see section "Certificate request" in SSL 3 drafts |
| 407 | * and in RFC 2246): */ |
| 408 | ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) && |
| 409 | /* ... except when the application insists on verification |
| 410 | * (against the specs, but s3_clnt.c accepts this for SSL 3) */ |
| 411 | !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) || |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 412 | /* With normal PSK Certificates and |
| 413 | * Certificate Requests are omitted */ |
David Benjamin | d26aea6 | 2014-07-12 00:13:56 -0400 | [diff] [blame] | 414 | (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 415 | { |
| 416 | /* no cert request */ |
| 417 | skip=1; |
| 418 | s->s3->tmp.cert_request=0; |
| 419 | s->state=SSL3_ST_SW_SRVR_DONE_A; |
| 420 | if (s->s3->handshake_buffer) |
| 421 | if (!ssl3_digest_cached_records(s)) |
| 422 | return -1; |
| 423 | } |
| 424 | else |
| 425 | { |
| 426 | s->s3->tmp.cert_request=1; |
| 427 | ret=ssl3_send_certificate_request(s); |
| 428 | if (ret <= 0) goto end; |
| 429 | #ifndef NETSCAPE_HANG_BUG |
| 430 | s->state=SSL3_ST_SW_SRVR_DONE_A; |
| 431 | #else |
| 432 | s->state=SSL3_ST_SW_FLUSH; |
| 433 | s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; |
| 434 | #endif |
| 435 | s->init_num=0; |
| 436 | } |
| 437 | break; |
| 438 | |
| 439 | case SSL3_ST_SW_SRVR_DONE_A: |
| 440 | case SSL3_ST_SW_SRVR_DONE_B: |
| 441 | ret=ssl3_send_server_done(s); |
| 442 | if (ret <= 0) goto end; |
| 443 | s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; |
| 444 | s->state=SSL3_ST_SW_FLUSH; |
| 445 | s->init_num=0; |
| 446 | break; |
| 447 | |
| 448 | case SSL3_ST_SW_FLUSH: |
| 449 | |
| 450 | /* This code originally checked to see if |
| 451 | * any data was pending using BIO_CTRL_INFO |
| 452 | * and then flushed. This caused problems |
| 453 | * as documented in PR#1939. The proposed |
| 454 | * fix doesn't completely resolve this issue |
| 455 | * as buggy implementations of BIO_CTRL_PENDING |
| 456 | * still exist. So instead we just flush |
| 457 | * unconditionally. |
| 458 | */ |
| 459 | |
| 460 | s->rwstate=SSL_WRITING; |
| 461 | if (BIO_flush(s->wbio) <= 0) |
| 462 | { |
| 463 | ret= -1; |
| 464 | goto end; |
| 465 | } |
| 466 | s->rwstate=SSL_NOTHING; |
| 467 | |
| 468 | s->state=s->s3->tmp.next_state; |
| 469 | break; |
| 470 | |
| 471 | case SSL3_ST_SR_CERT_A: |
| 472 | case SSL3_ST_SR_CERT_B: |
David Benjamin | 92909a6 | 2014-08-20 11:40:03 -0400 | [diff] [blame] | 473 | if (s->s3->tmp.cert_request) |
| 474 | { |
| 475 | ret=ssl3_get_client_certificate(s); |
| 476 | if (ret <= 0) goto end; |
| 477 | } |
| 478 | s->init_num=0; |
| 479 | s->state=SSL3_ST_SR_KEY_EXCH_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 480 | break; |
| 481 | |
| 482 | case SSL3_ST_SR_KEY_EXCH_A: |
| 483 | case SSL3_ST_SR_KEY_EXCH_B: |
| 484 | ret=ssl3_get_client_key_exchange(s); |
| 485 | if (ret <= 0) |
| 486 | goto end; |
David Benjamin | b52e3dd | 2014-08-20 11:12:48 -0400 | [diff] [blame] | 487 | s->state=SSL3_ST_SR_CERT_VRFY_A; |
| 488 | s->init_num=0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 489 | break; |
| 490 | |
| 491 | case SSL3_ST_SR_CERT_VRFY_A: |
| 492 | case SSL3_ST_SR_CERT_VRFY_B: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 493 | ret=ssl3_get_cert_verify(s); |
| 494 | if (ret <= 0) goto end; |
| 495 | |
David Benjamin | 84ec49e | 2014-07-22 14:25:28 -0400 | [diff] [blame] | 496 | s->state = SSL3_ST_SR_CHANGE; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 497 | s->init_num=0; |
| 498 | break; |
| 499 | |
David Benjamin | 84ec49e | 2014-07-22 14:25:28 -0400 | [diff] [blame] | 500 | case SSL3_ST_SR_CHANGE: { |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 501 | char next_proto_neg = 0; |
| 502 | char channel_id = 0; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 503 | # if !defined(OPENSSL_NO_NEXTPROTONEG) |
| 504 | next_proto_neg = s->s3->next_proto_neg_seen; |
| 505 | # endif |
| 506 | channel_id = s->s3->tlsext_channel_id_valid; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 507 | |
David Benjamin | 84ec49e | 2014-07-22 14:25:28 -0400 | [diff] [blame] | 508 | /* At this point, the next message must be entirely |
| 509 | * behind a ChangeCipherSpec. */ |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 510 | if (!ssl3_expect_change_cipher_spec(s)) |
| 511 | { |
| 512 | ret = -1; |
| 513 | goto end; |
| 514 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 515 | if (next_proto_neg) |
David Benjamin | 84ec49e | 2014-07-22 14:25:28 -0400 | [diff] [blame] | 516 | s->state = SSL3_ST_SR_NEXT_PROTO_A; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 517 | else if (channel_id) |
David Benjamin | 84ec49e | 2014-07-22 14:25:28 -0400 | [diff] [blame] | 518 | s->state = SSL3_ST_SR_CHANNEL_ID_A; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 519 | else |
David Benjamin | 84ec49e | 2014-07-22 14:25:28 -0400 | [diff] [blame] | 520 | s->state = SSL3_ST_SR_FINISHED_A; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 521 | break; |
| 522 | } |
| 523 | |
David Benjamin | 6dbd73d | 2014-07-03 15:59:49 -0400 | [diff] [blame] | 524 | #if !defined(OPENSSL_NO_NEXTPROTONEG) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 525 | case SSL3_ST_SR_NEXT_PROTO_A: |
| 526 | case SSL3_ST_SR_NEXT_PROTO_B: |
| 527 | ret=ssl3_get_next_proto(s); |
| 528 | if (ret <= 0) goto end; |
| 529 | s->init_num = 0; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 530 | if (s->s3->tlsext_channel_id_valid) |
| 531 | s->state=SSL3_ST_SR_CHANNEL_ID_A; |
| 532 | else |
| 533 | s->state=SSL3_ST_SR_FINISHED_A; |
| 534 | break; |
| 535 | #endif |
| 536 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 537 | case SSL3_ST_SR_CHANNEL_ID_A: |
| 538 | case SSL3_ST_SR_CHANNEL_ID_B: |
| 539 | ret=ssl3_get_channel_id(s); |
| 540 | if (ret <= 0) goto end; |
| 541 | s->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 542 | s->state=SSL3_ST_SR_FINISHED_A; |
| 543 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 544 | |
| 545 | case SSL3_ST_SR_FINISHED_A: |
| 546 | case SSL3_ST_SR_FINISHED_B: |
| 547 | ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A, |
| 548 | SSL3_ST_SR_FINISHED_B); |
| 549 | if (ret <= 0) goto end; |
| 550 | if (s->hit) |
| 551 | s->state=SSL_ST_OK; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 552 | else if (s->tlsext_ticket_expected) |
| 553 | s->state=SSL3_ST_SW_SESSION_TICKET_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 554 | else |
| 555 | s->state=SSL3_ST_SW_CHANGE_A; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 556 | /* If this is a full handshake with ChannelID then |
| 557 | * record the hashshake hashes in |s->session| in case |
| 558 | * we need them to verify a ChannelID signature on a |
| 559 | * resumption of this session in the future. */ |
| 560 | if (!s->hit && s->s3->tlsext_channel_id_new) |
| 561 | { |
| 562 | ret = tls1_record_handshake_hashes_for_channel_id(s); |
| 563 | if (ret <= 0) goto end; |
| 564 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 565 | s->init_num=0; |
| 566 | break; |
| 567 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 568 | case SSL3_ST_SW_SESSION_TICKET_A: |
| 569 | case SSL3_ST_SW_SESSION_TICKET_B: |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 570 | ret=ssl3_send_new_session_ticket(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 571 | if (ret <= 0) goto end; |
| 572 | s->state=SSL3_ST_SW_CHANGE_A; |
| 573 | s->init_num=0; |
| 574 | break; |
| 575 | |
| 576 | case SSL3_ST_SW_CERT_STATUS_A: |
| 577 | case SSL3_ST_SW_CERT_STATUS_B: |
| 578 | ret=ssl3_send_cert_status(s); |
| 579 | if (ret <= 0) goto end; |
| 580 | s->state=SSL3_ST_SW_KEY_EXCH_A; |
| 581 | s->init_num=0; |
| 582 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 583 | |
| 584 | case SSL3_ST_SW_CHANGE_A: |
| 585 | case SSL3_ST_SW_CHANGE_B: |
| 586 | |
| 587 | s->session->cipher=s->s3->tmp.new_cipher; |
| 588 | if (!s->method->ssl3_enc->setup_key_block(s)) |
| 589 | { ret= -1; goto end; } |
| 590 | |
| 591 | ret=ssl3_send_change_cipher_spec(s, |
| 592 | SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B); |
| 593 | |
| 594 | if (ret <= 0) goto end; |
| 595 | s->state=SSL3_ST_SW_FINISHED_A; |
| 596 | s->init_num=0; |
| 597 | |
| 598 | if (!s->method->ssl3_enc->change_cipher_state(s, |
| 599 | SSL3_CHANGE_CIPHER_SERVER_WRITE)) |
| 600 | { |
| 601 | ret= -1; |
| 602 | goto end; |
| 603 | } |
| 604 | |
| 605 | break; |
| 606 | |
| 607 | case SSL3_ST_SW_FINISHED_A: |
| 608 | case SSL3_ST_SW_FINISHED_B: |
| 609 | ret=ssl3_send_finished(s, |
| 610 | SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B, |
| 611 | s->method->ssl3_enc->server_finished_label, |
| 612 | s->method->ssl3_enc->server_finished_label_len); |
| 613 | if (ret <= 0) goto end; |
David Benjamin | 84ec49e | 2014-07-22 14:25:28 -0400 | [diff] [blame] | 614 | s->state = SSL3_ST_SW_FLUSH; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 615 | if (s->hit) |
David Benjamin | 84ec49e | 2014-07-22 14:25:28 -0400 | [diff] [blame] | 616 | s->s3->tmp.next_state = SSL3_ST_SR_CHANGE; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 617 | else |
David Benjamin | 84ec49e | 2014-07-22 14:25:28 -0400 | [diff] [blame] | 618 | s->s3->tmp.next_state = SSL_ST_OK; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 619 | s->init_num=0; |
| 620 | break; |
| 621 | |
| 622 | case SSL_ST_OK: |
| 623 | /* clean a few things up */ |
| 624 | ssl3_cleanup_key_block(s); |
| 625 | |
| 626 | BUF_MEM_free(s->init_buf); |
| 627 | s->init_buf=NULL; |
| 628 | |
| 629 | /* remove buffering on output */ |
| 630 | ssl_free_wbio_buffer(s); |
| 631 | |
| 632 | s->init_num=0; |
| 633 | |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 634 | /* If we aren't retaining peer certificates then we can |
| 635 | * discard it now. */ |
| 636 | if (s->session->peer && s->ctx->retain_only_sha256_of_client_certs) |
| 637 | { |
| 638 | X509_free(s->session->peer); |
| 639 | s->session->peer = NULL; |
| 640 | } |
| 641 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 642 | if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */ |
| 643 | { |
| 644 | s->renegotiate=0; |
| 645 | s->new_session=0; |
| 646 | |
| 647 | ssl_update_cache(s,SSL_SESS_CACHE_SERVER); |
| 648 | |
| 649 | s->ctx->stats.sess_accept_good++; |
| 650 | /* s->server=1; */ |
| 651 | s->handshake_func=ssl3_accept; |
| 652 | |
| 653 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); |
| 654 | } |
| 655 | |
| 656 | ret = 1; |
| 657 | goto end; |
| 658 | /* break; */ |
| 659 | |
| 660 | default: |
| 661 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_UNKNOWN_STATE); |
| 662 | ret= -1; |
| 663 | goto end; |
| 664 | /* break; */ |
| 665 | } |
| 666 | |
| 667 | if (!s->s3->tmp.reuse_message && !skip) |
| 668 | { |
| 669 | if (s->debug) |
| 670 | { |
| 671 | if ((ret=BIO_flush(s->wbio)) <= 0) |
| 672 | goto end; |
| 673 | } |
| 674 | |
| 675 | |
| 676 | if ((cb != NULL) && (s->state != state)) |
| 677 | { |
| 678 | new_state=s->state; |
| 679 | s->state=state; |
| 680 | cb(s,SSL_CB_ACCEPT_LOOP,1); |
| 681 | s->state=new_state; |
| 682 | } |
| 683 | } |
| 684 | skip=0; |
| 685 | } |
| 686 | end: |
| 687 | /* BIO_flush(s->wbio); */ |
| 688 | |
| 689 | s->in_handshake--; |
| 690 | if (cb != NULL) |
| 691 | cb(s,SSL_CB_ACCEPT_EXIT,ret); |
| 692 | return(ret); |
| 693 | } |
| 694 | |
| 695 | int ssl3_send_hello_request(SSL *s) |
| 696 | { |
| 697 | |
| 698 | if (s->state == SSL3_ST_SW_HELLO_REQ_A) |
| 699 | { |
| 700 | ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0); |
| 701 | s->state=SSL3_ST_SW_HELLO_REQ_B; |
| 702 | } |
| 703 | |
| 704 | /* SSL3_ST_SW_HELLO_REQ_B */ |
| 705 | return ssl_do_write(s); |
| 706 | } |
| 707 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 708 | int ssl3_get_client_hello(SSL *s) |
| 709 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 710 | int i,ok,al=SSL_AD_INTERNAL_ERROR,ret= -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 711 | long n; |
David Benjamin | 6f26001 | 2014-08-15 13:49:12 -0400 | [diff] [blame] | 712 | const SSL_CIPHER *c; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 713 | STACK_OF(SSL_CIPHER) *ciphers=NULL; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 714 | struct ssl_early_callback_ctx early_ctx; |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 715 | CBS client_hello; |
| 716 | uint16_t client_version; |
| 717 | CBS client_random, session_id, cipher_suites, compression_methods; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 718 | |
| 719 | /* We do this so that we will respond with our native type. |
| 720 | * If we are TLSv1 and we get SSLv3, we will respond with TLSv1, |
| 721 | * This down switching should be handled by a different method. |
| 722 | * If we are SSLv3, we will respond with SSLv3, even if prompted with |
| 723 | * TLSv1. |
| 724 | */ |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 725 | switch (s->state) { |
| 726 | case SSL3_ST_SR_CLNT_HELLO_A: |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 727 | case SSL3_ST_SR_CLNT_HELLO_B: |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 728 | s->first_packet=1; |
| 729 | n=s->method->ssl_get_message(s, |
David Benjamin | 95fcaa4 | 2014-08-05 13:10:14 -0400 | [diff] [blame] | 730 | SSL3_ST_SR_CLNT_HELLO_A, |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 731 | SSL3_ST_SR_CLNT_HELLO_B, |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 732 | SSL3_MT_CLIENT_HELLO, |
| 733 | SSL3_RT_MAX_PLAIN_LENGTH, |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 734 | SSL_GET_MESSAGE_HASH_MESSAGE, |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 735 | &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 736 | |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 737 | if (!ok) return((int)n); |
| 738 | s->first_packet=0; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 739 | |
| 740 | /* If we require cookies and this ClientHello doesn't |
| 741 | * contain one, just return since we do not want to |
| 742 | * allocate any memory yet. So check cookie length... |
| 743 | */ |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 744 | if (SSL_IS_DTLS(s) && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 745 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 746 | CBS session_id; |
| 747 | uint8_t cookie_length; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 748 | |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 749 | CBS_init(&client_hello, s->init_msg, n); |
| 750 | if (!CBS_skip(&client_hello, 2 + SSL3_RANDOM_SIZE) || |
| 751 | !CBS_get_u8_length_prefixed(&client_hello, &session_id) || |
| 752 | !CBS_get_u8(&client_hello, &cookie_length)) |
| 753 | { |
| 754 | al = SSL_AD_DECODE_ERROR; |
| 755 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR); |
| 756 | goto f_err; |
| 757 | } |
| 758 | |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 759 | if (cookie_length == 0) |
| 760 | return 1; |
| 761 | } |
| 762 | s->state = SSL3_ST_SR_CLNT_HELLO_C; |
| 763 | /* fallthrough */ |
| 764 | case SSL3_ST_SR_CLNT_HELLO_C: |
| 765 | case SSL3_ST_SR_CLNT_HELLO_D: |
| 766 | /* We have previously parsed the ClientHello message, |
| 767 | * and can't call ssl_get_message again without hashing |
| 768 | * the message into the Finished digest again. */ |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 769 | n = s->init_num; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 770 | |
| 771 | memset(&early_ctx, 0, sizeof(early_ctx)); |
| 772 | early_ctx.ssl = s; |
| 773 | early_ctx.client_hello = s->init_msg; |
| 774 | early_ctx.client_hello_len = n; |
| 775 | if (!ssl_early_callback_init(&early_ctx)) |
| 776 | { |
| 777 | al = SSL_AD_DECODE_ERROR; |
| 778 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CLIENTHELLO_PARSE_FAILED); |
| 779 | goto f_err; |
| 780 | } |
| 781 | |
| 782 | if (s->state == SSL3_ST_SR_CLNT_HELLO_C && |
| 783 | s->ctx->select_certificate_cb != NULL) |
| 784 | { |
| 785 | int ret; |
| 786 | |
| 787 | s->state = SSL3_ST_SR_CLNT_HELLO_D; |
| 788 | ret = s->ctx->select_certificate_cb(&early_ctx); |
| 789 | if (ret == 0) |
| 790 | return CERTIFICATE_SELECTION_PENDING; |
| 791 | else if (ret == -1) |
| 792 | { |
| 793 | /* Connection rejected. */ |
| 794 | al = SSL_AD_ACCESS_DENIED; |
| 795 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CONNECTION_REJECTED); |
| 796 | goto f_err; |
| 797 | } |
| 798 | } |
| 799 | s->state = SSL3_ST_SR_CLNT_HELLO_D; |
David Benjamin | 4c852c5 | 2014-07-02 15:24:48 -0400 | [diff] [blame] | 800 | break; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 801 | default: |
David Benjamin | 4c852c5 | 2014-07-02 15:24:48 -0400 | [diff] [blame] | 802 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNKNOWN_STATE); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 803 | return -1; |
| 804 | } |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 805 | |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 806 | CBS_init(&client_hello, s->init_msg, n); |
| 807 | if (!CBS_get_u16(&client_hello, &client_version) || |
| 808 | !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) || |
| 809 | !CBS_get_u8_length_prefixed(&client_hello, &session_id) || |
| 810 | CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 811 | { |
| 812 | al = SSL_AD_DECODE_ERROR; |
| 813 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR); |
| 814 | goto f_err; |
| 815 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 816 | |
| 817 | /* use version from inside client hello, not from record header |
| 818 | * (may differ: see RFC 2246, Appendix E, second paragraph) */ |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 819 | s->client_version = client_version; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 820 | |
| 821 | if (SSL_IS_DTLS(s) ? (s->client_version > s->version && |
| 822 | s->method->version != DTLS_ANY_VERSION) |
| 823 | : (s->client_version < s->version)) |
| 824 | { |
| 825 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER); |
| 826 | if ((s->client_version>>8) == SSL3_VERSION_MAJOR && |
| 827 | !s->enc_write_ctx && !s->write_hash) |
| 828 | { |
| 829 | /* similar to ssl3_get_record, send alert using remote version number */ |
| 830 | s->version = s->client_version; |
| 831 | } |
| 832 | al = SSL_AD_PROTOCOL_VERSION; |
| 833 | goto f_err; |
| 834 | } |
| 835 | |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 836 | /* Load the client random. */ |
| 837 | memcpy(s->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 838 | |
| 839 | s->hit=0; |
| 840 | /* Versions before 0.9.7 always allow clients to resume sessions in renegotiation. |
| 841 | * 0.9.7 and later allow this by default, but optionally ignore resumption requests |
| 842 | * with flag SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather |
| 843 | * than a change to default behavior so that applications relying on this for security |
| 844 | * won't even compile against older library versions). |
| 845 | * |
| 846 | * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to request |
| 847 | * renegotiation but not a new session (s->new_session remains unset): for servers, |
| 848 | * this essentially just means that the SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
| 849 | * setting will be ignored. |
| 850 | */ |
| 851 | if ((s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) |
| 852 | { |
| 853 | if (!ssl_get_new_session(s,1)) |
| 854 | goto err; |
| 855 | } |
| 856 | else |
| 857 | { |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 858 | i=ssl_get_prev_session(s, &early_ctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 859 | if (i == 1) |
| 860 | { /* previous session */ |
| 861 | s->hit=1; |
| 862 | } |
| 863 | else if (i == -1) |
| 864 | goto err; |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 865 | else if (i == PENDING_SESSION) |
| 866 | { |
| 867 | ret = PENDING_SESSION; |
| 868 | goto err; |
| 869 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 870 | else /* i == 0 */ |
| 871 | { |
| 872 | if (!ssl_get_new_session(s,1)) |
| 873 | goto err; |
| 874 | } |
| 875 | } |
| 876 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 877 | if (SSL_IS_DTLS(s)) |
| 878 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 879 | CBS cookie; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 880 | |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 881 | /* TODO(davidben): The length check here is off. Per |
| 882 | * spec, the maximum cookie length is 32. However, the |
| 883 | * DTLS1_COOKIE_LENGTH check is checking against 256, |
| 884 | * not 32 (so it's actually redundant). |
| 885 | * 07a9d1a2c2b735cbc327065000b545deb5e136cf from |
| 886 | * OpenSSL switched this from 32 to 256. */ |
| 887 | if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) || |
| 888 | CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 889 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 890 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 891 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 892 | goto f_err; |
| 893 | } |
| 894 | |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 895 | /* Verify the cookie if appropriate option is set. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 896 | if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 897 | CBS_len(&cookie) > 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 898 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 899 | if (s->ctx->app_verify_cookie_cb != NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 900 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 901 | if (s->ctx->app_verify_cookie_cb(s, |
David Benjamin | fb4ea28 | 2014-08-15 13:38:15 -0400 | [diff] [blame] | 902 | CBS_data(&cookie), CBS_len(&cookie)) == 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 903 | { |
| 904 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 905 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_COOKIE_MISMATCH); |
| 906 | goto f_err; |
| 907 | } |
| 908 | /* else cookie verification succeeded */ |
| 909 | } |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 910 | else if (!CBS_mem_equal(&cookie, s->d1->cookie, s->d1->cookie_len)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 911 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 912 | /* default verification */ |
| 913 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 914 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_COOKIE_MISMATCH); |
| 915 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 916 | } |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 917 | /* Set to -2 so if successful we return 2 and |
| 918 | * don't send HelloVerifyRequest. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 919 | ret = -2; |
| 920 | } |
| 921 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 922 | if (s->method->version == DTLS_ANY_VERSION) |
| 923 | { |
| 924 | /* Select version to use */ |
| 925 | if (s->client_version <= DTLS1_2_VERSION && |
| 926 | !(s->options & SSL_OP_NO_DTLSv1_2)) |
| 927 | { |
| 928 | s->version = DTLS1_2_VERSION; |
| 929 | s->method = DTLSv1_2_server_method(); |
| 930 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 931 | else if (s->client_version <= DTLS1_VERSION && |
| 932 | !(s->options & SSL_OP_NO_DTLSv1)) |
| 933 | { |
| 934 | s->version = DTLS1_VERSION; |
| 935 | s->method = DTLSv1_server_method(); |
| 936 | } |
| 937 | else |
| 938 | { |
| 939 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER); |
| 940 | s->version = s->client_version; |
| 941 | al = SSL_AD_PROTOCOL_VERSION; |
| 942 | goto f_err; |
| 943 | } |
| 944 | s->session->ssl_version = s->version; |
| 945 | } |
| 946 | } |
| 947 | |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 948 | if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) || |
| 949 | !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) || |
| 950 | CBS_len(&compression_methods) == 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 951 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 952 | al = SSL_AD_DECODE_ERROR; |
| 953 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR); |
| 954 | goto f_err; |
| 955 | } |
| 956 | |
| 957 | /* TODO(davidben): Per spec, cipher_suites can never be empty |
| 958 | * (specified at the ClientHello structure level). This logic |
| 959 | * allows it to be empty if resuming a session. Can we always |
| 960 | * require non-empty? If a client sends empty cipher_suites |
| 961 | * because it's resuming a session, it could always fail to |
| 962 | * resume a session, so it's unlikely to actually work. */ |
| 963 | if (CBS_len(&cipher_suites) == 0 && CBS_len(&session_id) != 0) |
| 964 | { |
| 965 | /* We need a cipher if we are not resuming a session. */ |
| 966 | al = SSL_AD_ILLEGAL_PARAMETER; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 967 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_SPECIFIED); |
| 968 | goto f_err; |
| 969 | } |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 970 | |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 971 | if (ssl_bytes_to_cipher_list(s, &cipher_suites, &ciphers) == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 972 | { |
| 973 | goto err; |
| 974 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 975 | |
| 976 | /* If it is a hit, check that the cipher is in the list */ |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 977 | if (s->hit && CBS_len(&cipher_suites) > 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 978 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 979 | int found_cipher = 0; |
| 980 | unsigned long id = s->session->cipher->id; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 981 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 982 | for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++) |
| 983 | { |
| 984 | c=sk_SSL_CIPHER_value(ciphers,i); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 985 | if (c->id == id) |
| 986 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 987 | found_cipher = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 988 | break; |
| 989 | } |
| 990 | } |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 991 | if (!found_cipher) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 992 | { |
| 993 | /* we need to have the cipher in the cipher |
| 994 | * list if we are asked to reuse it */ |
| 995 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 996 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_REQUIRED_CIPHER_MISSING); |
| 997 | goto f_err; |
| 998 | } |
| 999 | } |
| 1000 | |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 1001 | /* Only null compression is supported. */ |
| 1002 | if (memchr(CBS_data(&compression_methods), 0, |
| 1003 | CBS_len(&compression_methods)) == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1004 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 1005 | al = SSL_AD_ILLEGAL_PARAMETER; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1006 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_COMPRESSION_SPECIFIED); |
| 1007 | goto f_err; |
| 1008 | } |
| 1009 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1010 | /* TLS extensions*/ |
| 1011 | if (s->version >= SSL3_VERSION) |
| 1012 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 1013 | if (!ssl_parse_clienthello_tlsext(s, &client_hello)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1014 | { |
| 1015 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_PARSE_TLSEXT); |
| 1016 | goto err; |
| 1017 | } |
| 1018 | } |
| 1019 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1020 | /* There should be nothing left over in the record. */ |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 1021 | if (CBS_len(&client_hello) != 0) |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1022 | { |
| 1023 | /* wrong packet length */ |
| 1024 | al=SSL_AD_DECODE_ERROR; |
| 1025 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_BAD_PACKET_LENGTH); |
| 1026 | goto f_err; |
| 1027 | } |
| 1028 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1029 | /* Check if we want to use external pre-shared secret for this |
| 1030 | * handshake for not reused session only. We need to generate |
| 1031 | * server_random before calling tls_session_secret_cb in order to allow |
| 1032 | * SessionTicket processing to use it in key derivation. */ |
| 1033 | { |
| 1034 | unsigned char *pos; |
| 1035 | pos=s->s3->server_random; |
| 1036 | if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) |
| 1037 | { |
| 1038 | goto f_err; |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) |
| 1043 | { |
David Benjamin | 6f26001 | 2014-08-15 13:49:12 -0400 | [diff] [blame] | 1044 | const SSL_CIPHER *pref_cipher=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1045 | |
| 1046 | s->session->master_key_length=sizeof(s->session->master_key); |
| 1047 | if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length, |
| 1048 | ciphers, &pref_cipher, s->tls_session_secret_cb_arg)) |
| 1049 | { |
| 1050 | s->hit=1; |
| 1051 | s->session->ciphers=ciphers; |
| 1052 | s->session->verify_result=X509_V_OK; |
| 1053 | |
| 1054 | ciphers=NULL; |
| 1055 | |
| 1056 | /* check if some cipher was preferred by call back */ |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1057 | pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, ssl_get_cipher_preferences(s)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1058 | if (pref_cipher == NULL) |
| 1059 | { |
| 1060 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1061 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER); |
| 1062 | goto f_err; |
| 1063 | } |
| 1064 | |
| 1065 | s->session->cipher=pref_cipher; |
| 1066 | |
| 1067 | if (s->cipher_list) |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1068 | ssl_cipher_preference_list_free(s->cipher_list); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1069 | |
| 1070 | if (s->cipher_list_by_id) |
| 1071 | sk_SSL_CIPHER_free(s->cipher_list_by_id); |
| 1072 | |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1073 | s->cipher_list = ssl_cipher_preference_list_from_ciphers(s->session->ciphers); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1074 | s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers); |
| 1075 | } |
| 1076 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1077 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1078 | /* Given s->session->ciphers and SSL_get_ciphers, we must |
| 1079 | * pick a cipher */ |
| 1080 | |
| 1081 | if (!s->hit) |
| 1082 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1083 | if (s->session->ciphers != NULL) |
| 1084 | sk_SSL_CIPHER_free(s->session->ciphers); |
| 1085 | s->session->ciphers=ciphers; |
| 1086 | if (ciphers == NULL) |
| 1087 | { |
| 1088 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 1089 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_PASSED); |
| 1090 | goto f_err; |
| 1091 | } |
| 1092 | ciphers=NULL; |
| 1093 | /* Let cert callback update server certificates if required */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1094 | if (s->cert->cert_cb) |
| 1095 | { |
| 1096 | int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg); |
| 1097 | if (rv == 0) |
| 1098 | { |
| 1099 | al=SSL_AD_INTERNAL_ERROR; |
| 1100 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CERT_CB_ERROR); |
| 1101 | goto f_err; |
| 1102 | } |
| 1103 | if (rv < 0) |
| 1104 | { |
| 1105 | s->rwstate=SSL_X509_LOOKUP; |
| 1106 | return -1; |
| 1107 | } |
| 1108 | s->rwstate = SSL_NOTHING; |
| 1109 | } |
| 1110 | c=ssl3_choose_cipher(s,s->session->ciphers, |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1111 | ssl_get_cipher_preferences(s)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1112 | |
| 1113 | if (c == NULL) |
| 1114 | { |
| 1115 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1116 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER); |
| 1117 | goto f_err; |
| 1118 | } |
| 1119 | s->s3->tmp.new_cipher=c; |
| 1120 | } |
| 1121 | else |
| 1122 | { |
| 1123 | /* Session-id reuse */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1124 | s->s3->tmp.new_cipher=s->session->cipher; |
| 1125 | } |
| 1126 | |
| 1127 | if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) |
| 1128 | { |
| 1129 | if (!ssl3_digest_cached_records(s)) |
| 1130 | goto f_err; |
| 1131 | } |
| 1132 | |
| 1133 | /* we now have the following setup. |
| 1134 | * client_random |
| 1135 | * cipher_list - our prefered list of ciphers |
| 1136 | * ciphers - the clients prefered list of ciphers |
| 1137 | * compression - basically ignored right now |
| 1138 | * ssl version is set - sslv3 |
| 1139 | * s->session - The ssl session has been setup. |
| 1140 | * s->hit - session reuse flag |
| 1141 | * s->tmp.new_cipher - the new cipher to use. |
| 1142 | */ |
| 1143 | |
| 1144 | /* Handles TLS extensions that we couldn't check earlier */ |
| 1145 | if (s->version >= SSL3_VERSION) |
| 1146 | { |
| 1147 | if (ssl_check_clienthello_tlsext_late(s) <= 0) |
| 1148 | { |
| 1149 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CLIENTHELLO_TLSEXT); |
| 1150 | goto err; |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | if (ret < 0) ret=-ret; |
| 1155 | if (0) |
| 1156 | { |
| 1157 | f_err: |
| 1158 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1159 | } |
| 1160 | err: |
| 1161 | if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers); |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1162 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | int ssl3_send_server_hello(SSL *s) |
| 1166 | { |
| 1167 | unsigned char *buf; |
| 1168 | unsigned char *p,*d; |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 1169 | int sl; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1170 | unsigned long l; |
| 1171 | |
| 1172 | if (s->state == SSL3_ST_SW_SRVR_HELLO_A) |
| 1173 | { |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1174 | /* We only accept ChannelIDs on connections with ECDHE in order |
| 1175 | * to avoid a known attack while we fix ChannelID itself. */ |
| 1176 | if (s->s3 && |
| 1177 | s->s3->tlsext_channel_id_valid && |
| 1178 | (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kEECDH) == 0) |
| 1179 | s->s3->tlsext_channel_id_valid = 0; |
| 1180 | |
| 1181 | /* If this is a resumption and the original handshake didn't |
| 1182 | * support ChannelID then we didn't record the original |
| 1183 | * handshake hashes in the session and so cannot resume with |
| 1184 | * ChannelIDs. */ |
| 1185 | if (s->hit && |
| 1186 | s->s3->tlsext_channel_id_new && |
| 1187 | s->session->original_handshake_hash_len == 0) |
| 1188 | s->s3->tlsext_channel_id_valid = 0; |
| 1189 | |
Adam Langley | 1b9d477 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1190 | if (s->mode & SSL_MODE_RELEASE_BUFFERS) |
| 1191 | { |
| 1192 | /* Free s->session->ciphers in order to release memory. This |
| 1193 | * breaks SSL_get_shared_ciphers(), but many servers will |
David Benjamin | 5ffeb7c | 2014-07-15 00:34:01 -0400 | [diff] [blame] | 1194 | * prefer the memory savings. */ |
Adam Langley | 1b9d477 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1195 | sk_SSL_CIPHER_free(s->session->ciphers); |
| 1196 | s->session->ciphers = NULL; |
| 1197 | } |
| 1198 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1199 | buf=(unsigned char *)s->init_buf->data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1200 | /* Do the message type and length last */ |
| 1201 | d=p= ssl_handshake_start(s); |
| 1202 | |
| 1203 | *(p++)=s->version>>8; |
| 1204 | *(p++)=s->version&0xff; |
| 1205 | |
| 1206 | /* Random stuff */ |
| 1207 | memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); |
| 1208 | p+=SSL3_RANDOM_SIZE; |
| 1209 | |
| 1210 | /* There are several cases for the session ID to send |
| 1211 | * back in the server hello: |
| 1212 | * - For session reuse from the session cache, |
| 1213 | * we send back the old session ID. |
| 1214 | * - If stateless session reuse (using a session ticket) |
| 1215 | * is successful, we send back the client's "session ID" |
| 1216 | * (which doesn't actually identify the session). |
| 1217 | * - If it is a new session, we send back the new |
| 1218 | * session ID. |
| 1219 | * - However, if we want the new session to be single-use, |
| 1220 | * we send back a 0-length session ID. |
| 1221 | * s->hit is non-zero in either case of session reuse, |
| 1222 | * so the following won't overwrite an ID that we're supposed |
| 1223 | * to send back. |
| 1224 | */ |
| 1225 | if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) |
| 1226 | && !s->hit) |
| 1227 | s->session->session_id_length=0; |
| 1228 | |
| 1229 | sl=s->session->session_id_length; |
| 1230 | if (sl > (int)sizeof(s->session->session_id)) |
| 1231 | { |
| 1232 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR); |
| 1233 | return -1; |
| 1234 | } |
| 1235 | *(p++)=sl; |
| 1236 | memcpy(p,s->session->session_id,sl); |
| 1237 | p+=sl; |
| 1238 | |
| 1239 | /* put the cipher */ |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 1240 | s2n(ssl3_get_cipher_value(s->s3->tmp.new_cipher), p); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1241 | |
| 1242 | /* put the compression method */ |
| 1243 | *(p++)=0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1244 | if (ssl_prepare_serverhello_tlsext(s) <= 0) |
| 1245 | { |
| 1246 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, SSL_R_SERVERHELLO_TLSEXT); |
| 1247 | return -1; |
| 1248 | } |
| 1249 | if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL) |
| 1250 | { |
| 1251 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR); |
| 1252 | return -1; |
| 1253 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1254 | /* do the header */ |
| 1255 | l=(p-d); |
| 1256 | ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l); |
| 1257 | s->state=SSL3_ST_SW_SRVR_HELLO_B; |
| 1258 | } |
| 1259 | |
| 1260 | /* SSL3_ST_SW_SRVR_HELLO_B */ |
| 1261 | return ssl_do_write(s); |
| 1262 | } |
| 1263 | |
| 1264 | int ssl3_send_server_done(SSL *s) |
| 1265 | { |
| 1266 | |
| 1267 | if (s->state == SSL3_ST_SW_SRVR_DONE_A) |
| 1268 | { |
| 1269 | ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0); |
| 1270 | s->state = SSL3_ST_SW_SRVR_DONE_B; |
| 1271 | } |
| 1272 | |
| 1273 | /* SSL3_ST_SW_SRVR_DONE_B */ |
| 1274 | return ssl_do_write(s); |
| 1275 | } |
| 1276 | |
| 1277 | int ssl3_send_server_key_exchange(SSL *s) |
| 1278 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1279 | unsigned char *q; |
| 1280 | int j,num; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1281 | unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; |
| 1282 | unsigned int u; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1283 | #ifndef OPENSSL_NO_DH |
| 1284 | DH *dh=NULL,*dhp; |
| 1285 | #endif |
| 1286 | #ifndef OPENSSL_NO_ECDH |
| 1287 | EC_KEY *ecdh=NULL, *ecdhp; |
| 1288 | unsigned char *encodedPoint = NULL; |
| 1289 | int encodedlen = 0; |
| 1290 | int curve_id = 0; |
| 1291 | BN_CTX *bn_ctx = NULL; |
| 1292 | #endif |
David Benjamin | 77a942b | 2014-07-15 01:22:50 -0400 | [diff] [blame] | 1293 | const char* psk_identity_hint = NULL; |
| 1294 | size_t psk_identity_hint_len = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1295 | EVP_PKEY *pkey; |
| 1296 | const EVP_MD *md = NULL; |
| 1297 | unsigned char *p,*d; |
| 1298 | int al,i; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1299 | unsigned long alg_k; |
| 1300 | unsigned long alg_a; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1301 | int n; |
| 1302 | CERT *cert; |
| 1303 | BIGNUM *r[4]; |
| 1304 | int nr[4],kn; |
| 1305 | BUF_MEM *buf; |
| 1306 | EVP_MD_CTX md_ctx; |
| 1307 | |
| 1308 | EVP_MD_CTX_init(&md_ctx); |
| 1309 | if (s->state == SSL3_ST_SW_KEY_EXCH_A) |
| 1310 | { |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1311 | alg_k=s->s3->tmp.new_cipher->algorithm_mkey; |
| 1312 | alg_a=s->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1313 | cert=s->cert; |
| 1314 | |
| 1315 | buf=s->init_buf; |
| 1316 | |
| 1317 | r[0]=r[1]=r[2]=r[3]=NULL; |
| 1318 | n=0; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1319 | if (alg_a & SSL_aPSK) |
| 1320 | { |
| 1321 | /* size for PSK identity hint */ |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1322 | psk_identity_hint = s->session->psk_identity_hint; |
| 1323 | if (psk_identity_hint) |
| 1324 | psk_identity_hint_len = strlen(psk_identity_hint); |
| 1325 | else |
| 1326 | psk_identity_hint_len = 0; |
| 1327 | n+=2+psk_identity_hint_len; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1328 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1329 | #ifndef OPENSSL_NO_DH |
David Benjamin | 77a942b | 2014-07-15 01:22:50 -0400 | [diff] [blame] | 1330 | if (alg_k & SSL_kEDH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1331 | { |
| 1332 | dhp=cert->dh_tmp; |
| 1333 | if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL)) |
David Benjamin | 060d9d2 | 2014-07-15 00:54:26 -0400 | [diff] [blame] | 1334 | dhp=s->cert->dh_tmp_cb(s, 0, 1024); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1335 | if (dhp == NULL) |
| 1336 | { |
| 1337 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1338 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_MISSING_TMP_DH_KEY); |
| 1339 | goto f_err; |
| 1340 | } |
| 1341 | |
| 1342 | if (s->s3->tmp.dh != NULL) |
| 1343 | { |
| 1344 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_INTERNAL_ERROR); |
| 1345 | goto err; |
| 1346 | } |
| 1347 | |
| 1348 | if ((dh=DHparams_dup(dhp)) == NULL) |
| 1349 | { |
| 1350 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB); |
| 1351 | goto err; |
| 1352 | } |
| 1353 | |
| 1354 | s->s3->tmp.dh=dh; |
| 1355 | if ((dhp->pub_key == NULL || |
| 1356 | dhp->priv_key == NULL || |
| 1357 | (s->options & SSL_OP_SINGLE_DH_USE))) |
| 1358 | { |
| 1359 | if(!DH_generate_key(dh)) |
| 1360 | { |
| 1361 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB); |
| 1362 | goto err; |
| 1363 | } |
| 1364 | } |
| 1365 | else |
| 1366 | { |
| 1367 | dh->pub_key=BN_dup(dhp->pub_key); |
| 1368 | dh->priv_key=BN_dup(dhp->priv_key); |
| 1369 | if ((dh->pub_key == NULL) || |
| 1370 | (dh->priv_key == NULL)) |
| 1371 | { |
| 1372 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB); |
| 1373 | goto err; |
| 1374 | } |
| 1375 | } |
| 1376 | r[0]=dh->p; |
| 1377 | r[1]=dh->g; |
| 1378 | r[2]=dh->pub_key; |
| 1379 | } |
David Benjamin | 77a942b | 2014-07-15 01:22:50 -0400 | [diff] [blame] | 1380 | else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1381 | #endif |
| 1382 | #ifndef OPENSSL_NO_ECDH |
David Benjamin | 77a942b | 2014-07-15 01:22:50 -0400 | [diff] [blame] | 1383 | if (alg_k & SSL_kEECDH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1384 | { |
| 1385 | const EC_GROUP *group; |
| 1386 | |
| 1387 | ecdhp=cert->ecdh_tmp; |
| 1388 | if (s->cert->ecdh_tmp_auto) |
| 1389 | { |
| 1390 | /* Get NID of appropriate shared curve */ |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 1391 | int nid = tls1_get_shared_curve(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1392 | if (nid != NID_undef) |
| 1393 | ecdhp = EC_KEY_new_by_curve_name(nid); |
| 1394 | } |
| 1395 | else if ((ecdhp == NULL) && s->cert->ecdh_tmp_cb) |
| 1396 | { |
David Benjamin | 060d9d2 | 2014-07-15 00:54:26 -0400 | [diff] [blame] | 1397 | ecdhp = s->cert->ecdh_tmp_cb(s, 0, 1024); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1398 | } |
| 1399 | if (ecdhp == NULL) |
| 1400 | { |
| 1401 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1402 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_MISSING_TMP_ECDH_KEY); |
| 1403 | goto f_err; |
| 1404 | } |
| 1405 | |
| 1406 | if (s->s3->tmp.ecdh != NULL) |
| 1407 | { |
| 1408 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_INTERNAL_ERROR); |
| 1409 | goto err; |
| 1410 | } |
| 1411 | |
| 1412 | /* Duplicate the ECDH structure. */ |
| 1413 | if (ecdhp == NULL) |
| 1414 | { |
| 1415 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1416 | goto err; |
| 1417 | } |
| 1418 | if (s->cert->ecdh_tmp_auto) |
| 1419 | ecdh = ecdhp; |
| 1420 | else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) |
| 1421 | { |
| 1422 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1423 | goto err; |
| 1424 | } |
| 1425 | |
| 1426 | s->s3->tmp.ecdh=ecdh; |
| 1427 | if ((EC_KEY_get0_public_key(ecdh) == NULL) || |
| 1428 | (EC_KEY_get0_private_key(ecdh) == NULL) || |
| 1429 | (s->options & SSL_OP_SINGLE_ECDH_USE)) |
| 1430 | { |
| 1431 | if(!EC_KEY_generate_key(ecdh)) |
| 1432 | { |
| 1433 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1434 | goto err; |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | if (((group = EC_KEY_get0_group(ecdh)) == NULL) || |
| 1439 | (EC_KEY_get0_public_key(ecdh) == NULL) || |
| 1440 | (EC_KEY_get0_private_key(ecdh) == NULL)) |
| 1441 | { |
| 1442 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1443 | goto err; |
| 1444 | } |
| 1445 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1446 | /* XXX: For now, we only support ephemeral ECDH |
| 1447 | * keys over named (not generic) curves. For |
| 1448 | * supported named curves, curve_id is non-zero. |
| 1449 | */ |
| 1450 | if ((curve_id = |
| 1451 | tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group))) |
| 1452 | == 0) |
| 1453 | { |
| 1454 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); |
| 1455 | goto err; |
| 1456 | } |
| 1457 | |
| 1458 | /* Encode the public key. |
| 1459 | * First check the size of encoding and |
| 1460 | * allocate memory accordingly. |
| 1461 | */ |
| 1462 | encodedlen = EC_POINT_point2oct(group, |
| 1463 | EC_KEY_get0_public_key(ecdh), |
| 1464 | POINT_CONVERSION_UNCOMPRESSED, |
| 1465 | NULL, 0, NULL); |
| 1466 | |
| 1467 | encodedPoint = (unsigned char *) |
| 1468 | OPENSSL_malloc(encodedlen*sizeof(unsigned char)); |
| 1469 | bn_ctx = BN_CTX_new(); |
| 1470 | if ((encodedPoint == NULL) || (bn_ctx == NULL)) |
| 1471 | { |
| 1472 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_MALLOC_FAILURE); |
| 1473 | goto err; |
| 1474 | } |
| 1475 | |
| 1476 | |
| 1477 | encodedlen = EC_POINT_point2oct(group, |
| 1478 | EC_KEY_get0_public_key(ecdh), |
| 1479 | POINT_CONVERSION_UNCOMPRESSED, |
| 1480 | encodedPoint, encodedlen, bn_ctx); |
| 1481 | |
| 1482 | if (encodedlen == 0) |
| 1483 | { |
| 1484 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1485 | goto err; |
| 1486 | } |
| 1487 | |
| 1488 | BN_CTX_free(bn_ctx); bn_ctx=NULL; |
| 1489 | |
| 1490 | /* XXX: For now, we only support named (not |
| 1491 | * generic) curves in ECDH ephemeral key exchanges. |
| 1492 | * In this situation, we need four additional bytes |
| 1493 | * to encode the entire ServerECDHParams |
| 1494 | * structure. |
| 1495 | */ |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1496 | n += 4 + encodedlen; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1497 | |
| 1498 | /* We'll generate the serverKeyExchange message |
| 1499 | * explicitly so we can set these to NULLs |
| 1500 | */ |
| 1501 | r[0]=NULL; |
| 1502 | r[1]=NULL; |
| 1503 | r[2]=NULL; |
| 1504 | r[3]=NULL; |
| 1505 | } |
David Benjamin | 77a942b | 2014-07-15 01:22:50 -0400 | [diff] [blame] | 1506 | else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1507 | #endif /* !OPENSSL_NO_ECDH */ |
David Benjamin | 77a942b | 2014-07-15 01:22:50 -0400 | [diff] [blame] | 1508 | if (!(alg_k & SSL_kPSK)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1509 | { |
| 1510 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1511 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); |
| 1512 | goto f_err; |
| 1513 | } |
| 1514 | for (i=0; i < 4 && r[i] != NULL; i++) |
| 1515 | { |
| 1516 | nr[i]=BN_num_bytes(r[i]); |
| 1517 | n+=2+nr[i]; |
| 1518 | } |
| 1519 | |
David Benjamin | e8f3d66 | 2014-07-12 01:10:19 -0400 | [diff] [blame] | 1520 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1521 | { |
| 1522 | if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher,&md)) |
| 1523 | == NULL) |
| 1524 | { |
| 1525 | al=SSL_AD_DECODE_ERROR; |
| 1526 | goto f_err; |
| 1527 | } |
| 1528 | kn=EVP_PKEY_size(pkey); |
| 1529 | } |
| 1530 | else |
| 1531 | { |
| 1532 | pkey=NULL; |
| 1533 | kn=0; |
| 1534 | } |
| 1535 | |
| 1536 | if (!BUF_MEM_grow_clean(buf,n+SSL_HM_HEADER_LENGTH(s)+kn)) |
| 1537 | { |
| 1538 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_BUF); |
| 1539 | goto err; |
| 1540 | } |
| 1541 | d = p = ssl_handshake_start(s); |
| 1542 | |
| 1543 | for (i=0; i < 4 && r[i] != NULL; i++) |
| 1544 | { |
| 1545 | s2n(nr[i],p); |
| 1546 | BN_bn2bin(r[i],p); |
| 1547 | p+=nr[i]; |
| 1548 | } |
| 1549 | |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1550 | /* Note: ECDHE PSK ciphersuites use SSL_kEECDH and SSL_aPSK. |
| 1551 | * When one of them is used, the server key exchange record needs to have both |
| 1552 | * the psk_identity_hint and the ServerECDHParams. */ |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1553 | if (alg_a & SSL_aPSK) |
| 1554 | { |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1555 | /* copy PSK identity hint (if provided) */ |
| 1556 | s2n(psk_identity_hint_len, p); |
| 1557 | if (psk_identity_hint_len > 0) |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1558 | { |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1559 | memcpy(p, psk_identity_hint, psk_identity_hint_len); |
| 1560 | p+=psk_identity_hint_len; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1561 | } |
| 1562 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1563 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1564 | #ifndef OPENSSL_NO_ECDH |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1565 | if (alg_k & SSL_kEECDH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1566 | { |
| 1567 | /* XXX: For now, we only support named (not generic) curves. |
| 1568 | * In this situation, the serverKeyExchange message has: |
| 1569 | * [1 byte CurveType], [2 byte CurveName] |
| 1570 | * [1 byte length of encoded point], followed by |
| 1571 | * the actual encoded point itself |
| 1572 | */ |
| 1573 | *p = NAMED_CURVE_TYPE; |
| 1574 | p += 1; |
| 1575 | *p = 0; |
| 1576 | p += 1; |
| 1577 | *p = curve_id; |
| 1578 | p += 1; |
| 1579 | *p = encodedlen; |
| 1580 | p += 1; |
| 1581 | memcpy((unsigned char*)p, |
| 1582 | (unsigned char *)encodedPoint, |
| 1583 | encodedlen); |
| 1584 | OPENSSL_free(encodedPoint); |
| 1585 | encodedPoint = NULL; |
| 1586 | p += encodedlen; |
| 1587 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1588 | #endif /* OPENSSL_NO_ECDH */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1589 | |
| 1590 | /* not anonymous */ |
| 1591 | if (pkey != NULL) |
| 1592 | { |
| 1593 | /* n is the length of the params, they start at &(d[4]) |
| 1594 | * and p points to the space at the end. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1595 | if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) |
| 1596 | { |
| 1597 | q=md_buf; |
| 1598 | j=0; |
| 1599 | for (num=2; num > 0; num--) |
| 1600 | { |
David Benjamin | e712778 | 2014-08-18 23:15:29 -0400 | [diff] [blame] | 1601 | EVP_DigestInit_ex(&md_ctx, |
| 1602 | (num == 2) ? EVP_md5() : EVP_sha1(), NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1603 | EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); |
| 1604 | EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); |
| 1605 | EVP_DigestUpdate(&md_ctx,d,n); |
| 1606 | EVP_DigestFinal_ex(&md_ctx,q, |
| 1607 | (unsigned int *)&i); |
| 1608 | q+=i; |
| 1609 | j+=i; |
| 1610 | } |
| 1611 | if (RSA_sign(NID_md5_sha1, md_buf, j, |
| 1612 | &(p[2]), &u, pkey->pkey.rsa) <= 0) |
| 1613 | { |
| 1614 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_RSA); |
| 1615 | goto err; |
| 1616 | } |
| 1617 | s2n(u,p); |
| 1618 | n+=u+2; |
| 1619 | } |
| 1620 | else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1621 | if (md) |
| 1622 | { |
| 1623 | /* send signature algorithm */ |
| 1624 | if (SSL_USE_SIGALGS(s)) |
| 1625 | { |
| 1626 | if (!tls12_get_sigandhash(p, pkey, md)) |
| 1627 | { |
| 1628 | /* Should never happen */ |
| 1629 | al=SSL_AD_INTERNAL_ERROR; |
| 1630 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_INTERNAL_ERROR); |
| 1631 | goto f_err; |
| 1632 | } |
| 1633 | p+=2; |
| 1634 | } |
| 1635 | #ifdef SSL_DEBUG |
| 1636 | fprintf(stderr, "Using hash %s\n", |
| 1637 | EVP_MD_name(md)); |
| 1638 | #endif |
| 1639 | EVP_SignInit_ex(&md_ctx, md, NULL); |
| 1640 | EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); |
| 1641 | EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); |
| 1642 | EVP_SignUpdate(&md_ctx,d,n); |
| 1643 | if (!EVP_SignFinal(&md_ctx,&(p[2]), |
| 1644 | (unsigned int *)&i,pkey)) |
| 1645 | { |
| 1646 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_EVP); |
| 1647 | goto err; |
| 1648 | } |
| 1649 | s2n(i,p); |
| 1650 | n+=i+2; |
| 1651 | if (SSL_USE_SIGALGS(s)) |
| 1652 | n+= 2; |
| 1653 | } |
| 1654 | else |
| 1655 | { |
| 1656 | /* Is this error check actually needed? */ |
| 1657 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1658 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_UNKNOWN_PKEY_TYPE); |
| 1659 | goto f_err; |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n); |
| 1664 | } |
| 1665 | |
| 1666 | s->state = SSL3_ST_SW_KEY_EXCH_B; |
| 1667 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1668 | return ssl_do_write(s); |
| 1669 | f_err: |
| 1670 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1671 | err: |
| 1672 | #ifndef OPENSSL_NO_ECDH |
| 1673 | if (encodedPoint != NULL) OPENSSL_free(encodedPoint); |
| 1674 | BN_CTX_free(bn_ctx); |
| 1675 | #endif |
| 1676 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1677 | return(-1); |
| 1678 | } |
| 1679 | |
| 1680 | int ssl3_send_certificate_request(SSL *s) |
| 1681 | { |
| 1682 | unsigned char *p,*d; |
| 1683 | int i,j,nl,off,n; |
| 1684 | STACK_OF(X509_NAME) *sk=NULL; |
| 1685 | X509_NAME *name; |
| 1686 | BUF_MEM *buf; |
| 1687 | |
| 1688 | if (s->state == SSL3_ST_SW_CERT_REQ_A) |
| 1689 | { |
| 1690 | buf=s->init_buf; |
| 1691 | |
| 1692 | d=p=ssl_handshake_start(s); |
| 1693 | |
| 1694 | /* get the list of acceptable cert types */ |
| 1695 | p++; |
| 1696 | n=ssl3_get_req_cert_type(s,p); |
| 1697 | d[0]=n; |
| 1698 | p+=n; |
| 1699 | n++; |
| 1700 | |
| 1701 | if (SSL_USE_SIGALGS(s)) |
| 1702 | { |
| 1703 | const unsigned char *psigs; |
| 1704 | nl = tls12_get_psigalgs(s, &psigs); |
| 1705 | s2n(nl, p); |
| 1706 | memcpy(p, psigs, nl); |
| 1707 | p += nl; |
| 1708 | n += nl + 2; |
| 1709 | } |
| 1710 | |
| 1711 | off=n; |
| 1712 | p+=2; |
| 1713 | n+=2; |
| 1714 | |
| 1715 | sk=SSL_get_client_CA_list(s); |
| 1716 | nl=0; |
| 1717 | if (sk != NULL) |
| 1718 | { |
| 1719 | for (i=0; i<sk_X509_NAME_num(sk); i++) |
| 1720 | { |
| 1721 | name=sk_X509_NAME_value(sk,i); |
| 1722 | j=i2d_X509_NAME(name,NULL); |
| 1723 | if (!BUF_MEM_grow_clean(buf,SSL_HM_HEADER_LENGTH(s)+n+j+2)) |
| 1724 | { |
| 1725 | OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB); |
| 1726 | goto err; |
| 1727 | } |
| 1728 | p = ssl_handshake_start(s) + n; |
Alex Chernyakhovsky | 9c890d4 | 2014-07-05 00:53:11 -0400 | [diff] [blame] | 1729 | s2n(j,p); |
| 1730 | i2d_X509_NAME(name,&p); |
| 1731 | n+=2+j; |
| 1732 | nl+=2+j; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1733 | } |
| 1734 | } |
| 1735 | /* else no CA names */ |
| 1736 | p = ssl_handshake_start(s) + off; |
| 1737 | s2n(nl,p); |
| 1738 | |
| 1739 | ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n); |
| 1740 | |
| 1741 | #ifdef NETSCAPE_HANG_BUG |
| 1742 | if (!SSL_IS_DTLS(s)) |
| 1743 | { |
Adam Langley | ce1cfe1 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1744 | if (!BUF_MEM_grow_clean(buf, s->init_num + 4)) |
| 1745 | { |
| 1746 | OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB); |
| 1747 | goto err; |
| 1748 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1749 | p=(unsigned char *)s->init_buf->data + s->init_num; |
| 1750 | /* do the header */ |
| 1751 | *(p++)=SSL3_MT_SERVER_DONE; |
| 1752 | *(p++)=0; |
| 1753 | *(p++)=0; |
| 1754 | *(p++)=0; |
| 1755 | s->init_num += 4; |
| 1756 | } |
| 1757 | #endif |
| 1758 | |
| 1759 | s->state = SSL3_ST_SW_CERT_REQ_B; |
| 1760 | } |
| 1761 | |
| 1762 | /* SSL3_ST_SW_CERT_REQ_B */ |
| 1763 | return ssl_do_write(s); |
| 1764 | err: |
| 1765 | return(-1); |
| 1766 | } |
| 1767 | |
| 1768 | int ssl3_get_client_key_exchange(SSL *s) |
| 1769 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1770 | int al,ok; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1771 | long n; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1772 | CBS client_key_exchange; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1773 | unsigned long alg_k; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1774 | unsigned long alg_a; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1775 | uint8_t *premaster_secret = NULL; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1776 | size_t premaster_secret_len = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1777 | RSA *rsa=NULL; |
David Benjamin | 0aa0767 | 2014-07-24 15:34:14 -0400 | [diff] [blame] | 1778 | uint8_t *decrypt_buf = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1779 | EVP_PKEY *pkey=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1780 | #ifndef OPENSSL_NO_DH |
| 1781 | BIGNUM *pub=NULL; |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 1782 | DH *dh_srvr; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1783 | #endif |
| 1784 | |
| 1785 | #ifndef OPENSSL_NO_ECDH |
| 1786 | EC_KEY *srvr_ecdh = NULL; |
| 1787 | EVP_PKEY *clnt_pub_pkey = NULL; |
| 1788 | EC_POINT *clnt_ecpoint = NULL; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1789 | BN_CTX *bn_ctx = NULL; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1790 | unsigned int psk_len = 0; |
| 1791 | unsigned char psk[PSK_MAX_PSK_LEN]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1792 | #endif |
| 1793 | |
| 1794 | n=s->method->ssl_get_message(s, |
| 1795 | SSL3_ST_SR_KEY_EXCH_A, |
| 1796 | SSL3_ST_SR_KEY_EXCH_B, |
| 1797 | SSL3_MT_CLIENT_KEY_EXCHANGE, |
| 1798 | 2048, /* ??? */ |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 1799 | SSL_GET_MESSAGE_HASH_MESSAGE, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1800 | &ok); |
| 1801 | |
| 1802 | if (!ok) return((int)n); |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1803 | CBS_init(&client_key_exchange, s->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1804 | |
| 1805 | alg_k=s->s3->tmp.new_cipher->algorithm_mkey; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1806 | alg_a=s->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1807 | |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1808 | /* If using a PSK key exchange, prepare the pre-shared key. */ |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1809 | if (alg_a & SSL_aPSK) |
| 1810 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1811 | CBS psk_identity; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1812 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1813 | /* If using PSK, the ClientKeyExchange contains a |
| 1814 | * psk_identity. If PSK, then this is the only field |
| 1815 | * in the message. */ |
| 1816 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) || |
| 1817 | ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1818 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1819 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR); |
| 1820 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1821 | goto f_err; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1822 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1823 | |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1824 | if (s->psk_server_callback == NULL) |
| 1825 | { |
| 1826 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_PSK_NO_SERVER_CB); |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1827 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1828 | goto f_err; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1829 | } |
| 1830 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1831 | if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN || |
| 1832 | CBS_contains_zero_byte(&psk_identity)) |
| 1833 | { |
| 1834 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DATA_LENGTH_TOO_LONG); |
| 1835 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1836 | goto f_err; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1837 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1838 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1839 | if (!CBS_strdup(&psk_identity, &s->session->psk_identity)) |
| 1840 | { |
| 1841 | al = SSL_AD_INTERNAL_ERROR; |
| 1842 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1843 | goto f_err; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | /* Look up the key for the identity. */ |
| 1847 | psk_len = s->psk_server_callback(s, s->session->psk_identity, psk, sizeof(psk)); |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1848 | if (psk_len > PSK_MAX_PSK_LEN) |
| 1849 | { |
| 1850 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_INTERNAL_ERROR); |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1851 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1852 | goto f_err; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1853 | } |
| 1854 | else if (psk_len == 0) |
| 1855 | { |
| 1856 | /* PSK related to the given identity not found */ |
| 1857 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_PSK_IDENTITY_NOT_FOUND); |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1858 | al = SSL_AD_UNKNOWN_PSK_IDENTITY; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1859 | goto f_err; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1860 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1861 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1862 | |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1863 | /* Depending on the key exchange method, compute |premaster_secret| and |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 1864 | * |premaster_secret_len|. */ |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1865 | if (alg_k & SSL_kRSA) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1866 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1867 | CBS encrypted_premaster_secret; |
David Benjamin | 0aa0767 | 2014-07-24 15:34:14 -0400 | [diff] [blame] | 1868 | uint8_t rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH]; |
| 1869 | int decrypt_good_mask; |
| 1870 | uint8_t version_good; |
| 1871 | size_t rsa_size, decrypt_len, premaster_index, j; |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1872 | |
David Benjamin | b9cc33a | 2014-07-15 00:09:48 -0400 | [diff] [blame] | 1873 | pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey; |
| 1874 | if ( (pkey == NULL) || |
| 1875 | (pkey->type != EVP_PKEY_RSA) || |
| 1876 | (pkey->pkey.rsa == NULL)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1877 | { |
David Benjamin | b9cc33a | 2014-07-15 00:09:48 -0400 | [diff] [blame] | 1878 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1879 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_RSA_CERTIFICATE); |
| 1880 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1881 | } |
David Benjamin | b9cc33a | 2014-07-15 00:09:48 -0400 | [diff] [blame] | 1882 | rsa=pkey->pkey.rsa; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1883 | |
David Benjamin | cc23df5 | 2014-08-03 13:37:47 -0400 | [diff] [blame] | 1884 | /* TLS and [incidentally] DTLS{0xFEFF} */ |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1885 | if (s->version > SSL3_VERSION) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1886 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1887 | CBS copy = client_key_exchange; |
| 1888 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, |
| 1889 | &encrypted_premaster_secret) || |
| 1890 | CBS_len(&client_key_exchange) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1891 | { |
| 1892 | if (!(s->options & SSL_OP_TLS_D5_BUG)) |
| 1893 | { |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1894 | al = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1895 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1896 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1897 | } |
| 1898 | else |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1899 | encrypted_premaster_secret = copy; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1900 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1901 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1902 | else |
| 1903 | encrypted_premaster_secret = client_key_exchange; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1904 | |
David Benjamin | 0aa0767 | 2014-07-24 15:34:14 -0400 | [diff] [blame] | 1905 | /* Reject overly short RSA keys because we want to be sure that |
| 1906 | * the buffer size makes it safe to iterate over the entire size |
| 1907 | * of a premaster secret (SSL_MAX_MASTER_KEY_LENGTH). The actual |
| 1908 | * expected size is larger due to RSA padding, but the bound is |
| 1909 | * sufficient to be safe. */ |
| 1910 | rsa_size = RSA_size(rsa); |
| 1911 | if (rsa_size < SSL_MAX_MASTER_KEY_LENGTH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1912 | { |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1913 | al = SSL_AD_DECRYPT_ERROR; |
| 1914 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECRYPTION_FAILED); |
| 1915 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1916 | } |
| 1917 | |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1918 | /* We must not leak whether a decryption failure occurs because |
| 1919 | * of Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see |
| 1920 | * RFC 2246, section 7.4.7.1). The code follows that advice of |
| 1921 | * the TLS RFC and generates a random premaster secret for the |
| 1922 | * case that the decrypt fails. See |
| 1923 | * https://tools.ietf.org/html/rfc5246#section-7.4.7.1 */ |
| 1924 | if (RAND_pseudo_bytes(rand_premaster_secret, |
| 1925 | sizeof(rand_premaster_secret)) <= 0) |
| 1926 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1927 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1928 | /* Allocate a buffer large enough for an RSA decryption. */ |
David Benjamin | 0aa0767 | 2014-07-24 15:34:14 -0400 | [diff] [blame] | 1929 | decrypt_buf = OPENSSL_malloc(rsa_size); |
| 1930 | if (decrypt_buf == NULL) |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1931 | { |
| 1932 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 1933 | goto err; |
| 1934 | } |
| 1935 | |
David Benjamin | 0aa0767 | 2014-07-24 15:34:14 -0400 | [diff] [blame] | 1936 | /* Decrypt with no padding. PKCS#1 padding will be removed as |
| 1937 | * part of the timing-sensitive code below. */ |
| 1938 | if (!RSA_decrypt(rsa, &decrypt_len, decrypt_buf, rsa_size, |
| 1939 | CBS_data(&encrypted_premaster_secret), |
| 1940 | CBS_len(&encrypted_premaster_secret), |
| 1941 | RSA_NO_PADDING)) |
| 1942 | { |
| 1943 | goto err; |
| 1944 | } |
| 1945 | if (decrypt_len != rsa_size) |
| 1946 | { |
| 1947 | /* This should never happen, but do a check so we do not |
| 1948 | * read uninitialized memory. */ |
| 1949 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_INTERNAL_ERROR); |
| 1950 | goto err; |
| 1951 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1952 | |
David Benjamin | 0aa0767 | 2014-07-24 15:34:14 -0400 | [diff] [blame] | 1953 | /* Remove the PKCS#1 padding and adjust decrypt_len as |
| 1954 | * appropriate. decrypt_good_mask will be zero if the premaster |
| 1955 | * if good and non-zero otherwise. */ |
| 1956 | decrypt_good_mask = RSA_message_index_PKCS1_type_2( |
| 1957 | decrypt_buf, decrypt_len, &premaster_index); |
| 1958 | decrypt_good_mask--; |
| 1959 | decrypt_len = decrypt_len - premaster_index; |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1960 | |
David Benjamin | 0aa0767 | 2014-07-24 15:34:14 -0400 | [diff] [blame] | 1961 | /* decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. */ |
| 1962 | decrypt_good_mask |= decrypt_len ^ SSL_MAX_MASTER_KEY_LENGTH; |
| 1963 | |
| 1964 | /* Copy over the unpadded premaster. Whatever the value of |
| 1965 | * |decrypt_good_mask|, copy as if the premaster were the right |
| 1966 | * length. It is important the memory access pattern be |
| 1967 | * constant. */ |
| 1968 | premaster_secret = BUF_memdup( |
| 1969 | decrypt_buf + (rsa_size - SSL_MAX_MASTER_KEY_LENGTH), |
| 1970 | SSL_MAX_MASTER_KEY_LENGTH); |
| 1971 | if (premaster_secret == NULL) |
| 1972 | { |
| 1973 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 1974 | goto err; |
| 1975 | } |
| 1976 | OPENSSL_free(decrypt_buf); |
| 1977 | decrypt_buf = NULL; |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1978 | |
| 1979 | /* If the version in the decrypted pre-master secret is correct |
| 1980 | * then version_good will be zero. The Klima-Pokorny-Rosa |
| 1981 | * extension of Bleichenbacher's attack |
| 1982 | * (http://eprint.iacr.org/2003/052/) exploits the version |
| 1983 | * number check as a "bad version oracle". Thus version checks |
| 1984 | * are done in constant time and are treated like any other |
| 1985 | * decryption error. */ |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1986 | version_good = premaster_secret[0] ^ (s->client_version>>8); |
| 1987 | version_good |= premaster_secret[1] ^ (s->client_version&0xff); |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1988 | |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1989 | /* If any bits in version_good are set then they'll poision |
| 1990 | * decrypt_good_mask and cause rand_premaster_secret to be |
| 1991 | * used. */ |
| 1992 | decrypt_good_mask |= version_good; |
| 1993 | |
| 1994 | /* decrypt_good_mask will be zero iff decrypt_len == |
| 1995 | * SSL_MAX_MASTER_KEY_LENGTH and the version check passed. We |
| 1996 | * fold the bottom 32 bits of it with an OR so that the LSB |
| 1997 | * will be zero iff everything is good. This assumes that we'll |
| 1998 | * never decrypt a value > 2**31 bytes, which seems safe. */ |
| 1999 | decrypt_good_mask |= decrypt_good_mask >> 16; |
| 2000 | decrypt_good_mask |= decrypt_good_mask >> 8; |
| 2001 | decrypt_good_mask |= decrypt_good_mask >> 4; |
| 2002 | decrypt_good_mask |= decrypt_good_mask >> 2; |
| 2003 | decrypt_good_mask |= decrypt_good_mask >> 1; |
| 2004 | /* Now select only the LSB and subtract one. If decrypt_len == |
| 2005 | * SSL_MAX_MASTER_KEY_LENGTH and the version check passed then |
| 2006 | * decrypt_good_mask will be all ones. Otherwise it'll be all |
| 2007 | * zeros. */ |
| 2008 | decrypt_good_mask &= 1; |
| 2009 | decrypt_good_mask--; |
| 2010 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2011 | /* Now copy rand_premaster_secret over premaster_secret using |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2012 | * decrypt_good_mask. */ |
| 2013 | for (j = 0; j < sizeof(rand_premaster_secret); j++) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2014 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2015 | premaster_secret[j] = (premaster_secret[j] & decrypt_good_mask) | |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2016 | (rand_premaster_secret[j] & ~decrypt_good_mask); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2017 | } |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2018 | |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2019 | premaster_secret_len = sizeof(rand_premaster_secret); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2020 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2021 | #ifndef OPENSSL_NO_DH |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 2022 | else if (alg_k & SSL_kEDH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2023 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2024 | CBS dh_Yc; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2025 | int dh_len; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2026 | |
| 2027 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &dh_Yc) || |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2028 | CBS_len(&dh_Yc) == 0 || |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2029 | CBS_len(&client_key_exchange) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2030 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2031 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG); |
| 2032 | al = SSL_R_DECODE_ERROR; |
| 2033 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2034 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2035 | |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 2036 | if (s->s3->tmp.dh == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2037 | { |
| 2038 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2039 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_TMP_DH_KEY); |
| 2040 | goto f_err; |
| 2041 | } |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 2042 | dh_srvr=s->s3->tmp.dh; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2043 | |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2044 | pub = BN_bin2bn(CBS_data(&dh_Yc), CBS_len(&dh_Yc), NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2045 | if (pub == NULL) |
| 2046 | { |
| 2047 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_BN_LIB); |
| 2048 | goto err; |
| 2049 | } |
| 2050 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2051 | /* Allocate a buffer for the premaster secret. */ |
| 2052 | premaster_secret = OPENSSL_malloc(DH_size(dh_srvr)); |
| 2053 | if (premaster_secret == NULL) |
| 2054 | { |
| 2055 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2056 | goto err; |
| 2057 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2058 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2059 | dh_len = DH_compute_key(premaster_secret, pub, dh_srvr); |
| 2060 | if (dh_len <= 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2061 | { |
| 2062 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_DH_LIB); |
| 2063 | BN_clear_free(pub); |
| 2064 | goto err; |
| 2065 | } |
| 2066 | |
| 2067 | DH_free(s->s3->tmp.dh); |
| 2068 | s->s3->tmp.dh=NULL; |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2069 | BN_clear_free(pub); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2070 | pub=NULL; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2071 | |
| 2072 | premaster_secret_len = dh_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2073 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2074 | #endif |
| 2075 | |
| 2076 | #ifndef OPENSSL_NO_ECDH |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 2077 | else if (alg_k & SSL_kEECDH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2078 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2079 | int field_size = 0, ecdh_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2080 | const EC_KEY *tkey; |
| 2081 | const EC_GROUP *group; |
| 2082 | const BIGNUM *priv_key; |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2083 | CBS ecdh_Yc; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2084 | |
| 2085 | /* initialize structures for server's ECDH key pair */ |
| 2086 | if ((srvr_ecdh = EC_KEY_new()) == NULL) |
| 2087 | { |
| 2088 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2089 | goto err; |
| 2090 | } |
| 2091 | |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 2092 | /* Use the ephermeral values we saved when generating the |
| 2093 | * ServerKeyExchange msg. */ |
| 2094 | tkey = s->s3->tmp.ecdh; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2095 | |
| 2096 | group = EC_KEY_get0_group(tkey); |
| 2097 | priv_key = EC_KEY_get0_private_key(tkey); |
| 2098 | |
| 2099 | if (!EC_KEY_set_group(srvr_ecdh, group) || |
| 2100 | !EC_KEY_set_private_key(srvr_ecdh, priv_key)) |
| 2101 | { |
| 2102 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB); |
| 2103 | goto err; |
| 2104 | } |
| 2105 | |
| 2106 | /* Let's get client's public key */ |
| 2107 | if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) |
| 2108 | { |
| 2109 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2110 | goto err; |
| 2111 | } |
| 2112 | |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2113 | /* Get client's public key from encoded point |
| 2114 | * in the ClientKeyExchange message. |
| 2115 | */ |
| 2116 | if (!CBS_get_u8_length_prefixed(&client_key_exchange, &ecdh_Yc) || |
| 2117 | CBS_len(&client_key_exchange) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2118 | { |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2119 | al = SSL_AD_DECODE_ERROR; |
| 2120 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR); |
| 2121 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2122 | } |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2123 | |
| 2124 | if ((bn_ctx = BN_CTX_new()) == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2125 | { |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2126 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2127 | goto err; |
| 2128 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2129 | |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2130 | if (!EC_POINT_oct2point(group, clnt_ecpoint, |
| 2131 | CBS_data(&ecdh_Yc), CBS_len(&ecdh_Yc), bn_ctx)) |
| 2132 | { |
| 2133 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB); |
| 2134 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2135 | } |
| 2136 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2137 | /* Allocate a buffer for both the secret and the PSK. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2138 | field_size = EC_GROUP_get_degree(group); |
| 2139 | if (field_size <= 0) |
| 2140 | { |
| 2141 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB); |
| 2142 | goto err; |
| 2143 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2144 | |
| 2145 | ecdh_len = (field_size + 7) / 8; |
| 2146 | premaster_secret = OPENSSL_malloc(ecdh_len); |
| 2147 | if (premaster_secret == NULL) |
| 2148 | { |
| 2149 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2150 | goto err; |
| 2151 | } |
| 2152 | |
| 2153 | /* Compute the shared pre-master secret */ |
| 2154 | ecdh_len = ECDH_compute_key(premaster_secret, |
| 2155 | ecdh_len, clnt_ecpoint, srvr_ecdh, NULL); |
| 2156 | if (ecdh_len <= 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2157 | { |
| 2158 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB); |
| 2159 | goto err; |
| 2160 | } |
| 2161 | |
| 2162 | EVP_PKEY_free(clnt_pub_pkey); |
| 2163 | EC_POINT_free(clnt_ecpoint); |
| 2164 | EC_KEY_free(srvr_ecdh); |
| 2165 | BN_CTX_free(bn_ctx); |
| 2166 | EC_KEY_free(s->s3->tmp.ecdh); |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2167 | s->s3->tmp.ecdh = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2168 | |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2169 | premaster_secret_len = ecdh_len; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2170 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2171 | #endif |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2172 | else if (alg_k & SSL_kPSK) |
| 2173 | { |
| 2174 | /* For plain PSK, other_secret is a block of 0s with the same |
| 2175 | * length as the pre-shared key. */ |
| 2176 | premaster_secret_len = psk_len; |
| 2177 | premaster_secret = OPENSSL_malloc(premaster_secret_len); |
| 2178 | if (premaster_secret == NULL) |
| 2179 | { |
| 2180 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2181 | goto err; |
| 2182 | } |
| 2183 | memset(premaster_secret, 0, premaster_secret_len); |
| 2184 | } |
David Benjamin | 820c74a | 2014-07-13 04:22:14 -0400 | [diff] [blame] | 2185 | else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2186 | { |
| 2187 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2188 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_UNKNOWN_CIPHER_TYPE); |
| 2189 | goto f_err; |
| 2190 | } |
| 2191 | |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2192 | /* For a PSK cipher suite, the actual pre-master secret is combined with |
| 2193 | * the pre-shared key. */ |
| 2194 | if (alg_a & SSL_aPSK) |
| 2195 | { |
| 2196 | CBB new_premaster, child; |
| 2197 | uint8_t *new_data; |
| 2198 | size_t new_len; |
| 2199 | |
| 2200 | if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len)) |
| 2201 | { |
| 2202 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2203 | goto err; |
| 2204 | } |
| 2205 | if (!CBB_add_u16_length_prefixed(&new_premaster, &child) || |
| 2206 | !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) || |
| 2207 | !CBB_add_u16_length_prefixed(&new_premaster, &child) || |
| 2208 | !CBB_add_bytes(&child, psk, psk_len) || |
| 2209 | !CBB_finish(&new_premaster, &new_data, &new_len)) |
| 2210 | { |
| 2211 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_INTERNAL_ERROR); |
| 2212 | CBB_cleanup(&new_premaster); |
| 2213 | goto err; |
| 2214 | } |
| 2215 | |
| 2216 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 2217 | OPENSSL_free(premaster_secret); |
| 2218 | premaster_secret = new_data; |
| 2219 | premaster_secret_len = new_len; |
| 2220 | } |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2221 | |
| 2222 | /* Compute the master secret */ |
| 2223 | s->session->master_key_length = s->method->ssl3_enc |
| 2224 | ->generate_master_secret(s, |
| 2225 | s->session->master_key, premaster_secret, premaster_secret_len); |
| 2226 | |
| 2227 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 2228 | OPENSSL_free(premaster_secret); |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2229 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2230 | f_err: |
| 2231 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2232 | err: |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2233 | if (premaster_secret) |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2234 | { |
| 2235 | if (premaster_secret_len) |
| 2236 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2237 | OPENSSL_free(premaster_secret); |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2238 | } |
David Benjamin | 0aa0767 | 2014-07-24 15:34:14 -0400 | [diff] [blame] | 2239 | if (decrypt_buf) |
| 2240 | OPENSSL_free(decrypt_buf); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2241 | #ifndef OPENSSL_NO_ECDH |
| 2242 | EVP_PKEY_free(clnt_pub_pkey); |
| 2243 | EC_POINT_free(clnt_ecpoint); |
| 2244 | if (srvr_ecdh != NULL) |
| 2245 | EC_KEY_free(srvr_ecdh); |
| 2246 | BN_CTX_free(bn_ctx); |
| 2247 | #endif |
| 2248 | return(-1); |
| 2249 | } |
| 2250 | |
| 2251 | int ssl3_get_cert_verify(SSL *s) |
| 2252 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2253 | int al,ok,ret=0; |
| 2254 | long n; |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2255 | CBS certificate_verify, signature; |
David Benjamin | 6553b37 | 2014-07-22 14:11:30 -0400 | [diff] [blame] | 2256 | X509 *peer = s->session->peer; |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame^] | 2257 | EVP_PKEY *pkey = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2258 | const EVP_MD *md = NULL; |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame^] | 2259 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 2260 | size_t digest_length; |
| 2261 | EVP_PKEY_CTX *pctx = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2262 | |
David Benjamin | ef86550 | 2014-08-24 02:48:34 -0400 | [diff] [blame] | 2263 | /* Only RSA and ECDSA client certificates are supported, so a |
| 2264 | * CertificateVerify is required if and only if there's a |
| 2265 | * client certificate. */ |
| 2266 | if (peer == NULL) |
David Benjamin | 6553b37 | 2014-07-22 14:11:30 -0400 | [diff] [blame] | 2267 | { |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame^] | 2268 | if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) |
| 2269 | return -1; |
| 2270 | return 1; |
David Benjamin | 6553b37 | 2014-07-22 14:11:30 -0400 | [diff] [blame] | 2271 | } |
| 2272 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2273 | n=s->method->ssl_get_message(s, |
| 2274 | SSL3_ST_SR_CERT_VRFY_A, |
| 2275 | SSL3_ST_SR_CERT_VRFY_B, |
David Benjamin | 6553b37 | 2014-07-22 14:11:30 -0400 | [diff] [blame] | 2276 | SSL3_MT_CERTIFICATE_VERIFY, |
Adam Langley | 887b1c3 | 2014-07-24 18:00:06 -0700 | [diff] [blame] | 2277 | SSL3_RT_MAX_PLAIN_LENGTH, |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame^] | 2278 | SSL_GET_MESSAGE_DONT_HASH_MESSAGE, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2279 | &ok); |
| 2280 | |
David Benjamin | 734fca0 | 2014-08-05 13:06:20 -0400 | [diff] [blame] | 2281 | if (!ok) |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame^] | 2282 | return (int)n; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2283 | |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame^] | 2284 | /* Filter out unsupported certificate types. */ |
David Benjamin | ef86550 | 2014-08-24 02:48:34 -0400 | [diff] [blame] | 2285 | pkey = X509_get_pubkey(peer); |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame^] | 2286 | if (!(X509_certificate_type(peer, pkey) & EVP_PKT_SIGN) || |
| 2287 | (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_EC)) |
David Benjamin | ef86550 | 2014-08-24 02:48:34 -0400 | [diff] [blame] | 2288 | { |
David Benjamin | ef86550 | 2014-08-24 02:48:34 -0400 | [diff] [blame] | 2289 | al = SSL_AD_UNSUPPORTED_CERTIFICATE; |
| 2290 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE); |
| 2291 | goto f_err; |
| 2292 | } |
| 2293 | |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2294 | CBS_init(&certificate_verify, s->init_msg, n); |
| 2295 | |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame^] | 2296 | /* Determine the digest type if needbe. */ |
Adam Langley | 3213bed | 2014-06-23 12:26:07 -0700 | [diff] [blame] | 2297 | if (SSL_USE_SIGALGS(s)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2298 | { |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 2299 | if (!tls12_check_peer_sigalg(&md, &al, s, &certificate_verify, pkey)) |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2300 | goto f_err; |
Adam Langley | 3213bed | 2014-06-23 12:26:07 -0700 | [diff] [blame] | 2301 | } |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2302 | |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame^] | 2303 | /* Compute the digest. */ |
| 2304 | if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) |
| 2305 | goto err; |
| 2306 | |
| 2307 | /* The handshake buffer is no longer necessary, and we may hash the |
| 2308 | * current message.*/ |
| 2309 | if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) |
| 2310 | goto err; |
| 2311 | ssl3_hash_current_message(s); |
| 2312 | |
| 2313 | /* Parse and verify the signature. */ |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2314 | if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) || |
| 2315 | CBS_len(&certificate_verify) != 0) |
Adam Langley | 3213bed | 2014-06-23 12:26:07 -0700 | [diff] [blame] | 2316 | { |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2317 | al = SSL_AD_DECODE_ERROR; |
| 2318 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_DECODE_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2319 | goto f_err; |
| 2320 | } |
| 2321 | |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame^] | 2322 | pctx = EVP_PKEY_CTX_new(pkey, NULL); |
| 2323 | if (pctx == NULL) |
| 2324 | goto err; |
| 2325 | if (!EVP_PKEY_verify_init(pctx) || |
| 2326 | !EVP_PKEY_CTX_set_signature_md(pctx, md) || |
| 2327 | !EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature), |
| 2328 | digest, digest_length)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2329 | { |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame^] | 2330 | al = SSL_AD_DECRYPT_ERROR; |
| 2331 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_SIGNATURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2332 | goto f_err; |
| 2333 | } |
| 2334 | |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame^] | 2335 | ret = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2336 | if (0) |
| 2337 | { |
| 2338 | f_err: |
| 2339 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 2340 | } |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame^] | 2341 | err: |
| 2342 | EVP_PKEY_CTX_free(pctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2343 | EVP_PKEY_free(pkey); |
| 2344 | return(ret); |
| 2345 | } |
| 2346 | |
| 2347 | int ssl3_get_client_certificate(SSL *s) |
| 2348 | { |
| 2349 | int i,ok,al,ret= -1; |
| 2350 | X509 *x=NULL; |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2351 | unsigned long n; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2352 | STACK_OF(X509) *sk=NULL; |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2353 | SHA256_CTX sha256; |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2354 | CBS certificate_msg, certificate_list; |
| 2355 | int is_first_certificate = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2356 | |
| 2357 | n=s->method->ssl_get_message(s, |
| 2358 | SSL3_ST_SR_CERT_A, |
| 2359 | SSL3_ST_SR_CERT_B, |
| 2360 | -1, |
| 2361 | s->max_cert_list, |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 2362 | SSL_GET_MESSAGE_HASH_MESSAGE, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2363 | &ok); |
| 2364 | |
| 2365 | if (!ok) return((int)n); |
| 2366 | |
| 2367 | if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) |
| 2368 | { |
| 2369 | if ( (s->verify_mode & SSL_VERIFY_PEER) && |
| 2370 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) |
| 2371 | { |
| 2372 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
| 2373 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2374 | goto f_err; |
| 2375 | } |
| 2376 | /* If tls asked for a client cert, the client must return a 0 list */ |
| 2377 | if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) |
| 2378 | { |
| 2379 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST); |
| 2380 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 2381 | goto f_err; |
| 2382 | } |
| 2383 | s->s3->tmp.reuse_message=1; |
| 2384 | return(1); |
| 2385 | } |
| 2386 | |
| 2387 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) |
| 2388 | { |
| 2389 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 2390 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_WRONG_MESSAGE_TYPE); |
| 2391 | goto f_err; |
| 2392 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2393 | |
David Benjamin | 51b1f74 | 2014-07-12 16:31:12 -0400 | [diff] [blame] | 2394 | CBS_init(&certificate_msg, s->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2395 | |
| 2396 | if ((sk=sk_X509_new_null()) == NULL) |
| 2397 | { |
| 2398 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE); |
| 2399 | goto err; |
| 2400 | } |
| 2401 | |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2402 | if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) || |
| 2403 | CBS_len(&certificate_msg) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2404 | { |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2405 | al = SSL_AD_DECODE_ERROR; |
| 2406 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2407 | goto f_err; |
| 2408 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2409 | |
| 2410 | while (CBS_len(&certificate_list) > 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2411 | { |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2412 | CBS certificate; |
| 2413 | const uint8_t *data; |
| 2414 | |
| 2415 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2416 | { |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2417 | al = SSL_AD_DECODE_ERROR; |
| 2418 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2419 | goto f_err; |
| 2420 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2421 | if (is_first_certificate && s->ctx->retain_only_sha256_of_client_certs) |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2422 | { |
| 2423 | /* If this is the first certificate, and we don't want |
| 2424 | * to keep peer certificates in memory, then we hash it |
| 2425 | * right away. */ |
| 2426 | SHA256_Init(&sha256); |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2427 | SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate)); |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2428 | SHA256_Final(s->session->peer_sha256, &sha256); |
| 2429 | s->session->peer_sha256_valid = 1; |
| 2430 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2431 | is_first_certificate = 0; |
| 2432 | data = CBS_data(&certificate); |
| 2433 | x = d2i_X509(NULL, &data, CBS_len(&certificate)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2434 | if (x == NULL) |
| 2435 | { |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2436 | al = SSL_AD_BAD_CERTIFICATE; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2437 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_ASN1_LIB); |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2438 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2439 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2440 | if (!CBS_skip(&certificate, data - CBS_data(&certificate))) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2441 | { |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2442 | al = SSL_AD_INTERNAL_ERROR; |
| 2443 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_INTERNAL_ERROR); |
| 2444 | goto f_err; |
| 2445 | } |
| 2446 | if (CBS_len(&certificate) != 0) |
| 2447 | { |
| 2448 | al = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2449 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_CERT_LENGTH_MISMATCH); |
| 2450 | goto f_err; |
| 2451 | } |
| 2452 | if (!sk_X509_push(sk,x)) |
| 2453 | { |
| 2454 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE); |
| 2455 | goto err; |
| 2456 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2457 | x = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | if (sk_X509_num(sk) <= 0) |
| 2461 | { |
| 2462 | /* TLS does not mind 0 certs returned */ |
| 2463 | if (s->version == SSL3_VERSION) |
| 2464 | { |
| 2465 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2466 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_NO_CERTIFICATES_RETURNED); |
| 2467 | goto f_err; |
| 2468 | } |
| 2469 | /* Fail for TLS only if we required a certificate */ |
| 2470 | else if ((s->verify_mode & SSL_VERIFY_PEER) && |
| 2471 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) |
| 2472 | { |
| 2473 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
| 2474 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2475 | goto f_err; |
| 2476 | } |
| 2477 | /* No client certificate so digest cached records */ |
| 2478 | if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) |
| 2479 | { |
| 2480 | al=SSL_AD_INTERNAL_ERROR; |
| 2481 | goto f_err; |
| 2482 | } |
| 2483 | } |
| 2484 | else |
| 2485 | { |
| 2486 | i=ssl_verify_cert_chain(s,sk); |
| 2487 | if (i <= 0) |
| 2488 | { |
| 2489 | al=ssl_verify_alarm_type(s->verify_result); |
| 2490 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 2491 | goto f_err; |
| 2492 | } |
| 2493 | } |
| 2494 | |
| 2495 | if (s->session->peer != NULL) /* This should not be needed */ |
| 2496 | X509_free(s->session->peer); |
| 2497 | s->session->peer=sk_X509_shift(sk); |
| 2498 | s->session->verify_result = s->verify_result; |
| 2499 | |
| 2500 | /* With the current implementation, sess_cert will always be NULL |
| 2501 | * when we arrive here. */ |
| 2502 | if (s->session->sess_cert == NULL) |
| 2503 | { |
| 2504 | s->session->sess_cert = ssl_sess_cert_new(); |
| 2505 | if (s->session->sess_cert == NULL) |
| 2506 | { |
| 2507 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE); |
| 2508 | goto err; |
| 2509 | } |
| 2510 | } |
| 2511 | if (s->session->sess_cert->cert_chain != NULL) |
| 2512 | sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free); |
| 2513 | s->session->sess_cert->cert_chain=sk; |
| 2514 | /* Inconsistency alert: cert_chain does *not* include the |
| 2515 | * peer's own certificate, while we do include it in s3_clnt.c */ |
| 2516 | |
| 2517 | sk=NULL; |
| 2518 | |
| 2519 | ret=1; |
| 2520 | if (0) |
| 2521 | { |
| 2522 | f_err: |
| 2523 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 2524 | } |
| 2525 | err: |
| 2526 | if (x != NULL) X509_free(x); |
| 2527 | if (sk != NULL) sk_X509_pop_free(sk,X509_free); |
| 2528 | return(ret); |
| 2529 | } |
| 2530 | |
| 2531 | int ssl3_send_server_certificate(SSL *s) |
| 2532 | { |
| 2533 | CERT_PKEY *cpk; |
| 2534 | |
| 2535 | if (s->state == SSL3_ST_SW_CERT_A) |
| 2536 | { |
| 2537 | cpk=ssl_get_server_send_pkey(s); |
| 2538 | if (cpk == NULL) |
| 2539 | { |
David Benjamin | d26aea6 | 2014-07-12 00:13:56 -0400 | [diff] [blame] | 2540 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_certificate, ERR_R_INTERNAL_ERROR); |
| 2541 | return(0); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2542 | } |
| 2543 | |
| 2544 | ssl3_output_cert_chain(s,cpk); |
| 2545 | s->state=SSL3_ST_SW_CERT_B; |
| 2546 | } |
| 2547 | |
| 2548 | /* SSL3_ST_SW_CERT_B */ |
| 2549 | return ssl_do_write(s); |
| 2550 | } |
| 2551 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2552 | /* send a new session ticket (not necessarily for a new session) */ |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 2553 | int ssl3_send_new_session_ticket(SSL *s) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2554 | { |
| 2555 | if (s->state == SSL3_ST_SW_SESSION_TICKET_A) |
| 2556 | { |
| 2557 | unsigned char *p, *senc, *macstart; |
| 2558 | const unsigned char *const_p; |
| 2559 | int len, slen_full, slen; |
| 2560 | SSL_SESSION *sess; |
| 2561 | unsigned int hlen; |
| 2562 | EVP_CIPHER_CTX ctx; |
| 2563 | HMAC_CTX hctx; |
| 2564 | SSL_CTX *tctx = s->initial_ctx; |
| 2565 | unsigned char iv[EVP_MAX_IV_LENGTH]; |
| 2566 | unsigned char key_name[16]; |
| 2567 | |
| 2568 | /* get session encoding length */ |
| 2569 | slen_full = i2d_SSL_SESSION(s->session, NULL); |
| 2570 | /* Some length values are 16 bits, so forget it if session is |
| 2571 | * too long |
| 2572 | */ |
| 2573 | if (slen_full > 0xFF00) |
| 2574 | return -1; |
| 2575 | senc = OPENSSL_malloc(slen_full); |
| 2576 | if (!senc) |
| 2577 | return -1; |
| 2578 | p = senc; |
| 2579 | i2d_SSL_SESSION(s->session, &p); |
| 2580 | |
| 2581 | /* create a fresh copy (not shared with other threads) to clean up */ |
| 2582 | const_p = senc; |
| 2583 | sess = d2i_SSL_SESSION(NULL, &const_p, slen_full); |
| 2584 | if (sess == NULL) |
| 2585 | { |
| 2586 | OPENSSL_free(senc); |
| 2587 | return -1; |
| 2588 | } |
| 2589 | sess->session_id_length = 0; /* ID is irrelevant for the ticket */ |
| 2590 | |
| 2591 | slen = i2d_SSL_SESSION(sess, NULL); |
| 2592 | if (slen > slen_full) /* shouldn't ever happen */ |
| 2593 | { |
| 2594 | OPENSSL_free(senc); |
| 2595 | return -1; |
| 2596 | } |
| 2597 | p = senc; |
| 2598 | i2d_SSL_SESSION(sess, &p); |
| 2599 | SSL_SESSION_free(sess); |
| 2600 | |
| 2601 | /* Grow buffer if need be: the length calculation is as |
| 2602 | * follows handshake_header_length + |
| 2603 | * 4 (ticket lifetime hint) + 2 (ticket length) + |
| 2604 | * 16 (key name) + max_iv_len (iv length) + |
| 2605 | * session_length + max_enc_block_size (max encrypted session |
| 2606 | * length) + max_md_size (HMAC). |
| 2607 | */ |
| 2608 | if (!BUF_MEM_grow(s->init_buf, |
| 2609 | SSL_HM_HEADER_LENGTH(s) + 22 + EVP_MAX_IV_LENGTH + |
| 2610 | EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen)) |
| 2611 | return -1; |
| 2612 | p = ssl_handshake_start(s); |
| 2613 | EVP_CIPHER_CTX_init(&ctx); |
| 2614 | HMAC_CTX_init(&hctx); |
| 2615 | /* Initialize HMAC and cipher contexts. If callback present |
| 2616 | * it does all the work otherwise use generated values |
| 2617 | * from parent ctx. |
| 2618 | */ |
| 2619 | if (tctx->tlsext_ticket_key_cb) |
| 2620 | { |
| 2621 | if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, |
| 2622 | &hctx, 1) < 0) |
| 2623 | { |
| 2624 | OPENSSL_free(senc); |
| 2625 | return -1; |
| 2626 | } |
| 2627 | } |
| 2628 | else |
| 2629 | { |
| 2630 | RAND_pseudo_bytes(iv, 16); |
| 2631 | EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, |
| 2632 | tctx->tlsext_tick_aes_key, iv); |
| 2633 | HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, |
| 2634 | tlsext_tick_md(), NULL); |
| 2635 | memcpy(key_name, tctx->tlsext_tick_key_name, 16); |
| 2636 | } |
| 2637 | |
| 2638 | /* Ticket lifetime hint (advisory only): |
| 2639 | * We leave this unspecified for resumed session (for simplicity), |
| 2640 | * and guess that tickets for new sessions will live as long |
| 2641 | * as their sessions. */ |
| 2642 | l2n(s->hit ? 0 : s->session->timeout, p); |
| 2643 | |
| 2644 | /* Skip ticket length for now */ |
| 2645 | p += 2; |
| 2646 | /* Output key name */ |
| 2647 | macstart = p; |
| 2648 | memcpy(p, key_name, 16); |
| 2649 | p += 16; |
| 2650 | /* output IV */ |
| 2651 | memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx)); |
| 2652 | p += EVP_CIPHER_CTX_iv_length(&ctx); |
| 2653 | /* Encrypt session data */ |
| 2654 | EVP_EncryptUpdate(&ctx, p, &len, senc, slen); |
| 2655 | p += len; |
| 2656 | EVP_EncryptFinal_ex(&ctx, p, &len); |
| 2657 | p += len; |
| 2658 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2659 | |
| 2660 | HMAC_Update(&hctx, macstart, p - macstart); |
| 2661 | HMAC_Final(&hctx, p, &hlen); |
| 2662 | HMAC_CTX_cleanup(&hctx); |
| 2663 | |
| 2664 | p += hlen; |
| 2665 | /* Now write out lengths: p points to end of data written */ |
| 2666 | /* Total length */ |
| 2667 | len = p - ssl_handshake_start(s); |
| 2668 | ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len); |
| 2669 | /* Skip ticket lifetime hint */ |
| 2670 | p = ssl_handshake_start(s) + 4; |
| 2671 | s2n(len - 6, p); |
| 2672 | s->state=SSL3_ST_SW_SESSION_TICKET_B; |
| 2673 | OPENSSL_free(senc); |
| 2674 | } |
| 2675 | |
| 2676 | /* SSL3_ST_SW_SESSION_TICKET_B */ |
| 2677 | return ssl_do_write(s); |
| 2678 | } |
| 2679 | |
| 2680 | int ssl3_send_cert_status(SSL *s) |
| 2681 | { |
| 2682 | if (s->state == SSL3_ST_SW_CERT_STATUS_A) |
| 2683 | { |
| 2684 | unsigned char *p; |
| 2685 | /* Grow buffer if need be: the length calculation is as |
| 2686 | * follows 1 (message type) + 3 (message length) + |
| 2687 | * 1 (ocsp response type) + 3 (ocsp response length) |
| 2688 | * + (ocsp response) |
| 2689 | */ |
| 2690 | if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen)) |
| 2691 | return -1; |
| 2692 | |
| 2693 | p=(unsigned char *)s->init_buf->data; |
| 2694 | |
| 2695 | /* do the header */ |
| 2696 | *(p++)=SSL3_MT_CERTIFICATE_STATUS; |
| 2697 | /* message length */ |
| 2698 | l2n3(s->tlsext_ocsp_resplen + 4, p); |
| 2699 | /* status type */ |
| 2700 | *(p++)= s->tlsext_status_type; |
| 2701 | /* length of OCSP response */ |
| 2702 | l2n3(s->tlsext_ocsp_resplen, p); |
| 2703 | /* actual response */ |
| 2704 | memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen); |
| 2705 | /* number of bytes to write */ |
| 2706 | s->init_num = 8 + s->tlsext_ocsp_resplen; |
| 2707 | s->state=SSL3_ST_SW_CERT_STATUS_B; |
| 2708 | s->init_off = 0; |
| 2709 | } |
| 2710 | |
| 2711 | /* SSL3_ST_SW_CERT_STATUS_B */ |
| 2712 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 2713 | } |
| 2714 | |
| 2715 | # ifndef OPENSSL_NO_NEXTPROTONEG |
| 2716 | /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It |
| 2717 | * sets the next_proto member in s if found */ |
| 2718 | int ssl3_get_next_proto(SSL *s) |
| 2719 | { |
| 2720 | int ok; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2721 | long n; |
David Benjamin | ced551f | 2014-07-11 23:16:16 -0400 | [diff] [blame] | 2722 | CBS next_protocol, selected_protocol, padding; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2723 | |
| 2724 | /* Clients cannot send a NextProtocol message if we didn't see the |
| 2725 | * extension in their ClientHello */ |
| 2726 | if (!s->s3->next_proto_neg_seen) |
| 2727 | { |
| 2728 | OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto, SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION); |
| 2729 | return -1; |
| 2730 | } |
| 2731 | |
| 2732 | n=s->method->ssl_get_message(s, |
| 2733 | SSL3_ST_SR_NEXT_PROTO_A, |
| 2734 | SSL3_ST_SR_NEXT_PROTO_B, |
| 2735 | SSL3_MT_NEXT_PROTO, |
| 2736 | 514, /* See the payload format below */ |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 2737 | SSL_GET_MESSAGE_HASH_MESSAGE, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2738 | &ok); |
| 2739 | |
| 2740 | if (!ok) |
| 2741 | return((int)n); |
| 2742 | |
| 2743 | /* s->state doesn't reflect whether ChangeCipherSpec has been received |
| 2744 | * in this handshake, but s->s3->change_cipher_spec does (will be reset |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 2745 | * by ssl3_get_finished). |
| 2746 | * TODO(davidben): Is this check now redundant with |
| 2747 | * SSL3_FLAGS_EXPECT_CCS? */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2748 | if (!s->s3->change_cipher_spec) |
| 2749 | { |
| 2750 | OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto, SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS); |
| 2751 | return -1; |
| 2752 | } |
| 2753 | |
David Benjamin | 51b1f74 | 2014-07-12 16:31:12 -0400 | [diff] [blame] | 2754 | CBS_init(&next_protocol, s->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2755 | |
| 2756 | /* The payload looks like: |
| 2757 | * uint8 proto_len; |
| 2758 | * uint8 proto[proto_len]; |
| 2759 | * uint8 padding_len; |
| 2760 | * uint8 padding[padding_len]; |
| 2761 | */ |
David Benjamin | ced551f | 2014-07-11 23:16:16 -0400 | [diff] [blame] | 2762 | if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) || |
| 2763 | !CBS_get_u8_length_prefixed(&next_protocol, &padding) || |
| 2764 | CBS_len(&next_protocol) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2765 | return 0; |
| 2766 | |
David Benjamin | ced551f | 2014-07-11 23:16:16 -0400 | [diff] [blame] | 2767 | if (!CBS_stow(&selected_protocol, |
| 2768 | &s->next_proto_negotiated, |
| 2769 | &s->next_proto_negotiated_len)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2770 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2771 | |
| 2772 | return 1; |
| 2773 | } |
| 2774 | # endif |
| 2775 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2776 | /* ssl3_get_channel_id reads and verifies a ClientID handshake message. */ |
| 2777 | int ssl3_get_channel_id(SSL *s) |
| 2778 | { |
| 2779 | int ret = -1, ok; |
| 2780 | long n; |
David Benjamin | 880b14e | 2014-08-25 22:35:07 -0400 | [diff] [blame] | 2781 | EVP_MD_CTX md_ctx; |
| 2782 | uint8_t channel_id_hash[SHA256_DIGEST_LENGTH]; |
| 2783 | unsigned int channel_id_hash_len; |
David Benjamin | 9a6232f | 2014-07-11 23:07:47 -0400 | [diff] [blame] | 2784 | const uint8_t *p; |
| 2785 | uint16_t extension_type, expected_extension_type; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2786 | EC_GROUP* p256 = NULL; |
| 2787 | EC_KEY* key = NULL; |
| 2788 | EC_POINT* point = NULL; |
| 2789 | ECDSA_SIG sig; |
| 2790 | BIGNUM x, y; |
David Benjamin | 9a6232f | 2014-07-11 23:07:47 -0400 | [diff] [blame] | 2791 | CBS encrypted_extensions, extension; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2792 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2793 | n = s->method->ssl_get_message(s, |
| 2794 | SSL3_ST_SR_CHANNEL_ID_A, |
| 2795 | SSL3_ST_SR_CHANNEL_ID_B, |
| 2796 | SSL3_MT_ENCRYPTED_EXTENSIONS, |
| 2797 | 2 + 2 + TLSEXT_CHANNEL_ID_SIZE, |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 2798 | SSL_GET_MESSAGE_DONT_HASH_MESSAGE, |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2799 | &ok); |
| 2800 | |
| 2801 | if (!ok) |
| 2802 | return((int)n); |
| 2803 | |
David Benjamin | 880b14e | 2014-08-25 22:35:07 -0400 | [diff] [blame] | 2804 | /* Before incorporating the EncryptedExtensions message to the |
| 2805 | * handshake hash, compute the hash that should have been signed. */ |
| 2806 | channel_id_hash_len = sizeof(channel_id_hash); |
| 2807 | EVP_MD_CTX_init(&md_ctx); |
| 2808 | if (!EVP_DigestInit_ex(&md_ctx, EVP_sha256(), NULL) || |
| 2809 | !tls1_channel_id_hash(&md_ctx, s) || |
| 2810 | !EVP_DigestFinal(&md_ctx, channel_id_hash, &channel_id_hash_len)) |
| 2811 | { |
| 2812 | EVP_MD_CTX_cleanup(&md_ctx); |
| 2813 | return -1; |
| 2814 | } |
| 2815 | EVP_MD_CTX_cleanup(&md_ctx); |
| 2816 | assert(channel_id_hash_len == SHA256_DIGEST_LENGTH); |
| 2817 | |
| 2818 | ssl3_hash_current_message(s); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2819 | |
| 2820 | /* s->state doesn't reflect whether ChangeCipherSpec has been received |
| 2821 | * in this handshake, but s->s3->change_cipher_spec does (will be reset |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 2822 | * by ssl3_get_finished). |
| 2823 | * TODO(davidben): Is this check now redundant with |
| 2824 | * SSL3_FLAGS_EXPECT_CCS? */ |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2825 | if (!s->s3->change_cipher_spec) |
| 2826 | { |
| 2827 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_GOT_CHANNEL_ID_BEFORE_A_CCS); |
| 2828 | return -1; |
| 2829 | } |
| 2830 | |
David Benjamin | 51b1f74 | 2014-07-12 16:31:12 -0400 | [diff] [blame] | 2831 | CBS_init(&encrypted_extensions, s->init_msg, n); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2832 | |
David Benjamin | 9a6232f | 2014-07-11 23:07:47 -0400 | [diff] [blame] | 2833 | /* EncryptedExtensions could include multiple extensions, but |
| 2834 | * the only extension that could be negotiated is ChannelID, |
| 2835 | * so there can only be one entry. |
| 2836 | * |
| 2837 | * The payload looks like: |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2838 | * uint16 extension_type |
| 2839 | * uint16 extension_len; |
| 2840 | * uint8 x[32]; |
| 2841 | * uint8 y[32]; |
| 2842 | * uint8 r[32]; |
| 2843 | * uint8 s[32]; |
| 2844 | */ |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2845 | expected_extension_type = TLSEXT_TYPE_channel_id; |
| 2846 | if (s->s3->tlsext_channel_id_new) |
| 2847 | expected_extension_type = TLSEXT_TYPE_channel_id_new; |
| 2848 | |
David Benjamin | 9a6232f | 2014-07-11 23:07:47 -0400 | [diff] [blame] | 2849 | if (!CBS_get_u16(&encrypted_extensions, &extension_type) || |
| 2850 | !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) || |
| 2851 | CBS_len(&encrypted_extensions) != 0 || |
| 2852 | extension_type != expected_extension_type || |
| 2853 | CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2854 | { |
| 2855 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_INVALID_MESSAGE); |
| 2856 | return -1; |
| 2857 | } |
| 2858 | |
| 2859 | p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); |
| 2860 | if (!p256) |
| 2861 | { |
| 2862 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_NO_P256_SUPPORT); |
| 2863 | return -1; |
| 2864 | } |
| 2865 | |
| 2866 | BN_init(&x); |
| 2867 | BN_init(&y); |
| 2868 | sig.r = BN_new(); |
| 2869 | sig.s = BN_new(); |
| 2870 | |
David Benjamin | 9a6232f | 2014-07-11 23:07:47 -0400 | [diff] [blame] | 2871 | p = CBS_data(&extension); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2872 | if (BN_bin2bn(p + 0, 32, &x) == NULL || |
| 2873 | BN_bin2bn(p + 32, 32, &y) == NULL || |
| 2874 | BN_bin2bn(p + 64, 32, sig.r) == NULL || |
| 2875 | BN_bin2bn(p + 96, 32, sig.s) == NULL) |
| 2876 | goto err; |
| 2877 | |
| 2878 | point = EC_POINT_new(p256); |
| 2879 | if (!point || |
| 2880 | !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) |
| 2881 | goto err; |
| 2882 | |
| 2883 | key = EC_KEY_new(); |
| 2884 | if (!key || |
| 2885 | !EC_KEY_set_group(key, p256) || |
| 2886 | !EC_KEY_set_public_key(key, point)) |
| 2887 | goto err; |
| 2888 | |
| 2889 | /* We stored the handshake hash in |tlsext_channel_id| the first time |
| 2890 | * that we were called. */ |
David Benjamin | 880b14e | 2014-08-25 22:35:07 -0400 | [diff] [blame] | 2891 | if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) |
David Benjamin | 3a66e28 | 2014-08-24 02:22:29 -0400 | [diff] [blame] | 2892 | { |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2893 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_CHANNEL_ID_SIGNATURE_INVALID); |
| 2894 | s->s3->tlsext_channel_id_valid = 0; |
| 2895 | goto err; |
David Benjamin | 3a66e28 | 2014-08-24 02:22:29 -0400 | [diff] [blame] | 2896 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2897 | |
| 2898 | memcpy(s->s3->tlsext_channel_id, p, 64); |
| 2899 | ret = 1; |
| 2900 | |
| 2901 | err: |
| 2902 | BN_free(&x); |
| 2903 | BN_free(&y); |
| 2904 | BN_free(sig.r); |
| 2905 | BN_free(sig.s); |
| 2906 | if (key) |
| 2907 | EC_KEY_free(key); |
| 2908 | if (point) |
| 2909 | EC_POINT_free(point); |
| 2910 | if (p256) |
| 2911 | EC_GROUP_free(p256); |
| 2912 | return ret; |
| 2913 | } |
| 2914 | |