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> |
| 26 | #include <openssl/mem.h> |
| 27 | #include <openssl/rand.h> |
| 28 | #include <openssl/stack.h> |
| 29 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 30 | #include "../crypto/internal.h" |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 31 | #include "internal.h" |
| 32 | |
| 33 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 34 | BSSL_NAMESPACE_BEGIN |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 35 | |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 36 | static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0}; |
| 37 | |
David Benjamin | 6433a91 | 2019-05-04 14:17:08 -0500 | [diff] [blame] | 38 | // Allow a minute of ticket age skew in either direction. This covers |
| 39 | // transmission delays in ClientHello and NewSessionTicket, as well as |
| 40 | // drift between client and server clock rate since the ticket was issued. |
| 41 | // See RFC 8446, section 8.3. |
| 42 | static const int32_t kMaxTicketAgeSkewSeconds = 60; |
| 43 | |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 44 | static int resolve_ecdhe_secret(SSL_HANDSHAKE *hs, bool *out_need_retry, |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 45 | SSL_CLIENT_HELLO *client_hello) { |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 46 | SSL *const ssl = hs->ssl; |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 47 | *out_need_retry = false; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 48 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 49 | // We only support connections that include an ECDHE key exchange. |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 50 | CBS key_share; |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 51 | if (!ssl_client_hello_get_extension(client_hello, &key_share, |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 52 | TLSEXT_TYPE_key_share)) { |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 53 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 54 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION); |
David Benjamin | 6929f27 | 2016-11-16 19:10:08 +0900 | [diff] [blame] | 55 | return 0; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 56 | } |
| 57 | |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 58 | bool found_key_share; |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 59 | Array<uint8_t> dhe_secret; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 60 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8baf963 | 2016-11-17 17:11:16 +0900 | [diff] [blame] | 61 | if (!ssl_ext_key_share_parse_clienthello(hs, &found_key_share, &dhe_secret, |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 62 | &alert, &key_share)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 63 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | if (!found_key_share) { |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 68 | *out_need_retry = true; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
David Benjamin | 79b8b3a | 2019-08-16 18:58:13 -0400 | [diff] [blame] | 72 | return tls13_advance_key_schedule(hs, dhe_secret); |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 73 | } |
| 74 | |
Steven Valdez | 038da9b | 2017-07-10 12:57:25 -0400 | [diff] [blame] | 75 | static int ssl_ext_supported_versions_add_serverhello(SSL_HANDSHAKE *hs, |
| 76 | CBB *out) { |
| 77 | CBB contents; |
| 78 | if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) || |
| 79 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 80 | !CBB_add_u16(&contents, hs->ssl->version) || |
| 81 | !CBB_flush(out)) { |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | return 1; |
| 86 | } |
| 87 | |
David Benjamin | 34202b9 | 2016-11-16 19:07:53 +0900 | [diff] [blame] | 88 | static const SSL_CIPHER *choose_tls13_cipher( |
David Benjamin | 43cc9c6 | 2018-12-12 13:06:46 -0600 | [diff] [blame] | 89 | 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] | 90 | CBS cipher_suites; |
| 91 | CBS_init(&cipher_suites, client_hello->cipher_suites, |
| 92 | client_hello->cipher_suites_len); |
| 93 | |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 94 | const uint16_t version = ssl_protocol_version(ssl); |
David Benjamin | 34202b9 | 2016-11-16 19:07:53 +0900 | [diff] [blame] | 95 | |
Steven Valdez | d6f9c35 | 2019-06-25 10:13:18 -0400 | [diff] [blame] | 96 | return ssl_choose_tls13_cipher(cipher_suites, version, group_id); |
David Benjamin | 34202b9 | 2016-11-16 19:07:53 +0900 | [diff] [blame] | 97 | } |
| 98 | |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 99 | 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] | 100 | SSL *const ssl = hs->ssl; |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 101 | if (// If the client doesn't accept resumption with PSK_DHE_KE, don't send a |
| 102 | // session ticket. |
| 103 | !hs->accept_psk_mode || |
| 104 | // We only implement stateless resumption in TLS 1.3, so skip sending |
| 105 | // tickets if disabled. |
| 106 | (SSL_get_options(ssl) & SSL_OP_NO_TICKET)) { |
| 107 | *out_sent_tickets = false; |
| 108 | return true; |
| 109 | } |
| 110 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 111 | // TLS 1.3 recommends single-use tickets, so issue multiple tickets in case |
| 112 | // the client makes several connections before getting a renewal. |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 113 | static const int kNumTickets = 2; |
| 114 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 115 | // Rebase the session timestamp so that it is measured from ticket |
| 116 | // issuance. |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 117 | ssl_session_rebase_time(ssl, hs->new_session.get()); |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 118 | |
| 119 | for (int i = 0; i < kNumTickets; i++) { |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 120 | UniquePtr<SSL_SESSION> session( |
| 121 | SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_INCLUDE_NONAUTH)); |
| 122 | if (!session) { |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 123 | return false; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 124 | } |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 125 | |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 126 | if (!RAND_bytes((uint8_t *)&session->ticket_age_add, 4)) { |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 127 | return false; |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 128 | } |
David Benjamin | a3a71e9 | 2018-06-29 13:24:45 -0400 | [diff] [blame] | 129 | session->ticket_age_add_valid = true; |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 130 | if (ssl->enable_early_data) { |
David Benjamin | d634357 | 2019-08-15 17:29:02 -0400 | [diff] [blame] | 131 | // QUIC does not use the max_early_data_size parameter and always sets it |
| 132 | // to a fixed value. See draft-ietf-quic-tls-22, section 4.5. |
| 133 | session->ticket_max_early_data = |
| 134 | ssl->quic_method != nullptr ? 0xffffffff : kMaxEarlyDataAccepted; |
Steven Valdez | be165a2 | 2017-10-10 11:45:01 -0400 | [diff] [blame] | 135 | } |
| 136 | |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 137 | static_assert(kNumTickets < 256, "Too many tickets"); |
| 138 | uint8_t nonce[] = {static_cast<uint8_t>(i)}; |
| 139 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 140 | ScopedCBB cbb; |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 141 | CBB body, nonce_cbb, ticket, extensions; |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 142 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 143 | SSL3_MT_NEW_SESSION_TICKET) || |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 144 | !CBB_add_u32(&body, session->timeout) || |
| 145 | !CBB_add_u32(&body, session->ticket_age_add) || |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 146 | !CBB_add_u8_length_prefixed(&body, &nonce_cbb) || |
| 147 | !CBB_add_bytes(&nonce_cbb, nonce, sizeof(nonce)) || |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 148 | !CBB_add_u16_length_prefixed(&body, &ticket) || |
Alessandro Ghedini | 2cc6f44 | 2018-12-11 11:35:17 +0000 | [diff] [blame] | 149 | !tls13_derive_session_psk(session.get(), nonce) || |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 150 | !ssl_encrypt_ticket(hs, &ticket, session.get()) || |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 151 | !CBB_add_u16_length_prefixed(&body, &extensions)) { |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 152 | return false; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 153 | } |
| 154 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 155 | if (ssl->enable_early_data) { |
David Benjamin | a93beba | 2019-10-15 16:58:21 -0400 | [diff] [blame] | 156 | CBB early_data; |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 157 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_early_data) || |
David Benjamin | a93beba | 2019-10-15 16:58:21 -0400 | [diff] [blame] | 158 | !CBB_add_u16_length_prefixed(&extensions, &early_data) || |
| 159 | !CBB_add_u32(&early_data, session->ticket_max_early_data) || |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 160 | !CBB_flush(&extensions)) { |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 161 | return false; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 165 | // Add a fake extension. See draft-davidben-tls-grease-01. |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 166 | if (!CBB_add_u16(&extensions, |
David Benjamin | a7bc944 | 2018-01-18 10:08:53 -0500 | [diff] [blame] | 167 | ssl_get_grease_value(hs, ssl_grease_ticket_extension)) || |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 168 | !CBB_add_u16(&extensions, 0 /* empty */)) { |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 169 | return false; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 170 | } |
| 171 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 172 | if (!ssl_add_message_cbb(ssl, cbb.get())) { |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 173 | return false; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 177 | *out_sent_tickets = true; |
| 178 | return true; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 179 | } |
| 180 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 181 | static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 182 | // At this point, most ClientHello extensions have already been processed by |
| 183 | // the common handshake logic. Resolve the remaining non-PSK parameters. |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 184 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 185 | SSLMessage msg; |
| 186 | if (!ssl->method->get_message(ssl, &msg)) { |
| 187 | return ssl_hs_read_message; |
| 188 | } |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 189 | SSL_CLIENT_HELLO client_hello; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 190 | if (!ssl_client_hello_init(ssl, &client_hello, msg)) { |
David Benjamin | 34202b9 | 2016-11-16 19:07:53 +0900 | [diff] [blame] | 191 | OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 192 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 34202b9 | 2016-11-16 19:07:53 +0900 | [diff] [blame] | 193 | return ssl_hs_error; |
| 194 | } |
| 195 | |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 196 | OPENSSL_memcpy(hs->session_id, client_hello.session_id, |
| 197 | client_hello.session_id_len); |
| 198 | hs->session_id_len = client_hello.session_id_len; |
| 199 | |
David Benjamin | 43cc9c6 | 2018-12-12 13:06:46 -0600 | [diff] [blame] | 200 | uint16_t group_id; |
| 201 | if (!tls1_get_shared_group(hs, &group_id)) { |
| 202 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP); |
| 203 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 204 | return ssl_hs_error; |
| 205 | } |
| 206 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 207 | // Negotiate the cipher suite. |
David Benjamin | 43cc9c6 | 2018-12-12 13:06:46 -0600 | [diff] [blame] | 208 | hs->new_cipher = choose_tls13_cipher(ssl, &client_hello, group_id); |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 209 | if (hs->new_cipher == NULL) { |
David Benjamin | f01f42a | 2016-11-16 19:05:33 +0900 | [diff] [blame] | 210 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 211 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
David Benjamin | f01f42a | 2016-11-16 19:05:33 +0900 | [diff] [blame] | 212 | return ssl_hs_error; |
| 213 | } |
| 214 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 215 | // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was |
| 216 | // deferred. Complete it now. |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 217 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 218 | if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 219 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 220 | return ssl_hs_error; |
| 221 | } |
| 222 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 223 | // The PRF hash is now known. Set up the key schedule and hash the |
| 224 | // ClientHello. |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 225 | if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher)) { |
| 226 | return ssl_hs_error; |
| 227 | } |
| 228 | |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 229 | hs->tls13_state = state13_select_session; |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 230 | return ssl_hs_ok; |
| 231 | } |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 232 | |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 233 | static enum ssl_ticket_aead_result_t select_session( |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 234 | SSL_HANDSHAKE *hs, uint8_t *out_alert, UniquePtr<SSL_SESSION> *out_session, |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 235 | int32_t *out_ticket_age_skew, bool *out_offered_ticket, |
| 236 | const SSLMessage &msg, const SSL_CLIENT_HELLO *client_hello) { |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 237 | SSL *const ssl = hs->ssl; |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 238 | *out_session = nullptr; |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 239 | |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 240 | CBS pre_shared_key; |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 241 | *out_offered_ticket = ssl_client_hello_get_extension( |
| 242 | client_hello, &pre_shared_key, TLSEXT_TYPE_pre_shared_key); |
| 243 | if (!*out_offered_ticket) { |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 244 | return ssl_ticket_aead_ignore_ticket; |
| 245 | } |
| 246 | |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 247 | CBS ticket, binders; |
| 248 | uint32_t client_ticket_age; |
David Benjamin | 9806ae0 | 2019-08-16 15:32:03 -0400 | [diff] [blame] | 249 | if (!ssl_ext_pre_shared_key_parse_clienthello( |
| 250 | hs, &ticket, &binders, &client_ticket_age, out_alert, client_hello, |
| 251 | &pre_shared_key)) { |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 252 | return ssl_ticket_aead_error; |
| 253 | } |
| 254 | |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 255 | // If the peer did not offer psk_dhe, ignore the resumption. |
| 256 | if (!hs->accept_psk_mode) { |
| 257 | return ssl_ticket_aead_ignore_ticket; |
| 258 | } |
| 259 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 260 | // TLS 1.3 session tickets are renewed separately as part of the |
| 261 | // NewSessionTicket. |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 262 | bool unused_renew; |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 263 | UniquePtr<SSL_SESSION> session; |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 264 | enum ssl_ticket_aead_result_t ret = |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 265 | ssl_process_ticket(hs, &session, &unused_renew, ticket, {}); |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 266 | switch (ret) { |
| 267 | case ssl_ticket_aead_success: |
| 268 | break; |
| 269 | case ssl_ticket_aead_error: |
| 270 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 271 | return ret; |
| 272 | default: |
| 273 | return ret; |
| 274 | } |
| 275 | |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 276 | if (!ssl_session_is_resumable(hs, session.get()) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 277 | // Historically, some TLS 1.3 tickets were missing ticket_age_add. |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 278 | !session->ticket_age_add_valid) { |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 279 | return ssl_ticket_aead_ignore_ticket; |
| 280 | } |
| 281 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 282 | // Recover the client ticket age and convert to seconds. |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 283 | client_ticket_age -= session->ticket_age_add; |
| 284 | client_ticket_age /= 1000; |
| 285 | |
| 286 | struct OPENSSL_timeval now; |
| 287 | ssl_get_current_time(ssl, &now); |
| 288 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 289 | // Compute the server ticket age in seconds. |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 290 | assert(now.tv_sec >= session->time); |
| 291 | uint64_t server_ticket_age = now.tv_sec - session->time; |
| 292 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 293 | // To avoid overflowing |hs->ticket_age_skew|, we will not resume |
| 294 | // 68-year-old sessions. |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 295 | if (server_ticket_age > INT32_MAX) { |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 296 | return ssl_ticket_aead_ignore_ticket; |
| 297 | } |
| 298 | |
David Benjamin | 8c98bac | 2019-08-15 20:40:01 -0400 | [diff] [blame] | 299 | *out_ticket_age_skew = static_cast<int32_t>(client_ticket_age) - |
| 300 | static_cast<int32_t>(server_ticket_age); |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 301 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 302 | // Check the PSK binder. |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 303 | if (!tls13_verify_psk_binder(hs, session.get(), msg, &binders)) { |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 304 | *out_alert = SSL_AD_DECRYPT_ERROR; |
| 305 | return ssl_ticket_aead_error; |
| 306 | } |
| 307 | |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 308 | *out_session = std::move(session); |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 309 | return ssl_ticket_aead_success; |
| 310 | } |
| 311 | |
| 312 | static enum ssl_hs_wait_t do_select_session(SSL_HANDSHAKE *hs) { |
| 313 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 314 | SSLMessage msg; |
| 315 | if (!ssl->method->get_message(ssl, &msg)) { |
| 316 | return ssl_hs_read_message; |
| 317 | } |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 318 | SSL_CLIENT_HELLO client_hello; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 319 | if (!ssl_client_hello_init(ssl, &client_hello, msg)) { |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 320 | OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 321 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 322 | return ssl_hs_error; |
| 323 | } |
| 324 | |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 325 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 326 | UniquePtr<SSL_SESSION> session; |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 327 | bool offered_ticket = false; |
| 328 | switch (select_session(hs, &alert, &session, &ssl->s3->ticket_age_skew, |
| 329 | &offered_ticket, msg, &client_hello)) { |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 330 | case ssl_ticket_aead_ignore_ticket: |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 331 | assert(!session); |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 332 | if (!ssl->enable_early_data) { |
| 333 | ssl->s3->early_data_reason = ssl_early_data_disabled; |
| 334 | } else if (!offered_ticket) { |
| 335 | ssl->s3->early_data_reason = ssl_early_data_no_session_offered; |
| 336 | } else { |
| 337 | ssl->s3->early_data_reason = ssl_early_data_session_not_resumed; |
| 338 | } |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 339 | if (!ssl_get_new_session(hs, 1 /* server */)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 340 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | f01f42a | 2016-11-16 19:05:33 +0900 | [diff] [blame] | 341 | return ssl_hs_error; |
| 342 | } |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 343 | break; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 344 | |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 345 | case ssl_ticket_aead_success: |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 346 | // Carry over authentication information from the previous handshake into |
| 347 | // a fresh session. |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 348 | hs->new_session = |
| 349 | SSL_SESSION_dup(session.get(), SSL_SESSION_DUP_AUTH_ONLY); |
David Benjamin | 6433a91 | 2019-05-04 14:17:08 -0500 | [diff] [blame] | 350 | if (hs->new_session == nullptr) { |
| 351 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 352 | return ssl_hs_error; |
| 353 | } |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 354 | |
David Benjamin | 754d4c9 | 2020-02-11 16:27:21 -0500 | [diff] [blame] | 355 | // |ssl_session_is_resumable| forbids cross-cipher resumptions even if the |
| 356 | // PRF hashes match. |
| 357 | assert(hs->new_cipher == session->cipher); |
| 358 | |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 359 | if (!ssl->enable_early_data) { |
| 360 | ssl->s3->early_data_reason = ssl_early_data_disabled; |
| 361 | } else if (session->ticket_max_early_data == 0) { |
| 362 | ssl->s3->early_data_reason = ssl_early_data_unsupported_for_session; |
| 363 | } else if (!hs->early_data_offered) { |
| 364 | ssl->s3->early_data_reason = ssl_early_data_peer_declined; |
| 365 | } else if (ssl->s3->channel_id_valid) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 366 | // Channel ID is incompatible with 0-RTT. |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 367 | ssl->s3->early_data_reason = ssl_early_data_channel_id; |
| 368 | } else if (ssl->s3->token_binding_negotiated) { |
| 369 | // Token Binding is incompatible with 0-RTT. |
| 370 | ssl->s3->early_data_reason = ssl_early_data_token_binding; |
| 371 | } else if (MakeConstSpan(ssl->s3->alpn_selected) != session->early_alpn) { |
| 372 | // The negotiated ALPN must match the one in the ticket. |
| 373 | ssl->s3->early_data_reason = ssl_early_data_alpn_mismatch; |
David Benjamin | 6433a91 | 2019-05-04 14:17:08 -0500 | [diff] [blame] | 374 | } else if (ssl->s3->ticket_age_skew < -kMaxTicketAgeSkewSeconds || |
| 375 | kMaxTicketAgeSkewSeconds < ssl->s3->ticket_age_skew) { |
| 376 | ssl->s3->early_data_reason = ssl_early_data_ticket_age_skew; |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 377 | } else { |
| 378 | ssl->s3->early_data_reason = ssl_early_data_accepted; |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 379 | ssl->s3->early_data_accepted = true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 380 | } |
| 381 | |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 382 | ssl->s3->session_reused = true; |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 383 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 384 | // Resumption incorporates fresh key material, so refresh the timeout. |
David Benjamin | 98472cb | 2018-05-02 16:05:36 -0400 | [diff] [blame] | 385 | ssl_session_renew_timeout(ssl, hs->new_session.get(), |
| 386 | ssl->session_ctx->session_psk_dhe_timeout); |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 387 | break; |
| 388 | |
| 389 | case ssl_ticket_aead_error: |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 390 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 391 | return ssl_hs_error; |
| 392 | |
| 393 | case ssl_ticket_aead_retry: |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 394 | hs->tls13_state = state13_select_session; |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 395 | return ssl_hs_pending_ticket; |
| 396 | } |
| 397 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 398 | // Record connection properties in the new session. |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 399 | hs->new_session->cipher = hs->new_cipher; |
| 400 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 401 | // Store the initial negotiated ALPN in the session. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 402 | if (!hs->new_session->early_alpn.CopyFrom(ssl->s3->alpn_selected)) { |
| 403 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 404 | return ssl_hs_error; |
Steven Valdez | 27a9e6a | 2017-02-14 13:20:40 -0500 | [diff] [blame] | 405 | } |
| 406 | |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 407 | if (ssl->ctx->dos_protection_cb != NULL && |
| 408 | ssl->ctx->dos_protection_cb(&client_hello) == 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 409 | // Connection rejected for DOS reasons. |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 410 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 411 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 412 | return ssl_hs_error; |
| 413 | } |
| 414 | |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 415 | size_t hash_len = EVP_MD_size( |
| 416 | ssl_get_handshake_digest(ssl_protocol_version(ssl), hs->new_cipher)); |
| 417 | |
| 418 | // Set up the key schedule and incorporate the PSK into the running secret. |
David Benjamin | 8f820b4 | 2016-11-30 11:24:40 -0500 | [diff] [blame] | 419 | if (ssl->s3->session_reused) { |
David Benjamin | 79b8b3a | 2019-08-16 18:58:13 -0400 | [diff] [blame] | 420 | if (!tls13_init_key_schedule( |
| 421 | hs, MakeConstSpan(hs->new_session->master_key, |
| 422 | hs->new_session->master_key_length))) { |
David Benjamin | 8f820b4 | 2016-11-30 11:24:40 -0500 | [diff] [blame] | 423 | return ssl_hs_error; |
| 424 | } |
David Benjamin | 79b8b3a | 2019-08-16 18:58:13 -0400 | [diff] [blame] | 425 | } else if (!tls13_init_key_schedule(hs, MakeConstSpan(kZeroes, hash_len))) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 426 | return ssl_hs_error; |
| 427 | } |
| 428 | |
David Benjamin | 9806ae0 | 2019-08-16 15:32:03 -0400 | [diff] [blame] | 429 | if (!ssl_hash_message(hs, msg)) { |
| 430 | return ssl_hs_error; |
| 431 | } |
| 432 | |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 433 | if (ssl->s3->early_data_accepted) { |
David Benjamin | d634357 | 2019-08-15 17:29:02 -0400 | [diff] [blame] | 434 | if (!tls13_derive_early_secret(hs)) { |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 435 | return ssl_hs_error; |
| 436 | } |
| 437 | } else if (hs->early_data_offered) { |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 438 | ssl->s3->skip_early_data = true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 439 | } |
| 440 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 441 | // Resolve ECDHE and incorporate it into the secret. |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 442 | bool need_retry; |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 443 | if (!resolve_ecdhe_secret(hs, &need_retry, &client_hello)) { |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 444 | if (need_retry) { |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 445 | if (ssl->s3->early_data_accepted) { |
| 446 | ssl->s3->early_data_reason = ssl_early_data_hello_retry_request; |
| 447 | ssl->s3->early_data_accepted = false; |
| 448 | } |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 449 | ssl->s3->skip_early_data = true; |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 450 | ssl->method->next_message(ssl); |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 451 | if (!hs->transcript.UpdateForHelloRetryRequest()) { |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 452 | return ssl_hs_error; |
| 453 | } |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 454 | hs->tls13_state = state13_send_hello_retry_request; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 455 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 456 | } |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 457 | return ssl_hs_error; |
| 458 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 459 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 460 | ssl->method->next_message(ssl); |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 461 | hs->tls13_state = state13_send_server_hello; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 462 | return ssl_hs_ok; |
| 463 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 464 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 465 | static enum ssl_hs_wait_t do_send_hello_retry_request(SSL_HANDSHAKE *hs) { |
| 466 | SSL *const ssl = hs->ssl; |
Steven Valdez | 964b237 | 2017-11-07 17:09:52 -0500 | [diff] [blame] | 467 | |
| 468 | |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 469 | ScopedCBB cbb; |
| 470 | CBB body, session_id, extensions; |
| 471 | uint16_t group_id; |
| 472 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) || |
| 473 | !CBB_add_u16(&body, TLS1_2_VERSION) || |
| 474 | !CBB_add_bytes(&body, kHelloRetryRequest, SSL3_RANDOM_SIZE) || |
| 475 | !CBB_add_u8_length_prefixed(&body, &session_id) || |
| 476 | !CBB_add_bytes(&session_id, hs->session_id, hs->session_id_len) || |
| 477 | !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) || |
| 478 | !CBB_add_u8(&body, 0 /* no compression */) || |
| 479 | !tls1_get_shared_group(hs, &group_id) || |
| 480 | !CBB_add_u16_length_prefixed(&body, &extensions) || |
| 481 | !CBB_add_u16(&extensions, TLSEXT_TYPE_supported_versions) || |
| 482 | !CBB_add_u16(&extensions, 2 /* length */) || |
| 483 | !CBB_add_u16(&extensions, ssl->version) || |
| 484 | !CBB_add_u16(&extensions, TLSEXT_TYPE_key_share) || |
| 485 | !CBB_add_u16(&extensions, 2 /* length */) || |
| 486 | !CBB_add_u16(&extensions, group_id) || |
| 487 | !ssl_add_message_cbb(ssl, cbb.get())) { |
| 488 | return ssl_hs_error; |
| 489 | } |
Steven Valdez | 964b237 | 2017-11-07 17:09:52 -0500 | [diff] [blame] | 490 | |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 491 | if (!ssl->method->add_change_cipher_spec(ssl)) { |
| 492 | return ssl_hs_error; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 493 | } |
| 494 | |
Kris Kwiatkowski | b11902a | 2019-08-24 11:01:04 +0100 | [diff] [blame] | 495 | ssl->s3->used_hello_retry_request = true; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 496 | hs->tls13_state = state13_read_second_client_hello; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 497 | return ssl_hs_flush; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 498 | } |
| 499 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 500 | 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] | 501 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 502 | SSLMessage msg; |
| 503 | if (!ssl->method->get_message(ssl, &msg)) { |
| 504 | return ssl_hs_read_message; |
| 505 | } |
| 506 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) { |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 507 | return ssl_hs_error; |
| 508 | } |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 509 | SSL_CLIENT_HELLO client_hello; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 510 | if (!ssl_client_hello_init(ssl, &client_hello, msg)) { |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 511 | OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 512 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 513 | return ssl_hs_error; |
| 514 | } |
| 515 | |
David Benjamin | 9806ae0 | 2019-08-16 15:32:03 -0400 | [diff] [blame] | 516 | // We perform all our negotiation based on the first ClientHello (for |
| 517 | // consistency with what |select_certificate_cb| observed), which is in the |
| 518 | // transcript, so we can ignore most of this second one. |
| 519 | // |
| 520 | // We do, however, check the second PSK binder. This covers the client key |
| 521 | // share, in case we ever send half-RTT data (we currently do not). It is also |
| 522 | // a tricky computation, so we enforce the peer handled it correctly. |
| 523 | if (ssl->s3->session_reused) { |
| 524 | CBS pre_shared_key; |
| 525 | if (!ssl_client_hello_get_extension(&client_hello, &pre_shared_key, |
| 526 | TLSEXT_TYPE_pre_shared_key)) { |
| 527 | OPENSSL_PUT_ERROR(SSL, SSL_R_INCONSISTENT_CLIENT_HELLO); |
| 528 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 529 | return ssl_hs_error; |
| 530 | } |
| 531 | |
| 532 | CBS ticket, binders; |
| 533 | uint32_t client_ticket_age; |
| 534 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 535 | if (!ssl_ext_pre_shared_key_parse_clienthello( |
| 536 | hs, &ticket, &binders, &client_ticket_age, &alert, &client_hello, |
| 537 | &pre_shared_key)) { |
| 538 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 539 | return ssl_hs_error; |
| 540 | } |
| 541 | |
| 542 | // Note it is important that we do not obtain a new |SSL_SESSION| from |
| 543 | // |ticket|. We have already selected parameters based on the first |
| 544 | // ClientHello (in the transcript) and must not switch partway through. |
| 545 | if (!tls13_verify_psk_binder(hs, hs->new_session.get(), msg, &binders)) { |
| 546 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
| 547 | return ssl_hs_error; |
| 548 | } |
| 549 | } |
| 550 | |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 551 | bool need_retry; |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 552 | if (!resolve_ecdhe_secret(hs, &need_retry, &client_hello)) { |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 553 | if (need_retry) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 554 | // Only send one HelloRetryRequest. |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 555 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 556 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 557 | } |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 558 | return ssl_hs_error; |
| 559 | } |
| 560 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 561 | if (!ssl_hash_message(hs, msg)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 562 | return ssl_hs_error; |
| 563 | } |
| 564 | |
David Benjamin | f9cc26f | 2020-02-09 16:49:31 -0500 | [diff] [blame] | 565 | // ClientHello should be the end of the flight. |
| 566 | if (ssl->method->has_unprocessed_handshake_data(ssl)) { |
| 567 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 568 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA); |
| 569 | return ssl_hs_error; |
| 570 | } |
| 571 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 572 | ssl->method->next_message(ssl); |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 573 | hs->tls13_state = state13_send_server_hello; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 574 | return ssl_hs_ok; |
| 575 | } |
| 576 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 577 | static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) { |
| 578 | SSL *const ssl = hs->ssl; |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 579 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 580 | // Send a ServerHello. |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 581 | ScopedCBB cbb; |
| 582 | CBB body, extensions, session_id; |
| 583 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) || |
Steven Valdez | 64cc121 | 2017-12-04 11:15:37 -0500 | [diff] [blame] | 584 | !CBB_add_u16(&body, TLS1_2_VERSION) || |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 585 | !RAND_bytes(ssl->s3->server_random, sizeof(ssl->s3->server_random)) || |
| 586 | !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) || |
Steven Valdez | 64cc121 | 2017-12-04 11:15:37 -0500 | [diff] [blame] | 587 | !CBB_add_u8_length_prefixed(&body, &session_id) || |
| 588 | !CBB_add_bytes(&session_id, hs->session_id, hs->session_id_len) || |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 589 | !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) || |
Steven Valdez | 64cc121 | 2017-12-04 11:15:37 -0500 | [diff] [blame] | 590 | !CBB_add_u8(&body, 0) || |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 591 | !CBB_add_u16_length_prefixed(&body, &extensions) || |
David Benjamin | 8baf963 | 2016-11-17 17:11:16 +0900 | [diff] [blame] | 592 | !ssl_ext_pre_shared_key_add_serverhello(hs, &extensions) || |
Steven Valdez | 924a352 | 2017-03-02 16:05:03 -0500 | [diff] [blame] | 593 | !ssl_ext_key_share_add_serverhello(hs, &extensions) || |
Steven Valdez | 64cc121 | 2017-12-04 11:15:37 -0500 | [diff] [blame] | 594 | !ssl_ext_supported_versions_add_serverhello(hs, &extensions) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 595 | !ssl_add_message_cbb(ssl, cbb.get())) { |
| 596 | return ssl_hs_error; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 597 | } |
| 598 | |
Kris Kwiatkowski | b11902a | 2019-08-24 11:01:04 +0100 | [diff] [blame] | 599 | if (!ssl->s3->used_hello_retry_request && |
David Benjamin | 666d16e | 2017-10-06 18:45:16 -0400 | [diff] [blame] | 600 | !ssl->method->add_change_cipher_spec(ssl)) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 601 | return ssl_hs_error; |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 602 | } |
| 603 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 604 | // Derive and enable the handshake traffic secrets. |
Steven Valdez | 4cb8494 | 2016-12-16 11:29:28 -0500 | [diff] [blame] | 605 | if (!tls13_derive_handshake_secrets(hs) || |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 606 | !tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_seal, |
David Benjamin | 754d4c9 | 2020-02-11 16:27:21 -0500 | [diff] [blame] | 607 | hs->new_session.get(), |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 608 | hs->server_handshake_secret())) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 609 | return ssl_hs_error; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 610 | } |
| 611 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 612 | // Send EncryptedExtensions. |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 613 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 614 | SSL3_MT_ENCRYPTED_EXTENSIONS) || |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 615 | !ssl_add_serverhello_tlsext(hs, &body) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 616 | !ssl_add_message_cbb(ssl, cbb.get())) { |
| 617 | return ssl_hs_error; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 618 | } |
| 619 | |
David Benjamin | c3648fa | 2017-07-01 10:50:56 -0400 | [diff] [blame] | 620 | if (!ssl->s3->session_reused) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 621 | // Determine whether to request a client certificate. |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 622 | hs->cert_request = !!(hs->config->verify_mode & SSL_VERIFY_PEER); |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 623 | // Only request a certificate if Channel ID isn't negotiated. |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 624 | if ((hs->config->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) && |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 625 | ssl->s3->channel_id_valid) { |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 626 | hs->cert_request = false; |
David Benjamin | c3648fa | 2017-07-01 10:50:56 -0400 | [diff] [blame] | 627 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 628 | } |
| 629 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 630 | // Send a CertificateRequest, if necessary. |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 631 | if (hs->cert_request) { |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 632 | CBB cert_request_extensions, sigalg_contents, sigalgs_cbb; |
| 633 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
| 634 | SSL3_MT_CERTIFICATE_REQUEST) || |
| 635 | !CBB_add_u8(&body, 0 /* no certificate_request_context. */) || |
| 636 | !CBB_add_u16_length_prefixed(&body, &cert_request_extensions) || |
| 637 | !CBB_add_u16(&cert_request_extensions, |
| 638 | TLSEXT_TYPE_signature_algorithms) || |
| 639 | !CBB_add_u16_length_prefixed(&cert_request_extensions, |
| 640 | &sigalg_contents) || |
| 641 | !CBB_add_u16_length_prefixed(&sigalg_contents, &sigalgs_cbb) || |
David Benjamin | ebad508 | 2020-02-03 19:32:19 -0500 | [diff] [blame] | 642 | !tls12_add_verify_sigalgs(hs, &sigalgs_cbb)) { |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 643 | return ssl_hs_error; |
| 644 | } |
| 645 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 646 | if (ssl_has_client_CAs(hs->config)) { |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 647 | CBB ca_contents; |
| 648 | if (!CBB_add_u16(&cert_request_extensions, |
| 649 | TLSEXT_TYPE_certificate_authorities) || |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 650 | !CBB_add_u16_length_prefixed(&cert_request_extensions, |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 651 | &ca_contents) || |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 652 | !ssl_add_client_CA_list(hs, &ca_contents) || |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 653 | !CBB_flush(&cert_request_extensions)) { |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 654 | return ssl_hs_error; |
| 655 | } |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 656 | } |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 657 | |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 658 | if (!ssl_add_message_cbb(ssl, cbb.get())) { |
| 659 | return ssl_hs_error; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 663 | // Send the server Certificate message, if necessary. |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 664 | if (!ssl->s3->session_reused) { |
Christopher Patton | 9cde848 | 2018-07-17 11:36:36 -0700 | [diff] [blame] | 665 | if (!ssl_has_certificate(hs)) { |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 666 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 667 | return ssl_hs_error; |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 668 | } |
| 669 | |
David Benjamin | 0f24bed | 2017-01-12 19:46:50 -0500 | [diff] [blame] | 670 | if (!tls13_add_certificate(hs)) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 671 | return ssl_hs_error; |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 672 | } |
| 673 | |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 674 | hs->tls13_state = state13_send_server_certificate_verify; |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 675 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 676 | } |
| 677 | |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 678 | hs->tls13_state = state13_send_server_finished; |
David Benjamin | 25ac251 | 2017-01-12 19:31:28 -0500 | [diff] [blame] | 679 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 680 | } |
| 681 | |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 682 | static enum ssl_hs_wait_t do_send_server_certificate_verify(SSL_HANDSHAKE *hs) { |
| 683 | switch (tls13_add_certificate_verify(hs)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 684 | case ssl_private_key_success: |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 685 | hs->tls13_state = state13_send_server_finished; |
David Benjamin | 25ac251 | 2017-01-12 19:31:28 -0500 | [diff] [blame] | 686 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 687 | |
| 688 | case ssl_private_key_retry: |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 689 | hs->tls13_state = state13_send_server_certificate_verify; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 690 | return ssl_hs_private_key_operation; |
| 691 | |
| 692 | case ssl_private_key_failure: |
| 693 | return ssl_hs_error; |
| 694 | } |
| 695 | |
| 696 | assert(0); |
| 697 | return ssl_hs_error; |
| 698 | } |
| 699 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 700 | 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] | 701 | SSL *const ssl = hs->ssl; |
David Benjamin | 0f24bed | 2017-01-12 19:46:50 -0500 | [diff] [blame] | 702 | if (!tls13_add_finished(hs) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 703 | // Update the secret to the master secret and derive traffic keys. |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 704 | !tls13_advance_key_schedule( |
| 705 | hs, MakeConstSpan(kZeroes, hs->transcript.DigestLen())) || |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 706 | !tls13_derive_application_secrets(hs) || |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 707 | !tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_seal, |
David Benjamin | 754d4c9 | 2020-02-11 16:27:21 -0500 | [diff] [blame] | 708 | hs->new_session.get(), |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 709 | hs->server_traffic_secret_0())) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 710 | return ssl_hs_error; |
| 711 | } |
| 712 | |
Matthew Braithwaite | 093a823 | 2020-01-28 14:06:55 -0800 | [diff] [blame] | 713 | hs->tls13_state = state13_send_half_rtt_ticket; |
David Benjamin | 0c30649 | 2020-02-09 16:28:52 -0500 | [diff] [blame] | 714 | return hs->handback ? ssl_hs_handback : ssl_hs_ok; |
Matthew Braithwaite | 093a823 | 2020-01-28 14:06:55 -0800 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | static enum ssl_hs_wait_t do_send_half_rtt_ticket(SSL_HANDSHAKE *hs) { |
| 718 | SSL *const ssl = hs->ssl; |
| 719 | |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 720 | if (ssl->s3->early_data_accepted) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 721 | // If accepting 0-RTT, we send tickets half-RTT. This gets the tickets on |
| 722 | // the wire sooner and also avoids triggering a write on |SSL_read| when |
| 723 | // processing the client Finished. This requires computing the client |
David Benjamin | a130ce0 | 2018-08-14 22:26:39 -0500 | [diff] [blame] | 724 | // Finished early. See RFC 8446, section 4.6.1. |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 725 | static const uint8_t kEndOfEarlyData[4] = {SSL3_MT_END_OF_EARLY_DATA, 0, |
| 726 | 0, 0}; |
David Benjamin | d634357 | 2019-08-15 17:29:02 -0400 | [diff] [blame] | 727 | if (ssl->quic_method == nullptr && |
| 728 | !hs->transcript.Update(kEndOfEarlyData)) { |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 729 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 730 | return ssl_hs_error; |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 731 | } |
| 732 | |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 733 | size_t finished_len; |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 734 | if (!tls13_finished_mac(hs, hs->expected_client_finished().data(), |
| 735 | &finished_len, false /* client */)) { |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 736 | return ssl_hs_error; |
| 737 | } |
| 738 | |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 739 | if (finished_len != hs->expected_client_finished().size()) { |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 740 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 741 | return ssl_hs_error; |
| 742 | } |
| 743 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 744 | // Feed the predicted Finished into the transcript. This allows us to derive |
| 745 | // the resumption secret early and send half-RTT tickets. |
| 746 | // |
| 747 | // TODO(davidben): This will need to be updated for DTLS 1.3. |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 748 | assert(!SSL_is_dtls(hs->ssl)); |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 749 | assert(hs->expected_client_finished().size() <= 0xff); |
| 750 | uint8_t header[4] = { |
| 751 | SSL3_MT_FINISHED, 0, 0, |
| 752 | static_cast<uint8_t>(hs->expected_client_finished().size())}; |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 753 | bool unused_sent_tickets; |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 754 | if (!hs->transcript.Update(header) || |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 755 | !hs->transcript.Update(hs->expected_client_finished()) || |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 756 | !tls13_derive_resumption_secret(hs) || |
David Benjamin | 0cbb1af | 2018-07-17 21:26:05 -0400 | [diff] [blame] | 757 | !add_new_session_tickets(hs, &unused_sent_tickets)) { |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 758 | return ssl_hs_error; |
| 759 | } |
| 760 | } |
| 761 | |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 762 | hs->tls13_state = state13_read_second_client_flight; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 763 | return ssl_hs_flush; |
| 764 | } |
| 765 | |
| 766 | static enum ssl_hs_wait_t do_read_second_client_flight(SSL_HANDSHAKE *hs) { |
| 767 | SSL *const ssl = hs->ssl; |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 768 | if (ssl->s3->early_data_accepted) { |
David Benjamin | 1e85905 | 2020-02-09 16:04:58 -0500 | [diff] [blame] | 769 | 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] | 770 | hs->new_session.get(), |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 771 | hs->early_traffic_secret())) { |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 772 | return ssl_hs_error; |
| 773 | } |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 774 | hs->can_early_write = true; |
| 775 | hs->can_early_read = true; |
| 776 | hs->in_early_data = true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 777 | } |
David Benjamin | d634357 | 2019-08-15 17:29:02 -0400 | [diff] [blame] | 778 | |
| 779 | // QUIC doesn't use an EndOfEarlyData message (draft-ietf-quic-tls-22, |
| 780 | // section 8.3), so we switch to client_handshake_secret before the early |
| 781 | // return. |
| 782 | if (ssl->quic_method != nullptr) { |
| 783 | if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_open, |
David Benjamin | 754d4c9 | 2020-02-11 16:27:21 -0500 | [diff] [blame] | 784 | hs->new_session.get(), |
David Benjamin | d634357 | 2019-08-15 17:29:02 -0400 | [diff] [blame] | 785 | hs->client_handshake_secret())) { |
| 786 | return ssl_hs_error; |
| 787 | } |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 788 | hs->tls13_state = state13_read_client_certificate; |
David Benjamin | d634357 | 2019-08-15 17:29:02 -0400 | [diff] [blame] | 789 | return ssl->s3->early_data_accepted ? ssl_hs_early_return : ssl_hs_ok; |
| 790 | } |
| 791 | |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 792 | hs->tls13_state = state13_process_end_of_early_data; |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 793 | return ssl->s3->early_data_accepted ? ssl_hs_read_end_of_early_data |
| 794 | : ssl_hs_ok; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | 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] | 798 | SSL *const ssl = hs->ssl; |
David Benjamin | f350351 | 2019-08-20 15:55:24 -0400 | [diff] [blame] | 799 | // If early data was not accepted, the EndOfEarlyData will be in the discarded |
| 800 | // early data. |
| 801 | if (hs->ssl->s3->early_data_accepted) { |
| 802 | SSLMessage msg; |
| 803 | if (!ssl->method->get_message(ssl, &msg)) { |
| 804 | return ssl_hs_read_message; |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 805 | } |
David Benjamin | f350351 | 2019-08-20 15:55:24 -0400 | [diff] [blame] | 806 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_END_OF_EARLY_DATA)) { |
| 807 | return ssl_hs_error; |
| 808 | } |
| 809 | if (CBS_len(&msg.body) != 0) { |
| 810 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 811 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 812 | return ssl_hs_error; |
| 813 | } |
| 814 | ssl->method->next_message(ssl); |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 815 | } |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 816 | if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_open, |
David Benjamin | 754d4c9 | 2020-02-11 16:27:21 -0500 | [diff] [blame] | 817 | hs->new_session.get(), |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 818 | hs->client_handshake_secret())) { |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 819 | return ssl_hs_error; |
| 820 | } |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 821 | hs->tls13_state = state13_read_client_certificate; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 822 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 823 | } |
| 824 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 825 | 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] | 826 | SSL *const ssl = hs->ssl; |
| 827 | if (!hs->cert_request) { |
David Benjamin | f350351 | 2019-08-20 15:55:24 -0400 | [diff] [blame] | 828 | if (!ssl->s3->session_reused) { |
| 829 | // OpenSSL returns X509_V_OK when no certificates are requested. This is |
| 830 | // classed by them as a bug, but it's assumed by at least NGINX. (Only do |
| 831 | // this in full handshakes as resumptions should carry over the previous |
| 832 | // |verify_result|, though this is a no-op because servers do not |
| 833 | // implement the client's odd soft-fail mode.) |
| 834 | hs->new_session->verify_result = X509_V_OK; |
| 835 | } |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 836 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 837 | // Skip this state. |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 838 | hs->tls13_state = state13_read_channel_id; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 839 | return ssl_hs_ok; |
| 840 | } |
| 841 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 842 | const bool allow_anonymous = |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 843 | (hs->config->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) == 0; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 844 | SSLMessage msg; |
| 845 | if (!ssl->method->get_message(ssl, &msg)) { |
| 846 | return ssl_hs_read_message; |
| 847 | } |
| 848 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) || |
| 849 | !tls13_process_certificate(hs, msg, allow_anonymous) || |
| 850 | !ssl_hash_message(hs, msg)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 851 | return ssl_hs_error; |
| 852 | } |
| 853 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 854 | ssl->method->next_message(ssl); |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 855 | hs->tls13_state = state13_read_client_certificate_verify; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 856 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 857 | } |
| 858 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 859 | static enum ssl_hs_wait_t do_read_client_certificate_verify( |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 860 | SSL_HANDSHAKE *hs) { |
| 861 | SSL *const ssl = hs->ssl; |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 862 | if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 863 | // Skip this state. |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 864 | hs->tls13_state = state13_read_channel_id; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 865 | return ssl_hs_ok; |
| 866 | } |
| 867 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 868 | SSLMessage msg; |
| 869 | if (!ssl->method->get_message(ssl, &msg)) { |
| 870 | return ssl_hs_read_message; |
| 871 | } |
| 872 | |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 873 | switch (ssl_verify_peer_cert(hs)) { |
| 874 | case ssl_verify_ok: |
| 875 | break; |
| 876 | case ssl_verify_invalid: |
| 877 | return ssl_hs_error; |
| 878 | case ssl_verify_retry: |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 879 | hs->tls13_state = state13_read_client_certificate_verify; |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 880 | return ssl_hs_certificate_verify; |
| 881 | } |
| 882 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 883 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY) || |
| 884 | !tls13_process_certificate_verify(hs, msg) || |
| 885 | !ssl_hash_message(hs, msg)) { |
David Benjamin | 6929f27 | 2016-11-16 19:10:08 +0900 | [diff] [blame] | 886 | return ssl_hs_error; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 887 | } |
| 888 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 889 | ssl->method->next_message(ssl); |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 890 | hs->tls13_state = state13_read_channel_id; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 891 | return ssl_hs_ok; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 892 | } |
| 893 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 894 | 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] | 895 | SSL *const ssl = hs->ssl; |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 896 | if (!ssl->s3->channel_id_valid) { |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 897 | hs->tls13_state = state13_read_client_finished; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 898 | return ssl_hs_ok; |
| 899 | } |
| 900 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 901 | SSLMessage msg; |
| 902 | if (!ssl->method->get_message(ssl, &msg)) { |
| 903 | return ssl_hs_read_message; |
| 904 | } |
| 905 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) || |
| 906 | !tls1_verify_channel_id(hs, msg) || |
| 907 | !ssl_hash_message(hs, msg)) { |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 908 | return ssl_hs_error; |
| 909 | } |
| 910 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 911 | ssl->method->next_message(ssl); |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 912 | hs->tls13_state = state13_read_client_finished; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 913 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 914 | } |
| 915 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 916 | 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] | 917 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 918 | SSLMessage msg; |
| 919 | if (!ssl->method->get_message(ssl, &msg)) { |
| 920 | return ssl_hs_read_message; |
| 921 | } |
| 922 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 923 | // If early data was accepted, we've already computed the client Finished |
| 924 | // and derived the resumption secret. |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 925 | !tls13_process_finished(hs, msg, ssl->s3->early_data_accepted) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 926 | // evp_aead_seal keys have already been switched. |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 927 | !tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_open, |
David Benjamin | 754d4c9 | 2020-02-11 16:27:21 -0500 | [diff] [blame] | 928 | hs->new_session.get(), |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 929 | hs->client_traffic_secret_0())) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 930 | return ssl_hs_error; |
| 931 | } |
| 932 | |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 933 | if (!ssl->s3->early_data_accepted) { |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 934 | if (!ssl_hash_message(hs, msg) || |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 935 | !tls13_derive_resumption_secret(hs)) { |
| 936 | return ssl_hs_error; |
| 937 | } |
| 938 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 939 | // 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] | 940 | hs->tls13_state = state13_send_new_session_ticket; |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 941 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 942 | // We already sent half-RTT tickets. |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 943 | hs->tls13_state = state13_done; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 944 | } |
| 945 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 946 | ssl->method->next_message(ssl); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 947 | return ssl_hs_ok; |
| 948 | } |
| 949 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 950 | 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] | 951 | bool sent_tickets; |
| 952 | if (!add_new_session_tickets(hs, &sent_tickets)) { |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 953 | return ssl_hs_error; |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 954 | } |
| 955 | |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 956 | hs->tls13_state = state13_done; |
Steven Valdez | 777a239 | 2019-02-21 11:30:47 -0500 | [diff] [blame] | 957 | // In TLS 1.3, the NewSessionTicket isn't flushed until the server performs a |
| 958 | // write, to prevent a non-reading client from causing the server to hang in |
| 959 | // the case of a small server write buffer. Consumers which don't write data |
| 960 | // to the client will need to do a zero-byte write if they wish to flush the |
| 961 | // tickets. |
Goutam Tamvada | 49de1fc | 2019-10-04 16:58:29 -0400 | [diff] [blame] | 962 | if (hs->ssl->quic_method != nullptr && sent_tickets) { |
Steven Valdez | 777a239 | 2019-02-21 11:30:47 -0500 | [diff] [blame] | 963 | return ssl_hs_flush; |
| 964 | } |
| 965 | return ssl_hs_ok; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 966 | } |
| 967 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 968 | enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs) { |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 969 | while (hs->tls13_state != state13_done) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 970 | enum ssl_hs_wait_t ret = ssl_hs_error; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 971 | enum tls13_server_hs_state_t state = |
| 972 | static_cast<enum tls13_server_hs_state_t>(hs->tls13_state); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 973 | switch (state) { |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 974 | case state13_select_parameters: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 975 | ret = do_select_parameters(hs); |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 976 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 977 | case state13_select_session: |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 978 | ret = do_select_session(hs); |
| 979 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 980 | case state13_send_hello_retry_request: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 981 | ret = do_send_hello_retry_request(hs); |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 982 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 983 | case state13_read_second_client_hello: |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 984 | ret = do_read_second_client_hello(hs); |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 985 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 986 | case state13_send_server_hello: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 987 | ret = do_send_server_hello(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 988 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 989 | case state13_send_server_certificate_verify: |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 990 | ret = do_send_server_certificate_verify(hs); |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 991 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 992 | case state13_send_server_finished: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 993 | ret = do_send_server_finished(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 994 | break; |
Matthew Braithwaite | 093a823 | 2020-01-28 14:06:55 -0800 | [diff] [blame] | 995 | case state13_send_half_rtt_ticket: |
| 996 | ret = do_send_half_rtt_ticket(hs); |
| 997 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 998 | case state13_read_second_client_flight: |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 999 | ret = do_read_second_client_flight(hs); |
| 1000 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1001 | case state13_process_end_of_early_data: |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 1002 | ret = do_process_end_of_early_data(hs); |
| 1003 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1004 | case state13_read_client_certificate: |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1005 | ret = do_read_client_certificate(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1006 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1007 | case state13_read_client_certificate_verify: |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1008 | ret = do_read_client_certificate_verify(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1009 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1010 | case state13_read_channel_id: |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1011 | ret = do_read_channel_id(hs); |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 1012 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1013 | case state13_read_client_finished: |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1014 | ret = do_read_client_finished(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1015 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1016 | case state13_send_new_session_ticket: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1017 | ret = do_send_new_session_ticket(hs); |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 1018 | break; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1019 | case state13_done: |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1020 | ret = ssl_hs_ok; |
| 1021 | break; |
| 1022 | } |
| 1023 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1024 | if (hs->tls13_state != state) { |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1025 | ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1); |
| 1026 | } |
| 1027 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1028 | if (ret != ssl_hs_ok) { |
| 1029 | return ret; |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | return ssl_hs_ok; |
| 1034 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1035 | |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1036 | const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs) { |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1037 | enum tls13_server_hs_state_t state = |
| 1038 | static_cast<enum tls13_server_hs_state_t>(hs->tls13_state); |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1039 | switch (state) { |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1040 | case state13_select_parameters: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1041 | return "TLS 1.3 server select_parameters"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1042 | case state13_select_session: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1043 | return "TLS 1.3 server select_session"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1044 | case state13_send_hello_retry_request: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1045 | return "TLS 1.3 server send_hello_retry_request"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1046 | case state13_read_second_client_hello: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1047 | return "TLS 1.3 server read_second_client_hello"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1048 | case state13_send_server_hello: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1049 | return "TLS 1.3 server send_server_hello"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1050 | case state13_send_server_certificate_verify: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1051 | return "TLS 1.3 server send_server_certificate_verify"; |
Matthew Braithwaite | 093a823 | 2020-01-28 14:06:55 -0800 | [diff] [blame] | 1052 | case state13_send_half_rtt_ticket: |
| 1053 | return "TLS 1.3 server send_half_rtt_ticket"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1054 | case state13_send_server_finished: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1055 | return "TLS 1.3 server send_server_finished"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1056 | case state13_read_second_client_flight: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1057 | return "TLS 1.3 server read_second_client_flight"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1058 | case state13_process_end_of_early_data: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1059 | return "TLS 1.3 server process_end_of_early_data"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1060 | case state13_read_client_certificate: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1061 | return "TLS 1.3 server read_client_certificate"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1062 | case state13_read_client_certificate_verify: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1063 | return "TLS 1.3 server read_client_certificate_verify"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1064 | case state13_read_channel_id: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1065 | return "TLS 1.3 server read_channel_id"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1066 | case state13_read_client_finished: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1067 | return "TLS 1.3 server read_client_finished"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1068 | case state13_send_new_session_ticket: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1069 | return "TLS 1.3 server send_new_session_ticket"; |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1070 | case state13_done: |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 1071 | return "TLS 1.3 server done"; |
| 1072 | } |
| 1073 | |
| 1074 | return "TLS 1.3 server unknown"; |
| 1075 | } |
| 1076 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 1077 | BSSL_NAMESPACE_END |