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