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