Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -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> |
| 18 | #include <string.h> |
| 19 | |
Adam Langley | 4bfab5d | 2019-01-23 13:52:17 -0800 | [diff] [blame] | 20 | #include <tuple> |
| 21 | |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 22 | #include <openssl/aead.h> |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 23 | #include <openssl/bytestring.h> |
| 24 | #include <openssl/digest.h> |
| 25 | #include <openssl/err.h> |
David Benjamin | 070a6c3 | 2021-05-05 15:39:27 -0400 | [diff] [blame] | 26 | #include <openssl/hpke.h> |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 27 | #include <openssl/mem.h> |
| 28 | #include <openssl/rand.h> |
| 29 | #include <openssl/stack.h> |
| 30 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 31 | #include "../crypto/internal.h" |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 32 | #include "internal.h" |
| 33 | |
| 34 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 35 | BSSL_NAMESPACE_BEGIN |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 36 | |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 37 | static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0}; |
| 38 | |
David Benjamin | 6433a91 | 2019-05-04 14:17:08 -0500 | [diff] [blame] | 39 | // Allow a minute of ticket age skew in either direction. This covers |
| 40 | // transmission delays in ClientHello and NewSessionTicket, as well as |
| 41 | // drift between client and server clock rate since the ticket was issued. |
| 42 | // See RFC 8446, section 8.3. |
| 43 | static const int32_t kMaxTicketAgeSkewSeconds = 60; |
| 44 | |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 45 | static bool resolve_ecdhe_secret(SSL_HANDSHAKE *hs, |
| 46 | const SSL_CLIENT_HELLO *client_hello) { |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 47 | SSL *const ssl = hs->ssl; |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 48 | const uint16_t group_id = hs->new_session->group_id; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 49 | |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 50 | bool found_key_share; |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 51 | Span<const uint8_t> peer_key; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 52 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 53 | if (!ssl_ext_key_share_parse_clienthello(hs, &found_key_share, &peer_key, |
| 54 | &alert, client_hello)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 55 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 56 | return false; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | if (!found_key_share) { |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 60 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 61 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
| 62 | return false; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 63 | } |
| 64 | |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 65 | Array<uint8_t> secret; |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 66 | SSL_HANDSHAKE_HINTS *const hints = hs->hints.get(); |
| 67 | if (hints && !hs->hints_requested && hints->key_share_group_id == group_id && |
| 68 | !hints->key_share_secret.empty()) { |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 69 | // Copy the key_share secret from hints. |
| 70 | if (!hs->key_share_ciphertext.CopyFrom(hints->key_share_ciphertext) || |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 71 | !secret.CopyFrom(hints->key_share_secret)) { |
| 72 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 73 | return false; |
| 74 | } |
| 75 | } else { |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 76 | ScopedCBB ciphertext; |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 77 | UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id); |
| 78 | if (!key_share || // |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 79 | !CBB_init(ciphertext.get(), 32) || |
| 80 | !key_share->Encap(ciphertext.get(), &secret, &alert, peer_key) || |
| 81 | !CBBFinishArray(ciphertext.get(), &hs->key_share_ciphertext)) { |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 82 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 83 | return false; |
| 84 | } |
| 85 | if (hints && hs->hints_requested) { |
| 86 | hints->key_share_group_id = group_id; |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 87 | if (!hints->key_share_ciphertext.CopyFrom(hs->key_share_ciphertext) || |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 88 | !hints->key_share_secret.CopyFrom(secret)) { |
| 89 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 90 | return false; |
| 91 | } |
| 92 | } |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | return tls13_advance_key_schedule(hs, secret); |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 96 | } |
| 97 | |
Steven Valdez | 038da9b | 2017-07-10 12:57:25 -0400 | [diff] [blame] | 98 | static int ssl_ext_supported_versions_add_serverhello(SSL_HANDSHAKE *hs, |
| 99 | CBB *out) { |
| 100 | CBB contents; |
| 101 | if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) || |
| 102 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 103 | !CBB_add_u16(&contents, hs->ssl->version) || |
| 104 | !CBB_flush(out)) { |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | return 1; |
| 109 | } |
| 110 | |
David Benjamin | 34202b9 | 2016-11-16 19:07:53 +0900 | [diff] [blame] | 111 | static const SSL_CIPHER *choose_tls13_cipher( |
David Benjamin | 43cc9c6 | 2018-12-12 13:06:46 -0600 | [diff] [blame] | 112 | const SSL *ssl, const SSL_CLIENT_HELLO *client_hello, uint16_t group_id) { |
David Benjamin | 34202b9 | 2016-11-16 19:07:53 +0900 | [diff] [blame] | 113 | CBS cipher_suites; |
| 114 | CBS_init(&cipher_suites, client_hello->cipher_suites, |
| 115 | client_hello->cipher_suites_len); |
| 116 | |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 117 | const uint16_t version = ssl_protocol_version(ssl); |
David Benjamin | 34202b9 | 2016-11-16 19:07:53 +0900 | [diff] [blame] | 118 | |
Bob Beck | 8cacbd9 | 2023-04-05 14:38:38 -0600 | [diff] [blame] | 119 | return ssl_choose_tls13_cipher( |
| 120 | cipher_suites, |
| 121 | ssl->config->aes_hw_override ? ssl->config->aes_hw_override_value |
| 122 | : EVP_has_aes_hardware(), |
Adam Langley | 2f6409e | 2023-04-10 21:09:11 +0000 | [diff] [blame] | 123 | version, group_id, ssl->config->tls13_cipher_policy); |
David Benjamin | 34202b9 | 2016-11-16 19:07:53 +0900 | [diff] [blame] | 124 | } |
| 125 | |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 126 | static bool add_new_session_tickets(SSL_HANDSHAKE *hs, bool *out_sent_tickets) { |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 127 | SSL *const ssl = hs->ssl; |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 128 | if (// If the client doesn't accept resumption with PSK_DHE_KE, don't send a |
| 129 | // session ticket. |
| 130 | !hs->accept_psk_mode || |
| 131 | // We only implement stateless resumption in TLS 1.3, so skip sending |
| 132 | // tickets if disabled. |
| 133 | (SSL_get_options(ssl) & SSL_OP_NO_TICKET)) { |
| 134 | *out_sent_tickets = false; |
| 135 | return true; |
| 136 | } |
| 137 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 138 | // Rebase the session timestamp so that it is measured from ticket |
| 139 | // issuance. |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 140 | ssl_session_rebase_time(ssl, hs->new_session.get()); |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 141 | |
David Benjamin | 3f180b8 | 2022-05-09 17:45:18 -0400 | [diff] [blame] | 142 | assert(ssl->session_ctx->num_tickets <= kMaxTickets); |
| 143 | for (size_t i = 0; i < ssl->session_ctx->num_tickets; i++) { |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 144 | UniquePtr<SSL_SESSION> session( |
| 145 | SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_INCLUDE_NONAUTH)); |
| 146 | if (!session) { |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 147 | return false; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 148 | } |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 149 | |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 150 | if (!RAND_bytes((uint8_t *)&session->ticket_age_add, 4)) { |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 151 | return false; |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 152 | } |
David Benjamin | a3a71e9 | 2018-06-29 13:24:45 -0400 | [diff] [blame] | 153 | session->ticket_age_add_valid = true; |
Nick Harper | 8519432 | 2020-05-20 16:59:29 -0700 | [diff] [blame] | 154 | bool enable_early_data = |
| 155 | ssl->enable_early_data && |
| 156 | (!ssl->quic_method || !ssl->config->quic_early_data_context.empty()); |
| 157 | if (enable_early_data) { |
David Benjamin | d634357 | 2019-08-15 17:29:02 -0400 | [diff] [blame] | 158 | // QUIC does not use the max_early_data_size parameter and always sets it |
David Benjamin | a1d3bfb | 2021-06-01 12:12:44 -0400 | [diff] [blame] | 159 | // to a fixed value. See RFC 9001, section 4.6.1. |
David Benjamin | d634357 | 2019-08-15 17:29:02 -0400 | [diff] [blame] | 160 | session->ticket_max_early_data = |
| 161 | ssl->quic_method != nullptr ? 0xffffffff : kMaxEarlyDataAccepted; |
Steven Valdez | be165a2 | 2017-10-10 11:45:01 -0400 | [diff] [blame] | 162 | } |
| 163 | |
David Benjamin | 3f180b8 | 2022-05-09 17:45:18 -0400 | [diff] [blame] | 164 | static_assert(kMaxTickets < 256, "Too many tickets"); |
| 165 | assert(i < 256); |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 166 | uint8_t nonce[] = {static_cast<uint8_t>(i)}; |
| 167 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 168 | ScopedCBB cbb; |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 169 | CBB body, nonce_cbb, ticket, extensions; |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 170 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 171 | SSL3_MT_NEW_SESSION_TICKET) || |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 172 | !CBB_add_u32(&body, session->timeout) || |
| 173 | !CBB_add_u32(&body, session->ticket_age_add) || |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 174 | !CBB_add_u8_length_prefixed(&body, &nonce_cbb) || |
| 175 | !CBB_add_bytes(&nonce_cbb, nonce, sizeof(nonce)) || |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 176 | !CBB_add_u16_length_prefixed(&body, &ticket) || |
Alessandro Ghedini | 2cc6f44 | 2018-12-11 11:35:17 +0000 | [diff] [blame] | 177 | !tls13_derive_session_psk(session.get(), nonce) || |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 178 | !ssl_encrypt_ticket(hs, &ticket, session.get()) || |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 179 | !CBB_add_u16_length_prefixed(&body, &extensions)) { |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 180 | return false; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 181 | } |
| 182 | |
Nick Harper | 8519432 | 2020-05-20 16:59:29 -0700 | [diff] [blame] | 183 | if (enable_early_data) { |
David Benjamin | a93beba | 2019-10-15 16:58:21 -0400 | [diff] [blame] | 184 | CBB early_data; |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 185 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_early_data) || |
David Benjamin | a93beba | 2019-10-15 16:58:21 -0400 | [diff] [blame] | 186 | !CBB_add_u16_length_prefixed(&extensions, &early_data) || |
| 187 | !CBB_add_u32(&early_data, session->ticket_max_early_data) || |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 188 | !CBB_flush(&extensions)) { |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 189 | return false; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
David Benjamin | 3675eb3 | 2021-05-18 14:01:07 -0400 | [diff] [blame] | 193 | // Add a fake extension. See RFC 8701. |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 194 | if (!CBB_add_u16(&extensions, |
David Benjamin | a7bc944 | 2018-01-18 10:08:53 -0500 | [diff] [blame] | 195 | ssl_get_grease_value(hs, ssl_grease_ticket_extension)) || |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 196 | !CBB_add_u16(&extensions, 0 /* empty */)) { |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 197 | return false; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 198 | } |
| 199 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 200 | if (!ssl_add_message_cbb(ssl, cbb.get())) { |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 201 | return false; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 205 | *out_sent_tickets = true; |
| 206 | return true; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 207 | } |
| 208 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 209 | static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 210 | // At this point, most ClientHello extensions have already been processed by |
| 211 | // the common handshake logic. Resolve the remaining non-PSK parameters. |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 212 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 213 | SSLMessage msg; |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 214 | SSL_CLIENT_HELLO client_hello; |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 215 | if (!hs->GetClientHello(&msg, &client_hello)) { |
David Benjamin | 34202b9 | 2016-11-16 19:07:53 +0900 | [diff] [blame] | 216 | return ssl_hs_error; |
| 217 | } |
| 218 | |
Nick Harper | cac9392 | 2020-05-07 13:53:23 -0700 | [diff] [blame] | 219 | if (ssl->quic_method != nullptr && client_hello.session_id_len > 0) { |
| 220 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_COMPATIBILITY_MODE); |
| 221 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 222 | return ssl_hs_error; |
| 223 | } |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 224 | OPENSSL_memcpy(hs->session_id, client_hello.session_id, |
| 225 | client_hello.session_id_len); |
| 226 | hs->session_id_len = client_hello.session_id_len; |
| 227 | |
David Benjamin | 43cc9c6 | 2018-12-12 13:06:46 -0600 | [diff] [blame] | 228 | uint16_t group_id; |
| 229 | if (!tls1_get_shared_group(hs, &group_id)) { |
| 230 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP); |
| 231 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 232 | return ssl_hs_error; |
| 233 | } |
| 234 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 235 | // Negotiate the cipher suite. |
David Benjamin | 43cc9c6 | 2018-12-12 13:06:46 -0600 | [diff] [blame] | 236 | hs->new_cipher = choose_tls13_cipher(ssl, &client_hello, group_id); |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 237 | if (hs->new_cipher == NULL) { |
David Benjamin | f01f42a | 2016-11-16 19:05:33 +0900 | [diff] [blame] | 238 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 239 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
David Benjamin | f01f42a | 2016-11-16 19:05:33 +0900 | [diff] [blame] | 240 | return ssl_hs_error; |
| 241 | } |
| 242 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 243 | // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was |
| 244 | // deferred. Complete it now. |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 245 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 246 | if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 247 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 248 | return ssl_hs_error; |
| 249 | } |
| 250 | |
David Benjamin | d55f450 | 2021-08-11 13:19:19 -0400 | [diff] [blame] | 251 | // The PRF hash is now known. |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 252 | if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher)) { |
| 253 | return ssl_hs_error; |
| 254 | } |
| 255 | |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 256 | hs->tls13_state = state13_select_session; |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 257 | return ssl_hs_ok; |
| 258 | } |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 259 | |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 260 | static enum ssl_ticket_aead_result_t select_session( |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 261 | SSL_HANDSHAKE *hs, uint8_t *out_alert, UniquePtr<SSL_SESSION> *out_session, |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 262 | int32_t *out_ticket_age_skew, bool *out_offered_ticket, |
| 263 | const SSLMessage &msg, const SSL_CLIENT_HELLO *client_hello) { |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 264 | SSL *const ssl = hs->ssl; |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 265 | *out_session = nullptr; |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 266 | |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 267 | CBS pre_shared_key; |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 268 | *out_offered_ticket = ssl_client_hello_get_extension( |
| 269 | client_hello, &pre_shared_key, TLSEXT_TYPE_pre_shared_key); |
| 270 | if (!*out_offered_ticket) { |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 271 | return ssl_ticket_aead_ignore_ticket; |
| 272 | } |
| 273 | |
David Benjamin | 8648c53 | 2021-08-19 18:02:37 -0400 | [diff] [blame] | 274 | // Per RFC 8446, section 4.2.9, servers MUST abort the handshake if the client |
David Benjamin | 3af6226 | 2021-03-30 13:37:53 -0400 | [diff] [blame] | 275 | // sends pre_shared_key without psk_key_exchange_modes. |
| 276 | CBS unused; |
| 277 | if (!ssl_client_hello_get_extension(client_hello, &unused, |
| 278 | TLSEXT_TYPE_psk_key_exchange_modes)) { |
| 279 | *out_alert = SSL_AD_MISSING_EXTENSION; |
| 280 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
| 281 | return ssl_ticket_aead_error; |
| 282 | } |
| 283 | |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 284 | CBS ticket, binders; |
| 285 | uint32_t client_ticket_age; |
David Benjamin | 9806ae0 | 2019-08-16 15:32:03 -0400 | [diff] [blame] | 286 | if (!ssl_ext_pre_shared_key_parse_clienthello( |
| 287 | hs, &ticket, &binders, &client_ticket_age, out_alert, client_hello, |
| 288 | &pre_shared_key)) { |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 289 | return ssl_ticket_aead_error; |
| 290 | } |
| 291 | |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 292 | // If the peer did not offer psk_dhe, ignore the resumption. |
| 293 | if (!hs->accept_psk_mode) { |
| 294 | return ssl_ticket_aead_ignore_ticket; |
| 295 | } |
| 296 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 297 | // TLS 1.3 session tickets are renewed separately as part of the |
| 298 | // NewSessionTicket. |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 299 | bool unused_renew; |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 300 | UniquePtr<SSL_SESSION> session; |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 301 | enum ssl_ticket_aead_result_t ret = |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 302 | ssl_process_ticket(hs, &session, &unused_renew, ticket, {}); |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 303 | switch (ret) { |
| 304 | case ssl_ticket_aead_success: |
| 305 | break; |
| 306 | case ssl_ticket_aead_error: |
| 307 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 308 | return ret; |
| 309 | default: |
| 310 | return ret; |
| 311 | } |
| 312 | |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 313 | if (!ssl_session_is_resumable(hs, session.get()) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 314 | // Historically, some TLS 1.3 tickets were missing ticket_age_add. |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 315 | !session->ticket_age_add_valid) { |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 316 | return ssl_ticket_aead_ignore_ticket; |
| 317 | } |
| 318 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 319 | // Recover the client ticket age and convert to seconds. |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 320 | client_ticket_age -= session->ticket_age_add; |
| 321 | client_ticket_age /= 1000; |
| 322 | |
| 323 | struct OPENSSL_timeval now; |
| 324 | ssl_get_current_time(ssl, &now); |
| 325 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 326 | // Compute the server ticket age in seconds. |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 327 | assert(now.tv_sec >= session->time); |
| 328 | uint64_t server_ticket_age = now.tv_sec - session->time; |
| 329 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 330 | // To avoid overflowing |hs->ticket_age_skew|, we will not resume |
| 331 | // 68-year-old sessions. |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 332 | if (server_ticket_age > INT32_MAX) { |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 333 | return ssl_ticket_aead_ignore_ticket; |
| 334 | } |
| 335 | |
David Benjamin | 8c98bac | 2019-08-15 20:40:01 -0400 | [diff] [blame] | 336 | *out_ticket_age_skew = static_cast<int32_t>(client_ticket_age) - |
| 337 | static_cast<int32_t>(server_ticket_age); |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 338 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 339 | // Check the PSK binder. |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 340 | if (!tls13_verify_psk_binder(hs, session.get(), msg, &binders)) { |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 341 | *out_alert = SSL_AD_DECRYPT_ERROR; |
| 342 | return ssl_ticket_aead_error; |
| 343 | } |
| 344 | |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 345 | *out_session = std::move(session); |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 346 | return ssl_ticket_aead_success; |
| 347 | } |
| 348 | |
Nick Harper | 7c52299 | 2020-04-30 14:15:49 -0700 | [diff] [blame] | 349 | static bool quic_ticket_compatible(const SSL_SESSION *session, |
| 350 | const SSL_CONFIG *config) { |
| 351 | if (!session->is_quic) { |
| 352 | return true; |
| 353 | } |
Nick Harper | 8519432 | 2020-05-20 16:59:29 -0700 | [diff] [blame] | 354 | |
| 355 | if (session->quic_early_data_context.empty() || |
| 356 | config->quic_early_data_context.size() != |
| 357 | session->quic_early_data_context.size() || |
| 358 | CRYPTO_memcmp(config->quic_early_data_context.data(), |
| 359 | session->quic_early_data_context.data(), |
| 360 | session->quic_early_data_context.size()) != 0) { |
Nick Harper | 7c52299 | 2020-04-30 14:15:49 -0700 | [diff] [blame] | 361 | return false; |
| 362 | } |
| 363 | return true; |
| 364 | } |
| 365 | |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 366 | static enum ssl_hs_wait_t do_select_session(SSL_HANDSHAKE *hs) { |
| 367 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 368 | SSLMessage msg; |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 369 | SSL_CLIENT_HELLO client_hello; |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 370 | if (!hs->GetClientHello(&msg, &client_hello)) { |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 371 | return ssl_hs_error; |
| 372 | } |
| 373 | |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 374 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 375 | UniquePtr<SSL_SESSION> session; |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 376 | bool offered_ticket = false; |
| 377 | switch (select_session(hs, &alert, &session, &ssl->s3->ticket_age_skew, |
| 378 | &offered_ticket, msg, &client_hello)) { |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 379 | case ssl_ticket_aead_ignore_ticket: |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 380 | assert(!session); |
David Benjamin | 962b375 | 2021-05-10 15:17:18 -0400 | [diff] [blame] | 381 | if (!ssl_get_new_session(hs)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 382 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | f01f42a | 2016-11-16 19:05:33 +0900 | [diff] [blame] | 383 | return ssl_hs_error; |
| 384 | } |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 385 | break; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 386 | |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 387 | case ssl_ticket_aead_success: |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 388 | // Carry over authentication information from the previous handshake into |
| 389 | // a fresh session. |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 390 | hs->new_session = |
| 391 | SSL_SESSION_dup(session.get(), SSL_SESSION_DUP_AUTH_ONLY); |
David Benjamin | 6433a91 | 2019-05-04 14:17:08 -0500 | [diff] [blame] | 392 | if (hs->new_session == nullptr) { |
| 393 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 394 | return ssl_hs_error; |
| 395 | } |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 396 | |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 397 | ssl->s3->session_reused = true; |
David Benjamin | 9b2cdb7 | 2021-04-01 23:21:53 -0400 | [diff] [blame] | 398 | hs->can_release_private_key = true; |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 399 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 400 | // Resumption incorporates fresh key material, so refresh the timeout. |
David Benjamin | 98472cb | 2018-05-02 16:05:36 -0400 | [diff] [blame] | 401 | ssl_session_renew_timeout(ssl, hs->new_session.get(), |
| 402 | ssl->session_ctx->session_psk_dhe_timeout); |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 403 | break; |
| 404 | |
| 405 | case ssl_ticket_aead_error: |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 406 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 407 | return ssl_hs_error; |
| 408 | |
| 409 | case ssl_ticket_aead_retry: |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 410 | hs->tls13_state = state13_select_session; |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 411 | return ssl_hs_pending_ticket; |
| 412 | } |
| 413 | |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 414 | // Negotiate ALPS now, after ALPN is negotiated and |hs->new_session| is |
| 415 | // initialized. |
| 416 | if (!ssl_negotiate_alps(hs, &alert, &client_hello)) { |
| 417 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 418 | return ssl_hs_error; |
| 419 | } |
| 420 | |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 421 | // Record connection properties in the new session. |
| 422 | hs->new_session->cipher = hs->new_cipher; |
| 423 | if (!tls1_get_shared_group(hs, &hs->new_session->group_id)) { |
| 424 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP); |
| 425 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 426 | return ssl_hs_error; |
| 427 | } |
| 428 | |
| 429 | // Determine if we need HelloRetryRequest. |
| 430 | bool found_key_share; |
| 431 | if (!ssl_ext_key_share_parse_clienthello(hs, &found_key_share, |
| 432 | /*out_key_share=*/nullptr, &alert, |
| 433 | &client_hello)) { |
| 434 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 435 | return ssl_hs_error; |
| 436 | } |
| 437 | |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 438 | // Determine if we're negotiating 0-RTT. |
| 439 | if (!ssl->enable_early_data) { |
| 440 | ssl->s3->early_data_reason = ssl_early_data_disabled; |
| 441 | } else if (!offered_ticket) { |
| 442 | ssl->s3->early_data_reason = ssl_early_data_no_session_offered; |
| 443 | } else if (!session) { |
| 444 | ssl->s3->early_data_reason = ssl_early_data_session_not_resumed; |
| 445 | } else if (session->ticket_max_early_data == 0) { |
| 446 | ssl->s3->early_data_reason = ssl_early_data_unsupported_for_session; |
| 447 | } else if (!hs->early_data_offered) { |
| 448 | ssl->s3->early_data_reason = ssl_early_data_peer_declined; |
David Benjamin | 8acec00 | 2021-05-19 13:03:34 -0400 | [diff] [blame] | 449 | } else if (hs->channel_id_negotiated) { |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 450 | // Channel ID is incompatible with 0-RTT. |
| 451 | ssl->s3->early_data_reason = ssl_early_data_channel_id; |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 452 | } else if (MakeConstSpan(ssl->s3->alpn_selected) != session->early_alpn) { |
| 453 | // The negotiated ALPN must match the one in the ticket. |
| 454 | ssl->s3->early_data_reason = ssl_early_data_alpn_mismatch; |
| 455 | } else if (hs->new_session->has_application_settings != |
| 456 | session->has_application_settings || |
| 457 | MakeConstSpan(hs->new_session->local_application_settings) != |
| 458 | session->local_application_settings) { |
| 459 | ssl->s3->early_data_reason = ssl_early_data_alps_mismatch; |
| 460 | } else if (ssl->s3->ticket_age_skew < -kMaxTicketAgeSkewSeconds || |
| 461 | kMaxTicketAgeSkewSeconds < ssl->s3->ticket_age_skew) { |
| 462 | ssl->s3->early_data_reason = ssl_early_data_ticket_age_skew; |
| 463 | } else if (!quic_ticket_compatible(session.get(), hs->config)) { |
| 464 | ssl->s3->early_data_reason = ssl_early_data_quic_parameter_mismatch; |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 465 | } else if (!found_key_share) { |
| 466 | ssl->s3->early_data_reason = ssl_early_data_hello_retry_request; |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 467 | } else { |
| 468 | // |ssl_session_is_resumable| forbids cross-cipher resumptions even if the |
| 469 | // PRF hashes match. |
| 470 | assert(hs->new_cipher == session->cipher); |
| 471 | |
| 472 | ssl->s3->early_data_reason = ssl_early_data_accepted; |
| 473 | ssl->s3->early_data_accepted = true; |
| 474 | } |
| 475 | |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 476 | // Store the ALPN and ALPS values in the session for 0-RTT. Note the peer |
| 477 | // applications settings are not generally known until client |
| 478 | // EncryptedExtensions. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 479 | if (!hs->new_session->early_alpn.CopyFrom(ssl->s3->alpn_selected)) { |
| 480 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 481 | return ssl_hs_error; |
Steven Valdez | 27a9e6a | 2017-02-14 13:20:40 -0500 | [diff] [blame] | 482 | } |
| 483 | |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 484 | // The peer applications settings are usually received later, in |
| 485 | // EncryptedExtensions. But, in 0-RTT handshakes, we carry over the |
| 486 | // values from |session|. Do this now, before |session| is discarded. |
| 487 | if (ssl->s3->early_data_accepted && |
| 488 | hs->new_session->has_application_settings && |
| 489 | !hs->new_session->peer_application_settings.CopyFrom( |
| 490 | session->peer_application_settings)) { |
| 491 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 492 | return ssl_hs_error; |
| 493 | } |
| 494 | |
Nick Harper | 5e08695 | 2020-09-30 13:59:14 -0700 | [diff] [blame] | 495 | // Copy the QUIC early data context to the session. |
| 496 | if (ssl->enable_early_data && ssl->quic_method) { |
| 497 | if (!hs->new_session->quic_early_data_context.CopyFrom( |
| 498 | hs->config->quic_early_data_context)) { |
| 499 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 500 | return ssl_hs_error; |
| 501 | } |
| 502 | } |
| 503 | |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 504 | if (ssl->ctx->dos_protection_cb != NULL && |
| 505 | ssl->ctx->dos_protection_cb(&client_hello) == 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 506 | // Connection rejected for DOS reasons. |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 507 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 508 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 509 | return ssl_hs_error; |
| 510 | } |
| 511 | |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 512 | size_t hash_len = EVP_MD_size( |
| 513 | ssl_get_handshake_digest(ssl_protocol_version(ssl), hs->new_cipher)); |
| 514 | |
| 515 | // Set up the key schedule and incorporate the PSK into the running secret. |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 516 | if (!tls13_init_key_schedule( |
| 517 | hs, ssl->s3->session_reused |
| 518 | ? MakeConstSpan(hs->new_session->secret, |
| 519 | hs->new_session->secret_length) |
| 520 | : MakeConstSpan(kZeroes, hash_len)) || |
| 521 | !ssl_hash_message(hs, msg)) { |
David Benjamin | 9806ae0 | 2019-08-16 15:32:03 -0400 | [diff] [blame] | 522 | return ssl_hs_error; |
| 523 | } |
| 524 | |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 525 | if (ssl->s3->early_data_accepted) { |
David Benjamin | d634357 | 2019-08-15 17:29:02 -0400 | [diff] [blame] | 526 | if (!tls13_derive_early_secret(hs)) { |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 527 | return ssl_hs_error; |
| 528 | } |
| 529 | } else if (hs->early_data_offered) { |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 530 | ssl->s3->skip_early_data = true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 531 | } |
| 532 | |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 533 | if (!found_key_share) { |
| 534 | ssl->method->next_message(ssl); |
| 535 | if (!hs->transcript.UpdateForHelloRetryRequest()) { |
| 536 | return ssl_hs_error; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 537 | } |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 538 | hs->tls13_state = state13_send_hello_retry_request; |
| 539 | return ssl_hs_ok; |
| 540 | } |
| 541 | |
| 542 | if (!resolve_ecdhe_secret(hs, &client_hello)) { |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 543 | return ssl_hs_error; |
| 544 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 545 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 546 | ssl->method->next_message(ssl); |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 547 | hs->ech_client_hello_buf.Reset(); |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 548 | hs->tls13_state = state13_send_server_hello; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 549 | return ssl_hs_ok; |
| 550 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 551 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 552 | static enum ssl_hs_wait_t do_send_hello_retry_request(SSL_HANDSHAKE *hs) { |
| 553 | SSL *const ssl = hs->ssl; |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 554 | if (hs->hints_requested) { |
| 555 | return ssl_hs_hints_ready; |
| 556 | } |
Steven Valdez | 964b237 | 2017-11-07 17:09:52 -0500 | [diff] [blame] | 557 | |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 558 | ScopedCBB cbb; |
| 559 | CBB body, session_id, extensions; |
| 560 | uint16_t group_id; |
| 561 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) || |
| 562 | !CBB_add_u16(&body, TLS1_2_VERSION) || |
| 563 | !CBB_add_bytes(&body, kHelloRetryRequest, SSL3_RANDOM_SIZE) || |
| 564 | !CBB_add_u8_length_prefixed(&body, &session_id) || |
| 565 | !CBB_add_bytes(&session_id, hs->session_id, hs->session_id_len) || |
David Benjamin | 3743aaf | 2020-09-21 13:55:16 -0400 | [diff] [blame] | 566 | !CBB_add_u16(&body, SSL_CIPHER_get_protocol_id(hs->new_cipher)) || |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 567 | !CBB_add_u8(&body, 0 /* no compression */) || |
| 568 | !tls1_get_shared_group(hs, &group_id) || |
| 569 | !CBB_add_u16_length_prefixed(&body, &extensions) || |
| 570 | !CBB_add_u16(&extensions, TLSEXT_TYPE_supported_versions) || |
| 571 | !CBB_add_u16(&extensions, 2 /* length */) || |
| 572 | !CBB_add_u16(&extensions, ssl->version) || |
| 573 | !CBB_add_u16(&extensions, TLSEXT_TYPE_key_share) || |
| 574 | !CBB_add_u16(&extensions, 2 /* length */) || |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 575 | !CBB_add_u16(&extensions, group_id)) { |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 576 | return ssl_hs_error; |
| 577 | } |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 578 | if (hs->ech_is_inner) { |
| 579 | // Fill a placeholder for the ECH confirmation value. |
| 580 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_encrypted_client_hello) || |
| 581 | !CBB_add_u16(&extensions, ECH_CONFIRMATION_SIGNAL_LEN) || |
| 582 | !CBB_add_zeros(&extensions, ECH_CONFIRMATION_SIGNAL_LEN)) { |
| 583 | return ssl_hs_error; |
| 584 | } |
| 585 | } |
| 586 | Array<uint8_t> hrr; |
| 587 | if (!ssl->method->finish_message(ssl, cbb.get(), &hrr)) { |
| 588 | return ssl_hs_error; |
| 589 | } |
| 590 | if (hs->ech_is_inner) { |
| 591 | // Now that the message is encoded, fill in the whole value. |
| 592 | size_t offset = hrr.size() - ECH_CONFIRMATION_SIGNAL_LEN; |
| 593 | if (!ssl_ech_accept_confirmation( |
| 594 | hs, MakeSpan(hrr).last(ECH_CONFIRMATION_SIGNAL_LEN), |
| 595 | ssl->s3->client_random, hs->transcript, /*is_hrr=*/true, hrr, |
| 596 | offset)) { |
| 597 | return ssl_hs_error; |
| 598 | } |
| 599 | } |
Steven Valdez | 964b237 | 2017-11-07 17:09:52 -0500 | [diff] [blame] | 600 | |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 601 | if (!ssl->method->add_message(ssl, std::move(hrr)) || |
| 602 | !ssl->method->add_change_cipher_spec(ssl)) { |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 603 | return ssl_hs_error; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 604 | } |
| 605 | |
Kris Kwiatkowski | b11902a | 2019-08-24 11:01:04 +0100 | [diff] [blame] | 606 | ssl->s3->used_hello_retry_request = true; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 607 | hs->tls13_state = state13_read_second_client_hello; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 608 | return ssl_hs_flush; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 609 | } |
| 610 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 611 | static enum ssl_hs_wait_t do_read_second_client_hello(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 612 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 613 | SSLMessage msg; |
| 614 | if (!ssl->method->get_message(ssl, &msg)) { |
| 615 | return ssl_hs_read_message; |
| 616 | } |
| 617 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) { |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 618 | return ssl_hs_error; |
| 619 | } |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 620 | SSL_CLIENT_HELLO client_hello; |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 621 | if (!ssl_client_hello_init(ssl, &client_hello, msg.body)) { |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 622 | OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 623 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 624 | return ssl_hs_error; |
| 625 | } |
| 626 | |
David Benjamin | ba423c9 | 2021-06-15 16:26:58 -0400 | [diff] [blame] | 627 | if (ssl->s3->ech_status == ssl_ech_accepted) { |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 628 | // If we previously accepted the ClientHelloInner, the second ClientHello |
| 629 | // must contain an outer encrypted_client_hello extension. |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 630 | CBS ech_body; |
| 631 | if (!ssl_client_hello_get_extension(&client_hello, &ech_body, |
| 632 | TLSEXT_TYPE_encrypted_client_hello)) { |
| 633 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
| 634 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION); |
| 635 | return ssl_hs_error; |
| 636 | } |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 637 | uint16_t kdf_id, aead_id; |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 638 | uint8_t type, config_id; |
Steven Valdez | 94a63a5 | 2021-04-29 10:52:42 -0400 | [diff] [blame] | 639 | CBS enc, payload; |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 640 | if (!CBS_get_u8(&ech_body, &type) || // |
| 641 | type != ECH_CLIENT_OUTER || // |
| 642 | !CBS_get_u16(&ech_body, &kdf_id) || // |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 643 | !CBS_get_u16(&ech_body, &aead_id) || |
Steven Valdez | 94a63a5 | 2021-04-29 10:52:42 -0400 | [diff] [blame] | 644 | !CBS_get_u8(&ech_body, &config_id) || |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 645 | !CBS_get_u16_length_prefixed(&ech_body, &enc) || |
| 646 | !CBS_get_u16_length_prefixed(&ech_body, &payload) || |
| 647 | CBS_len(&ech_body) != 0) { |
| 648 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 649 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 650 | return ssl_hs_error; |
| 651 | } |
| 652 | |
David Benjamin | f39c81d | 2021-05-03 18:39:46 -0400 | [diff] [blame] | 653 | if (kdf_id != EVP_HPKE_KDF_id(EVP_HPKE_CTX_kdf(hs->ech_hpke_ctx.get())) || |
| 654 | aead_id != |
| 655 | EVP_HPKE_AEAD_id(EVP_HPKE_CTX_aead(hs->ech_hpke_ctx.get())) || |
| 656 | config_id != hs->ech_config_id || CBS_len(&enc) > 0) { |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 657 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 658 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 659 | return ssl_hs_error; |
| 660 | } |
| 661 | |
| 662 | // Decrypt the payload with the HPKE context from the first ClientHello. |
David Benjamin | 44425dd | 2022-01-27 12:22:42 -0500 | [diff] [blame] | 663 | uint8_t alert = SSL_AD_DECODE_ERROR; |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 664 | bool unused; |
David Benjamin | 44425dd | 2022-01-27 12:22:42 -0500 | [diff] [blame] | 665 | if (!ssl_client_hello_decrypt(hs, &alert, &unused, |
| 666 | &hs->ech_client_hello_buf, &client_hello, |
| 667 | payload)) { |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 668 | // Decryption failure is fatal in the second ClientHello. |
| 669 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 670 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 671 | return ssl_hs_error; |
| 672 | } |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 673 | |
| 674 | // Reparse |client_hello| from the buffer owned by |hs|. |
| 675 | if (!hs->GetClientHello(&msg, &client_hello)) { |
| 676 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 677 | return ssl_hs_error; |
| 678 | } |
| 679 | } |
| 680 | |
David Benjamin | 9806ae0 | 2019-08-16 15:32:03 -0400 | [diff] [blame] | 681 | // We perform all our negotiation based on the first ClientHello (for |
| 682 | // consistency with what |select_certificate_cb| observed), which is in the |
| 683 | // transcript, so we can ignore most of this second one. |
| 684 | // |
| 685 | // We do, however, check the second PSK binder. This covers the client key |
| 686 | // share, in case we ever send half-RTT data (we currently do not). It is also |
| 687 | // a tricky computation, so we enforce the peer handled it correctly. |
| 688 | if (ssl->s3->session_reused) { |
| 689 | CBS pre_shared_key; |
| 690 | if (!ssl_client_hello_get_extension(&client_hello, &pre_shared_key, |
| 691 | TLSEXT_TYPE_pre_shared_key)) { |
| 692 | OPENSSL_PUT_ERROR(SSL, SSL_R_INCONSISTENT_CLIENT_HELLO); |
| 693 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 694 | return ssl_hs_error; |
| 695 | } |
| 696 | |
| 697 | CBS ticket, binders; |
| 698 | uint32_t client_ticket_age; |
| 699 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 700 | if (!ssl_ext_pre_shared_key_parse_clienthello( |
| 701 | hs, &ticket, &binders, &client_ticket_age, &alert, &client_hello, |
| 702 | &pre_shared_key)) { |
| 703 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 704 | return ssl_hs_error; |
| 705 | } |
| 706 | |
| 707 | // Note it is important that we do not obtain a new |SSL_SESSION| from |
| 708 | // |ticket|. We have already selected parameters based on the first |
| 709 | // ClientHello (in the transcript) and must not switch partway through. |
| 710 | if (!tls13_verify_psk_binder(hs, hs->new_session.get(), msg, &binders)) { |
| 711 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
| 712 | return ssl_hs_error; |
| 713 | } |
| 714 | } |
| 715 | |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 716 | if (!resolve_ecdhe_secret(hs, &client_hello)) { |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 717 | return ssl_hs_error; |
| 718 | } |
| 719 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 720 | if (!ssl_hash_message(hs, msg)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 721 | return ssl_hs_error; |
| 722 | } |
| 723 | |
David Benjamin | f9cc26f | 2020-02-09 16:49:31 -0500 | [diff] [blame] | 724 | // ClientHello should be the end of the flight. |
| 725 | if (ssl->method->has_unprocessed_handshake_data(ssl)) { |
| 726 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 727 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA); |
| 728 | return ssl_hs_error; |
| 729 | } |
| 730 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 731 | ssl->method->next_message(ssl); |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 732 | hs->ech_client_hello_buf.Reset(); |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 733 | hs->tls13_state = state13_send_server_hello; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 734 | return ssl_hs_ok; |
| 735 | } |
| 736 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 737 | static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) { |
| 738 | SSL *const ssl = hs->ssl; |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 739 | |
Dan McArdle | c295935 | 2020-10-29 14:31:31 -0400 | [diff] [blame] | 740 | Span<uint8_t> random(ssl->s3->server_random); |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 741 | |
| 742 | SSL_HANDSHAKE_HINTS *const hints = hs->hints.get(); |
| 743 | if (hints && !hs->hints_requested && |
David Benjamin | 4a6c8fd | 2022-07-21 14:05:41 -0700 | [diff] [blame] | 744 | hints->server_random_tls13.size() == random.size()) { |
| 745 | OPENSSL_memcpy(random.data(), hints->server_random_tls13.data(), |
| 746 | random.size()); |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 747 | } else { |
| 748 | RAND_bytes(random.data(), random.size()); |
| 749 | if (hints && hs->hints_requested && |
David Benjamin | 4a6c8fd | 2022-07-21 14:05:41 -0700 | [diff] [blame] | 750 | !hints->server_random_tls13.CopyFrom(random)) { |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 751 | return ssl_hs_error; |
| 752 | } |
| 753 | } |
Dan McArdle | c295935 | 2020-10-29 14:31:31 -0400 | [diff] [blame] | 754 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 755 | Array<uint8_t> server_hello; |
| 756 | ScopedCBB cbb; |
| 757 | CBB body, extensions, session_id; |
| 758 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) || |
| 759 | !CBB_add_u16(&body, TLS1_2_VERSION) || |
| 760 | !CBB_add_bytes(&body, ssl->s3->server_random, |
| 761 | sizeof(ssl->s3->server_random)) || |
| 762 | !CBB_add_u8_length_prefixed(&body, &session_id) || |
| 763 | !CBB_add_bytes(&session_id, hs->session_id, hs->session_id_len) || |
| 764 | !CBB_add_u16(&body, SSL_CIPHER_get_protocol_id(hs->new_cipher)) || |
| 765 | !CBB_add_u8(&body, 0) || |
| 766 | !CBB_add_u16_length_prefixed(&body, &extensions) || |
| 767 | !ssl_ext_pre_shared_key_add_serverhello(hs, &extensions) || |
| 768 | !ssl_ext_key_share_add_serverhello(hs, &extensions) || |
| 769 | !ssl_ext_supported_versions_add_serverhello(hs, &extensions) || |
| 770 | !ssl->method->finish_message(ssl, cbb.get(), &server_hello)) { |
| 771 | return ssl_hs_error; |
Dan McArdle | c295935 | 2020-10-29 14:31:31 -0400 | [diff] [blame] | 772 | } |
| 773 | |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 774 | assert(ssl->s3->ech_status != ssl_ech_accepted || hs->ech_is_inner); |
| 775 | if (hs->ech_is_inner) { |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 776 | // Fill in the ECH confirmation signal. |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 777 | const size_t offset = ssl_ech_confirmation_signal_hello_offset(ssl); |
David Benjamin | 006f20a | 2021-06-22 23:00:49 -0400 | [diff] [blame] | 778 | Span<uint8_t> random_suffix = random.last(ECH_CONFIRMATION_SIGNAL_LEN); |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 779 | if (!ssl_ech_accept_confirmation(hs, random_suffix, ssl->s3->client_random, |
| 780 | hs->transcript, |
| 781 | /*is_hrr=*/false, server_hello, offset)) { |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 782 | return ssl_hs_error; |
| 783 | } |
| 784 | |
| 785 | // Update |server_hello|. |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 786 | Span<uint8_t> server_hello_out = |
| 787 | MakeSpan(server_hello).subspan(offset, ECH_CONFIRMATION_SIGNAL_LEN); |
| 788 | OPENSSL_memcpy(server_hello_out.data(), random_suffix.data(), |
| 789 | ECH_CONFIRMATION_SIGNAL_LEN); |
| 790 | } |
| 791 | |
| 792 | if (!ssl->method->add_message(ssl, std::move(server_hello))) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 793 | return ssl_hs_error; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 794 | } |
| 795 | |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 796 | hs->key_share_ciphertext.Reset(); // No longer needed. |
Kris Kwiatkowski | b11902a | 2019-08-24 11:01:04 +0100 | [diff] [blame] | 797 | if (!ssl->s3->used_hello_retry_request && |
David Benjamin | 666d16e | 2017-10-06 18:45:16 -0400 | [diff] [blame] | 798 | !ssl->method->add_change_cipher_spec(ssl)) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 799 | return ssl_hs_error; |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 800 | } |
| 801 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 802 | // Derive and enable the handshake traffic secrets. |
Steven Valdez | 4cb8494 | 2016-12-16 11:29:28 -0500 | [diff] [blame] | 803 | if (!tls13_derive_handshake_secrets(hs) || |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 804 | !tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_seal, |
David Benjamin | 754d4c9 | 2020-02-11 16:27:21 -0500 | [diff] [blame] | 805 | hs->new_session.get(), |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 806 | hs->server_handshake_secret())) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 807 | return ssl_hs_error; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 808 | } |
| 809 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 810 | // Send EncryptedExtensions. |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 811 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 812 | SSL3_MT_ENCRYPTED_EXTENSIONS) || |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 813 | !ssl_add_serverhello_tlsext(hs, &body) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 814 | !ssl_add_message_cbb(ssl, cbb.get())) { |
| 815 | return ssl_hs_error; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 816 | } |
| 817 | |
David Benjamin | c3648fa | 2017-07-01 10:50:56 -0400 | [diff] [blame] | 818 | if (!ssl->s3->session_reused) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 819 | // Determine whether to request a client certificate. |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 820 | hs->cert_request = !!(hs->config->verify_mode & SSL_VERIFY_PEER); |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 821 | // Only request a certificate if Channel ID isn't negotiated. |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 822 | if ((hs->config->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) && |
David Benjamin | 8acec00 | 2021-05-19 13:03:34 -0400 | [diff] [blame] | 823 | hs->channel_id_negotiated) { |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 824 | hs->cert_request = false; |
David Benjamin | c3648fa | 2017-07-01 10:50:56 -0400 | [diff] [blame] | 825 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 826 | } |
| 827 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 828 | // Send a CertificateRequest, if necessary. |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 829 | if (hs->cert_request) { |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 830 | CBB cert_request_extensions, sigalg_contents, sigalgs_cbb; |
| 831 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
| 832 | SSL3_MT_CERTIFICATE_REQUEST) || |
| 833 | !CBB_add_u8(&body, 0 /* no certificate_request_context. */) || |
| 834 | !CBB_add_u16_length_prefixed(&body, &cert_request_extensions) || |
| 835 | !CBB_add_u16(&cert_request_extensions, |
| 836 | TLSEXT_TYPE_signature_algorithms) || |
| 837 | !CBB_add_u16_length_prefixed(&cert_request_extensions, |
| 838 | &sigalg_contents) || |
| 839 | !CBB_add_u16_length_prefixed(&sigalg_contents, &sigalgs_cbb) || |
David Benjamin | ebad508 | 2020-02-03 19:32:19 -0500 | [diff] [blame] | 840 | !tls12_add_verify_sigalgs(hs, &sigalgs_cbb)) { |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 841 | return ssl_hs_error; |
| 842 | } |
| 843 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 844 | if (ssl_has_client_CAs(hs->config)) { |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 845 | CBB ca_contents; |
| 846 | if (!CBB_add_u16(&cert_request_extensions, |
| 847 | TLSEXT_TYPE_certificate_authorities) || |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 848 | !CBB_add_u16_length_prefixed(&cert_request_extensions, |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 849 | &ca_contents) || |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 850 | !ssl_add_client_CA_list(hs, &ca_contents) || |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 851 | !CBB_flush(&cert_request_extensions)) { |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 852 | return ssl_hs_error; |
| 853 | } |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 854 | } |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 855 | |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 856 | if (!ssl_add_message_cbb(ssl, cbb.get())) { |
| 857 | return ssl_hs_error; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 861 | // Send the server Certificate message, if necessary. |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 862 | if (!ssl->s3->session_reused) { |
Christopher Patton | 9cde848 | 2018-07-17 11:36:36 -0700 | [diff] [blame] | 863 | if (!ssl_has_certificate(hs)) { |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 864 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 865 | return ssl_hs_error; |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 866 | } |
| 867 | |
David Benjamin | 0f24bed | 2017-01-12 19:46:50 -0500 | [diff] [blame] | 868 | if (!tls13_add_certificate(hs)) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 869 | return ssl_hs_error; |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 870 | } |
| 871 | |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 872 | hs->tls13_state = state13_send_server_certificate_verify; |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 873 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 874 | } |
| 875 | |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 876 | hs->tls13_state = state13_send_server_finished; |
David Benjamin | 25ac251 | 2017-01-12 19:31:28 -0500 | [diff] [blame] | 877 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 878 | } |
| 879 | |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 880 | static enum ssl_hs_wait_t do_send_server_certificate_verify(SSL_HANDSHAKE *hs) { |
| 881 | switch (tls13_add_certificate_verify(hs)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 882 | case ssl_private_key_success: |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 883 | hs->tls13_state = state13_send_server_finished; |
David Benjamin | 25ac251 | 2017-01-12 19:31:28 -0500 | [diff] [blame] | 884 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 885 | |
| 886 | case ssl_private_key_retry: |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 887 | hs->tls13_state = state13_send_server_certificate_verify; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 888 | return ssl_hs_private_key_operation; |
| 889 | |
| 890 | case ssl_private_key_failure: |
| 891 | return ssl_hs_error; |
| 892 | } |
| 893 | |
| 894 | assert(0); |
| 895 | return ssl_hs_error; |
| 896 | } |
| 897 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 898 | static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 899 | SSL *const ssl = hs->ssl; |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 900 | if (hs->hints_requested) { |
| 901 | return ssl_hs_hints_ready; |
| 902 | } |
| 903 | |
David Benjamin | 9b2cdb7 | 2021-04-01 23:21:53 -0400 | [diff] [blame] | 904 | hs->can_release_private_key = true; |
David Benjamin | 0f24bed | 2017-01-12 19:46:50 -0500 | [diff] [blame] | 905 | if (!tls13_add_finished(hs) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 906 | // Update the secret to the master secret and derive traffic keys. |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 907 | !tls13_advance_key_schedule( |
| 908 | hs, MakeConstSpan(kZeroes, hs->transcript.DigestLen())) || |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 909 | !tls13_derive_application_secrets(hs) || |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 910 | !tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_seal, |
David Benjamin | 754d4c9 | 2020-02-11 16:27:21 -0500 | [diff] [blame] | 911 | hs->new_session.get(), |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 912 | hs->server_traffic_secret_0())) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 913 | return ssl_hs_error; |
| 914 | } |
| 915 | |
Matthew Braithwaite | 093a823 | 2020-01-28 14:06:55 -0800 | [diff] [blame] | 916 | hs->tls13_state = state13_send_half_rtt_ticket; |
David Benjamin | 0c30649 | 2020-02-09 16:28:52 -0500 | [diff] [blame] | 917 | return hs->handback ? ssl_hs_handback : ssl_hs_ok; |
Matthew Braithwaite | 093a823 | 2020-01-28 14:06:55 -0800 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | static enum ssl_hs_wait_t do_send_half_rtt_ticket(SSL_HANDSHAKE *hs) { |
| 921 | SSL *const ssl = hs->ssl; |
| 922 | |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 923 | if (ssl->s3->early_data_accepted) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 924 | // If accepting 0-RTT, we send tickets half-RTT. This gets the tickets on |
| 925 | // the wire sooner and also avoids triggering a write on |SSL_read| when |
| 926 | // processing the client Finished. This requires computing the client |
David Benjamin | a130ce0 | 2018-08-14 22:26:39 -0500 | [diff] [blame] | 927 | // Finished early. See RFC 8446, section 4.6.1. |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 928 | static const uint8_t kEndOfEarlyData[4] = {SSL3_MT_END_OF_EARLY_DATA, 0, |
| 929 | 0, 0}; |
David Benjamin | d634357 | 2019-08-15 17:29:02 -0400 | [diff] [blame] | 930 | if (ssl->quic_method == nullptr && |
| 931 | !hs->transcript.Update(kEndOfEarlyData)) { |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 932 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 933 | return ssl_hs_error; |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 934 | } |
| 935 | |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 936 | size_t finished_len; |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 937 | if (!tls13_finished_mac(hs, hs->expected_client_finished().data(), |
| 938 | &finished_len, false /* client */)) { |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 939 | return ssl_hs_error; |
| 940 | } |
| 941 | |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 942 | if (finished_len != hs->expected_client_finished().size()) { |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 943 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 944 | return ssl_hs_error; |
| 945 | } |
| 946 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 947 | // Feed the predicted Finished into the transcript. This allows us to derive |
| 948 | // the resumption secret early and send half-RTT tickets. |
| 949 | // |
| 950 | // TODO(davidben): This will need to be updated for DTLS 1.3. |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 951 | assert(!SSL_is_dtls(hs->ssl)); |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 952 | assert(hs->expected_client_finished().size() <= 0xff); |
| 953 | uint8_t header[4] = { |
| 954 | SSL3_MT_FINISHED, 0, 0, |
| 955 | static_cast<uint8_t>(hs->expected_client_finished().size())}; |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 956 | bool unused_sent_tickets; |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 957 | if (!hs->transcript.Update(header) || |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 958 | !hs->transcript.Update(hs->expected_client_finished()) || |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 959 | !tls13_derive_resumption_secret(hs) || |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 960 | !add_new_session_tickets(hs, &unused_sent_tickets)) { |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 961 | return ssl_hs_error; |
| 962 | } |
| 963 | } |
| 964 | |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 965 | hs->tls13_state = state13_read_second_client_flight; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 966 | return ssl_hs_flush; |
| 967 | } |
| 968 | |
| 969 | static enum ssl_hs_wait_t do_read_second_client_flight(SSL_HANDSHAKE *hs) { |
| 970 | SSL *const ssl = hs->ssl; |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 971 | if (ssl->s3->early_data_accepted) { |
David Benjamin | 1e85905 | 2020-02-09 16:04:58 -0500 | [diff] [blame] | 972 | if (!tls13_set_traffic_key(ssl, ssl_encryption_early_data, evp_aead_open, |
David Benjamin | 754d4c9 | 2020-02-11 16:27:21 -0500 | [diff] [blame] | 973 | hs->new_session.get(), |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 974 | hs->early_traffic_secret())) { |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 975 | return ssl_hs_error; |
| 976 | } |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 977 | hs->can_early_write = true; |
| 978 | hs->can_early_read = true; |
| 979 | hs->in_early_data = true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 980 | } |
David Benjamin | d634357 | 2019-08-15 17:29:02 -0400 | [diff] [blame] | 981 | |
David Benjamin | a1d3bfb | 2021-06-01 12:12:44 -0400 | [diff] [blame] | 982 | // QUIC doesn't use an EndOfEarlyData message (RFC 9001, section 8.3), so we |
| 983 | // switch to client_handshake_secret before the early return. |
David Benjamin | d634357 | 2019-08-15 17:29:02 -0400 | [diff] [blame] | 984 | if (ssl->quic_method != nullptr) { |
| 985 | if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_open, |
David Benjamin | 754d4c9 | 2020-02-11 16:27:21 -0500 | [diff] [blame] | 986 | hs->new_session.get(), |
David Benjamin | d634357 | 2019-08-15 17:29:02 -0400 | [diff] [blame] | 987 | hs->client_handshake_secret())) { |
| 988 | return ssl_hs_error; |
| 989 | } |
David Benjamin | 71ed9d7 | 2021-01-12 18:18:46 -0500 | [diff] [blame] | 990 | hs->tls13_state = state13_process_end_of_early_data; |
David Benjamin | d634357 | 2019-08-15 17:29:02 -0400 | [diff] [blame] | 991 | return ssl->s3->early_data_accepted ? ssl_hs_early_return : ssl_hs_ok; |
| 992 | } |
| 993 | |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 994 | hs->tls13_state = state13_process_end_of_early_data; |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 995 | return ssl->s3->early_data_accepted ? ssl_hs_read_end_of_early_data |
| 996 | : ssl_hs_ok; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | static enum ssl_hs_wait_t do_process_end_of_early_data(SSL_HANDSHAKE *hs) { |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 1000 | SSL *const ssl = hs->ssl; |
David Benjamin | 71ed9d7 | 2021-01-12 18:18:46 -0500 | [diff] [blame] | 1001 | // In protocols that use EndOfEarlyData, we must consume the extra message and |
| 1002 | // switch to client_handshake_secret after the early return. |
| 1003 | if (ssl->quic_method == nullptr) { |
| 1004 | // If early data was not accepted, the EndOfEarlyData will be in the |
| 1005 | // discarded early data. |
| 1006 | if (hs->ssl->s3->early_data_accepted) { |
| 1007 | SSLMessage msg; |
| 1008 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1009 | return ssl_hs_read_message; |
| 1010 | } |
| 1011 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_END_OF_EARLY_DATA)) { |
| 1012 | return ssl_hs_error; |
| 1013 | } |
| 1014 | if (CBS_len(&msg.body) != 0) { |
| 1015 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1016 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1017 | return ssl_hs_error; |
| 1018 | } |
| 1019 | ssl->method->next_message(ssl); |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 1020 | } |
David Benjamin | 71ed9d7 | 2021-01-12 18:18:46 -0500 | [diff] [blame] | 1021 | if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_open, |
| 1022 | hs->new_session.get(), |
| 1023 | hs->client_handshake_secret())) { |
David Benjamin | f350351 | 2019-08-20 15:55:24 -0400 | [diff] [blame] | 1024 | return ssl_hs_error; |
| 1025 | } |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 1026 | } |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 1027 | hs->tls13_state = state13_read_client_encrypted_extensions; |
| 1028 | return ssl_hs_ok; |
| 1029 | } |
| 1030 | |
| 1031 | static enum ssl_hs_wait_t do_read_client_encrypted_extensions( |
| 1032 | SSL_HANDSHAKE *hs) { |
| 1033 | SSL *const ssl = hs->ssl; |
| 1034 | // For now, only one extension uses client EncryptedExtensions. This function |
| 1035 | // may be generalized if others use it in the future. |
| 1036 | if (hs->new_session->has_application_settings && |
| 1037 | !ssl->s3->early_data_accepted) { |
| 1038 | SSLMessage msg; |
| 1039 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1040 | return ssl_hs_read_message; |
| 1041 | } |
| 1042 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_ENCRYPTED_EXTENSIONS)) { |
| 1043 | return ssl_hs_error; |
| 1044 | } |
| 1045 | |
| 1046 | CBS body = msg.body, extensions; |
| 1047 | if (!CBS_get_u16_length_prefixed(&body, &extensions) || |
| 1048 | CBS_len(&body) != 0) { |
| 1049 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1050 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1051 | return ssl_hs_error; |
| 1052 | } |
| 1053 | |
Victor Tan | 558960d | 2023-06-23 15:04:33 +0000 | [diff] [blame] | 1054 | uint16_t extension_type = TLSEXT_TYPE_application_settings_old; |
| 1055 | if (hs->config->alps_use_new_codepoint) { |
| 1056 | extension_type = TLSEXT_TYPE_application_settings; |
| 1057 | } |
| 1058 | SSLExtension application_settings(extension_type); |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 1059 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | a75027b | 2021-07-20 15:07:22 -0400 | [diff] [blame] | 1060 | if (!ssl_parse_extensions(&extensions, &alert, {&application_settings}, |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 1061 | /*ignore_unknown=*/false)) { |
| 1062 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 1063 | return ssl_hs_error; |
| 1064 | } |
| 1065 | |
David Benjamin | a75027b | 2021-07-20 15:07:22 -0400 | [diff] [blame] | 1066 | if (!application_settings.present) { |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 1067 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
| 1068 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION); |
| 1069 | return ssl_hs_error; |
| 1070 | } |
| 1071 | |
| 1072 | // Note that, if 0-RTT was accepted, these values will already have been |
| 1073 | // initialized earlier. |
| 1074 | if (!hs->new_session->peer_application_settings.CopyFrom( |
David Benjamin | a75027b | 2021-07-20 15:07:22 -0400 | [diff] [blame] | 1075 | application_settings.data) || |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 1076 | !ssl_hash_message(hs, msg)) { |
| 1077 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1078 | return ssl_hs_error; |
| 1079 | } |
| 1080 | |
| 1081 | ssl->method->next_message(ssl); |
| 1082 | } |
| 1083 | |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1084 | hs->tls13_state = state13_read_client_certificate; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1085 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1086 | } |
| 1087 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1088 | static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1089 | SSL *const ssl = hs->ssl; |
| 1090 | if (!hs->cert_request) { |
David Benjamin | f350351 | 2019-08-20 15:55:24 -0400 | [diff] [blame] | 1091 | if (!ssl->s3->session_reused) { |
| 1092 | // OpenSSL returns X509_V_OK when no certificates are requested. This is |
| 1093 | // classed by them as a bug, but it's assumed by at least NGINX. (Only do |
| 1094 | // this in full handshakes as resumptions should carry over the previous |
| 1095 | // |verify_result|, though this is a no-op because servers do not |
| 1096 | // implement the client's odd soft-fail mode.) |
| 1097 | hs->new_session->verify_result = X509_V_OK; |
| 1098 | } |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 1099 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1100 | // Skip this state. |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1101 | hs->tls13_state = state13_read_channel_id; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1102 | return ssl_hs_ok; |
| 1103 | } |
| 1104 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 1105 | const bool allow_anonymous = |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1106 | (hs->config->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) == 0; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1107 | SSLMessage msg; |
| 1108 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1109 | return ssl_hs_read_message; |
| 1110 | } |
| 1111 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) || |
| 1112 | !tls13_process_certificate(hs, msg, allow_anonymous) || |
| 1113 | !ssl_hash_message(hs, msg)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1114 | return ssl_hs_error; |
| 1115 | } |
| 1116 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 1117 | ssl->method->next_message(ssl); |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1118 | hs->tls13_state = state13_read_client_certificate_verify; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1119 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1120 | } |
| 1121 | |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 1122 | static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1123 | SSL *const ssl = hs->ssl; |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 1124 | if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1125 | // Skip this state. |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1126 | hs->tls13_state = state13_read_channel_id; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1127 | return ssl_hs_ok; |
| 1128 | } |
| 1129 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1130 | SSLMessage msg; |
| 1131 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1132 | return ssl_hs_read_message; |
| 1133 | } |
| 1134 | |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 1135 | switch (ssl_verify_peer_cert(hs)) { |
| 1136 | case ssl_verify_ok: |
| 1137 | break; |
| 1138 | case ssl_verify_invalid: |
| 1139 | return ssl_hs_error; |
| 1140 | case ssl_verify_retry: |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1141 | hs->tls13_state = state13_read_client_certificate_verify; |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 1142 | return ssl_hs_certificate_verify; |
| 1143 | } |
| 1144 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1145 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY) || |
| 1146 | !tls13_process_certificate_verify(hs, msg) || |
| 1147 | !ssl_hash_message(hs, msg)) { |
David Benjamin | 6929f27 | 2016-11-16 19:10:08 +0900 | [diff] [blame] | 1148 | return ssl_hs_error; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1149 | } |
| 1150 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 1151 | ssl->method->next_message(ssl); |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1152 | hs->tls13_state = state13_read_channel_id; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1153 | return ssl_hs_ok; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1156 | static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) { |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 1157 | SSL *const ssl = hs->ssl; |
David Benjamin | 8acec00 | 2021-05-19 13:03:34 -0400 | [diff] [blame] | 1158 | if (!hs->channel_id_negotiated) { |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1159 | hs->tls13_state = state13_read_client_finished; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 1160 | return ssl_hs_ok; |
| 1161 | } |
| 1162 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1163 | SSLMessage msg; |
| 1164 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1165 | return ssl_hs_read_message; |
| 1166 | } |
| 1167 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) || |
| 1168 | !tls1_verify_channel_id(hs, msg) || |
| 1169 | !ssl_hash_message(hs, msg)) { |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 1170 | return ssl_hs_error; |
| 1171 | } |
| 1172 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 1173 | ssl->method->next_message(ssl); |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1174 | hs->tls13_state = state13_read_client_finished; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1175 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1176 | } |
| 1177 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1178 | static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1179 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1180 | SSLMessage msg; |
| 1181 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1182 | return ssl_hs_read_message; |
| 1183 | } |
| 1184 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1185 | // If early data was accepted, we've already computed the client Finished |
| 1186 | // and derived the resumption secret. |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 1187 | !tls13_process_finished(hs, msg, ssl->s3->early_data_accepted) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1188 | // evp_aead_seal keys have already been switched. |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 1189 | !tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_open, |
David Benjamin | 754d4c9 | 2020-02-11 16:27:21 -0500 | [diff] [blame] | 1190 | hs->new_session.get(), |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 1191 | hs->client_traffic_secret_0())) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1192 | return ssl_hs_error; |
| 1193 | } |
| 1194 | |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 1195 | if (!ssl->s3->early_data_accepted) { |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1196 | if (!ssl_hash_message(hs, msg) || |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 1197 | !tls13_derive_resumption_secret(hs)) { |
| 1198 | return ssl_hs_error; |
| 1199 | } |
| 1200 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1201 | // We send post-handshake tickets as part of the handshake in 1-RTT. |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1202 | hs->tls13_state = state13_send_new_session_ticket; |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 1203 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1204 | // We already sent half-RTT tickets. |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1205 | hs->tls13_state = state13_done; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 1206 | } |
| 1207 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 1208 | ssl->method->next_message(ssl); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1209 | return ssl_hs_ok; |
| 1210 | } |
| 1211 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1212 | static enum ssl_hs_wait_t do_send_new_session_ticket(SSL_HANDSHAKE *hs) { |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 1213 | bool sent_tickets; |
| 1214 | if (!add_new_session_tickets(hs, &sent_tickets)) { |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 1215 | return ssl_hs_error; |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 1216 | } |
| 1217 | |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1218 | hs->tls13_state = state13_done; |
Steven Valdez | 777a239 | 2019-02-21 11:30:47 -0500 | [diff] [blame] | 1219 | // In TLS 1.3, the NewSessionTicket isn't flushed until the server performs a |
| 1220 | // write, to prevent a non-reading client from causing the server to hang in |
| 1221 | // the case of a small server write buffer. Consumers which don't write data |
| 1222 | // to the client will need to do a zero-byte write if they wish to flush the |
| 1223 | // tickets. |
Goutam Tamvada | 49de1fc | 2019-10-04 16:58:29 -0400 | [diff] [blame] | 1224 | if (hs->ssl->quic_method != nullptr && sent_tickets) { |
Steven Valdez | 777a239 | 2019-02-21 11:30:47 -0500 | [diff] [blame] | 1225 | return ssl_hs_flush; |
| 1226 | } |
| 1227 | return ssl_hs_ok; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 1228 | } |
| 1229 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1230 | enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs) { |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1231 | while (hs->tls13_state != state13_done) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1232 | enum ssl_hs_wait_t ret = ssl_hs_error; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1233 | enum tls13_server_hs_state_t state = |
| 1234 | static_cast<enum tls13_server_hs_state_t>(hs->tls13_state); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1235 | switch (state) { |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1236 | case state13_select_parameters: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1237 | ret = do_select_parameters(hs); |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 1238 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1239 | case state13_select_session: |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 1240 | ret = do_select_session(hs); |
| 1241 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1242 | case state13_send_hello_retry_request: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1243 | ret = do_send_hello_retry_request(hs); |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 1244 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1245 | case state13_read_second_client_hello: |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1246 | ret = do_read_second_client_hello(hs); |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 1247 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1248 | case state13_send_server_hello: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1249 | ret = do_send_server_hello(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1250 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1251 | case state13_send_server_certificate_verify: |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 1252 | ret = do_send_server_certificate_verify(hs); |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 1253 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1254 | case state13_send_server_finished: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1255 | ret = do_send_server_finished(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1256 | break; |
Matthew Braithwaite | 093a823 | 2020-01-28 14:06:55 -0800 | [diff] [blame] | 1257 | case state13_send_half_rtt_ticket: |
| 1258 | ret = do_send_half_rtt_ticket(hs); |
| 1259 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1260 | case state13_read_second_client_flight: |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 1261 | ret = do_read_second_client_flight(hs); |
| 1262 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1263 | case state13_process_end_of_early_data: |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 1264 | ret = do_process_end_of_early_data(hs); |
| 1265 | break; |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 1266 | case state13_read_client_encrypted_extensions: |
| 1267 | ret = do_read_client_encrypted_extensions(hs); |
| 1268 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1269 | case state13_read_client_certificate: |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1270 | ret = do_read_client_certificate(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1271 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1272 | case state13_read_client_certificate_verify: |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1273 | ret = do_read_client_certificate_verify(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1274 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1275 | case state13_read_channel_id: |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1276 | ret = do_read_channel_id(hs); |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 1277 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1278 | case state13_read_client_finished: |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1279 | ret = do_read_client_finished(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1280 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1281 | case state13_send_new_session_ticket: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1282 | ret = do_send_new_session_ticket(hs); |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 1283 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1284 | case state13_done: |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1285 | ret = ssl_hs_ok; |
| 1286 | break; |
| 1287 | } |
| 1288 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1289 | if (hs->tls13_state != state) { |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1290 | ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1); |
| 1291 | } |
| 1292 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1293 | if (ret != ssl_hs_ok) { |
| 1294 | return ret; |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | return ssl_hs_ok; |
| 1299 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1300 | |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1301 | const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs) { |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1302 | enum tls13_server_hs_state_t state = |
| 1303 | static_cast<enum tls13_server_hs_state_t>(hs->tls13_state); |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1304 | switch (state) { |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1305 | case state13_select_parameters: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1306 | return "TLS 1.3 server select_parameters"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1307 | case state13_select_session: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1308 | return "TLS 1.3 server select_session"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1309 | case state13_send_hello_retry_request: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1310 | return "TLS 1.3 server send_hello_retry_request"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1311 | case state13_read_second_client_hello: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1312 | return "TLS 1.3 server read_second_client_hello"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1313 | case state13_send_server_hello: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1314 | return "TLS 1.3 server send_server_hello"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1315 | case state13_send_server_certificate_verify: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1316 | return "TLS 1.3 server send_server_certificate_verify"; |
Matthew Braithwaite | 093a823 | 2020-01-28 14:06:55 -0800 | [diff] [blame] | 1317 | case state13_send_half_rtt_ticket: |
| 1318 | return "TLS 1.3 server send_half_rtt_ticket"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1319 | case state13_send_server_finished: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1320 | return "TLS 1.3 server send_server_finished"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1321 | case state13_read_second_client_flight: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1322 | return "TLS 1.3 server read_second_client_flight"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1323 | case state13_process_end_of_early_data: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1324 | return "TLS 1.3 server process_end_of_early_data"; |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 1325 | case state13_read_client_encrypted_extensions: |
| 1326 | return "TLS 1.3 server read_client_encrypted_extensions"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1327 | case state13_read_client_certificate: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1328 | return "TLS 1.3 server read_client_certificate"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1329 | case state13_read_client_certificate_verify: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1330 | return "TLS 1.3 server read_client_certificate_verify"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1331 | case state13_read_channel_id: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1332 | return "TLS 1.3 server read_channel_id"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1333 | case state13_read_client_finished: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1334 | return "TLS 1.3 server read_client_finished"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1335 | case state13_send_new_session_ticket: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1336 | return "TLS 1.3 server send_new_session_ticket"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1337 | case state13_done: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1338 | return "TLS 1.3 server done"; |
| 1339 | } |
| 1340 | |
| 1341 | return "TLS 1.3 server unknown"; |
| 1342 | } |
| 1343 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 1344 | BSSL_NAMESPACE_END |