diff --git a/secretstore/Cargo.toml b/secretstore/Cargo.toml index 59aa339..e952f88 100644 --- a/secretstore/Cargo.toml +++ b/secretstore/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "secretstore" -version = "1.0.0" +version = "1.1.0" description = "Store a secret (such as a private key) in an encrypted file" license = "MIT" edition = "2021" diff --git a/secretstore/src/lib.rs b/secretstore/src/lib.rs index e611436..b5bcb98 100644 --- a/secretstore/src/lib.rs +++ b/secretstore/src/lib.rs @@ -22,4 +22,4 @@ mod compat_backtest; mod test_secretstore; // re-exports -pub use crate::secretstore::{SecretStore, SecretStoreCreator}; +pub use crate::secretstore::{Options, SecretStore, SecretStoreCreator}; diff --git a/secretstore/src/secretstore.rs b/secretstore/src/secretstore.rs index 2ad4a9e..6f824f7 100644 --- a/secretstore/src/secretstore.rs +++ b/secretstore/src/secretstore.rs @@ -51,6 +51,13 @@ enum FormatVersion { One = 1, } +/// Various config options for usage, such as allow weak password. +#[derive(Default)] +pub struct Options { + /// If set, allow weak passowrd, skip password strength check. + allow_weak_password: bool, +} + const FORMAT_VERSION_LATEST: FormatVersion = FormatVersion::One; const FORMAT_VERSION_OLDEST: FormatVersion = FormatVersion::One; @@ -129,7 +136,7 @@ impl SecretStore { &self, path_for_secret_file: &str, encryption_password: &str, - allow_weak_password: Option, + allow_weak_password: Option, ) -> Result<(), String> { let file_exists = fs::exists(path_for_secret_file).map_err(|e| { format!( @@ -196,7 +203,7 @@ impl SecretStore { pub fn assemble_encrypted_payload( &self, encryption_password: &str, - allow_weak_password: Option, + allow_weak_password: Option, ) -> Result, String> { let mut encrypted = self.scrambled_secret_data.clone(); let _res = encrypt_scrambled_secret_data( @@ -205,7 +212,7 @@ impl SecretStore { self.encryption_version, encryption_password, &self.encryption_aux_data, - allow_weak_password, + allow_weak_password.unwrap_or_default().allow_weak_password, )?; assemble_payload( self.format_version, @@ -303,7 +310,7 @@ impl SecretStoreCreator { secretstore: &SecretStore, path_for_secret_file: &str, encryption_password: &str, - allow_weak_password: Option, + allow_weak_password: Option, ) -> Result<(), String> { secretstore.write_to_file( path_for_secret_file, @@ -322,6 +329,18 @@ impl FormatVersion { } } +impl Options { + pub fn new() -> Self { + Self::default() + } + + /// Allow weak password, skip passowrd strength check + pub fn allow_weak_password(mut self) -> Self { + self.allow_weak_password = true; + self + } +} + fn read_payload_from_file(path_for_secret_file: &str) -> Result, String> { let contents = fs::read(path_for_secret_file).map_err(|e| { format!( @@ -740,9 +759,9 @@ fn encrypt_scrambled_secret_data( encryption_version: EncryptionVersion, encryption_password: &str, aux_data: &EncryptionAuxData, - allow_weak_password: Option, + allow_weak_password: bool, ) -> Result<(), String> { - if !(allow_weak_password.unwrap_or(false)) { + if !allow_weak_password { let _res = SecretStore::validate_password(encryption_password)?; } diff --git a/secretstore/src/test_secretstore.rs b/secretstore/src/test_secretstore.rs index 4a41422..4ce4ad4 100644 --- a/secretstore/src/test_secretstore.rs +++ b/secretstore/src/test_secretstore.rs @@ -1,4 +1,4 @@ -use crate::{SecretStore, SecretStoreCreator}; +use crate::{Options, SecretStore, SecretStoreCreator}; use hex_conservative::{DisplayHex, FromHex}; use rand::Rng; use std::env::temp_dir; @@ -278,7 +278,11 @@ fn write_to_file_weak_password() { let temp_file = get_temp_file_name(); let password = PASSWORD1.to_owned(); let _res = store - .write_to_file(&temp_file, &password, Some(true)) + .write_to_file( + &temp_file, + &password, + Some(Options::new().allow_weak_password()), + ) .unwrap(); // check the file diff --git a/seedstore-tool/Cargo.toml b/seedstore-tool/Cargo.toml index 75bf39b..8b280e2 100644 --- a/seedstore-tool/Cargo.toml +++ b/seedstore-tool/Cargo.toml @@ -1,8 +1,10 @@ [package] name = "seedstore-tool" -version = "1.0.0" +version = "1.1.0" +description = "Store a secret (such as a private key) in an encrypted file" +license = "MIT" edition = "2021" [dependencies] bip39 = "2.1.0" -seedstore = { version = "1.0.0", path = "../seedstore", features = ["toolhelper"] } +seedstore = { version = "1.1.0", path = "../seedstore", features = ["toolhelper"] } diff --git a/seedstore/Cargo.toml b/seedstore/Cargo.toml index c02ca31..966c7de 100644 --- a/seedstore/Cargo.toml +++ b/seedstore/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "seedstore" -version = "1.0.0" +version = "1.1.0" description = "Store bitcoin secret material (BIP39 mnemonic entropy, or similar) in an encrypted file" license = "MIT" edition = "2021" @@ -16,7 +16,7 @@ toolhelper = ["rpassword"] bip39 = { version = "2.1.0", features = ["zeroize"] } bitcoin = "0.32.5" rpassword = { version = "7.4.0", optional = true } -secretstore = { version = "1.0.0", path = "../secretstore" } +secretstore = { version = "1.1.0", path = "../secretstore" } zeroize = "1.8.1" [dev-dependencies] diff --git a/seedstore/src/keystore.rs b/seedstore/src/keystore.rs index d3e0a98..403e581 100644 --- a/seedstore/src/keystore.rs +++ b/seedstore/src/keystore.rs @@ -14,7 +14,7 @@ use bitcoin::key::Secp256k1; use bitcoin::secp256k1::ecdsa::Signature; use bitcoin::secp256k1::{All, Message, PublicKey, SecretKey, Signing}; -use secretstore::{SecretStore, SecretStoreCreator}; +use secretstore::{Options, SecretStore, SecretStoreCreator}; use zeroize::{Zeroize, ZeroizeOnDrop}; const NONSECRET_DATA_LEN: usize = 4; @@ -114,7 +114,7 @@ impl KeyStore { &self, path_for_secret_file: &str, encryption_password: &str, - allow_weak_password: Option, + allow_weak_password: Option, ) -> Result<(), String> { SecretStoreCreator::write_to_file( &self.secretstore, @@ -205,7 +205,7 @@ impl KeyStoreCreator { seedstore: &KeyStore, path_for_secret_file: &str, encryption_password: &str, - allow_weak_password: Option, + allow_weak_password: Option, ) -> Result<(), String> { seedstore.write_to_file( path_for_secret_file, diff --git a/seedstore/src/lib.rs b/seedstore/src/lib.rs index d60bd31..bb8a669 100644 --- a/seedstore/src/lib.rs +++ b/seedstore/src/lib.rs @@ -30,3 +30,4 @@ pub use crate::keystore::{KeyStore, KeyStoreCreator}; pub use crate::seedstore::{ChildSpecifier, SeedStore, SeedStoreCreator}; #[cfg(feature = "toolhelper")] pub use crate::tool::SeedStoreTool; +pub use secretstore::Options; diff --git a/seedstore/src/seedstore.rs b/seedstore/src/seedstore.rs index ee8322a..74b3a9c 100644 --- a/seedstore/src/seedstore.rs +++ b/seedstore/src/seedstore.rs @@ -11,7 +11,7 @@ use bitcoin::key::{Keypair, Secp256k1}; use bitcoin::secp256k1::ecdsa::Signature; use bitcoin::secp256k1::{All, Message, PublicKey, SecretKey}; use bitcoin::{Address, CompressedPublicKey, Network, NetworkKind}; -use secretstore::{SecretStore, SecretStoreCreator}; +use secretstore::{Options, SecretStore, SecretStoreCreator}; use std::str::FromStr; use zeroize::{Zeroize, ZeroizeOnDrop}; @@ -114,7 +114,7 @@ impl SeedStore { &self, path_for_secret_file: &str, encryption_password: &str, - allow_weak_password: Option, + allow_weak_password: Option, ) -> Result<(), String> { SecretStoreCreator::write_to_file( &self.secretstore, @@ -423,7 +423,7 @@ impl SeedStoreCreator { seedstore: &SeedStore, path_for_secret_file: &str, encryption_password: &str, - allow_weak_password: Option, + allow_weak_password: Option, ) -> Result<(), String> { seedstore.write_to_file( path_for_secret_file, diff --git a/seedstore/src/tool.rs b/seedstore/src/tool.rs index a67b216..143d8cc 100644 --- a/seedstore/src/tool.rs +++ b/seedstore/src/tool.rs @@ -1,5 +1,5 @@ ///! Utility tool implementation: tool to create or check an encrypted secret seed file. -use crate::{SeedStore, SeedStoreCreator}; +use crate::{Options, SeedStore, SeedStoreCreator}; use bip39::Mnemonic; use std::io::{self, stdout, BufRead, Write}; use std::{fs, str::FromStr}; @@ -218,13 +218,14 @@ impl SeedStoreTool { let xpub = self.print_info(&seedstore)?; - let _res = SeedStoreCreator::write_to_file( - &seedstore, - &self.config.filename, - &password, - Some(self.config.allow_weak_password), - ) - .map_err(|e| format!("Could not write secret file, {}", e))?; + let options = if self.config.allow_weak_password { + Some(Options::new().allow_weak_password()) + } else { + None + }; + let _res = + SeedStoreCreator::write_to_file(&seedstore, &self.config.filename, &password, options) + .map_err(|e| format!("Could not write secret file, {}", e))?; println!("Seed written to encrypted file: {}", self.config.filename);