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