1919import static org .junit .jupiter .api .Assertions .assertEquals ;
2020import static org .junit .jupiter .api .Assertions .assertNotNull ;
2121import static org .junit .jupiter .api .Assertions .assertThrows ;
22+ import static org .junit .jupiter .api .Assertions .assertTrue ;
2223
2324import com .google .common .io .Resources ;
2425import dev .sigstore .json .ProtoJson ;
3435
3536public class TimestampVerifierTest {
3637 private static SigstoreTrustedRoot trustedRoot ;
38+ private static SigstoreTrustedRoot trustedRootWithOneTsa ;
3739 private static SigstoreTrustedRoot trustedRootWithOutdatedTsa ;
38- private static SigstoreTrustedRoot trustedRootWithMultipleTsas ;
40+ private static byte [] artifact ;
3941 private static byte [] trustedTsRespBytesWithEmbeddedCerts ;
4042 private static byte [] trustedTsRespBytesWithoutEmbeddedCerts ;
4143 private static byte [] invalidTsRespBytes ;
4244 private static byte [] untrustedTsRespBytes ;
4345
4446 @ BeforeAll
4547 public static void loadResources () throws Exception {
46- // Response from Sigstore TSA (in trusted root) with embedded certs
48+ artifact = "test\n " .getBytes (StandardCharsets .UTF_8 );
49+
4750 try (var is =
4851 Resources .getResource (
49- "dev/sigstore/samples/timestamp-response/valid/sigstore_tsa_response_with_embedded_certs .tsr" )
52+ "dev/sigstore/samples/timestamp-response/valid/sigstage_tsa_response_with_embedded_certs .tsr" )
5053 .openStream ()) {
5154 trustedTsRespBytesWithEmbeddedCerts = is .readAllBytes ();
5255 }
5356
5457 // Response from Sigstore TSA (in trusted root) without embedded certs
5558 try (var is =
5659 Resources .getResource (
57- "dev/sigstore/samples/timestamp-response/valid/sigstore_tsa_response_without_embedded_certs .tsr" )
60+ "dev/sigstore/samples/timestamp-response/valid/sigstage_tsa_response_without_embedded_certs .tsr" )
5861 .openStream ()) {
5962 if (is == null ) {
6063 throw new IOException (
61- "dev/sigstore/samples/timestamp-response/valid/sigstore_tsa_response_without_embedded_certs .tsr" );
64+ "dev/sigstore/samples/timestamp-response/valid/sigstage_tsa_response_without_embedded_certs .tsr" );
6265 }
6366 trustedTsRespBytesWithoutEmbeddedCerts = is .readAllBytes ();
6467 }
@@ -90,7 +93,7 @@ public static void loadResources() throws Exception {
9093 public static void initTrustRoot () throws Exception {
9194 var json =
9295 Resources .toString (
93- Resources .getResource ("dev/sigstore/trustroot/trusted_root .json" ),
96+ Resources .getResource ("dev/sigstore/trustroot/staging_trusted_root .json" ),
9497 StandardCharsets .UTF_8 );
9598 var builder = TrustedRoot .newBuilder ();
9699 ProtoJson .parser ().merge (json , builder );
@@ -99,69 +102,73 @@ public static void initTrustRoot() throws Exception {
99102
100103 json =
101104 Resources .toString (
102- Resources .getResource ("dev/sigstore/trustroot/trusted_root_with_outdated_tsa .json" ),
105+ Resources .getResource ("dev/sigstore/trustroot/staging_trusted_root_with_one_tsa .json" ),
103106 StandardCharsets .UTF_8 );
104107 builder = TrustedRoot .newBuilder ();
105108 ProtoJson .parser ().merge (json , builder );
106109
107- trustedRootWithOutdatedTsa = SigstoreTrustedRoot .from (builder .build ());
110+ trustedRootWithOneTsa = SigstoreTrustedRoot .from (builder .build ());
111+ trustedRootWithOneTsa = SigstoreTrustedRoot .from (builder .build ());
108112
109113 json =
110114 Resources .toString (
111- Resources .getResource ("dev/sigstore/trustroot/trusted_root_with_multiple_tsas.json" ),
115+ Resources .getResource (
116+ "dev/sigstore/trustroot/staging_trusted_root_with_outdated_tsa.json" ),
112117 StandardCharsets .UTF_8 );
113118 builder = TrustedRoot .newBuilder ();
114119 ProtoJson .parser ().merge (json , builder );
115120
116- trustedRootWithMultipleTsas = SigstoreTrustedRoot .from (builder .build ());
121+ trustedRootWithOutdatedTsa = SigstoreTrustedRoot .from (builder .build ());
122+ trustedRootWithOutdatedTsa = SigstoreTrustedRoot .from (builder .build ());
117123 }
118124
119125 @ Test
120- public void verify_success_validResponseWithEmbeddedCerts () throws Exception {
126+ public void verify_success_validResponseWithEmbeddedCerts_multipleTsas () throws Exception {
121127 var tsResp =
122128 ImmutableTimestampResponse .builder ().encoded (trustedTsRespBytesWithEmbeddedCerts ).build ();
123129 var verifier = TimestampVerifier .newTimestampVerifier (trustedRoot );
124130
125- assertDoesNotThrow (() -> verifier .verify (tsResp ));
131+ assertDoesNotThrow (() -> verifier .verify (tsResp , artifact ));
126132 }
127133
128134 @ Test
129- public void verify_success_validResponseWithoutEmbeddedCerts () throws Exception {
135+ public void verify_success_validResponseWithoutEmbeddedCerts_multipleTsas () throws Exception {
130136 var tsResp =
131137 ImmutableTimestampResponse .builder ()
132138 .encoded (trustedTsRespBytesWithoutEmbeddedCerts )
133139 .build ();
134140 var verifier = TimestampVerifier .newTimestampVerifier (trustedRoot );
135141
136- assertDoesNotThrow (() -> verifier .verify (tsResp ));
142+ assertDoesNotThrow (() -> verifier .verify (tsResp , artifact ));
137143 }
138144
139145 @ Test
140- public void verify_success_validResponseWithEmbeddedCerts_multipleTsas () throws Exception {
146+ public void verify_success_validResponseWithEmbeddedCerts_oneTsa () throws Exception {
141147 var tsResp =
142148 ImmutableTimestampResponse .builder ().encoded (trustedTsRespBytesWithEmbeddedCerts ).build ();
143- var verifier = TimestampVerifier .newTimestampVerifier (trustedRootWithMultipleTsas );
149+ var verifier = TimestampVerifier .newTimestampVerifier (trustedRootWithOneTsa );
144150
145- assertDoesNotThrow (() -> verifier .verify (tsResp ));
151+ assertDoesNotThrow (() -> verifier .verify (tsResp , artifact ));
146152 }
147153
148154 @ Test
149- public void verify_success_validResponseWithoutEmbeddedCerts_multipleTsas () throws Exception {
155+ public void verify_success_validResponseWithoutEmbeddedCerts_oneTsa () throws Exception {
150156 var tsResp =
151157 ImmutableTimestampResponse .builder ()
152158 .encoded (trustedTsRespBytesWithoutEmbeddedCerts )
153159 .build ();
154- var verifier = TimestampVerifier .newTimestampVerifier (trustedRootWithMultipleTsas );
160+ var verifier = TimestampVerifier .newTimestampVerifier (trustedRootWithOneTsa );
155161
156- assertDoesNotThrow (() -> verifier .verify (tsResp ));
162+ assertDoesNotThrow (() -> verifier .verify (tsResp , artifact ));
157163 }
158164
159165 @ Test
160166 public void verify_failure_invalidResponse () throws Exception {
161167 var tsResp = ImmutableTimestampResponse .builder ().encoded (invalidTsRespBytes ).build ();
162168 var verifier = TimestampVerifier .newTimestampVerifier (trustedRoot );
163169
164- var tsve = assertThrows (TimestampVerificationException .class , () -> verifier .verify (tsResp ));
170+ var tsve =
171+ assertThrows (TimestampVerificationException .class , () -> verifier .verify (tsResp , artifact ));
165172 assertEquals ("Failed to parse TimeStampResponse" , tsve .getMessage ());
166173 }
167174
@@ -172,10 +179,13 @@ public void verify_failure_untrustedTsa() throws Exception {
172179
173180 var verifier = TimestampVerifier .newTimestampVerifier (trustedRoot );
174181
175- var tsve = assertThrows (TimestampVerificationException .class , () -> verifier .verify (tsResp ));
176- assertEquals (
177- "Certificates in token were not verifiable against TSAs\n https://timestamp.sigstore.dev (Embedded leaf certificate does not match this trusted TSA's leaf.)" ,
178- tsve .getMessage ());
182+ var tsve =
183+ assertThrows (TimestampVerificationException .class , () -> verifier .verify (tsResp , artifact ));
184+ assertTrue (
185+ tsve .getMessage ().startsWith ("Certificates in token were not verifiable against TSAs" ));
186+ assertTrue (
187+ tsve .getMessage ()
188+ .contains ("Embedded leaf certificate does not match this trusted TSA's leaf." ));
179189 }
180190
181191 @ Test
@@ -186,9 +196,10 @@ public void verify_failure_outdatedTsa() throws Exception {
186196
187197 var verifier = TimestampVerifier .newTimestampVerifier (trustedRootWithOutdatedTsa );
188198
189- var tsve = assertThrows (TimestampVerificationException .class , () -> verifier .verify (tsResp ));
199+ var tsve =
200+ assertThrows (TimestampVerificationException .class , () -> verifier .verify (tsResp , artifact ));
190201 assertEquals (
191- "Certificate was not verifiable against TSAs\n https://timestamp.sigstore .dev (Timestamp generation time is not within TSA's validity period.)" ,
202+ "Certificate was not verifiable against TSAs\n https://timestamp.sigstage .dev/api/v1/timestamp (Timestamp generation time is not within TSA's validity period.)" ,
192203 tsve .getMessage ());
193204 }
194205
@@ -205,7 +216,8 @@ public void verify_failure_tsLacksToken() throws Exception {
205216 var tsResp = ImmutableTimestampResponse .builder ().encoded (failResponseBytes ).build ();
206217 var verifier = TimestampVerifier .newTimestampVerifier (trustedRoot );
207218
208- var tsve = assertThrows (TimestampVerificationException .class , () -> verifier .verify (tsResp ));
219+ var tsve =
220+ assertThrows (TimestampVerificationException .class , () -> verifier .verify (tsResp , artifact ));
209221 assertEquals ("No TimeStampToken found in response" , tsve .getMessage ());
210222 }
211223}
0 commit comments