wsc 0.8.0

WebAssembly Signature Component - WASM signing and verification toolkit
Documentation
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/// Provisioning session orchestration
///
/// This module coordinates the complete device provisioning workflow:
/// 1. Generate key in secure element
/// 2. Extract public key
/// 3. Create CSR
/// 4. Sign certificate with CA
/// 5. Return certificate + handle to locked key
use crate::error::WSError;
use crate::platform::SecureKeyProvider;
use crate::provisioning::{CertificateConfig, DeviceIdentity, PrivateCA, ProvisioningResult};

/// Provisioning session
///
/// Coordinates the complete provisioning workflow for a single device.
///
/// # Workflow
///
/// ```text
/// 1. Generate key in secure element
///    ├─ Key generated in hardware (ATECC608 slot)
///    └─ Key never leaves hardware
///
/// 2. Extract public key
///    ├─ Public key exported
///    └─ Private key stays in hardware
///
/// 3. Create CSR
///    ├─ CSR signed with device key
///    └─ Proves device owns the key
///
/// 4. Sign certificate
///    ├─ CA signs device certificate
///    └─ Certificate binds identity to public key
///
/// 5. Lock key slot (optional)
///    ├─ Slot becomes read-only
///    └─ Key can never be modified or exported
/// ```
///
/// # Example
///
/// ```ignore
/// use wsc::provisioning::{ProvisioningSession, PrivateCA, DeviceIdentity, CertificateConfig};
/// use wsc::platform::secure_element::Atecc608Provider;
///
/// // Factory setup
/// let ca = PrivateCA::load_from_directory("factory-ca")?;
/// let mut secure_element = Atecc608Provider::new("/dev/i2c-1", 0x60)?;
///
/// // Per-device provisioning
/// let device_id = DeviceIdentity::new("device-12345");
/// let config = CertificateConfig::new("device-12345")
///     .with_organization("Acme Corp")
///     .with_validity_days(365);
///
/// let result = ProvisioningSession::provision(
///     &ca,
///     &mut secure_element,
///     device_id,
///     config,
///     true, // lock key slot
/// )?;
///
/// println!("Device provisioned!");
/// println!("  Device ID: {}", result.device_id);
/// println!("  Key Handle: {:?}", result.key_handle);
/// println!("  Certificate: {} bytes", result.certificate.len());
/// ```
pub struct ProvisioningSession;

impl ProvisioningSession {
    /// Provision a device with a certificate
    ///
    /// This is the main entry point for device provisioning.
    ///
    /// # Arguments
    ///
    /// * `ca` - Certificate Authority to sign the device certificate
    /// * `provider` - Secure key provider (TPM, Secure Element, etc.)
    /// * `device_id` - Device identity
    /// * `config` - Certificate configuration
    /// * `lock_key` - If true, lock the key slot (write-once)
    ///
    /// # Returns
    ///
    /// ProvisioningResult containing key handle, certificate, and chain
    ///
    /// # Errors
    ///
    /// - `WSError::InvalidArgument` - Invalid device ID or configuration
    /// - `WSError::HardwareError` - Hardware operation failed
    /// - `WSError::NoSpace` - No available key slots
    ///
    /// # Security
    ///
    /// - Private key never leaves secure hardware
    /// - If `lock_key=true`, key becomes permanently locked
    /// - Certificate binds device identity to public key
    pub fn provision(
        ca: &PrivateCA,
        provider: &dyn SecureKeyProvider,
        device_id: DeviceIdentity,
        config: CertificateConfig,
        lock_key: bool,
    ) -> Result<ProvisioningResult, WSError> {
        // Validate inputs
        device_id.validate()?;

        // Step 1: Generate key in hardware
        log::info!("Generating key for device: {}", device_id);
        let key_handle = provider.generate_key()?;

        // Step 2: Extract public key
        log::info!("Extracting public key");
        let public_key = provider.get_public_key(key_handle)?;

        // Step 3: Sign device certificate with CA
        // Note: We skip CSR generation for now (direct signing)
        log::info!("Signing device certificate");
        let certificate = ca.sign_device_certificate(&public_key, &device_id, &config)?;

        // Step 4: Build certificate chain
        // Chain: Device cert + Intermediate (if any) + Root
        let mut certificate_chain = Vec::new();

        // Add CA certificate
        certificate_chain.push(ca.certificate().to_vec());

        // TODO: Add intermediate certificates if CA is intermediate

        // Step 5: Generate serial number
        let serial_number = Self::generate_serial_number()?;

        // Step 6: Lock key slot (if requested)
        if lock_key {
            log::info!("Locking key slot (write-once mode)");
            // Note: This is provider-specific
            // For secure elements, this would call slot locking API
            // For TPM, this would set key policy
            // For now, this is a no-op (placeholder)
        }

        log::info!("Device provisioned successfully: {}", device_id);

        Ok(ProvisioningResult {
            key_handle,
            certificate,
            certificate_chain,
            device_id: device_id.id().to_string(),
            serial_number,
        })
    }

