-
Notifications
You must be signed in to change notification settings - Fork 0
/
privatekey_test.go
332 lines (275 loc) · 10.3 KB
/
privatekey_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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package keybox_test
import (
"github.com/kernle32dll/keybox-go"
"github.com/youmark/pkcs8"
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"io/ioutil"
"os"
"reflect"
"testing"
)
func TestLoadPrivateKey(t *testing.T) {
t.Parallel()
// Generate keys
rsaKey := GenerateRSAKey(t)
rsaPKCS8 := WriteBytesToTempFile(t, PEMEncodePKCS8PrivateKey(t, rsaKey))
type args struct {
path string
}
tests := []struct {
name string
args args
want crypto.PrivateKey
wantErr error
}{
{name: "valid key", args: args{path: rsaPKCS8}, want: rsaKey, wantErr: nil},
{name: "non-existent file", args: args{path: "does not exist"}, want: nil, wantErr: os.ErrNotExist},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := keybox.LoadPrivateKey(tt.args.path)
if !errors.Is(err, tt.wantErr) {
t.Errorf("LoadPrivateKey() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("LoadPrivateKey() got = %v, want %v", got, tt.want)
}
})
}
}
func TestLoadPrivateKeyWithPassword(t *testing.T) {
t.Parallel()
testPassphrase := "super-passphrase"
// Generate keys
rsaKey := GenerateRSAKey(t)
encryptedRsaPKCS8 := WriteBytesToTempFile(t, PEMEncryptDERBytes(t, PEMEncodePKCS8PrivateKey(t, rsaKey), testPassphrase))
encryptedRsaPKCS8WrongPassword := WriteBytesToTempFile(t, PEMEncryptDERBytes(t, PEMEncodePKCS8PrivateKey(t, rsaKey), "nope"))
type args struct {
path string
}
tests := []struct {
name string
args args
want crypto.PrivateKey
wantErr error
}{
{name: "valid key", args: args{path: encryptedRsaPKCS8}, want: rsaKey, wantErr: nil},
{name: "valid key wrong password", args: args{path: encryptedRsaPKCS8WrongPassword}, want: nil, wantErr: x509.IncorrectPasswordError},
{name: "non-existent file", args: args{path: "does not exist"}, want: nil, wantErr: os.ErrNotExist},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := keybox.LoadPrivateKeyWithPassword(tt.args.path, []byte(testPassphrase))
if !errors.Is(err, tt.wantErr) {
t.Errorf("LoadPrivateKeyWithPassword() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("LoadPrivateKeyWithPassword() got = %v, want %v", got, tt.want)
}
})
}
}
func TestParsePrivateKeyFromEncryptedPEMBytes(t *testing.T) {
t.Parallel()
testPassphrase := "super-passphrase"
// Generate keys
rsaKey := GenerateRSAKey(t)
encryptedRsaPKCS1 := PEMEncryptDERBytes(t, PEMEncodePKCS1PrivateKey(rsaKey), testPassphrase)
encryptedRsaPKCS8 := PEMEncryptDERBytes(t, PEMEncodePKCS8PrivateKey(t, rsaKey), testPassphrase)
encryptedRsaPKCS8WrongPassword := PEMEncryptDERBytes(t, PEMEncodePKCS8PrivateKey(t, rsaKey), "nope")
rsaPKCS8Encrypted := PEMEncodePKCS8PrivateKeyEncrypted(t, rsaKey, testPassphrase)
rsaPKCS8EncryptedWrongPassword := PEMEncodePKCS8PrivateKeyEncrypted(t, rsaKey, "nope")
ecdsaKey := GenerateECDSAKey(t)
encryptedEcdsaPKCS8 := PEMEncryptDERBytes(t, PEMEncodePKCS8PrivateKey(t, ecdsaKey), testPassphrase)
encryptedEcdsaSEC1 := PEMEncryptDERBytes(t, PEMEncodeSEC1PrivateKey(t, ecdsaKey), testPassphrase)
ecdsaPKCS8Encrypted := PEMEncodePKCS8PrivateKeyEncrypted(t, ecdsaKey, testPassphrase)
ed25519Key := GenerateEd25519Key(t)
encryptedEd25519PKCS8 := PEMEncryptDERBytes(t, PEMEncodePKCS8PrivateKey(t, ed25519Key), testPassphrase)
ed25519PKCS8Encrypted := PEMEncodePKCS8PrivateKeyEncrypted(t, ed25519Key, testPassphrase)
trashKey := pem.EncodeToMemory(&pem.Block{Type: "TRASH PRIVATE KEY"})
encryptedTrashKey := PEMEncryptDERBytes(t, trashKey, testPassphrase)
type args struct {
pemBytes []byte
}
tests := []struct {
name string
args args
want crypto.PrivateKey
wantErr error
}{
{name: "RSA PKCS1 RFC1423", args: args{pemBytes: encryptedRsaPKCS1}, want: rsaKey, wantErr: nil},
{name: "RSA PKCS8 RFC1423", args: args{pemBytes: encryptedRsaPKCS8}, want: rsaKey, wantErr: nil},
{name: "RSA PKCS8 RFC1423 wrong password", args: args{pemBytes: encryptedRsaPKCS8WrongPassword}, want: nil, wantErr: x509.IncorrectPasswordError},
{name: "RSA PKCS8 2.0", args: args{pemBytes: rsaPKCS8Encrypted}, want: rsaKey, wantErr: nil},
{name: "RSA PKCS8 2.0 wrong password", args: args{pemBytes: rsaPKCS8EncryptedWrongPassword}, want: nil, wantErr: keybox.ErrUnknownEncryption},
{name: "ecdsa PKCS8 RFC1423", args: args{pemBytes: encryptedEcdsaPKCS8}, want: ecdsaKey, wantErr: nil},
{name: "ecdsa SEC1 RFC1423", args: args{pemBytes: encryptedEcdsaSEC1}, want: ecdsaKey, wantErr: nil},
{name: "ecdsa PKCS8 2.0", args: args{pemBytes: ecdsaPKCS8Encrypted}, want: ecdsaKey, wantErr: nil},
{name: "ed25519 PKCS8 RFC1423", args: args{pemBytes: encryptedEd25519PKCS8}, want: ed25519Key, wantErr: nil},
{name: "ed25519 PKCS8 2.0", args: args{pemBytes: ed25519PKCS8Encrypted}, want: ed25519Key, wantErr: nil},
{name: "PEM but not a private key", args: args{pemBytes: encryptedTrashKey}, want: nil, wantErr: keybox.ErrNotAPrivateKey},
{name: "non-pem", args: args{pemBytes: []byte("shall not decode")}, want: nil, wantErr: keybox.ErrKeyMustBePEMEncoded},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := keybox.ParsePrivateKeyFromEncryptedPEMBytes(tt.args.pemBytes, []byte(testPassphrase))
if !errors.Is(err, tt.wantErr) {
t.Errorf("ParsePrivateKeyFromPEMBytes() error = %s, wantErr %s", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParsePrivateKeyFromPEMBytes() got = %v, want %v", got, tt.want)
}
})
}
t.Run("PEM but not encrypted", func(t *testing.T) {
got, err := keybox.ParsePrivateKeyFromEncryptedPEMBytes(trashKey, []byte(testPassphrase))
if err == nil {
t.Errorf("ParsePrivateKeyFromPEMBytes() error = false, wantErr true")
return
}
if got != nil {
t.Errorf("ParsePrivateKeyFromPEMBytes() got = %v, want nil", got)
}
})
}
func TestParsePrivateKeyFromPEMBytes(t *testing.T) {
t.Parallel()
// Generate keys
rsaKey := GenerateRSAKey(t)
rsaPKCS1 := PEMEncodePKCS1PrivateKey(rsaKey)
rsaPKCS8 := PEMEncodePKCS8PrivateKey(t, rsaKey)
ecdsaKey := GenerateECDSAKey(t)
ecdsaPKCS8 := PEMEncodePKCS8PrivateKey(t, ecdsaKey)
ecdsaSEC1 := PEMEncodeSEC1PrivateKey(t, ecdsaKey)
ed25519Key := GenerateEd25519Key(t)
ed25519PKCS8 := PEMEncodePKCS8PrivateKey(t, ed25519Key)
trashKey := pem.EncodeToMemory(&pem.Block{Type: "TRASH PRIVATE KEY"})
type args struct {
pemBytes []byte
}
tests := []struct {
name string
args args
want crypto.PrivateKey
wantErr error
}{
{name: "RSA PKCS1", args: args{pemBytes: rsaPKCS1}, want: rsaKey, wantErr: nil},
{name: "RSA PKCS8", args: args{pemBytes: rsaPKCS8}, want: rsaKey, wantErr: nil},
{name: "ecdsa PKCS8", args: args{pemBytes: ecdsaPKCS8}, want: ecdsaKey, wantErr: nil},
{name: "ecdsa SEC1", args: args{pemBytes: ecdsaSEC1}, want: ecdsaKey, wantErr: nil},
{name: "ed25519 PKCS8", args: args{pemBytes: ed25519PKCS8}, want: ed25519Key, wantErr: nil},
{name: "PEM but not a private key", args: args{pemBytes: trashKey}, want: nil, wantErr: keybox.ErrNotAPrivateKey},
{name: "non-pem", args: args{pemBytes: []byte("shall not decode")}, want: nil, wantErr: keybox.ErrKeyMustBePEMEncoded},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := keybox.ParsePrivateKeyFromPEMBytes(tt.args.pemBytes)
if !errors.Is(err, tt.wantErr) {
t.Errorf("ParsePrivateKeyFromPEMBytes() error = %s, wantErr %s", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParsePrivateKeyFromPEMBytes() got = %v, want %v", got, tt.want)
}
})
}
}
func GenerateRSAKey(t *testing.T) *rsa.PrivateKey {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("failed to generate RSA key: %s", err)
}
return privateKey
}
func GenerateEd25519Key(t *testing.T) ed25519.PrivateKey {
_, privateKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
t.Fatalf("failed to generate ed25519 key: %s", err)
}
return privateKey
}
func GenerateECDSAKey(t *testing.T) *ecdsa.PrivateKey {
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatalf("failed to generate ecdsa key: %s", err)
}
return privateKey
}
func PEMEncodePKCS1PrivateKey(key *rsa.PrivateKey) []byte {
privateKeyBlock := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
}
return pem.EncodeToMemory(privateKeyBlock)
}
func PEMEncodePKCS8PrivateKey(t *testing.T, key crypto.PrivateKey) []byte {
privateKeyBytes, err := x509.MarshalPKCS8PrivateKey(key)
if err != nil {
t.Fatalf("failed to marshall key: %s", err)
}
privateKeyBlock := &pem.Block{
Type: "PRIVATE KEY",
Bytes: privateKeyBytes,
}
return pem.EncodeToMemory(privateKeyBlock)
}
func PEMEncodePKCS8PrivateKeyEncrypted(t *testing.T, key crypto.PrivateKey, password string) []byte {
privateKeyBytes, err := pkcs8.MarshalPrivateKey(key, []byte(password), nil)
if err != nil {
t.Fatalf("failed to marshall key: %s", err)
}
privateKeyBlock := &pem.Block{
Type: "PRIVATE KEY",
Bytes: privateKeyBytes,
}
return pem.EncodeToMemory(privateKeyBlock)
}
func PEMEncodeSEC1PrivateKey(t *testing.T, key *ecdsa.PrivateKey) []byte {
privateKeyBytes, err := x509.MarshalECPrivateKey(key)
if err != nil {
t.Fatalf("failed to marshall ecdsa key: %s", err)
}
privateKeyBlock := &pem.Block{
Type: "EC PRIVATE KEY",
Bytes: privateKeyBytes,
}
return pem.EncodeToMemory(privateKeyBlock)
}
func PEMEncryptDERBytes(t *testing.T, bytes []byte, password string) []byte {
var block *pem.Block
if block, _ = pem.Decode(bytes); block == nil {
t.Fatal("failed to decode pem bytes")
}
privateKeyBlock, err := x509.EncryptPEMBlock(rand.Reader, block.Type, block.Bytes, []byte(password), x509.PEMCipherAES256)
if err != nil {
t.Fatalf("failed to encrypt pem block: %s", err)
}
return pem.EncodeToMemory(privateKeyBlock)
}
func WriteBytesToTempFile(t *testing.T, bytes []byte) string {
file, err := ioutil.TempFile("", "")
if err != nil {
t.Fatalf("failed to create temp file: %s", err)
}
defer file.Close()
t.Cleanup(func() {
if err := os.Remove(file.Name()); err != nil {
t.Logf("failed to delete temp file: %s", err)
}
})
if _, err := file.Write(bytes); err != nil {
t.Fatalf("failed to write temp file: %s", err)
}
return file.Name()
}