Module file_tpm

Module file_tpm 

Source
Expand description

File-backed TPM (private-key store), wire-compatible with ndn-cxx’s tpm-file backend (path B + Ed25519 superset).

§Compatibility model

The on-disk format for RSA and ECDSA-P256 keys is bit-for-bit compatible with ndnsec. An ndn-rs binary writing an RSA or ECDSA key under this TPM produces a file ndnsec key-list and ndnsec sign can read, and vice versa. Pinned to ndn-cxx tag ndn-cxx-0.9.0, commit 0751bba8, file ndn-cxx/security/tpm/impl/back-end-file.cpp (lines 51–229).

Ed25519 is not supported by ndn-cxx tpm-file — its d2i_AutoPrivateKey path only autodetects RSA and EC from ASN.1 tags, and BackEndFile::createKey rejects anything else (back-end-file.cpp:130-139). To preserve Ed25519 as a first-class algorithm in ndn-rs without breaking ndn-cxx interop, this module stores Ed25519 keys with a sentinel filename suffix:

  • <HEX>.privkey → RSA / ECDSA, exactly as ndn-cxx writes
  • <HEX>.privkey-ed25519 → ndn-rs Ed25519 PKCS#8, ignored by ndnsec

ndn-cxx’s loader only opens *.privkey files and silently ignores the sentinel suffix; ndn-rs reads both. This is “path B” in the design discussion: superset compatibility, not strict.

§Storage rules (MUST match ndn-cxx for .privkey files)

  • Directory: $HOME/.ndn/ndnsec-key-file/. Honours TEST_HOME first, then HOME, then CWD. Created with 0o700 (ndn-cxx omits the explicit chmod but inherits umask; we set it explicitly because it’s the right thing).
  • Filename: hex(SHA256(key_name.wire_encode())).to_uppercase() plus .privkey (or .privkey-ed25519 for Ed25519). The hash input is the TLV wire encoding of the Name (outer type 0x07 + length
    • components), not the URI string. Easy to get wrong; the test filename_matches_known_hash asserts the format.
  • File body: base64 of the raw private-key DER, no PEM armor, no header, no encryption.
    • RSA → PKCS#1 RSAPrivateKey DER
    • ECDSA-P256 → SEC1 ECPrivateKey DER
    • Ed25519 (sentinel) → PKCS#8 PrivateKeyInfo DER
  • Permissions: per-file chmod 0o400 on save (read-only by owner, no write even by owner). back-end-file.cpp:228.

Public-key recovery is on demand from the loaded private key — there are no separate public-key files; the PIB references the public material via key_bits BLOBs.

Structs§

FileTpm
File-backed TPM. Stores private keys under <root>/<HEX>.privkey[-ed25519] files and reads them back on demand. All operations take &self; concurrent access is safe because each call performs an independent open/read/close.

Enums§

FileTpmError
Errors returned by FileTpm operations. Mapped to TrustError at the public boundary so callers don’t need to depend on this module’s type.
TpmKeyKind
Algorithm of a key stored in the TPM. Determined by file suffix and (for .privkey files) by ASN.1 autodetection of the inner DER.