/src/openssl/ssl/statem/statem_dtls.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 2005-2025 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  *  | 
4  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use  | 
5  |  |  * this file except in compliance with the License.  You can obtain a copy  | 
6  |  |  * in the file LICENSE in the source distribution or at  | 
7  |  |  * https://www.openssl.org/source/license.html  | 
8  |  |  */  | 
9  |  |  | 
10  |  | #include <assert.h>  | 
11  |  | #include <limits.h>  | 
12  |  | #include <string.h>  | 
13  |  | #include <stdio.h>  | 
14  |  | #include "../ssl_local.h"  | 
15  |  | #include "statem_local.h"  | 
16  |  | #include "internal/cryptlib.h"  | 
17  |  | #include "internal/ssl_unwrap.h"  | 
18  |  | #include <openssl/buffer.h>  | 
19  |  |  | 
20  |  | #define RSMBLY_BITMASK_SIZE(msg_len) (((msg_len) + 7) / 8)  | 
21  |  |  | 
22  | 0  | #define RSMBLY_BITMASK_MARK(bitmask, start, end) { \ | 
23  | 0  |                         if ((end) - (start) <= 8) { \ | 
24  | 0  |                                 long ii; \  | 
25  | 0  |                                 for (ii = (start); ii < (end); ii++) bitmask[((ii) >> 3)] |= (1 << ((ii) & 7)); \  | 
26  | 0  |                         } else { \ | 
27  | 0  |                                 long ii; \  | 
28  | 0  |                                 bitmask[((start) >> 3)] |= bitmask_start_values[((start) & 7)]; \  | 
29  | 0  |                                 for (ii = (((start) >> 3) + 1); ii < ((((end) - 1)) >> 3); ii++) bitmask[ii] = 0xff; \  | 
30  | 0  |                                 bitmask[(((end) - 1) >> 3)] |= bitmask_end_values[((end) & 7)]; \  | 
31  | 0  |                         } }  | 
32  |  |  | 
33  | 0  | #define RSMBLY_BITMASK_IS_COMPLETE(bitmask, msg_len, is_complete) { \ | 
34  | 0  |                         long ii; \  | 
35  | 0  |                         is_complete = 1; \  | 
36  | 0  |                         if (bitmask[(((msg_len) - 1) >> 3)] != bitmask_end_values[((msg_len) & 7)]) is_complete = 0; \  | 
37  | 0  |                         if (is_complete) for (ii = (((msg_len) - 1) >> 3) - 1; ii >= 0 ; ii--) \  | 
38  | 0  |                                 if (bitmask[ii] != 0xff) { is_complete = 0; break; } } | 
39  |  |  | 
40  |  | static const unsigned char bitmask_start_values[] = { | 
41  |  |     0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80  | 
42  |  | };  | 
43  |  | static const unsigned char bitmask_end_values[] = { | 
44  |  |     0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f  | 
45  |  | };  | 
46  |  |  | 
47  |  | static void dtls1_fix_message_header(SSL_CONNECTION *s, size_t frag_off,  | 
48  |  |                                      size_t frag_len);  | 
49  |  | static unsigned char *dtls1_write_message_header(SSL_CONNECTION *s,  | 
50  |  |                                                  unsigned char *p);  | 
51  |  | static void dtls1_set_message_header_int(SSL_CONNECTION *s, unsigned char mt,  | 
52  |  |                                          size_t len,  | 
53  |  |                                          unsigned short seq_num,  | 
54  |  |                                          size_t frag_off,  | 
55  |  |                                          size_t frag_len);  | 
56  |  | static int dtls_get_reassembled_message(SSL_CONNECTION *s, int *errtype,  | 
57  |  |                                         size_t *len);  | 
58  |  |  | 
59  |  | static hm_fragment *dtls1_hm_fragment_new(size_t frag_len, int reassembly)  | 
60  | 0  | { | 
61  | 0  |     hm_fragment *frag = NULL;  | 
62  | 0  |     unsigned char *buf = NULL;  | 
63  | 0  |     unsigned char *bitmask = NULL;  | 
64  |  | 
  | 
65  | 0  |     if ((frag = OPENSSL_zalloc(sizeof(*frag))) == NULL)  | 
66  | 0  |         return NULL;  | 
67  |  |  | 
68  | 0  |     if (frag_len) { | 
69  | 0  |         if ((buf = OPENSSL_malloc(frag_len)) == NULL) { | 
70  | 0  |             OPENSSL_free(frag);  | 
71  | 0  |             return NULL;  | 
72  | 0  |         }  | 
73  | 0  |     }  | 
74  |  |  | 
75  |  |     /* zero length fragment gets zero frag->fragment */  | 
76  | 0  |     frag->fragment = buf;  | 
77  |  |  | 
78  |  |     /* Initialize reassembly bitmask if necessary */  | 
79  | 0  |     if (reassembly) { | 
80  | 0  |         bitmask = OPENSSL_zalloc(RSMBLY_BITMASK_SIZE(frag_len));  | 
81  | 0  |         if (bitmask == NULL) { | 
82  | 0  |             OPENSSL_free(buf);  | 
83  | 0  |             OPENSSL_free(frag);  | 
84  | 0  |             return NULL;  | 
85  | 0  |         }  | 
86  | 0  |     }  | 
87  |  |  | 
88  | 0  |     frag->reassembly = bitmask;  | 
89  |  | 
  | 
90  | 0  |     return frag;  | 
91  | 0  | }  | 
92  |  |  | 
93  |  | void dtls1_hm_fragment_free(hm_fragment *frag)  | 
94  | 0  | { | 
95  | 0  |     if (!frag)  | 
96  | 0  |         return;  | 
97  |  |  | 
98  | 0  |     OPENSSL_free(frag->fragment);  | 
99  | 0  |     OPENSSL_free(frag->reassembly);  | 
100  | 0  |     OPENSSL_free(frag);  | 
101  | 0  | }  | 
102  |  |  | 
103  |  | /*  | 
104  |  |  * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or  | 
105  |  |  * SSL3_RT_CHANGE_CIPHER_SPEC)  | 
106  |  |  */  | 
107  |  | int dtls1_do_write(SSL_CONNECTION *s, uint8_t type)  | 
108  | 0  | { | 
109  | 0  |     int ret;  | 
110  | 0  |     size_t written;  | 
111  | 0  |     size_t curr_mtu;  | 
112  | 0  |     int retry = 1;  | 
113  | 0  |     size_t len, frag_off, overhead, used_len;  | 
114  | 0  |     SSL *ssl = SSL_CONNECTION_GET_SSL(s);  | 
115  | 0  |     SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);  | 
116  | 0  |     uint8_t saved_payload[DTLS1_HM_HEADER_LENGTH];  | 
117  |  | 
  | 
118  | 0  |     if (!dtls1_query_mtu(s))  | 
119  | 0  |         return -1;  | 
120  |  |  | 
121  | 0  |     if (s->d1->mtu < dtls1_min_mtu(s))  | 
122  |  |         /* should have something reasonable now */  | 
123  | 0  |         return -1;  | 
124  |  |  | 
125  | 0  |     if (s->init_off == 0 && type == SSL3_RT_HANDSHAKE) { | 
126  | 0  |         if (!ossl_assert(s->init_num ==  | 
127  | 0  |                          s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH))  | 
128  | 0  |             return -1;  | 
129  | 0  |     }  | 
130  |  |  | 
131  | 0  |     overhead = s->rlayer.wrlmethod->get_max_record_overhead(s->rlayer.wrl);  | 
132  |  | 
  | 
133  | 0  |     frag_off = 0;  | 
134  | 0  |     s->rwstate = SSL_NOTHING;  | 
135  |  |  | 
136  |  |     /* s->init_num shouldn't ever be < 0...but just in case */  | 
137  | 0  |     while (s->init_num > 0) { | 
138  | 0  |         if (type == SSL3_RT_HANDSHAKE && s->init_off != 0) { | 
139  |  |             /* We must be writing a fragment other than the first one */  | 
140  |  | 
  | 
141  | 0  |             if (frag_off > 0) { | 
142  |  |                 /* This is the first attempt at writing out this fragment */  | 
143  |  | 
  | 
144  | 0  |                 if (s->init_off <= DTLS1_HM_HEADER_LENGTH) { | 
145  |  |                     /*  | 
146  |  |                      * Each fragment that was already sent must at least have  | 
147  |  |                      * contained the message header plus one other byte.  | 
148  |  |                      * Therefore |init_off| must have progressed by at least  | 
149  |  |                      * |DTLS1_HM_HEADER_LENGTH + 1| bytes. If not something went  | 
150  |  |                      * wrong.  | 
151  |  |                      */  | 
152  | 0  |                     return -1;  | 
153  | 0  |                 }  | 
154  |  |  | 
155  |  |                 /*  | 
156  |  |                  * Adjust |init_off| and |init_num| to allow room for a new  | 
157  |  |                  * message header for this fragment.  | 
158  |  |                  */  | 
159  | 0  |                 s->init_off -= DTLS1_HM_HEADER_LENGTH;  | 
160  | 0  |                 s->init_num += DTLS1_HM_HEADER_LENGTH;  | 
161  | 0  |             } else { | 
162  |  |                 /*  | 
163  |  |                  * We must have been called again after a retry so use the  | 
164  |  |                  * fragment offset from our last attempt. We do not need  | 
165  |  |                  * to adjust |init_off| and |init_num| as above, because  | 
166  |  |                  * that should already have been done before the retry.  | 
167  |  |                  */  | 
168  | 0  |                 frag_off = s->d1->w_msg_hdr.frag_off;  | 
169  | 0  |             }  | 
170  | 0  |         }  | 
171  |  |  | 
172  | 0  |         used_len = BIO_wpending(s->wbio) + overhead;  | 
173  | 0  |         if (s->d1->mtu > used_len)  | 
174  | 0  |             curr_mtu = s->d1->mtu - used_len;  | 
175  | 0  |         else  | 
176  | 0  |             curr_mtu = 0;  | 
177  |  | 
  | 
178  | 0  |         if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) { | 
179  |  |             /*  | 
180  |  |              * grr.. we could get an error if MTU picked was wrong  | 
181  |  |              */  | 
182  | 0  |             ret = BIO_flush(s->wbio);  | 
183  | 0  |             if (ret <= 0) { | 
184  | 0  |                 s->rwstate = SSL_WRITING;  | 
185  | 0  |                 return ret;  | 
186  | 0  |             }  | 
187  | 0  |             if (s->d1->mtu > overhead + DTLS1_HM_HEADER_LENGTH) { | 
188  | 0  |                 curr_mtu = s->d1->mtu - overhead;  | 
189  | 0  |             } else { | 
190  |  |                 /* Shouldn't happen */  | 
191  | 0  |                 return -1;  | 
192  | 0  |             }  | 
193  | 0  |         }  | 
194  |  |  | 
195  |  |         /*  | 
196  |  |          * We just checked that s->init_num > 0 so this cast should be safe  | 
197  |  |          */  | 
198  | 0  |         if (((unsigned int)s->init_num) > curr_mtu)  | 
199  | 0  |             len = curr_mtu;  | 
200  | 0  |         else  | 
201  | 0  |             len = s->init_num;  | 
202  |  | 
  | 
