nono 0.11.0

Capability-based sandboxing library using Landlock (Linux) and Seatbelt (macOS)
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
//! DSSE (Dead Simple Signing Envelope) parsing and PAE construction
//!
//! Implements the DSSE protocol for instruction file attestation:
//! - Envelope parsing and serialization (JSON)
//! - Pre-Authentication Encoding (PAE) for signature verification
//! - In-toto Statement v1 extraction from envelope payload
//!
//! # References
//!
//! - [DSSE protocol](https://github.com/secure-systems-lab/dsse/blob/master/protocol.md)
//! - [In-toto Statement v1](https://github.com/in-toto/attestation/blob/main/spec/v1/statement.md)

use crate::error::{NonoError, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// The expected payload type for in-toto attestation statements.
pub const IN_TOTO_PAYLOAD_TYPE: &str = "application/vnd.in-toto+json";

/// The expected statement type for in-toto v1.
pub const IN_TOTO_STATEMENT_TYPE: &str = "https://in-toto.io/Statement/v1";

/// The predicate type for nono instruction file attestations.
pub const NONO_PREDICATE_TYPE: &str = "https://nono.sh/attestation/instruction-file/v1";

/// The predicate type for nono trust policy attestations.
pub const NONO_POLICY_PREDICATE_TYPE: &str = "https://nono.sh/attestation/trust-policy/v1";

/// The predicate type for nono multi-file attestations.
///
/// Used when multiple files are signed together in a single bundle
/// (e.g., a skill's SKILL.md + companion scripts). All files appear
/// as subjects in the same in-toto statement.
pub const NONO_MULTI_SUBJECT_PREDICATE_TYPE: &str = "https://nono.sh/attestation/multi-file/v1";

/// A DSSE (Dead Simple Signing Envelope).
///
/// Contains a base64url-encoded payload, its type identifier, and one or
/// more signatures over `PAE(payloadType, payload)`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DsseEnvelope {
    /// MIME type of the payload (e.g., `application/vnd.in-toto+json`)
    pub payload_type: String,
    /// Base64url-encoded payload (the in-toto statement)
    pub payload: String,
    /// One or more signatures over `PAE(payloadType, decoded_payload)`
    pub signatures: Vec<DsseSignature>,
}

impl DsseEnvelope {
    /// Parse a DSSE envelope from a JSON string.
    ///
    /// # Errors
    ///
    /// Returns `NonoError::TrustVerification` if the JSON is malformed.
    pub fn from_json(json: &str) -> Result<Self> {
        let envelope: Self =
            serde_json::from_str(json).map_err(|e| NonoError::TrustVerification {
                path: String::new(),
                reason: format!("invalid DSSE envelope: {e}"),
            })?;
        envelope.validate()?;
        Ok(envelope)
    }

    /// Serialize to JSON.
    ///
    /// # Errors
    ///
    /// Returns `NonoError::TrustVerification` if serialization fails.
    pub fn to_json(&self) -> Result<String> {
        serde_json::to_string(self).map_err(|e| NonoError::TrustVerification {
            path: String::new(),
            reason: format!("failed to serialize DSSE envelope: {e}"),
        })
    }

    /// Validate structural integrity of the envelope.
    fn validate(&self) -> Result<()> {
        if self.payload_type.is_empty() {
            return Err(NonoError::TrustVerification {
                path: String::new(),
                reason: "DSSE envelope has empty payloadType".to_string(),
            });
        }
        if self.payload.is_empty() {
            return Err(NonoError::TrustVerification {
                path: String::new(),
                reason: "DSSE envelope has empty payload".to_string(),
            });
        }
        if self.signatures.is_empty() {
            return Err(NonoError::TrustVerification {
                path: String::new(),
                reason: "DSSE envelope has no signatures".to_string(),
            });
        }
        Ok(())
    }

    /// Decode the payload from base64url.
    ///
    /// # Errors
    ///
    /// Returns `NonoError::TrustVerification` if the payload is not valid base64url.
    pub fn decode_payload(&self) -> Result<Vec<u8>> {
        base64url_decode(&self.payload).map_err(|e| NonoError::TrustVerification {
            path: String::new(),
            reason: format!("failed to decode DSSE payload: {e}"),
        })
    }

