Skip to content

Commit 44aae0c

Browse files
committed
Replace DTLS r_epoch with the read epoch from the TLSv1.2 record layer.
ok inoguchi@ tb@
1 parent 609ec8b commit 44aae0c

File tree

5 files changed

+26
-27
lines changed

5 files changed

+26
-27
lines changed

lib/libssl/d1_lib.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: d1_lib.c,v 1.58 2021/07/21 08:42:14 jsing Exp $ */
1+
/* $OpenBSD: d1_lib.c,v 1.59 2021/08/30 19:12:25 jsing Exp $ */
22
/*
33
* DTLS implementation written by Nagendra Modadugu
44
* (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
@@ -191,10 +191,8 @@ dtls1_clear(SSL *s)
191191
memset(s->d1, 0, sizeof(*s->d1));
192192
s->d1->internal = internal;
193193

194-
D1I(s)->r_epoch =
195-
tls12_record_layer_initial_epoch(s->internal->rl);
196-
197-
D1I(s)->unprocessed_rcds.epoch = D1I(s)->r_epoch + 1;
194+
D1I(s)->unprocessed_rcds.epoch =
195+
tls12_record_layer_read_epoch(s->internal->rl) + 1;
198196

199197
if (s->server) {
200198
D1I(s)->cookie_len = sizeof(D1I(s)->cookie);

lib/libssl/d1_pkt.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: d1_pkt.c,v 1.105 2021/07/31 09:31:04 jsing Exp $ */
1+
/* $OpenBSD: d1_pkt.c,v 1.106 2021/08/30 19:12:25 jsing Exp $ */
22
/*
33
* DTLS implementation written by Nagendra Modadugu
44
* (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
@@ -273,12 +273,14 @@ static int
273273
dtls1_process_buffered_record(SSL *s)
274274
{
275275
/* Check if epoch is current. */
276-
if (D1I(s)->unprocessed_rcds.epoch != D1I(s)->r_epoch)
276+
if (D1I(s)->unprocessed_rcds.epoch !=
277+
tls12_record_layer_read_epoch(s->internal->rl))
277278
return (0);
278279

279280
/* Update epoch once all unprocessed records have been processed. */
280281
if (pqueue_peek(D1I(s)->unprocessed_rcds.q) == NULL) {
281-
D1I(s)->unprocessed_rcds.epoch = D1I(s)->r_epoch + 1;
282+
D1I(s)->unprocessed_rcds.epoch =
283+
tls12_record_layer_read_epoch(s->internal->rl) + 1;
282284
return (0);
283285
}
284286

