-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_development.rs
More file actions
361 lines (307 loc) · 10.2 KB
/
Copy pathplugin_development.rs
File metadata and controls
361 lines (307 loc) · 10.2 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//! Plugin development example
//!
//! This example demonstrates how to create custom plugins to extend
//! CryptoTEE functionality with logging, auditing, key rotation, and more.
use async_trait::async_trait;
use crypto_tee::{
plugins::CryptoPlugin, Algorithm, CryptoTEE, CryptoTEEBuilder, CryptoTEEError,
CryptoTEEResult, KeyHandle, KeyOptions, KeyUsage,
};
use std::collections::HashMap;
use std::error::Error;
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};
/// Audit log entry
#[derive(Debug, Clone)]
struct AuditEntry {
timestamp: SystemTime,
operation: String,
key_alias: String,
success: bool,
details: String,
}
/// Audit logging plugin
///
/// Tracks all key operations for compliance and security monitoring
struct AuditPlugin {
logs: Arc<Mutex<Vec<AuditEntry>>>,
}
impl AuditPlugin {
fn new() -> Self {
Self {
logs: Arc::new(Mutex::new(Vec::new())),
}
}
fn add_entry(&self, operation: &str, key_alias: &str, success: bool, details: &str) {
let entry = AuditEntry {
timestamp: SystemTime::now(),
operation: operation.to_string(),
key_alias: key_alias.to_string(),
success,
details: details.to_string(),
};
self.logs.lock().unwrap().push(entry);
}
fn get_logs(&self) -> Vec<AuditEntry> {
self.logs.lock().unwrap().clone()
}
}
#[async_trait]
impl CryptoPlugin for AuditPlugin {
async fn on_key_generated(&self, alias: &str, _handle: &KeyHandle) -> CryptoTEEResult<()> {
self.add_entry("key_generated", alias, true, "New key created");
Ok(())
}
async fn on_key_deleted(&self, alias: &str) -> CryptoTEEResult<()> {
self.add_entry("key_deleted", alias, true, "Key removed");
Ok(())
}
async fn on_sign(&self, alias: &str, data_len: usize) -> CryptoTEEResult<()> {
self.add_entry(
"sign",
alias,
true,
&format!("Signed {} bytes", data_len),
);
Ok(())
}
async fn on_verify(&self, alias: &str, _data_len: usize, verified: bool) -> CryptoTEEResult<()> {
self.add_entry(
"verify",
alias,
true,
&format!("Verification result: {}", verified),
);
Ok(())
}
}
/// Key rotation plugin
///
/// Automatically rotates keys based on age or usage count
struct KeyRotationPlugin {
max_age: Duration,
max_operations: u64,
rotation_callback: Box<dyn Fn(&str) + Send + Sync>,
}
impl KeyRotationPlugin {
fn new<F>(max_age: Duration, max_operations: u64, callback: F) -> Self
where
F: Fn(&str) + Send + Sync + 'static,
{
Self {
max_age,
max_operations,
rotation_callback: Box::new(callback),
}
}
fn check_rotation_needed(&self, handle: &KeyHandle) -> bool {
// Check age
if let Ok(age) = SystemTime::now().duration_since(handle.metadata.created_at) {
if age > self.max_age {
return true;
}
}
// Check usage count
if handle.metadata.usage_count > self.max_operations {
return true;
}
false
}
}
#[async_trait]
impl CryptoPlugin for KeyRotationPlugin {
async fn on_sign(&self, alias: &str, _data_len: usize) -> CryptoTEEResult<()> {
// In a real implementation, we'd check the key and trigger rotation if needed
// For this example, we'll just demonstrate the concept
println!("[Rotation Plugin] Checking key {} for rotation", alias);
Ok(())
}
}
/// Rate limiting plugin
///
/// Prevents abuse by limiting operations per key
struct RateLimitPlugin {
limits: Arc<Mutex<HashMap<String, Vec<SystemTime>>>>,
max_ops_per_minute: usize,
}
impl RateLimitPlugin {
fn new(max_ops_per_minute: usize) -> Self {
Self {
limits: Arc::new(Mutex::new(HashMap::new())),
max_ops_per_minute,
}
}
fn check_rate_limit(&self, alias: &str) -> CryptoTEEResult<()> {
let mut limits = self.limits.lock().unwrap();
let now = SystemTime::now();
let one_minute_ago = now - Duration::from_secs(60);
// Get or create entry
let operations = limits.entry(alias.to_string()).or_insert_with(Vec::new);
// Remove old entries
operations.retain(|&time| time > one_minute_ago);
// Check limit
if operations.len() >= self.max_ops_per_minute {
return Err(CryptoTEEError::OperationError(
"Rate limit exceeded".to_string(),
));
}
// Record this operation
operations.push(now);
Ok(())
}
}
#[async_trait]
impl CryptoPlugin for RateLimitPlugin {
async fn on_sign(&self, alias: &str, _data_len: usize) -> CryptoTEEResult<()> {
self.check_rate_limit(alias)
}
async fn on_verify(&self, alias: &str, _data_len: usize, _verified: bool) -> CryptoTEEResult<()> {
self.check_rate_limit(alias)
}
}
/// Metrics collection plugin
///
/// Collects performance and usage metrics
struct MetricsPlugin {
metrics: Arc<Mutex<HashMap<String, u64>>>,
}
impl MetricsPlugin {
fn new() -> Self {
Self {
metrics: Arc::new(Mutex::new(HashMap::new())),
}
}
fn increment(&self, metric: &str) {
let mut metrics = self.metrics.lock().unwrap();
*metrics.entry(metric.to_string()).or_insert(0) += 1;
}
fn get_metrics(&self) -> HashMap<String, u64> {
self.metrics.lock().unwrap().clone()
}
}
#[async_trait]
impl CryptoPlugin for MetricsPlugin {
async fn on_key_generated(&self, _alias: &str, handle: &KeyHandle) -> CryptoTEEResult<()> {
self.increment("keys_generated");
self.increment(&format!("keys_generated_{:?}", handle.metadata.algorithm));
Ok(())
}
async fn on_sign(&self, _alias: &str, data_len: usize) -> CryptoTEEResult<()> {
self.increment("signatures_created");
self.increment(&format!("bytes_signed_{}", data_len / 1000)); // KB buckets
Ok(())
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
tracing_subscriber::fmt::init();
println!("CryptoTEE Plugin Development Example\n");
// Create CryptoTEE instance
let crypto_tee = CryptoTEEBuilder::new().build().await?;
// Create and register plugins
let audit_plugin = Arc::new(AuditPlugin::new());
let metrics_plugin = Arc::new(MetricsPlugin::new());
let rate_limit_plugin = Arc::new(RateLimitPlugin::new(10)); // 10 ops/minute
crypto_tee.register_plugin(audit_plugin.clone()).await;
crypto_tee.register_plugin(metrics_plugin.clone()).await;
crypto_tee.register_plugin(rate_limit_plugin.clone()).await;
// Also register a rotation plugin
let rotation_plugin = Arc::new(KeyRotationPlugin::new(
Duration::from_secs(3600), // 1 hour
1000, // 1000 operations
|alias| println!("⚠️ Key {} needs rotation!", alias),
));
crypto_tee.register_plugin(rotation_plugin).await;
println!("✓ Registered 4 plugins\n");
// Generate test keys
println!("Generating test keys...");
for i in 1..=3 {
let key = crypto_tee
.generate_key(
&format!("test-key-{}", i),
KeyOptions {
algorithm: if i % 2 == 0 {
Algorithm::EcdsaP256
} else {
Algorithm::Ed25519
},
usage: KeyUsage::SIGN_VERIFY,
extractable: false,
hardware_backed: true,
require_auth: false,
expires_at: None,
},
)
.await?;
println!(" ✓ Generated {}", key.alias);
}
// Perform operations
println!("\nPerforming operations...");
let data = b"Test data for signing";
// Normal operations
for i in 1..=3 {
let alias = format!("test-key-{}", i);
let signature = crypto_tee.sign(&alias, data, None).await?;
let valid = crypto_tee.verify(&alias, data, &signature, None).await?;
println!(" ✓ Key {} - Signed and verified: {}", alias, valid);
}
// Test rate limiting
println!("\nTesting rate limiting...");
for i in 1..=12 {
match crypto_tee.sign("test-key-1", data, None).await {
Ok(_) => println!(" ✓ Operation {} succeeded", i),
Err(e) => println!(" ✗ Operation {} blocked: {}", i, e),
}
}
// Display audit logs
println!("\nAudit Log:");
for entry in audit_plugin.get_logs().iter().take(10) {
println!(
" [{:?}] {} on {} - Success: {} - {}",
entry.timestamp.duration_since(SystemTime::UNIX_EPOCH)?.as_secs(),
entry.operation,
entry.key_alias,
entry.success,
entry.details
);
}
// Display metrics
println!("\nMetrics:");
for (metric, count) in metrics_plugin.get_metrics() {
println!(" {}: {}", metric, count);
}
// Clean up
println!("\nCleaning up...");
for i in 1..=3 {
crypto_tee.delete_key(&format!("test-key-{}", i)).await?;
}
println!("\nExample completed successfully!");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_audit_plugin() {
let plugin = AuditPlugin::new();
// Simulate operations
plugin.on_key_generated("test", &KeyHandle {
alias: "test".to_string(),
platform_handle: Default::default(),
metadata: Default::default(),
}).await.unwrap();
// Check logs
let logs = plugin.get_logs();
assert_eq!(logs.len(), 1);
assert_eq!(logs[0].operation, "key_generated");
}
#[tokio::test]
async fn test_rate_limit_plugin() {
let plugin = RateLimitPlugin::new(2);
// Should allow first two operations
assert!(plugin.on_sign("test", 100).await.is_ok());
assert!(plugin.on_sign("test", 100).await.is_ok());
// Should block third operation
assert!(plugin.on_sign("test", 100).await.is_err());
}
}