-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackup.go
More file actions
269 lines (220 loc) · 5.94 KB
/
backup.go
File metadata and controls
269 lines (220 loc) · 5.94 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
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
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/base32"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"strconv"
"strings"
"github.com/99designs/keyring"
"github.com/pkg/errors"
"golang.org/x/crypto/pbkdf2"
"github.com/endorama/2ami/internal/aegis"
)
const (
backupFormat2ami = "2ami"
backupFormatAegis = "aegis"
)
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, format string) error {
switch format {
case backupFormat2ami:
return restore2ami(storage, input, password)
case backupFormatAegis:
return restoreAegis(storage, input, password)
default:
return fmt.Errorf("unsupported backup format: %s", format)
}
}
func restore2ami(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 restoreAegis(storage Storage, input string, password string) error {
// Parse the Aegis backup
backup, err := aegis.ParseBackup([]byte(input))
if err != nil {
return fmt.Errorf("failed to parse Aegis backup: %w", err)
}
var db *aegis.DB
// Check if the backup is encrypted
if backup.IsEncrypted() {
// Decrypt the backup
db, err = backup.DecryptBackup(password)
if err != nil {
return fmt.Errorf("failed to decrypt Aegis backup: %w", err)
}
} else {
// Parse plain text backup
db, err = backup.ParsePlainBackup()
if err != nil {
return fmt.Errorf("failed to parse plain Aegis backup: %w", err)
}
}
// Import the entries
return importAegisEntries(storage, db.Entries)
}
func importAegisEntries(storage Storage, entries []aegis.Entry) error {
for _, entry := range entries {
// Extract secret from info
secretInterface, ok := entry.Info["secret"]
if !ok {
debugPrint(fmt.Sprintf("Skipping entry '%s' - no secret found", entry.Name))
continue
}
secret, ok := secretInterface.(string)
if !ok {
debugPrint(fmt.Sprintf("Skipping entry '%s' - invalid secret type", entry.Name))
continue
}
// Validate the secret is base32
if err := isValidBase32(secret); err != nil {
debugPrint(fmt.Sprintf("Skipping entry '%s' - invalid base32 secret: %v", entry.Name, err))
continue
}
// Determine entry name (use issuer + name if available)
entryName := entry.Name
if entry.Issuer != "" {
entryName = entry.Issuer + " - " + entry.Name
}
// Extract digits
var digits interface{}
if digitsInterface, ok := entry.Info["digits"]; ok {
if digitsFloat, ok := digitsInterface.(float64); ok {
digits = int(digitsFloat)
}
}
// Extract interval/period
var interval interface{}
if entry.Type == "totp" {
if periodInterface, ok := entry.Info["period"]; ok {
if periodFloat, ok := periodInterface.(float64); ok {
interval = int(periodFloat)
}
}
}
// Add the entry
debugPrint(fmt.Sprintf("Adding entry: %s, %s, %s, %s", entryName, secret, digits, interval))
err := add(storage, entryName, secret, digits, interval)
if err != nil {
debugPrint(fmt.Sprintf("Failed to add entry '%s': %v", entryName, err))
continue
}
debugPrint(fmt.Sprintf("Successfully imported entry: %s", entryName))
}
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)
}