]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Update epochs when changing key and cipher state for dtls 1.3
authorFrederik Wedel-Heinen <frederik.wedel-heinen@dencrypt.dk>
Mon, 8 Jan 2024 12:44:08 +0000 (13:44 +0100)
committerTomas Mraz <tomas@openssl.org>
Thu, 2 Oct 2025 12:47:23 +0000 (14:47 +0200)
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/23229)

doc/designs/dtlsv1_3/dtlsv1_3-main.md
ssl/record/rec_layer_d1.c
ssl/tls13_enc.c
test/recipes/70-test_dtls13epoch.t [new file with mode: 0644]
test/recipes/70-test_tls13alerts.t
test/tls13secretstest.c

index f66502377aecb14219e79e7ea75b505e41f5a514..da280ea127ed8c6126b79928ee855ce439fdf71f 100644 (file)
@@ -65,6 +65,11 @@ This is enforced by the macro `SSL_CONNECTION_MIDDLEBOX_IS_ENABLED(sc)`.
 The DTLSv1.3 implementation uses the label "dtls1.3" as described by RFC9147
 section 5.9.
 
+#### DTLS 1.3 Epoch
+
+The DTLSv1.3 implementation modifies the epoch according to RFC9147 section 6.1
+for DTLSv1.3 connections.
+
 Implementation progress
 -----------------------
 
@@ -83,7 +88,6 @@ is not covered by these workitems and must be implemented separately.
 | ACK messages                                        | -              |
 | Use HelloRetryRequest instead of HelloVerifyRequest | #22985, #22400 |
 | Message transcript                                  | -              |
-| DTLSv1.3 epoch                                      | #23553         |
 | ClientHello                                         | #23320         |
 | EndOfEarlyData message                              | -              |
 | Variable length header                              | -              |
@@ -129,13 +133,6 @@ And
 
 > 4.2.3. Record Number Encryption
 
-#### DTLSv1.3 epoch
-
-The epoch is maintained differently from DTLS 1.2
-
-> The DTLS epoch ...  is set as the least significant 2 octets of the connection
-> epoch, which is an 8 octet counter incremented on every KeyUpdate
-
 #### ClientHello
 
 DTLS adds legacy_cookie which has a forced value. And there are changes to the
