FileTpm

Struct FileTpm 

Source
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

Source

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.

Source

pub fn open_default() -> Result<Self, FileTpmError>

Open the default TPM at $HOME/.ndn/ndnsec-key-file/, mirroring ndn-cxx BackEndFile’s default constructor.

Source

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.

Source

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#1 RSAPrivateKey
  • EcdsaP256 → SEC1 ECPrivateKey
  • Ed25519 → PKCS#8 PrivateKeyInfo

The bytes are base64-encoded and written with 0o400.

Source

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.

Source

pub fn delete(&self, key_name: &Name) -> Result<(), FileTpmError>

Delete a key file (whichever form exists).

Source

pub fn has_key(&self, key_name: &Name) -> bool

Check whether a key exists in the TPM.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V