wsc 0.8.4

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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//! Trust Bundle data structures
//!
//! A Trust Bundle is a signed, versioned container of trust anchors
//! for offline Sigstore signature verification.

use crate::error::WSError;
use serde::{Deserialize, Serialize};

/// Current trust bundle format version
pub const TRUST_BUNDLE_FORMAT_VERSION: u8 = 1;

/// Maximum grace period: 365 days (in seconds)
///
/// SECURITY: Prevents malicious bundles from setting an excessively large
/// grace period that would effectively disable expiry checking.
pub const MAX_GRACE_PERIOD_SECONDS: u64 = 365 * 86400;

/// Trust bundle containing all trust anchors for offline verification
///
/// This structure contains:
/// - Fulcio root certificates (to anchor certificate chains)
/// - Rekor public keys (to verify Signed Entry Timestamps)
/// - Revocation list (certificate fingerprints to reject)
/// - Validity period with grace period support
///
/// The bundle is versioned for anti-rollback protection - devices reject
/// bundles with a version lower than their stored version.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrustBundle {
    /// Format version for forward compatibility
    pub format_version: u8,

    /// Monotonically increasing bundle version (anti-rollback)
    ///
    /// Devices must reject bundles with `version < stored_version`.
    /// Increment this on every bundle update.
    pub version: u32,

    /// Unique bundle identifier (SHA-256 of canonical form, hex-encoded)
    #[serde(default)]
    pub bundle_id: String,

    /// When this bundle was created (Unix timestamp)
    pub created_at: u64,

    /// Bundle validity period
    pub validity: ValidityPeriod,

    /// Fulcio certificate authorities
    ///
    /// Contains root and intermediate certificates used to anchor
    /// the certificate chains in keyless signatures.
    pub certificate_authorities: Vec<CertificateAuthority>,

    /// Rekor transparency log configurations
    ///
    /// Contains public keys for verifying Signed Entry Timestamps.
    pub transparency_logs: Vec<TransparencyLog>,

    /// Revoked certificate fingerprints
    ///
    /// SHA-256 hashes of DER-encoded leaf certificates that should
    /// be rejected even if otherwise valid. Hex-encoded.
    #[serde(default)]
    pub revocations: Vec<String>,
}

impl TrustBundle {
    /// Create a new empty trust bundle
    pub fn new(version: u32, validity_days: u32) -> Self {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        Self {
            format_version: TRUST_BUNDLE_FORMAT_VERSION,
            version,
            bundle_id: String::new(),
            created_at: now,
            validity: ValidityPeriod {
                not_before: now,
                not_after: now + (validity_days as u64 * 86400),
                grace_period_seconds: 30 * 86400, // 30 days default
            },
            certificate_authorities: Vec::new(),
            transparency_logs: Vec::new(),
            revocations: Vec::new(),
        }
    }

    /// Set the grace period, capping at MAX_GRACE_PERIOD_SECONDS (365 days).
    ///
    /// Returns the actual grace period applied (may be less than requested).
    pub fn set_grace_period(&mut self, seconds: u64) -> u64 {
        let capped = seconds.min(MAX_GRACE_PERIOD_SECONDS);
        if capped < seconds {
            log::warn!(
                "Grace period capped from {} to {} seconds (max {} days)",
                seconds,
                capped,
                MAX_GRACE_PERIOD_SECONDS / 86400
            );
        }
        self.validity.grace_period_seconds = capped;
        capped
    }

    /// Add a certificate authority
    pub fn add_certificate_authority(&mut self, ca: CertificateAuthority) {
        self.certificate_authorities.push(ca);
    }

    /// Add a transparency log
    pub fn add_transparency_log(&mut self, log: TransparencyLog) {
        self.transparency_logs.push(log);
    }

    /// Add a revoked certificate fingerprint
    pub fn add_revocation(&mut self, fingerprint: String) {
        if !self.revocations.contains(&fingerprint) {
            self.revocations.push(fingerprint);
        }
    }

    /// Check if the bundle is currently valid
    pub fn is_valid(&self, current_time: u64) -> bool {
        current_time >= self.validity.not_before && current_time <= self.validity.not_after
    }

