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