@@ -54,12 +54,47 @@ pub enum VerifyResult {
5454 /// The embedded intent and evidence were rehashed and match the
5555 /// seal. False for legacy seals, whose hashes cannot be recomputed.
5656 content_bound : bool ,
57+ /// A report was supplied and its SHA-256 matches `seal.report_hash`.
58+ report_checked : bool ,
5759 signature_verified : bool ,
5860 } ,
5961 /// One or more chain invariants failed.
6062 Failed ( Vec < String > ) ,
6163}
6264
65+ /// Optional checks on top of the chain invariants.
66+ #[ derive( Debug , Clone , Copy , Default ) ]
67+ pub struct VerifyOptions < ' a > {
68+ /// SHA-256 of the report file, as 64 lowercase hex characters (see
69+ /// [`sha256_reader`]). When given, it must equal `seal.report_hash`, so
70+ /// the attestation is checked against the report itself rather than only
71+ /// against the digest stored beside it. A digest rather than the bytes,
72+ /// so a caller can hash a large report as a stream.
73+ pub report_sha256 : Option < & ' a str > ,
74+ /// Reject legacy seals, whose intent and evidence cannot be rehashed.
75+ pub require_bound : bool ,
76+ }
77+
78+ /// SHA-256 of everything `reader` yields, as 64 lowercase hex characters.
79+ ///
80+ /// Hashes through a fixed-size buffer, so memory use does not grow with the
81+ /// input. Use it to fill [`VerifyOptions::report_sha256`]; a byte slice is a
82+ /// reader too.
83+ pub fn sha256_reader < R : std:: io:: Read > ( mut reader : R ) -> std:: io:: Result < String > {
84+ use sha2:: { Digest , Sha256 } ;
85+ let mut hasher = Sha256 :: new ( ) ;
86+ let mut buf = [ 0_u8 ; 8192 ] ;
87+ loop {
88+ match reader. read ( & mut buf) {
89+ Ok ( 0 ) => break ,
90+ Ok ( n) => hasher. update ( & buf[ ..n] ) ,
91+ Err ( e) if e. kind ( ) == std:: io:: ErrorKind :: Interrupted => { }
92+ Err ( e) => return Err ( e) ,
93+ }
94+ }
95+ Ok ( hex:: encode ( hasher. finalize ( ) ) )
96+ }
97+
6398/// Verify an attestation sidecar file.
6499///
65100/// Reads the JSON file at `path`, recomputes the chain hash from the stored
@@ -72,6 +107,14 @@ pub enum VerifyResult {
72107/// Returns `VerifyResult::Ok` when all checks pass, `VerifyResult::Failed`
73108/// with a list of failure reasons otherwise.
74109pub fn verify_attestation_file ( path : & std:: path:: Path ) -> anyhow:: Result < VerifyResult > {
110+ verify_attestation_file_with ( path, & VerifyOptions :: default ( ) )
111+ }
112+
113+ /// [`verify_attestation_file`] with the optional checks in `options`.
114+ pub fn verify_attestation_file_with (
115+ path : & std:: path:: Path ,
116+ options : & VerifyOptions < ' _ > ,
117+ ) -> anyhow:: Result < VerifyResult > {
75118 use std:: io:: Read ;
76119
77120 // Attestation envelopes are JSON. They embed three hashes plus a small
@@ -87,12 +130,20 @@ pub fn verify_attestation_file(path: &std::path::Path) -> anyhow::Result<VerifyR
87130 . read_to_string ( & mut content)
88131 . map_err ( |e| anyhow:: anyhow!( "reading {}: {}" , path. display( ) , e) ) ?;
89132
90- verify_attestation_str ( & content)
133+ verify_attestation_str_with ( & content, options )
91134}
92135
93136/// Verify an attestation envelope held in memory. See
94137/// [`verify_attestation_file`].
95138pub fn verify_attestation_str ( content : & str ) -> anyhow:: Result < VerifyResult > {
139+ verify_attestation_str_with ( content, & VerifyOptions :: default ( ) )
140+ }
141+
142+ /// [`verify_attestation_str`] with the optional checks in `options`.
143+ pub fn verify_attestation_str_with (
144+ content : & str ,
145+ options : & VerifyOptions < ' _ > ,
146+ ) -> anyhow:: Result < VerifyResult > {
96147 let tree: serde_json:: Value = serde_json:: from_str ( content)
97148 . map_err ( |e| anyhow:: anyhow!( "parsing attestation envelope: {}" , e) ) ?;
98149 let envelope: A2mlEnvelope = serde_json:: from_value ( tree. clone ( ) )
@@ -165,6 +216,25 @@ pub fn verify_attestation_str(content: &str) -> anyhow::Result<VerifyResult> {
165216 }
166217 } ;
167218
219+ if options. require_bound && seal. hash_scheme == seal:: HASH_SCHEME_LEGACY {
220+ failures
221+ . push ( "legacy hash scheme: intent and evidence are not bound to the seal" . to_string ( ) ) ;
222+ }
223+
224+ // The report itself, when supplied, must hash to seal.report_hash.
225+ let report_checked = match options. report_sha256 {
226+ Some ( computed) => {
227+ if computed != seal. report_hash {
228+ failures. push ( format ! (
229+ "report_hash mismatch: stored={} computed={}" ,
230+ seal. report_hash, computed
231+ ) ) ;
232+ }
233+ true
234+ }
235+ None => false ,
236+ } ;
237+
168238 // decision_hash in the envelope must match seal.report_hash
169239 if envelope. decision_hash != seal. report_hash {
170240 failures. push ( format ! (
@@ -185,6 +255,7 @@ pub fn verify_attestation_str(content: &str) -> anyhow::Result<VerifyResult> {
185255 issued_at : envelope. issued_at . clone ( ) ,
186256 chain_hash : seal. chain_hash . clone ( ) ,
187257 content_bound,
258+ report_checked,
188259 signature_verified,
189260 } )
190261}
@@ -270,6 +341,11 @@ mod tests {
270341 /// Seal a small scan of a temporary directory and return the sidecar
271342 /// exactly as `assail --attest` writes it.
272343 fn sealed_sidecar ( ) -> String {
344+ sealed_sidecar_and_report ( ) . 0
345+ }
346+
347+ /// The sidecar plus the exact report bytes it seals.
348+ fn sealed_sidecar_and_report ( ) -> ( String , String ) {
273349 let dir = tempfile:: tempdir ( ) . expect ( "tempdir" ) ;
274350 let args = vec ! [ "--verbose" . to_string( ) ] ;
275351 let mut builder = AttestationChainBuilder :: begin ( dir. path ( ) , & args) . expect ( "begin" ) ;
@@ -280,7 +356,8 @@ mod tests {
280356 let report = crate :: json:: to_canonical_string ( & serde_json:: json!( { "weak_points" : [ ] } ) )
281357 . expect ( "report" ) ;
282358 let envelope = builder. seal ( report. as_bytes ( ) , None ) . expect ( "seal" ) ;
283- crate :: json:: to_canonical_string ( & envelope) . expect ( "sidecar" )
359+ let sidecar = crate :: json:: to_canonical_string ( & envelope) . expect ( "sidecar" ) ;
360+ ( sidecar, report)
284361 }
285362
286363 fn edit ( sidecar : & str , change : impl FnOnce ( & mut serde_json:: Value ) ) -> String {
@@ -445,4 +522,90 @@ mod tests {
445522 "{reasons:?}"
446523 ) ;
447524 }
525+
526+ fn verify_with ( sidecar : & str , options : & VerifyOptions < ' _ > ) -> VerifyResult {
527+ verify_attestation_str_with ( sidecar, options) . expect ( "verify" )
528+ }
529+
530+ #[ test]
531+ fn sha256_reader_matches_the_one_shot_hash ( ) {
532+ // FIPS 180-2 "abc" vector, and a stream longer than the read buffer.
533+ assert_eq ! (
534+ sha256_reader( & b"abc" [ ..] ) . unwrap( ) ,
535+ "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
536+ ) ;
537+ let big = vec ! [ 0x5a_u8 ; 64 * 1024 + 7 ] ;
538+ assert_eq ! ( sha256_reader( & big[ ..] ) . unwrap( ) , intent:: sha256_str( & big) ) ;
539+ }
540+
541+ #[ test]
542+ fn the_sealed_report_verifies ( ) {
543+ let ( sidecar, report) = sealed_sidecar_and_report ( ) ;
544+ let options = VerifyOptions {
545+ report_sha256 : Some ( & sha256_reader ( report. as_bytes ( ) ) . expect ( "hash" ) ) ,
546+ ..VerifyOptions :: default ( )
547+ } ;
548+ match verify_with ( & sidecar, & options) {
549+ VerifyResult :: Ok { report_checked, .. } => assert ! ( report_checked) ,
550+ VerifyResult :: Failed ( reasons) => panic ! ( "{reasons:?}" ) ,
551+ }
552+ // Without --report the report is not claimed as checked.
553+ match verify_with ( & sidecar, & VerifyOptions :: default ( ) ) {
554+ VerifyResult :: Ok { report_checked, .. } => assert ! ( !report_checked) ,
555+ VerifyResult :: Failed ( reasons) => panic ! ( "{reasons:?}" ) ,
556+ }
557+ }
558+
559+ #[ test]
560+ fn an_edited_report_is_detected ( ) {
561+ // The seal and decision_hash agree with each other, so only
562+ // rehashing the report itself can catch this.
563+ let ( sidecar, report) = sealed_sidecar_and_report ( ) ;
564+ let tampered = report. replace ( "[]" , "[{}]" ) ;
565+ assert_ne ! ( tampered, report) ;
566+ let options = VerifyOptions {
567+ report_sha256 : Some ( & sha256_reader ( tampered. as_bytes ( ) ) . expect ( "hash" ) ) ,
568+ ..VerifyOptions :: default ( )
569+ } ;
570+ match verify_with ( & sidecar, & options) {
571+ VerifyResult :: Failed ( reasons) => assert ! (
572+ reasons
573+ . iter( )
574+ . any( |r| r. starts_with( "report_hash mismatch" ) ) ,
575+ "{reasons:?}"
576+ ) ,
577+ VerifyResult :: Ok { .. } => panic ! ( "edited report verified" ) ,
578+ }
579+ }
580+
581+ #[ test]
582+ fn require_bound_rejects_legacy_seals_only ( ) {
583+ let options = VerifyOptions {
584+ require_bound : true ,
585+ ..VerifyOptions :: default ( )
586+ } ;
587+ let legacy = edit ( & sealed_sidecar ( ) , |tree| {
588+ strip_scheme ( tree) ;
589+ let seal = & tree[ "attestation" ] [ "seal" ] ;
590+ let digest = |k : & str | seal[ k] . as_str ( ) . expect ( k) . to_string ( ) ;
591+ let chain = seal:: chain_hash (
592+ seal:: HASH_SCHEME_LEGACY ,
593+ & digest ( "intent_hash" ) ,
594+ & digest ( "evidence_hash" ) ,
595+ & digest ( "report_hash" ) ,
596+ ) ;
597+ tree[ "attestation" ] [ "seal" ] [ "chain_hash" ] = serde_json:: json!( chain) ;
598+ } ) ;
599+ match verify_with ( & legacy, & options) {
600+ VerifyResult :: Failed ( reasons) => assert_eq ! (
601+ reasons,
602+ [ "legacy hash scheme: intent and evidence are not bound to the seal" ]
603+ ) ,
604+ VerifyResult :: Ok { .. } => panic ! ( "legacy seal passed --require-bound" ) ,
605+ }
606+ match verify_with ( & sealed_sidecar ( ) , & options) {
607+ VerifyResult :: Ok { content_bound, .. } => assert ! ( content_bound) ,
608+ VerifyResult :: Failed ( reasons) => panic ! ( "{reasons:?}" ) ,
609+ }
610+ }
448611}
0 commit comments