-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_platform.rs
More file actions
327 lines (285 loc) · 9.77 KB
/
Copy pathmulti_platform.rs
File metadata and controls
327 lines (285 loc) · 9.77 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
//! Multi-platform key management example
//!
//! This example demonstrates how to use CryptoTEE across different platforms
//! with automatic vendor selection and fallback mechanisms.
use crypto_tee::{
Algorithm, CryptoTEE, CryptoTEEBuilder, KeyOptions, KeyUsage, PlatformConfig,
};
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
tracing_subscriber::fmt::init();
println!("CryptoTEE Multi-Platform Example\n");
// Example 1: Auto-detect best vendor
println!("Example 1: Auto-detection");
auto_detect_example().await?;
// Example 2: Platform-specific configuration
println!("\nExample 2: Platform-specific configuration");
platform_specific_example().await?;
// Example 3: Fallback handling
println!("\nExample 3: Fallback handling");
fallback_example().await?;
// Example 4: Cross-platform key migration
println!("\nExample 4: Cross-platform key migration");
key_migration_example().await?;
println!("\nExample completed successfully!");
Ok(())
}
async fn auto_detect_example() -> Result<(), Box<dyn Error>> {
// Create with auto-detection
let crypto_tee = CryptoTEEBuilder::new().build().await?;
// Check what was detected
let capabilities = crypto_tee.list_capabilities().await?;
println!(" Detected capabilities:");
for cap in &capabilities {
if cap.contains("vendor:") || cap.contains("hardware") {
println!(" - {}", cap);
}
}
// Generate a key using detected vendor
let key = crypto_tee
.generate_key(
"auto-detected-key",
KeyOptions {
algorithm: Algorithm::Ed25519,
usage: KeyUsage::SIGN_VERIFY,
extractable: false,
hardware_backed: true,
require_auth: false,
expires_at: None,
},
)
.await?;
println!(" ✓ Created key with auto-detected vendor");
// Clean up
crypto_tee.delete_key(&key.alias).await?;
Ok(())
}
async fn platform_specific_example() -> Result<(), Box<dyn Error>> {
// Platform-specific configurations
let configs = vec![
(
"Apple Priority",
PlatformConfig {
auto_detect: true,
preferred_vendor: Some("apple".to_string()),
fallback_to_software: true,
cache_keys: true,
},
),
(
"Samsung Priority",
PlatformConfig {
auto_detect: true,
preferred_vendor: Some("samsung".to_string()),
fallback_to_software: true,
cache_keys: true,
},
),
(
"Software Only",
PlatformConfig {
auto_detect: false,
preferred_vendor: Some("software".to_string()),
fallback_to_software: true,
cache_keys: false,
},
),
];
for (name, config) in configs {
println!("\n Testing configuration: {}", name);
match CryptoTEEBuilder::new()
.with_platform_config(config)
.build()
.await
{
Ok(crypto_tee) => {
// Check vendor
let caps = crypto_tee.list_capabilities().await?;
let vendor = caps
.iter()
.find(|c| c.starts_with("vendor:"))
.map(|c| c.trim_start_matches("vendor:"))
.unwrap_or("unknown");
println!(" ✓ Using vendor: {}", vendor);
// Test key generation
let key = crypto_tee
.generate_key(
&format!("{}-test-key", name.to_lowercase().replace(' ', "-")),
KeyOptions {
algorithm: Algorithm::Ed25519,
usage: KeyUsage::SIGN_VERIFY,
extractable: false,
hardware_backed: vendor != "software",
require_auth: false,
expires_at: None,
},
)
.await?;
crypto_tee.delete_key(&key.alias).await?;
}
Err(e) => {
println!(" ✗ Configuration not available: {}", e);
}
}
}
Ok(())
}
async fn fallback_example() -> Result<(), Box<dyn Error>> {
// Try hardware first, fall back to software
let config = PlatformConfig {
auto_detect: true,
preferred_vendor: Some("hardware".to_string()), // Prefer any hardware
fallback_to_software: true,
cache_keys: true,
};
let crypto_tee = CryptoTEEBuilder::new()
.with_platform_config(config)
.build()
.await?;
// Try to create hardware-backed key
match crypto_tee
.generate_key(
"hardware-preferred-key",
KeyOptions {
algorithm: Algorithm::Ed25519,
usage: KeyUsage::SIGN_VERIFY,
extractable: false,
hardware_backed: true,
require_auth: false,
expires_at: None,
},
)
.await
{
Ok(key) => {
let info = crypto_tee.get_key_info(&key.alias).await?;
println!(
" ✓ Created key - Hardware-backed: {}",
info.hardware_backed
);
crypto_tee.delete_key(&key.alias).await?;
}
Err(e) => {
println!(" ✗ Hardware key creation failed: {}", e);
// Try software fallback
let key = crypto_tee
.generate_key(
"software-fallback-key",
KeyOptions {
algorithm: Algorithm::Ed25519,
usage: KeyUsage::SIGN_VERIFY,
extractable: false,
hardware_backed: false,
require_auth: false,
expires_at: None,
},
)
.await?;
println!(" ✓ Created software-backed key as fallback");
crypto_tee.delete_key(&key.alias).await?;
}
}
Ok(())
}
async fn key_migration_example() -> Result<(), Box<dyn Error>> {
println!(" Demonstrating key migration between platforms:");
// Create source platform (software for portability)
let source_config = PlatformConfig {
auto_detect: false,
preferred_vendor: Some("software".to_string()),
fallback_to_software: true,
cache_keys: false,
};
let source_tee = CryptoTEEBuilder::new()
.with_platform_config(source_config)
.build()
.await?;
// Generate exportable key
let source_key = source_tee
.generate_key(
"migration-source-key",
KeyOptions {
algorithm: Algorithm::Ed25519,
usage: KeyUsage::SIGN_VERIFY,
extractable: true, // Must be exportable
hardware_backed: false,
require_auth: false,
expires_at: None,
},
)
.await?;
println!(" ✓ Created exportable key on source platform");
// Sign test data with source key
let test_data = b"Migration test data";
let signature = source_tee.sign(&source_key.alias, test_data, None).await?;
println!(" ✓ Signed data with source key");
// Export key (in real scenario, this would use export_key method)
// For this example, we'll simulate by using the same key data
let exported_key_data = b"<exported-key-data>"; // Placeholder
// Create destination platform
let dest_tee = CryptoTEEBuilder::new().build().await?;
// Import key to destination
// Note: In real implementation, you'd need to handle key format conversion
match dest_tee
.import_key(
"migration-dest-key",
exported_key_data,
KeyOptions {
algorithm: Algorithm::Ed25519,
usage: KeyUsage::SIGN_VERIFY,
extractable: false,
hardware_backed: true, // Try to import to hardware
require_auth: false,
expires_at: None,
},
)
.await
{
Ok(_) => {
println!(" ✓ Successfully imported key to destination platform");
}
Err(_) => {
println!(" ℹ Key import would require actual key data");
}
}
// Clean up
source_tee.delete_key(&source_key.alias).await?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_platform_fallback() {
// Configure to use software fallback
let config = PlatformConfig {
auto_detect: false,
preferred_vendor: Some("nonexistent".to_string()),
fallback_to_software: true,
cache_keys: false,
};
// Should fall back to software
let crypto_tee = CryptoTEEBuilder::new()
.with_platform_config(config)
.build()
.await
.unwrap();
// Should be able to create software keys
let key = crypto_tee
.generate_key(
"fallback-test",
KeyOptions {
algorithm: Algorithm::Ed25519,
usage: KeyUsage::SIGN_VERIFY,
extractable: false,
hardware_backed: false,
require_auth: false,
expires_at: None,
},
)
.await
.unwrap();
crypto_tee.delete_key(&key.alias).await.unwrap();
}
}