/
keys.go
300 lines (278 loc) · 8.22 KB
/
keys.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
// Copyright 2020 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package internal
import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"golang.org/x/crypto/ssh"
)
const (
ecPrivateKeyPEMType = "EC PRIVATE KEY"
ecPublicKeyPEMType = "EC PUBLIC KEY"
pkcs8PrivateKeyPEMType = "PRIVATE KEY"
)
var (
// ErrBadPassphrase is a possible return error from LoadPEMPrivateKey()
ErrBadPassphrase = errors.New("passphrase incorrect for decrypting private key")
// ErrPassphraseRequired is a possible return error from LoadPEMPrivateKey()
ErrPassphraseRequired = errors.New("passphrase required for decrypting private key")
)
func openKeyFile(keyFile string) (*os.File, error) {
f, err := os.OpenFile(keyFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0400)
if err != nil {
return nil, fmt.Errorf("failed to open %v for writing: %v", keyFile, err)
}
return f, nil
}
// CopyKeyFile copies a keyfile, it wqill fail if it can't overwrite
// an existing file.
func CopyKeyFile(fromFile, toFile string) error {
to, err := openKeyFile(toFile)
if err != nil {
return err
}
defer to.Close()
from, err := os.Open(fromFile)
if err != nil {
return err
}
defer from.Close()
_, err = io.Copy(to, from)
return err
}
// WritePEMKeyPair writes a key pair in pem format.
func WritePEMKeyPair(key interface{}, privateKeyFile, publicKeyFile string, passphrase []byte) error {
private, err := openKeyFile(privateKeyFile)
if err != nil {
return err
}
defer private.Close()
public, err := openKeyFile(publicKeyFile)
if err != nil {
return err
}
defer public.Close()
if err := SavePEMKeyPair(private, public, key, passphrase); err != nil {
return fmt.Errorf("failed to save private key to %v, %v: %v", privateKeyFile, publicKeyFile, err)
}
return nil
}
// loadPEMPrivateKey loads a key from 'r'. returns ErrBadPassphrase for incorrect Passphrase.
// If the key held in 'r' is unencrypted, 'passphrase' will be ignored.
func LoadPEMPrivateKey(r io.Reader, passphrase []byte) (interface{}, error) {
pemBlockBytes, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
pemBlock, _ := pem.Decode(pemBlockBytes)
if pemBlock == nil {
return nil, fmt.Errorf("no PEM key block read")
}
var data []byte
if x509.IsEncryptedPEMBlock(pemBlock) {
// Assume empty passphrase is disallowed.
if len(passphrase) == 0 {
return nil, ErrPassphraseRequired
}
data, err = x509.DecryptPEMBlock(pemBlock, passphrase)
if err != nil {
return nil, ErrBadPassphrase
}
} else {
data = pemBlock.Bytes
}
switch pemBlock.Type {
case ecPrivateKeyPEMType:
key, err := x509.ParseECPrivateKey(data)
if err != nil {
// x509.DecryptPEMBlock may occasionally return random
// bytes for data with a nil error when the passphrase
// is invalid; hence, failure to parse data could be due
// to a bad passphrase.
return nil, ErrBadPassphrase
}
return key, nil
case pkcs8PrivateKeyPEMType:
key, err := x509.ParsePKCS8PrivateKey(data)
if err != nil {
return nil, ErrBadPassphrase
}
return key, nil
}
return nil, fmt.Errorf("PEM key block has an unrecognized type: %v", pemBlock.Type)
}
// LoadPEMPublicKeyFile loads a public key file in PEM PKIX format.
func LoadPEMPublicKeyFile(filename string) (interface{}, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
key, err := LoadPEMPublicKey(f)
if err != nil {
return nil, fmt.Errorf("failed to load public key from: %v: %v", filename, err)
}
return key, nil
}
// LoadPEMPublicKey loads a public key in PEM PKIX format.
func LoadPEMPublicKey(r io.Reader) (interface{}, error) {
pemBlockBytes, err := ioutil.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("failed to read bytes: %v", err)
}
pemBlock, _ := pem.Decode(pemBlockBytes)
if pemBlock == nil {
return nil, fmt.Errorf("no PEM key block read")
}
key, err := x509.ParsePKIXPublicKey(pemBlock.Bytes)
if err != nil {
return nil, fmt.Errorf("x509.ParsePKIXPublicKey: failed to parse bytes: %v", err)
}
return key, nil
}
// LoadSSHPublicKeyFile loads a public key file in SSH authorized hosts format.
func LoadSSHPublicKeyFile(filename string) (ssh.PublicKey, string, error) {
if !strings.HasSuffix(filename, ".pub") {
return nil, "", fmt.Errorf("%v does not have suffix: .pub", filename)
}
f, err := os.Open(filename)
if err != nil {
return nil, "", err
}
defer f.Close()
key, comment, err := LoadSSHPublicKey(f)
if err != nil {
return nil, "", fmt.Errorf("failed to load ssh public key from: %v: %v", filename, err)
}
return key, comment, nil
}
// LoadSSHPublicKey loads a public key in SSH authorized hosts format.
func LoadSSHPublicKey(r io.Reader) (ssh.PublicKey, string, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, "", fmt.Errorf("failed to read bytes: %v", err)
}
key, comment, _, _, err := ssh.ParseAuthorizedKey(data)
if err != nil {
return nil, "", err
}
return key, comment, nil
}
// SavePEMKey marshals 'key', encrypts it using 'passphrase', and saves the bytes to 'w' in PEM format.
// If passphrase is nil, the key will not be encrypted.
//
// For example, if key is an ECDSA private key, it will be marshaled
// in ASN.1, DER format, encrypted, and then written in a PEM block.
func SavePEMKeyPair(private, public io.Writer, key interface{}, passphrase []byte) error {
var privateData, publicData []byte
var err error
var pemType string
switch k := key.(type) {
case *ecdsa.PrivateKey:
if privateData, err = x509.MarshalECPrivateKey(k); err != nil {
return err
}
if public != nil {
if publicData, err = x509.MarshalPKIXPublicKey(&k.PublicKey); err != nil {
return err
}
}
pemType = ecPrivateKeyPEMType
case ed25519.PrivateKey:
if privateData, err = x509.MarshalPKCS8PrivateKey(k); err != nil {
return err
}
if public != nil {
if publicData, err = x509.MarshalPKIXPublicKey(k.Public()); err != nil {
return err
}
}
pemType = pkcs8PrivateKeyPEMType
default:
return fmt.Errorf("key of type %T cannot be saved", k)
}
var pemKey *pem.Block
if passphrase != nil {
pemKey, err = x509.EncryptPEMBlock(rand.Reader, pemType, privateData, passphrase, x509.PEMCipherAES256)
if err != nil {
return fmt.Errorf("failed to encrypt pem block: %v", err)
}
} else {
pemKey = &pem.Block{
Type: pemType,
Bytes: privateData,
}
}
if err := pem.Encode(private, pemKey); err != nil {
return err
}
if public == nil {
return nil
}
pemKey = &pem.Block{
Type: ecPublicKeyPEMType,
Bytes: publicData,
}
return pem.Encode(public, pemKey)
}
// ParseECDSAKey creates an ecdsa.PublicKey from an ssh ECDSA key.
func ParseECDSAKey(key ssh.PublicKey) (*ecdsa.PublicKey, error) {
var sshWire struct {
Name string
ID string
Key []byte
}
if err := ssh.Unmarshal(key.Marshal(), &sshWire); err != nil {
return nil, fmt.Errorf("failed to unmarshal key type: %v: %v", key.Type(), err)
}
pk := new(ecdsa.PublicKey)
switch sshWire.ID {
case "nistp256":
pk.Curve = elliptic.P256()
case "nistp384":
pk.Curve = elliptic.P384()
case "nistp521":
pk.Curve = elliptic.P521()
default:
return nil, fmt.Errorf("uncrecognised ecdsa curve: %v", sshWire.ID)
}
pk.X, pk.Y = elliptic.Unmarshal(pk.Curve, sshWire.Key)
if pk.X == nil || pk.Y == nil {
return nil, fmt.Errorf("invalid curve point")
}
return pk, nil
}
// ParseED25519Key creates an ed25519.PublicKey from an ssh ED25519 key.
func ParseED25519Key(key ssh.PublicKey) (ed25519.PublicKey, error) {
var sshWire struct {
Name string
KeyBytes []byte
}
if err := ssh.Unmarshal(key.Marshal(), &sshWire); err != nil {
return nil, fmt.Errorf("failed to unmarshal key %v: %v", key.Type(), err)
}
return ed25519.PublicKey(sshWire.KeyBytes), nil
}
// CryptoKeyFromSSHKey returns one of *ecdsa.PublicKey or
// ed25519.PublicKey from the the supplied ssh PublicKey.
func CryptoKeyFromSSHKey(pk ssh.PublicKey) (interface{}, error) {
switch pk.Type() {
case ssh.KeyAlgoECDSA256, ssh.KeyAlgoECDSA384, ssh.KeyAlgoECDSA521:
return ParseECDSAKey(pk)
case ssh.KeyAlgoED25519:
return ParseED25519Key(pk)
default:
return nil, fmt.Errorf("unsupported ssh key key tyoe %v", pk.Type())
}
}