forked from clocklock/go-rfc3161
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfc3161_test.go
165 lines (146 loc) · 3.59 KB
/
rfc3161_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
package rfc3161
import (
"crypto"
"crypto/sha1"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
"github.com/blang/semver"
)
func TestUnmarshal(t *testing.T) {
req, err := ReadTSQ("./test/sha1.tsq")
if err != nil {
t.Error(err)
}
err = req.Verify()
if err != nil {
t.Error(err)
}
_, err = ReadTSR("./test/sha1.response.tsr")
if err != nil {
t.Error(err)
}
req, err = ReadTSQ("./test/sha1_nonce.tsq")
if err != nil {
t.Error(err)
}
err = req.Verify()
if err != nil {
t.Error(err)
}
_, err = ReadTSR("./test/sha1_nonce.response.tsr")
if err != nil {
t.Error(err)
}
}
// Contruct the tsr manually
func TestReqBuildManually(t *testing.T) {
mes, err := ioutil.ReadFile("./test/message.txt")
if err != nil {
t.Error(err)
}
digest := sha1.Sum(mes)
tsr2, err := NewTimeStampReq(crypto.SHA1, digest[:])
if err != nil {
t.Error(err)
}
err = tsr2.GenerateNonce()
if err != nil {
t.Error(err)
}
err = tsr2.Verify()
if err != nil {
t.Error(err)
}
}
// Round-trip test with OpenSSL
func TestOpenSSL(t *testing.T) {
err := checkOpenSSL()
if err != nil {
fmt.Println("Unable to test OpenSSL. Skipping OpenSSL Test. " + err.Error())
return
}
// Create temp dir
dir, err := ioutil.TempDir("", "rfc3161_test")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(dir) // clean up
// Files
cakey := "cakey.pem"
cacert := "cacert.pem"
keypath := "private.pem"
csrpath := "request.csr"
crtpath := "cert.pem"
tsqpath := "request.tsq"
tsrpath := "response.tsr"
cnfpath := "openssl.conf"
mespath := "message.txt"
// Copy config and message
os.Link("test/openssl.conf", dir+"/"+cnfpath)
os.Link("test/message.txt", dir+"/"+mespath)
// Change directory to our temporary working directory
curdir, _ := os.Getwd()
os.Chdir(dir)
defer os.Chdir(curdir)
// Commands
commands := [][]string{
{"genrsa", "-out", cakey, "1024"},
{"req", "-x509", "-new", "-key", cakey, "-out", cacert, "-days", "730", "-subj", "/CN=\"Clock Lock Test CA\""},
{"genrsa", "-out", keypath, "1024"},
{"req", "-new", "-key", keypath, "-out", csrpath, "-subj", "/C=GB/ST=London/L=London/O=GORFC3161/OU=Testing/CN=example.com", "-config", cnfpath},
{"x509", "-req", "-days", "365", "-in", csrpath, "-CAkey", cakey, "-CA", cacert, "-set_serial", "01", "-out", crtpath, "-extfile", cnfpath},
{"ts", "-query", "-data", mespath, "-sha1", "-cert", "-out", tsqpath},
{"ts", "-reply", "-queryfile", tsqpath, "-out", tsrpath, "-inkey", keypath, "-signer", crtpath, "-config", cnfpath},
}
// Run commands
for _, cmd := range commands {
out, err := exec.Command("openssl", cmd...).Output()
if err != nil {
t.Error(err, string(out), string(err.(*exec.ExitError).Stderr))
}
}
// Add the certificate to the root list
RootCerts = x509.NewCertPool()
certbytes, err := ioutil.ReadFile(cacert)
if err != nil {
t.Error(err)
}
ok := RootCerts.AppendCertsFromPEM(certbytes)
if !ok {
t.Error("Unable to add root cert")
}
req, err := ReadTSQ(tsqpath)
if err != nil {
t.Error(err)
}
resp, err := ReadTSR(tsrpath)
if err != nil {
t.Error(err)
}
err = resp.Verify(req, nil)
if err != nil {
t.Error(err)
}
}
func checkOpenSSL() error {
out, err := exec.Command("openssl", "version").Output()
if err != nil {
return err
}
ver := strings.TrimRight(strings.Split(string(out), " ")[1], "abcdefghijklmnopqrstuvwxyz")
version, err := semver.Make(ver)
if err != nil {
return err
}
requiredVersion, _ := semver.Make("1.0.0")
if version.LT(requiredVersion) {
return errors.New("OpenSSL is required to be at least version 1.0.0 to test Time Stamping")
}
return nil
}