    /// Decode and parse the payload as an in-toto Statement.
    ///
    /// # Errors
    ///
    /// Returns `NonoError::TrustVerification` if decoding or parsing fails,
    /// or if the payload type is not `application/vnd.in-toto+json`.
    pub fn extract_statement(&self) -> Result<InTotoStatement> {
        if self.payload_type != IN_TOTO_PAYLOAD_TYPE {
            return Err(NonoError::TrustVerification {
                path: String::new(),
                reason: format!(
                    "unexpected DSSE payloadType: expected '{}', got '{}'",
                    IN_TOTO_PAYLOAD_TYPE, self.payload_type
                ),
            });
        }
        let bytes = self.decode_payload()?;
        let json = std::str::from_utf8(&bytes).map_err(|e| NonoError::TrustVerification {
            path: String::new(),
            reason: format!("DSSE payload is not valid UTF-8: {e}"),
        })?;
        InTotoStatement::from_json(json)
    }

    /// Compute the PAE (Pre-Authentication Encoding) for this envelope.
    ///
    /// The PAE is the byte string that signatures are computed over:
    /// `PAE(payloadType, decoded_payload)`
    ///
    /// # Errors
    ///
    /// Returns `NonoError::TrustVerification` if the payload cannot be decoded.
    pub fn pae_bytes(&self) -> Result<Vec<u8>> {
        let decoded = self.decode_payload()?;
        Ok(pae(&self.payload_type, &decoded))
    }
}

/// A single signature within a DSSE envelope.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DsseSignature {
    /// Optional key identifier (not authenticated by DSSE)
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub keyid: String,
    /// Base64url-encoded signature over `PAE(payloadType, payload)`
    pub sig: String,
}

impl DsseSignature {
    /// Decode the signature bytes from base64url.
    ///
    /// # Errors
    ///
    /// Returns `NonoError::TrustVerification` if the signature is not valid base64url.
    pub fn decode_sig(&self) -> Result<Vec<u8>> {
        base64url_decode(&self.sig).map_err(|e| NonoError::TrustVerification {
            path: String::new(),
            reason: format!("failed to decode DSSE signature: {e}"),
        })
    }
}

/// An in-toto attestation Statement (v1).
///
/// The statement is the decoded payload of a DSSE envelope. It identifies
/// the artifact(s) being attested and the predicate (attestation metadata).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InTotoStatement {
    /// Statement type (must be `https://in-toto.io/Statement/v1`)
    #[serde(rename = "_type")]
    pub statement_type: String,
    /// Artifacts being attested
    pub subject: Vec<InTotoSubject>,
    /// Predicate type URI
    pub predicate_type: String,
    /// Predicate content (nono instruction file attestation metadata)
    pub predicate: serde_json::Value,
}

impl InTotoStatement {
    /// Parse from a JSON string.
    ///
    /// # Errors
    ///
    /// Returns `NonoError::TrustVerification` if parsing or validation fails.
    pub fn from_json(json: &str) -> Result<Self> {
        let stmt: Self = serde_json::from_str(json).map_err(|e| NonoError::TrustVerification {
            path: String::new(),
            reason: format!("invalid in-toto statement: {e}"),
        })?;
        stmt.validate()?;
        Ok(stmt)
    }

    /// Validate the statement structure.
    fn validate(&self) -> Result<()> {
        if self.statement_type != IN_TOTO_STATEMENT_TYPE {
            return Err(NonoError::TrustVerification {
                path: String::new(),
                reason: format!(
                    "unexpected in-toto statement type: expected '{}', got '{}'",
                    IN_TOTO_STATEMENT_TYPE, self.statement_type
                ),
            });
        }
        if self.subject.is_empty() {
            return Err(NonoError::TrustVerification {
                path: String::new(),
                reason: "in-toto statement has no subjects".to_string(),
            });
        }
        for subject in &self.subject {
            if subject.name.is_empty() {
                return Err(NonoError::TrustVerification {
                    path: String::new(),
                    reason: "in-toto subject has empty name".to_string(),
                });
            }
            if !subject.digest.contains_key("sha256") {
                return Err(NonoError::TrustVerification {
                    path: String::new(),
                    reason: format!("in-toto subject '{}' missing sha256 digest", subject.name),
                });
            }
        }
        Ok(())
    }

