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