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