David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2016, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include <openssl/ssl.h> |
| 16 | |
| 17 | #include <assert.h> |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 18 | #include <limits.h> |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 19 | #include <string.h> |
| 20 | |
| 21 | #include <openssl/bytestring.h> |
| 22 | #include <openssl/digest.h> |
| 23 | #include <openssl/err.h> |
| 24 | #include <openssl/mem.h> |
| 25 | #include <openssl/stack.h> |
| 26 | #include <openssl/x509.h> |
| 27 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 28 | #include "../crypto/internal.h" |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 29 | #include "internal.h" |
| 30 | |
| 31 | |
| 32 | enum client_hs_state_t { |
| 33 | state_process_hello_retry_request = 0, |
| 34 | state_send_second_client_hello, |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 35 | state_process_server_hello, |
| 36 | state_process_encrypted_extensions, |
| 37 | state_process_certificate_request, |
| 38 | state_process_server_certificate, |
| 39 | state_process_server_certificate_verify, |
| 40 | state_process_server_finished, |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 41 | state_send_client_certificate, |
| 42 | state_send_client_certificate_verify, |
| 43 | state_complete_client_certificate_verify, |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 44 | state_complete_second_flight, |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 45 | state_done, |
| 46 | }; |
| 47 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 48 | static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0}; |
| 49 | |
| 50 | static enum ssl_hs_wait_t do_process_hello_retry_request(SSL_HANDSHAKE *hs) { |
| 51 | SSL *const ssl = hs->ssl; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 52 | if (ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 53 | hs->tls13_state = state_process_server_hello; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 54 | return ssl_hs_ok; |
| 55 | } |
| 56 | |
| 57 | CBS cbs, extensions; |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 58 | uint16_t server_wire_version; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 59 | CBS_init(&cbs, ssl->init_msg, ssl->init_num); |
| 60 | if (!CBS_get_u16(&cbs, &server_wire_version) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 61 | !CBS_get_u16_length_prefixed(&cbs, &extensions) || |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 62 | /* HelloRetryRequest may not be empty. */ |
| 63 | CBS_len(&extensions) == 0 || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 64 | CBS_len(&cbs) != 0) { |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 65 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 66 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 67 | return ssl_hs_error; |
| 68 | } |
| 69 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 70 | int have_cookie, have_key_share; |
| 71 | CBS cookie, key_share; |
| 72 | const SSL_EXTENSION_TYPE ext_types[] = { |
| 73 | {TLSEXT_TYPE_key_share, &have_key_share, &key_share}, |
| 74 | {TLSEXT_TYPE_cookie, &have_cookie, &cookie}, |
| 75 | }; |
| 76 | |
| 77 | uint8_t alert; |
| 78 | if (!ssl_parse_extensions(&extensions, &alert, ext_types, |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 79 | OPENSSL_ARRAY_SIZE(ext_types), |
| 80 | 0 /* reject unknown */)) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 81 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 82 | return ssl_hs_error; |
| 83 | } |
| 84 | |
| 85 | if (have_cookie) { |
| 86 | CBS cookie_value; |
| 87 | if (!CBS_get_u16_length_prefixed(&cookie, &cookie_value) || |
| 88 | CBS_len(&cookie_value) == 0 || |
| 89 | CBS_len(&cookie) != 0) { |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 90 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 91 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 92 | return ssl_hs_error; |
| 93 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 94 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 95 | if (!CBS_stow(&cookie_value, &hs->cookie, &hs->cookie_len)) { |
| 96 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 100 | if (have_key_share) { |
| 101 | uint16_t group_id; |
| 102 | if (!CBS_get_u16(&key_share, &group_id) || CBS_len(&key_share) != 0) { |
| 103 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 104 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 105 | return ssl_hs_error; |
| 106 | } |
| 107 | |
| 108 | /* The group must be supported. */ |
| 109 | const uint16_t *groups; |
| 110 | size_t groups_len; |
| 111 | tls1_get_grouplist(ssl, &groups, &groups_len); |
| 112 | int found = 0; |
| 113 | for (size_t i = 0; i < groups_len; i++) { |
| 114 | if (groups[i] == group_id) { |
| 115 | found = 1; |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if (!found) { |
| 121 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 122 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
| 123 | return ssl_hs_error; |
| 124 | } |
| 125 | |
| 126 | /* Check that the HelloRetryRequest does not request the key share that |
| 127 | * was provided in the initial ClientHello. */ |
| 128 | if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) == group_id) { |
| 129 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 130 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
| 131 | return ssl_hs_error; |
| 132 | } |
| 133 | |
| 134 | SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx); |
| 135 | hs->retry_group = group_id; |
| 136 | } |
| 137 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 138 | if (!ssl_hash_current_message(ssl)) { |
| 139 | return ssl_hs_error; |
| 140 | } |
| 141 | |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 142 | hs->received_hello_retry_request = 1; |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 143 | hs->tls13_state = state_send_second_client_hello; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 144 | return ssl_hs_ok; |
| 145 | } |
| 146 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 147 | static enum ssl_hs_wait_t do_send_second_client_hello(SSL_HANDSHAKE *hs) { |
| 148 | if (!ssl_write_client_hello(hs)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 149 | return ssl_hs_error; |
| 150 | } |
| 151 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 152 | hs->tls13_state = state_process_server_hello; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 153 | return ssl_hs_flush_and_read_message; |
| 154 | } |
| 155 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 156 | static enum ssl_hs_wait_t do_process_server_hello(SSL_HANDSHAKE *hs) { |
| 157 | SSL *const ssl = hs->ssl; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 158 | if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 159 | return ssl_hs_error; |
| 160 | } |
| 161 | |
| 162 | CBS cbs, server_random, extensions; |
| 163 | uint16_t server_wire_version; |
| 164 | uint16_t cipher_suite; |
| 165 | CBS_init(&cbs, ssl->init_msg, ssl->init_num); |
| 166 | if (!CBS_get_u16(&cbs, &server_wire_version) || |
| 167 | !CBS_get_bytes(&cbs, &server_random, SSL3_RANDOM_SIZE) || |
| 168 | !CBS_get_u16(&cbs, &cipher_suite) || |
| 169 | !CBS_get_u16_length_prefixed(&cbs, &extensions) || |
| 170 | CBS_len(&cbs) != 0) { |
| 171 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 172 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 173 | return ssl_hs_error; |
| 174 | } |
| 175 | |
| 176 | if (server_wire_version != ssl->version) { |
| 177 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 178 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER); |
| 179 | return ssl_hs_error; |
| 180 | } |
| 181 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 182 | assert(ssl->s3->have_version); |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 183 | OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random), |
| 184 | SSL3_RANDOM_SIZE); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 185 | |
| 186 | const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite); |
| 187 | if (cipher == NULL) { |
| 188 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED); |
| 189 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 190 | return ssl_hs_error; |
| 191 | } |
| 192 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 193 | /* Check if the cipher is a TLS 1.3 cipher. */ |
| 194 | if (SSL_CIPHER_get_min_version(cipher) > ssl3_protocol_version(ssl) || |
| 195 | SSL_CIPHER_get_max_version(cipher) < ssl3_protocol_version(ssl)) { |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 196 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
| 197 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 198 | return ssl_hs_error; |
| 199 | } |
| 200 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 201 | /* Parse out the extensions. */ |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 202 | int have_key_share = 0, have_pre_shared_key = 0, have_short_header = 0; |
| 203 | CBS key_share, pre_shared_key, short_header; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 204 | const SSL_EXTENSION_TYPE ext_types[] = { |
| 205 | {TLSEXT_TYPE_key_share, &have_key_share, &key_share}, |
| 206 | {TLSEXT_TYPE_pre_shared_key, &have_pre_shared_key, &pre_shared_key}, |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 207 | {TLSEXT_TYPE_short_header, &have_short_header, &short_header}, |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 208 | }; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 209 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 210 | uint8_t alert; |
| 211 | if (!ssl_parse_extensions(&extensions, &alert, ext_types, |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 212 | OPENSSL_ARRAY_SIZE(ext_types), |
| 213 | 0 /* reject unknown */)) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 214 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 215 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 216 | } |
| 217 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 218 | alert = SSL_AD_DECODE_ERROR; |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 219 | if (have_pre_shared_key) { |
| 220 | if (ssl->session == NULL) { |
| 221 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 222 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION); |
| 223 | return ssl_hs_error; |
| 224 | } |
| 225 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 226 | if (!ssl_ext_pre_shared_key_parse_serverhello(hs, &alert, |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 227 | &pre_shared_key)) { |
| 228 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 229 | return ssl_hs_error; |
| 230 | } |
| 231 | |
| 232 | if (ssl->session->ssl_version != ssl->version) { |
| 233 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED); |
| 234 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 235 | return ssl_hs_error; |
| 236 | } |
| 237 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 238 | if (ssl->session->cipher->algorithm_prf != cipher->algorithm_prf) { |
| 239 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_PRF_HASH_MISMATCH); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 240 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 241 | return ssl_hs_error; |
| 242 | } |
| 243 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 244 | if (!ssl_session_is_context_valid(ssl, ssl->session)) { |
| 245 | /* This is actually a client application bug. */ |
| 246 | OPENSSL_PUT_ERROR(SSL, |
| 247 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
| 248 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 249 | return ssl_hs_error; |
| 250 | } |
| 251 | |
| 252 | ssl->s3->session_reused = 1; |
| 253 | /* Only authentication information carries over in TLS 1.3. */ |
| 254 | ssl->s3->new_session = |
| 255 | SSL_SESSION_dup(ssl->session, SSL_SESSION_DUP_AUTH_ONLY); |
| 256 | if (ssl->s3->new_session == NULL) { |
| 257 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 258 | return ssl_hs_error; |
| 259 | } |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 260 | ssl_set_session(ssl, NULL); |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 261 | |
| 262 | /* Resumption incorporates fresh key material, so refresh the timeout. */ |
| 263 | ssl_session_renew_timeout(ssl, ssl->s3->new_session, |
| 264 | ssl->session_psk_dhe_timeout); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 265 | } else if (!ssl_get_new_session(hs, 0)) { |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 266 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 267 | return ssl_hs_error; |
| 268 | } |
| 269 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 270 | ssl->s3->new_session->cipher = cipher; |
| 271 | ssl->s3->tmp.new_cipher = cipher; |
| 272 | |
| 273 | /* The PRF hash is now known. Set up the key schedule. */ |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 274 | size_t hash_len = |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 275 | EVP_MD_size(ssl_get_handshake_digest(ssl_get_algorithm_prf(ssl))); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 276 | if (!tls13_init_key_schedule(hs)) { |
| 277 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 278 | } |
| 279 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 280 | /* Incorporate the PSK into the running secret. */ |
| 281 | if (ssl->s3->session_reused) { |
| 282 | if (!tls13_advance_key_schedule(hs, ssl->s3->new_session->master_key, |
| 283 | ssl->s3->new_session->master_key_length)) { |
| 284 | return ssl_hs_error; |
| 285 | } |
| 286 | } else if (!tls13_advance_key_schedule(hs, kZeroes, hash_len)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 287 | return ssl_hs_error; |
| 288 | } |
| 289 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 290 | if (!have_key_share) { |
| 291 | /* We do not support psk_ke and thus always require a key share. */ |
| 292 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE); |
| 293 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION); |
| 294 | return ssl_hs_error; |
| 295 | } |
| 296 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 297 | /* Resolve ECDHE and incorporate it into the secret. */ |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 298 | uint8_t *dhe_secret; |
| 299 | size_t dhe_secret_len; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 300 | if (!ssl_ext_key_share_parse_serverhello(hs, &dhe_secret, &dhe_secret_len, |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 301 | &alert, &key_share)) { |
| 302 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 303 | return ssl_hs_error; |
| 304 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 305 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 306 | if (!tls13_advance_key_schedule(hs, dhe_secret, dhe_secret_len)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 307 | OPENSSL_free(dhe_secret); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 308 | return ssl_hs_error; |
| 309 | } |
| 310 | OPENSSL_free(dhe_secret); |
| 311 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 312 | /* Negotiate short record headers. */ |
| 313 | if (have_short_header) { |
| 314 | if (CBS_len(&short_header) != 0) { |
| 315 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 316 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 317 | return ssl_hs_error; |
| 318 | } |
| 319 | |
| 320 | if (!ssl->ctx->short_header_enabled) { |
| 321 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 322 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION); |
| 323 | return ssl_hs_error; |
| 324 | } |
| 325 | |
| 326 | ssl->s3->short_header = 1; |
| 327 | } |
| 328 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 329 | if (!ssl_hash_current_message(ssl) || |
| 330 | !tls13_derive_handshake_secrets(hs) || |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 331 | !tls13_set_traffic_key(ssl, evp_aead_open, hs->server_handshake_secret, |
| 332 | hs->hash_len) || |
| 333 | !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_handshake_secret, |
| 334 | hs->hash_len)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 335 | return ssl_hs_error; |
| 336 | } |
| 337 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 338 | hs->tls13_state = state_process_encrypted_extensions; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 339 | return ssl_hs_read_message; |
| 340 | } |
| 341 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 342 | static enum ssl_hs_wait_t do_process_encrypted_extensions(SSL_HANDSHAKE *hs) { |
| 343 | SSL *const ssl = hs->ssl; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 344 | if (!ssl_check_message_type(ssl, SSL3_MT_ENCRYPTED_EXTENSIONS)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 345 | return ssl_hs_error; |
| 346 | } |
| 347 | |
| 348 | CBS cbs; |
| 349 | CBS_init(&cbs, ssl->init_msg, ssl->init_num); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 350 | if (!ssl_parse_serverhello_tlsext(hs, &cbs)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 351 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 352 | return ssl_hs_error; |
| 353 | } |
| 354 | if (CBS_len(&cbs) != 0) { |
| 355 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 356 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 357 | return ssl_hs_error; |
| 358 | } |
| 359 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 360 | if (!ssl_hash_current_message(ssl)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 361 | return ssl_hs_error; |
| 362 | } |
| 363 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 364 | hs->tls13_state = state_process_certificate_request; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 365 | return ssl_hs_read_message; |
| 366 | } |
| 367 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 368 | static enum ssl_hs_wait_t do_process_certificate_request(SSL_HANDSHAKE *hs) { |
| 369 | SSL *const ssl = hs->ssl; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 370 | /* CertificateRequest may only be sent in non-resumption handshakes. */ |
| 371 | if (ssl->s3->session_reused) { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 372 | hs->tls13_state = state_process_server_finished; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 373 | return ssl_hs_ok; |
| 374 | } |
| 375 | |
| 376 | /* CertificateRequest is optional. */ |
| 377 | if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 378 | hs->tls13_state = state_process_server_certificate; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 379 | return ssl_hs_ok; |
| 380 | } |
| 381 | |
| 382 | CBS cbs, context, supported_signature_algorithms; |
| 383 | CBS_init(&cbs, ssl->init_msg, ssl->init_num); |
| 384 | if (!CBS_get_u8_length_prefixed(&cbs, &context) || |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 385 | /* The request context is always empty during the handshake. */ |
| 386 | CBS_len(&context) != 0 || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 387 | !CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) || |
| 388 | CBS_len(&supported_signature_algorithms) == 0 || |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 389 | !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 390 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 391 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 392 | return ssl_hs_error; |
| 393 | } |
| 394 | |
| 395 | uint8_t alert; |
| 396 | STACK_OF(X509_NAME) *ca_sk = ssl_parse_client_CA_list(ssl, &alert, &cbs); |
| 397 | if (ca_sk == NULL) { |
| 398 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 399 | return ssl_hs_error; |
| 400 | } |
| 401 | |
| 402 | /* Ignore extensions. */ |
| 403 | CBS extensions; |
| 404 | if (!CBS_get_u16_length_prefixed(&cbs, &extensions) || |
| 405 | CBS_len(&cbs) != 0) { |
| 406 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 407 | sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 408 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 409 | return ssl_hs_error; |
| 410 | } |
| 411 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 412 | hs->cert_request = 1; |
| 413 | sk_X509_NAME_pop_free(hs->ca_names, X509_NAME_free); |
| 414 | hs->ca_names = ca_sk; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 415 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 416 | if (!ssl_hash_current_message(ssl)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 417 | return ssl_hs_error; |
| 418 | } |
| 419 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 420 | hs->tls13_state = state_process_server_certificate; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 421 | return ssl_hs_read_message; |
| 422 | } |
| 423 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 424 | static enum ssl_hs_wait_t do_process_server_certificate(SSL_HANDSHAKE *hs) { |
| 425 | SSL *const ssl = hs->ssl; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 426 | if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE) || |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 427 | !tls13_process_certificate(hs, 0 /* certificate required */) || |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 428 | !ssl_hash_current_message(ssl)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 429 | return ssl_hs_error; |
| 430 | } |
| 431 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 432 | hs->tls13_state = state_process_server_certificate_verify; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 433 | return ssl_hs_read_message; |
| 434 | } |
| 435 | |
| 436 | static enum ssl_hs_wait_t do_process_server_certificate_verify( |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 437 | SSL_HANDSHAKE *hs) { |
| 438 | SSL *const ssl = hs->ssl; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 439 | if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY) || |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 440 | !tls13_process_certificate_verify(hs) || |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 441 | !ssl_hash_current_message(ssl)) { |
| 442 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 443 | } |
| 444 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 445 | hs->tls13_state = state_process_server_finished; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 446 | return ssl_hs_read_message; |
| 447 | } |
| 448 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 449 | static enum ssl_hs_wait_t do_process_server_finished(SSL_HANDSHAKE *hs) { |
| 450 | SSL *const ssl = hs->ssl; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 451 | if (!ssl_check_message_type(ssl, SSL3_MT_FINISHED) || |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 452 | !tls13_process_finished(hs) || |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 453 | !ssl_hash_current_message(ssl) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 454 | /* Update the secret to the master secret and derive traffic keys. */ |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 455 | !tls13_advance_key_schedule(hs, kZeroes, hs->hash_len) || |
| 456 | !tls13_derive_application_secrets(hs)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 457 | return ssl_hs_error; |
| 458 | } |
| 459 | |
| 460 | ssl->method->received_flight(ssl); |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 461 | hs->tls13_state = state_send_client_certificate; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 462 | return ssl_hs_ok; |
| 463 | } |
| 464 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 465 | static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 466 | SSL *const ssl = hs->ssl; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 467 | /* The peer didn't request a certificate. */ |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 468 | if (!hs->cert_request) { |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 469 | hs->tls13_state = state_complete_second_flight; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 470 | return ssl_hs_ok; |
| 471 | } |
| 472 | |
| 473 | /* Call cert_cb to update the certificate. */ |
| 474 | if (ssl->cert->cert_cb != NULL) { |
| 475 | int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg); |
| 476 | if (rv == 0) { |
| 477 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 478 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
| 479 | return ssl_hs_error; |
| 480 | } |
| 481 | if (rv < 0) { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 482 | hs->tls13_state = state_send_client_certificate; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 483 | return ssl_hs_x509_lookup; |
| 484 | } |
| 485 | } |
| 486 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 487 | if (!ssl_auto_chain_if_needed(ssl) || |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 488 | !tls13_add_certificate(hs)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 489 | return ssl_hs_error; |
| 490 | } |
| 491 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 492 | hs->tls13_state = state_send_client_certificate_verify; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 493 | return ssl_hs_ok; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 494 | } |
| 495 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 496 | static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs, |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 497 | int is_first_run) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 498 | SSL *const ssl = hs->ssl; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 499 | /* Don't send CertificateVerify if there is no certificate. */ |
| 500 | if (!ssl_has_certificate(ssl)) { |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 501 | hs->tls13_state = state_complete_second_flight; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 502 | return ssl_hs_ok; |
| 503 | } |
| 504 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 505 | switch (tls13_add_certificate_verify(hs, is_first_run)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 506 | case ssl_private_key_success: |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 507 | hs->tls13_state = state_complete_second_flight; |
| 508 | return ssl_hs_ok; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 509 | |
| 510 | case ssl_private_key_retry: |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 511 | hs->tls13_state = state_complete_client_certificate_verify; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 512 | return ssl_hs_private_key_operation; |
| 513 | |
| 514 | case ssl_private_key_failure: |
| 515 | return ssl_hs_error; |
| 516 | } |
| 517 | |
| 518 | assert(0); |
| 519 | return ssl_hs_error; |
| 520 | } |
| 521 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 522 | static enum ssl_hs_wait_t do_complete_second_flight(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 523 | SSL *const ssl = hs->ssl; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 524 | |
| 525 | /* Send a Channel ID assertion if necessary. */ |
| 526 | if (ssl->s3->tlsext_channel_id_valid) { |
| 527 | if (!ssl_do_channel_id_callback(ssl)) { |
| 528 | hs->tls13_state = state_complete_second_flight; |
| 529 | return ssl_hs_error; |
| 530 | } |
| 531 | |
| 532 | if (ssl->tlsext_channel_id_private == NULL) { |
| 533 | return ssl_hs_channel_id_lookup; |
| 534 | } |
| 535 | |
| 536 | CBB cbb, body; |
| 537 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) || |
| 538 | !tls1_write_channel_id(ssl, &body) || |
| 539 | !ssl_add_message_cbb(ssl, &cbb)) { |
| 540 | CBB_cleanup(&cbb); |
| 541 | return ssl_hs_error; |
| 542 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 543 | } |
| 544 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 545 | /* Send a Finished message. */ |
| 546 | if (!tls13_add_finished(hs)) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 547 | return ssl_hs_error; |
| 548 | } |
| 549 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 550 | /* Derive the final keys and enable them. */ |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 551 | if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->server_traffic_secret_0, |
| 552 | hs->hash_len) || |
| 553 | !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_traffic_secret_0, |
| 554 | hs->hash_len) || |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 555 | !tls13_derive_resumption_secret(hs)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 556 | return ssl_hs_error; |
| 557 | } |
| 558 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 559 | hs->tls13_state = state_done; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 560 | return ssl_hs_flush; |
| 561 | } |
| 562 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 563 | enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs) { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 564 | while (hs->tls13_state != state_done) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 565 | enum ssl_hs_wait_t ret = ssl_hs_error; |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 566 | enum client_hs_state_t state = hs->tls13_state; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 567 | switch (state) { |
| 568 | case state_process_hello_retry_request: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 569 | ret = do_process_hello_retry_request(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 570 | break; |
| 571 | case state_send_second_client_hello: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 572 | ret = do_send_second_client_hello(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 573 | break; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 574 | case state_process_server_hello: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 575 | ret = do_process_server_hello(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 576 | break; |
| 577 | case state_process_encrypted_extensions: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 578 | ret = do_process_encrypted_extensions(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 579 | break; |
| 580 | case state_process_certificate_request: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 581 | ret = do_process_certificate_request(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 582 | break; |
| 583 | case state_process_server_certificate: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 584 | ret = do_process_server_certificate(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 585 | break; |
| 586 | case state_process_server_certificate_verify: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 587 | ret = do_process_server_certificate_verify(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 588 | break; |
| 589 | case state_process_server_finished: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 590 | ret = do_process_server_finished(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 591 | break; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 592 | case state_send_client_certificate: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 593 | ret = do_send_client_certificate(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 594 | break; |
| 595 | case state_send_client_certificate_verify: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 596 | ret = do_send_client_certificate_verify(hs, 1 /* first run */); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 597 | break; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 598 | case state_complete_client_certificate_verify: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 599 | ret = do_send_client_certificate_verify(hs, 0 /* complete */); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 600 | break; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 601 | case state_complete_second_flight: |
| 602 | ret = do_complete_second_flight(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 603 | break; |
| 604 | case state_done: |
| 605 | ret = ssl_hs_ok; |
| 606 | break; |
| 607 | } |
| 608 | |
| 609 | if (ret != ssl_hs_ok) { |
| 610 | return ret; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | return ssl_hs_ok; |
| 615 | } |
| 616 | |
| 617 | int tls13_process_new_session_ticket(SSL *ssl) { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 618 | int ret = 0; |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 619 | SSL_SESSION *session = |
| 620 | SSL_SESSION_dup(ssl->s3->established_session, |
| 621 | SSL_SESSION_INCLUDE_NONAUTH); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 622 | if (session == NULL) { |
| 623 | return 0; |
| 624 | } |
| 625 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 626 | ssl_session_rebase_time(ssl, session); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 627 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 628 | uint32_t server_timeout; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 629 | CBS cbs, ticket, extensions; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 630 | CBS_init(&cbs, ssl->init_msg, ssl->init_num); |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 631 | if (!CBS_get_u32(&cbs, &server_timeout) || |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 632 | !CBS_get_u32(&cbs, &session->ticket_age_add) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 633 | !CBS_get_u16_length_prefixed(&cbs, &ticket) || |
| 634 | !CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen) || |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 635 | !CBS_get_u16_length_prefixed(&cbs, &extensions) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 636 | CBS_len(&cbs) != 0) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 637 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 638 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 639 | goto err; |
| 640 | } |
| 641 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 642 | /* Cap the renewable lifetime by the server advertised value. This avoids |
| 643 | * wasting bandwidth on 0-RTT when we know the server will reject it. |
| 644 | * |
| 645 | * TODO(davidben): This dance where we're not sure if long or uint32_t is |
| 646 | * bigger is silly. session->timeout should not be a long to begin with. |
| 647 | * https://crbug.com/boringssl/155. */ |
| 648 | #if LONG_MAX < 0xffffffff |
| 649 | if (server_timeout > LONG_MAX) { |
| 650 | server_timeout = LONG_MAX; |
| 651 | } |
| 652 | #endif |
| 653 | if (session->timeout > (long)server_timeout) { |
| 654 | session->timeout = (long)server_timeout; |
| 655 | } |
| 656 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 657 | /* Parse out the extensions. */ |
| 658 | int have_early_data_info = 0; |
| 659 | CBS early_data_info; |
| 660 | const SSL_EXTENSION_TYPE ext_types[] = { |
| 661 | {TLSEXT_TYPE_ticket_early_data_info, &have_early_data_info, |
| 662 | &early_data_info}, |
| 663 | }; |
| 664 | |
| 665 | uint8_t alert; |
| 666 | if (!ssl_parse_extensions(&extensions, &alert, ext_types, |
| 667 | OPENSSL_ARRAY_SIZE(ext_types), |
| 668 | 1 /* ignore unknown */)) { |
| 669 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 670 | goto err; |
| 671 | } |
| 672 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame^] | 673 | if (have_early_data_info && ssl->ctx->enable_early_data) { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 674 | if (!CBS_get_u32(&early_data_info, &session->ticket_max_early_data) || |
| 675 | CBS_len(&early_data_info) != 0) { |
| 676 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 677 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 678 | goto err; |
| 679 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 680 | } |
| 681 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 682 | session->ticket_age_add_valid = 1; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 683 | session->not_resumable = 0; |
| 684 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 685 | if (ssl->ctx->new_session_cb != NULL && |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 686 | ssl->ctx->new_session_cb(ssl, session)) { |
| 687 | /* |new_session_cb|'s return value signals that it took ownership. */ |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 688 | session = NULL; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 689 | } |
| 690 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 691 | ret = 1; |
| 692 | |
| 693 | err: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 694 | SSL_SESSION_free(session); |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 695 | return ret; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 696 | } |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 697 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 698 | void ssl_clear_tls13_state(SSL_HANDSHAKE *hs) { |
| 699 | SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx); |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 700 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 701 | OPENSSL_free(hs->key_share_bytes); |
| 702 | hs->key_share_bytes = NULL; |
| 703 | hs->key_share_bytes_len = 0; |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 704 | } |