203  | 0  |         if (len > ssl_get_max_send_fragment(s))  | 
204  | 0  |             len = ssl_get_max_send_fragment(s);  | 
205  |  |  | 
206  |  |         /*  | 
207  |  |          * XDTLS: this function is too long.  split out the CCS part  | 
208  |  |          */  | 
209  | 0  |         if (type == SSL3_RT_HANDSHAKE) { | 
210  | 0  |             if (len < DTLS1_HM_HEADER_LENGTH) { | 
211  |  |                 /*  | 
212  |  |                  * len is so small that we really can't do anything sensible  | 
213  |  |                  * so fail  | 
214  |  |                  */  | 
215  | 0  |                 return -1;  | 
216  | 0  |             }  | 
217  | 0  |             dtls1_fix_message_header(s, frag_off, len - DTLS1_HM_HEADER_LENGTH);  | 
218  |  |  | 
219  |  |             /*  | 
220  |  |              * Save the data that will be overwritten by  | 
221  |  |              * dtls1_write_messsage_header so no corruption occurs when using  | 
222  |  |              * a msg callback.  | 
223  |  |              */  | 
224  | 0  |             if (s->msg_callback && s->init_off != 0)  | 
225  | 0  |                 memcpy(saved_payload, &s->init_buf->data[s->init_off],  | 
226  | 0  |                        sizeof(saved_payload));  | 
227  |  | 
  | 
228  | 0  |             dtls1_write_message_header(s,  | 
229  | 0  |                                        (unsigned char *)&s->init_buf->  | 
230  | 0  |                                        data[s->init_off]);  | 
231  | 0  |         }  | 
232  |  |  | 
233  | 0  |         ret = dtls1_write_bytes(s, type, &s->init_buf->data[s->init_off], len,  | 
234  | 0  |                                 &written);  | 
235  |  | 
  | 
236  | 0  |         if (type == SSL3_RT_HANDSHAKE && s->msg_callback && s->init_off != 0)  | 
237  | 0  |             memcpy(&s->init_buf->data[s->init_off], saved_payload,  | 
238  | 0  |                    sizeof(saved_payload));  | 
239  |  | 
  | 
240  | 0  |         if (ret <= 0) { | 
241  |  |             /*  | 
242  |  |              * might need to update MTU here, but we don't know which  | 
243  |  |              * previous packet caused the failure -- so can't really  | 
244  |  |              * retransmit anything.  continue as if everything is fine and  | 
245  |  |              * wait for an alert to handle the retransmit  | 
246  |  |              */  | 
247  | 0  |             if (retry && BIO_ctrl(SSL_get_wbio(ssl),  | 
248  | 0  |                                   BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL) > 0) { | 
249  | 0  |                 if (!(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) { | 
250  | 0  |                     if (!dtls1_query_mtu(s))  | 
251  | 0  |                         return -1;  | 
252  |  |                     /* Have one more go */  | 
253  | 0  |                     retry = 0;  | 
254  | 0  |                 } else  | 
255  | 0  |                     return -1;  | 
256  | 0  |             } else { | 
257  | 0  |                 return -1;  | 
258  | 0  |             }  | 
259  | 0  |         } else { | 
260  |  |  | 
261  |  |             /*  | 
262  |  |              * bad if this assert fails, only part of the handshake message  | 
263  |  |              * got sent.  but why would this happen?  | 
264  |  |              */  | 
265  | 0  |             if (!ossl_assert(len == written))  | 
266  | 0  |                 return -1;  | 
267  |  |  | 
268  |  |             /*  | 
269  |  |              * We should not exceed the MTU size. If compression is in use  | 
270  |  |              * then the max record overhead calculation is unreliable so we do  | 
271  |  |              * not check in that case. We use assert rather than ossl_assert  | 
272  |  |              * because in a production build, if this assert were ever to fail,  | 
273  |  |              * then the best thing to do is probably carry on regardless.  | 
274  |  |              */  | 
275  | 0  |             assert(s->s3.tmp.new_compression != NULL  | 
276  | 0  |                    || BIO_wpending(s->wbio) <= (int)s->d1->mtu);  | 
277  |  | 
  | 
278  | 0  |             if (type == SSL3_RT_HANDSHAKE && !s->d1->retransmitting) { | 
279  |  |                 /*  | 
280  |  |                  * should not be done for 'Hello Request's, but in that case  | 
281  |  |                  * we'll ignore the result anyway  | 
282  |  |                  */  | 
283  | 0  |                 unsigned char *p =  | 
284  | 0  |                     (unsigned char *)&s->init_buf->data[s->init_off];  | 
285  | 0  |                 const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;  | 
286  | 0  |                 size_t xlen;  | 
287  |  | 
  | 
288  | 0  |                 if (frag_off == 0 && s->version != DTLS1_BAD_VER) { | 
289  |  |                     /*  | 
290  |  |                      * reconstruct message header is if it is being sent in  | 
291  |  |                      * single fragment  | 
292  |  |                      */  | 
293  | 0  |                     *p++ = msg_hdr->type;  | 
294  | 0  |                     l2n3(msg_hdr->msg_len, p);  | 
295  | 0  |                     s2n(msg_hdr->seq, p);  | 
296  | 0  |                     l2n3(0, p);  | 
297  | 0  |                     l2n3(msg_hdr->msg_len, p);  | 
298  | 0  |                     p -= DTLS1_HM_HEADER_LENGTH;  | 
299  | 0  |                     xlen = written;  | 
300  | 0  |                 } else { | 
301  | 0  |                     p += DTLS1_HM_HEADER_LENGTH;  | 
302  | 0  |                     xlen = written - DTLS1_HM_HEADER_LENGTH;  | 
303  | 0  |                 }  | 
304  |  | 
  | 
305  | 0  |                 if (!ssl3_finish_mac(s, p, xlen))  | 
306  | 0  |                     return -1;  | 
307  | 0  |             }  | 
308  |  |  | 
309  | 0  |             if (written == s->init_num) { | 
310  | 0  |                 if (s->msg_callback)  | 
311  | 0  |                     s->msg_callback(1, s->version, type, s->init_buf->data,  | 
312  | 0  |                                     s->init_off + s->init_num, ussl,  | 
313  | 0  |                                     s->msg_callback_arg);  | 
314  |  | 
  | 
315  | 0  |                 s->init_off = 0; /* done writing this message */  | 
316  | 0  |                 s->init_num = 0;  | 
317  |  | 
  | 
318  | 0  |                 return 1;  | 
319  | 0  |             }  | 
320  | 0  |             s->init_off += written;  | 
321  | 0  |             s->init_num -= written;  | 
322  | 0  |             written -= DTLS1_HM_HEADER_LENGTH;  | 
323  | 0  |             frag_off += written;  | 
324  |  |  | 
325  |  |             /*  | 
326  |  |              * We save the fragment offset for the next fragment so we have it  | 
327  |  |              * available in case of an IO retry. We don't know the length of the  | 
328  |  |              * next fragment yet so just set that to 0 for now. It will be  | 
329  |  |              * updated again later.  | 
330  |  |              */  | 
331  | 0  |             dtls1_fix_message_header(s, frag_off, 0);  | 
332  | 0  |         }  | 
333  | 0  |     }  | 
334  | 0  |     return 0;  | 
335  | 0  | }  | 
336  |  |  | 
337  |  | int dtls_get_message(SSL_CONNECTION *s, int *mt)  | 
338  | 0  | { | 
339  | 0  |     struct hm_header_st *msg_hdr;  | 
340  | 0  |     unsigned char *p;  | 
341  | 0  |     size_t msg_len;  | 
342  | 0  |     size_t tmplen;  | 
343  | 0  |     int errtype;  | 
344  |  | 
  | 
345  | 0  |     msg_hdr = &s->d1->r_msg_hdr;  | 
346  | 0  |     memset(msg_hdr, 0, sizeof(*msg_hdr));  | 
347  |  | 
  | 
348  | 0  |  again:  | 
349  | 0  |     if (!dtls_get_reassembled_message(s, &errtype, &tmplen)) { | 
350  | 0  |         if (errtype == DTLS1_HM_BAD_FRAGMENT  | 
351  | 0  |                 || errtype == DTLS1_HM_FRAGMENT_RETRY) { | 
352  |  |             /* bad fragment received */  | 
353  | 0  |             goto again;  | 
354  | 0  |         }  | 
355  | 0  |         return 0;  | 
356  | 0  |     }  | 
357  |  |  | 
358  | 0  |     *mt = s->s3.tmp.message_type;  | 
359  |  | 
  | 
360  | 0  |     p = (unsigned char *)s->init_buf->data;  | 
361  |  | 
  | 
362  | 0  |     if (*mt == SSL3_MT_CHANGE_CIPHER_SPEC) { | 
363  | 0  |         if (s->msg_callback) { | 
364  | 0  |             s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC,  | 
365  | 0  |                             p, 1, SSL_CONNECTION_GET_USER_SSL(s),  | 
366  | 0  |                             s->msg_callback_arg);  | 
367  | 0  |         }  | 
368  |  |         /*  | 
369  |  |          * This isn't a real handshake message so skip the processing below.  | 
370  |  |          */  | 
371  | 0  |         return 1;  | 
372  | 0  |     }  | 
373  |  |  | 
374  | 0  |     msg_len = msg_hdr->msg_len;  | 
375  |  |  | 
376  |  |     /* reconstruct message header */  | 
377  | 0  |     *(p++) = msg_hdr->type;  | 
378  | 0  |     l2n3(msg_len, p);  | 
379  | 0  |     s2n(msg_hdr->seq, p);  | 
380  | 0  |     l2n3(0, p);  | 
381  | 0  |     l2n3(msg_len, p);  | 
382  |  | 
  | 
