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