forked from github/smimesign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
166 lines (135 loc) · 3.5 KB
/
main_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
package cms
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/asn1"
"io"
"io/ioutil"
"math/big"
"net/http"
"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"
)
var (
// fake PKI setup
root = fakeca.New(fakeca.IsCA)
otherRoot = fakeca.New(fakeca.IsCA)
intermediateKey, _ = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
intermediate = root.Issue(fakeca.IsCA, fakeca.PrivateKey(intermediateKey))
leaf = intermediate.Issue(
fakeca.NotBefore(time.Now().Add(-time.Hour)),
fakeca.NotAfter(time.Now().Add(time.Hour)),
)
rootOpts = x509.VerifyOptions{Roots: root.ChainPool()}
otherRootOpts = x509.VerifyOptions{Roots: otherRoot.ChainPool()}
intermediateOpts = x509.VerifyOptions{Roots: intermediate.ChainPool()}
// fake timestamp authority setup
tsa = &testTSA{ident: intermediate.Issue()}
thc = &testHTTPClient{tsa}
)
func init() {
timestamp.DefaultHTTPClient = thc
}
type testTSA struct {
ident *fakeca.Identity
sn int64
hookInfo func(timestamp.Info) timestamp.Info
hookToken func(*protocol.SignedData) *protocol.SignedData
hookResponse func(timestamp.Response) timestamp.Response
}
func (tt *testTSA) Clear() {
tt.hookInfo = nil
tt.hookToken = nil
tt.hookResponse = nil
}
func (tt *testTSA) HookInfo(hook func(timestamp.Info) timestamp.Info) {
tt.Clear()
tt.hookInfo = hook
}
func (tt *testTSA) HookToken(hook func(*protocol.SignedData) *protocol.SignedData) {
tt.Clear()
tt.hookToken = hook
}
func (tt *testTSA) HookResponse(hook func(timestamp.Response) timestamp.Response) {
tt.Clear()
tt.hookResponse = hook
}
func (tt *testTSA) nextSN() *big.Int {
defer func() { tt.sn++ }()
return big.NewInt(tt.sn)
}
func (tt *testTSA) Do(req timestamp.Request) (timestamp.Response, error) {
info := timestamp.Info{
Version: 1,
Policy: asn1.ObjectIdentifier{1, 2, 3},
SerialNumber: tt.nextSN(),
GenTime: time.Now(),
MessageImprint: req.MessageImprint,
Nonce: req.Nonce,
}
if tt.hookInfo != nil {
info = tt.hookInfo(info)
}
eciDER, err := asn1.Marshal(info)
if err != nil {
panic(err)
}
eci, err := protocol.NewEncapsulatedContentInfo(oid.ContentTypeTSTInfo, eciDER)
if err != nil {
panic(err)
}
tst, err := protocol.NewSignedData(eci)
if err != nil {
panic(err)
}
if err = tst.AddSignerInfo(tsa.ident.Chain(), tsa.ident.PrivateKey); err != nil {
panic(err)
}
if tt.hookToken != nil {
tt.hookToken(tst)
}
ci, err := tst.ContentInfo()
if err != nil {
panic(err)
}
resp := timestamp.Response{
Status: timestamp.PKIStatusInfo{Status: 0},
TimeStampToken: ci,
}
if tt.hookResponse != nil {
resp = tt.hookResponse(resp)
}
return resp, nil
}
type testHTTPClient struct {
tt *testTSA
}
func (thc *testHTTPClient) Do(httpReq *http.Request) (*http.Response, error) {
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, httpReq.Body); err != nil {
return nil, err
}
var tsReq timestamp.Request
if _, err := asn1.Unmarshal(buf.Bytes(), &tsReq); err != nil {
return nil, err
}
tsResp, err := thc.tt.Do(tsReq)
if err != nil {
return nil, err
}
respDER, err := asn1.Marshal(tsResp)
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: 200,
Header: http.Header{"Content-Type": {"application/timestamp-reply"}},
Body: ioutil.NopCloser(bytes.NewReader(respDER)),
}, nil
}