383  | 0  |     memset(msg_hdr, 0, sizeof(*msg_hdr));  | 
384  |  | 
  | 
385  | 0  |     s->d1->handshake_read_seq++;  | 
386  |  | 
  | 
387  | 0  |     s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH;  | 
388  |  | 
  | 
389  | 0  |     return 1;  | 
390  | 0  | }  | 
391  |  |  | 
392  |  | /*  | 
393  |  |  * Actually we already have the message body - but this is an opportunity for  | 
394  |  |  * DTLS to do any further processing it wants at the same point that TLS would  | 
395  |  |  * be asked for the message body.  | 
396  |  |  */  | 
397  |  | int dtls_get_message_body(SSL_CONNECTION *s, size_t *len)  | 
398  | 0  | { | 
399  | 0  |     unsigned char *msg = (unsigned char *)s->init_buf->data;  | 
400  | 0  |     size_t msg_len = s->init_num + DTLS1_HM_HEADER_LENGTH;  | 
401  |  | 
  | 
402  | 0  |     if (s->s3.tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) { | 
403  |  |         /* Nothing to be done */  | 
404  | 0  |         goto end;  | 
405  | 0  |     }  | 
406  |  |     /*  | 
407  |  |      * If receiving Finished, record MAC of prior handshake messages for  | 
408  |  |      * Finished verification.  | 
409  |  |      */  | 
410  | 0  |     if (*(s->init_buf->data) == SSL3_MT_FINISHED && !ssl3_take_mac(s)) { | 
411  |  |         /* SSLfatal() already called */  | 
412  | 0  |         return 0;  | 
413  | 0  |     }  | 
414  |  |  | 
415  | 0  |     if (s->version == DTLS1_BAD_VER) { | 
416  | 0  |         msg += DTLS1_HM_HEADER_LENGTH;  | 
417  | 0  |         msg_len -= DTLS1_HM_HEADER_LENGTH;  | 
418  | 0  |     }  | 
419  |  | 
  | 
420  | 0  |     if (!ssl3_finish_mac(s, msg, msg_len))  | 
421  | 0  |         return 0;  | 
422  |  |  | 
423  | 0  |     if (s->msg_callback)  | 
424  | 0  |         s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,  | 
425  | 0  |                         s->init_buf->data, s->init_num + DTLS1_HM_HEADER_LENGTH,  | 
426  | 0  |                         SSL_CONNECTION_GET_USER_SSL(s), s->msg_callback_arg);  | 
427  |  | 
  | 
