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