wsc 0.8.1

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
//! Supply chain verification policy engine.
//!
//! This module provides a policy engine for enforcing SLSA levels and
//! supply chain security policies on WebAssembly transformation chains.
//!
//! # Policy File Format (TOML)
//!
//! ```toml
//! [policy]
//! name = "production-strict"
//! version = "1.0"
//! enforcement = "strict"  # or "report"
//!
//! [slsa]
//! minimum_level = 2  # L0-L4
//! enforcement = "strict"
//!
//! [signatures]
//! require_root_signatures = true
//! require_attestation_signatures = true
//! max_attestation_age_days = 30
//!
//! [trusted_tools.loom]
//! min_version = "0.1.0"
//! public_keys = [{ algorithm = "ed25519", key = "...", key_id = "loom-prod" }]
//!
//! [trusted_builders.github-actions]
//! builder_id = "https://github.com/actions/runner"
//! oidc_issuer = "https://token.actions.githubusercontent.com"
//! ```
//!
//! # Example
//!
//! ```rust,ignore
//! use wsc::policy::{Policy, evaluate_policy};
//! use wsc::composition::TransformationAttestation;
//!
//! let policy = Policy::from_toml_file("wsc-policy.toml")?;
//! let result = evaluate_policy(&attestation, &policy);
//!
//! if result.passed {
//!     println!("SLSA Level: {:?}", result.slsa_level);
//! } else {
//!     for rule in result.rules.iter().filter(|r| !r.passed) {
//!         eprintln!("FAILED: {} - {}", rule.rule, rule.message);
//!     }
//! }
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;

pub mod slsa;
pub mod eval;

#[cfg(feature = "rego")]
pub mod rego;

pub use slsa::{SlsaLevel, detect_slsa_level};
pub use eval::{evaluate_policy, PolicyEvaluationResult, RuleResult, PolicySummary};

// ============================================================================
// Core Policy Types
// ============================================================================

/// Enforcement mode for policy rules.
///
/// Determines whether a rule violation causes verification to fail
/// or is just reported as a warning.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Enforcement {
    /// Violation causes verification to fail
    #[default]
    Strict,
    /// Violation is reported but doesn't fail verification
    Report,
}

impl std::fmt::Display for Enforcement {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Enforcement::Strict => write!(f, "strict"),
            Enforcement::Report => write!(f, "report"),
        }
    }
}

/// A supply chain verification policy.
///
/// Policies define requirements for SLSA levels, signatures, trusted tools,
/// and trusted builders. Each section can have its own enforcement mode.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Policy {
    /// Policy metadata
    #[serde(default)]
    pub policy: PolicyMetadata,

    /// SLSA level requirements
    #[serde(default)]
    pub slsa: SlsaPolicy,

    /// Signature requirements
    #[serde(default)]
    pub signatures: SignaturePolicy,

    /// Trusted transformation tools
    #[serde(default)]
    pub trusted_tools: HashMap<String, TrustedToolPolicy>,

    /// Trusted CI/CD builders for SLSA provenance
    #[serde(default)]
    pub trusted_builders: HashMap<String, TrustedBuilderPolicy>,
}

/// Policy metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PolicyMetadata {
    /// Human-readable policy name
    #[serde(default = "default_policy_name")]
    pub name: String,

    /// Policy version
    #[serde(default = "default_policy_version")]
    pub version: String,

    /// Default enforcement mode for all rules
    #[serde(default)]
    pub enforcement: Enforcement,
}

fn default_policy_name() -> String {
    "default".to_string()
}

fn default_policy_version() -> String {
    "1.0".to_string()
}

impl Default for PolicyMetadata {
    fn default() -> Self {
        Self {
            name: default_policy_name(),
            version: default_policy_version(),
            enforcement: Enforcement::default(),
        }
    }
}

/// SLSA-specific policy requirements.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SlsaPolicy {
    /// Minimum required SLSA level (0-4)
    #[serde(default)]
    pub minimum_level: u8,

    /// Enforcement mode for SLSA checks
    #[serde(default)]
    pub enforcement: Option<Enforcement>,
}

impl Default for SlsaPolicy {
    fn default() -> Self {
        Self {
            minimum_level: 0,
            enforcement: None,
        }
    }
}

/// Signature requirements policy.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SignaturePolicy {
    /// Require root components to be signed
    #[serde(default)]
    pub require_root_signatures: bool,

    /// Require transformation attestations to be signed
    #[serde(default)]
    pub require_attestation_signatures: bool,

    /// Maximum age for attestations in days
    #[serde(default)]
    pub max_attestation_age_days: Option<u64>,

    /// Enforcement mode for signature checks
    #[serde(default)]
    pub enforcement: Option<Enforcement>,
}