428  | 0  |  end:  | 
429  | 0  |     *len = s->init_num;  | 
430  | 0  |     return 1;  | 
431  | 0  | }  | 
432  |  |  | 
433  |  | /*  | 
434  |  |  * dtls1_max_handshake_message_len returns the maximum number of bytes  | 
435  |  |  * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but  | 
436  |  |  * may be greater if the maximum certificate list size requires it.  | 
437  |  |  */  | 
438  |  | static size_t dtls1_max_handshake_message_len(const SSL_CONNECTION *s)  | 
439  | 0  | { | 
440  | 0  |     size_t max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;  | 
441  | 0  |     if (max_len < s->max_cert_list)  | 
442  | 0  |         return s->max_cert_list;  | 
443  | 0  |     return max_len;  | 
444  | 0  | }  | 
445  |  |  | 
446  |  | static int dtls1_preprocess_fragment(SSL_CONNECTION *s,  | 
447  |  |                                      struct hm_header_st *msg_hdr)  | 
448  | 0  | { | 
449  | 0  |     size_t frag_off, frag_len, msg_len;  | 
450  |  | 
  | 
451  | 0  |     msg_len = msg_hdr->msg_len;  | 
452  | 0  |     frag_off = msg_hdr->frag_off;  | 
453  | 0  |     frag_len = msg_hdr->frag_len;  | 
454  |  |  | 
455  |  |     /* sanity checking */  | 
456  | 0  |     if ((frag_off + frag_len) > msg_len  | 
457  | 0  |             || msg_len > dtls1_max_handshake_message_len(s)) { | 
458  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_EXCESSIVE_MESSAGE_SIZE);  | 
459  | 0  |         return 0;  | 
460  | 0  |     }  | 
461  |  |  | 
462  | 0  |     if (s->d1->r_msg_hdr.frag_off == 0) { /* first fragment */ | 
463  |  |         /*  | 
464  |  |          * msg_len is limited to 2^24, but is effectively checked against  | 
465  |  |          * dtls_max_handshake_message_len(s) above  | 
466  |  |          */  | 
467  | 0  |         if (!BUF_MEM_grow_clean(s->init_buf, msg_len + DTLS1_HM_HEADER_LENGTH)) { | 
468  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BUF_LIB);  | 
469  | 0  |             return 0;  | 
470  | 0  |         }  | 
471  |  |  | 
472  | 0  |         s->s3.tmp.message_size = msg_len;  | 
473  | 0  |         s->d1->r_msg_hdr.msg_len = msg_len;  | 
474  | 0  |         s->s3.tmp.message_type = msg_hdr->type;  | 
475  | 0  |         s->d1->r_msg_hdr.type = msg_hdr->type;  | 
476  | 0  |         s->d1->r_msg_hdr.seq = msg_hdr->seq;  | 
477  | 0  |     } else if (msg_len != s->d1->r_msg_hdr.msg_len) { | 
478  |  |         /*  | 
479  |  |          * They must be playing with us! BTW, failure to enforce upper limit  | 
480  |  |          * would open possibility for buffer overrun.  | 
481  |  |          */  | 
482  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_EXCESSIVE_MESSAGE_SIZE);  | 
483  | 0  |         return 0;  | 
484  | 0  |     }  | 
485  |  |  | 
486  | 0  |     return 1;  | 
487  | 0  | }  | 
488  |  |  | 
489  |  | /*  | 
490  |  |  * Returns 1 if there is a buffered fragment available, 0 if not, or -1 on a  | 
491  |  |  * fatal error.  | 
492  |  |  */  | 
493  |  | static int dtls1_retrieve_buffered_fragment(SSL_CONNECTION *s, size_t *len)  | 
494  | 0  | { | 
495  |  |     /*-  | 
496  |  |      * (0) check whether the desired fragment is available  | 
497  |  |      * if so:  | 
498  |  |      * (1) copy over the fragment to s->init_buf->data[]  | 
499  |  |      * (2) update s->init_num  | 
500  |  |      */  | 
501  | 0  |     pitem *item;  | 
502  | 0  |     piterator iter;  | 
503  | 0  |     hm_fragment *frag;  | 
504  | 0  |     int ret;  | 
505  | 0  |     int chretran = 0;  | 
506  |  | 
  | 
507  | 0  |     iter = pqueue_iterator(s->d1->buffered_messages);  | 
508  | 0  |     do { | 
509  | 0  |         item = pqueue_next(&iter);  | 
510  | 0  |         if (item == NULL)  | 
511  | 0  |             return 0;  | 
512  |  |  | 
513  | 0  |         frag = (hm_fragment *)item->data;  | 
514  |  | 
  | 
515  | 0  |         if (frag->msg_header.seq < s->d1->handshake_read_seq) { | 
516  | 0  |             pitem *next;  | 
517  | 0  |             hm_fragment *nextfrag;  | 
518  |  | 
  | 
519  | 0  |             if (!s->server  | 
520  | 0  |                     || frag->msg_header.seq != 0  | 
521  | 0  |                     || s->d1->handshake_read_seq != 1  | 
522  | 0  |                     || s->statem.hand_state != DTLS_ST_SW_HELLO_VERIFY_REQUEST) { | 
523  |  |                 /*  | 
524  |  |                  * This is a stale message that has been buffered so clear it.  | 
525  |  |                  * It is safe to pop this message from the queue even though  | 
526  |  |                  * we have an active iterator  | 
527  |  |                  */  | 
528  | 0  |                 pqueue_pop(s->d1->buffered_messages);  | 
529  | 0  |                 dtls1_hm_fragment_free(frag);  | 
530  | 0  |                 pitem_free(item);  | 
531  | 0  |                 item = NULL;  | 
532  | 0  |                 frag = NULL;  | 
533  | 0  |             } else { | 
534  |  |                 /*  | 
535  |  |                  * We have fragments for a ClientHello without a cookie,  | 
536  |  |                  * even though we have sent a HelloVerifyRequest. It is possible  | 
537  |  |                  * that the HelloVerifyRequest got lost and this is a  | 
538  |  |                  * retransmission of the original ClientHello  | 
539  |  |                  */  | 
540  | 0  |                 next = pqueue_next(&iter);  | 
541  | 0  |                 if (next != NULL) { | 
542  | 0  |                     nextfrag = (hm_fragment *)next->data;  | 
543  | 0  |                     if (nextfrag->msg_header.seq == s->d1->handshake_read_seq) { | 
544  |  |                         /*  | 
545  |  |                         * We have fragments for both a ClientHello without  | 
546  |  |                         * cookie and one with. Ditch the one without.  | 
547  |  |                         */  | 
548  | 0  |                         pqueue_pop(s->d1->buffered_messages);  | 
549  | 0  |                         dtls1_hm_fragment_free(frag);  | 
550  | 0  |                         pitem_free(item);  | 
551  | 0  |                         item = next;  | 
552  | 0  |                         frag = nextfrag;  | 
553  | 0  |                     } else { | 
554  | 0  |                         chretran = 1;  | 
555  | 0  |                     }  | 
556  | 0  |                 } else { | 
557  | 0  |                     chretran = 1;  | 
558  | 0  |                 }  | 
559  | 0  |             }  | 
560  | 0  |         }  | 
561  | 0  |     } while (item == NULL);  | 
562  |  |  | 
563  |  |     /* Don't return if reassembly still in progress */  | 
564  | 0  |     if (frag->reassembly != NULL)  | 
565  | 0  |         return 0;  | 
566  |  |  | 
567  | 0  |     if (s->d1->handshake_read_seq == frag->msg_header.seq || chretran) { | 
568  | 0  |         size_t frag_len = frag->msg_header.frag_len;  | 
569  | 0  |         pqueue_pop(s->d1->buffered_messages);  | 
570  |  |  | 
571  |  |         /* Calls SSLfatal() as required */  | 
572  | 0  |         ret = dtls1_preprocess_fragment(s, &frag->msg_header);  | 
573  |  | 
  | 
574  | 0  |         if (ret && frag->msg_header.frag_len > 0) { | 
575  | 0  |             unsigned char *p =  | 
576  | 0  |                 (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;  | 
577  | 0  |             memcpy(&p[frag->msg_header.frag_off], frag->fragment,  | 
578  | 0  |                    frag->msg_header.frag_len);  | 
579  | 0  |         }  | 
580  |  | 
  | 
581  | 0  |         dtls1_hm_fragment_free(frag);  | 
582  | 0  |         pitem_free(item);  | 
583  |  | 
  | 
584  | 0  |         if (ret) { | 
585  | 0  |             if (chretran) { | 
586  |  |                 /*  | 
587  |  |                  * We got a new ClientHello with a message sequence of 0.  | 
588  |  |                  * Reset the read/write sequences back to the beginning.  | 
589  |  |                  * We process it like this is the first time we've seen a  | 
590  |  |                  * ClientHello from the client.  | 
591  |  |                  */  | 
592  | 0  |                 s->d1->handshake_read_seq = 0;  | 
593  | 0  |                 s->d1->next_handshake_write_seq = 0;  | 
594  | 0  |             }  | 
595  | 0  |             *len = frag_len;  | 
596  | 0  |             return 1;  | 
597  | 0  |         }  | 
598  |  |  | 
599  |  |         /* Fatal error */  | 
600  | 0  |         s->init_num = 0;  | 
601  | 0  |         return -1;  | 
602  | 0  |     } else { | 
603  | 0  |         return 0;  | 
604  | 0  |     }  | 
605  | 0  | }  | 
606  |  |  | 
607  |  | static int dtls1_reassemble_fragment(SSL_CONNECTION *s,  | 
608  |  |                                      const struct hm_header_st *msg_hdr)  | 
609  | 0  | { | 
610  | 0  |     hm_fragment *frag = NULL;  | 
611  | 0  |     pitem *item = NULL;  | 
612  | 0  |     int i = -1, is_complete;  | 
613  | 0  |     unsigned char seq64be[8];  | 
614  | 0  |     size_t frag_len = msg_hdr->frag_len;  | 
615  | 0  |     size_t readbytes;  | 
616  | 0  |     SSL *ssl = SSL_CONNECTION_GET_SSL(s);  | 
617  |  | 
  | 
618  | 0  |     if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len ||  | 
619  | 0  |         msg_hdr->msg_len > dtls1_max_handshake_message_len(s))  | 
620  | 0  |         goto err;  | 
621  |  |  | 
622  | 0  |     if (frag_len == 0) { | 
623  | 0  |         return DTLS1_HM_FRAGMENT_RETRY;  | 
624  | 0  |     }  | 
625  |  |  | 
626  |  |     /* Try to find item in queue */  | 
627  | 0  |     memset(seq64be, 0, sizeof(seq64be));  | 
628  | 0  |     seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);  | 
629  | 0  |     seq64be[7] = (unsigned char)msg_hdr->seq;  | 
630  | 0  |     item = pqueue_find(s->d1->buffered_messages, seq64be);  | 
631  |  | 
  | 
632  | 0  |     if (item == NULL) { | 
633  | 0  |         frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1);  | 
634  | 0  |         if (frag == NULL)  | 
635  | 0  |             goto err;  | 
636  | 0  |         memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));  | 
637  | 0  |         frag->msg_header.frag_len = frag->msg_header.msg_len;  | 
638  | 0  |         frag->msg_header.frag_off = 0;  | 
639  | 0  |     } else { | 
640  | 0  |         frag = (hm_fragment *)item->data;  | 
641  | 0  |         if (frag->msg_header.msg_len != msg_hdr->msg_len) { | 
642  | 0  |             item = NULL;  | 
643  | 0  |             frag = NULL;  | 
644  | 0  |             goto err;  | 
645  | 0  |         }  | 
646  | 0  |     }  | 
647  |  |  | 
648  |  |     /*  | 
649  |  |      * If message is already reassembled, this must be a retransmit and can  | 
650  |  |      * be dropped. In this case item != NULL and so frag does not need to be  | 
651  |  |      * freed.  | 
652  |  |      */  | 
653  | 0  |     if (frag->reassembly == NULL) { | 
654  | 0  |         unsigned char devnull[256];  | 
655  |  | 
  | 
656  | 0  |         while (frag_len) { | 
657  | 0  |             i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,  | 
658  | 0  |                                             devnull,  | 
659  | 0  |                                             frag_len >  | 
660  | 0  |                                             sizeof(devnull) ? sizeof(devnull) :  | 
661  | 0  |                                             frag_len, 0, &readbytes);  | 
662  | 0  |             if (i <= 0)  | 
663  | 0  |                 goto err;  | 
664  | 0  |             frag_len -= readbytes;  | 
665  | 0  |         }  | 
666  | 0  |         return DTLS1_HM_FRAGMENT_RETRY;  | 
667  | 0  |     }  | 
668  |  |  | 
669  |  |     /* read the body of the fragment (header has already been read */  | 
670  | 0  |     i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,  | 
671  | 0  |                                     frag->fragment + msg_hdr->frag_off,  | 
672  | 0  |                                     frag_len, 0, &readbytes);  | 
673  | 0  |     if (i <= 0 || readbytes != frag_len)  | 
674  | 0  |         i = -1;  | 
675  | 0  |     if (i <= 0)  | 
676  | 0  |         goto err;  | 
677  |  |  | 
678  | 0  |     RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off,  | 
679  | 0  |                         (long)(msg_hdr->frag_off + frag_len));  | 
680  |  | 
  | 
681  | 0  |     if (!ossl_assert(msg_hdr->msg_len > 0))  | 
682  | 0  |         goto err;  | 
683  | 0  |     RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len,  | 
684  | 0  |                                is_complete);  | 
685  |  | 
  | 
686  | 0  |     if (is_complete) { | 
687  | 0  |         OPENSSL_free(frag->reassembly);  | 
688  | 0  |         frag->reassembly = NULL;  | 
689  | 0  |     }  | 
690  |  | 
  | 
