Crates.io | tss-keyfile |
lib.rs | tss-keyfile |
version | 0.1.5 |
created_at | 2025-08-21 12:38:26.227596+00 |
updated_at | 2025-09-11 10:35:54.807496+00 |
description | Decoding of TPM 2.0 TSS Keyfiles from PEM/DER/(ASN.1) |
homepage | |
repository | https://codeberg.org/mbodmer/rust-tss-keyfile |
max_upload_size | |
id | 1804801 |
size | 60,252 |
Decoding of TPM 2.0 TSS Keyfiles from PEM/DER/(ASN.1)
Keyfiles as generated by e.g. tm2tss-genkey
Implements specification: https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
let pem: Vec<u8> = std::fs::read("keyfile.tss.pem").unwrap();
let key = TPMKey::try_from_pem(&pem).unwrap();
println!("{key}");
let der: Vec<u8> = std::fs::read("keyfile.tss.der").unwrap();
let key = TPMKey::try_from_der(&der).unwrap();
println!("{key}");
fn load_private_key(
tcti: TctiNameConf,
keyfile_path: &Path,
) -> Result<KeyHandle, Box<dyn std::error::Error>> {
// Load and parse the TSS2 keyfile
let pem: Vec<u8> = std::fs::read(keyfile_path).unwrap();
let tpmkey = tss_keyfile::TPMKey::try_from_pem(&pem).unwrap();
// unmarshall `TPM2B_PRIVATE, TPM2B_PUBLIC` - `OctetSting`s and the persistent parent handle
let privkey = Private::try_from(&tpmkey.privkey[2..])?;
let pubkey_buffer = PublicBuffer::unmarshall(&tpmkey.pubkey.to_vec())?;
let pubkey = Public::try_from(pubkey_buffer)?;
let parent = PersistentTpmHandle::new(tpmkey.parent)?;
// Create TPM context
let mut context = Context::new(tcti)?;
// Create ESYS handle mapping for the persistent parent handle
let mapped_parent_obj = context.tr_from_tpm_public(parent.into())?;
let mapped_parent_handle = KeyHandle::from(mapped_parent_obj);
// Set up session and load key
context.set_sessions((Some(AuthSession::Password), None, None));
let key_handle = context.load(mapped_parent_handle, privkey, pubkey)?;
Ok(key_handle)
}