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