691  | 0  |     if (item == NULL) { | 
692  | 0  |         item = pitem_new(seq64be, frag);  | 
693  | 0  |         if (item == NULL) { | 
694  | 0  |             i = -1;  | 
695  | 0  |             goto err;  | 
696  | 0  |         }  | 
697  |  |  | 
698  | 0  |         item = pqueue_insert(s->d1->buffered_messages, item);  | 
699  |  |         /*  | 
700  |  |          * pqueue_insert fails iff a duplicate item is inserted. However,  | 
701  |  |          * |item| cannot be a duplicate. If it were, |pqueue_find|, above,  | 
702  |  |          * would have returned it and control would never have reached this  | 
703  |  |          * branch.  | 
704  |  |          */  | 
705  | 0  |         if (!ossl_assert(item != NULL))  | 
706  | 0  |             goto err;  | 
707  | 0  |     }  | 
708  |  |  | 
709  | 0  |     return DTLS1_HM_FRAGMENT_RETRY;  | 
710  |  |  | 
711  | 0  |  err:  | 
712  | 0  |     if (item == NULL)  | 
713  | 0  |         dtls1_hm_fragment_free(frag);  | 
714  | 0  |     return -1;  | 
715  | 0  | }  | 
716  |  |  | 
717  |  | static int dtls1_process_out_of_seq_message(SSL_CONNECTION *s,  | 
718  |  |                                             const struct hm_header_st *msg_hdr)  | 
719  | 0  | { | 
720  | 0  |     int i = -1;  | 
721  | 0  |     hm_fragment *frag = NULL;  | 
722  | 0  |     pitem *item = NULL;  | 
723  | 0  |     unsigned char seq64be[8];  | 
724  | 0  |     size_t frag_len = msg_hdr->frag_len;  | 
725  | 0  |     size_t readbytes;  | 
726  | 0  |     SSL *ssl = SSL_CONNECTION_GET_SSL(s);  | 
727  |  | 
  | 
728  | 0  |     if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len)  | 
729  | 0  |         goto err;  | 
730  |  |  | 
731  |  |     /* Try to find item in queue, to prevent duplicate entries */  | 
732  | 0  |     memset(seq64be, 0, sizeof(seq64be));  | 
733  | 0  |     seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);  | 
734  | 0  |     seq64be[7] = (unsigned char)msg_hdr->seq;  | 
735  | 0  |     item = pqueue_find(s->d1->buffered_messages, seq64be);  | 
736  |  |  | 
737  |  |     /*  | 
738  |  |      * If we already have an entry and this one is a fragment, don't discard  | 
739  |  |      * it and rather try to reassemble it.  | 
740  |  |      */  | 
741  | 0  |     if (item != NULL && frag_len != msg_hdr->msg_len)  | 
742  | 0  |         item = NULL;  | 
743  |  |  | 
744  |  |     /*  | 
745  |  |      * Discard the message if sequence number was already there, is too far  | 
746  |  |      * in the future, already in the queue or if we received a FINISHED  | 
747  |  |      * before the SERVER_HELLO, which then must be a stale retransmit.  | 
748  |  |      */  | 
749  | 0  |     if (msg_hdr->seq <= s->d1->handshake_read_seq ||  | 
750  | 0  |         msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL ||  | 
751  | 0  |         (s->d1->handshake_read_seq == 0 && msg_hdr->type == SSL3_MT_FINISHED)) { | 
752  | 0  |         unsigned char devnull[256];  | 
753  |  | 
  | 
754  | 0  |         while (frag_len) { | 
755  | 0  |             i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,  | 
756  | 0  |                                             devnull,  | 
757  | 0  |                                             frag_len >  | 
758  | 0  |                                             sizeof(devnull) ? sizeof(devnull) :  | 
759  | 0  |                                             frag_len, 0, &readbytes);  | 
760  | 0  |             if (i <= 0)  | 
761  | 0  |                 goto err;  | 
762  | 0  |             frag_len -= readbytes;  | 
763  | 0  |         }  | 
764  | 0  |     } else { | 
765  | 0  |         if (frag_len != msg_hdr->msg_len) { | 
766  | 0  |             return dtls1_reassemble_fragment(s, msg_hdr);  | 
767  | 0  |         }  | 
768  |  |  | 
769  | 0  |         if (frag_len > dtls1_max_handshake_message_len(s))  | 
770  | 0  |             goto err;  | 
771  |  |  | 
772  | 0  |         frag = dtls1_hm_fragment_new(frag_len, 0);  | 
773  | 0  |         if (frag == NULL)  | 
774  | 0  |             goto err;  | 
775  |  |  | 
776  | 0  |         memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));  | 
777  |  | 
  | 
778  | 0  |         if (frag_len) { | 
779  |  |             /*  | 
780  |  |              * read the body of the fragment (header has already been read  | 
781  |  |              */  | 
782  | 0  |             i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,  | 
783  | 0  |                                             frag->fragment, frag_len, 0,  | 
784  | 0  |                                             &readbytes);  | 
785  | 0  |             if (i<=0 || readbytes != frag_len)  | 
786  | 0  |                 i = -1;  | 
787  | 0  |             if (i <= 0)  | 
788  | 0  |                 goto err;  | 
789  | 0  |         }  | 
790  |  |  | 
791  | 0  |         item = pitem_new(seq64be, frag);  | 
792  | 0  |         if (item == NULL)  | 
793  | 0  |             goto err;  | 
794  |  |  | 
795  | 0  |         item = pqueue_insert(s->d1->buffered_messages, item);  | 
796  |  |         /*  | 
797  |  |          * pqueue_insert fails iff a duplicate item is inserted. However,  | 
798  |  |          * |item| cannot be a duplicate. If it were, |pqueue_find|, above,  | 
799  |  |          * would have returned it. Then, either |frag_len| !=  | 
800  |  |          * |msg_hdr->msg_len| in which case |item| is set to NULL and it will  | 
801  |  |          * have been processed with |dtls1_reassemble_fragment|, above, or  | 
802  |  |          * the record will have been discarded.  | 
803  |  |          */  | 
804  | 0  |         if (!ossl_assert(item != NULL))  | 
805  | 0  |             goto err;  | 
806  | 0  |     }  | 
807  |  |  | 
808  | 0  |     return DTLS1_HM_FRAGMENT_RETRY;  | 
809  |  |  | 
810  | 0  |  err:  | 
811  | 0  |     if (item == NULL)  | 
812  | 0  |         dtls1_hm_fragment_free(frag);  | 
813  | 0  |     return 0;  | 
814  | 0  | }  | 
815  |  |  | 
816  |  | static int dtls_get_reassembled_message(SSL_CONNECTION *s, int *errtype,  | 
817  |  |                                         size_t *len)  | 
818  | 0  | { | 
819  | 0  |     size_t mlen, frag_off, frag_len;  | 
820  | 0  |     int i, ret;  | 
821  | 0  |     uint8_t recvd_type;  | 
822  | 0  |     struct hm_header_st msg_hdr;  | 
823  | 0  |     size_t readbytes;  | 
824  | 0  |     SSL *ssl = SSL_CONNECTION_GET_SSL(s);  | 
825  | 0  |     SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);  | 
826  | 0  |     int chretran = 0;  | 
827  | 0  |     unsigned char *p;  | 
828  |  | 
  | 
829  | 0  |     *errtype = 0;  | 
830  |  | 
  | 
831  | 0  |     p = (unsigned char *)s->init_buf->data;  | 
832  |  | 
  | 
833  | 0  |  redo:  | 
834  |  |     /* see if we have the required fragment already */  | 
835  | 0  |     ret = dtls1_retrieve_buffered_fragment(s, &frag_len);  | 
836  | 0  |     if (ret < 0) { | 
837  |  |         /* SSLfatal() already called */  | 
838  | 0  |         return 0;  | 
839  | 0  |     }  | 
840  | 0  |     if (ret > 0) { | 
841  | 0  |         s->init_num = frag_len;  | 
842  | 0  |         *len = frag_len;  | 
843  | 0  |         return 1;  | 
844  | 0  |     }  | 
845  |  |  | 
846  |  |     /* read handshake message header */  | 
847  | 0  |     i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, &recvd_type, p,  | 
848  | 0  |                                     DTLS1_HM_HEADER_LENGTH, 0, &readbytes);  | 
849  | 0  |     if (i <= 0) {               /* nbio, or an error */ | 
850  | 0  |         s->rwstate = SSL_READING;  | 
851  | 0  |         *len = 0;  | 
852  | 0  |         return 0;  | 
853  | 0  |     }  | 
854  | 0  |     if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) { | 
855  | 0  |         if (p[0] != SSL3_MT_CCS) { | 
856  | 0  |             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,  | 
857  | 0  |                      SSL_R_BAD_CHANGE_CIPHER_SPEC);  | 
858  | 0  |             goto f_err;  | 
859  | 0  |         }  | 
860  |  |  | 
861  | 0  |         s->init_num = readbytes - 1;  | 
862  | 0  |         s->init_msg = s->init_buf->data + 1;  | 
863  | 0  |         s->s3.tmp.message_type = SSL3_MT_CHANGE_CIPHER_SPEC;  | 
864  | 0  |         s->s3.tmp.message_size = readbytes - 1;  | 
865  | 0  |         *len = readbytes - 1;  | 
866  | 0  |         return 1;  | 
867  | 0  |     }  | 
868  |  |  | 
869  |  |     /* Handshake fails if message header is incomplete */  | 
870  | 0  |     if (readbytes != DTLS1_HM_HEADER_LENGTH) { | 
871  | 0  |         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);  | 
872  | 0  |         goto f_err;  | 
873  | 0  |     }  | 
874  |  |  | 
875  |  |     /* parse the message fragment header */  | 
876  | 0  |     dtls1_get_message_header(p, &msg_hdr);  | 
877  |  | 
  | 
