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