    /// Check if the bundle is in grace period
    ///
    /// SECURITY: Caps grace period to MAX_GRACE_PERIOD_SECONDS and uses checked
    /// arithmetic to prevent overflow in the `not_after + grace` calculation.
    pub fn is_in_grace_period(&self, current_time: u64) -> bool {
        let capped_grace = self
            .validity
            .grace_period_seconds
            .min(MAX_GRACE_PERIOD_SECONDS);
        let grace_end = self
            .validity
            .not_after
            .checked_add(capped_grace)
            .unwrap_or(u64::MAX);
        current_time > self.validity.not_after && current_time <= grace_end
    }

    /// Check if a certificate fingerprint is revoked
    pub fn is_revoked(&self, fingerprint: &str) -> bool {
        self.revocations.iter().any(|r| r == fingerprint)
    }

    /// Compute bundle ID (SHA-256 of canonical JSON)
    pub fn compute_bundle_id(&mut self) -> Result<(), WSError> {
        // Temporarily clear bundle_id for canonical form
        let old_id = std::mem::take(&mut self.bundle_id);

        let canonical = serde_json::to_vec(self)
            .map_err(|e| WSError::InternalError(format!("Failed to serialize bundle: {}", e)))?;

        let hash = hmac_sha256::Hash::hash(&canonical);
        self.bundle_id = hex::encode(hash);

        // Restore if computation failed
        if self.bundle_id.is_empty() {
            self.bundle_id = old_id;
        }

        Ok(())
    }

    /// Serialize to JSON bytes
    pub fn to_json(&self) -> Result<Vec<u8>, WSError> {
        serde_json::to_vec_pretty(self)
            .map_err(|e| WSError::InternalError(format!("Failed to serialize bundle: {}", e)))
    }

    /// Deserialize from JSON bytes
    pub fn from_json(data: &[u8]) -> Result<Self, WSError> {
        serde_json::from_slice(data)
            .map_err(|e| WSError::InternalError(format!("Failed to parse bundle: {}", e)))
    }
}

/// Validity period with grace period support
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidityPeriod {
    /// Start of validity (Unix timestamp)
    pub not_before: u64,

    /// End of validity (Unix timestamp)
    pub not_after: u64,

    /// Grace period after `not_after` (seconds)
    ///
    /// During the grace period, verification succeeds with warnings.
    /// This allows time for bundle updates without hard failures.
    #[serde(default)]
    pub grace_period_seconds: u64,
}

/// Certificate authority entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CertificateAuthority {
    /// Human-readable name (e.g., "Sigstore Public Good Instance")
    pub name: String,

    /// URI identifier (e.g., "https://fulcio.sigstore.dev")
    pub uri: String,

    /// PEM-encoded certificates (root and intermediates)
    ///
    /// Multiple certificates can be included for chain building.
    pub certificates_pem: Vec<String>,

    /// Validity period for this CA
    ///
    /// Used for historical verification - old signatures may use
    /// older CA certificates that are no longer active.
    pub valid_for: ValidityPeriod,
}

impl CertificateAuthority {
    /// Create a new CA entry from PEM certificates
    pub fn new(name: &str, uri: &str, pem_certs: Vec<String>, validity_days: u32) -> Self {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        Self {
            name: name.to_string(),
            uri: uri.to_string(),
            certificates_pem: pem_certs,
            valid_for: ValidityPeriod {
                not_before: now,
                not_after: now + (validity_days as u64 * 86400),
                grace_period_seconds: 0,
            },
        }
    }
}

/// Transparency log entry (Rekor)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransparencyLog {
    /// Base URL (e.g., "https://rekor.sigstore.dev")
    pub base_url: String,

    /// Hash algorithm used (e.g., "sha256")
    pub hash_algorithm: String,

    /// PEM-encoded public key for SET verification
    pub public_key_pem: String,

    /// Log ID (SHA-256 of public key, hex-encoded)
    pub log_id: String,

    /// Validity period for this key
    pub valid_for: ValidityPeriod,
}

