fa6551dffb5385c579cef58b159bdf5e484a78d0
[openssl.git] / ssl / record / methods / tls_common.c
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
10 #include <openssl/bio.h>
11 #include <openssl/ssl.h>
12 #include <openssl/err.h>
13 #include <openssl/core_names.h>
14 #include "internal/e_os.h"
15 #include "internal/packet.h"
16 #include "../../ssl_local.h"
17 #include "../record_local.h"
18 #include "recmethod_local.h"
19
20 # define SSL_AD_NO_ALERT    -1
21
22 void ossl_rlayer_fatal(OSSL_RECORD_LAYER *rl, int al, int reason,
23                        const char *fmt, ...)
24 {
25     va_list args;
26
27     va_start(args, fmt);
28     ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
29     va_end(args);
30
31     rl->alert = al;
32 }
33
34 int ossl_set_tls_provider_parameters(OSSL_RECORD_LAYER *rl,
35                                      EVP_CIPHER_CTX *ctx,
36                                      const EVP_CIPHER *ciph,
37                                      const EVP_MD *md,
38                                      SSL_CONNECTION *s)
39 {
40     /*
41      * Provided cipher, the TLS padding/MAC removal is performed provider
42      * side so we need to tell the ctx about our TLS version and mac size
43      */
44     OSSL_PARAM params[3], *pprm = params;
45     size_t macsize = 0;
46     int imacsize = -1;
47
48     if ((EVP_CIPHER_get_flags(ciph) & EVP_CIPH_FLAG_AEAD_CIPHER) == 0
49                /*
50                 * We look at s->ext.use_etm instead of SSL_READ_ETM() or
51                 * SSL_WRITE_ETM() because this test applies to both reading
52                 * and writing.
53                 */
54             && !s->ext.use_etm)
55         imacsize = EVP_MD_get_size(md);
56     if (imacsize >= 0)
57         macsize = (size_t)imacsize;
58
59     *pprm++ = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS_VERSION,
60                                        &rl->version);
61     *pprm++ = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_TLS_MAC_SIZE,
62                                           &macsize);
63     *pprm = OSSL_PARAM_construct_end();
64
65     if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
66         ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
67         return 0;
68     }
69
70     return 1;
71 }
72
73 /*
74  * ssl3_cbc_record_digest_supported returns 1 iff |ctx| uses a hash function
75  * which ssl3_cbc_digest_record supports.
76  */
77 char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx)
78 {
79     switch (EVP_MD_CTX_get_type(ctx)) {
80     case NID_md5:
81     case NID_sha1:
82     case NID_sha224:
83     case NID_sha256:
84     case NID_sha384:
85     case NID_sha512:
86         return 1;
87     default:
88         return 0;
89     }
90 }
91
92 static int rlayer_allow_compression(OSSL_RECORD_LAYER *rl)
93 {
94     if (rl->options & SSL_OP_NO_COMPRESSION)
95         return 0;
96 #if 0
97     /* TODO(RECLAYER): Implement ssl_security inside the record layer */
98     return ssl_security(s, SSL_SECOP_COMPRESSION, 0, 0, NULL);
99 #else
100     return 1;
101 #endif
102 }
103
104 static int rlayer_setup_read_buffer(OSSL_RECORD_LAYER *rl)
105 {
106     unsigned char *p;
107     size_t len, align = 0, headerlen;
108     SSL3_BUFFER *b;
109
110     b = &rl->rbuf;
111
112     if (rl->isdtls)
113         headerlen = DTLS1_RT_HEADER_LENGTH;
114     else
115         headerlen = SSL3_RT_HEADER_LENGTH;
116
117 #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
118     align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1);
119 #endif
120
121     if (b->buf == NULL) {
122         len = SSL3_RT_MAX_PLAIN_LENGTH
123             + SSL3_RT_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
124 #ifndef OPENSSL_NO_COMP
125         if (rlayer_allow_compression(rl))
126             len += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
127 #endif
128         if (b->default_len > len)
129             len = b->default_len;
130         if ((p = OPENSSL_malloc(len)) == NULL) {
131             /*
132              * We've got a malloc failure, and we're still initialising buffers.
133              * We assume we're so doomed that we won't even be able to send an
134              * alert.
135              */
136             RLAYERfatal(rl, SSL_AD_NO_ALERT, ERR_R_MALLOC_FAILURE);
137             return 0;
138         }
139         b->buf = p;
140         b->len = len;
141     }
142
143     return 1;
144 }
145
146 static int rlayer_release_read_buffer(OSSL_RECORD_LAYER *rl)
147 {
148     SSL3_BUFFER *b;
149
150     b = &rl->rbuf;
151     if (rl->options & SSL_OP_CLEANSE_PLAINTEXT)
152         OPENSSL_cleanse(b->buf, b->len);
153     OPENSSL_free(b->buf);
154     b->buf = NULL;
155     return 1;
156 }
157
158 void tls_reset_packet_length(OSSL_RECORD_LAYER *rl)
159 {
160     rl->packet_length = 0;
161 }
162
163 /*
164  * Return values are as per SSL_read()
165  */
166 int tls_default_read_n(OSSL_RECORD_LAYER *rl, size_t n, size_t max, int extend,
167                        int clearold, size_t *readbytes)
168 {
169     /*
170      * If extend == 0, obtain new n-byte packet; if extend == 1, increase
171      * packet by another n bytes. The packet will be in the sub-array of
172      * s->rlayer.rbuf.buf specified by s->rlayer.packet and
173      * s->rlayer.packet_length. (If s->rlayer.read_ahead is set, 'max' bytes may
174      * be stored in rbuf [plus s->rlayer.packet_length bytes if extend == 1].)
175      * if clearold == 1, move the packet to the start of the buffer; if
176      * clearold == 0 then leave any old packets where they were
177      */
178     size_t len, left, align = 0;
179     unsigned char *pkt;
180     SSL3_BUFFER *rb;
181
182     if (n == 0)
183         return OSSL_RECORD_RETURN_NON_FATAL_ERR;
184
185     rb = &rl->rbuf;
186     /*
187      * TODO(RECLAYER): Once this function is only called from inside the rlayer
188      * directly, we can probably remove this since it is initialised in
189      * tls_get_more_records
190      */
191     if (rb->buf == NULL) {
192         if (!rlayer_setup_read_buffer(rl)) {
193             /* RLAYERfatal() already called */
194             return OSSL_RECORD_RETURN_FATAL;
195         }
196     }
197
198     left = rb->left;
199 #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0
200     align = (size_t)rb->buf + SSL3_RT_HEADER_LENGTH;
201     align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD);
202 #endif
203
204     if (!extend) {
205         /* start with empty packet ... */
206         if (left == 0) {
207             rb->offset = align;
208         } else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH) {
209             /*
210              * check if next packet length is large enough to justify payload
211              * alignment...
212              */
213             pkt = rb->buf + rb->offset;
214             if (pkt[0] == SSL3_RT_APPLICATION_DATA
215                     && (pkt[3] << 8 | pkt[4]) >= 128) {
216                 /*
217                  * Note that even if packet is corrupted and its length field
218                  * is insane, we can only be led to wrong decision about
219                  * whether memmove will occur or not. Header values has no
220                  * effect on memmove arguments and therefore no buffer
221                  * overrun can be triggered.
222                  */
223                 memmove(rb->buf + align, pkt, left);
224                 rb->offset = align;
225             }
226         }
227         rl->packet = rb->buf + rb->offset;
228         rl->packet_length = 0;
229         /* ... now we can act as if 'extend' was set */
230     }
231
232     len = rl->packet_length;
233     pkt = rb->buf + align;
234     /*
235      * Move any available bytes to front of buffer: 'len' bytes already
236      * pointed to by 'packet', 'left' extra ones at the end
237      */
238     if (rl->packet != pkt && clearold == 1) {
239         memmove(pkt, rl->packet, len + left);
240         rl->packet = pkt;
241         rb->offset = len + align;
242     }
243
244     /*
245      * For DTLS/UDP reads should not span multiple packets because the read
246      * operation returns the whole packet at once (as long as it fits into
247      * the buffer).
248      */
249     if (rl->isdtls) {
250         if (left == 0 && extend)
251             return 0;
252         if (left > 0 && n > left)
253             n = left;
254     }
255
256     /* if there is enough in the buffer from a previous read, take some */
257     if (left >= n) {
258         rl->packet_length += n;
259         rb->left = left - n;
260         rb->offset += n;
261         *readbytes = n;
262         return OSSL_RECORD_RETURN_SUCCESS;
263     }
264
265     /* else we need to read more data */
266
267     if (n > rb->len - rb->offset) {
268         /* does not happen */
269         RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
270         return OSSL_RECORD_RETURN_FATAL;
271     }
272
273     /* We always act like read_ahead is set for DTLS */
274     if (!rl->read_ahead && !rl->isdtls) {
275         /* ignore max parameter */
276         max = n;
277     } else {
278         if (max < n)
279             max = n;
280         if (max > rb->len - rb->offset)
281             max = rb->len - rb->offset;
282     }
283
284     while (left < n) {
285         size_t bioread = 0;
286         int ret;
287
288         /*
289          * Now we have len+left bytes at the front of s->s3.rbuf.buf and
290          * need to read in more until we have len+n (up to len+max if
291          * possible)
292          */
293
294         clear_sys_error();
295         if (rl->bio != NULL) {
296             ret = BIO_read(rl->bio, pkt + len + left, max - left);
297             if (ret > 0) {
298                 bioread = ret;
299                 ret = OSSL_RECORD_RETURN_SUCCESS;
300             } else if (BIO_should_retry(rl->bio)) {
301                 ret = OSSL_RECORD_RETURN_RETRY;
302             } else if (BIO_eof(rl->bio)) {
303                 ret = OSSL_RECORD_RETURN_EOF;
304             } else {
305                 ret = OSSL_RECORD_RETURN_FATAL;
306             }
307         } else {
308             RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_READ_BIO_NOT_SET);
309             ret = OSSL_RECORD_RETURN_FATAL;
310         }
311
312         if (ret <= OSSL_RECORD_RETURN_RETRY) {
313             rb->left = left;
314             if (rl->mode & SSL_MODE_RELEASE_BUFFERS && !rl->isdtls)
315                 if (len + left == 0)
316                     rlayer_release_read_buffer(rl);
317             return ret;
318         }
319         left += bioread;
320         /*
321          * reads should *never* span multiple packets for DTLS because the
322          * underlying transport protocol is message oriented as opposed to
323          * byte oriented as in the TLS case.
324          */
325         if (rl->isdtls) {
326             if (n > left)
327                 n = left;       /* makes the while condition false */
328         }
329     }
330
331     /* done reading, now the book-keeping */
332     rb->offset += n;
333     rb->left = left - n;
334     rl->packet_length += n;
335     *readbytes = n;
336     return OSSL_RECORD_RETURN_SUCCESS;
337 }
338
339 /*
340  * Peeks ahead into "read_ahead" data to see if we have a whole record waiting
341  * for us in the buffer.
342  */
343 static int tls_record_app_data_waiting(OSSL_RECORD_LAYER *rl)
344 {
345     SSL3_BUFFER *rbuf;
346     size_t left, len;
347     unsigned char *p;
348
349     rbuf = &rl->rbuf;
350
351     p = SSL3_BUFFER_get_buf(rbuf);
352     if (p == NULL)
353         return 0;
354
355     left = SSL3_BUFFER_get_left(rbuf);
356
357     if (left < SSL3_RT_HEADER_LENGTH)
358         return 0;
359
360     p += SSL3_BUFFER_get_offset(rbuf);
361
362     /*
363      * We only check the type and record length, we will sanity check version
364      * etc later
365      */
366     if (*p != SSL3_RT_APPLICATION_DATA)
367         return 0;
368
369     p += 3;
370     n2s(p, len);
371
372     if (left < SSL3_RT_HEADER_LENGTH + len)
373         return 0;
374
375     return 1;
376 }
377
378 /*
379  * MAX_EMPTY_RECORDS defines the number of consecutive, empty records that
380  * will be processed per call to ssl3_get_record. Without this limit an
381  * attacker could send empty records at a faster rate than we can process and
382  * cause ssl3_get_record to loop forever.
383  */
384 #define MAX_EMPTY_RECORDS 32
385
386 #define SSL2_RT_HEADER_LENGTH   2
387
388 /*-
389  * Call this to buffer new input records in rl->rrec.
390  * It will return a OSSL_RECORD_RETURN_* value.
391  * When it finishes successfully (OSSL_RECORD_RETURN_SUCCESS), |rl->num_recs|
392  * records have been decoded. For each record 'i':
393  * rrec[i].type    - is the type of record
394  * rrec[i].data,   - data
395  * rrec[i].length, - number of bytes
396  * Multiple records will only be returned if the record types are all
397  * SSL3_RT_APPLICATION_DATA. The number of records returned will always be <=
398  * |max_pipelines|
399  */
400 static int tls_get_more_records(OSSL_RECORD_LAYER *rl, 
401                                 /* TODO(RECLAYER): Remove me */ SSL_CONNECTION *s)
402 {
403     int enc_err, rret;
404     int i;
405     size_t more, n;
406     SSL3_RECORD *rr, *thisrr;
407     SSL3_BUFFER *rbuf;
408     SSL_SESSION *sess;
409     unsigned char *p;
410     unsigned char md[EVP_MAX_MD_SIZE];
411     unsigned int version;
412     size_t mac_size = 0;
413     int imac_size;
414     size_t num_recs = 0, max_recs, j;
415     PACKET pkt, sslv2pkt;
416     SSL_MAC_BUF *macbufs = NULL;
417     int ret = OSSL_RECORD_RETURN_FATAL;
418     SSL *ssl = SSL_CONNECTION_GET_SSL(s);
419
420     rr = rl->rrec;
421     rbuf = &rl->rbuf;
422     if (rbuf->buf == NULL) {
423         if (!rlayer_setup_read_buffer(rl)) {
424             /* RLAYERfatal() already called */
425             return OSSL_RECORD_RETURN_FATAL;
426         }
427     }
428
429     max_recs = s->max_pipelines;
430     if (max_recs == 0)
431         max_recs = 1;
432     sess = s->session;
433
434     do {
435         thisrr = &rr[num_recs];
436
437         /* check if we have the header */
438         if ((rl->rstate != SSL_ST_READ_BODY) ||
439             (rl->packet_length < SSL3_RT_HEADER_LENGTH)) {
440             size_t sslv2len;
441             unsigned int type;
442
443             rret = rl->funcs->read_n(rl, SSL3_RT_HEADER_LENGTH,
444                                      SSL3_BUFFER_get_len(rbuf), 0,
445                                      num_recs == 0 ? 1 : 0, &n);
446
447             if (rret < OSSL_RECORD_RETURN_SUCCESS)
448                 return rret; /* error or non-blocking */
449
450             rl->rstate = SSL_ST_READ_BODY;
451
452             p = rl->packet;
453             if (!PACKET_buf_init(&pkt, p, rl->packet_length)) {
454                 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
455                 return OSSL_RECORD_RETURN_FATAL;
456             }
457             sslv2pkt = pkt;
458             if (!PACKET_get_net_2_len(&sslv2pkt, &sslv2len)
459                     || !PACKET_get_1(&sslv2pkt, &type)) {
460                 RLAYERfatal(rl, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
461                 return OSSL_RECORD_RETURN_FATAL;
462             }
463             /*
464              * The first record received by the server may be a V2ClientHello.
465              */
466             if (rl->role == OSSL_RECORD_ROLE_SERVER
467                     && rl->is_first_record
468                     && (sslv2len & 0x8000) != 0
469                     && (type == SSL2_MT_CLIENT_HELLO)) {
470                 /*
471                  *  SSLv2 style record
472                  *
473                  * |num_recs| here will actually always be 0 because
474                  * |num_recs > 0| only ever occurs when we are processing
475                  * multiple app data records - which we know isn't the case here
476                  * because it is an SSLv2ClientHello. We keep it using
477                  * |num_recs| for the sake of consistency
478                  */
479                 thisrr->type = SSL3_RT_HANDSHAKE;
480                 thisrr->rec_version = SSL2_VERSION;
481
482                 thisrr->length = sslv2len & 0x7fff;
483
484                 if (thisrr->length > SSL3_BUFFER_get_len(rbuf)
485                     - SSL2_RT_HEADER_LENGTH) {
486                     RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW,
487                                 SSL_R_PACKET_LENGTH_TOO_LONG);
488                     return OSSL_RECORD_RETURN_FATAL;
489                 }
490             } else {
491                 /* SSLv3+ style record */
492
493                 /* Pull apart the header into the SSL3_RECORD */
494                 if (!PACKET_get_1(&pkt, &type)
495                         || !PACKET_get_net_2(&pkt, &version)
496                         || !PACKET_get_net_2_len(&pkt, &thisrr->length)) {
497                     if (s->msg_callback)
498                         s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, ssl,
499                                         s->msg_callback_arg);
500                     RLAYERfatal(rl, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
501                     return OSSL_RECORD_RETURN_FATAL;
502                 }
503                 thisrr->type = type;
504                 thisrr->rec_version = version;
505
506                 if (s->msg_callback)
507                     s->msg_callback(0, version, SSL3_RT_HEADER, p, 5, ssl,
508                                     s->msg_callback_arg);
509
510                 if (thisrr->length >
511                     SSL3_BUFFER_get_len(rbuf) - SSL3_RT_HEADER_LENGTH) {
512                     RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW,
513                                 SSL_R_PACKET_LENGTH_TOO_LONG);
514                     return OSSL_RECORD_RETURN_FATAL;
515                 }
516             }
517
518             if (!rl->funcs->validate_record_header(rl, thisrr)) {
519                 /* RLAYERfatal already called */
520                 return OSSL_RECORD_RETURN_FATAL;
521             }
522
523             /* now rl->rstate == SSL_ST_READ_BODY */
524         }
525
526         /*
527          * rl->rstate == SSL_ST_READ_BODY, get and decode the data. Calculate
528          * how much more data we need to read for the rest of the record
529          */
530         if (thisrr->rec_version == SSL2_VERSION) {
531             more = thisrr->length + SSL2_RT_HEADER_LENGTH
532                 - SSL3_RT_HEADER_LENGTH;
533         } else {
534             more = thisrr->length;
535         }
536
537         if (more > 0) {
538             /* now rl->packet_length == SSL3_RT_HEADER_LENGTH */
539
540             rret = rl->funcs->read_n(rl, more, more, 1, 0, &n);
541             if (rret < OSSL_RECORD_RETURN_SUCCESS)
542                 return rret;     /* error or non-blocking io */
543         }
544
545         /* set state for later operations */
546         rl->rstate = SSL_ST_READ_HEADER;
547
548         /*
549          * At this point, rl->packet_length == SSL3_RT_HEADER_LENGTH
550          * + thisrr->length, or rl->packet_length == SSL2_RT_HEADER_LENGTH
551          * + thisrr->length and we have that many bytes in rl->packet
552          */
553         if (thisrr->rec_version == SSL2_VERSION)
554             thisrr->input = &(rl->packet[SSL2_RT_HEADER_LENGTH]);
555         else
556             thisrr->input = &(rl->packet[SSL3_RT_HEADER_LENGTH]);
557
558         /*
559          * ok, we can now read from 'rl->packet' data into 'thisrr'.
560          * thisrr->input points at thisrr->length bytes, which need to be copied
561          * into thisrr->data by either the decryption or by the decompression.
562          * When the data is 'copied' into the thisrr->data buffer,
563          * thisrr->input will be updated to point at the new buffer
564          */
565
566         /*
567          * We now have - encrypted [ MAC [ compressed [ plain ] ] ]
568          * thisrr->length bytes of encrypted compressed stuff.
569          */
570
571         /* decrypt in place in 'thisrr->input' */
572         thisrr->data = thisrr->input;
573         thisrr->orig_len = thisrr->length;
574
575         /* Mark this record as not read by upper layers yet */
576         thisrr->read = 0;
577
578         num_recs++;
579
580         /* we have pulled in a full packet so zero things */
581         tls_reset_packet_length(rl);
582         rl->is_first_record = 0;
583     } while (num_recs < max_recs
584              && thisrr->type == SSL3_RT_APPLICATION_DATA
585              && SSL_USE_EXPLICIT_IV(s)
586              && rl->enc_read_ctx != NULL
587              && (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(rl->enc_read_ctx))
588                  & EVP_CIPH_FLAG_PIPELINE) != 0
589              && tls_record_app_data_waiting(rl));
590
591     if (num_recs == 1
592             && thisrr->type == SSL3_RT_CHANGE_CIPHER_SPEC
593             && (SSL_CONNECTION_IS_TLS13(s) || s->hello_retry_request != SSL_HRR_NONE)
594             && rl->is_first_handshake) {
595         /*
596          * CCS messages must be exactly 1 byte long, containing the value 0x01
597          */
598         if (thisrr->length != 1 || thisrr->data[0] != 0x01) {
599             RLAYERfatal(rl, SSL_AD_ILLEGAL_PARAMETER,
600                         SSL_R_INVALID_CCS_MESSAGE);
601             return OSSL_RECORD_RETURN_FATAL;
602         }
603         /*
604          * CCS messages are ignored in TLSv1.3. We treat it like an empty
605          * handshake record
606          */
607         thisrr->type = SSL3_RT_HANDSHAKE;
608         if (++(rl->empty_record_count) > MAX_EMPTY_RECORDS) {
609             RLAYERfatal(rl, SSL_AD_UNEXPECTED_MESSAGE,
610                         SSL_R_UNEXPECTED_CCS_MESSAGE);
611             return OSSL_RECORD_RETURN_FATAL;
612         }
613         thisrr->read = 1;
614         rl->num_recs = 0;
615         rl->curr_rec = 0;
616         rl->num_released = 0;
617
618         return OSSL_RECORD_RETURN_SUCCESS;
619     }
620
621     if (rl->read_hash != NULL) {
622         const EVP_MD *tmpmd = EVP_MD_CTX_get0_md(rl->read_hash);
623
624         if (tmpmd != NULL) {
625             imac_size = EVP_MD_get_size(tmpmd);
626             if (!ossl_assert(imac_size >= 0 && imac_size <= EVP_MAX_MD_SIZE)) {
627                     RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
628                     return OSSL_RECORD_RETURN_FATAL;
629             }
630             mac_size = (size_t)imac_size;
631         }
632     }
633
634     /*
635      * If in encrypt-then-mac mode calculate mac from encrypted record. All
636      * the details below are public so no timing details can leak.
637      */
638     if (SSL_READ_ETM(s) && rl->read_hash) {
639         unsigned char *mac;
640
641         for (j = 0; j < num_recs; j++) {
642             thisrr = &rr[j];
643
644             if (thisrr->length < mac_size) {
645                 RLAYERfatal(rl, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
646                 return OSSL_RECORD_RETURN_FATAL;
647             }
648             thisrr->length -= mac_size;
649             mac = thisrr->data + thisrr->length;
650             i = rl->funcs->mac(rl, thisrr, md, 0 /* not send */, s);
651             if (i == 0 || CRYPTO_memcmp(md, mac, mac_size) != 0) {
652                 RLAYERfatal(rl, SSL_AD_BAD_RECORD_MAC,
653                             SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
654                 return OSSL_RECORD_RETURN_FATAL;
655             }
656         }
657         /*
658          * We've handled the mac now - there is no MAC inside the encrypted
659          * record
660          */
661         mac_size = 0;
662     }
663
664     if (mac_size > 0) {
665         macbufs = OPENSSL_zalloc(sizeof(*macbufs) * num_recs);
666         if (macbufs == NULL) {
667             RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
668             return OSSL_RECORD_RETURN_FATAL;
669         }
670     }
671
672     /*
673      * TODO(RECLAYER): Only call rl functions once TLSv1.3/SSLv3 is moved to new
674      * record layer code
675      */
676     enc_err = rl->funcs->cipher(rl, rr, num_recs, 0, macbufs, mac_size, s);
677
678     /*-
679      * enc_err is:
680      *    0: if the record is publicly invalid, or an internal error, or AEAD
681      *       decryption failed, or ETM decryption failed.
682      *    1: Success or MTE decryption failed (MAC will be randomised)
683      */
684     if (enc_err == 0) {
685         if (ossl_statem_in_error(s)) {
686             /* SSLfatal() already got called */
687             goto end;
688         }
689         if (num_recs == 1 && ossl_statem_skip_early_data(s)) {
690             /*
691              * Valid early_data that we cannot decrypt will fail here. We treat
692              * it like an empty record.
693              */
694
695             thisrr = &rr[0];
696
697             if (!ossl_early_data_count_ok(s, thisrr->length,
698                                      EARLY_DATA_CIPHERTEXT_OVERHEAD, 0)) {
699                 /* SSLfatal() already called */
700                 goto end;
701             }
702
703             thisrr->length = 0;
704             thisrr->read = 1;
705             rl->num_recs = 0;
706             rl->curr_rec = 0;
707             rl->num_released = 0;
708             RECORD_LAYER_reset_read_sequence(&s->rlayer);
709             ret = 1;
710             goto end;
711         }
712         RLAYERfatal(rl, SSL_AD_BAD_RECORD_MAC,
713                     SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
714         goto end;
715     }
716     OSSL_TRACE_BEGIN(TLS) {
717         BIO_printf(trc_out, "dec %lu\n", (unsigned long)rr[0].length);
718         BIO_dump_indent(trc_out, rr[0].data, rr[0].length, 4);
719     } OSSL_TRACE_END(TLS);
720
721     /* r->length is now the compressed data plus mac */
722     if ((sess != NULL)
723             && (rl->enc_read_ctx != NULL)
724             && (!SSL_READ_ETM(s) && EVP_MD_CTX_get0_md(rl->read_hash) != NULL)) {
725         /* rl->read_hash != NULL => mac_size != -1 */
726
727         for (j = 0; j < num_recs; j++) {
728             SSL_MAC_BUF *thismb = &macbufs[j];
729             thisrr = &rr[j];
730
731             i = rl->funcs->mac(rl, thisrr, md, 0 /* not send */, s);
732             if (i == 0 || thismb == NULL || thismb->mac == NULL
733                 || CRYPTO_memcmp(md, thismb->mac, (size_t)mac_size) != 0)
734                 enc_err = 0;
735             if (thisrr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
736                 enc_err = 0;
737         }
738     }
739
740     if (enc_err == 0) {
741         if (ossl_statem_in_error(s)) {
742             /* We already called SSLfatal() */
743             goto end;
744         }
745         /*
746          * A separate 'decryption_failed' alert was introduced with TLS 1.0,
747          * SSL 3.0 only has 'bad_record_mac'.  But unless a decryption
748          * failure is directly visible from the ciphertext anyway, we should
749          * not reveal which kind of error occurred -- this might become
750          * visible to an attacker (e.g. via a logfile)
751          */
752         RLAYERfatal(rl, SSL_AD_BAD_RECORD_MAC,
753                     SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
754         goto end;
755     }
756
757     for (j = 0; j < num_recs; j++) {
758         thisrr = &rr[j];
759
760         if (!rl->funcs->post_process_record(rl, thisrr, s)) {
761             /* RLAYERfatal already called */
762             goto end;
763         }
764
765         /*
766          * Check if the received packet overflows the current
767          * Max Fragment Length setting.
768          * Note: USE_MAX_FRAGMENT_LENGTH_EXT and KTLS are mutually exclusive.
769          */
770         if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
771                 && thisrr->length > GET_MAX_FRAGMENT_LENGTH(s->session)) {
772             RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
773             goto end;
774         }
775
776         thisrr->off = 0;
777         /*-
778          * So at this point the following is true
779          * thisrr->type   is the type of record
780          * thisrr->length == number of bytes in record
781          * thisrr->off    == offset to first valid byte
782          * thisrr->data   == where to take bytes from, increment after use :-).
783          */
784
785         /* just read a 0 length packet */
786         if (thisrr->length == 0) {
787             if (++(rl->empty_record_count) > MAX_EMPTY_RECORDS) {
788                 RLAYERfatal(rl, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_RECORD_TOO_SMALL);
789                 goto end;
790             }
791         } else {
792             rl->empty_record_count = 0;
793         }
794     }
795
796     if (s->early_data_state == SSL_EARLY_DATA_READING) {
797         thisrr = &rr[0];
798         if (thisrr->type == SSL3_RT_APPLICATION_DATA
799                 && !ossl_early_data_count_ok(s, thisrr->length, 0, 0)) {
800             /* SSLfatal already called */
801             goto end;
802         }
803     }
804
805     rl->num_recs = num_recs;
806     rl->curr_rec = 0;
807     rl->num_released = 0;
808     ret = OSSL_RECORD_RETURN_SUCCESS;
809  end:
810     if (macbufs != NULL) {
811         for (j = 0; j < num_recs; j++) {
812             if (macbufs[j].alloced)
813                 OPENSSL_free(macbufs[j].mac);
814         }
815         OPENSSL_free(macbufs);
816     }
817     return ret;
818 }
819
820 /* Shared by ssl3_meth and tls1_meth */
821 int tls_default_validate_record_header(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec)
822 {
823     size_t len = SSL3_RT_MAX_ENCRYPTED_LENGTH;
824
825     if (rec->rec_version != rl->version) {
826         RLAYERfatal(rl, SSL_AD_PROTOCOL_VERSION, SSL_R_WRONG_VERSION_NUMBER);
827         return 0;
828     }
829
830 #ifndef OPENSSL_NO_COMP
831     /*
832         * If OPENSSL_NO_COMP is defined then SSL3_RT_MAX_ENCRYPTED_LENGTH
833         * does not include the compression overhead anyway.
834         */
835     if (rl->expand == NULL)
836         len -= SSL3_RT_MAX_COMPRESSED_OVERHEAD;
837 #endif
838
839     if (rec->length > len) {
840         RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW,
841                     SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
842         return 0;
843     }
844
845     return 1;
846 }
847
848 static int tls_do_uncompress(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec)
849 {
850 #ifndef OPENSSL_NO_COMP
851     int i;
852
853     if (rec->comp == NULL) {
854         rec->comp = (unsigned char *)
855             OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
856     }
857     if (rec->comp == NULL)
858         return 0;
859
860     i = COMP_expand_block(rl->expand, rec->comp,
861                           SSL3_RT_MAX_PLAIN_LENGTH, rec->data, (int)rec->length);
862     if (i < 0)
863         return 0;
864     else
865         rec->length = i;
866     rec->data = rec->comp;
867     return 1;
868 #else
869     return 0;
870 #endif
871 }
872
873 /* Shared by tlsany_meth, ssl3_meth and tls1_meth */
874 int tls_default_post_process_record(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec, SSL_CONNECTION *s)
875 {
876     if (rl->expand != NULL) {
877         if (rec->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
878             RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW,
879                         SSL_R_COMPRESSED_LENGTH_TOO_LONG);
880             return 0;
881         }
882         if (!tls_do_uncompress(rl, rec)) {
883             RLAYERfatal(rl, SSL_AD_DECOMPRESSION_FAILURE,
884                         SSL_R_BAD_DECOMPRESSION);
885             return 0;
886         }
887     }
888
889     if (rec->length > SSL3_RT_MAX_PLAIN_LENGTH) {
890         RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
891         return 0;
892     }
893
894     return 1;
895 }
896
897 /* Shared by tls13_meth and ktls_meth */
898 int tls13_common_post_process_record(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec,
899                                      SSL_CONNECTION *s)
900 {
901     SSL *ssl = SSL_CONNECTION_GET_SSL(s);
902
903     if (rec->type != SSL3_RT_APPLICATION_DATA
904             && rec->type != SSL3_RT_ALERT
905             && rec->type != SSL3_RT_HANDSHAKE) {
906         RLAYERfatal(rl, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_BAD_RECORD_TYPE);
907         return 0;
908     }
909
910     if (s->msg_callback)
911         s->msg_callback(0, rl->version, SSL3_RT_INNER_CONTENT_TYPE,
912                         &rec->type, 1, ssl, s->msg_callback_arg);
913
914     /*
915      * TLSv1.3 alert and handshake records are required to be non-zero in
916      * length.
917      */
918     if ((rec->type == SSL3_RT_HANDSHAKE
919                 || rec->type == SSL3_RT_ALERT)
920             && rec->length == 0) {
921         RLAYERfatal(rl, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_BAD_LENGTH);
922         return 0;
923     }
924
925     return 1;
926 }
927
928 int tls_read_record(OSSL_RECORD_LAYER *rl, void **rechandle,  int *rversion,
929                     int *type, unsigned char **data, size_t *datalen,
930                     uint16_t *epoch, unsigned char *seq_num,
931                     /* TODO(RECLAYER): Remove me */ SSL_CONNECTION *s)
932 {
933     SSL3_RECORD *rec;
934
935     /*
936      * tls_get_more_records() can return success without actually reading
937      * anything useful (i.e. if empty records are read). We loop here until
938      * we have something useful. tls_get_more_records() will eventually fail if
939      * too many sequential empty records are read.
940      */
941     while (rl->curr_rec >= rl->num_recs) {
942         int ret;
943
944         if (rl->num_released != rl->num_recs) {
945             RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_RECORDS_NOT_RELEASED);
946             return OSSL_RECORD_RETURN_FATAL;
947         }
948
949         ret = tls_get_more_records(rl, s);
950
951         if (ret != OSSL_RECORD_RETURN_SUCCESS)
952             return ret;
953     }
954
955     /*
956      * We have now got rl->num_recs records buffered in rl->rrec. rl->curr_rec
957      * points to the next one to read.
958      */
959     rec = &rl->rrec[rl->curr_rec++];
960
961     *rechandle = rec;
962     *rversion = rec->rec_version;
963     *type = rec->type;
964     *data = rec->data;
965     *datalen = rec->length;
966
967     return OSSL_RECORD_RETURN_SUCCESS;
968 }
969
970 int tls_release_record(OSSL_RECORD_LAYER *rl, void *rechandle)
971 {
972     if (!ossl_assert(rl->num_released < rl->curr_rec)
973             || !ossl_assert(rechandle == &rl->rrec[rl->num_released])) {
974         /* Should not happen */
975         RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_INVALID_RECORD);
976         return OSSL_RECORD_RETURN_FATAL;
977     }
978
979     rl->num_released++;
980
981     return OSSL_RECORD_RETURN_SUCCESS;
982 }
983
984 int
985 tls_int_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
986                          int role, int direction, int level, unsigned char *key,
987                          size_t keylen, unsigned char *iv, size_t ivlen,
988                          unsigned char *mackey, size_t mackeylen,
989                          const EVP_CIPHER *ciph, size_t taglen,
990                          /* TODO(RECLAYER): This probably should not be an int */
991                          int mactype,
992                          const EVP_MD *md, const SSL_COMP *comp, BIO *transport,
993                          BIO_ADDR *local, BIO_ADDR *peer,
994                          const OSSL_PARAM *settings, const OSSL_PARAM *options,
995                          OSSL_RECORD_LAYER **retrl,
996                          /* TODO(RECLAYER): Remove me */
997                          SSL_CONNECTION *s)
998 {
999     OSSL_RECORD_LAYER *rl = OPENSSL_zalloc(sizeof(*rl));
1000     const OSSL_PARAM *p;
1001
1002     *retrl = NULL;
1003
1004     if (rl == NULL) {
1005         RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
1006         return OSSL_RECORD_RETURN_FATAL;
1007     }
1008
1009     if (transport != NULL && !BIO_up_ref(transport)) {
1010         RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
1011         goto err;
1012     }
1013
1014     /*
1015      * TODO(RECLAYER): Need to handle the case where the params are updated
1016      * after the record layer has been created.
1017      */
1018     p = OSSL_PARAM_locate_const(options, OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS);
1019     if (p != NULL && !OSSL_PARAM_get_uint64(p, &rl->options)) {
1020         RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_FAILED_TO_GET_PARAMETER);
1021         goto err;
1022     }
1023
1024     p = OSSL_PARAM_locate_const(options, OSSL_LIBSSL_RECORD_LAYER_PARAM_MODE);
1025     if (p != NULL && !OSSL_PARAM_get_uint32(p, &rl->mode)) {
1026         RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_FAILED_TO_GET_PARAMETER);
1027         goto err;
1028     }
1029
1030     if (level == OSSL_RECORD_PROTECTION_LEVEL_APPLICATION) {
1031         /*
1032          * We ignore any read_ahead setting prior to the application protection
1033          * level. Otherwise we may read ahead data in a lower protection level
1034          * that is destined for a higher protection level. To simplify the logic
1035          * we don't support that at this stage.
1036          */
1037         /*
1038          * TODO(RECLAYER): Handle the case of read_ahead at the application
1039          * level and a key update/reneg occurs.
1040          */
1041         p = OSSL_PARAM_locate_const(options, OSSL_LIBSSL_RECORD_LAYER_PARAM_READ_AHEAD);
1042         if (p != NULL && !OSSL_PARAM_get_int(p, &rl->read_ahead)) {
1043             RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_FAILED_TO_GET_PARAMETER);
1044             goto err;
1045         }
1046     }
1047
1048     rl->libctx = libctx;
1049     rl->propq = propq;
1050
1051     rl->version = vers;
1052     rl->role = role;
1053     rl->direction = direction;
1054
1055     if (level == OSSL_RECORD_PROTECTION_LEVEL_NONE)
1056         rl->is_first_record = 1;
1057
1058     if (!tls_set1_bio(rl, transport))
1059         goto err;
1060
1061     *retrl = rl;
1062     return OSSL_RECORD_RETURN_SUCCESS;
1063  err:
1064     OPENSSL_free(rl);
1065     return OSSL_RECORD_RETURN_FATAL;
1066 }
1067
1068 static int
1069 tls_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
1070                      int role, int direction, int level, unsigned char *key,
1071                      size_t keylen, unsigned char *iv, size_t ivlen,
1072                      unsigned char *mackey, size_t mackeylen,
1073                      const EVP_CIPHER *ciph, size_t taglen,
1074                      /* TODO(RECLAYER): This probably should not be an int */
1075                      int mactype,
1076                      const EVP_MD *md, const SSL_COMP *comp, BIO *transport,
1077                      BIO_ADDR *local, BIO_ADDR *peer,
1078                      const OSSL_PARAM *settings, const OSSL_PARAM *options,
1079                      OSSL_RECORD_LAYER **retrl,
1080                      /* TODO(RECLAYER): Remove me */
1081                      SSL_CONNECTION *s)
1082 {
1083     int ret;
1084     
1085     ret = tls_int_new_record_layer(libctx, propq, vers, role, direction, level,
1086                                    key, keylen, iv, ivlen, mackey, mackeylen,
1087                                    ciph, taglen, mactype, md, comp, transport,
1088                                    local, peer, settings, options, retrl, s);
1089
1090     if (ret != OSSL_RECORD_RETURN_SUCCESS)
1091         return ret;
1092
1093     switch (vers) {
1094     case TLS_ANY_VERSION:
1095         (*retrl)->funcs = &tls_any_funcs;
1096         break;
1097     case TLS1_3_VERSION:
1098         (*retrl)->funcs = &tls_1_3_funcs;
1099         break;
1100     case TLS1_2_VERSION:
1101     case TLS1_1_VERSION:
1102     case TLS1_VERSION:
1103         (*retrl)->funcs = &tls_1_funcs;
1104         break;
1105     case SSL3_VERSION:
1106         (*retrl)->funcs = &ssl_3_0_funcs;
1107         break;
1108     default:
1109         /* Should not happen */
1110         ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
1111         ret = OSSL_RECORD_RETURN_FATAL;
1112         goto err;
1113     }
1114
1115     ret = (*retrl)->funcs->set_crypto_state(*retrl, level, key, keylen, iv,
1116                                              ivlen, mackey, mackeylen, ciph,
1117                                              taglen, mactype, md, comp, s);
1118
1119  err:
1120     if (ret != OSSL_RECORD_RETURN_SUCCESS) {
1121         OPENSSL_free(*retrl);
1122         *retrl = NULL;
1123     }
1124     return ret;
1125 }
1126
1127 /* TODO(RECLAYER): Temporary funcs */
1128 static int dtls_set_protocol_version(OSSL_RECORD_LAYER *rl, int version)
1129 {
1130     rl->version = version;
1131     return 1;
1132 }
1133 static struct record_functions_st dtls_funcs = {
1134     NULL,
1135     NULL,
1136     NULL,
1137     NULL,
1138     dtls_set_protocol_version,
1139     NULL
1140 };
1141
1142 static int
1143 dtls_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
1144                       int role, int direction, int level, unsigned char *key,
1145                       size_t keylen, unsigned char *iv, size_t ivlen,
1146                       unsigned char *mackey, size_t mackeylen,
1147                       const EVP_CIPHER *ciph, size_t taglen,
1148                       /* TODO(RECLAYER): This probably should not be an int */
1149                       int mactype,
1150                       const EVP_MD *md, const SSL_COMP *comp, BIO *transport,
1151                       BIO_ADDR *local, BIO_ADDR *peer,
1152                       const OSSL_PARAM *settings, const OSSL_PARAM *options,
1153                       OSSL_RECORD_LAYER **retrl,
1154                       /* TODO(RECLAYER): Remove me */
1155                       SSL_CONNECTION *s)
1156 {
1157     int ret;
1158
1159
1160     ret = tls_int_new_record_layer(libctx, propq, vers, role, direction, level,
1161                                    key, keylen, iv, ivlen, mackey, mackeylen,
1162                                    ciph, taglen, mactype, md, comp, transport,
1163                                    local, peer, settings, options, retrl, s);
1164
1165     if (ret != OSSL_RECORD_RETURN_SUCCESS)
1166         return ret;
1167
1168     (*retrl)->isdtls = 1;
1169     (*retrl)->funcs = &dtls_funcs;
1170
1171     return OSSL_RECORD_RETURN_SUCCESS;
1172 }
1173
1174 void tls_free(OSSL_RECORD_LAYER *rl)
1175 {
1176     /* TODO(RECLAYER): Cleanse sensitive fields */
1177     BIO_free(rl->bio);
1178     OPENSSL_free(rl);
1179 }
1180
1181 int tls_reset(OSSL_RECORD_LAYER *rl)
1182 {
1183     memset(rl, 0, sizeof(*rl));
1184     return 1;
1185 }
1186
1187 int tls_unprocessed_read_pending(OSSL_RECORD_LAYER *rl)
1188 {
1189     return SSL3_BUFFER_get_left(&rl->rbuf) != 0;;
1190 }
1191
1192 int tls_processed_read_pending(OSSL_RECORD_LAYER *rl)
1193 {
1194     return rl->curr_rec < rl->num_recs;
1195 }
1196
1197 size_t tls_app_data_pending(OSSL_RECORD_LAYER *rl)
1198 {
1199     return 0;
1200 }
1201
1202 int tls_write_pending(OSSL_RECORD_LAYER *rl)
1203 {
1204     return 0;
1205 }
1206
1207 size_t tls_get_max_record_len(OSSL_RECORD_LAYER *rl)
1208 {
1209     return 0;
1210 }
1211
1212 size_t tls_get_max_records(OSSL_RECORD_LAYER *rl)
1213 {
1214     return 0;
1215 }
1216
1217 int tls_write_records(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE **templates,
1218                       size_t numtempl, size_t allowance, size_t *sent)
1219 {
1220     return 0;
1221 }
1222
1223 int tls_retry_write_records(OSSL_RECORD_LAYER *rl, size_t allowance,
1224                             size_t *sent)
1225 {
1226     return 0;
1227 }
1228
1229
1230 int tls_get_alert_code(OSSL_RECORD_LAYER *rl)
1231 {
1232     return rl->alert;
1233 }
1234
1235 int tls_set1_bio(OSSL_RECORD_LAYER *rl, BIO *bio)
1236 {
1237     if (bio != NULL && !BIO_up_ref(bio))
1238         return 0;
1239     BIO_free(rl->bio);
1240     rl->bio = bio;
1241
1242     return 1;
1243 }
1244
1245 /* Shared by most methods except tlsany_meth */
1246 int tls_default_set_protocol_version(OSSL_RECORD_LAYER *rl, int version)
1247 {
1248     if (rl->version != version)
1249         return 0;
1250
1251     return 1;
1252 }
1253
1254 int tls_set_protocol_version(OSSL_RECORD_LAYER *rl, int version)
1255 {
1256     return rl->funcs->set_protocol_version(rl, version);
1257 }
1258
1259 void tls_set_plain_alerts(OSSL_RECORD_LAYER *rl, int allow)
1260 {
1261     rl->allow_plain_alerts = allow;
1262 }
1263
1264 void tls_set_first_handshake(OSSL_RECORD_LAYER *rl, int first)
1265 {
1266     rl->is_first_handshake = first;
1267 }
1268
1269 SSL3_BUFFER *tls_get0_rbuf(OSSL_RECORD_LAYER *rl)
1270 {
1271     return &rl->rbuf;
1272 }
1273
1274 unsigned char *tls_get0_packet(OSSL_RECORD_LAYER *rl)
1275 {
1276     return rl->packet;
1277 }
1278
1279 void tls_set0_packet(OSSL_RECORD_LAYER *rl, unsigned char *packet,
1280                      size_t packetlen)
1281 {
1282     rl->packet = packet;
1283     rl->packet_length = packetlen;
1284 }
1285
1286 size_t tls_get_packet_length(OSSL_RECORD_LAYER *rl)
1287 {
1288     return rl->packet_length;
1289 }
1290
1291 const OSSL_RECORD_METHOD ossl_tls_record_method = {
1292     tls_new_record_layer,
1293     tls_free,
1294     tls_reset,
1295     tls_unprocessed_read_pending,
1296     tls_processed_read_pending,
1297     tls_app_data_pending,
1298     tls_write_pending,
1299     tls_get_max_record_len,
1300     tls_get_max_records,
1301     tls_write_records,
1302     tls_retry_write_records,
1303     tls_read_record,
1304     tls_release_record,
1305     tls_get_alert_code,
1306     tls_set1_bio,
1307     tls_set_protocol_version,
1308     tls_set_plain_alerts,
1309     tls_set_first_handshake,
1310
1311     /*
1312      * TODO(RECLAYER): Remove these. These function pointers are temporary hacks
1313      * during the record layer refactoring. They need to be removed before the
1314      * refactor is complete.
1315      */
1316     tls_default_read_n,
1317     tls_get0_rbuf,
1318     tls_get0_packet,
1319     tls_set0_packet,
1320     tls_get_packet_length,
1321     tls_reset_packet_length
1322 };
1323
1324 const OSSL_RECORD_METHOD ossl_dtls_record_method = {
1325     dtls_new_record_layer,
1326     tls_free,
1327     tls_reset,
1328     tls_unprocessed_read_pending,
1329     tls_processed_read_pending,
1330     tls_app_data_pending,
1331     tls_write_pending,
1332     tls_get_max_record_len,
1333     tls_get_max_records,
1334     tls_write_records,
1335     tls_retry_write_records,
1336     tls_read_record,
1337     tls_release_record,
1338     tls_get_alert_code,
1339     tls_set1_bio,
1340     tls_set_protocol_version,
1341     NULL,
1342     tls_set_first_handshake,
1343
1344     /*
1345      * TODO(RECLAYER): Remove these. These function pointers are temporary hacks
1346      * during the record layer refactoring. They need to be removed before the
1347      * refactor is complete.
1348      */
1349     tls_default_read_n,
1350     tls_get0_rbuf,
1351     tls_get0_packet,
1352     tls_set0_packet,
1353     tls_get_packet_length,
1354     tls_reset_packet_length
1355 };