Commit 047bba53a3 for frr

commit 047bba53a31e627a32ee907f007ff22bed22dba3
Author: Abdul Wasey <w453y.me@gmail.com>
Date:   Fri Oct 30 18:01:29 2026 +0000

    tests: cover keyed SHA1 with sha-1 keys in bfdd

    bfd_authentication_topo1: a session between a sha-1 and a hmac-sha-1
    key stays down, and comes up once both ends use sha-1, with meticulous
    on and off. Two bfdd agreeing does not show either follows the RFC,
    so rt1's packets are also captured on rt2 and their digest checked
    against one computed independently, as RFC 5880 Section 6.7.4 gives it.

    bfd_dplane_auth_topo1: the listener declares BFDDP_CAP_AUTH_RFC_SHA1
    and must receive a sha-1 key flagged and a hmac-sha-1 key not.

    bfd_dplane_caps_topo1: a session offloaded with a hmac-sha-1 key comes
    back to bfdd when the key moves to sha-1, since the listener did not
    declare BFDDP_CAP_AUTH_RFC_SHA1.

    Signed-off-by: Abdul Wasey <w453y.me@gmail.com>

diff --git a/tests/topotests/bfd_authentication_topo1/rt1/step4/show_bfd_peers_sha1_newkey_meticulous.ref b/tests/topotests/bfd_authentication_topo1/rt1/step4/show_bfd_peers_sha1_newkey_meticulous.ref
new file mode 100644
index 0000000000..5f77c93d8c
--- /dev/null
+++ b/tests/topotests/bfd_authentication_topo1/rt1/step4/show_bfd_peers_sha1_newkey_meticulous.ref
@@ -0,0 +1,15 @@
+[
+  {
+    "multihop": false,
+    "peer": "10.0.1.2",
+    "vrf": "default",
+    "interface": "eth-rt2",
+    "authentication": {
+      "enabled": true,
+      "key-chain-name": "kc1",
+      "key-algorithm-meticulous": true,
+      "cryptoName": "meticulous keyed-sha1"
+    },
+    "status": "down"
+  }
+]
diff --git a/tests/topotests/bfd_authentication_topo1/test_bfd_authentication_topo1.py b/tests/topotests/bfd_authentication_topo1/test_bfd_authentication_topo1.py
index c47956f532..3bfd6f7cf8 100644
--- a/tests/topotests/bfd_authentication_topo1/test_bfd_authentication_topo1.py
+++ b/tests/topotests/bfd_authentication_topo1/test_bfd_authentication_topo1.py
@@ -20,6 +20,8 @@ test_bfd_authentication_topo1.py:

 """

+import hashlib
+import hmac
 import os
 import sys
 import pytest
@@ -38,6 +40,18 @@ from lib.topolog import logger

 pytestmark = [pytest.mark.bfdd, pytest.mark.isisd]

+# Sniffs rt1's control packets in rt2's namespace with scapy, as tcpdump is
+# often missing in CI images. lfilter rather than a BPF filter, which scapy
+# compiles with tcpdump.
+SNIFF_CONTROL = """
+from scapy.all import IP, UDP, sniff
+pkts = sniff(iface="eth-rt1", count=5, timeout=10,
+             lfilter=lambda p: IP in p and UDP in p and p[UDP].dport == 3784
+             and p[IP].src == "10.0.1.1")
+for p in pkts:
+    print("bfd: " + bytes(p[UDP].payload).hex())
+"""
+

 def setup_module(mod):
     "Sets up the pytest environment"
@@ -790,6 +804,142 @@ def test_bfd_authentication_peer_keychain_removed():
         pytest.fail(tgen.errors)


+def test_bfd_authentication_rfc_sha1_against_hmac_sha1_down():
+    "Test BFD keyed SHA1 down when only one side uses the RFC 5880 digest"
+    logger.info(
+        "Test: BFD-AUTHEN keyed SHA1 down when rt1 uses sha-1 and rt2 hmac-sha-1"
+    )
+
+    tgen = get_topogen()
+    if not tgen.gears["rt1"].has_crypto_openssl():
+        pytest.skip("crypto-openssl disabled. skipping SHA1 test")
+
+    # sha-1 and hmac-sha-1 put different digests on the wire for the same
+    # key, so a session between them must not come up.
+    tgen.gears["rt1"].vtysh_cmd(
+        """
+        configure terminal
+        key chain kc1
+        key 40
+        cryptographic-algorithm sha-1
+        """
+    )
+
+    router_compare_json_output(
+        "rt1",
+        "show bfd peers json",
+        "step4/show_bfd_peers_sha1_newkey_meticulous.ref",
+        20,
+        1,
+    )
+
+
+def test_bfd_authentication_rfc_sha1_up():
+    "Test BFD keyed SHA1 up when both sides use the RFC 5880 digest"
+    logger.info("Test: BFD-AUTHEN keyed SHA1 up when rt1 and rt2 use sha-1")
+
+    tgen = get_topogen()
+    if not tgen.gears["rt1"].has_crypto_openssl():
+        pytest.skip("crypto-openssl disabled. skipping SHA1 test")
+
+    tgen.gears["rt2"].vtysh_cmd(
+        """
+        configure terminal
+        key chain kc1
+        key 40
+        cryptographic-algorithm sha-1
+        """
+    )
+
+    router_compare_json_output(
+        "rt1",
+        "show bfd peers json",
+        "step4/show_bfd_peers_2_sha1_newkey_meticulous.ref",
+        20,
+        1,
+    )
+
+
+def test_bfd_authentication_rfc_sha1_not_meticulous_up():
+    "Test BFD keyed SHA1 up with the RFC 5880 digest, meticulous off"
+    logger.info("Test: BFD-AUTHEN keyed SHA1 up with sha-1, meticulous off")
+
+    tgen = get_topogen()
+    if not tgen.gears["rt1"].has_crypto_openssl():
+        pytest.skip("crypto-openssl disabled. skipping SHA1 test")
+
+    tgen.gears["rt1"].vtysh_cmd(
+        """
+        configure terminal
+        bfd
+        profile 0
+        no authentication algorithm meticulous
+        """
+    )
+    tgen.gears["rt2"].vtysh_cmd(
+        """
+        configure terminal
+        bfd
+        peer 10.0.1.1 interface eth-rt1
+        no authentication algorithm meticulous
+        """
+    )
+
+    router_compare_json_output(
+        "rt1", "show bfd peers json", "step4/show_bfd_peers_2_sha1_newkey.ref", 20, 1
+    )
+
+
+def test_bfd_authentication_rfc_sha1_digest_on_the_wire():
+    """Test the keyed SHA1 digest rt1 sends is the one RFC 5880 describes.
+
+    Two bfdd agreeing with each other does not show that either follows
+    the RFC, so the digest is recomputed here from the captured packet:
+    the key, zero padded to 20 bytes, in place of the digest, then SHA1
+    over the whole packet (RFC 5880 Section 6.7.4).
+    """
+    logger.info("Test: BFD-AUTHEN keyed SHA1 digest matches RFC 5880 6.7.4")
+
+    tgen = get_topogen()
+    if not tgen.gears["rt1"].has_crypto_openssl():
+        pytest.skip("crypto-openssl disabled. skipping SHA1 test")
+
+    key = b"mysecret123"
+    rt2 = tgen.gears["rt2"]
+    script = os.path.join(tgen.logdir, "rt2", "sniff_control.py")
+    with open(script, "w") as f:
+        f.write(SNIFF_CONTROL)
+    output = rt2.run("python3 {} 2>&1".format(script))
+
+    # scapy may warn on stderr, which is kept for the failure message.
+    pkts = [
+        bytes.fromhex(line[5:])
+        for line in output.splitlines()
+        if line.startswith("bfd: ")
+    ]
+    assert len(pkts) == 5, "did not capture 5 of rt1's packets: {}".format(output)
+
+    for pkt in pkts:
+        # A 24 byte header, then type 4 (keyed SHA1), length 28, key ID,
+        # reserved, sequence number and the 20 byte digest.
+        assert pkt[1] & 0x04, "rt1 sent a packet without authentication"
+        assert (
+            len(pkt) == 52 and pkt[24] == 4 and pkt[25] == 28
+        ), "rt1 sent no keyed SHA1 section: {}".format(pkt.hex())
+        digest = pkt[32:52]
+
+        rfc = bytearray(pkt)
+        rfc[32:52] = key.ljust(20, b"\0")
+        assert (
+            hashlib.sha1(bytes(rfc)).digest() == digest
+        ), "digest is not the one RFC 5880 Section 6.7.4 gives: {}".format(pkt.hex())
+
+        # And not the HMAC a hmac-sha-1 key would give.
+        zeroed = bytearray(pkt)
+        zeroed[32:52] = bytes(20)
+        assert hmac.new(key, bytes(zeroed), hashlib.sha1).digest() != digest
+
+
 def test_memory_leak():
     "Run the memory leak test and report results."
     tgen = get_topogen()
diff --git a/tests/topotests/bfd_dplane_auth_topo1/test_bfd_dplane_auth_topo1.py b/tests/topotests/bfd_dplane_auth_topo1/test_bfd_dplane_auth_topo1.py
index f404dc536f..136feb7017 100644
--- a/tests/topotests/bfd_dplane_auth_topo1/test_bfd_dplane_auth_topo1.py
+++ b/tests/topotests/bfd_dplane_auth_topo1/test_bfd_dplane_auth_topo1.py
@@ -8,7 +8,8 @@

 """
 Test that a session's authentication keys reach the data plane with the
