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