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