impl TransparencyLog {
    /// Create a new transparency log entry
    pub fn new(base_url: &str, public_key_pem: &str, validity_days: u32) -> Result<Self, WSError> {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        // Compute log ID from public key
        let log_id = Self::compute_log_id(public_key_pem)?;

        Ok(Self {
            base_url: base_url.to_string(),
            hash_algorithm: "sha256".to_string(),
            public_key_pem: public_key_pem.to_string(),
            log_id,
            valid_for: ValidityPeriod {
                not_before: now,
                not_after: now + (validity_days as u64 * 86400),
                grace_period_seconds: 0,
            },
        })
    }

    /// Compute log ID from PEM-encoded public key
    fn compute_log_id(pem: &str) -> Result<String, WSError> {
        // Extract DER from PEM
        let der = pem
            .lines()
            .filter(|line| !line.starts_with("-----"))
            .collect::<String>();

        let der_bytes = base64::Engine::decode(
            &base64::engine::general_purpose::STANDARD,
            &der,
        )
        .map_err(|e| WSError::InternalError(format!("Invalid PEM encoding: {}", e)))?;

        let hash = hmac_sha256::Hash::hash(&der_bytes);
        Ok(hex::encode(hash))
    }
}

/// Signed trust bundle for secure distribution
///
/// The bundle is signed with a long-lived offline key. Devices verify
/// the signature against a pre-provisioned public key before using
/// the bundle contents.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SignedTrustBundle {
    /// The trust bundle
    pub bundle: TrustBundle,

    /// Signature over the bundle
    pub signature: BundleSignature,
}

impl SignedTrustBundle {
    /// Create a signed bundle
    pub fn sign(bundle: TrustBundle, signing_key: &[u8]) -> Result<Self, WSError> {
        let bundle_bytes = bundle.to_json()?;
        let signature = BundleSignature::sign(&bundle_bytes, signing_key)?;

        Ok(Self { bundle, signature })
    }

    /// Verify the bundle signature
    pub fn verify(&self, verifier_key: &[u8]) -> Result<(), WSError> {
        let bundle_bytes = self.bundle.to_json()?;
        self.signature.verify(&bundle_bytes, verifier_key)
    }

    /// Serialize to JSON
    pub fn to_json(&self) -> Result<Vec<u8>, WSError> {
        serde_json::to_vec_pretty(self)
            .map_err(|e| WSError::InternalError(format!("Failed to serialize signed bundle: {}", e)))
    }

    /// Deserialize from JSON
    pub fn from_json(data: &[u8]) -> Result<Self, WSError> {
        serde_json::from_slice(data)
            .map_err(|e| WSError::InternalError(format!("Failed to parse signed bundle: {}", e)))
    }
}

/// Signature over trust bundle
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BundleSignature {
    /// Key identifier (first 8 hex chars of SHA-256 of public key)
    pub key_id: String,

    /// Signature algorithm
    pub algorithm: SignatureAlgorithm,

    /// Raw signature bytes (base64-encoded)
    pub signature: String,
}

impl BundleSignature {
    /// Sign data with Ed25519 key
    pub fn sign(data: &[u8], secret_key: &[u8]) -> Result<Self, WSError> {
        use ed25519_compact::{KeyPair, Seed};

        // Create keypair from secret key bytes
        let seed = if secret_key.len() == 32 {
            Seed::from_slice(secret_key)
                .map_err(|e| WSError::CryptoError(e))?
        } else if secret_key.len() == 64 {
            // Full keypair format - extract seed
            Seed::from_slice(&secret_key[..32])
                .map_err(|e| WSError::CryptoError(e))?
        } else {
            return Err(WSError::InvalidArgument);
        };

        let keypair = KeyPair::from_seed(seed);

        // Compute key ID
        let pk_bytes = keypair.pk.as_ref();
        let key_hash = hmac_sha256::Hash::hash(pk_bytes);
        let key_id = hex::encode(&key_hash[..4]);

        // Sign
        let sig = keypair.sk.sign(data, None);
        let signature = base64::Engine::encode(
            &base64::engine::general_purpose::STANDARD,
            sig.as_ref(),
        );

        Ok(Self {
            key_id,
            algorithm: SignatureAlgorithm::Ed25519,
            signature,
        })
    }

