Skip to content

Commit

Permalink
some fixes to change pass flow
Browse files Browse the repository at this point in the history
add short values for cli args
  • Loading branch information
radumarias committed Apr 21, 2024
1 parent 8a7531f commit 60e6381
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 77 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "encryptedfs"
description = "An encrypted file system that mounts with FUSE on Linux. It can be used to create encrypted directories."
version = "0.1.23"
version = "0.1.24"
edition = "2021"
license = "Apache-2.0"
authors = ["Radu Marias <[email protected]>"]
Expand Down
22 changes: 11 additions & 11 deletions src/encryptedfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ pub enum FsError {

#[error("encryption error: {0}")]
Encryption(#[from] ErrorStack),

#[error("invalid password")]
InvalidPassword,
}

#[derive(Debug, Clone, EnumIter, EnumString, Display)]
Expand Down Expand Up @@ -297,7 +300,7 @@ impl EncryptedFs {
opened_files_for_write: HashMap::new(),
};
let _ = fs.ensure_root_exists();
fs.check_password();
fs.check_password()?;

Ok(fs)
}
Expand Down Expand Up @@ -993,13 +996,13 @@ impl EncryptedFs {
encryptedfs::normalize_end_encrypt_file_name(name, &self.cipher, &self.key)
}
/// Change the password of the filesystem used to access the encryption key.
pub fn change_password(data_dir: &str, old_password: &str, new_password: &str, cipher: &Cipher, derive_key_hash_rounds: u32) -> FsResult<()> {
pub fn change_password(data_dir: &str, old_password: &str, new_password: &str, cipher: Cipher, derive_key_hash_rounds: u32) -> FsResult<()> {
let data_dir = PathBuf::from(data_dir);

// decrypt key
let initial_key = encryptedfs::derive_key(old_password, &cipher, derive_key_hash_rounds, "salt-42");
let enc_file = data_dir.join(SECURITY_DIR).join(KEY_ENC_FILENAME);
let mut decryptor = encryptedfs::create_decryptor(File::open(enc_file.clone())?, cipher, &initial_key);
let mut decryptor = encryptedfs::create_decryptor(File::open(enc_file.clone())?, &cipher, &initial_key);
let mut key: Vec<u8> = vec![];
decryptor.read_to_end(&mut key)?;
decryptor.finish();
Expand All @@ -1008,7 +1011,7 @@ impl EncryptedFs {
let new_key = encryptedfs::derive_key(new_password, &cipher, derive_key_hash_rounds, "salt-42");
fs::remove_file(enc_file.clone())?;
let mut encryptor = encryptedfs::create_encryptor(OpenOptions::new().read(true).write(true).create(true).truncate(true).open(enc_file.clone())?,
cipher, &new_key);
&cipher, &new_key);
encryptor.write_all(&key)?;
encryptor.finish()?;

Expand All @@ -1027,14 +1030,11 @@ impl EncryptedFs {
}
}

fn check_password(&mut self) {
fn check_password(&mut self) -> FsResult<()> {
match self.get_inode(ROOT_INODE) {
Err(FsError::SerializeError(_)) => {
println!("Cannot decrypt data, maybe password is wrong");
process::exit(2);
}
Err(err) => { panic!("Error while checking password: {:?}", err); }
_ => {}
Ok(_) => Ok(()),
Err(FsError::SerializeError(_)) => return Err(FsError::InvalidPassword),
Err(err) => return Err(err),
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/encryptedfs_fuse3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ impl EncryptedFsFuse3 {
direct_io: bool, _suid_support: bool) -> FsResult<Self> {
#[cfg(feature = "abi-7-26")] {
Ok(Self {
fs: const_reentrant_mutex(RefCell::new(EncryptedFs::new(data_dir, password, cipher, derive_key_hash_rounds).unwrap())),
fs: const_reentrant_mutex(RefCell::new(EncryptedFs::new(data_dir, password, cipher, derive_key_hash_rounds)?)),
direct_io,
suid_support: _suid_support,
})
}
#[cfg(not(feature = "abi-7-26"))] {
Ok(Self {
fs: const_reentrant_mutex(RefCell::new(EncryptedFs::new(data_dir, password, cipher, derive_key_hash_rounds).unwrap())),
fs: const_reentrant_mutex(RefCell::new(EncryptedFs::new(data_dir, password, cipher, derive_key_hash_rounds)?)),
direct_io,
suid_support: false,
})
Expand Down
Loading

0 comments on commit 60e6381

Please sign in to comment.