xref: /freebsd/crypto/openssl/ssl/record/methods/dtls_meth.c (revision 78e936b2d0b5e6554425009199be31e76bc67c10)
1 /*
2  * Copyright 2018-2026 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 <assert.h>
11 #include "../../ssl_local.h"
12 #include "../record_local.h"
13 #include "recmethod_local.h"
14 
15 /* mod 128 saturating subtract of two 64-bit values in big-endian order */
satsub64be(const unsigned char * v1,const unsigned char * v2)16 static int satsub64be(const unsigned char *v1, const unsigned char *v2)
17 {
18     int64_t ret;
19     uint64_t l1, l2;
20 
21     n2l8(v1, l1);
22     n2l8(v2, l2);
23 
24     ret = l1 - l2;
25 
26     /* We do not permit wrap-around */
27     if (l1 > l2 && ret < 0)
28         return 128;
29     else if (l2 > l1 && ret > 0)
30         return -128;
31 
32     if (ret > 128)
33         return 128;
34     else if (ret < -128)
35         return -128;
36     else
37         return (int)ret;
38 }
39 
dtls_record_replay_check(OSSL_RECORD_LAYER * rl,DTLS_BITMAP * bitmap)40 static int dtls_record_replay_check(OSSL_RECORD_LAYER *rl, DTLS_BITMAP *bitmap)
41 {
42     int cmp;
43     unsigned int shift;
44     const unsigned char *seq = rl->sequence;
45 
46     cmp = satsub64be(seq, bitmap->max_seq_num);
47     if (cmp > 0) {
48         ossl_tls_rl_record_set_seq_num(&rl->rrec[0], seq);
49         return 1; /* this record in new */
50     }
51     shift = -cmp;
52     if (shift >= sizeof(bitmap->map) * 8)
53         return 0; /* stale, outside the window */
54     else if (bitmap->map & ((uint64_t)1 << shift))
55         return 0; /* record previously received */
56 
57     ossl_tls_rl_record_set_seq_num(&rl->rrec[0], seq);
58     return 1;
59 }
60 
dtls_record_bitmap_update(OSSL_RECORD_LAYER * rl,DTLS_BITMAP * bitmap)61 static void dtls_record_bitmap_update(OSSL_RECORD_LAYER *rl,
62     DTLS_BITMAP *bitmap)
63 {
64     int cmp;
65     unsigned int shift;
66     const unsigned char *seq = rl->sequence;
67 
68     cmp = satsub64be(seq, bitmap->max_seq_num);
69     if (cmp > 0) {
70         shift = cmp;
71         if (shift < sizeof(bitmap->map) * 8)
72             bitmap->map <<= shift, bitmap->map |= 1UL;
73         else
74             bitmap->map = 1UL;
75         memcpy(bitmap->max_seq_num, seq, SEQ_NUM_SIZE);
76     } else {
77         shift = -cmp;
78         if (shift < sizeof(bitmap->map) * 8)
79             bitmap->map |= (uint64_t)1 << shift;
80     }
81 }
82 
dtls_get_bitmap(OSSL_RECORD_LAYER * rl,TLS_RL_RECORD * rr,unsigned int * is_next_epoch)83 static DTLS_BITMAP *dtls_get_bitmap(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *rr,
84     unsigned int *is_next_epoch)
85 {
86     *is_next_epoch = 0;
87 
88     /* In current epoch, accept HM, CCS, DATA, & ALERT */
89     if (rr->epoch == rl->epoch)
90         return &rl->bitmap;
91 
92     /*
93      * Check if the message is from the next epoch
94      */
95     else if (rr->epoch == rl->epoch + 1) {
96         *is_next_epoch = 1;
97         return &rl->next_bitmap;
98     }
99 
100     return NULL;
101 }
102 
dtls_set_in_init(OSSL_RECORD_LAYER * rl,int in_init)103 static void dtls_set_in_init(OSSL_RECORD_LAYER *rl, int in_init)
104 {
105     rl->in_init = in_init;
106 }
107 
dtls_process_record(OSSL_RECORD_LAYER * rl,DTLS_BITMAP * bitmap)108 static int dtls_process_record(OSSL_RECORD_LAYER *rl, DTLS_BITMAP *bitmap)
109 {
110     int i;
111     int enc_err;
112     TLS_RL_RECORD *rr;
113     int imac_size;
114     size_t mac_size = 0;
115     unsigned char md[EVP_MAX_MD_SIZE];
116     SSL_MAC_BUF macbuf = { NULL, 0 };
117     int ret = 0;
118 
119     rr = &rl->rrec[0];
120 
121     /*
122      * At this point, rl->packet_length == DTLS1_RT_HEADER_LENGTH + rr->length,
123      * and we have that many bytes in rl->packet
124      */
125     rr->input = &(rl->packet[DTLS1_RT_HEADER_LENGTH]);
126 
127     /*
128      * ok, we can now read from 'rl->packet' data into 'rr'. rr->input
129      * points at rr->length bytes, which need to be copied into rr->data by
130      * either the decryption or by the decompression. When the data is 'copied'
131      * into the rr->data buffer, rr->input will be pointed at the new buffer
132      */
133 
134     /*
135      * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
136      * bytes of encrypted compressed stuff.
137      */
138 
139     /* check is not needed I believe */
140     if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
141         RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
142         return 0;
143     }
144 
145     /* decrypt in place in 'rr->input' */
146     rr->data = rr->input;
147     rr->orig_len = rr->length;
148 
149     if (rl->md_ctx != NULL) {
150         const EVP_MD *tmpmd = EVP_MD_CTX_get0_md(rl->md_ctx);
151 
152         if (tmpmd != NULL) {
153             imac_size = EVP_MD_get_size(tmpmd);
154             if (!ossl_assert(imac_size > 0 && imac_size <= EVP_MAX_MD_SIZE)) {
155                 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
156                 return 0;
157             }
158             mac_size = (size_t)imac_size;
159         }
160     }
161 
162     if (rl->use_etm && rl->md_ctx != NULL) {
163         unsigned char *mac;
164 
165         if (rr->orig_len < mac_size) {
166             RLAYERfatal(rl, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
167             return 0;
168         }
169         rr->length -= mac_size;
170         mac = rr->data + rr->length;
171         i = rl->funcs->mac(rl, rr, md, 0 /* not send */);
172         if (i == 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {
173             RLAYERfatal(rl, SSL_AD_BAD_RECORD_MAC,
174                 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
175             return 0;
176         }
177         /*
178          * We've handled the mac now - there is no MAC inside the encrypted
179          * record
180          */
181         mac_size = 0;
182     }
183 
184     /*
185      * Set a mark around the packet decryption attempt.  This is DTLS, so
186      * bad packets are just ignored, and we don't want to leave stray
187      * errors in the queue from processing bogus junk that we ignored.
188      */
189     ERR_set_mark();
190     enc_err = rl->funcs->cipher(rl, rr, 1, 0, &macbuf, mac_size);
191 
192     /*-
193      * enc_err is:
194      *    0: if the record is publicly invalid, or an internal error, or AEAD
195      *       decryption failed, or ETM decryption failed.
196      *    1: Success or MTE decryption failed (MAC will be randomised)
197      */
198     if (enc_err == 0) {
199         ERR_pop_to_mark();
200         if (rl->alert != SSL_AD_NO_ALERT) {
201             /* RLAYERfatal() already called */
202             goto end;
203         }
204         /* For DTLS we simply ignore bad packets. */
205         rr->length = 0;
206         rl->packet_length = 0;
207         goto end;
208     }
209     ERR_clear_last_mark();
210     OSSL_TRACE_BEGIN(TLS)
211     {
212         BIO_printf(trc_out, "dec %zd\n", rr->length);
213         BIO_dump_indent(trc_out, rr->data, rr->length, 4);
214     }
215     OSSL_TRACE_END(TLS);
216 
217     /* r->length is now the compressed data plus mac */
218     if (!rl->use_etm
219         && (rl->enc_ctx != NULL)
220         && (EVP_MD_CTX_get0_md(rl->md_ctx) != NULL)) {
221         /* rl->md_ctx != NULL => mac_size != -1 */
222 
223         i = rl->funcs->mac(rl, rr, md, 0 /* not send */);
224         if (i == 0 || macbuf.mac == NULL
225             || CRYPTO_memcmp(md, macbuf.mac, mac_size) != 0)
226             enc_err = 0;
227         if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
228             enc_err = 0;
229     }
230 
231     if (enc_err == 0) {
232         /* decryption failed, silently discard message */
233         rr->length = 0;
234         rl->packet_length = 0;
235         goto end;
236     }
237 
238     /* r->length is now just compressed */
239     if (rl->compctx != NULL) {
240         if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
241             RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW,
242                 SSL_R_COMPRESSED_LENGTH_TOO_LONG);
243             goto end;
244         }
245         if (!tls_do_uncompress(rl, rr)) {
246             RLAYERfatal(rl, SSL_AD_DECOMPRESSION_FAILURE, SSL_R_BAD_DECOMPRESSION);
247             goto end;
248         }
249     }
250 
251     /*
252      * Check if the received packet overflows the current Max Fragment
253      * Length setting.
254      */
255     if (rr->length > rl->max_frag_len) {
256         RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
257         goto end;
258     }
259 
260     rr->off = 0;
261     /*-
262      * So at this point the following is true
263      * ssl->s3.rrec.type   is the type of record
264      * ssl->s3.rrec.length == number of bytes in record
265      * ssl->s3.rrec.off    == offset to first valid byte
266      * ssl->s3.rrec.data   == where to take bytes from, increment
267      *                        after use :-).
268      */
269 
270     /* we have pulled in a full packet so zero things */
271     rl->packet_length = 0;
272 
273     /* Mark receipt of record. */
274     dtls_record_bitmap_update(rl, bitmap);
275 
276     ret = 1;
277 end:
278     if (macbuf.alloced)
279         OPENSSL_free(macbuf.mac);
280     return ret;
281 }
282 
dtls_rlayer_buffer_record(OSSL_RECORD_LAYER * rl,struct pqueue_st * queue,unsigned char * priority)283 static int dtls_rlayer_buffer_record(OSSL_RECORD_LAYER *rl, struct pqueue_st *queue,
284     unsigned char *priority)
285 {
286     DTLS_RLAYER_RECORD_DATA *rdata;
287     pitem *item;
288 
289     /* Limit the size of the queue to prevent DOS attacks */
290     if (pqueue_size(queue) >= 16)
291         return 0;
292 
293     rdata = OPENSSL_malloc(sizeof(*rdata));
294     item = pitem_new(priority, rdata);
295     if (rdata == NULL || item == NULL) {
296         OPENSSL_free(rdata);
297         pitem_free(item);
298         RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
299         return -1;
300     }
301 
302     /*
303      * Take a copy of just this record's on-wire bytes (header + ciphertext)
304      * rather than the whole (much larger) read buffer. The live rl->rbuf is
305      * left untouched and continues to be used for subsequent reads.
306      */
307     rdata->packet_length = rl->packet_length;
308     rdata->packet = OPENSSL_memdup(rl->packet, rl->packet_length);
309     if (rdata->packet == NULL) {
310         OPENSSL_free(rdata);
311         pitem_free(item);
312         RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
313         return -1;
314     }
315     memcpy(&(rdata->rrec), &rl->rrec[0], sizeof(TLS_RL_RECORD));
316 
317     item->data = rdata;
318 
319     if (pqueue_insert(queue, item) == NULL) {
320         /* Must be a duplicate so ignore it */
321         OPENSSL_free(rdata->packet);
322         OPENSSL_free(rdata);
323         pitem_free(item);
324     }
325 
326     return 1;
327 }
328 
329 /*-
330  * Call this to get a new input record.
331  * It will return <= 0 if more data is needed, normally due to an error
332  * or non-blocking IO.
333  * When it finishes, one packet has been decoded and can be found in
334  * ssl->s3.rrec.type    - is the type of record
335  * ssl->s3.rrec.data    - data
336  * ssl->s3.rrec.length  - number of bytes
337  */
dtls_get_more_records(OSSL_RECORD_LAYER * rl)338 int dtls_get_more_records(OSSL_RECORD_LAYER *rl)
339 {
340     int ssl_major, ssl_minor;
341     int rret;
342     size_t more, n;
343     TLS_RL_RECORD *rr;
344     unsigned char *p = NULL;
345     DTLS_BITMAP *bitmap;
346     unsigned int is_next_epoch;
347 
348     rl->num_recs = 0;
349     rl->curr_rec = 0;
350     rl->num_released = 0;
351 
352     rr = rl->rrec;
353 
354     if (rl->rbuf.buf == NULL) {
355         if (!tls_setup_read_buffer(rl)) {
356             /* RLAYERfatal() already called */
357             return OSSL_RECORD_RETURN_FATAL;
358         }
359     }
360 
361 again:
362     /* get something from the wire */
363 
364     /* check if we have the header */
365     if ((rl->rstate != SSL_ST_READ_BODY) || (rl->packet_length < DTLS1_RT_HEADER_LENGTH)) {
366         rret = rl->funcs->read_n(rl, DTLS1_RT_HEADER_LENGTH,
367             TLS_BUFFER_get_len(&rl->rbuf), 0, 1, &n);
368         /* read timeout is handled by dtls1_read_bytes */
369         if (rret < OSSL_RECORD_RETURN_SUCCESS) {
370             /* RLAYERfatal() already called if appropriate */
371             return rret; /* error or non-blocking */
372         }
373 
374         /* this packet contained a partial record, dump it */
375         if (rl->packet_length != DTLS1_RT_HEADER_LENGTH) {
376             rl->packet_length = 0;
377             goto again;
378         }
379 
380         rl->rstate = SSL_ST_READ_BODY;
381 
382         p = rl->packet;
383 
384         /* Pull apart the header into the DTLS1_RECORD */
385         rr->type = *(p++);
386         ssl_major = *(p++);
387         ssl_minor = *(p++);
388         rr->rec_version = (ssl_major << 8) | ssl_minor;
389 
390         /* sequence number is 64 bits, with top 2 bytes = epoch */
391         n2s(p, rr->epoch);
392 
393         memcpy(&(rl->sequence[2]), p, 6);
394         p += 6;
395 
396         n2s(p, rr->length);
397 
398         if (rl->msg_callback != NULL)
399             rl->msg_callback(0, rr->rec_version, SSL3_RT_HEADER, rl->packet, DTLS1_RT_HEADER_LENGTH,
400                 rl->cbarg);
401 
402         /*
403          * Lets check the version. We tolerate alerts that don't have the exact
404          * version number (e.g. because of protocol version errors)
405          */
406         if (!rl->is_first_record && rr->type != SSL3_RT_ALERT) {
407             if (rr->rec_version != rl->version) {
408                 /* unexpected version, silently discard */
409                 rr->length = 0;
410                 rl->packet_length = 0;
411                 goto again;
412             }
413         }
414 
415         if (ssl_major != (rl->version == DTLS_ANY_VERSION ? DTLS1_VERSION_MAJOR : rl->version >> 8)) {
416             /* wrong version, silently discard record */
417             rr->length = 0;
418             rl->packet_length = 0;
419             goto again;
420         }
421 
422         if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
423             /* record too long, silently discard it */
424             rr->length = 0;
425             rl->packet_length = 0;
426             goto again;
427         }
428 
429         /*
430          * If received packet overflows maximum possible fragment length then
431          * silently discard it
432          */
433         if (rr->length > rl->max_frag_len + SSL3_RT_MAX_ENCRYPTED_OVERHEAD) {
434             /* record too long, silently discard it */
435             rr->length = 0;
436             rl->packet_length = 0;
437             goto again;
438         }
439 
440         /* now rl->rstate == SSL_ST_READ_BODY */
441     }
442 
443     /* rl->rstate == SSL_ST_READ_BODY, get and decode the data */
444 
445     if (rr->length > rl->packet_length - DTLS1_RT_HEADER_LENGTH) {
446         /* now rl->packet_length == DTLS1_RT_HEADER_LENGTH */
447         more = rr->length;
448         rret = rl->funcs->read_n(rl, more, more, 1, 1, &n);
449         /* this packet contained a partial record, dump it */
450         if (rret < OSSL_RECORD_RETURN_SUCCESS || n != more) {
451             if (rl->alert != SSL_AD_NO_ALERT) {
452                 /* read_n() called RLAYERfatal() */
453                 return OSSL_RECORD_RETURN_FATAL;
454             }
455             rr->length = 0;
456             rl->packet_length = 0;
457             goto again;
458         }
459 
460         /*
461          * now n == rr->length,
462          * and rl->packet_length ==  DTLS1_RT_HEADER_LENGTH + rr->length
463          */
464     }
465     /* set state for later operations */
466     rl->rstate = SSL_ST_READ_HEADER;
467 
468     /* match epochs.  NULL means the packet is dropped on the floor */
469     bitmap = dtls_get_bitmap(rl, rr, &is_next_epoch);
470     if (bitmap == NULL) {
471         rr->length = 0;
472         rl->packet_length = 0; /* dump this record */
473         goto again; /* get another record */
474     }
475 #ifndef OPENSSL_NO_SCTP
476     /* Only do replay check if no SCTP bio */
477     if (!BIO_dgram_is_sctp(rl->bio)) {
478 #endif
479         /* Check whether this is a repeat, or aged record. */
480         if (!dtls_record_replay_check(rl, bitmap)) {
481             rr->length = 0;
482             rl->packet_length = 0; /* dump this record */
483             goto again; /* get another record */
484         }
485 #ifndef OPENSSL_NO_SCTP
486     }
487 #endif
488 
489     /* just read a 0 length packet */
490     if (rr->length == 0)
491         goto again;
492 
493     /*
494      * If this record is from the next epoch (either HM or ALERT), and a
495      * handshake is currently in progress, buffer it since it cannot be
496      * processed at this time.
497      */
498     if (is_next_epoch) {
499         if (rl->in_init) {
500             if (dtls_rlayer_buffer_record(rl, rl->unprocessed_rcds,
501                     rr->seq_num)
502                 < 0) {
503                 /* RLAYERfatal() already called */
504                 return OSSL_RECORD_RETURN_FATAL;
505             }
506         }
507         rr->length = 0;
508         rl->packet_length = 0;
509         goto again;
510     }
511 
512     if (!dtls_process_record(rl, bitmap)) {
513         if (rl->alert != SSL_AD_NO_ALERT) {
514             /* dtls_process_record() called RLAYERfatal */
515             return OSSL_RECORD_RETURN_FATAL;
516         }
517         rr->length = 0;
518         rl->packet_length = 0; /* dump this record */
519         goto again; /* get another record */
520     }
521 
522     if (rl->funcs->post_process_record && !rl->funcs->post_process_record(rl, rr)) {
523         /* RLAYERfatal already called */
524         return OSSL_RECORD_RETURN_FATAL;
525     }
526 
527     if (rr->length == 0) {
528         /* No payload data in this record. Dump it */
529         rl->packet_length = 0;
530         goto again;
531     }
532 
533     rl->num_recs = 1;
534     return OSSL_RECORD_RETURN_SUCCESS;
535 }
536 
dtls_free(OSSL_RECORD_LAYER * rl)537 static int dtls_free(OSSL_RECORD_LAYER *rl)
538 {
539     TLS_BUFFER *rbuf;
540     size_t left, written;
541     pitem *item;
542     DTLS_RLAYER_RECORD_DATA *rdata;
543     int ret = 1;
544 
545     rbuf = &rl->rbuf;
546 
547     left = rbuf->left;
548     if (left > 0) {
549         /*
550          * This record layer is closing but we still have data left in our
551          * buffer. It must be destined for the next epoch - so push it there.
552          */
553         ret = BIO_write_ex(rl->next, rbuf->buf + rbuf->offset, left, &written);
554         rbuf->left = 0;
555     }
556 
557     if (rl->unprocessed_rcds != NULL) {
558         while ((item = pqueue_pop(rl->unprocessed_rcds)) != NULL) {
559             rdata = (DTLS_RLAYER_RECORD_DATA *)item->data;
560             /* Push to the next record layer */
561             ret &= BIO_write_ex(rl->next, rdata->packet, rdata->packet_length,
562                 &written);
563             OPENSSL_free(rdata->packet);
564             OPENSSL_free(item->data);
565             pitem_free(item);
566         }
567         pqueue_free(rl->unprocessed_rcds);
568     }
569 
570     return tls_free(rl) && ret;
571 }
572 
573 static int
dtls_new_record_layer(OSSL_LIB_CTX * libctx,const char * propq,int vers,int role,int direction,int level,uint16_t epoch,unsigned char * secret,size_t secretlen,unsigned char * key,size_t keylen,unsigned char * iv,size_t ivlen,unsigned char * mackey,size_t mackeylen,const EVP_CIPHER * ciph,size_t taglen,int mactype,const EVP_MD * md,COMP_METHOD * comp,const EVP_MD * kdfdigest,BIO * prev,BIO * transport,BIO * next,BIO_ADDR * local,BIO_ADDR * peer,const OSSL_PARAM * settings,const OSSL_PARAM * options,const OSSL_DISPATCH * fns,void * cbarg,void * rlarg,OSSL_RECORD_LAYER ** retrl)574 dtls_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
575     int role, int direction, int level, uint16_t epoch,
576     unsigned char *secret, size_t secretlen,
577     unsigned char *key, size_t keylen, unsigned char *iv,
578     size_t ivlen, unsigned char *mackey, size_t mackeylen,
579     const EVP_CIPHER *ciph, size_t taglen,
580     int mactype,
581     const EVP_MD *md, COMP_METHOD *comp,
582     const EVP_MD *kdfdigest, BIO *prev, BIO *transport,
583     BIO *next, BIO_ADDR *local, BIO_ADDR *peer,
584     const OSSL_PARAM *settings, const OSSL_PARAM *options,
585     const OSSL_DISPATCH *fns, void *cbarg, void *rlarg,
586     OSSL_RECORD_LAYER **retrl)
587 {
588     int ret;
589 
590     ret = tls_int_new_record_layer(libctx, propq, vers, role, direction, level,
591         ciph, taglen, md, comp, prev,
592         transport, next, settings,
593         options, fns, cbarg, retrl);
594 
595     if (ret != OSSL_RECORD_RETURN_SUCCESS)
596         return ret;
597 
598     (*retrl)->unprocessed_rcds = pqueue_new();
599 
600     if ((*retrl)->unprocessed_rcds == NULL) {
601         dtls_free(*retrl);
602         *retrl = NULL;
603         ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
604         return OSSL_RECORD_RETURN_FATAL;
605     }
606 
607     (*retrl)->isdtls = 1;
608     (*retrl)->epoch = epoch;
609     (*retrl)->in_init = 1;
610 
611     switch (vers) {
612     case DTLS_ANY_VERSION:
613         (*retrl)->funcs = &dtls_any_funcs;
614         break;
615     case DTLS1_2_VERSION:
616     case DTLS1_VERSION:
617     case DTLS1_BAD_VER:
618         (*retrl)->funcs = &dtls_1_funcs;
619         break;
620     default:
621         /* Should not happen */
622         ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
623         ret = OSSL_RECORD_RETURN_FATAL;
624         goto err;
625     }
626 
627     ret = (*retrl)->funcs->set_crypto_state(*retrl, level, key, keylen, iv,
628         ivlen, mackey, mackeylen, ciph,
629         taglen, mactype, md, comp);
630 
631 err:
632     if (ret != OSSL_RECORD_RETURN_SUCCESS) {
633         dtls_free(*retrl);
634         *retrl = NULL;
635     }
636     return ret;
637 }
638 
dtls_prepare_record_header(OSSL_RECORD_LAYER * rl,WPACKET * thispkt,OSSL_RECORD_TEMPLATE * templ,uint8_t rectype,unsigned char ** recdata)639 int dtls_prepare_record_header(OSSL_RECORD_LAYER *rl,
640     WPACKET *thispkt,
641     OSSL_RECORD_TEMPLATE *templ,
642     uint8_t rectype,
643     unsigned char **recdata)
644 {
645     size_t maxcomplen;
646 
647     *recdata = NULL;
648 
649     maxcomplen = templ->buflen;
650     if (rl->compctx != NULL)
651         maxcomplen += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
652 
653     if (!WPACKET_put_bytes_u8(thispkt, rectype)
654         || !WPACKET_put_bytes_u16(thispkt, templ->version)
655         || !WPACKET_put_bytes_u16(thispkt, rl->epoch)
656         || !WPACKET_memcpy(thispkt, &(rl->sequence[2]), 6)
657         || !WPACKET_start_sub_packet_u16(thispkt)
658         || (rl->eivlen > 0
659             && !WPACKET_allocate_bytes(thispkt, rl->eivlen, NULL))
660         || (maxcomplen > 0
661             && !WPACKET_reserve_bytes(thispkt, maxcomplen,
662                 recdata))) {
663         RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
664         return 0;
665     }
666 
667     return 1;
668 }
669 
dtls_post_encryption_processing(OSSL_RECORD_LAYER * rl,size_t mac_size,OSSL_RECORD_TEMPLATE * thistempl,WPACKET * thispkt,TLS_RL_RECORD * thiswr)670 int dtls_post_encryption_processing(OSSL_RECORD_LAYER *rl,
671     size_t mac_size,
672     OSSL_RECORD_TEMPLATE *thistempl,
673     WPACKET *thispkt,
674     TLS_RL_RECORD *thiswr)
675 {
676     if (!tls_post_encryption_processing_default(rl, mac_size, thistempl,
677             thispkt, thiswr)) {
678         /* RLAYERfatal() already called */
679         return 0;
680     }
681 
682     return tls_increment_sequence_ctr(rl);
683 }
684 
dtls_get_max_record_overhead(OSSL_RECORD_LAYER * rl)685 static size_t dtls_get_max_record_overhead(OSSL_RECORD_LAYER *rl)
686 {
687     size_t blocksize = 0;
688 
689     if (rl->enc_ctx != NULL && (EVP_CIPHER_CTX_get_mode(rl->enc_ctx) == EVP_CIPH_CBC_MODE))
690         blocksize = EVP_CIPHER_CTX_get_block_size(rl->enc_ctx);
691 
692     /*
693      * If we have a cipher in place then the tag is mandatory. If the cipher is
694      * CBC mode then an explicit IV is also mandatory. If we know the digest,
695      * then we check it is consistent with the taglen. In the case of stitched
696      * ciphers or AEAD ciphers we don't now the digest (or there isn't one) so
697      * we just trust that the taglen is correct.
698      */
699     assert(rl->enc_ctx == NULL || ((blocksize == 0 || rl->eivlen > 0) && rl->taglen > 0));
700     assert(rl->md == NULL || (int)rl->taglen == EVP_MD_size(rl->md));
701 
702     /*
703      * Record overhead consists of the record header, the explicit IV, any
704      * expansion due to cbc padding, and the mac/tag len. There could be
705      * further expansion due to compression - but we don't know what this will
706      * be without knowing the length of the data. However when this function is
707      * called we don't know what the length will be yet - so this is a catch-22.
708      * We *could* use SSL_3_RT_MAX_COMPRESSED_OVERHEAD which is an upper limit
709      * for the maximum record size. But this value is larger than our fallback
710      * MTU size - so isn't very helpful. We just ignore potential expansion
711      * due to compression.
712      */
713     return DTLS1_RT_HEADER_LENGTH + rl->eivlen + blocksize + rl->taglen;
714 }
715 
716 const OSSL_RECORD_METHOD ossl_dtls_record_method = {
717     dtls_new_record_layer,
718     dtls_free,
719     tls_unprocessed_read_pending,
720     tls_processed_read_pending,
721     tls_app_data_pending,
722     tls_get_max_records,
723     tls_write_records,
724     tls_retry_write_records,
725     tls_read_record,
726     tls_release_record,
727     tls_get_alert_code,
728     tls_set1_bio,
729     tls_set_protocol_version,
730     NULL,
731     tls_set_first_handshake,
732     tls_set_max_pipelines,
733     dtls_set_in_init,
734     tls_get_state,
735     tls_set_options,
736     tls_get_compression,
737     tls_set_max_frag_len,
738     dtls_get_max_record_overhead,
739     tls_increment_sequence_ctr,
740     tls_alloc_buffers,
741     tls_free_buffers
742 };
743