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/err.h> |
| 22 | #include <openssl/hkdf.h> |
| 23 | #include <openssl/mem.h> |
| 24 | #include <openssl/stack.h> |
| 25 | #include <openssl/x509.h> |
| 26 | #include <openssl/x509v3.h> |
| 27 | |
| 28 | #include "internal.h" |
| 29 | |
| 30 | |
Steven Valdez | 32635b8 | 2016-08-16 11:25:03 -0400 | [diff] [blame] | 31 | /* kMaxKeyUpdates is the number of consecutive KeyUpdates that will be |
| 32 | * processed. Without this limit an attacker could force unbounded processing |
| 33 | * without being able to return application data. */ |
| 34 | static const uint8_t kMaxKeyUpdates = 32; |
| 35 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 36 | int tls13_handshake(SSL *ssl) { |
| 37 | SSL_HANDSHAKE *hs = ssl->s3->hs; |
| 38 | |
| 39 | for (;;) { |
| 40 | /* Resolve the operation the handshake was waiting on. */ |
| 41 | switch (hs->wait) { |
| 42 | case ssl_hs_error: |
| 43 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 44 | return -1; |
| 45 | |
David Benjamin | f2401eb | 2016-07-18 22:25:05 +0200 | [diff] [blame] | 46 | case ssl_hs_flush: |
| 47 | case ssl_hs_flush_and_read_message: { |
| 48 | int ret = BIO_flush(ssl->wbio); |
| 49 | if (ret <= 0) { |
| 50 | ssl->rwstate = SSL_WRITING; |
| 51 | return ret; |
| 52 | } |
| 53 | if (hs->wait != ssl_hs_flush_and_read_message) { |
| 54 | break; |
| 55 | } |
David Benjamin | 613fe3b | 2016-07-22 17:39:29 +0200 | [diff] [blame] | 56 | ssl->method->expect_flight(ssl); |
David Benjamin | f2401eb | 2016-07-18 22:25:05 +0200 | [diff] [blame] | 57 | hs->wait = ssl_hs_read_message; |
| 58 | /* Fall-through. */ |
| 59 | } |
| 60 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 61 | case ssl_hs_read_message: { |
| 62 | int ret = ssl->method->ssl_get_message(ssl, -1, ssl_dont_hash_message); |
| 63 | if (ret <= 0) { |
| 64 | return ret; |
| 65 | } |
| 66 | break; |
| 67 | } |
| 68 | |
| 69 | case ssl_hs_write_message: { |
| 70 | int ret = ssl->method->write_message(ssl); |
| 71 | if (ret <= 0) { |
| 72 | return ret; |
| 73 | } |
| 74 | break; |
| 75 | } |
| 76 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 77 | case ssl_hs_x509_lookup: |
| 78 | ssl->rwstate = SSL_X509_LOOKUP; |
| 79 | hs->wait = ssl_hs_ok; |
| 80 | return -1; |
| 81 | |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 82 | case ssl_hs_channel_id_lookup: |
| 83 | ssl->rwstate = SSL_CHANNEL_ID_LOOKUP; |
| 84 | hs->wait = ssl_hs_ok; |
| 85 | return -1; |
| 86 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 87 | case ssl_hs_private_key_operation: |
| 88 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
| 89 | hs->wait = ssl_hs_ok; |
| 90 | return -1; |
| 91 | |
| 92 | case ssl_hs_ok: |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | /* Run the state machine again. */ |
| 97 | hs->wait = hs->do_handshake(ssl); |
| 98 | if (hs->wait == ssl_hs_error) { |
| 99 | /* Don't loop around to avoid a stray |SSL_R_SSL_HANDSHAKE_FAILURE| the |
| 100 | * first time around. */ |
| 101 | return -1; |
| 102 | } |
| 103 | if (hs->wait == ssl_hs_ok) { |
| 104 | /* The handshake has completed. */ |
| 105 | return 1; |
| 106 | } |
| 107 | |
| 108 | /* Otherwise, loop to the beginning and resolve what was blocking the |
| 109 | * handshake. */ |
| 110 | } |
| 111 | } |
| 112 | |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 113 | int tls13_get_cert_verify_signature_input( |
| 114 | SSL *ssl, uint8_t **out, size_t *out_len, |
| 115 | enum ssl_cert_verify_context_t cert_verify_context) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 116 | CBB cbb; |
| 117 | if (!CBB_init(&cbb, 64 + 33 + 1 + 2 * EVP_MAX_MD_SIZE)) { |
| 118 | goto err; |
| 119 | } |
| 120 | |
| 121 | for (size_t i = 0; i < 64; i++) { |
| 122 | if (!CBB_add_u8(&cbb, 0x20)) { |
| 123 | goto err; |
| 124 | } |
| 125 | } |
| 126 | |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 127 | const uint8_t *context; |
| 128 | size_t context_len; |
| 129 | if (cert_verify_context == ssl_cert_verify_server) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 130 | /* Include the NUL byte. */ |
| 131 | static const char kContext[] = "TLS 1.3, server CertificateVerify"; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 132 | context = (const uint8_t *)kContext; |
| 133 | context_len = sizeof(kContext); |
| 134 | } else if (cert_verify_context == ssl_cert_verify_client) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 135 | static const char kContext[] = "TLS 1.3, client CertificateVerify"; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 136 | context = (const uint8_t *)kContext; |
| 137 | context_len = sizeof(kContext); |
| 138 | } else if (cert_verify_context == ssl_cert_verify_channel_id) { |
| 139 | static const char kContext[] = "TLS 1.3, Channel ID"; |
| 140 | context = (const uint8_t *)kContext; |
| 141 | context_len = sizeof(kContext); |
| 142 | } else { |
| 143 | goto err; |
| 144 | } |
| 145 | |
| 146 | if (!CBB_add_bytes(&cbb, context, context_len)) { |
| 147 | goto err; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 148 | } |
| 149 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame^] | 150 | uint8_t context_hash[EVP_MAX_MD_SIZE]; |
| 151 | size_t context_hash_len; |
| 152 | if (!tls13_get_context_hash(ssl, context_hash, &context_hash_len) || |
| 153 | !CBB_add_bytes(&cbb, context_hash, context_hash_len) || |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 154 | !CBB_finish(&cbb, out, out_len)) { |
| 155 | goto err; |
| 156 | } |
| 157 | |
| 158 | return 1; |
| 159 | |
| 160 | err: |
| 161 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 162 | CBB_cleanup(&cbb); |
| 163 | return 0; |
| 164 | } |
| 165 | |
David Benjamin | 4087df9 | 2016-08-01 20:16:31 -0400 | [diff] [blame] | 166 | int tls13_process_certificate(SSL *ssl, int allow_anonymous) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame^] | 167 | CBS cbs, context, certificate_list; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 168 | CBS_init(&cbs, ssl->init_msg, ssl->init_num); |
| 169 | if (!CBS_get_u8_length_prefixed(&cbs, &context) || |
| 170 | CBS_len(&context) != 0) { |
| 171 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 172 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 173 | return 0; |
| 174 | } |
| 175 | |
David Benjamin | 4087df9 | 2016-08-01 20:16:31 -0400 | [diff] [blame] | 176 | const int retain_sha256 = |
| 177 | ssl->server && ssl->ctx->retain_only_sha256_of_client_certs; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 178 | int ret = 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame^] | 179 | |
| 180 | STACK_OF(X509) *chain = sk_X509_new_null(); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 181 | if (chain == NULL) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame^] | 182 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 183 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 184 | goto err; |
| 185 | } |
| 186 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame^] | 187 | if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list)) { |
| 188 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 189 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 190 | goto err; |
| 191 | } |
| 192 | |
| 193 | while (CBS_len(&certificate_list) > 0) { |
| 194 | CBS certificate, extensions; |
| 195 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) || |
| 196 | !CBS_get_u16_length_prefixed(&certificate_list, &extensions)) { |
| 197 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 198 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
| 199 | goto err; |
| 200 | } |
| 201 | |
| 202 | /* Retain the hash of the leaf certificate if requested. */ |
| 203 | if (sk_X509_num(chain) == 0 && retain_sha256) { |
| 204 | SHA256(CBS_data(&certificate), CBS_len(&certificate), |
| 205 | ssl->s3->new_session->peer_sha256); |
| 206 | } |
| 207 | |
| 208 | X509 *x = ssl_parse_x509(&certificate); |
| 209 | if (x == NULL || CBS_len(&certificate) != 0) { |
| 210 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 211 | X509_free(x); |
| 212 | goto err; |
| 213 | } |
| 214 | if (!sk_X509_push(chain, x)) { |
| 215 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 216 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 217 | X509_free(x); |
| 218 | goto err; |
| 219 | } |
| 220 | |
| 221 | /* Parse out the extensions. */ |
| 222 | int have_status_request = 0, have_sct = 0; |
| 223 | CBS status_request, sct; |
| 224 | while (CBS_len(&extensions) != 0) { |
| 225 | uint16_t type; |
| 226 | CBS extension; |
| 227 | if (!CBS_get_u16(&extensions, &type) || |
| 228 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 229 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 230 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 231 | goto err; |
| 232 | } |
| 233 | |
| 234 | switch (type) { |
| 235 | case TLSEXT_TYPE_status_request: |
| 236 | if (have_status_request) { |
| 237 | OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_EXTENSION); |
| 238 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 239 | goto err; |
| 240 | } |
| 241 | status_request = extension; |
| 242 | have_status_request = 1; |
| 243 | break; |
| 244 | case TLSEXT_TYPE_certificate_timestamp: |
| 245 | if (have_sct) { |
| 246 | OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_EXTENSION); |
| 247 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 248 | goto err; |
| 249 | } |
| 250 | sct = extension; |
| 251 | have_sct = 1; |
| 252 | break; |
| 253 | default: |
| 254 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 255 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION); |
| 256 | goto err; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /* All Certificate extensions are parsed, but only the leaf extensions are |
| 261 | * stored. */ |
| 262 | if (have_status_request) { |
| 263 | if (ssl->server || !ssl->ocsp_stapling_enabled) { |
| 264 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 265 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION); |
| 266 | goto err; |
| 267 | } |
| 268 | |
| 269 | uint8_t status_type; |
| 270 | CBS ocsp_response; |
| 271 | if (!CBS_get_u8(&status_request, &status_type) || |
| 272 | status_type != TLSEXT_STATUSTYPE_ocsp || |
| 273 | !CBS_get_u24_length_prefixed(&status_request, &ocsp_response) || |
| 274 | CBS_len(&ocsp_response) == 0 || |
| 275 | CBS_len(&status_request) != 0) { |
| 276 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 277 | goto err; |
| 278 | } |
| 279 | |
| 280 | if (sk_X509_num(chain) == 1 && |
| 281 | !CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response, |
| 282 | &ssl->s3->new_session->ocsp_response_length)) { |
| 283 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 284 | goto err; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if (have_sct) { |
| 289 | if (ssl->server || !ssl->signed_cert_timestamps_enabled) { |
| 290 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 291 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION); |
| 292 | goto err; |
| 293 | } |
| 294 | |
| 295 | if (CBS_len(&sct) == 0) { |
| 296 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 297 | goto err; |
| 298 | } |
| 299 | |
| 300 | if (sk_X509_num(chain) == 1 && |
| 301 | !CBS_stow(&sct, |
| 302 | &ssl->s3->new_session->tlsext_signed_cert_timestamp_list, |
| 303 | &ssl->s3->new_session |
| 304 | ->tlsext_signed_cert_timestamp_list_length)) { |
| 305 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 306 | goto err; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 311 | if (CBS_len(&cbs) != 0) { |
| 312 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 313 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 314 | goto err; |
| 315 | } |
| 316 | |
| 317 | if (sk_X509_num(chain) == 0) { |
David Benjamin | 4087df9 | 2016-08-01 20:16:31 -0400 | [diff] [blame] | 318 | if (!allow_anonymous) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 319 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
David Benjamin | 1db9e1b | 2016-10-07 20:51:43 -0400 | [diff] [blame] | 320 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_CERTIFICATE_REQUIRED); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 321 | goto err; |
| 322 | } |
| 323 | |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 324 | /* OpenSSL returns X509_V_OK when no certificates are requested. This is |
David Benjamin | dd634eb | 2016-08-18 16:40:28 -0400 | [diff] [blame] | 325 | * classed by them as a bug, but it's assumed by at least NGINX. */ |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 326 | ssl->s3->new_session->verify_result = X509_V_OK; |
| 327 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 328 | /* No certificate, so nothing more to do. */ |
| 329 | ret = 1; |
| 330 | goto err; |
| 331 | } |
| 332 | |
David Benjamin | 4087df9 | 2016-08-01 20:16:31 -0400 | [diff] [blame] | 333 | ssl->s3->new_session->peer_sha256_valid = retain_sha256; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 334 | |
David Benjamin | 7aa31d6 | 2016-08-08 21:38:32 -0400 | [diff] [blame] | 335 | if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result, |
| 336 | chain)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 337 | goto err; |
| 338 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 339 | |
Adam Langley | c5ac2b6 | 2016-11-07 12:02:35 -0800 | [diff] [blame] | 340 | X509_free(ssl->s3->new_session->x509_peer); |
David Benjamin | 96a16cd | 2016-08-11 12:14:47 -0400 | [diff] [blame] | 341 | X509 *leaf = sk_X509_value(chain, 0); |
| 342 | X509_up_ref(leaf); |
Adam Langley | c5ac2b6 | 2016-11-07 12:02:35 -0800 | [diff] [blame] | 343 | ssl->s3->new_session->x509_peer = leaf; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 344 | |
Adam Langley | c5ac2b6 | 2016-11-07 12:02:35 -0800 | [diff] [blame] | 345 | sk_X509_pop_free(ssl->s3->new_session->x509_chain, X509_free); |
| 346 | ssl->s3->new_session->x509_chain = chain; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 347 | chain = NULL; |
| 348 | |
| 349 | ret = 1; |
| 350 | |
| 351 | err: |
| 352 | sk_X509_pop_free(chain, X509_free); |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | int tls13_process_certificate_verify(SSL *ssl) { |
| 357 | int ret = 0; |
Adam Langley | c5ac2b6 | 2016-11-07 12:02:35 -0800 | [diff] [blame] | 358 | X509 *peer = ssl->s3->new_session->x509_peer; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 359 | EVP_PKEY *pkey = NULL; |
| 360 | uint8_t *msg = NULL; |
| 361 | size_t msg_len; |
| 362 | |
| 363 | /* Filter out unsupported certificate types. */ |
| 364 | pkey = X509_get_pubkey(peer); |
| 365 | if (pkey == NULL) { |
| 366 | goto err; |
| 367 | } |
| 368 | |
| 369 | CBS cbs, signature; |
| 370 | uint16_t signature_algorithm; |
| 371 | CBS_init(&cbs, ssl->init_msg, ssl->init_num); |
| 372 | if (!CBS_get_u16(&cbs, &signature_algorithm) || |
| 373 | !CBS_get_u16_length_prefixed(&cbs, &signature) || |
| 374 | CBS_len(&cbs) != 0) { |
| 375 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 376 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 377 | goto err; |
| 378 | } |
| 379 | |
| 380 | int al; |
| 381 | if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) { |
| 382 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
| 383 | goto err; |
| 384 | } |
| 385 | ssl->s3->tmp.peer_signature_algorithm = signature_algorithm; |
| 386 | |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 387 | if (!tls13_get_cert_verify_signature_input( |
| 388 | ssl, &msg, &msg_len, |
| 389 | ssl->server ? ssl_cert_verify_client : ssl_cert_verify_server)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 390 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 391 | goto err; |
| 392 | } |
| 393 | |
| 394 | int sig_ok = |
| 395 | ssl_public_key_verify(ssl, CBS_data(&signature), CBS_len(&signature), |
| 396 | signature_algorithm, pkey, msg, msg_len); |
David Benjamin | 04aa694 | 2016-08-19 14:51:10 -0400 | [diff] [blame] | 397 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 398 | sig_ok = 1; |
| 399 | ERR_clear_error(); |
| 400 | #endif |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 401 | if (!sig_ok) { |
| 402 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
| 403 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
| 404 | goto err; |
| 405 | } |
| 406 | |
| 407 | ret = 1; |
| 408 | |
| 409 | err: |
| 410 | EVP_PKEY_free(pkey); |
| 411 | OPENSSL_free(msg); |
| 412 | return ret; |
| 413 | } |
| 414 | |
| 415 | int tls13_check_message_type(SSL *ssl, int type) { |
| 416 | if (ssl->s3->tmp.message_type != type) { |
| 417 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 418 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | f2401eb | 2016-07-18 22:25:05 +0200 | [diff] [blame] | 419 | ERR_add_error_dataf("got type %d, wanted type %d", |
| 420 | ssl->s3->tmp.message_type, type); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | return 1; |
| 425 | } |
| 426 | |
| 427 | int tls13_process_finished(SSL *ssl) { |
| 428 | uint8_t verify_data[EVP_MAX_MD_SIZE]; |
| 429 | size_t verify_data_len; |
| 430 | if (!tls13_finished_mac(ssl, verify_data, &verify_data_len, !ssl->server)) { |
| 431 | return 0; |
| 432 | } |
| 433 | |
David Benjamin | 04aa694 | 2016-08-19 14:51:10 -0400 | [diff] [blame] | 434 | int finished_ok = |
| 435 | ssl->init_num == verify_data_len && |
| 436 | CRYPTO_memcmp(verify_data, ssl->init_msg, verify_data_len) == 0; |
| 437 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 438 | finished_ok = 1; |
| 439 | #endif |
| 440 | if (!finished_ok) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 441 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
| 442 | OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED); |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | return 1; |
| 447 | } |
| 448 | |
| 449 | int tls13_prepare_certificate(SSL *ssl) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame^] | 450 | CBB cbb, body, certificate_list; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 451 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CERTIFICATE) || |
David Benjamin | 8a8349b | 2016-08-18 02:32:23 -0400 | [diff] [blame] | 452 | /* The request context is always empty in the handshake. */ |
| 453 | !CBB_add_u8(&body, 0) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame^] | 454 | !CBB_add_u24_length_prefixed(&body, &certificate_list)) { |
| 455 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 456 | goto err; |
| 457 | } |
| 458 | |
| 459 | if (!ssl_has_certificate(ssl)) { |
| 460 | if (!ssl_complete_message(ssl, &cbb)) { |
| 461 | goto err; |
| 462 | } |
| 463 | |
| 464 | return 1; |
| 465 | } |
| 466 | |
| 467 | CERT *cert = ssl->cert; |
| 468 | CBB leaf, extensions; |
| 469 | if (!CBB_add_u24_length_prefixed(&certificate_list, &leaf) || |
| 470 | !ssl_add_cert_to_cbb(&leaf, cert->x509_leaf) || |
| 471 | !CBB_add_u16_length_prefixed(&certificate_list, &extensions)) { |
| 472 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 473 | goto err; |
| 474 | } |
| 475 | |
| 476 | if (ssl->s3->hs->scts_requested && |
| 477 | ssl->ctx->signed_cert_timestamp_list_length != 0) { |
| 478 | CBB contents; |
| 479 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_certificate_timestamp) || |
| 480 | !CBB_add_u16_length_prefixed(&extensions, &contents) || |
| 481 | !CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list, |
| 482 | ssl->ctx->signed_cert_timestamp_list_length)) { |
| 483 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 484 | goto err; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | if (ssl->s3->hs->ocsp_stapling_requested && |
| 489 | ssl->ctx->ocsp_response_length != 0) { |
| 490 | CBB contents, ocsp_response; |
| 491 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_status_request) || |
| 492 | !CBB_add_u16_length_prefixed(&extensions, &contents) || |
| 493 | !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) || |
| 494 | !CBB_add_u24_length_prefixed(&contents, &ocsp_response) || |
| 495 | !CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response, |
| 496 | ssl->ctx->ocsp_response_length)) { |
| 497 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 498 | goto err; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | for (size_t i = 0; i < sk_X509_num(cert->x509_chain); i++) { |
| 503 | CBB child; |
| 504 | if (!CBB_add_u24_length_prefixed(&certificate_list, &child) || |
| 505 | !ssl_add_cert_to_cbb(&child, sk_X509_value(cert->x509_chain, i)) || |
| 506 | !CBB_add_u16(&certificate_list, 0 /* no extensions */)) { |
| 507 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 508 | goto err; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | if (!ssl_complete_message(ssl, &cbb)) { |
| 513 | goto err; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | return 1; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame^] | 517 | |
| 518 | err: |
| 519 | CBB_cleanup(&cbb); |
| 520 | return 0; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | enum ssl_private_key_result_t tls13_prepare_certificate_verify( |
| 524 | SSL *ssl, int is_first_run) { |
| 525 | enum ssl_private_key_result_t ret = ssl_private_key_failure; |
| 526 | uint8_t *msg = NULL; |
| 527 | size_t msg_len; |
| 528 | CBB cbb, body; |
| 529 | CBB_zero(&cbb); |
| 530 | |
| 531 | uint16_t signature_algorithm; |
| 532 | if (!tls1_choose_signature_algorithm(ssl, &signature_algorithm)) { |
| 533 | goto err; |
| 534 | } |
| 535 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 536 | SSL3_MT_CERTIFICATE_VERIFY) || |
| 537 | !CBB_add_u16(&body, signature_algorithm)) { |
| 538 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 539 | goto err; |
| 540 | } |
| 541 | |
| 542 | /* Sign the digest. */ |
| 543 | CBB child; |
| 544 | const size_t max_sig_len = ssl_private_key_max_signature_len(ssl); |
| 545 | uint8_t *sig; |
| 546 | size_t sig_len; |
| 547 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
| 548 | !CBB_reserve(&child, &sig, max_sig_len)) { |
| 549 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 550 | goto err; |
| 551 | } |
| 552 | |
| 553 | enum ssl_private_key_result_t sign_result; |
| 554 | if (is_first_run) { |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 555 | if (!tls13_get_cert_verify_signature_input( |
| 556 | ssl, &msg, &msg_len, |
| 557 | ssl->server ? ssl_cert_verify_server : ssl_cert_verify_client)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 558 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 559 | goto err; |
| 560 | } |
| 561 | sign_result = ssl_private_key_sign(ssl, sig, &sig_len, max_sig_len, |
| 562 | signature_algorithm, msg, msg_len); |
| 563 | } else { |
| 564 | sign_result = ssl_private_key_complete(ssl, sig, &sig_len, max_sig_len); |
| 565 | } |
| 566 | |
| 567 | if (sign_result != ssl_private_key_success) { |
| 568 | ret = sign_result; |
| 569 | goto err; |
| 570 | } |
| 571 | |
| 572 | if (!CBB_did_write(&child, sig_len) || |
Steven Valdez | 5eead16 | 2016-11-11 22:23:25 -0500 | [diff] [blame] | 573 | !ssl_complete_message(ssl, &cbb)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 574 | goto err; |
| 575 | } |
| 576 | |
| 577 | ret = ssl_private_key_success; |
| 578 | |
| 579 | err: |
| 580 | CBB_cleanup(&cbb); |
| 581 | OPENSSL_free(msg); |
| 582 | return ret; |
| 583 | } |
| 584 | |
| 585 | int tls13_prepare_finished(SSL *ssl) { |
| 586 | size_t verify_data_len; |
| 587 | uint8_t verify_data[EVP_MAX_MD_SIZE]; |
| 588 | |
| 589 | if (!tls13_finished_mac(ssl, verify_data, &verify_data_len, ssl->server)) { |
| 590 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 591 | OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED); |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | CBB cbb, body; |
| 596 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_FINISHED) || |
| 597 | !CBB_add_bytes(&body, verify_data, verify_data_len) || |
Steven Valdez | 5eead16 | 2016-11-11 22:23:25 -0500 | [diff] [blame] | 598 | !ssl_complete_message(ssl, &cbb)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 599 | CBB_cleanup(&cbb); |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | return 1; |
| 604 | } |
Steven Valdez | 8e1c7be | 2016-07-26 12:39:22 -0400 | [diff] [blame] | 605 | |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 606 | static int tls13_receive_key_update(SSL *ssl) { |
Steven Valdez | c4aa727 | 2016-10-03 12:25:56 -0400 | [diff] [blame] | 607 | CBS cbs; |
| 608 | uint8_t key_update_request; |
| 609 | CBS_init(&cbs, ssl->init_msg, ssl->init_num); |
| 610 | if (!CBS_get_u8(&cbs, &key_update_request) || |
| 611 | CBS_len(&cbs) != 0 || |
| 612 | (key_update_request != SSL_KEY_UPDATE_NOT_REQUESTED && |
| 613 | key_update_request != SSL_KEY_UPDATE_REQUESTED)) { |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 614 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 615 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 616 | return 0; |
| 617 | } |
| 618 | |
Steven Valdez | c4aa727 | 2016-10-03 12:25:56 -0400 | [diff] [blame] | 619 | /* TODO(svaldez): Send KeyUpdate if |key_update_request| is |
| 620 | * |SSL_KEY_UPDATE_REQUESTED|. */ |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 621 | return tls13_rotate_traffic_key(ssl, evp_aead_open); |
| 622 | } |
| 623 | |
Steven Valdez | 8e1c7be | 2016-07-26 12:39:22 -0400 | [diff] [blame] | 624 | int tls13_post_handshake(SSL *ssl) { |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 625 | if (ssl->s3->tmp.message_type == SSL3_MT_KEY_UPDATE) { |
Steven Valdez | 32635b8 | 2016-08-16 11:25:03 -0400 | [diff] [blame] | 626 | ssl->s3->key_update_count++; |
| 627 | if (ssl->s3->key_update_count > kMaxKeyUpdates) { |
| 628 | OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_KEY_UPDATES); |
| 629 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 630 | return 0; |
| 631 | } |
| 632 | |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 633 | return tls13_receive_key_update(ssl); |
| 634 | } |
| 635 | |
Steven Valdez | 32635b8 | 2016-08-16 11:25:03 -0400 | [diff] [blame] | 636 | ssl->s3->key_update_count = 0; |
| 637 | |
Steven Valdez | 8e1c7be | 2016-07-26 12:39:22 -0400 | [diff] [blame] | 638 | if (ssl->s3->tmp.message_type == SSL3_MT_NEW_SESSION_TICKET && |
| 639 | !ssl->server) { |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 640 | return tls13_process_new_session_ticket(ssl); |
Steven Valdez | 8e1c7be | 2016-07-26 12:39:22 -0400 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | // TODO(svaldez): Handle post-handshake authentication. |
Steven Valdez | 8e1c7be | 2016-07-26 12:39:22 -0400 | [diff] [blame] | 644 | |
| 645 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 646 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
| 647 | return 0; |
| 648 | } |