Skip to content

Latest commit

 

History

History
432 lines (358 loc) · 13.4 KB

1pass.lua

File metadata and controls

432 lines (358 loc) · 13.4 KB
 
Dec 18, 2013
Dec 18, 2013
1
JSON = (loadfile "JSON.lua")()
Dec 23, 2013
Dec 23, 2013
2
3
dofile("dumptable.lua")
Dec 24, 2013
Dec 24, 2013
4
5
6
local basedir = "1Password/1Password.agilekeychain/data/default" -- !!! FIXME
local password = argv[2]
local items = nil
Apr 26, 2014
Apr 26, 2014
7
local faveitems = nil
Dec 24, 2013
Dec 24, 2013
8
9
local keyhookRunning = false
Dec 23, 2013
Dec 23, 2013
10
11
12
13
14
15
16
17
local passwordTypeNameMap = {
["webforms.WebForm"] = "Logins",
["wallet.financial.CreditCard"] = "Credit cards",
["passwords.Password"] = "Passwords",
["wallet.financial.BankAccountUS"] = "Bank accounts",
["wallet.membership.Membership"] = "Memberships",
["wallet.government.DriversLicense"] = "Drivers licenses",
["system.Tombstone"] = "Dead items",
Feb 11, 2014
Feb 11, 2014
18
["securenotes.SecureNote"] = "Secure notes",
Dec 23, 2013
Dec 23, 2013
19
20
21
22
23
24
25
26
27
28
-- !!! FIXME: more!
}
local passwordTypeOrdering = {
"webforms.WebForm",
"wallet.financial.CreditCard",
"passwords.Password",
"wallet.financial.BankAccountUS",
"wallet.membership.Membership",
"wallet.government.DriversLicense",
Feb 11, 2014
Feb 11, 2014
29
"securenotes.SecureNote",
Dec 23, 2013
Dec 23, 2013
30
31
32
-- never show "system.Tombstone",
-- !!! FIXME: more!
}
Dec 18, 2013
Dec 18, 2013
33
Dec 18, 2013
Dec 18, 2013
34
35
36
37
38
local function load_json_str(str, desc)
local retval = JSON:decode(str)
return retval
end
Dec 18, 2013
Dec 18, 2013
39
40
41
42
43
44
45
46
47
local function load_json(fname)
local f = io.open(fname, "rb")
if (f == nil) then
return nil
end
local str = f:read("*all")
f:close()
Dec 18, 2013
Dec 18, 2013
48
return load_json_str(str, fname)
Dec 18, 2013
Dec 18, 2013
49
50
51
end
Dec 18, 2013
Dec 18, 2013
52
local keys = {}
Dec 24, 2013
Dec 24, 2013
53
local function loadKey(level, password)
Dec 18, 2013
Dec 18, 2013
54
55
56
57
if keys[level] ~= nil then
return keys[level]
end
Dec 23, 2013
Dec 23, 2013
58
local keysjson = load_json(basedir .. "/encryptionKeys.js")
Dec 18, 2013
Dec 18, 2013
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
if (keysjson == nil) or (keysjson[level] == nil) then
return nil
end
local identifier = keysjson[level]
for i,v in ipairs(keysjson.list) do
if v.identifier == identifier then
local iterations = v.iterations
if (iterations == nil) or (iterations < 1000) then
iterations = 1000
end
local decrypted = decryptUsingPBKDF2(v.data, password, iterations)
if decrypted == nil then
return nil
end
local validate = decryptBase64UsingKey(v.validation, decrypted)
if validate ~= decrypted then
return nil
end
Dec 18, 2013
Dec 18, 2013
81
keys[level] = decrypted
Dec 18, 2013
Dec 18, 2013
82
83
84
85
86
87
88
return decrypted
end
end
return nil
end
Dec 24, 2013
Dec 24, 2013
89
local function getHint()
Dec 18, 2013
Dec 18, 2013
90
91
92
93
94
local f = io.open(basedir .. "/.password.hint", "r")
if (f == nil) then
return
end
Dec 23, 2013
Dec 23, 2013
95
local str = "(hint is '" .. f:read("*all") .. "')."
Dec 18, 2013
Dec 18, 2013
96
f:close()
Dec 23, 2013
Dec 23, 2013
97
98
--print(str)
return str
Dec 18, 2013
Dec 18, 2013
99
100
end
Dec 18, 2013
Dec 18, 2013
101
Dec 24, 2013
Dec 24, 2013
102
local function loadContents()
Dec 23, 2013
Dec 23, 2013
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
return load_json(basedir .. "/contents.js")
end
local function build_secret_menuitem(menu, type, str, hidden)
if str == nil then
return nil
end
local valuestr = str
if hidden == true then
valuestr = "*****"
end
local text = type .. " " .. valuestr
local callback = function()
copyToClipboard(str)
--print("Copied data [" .. str .. "] to clipboard.")
Dec 24, 2013
Dec 24, 2013
120
keyhookRunning = false
Dec 23, 2013
Dec 23, 2013
121
122
end
return appendGuiMenuItem(menu, text, callback)
Dec 18, 2013
Dec 18, 2013
123
124
end
Dec 23, 2013
Dec 23, 2013
125
126
127
128
129
130
131
local secret_menuitem_builders = {}
local function build_secret_menuitem_webform(menu, info, secure)
local addthis = false
local username = nil
local password = nil
Apr 19, 2014
Apr 19, 2014
132
133
local designated_password = nil
local designated_username = nil
Dec 23, 2013
Dec 23, 2013
134
local email = nil
Jan 19, 2014
Jan 19, 2014
135
Feb 11, 2014
Feb 11, 2014
136
137
138
139
140
if secure.fields == nil then
print("no secure fields, don't know how to handle this item")
return
end
Dec 23, 2013
Dec 23, 2013
141
142
143
for i,v in ipairs(secure.fields) do
--print(info.name .. ": " .. v.type .. ", " .. v.value)
local ignored = false
Apr 19, 2014
Apr 19, 2014
144
145
146
147
148
149
150
if (v.value == nil) or (v.value == "") then
ignored = true
elseif (v.designation ~= nil) and (v.designation == "password") then
designated_password = v.value
elseif (v.designation ~= nil) and (v.designation == "username") then
designated_username = v.value
elseif (v.type == "P") then
Dec 23, 2013
Dec 23, 2013
151
password = v.value
Apr 19, 2014
Apr 19, 2014
152
elseif (v.type == "T") then
Dec 23, 2013
Dec 23, 2013
153
username = v.value
Apr 19, 2014
Apr 19, 2014
154
elseif (v.type == "E") then
Dec 23, 2013
Dec 23, 2013
155
156
157
158
159
160
161
162
163
164
165
email = v.value
else
ignored = true
end
if not ignored then
addthis = true
end
end
if addthis then
Apr 19, 2014
Apr 19, 2014
166
167
168
169
170
171
172
173
174
-- designated fields always win out.
if (designated_username ~= nil) then
username = designated_username
end
if (designated_password ~= nil) then
password = designated_password
end
Dec 23, 2013
Dec 23, 2013
175
176
177
178
179
180
181
if (username ~= nil) and (email ~= nil) and (email == username) then
email = nil
end
build_secret_menuitem(menu, "username", username)
build_secret_menuitem(menu, "email", email)
build_secret_menuitem(menu, "password", password, true)
Dec 19, 2013
Dec 19, 2013
182
end
Dec 23, 2013
Dec 23, 2013
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
end
secret_menuitem_builders["webforms.WebForm"] = build_secret_menuitem_webform
local function build_secret_menuitem_password(menu, info, secure)
build_secret_menuitem(menu, "password", secure.password, true)
end
secret_menuitem_builders["passwords.Password"] = build_secret_menuitem_password
local function build_secret_menuitem_bankacctus(menu, info, secure)
-- !!! FIXME: there's more data than this in a generic dictionary.
build_secret_menuitem(menu, "Account type", secure.accountType)
build_secret_menuitem(menu, "Routing number", secure.routingNo)
build_secret_menuitem(menu, "Account number", secure.accountNo)
build_secret_menuitem(menu, "Bank name", secure.bankName)
build_secret_menuitem(menu, "Owner", secure.owner)
end
secret_menuitem_builders["wallet.financial.BankAccountUS"] = build_secret_menuitem_bankacctus
local function build_secret_menuitem_driverslic(menu, info, secure)
-- !!! FIXME: there's more data than this in a generic dictionary.
local birthdate = secure.birthdate_yy .. "/" .. string.sub("00" .. secure.birthdate_mm, -2) .. "/" .. string.sub("00" .. secure.birthdate_dd, -2)
local expiredate = secure.expiry_date_yy .. "/" .. string.sub("00" .. secure.expiry_date_mm, -2)
build_secret_menuitem(menu, "License number", secure.number)
build_secret_menuitem(menu, "Class", secure.class)
build_secret_menuitem(menu, "Expires", expiredate)
build_secret_menuitem(menu, "State", secure.state)
build_secret_menuitem(menu, "Country", secure.country)
build_secret_menuitem(menu, "Conditions", secure.conditions)
build_secret_menuitem(menu, "Full name", secure.fullname)
build_secret_menuitem(menu, "Address", secure.address)
build_secret_menuitem(menu, "Gender", secure.sex)
build_secret_menuitem(menu, "Birthdate", birthdate)
build_secret_menuitem(menu, "Height", secure.height)
end
secret_menuitem_builders["wallet.government.DriversLicense"] = build_secret_menuitem_driverslic
local function build_secret_menuitem_membership(menu, info, secure)
-- !!! FIXME: there's more data than this in a generic dictionary.
build_secret_menuitem(menu, "Membership number", secure.membership_no)
end
secret_menuitem_builders["wallet.membership.Membership"] = build_secret_menuitem_membership
local function build_secret_menuitem_creditcard(menu, info, secure)
-- !!! FIXME: there's more data than this in a generic dictionary.
local expiredate = secure.expiry_yy .. "/" .. string.sub("00" .. secure.expiry_mm, -2)
build_secret_menuitem(menu, "Type", secure.type)
build_secret_menuitem(menu, "CC number", secure.ccnum, true)
build_secret_menuitem(menu, "CVV", secure.cvv, true)
May 1, 2014
May 1, 2014
236
build_secret_menuitem(menu, "Expires", expiredate)
Dec 23, 2013
Dec 23, 2013
237
238
239
240
241
build_secret_menuitem(menu, "Card holder", secure.cardholder)
build_secret_menuitem(menu, "Bank", secure.bank)
end
secret_menuitem_builders["wallet.financial.CreditCard"] = build_secret_menuitem_creditcard
Feb 11, 2014
Feb 11, 2014
242
243
244
245
local function build_secret_menuitem_securenote(menu, info, secure)
build_secret_menuitem(menu, "Notes", secure.notesPlain, true)
end
secret_menuitem_builders["securenotes.SecureNote"] = build_secret_menuitem_securenote
Dec 23, 2013
Dec 23, 2013
246
Dec 24, 2013
Dec 24, 2013
247
local function build_secret_menuitems(info, menu)
Dec 23, 2013
Dec 23, 2013
248
local metadata = load_json(basedir .. "/" .. info.uuid .. ".1password")
Jul 9, 2014
Jul 9, 2014
249
if (metadata == nil) or (next(metadata) == nil) then -- the "next" trick tests if table is empty.
Dec 23, 2013
Dec 23, 2013
250
251
252
return
end
Jan 19, 2014
Jan 19, 2014
253
254
local securityLevel = metadata.securityLevel
if securityLevel == nil then
Apr 26, 2014
Apr 26, 2014
255
securityLevel = metadata.openContents.securityLevel
Jan 19, 2014
Jan 19, 2014
256
end
Apr 15, 2014
Apr 15, 2014
257
--print("title: " .. metadata.title)
Jan 19, 2014
Jan 19, 2014
258
259
260
261
262
263
if securityLevel == nil then
--print("can't find security level, assuming SL5" .. metadata.title)
securityLevel = "SL5"
end
local plaintext = decryptBase64UsingKey(metadata.encrypted, loadKey(securityLevel, password))
Dec 23, 2013
Dec 23, 2013
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
if plaintext == nil then
return
end
local secure = load_json_str(plaintext, info.uuid)
if secure == nil then
return
end
--dumptable("secure " .. info.name, secure)
local menuitem = appendGuiMenuItem(menu, info.name)
if secret_menuitem_builders[info.type] == nil then
print("WARNING: don't know how to handle items of type " .. info.type)
dumptable("secure " .. info.type .. " (" .. info.name .. ")", secure)
return
end
Apr 26, 2014
Apr 26, 2014
282
283
284
285
286
if metadata.faveIndex ~= nil then
--dumptable("fave metadata " .. info.name, metadata)
faveitems[metadata.faveIndex] = { info=info, secure=secure }
end
Dec 23, 2013
Dec 23, 2013
287
288
289
local submenu = makeGuiMenu()
secret_menuitem_builders[info.type](submenu, info, secure)
setGuiMenuItemSubmenu(menuitem, submenu)
Dec 19, 2013
Dec 19, 2013
290
291
end
Dec 24, 2013
Dec 24, 2013
292
293
294
295
296
297
298
local function prepItems()
items = {}
local contents = loadContents()
for i,v in ipairs(contents) do
local t = v[2]
if items[t] == nil then
items[t] = {}
Dec 23, 2013
Dec 23, 2013
299
end
Dec 24, 2013
Dec 24, 2013
300
301
local bucket = items[t]
bucket[#bucket+1] = { uuid=v[1], type=t, name=v[3], url=v[4] } -- !!! FIXME: there are more fields, don't know what they mean yet.
Dec 23, 2013
Dec 23, 2013
302
303
end
end
Dec 22, 2013
Dec 22, 2013
304
Dec 24, 2013
Dec 24, 2013
305
306
local passwordUnlockTime = nil
Apr 15, 2014
Apr 15, 2014
307
308
309
310
311
312
313
314
315
local function lockKeychain()
-- lose the existing password and key, prompt user again.
password = argv[2] -- might be nil, don't reset if on command line.
keys["SL5"] = nil
passwordUnlockTime = nil
keyhookRunning = false
setPowermateLED(false)
collectgarbage()
end
Dec 24, 2013
Dec 24, 2013
316
Apr 15, 2014
Apr 15, 2014
317
function pumpLua() -- not local! Called from C!
Apr 15, 2014
Apr 15, 2014
318
319
320
-- !!! FIXME: this should lose the key in RAM and turn off the Powermate
-- !!! FIXME: LED when the time expires instead of if the time has
-- !!! FIXME: expired when the user is trying to get at the keychain.
Dec 24, 2013
Dec 24, 2013
321
322
323
324
if passwordUnlockTime ~= nil then
local now = os.time()
local maxTime = (15 * 60) -- !!! FIXME: don't hardcode.
if os.difftime(now, passwordUnlockTime) > maxTime then
Apr 15, 2014
Apr 15, 2014
325
lockKeychain()
Dec 24, 2013
Dec 24, 2013
326
327
end
end
Apr 15, 2014
Apr 15, 2014
328
329
330
331
332
333
334
335
336
337
end
function keyhookPressed() -- not local! Called from C!
--print("keyhookPressed: running==" .. tostring(keyhookRunning))
-- if keyhookRunning then
-- return
-- end
keyhookRunning = true
Dec 24, 2013
Dec 24, 2013
338
Dec 24, 2013
Dec 24, 2013
339
340
341
342
343
344
345
346
347
348
349
350
351
while password == nil do
password = runGuiPasswordPrompt(getHint())
if password == nil then
keyhookRunning = false
return
end
if loadKey("SL5", password) == nil then
password = nil -- wrong password
local start = os.time() -- cook the CPU for three seconds.
local now = start
while os.difftime(now, start) < 3 do
now = os.time()
end
Dec 24, 2013
Dec 24, 2013
352
353
else
passwordUnlockTime = os.time()
Apr 15, 2014
Apr 15, 2014
354
setPowermateLED(true)
Dec 24, 2013
Dec 24, 2013
355
end
Dec 22, 2013
Dec 22, 2013
356
end
Dec 24, 2013
Dec 24, 2013
357
358
359
360
prepItems()
local topmenu = makeGuiMenu()
Apr 26, 2014
Apr 26, 2014
361
362
363
364
local favesmenu = makeGuiMenu()
faveitems = {}
setGuiMenuItemSubmenu(appendGuiMenuItem(topmenu, "Favorites"), favesmenu)
Dec 24, 2013
Dec 24, 2013
365
Apr 15, 2014
Apr 15, 2014
366
appendGuiMenuItem(topmenu, "Lock keychain", function() lockKeychain() end)
Dec 24, 2013
Dec 24, 2013
367
Dec 24, 2013
Dec 24, 2013
368
369
for orderi,type in ipairs(passwordTypeOrdering) do
local bucket = items[type]
Jan 19, 2014
Jan 19, 2014
370
371
372
373
374
375
376
377
378
379
380
381
382
if bucket ~= nil then
local realname = passwordTypeNameMap[type]
if realname == nil then
realname = type
end
local menuitem = appendGuiMenuItem(topmenu, realname)
local submenu = makeGuiMenu()
table.sort(bucket, function(a, b) return a.name < b.name end)
for i,v in pairs(bucket) do
build_secret_menuitems(v, submenu)
end
setGuiMenuItemSubmenu(menuitem, submenu)
else
Apr 26, 2014
Apr 26, 2014
383
--print("no bucket found for item type '" .. type .. "'")
Dec 24, 2013
Dec 24, 2013
384
end
Dec 22, 2013
Dec 22, 2013
385
end
Apr 26, 2014
Apr 26, 2014
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
-- This favepairs stuff is obnoxious.
local function favepairs(t)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a)
local i = 0
local iter = function()
i = i + 1
if a[i] == nil then
return nil
else
return a[i], t[a[i]]
end
end
return iter
end
for i,v in favepairs(faveitems) do
--dumptable("fave " .. i, v)
local menuitem = appendGuiMenuItem(favesmenu, v.info.name)
local submenu = makeGuiMenu()
secret_menuitem_builders[v.info.type](submenu, v.info, v.secure)
setGuiMenuItemSubmenu(menuitem, submenu)
end
favepairs = nil
faveitems = nil
Dec 24, 2013
Dec 24, 2013
414
415
popupGuiMenu(topmenu)
Dec 22, 2013
Dec 22, 2013
416
417
end
Dec 24, 2013
Dec 24, 2013
418
419
420
421
422
423
424
425
426
-- Mainline!
--for i,v in ipairs(argv) do
-- print("argv[" .. i .. "] = " .. v)
--end
-- !!! FIXME: message box, exit if basedir is wack.
-- !!! FIXME: this can probably happen in C now (the Lua mainline is basically gone now).
Apr 15, 2014
Apr 15, 2014
427
setPowermateLED(false) -- off by default
Feb 11, 2014
Feb 11, 2014
428
print("Now waiting for the magic key combo (probably Alt-Meta-\\) ...")
Dec 22, 2013
Dec 22, 2013
429
430
giveControlToGui()
Dec 18, 2013
Dec 18, 2013
431
-- end of 1pass.lua ...