Skip to content

Truly independent web browser

LibCrypto: Clear the plaintext of a failed decryption

Shannon Boothee8cb449ce8a
diff --git a/Libraries/LibCrypto/Rust/src/key.rs b/Libraries/LibCrypto/Rust/src/key.rs
index ad54099a753..4bac02269cb 100644
--- a/Libraries/LibCrypto/Rust/src/key.rs
+++ b/Libraries/LibCrypto/Rust/src/key.rs
@@ -9,6 +9,7 @@
use crate::ec::{self, EcPrivateKey, EcPublicKey};
use crate::ml::{self, MlDsaPublicKey, MlKemPublicKey, MlPrivateKey};
use crate::rsa::{self, RsaPrivateKey, RsaPublicKey};
+use crate::sys;
use crate::{OkpCurve, OkpKey, okp_key_from_raw};
pub struct KeyHandle(pub(crate) Key);
@@ -160,6 +161,12 @@ pub(crate) fn clear(bytes: &mut [u8]) {
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
}
+/// Overwrites all of the allocation of `bytes` with zeros, including what lies past its length.
+pub(crate) fn clear_allocation(bytes: &mut Vec<u8>) {
+ // SAFETY: The pointer and capacity describe the vector's allocation.
+ unsafe { sys::OPENSSL_cleanse(bytes.as_mut_ptr().cast(), bytes.capacity()) };
+}
+
pub struct KeyPair {
pub public_key: KeyHandle,
pub private_key: KeyHandle,
diff --git a/Libraries/LibCrypto/Rust/src/lib.rs b/Libraries/LibCrypto/Rust/src/lib.rs
index 14626d9d8c3..b375e368689 100644
--- a/Libraries/LibCrypto/Rust/src/lib.rs
+++ b/Libraries/LibCrypto/Rust/src/lib.rs
@@ -421,12 +421,15 @@ fn cipher(
context.init(encrypt, Some(cipher), Some(key), iv)?;
let mut output = Vec::with_capacity(input.len());
// NB: Key wrapping takes its input whole.
- if allow_key_wrapping {
- context.update_whole(input, &mut output)?;
+ let result = if allow_key_wrapping {
+ context.update_whole(input, &mut output)
} else {
- context.update(input, &mut output)?;
- }
- context.finalize(&mut output)?;
+ context.update(input, &mut output)
+ };
+ // NB: OpenSSL writes a plaintext out before it checks its padding.
+ result
+ .and_then(|()| context.finalize(&mut output))
+ .inspect_err(|_| key::clear_allocation(&mut output))?;
Ok(output)
}
@@ -529,8 +532,12 @@ fn aead(
context.add_additional_data(additional_data)?;
let mut output = Vec::with_capacity(input.len() + tag_length);
- context.update(input, &mut output)?;
- context.finalize(&mut output)?;
+ // NB: OpenSSL writes a plaintext out before it checks its tag, and a failed finalization does not count the
+ // bytes it wrote.
+ context
+ .update(input, &mut output)
+ .and_then(|()| context.finalize(&mut output))
+ .inspect_err(|_| key::clear_allocation(&mut output))?;
if encrypt {
output.extend_from_slice(&context.tag(tag_length)?);
}
diff --git a/Libraries/LibCrypto/Rust/src/openssl.rs b/Libraries/LibCrypto/Rust/src/openssl.rs
index 5c1aa7d998c..94994e964ad 100644
--- a/Libraries/LibCrypto/Rust/src/openssl.rs
+++ b/Libraries/LibCrypto/Rust/src/openssl.rs
@@ -1157,6 +1157,8 @@ impl CipherContext {
}
pub(crate) fn update(&mut self, input: &[u8], output: &mut Vec<u8>) -> Result<(), CryptoError> {
+ // NB: Reserving room for all of the output first keeps a reallocation from leaving a copy of it behind.
+ output.reserve(input.len() + MAXIMUM_UPDATE_GROWTH);
for chunk in input.chunks(MAXIMUM_UPDATE_LENGTH) {
self.update_whole(chunk, output)?;
}
diff --git a/Libraries/LibCrypto/Rust/src/sys.rs b/Libraries/LibCrypto/Rust/src/sys.rs
index 070359bcdfd..f3a00015f55 100644
--- a/Libraries/LibCrypto/Rust/src/sys.rs
+++ b/Libraries/LibCrypto/Rust/src/sys.rs
@@ -82,6 +82,7 @@ pub(crate) const POINT_CONVERSION_UNCOMPRESSED: c_int = 4;
unsafe extern "C" {
pub(crate) fn ERR_clear_error();
+ pub(crate) fn OPENSSL_cleanse(pointer: *mut c_void, length: usize);
pub(crate) fn RAND_bytes(buffer: *mut u8, length: c_int) -> c_int;
pub(crate) fn OSSL_PARAM_construct_octet_string(key: *const c_char, buffer: *mut c_void, size: usize)