/src/openssl34/ssl/d1_lib.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2005-2025 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 <stdio.h> |
12 | | #include <openssl/objects.h> |
13 | | #include <openssl/rand.h> |
14 | | #include "ssl_local.h" |
15 | | #include "internal/time.h" |
16 | | |
17 | | static int dtls1_handshake_write(SSL_CONNECTION *s); |
18 | | static size_t dtls1_link_min_mtu(void); |
19 | | |
20 | | /* XDTLS: figure out the right values */ |
21 | | static const size_t g_probable_mtu[] = { 1500, 512, 256 }; |
22 | | |
23 | | const SSL3_ENC_METHOD DTLSv1_enc_data = { |
24 | | tls1_setup_key_block, |
25 | | tls1_generate_master_secret, |
26 | | tls1_change_cipher_state, |
27 | | tls1_final_finish_mac, |
28 | | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
29 | | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
30 | | tls1_alert_code, |
31 | | tls1_export_keying_material, |
32 | | SSL_ENC_FLAG_DTLS, |
33 | | dtls1_set_handshake_header, |
34 | | dtls1_close_construct_packet, |
35 | | dtls1_handshake_write |
36 | | }; |
37 | | |
38 | | const SSL3_ENC_METHOD DTLSv1_2_enc_data = { |
39 | | tls1_setup_key_block, |
40 | | tls1_generate_master_secret, |
41 | | tls1_change_cipher_state, |
42 | | tls1_final_finish_mac, |
43 | | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
44 | | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
45 | | tls1_alert_code, |
46 | | tls1_export_keying_material, |
47 | | SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_SIGALGS |
48 | | | SSL_ENC_FLAG_SHA256_PRF | SSL_ENC_FLAG_TLS1_2_CIPHERS, |
49 | | dtls1_set_handshake_header, |
50 | | dtls1_close_construct_packet, |
51 | | dtls1_handshake_write |
52 | | }; |
53 | | |
54 | | OSSL_TIME dtls1_default_timeout(void) |
55 | 43.5k | { |
56 | | /* |
57 | | * 2 hours, the 24 hours mentioned in the DTLSv1 spec is way too long for |
58 | | * http, the cache would over fill |
59 | | */ |
60 | 43.5k | return ossl_seconds2time(60 * 60 * 2); |
61 | 43.5k | } |
62 | | |
63 | | int dtls1_new(SSL *ssl) |
64 | 35.8k | { |
65 | 35.8k | DTLS1_STATE *d1; |
66 | 35.8k | SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
67 | | |
68 | 35.8k | if (s == NULL) |
69 | 0 | return 0; |
70 | | |
71 | 35.8k | if (!DTLS_RECORD_LAYER_new(&s->rlayer)) { |
72 | 0 | return 0; |
73 | 0 | } |
74 | | |
75 | 35.8k | if (!ssl3_new(ssl)) |
76 | 0 | return 0; |
77 | 35.8k | if ((d1 = OPENSSL_zalloc(sizeof(*d1))) == NULL) { |
78 | 0 | ssl3_free(ssl); |
79 | 0 | return 0; |
80 | 0 | } |
81 | | |
82 | 35.8k | d1->buffered_messages = pqueue_new(); |
83 | 35.8k | d1->sent_messages = pqueue_new(); |
84 | | |
85 | 35.8k | if (s->server) { |
86 | 0 | d1->cookie_len = sizeof(s->d1->cookie); |
87 | 0 | } |
88 | | |
89 | 35.8k | d1->link_mtu = 0; |
90 | 35.8k | d1->mtu = 0; |
91 | | |
92 | 35.8k | if (d1->buffered_messages == NULL || d1->sent_messages == NULL) { |
93 | 0 | pqueue_free(d1->buffered_messages); |
94 | 0 | pqueue_free(d1->sent_messages); |
95 | 0 | OPENSSL_free(d1); |
96 | 0 | ssl3_free(ssl); |
97 | 0 | return 0; |
98 | 0 | } |
99 | | |
100 | 35.8k | s->d1 = d1; |
101 | | |
102 | 35.8k | if (!ssl->method->ssl_clear(ssl)) |
103 | 0 | return 0; |
104 | | |
105 | 35.8k | return 1; |
106 | 35.8k | } |
107 | | |
108 | | static void dtls1_clear_queues(SSL_CONNECTION *s) |
109 | 174k | { |
110 | 174k | dtls1_clear_received_buffer(s); |
111 | 174k | dtls1_clear_sent_buffer(s); |
112 | 174k | } |
113 | | |
114 | | void dtls1_clear_received_buffer(SSL_CONNECTION *s) |
115 | 182k | { |
116 | 182k | pitem *item = NULL; |
117 | 182k | hm_fragment *frag = NULL; |
118 | | |
119 | 188k | while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) { |
120 | 5.82k | frag = (hm_fragment *)item->data; |
121 | 5.82k | dtls1_hm_fragment_free(frag); |
122 | 5.82k | pitem_free(item); |
123 | 5.82k | } |
124 | 182k | } |
125 | | |
126 | | void dtls1_clear_sent_buffer(SSL_CONNECTION *s) |
127 | 194k | { |
128 | 194k | pitem *item = NULL; |
129 | 194k | hm_fragment *frag = NULL; |
130 | | |
131 | 272k | while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) { |
132 | 77.5k | frag = (hm_fragment *)item->data; |
133 | | |
134 | 77.5k | if (frag->msg_header.is_ccs |
135 | 2.77k | && frag->msg_header.saved_retransmit_state.wrlmethod != NULL |
136 | 2.77k | && s->rlayer.wrl != frag->msg_header.saved_retransmit_state.wrl) { |
137 | | /* |
138 | | * If we're freeing the CCS then we're done with the old wrl and it |
139 | | * can bee freed |
140 | | */ |
141 | 2.77k | frag->msg_header.saved_retransmit_state.wrlmethod->free(frag->msg_header.saved_retransmit_state.wrl); |
142 | 2.77k | } |
143 | | |
144 | 77.5k | dtls1_hm_fragment_free(frag); |
145 | 77.5k | pitem_free(item); |
146 | 77.5k | } |
147 | 194k | } |
148 | | |
149 | | void dtls1_free(SSL *ssl) |
150 | 43.5k | { |
151 | 43.5k | SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
152 | | |
153 | 43.5k | if (s == NULL) |
154 | 0 | return; |
155 | | |
156 | 43.5k | if (s->d1 != NULL) { |
157 | 43.5k | dtls1_clear_queues(s); |
158 | 43.5k | pqueue_free(s->d1->buffered_messages); |
159 | 43.5k | pqueue_free(s->d1->sent_messages); |
160 | 43.5k | } |
161 | | |
162 | 43.5k | DTLS_RECORD_LAYER_free(&s->rlayer); |
163 | | |
164 | 43.5k | ssl3_free(ssl); |
165 | | |
166 | 43.5k | OPENSSL_free(s->d1); |
167 | 43.5k | s->d1 = NULL; |
168 | 43.5k | } |
169 | | |
170 | | int dtls1_clear(SSL *ssl) |
171 | 143k | { |
172 | 143k | pqueue *buffered_messages; |
173 | 143k | pqueue *sent_messages; |
174 | 143k | size_t mtu; |
175 | 143k | size_t link_mtu; |
176 | | |
177 | 143k | SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
178 | | |
179 | 143k | if (s == NULL) |
180 | 0 | return 0; |
181 | | |
182 | 143k | DTLS_RECORD_LAYER_clear(&s->rlayer); |
183 | | |
184 | 143k | if (s->d1) { |
185 | 107k | DTLS_timer_cb timer_cb = s->d1->timer_cb; |
186 | | |
187 | 107k | buffered_messages = s->d1->buffered_messages; |
188 | 107k | sent_messages = s->d1->sent_messages; |
189 | 107k | mtu = s->d1->mtu; |
190 | 107k | link_mtu = s->d1->link_mtu; |
191 | | |
192 | 107k | dtls1_clear_queues(s); |
193 | | |
194 | 107k | memset(s->d1, 0, sizeof(*s->d1)); |
195 | | |
196 | | /* Restore the timer callback from previous state */ |
197 | 107k | s->d1->timer_cb = timer_cb; |
198 | | |
199 | 107k | if (s->server) { |
200 | 34.1k | s->d1->cookie_len = sizeof(s->d1->cookie); |
201 | 34.1k | } |
202 | | |
203 | 107k | if (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU) { |
204 | 0 | s->d1->mtu = mtu; |
205 | 0 | s->d1->link_mtu = link_mtu; |
206 | 0 | } |
207 | | |
208 | 107k | s->d1->buffered_messages = buffered_messages; |
209 | 107k | s->d1->sent_messages = sent_messages; |
210 | 107k | } |
211 | | |
212 | 143k | if (!ssl3_clear(ssl)) |
213 | 0 | return 0; |
214 | | |
215 | 143k | if (ssl->method->version == DTLS_ANY_VERSION) |
216 | 143k | s->version = DTLS_MAX_VERSION_INTERNAL; |
217 | 0 | #ifndef OPENSSL_NO_DTLS1_METHOD |
218 | 0 | else if (s->options & SSL_OP_CISCO_ANYCONNECT) |
219 | 0 | s->client_version = s->version = DTLS1_BAD_VER; |
220 | 0 | #endif |
221 | 0 | else |
222 | 0 | s->version = ssl->method->version; |
223 | | |
224 | 143k | return 1; |
225 | 143k | } |
226 | | |
227 | | long dtls1_ctrl(SSL *ssl, int cmd, long larg, void *parg) |
228 | 21.9k | { |
229 | 21.9k | int ret = 0; |
230 | 21.9k | OSSL_TIME t; |
231 | 21.9k | SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
232 | | |
233 | 21.9k | if (s == NULL) |
234 | 0 | return 0; |
235 | | |
236 | 21.9k | switch (cmd) { |
237 | 0 | case DTLS_CTRL_GET_TIMEOUT: |
238 | 0 | if (dtls1_get_timeout(s, &t)) { |
239 | 0 | *(struct timeval *)parg = ossl_time_to_timeval(t); |
240 | 0 | ret = 1; |
241 | 0 | } |
242 | 0 | break; |
243 | 0 | case DTLS_CTRL_HANDLE_TIMEOUT: |
244 | 0 | ret = dtls1_handle_timeout(s); |
245 | 0 | break; |
246 | 0 | case DTLS_CTRL_SET_LINK_MTU: |
247 | 0 | if (larg < (long)dtls1_link_min_mtu()) |
248 | 0 | return 0; |
249 | 0 | s->d1->link_mtu = larg; |
250 | 0 | return 1; |
251 | 0 | case DTLS_CTRL_GET_LINK_MIN_MTU: |
252 | 0 | return (long)dtls1_link_min_mtu(); |
253 | 0 | case SSL_CTRL_SET_MTU: |
254 | | /* |
255 | | * We may not have a BIO set yet so can't call dtls1_min_mtu() |
256 | | * We'll have to make do with dtls1_link_min_mtu() and max overhead |
257 | | */ |
258 | 0 | if (larg < (long)dtls1_link_min_mtu() - DTLS1_MAX_MTU_OVERHEAD) |
259 | 0 | return 0; |
260 | 0 | s->d1->mtu = larg; |
261 | 0 | return larg; |
262 | 21.9k | default: |
263 | 21.9k | ret = ssl3_ctrl(ssl, cmd, larg, parg); |
264 | 21.9k | break; |
265 | 21.9k | } |
266 | 21.9k | return ret; |
267 | 21.9k | } |
268 | | |
269 | | static void dtls1_bio_set_next_timeout(BIO *bio, const DTLS1_STATE *d1) |
270 | 98.0k | { |
271 | 98.0k | struct timeval tv = ossl_time_to_timeval(d1->next_timeout); |
272 | | |
273 | 98.0k | BIO_ctrl(bio, BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, &tv); |
274 | 98.0k | } |
275 | | |
276 | | void dtls1_start_timer(SSL_CONNECTION *s) |
277 | 77.5k | { |
278 | 77.5k | OSSL_TIME duration; |
279 | 77.5k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
280 | | |
281 | | #ifndef OPENSSL_NO_SCTP |
282 | | /* Disable timer for SCTP */ |
283 | | if (BIO_dgram_is_sctp(SSL_get_wbio(ssl))) { |
284 | | s->d1->next_timeout = ossl_time_zero(); |
285 | | return; |
286 | | } |
287 | | #endif |
288 | | |
289 | | /* |
290 | | * If timer is not set, initialize duration with 1 second or |
291 | | * a user-specified value if the timer callback is installed. |
292 | | */ |
293 | 77.5k | if (ossl_time_is_zero(s->d1->next_timeout)) { |
294 | 41.8k | if (s->d1->timer_cb != NULL) |
295 | 0 | s->d1->timeout_duration_us = s->d1->timer_cb(ssl, 0); |
296 | 41.8k | else |
297 | 41.8k | s->d1->timeout_duration_us = 1000000; |
298 | 41.8k | } |
299 | | |
300 | | /* Set timeout to current time plus duration */ |
301 | 77.5k | duration = ossl_us2time(s->d1->timeout_duration_us); |
302 | 77.5k | s->d1->next_timeout = ossl_time_add(ossl_time_now(), duration); |
303 | | |
304 | | /* set s->d1->next_timeout into ssl->rbio interface */ |
305 | 77.5k | dtls1_bio_set_next_timeout(SSL_get_rbio(ssl), s->d1); |
306 | 77.5k | } |
307 | | |
308 | | int dtls1_get_timeout(const SSL_CONNECTION *s, OSSL_TIME *timeleft) |
309 | 284k | { |
310 | 284k | OSSL_TIME timenow; |
311 | | |
312 | | /* If no timeout is set, just return NULL */ |
313 | 284k | if (ossl_time_is_zero(s->d1->next_timeout)) |
314 | 64.8k | return 0; |
315 | | |
316 | | /* Get current time */ |
317 | 219k | timenow = ossl_time_now(); |
318 | | |
319 | | /* |
320 | | * If timer already expired or if remaining time is less than 15 ms, |
321 | | * set it to 0 to prevent issues because of small divergences with |
322 | | * socket timeouts. |
323 | | */ |
324 | 219k | *timeleft = ossl_time_subtract(s->d1->next_timeout, timenow); |
325 | 219k | if (ossl_time_compare(*timeleft, ossl_ms2time(15)) <= 0) |
326 | 0 | *timeleft = ossl_time_zero(); |
327 | 219k | return 1; |
328 | 284k | } |
329 | | |
330 | | int dtls1_is_timer_expired(SSL_CONNECTION *s) |
331 | 284k | { |
332 | 284k | OSSL_TIME timeleft; |
333 | | |
334 | | /* Get time left until timeout, return false if no timer running */ |
335 | 284k | if (!dtls1_get_timeout(s, &timeleft)) |
336 | 64.8k | return 0; |
337 | | |
338 | | /* Return false if timer is not expired yet */ |
339 | 219k | if (!ossl_time_is_zero(timeleft)) |
340 | 219k | return 0; |
341 | | |
342 | | /* Timer expired, so return true */ |
343 | 0 | return 1; |
344 | 219k | } |
345 | | |
346 | | static void dtls1_double_timeout(SSL_CONNECTION *s) |
347 | 0 | { |
348 | 0 | s->d1->timeout_duration_us *= 2; |
349 | 0 | if (s->d1->timeout_duration_us > 60000000) |
350 | 0 | s->d1->timeout_duration_us = 60000000; |
351 | 0 | } |
352 | | |
353 | | void dtls1_stop_timer(SSL_CONNECTION *s) |
354 | 20.5k | { |
355 | | /* Reset everything */ |
356 | 20.5k | s->d1->timeout_num_alerts = 0; |
357 | 20.5k | s->d1->next_timeout = ossl_time_zero(); |
358 | 20.5k | s->d1->timeout_duration_us = 1000000; |
359 | 20.5k | dtls1_bio_set_next_timeout(s->rbio, s->d1); |
360 | | /* Clear retransmission buffer */ |
361 | 20.5k | dtls1_clear_sent_buffer(s); |
362 | 20.5k | } |
363 | | |
364 | | int dtls1_check_timeout_num(SSL_CONNECTION *s) |
365 | 0 | { |
366 | 0 | size_t mtu; |
367 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
368 | |
|
369 | 0 | s->d1->timeout_num_alerts++; |
370 | | |
371 | | /* Reduce MTU after 2 unsuccessful retransmissions */ |
372 | 0 | if (s->d1->timeout_num_alerts > 2 |
373 | 0 | && !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) { |
374 | 0 | mtu = BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, NULL); |
375 | 0 | if (mtu < s->d1->mtu) |
376 | 0 | s->d1->mtu = mtu; |
377 | 0 | } |
378 | |
|
379 | 0 | if (s->d1->timeout_num_alerts > DTLS1_TMO_ALERT_COUNT) { |
380 | | /* fail the connection, enough alerts have been sent */ |
381 | 0 | SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_READ_TIMEOUT_EXPIRED); |
382 | 0 | return -1; |
383 | 0 | } |
384 | | |
385 | 0 | return 0; |
386 | 0 | } |
387 | | |
388 | | int dtls1_handle_timeout(SSL_CONNECTION *s) |
389 | 265k | { |
390 | | /* if no timer is expired, don't do anything */ |
391 | 265k | if (!dtls1_is_timer_expired(s)) { |
392 | 265k | return 0; |
393 | 265k | } |
394 | | |
395 | 0 | if (s->d1->timer_cb != NULL) |
396 | 0 | s->d1->timeout_duration_us = s->d1->timer_cb(SSL_CONNECTION_GET_USER_SSL(s), |
397 | 0 | s->d1->timeout_duration_us); |
398 | 0 | else |
399 | 0 | dtls1_double_timeout(s); |
400 | |
|
401 | 0 | if (dtls1_check_timeout_num(s) < 0) { |
402 | | /* SSLfatal() already called */ |
403 | 0 | return -1; |
404 | 0 | } |
405 | | |
406 | 0 | dtls1_start_timer(s); |
407 | | /* Calls SSLfatal() if required */ |
408 | 0 | return dtls1_retransmit_buffered_messages(s); |
409 | 0 | } |
410 | | |
411 | 0 | #define LISTEN_SUCCESS 2 |
412 | 0 | #define LISTEN_SEND_VERIFY_REQUEST 1 |
413 | | |
414 | | #ifndef OPENSSL_NO_SOCK |
415 | | int DTLSv1_listen(SSL *ssl, BIO_ADDR *client) |
416 | 0 | { |
417 | 0 | int next, n, ret = 0; |
418 | 0 | unsigned char cookie[DTLS1_COOKIE_LENGTH]; |
419 | 0 | unsigned char seq[SEQ_NUM_SIZE]; |
420 | 0 | const unsigned char *data; |
421 | 0 | unsigned char *buf = NULL, *wbuf; |
422 | 0 | size_t fragoff, fraglen, msglen; |
423 | 0 | unsigned int rectype, versmajor, versminor, msgseq, msgtype, clientvers, cookielen; |
424 | 0 | BIO *rbio, *wbio; |
425 | 0 | BIO_ADDR *tmpclient = NULL; |
426 | 0 | PACKET pkt, msgpkt, msgpayload, session, cookiepkt; |
427 | 0 | SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
428 | |
|
429 | 0 | if (s == NULL) |
430 | 0 | return -1; |
431 | | |
432 | 0 | if (s->handshake_func == NULL) { |
433 | | /* Not properly initialized yet */ |
434 | 0 | SSL_set_accept_state(ssl); |
435 | 0 | } |
436 | | |
437 | | /* Ensure there is no state left over from a previous invocation */ |
438 | 0 | if (!SSL_clear(ssl)) |
439 | 0 | return -1; |
440 | | |
441 | 0 | ERR_clear_error(); |
442 | |
|
443 | 0 | rbio = SSL_get_rbio(ssl); |
444 | 0 | wbio = SSL_get_wbio(ssl); |
445 | |
|
446 | 0 | if (!rbio || !wbio) { |
447 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_BIO_NOT_SET); |
448 | 0 | return -1; |
449 | 0 | } |
450 | | |
451 | | /* |
452 | | * Note: This check deliberately excludes DTLS1_BAD_VER because that version |
453 | | * requires the MAC to be calculated *including* the first ClientHello |
454 | | * (without the cookie). Since DTLSv1_listen is stateless that cannot be |
455 | | * supported. DTLS1_BAD_VER must use cookies in a stateful manner (e.g. via |
456 | | * SSL_accept) |
457 | | */ |
458 | 0 | if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) { |
459 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_SSL_VERSION); |
460 | 0 | return -1; |
461 | 0 | } |
462 | | |
463 | 0 | buf = OPENSSL_malloc(DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_PLAIN_LENGTH); |
464 | 0 | if (buf == NULL) |
465 | 0 | return -1; |
466 | 0 | wbuf = OPENSSL_malloc(DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_PLAIN_LENGTH); |
467 | 0 | if (wbuf == NULL) { |
468 | 0 | OPENSSL_free(buf); |
469 | 0 | return -1; |
470 | 0 | } |
471 | | |
472 | 0 | do { |
473 | | /* Get a packet */ |
474 | |
|
475 | 0 | clear_sys_error(); |
476 | 0 | n = BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH + DTLS1_RT_HEADER_LENGTH); |
477 | 0 | if (n <= 0) { |
478 | 0 | if (BIO_should_retry(rbio)) { |
479 | | /* Non-blocking IO */ |
480 | 0 | goto end; |
481 | 0 | } |
482 | 0 | ret = -1; |
483 | 0 | goto end; |
484 | 0 | } |
485 | | |
486 | 0 | if (!PACKET_buf_init(&pkt, buf, n)) { |
487 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR); |
488 | 0 | ret = -1; |
489 | 0 | goto end; |
490 | 0 | } |
491 | | |
492 | | /* |
493 | | * Parse the received record. If there are any problems with it we just |
494 | | * dump it - with no alert. RFC6347 says this "Unlike TLS, DTLS is |
495 | | * resilient in the face of invalid records (e.g., invalid formatting, |
496 | | * length, MAC, etc.). In general, invalid records SHOULD be silently |
497 | | * discarded, thus preserving the association; however, an error MAY be |
498 | | * logged for diagnostic purposes." |
499 | | */ |
500 | | |
501 | | /* this packet contained a partial record, dump it */ |
502 | 0 | if (n < DTLS1_RT_HEADER_LENGTH) { |
503 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_RECORD_TOO_SMALL); |
504 | 0 | goto end; |
505 | 0 | } |
506 | | |
507 | | /* Get the record header */ |
508 | 0 | if (!PACKET_get_1(&pkt, &rectype) |
509 | 0 | || !PACKET_get_1(&pkt, &versmajor) |
510 | 0 | || !PACKET_get_1(&pkt, &versminor)) { |
511 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_LENGTH_MISMATCH); |
512 | 0 | goto end; |
513 | 0 | } |
514 | | |
515 | 0 | if (s->msg_callback) |
516 | 0 | s->msg_callback(0, (versmajor << 8) | versminor, SSL3_RT_HEADER, buf, |
517 | 0 | DTLS1_RT_HEADER_LENGTH, ssl, s->msg_callback_arg); |
518 | |
|
519 | 0 | if (rectype != SSL3_RT_HANDSHAKE) { |
520 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_UNEXPECTED_MESSAGE); |
521 | 0 | goto end; |
522 | 0 | } |
523 | | |
524 | | /* |
525 | | * Check record version number. We only check that the major version is |
526 | | * the same. |
527 | | */ |
528 | 0 | if (versmajor != DTLS1_VERSION_MAJOR) { |
529 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_BAD_PROTOCOL_VERSION_NUMBER); |
530 | 0 | goto end; |
531 | 0 | } |
532 | | |
533 | | /* Save the sequence number: 64 bits, with top 2 bytes = epoch */ |
534 | 0 | if (!PACKET_copy_bytes(&pkt, seq, SEQ_NUM_SIZE) |
535 | 0 | || !PACKET_get_length_prefixed_2(&pkt, &msgpkt)) { |
536 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_LENGTH_MISMATCH); |
537 | 0 | goto end; |
538 | 0 | } |
539 | | /* |
540 | | * We allow data remaining at the end of the packet because there could |
541 | | * be a second record (but we ignore it) |
542 | | */ |
543 | | |
544 | | /* This is an initial ClientHello so the epoch has to be 0 */ |
545 | 0 | if (seq[0] != 0 || seq[1] != 0) { |
546 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_UNEXPECTED_MESSAGE); |
547 | 0 | goto end; |
548 | 0 | } |
549 | | |
550 | | /* Get a pointer to the raw message for the later callback */ |
551 | 0 | data = PACKET_data(&msgpkt); |
552 | | |
553 | | /* Finished processing the record header, now process the message */ |
554 | 0 | if (!PACKET_get_1(&msgpkt, &msgtype) |
555 | 0 | || !PACKET_get_net_3_len(&msgpkt, &msglen) |
556 | 0 | || !PACKET_get_net_2(&msgpkt, &msgseq) |
557 | 0 | || !PACKET_get_net_3_len(&msgpkt, &fragoff) |
558 | 0 | || !PACKET_get_net_3_len(&msgpkt, &fraglen) |
559 | 0 | || !PACKET_get_sub_packet(&msgpkt, &msgpayload, fraglen) |
560 | 0 | || PACKET_remaining(&msgpkt) != 0) { |
561 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_LENGTH_MISMATCH); |
562 | 0 | goto end; |
563 | 0 | } |
564 | | |
565 | 0 | if (msgtype != SSL3_MT_CLIENT_HELLO) { |
566 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_UNEXPECTED_MESSAGE); |
567 | 0 | goto end; |
568 | 0 | } |
569 | | |
570 | | /* Message sequence number can only be 0 or 1 */ |
571 | 0 | if (msgseq > 1) { |
572 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SEQUENCE_NUMBER); |
573 | 0 | goto end; |
574 | 0 | } |
575 | | |
576 | | /* |
577 | | * We don't support fragment reassembly for ClientHellos whilst |
578 | | * listening because that would require server side state (which is |
579 | | * against the whole point of the ClientHello/HelloVerifyRequest |
580 | | * mechanism). Instead we only look at the first ClientHello fragment |
581 | | * and require that the cookie must be contained within it. |
582 | | */ |
583 | 0 | if (fragoff != 0 || fraglen > msglen) { |
584 | | /* Non initial ClientHello fragment (or bad fragment) */ |
585 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_FRAGMENTED_CLIENT_HELLO); |
586 | 0 | goto end; |
587 | 0 | } |
588 | | |
589 | 0 | if (s->msg_callback) |
590 | 0 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, data, |
591 | 0 | fraglen + DTLS1_HM_HEADER_LENGTH, ssl, |
592 | 0 | s->msg_callback_arg); |
593 | |
|
594 | 0 | if (!PACKET_get_net_2(&msgpayload, &clientvers)) { |
595 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_LENGTH_MISMATCH); |
596 | 0 | goto end; |
597 | 0 | } |
598 | | |
599 | | /* |
600 | | * Verify client version is supported |
601 | | */ |
602 | 0 | if (DTLS_VERSION_LT(clientvers, (unsigned int)ssl->method->version) && ssl->method->version != DTLS_ANY_VERSION) { |
603 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_VERSION_NUMBER); |
604 | 0 | goto end; |
605 | 0 | } |
606 | | |
607 | 0 | if (!PACKET_forward(&msgpayload, SSL3_RANDOM_SIZE) |
608 | 0 | || !PACKET_get_length_prefixed_1(&msgpayload, &session) |
609 | 0 | || !PACKET_get_length_prefixed_1(&msgpayload, &cookiepkt)) { |
610 | | /* |
611 | | * Could be malformed or the cookie does not fit within the initial |
612 | | * ClientHello fragment. Either way we can't handle it. |
613 | | */ |
614 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_LENGTH_MISMATCH); |
615 | 0 | goto end; |
616 | 0 | } |
617 | | |
618 | | /* |
619 | | * Check if we have a cookie or not. If not we need to send a |
620 | | * HelloVerifyRequest. |
621 | | */ |
622 | 0 | if (PACKET_remaining(&cookiepkt) == 0) { |
623 | 0 | next = LISTEN_SEND_VERIFY_REQUEST; |
624 | 0 | } else { |
625 | | /* |
626 | | * We have a cookie, so lets check it. |
627 | | */ |
628 | 0 | if (ssl->ctx->app_verify_cookie_cb == NULL) { |
629 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_NO_VERIFY_COOKIE_CALLBACK); |
630 | | /* This is fatal */ |
631 | 0 | ret = -1; |
632 | 0 | goto end; |
633 | 0 | } |
634 | 0 | if (ssl->ctx->app_verify_cookie_cb(ssl, PACKET_data(&cookiepkt), |
635 | 0 | (unsigned int)PACKET_remaining(&cookiepkt)) |
636 | 0 | == 0) { |
637 | | /* |
638 | | * We treat invalid cookies in the same was as no cookie as |
639 | | * per RFC6347 |
640 | | */ |
641 | 0 | next = LISTEN_SEND_VERIFY_REQUEST; |
642 | 0 | } else { |
643 | | /* Cookie verification succeeded */ |
644 | 0 | next = LISTEN_SUCCESS; |
645 | 0 | } |
646 | 0 | } |
647 | | |
648 | 0 | if (next == LISTEN_SEND_VERIFY_REQUEST) { |
649 | 0 | WPACKET wpkt; |
650 | 0 | unsigned int version; |
651 | 0 | size_t wreclen; |
652 | | |
653 | | /* |
654 | | * There was no cookie in the ClientHello so we need to send a |
655 | | * HelloVerifyRequest. If this fails we do not worry about trying |
656 | | * to resend, we just drop it. |
657 | | */ |
658 | | |
659 | | /* Generate the cookie */ |
660 | 0 | if (ssl->ctx->app_gen_cookie_cb == NULL || ssl->ctx->app_gen_cookie_cb(ssl, cookie, &cookielen) == 0 || cookielen > 255) { |
661 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_COOKIE_GEN_CALLBACK_FAILURE); |
662 | | /* This is fatal */ |
663 | 0 | ret = -1; |
664 | 0 | goto end; |
665 | 0 | } |
666 | | |
667 | | /* |
668 | | * Special case: for hello verify request, client version 1.0 and we |
669 | | * haven't decided which version to use yet send back using version |
670 | | * 1.0 header: otherwise some clients will ignore it. |
671 | | */ |
672 | 0 | version = (ssl->method->version == DTLS_ANY_VERSION) ? DTLS1_VERSION |
673 | 0 | : s->version; |
674 | | |
675 | | /* Construct the record and message headers */ |
676 | 0 | if (!WPACKET_init_static_len(&wpkt, |
677 | 0 | wbuf, |
678 | 0 | ssl_get_max_send_fragment(s) |
679 | 0 | + DTLS1_RT_HEADER_LENGTH, |
680 | 0 | 0) |
681 | 0 | || !WPACKET_put_bytes_u8(&wpkt, SSL3_RT_HANDSHAKE) |
682 | 0 | || !WPACKET_put_bytes_u16(&wpkt, version) |
683 | | /* |
684 | | * Record sequence number is always the same as in the |
685 | | * received ClientHello |
686 | | */ |
687 | 0 | || !WPACKET_memcpy(&wpkt, seq, SEQ_NUM_SIZE) |
688 | | /* End of record, start sub packet for message */ |
689 | 0 | || !WPACKET_start_sub_packet_u16(&wpkt) |
690 | | /* Message type */ |
691 | 0 | || !WPACKET_put_bytes_u8(&wpkt, |
692 | 0 | DTLS1_MT_HELLO_VERIFY_REQUEST) |
693 | | /* |
694 | | * Message length - doesn't follow normal TLS convention: |
695 | | * the length isn't the last thing in the message header. |
696 | | * We'll need to fill this in later when we know the |
697 | | * length. Set it to zero for now |
698 | | */ |
699 | 0 | || !WPACKET_put_bytes_u24(&wpkt, 0) |
700 | | /* |
701 | | * Message sequence number is always 0 for a |
702 | | * HelloVerifyRequest |
703 | | */ |
704 | 0 | || !WPACKET_put_bytes_u16(&wpkt, 0) |
705 | | /* |
706 | | * We never fragment a HelloVerifyRequest, so fragment |
707 | | * offset is 0 |
708 | | */ |
709 | 0 | || !WPACKET_put_bytes_u24(&wpkt, 0) |
710 | | /* |
711 | | * Fragment length is the same as message length, but |
712 | | * this *is* the last thing in the message header so we |
713 | | * can just start a sub-packet. No need to come back |
714 | | * later for this one. |
715 | | */ |
716 | 0 | || !WPACKET_start_sub_packet_u24(&wpkt) |
717 | | /* Create the actual HelloVerifyRequest body */ |
718 | 0 | || !dtls_raw_hello_verify_request(&wpkt, cookie, cookielen) |
719 | | /* Close message body */ |
720 | 0 | || !WPACKET_close(&wpkt) |
721 | | /* Close record body */ |
722 | 0 | || !WPACKET_close(&wpkt) |
723 | 0 | || !WPACKET_get_total_written(&wpkt, &wreclen) |
724 | 0 | || !WPACKET_finish(&wpkt)) { |
725 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR); |
726 | 0 | WPACKET_cleanup(&wpkt); |
727 | | /* This is fatal */ |
728 | 0 | ret = -1; |
729 | 0 | goto end; |
730 | 0 | } |
731 | | |
732 | | /* |
733 | | * Fix up the message len in the message header. Its the same as the |
734 | | * fragment len which has been filled in by WPACKET, so just copy |
735 | | * that. Destination for the message len is after the record header |
736 | | * plus one byte for the message content type. The source is the |
737 | | * last 3 bytes of the message header |
738 | | */ |
739 | 0 | memcpy(&wbuf[DTLS1_RT_HEADER_LENGTH + 1], |
740 | 0 | &wbuf[DTLS1_RT_HEADER_LENGTH + DTLS1_HM_HEADER_LENGTH - 3], |
741 | 0 | 3); |
742 | |
|
743 | 0 | if (s->msg_callback) |
744 | 0 | s->msg_callback(1, version, SSL3_RT_HEADER, wbuf, |
745 | 0 | DTLS1_RT_HEADER_LENGTH, ssl, |
746 | 0 | s->msg_callback_arg); |
747 | |
|
748 | 0 | if ((tmpclient = BIO_ADDR_new()) == NULL) { |
749 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_BIO_LIB); |
750 | 0 | goto end; |
751 | 0 | } |
752 | | |
753 | | /* |
754 | | * This is unnecessary if rbio and wbio are one and the same - but |
755 | | * maybe they're not. We ignore errors here - some BIOs do not |
756 | | * support this. |
757 | | */ |
758 | 0 | if (BIO_dgram_get_peer(rbio, tmpclient) > 0) { |
759 | 0 | (void)BIO_dgram_set_peer(wbio, tmpclient); |
760 | 0 | } |
761 | 0 | BIO_ADDR_free(tmpclient); |
762 | 0 | tmpclient = NULL; |
763 | |
|
764 | 0 | if (BIO_write(wbio, wbuf, wreclen) < (int)wreclen) { |
765 | 0 | if (BIO_should_retry(wbio)) { |
766 | | /* |
767 | | * Non-blocking IO...but we're stateless, so we're just |
768 | | * going to drop this packet. |
769 | | */ |
770 | 0 | goto end; |
771 | 0 | } |
772 | 0 | ret = -1; |
773 | 0 | goto end; |
774 | 0 | } |
775 | | |
776 | 0 | if (BIO_flush(wbio) <= 0) { |
777 | 0 | if (BIO_should_retry(wbio)) { |
778 | | /* |
779 | | * Non-blocking IO...but we're stateless, so we're just |
780 | | * going to drop this packet. |
781 | | */ |
782 | 0 | goto end; |
783 | 0 | } |
784 | 0 | ret = -1; |
785 | 0 | goto end; |
786 | 0 | } |
787 | 0 | } |
788 | 0 | } while (next != LISTEN_SUCCESS); |
789 | | |
790 | | /* |
791 | | * Set expected sequence numbers to continue the handshake. |
792 | | */ |
793 | 0 | s->d1->handshake_read_seq = 1; |
794 | 0 | s->d1->handshake_write_seq = 1; |
795 | 0 | s->d1->next_handshake_write_seq = 1; |
796 | 0 | s->rlayer.wrlmethod->increment_sequence_ctr(s->rlayer.wrl); |
797 | | |
798 | | /* |
799 | | * We are doing cookie exchange, so make sure we set that option in the |
800 | | * SSL object |
801 | | */ |
802 | 0 | SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE); |
803 | | |
804 | | /* |
805 | | * Tell the state machine that we've done the initial hello verify |
806 | | * exchange |
807 | | */ |
808 | 0 | ossl_statem_set_hello_verify_done(s); |
809 | | |
810 | | /* |
811 | | * Some BIOs may not support this. If we fail we clear the client address |
812 | | */ |
813 | 0 | if (BIO_dgram_get_peer(rbio, client) <= 0) |
814 | 0 | BIO_ADDR_clear(client); |
815 | | |
816 | | /* Buffer the record for use by the record layer */ |
817 | 0 | if (BIO_write(s->rlayer.rrlnext, buf, n) != n) { |
818 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR); |
819 | 0 | ret = -1; |
820 | 0 | goto end; |
821 | 0 | } |
822 | | |
823 | | /* |
824 | | * Reset the record layer - but this time we can use the record we just |
825 | | * buffered in s->rlayer.rrlnext |
826 | | */ |
827 | 0 | if (!ssl_set_new_record_layer(s, |
828 | 0 | DTLS_ANY_VERSION, |
829 | 0 | OSSL_RECORD_DIRECTION_READ, |
830 | 0 | OSSL_RECORD_PROTECTION_LEVEL_NONE, NULL, 0, |
831 | 0 | NULL, 0, NULL, 0, NULL, 0, NULL, 0, |
832 | 0 | NID_undef, NULL, NULL, NULL)) { |
833 | | /* SSLfatal already called */ |
834 | 0 | ret = -1; |
835 | 0 | goto end; |
836 | 0 | } |
837 | | |
838 | 0 | ret = 1; |
839 | 0 | end: |
840 | 0 | BIO_ADDR_free(tmpclient); |
841 | 0 | OPENSSL_free(buf); |
842 | 0 | OPENSSL_free(wbuf); |
843 | 0 | return ret; |
844 | 0 | } |
845 | | #endif |
846 | | |
847 | | static int dtls1_handshake_write(SSL_CONNECTION *s) |
848 | 74.7k | { |
849 | 74.7k | return dtls1_do_write(s, SSL3_RT_HANDSHAKE); |
850 | 74.7k | } |
851 | | |
852 | | int dtls1_shutdown(SSL *s) |
853 | 0 | { |
854 | 0 | int ret; |
855 | | #ifndef OPENSSL_NO_SCTP |
856 | | BIO *wbio; |
857 | | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s); |
858 | | |
859 | | if (sc == NULL) |
860 | | return -1; |
861 | | |
862 | | wbio = SSL_get_wbio(s); |
863 | | if (wbio != NULL && BIO_dgram_is_sctp(wbio) && !(sc->shutdown & SSL_SENT_SHUTDOWN)) { |
864 | | ret = BIO_dgram_sctp_wait_for_dry(wbio); |
865 | | if (ret < 0) |
866 | | return -1; |
867 | | |
868 | | if (ret == 0) |
869 | | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN, 1, |
870 | | NULL); |
871 | | } |
872 | | #endif |
873 | 0 | ret = ssl3_shutdown(s); |
874 | | #ifndef OPENSSL_NO_SCTP |
875 | | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN, 0, NULL); |
876 | | #endif |
877 | 0 | return ret; |
878 | 0 | } |
879 | | |
880 | | int dtls1_query_mtu(SSL_CONNECTION *s) |
881 | 77.5k | { |
882 | 77.5k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
883 | | |
884 | 77.5k | if (s->d1->link_mtu) { |
885 | 0 | s->d1->mtu = s->d1->link_mtu - BIO_dgram_get_mtu_overhead(SSL_get_wbio(ssl)); |
886 | 0 | s->d1->link_mtu = 0; |
887 | 0 | } |
888 | | |
889 | | /* AHA! Figure out the MTU, and stick to the right size */ |
890 | 77.5k | if (s->d1->mtu < dtls1_min_mtu(s)) { |
891 | 34.7k | if (!(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) { |
892 | 34.7k | s->d1->mtu = BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL); |
893 | | |
894 | | /* |
895 | | * I've seen the kernel return bogus numbers when it doesn't know |
896 | | * (initial write), so just make sure we have a reasonable number |
897 | | */ |
898 | 34.7k | if (s->d1->mtu < dtls1_min_mtu(s)) { |
899 | | /* Set to min mtu */ |
900 | 34.7k | s->d1->mtu = dtls1_min_mtu(s); |
901 | 34.7k | BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SET_MTU, |
902 | 34.7k | (long)s->d1->mtu, NULL); |
903 | 34.7k | } |
904 | 34.7k | } else |
905 | 0 | return 0; |
906 | 34.7k | } |
907 | 77.5k | return 1; |
908 | 77.5k | } |
909 | | |
910 | | static size_t dtls1_link_min_mtu(void) |
911 | 224k | { |
912 | 224k | return (g_probable_mtu[(sizeof(g_probable_mtu) / sizeof(g_probable_mtu[0])) - 1]); |
913 | 224k | } |
914 | | |
915 | | size_t dtls1_min_mtu(SSL_CONNECTION *s) |
916 | 224k | { |
917 | 224k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
918 | | |
919 | 224k | return dtls1_link_min_mtu() - BIO_dgram_get_mtu_overhead(SSL_get_wbio(ssl)); |
920 | 224k | } |
921 | | |
922 | | size_t DTLS_get_data_mtu(const SSL *ssl) |
923 | 0 | { |
924 | 0 | size_t mac_overhead, int_overhead, blocksize, ext_overhead; |
925 | 0 | const SSL_CIPHER *ciph = SSL_get_current_cipher(ssl); |
926 | 0 | size_t mtu; |
927 | 0 | const SSL_CONNECTION *s = SSL_CONNECTION_FROM_CONST_SSL_ONLY(ssl); |
928 | |
|
929 | 0 | if (s == NULL) |
930 | 0 | return 0; |
931 | | |
932 | 0 | mtu = s->d1->mtu; |
933 | |
|
934 | 0 | if (ciph == NULL) |
935 | 0 | return 0; |
936 | | |
937 | 0 | if (!ssl_cipher_get_overhead(ciph, &mac_overhead, &int_overhead, |
938 | 0 | &blocksize, &ext_overhead)) |
939 | 0 | return 0; |
940 | | |
941 | 0 | if (SSL_READ_ETM(s)) |
942 | 0 | ext_overhead += mac_overhead; |
943 | 0 | else |
944 | 0 | int_overhead += mac_overhead; |
945 | | |
946 | | /* Subtract external overhead (e.g. IV/nonce, separate MAC) */ |
947 | 0 | if (ext_overhead + DTLS1_RT_HEADER_LENGTH >= mtu) |
948 | 0 | return 0; |
949 | 0 | mtu -= ext_overhead + DTLS1_RT_HEADER_LENGTH; |
950 | | |
951 | | /* Round encrypted payload down to cipher block size (for CBC etc.) |
952 | | * No check for overflow since 'mtu % blocksize' cannot exceed mtu. */ |
953 | 0 | if (blocksize) |
954 | 0 | mtu -= (mtu % blocksize); |
955 | | |
956 | | /* Subtract internal overhead (e.g. CBC padding len byte) */ |
957 | 0 | if (int_overhead >= mtu) |
958 | 0 | return 0; |
959 | 0 | mtu -= int_overhead; |
960 | |
|
961 | 0 | return mtu; |
962 | 0 | } |
963 | | |
964 | | void DTLS_set_timer_cb(SSL *ssl, DTLS_timer_cb cb) |
965 | 0 | { |
966 | 0 | SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
967 | |
|
968 | 0 | if (s == NULL) |
969 | 0 | return; |
970 | | |
971 | 0 | s->d1->timer_cb = cb; |
972 | 0 | } |