| 1 |
package cms |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/ecdsa" |
| 6 |
"crypto/elliptic" |
| 7 |
"crypto/rand" |
| 8 |
"crypto/x509" |
| 9 |
"encoding/asn1" |
| 10 |
"io" |
| 11 |
"io/ioutil" |
| 12 |
"math/big" |
| 13 |
"net/http" |
| 14 |
"time" |
| 15 |
|
| 16 |
"github.com/github/smimesign/fakeca" |
| 17 |
"github.com/github/smimesign/ietf-cms/oid" |
| 18 |
"github.com/github/smimesign/ietf-cms/protocol" |
| 19 |
"github.com/github/smimesign/ietf-cms/timestamp" |
| 20 |
) |
| 21 |
|
| 22 |
var ( |
| 23 |
|
| 24 |
root = fakeca.New(fakeca.IsCA) |
| 25 |
otherRoot = fakeca.New(fakeca.IsCA) |
| 26 |
|
| 27 |
intermediateKey, _ = ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 28 |
intermediate = root.Issue(fakeca.IsCA, fakeca.PrivateKey(intermediateKey)) |
| 29 |
|
| 30 |
leaf = intermediate.Issue( |
| 31 |
fakeca.NotBefore(time.Now().Add(-time.Hour)), |
| 32 |
fakeca.NotAfter(time.Now().Add(time.Hour)), |
| 33 |
) |
| 34 |
|
| 35 |
rootOpts = x509.VerifyOptions{Roots: root.ChainPool()} |
| 36 |
otherRootOpts = x509.VerifyOptions{Roots: otherRoot.ChainPool()} |
| 37 |
intermediateOpts = x509.VerifyOptions{Roots: intermediate.ChainPool()} |
| 38 |
|
| 39 |
|
| 40 |
tsa = &testTSA{ident: intermediate.Issue()} |
| 41 |
thc = &testHTTPClient{tsa} |
| 42 |
) |
| 43 |
|
| 44 |
func init() { |
| 45 |
timestamp.DefaultHTTPClient = thc |
| 46 |
} |
| 47 |
|
| 48 |
type testTSA struct { |
| 49 |
ident *fakeca.Identity |
| 50 |
sn int64 |
| 51 |
hookInfo func(timestamp.Info) timestamp.Info |
| 52 |
hookToken func(*protocol.SignedData) *protocol.SignedData |
| 53 |
hookResponse func(timestamp.Response) timestamp.Response |
| 54 |
} |
| 55 |
|
| 56 |
func (tt *testTSA) Clear() { |
| 57 |
tt.hookInfo = nil |
| 58 |
tt.hookToken = nil |
| 59 |
tt.hookResponse = nil |
| 60 |
} |
| 61 |
|
| 62 |
func (tt *testTSA) HookInfo(hook func(timestamp.Info) timestamp.Info) { |
| 63 |
tt.Clear() |
| 64 |
tt.hookInfo = hook |
| 65 |
} |
| 66 |
|
| 67 |
func (tt *testTSA) HookToken(hook func(*protocol.SignedData) *protocol.SignedData) { |
| 68 |
tt.Clear() |
| 69 |
tt.hookToken = hook |
| 70 |
} |
| 71 |
|
| 72 |
func (tt *testTSA) HookResponse(hook func(timestamp.Response) timestamp.Response) { |
| 73 |
tt.Clear() |
| 74 |
tt.hookResponse = hook |
| 75 |
} |
| 76 |
|
| 77 |
func (tt *testTSA) nextSN() *big.Int { |
| 78 |
defer func() { tt.sn++ }() |
| 79 |
return big.NewInt(tt.sn) |
| 80 |
} |
| 81 |
|
| 82 |
func (tt *testTSA) Do(req timestamp.Request) (timestamp.Response, error) { |
| 83 |
info := timestamp.Info{ |
| 84 |
Version: 1, |
| 85 |
Policy: asn1.ObjectIdentifier{1, 2, 3}, |
| 86 |
SerialNumber: tt.nextSN(), |
| 87 |
GenTime: time.Now(), |
| 88 |
MessageImprint: req.MessageImprint, |
| 89 |
Nonce: req.Nonce, |
| 90 |
} |
| 91 |
|
| 92 |
if tt.hookInfo != nil { |
| 93 |
info = tt.hookInfo(info) |
| 94 |
} |
| 95 |
|
| 96 |
eciDER, err := asn1.Marshal(info) |
| 97 |
if err != nil { |
| 98 |
panic(err) |
| 99 |
} |
| 100 |
|
| 101 |
eci, err := protocol.NewEncapsulatedContentInfo(oid.ContentTypeTSTInfo, eciDER) |
| 102 |
if err != nil { |
| 103 |
panic(err) |
| 104 |
} |
| 105 |
|
| 106 |
tst, err := protocol.NewSignedData(eci) |
| 107 |
if err != nil { |
| 108 |
panic(err) |
| 109 |
} |
| 110 |
|
| 111 |
if err = tst.AddSignerInfo(tsa.ident.Chain(), tsa.ident.PrivateKey); err != nil { |
| 112 |
panic(err) |
| 113 |
} |
| 114 |
|
| 115 |
if tt.hookToken != nil { |
| 116 |
tt.hookToken(tst) |
| 117 |
} |
| 118 |
|
| 119 |
ci, err := tst.ContentInfo() |
| 120 |
if err != nil { |
| 121 |
panic(err) |
| 122 |
} |
| 123 |
|
| 124 |
resp := timestamp.Response{ |
| 125 |
Status: timestamp.PKIStatusInfo{Status: 0}, |
| 126 |
TimeStampToken: ci, |
| 127 |
} |
| 128 |
|
| 129 |
if tt.hookResponse != nil { |
| 130 |
resp = tt.hookResponse(resp) |
| 131 |
} |
| 132 |
|
| 133 |
return resp, nil |
| 134 |
} |
| 135 |
|
| 136 |
type testHTTPClient struct { |
| 137 |
tt *testTSA |
| 138 |
} |
| 139 |
|
| 140 |
func (thc *testHTTPClient) Do(httpReq *http.Request) (*http.Response, error) { |
| 141 |
buf := new(bytes.Buffer) |
| 142 |
if _, err := io.Copy(buf, httpReq.Body); err != nil { |
| 143 |
return nil, err |
| 144 |
} |
| 145 |
|
| 146 |
var tsReq timestamp.Request |
| 147 |
if _, err := asn1.Unmarshal(buf.Bytes(), &tsReq); err != nil { |
| 148 |
return nil, err |
| 149 |
} |
| 150 |
|
| 151 |
tsResp, err := thc.tt.Do(tsReq) |
| 152 |
if err != nil { |
| 153 |
return nil, err |
| 154 |
} |
| 155 |
|
| 156 |
respDER, err := asn1.Marshal(tsResp) |
| 157 |
if err != nil { |
| 158 |
return nil, err |
| 159 |
} |
| 160 |
|
| 161 |
return &http.Response{ |
| 162 |
StatusCode: 200, |
| 163 |
Header: http.Header{"Content-Type": {"application/timestamp-reply"}}, |
| 164 |
Body: ioutil.NopCloser(bytes.NewReader(respDER)), |
| 165 |
}, nil |
| 166 |
} |