-
Notifications
You must be signed in to change notification settings - Fork 88
/
gokey_test.go
279 lines (225 loc) · 6.62 KB
/
gokey_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
package gokey
import (
"bytes"
"crypto"
"encoding/asn1"
"encoding/hex"
"encoding/pem"
"reflect"
"strings"
"testing"
"golang.org/x/crypto/ed25519"
)
var passSpec = &PasswordSpec{16, 3, 3, 2, 1, ""}
func TestGetPass(t *testing.T) {
pass1Seed1, err := GenerateEncryptedKeySeed("pass1")
if err != nil {
t.Fatal(err)
}
pass1Seed2, err := GenerateEncryptedKeySeed("pass1")
if err != nil {
t.Fatal(err)
}
pass1Example1, err := GetPass("pass1", "example.com", nil, passSpec)
if err != nil {
t.Fatal(err)
}
pass1Example2, err := GetPass("pass1", "example2.com", nil, passSpec)
if err != nil {
t.Fatal(err)
}
pass2Example1, err := GetPass("pass2", "example.com", nil, passSpec)
if err != nil {
t.Fatal(err)
}
if strings.Compare(pass1Example1, pass1Example2) == 0 {
t.Fatal("passwords match for different realms")
}
if strings.Compare(pass1Example1, pass2Example1) == 0 {
t.Fatal("passwords match for different master passwords")
}
pass1Example1Seed1, err := GetPass("pass1", "example.com", pass1Seed1, passSpec)
if err != nil {
t.Fatal(err)
}
pass1Example1Seed2, err := GetPass("pass1", "example.com", pass1Seed2, passSpec)
if err != nil {
t.Fatal(err)
}
if strings.Compare(pass1Example1, pass1Example1Seed1) == 0 {
t.Fatal("passwords match for seeded and non-seeded master password")
}
if strings.Compare(pass1Example1Seed1, pass1Example1Seed2) == 0 {
t.Fatal("passwords match for different seeds")
}
pass1Example1Retry, err := GetPass("pass1", "example.com", nil, passSpec)
if err != nil {
t.Fatal(err)
}
pass1Example1Seed1Retry, err := GetPass("pass1", "example.com", pass1Seed1, passSpec)
if err != nil {
t.Fatal(err)
}
if (strings.Compare(pass1Example1, pass1Example1Retry) != 0) || (strings.Compare(pass1Example1Seed1, pass1Example1Seed1Retry) != 0) {
t.Fatal("passwords with same invocation options do not match")
}
}
func keyToBytes(key crypto.PrivateKey, t *testing.T) []byte {
buf := bytes.NewBuffer(nil)
err := EncodeToPem(key, buf)
if err != nil {
t.Fatal(err)
}
return buf.Bytes()
}
func testGetKeyType(kt KeyType, t *testing.T) {
pass1Seed1, err := GenerateEncryptedKeySeed("pass1")
if err != nil {
t.Fatal(err)
}
pass1Seed2, err := GenerateEncryptedKeySeed("pass1")
if err != nil {
t.Fatal(err)
}
key1Example1, err := GetKey("pass1", "example.com", nil, kt, true)
if err != nil {
t.Fatal(err)
}
key1Example2, err := GetKey("pass1", "example2.com", nil, kt, true)
if err != nil {
t.Fatal(err)
}
key2Example1, err := GetKey("pass2", "example.com", nil, kt, true)
if err != nil {
t.Fatal(err)
}
if bytes.Equal(keyToBytes(key1Example1, t), keyToBytes(key1Example2, t)) {
t.Fatal("keys match for different realms")
}
if bytes.Equal(keyToBytes(key1Example1, t), keyToBytes(key2Example1, t)) {
t.Fatal("keys match for different master passwords")
}
key1Example1Seed1, err := GetKey("pass1", "example.com", pass1Seed1, kt, false)
if err != nil {
t.Fatal(err)
}
key1Example1Seed2, err := GetKey("pass1", "example.com", pass1Seed2, kt, false)
if err != nil {
t.Fatal(err)
}
if bytes.Equal(keyToBytes(key1Example1, t), keyToBytes(key1Example1Seed1, t)) {
t.Fatal("keys match for seeded and non-seeded master password")
}
if bytes.Equal(keyToBytes(key1Example1Seed1, t), keyToBytes(key1Example1Seed2, t)) {
t.Fatal("keys match for different seeds")
}
key1Example1Retry, err := GetKey("pass1", "example.com", nil, kt, true)
if err != nil {
t.Fatal(err)
}
key1Example1Seed1Retry, err := GetKey("pass1", "example.com", pass1Seed1, kt, false)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(keyToBytes(key1Example1, t), keyToBytes(key1Example1Retry, t)) || !bytes.Equal(keyToBytes(key1Example1Seed1, t), keyToBytes(key1Example1Seed1Retry, t)) {
t.Fatal("keys with same invocation options do not match")
}
}
func TestGetKey(t *testing.T) {
for _, kt := range []KeyType{
EC256,
EC384,
EC521,
RSA2048,
RSA4096,
X25519,
ED25519,
} {
t.Run(kt.String(), func(t *testing.T) {
testGetKeyType(kt, t)
})
}
}
func TestGetKeyUnsafe(t *testing.T) {
_, err := GetKey("pass1", "example.com", nil, EC256, false)
if err == nil {
t.Fatal("allowed unsafe key generation")
}
}
func parse25519(t *testing.T, keyType KeyType, refKey string, refKeyBytes []byte) {
var suffix int
switch keyType {
case X25519:
suffix = x25519OidSuffix
case ED25519:
suffix = ed25519OidSuffix
}
block, _ := pem.Decode([]byte(refKey))
if block == nil {
t.Fatal("unable to pem-decode x25519 key")
}
var a25519 asn25519
_, err := asn1.Unmarshal(block.Bytes, &a25519)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(a25519.AlgId.Algorithm, asn1.ObjectIdentifier{1, 3, 101, suffix}) || !reflect.DeepEqual(a25519.PrivateKey, refKeyBytes) {
t.Fatalf("invalid %v key after parsing", keyType)
}
}
func TestParseX25519(t *testing.T) {
// generated by
// $ openssl genpkey -algorithm x25519
x25519Openssl := `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VuBCIEIEBQhR7E5x8vlFgWOhonI+/H3DY1R9mCh6wdwd8Hkgl1
-----END PRIVATE KEY-----`
// if you parse the key above the actual key bytes should be the following
// $ echo <above> | openssl asn1parse -i
x25519OpensslKeyBytes, err := hex.DecodeString("04204050851EC4E71F2F9458163A1A2723EFC7DC363547D98287AC1DC1DF07920975")
if err != nil {
t.Fatal(err)
}
parse25519(t, X25519, x25519Openssl, x25519OpensslKeyBytes)
}
func TestParseEd25519(t *testing.T) {
// taken from https://github.com/openssl/openssl/blob/60bbed3ff6716e8f1358396acc772908a758a0a0/test/certs/client-ed25519-key.pem
ed25519Openssl := `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEINZzpIpIiXXsKx4M7mUr2cb+DMfgHyu2msRAgNa5CxJJ
-----END PRIVATE KEY-----`
// if you parse the key above the actual key bytes should be the following
// $ echo <above> | openssl asn1parse -i
ed25519OpensslKeyBytes, err := hex.DecodeString("0420D673A48A488975EC2B1E0CEE652BD9C6FE0CC7E01F2BB69AC44080D6B90B1249")
if err != nil {
t.Fatal(err)
}
parse25519(t, ED25519, ed25519Openssl, ed25519OpensslKeyBytes)
}
func gen25519(t *testing.T, keyType KeyType) {
seed, err := GenerateEncryptedKeySeed("pass1")
if err != nil {
t.Fatal(err)
}
key, err := GetKey("pass1", "example.com", seed, keyType, false)
if err != nil {
t.Fatal(err)
}
var b strings.Builder
err = EncodeToPem(key, &b)
if err != nil {
t.Fatal(err)
}
var keyBytes []byte
switch keyType {
case X25519:
keyBytes = key.(x25519PrivateKey)[:]
case ED25519:
keyBytes = key.(*ed25519.PrivateKey).Seed()
}
parse25519(t, keyType, b.String(), append([]byte{0x04, 0x20}, keyBytes...))
}
func TestGenX25519(t *testing.T) {
gen25519(t, X25519)
}
func TestGenEd25519(t *testing.T) {
gen25519(t, ED25519)
}