index a66bd948c746e0ab74daf50a504037a01f687997..883f3ae9944a338ee153a6350dcf734655ed9180 100644 (file)
@@ -508,7 +508,7 @@ int dtls1_read_bytes(SSL *s, uint8_t type, uint8_t *recvd_type,
          * This may just be a stale retransmit. Also sanity check that we have
          * at least enough record bytes for a message header
          */
-        if (rr->epoch != sc->rlayer.d->r_epoch
+        if (rr->epoch != dtls1_get_epoch(sc, SSL3_CC_READ)
                 || rr->length < DTLS1_HM_HEADER_LENGTH) {
             if (!ssl_release_record(sc, rr, 0))
                 return -1;
index 2768e0ee9b85b0c56981cf873bbece99cb99898d..ec3cf21d5fbbff8ea71ab716f1b85cfecbe3eb46 100644 (file)
@@ -785,6 +785,17 @@ int tls13_change_cipher_state(SSL_CONNECTION *s, int which)
             dtls1_clear_received_buffer(s);
         else
             dtls1_clear_sent_buffer(s);
+
+        dtls1_increment_epoch(s, which);
+
+        if (level == OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE
+                && dtls1_get_epoch(s, which) == 1) {
+            /*
+             * We must manually increment epoch because
+             * client early traffic was not sent/recv
+             */
+            dtls1_increment_epoch(s, which);
+        }
     }
 
     if (!ssl_set_new_record_layer(s, s->version,
@@ -825,6 +836,7 @@ int tls13_update_key(SSL_CONNECTION *s, int sending)
     int ret = 0, l;
     int direction = sending ? OSSL_RECORD_DIRECTION_WRITE
                             : OSSL_RECORD_DIRECTION_READ;
+    int which = sending ? SSL3_CC_WRITE : SSL3_CC_READ;
     unsigned char iv_intern[EVP_MAX_IV_LENGTH];
     unsigned char *iv = iv_intern;
 
@@ -852,6 +864,9 @@ int tls13_update_key(SSL_CONNECTION *s, int sending)
 
     memcpy(insecret, secret, hashlen);
 
+    if (SSL_CONNECTION_IS_DTLS(s))
+        dtls1_increment_epoch(s, which);
+
     if (!ssl_set_new_record_layer(s, s->version,
                             direction,
                             OSSL_RECORD_PROTECTION_LEVEL_APPLICATION,
diff --git a/test/recipes/70-test_dtls13epoch.t b/test/recipes/70-test_dtls13epoch.t
new file mode 100644 (file)
index 0000000..663067b
--- /dev/null
@@ -0,0 +1,121 @@
+#! /usr/bin/env perl
+# Copyright 2024 The OpenSSL Project Authors. All Rights Reserved.
+#
+# Licensed under the Apache License 2.0 (the "License").  You may not use
+# this file except in compliance with the License.  You can obtain a copy
+# in the file LICENSE in the source distribution or at
+# https://www.openssl.org/source/license.html
+
+use strict;
+use feature 'state';
+
+use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
+use OpenSSL::Test::Utils;
+use TLSProxy::Proxy;
+use TLSProxy::Message;
+use Cwd qw(abs_path);
+
+my $test_name = "test_dtls13epoch";
+setup($test_name);
+
+plan skip_all => "DTLSProxy isn't usable on $^O"
+    if ($^O =~ /^(VMS)$/) || ($^O =~ /^(MSWin32)$/);
+
+plan skip_all => "$test_name needs the module feature enabled"
+    if disabled("module");
+
+plan skip_all => "$test_name needs the sock feature enabled"
+    if disabled("sock");
+
+# TODO(DTLSv1.3): Implement support for partial messages for DTLS
+plan skip_all => "DTLSProxy does not support partial messages"
+    if disabled("ec");
+
+plan skip_all => "$test_name needs DTLSv1.3 enabled"
+    if disabled("dtls1_3");
+
+$ENV{OPENSSL_MODULES} = abs_path(bldtop_dir("test"));
+
+my $proxy = TLSProxy::Proxy->new_dtls(
+    undef,
+    cmdstr(app(["openssl"]), display => 1),
+    srctop_file("apps", "server.pem"),
+    (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
+);
+
+plan tests => 1;
+
+my $epoch_check_failed;
+my $latest_epoch;
+
+#Test 1: Check that epoch is incremented as expected during a handshake
+$epoch_check_failed = 0;
+$latest_epoch = 0;
+$proxy->serverflags("-min_protocol DTLSv1.3 -max_protocol DTLSv1.3");
+$proxy->clientflags("-min_protocol DTLSv1.3 -max_protocol DTLSv1.3");
+$proxy->filter(\&current_record_epoch_filter);
+TLSProxy::Message->successondata(1);
+skip "TLS Proxy did not start", 1 if !$proxy->start();
+ok(!$epoch_check_failed
+   && $latest_epoch == 3, "Epoch changes correctly during handshake");
+
+sub current_record_epoch_filter
+{
+    my $records = $proxy->record_list;
+    my $latest_record = @{$records}[-1];
+    my $epoch = $latest_record->epoch;
+    my @badmessagetypes = undef;
+
+    $latest_epoch = $epoch;
+
+    if ($epoch == 0) {
+        @badmessagetypes = (
+            TLSProxy::Message::MT_NEW_SESSION_TICKET,
+            TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS,
+            TLSProxy::Message::MT_CERTIFICATE,
+            TLSProxy::Message::MT_SERVER_KEY_EXCHANGE,
+            TLSProxy::Message::MT_CERTIFICATE_REQUEST,
+            TLSProxy::Message::MT_SERVER_HELLO_DONE,
+            TLSProxy::Message::MT_CERTIFICATE_VERIFY,
+            TLSProxy::Message::MT_CLIENT_KEY_EXCHANGE,
+            TLSProxy::Message::MT_FINISHED,
+            TLSProxy::Message::MT_CERTIFICATE_STATUS,
+            TLSProxy::Message::MT_COMPRESSED_CERTIFICATE,
+            TLSProxy::Message::MT_NEXT_PROTO
+        );
+    } elsif ($epoch == 1) {
+        @badmessagetypes = (
+            TLSProxy::Message::MT_NEW_SESSION_TICKET,
+            TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS,
+            TLSProxy::Message::MT_CERTIFICATE,
+            TLSProxy::Message::MT_SERVER_KEY_EXCHANGE,
+            TLSProxy::Message::MT_CERTIFICATE_REQUEST,
+            TLSProxy::Message::MT_SERVER_HELLO_DONE,
+            TLSProxy::Message::MT_CERTIFICATE_VERIFY,
+            TLSProxy::Message::MT_CLIENT_KEY_EXCHANGE,
+            TLSProxy::Message::MT_FINISHED,
+            TLSProxy::Message::MT_CERTIFICATE_STATUS,
+            TLSProxy::Message::MT_COMPRESSED_CERTIFICATE,
+            TLSProxy::Message::MT_NEXT_PROTO
+        );
+
+    } elsif ($epoch == 2) {
+        @badmessagetypes = (
+            TLSProxy::Message::MT_NEW_SESSION_TICKET,
+            TLSProxy::Message::MT_CERTIFICATE_STATUS,
+            TLSProxy::Message::MT_COMPRESSED_CERTIFICATE,
+            TLSProxy::Message::MT_NEXT_PROTO
+        );
+    }
+
+    # Check that message types are acceptable
+    foreach (@{$proxy->message_list})
+    {
+        my $mt = $_->mt;
+
+        if (grep(/^$mt$/, @badmessagetypes)) {
+            print "Did not expect $mt in epoch $latest_epoch\n";
+            $epoch_check_failed = 1;
+        }
+    }
+}
index 99ec6e0b7a55d90e77903b0d44a1abce9183c214..f3fc5f8464eedc0c17a5426cda31e0e058433130 100644 (file)
@@ -70,15 +70,21 @@ sub run_tests
         );
     }
 
-    #Test 1: We test that a server can handle an unencrypted alert when normally the
-    #        next message is encrypted
-    $proxy->clear();
-    $proxy->filter(\&alert_filter);
-    $proxy_start_success = $proxy->start();
-    skip "TLSProxy did not start correctly", $testcount if $proxy_start_success == 0;
-
-    my $alert = TLSProxy::Message->alert();
-    ok(TLSProxy::Message->fail() && !$alert->server() && !$alert->encrypted(), "Client sends an unencrypted alert");
+    SKIP: {
+        skip "TODO(DTLSv1.3): Test fails because client sends alert with 0 epoch but"
+            . " the server increments the epoch after sending ServerHello and thus"
+            . " does not accept the alert message.",
+            $testcount if $run_test_as_dtls == 1;
+        #Test 1: We test that a server can handle an unencrypted alert when normally the
+        #        next message is encrypted
+        $proxy->clear();
+        $proxy->filter(\&alert_filter);
+        $proxy_start_success = $proxy->start();
+        skip "TLSProxy did not start correctly", $testcount if $proxy_start_success == 0;
+
+        my $alert = TLSProxy::Message->alert();
+        ok(TLSProxy::Message->fail() && !$alert->server() && !$alert->encrypted(), "Client sends an unencrypted alert");
+    }
 }
 
 sub alert_filter
index 9805aa98267e9fb47d45e5ae5c8bcf9f8c9dad8a..7a5cbcf2a4f541ca416cf16190a37c65c0e37145 100644 (file)
@@ -245,6 +245,15 @@ void dtls1_clear_sent_buffer(SSL_CONNECTION *s)
 {
 }
 
+uint16_t dtls1_get_epoch(SSL_CONNECTION *s, int rw)
+{
+    return 0;
+}
+
+void dtls1_increment_epoch(SSL_CONNECTION *s, int rw)
+{
+}
+
 /* End of mocked out code */
 
 static int test_secret(SSL_CONNECTION *s, unsigned char *prk,