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 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 20 | #include <utility> |
| 21 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 22 | #include <openssl/bytestring.h> |
| 23 | #include <openssl/err.h> |
| 24 | #include <openssl/hkdf.h> |
| 25 | #include <openssl/mem.h> |
| 26 | #include <openssl/stack.h> |
| 27 | #include <openssl/x509.h> |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 28 | |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 29 | #include "../crypto/internal.h" |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 30 | #include "internal.h" |
| 31 | |
| 32 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 33 | namespace bssl { |
| 34 | |
Steven Valdez | 32635b8 | 2016-08-16 11:25:03 -0400 | [diff] [blame] | 35 | /* kMaxKeyUpdates is the number of consecutive KeyUpdates that will be |
| 36 | * processed. Without this limit an attacker could force unbounded processing |
| 37 | * without being able to return application data. */ |
| 38 | static const uint8_t kMaxKeyUpdates = 32; |
| 39 | |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 40 | int tls13_handshake(SSL_HANDSHAKE *hs, int *out_early_return) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 41 | SSL *const ssl = hs->ssl; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 42 | for (;;) { |
| 43 | /* Resolve the operation the handshake was waiting on. */ |
| 44 | switch (hs->wait) { |
| 45 | case ssl_hs_error: |
| 46 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 47 | return -1; |
| 48 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 49 | case ssl_hs_flush: { |
David Benjamin | 8d5f9da | 2017-01-01 17:41:30 -0500 | [diff] [blame] | 50 | int ret = ssl->method->flush_flight(ssl); |
David Benjamin | f2401eb | 2016-07-18 22:25:05 +0200 | [diff] [blame] | 51 | if (ret <= 0) { |
David Benjamin | f2401eb | 2016-07-18 22:25:05 +0200 | [diff] [blame] | 52 | return ret; |
| 53 | } |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 54 | break; |
David Benjamin | f2401eb | 2016-07-18 22:25:05 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 57 | case ssl_hs_read_message: { |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 58 | int ret = ssl->method->read_message(ssl); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 59 | if (ret <= 0) { |
| 60 | return ret; |
| 61 | } |
| 62 | break; |
| 63 | } |
| 64 | |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 65 | case ssl_hs_read_change_cipher_spec: { |
| 66 | int ret = ssl->method->read_change_cipher_spec(ssl); |
| 67 | if (ret <= 0) { |
| 68 | return ret; |
| 69 | } |
| 70 | break; |
| 71 | } |
| 72 | |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 73 | case ssl_hs_read_end_of_early_data: { |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 74 | if (ssl->s3->hs->can_early_read) { |
| 75 | /* While we are processing early data, the handshake returns early. */ |
| 76 | *out_early_return = 1; |
| 77 | return 1; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 78 | } |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 79 | hs->wait = ssl_hs_ok; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 80 | break; |
| 81 | } |
| 82 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 83 | case ssl_hs_x509_lookup: |
| 84 | ssl->rwstate = SSL_X509_LOOKUP; |
| 85 | hs->wait = ssl_hs_ok; |
| 86 | return -1; |
| 87 | |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 88 | case ssl_hs_channel_id_lookup: |
| 89 | ssl->rwstate = SSL_CHANNEL_ID_LOOKUP; |
| 90 | hs->wait = ssl_hs_ok; |
| 91 | return -1; |
| 92 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 93 | case ssl_hs_private_key_operation: |
| 94 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
| 95 | hs->wait = ssl_hs_ok; |
| 96 | return -1; |
| 97 | |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 98 | case ssl_hs_pending_ticket: |
| 99 | ssl->rwstate = SSL_PENDING_TICKET; |
| 100 | hs->wait = ssl_hs_ok; |
| 101 | return -1; |
| 102 | |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 103 | case ssl_hs_certificate_verify: |
| 104 | ssl->rwstate = SSL_CERTIFICATE_VERIFY; |
| 105 | hs->wait = ssl_hs_ok; |
| 106 | return -1; |
| 107 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 108 | case ssl_hs_early_data_rejected: |
| 109 | ssl->rwstate = SSL_EARLY_DATA_REJECTED; |
| 110 | /* Cause |SSL_write| to start failing immediately. */ |
| 111 | hs->can_early_write = 0; |
| 112 | return -1; |
| 113 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 114 | case ssl_hs_ok: |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | /* Run the state machine again. */ |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 119 | hs->wait = hs->do_tls13_handshake(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 120 | if (hs->wait == ssl_hs_error) { |
| 121 | /* Don't loop around to avoid a stray |SSL_R_SSL_HANDSHAKE_FAILURE| the |
| 122 | * first time around. */ |
| 123 | return -1; |
| 124 | } |
| 125 | if (hs->wait == ssl_hs_ok) { |
| 126 | /* The handshake has completed. */ |
| 127 | return 1; |
| 128 | } |
| 129 | |
| 130 | /* Otherwise, loop to the beginning and resolve what was blocking the |
| 131 | * handshake. */ |
| 132 | } |
| 133 | } |
| 134 | |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 135 | int tls13_get_cert_verify_signature_input( |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 136 | SSL_HANDSHAKE *hs, uint8_t **out, size_t *out_len, |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 137 | enum ssl_cert_verify_context_t cert_verify_context) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 138 | ScopedCBB cbb; |
| 139 | if (!CBB_init(cbb.get(), 64 + 33 + 1 + 2 * EVP_MAX_MD_SIZE)) { |
| 140 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 141 | return 0; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | for (size_t i = 0; i < 64; i++) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 145 | if (!CBB_add_u8(cbb.get(), 0x20)) { |
| 146 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 147 | return 0; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 151 | const uint8_t *context; |
| 152 | size_t context_len; |
| 153 | if (cert_verify_context == ssl_cert_verify_server) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 154 | /* Include the NUL byte. */ |
| 155 | static const char kContext[] = "TLS 1.3, server CertificateVerify"; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 156 | context = (const uint8_t *)kContext; |
| 157 | context_len = sizeof(kContext); |
| 158 | } else if (cert_verify_context == ssl_cert_verify_client) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 159 | static const char kContext[] = "TLS 1.3, client CertificateVerify"; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 160 | context = (const uint8_t *)kContext; |
| 161 | context_len = sizeof(kContext); |
| 162 | } else if (cert_verify_context == ssl_cert_verify_channel_id) { |
| 163 | static const char kContext[] = "TLS 1.3, Channel ID"; |
| 164 | context = (const uint8_t *)kContext; |
| 165 | context_len = sizeof(kContext); |
| 166 | } else { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 167 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 168 | return 0; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 169 | } |
| 170 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 171 | if (!CBB_add_bytes(cbb.get(), context, context_len)) { |
| 172 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 173 | return 0; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 174 | } |
| 175 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 176 | uint8_t context_hash[EVP_MAX_MD_SIZE]; |
| 177 | size_t context_hash_len; |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 178 | if (!hs->transcript.GetHash(context_hash, &context_hash_len) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 179 | !CBB_add_bytes(cbb.get(), context_hash, context_hash_len) || |
| 180 | !CBB_finish(cbb.get(), out, out_len)) { |
| 181 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 182 | return 0; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | return 1; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 186 | } |
| 187 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 188 | int tls13_process_certificate(SSL_HANDSHAKE *hs, const SSLMessage &msg, |
| 189 | int allow_anonymous) { |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 190 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 191 | CBS body = msg.body, context, certificate_list; |
| 192 | if (!CBS_get_u8_length_prefixed(&body, &context) || |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 193 | CBS_len(&context) != 0 || |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 194 | !CBS_get_u24_length_prefixed(&body, &certificate_list) || |
| 195 | CBS_len(&body) != 0) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 196 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 197 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 198 | return 0; |
| 199 | } |
| 200 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 201 | UniquePtr<STACK_OF(CRYPTO_BUFFER)> certs(sk_CRYPTO_BUFFER_new_null()); |
| 202 | if (!certs) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 203 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 204 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 205 | return 0; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 206 | } |
| 207 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 208 | const bool retain_sha256 = |
| 209 | ssl->server && ssl->retain_only_sha256_of_client_certs; |
| 210 | UniquePtr<EVP_PKEY> pkey; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 211 | while (CBS_len(&certificate_list) > 0) { |
| 212 | CBS certificate, extensions; |
| 213 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) || |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 214 | !CBS_get_u16_length_prefixed(&certificate_list, &extensions) || |
| 215 | CBS_len(&certificate) == 0) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 216 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 217 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 218 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 219 | } |
| 220 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 221 | if (sk_CRYPTO_BUFFER_num(certs.get()) == 0) { |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 222 | pkey = ssl_cert_parse_pubkey(&certificate); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 223 | if (!pkey) { |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 224 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 225 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 226 | return 0; |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 227 | } |
Adam Langley | a4b9198 | 2016-12-12 12:05:53 -0800 | [diff] [blame] | 228 | /* TLS 1.3 always uses certificate keys for signing thus the correct |
| 229 | * keyUsage is enforced. */ |
| 230 | if (!ssl_cert_check_digital_signature_key_usage(&certificate)) { |
| 231 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 232 | return 0; |
Adam Langley | a4b9198 | 2016-12-12 12:05:53 -0800 | [diff] [blame] | 233 | } |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 234 | |
| 235 | if (retain_sha256) { |
| 236 | /* Retain the hash of the leaf certificate if requested. */ |
| 237 | SHA256(CBS_data(&certificate), CBS_len(&certificate), |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 238 | hs->new_session->peer_sha256); |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 239 | } |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 240 | } |
| 241 | |
David Benjamin | 6e9321f | 2017-07-25 23:49:58 -0400 | [diff] [blame] | 242 | UniquePtr<CRYPTO_BUFFER> buf( |
| 243 | CRYPTO_BUFFER_new_from_CBS(&certificate, ssl->ctx->pool)); |
| 244 | if (!buf || |
| 245 | !PushToStack(certs.get(), std::move(buf))) { |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame] | 246 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 247 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 248 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /* Parse out the extensions. */ |
| 252 | int have_status_request = 0, have_sct = 0; |
| 253 | CBS status_request, sct; |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 254 | const SSL_EXTENSION_TYPE ext_types[] = { |
| 255 | {TLSEXT_TYPE_status_request, &have_status_request, &status_request}, |
| 256 | {TLSEXT_TYPE_certificate_timestamp, &have_sct, &sct}, |
| 257 | }; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 258 | |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 259 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 260 | if (!ssl_parse_extensions(&extensions, &alert, ext_types, |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 261 | OPENSSL_ARRAY_SIZE(ext_types), |
| 262 | 0 /* reject unknown */)) { |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 263 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 264 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /* All Certificate extensions are parsed, but only the leaf extensions are |
| 268 | * stored. */ |
| 269 | if (have_status_request) { |
| 270 | if (ssl->server || !ssl->ocsp_stapling_enabled) { |
| 271 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 272 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 273 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | uint8_t status_type; |
| 277 | CBS ocsp_response; |
| 278 | if (!CBS_get_u8(&status_request, &status_type) || |
| 279 | status_type != TLSEXT_STATUSTYPE_ocsp || |
| 280 | !CBS_get_u24_length_prefixed(&status_request, &ocsp_response) || |
| 281 | CBS_len(&ocsp_response) == 0 || |
| 282 | CBS_len(&status_request) != 0) { |
| 283 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 284 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 285 | } |
| 286 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 287 | if (sk_CRYPTO_BUFFER_num(certs.get()) == 1 && |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 288 | !CBS_stow(&ocsp_response, &hs->new_session->ocsp_response, |
| 289 | &hs->new_session->ocsp_response_length)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 290 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 291 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | if (have_sct) { |
| 296 | if (ssl->server || !ssl->signed_cert_timestamps_enabled) { |
| 297 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 298 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 299 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 300 | } |
| 301 | |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 302 | if (!ssl_is_sct_list_valid(&sct)) { |
| 303 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 304 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 305 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 306 | } |
| 307 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 308 | if (sk_CRYPTO_BUFFER_num(certs.get()) == 1 && |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 309 | !CBS_stow( |
| 310 | &sct, &hs->new_session->tlsext_signed_cert_timestamp_list, |
| 311 | &hs->new_session->tlsext_signed_cert_timestamp_list_length)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 312 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 313 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 318 | /* Store a null certificate list rather than an empty one if the peer didn't |
| 319 | * send certificates. */ |
| 320 | if (sk_CRYPTO_BUFFER_num(certs.get()) == 0) { |
| 321 | certs.reset(); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 322 | } |
| 323 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 324 | hs->peer_pubkey = std::move(pkey); |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 325 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 326 | sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 327 | hs->new_session->certs = certs.release(); |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 328 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 329 | if (!ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) { |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 330 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 331 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 332 | return 0; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 333 | } |
| 334 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 335 | if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) { |
David Benjamin | 4087df9 | 2016-08-01 20:16:31 -0400 | [diff] [blame] | 336 | if (!allow_anonymous) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 337 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
David Benjamin | 1db9e1b | 2016-10-07 20:51:43 -0400 | [diff] [blame] | 338 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_CERTIFICATE_REQUIRED); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 339 | return 0; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 340 | } |
| 341 | |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 342 | /* OpenSSL returns X509_V_OK when no certificates are requested. This is |
David Benjamin | dd634eb | 2016-08-18 16:40:28 -0400 | [diff] [blame] | 343 | * classed by them as a bug, but it's assumed by at least NGINX. */ |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 344 | hs->new_session->verify_result = X509_V_OK; |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 345 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 346 | /* No certificate, so nothing more to do. */ |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 347 | return 1; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 348 | } |
| 349 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 350 | hs->new_session->peer_sha256_valid = retain_sha256; |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 351 | return 1; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 352 | } |
| 353 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 354 | int tls13_process_certificate_verify(SSL_HANDSHAKE *hs, const SSLMessage &msg) { |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 355 | SSL *const ssl = hs->ssl; |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 356 | if (hs->peer_pubkey == NULL) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 357 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 358 | return 0; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 359 | } |
| 360 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 361 | CBS body = msg.body, signature; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 362 | uint16_t signature_algorithm; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 363 | if (!CBS_get_u16(&body, &signature_algorithm) || |
| 364 | !CBS_get_u16_length_prefixed(&body, &signature) || |
| 365 | CBS_len(&body) != 0) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 366 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 367 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 368 | return 0; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 369 | } |
| 370 | |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 371 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 372 | if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) { |
| 373 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 374 | return 0; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 375 | } |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 376 | hs->new_session->peer_signature_algorithm = signature_algorithm; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 377 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 378 | uint8_t *input = NULL; |
| 379 | size_t input_len; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 380 | if (!tls13_get_cert_verify_signature_input( |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 381 | hs, &input, &input_len, |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 382 | ssl->server ? ssl_cert_verify_client : ssl_cert_verify_server)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 383 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 384 | return 0; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 385 | } |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 386 | UniquePtr<uint8_t> free_input(input); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 387 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 388 | int sig_ok = ssl_public_key_verify(ssl, CBS_data(&signature), |
| 389 | CBS_len(&signature), signature_algorithm, |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 390 | hs->peer_pubkey.get(), input, input_len); |
David Benjamin | 04aa694 | 2016-08-19 14:51:10 -0400 | [diff] [blame] | 391 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 392 | sig_ok = 1; |
| 393 | ERR_clear_error(); |
| 394 | #endif |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 395 | if (!sig_ok) { |
| 396 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
| 397 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 398 | return 0; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 399 | } |
| 400 | |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 401 | return 1; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 402 | } |
| 403 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 404 | int tls13_process_finished(SSL_HANDSHAKE *hs, const SSLMessage &msg, |
| 405 | int use_saved_value) { |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 406 | SSL *const ssl = hs->ssl; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 407 | uint8_t verify_data_buf[EVP_MAX_MD_SIZE]; |
| 408 | const uint8_t *verify_data; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 409 | size_t verify_data_len; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 410 | if (use_saved_value) { |
| 411 | assert(ssl->server); |
| 412 | verify_data = hs->expected_client_finished; |
| 413 | verify_data_len = hs->hash_len; |
| 414 | } else { |
| 415 | if (!tls13_finished_mac(hs, verify_data_buf, &verify_data_len, |
| 416 | !ssl->server)) { |
| 417 | return 0; |
| 418 | } |
| 419 | verify_data = verify_data_buf; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 420 | } |
| 421 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 422 | int finished_ok = CBS_mem_equal(&msg.body, verify_data, verify_data_len); |
David Benjamin | 04aa694 | 2016-08-19 14:51:10 -0400 | [diff] [blame] | 423 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 424 | finished_ok = 1; |
| 425 | #endif |
| 426 | if (!finished_ok) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 427 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
| 428 | OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED); |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | return 1; |
| 433 | } |
| 434 | |
David Benjamin | 0f24bed | 2017-01-12 19:46:50 -0500 | [diff] [blame] | 435 | int tls13_add_certificate(SSL_HANDSHAKE *hs) { |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 436 | SSL *const ssl = hs->ssl; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 437 | ScopedCBB cbb; |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 438 | CBB body, certificate_list; |
| 439 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CERTIFICATE) || |
David Benjamin | 8a8349b | 2016-08-18 02:32:23 -0400 | [diff] [blame] | 440 | /* The request context is always empty in the handshake. */ |
| 441 | !CBB_add_u8(&body, 0) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 442 | !CBB_add_u24_length_prefixed(&body, &certificate_list)) { |
| 443 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 444 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | if (!ssl_has_certificate(ssl)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 448 | return ssl_add_message_cbb(ssl, cbb.get()); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | CERT *cert = ssl->cert; |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 452 | CRYPTO_BUFFER *leaf_buf = sk_CRYPTO_BUFFER_value(cert->chain, 0); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 453 | CBB leaf, extensions; |
| 454 | if (!CBB_add_u24_length_prefixed(&certificate_list, &leaf) || |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 455 | !CBB_add_bytes(&leaf, CRYPTO_BUFFER_data(leaf_buf), |
| 456 | CRYPTO_BUFFER_len(leaf_buf)) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 457 | !CBB_add_u16_length_prefixed(&certificate_list, &extensions)) { |
| 458 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 459 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 460 | } |
| 461 | |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 462 | if (hs->scts_requested && ssl->cert->signed_cert_timestamp_list != NULL) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 463 | CBB contents; |
| 464 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_certificate_timestamp) || |
| 465 | !CBB_add_u16_length_prefixed(&extensions, &contents) || |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 466 | !CBB_add_bytes( |
| 467 | &contents, |
| 468 | CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list), |
| 469 | CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) || |
Adam Langley | 6f5f49f | 2016-11-18 11:05:00 -0800 | [diff] [blame] | 470 | !CBB_flush(&extensions)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 471 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 472 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 476 | if (hs->ocsp_stapling_requested && |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 477 | ssl->cert->ocsp_response != NULL) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 478 | CBB contents, ocsp_response; |
| 479 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_status_request) || |
| 480 | !CBB_add_u16_length_prefixed(&extensions, &contents) || |
| 481 | !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) || |
| 482 | !CBB_add_u24_length_prefixed(&contents, &ocsp_response) || |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 483 | !CBB_add_bytes(&ocsp_response, |
| 484 | CRYPTO_BUFFER_data(ssl->cert->ocsp_response), |
| 485 | CRYPTO_BUFFER_len(ssl->cert->ocsp_response)) || |
Adam Langley | 6f5f49f | 2016-11-18 11:05:00 -0800 | [diff] [blame] | 486 | !CBB_flush(&extensions)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 487 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 488 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 492 | for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cert->chain); i++) { |
| 493 | CRYPTO_BUFFER *cert_buf = sk_CRYPTO_BUFFER_value(cert->chain, i); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 494 | CBB child; |
| 495 | if (!CBB_add_u24_length_prefixed(&certificate_list, &child) || |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 496 | !CBB_add_bytes(&child, CRYPTO_BUFFER_data(cert_buf), |
| 497 | CRYPTO_BUFFER_len(cert_buf)) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 498 | !CBB_add_u16(&certificate_list, 0 /* no extensions */)) { |
| 499 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 500 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 504 | return ssl_add_message_cbb(ssl, cbb.get()); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 505 | } |
| 506 | |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 507 | enum ssl_private_key_result_t tls13_add_certificate_verify(SSL_HANDSHAKE *hs) { |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 508 | SSL *const ssl = hs->ssl; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 509 | uint16_t signature_algorithm; |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 510 | if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 511 | return ssl_private_key_failure; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 512 | } |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 513 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 514 | ScopedCBB cbb; |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 515 | CBB body; |
| 516 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 517 | SSL3_MT_CERTIFICATE_VERIFY) || |
| 518 | !CBB_add_u16(&body, signature_algorithm)) { |
| 519 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 520 | return ssl_private_key_failure; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /* Sign the digest. */ |
| 524 | CBB child; |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 525 | const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get()); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 526 | uint8_t *sig; |
| 527 | size_t sig_len; |
| 528 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
| 529 | !CBB_reserve(&child, &sig, max_sig_len)) { |
| 530 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 531 | return ssl_private_key_failure; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 532 | } |
| 533 | |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 534 | uint8_t *msg = NULL; |
| 535 | size_t msg_len; |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 536 | if (!tls13_get_cert_verify_signature_input( |
| 537 | hs, &msg, &msg_len, |
| 538 | ssl->server ? ssl_cert_verify_server : ssl_cert_verify_client)) { |
| 539 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 540 | return ssl_private_key_failure; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 541 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 542 | UniquePtr<uint8_t> free_msg(msg); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 543 | |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 544 | enum ssl_private_key_result_t sign_result = ssl_private_key_sign( |
| 545 | hs, sig, &sig_len, max_sig_len, signature_algorithm, msg, msg_len); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 546 | if (sign_result != ssl_private_key_success) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 547 | return sign_result; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | if (!CBB_did_write(&child, sig_len) || |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 551 | !ssl_add_message_cbb(ssl, cbb.get())) { |
| 552 | return ssl_private_key_failure; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 553 | } |
| 554 | |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 555 | return ssl_private_key_success; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 556 | } |
| 557 | |
David Benjamin | 0f24bed | 2017-01-12 19:46:50 -0500 | [diff] [blame] | 558 | int tls13_add_finished(SSL_HANDSHAKE *hs) { |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 559 | SSL *const ssl = hs->ssl; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 560 | size_t verify_data_len; |
| 561 | uint8_t verify_data[EVP_MAX_MD_SIZE]; |
| 562 | |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 563 | if (!tls13_finished_mac(hs, verify_data, &verify_data_len, ssl->server)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 564 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 565 | OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED); |
| 566 | return 0; |
| 567 | } |
| 568 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 569 | ScopedCBB cbb; |
| 570 | CBB body; |
| 571 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_FINISHED) || |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 572 | !CBB_add_bytes(&body, verify_data, verify_data_len) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 573 | !ssl_add_message_cbb(ssl, cbb.get())) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | return 1; |
| 578 | } |
Steven Valdez | 8e1c7be | 2016-07-26 12:39:22 -0400 | [diff] [blame] | 579 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 580 | static int tls13_receive_key_update(SSL *ssl, const SSLMessage &msg) { |
| 581 | CBS body = msg.body; |
Steven Valdez | c4aa727 | 2016-10-03 12:25:56 -0400 | [diff] [blame] | 582 | uint8_t key_update_request; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 583 | if (!CBS_get_u8(&body, &key_update_request) || |
| 584 | CBS_len(&body) != 0 || |
Steven Valdez | c4aa727 | 2016-10-03 12:25:56 -0400 | [diff] [blame] | 585 | (key_update_request != SSL_KEY_UPDATE_NOT_REQUESTED && |
| 586 | key_update_request != SSL_KEY_UPDATE_REQUESTED)) { |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 587 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 588 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 589 | return 0; |
| 590 | } |
| 591 | |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 592 | if (!tls13_rotate_traffic_key(ssl, evp_aead_open)) { |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | /* Acknowledge the KeyUpdate */ |
| 597 | if (key_update_request == SSL_KEY_UPDATE_REQUESTED && |
| 598 | !ssl->s3->key_update_pending) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 599 | ScopedCBB cbb; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 600 | CBB body_cbb; |
| 601 | if (!ssl->method->init_message(ssl, cbb.get(), &body_cbb, |
| 602 | SSL3_MT_KEY_UPDATE) || |
| 603 | !CBB_add_u8(&body_cbb, SSL_KEY_UPDATE_NOT_REQUESTED) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 604 | !ssl_add_message_cbb(ssl, cbb.get()) || |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 605 | !tls13_rotate_traffic_key(ssl, evp_aead_seal)) { |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | /* Suppress KeyUpdate acknowledgments until this change is written to the |
| 610 | * wire. This prevents us from accumulating write obligations when read and |
| 611 | * write progress at different rates. See draft-ietf-tls-tls13-18, section |
| 612 | * 4.5.3. */ |
| 613 | ssl->s3->key_update_pending = 1; |
| 614 | } |
| 615 | |
| 616 | return 1; |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 617 | } |
| 618 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 619 | int tls13_post_handshake(SSL *ssl, const SSLMessage &msg) { |
| 620 | if (msg.type == SSL3_MT_KEY_UPDATE) { |
Steven Valdez | 32635b8 | 2016-08-16 11:25:03 -0400 | [diff] [blame] | 621 | ssl->s3->key_update_count++; |
| 622 | if (ssl->s3->key_update_count > kMaxKeyUpdates) { |
| 623 | OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_KEY_UPDATES); |
| 624 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 625 | return 0; |
| 626 | } |
| 627 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 628 | return tls13_receive_key_update(ssl, msg); |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 629 | } |
| 630 | |
Steven Valdez | 32635b8 | 2016-08-16 11:25:03 -0400 | [diff] [blame] | 631 | ssl->s3->key_update_count = 0; |
| 632 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 633 | if (msg.type == SSL3_MT_NEW_SESSION_TICKET && !ssl->server) { |
| 634 | return tls13_process_new_session_ticket(ssl, msg); |
Steven Valdez | 8e1c7be | 2016-07-26 12:39:22 -0400 | [diff] [blame] | 635 | } |
| 636 | |
Steven Valdez | 8e1c7be | 2016-07-26 12:39:22 -0400 | [diff] [blame] | 637 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 638 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
| 639 | return 0; |
| 640 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 641 | |
| 642 | } // namespace bssl |