Clean up dtls1_reset_seq_numbers().
authorjsing <jsing@openbsd.org>
Sun, 2 May 2021 17:18:10 +0000 (17:18 +0000)
committerjsing <jsing@openbsd.org>
Sun, 2 May 2021 17:18:10 +0000 (17:18 +0000)
Rather than doing flag gymnastics, split dtls1_reset_seq_numbers() into
separate read and write functions. Move the calls of these functions into
tls1_change_cipher_state() so they directly follow the change of cipher
state in the record layer, which avoids having to duplicate the calls in
the client and server.

ok inoguchi@ tb@

lib/libssl/d1_pkt.c
lib/libssl/ssl_clnt.c
lib/libssl/ssl_locl.h
lib/libssl/ssl_srvr.c
lib/libssl/t1_enc.c

index 7f4261e..4cb26d7 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: d1_pkt.c,v 1.93 2021/02/20 14:14:16 tb Exp $ */
+/* $OpenBSD: d1_pkt.c,v 1.94 2021/05/02 17:18:10 jsing Exp $ */
 /*
  * DTLS implementation written by Nagendra Modadugu
  * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
@@ -869,9 +869,6 @@ dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
                if (!ssl3_do_change_cipher_spec(s))
                        goto err;
 
-               /* do this whenever CCS is processed */
-               dtls1_reset_seq_numbers(s, SSL3_CC_READ);
-
                goto start;
        }
 
@@ -1219,15 +1216,16 @@ dtls1_get_bitmap(SSL *s, SSL3_RECORD_INTERNAL *rr, unsigned int *is_next_epoch)
 }
 
 void
-dtls1_reset_seq_numbers(SSL *s, int rw)
+dtls1_reset_read_seq_numbers(SSL *s)
 {
-       if (rw & SSL3_CC_READ) {
-               D1I(s)->r_epoch++;
-               memcpy(&(D1I(s)->bitmap), &(D1I(s)->next_bitmap),
-                   sizeof(DTLS1_BITMAP));
-               memset(&(D1I(s)->next_bitmap), 0, sizeof(DTLS1_BITMAP));
-       } else {
-               D1I(s)->w_epoch++;
-               tls12_record_layer_set_write_epoch(s->internal->rl, D1I(s)->w_epoch);
-       }
+       D1I(s)->r_epoch++;
+       memcpy(&(D1I(s)->bitmap), &(D1I(s)->next_bitmap), sizeof(DTLS1_BITMAP));
+       memset(&(D1I(s)->next_bitmap), 0, sizeof(DTLS1_BITMAP));
+}
+
+void
+dtls1_reset_write_seq_numbers(SSL *s)
+{
+       D1I(s)->w_epoch++;
+       tls12_record_layer_set_write_epoch(s->internal->rl, D1I(s)->w_epoch);
 }
index a38d1f1..022efd8 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: ssl_clnt.c,v 1.94 2021/04/30 19:26:44 jsing Exp $ */
+/* $OpenBSD: ssl_clnt.c,v 1.95 2021/05/02 17:18:10 jsing Exp $ */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
@@ -486,10 +486,6 @@ ssl3_connect(SSL *s)
                                ret = -1;
                                goto end;
                        }
-
-                       if (SSL_is_dtls(s))
-                               dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
-
                        break;
 
                case SSL3_ST_CW_FINISHED_A:
index c55dada..38b6838 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: ssl_locl.h,v 1.339 2021/04/30 19:26:44 jsing Exp $ */
+/* $OpenBSD: ssl_locl.h,v 1.340 2021/05/02 17:18:10 jsing Exp $ */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
@@ -1293,7 +1293,8 @@ void dtls1_clear_record_buffer(SSL *s);
 int dtls1_get_message_header(unsigned char *data,
     struct hm_header_st *msg_hdr);
 void dtls1_get_ccs_header(unsigned char *data, struct ccs_header_st *ccs_hdr);
-void dtls1_reset_seq_numbers(SSL *s, int rw);
+void dtls1_reset_read_seq_numbers(SSL *s);
+void dtls1_reset_write_seq_numbers(SSL *s);
 struct timeval* dtls1_get_timeout(SSL *s, struct timeval* timeleft);
 int dtls1_check_timeout_num(SSL *s);
 int dtls1_handle_timeout(SSL *s);
index 32ffa88..2d1af2f 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: ssl_srvr.c,v 1.105 2021/04/30 19:26:45 jsing Exp $ */
+/* $OpenBSD: ssl_srvr.c,v 1.106 2021/05/02 17:18:10 jsing Exp $ */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
@@ -659,9 +659,6 @@ ssl3_accept(SSL *s)
                                ret = -1;
                                goto end;
                        }
-
-                       if (SSL_is_dtls(s))
-                               dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
                        break;
 
                case SSL3_ST_SW_FINISHED_A:
index 642c210..6cdae0c 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: t1_enc.c,v 1.140 2021/04/30 19:26:45 jsing Exp $ */
+/* $OpenBSD: t1_enc.c,v 1.141 2021/05/02 17:18:10 jsing Exp $ */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
@@ -369,12 +369,16 @@ tls1_change_cipher_state(SSL *s, int which)
                if (!tls12_record_layer_change_read_cipher_state(s->internal->rl,
                    mac_secret, mac_secret_size, key, key_len, iv, iv_len))
                        goto err;
+               if (SSL_is_dtls(s))
+                       dtls1_reset_read_seq_numbers(s);
                tls12_record_layer_read_cipher_hash(s->internal->rl,
                    &s->enc_read_ctx, &s->read_hash);
        } else {
                if (!tls12_record_layer_change_write_cipher_state(s->internal->rl,
                    mac_secret, mac_secret_size, key, key_len, iv, iv_len))
                        goto err;
+               if (SSL_is_dtls(s))
+                       dtls1_reset_write_seq_numbers(s);
        }
        return (1);