David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2016, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include <openssl/ssl.h> |
| 16 | |
| 17 | #include <assert.h> |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 18 | #include <limits.h> |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 19 | #include <string.h> |
| 20 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 21 | #include <utility> |
| 22 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 23 | #include <openssl/bytestring.h> |
| 24 | #include <openssl/digest.h> |
| 25 | #include <openssl/err.h> |
| 26 | #include <openssl/mem.h> |
| 27 | #include <openssl/stack.h> |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 28 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 29 | #include "../crypto/internal.h" |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 30 | #include "internal.h" |
| 31 | |
| 32 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 33 | namespace bssl { |
| 34 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 35 | enum client_hs_state_t { |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 36 | state_read_hello_retry_request = 0, |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 37 | state_send_second_client_hello, |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 38 | state_read_server_hello, |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 39 | state_read_encrypted_extensions, |
| 40 | state_read_certificate_request, |
| 41 | state_read_server_certificate, |
| 42 | state_read_server_certificate_verify, |
| 43 | state_read_server_finished, |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 44 | state_send_end_of_early_data, |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 45 | state_send_client_certificate, |
| 46 | state_send_client_certificate_verify, |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 47 | state_complete_second_flight, |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 48 | state_done, |
| 49 | }; |
| 50 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 51 | static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0}; |
| 52 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 53 | static enum ssl_hs_wait_t do_read_hello_retry_request(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 54 | SSL *const ssl = hs->ssl; |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 55 | assert(ssl->s3->have_version); |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 56 | SSLMessage msg; |
| 57 | if (!ssl->method->get_message(ssl, &msg)) { |
| 58 | return ssl_hs_read_message; |
| 59 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 60 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 61 | // Queue up a ChangeCipherSpec for whenever we next send something. This |
| 62 | // will be before the second ClientHello. If we offered early data, this was |
| 63 | // already done. |
| 64 | if (!hs->early_data_offered && |
| 65 | !ssl->method->add_change_cipher_spec(ssl)) { |
| 66 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 69 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) { |
| 70 | return ssl_hs_error; |
| 71 | } |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 72 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 73 | CBS body = msg.body, extensions, server_random, session_id; |
| 74 | uint16_t server_version, cipher_suite; |
| 75 | uint8_t compression_method; |
| 76 | if (!CBS_get_u16(&body, &server_version) || |
| 77 | !CBS_get_bytes(&body, &server_random, SSL3_RANDOM_SIZE) || |
| 78 | !CBS_get_u8_length_prefixed(&body, &session_id) || |
| 79 | !CBS_mem_equal(&session_id, hs->session_id, hs->session_id_len) || |
| 80 | !CBS_get_u16(&body, &cipher_suite) || |
| 81 | !CBS_get_u8(&body, &compression_method) || |
| 82 | compression_method != 0 || |
| 83 | !CBS_get_u16_length_prefixed(&body, &extensions) || |
| 84 | CBS_len(&extensions) == 0 || |
| 85 | CBS_len(&body) != 0) { |
| 86 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 87 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 88 | return ssl_hs_error; |
| 89 | } |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 90 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 91 | if (!CBS_mem_equal(&server_random, kHelloRetryRequest, SSL3_RANDOM_SIZE)) { |
| 92 | hs->tls13_state = state_read_server_hello; |
| 93 | return ssl_hs_ok; |
| 94 | } |
| 95 | |
| 96 | const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite); |
| 97 | // Check if the cipher is a TLS 1.3 cipher. |
| 98 | if (cipher == NULL || |
| 99 | SSL_CIPHER_get_min_version(cipher) > ssl_protocol_version(ssl) || |
| 100 | SSL_CIPHER_get_max_version(cipher) < ssl_protocol_version(ssl)) { |
| 101 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
| 102 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 103 | return ssl_hs_error; |
| 104 | } |
| 105 | |
| 106 | hs->new_cipher = cipher; |
| 107 | |
| 108 | if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) || |
| 109 | !hs->transcript.UpdateForHelloRetryRequest()) { |
| 110 | return ssl_hs_error; |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 114 | bool have_cookie, have_key_share, have_supported_versions; |
| 115 | CBS cookie, key_share, supported_versions; |
Robert Sloan | 0db7f54 | 2018-01-16 15:48:33 -0800 | [diff] [blame] | 116 | SSL_EXTENSION_TYPE ext_types[] = { |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 117 | {TLSEXT_TYPE_key_share, &have_key_share, &key_share}, |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 118 | {TLSEXT_TYPE_cookie, &have_cookie, &cookie}, |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 119 | {TLSEXT_TYPE_supported_versions, &have_supported_versions, |
| 120 | &supported_versions}, |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 121 | }; |
| 122 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 123 | uint8_t alert = SSL_AD_DECODE_ERROR; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 124 | if (!ssl_parse_extensions(&extensions, &alert, ext_types, |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 125 | OPENSSL_ARRAY_SIZE(ext_types), |
| 126 | 0 /* reject unknown */)) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 127 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 128 | return ssl_hs_error; |
| 129 | } |
| 130 | |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 131 | if (!have_cookie && !have_key_share) { |
| 132 | OPENSSL_PUT_ERROR(SSL, SSL_R_EMPTY_HELLO_RETRY_REQUEST); |
| 133 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 134 | return ssl_hs_error; |
| 135 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 136 | if (have_cookie) { |
| 137 | CBS cookie_value; |
| 138 | if (!CBS_get_u16_length_prefixed(&cookie, &cookie_value) || |
| 139 | CBS_len(&cookie_value) == 0 || |
| 140 | CBS_len(&cookie) != 0) { |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 141 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 142 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 143 | return ssl_hs_error; |
| 144 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 145 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 146 | if (!hs->cookie.CopyFrom(cookie_value)) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 147 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 151 | if (have_key_share) { |
| 152 | uint16_t group_id; |
| 153 | if (!CBS_get_u16(&key_share, &group_id) || CBS_len(&key_share) != 0) { |
| 154 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 155 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 156 | return ssl_hs_error; |
| 157 | } |
| 158 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 159 | // The group must be supported. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 160 | if (!tls1_check_group_id(hs, group_id)) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 161 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 162 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
| 163 | return ssl_hs_error; |
| 164 | } |
| 165 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 166 | // Check that the HelloRetryRequest does not request the key share that |
| 167 | // was provided in the initial ClientHello. |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 168 | if (hs->key_share->GroupID() == group_id) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 169 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 170 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
| 171 | return ssl_hs_error; |
| 172 | } |
| 173 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 174 | hs->key_share.reset(); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 175 | hs->retry_group = group_id; |
| 176 | } |
| 177 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 178 | if (!ssl_hash_message(hs, msg)) { |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 179 | return ssl_hs_error; |
| 180 | } |
| 181 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 182 | ssl->method->next_message(ssl); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 183 | hs->received_hello_retry_request = true; |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 184 | hs->tls13_state = state_send_second_client_hello; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 185 | // 0-RTT is rejected if we receive a HelloRetryRequest. |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 186 | if (hs->in_early_data) { |
| 187 | return ssl_hs_early_data_rejected; |
| 188 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 189 | return ssl_hs_ok; |
| 190 | } |
| 191 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 192 | static enum ssl_hs_wait_t do_send_second_client_hello(SSL_HANDSHAKE *hs) { |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 193 | SSL *const ssl = hs->ssl; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 194 | // Restore the null cipher. We may have switched due to 0-RTT. |
Robert Sloan | db4251a | 2017-09-18 09:38:15 -0700 | [diff] [blame] | 195 | bssl::UniquePtr<SSLAEADContext> null_ctx = |
| 196 | SSLAEADContext::CreateNullCipher(SSL_is_dtls(ssl)); |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 197 | if (!null_ctx || |
Robert Sloan | db4251a | 2017-09-18 09:38:15 -0700 | [diff] [blame] | 198 | !ssl->method->set_write_state(ssl, std::move(null_ctx))) { |
| 199 | return ssl_hs_error; |
| 200 | } |
| 201 | |
| 202 | ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version); |
| 203 | |
| 204 | if (!ssl_write_client_hello(hs)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 205 | return ssl_hs_error; |
| 206 | } |
| 207 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 208 | hs->tls13_state = state_read_server_hello; |
| 209 | return ssl_hs_flush; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 210 | } |
| 211 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 212 | static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 213 | SSL *const ssl = hs->ssl; |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 214 | SSLMessage msg; |
| 215 | if (!ssl->method->get_message(ssl, &msg)) { |
| 216 | return ssl_hs_read_message; |
| 217 | } |
| 218 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 219 | return ssl_hs_error; |
| 220 | } |
| 221 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 222 | CBS body = msg.body, server_random, session_id, extensions; |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 223 | uint16_t server_version; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 224 | uint16_t cipher_suite; |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 225 | uint8_t compression_method; |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 226 | if (!CBS_get_u16(&body, &server_version) || |
| 227 | !CBS_get_bytes(&body, &server_random, SSL3_RANDOM_SIZE) || |
Robert Sloan | 0da4395 | 2018-01-03 15:13:14 -0800 | [diff] [blame] | 228 | !CBS_get_u8_length_prefixed(&body, &session_id) || |
| 229 | !CBS_mem_equal(&session_id, hs->session_id, hs->session_id_len) || |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 230 | !CBS_get_u16(&body, &cipher_suite) || |
Robert Sloan | 0da4395 | 2018-01-03 15:13:14 -0800 | [diff] [blame] | 231 | !CBS_get_u8(&body, &compression_method) || |
| 232 | compression_method != 0 || |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 233 | !CBS_get_u16_length_prefixed(&body, &extensions) || |
| 234 | CBS_len(&body) != 0) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 235 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 236 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 237 | return ssl_hs_error; |
| 238 | } |
| 239 | |
Robert Sloan | 0da4395 | 2018-01-03 15:13:14 -0800 | [diff] [blame] | 240 | if (server_version != TLS1_2_VERSION) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 241 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 242 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER); |
| 243 | return ssl_hs_error; |
| 244 | } |
| 245 | |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 246 | // Forbid a second HelloRetryRequest. |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 247 | if (CBS_mem_equal(&server_random, kHelloRetryRequest, SSL3_RANDOM_SIZE)) { |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 248 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 249 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
| 250 | return ssl_hs_error; |
| 251 | } |
| 252 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 253 | OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random), |
| 254 | SSL3_RANDOM_SIZE); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 255 | |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 256 | // Check if the cipher is a TLS 1.3 cipher. |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 257 | const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite); |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 258 | if (cipher == nullptr || |
| 259 | SSL_CIPHER_get_min_version(cipher) > ssl_protocol_version(ssl) || |
| 260 | SSL_CIPHER_get_max_version(cipher) < ssl_protocol_version(ssl)) { |
| 261 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 262 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 263 | return ssl_hs_error; |
| 264 | } |
| 265 | |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 266 | // Check that the cipher matches the one in the HelloRetryRequest. |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 267 | if (hs->received_hello_retry_request && |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 268 | hs->new_cipher != cipher) { |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 269 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 270 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 271 | return ssl_hs_error; |
| 272 | } |
| 273 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 274 | // Parse out the extensions. |
| 275 | bool have_key_share = false, have_pre_shared_key = false, |
| 276 | have_supported_versions = false; |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 277 | CBS key_share, pre_shared_key, supported_versions; |
Robert Sloan | 0db7f54 | 2018-01-16 15:48:33 -0800 | [diff] [blame] | 278 | SSL_EXTENSION_TYPE ext_types[] = { |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 279 | {TLSEXT_TYPE_key_share, &have_key_share, &key_share}, |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 280 | {TLSEXT_TYPE_pre_shared_key, &have_pre_shared_key, &pre_shared_key}, |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 281 | {TLSEXT_TYPE_supported_versions, &have_supported_versions, |
| 282 | &supported_versions}, |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 283 | }; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 284 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 285 | uint8_t alert = SSL_AD_DECODE_ERROR; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 286 | if (!ssl_parse_extensions(&extensions, &alert, ext_types, |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 287 | OPENSSL_ARRAY_SIZE(ext_types), |
| 288 | 0 /* reject unknown */)) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 289 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 290 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 291 | } |
| 292 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 293 | if (ssl_is_draft28(ssl->version)) { |
| 294 | // Recheck supported_versions, in case this is the second ServerHello. |
| 295 | uint16_t version; |
| 296 | if (!have_supported_versions || |
| 297 | !CBS_get_u16(&supported_versions, &version) || |
| 298 | version != ssl->version) { |
| 299 | OPENSSL_PUT_ERROR(SSL, SSL_R_SECOND_SERVERHELLO_VERSION_MISMATCH); |
| 300 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 301 | return ssl_hs_error; |
| 302 | } |
| 303 | } |
| 304 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 305 | alert = SSL_AD_DECODE_ERROR; |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 306 | if (have_pre_shared_key) { |
| 307 | if (ssl->session == NULL) { |
| 308 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 309 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 310 | return ssl_hs_error; |
| 311 | } |
| 312 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 313 | if (!ssl_ext_pre_shared_key_parse_serverhello(hs, &alert, |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 314 | &pre_shared_key)) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 315 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 316 | return ssl_hs_error; |
| 317 | } |
| 318 | |
| 319 | if (ssl->session->ssl_version != ssl->version) { |
| 320 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 321 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 322 | return ssl_hs_error; |
| 323 | } |
| 324 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 325 | if (ssl->session->cipher->algorithm_prf != cipher->algorithm_prf) { |
| 326 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_PRF_HASH_MISMATCH); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 327 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 328 | return ssl_hs_error; |
| 329 | } |
| 330 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 331 | if (!ssl_session_is_context_valid(hs, ssl->session.get())) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 332 | // This is actually a client application bug. |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 333 | OPENSSL_PUT_ERROR(SSL, |
| 334 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 335 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 336 | return ssl_hs_error; |
| 337 | } |
| 338 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 339 | ssl->s3->session_reused = true; |
| 340 | // Only authentication information carries over in TLS 1.3. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 341 | hs->new_session = |
| 342 | SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_DUP_AUTH_ONLY); |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 343 | if (!hs->new_session) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 344 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 345 | return ssl_hs_error; |
| 346 | } |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 347 | ssl_set_session(ssl, NULL); |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 348 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 349 | // Resumption incorporates fresh key material, so refresh the timeout. |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 350 | ssl_session_renew_timeout(ssl, hs->new_session.get(), |
Robert Sloan | 1c9db53 | 2017-03-13 08:03:59 -0700 | [diff] [blame] | 351 | ssl->session_ctx->session_psk_dhe_timeout); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 352 | } else if (!ssl_get_new_session(hs, 0)) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 353 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 354 | return ssl_hs_error; |
| 355 | } |
| 356 | |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 357 | hs->new_session->cipher = cipher; |
| 358 | hs->new_cipher = cipher; |
| 359 | |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 360 | size_t hash_len = |
| 361 | EVP_MD_size(ssl_get_handshake_digest(ssl_protocol_version(ssl), cipher)); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 362 | |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 363 | // Set up the key schedule and incorporate the PSK into the running secret. |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 364 | if (ssl->s3->session_reused) { |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 365 | if (!tls13_init_key_schedule(hs, hs->new_session->master_key, |
| 366 | hs->new_session->master_key_length)) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 367 | return ssl_hs_error; |
| 368 | } |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 369 | } else if (!tls13_init_key_schedule(hs, kZeroes, hash_len)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 370 | return ssl_hs_error; |
| 371 | } |
| 372 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 373 | if (!have_key_share) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 374 | // We do not support psk_ke and thus always require a key share. |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 375 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 376 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION); |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 377 | return ssl_hs_error; |
| 378 | } |
| 379 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 380 | // Resolve ECDHE and incorporate it into the secret. |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 381 | Array<uint8_t> dhe_secret; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 382 | alert = SSL_AD_DECODE_ERROR; |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 383 | if (!ssl_ext_key_share_parse_serverhello(hs, &dhe_secret, &alert, |
| 384 | &key_share)) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 385 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 386 | return ssl_hs_error; |
| 387 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 388 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 389 | if (!tls13_advance_key_schedule(hs, dhe_secret.data(), dhe_secret.size()) || |
| 390 | !ssl_hash_message(hs, msg) || |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 391 | !tls13_derive_handshake_secrets(hs) || |
| 392 | !tls13_set_traffic_key(ssl, evp_aead_open, hs->server_handshake_secret, |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 393 | hs->hash_len)) { |
| 394 | return ssl_hs_error; |
| 395 | } |
| 396 | |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 397 | if (!hs->early_data_offered) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 398 | // If not sending early data, set client traffic keys now so that alerts are |
| 399 | // encrypted. |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 400 | if (!tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_handshake_secret, |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 401 | hs->hash_len)) { |
| 402 | return ssl_hs_error; |
| 403 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 404 | } |
| 405 | |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 406 | ssl->method->next_message(ssl); |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 407 | hs->tls13_state = state_read_encrypted_extensions; |
| 408 | return ssl_hs_ok; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 409 | } |
| 410 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 411 | static enum ssl_hs_wait_t do_read_encrypted_extensions(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 412 | SSL *const ssl = hs->ssl; |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 413 | SSLMessage msg; |
| 414 | if (!ssl->method->get_message(ssl, &msg)) { |
| 415 | return ssl_hs_read_message; |
| 416 | } |
| 417 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_ENCRYPTED_EXTENSIONS)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 418 | return ssl_hs_error; |
| 419 | } |
| 420 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 421 | CBS body = msg.body; |
| 422 | if (!ssl_parse_serverhello_tlsext(hs, &body)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 423 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 424 | return ssl_hs_error; |
| 425 | } |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 426 | if (CBS_len(&body) != 0) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 427 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 428 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 429 | return ssl_hs_error; |
| 430 | } |
| 431 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 432 | // Store the negotiated ALPN in the session. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 433 | if (!hs->new_session->early_alpn.CopyFrom(ssl->s3->alpn_selected)) { |
| 434 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 435 | return ssl_hs_error; |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Robert Sloan | 0da4395 | 2018-01-03 15:13:14 -0800 | [diff] [blame] | 438 | if (ssl->s3->early_data_accepted) { |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 439 | if (hs->early_session->cipher != hs->new_session->cipher || |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 440 | MakeConstSpan(hs->early_session->early_alpn) != |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 441 | ssl->s3->alpn_selected) { |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 442 | OPENSSL_PUT_ERROR(SSL, SSL_R_ALPN_MISMATCH_ON_EARLY_DATA); |
| 443 | return ssl_hs_error; |
| 444 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 445 | if (ssl->s3->channel_id_valid || ssl->s3->token_binding_negotiated) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 446 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION_ON_EARLY_DATA); |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 447 | return ssl_hs_error; |
| 448 | } |
| 449 | } |
| 450 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 451 | if (!ssl_hash_message(hs, msg)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 452 | return ssl_hs_error; |
| 453 | } |
| 454 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 455 | ssl->method->next_message(ssl); |
| 456 | hs->tls13_state = state_read_certificate_request; |
Robert Sloan | 0da4395 | 2018-01-03 15:13:14 -0800 | [diff] [blame] | 457 | if (hs->in_early_data && !ssl->s3->early_data_accepted) { |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 458 | return ssl_hs_early_data_rejected; |
| 459 | } |
| 460 | return ssl_hs_ok; |
| 461 | } |
| 462 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 463 | static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 464 | SSL *const ssl = hs->ssl; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 465 | // CertificateRequest may only be sent in non-resumption handshakes. |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 466 | if (ssl->s3->session_reused) { |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 467 | hs->tls13_state = state_read_server_finished; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 468 | return ssl_hs_ok; |
| 469 | } |
| 470 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 471 | SSLMessage msg; |
| 472 | if (!ssl->method->get_message(ssl, &msg)) { |
| 473 | return ssl_hs_read_message; |
| 474 | } |
| 475 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 476 | // CertificateRequest is optional. |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 477 | if (msg.type != SSL3_MT_CERTIFICATE_REQUEST) { |
| 478 | hs->tls13_state = state_read_server_certificate; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 479 | return ssl_hs_ok; |
| 480 | } |
| 481 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 482 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 483 | bool have_sigalgs = false, have_ca = false; |
| 484 | CBS sigalgs, ca; |
| 485 | const SSL_EXTENSION_TYPE ext_types[] = { |
| 486 | {TLSEXT_TYPE_signature_algorithms, &have_sigalgs, &sigalgs}, |
| 487 | {TLSEXT_TYPE_certificate_authorities, &have_ca, &ca}, |
| 488 | }; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 489 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 490 | CBS body = msg.body, context, extensions, supported_signature_algorithms; |
| 491 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 492 | if (!CBS_get_u8_length_prefixed(&body, &context) || |
| 493 | // The request context is always empty during the handshake. |
| 494 | CBS_len(&context) != 0 || |
| 495 | !CBS_get_u16_length_prefixed(&body, &extensions) || |
| 496 | CBS_len(&body) != 0 || |
| 497 | !ssl_parse_extensions(&extensions, &alert, ext_types, |
| 498 | OPENSSL_ARRAY_SIZE(ext_types), |
| 499 | 1 /* accept unknown */) || |
| 500 | (have_ca && CBS_len(&ca) == 0) || |
| 501 | !have_sigalgs || |
| 502 | !CBS_get_u16_length_prefixed(&sigalgs, |
| 503 | &supported_signature_algorithms) || |
| 504 | CBS_len(&supported_signature_algorithms) == 0 || |
| 505 | !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) { |
| 506 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 507 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 508 | return ssl_hs_error; |
| 509 | } |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 510 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 511 | if (have_ca) { |
| 512 | hs->ca_names = ssl_parse_client_CA_list(ssl, &alert, &ca); |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 513 | if (!hs->ca_names) { |
| 514 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 515 | return ssl_hs_error; |
| 516 | } |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 517 | } else { |
| 518 | hs->ca_names.reset(sk_CRYPTO_BUFFER_new_null()); |
| 519 | if (!hs->ca_names) { |
| 520 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 521 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 522 | return ssl_hs_error; |
| 523 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 524 | } |
| 525 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 526 | hs->cert_request = true; |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 527 | ssl->ctx->x509_method->hs_flush_cached_ca_names(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 528 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 529 | if (!ssl_hash_message(hs, msg)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 530 | return ssl_hs_error; |
| 531 | } |
| 532 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 533 | ssl->method->next_message(ssl); |
| 534 | hs->tls13_state = state_read_server_certificate; |
| 535 | return ssl_hs_ok; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 536 | } |
| 537 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 538 | static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 539 | SSL *const ssl = hs->ssl; |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 540 | SSLMessage msg; |
| 541 | if (!ssl->method->get_message(ssl, &msg)) { |
| 542 | return ssl_hs_read_message; |
| 543 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 544 | |
| 545 | if (msg.type != SSL3_MT_COMPRESSED_CERTIFICATE && |
| 546 | !ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE)) { |
| 547 | return ssl_hs_error; |
| 548 | } |
| 549 | |
| 550 | if (!tls13_process_certificate(hs, msg, 0 /* certificate required */) || |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 551 | !ssl_hash_message(hs, msg)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 552 | return ssl_hs_error; |
| 553 | } |
| 554 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 555 | ssl->method->next_message(ssl); |
| 556 | hs->tls13_state = state_read_server_certificate_verify; |
| 557 | return ssl_hs_ok; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 558 | } |
| 559 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 560 | static enum ssl_hs_wait_t do_read_server_certificate_verify( |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 561 | SSL_HANDSHAKE *hs) { |
| 562 | SSL *const ssl = hs->ssl; |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 563 | SSLMessage msg; |
| 564 | if (!ssl->method->get_message(ssl, &msg)) { |
| 565 | return ssl_hs_read_message; |
| 566 | } |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 567 | switch (ssl_verify_peer_cert(hs)) { |
| 568 | case ssl_verify_ok: |
| 569 | break; |
| 570 | case ssl_verify_invalid: |
| 571 | return ssl_hs_error; |
| 572 | case ssl_verify_retry: |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 573 | hs->tls13_state = state_read_server_certificate_verify; |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 574 | return ssl_hs_certificate_verify; |
| 575 | } |
| 576 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 577 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY) || |
| 578 | !tls13_process_certificate_verify(hs, msg) || |
| 579 | !ssl_hash_message(hs, msg)) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 580 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 581 | } |
| 582 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 583 | ssl->method->next_message(ssl); |
| 584 | hs->tls13_state = state_read_server_finished; |
| 585 | return ssl_hs_ok; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 586 | } |
| 587 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 588 | static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 589 | SSL *const ssl = hs->ssl; |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 590 | SSLMessage msg; |
| 591 | if (!ssl->method->get_message(ssl, &msg)) { |
| 592 | return ssl_hs_read_message; |
| 593 | } |
| 594 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED) || |
| 595 | !tls13_process_finished(hs, msg, 0 /* don't use saved value */) || |
| 596 | !ssl_hash_message(hs, msg) || |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 597 | // Update the secret to the master secret and derive traffic keys. |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 598 | !tls13_advance_key_schedule(hs, kZeroes, hs->hash_len) || |
| 599 | !tls13_derive_application_secrets(hs)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 600 | return ssl_hs_error; |
| 601 | } |
| 602 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 603 | ssl->method->next_message(ssl); |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 604 | hs->tls13_state = state_send_end_of_early_data; |
| 605 | return ssl_hs_ok; |
| 606 | } |
| 607 | |
| 608 | static enum ssl_hs_wait_t do_send_end_of_early_data(SSL_HANDSHAKE *hs) { |
| 609 | SSL *const ssl = hs->ssl; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 610 | |
Robert Sloan | 0da4395 | 2018-01-03 15:13:14 -0800 | [diff] [blame] | 611 | if (ssl->s3->early_data_accepted) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 612 | hs->can_early_write = false; |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 613 | ScopedCBB cbb; |
| 614 | CBB body; |
| 615 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
| 616 | SSL3_MT_END_OF_EARLY_DATA) || |
| 617 | !ssl_add_message_cbb(ssl, cbb.get())) { |
| 618 | return ssl_hs_error; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 619 | } |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 622 | if (hs->early_data_offered) { |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 623 | if (!tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_handshake_secret, |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 624 | hs->hash_len)) { |
| 625 | return ssl_hs_error; |
| 626 | } |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 629 | hs->tls13_state = state_send_client_certificate; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 630 | return ssl_hs_ok; |
| 631 | } |
| 632 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 633 | static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 634 | SSL *const ssl = hs->ssl; |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 635 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 636 | // The peer didn't request a certificate. |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 637 | if (!hs->cert_request) { |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 638 | hs->tls13_state = state_complete_second_flight; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 639 | return ssl_hs_ok; |
| 640 | } |
| 641 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 642 | // Call cert_cb to update the certificate. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 643 | if (hs->config->cert->cert_cb != NULL) { |
| 644 | int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 645 | if (rv == 0) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 646 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 647 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
| 648 | return ssl_hs_error; |
| 649 | } |
| 650 | if (rv < 0) { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 651 | hs->tls13_state = state_send_client_certificate; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 652 | return ssl_hs_x509_lookup; |
| 653 | } |
| 654 | } |
| 655 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 656 | if (!ssl_on_certificate_selected(hs) || |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 657 | !tls13_add_certificate(hs)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 658 | return ssl_hs_error; |
| 659 | } |
| 660 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 661 | hs->tls13_state = state_send_client_certificate_verify; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 662 | return ssl_hs_ok; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 663 | } |
| 664 | |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 665 | static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 666 | // Don't send CertificateVerify if there is no certificate. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 667 | if (!ssl_has_certificate(hs->config)) { |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 668 | hs->tls13_state = state_complete_second_flight; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 669 | return ssl_hs_ok; |
| 670 | } |
| 671 | |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 672 | switch (tls13_add_certificate_verify(hs)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 673 | case ssl_private_key_success: |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 674 | hs->tls13_state = state_complete_second_flight; |
| 675 | return ssl_hs_ok; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 676 | |
| 677 | case ssl_private_key_retry: |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 678 | hs->tls13_state = state_send_client_certificate_verify; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 679 | return ssl_hs_private_key_operation; |
| 680 | |
| 681 | case ssl_private_key_failure: |
| 682 | return ssl_hs_error; |
| 683 | } |
| 684 | |
| 685 | assert(0); |
| 686 | return ssl_hs_error; |
| 687 | } |
| 688 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 689 | static enum ssl_hs_wait_t do_complete_second_flight(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 690 | SSL *const ssl = hs->ssl; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 691 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 692 | // Send a Channel ID assertion if necessary. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 693 | if (ssl->s3->channel_id_valid) { |
| 694 | if (!ssl_do_channel_id_callback(hs)) { |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 695 | hs->tls13_state = state_complete_second_flight; |
| 696 | return ssl_hs_error; |
| 697 | } |
| 698 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 699 | if (hs->config->channel_id_private == NULL) { |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 700 | return ssl_hs_channel_id_lookup; |
| 701 | } |
| 702 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 703 | ScopedCBB cbb; |
| 704 | CBB body; |
| 705 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) || |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 706 | !tls1_write_channel_id(hs, &body) || |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 707 | !ssl_add_message_cbb(ssl, cbb.get())) { |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 708 | return ssl_hs_error; |
| 709 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 710 | } |
| 711 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 712 | // Send a Finished message. |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 713 | if (!tls13_add_finished(hs)) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 714 | return ssl_hs_error; |
| 715 | } |
| 716 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 717 | // Derive the final keys and enable them. |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 718 | if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->server_traffic_secret_0, |
| 719 | hs->hash_len) || |
| 720 | !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_traffic_secret_0, |
| 721 | hs->hash_len) || |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 722 | !tls13_derive_resumption_secret(hs)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 723 | return ssl_hs_error; |
| 724 | } |
| 725 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 726 | hs->tls13_state = state_done; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 727 | return ssl_hs_flush; |
| 728 | } |
| 729 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 730 | enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs) { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 731 | while (hs->tls13_state != state_done) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 732 | enum ssl_hs_wait_t ret = ssl_hs_error; |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 733 | enum client_hs_state_t state = |
| 734 | static_cast<enum client_hs_state_t>(hs->tls13_state); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 735 | switch (state) { |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 736 | case state_read_hello_retry_request: |
| 737 | ret = do_read_hello_retry_request(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 738 | break; |
| 739 | case state_send_second_client_hello: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 740 | ret = do_send_second_client_hello(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 741 | break; |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 742 | case state_read_server_hello: |
| 743 | ret = do_read_server_hello(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 744 | break; |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 745 | case state_read_encrypted_extensions: |
| 746 | ret = do_read_encrypted_extensions(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 747 | break; |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 748 | case state_read_certificate_request: |
| 749 | ret = do_read_certificate_request(hs); |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 750 | break; |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 751 | case state_read_server_certificate: |
| 752 | ret = do_read_server_certificate(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 753 | break; |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 754 | case state_read_server_certificate_verify: |
| 755 | ret = do_read_server_certificate_verify(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 756 | break; |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 757 | case state_read_server_finished: |
| 758 | ret = do_read_server_finished(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 759 | break; |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 760 | case state_send_end_of_early_data: |
| 761 | ret = do_send_end_of_early_data(hs); |
| 762 | break; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 763 | case state_send_client_certificate: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 764 | ret = do_send_client_certificate(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 765 | break; |
| 766 | case state_send_client_certificate_verify: |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 767 | ret = do_send_client_certificate_verify(hs); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 768 | break; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 769 | case state_complete_second_flight: |
| 770 | ret = do_complete_second_flight(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 771 | break; |
| 772 | case state_done: |
| 773 | ret = ssl_hs_ok; |
| 774 | break; |
| 775 | } |
| 776 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 777 | if (hs->tls13_state != state) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 778 | ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1); |
| 779 | } |
| 780 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 781 | if (ret != ssl_hs_ok) { |
| 782 | return ret; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | return ssl_hs_ok; |
| 787 | } |
| 788 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 789 | const char *tls13_client_handshake_state(SSL_HANDSHAKE *hs) { |
| 790 | enum client_hs_state_t state = |
| 791 | static_cast<enum client_hs_state_t>(hs->tls13_state); |
| 792 | switch (state) { |
| 793 | case state_read_hello_retry_request: |
| 794 | return "TLS 1.3 client read_hello_retry_request"; |
| 795 | case state_send_second_client_hello: |
| 796 | return "TLS 1.3 client send_second_client_hello"; |
| 797 | case state_read_server_hello: |
| 798 | return "TLS 1.3 client read_server_hello"; |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 799 | case state_read_encrypted_extensions: |
| 800 | return "TLS 1.3 client read_encrypted_extensions"; |
| 801 | case state_read_certificate_request: |
| 802 | return "TLS 1.3 client read_certificate_request"; |
| 803 | case state_read_server_certificate: |
| 804 | return "TLS 1.3 client read_server_certificate"; |
| 805 | case state_read_server_certificate_verify: |
| 806 | return "TLS 1.3 client read_server_certificate_verify"; |
| 807 | case state_read_server_finished: |
| 808 | return "TLS 1.3 client read_server_finished"; |
| 809 | case state_send_end_of_early_data: |
| 810 | return "TLS 1.3 client send_end_of_early_data"; |
| 811 | case state_send_client_certificate: |
| 812 | return "TLS 1.3 client send_client_certificate"; |
| 813 | case state_send_client_certificate_verify: |
| 814 | return "TLS 1.3 client send_client_certificate_verify"; |
| 815 | case state_complete_second_flight: |
| 816 | return "TLS 1.3 client complete_second_flight"; |
| 817 | case state_done: |
| 818 | return "TLS 1.3 client done"; |
| 819 | } |
| 820 | |
| 821 | return "TLS 1.3 client unknown"; |
| 822 | } |
| 823 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 824 | int tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg) { |
Robert Sloan | 3627296 | 2017-10-23 10:28:39 -0700 | [diff] [blame] | 825 | if (ssl->s3->write_shutdown != ssl_shutdown_none) { |
| 826 | // Ignore tickets on shutdown. Callers tend to indiscriminately call |
| 827 | // |SSL_shutdown| before destroying an |SSL|, at which point calling the new |
| 828 | // session callback may be confusing. |
| 829 | return 1; |
| 830 | } |
| 831 | |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 832 | UniquePtr<SSL_SESSION> session = SSL_SESSION_dup( |
| 833 | ssl->s3->established_session.get(), SSL_SESSION_INCLUDE_NONAUTH); |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 834 | if (!session) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 835 | return 0; |
| 836 | } |
| 837 | |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 838 | ssl_session_rebase_time(ssl, session.get()); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 839 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 840 | uint32_t server_timeout; |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 841 | CBS body = msg.body, ticket_nonce, ticket, extensions; |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 842 | if (!CBS_get_u32(&body, &server_timeout) || |
| 843 | !CBS_get_u32(&body, &session->ticket_age_add) || |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 844 | !CBS_get_u8_length_prefixed(&body, &ticket_nonce) || |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 845 | !CBS_get_u16_length_prefixed(&body, &ticket) || |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 846 | !session->ticket.CopyFrom(ticket) || |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 847 | !CBS_get_u16_length_prefixed(&body, &extensions) || |
| 848 | CBS_len(&body) != 0) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 849 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 850 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 851 | return 0; |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 852 | } |
| 853 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 854 | // Cap the renewable lifetime by the server advertised value. This avoids |
| 855 | // wasting bandwidth on 0-RTT when we know the server will reject it. |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 856 | if (session->timeout > server_timeout) { |
| 857 | session->timeout = server_timeout; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 858 | } |
| 859 | |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 860 | if (!tls13_derive_session_psk(session.get(), ticket_nonce)) { |
| 861 | return 0; |
| 862 | } |
| 863 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 864 | // Parse out the extensions. |
| 865 | bool have_early_data_info = false; |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 866 | CBS early_data_info; |
| 867 | const SSL_EXTENSION_TYPE ext_types[] = { |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 868 | {TLSEXT_TYPE_early_data, &have_early_data_info, &early_data_info}, |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 869 | }; |
| 870 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 871 | uint8_t alert = SSL_AD_DECODE_ERROR; |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 872 | if (!ssl_parse_extensions(&extensions, &alert, ext_types, |
| 873 | OPENSSL_ARRAY_SIZE(ext_types), |
| 874 | 1 /* ignore unknown */)) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 875 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 876 | return 0; |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 877 | } |
| 878 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 879 | if (have_early_data_info && ssl->enable_early_data) { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 880 | if (!CBS_get_u32(&early_data_info, &session->ticket_max_early_data) || |
| 881 | CBS_len(&early_data_info) != 0) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 882 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 883 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 884 | return 0; |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 885 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 886 | } |
| 887 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 888 | session->ticket_age_add_valid = true; |
| 889 | session->not_resumable = false; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 890 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 891 | if ((ssl->session_ctx->session_cache_mode & SSL_SESS_CACHE_CLIENT) && |
| 892 | ssl->session_ctx->new_session_cb != NULL && |
| 893 | ssl->session_ctx->new_session_cb(ssl, session.get())) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 894 | // |new_session_cb|'s return value signals that it took ownership. |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 895 | session.release(); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 896 | } |
| 897 | |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 898 | return 1; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 899 | } |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 900 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 901 | } // namespace bssl |