feat(vault): add password-related methods to storage abstraction

This commit is contained in:
Gianguido Sorà 2022-07-12 17:30:11 +02:00
parent 371164b82f
commit bdeb68a158

View file

@ -1,6 +1,7 @@
use chrono::{DateTime, NaiveDateTime, Utc};
use std::convert::TryFrom;
use std::{
io::Read,
io::Write,
time::{SystemTime, UNIX_EPOCH},
};
@ -18,6 +19,31 @@ pub struct Manager {
trng: trng::Trng,
}
#[derive(Debug)]
pub enum Error {
IoError(std::io::Error),
TotpSerError(TOTPSerializationError),
PasswordSerError(PasswordSerializationError),
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Self::IoError(e)
}
}
impl From<TOTPSerializationError> for Error {
fn from(e: TOTPSerializationError) -> Self {
Self::TotpSerError(e)
}
}
impl From<PasswordSerializationError> for Error {
fn from(e: PasswordSerializationError) -> Self {
Self::PasswordSerError(e)
}
}
impl Manager {
pub fn new(xns: &xous_names::XousNames) -> Manager {
Manager {
@ -32,27 +58,163 @@ impl Manager {
hex::encode(guid)
}
pub fn new_totp_record(&mut self, record: TotpRecord) -> Result<(), std::io::Error> {
fn pddb_store(
&mut self,
payload: &[u8],
dict: &str,
key_name: &str,
alloc_hint: Option<usize>,
basis: Option<String>,
) -> Result<(), Error> {
match self.pddb.get(
dict,
&key_name,
basis.as_deref(),
true,
true,
alloc_hint,
Some(crate::basis_change),
) {
Ok(mut data) => match data.write(payload) {
Ok(_) => Ok(self.pddb.sync().unwrap_or(())),
Err(e) => Err(Error::IoError(e)),
},
Err(e) => Err(Error::IoError(e)),
}
}
fn pddb_get(&mut self, dict: &str, key_name: &str) -> Result<Vec<u8>, Error> {
match self.pddb.get(
dict,
key_name,
None,
false,
false,
None,
Some(crate::basis_change),
) {
Ok(mut record) => {
let mut data = Vec::<u8>::new();
record.read_to_end(&mut data)?;
Ok(data)
}
Err(e) => return Err(Error::IoError(e)),
}
}
fn basis_for_key(&mut self, dict: &str, key_name: &str) -> Result<String, Error> {
match self.pddb.get(
dict,
key_name,
None,
false,
false,
None,
Some(crate::basis_change),
) {
Ok(record) => Ok(record
.attributes()
.expect("couldn't get key attributes")
.basis),
Err(e) => return Err(Error::IoError(e)),
}
}
pub fn new_totp_record(
&mut self,
record: TotpRecord,
basis: Option<String>,
) -> Result<(), Error> {
let mut record = record;
record.ctime = utc_now().timestamp() as u64;
let serialized_record: Vec<u8> = record.into();
let guid = self.gen_guid();
match self.pddb.get(
self.pddb_store(
&serialized_record,
VAULT_TOTP_DICT,
&guid,
None,
true,
true,
Some(VAULT_TOTP_ALLOC_HINT),
Some(crate::basis_change),
) {
Ok(mut data) => match data.write(&serialized_record) {
Ok(_) => Ok(self.pddb.sync().unwrap_or(())),
Err(e) => Err(e),
},
Err(e) => Err(e),
basis,
)
}
pub fn new_password_record(
&mut self,
record: PasswordRecord,
basis: Option<String>,
) -> Result<(), Error> {
let mut record = record;
record.ctime = utc_now().timestamp() as u64;
let serialized_record: Vec<u8> = record.into();
let guid = self.gen_guid();
self.pddb_store(
&serialized_record,
VAULT_PASSWORD_DICT,
&guid,
Some(VAULT_TOTP_ALLOC_HINT),
basis,
)
.into()
}
pub fn all_totp(&mut self) -> Result<Vec<TotpRecord>, Error> {
let keylist = self.pddb.list_keys(VAULT_TOTP_DICT, None)?;
let mut ret = vec![];
for key in keylist {
let record = TotpRecord::try_from(self.pddb_get(VAULT_TOTP_DICT, &key)?)?;
ret.push(record);
}
Ok(ret)
}
pub fn all_passwords(&mut self) -> Result<Vec<PasswordRecord>, Error> {
let keylist = self.pddb.list_keys(VAULT_PASSWORD_DICT, None)?;
let mut ret = vec![];
for key in keylist {
let record = PasswordRecord::try_from(self.pddb_get(VAULT_PASSWORD_DICT, &key)?)?;
ret.push(record);
}
Ok(ret)
}
pub fn totp(&mut self, key_name: &str) -> Result<TotpRecord, Error> {
TotpRecord::try_from(self.pddb_get(VAULT_TOTP_DICT, key_name)?)
.map_err(|error| Error::TotpSerError(error))
}
pub fn password(&mut self, key_name: &str) -> Result<PasswordRecord, Error> {
PasswordRecord::try_from(self.pddb_get(VAULT_PASSWORD_DICT, key_name)?)
.map_err(|error| Error::PasswordSerError(error))
}
pub fn update_totp(&mut self, key_name: &str, record: TotpRecord) -> Result<(), Error> {
let basis = self.basis_for_key(VAULT_TOTP_DICT, key_name)?;
self.pddb
.delete_key(VAULT_TOTP_DICT, key_name, Some(&basis))?;
self.new_totp_record(record, Some(basis))
}
pub fn delete_totp(&mut self, key_name: &str) -> Result<(), Error> {
let basis = self.basis_for_key(VAULT_TOTP_DICT, key_name)?;
self.pddb
.delete_key(VAULT_TOTP_DICT, key_name, Some(&basis))
.map_err(|error| Error::IoError(error))
}
pub fn delete_password(&mut self, key_name: &str) -> Result<(), Error> {
let basis = self.basis_for_key(VAULT_PASSWORD_DICT, key_name)?;
self.pddb
.delete_key(VAULT_PASSWORD_DICT, key_name, Some(&basis))
.map_err(|error| Error::IoError(error))
}
}
@ -181,6 +343,124 @@ impl From<TotpRecord> for Vec<u8> {
}
}
#[derive(Debug)]
pub enum PasswordSerializationError {
MalformedInput,
BadVersion,
BadCount,
BadCtime,
BadAtime,
}
pub struct PasswordRecord {
pub version: u32,
pub description: String,
pub username: String,
pub password: String,
pub notes: String,
pub ctime: u64,
pub atime: u64,
pub count: u64,
}
impl TryFrom<Vec<u8>> for PasswordRecord {
type Error = PasswordSerializationError;
fn try_from(data: Vec<u8>) -> Result<Self, Self::Error> {
let desc_str =
String::from_utf8(data).or(Err(PasswordSerializationError::MalformedInput))?;
let mut pr = PasswordRecord {
version: 0,
description: String::new(),
username: String::new(),
password: String::new(),
notes: String::new(),
ctime: 0,
atime: 0,
count: 0,
};
let lines = desc_str.split('\n');
for line in lines {
if let Some((tag, data)) = line.split_once(':') {
match tag {
"version" => {
if let Ok(ver) = u32::from_str_radix(data, 10) {
pr.version = ver
} else {
log::warn!("ver error");
return Err(PasswordSerializationError::BadVersion);
}
}
"description" => pr.description.push_str(data),
"username" => pr.username.push_str(data),
"password" => pr.password.push_str(data),
"notes" => pr.notes.push_str(data),
"ctime" => {
if let Ok(ctime) = u64::from_str_radix(data, 10) {
pr.ctime = ctime;
} else {
log::warn!("ctime error");
return Err(PasswordSerializationError::BadCtime);
}
}
"atime" => {
if let Ok(atime) = u64::from_str_radix(data, 10) {
pr.atime = atime;
} else {
log::warn!("atime error");
return Err(PasswordSerializationError::BadAtime);
}
}
"count" => {
if let Ok(count) = u64::from_str_radix(data, 10) {
pr.count = count;
} else {
log::warn!("count error");
return Err(PasswordSerializationError::BadCount);
}
}
_ => {
log::warn!(
"unexpected tag {} encountered parsing password info, ignoring",
tag
);
}
}
} else {
log::trace!("invalid line skipped: {:?}", line);
}
}
Ok(pr)
}
}
impl From<PasswordRecord> for Vec<u8> {
fn from(pr: PasswordRecord) -> Self {
format!(
"{}:{}\n{}:{}\n{}:{}\n{}:{}\n{}:{}\n{}:{}\n{}:{}\n{}:{}\n",
"version",
pr.version,
"description",
pr.description,
"username",
pr.username,
"password",
pr.password,
"notes",
pr.notes,
"ctime",
pr.ctime,
"atime",
pr.atime,
"count",
pr.count,
)
.into_bytes()
}
}
/// because we don't get Utc::now, as the crate checks your architecture and xous is not recognized as a valid target
fn utc_now() -> DateTime<Utc> {
let now = SystemTime::now()