-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeys.go
More file actions
145 lines (118 loc) · 3.05 KB
/
keys.go
File metadata and controls
145 lines (118 loc) · 3.05 KB
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
package profilefed
import (
"crypto/ed25519"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"errors"
"os"
)
// LoadOrGenerateKeys checks whether the file at path exists. If it does,
// the private and public keys at that path are loaded and returned.
// If not, new keys are generated and saved to the given path.
func LoadOrGenerateKeys(path string) (ed25519.PublicKey, ed25519.PrivateKey, error) {
if _, err := os.Stat(path); err != nil {
return generateKeys(path)
}
return loadKeys(path)
}
func loadKeys(path string) (ed25519.PublicKey, ed25519.PrivateKey, error) {
privData, err := os.ReadFile(path)
if err != nil {
return nil, nil, err
}
pubData, err := os.ReadFile(path + ".pub")
if err != nil {
return nil, nil, err
}
privBlock, _ := pem.Decode(privData)
pubBlock, _ := pem.Decode(pubData)
if privBlock == nil {
return nil, nil, errors.New("invalid private key data")
}
if pubBlock == nil {
return nil, nil, errors.New("invalid public key data")
}
privkey, err := x509.ParsePKCS8PrivateKey(privBlock.Bytes)
if err != nil {
return nil, nil, err
}
pubkey, err := x509.ParsePKIXPublicKey(pubBlock.Bytes)
if err != nil {
return nil, nil, err
}
priv, ok := privkey.(ed25519.PrivateKey)
if !ok {
return nil, nil, errors.New("invalid private key type")
}
pub, ok := pubkey.(ed25519.PublicKey)
if !ok {
return nil, nil, errors.New("invalid public key type")
}
return pub, priv, nil
}
func generateKeys(path string) (ed25519.PublicKey, ed25519.PrivateKey, error) {
pub, priv, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return nil, nil, err
}
privData, err := x509.MarshalPKCS8PrivateKey(priv)
if err != nil {
return nil, nil, err
}
privBlock := &pem.Block{
Type: "ED25519 PRIVATE KEY",
Bytes: privData,
}
err = os.WriteFile(path, pem.EncodeToMemory(privBlock), 0o600)
if err != nil {
return nil, nil, err
}
pubData, err := x509.MarshalPKIXPublicKey(pub)
if err != nil {
return nil, nil, err
}
pubBlock := &pem.Block{
Type: "ED25519 PUBLIC KEY",
Bytes: pubData,
}
err = os.WriteFile(path+".pub", pem.EncodeToMemory(pubBlock), 0o644)
if err != nil {
return nil, nil, err
}
return pub, priv, nil
}
// LoadPrivateKeys loads the private keys at all the provided paths.
//
// Any invalid keys are skipped.
func LoadPrivateKeys(paths ...string) []ed25519.PrivateKey {
out := make([]ed25519.PrivateKey, len(paths))
for i, path := range paths {
privkey, err := LoadPrivateKey(path)
if err != nil {
continue
}
out[i] = privkey
}
return out
}
// LoadPrivateKey loads a private Ed25519 key from the given path.
func LoadPrivateKey(path string) (ed25519.PrivateKey, error) {
privData, err := os.ReadFile(path)
if err != nil {
return nil, err
}
privBlock, _ := pem.Decode(privData)
if privBlock == nil {
return nil, errors.New("invalid private key data")
}
privkey, err := x509.ParsePKCS8PrivateKey(privBlock.Bytes)
if err != nil {
return nil, err
}
priv, ok := privkey.(ed25519.PrivateKey)
if !ok {
return nil, errors.New("invalid private key type")
}
return priv, nil
}