-
Notifications
You must be signed in to change notification settings - Fork 0
/
keepass.js
350 lines (312 loc) · 10.9 KB
/
keepass.js
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
var fs = require('fs'),
constants = require('constants'),
Struct = require('./struct.js').Struct,
crypto = require('./crypto.js').Crypto,
sax = require('sax'),
salsa20 = require('./salsa20').salsa20,
JXG = require('./jsxcompressor').JXG;
function toHex(a) {
if (a instanceof Array) {
var result = '';
for (var i = 0; i < a.length; i++) {
result += toHex(a[i]);
}
return result;
} else if (typeof a === 'number') {
if (a > 255 || a < 0) { throw new Error('Invalid input, toHex received a non-byte'); }
return (a < 16 ? '0' : '') + a.toString(16);
} else {
throw new Error('Unknown type, not able to convert to hex: ' + typeof a);
}
}
function BufferToBytes(b) {
return Array.prototype.slice.call(b, 0);
}
function UTF8ToBytes(s) {
var bytes = [];
for(var i=0;i<s.length;i++) {
bytes.push(s.charCodeAt(i));
}
return bytes;
}
function BytesToUTF8(bytes) {
var s = '';
for(var i=0;i<bytes.length;i++) {
s += String.fromCharCode(bytes[i]);
}
return s;
}
function dbg(i) {
// Disabled debugging.
//console.log.apply(console, Array.prototype.slice.apply(arguments, [0, arguments.length-1]).concat([toHex(arguments[arguments.length-1])]));
return arguments[arguments.length-1];
}
function KeePassError(message) {
this.message = message;
}
exports.userPassword = function(password) {
return crypto.SHA256(UTF8ToBytes(password), {asBytes: true});
}
exports.readDatabaseFromFile = function(userKeys, filePath, result, error) {
var fd = fs.openSync(filePath, 'r');
var fileSize = fs.statSync(filePath).size;
var buffer = new Buffer(fileSize);
if (fs.readSync(fd, buffer, 0, fileSize) < fileSize) {
throw new KeePassError("Could not read all bytes of file!");
}
var bytes = BufferToBytes(buffer);
return exports.readDatabaseFromBytes(userKeys, bytes, result, error);
};
exports.readDatabaseFromBytes = function(userKeys, bytes, result, error) {
var filePosition = 0;
function readStruct(fmt) {
var l = Struct.CalcLength(fmt);
var r = Struct.Unpack(fmt, bytes, filePosition);
filePosition += l;
return r;
}
function readBytes(l) {
var r = bytes.slice(filePosition, filePosition+l);
filePosition += l;
return r;
}
var header = (function readDatabaseHeader() {
var sigs = readStruct('<I<I');
if (sigs[0] !== 0x9AA2D903 ||
sigs[1] !== 0xB54BFB67) {
throw new KeePassError("Incorrect format");
}
var version = readStruct('<I');
if ((version & 0xFFFF0000) > (0x02010100 & 0xFFFF0000)) {
throw new KeePassError("Incorrect version");
}
var header = {};
while (!readHeaderField()) { }
function readHeaderField() {
var fieldID = readStruct('<B')[0];
var fieldSize = readStruct('<H')[0];
var fieldData = readBytes(fieldSize);
return ({
0: function EndOfHeader(b) { return true; },
1: function Comment(b) { header.comment = BytesToUTF8(b); },
2: function CipherID(b) { header.dataCipher = b; },
3: function CompressionFlags(b) { header.compression = Struct.Unpack('<I', b)[0]; },
4: function MasterSeed(b) { header.masterSeed = b; },
5: function TransformSeed(b) { header.transformSeed = b; },
6: function TransformRounds(b) { header.transformRounds = Struct.Unpack('<L', b)[0]; },
7: function EncryptionIV(b) { header.encryptionIV = b; },
8: function ProtectedStreamKey(b) { header.protectedStreamKey = b; },
9: function StreamStartBytes(b) { header.streamStartBytes = b; },
10: function RandomStreamID(b) { header.randomStreamID = Struct.Unpack('<I', b)[0]; }
}[fieldID])(fieldData) || false;
}
return header;
})();
// Create key with which to decrypt the rest of the file.
var finalKey = (function fabricateDecryptionKey(userKeys) {
function transformKey(raw, keyseed, rounds) {
var key = raw;
var iv = [];
for (var i = 0; i < 16; i++) { iv.push(0); }
var opt = {mode: new crypto.mode.ECB(crypto.pad.NoPadding), iv: iv, asBytes: true};
for (var i = 0; i < rounds; i++) {
key = crypto.AES.encrypt(key, keyseed, opt);
}
return crypto.SHA256(key, {asBytes: true});
}
var concatKeys = [];
userKeys.forEach(function(userKey) { concatKeys = concatKeys.concat(userKey); });
var compositeKey = crypto.SHA256(concatKeys, {asBytes: true});
var transformedMasterKey = transformKey(compositeKey, header.transformSeed, header.transformRounds);
return crypto.SHA256(header.masterSeed.concat(transformedMasterKey), {asBytes: true});
})(userKeys);
// Create the decryptor based on the DataCipher-type that is stored in the database.
var decrypter = ({
'31c1f2e6bf714350be5805216afc5aff': function AES_CBC_PKCS7(key, iv) {
var options = {mode: new crypto.mode.CBC(crypto.pad.pkcs7), iv: iv, asBytes: true};
return function(input) {
return crypto.AES.decrypt(input, key, options);
};
}
}[toHex(header.dataCipher)])(finalKey, header.encryptionIV);
// Decrypt the rest of the file using the decrypter.
var content = (function ReadDecryptAndCheck(decrypter) {
var bcrypted = readBytes(bytes.length - filePosition);
var bdecrypted = decrypter(bcrypted);
startBytes = bdecrypted.slice(0, 32);
// Check the first 32 bytes (= startBytes), which should match the unencrypted 32 bytes that was stored in the header (= header.streamStartBytes).
for (var i=0;i<startBytes.length;i++) {
if (startBytes[i] !== header.streamStartBytes[i]) {
throw new KeePassError('Could not decrypt file');
}
}
return bdecrypted.slice(32);
})(decrypter);
// Check every chunk of data with its preceding hash and concat all chunks to one byte array.
content = (function CheckAndConcatHashedBlocks(content) {
var contentPosition = 0;
function unpack(fmt) {
var l = Struct.CalcLength(fmt);
var r = Struct.Unpack(fmt, content, contentPosition);
contentPosition += l;
return r;
}
function readBytes(l) {
var r = content.slice(contentPosition, contentPosition+l);
contentPosition += l;
return r;
}
var blockIndex = 0;
var unhashedContent = [];
while(true) {
var readBlockIndex = unpack('<I')[0];
if (blockIndex !== readBlockIndex) {
throw 'Invalid index, file is broken?';
}
blockIndex++;
var blockHash = readBytes(32);
var blockSize = unpack('<i')[0];
// End Of File
if (blockSize === 0) { break; }
var blockBytes = readBytes(blockSize);
var calculatedHash = crypto.SHA256(blockBytes, {asBytes: true});
for (var i=0;i<32;i++) {
if (blockHash[i] !== calculatedHash[i]) {
throw new Error('Invalid hash, file broken?');
}
}
unhashedContent = unhashedContent.concat(blockBytes);
}
return unhashedContent;
})(content);
// Uncompress the resulting bytes if the database is indeed marked (by db.compression) as compressed.
content = ({
0: function Uncompressed(input) { return BytesToUTF8(input); },
1: function Gzip(input) { return new JXG.Util.Unzip(input).unzip()[0][0]; }
}[header.compression])(content);
// Random bytes generator for in-memory protection. Protected strings are xor-ed with random bytes from this generator.
var randomStream = ({
0: function Null(key) { return function(data) { return data; } },
1: function ArcFourVariant(key) {
/*
for(uint w = 0; w < uRequestedCount; ++w)
{
++m_i;
m_j += m_pbState[m_i];
byte t = m_pbState[m_i]; // Swap entries
m_pbState[m_i] = m_pbState[m_j];
m_pbState[m_j] = t;
t = (byte)(m_pbState[m_i] + m_pbState[m_j]);
pbRet[w] = m_pbState[t];
}
*/
},
2: salsa20
}[header.randomStreamID])(crypto.SHA256(header.protectedStreamKey, {asBytes: true}));
(function parseXML(result) {
var parser = sax.parser(true);
var tagStack = [];
var root = undefined;
tagStack.peek = function() { return this[this.length-1]; };
parser.onerror = function() {
error('error parsing xml');
};
parser.ontext = function(t) {
var parent = tagStack.peek();
if (parent) {
parent.xml._text = t;
}
};
parser.onopentag = function(t) {
var e = {xml: {name: t.name, attributes: [], children: []}};
var parent = tagStack.peek();
if (parent) {
e.xml.parent = parent;
parent.xml.children.push(e);
}
tagStack.push(e);
};
parser.onclosetag = function(t) {
var e = tagStack.pop();
// Add extra info to make elements workable with js.
function getall(name) { return function() { return e.xml.children.filter(function(e) { return e.xml.name === name; }); } }
function getfirst(name) { return function() { return e.xml.children.filter(function(e) { return e.xml.name === name; })[0]; } }
function gettext(name) { return function() { return e.xml.children.filter(function(e) { return e.xml.name === name; })[0]._text; } }
switch(e.xml.name) {
case 'String':
var isprotected = e.xml.attributes.filter(function(a) { return a.name === 'Protected' && a.value === 'True' }).length > 0;
var key = e.xml.children.filter(function(e) { return e.xml.name === 'Key' }).map(function(e) { return e.xml._text })[0];
var value = function() { return e.xml.children.filter(function(e) { return e.xml.name === 'Value' }).map(function(e) { return e.xml._text})[0]; };
if (key && value()) {
if (isprotected) {
var cryptedBytesLength = JXG.Util.Base64.decode(value()).length;
var randomBytes = randomStream(cryptedBytesLength);
e.xml.parent[key] = function() {
var bytes = UTF8ToBytes(JXG.Util.Base64.decode(value()));
for (var i=0;i<bytes.length; i++) {
bytes[i] = bytes[i] ^ randomBytes[i];
}
return BytesToUTF8(bytes);
};
} else {
e.xml.parent[key] = value;
}
}
break;
case 'KeePassFile':
e.root = getfirst('Root');
e.meta = getfirst('Meta');
e.header = function() { return header; };
break;
case 'Root':
case 'Group':
e.uuid = gettext('UUID');
e.name = gettext('Name');
e.entries = getall('Entry');
e.groups = getall('Group');
break;
}
if (tagStack.length === 0) {
result(e);
}
};
parser.onattribute = function(a) {
var parent = tagStack.peek();
if (parent) {
parent.xml.attributes.push(a);
}
};
parser.write(content);
})(function(r) {
result(r);
});
/*} catch(e) {
if (e instanceof KeePassError) {
error(new Error(e.message));
} else {
throw e;
}
}*/}
function arr(o) {
if (o) {
if (o instanceof Array) { return o; }
if (typeof o === 'object') { return [o]; }
}
return [];
}
function getentries(database, entry) {
(function handlegroup(group) {
arr(group.Entry).forEach(function(e) { entry(e); });
arr(group.Group).forEach(handlegroup);
})(database.Root.Group);
}
function handleentries(entries) {
function getString(e, name) {
return arr(e.String).filter(function(str) { return str.Key === name; }).map(function(str) { return typeof str.Value === 'string' ? str.Value : undefined; })[0];
}
function title(t) {
if (typeof t === 'string') { t = new RegExp(t); }
return function(e) { return t.test(getString(e, 'Title')); }
}
entries.filter(title(/Softwarebakery/)).forEach(function(e) { console.log(getString(e, 'Title'), getString(e, 'UserName')); });
}