-lifetimes that say when each may be used.
+lifetimes that say when each may be used, and with the keyed SHA1 digest
+each one uses.

 The data plane picks the key, not the daemon: it has the packets, so it is
 the only side that can tell which key applies to one. That only works if
@@ -59,13 +60,14 @@ def setup_module(mod):
     dump_file = os.path.join(router.gearlogdir, "bfd_dplane.data")

     # The listener has to be accepting before bfdd starts, since bfdd
-    # connects to it as a client. It declares that it authenticates, or
-    # bfdd would keep the session instead of offloading it.
+    # connects to it as a client. It declares that it authenticates, and
+    # computes keyed SHA1 as RFC 5880 does, or bfdd would keep the session
+    # instead of offloading it.
     router.load_frr_config(
         daemons=[
             (
                 TopoRouter.RD_BFD_DPLANE_LISTENER,
-                "-c 0x1 -p {} -z {}".format(DPLANE_PORT, dump_file),
+                "-c 0x3 -p {} -z {}".format(DPLANE_PORT, dump_file),
             ),
             (TopoRouter.RD_ZEBRA, None),
             (
@@ -154,7 +156,7 @@ def _keys(dump):
     out = []
     pattern = re.compile(
         r"auth key: id (\d+) type (\d+) len (\d+) "
-        r"send (-?\d+) (-?\d+) accept (-?\d+) (-?\d+)"
+        r"send (-?\d+) (-?\d+) accept (-?\d+) (-?\d+) flags (\d+)"
     )
     for line in dump.splitlines():
         m = pattern.search(line)
@@ -169,6 +171,7 @@ def _keys(dump):
                     "send_end": n[4],
                     "accept_start": n[5],
                     "accept_end": n[6],
+                    "flags": n[7],
                 }
             )
     return out
@@ -319,6 +322,42 @@ def test_a_lifetime_edit_is_pushed():
     )


