]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
cf72c757 | 2 | * Copyright 2005-2017 The OpenSSL Project Authors. All Rights Reserved. |
0f113f3e | 3 | * |
846e33c7 RS |
4 | * Licensed under the OpenSSL license (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 | |
36d16f8e BL |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
11 | #include <errno.h> | |
999005e4 | 12 | #include "../ssl_locl.h" |
36d16f8e BL |
13 | #include <openssl/evp.h> |
14 | #include <openssl/buffer.h> | |
1711f8de | 15 | #include "record_locl.h" |
bd990e25 | 16 | #include "../packet_locl.h" |
67dc995e | 17 | #include "internal/cryptlib.h" |
40f37188 MC |
18 | |
19 | int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl) | |
20 | { | |
21 | DTLS_RECORD_LAYER *d; | |
0485d540 | 22 | |
b4faea50 | 23 | if ((d = OPENSSL_malloc(sizeof(*d))) == NULL) |
26a7d938 | 24 | return 0; |
40f37188 MC |
25 | |
26 | rl->d = d; | |
5fb6f80c | 27 | |
cb2ce7ab MC |
28 | d->unprocessed_rcds.q = pqueue_new(); |
29 | d->processed_rcds.q = pqueue_new(); | |
24a1e2f2 | 30 | d->buffered_app_data.q = pqueue_new(); |
cb2ce7ab | 31 | |
a71edf3b MC |
32 | if (d->unprocessed_rcds.q == NULL || d->processed_rcds.q == NULL |
33 | || d->buffered_app_data.q == NULL) { | |
25aaa98a RS |
34 | pqueue_free(d->unprocessed_rcds.q); |
35 | pqueue_free(d->processed_rcds.q); | |
36 | pqueue_free(d->buffered_app_data.q); | |
cb2ce7ab MC |
37 | OPENSSL_free(d); |
38 | rl->d = NULL; | |
26a7d938 | 39 | return 0; |
cb2ce7ab | 40 | } |
40f37188 MC |
41 | |
42 | return 1; | |
43 | } | |
44 | ||
45 | void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl) | |
46 | { | |
cb2ce7ab MC |
47 | DTLS_RECORD_LAYER_clear(rl); |
48 | pqueue_free(rl->d->unprocessed_rcds.q); | |
49 | pqueue_free(rl->d->processed_rcds.q); | |
24a1e2f2 | 50 | pqueue_free(rl->d->buffered_app_data.q); |
40f37188 MC |
51 | OPENSSL_free(rl->d); |
52 | rl->d = NULL; | |
53 | } | |
54 | ||
55 | void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl) | |
56 | { | |
57 | DTLS_RECORD_LAYER *d; | |
cb2ce7ab MC |
58 | pitem *item = NULL; |
59 | DTLS1_RECORD_DATA *rdata; | |
cf2cede4 RS |
60 | pqueue *unprocessed_rcds; |
61 | pqueue *processed_rcds; | |
62 | pqueue *buffered_app_data; | |
cb2ce7ab | 63 | |
40f37188 | 64 | d = rl->d; |
0485d540 | 65 | |
cb2ce7ab MC |
66 | while ((item = pqueue_pop(d->unprocessed_rcds.q)) != NULL) { |
67 | rdata = (DTLS1_RECORD_DATA *)item->data; | |
b548a1f1 | 68 | OPENSSL_free(rdata->rbuf.buf); |
cb2ce7ab MC |
69 | OPENSSL_free(item->data); |
70 | pitem_free(item); | |
71 | } | |
72 | ||
73 | while ((item = pqueue_pop(d->processed_rcds.q)) != NULL) { | |
74 | rdata = (DTLS1_RECORD_DATA *)item->data; | |
b548a1f1 | 75 | OPENSSL_free(rdata->rbuf.buf); |
cb2ce7ab MC |
76 | OPENSSL_free(item->data); |
77 | pitem_free(item); | |
78 | } | |
79 | ||
24a1e2f2 MC |
80 | while ((item = pqueue_pop(d->buffered_app_data.q)) != NULL) { |
81 | rdata = (DTLS1_RECORD_DATA *)item->data; | |
b548a1f1 | 82 | OPENSSL_free(rdata->rbuf.buf); |
24a1e2f2 MC |
83 | OPENSSL_free(item->data); |
84 | pitem_free(item); | |
85 | } | |
86 | ||
cb2ce7ab MC |
87 | unprocessed_rcds = d->unprocessed_rcds.q; |
88 | processed_rcds = d->processed_rcds.q; | |
24a1e2f2 | 89 | buffered_app_data = d->buffered_app_data.q; |
b4faea50 | 90 | memset(d, 0, sizeof(*d)); |
cb2ce7ab MC |
91 | d->unprocessed_rcds.q = unprocessed_rcds; |
92 | d->processed_rcds.q = processed_rcds; | |
24a1e2f2 | 93 | d->buffered_app_data.q = buffered_app_data; |
40f37188 MC |
94 | } |
95 | ||
3bb8f87d MC |
96 | void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e) |
97 | { | |
98 | if (e == rl->d->w_epoch - 1) { | |
99 | memcpy(rl->d->curr_write_sequence, | |
a230b26e | 100 | rl->write_sequence, sizeof(rl->write_sequence)); |
3bb8f87d | 101 | memcpy(rl->write_sequence, |
a230b26e | 102 | rl->d->last_write_sequence, sizeof(rl->write_sequence)); |
3bb8f87d MC |
103 | } else if (e == rl->d->w_epoch + 1) { |
104 | memcpy(rl->d->last_write_sequence, | |
a230b26e | 105 | rl->write_sequence, sizeof(unsigned char[8])); |
3bb8f87d | 106 | memcpy(rl->write_sequence, |
a230b26e | 107 | rl->d->curr_write_sequence, sizeof(rl->write_sequence)); |
3bb8f87d MC |
108 | } |
109 | rl->d->w_epoch = e; | |
110 | } | |
111 | ||
e3d0dae7 MC |
112 | void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, unsigned char *seq) |
113 | { | |
114 | memcpy(rl->write_sequence, seq, SEQ_NUM_SIZE); | |
115 | } | |
116 | ||
36d16f8e | 117 | /* copy buffered record into SSL structure */ |
0f113f3e MC |
118 | static int dtls1_copy_record(SSL *s, pitem *item) |
119 | { | |
36d16f8e BL |
120 | DTLS1_RECORD_DATA *rdata; |
121 | ||
122 | rdata = (DTLS1_RECORD_DATA *)item->data; | |
0f113f3e | 123 | |
88c23039 | 124 | SSL3_BUFFER_release(&s->rlayer.rbuf); |
0f113f3e | 125 | |
7a7048af MC |
126 | s->rlayer.packet = rdata->packet; |
127 | s->rlayer.packet_length = rdata->packet_length; | |
88c23039 MC |
128 | memcpy(&s->rlayer.rbuf, &(rdata->rbuf), sizeof(SSL3_BUFFER)); |
129 | memcpy(&s->rlayer.rrec, &(rdata->rrec), sizeof(SSL3_RECORD)); | |
36d16f8e | 130 | |
0f113f3e | 131 | /* Set proper sequence number for mac calculation */ |
de07f311 | 132 | memcpy(&(s->rlayer.read_sequence[2]), &(rdata->packet[5]), 6); |
0f113f3e | 133 | |
208fb891 | 134 | return 1; |
0f113f3e | 135 | } |
36d16f8e | 136 | |
6f7ae319 | 137 | int dtls1_buffer_record(SSL *s, record_pqueue *queue, unsigned char *priority) |
0f113f3e MC |
138 | { |
139 | DTLS1_RECORD_DATA *rdata; | |
140 | pitem *item; | |
141 | ||
142 | /* Limit the size of the queue to prevent DOS attacks */ | |
143 | if (pqueue_size(queue->q) >= 100) | |
144 | return 0; | |
145 | ||
b4faea50 | 146 | rdata = OPENSSL_malloc(sizeof(*rdata)); |
0f113f3e MC |
147 | item = pitem_new(priority, rdata); |
148 | if (rdata == NULL || item == NULL) { | |
b548a1f1 | 149 | OPENSSL_free(rdata); |
25aaa98a | 150 | pitem_free(item); |
0f113f3e | 151 | SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR); |
06c6a2b4 | 152 | return -1; |
0f113f3e MC |
153 | } |
154 | ||
7a7048af MC |
155 | rdata->packet = s->rlayer.packet; |
156 | rdata->packet_length = s->rlayer.packet_length; | |
88c23039 MC |
157 | memcpy(&(rdata->rbuf), &s->rlayer.rbuf, sizeof(SSL3_BUFFER)); |
158 | memcpy(&(rdata->rrec), &s->rlayer.rrec, sizeof(SSL3_RECORD)); | |
0f113f3e MC |
159 | |
160 | item->data = rdata; | |
36d16f8e | 161 | |
7e159e01 | 162 | #ifndef OPENSSL_NO_SCTP |
0f113f3e MC |
163 | /* Store bio_dgram_sctp_rcvinfo struct */ |
164 | if (BIO_dgram_is_sctp(SSL_get_rbio(s)) && | |
5998e290 MC |
165 | (SSL_get_state(s) == TLS_ST_SR_FINISHED |
166 | || SSL_get_state(s) == TLS_ST_CR_FINISHED)) { | |
0f113f3e MC |
167 | BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_GET_RCVINFO, |
168 | sizeof(rdata->recordinfo), &rdata->recordinfo); | |
169 | } | |
7e159e01 DSH |
170 | #endif |
171 | ||
7a7048af MC |
172 | s->rlayer.packet = NULL; |
173 | s->rlayer.packet_length = 0; | |
16f8d4eb RS |
174 | memset(&s->rlayer.rbuf, 0, sizeof(s->rlayer.rbuf)); |
175 | memset(&s->rlayer.rrec, 0, sizeof(s->rlayer.rrec)); | |
0f113f3e MC |
176 | |
177 | if (!ssl3_setup_buffers(s)) { | |
178 | SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR); | |
b548a1f1 | 179 | OPENSSL_free(rdata->rbuf.buf); |
0f113f3e MC |
180 | OPENSSL_free(rdata); |
181 | pitem_free(item); | |
26a7d938 | 182 | return -1; |
0f113f3e | 183 | } |
36d16f8e | 184 | |
0f113f3e MC |
185 | /* insert should not fail, since duplicates are dropped */ |
186 | if (pqueue_insert(queue->q, item) == NULL) { | |
187 | SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR); | |
b548a1f1 | 188 | OPENSSL_free(rdata->rbuf.buf); |
0f113f3e MC |
189 | OPENSSL_free(rdata); |
190 | pitem_free(item); | |
26a7d938 | 191 | return -1; |
0f113f3e | 192 | } |
36d16f8e | 193 | |
208fb891 | 194 | return 1; |
0f113f3e MC |
195 | } |
196 | ||
fe589e61 | 197 | int dtls1_retrieve_buffered_record(SSL *s, record_pqueue *queue) |
0f113f3e | 198 | { |
36d16f8e BL |
199 | pitem *item; |
200 | ||
201 | item = pqueue_pop(queue->q); | |
0f113f3e | 202 | if (item) { |
36d16f8e BL |
203 | dtls1_copy_record(s, item); |
204 | ||
205 | OPENSSL_free(item->data); | |
0f113f3e | 206 | pitem_free(item); |
36d16f8e | 207 | |
208fb891 | 208 | return 1; |
36d16f8e BL |
209 | } |
210 | ||
26a7d938 | 211 | return 0; |
0f113f3e | 212 | } |
36d16f8e | 213 | |
0f113f3e MC |
214 | /* |
215 | * retrieve a buffered record that belongs to the new epoch, i.e., not | |
216 | * processed yet | |
217 | */ | |
36d16f8e BL |
218 | #define dtls1_get_unprocessed_record(s) \ |
219 | dtls1_retrieve_buffered_record((s), \ | |
cb2ce7ab | 220 | &((s)->rlayer.d->unprocessed_rcds)) |
36d16f8e | 221 | |
fe589e61 | 222 | int dtls1_process_buffered_records(SSL *s) |
0f113f3e | 223 | { |
36d16f8e | 224 | pitem *item; |
738ad946 | 225 | SSL3_BUFFER *rb; |
1fb9fdc3 MC |
226 | SSL3_RECORD *rr; |
227 | DTLS1_BITMAP *bitmap; | |
228 | unsigned int is_next_epoch; | |
229 | int replayok = 1; | |
0f113f3e | 230 | |
cb2ce7ab | 231 | item = pqueue_peek(s->rlayer.d->unprocessed_rcds.q); |
0f113f3e | 232 | if (item) { |
36d16f8e | 233 | /* Check if epoch is current. */ |
cb2ce7ab | 234 | if (s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch) |
1fb9fdc3 MC |
235 | return 1; /* Nothing to do. */ |
236 | ||
237 | rr = RECORD_LAYER_get_rrec(&s->rlayer); | |
0f113f3e | 238 | |
738ad946 MC |
239 | rb = RECORD_LAYER_get_rbuf(&s->rlayer); |
240 | ||
241 | if (SSL3_BUFFER_get_left(rb) > 0) { | |
242 | /* | |
243 | * We've still got data from the current packet to read. There could | |
244 | * be a record from the new epoch in it - so don't overwrite it | |
245 | * with the unprocessed records yet (we'll do it when we've | |
246 | * finished reading the current packet). | |
247 | */ | |
248 | return 1; | |
249 | } | |
250 | ||
36d16f8e | 251 | /* Process all the records. */ |
cb2ce7ab | 252 | while (pqueue_peek(s->rlayer.d->unprocessed_rcds.q)) { |
36d16f8e | 253 | dtls1_get_unprocessed_record(s); |
1fb9fdc3 MC |
254 | bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch); |
255 | if (bitmap == NULL) { | |
256 | /* | |
257 | * Should not happen. This will only ever be NULL when the | |
258 | * current record is from a different epoch. But that cannot | |
259 | * be the case because we already checked the epoch above | |
260 | */ | |
261 | SSLerr(SSL_F_DTLS1_PROCESS_BUFFERED_RECORDS, | |
262 | ERR_R_INTERNAL_ERROR); | |
263 | return 0; | |
264 | } | |
265 | #ifndef OPENSSL_NO_SCTP | |
266 | /* Only do replay check if no SCTP bio */ | |
267 | if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) | |
268 | #endif | |
269 | { | |
270 | /* | |
271 | * Check whether this is a repeat, or aged record. We did this | |
272 | * check once already when we first received the record - but | |
273 | * we might have updated the window since then due to | |
274 | * records we subsequently processed. | |
275 | */ | |
276 | replayok = dtls1_record_replay_check(s, bitmap); | |
277 | } | |
278 | ||
279 | if (!replayok || !dtls1_process_record(s, bitmap)) { | |
280 | /* dump this record */ | |
281 | rr->length = 0; | |
282 | RECORD_LAYER_reset_packet_length(&s->rlayer); | |
283 | continue; | |
284 | } | |
285 | ||
cb2ce7ab | 286 | if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds), |
1fb9fdc3 MC |
287 | SSL3_RECORD_get_seq_num(s->rlayer.rrec)) < 0) |
288 | return 0; | |
36d16f8e | 289 | } |
0f113f3e | 290 | } |
36d16f8e | 291 | |
0f113f3e MC |
292 | /* |
293 | * sync epoch numbers once all the unprocessed records have been | |
294 | * processed | |
295 | */ | |
cb2ce7ab MC |
296 | s->rlayer.d->processed_rcds.epoch = s->rlayer.d->r_epoch; |
297 | s->rlayer.d->unprocessed_rcds.epoch = s->rlayer.d->r_epoch + 1; | |
36d16f8e | 298 | |
1fb9fdc3 | 299 | return 1; |
0f113f3e | 300 | } |
36d16f8e | 301 | |
1d97c843 TH |
302 | /*- |
303 | * Return up to 'len' payload bytes received in 'type' records. | |
36d16f8e BL |
304 | * 'type' is one of the following: |
305 | * | |
306 | * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us) | |
307 | * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us) | |
308 | * - 0 (during a shutdown, no data has to be returned) | |
309 | * | |
310 | * If we don't have stored data to work from, read a SSL/TLS record first | |
311 | * (possibly multiple records if we still don't have anything to return). | |
312 | * | |
313 | * This function must handle any surprises the peer may have for us, such as | |
c69f2adf MC |
314 | * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec |
315 | * messages are treated as if they were handshake messages *if* the |recd_type| | |
316 | * argument is non NULL. | |
36d16f8e BL |
317 | * Also if record payloads contain fragments too small to process, we store |
318 | * them until there is enough for the respective protocol (the record protocol | |
319 | * may use arbitrary fragmentation and even interleaving): | |
320 | * Change cipher spec protocol | |
321 | * just 1 byte needed, no need for keeping anything stored | |
322 | * Alert protocol | |
323 | * 2 bytes needed (AlertLevel, AlertDescription) | |
324 | * Handshake protocol | |
325 | * 4 bytes needed (HandshakeType, uint24 length) -- we just have | |
326 | * to detect unexpected Client Hello and Hello Request messages | |
327 | * here, anything else is handled by higher layers | |
328 | * Application data protocol | |
329 | * none of our business | |
330 | */ | |
657da85e | 331 | int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf, |
02ba18a6 | 332 | size_t len, int peek, size_t *readbytes) |
0f113f3e | 333 | { |
eda75751 | 334 | int al, i, j, iret; |
bd990e25 | 335 | size_t n; |
0f113f3e MC |
336 | SSL3_RECORD *rr; |
337 | void (*cb) (const SSL *ssl, int type2, int val) = NULL; | |
338 | ||
88c23039 | 339 | if (!SSL3_BUFFER_is_initialised(&s->rlayer.rbuf)) { |
28d59af8 | 340 | /* Not initialized yet */ |
0f113f3e | 341 | if (!ssl3_setup_buffers(s)) |
26a7d938 | 342 | return -1; |
28d59af8 | 343 | } |
0f113f3e MC |
344 | |
345 | if ((type && (type != SSL3_RT_APPLICATION_DATA) && | |
346 | (type != SSL3_RT_HANDSHAKE)) || | |
347 | (peek && (type != SSL3_RT_APPLICATION_DATA))) { | |
348 | SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR); | |
349 | return -1; | |
350 | } | |
351 | ||
024f543c | 352 | if (!ossl_statem_get_in_handshake(s) && SSL_in_init(s)) |
0f113f3e MC |
353 | { |
354 | /* type == SSL3_RT_APPLICATION_DATA */ | |
355 | i = s->handshake_func(s); | |
356 | if (i < 0) | |
eda75751 | 357 | return i; |
0f113f3e MC |
358 | if (i == 0) { |
359 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE); | |
eda75751 | 360 | return -1; |
0f113f3e MC |
361 | } |
362 | } | |
363 | ||
364 | start: | |
365 | s->rwstate = SSL_NOTHING; | |
366 | ||
50e735f9 MC |
367 | /*- |
368 | * s->s3->rrec.type - is the type of record | |
369 | * s->s3->rrec.data, - data | |
370 | * s->s3->rrec.off, - offset into 'data' for next read | |
371 | * s->s3->rrec.length, - number of bytes. | |
372 | */ | |
94777c9c | 373 | rr = s->rlayer.rrec; |
0f113f3e MC |
374 | |
375 | /* | |
376 | * We are not handshaking and have no data yet, so process data buffered | |
377 | * during the last handshake in advance, if any. | |
378 | */ | |
49ae7423 | 379 | if (SSL_is_init_finished(s) && SSL3_RECORD_get_length(rr) == 0) { |
0f113f3e | 380 | pitem *item; |
24a1e2f2 | 381 | item = pqueue_pop(s->rlayer.d->buffered_app_data.q); |
0f113f3e | 382 | if (item) { |
7e159e01 | 383 | #ifndef OPENSSL_NO_SCTP |
0f113f3e MC |
384 | /* Restore bio_dgram_sctp_rcvinfo struct */ |
385 | if (BIO_dgram_is_sctp(SSL_get_rbio(s))) { | |
386 | DTLS1_RECORD_DATA *rdata = (DTLS1_RECORD_DATA *)item->data; | |
387 | BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_SET_RCVINFO, | |
388 | sizeof(rdata->recordinfo), &rdata->recordinfo); | |
389 | } | |
7e159e01 DSH |
390 | #endif |
391 | ||
0f113f3e MC |
392 | dtls1_copy_record(s, item); |
393 | ||
394 | OPENSSL_free(item->data); | |
395 | pitem_free(item); | |
396 | } | |
397 | } | |
398 | ||
399 | /* Check for timeout */ | |
d273b60b | 400 | if (dtls1_handle_timeout(s) > 0) { |
0f113f3e | 401 | goto start; |
d273b60b MC |
402 | } else if (ossl_statem_in_error(s)) { |
403 | /* dtls1_handle_timeout() has failed with a fatal error */ | |
404 | return -1; | |
405 | } | |
0f113f3e MC |
406 | |
407 | /* get new packet if necessary */ | |
747e1639 | 408 | if ((SSL3_RECORD_get_length(rr) == 0) |
a230b26e | 409 | || (s->rlayer.rstate == SSL_ST_READ_BODY)) { |
eda75751 MC |
410 | iret = dtls1_get_record(s); |
411 | if (iret <= 0) { | |
412 | iret = dtls1_read_failed(s, iret); | |
0f113f3e | 413 | /* anything other than a timeout is an error */ |
eda75751 MC |
414 | if (iret <= 0) |
415 | return iret; | |
0f113f3e MC |
416 | else |
417 | goto start; | |
418 | } | |
419 | } | |
420 | ||
af58be76 MC |
421 | /* |
422 | * Reset the count of consecutive warning alerts if we've got a non-empty | |
423 | * record that isn't an alert. | |
424 | */ | |
425 | if (SSL3_RECORD_get_type(rr) != SSL3_RT_ALERT | |
426 | && SSL3_RECORD_get_length(rr) != 0) | |
427 | s->rlayer.alert_count = 0; | |
428 | ||
0f113f3e MC |
429 | /* we now have a packet which can be read and processed */ |
430 | ||
431 | if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec, | |
432 | * reset by ssl3_get_finished */ | |
747e1639 | 433 | && (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) { |
0f113f3e MC |
434 | /* |
435 | * We now have application data between CCS and Finished. Most likely | |
436 | * the packets were reordered on their way, so buffer the application | |
437 | * data for later processing rather than dropping the connection. | |
438 | */ | |
24a1e2f2 | 439 | if (dtls1_buffer_record(s, &(s->rlayer.d->buffered_app_data), |
a230b26e | 440 | SSL3_RECORD_get_seq_num(rr)) < 0) { |
0f113f3e MC |
441 | SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR); |
442 | return -1; | |
443 | } | |
747e1639 | 444 | SSL3_RECORD_set_length(rr, 0); |
0f113f3e MC |
445 | goto start; |
446 | } | |
447 | ||
448 | /* | |
449 | * If the other end has shut down, throw anything we read away (even in | |
450 | * 'peek' mode) | |
451 | */ | |
452 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { | |
747e1639 | 453 | SSL3_RECORD_set_length(rr, 0); |
0f113f3e | 454 | s->rwstate = SSL_NOTHING; |
eda75751 | 455 | return 0; |
0f113f3e MC |
456 | } |
457 | ||
c69f2adf | 458 | if (type == SSL3_RECORD_get_type(rr) |
a230b26e EK |
459 | || (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC |
460 | && type == SSL3_RT_HANDSHAKE && recvd_type != NULL)) { | |
c69f2adf MC |
461 | /* |
462 | * SSL3_RT_APPLICATION_DATA or | |
463 | * SSL3_RT_HANDSHAKE or | |
464 | * SSL3_RT_CHANGE_CIPHER_SPEC | |
465 | */ | |
0f113f3e MC |
466 | /* |
467 | * make sure that we are not getting application data when we are | |
468 | * doing a handshake for the first time | |
469 | */ | |
470 | if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) && | |
471 | (s->enc_read_ctx == NULL)) { | |
472 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
473 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_APP_DATA_IN_HANDSHAKE); | |
474 | goto f_err; | |
475 | } | |
7e159e01 | 476 | |
c69f2adf MC |
477 | if (recvd_type != NULL) |
478 | *recvd_type = SSL3_RECORD_get_type(rr); | |
479 | ||
eda75751 MC |
480 | if (len == 0) |
481 | return 0; | |
0f113f3e | 482 | |
eda75751 | 483 | if (len > SSL3_RECORD_get_length(rr)) |
747e1639 | 484 | n = SSL3_RECORD_get_length(rr); |
0f113f3e | 485 | else |
eda75751 | 486 | n = len; |
0f113f3e | 487 | |
747e1639 | 488 | memcpy(buf, &(SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)]), n); |
0f113f3e | 489 | if (!peek) { |
753be41d | 490 | SSL3_RECORD_sub_length(rr, n); |
747e1639 MC |
491 | SSL3_RECORD_add_off(rr, n); |
492 | if (SSL3_RECORD_get_length(rr) == 0) { | |
295c3f41 | 493 | s->rlayer.rstate = SSL_ST_READ_HEADER; |
747e1639 | 494 | SSL3_RECORD_set_off(rr, 0); |
0f113f3e MC |
495 | } |
496 | } | |
7e159e01 | 497 | #ifndef OPENSSL_NO_SCTP |
0f113f3e MC |
498 | /* |
499 | * We might had to delay a close_notify alert because of reordered | |
500 | * app data. If there was an alert and there is no message to read | |
501 | * anymore, finally set shutdown. | |
502 | */ | |
503 | if (BIO_dgram_is_sctp(SSL_get_rbio(s)) && | |
504 | s->d1->shutdown_received | |
505 | && !BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) { | |
506 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; | |
eda75751 | 507 | return 0; |
0f113f3e MC |
508 | } |
509 | #endif | |
02ba18a6 | 510 | *readbytes = n; |
eda75751 | 511 | return 1; |
0f113f3e MC |
512 | } |
513 | ||
514 | /* | |
515 | * If we get here, then type != rr->type; if we have a handshake message, | |
516 | * then it was unexpected (Hello Request or Client Hello). | |
517 | */ | |
518 | ||
bd990e25 MC |
519 | if (SSL3_RECORD_get_type(rr) == SSL3_RT_ALERT) { |
520 | unsigned int alert_level, alert_descr; | |
521 | unsigned char *alert_bytes = SSL3_RECORD_get_data(rr) | |
522 | + SSL3_RECORD_get_off(rr); | |
523 | PACKET alert; | |
0f113f3e | 524 | |
bd990e25 MC |
525 | if (!PACKET_buf_init(&alert, alert_bytes, SSL3_RECORD_get_length(rr)) |
526 | || !PACKET_get_1(&alert, &alert_level) | |
527 | || !PACKET_get_1(&alert, &alert_descr) | |
528 | || PACKET_remaining(&alert) != 0) { | |
0f113f3e | 529 | al = SSL_AD_UNEXPECTED_MESSAGE; |
bd990e25 | 530 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_INVALID_ALERT); |
0f113f3e MC |
531 | goto f_err; |
532 | } | |
533 | ||
0f113f3e | 534 | if (s->msg_callback) |
bd990e25 | 535 | s->msg_callback(0, s->version, SSL3_RT_ALERT, alert_bytes, 2, s, |
c661ac16 | 536 | s->msg_callback_arg); |
0f113f3e MC |
537 | |
538 | if (s->info_callback != NULL) | |
539 | cb = s->info_callback; | |
540 | else if (s->ctx->info_callback != NULL) | |
541 | cb = s->ctx->info_callback; | |
542 | ||
543 | if (cb != NULL) { | |
544 | j = (alert_level << 8) | alert_descr; | |
545 | cb(s, SSL_CB_READ_ALERT, j); | |
546 | } | |
547 | ||
fd865cad | 548 | if (alert_level == SSL3_AL_WARNING) { |
0f113f3e | 549 | s->s3->warn_alert = alert_descr; |
af58be76 MC |
550 | |
551 | s->rlayer.alert_count++; | |
552 | if (s->rlayer.alert_count == MAX_WARN_ALERT_COUNT) { | |
553 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
554 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_TOO_MANY_WARN_ALERTS); | |
555 | goto f_err; | |
556 | } | |
557 | ||
0f113f3e | 558 | if (alert_descr == SSL_AD_CLOSE_NOTIFY) { |
7e159e01 | 559 | #ifndef OPENSSL_NO_SCTP |
0f113f3e MC |
560 | /* |
561 | * With SCTP and streams the socket may deliver app data | |
562 | * after a close_notify alert. We have to check this first so | |
563 | * that nothing gets discarded. | |
564 | */ | |
565 | if (BIO_dgram_is_sctp(SSL_get_rbio(s)) && | |
566 | BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) { | |
567 | s->d1->shutdown_received = 1; | |
568 | s->rwstate = SSL_READING; | |
569 | BIO_clear_retry_flags(SSL_get_rbio(s)); | |
570 | BIO_set_retry_read(SSL_get_rbio(s)); | |
571 | return -1; | |
572 | } | |
7e159e01 | 573 | #endif |
0f113f3e | 574 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; |
eda75751 | 575 | return 0; |
0f113f3e | 576 | } |
fd865cad | 577 | } else if (alert_level == SSL3_AL_FATAL) { |
0f113f3e MC |
578 | char tmp[16]; |
579 | ||
580 | s->rwstate = SSL_NOTHING; | |
581 | s->s3->fatal_alert = alert_descr; | |
a230b26e | 582 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr); |
cbe29648 | 583 | BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr); |
0f113f3e MC |
584 | ERR_add_error_data(2, "SSL alert number ", tmp); |
585 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; | |
e2bb9b9b | 586 | SSL_CTX_remove_session(s->session_ctx, s->session); |
eda75751 | 587 | return 0; |
0f113f3e MC |
588 | } else { |
589 | al = SSL_AD_ILLEGAL_PARAMETER; | |
590 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE); | |
591 | goto f_err; | |
592 | } | |
593 | ||
594 | goto start; | |
595 | } | |
596 | ||
597 | if (s->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a | |
598 | * shutdown */ | |
599 | s->rwstate = SSL_NOTHING; | |
747e1639 | 600 | SSL3_RECORD_set_length(rr, 0); |
eda75751 | 601 | return 0; |
0f113f3e MC |
602 | } |
603 | ||
747e1639 | 604 | if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) { |
0f113f3e MC |
605 | /* |
606 | * We can't process a CCS now, because previous handshake messages | |
607 | * are still missing, so just drop it. | |
608 | */ | |
c69f2adf | 609 | SSL3_RECORD_set_length(rr, 0); |
0f113f3e MC |
610 | goto start; |
611 | } | |
612 | ||
613 | /* | |
614 | * Unexpected handshake message (Client Hello, or protocol violation) | |
615 | */ | |
bd990e25 MC |
616 | if ((SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) && |
617 | !ossl_statem_get_in_handshake(s)) { | |
0f113f3e MC |
618 | struct hm_header_st msg_hdr; |
619 | ||
bd990e25 MC |
620 | /* |
621 | * This may just be a stale retransmit. Also sanity check that we have | |
622 | * at least enough record bytes for a message header | |
623 | */ | |
624 | if (SSL3_RECORD_get_epoch(rr) != s->rlayer.d->r_epoch | |
625 | || SSL3_RECORD_get_length(rr) < DTLS1_HM_HEADER_LENGTH) { | |
747e1639 | 626 | SSL3_RECORD_set_length(rr, 0); |
0f113f3e MC |
627 | goto start; |
628 | } | |
629 | ||
bd990e25 MC |
630 | dtls1_get_message_header(rr->data, &msg_hdr); |
631 | ||
0f113f3e MC |
632 | /* |
633 | * If we are server, we may have a repeated FINISHED of the client | |
634 | * here, then retransmit our CCS and FINISHED. | |
635 | */ | |
636 | if (msg_hdr.type == SSL3_MT_FINISHED) { | |
637 | if (dtls1_check_timeout_num(s) < 0) | |
638 | return -1; | |
639 | ||
d273b60b MC |
640 | if (dtls1_retransmit_buffered_messages(s) <= 0) { |
641 | /* Fail if we encountered a fatal error */ | |
642 | if (ossl_statem_in_error(s)) | |
643 | return -1; | |
d273b60b | 644 | } |
747e1639 | 645 | SSL3_RECORD_set_length(rr, 0); |
0f113f3e MC |
646 | goto start; |
647 | } | |
648 | ||
c7f47786 MC |
649 | /* |
650 | * To get here we must be trying to read app data but found handshake | |
651 | * data. But if we're trying to read app data, and we're not in init | |
652 | * (which is tested for at the top of this function) then init must be | |
653 | * finished | |
654 | */ | |
b77f3ed1 | 655 | if (!ossl_assert(SSL_is_init_finished(s))) { |
c7f47786 MC |
656 | al = SSL_AD_INTERNAL_ERROR; |
657 | SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR); | |
658 | goto f_err; | |
0f113f3e | 659 | } |
c7f47786 MC |
660 | |
661 | /* We found handshake data, so we're going back into init */ | |
662 | ossl_statem_set_in_init(s, 1); | |
663 | ||
0f113f3e MC |
664 | i = s->handshake_func(s); |
665 | if (i < 0) | |
eda75751 | 666 | return i; |
0f113f3e MC |
667 | if (i == 0) { |
668 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE); | |
eda75751 | 669 | return -1; |
0f113f3e MC |
670 | } |
671 | ||
672 | if (!(s->mode & SSL_MODE_AUTO_RETRY)) { | |
88c23039 | 673 | if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) { |
28d59af8 | 674 | /* no read-ahead left? */ |
0f113f3e MC |
675 | BIO *bio; |
676 | /* | |
677 | * In the case where we try to read application data, but we | |
678 | * trigger an SSL handshake, we return -1 with the retry | |
679 | * option set. Otherwise renegotiation may cause nasty | |
680 | * problems in the blocking world | |
681 | */ | |
682 | s->rwstate = SSL_READING; | |
683 | bio = SSL_get_rbio(s); | |
684 | BIO_clear_retry_flags(bio); | |
685 | BIO_set_retry_read(bio); | |
eda75751 | 686 | return -1; |
0f113f3e MC |
687 | } |
688 | } | |
689 | goto start; | |
690 | } | |
691 | ||
747e1639 | 692 | switch (SSL3_RECORD_get_type(rr)) { |
0f113f3e | 693 | default: |
0f113f3e MC |
694 | al = SSL_AD_UNEXPECTED_MESSAGE; |
695 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD); | |
696 | goto f_err; | |
697 | case SSL3_RT_CHANGE_CIPHER_SPEC: | |
698 | case SSL3_RT_ALERT: | |
699 | case SSL3_RT_HANDSHAKE: | |
700 | /* | |
701 | * we already handled all of these, with the possible exception of | |
024f543c MC |
702 | * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but |
703 | * that should not happen when type != rr->type | |
0f113f3e MC |
704 | */ |
705 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
706 | SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR); | |
707 | goto f_err; | |
708 | case SSL3_RT_APPLICATION_DATA: | |
709 | /* | |
710 | * At this point, we were expecting handshake data, but have | |
711 | * application data. If the library was running inside ssl3_read() | |
712 | * (i.e. in_read_app_data is set) and it makes sense to read | |
713 | * application data at this point (session renegotiation not yet | |
714 | * started), we will indulge it. | |
715 | */ | |
716 | if (s->s3->in_read_app_data && | |
717 | (s->s3->total_renegotiations != 0) && | |
fe3a3291 | 718 | ossl_statem_app_data_allowed(s)) { |
0f113f3e | 719 | s->s3->in_read_app_data = 2; |
eda75751 | 720 | return -1; |
0f113f3e MC |
721 | } else { |
722 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
723 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD); | |
724 | goto f_err; | |
725 | } | |
726 | } | |
727 | /* not reached */ | |
728 | ||
729 | f_err: | |
730 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
eda75751 | 731 | return -1; |
0f113f3e MC |
732 | } |
733 | ||
0f113f3e MC |
734 | /* |
735 | * Call this to write data in records of type 'type' It will return <= 0 if | |
736 | * not all data has been sent or non-blocking IO. | |
36d16f8e | 737 | */ |
7ee8627f MC |
738 | int dtls1_write_bytes(SSL *s, int type, const void *buf, size_t len, |
739 | size_t *written) | |
0f113f3e MC |
740 | { |
741 | int i; | |
742 | ||
42bd7a16 MC |
743 | if (!ossl_assert(len <= SSL3_RT_MAX_PLAIN_LENGTH)) { |
744 | SSLerr(SSL_F_DTLS1_WRITE_BYTES, ERR_R_INTERNAL_ERROR); | |
380a522f | 745 | return -1; |
42bd7a16 | 746 | } |
0f113f3e | 747 | s->rwstate = SSL_NOTHING; |
7ee8627f | 748 | i = do_dtls1_write(s, type, buf, len, 0, written); |
0f113f3e MC |
749 | return i; |
750 | } | |
751 | ||
752 | int do_dtls1_write(SSL *s, int type, const unsigned char *buf, | |
7ee8627f | 753 | size_t len, int create_empty_fragment, size_t *written) |
0f113f3e MC |
754 | { |
755 | unsigned char *p, *pseq; | |
756 | int i, mac_size, clear = 0; | |
7ee8627f | 757 | size_t prefix_len = 0; |
0f113f3e | 758 | int eivlen; |
f482740f | 759 | SSL3_RECORD wr; |
0f113f3e MC |
760 | SSL3_BUFFER *wb; |
761 | SSL_SESSION *sess; | |
762 | ||
d102d9df | 763 | wb = &s->rlayer.wbuf[0]; |
db9a32e7 | 764 | |
0f113f3e MC |
765 | /* |
766 | * first check if there is a SSL3_BUFFER still being written out. This | |
767 | * will happen with non blocking IO | |
768 | */ | |
380a522f MC |
769 | if (!ossl_assert(SSL3_BUFFER_get_left(wb) == 0)) { |
770 | SSLerr(SSL_F_DO_DTLS1_WRITE, ERR_R_INTERNAL_ERROR); | |
771 | return 0; | |
0f113f3e MC |
772 | } |
773 | ||
774 | /* If we have an alert to send, lets send it */ | |
775 | if (s->s3->alert_dispatch) { | |
776 | i = s->method->ssl_dispatch_alert(s); | |
777 | if (i <= 0) | |
7ee8627f | 778 | return i; |
0f113f3e MC |
779 | /* if it went, fall through and send more stuff */ |
780 | } | |
781 | ||
782 | if (len == 0 && !create_empty_fragment) | |
783 | return 0; | |
784 | ||
cf72c757 | 785 | if (len > ssl_get_max_send_fragment(s)) { |
aefb9256 MC |
786 | SSLerr(SSL_F_DO_DTLS1_WRITE, SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE); |
787 | return 0; | |
788 | } | |
789 | ||
0f113f3e MC |
790 | sess = s->session; |
791 | ||
792 | if ((sess == NULL) || | |
793 | (s->enc_write_ctx == NULL) || (EVP_MD_CTX_md(s->write_hash) == NULL)) | |
794 | clear = 1; | |
795 | ||
796 | if (clear) | |
797 | mac_size = 0; | |
798 | else { | |
799 | mac_size = EVP_MD_CTX_size(s->write_hash); | |
800 | if (mac_size < 0) | |
801 | goto err; | |
802 | } | |
803 | ||
747e1639 | 804 | p = SSL3_BUFFER_get_buf(wb) + prefix_len; |
0f113f3e MC |
805 | |
806 | /* write the header */ | |
807 | ||
808 | *(p++) = type & 0xff; | |
f482740f | 809 | SSL3_RECORD_set_type(&wr, type); |
0f113f3e MC |
810 | /* |
811 | * Special case: for hello verify request, client version 1.0 and we | |
812 | * haven't decided which version to use yet send back using version 1.0 | |
813 | * header: otherwise some clients will ignore it. | |
814 | */ | |
032924c4 | 815 | if (s->method->version == DTLS_ANY_VERSION && |
a230b26e | 816 | s->max_proto_version != DTLS1_BAD_VER) { |
0f113f3e MC |
817 | *(p++) = DTLS1_VERSION >> 8; |
818 | *(p++) = DTLS1_VERSION & 0xff; | |
819 | } else { | |
820 | *(p++) = s->version >> 8; | |
821 | *(p++) = s->version & 0xff; | |
822 | } | |
823 | ||
824 | /* field where we are to write out packet epoch, seq num and len */ | |
825 | pseq = p; | |
826 | p += 10; | |
827 | ||
828 | /* Explicit IV length, block ciphers appropriate version flag */ | |
829 | if (s->enc_write_ctx) { | |
830 | int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx); | |
831 | if (mode == EVP_CIPH_CBC_MODE) { | |
832 | eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx); | |
833 | if (eivlen <= 1) | |
834 | eivlen = 0; | |
835 | } | |
836 | /* Need explicit part of IV for GCM mode */ | |
837 | else if (mode == EVP_CIPH_GCM_MODE) | |
838 | eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN; | |
e75c5a79 DSH |
839 | else if (mode == EVP_CIPH_CCM_MODE) |
840 | eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN; | |
0f113f3e MC |
841 | else |
842 | eivlen = 0; | |
843 | } else | |
844 | eivlen = 0; | |
845 | ||
846 | /* lets setup the record stuff. */ | |
f482740f | 847 | SSL3_RECORD_set_data(&wr, p + eivlen); /* make room for IV in case of CBC */ |
7ee8627f | 848 | SSL3_RECORD_set_length(&wr, len); |
f482740f | 849 | SSL3_RECORD_set_input(&wr, (unsigned char *)buf); |
0f113f3e MC |
850 | |
851 | /* | |
f482740f | 852 | * we now 'read' from wr.input, wr.length bytes into wr.data |
0f113f3e MC |
853 | */ |
854 | ||
855 | /* first we compress */ | |
856 | if (s->compress != NULL) { | |
f482740f | 857 | if (!ssl3_do_compress(s, &wr)) { |
0f113f3e MC |
858 | SSLerr(SSL_F_DO_DTLS1_WRITE, SSL_R_COMPRESSION_FAILURE); |
859 | goto err; | |
860 | } | |
861 | } else { | |
f482740f MC |
862 | memcpy(SSL3_RECORD_get_data(&wr), SSL3_RECORD_get_input(&wr), |
863 | SSL3_RECORD_get_length(&wr)); | |
864 | SSL3_RECORD_reset_input(&wr); | |
0f113f3e | 865 | } |
36d16f8e | 866 | |
0f113f3e | 867 | /* |
f482740f MC |
868 | * we should still have the output to wr.data and the input from |
869 | * wr.input. Length should be wr.length. wr.data still points in the | |
0f113f3e MC |
870 | * wb->buf |
871 | */ | |
36d16f8e | 872 | |
28a31a0a | 873 | if (!SSL_WRITE_ETM(s) && mac_size != 0) { |
a14aa99b MC |
874 | if (!s->method->ssl3_enc->mac(s, &wr, |
875 | &(p[SSL3_RECORD_get_length(&wr) + eivlen]), | |
876 | 1)) | |
0f113f3e | 877 | goto err; |
f482740f | 878 | SSL3_RECORD_add_length(&wr, mac_size); |
0f113f3e | 879 | } |
36d16f8e | 880 | |
0f113f3e | 881 | /* this is true regardless of mac size */ |
f482740f MC |
882 | SSL3_RECORD_set_data(&wr, p); |
883 | SSL3_RECORD_reset_input(&wr); | |
36d16f8e | 884 | |
0f113f3e | 885 | if (eivlen) |
f482740f | 886 | SSL3_RECORD_add_length(&wr, eivlen); |
36d16f8e | 887 | |
f482740f | 888 | if (s->method->ssl3_enc->enc(s, &wr, 1, 1) < 1) |
0f113f3e | 889 | goto err; |
36d16f8e | 890 | |
28a31a0a | 891 | if (SSL_WRITE_ETM(s) && mac_size != 0) { |
a14aa99b MC |
892 | if (!s->method->ssl3_enc->mac(s, &wr, |
893 | &(p[SSL3_RECORD_get_length(&wr)]), 1)) | |
e23d5071 DW |
894 | goto err; |
895 | SSL3_RECORD_add_length(&wr, mac_size); | |
896 | } | |
897 | ||
0f113f3e | 898 | /* record length after mac and block padding */ |
36d16f8e | 899 | |
0f113f3e | 900 | /* there's only one epoch between handshake and app data */ |
36d16f8e | 901 | |
78a39fe7 | 902 | s2n(s->rlayer.d->w_epoch, pseq); |
36d16f8e | 903 | |
de07f311 | 904 | memcpy(pseq, &(s->rlayer.write_sequence[2]), 6); |
0f113f3e | 905 | pseq += 6; |
f482740f | 906 | s2n(SSL3_RECORD_get_length(&wr), pseq); |
36d16f8e | 907 | |
0f113f3e MC |
908 | if (s->msg_callback) |
909 | s->msg_callback(1, 0, SSL3_RT_HEADER, pseq - DTLS1_RT_HEADER_LENGTH, | |
910 | DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg); | |
36d16f8e | 911 | |
0f113f3e | 912 | /* |
f482740f | 913 | * we should now have wr.data pointing to the encrypted data, which is |
0f113f3e MC |
914 | * wr->length long |
915 | */ | |
f482740f MC |
916 | SSL3_RECORD_set_type(&wr, type); /* not needed but helps for debugging */ |
917 | SSL3_RECORD_add_length(&wr, DTLS1_RT_HEADER_LENGTH); | |
36d16f8e | 918 | |
de07f311 | 919 | ssl3_record_sequence_update(&(s->rlayer.write_sequence[0])); |
36d16f8e | 920 | |
0f113f3e MC |
921 | if (create_empty_fragment) { |
922 | /* | |
923 | * we are in a recursive call; just return the length, don't write | |
924 | * out anything here | |
925 | */ | |
7ee8627f MC |
926 | *written = wr.length; |
927 | return 1; | |
0f113f3e | 928 | } |
36d16f8e | 929 | |
0f113f3e | 930 | /* now let's set up wb */ |
f482740f | 931 | SSL3_BUFFER_set_left(wb, prefix_len + SSL3_RECORD_get_length(&wr)); |
747e1639 | 932 | SSL3_BUFFER_set_offset(wb, 0); |
0f113f3e MC |
933 | |
934 | /* | |
935 | * memorize arguments so that ssl3_write_pending can detect bad write | |
936 | * retries later | |
937 | */ | |
f8caa3c8 MC |
938 | s->rlayer.wpend_tot = len; |
939 | s->rlayer.wpend_buf = buf; | |
940 | s->rlayer.wpend_type = type; | |
941 | s->rlayer.wpend_ret = len; | |
0f113f3e MC |
942 | |
943 | /* we now just need to write the buffer */ | |
7ee8627f | 944 | return ssl3_write_pending(s, type, buf, len, written); |
0f113f3e MC |
945 | err: |
946 | return -1; | |
947 | } | |
36d16f8e | 948 | |
fe589e61 | 949 | DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr, |
a230b26e | 950 | unsigned int *is_next_epoch) |
0f113f3e MC |
951 | { |
952 | ||
36d16f8e BL |
953 | *is_next_epoch = 0; |
954 | ||
955 | /* In current epoch, accept HM, CCS, DATA, & ALERT */ | |
78a39fe7 | 956 | if (rr->epoch == s->rlayer.d->r_epoch) |
91f93f69 | 957 | return &s->rlayer.d->bitmap; |
36d16f8e | 958 | |
738ad946 MC |
959 | /* |
960 | * Only HM and ALERT messages can be from the next epoch and only if we | |
961 | * have already processed all of the unprocessed records from the last | |
962 | * epoch | |
963 | */ | |
78a39fe7 | 964 | else if (rr->epoch == (unsigned long)(s->rlayer.d->r_epoch + 1) && |
738ad946 | 965 | s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch && |
a230b26e | 966 | (rr->type == SSL3_RT_HANDSHAKE || rr->type == SSL3_RT_ALERT)) { |
36d16f8e | 967 | *is_next_epoch = 1; |
91f93f69 | 968 | return &s->rlayer.d->next_bitmap; |
0f113f3e | 969 | } |
36d16f8e BL |
970 | |
971 | return NULL; | |
0f113f3e | 972 | } |
36d16f8e | 973 | |
0f113f3e MC |
974 | void dtls1_reset_seq_numbers(SSL *s, int rw) |
975 | { | |
976 | unsigned char *seq; | |
de07f311 | 977 | unsigned int seq_bytes = sizeof(s->rlayer.read_sequence); |
0f113f3e MC |
978 | |
979 | if (rw & SSL3_CC_READ) { | |
de07f311 | 980 | seq = s->rlayer.read_sequence; |
78a39fe7 | 981 | s->rlayer.d->r_epoch++; |
16f8d4eb RS |
982 | memcpy(&s->rlayer.d->bitmap, &s->rlayer.d->next_bitmap, |
983 | sizeof(s->rlayer.d->bitmap)); | |
a230b26e | 984 | memset(&s->rlayer.d->next_bitmap, 0, sizeof(s->rlayer.d->next_bitmap)); |
5cb4d646 MC |
985 | |
986 | /* | |
987 | * We must not use any buffered messages received from the previous | |
988 | * epoch | |
989 | */ | |
990 | dtls1_clear_received_buffer(s); | |
0f113f3e | 991 | } else { |
de07f311 | 992 | seq = s->rlayer.write_sequence; |
3bb8f87d | 993 | memcpy(s->rlayer.d->last_write_sequence, seq, |
de07f311 | 994 | sizeof(s->rlayer.write_sequence)); |
78a39fe7 | 995 | s->rlayer.d->w_epoch++; |
0f113f3e MC |
996 | } |
997 | ||
16f8d4eb | 998 | memset(seq, 0, seq_bytes); |
0f113f3e | 999 | } |