    /// Get the SHA-256 digest of the first subject.
    ///
    /// Returns `None` if the statement has no subjects or the first
    /// subject has no sha256 digest.
    #[must_use]
    pub fn first_subject_digest(&self) -> Option<&str> {
        self.subject
            .first()
            .and_then(|s| s.digest.get("sha256"))
            .map(String::as_str)
    }

    /// Get the name of the first subject.
    #[must_use]
    pub fn first_subject_name(&self) -> Option<&str> {
        self.subject.first().map(|s| s.name.as_str())
    }

    /// Extract the signer identity from the predicate.
    ///
    /// Parses the `predicate.signer` object to determine if the attestation
    /// was produced by a keyed or keyless signer.
    ///
    /// # Errors
    ///
    /// Returns `NonoError::TrustVerification` if the predicate is missing
    /// or has an unrecognized signer kind.
    pub fn extract_signer(&self) -> Result<super::types::SignerIdentity> {
        let signer = self
            .predicate
            .get("signer")
            .ok_or_else(|| NonoError::TrustVerification {
                path: String::new(),
                reason: "predicate missing 'signer' field".to_string(),
            })?;

        let kind = signer.get("kind").and_then(|v| v.as_str()).ok_or_else(|| {
            NonoError::TrustVerification {
                path: String::new(),
                reason: "signer missing 'kind' field".to_string(),
            }
        })?;

        match kind {
            "keyed" => {
                let key_id = signer
                    .get("key_id")
                    .and_then(|v| v.as_str())
                    .ok_or_else(|| NonoError::TrustVerification {
                        path: String::new(),
                        reason: "keyed signer missing 'key_id'".to_string(),
                    })?;
                Ok(super::types::SignerIdentity::Keyed {
                    key_id: key_id.to_string(),
                })
            }
            "keyless" => {
                let issuer = get_str_field(signer, "issuer")?;
                let repository = get_str_field(signer, "repository")?;
                let workflow = signer
                    .get("workflow_ref")
                    .and_then(|v| v.as_str())
                    .map(|s| {
                        // Strip @ref suffix if present (workflow_ref includes it)
                        s.split('@').next().unwrap_or(s).to_string()
                    })
                    .ok_or_else(|| NonoError::TrustVerification {
                        path: String::new(),
                        reason: "keyless signer missing 'workflow_ref'".to_string(),
                    })?;
                let git_ref = signer
                    .get("subject")
                    .and_then(|v| v.as_str())
                    .map(|s| {
                        // Extract ref from "repo:org/repo:ref:refs/..." format
                        extract_ref_from_subject(s)
                    })
                    .ok_or_else(|| NonoError::TrustVerification {
                        path: String::new(),
                        reason: "keyless signer missing 'subject'".to_string(),
                    })?;
                Ok(super::types::SignerIdentity::Keyless {
                    issuer,
                    repository,
                    workflow,
                    git_ref,
                })
            }
            other => Err(NonoError::TrustVerification {
                path: String::new(),
                reason: format!("unknown signer kind: '{other}'"),
            }),
        }
    }
}

/// A single subject in an in-toto statement.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InTotoSubject {
    /// Name of the artifact (typically the filename)
    pub name: String,
    /// Map of digest algorithm to hex-encoded digest value
    pub digest: HashMap<String, String>,
}

// ---------------------------------------------------------------------------
// PAE (Pre-Authentication Encoding)
// ---------------------------------------------------------------------------

/// Compute the DSSE Pre-Authentication Encoding.
///
/// ```text
/// PAE(type, body) = "DSSEv1" + SP + LEN(type) + SP + type + SP + LEN(body) + SP + body
/// ```
///
/// Where `SP` is ASCII space (0x20) and `LEN(s)` is the decimal byte length.
#[must_use]
pub fn pae(payload_type: &str, payload: &[u8]) -> Vec<u8> {
    let header = format!(
        "DSSEv1 {} {} {} ",
        payload_type.len(),
        payload_type,
        payload.len()
    );
    let mut result = Vec::with_capacity(header.len() + payload.len());
    result.extend_from_slice(header.as_bytes());
    result.extend_from_slice(payload);
    result
}

// ---------------------------------------------------------------------------
// In-toto statement construction helpers
// ---------------------------------------------------------------------------

