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 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 33 | BSSL_NAMESPACE_BEGIN |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 34 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -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. |
Steven Valdez | 32635b8 | 2016-08-16 11:25:03 -0400 | [diff] [blame] | 38 | static const uint8_t kMaxKeyUpdates = 32; |
| 39 | |
Steven Valdez | 964b237 | 2017-11-07 17:09:52 -0500 | [diff] [blame] | 40 | const uint8_t kHelloRetryRequest[SSL3_RANDOM_SIZE] = { |
| 41 | 0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, |
| 42 | 0x02, 0x1e, 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, |
| 43 | 0x8c, 0x5e, 0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, |
| 44 | }; |
| 45 | |
David Benjamin | 6965d25 | 2018-11-19 15:49:56 -0600 | [diff] [blame] | 46 | // See RFC 8446, section 4.1.3. |
Steven Valdez | f1af129 | 2018-08-13 10:54:48 -0400 | [diff] [blame] | 47 | const uint8_t kTLS12DowngradeRandom[8] = {0x44, 0x4f, 0x57, 0x4e, |
| 48 | 0x47, 0x52, 0x44, 0x00}; |
Steven Valdez | f1af129 | 2018-08-13 10:54:48 -0400 | [diff] [blame] | 49 | const uint8_t kTLS13DowngradeRandom[8] = {0x44, 0x4f, 0x57, 0x4e, |
| 50 | 0x47, 0x52, 0x44, 0x01}; |
David Benjamin | 6df6540 | 2017-12-18 18:00:23 -0500 | [diff] [blame] | 51 | |
David Benjamin | 6965d25 | 2018-11-19 15:49:56 -0600 | [diff] [blame] | 52 | // This is a non-standard randomly-generated value. |
| 53 | const uint8_t kJDK11DowngradeRandom[8] = {0xed, 0xbf, 0xb4, 0xa8, |
| 54 | 0xc2, 0x47, 0x10, 0xff}; |
David Benjamin | 6df6540 | 2017-12-18 18:00:23 -0500 | [diff] [blame] | 55 | |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 56 | bool tls13_get_cert_verify_signature_input( |
| 57 | SSL_HANDSHAKE *hs, Array<uint8_t> *out, |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 58 | enum ssl_cert_verify_context_t cert_verify_context) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 59 | ScopedCBB cbb; |
| 60 | if (!CBB_init(cbb.get(), 64 + 33 + 1 + 2 * EVP_MAX_MD_SIZE)) { |
| 61 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 62 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | for (size_t i = 0; i < 64; i++) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 66 | if (!CBB_add_u8(cbb.get(), 0x20)) { |
| 67 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 68 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 72 | Span<const char> context; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 73 | if (cert_verify_context == ssl_cert_verify_server) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 74 | static const char kContext[] = "TLS 1.3, server CertificateVerify"; |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 75 | context = kContext; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 76 | } else if (cert_verify_context == ssl_cert_verify_client) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 77 | static const char kContext[] = "TLS 1.3, client CertificateVerify"; |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 78 | context = kContext; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 79 | } else if (cert_verify_context == ssl_cert_verify_channel_id) { |
| 80 | static const char kContext[] = "TLS 1.3, Channel ID"; |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 81 | context = kContext; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 82 | } else { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 83 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 84 | return false; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 85 | } |
| 86 | |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 87 | // Note |context| includes the NUL byte separator. |
| 88 | if (!CBB_add_bytes(cbb.get(), |
| 89 | reinterpret_cast<const uint8_t *>(context.data()), |
| 90 | context.size())) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 91 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 92 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 93 | } |
| 94 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 95 | uint8_t context_hash[EVP_MAX_MD_SIZE]; |
| 96 | size_t context_hash_len; |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 97 | if (!hs->transcript.GetHash(context_hash, &context_hash_len) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 98 | !CBB_add_bytes(cbb.get(), context_hash, context_hash_len) || |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 99 | !CBBFinishArray(cbb.get(), out)) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 100 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 101 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 102 | } |
| 103 | |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 104 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 105 | } |
| 106 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 107 | bool tls13_process_certificate(SSL_HANDSHAKE *hs, const SSLMessage &msg, |
| 108 | bool allow_anonymous) { |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 109 | SSL *const ssl = hs->ssl; |
Adam Langley | 0080d83 | 2018-06-07 16:39:49 -0700 | [diff] [blame] | 110 | CBS body = msg.body; |
| 111 | bssl::UniquePtr<CRYPTO_BUFFER> decompressed; |
| 112 | |
| 113 | if (msg.type == SSL3_MT_COMPRESSED_CERTIFICATE) { |
| 114 | CBS compressed; |
| 115 | uint16_t alg_id; |
| 116 | uint32_t uncompressed_len; |
| 117 | |
| 118 | if (!CBS_get_u16(&body, &alg_id) || |
| 119 | !CBS_get_u24(&body, &uncompressed_len) || |
| 120 | !CBS_get_u24_length_prefixed(&body, &compressed) || |
| 121 | CBS_len(&body) != 0) { |
| 122 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 123 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 124 | return false; |
Adam Langley | 0080d83 | 2018-06-07 16:39:49 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | if (uncompressed_len > ssl->max_cert_list) { |
| 128 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 129 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNCOMPRESSED_CERT_TOO_LARGE); |
| 130 | ERR_add_error_dataf("requested=%u", |
| 131 | static_cast<unsigned>(uncompressed_len)); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 132 | return false; |
Adam Langley | 0080d83 | 2018-06-07 16:39:49 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Adam Langley | 8596795 | 2018-07-03 08:04:58 -0700 | [diff] [blame] | 135 | ssl_cert_decompression_func_t decompress = nullptr; |
David Benjamin | 8fe1584 | 2019-10-08 16:57:38 -0400 | [diff] [blame] | 136 | for (const auto &alg : ssl->ctx->cert_compression_algs) { |
| 137 | if (alg.alg_id == alg_id) { |
| 138 | decompress = alg.decompress; |
Adam Langley | 0080d83 | 2018-06-07 16:39:49 -0700 | [diff] [blame] | 139 | break; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (decompress == nullptr) { |
| 144 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 145 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERT_COMPRESSION_ALG); |
| 146 | ERR_add_error_dataf("alg=%d", static_cast<int>(alg_id)); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 147 | return false; |
Adam Langley | 0080d83 | 2018-06-07 16:39:49 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Adam Langley | 8596795 | 2018-07-03 08:04:58 -0700 | [diff] [blame] | 150 | CRYPTO_BUFFER *decompressed_ptr = nullptr; |
| 151 | if (!decompress(ssl, &decompressed_ptr, uncompressed_len, |
| 152 | CBS_data(&compressed), CBS_len(&compressed))) { |
Adam Langley | 0080d83 | 2018-06-07 16:39:49 -0700 | [diff] [blame] | 153 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 154 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_DECOMPRESSION_FAILED); |
| 155 | ERR_add_error_dataf("alg=%d", static_cast<int>(alg_id)); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 156 | return false; |
Adam Langley | 0080d83 | 2018-06-07 16:39:49 -0700 | [diff] [blame] | 157 | } |
Adam Langley | 8596795 | 2018-07-03 08:04:58 -0700 | [diff] [blame] | 158 | decompressed.reset(decompressed_ptr); |
Adam Langley | 0080d83 | 2018-06-07 16:39:49 -0700 | [diff] [blame] | 159 | |
Adam Langley | 8596795 | 2018-07-03 08:04:58 -0700 | [diff] [blame] | 160 | if (CRYPTO_BUFFER_len(decompressed_ptr) != uncompressed_len) { |
| 161 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 162 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_DECOMPRESSION_FAILED); |
| 163 | ERR_add_error_dataf( |
| 164 | "alg=%d got=%u expected=%u", static_cast<int>(alg_id), |
| 165 | static_cast<unsigned>(CRYPTO_BUFFER_len(decompressed_ptr)), |
| 166 | static_cast<unsigned>(uncompressed_len)); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 167 | return false; |
Adam Langley | 8596795 | 2018-07-03 08:04:58 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | CBS_init(&body, CRYPTO_BUFFER_data(decompressed_ptr), |
| 171 | CRYPTO_BUFFER_len(decompressed_ptr)); |
Adam Langley | 0080d83 | 2018-06-07 16:39:49 -0700 | [diff] [blame] | 172 | } else { |
| 173 | assert(msg.type == SSL3_MT_CERTIFICATE); |
| 174 | } |
| 175 | |
| 176 | CBS context, certificate_list; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 177 | if (!CBS_get_u8_length_prefixed(&body, &context) || |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 178 | CBS_len(&context) != 0 || |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 179 | !CBS_get_u24_length_prefixed(&body, &certificate_list) || |
| 180 | CBS_len(&body) != 0) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 181 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 182 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 183 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 184 | } |
| 185 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 186 | UniquePtr<STACK_OF(CRYPTO_BUFFER)> certs(sk_CRYPTO_BUFFER_new_null()); |
| 187 | if (!certs) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 188 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 189 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 190 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 191 | } |
| 192 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 193 | const bool retain_sha256 = |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 194 | ssl->server && hs->config->retain_only_sha256_of_client_certs; |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 195 | UniquePtr<EVP_PKEY> pkey; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 196 | while (CBS_len(&certificate_list) > 0) { |
| 197 | CBS certificate, extensions; |
| 198 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) || |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 199 | !CBS_get_u16_length_prefixed(&certificate_list, &extensions) || |
| 200 | CBS_len(&certificate) == 0) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 201 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 202 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 203 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 204 | } |
| 205 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 206 | if (sk_CRYPTO_BUFFER_num(certs.get()) == 0) { |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 207 | pkey = ssl_cert_parse_pubkey(&certificate); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 208 | if (!pkey) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 209 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 210 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 211 | return false; |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 212 | } |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 213 | // TLS 1.3 always uses certificate keys for signing thus the correct |
| 214 | // keyUsage is enforced. |
Jesse Selover | d7266ec | 2019-01-30 16:06:10 -0500 | [diff] [blame] | 215 | if (!ssl_cert_check_key_usage(&certificate, |
| 216 | key_usage_digital_signature)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 217 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 218 | return false; |
Adam Langley | a4b9198 | 2016-12-12 12:05:53 -0800 | [diff] [blame] | 219 | } |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 220 | |
| 221 | if (retain_sha256) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 222 | // Retain the hash of the leaf certificate if requested. |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 223 | SHA256(CBS_data(&certificate), CBS_len(&certificate), |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 224 | hs->new_session->peer_sha256); |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 225 | } |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 226 | } |
| 227 | |
David Benjamin | 6e9321f | 2017-07-25 23:49:58 -0400 | [diff] [blame] | 228 | UniquePtr<CRYPTO_BUFFER> buf( |
| 229 | CRYPTO_BUFFER_new_from_CBS(&certificate, ssl->ctx->pool)); |
| 230 | if (!buf || |
| 231 | !PushToStack(certs.get(), std::move(buf))) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 232 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame] | 233 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 234 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 235 | } |
| 236 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 237 | // Parse out the extensions. |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 238 | bool have_status_request = false, have_sct = false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 239 | CBS status_request, sct; |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 240 | const SSL_EXTENSION_TYPE ext_types[] = { |
| 241 | {TLSEXT_TYPE_status_request, &have_status_request, &status_request}, |
| 242 | {TLSEXT_TYPE_certificate_timestamp, &have_sct, &sct}, |
| 243 | }; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 244 | |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 245 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 246 | if (!ssl_parse_extensions(&extensions, &alert, ext_types, |
David Benjamin | c4ec14c | 2020-09-21 18:42:52 -0400 | [diff] [blame] | 247 | /*ignore_unknown=*/false)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 248 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 249 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 250 | } |
| 251 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 252 | // All Certificate extensions are parsed, but only the leaf extensions are |
| 253 | // stored. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 254 | if (have_status_request) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 255 | if (ssl->server || !hs->config->ocsp_stapling_enabled) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 256 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 257 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 258 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | uint8_t status_type; |
| 262 | CBS ocsp_response; |
| 263 | if (!CBS_get_u8(&status_request, &status_type) || |
| 264 | status_type != TLSEXT_STATUSTYPE_ocsp || |
| 265 | !CBS_get_u24_length_prefixed(&status_request, &ocsp_response) || |
| 266 | CBS_len(&ocsp_response) == 0 || |
| 267 | CBS_len(&status_request) != 0) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 268 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 269 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 270 | } |
| 271 | |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 272 | if (sk_CRYPTO_BUFFER_num(certs.get()) == 1) { |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 273 | hs->new_session->ocsp_response.reset( |
| 274 | CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool)); |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 275 | if (hs->new_session->ocsp_response == nullptr) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 276 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 277 | return false; |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 278 | } |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | if (have_sct) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 283 | if (ssl->server || !hs->config->signed_cert_timestamps_enabled) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 284 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 285 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 286 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 287 | } |
| 288 | |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 289 | if (!ssl_is_sct_list_valid(&sct)) { |
| 290 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 291 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 292 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 293 | } |
| 294 | |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 295 | if (sk_CRYPTO_BUFFER_num(certs.get()) == 1) { |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 296 | hs->new_session->signed_cert_timestamp_list.reset( |
| 297 | CRYPTO_BUFFER_new_from_CBS(&sct, ssl->ctx->pool)); |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 298 | if (hs->new_session->signed_cert_timestamp_list == nullptr) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 299 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 300 | return false; |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 301 | } |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 306 | // Store a null certificate list rather than an empty one if the peer didn't |
| 307 | // send certificates. |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 308 | if (sk_CRYPTO_BUFFER_num(certs.get()) == 0) { |
| 309 | certs.reset(); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 310 | } |
| 311 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 312 | hs->peer_pubkey = std::move(pkey); |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 313 | hs->new_session->certs = std::move(certs); |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 314 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 315 | if (!ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) { |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 316 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 317 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 318 | return false; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 319 | } |
| 320 | |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 321 | if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) { |
David Benjamin | 4087df9 | 2016-08-01 20:16:31 -0400 | [diff] [blame] | 322 | if (!allow_anonymous) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 323 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 324 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_CERTIFICATE_REQUIRED); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 325 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 326 | } |
| 327 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 328 | // OpenSSL returns X509_V_OK when no certificates are requested. This is |
| 329 | // 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] | 330 | hs->new_session->verify_result = X509_V_OK; |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 331 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 332 | // No certificate, so nothing more to do. |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 333 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 334 | } |
| 335 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 336 | hs->new_session->peer_sha256_valid = retain_sha256; |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 337 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 338 | } |
| 339 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 340 | bool tls13_process_certificate_verify(SSL_HANDSHAKE *hs, const SSLMessage &msg) { |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 341 | SSL *const ssl = hs->ssl; |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 342 | if (hs->peer_pubkey == NULL) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 343 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 344 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 345 | } |
| 346 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 347 | CBS body = msg.body, signature; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 348 | uint16_t signature_algorithm; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 349 | if (!CBS_get_u16(&body, &signature_algorithm) || |
| 350 | !CBS_get_u16_length_prefixed(&body, &signature) || |
| 351 | CBS_len(&body) != 0) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 352 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 353 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 354 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 355 | } |
| 356 | |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 357 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | ebad508 | 2020-02-03 19:32:19 -0500 | [diff] [blame] | 358 | if (!tls12_check_peer_sigalg(hs, &alert, signature_algorithm)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 359 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 360 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 361 | } |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 362 | hs->new_session->peer_signature_algorithm = signature_algorithm; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 363 | |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 364 | Array<uint8_t> input; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 365 | if (!tls13_get_cert_verify_signature_input( |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 366 | hs, &input, |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 367 | ssl->server ? ssl_cert_verify_client : ssl_cert_verify_server)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 368 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 369 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 370 | } |
| 371 | |
David Benjamin | 4dfd5af | 2019-07-19 17:34:37 -0400 | [diff] [blame] | 372 | if (!ssl_public_key_verify(ssl, signature, signature_algorithm, |
| 373 | hs->peer_pubkey.get(), input)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 374 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 375 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 376 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 377 | } |
| 378 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 379 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 380 | } |
| 381 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 382 | bool tls13_process_finished(SSL_HANDSHAKE *hs, const SSLMessage &msg, |
| 383 | bool use_saved_value) { |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 384 | SSL *const ssl = hs->ssl; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 385 | uint8_t verify_data_buf[EVP_MAX_MD_SIZE]; |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 386 | Span<const uint8_t> verify_data; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 387 | if (use_saved_value) { |
| 388 | assert(ssl->server); |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 389 | verify_data = hs->expected_client_finished(); |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 390 | } else { |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 391 | size_t len; |
| 392 | if (!tls13_finished_mac(hs, verify_data_buf, &len, !ssl->server)) { |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 393 | return false; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 394 | } |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 395 | verify_data = MakeConstSpan(verify_data_buf, len); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 396 | } |
| 397 | |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 398 | bool finished_ok = |
| 399 | CBS_mem_equal(&msg.body, verify_data.data(), verify_data.size()); |
David Benjamin | 04aa694 | 2016-08-19 14:51:10 -0400 | [diff] [blame] | 400 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 401 | finished_ok = true; |
David Benjamin | 04aa694 | 2016-08-19 14:51:10 -0400 | [diff] [blame] | 402 | #endif |
| 403 | if (!finished_ok) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 404 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 405 | OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 406 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 407 | } |
| 408 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 409 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 410 | } |
| 411 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 412 | bool tls13_add_certificate(SSL_HANDSHAKE *hs) { |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 413 | SSL *const ssl = hs->ssl; |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 414 | CERT *const cert = hs->config->cert.get(); |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 415 | DC *const dc = cert->dc.get(); |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 416 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 417 | ScopedCBB cbb; |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 418 | CBB *body, body_storage, certificate_list; |
| 419 | |
| 420 | if (hs->cert_compression_negotiated) { |
| 421 | if (!CBB_init(cbb.get(), 1024)) { |
| 422 | return false; |
| 423 | } |
| 424 | body = cbb.get(); |
| 425 | } else { |
| 426 | body = &body_storage; |
| 427 | if (!ssl->method->init_message(ssl, cbb.get(), body, SSL3_MT_CERTIFICATE)) { |
| 428 | return false; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | if (// The request context is always empty in the handshake. |
| 433 | !CBB_add_u8(body, 0) || |
| 434 | !CBB_add_u24_length_prefixed(body, &certificate_list)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 435 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 436 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 437 | } |
| 438 | |
Christopher Patton | 9cde848 | 2018-07-17 11:36:36 -0700 | [diff] [blame] | 439 | if (!ssl_has_certificate(hs)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 440 | return ssl_add_message_cbb(ssl, cbb.get()); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 441 | } |
| 442 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 443 | CRYPTO_BUFFER *leaf_buf = sk_CRYPTO_BUFFER_value(cert->chain.get(), 0); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 444 | CBB leaf, extensions; |
| 445 | if (!CBB_add_u24_length_prefixed(&certificate_list, &leaf) || |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 446 | !CBB_add_bytes(&leaf, CRYPTO_BUFFER_data(leaf_buf), |
| 447 | CRYPTO_BUFFER_len(leaf_buf)) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 448 | !CBB_add_u16_length_prefixed(&certificate_list, &extensions)) { |
| 449 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 450 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 451 | } |
| 452 | |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 453 | if (hs->scts_requested && cert->signed_cert_timestamp_list != nullptr) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 454 | CBB contents; |
| 455 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_certificate_timestamp) || |
| 456 | !CBB_add_u16_length_prefixed(&extensions, &contents) || |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 457 | !CBB_add_bytes( |
| 458 | &contents, |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 459 | CRYPTO_BUFFER_data(cert->signed_cert_timestamp_list.get()), |
| 460 | CRYPTO_BUFFER_len(cert->signed_cert_timestamp_list.get())) || |
Adam Langley | 6f5f49f | 2016-11-18 11:05:00 -0800 | [diff] [blame] | 461 | !CBB_flush(&extensions)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 462 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 463 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 467 | if (hs->ocsp_stapling_requested && cert->ocsp_response != NULL) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 468 | CBB contents, ocsp_response; |
| 469 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_status_request) || |
| 470 | !CBB_add_u16_length_prefixed(&extensions, &contents) || |
| 471 | !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) || |
| 472 | !CBB_add_u24_length_prefixed(&contents, &ocsp_response) || |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 473 | !CBB_add_bytes(&ocsp_response, |
| 474 | CRYPTO_BUFFER_data(cert->ocsp_response.get()), |
| 475 | CRYPTO_BUFFER_len(cert->ocsp_response.get())) || |
Adam Langley | 6f5f49f | 2016-11-18 11:05:00 -0800 | [diff] [blame] | 476 | !CBB_flush(&extensions)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 477 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 478 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 482 | if (ssl_signing_with_dc(hs)) { |
| 483 | const CRYPTO_BUFFER *raw = dc->raw.get(); |
David Benjamin | 85eef29 | 2019-03-29 23:38:36 -0500 | [diff] [blame] | 484 | CBB child; |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 485 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_delegated_credential) || |
David Benjamin | 85eef29 | 2019-03-29 23:38:36 -0500 | [diff] [blame] | 486 | !CBB_add_u16_length_prefixed(&extensions, &child) || |
| 487 | !CBB_add_bytes(&child, CRYPTO_BUFFER_data(raw), |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 488 | CRYPTO_BUFFER_len(raw)) || |
| 489 | !CBB_flush(&extensions)) { |
| 490 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 491 | return 0; |
| 492 | } |
Watson Ladd | 629f321 | 2019-06-11 17:46:52 -0700 | [diff] [blame] | 493 | ssl->s3->delegated_credential_used = true; |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 494 | } |
| 495 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 496 | for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cert->chain.get()); i++) { |
| 497 | CRYPTO_BUFFER *cert_buf = sk_CRYPTO_BUFFER_value(cert->chain.get(), i); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 498 | CBB child; |
| 499 | if (!CBB_add_u24_length_prefixed(&certificate_list, &child) || |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 500 | !CBB_add_bytes(&child, CRYPTO_BUFFER_data(cert_buf), |
| 501 | CRYPTO_BUFFER_len(cert_buf)) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 502 | !CBB_add_u16(&certificate_list, 0 /* no extensions */)) { |
| 503 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 504 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 508 | if (!hs->cert_compression_negotiated) { |
| 509 | return ssl_add_message_cbb(ssl, cbb.get()); |
| 510 | } |
| 511 | |
| 512 | Array<uint8_t> msg; |
| 513 | if (!CBBFinishArray(cbb.get(), &msg)) { |
| 514 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 515 | return false; |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | const CertCompressionAlg *alg = nullptr; |
David Benjamin | 8fe1584 | 2019-10-08 16:57:38 -0400 | [diff] [blame] | 519 | for (const auto &candidate : ssl->ctx->cert_compression_algs) { |
| 520 | if (candidate.alg_id == hs->cert_compression_alg_id) { |
| 521 | alg = &candidate; |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 522 | break; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | if (alg == nullptr || alg->compress == nullptr) { |
| 527 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 528 | return false; |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | CBB compressed; |
| 532 | body = &body_storage; |
| 533 | if (!ssl->method->init_message(ssl, cbb.get(), body, |
| 534 | SSL3_MT_COMPRESSED_CERTIFICATE) || |
| 535 | !CBB_add_u16(body, hs->cert_compression_alg_id) || |
| 536 | !CBB_add_u24(body, msg.size()) || |
| 537 | !CBB_add_u24_length_prefixed(body, &compressed) || |
Adam Langley | 8596795 | 2018-07-03 08:04:58 -0700 | [diff] [blame] | 538 | !alg->compress(ssl, &compressed, msg.data(), msg.size()) || |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 539 | !ssl_add_message_cbb(ssl, cbb.get())) { |
| 540 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 541 | return false; |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 542 | } |
| 543 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 544 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 545 | } |
| 546 | |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 547 | 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] | 548 | SSL *const ssl = hs->ssl; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 549 | uint16_t signature_algorithm; |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 550 | if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) { |
Adam Langley | e0afc85 | 2018-07-09 16:59:33 -0700 | [diff] [blame] | 551 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 552 | return ssl_private_key_failure; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 553 | } |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 554 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 555 | ScopedCBB cbb; |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 556 | CBB body; |
| 557 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 558 | SSL3_MT_CERTIFICATE_VERIFY) || |
| 559 | !CBB_add_u16(&body, signature_algorithm)) { |
| 560 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 561 | return ssl_private_key_failure; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 562 | } |
| 563 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 564 | // Sign the digest. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 565 | CBB child; |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 566 | 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] | 567 | uint8_t *sig; |
| 568 | size_t sig_len; |
| 569 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
| 570 | !CBB_reserve(&child, &sig, max_sig_len)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 571 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 572 | return ssl_private_key_failure; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 573 | } |
| 574 | |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 575 | Array<uint8_t> msg; |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 576 | if (!tls13_get_cert_verify_signature_input( |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 577 | hs, &msg, |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 578 | ssl->server ? ssl_cert_verify_server : ssl_cert_verify_client)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 579 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 580 | return ssl_private_key_failure; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 581 | } |
| 582 | |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 583 | enum ssl_private_key_result_t sign_result = ssl_private_key_sign( |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 584 | hs, sig, &sig_len, max_sig_len, signature_algorithm, msg); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 585 | if (sign_result != ssl_private_key_success) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 586 | return sign_result; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | if (!CBB_did_write(&child, sig_len) || |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 590 | !ssl_add_message_cbb(ssl, cbb.get())) { |
| 591 | return ssl_private_key_failure; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 592 | } |
| 593 | |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 594 | return ssl_private_key_success; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 595 | } |
| 596 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 597 | bool tls13_add_finished(SSL_HANDSHAKE *hs) { |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 598 | SSL *const ssl = hs->ssl; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 599 | size_t verify_data_len; |
| 600 | uint8_t verify_data[EVP_MAX_MD_SIZE]; |
| 601 | |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 602 | if (!tls13_finished_mac(hs, verify_data, &verify_data_len, ssl->server)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 603 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 604 | OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 605 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 606 | } |
| 607 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 608 | ScopedCBB cbb; |
| 609 | CBB body; |
| 610 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_FINISHED) || |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 611 | !CBB_add_bytes(&body, verify_data, verify_data_len) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 612 | !ssl_add_message_cbb(ssl, cbb.get())) { |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 613 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 614 | } |
| 615 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 616 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 617 | } |
Steven Valdez | 8e1c7be | 2016-07-26 12:39:22 -0400 | [diff] [blame] | 618 | |
Adam Langley | ba9ad66 | 2018-12-17 13:59:38 -0800 | [diff] [blame] | 619 | bool tls13_add_key_update(SSL *ssl, int update_requested) { |
| 620 | ScopedCBB cbb; |
| 621 | CBB body_cbb; |
| 622 | if (!ssl->method->init_message(ssl, cbb.get(), &body_cbb, |
| 623 | SSL3_MT_KEY_UPDATE) || |
| 624 | !CBB_add_u8(&body_cbb, update_requested) || |
| 625 | !ssl_add_message_cbb(ssl, cbb.get()) || |
| 626 | !tls13_rotate_traffic_key(ssl, evp_aead_seal)) { |
| 627 | return false; |
| 628 | } |
| 629 | |
| 630 | // Suppress KeyUpdate acknowledgments until this change is written to the |
| 631 | // wire. This prevents us from accumulating write obligations when read and |
| 632 | // write progress at different rates. See RFC 8446, section 4.6.3. |
| 633 | ssl->s3->key_update_pending = true; |
| 634 | |
| 635 | return true; |
| 636 | } |
| 637 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 638 | static bool tls13_receive_key_update(SSL *ssl, const SSLMessage &msg) { |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 639 | CBS body = msg.body; |
Steven Valdez | c4aa727 | 2016-10-03 12:25:56 -0400 | [diff] [blame] | 640 | uint8_t key_update_request; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 641 | if (!CBS_get_u8(&body, &key_update_request) || |
| 642 | CBS_len(&body) != 0 || |
Steven Valdez | c4aa727 | 2016-10-03 12:25:56 -0400 | [diff] [blame] | 643 | (key_update_request != SSL_KEY_UPDATE_NOT_REQUESTED && |
| 644 | key_update_request != SSL_KEY_UPDATE_REQUESTED)) { |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 645 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 646 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 647 | return false; |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 648 | } |
| 649 | |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 650 | if (!tls13_rotate_traffic_key(ssl, evp_aead_open)) { |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 651 | return false; |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 652 | } |
| 653 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 654 | // Acknowledge the KeyUpdate |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 655 | if (key_update_request == SSL_KEY_UPDATE_REQUESTED && |
Adam Langley | ba9ad66 | 2018-12-17 13:59:38 -0800 | [diff] [blame] | 656 | !ssl->s3->key_update_pending && |
| 657 | !tls13_add_key_update(ssl, SSL_KEY_UPDATE_NOT_REQUESTED)) { |
| 658 | return false; |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 659 | } |
| 660 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 661 | return true; |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 662 | } |
| 663 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 664 | bool tls13_post_handshake(SSL *ssl, const SSLMessage &msg) { |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 665 | if (msg.type == SSL3_MT_KEY_UPDATE) { |
Steven Valdez | 32635b8 | 2016-08-16 11:25:03 -0400 | [diff] [blame] | 666 | ssl->s3->key_update_count++; |
Alessandro Ghedini | 3cbb029 | 2018-12-13 13:53:57 +0000 | [diff] [blame] | 667 | if (ssl->quic_method != nullptr || |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 668 | ssl->s3->key_update_count > kMaxKeyUpdates) { |
Steven Valdez | 32635b8 | 2016-08-16 11:25:03 -0400 | [diff] [blame] | 669 | OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_KEY_UPDATES); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 670 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 671 | return false; |
Steven Valdez | 32635b8 | 2016-08-16 11:25:03 -0400 | [diff] [blame] | 672 | } |
| 673 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 674 | return tls13_receive_key_update(ssl, msg); |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 675 | } |
| 676 | |
Steven Valdez | 32635b8 | 2016-08-16 11:25:03 -0400 | [diff] [blame] | 677 | ssl->s3->key_update_count = 0; |
| 678 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 679 | if (msg.type == SSL3_MT_NEW_SESSION_TICKET && !ssl->server) { |
| 680 | return tls13_process_new_session_ticket(ssl, msg); |
Steven Valdez | 8e1c7be | 2016-07-26 12:39:22 -0400 | [diff] [blame] | 681 | } |
| 682 | |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 683 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
Steven Valdez | 8e1c7be | 2016-07-26 12:39:22 -0400 | [diff] [blame] | 684 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 685 | return false; |
Steven Valdez | 8e1c7be | 2016-07-26 12:39:22 -0400 | [diff] [blame] | 686 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 687 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 688 | BSSL_NAMESPACE_END |