/src/boringssl/ssl/d1_both.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * DTLS implementation written by Nagendra Modadugu |
3 | | * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. |
4 | | */ |
5 | | /* ==================================================================== |
6 | | * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. |
7 | | * |
8 | | * Redistribution and use in source and binary forms, with or without |
9 | | * modification, are permitted provided that the following conditions |
10 | | * are met: |
11 | | * |
12 | | * 1. Redistributions of source code must retain the above copyright |
13 | | * notice, this list of conditions and the following disclaimer. |
14 | | * |
15 | | * 2. Redistributions in binary form must reproduce the above copyright |
16 | | * notice, this list of conditions and the following disclaimer in |
17 | | * the documentation and/or other materials provided with the |
18 | | * distribution. |
19 | | * |
20 | | * 3. All advertising materials mentioning features or use of this |
21 | | * software must display the following acknowledgment: |
22 | | * "This product includes software developed by the OpenSSL Project |
23 | | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
24 | | * |
25 | | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
26 | | * endorse or promote products derived from this software without |
27 | | * prior written permission. For written permission, please contact |
28 | | * openssl-core@openssl.org. |
29 | | * |
30 | | * 5. Products derived from this software may not be called "OpenSSL" |
31 | | * nor may "OpenSSL" appear in their names without prior written |
32 | | * permission of the OpenSSL Project. |
33 | | * |
34 | | * 6. Redistributions of any form whatsoever must retain the following |
35 | | * acknowledgment: |
36 | | * "This product includes software developed by the OpenSSL Project |
37 | | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
38 | | * |
39 | | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
40 | | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
41 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
42 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
43 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
44 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
45 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
46 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
47 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
48 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
49 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
50 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
51 | | * ==================================================================== |
52 | | * |
53 | | * This product includes cryptographic software written by Eric Young |
54 | | * (eay@cryptsoft.com). This product includes software written by Tim |
55 | | * Hudson (tjh@cryptsoft.com). |
56 | | * |
57 | | */ |
58 | | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
59 | | * All rights reserved. |
60 | | * |
61 | | * This package is an SSL implementation written |
62 | | * by Eric Young (eay@cryptsoft.com). |
63 | | * The implementation was written so as to conform with Netscapes SSL. |
64 | | * |
65 | | * This library is free for commercial and non-commercial use as long as |
66 | | * the following conditions are aheared to. The following conditions |
67 | | * apply to all code found in this distribution, be it the RC4, RSA, |
68 | | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
69 | | * included with this distribution is covered by the same copyright terms |
70 | | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
71 | | * |
72 | | * Copyright remains Eric Young's, and as such any Copyright notices in |
73 | | * the code are not to be removed. |
74 | | * If this package is used in a product, Eric Young should be given attribution |
75 | | * as the author of the parts of the library used. |
76 | | * This can be in the form of a textual message at program startup or |
77 | | * in documentation (online or textual) provided with the package. |
78 | | * |
79 | | * Redistribution and use in source and binary forms, with or without |
80 | | * modification, are permitted provided that the following conditions |
81 | | * are met: |
82 | | * 1. Redistributions of source code must retain the copyright |
83 | | * notice, this list of conditions and the following disclaimer. |
84 | | * 2. Redistributions in binary form must reproduce the above copyright |
85 | | * notice, this list of conditions and the following disclaimer in the |
86 | | * documentation and/or other materials provided with the distribution. |
87 | | * 3. All advertising materials mentioning features or use of this software |
88 | | * must display the following acknowledgement: |
89 | | * "This product includes cryptographic software written by |
90 | | * Eric Young (eay@cryptsoft.com)" |
91 | | * The word 'cryptographic' can be left out if the rouines from the library |
92 | | * being used are not cryptographic related :-). |
93 | | * 4. If you include any Windows specific code (or a derivative thereof) from |
94 | | * the apps directory (application code) you must include an acknowledgement: |
95 | | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
96 | | * |
97 | | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
98 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
99 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
100 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
101 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
102 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
103 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
104 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
105 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
106 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
107 | | * SUCH DAMAGE. |
108 | | * |
109 | | * The licence and distribution terms for any publically available version or |
110 | | * derivative of this code cannot be changed. i.e. this code cannot simply be |
111 | | * copied and put under another distribution licence |
112 | | * [including the GNU Public Licence.] */ |
113 | | |
114 | | #include <openssl/ssl.h> |
115 | | |
116 | | #include <assert.h> |
117 | | #include <limits.h> |
118 | | #include <string.h> |
119 | | |
120 | | #include <openssl/err.h> |
121 | | #include <openssl/evp.h> |
122 | | #include <openssl/mem.h> |
123 | | #include <openssl/rand.h> |
124 | | |
125 | | #include "../crypto/internal.h" |
126 | | #include "internal.h" |
127 | | |
128 | | |
129 | | BSSL_NAMESPACE_BEGIN |
130 | | |
131 | | // TODO(davidben): 28 comes from the size of IP + UDP header. Is this reasonable |
132 | | // for these values? Notably, why is kMinMTU a function of the transport |
133 | | // protocol's overhead rather than, say, what's needed to hold a minimally-sized |
134 | | // handshake fragment plus protocol overhead. |
135 | | |
136 | | // kMinMTU is the minimum acceptable MTU value. |
137 | | static const unsigned int kMinMTU = 256 - 28; |
138 | | |
139 | | // kDefaultMTU is the default MTU value to use if neither the user nor |
140 | | // the underlying BIO supplies one. |
141 | | static const unsigned int kDefaultMTU = 1500 - 28; |
142 | | |
143 | | |
144 | | // Receiving handshake messages. |
145 | | |
146 | 0 | hm_fragment::~hm_fragment() { |
147 | 0 | OPENSSL_free(data); |
148 | 0 | OPENSSL_free(reassembly); |
149 | 0 | } |
150 | | |
151 | | static UniquePtr<hm_fragment> dtls1_hm_fragment_new( |
152 | 0 | const struct hm_header_st *msg_hdr) { |
153 | 0 | ScopedCBB cbb; |
154 | 0 | UniquePtr<hm_fragment> frag = MakeUnique<hm_fragment>(); |
155 | 0 | if (!frag) { |
156 | 0 | return nullptr; |
157 | 0 | } |
158 | 0 | frag->type = msg_hdr->type; |
159 | 0 | frag->seq = msg_hdr->seq; |
160 | 0 | frag->msg_len = msg_hdr->msg_len; |
161 | | |
162 | | // Allocate space for the reassembled message and fill in the header. |
163 | 0 | frag->data = |
164 | 0 | (uint8_t *)OPENSSL_malloc(DTLS1_HM_HEADER_LENGTH + msg_hdr->msg_len); |
165 | 0 | if (frag->data == NULL) { |
166 | 0 | return nullptr; |
167 | 0 | } |
168 | | |
169 | 0 | if (!CBB_init_fixed(cbb.get(), frag->data, DTLS1_HM_HEADER_LENGTH) || |
170 | 0 | !CBB_add_u8(cbb.get(), msg_hdr->type) || |
171 | 0 | !CBB_add_u24(cbb.get(), msg_hdr->msg_len) || |
172 | 0 | !CBB_add_u16(cbb.get(), msg_hdr->seq) || |
173 | 0 | !CBB_add_u24(cbb.get(), 0 /* frag_off */) || |
174 | 0 | !CBB_add_u24(cbb.get(), msg_hdr->msg_len) || |
175 | 0 | !CBB_finish(cbb.get(), NULL, NULL)) { |
176 | 0 | return nullptr; |
177 | 0 | } |
178 | | |
179 | | // If the handshake message is empty, |frag->reassembly| is NULL. |
180 | 0 | if (msg_hdr->msg_len > 0) { |
181 | | // Initialize reassembly bitmask. |
182 | 0 | if (msg_hdr->msg_len + 7 < msg_hdr->msg_len) { |
183 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
184 | 0 | return nullptr; |
185 | 0 | } |
186 | 0 | size_t bitmask_len = (msg_hdr->msg_len + 7) / 8; |
187 | 0 | frag->reassembly = (uint8_t *)OPENSSL_malloc(bitmask_len); |
188 | 0 | if (frag->reassembly == NULL) { |
189 | 0 | return nullptr; |
190 | 0 | } |
191 | 0 | OPENSSL_memset(frag->reassembly, 0, bitmask_len); |
192 | 0 | } |
193 | | |
194 | 0 | return frag; |
195 | 0 | } |
196 | | |
197 | | // bit_range returns a |uint8_t| with bits |start|, inclusive, to |end|, |
198 | | // exclusive, set. |
199 | 0 | static uint8_t bit_range(size_t start, size_t end) { |
200 | 0 | return (uint8_t)(~((1u << start) - 1) & ((1u << end) - 1)); |
201 | 0 | } |
202 | | |
203 | | // dtls1_hm_fragment_mark marks bytes |start|, inclusive, to |end|, exclusive, |
204 | | // as received in |frag|. If |frag| becomes complete, it clears |
205 | | // |frag->reassembly|. The range must be within the bounds of |frag|'s message |
206 | | // and |frag->reassembly| must not be NULL. |
207 | | static void dtls1_hm_fragment_mark(hm_fragment *frag, size_t start, |
208 | 0 | size_t end) { |
209 | 0 | size_t msg_len = frag->msg_len; |
210 | |
|
211 | 0 | if (frag->reassembly == NULL || start > end || end > msg_len) { |
212 | 0 | assert(0); |
213 | 0 | return; |
214 | 0 | } |
215 | | // A zero-length message will never have a pending reassembly. |
216 | 0 | assert(msg_len > 0); |
217 | | |
218 | 0 | if (start == end) { |
219 | 0 | return; |
220 | 0 | } |
221 | | |
222 | 0 | if ((start >> 3) == (end >> 3)) { |
223 | 0 | frag->reassembly[start >> 3] |= bit_range(start & 7, end & 7); |
224 | 0 | } else { |
225 | 0 | frag->reassembly[start >> 3] |= bit_range(start & 7, 8); |
226 | 0 | for (size_t i = (start >> 3) + 1; i < (end >> 3); i++) { |
227 | 0 | frag->reassembly[i] = 0xff; |
228 | 0 | } |
229 | 0 | if ((end & 7) != 0) { |
230 | 0 | frag->reassembly[end >> 3] |= bit_range(0, end & 7); |
231 | 0 | } |
232 | 0 | } |
233 | | |
234 | | // Check if the fragment is complete. |
235 | 0 | for (size_t i = 0; i < (msg_len >> 3); i++) { |
236 | 0 | if (frag->reassembly[i] != 0xff) { |
237 | 0 | return; |
238 | 0 | } |
239 | 0 | } |
240 | 0 | if ((msg_len & 7) != 0 && |
241 | 0 | frag->reassembly[msg_len >> 3] != bit_range(0, msg_len & 7)) { |
242 | 0 | return; |
243 | 0 | } |
244 | | |
245 | 0 | OPENSSL_free(frag->reassembly); |
246 | 0 | frag->reassembly = NULL; |
247 | 0 | } |
248 | | |
249 | | // dtls1_is_current_message_complete returns whether the current handshake |
250 | | // message is complete. |
251 | 0 | static bool dtls1_is_current_message_complete(const SSL *ssl) { |
252 | 0 | size_t idx = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT; |
253 | 0 | hm_fragment *frag = ssl->d1->incoming_messages[idx].get(); |
254 | 0 | return frag != NULL && frag->reassembly == NULL; |
255 | 0 | } |
256 | | |
257 | | // dtls1_get_incoming_message returns the incoming message corresponding to |
258 | | // |msg_hdr|. If none exists, it creates a new one and inserts it in the |
259 | | // queue. Otherwise, it checks |msg_hdr| is consistent with the existing one. It |
260 | | // returns NULL on failure. The caller does not take ownership of the result. |
261 | | static hm_fragment *dtls1_get_incoming_message( |
262 | 0 | SSL *ssl, uint8_t *out_alert, const struct hm_header_st *msg_hdr) { |
263 | 0 | if (msg_hdr->seq < ssl->d1->handshake_read_seq || |
264 | 0 | msg_hdr->seq - ssl->d1->handshake_read_seq >= SSL_MAX_HANDSHAKE_FLIGHT) { |
265 | 0 | *out_alert = SSL_AD_INTERNAL_ERROR; |
266 | 0 | return NULL; |
267 | 0 | } |
268 | | |
269 | 0 | size_t idx = msg_hdr->seq % SSL_MAX_HANDSHAKE_FLIGHT; |
270 | 0 | hm_fragment *frag = ssl->d1->incoming_messages[idx].get(); |
271 | 0 | if (frag != NULL) { |
272 | 0 | assert(frag->seq == msg_hdr->seq); |
273 | | // The new fragment must be compatible with the previous fragments from this |
274 | | // message. |
275 | 0 | if (frag->type != msg_hdr->type || |
276 | 0 | frag->msg_len != msg_hdr->msg_len) { |
277 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_FRAGMENT_MISMATCH); |
278 | 0 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
279 | 0 | return NULL; |
280 | 0 | } |
281 | 0 | return frag; |
282 | 0 | } |
283 | | |
284 | | // This is the first fragment from this message. |
285 | 0 | ssl->d1->incoming_messages[idx] = dtls1_hm_fragment_new(msg_hdr); |
286 | 0 | if (!ssl->d1->incoming_messages[idx]) { |
287 | 0 | *out_alert = SSL_AD_INTERNAL_ERROR; |
288 | 0 | return NULL; |
289 | 0 | } |
290 | 0 | return ssl->d1->incoming_messages[idx].get(); |
291 | 0 | } |
292 | | |
293 | | ssl_open_record_t dtls1_open_handshake(SSL *ssl, size_t *out_consumed, |
294 | 0 | uint8_t *out_alert, Span<uint8_t> in) { |
295 | 0 | uint8_t type; |
296 | 0 | Span<uint8_t> record; |
297 | 0 | auto ret = dtls_open_record(ssl, &type, &record, out_consumed, out_alert, in); |
298 | 0 | if (ret != ssl_open_record_success) { |
299 | 0 | return ret; |
300 | 0 | } |
301 | | |
302 | 0 | switch (type) { |
303 | 0 | case SSL3_RT_APPLICATION_DATA: |
304 | | // Unencrypted application data records are always illegal. |
305 | 0 | if (ssl->s3->aead_read_ctx->is_null_cipher()) { |
306 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
307 | 0 | *out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
308 | 0 | return ssl_open_record_error; |
309 | 0 | } |
310 | | |
311 | | // Out-of-order application data may be received between ChangeCipherSpec |
312 | | // and finished. Discard it. |
313 | 0 | return ssl_open_record_discard; |
314 | | |
315 | 0 | case SSL3_RT_CHANGE_CIPHER_SPEC: |
316 | | // We do not support renegotiation, so encrypted ChangeCipherSpec records |
317 | | // are illegal. |
318 | 0 | if (!ssl->s3->aead_read_ctx->is_null_cipher()) { |
319 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
320 | 0 | *out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
321 | 0 | return ssl_open_record_error; |
322 | 0 | } |
323 | | |
324 | 0 | if (record.size() != 1u || record[0] != SSL3_MT_CCS) { |
325 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC); |
326 | 0 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
327 | 0 | return ssl_open_record_error; |
328 | 0 | } |
329 | | |
330 | | // Flag the ChangeCipherSpec for later. |
331 | 0 | ssl->d1->has_change_cipher_spec = true; |
332 | 0 | ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_CHANGE_CIPHER_SPEC, |
333 | 0 | record); |
334 | 0 | return ssl_open_record_success; |
335 | | |
336 | 0 | case SSL3_RT_HANDSHAKE: |
337 | | // Break out to main processing. |
338 | 0 | break; |
339 | | |
340 | 0 | default: |
341 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
342 | 0 | *out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
343 | 0 | return ssl_open_record_error; |
344 | 0 | } |
345 | | |
346 | 0 | CBS cbs; |
347 | 0 | CBS_init(&cbs, record.data(), record.size()); |
348 | 0 | while (CBS_len(&cbs) > 0) { |
349 | | // Read a handshake fragment. |
350 | 0 | struct hm_header_st msg_hdr; |
351 | 0 | CBS body; |
352 | 0 | if (!dtls1_parse_fragment(&cbs, &msg_hdr, &body)) { |
353 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD); |
354 | 0 | *out_alert = SSL_AD_DECODE_ERROR; |
355 | 0 | return ssl_open_record_error; |
356 | 0 | } |
357 | | |
358 | 0 | const size_t frag_off = msg_hdr.frag_off; |
359 | 0 | const size_t frag_len = msg_hdr.frag_len; |
360 | 0 | const size_t msg_len = msg_hdr.msg_len; |
361 | 0 | if (frag_off > msg_len || frag_off + frag_len < frag_off || |
362 | 0 | frag_off + frag_len > msg_len || |
363 | 0 | msg_len > ssl_max_handshake_message_len(ssl)) { |
364 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE); |
365 | 0 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
366 | 0 | return ssl_open_record_error; |
367 | 0 | } |
368 | | |
369 | | // The encrypted epoch in DTLS has only one handshake message. |
370 | 0 | if (ssl->d1->r_epoch == 1 && msg_hdr.seq != ssl->d1->handshake_read_seq) { |
371 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
372 | 0 | *out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
373 | 0 | return ssl_open_record_error; |
374 | 0 | } |
375 | | |
376 | 0 | if (msg_hdr.seq < ssl->d1->handshake_read_seq || |
377 | 0 | msg_hdr.seq > |
378 | 0 | (unsigned)ssl->d1->handshake_read_seq + SSL_MAX_HANDSHAKE_FLIGHT) { |
379 | | // Ignore fragments from the past, or ones too far in the future. |
380 | 0 | continue; |
381 | 0 | } |
382 | | |
383 | 0 | hm_fragment *frag = dtls1_get_incoming_message(ssl, out_alert, &msg_hdr); |
384 | 0 | if (frag == NULL) { |
385 | 0 | return ssl_open_record_error; |
386 | 0 | } |
387 | 0 | assert(frag->msg_len == msg_len); |
388 | | |
389 | 0 | if (frag->reassembly == NULL) { |
390 | | // The message is already assembled. |
391 | 0 | continue; |
392 | 0 | } |
393 | 0 | assert(msg_len > 0); |
394 | | |
395 | | // Copy the body into the fragment. |
396 | 0 | OPENSSL_memcpy(frag->data + DTLS1_HM_HEADER_LENGTH + frag_off, |
397 | 0 | CBS_data(&body), CBS_len(&body)); |
398 | 0 | dtls1_hm_fragment_mark(frag, frag_off, frag_off + frag_len); |
399 | 0 | } |
400 | | |
401 | 0 | return ssl_open_record_success; |
402 | 0 | } |
403 | | |
404 | 0 | bool dtls1_get_message(const SSL *ssl, SSLMessage *out) { |
405 | 0 | if (!dtls1_is_current_message_complete(ssl)) { |
406 | 0 | return false; |
407 | 0 | } |
408 | | |
409 | 0 | size_t idx = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT; |
410 | 0 | hm_fragment *frag = ssl->d1->incoming_messages[idx].get(); |
411 | 0 | out->type = frag->type; |
412 | 0 | CBS_init(&out->body, frag->data + DTLS1_HM_HEADER_LENGTH, frag->msg_len); |
413 | 0 | CBS_init(&out->raw, frag->data, DTLS1_HM_HEADER_LENGTH + frag->msg_len); |
414 | 0 | out->is_v2_hello = false; |
415 | 0 | if (!ssl->s3->has_message) { |
416 | 0 | ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HANDSHAKE, out->raw); |
417 | 0 | ssl->s3->has_message = true; |
418 | 0 | } |
419 | 0 | return true; |
420 | 0 | } |
421 | | |
422 | 0 | void dtls1_next_message(SSL *ssl) { |
423 | 0 | assert(ssl->s3->has_message); |
424 | 0 | assert(dtls1_is_current_message_complete(ssl)); |
425 | 0 | size_t index = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT; |
426 | 0 | ssl->d1->incoming_messages[index].reset(); |
427 | 0 | ssl->d1->handshake_read_seq++; |
428 | 0 | ssl->s3->has_message = false; |
429 | | // If we previously sent a flight, mark it as having a reply, so |
430 | | // |on_handshake_complete| can manage post-handshake retransmission. |
431 | 0 | if (ssl->d1->outgoing_messages_complete) { |
432 | 0 | ssl->d1->flight_has_reply = true; |
433 | 0 | } |
434 | 0 | } |
435 | | |
436 | 0 | bool dtls_has_unprocessed_handshake_data(const SSL *ssl) { |
437 | 0 | size_t current = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT; |
438 | 0 | for (size_t i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) { |
439 | | // Skip the current message. |
440 | 0 | if (ssl->s3->has_message && i == current) { |
441 | 0 | assert(dtls1_is_current_message_complete(ssl)); |
442 | 0 | continue; |
443 | 0 | } |
444 | 0 | if (ssl->d1->incoming_messages[i] != nullptr) { |
445 | 0 | return true; |
446 | 0 | } |
447 | 0 | } |
448 | 0 | return false; |
449 | 0 | } |
450 | | |
451 | | bool dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr, |
452 | 0 | CBS *out_body) { |
453 | 0 | OPENSSL_memset(out_hdr, 0x00, sizeof(struct hm_header_st)); |
454 | |
|
455 | 0 | if (!CBS_get_u8(cbs, &out_hdr->type) || |
456 | 0 | !CBS_get_u24(cbs, &out_hdr->msg_len) || |
457 | 0 | !CBS_get_u16(cbs, &out_hdr->seq) || |
458 | 0 | !CBS_get_u24(cbs, &out_hdr->frag_off) || |
459 | 0 | !CBS_get_u24(cbs, &out_hdr->frag_len) || |
460 | 0 | !CBS_get_bytes(cbs, out_body, out_hdr->frag_len)) { |
461 | 0 | return false; |
462 | 0 | } |
463 | | |
464 | 0 | return true; |
465 | 0 | } |
466 | | |
467 | | ssl_open_record_t dtls1_open_change_cipher_spec(SSL *ssl, size_t *out_consumed, |
468 | | uint8_t *out_alert, |
469 | 0 | Span<uint8_t> in) { |
470 | 0 | if (!ssl->d1->has_change_cipher_spec) { |
471 | | // dtls1_open_handshake processes both handshake and ChangeCipherSpec. |
472 | 0 | auto ret = dtls1_open_handshake(ssl, out_consumed, out_alert, in); |
473 | 0 | if (ret != ssl_open_record_success) { |
474 | 0 | return ret; |
475 | 0 | } |
476 | 0 | } |
477 | 0 | if (ssl->d1->has_change_cipher_spec) { |
478 | 0 | ssl->d1->has_change_cipher_spec = false; |
479 | 0 | return ssl_open_record_success; |
480 | 0 | } |
481 | 0 | return ssl_open_record_discard; |
482 | 0 | } |
483 | | |
484 | | |
485 | | // Sending handshake messages. |
486 | | |
487 | 0 | void DTLS_OUTGOING_MESSAGE::Clear() { data.Reset(); } |
488 | | |
489 | 0 | void dtls_clear_outgoing_messages(SSL *ssl) { |
490 | 0 | for (size_t i = 0; i < ssl->d1->outgoing_messages_len; i++) { |
491 | 0 | ssl->d1->outgoing_messages[i].Clear(); |
492 | 0 | } |
493 | 0 | ssl->d1->outgoing_messages_len = 0; |
494 | 0 | ssl->d1->outgoing_written = 0; |
495 | 0 | ssl->d1->outgoing_offset = 0; |
496 | 0 | ssl->d1->outgoing_messages_complete = false; |
497 | 0 | ssl->d1->flight_has_reply = false; |
498 | 0 | } |
499 | | |
500 | 0 | bool dtls1_init_message(const SSL *ssl, CBB *cbb, CBB *body, uint8_t type) { |
501 | | // Pick a modest size hint to save most of the |realloc| calls. |
502 | 0 | if (!CBB_init(cbb, 64) || |
503 | 0 | !CBB_add_u8(cbb, type) || |
504 | 0 | !CBB_add_u24(cbb, 0 /* length (filled in later) */) || |
505 | 0 | !CBB_add_u16(cbb, ssl->d1->handshake_write_seq) || |
506 | 0 | !CBB_add_u24(cbb, 0 /* offset */) || |
507 | 0 | !CBB_add_u24_length_prefixed(cbb, body)) { |
508 | 0 | return false; |
509 | 0 | } |
510 | | |
511 | 0 | return true; |
512 | 0 | } |
513 | | |
514 | 0 | bool dtls1_finish_message(const SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg) { |
515 | 0 | if (!CBBFinishArray(cbb, out_msg) || |
516 | 0 | out_msg->size() < DTLS1_HM_HEADER_LENGTH) { |
517 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
518 | 0 | return false; |
519 | 0 | } |
520 | | |
521 | | // Fix up the header. Copy the fragment length into the total message |
522 | | // length. |
523 | 0 | OPENSSL_memcpy(out_msg->data() + 1, |
524 | 0 | out_msg->data() + DTLS1_HM_HEADER_LENGTH - 3, 3); |
525 | 0 | return true; |
526 | 0 | } |
527 | | |
528 | | // ssl_size_t_greater_than_32_bits returns whether |v| exceeds the bounds of a |
529 | | // 32-bit value. The obvious thing doesn't work because, in some 32-bit build |
530 | | // configurations, the compiler warns that the test is always false and breaks |
531 | | // the build. |
532 | 0 | static bool ssl_size_t_greater_than_32_bits(size_t v) { |
533 | 0 | #if defined(OPENSSL_64_BIT) |
534 | 0 | return v > 0xffffffff; |
535 | | #elif defined(OPENSSL_32_BIT) |
536 | | return false; |
537 | | #else |
538 | | #error "Building for neither 32- nor 64-bits." |
539 | | #endif |
540 | 0 | } |
541 | | |
542 | | // add_outgoing adds a new handshake message or ChangeCipherSpec to the current |
543 | | // outgoing flight. It returns true on success and false on error. |
544 | 0 | static bool add_outgoing(SSL *ssl, bool is_ccs, Array<uint8_t> data) { |
545 | 0 | if (ssl->d1->outgoing_messages_complete) { |
546 | | // If we've begun writing a new flight, we received the peer flight. Discard |
547 | | // the timer and the our flight. |
548 | 0 | dtls1_stop_timer(ssl); |
549 | 0 | dtls_clear_outgoing_messages(ssl); |
550 | 0 | } |
551 | |
|
552 | 0 | static_assert(SSL_MAX_HANDSHAKE_FLIGHT < |
553 | 0 | (1 << 8 * sizeof(ssl->d1->outgoing_messages_len)), |
554 | 0 | "outgoing_messages_len is too small"); |
555 | 0 | if (ssl->d1->outgoing_messages_len >= SSL_MAX_HANDSHAKE_FLIGHT || |
556 | 0 | ssl_size_t_greater_than_32_bits(data.size())) { |
557 | 0 | assert(false); |
558 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
559 | 0 | return false; |
560 | 0 | } |
561 | | |
562 | 0 | if (!is_ccs) { |
563 | | // TODO(svaldez): Move this up a layer to fix abstraction for SSLTranscript |
564 | | // on hs. |
565 | 0 | if (ssl->s3->hs != NULL && |
566 | 0 | !ssl->s3->hs->transcript.Update(data)) { |
567 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
568 | 0 | return false; |
569 | 0 | } |
570 | 0 | ssl->d1->handshake_write_seq++; |
571 | 0 | } |
572 | | |
573 | 0 | DTLS_OUTGOING_MESSAGE *msg = |
574 | 0 | &ssl->d1->outgoing_messages[ssl->d1->outgoing_messages_len]; |
575 | 0 | msg->data = std::move(data); |
576 | 0 | msg->epoch = ssl->d1->w_epoch; |
577 | 0 | msg->is_ccs = is_ccs; |
578 | |
|
579 | 0 | ssl->d1->outgoing_messages_len++; |
580 | 0 | return true; |
581 | 0 | } |
582 | | |
583 | 0 | bool dtls1_add_message(SSL *ssl, Array<uint8_t> data) { |
584 | 0 | return add_outgoing(ssl, false /* handshake */, std::move(data)); |
585 | 0 | } |
586 | | |
587 | 0 | bool dtls1_add_change_cipher_spec(SSL *ssl) { |
588 | 0 | return add_outgoing(ssl, true /* ChangeCipherSpec */, Array<uint8_t>()); |
589 | 0 | } |
590 | | |
591 | | // dtls1_update_mtu updates the current MTU from the BIO, ensuring it is above |
592 | | // the minimum. |
593 | 0 | static void dtls1_update_mtu(SSL *ssl) { |
594 | | // TODO(davidben): No consumer implements |BIO_CTRL_DGRAM_SET_MTU| and the |
595 | | // only |BIO_CTRL_DGRAM_QUERY_MTU| implementation could use |
596 | | // |SSL_set_mtu|. Does this need to be so complex? |
597 | 0 | if (ssl->d1->mtu < dtls1_min_mtu() && |
598 | 0 | !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) { |
599 | 0 | long mtu = BIO_ctrl(ssl->wbio.get(), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL); |
600 | 0 | if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) { |
601 | 0 | ssl->d1->mtu = (unsigned)mtu; |
602 | 0 | } else { |
603 | 0 | ssl->d1->mtu = kDefaultMTU; |
604 | 0 | BIO_ctrl(ssl->wbio.get(), BIO_CTRL_DGRAM_SET_MTU, ssl->d1->mtu, NULL); |
605 | 0 | } |
606 | 0 | } |
607 | | |
608 | | // The MTU should be above the minimum now. |
609 | 0 | assert(ssl->d1->mtu >= dtls1_min_mtu()); |
610 | 0 | } |
611 | | |
612 | | enum seal_result_t { |
613 | | seal_error, |
614 | | seal_no_progress, |
615 | | seal_partial, |
616 | | seal_success, |
617 | | }; |
618 | | |
619 | | // seal_next_message seals |msg|, which must be the next message, to |out|. If |
620 | | // progress was made, it returns |seal_partial| or |seal_success| and sets |
621 | | // |*out_len| to the number of bytes written. |
622 | | static enum seal_result_t seal_next_message(SSL *ssl, uint8_t *out, |
623 | | size_t *out_len, size_t max_out, |
624 | 0 | const DTLS_OUTGOING_MESSAGE *msg) { |
625 | 0 | assert(ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len); |
626 | 0 | assert(msg == &ssl->d1->outgoing_messages[ssl->d1->outgoing_written]); |
627 | | |
628 | 0 | enum dtls1_use_epoch_t use_epoch = dtls1_use_current_epoch; |
629 | 0 | if (ssl->d1->w_epoch >= 1 && msg->epoch == ssl->d1->w_epoch - 1) { |
630 | 0 | use_epoch = dtls1_use_previous_epoch; |
631 | 0 | } else if (msg->epoch != ssl->d1->w_epoch) { |
632 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
633 | 0 | return seal_error; |
634 | 0 | } |
635 | | |
636 | 0 | size_t overhead = dtls_max_seal_overhead(ssl, use_epoch); |
637 | 0 | size_t prefix = dtls_seal_prefix_len(ssl, use_epoch); |
638 | |
|
639 | 0 | if (msg->is_ccs) { |
640 | | // Check there is room for the ChangeCipherSpec. |
641 | 0 | static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS}; |
642 | 0 | if (max_out < sizeof(kChangeCipherSpec) + overhead) { |
643 | 0 | return seal_no_progress; |
644 | 0 | } |
645 | | |
646 | 0 | if (!dtls_seal_record(ssl, out, out_len, max_out, |
647 | 0 | SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec, |
648 | 0 | sizeof(kChangeCipherSpec), use_epoch)) { |
649 | 0 | return seal_error; |
650 | 0 | } |
651 | | |
652 | 0 | ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_CHANGE_CIPHER_SPEC, |
653 | 0 | kChangeCipherSpec); |
654 | 0 | return seal_success; |
655 | 0 | } |
656 | | |
657 | | // DTLS messages are serialized as a single fragment in |msg|. |
658 | 0 | CBS cbs, body; |
659 | 0 | struct hm_header_st hdr; |
660 | 0 | CBS_init(&cbs, msg->data.data(), msg->data.size()); |
661 | 0 | if (!dtls1_parse_fragment(&cbs, &hdr, &body) || |
662 | 0 | hdr.frag_off != 0 || |
663 | 0 | hdr.frag_len != CBS_len(&body) || |
664 | 0 | hdr.msg_len != CBS_len(&body) || |
665 | 0 | !CBS_skip(&body, ssl->d1->outgoing_offset) || |
666 | 0 | CBS_len(&cbs) != 0) { |
667 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
668 | 0 | return seal_error; |
669 | 0 | } |
670 | | |
671 | | // Determine how much progress can be made. |
672 | 0 | if (max_out < DTLS1_HM_HEADER_LENGTH + 1 + overhead || max_out < prefix) { |
673 | 0 | return seal_no_progress; |
674 | 0 | } |
675 | 0 | size_t todo = CBS_len(&body); |
676 | 0 | if (todo > max_out - DTLS1_HM_HEADER_LENGTH - overhead) { |
677 | 0 | todo = max_out - DTLS1_HM_HEADER_LENGTH - overhead; |
678 | 0 | } |
679 | | |
680 | | // Assemble a fragment, to be sealed in-place. |
681 | 0 | ScopedCBB cbb; |
682 | 0 | CBB child; |
683 | 0 | uint8_t *frag = out + prefix; |
684 | 0 | size_t max_frag = max_out - prefix, frag_len; |
685 | 0 | if (!CBB_init_fixed(cbb.get(), frag, max_frag) || |
686 | 0 | !CBB_add_u8(cbb.get(), hdr.type) || |
687 | 0 | !CBB_add_u24(cbb.get(), hdr.msg_len) || |
688 | 0 | !CBB_add_u16(cbb.get(), hdr.seq) || |
689 | 0 | !CBB_add_u24(cbb.get(), ssl->d1->outgoing_offset) || |
690 | 0 | !CBB_add_u24_length_prefixed(cbb.get(), &child) || |
691 | 0 | !CBB_add_bytes(&child, CBS_data(&body), todo) || |
692 | 0 | !CBB_finish(cbb.get(), NULL, &frag_len)) { |
693 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
694 | 0 | return seal_error; |
695 | 0 | } |
696 | | |
697 | 0 | ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HANDSHAKE, |
698 | 0 | MakeSpan(frag, frag_len)); |
699 | |
|
700 | 0 | if (!dtls_seal_record(ssl, out, out_len, max_out, SSL3_RT_HANDSHAKE, |
701 | 0 | out + prefix, frag_len, use_epoch)) { |
702 | 0 | return seal_error; |
703 | 0 | } |
704 | | |
705 | 0 | if (todo == CBS_len(&body)) { |
706 | | // The next message is complete. |
707 | 0 | ssl->d1->outgoing_offset = 0; |
708 | 0 | return seal_success; |
709 | 0 | } |
710 | | |
711 | 0 | ssl->d1->outgoing_offset += todo; |
712 | 0 | return seal_partial; |
713 | 0 | } |
714 | | |
715 | | // seal_next_packet writes as much of the next flight as possible to |out| and |
716 | | // advances |ssl->d1->outgoing_written| and |ssl->d1->outgoing_offset| as |
717 | | // appropriate. |
718 | | static bool seal_next_packet(SSL *ssl, uint8_t *out, size_t *out_len, |
719 | 0 | size_t max_out) { |
720 | 0 | bool made_progress = false; |
721 | 0 | size_t total = 0; |
722 | 0 | assert(ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len); |
723 | 0 | for (; ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len; |
724 | 0 | ssl->d1->outgoing_written++) { |
725 | 0 | const DTLS_OUTGOING_MESSAGE *msg = |
726 | 0 | &ssl->d1->outgoing_messages[ssl->d1->outgoing_written]; |
727 | 0 | size_t len; |
728 | 0 | enum seal_result_t ret = seal_next_message(ssl, out, &len, max_out, msg); |
729 | 0 | switch (ret) { |
730 | 0 | case seal_error: |
731 | 0 | return false; |
732 | | |
733 | 0 | case seal_no_progress: |
734 | 0 | goto packet_full; |
735 | | |
736 | 0 | case seal_partial: |
737 | 0 | case seal_success: |
738 | 0 | out += len; |
739 | 0 | max_out -= len; |
740 | 0 | total += len; |
741 | 0 | made_progress = true; |
742 | |
|
743 | 0 | if (ret == seal_partial) { |
744 | 0 | goto packet_full; |
745 | 0 | } |
746 | 0 | break; |
747 | 0 | } |
748 | 0 | } |
749 | | |
750 | 0 | packet_full: |
751 | | // The MTU was too small to make any progress. |
752 | 0 | if (!made_progress) { |
753 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_MTU_TOO_SMALL); |
754 | 0 | return false; |
755 | 0 | } |
756 | | |
757 | 0 | *out_len = total; |
758 | 0 | return true; |
759 | 0 | } |
760 | | |
761 | 0 | static int send_flight(SSL *ssl) { |
762 | 0 | if (ssl->s3->write_shutdown != ssl_shutdown_none) { |
763 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
764 | 0 | return -1; |
765 | 0 | } |
766 | | |
767 | 0 | if (ssl->wbio == nullptr) { |
768 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET); |
769 | 0 | return -1; |
770 | 0 | } |
771 | | |
772 | 0 | dtls1_update_mtu(ssl); |
773 | |
|
774 | 0 | Array<uint8_t> packet; |
775 | 0 | if (!packet.Init(ssl->d1->mtu)) { |
776 | 0 | return -1; |
777 | 0 | } |
778 | | |
779 | 0 | while (ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len) { |
780 | 0 | uint8_t old_written = ssl->d1->outgoing_written; |
781 | 0 | uint32_t old_offset = ssl->d1->outgoing_offset; |
782 | |
|
783 | 0 | size_t packet_len; |
784 | 0 | if (!seal_next_packet(ssl, packet.data(), &packet_len, packet.size())) { |
785 | 0 | return -1; |
786 | 0 | } |
787 | | |
788 | 0 | int bio_ret = BIO_write(ssl->wbio.get(), packet.data(), packet_len); |
789 | 0 | if (bio_ret <= 0) { |
790 | | // Retry this packet the next time around. |
791 | 0 | ssl->d1->outgoing_written = old_written; |
792 | 0 | ssl->d1->outgoing_offset = old_offset; |
793 | 0 | ssl->s3->rwstate = SSL_ERROR_WANT_WRITE; |
794 | 0 | return bio_ret; |
795 | 0 | } |
796 | 0 | } |
797 | | |
798 | 0 | if (BIO_flush(ssl->wbio.get()) <= 0) { |
799 | 0 | ssl->s3->rwstate = SSL_ERROR_WANT_WRITE; |
800 | 0 | return -1; |
801 | 0 | } |
802 | | |
803 | 0 | return 1; |
804 | 0 | } |
805 | | |
806 | 0 | int dtls1_flush_flight(SSL *ssl) { |
807 | 0 | ssl->d1->outgoing_messages_complete = true; |
808 | | // Start the retransmission timer for the next flight (if any). |
809 | 0 | dtls1_start_timer(ssl); |
810 | 0 | return send_flight(ssl); |
811 | 0 | } |
812 | | |
813 | 0 | int dtls1_retransmit_outgoing_messages(SSL *ssl) { |
814 | | // Rewind to the start of the flight and write it again. |
815 | | // |
816 | | // TODO(davidben): This does not allow retransmits to be resumed on |
817 | | // non-blocking write. |
818 | 0 | ssl->d1->outgoing_written = 0; |
819 | 0 | ssl->d1->outgoing_offset = 0; |
820 | |
|
821 | 0 | return send_flight(ssl); |
822 | 0 | } |
823 | | |
824 | 0 | unsigned int dtls1_min_mtu(void) { |
825 | 0 | return kMinMTU; |
826 | 0 | } |
827 | | |
828 | | BSSL_NAMESPACE_END |