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