forked from github/certstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
certstore_test.go
296 lines (258 loc) · 7.13 KB
/
certstore_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
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
package certstore
import (
"crypto"
"crypto/ecdsa"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"crypto/x509"
"testing"
"github.com/github/fakeca"
)
func TestImportDeleteRSA(t *testing.T) {
ImportDeleteHelper(t, leafRSA)
}
func TestImportDeleteECDSA(t *testing.T) {
ImportDeleteHelper(t, leafEC)
}
// ImportDeleteHelper is an abstraction for testing identity Import()/Delete().
func ImportDeleteHelper(t *testing.T, i *fakeca.Identity) {
withStore(t, func(store Store) {
// Import an identity
if err := store.Import(i.PFX("asdf"), "asdf"); err != nil {
t.Fatal(err)
}
// Look for our imported identity
idents, err := store.Identities()
if err != nil {
t.Fatal(err)
}
for _, ident := range idents {
defer ident.Close()
}
var found Identity
for _, ident := range idents {
crt, errr := ident.Certificate()
if errr != nil {
t.Fatal(errr)
}
if i.Certificate.Equal(crt) {
if found != nil {
t.Fatal("duplicate identity imported")
}
found = ident
}
}
if found == nil {
t.Fatal("imported identity not found")
}
// Delete it
if err = found.Delete(); err != nil {
t.Fatal(err)
}
// Look for our deleted identity
idents, err = store.Identities()
if err != nil {
t.Fatal(err)
}
for _, ident := range idents {
defer ident.Close()
}
found = nil
for _, ident := range idents {
crt, err := ident.Certificate()
if err != nil {
t.Fatal(err)
}
if i.Certificate.Equal(crt) {
found = ident
}
}
if found != nil {
t.Fatal("imported identity not deleted")
}
})
}
func TestSignerRSA(t *testing.T) {
rsaPriv, ok := leafRSA.PrivateKey.(*rsa.PrivateKey)
if !ok {
t.Fatal("expected priv to be an RSA private key")
}
withIdentity(t, leafRSA, func(ident Identity) {
signer, err := ident.Signer()
if err != nil {
t.Fatal(err)
}
pk := signer.Public()
rsaPub, ok := pk.(*rsa.PublicKey)
if !ok {
t.Fatal("expected pk to be an RSA public key")
}
if rsaPub.E != rsaPriv.E {
t.Fatalf("bad E. Got %d, expected %d", rsaPub.E, rsaPriv.E)
}
if rsaPub.N.Cmp(rsaPriv.N) != 0 {
t.Fatalf("bad N. Got %s, expected %s", rsaPub.N.Text(16), rsaPriv.N.Text(16))
}
// SHA1WithRSA
sha1Digest := sha1.Sum([]byte("hello"))
sig, err := signer.Sign(rand.Reader, sha1Digest[:], crypto.SHA1)
if err != nil {
// SHA1 should be supported by all platforms.
t.Fatal(err)
}
if err = leafRSA.Certificate.CheckSignature(x509.SHA1WithRSA, []byte("hello"), sig); err != nil {
t.Fatal(err)
}
// SHA256WithRSA
sha256Digest := sha256.Sum256([]byte("hello"))
sig, err = signer.Sign(rand.Reader, sha256Digest[:], crypto.SHA256)
if err == ErrUnsupportedHash {
// Some Windows CSPs may not support this algorithm. Pass...
} else if err != nil {
t.Fatal(err)
} else {
if err = leafRSA.Certificate.CheckSignature(x509.SHA256WithRSA, []byte("hello"), sig); err != nil {
t.Fatal(err)
}
}
// SHA384WithRSA
sha384Digest := sha512.Sum384([]byte("hello"))
sig, err = signer.Sign(rand.Reader, sha384Digest[:], crypto.SHA384)
if err == ErrUnsupportedHash {
// Some Windows CSPs may not support this algorithm. Pass...
} else if err != nil {
t.Fatal(err)
} else {
if err = leafRSA.Certificate.CheckSignature(x509.SHA384WithRSA, []byte("hello"), sig); err != nil {
t.Fatal(err)
}
}
// SHA512WithRSA
sha512Digest := sha512.Sum512([]byte("hello"))
sig, err = signer.Sign(rand.Reader, sha512Digest[:], crypto.SHA512)
if err == ErrUnsupportedHash {
// Some Windows CSPs may not support this algorithm. Pass...
} else if err != nil {
t.Fatal(err)
} else {
if err = leafRSA.Certificate.CheckSignature(x509.SHA512WithRSA, []byte("hello"), sig); err != nil {
t.Fatal(err)
}
}
// Bad digest size
_, err = signer.Sign(rand.Reader, sha1Digest[5:], crypto.SHA1)
if err == nil {
t.Fatal("expected error for bad digest size")
}
// Unsupported hash
sha224Digest := sha256.Sum224([]byte("hello"))
_, err = signer.Sign(rand.Reader, sha224Digest[:], crypto.SHA224)
if err != ErrUnsupportedHash {
t.Fatal("expected ErrUnsupportedHash, got ", err)
}
})
}
func TestSignerECDSA(t *testing.T) {
ecPriv, ok := leafEC.PrivateKey.(*ecdsa.PrivateKey)
if !ok {
t.Fatal("expected priv to be an ECDSA private key")
}
withIdentity(t, leafEC, func(ident Identity) {
signer, err := ident.Signer()
if err != nil {
t.Fatal(err)
}
pk := signer.Public()
ecPub, ok := pk.(*ecdsa.PublicKey)
if !ok {
t.Fatal("expected pk to be an RSA public key")
}
if ecPub.X.Cmp(ecPriv.X) != 0 {
t.Fatalf("bad X. Got %s, expected %s", ecPub.X.Text(16), ecPriv.X.Text(16))
}
if ecPub.Y.Cmp(ecPriv.Y) != 0 {
t.Fatalf("bad Y. Got %s, expected %s", ecPub.Y.Text(16), ecPriv.Y.Text(16))
}
// ECDSAWithSHA1
sha1Digest := sha1.Sum([]byte("hello"))
sig, err := signer.Sign(rand.Reader, sha1Digest[:], crypto.SHA1)
if err != nil {
t.Fatal(err)
}
if err = leafEC.Certificate.CheckSignature(x509.ECDSAWithSHA1, []byte("hello"), sig); err != nil {
t.Fatal(err)
}
// ECDSAWithSHA256
sha256Digest := sha256.Sum256([]byte("hello"))
sig, err = signer.Sign(rand.Reader, sha256Digest[:], crypto.SHA256)
if err != nil {
t.Fatal(err)
}
if err = leafEC.Certificate.CheckSignature(x509.ECDSAWithSHA256, []byte("hello"), sig); err != nil {
t.Fatal(err)
}
// ECDSAWithSHA384
sha384Digest := sha512.Sum384([]byte("hello"))
sig, err = signer.Sign(rand.Reader, sha384Digest[:], crypto.SHA384)
if err != nil {
t.Fatal(err)
}
if err = leafEC.Certificate.CheckSignature(x509.ECDSAWithSHA384, []byte("hello"), sig); err != nil {
t.Fatal(err)
}
// ECDSAWithSHA512
sha512Digest := sha512.Sum512([]byte("hello"))
sig, err = signer.Sign(rand.Reader, sha512Digest[:], crypto.SHA512)
if err != nil {
t.Fatal(err)
}
if err = leafEC.Certificate.CheckSignature(x509.ECDSAWithSHA512, []byte("hello"), sig); err != nil {
t.Fatal(err)
}
// Bad digest size
_, err = signer.Sign(rand.Reader, sha512Digest[5:], crypto.SHA512)
if err == nil {
t.Fatal("expected error for bad digest size")
}
})
}
func TestCertificateRSA(t *testing.T) {
CertificateHelper(t, leafRSA)
}
func TestCertificateEC(t *testing.T) {
CertificateHelper(t, leafEC)
}
func CertificateHelper(t *testing.T, leaf *fakeca.Identity) {
withIdentity(t, root, func(caIdent Identity) {
withIdentity(t, intermediate, func(interIdent Identity) {
withIdentity(t, leaf, func(leafIdent Identity) {
crtActual, err := leafIdent.Certificate()
if err != nil {
t.Fatal(err)
}
if !leaf.Certificate.Equal(crtActual) {
t.Fatal("Expected cert to match pfx")
}
chain, err := leafIdent.CertificateChain()
if err != nil {
t.Fatal(err)
}
if len(chain) != 3 {
t.Fatalf("bad chain len. expected 3, got %d", len(chain))
}
if !leaf.Certificate.Equal(chain[0]) {
t.Fatal("first chain cert should be leaf")
}
if !intermediate.Certificate.Equal(chain[1]) {
t.Fatal("second chain cert should be intermediate")
}
if !root.Certificate.Equal(chain[2]) {
t.Fatal("second chain cert should be intermediate")
}
})
})
})
}