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