totp demo working

This commit is contained in:
bunnie 2022-06-24 01:18:37 +08:00
parent 57845b6fb6
commit 5d8b85c0bf
3 changed files with 75 additions and 12 deletions

View file

@ -788,7 +788,8 @@ impl ActionManager {
match record.read_to_end(&mut data) {
Ok(_len) => {
if let Some(totp) = deserialize_totp(data) {
let extra = totp.secret;
let alg: String = totp.algorithm.into();
let extra = format!("{}:{}:{}:{}", totp.secret, totp.digits, totp.timestep, alg);
let desc = format!("{}", totp.name);
let li = ListItem {
name: desc,
@ -994,6 +995,41 @@ impl ActionManager {
}
}
}
// specific TOTP entry with a known shared secret for testing
let record = TotpRecord {
version: VAULT_TOTP_REC_VERSION,
secret: "I65VU7K5ZQL7WB4E".to_string(),
name: "totp@authenticationtest.com".to_string(),
algorithm: TotpAlgorithm::HmacSha1,
notes: "Predefined test".to_string(),
digits: 6,
timestep: 30,
ctime: utc_now().timestamp() as u64,
};
let ser = serialize_totp(&record);
let guid = self.gen_guid();
match self.pddb.borrow().get(
VAULT_TOTP_DICT,
&guid,
None, true, true,
Some(VAULT_TOTP_ALLOC_HINT), Some(crate::basis_change)
) {
Ok(mut data) => {
match data.write(&ser) {
Ok(len) => log::debug!("wrote {} bytes", len),
Err(e) => {
self.modals.show_notification(&format!("{}\n{:?}",
t!("vault.error.internal_error", xous::LANG), e
), None).ok();
}
}
}
Err(e) => {
self.modals.show_notification(&format!("{}\n{:?}",
t!("vault.error.internal_error", xous::LANG), e
), None).ok();
}
}
}
self.pddb.borrow().sync().ok();
}

View file

@ -1,7 +1,6 @@
use crypto_common::InvalidLength;
use sha1::Sha1;
use hmac::{Hmac, Mac};
use digest::Update;
use std::{
convert::TryFrom,
time::{SystemTime, SystemTimeError},
@ -46,16 +45,15 @@ impl Into<String> for TotpAlgorithm {
}
#[derive(Debug)]
struct TotpEntry {
name: String,
step_seconds: u16,
shared_secret: Vec<u8>,
digit_count: u8,
algorithm: TotpAlgorithm,
pub(crate) struct TotpEntry {
pub step_seconds: u16,
pub shared_secret: Vec<u8>,
pub digit_count: u8,
pub algorithm: TotpAlgorithm,
}
#[derive(Debug)]
enum Error {
pub(crate) enum Error {
Io(std::io::Error),
DigestLength(InvalidLength),
}
@ -72,7 +70,7 @@ impl From<InvalidLength> for Error {
}
}
fn get_current_unix_time() -> Result<u64, SystemTimeError> {
pub(crate) fn get_current_unix_time() -> Result<u64, SystemTimeError> {
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.map(|duration| duration.as_secs())
@ -102,7 +100,7 @@ fn generate_hmac_bytes(unix_timestamp: u64, totp_entry: &TotpEntry) -> Result<Ve
Ok(computed_hmac)
}
fn generate_totp_code(unix_timestamp: u64, totp_entry: &TotpEntry) -> Result<String, Error> {
pub(crate) fn generate_totp_code(unix_timestamp: u64, totp_entry: &TotpEntry) -> Result<String, Error> {
let hash = generate_hmac_bytes(unix_timestamp, totp_entry)?;
let offset: usize = (hash.last().unwrap_or(&0) & 0xf) as usize;
let binary: u64 = (((hash[offset] & 0x7f) as u64) << 24)

View file

@ -1,4 +1,5 @@
use crate::*;
use crate::totp::{TotpAlgorithm, generate_totp_code};
use gam::{UxRegistration, GlyphStyle, MenuMatic, MenuItem, MenuPayload};
use graphics_server::{Gid, Point, Rectangle, DrawStyle, PixelColor, TextView};
use std::fmt::Write;
@ -8,6 +9,7 @@ use std::cmp::Ordering;
use std::io::{Read, Write as FsWrite};
use actions::ActionOp;
use std::sync::atomic::Ordering as AtomicOrdering;
use std::convert::TryFrom;
/// Display list for items. "name" is the key by which the list is sorted.
/// "extra" is more information about the item, which should not be part of the sort.
@ -153,6 +155,7 @@ impl VaultUx {
let pddb = pddb::Pddb::new();
// TODO: put some informative message asking to mount the PDDB if it's not mounted, right now you just get a blank screen.
// TODO: also add routines to detect if time is not set up, and block initialization until that happens.
pddb.is_mounted_blocking();
// temporary style setting, this will get over-ridden after init
let style = GlyphStyle::Regular;
@ -532,7 +535,33 @@ impl VaultUx {
if index == selected as usize {
box_text.border_width = 4;
}
write!(box_text, "{}\n{}", item.name, item.extra).ok();
match *self.mode.lock().unwrap() {
VaultMode::Fido | VaultMode::Password => {write!(box_text, "{}\n{}", item.name, item.extra).ok();},
VaultMode::Totp => {
let fields = item.extra.split(':').collect::<Vec<&str>>();
if fields.len() == 4 {
let shared_secret = base32::decode(
base32::Alphabet::RFC4648 { padding: false }, fields[0])
.unwrap_or(vec![]);
let digit_count = u8::from_str_radix(fields[1], 10).unwrap_or(6);
let step_seconds = u16::from_str_radix(fields[2], 10).unwrap_or(30);
let algorithm = TotpAlgorithm::try_from(fields[2]).unwrap_or(TotpAlgorithm::HmacSha1);
let totp = totp::TotpEntry {
step_seconds,
shared_secret,
digit_count,
algorithm
};
let code = generate_totp_code(
totp::get_current_unix_time().unwrap_or(0),
&totp
).unwrap_or(t!("vault.error.record_error", xous::LANG).to_string());
write!(box_text, "{}\n{}", item.name, code).ok();
} else {
write!(box_text, "{}", t!("vault.error.record_error", xous::LANG)).ok();
}
},
}
self.gam.post_textview(&mut box_text).expect("couldn't post list item");
item.dirty = false;
}