use std::borrow::Cow;
use super::ByteBuf;
use crate::keytype::*;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Encrypted;
impl KeyType for Encrypted {
type Description = str;
type Payload = Payload;
fn name() -> &'static str {
"encrypted"
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Format {
Default,
Ecryptfs,
Enc32,
}
impl Format {
fn name(&self) -> &str {
match *self {
Format::Default => "default",
Format::Ecryptfs => "ecryptfs",
Format::Enc32 => "enc32",
}
}
}
#[allow(clippy::derivable_impls)]
impl Default for Format {
fn default() -> Self {
Format::Default
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MasterKeyType {
Trusted,
User,
}
impl MasterKeyType {
fn name(&self) -> &str {
match self {
MasterKeyType::Trusted => "trusted",
MasterKeyType::User => "user",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Payload {
New {
format: Option<Format>,
keytype: MasterKeyType,
description: Cow<'static, str>,
keylen: usize,
},
Load {
blob: Vec<u8>,
},
Update {
keytype: MasterKeyType,
description: Cow<'static, str>,
},
}
impl KeyPayload for Payload {
fn payload(&self) -> Cow<[u8]> {
match self {
Payload::New {
format,
keytype,
description,
keylen,
} => {
format!(
"new {} {}:{} {}",
format.unwrap_or_default().name(),
keytype.name(),
description,
keylen,
)
},
Payload::Load {
blob,
} => format!("load {:x}", ByteBuf(blob)),
Payload::Update {
keytype,
description,
} => format!("update {}:{}", keytype.name(), description),
}
.into_bytes()
.into()
}
}