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