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 | a75027b | 2021-07-20 15:07:22 -0400 | [diff] [blame] | 238 | SSLExtension status_request( |
| 239 | TLSEXT_TYPE_status_request, |
| 240 | !ssl->server && hs->config->ocsp_stapling_enabled); |
| 241 | SSLExtension sct( |
| 242 | TLSEXT_TYPE_certificate_timestamp, |
| 243 | !ssl->server && hs->config->signed_cert_timestamps_enabled); |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 244 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | a75027b | 2021-07-20 15:07:22 -0400 | [diff] [blame] | 245 | if (!ssl_parse_extensions(&extensions, &alert, {&status_request, &sct}, |
David Benjamin | c4ec14c | 2020-09-21 18:42:52 -0400 | [diff] [blame] | 246 | /*ignore_unknown=*/false)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 247 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 248 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 249 | } |
| 250 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 251 | // All Certificate extensions are parsed, but only the leaf extensions are |
| 252 | // stored. |
David Benjamin | a75027b | 2021-07-20 15:07:22 -0400 | [diff] [blame] | 253 | if (status_request.present) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 254 | uint8_t status_type; |
| 255 | CBS ocsp_response; |
David Benjamin | a75027b | 2021-07-20 15:07:22 -0400 | [diff] [blame] | 256 | if (!CBS_get_u8(&status_request.data, &status_type) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 257 | status_type != TLSEXT_STATUSTYPE_ocsp || |
David Benjamin | a75027b | 2021-07-20 15:07:22 -0400 | [diff] [blame] | 258 | !CBS_get_u24_length_prefixed(&status_request.data, &ocsp_response) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 259 | CBS_len(&ocsp_response) == 0 || |
David Benjamin | a75027b | 2021-07-20 15:07:22 -0400 | [diff] [blame] | 260 | CBS_len(&status_request.data) != 0) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 261 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 262 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 263 | } |
| 264 | |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 265 | if (sk_CRYPTO_BUFFER_num(certs.get()) == 1) { |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 266 | hs->new_session->ocsp_response.reset( |
| 267 | CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool)); |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 268 | if (hs->new_session->ocsp_response == nullptr) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 269 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 270 | return false; |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 271 | } |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
David Benjamin | a75027b | 2021-07-20 15:07:22 -0400 | [diff] [blame] | 275 | if (sct.present) { |
| 276 | if (!ssl_is_sct_list_valid(&sct.data)) { |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 277 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 278 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 279 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 280 | } |
| 281 | |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 282 | if (sk_CRYPTO_BUFFER_num(certs.get()) == 1) { |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 283 | hs->new_session->signed_cert_timestamp_list.reset( |
David Benjamin | a75027b | 2021-07-20 15:07:22 -0400 | [diff] [blame] | 284 | CRYPTO_BUFFER_new_from_CBS(&sct.data, ssl->ctx->pool)); |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 285 | if (hs->new_session->signed_cert_timestamp_list == nullptr) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 286 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 287 | return false; |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 288 | } |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 293 | // Store a null certificate list rather than an empty one if the peer didn't |
| 294 | // send certificates. |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 295 | if (sk_CRYPTO_BUFFER_num(certs.get()) == 0) { |
| 296 | certs.reset(); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 297 | } |
| 298 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 299 | hs->peer_pubkey = std::move(pkey); |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 300 | hs->new_session->certs = std::move(certs); |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 301 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 302 | if (!ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) { |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 303 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 304 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 305 | return false; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 306 | } |
| 307 | |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 308 | if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) { |
David Benjamin | 4087df9 | 2016-08-01 20:16:31 -0400 | [diff] [blame] | 309 | if (!allow_anonymous) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 310 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 311 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_CERTIFICATE_REQUIRED); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 312 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 313 | } |
| 314 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 315 | // OpenSSL returns X509_V_OK when no certificates are requested. This is |
| 316 | // 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] | 317 | hs->new_session->verify_result = X509_V_OK; |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 318 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 319 | // No certificate, so nothing more to do. |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 320 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 321 | } |
| 322 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 323 | hs->new_session->peer_sha256_valid = retain_sha256; |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 324 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 325 | } |
| 326 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 327 | bool tls13_process_certificate_verify(SSL_HANDSHAKE *hs, const SSLMessage &msg) { |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 328 | SSL *const ssl = hs->ssl; |
Adam Langley | 0c29425 | 2016-12-12 11:46:09 -0800 | [diff] [blame] | 329 | if (hs->peer_pubkey == NULL) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 330 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 331 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 332 | } |
| 333 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 334 | CBS body = msg.body, signature; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 335 | uint16_t signature_algorithm; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 336 | if (!CBS_get_u16(&body, &signature_algorithm) || |
| 337 | !CBS_get_u16_length_prefixed(&body, &signature) || |
| 338 | CBS_len(&body) != 0) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 339 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 340 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 341 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 342 | } |
| 343 | |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 344 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | ebad508 | 2020-02-03 19:32:19 -0500 | [diff] [blame] | 345 | if (!tls12_check_peer_sigalg(hs, &alert, signature_algorithm)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 346 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 347 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 348 | } |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 349 | hs->new_session->peer_signature_algorithm = signature_algorithm; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 350 | |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 351 | Array<uint8_t> input; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 352 | if (!tls13_get_cert_verify_signature_input( |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 353 | hs, &input, |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 354 | ssl->server ? ssl_cert_verify_client : ssl_cert_verify_server)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 355 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 356 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 357 | } |
| 358 | |
David Benjamin | 4dfd5af | 2019-07-19 17:34:37 -0400 | [diff] [blame] | 359 | if (!ssl_public_key_verify(ssl, signature, signature_algorithm, |
| 360 | hs->peer_pubkey.get(), input)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 361 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 362 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 363 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 364 | } |
| 365 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 366 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 367 | } |
| 368 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 369 | bool tls13_process_finished(SSL_HANDSHAKE *hs, const SSLMessage &msg, |
| 370 | bool use_saved_value) { |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 371 | SSL *const ssl = hs->ssl; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 372 | uint8_t verify_data_buf[EVP_MAX_MD_SIZE]; |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 373 | Span<const uint8_t> verify_data; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 374 | if (use_saved_value) { |
| 375 | assert(ssl->server); |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 376 | verify_data = hs->expected_client_finished(); |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 377 | } else { |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 378 | size_t len; |
| 379 | if (!tls13_finished_mac(hs, verify_data_buf, &len, !ssl->server)) { |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 380 | return false; |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 381 | } |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 382 | verify_data = MakeConstSpan(verify_data_buf, len); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 383 | } |
| 384 | |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 385 | bool finished_ok = |
| 386 | CBS_mem_equal(&msg.body, verify_data.data(), verify_data.size()); |
David Benjamin | 04aa694 | 2016-08-19 14:51:10 -0400 | [diff] [blame] | 387 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 388 | finished_ok = true; |
David Benjamin | 04aa694 | 2016-08-19 14:51:10 -0400 | [diff] [blame] | 389 | #endif |
| 390 | if (!finished_ok) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 391 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 392 | OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 393 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 394 | } |
| 395 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 396 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 397 | } |
| 398 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 399 | bool tls13_add_certificate(SSL_HANDSHAKE *hs) { |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 400 | SSL *const ssl = hs->ssl; |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 401 | CERT *const cert = hs->config->cert.get(); |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 402 | DC *const dc = cert->dc.get(); |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 403 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 404 | ScopedCBB cbb; |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 405 | CBB *body, body_storage, certificate_list; |
| 406 | |
| 407 | if (hs->cert_compression_negotiated) { |
| 408 | if (!CBB_init(cbb.get(), 1024)) { |
| 409 | return false; |
| 410 | } |
| 411 | body = cbb.get(); |
| 412 | } else { |
| 413 | body = &body_storage; |
| 414 | if (!ssl->method->init_message(ssl, cbb.get(), body, SSL3_MT_CERTIFICATE)) { |
| 415 | return false; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | if (// The request context is always empty in the handshake. |
| 420 | !CBB_add_u8(body, 0) || |
| 421 | !CBB_add_u24_length_prefixed(body, &certificate_list)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 422 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 423 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 424 | } |
| 425 | |
Christopher Patton | 9cde848 | 2018-07-17 11:36:36 -0700 | [diff] [blame] | 426 | if (!ssl_has_certificate(hs)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 427 | return ssl_add_message_cbb(ssl, cbb.get()); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 428 | } |
| 429 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 430 | CRYPTO_BUFFER *leaf_buf = sk_CRYPTO_BUFFER_value(cert->chain.get(), 0); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 431 | CBB leaf, extensions; |
| 432 | if (!CBB_add_u24_length_prefixed(&certificate_list, &leaf) || |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 433 | !CBB_add_bytes(&leaf, CRYPTO_BUFFER_data(leaf_buf), |
| 434 | CRYPTO_BUFFER_len(leaf_buf)) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 435 | !CBB_add_u16_length_prefixed(&certificate_list, &extensions)) { |
| 436 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 437 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 438 | } |
| 439 | |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 440 | if (hs->scts_requested && cert->signed_cert_timestamp_list != nullptr) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 441 | CBB contents; |
| 442 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_certificate_timestamp) || |
| 443 | !CBB_add_u16_length_prefixed(&extensions, &contents) || |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 444 | !CBB_add_bytes( |
| 445 | &contents, |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 446 | CRYPTO_BUFFER_data(cert->signed_cert_timestamp_list.get()), |
| 447 | CRYPTO_BUFFER_len(cert->signed_cert_timestamp_list.get())) || |
Adam Langley | 6f5f49f | 2016-11-18 11:05:00 -0800 | [diff] [blame] | 448 | !CBB_flush(&extensions)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 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 | } |
| 453 | |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 454 | if (hs->ocsp_stapling_requested && cert->ocsp_response != NULL) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 455 | CBB contents, ocsp_response; |
| 456 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_status_request) || |
| 457 | !CBB_add_u16_length_prefixed(&extensions, &contents) || |
| 458 | !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) || |
| 459 | !CBB_add_u24_length_prefixed(&contents, &ocsp_response) || |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 460 | !CBB_add_bytes(&ocsp_response, |
| 461 | CRYPTO_BUFFER_data(cert->ocsp_response.get()), |
| 462 | CRYPTO_BUFFER_len(cert->ocsp_response.get())) || |
Adam Langley | 6f5f49f | 2016-11-18 11:05:00 -0800 | [diff] [blame] | 463 | !CBB_flush(&extensions)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 464 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 465 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 469 | if (ssl_signing_with_dc(hs)) { |
| 470 | const CRYPTO_BUFFER *raw = dc->raw.get(); |
David Benjamin | 85eef29 | 2019-03-29 23:38:36 -0500 | [diff] [blame] | 471 | CBB child; |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 472 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_delegated_credential) || |
David Benjamin | 85eef29 | 2019-03-29 23:38:36 -0500 | [diff] [blame] | 473 | !CBB_add_u16_length_prefixed(&extensions, &child) || |
| 474 | !CBB_add_bytes(&child, CRYPTO_BUFFER_data(raw), |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 475 | CRYPTO_BUFFER_len(raw)) || |
| 476 | !CBB_flush(&extensions)) { |
| 477 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Anton Bikineev | 50e7ea5 | 2022-01-23 22:35:48 +0100 | [diff] [blame] | 478 | return false; |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 479 | } |
Watson Ladd | 629f321 | 2019-06-11 17:46:52 -0700 | [diff] [blame] | 480 | ssl->s3->delegated_credential_used = true; |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 481 | } |
| 482 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 483 | for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cert->chain.get()); i++) { |
| 484 | CRYPTO_BUFFER *cert_buf = sk_CRYPTO_BUFFER_value(cert->chain.get(), i); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 485 | CBB child; |
| 486 | if (!CBB_add_u24_length_prefixed(&certificate_list, &child) || |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 487 | !CBB_add_bytes(&child, CRYPTO_BUFFER_data(cert_buf), |
| 488 | CRYPTO_BUFFER_len(cert_buf)) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 489 | !CBB_add_u16(&certificate_list, 0 /* no extensions */)) { |
| 490 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 491 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 495 | if (!hs->cert_compression_negotiated) { |
| 496 | return ssl_add_message_cbb(ssl, cbb.get()); |
| 497 | } |
| 498 | |
| 499 | Array<uint8_t> msg; |
| 500 | if (!CBBFinishArray(cbb.get(), &msg)) { |
| 501 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 502 | return false; |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | const CertCompressionAlg *alg = nullptr; |
David Benjamin | 8fe1584 | 2019-10-08 16:57:38 -0400 | [diff] [blame] | 506 | for (const auto &candidate : ssl->ctx->cert_compression_algs) { |
| 507 | if (candidate.alg_id == hs->cert_compression_alg_id) { |
| 508 | alg = &candidate; |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 509 | break; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | if (alg == nullptr || alg->compress == nullptr) { |
| 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 | CBB compressed; |
| 519 | body = &body_storage; |
| 520 | if (!ssl->method->init_message(ssl, cbb.get(), body, |
| 521 | SSL3_MT_COMPRESSED_CERTIFICATE) || |
| 522 | !CBB_add_u16(body, hs->cert_compression_alg_id) || |
| 523 | !CBB_add_u24(body, msg.size()) || |
David Benjamin | 26f186b | 2021-06-08 19:17:58 -0400 | [diff] [blame] | 524 | !CBB_add_u24_length_prefixed(body, &compressed)) { |
| 525 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 526 | return false; |
| 527 | } |
| 528 | |
| 529 | SSL_HANDSHAKE_HINTS *const hints = hs->hints.get(); |
| 530 | if (hints && !hs->hints_requested && |
| 531 | hints->cert_compression_alg_id == hs->cert_compression_alg_id && |
| 532 | hints->cert_compression_input == MakeConstSpan(msg) && |
| 533 | !hints->cert_compression_output.empty()) { |
| 534 | if (!CBB_add_bytes(&compressed, hints->cert_compression_output.data(), |
| 535 | hints->cert_compression_output.size())) { |
| 536 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 537 | return false; |
| 538 | } |
| 539 | } else { |
| 540 | if (!alg->compress(ssl, &compressed, msg.data(), msg.size())) { |
| 541 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 542 | return false; |
| 543 | } |
| 544 | if (hints && hs->hints_requested) { |
| 545 | hints->cert_compression_alg_id = hs->cert_compression_alg_id; |
| 546 | if (!hints->cert_compression_input.CopyFrom(msg) || |
| 547 | !hints->cert_compression_output.CopyFrom( |
| 548 | MakeConstSpan(CBB_data(&compressed), CBB_len(&compressed)))) { |
| 549 | return false; |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | if (!ssl_add_message_cbb(ssl, cbb.get())) { |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 555 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 556 | return false; |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 557 | } |
| 558 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 559 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 560 | } |
| 561 | |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 562 | 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] | 563 | SSL *const ssl = hs->ssl; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 564 | uint16_t signature_algorithm; |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 565 | if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) { |
Adam Langley | e0afc85 | 2018-07-09 16:59:33 -0700 | [diff] [blame] | 566 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 567 | return ssl_private_key_failure; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 568 | } |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 569 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 570 | ScopedCBB cbb; |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 571 | CBB body; |
| 572 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 573 | SSL3_MT_CERTIFICATE_VERIFY) || |
| 574 | !CBB_add_u16(&body, signature_algorithm)) { |
| 575 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 576 | return ssl_private_key_failure; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 577 | } |
| 578 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 579 | CBB child; |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 580 | 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] | 581 | uint8_t *sig; |
| 582 | size_t sig_len; |
| 583 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
| 584 | !CBB_reserve(&child, &sig, max_sig_len)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 585 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 586 | return ssl_private_key_failure; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 587 | } |
| 588 | |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 589 | Array<uint8_t> msg; |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 590 | if (!tls13_get_cert_verify_signature_input( |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 591 | hs, &msg, |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 592 | ssl->server ? ssl_cert_verify_server : ssl_cert_verify_client)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 593 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 594 | return ssl_private_key_failure; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 595 | } |
| 596 | |
David Benjamin | 4a6c8fd | 2022-07-21 14:05:41 -0700 | [diff] [blame] | 597 | enum ssl_private_key_result_t sign_result = ssl_private_key_sign( |
| 598 | hs, sig, &sig_len, max_sig_len, signature_algorithm, msg); |
| 599 | if (sign_result != ssl_private_key_success) { |
| 600 | return sign_result; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | if (!CBB_did_write(&child, sig_len) || |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 604 | !ssl_add_message_cbb(ssl, cbb.get())) { |
| 605 | return ssl_private_key_failure; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 606 | } |
| 607 | |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 608 | return ssl_private_key_success; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 609 | } |
| 610 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 611 | bool tls13_add_finished(SSL_HANDSHAKE *hs) { |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 612 | SSL *const ssl = hs->ssl; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 613 | size_t verify_data_len; |
| 614 | uint8_t verify_data[EVP_MAX_MD_SIZE]; |
| 615 | |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 616 | if (!tls13_finished_mac(hs, verify_data, &verify_data_len, ssl->server)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 617 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 618 | OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 619 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 620 | } |
| 621 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 622 | ScopedCBB cbb; |
| 623 | CBB body; |
| 624 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_FINISHED) || |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 625 | !CBB_add_bytes(&body, verify_data, verify_data_len) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 626 | !ssl_add_message_cbb(ssl, cbb.get())) { |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 627 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 628 | } |
| 629 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 630 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 631 | } |
Steven Valdez | 8e1c7be | 2016-07-26 12:39:22 -0400 | [diff] [blame] | 632 | |
Adam Langley | ba9ad66 | 2018-12-17 13:59:38 -0800 | [diff] [blame] | 633 | bool tls13_add_key_update(SSL *ssl, int update_requested) { |
| 634 | ScopedCBB cbb; |
| 635 | CBB body_cbb; |
| 636 | if (!ssl->method->init_message(ssl, cbb.get(), &body_cbb, |
| 637 | SSL3_MT_KEY_UPDATE) || |
| 638 | !CBB_add_u8(&body_cbb, update_requested) || |
| 639 | !ssl_add_message_cbb(ssl, cbb.get()) || |
| 640 | !tls13_rotate_traffic_key(ssl, evp_aead_seal)) { |
| 641 | return false; |
| 642 | } |
| 643 | |
| 644 | // Suppress KeyUpdate acknowledgments until this change is written to the |
| 645 | // wire. This prevents us from accumulating write obligations when read and |
| 646 | // write progress at different rates. See RFC 8446, section 4.6.3. |
| 647 | ssl->s3->key_update_pending = true; |
| 648 | |
| 649 | return true; |
| 650 | } |
| 651 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 652 | static bool tls13_receive_key_update(SSL *ssl, const SSLMessage &msg) { |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 653 | CBS body = msg.body; |
Steven Valdez | c4aa727 | 2016-10-03 12:25:56 -0400 | [diff] [blame] | 654 | uint8_t key_update_request; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 655 | if (!CBS_get_u8(&body, &key_update_request) || |
| 656 | CBS_len(&body) != 0 || |
Steven Valdez | c4aa727 | 2016-10-03 12:25:56 -0400 | [diff] [blame] | 657 | (key_update_request != SSL_KEY_UPDATE_NOT_REQUESTED && |
| 658 | key_update_request != SSL_KEY_UPDATE_REQUESTED)) { |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 659 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 660 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 661 | return false; |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 662 | } |
| 663 | |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 664 | if (!tls13_rotate_traffic_key(ssl, evp_aead_open)) { |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 665 | return false; |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 666 | } |
| 667 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 668 | // Acknowledge the KeyUpdate |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 669 | if (key_update_request == SSL_KEY_UPDATE_REQUESTED && |
Adam Langley | ba9ad66 | 2018-12-17 13:59:38 -0800 | [diff] [blame] | 670 | !ssl->s3->key_update_pending && |
| 671 | !tls13_add_key_update(ssl, SSL_KEY_UPDATE_NOT_REQUESTED)) { |
| 672 | return false; |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 673 | } |
| 674 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 675 | return true; |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 676 | } |
| 677 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 678 | bool tls13_post_handshake(SSL *ssl, const SSLMessage &msg) { |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 679 | if (msg.type == SSL3_MT_KEY_UPDATE) { |
Steven Valdez | 32635b8 | 2016-08-16 11:25:03 -0400 | [diff] [blame] | 680 | ssl->s3->key_update_count++; |
Alessandro Ghedini | 3cbb029 | 2018-12-13 13:53:57 +0000 | [diff] [blame] | 681 | if (ssl->quic_method != nullptr || |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 682 | ssl->s3->key_update_count > kMaxKeyUpdates) { |
Steven Valdez | 32635b8 | 2016-08-16 11:25:03 -0400 | [diff] [blame] | 683 | OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_KEY_UPDATES); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 684 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 685 | return false; |
Steven Valdez | 32635b8 | 2016-08-16 11:25:03 -0400 | [diff] [blame] | 686 | } |
| 687 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 688 | return tls13_receive_key_update(ssl, msg); |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 689 | } |
| 690 | |
Steven Valdez | 32635b8 | 2016-08-16 11:25:03 -0400 | [diff] [blame] | 691 | ssl->s3->key_update_count = 0; |
| 692 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 693 | if (msg.type == SSL3_MT_NEW_SESSION_TICKET && !ssl->server) { |
| 694 | return tls13_process_new_session_ticket(ssl, msg); |
Steven Valdez | 8e1c7be | 2016-07-26 12:39:22 -0400 | [diff] [blame] | 695 | } |
| 696 | |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 697 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
Steven Valdez | 8e1c7be | 2016-07-26 12:39:22 -0400 | [diff] [blame] | 698 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 699 | return false; |
Steven Valdez | 8e1c7be | 2016-07-26 12:39:22 -0400 | [diff] [blame] | 700 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 701 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 702 | BSSL_NAMESPACE_END |