use std::borrow::Cow;
use std::fmt;
use super::ByteBuf;
use crate::keytype::*;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Trusted;
impl KeyType for Trusted {
type Description = str;
type Payload = Payload;
fn name() -> &'static str {
"trusted"
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TpmHash {
Sha1,
Sha256,
Sha384,
Sha512,
Sm3_256,
}
impl TpmHash {
fn name(self) -> &'static str {
match self {
TpmHash::Sha1 => "sha1",
TpmHash::Sha256 => "sha256",
TpmHash::Sha384 => "sha384",
TpmHash::Sha512 => "sha512",
TpmHash::Sm3_256 => "sm3-256",
}
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct TrustedOptions {
pub keyhandle: Option<u32>,
pub keyauth: Option<[u8; 20]>,
pub blobauth: Option<[u8; 20]>,
pub pcrinfo: Option<Vec<u8>>,
pub pcrlock: Option<u32>,
pub migratable: Option<bool>,
pub hash: Option<TpmHash>,
pub policydigest: Option<Vec<u8>>,
pub policyhandle: Option<u32>,
}
impl fmt::Display for TrustedOptions {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(keyhandle) = self.keyhandle {
write!(f, " keyhandle={:x}", keyhandle)?;
}
if let Some(keyauth) = self.keyauth.as_ref() {
write!(f, " keyauth={:x}", ByteBuf(keyauth))?;
}
if let Some(blobauth) = self.blobauth.as_ref() {
write!(f, " blobauth={:x}", ByteBuf(blobauth))?;
}
if let Some(pcrinfo) = self.pcrinfo.as_ref() {
write!(f, " pcrinfo={:x}", ByteBuf(pcrinfo))?;
}
if let Some(pcrlock) = self.pcrlock {
write!(f, " pcrlock={}", pcrlock)?;
}
if let Some(migratable) = self.migratable {
write!(f, " migratable={}", migratable as u8)?;
}
if let Some(hash) = self.hash {
write!(f, " hash={}", hash.name())?;
}
if let Some(policydigest) = self.policydigest.as_ref() {
write!(f, " policydigest={:x}", ByteBuf(policydigest))?;
}
if let Some(policyhandle) = self.policyhandle {
write!(f, " policyhandle={:x}", policyhandle)?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Payload {
New {
keylen: usize,
options: TrustedOptions,
},
Load {
blob: Vec<u8>,
options: TrustedOptions,
},
Update {
options: TrustedOptions,
},
}
impl KeyPayload for Payload {
fn payload(&self) -> Cow<[u8]> {
match self {
Payload::New {
keylen,
options,
} => format!("new {}{}", keylen, options),
Payload::Load {
blob,
options,
} => format!("load {:x}{}", ByteBuf(blob), options),
Payload::Update {
options,
} => format!("update{}", options),
}
.into_bytes()
.into()
}
}