/src/boringssl/ssl/tls13_both.cc
Line | Count | Source (jump to first uncovered line) |
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 | | |
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 | 422 | enum ssl_cert_verify_context_t cert_verify_context) { |
59 | 422 | ScopedCBB cbb; |
60 | 422 | if (!CBB_init(cbb.get(), 64 + 33 + 1 + 2 * EVP_MAX_MD_SIZE)) { |
61 | 0 | return false; |
62 | 0 | } |
63 | | |
64 | 27.4k | for (size_t i = 0; i < 64; i++) { |
65 | 27.0k | if (!CBB_add_u8(cbb.get(), 0x20)) { |
66 | 0 | return false; |
67 | 0 | } |
68 | 27.0k | } |
69 | | |
70 | 422 | Span<const char> context; |
71 | 422 | if (cert_verify_context == ssl_cert_verify_server) { |
72 | 388 | static const char kContext[] = "TLS 1.3, server CertificateVerify"; |
73 | 388 | context = kContext; |
74 | 388 | } else if (cert_verify_context == ssl_cert_verify_client) { |
75 | 21 | static const char kContext[] = "TLS 1.3, client CertificateVerify"; |
76 | 21 | context = kContext; |
77 | 21 | } else if (cert_verify_context == ssl_cert_verify_channel_id) { |
78 | 13 | static const char kContext[] = "TLS 1.3, Channel ID"; |
79 | 13 | context = kContext; |
80 | 13 | } else { |
81 | 0 | return false; |
82 | 0 | } |
83 | | |
84 | | // Note |context| includes the NUL byte separator. |
85 | 422 | if (!CBB_add_bytes(cbb.get(), |
86 | 422 | reinterpret_cast<const uint8_t *>(context.data()), |
87 | 422 | context.size())) { |
88 | 0 | return false; |
89 | 0 | } |
90 | | |
91 | 422 | uint8_t context_hash[EVP_MAX_MD_SIZE]; |
92 | 422 | size_t context_hash_len; |
93 | 422 | if (!hs->transcript.GetHash(context_hash, &context_hash_len) || |
94 | 422 | !CBB_add_bytes(cbb.get(), context_hash, context_hash_len) || |
95 | 422 | !CBBFinishArray(cbb.get(), out)) { |
96 | 0 | return false; |
97 | 0 | } |
98 | | |
99 | 422 | return true; |
100 | 422 | } |
101 | | |
102 | | bool tls13_process_certificate(SSL_HANDSHAKE *hs, const SSLMessage &msg, |
103 | 98 | bool allow_anonymous) { |
104 | 98 | SSL *const ssl = hs->ssl; |
105 | 98 | CBS body = msg.body; |
106 | 98 | bssl::UniquePtr<CRYPTO_BUFFER> decompressed; |
107 | | |
108 | 98 | 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 | 98 | } else { |
168 | 98 | assert(msg.type == SSL3_MT_CERTIFICATE); |
169 | 98 | } |
170 | | |
171 | 98 | CBS context, certificate_list; |
172 | 98 | if (!CBS_get_u8_length_prefixed(&body, &context) || |
173 | 98 | CBS_len(&context) != 0 || |
174 | 98 | !CBS_get_u24_length_prefixed(&body, &certificate_list) || |
175 | 98 | CBS_len(&body) != 0) { |
176 | 10 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
177 | 10 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
178 | 10 | return false; |
179 | 10 | } |
180 | | |
181 | 88 | UniquePtr<STACK_OF(CRYPTO_BUFFER)> certs(sk_CRYPTO_BUFFER_new_null()); |
182 | 88 | if (!certs) { |
183 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
184 | 0 | return false; |
185 | 0 | } |
186 | | |
187 | 88 | const bool retain_sha256 = |
188 | 88 | ssl->server && hs->config->retain_only_sha256_of_client_certs; |
189 | 88 | UniquePtr<EVP_PKEY> pkey; |
190 | 300 | while (CBS_len(&certificate_list) > 0) { |
191 | 255 | CBS certificate, extensions; |
192 | 255 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) || |
193 | 255 | !CBS_get_u16_length_prefixed(&certificate_list, &extensions) || |
194 | 255 | CBS_len(&certificate) == 0) { |
195 | 7 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
196 | 7 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
197 | 7 | return false; |
198 | 7 | } |
199 | | |
200 | 248 | if (sk_CRYPTO_BUFFER_num(certs.get()) == 0) { |
201 | 84 | pkey = ssl_cert_parse_pubkey(&certificate); |
202 | 84 | if (!pkey) { |
203 | 3 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
204 | 3 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
205 | 3 | return false; |
206 | 3 | } |
207 | | // TLS 1.3 always uses certificate keys for signing thus the correct |
208 | | // keyUsage is enforced. |
209 | 81 | if (!ssl_cert_check_key_usage(&certificate, |
210 | 81 | key_usage_digital_signature)) { |
211 | 15 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
212 | 15 | return false; |
213 | 15 | } |
214 | | |
215 | 66 | if (retain_sha256) { |
216 | | // Retain the hash of the leaf certificate if requested. |
217 | 0 | SHA256(CBS_data(&certificate), CBS_len(&certificate), |
218 | 0 | hs->new_session->peer_sha256); |
219 | 0 | } |
220 | 66 | } |
221 | | |
222 | 230 | UniquePtr<CRYPTO_BUFFER> buf( |
223 | 230 | CRYPTO_BUFFER_new_from_CBS(&certificate, ssl->ctx->pool)); |
224 | 230 | if (!buf || |
225 | 230 | !PushToStack(certs.get(), std::move(buf))) { |
226 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
227 | 0 | return false; |
228 | 0 | } |
229 | | |
230 | | // Parse out the extensions. |
231 | 230 | SSLExtension status_request( |
232 | 230 | TLSEXT_TYPE_status_request, |
233 | 230 | !ssl->server && hs->config->ocsp_stapling_enabled); |
234 | 230 | SSLExtension sct( |
235 | 230 | TLSEXT_TYPE_certificate_timestamp, |
236 | 230 | !ssl->server && hs->config->signed_cert_timestamps_enabled); |
237 | 230 | uint8_t alert = SSL_AD_DECODE_ERROR; |
238 | 230 | if (!ssl_parse_extensions(&extensions, &alert, {&status_request, &sct}, |
239 | 230 | /*ignore_unknown=*/false)) { |
240 | 18 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
241 | 18 | return false; |
242 | 18 | } |
243 | | |
244 | | // All Certificate extensions are parsed, but only the leaf extensions are |
245 | | // stored. |
246 | 212 | if (status_request.present) { |
247 | 0 | uint8_t status_type; |
248 | 0 | CBS ocsp_response; |
249 | 0 | if (!CBS_get_u8(&status_request.data, &status_type) || |
250 | 0 | status_type != TLSEXT_STATUSTYPE_ocsp || |
251 | 0 | !CBS_get_u24_length_prefixed(&status_request.data, &ocsp_response) || |
252 | 0 | CBS_len(&ocsp_response) == 0 || |
253 | 0 | CBS_len(&status_request.data) != 0) { |
254 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
255 | 0 | return false; |
256 | 0 | } |
257 | | |
258 | 0 | if (sk_CRYPTO_BUFFER_num(certs.get()) == 1) { |
259 | 0 | hs->new_session->ocsp_response.reset( |
260 | 0 | CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool)); |
261 | 0 | if (hs->new_session->ocsp_response == nullptr) { |
262 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
263 | 0 | return false; |
264 | 0 | } |
265 | 0 | } |
266 | 0 | } |
267 | | |
268 | 212 | if (sct.present) { |
269 | 0 | if (!ssl_is_sct_list_valid(&sct.data)) { |
270 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
271 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
272 | 0 | return false; |
273 | 0 | } |
274 | | |
275 | 0 | if (sk_CRYPTO_BUFFER_num(certs.get()) == 1) { |
276 | 0 | hs->new_session->signed_cert_timestamp_list.reset( |
277 | 0 | CRYPTO_BUFFER_new_from_CBS(&sct.data, ssl->ctx->pool)); |
278 | 0 | if (hs->new_session->signed_cert_timestamp_list == nullptr) { |
279 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
280 | 0 | return false; |
281 | 0 | } |
282 | 0 | } |
283 | 0 | } |
284 | 212 | } |
285 | | |
286 | | // Store a null certificate list rather than an empty one if the peer didn't |
287 | | // send certificates. |
288 | 45 | if (sk_CRYPTO_BUFFER_num(certs.get()) == 0) { |
289 | 1 | certs.reset(); |
290 | 1 | } |
291 | | |
292 | 45 | hs->peer_pubkey = std::move(pkey); |
293 | 45 | hs->new_session->certs = std::move(certs); |
294 | | |
295 | 45 | if (!ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) { |
296 | 6 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
297 | 6 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
298 | 6 | return false; |
299 | 6 | } |
300 | | |
301 | 39 | if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) { |
302 | 1 | if (!allow_anonymous) { |
303 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
304 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_CERTIFICATE_REQUIRED); |
305 | 0 | return false; |
306 | 0 | } |
307 | | |
308 | | // OpenSSL returns X509_V_OK when no certificates are requested. This is |
309 | | // classed by them as a bug, but it's assumed by at least NGINX. |
310 | 1 | hs->new_session->verify_result = X509_V_OK; |
311 | | |
312 | | // No certificate, so nothing more to do. |
313 | 1 | return true; |
314 | 1 | } |
315 | | |
316 | 38 | hs->new_session->peer_sha256_valid = retain_sha256; |
317 | 38 | return true; |
318 | 39 | } |
319 | | |
320 | 28 | bool tls13_process_certificate_verify(SSL_HANDSHAKE *hs, const SSLMessage &msg) { |
321 | 28 | SSL *const ssl = hs->ssl; |
322 | 28 | if (hs->peer_pubkey == NULL) { |
323 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
324 | 0 | return false; |
325 | 0 | } |
326 | | |
327 | 28 | CBS body = msg.body, signature; |
328 | 28 | uint16_t signature_algorithm; |
329 | 28 | if (!CBS_get_u16(&body, &signature_algorithm) || |
330 | 28 | !CBS_get_u16_length_prefixed(&body, &signature) || |
331 | 28 | CBS_len(&body) != 0) { |
332 | 6 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
333 | 6 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
334 | 6 | return false; |
335 | 6 | } |
336 | | |
337 | 22 | uint8_t alert = SSL_AD_DECODE_ERROR; |
338 | 22 | if (!tls12_check_peer_sigalg(hs, &alert, signature_algorithm)) { |
339 | 1 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
340 | 1 | return false; |
341 | 1 | } |
342 | 21 | hs->new_session->peer_signature_algorithm = signature_algorithm; |
343 | | |
344 | 21 | Array<uint8_t> input; |
345 | 21 | if (!tls13_get_cert_verify_signature_input( |
346 | 21 | hs, &input, |
347 | 21 | ssl->server ? ssl_cert_verify_client : ssl_cert_verify_server)) { |
348 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
349 | 0 | return false; |
350 | 0 | } |
351 | | |
352 | 21 | if (!ssl_public_key_verify(ssl, signature, signature_algorithm, |
353 | 21 | hs->peer_pubkey.get(), input)) { |
354 | 2 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
355 | 2 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
356 | 2 | return false; |
357 | 2 | } |
358 | | |
359 | 19 | return true; |
360 | 21 | } |
361 | | |
362 | | bool tls13_process_finished(SSL_HANDSHAKE *hs, const SSLMessage &msg, |
363 | 215 | bool use_saved_value) { |
364 | 215 | SSL *const ssl = hs->ssl; |
365 | 215 | uint8_t verify_data_buf[EVP_MAX_MD_SIZE]; |
366 | 215 | Span<const uint8_t> verify_data; |
367 | 215 | if (use_saved_value) { |
368 | 1 | assert(ssl->server); |
369 | 0 | verify_data = hs->expected_client_finished(); |
370 | 214 | } else { |
371 | 214 | size_t len; |
372 | 214 | if (!tls13_finished_mac(hs, verify_data_buf, &len, !ssl->server)) { |
373 | 0 | return false; |
374 | 0 | } |
375 | 214 | verify_data = MakeConstSpan(verify_data_buf, len); |
376 | 214 | } |
377 | | |
378 | 215 | bool finished_ok = |
379 | 215 | CBS_mem_equal(&msg.body, verify_data.data(), verify_data.size()); |
380 | 215 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
381 | 215 | finished_ok = true; |
382 | 215 | #endif |
383 | 215 | if (!finished_ok) { |
384 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
385 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED); |
386 | 0 | return false; |
387 | 0 | } |
388 | | |
389 | 215 | return true; |
390 | 215 | } |
391 | | |
392 | 409 | bool tls13_add_certificate(SSL_HANDSHAKE *hs) { |
393 | 409 | SSL *const ssl = hs->ssl; |
394 | 409 | CERT *const cert = hs->config->cert.get(); |
395 | 409 | DC *const dc = cert->dc.get(); |
396 | | |
397 | 409 | ScopedCBB cbb; |
398 | 409 | CBB *body, body_storage, certificate_list; |
399 | | |
400 | 409 | if (hs->cert_compression_negotiated) { |
401 | 0 | if (!CBB_init(cbb.get(), 1024)) { |
402 | 0 | return false; |
403 | 0 | } |
404 | 0 | body = cbb.get(); |
405 | 409 | } else { |
406 | 409 | body = &body_storage; |
407 | 409 | if (!ssl->method->init_message(ssl, cbb.get(), body, SSL3_MT_CERTIFICATE)) { |
408 | 0 | return false; |
409 | 0 | } |
410 | 409 | } |
411 | | |
412 | 409 | if (// The request context is always empty in the handshake. |
413 | 409 | !CBB_add_u8(body, 0) || |
414 | 409 | !CBB_add_u24_length_prefixed(body, &certificate_list)) { |
415 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
416 | 0 | return false; |
417 | 0 | } |
418 | | |
419 | 409 | if (!ssl_has_certificate(hs)) { |
420 | 0 | return ssl_add_message_cbb(ssl, cbb.get()); |
421 | 0 | } |
422 | | |
423 | 409 | CRYPTO_BUFFER *leaf_buf = sk_CRYPTO_BUFFER_value(cert->chain.get(), 0); |
424 | 409 | CBB leaf, extensions; |
425 | 409 | if (!CBB_add_u24_length_prefixed(&certificate_list, &leaf) || |
426 | 409 | !CBB_add_bytes(&leaf, CRYPTO_BUFFER_data(leaf_buf), |
427 | 409 | CRYPTO_BUFFER_len(leaf_buf)) || |
428 | 409 | !CBB_add_u16_length_prefixed(&certificate_list, &extensions)) { |
429 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
430 | 0 | return false; |
431 | 0 | } |
432 | | |
433 | 409 | if (hs->scts_requested && cert->signed_cert_timestamp_list != nullptr) { |
434 | 95 | CBB contents; |
435 | 95 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_certificate_timestamp) || |
436 | 95 | !CBB_add_u16_length_prefixed(&extensions, &contents) || |
437 | 95 | !CBB_add_bytes( |
438 | 95 | &contents, |
439 | 95 | CRYPTO_BUFFER_data(cert->signed_cert_timestamp_list.get()), |
440 | 95 | CRYPTO_BUFFER_len(cert->signed_cert_timestamp_list.get())) || |
441 | 95 | !CBB_flush(&extensions)) { |
442 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
443 | 0 | return false; |
444 | 0 | } |
445 | 95 | } |
446 | | |
447 | 409 | if (hs->ocsp_stapling_requested && cert->ocsp_response != NULL) { |
448 | 102 | CBB contents, ocsp_response; |
449 | 102 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_status_request) || |
450 | 102 | !CBB_add_u16_length_prefixed(&extensions, &contents) || |
451 | 102 | !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) || |
452 | 102 | !CBB_add_u24_length_prefixed(&contents, &ocsp_response) || |
453 | 102 | !CBB_add_bytes(&ocsp_response, |
454 | 102 | CRYPTO_BUFFER_data(cert->ocsp_response.get()), |
455 | 102 | CRYPTO_BUFFER_len(cert->ocsp_response.get())) || |
456 | 102 | !CBB_flush(&extensions)) { |
457 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
458 | 0 | return false; |
459 | 0 | } |
460 | 102 | } |
461 | | |
462 | 409 | if (ssl_signing_with_dc(hs)) { |
463 | 0 | const CRYPTO_BUFFER *raw = dc->raw.get(); |
464 | 0 | CBB child; |
465 | 0 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_delegated_credential) || |
466 | 0 | !CBB_add_u16_length_prefixed(&extensions, &child) || |
467 | 0 | !CBB_add_bytes(&child, CRYPTO_BUFFER_data(raw), |
468 | 0 | CRYPTO_BUFFER_len(raw)) || |
469 | 0 | !CBB_flush(&extensions)) { |
470 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
471 | 0 | return false; |
472 | 0 | } |
473 | 0 | ssl->s3->delegated_credential_used = true; |
474 | 0 | } |
475 | | |
476 | 409 | for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cert->chain.get()); i++) { |
477 | 0 | CRYPTO_BUFFER *cert_buf = sk_CRYPTO_BUFFER_value(cert->chain.get(), i); |
478 | 0 | CBB child; |
479 | 0 | if (!CBB_add_u24_length_prefixed(&certificate_list, &child) || |
480 | 0 | !CBB_add_bytes(&child, CRYPTO_BUFFER_data(cert_buf), |
481 | 0 | CRYPTO_BUFFER_len(cert_buf)) || |
482 | 0 | !CBB_add_u16(&certificate_list, 0 /* no extensions */)) { |
483 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
484 | 0 | return false; |
485 | 0 | } |
486 | 0 | } |
487 | | |
488 | 409 | if (!hs->cert_compression_negotiated) { |
489 | 409 | return ssl_add_message_cbb(ssl, cbb.get()); |
490 | 409 | } |
491 | | |
492 | 0 | Array<uint8_t> msg; |
493 | 0 | if (!CBBFinishArray(cbb.get(), &msg)) { |
494 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
495 | 0 | return false; |
496 | 0 | } |
497 | | |
498 | 0 | const CertCompressionAlg *alg = nullptr; |
499 | 0 | for (const auto &candidate : ssl->ctx->cert_compression_algs) { |
500 | 0 | if (candidate.alg_id == hs->cert_compression_alg_id) { |
501 | 0 | alg = &candidate; |
502 | 0 | break; |
503 | 0 | } |
504 | 0 | } |
505 | |
|
506 | 0 | if (alg == nullptr || alg->compress == nullptr) { |
507 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
508 | 0 | return false; |
509 | 0 | } |
510 | | |
511 | 0 | CBB compressed; |
512 | 0 | body = &body_storage; |
513 | 0 | if (!ssl->method->init_message(ssl, cbb.get(), body, |
514 | 0 | SSL3_MT_COMPRESSED_CERTIFICATE) || |
515 | 0 | !CBB_add_u16(body, hs->cert_compression_alg_id) || |
516 | 0 | msg.size() > (1u << 24) - 1 || // |
517 | 0 | !CBB_add_u24(body, static_cast<uint32_t>(msg.size())) || |
518 | 0 | !CBB_add_u24_length_prefixed(body, &compressed)) { |
519 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
520 | 0 | return false; |
521 | 0 | } |
522 | | |
523 | 0 | SSL_HANDSHAKE_HINTS *const hints = hs->hints.get(); |
524 | 0 | if (hints && !hs->hints_requested && |
525 | 0 | hints->cert_compression_alg_id == hs->cert_compression_alg_id && |
526 | 0 | hints->cert_compression_input == MakeConstSpan(msg) && |
527 | 0 | !hints->cert_compression_output.empty()) { |
528 | 0 | if (!CBB_add_bytes(&compressed, hints->cert_compression_output.data(), |
529 | 0 | hints->cert_compression_output.size())) { |
530 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
531 | 0 | return false; |
532 | 0 | } |
533 | 0 | } else { |
534 | 0 | if (!alg->compress(ssl, &compressed, msg.data(), msg.size())) { |
535 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
536 | 0 | return false; |
537 | 0 | } |
538 | 0 | if (hints && hs->hints_requested) { |
539 | 0 | hints->cert_compression_alg_id = hs->cert_compression_alg_id; |
540 | 0 | if (!hints->cert_compression_input.CopyFrom(msg) || |
541 | 0 | !hints->cert_compression_output.CopyFrom( |
542 | 0 | MakeConstSpan(CBB_data(&compressed), CBB_len(&compressed)))) { |
543 | 0 | return false; |
544 | 0 | } |
545 | 0 | } |
546 | 0 | } |
547 | | |
548 | 0 | if (!ssl_add_message_cbb(ssl, cbb.get())) { |
549 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
550 | 0 | return false; |
551 | 0 | } |
552 | | |
553 | 0 | return true; |
554 | 0 | } |
555 | | |
556 | 409 | enum ssl_private_key_result_t tls13_add_certificate_verify(SSL_HANDSHAKE *hs) { |
557 | 409 | SSL *const ssl = hs->ssl; |
558 | 409 | uint16_t signature_algorithm; |
559 | 409 | if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) { |
560 | 21 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
561 | 21 | return ssl_private_key_failure; |
562 | 21 | } |
563 | | |
564 | 388 | ScopedCBB cbb; |
565 | 388 | CBB body; |
566 | 388 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
567 | 388 | SSL3_MT_CERTIFICATE_VERIFY) || |
568 | 388 | !CBB_add_u16(&body, signature_algorithm)) { |
569 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
570 | 0 | return ssl_private_key_failure; |
571 | 0 | } |
572 | | |
573 | 388 | CBB child; |
574 | 388 | const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get()); |
575 | 388 | uint8_t *sig; |
576 | 388 | size_t sig_len; |
577 | 388 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
578 | 388 | !CBB_reserve(&child, &sig, max_sig_len)) { |
579 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
580 | 0 | return ssl_private_key_failure; |
581 | 0 | } |
582 | | |
583 | 388 | Array<uint8_t> msg; |
584 | 388 | if (!tls13_get_cert_verify_signature_input( |
585 | 388 | hs, &msg, |
586 | 388 | ssl->server ? ssl_cert_verify_server : ssl_cert_verify_client)) { |
587 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
588 | 0 | return ssl_private_key_failure; |
589 | 0 | } |
590 | | |
591 | 388 | enum ssl_private_key_result_t sign_result = ssl_private_key_sign( |
592 | 388 | hs, sig, &sig_len, max_sig_len, signature_algorithm, msg); |
593 | 388 | if (sign_result != ssl_private_key_success) { |
594 | 0 | return sign_result; |
595 | 0 | } |
596 | | |
597 | 388 | if (!CBB_did_write(&child, sig_len) || |
598 | 388 | !ssl_add_message_cbb(ssl, cbb.get())) { |
599 | 0 | return ssl_private_key_failure; |
600 | 0 | } |
601 | | |
602 | 388 | return ssl_private_key_success; |
603 | 388 | } |
604 | | |
605 | 472 | bool tls13_add_finished(SSL_HANDSHAKE *hs) { |
606 | 472 | SSL *const ssl = hs->ssl; |
607 | 472 | size_t verify_data_len; |
608 | 472 | uint8_t verify_data[EVP_MAX_MD_SIZE]; |
609 | | |
610 | 472 | if (!tls13_finished_mac(hs, verify_data, &verify_data_len, ssl->server)) { |
611 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
612 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED); |
613 | 0 | return false; |
614 | 0 | } |
615 | | |
616 | 472 | ScopedCBB cbb; |
617 | 472 | CBB body; |
618 | 472 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_FINISHED) || |
619 | 472 | !CBB_add_bytes(&body, verify_data, verify_data_len) || |
620 | 472 | !ssl_add_message_cbb(ssl, cbb.get())) { |
621 | 0 | return false; |
622 | 0 | } |
623 | | |
624 | 472 | return true; |
625 | 472 | } |
626 | | |
627 | 123 | bool tls13_add_key_update(SSL *ssl, int update_requested) { |
628 | 123 | ScopedCBB cbb; |
629 | 123 | CBB body_cbb; |
630 | 123 | if (!ssl->method->init_message(ssl, cbb.get(), &body_cbb, |
631 | 123 | SSL3_MT_KEY_UPDATE) || |
632 | 123 | !CBB_add_u8(&body_cbb, update_requested) || |
633 | 123 | !ssl_add_message_cbb(ssl, cbb.get()) || |
634 | 123 | !tls13_rotate_traffic_key(ssl, evp_aead_seal)) { |
635 | 0 | return false; |
636 | 0 | } |
637 | | |
638 | | // Suppress KeyUpdate acknowledgments until this change is written to the |
639 | | // wire. This prevents us from accumulating write obligations when read and |
640 | | // write progress at different rates. See RFC 8446, section 4.6.3. |
641 | 123 | ssl->s3->key_update_pending = true; |
642 | | |
643 | 123 | return true; |
644 | 123 | } |
645 | | |
646 | 6.10k | static bool tls13_receive_key_update(SSL *ssl, const SSLMessage &msg) { |
647 | 6.10k | CBS body = msg.body; |
648 | 6.10k | uint8_t key_update_request; |
649 | 6.10k | if (!CBS_get_u8(&body, &key_update_request) || |
650 | 6.10k | CBS_len(&body) != 0 || |
651 | 6.10k | (key_update_request != SSL_KEY_UPDATE_NOT_REQUESTED && |
652 | 6.10k | key_update_request != SSL_KEY_UPDATE_REQUESTED)) { |
653 | 6 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
654 | 6 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
655 | 6 | return false; |
656 | 6 | } |
657 | | |
658 | 6.10k | if (!tls13_rotate_traffic_key(ssl, evp_aead_open)) { |
659 | 8 | return false; |
660 | 8 | } |
661 | | |
662 | | // Acknowledge the KeyUpdate |
663 | 6.09k | if (key_update_request == SSL_KEY_UPDATE_REQUESTED && |
664 | 6.09k | !ssl->s3->key_update_pending && |
665 | 6.09k | !tls13_add_key_update(ssl, SSL_KEY_UPDATE_NOT_REQUESTED)) { |
666 | 0 | return false; |
667 | 0 | } |
668 | | |
669 | 6.09k | return true; |
670 | 6.09k | } |
671 | | |
672 | 6.11k | bool tls13_post_handshake(SSL *ssl, const SSLMessage &msg) { |
673 | 6.11k | if (msg.type == SSL3_MT_KEY_UPDATE) { |
674 | 6.10k | ssl->s3->key_update_count++; |
675 | 6.10k | if (ssl->quic_method != nullptr || |
676 | 6.10k | ssl->s3->key_update_count > kMaxKeyUpdates) { |
677 | 1 | OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_KEY_UPDATES); |
678 | 1 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
679 | 1 | return false; |
680 | 1 | } |
681 | | |
682 | 6.10k | return tls13_receive_key_update(ssl, msg); |
683 | 6.10k | } |
684 | | |
685 | 3 | ssl->s3->key_update_count = 0; |
686 | | |
687 | 3 | if (msg.type == SSL3_MT_NEW_SESSION_TICKET && !ssl->server) { |
688 | 0 | return tls13_process_new_session_ticket(ssl, msg); |
689 | 0 | } |
690 | | |
691 | 3 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
692 | 3 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
693 | 3 | return false; |
694 | 3 | } |
695 | | |
696 | | BSSL_NAMESPACE_END |