+def test_keyed_sha1_digest_is_flagged():
+    """Each keyed SHA1 key says which digest it uses.
+
+    "sha-1" is the digest of RFC 5880 Section 6.7.4 and "hmac-sha-1" an
+    HMAC over the packet. They differ on the wire, so the data plane has to
+    be told which one a key uses.
+    """
+    _skip_on_failure()
+
+    router = get_topogen().gears["r1"]
+    if not router.has_crypto_openssl():
+        pytest.skip("crypto-openssl disabled. skipping SHA1 test")
+
+    router.vtysh_cmd(
+        """
+        configure terminal
+        key chain rollover
+        key 5
+        key-string fifthkey00000005
+        cryptographic-algorithm sha-1
+        exit
+        key 6
+        key-string sixthkey00000006
+        cryptographic-algorithm hmac-sha-1
+        end
+        """
+    )
+
+    def _flags():
+        return {k["id"]: k["flags"] for k in _keys(_listener_dump())}
+
+    want = {1: 0, 2: 0, 3: 0, 4: 0, 5: 1, 6: 0}
+    _, result = topotest.run_and_expect(_flags, want, count=30, wait=1)
+    assert result == want, "the data plane holds keys with flags {}".format(result)
+
+
 def test_memory_leak():
     "Run the memory leak test and report results."
     tgen = get_topogen()
