Skip to content

Commit 92501e2

Browse files
committed
ACME: zeroize buffers with private keys after use.
We trust the SSL library to securely clear the EVP_PKEY objects, but all the places where we may store a PEM data should be cleared by us.
1 parent ab2aef3 commit 92501e2

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ openssl-foreign-types = { package = "foreign-types", version = "0.3" }
1717
openssl-sys = { version = "0.9.109", features = ["bindgen"] }
1818
siphasher = { version = "1.0.1", default-features = false }
1919
thiserror = { version = "2.0.12", default-features = false }
20+
zeroize = "1.8.1"
2021

2122
[dependencies.nginx-sys]
2223
git = "https://github.com/nginx/ngx-rust"

src/conf/issuer.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use ngx::ngx_log_debug;
1616
use ngx::sync::RwLock;
1717
use openssl::pkey::{PKey, Private};
1818
use thiserror::Error;
19+
use zeroize::Zeroizing;
1920

2021
use super::ext::NgxConfExt;
2122
use super::order::CertificateOrder;
@@ -256,7 +257,7 @@ impl Issuer {
256257
}
257258
}
258259

259-
if let Ok(buf) = pkey.private_key_to_pem_pkcs8() {
260+
if let Ok(buf) = pkey.private_key_to_pem_pkcs8().map(Zeroizing::new) {
260261
// Ignore write errors.
261262
let _ = state_dir.write(&path, &buf);
262263
}
@@ -349,7 +350,8 @@ impl StateDir {
349350
}
350351

351352
let mut cert = CertificateContextInner::new_in(cf.pool());
352-
cert.set(&chain, &pkey.private_key_to_pem_pkcs8()?, valid)?;
353+
let pkey = Zeroizing::new(pkey.private_key_to_pem_pkcs8()?);
354+
cert.set(&chain, &pkey, valid)?;
353355

354356
Ok(cert)
355357
}

src/state/certificate.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use ngx::allocator::{AllocError, Allocator, TryCloneIn};
44
use ngx::collections::Vec;
55
use ngx::core::{Pool, SlabPool};
66
use ngx::sync::RwLock;
7+
use zeroize::Zeroize;
78

89
use crate::time::{jitter, Time, TimeRange};
910

@@ -116,6 +117,10 @@ where
116117
.try_reserve_exact(PREFIX.len() + pkey.len())
117118
.map_err(|_| AllocError)?;
118119

120+
// Zeroize is not implemented for allocator-api2 types.
121+
self.chain.as_mut_slice().zeroize();
122+
self.pkey.as_mut_slice().zeroize();
123+
119124
self.chain = new_chain;
120125
self.pkey = new_pkey;
121126
}
@@ -179,3 +184,14 @@ where
179184
Time::now() >= self.next
180185
}
181186
}
187+
188+
impl<A> Drop for CertificateContextInner<A>
189+
where
190+
A: Allocator + Clone,
191+
{
192+
fn drop(&mut self) {
193+
// Zeroize is not implemented for allocator-api2 types.
194+
self.chain.as_mut_slice().zeroize();
195+
self.pkey.as_mut_slice().zeroize();
196+
}
197+
}

0 commit comments

Comments
 (0)