-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaegis.go
247 lines (218 loc) · 5.89 KB
/
aegis.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
// This file is part of termOTP, a TOTP program for your terminal.
// https://github.com/marcopaganini/termotp.
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"regexp"
"strings"
"github.com/romana/rlog"
"github.com/xlzd/gotp"
"golang.org/x/crypto/scrypt"
"golang.org/x/term"
)
const (
aegisKeyLen = 32
)
// aegisEncryptedJSON represents an encrypted Aegis JSON export file.
type aegisEncryptedJSON struct {
Version int `json:"version"`
Header struct {
Slots []struct {
Type int `json:"type"`
UUID string `json:"uuid"`
Key string `json:"key"`
KeyParams struct {
Nonce string `json:"nonce"`
Tag string `json:"tag"`
} `json:"key_params"`
N int `json:"n"`
R int `json:"r"`
P int `json:"p"`
Salt string `json:"salt"`
} `json:"slots"`
Params struct {
Nonce string `json:"nonce"`
Tag string `json:"tag"`
} `json:"params"`
} `json:"header"`
Db string `json:"db"`
}
// aegisJSON represents a plain Aegis JSON export file.
type aegisJSON struct {
Version int `json:"version"`
Entries []struct {
Type string `json:"type"`
Name string `json:"name"`
Issuer string `json:"issuer"`
Icon string `json:"icon"`
Info struct {
Secret string `json:"secret"`
Digits int `json:"digits"`
Algo string `json:"algo"`
Period int `json:"period"`
} `json:"info"`
}
}
// newAES creates a new AESGCM cipher.
func newAES(key []byte) (cipher.AEAD, error) {
// AES GCM decrypt.
b, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
aesgcm, err := cipher.NewGCM(b)
if err != nil {
return nil, err
}
return aesgcm, nil
}
// readPassword reads the user password from the terminal. If the input is a
// terminal, it uses terminal specific codes to turn off typing echo. If the
// input is not a terminal, it assumes we can read the password directly from
// it (E.g, when redirecting from a process or a file.)
func readPassword() ([]byte, error) {
fi, err := os.Stdin.Stat()
if err != nil {
return nil, err
}
// Test if we're reading from pipe or terminal.
var password string
if (fi.Mode() & os.ModeCharDevice) != 0 {
// Reading from terminal.
savedState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
return nil, err
}
defer term.Restore(int(os.Stdin.Fd()), savedState)
terminal := term.NewTerminal(os.Stdin, ">")
password, err = terminal.ReadPassword("Enter password: ")
if err != nil {
return nil, err
}
} else {
// 256-byte passwords ought to be enough for everybody :)
buf := make([]byte, 256)
if _, err := os.Stdin.Read(buf); err != nil {
return nil, err
}
password = strings.TrimRight(string(buf), "\r\n\x00")
}
return []byte(password), nil
}
// filterAegisVault filters an Aegis plain JSON into our internal
// representation of the vault, using "rematch" as a regular expression to
// match the issuer or account.
func filterAegisVault(plainJSON []byte, rematch *regexp.Regexp) ([]otpEntry, error) {
vault := &aegisJSON{}
if err := json.Unmarshal(plainJSON, &vault); err != nil {
return nil, err
}
ret := []otpEntry{}
for _, entry := range vault.Entries {
token := "Unknown OTP type: " + entry.Type
if entry.Type == "totp" {
token = gotp.NewDefaultTOTP(entry.Info.Secret).Now()
}
if rematch.MatchString(entry.Issuer) || rematch.MatchString(entry.Name) {
ret = append(ret, otpEntry{
Issuer: entry.Issuer,
Account: entry.Name,
Token: token,
})
}
}
return ret, nil
}
// aegisDecrypt opens an encrypted Aegis JSON export file and
// returns the plain json contents.
func aegisDecrypt(fname string, password []byte) ([]byte, error) {
buf, err := os.ReadFile(fname)
if err != nil {
return nil, err
}
encJSON := aegisEncryptedJSON{}
if err := json.Unmarshal(buf, &encJSON); err != nil {
return nil, err
}
// Extract all master key slots from header.
// Exit when a valid masterkey has been found.
var masterkey []byte
for _, slot := range encJSON.Header.Slots {
var (
nonce []byte
keyslot []byte
tag []byte
salt []byte
)
if slot.Type != 1 {
continue
}
if salt, err = hex.DecodeString(slot.Salt); err != nil {
return nil, fmt.Errorf("Slot salt: %v", err)
}
key, err := scrypt.Key(password, salt, slot.N, slot.R, slot.P, aegisKeyLen)
if err != nil {
return nil, err
}
// AES GCM decrypt.
aesgcm, err := newAES(key)
if err != nil {
return nil, err
}
if nonce, err = hex.DecodeString(slot.KeyParams.Nonce); err != nil {
return nil, fmt.Errorf("Slot nonce: %v", err)
}
if tag, err = hex.DecodeString(slot.KeyParams.Tag); err != nil {
return nil, fmt.Errorf("Slot tag: %v", err)
}
if keyslot, err = hex.DecodeString(slot.Key); err != nil {
return nil, fmt.Errorf("Slot key: %v", err)
}
// ciphertext := keyslot + tag
ciphertext := keyslot
ciphertext = append(ciphertext, tag...)
// Decrypt and break out of the loop if found. If not, try the next slot.
masterkey, err = aesgcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
// Issue a warning only, but continue.
rlog.Debug(err)
continue
}
break
}
if len(masterkey) == 0 {
return nil, errors.New("Unable to decrypt the master key with the given password")
}
// Decode DB contents.
content, err := base64.StdEncoding.DecodeString(encJSON.Db)
if err != nil {
return nil, err
}
// Decrypt the vault contents using the master key.
cipher, err := newAES(masterkey)
if err != nil {
return nil, err
}
nonce, err := hex.DecodeString(encJSON.Header.Params.Nonce)
if err != nil {
return nil, fmt.Errorf("Params nonce: %v", err)
}
tag, err := hex.DecodeString(encJSON.Header.Params.Tag)
if err != nil {
return nil, fmt.Errorf("Params tag: %v", err)
}
data := append(content, tag...)
// Decrypt and return.
db, err := cipher.Open(nil, nonce, data, nil)
if err != nil {
return nil, err
}
return db, nil
}