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. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 7 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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). |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 14 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 21 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 :-). |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 39 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 51 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 65 | * notice, this list of conditions and the following disclaimer. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | * |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 113 | * Portions of the attached software ("Contribution") are developed by |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | */ |
| 149 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 150 | #include <openssl/ssl.h> |
| 151 | |
David Benjamin | 81ea0bf | 2014-11-23 04:20:17 -0500 | [diff] [blame] | 152 | #include <assert.h> |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 153 | #include <limits.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 154 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 155 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 156 | #include <utility> |
| 157 | |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 158 | #include <openssl/aead.h> |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 159 | #include <openssl/bn.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 160 | #include <openssl/buf.h> |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 161 | #include <openssl/bytestring.h> |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 162 | #include <openssl/ec_key.h> |
| 163 | #include <openssl/ecdsa.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 164 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 165 | #include <openssl/evp.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 166 | #include <openssl/md5.h> |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 167 | #include <openssl/mem.h> |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 168 | #include <openssl/rand.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 169 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 170 | #include "../crypto/internal.h" |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 171 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 172 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 173 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 174 | namespace bssl { |
| 175 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 176 | enum ssl_client_hs_state_t { |
| 177 | state_start_connect = 0, |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 178 | state_enter_early_data, |
| 179 | state_read_hello_verify_request, |
| 180 | state_read_server_hello, |
| 181 | state_tls13, |
| 182 | state_read_server_certificate, |
| 183 | state_read_certificate_status, |
| 184 | state_verify_server_certificate, |
| 185 | state_read_server_key_exchange, |
| 186 | state_read_certificate_request, |
| 187 | state_read_server_hello_done, |
| 188 | state_send_client_certificate, |
| 189 | state_send_client_key_exchange, |
| 190 | state_send_client_certificate_verify, |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 191 | state_send_client_finished, |
| 192 | state_finish_flight, |
| 193 | state_read_session_ticket, |
| 194 | state_process_change_cipher_spec, |
| 195 | state_read_server_finished, |
| 196 | state_finish_client_handshake, |
| 197 | state_done, |
| 198 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 199 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 200 | // ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of |
| 201 | // disabled algorithms. |
David Benjamin | 2fe6e22 | 2017-01-25 02:18:43 -0500 | [diff] [blame] | 202 | static void ssl_get_client_disabled(SSL *ssl, uint32_t *out_mask_a, |
| 203 | uint32_t *out_mask_k) { |
David Benjamin | 2fe6e22 | 2017-01-25 02:18:43 -0500 | [diff] [blame] | 204 | *out_mask_a = 0; |
| 205 | *out_mask_k = 0; |
| 206 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 207 | // PSK requires a client callback. |
David Benjamin | 2fe6e22 | 2017-01-25 02:18:43 -0500 | [diff] [blame] | 208 | if (ssl->psk_client_callback == NULL) { |
| 209 | *out_mask_a |= SSL_aPSK; |
| 210 | *out_mask_k |= SSL_kPSK; |
| 211 | } |
| 212 | } |
| 213 | |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 214 | static int ssl_write_client_cipher_list(SSL_HANDSHAKE *hs, CBB *out) { |
| 215 | SSL *const ssl = hs->ssl; |
David Benjamin | 2fe6e22 | 2017-01-25 02:18:43 -0500 | [diff] [blame] | 216 | uint32_t mask_a, mask_k; |
| 217 | ssl_get_client_disabled(ssl, &mask_a, &mask_k); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 218 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 219 | CBB child; |
| 220 | if (!CBB_add_u16_length_prefixed(out, &child)) { |
| 221 | return 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 222 | } |
| 223 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 224 | // Add a fake cipher suite. See draft-davidben-tls-grease-01. |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 225 | if (ssl->ctx->grease_enabled && |
| 226 | !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) { |
| 227 | return 0; |
| 228 | } |
| 229 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 230 | // Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on |
| 231 | // hardware support. |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 232 | if (hs->max_version >= TLS1_3_VERSION) { |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 233 | if (!EVP_has_aes_hardware() && |
| 234 | !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) { |
| 235 | return 0; |
David Benjamin | c7817d8 | 2015-11-05 18:28:33 -0500 | [diff] [blame] | 236 | } |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 237 | if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) || |
| 238 | !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) { |
| 239 | return 0; |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 240 | } |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 241 | if (EVP_has_aes_hardware() && |
| 242 | !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | } |
| 246 | |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 247 | if (hs->min_version < TLS1_3_VERSION) { |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 248 | int any_enabled = 0; |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 249 | for (const SSL_CIPHER *cipher : SSL_get_ciphers(ssl)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 250 | // Skip disabled ciphers |
David Benjamin | 2fe6e22 | 2017-01-25 02:18:43 -0500 | [diff] [blame] | 251 | if ((cipher->algorithm_mkey & mask_k) || |
| 252 | (cipher->algorithm_auth & mask_a)) { |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 253 | continue; |
| 254 | } |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 255 | if (SSL_CIPHER_get_min_version(cipher) > hs->max_version || |
| 256 | SSL_CIPHER_get_max_version(cipher) < hs->min_version) { |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 257 | continue; |
| 258 | } |
| 259 | any_enabled = 1; |
| 260 | if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) { |
| 261 | return 0; |
| 262 | } |
| 263 | } |
| 264 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 265 | // If all ciphers were disabled, return the error to the caller. |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 266 | if (!any_enabled && hs->max_version < TLS1_3_VERSION) { |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 267 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE); |
| 268 | return 0; |
| 269 | } |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 270 | } |
| 271 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 272 | // For SSLv3, the SCSV is added. Otherwise the renegotiation extension is |
| 273 | // added. |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 274 | if (hs->max_version == SSL3_VERSION && |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 275 | !ssl->s3->initial_handshake_complete) { |
| 276 | if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) { |
| 277 | return 0; |
| 278 | } |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 279 | } |
| 280 | |
David Benjamin | e0ff767 | 2016-09-19 18:40:03 -0400 | [diff] [blame] | 281 | if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) { |
David Benjamin | 5e7e7cc | 2016-07-21 12:55:28 +0200 | [diff] [blame] | 282 | if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) { |
| 283 | return 0; |
| 284 | } |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | return CBB_flush(out); |
| 288 | } |
| 289 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 290 | int ssl_write_client_hello(SSL_HANDSHAKE *hs) { |
| 291 | SSL *const ssl = hs->ssl; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 292 | ScopedCBB cbb; |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 293 | CBB body; |
| 294 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO)) { |
| 295 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 296 | } |
| 297 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 298 | // Renegotiations do not participate in session resumption. |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 299 | int has_session_id = ssl->session != NULL && |
| 300 | !ssl->s3->initial_handshake_complete && |
| 301 | ssl->session->session_id_length > 0; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 302 | |
| 303 | CBB child; |
David Benjamin | f04c2e9 | 2016-12-06 13:35:25 -0500 | [diff] [blame] | 304 | if (!CBB_add_u16(&body, hs->client_version) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 305 | !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) || |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 306 | !CBB_add_u8_length_prefixed(&body, &child)) { |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 307 | return 0; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 308 | } |
| 309 | |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 310 | if (has_session_id) { |
| 311 | if (!CBB_add_bytes(&child, ssl->session->session_id, |
| 312 | ssl->session->session_id_length)) { |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 313 | return 0; |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 314 | } |
| 315 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 316 | // In TLS 1.3 experimental encodings, send a fake placeholder session ID |
| 317 | // when we do not otherwise have one to send. |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 318 | if (hs->max_version >= TLS1_3_VERSION && |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 319 | ssl_is_resumption_variant(ssl->tls13_variant) && |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 320 | !CBB_add_bytes(&child, hs->session_id, hs->session_id_len)) { |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 321 | return 0; |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 325 | if (SSL_is_dtls(ssl)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 326 | if (!CBB_add_u8_length_prefixed(&body, &child) || |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 327 | !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) { |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 328 | return 0; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | size_t header_len = |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 333 | SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH; |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 334 | if (!ssl_write_client_cipher_list(hs, &body) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 335 | !CBB_add_u8(&body, 1 /* one compression method */) || |
| 336 | !CBB_add_u8(&body, 0 /* null compression */) || |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 337 | !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) { |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 338 | return 0; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 339 | } |
| 340 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 341 | uint8_t *msg = NULL; |
| 342 | size_t len; |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 343 | if (!ssl->method->finish_message(ssl, cbb.get(), &msg, &len)) { |
| 344 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 345 | } |
| 346 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 347 | // Now that the length prefixes have been computed, fill in the placeholder |
| 348 | // PSK binder. |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 349 | if (hs->needs_psk_binder && |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 350 | !tls13_write_psk_binder(hs, msg, len)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 351 | OPENSSL_free(msg); |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 352 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 353 | } |
| 354 | |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 355 | return ssl->method->add_message(ssl, msg, len); |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 356 | } |
| 357 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 358 | static int parse_server_version(SSL_HANDSHAKE *hs, uint16_t *out, |
| 359 | const SSLMessage &msg) { |
David Benjamin | b853f31 | 2017-07-14 18:40:34 -0400 | [diff] [blame] | 360 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 361 | if (msg.type != SSL3_MT_SERVER_HELLO && |
| 362 | msg.type != SSL3_MT_HELLO_RETRY_REQUEST) { |
David Benjamin | b853f31 | 2017-07-14 18:40:34 -0400 | [diff] [blame] | 363 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 364 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
| 365 | return 0; |
| 366 | } |
| 367 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 368 | CBS server_hello = msg.body; |
David Benjamin | b853f31 | 2017-07-14 18:40:34 -0400 | [diff] [blame] | 369 | if (!CBS_get_u16(&server_hello, out)) { |
| 370 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 371 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 372 | return 0; |
| 373 | } |
| 374 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 375 | // The server version may also be in the supported_versions extension if |
| 376 | // applicable. |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 377 | if (msg.type != SSL3_MT_SERVER_HELLO || *out != TLS1_2_VERSION) { |
David Benjamin | b853f31 | 2017-07-14 18:40:34 -0400 | [diff] [blame] | 378 | return 1; |
| 379 | } |
| 380 | |
| 381 | uint8_t sid_length; |
| 382 | if (!CBS_skip(&server_hello, SSL3_RANDOM_SIZE) || |
| 383 | !CBS_get_u8(&server_hello, &sid_length) || |
| 384 | !CBS_skip(&server_hello, sid_length + 2 /* cipher_suite */ + |
| 385 | 1 /* compression_method */)) { |
| 386 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 387 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 388 | return 0; |
| 389 | } |
| 390 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 391 | // The extensions block may not be present. |
David Benjamin | b853f31 | 2017-07-14 18:40:34 -0400 | [diff] [blame] | 392 | if (CBS_len(&server_hello) == 0) { |
| 393 | return 1; |
| 394 | } |
| 395 | |
| 396 | CBS extensions; |
| 397 | if (!CBS_get_u16_length_prefixed(&server_hello, &extensions) || |
| 398 | CBS_len(&server_hello) != 0) { |
| 399 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 400 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 401 | return 0; |
| 402 | } |
| 403 | |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 404 | bool have_supported_versions; |
David Benjamin | b853f31 | 2017-07-14 18:40:34 -0400 | [diff] [blame] | 405 | CBS supported_versions; |
| 406 | const SSL_EXTENSION_TYPE ext_types[] = { |
| 407 | {TLSEXT_TYPE_supported_versions, &have_supported_versions, |
| 408 | &supported_versions}, |
| 409 | }; |
| 410 | |
| 411 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 412 | if (!ssl_parse_extensions(&extensions, &alert, ext_types, |
| 413 | OPENSSL_ARRAY_SIZE(ext_types), |
| 414 | 1 /* ignore unknown */)) { |
| 415 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | if (have_supported_versions && |
| 420 | (!CBS_get_u16(&supported_versions, out) || |
| 421 | CBS_len(&supported_versions) != 0)) { |
| 422 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | return 1; |
| 427 | } |
| 428 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 429 | static enum ssl_hs_wait_t do_start_connect(SSL_HANDSHAKE *hs) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 430 | SSL *const ssl = hs->ssl; |
| 431 | |
Steven Valdez | 398085b | 2017-08-29 13:25:56 -0400 | [diff] [blame] | 432 | ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1); |
David Benjamin | d0beda0 | 2017-09-06 16:46:50 -0400 | [diff] [blame] | 433 | // |session_reused| must be reset in case this is a renegotiation. |
| 434 | ssl->s3->session_reused = false; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 435 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 436 | // Freeze the version range. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 437 | if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) { |
| 438 | return ssl_hs_error; |
| 439 | } |
| 440 | |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 441 | // SSL 3.0 ClientHellos should use SSL 3.0 not TLS 1.0, for the record-layer |
| 442 | // version. |
| 443 | if (hs->max_version == SSL3_VERSION) { |
| 444 | ssl->s3->aead_write_ctx->SetVersionIfNullCipher(SSL3_VERSION); |
| 445 | } |
| 446 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 447 | // Always advertise the ClientHello version from the original maximum version, |
| 448 | // even on renegotiation. The static RSA key exchange uses this field, and |
| 449 | // some servers fail when it changes across handshakes. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 450 | if (SSL_is_dtls(hs->ssl)) { |
| 451 | hs->client_version = |
| 452 | hs->max_version >= TLS1_2_VERSION ? DTLS1_2_VERSION : DTLS1_VERSION; |
| 453 | } else { |
| 454 | hs->client_version = |
| 455 | hs->max_version >= TLS1_2_VERSION ? TLS1_2_VERSION : hs->max_version; |
| 456 | } |
| 457 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 458 | // If the configured session has expired or was created at a disabled |
| 459 | // version, drop it. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 460 | if (ssl->session != NULL) { |
| 461 | if (ssl->session->is_server || |
| 462 | !ssl_supports_version(hs, ssl->session->ssl_version) || |
| 463 | (ssl->session->session_id_length == 0 && |
| 464 | ssl->session->tlsext_ticklen == 0) || |
| 465 | ssl->session->not_resumable || |
| 466 | !ssl_session_is_time_valid(ssl, ssl->session)) { |
| 467 | ssl_set_session(ssl, NULL); |
| 468 | } |
| 469 | } |
| 470 | |
Steven Valdez | 398085b | 2017-08-29 13:25:56 -0400 | [diff] [blame] | 471 | if (!RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 472 | return ssl_hs_error; |
| 473 | } |
| 474 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 475 | // Initialize a random session ID for the experimental TLS 1.3 variant |
| 476 | // requiring a session id. |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 477 | if (ssl_is_resumption_variant(ssl->tls13_variant)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 478 | hs->session_id_len = sizeof(hs->session_id); |
| 479 | if (!RAND_bytes(hs->session_id, hs->session_id_len)) { |
| 480 | return ssl_hs_error; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | if (!ssl_write_client_hello(hs)) { |
| 485 | return ssl_hs_error; |
| 486 | } |
| 487 | |
| 488 | hs->state = state_enter_early_data; |
| 489 | return ssl_hs_flush; |
| 490 | } |
| 491 | |
| 492 | static enum ssl_hs_wait_t do_enter_early_data(SSL_HANDSHAKE *hs) { |
| 493 | SSL *const ssl = hs->ssl; |
| 494 | |
Steven Valdez | 398085b | 2017-08-29 13:25:56 -0400 | [diff] [blame] | 495 | if (SSL_is_dtls(ssl)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 496 | hs->state = state_read_hello_verify_request; |
| 497 | return ssl_hs_ok; |
| 498 | } |
| 499 | |
| 500 | if (!hs->early_data_offered) { |
| 501 | hs->state = state_read_server_hello; |
| 502 | return ssl_hs_ok; |
| 503 | } |
| 504 | |
| 505 | if (!tls13_init_early_key_schedule(hs) || |
| 506 | !tls13_advance_key_schedule(hs, ssl->session->master_key, |
| 507 | ssl->session->master_key_length) || |
| 508 | !tls13_derive_early_secrets(hs) || |
| 509 | !tls13_set_traffic_key(ssl, evp_aead_seal, hs->early_traffic_secret, |
| 510 | hs->hash_len)) { |
| 511 | return ssl_hs_error; |
| 512 | } |
| 513 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 514 | // Stash the early data session, so connection properties may be queried out |
| 515 | // of it. |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 516 | hs->in_early_data = true; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 517 | SSL_SESSION_up_ref(ssl->session); |
| 518 | hs->early_session.reset(ssl->session); |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 519 | hs->can_early_write = true; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 520 | |
| 521 | hs->state = state_read_server_hello; |
| 522 | return ssl_hs_early_return; |
| 523 | } |
| 524 | |
| 525 | static enum ssl_hs_wait_t do_read_hello_verify_request(SSL_HANDSHAKE *hs) { |
| 526 | SSL *const ssl = hs->ssl; |
| 527 | |
| 528 | assert(SSL_is_dtls(ssl)); |
| 529 | |
| 530 | SSLMessage msg; |
| 531 | if (!ssl->method->get_message(ssl, &msg)) { |
| 532 | return ssl_hs_read_message; |
| 533 | } |
| 534 | |
| 535 | if (msg.type != DTLS1_MT_HELLO_VERIFY_REQUEST) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 536 | hs->state = state_read_server_hello; |
| 537 | return ssl_hs_ok; |
| 538 | } |
| 539 | |
| 540 | CBS hello_verify_request = msg.body, cookie; |
| 541 | uint16_t server_version; |
| 542 | if (!CBS_get_u16(&hello_verify_request, &server_version) || |
| 543 | !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) || |
| 544 | CBS_len(&cookie) > sizeof(ssl->d1->cookie) || |
| 545 | CBS_len(&hello_verify_request) != 0) { |
| 546 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 547 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 548 | return ssl_hs_error; |
| 549 | } |
| 550 | |
| 551 | OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie)); |
| 552 | ssl->d1->cookie_len = CBS_len(&cookie); |
| 553 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 554 | ssl->method->next_message(ssl); |
Steven Valdez | 398085b | 2017-08-29 13:25:56 -0400 | [diff] [blame] | 555 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 556 | // DTLS resets the handshake buffer after HelloVerifyRequest. |
Steven Valdez | 398085b | 2017-08-29 13:25:56 -0400 | [diff] [blame] | 557 | if (!hs->transcript.Init()) { |
| 558 | return ssl_hs_error; |
| 559 | } |
| 560 | |
| 561 | if (!ssl_write_client_hello(hs)) { |
| 562 | return ssl_hs_error; |
| 563 | } |
| 564 | |
| 565 | hs->state = state_read_server_hello; |
| 566 | return ssl_hs_flush; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 570 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 571 | SSLMessage msg; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 572 | if (!ssl->method->get_message(ssl, &msg)) { |
| 573 | return ssl_hs_read_server_hello; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 574 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 575 | |
David Benjamin | b853f31 | 2017-07-14 18:40:34 -0400 | [diff] [blame] | 576 | uint16_t server_version; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 577 | if (!parse_server_version(hs, &server_version, msg)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 578 | return ssl_hs_error; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 579 | } |
| 580 | |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 581 | if (!ssl_supports_version(hs, server_version)) { |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 582 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 583 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 584 | return ssl_hs_error; |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 585 | } |
| 586 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 587 | assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete); |
| 588 | if (!ssl->s3->have_version) { |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 589 | ssl->version = server_version; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 590 | // At this point, the connection's version is known and ssl->version is |
| 591 | // fixed. Begin enforcing the record-layer version. |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 592 | ssl->s3->have_version = true; |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 593 | ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version); |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 594 | } else if (server_version != ssl->version) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 595 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 596 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 597 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 598 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 599 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 600 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 601 | hs->state = state_tls13; |
| 602 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 603 | } |
| 604 | |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 605 | if (hs->early_data_offered) { |
| 606 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 607 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 608 | return ssl_hs_error; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 609 | } |
| 610 | |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 611 | ssl_clear_tls13_state(hs); |
David Benjamin | 4fe3c90 | 2016-08-16 02:17:03 -0400 | [diff] [blame] | 612 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 613 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 614 | return ssl_hs_error; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 615 | } |
| 616 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 617 | CBS server_hello = msg.body, server_random, session_id; |
David Benjamin | b853f31 | 2017-07-14 18:40:34 -0400 | [diff] [blame] | 618 | uint16_t cipher_suite; |
| 619 | uint8_t compression_method; |
David Benjamin | b853f31 | 2017-07-14 18:40:34 -0400 | [diff] [blame] | 620 | if (!CBS_skip(&server_hello, 2 /* version */) || |
| 621 | !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) || |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 622 | !CBS_get_u8_length_prefixed(&server_hello, &session_id) || |
| 623 | CBS_len(&session_id) > SSL3_SESSION_ID_SIZE || |
| 624 | !CBS_get_u16(&server_hello, &cipher_suite) || |
| 625 | !CBS_get_u8(&server_hello, &compression_method)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 626 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 627 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 628 | return ssl_hs_error; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 629 | } |
| 630 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 631 | // Copy over the server random. |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 632 | OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random), |
| 633 | SSL3_RANDOM_SIZE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 634 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 635 | // TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS |
| 636 | // 1.3 is finalized and we are not implementing a draft version. |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 637 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 638 | if (!ssl->s3->initial_handshake_complete && ssl->session != NULL && |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 639 | ssl->session->session_id_length != 0 && |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 640 | CBS_mem_equal(&session_id, ssl->session->session_id, |
| 641 | ssl->session->session_id_length)) { |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 642 | ssl->s3->session_reused = true; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 643 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 644 | // The session wasn't resumed. Create a fresh SSL_SESSION to |
| 645 | // fill out. |
David Benjamin | 4d0be24 | 2016-09-01 01:10:07 -0400 | [diff] [blame] | 646 | ssl_set_session(ssl, NULL); |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 647 | if (!ssl_get_new_session(hs, 0 /* client */)) { |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 648 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 649 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 650 | } |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 651 | // Note: session_id could be empty. |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 652 | hs->new_session->session_id_length = CBS_len(&session_id); |
| 653 | OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id), |
| 654 | CBS_len(&session_id)); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 655 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 656 | |
David Benjamin | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 657 | const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite); |
| 658 | if (cipher == NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 659 | // unknown cipher |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 660 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 661 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 662 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 663 | } |
David Benjamin | d2cb1c1 | 2016-11-02 17:49:09 -0400 | [diff] [blame] | 664 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 665 | // The cipher must be allowed in the selected version and enabled. |
David Benjamin | 2fe6e22 | 2017-01-25 02:18:43 -0500 | [diff] [blame] | 666 | uint32_t mask_a, mask_k; |
| 667 | ssl_get_client_disabled(ssl, &mask_a, &mask_k); |
David Benjamin | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 668 | if ((cipher->algorithm_mkey & mask_k) || (cipher->algorithm_auth & mask_a) || |
| 669 | SSL_CIPHER_get_min_version(cipher) > ssl3_protocol_version(ssl) || |
| 670 | SSL_CIPHER_get_max_version(cipher) < ssl3_protocol_version(ssl) || |
| 671 | !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, cipher)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 672 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 673 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 674 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 675 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 676 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 677 | if (ssl->session != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 678 | if (ssl->session->ssl_version != ssl->version) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 679 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 680 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 681 | return ssl_hs_error; |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 682 | } |
David Benjamin | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 683 | if (ssl->session->cipher != cipher) { |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 684 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 685 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 686 | return ssl_hs_error; |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 687 | } |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 688 | if (!ssl_session_is_context_valid(ssl, ssl->session)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 689 | // This is actually a client application bug. |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 690 | OPENSSL_PUT_ERROR(SSL, |
| 691 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 692 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 693 | return ssl_hs_error; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 694 | } |
David Benjamin | 0623bce | 2015-12-25 15:40:14 -0500 | [diff] [blame] | 695 | } else { |
David Benjamin | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 696 | hs->new_session->cipher = cipher; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 697 | } |
David Benjamin | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 698 | hs->new_cipher = cipher; |
David Benjamin | bdf5e72 | 2014-11-11 00:52:15 -0500 | [diff] [blame] | 699 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 700 | // Now that the cipher is known, initialize the handshake hash and hash the |
| 701 | // ServerHello. |
David Benjamin | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 702 | if (!hs->transcript.InitHash(ssl3_protocol_version(ssl), hs->new_cipher) || |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 703 | !ssl_hash_message(hs, msg)) { |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 704 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 705 | return ssl_hs_error; |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 706 | } |
| 707 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 708 | // If doing a full handshake, the server may request a client certificate |
| 709 | // which requires hashing the handshake transcript. Otherwise, the handshake |
| 710 | // buffer may be released. |
David Benjamin | 3d458dc | 2016-09-12 22:40:27 +0000 | [diff] [blame] | 711 | if (ssl->session != NULL || |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 712 | !ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 713 | hs->transcript.FreeBuffer(); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 714 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 715 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 716 | // Only the NULL compression algorithm is supported. |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 717 | if (compression_method != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 718 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 719 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 720 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 721 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 722 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 723 | // TLS extensions |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 724 | if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 725 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 726 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 727 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 728 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 729 | // There should be nothing left over in the record. |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 730 | if (CBS_len(&server_hello) != 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 731 | // wrong packet length |
David Benjamin | 639846e | 2016-09-09 11:41:18 -0400 | [diff] [blame] | 732 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 733 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 734 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 735 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 736 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 737 | if (ssl->session != NULL && |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 738 | hs->extended_master_secret != ssl->session->extended_master_secret) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 739 | if (ssl->session->extended_master_secret) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 740 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION); |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 741 | } else { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 742 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION); |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 743 | } |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 744 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 745 | return ssl_hs_error; |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 746 | } |
| 747 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 748 | ssl->method->next_message(ssl); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 749 | |
| 750 | if (ssl->session != NULL) { |
| 751 | hs->state = state_read_session_ticket; |
| 752 | return ssl_hs_ok; |
| 753 | } |
| 754 | |
| 755 | hs->state = state_read_server_certificate; |
| 756 | return ssl_hs_ok; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 757 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 758 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 759 | static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) { |
| 760 | enum ssl_hs_wait_t wait = tls13_client_handshake(hs); |
| 761 | if (wait == ssl_hs_ok) { |
| 762 | hs->state = state_finish_client_handshake; |
| 763 | return ssl_hs_ok; |
| 764 | } |
| 765 | |
| 766 | return wait; |
| 767 | } |
| 768 | |
| 769 | static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 770 | SSL *const ssl = hs->ssl; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 771 | |
| 772 | if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
| 773 | hs->state = state_read_certificate_status; |
| 774 | return ssl_hs_ok; |
| 775 | } |
| 776 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 777 | SSLMessage msg; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 778 | if (!ssl->method->get_message(ssl, &msg)) { |
| 779 | return ssl_hs_read_message; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 780 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 781 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 782 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) || |
| 783 | !ssl_hash_message(hs, msg)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 784 | return ssl_hs_error; |
David Benjamin | 276b7e8 | 2017-01-21 14:13:39 -0500 | [diff] [blame] | 785 | } |
| 786 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 787 | CBS body = msg.body; |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 788 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 789 | UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 790 | if (!ssl_parse_cert_chain(&alert, &chain, &hs->peer_pubkey, NULL, &body, |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 791 | ssl->ctx->pool)) { |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 792 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 793 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 794 | } |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 795 | sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free); |
| 796 | hs->new_session->certs = chain.release(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 797 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 798 | if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0 || |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 799 | CBS_len(&body) != 0 || |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 800 | !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 801 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 802 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 803 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 804 | } |
Alex Chernyakhovsky | c6318e3 | 2014-07-04 22:52:07 -0400 | [diff] [blame] | 805 | |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 806 | if (!ssl_check_leaf_certificate( |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 807 | hs, hs->peer_pubkey.get(), |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 808 | sk_CRYPTO_BUFFER_value(hs->new_session->certs, 0))) { |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 809 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 810 | return ssl_hs_error; |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 811 | } |
| 812 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 813 | ssl->method->next_message(ssl); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 814 | |
| 815 | hs->state = state_read_certificate_status; |
| 816 | return ssl_hs_ok; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 817 | } |
| 818 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 819 | static enum ssl_hs_wait_t do_read_certificate_status(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 820 | SSL *const ssl = hs->ssl; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 821 | |
| 822 | if (!hs->certificate_status_expected) { |
| 823 | hs->state = state_verify_server_certificate; |
| 824 | return ssl_hs_ok; |
| 825 | } |
| 826 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 827 | SSLMessage msg; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 828 | if (!ssl->method->get_message(ssl, &msg)) { |
| 829 | return ssl_hs_read_message; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 830 | } |
| 831 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 832 | if (msg.type != SSL3_MT_CERTIFICATE_STATUS) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 833 | // A server may send status_request in ServerHello and then change its mind |
| 834 | // about sending CertificateStatus. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 835 | hs->state = state_verify_server_certificate; |
| 836 | return ssl_hs_ok; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 837 | } |
| 838 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 839 | if (!ssl_hash_message(hs, msg)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 840 | return ssl_hs_error; |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 841 | } |
| 842 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 843 | CBS certificate_status = msg.body, ocsp_response; |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 844 | uint8_t status_type; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 845 | if (!CBS_get_u8(&certificate_status, &status_type) || |
| 846 | status_type != TLSEXT_STATUSTYPE_ocsp || |
| 847 | !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) || |
| 848 | CBS_len(&ocsp_response) == 0 || |
| 849 | CBS_len(&certificate_status) != 0) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 850 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 851 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 852 | return ssl_hs_error; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 853 | } |
| 854 | |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 855 | CRYPTO_BUFFER_free(hs->new_session->ocsp_response); |
| 856 | hs->new_session->ocsp_response = |
| 857 | CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool); |
| 858 | if (hs->new_session->ocsp_response == nullptr) { |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 859 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 860 | return ssl_hs_error; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 861 | } |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 862 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 863 | ssl->method->next_message(ssl); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 864 | |
| 865 | hs->state = state_verify_server_certificate; |
| 866 | return ssl_hs_ok; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 867 | } |
| 868 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 869 | static enum ssl_hs_wait_t do_verify_server_certificate(SSL_HANDSHAKE *hs) { |
| 870 | if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
| 871 | hs->state = state_read_server_key_exchange; |
| 872 | return ssl_hs_ok; |
| 873 | } |
| 874 | |
| 875 | switch (ssl_verify_peer_cert(hs)) { |
| 876 | case ssl_verify_ok: |
| 877 | break; |
| 878 | case ssl_verify_invalid: |
| 879 | return ssl_hs_error; |
| 880 | case ssl_verify_retry: |
| 881 | hs->state = state_verify_server_certificate; |
| 882 | return ssl_hs_certificate_verify; |
| 883 | } |
| 884 | |
| 885 | hs->state = state_read_server_key_exchange; |
| 886 | return ssl_hs_ok; |
| 887 | } |
| 888 | |
| 889 | static enum ssl_hs_wait_t do_read_server_key_exchange(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 890 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 891 | SSLMessage msg; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 892 | if (!ssl->method->get_message(ssl, &msg)) { |
| 893 | return ssl_hs_read_message; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 894 | } |
| 895 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 896 | if (msg.type != SSL3_MT_SERVER_KEY_EXCHANGE) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 897 | // Some ciphers (pure PSK) have an optional ServerKeyExchange message. |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 898 | if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 899 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 900 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 901 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 902 | } |
| 903 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 904 | hs->state = state_read_certificate_request; |
| 905 | return ssl_hs_ok; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 906 | } |
| 907 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 908 | if (!ssl_hash_message(hs, msg)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 909 | return ssl_hs_error; |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 910 | } |
| 911 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 912 | uint32_t alg_k = hs->new_cipher->algorithm_mkey; |
| 913 | uint32_t alg_a = hs->new_cipher->algorithm_auth; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 914 | CBS server_key_exchange = msg.body; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 915 | if (alg_a & SSL_aPSK) { |
| 916 | CBS psk_identity_hint; |
| 917 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 918 | // Each of the PSK key exchanges begins with a psk_identity_hint. |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 919 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, |
| 920 | &psk_identity_hint)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 921 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 922 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 923 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 924 | } |
| 925 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 926 | // Store PSK identity hint for later use, hint is used in |
| 927 | // ssl3_send_client_key_exchange. Assume that the maximum length of a PSK |
| 928 | // identity hint can be as long as the maximum length of a PSK identity. |
| 929 | // Also do not allow NULL characters; identities are saved as C strings. |
| 930 | // |
| 931 | // TODO(davidben): Should invalid hints be ignored? It's a hint rather than |
| 932 | // a specific identity. |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 933 | if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN || |
| 934 | CBS_contains_zero_byte(&psk_identity_hint)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 935 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 936 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 937 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 938 | } |
| 939 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 940 | // Save non-empty identity hints as a C string. Empty identity hints we |
| 941 | // treat as missing. Plain PSK makes it possible to send either no hint |
| 942 | // (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell |
| 943 | // empty hint. Having different capabilities is odd, so we interpret empty |
| 944 | // and missing as identical. |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 945 | char *raw = nullptr; |
David Benjamin | 7867934 | 2016-09-16 19:42:05 -0400 | [diff] [blame] | 946 | if (CBS_len(&psk_identity_hint) != 0 && |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 947 | !CBS_strdup(&psk_identity_hint, &raw)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 948 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 949 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 950 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 951 | } |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 952 | hs->peer_psk_identity_hint.reset(raw); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 953 | } |
| 954 | |
Matthew Braithwaite | 7e06de5 | 2017-04-10 15:52:14 -0700 | [diff] [blame] | 955 | if (alg_k & SSL_kECDHE) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 956 | // Parse the server parameters. |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 957 | uint8_t group_type; |
| 958 | uint16_t group_id; |
David Benjamin | 48cce66 | 2015-12-17 01:33:08 -0500 | [diff] [blame] | 959 | CBS point; |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 960 | if (!CBS_get_u8(&server_key_exchange, &group_type) || |
| 961 | group_type != NAMED_CURVE_TYPE || |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 962 | !CBS_get_u16(&server_key_exchange, &group_id) || |
| 963 | !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 964 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 965 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 966 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 967 | } |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 968 | hs->new_session->group_id = group_id; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 969 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 970 | // Ensure the group is consistent with preferences. |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 971 | if (!tls1_check_group_id(ssl, group_id)) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 972 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 973 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 974 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 975 | } |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 976 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 977 | // Initialize ECDH and save the peer public key for later. |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 978 | hs->key_share = SSLKeyShare::Create(group_id); |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 979 | uint8_t *peer_key = nullptr; |
| 980 | size_t peer_key_len = 0; |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 981 | if (!hs->key_share || |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 982 | !CBS_stow(&point, &peer_key, &peer_key_len)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 983 | return ssl_hs_error; |
David Benjamin | c1cc858 | 2015-12-17 01:22:15 -0500 | [diff] [blame] | 984 | } |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 985 | hs->peer_key.Reset(peer_key, peer_key_len); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 986 | } else if (!(alg_k & SSL_kPSK)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 987 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 988 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 989 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 990 | } |
| 991 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 992 | // At this point, |server_key_exchange| contains the signature, if any, while |
| 993 | // |msg.body| contains the entire message. From that, derive a CBS containing |
| 994 | // just the parameter. |
David Benjamin | b35d684 | 2015-12-29 17:08:08 -0500 | [diff] [blame] | 995 | CBS parameter; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 996 | CBS_init(¶meter, CBS_data(&msg.body), |
| 997 | CBS_len(&msg.body) - CBS_len(&server_key_exchange)); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 998 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 999 | // ServerKeyExchange should be signed by the server's public key. |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1000 | if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1001 | uint16_t signature_algorithm = 0; |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1002 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 1003 | if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) { |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1004 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1005 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1006 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1007 | } |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1008 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 1009 | if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) { |
| 1010 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1011 | return ssl_hs_error; |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1012 | } |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1013 | hs->new_session->peer_signature_algorithm = signature_algorithm; |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 1014 | } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm, |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1015 | hs->peer_pubkey.get())) { |
David Benjamin | 49ec9bb | 2016-07-14 00:11:26 -0400 | [diff] [blame] | 1016 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1017 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1018 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1019 | } |
| 1020 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1021 | // The last field in |server_key_exchange| is the signature. |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1022 | CBS signature; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1023 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) || |
| 1024 | CBS_len(&server_key_exchange) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1025 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1026 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1027 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1028 | } |
| 1029 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1030 | ScopedCBB transcript; |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1031 | uint8_t *transcript_data; |
| 1032 | size_t transcript_len; |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1033 | if (!CBB_init(transcript.get(), |
| 1034 | 2 * SSL3_RANDOM_SIZE + CBS_len(¶meter)) || |
| 1035 | !CBB_add_bytes(transcript.get(), ssl->s3->client_random, |
| 1036 | SSL3_RANDOM_SIZE) || |
| 1037 | !CBB_add_bytes(transcript.get(), ssl->s3->server_random, |
| 1038 | SSL3_RANDOM_SIZE) || |
| 1039 | !CBB_add_bytes(transcript.get(), CBS_data(¶meter), |
| 1040 | CBS_len(¶meter)) || |
| 1041 | !CBB_finish(transcript.get(), &transcript_data, &transcript_len)) { |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1042 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1043 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1044 | return ssl_hs_error; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1045 | } |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1046 | |
| 1047 | int sig_ok = ssl_public_key_verify( |
| 1048 | ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm, |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1049 | hs->peer_pubkey.get(), transcript_data, transcript_len); |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1050 | OPENSSL_free(transcript_data); |
| 1051 | |
David Benjamin | bf82aed | 2016-03-01 22:57:40 -0500 | [diff] [blame] | 1052 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 1053 | sig_ok = 1; |
| 1054 | ERR_clear_error(); |
| 1055 | #endif |
| 1056 | if (!sig_ok) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1057 | // bad signature |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1058 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1059 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1060 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1061 | } |
| 1062 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1063 | // PSK ciphers are the only supported certificate-less ciphers. |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1064 | assert(alg_a == SSL_aPSK); |
| 1065 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1066 | if (CBS_len(&server_key_exchange) > 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1067 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1068 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1069 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1070 | } |
| 1071 | } |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 1072 | |
| 1073 | ssl->method->next_message(ssl); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1074 | hs->state = state_read_certificate_request; |
| 1075 | return ssl_hs_ok; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1076 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1077 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1078 | static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1079 | SSL *const ssl = hs->ssl; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1080 | |
| 1081 | if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
| 1082 | hs->state = state_read_server_hello_done; |
| 1083 | return ssl_hs_ok; |
| 1084 | } |
| 1085 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1086 | SSLMessage msg; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1087 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1088 | return ssl_hs_read_message; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1089 | } |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 1090 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1091 | if (msg.type == SSL3_MT_SERVER_HELLO_DONE) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1092 | // If we get here we don't need the handshake buffer as we won't be doing |
| 1093 | // client auth. |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 1094 | hs->transcript.FreeBuffer(); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1095 | hs->state = state_read_server_hello_done; |
| 1096 | return ssl_hs_ok; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1097 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1098 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1099 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_REQUEST) || |
| 1100 | !ssl_hash_message(hs, msg)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1101 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1102 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1103 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1104 | // Get the certificate types. |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1105 | CBS body = msg.body, certificate_types; |
| 1106 | if (!CBS_get_u8_length_prefixed(&body, &certificate_types)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1107 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1108 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1109 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1110 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1111 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1112 | if (!CBS_stow(&certificate_types, &hs->certificate_types, |
| 1113 | &hs->num_certificate_types)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1114 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1115 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1116 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1117 | |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1118 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1119 | CBS supported_signature_algorithms; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1120 | if (!CBS_get_u16_length_prefixed(&body, &supported_signature_algorithms) || |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 1121 | !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1122 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1123 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1124 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1125 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1126 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1127 | |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 1128 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1129 | UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names = |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1130 | ssl_parse_client_CA_list(ssl, &alert, &body); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1131 | if (!ca_names) { |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 1132 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1133 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1134 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1135 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1136 | if (CBS_len(&body) != 0) { |
David Benjamin | 639846e | 2016-09-09 11:41:18 -0400 | [diff] [blame] | 1137 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 639846e | 2016-09-09 11:41:18 -0400 | [diff] [blame] | 1138 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1139 | return ssl_hs_error; |
David Benjamin | 639846e | 2016-09-09 11:41:18 -0400 | [diff] [blame] | 1140 | } |
| 1141 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1142 | hs->cert_request = true; |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1143 | hs->ca_names = std::move(ca_names); |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 1144 | ssl->ctx->x509_method->hs_flush_cached_ca_names(hs); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1145 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 1146 | ssl->method->next_message(ssl); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1147 | hs->state = state_read_server_hello_done; |
| 1148 | return ssl_hs_ok; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1149 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1150 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1151 | static enum ssl_hs_wait_t do_read_server_hello_done(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1152 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1153 | SSLMessage msg; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1154 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1155 | return ssl_hs_read_message; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1156 | } |
David Benjamin | 4606268 | 2014-07-14 19:14:32 -0400 | [diff] [blame] | 1157 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1158 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO_DONE) || |
| 1159 | !ssl_hash_message(hs, msg)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1160 | return ssl_hs_error; |
David Benjamin | 276b7e8 | 2017-01-21 14:13:39 -0500 | [diff] [blame] | 1161 | } |
| 1162 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1163 | // ServerHelloDone is empty. |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1164 | if (CBS_len(&msg.body) != 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1165 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 639846e | 2016-09-09 11:41:18 -0400 | [diff] [blame] | 1166 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1167 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1168 | } |
David Benjamin | 4606268 | 2014-07-14 19:14:32 -0400 | [diff] [blame] | 1169 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 1170 | ssl->method->next_message(ssl); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1171 | hs->state = state_send_client_certificate; |
| 1172 | return ssl_hs_ok; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1173 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1174 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1175 | static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1176 | SSL *const ssl = hs->ssl; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1177 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1178 | // The peer didn't request a certificate. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1179 | if (!hs->cert_request) { |
| 1180 | hs->state = state_send_client_key_exchange; |
| 1181 | return ssl_hs_ok; |
| 1182 | } |
| 1183 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1184 | // Call cert_cb to update the certificate. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1185 | if (ssl->cert->cert_cb != NULL) { |
| 1186 | int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg); |
| 1187 | if (rv == 0) { |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1188 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1189 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
| 1190 | return ssl_hs_error; |
| 1191 | } |
| 1192 | if (rv < 0) { |
| 1193 | hs->state = state_send_client_certificate; |
| 1194 | return ssl_hs_x509_lookup; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1195 | } |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1196 | } |
| 1197 | |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1198 | if (!ssl_has_certificate(ssl)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1199 | // Without a client certificate, the handshake buffer may be released. |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 1200 | hs->transcript.FreeBuffer(); |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1201 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1202 | // In SSL 3.0, the Certificate message is replaced with a warning alert. |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1203 | if (ssl->version == SSL3_VERSION) { |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 1204 | if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING, |
| 1205 | SSL_AD_NO_CERTIFICATE)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1206 | return ssl_hs_error; |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 1207 | } |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1208 | hs->state = state_send_client_key_exchange; |
| 1209 | return ssl_hs_ok; |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | |
David Benjamin | a232a71 | 2017-03-30 15:51:53 -0500 | [diff] [blame] | 1213 | if (!ssl_on_certificate_selected(hs) || |
David Benjamin | 650aa1c | 2016-12-20 18:55:16 -0500 | [diff] [blame] | 1214 | !ssl3_output_cert_chain(ssl)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1215 | return ssl_hs_error; |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1216 | } |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1217 | |
| 1218 | |
| 1219 | hs->state = state_send_client_key_exchange; |
| 1220 | return ssl_hs_ok; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1221 | } |
| 1222 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 1223 | static_assert(sizeof(size_t) >= sizeof(unsigned), |
| 1224 | "size_t is smaller than unsigned"); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1225 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1226 | static enum ssl_hs_wait_t do_send_client_key_exchange(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1227 | SSL *const ssl = hs->ssl; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1228 | ScopedCBB cbb; |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1229 | CBB body; |
| 1230 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1231 | SSL3_MT_CLIENT_KEY_EXCHANGE)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1232 | return ssl_hs_error; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1233 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1234 | |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1235 | Array<uint8_t> pms; |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1236 | uint32_t alg_k = hs->new_cipher->algorithm_mkey; |
| 1237 | uint32_t alg_a = hs->new_cipher->algorithm_auth; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1238 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1239 | // If using a PSK key exchange, prepare the pre-shared key. |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1240 | unsigned psk_len = 0; |
| 1241 | uint8_t psk[PSK_MAX_PSK_LEN]; |
| 1242 | if (alg_a & SSL_aPSK) { |
| 1243 | if (ssl->psk_client_callback == NULL) { |
| 1244 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB); |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1245 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1246 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1247 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1248 | char identity[PSK_MAX_IDENTITY_LEN + 1]; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1249 | OPENSSL_memset(identity, 0, sizeof(identity)); |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1250 | psk_len = |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1251 | ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint.get(), |
| 1252 | identity, sizeof(identity), psk, sizeof(psk)); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1253 | if (psk_len == 0) { |
| 1254 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
| 1255 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1256 | return ssl_hs_error; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1257 | } |
| 1258 | assert(psk_len <= PSK_MAX_PSK_LEN); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1259 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1260 | OPENSSL_free(hs->new_session->psk_identity); |
| 1261 | hs->new_session->psk_identity = BUF_strdup(identity); |
| 1262 | if (hs->new_session->psk_identity == NULL) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1263 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1264 | return ssl_hs_error; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1265 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1266 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1267 | // Write out psk_identity. |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1268 | CBB child; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1269 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1270 | !CBB_add_bytes(&child, (const uint8_t *)identity, |
| 1271 | OPENSSL_strnlen(identity, sizeof(identity))) || |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1272 | !CBB_flush(&body)) { |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1273 | return ssl_hs_error; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1274 | } |
| 1275 | } |
David Benjamin | 758d127 | 2015-11-20 17:47:25 -0500 | [diff] [blame] | 1276 | |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1277 | // Depending on the key exchange method, compute |pms|. |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1278 | if (alg_k & SSL_kRSA) { |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1279 | if (!pms.Init(SSL_MAX_MASTER_KEY_LENGTH)) { |
| 1280 | return ssl_hs_error; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1281 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1282 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1283 | RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey.get()); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1284 | if (rsa == NULL) { |
| 1285 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1286 | return ssl_hs_error; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1287 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1288 | |
David Benjamin | f04c2e9 | 2016-12-06 13:35:25 -0500 | [diff] [blame] | 1289 | pms[0] = hs->client_version >> 8; |
| 1290 | pms[1] = hs->client_version & 0xff; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1291 | if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) { |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1292 | return ssl_hs_error; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1293 | } |
| 1294 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1295 | CBB child, *enc_pms = &body; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1296 | size_t enc_pms_len; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1297 | // In TLS, there is a length prefix. |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1298 | if (ssl->version > SSL3_VERSION) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1299 | if (!CBB_add_u16_length_prefixed(&body, &child)) { |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1300 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1301 | } |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1302 | enc_pms = &child; |
| 1303 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1304 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1305 | uint8_t *ptr; |
| 1306 | if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) || |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1307 | !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms.data(), |
| 1308 | pms.size(), RSA_PKCS1_PADDING) || |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1309 | !CBB_did_write(enc_pms, enc_pms_len) || |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1310 | !CBB_flush(&body)) { |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1311 | return ssl_hs_error; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1312 | } |
Matthew Braithwaite | 7e06de5 | 2017-04-10 15:52:14 -0700 | [diff] [blame] | 1313 | } else if (alg_k & SSL_kECDHE) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1314 | // Generate a keypair and serialize the public half. |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1315 | CBB child; |
Matthew Braithwaite | 7e06de5 | 2017-04-10 15:52:14 -0700 | [diff] [blame] | 1316 | if (!CBB_add_u8_length_prefixed(&body, &child)) { |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1317 | return ssl_hs_error; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1318 | } |
| 1319 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1320 | // Compute the premaster. |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 1321 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1322 | if (!hs->key_share->Accept(&child, &pms, &alert, hs->peer_key)) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1323 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1324 | return ssl_hs_error; |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1325 | } |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1326 | if (!CBB_flush(&body)) { |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1327 | return ssl_hs_error; |
Matt Braithwaite | f4ce8e5 | 2016-05-16 14:27:14 -0700 | [diff] [blame] | 1328 | } |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1329 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1330 | // The key exchange state may now be discarded. |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 1331 | hs->key_share.reset(); |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1332 | hs->peer_key.Reset(); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1333 | } else if (alg_k & SSL_kPSK) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1334 | // For plain PSK, other_secret is a block of 0s with the same length as |
| 1335 | // the pre-shared key. |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1336 | if (!pms.Init(psk_len)) { |
| 1337 | return ssl_hs_error; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1338 | } |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1339 | OPENSSL_memset(pms.data(), 0, pms.size()); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1340 | } else { |
| 1341 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1342 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1343 | return ssl_hs_error; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1344 | } |
| 1345 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1346 | // For a PSK cipher suite, other_secret is combined with the pre-shared |
| 1347 | // key. |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1348 | if (alg_a & SSL_aPSK) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1349 | ScopedCBB pms_cbb; |
| 1350 | CBB child; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1351 | uint8_t *new_pms; |
| 1352 | size_t new_pms_len; |
| 1353 | |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1354 | if (!CBB_init(pms_cbb.get(), 2 + psk_len + 2 + pms.size()) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1355 | !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) || |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1356 | !CBB_add_bytes(&child, pms.data(), pms.size()) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1357 | !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) || |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1358 | !CBB_add_bytes(&child, psk, psk_len) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1359 | !CBB_finish(pms_cbb.get(), &new_pms, &new_pms_len)) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1360 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1361 | return ssl_hs_error; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1362 | } |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1363 | pms.Reset(new_pms, new_pms_len); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1364 | } |
| 1365 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1366 | // The message must be added to the finished hash before calculating the |
| 1367 | // master secret. |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1368 | if (!ssl_add_message_cbb(ssl, cbb.get())) { |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1369 | return ssl_hs_error; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1370 | } |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1371 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1372 | hs->new_session->master_key_length = tls1_generate_master_secret( |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1373 | hs, hs->new_session->master_key, pms.data(), pms.size()); |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1374 | if (hs->new_session->master_key_length == 0) { |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1375 | return ssl_hs_error; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1376 | } |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 1377 | hs->new_session->extended_master_secret = hs->extended_master_secret; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1378 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1379 | hs->state = state_send_client_certificate_verify; |
| 1380 | return ssl_hs_ok; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1381 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1382 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1383 | static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1384 | SSL *const ssl = hs->ssl; |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1385 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1386 | if (!hs->cert_request || !ssl_has_certificate(ssl)) { |
David Benjamin | 4cbb931 | 2017-08-31 14:52:31 -0400 | [diff] [blame] | 1387 | hs->state = state_send_client_finished; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1388 | return ssl_hs_ok; |
| 1389 | } |
| 1390 | |
| 1391 | assert(ssl_has_private_key(ssl)); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1392 | ScopedCBB cbb; |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1393 | CBB body, child; |
| 1394 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1395 | SSL3_MT_CERTIFICATE_VERIFY)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1396 | return ssl_hs_error; |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1397 | } |
| 1398 | |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 1399 | uint16_t signature_algorithm; |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 1400 | if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1401 | return ssl_hs_error; |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 1402 | } |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1403 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1404 | // Write out the digest type in TLS 1.2. |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1405 | if (!CBB_add_u16(&body, signature_algorithm)) { |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1406 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1407 | return ssl_hs_error; |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1408 | } |
| 1409 | } |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1410 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1411 | // Set aside space for the signature. |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1412 | const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get()); |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1413 | uint8_t *ptr; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1414 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1415 | !CBB_reserve(&child, &ptr, max_sig_len)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1416 | return ssl_hs_error; |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1417 | } |
| 1418 | |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1419 | size_t sig_len = max_sig_len; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1420 | // The SSL3 construction for CertificateVerify does not decompose into a |
| 1421 | // single final digest and signature, and must be special-cased. |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 1422 | if (ssl3_protocol_version(ssl) == SSL3_VERSION) { |
| 1423 | if (ssl->cert->key_method != NULL) { |
| 1424 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1425 | return ssl_hs_error; |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1426 | } |
| 1427 | |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 1428 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 1429 | size_t digest_len; |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 1430 | if (!hs->transcript.GetSSL3CertVerifyHash( |
| 1431 | digest, &digest_len, hs->new_session.get(), signature_algorithm)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1432 | return ssl_hs_error; |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 1433 | } |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1434 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1435 | UniquePtr<EVP_PKEY_CTX> pctx(EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL)); |
| 1436 | if (!pctx || |
| 1437 | !EVP_PKEY_sign_init(pctx.get()) || |
| 1438 | !EVP_PKEY_sign(pctx.get(), ptr, &sig_len, digest, digest_len)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1439 | return ssl_hs_error; |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 1440 | } |
| 1441 | } else { |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 1442 | switch (ssl_private_key_sign( |
| 1443 | hs, ptr, &sig_len, max_sig_len, signature_algorithm, |
| 1444 | hs->transcript.buffer_data(), hs->transcript.buffer_len())) { |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 1445 | case ssl_private_key_success: |
| 1446 | break; |
| 1447 | case ssl_private_key_failure: |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1448 | return ssl_hs_error; |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 1449 | case ssl_private_key_retry: |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1450 | hs->state = state_send_client_certificate_verify; |
| 1451 | return ssl_hs_private_key_operation; |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 1452 | } |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1453 | } |
| 1454 | |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1455 | if (!CBB_did_write(&child, sig_len) || |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1456 | !ssl_add_message_cbb(ssl, cbb.get())) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1457 | return ssl_hs_error; |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1458 | } |
| 1459 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1460 | // The handshake buffer is no longer necessary. |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 1461 | hs->transcript.FreeBuffer(); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1462 | |
David Benjamin | 4cbb931 | 2017-08-31 14:52:31 -0400 | [diff] [blame] | 1463 | hs->state = state_send_client_finished; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1464 | return ssl_hs_ok; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1465 | } |
| 1466 | |
David Benjamin | 4cbb931 | 2017-08-31 14:52:31 -0400 | [diff] [blame] | 1467 | static enum ssl_hs_wait_t do_send_client_finished(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1468 | SSL *const ssl = hs->ssl; |
David Benjamin | 4cbb931 | 2017-08-31 14:52:31 -0400 | [diff] [blame] | 1469 | // Resolve Channel ID first, before any non-idempotent operations. |
| 1470 | if (ssl->s3->tlsext_channel_id_valid) { |
| 1471 | if (!ssl_do_channel_id_callback(ssl)) { |
| 1472 | return ssl_hs_error; |
| 1473 | } |
| 1474 | |
| 1475 | if (ssl->tlsext_channel_id_private == NULL) { |
| 1476 | hs->state = state_send_client_finished; |
| 1477 | return ssl_hs_channel_id_lookup; |
| 1478 | } |
| 1479 | } |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1480 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1481 | if (!ssl->method->add_change_cipher_spec(ssl) || |
David Benjamin | e58f8a6 | 2017-09-21 19:07:15 -0400 | [diff] [blame] | 1482 | !tls1_change_cipher_state(hs, evp_aead_seal)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1483 | return ssl_hs_error; |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1484 | } |
| 1485 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1486 | if (hs->next_proto_neg_seen) { |
| 1487 | static const uint8_t kZero[32] = {0}; |
| 1488 | size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32); |
| 1489 | |
| 1490 | ScopedCBB cbb; |
| 1491 | CBB body, child; |
| 1492 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_NEXT_PROTO) || |
| 1493 | !CBB_add_u8_length_prefixed(&body, &child) || |
| 1494 | !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated, |
| 1495 | ssl->s3->next_proto_negotiated_len) || |
| 1496 | !CBB_add_u8_length_prefixed(&body, &child) || |
| 1497 | !CBB_add_bytes(&child, kZero, padding_len) || |
| 1498 | !ssl_add_message_cbb(ssl, cbb.get())) { |
| 1499 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1500 | return ssl_hs_error; |
| 1501 | } |
| 1502 | } |
| 1503 | |
David Benjamin | 4cbb931 | 2017-08-31 14:52:31 -0400 | [diff] [blame] | 1504 | if (ssl->s3->tlsext_channel_id_valid) { |
| 1505 | ScopedCBB cbb; |
| 1506 | CBB body; |
| 1507 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) || |
| 1508 | !tls1_write_channel_id(hs, &body) || |
| 1509 | !ssl_add_message_cbb(ssl, cbb.get())) { |
| 1510 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1511 | return ssl_hs_error; |
| 1512 | } |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1513 | } |
| 1514 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1515 | if (!ssl3_send_finished(hs)) { |
| 1516 | return ssl_hs_error; |
| 1517 | } |
| 1518 | |
| 1519 | hs->state = state_finish_flight; |
| 1520 | return ssl_hs_flush; |
| 1521 | } |
| 1522 | |
| 1523 | static enum ssl_hs_wait_t do_finish_flight(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1524 | SSL *const ssl = hs->ssl; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1525 | if (ssl->session != NULL) { |
| 1526 | hs->state = state_finish_client_handshake; |
| 1527 | return ssl_hs_ok; |
| 1528 | } |
| 1529 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1530 | // This is a full handshake. If it involves ChannelID, then record the |
| 1531 | // handshake hashes at this point in the session so that any resumption of |
| 1532 | // this session with ChannelID can sign those hashes. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1533 | if (!tls1_record_handshake_hashes_for_channel_id(hs)) { |
| 1534 | return ssl_hs_error; |
| 1535 | } |
| 1536 | |
| 1537 | hs->state = state_read_session_ticket; |
| 1538 | |
| 1539 | if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) && |
| 1540 | ssl3_can_false_start(ssl) && |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1541 | // No False Start on renegotiation (would complicate the state machine). |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1542 | !ssl->s3->initial_handshake_complete) { |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1543 | hs->in_false_start = true; |
| 1544 | hs->can_early_write = true; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1545 | return ssl_hs_early_return; |
| 1546 | } |
| 1547 | |
| 1548 | return ssl_hs_ok; |
| 1549 | } |
| 1550 | |
| 1551 | static enum ssl_hs_wait_t do_read_session_ticket(SSL_HANDSHAKE *hs) { |
| 1552 | SSL *const ssl = hs->ssl; |
| 1553 | |
| 1554 | if (!hs->ticket_expected) { |
| 1555 | hs->state = state_process_change_cipher_spec; |
| 1556 | return ssl_hs_read_change_cipher_spec; |
| 1557 | } |
| 1558 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1559 | SSLMessage msg; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1560 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1561 | return ssl_hs_read_message; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1562 | } |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 1563 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1564 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEW_SESSION_TICKET) || |
| 1565 | !ssl_hash_message(hs, msg)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1566 | return ssl_hs_error; |
David Benjamin | 276b7e8 | 2017-01-21 14:13:39 -0500 | [diff] [blame] | 1567 | } |
| 1568 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1569 | CBS new_session_ticket = msg.body, ticket; |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 1570 | uint32_t tlsext_tick_lifetime_hint; |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 1571 | if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) || |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1572 | !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) || |
| 1573 | CBS_len(&new_session_ticket) != 0) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1574 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1575 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1576 | return ssl_hs_error; |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 1577 | } |
| 1578 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1579 | if (CBS_len(&ticket) == 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1580 | // RFC 5077 allows a server to change its mind and send no ticket after |
| 1581 | // negotiating the extension. The value of |ticket_expected| is checked in |
| 1582 | // |ssl_update_cache| so is cleared here to avoid an unnecessary update. |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1583 | hs->ticket_expected = false; |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 1584 | ssl->method->next_message(ssl); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1585 | hs->state = state_process_change_cipher_spec; |
| 1586 | return ssl_hs_read_change_cipher_spec; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1587 | } |
| 1588 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1589 | SSL_SESSION *session = hs->new_session.get(); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1590 | UniquePtr<SSL_SESSION> renewed_session; |
| 1591 | if (ssl->session != NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1592 | // The server is sending a new ticket for an existing session. Sessions are |
| 1593 | // immutable once established, so duplicate all but the ticket of the |
| 1594 | // existing session. |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1595 | renewed_session = |
| 1596 | SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1597 | if (!renewed_session) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1598 | // This should never happen. |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1599 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1600 | return ssl_hs_error; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1601 | } |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1602 | session = renewed_session.get(); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1603 | } |
| 1604 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1605 | // |tlsext_tick_lifetime_hint| is measured from when the ticket was issued. |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 1606 | ssl_session_rebase_time(ssl, session); |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame] | 1607 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1608 | if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1609 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1610 | return ssl_hs_error; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1611 | } |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 1612 | session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1613 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1614 | // Generate a session ID for this session based on the session ticket. We use |
| 1615 | // the session ID mechanism for detecting ticket resumption. This also fits in |
| 1616 | // with assumptions elsewhere in OpenSSL. |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1617 | if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), |
| 1618 | session->session_id, &session->session_id_length, |
| 1619 | EVP_sha256(), NULL)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1620 | return ssl_hs_error; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1621 | } |
| 1622 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1623 | if (renewed_session) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1624 | session->not_resumable = 0; |
| 1625 | SSL_SESSION_free(ssl->session); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1626 | ssl->session = renewed_session.release(); |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1627 | } |
| 1628 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 1629 | ssl->method->next_message(ssl); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1630 | hs->state = state_process_change_cipher_spec; |
| 1631 | return ssl_hs_read_change_cipher_spec; |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 1632 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1633 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1634 | static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) { |
David Benjamin | e58f8a6 | 2017-09-21 19:07:15 -0400 | [diff] [blame] | 1635 | if (!tls1_change_cipher_state(hs, evp_aead_open)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1636 | return ssl_hs_error; |
| 1637 | } |
| 1638 | |
| 1639 | hs->state = state_read_server_finished; |
| 1640 | return ssl_hs_ok; |
| 1641 | } |
| 1642 | |
| 1643 | static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) { |
| 1644 | SSL *const ssl = hs->ssl; |
| 1645 | enum ssl_hs_wait_t wait = ssl_get_finished(hs); |
| 1646 | if (wait != ssl_hs_ok) { |
| 1647 | return wait; |
| 1648 | } |
| 1649 | |
| 1650 | if (ssl->session != NULL) { |
David Benjamin | 4cbb931 | 2017-08-31 14:52:31 -0400 | [diff] [blame] | 1651 | hs->state = state_send_client_finished; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1652 | return ssl_hs_ok; |
| 1653 | } |
| 1654 | |
| 1655 | hs->state = state_finish_client_handshake; |
| 1656 | return ssl_hs_ok; |
| 1657 | } |
| 1658 | |
| 1659 | static enum ssl_hs_wait_t do_finish_client_handshake(SSL_HANDSHAKE *hs) { |
| 1660 | SSL *const ssl = hs->ssl; |
| 1661 | |
| 1662 | ssl->method->on_handshake_complete(ssl); |
| 1663 | |
| 1664 | SSL_SESSION_free(ssl->s3->established_session); |
| 1665 | if (ssl->session != NULL) { |
| 1666 | SSL_SESSION_up_ref(ssl->session); |
| 1667 | ssl->s3->established_session = ssl->session; |
| 1668 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1669 | // We make a copy of the session in order to maintain the immutability |
| 1670 | // of the new established_session due to False Start. The caller may |
| 1671 | // have taken a reference to the temporary session. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1672 | ssl->s3->established_session = |
| 1673 | SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_DUP_ALL) |
| 1674 | .release(); |
| 1675 | if (ssl->s3->established_session == NULL) { |
| 1676 | return ssl_hs_error; |
| 1677 | } |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1678 | // Renegotiations do not participate in session resumption. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1679 | if (!ssl->s3->initial_handshake_complete) { |
| 1680 | ssl->s3->established_session->not_resumable = 0; |
| 1681 | } |
| 1682 | |
| 1683 | hs->new_session.reset(); |
| 1684 | } |
| 1685 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1686 | hs->handshake_finalized = true; |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 1687 | ssl->s3->initial_handshake_complete = true; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1688 | ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT); |
| 1689 | |
| 1690 | hs->state = state_done; |
| 1691 | return ssl_hs_ok; |
| 1692 | } |
| 1693 | |
| 1694 | enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs) { |
| 1695 | while (hs->state != state_done) { |
| 1696 | enum ssl_hs_wait_t ret = ssl_hs_error; |
| 1697 | enum ssl_client_hs_state_t state = |
| 1698 | static_cast<enum ssl_client_hs_state_t>(hs->state); |
| 1699 | switch (state) { |
| 1700 | case state_start_connect: |
| 1701 | ret = do_start_connect(hs); |
| 1702 | break; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1703 | case state_enter_early_data: |
| 1704 | ret = do_enter_early_data(hs); |
| 1705 | break; |
| 1706 | case state_read_hello_verify_request: |
| 1707 | ret = do_read_hello_verify_request(hs); |
| 1708 | break; |
| 1709 | case state_read_server_hello: |
| 1710 | ret = do_read_server_hello(hs); |
| 1711 | break; |
| 1712 | case state_tls13: |
| 1713 | ret = do_tls13(hs); |
| 1714 | break; |
| 1715 | case state_read_server_certificate: |
| 1716 | ret = do_read_server_certificate(hs); |
| 1717 | break; |
| 1718 | case state_read_certificate_status: |
| 1719 | ret = do_read_certificate_status(hs); |
| 1720 | break; |
| 1721 | case state_verify_server_certificate: |
| 1722 | ret = do_verify_server_certificate(hs); |
| 1723 | break; |
| 1724 | case state_read_server_key_exchange: |
| 1725 | ret = do_read_server_key_exchange(hs); |
| 1726 | break; |
| 1727 | case state_read_certificate_request: |
| 1728 | ret = do_read_certificate_request(hs); |
| 1729 | break; |
| 1730 | case state_read_server_hello_done: |
| 1731 | ret = do_read_server_hello_done(hs); |
| 1732 | break; |
| 1733 | case state_send_client_certificate: |
| 1734 | ret = do_send_client_certificate(hs); |
| 1735 | break; |
| 1736 | case state_send_client_key_exchange: |
| 1737 | ret = do_send_client_key_exchange(hs); |
| 1738 | break; |
| 1739 | case state_send_client_certificate_verify: |
| 1740 | ret = do_send_client_certificate_verify(hs); |
| 1741 | break; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1742 | case state_send_client_finished: |
| 1743 | ret = do_send_client_finished(hs); |
| 1744 | break; |
| 1745 | case state_finish_flight: |
| 1746 | ret = do_finish_flight(hs); |
| 1747 | break; |
| 1748 | case state_read_session_ticket: |
| 1749 | ret = do_read_session_ticket(hs); |
| 1750 | break; |
| 1751 | case state_process_change_cipher_spec: |
| 1752 | ret = do_process_change_cipher_spec(hs); |
| 1753 | break; |
| 1754 | case state_read_server_finished: |
| 1755 | ret = do_read_server_finished(hs); |
| 1756 | break; |
| 1757 | case state_finish_client_handshake: |
| 1758 | ret = do_finish_client_handshake(hs); |
| 1759 | break; |
| 1760 | case state_done: |
| 1761 | ret = ssl_hs_ok; |
| 1762 | break; |
| 1763 | } |
| 1764 | |
| 1765 | if (hs->state != state) { |
| 1766 | ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1); |
| 1767 | } |
| 1768 | |
| 1769 | if (ret != ssl_hs_ok) { |
| 1770 | return ret; |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1); |
| 1775 | return ssl_hs_ok; |
| 1776 | } |
| 1777 | |
| 1778 | const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs) { |
| 1779 | enum ssl_client_hs_state_t state = |
| 1780 | static_cast<enum ssl_client_hs_state_t>(hs->state); |
| 1781 | switch (state) { |
| 1782 | case state_start_connect: |
| 1783 | return "TLS client start_connect"; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1784 | case state_enter_early_data: |
| 1785 | return "TLS client enter_early_data"; |
| 1786 | case state_read_hello_verify_request: |
| 1787 | return "TLS client read_hello_verify_request"; |
| 1788 | case state_read_server_hello: |
| 1789 | return "TLS client read_server_hello"; |
| 1790 | case state_tls13: |
| 1791 | return tls13_client_handshake_state(hs); |
| 1792 | case state_read_server_certificate: |
| 1793 | return "TLS client read_server_certificate"; |
| 1794 | case state_read_certificate_status: |
| 1795 | return "TLS client read_certificate_status"; |
| 1796 | case state_verify_server_certificate: |
| 1797 | return "TLS client verify_server_certificate"; |
| 1798 | case state_read_server_key_exchange: |
| 1799 | return "TLS client read_server_key_exchange"; |
| 1800 | case state_read_certificate_request: |
| 1801 | return "TLS client read_certificate_request"; |
| 1802 | case state_read_server_hello_done: |
| 1803 | return "TLS client read_server_hello_done"; |
| 1804 | case state_send_client_certificate: |
| 1805 | return "TLS client send_client_certificate"; |
| 1806 | case state_send_client_key_exchange: |
| 1807 | return "TLS client send_client_key_exchange"; |
| 1808 | case state_send_client_certificate_verify: |
| 1809 | return "TLS client send_client_certificate_verify"; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1810 | case state_send_client_finished: |
| 1811 | return "TLS client send_client_finished"; |
| 1812 | case state_finish_flight: |
| 1813 | return "TLS client finish_flight"; |
| 1814 | case state_read_session_ticket: |
| 1815 | return "TLS client read_session_ticket"; |
| 1816 | case state_process_change_cipher_spec: |
| 1817 | return "TLS client process_change_cipher_spec"; |
| 1818 | case state_read_server_finished: |
| 1819 | return "TLS client read_server_finished"; |
| 1820 | case state_finish_client_handshake: |
| 1821 | return "TLS client finish_client_handshake"; |
| 1822 | case state_done: |
| 1823 | return "TLS client done"; |
| 1824 | } |
| 1825 | |
| 1826 | return "TLS client unknown"; |
| 1827 | } |
| 1828 | |
| 1829 | } |