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; |
| 489 | |
| 490 | /* TODO(davidben): These two blocks are different |
| 491 | * between SSL and DTLS. Resolve the difference and code |
| 492 | * duplication. */ |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 493 | if (SSL_USE_SIGALGS(s)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 494 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 495 | if (!s->session->peer) |
| 496 | break; |
| 497 | /* For sigalgs freeze the handshake buffer |
| 498 | * at this point and digest cached records. |
| 499 | */ |
| 500 | if (!s->s3->handshake_buffer) |
| 501 | { |
| 502 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR); |
| 503 | return -1; |
| 504 | } |
| 505 | s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE; |
| 506 | if (!ssl3_digest_cached_records(s)) |
| 507 | return -1; |
| 508 | } |
| 509 | else |
| 510 | { |
| 511 | int offset=0; |
| 512 | int dgst_num; |
| 513 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 514 | /* We need to get hashes here so if there is |
| 515 | * a client cert, it can be verified |
| 516 | * FIXME - digest processing for CertificateVerify |
| 517 | * should be generalized. But it is next step |
| 518 | */ |
| 519 | if (s->s3->handshake_buffer) |
| 520 | if (!ssl3_digest_cached_records(s)) |
| 521 | return -1; |
| 522 | for (dgst_num=0; dgst_num<SSL_MAX_DIGEST;dgst_num++) |
| 523 | if (s->s3->handshake_dgst[dgst_num]) |
| 524 | { |
| 525 | int dgst_size; |
| 526 | |
| 527 | s->method->ssl3_enc->cert_verify_mac(s,EVP_MD_CTX_type(s->s3->handshake_dgst[dgst_num]),&(s->s3->tmp.cert_verify_md[offset])); |
| 528 | dgst_size=EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]); |
| 529 | if (dgst_size < 0) |
| 530 | { |
| 531 | ret = -1; |
| 532 | goto end; |
| 533 | } |
| 534 | offset+=dgst_size; |
| 535 | } |
| 536 | } |
| 537 | break; |
| 538 | |
| 539 | case SSL3_ST_SR_CERT_VRFY_A: |
| 540 | case SSL3_ST_SR_CERT_VRFY_B: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 541 | ret=ssl3_get_cert_verify(s); |
| 542 | if (ret <= 0) goto end; |
| 543 | |
David Benjamin | 84ec49e | 2014-07-22 14:25:28 -0400 | [diff] [blame] | 544 | s->state = SSL3_ST_SR_CHANGE; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 545 | s->init_num=0; |
| 546 | break; |
| 547 | |
David Benjamin | 84ec49e | 2014-07-22 14:25:28 -0400 | [diff] [blame] | 548 | case SSL3_ST_SR_CHANGE: { |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 549 | char next_proto_neg = 0; |
| 550 | char channel_id = 0; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 551 | # if !defined(OPENSSL_NO_NEXTPROTONEG) |
| 552 | next_proto_neg = s->s3->next_proto_neg_seen; |
| 553 | # endif |
| 554 | channel_id = s->s3->tlsext_channel_id_valid; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 555 | |
David Benjamin | 84ec49e | 2014-07-22 14:25:28 -0400 | [diff] [blame] | 556 | /* At this point, the next message must be entirely |
| 557 | * behind a ChangeCipherSpec. */ |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 558 | if (!ssl3_expect_change_cipher_spec(s)) |
| 559 | { |
| 560 | ret = -1; |
| 561 | goto end; |
| 562 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 563 | if (next_proto_neg) |
David Benjamin | 84ec49e | 2014-07-22 14:25:28 -0400 | [diff] [blame] | 564 | s->state = SSL3_ST_SR_NEXT_PROTO_A; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 565 | else if (channel_id) |
David Benjamin | 84ec49e | 2014-07-22 14:25:28 -0400 | [diff] [blame] | 566 | s->state = SSL3_ST_SR_CHANNEL_ID_A; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 567 | else |
David Benjamin | 84ec49e | 2014-07-22 14:25:28 -0400 | [diff] [blame] | 568 | s->state = SSL3_ST_SR_FINISHED_A; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 569 | break; |
| 570 | } |
| 571 | |
David Benjamin | 6dbd73d | 2014-07-03 15:59:49 -0400 | [diff] [blame] | 572 | #if !defined(OPENSSL_NO_NEXTPROTONEG) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 573 | case SSL3_ST_SR_NEXT_PROTO_A: |
| 574 | case SSL3_ST_SR_NEXT_PROTO_B: |
| 575 | ret=ssl3_get_next_proto(s); |
| 576 | if (ret <= 0) goto end; |
| 577 | s->init_num = 0; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 578 | if (s->s3->tlsext_channel_id_valid) |
| 579 | s->state=SSL3_ST_SR_CHANNEL_ID_A; |
| 580 | else |
| 581 | s->state=SSL3_ST_SR_FINISHED_A; |
| 582 | break; |
| 583 | #endif |
| 584 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 585 | case SSL3_ST_SR_CHANNEL_ID_A: |
| 586 | case SSL3_ST_SR_CHANNEL_ID_B: |
| 587 | ret=ssl3_get_channel_id(s); |
| 588 | if (ret <= 0) goto end; |
| 589 | s->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 590 | s->state=SSL3_ST_SR_FINISHED_A; |
| 591 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 592 | |
| 593 | case SSL3_ST_SR_FINISHED_A: |
| 594 | case SSL3_ST_SR_FINISHED_B: |
| 595 | ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A, |
| 596 | SSL3_ST_SR_FINISHED_B); |
| 597 | if (ret <= 0) goto end; |
| 598 | if (s->hit) |
| 599 | s->state=SSL_ST_OK; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 600 | else if (s->tlsext_ticket_expected) |
| 601 | s->state=SSL3_ST_SW_SESSION_TICKET_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 602 | else |
| 603 | s->state=SSL3_ST_SW_CHANGE_A; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 604 | /* If this is a full handshake with ChannelID then |
| 605 | * record the hashshake hashes in |s->session| in case |
| 606 | * we need them to verify a ChannelID signature on a |
| 607 | * resumption of this session in the future. */ |
| 608 | if (!s->hit && s->s3->tlsext_channel_id_new) |
| 609 | { |
| 610 | ret = tls1_record_handshake_hashes_for_channel_id(s); |
| 611 | if (ret <= 0) goto end; |
| 612 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 613 | s->init_num=0; |
| 614 | break; |
| 615 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 616 | case SSL3_ST_SW_SESSION_TICKET_A: |
| 617 | case SSL3_ST_SW_SESSION_TICKET_B: |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 618 | ret=ssl3_send_new_session_ticket(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 619 | if (ret <= 0) goto end; |
| 620 | s->state=SSL3_ST_SW_CHANGE_A; |
| 621 | s->init_num=0; |
| 622 | break; |
| 623 | |
| 624 | case SSL3_ST_SW_CERT_STATUS_A: |
| 625 | case SSL3_ST_SW_CERT_STATUS_B: |
| 626 | ret=ssl3_send_cert_status(s); |
| 627 | if (ret <= 0) goto end; |
| 628 | s->state=SSL3_ST_SW_KEY_EXCH_A; |
| 629 | s->init_num=0; |
| 630 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 631 | |
| 632 | case SSL3_ST_SW_CHANGE_A: |
| 633 | case SSL3_ST_SW_CHANGE_B: |
| 634 | |
| 635 | s->session->cipher=s->s3->tmp.new_cipher; |
| 636 | if (!s->method->ssl3_enc->setup_key_block(s)) |
| 637 | { ret= -1; goto end; } |
| 638 | |
| 639 | ret=ssl3_send_change_cipher_spec(s, |
| 640 | SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B); |
| 641 | |
| 642 | if (ret <= 0) goto end; |
| 643 | s->state=SSL3_ST_SW_FINISHED_A; |
| 644 | s->init_num=0; |
| 645 | |
| 646 | if (!s->method->ssl3_enc->change_cipher_state(s, |
| 647 | SSL3_CHANGE_CIPHER_SERVER_WRITE)) |
| 648 | { |
| 649 | ret= -1; |
| 650 | goto end; |
| 651 | } |
| 652 | |
| 653 | break; |
| 654 | |
| 655 | case SSL3_ST_SW_FINISHED_A: |
| 656 | case SSL3_ST_SW_FINISHED_B: |
| 657 | ret=ssl3_send_finished(s, |
| 658 | SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B, |
| 659 | s->method->ssl3_enc->server_finished_label, |
| 660 | s->method->ssl3_enc->server_finished_label_len); |
| 661 | if (ret <= 0) goto end; |
David Benjamin | 84ec49e | 2014-07-22 14:25:28 -0400 | [diff] [blame] | 662 | s->state = SSL3_ST_SW_FLUSH; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 663 | if (s->hit) |
David Benjamin | 84ec49e | 2014-07-22 14:25:28 -0400 | [diff] [blame] | 664 | s->s3->tmp.next_state = SSL3_ST_SR_CHANGE; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 665 | else |
David Benjamin | 84ec49e | 2014-07-22 14:25:28 -0400 | [diff] [blame] | 666 | s->s3->tmp.next_state = SSL_ST_OK; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 667 | s->init_num=0; |
| 668 | break; |
| 669 | |
| 670 | case SSL_ST_OK: |
| 671 | /* clean a few things up */ |
| 672 | ssl3_cleanup_key_block(s); |
| 673 | |
| 674 | BUF_MEM_free(s->init_buf); |
| 675 | s->init_buf=NULL; |
| 676 | |
| 677 | /* remove buffering on output */ |
| 678 | ssl_free_wbio_buffer(s); |
| 679 | |
| 680 | s->init_num=0; |
| 681 | |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 682 | /* If we aren't retaining peer certificates then we can |
| 683 | * discard it now. */ |
| 684 | if (s->session->peer && s->ctx->retain_only_sha256_of_client_certs) |
| 685 | { |
| 686 | X509_free(s->session->peer); |
| 687 | s->session->peer = NULL; |
| 688 | } |
| 689 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 690 | if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */ |
| 691 | { |
| 692 | s->renegotiate=0; |
| 693 | s->new_session=0; |
| 694 | |
| 695 | ssl_update_cache(s,SSL_SESS_CACHE_SERVER); |
| 696 | |
| 697 | s->ctx->stats.sess_accept_good++; |
| 698 | /* s->server=1; */ |
| 699 | s->handshake_func=ssl3_accept; |
| 700 | |
| 701 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); |
| 702 | } |
| 703 | |
| 704 | ret = 1; |
| 705 | goto end; |
| 706 | /* break; */ |
| 707 | |
| 708 | default: |
| 709 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_UNKNOWN_STATE); |
| 710 | ret= -1; |
| 711 | goto end; |
| 712 | /* break; */ |
| 713 | } |
| 714 | |
| 715 | if (!s->s3->tmp.reuse_message && !skip) |
| 716 | { |
| 717 | if (s->debug) |
| 718 | { |
| 719 | if ((ret=BIO_flush(s->wbio)) <= 0) |
| 720 | goto end; |
| 721 | } |
| 722 | |
| 723 | |
| 724 | if ((cb != NULL) && (s->state != state)) |
| 725 | { |
| 726 | new_state=s->state; |
| 727 | s->state=state; |
| 728 | cb(s,SSL_CB_ACCEPT_LOOP,1); |
| 729 | s->state=new_state; |
| 730 | } |
| 731 | } |
| 732 | skip=0; |
| 733 | } |
| 734 | end: |
| 735 | /* BIO_flush(s->wbio); */ |
| 736 | |
| 737 | s->in_handshake--; |
| 738 | if (cb != NULL) |
| 739 | cb(s,SSL_CB_ACCEPT_EXIT,ret); |
| 740 | return(ret); |
| 741 | } |
| 742 | |
| 743 | int ssl3_send_hello_request(SSL *s) |
| 744 | { |
| 745 | |
| 746 | if (s->state == SSL3_ST_SW_HELLO_REQ_A) |
| 747 | { |
| 748 | ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0); |
| 749 | s->state=SSL3_ST_SW_HELLO_REQ_B; |
| 750 | } |
| 751 | |
| 752 | /* SSL3_ST_SW_HELLO_REQ_B */ |
| 753 | return ssl_do_write(s); |
| 754 | } |
| 755 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 756 | int ssl3_get_client_hello(SSL *s) |
| 757 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 758 | int i,ok,al=SSL_AD_INTERNAL_ERROR,ret= -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 759 | long n; |
David Benjamin | 6f26001 | 2014-08-15 13:49:12 -0400 | [diff] [blame] | 760 | const SSL_CIPHER *c; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 761 | STACK_OF(SSL_CIPHER) *ciphers=NULL; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 762 | struct ssl_early_callback_ctx early_ctx; |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 763 | CBS client_hello; |
| 764 | uint16_t client_version; |
| 765 | CBS client_random, session_id, cipher_suites, compression_methods; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 766 | |
| 767 | /* We do this so that we will respond with our native type. |
| 768 | * If we are TLSv1 and we get SSLv3, we will respond with TLSv1, |
| 769 | * This down switching should be handled by a different method. |
| 770 | * If we are SSLv3, we will respond with SSLv3, even if prompted with |
| 771 | * TLSv1. |
| 772 | */ |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 773 | switch (s->state) { |
| 774 | case SSL3_ST_SR_CLNT_HELLO_A: |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 775 | case SSL3_ST_SR_CLNT_HELLO_B: |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 776 | s->first_packet=1; |
| 777 | n=s->method->ssl_get_message(s, |
David Benjamin | 95fcaa4 | 2014-08-05 13:10:14 -0400 | [diff] [blame] | 778 | SSL3_ST_SR_CLNT_HELLO_A, |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 779 | SSL3_ST_SR_CLNT_HELLO_B, |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 780 | SSL3_MT_CLIENT_HELLO, |
| 781 | SSL3_RT_MAX_PLAIN_LENGTH, |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 782 | SSL_GET_MESSAGE_HASH_MESSAGE, |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 783 | &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 784 | |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 785 | if (!ok) return((int)n); |
| 786 | s->first_packet=0; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 787 | |
| 788 | /* If we require cookies and this ClientHello doesn't |
| 789 | * contain one, just return since we do not want to |
| 790 | * allocate any memory yet. So check cookie length... |
| 791 | */ |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 792 | 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] | 793 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 794 | CBS session_id; |
| 795 | uint8_t cookie_length; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 796 | |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 797 | CBS_init(&client_hello, s->init_msg, n); |
| 798 | if (!CBS_skip(&client_hello, 2 + SSL3_RANDOM_SIZE) || |
| 799 | !CBS_get_u8_length_prefixed(&client_hello, &session_id) || |
| 800 | !CBS_get_u8(&client_hello, &cookie_length)) |
| 801 | { |
| 802 | al = SSL_AD_DECODE_ERROR; |
| 803 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR); |
| 804 | goto f_err; |
| 805 | } |
| 806 | |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 807 | if (cookie_length == 0) |
| 808 | return 1; |
| 809 | } |
| 810 | s->state = SSL3_ST_SR_CLNT_HELLO_C; |
| 811 | /* fallthrough */ |
| 812 | case SSL3_ST_SR_CLNT_HELLO_C: |
| 813 | case SSL3_ST_SR_CLNT_HELLO_D: |
| 814 | /* We have previously parsed the ClientHello message, |
| 815 | * and can't call ssl_get_message again without hashing |
| 816 | * the message into the Finished digest again. */ |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 817 | n = s->init_num; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 818 | |
| 819 | memset(&early_ctx, 0, sizeof(early_ctx)); |
| 820 | early_ctx.ssl = s; |
| 821 | early_ctx.client_hello = s->init_msg; |
| 822 | early_ctx.client_hello_len = n; |
| 823 | if (!ssl_early_callback_init(&early_ctx)) |
| 824 | { |
| 825 | al = SSL_AD_DECODE_ERROR; |
| 826 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CLIENTHELLO_PARSE_FAILED); |
| 827 | goto f_err; |
| 828 | } |
| 829 | |
| 830 | if (s->state == SSL3_ST_SR_CLNT_HELLO_C && |
| 831 | s->ctx->select_certificate_cb != NULL) |
| 832 | { |
| 833 | int ret; |
| 834 | |
| 835 | s->state = SSL3_ST_SR_CLNT_HELLO_D; |
| 836 | ret = s->ctx->select_certificate_cb(&early_ctx); |
| 837 | if (ret == 0) |
| 838 | return CERTIFICATE_SELECTION_PENDING; |
| 839 | else if (ret == -1) |
| 840 | { |
| 841 | /* Connection rejected. */ |
| 842 | al = SSL_AD_ACCESS_DENIED; |
| 843 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CONNECTION_REJECTED); |
| 844 | goto f_err; |
| 845 | } |
| 846 | } |
| 847 | s->state = SSL3_ST_SR_CLNT_HELLO_D; |
David Benjamin | 4c852c5 | 2014-07-02 15:24:48 -0400 | [diff] [blame] | 848 | break; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 849 | default: |
David Benjamin | 4c852c5 | 2014-07-02 15:24:48 -0400 | [diff] [blame] | 850 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNKNOWN_STATE); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 851 | return -1; |
| 852 | } |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 853 | |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 854 | CBS_init(&client_hello, s->init_msg, n); |
| 855 | if (!CBS_get_u16(&client_hello, &client_version) || |
| 856 | !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) || |
| 857 | !CBS_get_u8_length_prefixed(&client_hello, &session_id) || |
| 858 | CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 859 | { |
| 860 | al = SSL_AD_DECODE_ERROR; |
| 861 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR); |
| 862 | goto f_err; |
| 863 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 864 | |
| 865 | /* use version from inside client hello, not from record header |
| 866 | * (may differ: see RFC 2246, Appendix E, second paragraph) */ |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 867 | s->client_version = client_version; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 868 | |
| 869 | if (SSL_IS_DTLS(s) ? (s->client_version > s->version && |
| 870 | s->method->version != DTLS_ANY_VERSION) |
| 871 | : (s->client_version < s->version)) |
| 872 | { |
| 873 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER); |
| 874 | if ((s->client_version>>8) == SSL3_VERSION_MAJOR && |
| 875 | !s->enc_write_ctx && !s->write_hash) |
| 876 | { |
| 877 | /* similar to ssl3_get_record, send alert using remote version number */ |
| 878 | s->version = s->client_version; |
| 879 | } |
| 880 | al = SSL_AD_PROTOCOL_VERSION; |
| 881 | goto f_err; |
| 882 | } |
| 883 | |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 884 | /* Load the client random. */ |
| 885 | memcpy(s->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 886 | |
| 887 | s->hit=0; |
| 888 | /* Versions before 0.9.7 always allow clients to resume sessions in renegotiation. |
| 889 | * 0.9.7 and later allow this by default, but optionally ignore resumption requests |
| 890 | * with flag SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather |
| 891 | * than a change to default behavior so that applications relying on this for security |
| 892 | * won't even compile against older library versions). |
| 893 | * |
| 894 | * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to request |
| 895 | * renegotiation but not a new session (s->new_session remains unset): for servers, |
| 896 | * this essentially just means that the SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
| 897 | * setting will be ignored. |
| 898 | */ |
| 899 | if ((s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) |
| 900 | { |
| 901 | if (!ssl_get_new_session(s,1)) |
| 902 | goto err; |
| 903 | } |
| 904 | else |
| 905 | { |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 906 | i=ssl_get_prev_session(s, &early_ctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 907 | if (i == 1) |
| 908 | { /* previous session */ |
| 909 | s->hit=1; |
| 910 | } |
| 911 | else if (i == -1) |
| 912 | goto err; |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 913 | else if (i == PENDING_SESSION) |
| 914 | { |
| 915 | ret = PENDING_SESSION; |
| 916 | goto err; |
| 917 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 918 | else /* i == 0 */ |
| 919 | { |
| 920 | if (!ssl_get_new_session(s,1)) |
| 921 | goto err; |
| 922 | } |
| 923 | } |
| 924 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 925 | if (SSL_IS_DTLS(s)) |
| 926 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 927 | CBS cookie; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 928 | |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 929 | /* TODO(davidben): The length check here is off. Per |
| 930 | * spec, the maximum cookie length is 32. However, the |
| 931 | * DTLS1_COOKIE_LENGTH check is checking against 256, |
| 932 | * not 32 (so it's actually redundant). |
| 933 | * 07a9d1a2c2b735cbc327065000b545deb5e136cf from |
| 934 | * OpenSSL switched this from 32 to 256. */ |
| 935 | if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) || |
| 936 | CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 937 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 938 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 939 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 940 | goto f_err; |
| 941 | } |
| 942 | |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 943 | /* Verify the cookie if appropriate option is set. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 944 | if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 945 | CBS_len(&cookie) > 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 946 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 947 | if (s->ctx->app_verify_cookie_cb != NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 948 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 949 | if (s->ctx->app_verify_cookie_cb(s, |
David Benjamin | fb4ea28 | 2014-08-15 13:38:15 -0400 | [diff] [blame] | 950 | CBS_data(&cookie), CBS_len(&cookie)) == 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 951 | { |
| 952 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 953 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_COOKIE_MISMATCH); |
| 954 | goto f_err; |
| 955 | } |
| 956 | /* else cookie verification succeeded */ |
| 957 | } |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 958 | 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] | 959 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 960 | /* default verification */ |
| 961 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 962 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_COOKIE_MISMATCH); |
| 963 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 964 | } |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 965 | /* Set to -2 so if successful we return 2 and |
| 966 | * don't send HelloVerifyRequest. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 967 | ret = -2; |
| 968 | } |
| 969 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 970 | if (s->method->version == DTLS_ANY_VERSION) |
| 971 | { |
| 972 | /* Select version to use */ |
| 973 | if (s->client_version <= DTLS1_2_VERSION && |
| 974 | !(s->options & SSL_OP_NO_DTLSv1_2)) |
| 975 | { |
| 976 | s->version = DTLS1_2_VERSION; |
| 977 | s->method = DTLSv1_2_server_method(); |
| 978 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 979 | else if (s->client_version <= DTLS1_VERSION && |
| 980 | !(s->options & SSL_OP_NO_DTLSv1)) |
| 981 | { |
| 982 | s->version = DTLS1_VERSION; |
| 983 | s->method = DTLSv1_server_method(); |
| 984 | } |
| 985 | else |
| 986 | { |
| 987 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER); |
| 988 | s->version = s->client_version; |
| 989 | al = SSL_AD_PROTOCOL_VERSION; |
| 990 | goto f_err; |
| 991 | } |
| 992 | s->session->ssl_version = s->version; |
| 993 | } |
| 994 | } |
| 995 | |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 996 | if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) || |
| 997 | !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) || |
| 998 | CBS_len(&compression_methods) == 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 999 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 1000 | al = SSL_AD_DECODE_ERROR; |
| 1001 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR); |
| 1002 | goto f_err; |
| 1003 | } |
| 1004 | |
| 1005 | /* TODO(davidben): Per spec, cipher_suites can never be empty |
| 1006 | * (specified at the ClientHello structure level). This logic |
| 1007 | * allows it to be empty if resuming a session. Can we always |
| 1008 | * require non-empty? If a client sends empty cipher_suites |
| 1009 | * because it's resuming a session, it could always fail to |
| 1010 | * resume a session, so it's unlikely to actually work. */ |
| 1011 | if (CBS_len(&cipher_suites) == 0 && CBS_len(&session_id) != 0) |
| 1012 | { |
| 1013 | /* We need a cipher if we are not resuming a session. */ |
| 1014 | al = SSL_AD_ILLEGAL_PARAMETER; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1015 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_SPECIFIED); |
| 1016 | goto f_err; |
| 1017 | } |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 1018 | |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 1019 | if (ssl_bytes_to_cipher_list(s, &cipher_suites, &ciphers) == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1020 | { |
| 1021 | goto err; |
| 1022 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1023 | |
| 1024 | /* 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] | 1025 | if (s->hit && CBS_len(&cipher_suites) > 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1026 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 1027 | int found_cipher = 0; |
| 1028 | unsigned long id = s->session->cipher->id; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1029 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1030 | for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++) |
| 1031 | { |
| 1032 | c=sk_SSL_CIPHER_value(ciphers,i); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1033 | if (c->id == id) |
| 1034 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 1035 | found_cipher = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1036 | break; |
| 1037 | } |
| 1038 | } |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 1039 | if (!found_cipher) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1040 | { |
| 1041 | /* we need to have the cipher in the cipher |
| 1042 | * list if we are asked to reuse it */ |
| 1043 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 1044 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_REQUIRED_CIPHER_MISSING); |
| 1045 | goto f_err; |
| 1046 | } |
| 1047 | } |
| 1048 | |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 1049 | /* Only null compression is supported. */ |
| 1050 | if (memchr(CBS_data(&compression_methods), 0, |
| 1051 | CBS_len(&compression_methods)) == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1052 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 1053 | al = SSL_AD_ILLEGAL_PARAMETER; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1054 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_COMPRESSION_SPECIFIED); |
| 1055 | goto f_err; |
| 1056 | } |
| 1057 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1058 | /* TLS extensions*/ |
| 1059 | if (s->version >= SSL3_VERSION) |
| 1060 | { |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 1061 | if (!ssl_parse_clienthello_tlsext(s, &client_hello)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1062 | { |
| 1063 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_PARSE_TLSEXT); |
| 1064 | goto err; |
| 1065 | } |
| 1066 | } |
| 1067 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1068 | /* There should be nothing left over in the record. */ |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 1069 | if (CBS_len(&client_hello) != 0) |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1070 | { |
| 1071 | /* wrong packet length */ |
| 1072 | al=SSL_AD_DECODE_ERROR; |
| 1073 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_BAD_PACKET_LENGTH); |
| 1074 | goto f_err; |
| 1075 | } |
| 1076 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1077 | /* Check if we want to use external pre-shared secret for this |
| 1078 | * handshake for not reused session only. We need to generate |
| 1079 | * server_random before calling tls_session_secret_cb in order to allow |
| 1080 | * SessionTicket processing to use it in key derivation. */ |
| 1081 | { |
| 1082 | unsigned char *pos; |
| 1083 | pos=s->s3->server_random; |
| 1084 | if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) |
| 1085 | { |
| 1086 | goto f_err; |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) |
| 1091 | { |
David Benjamin | 6f26001 | 2014-08-15 13:49:12 -0400 | [diff] [blame] | 1092 | const SSL_CIPHER *pref_cipher=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1093 | |
| 1094 | s->session->master_key_length=sizeof(s->session->master_key); |
| 1095 | if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length, |
| 1096 | ciphers, &pref_cipher, s->tls_session_secret_cb_arg)) |
| 1097 | { |
| 1098 | s->hit=1; |
| 1099 | s->session->ciphers=ciphers; |
| 1100 | s->session->verify_result=X509_V_OK; |
| 1101 | |
| 1102 | ciphers=NULL; |
| 1103 | |
| 1104 | /* check if some cipher was preferred by call back */ |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1105 | 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] | 1106 | if (pref_cipher == NULL) |
| 1107 | { |
| 1108 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1109 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER); |
| 1110 | goto f_err; |
| 1111 | } |
| 1112 | |
| 1113 | s->session->cipher=pref_cipher; |
| 1114 | |
| 1115 | if (s->cipher_list) |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1116 | ssl_cipher_preference_list_free(s->cipher_list); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1117 | |
| 1118 | if (s->cipher_list_by_id) |
| 1119 | sk_SSL_CIPHER_free(s->cipher_list_by_id); |
| 1120 | |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1121 | s->cipher_list = ssl_cipher_preference_list_from_ciphers(s->session->ciphers); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1122 | s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers); |
| 1123 | } |
| 1124 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1125 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1126 | /* Given s->session->ciphers and SSL_get_ciphers, we must |
| 1127 | * pick a cipher */ |
| 1128 | |
| 1129 | if (!s->hit) |
| 1130 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1131 | if (s->session->ciphers != NULL) |
| 1132 | sk_SSL_CIPHER_free(s->session->ciphers); |
| 1133 | s->session->ciphers=ciphers; |
| 1134 | if (ciphers == NULL) |
| 1135 | { |
| 1136 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 1137 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_PASSED); |
| 1138 | goto f_err; |
| 1139 | } |
| 1140 | ciphers=NULL; |
| 1141 | /* Let cert callback update server certificates if required */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1142 | if (s->cert->cert_cb) |
| 1143 | { |
| 1144 | int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg); |
| 1145 | if (rv == 0) |
| 1146 | { |
| 1147 | al=SSL_AD_INTERNAL_ERROR; |
| 1148 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CERT_CB_ERROR); |
| 1149 | goto f_err; |
| 1150 | } |
| 1151 | if (rv < 0) |
| 1152 | { |
| 1153 | s->rwstate=SSL_X509_LOOKUP; |
| 1154 | return -1; |
| 1155 | } |
| 1156 | s->rwstate = SSL_NOTHING; |
| 1157 | } |
| 1158 | c=ssl3_choose_cipher(s,s->session->ciphers, |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1159 | ssl_get_cipher_preferences(s)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1160 | |
| 1161 | if (c == NULL) |
| 1162 | { |
| 1163 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1164 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER); |
| 1165 | goto f_err; |
| 1166 | } |
| 1167 | s->s3->tmp.new_cipher=c; |
| 1168 | } |
| 1169 | else |
| 1170 | { |
| 1171 | /* Session-id reuse */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1172 | s->s3->tmp.new_cipher=s->session->cipher; |
| 1173 | } |
| 1174 | |
| 1175 | if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) |
| 1176 | { |
| 1177 | if (!ssl3_digest_cached_records(s)) |
| 1178 | goto f_err; |
| 1179 | } |
| 1180 | |
| 1181 | /* we now have the following setup. |
| 1182 | * client_random |
| 1183 | * cipher_list - our prefered list of ciphers |
| 1184 | * ciphers - the clients prefered list of ciphers |
| 1185 | * compression - basically ignored right now |
| 1186 | * ssl version is set - sslv3 |
| 1187 | * s->session - The ssl session has been setup. |
| 1188 | * s->hit - session reuse flag |
| 1189 | * s->tmp.new_cipher - the new cipher to use. |
| 1190 | */ |
| 1191 | |
| 1192 | /* Handles TLS extensions that we couldn't check earlier */ |
| 1193 | if (s->version >= SSL3_VERSION) |
| 1194 | { |
| 1195 | if (ssl_check_clienthello_tlsext_late(s) <= 0) |
| 1196 | { |
| 1197 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CLIENTHELLO_TLSEXT); |
| 1198 | goto err; |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | if (ret < 0) ret=-ret; |
| 1203 | if (0) |
| 1204 | { |
| 1205 | f_err: |
| 1206 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1207 | } |
| 1208 | err: |
| 1209 | if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers); |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1210 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | int ssl3_send_server_hello(SSL *s) |
| 1214 | { |
| 1215 | unsigned char *buf; |
| 1216 | unsigned char *p,*d; |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 1217 | int sl; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1218 | unsigned long l; |
| 1219 | |
| 1220 | if (s->state == SSL3_ST_SW_SRVR_HELLO_A) |
| 1221 | { |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1222 | /* We only accept ChannelIDs on connections with ECDHE in order |
| 1223 | * to avoid a known attack while we fix ChannelID itself. */ |
| 1224 | if (s->s3 && |
| 1225 | s->s3->tlsext_channel_id_valid && |
| 1226 | (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kEECDH) == 0) |
| 1227 | s->s3->tlsext_channel_id_valid = 0; |
| 1228 | |
| 1229 | /* If this is a resumption and the original handshake didn't |
| 1230 | * support ChannelID then we didn't record the original |
| 1231 | * handshake hashes in the session and so cannot resume with |
| 1232 | * ChannelIDs. */ |
| 1233 | if (s->hit && |
| 1234 | s->s3->tlsext_channel_id_new && |
| 1235 | s->session->original_handshake_hash_len == 0) |
| 1236 | s->s3->tlsext_channel_id_valid = 0; |
| 1237 | |
Adam Langley | 1b9d477 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1238 | if (s->mode & SSL_MODE_RELEASE_BUFFERS) |
| 1239 | { |
| 1240 | /* Free s->session->ciphers in order to release memory. This |
| 1241 | * breaks SSL_get_shared_ciphers(), but many servers will |
David Benjamin | 5ffeb7c | 2014-07-15 00:34:01 -0400 | [diff] [blame] | 1242 | * prefer the memory savings. */ |
Adam Langley | 1b9d477 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1243 | sk_SSL_CIPHER_free(s->session->ciphers); |
| 1244 | s->session->ciphers = NULL; |
| 1245 | } |
| 1246 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1247 | buf=(unsigned char *)s->init_buf->data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1248 | /* Do the message type and length last */ |
| 1249 | d=p= ssl_handshake_start(s); |
| 1250 | |
| 1251 | *(p++)=s->version>>8; |
| 1252 | *(p++)=s->version&0xff; |
| 1253 | |
| 1254 | /* Random stuff */ |
| 1255 | memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); |
| 1256 | p+=SSL3_RANDOM_SIZE; |
| 1257 | |
| 1258 | /* There are several cases for the session ID to send |
| 1259 | * back in the server hello: |
| 1260 | * - For session reuse from the session cache, |
| 1261 | * we send back the old session ID. |
| 1262 | * - If stateless session reuse (using a session ticket) |
| 1263 | * is successful, we send back the client's "session ID" |
| 1264 | * (which doesn't actually identify the session). |
| 1265 | * - If it is a new session, we send back the new |
| 1266 | * session ID. |
| 1267 | * - However, if we want the new session to be single-use, |
| 1268 | * we send back a 0-length session ID. |
| 1269 | * s->hit is non-zero in either case of session reuse, |
| 1270 | * so the following won't overwrite an ID that we're supposed |
| 1271 | * to send back. |
| 1272 | */ |
| 1273 | if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) |
| 1274 | && !s->hit) |
| 1275 | s->session->session_id_length=0; |
| 1276 | |
| 1277 | sl=s->session->session_id_length; |
| 1278 | if (sl > (int)sizeof(s->session->session_id)) |
| 1279 | { |
| 1280 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR); |
| 1281 | return -1; |
| 1282 | } |
| 1283 | *(p++)=sl; |
| 1284 | memcpy(p,s->session->session_id,sl); |
| 1285 | p+=sl; |
| 1286 | |
| 1287 | /* put the cipher */ |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 1288 | s2n(ssl3_get_cipher_value(s->s3->tmp.new_cipher), p); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1289 | |
| 1290 | /* put the compression method */ |
| 1291 | *(p++)=0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1292 | if (ssl_prepare_serverhello_tlsext(s) <= 0) |
| 1293 | { |
| 1294 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, SSL_R_SERVERHELLO_TLSEXT); |
| 1295 | return -1; |
| 1296 | } |
| 1297 | if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL) |
| 1298 | { |
| 1299 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR); |
| 1300 | return -1; |
| 1301 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1302 | /* do the header */ |
| 1303 | l=(p-d); |
| 1304 | ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l); |
| 1305 | s->state=SSL3_ST_SW_SRVR_HELLO_B; |
| 1306 | } |
| 1307 | |
| 1308 | /* SSL3_ST_SW_SRVR_HELLO_B */ |
| 1309 | return ssl_do_write(s); |
| 1310 | } |
| 1311 | |
| 1312 | int ssl3_send_server_done(SSL *s) |
| 1313 | { |
| 1314 | |
| 1315 | if (s->state == SSL3_ST_SW_SRVR_DONE_A) |
| 1316 | { |
| 1317 | ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0); |
| 1318 | s->state = SSL3_ST_SW_SRVR_DONE_B; |
| 1319 | } |
| 1320 | |
| 1321 | /* SSL3_ST_SW_SRVR_DONE_B */ |
| 1322 | return ssl_do_write(s); |
| 1323 | } |
| 1324 | |
| 1325 | int ssl3_send_server_key_exchange(SSL *s) |
| 1326 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1327 | unsigned char *q; |
| 1328 | int j,num; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1329 | unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; |
| 1330 | unsigned int u; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1331 | #ifndef OPENSSL_NO_DH |
| 1332 | DH *dh=NULL,*dhp; |
| 1333 | #endif |
| 1334 | #ifndef OPENSSL_NO_ECDH |
| 1335 | EC_KEY *ecdh=NULL, *ecdhp; |
| 1336 | unsigned char *encodedPoint = NULL; |
| 1337 | int encodedlen = 0; |
| 1338 | int curve_id = 0; |
| 1339 | BN_CTX *bn_ctx = NULL; |
| 1340 | #endif |
David Benjamin | 77a942b | 2014-07-15 01:22:50 -0400 | [diff] [blame] | 1341 | const char* psk_identity_hint = NULL; |
| 1342 | size_t psk_identity_hint_len = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1343 | EVP_PKEY *pkey; |
| 1344 | const EVP_MD *md = NULL; |
| 1345 | unsigned char *p,*d; |
| 1346 | int al,i; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1347 | unsigned long alg_k; |
| 1348 | unsigned long alg_a; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1349 | int n; |
| 1350 | CERT *cert; |
| 1351 | BIGNUM *r[4]; |
| 1352 | int nr[4],kn; |
| 1353 | BUF_MEM *buf; |
| 1354 | EVP_MD_CTX md_ctx; |
| 1355 | |
| 1356 | EVP_MD_CTX_init(&md_ctx); |
| 1357 | if (s->state == SSL3_ST_SW_KEY_EXCH_A) |
| 1358 | { |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1359 | alg_k=s->s3->tmp.new_cipher->algorithm_mkey; |
| 1360 | alg_a=s->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1361 | cert=s->cert; |
| 1362 | |
| 1363 | buf=s->init_buf; |
| 1364 | |
| 1365 | r[0]=r[1]=r[2]=r[3]=NULL; |
| 1366 | n=0; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1367 | if (alg_a & SSL_aPSK) |
| 1368 | { |
| 1369 | /* size for PSK identity hint */ |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1370 | psk_identity_hint = s->session->psk_identity_hint; |
| 1371 | if (psk_identity_hint) |
| 1372 | psk_identity_hint_len = strlen(psk_identity_hint); |
| 1373 | else |
| 1374 | psk_identity_hint_len = 0; |
| 1375 | n+=2+psk_identity_hint_len; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1376 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1377 | #ifndef OPENSSL_NO_DH |
David Benjamin | 77a942b | 2014-07-15 01:22:50 -0400 | [diff] [blame] | 1378 | if (alg_k & SSL_kEDH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1379 | { |
| 1380 | dhp=cert->dh_tmp; |
| 1381 | if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL)) |
David Benjamin | 060d9d2 | 2014-07-15 00:54:26 -0400 | [diff] [blame] | 1382 | dhp=s->cert->dh_tmp_cb(s, 0, 1024); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1383 | if (dhp == NULL) |
| 1384 | { |
| 1385 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1386 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_MISSING_TMP_DH_KEY); |
| 1387 | goto f_err; |
| 1388 | } |
| 1389 | |
| 1390 | if (s->s3->tmp.dh != NULL) |
| 1391 | { |
| 1392 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_INTERNAL_ERROR); |
| 1393 | goto err; |
| 1394 | } |
| 1395 | |
| 1396 | if ((dh=DHparams_dup(dhp)) == NULL) |
| 1397 | { |
| 1398 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB); |
| 1399 | goto err; |
| 1400 | } |
| 1401 | |
| 1402 | s->s3->tmp.dh=dh; |
| 1403 | if ((dhp->pub_key == NULL || |
| 1404 | dhp->priv_key == NULL || |
| 1405 | (s->options & SSL_OP_SINGLE_DH_USE))) |
| 1406 | { |
| 1407 | if(!DH_generate_key(dh)) |
| 1408 | { |
| 1409 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB); |
| 1410 | goto err; |
| 1411 | } |
| 1412 | } |
| 1413 | else |
| 1414 | { |
| 1415 | dh->pub_key=BN_dup(dhp->pub_key); |
| 1416 | dh->priv_key=BN_dup(dhp->priv_key); |
| 1417 | if ((dh->pub_key == NULL) || |
| 1418 | (dh->priv_key == NULL)) |
| 1419 | { |
| 1420 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB); |
| 1421 | goto err; |
| 1422 | } |
| 1423 | } |
| 1424 | r[0]=dh->p; |
| 1425 | r[1]=dh->g; |
| 1426 | r[2]=dh->pub_key; |
| 1427 | } |
David Benjamin | 77a942b | 2014-07-15 01:22:50 -0400 | [diff] [blame] | 1428 | else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1429 | #endif |
| 1430 | #ifndef OPENSSL_NO_ECDH |
David Benjamin | 77a942b | 2014-07-15 01:22:50 -0400 | [diff] [blame] | 1431 | if (alg_k & SSL_kEECDH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1432 | { |
| 1433 | const EC_GROUP *group; |
| 1434 | |
| 1435 | ecdhp=cert->ecdh_tmp; |
| 1436 | if (s->cert->ecdh_tmp_auto) |
| 1437 | { |
| 1438 | /* Get NID of appropriate shared curve */ |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 1439 | int nid = tls1_get_shared_curve(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1440 | if (nid != NID_undef) |
| 1441 | ecdhp = EC_KEY_new_by_curve_name(nid); |
| 1442 | } |
| 1443 | else if ((ecdhp == NULL) && s->cert->ecdh_tmp_cb) |
| 1444 | { |
David Benjamin | 060d9d2 | 2014-07-15 00:54:26 -0400 | [diff] [blame] | 1445 | ecdhp = s->cert->ecdh_tmp_cb(s, 0, 1024); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1446 | } |
| 1447 | if (ecdhp == NULL) |
| 1448 | { |
| 1449 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1450 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_MISSING_TMP_ECDH_KEY); |
| 1451 | goto f_err; |
| 1452 | } |
| 1453 | |
| 1454 | if (s->s3->tmp.ecdh != NULL) |
| 1455 | { |
| 1456 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_INTERNAL_ERROR); |
| 1457 | goto err; |
| 1458 | } |
| 1459 | |
| 1460 | /* Duplicate the ECDH structure. */ |
| 1461 | if (ecdhp == NULL) |
| 1462 | { |
| 1463 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1464 | goto err; |
| 1465 | } |
| 1466 | if (s->cert->ecdh_tmp_auto) |
| 1467 | ecdh = ecdhp; |
| 1468 | else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) |
| 1469 | { |
| 1470 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1471 | goto err; |
| 1472 | } |
| 1473 | |
| 1474 | s->s3->tmp.ecdh=ecdh; |
| 1475 | if ((EC_KEY_get0_public_key(ecdh) == NULL) || |
| 1476 | (EC_KEY_get0_private_key(ecdh) == NULL) || |
| 1477 | (s->options & SSL_OP_SINGLE_ECDH_USE)) |
| 1478 | { |
| 1479 | if(!EC_KEY_generate_key(ecdh)) |
| 1480 | { |
| 1481 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1482 | goto err; |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | if (((group = EC_KEY_get0_group(ecdh)) == NULL) || |
| 1487 | (EC_KEY_get0_public_key(ecdh) == NULL) || |
| 1488 | (EC_KEY_get0_private_key(ecdh) == NULL)) |
| 1489 | { |
| 1490 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1491 | goto err; |
| 1492 | } |
| 1493 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1494 | /* XXX: For now, we only support ephemeral ECDH |
| 1495 | * keys over named (not generic) curves. For |
| 1496 | * supported named curves, curve_id is non-zero. |
| 1497 | */ |
| 1498 | if ((curve_id = |
| 1499 | tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group))) |
| 1500 | == 0) |
| 1501 | { |
| 1502 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); |
| 1503 | goto err; |
| 1504 | } |
| 1505 | |
| 1506 | /* Encode the public key. |
| 1507 | * First check the size of encoding and |
| 1508 | * allocate memory accordingly. |
| 1509 | */ |
| 1510 | encodedlen = EC_POINT_point2oct(group, |
| 1511 | EC_KEY_get0_public_key(ecdh), |
| 1512 | POINT_CONVERSION_UNCOMPRESSED, |
| 1513 | NULL, 0, NULL); |
| 1514 | |
| 1515 | encodedPoint = (unsigned char *) |
| 1516 | OPENSSL_malloc(encodedlen*sizeof(unsigned char)); |
| 1517 | bn_ctx = BN_CTX_new(); |
| 1518 | if ((encodedPoint == NULL) || (bn_ctx == NULL)) |
| 1519 | { |
| 1520 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_MALLOC_FAILURE); |
| 1521 | goto err; |
| 1522 | } |
| 1523 | |
| 1524 | |
| 1525 | encodedlen = EC_POINT_point2oct(group, |
| 1526 | EC_KEY_get0_public_key(ecdh), |
| 1527 | POINT_CONVERSION_UNCOMPRESSED, |
| 1528 | encodedPoint, encodedlen, bn_ctx); |
| 1529 | |
| 1530 | if (encodedlen == 0) |
| 1531 | { |
| 1532 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1533 | goto err; |
| 1534 | } |
| 1535 | |
| 1536 | BN_CTX_free(bn_ctx); bn_ctx=NULL; |
| 1537 | |
| 1538 | /* XXX: For now, we only support named (not |
| 1539 | * generic) curves in ECDH ephemeral key exchanges. |
| 1540 | * In this situation, we need four additional bytes |
| 1541 | * to encode the entire ServerECDHParams |
| 1542 | * structure. |
| 1543 | */ |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1544 | n += 4 + encodedlen; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1545 | |
| 1546 | /* We'll generate the serverKeyExchange message |
| 1547 | * explicitly so we can set these to NULLs |
| 1548 | */ |
| 1549 | r[0]=NULL; |
| 1550 | r[1]=NULL; |
| 1551 | r[2]=NULL; |
| 1552 | r[3]=NULL; |
| 1553 | } |
David Benjamin | 77a942b | 2014-07-15 01:22:50 -0400 | [diff] [blame] | 1554 | else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1555 | #endif /* !OPENSSL_NO_ECDH */ |
David Benjamin | 77a942b | 2014-07-15 01:22:50 -0400 | [diff] [blame] | 1556 | if (!(alg_k & SSL_kPSK)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1557 | { |
| 1558 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1559 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); |
| 1560 | goto f_err; |
| 1561 | } |
| 1562 | for (i=0; i < 4 && r[i] != NULL; i++) |
| 1563 | { |
| 1564 | nr[i]=BN_num_bytes(r[i]); |
| 1565 | n+=2+nr[i]; |
| 1566 | } |
| 1567 | |
David Benjamin | e8f3d66 | 2014-07-12 01:10:19 -0400 | [diff] [blame] | 1568 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1569 | { |
| 1570 | if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher,&md)) |
| 1571 | == NULL) |
| 1572 | { |
| 1573 | al=SSL_AD_DECODE_ERROR; |
| 1574 | goto f_err; |
| 1575 | } |
| 1576 | kn=EVP_PKEY_size(pkey); |
| 1577 | } |
| 1578 | else |
| 1579 | { |
| 1580 | pkey=NULL; |
| 1581 | kn=0; |
| 1582 | } |
| 1583 | |
| 1584 | if (!BUF_MEM_grow_clean(buf,n+SSL_HM_HEADER_LENGTH(s)+kn)) |
| 1585 | { |
| 1586 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_BUF); |
| 1587 | goto err; |
| 1588 | } |
| 1589 | d = p = ssl_handshake_start(s); |
| 1590 | |
| 1591 | for (i=0; i < 4 && r[i] != NULL; i++) |
| 1592 | { |
| 1593 | s2n(nr[i],p); |
| 1594 | BN_bn2bin(r[i],p); |
| 1595 | p+=nr[i]; |
| 1596 | } |
| 1597 | |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1598 | /* Note: ECDHE PSK ciphersuites use SSL_kEECDH and SSL_aPSK. |
| 1599 | * When one of them is used, the server key exchange record needs to have both |
| 1600 | * the psk_identity_hint and the ServerECDHParams. */ |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1601 | if (alg_a & SSL_aPSK) |
| 1602 | { |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1603 | /* copy PSK identity hint (if provided) */ |
| 1604 | s2n(psk_identity_hint_len, p); |
| 1605 | if (psk_identity_hint_len > 0) |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1606 | { |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1607 | memcpy(p, psk_identity_hint, psk_identity_hint_len); |
| 1608 | p+=psk_identity_hint_len; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1609 | } |
| 1610 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1611 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1612 | #ifndef OPENSSL_NO_ECDH |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1613 | if (alg_k & SSL_kEECDH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1614 | { |
| 1615 | /* XXX: For now, we only support named (not generic) curves. |
| 1616 | * In this situation, the serverKeyExchange message has: |
| 1617 | * [1 byte CurveType], [2 byte CurveName] |
| 1618 | * [1 byte length of encoded point], followed by |
| 1619 | * the actual encoded point itself |
| 1620 | */ |
| 1621 | *p = NAMED_CURVE_TYPE; |
| 1622 | p += 1; |
| 1623 | *p = 0; |
| 1624 | p += 1; |
| 1625 | *p = curve_id; |
| 1626 | p += 1; |
| 1627 | *p = encodedlen; |
| 1628 | p += 1; |
| 1629 | memcpy((unsigned char*)p, |
| 1630 | (unsigned char *)encodedPoint, |
| 1631 | encodedlen); |
| 1632 | OPENSSL_free(encodedPoint); |
| 1633 | encodedPoint = NULL; |
| 1634 | p += encodedlen; |
| 1635 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1636 | #endif /* OPENSSL_NO_ECDH */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1637 | |
| 1638 | /* not anonymous */ |
| 1639 | if (pkey != NULL) |
| 1640 | { |
| 1641 | /* n is the length of the params, they start at &(d[4]) |
| 1642 | * and p points to the space at the end. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1643 | if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) |
| 1644 | { |
| 1645 | q=md_buf; |
| 1646 | j=0; |
| 1647 | for (num=2; num > 0; num--) |
| 1648 | { |
David Benjamin | e712778 | 2014-08-18 23:15:29 -0400 | [diff] [blame] | 1649 | EVP_DigestInit_ex(&md_ctx, |
| 1650 | (num == 2) ? EVP_md5() : EVP_sha1(), NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1651 | EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); |
| 1652 | EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); |
| 1653 | EVP_DigestUpdate(&md_ctx,d,n); |
| 1654 | EVP_DigestFinal_ex(&md_ctx,q, |
| 1655 | (unsigned int *)&i); |
| 1656 | q+=i; |
| 1657 | j+=i; |
| 1658 | } |
| 1659 | if (RSA_sign(NID_md5_sha1, md_buf, j, |
| 1660 | &(p[2]), &u, pkey->pkey.rsa) <= 0) |
| 1661 | { |
| 1662 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_RSA); |
| 1663 | goto err; |
| 1664 | } |
| 1665 | s2n(u,p); |
| 1666 | n+=u+2; |
| 1667 | } |
| 1668 | else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1669 | if (md) |
| 1670 | { |
| 1671 | /* send signature algorithm */ |
| 1672 | if (SSL_USE_SIGALGS(s)) |
| 1673 | { |
| 1674 | if (!tls12_get_sigandhash(p, pkey, md)) |
| 1675 | { |
| 1676 | /* Should never happen */ |
| 1677 | al=SSL_AD_INTERNAL_ERROR; |
| 1678 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_INTERNAL_ERROR); |
| 1679 | goto f_err; |
| 1680 | } |
| 1681 | p+=2; |
| 1682 | } |
| 1683 | #ifdef SSL_DEBUG |
| 1684 | fprintf(stderr, "Using hash %s\n", |
| 1685 | EVP_MD_name(md)); |
| 1686 | #endif |
| 1687 | EVP_SignInit_ex(&md_ctx, md, NULL); |
| 1688 | EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); |
| 1689 | EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); |
| 1690 | EVP_SignUpdate(&md_ctx,d,n); |
| 1691 | if (!EVP_SignFinal(&md_ctx,&(p[2]), |
| 1692 | (unsigned int *)&i,pkey)) |
| 1693 | { |
| 1694 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_EVP); |
| 1695 | goto err; |
| 1696 | } |
| 1697 | s2n(i,p); |
| 1698 | n+=i+2; |
| 1699 | if (SSL_USE_SIGALGS(s)) |
| 1700 | n+= 2; |
| 1701 | } |
| 1702 | else |
| 1703 | { |
| 1704 | /* Is this error check actually needed? */ |
| 1705 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1706 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_UNKNOWN_PKEY_TYPE); |
| 1707 | goto f_err; |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n); |
| 1712 | } |
| 1713 | |
| 1714 | s->state = SSL3_ST_SW_KEY_EXCH_B; |
| 1715 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1716 | return ssl_do_write(s); |
| 1717 | f_err: |
| 1718 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1719 | err: |
| 1720 | #ifndef OPENSSL_NO_ECDH |
| 1721 | if (encodedPoint != NULL) OPENSSL_free(encodedPoint); |
| 1722 | BN_CTX_free(bn_ctx); |
| 1723 | #endif |
| 1724 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1725 | return(-1); |
| 1726 | } |
| 1727 | |
| 1728 | int ssl3_send_certificate_request(SSL *s) |
| 1729 | { |
| 1730 | unsigned char *p,*d; |
| 1731 | int i,j,nl,off,n; |
| 1732 | STACK_OF(X509_NAME) *sk=NULL; |
| 1733 | X509_NAME *name; |
| 1734 | BUF_MEM *buf; |
| 1735 | |
| 1736 | if (s->state == SSL3_ST_SW_CERT_REQ_A) |
| 1737 | { |
| 1738 | buf=s->init_buf; |
| 1739 | |
| 1740 | d=p=ssl_handshake_start(s); |
| 1741 | |
| 1742 | /* get the list of acceptable cert types */ |
| 1743 | p++; |
| 1744 | n=ssl3_get_req_cert_type(s,p); |
| 1745 | d[0]=n; |
| 1746 | p+=n; |
| 1747 | n++; |
| 1748 | |
| 1749 | if (SSL_USE_SIGALGS(s)) |
| 1750 | { |
| 1751 | const unsigned char *psigs; |
| 1752 | nl = tls12_get_psigalgs(s, &psigs); |
| 1753 | s2n(nl, p); |
| 1754 | memcpy(p, psigs, nl); |
| 1755 | p += nl; |
| 1756 | n += nl + 2; |
| 1757 | } |
| 1758 | |
| 1759 | off=n; |
| 1760 | p+=2; |
| 1761 | n+=2; |
| 1762 | |
| 1763 | sk=SSL_get_client_CA_list(s); |
| 1764 | nl=0; |
| 1765 | if (sk != NULL) |
| 1766 | { |
| 1767 | for (i=0; i<sk_X509_NAME_num(sk); i++) |
| 1768 | { |
| 1769 | name=sk_X509_NAME_value(sk,i); |
| 1770 | j=i2d_X509_NAME(name,NULL); |
| 1771 | if (!BUF_MEM_grow_clean(buf,SSL_HM_HEADER_LENGTH(s)+n+j+2)) |
| 1772 | { |
| 1773 | OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB); |
| 1774 | goto err; |
| 1775 | } |
| 1776 | p = ssl_handshake_start(s) + n; |
Alex Chernyakhovsky | 9c890d4 | 2014-07-05 00:53:11 -0400 | [diff] [blame] | 1777 | s2n(j,p); |
| 1778 | i2d_X509_NAME(name,&p); |
| 1779 | n+=2+j; |
| 1780 | nl+=2+j; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1781 | } |
| 1782 | } |
| 1783 | /* else no CA names */ |
| 1784 | p = ssl_handshake_start(s) + off; |
| 1785 | s2n(nl,p); |
| 1786 | |
| 1787 | ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n); |
| 1788 | |
| 1789 | #ifdef NETSCAPE_HANG_BUG |
| 1790 | if (!SSL_IS_DTLS(s)) |
| 1791 | { |
Adam Langley | ce1cfe1 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1792 | if (!BUF_MEM_grow_clean(buf, s->init_num + 4)) |
| 1793 | { |
| 1794 | OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB); |
| 1795 | goto err; |
| 1796 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1797 | p=(unsigned char *)s->init_buf->data + s->init_num; |
| 1798 | /* do the header */ |
| 1799 | *(p++)=SSL3_MT_SERVER_DONE; |
| 1800 | *(p++)=0; |
| 1801 | *(p++)=0; |
| 1802 | *(p++)=0; |
| 1803 | s->init_num += 4; |
| 1804 | } |
| 1805 | #endif |
| 1806 | |
| 1807 | s->state = SSL3_ST_SW_CERT_REQ_B; |
| 1808 | } |
| 1809 | |
| 1810 | /* SSL3_ST_SW_CERT_REQ_B */ |
| 1811 | return ssl_do_write(s); |
| 1812 | err: |
| 1813 | return(-1); |
| 1814 | } |
| 1815 | |
| 1816 | int ssl3_get_client_key_exchange(SSL *s) |
| 1817 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1818 | int al,ok; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1819 | long n; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1820 | CBS client_key_exchange; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1821 | unsigned long alg_k; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1822 | unsigned long alg_a; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1823 | uint8_t *premaster_secret = NULL; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1824 | size_t premaster_secret_len = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1825 | RSA *rsa=NULL; |
David Benjamin | 0aa0767 | 2014-07-24 15:34:14 -0400 | [diff] [blame] | 1826 | uint8_t *decrypt_buf = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1827 | EVP_PKEY *pkey=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1828 | #ifndef OPENSSL_NO_DH |
| 1829 | BIGNUM *pub=NULL; |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 1830 | DH *dh_srvr; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1831 | #endif |
| 1832 | |
| 1833 | #ifndef OPENSSL_NO_ECDH |
| 1834 | EC_KEY *srvr_ecdh = NULL; |
| 1835 | EVP_PKEY *clnt_pub_pkey = NULL; |
| 1836 | EC_POINT *clnt_ecpoint = NULL; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1837 | BN_CTX *bn_ctx = NULL; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1838 | unsigned int psk_len = 0; |
| 1839 | unsigned char psk[PSK_MAX_PSK_LEN]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1840 | #endif |
| 1841 | |
| 1842 | n=s->method->ssl_get_message(s, |
| 1843 | SSL3_ST_SR_KEY_EXCH_A, |
| 1844 | SSL3_ST_SR_KEY_EXCH_B, |
| 1845 | SSL3_MT_CLIENT_KEY_EXCHANGE, |
| 1846 | 2048, /* ??? */ |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 1847 | SSL_GET_MESSAGE_HASH_MESSAGE, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1848 | &ok); |
| 1849 | |
| 1850 | if (!ok) return((int)n); |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1851 | CBS_init(&client_key_exchange, s->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1852 | |
| 1853 | alg_k=s->s3->tmp.new_cipher->algorithm_mkey; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1854 | alg_a=s->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1855 | |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1856 | /* If using a PSK key exchange, prepare the pre-shared key. */ |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1857 | if (alg_a & SSL_aPSK) |
| 1858 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1859 | CBS psk_identity; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1860 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1861 | /* If using PSK, the ClientKeyExchange contains a |
| 1862 | * psk_identity. If PSK, then this is the only field |
| 1863 | * in the message. */ |
| 1864 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) || |
| 1865 | ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1866 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1867 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR); |
| 1868 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1869 | goto f_err; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1870 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1871 | |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1872 | if (s->psk_server_callback == NULL) |
| 1873 | { |
| 1874 | 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] | 1875 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1876 | goto f_err; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1877 | } |
| 1878 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1879 | if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN || |
| 1880 | CBS_contains_zero_byte(&psk_identity)) |
| 1881 | { |
| 1882 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DATA_LENGTH_TOO_LONG); |
| 1883 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1884 | goto f_err; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1885 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1886 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1887 | if (!CBS_strdup(&psk_identity, &s->session->psk_identity)) |
| 1888 | { |
| 1889 | al = SSL_AD_INTERNAL_ERROR; |
| 1890 | 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] | 1891 | goto f_err; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | /* Look up the key for the identity. */ |
| 1895 | 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] | 1896 | if (psk_len > PSK_MAX_PSK_LEN) |
| 1897 | { |
| 1898 | 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] | 1899 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1900 | goto f_err; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1901 | } |
| 1902 | else if (psk_len == 0) |
| 1903 | { |
| 1904 | /* PSK related to the given identity not found */ |
| 1905 | 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] | 1906 | al = SSL_AD_UNKNOWN_PSK_IDENTITY; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1907 | goto f_err; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1908 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1909 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1910 | |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1911 | /* Depending on the key exchange method, compute |premaster_secret| and |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 1912 | * |premaster_secret_len|. */ |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1913 | if (alg_k & SSL_kRSA) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1914 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1915 | CBS encrypted_premaster_secret; |
David Benjamin | 0aa0767 | 2014-07-24 15:34:14 -0400 | [diff] [blame] | 1916 | uint8_t rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH]; |
| 1917 | int decrypt_good_mask; |
| 1918 | uint8_t version_good; |
| 1919 | size_t rsa_size, decrypt_len, premaster_index, j; |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1920 | |
David Benjamin | b9cc33a | 2014-07-15 00:09:48 -0400 | [diff] [blame] | 1921 | pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey; |
| 1922 | if ( (pkey == NULL) || |
| 1923 | (pkey->type != EVP_PKEY_RSA) || |
| 1924 | (pkey->pkey.rsa == NULL)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1925 | { |
David Benjamin | b9cc33a | 2014-07-15 00:09:48 -0400 | [diff] [blame] | 1926 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1927 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_RSA_CERTIFICATE); |
| 1928 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1929 | } |
David Benjamin | b9cc33a | 2014-07-15 00:09:48 -0400 | [diff] [blame] | 1930 | rsa=pkey->pkey.rsa; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1931 | |
David Benjamin | cc23df5 | 2014-08-03 13:37:47 -0400 | [diff] [blame] | 1932 | /* TLS and [incidentally] DTLS{0xFEFF} */ |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1933 | if (s->version > SSL3_VERSION) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1934 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1935 | CBS copy = client_key_exchange; |
| 1936 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, |
| 1937 | &encrypted_premaster_secret) || |
| 1938 | CBS_len(&client_key_exchange) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1939 | { |
| 1940 | if (!(s->options & SSL_OP_TLS_D5_BUG)) |
| 1941 | { |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1942 | al = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1943 | 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] | 1944 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1945 | } |
| 1946 | else |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1947 | encrypted_premaster_secret = copy; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1948 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1949 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1950 | else |
| 1951 | encrypted_premaster_secret = client_key_exchange; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1952 | |
David Benjamin | 0aa0767 | 2014-07-24 15:34:14 -0400 | [diff] [blame] | 1953 | /* Reject overly short RSA keys because we want to be sure that |
| 1954 | * the buffer size makes it safe to iterate over the entire size |
| 1955 | * of a premaster secret (SSL_MAX_MASTER_KEY_LENGTH). The actual |
| 1956 | * expected size is larger due to RSA padding, but the bound is |
| 1957 | * sufficient to be safe. */ |
| 1958 | rsa_size = RSA_size(rsa); |
| 1959 | if (rsa_size < SSL_MAX_MASTER_KEY_LENGTH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1960 | { |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1961 | al = SSL_AD_DECRYPT_ERROR; |
| 1962 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECRYPTION_FAILED); |
| 1963 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1964 | } |
| 1965 | |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1966 | /* We must not leak whether a decryption failure occurs because |
| 1967 | * of Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see |
| 1968 | * RFC 2246, section 7.4.7.1). The code follows that advice of |
| 1969 | * the TLS RFC and generates a random premaster secret for the |
| 1970 | * case that the decrypt fails. See |
| 1971 | * https://tools.ietf.org/html/rfc5246#section-7.4.7.1 */ |
| 1972 | if (RAND_pseudo_bytes(rand_premaster_secret, |
| 1973 | sizeof(rand_premaster_secret)) <= 0) |
| 1974 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1975 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1976 | /* Allocate a buffer large enough for an RSA decryption. */ |
David Benjamin | 0aa0767 | 2014-07-24 15:34:14 -0400 | [diff] [blame] | 1977 | decrypt_buf = OPENSSL_malloc(rsa_size); |
| 1978 | if (decrypt_buf == NULL) |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1979 | { |
| 1980 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 1981 | goto err; |
| 1982 | } |
| 1983 | |
David Benjamin | 0aa0767 | 2014-07-24 15:34:14 -0400 | [diff] [blame] | 1984 | /* Decrypt with no padding. PKCS#1 padding will be removed as |
| 1985 | * part of the timing-sensitive code below. */ |
| 1986 | if (!RSA_decrypt(rsa, &decrypt_len, decrypt_buf, rsa_size, |
| 1987 | CBS_data(&encrypted_premaster_secret), |
| 1988 | CBS_len(&encrypted_premaster_secret), |
| 1989 | RSA_NO_PADDING)) |
| 1990 | { |
| 1991 | goto err; |
| 1992 | } |
| 1993 | if (decrypt_len != rsa_size) |
| 1994 | { |
| 1995 | /* This should never happen, but do a check so we do not |
| 1996 | * read uninitialized memory. */ |
| 1997 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_INTERNAL_ERROR); |
| 1998 | goto err; |
| 1999 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2000 | |
David Benjamin | 0aa0767 | 2014-07-24 15:34:14 -0400 | [diff] [blame] | 2001 | /* Remove the PKCS#1 padding and adjust decrypt_len as |
| 2002 | * appropriate. decrypt_good_mask will be zero if the premaster |
| 2003 | * if good and non-zero otherwise. */ |
| 2004 | decrypt_good_mask = RSA_message_index_PKCS1_type_2( |
| 2005 | decrypt_buf, decrypt_len, &premaster_index); |
| 2006 | decrypt_good_mask--; |
| 2007 | decrypt_len = decrypt_len - premaster_index; |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2008 | |
David Benjamin | 0aa0767 | 2014-07-24 15:34:14 -0400 | [diff] [blame] | 2009 | /* decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. */ |
| 2010 | decrypt_good_mask |= decrypt_len ^ SSL_MAX_MASTER_KEY_LENGTH; |
| 2011 | |
| 2012 | /* Copy over the unpadded premaster. Whatever the value of |
| 2013 | * |decrypt_good_mask|, copy as if the premaster were the right |
| 2014 | * length. It is important the memory access pattern be |
| 2015 | * constant. */ |
| 2016 | premaster_secret = BUF_memdup( |
| 2017 | decrypt_buf + (rsa_size - SSL_MAX_MASTER_KEY_LENGTH), |
| 2018 | SSL_MAX_MASTER_KEY_LENGTH); |
| 2019 | if (premaster_secret == NULL) |
| 2020 | { |
| 2021 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2022 | goto err; |
| 2023 | } |
| 2024 | OPENSSL_free(decrypt_buf); |
| 2025 | decrypt_buf = NULL; |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2026 | |
| 2027 | /* If the version in the decrypted pre-master secret is correct |
| 2028 | * then version_good will be zero. The Klima-Pokorny-Rosa |
| 2029 | * extension of Bleichenbacher's attack |
| 2030 | * (http://eprint.iacr.org/2003/052/) exploits the version |
| 2031 | * number check as a "bad version oracle". Thus version checks |
| 2032 | * are done in constant time and are treated like any other |
| 2033 | * decryption error. */ |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2034 | version_good = premaster_secret[0] ^ (s->client_version>>8); |
| 2035 | version_good |= premaster_secret[1] ^ (s->client_version&0xff); |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2036 | |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2037 | /* If any bits in version_good are set then they'll poision |
| 2038 | * decrypt_good_mask and cause rand_premaster_secret to be |
| 2039 | * used. */ |
| 2040 | decrypt_good_mask |= version_good; |
| 2041 | |
| 2042 | /* decrypt_good_mask will be zero iff decrypt_len == |
| 2043 | * SSL_MAX_MASTER_KEY_LENGTH and the version check passed. We |
| 2044 | * fold the bottom 32 bits of it with an OR so that the LSB |
| 2045 | * will be zero iff everything is good. This assumes that we'll |
| 2046 | * never decrypt a value > 2**31 bytes, which seems safe. */ |
| 2047 | decrypt_good_mask |= decrypt_good_mask >> 16; |
| 2048 | decrypt_good_mask |= decrypt_good_mask >> 8; |
| 2049 | decrypt_good_mask |= decrypt_good_mask >> 4; |
| 2050 | decrypt_good_mask |= decrypt_good_mask >> 2; |
| 2051 | decrypt_good_mask |= decrypt_good_mask >> 1; |
| 2052 | /* Now select only the LSB and subtract one. If decrypt_len == |
| 2053 | * SSL_MAX_MASTER_KEY_LENGTH and the version check passed then |
| 2054 | * decrypt_good_mask will be all ones. Otherwise it'll be all |
| 2055 | * zeros. */ |
| 2056 | decrypt_good_mask &= 1; |
| 2057 | decrypt_good_mask--; |
| 2058 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2059 | /* Now copy rand_premaster_secret over premaster_secret using |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2060 | * decrypt_good_mask. */ |
| 2061 | for (j = 0; j < sizeof(rand_premaster_secret); j++) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2062 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2063 | premaster_secret[j] = (premaster_secret[j] & decrypt_good_mask) | |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2064 | (rand_premaster_secret[j] & ~decrypt_good_mask); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2065 | } |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2066 | |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2067 | premaster_secret_len = sizeof(rand_premaster_secret); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2068 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2069 | #ifndef OPENSSL_NO_DH |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 2070 | else if (alg_k & SSL_kEDH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2071 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2072 | CBS dh_Yc; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2073 | int dh_len; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2074 | |
| 2075 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &dh_Yc) || |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2076 | CBS_len(&dh_Yc) == 0 || |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2077 | CBS_len(&client_key_exchange) != 0) |
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 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG); |
| 2080 | al = SSL_R_DECODE_ERROR; |
| 2081 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2082 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2083 | |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 2084 | if (s->s3->tmp.dh == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2085 | { |
| 2086 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2087 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_TMP_DH_KEY); |
| 2088 | goto f_err; |
| 2089 | } |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 2090 | dh_srvr=s->s3->tmp.dh; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2091 | |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2092 | pub = BN_bin2bn(CBS_data(&dh_Yc), CBS_len(&dh_Yc), NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2093 | if (pub == NULL) |
| 2094 | { |
| 2095 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_BN_LIB); |
| 2096 | goto err; |
| 2097 | } |
| 2098 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2099 | /* Allocate a buffer for the premaster secret. */ |
| 2100 | premaster_secret = OPENSSL_malloc(DH_size(dh_srvr)); |
| 2101 | if (premaster_secret == NULL) |
| 2102 | { |
| 2103 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2104 | goto err; |
| 2105 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2106 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2107 | dh_len = DH_compute_key(premaster_secret, pub, dh_srvr); |
| 2108 | if (dh_len <= 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2109 | { |
| 2110 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_DH_LIB); |
| 2111 | BN_clear_free(pub); |
| 2112 | goto err; |
| 2113 | } |
| 2114 | |
| 2115 | DH_free(s->s3->tmp.dh); |
| 2116 | s->s3->tmp.dh=NULL; |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2117 | BN_clear_free(pub); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2118 | pub=NULL; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2119 | |
| 2120 | premaster_secret_len = dh_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2121 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2122 | #endif |
| 2123 | |
| 2124 | #ifndef OPENSSL_NO_ECDH |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 2125 | else if (alg_k & SSL_kEECDH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2126 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2127 | int field_size = 0, ecdh_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2128 | const EC_KEY *tkey; |
| 2129 | const EC_GROUP *group; |
| 2130 | const BIGNUM *priv_key; |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2131 | CBS ecdh_Yc; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2132 | |
| 2133 | /* initialize structures for server's ECDH key pair */ |
| 2134 | if ((srvr_ecdh = EC_KEY_new()) == NULL) |
| 2135 | { |
| 2136 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2137 | goto err; |
| 2138 | } |
| 2139 | |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 2140 | /* Use the ephermeral values we saved when generating the |
| 2141 | * ServerKeyExchange msg. */ |
| 2142 | tkey = s->s3->tmp.ecdh; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2143 | |
| 2144 | group = EC_KEY_get0_group(tkey); |
| 2145 | priv_key = EC_KEY_get0_private_key(tkey); |
| 2146 | |
| 2147 | if (!EC_KEY_set_group(srvr_ecdh, group) || |
| 2148 | !EC_KEY_set_private_key(srvr_ecdh, priv_key)) |
| 2149 | { |
| 2150 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB); |
| 2151 | goto err; |
| 2152 | } |
| 2153 | |
| 2154 | /* Let's get client's public key */ |
| 2155 | if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) |
| 2156 | { |
| 2157 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2158 | goto err; |
| 2159 | } |
| 2160 | |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2161 | /* Get client's public key from encoded point |
| 2162 | * in the ClientKeyExchange message. |
| 2163 | */ |
| 2164 | if (!CBS_get_u8_length_prefixed(&client_key_exchange, &ecdh_Yc) || |
| 2165 | CBS_len(&client_key_exchange) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2166 | { |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2167 | al = SSL_AD_DECODE_ERROR; |
| 2168 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR); |
| 2169 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2170 | } |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2171 | |
| 2172 | if ((bn_ctx = BN_CTX_new()) == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2173 | { |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2174 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2175 | goto err; |
| 2176 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2177 | |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2178 | if (!EC_POINT_oct2point(group, clnt_ecpoint, |
| 2179 | CBS_data(&ecdh_Yc), CBS_len(&ecdh_Yc), bn_ctx)) |
| 2180 | { |
| 2181 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB); |
| 2182 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2183 | } |
| 2184 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2185 | /* Allocate a buffer for both the secret and the PSK. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2186 | field_size = EC_GROUP_get_degree(group); |
| 2187 | if (field_size <= 0) |
| 2188 | { |
| 2189 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB); |
| 2190 | goto err; |
| 2191 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2192 | |
| 2193 | ecdh_len = (field_size + 7) / 8; |
| 2194 | premaster_secret = OPENSSL_malloc(ecdh_len); |
| 2195 | if (premaster_secret == NULL) |
| 2196 | { |
| 2197 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2198 | goto err; |
| 2199 | } |
| 2200 | |
| 2201 | /* Compute the shared pre-master secret */ |
| 2202 | ecdh_len = ECDH_compute_key(premaster_secret, |
| 2203 | ecdh_len, clnt_ecpoint, srvr_ecdh, NULL); |
| 2204 | if (ecdh_len <= 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2205 | { |
| 2206 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB); |
| 2207 | goto err; |
| 2208 | } |
| 2209 | |
| 2210 | EVP_PKEY_free(clnt_pub_pkey); |
| 2211 | EC_POINT_free(clnt_ecpoint); |
| 2212 | EC_KEY_free(srvr_ecdh); |
| 2213 | BN_CTX_free(bn_ctx); |
| 2214 | EC_KEY_free(s->s3->tmp.ecdh); |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2215 | s->s3->tmp.ecdh = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2216 | |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2217 | premaster_secret_len = ecdh_len; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2218 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2219 | #endif |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2220 | else if (alg_k & SSL_kPSK) |
| 2221 | { |
| 2222 | /* For plain PSK, other_secret is a block of 0s with the same |
| 2223 | * length as the pre-shared key. */ |
| 2224 | premaster_secret_len = psk_len; |
| 2225 | premaster_secret = OPENSSL_malloc(premaster_secret_len); |
| 2226 | if (premaster_secret == NULL) |
| 2227 | { |
| 2228 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2229 | goto err; |
| 2230 | } |
| 2231 | memset(premaster_secret, 0, premaster_secret_len); |
| 2232 | } |
David Benjamin | 820c74a | 2014-07-13 04:22:14 -0400 | [diff] [blame] | 2233 | else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2234 | { |
| 2235 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2236 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_UNKNOWN_CIPHER_TYPE); |
| 2237 | goto f_err; |
| 2238 | } |
| 2239 | |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2240 | /* For a PSK cipher suite, the actual pre-master secret is combined with |
| 2241 | * the pre-shared key. */ |
| 2242 | if (alg_a & SSL_aPSK) |
| 2243 | { |
| 2244 | CBB new_premaster, child; |
| 2245 | uint8_t *new_data; |
| 2246 | size_t new_len; |
| 2247 | |
| 2248 | if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len)) |
| 2249 | { |
| 2250 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2251 | goto err; |
| 2252 | } |
| 2253 | if (!CBB_add_u16_length_prefixed(&new_premaster, &child) || |
| 2254 | !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) || |
| 2255 | !CBB_add_u16_length_prefixed(&new_premaster, &child) || |
| 2256 | !CBB_add_bytes(&child, psk, psk_len) || |
| 2257 | !CBB_finish(&new_premaster, &new_data, &new_len)) |
| 2258 | { |
| 2259 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_INTERNAL_ERROR); |
| 2260 | CBB_cleanup(&new_premaster); |
| 2261 | goto err; |
| 2262 | } |
| 2263 | |
| 2264 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 2265 | OPENSSL_free(premaster_secret); |
| 2266 | premaster_secret = new_data; |
| 2267 | premaster_secret_len = new_len; |
| 2268 | } |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2269 | |
| 2270 | /* Compute the master secret */ |
| 2271 | s->session->master_key_length = s->method->ssl3_enc |
| 2272 | ->generate_master_secret(s, |
| 2273 | s->session->master_key, premaster_secret, premaster_secret_len); |
| 2274 | |
| 2275 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 2276 | OPENSSL_free(premaster_secret); |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2277 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2278 | f_err: |
| 2279 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2280 | err: |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2281 | if (premaster_secret) |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2282 | { |
| 2283 | if (premaster_secret_len) |
| 2284 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 2285 | OPENSSL_free(premaster_secret); |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2286 | } |
David Benjamin | 0aa0767 | 2014-07-24 15:34:14 -0400 | [diff] [blame] | 2287 | if (decrypt_buf) |
| 2288 | OPENSSL_free(decrypt_buf); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2289 | #ifndef OPENSSL_NO_ECDH |
| 2290 | EVP_PKEY_free(clnt_pub_pkey); |
| 2291 | EC_POINT_free(clnt_ecpoint); |
| 2292 | if (srvr_ecdh != NULL) |
| 2293 | EC_KEY_free(srvr_ecdh); |
| 2294 | BN_CTX_free(bn_ctx); |
| 2295 | #endif |
| 2296 | return(-1); |
| 2297 | } |
| 2298 | |
| 2299 | int ssl3_get_cert_verify(SSL *s) |
| 2300 | { |
| 2301 | EVP_PKEY *pkey=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2302 | int al,ok,ret=0; |
| 2303 | long n; |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2304 | CBS certificate_verify, signature; |
| 2305 | int type = 0; |
David Benjamin | 6553b37 | 2014-07-22 14:11:30 -0400 | [diff] [blame] | 2306 | X509 *peer = s->session->peer; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2307 | const EVP_MD *md = NULL; |
| 2308 | EVP_MD_CTX mctx; |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2309 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2310 | EVP_MD_CTX_init(&mctx); |
| 2311 | |
David Benjamin | ef86550 | 2014-08-24 02:48:34 -0400 | [diff] [blame] | 2312 | /* Only RSA and ECDSA client certificates are supported, so a |
| 2313 | * CertificateVerify is required if and only if there's a |
| 2314 | * client certificate. */ |
| 2315 | if (peer == NULL) |
David Benjamin | 6553b37 | 2014-07-22 14:11:30 -0400 | [diff] [blame] | 2316 | { |
| 2317 | ret = 1; |
David Benjamin | 734fca0 | 2014-08-05 13:06:20 -0400 | [diff] [blame] | 2318 | goto done_with_buffer; |
David Benjamin | 6553b37 | 2014-07-22 14:11:30 -0400 | [diff] [blame] | 2319 | } |
| 2320 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2321 | n=s->method->ssl_get_message(s, |
| 2322 | SSL3_ST_SR_CERT_VRFY_A, |
| 2323 | SSL3_ST_SR_CERT_VRFY_B, |
David Benjamin | 6553b37 | 2014-07-22 14:11:30 -0400 | [diff] [blame] | 2324 | SSL3_MT_CERTIFICATE_VERIFY, |
Adam Langley | 887b1c3 | 2014-07-24 18:00:06 -0700 | [diff] [blame] | 2325 | SSL3_RT_MAX_PLAIN_LENGTH, |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 2326 | SSL_GET_MESSAGE_HASH_MESSAGE, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2327 | &ok); |
| 2328 | |
David Benjamin | 734fca0 | 2014-08-05 13:06:20 -0400 | [diff] [blame] | 2329 | if (!ok) |
| 2330 | { |
| 2331 | ret = (int)n; |
| 2332 | goto done; |
| 2333 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2334 | |
David Benjamin | ef86550 | 2014-08-24 02:48:34 -0400 | [diff] [blame] | 2335 | pkey = X509_get_pubkey(peer); |
| 2336 | type = X509_certificate_type(peer,pkey); |
| 2337 | if (!(type & EVP_PKT_SIGN)) |
| 2338 | { |
| 2339 | /* If it's not a signing certificate, it's unsupported. */ |
| 2340 | al = SSL_AD_UNSUPPORTED_CERTIFICATE; |
| 2341 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE); |
| 2342 | goto f_err; |
| 2343 | } |
| 2344 | |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2345 | CBS_init(&certificate_verify, s->init_msg, n); |
| 2346 | |
| 2347 | /* We now have a signature that we need to verify. */ |
| 2348 | /* TODO(davidben): This should share code with |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 2349 | * ssl3_get_server_key_exchange. */ |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 2350 | |
Adam Langley | 3213bed | 2014-06-23 12:26:07 -0700 | [diff] [blame] | 2351 | if (SSL_USE_SIGALGS(s)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2352 | { |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 2353 | if (!tls12_check_peer_sigalg(&md, &al, s, &certificate_verify, pkey)) |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2354 | goto f_err; |
Adam Langley | 3213bed | 2014-06-23 12:26:07 -0700 | [diff] [blame] | 2355 | } |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2356 | |
| 2357 | if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) || |
| 2358 | CBS_len(&certificate_verify) != 0) |
Adam Langley | 3213bed | 2014-06-23 12:26:07 -0700 | [diff] [blame] | 2359 | { |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2360 | al = SSL_AD_DECODE_ERROR; |
| 2361 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_DECODE_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2362 | goto f_err; |
| 2363 | } |
| 2364 | |
| 2365 | if (SSL_USE_SIGALGS(s)) |
| 2366 | { |
Adam Langley | 37a623c | 2014-07-18 09:28:40 -0700 | [diff] [blame] | 2367 | size_t hdatalen; |
| 2368 | const uint8_t *hdata; |
| 2369 | if (!BIO_mem_contents(s->s3->handshake_buffer, &hdata, &hdatalen)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2370 | { |
| 2371 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, ERR_R_INTERNAL_ERROR); |
| 2372 | al=SSL_AD_INTERNAL_ERROR; |
| 2373 | goto f_err; |
| 2374 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2375 | if (!EVP_VerifyInit_ex(&mctx, md, NULL) |
| 2376 | || !EVP_VerifyUpdate(&mctx, hdata, hdatalen)) |
| 2377 | { |
| 2378 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, ERR_R_EVP_LIB); |
| 2379 | al=SSL_AD_INTERNAL_ERROR; |
| 2380 | goto f_err; |
| 2381 | } |
| 2382 | |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2383 | if (EVP_VerifyFinal(&mctx, |
| 2384 | CBS_data(&signature), CBS_len(&signature), |
| 2385 | pkey) <= 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2386 | { |
| 2387 | al=SSL_AD_DECRYPT_ERROR; |
| 2388 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_SIGNATURE); |
| 2389 | goto f_err; |
| 2390 | } |
| 2391 | } |
| 2392 | else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2393 | if (pkey->type == EVP_PKEY_RSA) |
| 2394 | { |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2395 | if (!RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md, |
| 2396 | MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, |
| 2397 | CBS_data(&signature), CBS_len(&signature), |
| 2398 | pkey->pkey.rsa)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2399 | { |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2400 | al = SSL_AD_DECRYPT_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2401 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_RSA_SIGNATURE); |
| 2402 | goto f_err; |
| 2403 | } |
| 2404 | } |
| 2405 | else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2406 | #ifndef OPENSSL_NO_ECDSA |
| 2407 | if (pkey->type == EVP_PKEY_EC) |
| 2408 | { |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2409 | if (!ECDSA_verify(pkey->save_type, |
| 2410 | &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]), |
| 2411 | SHA_DIGEST_LENGTH, |
| 2412 | CBS_data(&signature), CBS_len(&signature), |
| 2413 | pkey->pkey.ec)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2414 | { |
| 2415 | /* bad signature */ |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2416 | al = SSL_AD_DECRYPT_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2417 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_ECDSA_SIGNATURE); |
| 2418 | goto f_err; |
| 2419 | } |
| 2420 | } |
| 2421 | else |
| 2422 | #endif |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2423 | { |
| 2424 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, ERR_R_INTERNAL_ERROR); |
| 2425 | al=SSL_AD_UNSUPPORTED_CERTIFICATE; |
| 2426 | goto f_err; |
| 2427 | } |
| 2428 | |
| 2429 | |
| 2430 | ret=1; |
| 2431 | if (0) |
| 2432 | { |
| 2433 | f_err: |
| 2434 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 2435 | } |
David Benjamin | 734fca0 | 2014-08-05 13:06:20 -0400 | [diff] [blame] | 2436 | done_with_buffer: |
| 2437 | /* There is no more need for the handshake buffer. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2438 | if (s->s3->handshake_buffer) |
| 2439 | { |
| 2440 | BIO_free(s->s3->handshake_buffer); |
| 2441 | s->s3->handshake_buffer = NULL; |
| 2442 | s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE; |
| 2443 | } |
David Benjamin | 734fca0 | 2014-08-05 13:06:20 -0400 | [diff] [blame] | 2444 | done: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2445 | EVP_MD_CTX_cleanup(&mctx); |
| 2446 | EVP_PKEY_free(pkey); |
| 2447 | return(ret); |
| 2448 | } |
| 2449 | |
| 2450 | int ssl3_get_client_certificate(SSL *s) |
| 2451 | { |
| 2452 | int i,ok,al,ret= -1; |
| 2453 | X509 *x=NULL; |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2454 | unsigned long n; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2455 | STACK_OF(X509) *sk=NULL; |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2456 | SHA256_CTX sha256; |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2457 | CBS certificate_msg, certificate_list; |
| 2458 | int is_first_certificate = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2459 | |
| 2460 | n=s->method->ssl_get_message(s, |
| 2461 | SSL3_ST_SR_CERT_A, |
| 2462 | SSL3_ST_SR_CERT_B, |
| 2463 | -1, |
| 2464 | s->max_cert_list, |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 2465 | SSL_GET_MESSAGE_HASH_MESSAGE, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2466 | &ok); |
| 2467 | |
| 2468 | if (!ok) return((int)n); |
| 2469 | |
| 2470 | if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) |
| 2471 | { |
| 2472 | if ( (s->verify_mode & SSL_VERIFY_PEER) && |
| 2473 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) |
| 2474 | { |
| 2475 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
| 2476 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2477 | goto f_err; |
| 2478 | } |
| 2479 | /* If tls asked for a client cert, the client must return a 0 list */ |
| 2480 | if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) |
| 2481 | { |
| 2482 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST); |
| 2483 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 2484 | goto f_err; |
| 2485 | } |
| 2486 | s->s3->tmp.reuse_message=1; |
| 2487 | return(1); |
| 2488 | } |
| 2489 | |
| 2490 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) |
| 2491 | { |
| 2492 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 2493 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_WRONG_MESSAGE_TYPE); |
| 2494 | goto f_err; |
| 2495 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2496 | |
David Benjamin | 51b1f74 | 2014-07-12 16:31:12 -0400 | [diff] [blame] | 2497 | CBS_init(&certificate_msg, s->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2498 | |
| 2499 | if ((sk=sk_X509_new_null()) == NULL) |
| 2500 | { |
| 2501 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE); |
| 2502 | goto err; |
| 2503 | } |
| 2504 | |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2505 | if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) || |
| 2506 | CBS_len(&certificate_msg) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2507 | { |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2508 | al = SSL_AD_DECODE_ERROR; |
| 2509 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2510 | goto f_err; |
| 2511 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2512 | |
| 2513 | while (CBS_len(&certificate_list) > 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2514 | { |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2515 | CBS certificate; |
| 2516 | const uint8_t *data; |
| 2517 | |
| 2518 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2519 | { |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2520 | al = SSL_AD_DECODE_ERROR; |
| 2521 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2522 | goto f_err; |
| 2523 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2524 | if (is_first_certificate && s->ctx->retain_only_sha256_of_client_certs) |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2525 | { |
| 2526 | /* If this is the first certificate, and we don't want |
| 2527 | * to keep peer certificates in memory, then we hash it |
| 2528 | * right away. */ |
| 2529 | SHA256_Init(&sha256); |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2530 | SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate)); |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2531 | SHA256_Final(s->session->peer_sha256, &sha256); |
| 2532 | s->session->peer_sha256_valid = 1; |
| 2533 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2534 | is_first_certificate = 0; |
| 2535 | data = CBS_data(&certificate); |
| 2536 | x = d2i_X509(NULL, &data, CBS_len(&certificate)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2537 | if (x == NULL) |
| 2538 | { |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2539 | al = SSL_AD_BAD_CERTIFICATE; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2540 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_ASN1_LIB); |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2541 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2542 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2543 | if (!CBS_skip(&certificate, data - CBS_data(&certificate))) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2544 | { |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2545 | al = SSL_AD_INTERNAL_ERROR; |
| 2546 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_INTERNAL_ERROR); |
| 2547 | goto f_err; |
| 2548 | } |
| 2549 | if (CBS_len(&certificate) != 0) |
| 2550 | { |
| 2551 | al = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2552 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_CERT_LENGTH_MISMATCH); |
| 2553 | goto f_err; |
| 2554 | } |
| 2555 | if (!sk_X509_push(sk,x)) |
| 2556 | { |
| 2557 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE); |
| 2558 | goto err; |
| 2559 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2560 | x = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | if (sk_X509_num(sk) <= 0) |
| 2564 | { |
| 2565 | /* TLS does not mind 0 certs returned */ |
| 2566 | if (s->version == SSL3_VERSION) |
| 2567 | { |
| 2568 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2569 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_NO_CERTIFICATES_RETURNED); |
| 2570 | goto f_err; |
| 2571 | } |
| 2572 | /* Fail for TLS only if we required a certificate */ |
| 2573 | else if ((s->verify_mode & SSL_VERIFY_PEER) && |
| 2574 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) |
| 2575 | { |
| 2576 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
| 2577 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2578 | goto f_err; |
| 2579 | } |
| 2580 | /* No client certificate so digest cached records */ |
| 2581 | if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) |
| 2582 | { |
| 2583 | al=SSL_AD_INTERNAL_ERROR; |
| 2584 | goto f_err; |
| 2585 | } |
| 2586 | } |
| 2587 | else |
| 2588 | { |
| 2589 | i=ssl_verify_cert_chain(s,sk); |
| 2590 | if (i <= 0) |
| 2591 | { |
| 2592 | al=ssl_verify_alarm_type(s->verify_result); |
| 2593 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 2594 | goto f_err; |
| 2595 | } |
| 2596 | } |
| 2597 | |
| 2598 | if (s->session->peer != NULL) /* This should not be needed */ |
| 2599 | X509_free(s->session->peer); |
| 2600 | s->session->peer=sk_X509_shift(sk); |
| 2601 | s->session->verify_result = s->verify_result; |
| 2602 | |
| 2603 | /* With the current implementation, sess_cert will always be NULL |
| 2604 | * when we arrive here. */ |
| 2605 | if (s->session->sess_cert == NULL) |
| 2606 | { |
| 2607 | s->session->sess_cert = ssl_sess_cert_new(); |
| 2608 | if (s->session->sess_cert == NULL) |
| 2609 | { |
| 2610 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE); |
| 2611 | goto err; |
| 2612 | } |
| 2613 | } |
| 2614 | if (s->session->sess_cert->cert_chain != NULL) |
| 2615 | sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free); |
| 2616 | s->session->sess_cert->cert_chain=sk; |
| 2617 | /* Inconsistency alert: cert_chain does *not* include the |
| 2618 | * peer's own certificate, while we do include it in s3_clnt.c */ |
| 2619 | |
| 2620 | sk=NULL; |
| 2621 | |
| 2622 | ret=1; |
| 2623 | if (0) |
| 2624 | { |
| 2625 | f_err: |
| 2626 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 2627 | } |
| 2628 | err: |
| 2629 | if (x != NULL) X509_free(x); |
| 2630 | if (sk != NULL) sk_X509_pop_free(sk,X509_free); |
| 2631 | return(ret); |
| 2632 | } |
| 2633 | |
| 2634 | int ssl3_send_server_certificate(SSL *s) |
| 2635 | { |
| 2636 | CERT_PKEY *cpk; |
| 2637 | |
| 2638 | if (s->state == SSL3_ST_SW_CERT_A) |
| 2639 | { |
| 2640 | cpk=ssl_get_server_send_pkey(s); |
| 2641 | if (cpk == NULL) |
| 2642 | { |
David Benjamin | d26aea6 | 2014-07-12 00:13:56 -0400 | [diff] [blame] | 2643 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_certificate, ERR_R_INTERNAL_ERROR); |
| 2644 | return(0); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | ssl3_output_cert_chain(s,cpk); |
| 2648 | s->state=SSL3_ST_SW_CERT_B; |
| 2649 | } |
| 2650 | |
| 2651 | /* SSL3_ST_SW_CERT_B */ |
| 2652 | return ssl_do_write(s); |
| 2653 | } |
| 2654 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2655 | /* send a new session ticket (not necessarily for a new session) */ |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 2656 | int ssl3_send_new_session_ticket(SSL *s) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2657 | { |
| 2658 | if (s->state == SSL3_ST_SW_SESSION_TICKET_A) |
| 2659 | { |
| 2660 | unsigned char *p, *senc, *macstart; |
| 2661 | const unsigned char *const_p; |
| 2662 | int len, slen_full, slen; |
| 2663 | SSL_SESSION *sess; |
| 2664 | unsigned int hlen; |
| 2665 | EVP_CIPHER_CTX ctx; |
| 2666 | HMAC_CTX hctx; |
| 2667 | SSL_CTX *tctx = s->initial_ctx; |
| 2668 | unsigned char iv[EVP_MAX_IV_LENGTH]; |
| 2669 | unsigned char key_name[16]; |
| 2670 | |
| 2671 | /* get session encoding length */ |
| 2672 | slen_full = i2d_SSL_SESSION(s->session, NULL); |
| 2673 | /* Some length values are 16 bits, so forget it if session is |
| 2674 | * too long |
| 2675 | */ |
| 2676 | if (slen_full > 0xFF00) |
| 2677 | return -1; |
| 2678 | senc = OPENSSL_malloc(slen_full); |
| 2679 | if (!senc) |
| 2680 | return -1; |
| 2681 | p = senc; |
| 2682 | i2d_SSL_SESSION(s->session, &p); |
| 2683 | |
| 2684 | /* create a fresh copy (not shared with other threads) to clean up */ |
| 2685 | const_p = senc; |
| 2686 | sess = d2i_SSL_SESSION(NULL, &const_p, slen_full); |
| 2687 | if (sess == NULL) |
| 2688 | { |
| 2689 | OPENSSL_free(senc); |
| 2690 | return -1; |
| 2691 | } |
| 2692 | sess->session_id_length = 0; /* ID is irrelevant for the ticket */ |
| 2693 | |
| 2694 | slen = i2d_SSL_SESSION(sess, NULL); |
| 2695 | if (slen > slen_full) /* shouldn't ever happen */ |
| 2696 | { |
| 2697 | OPENSSL_free(senc); |
| 2698 | return -1; |
| 2699 | } |
| 2700 | p = senc; |
| 2701 | i2d_SSL_SESSION(sess, &p); |
| 2702 | SSL_SESSION_free(sess); |
| 2703 | |
| 2704 | /* Grow buffer if need be: the length calculation is as |
| 2705 | * follows handshake_header_length + |
| 2706 | * 4 (ticket lifetime hint) + 2 (ticket length) + |
| 2707 | * 16 (key name) + max_iv_len (iv length) + |
| 2708 | * session_length + max_enc_block_size (max encrypted session |
| 2709 | * length) + max_md_size (HMAC). |
| 2710 | */ |
| 2711 | if (!BUF_MEM_grow(s->init_buf, |
| 2712 | SSL_HM_HEADER_LENGTH(s) + 22 + EVP_MAX_IV_LENGTH + |
| 2713 | EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen)) |
| 2714 | return -1; |
| 2715 | p = ssl_handshake_start(s); |
| 2716 | EVP_CIPHER_CTX_init(&ctx); |
| 2717 | HMAC_CTX_init(&hctx); |
| 2718 | /* Initialize HMAC and cipher contexts. If callback present |
| 2719 | * it does all the work otherwise use generated values |
| 2720 | * from parent ctx. |
| 2721 | */ |
| 2722 | if (tctx->tlsext_ticket_key_cb) |
| 2723 | { |
| 2724 | if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, |
| 2725 | &hctx, 1) < 0) |
| 2726 | { |
| 2727 | OPENSSL_free(senc); |
| 2728 | return -1; |
| 2729 | } |
| 2730 | } |
| 2731 | else |
| 2732 | { |
| 2733 | RAND_pseudo_bytes(iv, 16); |
| 2734 | EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, |
| 2735 | tctx->tlsext_tick_aes_key, iv); |
| 2736 | HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, |
| 2737 | tlsext_tick_md(), NULL); |
| 2738 | memcpy(key_name, tctx->tlsext_tick_key_name, 16); |
| 2739 | } |
| 2740 | |
| 2741 | /* Ticket lifetime hint (advisory only): |
| 2742 | * We leave this unspecified for resumed session (for simplicity), |
| 2743 | * and guess that tickets for new sessions will live as long |
| 2744 | * as their sessions. */ |
| 2745 | l2n(s->hit ? 0 : s->session->timeout, p); |
| 2746 | |
| 2747 | /* Skip ticket length for now */ |
| 2748 | p += 2; |
| 2749 | /* Output key name */ |
| 2750 | macstart = p; |
| 2751 | memcpy(p, key_name, 16); |
| 2752 | p += 16; |
| 2753 | /* output IV */ |
| 2754 | memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx)); |
| 2755 | p += EVP_CIPHER_CTX_iv_length(&ctx); |
| 2756 | /* Encrypt session data */ |
| 2757 | EVP_EncryptUpdate(&ctx, p, &len, senc, slen); |
| 2758 | p += len; |
| 2759 | EVP_EncryptFinal_ex(&ctx, p, &len); |
| 2760 | p += len; |
| 2761 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2762 | |
| 2763 | HMAC_Update(&hctx, macstart, p - macstart); |
| 2764 | HMAC_Final(&hctx, p, &hlen); |
| 2765 | HMAC_CTX_cleanup(&hctx); |
| 2766 | |
| 2767 | p += hlen; |
| 2768 | /* Now write out lengths: p points to end of data written */ |
| 2769 | /* Total length */ |
| 2770 | len = p - ssl_handshake_start(s); |
| 2771 | ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len); |
| 2772 | /* Skip ticket lifetime hint */ |
| 2773 | p = ssl_handshake_start(s) + 4; |
| 2774 | s2n(len - 6, p); |
| 2775 | s->state=SSL3_ST_SW_SESSION_TICKET_B; |
| 2776 | OPENSSL_free(senc); |
| 2777 | } |
| 2778 | |
| 2779 | /* SSL3_ST_SW_SESSION_TICKET_B */ |
| 2780 | return ssl_do_write(s); |
| 2781 | } |
| 2782 | |
| 2783 | int ssl3_send_cert_status(SSL *s) |
| 2784 | { |
| 2785 | if (s->state == SSL3_ST_SW_CERT_STATUS_A) |
| 2786 | { |
| 2787 | unsigned char *p; |
| 2788 | /* Grow buffer if need be: the length calculation is as |
| 2789 | * follows 1 (message type) + 3 (message length) + |
| 2790 | * 1 (ocsp response type) + 3 (ocsp response length) |
| 2791 | * + (ocsp response) |
| 2792 | */ |
| 2793 | if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen)) |
| 2794 | return -1; |
| 2795 | |
| 2796 | p=(unsigned char *)s->init_buf->data; |
| 2797 | |
| 2798 | /* do the header */ |
| 2799 | *(p++)=SSL3_MT_CERTIFICATE_STATUS; |
| 2800 | /* message length */ |
| 2801 | l2n3(s->tlsext_ocsp_resplen + 4, p); |
| 2802 | /* status type */ |
| 2803 | *(p++)= s->tlsext_status_type; |
| 2804 | /* length of OCSP response */ |
| 2805 | l2n3(s->tlsext_ocsp_resplen, p); |
| 2806 | /* actual response */ |
| 2807 | memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen); |
| 2808 | /* number of bytes to write */ |
| 2809 | s->init_num = 8 + s->tlsext_ocsp_resplen; |
| 2810 | s->state=SSL3_ST_SW_CERT_STATUS_B; |
| 2811 | s->init_off = 0; |
| 2812 | } |
| 2813 | |
| 2814 | /* SSL3_ST_SW_CERT_STATUS_B */ |
| 2815 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 2816 | } |
| 2817 | |
| 2818 | # ifndef OPENSSL_NO_NEXTPROTONEG |
| 2819 | /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It |
| 2820 | * sets the next_proto member in s if found */ |
| 2821 | int ssl3_get_next_proto(SSL *s) |
| 2822 | { |
| 2823 | int ok; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2824 | long n; |
David Benjamin | ced551f | 2014-07-11 23:16:16 -0400 | [diff] [blame] | 2825 | CBS next_protocol, selected_protocol, padding; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2826 | |
| 2827 | /* Clients cannot send a NextProtocol message if we didn't see the |
| 2828 | * extension in their ClientHello */ |
| 2829 | if (!s->s3->next_proto_neg_seen) |
| 2830 | { |
| 2831 | OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto, SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION); |
| 2832 | return -1; |
| 2833 | } |
| 2834 | |
| 2835 | n=s->method->ssl_get_message(s, |
| 2836 | SSL3_ST_SR_NEXT_PROTO_A, |
| 2837 | SSL3_ST_SR_NEXT_PROTO_B, |
| 2838 | SSL3_MT_NEXT_PROTO, |
| 2839 | 514, /* See the payload format below */ |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 2840 | SSL_GET_MESSAGE_HASH_MESSAGE, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2841 | &ok); |
| 2842 | |
| 2843 | if (!ok) |
| 2844 | return((int)n); |
| 2845 | |
| 2846 | /* s->state doesn't reflect whether ChangeCipherSpec has been received |
| 2847 | * 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] | 2848 | * by ssl3_get_finished). |
| 2849 | * TODO(davidben): Is this check now redundant with |
| 2850 | * SSL3_FLAGS_EXPECT_CCS? */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2851 | if (!s->s3->change_cipher_spec) |
| 2852 | { |
| 2853 | OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto, SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS); |
| 2854 | return -1; |
| 2855 | } |
| 2856 | |
David Benjamin | 51b1f74 | 2014-07-12 16:31:12 -0400 | [diff] [blame] | 2857 | CBS_init(&next_protocol, s->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2858 | |
| 2859 | /* The payload looks like: |
| 2860 | * uint8 proto_len; |
| 2861 | * uint8 proto[proto_len]; |
| 2862 | * uint8 padding_len; |
| 2863 | * uint8 padding[padding_len]; |
| 2864 | */ |
David Benjamin | ced551f | 2014-07-11 23:16:16 -0400 | [diff] [blame] | 2865 | if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) || |
| 2866 | !CBS_get_u8_length_prefixed(&next_protocol, &padding) || |
| 2867 | CBS_len(&next_protocol) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2868 | return 0; |
| 2869 | |
David Benjamin | ced551f | 2014-07-11 23:16:16 -0400 | [diff] [blame] | 2870 | if (!CBS_stow(&selected_protocol, |
| 2871 | &s->next_proto_negotiated, |
| 2872 | &s->next_proto_negotiated_len)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2873 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2874 | |
| 2875 | return 1; |
| 2876 | } |
| 2877 | # endif |
| 2878 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2879 | /* ssl3_get_channel_id reads and verifies a ClientID handshake message. */ |
| 2880 | int ssl3_get_channel_id(SSL *s) |
| 2881 | { |
| 2882 | int ret = -1, ok; |
| 2883 | long n; |
David Benjamin | 880b14e | 2014-08-25 22:35:07 -0400 | [diff] [blame^] | 2884 | EVP_MD_CTX md_ctx; |
| 2885 | uint8_t channel_id_hash[SHA256_DIGEST_LENGTH]; |
| 2886 | unsigned int channel_id_hash_len; |
David Benjamin | 9a6232f | 2014-07-11 23:07:47 -0400 | [diff] [blame] | 2887 | const uint8_t *p; |
| 2888 | uint16_t extension_type, expected_extension_type; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2889 | EC_GROUP* p256 = NULL; |
| 2890 | EC_KEY* key = NULL; |
| 2891 | EC_POINT* point = NULL; |
| 2892 | ECDSA_SIG sig; |
| 2893 | BIGNUM x, y; |
David Benjamin | 9a6232f | 2014-07-11 23:07:47 -0400 | [diff] [blame] | 2894 | CBS encrypted_extensions, extension; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2895 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2896 | n = s->method->ssl_get_message(s, |
| 2897 | SSL3_ST_SR_CHANNEL_ID_A, |
| 2898 | SSL3_ST_SR_CHANNEL_ID_B, |
| 2899 | SSL3_MT_ENCRYPTED_EXTENSIONS, |
| 2900 | 2 + 2 + TLSEXT_CHANNEL_ID_SIZE, |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 2901 | SSL_GET_MESSAGE_DONT_HASH_MESSAGE, |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2902 | &ok); |
| 2903 | |
| 2904 | if (!ok) |
| 2905 | return((int)n); |
| 2906 | |
David Benjamin | 880b14e | 2014-08-25 22:35:07 -0400 | [diff] [blame^] | 2907 | /* Before incorporating the EncryptedExtensions message to the |
| 2908 | * handshake hash, compute the hash that should have been signed. */ |
| 2909 | channel_id_hash_len = sizeof(channel_id_hash); |
| 2910 | EVP_MD_CTX_init(&md_ctx); |
| 2911 | if (!EVP_DigestInit_ex(&md_ctx, EVP_sha256(), NULL) || |
| 2912 | !tls1_channel_id_hash(&md_ctx, s) || |
| 2913 | !EVP_DigestFinal(&md_ctx, channel_id_hash, &channel_id_hash_len)) |
| 2914 | { |
| 2915 | EVP_MD_CTX_cleanup(&md_ctx); |
| 2916 | return -1; |
| 2917 | } |
| 2918 | EVP_MD_CTX_cleanup(&md_ctx); |
| 2919 | assert(channel_id_hash_len == SHA256_DIGEST_LENGTH); |
| 2920 | |
| 2921 | ssl3_hash_current_message(s); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2922 | |
| 2923 | /* s->state doesn't reflect whether ChangeCipherSpec has been received |
| 2924 | * 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] | 2925 | * by ssl3_get_finished). |
| 2926 | * TODO(davidben): Is this check now redundant with |
| 2927 | * SSL3_FLAGS_EXPECT_CCS? */ |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2928 | if (!s->s3->change_cipher_spec) |
| 2929 | { |
| 2930 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_GOT_CHANNEL_ID_BEFORE_A_CCS); |
| 2931 | return -1; |
| 2932 | } |
| 2933 | |
David Benjamin | 51b1f74 | 2014-07-12 16:31:12 -0400 | [diff] [blame] | 2934 | CBS_init(&encrypted_extensions, s->init_msg, n); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2935 | |
David Benjamin | 9a6232f | 2014-07-11 23:07:47 -0400 | [diff] [blame] | 2936 | /* EncryptedExtensions could include multiple extensions, but |
| 2937 | * the only extension that could be negotiated is ChannelID, |
| 2938 | * so there can only be one entry. |
| 2939 | * |
| 2940 | * The payload looks like: |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2941 | * uint16 extension_type |
| 2942 | * uint16 extension_len; |
| 2943 | * uint8 x[32]; |
| 2944 | * uint8 y[32]; |
| 2945 | * uint8 r[32]; |
| 2946 | * uint8 s[32]; |
| 2947 | */ |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2948 | expected_extension_type = TLSEXT_TYPE_channel_id; |
| 2949 | if (s->s3->tlsext_channel_id_new) |
| 2950 | expected_extension_type = TLSEXT_TYPE_channel_id_new; |
| 2951 | |
David Benjamin | 9a6232f | 2014-07-11 23:07:47 -0400 | [diff] [blame] | 2952 | if (!CBS_get_u16(&encrypted_extensions, &extension_type) || |
| 2953 | !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) || |
| 2954 | CBS_len(&encrypted_extensions) != 0 || |
| 2955 | extension_type != expected_extension_type || |
| 2956 | CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2957 | { |
| 2958 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_INVALID_MESSAGE); |
| 2959 | return -1; |
| 2960 | } |
| 2961 | |
| 2962 | p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); |
| 2963 | if (!p256) |
| 2964 | { |
| 2965 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_NO_P256_SUPPORT); |
| 2966 | return -1; |
| 2967 | } |
| 2968 | |
| 2969 | BN_init(&x); |
| 2970 | BN_init(&y); |
| 2971 | sig.r = BN_new(); |
| 2972 | sig.s = BN_new(); |
| 2973 | |
David Benjamin | 9a6232f | 2014-07-11 23:07:47 -0400 | [diff] [blame] | 2974 | p = CBS_data(&extension); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2975 | if (BN_bin2bn(p + 0, 32, &x) == NULL || |
| 2976 | BN_bin2bn(p + 32, 32, &y) == NULL || |
| 2977 | BN_bin2bn(p + 64, 32, sig.r) == NULL || |
| 2978 | BN_bin2bn(p + 96, 32, sig.s) == NULL) |
| 2979 | goto err; |
| 2980 | |
| 2981 | point = EC_POINT_new(p256); |
| 2982 | if (!point || |
| 2983 | !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) |
| 2984 | goto err; |
| 2985 | |
| 2986 | key = EC_KEY_new(); |
| 2987 | if (!key || |
| 2988 | !EC_KEY_set_group(key, p256) || |
| 2989 | !EC_KEY_set_public_key(key, point)) |
| 2990 | goto err; |
| 2991 | |
| 2992 | /* We stored the handshake hash in |tlsext_channel_id| the first time |
| 2993 | * that we were called. */ |
David Benjamin | 880b14e | 2014-08-25 22:35:07 -0400 | [diff] [blame^] | 2994 | 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] | 2995 | { |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2996 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_CHANNEL_ID_SIGNATURE_INVALID); |
| 2997 | s->s3->tlsext_channel_id_valid = 0; |
| 2998 | goto err; |
David Benjamin | 3a66e28 | 2014-08-24 02:22:29 -0400 | [diff] [blame] | 2999 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3000 | |
| 3001 | memcpy(s->s3->tlsext_channel_id, p, 64); |
| 3002 | ret = 1; |
| 3003 | |
| 3004 | err: |
| 3005 | BN_free(&x); |
| 3006 | BN_free(&y); |
| 3007 | BN_free(sig.r); |
| 3008 | BN_free(sig.s); |
| 3009 | if (key) |
| 3010 | EC_KEY_free(key); |
| 3011 | if (point) |
| 3012 | EC_POINT_free(point); |
| 3013 | if (p256) |
| 3014 | EC_GROUP_free(p256); |
| 3015 | return ret; |
| 3016 | } |
| 3017 | |