-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtimestamp_test.go
More file actions
516 lines (437 loc) · 15 KB
/
timestamp_test.go
File metadata and controls
516 lines (437 loc) · 15 KB
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
package timestamp
import (
"bytes"
"crypto"
"crypto/rsa"
_ "crypto/sha1"
"crypto/sha256"
_ "crypto/sha256"
_ "crypto/sha512"
"crypto/x509"
_ "embed"
"encoding/asn1"
"encoding/pem"
"fmt"
"math/big"
"strings"
"testing"
"time"
)
var (
// Random data, to create with OpenSSL:
// $ openssl rand -base64 32 > data.txt
// Contents of our random date in tests below "RhT49MYCgJzWssTF+LXtFZbLD4pe94q4uezLWoTLzyM="
//go:embed testdata/hashedMessage
hashedMessage []byte
// Time-Stamp request with nonce, to create with OpenSSL:
// $ openssl ts -query -data data.txt -cert -sha256 -out reqnonoce.tsq
//go:embed testdata/reqNonce
reqNonce []byte
//go:embed testdata/respNonce
respNonce []byte
// Time-Stamp request without nonce, to create with OpenSSL:
// $ openssl ts -query -data data.txt -cert -sha256 -no_nonce -out reqnonoce.tsq
//go:embed testdata/reqNoNonce
reqNoNonce []byte
//go:embed testdata/respNoNonce
respNoNonce []byte
// Time-Stamp request without certificate, to create with OpenSSL:
// $ openssl ts -query -data data.txt -sha256 -out reqnonoce.tsq
//go:embed testdata/reqNoCert
reqNoCert []byte
//go:embed testdata/respNoCert
respNoCert []byte
// TimeStampToken excluding response struct
//go:embed testdata/timeStampToken
timeStampToken []byte
)
type testData struct {
name string
request []byte
response []byte
certificates bool
nonce *big.Int
time time.Time
accuracy time.Duration
hashedMessage []byte
hashAlgorithm crypto.Hash
}
var testCases = []testData{
{
name: "Including nonce",
request: reqNonce,
response: respNonce,
certificates: true,
nonce: big.NewInt(0).SetBytes([]byte{0x9, 0x2e, 0xf1, 0x9f, 0xfb, 0x5d, 0x2a, 0xe8}),
time: time.Date(2017, 4, 19, 6, 29, 53, 0, time.UTC),
accuracy: time.Second,
hashedMessage: hashedMessage,
hashAlgorithm: crypto.SHA256,
},
{
name: "Containing no nonce",
request: reqNoNonce,
response: respNoNonce,
certificates: true,
nonce: nil,
time: time.Date(2017, 4, 19, 6, 28, 13, 0, time.UTC),
accuracy: time.Second,
hashedMessage: hashedMessage,
hashAlgorithm: crypto.SHA256,
},
{
name: "Containing no certificates",
request: reqNoCert,
response: respNoCert,
certificates: false,
nonce: big.NewInt(0).SetBytes([]byte{0xb1, 0xfc, 0x81, 0xde, 0xc9, 0x57, 0x49, 0xd9}),
time: time.Date(2017, 4, 19, 6, 32, 43, 0, time.UTC),
accuracy: time.Second,
hashedMessage: hashedMessage,
hashAlgorithm: crypto.SHA256,
},
}
// Send the timestamp request to our timestamp server and save the response
// $ curl --globoff -s -S -H Content-Type:application/timestamp-query -H Host:${HOST} --data-binary @request-sha256.tsq -o ts-output.tsr ${URL}
func TestParseRequest(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.request == nil {
return
}
req, err := ParseRequest(tc.request)
if err != nil {
t.Errorf("failed to parse request: %s", err.Error())
return
}
if !bytes.Equal(req.HashedMessage, tc.hashedMessage) {
t.Errorf("req.HashedMessage: got %x, want %x", req.HashedMessage, tc.hashedMessage)
}
if (tc.nonce != nil && tc.nonce.CmpAbs(req.Nonce) != 0) || (req.Nonce != nil && tc.nonce == nil) {
t.Errorf("req.Nonce: got %v, want %v", req.Nonce, tc.nonce)
}
if req.HashAlgorithm != tc.hashAlgorithm {
t.Errorf("req.HashAlgorithm: got %v, want %v", req.HashAlgorithm, tc.hashAlgorithm)
}
if req.Certificates != tc.certificates {
t.Errorf("req.Certificates: got %v, want %v", req.Certificates, tc.certificates)
}
})
}
}
func TestParseResponse(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.response == nil {
return
}
resp, err := ParseResponse(tc.response)
if err != nil {
t.Errorf("failed to parse response: %s", err.Error())
return
}
if !bytes.Equal(resp.HashedMessage, tc.hashedMessage) {
t.Errorf("resp.HashedMessage: got %x, want %x", resp.HashedMessage, tc.hashedMessage)
}
if resp.HashAlgorithm != tc.hashAlgorithm {
t.Errorf("resp.HashAlgorithm: got %v, want %v", resp.HashAlgorithm, tc.hashAlgorithm)
}
if !resp.Time.Equal(tc.time) {
t.Errorf("resp.Time: got %v, want %v", resp.Time, tc.time)
}
if resp.Accuracy != tc.accuracy {
t.Errorf("resp.Accuracy: got %v, want %v", resp.Accuracy, tc.accuracy)
}
if tc.certificates && len(resp.Certificates) == 0 {
t.Errorf("resp.Certificates: got %v, want %v", len(resp.Certificates), tc.certificates)
}
/*
if !(resp.Nonce == nil && tc.nonce == nil && resp.Nonce.Cmp(tc.nonce) != 0 {
t.Errorf("resp.Nonce: got %v, want %v", resp.Nonce, tc.nonce)
}
*/
})
}
}
func TestParse(t *testing.T) {
ts, err := Parse(timeStampToken)
if err != nil {
t.Errorf("failed to parse timeStampToken: %s", err.Error())
}
hashedMessage := []byte{0x2d, 0xa8, 0x2c, 0x15, 0x72, 0xff, 0x2, 0x5, 0x24, 0xe0, 0x50, 0x69, 0x21, 0x99, 0x75, 0xc9, 0xba, 0x90, 0x72, 0x3f, 0x1a, 0x4d, 0xd5, 0xb9, 0x72, 0x2a, 0xee, 0x8e, 0xc5, 0x47, 0xa2, 0xff}
if !bytes.Equal(ts.HashedMessage, hashedMessage) {
t.Errorf("ts.HashedMessage: got %x, want %x", ts.HashedMessage, hashedMessage)
}
tsTime := time.Date(2017, 2, 1, 15, 39, 50, 0, time.UTC)
if !ts.Time.Equal(tsTime) {
t.Errorf("ts.Time: got %v, want %v", ts.Time, tsTime)
}
if ts.Accuracy != time.Second {
t.Errorf("ts.Accuracy: got %v, want %v", ts.Accuracy, time.Second)
}
}
func TestMarshalRequest(t *testing.T) {
req, err := ParseRequest(reqNoNonce)
if err != nil {
t.Fatal(err)
}
reqByes, err := req.Marshal()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(reqByes, reqNoNonce) {
t.Error("Marshalled response bytes are not the same as parsed")
}
}
func TestCreateRequest(t *testing.T) {
var testHashes = []crypto.Hash{crypto.SHA1, crypto.SHA256, crypto.SHA384, crypto.SHA512}
nonce := big.NewInt(0)
nonce = nonce.SetBytes([]byte{0x1, 0x2, 0x3})
for _, th := range testHashes {
t.Run(fmt.Sprintf("%d", th), func(t *testing.T) {
msg := "Content to by timestamped"
h := th.New()
_, err := h.Write([]byte(msg))
if err != nil {
t.Fatal(err)
}
hashedMsg := h.Sum(nil)
req, err := CreateRequest(strings.NewReader(msg), &RequestOptions{
Hash: th,
Nonce: nonce,
TSAPolicyOID: asn1.ObjectIdentifier{2, 5, 6, 7},
Certificates: true,
})
if err != nil {
t.Fatal(err)
}
if len(req) == 0 {
t.Error("request contains no bytes")
}
reqCheck, err := ParseRequest(req)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(reqCheck.HashedMessage, hashedMsg) {
t.Errorf("reqCheck.HashedMessage: got %x, want %x", reqCheck.HashedMessage, hashedMsg)
}
if reqCheck.Nonce.Cmp(nonce) != 0 {
t.Errorf("reqCheck.Nonce: got %x, want %x", reqCheck.Nonce, nonce)
}
if reqCheck.Certificates != true {
t.Errorf("reqCheck.Certificates: got %t, want %t", reqCheck.Certificates, true)
}
if !reqCheck.TSAPolicyOID.Equal(asn1.ObjectIdentifier{2, 5, 6, 7}) {
t.Errorf("reqCheck.TSAPolicyOID: got %x, want %x", reqCheck.TSAPolicyOID, asn1.ObjectIdentifier{2, 5, 6, 7})
}
})
}
}
func BenchmarkCreateRequest(b *testing.B) {
reader := strings.NewReader("Content to be time-stamped")
for n := 0; n < b.N; n++ {
_, _ = CreateRequest(reader, nil)
}
}
func BenchmarkParseRequest(b *testing.B) {
for n := 0; n < b.N; n++ {
_, _ = ParseRequest(reqNonce)
}
}
func BenchmarkParseResponse(b *testing.B) {
for n := 0; n < b.N; n++ {
_, _ = ParseResponse(respNonce)
}
}
// ExampleCreateRequest demonstrates how to create a new time-stamping request
// for an io.Reader.
func ExampleCreateRequest() {
_, err := CreateRequest(strings.NewReader("Content to be time-stamped"), nil)
if err != nil {
panic(err)
}
}
// ExampleCreateRequest_customHashingAlgorithm demonstrates how to create a new
// time-stamping request with options
func ExampleCreateRequest_customHashingAlgorithm() {
_, err := CreateRequest(
strings.NewReader("Content to be time-stamped"),
&RequestOptions{
Hash: crypto.SHA512,
})
if err != nil {
panic(err)
}
}
// ExampleParseRequest demonstrates how to parse a raw der time-stamping request
func ExampleParseRequest() {
// CreateRequest returns the request in der bytes
createdRequest, err := CreateRequest(strings.NewReader("Content to be time-stamped"), nil)
if err != nil {
panic(err)
}
// ParseRequest parses a request in der bytes
parsedRequest, err := ParseRequest(createdRequest)
if err != nil {
panic(err)
}
fmt.Printf("%x\n", parsedRequest.HashedMessage)
// Output: 51a3620a3b62ffaff41a434e932223b31bc69e86490c365fa1186033904f1132
}
func TestCreateResponseWithIncludeTSACertificate(t *testing.T) {
tsakey := getTSARSAKey()
tsaCert := getTSACert()
h := sha256.New()
_, err := h.Write([]byte("Hello World"))
if err != nil {
t.Fatal(err)
}
genTime := time.Now().UTC()
nonce := big.NewInt(0)
nonce = nonce.SetBytes([]byte{0x1, 0x2, 0x3})
duration, _ := time.ParseDuration("1s")
timestamp := Timestamp{
HashAlgorithm: crypto.SHA256,
HashedMessage: h.Sum(nil),
Time: genTime,
Nonce: nonce,
Policy: asn1.ObjectIdentifier{2, 4, 5, 6},
Ordering: true,
Accuracy: duration,
Qualified: true,
AddTSACertificate: true,
}
timestampBytes, err := timestamp.CreateResponse(tsaCert, tsakey)
if err != nil {
t.Errorf("unable to generate time stamp response: %s", err.Error())
}
// To verify the reponse using OpenSSL
// openssl ts -reply -in timestamp.tsr -text
// _ = ioutil.WriteFile("timestamp.tsr", timestampBytes, 0644)
timestampRes, err := ParseResponse(timestampBytes)
if err != nil {
t.Errorf("unable to parse time stamp response: %s", err.Error())
}
if timestampRes.HashAlgorithm.HashFunc() != crypto.SHA256 {
t.Errorf("expected hash algorithm is SHA256")
}
if len(timestampRes.HashedMessage) != 32 {
t.Errorf("got %d: expected: %d", len(timestampRes.HashedMessage), 32)
}
if timestampRes.Accuracy != duration {
t.Errorf("got accuracy %s: expected: %s", timestampRes.Accuracy, duration)
}
if !timestampRes.Qualified {
t.Errorf("got %t: expected: %t", timestampRes.Qualified, true)
}
if !timestampRes.AddTSACertificate {
t.Error("TSA certificate must be included in timestamp response")
}
}
func TestCreateResponseWithNoTSACertificate(t *testing.T) {
tsakey := getTSARSAKey()
tsaCert := getTSACert()
h := sha256.New()
_, err := h.Write([]byte("Hello World"))
if err != nil {
t.Fatal(err)
}
genTime := time.Now().UTC()
nonce := big.NewInt(0)
nonce = nonce.SetBytes([]byte{0x1, 0x2, 0x3})
duration, _ := time.ParseDuration("1s")
timestamp := Timestamp{
HashAlgorithm: crypto.SHA256,
HashedMessage: h.Sum(nil),
Time: genTime,
Nonce: nonce,
Policy: asn1.ObjectIdentifier{2, 4, 5, 6},
Ordering: true,
Accuracy: duration,
Qualified: false,
AddTSACertificate: false,
}
timestampBytes, err := timestamp.CreateResponse(tsaCert, tsakey)
if err != nil {
t.Errorf("unable to generate time stamp response: %s", err.Error())
}
timestampRes, err := ParseResponse(timestampBytes)
if err != nil {
t.Errorf("unable to parse time stamp response: %s", err.Error())
}
if timestampRes.HashAlgorithm.HashFunc() != crypto.SHA256 {
t.Errorf("expected hash algorithm is SHA256")
}
if len(timestampRes.HashedMessage) != 32 {
t.Errorf("got %d: expected: %d", len(timestampRes.HashedMessage), 32)
}
if timestampRes.Qualified {
t.Errorf("got %t: expected: %t", timestampRes.Qualified, true)
}
if timestampRes.AddTSACertificate {
t.Error("TSA certificate must not be included in timestamp response")
}
}
func getTSARSAKey() *rsa.PrivateKey {
tsaRSAKeyPEM := `
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEApeencH+4Wo3Ry65t2/FdZkHLyQcizv8Xu+4NTEGF502fPV2y
May4/ZU+GXeVTPhwfJuFj1D8Id6skgZ1DlAz+cpIqQQoaUuVM6M5MMJ6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MPpkHfGBKHSDkhWHKsB39Azan
TZU/nCcG09sv2qH33c+8wcUCgYEAli3TqKNWqUSsZ9WZ43ES8zA8ILAwxpLVILKq
Foddu1VaAyngnPQofiDe6XgnIYq1TqH+4V4kA4dVXV/kbbffMyS8SD19jbK1PbgP
Nu0ISEk7jkro7aarrrPZ/XyiyT56IghNuPsQtE1LtMA07mlYGUD3Q5gxQvMiKcQs
w0FZ8vkCgYA7wuwLs7d9LJ4KqMNmOe0eRvIxp+Y8psxykMd1wz3PjdPz30U03xe2
o40r2ZNTK/OGYPmAOcwma7SjenBQve19eVUaECUVREmbvaJqVzz0uSrfqXrUVIiJ
YyOfhPUI5XhkyUlunO5pSAd0CtRv7NVW1wKDjMbJvgV0MlbVvGraAg==
-----END RSA PRIVATE KEY-----
`
tsaKeyPEMBlock, _ := pem.Decode([]byte(tsaRSAKeyPEM))
pvtKey, _ := x509.ParsePKCS1PrivateKey(tsaKeyPEMBlock.Bytes)
return pvtKey
}
func getTSACert() *x509.Certificate {
tsaCertPEM := `
-----BEGIN CERTIFICATE-----
MIIDmzCCAoOgAwIBAgIUTrgB1p7WpwYXjwGs/uwfKJt4cFcwDQYJKoZIhvcNAQEL
BQAwXTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEWMBQGA1UEAwwNVGVzdCBSU0EgQ2Vy
dDAeFw0yMDAzMDQyMjA4MDVaFw00MDAyMjgyMjA4MDVaMF0xCzAJBgNVBAYTAkFV
MRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRz
IFB0eSBMdGQxFjAUBgNVBAMMDVRlc3QgUlNBIENlcnQwggEiMA0GCSqGSIb3DQEB
AQUAA4IBDwAwggEKAoIBAQCl56dwf7hajdHLrm3b8V1mQcvJByLO/xe77g1MQYXn
TZ89XbIxrLj9lT4Zd5VM+HB8m4WPUPwh3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