]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/record/rec_layer_d1.c
Use record layer buffers for DTLS rather than the buffers in s->rlayer
[thirdparty/openssl.git] / ssl / record / rec_layer_d1.c
CommitLineData
0f113f3e 1/*
4333b89f 2 * Copyright 2005-2021 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>
706457b7 12#include "../ssl_local.h"
36d16f8e
BL
13#include <openssl/evp.h>
14#include <openssl/buffer.h>
706457b7 15#include "record_local.h"
0d345f0e 16#include "internal/packet.h"
67dc995e 17#include "internal/cryptlib.h"
40f37188
MC
18
19int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl)
20{
21 DTLS_RECORD_LAYER *d;
0485d540 22
e077455e 23 if ((d = OPENSSL_malloc(sizeof(*d))) == NULL)
26a7d938 24 return 0;
40f37188
MC
25
26 rl->d = d;
5fb6f80c 27
24a1e2f2 28 d->buffered_app_data.q = pqueue_new();
cb2ce7ab 29
eddb067e 30 if (d->buffered_app_data.q == NULL) {
cb2ce7ab
MC
31 OPENSSL_free(d);
32 rl->d = NULL;
26a7d938 33 return 0;
cb2ce7ab 34 }
40f37188
MC
35
36 return 1;
37}
38
39void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl)
40{
d0afb30e
MC
41 if (rl->d == NULL)
42 return;
43
cb2ce7ab 44 DTLS_RECORD_LAYER_clear(rl);
24a1e2f2 45 pqueue_free(rl->d->buffered_app_data.q);
40f37188
MC
46 OPENSSL_free(rl->d);
47 rl->d = NULL;
48}
49
50void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl)
51{
52 DTLS_RECORD_LAYER *d;
cb2ce7ab 53 pitem *item = NULL;
eddb067e 54 TLS_RECORD *rec;
cf2cede4 55 pqueue *buffered_app_data;
cb2ce7ab 56
40f37188 57 d = rl->d;
0485d540 58
24a1e2f2 59 while ((item = pqueue_pop(d->buffered_app_data.q)) != NULL) {
eddb067e 60 rec = (TLS_RECORD *)item->data;
163b8016 61 if (rl->s->options & SSL_OP_CLEANSE_PLAINTEXT)
eddb067e
MC
62 OPENSSL_cleanse(rec->data, rec->length);
63 OPENSSL_free(rec->data);
24a1e2f2
MC
64 OPENSSL_free(item->data);
65 pitem_free(item);
66 }
67
24a1e2f2 68 buffered_app_data = d->buffered_app_data.q;
b4faea50 69 memset(d, 0, sizeof(*d));
24a1e2f2 70 d->buffered_app_data.q = buffered_app_data;
40f37188
MC
71}
72
3bb8f87d
MC
73void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e)
74{
75 if (e == rl->d->w_epoch - 1) {
76 memcpy(rl->d->curr_write_sequence,
a230b26e 77 rl->write_sequence, sizeof(rl->write_sequence));
3bb8f87d 78 memcpy(rl->write_sequence,
a230b26e 79 rl->d->last_write_sequence, sizeof(rl->write_sequence));
3bb8f87d
MC
80 } else if (e == rl->d->w_epoch + 1) {
81 memcpy(rl->d->last_write_sequence,
a230b26e 82 rl->write_sequence, sizeof(unsigned char[8]));
3bb8f87d 83 memcpy(rl->write_sequence,
a230b26e 84 rl->d->curr_write_sequence, sizeof(rl->write_sequence));
3bb8f87d
MC
85 }
86 rl->d->w_epoch = e;
87}
88
e3d0dae7
MC
89void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, unsigned char *seq)
90{
91 memcpy(rl->write_sequence, seq, SEQ_NUM_SIZE);
92}
93
eddb067e 94int dtls_buffer_record(SSL_CONNECTION *s, TLS_RECORD *rec)
0f113f3e 95{
eddb067e 96 TLS_RECORD *rdata;
0f113f3e 97 pitem *item;
eddb067e 98 record_pqueue *queue = &(s->rlayer.d->buffered_app_data);
0f113f3e
MC
99
100 /* Limit the size of the queue to prevent DOS attacks */
101 if (pqueue_size(queue->q) >= 100)
102 return 0;
103
eddb067e
MC
104 /* We don't buffer partially read records */
105 if (!ossl_assert(rec->off == 0))
106 return -1;
107
b4faea50 108 rdata = OPENSSL_malloc(sizeof(*rdata));
eddb067e 109 item = pitem_new(rec->seq_num, rdata);
0f113f3e 110 if (rdata == NULL || item == NULL) {
b548a1f1 111 OPENSSL_free(rdata);
25aaa98a 112 pitem_free(item);
c48ffbcc 113 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
06c6a2b4 114 return -1;
0f113f3e
MC
115 }
116
eddb067e
MC
117 *rdata = *rec;
118 /*
119 * We will release the record from the record layer soon, so we take a copy
120 * now. Copying data isn't good - but this should be infrequent so we
121 * accept it here.
122 */
123 rdata->data = OPENSSL_memdup(rec->data, rec->length);
124 if (rdata->data == NULL) {
125 OPENSSL_free(rdata);
126 pitem_free(item);
e077455e 127 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
eddb067e
MC
128 return -1;
129 }
130 /*
131 * We use a NULL rechandle to indicate that the data field has been
132 * allocated by us.
133 */
134 rdata->rechandle = NULL;
0f113f3e
MC
135
136 item->data = rdata;
36d16f8e 137
7e159e01 138#ifndef OPENSSL_NO_SCTP
0f113f3e 139 /* Store bio_dgram_sctp_rcvinfo struct */
846975f3 140 if (BIO_dgram_is_sctp(s->rbio) &&
141 (ossl_statem_get_state(s) == TLS_ST_SR_FINISHED
142 || ossl_statem_get_state(s) == TLS_ST_CR_FINISHED)) {
143 BIO_ctrl(s->rbio, BIO_CTRL_DGRAM_SCTP_GET_RCVINFO,
0f113f3e
MC
144 sizeof(rdata->recordinfo), &rdata->recordinfo);
145 }
7e159e01
DSH
146#endif
147
0f113f3e 148 if (pqueue_insert(queue->q, item) == NULL) {
840facc3 149 /* Must be a duplicate so ignore it */
eddb067e 150 OPENSSL_free(rdata->data);
0f113f3e
MC
151 OPENSSL_free(rdata);
152 pitem_free(item);
0f113f3e 153 }
36d16f8e 154
208fb891 155 return 1;
0f113f3e
MC
156}
157
eddb067e
MC
158/* Unbuffer a previously buffered TLS_RECORD structure if any */
159static void dtls_unbuffer_record(SSL_CONNECTION *s)
0f113f3e 160{
eddb067e 161 TLS_RECORD *rdata;
36d16f8e
BL
162 pitem *item;
163
eddb067e
MC
164 /* If we already have records to handle then do nothing */
165 if (s->rlayer.curr_rec < s->rlayer.num_recs)
166 return;
1fb9fdc3 167
eddb067e
MC
168 item = pqueue_pop(s->rlayer.d->buffered_app_data.q);
169 if (item != NULL) {
170 rdata = (TLS_RECORD *)item->data;
0f113f3e 171
eddb067e
MC
172 s->rlayer.tlsrecs[0] = *rdata;
173 s->rlayer.num_recs = 1;
174 s->rlayer.curr_rec = 0;
738ad946 175
1fb9fdc3 176#ifndef OPENSSL_NO_SCTP
eddb067e 177 /* Restore bio_dgram_sctp_rcvinfo struct */
846975f3 178 if (BIO_dgram_is_sctp(s->rbio)) {
179 BIO_ctrl(s->rbio, BIO_CTRL_DGRAM_SCTP_SET_RCVINFO,
180 sizeof(rdata->recordinfo), &rdata->recordinfo);
eddb067e 181 }
1fb9fdc3 182#endif
1fb9fdc3 183
eddb067e
MC
184 OPENSSL_free(item->data);
185 pitem_free(item);
0f113f3e 186 }
0f113f3e 187}
36d16f8e 188
1d97c843
TH
189/*-
190 * Return up to 'len' payload bytes received in 'type' records.
36d16f8e
BL
191 * 'type' is one of the following:
192 *
193 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
194 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
195 * - 0 (during a shutdown, no data has to be returned)
196 *
197 * If we don't have stored data to work from, read a SSL/TLS record first
198 * (possibly multiple records if we still don't have anything to return).
199 *
200 * This function must handle any surprises the peer may have for us, such as
c69f2adf
MC
201 * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec
202 * messages are treated as if they were handshake messages *if* the |recd_type|
203 * argument is non NULL.
36d16f8e
BL
204 * Also if record payloads contain fragments too small to process, we store
205 * them until there is enough for the respective protocol (the record protocol
206 * may use arbitrary fragmentation and even interleaving):
207 * Change cipher spec protocol
208 * just 1 byte needed, no need for keeping anything stored
209 * Alert protocol
210 * 2 bytes needed (AlertLevel, AlertDescription)
211 * Handshake protocol
212 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
213 * to detect unexpected Client Hello and Hello Request messages
214 * here, anything else is handled by higher layers
215 * Application data protocol
216 * none of our business
217 */
657da85e 218int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
02ba18a6 219 size_t len, int peek, size_t *readbytes)
0f113f3e 220{
eddb067e 221 int i, j, ret;
bd990e25 222 size_t n;
eddb067e 223 TLS_RECORD *rr;
0f113f3e 224 void (*cb) (const SSL *ssl, int type2, int val) = NULL;
38b051a1
TM
225 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
226
227 if (sc == NULL)
228 return -1;
0f113f3e 229
0f113f3e
MC
230 if ((type && (type != SSL3_RT_APPLICATION_DATA) &&
231 (type != SSL3_RT_HANDSHAKE)) ||
232 (peek && (type != SSL3_RT_APPLICATION_DATA))) {
38b051a1 233 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
234 return -1;
235 }
236
38b051a1 237 if (!ossl_statem_get_in_handshake(sc) && SSL_in_init(s)) {
0f113f3e 238 /* type == SSL3_RT_APPLICATION_DATA */
38b051a1 239 i = sc->handshake_func(s);
c2853382 240 /* SSLfatal() already called if appropriate */
0f113f3e 241 if (i < 0)
eda75751 242 return i;
c2853382 243 if (i == 0)
eda75751 244 return -1;
0f113f3e
MC
245 }
246
247 start:
38b051a1 248 sc->rwstate = SSL_NOTHING;
0f113f3e 249
0f113f3e
MC
250 /*
251 * We are not handshaking and have no data yet, so process data buffered
252 * during the last handshake in advance, if any.
253 */
eddb067e
MC
254 if (SSL_is_init_finished(s))
255 dtls_unbuffer_record(sc);
0f113f3e
MC
256
257 /* Check for timeout */
38b051a1 258 if (dtls1_handle_timeout(sc) > 0) {
0f113f3e 259 goto start;
38b051a1 260 } else if (ossl_statem_in_error(sc)) {
d273b60b
MC
261 /* dtls1_handle_timeout() has failed with a fatal error */
262 return -1;
263 }
0f113f3e
MC
264
265 /* get new packet if necessary */
eddb067e
MC
266 if (sc->rlayer.curr_rec >= sc->rlayer.num_recs) {
267 sc->rlayer.curr_rec = sc->rlayer.num_recs = 0;
268 do {
269 rr = &sc->rlayer.tlsrecs[sc->rlayer.num_recs];
270
320145d5 271 ret = HANDLE_RLAYER_READ_RETURN(sc,
cffafb5f
MC
272 sc->rlayer.rrlmethod->read_record(sc->rlayer.rrl,
273 &rr->rechandle,
274 &rr->version, &rr->type,
275 &rr->data, &rr->length,
276 &rr->epoch, rr->seq_num));
eddb067e
MC
277 if (ret <= 0) {
278 ret = dtls1_read_failed(sc, ret);
279 /*
280 * Anything other than a timeout is an error. SSLfatal() already
281 * called if appropriate.
282 */
283 if (ret <= 0)
284 return ret;
285 else
286 goto start;
287 }
288 rr->off = 0;
289 sc->rlayer.num_recs++;
cffafb5f 290 } while (sc->rlayer.rrlmethod->processed_read_pending(sc->rlayer.rrl)
eddb067e 291 && sc->rlayer.num_recs < SSL_MAX_PIPELINES);
0f113f3e 292 }
eddb067e 293 rr = &sc->rlayer.tlsrecs[sc->rlayer.curr_rec];
0f113f3e 294
af58be76
MC
295 /*
296 * Reset the count of consecutive warning alerts if we've got a non-empty
297 * record that isn't an alert.
298 */
eddb067e 299 if (rr->type != SSL3_RT_ALERT && rr->length != 0)
38b051a1 300 sc->rlayer.alert_count = 0;
af58be76 301
0f113f3e
MC
302 /* we now have a packet which can be read and processed */
303
38b051a1 304 if (sc->s3.change_cipher_spec /* set when we receive ChangeCipherSpec,
555cbb32 305 * reset by ssl3_get_finished */
eddb067e 306 && (rr->type != SSL3_RT_HANDSHAKE)) {
0f113f3e
MC
307 /*
308 * We now have application data between CCS and Finished. Most likely
309 * the packets were reordered on their way, so buffer the application
310 * data for later processing rather than dropping the connection.
311 */
eddb067e 312 if (dtls_buffer_record(sc, rr) < 0) {
c2853382 313 /* SSLfatal() already called */
0f113f3e
MC
314 return -1;
315 }
eddb067e 316 ssl_release_record(sc, rr);
0f113f3e
MC
317 goto start;
318 }
319
320 /*
321 * If the other end has shut down, throw anything we read away (even in
322 * 'peek' mode)
323 */
38b051a1 324 if (sc->shutdown & SSL_RECEIVED_SHUTDOWN) {
eddb067e 325 ssl_release_record(sc, rr);
38b051a1 326 sc->rwstate = SSL_NOTHING;
eda75751 327 return 0;
0f113f3e
MC
328 }
329
eddb067e
MC
330 if (type == rr->type
331 || (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC
a230b26e 332 && type == SSL3_RT_HANDSHAKE && recvd_type != NULL)) {
c69f2adf
MC
333 /*
334 * SSL3_RT_APPLICATION_DATA or
335 * SSL3_RT_HANDSHAKE or
336 * SSL3_RT_CHANGE_CIPHER_SPEC
337 */
0f113f3e
MC
338 /*
339 * make sure that we are not getting application data when we are
340 * doing a handshake for the first time
341 */
342 if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
38b051a1
TM
343 (sc->enc_read_ctx == NULL)) {
344 SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE,
c2853382
MC
345 SSL_R_APP_DATA_IN_HANDSHAKE);
346 return -1;
0f113f3e 347 }
7e159e01 348
c69f2adf 349 if (recvd_type != NULL)
eddb067e 350 *recvd_type = rr->type;
c69f2adf 351
66fab923
MC
352 if (len == 0) {
353 /*
eddb067e 354 * Release a zero length record. This ensures multiple calls to
66fab923
MC
355 * SSL_read() with a zero length buffer will eventually cause
356 * SSL_pending() to report data as being available.
357 */
eddb067e
MC
358 if (rr->length == 0)
359 ssl_release_record(sc, rr);
eda75751 360 return 0;
66fab923 361 }
0f113f3e 362
eddb067e
MC
363 if (len > rr->length)
364 n = rr->length;
0f113f3e 365 else
eda75751 366 n = len;
0f113f3e 367
eddb067e 368 memcpy(buf, &(rr->data[rr->off]), n);
66fab923 369 if (peek) {
eddb067e
MC
370 if (rr->length == 0)
371 ssl_release_record(sc, rr);
66fab923 372 } else {
38b051a1 373 if (sc->options & SSL_OP_CLEANSE_PLAINTEXT)
eddb067e
MC
374 OPENSSL_cleanse(&(rr->data[rr->off]), n);
375 rr->length -= n;
376 rr->off += n;
8bbf7ef6 377 if (rr->length == 0)
eddb067e 378 ssl_release_record(sc, rr);
0f113f3e 379 }
7e159e01 380#ifndef OPENSSL_NO_SCTP
0f113f3e
MC
381 /*
382 * We might had to delay a close_notify alert because of reordered
383 * app data. If there was an alert and there is no message to read
384 * anymore, finally set shutdown.
385 */
386 if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
38b051a1 387 sc->d1->shutdown_received
639e5760 388 && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s)) <= 0) {
38b051a1 389 sc->shutdown |= SSL_RECEIVED_SHUTDOWN;
eda75751 390 return 0;
0f113f3e
MC
391 }
392#endif
02ba18a6 393 *readbytes = n;
eda75751 394 return 1;
0f113f3e
MC
395 }
396
397 /*
398 * If we get here, then type != rr->type; if we have a handshake message,
399 * then it was unexpected (Hello Request or Client Hello).
400 */
401
eddb067e 402 if (rr->type == SSL3_RT_ALERT) {
bd990e25 403 unsigned int alert_level, alert_descr;
eddb067e 404 unsigned char *alert_bytes = rr->data + rr->off;
bd990e25 405 PACKET alert;
0f113f3e 406
eddb067e 407 if (!PACKET_buf_init(&alert, alert_bytes, rr->length)
bd990e25
MC
408 || !PACKET_get_1(&alert, &alert_level)
409 || !PACKET_get_1(&alert, &alert_descr)
410 || PACKET_remaining(&alert) != 0) {
38b051a1 411 SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_INVALID_ALERT);
c2853382 412 return -1;
0f113f3e
MC
413 }
414
38b051a1
TM
415 if (sc->msg_callback)
416 sc->msg_callback(0, sc->version, SSL3_RT_ALERT, alert_bytes, 2, s,
417 sc->msg_callback_arg);
0f113f3e 418
38b051a1
TM
419 if (sc->info_callback != NULL)
420 cb = sc->info_callback;
0f113f3e
MC
421 else if (s->ctx->info_callback != NULL)
422 cb = s->ctx->info_callback;
423
424 if (cb != NULL) {
425 j = (alert_level << 8) | alert_descr;
426 cb(s, SSL_CB_READ_ALERT, j);
427 }
428
fd865cad 429 if (alert_level == SSL3_AL_WARNING) {
38b051a1 430 sc->s3.warn_alert = alert_descr;
eddb067e 431 ssl_release_record(sc, rr);
af58be76 432
38b051a1
TM
433 sc->rlayer.alert_count++;
434 if (sc->rlayer.alert_count == MAX_WARN_ALERT_COUNT) {
435 SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE,
c2853382
MC
436 SSL_R_TOO_MANY_WARN_ALERTS);
437 return -1;
af58be76
MC
438 }
439
0f113f3e 440 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
7e159e01 441#ifndef OPENSSL_NO_SCTP
0f113f3e
MC
442 /*
443 * With SCTP and streams the socket may deliver app data
444 * after a close_notify alert. We have to check this first so
445 * that nothing gets discarded.
446 */
447 if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
639e5760 448 BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s)) > 0) {
38b051a1
TM
449 sc->d1->shutdown_received = 1;
450 sc->rwstate = SSL_READING;
0f113f3e
MC
451 BIO_clear_retry_flags(SSL_get_rbio(s));
452 BIO_set_retry_read(SSL_get_rbio(s));
453 return -1;
454 }
7e159e01 455#endif
38b051a1 456 sc->shutdown |= SSL_RECEIVED_SHUTDOWN;
eda75751 457 return 0;
0f113f3e 458 }
fd865cad 459 } else if (alert_level == SSL3_AL_FATAL) {
38b051a1
TM
460 sc->rwstate = SSL_NOTHING;
461 sc->s3.fatal_alert = alert_descr;
462 SSLfatal_data(sc, SSL_AD_NO_ALERT,
c48ffbcc
RL
463 SSL_AD_REASON_OFFSET + alert_descr,
464 "SSL alert number %d", alert_descr);
38b051a1 465 sc->shutdown |= SSL_RECEIVED_SHUTDOWN;
eddb067e 466 ssl_release_record(sc, rr);
38b051a1 467 SSL_CTX_remove_session(sc->session_ctx, sc->session);
eda75751 468 return 0;
0f113f3e 469 } else {
38b051a1 470 SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_ALERT_TYPE);
c2853382 471 return -1;
0f113f3e
MC
472 }
473
474 goto start;
475 }
476
38b051a1 477 if (sc->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a
0f113f3e 478 * shutdown */
38b051a1 479 sc->rwstate = SSL_NOTHING;
eddb067e 480 ssl_release_record(sc, rr);
eda75751 481 return 0;
0f113f3e
MC
482 }
483
eddb067e 484 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
0f113f3e
MC
485 /*
486 * We can't process a CCS now, because previous handshake messages
487 * are still missing, so just drop it.
488 */
eddb067e 489 ssl_release_record(sc, rr);
0f113f3e
MC
490 goto start;
491 }
492
493 /*
494 * Unexpected handshake message (Client Hello, or protocol violation)
495 */
eddb067e 496 if (rr->type == SSL3_RT_HANDSHAKE && !ossl_statem_get_in_handshake(sc)) {
0f113f3e
MC
497 struct hm_header_st msg_hdr;
498
bd990e25
MC
499 /*
500 * This may just be a stale retransmit. Also sanity check that we have
501 * at least enough record bytes for a message header
502 */
eddb067e
MC
503 if (rr->epoch != sc->rlayer.d->r_epoch
504 || rr->length < DTLS1_HM_HEADER_LENGTH) {
505 ssl_release_record(sc, rr);
0f113f3e
MC
506 goto start;
507 }
508
bd990e25
MC
509 dtls1_get_message_header(rr->data, &msg_hdr);
510
0f113f3e
MC
511 /*
512 * If we are server, we may have a repeated FINISHED of the client
513 * here, then retransmit our CCS and FINISHED.
514 */
515 if (msg_hdr.type == SSL3_MT_FINISHED) {
38b051a1 516 if (dtls1_check_timeout_num(sc) < 0) {
c2853382 517 /* SSLfatal) already called */
0f113f3e 518 return -1;
c2853382 519 }
0f113f3e 520
38b051a1 521 if (dtls1_retransmit_buffered_messages(sc) <= 0) {
d273b60b 522 /* Fail if we encountered a fatal error */
38b051a1 523 if (ossl_statem_in_error(sc))
d273b60b 524 return -1;
d273b60b 525 }
eddb067e 526 ssl_release_record(sc, rr);
38b051a1 527 if (!(sc->mode & SSL_MODE_AUTO_RETRY)) {
cffafb5f 528 if (!sc->rlayer.rrlmethod->unprocessed_read_pending(sc->rlayer.rrl)) {
ad962252
MC
529 /* no read-ahead left? */
530 BIO *bio;
531
38b051a1 532 sc->rwstate = SSL_READING;
ad962252
MC
533 bio = SSL_get_rbio(s);
534 BIO_clear_retry_flags(bio);
535 BIO_set_retry_read(bio);
536 return -1;
537 }
538 }
0f113f3e
MC
539 goto start;
540 }
541
c7f47786
MC
542 /*
543 * To get here we must be trying to read app data but found handshake
544 * data. But if we're trying to read app data, and we're not in init
545 * (which is tested for at the top of this function) then init must be
546 * finished
547 */
b77f3ed1 548 if (!ossl_assert(SSL_is_init_finished(s))) {
38b051a1 549 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
c2853382 550 return -1;
0f113f3e 551 }
c7f47786
MC
552
553 /* We found handshake data, so we're going back into init */
38b051a1 554 ossl_statem_set_in_init(sc, 1);
c7f47786 555
38b051a1 556 i = sc->handshake_func(s);
c2853382 557 /* SSLfatal() called if appropriate */
0f113f3e 558 if (i < 0)
eda75751 559 return i;
c2853382 560 if (i == 0)
eda75751 561 return -1;
0f113f3e 562
38b051a1 563 if (!(sc->mode & SSL_MODE_AUTO_RETRY)) {
cffafb5f 564 if (!sc->rlayer.rrlmethod->unprocessed_read_pending(sc->rlayer.rrl)) {
28d59af8 565 /* no read-ahead left? */
0f113f3e
MC
566 BIO *bio;
567 /*
568 * In the case where we try to read application data, but we
569 * trigger an SSL handshake, we return -1 with the retry
570 * option set. Otherwise renegotiation may cause nasty
571 * problems in the blocking world
572 */
38b051a1 573 sc->rwstate = SSL_READING;
0f113f3e
MC
574 bio = SSL_get_rbio(s);
575 BIO_clear_retry_flags(bio);
576 BIO_set_retry_read(bio);
eda75751 577 return -1;
0f113f3e
MC
578 }
579 }
580 goto start;
581 }
582
eddb067e 583 switch (rr->type) {
0f113f3e 584 default:
38b051a1 585 SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD);
c2853382 586 return -1;
0f113f3e
MC
587 case SSL3_RT_CHANGE_CIPHER_SPEC:
588 case SSL3_RT_ALERT:
589 case SSL3_RT_HANDSHAKE:
590 /*
591 * we already handled all of these, with the possible exception of
024f543c
MC
592 * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but
593 * that should not happen when type != rr->type
0f113f3e 594 */
38b051a1 595 SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE, ERR_R_INTERNAL_ERROR);
c2853382 596 return -1;
0f113f3e
MC
597 case SSL3_RT_APPLICATION_DATA:
598 /*
599 * At this point, we were expecting handshake data, but have
600 * application data. If the library was running inside ssl3_read()
601 * (i.e. in_read_app_data is set) and it makes sense to read
602 * application data at this point (session renegotiation not yet
603 * started), we will indulge it.
604 */
38b051a1
TM
605 if (sc->s3.in_read_app_data &&
606 (sc->s3.total_renegotiations != 0) &&
607 ossl_statem_app_data_allowed(sc)) {
608 sc->s3.in_read_app_data = 2;
eda75751 609 return -1;
0f113f3e 610 } else {
38b051a1 611 SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD);
c2853382 612 return -1;
0f113f3e
MC
613 }
614 }
615 /* not reached */
0f113f3e
MC
616}
617
0f113f3e
MC
618/*
619 * Call this to write data in records of type 'type' It will return <= 0 if
620 * not all data has been sent or non-blocking IO.
36d16f8e 621 */
38b051a1
TM
622int dtls1_write_bytes(SSL_CONNECTION *s, int type, const void *buf,
623 size_t len, size_t *written)
0f113f3e
MC
624{
625 int i;
626
42bd7a16 627 if (!ossl_assert(len <= SSL3_RT_MAX_PLAIN_LENGTH)) {
c48ffbcc 628 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
380a522f 629 return -1;
42bd7a16 630 }
0f113f3e 631 s->rwstate = SSL_NOTHING;
22d6e854 632 i = do_dtls1_write(s, type, buf, len, written);
0f113f3e
MC
633 return i;
634}
635
88bf978e
MC
636int do_dtls1_write(SSL_CONNECTION *sc, int type, const unsigned char *buf,
637 size_t len, size_t *written)
638{
639 int i;
640 OSSL_RECORD_TEMPLATE tmpl;
641 SSL *s = SSL_CONNECTION_GET_SSL(sc);
642 SSL3_BUFFER *wb;
643 int ret;
644
645 wb = &sc->rlayer.wbuf[0];
646
647 /*
648 * DTLS writes whole datagrams, so there can't be anything left in
649 * the buffer.
650 */
651 /* TODO(RECLAYER): Remove me */
652 if (!ossl_assert(SSL3_BUFFER_get_left(wb) == 0)) {
653 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
654 return 0;
655 }
656
657 /* If we have an alert to send, lets send it */
658 if (sc->s3.alert_dispatch) {
659 i = s->method->ssl_dispatch_alert(s);
660 if (i <= 0)
661 return i;
662 /* if it went, fall through and send more stuff */
663 }
664
665 if (len == 0)
666 return 0;
667
668 if (len > ssl_get_max_send_fragment(sc)) {
669 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE);
670 return 0;
671 }
672
673 tmpl.type = type;
674 /*
675 * Special case: for hello verify request, client version 1.0 and we
676 * haven't decided which version to use yet send back using version 1.0
677 * header: otherwise some clients will ignore it.
678 */
679 if (s->method->version == DTLS_ANY_VERSION
680 && sc->max_proto_version != DTLS1_BAD_VER)
681 tmpl.version = DTLS1_VERSION;
682 else
683 tmpl.version = sc->version;
684 tmpl.buf = buf;
685 tmpl.buflen = len;
686
fc938db6 687 ret = sc->rlayer.wrlmethod->write_records(sc->rlayer.wrl, &tmpl, 1);
88bf978e
MC
688
689 if (ret > 0)
690 *written = (int)len;
691
692 return ret;
0f113f3e 693}
36d16f8e 694
38b051a1 695void dtls1_reset_seq_numbers(SSL_CONNECTION *s, int rw)
0f113f3e
MC
696{
697 unsigned char *seq;
0f113f3e
MC
698
699 if (rw & SSL3_CC_READ) {
78a39fe7 700 s->rlayer.d->r_epoch++;
5cb4d646
MC
701
702 /*
703 * We must not use any buffered messages received from the previous
704 * epoch
705 */
706 dtls1_clear_received_buffer(s);
0f113f3e 707 } else {
de07f311 708 seq = s->rlayer.write_sequence;
3bb8f87d 709 memcpy(s->rlayer.d->last_write_sequence, seq,
de07f311 710 sizeof(s->rlayer.write_sequence));
78a39fe7 711 s->rlayer.d->w_epoch++;
19d00444 712 memset(seq, 0, sizeof(s->rlayer.write_sequence));
0f113f3e 713 }
0f113f3e 714}