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