diff --git a/tests/topotests/bfd_dplane_caps_topo1/test_bfd_dplane_caps_topo1.py b/tests/topotests/bfd_dplane_caps_topo1/test_bfd_dplane_caps_topo1.py
index ef61330f76..bf40505a76 100644
--- a/tests/topotests/bfd_dplane_caps_topo1/test_bfd_dplane_caps_topo1.py
+++ b/tests/topotests/bfd_dplane_caps_topo1/test_bfd_dplane_caps_topo1.py
@@ -16,6 +16,9 @@ declaration, and the authenticating session must come back to bfdd while the
 other stays. Last, a key chain is given to the session still offloaded, and
 that one must come back too.

+The listener does not declare that it computes keyed SHA1 as RFC 5880 does,
+so a session whose key chain holds a "sha-1" key must stay in bfdd as well.
+
 The listener stands in for a data plane. It runs no BFD state machine, it
 records what it was sent.
 """
@@ -40,6 +43,7 @@ DPLANE_PORT = 50700

 AUTH_PEER = "192.168.1.2"
 PLAIN_PEER = "192.168.1.3"
+SHA1_PEER = "192.168.1.4"


 def setup_module(mod):
@@ -164,6 +168,59 @@ def test_both_sessions_offloaded():
     )


+def test_rfc_sha1_key_takes_the_session_back():
+    """A key using the RFC 5880 digest needs the data plane to declare it.
+
+    The session is first offloaded with an HMAC-SHA1 key, which the
+    listener can run, so it is known to reach the data plane at all. Its
+    key then moves to the RFC 5880 digest, which the listener did not
+    declare, and the session must come back to bfdd.
+    """
+    _skip_on_failure()
+
+    router = get_topogen().gears["r1"]
+    if not router.has_crypto_openssl():
+        pytest.skip("crypto-openssl disabled. skipping SHA1 test")
+
+    router.vtysh_cmd(
+        """
+        configure terminal
+        key chain sha1
+        key 1
+        key-string sha1key000000001
+        cryptographic-algorithm hmac-sha-1
+        exit
+        exit
+        bfd
+        peer {}
+        transmit-interval 1000
+        receive-interval 1000
+        authentication key-chain sha1
+        end
+        """.format(
+            SHA1_PEER
+        )
+    )
+    _expect_offloaded(
+        [AUTH_PEER, PLAIN_PEER, SHA1_PEER],
+        "the session with an HMAC-SHA1 key was not offloaded",
+    )
+
+    router.vtysh_cmd(
+        """
+        configure terminal
+        key chain sha1
+        key 1
+        cryptographic-algorithm sha-1
+        end
+        """
+    )
+    _expect_offloaded(
+        [AUTH_PEER, PLAIN_PEER],
+        "the session with an RFC 5880 SHA1 key was left with the data plane",
+    )
+
+
 def test_withdrawn_capability_takes_the_session_back():
     """Withdrawing the capability hands the authenticating session back.