19
19
import static org .junit .jupiter .api .Assertions .assertEquals ;
20
20
import static org .junit .jupiter .api .Assertions .assertNotNull ;
21
21
import static org .junit .jupiter .api .Assertions .assertThrows ;
22
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
22
23
23
24
import com .google .common .io .Resources ;
24
25
import dev .sigstore .json .ProtoJson ;
34
35
35
36
public class TimestampVerifierTest {
36
37
private static SigstoreTrustedRoot trustedRoot ;
38
+ private static SigstoreTrustedRoot trustedRootWithOneTsa ;
37
39
private static SigstoreTrustedRoot trustedRootWithOutdatedTsa ;
38
- private static SigstoreTrustedRoot trustedRootWithMultipleTsas ;
40
+ private static byte [] artifact ;
39
41
private static byte [] trustedTsRespBytesWithEmbeddedCerts ;
40
42
private static byte [] trustedTsRespBytesWithoutEmbeddedCerts ;
41
43
private static byte [] invalidTsRespBytes ;
42
44
private static byte [] untrustedTsRespBytes ;
43
45
44
46
@ BeforeAll
45
47
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
+
47
50
try (var is =
48
51
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" )
50
53
.openStream ()) {
51
54
trustedTsRespBytesWithEmbeddedCerts = is .readAllBytes ();
52
55
}
53
56
54
57
// Response from Sigstore TSA (in trusted root) without embedded certs
55
58
try (var is =
56
59
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" )
58
61
.openStream ()) {
59
62
if (is == null ) {
60
63
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" );
62
65
}
63
66
trustedTsRespBytesWithoutEmbeddedCerts = is .readAllBytes ();
64
67
}
@@ -90,7 +93,7 @@ public static void loadResources() throws Exception {
90
93
public static void initTrustRoot () throws Exception {
91
94
var json =
92
95
Resources .toString (
93
- Resources .getResource ("dev/sigstore/trustroot/trusted_root .json" ),
96
+ Resources .getResource ("dev/sigstore/trustroot/staging_trusted_root .json" ),
94
97
StandardCharsets .UTF_8 );
95
98
var builder = TrustedRoot .newBuilder ();
96
99
ProtoJson .parser ().merge (json , builder );
@@ -99,69 +102,73 @@ public static void initTrustRoot() throws Exception {
99
102
100
103
json =
101
104
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" ),
103
106
StandardCharsets .UTF_8 );
104
107
builder = TrustedRoot .newBuilder ();
105
108
ProtoJson .parser ().merge (json , builder );
106
109
107
- trustedRootWithOutdatedTsa = SigstoreTrustedRoot .from (builder .build ());
110
+ trustedRootWithOneTsa = SigstoreTrustedRoot .from (builder .build ());
111
+ trustedRootWithOneTsa = SigstoreTrustedRoot .from (builder .build ());
108
112
109
113
json =
110
114
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" ),
112
117
StandardCharsets .UTF_8 );
113
118
builder = TrustedRoot .newBuilder ();
114
119
ProtoJson .parser ().merge (json , builder );
115
120
116
- trustedRootWithMultipleTsas = SigstoreTrustedRoot .from (builder .build ());
121
+ trustedRootWithOutdatedTsa = SigstoreTrustedRoot .from (builder .build ());
122
+ trustedRootWithOutdatedTsa = SigstoreTrustedRoot .from (builder .build ());
117
123
}
118
124
119
125
@ Test
120
- public void verify_success_validResponseWithEmbeddedCerts () throws Exception {
126
+ public void verify_success_validResponseWithEmbeddedCerts_multipleTsas () throws Exception {
121
127
var tsResp =
122
128
ImmutableTimestampResponse .builder ().encoded (trustedTsRespBytesWithEmbeddedCerts ).build ();
123
129
var verifier = TimestampVerifier .newTimestampVerifier (trustedRoot );
124
130
125
- assertDoesNotThrow (() -> verifier .verify (tsResp ));
131
+ assertDoesNotThrow (() -> verifier .verify (tsResp , artifact ));
126
132
}
127
133
128
134
@ Test
129
- public void verify_success_validResponseWithoutEmbeddedCerts () throws Exception {
135
+ public void verify_success_validResponseWithoutEmbeddedCerts_multipleTsas () throws Exception {
130
136
var tsResp =
131
137
ImmutableTimestampResponse .builder ()
132
138
.encoded (trustedTsRespBytesWithoutEmbeddedCerts )
133
139
.build ();
134
140
var verifier = TimestampVerifier .newTimestampVerifier (trustedRoot );
135
141
136
- assertDoesNotThrow (() -> verifier .verify (tsResp ));
142
+ assertDoesNotThrow (() -> verifier .verify (tsResp , artifact ));
137
143
}
138
144
139
145
@ Test
140
- public void verify_success_validResponseWithEmbeddedCerts_multipleTsas () throws Exception {
146
+ public void verify_success_validResponseWithEmbeddedCerts_oneTsa () throws Exception {
141
147
var tsResp =
142
148
ImmutableTimestampResponse .builder ().encoded (trustedTsRespBytesWithEmbeddedCerts ).build ();
143
- var verifier = TimestampVerifier .newTimestampVerifier (trustedRootWithMultipleTsas );
149
+ var verifier = TimestampVerifier .newTimestampVerifier (trustedRootWithOneTsa );
144
150
145
- assertDoesNotThrow (() -> verifier .verify (tsResp ));
151
+ assertDoesNotThrow (() -> verifier .verify (tsResp , artifact ));
146
152
}
147
153
148
154
@ Test
149
- public void verify_success_validResponseWithoutEmbeddedCerts_multipleTsas () throws Exception {
155
+ public void verify_success_validResponseWithoutEmbeddedCerts_oneTsa () throws Exception {
150
156
var tsResp =
151
157
ImmutableTimestampResponse .builder ()
152
158
.encoded (trustedTsRespBytesWithoutEmbeddedCerts )
153
159
.build ();
154
- var verifier = TimestampVerifier .newTimestampVerifier (trustedRootWithMultipleTsas );
160
+ var verifier = TimestampVerifier .newTimestampVerifier (trustedRootWithOneTsa );
155
161
156
- assertDoesNotThrow (() -> verifier .verify (tsResp ));
162
+ assertDoesNotThrow (() -> verifier .verify (tsResp , artifact ));
157
163
}
158
164
159
165
@ Test
160
166
public void verify_failure_invalidResponse () throws Exception {
161
167
var tsResp = ImmutableTimestampResponse .builder ().encoded (invalidTsRespBytes ).build ();
162
168
var verifier = TimestampVerifier .newTimestampVerifier (trustedRoot );
163
169
164
- var tsve = assertThrows (TimestampVerificationException .class , () -> verifier .verify (tsResp ));
170
+ var tsve =
171
+ assertThrows (TimestampVerificationException .class , () -> verifier .verify (tsResp , artifact ));
165
172
assertEquals ("Failed to parse TimeStampResponse" , tsve .getMessage ());
166
173
}
167
174
@@ -172,10 +179,13 @@ public void verify_failure_untrustedTsa() throws Exception {
172
179
173
180
var verifier = TimestampVerifier .newTimestampVerifier (trustedRoot );
174
181
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." ));
179
189
}
180
190
181
191
@ Test
@@ -186,9 +196,10 @@ public void verify_failure_outdatedTsa() throws Exception {
186
196
187
197
var verifier = TimestampVerifier .newTimestampVerifier (trustedRootWithOutdatedTsa );
188
198
189
- var tsve = assertThrows (TimestampVerificationException .class , () -> verifier .verify (tsResp ));
199
+ var tsve =
200
+ assertThrows (TimestampVerificationException .class , () -> verifier .verify (tsResp , artifact ));
190
201
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.)" ,
192
203
tsve .getMessage ());
193
204
}
194
205
@@ -205,7 +216,8 @@ public void verify_failure_tsLacksToken() throws Exception {
205
216
var tsResp = ImmutableTimestampResponse .builder ().encoded (failResponseBytes ).build ();
206
217
var verifier = TimestampVerifier .newTimestampVerifier (trustedRoot );
207
218
208
- var tsve = assertThrows (TimestampVerificationException .class , () -> verifier .verify (tsResp ));
219
+ var tsve =
220
+ assertThrows (TimestampVerificationException .class , () -> verifier .verify (tsResp , artifact ));
209
221
assertEquals ("No TimeStampToken found in response" , tsve .getMessage ());
210
222
}
211
223
}
0 commit comments