-
Notifications
You must be signed in to change notification settings - Fork 1
/
keypairs_test.go
134 lines (120 loc) · 3.13 KB
/
keypairs_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
package keypairs
import (
"crypto/ecdsa"
"crypto/rsa"
"io/ioutil"
"log"
"net/http"
"testing"
)
// TODO package all fixtures with fileb0x
func TestParsePrivateKeyEC(t *testing.T) {
keys := [][]string{
[]string{"fixtures/privkey-ec-p256.jwk.json", "bJiCcZHuAF9dDetKEdGjJU3pFvFLoB_QHe9_6cAuY8c"},
// has openssl EC Param block
[]string{"fixtures/privkey-ec-p256.sec1.pem", "bJiCcZHuAF9dDetKEdGjJU3pFvFLoB_QHe9_6cAuY8c"},
[]string{"fixtures/privkey-ec-p256.pkcs8.pem", "bJiCcZHuAF9dDetKEdGjJU3pFvFLoB_QHe9_6cAuY8c"},
[]string{"fixtures/privkey-ec-p384.jwk.json", "-WoRXrk3FZ7tGi8oj5wJHDDfFMBCGlUbpwil1WhpxrU"},
[]string{"fixtures/privkey-ec-p384.sec1.pem", "-WoRXrk3FZ7tGi8oj5wJHDDfFMBCGlUbpwil1WhpxrU"},
[]string{"fixtures/privkey-ec-p384.pkcs8.pem", "-WoRXrk3FZ7tGi8oj5wJHDDfFMBCGlUbpwil1WhpxrU"},
}
for i := range keys {
path := keys[i][0]
thumb := keys[i][1]
b, err := ioutil.ReadFile(path)
if nil != err {
t.Fatal(path, err)
}
key, err := ParsePrivateKey(b)
if nil != err {
t.Fatal(path, err)
}
eckey := key.(*ecdsa.PrivateKey)
thumb2 := ThumbprintECPublicKey(eckey.Public().(*ecdsa.PublicKey))
if thumb != thumb2 {
t.Fatalf("EC thumbprints do not match: %q, %q, %q", path, thumb, thumb2)
}
}
}
func TestParseUnexpectedPrivateKey(t *testing.T) {
keypaths := []string{
"fixtures/privkey-ec-p256.jwk.json",
"fixtures/privkey-ec-p256.sec1.pem",
"fixtures/privkey-ec-p256.pkcs8.pem",
"fixtures/privkey-rsa-2048.jwk.json",
"fixtures/privkey-rsa-2048.pkcs1.pem",
"fixtures/privkey-rsa-2048.pkcs8.pem",
}
for i := range keypaths {
path := keypaths[i]
b, err := ioutil.ReadFile(path)
if nil != err {
t.Fatal(path, err)
}
_, err = ParsePublicKey(b)
switch err {
case ErrUnexpectedPrivateKey:
continue
default:
t.Fatal(path, err)
}
}
}
func TestParseUnexpectedPublicKey(t *testing.T) {
keypaths := []string{
"fixtures/pub-ec-p256.jwk.json",
}
for i := range keypaths {
path := keypaths[i]
b, err := ioutil.ReadFile(path)
if nil != err {
t.Fatal(path, err)
}
_, err = ParsePrivateKey(b)
switch err {
case ErrUnexpectedPublicKey:
continue
default:
t.Fatal(path, err)
}
}
}
func TestParsePrivateKeyRSA(t *testing.T) {
keypaths := []string{
"fixtures/privkey-rsa-2048.jwk.json",
"fixtures/privkey-rsa-2048.pkcs1.pem",
"fixtures/privkey-rsa-2048.pkcs8.pem",
}
for i := range keypaths {
path := keypaths[i]
b, err := ioutil.ReadFile(path)
if nil != err {
t.Fatal(path, err)
}
key, err := ParsePrivateKey(b)
if nil != err {
t.Fatal(path, err)
}
rsakey := key.(*rsa.PrivateKey)
thumb := "UIyZzFXPL-mTLnxQeSAHgu7gV16tro3evksnFb8fFQQ"
thumb2 := ThumbprintRSAPublicKey(rsakey.Public().(*rsa.PublicKey))
if thumb != thumb2 {
t.Fatalf("RSA thumbprints do not match: %q, %q, %q", path, thumb, thumb2)
}
}
}
func TestParseCertificate(t *testing.T) {
resp, err := http.Get("https://example.auth0.com/pem")
if nil != err {
log.Fatal(err)
}
bytes, err := ioutil.ReadAll(resp.Body)
if nil != err {
log.Fatal(err)
}
_, err = ParsePublicKey(bytes)
if nil != err {
log.Fatal("Could not parse PEM/cert from auth0")
log.Fatal(err)
}
}