-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
424 lines (376 loc) · 10.5 KB
/
main.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package main
import (
"bytes"
"crypto/hmac"
"crypto/rsa"
"crypto/sha256"
"crypto/sha512"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor"
"golang.org/x/crypto/openpgp/packet"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/ripemd160"
"golang.org/x/crypto/argon2"
"golang.org/x/crypto/ssh"
)
const (
seedSize = 32
)
type randReader struct {
seed, salt, key []byte
mu *sync.RWMutex
total int
}
func (r *randReader) Read(p []byte) (int, error) {
// https://github.com/cornfeedhobo/ssh-keydgen/blob/master/slowseeder/slowseeder.go
r.mu.Lock()
defer r.mu.Unlock()
r.seed = pbkdf2.Key(r.seed, r.key, 16, sha512.Size, sha512.New)
r.salt = pbkdf2.Key(r.salt, r.key, 16, ripemd160.Size, ripemd160.New)
r.key = argon2.Key(r.seed, r.salt, 1, 64*1024, 4, uint32(len(p)))
r.total += len(p)
return copy(p, r.key), nil
}
type key []byte
func (k key) extendKey(name string) key {
// HMAC-SHA256
h := hmac.New(sha256.New, k)
h.Write([]byte(name))
return h.Sum(nil)
}
func (k key) generateKey(basePath, path string) key {
pathList := strings.Split(path, "/")
if len(pathList) > 1 {
newBasePath := filepath.Join(basePath, pathList[0])
newPath := filepath.Join(pathList[1:]...)
// check for existence of override key
newDirKey, err := loadKey(newBasePath)
if err != nil {
newDirKey = k.extendKey(pathList[0])
}
return newDirKey.generateKey(newBasePath, newPath)
}
fileKey := k.extendKey(pathList[0])
return fileKey
}
func (k key) newRandReader() *randReader {
return &randReader{
seed: k,
salt: k,
mu: &sync.RWMutex{},
}
}
func (k key) generateRsaKey() (*rsa.PrivateKey, error) {
bitSize := 2048
privateKey, err := rsa.GenerateKey(k.newRandReader(), bitSize)
if err != nil {
return nil, err
}
err = privateKey.Validate()
if err != nil {
return nil, err
}
return privateKey, nil
}
func loadKey(dir string) (key, error) {
// check for raw ".dkey_master" file
master, err := ioutil.ReadFile(filepath.Join(dir, ".dkey_master"))
if err == nil {
if len(master) != seedSize {
return nil, fmt.Errorf("invalid master key length: expected=%v got=%v",
seedSize, len(master))
}
return key(master), nil
}
// check for user password ".rekey_pass" file
pass, err := ioutil.ReadFile(filepath.Join(dir, ".dkey_pass"))
if err == nil {
salt := pbkdf2.Key(pass, pass, 1024, sha512.Size, sha512.New)
key := argon2.IDKey(pass, salt, 256, 64*1024, 4, seedSize)
return key, nil
}
return nil, fmt.Errorf("missing .dkey_pass or .dkey_master in dir '%v'", dir)
}
type keyConfig struct {
path string
key key
options map[string]string
}
func (c keyConfig) KeyPath(file string) string {
return filepath.Join(c.path, file)
}
const (
readOnlyMode = 0600
)
type appKey interface {
OutputKeys(config *keyConfig) error
}
type defaultKey struct{}
func (k defaultKey) OutputKeys(config *keyConfig) error {
// write the secret to .dkey_master
keyPath := config.KeyPath(".dkey_master")
err := ioutil.WriteFile(keyPath, config.key, readOnlyMode)
if err != nil {
return fmt.Errorf("failed to write key file '%v': err=%v", keyPath, err)
}
return nil
}
type wireguardKey struct{}
func (k wireguardKey) OutputKeys(config *keyConfig) error {
// clamp the curve25519 key -- see https://git.zx2c4.com/WireGuard/tree/src/tools/curve25519.h
config.key[0] &= 248
config.key[31] = (config.key[31] & 127) | 64
// write the private key to "private_key"
privKeyData := []byte(base64.StdEncoding.EncodeToString(config.key))
privKeyPath := config.KeyPath("private_key")
err := ioutil.WriteFile(privKeyPath, privKeyData, readOnlyMode)
if err != nil {
return fmt.Errorf("failed to write wireguard private key file '%v': err=%v", privKeyPath, err)
}
return nil
}
type sshRSAKey struct{}
func (k sshRSAKey) OutputKeys(config *keyConfig) error {
privateKey, err := config.key.generateRsaKey()
if err != nil {
return fmt.Errorf("failed to generate RSA key for ssh key: err=%v", err)
}
// write private key
privKeyPath := config.KeyPath("id_rsa")
privKeyBytes := pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Headers: nil,
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
})
err = ioutil.WriteFile(privKeyPath, privKeyBytes, readOnlyMode)
if err != nil {
return fmt.Errorf("failed to write ssh rsa private key '%v': err=%v", privKeyPath, err)
}
// write public key
pubKeyPath := config.KeyPath("id_rsa.pub")
publicRsaKey, err := ssh.NewPublicKey(&privateKey.PublicKey)
if err != nil {
return fmt.Errorf("failed to generate ssh public key: err=%v", err)
}
pubKeyBytes := ssh.MarshalAuthorizedKey(publicRsaKey)
err = ioutil.WriteFile(pubKeyPath, pubKeyBytes, readOnlyMode)
if err != nil {
return fmt.Errorf("failed to write ssh rsa public key '%v': err=%v", pubKeyPath, err)
}
// log.Printf("ssh rsa key '%v': pub=%v", config.path, string(pubKeyBytes))
return nil
}
// TODO: https://github.com/mikesmitty/edkey
// no dropbear support for ed25519 keys
// https://github.com/pts/pts-dropbear / https://github.com/mkj/dropbear/pull/75
/*
type sshED25519Key struct{}
func (k sshED25519Key) OutputKeys(config *keyConfig) error {
return nil
}
*/
type gpgRSAKey struct{}
func (k gpgRSAKey) OutputKeys(config *keyConfig) error {
pktConfig := &packet.Config{
Rand: config.key.newRandReader(),
RSABits: 2048,
}
entity, err := openpgp.NewEntity(
config.options["name"],
config.options["comment"],
config.options["email"],
pktConfig,
)
if err != nil {
return fmt.Errorf("failed to generate entity for gpg key: err=%v", err)
}
privBuf := bytes.NewBuffer([]byte{})
privCloser, err := armor.Encode(privBuf, openpgp.PrivateKeyType, nil)
if err != nil {
return fmt.Errorf("failed to create armored private key encoder: err=%v", err)
}
err = entity.SerializePrivate(privCloser, pktConfig)
if err != nil {
return fmt.Errorf("failed to serialize gnupg private key: err=%v", err)
}
privCloser.Close()
privKeyPath := config.KeyPath("private.asc")
err = ioutil.WriteFile(privKeyPath, privBuf.Bytes(), readOnlyMode)
if err != nil {
return fmt.Errorf("failed to write gnupg private key '%v': err=%v", privKeyPath, err)
}
pubBuf := bytes.NewBuffer([]byte{})
pubCloser, err := armor.Encode(pubBuf, openpgp.PublicKeyType, nil)
if err != nil {
return fmt.Errorf("failed to create armored public key encoder: err=%v", err)
}
err = entity.Serialize(pubCloser)
if err != nil {
return fmt.Errorf("failed to serialize gnupg public key: err=%v", err)
}
pubCloser.Close()
pubKeyPath := config.KeyPath("public.asc")
err = ioutil.WriteFile(pubKeyPath, pubBuf.Bytes(), readOnlyMode)
if err != nil {
return fmt.Errorf("failed to write gnupg public key '%v': err=%v", pubKeyPath, err)
}
// log.Printf("gpg key '%v': pub=%v", config.path, pubBuf.String())
return nil
}
// TODO:
// golang openpgp doesn't have support for this yet...
// https://github.com/ProtonMail/crypto has support
// https://github.com/keybase/go-crypto has support
/*
type gpgED25519Key struct{}
func (k gpgED25519Key) OutputKeys(config *keyConfig) error {
return nil
}
*/
type luksKey struct{}
func (k luksKey) OutputKeys(config *keyConfig) error {
randReader := config.key.newRandReader()
size := 4096 / 8 // TODO: read size from config
keyBytes := make([]byte, size)
randReader.Read(keyBytes)
keyPath := config.KeyPath("keyfile")
err := ioutil.WriteFile(keyPath, keyBytes, readOnlyMode)
if err != nil {
return fmt.Errorf("failed to write luks keyfile '%v': err=%v", keyPath, err)
}
return nil
}
var (
appTypes = map[string]appKey{
"master": defaultKey{},
"aes-256": defaultKey{},
"wireguard": wireguardKey{},
"ssh-rsa": sshRSAKey{},
// "ssh-ed25519": sshED25519Key{},
"gpg-rsa": gpgRSAKey{},
// "gpg-ed25519": gpgED25519Key{},
"luks": luksKey{},
}
)
func parseOptions(sOptions string) map[string]string {
pairs := strings.Split(sOptions, ",")
if len(pairs) == 0 {
return nil
}
m := map[string]string{}
for _, pair := range pairs {
fields := strings.Split(pair, "=")
if len(fields) > 2 || len(fields) == 0 {
continue
}
key := fields[0]
val := ""
if len(fields) > 1 {
val = fields[1]
}
m[key] = val
}
return m
}
type appConfig struct {
Path string
App string
Options map[string]string
}
func (config appConfig) Process(rootKey key, baseDir string) {
path := filepath.Join(baseDir, config.Path)
key := rootKey.generateKey(baseDir, config.Path)
log.Printf("file key '%v' type '%v'", config.Path, config.App)
// log.Printf("file key '%v' type '%v': %v", config.Path, config.App,
// base64.StdEncoding.EncodeToString(key))
appType, ok := appTypes[config.App]
if !ok {
log.Fatalf("invalid app type '%v'", config.App)
}
err := os.MkdirAll(path, 0700)
if err != nil {
log.Fatalf("failed to create directory to '%v': err=%v", path, err)
}
keyConfig := &keyConfig{
path: path,
key: key,
options: config.Options,
}
err = appType.OutputKeys(keyConfig)
if err != nil {
log.Fatalf("failed to output keys for '%v' type '%v': err=%v",
config.Path, config.App, err)
}
}
func parseConfig(config string) *appConfig {
var path, app string
appOptions := map[string]string{}
parts := strings.Split(config, ":")
if len(parts) == 0 || len(parts[0]) == 0 {
return nil
}
path = parts[0]
if len(parts) > 1 {
app = parts[1]
}
if len(parts) > 2 {
appOptions = parseOptions(parts[2])
}
return &appConfig{
Path: path,
App: app,
Options: appOptions,
}
}
func loadConfig(baseDir string) ([]*appConfig, error) {
// read lines
configPath := filepath.Join(baseDir, ".dkey_config")
bytes, err := ioutil.ReadFile(configPath)
if err != nil {
return nil, fmt.Errorf("failed to read config file at '%v': err=%v",
configPath, err)
}
lines := strings.Split(string(bytes), "\n")
configs := []*appConfig{}
for _, line := range lines {
if strings.HasPrefix(strings.TrimSpace(line), "#") {
continue
}
config := parseConfig(line)
if config != nil {
configs = append(configs, config)
}
}
return configs, err
}
func main() {
baseDir := "."
if len(os.Args) > 1 {
baseDir = os.Args[1]
}
now := time.Now()
rootKey, err := loadKey(baseDir)
if err != nil {
log.Fatalf("main.loadKey: err=%v", err)
}
log.Printf("root key: load took %v ms", ((time.Now().UnixNano() - now.UnixNano()) / 1000000))
// log.Printf("root key: %v", base64.StdEncoding.EncodeToString(rootKey))
configs, err := loadConfig(baseDir)
if err != nil {
log.Fatalf("main.loadConfig: err=%v", err)
}
for _, config := range configs {
config.Process(rootKey, baseDir)
}
}