/src/openssl41/ssl/quic/quic_tls.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2022-2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | #include <openssl/ssl.h> |
10 | | #include "internal/recordmethod.h" |
11 | | #include "internal/quic_tls.h" |
12 | | #include "../ssl_local.h" |
13 | | #include "internal/quic_record_util.h" |
14 | | #include "internal/quic_error.h" |
15 | | #include "internal/quic_types.h" |
16 | | #include "internal/ssl_unwrap.h" |
17 | | |
18 | | #define QUIC_TLS_FATAL(rl, ad, err) \ |
19 | 44 | do { \ |
20 | 44 | if ((rl) != NULL) \ |
21 | 44 | (rl)->alert = (ad); \ |
22 | 44 | ERR_raise(ERR_LIB_SSL, (err)); \ |
23 | 44 | if ((rl) != NULL) \ |
24 | 44 | (rl)->qtls->inerror = 1; \ |
25 | 44 | } while (0) |
26 | | |
27 | | struct quic_tls_st { |
28 | | QUIC_TLS_ARGS args; |
29 | | |
30 | | /* |
31 | | * Transport parameters which client should send. Buffer lifetime must |
32 | | * exceed the lifetime of the QUIC_TLS object. |
33 | | */ |
34 | | const unsigned char *local_transport_params; |
35 | | size_t local_transport_params_len; |
36 | | |
37 | | ERR_STATE *error_state; |
38 | | |
39 | | /* |
40 | | * QUIC error code (usually in the TLS Alert-mapped CRYPTO_ERR range). Valid |
41 | | * only if inerror is 1. |
42 | | */ |
43 | | uint64_t error_code; |
44 | | |
45 | | /* |
46 | | * Error message with static storage duration. Valid only if inerror is 1. |
47 | | * Should be suitable for encapsulation in a CONNECTION_CLOSE frame. |
48 | | */ |
49 | | const char *error_msg; |
50 | | |
51 | | /* Whether our SSL object for TLS has been configured for use in QUIC */ |
52 | | unsigned int configured : 1; |
53 | | |
54 | | /* Set if we have hit any error state */ |
55 | | unsigned int inerror : 1; |
56 | | |
57 | | /* Set if the handshake has completed */ |
58 | | unsigned int complete : 1; |
59 | | |
60 | | /* Set if we have consumed the local transport parameters yet. */ |
61 | | unsigned int local_transport_params_consumed : 1; |
62 | | }; |
63 | | |
64 | | struct ossl_record_layer_st { |
65 | | QUIC_TLS *qtls; |
66 | | |
67 | | /* Protection level */ |
68 | | int level; |
69 | | |
70 | | /* Only used for retry flags */ |
71 | | BIO *dummybio; |
72 | | |
73 | | /* Number of bytes written so far if we are part way through a write */ |
74 | | size_t written; |
75 | | |
76 | | /* If we are part way through a write, a copy of the template */ |
77 | | OSSL_RECORD_TEMPLATE template; |
78 | | |
79 | | /* |
80 | | * If we hit an error, what alert code should be used |
81 | | */ |
82 | | int alert; |
83 | | |
84 | | /* Amount of crypto stream data we read in the last call to quic_read_record */ |
85 | | size_t recread; |
86 | | |
87 | | /* Amount of crypto stream data read but not yet released */ |
88 | | size_t recunreleased; |
89 | | |
90 | | /* Callbacks */ |
91 | | OSSL_FUNC_rlayer_msg_callback_fn *msg_callback; |
92 | | void *cbarg; |
93 | | }; |
94 | | |
95 | | static int quic_set1_bio(OSSL_RECORD_LAYER *rl, BIO *bio); |
96 | | static int quic_free(OSSL_RECORD_LAYER *r); |
97 | | |
98 | | static int |
99 | | quic_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers, |
100 | | int role, int direction, int level, uint64_t epoch, |
101 | | unsigned char *secret, size_t secretlen, |
102 | | unsigned char *snkey, unsigned char *key, size_t keylen, |
103 | | unsigned char *iv, size_t ivlen, |
104 | | unsigned char *mackey, size_t mackeylen, |
105 | | const EVP_CIPHER *snciph, |
106 | | const EVP_CIPHER *ciph, size_t taglen, |
107 | | int mactype, |
108 | | const EVP_MD *md, COMP_METHOD *comp, |
109 | | const EVP_MD *kdfdigest, BIO *prev, BIO *transport, |
110 | | BIO *next, |
111 | | int use_urxe, |
112 | | const OSSL_PARAM *settings, const OSSL_PARAM *options, |
113 | | const OSSL_DISPATCH *fns, void *cbarg, void *rlarg, |
114 | | OSSL_RECORD_LAYER **retrl) |
115 | 180k | { |
116 | 180k | OSSL_RECORD_LAYER *rl = OPENSSL_zalloc(sizeof(*rl)); |
117 | 180k | int qdir; |
118 | 180k | uint32_t suite_id = 0; |
119 | | |
120 | 180k | if (rl == NULL) { |
121 | 0 | QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
122 | 0 | return 0; |
123 | 0 | } |
124 | | |
125 | 180k | rl->qtls = (QUIC_TLS *)rlarg; |
126 | 180k | rl->level = level; |
127 | 180k | if (!quic_set1_bio(rl, transport)) { |
128 | 0 | QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
129 | 0 | goto err; |
130 | 0 | } |
131 | 180k | rl->cbarg = cbarg; |
132 | 180k | *retrl = rl; |
133 | | |
134 | 180k | if (fns != NULL) { |
135 | 681k | for (; fns->function_id != 0; fns++) { |
136 | 501k | switch (fns->function_id) { |
137 | 0 | break; |
138 | 0 | case OSSL_FUNC_RLAYER_MSG_CALLBACK: |
139 | 0 | rl->msg_callback = OSSL_FUNC_rlayer_msg_callback(fns); |
140 | 0 | break; |
141 | 501k | default: |
142 | | /* Just ignore anything we don't understand */ |
143 | 501k | break; |
144 | 501k | } |
145 | 501k | } |
146 | 180k | } |
147 | | |
148 | 180k | if (level == OSSL_RECORD_PROTECTION_LEVEL_NONE) |
149 | 131k | return 1; |
150 | | |
151 | 48.6k | if (direction == OSSL_RECORD_DIRECTION_READ) |
152 | 24.3k | qdir = 0; |
153 | 24.3k | else |
154 | 24.3k | qdir = 1; |
155 | | |
156 | 48.6k | if (rl->qtls->args.ossl_quic) { |
157 | 48.6k | #ifndef OPENSSL_NO_QUIC |
158 | | /* |
159 | | * We only look up the suite_id/MD for internal callers. Not used in the |
160 | | * public API. We assume that a 3rd party QUIC stack will want to |
161 | | * figure this out by itself (e.g. so that they could add new |
162 | | * ciphersuites at a different pace to us) |
163 | | */ |
164 | 48.6k | if (EVP_CIPHER_is_a(ciph, "AES-128-GCM")) { |
165 | 1.45k | suite_id = QRL_SUITE_AES128GCM; |
166 | 47.1k | } else if (EVP_CIPHER_is_a(ciph, "AES-256-GCM")) { |
167 | 44.1k | suite_id = QRL_SUITE_AES256GCM; |
168 | 44.1k | } else if (EVP_CIPHER_is_a(ciph, "CHACHA20-POLY1305")) { |
169 | 2.98k | suite_id = QRL_SUITE_CHACHA20POLY1305; |
170 | 2.98k | } else { |
171 | 0 | QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_CIPHER_TYPE); |
172 | 0 | goto err; |
173 | 0 | } |
174 | | |
175 | | /* We pass a ref to the md in a successful yield_secret_cb call */ |
176 | | /* TODO(QUIC FUTURE): This cast is horrible. We should try and remove it */ |
177 | 48.6k | if (!EVP_MD_up_ref((EVP_MD *)kdfdigest)) { |
178 | 0 | QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
179 | 0 | goto err; |
180 | 0 | } |
181 | | #else |
182 | | if (!ossl_assert("Should not happen" == NULL)) |
183 | | goto err; |
184 | | #endif |
185 | 48.6k | } else { |
186 | 0 | kdfdigest = NULL; |
187 | 0 | } |
188 | | |
189 | 48.6k | if (!rl->qtls->args.yield_secret_cb(level, qdir, suite_id, |
190 | 48.6k | (EVP_MD *)kdfdigest, secret, secretlen, |
191 | 48.6k | rl->qtls->args.yield_secret_cb_arg)) { |
192 | 30 | QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
193 | 30 | EVP_MD_free((EVP_MD *)kdfdigest); |
194 | 30 | goto err; |
195 | 30 | } |
196 | | |
197 | 48.5k | return 1; |
198 | 30 | err: |
199 | 30 | *retrl = NULL; |
200 | 30 | quic_free(rl); |
201 | 30 | return 0; |
202 | 48.6k | } |
203 | | |
204 | | static int quic_free(OSSL_RECORD_LAYER *rl) |
205 | 228k | { |
206 | 228k | if (rl == NULL) |
207 | 0 | return 1; |
208 | | |
209 | 228k | BIO_free(rl->dummybio); |
210 | 228k | OPENSSL_free(rl); |
211 | 228k | return 1; |
212 | 228k | } |
213 | | |
214 | | static int quic_unprocessed_read_pending(OSSL_RECORD_LAYER *rl) |
215 | 0 | { |
216 | | /* |
217 | | * Read ahead isn't really a thing for QUIC so we never have unprocessed |
218 | | * data pending |
219 | | */ |
220 | 0 | return 0; |
221 | 0 | } |
222 | | |
223 | | static int quic_processed_read_pending(OSSL_RECORD_LAYER *rl) |
224 | 88.9k | { |
225 | | /* |
226 | | * This is currently only ever used by: |
227 | | * - SSL_has_pending() |
228 | | * - to check whether we have more records that we want to supply to the |
229 | | * upper layers |
230 | | * |
231 | | * We only ever supply 1 record at a time to the upper layers, and |
232 | | * SSL_has_pending() will go via the QUIC method not the TLS method so that |
233 | | * use case doesn't apply here. |
234 | | * Therefore we can ignore this for now and always return 0. We might |
235 | | * eventually want to change this to check in the receive buffers to see if |
236 | | * we have any more data pending. |
237 | | */ |
238 | 88.9k | return 0; |
239 | 88.9k | } |
240 | | |
241 | | static size_t quic_get_max_records(OSSL_RECORD_LAYER *rl, uint8_t type, |
242 | | size_t len, |
243 | | size_t maxfrag, size_t *preffrag) |
244 | 51.7k | { |
245 | 51.7k | return 1; |
246 | 51.7k | } |
247 | | |
248 | | static int quic_write_records(OSSL_RECORD_LAYER *rl, |
249 | | OSSL_RECORD_TEMPLATE *template, |
250 | | size_t numtempl) |
251 | 61.0k | { |
252 | 61.0k | size_t consumed; |
253 | 61.0k | unsigned char alert; |
254 | | |
255 | 61.0k | if (!ossl_assert(numtempl == 1)) { |
256 | | /* How could this be? quic_get_max_records() always returns 1 */ |
257 | 0 | QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
258 | 0 | return OSSL_RECORD_RETURN_FATAL; |
259 | 0 | } |
260 | | |
261 | 61.0k | BIO_clear_retry_flags(rl->dummybio); |
262 | | |
263 | 61.0k | if (rl->msg_callback != NULL) { |
264 | 0 | unsigned char dummyrec[SSL3_RT_HEADER_LENGTH]; |
265 | | |
266 | | /* |
267 | | * For the purposes of the callback we "pretend" to be normal TLS, |
268 | | * and manufacture a dummy record header |
269 | | */ |
270 | 0 | dummyrec[0] = (rl->level == OSSL_RECORD_PROTECTION_LEVEL_NONE) |
271 | 0 | ? template->type |
272 | 0 | : SSL3_RT_APPLICATION_DATA; |
273 | 0 | dummyrec[1] = (unsigned char)((template->version >> 8) & 0xff); |
274 | 0 | dummyrec[2] = (unsigned char)(template->version & 0xff); |
275 | | /* |
276 | | * We assume that buflen is always <= UINT16_MAX. Since this is |
277 | | * generated by libssl itself we actually expect it to never |
278 | | * exceed SSL3_RT_MAX_PLAIN_LENGTH - so it should be a safe |
279 | | * assumption |
280 | | */ |
281 | 0 | dummyrec[3] = (unsigned char)((template->buflen >> 8) & 0xff); |
282 | 0 | dummyrec[4] = (unsigned char)(template->buflen & 0xff); |
283 | |
|
284 | 0 | rl->msg_callback(1, TLS1_3_VERSION, SSL3_RT_HEADER, dummyrec, |
285 | 0 | SSL3_RT_HEADER_LENGTH, rl->cbarg); |
286 | |
|
287 | 0 | if (rl->level != OSSL_RECORD_PROTECTION_LEVEL_NONE) { |
288 | 0 | rl->msg_callback(1, TLS1_3_VERSION, SSL3_RT_INNER_CONTENT_TYPE, |
289 | 0 | &template->type, 1, rl->cbarg); |
290 | 0 | } |
291 | 0 | } |
292 | | |
293 | 61.0k | switch (template->type) { |
294 | 9.30k | case SSL3_RT_ALERT: |
295 | 9.30k | if (template->buflen != 2) { |
296 | | /* |
297 | | * We assume that libssl always sends both bytes of an alert to |
298 | | * us in one go, and never fragments it. If we ever get more |
299 | | * or less bytes than exactly 2 then this is very unexpected. |
300 | | */ |
301 | 0 | QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_VALUE); |
302 | 0 | return OSSL_RECORD_RETURN_FATAL; |
303 | 0 | } |
304 | | /* |
305 | | * Byte 0 is the alert level (we ignore it) and byte 1 is the alert |
306 | | * description that we are actually interested in. |
307 | | */ |
308 | 9.30k | alert = template->buf[1]; |
309 | | |
310 | 9.30k | if (!rl->qtls->args.alert_cb(rl->qtls->args.alert_cb_arg, alert)) { |
311 | 0 | QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
312 | 0 | return OSSL_RECORD_RETURN_FATAL; |
313 | 0 | } |
314 | 9.30k | break; |
315 | | |
316 | 51.7k | case SSL3_RT_HANDSHAKE: |
317 | | /* |
318 | | * We expect this to only fail on some fatal error (e.g. malloc |
319 | | * failure) |
320 | | */ |
321 | 51.7k | if (!rl->qtls->args.crypto_send_cb(template->buf + rl->written, |
322 | 51.7k | template->buflen - rl->written, |
323 | 51.7k | &consumed, |
324 | 51.7k | rl->qtls->args.crypto_send_cb_arg)) { |
325 | 0 | QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
326 | 0 | return OSSL_RECORD_RETURN_FATAL; |
327 | 0 | } |
328 | | /* |
329 | | * We might have written less than we wanted to if we have filled the |
330 | | * send stream buffer. |
331 | | */ |
332 | 51.7k | if (consumed + rl->written != template->buflen) { |
333 | 0 | if (!ossl_assert(consumed + rl->written < template->buflen)) { |
334 | 0 | QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
335 | 0 | return OSSL_RECORD_RETURN_FATAL; |
336 | 0 | } |
337 | | |
338 | | /* |
339 | | * We've not written everything we wanted to. Take a copy of the |
340 | | * template, remember how much we wrote so far and signal a retry. |
341 | | * The buffer supplied in the template is guaranteed to be the same |
342 | | * on a retry for handshake data |
343 | | */ |
344 | 0 | rl->written += consumed; |
345 | 0 | rl->template = *template; |
346 | 0 | BIO_set_retry_write(rl->dummybio); |
347 | |
|
348 | 0 | return OSSL_RECORD_RETURN_RETRY; |
349 | 0 | } |
350 | 51.7k | rl->written = 0; |
351 | 51.7k | break; |
352 | | |
353 | 0 | default: |
354 | | /* Anything else is unexpected and an error */ |
355 | 0 | QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
356 | 0 | return OSSL_RECORD_RETURN_FATAL; |
357 | 61.0k | } |
358 | | |
359 | 61.0k | return OSSL_RECORD_RETURN_SUCCESS; |
360 | 61.0k | } |
361 | | |
362 | | static int quic_retry_write_records(OSSL_RECORD_LAYER *rl) |
363 | 0 | { |
364 | 0 | return quic_write_records(rl, &rl->template, 1); |
365 | 0 | } |
366 | | |
367 | | static int quic_read_record(OSSL_RECORD_LAYER *rl, void **rechandle, |
368 | | int *rversion, uint8_t *type, const unsigned char **data, |
369 | | size_t *datalen, uint64_t *epoch, |
370 | | uint64_t *seq_num) |
371 | 49.3M | { |
372 | 49.3M | if (rl->recread != 0 || rl->recunreleased != 0) |
373 | 0 | return OSSL_RECORD_RETURN_FATAL; |
374 | | |
375 | 49.3M | BIO_clear_retry_flags(rl->dummybio); |
376 | | |
377 | 49.3M | if (!rl->qtls->args.crypto_recv_rcd_cb(data, datalen, |
378 | 49.3M | rl->qtls->args.crypto_recv_rcd_cb_arg)) { |
379 | 14 | QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
380 | 14 | return OSSL_RECORD_RETURN_FATAL; |
381 | 14 | } |
382 | | |
383 | 49.3M | if (*datalen == 0) { |
384 | 49.2M | BIO_set_retry_read(rl->dummybio); |
385 | 49.2M | return OSSL_RECORD_RETURN_RETRY; |
386 | 49.2M | } |
387 | | |
388 | 58.0k | *rechandle = rl; |
389 | 58.0k | *rversion = TLS1_3_VERSION; |
390 | 58.0k | *type = SSL3_RT_HANDSHAKE; |
391 | 58.0k | rl->recread = rl->recunreleased = *datalen; |
392 | | /* epoch/seq_num are not relevant for TLS */ |
393 | | |
394 | 58.0k | if (rl->msg_callback != NULL) { |
395 | 0 | unsigned char dummyrec[SSL3_RT_HEADER_LENGTH]; |
396 | | |
397 | | /* |
398 | | * For the purposes of the callback we "pretend" to be normal TLS, |
399 | | * and manufacture a dummy record header |
400 | | */ |
401 | 0 | dummyrec[0] = (rl->level == OSSL_RECORD_PROTECTION_LEVEL_NONE) |
402 | 0 | ? SSL3_RT_HANDSHAKE |
403 | 0 | : SSL3_RT_APPLICATION_DATA; |
404 | 0 | dummyrec[1] = (unsigned char)((TLS1_2_VERSION >> 8) & 0xff); |
405 | 0 | dummyrec[2] = (unsigned char)(TLS1_2_VERSION & 0xff); |
406 | | /* |
407 | | * *datalen will always fit into 2 bytes because our original buffer |
408 | | * size is less than that. |
409 | | */ |
410 | 0 | dummyrec[3] = (unsigned char)((*datalen >> 8) & 0xff); |
411 | 0 | dummyrec[4] = (unsigned char)(*datalen & 0xff); |
412 | |
|
413 | 0 | rl->msg_callback(0, TLS1_3_VERSION, SSL3_RT_HEADER, dummyrec, |
414 | 0 | SSL3_RT_HEADER_LENGTH, rl->cbarg); |
415 | 0 | rl->msg_callback(0, TLS1_3_VERSION, SSL3_RT_INNER_CONTENT_TYPE, type, 1, |
416 | 0 | rl->cbarg); |
417 | 0 | } |
418 | | |
419 | 58.0k | return OSSL_RECORD_RETURN_SUCCESS; |
420 | 49.3M | } |
421 | | |
422 | | static int quic_release_record(OSSL_RECORD_LAYER *rl, void *rechandle, |
423 | | size_t length) |
424 | 159k | { |
425 | 159k | if (!ossl_assert(rl->recread > 0) |
426 | 159k | || !ossl_assert(rl->recunreleased <= rl->recread) |
427 | 159k | || !ossl_assert(rl == rechandle) |
428 | 159k | || !ossl_assert(length <= rl->recunreleased)) { |
429 | 0 | QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
430 | 0 | return OSSL_RECORD_RETURN_FATAL; |
431 | 0 | } |
432 | | |
433 | 159k | if (rl->recunreleased == length) { |
434 | 45.8k | if (!rl->qtls->args.crypto_release_rcd_cb(rl->recread, |
435 | 45.8k | rl->qtls->args.crypto_release_rcd_cb_arg)) { |
436 | 0 | QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
437 | 0 | return OSSL_RECORD_RETURN_FATAL; |
438 | 0 | } |
439 | 45.8k | rl->recread = 0; |
440 | 45.8k | } |
441 | 159k | rl->recunreleased -= length; |
442 | 159k | return OSSL_RECORD_RETURN_SUCCESS; |
443 | 159k | } |
444 | | |
445 | | static int quic_get_alert_code(OSSL_RECORD_LAYER *rl) |
446 | 14 | { |
447 | 14 | return rl->alert; |
448 | 14 | } |
449 | | |
450 | | static int quic_set_protocol_version(OSSL_RECORD_LAYER *rl, int version) |
451 | 83.9k | { |
452 | | /* We only support TLSv1.3, so its bad if we negotiate anything else */ |
453 | 83.9k | if (!ossl_assert(version == TLS1_3_VERSION)) { |
454 | 0 | QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
455 | 0 | return 0; |
456 | 0 | } |
457 | | |
458 | 83.9k | return 1; |
459 | 83.9k | } |
460 | | |
461 | | static void quic_set_plain_alerts(OSSL_RECORD_LAYER *rl, int allow) |
462 | 30.6k | { |
463 | | /* We don't care */ |
464 | 30.6k | } |
465 | | |
466 | | static void quic_set_first_handshake(OSSL_RECORD_LAYER *rl, int first) |
467 | 210k | { |
468 | | /* We don't care */ |
469 | 210k | } |
470 | | |
471 | | static void quic_set_max_pipelines(OSSL_RECORD_LAYER *rl, size_t max_pipelines) |
472 | 0 | { |
473 | | /* We don't care */ |
474 | 0 | } |
475 | | |
476 | | static void quic_get_state(OSSL_RECORD_LAYER *rl, const char **shortstr, |
477 | | const char **longstr) |
478 | 0 | { |
479 | | /* |
480 | | * According to the docs, valid read state strings are: "RH"/"read header", |
481 | | * "RB"/"read body", and "unknown"/"unknown". We don't read records in quite |
482 | | * that way, so we report every "normal" state as "read header". In the |
483 | | * event of error then we report "unknown". |
484 | | */ |
485 | |
|
486 | 0 | if (rl->qtls->inerror) { |
487 | 0 | if (shortstr != NULL) |
488 | 0 | *shortstr = "unknown"; |
489 | 0 | if (longstr != NULL) |
490 | 0 | *longstr = "unknown"; |
491 | 0 | } else { |
492 | 0 | if (shortstr != NULL) |
493 | 0 | *shortstr = "RH"; |
494 | 0 | if (longstr != NULL) |
495 | 0 | *longstr = "read header"; |
496 | 0 | } |
497 | 0 | } |
498 | | |
499 | | static int quic_set_options(OSSL_RECORD_LAYER *rl, const OSSL_PARAM *options) |
500 | 0 | { |
501 | | /* |
502 | | * We don't support any options yet - but we might do at some point so |
503 | | * this could be useful. |
504 | | */ |
505 | 0 | return 1; |
506 | 0 | } |
507 | | |
508 | | static const COMP_METHOD *quic_get_compression(OSSL_RECORD_LAYER *rl) |
509 | 0 | { |
510 | | /* We only support TLSv1.3 which doesn't have compression */ |
511 | 0 | return NULL; |
512 | 0 | } |
513 | | |
514 | | static void quic_set_max_frag_len(OSSL_RECORD_LAYER *rl, size_t max_frag_len) |
515 | 0 | { |
516 | | /* This really doesn't make any sense for QUIC. Ignore it */ |
517 | 0 | } |
518 | | |
519 | | static int quic_alloc_buffers(OSSL_RECORD_LAYER *rl) |
520 | 0 | { |
521 | | /* |
522 | | * This is a hint only. We don't support it (yet), so just ignore the |
523 | | * request |
524 | | */ |
525 | 0 | return 1; |
526 | 0 | } |
527 | | |
528 | | static int quic_free_buffers(OSSL_RECORD_LAYER *rl) |
529 | 0 | { |
530 | | /* |
531 | | * This is a hint only. We don't support it (yet), so just ignore the |
532 | | * request |
533 | | */ |
534 | 0 | return 1; |
535 | 0 | } |
536 | | |
537 | | static int quic_set1_bio(OSSL_RECORD_LAYER *rl, BIO *bio) |
538 | 322k | { |
539 | 322k | if (bio != NULL && !BIO_up_ref(bio)) |
540 | 0 | return 0; |
541 | 322k | BIO_free(rl->dummybio); |
542 | 322k | rl->dummybio = bio; |
543 | | |
544 | 322k | return 1; |
545 | 322k | } |
546 | | |
547 | | /* |
548 | | * Never called functions |
549 | | * |
550 | | * Due to the way we are configured and used we never expect any of the next set |
551 | | * of functions to be called. Therefore we set them to always fail. |
552 | | */ |
553 | | |
554 | | static size_t quic_app_data_pending(OSSL_RECORD_LAYER *rl) |
555 | 0 | { |
556 | 0 | QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
557 | 0 | return (size_t)ossl_assert(0); |
558 | 0 | } |
559 | | |
560 | | static size_t quic_get_max_record_overhead(OSSL_RECORD_LAYER *rl) |
561 | 0 | { |
562 | 0 | QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
563 | 0 | return (size_t)ossl_assert(0); |
564 | 0 | } |
565 | | |
566 | | static int quic_increment_sequence_ctr(OSSL_RECORD_LAYER *rl) |
567 | 0 | { |
568 | 0 | QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
569 | 0 | return ossl_assert(0); |
570 | 0 | } |
571 | | |
572 | | /* End of never called functions */ |
573 | | |
574 | | static const OSSL_RECORD_METHOD quic_tls_record_method = { |
575 | | quic_new_record_layer, |
576 | | quic_free, |
577 | | quic_unprocessed_read_pending, |
578 | | quic_processed_read_pending, |
579 | | quic_app_data_pending, /* Never called */ |
580 | | quic_get_max_records, |
581 | | quic_write_records, |
582 | | quic_retry_write_records, |
583 | | quic_read_record, |
584 | | quic_release_record, |
585 | | quic_get_alert_code, |
586 | | quic_set1_bio, |
587 | | NULL, /* set1_peer: Not used for QUIC */ |
588 | | NULL, /* set_use_urxe: Not used for QUIC */ |
589 | | quic_set_protocol_version, |
590 | | quic_set_plain_alerts, |
591 | | quic_set_first_handshake, |
592 | | quic_set_max_pipelines, |
593 | | NULL, /* set_in_init: Optional - we don't need it */ |
594 | | quic_get_state, |
595 | | quic_set_options, |
596 | | quic_get_compression, |
597 | | quic_set_max_frag_len, |
598 | | quic_get_max_record_overhead, /* Never called */ |
599 | | quic_increment_sequence_ctr, /* Never called */ |
600 | | NULL, |
601 | | NULL, |
602 | | NULL, |
603 | | NULL, |
604 | | NULL, |
605 | | quic_alloc_buffers, |
606 | | quic_free_buffers |
607 | | }; |
608 | | |
609 | | static int add_transport_params_cb(SSL *s, unsigned int ext_type, |
610 | | unsigned int context, |
611 | | const unsigned char **out, size_t *outlen, |
612 | | X509 *x, size_t chainidx, int *al, |
613 | | void *add_arg) |
614 | 41.7k | { |
615 | 41.7k | QUIC_TLS *qtls = add_arg; |
616 | | |
617 | 41.7k | *out = qtls->local_transport_params; |
618 | 41.7k | *outlen = qtls->local_transport_params_len; |
619 | 41.7k | qtls->local_transport_params_consumed = 1; |
620 | 41.7k | return 1; |
621 | 41.7k | } |
622 | | |
623 | | static void free_transport_params_cb(SSL *s, unsigned int ext_type, |
624 | | unsigned int context, |
625 | | const unsigned char *out, |
626 | | void *add_arg) |
627 | 41.7k | { |
628 | 41.7k | } |
629 | | |
630 | | static int parse_transport_params_cb(SSL *s, unsigned int ext_type, |
631 | | unsigned int context, |
632 | | const unsigned char *in, |
633 | | size_t inlen, X509 *x, |
634 | | size_t chainidx, |
635 | | int *al, void *parse_arg) |
636 | 16.2k | { |
637 | 16.2k | QUIC_TLS *qtls = parse_arg; |
638 | | |
639 | 16.2k | return qtls->args.got_transport_params_cb(in, inlen, |
640 | 16.2k | qtls->args.got_transport_params_cb_arg); |
641 | 16.2k | } |
642 | | |
643 | | QUIC_TLS *ossl_quic_tls_new(const QUIC_TLS_ARGS *args) |
644 | 32.8k | { |
645 | 32.8k | QUIC_TLS *qtls; |
646 | | |
647 | 32.8k | if (args->crypto_send_cb == NULL |
648 | 32.8k | || args->crypto_recv_rcd_cb == NULL |
649 | 32.8k | || args->crypto_release_rcd_cb == NULL) { |
650 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER); |
651 | 0 | return NULL; |
652 | 0 | } |
653 | | |
654 | 32.8k | qtls = OPENSSL_zalloc(sizeof(*qtls)); |
655 | 32.8k | if (qtls == NULL) |
656 | 0 | return NULL; |
657 | | |
658 | 32.8k | if (args->ossl_quic && (qtls->error_state = OSSL_ERR_STATE_new()) == NULL) { |
659 | 0 | OPENSSL_free(qtls); |
660 | 0 | return NULL; |
661 | 0 | } |
662 | | |
663 | 32.8k | qtls->args = *args; |
664 | 32.8k | return qtls; |
665 | 32.8k | } |
666 | | |
667 | | int ossl_quic_tls_set0_ssl(QUIC_TLS *qtls, SSL *ssl) |
668 | 0 | { |
669 | 0 | if (!ossl_assert(qtls != NULL && ssl != NULL |
670 | 0 | && qtls->args.s == NULL && !qtls->configured)) |
671 | 0 | return 0; |
672 | | |
673 | 0 | qtls->args.s = ssl; |
674 | 0 | return 1; |
675 | 0 | } |
676 | | |
677 | | void ossl_quic_tls_free(QUIC_TLS *qtls) |
678 | 148k | { |
679 | 148k | if (qtls == NULL) |
680 | 107k | return; |
681 | 41.7k | OSSL_ERR_STATE_free(qtls->error_state); |
682 | 41.7k | OPENSSL_free(qtls); |
683 | 41.7k | } |
684 | | |
685 | | static int raise_error(QUIC_TLS *qtls, uint64_t error_code, |
686 | | const char *error_msg, |
687 | | const char *src_file, |
688 | | int src_line, |
689 | | const char *src_func) |
690 | 7.36k | { |
691 | | /* |
692 | | * When QTLS fails, add a "cover letter" error with information, potentially |
693 | | * with any underlying libssl errors underneath it (but our cover error may |
694 | | * be the only error in some cases). Then capture this into an ERR_STATE so |
695 | | * we can report it later if need be when the QUIC_CHANNEL asks for it. |
696 | | * For external QUIC TLS we just raise the error. |
697 | | */ |
698 | 7.36k | ERR_new(); |
699 | 7.36k | ERR_set_debug(src_file, src_line, src_func); |
700 | 7.36k | ERR_set_error(ERR_LIB_SSL, SSL_R_QUIC_HANDSHAKE_LAYER_ERROR, |
701 | 7.36k | "handshake layer error, error code %llu (0x%llx) (\"%s\")", |
702 | 7.36k | error_code, error_code, error_msg); |
703 | | |
704 | 7.36k | if (qtls->args.ossl_quic) { |
705 | 7.36k | OSSL_ERR_STATE_save_to_mark(qtls->error_state); |
706 | | |
707 | | /* |
708 | | * We record the error information reported via the QUIC protocol |
709 | | * separately. |
710 | | */ |
711 | 7.36k | qtls->error_code = error_code; |
712 | 7.36k | qtls->error_msg = error_msg; |
713 | 7.36k | qtls->inerror = 1; |
714 | | |
715 | 7.36k | ERR_pop_to_mark(); |
716 | 7.36k | } |
717 | 7.36k | return 0; |
718 | 7.36k | } |
719 | | |
720 | | #define RAISE_ERROR(qtls, error_code, error_msg) \ |
721 | 1.74k | raise_error((qtls), (error_code), (error_msg), \ |
722 | 1.74k | OPENSSL_FILE, OPENSSL_LINE, OPENSSL_FUNC) |
723 | | |
724 | | #ifndef OPENSSL_NO_QUIC |
725 | | #define RAISE_INTERNAL_ERROR(qtls) \ |
726 | 1.73k | RAISE_ERROR((qtls), OSSL_QUIC_ERR_INTERNAL_ERROR, "internal error") |
727 | | #else |
728 | | #define RAISE_INTERNAL_ERROR(qtls) \ |
729 | | RAISE_ERROR((qtls), 0x01, "internal error") |
730 | | #endif |
731 | | |
732 | | int ossl_quic_tls_configure(QUIC_TLS *qtls) |
733 | 32.8k | { |
734 | 32.8k | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(qtls->args.s); |
735 | 32.8k | BIO *nullbio; |
736 | | |
737 | 32.8k | if (sc == NULL || !SSL_set_min_proto_version(qtls->args.s, TLS1_3_VERSION)) |
738 | 0 | return RAISE_INTERNAL_ERROR(qtls); |
739 | | |
740 | 32.8k | nullbio = BIO_new(BIO_s_null()); |
741 | 32.8k | if (nullbio == NULL) |
742 | 0 | return RAISE_INTERNAL_ERROR(qtls); |
743 | | |
744 | | /* |
745 | | * Our custom record layer doesn't use the BIO - but libssl generally |
746 | | * expects one to be present. |
747 | | */ |
748 | 32.8k | SSL_set_bio(qtls->args.s, nullbio, nullbio); |
749 | | |
750 | 32.8k | SSL_clear_options(qtls->args.s, SSL_OP_ENABLE_MIDDLEBOX_COMPAT); |
751 | 32.8k | ossl_ssl_set_custom_record_layer(sc, &quic_tls_record_method, qtls); |
752 | | |
753 | 32.8k | if (!ossl_tls_add_custom_ext_intern(NULL, &sc->cert->custext, |
754 | 32.8k | qtls->args.is_server ? ENDPOINT_SERVER |
755 | 32.8k | : ENDPOINT_CLIENT, |
756 | 32.8k | TLSEXT_TYPE_quic_transport_parameters, |
757 | 32.8k | SSL_EXT_TLS1_3_ONLY |
758 | 32.8k | | SSL_EXT_CLIENT_HELLO |
759 | 32.8k | | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, |
760 | 32.8k | add_transport_params_cb, |
761 | 32.8k | free_transport_params_cb, qtls, |
762 | 32.8k | parse_transport_params_cb, qtls)) |
763 | 0 | return 0; |
764 | | |
765 | 32.8k | sc->s3.flags |= TLS1_FLAGS_QUIC; |
766 | | |
767 | 32.8k | return 1; |
768 | 32.8k | } |
769 | | |
770 | | #ifndef OPENSSL_NO_QUIC |
771 | | int ossl_quic_tls_tick(QUIC_TLS *qtls) |
772 | 6.63M | { |
773 | 6.63M | int ret, err; |
774 | 6.63M | const unsigned char *alpn; |
775 | 6.63M | unsigned int alpnlen; |
776 | | |
777 | 6.63M | if (qtls->inerror) |
778 | 1.15k | return 0; |
779 | | |
780 | | /* SSL_listen_ex() attaches the SSL after the channel is queued. */ |
781 | 6.63M | if (qtls->args.s == NULL) |
782 | 0 | return 1; |
783 | | |
784 | | /* |
785 | | * SSL_get_error does not truly know what the cause of an SSL_read failure |
786 | | * is and to some extent guesses based on contextual information. In |
787 | | * particular, if there is _any_ ERR on the error stack, SSL_ERROR_SSL or |
788 | | * SSL_ERROR_SYSCALL will be returned no matter what and there is no |
789 | | * possibility of SSL_ERROR_WANT_READ/WRITE being returned, even if that was |
790 | | * the actual cause of the SSL_read() failure. |
791 | | * |
792 | | * This means that ordinarily, the below code might not work right if the |
793 | | * application has any ERR on the error stack. In order to make this code |
794 | | * perform correctly regardless of prior ERR state, we use a variant of |
795 | | * SSL_get_error() which ignores the error stack. However, some ERRs are |
796 | | * raised by SSL_read() and actually indicate that something has gone wrong |
797 | | * during the call to SSL_read(). We therefore adopt a strategy of marking |
798 | | * the ERR stack and seeing if any errors get appended during the call to |
799 | | * SSL_read(). If they are, we assume SSL_read() has raised an error and |
800 | | * that we should use normal SSL_get_error() handling. |
801 | | * |
802 | | * NOTE: Ensure all escape paths from this function call |
803 | | * ERR_clear_to_mark(). The RAISE macros handle this in failure cases. |
804 | | */ |
805 | 6.63M | ERR_set_mark(); |
806 | | |
807 | 6.63M | if (!qtls->configured) { |
808 | 8.47k | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(qtls->args.s); |
809 | 8.47k | SSL_CTX *sctx; |
810 | | |
811 | 8.47k | if (sc == NULL) |
812 | 0 | return RAISE_INTERNAL_ERROR(qtls); |
813 | 8.47k | sctx = SSL_CONNECTION_GET_CTX(sc); |
814 | | |
815 | | /* |
816 | | * No matter how the user has configured us, there are certain |
817 | | * requirements for QUIC-TLS that we enforce |
818 | | */ |
819 | | |
820 | | /* ALPN is a requirement for QUIC and must be set */ |
821 | 8.47k | if (qtls->args.is_server) { |
822 | 0 | if (sctx->ext.alpn_select_cb == NULL) |
823 | 0 | return RAISE_INTERNAL_ERROR(qtls); |
824 | 8.47k | } else { |
825 | 8.47k | if (sc->ext.alpn == NULL || sc->ext.alpn_len == 0) |
826 | 0 | return RAISE_ERROR(qtls, OSSL_QUIC_ERR_CRYPTO_NO_APP_PROTO, |
827 | 8.47k | "ALPN must be configured when using QUIC"); |
828 | 8.47k | } |
829 | | |
830 | 8.47k | if (!ossl_quic_tls_configure(qtls)) |
831 | 0 | return RAISE_INTERNAL_ERROR(qtls); |
832 | | |
833 | 8.47k | sc->s3.flags |= TLS1_FLAGS_QUIC_INTERNAL; |
834 | | |
835 | 8.47k | if (qtls->args.is_server) |
836 | 0 | SSL_set_accept_state(qtls->args.s); |
837 | 8.47k | else |
838 | 8.47k | SSL_set_connect_state(qtls->args.s); |
839 | | |
840 | 8.47k | qtls->configured = 1; |
841 | 8.47k | } |
842 | | |
843 | 6.63M | if (qtls->complete) |
844 | | /* |
845 | | * There should never be app data to read, but calling SSL_read() will |
846 | | * ensure any post-handshake messages are processed. |
847 | | */ |
848 | 4.48M | ret = SSL_read(qtls->args.s, NULL, 0); |
849 | 2.15M | else |
850 | 2.15M | ret = SSL_do_handshake(qtls->args.s); |
851 | | |
852 | 6.63M | if (ret <= 0) { |
853 | 6.63M | err = ossl_ssl_get_error(qtls->args.s, ret, |
854 | 6.63M | /*check_err=*/ERR_count_to_mark() > 0); |
855 | | |
856 | 6.63M | switch (err) { |
857 | 6.63M | case SSL_ERROR_WANT_READ: |
858 | 6.63M | case SSL_ERROR_WANT_WRITE: |
859 | 6.63M | case SSL_ERROR_WANT_CLIENT_HELLO_CB: |
860 | 6.63M | case SSL_ERROR_WANT_X509_LOOKUP: |
861 | 6.63M | case SSL_ERROR_WANT_RETRY_VERIFY: |
862 | 6.63M | ERR_pop_to_mark(); |
863 | 6.63M | return 1; |
864 | | |
865 | 1.73k | default: |
866 | 1.73k | return RAISE_INTERNAL_ERROR(qtls); |
867 | 6.63M | } |
868 | 6.63M | } |
869 | | |
870 | 2.20k | if (!qtls->complete) { |
871 | | /* Validate that we have ALPN */ |
872 | 2.20k | SSL_get0_alpn_selected(qtls->args.s, &alpn, &alpnlen); |
873 | 2.20k | if (alpn == NULL || alpnlen == 0) |
874 | 4 | return RAISE_ERROR(qtls, OSSL_QUIC_ERR_CRYPTO_NO_APP_PROTO, |
875 | 2.20k | "no application protocol negotiated"); |
876 | | |
877 | 2.20k | qtls->complete = 1; |
878 | 2.20k | ERR_pop_to_mark(); |
879 | 2.20k | return qtls->args.handshake_complete_cb(qtls->args.handshake_complete_cb_arg); |
880 | 2.20k | } |
881 | | |
882 | 0 | ERR_pop_to_mark(); |
883 | 0 | return 1; |
884 | 2.20k | } |
885 | | #endif |
886 | | |
887 | | void ossl_quic_tls_clear(QUIC_TLS *qtls) |
888 | 214k | { |
889 | 214k | if (qtls == NULL) |
890 | 214k | return; |
891 | 0 | qtls->local_transport_params_consumed = 0; |
892 | 0 | } |
893 | | |
894 | | int ossl_quic_tls_set_transport_params(QUIC_TLS *qtls, |
895 | | const unsigned char *transport_params, |
896 | | size_t transport_params_len) |
897 | 32.8k | { |
898 | 32.8k | if (qtls->local_transport_params_consumed) |
899 | 0 | return 0; |
900 | | |
901 | 32.8k | qtls->local_transport_params = transport_params; |
902 | 32.8k | qtls->local_transport_params_len = transport_params_len; |
903 | 32.8k | return 1; |
904 | 32.8k | } |
905 | | |
906 | | int ossl_quic_tls_get_error(QUIC_TLS *qtls, |
907 | | uint64_t *error_code, |
908 | | const char **error_msg, |
909 | | ERR_STATE **error_state) |
910 | 49.2M | { |
911 | 49.2M | if (qtls->inerror) { |
912 | 15.0k | *error_code = qtls->error_code; |
913 | 15.0k | *error_msg = qtls->error_msg; |
914 | 15.0k | *error_state = qtls->error_state; |
915 | 15.0k | } |
916 | | |
917 | 49.2M | return qtls->inerror; |
918 | 49.2M | } |
919 | | |
920 | | /* |
921 | | * Returns true if the last handshake record message we processed was a |
922 | | * CertificateRequest |
923 | | */ |
924 | | int ossl_quic_tls_is_cert_request(QUIC_TLS *qtls) |
925 | 122 | { |
926 | 122 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(qtls->args.s); |
927 | | |
928 | 122 | if (sc == NULL) |
929 | 0 | return 0; |
930 | | |
931 | 122 | return sc->s3.tmp.message_type == SSL3_MT_CERTIFICATE_REQUEST; |
932 | 122 | } |
933 | | |
934 | | /* |
935 | | * Returns true if the last session associated with the connection has an |
936 | | * invalid max_early_data value for QUIC. |
937 | | */ |
938 | | int ossl_quic_tls_has_bad_max_early_data(QUIC_TLS *qtls) |
939 | 11 | { |
940 | 11 | uint32_t max_early_data = SSL_get0_session(qtls->args.s)->ext.max_early_data; |
941 | | |
942 | | /* |
943 | | * If max_early_data was present we always ensure a non-zero value is |
944 | | * stored in the session for QUIC. Therefore if max_early_data == 0 here |
945 | | * we can be confident that it was not present in the NewSessionTicket |
946 | | */ |
947 | 11 | return max_early_data != 0xffffffff && max_early_data != 0; |
948 | 11 | } |
949 | | |
950 | | int ossl_quic_tls_set_early_data_enabled(QUIC_TLS *qtls, int enabled) |
951 | 0 | { |
952 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(qtls->args.s); |
953 | |
|
954 | 0 | if (sc == NULL || !SSL_IS_QUIC_HANDSHAKE(sc) || !SSL_in_before(qtls->args.s)) |
955 | 0 | return 0; |
956 | | |
957 | 0 | if (!enabled) { |
958 | 0 | sc->max_early_data = 0; |
959 | 0 | sc->early_data_state = SSL_EARLY_DATA_NONE; |
960 | 0 | return 1; |
961 | 0 | } |
962 | | |
963 | 0 | if (sc->server) { |
964 | 0 | sc->max_early_data = 0xffffffff; |
965 | 0 | sc->early_data_state = SSL_EARLY_DATA_ACCEPTING; |
966 | 0 | return 1; |
967 | 0 | } |
968 | | |
969 | 0 | if ((sc->session == NULL || sc->session->ext.max_early_data != 0xffffffff) |
970 | 0 | && sc->psk_use_session_cb == NULL) |
971 | 0 | return 0; |
972 | | |
973 | 0 | sc->early_data_state = SSL_EARLY_DATA_CONNECTING; |
974 | 0 | return 1; |
975 | 0 | } |