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 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 77 | uint8_t alert = SSL_AD_DECODE_ERROR; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 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 | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 138 | if (!ssl_hash_current_message(hs)) { |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 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 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 210 | uint8_t alert = SSL_AD_DECODE_ERROR; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 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, |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 264 | ssl->initial_ctx->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. */ |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 274 | if (!tls13_init_key_schedule(hs)) { |
| 275 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 276 | } |
| 277 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 278 | /* Incorporate the PSK into the running secret. */ |
| 279 | if (ssl->s3->session_reused) { |
| 280 | if (!tls13_advance_key_schedule(hs, ssl->s3->new_session->master_key, |
| 281 | ssl->s3->new_session->master_key_length)) { |
| 282 | return ssl_hs_error; |
| 283 | } |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 284 | } else if (!tls13_advance_key_schedule(hs, kZeroes, hs->hash_len)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 285 | return ssl_hs_error; |
| 286 | } |
| 287 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 288 | if (!have_key_share) { |
| 289 | /* We do not support psk_ke and thus always require a key share. */ |
| 290 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE); |
| 291 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION); |
| 292 | return ssl_hs_error; |
| 293 | } |
| 294 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 295 | /* Resolve ECDHE and incorporate it into the secret. */ |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 296 | uint8_t *dhe_secret; |
| 297 | size_t dhe_secret_len; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 298 | alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 299 | 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] | 300 | &alert, &key_share)) { |
| 301 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 302 | return ssl_hs_error; |
| 303 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 304 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 305 | if (!tls13_advance_key_schedule(hs, dhe_secret, dhe_secret_len)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 306 | OPENSSL_free(dhe_secret); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 307 | return ssl_hs_error; |
| 308 | } |
| 309 | OPENSSL_free(dhe_secret); |
| 310 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 311 | /* Negotiate short record headers. */ |
| 312 | if (have_short_header) { |
| 313 | if (CBS_len(&short_header) != 0) { |
| 314 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 315 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 316 | return ssl_hs_error; |
| 317 | } |
| 318 | |
| 319 | if (!ssl->ctx->short_header_enabled) { |
| 320 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 321 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION); |
| 322 | return ssl_hs_error; |
| 323 | } |
| 324 | |
| 325 | ssl->s3->short_header = 1; |
| 326 | } |
| 327 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 328 | if (!ssl_hash_current_message(hs) || |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 329 | !tls13_derive_handshake_secrets(hs) || |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 330 | !tls13_set_traffic_key(ssl, evp_aead_open, hs->server_handshake_secret, |
| 331 | hs->hash_len) || |
| 332 | !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_handshake_secret, |
| 333 | hs->hash_len)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 334 | return ssl_hs_error; |
| 335 | } |
| 336 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 337 | hs->tls13_state = state_process_encrypted_extensions; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 338 | return ssl_hs_read_message; |
| 339 | } |
| 340 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 341 | static enum ssl_hs_wait_t do_process_encrypted_extensions(SSL_HANDSHAKE *hs) { |
| 342 | SSL *const ssl = hs->ssl; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 343 | if (!ssl_check_message_type(ssl, SSL3_MT_ENCRYPTED_EXTENSIONS)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 344 | return ssl_hs_error; |
| 345 | } |
| 346 | |
| 347 | CBS cbs; |
| 348 | CBS_init(&cbs, ssl->init_msg, ssl->init_num); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 349 | if (!ssl_parse_serverhello_tlsext(hs, &cbs)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 350 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 351 | return ssl_hs_error; |
| 352 | } |
| 353 | if (CBS_len(&cbs) != 0) { |
| 354 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 355 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 356 | return ssl_hs_error; |
| 357 | } |
| 358 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 359 | if (!ssl_hash_current_message(hs)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 360 | return ssl_hs_error; |
| 361 | } |
| 362 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 363 | hs->tls13_state = state_process_certificate_request; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 364 | return ssl_hs_read_message; |
| 365 | } |
| 366 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 367 | static enum ssl_hs_wait_t do_process_certificate_request(SSL_HANDSHAKE *hs) { |
| 368 | SSL *const ssl = hs->ssl; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 369 | /* CertificateRequest may only be sent in non-resumption handshakes. */ |
| 370 | if (ssl->s3->session_reused) { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 371 | hs->tls13_state = state_process_server_finished; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 372 | return ssl_hs_ok; |
| 373 | } |
| 374 | |
| 375 | /* CertificateRequest is optional. */ |
| 376 | if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 377 | hs->tls13_state = state_process_server_certificate; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 378 | return ssl_hs_ok; |
| 379 | } |
| 380 | |
| 381 | CBS cbs, context, supported_signature_algorithms; |
| 382 | CBS_init(&cbs, ssl->init_msg, ssl->init_num); |
| 383 | if (!CBS_get_u8_length_prefixed(&cbs, &context) || |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 384 | /* The request context is always empty during the handshake. */ |
| 385 | CBS_len(&context) != 0 || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 386 | !CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) || |
| 387 | CBS_len(&supported_signature_algorithms) == 0 || |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 388 | !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 389 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 390 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 391 | return ssl_hs_error; |
| 392 | } |
| 393 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 394 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 395 | STACK_OF(X509_NAME) *ca_sk = ssl_parse_client_CA_list(ssl, &alert, &cbs); |
| 396 | if (ca_sk == NULL) { |
| 397 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 398 | return ssl_hs_error; |
| 399 | } |
| 400 | |
| 401 | /* Ignore extensions. */ |
| 402 | CBS extensions; |
| 403 | if (!CBS_get_u16_length_prefixed(&cbs, &extensions) || |
| 404 | CBS_len(&cbs) != 0) { |
| 405 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 406 | sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 407 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 408 | return ssl_hs_error; |
| 409 | } |
| 410 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 411 | hs->cert_request = 1; |
| 412 | sk_X509_NAME_pop_free(hs->ca_names, X509_NAME_free); |
| 413 | hs->ca_names = ca_sk; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 414 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 415 | if (!ssl_hash_current_message(hs)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 416 | return ssl_hs_error; |
| 417 | } |
| 418 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 419 | hs->tls13_state = state_process_server_certificate; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 420 | return ssl_hs_read_message; |
| 421 | } |
| 422 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 423 | static enum ssl_hs_wait_t do_process_server_certificate(SSL_HANDSHAKE *hs) { |
| 424 | SSL *const ssl = hs->ssl; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 425 | if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE) || |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 426 | !tls13_process_certificate(hs, 0 /* certificate required */) || |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 427 | !ssl_hash_current_message(hs)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 428 | return ssl_hs_error; |
| 429 | } |
| 430 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 431 | hs->tls13_state = state_process_server_certificate_verify; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 432 | return ssl_hs_read_message; |
| 433 | } |
| 434 | |
| 435 | static enum ssl_hs_wait_t do_process_server_certificate_verify( |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 436 | SSL_HANDSHAKE *hs) { |
| 437 | SSL *const ssl = hs->ssl; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 438 | if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY) || |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 439 | !tls13_process_certificate_verify(hs) || |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 440 | !ssl_hash_current_message(hs)) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 441 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 442 | } |
| 443 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 444 | hs->tls13_state = state_process_server_finished; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 445 | return ssl_hs_read_message; |
| 446 | } |
| 447 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 448 | static enum ssl_hs_wait_t do_process_server_finished(SSL_HANDSHAKE *hs) { |
| 449 | SSL *const ssl = hs->ssl; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 450 | if (!ssl_check_message_type(ssl, SSL3_MT_FINISHED) || |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 451 | !tls13_process_finished(hs) || |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 452 | !ssl_hash_current_message(hs) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 453 | /* Update the secret to the master secret and derive traffic keys. */ |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 454 | !tls13_advance_key_schedule(hs, kZeroes, hs->hash_len) || |
| 455 | !tls13_derive_application_secrets(hs)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 456 | return ssl_hs_error; |
| 457 | } |
| 458 | |
| 459 | ssl->method->received_flight(ssl); |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 460 | hs->tls13_state = state_send_client_certificate; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 461 | return ssl_hs_ok; |
| 462 | } |
| 463 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 464 | 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] | 465 | SSL *const ssl = hs->ssl; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 466 | /* The peer didn't request a certificate. */ |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 467 | if (!hs->cert_request) { |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 468 | hs->tls13_state = state_complete_second_flight; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 469 | return ssl_hs_ok; |
| 470 | } |
| 471 | |
| 472 | /* Call cert_cb to update the certificate. */ |
| 473 | if (ssl->cert->cert_cb != NULL) { |
| 474 | int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg); |
| 475 | if (rv == 0) { |
| 476 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 477 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
| 478 | return ssl_hs_error; |
| 479 | } |
| 480 | if (rv < 0) { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 481 | hs->tls13_state = state_send_client_certificate; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 482 | return ssl_hs_x509_lookup; |
| 483 | } |
| 484 | } |
| 485 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 486 | if (!ssl_auto_chain_if_needed(ssl) || |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 487 | !tls13_add_certificate(hs)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 488 | return ssl_hs_error; |
| 489 | } |
| 490 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 491 | hs->tls13_state = state_send_client_certificate_verify; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 492 | return ssl_hs_ok; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 493 | } |
| 494 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 495 | 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] | 496 | int is_first_run) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 497 | SSL *const ssl = hs->ssl; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 498 | /* Don't send CertificateVerify if there is no certificate. */ |
| 499 | if (!ssl_has_certificate(ssl)) { |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 500 | hs->tls13_state = state_complete_second_flight; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 501 | return ssl_hs_ok; |
| 502 | } |
| 503 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 504 | switch (tls13_add_certificate_verify(hs, is_first_run)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 505 | case ssl_private_key_success: |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 506 | hs->tls13_state = state_complete_second_flight; |
| 507 | return ssl_hs_ok; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 508 | |
| 509 | case ssl_private_key_retry: |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 510 | hs->tls13_state = state_complete_client_certificate_verify; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 511 | return ssl_hs_private_key_operation; |
| 512 | |
| 513 | case ssl_private_key_failure: |
| 514 | return ssl_hs_error; |
| 515 | } |
| 516 | |
| 517 | assert(0); |
| 518 | return ssl_hs_error; |
| 519 | } |
| 520 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 521 | 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] | 522 | SSL *const ssl = hs->ssl; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 523 | |
| 524 | /* Send a Channel ID assertion if necessary. */ |
| 525 | if (ssl->s3->tlsext_channel_id_valid) { |
| 526 | if (!ssl_do_channel_id_callback(ssl)) { |
| 527 | hs->tls13_state = state_complete_second_flight; |
| 528 | return ssl_hs_error; |
| 529 | } |
| 530 | |
| 531 | if (ssl->tlsext_channel_id_private == NULL) { |
| 532 | return ssl_hs_channel_id_lookup; |
| 533 | } |
| 534 | |
| 535 | CBB cbb, body; |
| 536 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) || |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 537 | !tls1_write_channel_id(hs, &body) || |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 538 | !ssl_add_message_cbb(ssl, &cbb)) { |
| 539 | CBB_cleanup(&cbb); |
| 540 | return ssl_hs_error; |
| 541 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 542 | } |
| 543 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 544 | /* Send a Finished message. */ |
| 545 | if (!tls13_add_finished(hs)) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 546 | return ssl_hs_error; |
| 547 | } |
| 548 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 549 | /* Derive the final keys and enable them. */ |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 550 | if (!tls13_set_traffic_key(ssl, evp_aead_open, hs->server_traffic_secret_0, |
| 551 | hs->hash_len) || |
| 552 | !tls13_set_traffic_key(ssl, evp_aead_seal, hs->client_traffic_secret_0, |
| 553 | hs->hash_len) || |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 554 | !tls13_derive_resumption_secret(hs)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 555 | return ssl_hs_error; |
| 556 | } |
| 557 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 558 | hs->tls13_state = state_done; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 559 | return ssl_hs_flush; |
| 560 | } |
| 561 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 562 | enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs) { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 563 | while (hs->tls13_state != state_done) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 564 | enum ssl_hs_wait_t ret = ssl_hs_error; |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 565 | enum client_hs_state_t state = hs->tls13_state; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 566 | switch (state) { |
| 567 | case state_process_hello_retry_request: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 568 | ret = do_process_hello_retry_request(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 569 | break; |
| 570 | case state_send_second_client_hello: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 571 | ret = do_send_second_client_hello(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 572 | break; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 573 | case state_process_server_hello: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 574 | ret = do_process_server_hello(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 575 | break; |
| 576 | case state_process_encrypted_extensions: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 577 | ret = do_process_encrypted_extensions(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 578 | break; |
| 579 | case state_process_certificate_request: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 580 | ret = do_process_certificate_request(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 581 | break; |
| 582 | case state_process_server_certificate: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 583 | ret = do_process_server_certificate(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 584 | break; |
| 585 | case state_process_server_certificate_verify: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 586 | ret = do_process_server_certificate_verify(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 587 | break; |
| 588 | case state_process_server_finished: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 589 | ret = do_process_server_finished(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 590 | break; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 591 | case state_send_client_certificate: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 592 | ret = do_send_client_certificate(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 593 | break; |
| 594 | case state_send_client_certificate_verify: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 595 | ret = do_send_client_certificate_verify(hs, 1 /* first run */); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 596 | break; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 597 | case state_complete_client_certificate_verify: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 598 | ret = do_send_client_certificate_verify(hs, 0 /* complete */); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 599 | break; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 600 | case state_complete_second_flight: |
| 601 | ret = do_complete_second_flight(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 602 | break; |
| 603 | case state_done: |
| 604 | ret = ssl_hs_ok; |
| 605 | break; |
| 606 | } |
| 607 | |
| 608 | if (ret != ssl_hs_ok) { |
| 609 | return ret; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | return ssl_hs_ok; |
| 614 | } |
| 615 | |
| 616 | int tls13_process_new_session_ticket(SSL *ssl) { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 617 | int ret = 0; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 618 | SSL_SESSION *session = SSL_SESSION_dup(ssl->s3->established_session, |
| 619 | SSL_SESSION_INCLUDE_NONAUTH); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 620 | if (session == NULL) { |
| 621 | return 0; |
| 622 | } |
| 623 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 624 | ssl_session_rebase_time(ssl, session); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 625 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 626 | uint32_t server_timeout; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 627 | CBS cbs, ticket, extensions; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 628 | CBS_init(&cbs, ssl->init_msg, ssl->init_num); |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 629 | if (!CBS_get_u32(&cbs, &server_timeout) || |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 630 | !CBS_get_u32(&cbs, &session->ticket_age_add) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 631 | !CBS_get_u16_length_prefixed(&cbs, &ticket) || |
| 632 | !CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen) || |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 633 | !CBS_get_u16_length_prefixed(&cbs, &extensions) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 634 | CBS_len(&cbs) != 0) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 635 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 636 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 637 | goto err; |
| 638 | } |
| 639 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 640 | /* Cap the renewable lifetime by the server advertised value. This avoids |
| 641 | * wasting bandwidth on 0-RTT when we know the server will reject it. |
| 642 | * |
| 643 | * TODO(davidben): This dance where we're not sure if long or uint32_t is |
| 644 | * bigger is silly. session->timeout should not be a long to begin with. |
| 645 | * https://crbug.com/boringssl/155. */ |
| 646 | #if LONG_MAX < 0xffffffff |
| 647 | if (server_timeout > LONG_MAX) { |
| 648 | server_timeout = LONG_MAX; |
| 649 | } |
| 650 | #endif |
| 651 | if (session->timeout > (long)server_timeout) { |
| 652 | session->timeout = (long)server_timeout; |
| 653 | } |
| 654 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 655 | /* Parse out the extensions. */ |
| 656 | int have_early_data_info = 0; |
| 657 | CBS early_data_info; |
| 658 | const SSL_EXTENSION_TYPE ext_types[] = { |
| 659 | {TLSEXT_TYPE_ticket_early_data_info, &have_early_data_info, |
| 660 | &early_data_info}, |
| 661 | }; |
| 662 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 663 | uint8_t alert = SSL_AD_DECODE_ERROR; |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 664 | if (!ssl_parse_extensions(&extensions, &alert, ext_types, |
| 665 | OPENSSL_ARRAY_SIZE(ext_types), |
| 666 | 1 /* ignore unknown */)) { |
| 667 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 668 | goto err; |
| 669 | } |
| 670 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 671 | if (have_early_data_info && ssl->ctx->enable_early_data) { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 672 | if (!CBS_get_u32(&early_data_info, &session->ticket_max_early_data) || |
| 673 | CBS_len(&early_data_info) != 0) { |
| 674 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 675 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 676 | goto err; |
| 677 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 678 | } |
| 679 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 680 | session->ticket_age_add_valid = 1; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 681 | session->not_resumable = 0; |
| 682 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 683 | if (ssl->ctx->new_session_cb != NULL && |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 684 | ssl->ctx->new_session_cb(ssl, session)) { |
| 685 | /* |new_session_cb|'s return value signals that it took ownership. */ |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 686 | session = NULL; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 687 | } |
| 688 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 689 | ret = 1; |
| 690 | |
| 691 | err: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 692 | SSL_SESSION_free(session); |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 693 | return ret; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 694 | } |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 695 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 696 | void ssl_clear_tls13_state(SSL_HANDSHAKE *hs) { |
| 697 | SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx); |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 698 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 699 | OPENSSL_free(hs->key_share_bytes); |
| 700 | hs->key_share_bytes = NULL; |
| 701 | hs->key_share_bytes_len = 0; |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 702 | } |