@@ -858,7 +860,7 @@ dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
858860
/* this may just be a stale retransmit */
859861
if (!dtls1_get_message_header(rr->data, &msg_hdr))
860862
return -1;
861-
if (rr->epoch != D1I(s)->r_epoch) {
863+
if (rr->epoch != tls12_record_layer_read_epoch(s->internal->rl)) {
862864
rr->length = 0;
863865
goto start;
864866
}
@@ -1136,17 +1138,20 @@ dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap,
11361138
static DTLS1_BITMAP *
11371139
dtls1_get_bitmap(SSL *s, SSL3_RECORD_INTERNAL *rr, unsigned int *is_next_epoch)
11381140
{
1139-
uint16_t next_epoch = D1I(s)->r_epoch + 1;
1141+
uint16_t read_epoch, read_epoch_next;
11401142

11411143
*is_next_epoch = 0;
11421144

1145+
read_epoch = tls12_record_layer_read_epoch(s->internal->rl);
1146+
read_epoch_next = read_epoch + 1;
1147+
11431148
/* In current epoch, accept HM, CCS, DATA, & ALERT */
1144-
if (rr->epoch == D1I(s)->r_epoch)
1149+
if (rr->epoch == read_epoch)
11451150
return &D1I(s)->bitmap;
11461151

11471152
/* Only HM and ALERT messages can be from the next epoch */
1148-
else if (rr->epoch == next_epoch &&
1149-
(rr->type == SSL3_RT_HANDSHAKE || rr->type == SSL3_RT_ALERT)) {
1153+
if (rr->epoch == read_epoch_next &&
1154+
(rr->type == SSL3_RT_HANDSHAKE || rr->type == SSL3_RT_ALERT)) {
11501155
*is_next_epoch = 1;
11511156
return &D1I(s)->next_bitmap;
11521157
}
@@ -1157,7 +1162,6 @@ dtls1_get_bitmap(SSL *s, SSL3_RECORD_INTERNAL *rr, unsigned int *is_next_epoch)
11571162
void
11581163
dtls1_reset_read_seq_numbers(SSL *s)
11591164
{
1160-
D1I(s)->r_epoch++;
11611165
memcpy(&(D1I(s)->bitmap), &(D1I(s)->next_bitmap), sizeof(DTLS1_BITMAP));
11621166
memset(&(D1I(s)->next_bitmap), 0, sizeof(DTLS1_BITMAP));
11631167
}

lib/libssl/dtls_locl.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: dtls_locl.h,v 1.4 2021/07/26 03:17:38 jsing Exp $ */
1+
/* $OpenBSD: dtls_locl.h,v 1.5 2021/08/30 19:12:25 jsing Exp $ */
22
/*
33
* DTLS implementation written by Nagendra Modadugu
44
* (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
@@ -132,13 +132,6 @@ typedef struct dtls1_state_internal_st {
132132
unsigned char rcvd_cookie[DTLS1_COOKIE_LENGTH];
133133
unsigned int cookie_len;
134134

135-
/*
136-
* The current data and handshake epoch. This is initially
137-
* undefined, and starts at zero once the initial handshake is
138-
* completed
139-
*/
140-
unsigned short r_epoch;
141-
142135
/* records being received in the current epoch */
143136
DTLS1_BITMAP bitmap;
144137

lib/libssl/ssl_locl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: ssl_locl.h,v 1.356 2021/07/26 03:17:38 jsing Exp $ */
1+
/* $OpenBSD: ssl_locl.h,v 1.357 2021/08/30 19:12:25 jsing Exp $ */
22
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
33
* All rights reserved.
44
*
@@ -641,7 +641,7 @@ void tls12_record_layer_set_version(struct tls12_record_layer *rl,
641641
uint16_t version);
642642
void tls12_record_layer_set_initial_epoch(struct tls12_record_layer *rl,
643643
uint16_t epoch);
644-
uint16_t tls12_record_layer_initial_epoch(struct tls12_record_layer *rl);
644+
uint16_t tls12_record_layer_read_epoch(struct tls12_record_layer *rl);
645645
uint16_t tls12_record_layer_write_epoch(struct tls12_record_layer *rl);
646646
int tls12_record_layer_use_write_epoch(struct tls12_record_layer *rl,
647647
uint16_t epoch);

lib/libssl/tls12_record_layer.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: tls12_record_layer.c,v 1.33 2021/08/30 19:00:49 jsing Exp $ */
1+
/* $OpenBSD: tls12_record_layer.c,v 1.34 2021/08/30 19:12:25 jsing Exp $ */
22
/*
33
* Copyright (c) 2020 Joel Sing <jsing@openbsd.org>
44
*
@@ -296,9 +296,9 @@ tls12_record_layer_set_initial_epoch(struct tls12_record_layer *rl,
296296
}
297297

298298
uint16_t
299-
tls12_record_layer_initial_epoch(struct tls12_record_layer *rl)
299+
tls12_record_layer_read_epoch(struct tls12_record_layer *rl)
300300
{
301-
return rl->initial_epoch;
301+
return rl->read->epoch;
302302
}
303303

304304
uint16_t
@@ -580,6 +580,10 @@ tls12_record_layer_change_read_cipher_state(struct tls12_record_layer *rl,
580580

581581
/* Read sequence number gets reset to zero. */
582582

583+
/* DTLS epoch is incremented and is permitted to wrap. */
584+
if (rl->dtls)
585+
read_new->epoch = rl->read_current->epoch + 1;
586+
583587
if (!tls12_record_layer_change_cipher_state(rl, read_new, 0,
584588
mac_key, key, iv))
585589
goto err;

0 commit comments

Comments
 (0)