-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathindex.js
executable file
·373 lines (263 loc) · 9.96 KB
/
index.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
'use strict';
const Crypto = require('crypto');
const B64 = require('@hapi/b64');
const Boom = require('@hapi/boom');
const Bourne = require('@hapi/bourne');
const Cryptiles = require('@hapi/cryptiles');
const Hoek = require('@hapi/hoek');
const internals = {};
exports.defaults = {
encryption: {
saltBits: 256,
algorithm: 'aes-256-cbc',
iterations: 1,
minPasswordlength: 32
},
integrity: {
saltBits: 256,
algorithm: 'sha256',
iterations: 1,
minPasswordlength: 32
},
ttl: 0, // Milliseconds, 0 means forever
timestampSkewSec: 60, // Seconds of permitted clock skew for incoming expirations
localtimeOffsetMsec: 0 // Local clock time offset express in a number of milliseconds (positive or negative)
};
// Algorithm configuration
exports.algorithms = {
'aes-128-ctr': { keyBits: 128, ivBits: 128 },
'aes-256-cbc': { keyBits: 256, ivBits: 128 },
'sha256': { keyBits: 256 }
};
// MAC normalization format version
exports.macFormatVersion = '2'; // Prevent comparison of mac values generated with different normalized string formats
exports.macPrefix = 'Fe26.' + exports.macFormatVersion;
// Generate a unique encryption key
/*
const options = {
saltBits: 256, // Ignored if salt is set
salt: '4d8nr9q384nr9q384nr93q8nruq9348run',
algorithm: 'aes-128-ctr',
iterations: 10000,
iv: 'sdfsdfsdfsdfscdrgercgesrcgsercg', // Optional
minPasswordlength: 32
};
*/
exports.generateKey = async function (password, options) {
if (!password) {
throw new Boom.Boom('Empty password');
}
if (!options ||
typeof options !== 'object') {
throw new Boom.Boom('Bad options');
}
const algorithm = exports.algorithms[options.algorithm];
if (!algorithm) {
throw new Boom.Boom('Unknown algorithm: ' + options.algorithm);
}
const result = {};
if (Buffer.isBuffer(password)) {
if (password.length < algorithm.keyBits / 8) {
throw new Boom.Boom('Key buffer (password) too small');
}
result.key = password;
result.salt = '';
}
else {
if (password.length < options.minPasswordlength) {
throw new Boom.Boom('Password string too short (min ' + options.minPasswordlength + ' characters required)');
}
let salt = options.salt;
if (!salt) {
if (!options.saltBits) {
throw new Boom.Boom('Missing salt and saltBits options');
}
const randomSalt = Cryptiles.randomBits(options.saltBits);
salt = randomSalt.toString('hex');
}
const derivedKey = await internals.pbkdf2(password, salt, options.iterations, algorithm.keyBits / 8, 'sha1');
result.key = derivedKey;
result.salt = salt;
}
if (options.iv) {
result.iv = options.iv;
}
else if (algorithm.ivBits) {
result.iv = Cryptiles.randomBits(algorithm.ivBits);
}
return result;
};
// Encrypt data
// options: see exports.generateKey()
exports.encrypt = async function (password, options, data) {
const key = await exports.generateKey(password, options);
const cipher = Crypto.createCipheriv(options.algorithm, key.key, key.iv);
const encrypted = Buffer.concat([cipher.update(data, 'utf8'), cipher.final()]);
return { encrypted, key };
};
// Decrypt data
// options: see exports.generateKey()
exports.decrypt = async function (password, options, data) {
const key = await exports.generateKey(password, options);
const decipher = Crypto.createDecipheriv(options.algorithm, key.key, key.iv);
let dec = decipher.update(data, null, 'utf8');
dec = dec + decipher.final('utf8');
return dec;
};
// HMAC using a password
// options: see exports.generateKey()
exports.hmacWithPassword = async function (password, options, data) {
const key = await exports.generateKey(password, options);
const hmac = Crypto.createHmac(options.algorithm, key.key).update(data);
const digest = hmac.digest('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');
return {
digest,
salt: key.salt
};
};
// Normalizes a password parameter into a { id, encryption, integrity } object
// password: string, buffer or object with { id, secret } or { id, encryption, integrity }
internals.normalizePassword = function (password) {
if (password &&
typeof password === 'object' &&
!Buffer.isBuffer(password)) {
return {
id: password.id,
encryption: password.secret ?? password.encryption,
integrity: password.secret ?? password.integrity
};
}
return {
encryption: password,
integrity: password
};
};
// Encrypt and HMAC an object
// password: string, buffer or object with { id, secret } or { id, encryption, integrity }
// options: see exports.defaults
exports.seal = async function (object, password, options) {
options = Object.assign({}, options); // Shallow cloned to prevent changes during async operations
const now = Date.now() + (options.localtimeOffsetMsec ?? 0); // Measure now before any other processing
// Serialize object
const objectString = internals.stringify(object);
// Obtain password
let passwordId = '';
password = internals.normalizePassword(password);
if (password.id) {
if (!/^\w+$/.test(password.id)) {
throw new Boom.Boom('Invalid password id');
}
passwordId = password.id;
}
// Encrypt object string
const { encrypted, key } = await exports.encrypt(password.encryption, options.encryption, objectString);
// Base64url the encrypted value
const encryptedB64 = B64.base64urlEncode(encrypted);
const iv = B64.base64urlEncode(key.iv);
const expiration = (options.ttl ? now + options.ttl : '');
const macBaseString = exports.macPrefix + '*' + passwordId + '*' + key.salt + '*' + iv + '*' + encryptedB64 + '*' + expiration;
// Mac the combined values
const mac = await exports.hmacWithPassword(password.integrity, options.integrity, macBaseString);
// Put it all together
// prefix*[password-id]*encryption-salt*encryption-iv*encrypted*[expiration]*hmac-salt*hmac
// Allowed URI query name/value characters: *-. \d \w
const sealed = macBaseString + '*' + mac.salt + '*' + mac.digest;
return sealed;
};
// Decrypt and validate sealed string
// password: string, buffer or object with { id: secret } or { id: { encryption, integrity } }
// options: see exports.defaults
exports.unseal = async function (sealed, password, options) {
options = Object.assign({}, options); // Shallow cloned to prevent changes during async operations
const now = Date.now() + (options.localtimeOffsetMsec ?? 0); // Measure now before any other processing
// Break string into components
const parts = sealed.split('*');
if (parts.length !== 8) {
throw new Boom.Boom('Incorrect number of sealed components');
}
const macPrefix = parts[0];
const passwordId = parts[1];
const encryptionSalt = parts[2];
const encryptionIv = parts[3];
const encryptedB64 = parts[4];
const expiration = parts[5];
const hmacSalt = parts[6];
const hmac = parts[7];
const macBaseString = macPrefix + '*' + passwordId + '*' + encryptionSalt + '*' + encryptionIv + '*' + encryptedB64 + '*' + expiration;
// Check prefix
if (macPrefix !== exports.macPrefix) {
throw new Boom.Boom('Wrong mac prefix');
}
// Check expiration
if (expiration) {
if (!expiration.match(/^\d+$/)) {
throw new Boom.Boom('Invalid expiration');
}
const exp = parseInt(expiration, 10);
if (exp <= (now - (options.timestampSkewSec * 1000))) {
throw new Boom.Boom('Expired seal');
}
}
// Obtain password
if (!password) {
throw new Boom.Boom('Empty password');
}
if (typeof password === 'object' &&
!Buffer.isBuffer(password)) {
password = password[passwordId || 'default'];
if (!password) {
throw new Boom.Boom('Cannot find password: ' + passwordId);
}
}
password = internals.normalizePassword(password);
// Check hmac
const macOptions = Hoek.clone(options.integrity);
macOptions.salt = hmacSalt;
const mac = await exports.hmacWithPassword(password.integrity, macOptions, macBaseString);
if (!Cryptiles.fixedTimeComparison(mac.digest, hmac)) {
throw new Boom.Boom('Bad hmac value');
}
// Decrypt
try {
var encrypted = B64.base64urlDecode(encryptedB64, 'buffer');
}
catch (err) {
throw Boom.boomify(err);
}
const decryptOptions = Hoek.clone(options.encryption);
decryptOptions.salt = encryptionSalt;
try {
decryptOptions.iv = B64.base64urlDecode(encryptionIv, 'buffer');
}
catch (err) {
throw Boom.boomify(err);
}
const decrypted = await exports.decrypt(password.encryption, decryptOptions, encrypted);
// Parse JSON
try {
return Bourne.parse(decrypted);
}
catch (err) {
throw new Boom.Boom('Failed parsing sealed object JSON: ' + err.message);
}
};
internals.stringify = function (object) {
try {
return JSON.stringify(object);
}
catch (err) {
throw new Boom.Boom('Failed to stringify object: ' + err.message);
}
};
internals.pbkdf2 = function (...args) {
return new Promise((resolve, reject) => {
const next = (err, result) => {
if (err) {
return reject(Boom.boomify(err));
}
resolve(result);
};
args.push(next);
Crypto.pbkdf2(...args);
});
};