/// Create a new in-toto statement with a custom predicate type.
///
/// Generic constructor used by both instruction file and trust policy
/// attestation builders.
#[must_use]
pub fn new_statement(
    subject_name: &str,
    sha256_digest: &str,
    predicate: serde_json::Value,
    predicate_type: &str,
) -> InTotoStatement {
    let mut digest = HashMap::new();
    digest.insert("sha256".to_string(), sha256_digest.to_string());

    InTotoStatement {
        statement_type: IN_TOTO_STATEMENT_TYPE.to_string(),
        subject: vec![InTotoSubject {
            name: subject_name.to_string(),
            digest,
        }],
        predicate_type: predicate_type.to_string(),
        predicate,
    }
}

/// Create a new in-toto statement for a nono instruction file attestation.
///
/// This is used during signing to build the statement that goes into the
/// DSSE envelope payload.
#[must_use]
pub fn new_instruction_statement(
    filename: &str,
    sha256_digest: &str,
    signer_predicate: serde_json::Value,
) -> InTotoStatement {
    new_statement(
        filename,
        sha256_digest,
        signer_predicate,
        NONO_PREDICATE_TYPE,
    )
}

/// Create a new in-toto statement for a nono trust policy attestation.
///
/// Uses the policy-specific predicate type to distinguish policy bundles
/// from instruction file bundles.
#[must_use]
pub fn new_policy_statement(
    filename: &str,
    sha256_digest: &str,
    signer_predicate: serde_json::Value,
) -> InTotoStatement {
    new_statement(
        filename,
        sha256_digest,
        signer_predicate,
        NONO_POLICY_PREDICATE_TYPE,
    )
}

/// Create a new in-toto statement for a multi-file attestation.
///
/// Each `(name, sha256_hex)` pair becomes a subject in the statement.
/// Used when signing multiple files together (e.g., SKILL.md + lib/script.py).
///
/// # Panics
///
/// Does not panic. Returns a statement with the provided subjects.
#[must_use]
pub fn new_multi_subject_statement(
    subjects: &[(String, String)],
    signer_predicate: serde_json::Value,
) -> InTotoStatement {
    let subject = subjects
        .iter()
        .map(|(name, sha256_hex)| {
            let mut digest = HashMap::new();
            digest.insert("sha256".to_string(), sha256_hex.clone());
            InTotoSubject {
                name: name.clone(),
                digest,
            }
        })
        .collect();

    InTotoStatement {
        statement_type: IN_TOTO_STATEMENT_TYPE.to_string(),
        subject,
        predicate_type: NONO_MULTI_SUBJECT_PREDICATE_TYPE.to_string(),
        predicate: signer_predicate,
    }
}

/// Create a DSSE envelope wrapping an in-toto statement.
///
/// The payload is base64url-encoded. Signatures must be added separately
/// after computing `PAE(payloadType, raw_payload)`.
///
/// # Errors
///
/// Returns `NonoError::TrustVerification` if statement serialization fails.
pub fn new_envelope(statement: &InTotoStatement) -> Result<DsseEnvelope> {
    let payload_json =
        serde_json::to_string(statement).map_err(|e| NonoError::TrustVerification {
            path: String::new(),
            reason: format!("failed to serialize in-toto statement: {e}"),
        })?;
    let payload_b64 = base64url_encode(payload_json.as_bytes());

    Ok(DsseEnvelope {
        payload_type: IN_TOTO_PAYLOAD_TYPE.to_string(),
        payload: payload_b64,
        signatures: Vec::new(),
    })
}

// Base64url helpers delegated to the shared base64 module
use super::base64::{base64url_decode, base64url_encode};

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

/// Extract a required string field from a JSON value.
fn get_str_field(value: &serde_json::Value, field: &str) -> Result<String> {
    value
        .get(field)
        .and_then(|v| v.as_str())
        .map(String::from)
        .ok_or_else(|| NonoError::TrustVerification {
            path: String::new(),
            reason: format!("keyless signer missing '{field}'"),
        })
}