impl Default for SignaturePolicy {
    fn default() -> Self {
        Self {
            require_root_signatures: false,
            require_attestation_signatures: false,
            max_attestation_age_days: None,
            enforcement: None,
        }
    }
}

impl SignaturePolicy {
    /// Get max attestation age as Duration
    pub fn max_attestation_age(&self) -> Option<Duration> {
        self.max_attestation_age_days
            .map(|days| Duration::from_secs(days * 24 * 60 * 60))
    }
}

/// Trusted transformation tool configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrustedToolPolicy {
    /// Minimum required version (semver)
    #[serde(default)]
    pub min_version: Option<String>,

    /// Maximum allowed version (semver)
    #[serde(default)]
    pub max_version: Option<String>,

    /// Required tool binary hash
    #[serde(default)]
    pub required_hash: Option<String>,

    /// Trusted public keys for signature verification
    #[serde(default)]
    pub public_keys: Vec<TrustedPublicKeyConfig>,

    /// Keyless (OIDC) verification configuration
    #[serde(default)]
    pub keyless: Option<KeylessConfig>,

    /// Enforcement mode for this tool
    #[serde(default)]
    pub enforcement: Option<Enforcement>,
}

impl Default for TrustedToolPolicy {
    fn default() -> Self {
        Self {
            min_version: None,
            max_version: None,
            required_hash: None,
            public_keys: Vec::new(),
            keyless: None,
            enforcement: None,
        }
    }
}

/// Public key configuration for trusted tools.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrustedPublicKeyConfig {
    /// Signing algorithm (e.g., "ed25519", "ecdsa-p256")
    pub algorithm: String,

    /// Base64-encoded public key
    pub key: String,

    /// Optional key identifier
    #[serde(default)]
    pub key_id: Option<String>,
}

/// Keyless (OIDC) verification configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeylessConfig {
    /// Trusted OIDC token issuers
    #[serde(default)]
    pub oidc_issuers: Vec<String>,

    /// Allowed certificate subjects (supports wildcards)
    #[serde(default)]
    pub subjects: Vec<String>,
}

/// Trusted CI/CD builder configuration for SLSA provenance.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrustedBuilderPolicy {
    /// Builder identity URI
    pub builder_id: String,

    /// Expected OIDC issuer
    #[serde(default)]
    pub oidc_issuer: Option<String>,

    /// Allowed repository patterns (supports wildcards)
    #[serde(default)]
    pub allowed_repos: Vec<String>,

    /// Enforcement mode for this builder
    #[serde(default)]
    pub enforcement: Option<Enforcement>,
}

// ============================================================================
// Policy Loading
// ============================================================================

/// Error type for policy operations.
#[derive(Debug, Clone)]
pub enum PolicyError {
    /// Failed to parse TOML
    ParseError(String),
    /// Failed to read file
    IoError(String),
    /// Invalid policy configuration
    ValidationError(String),
}

impl std::fmt::Display for PolicyError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PolicyError::ParseError(msg) => write!(f, "Policy parse error: {}", msg),
            PolicyError::IoError(msg) => write!(f, "Policy I/O error: {}", msg),
            PolicyError::ValidationError(msg) => write!(f, "Policy validation error: {}", msg),
        }
    }
}

impl std::error::Error for PolicyError {}

impl Policy {
    /// Create a default permissive policy (no requirements).
    pub fn permissive() -> Self {
        Self {
            policy: PolicyMetadata {
                name: "permissive".to_string(),
                version: "1.0".to_string(),
                enforcement: Enforcement::Report,
            },
            slsa: SlsaPolicy::default(),
            signatures: SignaturePolicy::default(),
            trusted_tools: HashMap::new(),
            trusted_builders: HashMap::new(),
        }
    }

    /// Create a strict policy requiring SLSA L2 and signed attestations.
    pub fn strict() -> Self {
        Self {
            policy: PolicyMetadata {
                name: "strict".to_string(),
                version: "1.0".to_string(),
                enforcement: Enforcement::Strict,
            },
            slsa: SlsaPolicy {
                minimum_level: 2,
                enforcement: Some(Enforcement::Strict),
            },
            signatures: SignaturePolicy {
                require_root_signatures: true,
                require_attestation_signatures: true,
                max_attestation_age_days: Some(30),
                enforcement: Some(Enforcement::Strict),
            },
            trusted_tools: HashMap::new(),
            trusted_builders: HashMap::new(),
        }
    }

    /// Parse policy from TOML string.
    pub fn from_toml(toml_str: &str) -> Result<Self, PolicyError> {
        toml::from_str(toml_str).map_err(|e| PolicyError::ParseError(e.to_string()))
    }

    /// Load policy from TOML file.
    pub fn from_toml_file(path: &str) -> Result<Self, PolicyError> {
        let content = std::fs::read_to_string(path)
            .map_err(|e| PolicyError::IoError(format!("{}: {}", path, e)))?;
        Self::from_toml(&content)
    }