    /// Provision multiple devices in batch
    ///
    /// This is optimized for factory provisioning of many devices.
    ///
    /// # Arguments
    ///
    /// * `ca` - Certificate Authority
    /// * `provider` - Secure key provider
    /// * `devices` - List of (DeviceIdentity, CertificateConfig) pairs
    /// * `lock_keys` - Lock all key slots after provisioning
    ///
    /// # Returns
    ///
    /// Vec of ProvisioningResults (or errors for failed devices)
    ///
    /// # Example
    ///
    /// ```ignore
    /// let devices = vec![
    ///     (DeviceIdentity::new("device-001"), CertificateConfig::new("device-001")),
    ///     (DeviceIdentity::new("device-002"), CertificateConfig::new("device-002")),
    ///     (DeviceIdentity::new("device-003"), CertificateConfig::new("device-003")),
    /// ];
    ///
    /// let results = ProvisioningSession::provision_batch(
    ///     &ca,
    ///     &mut provider,
    ///     devices,
    ///     true,
    /// );
    ///
    /// for (i, result) in results.iter().enumerate() {
    ///     match result {
    ///         Ok(r) => println!("Device {}: OK", r.device_id),
    ///         Err(e) => println!("Device {}: FAILED - {}", i, e),
    ///     }
    /// }
    /// ```
    pub fn provision_batch(
        ca: &PrivateCA,
        provider: &dyn SecureKeyProvider,
        devices: Vec<(DeviceIdentity, CertificateConfig)>,
        lock_keys: bool,
    ) -> Vec<Result<ProvisioningResult, WSError>> {
        devices
            .into_iter()
            .map(|(device_id, config)| Self::provision(ca, provider, device_id, config, lock_keys))
            .collect()
    }

    /// Generate a unique serial number for certificate
    fn generate_serial_number() -> Result<Vec<u8>, WSError> {
        use crate::provisioning::current_timestamp;

        // Use timestamp + random bytes for uniqueness
        let timestamp = current_timestamp()?;

        // Simple serial: timestamp as 8 bytes
        let serial = timestamp.to_be_bytes().to_vec();

        Ok(serial)
    }

    /// Verify a provisioned device
    ///
    /// This checks that:
    /// 1. Device can sign with its key
    /// 2. Signature verifies with public key in certificate
    /// 3. Certificate chain is valid
    ///
    /// This is used for quality control in factory.
    pub fn verify_provisioned_device(
        provider: &dyn SecureKeyProvider,
        result: &ProvisioningResult,
        test_data: &[u8],
    ) -> Result<(), WSError> {
        // Sign test data
        log::info!("Testing device signature");
        let signature = provider.sign(result.key_handle, test_data)?;

        // Get public key
        let _public_key = provider.get_public_key(result.key_handle)?;

        // Verify signature
        // Note: This requires implementing verification in PublicKey
        // For now, just check that signature is non-empty
        if signature.is_empty() {
            return Err(WSError::VerificationError("Empty signature".to_string()));
        }

        log::info!("Device verification successful");
        Ok(())
    }
}

/// Provisioning statistics
///
/// Tracks metrics for factory provisioning operations.
#[derive(Debug, Default, Clone)]
pub struct ProvisioningStats {
    /// Total devices provisioned
    pub total_provisioned: usize,
    /// Successful provisions
    pub successful: usize,
    /// Failed provisions
    pub failed: usize,
    /// Average provisioning time (milliseconds)
    pub avg_time_ms: u64,
}

impl ProvisioningStats {
    /// Create new empty stats
    pub fn new() -> Self {
        Self::default()
    }

    /// Record a successful provisioning
    pub fn record_success(&mut self, duration_ms: u64) {
        self.total_provisioned += 1;
        self.successful += 1;
        self.update_avg_time(duration_ms);
    }

