1pass.lua
author Ryan C. Gordon <icculus@icculus.org>
Wed, 18 Dec 2013 00:24:01 -0500
changeset 6 b7f19e0599db
parent 5 6f80cd157c13
child 7 682d7ea1e7f3
permissions -rw-r--r--
Dump out all the encrypted data.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
JSON = (loadfile "JSON.lua")()
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
local function dumptable(tabname, tab, depth)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
    if depth == nil then  -- first call, before any recursion?
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
        depth = 1
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
    end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
    if tabname ~= nil then
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
        if tab == nil then
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
            print(tabname .. " = nil")
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
            return
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
        else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
            print(tabname .. " = {")
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
        end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
    end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
    local depthstr = ""
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
    for i=1,(depth*4) do
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
        depthstr = depthstr .. " "
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
    end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
    if tab.DUMPTABLE_ITERATED then
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
        print(depthstr .. "(...circular reference...)")
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
    else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
        tab.DUMPTABLE_ITERATED = true
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
        for k,v in pairs(tab) do
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
            if type(v) == "table" then
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
                print(depthstr .. tostring(k) .. " = {")
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
                dumptable(nil, v, depth + 1)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
                print(depthstr .. "}")
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
            else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
                if k ~= "DUMPTABLE_ITERATED" then
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
                    print(depthstr .. tostring(k) .. " = " .. tostring(v))
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
                end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
            end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
        end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
        tab.DUMPTABLE_ITERATED = nil
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
    end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
    if tabname ~= nil then
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
        print("}")
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
    end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
6
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
    45
local function load_json_str(str, desc)
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
    46
    local retval = JSON:decode(str)
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
    47
    dumptable("JSON " .. desc, retval)
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
    48
    return retval
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
    49
end
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
    50
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
local function load_json(fname)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    local f = io.open(fname, "rb")
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    if (f == nil) then
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
        return nil
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
    end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    local str = f:read("*all")
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
    f:close()
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
6
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
    60
    return load_json_str(str, fname)
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
5
6f80cd157c13 Cache loaded encryption keys.
Ryan C. Gordon <icculus@icculus.org>
parents: 1
diff changeset
    64
local keys = {}
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
function loadKey(basedir, level, password)
5
6f80cd157c13 Cache loaded encryption keys.
Ryan C. Gordon <icculus@icculus.org>
parents: 1
diff changeset
    66
    if keys[level] ~= nil then
6f80cd157c13 Cache loaded encryption keys.
Ryan C. Gordon <icculus@icculus.org>
parents: 1
diff changeset
    67
        return keys[level]
6f80cd157c13 Cache loaded encryption keys.
Ryan C. Gordon <icculus@icculus.org>
parents: 1
diff changeset
    68
    end
6f80cd157c13 Cache loaded encryption keys.
Ryan C. Gordon <icculus@icculus.org>
parents: 1
diff changeset
    69
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    local keysjson = load_json(basedir .. "/encryptionKeys.js");
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
    if (keysjson == nil) or (keysjson[level] == nil) then
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
        return nil
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
    end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    local identifier = keysjson[level]
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
    for i,v in ipairs(keysjson.list) do
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
        if v.identifier == identifier then
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
			local iterations = v.iterations
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
            if (iterations == nil) or (iterations < 1000) then
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
			    iterations = 1000
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
            end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
			local decrypted = decryptUsingPBKDF2(v.data, password, iterations)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
			if decrypted == nil then
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
                return nil
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
            end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
			local validate = decryptBase64UsingKey(v.validation, decrypted)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
			if validate ~= decrypted then
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
                return nil
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
            end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
5
6f80cd157c13 Cache loaded encryption keys.
Ryan C. Gordon <icculus@icculus.org>
parents: 1
diff changeset
    93
            keys[level] = decrypted
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
            return decrypted
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
        end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
    end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
    return nil
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
1
0919d17b13f9 Move the mainline into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   101
local function showHint(basedir)
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
    local f = io.open(basedir .. "/.password.hint", "r")
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
    if (f == nil) then
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
        return
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
    end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
    local str = f:read("*all")
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
    f:close()
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
    print("(hint is '" .. str .. "').")
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
end
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
1
0919d17b13f9 Move the mainline into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   113
6
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   114
function loadContents(basedir)
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   115
    return load_json(basedir .. "/contents.js");
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   116
end
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   117
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   118
1
0919d17b13f9 Move the mainline into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   119
-- Mainline!
0919d17b13f9 Move the mainline into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   120
0919d17b13f9 Move the mainline into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   121
local basedir = "1Password/1Password.agilekeychain/data/default"  -- !!! FIXME
0919d17b13f9 Move the mainline into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   122
0919d17b13f9 Move the mainline into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   123
showHint(basedir)
0919d17b13f9 Move the mainline into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   124
io.write("password: ")
0919d17b13f9 Move the mainline into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   125
local password = io.read("*l")
0919d17b13f9 Move the mainline into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   126
5
6f80cd157c13 Cache loaded encryption keys.
Ryan C. Gordon <icculus@icculus.org>
parents: 1
diff changeset
   127
if loadKey(basedir, "SL5", password) == nil then
1
0919d17b13f9 Move the mainline into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   128
    print("wrong password?\n")
0919d17b13f9 Move the mainline into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   129
    os.exit(1)
0919d17b13f9 Move the mainline into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   130
end
0919d17b13f9 Move the mainline into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   131
6
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   132
items = loadContents(basedir)
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   133
for i,v in ipairs(items) do
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   134
    if v[2] ~= "system.Tombstone" then  -- I guess those are dead items?
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   135
        local metadata = load_json(basedir .. "/" .. v[1] .. ".1password")
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   136
        if metadata ~= nil then
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   137
            local plaintext = decryptBase64UsingKey(metadata.encrypted, loadKey(basedir, metadata.securityLevel, password))
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   138
            if plaintext ~= nil then
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   139
                local secure = load_json_str(plaintext, v[1])
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   140
            end
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   141
        end
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   142
    end
b7f19e0599db Dump out all the encrypted data.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   143
end
1
0919d17b13f9 Move the mainline into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   144
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
-- end of 1pass.lua ...
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146