-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenpass2keepassxc.cr
291 lines (240 loc) · 8.57 KB
/
enpass2keepassxc.cr
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
require "base64"
require "http/params"
require "json"
require "uri"
require "xml"
module Enpass
class Export
include JSON::Serializable
property folders : Array(Folder)
property items : Array(Item)
class Folder
include JSON::Serializable
property uuid : String
property parent_uuid : String
property title : String
property icon : String
property updated_at : Int64
end
class Item
include JSON::Serializable
property uuid : String
property title : String
property auto_submit : Int32
property category : String
property favorite : Int32
property folders : Array(String)?
property fields : Array(Item::Field)?
property note : String
property subtitle : String
property template_type : String
property updated_at : Int64
class Field
include JSON::Serializable
property label : String
property type : String
property history : Array(Field::History)?
property order : Int32
property sensitive : Int32
property uid : Int32
property updated_at : Int64
property value : String
property value_updated_at : Int64
class History
include JSON::Serializable
property updated_at : Int64
property value : String
end
end
end
end
end
def uuid_str_to_b64(uuid : String)
raise "not a uuid" unless uuid.size == 36
bytes = uuid.gsub("-", "").scan(/../).map(&.[](0).to_u8(16))
io = IO::Memory.new(bytes.size)
bytes.each { |b| io.write_byte b }
Base64.encode(io).strip
end
def unix_to_iso8601(time)
Time::Format::ISO_8601_DATE_TIME.format(Time.unix(time))
end
def icon_for_category(category)
{
"license" => "17",
"note" => "44",
"creditcard" => "66",
"finance" => "66",
}.fetch(category, "0")
end
def build_entry(xml, item)
# creation time seems to be just the oldest updated_at value of any field
created_at = item.updated_at
if item.fields
created_at = item.fields.not_nil!.map(&.updated_at).min
end
xml.element("Entry") do
xml.element("UUID") { xml.text(uuid_str_to_b64(item.uuid)) }
xml.element("IconID") { xml.text(icon_for_category(item.category)) }
%w[ForegroundColor BackgroundColor OverrideURL Tags].each do |t|
xml.element(t)
end
xml.element("Times") do
xml.element("LastModificationTime") { xml.text(unix_to_iso8601(item.updated_at)) }
xml.element("CreationTime") { xml.text(unix_to_iso8601(created_at)) }
xml.element("LastAccessTime") { xml.text(unix_to_iso8601(item.updated_at)) }
xml.element("ExpiryTime") { xml.text(unix_to_iso8601(Int32::MAX)) }
xml.element("Expires") { xml.text("False") }
xml.element("UsageCount") { xml.text("0") }
xml.element("LocationChanged") { xml.text(unix_to_iso8601(item.updated_at)) }
end
xml.element("String") do
xml.element("Key") { xml.text("Title") }
xml.element("Value") { xml.text(item.title) }
end
xml.element("String") do
xml.element("Key") { xml.text("Notes") }
xml.element("Value") { xml.text(item.note) unless item.note.empty? }
end
username : String? = nil
email : String? = nil
custom_attributes_count = {} of String => Int32
if item.fields
item.fields.not_nil!.each do |field|
key = field.label
value_args = {} of String => String
value_args["ProtectInMemory"] = "True" unless field.sensitive.zero?
case field.type
when "username"
username = field.value unless field.value.empty?
next # handled below
when "email"
email = field.value unless field.value.empty?
next # handled below
when "password"
key = "Password"
when "url"
key = "URL"
when "totp"
next # handled below
else
# don't care if we don't have a value for non-mandatory fields
next if field.value.empty?
end
custom_attributes_count[key] ||= 0
custom_attributes_count[key] += 1
if custom_attributes_count[key] > 1
key = "#{key} (#{custom_attributes_count[key]})"
end
xml.element("String") do
xml.element("Key") { xml.text(key) }
xml.element("Value", value_args) { xml.text(field.value) unless field.value.empty? }
end
end
# set the UserName to the email if the username is not set
if !email.nil? && !email.not_nil!.empty?
key = "UserName"
if !username.nil? && !username.not_nil!.empty?
xml.element("String") do
xml.element("Key") { xml.text("UserName") }
xml.element("Value") { xml.text(username) }
end
key = "E-Mail"
end
xml.element("String") do
xml.element("Key") { xml.text(key) }
xml.element("Value") { xml.text(email) }
end
else
xml.element("String") do
xml.element("Key") { xml.text("UserName") }
xml.element("Value") { xml.text(username) if !username.nil? && !username.not_nil!.empty? }
end
end
totp_fields = item.fields.not_nil!.select { |f| f.type == "totp" && !f.value.empty? }
unless totp_fields.size.zero?
# get the most recent TOTP value
totp_value = totp_fields.sort_by(&.value_updated_at).last.value
# normalise it
totp_value = totp_value.gsub(" ", "").upcase
totp_params = HTTP::Params.encode({"secret" => totp_value,
"period" => "30",
"digits" => "6",
"issuer" => item.title})
totp_uri = URI.new(
scheme: "otpauth",
host: "totp",
path: URI.encode_path([item.title, "someone"].join(":")), # here's hoping keepassxc doesn't care about the username in here
query: totp_params
).to_s
xml.element("String") do
xml.element("Key") { xml.text("otp") }
xml.element("Value", {"ProtectInMemory" => "True"}) { xml.text(totp_uri) }
end
end
end
xml.element("AutoType") do
xml.element("Enabled") { xml.text("True") }
xml.element("DataTransferObfuscation") { xml.text("0") }
xml.element("DefaultSequence")
end
xml.element("History")
end
end
def convert_enpass_export(enpass)
entries_by_group = enpass.items.group_by(&.folders)
root_uuid = "5ca246df-de73-47fe-b4a9-28ae68817f78"
watch_uuid = "d275de30-d63b-4a07-a3ca-1be78047ba14"
XML.build(indent: " ") do |xml|
xml.element("KeePassFile") do
xml.element("Meta") do
xml.element("Generator") { xml.text("Enpass2KeepassXC") }
xml.element("MemoryProtection") do
xml.element("ProtectTitle") { xml.text("False") }
xml.element("ProtectUserName") { xml.text("False") }
xml.element("ProtectPassword") { xml.text("True") }
xml.element("ProtectURL") { xml.text("False") }
xml.element("ProtectNotes") { xml.text("False") }
end
end
xml.element("Root") do
xml.element("Group") do
xml.element("UUID") { xml.text(uuid_str_to_b64(root_uuid)) }
xml.element("Name") { xml.text("Root") }
xml.element("Notes")
xml.element("IconID") { xml.text("48") }
xml.element("IsExpanded") { xml.text("True") }
entries_by_group[nil].each do |item|
build_entry(xml, item)
end
entries_by_group.each do |group, items|
next unless group
folder_uuid = group.first
folder_metadata = enpass.folders.find { |f| f.uuid == folder_uuid }
next unless folder_metadata
folder_metadata = folder_metadata.not_nil!
folder_uuid = watch_uuid if folder_uuid == "watch-folder-uuid"
xml.element("Group") do
xml.element("UUID") { xml.text(uuid_str_to_b64(folder_uuid)) }
xml.element("Name") { xml.text(folder_metadata.title) }
xml.element("Notes")
xml.element("IconID") { xml.text("48") }
xml.element("IsExpanded") { xml.text("True") }
items.each do |item|
build_entry(xml, item)
end
end
end
end
end
end
end
end
def main(argv)
abort "usage: enpass2keepassxc JSON_EXPORT" if argv.size != 1
enpassfile = argv.shift
enpass_export = Enpass::Export.from_json(File.read(enpassfile))
document = convert_enpass_export(enpass_export)
puts document
end
main ARGV.dup