Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2016, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include <openssl/ssl.h> |
| 16 | |
| 17 | #include <assert.h> |
| 18 | #include <string.h> |
| 19 | |
| 20 | #include <openssl/bytestring.h> |
| 21 | #include <openssl/digest.h> |
| 22 | #include <openssl/err.h> |
| 23 | #include <openssl/mem.h> |
| 24 | #include <openssl/stack.h> |
| 25 | #include <openssl/x509.h> |
| 26 | |
| 27 | #include "internal.h" |
| 28 | |
| 29 | |
| 30 | enum client_hs_state_t { |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 31 | state_process_hello_retry_request = 0, |
| 32 | state_send_second_client_hello, |
| 33 | state_flush_second_client_hello, |
| 34 | state_process_server_hello, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 35 | state_process_encrypted_extensions, |
| 36 | state_process_certificate_request, |
| 37 | state_process_server_certificate, |
| 38 | state_process_server_certificate_verify, |
| 39 | state_process_server_finished, |
| 40 | state_certificate_callback, |
| 41 | state_send_client_certificate, |
| 42 | state_send_client_certificate_verify, |
| 43 | state_complete_client_certificate_verify, |
| 44 | state_send_client_finished, |
| 45 | state_flush, |
| 46 | state_done, |
| 47 | }; |
| 48 | |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 49 | static enum ssl_hs_wait_t do_process_hello_retry_request(SSL *ssl, |
| 50 | SSL_HANDSHAKE *hs) { |
| 51 | if (ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) { |
| 52 | hs->state = state_process_server_hello; |
| 53 | return ssl_hs_ok; |
| 54 | } |
| 55 | |
| 56 | CBS cbs, extensions; |
| 57 | uint16_t server_wire_version, cipher_suite, group_id; |
| 58 | CBS_init(&cbs, ssl->init_msg, ssl->init_num); |
| 59 | if (!CBS_get_u16(&cbs, &server_wire_version) || |
| 60 | !CBS_get_u16(&cbs, &cipher_suite) || |
| 61 | !CBS_get_u16(&cbs, &group_id) || |
| 62 | /* We do not currently parse any HelloRetryRequest extensions. */ |
| 63 | !CBS_get_u16_length_prefixed(&cbs, &extensions) || |
| 64 | CBS_len(&cbs) != 0) { |
| 65 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 66 | return ssl_hs_error; |
| 67 | } |
| 68 | |
| 69 | /* TODO(svaldez): Don't do early_data on HelloRetryRequest. */ |
| 70 | |
| 71 | const uint16_t *groups; |
| 72 | size_t groups_len; |
| 73 | tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len); |
| 74 | int found = 0; |
| 75 | for (size_t i = 0; i < groups_len; i++) { |
| 76 | if (groups[i] == group_id) { |
| 77 | found = 1; |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if (!found) { |
| 83 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 84 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
| 85 | return ssl_hs_error; |
| 86 | } |
| 87 | |
| 88 | for (size_t i = 0; i < ssl->s3->hs->groups_len; i++) { |
| 89 | /* Check that the HelloRetryRequest does not request a key share that was |
| 90 | * provided in the initial ClientHello. |
| 91 | * |
| 92 | * TODO(svaldez): Don't enforce this check when the HelloRetryRequest is due |
| 93 | * to a cookie. */ |
| 94 | if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->groups[i]) == group_id) { |
| 95 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 96 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
| 97 | return ssl_hs_error; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | ssl_handshake_clear_groups(ssl->s3->hs); |
| 102 | ssl->s3->hs->retry_group = group_id; |
| 103 | |
| 104 | hs->state = state_send_second_client_hello; |
| 105 | return ssl_hs_ok; |
| 106 | } |
| 107 | |
| 108 | static enum ssl_hs_wait_t do_send_second_client_hello(SSL *ssl, |
| 109 | SSL_HANDSHAKE *hs) { |
| 110 | CBB cbb, body; |
| 111 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CLIENT_HELLO) || |
| 112 | !ssl_add_client_hello_body(ssl, &body) || |
| 113 | !ssl->method->finish_message(ssl, &cbb)) { |
| 114 | CBB_cleanup(&cbb); |
| 115 | return ssl_hs_error; |
| 116 | } |
| 117 | |
| 118 | hs->state = state_flush_second_client_hello; |
| 119 | return ssl_hs_write_message; |
| 120 | } |
| 121 | |
| 122 | static enum ssl_hs_wait_t do_flush_second_client_hello(SSL *ssl, |
| 123 | SSL_HANDSHAKE *hs) { |
| 124 | hs->state = state_process_server_hello; |
| 125 | return ssl_hs_flush_and_read_message; |
| 126 | } |
| 127 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 128 | static enum ssl_hs_wait_t do_process_server_hello(SSL *ssl, SSL_HANDSHAKE *hs) { |
| 129 | if (!tls13_check_message_type(ssl, SSL3_MT_SERVER_HELLO)) { |
| 130 | return ssl_hs_error; |
| 131 | } |
| 132 | |
| 133 | CBS cbs, server_random, extensions; |
| 134 | uint16_t server_wire_version; |
| 135 | uint16_t cipher_suite; |
| 136 | CBS_init(&cbs, ssl->init_msg, ssl->init_num); |
| 137 | if (!CBS_get_u16(&cbs, &server_wire_version) || |
| 138 | !CBS_get_bytes(&cbs, &server_random, SSL3_RANDOM_SIZE) || |
| 139 | !CBS_get_u16(&cbs, &cipher_suite) || |
| 140 | !CBS_get_u16_length_prefixed(&cbs, &extensions) || |
| 141 | CBS_len(&cbs) != 0) { |
| 142 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 143 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 144 | return ssl_hs_error; |
| 145 | } |
| 146 | |
| 147 | if (server_wire_version != ssl->version) { |
| 148 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 149 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 150 | return ssl_hs_error; |
| 151 | } |
| 152 | |
| 153 | /* Parse out the extensions. */ |
| 154 | int have_key_share = 0; |
| 155 | CBS key_share; |
| 156 | while (CBS_len(&extensions) != 0) { |
| 157 | uint16_t type; |
| 158 | CBS extension; |
| 159 | if (!CBS_get_u16(&extensions, &type) || |
| 160 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 161 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 162 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 163 | return ssl_hs_error; |
| 164 | } |
| 165 | |
| 166 | switch (type) { |
| 167 | case TLSEXT_TYPE_key_share: |
| 168 | if (have_key_share) { |
| 169 | OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_EXTENSION); |
| 170 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 171 | return ssl_hs_error; |
| 172 | } |
| 173 | key_share = extension; |
| 174 | have_key_share = 1; |
| 175 | break; |
| 176 | default: |
| 177 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 178 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION); |
| 179 | return ssl_hs_error; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | assert(ssl->s3->have_version); |
| 184 | memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE); |
| 185 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 186 | SSL_set_session(ssl, NULL); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 187 | if (!ssl_get_new_session(ssl, 0)) { |
| 188 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 189 | return ssl_hs_error; |
| 190 | } |
| 191 | |
| 192 | const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite); |
| 193 | if (cipher == NULL) { |
| 194 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED); |
| 195 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 196 | return ssl_hs_error; |
| 197 | } |
| 198 | |
| 199 | /* Check if the cipher is disabled. */ |
| 200 | if ((cipher->algorithm_mkey & ssl->cert->mask_k) || |
| 201 | (cipher->algorithm_auth & ssl->cert->mask_a) || |
| 202 | SSL_CIPHER_get_min_version(cipher) > ssl3_protocol_version(ssl) || |
| 203 | SSL_CIPHER_get_max_version(cipher) < ssl3_protocol_version(ssl) || |
| 204 | !sk_SSL_CIPHER_find(ssl_get_ciphers_by_id(ssl), NULL, cipher)) { |
| 205 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
| 206 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 207 | return ssl_hs_error; |
| 208 | } |
| 209 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 210 | ssl->s3->new_session->cipher = cipher; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 211 | ssl->s3->tmp.new_cipher = cipher; |
| 212 | |
| 213 | /* The PRF hash is now known. Set up the key schedule. */ |
| 214 | static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0}; |
| 215 | size_t hash_len = |
| 216 | EVP_MD_size(ssl_get_handshake_digest(ssl_get_algorithm_prf(ssl))); |
| 217 | if (!tls13_init_key_schedule(ssl, kZeroes, hash_len)) { |
| 218 | return ssl_hs_error; |
| 219 | } |
| 220 | |
| 221 | /* Resolve PSK and incorporate it into the secret. */ |
| 222 | if (cipher->algorithm_auth == SSL_aPSK) { |
| 223 | /* TODO(davidben): Support PSK. */ |
| 224 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 225 | return ssl_hs_error; |
| 226 | } else if (!tls13_advance_key_schedule(ssl, kZeroes, hash_len)) { |
| 227 | return ssl_hs_error; |
| 228 | } |
| 229 | |
| 230 | /* Resolve ECDHE and incorporate it into the secret. */ |
| 231 | if (cipher->algorithm_mkey == SSL_kECDHE) { |
| 232 | if (!have_key_share) { |
| 233 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE); |
| 234 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION); |
| 235 | return ssl_hs_error; |
| 236 | } |
| 237 | |
| 238 | uint8_t *dhe_secret; |
| 239 | size_t dhe_secret_len; |
| 240 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 241 | if (!ext_key_share_parse_serverhello(ssl, &dhe_secret, &dhe_secret_len, |
| 242 | &alert, &key_share)) { |
| 243 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 244 | return ssl_hs_error; |
| 245 | } |
| 246 | |
| 247 | int ok = tls13_advance_key_schedule(ssl, dhe_secret, dhe_secret_len); |
| 248 | OPENSSL_free(dhe_secret); |
| 249 | if (!ok) { |
| 250 | return ssl_hs_error; |
| 251 | } |
| 252 | } else { |
| 253 | if (have_key_share) { |
| 254 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 255 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION); |
| 256 | return ssl_hs_error; |
| 257 | } |
| 258 | if (!tls13_advance_key_schedule(ssl, kZeroes, hash_len)) { |
| 259 | return ssl_hs_error; |
| 260 | } |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* If there was no HelloRetryRequest, the version negotiation logic has |
| 264 | * already hashed the message. */ |
| 265 | if (ssl->s3->hs->retry_group != 0 && |
| 266 | !ssl->method->hash_current_message(ssl)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 267 | return ssl_hs_error; |
| 268 | } |
| 269 | |
| 270 | if (!tls13_set_handshake_traffic(ssl)) { |
| 271 | return ssl_hs_error; |
| 272 | } |
| 273 | |
| 274 | hs->state = state_process_encrypted_extensions; |
| 275 | return ssl_hs_read_message; |
| 276 | } |
| 277 | |
| 278 | static enum ssl_hs_wait_t do_process_encrypted_extensions(SSL *ssl, |
| 279 | SSL_HANDSHAKE *hs) { |
| 280 | if (!tls13_check_message_type(ssl, SSL3_MT_ENCRYPTED_EXTENSIONS)) { |
| 281 | return ssl_hs_error; |
| 282 | } |
| 283 | |
| 284 | CBS cbs; |
| 285 | CBS_init(&cbs, ssl->init_msg, ssl->init_num); |
| 286 | if (!ssl_parse_serverhello_tlsext(ssl, &cbs)) { |
| 287 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 288 | return ssl_hs_error; |
| 289 | } |
| 290 | if (CBS_len(&cbs) != 0) { |
| 291 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 292 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 293 | return ssl_hs_error; |
| 294 | } |
| 295 | |
| 296 | if (!ssl->method->hash_current_message(ssl)) { |
| 297 | return ssl_hs_error; |
| 298 | } |
| 299 | |
| 300 | hs->state = state_process_certificate_request; |
| 301 | return ssl_hs_read_message; |
| 302 | } |
| 303 | |
| 304 | static enum ssl_hs_wait_t do_process_certificate_request(SSL *ssl, |
| 305 | SSL_HANDSHAKE *hs) { |
| 306 | ssl->s3->tmp.cert_request = 0; |
| 307 | |
| 308 | /* CertificateRequest may only be sent in certificate-based ciphers. */ |
| 309 | if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
| 310 | hs->state = state_process_server_finished; |
| 311 | return ssl_hs_ok; |
| 312 | } |
| 313 | |
| 314 | /* CertificateRequest is optional. */ |
| 315 | if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) { |
| 316 | hs->state = state_process_server_certificate; |
| 317 | return ssl_hs_ok; |
| 318 | } |
| 319 | |
| 320 | CBS cbs, context, supported_signature_algorithms; |
| 321 | CBS_init(&cbs, ssl->init_msg, ssl->init_num); |
| 322 | if (!CBS_get_u8_length_prefixed(&cbs, &context) || |
| 323 | !CBS_stow(&context, &ssl->s3->hs->cert_context, |
| 324 | &ssl->s3->hs->cert_context_len) || |
| 325 | !CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) || |
David Benjamin | 4890165 | 2016-08-01 12:12:47 -0400 | [diff] [blame^] | 326 | CBS_len(&supported_signature_algorithms) == 0 || |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 327 | !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) { |
| 328 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 329 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 330 | return ssl_hs_error; |
| 331 | } |
| 332 | |
| 333 | uint8_t alert; |
| 334 | STACK_OF(X509_NAME) *ca_sk = ssl_parse_client_CA_list(ssl, &alert, &cbs); |
| 335 | if (ca_sk == NULL) { |
| 336 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 337 | return ssl_hs_error; |
| 338 | } |
| 339 | |
| 340 | /* Ignore extensions. */ |
| 341 | CBS extensions; |
| 342 | if (!CBS_get_u16_length_prefixed(&cbs, &extensions) || |
| 343 | CBS_len(&cbs) != 0) { |
| 344 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 345 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 346 | return ssl_hs_error; |
| 347 | } |
| 348 | |
| 349 | ssl->s3->tmp.cert_request = 1; |
| 350 | sk_X509_NAME_pop_free(ssl->s3->tmp.ca_names, X509_NAME_free); |
| 351 | ssl->s3->tmp.ca_names = ca_sk; |
| 352 | |
| 353 | if (!ssl->method->hash_current_message(ssl)) { |
| 354 | return ssl_hs_error; |
| 355 | } |
| 356 | |
| 357 | hs->state = state_process_server_certificate; |
| 358 | return ssl_hs_read_message; |
| 359 | } |
| 360 | |
| 361 | static enum ssl_hs_wait_t do_process_server_certificate(SSL *ssl, |
| 362 | SSL_HANDSHAKE *hs) { |
| 363 | if (!tls13_check_message_type(ssl, SSL3_MT_CERTIFICATE) || |
| 364 | !tls13_process_certificate(ssl) || |
| 365 | !ssl->method->hash_current_message(ssl)) { |
| 366 | return ssl_hs_error; |
| 367 | } |
| 368 | |
| 369 | hs->state = state_process_server_certificate_verify; |
| 370 | return ssl_hs_read_message; |
| 371 | } |
| 372 | |
| 373 | static enum ssl_hs_wait_t do_process_server_certificate_verify( |
| 374 | SSL *ssl, SSL_HANDSHAKE *hs) { |
| 375 | if (!tls13_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY) || |
| 376 | !tls13_process_certificate_verify(ssl) || |
| 377 | !ssl->method->hash_current_message(ssl)) { |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | hs->state = state_process_server_finished; |
| 382 | return ssl_hs_read_message; |
| 383 | } |
| 384 | |
| 385 | static enum ssl_hs_wait_t do_process_server_finished(SSL *ssl, |
| 386 | SSL_HANDSHAKE *hs) { |
| 387 | static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0}; |
| 388 | |
| 389 | if (!tls13_check_message_type(ssl, SSL3_MT_FINISHED) || |
| 390 | !tls13_process_finished(ssl) || |
| 391 | !ssl->method->hash_current_message(ssl) || |
| 392 | /* Update the secret to the master secret and derive traffic keys. */ |
| 393 | !tls13_advance_key_schedule(ssl, kZeroes, hs->hash_len) || |
| 394 | !tls13_derive_traffic_secret_0(ssl)) { |
| 395 | return ssl_hs_error; |
| 396 | } |
| 397 | |
David Benjamin | 613fe3b | 2016-07-22 17:39:29 +0200 | [diff] [blame] | 398 | ssl->method->received_flight(ssl); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 399 | hs->state = state_certificate_callback; |
| 400 | return ssl_hs_ok; |
| 401 | } |
| 402 | |
| 403 | static enum ssl_hs_wait_t do_certificate_callback(SSL *ssl, SSL_HANDSHAKE *hs) { |
| 404 | /* The peer didn't request a certificate. */ |
| 405 | if (!ssl->s3->tmp.cert_request) { |
| 406 | hs->state = state_send_client_finished; |
| 407 | return ssl_hs_ok; |
| 408 | } |
| 409 | |
| 410 | /* Call cert_cb to update the certificate. */ |
| 411 | if (ssl->cert->cert_cb != NULL) { |
| 412 | int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg); |
| 413 | if (rv == 0) { |
| 414 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 415 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
| 416 | return ssl_hs_error; |
| 417 | } |
| 418 | if (rv < 0) { |
| 419 | hs->state = state_certificate_callback; |
| 420 | return ssl_hs_x509_lookup; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | hs->state = state_send_client_certificate; |
| 425 | return ssl_hs_ok; |
| 426 | } |
| 427 | |
| 428 | static enum ssl_hs_wait_t do_send_client_certificate(SSL *ssl, |
| 429 | SSL_HANDSHAKE *hs) { |
David Benjamin | 13f1ebe | 2016-07-20 10:11:04 +0200 | [diff] [blame] | 430 | /* Call client_cert_cb to update the certificate. */ |
| 431 | int should_retry; |
| 432 | if (!ssl_do_client_cert_cb(ssl, &should_retry)) { |
| 433 | if (should_retry) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 434 | hs->state = state_send_client_certificate; |
| 435 | return ssl_hs_x509_lookup; |
| 436 | } |
David Benjamin | 13f1ebe | 2016-07-20 10:11:04 +0200 | [diff] [blame] | 437 | return ssl_hs_error; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | if (!tls13_prepare_certificate(ssl)) { |
| 441 | return ssl_hs_error; |
| 442 | } |
| 443 | |
| 444 | hs->state = state_send_client_certificate_verify; |
| 445 | return ssl_hs_write_message; |
| 446 | } |
| 447 | |
| 448 | static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL *ssl, |
| 449 | SSL_HANDSHAKE *hs, |
| 450 | int is_first_run) { |
| 451 | /* Don't send CertificateVerify if there is no certificate. */ |
| 452 | if (!ssl_has_certificate(ssl)) { |
| 453 | hs->state = state_send_client_finished; |
| 454 | return ssl_hs_ok; |
| 455 | } |
| 456 | |
| 457 | switch (tls13_prepare_certificate_verify(ssl, is_first_run)) { |
| 458 | case ssl_private_key_success: |
| 459 | hs->state = state_send_client_finished; |
| 460 | return ssl_hs_write_message; |
| 461 | |
| 462 | case ssl_private_key_retry: |
| 463 | hs->state = state_complete_client_certificate_verify; |
| 464 | return ssl_hs_private_key_operation; |
| 465 | |
| 466 | case ssl_private_key_failure: |
| 467 | return ssl_hs_error; |
| 468 | } |
| 469 | |
| 470 | assert(0); |
| 471 | return ssl_hs_error; |
| 472 | } |
| 473 | |
| 474 | static enum ssl_hs_wait_t do_send_client_finished(SSL *ssl, SSL_HANDSHAKE *hs) { |
| 475 | if (!tls13_prepare_finished(ssl)) { |
| 476 | return ssl_hs_error; |
| 477 | } |
| 478 | |
| 479 | hs->state = state_flush; |
| 480 | return ssl_hs_write_message; |
| 481 | } |
| 482 | |
| 483 | static enum ssl_hs_wait_t do_flush(SSL *ssl, SSL_HANDSHAKE *hs) { |
| 484 | if (!tls13_set_traffic_key(ssl, type_data, evp_aead_open, |
| 485 | hs->traffic_secret_0, hs->hash_len) || |
| 486 | !tls13_set_traffic_key(ssl, type_data, evp_aead_seal, |
| 487 | hs->traffic_secret_0, hs->hash_len) || |
| 488 | !tls13_finalize_keys(ssl)) { |
| 489 | return ssl_hs_error; |
| 490 | } |
| 491 | |
| 492 | hs->state = state_done; |
| 493 | return ssl_hs_flush; |
| 494 | } |
| 495 | |
| 496 | enum ssl_hs_wait_t tls13_client_handshake(SSL *ssl) { |
| 497 | SSL_HANDSHAKE *hs = ssl->s3->hs; |
| 498 | |
| 499 | while (hs->state != state_done) { |
| 500 | enum ssl_hs_wait_t ret = ssl_hs_error; |
| 501 | enum client_hs_state_t state = hs->state; |
| 502 | switch (state) { |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 503 | case state_process_hello_retry_request: |
| 504 | ret = do_process_hello_retry_request(ssl, hs); |
| 505 | break; |
| 506 | case state_send_second_client_hello: |
| 507 | ret = do_send_second_client_hello(ssl, hs); |
| 508 | break; |
| 509 | case state_flush_second_client_hello: |
| 510 | ret = do_flush_second_client_hello(ssl, hs); |
| 511 | break; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 512 | case state_process_server_hello: |
| 513 | ret = do_process_server_hello(ssl, hs); |
| 514 | break; |
| 515 | case state_process_encrypted_extensions: |
| 516 | ret = do_process_encrypted_extensions(ssl, hs); |
| 517 | break; |
| 518 | case state_process_certificate_request: |
| 519 | ret = do_process_certificate_request(ssl, hs); |
| 520 | break; |
| 521 | case state_process_server_certificate: |
| 522 | ret = do_process_server_certificate(ssl, hs); |
| 523 | break; |
| 524 | case state_process_server_certificate_verify: |
| 525 | ret = do_process_server_certificate_verify(ssl, hs); |
| 526 | break; |
| 527 | case state_process_server_finished: |
| 528 | ret = do_process_server_finished(ssl, hs); |
| 529 | break; |
| 530 | case state_certificate_callback: |
| 531 | ret = do_certificate_callback(ssl, hs); |
| 532 | break; |
| 533 | case state_send_client_certificate: |
| 534 | ret = do_send_client_certificate(ssl, hs); |
| 535 | break; |
| 536 | case state_send_client_certificate_verify: |
| 537 | ret = do_send_client_certificate_verify(ssl, hs, 1 /* first run */); |
| 538 | break; |
| 539 | case state_complete_client_certificate_verify: |
| 540 | ret = do_send_client_certificate_verify(ssl, hs, 0 /* complete */); |
| 541 | break; |
| 542 | case state_send_client_finished: |
| 543 | ret = do_send_client_finished(ssl, hs); |
| 544 | break; |
| 545 | case state_flush: |
| 546 | ret = do_flush(ssl, hs); |
| 547 | break; |
| 548 | case state_done: |
| 549 | ret = ssl_hs_ok; |
| 550 | break; |
| 551 | } |
| 552 | |
| 553 | if (ret != ssl_hs_ok) { |
| 554 | return ret; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | return ssl_hs_ok; |
| 559 | } |