878  | 0  |     mlen = msg_hdr.msg_len;  | 
879  | 0  |     frag_off = msg_hdr.frag_off;  | 
880  | 0  |     frag_len = msg_hdr.frag_len;  | 
881  |  |  | 
882  |  |     /*  | 
883  |  |      * We must have at least frag_len bytes left in the record to be read.  | 
884  |  |      * Fragments must not span records.  | 
885  |  |      */  | 
886  | 0  |     if (frag_len > s->rlayer.tlsrecs[s->rlayer.curr_rec].length) { | 
887  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_LENGTH);  | 
888  | 0  |         goto f_err;  | 
889  | 0  |     }  | 
890  |  |  | 
891  |  |     /*  | 
892  |  |      * if this is a future (or stale) message it gets buffered  | 
893  |  |      * (or dropped)--no further processing at this time  | 
894  |  |      * While listening, we accept seq 1 (ClientHello with cookie)  | 
895  |  |      * although we're still expecting seq 0 (ClientHello)  | 
896  |  |      */  | 
897  | 0  |     if (msg_hdr.seq != s->d1->handshake_read_seq) { | 
898  | 0  |         if (!s->server  | 
899  | 0  |                 || msg_hdr.seq != 0  | 
900  | 0  |                 || s->d1->handshake_read_seq != 1  | 
901  | 0  |                 || p[0] != SSL3_MT_CLIENT_HELLO  | 
902  | 0  |                 || s->statem.hand_state != DTLS_ST_SW_HELLO_VERIFY_REQUEST) { | 
903  | 0  |             *errtype = dtls1_process_out_of_seq_message(s, &msg_hdr);  | 
904  | 0  |             return 0;  | 
905  | 0  |         }  | 
906  |  |         /*  | 
907  |  |          * We received a ClientHello and sent back a HelloVerifyRequest. We  | 
908  |  |          * now seem to have received a retransmitted initial ClientHello. That  | 
909  |  |          * is allowed (possibly our HelloVerifyRequest got lost).  | 
910  |  |          */  | 
911  | 0  |         chretran = 1;  | 
912  | 0  |     }  | 
913  |  |  | 
914  | 0  |     if (frag_len && frag_len < mlen) { | 
915  | 0  |         *errtype = dtls1_reassemble_fragment(s, &msg_hdr);  | 
916  | 0  |         return 0;  | 
917  | 0  |     }  | 
918  |  |  | 
919  | 0  |     if (!s->server  | 
920  | 0  |             && s->d1->r_msg_hdr.frag_off == 0  | 
921  | 0  |             && s->statem.hand_state != TLS_ST_OK  | 
922  | 0  |             && p[0] == SSL3_MT_HELLO_REQUEST) { | 
923  |  |         /*  | 
924  |  |          * The server may always send 'Hello Request' messages -- we are  | 
925  |  |          * doing a handshake anyway now, so ignore them if their format is  | 
926  |  |          * correct. Does not count for 'Finished' MAC.  | 
927  |  |          */  | 
928  | 0  |         if (p[1] == 0 && p[2] == 0 && p[3] == 0) { | 
929  | 0  |             if (s->msg_callback)  | 
930  | 0  |                 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,  | 
931  | 0  |                                 p, DTLS1_HM_HEADER_LENGTH, ussl,  | 
932  | 0  |                                 s->msg_callback_arg);  | 
933  |  | 
  | 
934  | 0  |             s->init_num = 0;  | 
935  | 0  |             goto redo;  | 
936  | 0  |         } else {                /* Incorrectly formatted Hello request */ | 
937  |  | 
  | 
938  | 0  |             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);  | 
939  | 0  |             goto f_err;  | 
940  | 0  |         }  | 
941  | 0  |     }  | 
942  |  |  | 
943  | 0  |     if (!dtls1_preprocess_fragment(s, &msg_hdr)) { | 
944  |  |         /* SSLfatal() already called */  | 
945  | 0  |         goto f_err;  | 
946  | 0  |     }  | 
947  |  |  | 
948  | 0  |     if (frag_len > 0) { | 
949  | 0  |         p += DTLS1_HM_HEADER_LENGTH;  | 
950  |  | 
  | 
951  | 0  |         i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,  | 
952  | 0  |                                         &p[frag_off], frag_len, 0, &readbytes);  | 
953  |  |  | 
954  |  |         /*  | 
955  |  |          * This shouldn't ever fail due to NBIO because we already checked  | 
956  |  |          * that we have enough data in the record  | 
957  |  |          */  | 
958  | 0  |         if (i <= 0) { | 
959  | 0  |             s->rwstate = SSL_READING;  | 
960  | 0  |             *len = 0;  | 
961  | 0  |             return 0;  | 
962  | 0  |         }  | 
963  | 0  |     } else { | 
964  | 0  |         readbytes = 0;  | 
965  | 0  |     }  | 
966  |  |  | 
967  |  |     /*  | 
968  |  |      * XDTLS: an incorrectly formatted fragment should cause the handshake  | 
969  |  |      * to fail  | 
970  |  |      */  | 
971  | 0  |     if (readbytes != frag_len) { | 
972  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_LENGTH);  | 
973  | 0  |         goto f_err;  | 
974  | 0  |     }  | 
975  |  |  | 
976  | 0  |     if (chretran) { | 
977  |  |         /*  | 
978  |  |          * We got a new ClientHello with a message sequence of 0.  | 
979  |  |          * Reset the read/write sequences back to the beginning.  | 
980  |  |          * We process it like this is the first time we've seen a ClientHello  | 
981  |  |          * from the client.  | 
982  |  |          */  | 
983  | 0  |         s->d1->handshake_read_seq = 0;  | 
984  | 0  |         s->d1->next_handshake_write_seq = 0;  | 
985  | 0  |     }  | 
986  |  |  | 
987  |  |     /*  | 
988  |  |      * Note that s->init_num is *not* used as current offset in  | 
989  |  |      * s->init_buf->data, but as a counter summing up fragments' lengths: as  | 
990  |  |      * soon as they sum up to handshake packet length, we assume we have got  | 
991  |  |      * all the fragments.  | 
992  |  |      */  | 
993  | 0  |     *len = s->init_num = frag_len;  | 
994  | 0  |     return 1;  | 
995  |  |  | 
996  | 0  |  f_err:  | 
997  | 0  |     s->init_num = 0;  | 
998  | 0  |     *len = 0;  | 
999  | 0  |     return 0;  | 
1000  | 0  | }  | 
1001  |  |  | 
1002  |  | /*-  | 
1003  |  |  * for these 2 messages, we need to  | 
1004  |  |  * ssl->session->read_sym_enc           assign  | 
1005  |  |  * ssl->session->read_compression       assign  | 
1006  |  |  * ssl->session->read_hash              assign  | 
1007  |  |  */  | 
1008  |  | CON_FUNC_RETURN dtls_construct_change_cipher_spec(SSL_CONNECTION *s,  | 
1009  |  |                                                   WPACKET *pkt)  | 
1010  | 0  | { | 
1011  | 0  |     if (s->version == DTLS1_BAD_VER) { | 
1012  | 0  |         s->d1->next_handshake_write_seq++;  | 
1013  |  | 
  | 
1014  | 0  |         if (!WPACKET_put_bytes_u16(pkt, s->d1->handshake_write_seq)) { | 
1015  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1016  | 0  |             return CON_FUNC_ERROR;  | 
1017  | 0  |         }  | 
1018  | 0  |     }  | 
1019  |  |  | 
1020  | 0  |     return CON_FUNC_SUCCESS;  | 
1021  | 0  | }  | 
1022  |  |  | 
1023  |  | #ifndef OPENSSL_NO_SCTP  | 
1024  |  | /*  | 
1025  |  |  * Wait for a dry event. Should only be called at a point in the handshake  | 
1026  |  |  * where we are not expecting any data from the peer except an alert.  | 
1027  |  |  */  | 
1028  |  | WORK_STATE dtls_wait_for_dry(SSL_CONNECTION *s)  | 
1029  |  | { | 
1030  |  |     int ret, errtype;  | 
1031  |  |     size_t len;  | 
1032  |  |     SSL *ssl = SSL_CONNECTION_GET_SSL(s);  | 
1033  |  |  | 
1034  |  |     /* read app data until dry event */  | 
1035  |  |     ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(ssl));  | 
1036  |  |     if (ret < 0) { | 
1037  |  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1038  |  |         return WORK_ERROR;  | 
1039  |  |     }  | 
1040  |  |  | 
1041  |  |     if (ret == 0) { | 
1042  |  |         /*  | 
1043  |  |          * We're not expecting any more messages from the peer at this point -  | 
1044  |  |          * but we could get an alert. If an alert is waiting then we will never  | 
1045  |  |          * return successfully. Therefore we attempt to read a message. This  | 
1046  |  |          * should never succeed but will process any waiting alerts.  | 
1047  |  |          */  | 
1048  |  |         if (dtls_get_reassembled_message(s, &errtype, &len)) { | 
1049  |  |             /* The call succeeded! This should never happen */  | 
1050  |  |             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);  | 
1051  |  |             return WORK_ERROR;  | 
1052  |  |         }  | 
1053  |  |  | 
1054  |  |         s->s3.in_read_app_data = 2;  | 
1055  |  |         s->rwstate = SSL_READING;  | 
1056  |  |         BIO_clear_retry_flags(SSL_get_rbio(ssl));  | 
1057  |  |         BIO_set_retry_read(SSL_get_rbio(ssl));  | 
1058  |  |         return WORK_MORE_A;  | 
1059  |  |     }  | 
1060  |  |     return WORK_FINISHED_CONTINUE;  | 
1061  |  | }  | 
1062  |  | #endif  | 
1063  |  |  | 
1064  |  | int dtls1_read_failed(SSL_CONNECTION *s, int code)  | 
1065  | 0  | { | 
1066  | 0  |     SSL *ssl = SSL_CONNECTION_GET_SSL(s);  | 
1067  |  | 
  | 
1068  | 0  |     if (code > 0) { | 
1069  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1070  | 0  |         return 0;  | 
1071  | 0  |     }  | 
1072  |  |  | 
1073  | 0  |     if (!dtls1_is_timer_expired(s) || ossl_statem_in_error(s)) { | 
1074  |  |         /*  | 
1075  |  |          * not a timeout, none of our business, let higher layers handle  | 
1076  |  |          * this.  in fact it's probably an error  | 
1077  |  |          */  | 
1078  | 0  |         return code;  | 
1079  | 0  |     }  | 
1080  |  |     /* done, no need to send a retransmit */  | 
1081  | 0  |     if (!SSL_in_init(ssl)) { | 
1082  | 0  |         BIO_set_flags(SSL_get_rbio(ssl), BIO_FLAGS_READ);  | 
1083  | 0  |         return code;  | 
1084  | 0  |     }  | 
1085  |  |  | 
1086  | 0  |     return dtls1_handle_timeout(s);  | 
1087  | 0  | }  | 
1088  |  |  | 
1089  |  | int dtls1_get_queue_priority(unsigned short seq, int is_ccs)  | 
1090  | 0  | { | 
1091  |  |     /*  | 
1092  |  |      * The index of the retransmission queue actually is the message sequence  | 
1093  |  |      * number, since the queue only contains messages of a single handshake.  | 
1094  |  |      * However, the ChangeCipherSpec has no message sequence number and so  | 
1095  |  |      * using only the sequence will result in the CCS and Finished having the  | 
1096  |  |      * same index. To prevent this, the sequence number is multiplied by 2.  | 
1097  |  |      * In case of a CCS 1 is subtracted. This does not only differ CSS and  | 
1098  |  |      * Finished, it also maintains the order of the index (important for  | 
1099  |  |      * priority queues) and fits in the unsigned short variable.  | 
1100  |  |      */  | 
1101  | 0  |     return seq * 2 - is_ccs;  | 
1102  | 0  | }  | 
1103  |  |  | 
1104  |  | int dtls1_retransmit_buffered_messages(SSL_CONNECTION *s)  | 
1105  | 0  | { | 
1106  | 0  |     pqueue *sent = s->d1->sent_messages;  | 
1107  | 0  |     piterator iter;  | 
1108  | 0  |     pitem *item;  | 
1109  | 0  |     hm_fragment *frag;  | 
1110  | 0  |     int found = 0;  | 
1111  |  | 
  | 
1112  | 0  |     iter = pqueue_iterator(sent);  | 
1113  |  | 
  | 
1114  | 0  |     for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) { | 
1115  | 0  |         frag = (hm_fragment *)item->data;  | 
1116  | 0  |         if (dtls1_retransmit_message(s, (unsigned short)  | 
1117  | 0  |                                      dtls1_get_queue_priority  | 
1118  | 0  |                                      (frag->msg_header.seq,  | 
1119  | 0  |                                       frag->msg_header.is_ccs), &found) <= 0)  | 
1120  | 0  |             return -1;  | 
1121  | 0  |     }  | 
1122  |  |  | 
1123  | 0  |     return 1;  | 
1124  | 0  | }  | 
1125  |  |  | 
1126  |  | int dtls1_buffer_message(SSL_CONNECTION *s, int is_ccs)  | 
1127  | 0  | { | 
1128  | 0  |     pitem *item;  | 
1129  | 0  |     hm_fragment *frag;  | 
1130  | 0  |     unsigned char seq64be[8];  | 
1131  |  |  | 
1132  |  |     /*  | 
1133  |  |      * this function is called immediately after a message has been  | 
1134  |  |      * serialized  | 
1135  |  |      */  | 
1136  | 0  |     if (!ossl_assert(s->init_off == 0))  | 
1137  | 0  |         return 0;  | 
1138  |  |  | 
1139  | 0  |     frag = dtls1_hm_fragment_new(s->init_num, 0);  | 
1140  | 0  |     if (frag == NULL)  | 
1141  | 0  |         return 0;  | 
1142  |  |  | 
1143  | 0  |     memcpy(frag->fragment, s->init_buf->data, s->init_num);  | 
1144  |  | 
  | 
1145  | 0  |     if (is_ccs) { | 
1146  |  |         /* For DTLS1_BAD_VER the header length is non-standard */  | 
1147  | 0  |         if (!ossl_assert(s->d1->w_msg_hdr.msg_len +  | 
1148  | 0  |                          ((s->version ==  | 
1149  | 0  |                            DTLS1_BAD_VER) ? 3 : DTLS1_CCS_HEADER_LENGTH)  | 
1150  | 0  |                          == (unsigned int)s->init_num)) { | 
1151  | 0  |             dtls1_hm_fragment_free(frag);  | 
1152  | 0  |             return 0;  | 
1153  | 0  |         }  | 
1154  | 0  |     } else { | 
1155  | 0  |         if (!ossl_assert(s->d1->w_msg_hdr.msg_len +  | 
1156  | 0  |                          DTLS1_HM_HEADER_LENGTH == (unsigned int)s->init_num)) { | 
1157  | 0  |             dtls1_hm_fragment_free(frag);  | 
1158  | 0  |             return 0;  | 
1159  | 0  |         }  | 
1160  | 0  |     }  | 
1161  |  |  | 
1162  | 0  |     frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;  | 
1163  | 0  |     frag->msg_header.seq = s->d1->w_msg_hdr.seq;  | 
1164  | 0  |     frag->msg_header.type = s->d1->w_msg_hdr.type;  | 
1165  | 0  |     frag->msg_header.frag_off = 0;  | 
1166  | 0  |     frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;  | 
1167  | 0  |     frag->msg_header.is_ccs = is_ccs;  | 
1168  |  |  | 
1169  |  |     /* save current state */  | 
1170  | 0  |     frag->msg_header.saved_retransmit_state.wrlmethod = s->rlayer.wrlmethod;  | 
1171  | 0  |     frag->msg_header.saved_retransmit_state.wrl = s->rlayer.wrl;  | 
1172  |  |  | 
1173  |  | 
  | 
1174  | 0  |     memset(seq64be, 0, sizeof(seq64be));  | 
1175  | 0  |     seq64be[6] =  | 
1176  | 0  |         (unsigned  | 
1177  | 0  |          char)(dtls1_get_queue_priority(frag->msg_header.seq,  | 
1178  | 0  |                                         frag->msg_header.is_ccs) >> 8);  | 
1179  | 0  |     seq64be[7] =  | 
1180  | 0  |         (unsigned  | 
1181  | 0  |          char)(dtls1_get_queue_priority(frag->msg_header.seq,  | 
1182  | 0  |                                         frag->msg_header.is_ccs));  | 
1183  |  | 
  | 
1184  | 0  |     item = pitem_new(seq64be, frag);  | 
1185  | 0  |     if (item == NULL) { | 
1186  | 0  |         dtls1_hm_fragment_free(frag);  | 
1187  | 0  |         return 0;  | 
1188  | 0  |     }  | 
1189  |  |  | 
1190  | 0  |     pqueue_insert(s->d1->sent_messages, item);  | 
1191  | 0  |     return 1;  | 
1192  | 0  | }  | 
1193  |  |  | 
1194  |  | int dtls1_retransmit_message(SSL_CONNECTION *s, unsigned short seq, int *found)  | 
1195  | 0  | { | 
1196  | 0  |     int ret;  | 
1197  |  |     /* XDTLS: for now assuming that read/writes are blocking */  | 
1198  | 0  |     pitem *item;  | 
1199  | 0  |     hm_fragment *frag;  | 
1200  | 0  |     unsigned long header_length;  | 
1201  | 0  |     unsigned char seq64be[8];  | 
1202  | 0  |     struct dtls1_retransmit_state saved_state;  | 
1203  |  |  | 
1204  |  |     /* XDTLS:  the requested message ought to be found, otherwise error */  | 
1205  | 0  |     memset(seq64be, 0, sizeof(seq64be));  | 
1206  | 0  |     seq64be[6] = (unsigned char)(seq >> 8);  | 
1207  | 0  |     seq64be[7] = (unsigned char)seq;  | 
1208  |  | 
  | 
1209  | 0  |     item = pqueue_find(s->d1->sent_messages, seq64be);  | 
1210  | 0  |     if (item == NULL) { | 
1211  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1212  | 0  |         *found = 0;  | 
1213  | 0  |         return 0;  | 
1214  | 0  |     }  | 
1215  |  |  | 
1216  | 0  |     *found = 1;  | 
1217  | 0  |     frag = (hm_fragment *)item->data;  | 
1218  |  | 
  | 
1219  | 0  |     if (frag->msg_header.is_ccs)  | 
1220  | 0  |         header_length = DTLS1_CCS_HEADER_LENGTH;  | 
1221  | 0  |     else  | 
1222  | 0  |         header_length = DTLS1_HM_HEADER_LENGTH;  | 
1223  |  | 
  | 
1224  | 0  |     memcpy(s->init_buf->data, frag->fragment,  | 
1225  | 0  |            frag->msg_header.msg_len + header_length);  | 
1226  | 0  |     s->init_num = frag->msg_header.msg_len + header_length;  | 
1227  |  | 
  | 
1228  | 0  |     dtls1_set_message_header_int(s, frag->msg_header.type,  | 
1229  | 0  |                                  frag->msg_header.msg_len,  | 
1230  | 0  |                                  frag->msg_header.seq, 0,  | 
1231  | 0  |                                  frag->msg_header.frag_len);  | 
1232  |  |  | 
1233  |  |     /* save current state */  | 
1234  | 0  |     saved_state.wrlmethod = s->rlayer.wrlmethod;  | 
1235  | 0  |     saved_state.wrl = s->rlayer.wrl;  | 
1236  |  | 
  | 
1237  | 0  |     s->d1->retransmitting = 1;  | 
1238  |  |  | 
1239  |  |     /* restore state in which the message was originally sent */  | 
1240  | 0  |     s->rlayer.wrlmethod = frag->msg_header.saved_retransmit_state.wrlmethod;  | 
1241  | 0  |     s->rlayer.wrl = frag->msg_header.saved_retransmit_state.wrl;  | 
1242  |  |  | 
1243  |  |     /*  | 
1244  |  |      * The old wrl may be still pointing at an old BIO. Update it to what we're  | 
1245  |  |      * using now.  | 
1246  |  |      */  | 
1247  | 0  |     s->rlayer.wrlmethod->set1_bio(s->rlayer.wrl, s->wbio);  | 
1248  |  | 
  | 
1249  | 0  |     ret = dtls1_do_write(s, frag->msg_header.is_ccs ?  | 
1250  | 0  |                          SSL3_RT_CHANGE_CIPHER_SPEC : SSL3_RT_HANDSHAKE);  | 
1251  |  |  | 
1252  |  |     /* restore current state */  | 
1253  | 0  |     s->rlayer.wrlmethod = saved_state.wrlmethod;  | 
1254  | 0  |     s->rlayer.wrl = saved_state.wrl;  | 
1255  |  | 
  | 
1256  | 0  |     s->d1->retransmitting = 0;  | 
1257  |  | 
  | 
1258  | 0  |     (void)BIO_flush(s->wbio);  | 
1259  | 0  |     return ret;  | 
1260  | 0  | }  | 
1261  |  |  | 
1262  |  | void dtls1_set_message_header(SSL_CONNECTION *s,  | 
1263  |  |                               unsigned char mt, size_t len,  | 
1264  |  |                               size_t frag_off, size_t frag_len)  | 
1265  | 0  | { | 
1266  | 0  |     if (frag_off == 0) { | 
1267  | 0  |         s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;  | 
1268  | 0  |         s->d1->next_handshake_write_seq++;  | 
1269  | 0  |     }  | 
1270  |  | 
  | 
1271  | 0  |     dtls1_set_message_header_int(s, mt, len, s->d1->handshake_write_seq,  | 
1272  | 0  |                                  frag_off, frag_len);  | 
1273  | 0  | }  | 
1274  |  |  | 
1275  |  | /* don't actually do the writing, wait till the MTU has been retrieved */  | 
1276  |  | static void  | 
1277  |  | dtls1_set_message_header_int(SSL_CONNECTION *s, unsigned char mt,  | 
1278  |  |                              size_t len, unsigned short seq_num,  | 
1279  |  |                              size_t frag_off, size_t frag_len)  | 
1280  | 0  | { | 
1281  | 0  |     struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;  | 
1282  |  | 
  | 
1283  | 0  |     msg_hdr->type = mt;  | 
1284  | 0  |     msg_hdr->msg_len = len;  | 
1285  | 0  |     msg_hdr->seq = seq_num;  | 
1286  | 0  |     msg_hdr->frag_off = frag_off;  | 
1287  | 0  |     msg_hdr->frag_len = frag_len;  | 
1288  | 0  | }  | 
1289  |  |  | 
1290  |  | static void  | 
1291  |  | dtls1_fix_message_header(SSL_CONNECTION *s, size_t frag_off, size_t frag_len)  | 
1292  | 0  | { | 
1293  | 0  |     struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;  | 
1294  |  | 
  | 
1295  | 0  |     msg_hdr->frag_off = frag_off;  | 
1296  | 0  |     msg_hdr->frag_len = frag_len;  | 
1297  | 0  | }  | 
1298  |  |  | 
1299  |  | static unsigned char *dtls1_write_message_header(SSL_CONNECTION *s,  | 
1300  |  |                                                  unsigned char *p)  | 
1301  | 0  | { | 
1302  | 0  |     struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;  | 
1303  |  | 
  | 
1304  | 0  |     *p++ = msg_hdr->type;  | 
1305  | 0  |     l2n3(msg_hdr->msg_len, p);  | 
1306  |  | 
  | 
1307  | 0  |     s2n(msg_hdr->seq, p);  | 
1308  | 0  |     l2n3(msg_hdr->frag_off, p);  | 
1309  | 0  |     l2n3(msg_hdr->frag_len, p);  | 
1310  |  | 
  | 
1311  | 0  |     return p;  | 
1312  | 0  | }  | 
1313  |  |  | 
1314  |  | void dtls1_get_message_header(const unsigned char *data, struct  | 
1315  |  |                               hm_header_st *msg_hdr)  | 
1316  | 0  | { | 
1317  | 0  |     memset(msg_hdr, 0, sizeof(*msg_hdr));  | 
1318  | 0  |     msg_hdr->type = *(data++);  | 
1319  | 0  |     n2l3(data, msg_hdr->msg_len);  | 
1320  |  | 
  | 
1321  | 0  |     n2s(data, msg_hdr->seq);  | 
1322  | 0  |     n2l3(data, msg_hdr->frag_off);  | 
1323  | 0  |     n2l3(data, msg_hdr->frag_len);  | 
1324  | 0  | }  | 
1325  |  |  | 
1326  |  | int dtls1_set_handshake_header(SSL_CONNECTION *s, WPACKET *pkt, int htype)  | 
1327  | 0  | { | 
1328  | 0  |     unsigned char *header;  | 
1329  |  | 
  | 
1330  | 0  |     if (htype == SSL3_MT_CHANGE_CIPHER_SPEC) { | 
1331  | 0  |         s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;  | 
1332  | 0  |         dtls1_set_message_header_int(s, SSL3_MT_CCS, 0,  | 
1333  | 0  |                                      s->d1->handshake_write_seq, 0, 0);  | 
1334  | 0  |         if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS))  | 
1335  | 0  |             return 0;  | 
1336  | 0  |     } else { | 
1337  | 0  |         dtls1_set_message_header(s, htype, 0, 0, 0);  | 
1338  |  |         /*  | 
1339  |  |          * We allocate space at the start for the message header. This gets  | 
1340  |  |          * filled in later  | 
1341  |  |          */  | 
1342  | 0  |         if (!WPACKET_allocate_bytes(pkt, DTLS1_HM_HEADER_LENGTH, &header)  | 
1343  | 0  |                 || !WPACKET_start_sub_packet(pkt))  | 
1344  | 0  |             return 0;  | 
1345  | 0  |     }  | 
1346  |  |  | 
1347  | 0  |     return 1;  | 
1348  | 0  | }  | 
1349  |  |  | 
1350  |  | int dtls1_close_construct_packet(SSL_CONNECTION *s, WPACKET *pkt, int htype)  | 
1351  | 0  | { | 
1352  | 0  |     size_t msglen;  | 
1353  |  | 
  | 
1354  | 0  |     if ((htype != SSL3_MT_CHANGE_CIPHER_SPEC && !WPACKET_close(pkt))  | 
1355  | 0  |             || !WPACKET_get_length(pkt, &msglen)  | 
1356  | 0  |             || msglen > INT_MAX)  | 
1357  | 0  |         return 0;  | 
1358  |  |  | 
1359  | 0  |     if (htype != SSL3_MT_CHANGE_CIPHER_SPEC) { | 
1360  | 0  |         s->d1->w_msg_hdr.msg_len = msglen - DTLS1_HM_HEADER_LENGTH;  | 
1361  | 0  |         s->d1->w_msg_hdr.frag_len = msglen - DTLS1_HM_HEADER_LENGTH;  | 
1362  | 0  |     }  | 
1363  | 0  |     s->init_num = (int)msglen;  | 
1364  | 0  |     s->init_off = 0;  | 
1365  |  | 
  | 
1366  | 0  |     if (htype != DTLS1_MT_HELLO_VERIFY_REQUEST) { | 
1367  |  |         /* Buffer the message to handle re-xmits */  | 
1368  | 0  |         if (!dtls1_buffer_message(s, htype == SSL3_MT_CHANGE_CIPHER_SPEC  | 
1369  | 0  |                                      ? 1 : 0))  | 
1370  | 0  |             return 0;  | 
1371  | 0  |     }  | 
1372  |  |  | 
1373  | 0  |     return 1;  | 
1374  | 0  | }  |