1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// SPDX-License-Identifier: BUSL-1.1
//! Certificate Revocation List (CRL) loading and checking.
//!
//! When `crl_path` is configured, the CRL file is loaded on startup and
//! periodically reloaded at `cert_reload_interval_secs`. During mTLS
//! handshake, the client certificate's serial number is checked against
//! the revoked serials set.
//!
//! ## CRL format
//!
//! PEM-encoded X.509 CRL (-----BEGIN X509 CRL-----). Multiple CRLs may
//! be concatenated in a single file.
//!
//! ## Integration
//!
//! `CrlStore` is wrapped in `Arc<RwLock<>>` and shared between the TLS
//! acceptor (reads) and the CRL reload background task (writes).
use std::collections::HashSet;
use std::fs;
use std::path::Path;
use std::sync::{Arc, RwLock};
use tracing::{info, warn};
/// A set of revoked certificate serial numbers.
///
/// Serial numbers are stored as hex strings (uppercase) for fast lookup.
/// This is transport-agnostic — it doesn't depend on rustls internals.
#[derive(Debug, Clone, Default)]
pub struct CrlStore {
/// Set of revoked serial numbers (hex-encoded, uppercase).
revoked_serials: HashSet<String>,
/// When the CRL was last loaded (epoch microseconds).
loaded_at_us: u64,
/// Number of times the CRL has been reloaded.
reload_count: u64,
}
impl CrlStore {
/// Create an empty CRL store (no revocations).
pub fn new() -> Self {
Self::default()
}
/// Load revoked serials from a PEM-encoded CRL file.
///
/// Parses the CRL, extracts all revoked certificate serial numbers,
/// and stores them as hex strings for O(1) lookup.
pub fn load_from_file(path: &Path) -> Result<Self, CrlError> {
let pem_data = fs::read(path).map_err(|e| CrlError::IoError {
path: path.display().to_string(),
detail: e.to_string(),
})?;
Self::load_from_pem(&pem_data)
}
/// Load from PEM bytes (for testing without files).
pub fn load_from_pem(pem_data: &[u8]) -> Result<Self, CrlError> {
let mut revoked_serials = HashSet::new();
// Parse PEM blocks looking for X509 CRL entries.
for pem in pem::parse_many(pem_data).map_err(|e| CrlError::ParseError {
detail: format!("PEM parse failed: {e}"),
})? {
if pem.tag() != "X509 CRL" {
continue;
}
// Parse the DER-encoded CRL.
let serials = parse_crl_der(pem.contents())?;
revoked_serials.extend(serials);
}
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_micros() as u64;
info!(revoked_count = revoked_serials.len(), "CRL loaded");
Ok(Self {
revoked_serials,
loaded_at_us: now,
reload_count: 0,
})
}
/// Check if a certificate serial number is revoked.
///
/// `serial_hex` should be the certificate's serial number as a
/// hex string (uppercase, no leading zeros stripped).
pub fn is_revoked(&self, serial_hex: &str) -> bool {
self.revoked_serials.contains(serial_hex)
}
/// Number of revoked serials in the store.
pub fn revoked_count(&self) -> usize {
self.revoked_serials.len()
}
/// When the CRL was last loaded.
pub fn loaded_at_us(&self) -> u64 {
self.loaded_at_us
}
/// Number of CRL reloads.
pub fn reload_count(&self) -> u64 {
self.reload_count
}
/// Reload the CRL from file, replacing the current revoked set.
pub fn reload_from_file(&mut self, path: &Path) -> Result<(), CrlError> {
let new = Self::load_from_file(path)?;
self.revoked_serials = new.revoked_serials;
self.loaded_at_us = new.loaded_at_us;
self.reload_count += 1;
Ok(())
}
}
/// Parse a DER-encoded CRL and extract revoked serial numbers as hex strings.
///
/// Uses a minimal ASN.1 parser to extract the revokedCertificates sequence
/// from the TBSCertList structure (RFC 5280 Section 5.1).
fn parse_crl_der(der: &[u8]) -> Result<Vec<String>, CrlError> {
// CRL structure (RFC 5280):
// CertificateList ::= SEQUENCE {
// tbsCertList TBSCertList,
// signatureAlgorithm AlgorithmIdentifier,
// signatureValue BIT STRING
// }
// TBSCertList ::= SEQUENCE {
// version INTEGER OPTIONAL,
// signature AlgorithmIdentifier,
// issuer Name,
// thisUpdate Time,
// nextUpdate Time OPTIONAL,
// revokedCertificates SEQUENCE OF SEQUENCE {
// userCertificate CertificateSerialNumber,
// revocationDate Time,
// crlEntryExtensions Extensions OPTIONAL
// } OPTIONAL,
// crlExtensions [0] EXPLICIT Extensions OPTIONAL
// }
//
// We use x509-parser for robust ASN.1 handling.
use x509_parser::prelude::FromDer;
use x509_parser::revocation_list::CertificateRevocationList;
let (_, crl) = CertificateRevocationList::from_der(der).map_err(|e| CrlError::ParseError {
detail: format!("CRL DER parse failed: {e}"),
})?;
let mut serials = Vec::new();
for revoked in crl.iter_revoked_certificates() {
let serial = revoked.raw_serial_as_string();
serials.push(serial.to_uppercase());
}
Ok(serials)
}
/// Thread-safe, reloadable CRL store.
pub type SharedCrlStore = Arc<RwLock<CrlStore>>;
/// Create a shared CRL store from a file path.
///
/// Returns `None` if no CRL path is configured.
pub fn load_shared_crl(crl_path: Option<&Path>) -> Result<Option<SharedCrlStore>, CrlError> {
match crl_path {
Some(path) => {
let store = CrlStore::load_from_file(path)?;
Ok(Some(Arc::new(RwLock::new(store))))
}
None => Ok(None),
}
}
/// Spawn a background task that periodically reloads the CRL.
pub fn spawn_crl_reload_task(
crl_store: SharedCrlStore,
crl_path: std::path::PathBuf,
interval_secs: u64,
mut shutdown: tokio::sync::watch::Receiver<bool>,
) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
let interval = std::time::Duration::from_secs(interval_secs);
info!(
interval_secs,
path = %crl_path.display(),
"CRL reload task started"
);
loop {
tokio::select! {
_ = tokio::time::sleep(interval) => {}
_ = shutdown.changed() => {
if *shutdown.borrow() {
info!("CRL reload task stopped");
return;
}
}
}
let mut store = crl_store.write().unwrap_or_else(|p| {
warn!("CRL store lock poisoned, recovering");
p.into_inner()
});
if let Err(e) = store.reload_from_file(&crl_path) {
warn!(
error = %e,
path = %crl_path.display(),
"CRL reload failed"
);
}
}
})
}
/// Check a client certificate serial against the CRL store.
///
/// Returns `true` if the certificate should be rejected (is revoked).
pub fn check_revocation(crl_store: &SharedCrlStore, serial_hex: &str) -> bool {
match crl_store.read() {
Ok(store) => store.is_revoked(serial_hex),
Err(_) => {
warn!("CRL store lock poisoned — failing open (allowing connection)");
false
}
}
}
/// CRL-related errors.
#[derive(Debug, thiserror::Error)]
pub enum CrlError {
#[error("CRL I/O error ({path}): {detail}")]
IoError { path: String, detail: String },
#[error("CRL parse error: {detail}")]
ParseError { detail: String },
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_crl_store() {
let store = CrlStore::new();
assert_eq!(store.revoked_count(), 0);
assert!(!store.is_revoked("DEADBEEF"));
}
#[test]
fn manual_serial_check() {
let mut store = CrlStore::new();
store.revoked_serials.insert("0A".into());
store.revoked_serials.insert("FF".into());
assert!(store.is_revoked("0A"));
assert!(store.is_revoked("FF"));
assert!(!store.is_revoked("0B"));
}
#[test]
fn shared_crl_no_path() {
let result = load_shared_crl(None).unwrap();
assert!(result.is_none());
}
#[test]
fn shared_crl_nonexistent_path() {
let result = load_shared_crl(Some(Path::new("/nonexistent/crl.pem")));
assert!(result.is_err());
}
#[test]
fn revocation_check_thread_safe() {
let store = Arc::new(RwLock::new(CrlStore::new()));
store.write().unwrap().revoked_serials.insert("AA".into());
assert!(check_revocation(&store, "AA"));
assert!(!check_revocation(&store, "BB"));
}
}