From 2b2b5c8905d0e823d03b5ba2a115298e80e08b74 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Thu, 19 Dec 2019 10:32:00 +0100 Subject: openpgp: Encrypt passwords in memory. --- openpgp/src/crypto/mod.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'openpgp/src/crypto/mod.rs') diff --git a/openpgp/src/crypto/mod.rs b/openpgp/src/crypto/mod.rs index fc2a0fef..e4b26810 100644 --- a/openpgp/src/crypto/mod.rs +++ b/openpgp/src/crypto/mod.rs @@ -112,19 +112,22 @@ impl fmt::Debug for SessionKey { /// Holds a password. /// -/// The password is cleared when dropped. +/// The password is encrypted in memory and only decrypted on demand. +/// See [`mem::Encrypted`] for details. +/// +/// [`mem::Encrypted`]: mem/struct.Encrypted.html #[derive(Clone, PartialEq, Eq)] -pub struct Password(mem::Protected); +pub struct Password(mem::Encrypted); impl From> for Password { fn from(v: Vec) -> Self { - Password(v.into()) + Password(mem::Encrypted::new(v.into())) } } impl From> for Password { fn from(v: Box<[u8]>) -> Self { - Password(v.into()) + Password(mem::Encrypted::new(v.into())) } } @@ -148,16 +151,20 @@ impl From<&[u8]> for Password { impl fmt::Debug for Password { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Password ({:?})", self.0) + if cfg!(debug_assertions) { + self.map(|p| write!(f, "Password({:?})", p)) + } else { + f.write_str("Password()") + } } } impl Password { /// Maps the given function over the password. - pub fn map(&self, mut fun: F) -> T + pub fn map(&self, fun: F) -> T where F: FnMut(&mem::Protected) -> T { - fun(&self.0) + self.0.map(fun) } } -- cgit v1.2.3