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