forked from github/smimesign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timestamp_test.go
197 lines (182 loc) · 6.15 KB
/
timestamp_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
package cms
import (
"crypto/rsa"
"crypto/x509"
"strings"
"testing"
"time"
"github.com/github/smimesign/fakeca"
"github.com/github/smimesign/ietf-cms/oid"
"github.com/github/smimesign/ietf-cms/protocol"
"github.com/github/smimesign/ietf-cms/timestamp"
)
func TestAddTimestamps(t *testing.T) {
// Good response
tsa.Clear()
sd, _ := NewSignedData([]byte("hi"))
sd.Sign(leaf.Chain(), leaf.PrivateKey)
if err := sd.AddTimestamps("https://google.com"); err != nil {
t.Fatal(err)
}
if _, err := sd.Verify(intermediateOpts); err != nil {
t.Fatal(err)
}
if _, err := getTimestamp(sd.psd.SignerInfos[0], intermediateOpts); err != nil {
t.Fatal(err)
}
// Error status in response
tsa.HookResponse(func(resp timestamp.Response) timestamp.Response {
resp.Status.Status = 1
return resp
})
sd, _ = NewSignedData([]byte("hi"))
sd.Sign(leaf.Chain(), leaf.PrivateKey)
if err := sd.AddTimestamps("https://google.com"); err != nil {
if _, isStatusErr := err.(timestamp.PKIStatusInfo); !isStatusErr {
t.Fatalf("expected timestamp.PKIStatusInfo error, got %v", err)
}
}
// Bad nonce
tsa.HookInfo(func(info timestamp.Info) timestamp.Info {
info.Nonce.SetInt64(123123)
return info
})
sd, _ = NewSignedData([]byte("hi"))
sd.Sign(leaf.Chain(), leaf.PrivateKey)
if err := sd.AddTimestamps("https://google.com"); err == nil || err.Error() != "invalid message imprint" {
t.Fatalf("expected 'invalid message imprint', got %v", err)
}
// Bad message imprint
tsa.HookInfo(func(info timestamp.Info) timestamp.Info {
info.MessageImprint.HashedMessage[0] ^= 0xFF
return info
})
sd, _ = NewSignedData([]byte("hi"))
sd.Sign(leaf.Chain(), leaf.PrivateKey)
if err := sd.AddTimestamps("https://google.com"); err == nil || err.Error() != "invalid message imprint" {
t.Fatalf("expected 'invalid message imprint', got %v", err)
}
}
func TestTimestampsVerifications(t *testing.T) {
getTimestampedSignedData := func() *SignedData {
sd, _ := NewSignedData([]byte("hi"))
sd.Sign(leaf.Chain(), leaf.PrivateKey)
tsReq, _ := tsRequest(sd.psd.SignerInfos[0])
tsResp, _ := tsa.Do(tsReq)
tsAttr, _ := protocol.NewAttribute(oid.AttributeTimeStampToken, tsResp.TimeStampToken)
sd.psd.SignerInfos[0].UnsignedAttrs = append(sd.psd.SignerInfos[0].UnsignedAttrs, tsAttr)
return sd
}
// Good timestamp
tsa.Clear()
sd := getTimestampedSignedData()
if _, err := getTimestamp(sd.psd.SignerInfos[0], intermediateOpts); err != nil {
t.Fatal(err)
}
if _, err := sd.Verify(intermediateOpts); err != nil {
t.Fatal(err)
}
// Timestamped maybe before not-before
//
// Not-Before Not-After
// |--------------------------------|
// |--------|
// sig-min sig-max
tsa.HookInfo(func(info timestamp.Info) timestamp.Info {
info.Accuracy.Seconds = 30
info.GenTime = leaf.Certificate.NotBefore
return info
})
sd = getTimestampedSignedData()
if _, err := getTimestamp(sd.psd.SignerInfos[0], intermediateOpts); err != nil {
t.Fatal(err)
}
if _, err := sd.Verify(intermediateOpts); err == nil || !strings.HasPrefix(err.Error(), "x509: certificate has expired") {
t.Fatalf("expected expired error, got %v", err)
}
// Timestamped after not-before
//
// Not-Before Not-After
// |--------------------------------|
// |--------|
// sig-min sig-max
tsa.HookInfo(func(info timestamp.Info) timestamp.Info {
info.Accuracy.Seconds = 30
info.GenTime = leaf.Certificate.NotBefore.Add(31 * time.Second)
return info
})
sd = getTimestampedSignedData()
if _, err := getTimestamp(sd.psd.SignerInfos[0], intermediateOpts); err != nil {
t.Fatal(err)
}
if _, err := sd.Verify(intermediateOpts); err != nil {
t.Fatal(err)
}
// Timestamped maybe after not-after
//
// Not-Before Not-After
// |--------------------------------|
// |--------|
// sig-min sig-max
tsa.HookInfo(func(info timestamp.Info) timestamp.Info {
info.Accuracy.Seconds = 30
info.GenTime = leaf.Certificate.NotAfter
return info
})
sd = getTimestampedSignedData()
if _, err := getTimestamp(sd.psd.SignerInfos[0], intermediateOpts); err != nil {
t.Fatal(err)
}
if _, err := sd.Verify(intermediateOpts); err == nil || !strings.HasPrefix(err.Error(), "x509: certificate has expired") {
t.Fatalf("expected expired error, got %v", err)
}
// Timestamped before not-after
//
// Not-Before Not-After
// |--------------------------------|
// |--------|
// sig-min sig-max
tsa.HookInfo(func(info timestamp.Info) timestamp.Info {
info.Accuracy.Seconds = 30
info.GenTime = leaf.Certificate.NotAfter.Add(-31 * time.Second)
return info
})
sd = getTimestampedSignedData()
if _, err := getTimestamp(sd.psd.SignerInfos[0], intermediateOpts); err != nil {
t.Fatal(err)
}
if _, err := sd.Verify(intermediateOpts); err != nil {
t.Fatal(err)
}
// Bad message imprint
tsa.HookInfo(func(info timestamp.Info) timestamp.Info {
info.MessageImprint.HashedMessage[0] ^= 0xFF
return info
})
sd = getTimestampedSignedData()
if _, err := getTimestamp(sd.psd.SignerInfos[0], intermediateOpts); err == nil || err.Error() != "invalid message imprint" {
t.Fatalf("expected 'invalid message imprint', got %v", err)
}
// Untrusted signature
tsa.HookToken(func(tst *protocol.SignedData) *protocol.SignedData {
badIdent := fakeca.New()
tst.SignerInfos = nil
tst.AddSignerInfo(badIdent.Chain(), badIdent.PrivateKey)
return tst
})
sd = getTimestampedSignedData()
if _, err := getTimestamp(sd.psd.SignerInfos[0], intermediateOpts); err == nil {
t.Fatal("expected error")
} else if _, ok := err.(x509.UnknownAuthorityError); !ok {
t.Fatalf("expected x509.UnknownAuthorityError, got %v", err)
}
// Bad signature
tsa.HookToken(func(tst *protocol.SignedData) *protocol.SignedData {
tst.SignerInfos[0].Signature[0] ^= 0xFF
return tst
})
sd = getTimestampedSignedData()
if _, err := getTimestamp(sd.psd.SignerInfos[0], intermediateOpts); err != rsa.ErrVerification {
t.Fatalf("expected %v, got %v", rsa.ErrVerification, err)
}
}