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> |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 18 | #include <limits.h> |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 19 | #include <string.h> |
| 20 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 21 | #include <utility> |
| 22 | |
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/stack.h> |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 28 | |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 29 | #include "../crypto/internal.h" |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 30 | #include "internal.h" |
| 31 | |
| 32 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 33 | namespace bssl { |
| 34 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 35 | enum client_hs_state_t { |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 36 | state_read_hello_retry_request = 0, |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 37 | state_send_second_client_hello, |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 38 | state_read_server_hello, |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 39 | state_process_change_cipher_spec, |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 40 | state_read_encrypted_extensions, |
| 41 | state_read_certificate_request, |
| 42 | state_read_server_certificate, |
| 43 | state_read_server_certificate_verify, |
| 44 | state_read_server_finished, |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 45 | state_send_end_of_early_data, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 46 | state_send_client_certificate, |
| 47 | state_send_client_certificate_verify, |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 48 | state_complete_second_flight, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 49 | state_done, |
| 50 | }; |
| 51 | |
David Benjamin | 8f820b4 | 2016-11-30 11:24:40 -0500 | [diff] [blame] | 52 | static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0}; |
| 53 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 54 | static enum ssl_hs_wait_t do_read_hello_retry_request(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 55 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 56 | SSLMessage msg; |
| 57 | if (!ssl->method->get_message(ssl, &msg)) { |
| 58 | return ssl_hs_read_message; |
| 59 | } |
| 60 | if (msg.type != SSL3_MT_HELLO_RETRY_REQUEST) { |
| 61 | hs->tls13_state = state_read_server_hello; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 62 | return ssl_hs_ok; |
| 63 | } |
| 64 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 65 | CBS body = msg.body, extensions; |
Steven Valdez | 038da9b | 2017-07-10 12:57:25 -0400 | [diff] [blame] | 66 | uint16_t server_version; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 67 | if (!CBS_get_u16(&body, &server_version) || |
| 68 | !CBS_get_u16_length_prefixed(&body, &extensions) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 69 | // HelloRetryRequest may not be empty. |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 70 | CBS_len(&extensions) == 0 || |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 71 | CBS_len(&body) != 0) { |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 72 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 73 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 74 | return ssl_hs_error; |
| 75 | } |
| 76 | |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 77 | bool have_cookie, have_key_share; |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 78 | CBS cookie, key_share; |
| 79 | const SSL_EXTENSION_TYPE ext_types[] = { |
| 80 | {TLSEXT_TYPE_key_share, &have_key_share, &key_share}, |
| 81 | {TLSEXT_TYPE_cookie, &have_cookie, &cookie}, |
| 82 | }; |
| 83 | |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 84 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 85 | if (!ssl_parse_extensions(&extensions, &alert, ext_types, |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 86 | OPENSSL_ARRAY_SIZE(ext_types), |
| 87 | 0 /* reject unknown */)) { |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 88 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 89 | return ssl_hs_error; |
| 90 | } |
| 91 | |
| 92 | if (have_cookie) { |
| 93 | CBS cookie_value; |
| 94 | if (!CBS_get_u16_length_prefixed(&cookie, &cookie_value) || |
| 95 | CBS_len(&cookie_value) == 0 || |
| 96 | CBS_len(&cookie) != 0) { |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 97 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 98 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 99 | return ssl_hs_error; |
| 100 | } |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 101 | |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 102 | if (!CBS_stow(&cookie_value, &hs->cookie, &hs->cookie_len)) { |
| 103 | return ssl_hs_error; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 107 | if (have_key_share) { |
| 108 | uint16_t group_id; |
| 109 | if (!CBS_get_u16(&key_share, &group_id) || CBS_len(&key_share) != 0) { |
| 110 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 111 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 112 | return ssl_hs_error; |
| 113 | } |
| 114 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 115 | // The group must be supported. |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 116 | const uint16_t *groups; |
| 117 | size_t groups_len; |
| 118 | tls1_get_grouplist(ssl, &groups, &groups_len); |
| 119 | int found = 0; |
| 120 | for (size_t i = 0; i < groups_len; i++) { |
| 121 | if (groups[i] == group_id) { |
| 122 | found = 1; |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (!found) { |
| 128 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 129 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
| 130 | return ssl_hs_error; |
| 131 | } |
| 132 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 133 | // Check that the HelloRetryRequest does not request the key share that |
| 134 | // was provided in the initial ClientHello. |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 135 | if (hs->key_share->GroupID() == group_id) { |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 136 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 137 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
| 138 | return ssl_hs_error; |
| 139 | } |
| 140 | |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 141 | hs->key_share.reset(); |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 142 | hs->retry_group = group_id; |
| 143 | } |
| 144 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 145 | if (!ssl_hash_message(hs, msg)) { |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 146 | return ssl_hs_error; |
| 147 | } |
| 148 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 149 | ssl->method->next_message(ssl); |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 150 | hs->received_hello_retry_request = true; |
David Benjamin | 3977f30 | 2016-12-11 13:30:41 -0500 | [diff] [blame] | 151 | hs->tls13_state = state_send_second_client_hello; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 152 | // 0-RTT is rejected if we receive a HelloRetryRequest. |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 153 | if (hs->in_early_data) { |
| 154 | return ssl_hs_early_data_rejected; |
| 155 | } |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 156 | return ssl_hs_ok; |
| 157 | } |
| 158 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 159 | static enum ssl_hs_wait_t do_send_second_client_hello(SSL_HANDSHAKE *hs) { |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 160 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 161 | // Restore the null cipher. We may have switched due to 0-RTT. |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 162 | bssl::UniquePtr<SSLAEADContext> null_ctx = |
| 163 | SSLAEADContext::CreateNullCipher(SSL_is_dtls(ssl)); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 164 | if (!null_ctx || |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 165 | !ssl->method->set_write_state(ssl, std::move(null_ctx))) { |
| 166 | return ssl_hs_error; |
| 167 | } |
| 168 | |
| 169 | ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version); |
| 170 | |
| 171 | if (!ssl_write_client_hello(hs)) { |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 172 | return ssl_hs_error; |
| 173 | } |
| 174 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 175 | hs->tls13_state = state_read_server_hello; |
| 176 | return ssl_hs_flush; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 177 | } |
| 178 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 179 | static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 180 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 181 | SSLMessage msg; |
| 182 | if (!ssl->method->get_message(ssl, &msg)) { |
| 183 | return ssl_hs_read_message; |
| 184 | } |
| 185 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 186 | return ssl_hs_error; |
| 187 | } |
| 188 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 189 | CBS body = msg.body, server_random, session_id, extensions; |
Steven Valdez | 038da9b | 2017-07-10 12:57:25 -0400 | [diff] [blame] | 190 | uint16_t server_version; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 191 | uint16_t cipher_suite; |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 192 | uint8_t compression_method; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 193 | if (!CBS_get_u16(&body, &server_version) || |
| 194 | !CBS_get_bytes(&body, &server_random, SSL3_RANDOM_SIZE) || |
Steven Valdez | 1682126 | 2017-09-08 17:03:42 -0400 | [diff] [blame] | 195 | (ssl_is_resumption_experiment(ssl->version) && |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 196 | !CBS_get_u8_length_prefixed(&body, &session_id)) || |
| 197 | !CBS_get_u16(&body, &cipher_suite) || |
Steven Valdez | 1682126 | 2017-09-08 17:03:42 -0400 | [diff] [blame] | 198 | (ssl_is_resumption_experiment(ssl->version) && |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 199 | (!CBS_get_u8(&body, &compression_method) || compression_method != 0)) || |
| 200 | !CBS_get_u16_length_prefixed(&body, &extensions) || |
| 201 | CBS_len(&body) != 0) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 202 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 203 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 204 | return ssl_hs_error; |
| 205 | } |
| 206 | |
Steven Valdez | 1682126 | 2017-09-08 17:03:42 -0400 | [diff] [blame] | 207 | uint16_t expected_version = ssl_is_resumption_experiment(ssl->version) |
| 208 | ? TLS1_2_VERSION |
| 209 | : ssl->version; |
Steven Valdez | 038da9b | 2017-07-10 12:57:25 -0400 | [diff] [blame] | 210 | if (server_version != expected_version) { |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 211 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 212 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 213 | return ssl_hs_error; |
| 214 | } |
| 215 | |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 216 | assert(ssl->s3->have_version); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 217 | OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random), |
| 218 | SSL3_RANDOM_SIZE); |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 219 | |
| 220 | const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite); |
| 221 | if (cipher == NULL) { |
| 222 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED); |
| 223 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 224 | return ssl_hs_error; |
| 225 | } |
| 226 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 227 | // Check if the cipher is a TLS 1.3 cipher. |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 228 | if (SSL_CIPHER_get_min_version(cipher) > ssl3_protocol_version(ssl) || |
| 229 | SSL_CIPHER_get_max_version(cipher) < ssl3_protocol_version(ssl)) { |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 230 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
| 231 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 232 | return ssl_hs_error; |
| 233 | } |
| 234 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 235 | // Parse out the extensions. |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 236 | bool have_key_share = false, have_pre_shared_key = false, |
| 237 | have_supported_versions = false; |
Steven Valdez | 038da9b | 2017-07-10 12:57:25 -0400 | [diff] [blame] | 238 | CBS key_share, pre_shared_key, supported_versions; |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 239 | const SSL_EXTENSION_TYPE ext_types[] = { |
| 240 | {TLSEXT_TYPE_key_share, &have_key_share, &key_share}, |
| 241 | {TLSEXT_TYPE_pre_shared_key, &have_pre_shared_key, &pre_shared_key}, |
Steven Valdez | 038da9b | 2017-07-10 12:57:25 -0400 | [diff] [blame] | 242 | {TLSEXT_TYPE_supported_versions, &have_supported_versions, |
| 243 | &supported_versions}, |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 244 | }; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 245 | |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 246 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 247 | if (!ssl_parse_extensions(&extensions, &alert, ext_types, |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 248 | OPENSSL_ARRAY_SIZE(ext_types), |
| 249 | 0 /* reject unknown */)) { |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 250 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 251 | return ssl_hs_error; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 252 | } |
| 253 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 254 | // supported_versions is parsed in handshake_client to select the experimental |
| 255 | // TLS 1.3 version. |
Steven Valdez | 1682126 | 2017-09-08 17:03:42 -0400 | [diff] [blame] | 256 | if (have_supported_versions && !ssl_is_resumption_experiment(ssl->version)) { |
Steven Valdez | 038da9b | 2017-07-10 12:57:25 -0400 | [diff] [blame] | 257 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 258 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION); |
| 259 | return ssl_hs_error; |
| 260 | } |
| 261 | |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 262 | alert = SSL_AD_DECODE_ERROR; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 263 | if (have_pre_shared_key) { |
| 264 | if (ssl->session == NULL) { |
| 265 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 266 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION); |
| 267 | return ssl_hs_error; |
| 268 | } |
| 269 | |
David Benjamin | 8baf963 | 2016-11-17 17:11:16 +0900 | [diff] [blame] | 270 | if (!ssl_ext_pre_shared_key_parse_serverhello(hs, &alert, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 271 | &pre_shared_key)) { |
| 272 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 273 | return ssl_hs_error; |
| 274 | } |
| 275 | |
| 276 | if (ssl->session->ssl_version != ssl->version) { |
| 277 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED); |
| 278 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 279 | return ssl_hs_error; |
| 280 | } |
| 281 | |
David Benjamin | e1cc35e | 2016-11-16 16:25:58 +0900 | [diff] [blame] | 282 | if (ssl->session->cipher->algorithm_prf != cipher->algorithm_prf) { |
| 283 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_PRF_HASH_MISMATCH); |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 284 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 285 | return ssl_hs_error; |
| 286 | } |
| 287 | |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 288 | if (!ssl_session_is_context_valid(ssl, ssl->session)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 289 | // This is actually a client application bug. |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 290 | OPENSSL_PUT_ERROR(SSL, |
| 291 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
| 292 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 293 | return ssl_hs_error; |
| 294 | } |
| 295 | |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 296 | ssl->s3->session_reused = true; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 297 | // Only authentication information carries over in TLS 1.3. |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 298 | hs->new_session = SSL_SESSION_dup(ssl->session, SSL_SESSION_DUP_AUTH_ONLY); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 299 | if (!hs->new_session) { |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 300 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 301 | return ssl_hs_error; |
| 302 | } |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 303 | ssl_set_session(ssl, NULL); |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 304 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 305 | // Resumption incorporates fresh key material, so refresh the timeout. |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 306 | ssl_session_renew_timeout(ssl, hs->new_session.get(), |
David Benjamin | be49706 | 2017-03-10 16:08:36 -0500 | [diff] [blame] | 307 | ssl->session_ctx->session_psk_dhe_timeout); |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 308 | } else if (!ssl_get_new_session(hs, 0)) { |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 309 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 310 | return ssl_hs_error; |
| 311 | } |
| 312 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 313 | hs->new_session->cipher = cipher; |
| 314 | hs->new_cipher = cipher; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 315 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 316 | // The PRF hash is now known. Set up the key schedule. |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 317 | if (!tls13_init_key_schedule(hs)) { |
David Benjamin | 8f820b4 | 2016-11-30 11:24:40 -0500 | [diff] [blame] | 318 | return ssl_hs_error; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 319 | } |
| 320 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 321 | // Incorporate the PSK into the running secret. |
David Benjamin | 8f820b4 | 2016-11-30 11:24:40 -0500 | [diff] [blame] | 322 | if (ssl->s3->session_reused) { |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 323 | if (!tls13_advance_key_schedule(hs, hs->new_session->master_key, |
| 324 | hs->new_session->master_key_length)) { |
David Benjamin | 8f820b4 | 2016-11-30 11:24:40 -0500 | [diff] [blame] | 325 | return ssl_hs_error; |
| 326 | } |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 327 | } else if (!tls13_advance_key_schedule(hs, kZeroes, hs->hash_len)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 328 | return ssl_hs_error; |
| 329 | } |
| 330 | |
David Benjamin | db5bd72 | 2016-12-08 18:21:27 -0500 | [diff] [blame] | 331 | if (!have_key_share) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 332 | // We do not support psk_ke and thus always require a key share. |
David Benjamin | db5bd72 | 2016-12-08 18:21:27 -0500 | [diff] [blame] | 333 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE); |
| 334 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION); |
| 335 | return ssl_hs_error; |
| 336 | } |
| 337 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 338 | // Resolve ECDHE and incorporate it into the secret. |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 339 | uint8_t *dhe_secret; |
| 340 | size_t dhe_secret_len; |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 341 | alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8baf963 | 2016-11-17 17:11:16 +0900 | [diff] [blame] | 342 | if (!ssl_ext_key_share_parse_serverhello(hs, &dhe_secret, &dhe_secret_len, |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 343 | &alert, &key_share)) { |
| 344 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 345 | return ssl_hs_error; |
| 346 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 347 | |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 348 | if (!tls13_advance_key_schedule(hs, dhe_secret, dhe_secret_len)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 349 | OPENSSL_free(dhe_secret); |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 350 | return ssl_hs_error; |
| 351 | } |
| 352 | OPENSSL_free(dhe_secret); |
| 353 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 354 | if (!ssl_hash_message(hs, msg) || |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 355 | !tls13_derive_handshake_secrets(hs)) { |
| 356 | return ssl_hs_error; |
| 357 | } |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 358 | |
| 359 | ssl->method->next_message(ssl); |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 360 | hs->tls13_state = state_process_change_cipher_spec; |
Steven Valdez | 1682126 | 2017-09-08 17:03:42 -0400 | [diff] [blame] | 361 | return ssl_is_resumption_experiment(ssl->version) |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 362 | ? ssl_hs_read_change_cipher_spec |
| 363 | : ssl_hs_ok; |
| 364 | } |
| 365 | |
| 366 | static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) { |
| 367 | SSL *const ssl = hs->ssl; |
| 368 | if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->server_handshake_secret, |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 369 | hs->hash_len)) { |
| 370 | return ssl_hs_error; |
| 371 | } |
| 372 | |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 373 | if (!hs->early_data_offered) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 374 | // If not sending early data, set client traffic keys now so that alerts are |
| 375 | // encrypted. |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 376 | if ((ssl_is_resumption_client_ccs_experiment(ssl->version) && |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 377 | !ssl3_add_change_cipher_spec(ssl)) || |
| 378 | !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_handshake_secret, |
| 379 | hs->hash_len)) { |
| 380 | return ssl_hs_error; |
| 381 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 382 | } |
| 383 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 384 | hs->tls13_state = state_read_encrypted_extensions; |
| 385 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 386 | } |
| 387 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 388 | static enum ssl_hs_wait_t do_read_encrypted_extensions(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 389 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 390 | SSLMessage msg; |
| 391 | if (!ssl->method->get_message(ssl, &msg)) { |
| 392 | return ssl_hs_read_message; |
| 393 | } |
| 394 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_ENCRYPTED_EXTENSIONS)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 395 | return ssl_hs_error; |
| 396 | } |
| 397 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 398 | CBS body = msg.body; |
| 399 | if (!ssl_parse_serverhello_tlsext(hs, &body)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 400 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 401 | return ssl_hs_error; |
| 402 | } |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 403 | if (CBS_len(&body) != 0) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 404 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 405 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 406 | return ssl_hs_error; |
| 407 | } |
| 408 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 409 | // Store the negotiated ALPN in the session. |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 410 | if (ssl->s3->alpn_selected != NULL) { |
David Benjamin | d304a2f | 2017-07-12 23:00:28 -0400 | [diff] [blame] | 411 | hs->new_session->early_alpn = (uint8_t *)BUF_memdup( |
| 412 | ssl->s3->alpn_selected, ssl->s3->alpn_selected_len); |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 413 | if (hs->new_session->early_alpn == NULL) { |
| 414 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 415 | return ssl_hs_error; |
| 416 | } |
| 417 | hs->new_session->early_alpn_len = ssl->s3->alpn_selected_len; |
| 418 | } |
| 419 | |
| 420 | if (ssl->early_data_accepted) { |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 421 | if (hs->early_session->cipher != hs->new_session->cipher || |
| 422 | hs->early_session->early_alpn_len != ssl->s3->alpn_selected_len || |
| 423 | OPENSSL_memcmp(hs->early_session->early_alpn, ssl->s3->alpn_selected, |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 424 | ssl->s3->alpn_selected_len) != 0) { |
| 425 | OPENSSL_PUT_ERROR(SSL, SSL_R_ALPN_MISMATCH_ON_EARLY_DATA); |
| 426 | return ssl_hs_error; |
| 427 | } |
Steven Valdez | f4ecc84 | 2017-08-10 14:02:56 -0400 | [diff] [blame] | 428 | if (ssl->s3->tlsext_channel_id_valid || hs->received_custom_extension) { |
| 429 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION_ON_EARLY_DATA); |
Steven Valdez | 2a07072 | 2017-03-25 20:54:16 -0500 | [diff] [blame] | 430 | return ssl_hs_error; |
| 431 | } |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 432 | } |
| 433 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 434 | if (!ssl_hash_message(hs, msg)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 435 | return ssl_hs_error; |
| 436 | } |
| 437 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 438 | ssl->method->next_message(ssl); |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 439 | hs->tls13_state = state_read_certificate_request; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 440 | if (hs->in_early_data && !ssl->early_data_accepted) { |
| 441 | return ssl_hs_early_data_rejected; |
| 442 | } |
| 443 | return ssl_hs_ok; |
| 444 | } |
| 445 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 446 | static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 447 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 448 | // CertificateRequest may only be sent in non-resumption handshakes. |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 449 | if (ssl->s3->session_reused) { |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 450 | hs->tls13_state = state_read_server_finished; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 451 | return ssl_hs_ok; |
| 452 | } |
| 453 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 454 | SSLMessage msg; |
| 455 | if (!ssl->method->get_message(ssl, &msg)) { |
| 456 | return ssl_hs_read_message; |
| 457 | } |
| 458 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 459 | // CertificateRequest is optional. |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 460 | if (msg.type != SSL3_MT_CERTIFICATE_REQUEST) { |
| 461 | hs->tls13_state = state_read_server_certificate; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 462 | return ssl_hs_ok; |
| 463 | } |
| 464 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 465 | CBS body = msg.body, context, supported_signature_algorithms; |
| 466 | if (!CBS_get_u8_length_prefixed(&body, &context) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 467 | // The request context is always empty during the handshake. |
David Benjamin | 8a8349b | 2016-08-18 02:32:23 -0400 | [diff] [blame] | 468 | CBS_len(&context) != 0 || |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 469 | !CBS_get_u16_length_prefixed(&body, &supported_signature_algorithms) || |
David Benjamin | 4890165 | 2016-08-01 12:12:47 -0400 | [diff] [blame] | 470 | CBS_len(&supported_signature_algorithms) == 0 || |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 471 | !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 472 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 473 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 474 | return ssl_hs_error; |
| 475 | } |
| 476 | |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 477 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 478 | UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names = |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 479 | ssl_parse_client_CA_list(ssl, &alert, &body); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 480 | if (!ca_names) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 481 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 482 | return ssl_hs_error; |
| 483 | } |
| 484 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 485 | // Ignore extensions. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 486 | CBS extensions; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 487 | if (!CBS_get_u16_length_prefixed(&body, &extensions) || |
| 488 | CBS_len(&body) != 0) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 489 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 490 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 491 | return ssl_hs_error; |
| 492 | } |
| 493 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 494 | hs->cert_request = true; |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 495 | hs->ca_names = std::move(ca_names); |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 496 | ssl->ctx->x509_method->hs_flush_cached_ca_names(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 497 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 498 | if (!ssl_hash_message(hs, msg)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 499 | return ssl_hs_error; |
| 500 | } |
| 501 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 502 | ssl->method->next_message(ssl); |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 503 | hs->tls13_state = state_read_server_certificate; |
| 504 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 505 | } |
| 506 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 507 | static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 508 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 509 | SSLMessage msg; |
| 510 | if (!ssl->method->get_message(ssl, &msg)) { |
| 511 | return ssl_hs_read_message; |
| 512 | } |
| 513 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) || |
| 514 | !tls13_process_certificate(hs, msg, 0 /* certificate required */) || |
| 515 | !ssl_hash_message(hs, msg)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 516 | return ssl_hs_error; |
| 517 | } |
| 518 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 519 | ssl->method->next_message(ssl); |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 520 | hs->tls13_state = state_read_server_certificate_verify; |
| 521 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 522 | } |
| 523 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 524 | static enum ssl_hs_wait_t do_read_server_certificate_verify( |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 525 | SSL_HANDSHAKE *hs) { |
| 526 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 527 | SSLMessage msg; |
| 528 | if (!ssl->method->get_message(ssl, &msg)) { |
| 529 | return ssl_hs_read_message; |
| 530 | } |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 531 | switch (ssl_verify_peer_cert(hs)) { |
| 532 | case ssl_verify_ok: |
| 533 | break; |
| 534 | case ssl_verify_invalid: |
| 535 | return ssl_hs_error; |
| 536 | case ssl_verify_retry: |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 537 | hs->tls13_state = state_read_server_certificate_verify; |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 538 | return ssl_hs_certificate_verify; |
| 539 | } |
| 540 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 541 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY) || |
| 542 | !tls13_process_certificate_verify(hs, msg) || |
| 543 | !ssl_hash_message(hs, msg)) { |
David Benjamin | 6929f27 | 2016-11-16 19:10:08 +0900 | [diff] [blame] | 544 | return ssl_hs_error; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 545 | } |
| 546 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 547 | ssl->method->next_message(ssl); |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 548 | hs->tls13_state = state_read_server_finished; |
| 549 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 550 | } |
| 551 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 552 | static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 553 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 554 | SSLMessage msg; |
| 555 | if (!ssl->method->get_message(ssl, &msg)) { |
| 556 | return ssl_hs_read_message; |
| 557 | } |
| 558 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED) || |
| 559 | !tls13_process_finished(hs, msg, 0 /* don't use saved value */) || |
| 560 | !ssl_hash_message(hs, msg) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 561 | // Update the secret to the master secret and derive traffic keys. |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 562 | !tls13_advance_key_schedule(hs, kZeroes, hs->hash_len) || |
| 563 | !tls13_derive_application_secrets(hs)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 564 | return ssl_hs_error; |
| 565 | } |
| 566 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 567 | ssl->method->next_message(ssl); |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 568 | hs->tls13_state = state_send_end_of_early_data; |
| 569 | return ssl_hs_ok; |
| 570 | } |
| 571 | |
| 572 | static enum ssl_hs_wait_t do_send_end_of_early_data(SSL_HANDSHAKE *hs) { |
| 573 | SSL *const ssl = hs->ssl; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 574 | |
| 575 | if (ssl->early_data_accepted) { |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 576 | hs->can_early_write = false; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 577 | if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING, |
| 578 | TLS1_AD_END_OF_EARLY_DATA)) { |
| 579 | return ssl_hs_error; |
| 580 | } |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 581 | } |
| 582 | |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 583 | if (hs->early_data_offered) { |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 584 | if ((ssl_is_resumption_client_ccs_experiment(ssl->version) && |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 585 | !ssl3_add_change_cipher_spec(ssl)) || |
| 586 | !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_handshake_secret, |
| 587 | hs->hash_len)) { |
| 588 | return ssl_hs_error; |
| 589 | } |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 590 | } |
| 591 | |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 592 | hs->tls13_state = state_send_client_certificate; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 593 | return ssl_hs_ok; |
| 594 | } |
| 595 | |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 596 | static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 597 | SSL *const ssl = hs->ssl; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 598 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 599 | // The peer didn't request a certificate. |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 600 | if (!hs->cert_request) { |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 601 | hs->tls13_state = state_complete_second_flight; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 602 | return ssl_hs_ok; |
| 603 | } |
| 604 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 605 | // Call cert_cb to update the certificate. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 606 | if (ssl->cert->cert_cb != NULL) { |
| 607 | int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg); |
| 608 | if (rv == 0) { |
| 609 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 610 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
| 611 | return ssl_hs_error; |
| 612 | } |
| 613 | if (rv < 0) { |
David Benjamin | 3977f30 | 2016-12-11 13:30:41 -0500 | [diff] [blame] | 614 | hs->tls13_state = state_send_client_certificate; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 615 | return ssl_hs_x509_lookup; |
| 616 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 617 | } |
| 618 | |
David Benjamin | a232a71 | 2017-03-30 15:51:53 -0500 | [diff] [blame] | 619 | if (!ssl_on_certificate_selected(hs) || |
David Benjamin | 0f24bed | 2017-01-12 19:46:50 -0500 | [diff] [blame] | 620 | !tls13_add_certificate(hs)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 621 | return ssl_hs_error; |
| 622 | } |
| 623 | |
David Benjamin | 3977f30 | 2016-12-11 13:30:41 -0500 | [diff] [blame] | 624 | hs->tls13_state = state_send_client_certificate_verify; |
David Benjamin | 25ac251 | 2017-01-12 19:31:28 -0500 | [diff] [blame] | 625 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 626 | } |
| 627 | |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 628 | static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 629 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 630 | // Don't send CertificateVerify if there is no certificate. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 631 | if (!ssl_has_certificate(ssl)) { |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 632 | hs->tls13_state = state_complete_second_flight; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 633 | return ssl_hs_ok; |
| 634 | } |
| 635 | |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 636 | switch (tls13_add_certificate_verify(hs)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 637 | case ssl_private_key_success: |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 638 | hs->tls13_state = state_complete_second_flight; |
David Benjamin | 25ac251 | 2017-01-12 19:31:28 -0500 | [diff] [blame] | 639 | return ssl_hs_ok; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 640 | |
| 641 | case ssl_private_key_retry: |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 642 | hs->tls13_state = state_send_client_certificate_verify; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 643 | return ssl_hs_private_key_operation; |
| 644 | |
| 645 | case ssl_private_key_failure: |
| 646 | return ssl_hs_error; |
| 647 | } |
| 648 | |
| 649 | assert(0); |
| 650 | return ssl_hs_error; |
| 651 | } |
| 652 | |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 653 | static enum ssl_hs_wait_t do_complete_second_flight(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 654 | SSL *const ssl = hs->ssl; |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 655 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 656 | // Send a Channel ID assertion if necessary. |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 657 | if (ssl->s3->tlsext_channel_id_valid) { |
| 658 | if (!ssl_do_channel_id_callback(ssl)) { |
| 659 | hs->tls13_state = state_complete_second_flight; |
| 660 | return ssl_hs_error; |
| 661 | } |
| 662 | |
| 663 | if (ssl->tlsext_channel_id_private == NULL) { |
| 664 | return ssl_hs_channel_id_lookup; |
| 665 | } |
| 666 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 667 | ScopedCBB cbb; |
| 668 | CBB body; |
| 669 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) || |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 670 | !tls1_write_channel_id(hs, &body) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 671 | !ssl_add_message_cbb(ssl, cbb.get())) { |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 672 | return ssl_hs_error; |
| 673 | } |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 674 | } |
| 675 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 676 | // Send a Finished message. |
David Benjamin | 0f24bed | 2017-01-12 19:46:50 -0500 | [diff] [blame] | 677 | if (!tls13_add_finished(hs)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 678 | return ssl_hs_error; |
| 679 | } |
| 680 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 681 | // Derive the final keys and enable them. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 682 | if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->server_traffic_secret_0, |
| 683 | hs->hash_len) || |
| 684 | !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_traffic_secret_0, |
| 685 | hs->hash_len) || |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 686 | !tls13_derive_resumption_secret(hs)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 687 | return ssl_hs_error; |
| 688 | } |
| 689 | |
David Benjamin | 3977f30 | 2016-12-11 13:30:41 -0500 | [diff] [blame] | 690 | hs->tls13_state = state_done; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 691 | return ssl_hs_flush; |
| 692 | } |
| 693 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 694 | enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs) { |
David Benjamin | 3977f30 | 2016-12-11 13:30:41 -0500 | [diff] [blame] | 695 | while (hs->tls13_state != state_done) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 696 | enum ssl_hs_wait_t ret = ssl_hs_error; |
David Benjamin | d304a2f | 2017-07-12 23:00:28 -0400 | [diff] [blame] | 697 | enum client_hs_state_t state = |
| 698 | static_cast<enum client_hs_state_t>(hs->tls13_state); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 699 | switch (state) { |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 700 | case state_read_hello_retry_request: |
| 701 | ret = do_read_hello_retry_request(hs); |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 702 | break; |
| 703 | case state_send_second_client_hello: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 704 | ret = do_send_second_client_hello(hs); |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 705 | break; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 706 | case state_read_server_hello: |
| 707 | ret = do_read_server_hello(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 708 | break; |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 709 | case state_process_change_cipher_spec: |
| 710 | ret = do_process_change_cipher_spec(hs); |
| 711 | break; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 712 | case state_read_encrypted_extensions: |
| 713 | ret = do_read_encrypted_extensions(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 714 | break; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 715 | case state_read_certificate_request: |
| 716 | ret = do_read_certificate_request(hs); |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 717 | break; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 718 | case state_read_server_certificate: |
| 719 | ret = do_read_server_certificate(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 720 | break; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 721 | case state_read_server_certificate_verify: |
| 722 | ret = do_read_server_certificate_verify(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 723 | break; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 724 | case state_read_server_finished: |
| 725 | ret = do_read_server_finished(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 726 | break; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 727 | case state_send_end_of_early_data: |
| 728 | ret = do_send_end_of_early_data(hs); |
| 729 | break; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 730 | case state_send_client_certificate: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 731 | ret = do_send_client_certificate(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 732 | break; |
| 733 | case state_send_client_certificate_verify: |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 734 | ret = do_send_client_certificate_verify(hs); |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 735 | break; |
David Benjamin | 81b7bc3 | 2017-01-12 19:44:57 -0500 | [diff] [blame] | 736 | case state_complete_second_flight: |
| 737 | ret = do_complete_second_flight(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 738 | break; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 739 | case state_done: |
| 740 | ret = ssl_hs_ok; |
| 741 | break; |
| 742 | } |
| 743 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 744 | if (hs->tls13_state != state) { |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 745 | ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1); |
| 746 | } |
| 747 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 748 | if (ret != ssl_hs_ok) { |
| 749 | return ret; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | return ssl_hs_ok; |
| 754 | } |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 755 | |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 756 | const char *tls13_client_handshake_state(SSL_HANDSHAKE *hs) { |
| 757 | enum client_hs_state_t state = |
| 758 | static_cast<enum client_hs_state_t>(hs->tls13_state); |
| 759 | switch (state) { |
| 760 | case state_read_hello_retry_request: |
| 761 | return "TLS 1.3 client read_hello_retry_request"; |
| 762 | case state_send_second_client_hello: |
| 763 | return "TLS 1.3 client send_second_client_hello"; |
| 764 | case state_read_server_hello: |
| 765 | return "TLS 1.3 client read_server_hello"; |
| 766 | case state_process_change_cipher_spec: |
| 767 | return "TLS 1.3 client process_change_cipher_spec"; |
| 768 | case state_read_encrypted_extensions: |
| 769 | return "TLS 1.3 client read_encrypted_extensions"; |
| 770 | case state_read_certificate_request: |
| 771 | return "TLS 1.3 client read_certificate_request"; |
| 772 | case state_read_server_certificate: |
| 773 | return "TLS 1.3 client read_server_certificate"; |
| 774 | case state_read_server_certificate_verify: |
| 775 | return "TLS 1.3 client read_server_certificate_verify"; |
| 776 | case state_read_server_finished: |
| 777 | return "TLS 1.3 client read_server_finished"; |
| 778 | case state_send_end_of_early_data: |
| 779 | return "TLS 1.3 client send_end_of_early_data"; |
| 780 | case state_send_client_certificate: |
| 781 | return "TLS 1.3 client send_client_certificate"; |
| 782 | case state_send_client_certificate_verify: |
| 783 | return "TLS 1.3 client send_client_certificate_verify"; |
| 784 | case state_complete_second_flight: |
| 785 | return "TLS 1.3 client complete_second_flight"; |
| 786 | case state_done: |
| 787 | return "TLS 1.3 client done"; |
| 788 | } |
| 789 | |
| 790 | return "TLS 1.3 client unknown"; |
| 791 | } |
| 792 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 793 | int tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 794 | UniquePtr<SSL_SESSION> session(SSL_SESSION_dup(ssl->s3->established_session, |
| 795 | SSL_SESSION_INCLUDE_NONAUTH)); |
David Benjamin | d304a2f | 2017-07-12 23:00:28 -0400 | [diff] [blame] | 796 | if (!session) { |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 797 | return 0; |
| 798 | } |
| 799 | |
David Benjamin | d304a2f | 2017-07-12 23:00:28 -0400 | [diff] [blame] | 800 | ssl_session_rebase_time(ssl, session.get()); |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame] | 801 | |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 802 | uint32_t server_timeout; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 803 | CBS body = msg.body, ticket, extensions; |
| 804 | if (!CBS_get_u32(&body, &server_timeout) || |
| 805 | !CBS_get_u32(&body, &session->ticket_age_add) || |
| 806 | !CBS_get_u16_length_prefixed(&body, &ticket) || |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 807 | !CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen) || |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 808 | !CBS_get_u16_length_prefixed(&body, &extensions) || |
| 809 | CBS_len(&body) != 0) { |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 810 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 811 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | d304a2f | 2017-07-12 23:00:28 -0400 | [diff] [blame] | 812 | return 0; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 813 | } |
| 814 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 815 | // Cap the renewable lifetime by the server advertised value. This avoids |
| 816 | // wasting bandwidth on 0-RTT when we know the server will reject it. |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 817 | if (session->timeout > server_timeout) { |
| 818 | session->timeout = server_timeout; |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 819 | } |
| 820 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 821 | // Parse out the extensions. |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 822 | bool have_early_data_info = false; |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 823 | CBS early_data_info; |
| 824 | const SSL_EXTENSION_TYPE ext_types[] = { |
| 825 | {TLSEXT_TYPE_ticket_early_data_info, &have_early_data_info, |
| 826 | &early_data_info}, |
| 827 | }; |
| 828 | |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 829 | uint8_t alert = SSL_AD_DECODE_ERROR; |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 830 | if (!ssl_parse_extensions(&extensions, &alert, ext_types, |
| 831 | OPENSSL_ARRAY_SIZE(ext_types), |
| 832 | 1 /* ignore unknown */)) { |
| 833 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | d304a2f | 2017-07-12 23:00:28 -0400 | [diff] [blame] | 834 | return 0; |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 835 | } |
| 836 | |
Alessandro Ghedini | 67bb45f | 2017-03-30 16:33:24 -0500 | [diff] [blame] | 837 | if (have_early_data_info && ssl->cert->enable_early_data) { |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 838 | if (!CBS_get_u32(&early_data_info, &session->ticket_max_early_data) || |
| 839 | CBS_len(&early_data_info) != 0) { |
| 840 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 9c33ae8 | 2017-01-08 06:04:43 -0500 | [diff] [blame] | 841 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | d304a2f | 2017-07-12 23:00:28 -0400 | [diff] [blame] | 842 | return 0; |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 846 | session->ticket_age_add_valid = 1; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 847 | session->not_resumable = 0; |
| 848 | |
David Benjamin | 3d8f080 | 2017-09-06 16:12:52 -0400 | [diff] [blame] | 849 | if ((ssl->ctx->session_cache_mode & SSL_SESS_CACHE_CLIENT) && |
| 850 | ssl->ctx->new_session_cb != NULL && |
David Benjamin | d304a2f | 2017-07-12 23:00:28 -0400 | [diff] [blame] | 851 | ssl->ctx->new_session_cb(ssl, session.get())) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 852 | // |new_session_cb|'s return value signals that it took ownership. |
David Benjamin | d304a2f | 2017-07-12 23:00:28 -0400 | [diff] [blame] | 853 | session.release(); |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 854 | } |
| 855 | |
David Benjamin | d304a2f | 2017-07-12 23:00:28 -0400 | [diff] [blame] | 856 | return 1; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 857 | } |
David Benjamin | 4fe3c90 | 2016-08-16 02:17:03 -0400 | [diff] [blame] | 858 | |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 859 | void ssl_clear_tls13_state(SSL_HANDSHAKE *hs) { |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 860 | hs->key_share.reset(); |
David Benjamin | 4fe3c90 | 2016-08-16 02:17:03 -0400 | [diff] [blame] | 861 | |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 862 | OPENSSL_free(hs->key_share_bytes); |
| 863 | hs->key_share_bytes = NULL; |
| 864 | hs->key_share_bytes_len = 0; |
David Benjamin | 4fe3c90 | 2016-08-16 02:17:03 -0400 | [diff] [blame] | 865 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 866 | |
| 867 | } // namespace bssl |