/src/openssl41/ssl/d1_lib.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2005-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 | | |
10 | | #include "internal/e_os.h" |
11 | | #include "internal/e_winsock.h" /* struct timeval for DTLS_CTRL_GET_TIMEOUT */ |
12 | | #include <stdio.h> |
13 | | #include <openssl/objects.h> |
14 | | #include <openssl/rand.h> |
15 | | #include <openssl/evp.h> |
16 | | #include <openssl/core_names.h> |
17 | | #include "ssl_local.h" |
18 | | #include "internal/time.h" |
19 | | #include "internal/ssl_unwrap.h" |
20 | | #include "internal/hashfunc.h" |
21 | | #include "internal/dtls_record_rx.h" |
22 | | #include "internal/dgram_demux.h" |
23 | | #include "internal/dgram_conn_lookup.h" |
24 | | #include "internal/rio_notifier.h" |
25 | | |
26 | | static int dtls1_handshake_write(SSL_CONNECTION *s); |
27 | | static const size_t dtls1_link_min_mtu = 256; |
28 | | #ifndef OPENSSL_NO_DTLS |
29 | | static OSSL_TIME dtls_listener_get_time_direct(DTLS_LISTENER *dl); |
30 | | #endif |
31 | | |
32 | | const SSL3_ENC_METHOD DTLSv1_enc_data = { |
33 | | tls1_setup_key_block, |
34 | | tls1_generate_master_secret, |
35 | | tls1_change_cipher_state, |
36 | | tls1_final_finish_mac, |
37 | | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
38 | | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
39 | | tls1_alert_code, |
40 | | tls1_export_keying_material, |
41 | | SSL_ENC_FLAG_DTLS, |
42 | | dtls1_set_handshake_header, |
43 | | dtls1_close_construct_packet, |
44 | | dtls1_handshake_write |
45 | | }; |
46 | | |
47 | | const SSL3_ENC_METHOD DTLSv1_2_enc_data = { |
48 | | tls1_setup_key_block, |
49 | | tls1_generate_master_secret, |
50 | | tls1_change_cipher_state, |
51 | | tls1_final_finish_mac, |
52 | | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
53 | | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
54 | | tls1_alert_code, |
55 | | tls1_export_keying_material, |
56 | | SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_SIGALGS |
57 | | | SSL_ENC_FLAG_SHA256_PRF | SSL_ENC_FLAG_TLS1_2_CIPHERS, |
58 | | dtls1_set_handshake_header, |
59 | | dtls1_close_construct_packet, |
60 | | dtls1_handshake_write |
61 | | }; |
62 | | |
63 | | const SSL3_ENC_METHOD DTLSv1_3_enc_data = { |
64 | | tls13_setup_key_block, |
65 | | tls13_generate_master_secret, |
66 | | tls13_change_cipher_state, |
67 | | tls13_final_finish_mac, |
68 | | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
69 | | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
70 | | tls13_alert_code, |
71 | | tls13_export_keying_material, |
72 | | SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF, |
73 | | dtls1_set_handshake_header, |
74 | | dtls1_close_construct_packet, |
75 | | dtls1_handshake_write |
76 | | }; |
77 | | |
78 | | OSSL_TIME dtls1_default_timeout(void) |
79 | 40.9k | { |
80 | | /* |
81 | | * 2 hours, the 24 hours mentioned in the DTLSv1 spec is way too long for |
82 | | * http, the cache would over fill |
83 | | */ |
84 | 40.9k | return ossl_seconds2time(60 * 60 * 2); |
85 | 40.9k | } |
86 | | |
87 | | int dtls1_new(SSL *ssl) |
88 | 10.1k | { |
89 | 10.1k | DTLS1_STATE *d1; |
90 | 10.1k | SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
91 | | |
92 | 10.1k | if (s == NULL) |
93 | 0 | return 0; |
94 | | |
95 | 10.1k | if (!DTLS_RECORD_LAYER_new(&s->rlayer)) { |
96 | 0 | return 0; |
97 | 0 | } |
98 | | |
99 | 10.1k | if (!ssl3_new(ssl)) |
100 | 0 | return 0; |
101 | 10.1k | if ((d1 = OPENSSL_zalloc(sizeof(*d1))) == NULL) { |
102 | 0 | ssl3_free(ssl); |
103 | 0 | return 0; |
104 | 0 | } |
105 | | |
106 | 10.1k | d1->hello_verify_request = SSL_HVR_NONE; |
107 | | |
108 | 10.1k | s->d1 = d1; |
109 | | |
110 | 10.1k | if (!ssl->method->ssl_clear(ssl)) |
111 | 0 | return 0; |
112 | | |
113 | 10.1k | return 1; |
114 | 10.1k | } |
115 | | |
116 | | static void dtls1_clear_queues(SSL_CONNECTION *s) |
117 | 163k | { |
118 | 163k | dtls1_clear_received_buffer(s); |
119 | 163k | dtls1_clear_sent_buffer(s, 0); |
120 | 163k | ossl_list_record_number_elem_free(&s->d1->ack_rec_num); |
121 | 163k | } |
122 | | |
123 | | void dtls1_clear_received_buffer(SSL_CONNECTION *s) |
124 | 172k | { |
125 | 172k | pitem *item = NULL; |
126 | 172k | hm_fragment *frag = NULL; |
127 | 172k | pqueue *rcvd_messages = &s->d1->rcvd_messages; |
128 | | |
129 | 178k | while ((item = pqueue_pop(rcvd_messages)) != NULL) { |
130 | 5.26k | frag = (hm_fragment *)item->data; |
131 | 5.26k | dtls1_hm_fragment_free(frag); |
132 | 5.26k | pitem_free(item); |
133 | 5.26k | } |
134 | 172k | s->d1->has_change_cipher_spec = 0; |
135 | 172k | } |
136 | | |
137 | | void ossl_list_record_number_elem_free(OSSL_LIST(record_number) * p_list) |
138 | 62.8k | { |
139 | 62.8k | DTLS1_RECORD_NUMBER *p_elem; |
140 | 62.8k | DTLS1_RECORD_NUMBER *p_elem_next = NULL; |
141 | | |
142 | 62.8k | if (p_list != NULL) |
143 | 62.8k | p_elem_next = ossl_list_record_number_head(p_list); |
144 | | |
145 | 62.9k | while ((p_elem = p_elem_next) != NULL) { |
146 | 95 | p_elem_next = ossl_list_record_number_next(p_elem_next); |
147 | 95 | ossl_list_record_number_remove(p_list, p_elem); |
148 | 95 | OPENSSL_free(p_elem); |
149 | 95 | } |
150 | 62.8k | } |
151 | | |
152 | | DTLS1_RECORD_NUMBER *dtls1_record_number_new(uint64_t epoch, uint64_t seqnum) |
153 | 95 | { |
154 | 95 | DTLS1_RECORD_NUMBER *recnum = OPENSSL_zalloc(sizeof(*recnum)); |
155 | | |
156 | 95 | if (recnum != NULL) { |
157 | 95 | recnum->epoch = epoch; |
158 | 95 | recnum->seqnum = seqnum; |
159 | 95 | } |
160 | | |
161 | 95 | return recnum; |
162 | 95 | } |
163 | | |
164 | | void dtls1_acknowledge_sent_buffer(SSL_CONNECTION *s, uint64_t before_epoch) |
165 | 0 | { |
166 | 0 | pitem *item = NULL; |
167 | 0 | piterator iter = pqueue_iterator(&s->d1->sent_messages); |
168 | |
|
169 | 0 | while ((item = pqueue_next(&iter)) != NULL) { |
170 | 0 | dtls_sent_msg *sent_msg = (dtls_sent_msg *)item->data; |
171 | 0 | DTLS1_RECORD_NUMBER *recnum; |
172 | 0 | DTLS1_RECORD_NUMBER *recnum_next = ossl_list_record_number_head(&sent_msg->rec_nums); |
173 | |
|
174 | 0 | while ((recnum = recnum_next) != NULL) { |
175 | 0 | recnum_next = ossl_list_record_number_next(recnum_next); |
176 | |
|
177 | 0 | if (recnum->epoch < before_epoch) { |
178 | 0 | ossl_list_record_number_remove(&sent_msg->rec_nums, recnum); |
179 | 0 | OPENSSL_free(recnum); |
180 | 0 | } |
181 | 0 | } |
182 | 0 | } |
183 | 0 | } |
184 | | |
185 | | void dtls1_clear_sent_buffer(SSL_CONNECTION *s, int keep_unacked_msgs) |
186 | 46.3k | { |
187 | 46.3k | pitem *item = NULL; |
188 | 46.3k | pqueue *remaining_sent_messages = pqueue_new(); |
189 | 46.3k | pqueue *sent_messages = &s->d1->sent_messages; |
190 | | |
191 | 68.6k | while ((item = pqueue_pop(sent_messages)) != NULL) { |
192 | 22.3k | dtls_sent_msg *sent_msg = (dtls_sent_msg *)item->data; |
193 | 22.3k | unsigned char msg_type = sent_msg->msg_info.msg_type; |
194 | 22.3k | unsigned char record_type = sent_msg->msg_info.record_type; |
195 | | |
196 | 22.3k | if (SSL_CONNECTION_IS_DTLS13(s) |
197 | 7 | && !ossl_list_record_number_is_empty(&sent_msg->rec_nums) |
198 | 0 | && keep_unacked_msgs) { |
199 | 0 | pqueue_insert(remaining_sent_messages, item); |
200 | 0 | continue; |
201 | 0 | } |
202 | | |
203 | 22.3k | if (((!SSL_CONNECTION_IS_DTLS13(s) && record_type == SSL3_RT_CHANGE_CIPHER_SPEC) |
204 | 20.4k | || (SSL_CONNECTION_IS_DTLS13(s) |
205 | 7 | && (msg_type == SSL3_MT_FINISHED |
206 | 7 | || msg_type == SSL3_MT_SERVER_HELLO |
207 | 7 | || msg_type == SSL3_MT_KEY_UPDATE))) |
208 | 1.87k | && sent_msg->saved_retransmit_state.wrlmethod != NULL |
209 | 1.87k | && s->rlayer.wrl != sent_msg->saved_retransmit_state.wrl) { |
210 | | /* |
211 | | * If we're freeing the CCS then we're done with the old wrl and it |
212 | | * can bee freed |
213 | | */ |
214 | 1.87k | sent_msg->saved_retransmit_state.wrlmethod->free(sent_msg->saved_retransmit_state.wrl); |
215 | 1.87k | } |
216 | | |
217 | 22.3k | dtls1_sent_msg_free(sent_msg); |
218 | 22.3k | pitem_free(item); |
219 | 22.3k | } |
220 | | |
221 | 46.3k | if (SSL_CONNECTION_IS_DTLS13(s)) |
222 | 7 | while ((item = pqueue_pop(remaining_sent_messages)) != NULL) |
223 | 0 | pqueue_insert(&s->d1->sent_messages, item); |
224 | | |
225 | 46.3k | pqueue_free(remaining_sent_messages); |
226 | 46.3k | } |
227 | | |
228 | | /* |
229 | | * Before RECORD_LAYER_clear() frees s->rlayer.wrl, null out any |
230 | | * saved_retransmit_state.wrl pointers in the sent_messages queue that |
231 | | * reference it. This transfers ownership of that free exclusively to |
232 | | * RECORD_LAYER_clear and prevents dtls1_clear_sent_buffer from freeing |
233 | | * the same pointer a second time. Entries with a different (older) wrl |
234 | | * pointer are left untouched and will be freed correctly later. |
235 | | */ |
236 | | void dtls1_clear_current_wrl_from_sent_buffer(SSL_CONNECTION *s) |
237 | 10.1k | { |
238 | 10.1k | pitem *item; |
239 | 10.1k | piterator iter = pqueue_iterator(&s->d1->sent_messages); |
240 | | |
241 | 29.5k | while ((item = pqueue_next(&iter)) != NULL) { |
242 | 19.4k | dtls_sent_msg *sent_msg = (dtls_sent_msg *)item->data; |
243 | | |
244 | 19.4k | if (sent_msg->saved_retransmit_state.wrl == s->rlayer.wrl) { |
245 | 15.6k | sent_msg->saved_retransmit_state.wrl = NULL; |
246 | 15.6k | sent_msg->saved_retransmit_state.wrlmethod = NULL; |
247 | 15.6k | } |
248 | 19.4k | } |
249 | 10.1k | } |
250 | | |
251 | | int dtls_any_sent_messages_are_missing_acknowledge(SSL_CONNECTION *s) |
252 | 0 | { |
253 | 0 | pitem *item; |
254 | 0 | piterator iter = pqueue_iterator(&s->d1->sent_messages); |
255 | |
|
256 | 0 | while ((item = pqueue_next(&iter)) != NULL) { |
257 | 0 | dtls_sent_msg *msg = (dtls_sent_msg *)item->data; |
258 | |
|
259 | 0 | if (!ossl_list_record_number_is_empty(&msg->rec_nums)) |
260 | 0 | return 1; |
261 | 0 | } |
262 | | |
263 | 0 | return 0; |
264 | 0 | } |
265 | | |
266 | | void dtls1_free(SSL *ssl) |
267 | 10.1k | { |
268 | 10.1k | SSL_CONNECTION *s; |
269 | | |
270 | 10.1k | #ifndef OPENSSL_NO_DTLS |
271 | 10.1k | if (IS_DTLS_LISTENER(ssl)) { |
272 | 0 | ossl_dtls_listener_free(ssl); |
273 | 0 | return; |
274 | 0 | } |
275 | 10.1k | #endif |
276 | | |
277 | 10.1k | s = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
278 | | |
279 | 10.1k | if (s == NULL) |
280 | 0 | return; |
281 | | |
282 | 10.1k | #ifndef OPENSSL_NO_DTLS |
283 | | /* |
284 | | * If this connection was created by a listener, unregister it from the |
285 | | * listener's established_conns lookup table to prevent use-after-free. |
286 | | * The listener routes incoming packets to connections via this table, |
287 | | * so we must remove ourselves before freeing. |
288 | | */ |
289 | 10.1k | if (s->d1 != NULL && s->d1->listener != NULL) |
290 | 0 | ossl_dtls_listener_unregister_established_conn(s->d1->listener, |
291 | 0 | &s->d1->peer_addr); |
292 | 10.1k | #endif |
293 | | |
294 | 10.1k | if (s->d1 != NULL) |
295 | 10.1k | dtls1_clear_queues(s); |
296 | | |
297 | 10.1k | #ifndef OPENSSL_NO_DTLS |
298 | 10.1k | if (s->d1 != NULL) { |
299 | 10.1k | ossl_dtls_rx_free(s->d1->rx); |
300 | | |
301 | 10.1k | if (s->d1->listener != NULL) |
302 | 0 | SSL_free(s->d1->listener); |
303 | 10.1k | } |
304 | 10.1k | #endif |
305 | | |
306 | 10.1k | DTLS_RECORD_LAYER_free(&s->rlayer); |
307 | 10.1k | ssl3_free(ssl); |
308 | 10.1k | OPENSSL_free(s->d1); |
309 | 10.1k | s->d1 = NULL; |
310 | 10.1k | } |
311 | | |
312 | | int dtls1_clear(SSL *ssl) |
313 | 40.5k | { |
314 | 40.5k | size_t mtu; |
315 | 40.5k | size_t link_mtu; |
316 | 40.5k | SSL_CONNECTION *s; |
317 | | |
318 | 40.5k | #ifndef OPENSSL_NO_DTLS |
319 | 40.5k | if (IS_DTLS_LISTENER(ssl)) |
320 | 0 | return 1; |
321 | 40.5k | #endif |
322 | | |
323 | 40.5k | s = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
324 | | |
325 | 40.5k | if (s == NULL) |
326 | 0 | return 0; |
327 | | |
328 | 40.5k | DTLS_RECORD_LAYER_clear(&s->rlayer); |
329 | | |
330 | 40.5k | if (s->d1) { |
331 | 30.4k | DTLS_timer_cb timer_cb = s->d1->timer_cb; |
332 | 30.4k | #ifndef OPENSSL_NO_SOCK |
333 | 30.4k | BIO_ADDR peer_addr = s->d1->peer_addr; |
334 | 30.4k | #endif |
335 | 30.4k | #ifndef OPENSSL_NO_DTLS |
336 | 30.4k | DTLS_RX *rx = s->d1->rx; |
337 | 30.4k | SSL *listener = s->d1->listener; |
338 | 30.4k | OSSL_TIME created_at = s->d1->created_at; |
339 | 30.4k | unsigned int req_blocking_mode = s->d1->req_blocking_mode; |
340 | 30.4k | unsigned int force_nonblocking = s->d1->force_nonblocking; |
341 | 30.4k | unsigned int being_driven = s->d1->being_driven; |
342 | 30.4k | #endif |
343 | | |
344 | 30.4k | mtu = s->d1->mtu; |
345 | 30.4k | link_mtu = s->d1->link_mtu; |
346 | | |
347 | 30.4k | dtls1_clear_queues(s); |
348 | | |
349 | 30.4k | memset(s->d1, 0, sizeof(*s->d1)); |
350 | | |
351 | | /* Restore the timer callback from previous state */ |
352 | 30.4k | s->d1->timer_cb = timer_cb; |
353 | | |
354 | 30.4k | #ifndef OPENSSL_NO_SOCK |
355 | | /* |
356 | | * Restore peer address, DTLS_RX, listener, and created_at for |
357 | | * listener-created connections. These are set via |
358 | | * SSL_set1_initial_peer_addr(), ossl_dtls_rx_new(), and |
359 | | * dtls_listener_create_conn_ssl() before the handshake starts, |
360 | | * and must be preserved across SSL_clear(). |
361 | | */ |
362 | 30.4k | s->d1->peer_addr = peer_addr; |
363 | 30.4k | #endif |
364 | 30.4k | #ifndef OPENSSL_NO_DTLS |
365 | 30.4k | s->d1->rx = rx; |
366 | 30.4k | s->d1->listener = listener; |
367 | | /* |
368 | | * The blocking mode is a property of the connection as the application |
369 | | * configured it, not of the handshake, so it survives a clear. |
370 | | */ |
371 | 30.4k | s->d1->req_blocking_mode = req_blocking_mode; |
372 | | /* |
373 | | * SSL_clear() can be called from inside the very SSL_accept() the |
374 | | * listener is driving, so losing this would let the connection block |
375 | | * there and stall the listener. |
376 | | */ |
377 | 30.4k | s->d1->force_nonblocking = force_nonblocking; |
378 | | /* |
379 | | * being_driven says the listener is driving this connection's |
380 | | * handshake, and is what keeps a concurrent tick from collecting it a |
381 | | * second time. Losing it would let two threads into the state machine |
382 | | * for one connection. |
383 | | */ |
384 | 30.4k | s->d1->being_driven = being_driven; |
385 | 30.4k | s->d1->created_at = created_at; |
386 | 30.4k | #endif |
387 | | |
388 | 30.4k | if (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU) { |
389 | 0 | s->d1->mtu = mtu; |
390 | 0 | s->d1->link_mtu = link_mtu; |
391 | 0 | } |
392 | 30.4k | } |
393 | | |
394 | 40.5k | if (!ssl3_clear(ssl)) |
395 | 0 | return 0; |
396 | | |
397 | 40.5k | if (ssl->method->version == DTLS_ANY_VERSION) |
398 | 40.5k | s->version = DTLS_MAX_VERSION_INTERNAL; |
399 | 0 | #ifndef OPENSSL_NO_DTLS1_METHOD |
400 | 0 | else if (s->options & SSL_OP_CISCO_ANYCONNECT) |
401 | 0 | s->client_version = s->version = DTLS1_BAD_VER; |
402 | 0 | #endif |
403 | 0 | else |
404 | 0 | s->version = ssl->method->version; |
405 | | |
406 | 40.5k | return 1; |
407 | 40.5k | } |
408 | | |
409 | | long dtls1_ctrl(SSL *ssl, int cmd, long larg, void *parg) |
410 | 5.72k | { |
411 | 5.72k | int ret = 0; |
412 | 5.72k | OSSL_TIME t; |
413 | 5.72k | SSL_CONNECTION *s; |
414 | | |
415 | 5.72k | if (IS_DTLS_LISTENER(ssl)) |
416 | 0 | return 0; |
417 | | |
418 | 5.72k | s = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
419 | | |
420 | 5.72k | if (s == NULL) |
421 | 0 | return 0; |
422 | | |
423 | 5.72k | switch (cmd) { |
424 | 0 | case DTLS_CTRL_GET_TIMEOUT: |
425 | 0 | if (dtls1_get_timeout(s, &t)) { |
426 | 0 | *(struct timeval *)parg = ossl_time_to_timeval(t); |
427 | 0 | ret = 1; |
428 | 0 | } |
429 | 0 | break; |
430 | 0 | case DTLS_CTRL_HANDLE_TIMEOUT: |
431 | 0 | ret = dtls1_handle_timeout(s); |
432 | 0 | break; |
433 | 0 | case DTLS_CTRL_SET_LINK_MTU: |
434 | 0 | if (larg < (long)dtls1_link_min_mtu) |
435 | 0 | return 0; |
436 | 0 | s->d1->link_mtu = larg; |
437 | 0 | return 1; |
438 | 0 | case DTLS_CTRL_GET_LINK_MIN_MTU: |
439 | 0 | return (long)dtls1_link_min_mtu; |
440 | 0 | case SSL_CTRL_SET_MTU: |
441 | | /* |
442 | | * We may not have a BIO set yet so can't call dtls1_min_mtu() |
443 | | * We'll have to make do with dtls1_link_min_mtu and max overhead |
444 | | */ |
445 | 0 | if (larg < (long)dtls1_link_min_mtu - DTLS1_MAX_MTU_OVERHEAD) |
446 | 0 | return 0; |
447 | 0 | s->d1->mtu = larg; |
448 | 0 | return larg; |
449 | 5.72k | default: |
450 | 5.72k | ret = ssl3_ctrl(ssl, cmd, larg, parg); |
451 | 5.72k | break; |
452 | 5.72k | } |
453 | 5.72k | return ret; |
454 | 5.72k | } |
455 | | |
456 | | static void dtls1_bio_set_next_timeout(BIO *bio, const DTLS1_STATE *d1) |
457 | 95.9k | { |
458 | 95.9k | struct timeval tv = ossl_time_to_timeval(d1->next_timeout); |
459 | | |
460 | 95.9k | BIO_ctrl(bio, BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, &tv); |
461 | 95.9k | } |
462 | | |
463 | | void dtls1_start_timer(SSL_CONNECTION *s) |
464 | 76.7k | { |
465 | 76.7k | OSSL_TIME duration; |
466 | 76.7k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
467 | | |
468 | | #ifndef OPENSSL_NO_SCTP |
469 | | /* Disable timer for SCTP */ |
470 | | if (SSL_get_wbio(ssl) != NULL && BIO_dgram_is_sctp(SSL_get_wbio(ssl))) { |
471 | | s->d1->next_timeout = ossl_time_zero(); |
472 | | return; |
473 | | } |
474 | | #endif |
475 | | |
476 | | /* |
477 | | * If timer is not set, initialize duration with 1 second or |
478 | | * a user-specified value if the timer callback is installed. |
479 | | */ |
480 | 76.7k | if (ossl_time_is_zero(s->d1->next_timeout)) { |
481 | 41.3k | if (s->d1->timer_cb != NULL) |
482 | 0 | s->d1->timeout_duration_us = s->d1->timer_cb(ssl, 0); |
483 | 41.3k | else |
484 | 41.3k | s->d1->timeout_duration_us = 1000000; |
485 | 41.3k | } |
486 | | |
487 | | /* Set timeout to current time plus duration */ |
488 | 76.7k | duration = ossl_us2time(s->d1->timeout_duration_us); |
489 | 76.7k | s->d1->next_timeout = ossl_time_add(ossl_time_now(), duration); |
490 | | |
491 | | /* set s->d1->next_timeout into ssl->rbio interface */ |
492 | 76.7k | dtls1_bio_set_next_timeout(SSL_get_rbio(ssl), s->d1); |
493 | 76.7k | } |
494 | | |
495 | | int dtls1_get_timeout(const SSL_CONNECTION *s, OSSL_TIME *timeleft) |
496 | 264k | { |
497 | 264k | OSSL_TIME timenow; |
498 | | |
499 | | /* If no timeout is set, just return NULL */ |
500 | 264k | if (ossl_time_is_zero(s->d1->next_timeout)) |
501 | 55.7k | return 0; |
502 | | |
503 | | /* Get current time */ |
504 | 209k | timenow = ossl_time_now(); |
505 | | |
506 | | /* |
507 | | * If timer already expired or if remaining time is less than 15 ms, |
508 | | * set it to 0 to prevent issues because of small divergences with |
509 | | * socket timeouts. |
510 | | */ |
511 | 209k | *timeleft = ossl_time_subtract(s->d1->next_timeout, timenow); |
512 | 209k | if (ossl_time_compare(*timeleft, ossl_ms2time(15)) <= 0) |
513 | 0 | *timeleft = ossl_time_zero(); |
514 | 209k | return 1; |
515 | 264k | } |
516 | | |
517 | | int dtls1_is_timer_expired(SSL_CONNECTION *s) |
518 | 264k | { |
519 | 264k | OSSL_TIME timeleft; |
520 | | |
521 | | /* Get time left until timeout, return false if no timer running */ |
522 | 264k | if (!dtls1_get_timeout(s, &timeleft)) |
523 | 55.7k | return 0; |
524 | | |
525 | | /* Return false if timer is not expired yet */ |
526 | 209k | if (!ossl_time_is_zero(timeleft)) |
527 | 209k | return 0; |
528 | | |
529 | | /* Timer expired, so return true */ |
530 | 0 | return 1; |
531 | 209k | } |
532 | | |
533 | | static void dtls1_double_timeout(SSL_CONNECTION *s) |
534 | 0 | { |
535 | 0 | s->d1->timeout_duration_us *= 2; |
536 | 0 | if (s->d1->timeout_duration_us > 60000000) |
537 | 0 | s->d1->timeout_duration_us = 60000000; |
538 | 0 | } |
539 | | |
540 | | void dtls1_stop_timer(SSL_CONNECTION *s) |
541 | 19.2k | { |
542 | | /* Reset everything */ |
543 | 19.2k | s->d1->timeout_num_alerts = 0; |
544 | 19.2k | s->d1->next_timeout = ossl_time_zero(); |
545 | 19.2k | s->d1->timeout_duration_us = 1000000; |
546 | 19.2k | dtls1_bio_set_next_timeout(s->rbio, s->d1); |
547 | | /* Clear retransmission buffer */ |
548 | 19.2k | dtls1_clear_sent_buffer(s, 0); |
549 | 19.2k | } |
550 | | |
551 | | int dtls1_check_timeout_num(SSL_CONNECTION *s) |
552 | 0 | { |
553 | 0 | size_t mtu; |
554 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
555 | |
|
556 | 0 | s->d1->timeout_num_alerts++; |
557 | | |
558 | | /* Reduce MTU after 2 unsuccessful retransmissions */ |
559 | 0 | if (s->d1->timeout_num_alerts > 2 |
560 | 0 | && !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) { |
561 | 0 | mtu = BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, NULL); |
562 | 0 | if (mtu < s->d1->mtu) |
563 | 0 | s->d1->mtu = mtu; |
564 | 0 | } |
565 | |
|
566 | 0 | if (s->d1->timeout_num_alerts > DTLS1_TMO_ALERT_COUNT) { |
567 | | /* fail the connection, enough alerts have been sent */ |
568 | 0 | SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_READ_TIMEOUT_EXPIRED); |
569 | 0 | return -1; |
570 | 0 | } |
571 | | |
572 | 0 | return 0; |
573 | 0 | } |
574 | | |
575 | | int dtls1_handle_timeout(SSL_CONNECTION *s) |
576 | 244k | { |
577 | | /* if no timer is expired, don't do anything */ |
578 | 244k | if (!dtls1_is_timer_expired(s)) { |
579 | 244k | return 0; |
580 | 244k | } |
581 | | |
582 | 0 | if (s->d1->timer_cb != NULL) |
583 | 0 | s->d1->timeout_duration_us = s->d1->timer_cb(SSL_CONNECTION_GET_USER_SSL(s), |
584 | 0 | s->d1->timeout_duration_us); |
585 | 0 | else |
586 | 0 | dtls1_double_timeout(s); |
587 | |
|
588 | 0 | if (dtls1_check_timeout_num(s) < 0) { |
589 | | /* |
590 | | * SSLfatal() already called, so the connection is finished. Stop the |
591 | | * timer rather than returning with next_timeout left in the past: |
592 | | * nothing will re-arm or clear it from here, so DTLSv1_get_timeout() |
593 | | * would report "due now" for ever and spin any caller which waits on |
594 | | * it. |
595 | | */ |
596 | 0 | dtls1_stop_timer(s); |
597 | 0 | return -1; |
598 | 0 | } |
599 | | |
600 | 0 | dtls1_start_timer(s); |
601 | | /* Calls SSLfatal() if required */ |
602 | 0 | return dtls1_retransmit_sent_messages(s); |
603 | 0 | } |
604 | | |
605 | 0 | #define LISTEN_SUCCESS 2 |
606 | 0 | #define LISTEN_SEND_VERIFY_REQUEST 1 |
607 | | |
608 | | #ifndef OPENSSL_NO_SOCK |
609 | | int DTLSv1_listen(SSL *ssl, BIO_ADDR *client) |
610 | 0 | { |
611 | 0 | int next, n, ret = 0; |
612 | 0 | unsigned char cookie[DTLS1_COOKIE_LENGTH]; |
613 | 0 | unsigned char seq[SEQ_NUM_SIZE]; |
614 | 0 | const unsigned char *data; |
615 | 0 | unsigned char *buf = NULL, *wbuf; |
616 | 0 | size_t fragoff, fraglen, msglen; |
617 | 0 | uint64_t record_sequence = 0; |
618 | 0 | unsigned int rectype, versmajor, versminor, msgseq, msgtype, clientvers, cookielen; |
619 | 0 | BIO *rbio, *wbio; |
620 | 0 | BIO_ADDR *tmpclient = NULL; |
621 | 0 | PACKET pkt, msgpkt, msgpayload, session, cookiepkt; |
622 | 0 | SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
623 | |
|
624 | 0 | if (s == NULL) |
625 | 0 | return -1; |
626 | | |
627 | 0 | if (s->handshake_func == NULL) { |
628 | | /* Not properly initialized yet */ |
629 | 0 | SSL_set_accept_state(ssl); |
630 | 0 | } |
631 | | |
632 | | /* Ensure there is no state left over from a previous invocation */ |
633 | 0 | if (!SSL_clear(ssl)) |
634 | 0 | return -1; |
635 | | |
636 | 0 | ERR_clear_error(); |
637 | |
|
638 | 0 | rbio = SSL_get_rbio(ssl); |
639 | 0 | wbio = SSL_get_wbio(ssl); |
640 | |
|
641 | 0 | if (!rbio || !wbio) { |
642 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_BIO_NOT_SET); |
643 | 0 | return -1; |
644 | 0 | } |
645 | | |
646 | | /* |
647 | | * Note: This check deliberately excludes DTLS1_BAD_VER because that version |
648 | | * requires the MAC to be calculated *including* the first ClientHello |
649 | | * (without the cookie). Since DTLSv1_listen is stateless that cannot be |
650 | | * supported. DTLS1_BAD_VER must use cookies in a stateful manner (e.g. via |
651 | | * SSL_accept) |
652 | | */ |
653 | 0 | if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) { |
654 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_SSL_VERSION); |
655 | 0 | return -1; |
656 | 0 | } |
657 | | |
658 | | /* |
659 | | * DTLSv1_listen() only supports the legacy HelloVerifyRequest mechanism |
660 | | * which is not used in DTLS 1.3. For DTLS 1.3, use the SSL_new_listener() |
661 | | * API instead which supports HelloRetryRequest with cookies. |
662 | | * |
663 | | * If the SSL object is configured for DTLS 1.3 only (both min and max |
664 | | * are set to DTLS 1.3), we must fail since there's no room to downgrade. |
665 | | * Otherwise, if max allows DTLS 1.3, we clamp it down to DTLS 1.2 so |
666 | | * that the handshake will use HelloVerifyRequest. |
667 | | */ |
668 | 0 | if (SSL_CONNECTION_IS_DTLS(s)) { |
669 | 0 | int min_version = s->min_proto_version; |
670 | 0 | int max_version = s->max_proto_version; |
671 | | |
672 | | /* |
673 | | * Check if configured for DTLS 1.3 only - this is not supported. |
674 | | * min_proto_version of 0 means "use default" which includes older versions, |
675 | | * so only fail if min is explicitly set to DTLS 1.3. |
676 | | */ |
677 | 0 | if (min_version == DTLS1_3_VERSION |
678 | 0 | && (max_version == 0 || max_version == DTLS1_3_VERSION)) { |
679 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_SSL_VERSION); |
680 | 0 | return -1; |
681 | 0 | } |
682 | | |
683 | | /* max_proto_version of 0 means "use default" which could include 1.3 */ |
684 | 0 | if (max_version == 0 || DTLS_VERSION_GE(max_version, DTLS1_3_VERSION)) { |
685 | 0 | if (!SSL_set_max_proto_version(ssl, DTLS1_2_VERSION)) { |
686 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_SSL_VERSION); |
687 | 0 | return -1; |
688 | 0 | } |
689 | 0 | } |
690 | 0 | } |
691 | | |
692 | 0 | buf = OPENSSL_malloc(DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_PLAIN_LENGTH); |
693 | 0 | if (buf == NULL) |
694 | 0 | return -1; |
695 | 0 | wbuf = OPENSSL_malloc(DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_PLAIN_LENGTH); |
696 | 0 | if (wbuf == NULL) { |
697 | 0 | OPENSSL_free(buf); |
698 | 0 | return -1; |
699 | 0 | } |
700 | | |
701 | 0 | do { |
702 | | /* Get a packet */ |
703 | |
|
704 | 0 | clear_sys_error(); |
705 | 0 | n = BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH + DTLS1_RT_HEADER_LENGTH); |
706 | 0 | if (n <= 0) { |
707 | 0 | if (BIO_should_retry(rbio)) { |
708 | | /* Non-blocking IO */ |
709 | 0 | goto end; |
710 | 0 | } |
711 | 0 | ret = -1; |
712 | 0 | goto end; |
713 | 0 | } |
714 | | |
715 | 0 | if (!PACKET_buf_init(&pkt, buf, n)) { |
716 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR); |
717 | 0 | ret = -1; |
718 | 0 | goto end; |
719 | 0 | } |
720 | | |
721 | | /* |
722 | | * Parse the received record. If there are any problems with it we just |
723 | | * dump it - with no alert. RFC6347 says this "Unlike TLS, DTLS is |
724 | | * resilient in the face of invalid records (e.g., invalid formatting, |
725 | | * length, MAC, etc.). In general, invalid records SHOULD be silently |
726 | | * discarded, thus preserving the association; however, an error MAY be |
727 | | * logged for diagnostic purposes." |
728 | | */ |
729 | | |
730 | | /* this packet contained a partial record, dump it */ |
731 | 0 | if (n < DTLS1_RT_HEADER_LENGTH) { |
732 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_RECORD_TOO_SMALL); |
733 | 0 | goto end; |
734 | 0 | } |
735 | | |
736 | | /* Get the record header */ |
737 | 0 | if (!PACKET_get_1(&pkt, &rectype) |
738 | 0 | || !PACKET_get_1(&pkt, &versmajor) |
739 | 0 | || !PACKET_get_1(&pkt, &versminor)) { |
740 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_LENGTH_MISMATCH); |
741 | 0 | goto end; |
742 | 0 | } |
743 | | |
744 | 0 | if (s->msg_callback) |
745 | 0 | s->msg_callback(0, (versmajor << 8) | versminor, SSL3_RT_HEADER, buf, |
746 | 0 | DTLS1_RT_HEADER_LENGTH, ssl, s->msg_callback_arg); |
747 | |
|
748 | 0 | if (rectype != SSL3_RT_HANDSHAKE) { |
749 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_UNEXPECTED_MESSAGE); |
750 | 0 | goto end; |
751 | 0 | } |
752 | | |
753 | | /* |
754 | | * Check record version number. We only check that the major version is |
755 | | * the same. |
756 | | */ |
757 | 0 | if (versmajor != DTLS1_VERSION_MAJOR) { |
758 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_BAD_PROTOCOL_VERSION_NUMBER); |
759 | 0 | goto end; |
760 | 0 | } |
761 | | |
762 | | /* Save the sequence number: 64 bits, with top 2 bytes = epoch */ |
763 | 0 | if (!PACKET_copy_bytes(&pkt, seq, SEQ_NUM_SIZE) |
764 | 0 | || !PACKET_get_length_prefixed_2(&pkt, &msgpkt)) { |
765 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_LENGTH_MISMATCH); |
766 | 0 | goto end; |
767 | 0 | } |
768 | | /* |
769 | | * We allow data remaining at the end of the packet because there could |
770 | | * be a second record (but we ignore it) |
771 | | */ |
772 | | |
773 | | /* This is an initial ClientHello so the epoch has to be 0 */ |
774 | 0 | if (seq[0] != 0 || seq[1] != 0) { |
775 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_UNEXPECTED_MESSAGE); |
776 | 0 | goto end; |
777 | 0 | } |
778 | 0 | record_sequence = ((uint64_t)seq[2]) << 40; |
779 | 0 | record_sequence |= ((uint64_t)seq[3]) << 32; |
780 | 0 | record_sequence |= ((uint64_t)seq[4]) << 24; |
781 | 0 | record_sequence |= ((uint64_t)seq[5]) << 16; |
782 | 0 | record_sequence |= ((uint64_t)seq[6]) << 8; |
783 | 0 | record_sequence |= ((uint64_t)seq[7]); |
784 | | |
785 | | /* Get a pointer to the raw message for the later callback */ |
786 | 0 | data = PACKET_data(&msgpkt); |
787 | | |
788 | | /* Finished processing the record header, now process the message */ |
789 | 0 | if (!PACKET_get_1(&msgpkt, &msgtype) |
790 | 0 | || !PACKET_get_net_3_len(&msgpkt, &msglen) |
791 | 0 | || !PACKET_get_net_2(&msgpkt, &msgseq) |
792 | 0 | || !PACKET_get_net_3_len(&msgpkt, &fragoff) |
793 | 0 | || !PACKET_get_net_3_len(&msgpkt, &fraglen) |
794 | 0 | || !PACKET_get_sub_packet(&msgpkt, &msgpayload, fraglen) |
795 | 0 | || PACKET_remaining(&msgpkt) != 0) { |
796 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_LENGTH_MISMATCH); |
797 | 0 | goto end; |
798 | 0 | } |
799 | | |
800 | 0 | if (msgtype != SSL3_MT_CLIENT_HELLO) { |
801 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_UNEXPECTED_MESSAGE); |
802 | 0 | goto end; |
803 | 0 | } |
804 | | |
805 | | /* Message sequence number can only be 0 or 1 */ |
806 | 0 | if (msgseq > 1) { |
807 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SEQUENCE_NUMBER); |
808 | 0 | goto end; |
809 | 0 | } |
810 | | |
811 | | /* |
812 | | * We don't support fragment reassembly for ClientHellos whilst |
813 | | * listening because that would require server side state (which is |
814 | | * against the whole point of the ClientHello/HelloVerifyRequest |
815 | | * mechanism). Instead we only look at the first ClientHello fragment |
816 | | * and require that the cookie must be contained within it. |
817 | | */ |
818 | 0 | if (fragoff != 0 || fraglen > msglen) { |
819 | | /* Non initial ClientHello fragment (or bad fragment) */ |
820 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_FRAGMENTED_CLIENT_HELLO); |
821 | 0 | goto end; |
822 | 0 | } |
823 | | |
824 | 0 | if (s->msg_callback) |
825 | 0 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, data, |
826 | 0 | fraglen + DTLS1_HM_HEADER_LENGTH, ssl, |
827 | 0 | s->msg_callback_arg); |
828 | |
|
829 | 0 | if (!PACKET_get_net_2(&msgpayload, &clientvers)) { |
830 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_LENGTH_MISMATCH); |
831 | 0 | goto end; |
832 | 0 | } |
833 | | |
834 | | /* |
835 | | * Verify client version is supported |
836 | | */ |
837 | 0 | if (DTLS_VERSION_LT(clientvers, (unsigned int)ssl->method->version) && ssl->method->version != DTLS_ANY_VERSION) { |
838 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_VERSION_NUMBER); |
839 | 0 | goto end; |
840 | 0 | } |
841 | | |
842 | 0 | if (!PACKET_forward(&msgpayload, SSL3_RANDOM_SIZE) |
843 | 0 | || !PACKET_get_length_prefixed_1(&msgpayload, &session) |
844 | 0 | || !PACKET_get_length_prefixed_1(&msgpayload, &cookiepkt)) { |
845 | | /* |
846 | | * Could be malformed or the cookie does not fit within the initial |
847 | | * ClientHello fragment. Either way we can't handle it. |
848 | | */ |
849 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_LENGTH_MISMATCH); |
850 | 0 | goto end; |
851 | 0 | } |
852 | | |
853 | | /* |
854 | | * Check if we have a cookie or not. If not we need to send a |
855 | | * HelloVerifyRequest. |
856 | | */ |
857 | 0 | if (PACKET_remaining(&cookiepkt) == 0) { |
858 | 0 | next = LISTEN_SEND_VERIFY_REQUEST; |
859 | 0 | } else { |
860 | | /* |
861 | | * We have a cookie, so lets check it. |
862 | | */ |
863 | 0 | if (ssl->ctx->app_verify_cookie_cb == NULL) { |
864 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_NO_VERIFY_COOKIE_CALLBACK); |
865 | | /* This is fatal */ |
866 | 0 | ret = -1; |
867 | 0 | goto end; |
868 | 0 | } |
869 | 0 | if (ssl->ctx->app_verify_cookie_cb(ssl, PACKET_data(&cookiepkt), |
870 | 0 | (unsigned int)PACKET_remaining(&cookiepkt)) |
871 | 0 | == 0) { |
872 | | /* |
873 | | * We treat invalid cookies in the same was as no cookie as |
874 | | * per RFC6347 |
875 | | */ |
876 | 0 | next = LISTEN_SEND_VERIFY_REQUEST; |
877 | 0 | } else { |
878 | | /* Cookie verification succeeded */ |
879 | 0 | next = LISTEN_SUCCESS; |
880 | 0 | } |
881 | 0 | } |
882 | | |
883 | 0 | if (next == LISTEN_SEND_VERIFY_REQUEST) { |
884 | 0 | WPACKET wpkt; |
885 | 0 | unsigned int version; |
886 | 0 | size_t wreclen; |
887 | | |
888 | | /* |
889 | | * There was no cookie in the ClientHello so we need to send a |
890 | | * HelloVerifyRequest. If this fails we do not worry about trying |
891 | | * to resend, we just drop it. |
892 | | */ |
893 | | |
894 | | /* Generate the cookie */ |
895 | 0 | if (ssl->ctx->app_gen_cookie_cb == NULL || ssl->ctx->app_gen_cookie_cb(ssl, cookie, &cookielen) == 0 || cookielen > 255) { |
896 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_COOKIE_GEN_CALLBACK_FAILURE); |
897 | | /* This is fatal */ |
898 | 0 | ret = -1; |
899 | 0 | goto end; |
900 | 0 | } |
901 | | |
902 | | /* |
903 | | * Special case: for hello verify request, client version 1.0 and we |
904 | | * haven't decided which version to use yet send back using version |
905 | | * 1.0 header: otherwise some clients will ignore it. |
906 | | */ |
907 | 0 | version = (ssl->method->version == DTLS_ANY_VERSION) ? DTLS1_VERSION |
908 | 0 | : s->version; |
909 | | |
910 | | /* Construct the record and message headers */ |
911 | 0 | if (!WPACKET_init_static_len(&wpkt, |
912 | 0 | wbuf, |
913 | 0 | ssl_get_max_send_fragment(s) |
914 | 0 | + DTLS1_RT_HEADER_LENGTH, |
915 | 0 | 0) |
916 | 0 | || !WPACKET_put_bytes_u8(&wpkt, SSL3_RT_HANDSHAKE) |
917 | 0 | || !WPACKET_put_bytes_u16(&wpkt, version) |
918 | | /* |
919 | | * Record sequence number is always the same as in the |
920 | | * received ClientHello |
921 | | */ |
922 | 0 | || !WPACKET_memcpy(&wpkt, seq, SEQ_NUM_SIZE) |
923 | | /* End of record, start sub packet for message */ |
924 | 0 | || !WPACKET_start_sub_packet_u16(&wpkt) |
925 | | /* Message type */ |
926 | 0 | || !WPACKET_put_bytes_u8(&wpkt, |
927 | 0 | DTLS1_MT_HELLO_VERIFY_REQUEST) |
928 | | /* |
929 | | * Message length - doesn't follow normal TLS convention: |
930 | | * the length isn't the last thing in the message header. |
931 | | * We'll need to fill this in later when we know the |
932 | | * length. Set it to zero for now |
933 | | */ |
934 | 0 | || !WPACKET_put_bytes_u24(&wpkt, 0) |
935 | | /* |
936 | | * Message sequence number is always 0 for a |
937 | | * HelloVerifyRequest |
938 | | */ |
939 | 0 | || !WPACKET_put_bytes_u16(&wpkt, 0) |
940 | | /* |
941 | | * We never fragment a HelloVerifyRequest, so fragment |
942 | | * offset is 0 |
943 | | */ |
944 | 0 | || !WPACKET_put_bytes_u24(&wpkt, 0) |
945 | | /* |
946 | | * Fragment length is the same as message length, but |
947 | | * this *is* the last thing in the message header so we |
948 | | * can just start a sub-packet. No need to come back |
949 | | * later for this one. |
950 | | */ |
951 | 0 | || !WPACKET_start_sub_packet_u24(&wpkt) |
952 | | /* Create the actual HelloVerifyRequest body */ |
953 | 0 | || !dtls_raw_hello_verify_request(&wpkt, cookie, cookielen) |
954 | | /* Close message body */ |
955 | 0 | || !WPACKET_close(&wpkt) |
956 | | /* Close record body */ |
957 | 0 | || !WPACKET_close(&wpkt) |
958 | 0 | || !WPACKET_get_total_written(&wpkt, &wreclen) |
959 | 0 | || !WPACKET_finish(&wpkt)) { |
960 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR); |
961 | 0 | WPACKET_cleanup(&wpkt); |
962 | | /* This is fatal */ |
963 | 0 | ret = -1; |
964 | 0 | goto end; |
965 | 0 | } |
966 | | |
967 | | /* |
968 | | * Fix up the message len in the message header. Its the same as the |
969 | | * fragment len which has been filled in by WPACKET, so just copy |
970 | | * that. Destination for the message len is after the record header |
971 | | * plus one byte for the message content type. The source is the |
972 | | * last 3 bytes of the message header |
973 | | */ |
974 | 0 | memcpy(&wbuf[DTLS1_RT_HEADER_LENGTH + 1], |
975 | 0 | &wbuf[DTLS1_RT_HEADER_LENGTH + DTLS1_HM_HEADER_LENGTH - 3], |
976 | 0 | 3); |
977 | |
|
978 | 0 | if (s->msg_callback) { |
979 | | /* Report the outgoing DTLS record header */ |
980 | 0 | s->msg_callback(1, (int)version, SSL3_RT_HEADER, |
981 | 0 | wbuf, DTLS1_RT_HEADER_LENGTH, |
982 | 0 | ssl, s->msg_callback_arg); |
983 | | /* Report the HelloVerifyRequest handshake message */ |
984 | 0 | s->msg_callback(1, (int)version, SSL3_RT_HANDSHAKE, |
985 | 0 | wbuf + DTLS1_RT_HEADER_LENGTH, |
986 | 0 | wreclen - DTLS1_RT_HEADER_LENGTH, |
987 | 0 | ssl, s->msg_callback_arg); |
988 | 0 | } |
989 | |
|
990 | 0 | if ((tmpclient = BIO_ADDR_new()) == NULL) { |
991 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_BIO_LIB); |
992 | 0 | goto end; |
993 | 0 | } |
994 | | |
995 | | /* |
996 | | * This is unnecessary if rbio and wbio are one and the same - but |
997 | | * maybe they're not. We ignore errors here - some BIOs do not |
998 | | * support this. |
999 | | */ |
1000 | 0 | if (BIO_dgram_get_peer(rbio, tmpclient) > 0) { |
1001 | 0 | (void)BIO_dgram_set_peer(wbio, tmpclient); |
1002 | 0 | } |
1003 | 0 | BIO_ADDR_free(tmpclient); |
1004 | 0 | tmpclient = NULL; |
1005 | |
|
1006 | 0 | if (BIO_write(wbio, wbuf, (int)wreclen) < (int)wreclen) { |
1007 | 0 | if (BIO_should_retry(wbio)) { |
1008 | | /* |
1009 | | * Non-blocking IO...but we're stateless, so we're just |
1010 | | * going to drop this packet. |
1011 | | */ |
1012 | 0 | goto end; |
1013 | 0 | } |
1014 | 0 | ret = -1; |
1015 | 0 | goto end; |
1016 | 0 | } |
1017 | | |
1018 | 0 | if (BIO_flush(wbio) <= 0) { |
1019 | 0 | if (BIO_should_retry(wbio)) { |
1020 | | /* |
1021 | | * Non-blocking IO...but we're stateless, so we're just |
1022 | | * going to drop this packet. |
1023 | | */ |
1024 | 0 | goto end; |
1025 | 0 | } |
1026 | 0 | ret = -1; |
1027 | 0 | goto end; |
1028 | 0 | } |
1029 | 0 | } |
1030 | 0 | } while (next != LISTEN_SUCCESS); |
1031 | | |
1032 | | /* |
1033 | | * Set expected sequence numbers to continue the handshake. |
1034 | | */ |
1035 | 0 | s->d1->handshake_read_seq = 1; |
1036 | 0 | s->d1->handshake_write_seq = 1; |
1037 | 0 | s->d1->next_handshake_write_seq = 1; |
1038 | 0 | if (s->rlayer.wrlmethod->set_sequence == NULL |
1039 | 0 | || !s->rlayer.wrlmethod->set_sequence(s->rlayer.wrl, |
1040 | 0 | record_sequence)) { |
1041 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR); |
1042 | 0 | ret = -1; |
1043 | 0 | goto end; |
1044 | 0 | } |
1045 | | |
1046 | | /* |
1047 | | * We are doing cookie exchange, so make sure we set that option in the |
1048 | | * SSL object |
1049 | | */ |
1050 | 0 | SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE); |
1051 | | |
1052 | | /* |
1053 | | * Tell the state machine that we've done the initial hello verify |
1054 | | * exchange |
1055 | | */ |
1056 | 0 | ossl_statem_set_hello_verify_done(s); |
1057 | | |
1058 | | /* |
1059 | | * Some BIOs may not support this. If we fail we clear the client address |
1060 | | */ |
1061 | 0 | if (BIO_dgram_get_peer(rbio, client) <= 0) |
1062 | 0 | BIO_ADDR_clear(client); |
1063 | | |
1064 | | /* Buffer the record for use by the record layer */ |
1065 | 0 | if (BIO_write(s->rlayer.rrlnext, buf, n) != n) { |
1066 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR); |
1067 | 0 | ret = -1; |
1068 | 0 | goto end; |
1069 | 0 | } |
1070 | | |
1071 | | /* |
1072 | | * Reset the record layer - but this time we can use the record we just |
1073 | | * buffered in s->rlayer.rrlnext |
1074 | | */ |
1075 | 0 | if (!ssl_set_new_record_layer(s, DTLS_ANY_VERSION, |
1076 | 0 | OSSL_RECORD_DIRECTION_READ, |
1077 | 0 | OSSL_RECORD_PROTECTION_LEVEL_NONE, NULL, 0, |
1078 | 0 | NULL, NULL, 0, NULL, 0, NULL, 0, NULL, NULL, |
1079 | 0 | 0, NID_undef, NULL, NULL, NULL)) { |
1080 | | /* SSLfatal already called */ |
1081 | 0 | ret = -1; |
1082 | 0 | goto end; |
1083 | 0 | } |
1084 | | |
1085 | 0 | ret = 1; |
1086 | 0 | end: |
1087 | 0 | BIO_ADDR_free(tmpclient); |
1088 | 0 | OPENSSL_free(buf); |
1089 | 0 | OPENSSL_free(wbuf); |
1090 | 0 | return ret; |
1091 | 0 | } |
1092 | | #endif |
1093 | | |
1094 | | static int dtls1_handshake_write(SSL_CONNECTION *s) |
1095 | 70.9k | { |
1096 | 70.9k | return dtls1_do_write(s, SSL3_RT_HANDSHAKE); |
1097 | 70.9k | } |
1098 | | |
1099 | | int dtls1_shutdown(SSL *s) |
1100 | 0 | { |
1101 | 0 | int ret; |
1102 | | #ifndef OPENSSL_NO_SCTP |
1103 | | BIO *wbio; |
1104 | | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s); |
1105 | | |
1106 | | if (sc == NULL) |
1107 | | return -1; |
1108 | | |
1109 | | wbio = SSL_get_wbio(s); |
1110 | | if (wbio != NULL && BIO_dgram_is_sctp(wbio) && !(sc->shutdown & SSL_SENT_SHUTDOWN)) { |
1111 | | ret = BIO_dgram_sctp_wait_for_dry(wbio); |
1112 | | if (ret < 0) |
1113 | | return -1; |
1114 | | |
1115 | | if (ret == 0) |
1116 | | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN, 1, |
1117 | | NULL); |
1118 | | } |
1119 | | #endif |
1120 | 0 | ret = ssl3_shutdown(s); |
1121 | | #ifndef OPENSSL_NO_SCTP |
1122 | | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN, 0, NULL); |
1123 | | #endif |
1124 | 0 | return ret; |
1125 | 0 | } |
1126 | | |
1127 | | int dtls1_query_mtu(SSL_CONNECTION *s) |
1128 | 76.7k | { |
1129 | 76.7k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
1130 | | |
1131 | 76.7k | if (s->d1->link_mtu) { |
1132 | 0 | s->d1->mtu = s->d1->link_mtu - BIO_dgram_get_mtu_overhead(SSL_get_wbio(ssl)); |
1133 | 0 | s->d1->link_mtu = 0; |
1134 | 0 | } |
1135 | | |
1136 | | /* AHA! Figure out the MTU, and stick to the right size */ |
1137 | 76.7k | if (s->d1->mtu < dtls1_min_mtu(s)) { |
1138 | 32.7k | if (!(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) { |
1139 | 32.7k | s->d1->mtu = BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL); |
1140 | | |
1141 | | /* |
1142 | | * I've seen the kernel return bogus numbers when it doesn't know |
1143 | | * (initial write), so just make sure we have a reasonable number |
1144 | | */ |
1145 | 32.7k | if (s->d1->mtu < dtls1_min_mtu(s)) { |
1146 | | /* Set to min mtu */ |
1147 | 32.7k | s->d1->mtu = dtls1_min_mtu(s); |
1148 | 32.7k | BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SET_MTU, |
1149 | 32.7k | (long)s->d1->mtu, NULL); |
1150 | 32.7k | } |
1151 | 32.7k | } else |
1152 | 0 | return 0; |
1153 | 32.7k | } |
1154 | 76.7k | return 1; |
1155 | 76.7k | } |
1156 | | |
1157 | | size_t dtls1_min_mtu(SSL_CONNECTION *s) |
1158 | 218k | { |
1159 | 218k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
1160 | | |
1161 | 218k | return dtls1_link_min_mtu - BIO_dgram_get_mtu_overhead(SSL_get_wbio(ssl)); |
1162 | 218k | } |
1163 | | |
1164 | | size_t DTLS_get_data_mtu(const SSL *ssl) |
1165 | 0 | { |
1166 | 0 | size_t mac_overhead, int_overhead, blocksize, ext_overhead, rechdrlen = 0; |
1167 | 0 | const SSL_CIPHER *ciph = SSL_get_current_cipher(ssl); |
1168 | 0 | size_t mtu; |
1169 | 0 | const SSL_CONNECTION *s = SSL_CONNECTION_FROM_CONST_SSL_ONLY(ssl); |
1170 | |
|
1171 | 0 | if (s == NULL) |
1172 | 0 | return 0; |
1173 | | |
1174 | 0 | mtu = s->d1->mtu; |
1175 | |
|
1176 | 0 | if (ciph == NULL) |
1177 | 0 | return 0; |
1178 | | |
1179 | 0 | if (!ssl_cipher_get_overhead(ciph, SSL_version(ssl), &mac_overhead, |
1180 | 0 | &int_overhead, &blocksize, &ext_overhead)) |
1181 | 0 | return 0; |
1182 | | |
1183 | 0 | if (SSL_READ_ETM(s)) |
1184 | 0 | ext_overhead += mac_overhead; |
1185 | 0 | else |
1186 | 0 | int_overhead += mac_overhead; |
1187 | |
|
1188 | 0 | if (SSL_version(ssl) == DTLS1_3_VERSION) { |
1189 | 0 | switch (SSL_get_state(ssl)) { |
1190 | 0 | case TLS_ST_BEFORE: |
1191 | 0 | case DTLS_ST_CR_HELLO_VERIFY_REQUEST: |
1192 | 0 | case TLS_ST_CR_SRVR_HELLO: |
1193 | 0 | case TLS_ST_CW_CLNT_HELLO: |
1194 | 0 | case TLS_ST_CW_COMP_CERT: |
1195 | 0 | case TLS_ST_CW_KEY_EXCH: |
1196 | 0 | case TLS_ST_SW_HELLO_REQ: |
1197 | 0 | case TLS_ST_SR_CLNT_HELLO: |
1198 | 0 | case DTLS_ST_SW_HELLO_VERIFY_REQUEST: |
1199 | 0 | case TLS_ST_SW_SRVR_HELLO: |
1200 | 0 | case TLS_ST_CR_HELLO_REQ: |
1201 | 0 | rechdrlen = DTLS1_RT_HEADER_LENGTH; |
1202 | 0 | break; |
1203 | 0 | default: |
1204 | 0 | rechdrlen = DTLS13_UNI_HDR_FIXED_LENGTH; |
1205 | 0 | break; |
1206 | 0 | } |
1207 | | |
1208 | | /* Added record type at the end of the data */ |
1209 | 0 | int_overhead++; |
1210 | 0 | } else { |
1211 | 0 | rechdrlen = DTLS1_RT_HEADER_LENGTH; |
1212 | 0 | } |
1213 | | |
1214 | | /* Subtract external overhead (e.g. IV/nonce, separate MAC) */ |
1215 | 0 | if (ext_overhead + rechdrlen >= mtu) |
1216 | 0 | return 0; |
1217 | 0 | mtu -= ext_overhead + rechdrlen; |
1218 | | |
1219 | | /* Round encrypted payload down to cipher block size (for CBC etc.) |
1220 | | * No check for overflow since 'mtu % blocksize' cannot exceed mtu. */ |
1221 | 0 | if (blocksize) |
1222 | 0 | mtu -= (mtu % blocksize); |
1223 | | |
1224 | | /* Subtract internal overhead (e.g. CBC padding len byte) */ |
1225 | 0 | if (int_overhead >= mtu) |
1226 | 0 | return 0; |
1227 | 0 | mtu -= int_overhead; |
1228 | |
|
1229 | 0 | return mtu; |
1230 | 0 | } |
1231 | | |
1232 | | void DTLS_set_timer_cb(SSL *ssl, DTLS_timer_cb cb) |
1233 | 0 | { |
1234 | 0 | SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
1235 | |
|
1236 | 0 | if (s == NULL) |
1237 | 0 | return; |
1238 | | |
1239 | 0 | s->d1->timer_cb = cb; |
1240 | 0 | } |
1241 | | |
1242 | | #if !defined(OPENSSL_NO_DTLS) && !defined(OPENSSL_NO_SOCK) |
1243 | | /* |
1244 | | * dtls_listener_connection_free - free an SSL connection owned by the listener. |
1245 | | * |
1246 | | * This function is used to free SSL connections that are in the listener's |
1247 | | * pending_conns or incoming_connections queues. These connections are owned |
1248 | | * by the listener, NOT by the application. |
1249 | | * |
1250 | | * Connections in pending_conns and incoming_connections do NOT hold a reference |
1251 | | * to the listener, even though sc->d1->listener points to it. This is intentional: |
1252 | | * if these connections held a reference to the listener, the listener's reference |
1253 | | * count would never reach zero, and ossl_dtls_listener_free() would never be |
1254 | | * called to clean up the pending/incoming connections - creating a circular |
1255 | | * dependency. |
1256 | | * |
1257 | | * Only when a connection is returned to the application via SSL_accept_connection() |
1258 | | * does it take a reference on the listener. At that point, ownership transfers |
1259 | | * to the application, and the normal SSL_free() path is used. |
1260 | | * |
1261 | | * The assert on ssl->references == 1 ensures that nobody else has taken a |
1262 | | * reference to this connection while it was in the listener's queues. If |
1263 | | * this assert fires, something has gone wrong with ownership tracking. |
1264 | | */ |
1265 | | static void dtls_listener_connection_free(SSL *ssl) |
1266 | 0 | { |
1267 | 0 | SSL_CONNECTION *sc; |
1268 | |
|
1269 | 0 | if (ssl == NULL) |
1270 | 0 | return; |
1271 | | |
1272 | 0 | sc = SSL_CONNECTION_FROM_SSL(ssl); |
1273 | |
|
1274 | 0 | if (sc != NULL && sc->d1 != NULL) { |
1275 | | /* |
1276 | | * Clear listener reference to prevent dtls1_free() from calling |
1277 | | * SSL_free() on the listener. The connection does not own the listener |
1278 | | * and SSL_free must not free the listener |
1279 | | */ |
1280 | 0 | sc->d1->listener = NULL; |
1281 | 0 | } |
1282 | 0 | SSL_free(ssl); |
1283 | 0 | } |
1284 | | |
1285 | | /* |
1286 | | * dtls_listener_create_conn_ssl - create an SSL object for a new connection. |
1287 | | * |
1288 | | * Creates and initializes an SSL object for handling a new incoming |
1289 | | * connection. Sets up the DTLS_RX for URXE-based packet injection, with |
1290 | | * the write BIO connected to the listener's network BIO. |
1291 | | * |
1292 | | * Returns: new SSL object on success, NULL on failure |
1293 | | */ |
1294 | | static SSL *dtls_listener_create_conn_ssl(DTLS_LISTENER *dl, |
1295 | | const BIO_ADDR *peer) |
1296 | 0 | { |
1297 | 0 | SSL *ssl = NULL; |
1298 | 0 | SSL_CONNECTION *sc = NULL; |
1299 | 0 | BIO *wbio = NULL; |
1300 | |
|
1301 | 0 | ssl = SSL_new(dl->ssl.ctx); |
1302 | 0 | if (ssl == NULL) |
1303 | 0 | goto err; |
1304 | | |
1305 | 0 | sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
1306 | 0 | if (sc == NULL || sc->d1 == NULL) |
1307 | 0 | goto err; |
1308 | | |
1309 | 0 | SSL_set_accept_state(ssl); |
1310 | | |
1311 | | /* |
1312 | | * Create DTLS_RX for this connection. The demux is owned by the listener |
1313 | | * and will outlive this connection. DTLS_RX manages the URXE queue for |
1314 | | * incoming packets. |
1315 | | */ |
1316 | 0 | sc->d1->rx = ossl_dtls_rx_new(dl->demux); |
1317 | 0 | if (sc->d1->rx == NULL) |
1318 | 0 | goto err; |
1319 | | |
1320 | | /* |
1321 | | * Update the read record layer to use the URXE queue if it already exists. |
1322 | | * This is needed because the record layer may have been created before |
1323 | | * sc->d1->rx was set, similar to how SSL_set1_initial_peer_addr() updates |
1324 | | * the peer address on existing record layers. |
1325 | | */ |
1326 | 0 | if (sc->rlayer.rrlmethod != NULL && sc->rlayer.rrl != NULL |
1327 | 0 | && sc->rlayer.rrlmethod->set_use_urxe != NULL) |
1328 | 0 | sc->rlayer.rrlmethod->set_use_urxe(sc->rlayer.rrl, 1); |
1329 | | |
1330 | | /* |
1331 | | * Store reference to parent listener. This allows the connection to |
1332 | | * trigger the listener's demux pump when reading data. |
1333 | | */ |
1334 | 0 | sc->d1->listener = &dl->ssl; |
1335 | | |
1336 | | /* |
1337 | | * Record when this connection was created. This is used to detect and |
1338 | | * clean up stale pending connections that haven't completed their |
1339 | | * handshake within the timeout period. |
1340 | | */ |
1341 | 0 | sc->d1->created_at = dtls_listener_get_time_direct(dl); |
1342 | | |
1343 | | /* |
1344 | | * For writes, use the shared network wbio. The peer address is NOT set |
1345 | | * on the BIO itself (which would affect all connections sharing this BIO). |
1346 | | * Instead, the peer address will be passed to the record layer during |
1347 | | * SSL_do_handshake(), and the record layer will use BIO_sendmmsg() with |
1348 | | * the peer address for each write. |
1349 | | */ |
1350 | 0 | wbio = dl->net_wbio; |
1351 | |
|
1352 | 0 | if (wbio == NULL) { |
1353 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_BIO_NOT_SET); |
1354 | 0 | goto err; |
1355 | 0 | } |
1356 | | |
1357 | 0 | if (!BIO_up_ref(wbio)) |
1358 | 0 | goto err; |
1359 | | |
1360 | 0 | SSL_set0_rbio(ssl, NULL); |
1361 | 0 | SSL_set0_wbio(ssl, wbio); |
1362 | 0 | wbio = NULL; /* ownership transferred */ |
1363 | | |
1364 | | /* |
1365 | | * Store the peer address in the SSL connection. This will be passed to |
1366 | | * the record layer when it is created during SSL_do_handshake(). |
1367 | | */ |
1368 | 0 | if (!SSL_set1_initial_peer_addr(ssl, peer)) |
1369 | 0 | goto err; |
1370 | | |
1371 | | /* |
1372 | | * Enable cookie exchange if required by listener flags. |
1373 | | * This tells the state machine to perform HVR (DTLS 1.2) or |
1374 | | * HRR with cookie (DTLS 1.3) validation. |
1375 | | */ |
1376 | 0 | if (dl->require_hvr_cookie || dl->require_hrr_cookie) |
1377 | 0 | SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE); |
1378 | |
|
1379 | 0 | return ssl; |
1380 | | |
1381 | 0 | err: |
1382 | 0 | dtls_listener_connection_free(ssl); |
1383 | 0 | return NULL; |
1384 | 0 | } |
1385 | | |
1386 | | /* |
1387 | | * dtls_listener_signal_notifier - wake threads blocked on this listener. |
1388 | | * |
1389 | | * Readiness may be produced by the thread which pumps the demux while a |
1390 | | * different thread is blocked in poll() on the network socket. That socket |
1391 | | * will not necessarily become readable again from the blocked thread's point |
1392 | | * of view, so the notifier is used to wake it. |
1393 | | * |
1394 | | * The caller must hold dl->mutex. |
1395 | | */ |
1396 | | static void dtls_listener_signal_notifier(DTLS_LISTENER *dl) |
1397 | 0 | { |
1398 | 0 | if (dl->have_notifier && dl->cur_blocking_waiters > 0 |
1399 | 0 | && !dl->signalled_notifier) { |
1400 | 0 | ossl_rio_notifier_signal(&dl->notifier); |
1401 | 0 | dl->signalled_notifier = 1; |
1402 | 0 | } |
1403 | 0 | } |
1404 | | |
1405 | | /* |
1406 | | * dtls_listener_packet_handler - callback for handling incoming datagrams. |
1407 | | * |
1408 | | * This callback is invoked by the demux for each received datagram. It routes |
1409 | | * the URXE to the appropriate connection based on peer address, creating a |
1410 | | * new pending connection if necessary. |
1411 | | * |
1412 | | * The URXE ownership is transferred to the connection's DTLS_RX queue. |
1413 | | * If routing fails, the URXE is released back to the demux. |
1414 | | */ |
1415 | | static void dtls_listener_packet_handler(DGRAM_URXE *urxe, void *arg) |
1416 | 0 | { |
1417 | 0 | DTLS_LISTENER *dl = arg; |
1418 | 0 | SSL *conn_ssl = NULL; |
1419 | 0 | SSL_CONNECTION *sc = NULL; |
1420 | |
|
1421 | 0 | ossl_crypto_mutex_lock(dl->mutex); |
1422 | | |
1423 | | /* Check established connections first */ |
1424 | 0 | if (dl->established_conns != NULL) |
1425 | 0 | conn_ssl = ossl_dgram_conn_lookup_find(dl->established_conns, urxe); |
1426 | | |
1427 | | /* Check pending connections */ |
1428 | 0 | if (conn_ssl == NULL) |
1429 | 0 | conn_ssl = ossl_dgram_conn_lookup_find(dl->pending_conns, urxe); |
1430 | | |
1431 | | /* Create new pending connection if needed */ |
1432 | 0 | if (conn_ssl == NULL) { |
1433 | | /* |
1434 | | * Reject before allocating anything if we have reached the pending |
1435 | | * connection limit. The LHASH item count is O(1), and this check does |
1436 | | * not need a conn_ssl, so performing it first avoids creating and then |
1437 | | * immediately freeing a connection when we are at capacity. |
1438 | | */ |
1439 | 0 | if (ossl_dgram_conn_lookup_num_items(dl->pending_conns) >= dl->max_pending_conns) |
1440 | 0 | goto release; |
1441 | | |
1442 | 0 | conn_ssl = dtls_listener_create_conn_ssl(dl, &urxe->peer); |
1443 | 0 | if (conn_ssl == NULL) |
1444 | 0 | goto release; |
1445 | | |
1446 | | /* |
1447 | | * Register the connection in pending_conns before running the |
1448 | | * application callback. This is to avoid a race condition where |
1449 | | * another thread grabs the lock and tries to register a connection |
1450 | | * for this address. |
1451 | | */ |
1452 | 0 | if (!ossl_dgram_conn_lookup_register(dl->pending_conns, urxe, conn_ssl)) { |
1453 | 0 | dtls_listener_connection_free(conn_ssl); |
1454 | 0 | goto release; |
1455 | 0 | } |
1456 | | |
1457 | | /* |
1458 | | * Give the application a chance to decorate or veto the new |
1459 | | * pending connection via SSL_CTX_set_new_pending_conn_cb(). |
1460 | | * |
1461 | | * A return value of 0 from the callback means "discard this |
1462 | | * connection". On a non-zero return there is nothing more to do here: |
1463 | | * we already registered the connection above. |
1464 | | */ |
1465 | 0 | if (dl->ssl.ctx->new_pending_conn_cb != NULL) { |
1466 | 0 | int keep; |
1467 | |
|
1468 | 0 | sc = SSL_CONNECTION_FROM_SSL_ONLY(conn_ssl); |
1469 | 0 | if (sc == NULL || sc->d1 == NULL) { |
1470 | 0 | ossl_dgram_conn_lookup_unregister(dl->pending_conns, &urxe->peer); |
1471 | 0 | dtls_listener_connection_free(conn_ssl); |
1472 | 0 | goto release; |
1473 | 0 | } |
1474 | | |
1475 | | /* |
1476 | | * Mark the connection being_driven while the mutex is dropped for |
1477 | | * the callback. This keeps the tick loop away from this connection. |
1478 | | */ |
1479 | 0 | sc->d1->being_driven = 1; |
1480 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
1481 | 0 | keep = dl->ssl.ctx->new_pending_conn_cb(dl->ssl.ctx, conn_ssl, |
1482 | 0 | dl->ssl.ctx->new_pending_conn_arg); |
1483 | 0 | ossl_crypto_mutex_lock(dl->mutex); |
1484 | |
|
1485 | 0 | if (!keep) { |
1486 | | /* |
1487 | | * The pending callback doesn't want this connection, so |
1488 | | * unregister and free it. While the mutex was dropped a |
1489 | | * concurrent handler may have found this same connection and |
1490 | | * injected datagrams into its RX queue; freeing releases them |
1491 | | * back to the demux via ossl_dtls_rx_free(), so nothing leaks. |
1492 | | * being_driven kept the tick away, so the connection still |
1493 | | * holds only its single reference and the free is safe. |
1494 | | */ |
1495 | 0 | ossl_dgram_conn_lookup_unregister(dl->pending_conns, &urxe->peer); |
1496 | 0 | dtls_listener_connection_free(conn_ssl); |
1497 | 0 | goto release; |
1498 | 0 | } |
1499 | | |
1500 | 0 | sc->d1->being_driven = 0; |
1501 | 0 | } |
1502 | 0 | } |
1503 | | |
1504 | 0 | sc = SSL_CONNECTION_FROM_SSL_ONLY(conn_ssl); |
1505 | 0 | if (sc == NULL || sc->d1 == NULL || sc->d1->rx == NULL) |
1506 | 0 | goto release; |
1507 | | |
1508 | | /* Inject packet into connection's URXE queue */ |
1509 | 0 | ossl_dtls_rx_inject_urxe(sc->d1->rx, urxe); |
1510 | | |
1511 | | /* Signal notifier if needed */ |
1512 | 0 | dtls_listener_signal_notifier(dl); |
1513 | |
|
1514 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
1515 | 0 | return; |
1516 | | |
1517 | 0 | release: |
1518 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
1519 | 0 | ossl_dgram_demux_release_urxe(dl->demux, urxe); |
1520 | 0 | } |
1521 | | |
1522 | | /* |
1523 | | * DTLS Listener Internal Cookie Callbacks |
1524 | | * |
1525 | | * These callbacks are used internally by the DTLS listener to generate and |
1526 | | * verify cookies for address validation. They use HMAC-SHA256 with the |
1527 | | * SSL_CTX's cookie_hmac_key to create cookies that bind to the client's |
1528 | | * address. |
1529 | | * |
1530 | | * Cookie format: |
1531 | | * - 8 bytes: timestamp (seconds since epoch) |
1532 | | * - 32 bytes: HMAC-SHA256(timestamp || peer_address) |
1533 | | * |
1534 | | * Total cookie size: 40 bytes |
1535 | | */ |
1536 | 0 | #define DTLS_LISTENER_COOKIE_TIMESTAMP_LEN 8 |
1537 | 0 | #define DTLS_LISTENER_COOKIE_HMAC_LEN 32 |
1538 | 0 | #define DTLS_LISTENER_COOKIE_LEN (DTLS_LISTENER_COOKIE_TIMESTAMP_LEN + DTLS_LISTENER_COOKIE_HMAC_LEN) |
1539 | | |
1540 | | /* Maximum age of a cookie in seconds (default: 60 seconds) */ |
1541 | 0 | #define DTLS_LISTENER_COOKIE_MAX_AGE 60 |
1542 | | |
1543 | | /* |
1544 | | * dtls_listener_get_time - get current time from the listener |
1545 | | * |
1546 | | * Returns the current time using the listener's time callback if set, |
1547 | | * otherwise uses ossl_time_now(). |
1548 | | * |
1549 | | * If ssl is not associated with a listener, returns ossl_time_now(). |
1550 | | */ |
1551 | | static OSSL_TIME dtls_listener_get_time(SSL *ssl) |
1552 | 0 | { |
1553 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
1554 | 0 | DTLS_LISTENER *dl; |
1555 | |
|
1556 | 0 | if (sc == NULL || sc->d1 == NULL || sc->d1->listener == NULL) |
1557 | 0 | return ossl_time_now(); |
1558 | | |
1559 | 0 | dl = (DTLS_LISTENER *)sc->d1->listener; |
1560 | |
|
1561 | 0 | if (dl->now_cb == NULL) |
1562 | 0 | return ossl_time_now(); |
1563 | | |
1564 | 0 | return dl->now_cb(dl->now_cb_arg); |
1565 | 0 | } |
1566 | | |
1567 | | /* |
1568 | | * dtls_listener_get_time_direct - get current time directly from listener |
1569 | | * |
1570 | | * Same as dtls_listener_get_time but takes the listener directly. |
1571 | | * Used during connection creation before listener reference is fully set up. |
1572 | | */ |
1573 | | static OSSL_TIME dtls_listener_get_time_direct(DTLS_LISTENER *dl) |
1574 | 0 | { |
1575 | 0 | if (dl == NULL) |
1576 | 0 | return ossl_time_now(); |
1577 | | |
1578 | 0 | if (dl->now_cb == NULL) |
1579 | 0 | return ossl_time_now(); |
1580 | | |
1581 | 0 | return dl->now_cb(dl->now_cb_arg); |
1582 | 0 | } |
1583 | | |
1584 | | /* |
1585 | | * dtls_listener_cookie_hmac - compute HMAC for cookie validation |
1586 | | * |
1587 | | * Computes HMAC-SHA256(timestamp || port || raw_address) using the |
1588 | | * context's cookie_hmac_key. |
1589 | | * |
1590 | | * Returns 1 on success, 0 on failure. |
1591 | | */ |
1592 | | static int dtls_listener_cookie_hmac(SSL *ssl, uint64_t timestamp, |
1593 | | unsigned char *hmac_out) |
1594 | 0 | { |
1595 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
1596 | 0 | SSL_CTX *ctx; |
1597 | 0 | EVP_MAC_CTX *mctx = NULL; |
1598 | 0 | OSSL_PARAM params[2]; |
1599 | | /* 8 (timestamp) + 2 (port) + max address size */ |
1600 | 0 | unsigned char data[8 + sizeof(uint16_t) + 64]; |
1601 | 0 | unsigned char addr_buf[64]; |
1602 | 0 | size_t data_len = 0; |
1603 | 0 | size_t addr_len = 0; |
1604 | 0 | size_t hmac_len = DTLS_LISTENER_COOKIE_HMAC_LEN; |
1605 | 0 | uint16_t port; |
1606 | 0 | WPACKET pkt; |
1607 | 0 | int ret = 0; |
1608 | |
|
1609 | 0 | if (sc == NULL || sc->d1 == NULL) |
1610 | 0 | return 0; |
1611 | | |
1612 | 0 | ctx = SSL_CONNECTION_GET_CTX(sc); |
1613 | 0 | if (ctx == NULL) |
1614 | 0 | return 0; |
1615 | | |
1616 | | /* Get port and raw address */ |
1617 | 0 | port = BIO_ADDR_rawport(&sc->d1->peer_addr); |
1618 | |
|
1619 | 0 | if (!BIO_ADDR_rawaddress(&sc->d1->peer_addr, addr_buf, &addr_len)) |
1620 | 0 | return 0; |
1621 | | |
1622 | | /* Build data to HMAC: timestamp || port || raw_address */ |
1623 | 0 | if (!WPACKET_init_static_len(&pkt, data, sizeof(data), 0) |
1624 | 0 | || !WPACKET_put_bytes_u64(&pkt, timestamp) |
1625 | 0 | || !WPACKET_put_bytes_u16(&pkt, port) |
1626 | 0 | || !WPACKET_memcpy(&pkt, addr_buf, addr_len) |
1627 | 0 | || !WPACKET_get_total_written(&pkt, &data_len) |
1628 | 0 | || !WPACKET_finish(&pkt)) { |
1629 | 0 | WPACKET_cleanup(&pkt); |
1630 | 0 | return 0; |
1631 | 0 | } |
1632 | | |
1633 | 0 | mctx = EVP_MAC_CTX_new(ctx->hmac); |
1634 | 0 | if (mctx == NULL) |
1635 | 0 | goto err; |
1636 | | |
1637 | 0 | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, |
1638 | 0 | "SHA2-256", 0); |
1639 | 0 | params[1] = OSSL_PARAM_construct_end(); |
1640 | |
|
1641 | 0 | if (!EVP_MAC_init(mctx, ctx->ext.cookie_hmac_key, |
1642 | 0 | sizeof(ctx->ext.cookie_hmac_key), params)) |
1643 | 0 | goto err; |
1644 | | |
1645 | 0 | if (!EVP_MAC_update(mctx, data, data_len)) |
1646 | 0 | goto err; |
1647 | | |
1648 | 0 | if (!EVP_MAC_final(mctx, hmac_out, &hmac_len, hmac_len)) |
1649 | 0 | goto err; |
1650 | | |
1651 | 0 | ret = 1; |
1652 | |
|
1653 | 0 | err: |
1654 | 0 | EVP_MAC_CTX_free(mctx); |
1655 | 0 | return ret; |
1656 | 0 | } |
1657 | | |
1658 | | /* |
1659 | | * ossl_dtls_listener_gen_cookie_cb - internal HVR cookie generate callback |
1660 | | * |
1661 | | * Generates a cookie for HelloVerifyRequest (DTLS 1.2). |
1662 | | * Cookie format: timestamp (8 bytes) || HMAC (32 bytes) |
1663 | | */ |
1664 | | int ossl_dtls_listener_gen_cookie_cb(SSL *ssl, unsigned char *cookie, |
1665 | | unsigned int *cookie_len) |
1666 | 0 | { |
1667 | 0 | uint64_t now = ossl_time2seconds(dtls_listener_get_time(ssl)); |
1668 | | |
1669 | | /* Write timestamp */ |
1670 | 0 | cookie[0] = (unsigned char)(now >> 56); |
1671 | 0 | cookie[1] = (unsigned char)(now >> 48); |
1672 | 0 | cookie[2] = (unsigned char)(now >> 40); |
1673 | 0 | cookie[3] = (unsigned char)(now >> 32); |
1674 | 0 | cookie[4] = (unsigned char)(now >> 24); |
1675 | 0 | cookie[5] = (unsigned char)(now >> 16); |
1676 | 0 | cookie[6] = (unsigned char)(now >> 8); |
1677 | 0 | cookie[7] = (unsigned char)(now); |
1678 | | |
1679 | | /* Compute and append HMAC */ |
1680 | 0 | if (!dtls_listener_cookie_hmac(ssl, now, cookie + DTLS_LISTENER_COOKIE_TIMESTAMP_LEN)) |
1681 | 0 | return 0; |
1682 | | |
1683 | 0 | *cookie_len = DTLS_LISTENER_COOKIE_LEN; |
1684 | 0 | return 1; |
1685 | 0 | } |
1686 | | |
1687 | | /* |
1688 | | * ossl_dtls_listener_verify_cookie_cb - internal HVR cookie verify callback |
1689 | | * |
1690 | | * Verifies a cookie from ClientHello (DTLS 1.2). |
1691 | | * Checks that: |
1692 | | * 1. Cookie length is correct |
1693 | | * 2. Timestamp is not too old |
1694 | | * 3. HMAC matches |
1695 | | */ |
1696 | | int ossl_dtls_listener_verify_cookie_cb(SSL *ssl, const unsigned char *cookie, |
1697 | | unsigned int cookie_len) |
1698 | 0 | { |
1699 | 0 | uint64_t cookie_time, now; |
1700 | 0 | unsigned char expected_hmac[DTLS_LISTENER_COOKIE_HMAC_LEN]; |
1701 | |
|
1702 | 0 | if (cookie_len != DTLS_LISTENER_COOKIE_LEN) |
1703 | 0 | return 0; |
1704 | | |
1705 | | /* Extract timestamp from cookie */ |
1706 | 0 | cookie_time = ((uint64_t)cookie[0] << 56) |
1707 | 0 | | ((uint64_t)cookie[1] << 48) |
1708 | 0 | | ((uint64_t)cookie[2] << 40) |
1709 | 0 | | ((uint64_t)cookie[3] << 32) |
1710 | 0 | | ((uint64_t)cookie[4] << 24) |
1711 | 0 | | ((uint64_t)cookie[5] << 16) |
1712 | 0 | | ((uint64_t)cookie[6] << 8) |
1713 | 0 | | ((uint64_t)cookie[7]); |
1714 | | |
1715 | | /* Check timestamp is not too old */ |
1716 | 0 | now = ossl_time2seconds(dtls_listener_get_time(ssl)); |
1717 | 0 | if (now > cookie_time && (now - cookie_time) > DTLS_LISTENER_COOKIE_MAX_AGE) |
1718 | 0 | return 0; |
1719 | | |
1720 | | /* Compute expected HMAC and compare */ |
1721 | 0 | if (!dtls_listener_cookie_hmac(ssl, cookie_time, expected_hmac)) |
1722 | 0 | return 0; |
1723 | | |
1724 | 0 | if (CRYPTO_memcmp(cookie + DTLS_LISTENER_COOKIE_TIMESTAMP_LEN, |
1725 | 0 | expected_hmac, DTLS_LISTENER_COOKIE_HMAC_LEN) |
1726 | 0 | != 0) |
1727 | 0 | return 0; |
1728 | | |
1729 | 0 | return 1; |
1730 | 0 | } |
1731 | | |
1732 | | /* |
1733 | | * ossl_dtls_listener_gen_stateless_cookie_cb - internal HRR cookie generate callback |
1734 | | * |
1735 | | * Generates a cookie for HelloRetryRequest (DTLS 1.3). |
1736 | | * Uses the same format as the HVR cookie. |
1737 | | */ |
1738 | | int ossl_dtls_listener_gen_stateless_cookie_cb(SSL *ssl, unsigned char *cookie, |
1739 | | size_t *cookie_len) |
1740 | 0 | { |
1741 | 0 | uint64_t now = ossl_time2seconds(dtls_listener_get_time(ssl)); |
1742 | | |
1743 | | /* Write timestamp */ |
1744 | 0 | cookie[0] = (unsigned char)(now >> 56); |
1745 | 0 | cookie[1] = (unsigned char)(now >> 48); |
1746 | 0 | cookie[2] = (unsigned char)(now >> 40); |
1747 | 0 | cookie[3] = (unsigned char)(now >> 32); |
1748 | 0 | cookie[4] = (unsigned char)(now >> 24); |
1749 | 0 | cookie[5] = (unsigned char)(now >> 16); |
1750 | 0 | cookie[6] = (unsigned char)(now >> 8); |
1751 | 0 | cookie[7] = (unsigned char)(now); |
1752 | | |
1753 | | /* Compute and append HMAC */ |
1754 | 0 | if (!dtls_listener_cookie_hmac(ssl, now, cookie + DTLS_LISTENER_COOKIE_TIMESTAMP_LEN)) |
1755 | 0 | return 0; |
1756 | | |
1757 | 0 | *cookie_len = DTLS_LISTENER_COOKIE_LEN; |
1758 | 0 | return 1; |
1759 | 0 | } |
1760 | | |
1761 | | /* |
1762 | | * ossl_dtls_listener_verify_stateless_cookie_cb - internal HRR cookie verify callback |
1763 | | * |
1764 | | * Verifies a cookie from ClientHello (DTLS 1.3). |
1765 | | * Uses the same verification logic as the HVR cookie. |
1766 | | */ |
1767 | | int ossl_dtls_listener_verify_stateless_cookie_cb(SSL *ssl, |
1768 | | const unsigned char *cookie, |
1769 | | size_t cookie_len) |
1770 | 0 | { |
1771 | 0 | uint64_t cookie_time, now; |
1772 | 0 | unsigned char expected_hmac[DTLS_LISTENER_COOKIE_HMAC_LEN]; |
1773 | |
|
1774 | 0 | if (cookie_len != DTLS_LISTENER_COOKIE_LEN) |
1775 | 0 | return 0; |
1776 | | |
1777 | | /* Extract timestamp from cookie */ |
1778 | 0 | cookie_time = ((uint64_t)cookie[0] << 56) |
1779 | 0 | | ((uint64_t)cookie[1] << 48) |
1780 | 0 | | ((uint64_t)cookie[2] << 40) |
1781 | 0 | | ((uint64_t)cookie[3] << 32) |
1782 | 0 | | ((uint64_t)cookie[4] << 24) |
1783 | 0 | | ((uint64_t)cookie[5] << 16) |
1784 | 0 | | ((uint64_t)cookie[6] << 8) |
1785 | 0 | | ((uint64_t)cookie[7]); |
1786 | | |
1787 | | /* Check timestamp is not too old */ |
1788 | 0 | now = ossl_time2seconds(dtls_listener_get_time(ssl)); |
1789 | 0 | if (now > cookie_time && (now - cookie_time) > DTLS_LISTENER_COOKIE_MAX_AGE) |
1790 | 0 | return 0; |
1791 | | |
1792 | | /* Compute expected HMAC and compare */ |
1793 | 0 | if (!dtls_listener_cookie_hmac(ssl, cookie_time, expected_hmac)) |
1794 | 0 | return 0; |
1795 | | |
1796 | 0 | if (CRYPTO_memcmp(cookie + DTLS_LISTENER_COOKIE_TIMESTAMP_LEN, |
1797 | 0 | expected_hmac, DTLS_LISTENER_COOKIE_HMAC_LEN) |
1798 | 0 | != 0) |
1799 | 0 | return 0; |
1800 | | |
1801 | 0 | return 1; |
1802 | 0 | } |
1803 | | |
1804 | | SSL *ossl_dtls_new_listener(SSL_CTX *ctx, uint64_t flags) |
1805 | 0 | { |
1806 | 0 | DTLS_LISTENER *dl = NULL; |
1807 | 0 | int ssl_init_done = 0; |
1808 | |
|
1809 | 0 | if (ctx == NULL) { |
1810 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER); |
1811 | 0 | return NULL; |
1812 | 0 | } |
1813 | | |
1814 | 0 | if ((dl = OPENSSL_zalloc(sizeof(*dl))) == NULL) { |
1815 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB); |
1816 | 0 | goto err; |
1817 | 0 | } |
1818 | | |
1819 | | /* |
1820 | | * Use ossl_ssl_init to initialize the SSL object header consistently |
1821 | | * with other SSL object types. |
1822 | | */ |
1823 | 0 | if (!ossl_ssl_init(&dl->ssl, ctx, ctx->method, SSL_TYPE_DTLS_LISTENER)) { |
1824 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB); |
1825 | 0 | goto err; |
1826 | 0 | } |
1827 | 0 | ssl_init_done = 1; |
1828 | |
|
1829 | 0 | dl->mutex = ossl_crypto_mutex_new(); |
1830 | 0 | #ifdef OPENSSL_THREADS |
1831 | 0 | if (dl->mutex == NULL) { |
1832 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB); |
1833 | 0 | goto err; |
1834 | 0 | } |
1835 | 0 | #endif |
1836 | | |
1837 | | /* Create demux with internal locking for thread safety. */ |
1838 | 0 | dl->demux = ossl_dgram_demux_new(NULL, 1, NULL, NULL); |
1839 | 0 | if (dl->demux == NULL) { |
1840 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB); |
1841 | 0 | goto err; |
1842 | 0 | } |
1843 | | |
1844 | | /* Set up the packet handler callback for routing datagrams to connections */ |
1845 | 0 | ossl_dgram_demux_set_default_handler(dl->demux, dtls_listener_packet_handler, dl); |
1846 | |
|
1847 | 0 | dl->incoming_connections = sk_SSL_new_null(); |
1848 | 0 | if (dl->incoming_connections == NULL) { |
1849 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB); |
1850 | 0 | goto err; |
1851 | 0 | } |
1852 | | |
1853 | 0 | dl->pending_conns = ossl_dgram_conn_lookup_new_addr(); |
1854 | 0 | if (dl->pending_conns == NULL) { |
1855 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB); |
1856 | 0 | goto err; |
1857 | 0 | } |
1858 | | |
1859 | 0 | dl->established_conns = ossl_dgram_conn_lookup_new_addr(); |
1860 | 0 | if (dl->established_conns == NULL) { |
1861 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB); |
1862 | 0 | goto err; |
1863 | 0 | } |
1864 | | |
1865 | 0 | dl->net_rbio = NULL; |
1866 | 0 | dl->net_wbio = NULL; |
1867 | 0 | tsan_store(&dl->listening, 0); |
1868 | 0 | dl->fatal = 0; |
1869 | | |
1870 | | /* Default timeout for pending connections: 30 seconds */ |
1871 | 0 | dl->pending_timeout = ossl_seconds2time(30); |
1872 | | |
1873 | | /* Default maximum pending connections */ |
1874 | 0 | dl->max_pending_conns = DTLS_LISTENER_DEFAULT_MAX_PENDING_CONNS; |
1875 | | |
1876 | | /* |
1877 | | * The listener owns its receive-buffer size independent of any |
1878 | | * network BIO's send-path MTU. |
1879 | | */ |
1880 | 0 | dl->max_dgram_size = DTLS_LISTENER_DEFAULT_MAX_DGRAM_SIZE; |
1881 | 0 | ossl_dgram_demux_set_mtu(dl->demux, (unsigned int)dl->max_dgram_size); |
1882 | | |
1883 | | /* |
1884 | | * Address validation is performed by default: HelloVerifyRequest for |
1885 | | * DTLS 1.0/1.2 and a HelloRetryRequest cookie for DTLS 1.3. It can be |
1886 | | * requested explicitly with SSL_LISTENER_FLAG_ADDRESS_VALIDATION, or |
1887 | | * disabled with SSL_LISTENER_FLAG_NO_VALIDATE. If both are specified we |
1888 | | * fail safe and validate: SSL_LISTENER_FLAG_ADDRESS_VALIDATION wins. |
1889 | | */ |
1890 | 0 | if ((flags & SSL_LISTENER_FLAG_NO_VALIDATE) == 0 |
1891 | 0 | || (flags & SSL_LISTENER_FLAG_ADDRESS_VALIDATION) != 0) { |
1892 | 0 | dl->require_hvr_cookie = 1; |
1893 | 0 | dl->require_hrr_cookie = 1; |
1894 | 0 | } |
1895 | |
|
1896 | 0 | dl->have_notifier = 0; |
1897 | 0 | dl->signalled_notifier = 0; |
1898 | 0 | dl->cur_blocking_waiters = 0; |
1899 | |
|
1900 | 0 | if ((flags & SSL_LISTENER_FLAG_SINGLE_THREAD) == 0) { |
1901 | 0 | if (!ossl_rio_notifier_init(&dl->notifier)) |
1902 | 0 | goto err; |
1903 | | |
1904 | 0 | dl->notifier_cv = ossl_crypto_condvar_new(); |
1905 | 0 | if (dl->notifier_cv == NULL) { |
1906 | 0 | ossl_rio_notifier_cleanup(&dl->notifier); |
1907 | 0 | goto err; |
1908 | 0 | } |
1909 | | |
1910 | 0 | dl->have_notifier = 1; |
1911 | 0 | } |
1912 | | |
1913 | 0 | return &dl->ssl; |
1914 | | |
1915 | 0 | err: |
1916 | 0 | if (dl == NULL) |
1917 | 0 | return NULL; |
1918 | | |
1919 | | /* |
1920 | | * If ossl_ssl_init succeeded, SSL_free handles all cleanup |
1921 | | * including incoming_connections, notifier_cv, and OPENSSL_free(dl) |
1922 | | * itself via ossl_dtls_listener_free. Otherwise ossl_ssl_init |
1923 | | * did not run or partially failed, so we must free the raw |
1924 | | * allocation directly. |
1925 | | */ |
1926 | 0 | if (ssl_init_done) |
1927 | 0 | SSL_free(&dl->ssl); |
1928 | 0 | else |
1929 | 0 | OPENSSL_free(dl); |
1930 | 0 | return NULL; |
1931 | 0 | } |
1932 | | |
1933 | | /* |
1934 | | * Callback to free SSL objects in pending_conns hash table. |
1935 | | * The pending_conns hash table owns the SSL objects it contains, |
1936 | | * so we must free them before freeing the hash table itself. |
1937 | | */ |
1938 | | static void dtls_free_pending_ssl_cb(SSL *ssl, const BIO_ADDR *peer, void *arg) |
1939 | 0 | { |
1940 | 0 | dtls_listener_connection_free(ssl); |
1941 | 0 | } |
1942 | | |
1943 | | void ossl_dtls_listener_free(SSL *s) |
1944 | 0 | { |
1945 | 0 | DTLS_LISTENER *dl; |
1946 | |
|
1947 | 0 | if (!IS_DTLS_LISTENER(s)) |
1948 | 0 | return; |
1949 | | |
1950 | 0 | dl = (DTLS_LISTENER *)s; |
1951 | | |
1952 | | /* Free any pending incoming connections */ |
1953 | 0 | if (dl->incoming_connections != NULL) { |
1954 | 0 | while (sk_SSL_num(dl->incoming_connections) > 0) { |
1955 | 0 | SSL *conn = sk_SSL_pop(dl->incoming_connections); |
1956 | |
|
1957 | 0 | dtls_listener_connection_free(conn); |
1958 | 0 | } |
1959 | 0 | sk_SSL_free(dl->incoming_connections); |
1960 | 0 | dl->incoming_connections = NULL; |
1961 | 0 | } |
1962 | | |
1963 | | /* |
1964 | | * Free all pending connections in the hash table. |
1965 | | */ |
1966 | 0 | if (dl->pending_conns != NULL) { |
1967 | 0 | ossl_dgram_conn_lookup_foreach(dl->pending_conns, dtls_free_pending_ssl_cb, NULL); |
1968 | 0 | ossl_dgram_conn_lookup_free(dl->pending_conns); |
1969 | 0 | dl->pending_conns = NULL; |
1970 | 0 | } |
1971 | | |
1972 | | /* Free all established connections in the hash table (no SSL ownership) */ |
1973 | 0 | if (dl->established_conns != NULL) { |
1974 | 0 | ossl_dgram_conn_lookup_free(dl->established_conns); |
1975 | 0 | dl->established_conns = NULL; |
1976 | 0 | } |
1977 | |
|
1978 | 0 | ossl_crypto_mutex_free(&dl->mutex); |
1979 | | |
1980 | | /* Free the demux after all connections that reference it are freed */ |
1981 | 0 | if (dl->demux != NULL) |
1982 | 0 | ossl_dgram_demux_free(dl->demux); |
1983 | |
|
1984 | 0 | BIO_free_all(dl->net_wbio); |
1985 | 0 | BIO_free_all(dl->net_rbio); |
1986 | |
|
1987 | 0 | if (dl->have_notifier) { |
1988 | 0 | ossl_crypto_condvar_free(&dl->notifier_cv); |
1989 | 0 | ossl_rio_notifier_cleanup(&dl->notifier); |
1990 | 0 | } |
1991 | 0 | } |
1992 | | |
1993 | | SSL *ossl_dtls_get0_listener(const SSL *ssl) |
1994 | 0 | { |
1995 | 0 | if (!IS_DTLS_LISTENER(ssl)) |
1996 | 0 | return NULL; |
1997 | | |
1998 | 0 | return (SSL *)ssl; |
1999 | 0 | } |
2000 | | |
2001 | | /* |
2002 | | * ossl_dtls_listen - start a DTLS listener accepting incoming connections. |
2003 | | */ |
2004 | | int ossl_dtls_listen(SSL *ssl) |
2005 | 0 | { |
2006 | 0 | DTLS_LISTENER *dl; |
2007 | |
|
2008 | 0 | if (!IS_DTLS_LISTENER(ssl)) { |
2009 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
2010 | 0 | return 0; |
2011 | 0 | } |
2012 | | |
2013 | 0 | dl = (DTLS_LISTENER *)ssl; |
2014 | | |
2015 | | /* Already listening is not an error. */ |
2016 | 0 | if (tsan_load(&dl->listening)) |
2017 | 0 | return 1; |
2018 | | |
2019 | 0 | tsan_store(&dl->listening, 1); |
2020 | 0 | return 1; |
2021 | 0 | } |
2022 | | |
2023 | | /* |
2024 | | * dtls_listener_conn_ready - check if connection is ready for accept queue. |
2025 | | * |
2026 | | * Determines whether the SSL object has completed cookie validation (if required) |
2027 | | * or has received a valid ClientHello (if no validation) and is ready to be |
2028 | | * moved to the incoming_connections queue. |
2029 | | * |
2030 | | * The connection is returned to the application BEFORE the handshake completes, |
2031 | | * allowing the application to finish the handshake itself. This provides more |
2032 | | * control over the handshake process. |
2033 | | * |
2034 | | * For HRR (DTLS 1.3 with validation): Ready when sc->ext.cookieok is set |
2035 | | * For HVR (DTLS 1.2 with validation): Ready when sc->d1->cookie_verified is set |
2036 | | * For no validation: Ready after receiving the first ClientHello |
2037 | | * |
2038 | | * Returns: 1 if ready, 0 if still in progress |
2039 | | */ |
2040 | | static int dtls_listener_conn_ready(SSL *ssl, DTLS_LISTENER *dl) |
2041 | 0 | { |
2042 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
2043 | |
|
2044 | 0 | if (sc == NULL) |
2045 | 0 | return 0; |
2046 | | |
2047 | | /* |
2048 | | * No validation required (SSL_LISTENER_FLAG_NO_VALIDATE): |
2049 | | * Ready immediately after receiving the first ClientHello. |
2050 | | * The connection exists in pending_conns, so it's ready. |
2051 | | */ |
2052 | 0 | if (!dl->require_hrr_cookie && !dl->require_hvr_cookie) |
2053 | 0 | return 1; |
2054 | | |
2055 | | /* |
2056 | | * For DTLS 1.3 with HRR requirement: |
2057 | | * Ready when the cookie has been validated (second ClientHello received |
2058 | | * with valid cookie after HRR was sent). The cookieok flag is set during |
2059 | | * ClientHello processing when the HRR cookie is successfully verified. |
2060 | | */ |
2061 | 0 | if (dl->require_hrr_cookie && sc->ext.cookieok) |
2062 | 0 | return 1; |
2063 | | |
2064 | | /* |
2065 | | * For DTLS 1.2 (and earlier) with HVR requirement: |
2066 | | * Ready when the cookie has been validated (second ClientHello received |
2067 | | * with valid cookie after HVR was sent). The cookie_verified flag is set |
2068 | | * during ClientHello processing when the HVR cookie is successfully verified. |
2069 | | */ |
2070 | 0 | if (dl->require_hvr_cookie && sc->d1 != NULL && sc->d1->cookie_verified) |
2071 | 0 | return 1; |
2072 | | |
2073 | | /* Not ready yet - still waiting for cookie validation */ |
2074 | 0 | return 0; |
2075 | 0 | } |
2076 | | |
2077 | | /* |
2078 | | * dtls_listener_conn_needs_retry - check if connection is waiting for more data. |
2079 | | * |
2080 | | * Determines whether the SSL object has sent an HRR/HVR and is waiting |
2081 | | * for the client's response. |
2082 | | * |
2083 | | * Returns: 1 if waiting for retry, 0 otherwise |
2084 | | */ |
2085 | | static int dtls_listener_conn_needs_retry(SSL *ssl) |
2086 | 0 | { |
2087 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
2088 | |
|
2089 | 0 | if (sc == NULL) |
2090 | 0 | return 0; |
2091 | | |
2092 | | /* |
2093 | | * For DTLS 1.3: HRR has been sent, waiting for second ClientHello |
2094 | | */ |
2095 | 0 | if (sc->hello_retry_request == SSL_HRR_PENDING |
2096 | 0 | && !ossl_statem_in_error(sc)) |
2097 | 0 | return 1; |
2098 | | |
2099 | | /* |
2100 | | * For DTLS 1.2: Check if we're in a state that indicates HVR was sent. |
2101 | | * The state machine will be waiting for the next ClientHello. |
2102 | | */ |
2103 | 0 | if (sc->statem.hand_state == DTLS_ST_SW_HELLO_VERIFY_REQUEST) |
2104 | 0 | return 1; |
2105 | | |
2106 | 0 | return 0; |
2107 | 0 | } |
2108 | | |
2109 | | /* |
2110 | | * Context for drive_pending iteration. |
2111 | | */ |
2112 | | typedef struct { |
2113 | | DTLS_LISTENER *dl; |
2114 | | int ready_count; /* Connections ready to move to established */ |
2115 | | int error_count; /* Connections with fatal errors */ |
2116 | | STACK_OF(SSL) *to_drive; /* Connections to drive (collected in phase 1) */ |
2117 | | STACK_OF(SSL) *ready_conns; /* Connections to move */ |
2118 | | STACK_OF(SSL) *failed_conns; /* Connections to remove */ |
2119 | | } DRIVE_PENDING_CTX; |
2120 | | |
2121 | | /* |
2122 | | * Callback for collecting pending connections to drive. |
2123 | | * Called with dl->mutex held. Marks connections as being_driven and up-refs them. |
2124 | | * Also checks for timed-out connections and marks them as failed. |
2125 | | */ |
2126 | | static void collect_pending_cb(SSL *ssl, const BIO_ADDR *peer, void *arg) |
2127 | 0 | { |
2128 | 0 | DRIVE_PENDING_CTX *ctx = arg; |
2129 | 0 | DTLS_LISTENER *dl = ctx->dl; |
2130 | 0 | SSL_CONNECTION *sc; |
2131 | |
|
2132 | 0 | sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
2133 | 0 | if (sc == NULL) |
2134 | 0 | return; |
2135 | | |
2136 | 0 | if (sc->d1 == NULL || sc->d1->rx == NULL) |
2137 | 0 | return; |
2138 | | |
2139 | | /* |
2140 | | * Skip if already being driven by another thread. |
2141 | | */ |
2142 | 0 | if (sc->d1->being_driven) |
2143 | 0 | return; |
2144 | | |
2145 | | /* |
2146 | | * Check if this pending connection has exceeded the timeout. |
2147 | | * Stale connections that haven't completed their handshake are removed |
2148 | | * to prevent resource exhaustion from incomplete handshakes. |
2149 | | */ |
2150 | 0 | if (!ossl_time_is_infinite(dl->pending_timeout)) { |
2151 | 0 | OSSL_TIME now = dtls_listener_get_time_direct(dl); |
2152 | 0 | OSSL_TIME age = ossl_time_subtract(now, sc->d1->created_at); |
2153 | |
|
2154 | 0 | if (ossl_time_compare(age, dl->pending_timeout) > 0) { |
2155 | | /* |
2156 | | * Connection has timed out - mark for removal |
2157 | | * |
2158 | | * Set being_driven so that a concurrent ossl_dtls_tick() running |
2159 | | * phase 1 hits the being_driven check above and skips this |
2160 | | * connection, instead of collecting it and freeing it a second |
2161 | | * time (double-free). |
2162 | | * |
2163 | | * No up-ref is needed: phase 1 runs under dl->mutex, so collection |
2164 | | * is serialized, and being_driven keeps any other tick away until |
2165 | | * our phase 3 frees this connection. The single pending-queue |
2166 | | * reference is released by dtls_listener_connection_free() in the |
2167 | | * failed_conns loop. |
2168 | | * |
2169 | | * We intentionally do not handle a failed push specially: if the |
2170 | | * push fails (allocation failure) the connection stays registered |
2171 | | * in pending_conns and is simply retried on the next tick. |
2172 | | */ |
2173 | 0 | if (ctx->failed_conns != NULL && sk_SSL_push(ctx->failed_conns, ssl) > 0) { |
2174 | 0 | sc->d1->being_driven = 1; |
2175 | 0 | ctx->error_count++; |
2176 | 0 | } |
2177 | 0 | return; |
2178 | 0 | } |
2179 | 0 | } |
2180 | | |
2181 | | /* |
2182 | | * Up-ref and add to list first, then mark as being driven. |
2183 | | * The up-ref ensures the connection stays valid while we drive it |
2184 | | * without holding the mutex. |
2185 | | */ |
2186 | 0 | if (!SSL_up_ref(ssl)) |
2187 | 0 | return; |
2188 | | |
2189 | 0 | if (sk_SSL_push(ctx->to_drive, ssl) <= 0) { |
2190 | 0 | SSL_free(ssl); /* Release the ref we just took */ |
2191 | 0 | return; |
2192 | 0 | } |
2193 | | |
2194 | 0 | sc->d1->being_driven = 1; |
2195 | 0 | } |
2196 | | |
2197 | | /* |
2198 | | * Drive a single connection's handshake. |
2199 | | * Called WITHOUT holding dl->mutex so demux_pump can be called safely. |
2200 | | */ |
2201 | | static void drive_single_connection(SSL *ssl, DTLS_LISTENER *dl, |
2202 | | DRIVE_PENDING_CTX *ctx) |
2203 | 0 | { |
2204 | 0 | SSL_CONNECTION *sc; |
2205 | 0 | int ret, ssl_err; |
2206 | |
|
2207 | 0 | sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
2208 | 0 | if (sc == NULL) |
2209 | 0 | return; |
2210 | | |
2211 | | /* |
2212 | | * Drive the state machine with SSL_accept(). |
2213 | | * |
2214 | | * We MUST set TLS1_FLAGS_STATELESS to prevent the state machine from |
2215 | | * calling SSL_clear() when entering the handshake. |
2216 | | * |
2217 | | * Without the flag, the state machine in state_machine() calls SSL_clear() |
2218 | | * when SSL_in_before() is true, which wipes out our restored state. |
2219 | | */ |
2220 | 0 | if (dl->require_hrr_cookie || dl->require_hvr_cookie) |
2221 | 0 | sc->s3.flags |= TLS1_FLAGS_STATELESS; |
2222 | | |
2223 | | /* |
2224 | | * We are inside the listener's own tick, so this must not block: nothing |
2225 | | * else can make progress while it does, including whatever it would be |
2226 | | * waiting for. |
2227 | | */ |
2228 | 0 | sc->d1->force_nonblocking = 1; |
2229 | 0 | ret = SSL_accept(ssl); |
2230 | 0 | sc->d1->force_nonblocking = 0; |
2231 | | |
2232 | | /* |
2233 | | * Always clear the stateless flag after SSL_accept() completes. |
2234 | | */ |
2235 | 0 | if (dl->require_hrr_cookie || dl->require_hvr_cookie) |
2236 | 0 | sc->s3.flags &= ~TLS1_FLAGS_STATELESS; |
2237 | | |
2238 | | /* Check if connection is ready to move to established */ |
2239 | 0 | if (dtls_listener_conn_ready(ssl, dl)) { |
2240 | 0 | if (ctx->ready_conns != NULL && sk_SSL_push(ctx->ready_conns, ssl) > 0) |
2241 | 0 | ctx->ready_count++; |
2242 | 0 | return; |
2243 | 0 | } |
2244 | | |
2245 | | /* Check if connection needs retry (HRR/HVR sent) */ |
2246 | 0 | if (dtls_listener_conn_needs_retry(ssl)) |
2247 | 0 | return; |
2248 | | |
2249 | | /* Check SSL error */ |
2250 | 0 | ssl_err = SSL_get_error(ssl, ret); |
2251 | |
|
2252 | 0 | if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) { |
2253 | | /* Handshake in progress, needs more data - keep pending */ |
2254 | 0 | return; |
2255 | 0 | } |
2256 | | |
2257 | | /* Fatal error on this connection - mark for removal */ |
2258 | 0 | if (ssl_err == SSL_ERROR_SYSCALL || ssl_err == SSL_ERROR_SSL) { |
2259 | 0 | if (ctx->failed_conns != NULL && sk_SSL_push(ctx->failed_conns, ssl) > 0) |
2260 | 0 | ctx->error_count++; |
2261 | 0 | } |
2262 | 0 | } |
2263 | | |
2264 | | /* |
2265 | | * dtls_listener_drive_pending - drive handshakes for all pending connections. |
2266 | | * |
2267 | | * Uses a three-phase approach to minimize lock contention: |
2268 | | * Phase 1: LOCK - collect connections to drive, mark as being_driven, up-ref |
2269 | | * Phase 2: UNLOCK - drive each connection (can safely call demux_pump) |
2270 | | * Phase 3: LOCK - update data structures, clear being_driven, release refs |
2271 | | * |
2272 | | * This approach allows SSL_accept() to call demux_pump() without deadlock, |
2273 | | * since the mutex is not held during phase 2. |
2274 | | * |
2275 | | * Returns: |
2276 | | * 1 At least one connection was moved to incoming_connections |
2277 | | * 0 No connections completed (all still pending or failed) |
2278 | | * -1 Fatal error |
2279 | | */ |
2280 | | static int dtls_listener_drive_pending(DTLS_LISTENER *dl) |
2281 | 0 | { |
2282 | 0 | DRIVE_PENDING_CTX ctx; |
2283 | 0 | SSL *ssl; |
2284 | 0 | SSL_CONNECTION *sc; |
2285 | 0 | int i, result = 0; |
2286 | |
|
2287 | 0 | memset(&ctx, 0, sizeof(ctx)); |
2288 | 0 | ctx.dl = dl; |
2289 | 0 | ctx.to_drive = sk_SSL_new_null(); |
2290 | 0 | ctx.ready_conns = sk_SSL_new_null(); |
2291 | 0 | ctx.failed_conns = sk_SSL_new_null(); |
2292 | |
|
2293 | 0 | if (ctx.to_drive == NULL || ctx.ready_conns == NULL |
2294 | 0 | || ctx.failed_conns == NULL) { |
2295 | 0 | sk_SSL_free(ctx.to_drive); |
2296 | 0 | sk_SSL_free(ctx.ready_conns); |
2297 | 0 | sk_SSL_free(ctx.failed_conns); |
2298 | 0 | return -1; |
2299 | 0 | } |
2300 | | |
2301 | | /* |
2302 | | * Phase 1: Collect connections to drive. |
2303 | | * Hold mutex while iterating pending_conns, mark connections as being_driven, |
2304 | | * and up-ref them so they stay valid after we release the mutex. |
2305 | | */ |
2306 | 0 | ossl_crypto_mutex_lock(dl->mutex); |
2307 | 0 | ossl_dgram_conn_lookup_foreach(dl->pending_conns, collect_pending_cb, &ctx); |
2308 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
2309 | | |
2310 | | /* |
2311 | | * Phase 2: Drive connections WITHOUT holding mutex. |
2312 | | * This allows SSL_accept() to call demux_pump() which may invoke |
2313 | | * packet_handler(), which needs to acquire the mutex. |
2314 | | */ |
2315 | 0 | for (i = 0; i < sk_SSL_num(ctx.to_drive); i++) { |
2316 | 0 | ssl = sk_SSL_value(ctx.to_drive, i); |
2317 | 0 | drive_single_connection(ssl, dl, &ctx); |
2318 | 0 | } |
2319 | | |
2320 | | /* |
2321 | | * Phase 3: Update data structures. |
2322 | | * Re-acquire mutex to move ready connections to established, |
2323 | | * remove failed connections, clear being_driven flags, and release refs. |
2324 | | */ |
2325 | 0 | ossl_crypto_mutex_lock(dl->mutex); |
2326 | | |
2327 | | /* |
2328 | | * Since we have the mutex, clear the driven flag and release the |
2329 | | * up-ref for each connection. |
2330 | | */ |
2331 | 0 | for (i = 0; i < sk_SSL_num(ctx.to_drive); i++) { |
2332 | 0 | ssl = sk_SSL_value(ctx.to_drive, i); |
2333 | 0 | sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
2334 | |
|
2335 | 0 | if (sc != NULL && sc->d1 != NULL) |
2336 | 0 | sc->d1->being_driven = 0; |
2337 | |
|
2338 | 0 | SSL_free(ssl); /* Release reference from phase 1 */ |
2339 | 0 | } |
2340 | | |
2341 | | /* Move ready connections to established and incoming queue */ |
2342 | 0 | for (i = 0; i < sk_SSL_num(ctx.ready_conns); i++) { |
2343 | 0 | ssl = sk_SSL_value(ctx.ready_conns, i); |
2344 | 0 | sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
2345 | |
|
2346 | 0 | if (sc == NULL || sc->d1 == NULL) |
2347 | 0 | continue; |
2348 | | |
2349 | | /* Get peer address from the connection */ |
2350 | 0 | if (BIO_ADDR_family(&sc->d1->peer_addr) != AF_UNSPEC) { |
2351 | | |
2352 | | /* Remove from pending */ |
2353 | 0 | ossl_dgram_conn_lookup_unregister(dl->pending_conns, &sc->d1->peer_addr); |
2354 | | |
2355 | | /* Add to established connections (inline, we already hold mutex) */ |
2356 | 0 | if (dl->established_conns == NULL || !ossl_dgram_conn_lookup_register_addr(dl->established_conns, &sc->d1->peer_addr, ssl)) { |
2357 | 0 | dtls_listener_connection_free(ssl); |
2358 | 0 | continue; |
2359 | 0 | } |
2360 | | |
2361 | | /* Add to incoming queue */ |
2362 | 0 | if (sk_SSL_push(dl->incoming_connections, ssl) > 0) { |
2363 | 0 | result = 1; |
2364 | 0 | } else { |
2365 | | /* Failed to add to queue, unregister and free */ |
2366 | 0 | ossl_dgram_conn_lookup_unregister(dl->established_conns, |
2367 | 0 | &sc->d1->peer_addr); |
2368 | 0 | dtls_listener_connection_free(ssl); |
2369 | 0 | } |
2370 | 0 | } |
2371 | 0 | } |
2372 | | |
2373 | | /* |
2374 | | * A connection became acceptable. Any thread blocked waiting for one is |
2375 | | * polling the network socket, which will not necessarily become readable |
2376 | | * again on its behalf, so wake it explicitly. |
2377 | | */ |
2378 | 0 | if (result) |
2379 | 0 | dtls_listener_signal_notifier(dl); |
2380 | | |
2381 | | /* Remove failed connections (after releasing refs so ref count is 1) */ |
2382 | 0 | for (i = 0; i < sk_SSL_num(ctx.failed_conns); i++) { |
2383 | 0 | ssl = sk_SSL_value(ctx.failed_conns, i); |
2384 | 0 | sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
2385 | |
|
2386 | 0 | if (sc != NULL && sc->d1 != NULL) |
2387 | 0 | ossl_dgram_conn_lookup_unregister(dl->pending_conns, &sc->d1->peer_addr); |
2388 | |
|
2389 | 0 | dtls_listener_connection_free(ssl); |
2390 | 0 | } |
2391 | |
|
2392 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
2393 | |
|
2394 | 0 | sk_SSL_free(ctx.to_drive); |
2395 | 0 | sk_SSL_free(ctx.ready_conns); |
2396 | 0 | sk_SSL_free(ctx.failed_conns); |
2397 | |
|
2398 | 0 | return result; |
2399 | 0 | } |
2400 | | |
2401 | | /* |
2402 | | * ossl_dtls_tick - drive one iteration of the DTLS listener I/O loop. |
2403 | | * |
2404 | | * Uses the demux pump/callback architecture for efficient packet handling: |
2405 | | * 1. Call ossl_dgram_demux_pump() to read datagrams from the network |
2406 | | * 2. The demux invokes dtls_listener_packet_handler() for each datagram |
2407 | | * 3. The handler routes URXEs to connections (established or pending) |
2408 | | * 4. Drive handshakes for pending connections |
2409 | | * 5. Move completed connections to established_conns and incoming queue |
2410 | | * |
2411 | | * Return values: |
2412 | | * 1 A verified connection was pushed onto dl->incoming_connections. |
2413 | | * 0 Exchange incomplete (HRR/HVR sent, or no data yet); call again. |
2414 | | * -1 Fatal error; dl->fatal is set. |
2415 | | */ |
2416 | | int ossl_dtls_tick(DTLS_LISTENER *dl) |
2417 | 0 | { |
2418 | 0 | int pump_ret; |
2419 | |
|
2420 | 0 | if (dl == NULL || dl->net_rbio == NULL) |
2421 | 0 | return 0; |
2422 | | |
2423 | | /* |
2424 | | * Get datagrams from the network and route them to connections. |
2425 | | */ |
2426 | 0 | pump_ret = ossl_dgram_demux_pump(dl->demux); |
2427 | |
|
2428 | 0 | if (pump_ret == DGRAM_DEMUX_PUMP_RES_PERMANENT_FAIL) { |
2429 | | /* Fatal BIO or allocation error */ |
2430 | 0 | ossl_crypto_mutex_lock(dl->mutex); |
2431 | 0 | dl->fatal = 1; |
2432 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
2433 | 0 | return -1; |
2434 | 0 | } |
2435 | | |
2436 | | /* |
2437 | | * Drive Handshakes for pending connections. |
2438 | | * call even if pump_ret indicates no data or temporary failure, |
2439 | | * to allow handshakes to progress even when no new data is arriving |
2440 | | */ |
2441 | 0 | return dtls_listener_drive_pending(dl); |
2442 | 0 | } |
2443 | | |
2444 | | SSL *ossl_dtls_accept_connection(SSL *ssl, uint64_t flags) |
2445 | 0 | { |
2446 | 0 | DTLS_LISTENER *dl; |
2447 | 0 | SSL *conn = NULL; |
2448 | 0 | SSL_CONNECTION *sc = NULL; |
2449 | 0 | int no_block = ((flags & SSL_ACCEPT_CONNECTION_NO_BLOCK) != 0); |
2450 | |
|
2451 | 0 | if (!IS_DTLS_LISTENER(ssl)) { |
2452 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
2453 | 0 | return NULL; |
2454 | 0 | } |
2455 | | |
2456 | 0 | dl = (DTLS_LISTENER *)ssl; |
2457 | |
|
2458 | 0 | if (!ossl_dtls_listen(ssl)) |
2459 | 0 | return NULL; |
2460 | | |
2461 | | /* If a previous tick produced a fatal BIO error, do not try again. */ |
2462 | 0 | ossl_crypto_mutex_lock(dl->mutex); |
2463 | 0 | if (dl->fatal) { |
2464 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
2465 | 0 | return NULL; |
2466 | 0 | } |
2467 | | |
2468 | | /* Fast path: return any already-queued connection immediately. */ |
2469 | 0 | conn = sk_SSL_shift(dl->incoming_connections); |
2470 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
2471 | 0 | if (conn != NULL) |
2472 | 0 | goto end; |
2473 | | |
2474 | | /* |
2475 | | * Wait only if the caller has not asked us not to and the listener is in |
2476 | | * blocking mode. Note that the check for a network BIO below is deliberately |
2477 | | * left ahead of this, so that asking to wait on a listener which has none |
2478 | | * remains an error rather than silently returning nothing. |
2479 | | */ |
2480 | 0 | if (!no_block && !ossl_dtls_blocking(ssl) && dl->net_rbio != NULL) |
2481 | 0 | no_block = 1; |
2482 | |
|
2483 | 0 | if (no_block) { |
2484 | | /* |
2485 | | * Non-blocking: run one tick to drain any pending datagram, then |
2486 | | * return whatever is in the queue |
2487 | | */ |
2488 | 0 | if (dl->net_rbio != NULL) { |
2489 | 0 | if (ossl_dtls_tick(dl) < 0) { |
2490 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR); |
2491 | 0 | return NULL; |
2492 | 0 | } |
2493 | 0 | } |
2494 | 0 | ossl_crypto_mutex_lock(dl->mutex); |
2495 | 0 | conn = sk_SSL_shift(dl->incoming_connections); |
2496 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
2497 | 0 | goto end; |
2498 | 0 | } |
2499 | | |
2500 | | /* Blocking path: we need a BIO to make any progress. */ |
2501 | 0 | if (dl->net_rbio == NULL) { |
2502 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_BIO_NOT_SET); |
2503 | 0 | return NULL; |
2504 | 0 | } |
2505 | | |
2506 | | /* |
2507 | | * Blocking path: tick to make whatever progress is possible now, and if |
2508 | | * that did not produce a connection, wait for readiness before ticking |
2509 | | * again. |
2510 | | * |
2511 | | * The wait is what stops this from being a busy loop. The network BIO is |
2512 | | * non-blocking, so a tick which finds no datagram returns immediately; |
2513 | | * without waiting in between, this loop would spin. |
2514 | | */ |
2515 | 0 | for (;;) { |
2516 | 0 | if (ossl_dtls_tick(dl) < 0) { |
2517 | | /* fatal BIO error */ |
2518 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR); |
2519 | 0 | break; |
2520 | 0 | } |
2521 | | |
2522 | 0 | ossl_crypto_mutex_lock(dl->mutex); |
2523 | 0 | conn = sk_SSL_shift(dl->incoming_connections); |
2524 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
2525 | 0 | if (conn != NULL) |
2526 | 0 | break; |
2527 | | |
2528 | | /* |
2529 | | * Nothing yet, so wait for the listener to become ready before ticking |
2530 | | * again. What that amounts to is decided by the poll translation for a |
2531 | | * listener: the network socket becoming readable, or another thread |
2532 | | * signalling the notifier because it produced readiness on our behalf. |
2533 | | */ |
2534 | 0 | if (!ossl_dtls_block_until_ready(ssl, SSL_POLL_EVENT_IC, |
2535 | 0 | ossl_time_infinite(), /*bound_by_event_timeout=*/1)) |
2536 | 0 | break; |
2537 | 0 | } |
2538 | |
|
2539 | 0 | end: |
2540 | 0 | if (conn != NULL) { |
2541 | 0 | sc = SSL_CONNECTION_FROM_SSL(conn); |
2542 | | /* |
2543 | | * Take a reference on the listener now that ownership of the connection |
2544 | | * is transferring to the application. While in incoming_connections, |
2545 | | * the connection did not hold a reference to avoid circular dependencies. |
2546 | | * Now that the application owns the connection, it must hold a reference |
2547 | | * to ensure the listener stays alive - the connection needs the listener |
2548 | | * for packet routing |
2549 | | */ |
2550 | 0 | if (sc == NULL || sc->d1 == NULL || sc->d1->listener == NULL |
2551 | 0 | || !SSL_up_ref(sc->d1->listener)) { |
2552 | | /* |
2553 | | * Ownership did not transfer to the application. A connection taken |
2554 | | * from incoming_connections is still registered in established_conns; |
2555 | | */ |
2556 | 0 | if (sc != NULL && sc->d1 != NULL) |
2557 | 0 | ossl_dtls_listener_unregister_established_conn(ssl, &sc->d1->peer_addr); |
2558 | 0 | dtls_listener_connection_free(conn); |
2559 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR); |
2560 | 0 | return NULL; |
2561 | 0 | } |
2562 | 0 | } |
2563 | 0 | return conn; |
2564 | 0 | } |
2565 | | |
2566 | | /* |
2567 | | * ossl_dtls_listener_set0_net_rbio - set the network read BIO for a listener. |
2568 | | * |
2569 | | * Thread safety: The caller must ensure that this function is not called |
2570 | | * concurrently with any other operations on the listener or its connections. |
2571 | | * This includes SSL_accept_connection(), SSL_poll(), SSL_tick(), and any |
2572 | | * I/O operations on connections created from this listener. |
2573 | | * |
2574 | | * The BIO must not be changed while other threads are actively using the |
2575 | | * listener. Typically, the BIO should be set once before calling SSL_listen() |
2576 | | * and not modified afterward. |
2577 | | */ |
2578 | | void ossl_dtls_listener_set0_net_rbio(SSL *s, BIO *bio) |
2579 | 0 | { |
2580 | 0 | DTLS_LISTENER *dl; |
2581 | 0 | BIO *old_rbio; |
2582 | |
|
2583 | 0 | if (!IS_DTLS_LISTENER(s)) |
2584 | 0 | return; |
2585 | | |
2586 | 0 | dl = (DTLS_LISTENER *)s; |
2587 | | |
2588 | | /* |
2589 | | * The listener demultiplexes one socket to many connections, so it can |
2590 | | * never afford to block inside a read: a read for one connection would |
2591 | | * stall every other, and the demux lock is held across it. Blocking |
2592 | | * behaviour is provided by waiting for readiness instead, so configure the |
2593 | | * BIO for non-blocking operation on the application's behalf, as QUIC does. |
2594 | | */ |
2595 | 0 | if (bio != NULL) |
2596 | 0 | BIO_set_nbio(bio, 1); /* best effort autoconfig */ |
2597 | |
|
2598 | 0 | ossl_crypto_mutex_lock(dl->mutex); |
2599 | | |
2600 | | /* |
2601 | | * The demux receive-buffer size is the listener's configured maximum |
2602 | | * datagram size, independent of the network BIO's path MTU. Size the demux |
2603 | | * to that value for whatever BIO is attached. |
2604 | | */ |
2605 | 0 | ossl_dgram_demux_set_bio(dl->demux, bio); |
2606 | 0 | ossl_dgram_demux_set_mtu(dl->demux, (unsigned int)dl->max_dgram_size); |
2607 | |
|
2608 | 0 | old_rbio = dl->net_rbio; |
2609 | | |
2610 | | /* No change - nothing to do */ |
2611 | 0 | if (old_rbio == bio) { |
2612 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
2613 | 0 | return; |
2614 | 0 | } |
2615 | | |
2616 | 0 | dl->net_rbio = bio; |
2617 | |
|
2618 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
2619 | | |
2620 | | /* Free the old BIO now that we've taken ownership of the new one */ |
2621 | 0 | BIO_free_all(old_rbio); |
2622 | 0 | } |
2623 | | |
2624 | | /* |
2625 | | * update_conn_wbio - callback to update the wbio on a single connection. |
2626 | | * |
2627 | | * Used by ossl_dtls_listener_set0_net_wbio() to propagate wbio changes |
2628 | | * to all pending and established connections |
2629 | | */ |
2630 | | static void update_conn_wbio(SSL *ssl, const BIO_ADDR *peer, void *arg) |
2631 | 0 | { |
2632 | 0 | BIO *new_wbio = arg; |
2633 | |
|
2634 | 0 | if (SSL_get_wbio(ssl) == new_wbio) |
2635 | 0 | return; |
2636 | | |
2637 | 0 | if (new_wbio != NULL && !BIO_up_ref(new_wbio)) |
2638 | 0 | return; |
2639 | | |
2640 | 0 | SSL_set0_wbio(ssl, new_wbio); |
2641 | 0 | } |
2642 | | |
2643 | | /* |
2644 | | * ossl_dtls_listener_set0_net_wbio - set the network write BIO for a listener. |
2645 | | * |
2646 | | * Thread safety: The caller must ensure that this function is not called |
2647 | | * concurrently with any other operations on the listener or its connections. |
2648 | | * This includes SSL_accept_connection(), SSL_poll(), SSL_tick(), and any |
2649 | | * I/O operations on connections created from this listener. |
2650 | | * |
2651 | | * The BIO must not be changed while other threads are actively using the |
2652 | | * listener. Typically, the BIO should be set once before calling SSL_listen() |
2653 | | * and not modified afterward. |
2654 | | */ |
2655 | | void ossl_dtls_listener_set0_net_wbio(SSL *s, BIO *bio) |
2656 | 0 | { |
2657 | 0 | DTLS_LISTENER *dl; |
2658 | 0 | BIO *old_wbio; |
2659 | |
|
2660 | 0 | if (!IS_DTLS_LISTENER(s)) |
2661 | 0 | return; |
2662 | | |
2663 | 0 | dl = (DTLS_LISTENER *)s; |
2664 | | |
2665 | | /* See ossl_dtls_listener_set0_net_rbio() as to why. */ |
2666 | 0 | if (bio != NULL) |
2667 | 0 | BIO_set_nbio(bio, 1); /* best effort autoconfig */ |
2668 | |
|
2669 | 0 | old_wbio = dl->net_wbio; |
2670 | | |
2671 | | /* No change - nothing to do */ |
2672 | 0 | if (old_wbio == bio) { |
2673 | 0 | return; |
2674 | 0 | } |
2675 | | |
2676 | | /* Update wbio in all pending connections */ |
2677 | 0 | if (dl->pending_conns != NULL) |
2678 | 0 | ossl_dgram_conn_lookup_foreach(dl->pending_conns, update_conn_wbio, bio); |
2679 | | |
2680 | | /* Update wbio in all established connections */ |
2681 | 0 | if (dl->established_conns != NULL) |
2682 | 0 | ossl_dgram_conn_lookup_foreach(dl->established_conns, update_conn_wbio, bio); |
2683 | |
|
2684 | 0 | dl->net_wbio = bio; |
2685 | | |
2686 | | /* Free the old BIO now that we've taken ownership of the new one */ |
2687 | 0 | BIO_free_all(old_wbio); |
2688 | 0 | } |
2689 | | |
2690 | | /* |
2691 | | * ossl_dtls_listener_get_net_rbio - get the network read BIO for a listener. |
2692 | | * |
2693 | | * Thread safety: The caller must ensure that the BIO is not being changed |
2694 | | * concurrently via SSL_set0_rbio(). The returned BIO pointer is only valid |
2695 | | * as long as no other thread modifies it. |
2696 | | */ |
2697 | | BIO *ossl_dtls_listener_get_net_rbio(const SSL *s) |
2698 | 0 | { |
2699 | 0 | const DTLS_LISTENER *dl; |
2700 | |
|
2701 | 0 | if (!IS_DTLS_LISTENER(s)) |
2702 | 0 | return NULL; |
2703 | | |
2704 | 0 | dl = (const DTLS_LISTENER *)s; |
2705 | |
|
2706 | 0 | return dl->net_rbio; |
2707 | 0 | } |
2708 | | |
2709 | | /* |
2710 | | * ossl_dtls_listener_get_net_wbio - get the network write BIO for a listener. |
2711 | | * |
2712 | | * Thread safety: The caller must ensure that the BIO is not being changed |
2713 | | * concurrently via SSL_set0_wbio(). The returned BIO pointer is only valid |
2714 | | * as long as no other thread modifies it. |
2715 | | */ |
2716 | | BIO *ossl_dtls_listener_get_net_wbio(const SSL *s) |
2717 | 0 | { |
2718 | 0 | const DTLS_LISTENER *dl; |
2719 | |
|
2720 | 0 | if (!IS_DTLS_LISTENER(s)) |
2721 | 0 | return NULL; |
2722 | | |
2723 | 0 | dl = (const DTLS_LISTENER *)s; |
2724 | |
|
2725 | 0 | return dl->net_wbio; |
2726 | 0 | } |
2727 | | |
2728 | | /* |
2729 | | * Established connections API - these handle their own locking. |
2730 | | * |
2731 | | * The established_conns lookup table is accessed from multiple threads: |
2732 | | * - Listener thread: looking up and registering connections |
2733 | | * - Connection thread: unregistering via SSL_free -> dtls1_free |
2734 | | */ |
2735 | | |
2736 | | /* |
2737 | | * ossl_dtls_listener_find_established_conn - find an established connection. |
2738 | | * |
2739 | | * Looks up a connection in the established_conns table by peer address. |
2740 | | * Returns the SSL connection if found, NULL otherwise. |
2741 | | */ |
2742 | | SSL *ossl_dtls_listener_find_established_conn(DTLS_LISTENER *dl, |
2743 | | const DGRAM_URXE *urxe) |
2744 | 0 | { |
2745 | 0 | SSL *result = NULL; |
2746 | |
|
2747 | 0 | if (dl == NULL || urxe == NULL) |
2748 | 0 | return NULL; |
2749 | | |
2750 | 0 | ossl_crypto_mutex_lock(dl->mutex); |
2751 | |
|
2752 | 0 | if (dl->established_conns != NULL) |
2753 | 0 | result = ossl_dgram_conn_lookup_find(dl->established_conns, urxe); |
2754 | |
|
2755 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
2756 | 0 | return result; |
2757 | 0 | } |
2758 | | |
2759 | | /* |
2760 | | * ossl_dtls_listener_unregister_established_conn - unregister an established |
2761 | | * connection from the listener. |
2762 | | * |
2763 | | * Called when a DTLS connection created by this listener is being freed. |
2764 | | */ |
2765 | | void ossl_dtls_listener_unregister_established_conn(SSL *s, const BIO_ADDR *peer_addr) |
2766 | 0 | { |
2767 | 0 | DTLS_LISTENER *dl; |
2768 | |
|
2769 | 0 | if (!IS_DTLS_LISTENER(s)) |
2770 | 0 | return; |
2771 | | |
2772 | 0 | if (peer_addr == NULL || BIO_ADDR_family(peer_addr) == AF_UNSPEC) |
2773 | 0 | return; |
2774 | | |
2775 | 0 | dl = (DTLS_LISTENER *)s; |
2776 | |
|
2777 | 0 | ossl_crypto_mutex_lock(dl->mutex); |
2778 | |
|
2779 | 0 | if (dl->established_conns != NULL) |
2780 | 0 | ossl_dgram_conn_lookup_unregister(dl->established_conns, peer_addr); |
2781 | |
|
2782 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
2783 | 0 | } |
2784 | | |
2785 | | /* |
2786 | | * ossl_dtls_listener_clear_established_conns - clear and recreate the |
2787 | | * established_conns table. |
2788 | | */ |
2789 | | void ossl_dtls_listener_clear_established_conns(DTLS_LISTENER *dl) |
2790 | 0 | { |
2791 | 0 | if (dl == NULL) |
2792 | 0 | return; |
2793 | | |
2794 | 0 | ossl_crypto_mutex_lock(dl->mutex); |
2795 | |
|
2796 | 0 | if (dl->established_conns != NULL) |
2797 | 0 | ossl_dgram_conn_lookup_free(dl->established_conns); |
2798 | 0 | dl->established_conns = ossl_dgram_conn_lookup_new_addr(); |
2799 | |
|
2800 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
2801 | 0 | } |
2802 | | |
2803 | | size_t ossl_dtls_get_accept_connection_queue_len(SSL *ssl) |
2804 | 0 | { |
2805 | 0 | DTLS_LISTENER *dl; |
2806 | 0 | size_t len; |
2807 | |
|
2808 | 0 | if (!IS_DTLS_LISTENER(ssl)) |
2809 | 0 | return 0; |
2810 | | |
2811 | 0 | dl = (DTLS_LISTENER *)ssl; |
2812 | |
|
2813 | 0 | ossl_crypto_mutex_lock(dl->mutex); |
2814 | 0 | len = (size_t)sk_SSL_num(dl->incoming_connections); |
2815 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
2816 | |
|
2817 | 0 | return len; |
2818 | 0 | } |
2819 | | |
2820 | | /* |
2821 | | * Set an override time callback for the DTLS listener. |
2822 | | * This is primarily for testing purposes to allow time injection. |
2823 | | * If now_cb is NULL, the listener will use ossl_time_now(). |
2824 | | */ |
2825 | | int ossl_dtls_listener_set_override_now_cb(SSL *s, |
2826 | | OSSL_TIME (*now_cb)(void *arg), |
2827 | | void *now_cb_arg) |
2828 | 0 | { |
2829 | 0 | DTLS_LISTENER *dl; |
2830 | |
|
2831 | 0 | if (!IS_DTLS_LISTENER(s)) |
2832 | 0 | return 0; |
2833 | | |
2834 | 0 | dl = (DTLS_LISTENER *)s; |
2835 | 0 | dl->now_cb = now_cb; |
2836 | 0 | dl->now_cb_arg = now_cb_arg; |
2837 | |
|
2838 | 0 | return 1; |
2839 | 0 | } |
2840 | | |
2841 | | /* |
2842 | | * ossl_dtls_get_value_uint - read a tunable value from a DTLS listener. |
2843 | | * |
2844 | | * DTLS-side implementation backing SSL_get_value_uint(3) when the target |
2845 | | * SSL object is a DTLS listener created by SSL_new_listener(). |
2846 | | * |
2847 | | * Supported (id) values: |
2848 | | * SSL_VALUE_DTLS_LISTENER_MAX_PENDING_CONNS |
2849 | | * Current cap on the number of pending (handshake-in-progress) |
2850 | | * connections the listener will track. |
2851 | | * SSL_VALUE_DTLS_LISTENER_PENDING_TIMEOUT |
2852 | | * Current reap timeout for pending connections, in milliseconds. |
2853 | | * UINT64_MAX means "infinite / disabled". |
2854 | | * SSL_VALUE_DTLS_LISTENER_MAX_DGRAM_SIZE |
2855 | | * Maximum size in bytes of a datagram the listener will receive. |
2856 | | * |
2857 | | * Only SSL_VALUE_CLASS_GENERIC is accepted for class_; other |
2858 | | * classes are rejected with a return of 0 |
2859 | | * |
2860 | | * Parameters: |
2861 | | * s - listener SSL. Must satisfy IS_DTLS_LISTENER(s). |
2862 | | * class_ - value class; must be SSL_VALUE_CLASS_GENERIC. |
2863 | | * id - one of the SSL_VALUE_DTLS_LISTENER_* ids listed above. |
2864 | | * value - out-parameter receiving the current value. Must be non-NULL. |
2865 | | * |
2866 | | * Returns: |
2867 | | * 1 on success (*value populated). |
2868 | | * 0 on failure (unsupported id, wrong class, NULL value, or not a |
2869 | | * DTLS listener). |
2870 | | */ |
2871 | | int ossl_dtls_get_value_uint(SSL *s, uint32_t class_, uint32_t id, uint64_t *value) |
2872 | 0 | { |
2873 | 0 | DTLS_LISTENER *dl; |
2874 | 0 | int ret = 1; |
2875 | |
|
2876 | 0 | if (!IS_DTLS_LISTENER(s)) { |
2877 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_LISTENER_USE_ONLY); |
2878 | 0 | return 0; |
2879 | 0 | } |
2880 | 0 | if (class_ != SSL_VALUE_CLASS_GENERIC) { |
2881 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS); |
2882 | 0 | return 0; |
2883 | 0 | } |
2884 | 0 | if (value == NULL) { |
2885 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
2886 | 0 | return 0; |
2887 | 0 | } |
2888 | | |
2889 | 0 | dl = (DTLS_LISTENER *)s; |
2890 | |
|
2891 | 0 | ossl_crypto_mutex_lock(dl->mutex); |
2892 | |
|
2893 | 0 | switch (id) { |
2894 | 0 | case SSL_VALUE_DTLS_LISTENER_MAX_PENDING_CONNS: |
2895 | 0 | *value = (uint64_t)dl->max_pending_conns; |
2896 | 0 | break; |
2897 | 0 | case SSL_VALUE_DTLS_LISTENER_PENDING_TIMEOUT: |
2898 | 0 | if (ossl_time_is_infinite(dl->pending_timeout)) |
2899 | 0 | *value = UINT64_MAX; |
2900 | 0 | else |
2901 | 0 | *value = ossl_time2ms(dl->pending_timeout); |
2902 | 0 | break; |
2903 | 0 | case SSL_VALUE_DTLS_LISTENER_MAX_DGRAM_SIZE: |
2904 | 0 | *value = (uint64_t)dl->max_dgram_size; |
2905 | 0 | break; |
2906 | 0 | default: |
2907 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_CONFIG_VALUE); |
2908 | 0 | ret = 0; |
2909 | 0 | break; |
2910 | 0 | } |
2911 | | |
2912 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
2913 | 0 | return ret; |
2914 | 0 | } |
2915 | | |
2916 | | /* |
2917 | | * ossl_dtls_set_value_uint - write a tunable value on a DTLS listener. |
2918 | | * |
2919 | | * DTLS-side implementation backing SSL_set_value_uint(3) when the target |
2920 | | * SSL object is a DTLS listener created by SSL_new_listener(). |
2921 | | * |
2922 | | * Supported (id) values -- see ossl_dtls_get_value_uint() above. |
2923 | | * |
2924 | | * Per-id policy: |
2925 | | * SSL_VALUE_DTLS_LISTENER_MAX_PENDING_CONNS |
2926 | | * value == 0 is rejected (a zero cap would reject every incoming |
2927 | | * connection). Values larger than SIZE_MAX are clamped to SIZE_MAX |
2928 | | * to avoid silent truncation on 32-bit builds. |
2929 | | * SSL_VALUE_DTLS_LISTENER_PENDING_TIMEOUT |
2930 | | * Interpreted as milliseconds. value == 0 is rejected. UINT64_MAX is |
2931 | | * treated as "infinite / disabled". |
2932 | | * SSL_VALUE_DTLS_LISTENER_MAX_DGRAM_SIZE |
2933 | | * Clamped to the maximum UDP payload (DTLS_LISTENER_MAX_DGRAM_SIZE); |
2934 | | * values below the demux minimum receive size are rejected. |
2935 | | * |
2936 | | * Only SSL_VALUE_CLASS_GENERIC is accepted for class_. |
2937 | | * |
2938 | | * Parameters: |
2939 | | * s - listener SSL. Must satisfy IS_DTLS_LISTENER(s). |
2940 | | * class_ - value class; must be SSL_VALUE_CLASS_GENERIC. |
2941 | | * id - one of the SSL_VALUE_DTLS_LISTENER_* ids. |
2942 | | * value - new value to store, in the units documented per id. |
2943 | | * |
2944 | | * Returns: |
2945 | | * 1 on success. |
2946 | | * 0 on failure (unsupported id, wrong class, not a DTLS listener, |
2947 | | * or policy rejection such as 0 on the cap). |
2948 | | */ |
2949 | | int ossl_dtls_set_value_uint(SSL *s, uint32_t class_, uint32_t id, uint64_t value) |
2950 | 0 | { |
2951 | 0 | DTLS_LISTENER *dl; |
2952 | 0 | int ret = 1; |
2953 | |
|
2954 | 0 | if (!IS_DTLS_LISTENER(s)) { |
2955 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_LISTENER_USE_ONLY); |
2956 | 0 | return 0; |
2957 | 0 | } |
2958 | 0 | if (class_ != SSL_VALUE_CLASS_GENERIC) { |
2959 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS); |
2960 | 0 | return 0; |
2961 | 0 | } |
2962 | | |
2963 | 0 | dl = (DTLS_LISTENER *)s; |
2964 | |
|
2965 | 0 | ossl_crypto_mutex_lock(dl->mutex); |
2966 | |
|
2967 | 0 | switch (id) { |
2968 | 0 | case SSL_VALUE_DTLS_LISTENER_MAX_PENDING_CONNS: |
2969 | 0 | if (value == 0) { |
2970 | | /* A zero cap would reject every connection (num_items >= 0). */ |
2971 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
2972 | 0 | ret = 0; |
2973 | 0 | break; |
2974 | 0 | } |
2975 | | /* Clamp to SIZE_MAX to prevent silent truncation on 32-bit. */ |
2976 | 0 | dl->max_pending_conns = (value > SIZE_MAX) ? SIZE_MAX : (size_t)value; |
2977 | 0 | break; |
2978 | 0 | case SSL_VALUE_DTLS_LISTENER_PENDING_TIMEOUT: |
2979 | 0 | if (value == 0) { |
2980 | | /* |
2981 | | * A zero timeout will remove all pending connections on the next |
2982 | | * tick, before the handshake could complete. |
2983 | | */ |
2984 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
2985 | 0 | ret = 0; |
2986 | 0 | break; |
2987 | 0 | } |
2988 | | /* |
2989 | | * ossl_ms2time() scales by OSSL_TIME_MS (10^6 ns/ms), so any value |
2990 | | * above UINT64_MAX / OSSL_TIME_MS would overflow the product and |
2991 | | * silently wrap to a tiny timeout. Treat those (which includes the |
2992 | | * UINT64_MAX "infinite" sentinel) as an infinite timeout. |
2993 | | */ |
2994 | 0 | if (value > UINT64_MAX / OSSL_TIME_MS) |
2995 | 0 | dl->pending_timeout = ossl_time_infinite(); |
2996 | 0 | else |
2997 | 0 | dl->pending_timeout = ossl_ms2time(value); |
2998 | 0 | break; |
2999 | 0 | case SSL_VALUE_DTLS_LISTENER_MAX_DGRAM_SIZE: |
3000 | | /* Nothing larger than the maximum UDP payload can ever arrive. */ |
3001 | 0 | if (value > DTLS_LISTENER_MAX_DGRAM_SIZE) |
3002 | 0 | value = DTLS_LISTENER_MAX_DGRAM_SIZE; |
3003 | | /* set_mtu returns 0 (rejecting) for values below the demux minimum. */ |
3004 | 0 | if (!ossl_dgram_demux_set_mtu(dl->demux, (unsigned int)value)) { |
3005 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
3006 | 0 | ret = 0; |
3007 | 0 | } else { |
3008 | 0 | dl->max_dgram_size = (size_t)value; |
3009 | 0 | } |
3010 | 0 | break; |
3011 | 0 | default: |
3012 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_CONFIG_VALUE); |
3013 | 0 | ret = 0; |
3014 | 0 | break; |
3015 | 0 | } |
3016 | | |
3017 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
3018 | 0 | return ret; |
3019 | 0 | } |
3020 | | |
3021 | | /* |
3022 | | * Resolve the requested blocking mode of a DTLS listener, or of a connection |
3023 | | * created from one, following the inheritance chain. |
3024 | | * |
3025 | | * A connection set to INHERIT follows its listener; a listener set to INHERIT |
3026 | | * is blocking, there being nothing further to inherit from. Blocking is |
3027 | | * therefore the default unless the application asks otherwise. |
3028 | | * |
3029 | | * Returns 1 if blocking is wanted, which says nothing about whether it can be |
3030 | | * provided - see ossl_dtls_can_support_blocking(). |
3031 | | */ |
3032 | | static int ossl_dtls_desires_blocking(const SSL *s) |
3033 | 0 | { |
3034 | 0 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s); |
3035 | 0 | const DTLS_LISTENER *dl = NULL; |
3036 | |
|
3037 | 0 | if (sc != NULL && sc->d1 != NULL) { |
3038 | | /* The listener is driving this connection; it must not block. */ |
3039 | 0 | if (sc->d1->force_nonblocking) |
3040 | 0 | return 0; |
3041 | | |
3042 | 0 | if (sc->d1->req_blocking_mode != DTLS_BLOCKING_MODE_INHERIT) |
3043 | 0 | return sc->d1->req_blocking_mode == DTLS_BLOCKING_MODE_BLOCKING; |
3044 | | |
3045 | 0 | dl = (const DTLS_LISTENER *)sc->d1->listener; |
3046 | 0 | } else if (IS_DTLS_LISTENER(s)) { |
3047 | 0 | dl = (const DTLS_LISTENER *)s; |
3048 | 0 | } |
3049 | | |
3050 | 0 | if (dl == NULL) |
3051 | 0 | return 0; |
3052 | | |
3053 | 0 | return dl->req_blocking_mode != DTLS_BLOCKING_MODE_NONBLOCKING; |
3054 | 0 | } |
3055 | | |
3056 | | /* |
3057 | | * Report whether blocking mode can be provided for a DTLS listener or a |
3058 | | * connection created from one. |
3059 | | * |
3060 | | * Blocking is emulated by waiting for readiness of the listener's network |
3061 | | * socket, so it requires a BIO which can supply a poll descriptor to wait on. |
3062 | | * A memory BIO cannot, and such a listener is therefore non-blocking whatever |
3063 | | * was requested, as is the case for QUIC. |
3064 | | */ |
3065 | | static int ossl_dtls_can_support_blocking(const SSL *s) |
3066 | 0 | { |
3067 | 0 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s); |
3068 | 0 | const SSL *listener = NULL; |
3069 | 0 | BIO_POLL_DESCRIPTOR desc; |
3070 | 0 | BIO *rbio; |
3071 | |
|
3072 | 0 | if (sc != NULL && sc->d1 != NULL) |
3073 | 0 | listener = sc->d1->listener; |
3074 | 0 | else if (IS_DTLS_LISTENER(s)) |
3075 | 0 | listener = s; |
3076 | |
|
3077 | 0 | if (listener == NULL) |
3078 | 0 | return 0; |
3079 | | |
3080 | 0 | rbio = SSL_get_rbio(listener); |
3081 | 0 | if (rbio == NULL) |
3082 | 0 | return 0; |
3083 | | |
3084 | 0 | return BIO_get_rpoll_descriptor(rbio, &desc) != 0 |
3085 | 0 | && desc.type == BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD; |
3086 | 0 | } |
3087 | | |
3088 | | /* |
3089 | | * Report whether a call on this object should block, which is the case when |
3090 | | * blocking is both wanted and possible. |
3091 | | */ |
3092 | | int ossl_dtls_blocking(const SSL *s) |
3093 | 0 | { |
3094 | 0 | return ossl_dtls_desires_blocking(s) && ossl_dtls_can_support_blocking(s); |
3095 | 0 | } |
3096 | | |
3097 | | int ossl_dtls_set_blocking_mode(SSL *s, int blocking) |
3098 | 0 | { |
3099 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s); |
3100 | 0 | unsigned int mode = (blocking != 0) |
3101 | 0 | ? DTLS_BLOCKING_MODE_BLOCKING |
3102 | 0 | : DTLS_BLOCKING_MODE_NONBLOCKING; |
3103 | | |
3104 | | /* |
3105 | | * Only a listener, or a connection created from one, has a blocking mode. |
3106 | | * Any other DTLS object takes its behaviour from its own BIO in the |
3107 | | * traditional way, so there is nothing here to configure. |
3108 | | */ |
3109 | 0 | if (!IS_DTLS_LISTENER(s) |
3110 | 0 | && (sc == NULL || sc->d1 == NULL || sc->d1->listener == NULL)) { |
3111 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
3112 | 0 | return 0; |
3113 | 0 | } |
3114 | | |
3115 | | /* |
3116 | | * Refuse to claim blocking we cannot deliver, as QUIC does. Checked before |
3117 | | * anything is written, so that a call which fails leaves the mode alone |
3118 | | * rather than reporting failure having already changed it. |
3119 | | */ |
3120 | 0 | if (blocking && !ossl_dtls_can_support_blocking(s)) { |
3121 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_UNSUPPORTED); |
3122 | 0 | return 0; |
3123 | 0 | } |
3124 | | |
3125 | 0 | if (IS_DTLS_LISTENER(s)) |
3126 | 0 | ((DTLS_LISTENER *)s)->req_blocking_mode = mode; |
3127 | 0 | else |
3128 | 0 | sc->d1->req_blocking_mode = mode; |
3129 | |
|
3130 | 0 | return 1; |
3131 | 0 | } |
3132 | | |
3133 | | int ossl_dtls_get_blocking_mode(const SSL *s) |
3134 | 0 | { |
3135 | 0 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s); |
3136 | |
|
3137 | 0 | if (!IS_DTLS_LISTENER(s) |
3138 | 0 | && (sc == NULL || sc->d1 == NULL || sc->d1->listener == NULL)) |
3139 | 0 | return -1; |
3140 | | |
3141 | 0 | return ossl_dtls_blocking(s); |
3142 | 0 | } |
3143 | | |
3144 | | /* |
3145 | | * Wait until a datagram has been demultiplexed to this connection's receive |
3146 | | * queue, for a connection which is in blocking mode. |
3147 | | * |
3148 | | * This is what makes a blocking read on a listener based connection block. Such |
3149 | | * a connection has no BIO of its own to block in: it reads from a queue which |
3150 | | * the listener fills, so the wait has to happen here instead. |
3151 | | * |
3152 | | * A wakeup does not mean the datagram was ours - the listener's socket is |
3153 | | * shared, and another connection may be the one with data - so this loops until |
3154 | | * something actually lands in our queue. Events are handled after each wait |
3155 | | * because nothing else will do it while we are in here, and the retransmission |
3156 | | * timer needs servicing if it is what woke us. |
3157 | | * |
3158 | | * A datagram which is already waiting costs nothing: the wait pumps the |
3159 | | * listener's demux while translating the poll, and returns without sleeping if |
3160 | | * anything has been queued for us by then. |
3161 | | * |
3162 | | * Returns 1 if a datagram is now queued for this connection, or 0 if the wait |
3163 | | * could not be performed or the listener has failed. |
3164 | | */ |
3165 | | int ossl_dtls_conn_wait_for_datagram(SSL *s) |
3166 | 0 | { |
3167 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s); |
3168 | 0 | DTLS_LISTENER *dl; |
3169 | 0 | int empty; |
3170 | |
|
3171 | 0 | if (sc == NULL || sc->d1 == NULL || sc->d1->rx == NULL |
3172 | 0 | || sc->d1->listener == NULL) |
3173 | 0 | return 0; |
3174 | | |
3175 | 0 | dl = (DTLS_LISTENER *)sc->d1->listener; |
3176 | |
|
3177 | 0 | for (;;) { |
3178 | 0 | ossl_crypto_mutex_lock(dl->mutex); |
3179 | 0 | if (dl->fatal) { |
3180 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
3181 | 0 | return 0; |
3182 | 0 | } |
3183 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
3184 | | |
3185 | | /* |
3186 | | * An infinite deadline here is bounded by the connection's own event |
3187 | | * timeout, which the poll translation folds in, so this still wakes in |
3188 | | * time to retransmit. |
3189 | | */ |
3190 | 0 | if (!ossl_dtls_block_until_ready(s, SSL_POLL_EVENT_R, |
3191 | 0 | ossl_time_infinite(), /*bound_by_event_timeout=*/1)) |
3192 | 0 | return 0; |
3193 | | |
3194 | 0 | if (!SSL_handle_events(s)) |
3195 | 0 | return 0; |
3196 | | |
3197 | 0 | ossl_dgram_demux_pump(sc->d1->rx->demux); |
3198 | |
|
3199 | 0 | ossl_crypto_mutex_lock(sc->d1->rx->mutex); |
3200 | 0 | empty = ossl_list_urxe_is_empty(&sc->d1->rx->urxe_pending); |
3201 | 0 | ossl_crypto_mutex_unlock(sc->d1->rx->mutex); |
3202 | |
|
3203 | 0 | if (!empty) |
3204 | 0 | return 1; |
3205 | 0 | } |
3206 | 0 | } |
3207 | | |
3208 | | /* |
3209 | | * Wait until the listener's socket can accept another datagram, for a |
3210 | | * connection which is in blocking mode. |
3211 | | * |
3212 | | * The socket is shared with every other connection and is always |
3213 | | * non-blocking, so a send which cannot be completed has nowhere to wait. For |
3214 | | * DTLS the record layer would otherwise discard the datagram - a reasonable |
3215 | | * default for an unreliable transport, but not what an application which asked |
3216 | | * for blocking writes expects. |
3217 | | * |
3218 | | * Only one wait is performed. The caller retries the send, and comes back here |
3219 | | * if it still cannot proceed, so a wakeup which turns out not to leave room in |
3220 | | * the socket buffer costs an extra attempt rather than a lost datagram. |
3221 | | * |
3222 | | * The retransmission timer deliberately does not shorten this wait, unlike the |
3223 | | * one for a datagram above. There the wakeup is useful, because the wait can |
3224 | | * service the timer itself; here it cannot. Servicing it would mean |
3225 | | * retransmitting a flight from inside tls_retry_write_records(), which is |
3226 | | * part-way through sending one and holds write buffer state that a |
3227 | | * re-entrant do_dtls1_write() would clobber. Waking for a timer nothing then |
3228 | | * services would be worse than not waking: the timeout stays expired, and an |
3229 | | * expired timeout reads as a zero deadline, so every later wait would return |
3230 | | * at once and the caller's retry loop would spin without sleeping. Waiting for |
3231 | | * the socket alone is also what the send actually needs. Retransmission is not |
3232 | | * the right response to a flight which has not finished going out, and once it |
3233 | | * has, the state machine handles the timer as usual. |
3234 | | * |
3235 | | * Returns 1 if the send should be retried, or 0 if the wait could not be |
3236 | | * performed or the listener has failed. |
3237 | | */ |
3238 | | int ossl_dtls_conn_wait_for_write(SSL *s) |
3239 | 0 | { |
3240 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s); |
3241 | 0 | DTLS_LISTENER *dl; |
3242 | 0 | int fatal; |
3243 | |
|
3244 | 0 | if (sc == NULL || sc->d1 == NULL || sc->d1->listener == NULL) |
3245 | 0 | return 0; |
3246 | | |
3247 | 0 | dl = (DTLS_LISTENER *)sc->d1->listener; |
3248 | |
|
3249 | 0 | ossl_crypto_mutex_lock(dl->mutex); |
3250 | 0 | fatal = dl->fatal; |
3251 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
3252 | 0 | if (fatal) |
3253 | 0 | return 0; |
3254 | | |
3255 | 0 | return ossl_dtls_block_until_ready(s, SSL_POLL_EVENT_W, |
3256 | 0 | ossl_time_infinite(), /*bound_by_event_timeout=*/0); |
3257 | 0 | } |
3258 | | |
3259 | | void ossl_dtls_listener_enter_blocking_section(SSL *s) |
3260 | 0 | { |
3261 | 0 | DTLS_LISTENER *dl; |
3262 | |
|
3263 | 0 | if (!IS_DTLS_LISTENER(s)) |
3264 | 0 | return; |
3265 | | |
3266 | 0 | dl = (DTLS_LISTENER *)s; |
3267 | |
|
3268 | 0 | if (dl->have_notifier) { |
3269 | 0 | ossl_crypto_mutex_lock(dl->mutex); |
3270 | 0 | dl->cur_blocking_waiters++; |
3271 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
3272 | 0 | } |
3273 | 0 | } |
3274 | | |
3275 | | void ossl_dtls_listener_leave_blocking_section(SSL *s) |
3276 | 0 | { |
3277 | 0 | DTLS_LISTENER *dl; |
3278 | |
|
3279 | 0 | if (!IS_DTLS_LISTENER(s)) |
3280 | 0 | return; |
3281 | | |
3282 | 0 | dl = (DTLS_LISTENER *)s; |
3283 | |
|
3284 | 0 | if (dl->have_notifier) { |
3285 | 0 | ossl_crypto_mutex_lock(dl->mutex); |
3286 | |
|
3287 | 0 | assert(dl->cur_blocking_waiters > 0); |
3288 | 0 | --dl->cur_blocking_waiters; |
3289 | |
|
3290 | 0 | if (dl->signalled_notifier) { |
3291 | 0 | if (dl->cur_blocking_waiters == 0) { |
3292 | 0 | ossl_rio_notifier_unsignal(&dl->notifier); |
3293 | 0 | dl->signalled_notifier = 0; |
3294 | | |
3295 | | /* |
3296 | | * Release the other threads which have woken up |
3297 | | */ |
3298 | 0 | ossl_crypto_condvar_broadcast(dl->notifier_cv); |
3299 | 0 | } else { |
3300 | | /* We are not the last waiter out - so wait for that one. */ |
3301 | 0 | while (dl->signalled_notifier) |
3302 | 0 | ossl_crypto_condvar_wait(dl->notifier_cv, dl->mutex); |
3303 | 0 | } |
3304 | 0 | } |
3305 | |
|
3306 | 0 | ossl_crypto_mutex_unlock(dl->mutex); |
3307 | 0 | } |
3308 | 0 | } |
3309 | | |
3310 | | int ossl_dtls_listener_poll_events(SSL *s, uint64_t events, int do_tick, |
3311 | | uint64_t *revents) |
3312 | 0 | { |
3313 | 0 | DTLS_LISTENER *dl; |
3314 | 0 | uint64_t result = 0; |
3315 | |
|
3316 | 0 | if (!ossl_assert(IS_DTLS_LISTENER(s))) |
3317 | 0 | return 0; |
3318 | | |
3319 | 0 | dl = (DTLS_LISTENER *)s; |
3320 | |
|
3321 | 0 | if (do_tick) |
3322 | 0 | ossl_dtls_tick(dl); |
3323 | |
|
3324 | 0 | if ((events & SSL_POLL_EVENT_IC) != 0) { |
3325 | 0 | if (SSL_get_accept_connection_queue_len(s) > 0) |
3326 | 0 | result |= SSL_POLL_EVENT_IC; |
3327 | 0 | } |
3328 | |
|
3329 | 0 | if ((events & SSL_POLL_EVENT_R) != 0) { |
3330 | 0 | BIO *rbio = SSL_get_rbio(s); |
3331 | 0 | if (rbio != NULL && BIO_pending(rbio) > 0) |
3332 | 0 | result |= SSL_POLL_EVENT_R; |
3333 | 0 | } |
3334 | |
|
3335 | 0 | *revents = result; |
3336 | 0 | return 1; |
3337 | 0 | } |
3338 | | |
3339 | | int ossl_dtls_conn_poll_events(SSL *s, uint64_t events, int do_tick, |
3340 | | uint64_t *revents) |
3341 | 0 | { |
3342 | 0 | SSL_CONNECTION *sc; |
3343 | 0 | uint64_t result = 0; |
3344 | 0 | BIO_POLL_DESCRIPTOR desc; |
3345 | 0 | int has_pending; |
3346 | |
|
3347 | 0 | sc = SSL_CONNECTION_FROM_SSL(s); |
3348 | 0 | if (sc == NULL || sc->d1 == NULL) |
3349 | 0 | return 0; |
3350 | | |
3351 | | /* |
3352 | | * For DTLS connections that came from a listener, data arrives via |
3353 | | * URXEs injected by the listener's demux. When do_tick is set and |
3354 | | * we have a listener reference, pump the demux to get new data. |
3355 | | */ |
3356 | 0 | if (do_tick && sc->d1->listener != NULL) { |
3357 | 0 | DTLS_LISTENER *dl = (DTLS_LISTENER *)sc->d1->listener; |
3358 | 0 | ossl_dtls_tick(dl); |
3359 | 0 | } |
3360 | | |
3361 | | /* |
3362 | | * Handle events for the connection itself, which for DTLS means servicing |
3363 | | * the retransmission timer. The caller may have blocked until that timer |
3364 | | * expired, so if nothing retransmits here then nothing will, and the |
3365 | | * deadline would be recomputed as "now" on every subsequent wait. |
3366 | | * |
3367 | | * A failure here leaves the connection in a fatal error state, which the |
3368 | | * SSL_POLL_EVENT_EC check below reports. |
3369 | | */ |
3370 | 0 | if (do_tick) |
3371 | 0 | SSL_handle_events(s); |
3372 | |
|
3373 | 0 | if ((events & SSL_POLL_EVENT_R) != 0) { |
3374 | 0 | if (SSL_has_pending(s) || SSL_pending(s) > 0) { |
3375 | 0 | result |= SSL_POLL_EVENT_R; |
3376 | 0 | } else if (sc->d1->rx != NULL) { |
3377 | | /* Listener-based connection: check URXE queue */ |
3378 | 0 | ossl_crypto_mutex_lock(sc->d1->rx->mutex); |
3379 | 0 | has_pending = !ossl_list_urxe_is_empty(&sc->d1->rx->urxe_pending); |
3380 | 0 | ossl_crypto_mutex_unlock(sc->d1->rx->mutex); |
3381 | 0 | if (has_pending) |
3382 | 0 | result |= SSL_POLL_EVENT_R; |
3383 | 0 | } else { |
3384 | | /* |
3385 | | * Standalone DTLS SSL object (not from a listener). |
3386 | | * Check the underlying socket for readability. |
3387 | | */ |
3388 | 0 | BIO *rbio = SSL_get_rbio(s); |
3389 | |
|
3390 | 0 | if (rbio != NULL) { |
3391 | 0 | if (BIO_get_rpoll_descriptor(rbio, &desc) |
3392 | 0 | && desc.type == BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD |
3393 | 0 | && desc.value.fd >= 0) { |
3394 | 0 | if (BIO_socket_ready(desc.value.fd, 1) > 0) |
3395 | 0 | result |= SSL_POLL_EVENT_R; |
3396 | 0 | } else { |
3397 | | /* Checking non-socket BIO */ |
3398 | 0 | if (BIO_pending(rbio) > 0) |
3399 | 0 | result |= SSL_POLL_EVENT_R; |
3400 | 0 | } |
3401 | 0 | } |
3402 | 0 | } |
3403 | 0 | } |
3404 | |
|
3405 | 0 | if ((events & SSL_POLL_EVENT_W) != 0) { |
3406 | 0 | result |= SSL_POLL_EVENT_W; |
3407 | 0 | } |
3408 | |
|
3409 | 0 | if ((events & (SSL_POLL_EVENT_EC | SSL_POLL_EVENT_F)) != 0) { |
3410 | 0 | if (SSL_get_error(s, 0) == SSL_ERROR_SSL || SSL_get_shutdown(s) != 0) |
3411 | 0 | result |= SSL_POLL_EVENT_EC; |
3412 | 0 | } |
3413 | |
|
3414 | 0 | *revents = result; |
3415 | 0 | return 1; |
3416 | 0 | } |
3417 | | |
3418 | | #endif /* !OPENSSL_NO_DTLS && !OPENSSL_NO_SOCK */ |