]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/record/methods/tls_common.c
Move declarations out of record.h and record_local.h
[thirdparty/openssl.git] / ssl / record / methods / tls_common.c
CommitLineData
34a4068c
MC
1/*
2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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
8 */
9
1d367677 10#include <assert.h>
34a4068c
MC
11#include <openssl/bio.h>
12#include <openssl/ssl.h>
e2d5742b
MC
13#include <openssl/err.h>
14#include <openssl/core_names.h>
1e76110b 15#include <openssl/comp.h>
91fe8ff0 16#include <openssl/ssl.h>
e2d5742b 17#include "internal/e_os.h"
4030869d 18#include "internal/packet.h"
4840c2a5
MC
19#include "../../ssl_local.h"
20#include "../record_local.h"
50023e9b 21#include "recmethod_local.h"
e2d5742b 22
359affde
MC
23static void tls_int_free(OSSL_RECORD_LAYER *rl);
24
e158ada6
MC
25void SSL3_BUFFER_release(SSL3_BUFFER *b)
26{
27 OPENSSL_free(b->buf);
28 b->buf = NULL;
29}
30
23c57f00
MC
31static void SSL3_RECORD_release(SSL3_RECORD *r, size_t num_recs)
32{
33 size_t i;
34
35 for (i = 0; i < num_recs; i++) {
36 OPENSSL_free(r[i].comp);
37 r[i].comp = NULL;
38 }
39}
40
41void SSL3_RECORD_set_seq_num(SSL3_RECORD *r, const unsigned char *seq_num)
42{
43 memcpy(r->seq_num, seq_num, SEQ_NUM_SIZE);
44}
45
50023e9b
MC
46void ossl_rlayer_fatal(OSSL_RECORD_LAYER *rl, int al, int reason,
47 const char *fmt, ...)
e2d5742b
MC
48{
49 va_list args;
50
51 va_start(args, fmt);
52 ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
53 va_end(args);
54
55 rl->alert = al;
56}
57
50023e9b
MC
58int ossl_set_tls_provider_parameters(OSSL_RECORD_LAYER *rl,
59 EVP_CIPHER_CTX *ctx,
60 const EVP_CIPHER *ciph,
7f2f0ac7 61 const EVP_MD *md)
aedbb71b
MC
62{
63 /*
64 * Provided cipher, the TLS padding/MAC removal is performed provider
65 * side so we need to tell the ctx about our TLS version and mac size
66 */
67 OSSL_PARAM params[3], *pprm = params;
68 size_t macsize = 0;
69 int imacsize = -1;
70
71 if ((EVP_CIPHER_get_flags(ciph) & EVP_CIPH_FLAG_AEAD_CIPHER) == 0
7f2f0ac7 72 && !rl->use_etm)
aedbb71b
MC
73 imacsize = EVP_MD_get_size(md);
74 if (imacsize >= 0)
75 macsize = (size_t)imacsize;
76
77 *pprm++ = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS_VERSION,
78 &rl->version);
79 *pprm++ = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_TLS_MAC_SIZE,
80 &macsize);
81 *pprm = OSSL_PARAM_construct_end();
82
83 if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
7c293999 84 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
aedbb71b
MC
85 return 0;
86 }
87
88 return 1;
89}
90
50023e9b
MC
91/*
92 * ssl3_cbc_record_digest_supported returns 1 iff |ctx| uses a hash function
93 * which ssl3_cbc_digest_record supports.
aedbb71b 94 */
50023e9b 95char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx)
2b891e30 96{
50023e9b
MC
97 switch (EVP_MD_CTX_get_type(ctx)) {
98 case NID_md5:
99 case NID_sha1:
100 case NID_sha224:
101 case NID_sha256:
102 case NID_sha384:
103 case NID_sha512:
2b891e30 104 return 1;
50023e9b 105 default:
aedbb71b 106 return 0;
aedbb71b 107 }
aedbb71b
MC
108}
109
976b263d 110#ifndef OPENSSL_NO_COMP
9b7fb65e 111static int tls_allow_compression(OSSL_RECORD_LAYER *rl)
e2d5742b
MC
112{
113 if (rl->options & SSL_OP_NO_COMPRESSION)
114 return 0;
ed0e298f 115
b85ebc4b
MC
116 return rl->security == NULL
117 || rl->security(rl->cbarg, SSL_SECOP_COMPRESSION, 0, 0, NULL);
e2d5742b 118}
976b263d 119#endif
e2d5742b 120
23bf52a4
MC
121static void tls_release_write_buffer_int(OSSL_RECORD_LAYER *rl, size_t start)
122{
123 SSL3_BUFFER *wb;
124 size_t pipes;
125
126 pipes = rl->numwpipes;
127
128 while (pipes > start) {
129 wb = &rl->wbuf[pipes - 1];
130
131 if (SSL3_BUFFER_is_app_buffer(wb))
132 SSL3_BUFFER_set_app_buffer(wb, 0);
133 else
134 OPENSSL_free(wb->buf);
135 wb->buf = NULL;
136 pipes--;
137 }
138}
139
bafe524b
MC
140int tls_setup_write_buffer(OSSL_RECORD_LAYER *rl, size_t numwpipes,
141 size_t firstlen, size_t nextlen)
151f313e
MC
142{
143 unsigned char *p;
144 size_t align = 0, headerlen;
145 SSL3_BUFFER *wb;
146 size_t currpipe;
85b358b0 147 size_t defltlen = 0;
151f313e 148
85b358b0 149 if (firstlen == 0 || (numwpipes > 1 && nextlen == 0)) {
320145d5 150 if (rl->isdtls)
151f313e
MC
151 headerlen = DTLS1_RT_HEADER_LENGTH + 1;
152 else
153 headerlen = SSL3_RT_HEADER_LENGTH;
154
85b358b0 155#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0
151f313e
MC
156 align = SSL3_ALIGN_PAYLOAD - 1;
157#endif
158
435d88d7
MC
159 defltlen = rl->max_frag_len + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD
160 + headerlen + align;
151f313e 161#ifndef OPENSSL_NO_COMP
9251c3c4 162 if (tls_allow_compression(rl))
85b358b0 163 defltlen += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
151f313e
MC
164#endif
165 if (!(rl->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
85b358b0 166 defltlen += headerlen + align + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
151f313e
MC
167 }
168
169 wb = rl->wbuf;
170 for (currpipe = 0; currpipe < numwpipes; currpipe++) {
171 SSL3_BUFFER *thiswb = &wb[currpipe];
85b358b0
MC
172 size_t len = (currpipe == 0) ? firstlen : nextlen;
173
174 if (len == 0)
175 len = defltlen;
151f313e
MC
176
177 if (thiswb->len != len) {
178 OPENSSL_free(thiswb->buf);
179 thiswb->buf = NULL; /* force reallocation */
180 }
181
5bc226ab
MC
182 p = thiswb->buf;
183 if (p == NULL) {
184 p = OPENSSL_malloc(len);
185 if (p == NULL) {
186 if (rl->numwpipes < currpipe)
187 rl->numwpipes = currpipe;
188 /*
189 * We've got a malloc failure, and we're still initialising
190 * buffers. We assume we're so doomed that we won't even be able
191 * to send an alert.
192 */
193 RLAYERfatal(rl, SSL_AD_NO_ALERT, ERR_R_CRYPTO_LIB);
194 return 0;
151f313e 195 }
151f313e 196 }
5bc226ab
MC
197 memset(thiswb, 0, sizeof(SSL3_BUFFER));
198 thiswb->buf = p;
199 thiswb->len = len;
151f313e
MC
200 }
201
23bf52a4
MC
202 /* Free any previously allocated buffers that we are no longer using */
203 tls_release_write_buffer_int(rl, currpipe);
204
205 rl->numwpipes = numwpipes;
206
151f313e
MC
207 return 1;
208}
209
210static void tls_release_write_buffer(OSSL_RECORD_LAYER *rl)
211{
23bf52a4 212 tls_release_write_buffer_int(rl, 0);
e7694c69
MC
213
214 rl->numwpipes = 0;
151f313e
MC
215}
216
9b7fb65e 217int tls_setup_read_buffer(OSSL_RECORD_LAYER *rl)
e2d5742b
MC
218{
219 unsigned char *p;
220 size_t len, align = 0, headerlen;
221 SSL3_BUFFER *b;
222
223 b = &rl->rbuf;
224
225 if (rl->isdtls)
226 headerlen = DTLS1_RT_HEADER_LENGTH;
227 else
228 headerlen = SSL3_RT_HEADER_LENGTH;
229
1704961c 230#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0
e2d5742b
MC
231 align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1);
232#endif
233
234 if (b->buf == NULL) {
435d88d7
MC
235 len = rl->max_frag_len
236 + SSL3_RT_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
e2d5742b 237#ifndef OPENSSL_NO_COMP
9b7fb65e 238 if (tls_allow_compression(rl))
e2d5742b
MC
239 len += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
240#endif
8ccde3fc
MC
241
242 /* Ensure our buffer is large enough to support all our pipelines */
243 if (rl->max_pipelines > 1)
244 len *= rl->max_pipelines;
245
e2d5742b
MC
246 if (b->default_len > len)
247 len = b->default_len;
8ccde3fc 248
e2d5742b
MC
249 if ((p = OPENSSL_malloc(len)) == NULL) {
250 /*
251 * We've got a malloc failure, and we're still initialising buffers.
252 * We assume we're so doomed that we won't even be able to send an
253 * alert.
254 */
e077455e 255 RLAYERfatal(rl, SSL_AD_NO_ALERT, ERR_R_CRYPTO_LIB);
e2d5742b
MC
256 return 0;
257 }
258 b->buf = p;
259 b->len = len;
260 }
261
262 return 1;
263}
264
9b7fb65e 265static int tls_release_read_buffer(OSSL_RECORD_LAYER *rl)
e2d5742b
MC
266{
267 SSL3_BUFFER *b;
268
269 b = &rl->rbuf;
1704961c 270 if ((rl->options & SSL_OP_CLEANSE_PLAINTEXT) != 0)
e2d5742b
MC
271 OPENSSL_cleanse(b->buf, b->len);
272 OPENSSL_free(b->buf);
273 b->buf = NULL;
274 return 1;
275}
276
277/*
278 * Return values are as per SSL_read()
279 */
1853d20a
MC
280int tls_default_read_n(OSSL_RECORD_LAYER *rl, size_t n, size_t max, int extend,
281 int clearold, size_t *readbytes)
e2d5742b
MC
282{
283 /*
284 * If extend == 0, obtain new n-byte packet; if extend == 1, increase
285 * packet by another n bytes. The packet will be in the sub-array of
b0a9042e
MC
286 * rl->rbuf.buf specified by rl->packet and rl->packet_length. (If
287 * rl->read_ahead is set, 'max' bytes may be stored in rbuf [plus
288 * rl->packet_length bytes if extend == 1].) if clearold == 1, move the
289 * packet to the start of the buffer; if clearold == 0 then leave any old
290 * packets where they were
e2d5742b
MC
291 */
292 size_t len, left, align = 0;
293 unsigned char *pkt;
294 SSL3_BUFFER *rb;
295
296 if (n == 0)
297 return OSSL_RECORD_RETURN_NON_FATAL_ERR;
298
299 rb = &rl->rbuf;
e2d5742b
MC
300 left = rb->left;
301#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0
302 align = (size_t)rb->buf + SSL3_RT_HEADER_LENGTH;
303 align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD);
304#endif
305
306 if (!extend) {
307 /* start with empty packet ... */
8ccde3fc 308 if (left == 0)
e2d5742b 309 rb->offset = align;
8ccde3fc 310
e2d5742b
MC
311 rl->packet = rb->buf + rb->offset;
312 rl->packet_length = 0;
313 /* ... now we can act as if 'extend' was set */
314 }
315
316 len = rl->packet_length;
317 pkt = rb->buf + align;
318 /*
319 * Move any available bytes to front of buffer: 'len' bytes already
320 * pointed to by 'packet', 'left' extra ones at the end
321 */
322 if (rl->packet != pkt && clearold == 1) {
323 memmove(pkt, rl->packet, len + left);
324 rl->packet = pkt;
325 rb->offset = len + align;
326 }
327
328 /*
329 * For DTLS/UDP reads should not span multiple packets because the read
330 * operation returns the whole packet at once (as long as it fits into
331 * the buffer).
332 */
333 if (rl->isdtls) {
f78c5199
MC
334 if (left == 0 && extend) {
335 /*
336 * We received a record with a header but no body data. This will
337 * get dumped.
338 */
339 return OSSL_RECORD_RETURN_NON_FATAL_ERR;
340 }
e2d5742b
MC
341 if (left > 0 && n > left)
342 n = left;
343 }
344
345 /* if there is enough in the buffer from a previous read, take some */
346 if (left >= n) {
347 rl->packet_length += n;
348 rb->left = left - n;
349 rb->offset += n;
350 *readbytes = n;
351 return OSSL_RECORD_RETURN_SUCCESS;
352 }
353
354 /* else we need to read more data */
355
356 if (n > rb->len - rb->offset) {
357 /* does not happen */
358 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
359 return OSSL_RECORD_RETURN_FATAL;
360 }
361
1853d20a
MC
362 /* We always act like read_ahead is set for DTLS */
363 if (!rl->read_ahead && !rl->isdtls) {
e2d5742b
MC
364 /* ignore max parameter */
365 max = n;
366 } else {
367 if (max < n)
368 max = n;
369 if (max > rb->len - rb->offset)
370 max = rb->len - rb->offset;
371 }
372
373 while (left < n) {
374 size_t bioread = 0;
375 int ret;
359affde 376 BIO *bio = rl->prev != NULL ? rl->prev : rl->bio;
e2d5742b
MC
377
378 /*
1704961c
MC
379 * Now we have len+left bytes at the front of rl->rbuf.buf and
380 * need to read in more until we have len + n (up to len + max if
e2d5742b
MC
381 * possible)
382 */
383
384 clear_sys_error();
359affde
MC
385 if (bio != NULL) {
386 ret = BIO_read(bio, pkt + len + left, max - left);
e2d5742b
MC
387 if (ret > 0) {
388 bioread = ret;
389 ret = OSSL_RECORD_RETURN_SUCCESS;
359affde
MC
390 } else if (BIO_should_retry(bio)) {
391 if (rl->prev != NULL) {
392 /*
393 * We were reading from the previous epoch. Now there is no
394 * more data, so swap to the actual transport BIO
395 */
396 BIO_free(rl->prev);
397 rl->prev = NULL;
398 continue;
399 }
e2d5742b 400 ret = OSSL_RECORD_RETURN_RETRY;
359affde 401 } else if (BIO_eof(bio)) {
e2d5742b
MC
402 ret = OSSL_RECORD_RETURN_EOF;
403 } else {
404 ret = OSSL_RECORD_RETURN_FATAL;
405 }
406 } else {
407 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_READ_BIO_NOT_SET);
408 ret = OSSL_RECORD_RETURN_FATAL;
409 }
410
411 if (ret <= OSSL_RECORD_RETURN_RETRY) {
412 rb->left = left;
3de76959 413 if ((rl->mode & SSL_MODE_RELEASE_BUFFERS) != 0 && !rl->isdtls)
e2d5742b 414 if (len + left == 0)
9b7fb65e 415 tls_release_read_buffer(rl);
e2d5742b
MC
416 return ret;
417 }
418 left += bioread;
419 /*
420 * reads should *never* span multiple packets for DTLS because the
421 * underlying transport protocol is message oriented as opposed to
422 * byte oriented as in the TLS case.
423 */
424 if (rl->isdtls) {
425 if (n > left)
426 n = left; /* makes the while condition false */
427 }
428 }
429
430 /* done reading, now the book-keeping */
431 rb->offset += n;
432 rb->left = left - n;
433 rl->packet_length += n;
434 *readbytes = n;
435 return OSSL_RECORD_RETURN_SUCCESS;
436}
437
4030869d
MC
438/*
439 * Peeks ahead into "read_ahead" data to see if we have a whole record waiting
440 * for us in the buffer.
441 */
442static int tls_record_app_data_waiting(OSSL_RECORD_LAYER *rl)
443{
444 SSL3_BUFFER *rbuf;
445 size_t left, len;
446 unsigned char *p;
447
448 rbuf = &rl->rbuf;
449
450 p = SSL3_BUFFER_get_buf(rbuf);
451 if (p == NULL)
452 return 0;
453
454 left = SSL3_BUFFER_get_left(rbuf);
455
456 if (left < SSL3_RT_HEADER_LENGTH)
457 return 0;
458
459 p += SSL3_BUFFER_get_offset(rbuf);
460
461 /*
462 * We only check the type and record length, we will sanity check version
463 * etc later
464 */
465 if (*p != SSL3_RT_APPLICATION_DATA)
466 return 0;
467
468 p += 3;
469 n2s(p, len);
470
471 if (left < SSL3_RT_HEADER_LENGTH + len)
472 return 0;
473
474 return 1;
475}
476
9dd90232
MC
477static int rlayer_early_data_count_ok(OSSL_RECORD_LAYER *rl, size_t length,
478 size_t overhead, int send)
479{
480 uint32_t max_early_data = rl->max_early_data;
481
482 if (max_early_data == 0) {
483 RLAYERfatal(rl, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
1704961c 484 SSL_R_TOO_MUCH_EARLY_DATA);
9dd90232
MC
485 return 0;
486 }
487
488 /* If we are dealing with ciphertext we need to allow for the overhead */
489 max_early_data += overhead;
490
491 if (rl->early_data_count + length > max_early_data) {
492 RLAYERfatal(rl, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
1704961c 493 SSL_R_TOO_MUCH_EARLY_DATA);
9dd90232
MC
494 return 0;
495 }
496 rl->early_data_count += length;
497
498 return 1;
499}
500
4030869d
MC
501/*
502 * MAX_EMPTY_RECORDS defines the number of consecutive, empty records that
1704961c 503 * will be processed per call to tls_get_more_records. Without this limit an
4030869d 504 * attacker could send empty records at a faster rate than we can process and
1704961c 505 * cause tls_get_more_records to loop forever.
4030869d
MC
506 */
507#define MAX_EMPTY_RECORDS 32
508
509#define SSL2_RT_HEADER_LENGTH 2
510
511/*-
512 * Call this to buffer new input records in rl->rrec.
513 * It will return a OSSL_RECORD_RETURN_* value.
514 * When it finishes successfully (OSSL_RECORD_RETURN_SUCCESS), |rl->num_recs|
515 * records have been decoded. For each record 'i':
516 * rrec[i].type - is the type of record
517 * rrec[i].data, - data
518 * rrec[i].length, - number of bytes
519 * Multiple records will only be returned if the record types are all
520 * SSL3_RT_APPLICATION_DATA. The number of records returned will always be <=
521 * |max_pipelines|
522 */
eddb067e 523int tls_get_more_records(OSSL_RECORD_LAYER *rl)
4030869d
MC
524{
525 int enc_err, rret;
526 int i;
527 size_t more, n;
528 SSL3_RECORD *rr, *thisrr;
529 SSL3_BUFFER *rbuf;
4030869d
MC
530 unsigned char *p;
531 unsigned char md[EVP_MAX_MD_SIZE];
532 unsigned int version;
533 size_t mac_size = 0;
534 int imac_size;
535 size_t num_recs = 0, max_recs, j;
536 PACKET pkt, sslv2pkt;
4030869d
MC
537 SSL_MAC_BUF *macbufs = NULL;
538 int ret = OSSL_RECORD_RETURN_FATAL;
4030869d
MC
539
540 rr = rl->rrec;
541 rbuf = &rl->rbuf;
cc110a0a 542 if (rbuf->buf == NULL) {
9b7fb65e 543 if (!tls_setup_read_buffer(rl)) {
cc110a0a
MC
544 /* RLAYERfatal() already called */
545 return OSSL_RECORD_RETURN_FATAL;
546 }
547 }
4030869d 548
8124ab56
MC
549 max_recs = rl->max_pipelines;
550
4030869d
MC
551 if (max_recs == 0)
552 max_recs = 1;
4030869d 553
4030869d
MC
554 do {
555 thisrr = &rr[num_recs];
556
557 /* check if we have the header */
558 if ((rl->rstate != SSL_ST_READ_BODY) ||
559 (rl->packet_length < SSL3_RT_HEADER_LENGTH)) {
560 size_t sslv2len;
561 unsigned int type;
562
1853d20a
MC
563 rret = rl->funcs->read_n(rl, SSL3_RT_HEADER_LENGTH,
564 SSL3_BUFFER_get_len(rbuf), 0,
565 num_recs == 0 ? 1 : 0, &n);
566
567 if (rret < OSSL_RECORD_RETURN_SUCCESS)
568 return rret; /* error or non-blocking */
569
4030869d
MC
570 rl->rstate = SSL_ST_READ_BODY;
571
572 p = rl->packet;
573 if (!PACKET_buf_init(&pkt, p, rl->packet_length)) {
574 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
575 return OSSL_RECORD_RETURN_FATAL;
576 }
577 sslv2pkt = pkt;
578 if (!PACKET_get_net_2_len(&sslv2pkt, &sslv2len)
579 || !PACKET_get_1(&sslv2pkt, &type)) {
580 RLAYERfatal(rl, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
581 return OSSL_RECORD_RETURN_FATAL;
582 }
583 /*
584 * The first record received by the server may be a V2ClientHello.
585 */
586 if (rl->role == OSSL_RECORD_ROLE_SERVER
587 && rl->is_first_record
588 && (sslv2len & 0x8000) != 0
589 && (type == SSL2_MT_CLIENT_HELLO)) {
590 /*
591 * SSLv2 style record
592 *
593 * |num_recs| here will actually always be 0 because
594 * |num_recs > 0| only ever occurs when we are processing
595 * multiple app data records - which we know isn't the case here
596 * because it is an SSLv2ClientHello. We keep it using
597 * |num_recs| for the sake of consistency
598 */
599 thisrr->type = SSL3_RT_HANDSHAKE;
600 thisrr->rec_version = SSL2_VERSION;
601
602 thisrr->length = sslv2len & 0x7fff;
603
604 if (thisrr->length > SSL3_BUFFER_get_len(rbuf)
1704961c 605 - SSL2_RT_HEADER_LENGTH) {
4030869d
MC
606 RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW,
607 SSL_R_PACKET_LENGTH_TOO_LONG);
608 return OSSL_RECORD_RETURN_FATAL;
609 }
4030869d
MC
610 } else {
611 /* SSLv3+ style record */
612
613 /* Pull apart the header into the SSL3_RECORD */
614 if (!PACKET_get_1(&pkt, &type)
615 || !PACKET_get_net_2(&pkt, &version)
616 || !PACKET_get_net_2_len(&pkt, &thisrr->length)) {
b85ebc4b
MC
617 if (rl->msg_callback != NULL)
618 rl->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, rl->cbarg);
4030869d
MC
619 RLAYERfatal(rl, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
620 return OSSL_RECORD_RETURN_FATAL;
621 }
622 thisrr->type = type;
623 thisrr->rec_version = version;
624
014baa8a
MC
625 /*
626 * When we call validate_record_header() only records actually
627 * received in SSLv2 format should have the record version set
628 * to SSL2_VERSION. This way validate_record_header() can know
629 * what format the record was in based on the version.
630 */
631 if (thisrr->rec_version == SSL2_VERSION) {
632 RLAYERfatal(rl, SSL_AD_PROTOCOL_VERSION,
633 SSL_R_WRONG_VERSION_NUMBER);
634 return OSSL_RECORD_RETURN_FATAL;
635 }
636
b85ebc4b
MC
637 if (rl->msg_callback != NULL)
638 rl->msg_callback(0, version, SSL3_RT_HEADER, p, 5, rl->cbarg);
4030869d 639
4030869d
MC
640 if (thisrr->length >
641 SSL3_BUFFER_get_len(rbuf) - SSL3_RT_HEADER_LENGTH) {
642 RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW,
643 SSL_R_PACKET_LENGTH_TOO_LONG);
644 return OSSL_RECORD_RETURN_FATAL;
645 }
646 }
647
1853d20a
MC
648 if (!rl->funcs->validate_record_header(rl, thisrr)) {
649 /* RLAYERfatal already called */
4030869d
MC
650 return OSSL_RECORD_RETURN_FATAL;
651 }
4030869d 652
1853d20a 653 /* now rl->rstate == SSL_ST_READ_BODY */
4030869d
MC
654 }
655
656 /*
657 * rl->rstate == SSL_ST_READ_BODY, get and decode the data. Calculate
658 * how much more data we need to read for the rest of the record
659 */
660 if (thisrr->rec_version == SSL2_VERSION) {
661 more = thisrr->length + SSL2_RT_HEADER_LENGTH
1704961c 662 - SSL3_RT_HEADER_LENGTH;
4030869d
MC
663 } else {
664 more = thisrr->length;
665 }
666
667 if (more > 0) {
668 /* now rl->packet_length == SSL3_RT_HEADER_LENGTH */
669
1853d20a 670 rret = rl->funcs->read_n(rl, more, more, 1, 0, &n);
4030869d
MC
671 if (rret < OSSL_RECORD_RETURN_SUCCESS)
672 return rret; /* error or non-blocking io */
673 }
674
675 /* set state for later operations */
676 rl->rstate = SSL_ST_READ_HEADER;
677
678 /*
679 * At this point, rl->packet_length == SSL3_RT_HEADER_LENGTH
680 * + thisrr->length, or rl->packet_length == SSL2_RT_HEADER_LENGTH
681 * + thisrr->length and we have that many bytes in rl->packet
682 */
683 if (thisrr->rec_version == SSL2_VERSION)
684 thisrr->input = &(rl->packet[SSL2_RT_HEADER_LENGTH]);
685 else
686 thisrr->input = &(rl->packet[SSL3_RT_HEADER_LENGTH]);
687
688 /*
689 * ok, we can now read from 'rl->packet' data into 'thisrr'.
690 * thisrr->input points at thisrr->length bytes, which need to be copied
691 * into thisrr->data by either the decryption or by the decompression.
692 * When the data is 'copied' into the thisrr->data buffer,
693 * thisrr->input will be updated to point at the new buffer
694 */
695
696 /*
697 * We now have - encrypted [ MAC [ compressed [ plain ] ] ]
698 * thisrr->length bytes of encrypted compressed stuff.
699 */
700
701 /* decrypt in place in 'thisrr->input' */
702 thisrr->data = thisrr->input;
703 thisrr->orig_len = thisrr->length;
704
4030869d
MC
705 num_recs++;
706
707 /* we have pulled in a full packet so zero things */
81c9ebd9 708 rl->packet_length = 0;
4030869d
MC
709 rl->is_first_record = 0;
710 } while (num_recs < max_recs
711 && thisrr->type == SSL3_RT_APPLICATION_DATA
88d61680 712 && RLAYER_USE_EXPLICIT_IV(rl)
6366bdd9
MC
713 && rl->enc_ctx != NULL
714 && (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(rl->enc_ctx))
4030869d
MC
715 & EVP_CIPH_FLAG_PIPELINE) != 0
716 && tls_record_app_data_waiting(rl));
717
718 if (num_recs == 1
719 && thisrr->type == SSL3_RT_CHANGE_CIPHER_SPEC
9cd9e097
MC
720 /* The following can happen in tlsany_meth after HRR */
721 && rl->version == TLS1_3_VERSION
1853d20a 722 && rl->is_first_handshake) {
4030869d
MC
723 /*
724 * CCS messages must be exactly 1 byte long, containing the value 0x01
725 */
726 if (thisrr->length != 1 || thisrr->data[0] != 0x01) {
727 RLAYERfatal(rl, SSL_AD_ILLEGAL_PARAMETER,
728 SSL_R_INVALID_CCS_MESSAGE);
729 return OSSL_RECORD_RETURN_FATAL;
730 }
731 /*
732 * CCS messages are ignored in TLSv1.3. We treat it like an empty
733 * handshake record
734 */
735 thisrr->type = SSL3_RT_HANDSHAKE;
aedbb71b 736 if (++(rl->empty_record_count) > MAX_EMPTY_RECORDS) {
4030869d
MC
737 RLAYERfatal(rl, SSL_AD_UNEXPECTED_MESSAGE,
738 SSL_R_UNEXPECTED_CCS_MESSAGE);
739 return OSSL_RECORD_RETURN_FATAL;
740 }
4030869d
MC
741 rl->num_recs = 0;
742 rl->curr_rec = 0;
743 rl->num_released = 0;
744
745 return OSSL_RECORD_RETURN_SUCCESS;
746 }
747
6366bdd9
MC
748 if (rl->md_ctx != NULL) {
749 const EVP_MD *tmpmd = EVP_MD_CTX_get0_md(rl->md_ctx);
4030869d
MC
750
751 if (tmpmd != NULL) {
752 imac_size = EVP_MD_get_size(tmpmd);
753 if (!ossl_assert(imac_size >= 0 && imac_size <= EVP_MAX_MD_SIZE)) {
1704961c
MC
754 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
755 return OSSL_RECORD_RETURN_FATAL;
4030869d
MC
756 }
757 mac_size = (size_t)imac_size;
758 }
759 }
760
761 /*
762 * If in encrypt-then-mac mode calculate mac from encrypted record. All
763 * the details below are public so no timing details can leak.
764 */
bed07b18 765 if (rl->use_etm && rl->md_ctx != NULL) {
4030869d
MC
766 unsigned char *mac;
767
768 for (j = 0; j < num_recs; j++) {
769 thisrr = &rr[j];
770
771 if (thisrr->length < mac_size) {
772 RLAYERfatal(rl, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
773 return OSSL_RECORD_RETURN_FATAL;
774 }
775 thisrr->length -= mac_size;
776 mac = thisrr->data + thisrr->length;
8124ab56 777 i = rl->funcs->mac(rl, thisrr, md, 0 /* not send */);
4030869d
MC
778 if (i == 0 || CRYPTO_memcmp(md, mac, mac_size) != 0) {
779 RLAYERfatal(rl, SSL_AD_BAD_RECORD_MAC,
780 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
781 return OSSL_RECORD_RETURN_FATAL;
782 }
783 }
784 /*
785 * We've handled the mac now - there is no MAC inside the encrypted
786 * record
787 */
788 mac_size = 0;
789 }
790
791 if (mac_size > 0) {
792 macbufs = OPENSSL_zalloc(sizeof(*macbufs) * num_recs);
793 if (macbufs == NULL) {
e077455e 794 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
4030869d
MC
795 return OSSL_RECORD_RETURN_FATAL;
796 }
797 }
798
8124ab56 799 enc_err = rl->funcs->cipher(rl, rr, num_recs, 0, macbufs, mac_size);
4030869d
MC
800
801 /*-
802 * enc_err is:
803 * 0: if the record is publicly invalid, or an internal error, or AEAD
804 * decryption failed, or ETM decryption failed.
805 * 1: Success or MTE decryption failed (MAC will be randomised)
806 */
807 if (enc_err == 0) {
d3192c26 808 if (rl->alert != SSL_AD_NO_ALERT) {
651216dd 809 /* RLAYERfatal() already got called */
4030869d
MC
810 goto end;
811 }
b85ebc4b
MC
812 if (num_recs == 1
813 && rl->skip_early_data != NULL
814 && rl->skip_early_data(rl->cbarg)) {
4030869d
MC
815 /*
816 * Valid early_data that we cannot decrypt will fail here. We treat
817 * it like an empty record.
818 */
819
820 thisrr = &rr[0];
821
9dd90232
MC
822 if (!rlayer_early_data_count_ok(rl, thisrr->length,
823 EARLY_DATA_CIPHERTEXT_OVERHEAD, 0)) {
824 /* RLAYERfatal() already called */
4030869d
MC
825 goto end;
826 }
827
828 thisrr->length = 0;
4030869d
MC
829 rl->num_recs = 0;
830 rl->curr_rec = 0;
831 rl->num_released = 0;
0755722c
MC
832 /* Reset the read sequence */
833 memset(rl->sequence, 0, sizeof(rl->sequence));
4030869d
MC
834 ret = 1;
835 goto end;
836 }
837 RLAYERfatal(rl, SSL_AD_BAD_RECORD_MAC,
838 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
839 goto end;
840 }
841 OSSL_TRACE_BEGIN(TLS) {
842 BIO_printf(trc_out, "dec %lu\n", (unsigned long)rr[0].length);
843 BIO_dump_indent(trc_out, rr[0].data, rr[0].length, 4);
844 } OSSL_TRACE_END(TLS);
845
846 /* r->length is now the compressed data plus mac */
6366bdd9 847 if (rl->enc_ctx != NULL
ffbd6e67 848 && !rl->use_etm
6366bdd9 849 && EVP_MD_CTX_get0_md(rl->md_ctx) != NULL) {
4030869d
MC
850 for (j = 0; j < num_recs; j++) {
851 SSL_MAC_BUF *thismb = &macbufs[j];
1704961c 852
4030869d
MC
853 thisrr = &rr[j];
854
8124ab56 855 i = rl->funcs->mac(rl, thisrr, md, 0 /* not send */);
4030869d
MC
856 if (i == 0 || thismb == NULL || thismb->mac == NULL
857 || CRYPTO_memcmp(md, thismb->mac, (size_t)mac_size) != 0)
858 enc_err = 0;
859 if (thisrr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
860 enc_err = 0;
861 }
862 }
863
864 if (enc_err == 0) {
d3192c26 865 if (rl->alert != SSL_AD_NO_ALERT) {
651216dd 866 /* We already called RLAYERfatal() */
4030869d
MC
867 goto end;
868 }
869 /*
870 * A separate 'decryption_failed' alert was introduced with TLS 1.0,
871 * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
872 * failure is directly visible from the ciphertext anyway, we should
873 * not reveal which kind of error occurred -- this might become
874 * visible to an attacker (e.g. via a logfile)
875 */
876 RLAYERfatal(rl, SSL_AD_BAD_RECORD_MAC,
877 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
878 goto end;
879 }
880
4030869d
MC
881 for (j = 0; j < num_recs; j++) {
882 thisrr = &rr[j];
883
8124ab56 884 if (!rl->funcs->post_process_record(rl, thisrr)) {
1853d20a 885 /* RLAYERfatal already called */
4030869d
MC
886 goto end;
887 }
888
889 /*
890 * Check if the received packet overflows the current
891 * Max Fragment Length setting.
ffbd6e67 892 * Note: rl->max_frag_len > 0 and KTLS are mutually exclusive.
4030869d 893 */
435d88d7 894 if (thisrr->length > rl->max_frag_len) {
4030869d
MC
895 RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
896 goto end;
897 }
898
899 thisrr->off = 0;
900 /*-
901 * So at this point the following is true
902 * thisrr->type is the type of record
903 * thisrr->length == number of bytes in record
904 * thisrr->off == offset to first valid byte
905 * thisrr->data == where to take bytes from, increment after use :-).
906 */
907
908 /* just read a 0 length packet */
909 if (thisrr->length == 0) {
aedbb71b 910 if (++(rl->empty_record_count) > MAX_EMPTY_RECORDS) {
1704961c
MC
911 RLAYERfatal(rl, SSL_AD_UNEXPECTED_MESSAGE,
912 SSL_R_RECORD_TOO_SMALL);
4030869d
MC
913 goto end;
914 }
915 } else {
aedbb71b 916 rl->empty_record_count = 0;
4030869d
MC
917 }
918 }
919
9dd90232 920 if (rl->level == OSSL_RECORD_PROTECTION_LEVEL_EARLY) {
4030869d
MC
921 thisrr = &rr[0];
922 if (thisrr->type == SSL3_RT_APPLICATION_DATA
9dd90232
MC
923 && !rlayer_early_data_count_ok(rl, thisrr->length, 0, 0)) {
924 /* RLAYERfatal already called */
4030869d
MC
925 goto end;
926 }
927 }
928
929 rl->num_recs = num_recs;
930 rl->curr_rec = 0;
931 rl->num_released = 0;
932 ret = OSSL_RECORD_RETURN_SUCCESS;
933 end:
934 if (macbufs != NULL) {
935 for (j = 0; j < num_recs; j++) {
936 if (macbufs[j].alloced)
937 OPENSSL_free(macbufs[j].mac);
938 }
939 OPENSSL_free(macbufs);
940 }
941 return ret;
942}
943
1853d20a
MC
944/* Shared by ssl3_meth and tls1_meth */
945int tls_default_validate_record_header(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec)
946{
947 size_t len = SSL3_RT_MAX_ENCRYPTED_LENGTH;
948
949 if (rec->rec_version != rl->version) {
950 RLAYERfatal(rl, SSL_AD_PROTOCOL_VERSION, SSL_R_WRONG_VERSION_NUMBER);
951 return 0;
952 }
953
954#ifndef OPENSSL_NO_COMP
955 /*
1704961c
MC
956 * If OPENSSL_NO_COMP is defined then SSL3_RT_MAX_ENCRYPTED_LENGTH
957 * does not include the compression overhead anyway.
958 */
9251c3c4 959 if (rl->compctx == NULL)
1853d20a
MC
960 len -= SSL3_RT_MAX_COMPRESSED_OVERHEAD;
961#endif
962
963 if (rec->length > len) {
964 RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW,
965 SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
966 return 0;
967 }
968
969 return 1;
970}
971
b9e37f8f 972int tls_do_compress(OSSL_RECORD_LAYER *rl, SSL3_RECORD *wr)
9251c3c4
MC
973{
974#ifndef OPENSSL_NO_COMP
975 int i;
976
977 i = COMP_compress_block(rl->compctx, wr->data,
978 (int)(wr->length + SSL3_RT_MAX_COMPRESSED_OVERHEAD),
979 wr->input, (int)wr->length);
980 if (i < 0)
981 return 0;
982
983 wr->length = i;
984 wr->input = wr->data;
985 return 1;
986#else
987 return 0;
988#endif
989}
990
222cf410 991int tls_do_uncompress(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec)
1853d20a
MC
992{
993#ifndef OPENSSL_NO_COMP
994 int i;
995
996 if (rec->comp == NULL) {
997 rec->comp = (unsigned char *)
998 OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
999 }
1000 if (rec->comp == NULL)
1001 return 0;
1002
9251c3c4 1003 i = COMP_expand_block(rl->compctx, rec->comp, SSL3_RT_MAX_PLAIN_LENGTH,
1704961c 1004 rec->data, (int)rec->length);
1853d20a
MC
1005 if (i < 0)
1006 return 0;
1007 else
1008 rec->length = i;
1009 rec->data = rec->comp;
1010 return 1;
1011#else
1012 return 0;
1013#endif
1014}
1015
1016/* Shared by tlsany_meth, ssl3_meth and tls1_meth */
8124ab56 1017int tls_default_post_process_record(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec)
1853d20a 1018{
9251c3c4 1019 if (rl->compctx != NULL) {
1853d20a
MC
1020 if (rec->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
1021 RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW,
1022 SSL_R_COMPRESSED_LENGTH_TOO_LONG);
1023 return 0;
1024 }
1025 if (!tls_do_uncompress(rl, rec)) {
1026 RLAYERfatal(rl, SSL_AD_DECOMPRESSION_FAILURE,
1027 SSL_R_BAD_DECOMPRESSION);
1028 return 0;
1029 }
1030 }
1031
1032 if (rec->length > SSL3_RT_MAX_PLAIN_LENGTH) {
1033 RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
1034 return 0;
1035 }
1036
1037 return 1;
1038}
1039
1040/* Shared by tls13_meth and ktls_meth */
8124ab56 1041int tls13_common_post_process_record(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec)
1853d20a 1042{
1853d20a
MC
1043 if (rec->type != SSL3_RT_APPLICATION_DATA
1044 && rec->type != SSL3_RT_ALERT
1045 && rec->type != SSL3_RT_HANDSHAKE) {
1046 RLAYERfatal(rl, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_BAD_RECORD_TYPE);
1047 return 0;
1048 }
1049
b85ebc4b
MC
1050 if (rl->msg_callback != NULL)
1051 rl->msg_callback(0, rl->version, SSL3_RT_INNER_CONTENT_TYPE, &rec->type,
1052 1, rl->cbarg);
1853d20a
MC
1053
1054 /*
1055 * TLSv1.3 alert and handshake records are required to be non-zero in
1056 * length.
1057 */
1704961c 1058 if ((rec->type == SSL3_RT_HANDSHAKE || rec->type == SSL3_RT_ALERT)
1853d20a
MC
1059 && rec->length == 0) {
1060 RLAYERfatal(rl, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_BAD_LENGTH);
1061 return 0;
1062 }
1063
1064 return 1;
1065}
1066
1704961c 1067int tls_read_record(OSSL_RECORD_LAYER *rl, void **rechandle, int *rversion,
1853d20a 1068 int *type, unsigned char **data, size_t *datalen,
8124ab56 1069 uint16_t *epoch, unsigned char *seq_num)
4030869d
MC
1070{
1071 SSL3_RECORD *rec;
1072
1073 /*
1074 * tls_get_more_records() can return success without actually reading
1075 * anything useful (i.e. if empty records are read). We loop here until
1076 * we have something useful. tls_get_more_records() will eventually fail if
1077 * too many sequential empty records are read.
1078 */
1079 while (rl->curr_rec >= rl->num_recs) {
1080 int ret;
1081
1082 if (rl->num_released != rl->num_recs) {
1083 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_RECORDS_NOT_RELEASED);
1084 return OSSL_RECORD_RETURN_FATAL;
1085 }
1086
eddb067e 1087 ret = rl->funcs->get_more_records(rl);
4030869d
MC
1088
1089 if (ret != OSSL_RECORD_RETURN_SUCCESS)
1090 return ret;
1091 }
1092
1093 /*
1094 * We have now got rl->num_recs records buffered in rl->rrec. rl->curr_rec
1095 * points to the next one to read.
1096 */
1097 rec = &rl->rrec[rl->curr_rec++];
1098
1099 *rechandle = rec;
1100 *rversion = rec->rec_version;
1101 *type = rec->type;
eddb067e 1102 *data = rec->data + rec->off;
4030869d 1103 *datalen = rec->length;
eddb067e 1104 if (rl->isdtls) {
279754d4 1105 *epoch = rec->epoch;
eddb067e
MC
1106 memcpy(seq_num, rec->seq_num, sizeof(rec->seq_num));
1107 }
4030869d
MC
1108
1109 return OSSL_RECORD_RETURN_SUCCESS;
1110}
1111
1853d20a 1112int tls_release_record(OSSL_RECORD_LAYER *rl, void *rechandle)
4030869d
MC
1113{
1114 if (!ossl_assert(rl->num_released < rl->curr_rec)
1115 || !ossl_assert(rechandle == &rl->rrec[rl->num_released])) {
1116 /* Should not happen */
1117 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_INVALID_RECORD);
1118 return OSSL_RECORD_RETURN_FATAL;
1119 }
1120
1121 rl->num_released++;
1122
3de76959
MC
1123 if (rl->curr_rec == rl->num_released
1124 && (rl->mode & SSL_MODE_RELEASE_BUFFERS) != 0
1125 && SSL3_BUFFER_get_left(&rl->rbuf) == 0)
9b7fb65e 1126 tls_release_read_buffer(rl);
3de76959 1127
4030869d
MC
1128 return OSSL_RECORD_RETURN_SUCCESS;
1129}
1130
4566dae7
MC
1131int tls_set_options(OSSL_RECORD_LAYER *rl, const OSSL_PARAM *options)
1132{
1133 const OSSL_PARAM *p;
1134
1135 p = OSSL_PARAM_locate_const(options, OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS);
1136 if (p != NULL && !OSSL_PARAM_get_uint64(p, &rl->options)) {
1137 ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
1138 return 0;
1139 }
1140
1141 p = OSSL_PARAM_locate_const(options, OSSL_LIBSSL_RECORD_LAYER_PARAM_MODE);
1142 if (p != NULL && !OSSL_PARAM_get_uint32(p, &rl->mode)) {
1143 ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
1144 return 0;
1145 }
1146
eb7d6c2a
MC
1147 if (rl->direction == OSSL_RECORD_DIRECTION_READ) {
1148 p = OSSL_PARAM_locate_const(options,
1149 OSSL_LIBSSL_RECORD_LAYER_READ_BUFFER_LEN);
1150 if (p != NULL && !OSSL_PARAM_get_size_t(p, &rl->rbuf.default_len)) {
1151 ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
1152 return 0;
1153 }
1154 } else {
1155 p = OSSL_PARAM_locate_const(options,
1156 OSSL_LIBSSL_RECORD_LAYER_PARAM_BLOCK_PADDING);
1157 if (p != NULL && !OSSL_PARAM_get_size_t(p, &rl->block_padding)) {
1158 ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
1159 return 0;
1160 }
4566dae7
MC
1161 }
1162
1163 if (rl->level == OSSL_RECORD_PROTECTION_LEVEL_APPLICATION) {
1164 /*
1165 * We ignore any read_ahead setting prior to the application protection
1166 * level. Otherwise we may read ahead data in a lower protection level
1167 * that is destined for a higher protection level. To simplify the logic
1168 * we don't support that at this stage.
1169 */
1170 p = OSSL_PARAM_locate_const(options,
1171 OSSL_LIBSSL_RECORD_LAYER_PARAM_READ_AHEAD);
1172 if (p != NULL && !OSSL_PARAM_get_int(p, &rl->read_ahead)) {
1173 ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
1174 return 0;
1175 }
1176 }
1177
1178 return 1;
1179}
1180
1853d20a 1181int
cc110a0a
MC
1182tls_int_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
1183 int role, int direction, int level, unsigned char *key,
1184 size_t keylen, unsigned char *iv, size_t ivlen,
1185 unsigned char *mackey, size_t mackeylen,
1186 const EVP_CIPHER *ciph, size_t taglen,
cc110a0a 1187 int mactype,
1e76110b 1188 const EVP_MD *md, COMP_METHOD *comp, BIO *prev,
359affde
MC
1189 BIO *transport, BIO *next, BIO_ADDR *local,
1190 BIO_ADDR *peer, const OSSL_PARAM *settings,
9dd90232
MC
1191 const OSSL_PARAM *options,
1192 const OSSL_DISPATCH *fns, void *cbarg,
8124ab56 1193 OSSL_RECORD_LAYER **retrl)
34a4068c
MC
1194{
1195 OSSL_RECORD_LAYER *rl = OPENSSL_zalloc(sizeof(*rl));
e2d5742b
MC
1196 const OSSL_PARAM *p;
1197
7c293999
MC
1198 *retrl = NULL;
1199
e077455e 1200 if (rl == NULL)
7c293999 1201 return OSSL_RECORD_RETURN_FATAL;
e2d5742b 1202
435d88d7
MC
1203 /*
1204 * Default the value for max_frag_len. This may be overridden by the
1205 * settings
1206 */
1207 rl->max_frag_len = SSL3_RT_MAX_PLAIN_LENGTH;
1208
7f2f0ac7 1209 /* Loop through all the settings since they must all be understood */
a16f9d33
MC
1210 if (settings != NULL) {
1211 for (p = settings; p->key != NULL; p++) {
1212 if (strcmp(p->key, OSSL_LIBSSL_RECORD_LAYER_PARAM_USE_ETM) == 0) {
1213 if (!OSSL_PARAM_get_int(p, &rl->use_etm)) {
7b7ad9e5 1214 ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
a16f9d33
MC
1215 goto err;
1216 }
1217 } else if (strcmp(p->key,
1218 OSSL_LIBSSL_RECORD_LAYER_PARAM_MAX_FRAG_LEN) == 0) {
1219 if (!OSSL_PARAM_get_uint(p, &rl->max_frag_len)) {
7b7ad9e5 1220 ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
a16f9d33
MC
1221 goto err;
1222 }
1223 } else if (strcmp(p->key,
1224 OSSL_LIBSSL_RECORD_LAYER_PARAM_MAX_EARLY_DATA) == 0) {
1225 if (!OSSL_PARAM_get_uint32(p, &rl->max_early_data)) {
7b7ad9e5 1226 ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
a16f9d33
MC
1227 goto err;
1228 }
1229 } else if (strcmp(p->key,
1230 OSSL_LIBSSL_RECORD_LAYER_PARAM_STREAM_MAC) == 0) {
1231 if (!OSSL_PARAM_get_int(p, &rl->stream_mac)) {
7b7ad9e5 1232 ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
a16f9d33
MC
1233 goto err;
1234 }
1704961c
MC
1235 } else if (strcmp(p->key,
1236 OSSL_LIBSSL_RECORD_LAYER_PARAM_TLSTREE) == 0) {
a16f9d33 1237 if (!OSSL_PARAM_get_int(p, &rl->tlstree)) {
7b7ad9e5 1238 ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
a16f9d33
MC
1239 goto err;
1240 }
1241 } else {
7b7ad9e5 1242 ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_MANDATORY_PARAMETER);
8124ab56
MC
1243 goto err;
1244 }
7f2f0ac7
MC
1245 }
1246 }
1247
aedbb71b
MC
1248 rl->libctx = libctx;
1249 rl->propq = propq;
1250
e2d5742b
MC
1251 rl->version = vers;
1252 rl->role = role;
1253 rl->direction = direction;
9dd90232 1254 rl->level = level;
b05fbac1
MC
1255 rl->taglen = taglen;
1256 rl->md = md;
4030869d 1257
d3192c26
MC
1258 rl->alert = SSL_AD_NO_ALERT;
1259
1853d20a 1260 if (level == OSSL_RECORD_PROTECTION_LEVEL_NONE)
4030869d
MC
1261 rl->is_first_record = 1;
1262
e2d5742b
MC
1263 if (!tls_set1_bio(rl, transport))
1264 goto err;
1265
359affde
MC
1266 if (prev != NULL && !BIO_up_ref(prev))
1267 goto err;
1268 rl->prev = prev;
1269
1270 if (next != NULL && !BIO_up_ref(next))
1271 goto err;
1272 rl->next = next;
1273
9dd90232 1274 rl->cbarg = cbarg;
a16f9d33
MC
1275 if (fns != NULL) {
1276 for (; fns->function_id != 0; fns++) {
1277 switch (fns->function_id) {
1278 case OSSL_FUNC_RLAYER_SKIP_EARLY_DATA:
1279 rl->skip_early_data = OSSL_FUNC_rlayer_skip_early_data(fns);
1280 break;
1281 case OSSL_FUNC_RLAYER_MSG_CALLBACK:
1282 rl->msg_callback = OSSL_FUNC_rlayer_msg_callback(fns);
1283 break;
1284 case OSSL_FUNC_RLAYER_SECURITY:
1285 rl->security = OSSL_FUNC_rlayer_security(fns);
1286 break;
5f95eb77
MC
1287 case OSSL_FUNC_RLAYER_PADDING:
1288 rl->padding = OSSL_FUNC_rlayer_padding(fns);
a16f9d33
MC
1289 default:
1290 /* Just ignore anything we don't understand */
1291 break;
1292 }
9dd90232
MC
1293 }
1294 }
1295
4566dae7 1296 if (!tls_set_options(rl, options)) {
7b7ad9e5 1297 ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
4566dae7
MC
1298 goto err;
1299 }
1300
b9e4e783
MC
1301 if ((rl->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) == 0
1302 && rl->version <= TLS1_VERSION
1303 && !EVP_CIPHER_is_a(ciph, "NULL")
1304 && !EVP_CIPHER_is_a(ciph, "RC4")) {
1305 /*
1306 * Enable vulnerability countermeasure for CBC ciphers with known-IV
1307 * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
1308 */
1309 rl->need_empty_fragments = 1;
1310 }
1311
7c293999
MC
1312 *retrl = rl;
1313 return OSSL_RECORD_RETURN_SUCCESS;
cc110a0a 1314 err:
359affde 1315 tls_int_free(rl);
7c293999 1316 return OSSL_RECORD_RETURN_FATAL;
cc110a0a
MC
1317}
1318
7c293999 1319static int
cc110a0a 1320tls_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
279754d4 1321 int role, int direction, int level, uint16_t epoch,
222cf410
MC
1322 unsigned char *key, size_t keylen, unsigned char *iv,
1323 size_t ivlen, unsigned char *mackey, size_t mackeylen,
cc110a0a 1324 const EVP_CIPHER *ciph, size_t taglen,
cc110a0a 1325 int mactype,
1e76110b 1326 const EVP_MD *md, COMP_METHOD *comp, BIO *prev,
359affde 1327 BIO *transport, BIO *next, BIO_ADDR *local, BIO_ADDR *peer,
cc110a0a 1328 const OSSL_PARAM *settings, const OSSL_PARAM *options,
9dd90232 1329 const OSSL_DISPATCH *fns, void *cbarg,
8124ab56 1330 OSSL_RECORD_LAYER **retrl)
cc110a0a 1331{
7c293999 1332 int ret;
1704961c 1333
7c293999
MC
1334 ret = tls_int_new_record_layer(libctx, propq, vers, role, direction, level,
1335 key, keylen, iv, ivlen, mackey, mackeylen,
359affde
MC
1336 ciph, taglen, mactype, md, comp, prev,
1337 transport, next, local, peer, settings,
8124ab56 1338 options, fns, cbarg, retrl);
cc110a0a 1339
7c293999
MC
1340 if (ret != OSSL_RECORD_RETURN_SUCCESS)
1341 return ret;
cc110a0a 1342
aedbb71b
MC
1343 switch (vers) {
1344 case TLS_ANY_VERSION:
7c293999 1345 (*retrl)->funcs = &tls_any_funcs;
aedbb71b
MC
1346 break;
1347 case TLS1_3_VERSION:
7c293999 1348 (*retrl)->funcs = &tls_1_3_funcs;
aedbb71b
MC
1349 break;
1350 case TLS1_2_VERSION:
aedbb71b 1351 case TLS1_1_VERSION:
aedbb71b 1352 case TLS1_VERSION:
7c293999 1353 (*retrl)->funcs = &tls_1_funcs;
aedbb71b
MC
1354 break;
1355 case SSL3_VERSION:
7c293999 1356 (*retrl)->funcs = &ssl_3_0_funcs;
aedbb71b
MC
1357 break;
1358 default:
1359 /* Should not happen */
7c293999
MC
1360 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
1361 ret = OSSL_RECORD_RETURN_FATAL;
aedbb71b
MC
1362 goto err;
1363 }
1364
7c293999 1365 ret = (*retrl)->funcs->set_crypto_state(*retrl, level, key, keylen, iv,
1704961c
MC
1366 ivlen, mackey, mackeylen, ciph,
1367 taglen, mactype, md, comp);
aedbb71b 1368
e2d5742b 1369 err:
7c293999
MC
1370 if (ret != OSSL_RECORD_RETURN_SUCCESS) {
1371 OPENSSL_free(*retrl);
1372 *retrl = NULL;
1373 }
1374 return ret;
e2d5742b
MC
1375}
1376
359affde 1377static void tls_int_free(OSSL_RECORD_LAYER *rl)
34a4068c 1378{
359affde 1379 BIO_free(rl->prev);
e2d5742b 1380 BIO_free(rl->bio);
359affde
MC
1381 BIO_free(rl->next);
1382 SSL3_BUFFER_release(&rl->rbuf);
1383
151f313e
MC
1384 tls_release_write_buffer(rl);
1385
6366bdd9
MC
1386 EVP_CIPHER_CTX_free(rl->enc_ctx);
1387 EVP_MD_CTX_free(rl->md_ctx);
976b263d 1388#ifndef OPENSSL_NO_COMP
9251c3c4 1389 COMP_CTX_free(rl->compctx);
976b263d 1390#endif
359affde 1391
c77d4556
MC
1392 if (rl->version == SSL3_VERSION)
1393 OPENSSL_cleanse(rl->mac_secret, sizeof(rl->mac_secret));
1394
6b5c7ef7
JC
1395 SSL3_RECORD_release(rl->rrec, SSL_MAX_PIPELINES);
1396
34a4068c
MC
1397 OPENSSL_free(rl);
1398}
1399
359affde
MC
1400int tls_free(OSSL_RECORD_LAYER *rl)
1401{
1402 SSL3_BUFFER *rbuf;
1403 size_t left, written;
1404 int ret = 1;
1405
50bed93a
MC
1406 if (rl == NULL)
1407 return 1;
1408
359affde
MC
1409 rbuf = &rl->rbuf;
1410
1411 left = SSL3_BUFFER_get_left(rbuf);
1412 if (left > 0) {
1413 /*
1414 * This record layer is closing but we still have data left in our
1415 * buffer. It must be destined for the next epoch - so push it there.
1416 */
1417 ret = BIO_write_ex(rl->next, rbuf->buf + rbuf->offset, left, &written);
1418 }
1419 tls_int_free(rl);
1420
1421 return ret;
1422}
1423
1853d20a 1424int tls_reset(OSSL_RECORD_LAYER *rl)
34a4068c
MC
1425{
1426 memset(rl, 0, sizeof(*rl));
1427 return 1;
1428}
1429
1853d20a 1430int tls_unprocessed_read_pending(OSSL_RECORD_LAYER *rl)
34a4068c 1431{
0755722c 1432 return SSL3_BUFFER_get_left(&rl->rbuf) != 0;
34a4068c
MC
1433}
1434
1853d20a 1435int tls_processed_read_pending(OSSL_RECORD_LAYER *rl)
34a4068c 1436{
4030869d 1437 return rl->curr_rec < rl->num_recs;
34a4068c
MC
1438}
1439
1853d20a 1440size_t tls_app_data_pending(OSSL_RECORD_LAYER *rl)
34a4068c 1441{
8bbf7ef6
MC
1442 size_t i;
1443 size_t num = 0;
1444
1704961c 1445 for (i = rl->curr_rec; i < rl->num_recs; i++) {
8bbf7ef6
MC
1446 if (rl->rrec[i].type != SSL3_RT_APPLICATION_DATA)
1447 return num;
1448 num += rl->rrec[i].length;
1449 }
1450 return num;
34a4068c
MC
1451}
1452
bafe524b
MC
1453size_t tls_get_max_records_default(OSSL_RECORD_LAYER *rl, int type, size_t len,
1454 size_t maxfrag, size_t *preffrag)
34a4068c 1455{
c6186792
MC
1456 /*
1457 * If we have a pipeline capable cipher, and we have been configured to use
1458 * it, then return the preferred number of pipelines.
1459 */
1460 if (rl->max_pipelines > 0
91fe8ff0 1461 && rl->enc_ctx != NULL
2c50d7fb 1462 && (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(rl->enc_ctx))
c6186792
MC
1463 & EVP_CIPH_FLAG_PIPELINE) != 0
1464 && RLAYER_USE_EXPLICIT_IV(rl)) {
1465 size_t pipes;
1466
1467 if (len == 0)
1468 return 1;
1469 pipes = ((len - 1) / *preffrag) + 1;
1470
1471 return (pipes < rl->max_pipelines) ? pipes : rl->max_pipelines;
1472 }
1473
02719d5c 1474 return 1;
34a4068c
MC
1475}
1476
bafe524b
MC
1477size_t tls_get_max_records(OSSL_RECORD_LAYER *rl, int type, size_t len,
1478 size_t maxfrag, size_t *preffrag)
23bf52a4 1479{
bafe524b 1480 return rl->funcs->get_max_records(rl, type, len, maxfrag, preffrag);
23bf52a4
MC
1481}
1482
91fe8ff0
MC
1483int tls_allocate_write_buffers_default(OSSL_RECORD_LAYER *rl,
1484 OSSL_RECORD_TEMPLATE *templates,
1485 size_t numtempl,
1486 size_t *prefix)
1487{
1488 if (!tls_setup_write_buffer(rl, numtempl, 0, 0)) {
1489 /* RLAYERfatal() already called */
1490 return 0;
1491 }
1492
1493 return 1;
1494}
1495
1496int tls_initialise_write_packets_default(OSSL_RECORD_LAYER *rl,
1497 OSSL_RECORD_TEMPLATE *templates,
1498 size_t numtempl,
1499 OSSL_RECORD_TEMPLATE *prefixtempl,
1500 WPACKET *pkt,
1501 SSL3_BUFFER *bufs,
1502 size_t *wpinited)
1503{
1504 WPACKET *thispkt;
1505 size_t j, align;
1506 SSL3_BUFFER *wb;
1507
1508 for (j = 0; j < numtempl; j++) {
1509 thispkt = &pkt[j];
1510 wb = &bufs[j];
1511
1512 wb->type = templates[j].type;
1513
1514#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0
b9e37f8f
MC
1515 align = (size_t)SSL3_BUFFER_get_buf(wb);
1516 align += rl->isdtls ? DTLS1_RT_HEADER_LENGTH : SSL3_RT_HEADER_LENGTH;
91fe8ff0
MC
1517 align = SSL3_ALIGN_PAYLOAD - 1
1518 - ((align - 1) % SSL3_ALIGN_PAYLOAD);
1519#endif
1520 SSL3_BUFFER_set_offset(wb, align);
1521
1522 if (!WPACKET_init_static_len(thispkt, SSL3_BUFFER_get_buf(wb),
1523 SSL3_BUFFER_get_len(wb), 0)) {
1524 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1525 return 0;
1526 }
1527 (*wpinited)++;
1528 if (!WPACKET_allocate_bytes(thispkt, align, NULL)) {
1529 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1530 return 0;
1531 }
1532 }
1533
1534 return 1;
1535}
1536
aca70ca8
MC
1537int tls_prepare_record_header_default(OSSL_RECORD_LAYER *rl,
1538 WPACKET *thispkt,
1539 OSSL_RECORD_TEMPLATE *templ,
1540 unsigned int rectype,
1541 unsigned char **recdata)
1542{
1543 size_t maxcomplen;
1544
1545 *recdata = NULL;
1546
1547 maxcomplen = templ->buflen;
1548 if (rl->compctx != NULL)
1549 maxcomplen += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
1550
1551 if (!WPACKET_put_bytes_u8(thispkt, rectype)
1552 || !WPACKET_put_bytes_u16(thispkt, templ->version)
1553 || !WPACKET_start_sub_packet_u16(thispkt)
1554 || (rl->eivlen > 0
1555 && !WPACKET_allocate_bytes(thispkt, rl->eivlen, NULL))
1556 || (maxcomplen > 0
1557 && !WPACKET_reserve_bytes(thispkt, maxcomplen,
1558 recdata))) {
1559 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1560 return 0;
1561 }
1562
1563 return 1;
1564}
1565
757ef3ba
MC
1566int tls_prepare_for_encryption_default(OSSL_RECORD_LAYER *rl,
1567 size_t mac_size,
1568 WPACKET *thispkt,
1569 SSL3_RECORD *thiswr)
1570{
1571 size_t len;
1572 unsigned char *recordstart;
1573
1574 /*
1575 * we should still have the output to thiswr->data and the input from
1576 * wr->input. Length should be thiswr->length. thiswr->data still points
1577 * in the wb->buf
1578 */
1579
1580 if (!rl->use_etm && mac_size != 0) {
1581 unsigned char *mac;
1582
1583 if (!WPACKET_allocate_bytes(thispkt, mac_size, &mac)
1584 || !rl->funcs->mac(rl, thiswr, mac, 1)) {
1585 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1586 return 0;
1587 }
1588 }
1589
ecacbc5e
MC
1590 /*
1591 * Reserve some bytes for any growth that may occur during encryption. If
1592 * we are adding the MAC independently of the cipher algorithm, then the
1593 * max encrypted overhead does not need to include an allocation for that
1594 * MAC
1595 */
1596 if (!WPACKET_reserve_bytes(thispkt, SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD
1597 - mac_size, NULL)
757ef3ba
MC
1598 /*
1599 * We also need next the amount of bytes written to this
1600 * sub-packet
1601 */
1602 || !WPACKET_get_length(thispkt, &len)) {
1603 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1604 return 0;
1605 }
1606
1607 /* Get a pointer to the start of this record excluding header */
1608 recordstart = WPACKET_get_curr(thispkt) - len;
1609 SSL3_RECORD_set_data(thiswr, recordstart);
1610 SSL3_RECORD_reset_input(thiswr);
1611 SSL3_RECORD_set_length(thiswr, len);
1612
1613 return 1;
1614}
1615
2a354d54
MC
1616int tls_post_encryption_processing_default(OSSL_RECORD_LAYER *rl,
1617 size_t mac_size,
1618 OSSL_RECORD_TEMPLATE *thistempl,
1619 WPACKET *thispkt,
1620 SSL3_RECORD *thiswr)
1621{
1622 size_t origlen, len;
b9e37f8f
MC
1623 size_t headerlen = rl->isdtls ? DTLS1_RT_HEADER_LENGTH
1624 : SSL3_RT_HEADER_LENGTH;
2a354d54
MC
1625
1626 /* Allocate bytes for the encryption overhead */
1627 if (!WPACKET_get_length(thispkt, &origlen)
830eae60 1628 /* Check we allowed enough room for the encryption growth */
ecacbc5e
MC
1629 || !ossl_assert(origlen + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD
1630 - mac_size >= thiswr->length)
2a354d54
MC
1631 /* Encryption should never shrink the data! */
1632 || origlen > thiswr->length
1633 || (thiswr->length > origlen
1634 && !WPACKET_allocate_bytes(thispkt,
1635 thiswr->length - origlen,
1636 NULL))) {
1637 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1638 return 0;
1639 }
1640 if (rl->use_etm && mac_size != 0) {
1641 unsigned char *mac;
1642
1643 if (!WPACKET_allocate_bytes(thispkt, mac_size, &mac)
1644 || !rl->funcs->mac(rl, thiswr, mac, 1)) {
1645 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1646 return 0;
1647 }
1648
1649 SSL3_RECORD_add_length(thiswr, mac_size);
1650 }
1651
1652 if (!WPACKET_get_length(thispkt, &len)
1653 || !WPACKET_close(thispkt)) {
1654 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1655 return 0;
1656 }
1657
1658 if (rl->msg_callback != NULL) {
1659 unsigned char *recordstart;
1660
b9e37f8f 1661 recordstart = WPACKET_get_curr(thispkt) - len - headerlen;
2a354d54 1662 rl->msg_callback(1, thiswr->rec_version, SSL3_RT_HEADER, recordstart,
b9e37f8f 1663 headerlen, rl->cbarg);
2a354d54
MC
1664
1665 if (rl->version == TLS1_3_VERSION && rl->enc_ctx != NULL) {
1666 unsigned char ctype = thistempl->type;
1667
1668 rl->msg_callback(1, thiswr->rec_version, SSL3_RT_INNER_CONTENT_TYPE,
1669 &ctype, 1, rl->cbarg);
1670 }
1671 }
1672
1673 if (!WPACKET_finish(thispkt)) {
1674 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1675 return 0;
1676 }
1677
b9e37f8f 1678 SSL3_RECORD_add_length(thiswr, headerlen);
2a354d54
MC
1679
1680 return 1;
1681}
1682
bafe524b
MC
1683int tls_write_records_default(OSSL_RECORD_LAYER *rl,
1684 OSSL_RECORD_TEMPLATE *templates,
1685 size_t numtempl)
34a4068c 1686{
2b71b042
MC
1687 WPACKET pkt[SSL_MAX_PIPELINES + 1];
1688 SSL3_RECORD wr[SSL_MAX_PIPELINES + 1];
1689 WPACKET *thispkt;
1690 SSL3_RECORD *thiswr;
9251c3c4 1691 int mac_size = 0, ret = 0;
2a354d54 1692 size_t wpinited = 0;
2b71b042 1693 size_t j, prefix = 0;
2b71b042
MC
1694 OSSL_RECORD_TEMPLATE prefixtempl;
1695 OSSL_RECORD_TEMPLATE *thistempl;
1696
bfda3aee
MC
1697 if (rl->md_ctx != NULL && EVP_MD_CTX_get0_md(rl->md_ctx) != NULL) {
1698 mac_size = EVP_MD_CTX_get_size(rl->md_ctx);
1699 if (mac_size < 0) {
1700 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1701 goto err;
2b71b042
MC
1702 }
1703 }
bfda3aee 1704
91fe8ff0
MC
1705 if (!rl->funcs->allocate_write_buffers(rl, templates, numtempl, &prefix)) {
1706 /* RLAYERfatal() already called */
1707 goto err;
1708 }
2b71b042 1709
91fe8ff0
MC
1710 if (!rl->funcs->initialise_write_packets(rl, templates, numtempl,
1711 &prefixtempl, pkt, rl->wbuf,
1712 &wpinited)) {
23bf52a4
MC
1713 /* RLAYERfatal() already called */
1714 goto err;
2b71b042
MC
1715 }
1716
2b71b042
MC
1717 /* Clear our SSL3_RECORD structures */
1718 memset(wr, 0, sizeof(wr));
1719 for (j = 0; j < numtempl + prefix; j++) {
2b71b042 1720 unsigned char *compressdata = NULL;
2b71b042
MC
1721 unsigned int rectype;
1722
1723 thispkt = &pkt[j];
1724 thiswr = &wr[j];
91fe8ff0 1725 thistempl = (j < prefix) ? &prefixtempl : &templates[j - prefix];
2b71b042
MC
1726
1727 /*
7ca61d63
MC
1728 * Default to the record type as specified in the template unless the
1729 * protocol implementation says differently.
2b71b042 1730 */
7ca61d63
MC
1731 if (rl->funcs->get_record_type != NULL)
1732 rectype = rl->funcs->get_record_type(rl, thistempl);
2b71b042
MC
1733 else
1734 rectype = thistempl->type;
1735
1736 SSL3_RECORD_set_type(thiswr, rectype);
1d367677 1737 SSL3_RECORD_set_rec_version(thiswr, thistempl->version);
2b71b042 1738
aca70ca8
MC
1739 if (!rl->funcs->prepare_record_header(rl, thispkt, thistempl, rectype,
1740 &compressdata)) {
2582de25 1741 /* RLAYERfatal() already called */
2b71b042
MC
1742 goto err;
1743 }
1744
1745 /* lets setup the record stuff. */
1746 SSL3_RECORD_set_data(thiswr, compressdata);
1747 SSL3_RECORD_set_length(thiswr, thistempl->buflen);
4fed6ee1 1748
2b71b042 1749 SSL3_RECORD_set_input(thiswr, (unsigned char *)thistempl->buf);
2b71b042
MC
1750
1751 /*
1752 * we now 'read' from thiswr->input, thiswr->length bytes into
1753 * thiswr->data
1754 */
1755
1756 /* first we compress */
9251c3c4
MC
1757 if (rl->compctx != NULL) {
1758 if (!tls_do_compress(rl, thiswr)
2b71b042 1759 || !WPACKET_allocate_bytes(thispkt, thiswr->length, NULL)) {
320145d5 1760 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_COMPRESSION_FAILURE);
2b71b042
MC
1761 goto err;
1762 }
aca70ca8
MC
1763 } else if (compressdata != NULL) {
1764 if (!WPACKET_memcpy(thispkt, thiswr->input, thiswr->length)) {
1765 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1766 goto err;
2b71b042 1767 }
aca70ca8 1768 SSL3_RECORD_reset_input(&wr[j]);
2b71b042
MC
1769 }
1770
2582de25
MC
1771 if (rl->funcs->add_record_padding != NULL
1772 && !rl->funcs->add_record_padding(rl, thistempl, thispkt,
1773 thiswr)) {
1774 /* RLAYERfatal() already called */
1775 goto err;
2b71b042
MC
1776 }
1777
757ef3ba
MC
1778 if (!rl->funcs->prepare_for_encryption(rl, mac_size, thispkt, thiswr)) {
1779 /* RLAYERfatal() already called */
1780 goto err;
2b71b042
MC
1781 }
1782 }
1783
b6f7519b
MC
1784 if (prefix) {
1785 if (rl->funcs->cipher(rl, wr, 1, 1, NULL, mac_size) < 1) {
4bf610bd 1786 if (rl->alert == SSL_AD_NO_ALERT) {
2f6e24eb
MC
1787 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1788 }
1789 goto err;
1790 }
2b71b042
MC
1791 }
1792
b6f7519b
MC
1793 if (rl->funcs->cipher(rl, wr + prefix, numtempl, 1, NULL, mac_size) < 1) {
1794 if (rl->alert == SSL_AD_NO_ALERT) {
1795 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1796 }
1797 goto err;
1798 }
1799
91fe8ff0 1800 for (j = 0; j < numtempl + prefix; j++) {
2b71b042
MC
1801 thispkt = &pkt[j];
1802 thiswr = &wr[j];
91fe8ff0 1803 thistempl = (j < prefix) ? &prefixtempl : &templates[j - prefix];
2b71b042 1804
2a354d54
MC
1805 if (!rl->funcs->post_encryption_processing(rl, mac_size, thistempl,
1806 thispkt, thiswr)) {
1807 /* RLAYERfatal() already called */
2b71b042
MC
1808 goto err;
1809 }
1810
2b71b042 1811 /* now let's set up wb */
151f313e 1812 SSL3_BUFFER_set_left(&rl->wbuf[j], SSL3_RECORD_get_length(thiswr));
2b71b042
MC
1813 }
1814
23bf52a4 1815 ret = 1;
2b71b042
MC
1816 err:
1817 for (j = 0; j < wpinited; j++)
1818 WPACKET_cleanup(&pkt[j]);
23bf52a4
MC
1819 return ret;
1820}
1821
1822int tls_write_records(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
1823 size_t numtempl)
1824{
23bf52a4
MC
1825 /* Check we don't have pending data waiting to write */
1826 if (!ossl_assert(rl->nextwbuf >= rl->numwpipes
1827 || SSL3_BUFFER_get_left(&rl->wbuf[rl->nextwbuf]) == 0)) {
1828 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1829 return OSSL_RECORD_RETURN_FATAL;
1830 }
1831
bafe524b 1832 if (!rl->funcs->write_records(rl, templates, numtempl)) {
23bf52a4
MC
1833 /* RLAYERfatal already called */
1834 return OSSL_RECORD_RETURN_FATAL;
1835 }
23bf52a4
MC
1836
1837 rl->nextwbuf = 0;
1838 /* we now just need to write the buffers */
1839 return tls_retry_write_records(rl);
34a4068c
MC
1840}
1841
2b71b042 1842int tls_retry_write_records(OSSL_RECORD_LAYER *rl)
34a4068c 1843{
320145d5 1844 int i, ret;
2b71b042 1845 SSL3_BUFFER *thiswb;
2b71b042 1846 size_t tmpwrit = 0;
2b71b042 1847
e7694c69 1848 if (rl->nextwbuf >= rl->numwpipes)
320145d5 1849 return OSSL_RECORD_RETURN_SUCCESS;
151f313e 1850
2b71b042 1851 for (;;) {
151f313e
MC
1852 thiswb = &rl->wbuf[rl->nextwbuf];
1853
2b71b042 1854 clear_sys_error();
b5cf81f7 1855 if (rl->bio != NULL) {
ace38195
MC
1856 if (rl->funcs->prepare_write_bio != NULL) {
1857 ret = rl->funcs->prepare_write_bio(rl, thiswb->type);
1858 if (ret != OSSL_RECORD_RETURN_SUCCESS)
320145d5 1859 return ret;
2b71b042 1860 }
b5cf81f7 1861 i = BIO_write(rl->bio, (char *)
2b71b042
MC
1862 &(SSL3_BUFFER_get_buf(thiswb)
1863 [SSL3_BUFFER_get_offset(thiswb)]),
1864 (unsigned int)SSL3_BUFFER_get_left(thiswb));
320145d5 1865 if (i >= 0) {
2b71b042 1866 tmpwrit = i;
320145d5
MC
1867 if (i == 0 && BIO_should_retry(rl->bio))
1868 ret = OSSL_RECORD_RETURN_RETRY;
1869 else
1870 ret = OSSL_RECORD_RETURN_SUCCESS;
1871 } else {
1872 if (BIO_should_retry(rl->bio))
1873 ret = OSSL_RECORD_RETURN_RETRY;
1874 else
1875 ret = OSSL_RECORD_RETURN_FATAL;
1876 }
2b71b042 1877 } else {
320145d5
MC
1878 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_BIO_NOT_SET);
1879 ret = OSSL_RECORD_RETURN_FATAL;
2b71b042
MC
1880 i = -1;
1881 }
1882
1883 /*
1884 * When an empty fragment is sent on a connection using KTLS,
1885 * it is sent as a write of zero bytes. If this zero byte
1886 * write succeeds, i will be 0 rather than a non-zero value.
1887 * Treat i == 0 as success rather than an error for zero byte
1888 * writes to permit this case.
1889 */
1890 if (i >= 0 && tmpwrit == SSL3_BUFFER_get_left(thiswb)) {
1891 SSL3_BUFFER_set_left(thiswb, 0);
1892 SSL3_BUFFER_add_offset(thiswb, tmpwrit);
e7694c69 1893 if (++(rl->nextwbuf) < rl->numwpipes)
2b71b042 1894 continue;
151f313e 1895
e7694c69 1896 if (rl->nextwbuf == rl->numwpipes
151f313e
MC
1897 && (rl->mode & SSL_MODE_RELEASE_BUFFERS) != 0)
1898 tls_release_write_buffer(rl);
320145d5 1899 return OSSL_RECORD_RETURN_SUCCESS;
2b71b042 1900 } else if (i <= 0) {
5361a5a9 1901 if (rl->isdtls) {
2b71b042
MC
1902 /*
1903 * For DTLS, just drop it. That's kind of the whole point in
1904 * using a datagram service
1905 */
1906 SSL3_BUFFER_set_left(thiswb, 0);
e7694c69 1907 if (++(rl->nextwbuf) == rl->numwpipes
151f313e
MC
1908 && (rl->mode & SSL_MODE_RELEASE_BUFFERS) != 0)
1909 tls_release_write_buffer(rl);
1910
2b71b042 1911 }
320145d5 1912 return ret;
2b71b042
MC
1913 }
1914 SSL3_BUFFER_add_offset(thiswb, tmpwrit);
1915 SSL3_BUFFER_sub_left(thiswb, tmpwrit);
1916 }
34a4068c
MC
1917}
1918
1853d20a 1919int tls_get_alert_code(OSSL_RECORD_LAYER *rl)
e2d5742b
MC
1920{
1921 return rl->alert;
1922}
1923
1853d20a 1924int tls_set1_bio(OSSL_RECORD_LAYER *rl, BIO *bio)
e2d5742b
MC
1925{
1926 if (bio != NULL && !BIO_up_ref(bio))
1927 return 0;
1928 BIO_free(rl->bio);
1929 rl->bio = bio;
1930
1931 return 1;
1932}
1933
1853d20a
MC
1934/* Shared by most methods except tlsany_meth */
1935int tls_default_set_protocol_version(OSSL_RECORD_LAYER *rl, int version)
1936{
1937 if (rl->version != version)
1938 return 0;
1939
1940 return 1;
1941}
1942
1943int tls_set_protocol_version(OSSL_RECORD_LAYER *rl, int version)
1944{
1945 return rl->funcs->set_protocol_version(rl, version);
1946}
1947
1948void tls_set_plain_alerts(OSSL_RECORD_LAYER *rl, int allow)
1949{
1950 rl->allow_plain_alerts = allow;
1951}
1952
1953void tls_set_first_handshake(OSSL_RECORD_LAYER *rl, int first)
1954{
1955 rl->is_first_handshake = first;
1956}
1957
8124ab56
MC
1958void tls_set_max_pipelines(OSSL_RECORD_LAYER *rl, size_t max_pipelines)
1959{
1960 rl->max_pipelines = max_pipelines;
1961 if (max_pipelines > 1)
1962 rl->read_ahead = 1;
1963}
1964
d0b17ea0
MC
1965void tls_get_state(OSSL_RECORD_LAYER *rl, const char **shortstr,
1966 const char **longstr)
1967{
1968 const char *shrt, *lng;
1704961c 1969
d0b17ea0
MC
1970 switch (rl->rstate) {
1971 case SSL_ST_READ_HEADER:
1972 shrt = "RH";
1973 lng = "read header";
1974 break;
1975 case SSL_ST_READ_BODY:
1976 shrt = "RB";
1977 lng = "read body";
1978 break;
1979 default:
1980 shrt = lng = "unknown";
1981 break;
1982 }
1983 if (shortstr != NULL)
1984 *shortstr = shrt;
1985 if (longstr != NULL)
1986 *longstr = lng;
1987}
1988
1e76110b
MC
1989const COMP_METHOD *tls_get_compression(OSSL_RECORD_LAYER *rl)
1990{
1991#ifndef OPENSSL_NO_COMP
1992 return (rl->compctx == NULL) ? NULL : COMP_CTX_get_method(rl->compctx);
1993#else
1994 return NULL;
1995#endif
1996}
1997
435d88d7
MC
1998void tls_set_max_frag_len(OSSL_RECORD_LAYER *rl, size_t max_frag_len)
1999{
2000 rl->max_frag_len = max_frag_len;
2001 /*
2002 * We don't need to adjust buffer sizes. Write buffer sizes are
2003 * automatically checked anyway. We should only be changing the read buffer
2004 * size during the handshake, so we will create a new buffer when we create
2005 * the new record layer. We can't change the existing buffer because it may
2006 * already have data in it.
2007 */
2008}
2009
b92fc4ae
MC
2010int tls_increment_sequence_ctr(OSSL_RECORD_LAYER *rl)
2011{
2012 int i;
2013
2014 /* Increment the sequence counter */
2015 for (i = SEQ_NUM_SIZE; i > 0; i--) {
2016 ++(rl->sequence[i - 1]);
2017 if (rl->sequence[i - 1] != 0)
2018 break;
2019 }
2020 if (i == 0) {
2021 /* Sequence has wrapped */
2022 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_SEQUENCE_CTR_WRAPPED);
2023 return 0;
2024 }
2025 return 1;
2026}
2027
7eb39ecb
MC
2028int tls_alloc_buffers(OSSL_RECORD_LAYER *rl)
2029{
2030 if (rl->direction == OSSL_RECORD_DIRECTION_WRITE) {
2031 /* If we have a pending write then buffers are already allocated */
2032 if (rl->nextwbuf < rl->numwpipes)
2033 return 1;
2034 /*
2035 * We assume 1 pipe with default sized buffer. If what we need ends up
2036 * being a different size to that then it will be reallocated on demand.
2037 * If we need more than 1 pipe then that will also be allocated on
2038 * demand
2039 */
2040 if (!tls_setup_write_buffer(rl, 1, 0, 0))
2041 return 0;
2042
2043 /*
2044 * Normally when we allocate write buffers we immediately write
2045 * something into it. In this case we're not doing that so mark the
2046 * buffer as empty.
2047 */
2048 SSL3_BUFFER_set_left(&rl->wbuf[0], 0);
2049 return 1;
2050 }
2051
2052 /* Read direction */
2053
2054 /* If we have pending data to be read then buffers are already allocated */
2055 if (rl->curr_rec < rl->num_recs || SSL3_BUFFER_get_left(&rl->rbuf) != 0)
2056 return 1;
2057 return tls_setup_read_buffer(rl);
2058}
2059
2060int tls_free_buffers(OSSL_RECORD_LAYER *rl)
2061{
2062 if (rl->direction == OSSL_RECORD_DIRECTION_WRITE) {
2063 if (rl->nextwbuf < rl->numwpipes) {
2064 /*
2065 * We may have pending data. If we've just got one empty buffer
2066 * allocated then it has probably just been alloc'd via
2067 * tls_alloc_buffers, and it is fine to free it. Otherwise this
2068 * looks like real pending data and it is an error.
2069 */
2070 if (rl->nextwbuf != 0
2071 || rl->numwpipes != 1
2072 || SSL3_BUFFER_get_left(&rl->wbuf[0]) != 0)
2073 return 0;
2074 }
2075 tls_release_write_buffer(rl);
2076 return 1;
2077 }
2078
2079 /* Read direction */
2080
2081 /* If we have pending data to be read then fail */
2082 if (rl->curr_rec < rl->num_recs || SSL3_BUFFER_get_left(&rl->rbuf) != 0)
2083 return 0;
2084
2085 return tls_release_read_buffer(rl);
2086}
2087
34a4068c
MC
2088const OSSL_RECORD_METHOD ossl_tls_record_method = {
2089 tls_new_record_layer,
2090 tls_free,
2091 tls_reset,
2092 tls_unprocessed_read_pending,
2093 tls_processed_read_pending,
2094 tls_app_data_pending,
34a4068c 2095 tls_get_max_records,
2b71b042
MC
2096 tls_write_records,
2097 tls_retry_write_records,
34a4068c 2098 tls_read_record,
e2d5742b
MC
2099 tls_release_record,
2100 tls_get_alert_code,
2101 tls_set1_bio,
1853d20a
MC
2102 tls_set_protocol_version,
2103 tls_set_plain_alerts,
2104 tls_set_first_handshake,
8124ab56 2105 tls_set_max_pipelines,
d0b17ea0 2106 NULL,
4566dae7 2107 tls_get_state,
1e76110b 2108 tls_set_options,
435d88d7 2109 tls_get_compression,
4f428e86 2110 tls_set_max_frag_len,
b92fc4ae 2111 NULL,
7eb39ecb
MC
2112 tls_increment_sequence_ctr,
2113 tls_alloc_buffers,
2114 tls_free_buffers
cc110a0a 2115};