pub struct FileTpm { /* private fields */ }Expand description
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.
Implementations§
Source§impl FileTpm
impl FileTpm
Sourcepub fn open(root: impl AsRef<Path>) -> Result<Self, FileTpmError>
pub fn open(root: impl AsRef<Path>) -> Result<Self, FileTpmError>
Open or create a TPM at the given directory. Creates the
directory tree (with 0o700 permissions) if absent.
Sourcepub fn open_default() -> Result<Self, FileTpmError>
pub fn open_default() -> Result<Self, FileTpmError>
Open the default TPM at $HOME/.ndn/ndnsec-key-file/, mirroring
ndn-cxx BackEndFile’s default constructor.
Sourcepub fn locator(&self) -> String
pub fn locator(&self) -> String
Locator string the PIB persists for this TPM. Matches ndn-cxx’s
canonical form: tpm-file: for the default location, or
tpm-file:<absolute-path> for a custom one. ndn-cxx’s
parseAndCheckTpmLocator rejects mismatches at KeyChain open
time, so writing the wrong string here will break interop.
Sourcepub fn save_raw(
&self,
key_name: &Name,
kind: TpmKeyKind,
der: &[u8],
) -> Result<(), FileTpmError>
pub fn save_raw( &self, key_name: &Name, kind: TpmKeyKind, der: &[u8], ) -> Result<(), FileTpmError>
Save raw DER bytes for a key. The DER must already be in the
algorithm’s canonical form for kind:
Rsa→ PKCS#1RSAPrivateKeyEcdsaP256→ SEC1ECPrivateKeyEd25519→ PKCS#8PrivateKeyInfo
The bytes are base64-encoded and written with 0o400.
Sourcepub fn load_raw(
&self,
key_name: &Name,
) -> Result<(TpmKeyKind, Vec<u8>), FileTpmError>
pub fn load_raw( &self, key_name: &Name, ) -> Result<(TpmKeyKind, Vec<u8>), FileTpmError>
Load raw DER bytes for a key. Tries the .privkey file first
(RSA / ECDSA), then .privkey-ed25519. Returns the kind alongside
the bytes so callers can dispatch on algorithm.
Sourcepub fn delete(&self, key_name: &Name) -> Result<(), FileTpmError>
pub fn delete(&self, key_name: &Name) -> Result<(), FileTpmError>
Delete a key file (whichever form exists).
Sourcepub fn generate_ed25519(
&self,
key_name: &Name,
) -> Result<[u8; 32], FileTpmError>
pub fn generate_ed25519( &self, key_name: &Name, ) -> Result<[u8; 32], FileTpmError>
Generate a fresh Ed25519 key, persist it under the sentinel
suffix, and return the 32-byte raw seed. Callers that want a
Signer should pass the seed to Ed25519Signer::from_seed.
Sourcepub fn sign(
&self,
key_name: &Name,
region: &[u8],
) -> Result<Bytes, FileTpmError>
pub fn sign( &self, key_name: &Name, region: &[u8], ) -> Result<Bytes, FileTpmError>
Sign region with the key stored under key_name. Returns raw
signature bytes. Algorithm is determined by which file form
exists on disk.
Sourcepub fn public_key(&self, key_name: &Name) -> Result<Vec<u8>, FileTpmError>
pub fn public_key(&self, key_name: &Name) -> Result<Vec<u8>, FileTpmError>
Derive the public key bytes for key_name. Format matches what
the PIB’s key_bits column expects: SubjectPublicKeyInfo DER
for RSA / ECDSA, raw 32-byte key for Ed25519.
Sourcepub fn export_to_safebag(
&self,
key_name: &Name,
certificate: Bytes,
password: &[u8],
) -> Result<SafeBag, SafeBagError>
pub fn export_to_safebag( &self, key_name: &Name, certificate: Bytes, password: &[u8], ) -> Result<SafeBag, SafeBagError>
Export key_name as a crate::safe_bag::SafeBag for transfer
to another machine. Bundles the password-encrypted private key
with the certificate the caller looked up from the PIB.
The on-disk private key is converted to an unencrypted PKCS#8
PrivateKeyInfo first (RSA goes PKCS#1 → PKCS#8, ECDSA goes
SEC1 → PKCS#8, Ed25519 is already PKCS#8 on disk) and then
encrypted via PBES2 + PBKDF2-HMAC-SHA256 + AES-256-CBC inside
the rustcrypto pkcs8 crate’s encrypt method. The resulting
EncryptedPrivateKeyInfo is wire-compatible with what
ndnsec export and OpenSSL i2d_PKCS8PrivateKey_bio produce.
Caveat: Ed25519 SafeBags roundtrip ndn-rs ↔ ndn-rs but not
to ndn-cxx, because ndn-cxx tpm-file has no Ed25519 path
regardless of how the bytes arrive on disk
(back-end-file.cpp:130-139 rejects Ed25519 at the algorithm
switch). RSA and ECDSA-P256 SafeBags roundtrip with ndnsec
in both directions.
Sourcepub fn import_from_safebag(
&self,
safebag: &SafeBag,
key_name: &Name,
password: &[u8],
) -> Result<Bytes, SafeBagError>
pub fn import_from_safebag( &self, safebag: &SafeBag, key_name: &Name, password: &[u8], ) -> Result<Bytes, SafeBagError>
Import a crate::safe_bag::SafeBag as a stored private key
under key_name. Decrypts the embedded EncryptedPrivateKeyInfo
with password, dispatches on the PKCS#8 algorithm OID to
pick the on-disk format, converts back to the FileTpm form
(PKCS#1 / SEC1 / PKCS#8), and writes it.
Returns the certificate Data wire bytes from the SafeBag so the caller can insert them into their PIB. FileTpm itself does not store certs — the certificate side of the bag is the PIB’s responsibility.
key_name is an explicit argument because the SafeBag does
not record where the key should land in any particular PIB —
the caller is expected to extract it from the certificate’s
Name (typically a prefix of the cert name) and pass it in.