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