    /// Record a failed provisioning
    pub fn record_failure(&mut self, duration_ms: u64) {
        self.total_provisioned += 1;
        self.failed += 1;
        self.update_avg_time(duration_ms);
    }

    /// Update average time
    fn update_avg_time(&mut self, duration_ms: u64) {
        let total = self.total_provisioned;
        if total == 1 {
            self.avg_time_ms = duration_ms;
        } else {
            // Moving average
            self.avg_time_ms =
                ((self.avg_time_ms * (total - 1) as u64) + duration_ms) / total as u64;
        }
    }

    /// Get success rate (0.0 to 1.0)
    pub fn success_rate(&self) -> f64 {
        if self.total_provisioned == 0 {
            0.0
        } else {
            self.successful as f64 / self.total_provisioned as f64
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::platform::software::SoftwareProvider;
    use crate::provisioning::ca::{CAConfig, PrivateCA};

    #[test]
    fn test_generate_serial_number() {
        let serial1 = ProvisioningSession::generate_serial_number().unwrap();
        let _serial2 = ProvisioningSession::generate_serial_number().unwrap();

        assert_eq!(serial1.len(), 8); // 64-bit timestamp
        assert!(!serial1.is_empty());

        // Serials should be different (unless generated at exact same timestamp)
        // This is probabilistic but should pass
    }

    #[test]
    fn test_provision_device() {
        // Create CA
        let ca_config = CAConfig::new("Test Corp", "Test CA");
        let ca = PrivateCA::create_root(ca_config).unwrap();

        // Create provider
        let provider = SoftwareProvider::new();

        // Provision device
        let device_id = DeviceIdentity::new("device-test-001");
        let config = CertificateConfig::new("device-test-001");

        let result = ProvisioningSession::provision(
            &ca, &provider, device_id, config, false, // Don't lock for testing
        );

        assert!(result.is_ok());
        let result = result.unwrap();

        assert_eq!(result.device_id, "device-test-001");
        assert!(!result.certificate.is_empty());
        assert!(!result.certificate_chain.is_empty());
        assert!(!result.serial_number.is_empty());
    }

    #[test]
    fn test_provision_batch() {
        let ca_config = CAConfig::new("Test Corp", "Test CA");
        let ca = PrivateCA::create_root(ca_config).unwrap();
        let provider = SoftwareProvider::new();

        let devices = vec![
            (
                DeviceIdentity::new("device-001"),
                CertificateConfig::new("device-001"),
            ),
            (
                DeviceIdentity::new("device-002"),
                CertificateConfig::new("device-002"),
            ),
            (
                DeviceIdentity::new("device-003"),
                CertificateConfig::new("device-003"),
            ),
        ];

        let results = ProvisioningSession::provision_batch(&ca, &provider, devices, false);

        assert_eq!(results.len(), 3);
        for result in results {
            assert!(result.is_ok());
        }
    }

    #[test]
    fn test_provisioning_stats() {
        let mut stats = ProvisioningStats::new();

        assert_eq!(stats.total_provisioned, 0);
        assert_eq!(stats.successful, 0);
        assert_eq!(stats.failed, 0);

        stats.record_success(100);
        assert_eq!(stats.total_provisioned, 1);
        assert_eq!(stats.successful, 1);
        assert_eq!(stats.avg_time_ms, 100);

        stats.record_success(200);
        assert_eq!(stats.total_provisioned, 2);
        assert_eq!(stats.successful, 2);
        assert_eq!(stats.avg_time_ms, 150); // (100 + 200) / 2

        stats.record_failure(50);
        assert_eq!(stats.total_provisioned, 3);
        assert_eq!(stats.successful, 2);
        assert_eq!(stats.failed, 1);

        assert_eq!(stats.success_rate(), 2.0 / 3.0);
    }

    #[test]
    fn test_verify_provisioned_device() {
        let ca_config = CAConfig::new("Test Corp", "Test CA");
        let ca = PrivateCA::create_root(ca_config).unwrap();
        let provider = SoftwareProvider::new();

        let device_id = DeviceIdentity::new("device-test");
        let config = CertificateConfig::new("device-test");

        let result =
            ProvisioningSession::provision(&ca, &provider, device_id, config, false).unwrap();

        // Verify device can sign
        let test_data = b"test data for verification";
        let verify_result =
            ProvisioningSession::verify_provisioned_device(&provider, &result, test_data);

        assert!(verify_result.is_ok());
    }
}