/src/boringssl/ssl/tls13_both.cc
Line | Count | Source |
1 | | // Copyright 2016 The BoringSSL Authors |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <openssl/ssl.h> |
16 | | |
17 | | #include <assert.h> |
18 | | #include <string.h> |
19 | | |
20 | | #include <utility> |
21 | | |
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> |
28 | | |
29 | | #include "../crypto/bytestring/internal.h" |
30 | | #include "../crypto/internal.h" |
31 | | #include "internal.h" |
32 | | |
33 | | |
34 | | BSSL_NAMESPACE_BEGIN |
35 | | |
36 | | // kMaxKeyUpdates is the number of consecutive KeyUpdates that will be |
37 | | // processed. Without this limit an attacker could force unbounded processing |
38 | | // without being able to return application data. |
39 | | static const uint8_t kMaxKeyUpdates = 32; |
40 | | |
41 | | const uint8_t kHelloRetryRequest[SSL3_RANDOM_SIZE] = { |
42 | | 0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, |
43 | | 0x02, 0x1e, 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, |
44 | | 0x8c, 0x5e, 0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, |
45 | | }; |
46 | | |
47 | | // See RFC 8446, section 4.1.3. |
48 | | const uint8_t kTLS12DowngradeRandom[8] = {0x44, 0x4f, 0x57, 0x4e, |
49 | | 0x47, 0x52, 0x44, 0x00}; |
50 | | const uint8_t kTLS13DowngradeRandom[8] = {0x44, 0x4f, 0x57, 0x4e, |
51 | | 0x47, 0x52, 0x44, 0x01}; |
52 | | |
53 | | // This is a non-standard randomly-generated value. |
54 | | const uint8_t kJDK11DowngradeRandom[8] = {0xed, 0xbf, 0xb4, 0xa8, |
55 | | 0xc2, 0x47, 0x10, 0xff}; |
56 | | |
57 | | bool tls13_get_cert_verify_signature_input( |
58 | | SSL_HANDSHAKE *hs, Array<uint8_t> *out, |
59 | 4.25k | enum ssl_cert_verify_context_t cert_verify_context) { |
60 | 4.25k | ScopedCBB cbb; |
61 | 4.25k | if (!CBB_init(cbb.get(), 64 + 33 + 1 + 2 * EVP_MAX_MD_SIZE)) { |
62 | 0 | return false; |
63 | 0 | } |
64 | | |
65 | 276k | for (size_t i = 0; i < 64; i++) { |
66 | 272k | if (!CBB_add_u8(cbb.get(), 0x20)) { |
67 | 0 | return false; |
68 | 0 | } |
69 | 272k | } |
70 | | |
71 | 4.25k | Span<const char> context; |
72 | 4.25k | if (cert_verify_context == ssl_cert_verify_server) { |
73 | 4.18k | static const char kContext[] = "TLS 1.3, server CertificateVerify"; |
74 | 4.18k | context = kContext; |
75 | 4.18k | } else if (cert_verify_context == ssl_cert_verify_client) { |
76 | 64 | static const char kContext[] = "TLS 1.3, client CertificateVerify"; |
77 | 64 | context = kContext; |
78 | 64 | } else if (cert_verify_context == ssl_cert_verify_channel_id) { |
79 | 5 | static const char kContext[] = "TLS 1.3, Channel ID"; |
80 | 5 | context = kContext; |
81 | 5 | } else { |
82 | 0 | return false; |
83 | 0 | } |
84 | | |
85 | | // Note |context| includes the NUL byte separator. |
86 | 4.25k | if (!CBB_add_bytes(cbb.get(), |
87 | 4.25k | reinterpret_cast<const uint8_t *>(context.data()), |
88 | 4.25k | context.size())) { |
89 | 0 | return false; |
90 | 0 | } |
91 | | |
92 | 4.25k | uint8_t context_hash[EVP_MAX_MD_SIZE]; |
93 | 4.25k | size_t context_hash_len; |
94 | 4.25k | if (!hs->transcript.GetHash(context_hash, &context_hash_len) || |
95 | 4.25k | !CBB_add_bytes(cbb.get(), context_hash, context_hash_len) || |
96 | 4.25k | !CBBFinishArray(cbb.get(), out)) { |
97 | 0 | return false; |
98 | 0 | } |
99 | | |
100 | 4.25k | return true; |
101 | 4.25k | } |
102 | | |
103 | | bool tls13_process_certificate(SSL_HANDSHAKE *hs, const SSLMessage &msg, |
104 | 1.49k | bool allow_anonymous) { |
105 | 1.49k | SSL *const ssl = hs->ssl; |
106 | 1.49k | CBS body = msg.body; |
107 | 1.49k | bssl::UniquePtr<CRYPTO_BUFFER> decompressed; |
108 | | |
109 | 1.49k | if (msg.type == SSL3_MT_COMPRESSED_CERTIFICATE) { |
110 | 24 | CBS compressed; |
111 | 24 | uint16_t alg_id; |
112 | 24 | uint32_t uncompressed_len; |
113 | | |
114 | 24 | if (!CBS_get_u16(&body, &alg_id) || |
115 | 23 | !CBS_get_u24(&body, &uncompressed_len) || |
116 | 21 | !CBS_get_u24_length_prefixed(&body, &compressed) || |
117 | 19 | CBS_len(&body) != 0) { |
118 | 7 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
119 | 7 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
120 | 7 | return false; |
121 | 7 | } |
122 | | |
123 | 17 | if (uncompressed_len > ssl->max_cert_list) { |
124 | 15 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
125 | 15 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNCOMPRESSED_CERT_TOO_LARGE); |
126 | 15 | ERR_add_error_dataf("requested=%u", |
127 | 15 | static_cast<unsigned>(uncompressed_len)); |
128 | 15 | return false; |
129 | 15 | } |
130 | | |
131 | 2 | ssl_cert_decompression_func_t decompress = nullptr; |
132 | 2 | for (const auto &alg : ssl->ctx->cert_compression_algs) { |
133 | 0 | if (alg.alg_id == alg_id) { |
134 | 0 | decompress = alg.decompress; |
135 | 0 | break; |
136 | 0 | } |
137 | 0 | } |
138 | | |
139 | 2 | if (decompress == nullptr) { |
140 | 2 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
141 | 2 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERT_COMPRESSION_ALG); |
142 | 2 | ERR_add_error_dataf("alg=%d", static_cast<int>(alg_id)); |
143 | 2 | return false; |
144 | 2 | } |
145 | | |
146 | 0 | CRYPTO_BUFFER *decompressed_ptr = nullptr; |
147 | 0 | if (!decompress(ssl, &decompressed_ptr, uncompressed_len, |
148 | 0 | CBS_data(&compressed), CBS_len(&compressed))) { |
149 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
150 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_DECOMPRESSION_FAILED); |
151 | 0 | ERR_add_error_dataf("alg=%d", static_cast<int>(alg_id)); |
152 | 0 | return false; |
153 | 0 | } |
154 | 0 | decompressed.reset(decompressed_ptr); |
155 | |
|
156 | 0 | if (CRYPTO_BUFFER_len(decompressed_ptr) != uncompressed_len) { |
157 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
158 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_DECOMPRESSION_FAILED); |
159 | 0 | ERR_add_error_dataf( |
160 | 0 | "alg=%d got=%u expected=%u", static_cast<int>(alg_id), |
161 | 0 | static_cast<unsigned>(CRYPTO_BUFFER_len(decompressed_ptr)), |
162 | 0 | static_cast<unsigned>(uncompressed_len)); |
163 | 0 | return false; |
164 | 0 | } |
165 | | |
166 | 0 | CBS_init(&body, CRYPTO_BUFFER_data(decompressed_ptr), |
167 | 0 | CRYPTO_BUFFER_len(decompressed_ptr)); |
168 | 1.46k | } else { |
169 | 1.46k | assert(msg.type == SSL3_MT_CERTIFICATE); |
170 | 1.46k | } |
171 | | |
172 | 1.46k | CBS context, certificate_list; |
173 | 1.46k | if (!CBS_get_u8_length_prefixed(&body, &context) || // |
174 | 1.46k | CBS_len(&context) != 0 || // |
175 | 1.45k | !CBS_get_u24_length_prefixed(&body, &certificate_list) || // |
176 | 1.45k | CBS_len(&body) != 0) { |
177 | 24 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
178 | 24 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
179 | 24 | return false; |
180 | 24 | } |
181 | | |
182 | 1.44k | UniquePtr<STACK_OF(CRYPTO_BUFFER)> certs(sk_CRYPTO_BUFFER_new_null()); |
183 | 1.44k | if (!certs) { |
184 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
185 | 0 | return false; |
186 | 0 | } |
187 | | |
188 | 1.44k | const bool retain_sha256 = |
189 | 1.44k | ssl->server && hs->config->retain_only_sha256_of_client_certs; |
190 | 1.44k | UniquePtr<EVP_PKEY> pkey; |
191 | 3.58k | while (CBS_len(&certificate_list) > 0) { |
192 | 2.35k | CBS certificate, extensions; |
193 | 2.35k | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) || |
194 | 2.34k | !CBS_get_u16_length_prefixed(&certificate_list, &extensions) || |
195 | 2.33k | CBS_len(&certificate) == 0) { |
196 | 26 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
197 | 26 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
198 | 26 | return false; |
199 | 26 | } |
200 | | |
201 | 2.32k | const bool is_leaf = sk_CRYPTO_BUFFER_num(certs.get()) == 0; |
202 | 2.32k | if (is_leaf) { |
203 | 1.43k | pkey = ssl_cert_parse_pubkey(&certificate); |
204 | 1.43k | if (!pkey) { |
205 | 103 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
206 | 103 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
207 | 103 | return false; |
208 | 103 | } |
209 | | // TLS 1.3 always uses certificate keys for signing thus the correct |
210 | | // keyUsage is enforced. |
211 | 1.32k | if (!ssl_cert_check_key_usage(&certificate, |
212 | 1.32k | key_usage_digital_signature)) { |
213 | 51 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
214 | 51 | return false; |
215 | 51 | } |
216 | | |
217 | 1.27k | if (retain_sha256) { |
218 | | // Retain the hash of the leaf certificate if requested. |
219 | 0 | SHA256(CBS_data(&certificate), CBS_len(&certificate), |
220 | 0 | hs->new_session->peer_sha256); |
221 | 0 | } |
222 | 1.27k | } |
223 | | |
224 | 2.17k | UniquePtr<CRYPTO_BUFFER> buf( |
225 | 2.17k | CRYPTO_BUFFER_new_from_CBS(&certificate, ssl->ctx->pool)); |
226 | 2.17k | if (!buf || // |
227 | 2.17k | !PushToStack(certs.get(), std::move(buf))) { |
228 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
229 | 0 | return false; |
230 | 0 | } |
231 | | |
232 | | // Parse out the extensions. |
233 | 2.17k | SSLExtension status_request( |
234 | 2.17k | TLSEXT_TYPE_status_request, |
235 | 2.17k | !ssl->server && hs->config->ocsp_stapling_enabled); |
236 | 2.17k | SSLExtension sct( |
237 | 2.17k | TLSEXT_TYPE_certificate_timestamp, |
238 | 2.17k | !ssl->server && hs->config->signed_cert_timestamps_enabled); |
239 | 2.17k | SSLExtension trust_anchors( |
240 | 2.17k | TLSEXT_TYPE_trust_anchors, |
241 | 2.17k | !ssl->server && is_leaf && |
242 | 1.18k | hs->config->requested_trust_anchors.has_value()); |
243 | 2.17k | uint8_t alert = SSL_AD_DECODE_ERROR; |
244 | 2.17k | if (!ssl_parse_extensions(&extensions, &alert, |
245 | 2.17k | {&status_request, &sct, &trust_anchors}, |
246 | 2.17k | /*ignore_unknown=*/false)) { |
247 | 28 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
248 | 28 | return false; |
249 | 28 | } |
250 | | |
251 | | // All Certificate extensions are parsed, but only the leaf extensions are |
252 | | // stored. |
253 | 2.14k | if (status_request.present) { |
254 | 7 | uint8_t status_type; |
255 | 7 | CBS ocsp_response; |
256 | 7 | if (!CBS_get_u8(&status_request.data, &status_type) || |
257 | 6 | status_type != TLSEXT_STATUSTYPE_ocsp || |
258 | 5 | !CBS_get_u24_length_prefixed(&status_request.data, &ocsp_response) || |
259 | 5 | CBS_len(&ocsp_response) == 0 || CBS_len(&status_request.data) != 0) { |
260 | 5 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
261 | 5 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
262 | 5 | return false; |
263 | 5 | } |
264 | | |
265 | 2 | if (sk_CRYPTO_BUFFER_num(certs.get()) == 1) { |
266 | 2 | hs->new_session->ocsp_response.reset( |
267 | 2 | CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool)); |
268 | 2 | if (hs->new_session->ocsp_response == nullptr) { |
269 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
270 | 0 | return false; |
271 | 0 | } |
272 | 2 | } |
273 | 2 | } |
274 | | |
275 | 2.14k | if (sct.present) { |
276 | 7 | if (!ssl_is_sct_list_valid(&sct.data)) { |
277 | 5 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
278 | 5 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
279 | 5 | return false; |
280 | 5 | } |
281 | | |
282 | 2 | if (sk_CRYPTO_BUFFER_num(certs.get()) == 1) { |
283 | 2 | hs->new_session->signed_cert_timestamp_list.reset( |
284 | 2 | CRYPTO_BUFFER_new_from_CBS(&sct.data, ssl->ctx->pool)); |
285 | 2 | if (hs->new_session->signed_cert_timestamp_list == nullptr) { |
286 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
287 | 0 | return false; |
288 | 0 | } |
289 | 2 | } |
290 | 2 | } |
291 | | |
292 | 2.13k | if (trust_anchors.present) { |
293 | 0 | if (CBS_len(&trust_anchors.data) != 0) { |
294 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
295 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
296 | 0 | return false; |
297 | 0 | } |
298 | 0 | hs->peer_matched_trust_anchor = true; |
299 | 0 | } |
300 | 2.13k | } |
301 | | |
302 | | // Store a null certificate list rather than an empty one if the peer didn't |
303 | | // send certificates. |
304 | 1.22k | if (sk_CRYPTO_BUFFER_num(certs.get()) == 0) { |
305 | 2 | certs.reset(); |
306 | 2 | } |
307 | | |
308 | 1.22k | hs->peer_pubkey = std::move(pkey); |
309 | 1.22k | hs->new_session->certs = std::move(certs); |
310 | | |
311 | 1.22k | if (!ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) { |
312 | 216 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
313 | 216 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
314 | 216 | return false; |
315 | 216 | } |
316 | | |
317 | 1.00k | if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) { |
318 | 2 | if (!allow_anonymous) { |
319 | 1 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
320 | 1 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_CERTIFICATE_REQUIRED); |
321 | 1 | return false; |
322 | 1 | } |
323 | | |
324 | | // OpenSSL returns X509_V_OK when no certificates are requested. This is |
325 | | // classed by them as a bug, but it's assumed by at least NGINX. |
326 | 1 | hs->new_session->verify_result = X509_V_OK; |
327 | | |
328 | | // No certificate, so nothing more to do. |
329 | 1 | return true; |
330 | 2 | } |
331 | | |
332 | 1.00k | hs->new_session->peer_sha256_valid = retain_sha256; |
333 | 1.00k | return true; |
334 | 1.00k | } |
335 | | |
336 | | bool tls13_process_certificate_verify(SSL_HANDSHAKE *hs, |
337 | 950 | const SSLMessage &msg) { |
338 | 950 | SSL *const ssl = hs->ssl; |
339 | 950 | if (hs->peer_pubkey == nullptr) { |
340 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
341 | 0 | return false; |
342 | 0 | } |
343 | | |
344 | 950 | CBS body = msg.body, signature; |
345 | 950 | uint16_t signature_algorithm; |
346 | 950 | if (!CBS_get_u16(&body, &signature_algorithm) || // |
347 | 948 | !CBS_get_u16_length_prefixed(&body, &signature) || // |
348 | 943 | CBS_len(&body) != 0) { |
349 | 14 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
350 | 14 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
351 | 14 | return false; |
352 | 14 | } |
353 | | |
354 | 936 | uint8_t alert = SSL_AD_DECODE_ERROR; |
355 | 936 | if (!tls12_check_peer_sigalg(hs, &alert, signature_algorithm, |
356 | 936 | hs->peer_pubkey.get())) { |
357 | 14 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
358 | 14 | return false; |
359 | 14 | } |
360 | 922 | hs->new_session->peer_signature_algorithm = signature_algorithm; |
361 | | |
362 | 922 | Array<uint8_t> input; |
363 | 922 | if (!tls13_get_cert_verify_signature_input( |
364 | 922 | hs, &input, |
365 | 922 | ssl->server ? ssl_cert_verify_client : ssl_cert_verify_server)) { |
366 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
367 | 0 | return false; |
368 | 0 | } |
369 | | |
370 | 922 | if (!ssl_public_key_verify(ssl, signature, signature_algorithm, |
371 | 922 | hs->peer_pubkey.get(), input)) { |
372 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
373 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
374 | 0 | return false; |
375 | 0 | } |
376 | | |
377 | 922 | return true; |
378 | 922 | } |
379 | | |
380 | | bool tls13_process_finished(SSL_HANDSHAKE *hs, const SSLMessage &msg, |
381 | 3.12k | bool use_saved_value) { |
382 | 3.12k | SSL *const ssl = hs->ssl; |
383 | 3.12k | uint8_t verify_data_buf[EVP_MAX_MD_SIZE]; |
384 | 3.12k | Span<const uint8_t> verify_data; |
385 | 3.12k | if (use_saved_value) { |
386 | 3 | assert(ssl->server); |
387 | 3 | verify_data = hs->expected_client_finished; |
388 | 3.12k | } else { |
389 | 3.12k | size_t len; |
390 | 3.12k | if (!tls13_finished_mac(hs, verify_data_buf, &len, !ssl->server)) { |
391 | 0 | return false; |
392 | 0 | } |
393 | 3.12k | verify_data = Span(verify_data_buf, len); |
394 | 3.12k | } |
395 | | |
396 | 3.12k | bool finished_ok = |
397 | 3.12k | CBS_mem_equal(&msg.body, verify_data.data(), verify_data.size()); |
398 | 3.12k | if (CRYPTO_fuzzer_mode_enabled()) { |
399 | 3.10k | finished_ok = true; |
400 | 3.10k | } |
401 | 3.12k | if (!finished_ok) { |
402 | 2 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
403 | 2 | OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED); |
404 | 2 | return false; |
405 | 2 | } |
406 | | |
407 | 3.12k | return true; |
408 | 3.12k | } |
409 | | |
410 | 3.32k | bool tls13_add_certificate(SSL_HANDSHAKE *hs) { |
411 | 3.32k | SSL *const ssl = hs->ssl; |
412 | 3.32k | const SSL_CREDENTIAL *cred = hs->credential.get(); |
413 | | |
414 | 3.32k | ScopedCBB cbb; |
415 | 3.32k | CBB *body, body_storage, certificate_list; |
416 | | |
417 | 3.32k | if (hs->cert_compression_negotiated) { |
418 | 0 | if (!CBB_init(cbb.get(), 1024)) { |
419 | 0 | return false; |
420 | 0 | } |
421 | 0 | body = cbb.get(); |
422 | 3.32k | } else { |
423 | 3.32k | body = &body_storage; |
424 | 3.32k | if (!ssl->method->init_message(ssl, cbb.get(), body, SSL3_MT_CERTIFICATE)) { |
425 | 0 | return false; |
426 | 0 | } |
427 | 3.32k | } |
428 | | |
429 | 3.32k | if ( // The request context is always empty in the handshake. |
430 | 3.32k | !CBB_add_u8(body, 0) || |
431 | 3.32k | !CBB_add_u24_length_prefixed(body, &certificate_list)) { |
432 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
433 | 0 | return false; |
434 | 0 | } |
435 | | |
436 | 3.32k | if (hs->credential == nullptr) { |
437 | 0 | return ssl_add_message_cbb(ssl, cbb.get()); |
438 | 0 | } |
439 | | |
440 | 3.32k | assert(hs->credential->UsesX509()); |
441 | 3.32k | CRYPTO_BUFFER *leaf_buf = sk_CRYPTO_BUFFER_value(cred->chain.get(), 0); |
442 | 3.32k | CBB leaf, extensions; |
443 | 3.32k | if (!CBB_add_u24_length_prefixed(&certificate_list, &leaf) || |
444 | 3.32k | !CBB_add_bytes(&leaf, CRYPTO_BUFFER_data(leaf_buf), |
445 | 3.32k | CRYPTO_BUFFER_len(leaf_buf)) || |
446 | 3.32k | !CBB_add_u16_length_prefixed(&certificate_list, &extensions)) { |
447 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
448 | 0 | return false; |
449 | 0 | } |
450 | | |
451 | 3.32k | if (hs->scts_requested && cred->signed_cert_timestamp_list != nullptr) { |
452 | 2.47k | CBB contents; |
453 | 2.47k | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_certificate_timestamp) || |
454 | 2.47k | !CBB_add_u16_length_prefixed(&extensions, &contents) || |
455 | 2.47k | !CBB_add_bytes( |
456 | 2.47k | &contents, |
457 | 2.47k | CRYPTO_BUFFER_data(cred->signed_cert_timestamp_list.get()), |
458 | 2.47k | CRYPTO_BUFFER_len(cred->signed_cert_timestamp_list.get())) || |
459 | 2.47k | !CBB_flush(&extensions)) { |
460 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
461 | 0 | return false; |
462 | 0 | } |
463 | 2.47k | } |
464 | | |
465 | 3.32k | if (hs->ocsp_stapling_requested && cred->ocsp_response != nullptr) { |
466 | 2.37k | CBB contents, ocsp_response; |
467 | 2.37k | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_status_request) || |
468 | 2.37k | !CBB_add_u16_length_prefixed(&extensions, &contents) || |
469 | 2.37k | !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) || |
470 | 2.37k | !CBB_add_u24_length_prefixed(&contents, &ocsp_response) || |
471 | 2.37k | !CBB_add_bytes(&ocsp_response, |
472 | 2.37k | CRYPTO_BUFFER_data(cred->ocsp_response.get()), |
473 | 2.37k | CRYPTO_BUFFER_len(cred->ocsp_response.get())) || |
474 | 2.37k | !CBB_flush(&extensions)) { |
475 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
476 | 0 | return false; |
477 | 0 | } |
478 | 2.37k | } |
479 | | |
480 | 3.32k | if (cred->type == SSLCredentialType::kDelegated) { |
481 | 0 | CBB child; |
482 | 0 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_delegated_credential) || |
483 | 0 | !CBB_add_u16_length_prefixed(&extensions, &child) || |
484 | 0 | !CBB_add_bytes(&child, CRYPTO_BUFFER_data(cred->dc.get()), |
485 | 0 | CRYPTO_BUFFER_len(cred->dc.get())) || |
486 | 0 | !CBB_flush(&extensions)) { |
487 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
488 | 0 | return false; |
489 | 0 | } |
490 | 0 | } |
491 | | |
492 | 3.32k | if (hs->matched_peer_trust_anchor) { |
493 | | // Let the peer know we matched a requested trust anchor. |
494 | 0 | CBB empty_contents; |
495 | 0 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_trust_anchors) || // |
496 | 0 | !CBB_add_u16_length_prefixed(&extensions, &empty_contents) || // |
497 | 0 | !CBB_flush(&extensions)) { |
498 | 0 | return false; |
499 | 0 | } |
500 | 0 | } |
501 | | |
502 | 3.32k | for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cred->chain.get()); i++) { |
503 | 0 | CRYPTO_BUFFER *cert_buf = sk_CRYPTO_BUFFER_value(cred->chain.get(), i); |
504 | 0 | CBB child; |
505 | 0 | if (!CBB_add_u24_length_prefixed(&certificate_list, &child) || |
506 | 0 | !CBB_add_bytes(&child, CRYPTO_BUFFER_data(cert_buf), |
507 | 0 | CRYPTO_BUFFER_len(cert_buf)) || |
508 | 0 | !CBB_add_u16(&certificate_list, 0 /* no extensions */)) { |
509 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
510 | 0 | return false; |
511 | 0 | } |
512 | 0 | } |
513 | | |
514 | 3.32k | if (!hs->cert_compression_negotiated) { |
515 | 3.32k | return ssl_add_message_cbb(ssl, cbb.get()); |
516 | 3.32k | } |
517 | | |
518 | 0 | Array<uint8_t> msg; |
519 | 0 | if (!CBBFinishArray(cbb.get(), &msg)) { |
520 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
521 | 0 | return false; |
522 | 0 | } |
523 | | |
524 | 0 | const CertCompressionAlg *alg = nullptr; |
525 | 0 | for (const auto &candidate : ssl->ctx->cert_compression_algs) { |
526 | 0 | if (candidate.alg_id == hs->cert_compression_alg_id) { |
527 | 0 | alg = &candidate; |
528 | 0 | break; |
529 | 0 | } |
530 | 0 | } |
531 | |
|
532 | 0 | if (alg == nullptr || alg->compress == nullptr) { |
533 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
534 | 0 | return false; |
535 | 0 | } |
536 | | |
537 | 0 | CBB compressed; |
538 | 0 | body = &body_storage; |
539 | 0 | if (!ssl->method->init_message(ssl, cbb.get(), body, |
540 | 0 | SSL3_MT_COMPRESSED_CERTIFICATE) || |
541 | 0 | !CBB_add_u16(body, hs->cert_compression_alg_id) || |
542 | 0 | msg.size() > (1u << 24) - 1 || // |
543 | 0 | !CBB_add_u24(body, static_cast<uint32_t>(msg.size())) || |
544 | 0 | !CBB_add_u24_length_prefixed(body, &compressed)) { |
545 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
546 | 0 | return false; |
547 | 0 | } |
548 | | |
549 | 0 | SSL_HANDSHAKE_HINTS *const hints = hs->hints.get(); |
550 | 0 | if (hints && !hs->hints_requested && |
551 | 0 | hints->cert_compression_alg_id == hs->cert_compression_alg_id && |
552 | 0 | hints->cert_compression_input == Span(msg) && |
553 | 0 | !hints->cert_compression_output.empty()) { |
554 | 0 | if (!CBB_add_bytes(&compressed, hints->cert_compression_output.data(), |
555 | 0 | hints->cert_compression_output.size())) { |
556 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
557 | 0 | return false; |
558 | 0 | } |
559 | 0 | } else { |
560 | 0 | if (!alg->compress(ssl, &compressed, msg.data(), msg.size())) { |
561 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
562 | 0 | return false; |
563 | 0 | } |
564 | 0 | if (hints && hs->hints_requested) { |
565 | 0 | hints->cert_compression_alg_id = hs->cert_compression_alg_id; |
566 | 0 | if (!hints->cert_compression_input.CopyFrom(msg) || |
567 | 0 | !hints->cert_compression_output.CopyFrom(CBBAsSpan(&compressed))) { |
568 | 0 | return false; |
569 | 0 | } |
570 | 0 | } |
571 | 0 | } |
572 | | |
573 | 0 | if (!ssl_add_message_cbb(ssl, cbb.get())) { |
574 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
575 | 0 | return false; |
576 | 0 | } |
577 | | |
578 | 0 | return true; |
579 | 0 | } |
580 | | |
581 | 3.32k | enum ssl_private_key_result_t tls13_add_certificate_verify(SSL_HANDSHAKE *hs) { |
582 | 3.32k | SSL *const ssl = hs->ssl; |
583 | 3.32k | assert(hs->signature_algorithm != 0); |
584 | 3.32k | ScopedCBB cbb; |
585 | 3.32k | CBB body; |
586 | 3.32k | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
587 | 3.32k | SSL3_MT_CERTIFICATE_VERIFY) || |
588 | 3.32k | !CBB_add_u16(&body, hs->signature_algorithm)) { |
589 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
590 | 0 | return ssl_private_key_failure; |
591 | 0 | } |
592 | | |
593 | 3.32k | CBB child; |
594 | 3.32k | const size_t max_sig_len = EVP_PKEY_size(hs->credential->pubkey.get()); |
595 | 3.32k | uint8_t *sig; |
596 | 3.32k | size_t sig_len; |
597 | 3.32k | if (!CBB_add_u16_length_prefixed(&body, &child) || |
598 | 3.32k | !CBB_reserve(&child, &sig, max_sig_len)) { |
599 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
600 | 0 | return ssl_private_key_failure; |
601 | 0 | } |
602 | | |
603 | 3.32k | Array<uint8_t> msg; |
604 | 3.32k | if (!tls13_get_cert_verify_signature_input( |
605 | 3.32k | hs, &msg, |
606 | 3.32k | ssl->server ? ssl_cert_verify_server : ssl_cert_verify_client)) { |
607 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
608 | 0 | return ssl_private_key_failure; |
609 | 0 | } |
610 | | |
611 | 3.32k | enum ssl_private_key_result_t sign_result = ssl_private_key_sign( |
612 | 3.32k | hs, sig, &sig_len, max_sig_len, hs->signature_algorithm, msg); |
613 | 3.32k | if (sign_result != ssl_private_key_success) { |
614 | 0 | return sign_result; |
615 | 0 | } |
616 | | |
617 | 3.32k | if (!CBB_did_write(&child, sig_len) || // |
618 | 3.32k | !ssl_add_message_cbb(ssl, cbb.get())) { |
619 | 0 | return ssl_private_key_failure; |
620 | 0 | } |
621 | | |
622 | 3.32k | return ssl_private_key_success; |
623 | 3.32k | } |
624 | | |
625 | 3.99k | bool tls13_add_finished(SSL_HANDSHAKE *hs) { |
626 | 3.99k | SSL *const ssl = hs->ssl; |
627 | 3.99k | size_t verify_data_len; |
628 | 3.99k | uint8_t verify_data[EVP_MAX_MD_SIZE]; |
629 | | |
630 | 3.99k | if (!tls13_finished_mac(hs, verify_data, &verify_data_len, ssl->server)) { |
631 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
632 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED); |
633 | 0 | return false; |
634 | 0 | } |
635 | | |
636 | 3.99k | ScopedCBB cbb; |
637 | 3.99k | CBB body; |
638 | 3.99k | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_FINISHED) || |
639 | 3.99k | !CBB_add_bytes(&body, verify_data, verify_data_len) || |
640 | 3.99k | !ssl_add_message_cbb(ssl, cbb.get())) { |
641 | 0 | return false; |
642 | 0 | } |
643 | | |
644 | 3.99k | return true; |
645 | 3.99k | } |
646 | | |
647 | 6.85k | bool tls13_add_key_update(SSL *ssl, int request_type) { |
648 | 6.85k | if (ssl->s3->key_update_pending) { |
649 | 6.50k | return true; |
650 | 6.50k | } |
651 | | |
652 | | // We do not support multiple parallel outgoing flights. If there is an |
653 | | // outgoing flight pending, queue the KeyUpdate for later. |
654 | 351 | if (SSL_is_dtls(ssl) && !ssl->d1->outgoing_messages.empty()) { |
655 | 114 | ssl->d1->queued_key_update = request_type == SSL_KEY_UPDATE_REQUESTED |
656 | 114 | ? QueuedKeyUpdate::kUpdateRequested |
657 | 114 | : QueuedKeyUpdate::kUpdateNotRequested; |
658 | 114 | return true; |
659 | 114 | } |
660 | | |
661 | 237 | ScopedCBB cbb; |
662 | 237 | CBB body_cbb; |
663 | 237 | if (!ssl->method->init_message(ssl, cbb.get(), &body_cbb, |
664 | 237 | SSL3_MT_KEY_UPDATE) || |
665 | 237 | !CBB_add_u8(&body_cbb, request_type) || |
666 | 237 | !ssl_add_message_cbb(ssl, cbb.get())) { |
667 | 0 | return false; |
668 | 0 | } |
669 | | |
670 | | // In DTLS, the actual key update is deferred until KeyUpdate is ACKed. |
671 | 237 | if (!SSL_is_dtls(ssl) && !tls13_rotate_traffic_key(ssl, evp_aead_seal)) { |
672 | 0 | return false; |
673 | 0 | } |
674 | | |
675 | | // Suppress KeyUpdate acknowledgments until this change is written to the |
676 | | // wire. This prevents us from accumulating write obligations when read and |
677 | | // write progress at different rates. See RFC 8446, section 4.6.3. |
678 | 237 | ssl->s3->key_update_pending = true; |
679 | 237 | ssl->method->finish_flight(ssl); |
680 | 237 | return true; |
681 | 237 | } |
682 | | |
683 | 33.1k | static bool tls13_receive_key_update(SSL *ssl, const SSLMessage &msg) { |
684 | 33.1k | CBS body = msg.body; |
685 | 33.1k | uint8_t key_update_request; |
686 | 33.1k | if (!CBS_get_u8(&body, &key_update_request) || // |
687 | 33.1k | CBS_len(&body) != 0 || // |
688 | 33.1k | (key_update_request != SSL_KEY_UPDATE_NOT_REQUESTED && // |
689 | 6.86k | key_update_request != SSL_KEY_UPDATE_REQUESTED)) { |
690 | 23 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
691 | 23 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
692 | 23 | return false; |
693 | 23 | } |
694 | | |
695 | 33.1k | if (!tls13_rotate_traffic_key(ssl, evp_aead_open)) { |
696 | 9 | return false; |
697 | 9 | } |
698 | | |
699 | | // Acknowledge the KeyUpdate |
700 | 33.1k | if (key_update_request == SSL_KEY_UPDATE_REQUESTED && |
701 | 6.85k | !tls13_add_key_update(ssl, SSL_KEY_UPDATE_NOT_REQUESTED)) { |
702 | 0 | return false; |
703 | 0 | } |
704 | | |
705 | 33.1k | return true; |
706 | 33.1k | } |
707 | | |
708 | 40.1k | bool tls13_post_handshake(SSL *ssl, const SSLMessage &msg) { |
709 | 40.1k | if (msg.type == SSL3_MT_NEW_SESSION_TICKET && !ssl->server) { |
710 | 6.92k | return tls13_process_new_session_ticket(ssl, msg); |
711 | 6.92k | } |
712 | | |
713 | 33.2k | if (msg.type == SSL3_MT_KEY_UPDATE) { |
714 | 33.1k | ssl->s3->key_update_count++; |
715 | 33.1k | if (SSL_is_quic(ssl) || ssl->s3->key_update_count > kMaxKeyUpdates) { |
716 | 1 | OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_KEY_UPDATES); |
717 | 1 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
718 | 1 | return false; |
719 | 1 | } |
720 | | |
721 | 33.1k | return tls13_receive_key_update(ssl, msg); |
722 | 33.1k | } |
723 | | |
724 | 38 | ssl->s3->key_update_count = 0; |
725 | | |
726 | 38 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
727 | 38 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
728 | 38 | return false; |
729 | 33.2k | } |
730 | | |
731 | | BSSL_NAMESPACE_END |