Bundle Protocol Security (RFC 9172)
README.md

bpsec #

Bundle Protocol Security (RFC 9172) for OCaml.

Delay-tolerant networks, such as the links between a spacecraft, a relay
satellite and a ground station, carry data as bundles that wait at each hop
until the next link opens, sometimes for hours. With no end-to-end session
to run TLS in, the security has to travel with the bundle.

A Bundle Protocol v7 bundle (RFC 9171, 4.2 and 4.3) is a CBOR array of
blocks: one primary block (version, flags, source and destination endpoint
IDs, creation timestamp, lifetime) followed by numbered canonical blocks,
of which the payload block is number 1; every other canonical block is an
extension block. An endpoint ID such as ipn:2.1 names service 1 on node 2,
written Bundle.Ipn (2L, 1L) in the example further down. BPSec protects the
blocks of a bundle with two types of extension block.

  • Block Integrity Block (BIB) is an HMAC over a target block. It covers
    the Integrity-Protected Plaintext of RFC 9173, 3.7: the integrity scope
    flags; then, as those flags select, the canonical CBOR of the primary
    block, of the target block's header and of the BIB's own header; and last
    the target block's data. The flags thus decide whether rewriting the
    bundle's addresses breaks the check.
  • Block Confidentiality Block (BCB) encrypts a target block's data with
    AES-GCM, using the headers the scope flags select as additional
    authenticated data.

Features #

  • All of RFC 9172: the BIB and BCB blocks
  • RFC 9173 default security contexts:
    • BIB-HMAC-SHA2 (SHA-256, SHA-384, SHA-512)
    • BCB-AES-GCM (A128GCM, A256GCM)
  • Keys sent wrapped under a key-encryption key (KEK) with AES Key Wrap
    (RFC 3394), in the Wrapped Key parameter (RFC 9173, Sections 3.3.2 and
    4.3.3). ~kek produces them, and verification and decryption unwrap them
    with the KEK (RFC 9173, Appendix A.2)
  • BIBs whose target is the primary block (RFC 9173, Appendix A.3)
  • CBOR encoding and decoding
  • Integration with the bundle library
  • Anti_replay, a replay cache sized for DTN delays, kept over a time window

Replay protection #

The BIB of RFC 9172 tells a node that a bundle is intact and says nothing
about replay. In a DTN, store-and-forward takes hours to days, so a window
counted in frames does not work. SDLS (CCSDS 355.0), the security protocol of
space data links, accepts a frame only if its sequence number falls in a
fixed window just above the last one seen, which assumes that frames arrive
within a few places of the order they were sent in. A bundle can wait days
at a relay and arrive long after bundles sent later. Bpsec.Anti_replay
remembers instead which bundle identities it has
seen within a window measured in time: 7 days by default, with 5 minutes of
tolerance for clocks that disagree and at most 65,536 entries. It provides
check, record and evict over an explicit t, configured by max_age,
clock_skew and max_entries, with encode and decode to persist it.

The cache is keyed on a bundle id that must come from data the BIB
authenticated, never on the security source of the abstract security block,
which a BIB does not cover: Bpsec.replay_id builds one from the BIB's
integrity results.

verify_integrity does not call it. What to do on Reject_replay or
Reject_too_old (drop the bundle, log it, report it) is up to the node, and
this package has no node loop. A Bundle Protocol Agent that calls
verify_integrity should take replay_id of the received bundle, call
Anti_replay.check before verify_integrity and record after it. That
belongs in a DTN forwarding engine; the ingress step of
ocaml-cgr does exactly
this.

Installation #

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam install bpsec

Usage #

let () = Crypto_rng_unix.use_default ()

(* A bundle with a single payload block (number 1). *)
let bundle : Bundle.t =
  Bundle.v
    ~source:(Bundle.Ipn (2L, 1L))
    ~destination:(Bundle.Ipn (1L, 2L))
    ~creation_timestamp:{ Bundle.time = 0L; seq = 0L }
    ~payload:"secret payload" ()

let source = Bundle.Ipn (2L, 1L)
let hmac_key = String.make 32 '\x42'
let aes_key = String.make 32 '\x11'

(* Add a Block Integrity Block over the payload, then verify and strip it. *)
let protected = Result.get_ok (Bpsec.add_integrity ~target:1 ~source ~key:hmac_key bundle)
let () = assert (Result.is_ok (Bpsec.verify_integrity ~key:hmac_key protected))

(* Encrypt the payload under a Block Confidentiality Block, then decrypt. *)
let encrypted = Result.get_ok (Bpsec.add_confidentiality ~target:1 ~source ~key:aes_key bundle)
let recovered = Result.get_ok (Bpsec.remove_confidentiality ~key:aes_key encrypted)
let () = assert (Bundle.payload recovered = Some "secret payload")
  • ION - NASA/JPL DTN with BPSec
  • µD3TN - Lightweight DTN implementation
  • DTN7-go - Go BPv7 with BPSec support

References #

Both texts are kept under spec/, so a section that the code cites
can be read next to it. spec/SOURCES.md records where
each text came from and its digest.

Licence #

ISC. See LICENSE.md.