-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup.go
163 lines (131 loc) · 3.23 KB
/
backup.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
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/base32"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/99designs/keyring"
"github.com/pkg/errors"
"golang.org/x/crypto/pbkdf2"
"io"
"strconv"
"strings"
)
type backup struct {
Name string `json:"name"`
Digits string `json:"digits"`
Interval string `json:"interval"`
Secret string `json:"secret"`
}
func backupAllKeys(storage Storage, password string) (string, error) {
ring, err := openKeyring()
if err != nil {
return "", err
}
keys, err := storage.ListKey()
if err != nil {
return "", err
}
allKeys := make([]string, 0)
for _, v := range keys {
value, err := backupKeyFromRing(storage, ring, v, password)
if err != nil {
return "", err
}
allKeys = append(allKeys, value)
}
return strings.Join(allKeys, "."), nil
}
func restore(storage Storage, input string, password string) error {
sections := strings.Split(input, ".")
for _, section := range sections {
b, err := decryptBackup(section, password)
if err != nil {
return err
}
var digits interface{}
if b.Digits != "" {
digits = b.Digits
}
var interval interface{}
if b.Interval != "" {
interval = b.Interval
}
err = add(storage, b.Name, b.Secret, digits, interval)
if err != nil {
return err
}
}
return nil
}
func backupKeyFromRing(storage Storage, ring keyring.Keyring, keyName string, password string) (string, error) {
debugPrint(fmt.Sprintf("Retrieving and encrypting key '%v' for backup", keyName))
key := KeyFromStorage(storage, ring, keyName)
rawSecret, err := key.secret.Value()
if err != nil {
return "", err
}
secret := base32.StdEncoding.EncodeToString(rawSecret)
b := backup{
Name: key.Name,
Digits: strconv.Itoa(key.Digits),
Interval: strconv.Itoa(key.Interval),
Secret: secret,
}
return encryptBackup(b, password)
}
func encryptBackup(value backup, password string) (string, error) {
encoded, err := json.Marshal(value)
if err != nil {
return "", err
}
c, err := aes.NewCipher(deriveKey(password))
if err != nil {
return "", err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return "", err
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return "", err
}
sealed := gcm.Seal(nonce, nonce, encoded, nil)
return base64.StdEncoding.EncodeToString(sealed), nil
}
func decryptBackup(value string, password string) (backup, error) {
decoded, err := base64.StdEncoding.DecodeString(value)
if err != nil {
return backup{}, err
}
c, err := aes.NewCipher(deriveKey(password))
if err != nil {
return backup{}, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return backup{}, err
}
nonceSize := gcm.NonceSize()
if len(decoded) < nonceSize {
return backup{}, errors.New("invalid nonce size, cannot decrypt supplied value")
}
nonce, ciphertext := decoded[:nonceSize], decoded[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return backup{}, err
}
b := backup{}
if err := json.Unmarshal(plaintext, &b); err != nil {
return backup{}, err
}
return b, nil
}
func deriveKey(passphrase string) []byte {
return pbkdf2.Key([]byte(passphrase), nil, 1000, 32, sha256.New)
}