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