/// Extract the git ref from a Fulcio subject string.
///
/// Format: `repo:org/repo:ref:refs/heads/main` -> `refs/heads/main`
fn extract_ref_from_subject(subject: &str) -> String {
    // Look for ":ref:" separator
    if let Some(idx) = subject.find(":ref:") {
        return subject[idx + 5..].to_string();
    }
    // Fallback: return as-is
    subject.to_string()
}

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

    // -----------------------------------------------------------------------
    // PAE
    // -----------------------------------------------------------------------

    #[test]
    fn pae_spec_test_vector() {
        // From the DSSE spec
        let result = pae("http://example.com/HelloWorld", b"hello world");
        let expected = b"DSSEv1 29 http://example.com/HelloWorld 11 hello world";
        assert_eq!(result, expected.to_vec());
    }

    #[test]
    fn pae_in_toto_type() {
        let payload = b"test payload";
        let result = pae(IN_TOTO_PAYLOAD_TYPE, payload);
        let expected_prefix = format!(
            "DSSEv1 {} {} {} ",
            IN_TOTO_PAYLOAD_TYPE.len(),
            IN_TOTO_PAYLOAD_TYPE,
            payload.len()
        );
        assert!(result.starts_with(expected_prefix.as_bytes()));
        assert!(result.ends_with(payload));
    }

    #[test]
    fn pae_empty_payload() {
        let result = pae("type", b"");
        assert_eq!(result, b"DSSEv1 4 type 0 ".to_vec());
    }

    #[test]
    fn pae_binary_payload() {
        let payload = vec![0x00, 0x01, 0xFF, 0xFE];
        let result = pae("binary", &payload);
        assert!(result.ends_with(&payload));
    }

    // -----------------------------------------------------------------------
    // DsseEnvelope
    // -----------------------------------------------------------------------

    fn sample_statement_json() -> String {
        serde_json::json!({
            "_type": IN_TOTO_STATEMENT_TYPE,
            "subject": [{
                "name": "SKILLS.md",
                "digest": { "sha256": "abcdef1234567890" }
            }],
            "predicateType": NONO_PREDICATE_TYPE,
            "predicate": {
                "version": 1,
                "signer": {
                    "kind": "keyed",
                    "key_id": "nono-keystore:default"
                }
            }
        })
        .to_string()
    }

    fn sample_envelope_json() -> String {
        let payload = base64url_encode(sample_statement_json().as_bytes());
        serde_json::json!({
            "payloadType": IN_TOTO_PAYLOAD_TYPE,
            "payload": payload,
            "signatures": [{
                "keyid": "",
                "sig": base64url_encode(b"fake-signature")
            }]
        })
        .to_string()
    }

    #[test]
    fn envelope_parse_valid() {
        let json = sample_envelope_json();
        let envelope = DsseEnvelope::from_json(&json).unwrap();
        assert_eq!(envelope.payload_type, IN_TOTO_PAYLOAD_TYPE);
        assert_eq!(envelope.signatures.len(), 1);
    }

    #[test]
    fn envelope_parse_invalid_json() {
        let result = DsseEnvelope::from_json("not json");
        assert!(result.is_err());
    }

    #[test]
    fn envelope_parse_empty_payload() {
        let json = serde_json::json!({
            "payloadType": IN_TOTO_PAYLOAD_TYPE,
            "payload": "",
            "signatures": [{"sig": "abc"}]
        })
        .to_string();
        let result = DsseEnvelope::from_json(&json);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("empty payload"));
    }

    #[test]
    fn envelope_parse_no_signatures() {
        let json = serde_json::json!({
            "payloadType": IN_TOTO_PAYLOAD_TYPE,
            "payload": "dGVzdA",
            "signatures": []
        })
        .to_string();
        let result = DsseEnvelope::from_json(&json);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("no signatures"));
    }

    #[test]
    fn envelope_decode_payload() {
        let json = sample_envelope_json();
        let envelope = DsseEnvelope::from_json(&json).unwrap();
        let decoded = envelope.decode_payload().unwrap();
        let decoded_str = std::str::from_utf8(&decoded).unwrap();
        assert!(decoded_str.contains(IN_TOTO_STATEMENT_TYPE));
    }

    #[test]
    fn envelope_extract_statement() {
        let json = sample_envelope_json();
        let envelope = DsseEnvelope::from_json(&json).unwrap();
        let stmt = envelope.extract_statement().unwrap();
        assert_eq!(stmt.statement_type, IN_TOTO_STATEMENT_TYPE);
        assert_eq!(stmt.subject.len(), 1);
        assert_eq!(stmt.subject[0].name, "SKILLS.md");
    }

    #[test]
    fn envelope_extract_statement_wrong_type() {
        let payload = base64url_encode(b"{}");
        let json = serde_json::json!({
            "payloadType": "text/plain",
            "payload": payload,
            "signatures": [{"sig": "abc"}]
        })
        .to_string();
        let envelope = DsseEnvelope::from_json(&json).unwrap();
        let result = envelope.extract_statement();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("unexpected DSSE payloadType"));
    }

    #[test]
    fn envelope_pae_bytes() {
        let json = sample_envelope_json();
        let envelope = DsseEnvelope::from_json(&json).unwrap();
        let pae_result = envelope.pae_bytes().unwrap();
        // Should start with DSSEv1
        assert!(pae_result.starts_with(b"DSSEv1"));
    }

    #[test]
    fn envelope_to_json_roundtrip() {
        let original = sample_envelope_json();
        let envelope = DsseEnvelope::from_json(&original).unwrap();
        let serialized = envelope.to_json().unwrap();
        let reparsed = DsseEnvelope::from_json(&serialized).unwrap();
        assert_eq!(reparsed.payload_type, envelope.payload_type);
        assert_eq!(reparsed.payload, envelope.payload);
    }

    // -----------------------------------------------------------------------
    // DsseSignature
    // -----------------------------------------------------------------------

    #[test]
    fn signature_decode() {
        let sig = DsseSignature {
            keyid: String::new(),
            sig: base64url_encode(b"signature bytes"),
        };
        let decoded = sig.decode_sig().unwrap();
        assert_eq!(decoded, b"signature bytes");
    }

    // -----------------------------------------------------------------------
    // InTotoStatement
    // -----------------------------------------------------------------------

    #[test]
    fn statement_parse_valid() {
        let json = sample_statement_json();
        let stmt = InTotoStatement::from_json(&json).unwrap();
        assert_eq!(stmt.statement_type, IN_TOTO_STATEMENT_TYPE);
        assert_eq!(stmt.predicate_type, NONO_PREDICATE_TYPE);
    }

    #[test]
    fn statement_wrong_type() {
        let json = serde_json::json!({
            "_type": "https://wrong.type/v1",
            "subject": [{ "name": "f", "digest": { "sha256": "abc" } }],
            "predicateType": NONO_PREDICATE_TYPE,
            "predicate": {}
        })
        .to_string();
        let result = InTotoStatement::from_json(&json);
        assert!(result.is_err());
    }

    #[test]
    fn statement_empty_subjects() {
        let json = serde_json::json!({
            "_type": IN_TOTO_STATEMENT_TYPE,
            "subject": [],
            "predicateType": NONO_PREDICATE_TYPE,
            "predicate": {}
        })
        .to_string();
        let result = InTotoStatement::from_json(&json);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("no subjects"));
    }

    #[test]
    fn statement_subject_missing_digest() {
        let json = serde_json::json!({
            "_type": IN_TOTO_STATEMENT_TYPE,
            "subject": [{ "name": "f", "digest": {} }],
            "predicateType": NONO_PREDICATE_TYPE,
            "predicate": {}
        })
        .to_string();
        let result = InTotoStatement::from_json(&json);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("sha256 digest"));
    }

    #[test]
    fn statement_first_subject_accessors() {
        let stmt = InTotoStatement::from_json(&sample_statement_json()).unwrap();
        assert_eq!(stmt.first_subject_name(), Some("SKILLS.md"));
        assert_eq!(stmt.first_subject_digest(), Some("abcdef1234567890"));
    }

    #[test]
    fn statement_extract_keyed_signer() {
        let stmt = InTotoStatement::from_json(&sample_statement_json()).unwrap();
        let identity = stmt.extract_signer().unwrap();
        match identity {
            super::super::types::SignerIdentity::Keyed { key_id } => {
                assert_eq!(key_id, "nono-keystore:default");
            }
            _ => panic!("expected keyed signer"),
        }
    }

    #[test]
    fn statement_extract_keyless_signer() {
        let json = serde_json::json!({
            "_type": IN_TOTO_STATEMENT_TYPE,
            "subject": [{ "name": "SKILLS.md", "digest": { "sha256": "abc" } }],
            "predicateType": NONO_PREDICATE_TYPE,
            "predicate": {
                "version": 1,
                "signer": {
                    "kind": "keyless",
                    "issuer": "https://token.actions.githubusercontent.com",
                    "subject": "repo:org/repo:ref:refs/tags/v1.0.0",
                    "repository": "org/repo",
                    "workflow_ref": ".github/workflows/sign.yml@refs/heads/main"
                }
            }
        })
        .to_string();
        let stmt = InTotoStatement::from_json(&json).unwrap();
        let identity = stmt.extract_signer().unwrap();
        match identity {
            super::super::types::SignerIdentity::Keyless {
                issuer,
                repository,
                workflow,
                git_ref,
            } => {
                assert_eq!(issuer, "https://token.actions.githubusercontent.com");
                assert_eq!(repository, "org/repo");
                assert_eq!(workflow, ".github/workflows/sign.yml");
                assert_eq!(git_ref, "refs/tags/v1.0.0");
            }
            _ => panic!("expected keyless signer"),
        }
    }

    #[test]
    fn statement_extract_signer_unknown_kind() {
        let json = serde_json::json!({
            "_type": IN_TOTO_STATEMENT_TYPE,
            "subject": [{ "name": "f", "digest": { "sha256": "abc" } }],
            "predicateType": NONO_PREDICATE_TYPE,
            "predicate": {
                "signer": { "kind": "unknown" }
            }
        })
        .to_string();
        let stmt = InTotoStatement::from_json(&json).unwrap();
        let result = stmt.extract_signer();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("unknown signer kind"));
    }

    #[test]
    fn statement_extract_signer_missing() {
        let json = serde_json::json!({
            "_type": IN_TOTO_STATEMENT_TYPE,
            "subject": [{ "name": "f", "digest": { "sha256": "abc" } }],
            "predicateType": NONO_PREDICATE_TYPE,
            "predicate": {}
        })
        .to_string();
        let stmt = InTotoStatement::from_json(&json).unwrap();
        let result = stmt.extract_signer();
        assert!(result.is_err());
    }

    // -----------------------------------------------------------------------
    // Construction helpers
    // -----------------------------------------------------------------------

    #[test]
    fn new_instruction_statement_structure() {
        let predicate = serde_json::json!({
            "version": 1,
            "signer": { "kind": "keyed", "key_id": "test" }
        });
        let stmt = new_instruction_statement("SKILLS.md", "abcdef", predicate);
        assert_eq!(stmt.statement_type, IN_TOTO_STATEMENT_TYPE);
        assert_eq!(stmt.predicate_type, NONO_PREDICATE_TYPE);
        assert_eq!(stmt.subject.len(), 1);
        assert_eq!(stmt.subject[0].name, "SKILLS.md");
        assert_eq!(stmt.subject[0].digest["sha256"], "abcdef");
    }

    #[test]
    fn new_envelope_creates_valid_structure() {
        let predicate = serde_json::json!({
            "version": 1,
            "signer": { "kind": "keyed", "key_id": "test" }
        });
        let stmt = new_instruction_statement("SKILLS.md", "abcdef", predicate);
        let envelope = new_envelope(&stmt).unwrap();
        assert_eq!(envelope.payload_type, IN_TOTO_PAYLOAD_TYPE);
        assert!(envelope.signatures.is_empty());
        // Verify payload roundtrips
        let extracted = envelope.extract_statement().unwrap();
        assert_eq!(extracted.first_subject_name(), Some("SKILLS.md"));
    }

    #[test]
    fn new_policy_statement_uses_policy_predicate_type() {
        let predicate = serde_json::json!({
            "version": 1,
            "signer": { "kind": "keyed", "key_id": "test" }
        });
        let stmt = new_policy_statement("trust-policy.json", "abcdef", predicate);
        assert_eq!(stmt.statement_type, IN_TOTO_STATEMENT_TYPE);
        assert_eq!(stmt.predicate_type, NONO_POLICY_PREDICATE_TYPE);
        assert_eq!(stmt.subject[0].name, "trust-policy.json");
        assert_eq!(stmt.subject[0].digest["sha256"], "abcdef");
    }

    #[test]
    fn new_statement_accepts_custom_predicate_type() {
        let predicate = serde_json::json!({"version": 1});
        let stmt = new_statement("file.txt", "digest", predicate, "custom/type/v1");
        assert_eq!(stmt.predicate_type, "custom/type/v1");
        assert_eq!(stmt.subject[0].name, "file.txt");
    }

    #[test]
    fn instruction_and_policy_predicate_types_differ() {
        assert_ne!(NONO_PREDICATE_TYPE, NONO_POLICY_PREDICATE_TYPE);
    }

    #[test]
    fn multi_subject_predicate_type_is_unique() {
        assert_ne!(NONO_MULTI_SUBJECT_PREDICATE_TYPE, NONO_PREDICATE_TYPE);
        assert_ne!(
            NONO_MULTI_SUBJECT_PREDICATE_TYPE,
            NONO_POLICY_PREDICATE_TYPE
        );
    }

    // -----------------------------------------------------------------------
    // new_multi_subject_statement
    // -----------------------------------------------------------------------

    #[test]
    fn multi_subject_statement_structure() {
        let subjects = vec![
            ("SKILL.md".to_string(), "aaa111".to_string()),
            ("lib/script.py".to_string(), "bbb222".to_string()),
        ];
        let predicate = serde_json::json!({
            "version": 1,
            "signer": { "kind": "keyed", "key_id": "test" }
        });
        let stmt = new_multi_subject_statement(&subjects, predicate);

        assert_eq!(stmt.statement_type, IN_TOTO_STATEMENT_TYPE);
        assert_eq!(stmt.predicate_type, NONO_MULTI_SUBJECT_PREDICATE_TYPE);
        assert_eq!(stmt.subject.len(), 2);
        assert_eq!(stmt.subject[0].name, "SKILL.md");
        assert_eq!(stmt.subject[0].digest["sha256"], "aaa111");
        assert_eq!(stmt.subject[1].name, "lib/script.py");
        assert_eq!(stmt.subject[1].digest["sha256"], "bbb222");
    }

    #[test]
    fn multi_subject_statement_single_subject() {
        let subjects = vec![("only.md".to_string(), "digest123".to_string())];
        let predicate = serde_json::json!({"version": 1});
        let stmt = new_multi_subject_statement(&subjects, predicate);

        assert_eq!(stmt.subject.len(), 1);
        assert_eq!(stmt.subject[0].name, "only.md");
    }

    #[test]
    fn multi_subject_statement_roundtrips_through_envelope() {
        let subjects = vec![
            ("a.md".to_string(), "aaa".to_string()),
            ("b.py".to_string(), "bbb".to_string()),
            ("c.json".to_string(), "ccc".to_string()),
        ];
        let predicate = serde_json::json!({
            "version": 1,
            "signer": { "kind": "keyed", "key_id": "test" }
        });
        let stmt = new_multi_subject_statement(&subjects, predicate);
        let envelope = new_envelope(&stmt).unwrap();
        let extracted = envelope.extract_statement().unwrap();

        assert_eq!(extracted.subject.len(), 3);
        assert_eq!(extracted.predicate_type, NONO_MULTI_SUBJECT_PREDICATE_TYPE);
        assert_eq!(extracted.subject[0].name, "a.md");
        assert_eq!(extracted.subject[1].name, "b.py");
        assert_eq!(extracted.subject[2].name, "c.json");
    }

    #[test]
    fn multi_subject_statement_preserves_signer_predicate() {
        let subjects = vec![("f.md".to_string(), "ddd".to_string())];
        let predicate = serde_json::json!({
            "version": 1,
            "signer": { "kind": "keyed", "key_id": "nono-keystore:default" }
        });
        let stmt = new_multi_subject_statement(&subjects, predicate);
        let identity = stmt.extract_signer().unwrap();
        match identity {
            super::super::types::SignerIdentity::Keyed { key_id } => {
                assert_eq!(key_id, "nono-keystore:default");
            }
            _ => panic!("expected keyed signer"),
        }
    }

    // -----------------------------------------------------------------------
    // extract_ref_from_subject
    // -----------------------------------------------------------------------

    #[test]
    fn extract_ref_standard_format() {
        assert_eq!(
            extract_ref_from_subject("repo:org/repo:ref:refs/tags/v1.0.0"),
            "refs/tags/v1.0.0"
        );
    }

    #[test]
    fn extract_ref_heads() {
        assert_eq!(
            extract_ref_from_subject("repo:org/repo:ref:refs/heads/main"),
            "refs/heads/main"
        );
    }

    #[test]
    fn extract_ref_fallback() {
        assert_eq!(
            extract_ref_from_subject("no-ref-separator"),
            "no-ref-separator"
        );
    }
}