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