-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdb.go
303 lines (280 loc) · 7.67 KB
/
db.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
package main
import (
"fmt"
"github.com/tobischo/gokeepasslib/v3"
"github.com/tobischo/gokeepasslib/v3/wrappers"
"maps"
"net/url"
"os"
"strconv"
)
type OtpStyle int
const (
KeeTrayTotp OtpStyle = iota
KeePass2
KeeWebOtp
)
type db struct {
Version int
Entries []entry
Groups []group
}
type entry struct {
Type string
UUID string `json:"uuid"`
Name string
Issuer string
Note string
Favorite bool
Icon string
IconMime string `json:"icon_mime"`
Info dbInfo
Groups []string
}
type dbInfo struct {
Secret string
Algo string
Digits int
Period int
Counter int
}
type group struct {
UUID string `json:"uuid"`
Name string
}
func incompatibleFormatError(entry, format, style string) error {
return fmt.Errorf("Entry \"%s\" uses %s format, which is not supported for %s-style OTPs", entry, format, style)
}
func (e entry) GetKeeTrayFormat() ([]gokeepasslib.ValueData, error) {
var content string
if e.Type == "totp" {
content = fmt.Sprintf("%d;%d", e.Info.Period, e.Info.Digits)
} else if e.Type == "steam" {
content = fmt.Sprintf("%d;S", e.Info.Period)
} else if e.Type == "hotp" {
return nil, incompatibleFormatError(e.Name, "HOTP", "KeePassXC/KeeTray")
} else {
return nil, fmt.Errorf("Unknown type for entry \"%s\": %s", e.Name, e.Type)
}
return []gokeepasslib.ValueData{
{
Key: "TOTP Settings",
Value: gokeepasslib.V{
Content: content,
Protected: wrappers.BoolWrapper{Bool: true},
},
},
{
Key: "TOTP Seed",
Value: gokeepasslib.V{
Content: e.Info.Secret,
Protected: wrappers.BoolWrapper{Bool: true},
},
}}, nil
}
func (e entry) GetKeePass2TotpFormat() ([]gokeepasslib.ValueData, error) {
var algo string
switch e.Info.Algo {
case "SHA1":
algo = "HMAC-SHA-1"
case "SHA256":
algo = "HMAC-SHA-256"
case "SHA512":
algo = "HMAC-SHA-512"
default:
return nil, fmt.Errorf("Unknown algorithm for entry \"%s\": %s", e.Name, e.Info.Algo)
}
return []gokeepasslib.ValueData{
{
Key: "TimeOtp-Secret-Base32",
Value: gokeepasslib.V{
Content: e.Info.Secret,
Protected: wrappers.BoolWrapper{Bool: true},
},
},
{
Key: "TimeOtp-Period",
Value: gokeepasslib.V{
Content: strconv.Itoa(e.Info.Period),
Protected: wrappers.BoolWrapper{Bool: true},
},
},
{
Key: "TimeOtp-Length",
Value: gokeepasslib.V{
Content: strconv.Itoa(e.Info.Digits),
Protected: wrappers.BoolWrapper{Bool: true},
},
},
{
Key: "TimeOtp-Algorithm",
Value: gokeepasslib.V{
Content: algo,
Protected: wrappers.BoolWrapper{Bool: true},
},
},
}, nil
}
func (e entry) GetKeePass2HotpFormat() ([]gokeepasslib.ValueData, error) {
return []gokeepasslib.ValueData{
{
Key: "HmacOtp-Secret-Base32",
Value: gokeepasslib.V{
Content: e.Info.Secret,
Protected: wrappers.BoolWrapper{Bool: true},
},
},
{
Key: "HmacOtp-Counter",
Value: gokeepasslib.V{
Content: strconv.Itoa(e.Info.Counter),
Protected: wrappers.BoolWrapper{Bool: true},
},
},
}, nil
}
func (e entry) GetKeeWebFormat() ([]gokeepasslib.ValueData, error) {
issuer := url.QueryEscape(e.Issuer)
label := url.QueryEscape(e.Name)
var content string
switch e.Type {
case "steam":
// https://github.com/keeweb/keeweb/issues/564#issuecomment-535221800
// kinda annoying since other clients seem to expect otpauth://steam
fmt.Printf("Setting \"otp\" value for \"%s\" entry to otpauth://totp, depending on your client you may need to change it manually to otpauth://steam\n", e.Name)
content = fmt.Sprintf("otpauth://totp/%s:%s?issuer=%s&secret=%s", label, issuer, issuer, e.Info.Secret)
case "totp":
content = fmt.Sprintf("otpauth://%s/%s:%s?issuer=%s&secret=%s&algorithm=%s&digits=%d&period=%d", e.Type, label, issuer, issuer, e.Info.Secret, e.Info.Algo, e.Info.Digits, e.Info.Period)
case "hotp":
content = fmt.Sprintf("otpauth://%s/%s:%s?issuer=%s&secret=%s&counter=%d", e.Type, label, issuer, issuer, e.Info.Secret, e.Info.Counter)
default:
return nil, fmt.Errorf("Unknown algorithm for entry \"%s\": %s", e.Name, e.Info.Algo)
}
return []gokeepasslib.ValueData{{
Key: "otp",
Value: gokeepasslib.V{
Content: content,
Protected: wrappers.BoolWrapper{Bool: true},
},
}}, nil
}
func (e entry) ToKeePassEntry(style OtpStyle) (gokeepasslib.Entry, error) {
convertedEntry := gokeepasslib.NewEntry()
values := []gokeepasslib.ValueData{
{
Key: "Title",
Value: gokeepasslib.V{
Content: e.Issuer,
},
},
{
Key: "UserName",
Value: gokeepasslib.V{
Content: e.Name,
},
},
{
Key: "Notes",
Value: gokeepasslib.V{
Content: e.Note,
},
},
}
switch style {
case KeeTrayTotp:
convertedEntry.AutoType.DefaultSequence = "{TOTP}"
keetrayValues, err := e.GetKeeTrayFormat()
if err != nil {
return convertedEntry, err
}
values = append(values, keetrayValues...)
case KeePass2:
var keepass2Values []gokeepasslib.ValueData
var err error
switch e.Type {
case "totp":
convertedEntry.AutoType.DefaultSequence = "{TIMEOTP}"
keepass2Values, err = e.GetKeePass2TotpFormat()
case "hotp":
convertedEntry.AutoType.DefaultSequence = "{HMACOTP}"
keepass2Values, err = e.GetKeePass2HotpFormat()
case "steam":
return convertedEntry, incompatibleFormatError(e.Name, "Steam", "KeePass2")
default:
return convertedEntry, fmt.Errorf("Unknown type for entry \"%s\": %s", e.Name, e.Type)
}
if err != nil {
return convertedEntry, err
}
values = append(values, keepass2Values...)
case KeeWebOtp:
convertedEntry.AutoType.DefaultSequence = "{TOTP}"
keeWebValues, err := e.GetKeeWebFormat()
if err != nil {
return convertedEntry, err
}
values = append(values, keeWebValues...)
}
convertedEntry.Values = values
convertedEntry.AutoType.Enabled = wrappers.BoolWrapper{Bool: true}
return convertedEntry, nil
}
func (d db) ToKeePass(path string, password []byte, style OtpStyle) error {
file, err := os.Create(path)
if err != nil {
return fmt.Errorf("failed to create file for KeePass database: %v", err)
}
defer file.Close()
db := gokeepasslib.NewDatabase(gokeepasslib.WithDatabaseKDBXVersion4())
db.Content.Meta.DatabaseName = "TOTP"
db.Credentials = gokeepasslib.NewPasswordCredentials(string(password))
rootGroup := gokeepasslib.NewGroup()
rootGroup.Name = "Default"
groups := make(map[string]*gokeepasslib.Group)
for _, group := range d.Groups {
keepassGroup := gokeepasslib.NewGroup()
keepassGroup.Name = group.Name
groups[group.UUID] = &keepassGroup
}
for _, entry := range d.Entries {
converted, err := entry.ToKeePassEntry(style)
if err != nil {
fmt.Println(err)
continue
}
if entry.Icon != "" {
iconUUID := gokeepasslib.NewUUID()
db.Content.Meta.CustomIcons = append(db.Content.Meta.CustomIcons, gokeepasslib.CustomIcon{
UUID: iconUUID,
Data: entry.Icon,
})
converted.CustomIconUUID = iconUUID
}
if len(entry.Groups) == 0 {
rootGroup.Entries = append(rootGroup.Entries, converted)
continue
}
for _, groupUUID := range entry.Groups {
keepassGroup, ok := groups[groupUUID]
if !ok {
fmt.Printf("Entry \"%s\" is listed as part of a group %s, but no such group was found\n", entry.Name, groupUUID)
continue
}
keepassGroup.Entries = append(keepassGroup.Entries, converted)
}
}
for groupPointer := range maps.Values(groups) {
rootGroup.Groups = append(rootGroup.Groups, *groupPointer)
}
db.Content.Root.Groups = []gokeepasslib.Group{rootGroup}
err = db.LockProtectedEntries()
if err != nil {
return fmt.Errorf("failed to lock entries when saving KeePass database: %v", err)
}
keepassEncoder := gokeepasslib.NewEncoder(file)
err = keepassEncoder.Encode(db)
if err != nil {
return fmt.Errorf("failed to save KeePass database: %v", err)
}
return nil
}