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
//! Revocation Manager
//!
//! This module provides the `RevocationManager` which is responsible for:
//! 1. Accepting and validating `RevocationRequest`s
//! 2. Maintaining a state of pending revocations
//! 3. Generating signed `SignedRevocationList`s (SRLs)
//!
//! # Example
//!
//! ```rust,ignore
//! let mut manager = RevocationManager::new();
//!
//! // Submit a request
//! manager.submit_request(request, &warrant, &issuer_key, None, &cp_key)?;
//!
//! // Generate SRL
//! let srl = manager.generate_srl(&cp_keypair, 1)?;
//! ```
use crate::crypto::{PublicKey, SigningKey};
use crate::error::Result;
use crate::revocation::{RevocationRequest, SignedRevocationList};
use chrono::{DateTime, Utc};
use std::collections::HashMap;
/// Manages revocation requests and SRL generation.
#[derive(Debug, Default)]
pub struct RevocationManager {
/// Pending revocation requests, keyed by warrant ID.
pending_requests: HashMap<String, RevocationRequest>,
}
impl RevocationManager {
/// Create a new empty revocation manager.
pub fn new() -> Self {
Self {
pending_requests: HashMap::new(),
}
}
/// Submit a revocation request.
///
/// Validates the request and adds it to the pending list if valid.
///
/// # Arguments
/// * `request` - The revocation request
/// * `warrant_id` - The ID of the warrant to revoke
/// * `warrant_issuer` - The issuer of the warrant
/// * `warrant_holder` - The holder of the warrant (if any)
/// * `warrant_expires_at` - When the warrant expires
/// * `control_plane_key` - The Control Plane's public key
pub fn submit_request(
&mut self,
request: RevocationRequest,
warrant_id: &str,
warrant_issuer: &PublicKey,
warrant_holder: Option<&PublicKey>,
warrant_expires_at: DateTime<Utc>,
control_plane_key: &PublicKey,
) -> Result<()> {
// Validate the request
request.validate(
warrant_id,
warrant_issuer,
warrant_holder,
warrant_expires_at,
control_plane_key,
)?;
// Store it
self.pending_requests
.insert(request.warrant_id.clone(), request);
Ok(())
}
/// Get all pending warrant IDs for SRL generation.
pub fn pending_ids(&self) -> impl Iterator<Item = &str> {
self.pending_requests.keys().map(|s| s.as_str())
}
/// Generate a new Signed Revocation List (SRL).
///
/// This aggregates all pending requests into a new SRL.
///
/// # Arguments
/// * `signer` - The keypair to sign the SRL (usually Control Plane)
/// * `version` - The version number for the new SRL
pub fn generate_srl(&self, signer: &SigningKey, version: u64) -> Result<SignedRevocationList> {
let mut builder = SignedRevocationList::builder().version(version);
// Add revoked warrant IDs
for request in self.pending_requests.values() {
builder = builder.revoke(&request.warrant_id);
}
builder.build(signer)
}
/// Generate an SRL with additional warrant IDs (e.g., from key revocation cascade).
///
/// Use this when `NotaryRegistry.revoke_key()` returns affected warrant IDs.
pub fn generate_srl_with_cascade(
&self,
signer: &SigningKey,
version: u64,
cascade_ids: impl IntoIterator<Item = impl AsRef<str>>,
) -> Result<SignedRevocationList> {
let mut builder = SignedRevocationList::builder().version(version);
// Add revoked warrant IDs from requests
for request in self.pending_requests.values() {
builder = builder.revoke(&request.warrant_id);
}
// Add cascaded IDs from key revocation
for id in cascade_ids {
builder = builder.revoke(id.as_ref());
}
builder.build(signer)
}
/// Prune expired requests.
///
/// Removes requests for warrants that have expired (since they don't need to be in the SRL anymore).
///
/// # Arguments
/// * `is_expired` - A function that returns true if a warrant ID corresponds to an expired warrant.
pub fn prune_expired<F>(&mut self, is_expired: F)
where
F: Fn(&str) -> bool,
{
self.pending_requests
.retain(|warrant_id, _| !is_expired(warrant_id));
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::crypto::SigningKey;
#[test]
fn test_revocation_manager_flow() {
let cp = SigningKey::generate();
let issuer = SigningKey::generate();
let holder = SigningKey::generate();
let mut manager = RevocationManager::new();
// 1. Submit a valid request
let request = RevocationRequest::new(
"warrant_1",
"test",
&issuer, // Issuer revoking their own warrant
)
.unwrap();
manager
.submit_request(
request,
"warrant_1",
&issuer.public_key(),
Some(&holder.public_key()),
Utc::now() + chrono::Duration::hours(1),
&cp.public_key(),
)
.unwrap();
// 2. Generate SRL
let srl = manager.generate_srl(&cp, 1).unwrap();
assert!(srl.is_revoked("warrant_1"));
}
#[test]
fn test_cascade_from_key_revocation() {
let cp = SigningKey::generate();
let manager = RevocationManager::new();
// Simulate cascading revocation: NotaryRegistry.revoke_key()
// returns affected warrant IDs, which we add to the SRL
let affected_ids = vec!["warrant_a", "warrant_b", "warrant_c"];
let srl = manager
.generate_srl_with_cascade(&cp, 1, &affected_ids)
.unwrap();
assert!(srl.is_revoked("warrant_a"));
assert!(srl.is_revoked("warrant_b"));
assert!(srl.is_revoked("warrant_c"));
assert!(!srl.is_revoked("warrant_d"));
}
}