    /// Serialize policy to TOML string.
    pub fn to_toml(&self) -> Result<String, PolicyError> {
        toml::to_string_pretty(self).map_err(|e| PolicyError::ParseError(e.to_string()))
    }

    /// Get effective enforcement for a section.
    pub fn effective_enforcement(&self, section_enforcement: Option<Enforcement>) -> Enforcement {
        section_enforcement.unwrap_or(self.policy.enforcement)
    }

    /// Add a trusted tool to the policy.
    pub fn add_trusted_tool(&mut self, name: impl Into<String>, tool: TrustedToolPolicy) {
        self.trusted_tools.insert(name.into(), tool);
    }

    /// Add a trusted builder to the policy.
    pub fn add_trusted_builder(&mut self, name: impl Into<String>, builder: TrustedBuilderPolicy) {
        self.trusted_builders.insert(name.into(), builder);
    }

    /// Check if a tool is trusted.
    pub fn is_tool_trusted(&self, name: &str) -> bool {
        self.trusted_tools.contains_key(name)
    }

    /// Get trusted tool configuration.
    pub fn get_trusted_tool(&self, name: &str) -> Option<&TrustedToolPolicy> {
        self.trusted_tools.get(name)
    }
}

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

    #[test]
    fn test_parse_minimal_policy() {
        let toml = r#"
[policy]
name = "test"
"#;
        let policy = Policy::from_toml(toml).unwrap();
        assert_eq!(policy.policy.name, "test");
        assert_eq!(policy.slsa.minimum_level, 0);
    }

    #[test]
    fn test_parse_full_policy() {
        let toml = r#"
[policy]
name = "production"
version = "1.0"
enforcement = "strict"

[slsa]
minimum_level = 2
enforcement = "strict"

[signatures]
require_root_signatures = true
require_attestation_signatures = true
max_attestation_age_days = 30

[trusted_tools.loom]
min_version = "0.1.0"
public_keys = [{ algorithm = "ed25519", key = "abc123", key_id = "loom-prod" }]

[trusted_tools.wac]
min_version = "0.5.0"
keyless = { oidc_issuers = ["https://token.actions.githubusercontent.com"], subjects = ["https://github.com/bytecodealliance/*"] }

[trusted_builders.github-actions]
builder_id = "https://github.com/actions/runner"
oidc_issuer = "https://token.actions.githubusercontent.com"
allowed_repos = ["pulseengine/*"]
"#;
        let policy = Policy::from_toml(toml).unwrap();

        assert_eq!(policy.policy.name, "production");
        assert_eq!(policy.policy.enforcement, Enforcement::Strict);
        assert_eq!(policy.slsa.minimum_level, 2);
        assert!(policy.signatures.require_root_signatures);
        assert!(policy.signatures.require_attestation_signatures);
        assert_eq!(policy.signatures.max_attestation_age_days, Some(30));

        assert!(policy.trusted_tools.contains_key("loom"));
        assert!(policy.trusted_tools.contains_key("wac"));

        let loom = policy.trusted_tools.get("loom").unwrap();
        assert_eq!(loom.min_version, Some("0.1.0".to_string()));
        assert_eq!(loom.public_keys.len(), 1);

        let wac = policy.trusted_tools.get("wac").unwrap();
        assert!(wac.keyless.is_some());

        assert!(policy.trusted_builders.contains_key("github-actions"));
    }

    #[test]
    fn test_enforcement_default() {
        let policy = Policy::permissive();
        assert_eq!(policy.policy.enforcement, Enforcement::Report);

        let policy = Policy::strict();
        assert_eq!(policy.policy.enforcement, Enforcement::Strict);
    }

    #[test]
    fn test_effective_enforcement() {
        let mut policy = Policy::permissive();
        policy.policy.enforcement = Enforcement::Report;

        // Section without override uses default
        assert_eq!(
            policy.effective_enforcement(None),
            Enforcement::Report
        );

        // Section with override uses override
        assert_eq!(
            policy.effective_enforcement(Some(Enforcement::Strict)),
            Enforcement::Strict
        );
    }

    #[test]
    fn test_policy_to_toml() {
        let policy = Policy::strict();
        let toml = policy.to_toml().unwrap();

        assert!(toml.contains("[policy]"));
        assert!(toml.contains("strict"));

        // Round-trip
        let parsed = Policy::from_toml(&toml).unwrap();
        assert_eq!(parsed.policy.name, policy.policy.name);
    }

    #[test]
    fn test_max_attestation_age() {
        let mut policy = SignaturePolicy::default();
        assert!(policy.max_attestation_age().is_none());

        policy.max_attestation_age_days = Some(30);
        let duration = policy.max_attestation_age().unwrap();
        assert_eq!(duration, Duration::from_secs(30 * 24 * 60 * 60));
    }
}