use crate::config::Provider;
use crate::error::{Error, IoContext, Result};
use crate::fs::{Privacy, atomic_write_private_with};
use base64::Engine as _;
use base64::engine::general_purpose::STANDARD as B64;
use secrecy::{ExposeSecret, SecretString};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::path::Path;
use weirdnet_keys::{Key32, Sealed, Zeroizing, aead, kdf, words};
pub const VAULT_FILE_NAME: &str = "vault.json";
pub const VAULT_VERSION: u32 = 1;
pub const AAD_CONTEXT: &str = "grindvakt-vault-v1";
pub const KDF_MEMORY_MIB: std::ops::RangeInclusive<u32> = 64..=64;
pub const KDF_ITERATIONS: std::ops::RangeInclusive<u32> = 1..=6;
pub const KDF_PARALLELISM: std::ops::RangeInclusive<u32> = 1..=4;
const NONCE_LEN: usize = 24;
const TAG_LEN: usize = 16;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct VaultFile {
pub version: u32,
pub kdf: KdfParams,
pub salt_password: String,
pub salt_recovery: String,
pub dek_wrapped_by_password: String,
pub dek_wrapped_by_recovery: String,
pub secrets_aead_nonce: String,
pub secrets_aead_ciphertext: String,
pub created_unix: i64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct KdfParams {
pub mem_mib: u32,
pub iterations: u32,
pub parallelism: u32,
}
impl Default for KdfParams {
fn default() -> Self {
Self {
mem_mib: 64,
iterations: 3,
parallelism: 1,
}
}
}
impl KdfParams {
pub fn testing() -> Self {
Self {
mem_mib: 64,
iterations: 1,
parallelism: 1,
}
}
pub fn check(&self) -> Result<()> {
if !KDF_MEMORY_MIB.contains(&self.mem_mib)
|| !KDF_ITERATIONS.contains(&self.iterations)
|| !KDF_PARALLELISM.contains(&self.parallelism)
{
return Err(Error::UnsupportedVault(
"vault KDF parameters out of bounds".into(),
));
}
Ok(())
}
}
#[derive(Clone)]
pub struct Dek(Key32);
impl Dek {
pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
if bytes.len() != 32 {
return Err(Error::Crypto(format!(
"DEK must be 32 bytes, got {}",
bytes.len()
)));
}
Ok(Dek(Key32::from_slice(bytes)?))
}
pub fn as_bytes(&self) -> &[u8; 32] {
&self.0.0
}
}
impl fmt::Debug for Dek {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Dek(<redacted>)")
}
}
#[derive(Clone)]
pub struct RecoveryPhrase {
words: SecretString,
entropy: Key32,
}
impl RecoveryPhrase {
pub fn generate() -> Result<Self> {
let entropy = Key32::random()?;
let words = words::to_words(&entropy)?;
Ok(Self {
words: SecretString::from(words.to_string()),
entropy,
})
}
pub fn parse(text: &str) -> Result<Self> {
let entropy = words::from_words(text).map_err(|_| {
Error::InvalidRecoveryPhrase("expected 24 valid BIP39 English words".into())
})?;
let words = words::to_words(&entropy)?;
Ok(Self {
words: SecretString::from(words.to_string()),
entropy,
})
}
pub fn expose_words(&self) -> &str {
self.words.expose_secret()
}
pub fn matches(&self, text: &str) -> bool {
RecoveryPhrase::parse(text)
.map(|p| p.entropy == self.entropy)
.unwrap_or(false)
}
fn key(&self, salt: &[u8]) -> Result<Key32> {
Ok(kdf::hkdf(
&self.entropy.0,
Some(salt),
AAD_CONTEXT.as_bytes(),
)?)
}
}
impl fmt::Debug for RecoveryPhrase {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("RecoveryPhrase(<redacted>)")
}
}
#[derive(Clone, Default)]
pub struct Tokens {
dnsimple: Option<SecretString>,
digitalocean: Option<SecretString>,
}
impl Tokens {
pub fn get(&self, provider: Provider) -> Option<&SecretString> {
match provider {
Provider::Dnsimple => self.dnsimple.as_ref(),
Provider::Digitalocean => self.digitalocean.as_ref(),
}
}
pub fn set(&mut self, provider: Provider, token: SecretString) {
match provider {
Provider::Dnsimple => self.dnsimple = Some(token),
Provider::Digitalocean => self.digitalocean = Some(token),
}
}
pub fn clear(&mut self, provider: Provider) {
match provider {
Provider::Dnsimple => self.dnsimple = None,
Provider::Digitalocean => self.digitalocean = None,
}
}
pub fn status(&self) -> TokenStatus {
TokenStatus {
dnsimple: self.dnsimple.is_some(),
digitalocean: self.digitalocean.is_some(),
}
}
fn to_json(&self) -> Result<Zeroizing<Vec<u8>>> {
let wire = SecretsWire {
dnsimple_token: self
.dnsimple
.as_ref()
.map(|s| s.expose_secret().to_string()),
digitalocean_token: self
.digitalocean
.as_ref()
.map(|s| s.expose_secret().to_string()),
};
let out = serde_json::to_vec(&wire).map_err(|e| Error::Crypto(e.to_string()));
wire.wipe();
Ok(Zeroizing::new(out?))
}
fn from_json(bytes: &[u8]) -> Result<Self> {
let wire: SecretsWire = serde_json::from_slice(bytes)
.map_err(|_| Error::CorruptVault("could not parse decrypted secrets".into()))?;
let t = Tokens {
dnsimple: wire.dnsimple_token.clone().map(SecretString::from),
digitalocean: wire.digitalocean_token.clone().map(SecretString::from),
};
wire.wipe();
Ok(t)
}
}
impl fmt::Debug for Tokens {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Tokens")
.field("status", &self.status())
.finish()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct TokenStatus {
pub dnsimple: bool,
pub digitalocean: bool,
}
#[derive(Serialize, Deserialize, Default)]
struct SecretsWire {
#[serde(default)]
dnsimple_token: Option<String>,
#[serde(default)]
digitalocean_token: Option<String>,
}
impl SecretsWire {
fn wipe(mut self) {
use zeroize::Zeroize;
if let Some(s) = self.dnsimple_token.as_mut() {
s.zeroize();
}
if let Some(s) = self.digitalocean_token.as_mut() {
s.zeroize();
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Vault {
file: VaultFile,
}
impl Vault {
pub fn create(password: &SecretString, tokens: &Tokens) -> Result<(Vault, RecoveryPhrase)> {
Self::create_with_kdf(password, tokens, KdfParams::default())
}
pub fn create_with_kdf(
password: &SecretString,
tokens: &Tokens,
kdf: KdfParams,
) -> Result<(Vault, RecoveryPhrase)> {
let phrase = RecoveryPhrase::generate()?;
let v = Self::create_with_phrase(password, &phrase, tokens, kdf)?;
Ok((v, phrase))
}
pub fn create_with_phrase(
password: &SecretString,
phrase: &RecoveryPhrase,
tokens: &Tokens,
kdf: KdfParams,
) -> Result<Vault> {
kdf.check()?;
let salt_password: [u8; 16] = weirdnet_keys::random_bytes()?;
let salt_recovery: [u8; 16] = weirdnet_keys::random_bytes()?;
let dek = Dek(Key32::random()?);
let pwd_key = password_key(password, &salt_password, &kdf)?;
let wrapped_pwd = seal_blob(&pwd_key, dek.as_bytes())?;
let rec_key = phrase.key(&salt_recovery)?;
let wrapped_rec = seal_blob(&rec_key, dek.as_bytes())?;
let created_unix = chrono::Utc::now().timestamp();
let mut file = VaultFile {
version: VAULT_VERSION,
kdf,
salt_password: B64.encode(salt_password),
salt_recovery: B64.encode(salt_recovery),
dek_wrapped_by_password: B64.encode(wrapped_pwd),
dek_wrapped_by_recovery: B64.encode(wrapped_rec),
secrets_aead_nonce: String::new(),
secrets_aead_ciphertext: String::new(),
created_unix,
};
write_tokens(&mut file, &dek, tokens)?;
Ok(Vault { file })
}
pub fn from_file(file: VaultFile) -> Result<Vault> {
if file.version != VAULT_VERSION {
return Err(Error::UnsupportedVault(format!(
"unsupported vault version {}",
file.version
)));
}
file.kdf.check()?;
Ok(Vault { file })
}
pub fn from_json(text: &str) -> Result<Vault> {
let file: VaultFile = serde_json::from_str(text)
.map_err(|e| Error::CorruptVault(format!("parse vault: {e}")))?;
Self::from_file(file)
}
pub fn to_json(&self) -> String {
serde_json::to_string_pretty(&self.file).expect("vault file serializes")
}
pub fn load(path: &Path) -> Result<Vault> {
let text =
std::fs::read_to_string(path).ctx(|| format!("read vault {}", path.display()))?;
Self::from_json(&text)
}
pub fn save(&self, path: &Path) -> Result<()> {
self.save_with(path, Privacy::CurrentUser)
}
pub fn save_with(&self, path: &Path, privacy: Privacy) -> Result<()> {
atomic_write_private_with(path, self.to_json().as_bytes(), privacy)
}
pub fn file(&self) -> &VaultFile {
&self.file
}
pub fn unlock_with_password(&self, password: &SecretString) -> Result<Dek> {
let salt = b64(&self.file.salt_password, "salt_password")?;
let key = password_key(password, &salt, &self.file.kdf)?;
open_dek(&key, &self.file.dek_wrapped_by_password)
}
pub fn unlock_with_recovery(&self, phrase: &RecoveryPhrase) -> Result<Dek> {
let salt = b64(&self.file.salt_recovery, "salt_recovery")?;
let key = phrase.key(&salt)?;
open_dek(&key, &self.file.dek_wrapped_by_recovery)
}
pub fn verify_dek(&self, dek: &Dek) -> Result<()> {
self.tokens(dek).map(|_| ())
}
pub fn tokens(&self, dek: &Dek) -> Result<Tokens> {
let nonce = b64(&self.file.secrets_aead_nonce, "secrets_aead_nonce")?;
let ct = b64(
&self.file.secrets_aead_ciphertext,
"secrets_aead_ciphertext",
)?;
if nonce.len() != NONCE_LEN {
return Err(Error::CorruptVault("secrets nonce must be 24 bytes".into()));
}
let sealed = Sealed {
nonce: hex(&nonce),
ct: hex(&ct),
};
let pt = aead::open(&dek.0, &sealed, AAD_CONTEXT.as_bytes())?;
Tokens::from_json(&pt)
}
pub fn token(&self, dek: &Dek, provider: Provider) -> Result<SecretString> {
self.tokens(dek)?
.get(provider)
.cloned()
.ok_or(Error::TokenMissing(provider))
}
pub fn token_status(&self, dek: &Dek) -> Result<TokenStatus> {
Ok(self.tokens(dek)?.status())
}
pub fn set_token(&mut self, dek: &Dek, provider: Provider, token: SecretString) -> Result<()> {
let mut t = self.tokens(dek)?;
t.set(provider, token);
self.set_tokens(dek, &t)
}
pub fn set_tokens(&mut self, dek: &Dek, tokens: &Tokens) -> Result<()> {
self.verify_dek(dek)?;
write_tokens(&mut self.file, dek, tokens)
}
pub fn set_password(&mut self, dek: &Dek, new_password: &SecretString) -> Result<()> {
self.verify_dek(dek)?;
let salt = b64(&self.file.salt_password, "salt_password")?;
let key = password_key(new_password, &salt, &self.file.kdf)?;
self.file.dek_wrapped_by_password = B64.encode(seal_blob(&key, dek.as_bytes())?);
Ok(())
}
pub fn change_password(
&mut self,
current: &SecretString,
new_password: &SecretString,
) -> Result<()> {
let dek = self.unlock_with_password(current)?;
self.set_password(&dek, new_password)
}
pub fn reset_password_with_recovery(
&mut self,
phrase: &RecoveryPhrase,
new_password: &SecretString,
) -> Result<()> {
let dek = self.unlock_with_recovery(phrase)?;
self.set_password(&dek, new_password)
}
}
fn write_tokens(file: &mut VaultFile, dek: &Dek, tokens: &Tokens) -> Result<()> {
let json = tokens.to_json()?;
let s = aead::seal(&dek.0, &json, AAD_CONTEXT.as_bytes())?;
file.secrets_aead_nonce = B64.encode(unhex(&s.nonce)?);
file.secrets_aead_ciphertext = B64.encode(unhex(&s.ct)?);
Ok(())
}
fn password_key(password: &SecretString, salt: &[u8], p: &KdfParams) -> Result<Key32> {
p.check()?;
Ok(kdf::argon2id(
password,
salt,
p.mem_mib.saturating_mul(1024),
p.iterations,
p.parallelism,
)?)
}
fn seal_blob(key: &Key32, plaintext: &[u8]) -> Result<Vec<u8>> {
let s = aead::seal(key, plaintext, AAD_CONTEXT.as_bytes())?;
let mut out = unhex(&s.nonce)?;
out.extend_from_slice(&unhex(&s.ct)?);
Ok(out)
}
fn open_dek(key: &Key32, wrapped_b64: &str) -> Result<Dek> {
let blob = b64(wrapped_b64, "wrapped DEK")?;
if blob.len() < NONCE_LEN + TAG_LEN {
return Err(Error::CorruptVault("wrapped DEK too short".into()));
}
let (nonce, ct) = blob.split_at(NONCE_LEN);
let pt = aead::open(
key,
&Sealed {
nonce: hex(nonce),
ct: hex(ct),
},
AAD_CONTEXT.as_bytes(),
)?;
Dek::from_bytes(&pt).map_err(|_| Error::CorruptVault("wrapped DEK has the wrong length".into()))
}
fn b64(s: &str, what: &str) -> Result<Vec<u8>> {
B64.decode(s)
.map_err(|_| Error::CorruptVault(format!("{what} is not valid base64")))
}
fn hex(b: &[u8]) -> String {
const H: &[u8; 16] = b"0123456789abcdef";
let mut s = String::with_capacity(b.len() * 2);
for x in b {
s.push(H[(x >> 4) as usize] as char);
s.push(H[(x & 15) as usize] as char);
}
s
}
fn unhex(s: &str) -> Result<Vec<u8>> {
let v = |c: u8| -> Result<u8> {
match c {
b'0'..=b'9' => Ok(c - b'0'),
b'a'..=b'f' => Ok(c - b'a' + 10),
b'A'..=b'F' => Ok(c - b'A' + 10),
_ => Err(Error::Crypto("bad hex".into())),
}
};
let b = s.as_bytes();
if !b.len().is_multiple_of(2) {
return Err(Error::Crypto("bad hex".into()));
}
b.chunks(2).map(|p| Ok(v(p[0])? << 4 | v(p[1])?)).collect()
}
#[cfg(test)]
mod tests {
use super::*;
fn pw(s: &str) -> SecretString {
SecretString::from(s.to_string())
}
#[test]
fn hex_roundtrip() {
let b: Vec<u8> = (0..=255).collect();
assert_eq!(unhex(&hex(&b)).unwrap(), b);
}
#[test]
fn wrong_password_and_phrase_fail() {
let mut t = Tokens::default();
t.set(Provider::Dnsimple, pw("tok"));
let (v, _p) = Vault::create_with_kdf(&pw("a"), &t, KdfParams::testing()).unwrap();
assert!(matches!(
v.unlock_with_password(&pw("b")),
Err(Error::WrongSecret)
));
let other = RecoveryPhrase::generate().unwrap();
assert!(matches!(
v.unlock_with_recovery(&other),
Err(Error::WrongSecret)
));
}
#[test]
fn tamper_detected() {
let (v, _) =
Vault::create_with_kdf(&pw("a"), &Tokens::default(), KdfParams::testing()).unwrap();
let mut f = v.file().clone();
let mut ct = B64.decode(&f.secrets_aead_ciphertext).unwrap();
ct[0] ^= 1;
f.secrets_aead_ciphertext = B64.encode(ct);
let v2 = Vault::from_file(f).unwrap();
let dek = v2.unlock_with_password(&pw("a")).unwrap();
assert!(matches!(v2.tokens(&dek), Err(Error::WrongSecret)));
}
#[test]
fn kdf_bounds_enforced() {
let (v, _) =
Vault::create_with_kdf(&pw("a"), &Tokens::default(), KdfParams::testing()).unwrap();
for bad in [
KdfParams {
mem_mib: 128,
iterations: 3,
parallelism: 1,
},
KdfParams {
mem_mib: 64,
iterations: 0,
parallelism: 1,
},
KdfParams {
mem_mib: 64,
iterations: 7,
parallelism: 1,
},
KdfParams {
mem_mib: 64,
iterations: 3,
parallelism: 5,
},
] {
let mut f = v.file().clone();
f.kdf = bad;
assert!(matches!(
Vault::from_file(f),
Err(Error::UnsupportedVault(_))
));
}
let mut f = v.file().clone();
f.version = 2;
assert!(matches!(
Vault::from_file(f),
Err(Error::UnsupportedVault(_))
));
}
#[test]
fn change_and_reset_password() {
let mut t = Tokens::default();
t.set(Provider::Digitalocean, pw("do"));
let (mut v, phrase) = Vault::create_with_kdf(&pw("one"), &t, KdfParams::testing()).unwrap();
v.change_password(&pw("one"), &pw("two")).unwrap();
assert!(v.unlock_with_password(&pw("one")).is_err());
v.reset_password_with_recovery(&phrase, &pw("three"))
.unwrap();
let dek = v.unlock_with_password(&pw("three")).unwrap();
assert_eq!(
v.token(&dek, Provider::Digitalocean)
.unwrap()
.expose_secret(),
"do"
);
assert!(matches!(
v.token(&dek, Provider::Dnsimple),
Err(Error::TokenMissing(Provider::Dnsimple))
));
v.set_token(&dek, Provider::Dnsimple, pw("dns")).unwrap();
assert_eq!(
v.token_status(&dek).unwrap(),
TokenStatus {
dnsimple: true,
digitalocean: true
}
);
}
#[test]
fn phrase_parse_is_lenient_about_case_and_space() {
let p = RecoveryPhrase::generate().unwrap();
let messy = format!(
" {} ",
p.expose_words().to_uppercase().replace(' ', " ")
);
assert!(p.matches(&messy));
assert!(RecoveryPhrase::parse("abandon abandon").is_err());
}
#[test]
fn debug_is_redacted() {
let p = RecoveryPhrase::generate().unwrap();
let first = p.expose_words().split(' ').next().unwrap().to_string();
let mut t = Tokens::default();
t.set(Provider::Dnsimple, pw("super-secret-token"));
let (v, _) = Vault::create_with_kdf(&pw("a"), &t, KdfParams::testing()).unwrap();
let dek = v.unlock_with_password(&pw("a")).unwrap();
let all = format!("{p:?} {t:?} {dek:?}");
assert!(!all.contains("super-secret-token"));
assert!(!all.contains(&first) || first.len() < 3);
assert!(all.contains("redacted"));
}
#[test]
fn secrets_json_shape_matches_v0() {
let mut t = Tokens::default();
t.set(Provider::Dnsimple, pw("x"));
assert_eq!(
&*t.to_json().unwrap(),
br#"{"dnsimple_token":"x","digitalocean_token":null}"#
);
}
}