    /// Verify signature
    pub fn verify(&self, data: &[u8], public_key: &[u8]) -> Result<(), WSError> {
        use ed25519_compact::{PublicKey, Signature};

        match self.algorithm {
            SignatureAlgorithm::Ed25519 => {
                let pk = PublicKey::from_slice(public_key)
                    .map_err(|e| WSError::CryptoError(e))?;

                let sig_bytes = base64::Engine::decode(
                    &base64::engine::general_purpose::STANDARD,
                    &self.signature,
                )
                .map_err(|_| WSError::InvalidArgument)?;

                let sig = Signature::from_slice(&sig_bytes)
                    .map_err(|e| WSError::CryptoError(e))?;

                pk.verify(data, &sig)
                    .map_err(|e| WSError::CryptoError(e))
            }
            _ => Err(WSError::UnsupportedAlgorithm(format!("{:?}", self.algorithm))),
        }
    }
}

/// Supported signature algorithms for bundles
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SignatureAlgorithm {
    /// Ed25519 (recommended for embedded - small keys and signatures)
    Ed25519,
    /// ECDSA with P-256 curve
    EcdsaP256Sha256,
    /// ECDSA with P-384 curve
    EcdsaP384Sha384,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_trust_bundle_creation() {
        let bundle = TrustBundle::new(1, 365);
        assert_eq!(bundle.format_version, TRUST_BUNDLE_FORMAT_VERSION);
        assert_eq!(bundle.version, 1);
        assert!(bundle.is_valid(bundle.created_at));
    }

    #[test]
    fn test_trust_bundle_validity() {
        let mut bundle = TrustBundle::new(1, 365);
        bundle.validity.not_before = 1000;
        bundle.validity.not_after = 2000;
        bundle.validity.grace_period_seconds = 500;

        assert!(!bundle.is_valid(500)); // Before not_before
        assert!(bundle.is_valid(1500)); // Within validity
        assert!(!bundle.is_valid(2500)); // After not_after

        assert!(!bundle.is_in_grace_period(1500)); // Still valid
        assert!(bundle.is_in_grace_period(2100)); // In grace period
        assert!(!bundle.is_in_grace_period(2600)); // Past grace period
    }

    #[test]
    fn test_trust_bundle_revocation() {
        let mut bundle = TrustBundle::new(1, 365);
        bundle.add_revocation("abc123".to_string());

        assert!(bundle.is_revoked("abc123"));
        assert!(!bundle.is_revoked("def456"));
    }

    #[test]
    fn test_trust_bundle_json_roundtrip() {
        let mut bundle = TrustBundle::new(1, 365);
        bundle.add_certificate_authority(CertificateAuthority::new(
            "Test CA",
            "https://example.com",
            vec!["-----BEGIN CERTIFICATE-----\ntest\n-----END CERTIFICATE-----".to_string()],
            365,
        ));

        let json = bundle.to_json().unwrap();
        let parsed = TrustBundle::from_json(&json).unwrap();

        assert_eq!(parsed.version, bundle.version);
        assert_eq!(parsed.certificate_authorities.len(), 1);
    }

    #[test]
    fn test_signed_bundle_roundtrip() {
        use ed25519_compact::KeyPair;

        // Generate test keypair
        let keypair = KeyPair::generate();
        let seed = keypair.sk.seed();
        let secret_key = seed.as_ref();

        let bundle = TrustBundle::new(1, 365);
        let signed = SignedTrustBundle::sign(bundle, secret_key).unwrap();

        // Verify with correct key
        let public_key = keypair.pk.as_ref();
        assert!(signed.verify(public_key).is_ok());

        // Verify roundtrip
        let json = signed.to_json().unwrap();
        let parsed = SignedTrustBundle::from_json(&json).unwrap();
        assert!(parsed.verify(public_key).is_ok());
    }

    #[test]
    fn test_signed_bundle_wrong_key_fails() {
        use ed25519_compact::KeyPair;

        let keypair1 = KeyPair::generate();
        let keypair2 = KeyPair::generate();

        let bundle = TrustBundle::new(1, 365);
        let seed1 = keypair1.sk.seed();
        let signed = SignedTrustBundle::sign(bundle, seed1.as_ref()).unwrap();

        // Wrong key should fail
        let result = signed.verify(keypair2.pk.as_ref());
        assert!(result.is_err());
    }
}