Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions client/src/crypto/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum Error {
#[cfg(feature = "native_crypto")]
PadError(cipher::inout::PadError),
Getrandom(getrandom::Error),
Other(String),
}

#[cfg(feature = "openssl_crypto")]
Expand Down Expand Up @@ -39,6 +40,7 @@ impl std::error::Error for Error {
#[cfg(feature = "native_crypto")]
Self::PadError(_) => None,
Self::Getrandom(_) => None,
Self::Other(_) => None,
}
}
}
Expand All @@ -51,6 +53,7 @@ impl std::fmt::Display for Error {
#[cfg(feature = "native_crypto")]
Self::PadError(e) => f.write_fmt(format_args!("Wrong padding error: {e}")),
Self::Getrandom(e) => f.write_fmt(format_args!("Random number generation error: {e}")),
Self::Other(e) => f.write_str(e),
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions kwallet/parser/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ fn hash_block_2000(block: &[u8]) -> [u8; 20] {

/// Legacy key derivation: split password into 16-byte blocks, hash each 2000
/// times with SHA1
pub fn derive_key_legacy(password: &[u8]) -> Vec<u8> {
pub fn derive_key_legacy(password: &[u8]) -> Zeroizing<Vec<u8>> {
if password.is_empty() {
return hash_block_2000(&[]).to_vec();
return Zeroizing::new(hash_block_2000(&[]).to_vec());
}

let blocks: Vec<&[u8]> = password.chunks(16).collect();
let hashed_blocks: Vec<[u8; 20]> = blocks.iter().map(|b| hash_block_2000(b)).collect();

match password.len() {
Zeroizing::new(match password.len() {
0..=16 => hashed_blocks[0].to_vec(),
17..=32 => {
let mut key = Vec::with_capacity(40);
Expand All @@ -151,7 +151,7 @@ pub fn derive_key_legacy(password: &[u8]) -> Vec<u8> {
}
key
}
}
})
}

/// Switch endianness (swap every 4 bytes) for legacy Blowfish-ECB
Expand Down
2 changes: 1 addition & 1 deletion kwallet/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl KWalletFile {
return Err(Error::InvalidSalt);
}

crypto::derive_key_pbkdf2_sha512(password, &salt).to_vec()
zeroize::Zeroizing::new(crypto::derive_key_pbkdf2_sha512(password, &salt).to_vec())
}
HashType::SHA1 => crypto::derive_key_legacy(password),
HashType::MD5 => return Err(Error::UnsupportedHash(header.hash_type())),
Expand Down
5 changes: 2 additions & 3 deletions server/src/gnome/crypto/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ pub fn generate_aes_key_for_secret_exchange(
let private_key_uint = BigUint::from_bytes_be(private_key.as_ref());
let common_secret = powm_for_secret_exchange(&server_public_key_uint, private_key_uint);

let mut common_secret_bytes = common_secret.to_bytes_be();
let mut common_secret_padded = vec![0; 192 - common_secret_bytes.len()];
// inefficient, but ok for now
let mut common_secret_bytes = Zeroizing::new(common_secret.to_bytes_be());
let mut common_secret_padded = Zeroizing::new(vec![0; 192 - common_secret_bytes.len()]);
common_secret_padded.append(&mut common_secret_bytes);

// hkdf
Expand Down
10 changes: 5 additions & 5 deletions server/src/gnome/crypto/openssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ pub fn generate_aes_key_for_secret_exchange(
None,
BigNum::from_u32(2).unwrap(),
)?;
let mut common_secret_bytes = dh
.set_private_key(private_key_bn)?
.compute_key(&server_public_key_bn)?;
let mut common_secret_bytes = Zeroizing::new(
dh.set_private_key(private_key_bn)?
.compute_key(&server_public_key_bn)?,
);

let mut common_secret_padded = vec![0; 192 - common_secret_bytes.len()];
// inefficient, but ok for now
let mut common_secret_padded = Zeroizing::new(vec![0; 192 - common_secret_bytes.len()]);
common_secret_padded.append(&mut common_secret_bytes);

// hkdf
Expand Down
16 changes: 7 additions & 9 deletions server/src/gnome/secret_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ pub fn begin(public_key: &Key) -> String {

// Creates the shared secret: an AES key
pub fn handshake(private_key: &Key, exchange: &str) -> Result<Key, crypto::Error> {
let decoded =
decode(exchange).expect("SecretExchange decode error: failed to decode exchange string");
let public_key = Key::new(
decoded
.get(PUBLIC)
.expect("SecretExchange decode error: PUBLIC parameter is empty")
.to_vec(),
);
// Above two calls should never fail during SecretExchange
let decoded = decode(exchange).ok_or(crypto::Error::Other(
"SecretExchange: failed to decode exchange string".into(),
))?;
let public_key_bytes = decoded.get(PUBLIC).ok_or(crypto::Error::Other(
"SecretExchange: PUBLIC parameter is missing".into(),
))?;
let public_key = Key::new(public_key_bytes.to_vec());
let aes_key = crate::gnome::crypto::generate_aes_key(private_key, &public_key)?;

Ok(aes_key)
Expand Down
4 changes: 1 addition & 3 deletions server/src/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn init_logging() {
fn main() {
init_logging();

let mut secret = vec![];
let mut secret = zeroize::Zeroizing::new(vec![]);
io::stdin()
.lock()
.read_to_end(&mut secret)
Expand Down Expand Up @@ -140,8 +140,6 @@ fn main() {
std::process::exit(1);
});

zeroize::Zeroize::zeroize(&mut secret);

rustix::fs::fcntl_add_seals(
&memfd,
SealFlags::WRITE | SealFlags::SHRINK | SealFlags::GROW | SealFlags::SEAL,
Expand Down
13 changes: 7 additions & 6 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use std::{
use clap::Parser;
use rustix::net::{RecvAncillaryBuffer, RecvAncillaryMessage, RecvFlags};
use service::Service;
use zeroize::Zeroizing;

use crate::error::Error;

Expand Down Expand Up @@ -119,11 +120,11 @@ async fn read_secret_from_login_helper() -> Option<oo7::Secret> {
if rustix::fs::seek(&fd, rustix::fs::SeekFrom::Start(0)).is_err() {
continue;
}
let mut secret = Vec::new();
let mut secret = Zeroizing::new(Vec::new());
let mut file = std::fs::File::from(fd);
if file.read_to_end(&mut secret).is_ok() && !secret.is_empty() {
tracing::info!("Received login secret from helper");
return Some(oo7::Secret::from(secret));
return Some(oo7::Secret::from(secret.as_slice()));
}
}
}
Expand All @@ -139,15 +140,15 @@ async fn read_secret_from_credentials_directory() -> Option<oo7::Secret> {

match tokio::fs::File::open(&cred_path).await {
Ok(mut cred_file) => {
let mut contents = Vec::new();
let mut contents = Zeroizing::new(Vec::new());
match tokio::io::AsyncReadExt::read_to_end(&mut cred_file, &mut contents).await {
Ok(_) if contents.is_empty() => {
tracing::info!("Empty systemd credential, treating as unencrypted");
None
}
Ok(_) => {
tracing::info!("Unlocking session keyring with systemd credential");
Some(oo7::Secret::from(contents))
Some(oo7::Secret::from(contents.as_slice()))
}
Err(e) => {
tracing::error!("Failed to read credential: {e}");
Expand Down Expand Up @@ -177,12 +178,12 @@ async fn inner_main(args: Args) -> Result<(), Error> {
}
Some(oo7::Secret::text(password))
} else {
let mut buff = vec![];
let mut buff = Zeroizing::new(vec![]);
stdin.read_to_end(&mut buff)?;
if buff.is_empty() {
None
} else {
Some(oo7::Secret::from(buff))
Some(oo7::Secret::from(buff.as_slice()))
}
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions server/src/prompt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,14 +437,14 @@ impl Prompt {
})?;
let mut stream = tokio::net::UnixStream::from_std(std_stream)
.expect("Failed to create Tokio UnixStream");
let mut buffer = String::new();
stream.read_to_string(&mut buffer).await.map_err(|e| {
let mut buffer = zeroize::Zeroizing::new(Vec::new());
stream.read_to_end(&mut buffer).await.map_err(|e| {
custom_service_error(&format!("Failed to read secret from CLI prompter: {e}"))
})?;
let secret = if buffer.is_empty() {
None
} else {
Some(Secret::from(buffer))
Some(Secret::from(buffer.as_slice()))
};

match self.role {
Expand Down
Loading