-
Notifications
You must be signed in to change notification settings - Fork 4
/
response_test.go
406 lines (378 loc) · 12.3 KB
/
response_test.go
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
// Copyright The Notary Project Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tspclient
import (
"context"
"crypto"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/hex"
"errors"
"math/big"
"os"
"testing"
"time"
"github.com/notaryproject/tspclient-go/internal/oid"
"github.com/notaryproject/tspclient-go/pki"
)
func TestResponseMarshalBinary(t *testing.T) {
var r *Response
_, err := r.MarshalBinary()
if err == nil || err.Error() != "nil response" {
t.Fatalf("expected error nil response, but got %v", err)
}
testResponse := Response{
Status: pki.StatusInfo{
Status: pki.StatusGranted,
},
}
_, err = (&testResponse).MarshalBinary()
if err != nil {
t.Fatal(err)
}
}
func TestResponseUnmarshalBinary(t *testing.T) {
var r *Response
expectedErrMsg := "asn1: Unmarshal recipient value is nil *tspclient.Response"
err := r.UnmarshalBinary([]byte("test"))
if err == nil || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}
}
func TestValidateStatus(t *testing.T) {
badResponse := Response{
Status: pki.StatusInfo{
Status: pki.StatusRejection,
},
}
expectedErrMsg := "invalid timestamping response: invalid response with status code 2: rejected"
err := (&badResponse).validateStatus()
if err == nil || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}
badResponse = Response{
Status: pki.StatusInfo{
Status: pki.StatusRejection,
FailInfo: asn1.BitString{
Bytes: []byte{0x80},
BitLength: 1,
},
},
}
expectedErrMsg = "invalid timestamping response: invalid response with status code 2: rejected. Failure info: unrecognized or unsupported Algorithm Identifier"
err = (&badResponse).validateStatus()
if err == nil || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}
validResponse := Response{
Status: pki.StatusInfo{
Status: pki.StatusGranted,
},
}
err = (&validResponse).validateStatus()
if err != nil {
t.Fatal(err)
}
}
func TestSignedToken(t *testing.T) {
badResponse := Response{
Status: pki.StatusInfo{
Status: pki.StatusRejection,
},
}
expectedErrMsg := "invalid timestamping response: invalid response with status code 2: rejected"
_, err := (&badResponse).SignedToken()
if err == nil || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}
validResponse := Response{
Status: pki.StatusInfo{
Status: pki.StatusGranted,
},
}
expectedErrMsg = "cms: syntax error: invalid signed data: failed to convert from BER to DER: asn1: syntax error: BER-encoded ASN.1 data structures is empty"
_, err = (&validResponse).SignedToken()
if err == nil || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}
}
func TestValidateResponse(t *testing.T) {
var invalidResponse *InvalidResponseError
var req *Request
var resp *Response
expectedErrMsg := "invalid timestamping response: missing corresponding request"
err := resp.Validate(req)
if err == nil || !errors.As(err, &invalidResponse) || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}
req = &Request{
Version: 1,
}
expectedErrMsg = "invalid timestamping response: response cannot be nil"
err = resp.Validate(req)
if err == nil || !errors.As(err, &invalidResponse) || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}
resp = &Response{
Status: pki.StatusInfo{
Status: pki.StatusRejection,
},
}
expectedErrMsg = "invalid timestamping response: invalid response with status code 2: rejected"
err = resp.Validate(req)
if err == nil || !errors.As(err, &invalidResponse) || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}
resp = &Response{
Status: pki.StatusInfo{
Status: pki.StatusGranted,
},
}
expectedErrMsg = "invalid timestamping response: cms: syntax error: invalid signed data: failed to convert from BER to DER: asn1: syntax error: BER-encoded ASN.1 data structures is empty"
err = resp.Validate(req)
if err == nil || !errors.As(err, &invalidResponse) || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}
token, err := os.ReadFile("testdata/TimeStampTokenWithInvalidTSTInfo.p7s")
if err != nil {
t.Fatal("failed to read timestamp token from file:", err)
}
resp = &Response{
Status: pki.StatusInfo{
Status: pki.StatusGranted,
},
TimestampToken: asn1.RawValue{
FullBytes: token,
},
}
expectedErrMsg = "invalid timestamping response: cannot unmarshal TSTInfo from timestamp token: asn1: structure error: tags don't match (23 vs {class:0 tag:16 length:3 isCompound:true}) {optional:false explicit:false application:false private:false defaultValue:<nil> tag:<nil> stringType:0 timeType:24 set:false omitEmpty:false} Time @89"
err = resp.Validate(req)
if err == nil || !errors.As(err, &invalidResponse) || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}
req = &Request{}
token, err = os.ReadFile("testdata/TimeStampTokenWithTSTInfoVersion2.p7s")
if err != nil {
t.Fatal("failed to read timestamp token from file:", err)
}
resp = &Response{
Status: pki.StatusInfo{
Status: pki.StatusGranted,
},
TimestampToken: asn1.RawValue{
FullBytes: token,
},
}
expectedErrMsg = "invalid timestamping response: timestamp token info version must be 1, but got 2"
err = resp.Validate(req)
if err == nil || !errors.As(err, &invalidResponse) || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}
req = &Request{
ReqPolicy: asn1.ObjectIdentifier{1},
}
token, err = os.ReadFile("testdata/TimeStampToken.p7s")
if err != nil {
t.Fatal("failed to read timestamp token from file:", err)
}
resp = &Response{
Status: pki.StatusInfo{
Status: pki.StatusGranted,
},
TimestampToken: asn1.RawValue{
FullBytes: token,
},
}
expectedErrMsg = "invalid timestamping response: policy in response 1.3.6.1.4.1.4146.2.3 does not match policy in request 1"
err = resp.Validate(req)
if err == nil || !errors.As(err, &invalidResponse) || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}
req = &Request{}
token, err = os.ReadFile("testdata/TimeStampToken.p7s")
if err != nil {
t.Fatal("failed to read timestamp token from file:", err)
}
resp = &Response{
Status: pki.StatusInfo{
Status: pki.StatusGranted,
},
TimestampToken: asn1.RawValue{
FullBytes: token,
},
}
expectedErrMsg = "invalid timestamping response: message imprint in response {HashAlgorithm:{Algorithm:2.16.840.1.101.3.4.2.1 Parameters:{Class:0 Tag:5 IsCompound:false Bytes:[] FullBytes:[5 0]}} HashedMessage:[131 38 244 112 157 64 29 250 191 167 131 2 251 28 222 160 241 128 72 164 64 64 194 18 189 142 40 218 107 198 81 199]} does not match with request {HashAlgorithm:{Algorithm: Parameters:{Class:0 Tag:0 IsCompound:false Bytes:[] FullBytes:[]}} HashedMessage:[]}"
err = resp.Validate(req)
if err == nil || !errors.As(err, &invalidResponse) || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}
digest, err := hex.DecodeString("8326f4709d401dfabfa78302fb1cdea0f18048a44040c212bd8e28da6bc651c7")
if err != nil {
t.Fatal(err)
}
messageImprint := MessageImprint{
HashAlgorithm: pkix.AlgorithmIdentifier{
Algorithm: oid.SHA256,
Parameters: asn1.RawValue{
Class: 0,
Tag: 5,
FullBytes: []byte{5, 0},
},
},
HashedMessage: digest,
}
req = &Request{
MessageImprint: messageImprint,
Nonce: big.NewInt(63456),
}
token, err = os.ReadFile("testdata/TimeStampToken.p7s")
if err != nil {
t.Fatal("failed to read timestamp token from file:", err)
}
resp = &Response{
Status: pki.StatusInfo{
Status: pki.StatusGranted,
},
TimestampToken: asn1.RawValue{
FullBytes: token,
},
}
expectedErrMsg = "invalid timestamping response: nonce in response <nil> does not match nonce in request 63456"
err = resp.Validate(req)
if err == nil || !errors.As(err, &invalidResponse) || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}
req = &Request{
MessageImprint: messageImprint,
CertReq: true,
}
token, err = os.ReadFile("testdata/TimeStampTokenWithoutCertificate.p7s")
if err != nil {
t.Fatal("failed to read timestamp token from file:", err)
}
resp = &Response{
Status: pki.StatusInfo{
Status: pki.StatusGranted,
},
TimestampToken: asn1.RawValue{
FullBytes: token,
},
}
expectedErrMsg = "invalid timestamping response: certReq is True in request, but did not find any TSA signing certificate in the response"
err = resp.Validate(req)
if err == nil || !errors.As(err, &invalidResponse) || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}
req = &Request{
MessageImprint: messageImprint,
CertReq: false,
}
token, err = os.ReadFile("testdata/TimeStampToken.p7s")
if err != nil {
t.Fatal("failed to read timestamp token from file:", err)
}
resp = &Response{
Status: pki.StatusInfo{
Status: pki.StatusGranted,
},
TimestampToken: asn1.RawValue{
FullBytes: token,
},
}
expectedErrMsg = "invalid timestamping response: certReq is False in request, but certificates field is included in the response"
err = resp.Validate(req)
if err == nil || !errors.As(err, &invalidResponse) || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}
req = &Request{
Version: 1,
MessageImprint: messageImprint,
ReqPolicy: asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 4146, 2, 3},
CertReq: true,
}
token, err = os.ReadFile("testdata/TimeStampToken.p7s")
if err != nil {
t.Fatal("failed to read timestamp token from file:", err)
}
resp = &Response{
Status: pki.StatusInfo{
Status: pki.StatusGranted,
},
TimestampToken: asn1.RawValue{
FullBytes: token,
},
}
err = resp.Validate(req)
if err != nil {
t.Fatalf("expected nil error, but got %v", err)
}
req = &Request{
Version: 1,
MessageImprint: messageImprint,
ReqPolicy: asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 4146, 2, 3},
CertReq: false,
}
token, err = os.ReadFile("testdata/TimeStampTokenWithoutCertificate.p7s")
if err != nil {
t.Fatal("failed to read timestamp token from file:", err)
}
resp = &Response{
Status: pki.StatusInfo{
Status: pki.StatusGranted,
},
TimestampToken: asn1.RawValue{
FullBytes: token,
},
}
err = resp.Validate(req)
if err != nil {
t.Fatalf("expected nil error, but got %v", err)
}
}
func TestTSAWithGenTimeNotUTC(t *testing.T) {
// prepare TSA
loc := time.FixedZone("UTC+8", 8*60*60)
now := time.Date(2021, 9, 18, 11, 54, 34, 0, loc)
tsa, err := newTestTSA(false, true)
if err != nil {
t.Fatalf("NewTSA() error = %v", err)
}
tsa.nowFunc = func() time.Time {
return now
}
tsa.malformedTimeZone = true
// do timestamp
requestOpts := RequestOptions{
Content: []byte("notation"),
HashAlgorithm: crypto.SHA256,
}
req, err := NewRequest(requestOpts)
if err != nil {
t.Fatalf("NewRequest() error = %v", err)
}
ctx := context.Background()
resp, err := tsa.Timestamp(ctx, req)
if err != nil {
t.Fatalf("TSA.Timestamp() error = %v", err)
}
wantStatus := pki.StatusGranted
if got := resp.Status.Status; got != wantStatus {
t.Fatalf("Response.Status = %v, want %v", got, wantStatus)
}
expectedErrMsg := "invalid timestamping response: TSTInfo genTime must be in UTC"
var invalidResponse *InvalidResponseError
err = resp.Validate(req)
if err == nil || !errors.As(err, &invalidResponse) || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}
}