-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathencryption.js
More file actions
652 lines (556 loc) · 21.5 KB
/
encryption.js
File metadata and controls
652 lines (556 loc) · 21.5 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/*!
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
*/
import '@webex/internal-plugin-metrics';
import '@webex/internal-plugin-encryption';
import {isBuffer} from '@webex/common';
import {assert, expect} from '@webex/test-helper-chai';
import file from '@webex/test-helper-file';
import sinon from 'sinon';
import WebexCore from '@webex/webex-core';
import testUsers from '@webex/test-helper-test-users';
import makeLocalUrl from '@webex/test-helper-make-local-url';
describe('Encryption', function () {
this.timeout(30000);
let key, user, webex;
const PLAINTEXT =
'Admiral, if we go "by the book". like Lieutenant Saavik, hours could seem like days.';
let FILE = makeLocalUrl('/sample-image-small-one.png');
before('create test user', () =>
testUsers.create({count: 1}).then((users) => {
user = users[0];
webex = new WebexCore({
credentials: {
authorization: user.token,
},
});
assert.isTrue(webex.isAuthenticated || webex.canAuthorize);
})
);
before('create unbound key', () =>
webex.internal.encryption.kms.createUnboundKeys({count: 1}).then(([k]) => {
key = k;
})
);
before('fetch file fixture', () =>
webex
.request({
uri: FILE,
responseType: 'buffer',
})
.then((res) => {
FILE = res.body;
})
);
after(() => webex && webex.internal.mercury.disconnect());
describe('#decryptBinary()', () => {
it('decrypts a binary file', () =>
webex.internal.encryption.encryptBinary(FILE).then(({scr, cdata}) => {
scr.loc = 'file:///file.enc';
return webex.internal.encryption
.encryptScr(key, scr)
.then((cipherScr) => webex.internal.encryption.decryptScr(key, cipherScr))
.then((decryptedScr) => webex.internal.encryption.decryptBinary(decryptedScr, cdata))
.then((f) => {
assert.isTrue(isBuffer(f));
return assert.equal(f.byteLength, FILE.byteLength);
});
}));
});
describe('#decryptScr()', () => {
it('decrypts an scr', () =>
webex.internal.encryption.encryptBinary(FILE).then(({scr}) => {
scr.loc = 'file:///file.enc';
return webex.internal.encryption
.encryptScr(key, scr)
.then((cipherScr) => webex.internal.encryption.decryptScr(key, cipherScr))
.then((decryptedScr) => assert.deepEqual(decryptedScr, scr));
}));
});
describe('#decryptText()', () => {
it('decrypts text', () =>
webex.internal.encryption
.encryptText(key, PLAINTEXT)
.then((ciphertext) => {
assert.notEqual(ciphertext, PLAINTEXT);
return webex.internal.encryption.decryptText(key, ciphertext);
})
.then((plaintext) => assert.equal(plaintext, PLAINTEXT)));
});
describe('#decryptBinaryData()', () => {
it('decrypts binary data', () =>
webex.internal.encryption
.encryptText(key, FILE.toString('base64'))
.then((ciphertext) => {
assert.notEqual(ciphertext, FILE.toString('base64'));
return webex.internal.encryption.decryptBinaryData(key, ciphertext);
})
.then((decryptedData) => {
assert.isTrue(isBuffer(decryptedData));
assert.equal(decryptedData.toString('base64'), FILE.toString('base64'));
}));
it('decrypts binary data with Buffer input', () => {
const binaryData = Buffer.from('test binary data', 'utf8');
return webex.internal.encryption
.encryptText(key, binaryData.toString('base64'))
.then((ciphertext) => {
assert.notEqual(ciphertext, binaryData.toString('base64'));
return webex.internal.encryption.decryptBinaryData(key, ciphertext);
})
.then((decryptedData) => {
assert.isTrue(isBuffer(decryptedData));
assert.equal(decryptedData.toString('utf8'), 'test binary data');
});
});
it('decrypts binary data with options parameter', () => {
const binaryData = Buffer.from('test binary data with options', 'utf8');
return webex.internal.encryption
.encryptText(key, binaryData.toString('base64'))
.then((ciphertext) => {
assert.notEqual(ciphertext, binaryData.toString('base64'));
return webex.internal.encryption.decryptBinaryData(key, ciphertext, {});
})
.then((decryptedData) => {
assert.isTrue(isBuffer(decryptedData));
assert.equal(decryptedData.toString('utf8'), 'test binary data with options');
});
});
it('decrypts binary data with dynamically generated 3000 character string', () => {
// Generate a 3000-character string dynamically using different character sets
const generateLargeString = (length) => {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+-=[]{}|;:,.<>?';
let result = '';
const charsLength = chars.length;
for (let i = 0; i < length; i++) {
// Use modulo to cycle through different patterns for variety
const index = (i * 7 + Math.floor(i / 100) * 3) % charsLength;
result += chars.charAt(index);
}
return result;
};
const largeString = generateLargeString(3000);
const binaryData = Buffer.from(largeString, 'utf8');
// Verify the string is exactly 3000 characters
assert.equal(largeString.length, 3000);
return webex.internal.encryption
.encryptText(key, binaryData.toString('base64'))
.then((ciphertext) => {
assert.notEqual(ciphertext, binaryData.toString('base64'));
return webex.internal.encryption.decryptBinaryData(key, ciphertext);
})
.then((decryptedData) => {
assert.isTrue(isBuffer(decryptedData));
assert.equal(decryptedData.toString('utf8'), largeString);
assert.equal(decryptedData.toString('utf8').length, 3000);
});
});
});
describe('#getKey()', () => {
let fetchKeySpy, otherWebex, otherUser, storageGetSpy;
before('create test user', () =>
testUsers.create({count: 1}).then((users) => {
otherUser = users[0];
otherWebex = new WebexCore({
credentials: {
authorization: otherUser.token,
},
});
assert.isTrue(otherWebex.canAuthorize);
})
);
before('create kms resource', () =>
webex.internal.encryption.kms.createResource({
key,
userIds: [webex.internal.device.userId, otherUser.id],
})
);
after(() => otherWebex && otherWebex.internal.mercury.disconnect());
beforeEach(() => {
fetchKeySpy = sinon.spy(otherWebex.internal.encryption.kms, 'fetchKey');
storageGetSpy = sinon.spy(otherWebex.internal.encryption.unboundedStorage, 'get');
});
afterEach(() => {
fetchKeySpy.restore();
storageGetSpy.restore();
});
it('shortcircuits if it receives a key instead of a keyUri', () =>
webex.internal.encryption
.getKey(key)
// Reminder: If this starts failing after a node-jose upgrade, it probably
// implies node-jose stopped shortcircuiting correctly.
.then((k) => assert.equal(k, key)));
it('attempts to retrieve the specified key from the local cache', () =>
otherWebex.internal.encryption
.getKey(key.uri)
.then((k) => assert.calledWith(storageGetSpy, k.uri)));
it('fetches the key from the kms', () =>
otherWebex.internal.encryption.unboundedStorage
.del(key.uri)
.then(() => assert.notCalled(fetchKeySpy))
.then(() => otherWebex.internal.encryption.getKey(key.uri))
.then(() => assert.calledOnce(fetchKeySpy)));
it('stores the newly retrieved key', () =>
otherWebex.internal.encryption
.getKey(key.uri)
.then((k) => otherWebex.internal.encryption.unboundedStorage.get(k.uri))
.then((str) => JSON.parse(str))
.then((k2) => {
assert.property(k2, 'jwk');
assert.property(k2.jwk, 'k');
assert.equal(key.jwk.kid, k2.jwk.kid);
}));
});
// SPARK-413317
describe.skip('#download()', () => {
it('downloads and decrypts an encrypted file', () =>
webex.internal.encryption
.encryptBinary(FILE)
.then(({scr, cdata}) =>
webex
.request({
method: 'POST',
uri: makeLocalUrl('/files/upload'),
body: cdata,
})
.then((res) => {
scr.loc = makeLocalUrl(res.body.loc, {full: true});
return webex.internal.encryption.encryptScr(key, scr);
})
)
.then((cipherScr) => webex.internal.encryption.decryptScr(key, cipherScr))
.then((scr) => webex.internal.encryption.download(scr))
.then((f) =>
file.isMatchingFile(f, FILE).then((result) => assert.deepEqual(result, true))
));
it('downloads and decrypts an encrypted file with options param', () =>
webex.internal.encryption
.encryptBinary(FILE)
.then(({scr, cdata}) =>
webex
.request({
method: 'POST',
uri: makeLocalUrl('/files/upload'),
body: cdata,
})
.then((res) => {
scr.loc = makeLocalUrl(res.body.loc, {full: true});
return webex.internal.encryption.encryptScr(key, scr);
})
)
.then((cipherScr) => webex.internal.encryption.decryptScr(key, cipherScr))
.then((scr) => {
const options = {
params: {
allow: 'none',
},
};
return webex.internal.encryption.download(scr, options);
})
.then((f) => file.isMatchingFile(f, FILE))
.then((result) => assert.deepEqual(result, true)));
it('emits progress events', () => {
const spy = sinon.spy();
return webex.internal.encryption
.encryptBinary(FILE)
.then(({scr, cdata}) =>
webex
.request({
method: 'POST',
uri: makeLocalUrl('/files/upload'),
body: cdata,
})
.then((res) => {
scr.loc = makeLocalUrl(res.body.loc, {full: true});
return webex.internal.encryption.encryptScr(key, scr);
})
)
.then((cipherScr) => webex.internal.encryption.decryptScr(key, cipherScr))
.then((scr) => webex.internal.encryption.download(scr).on('progress', spy))
.then(() => assert.called(spy));
});
it('checks body of the API call /downloads/endpoints', () =>
webex.internal.encryption
.encryptBinary(FILE)
.then(({scr, cdata}) =>
webex
.request({
method: 'POST',
uri: makeLocalUrl('/files/upload'),
body: cdata,
})
.then((res) => {
scr.loc = makeLocalUrl(res.body.loc, {full: true});
return webex.internal.encryption.encryptScr(key, scr);
})
)
.then((cipherScr) => webex.internal.encryption.decryptScr(key, cipherScr))
.then((scr) => {
const options = {
params: {
allow: ['unchecked', 'evaluating'],
},
};
return webex.internal.encryption.download(scr, options);
})
.then((f) => file.isMatchingFile(f, FILE))
.then((result) => assert.deepEqual(result, true)));
it('checks _fetchDownloadUrl()', () =>
webex.internal.encryption
.encryptBinary(FILE)
.then(({scr, cdata}) =>
webex
.request({
method: 'POST',
uri: makeLocalUrl('/files/upload'),
body: cdata,
})
.then((res) => {
scr.loc = makeLocalUrl(res.body.loc, {full: true});
return webex.internal.encryption.encryptScr(key, scr);
})
)
.then((cipherScr) => webex.internal.encryption.decryptScr(key, cipherScr))
.then((scr) => {
const options = {
params: {
allow: ['unchecked', 'evaluating'],
},
};
return webex.internal.encryption._fetchDownloadUrl(scr, options);
})
.then((result) => assert.isString(result)));
});
describe('#encryptBinary()', () => {
it('encrypts a binary file', () =>
webex.internal.encryption.encryptBinary(FILE).then(({scr, cdata}) => {
assert.property(scr, 'enc');
assert.property(scr, 'key');
assert.property(scr, 'iv');
return assert.isBufferLike(cdata);
}));
// browserOnly(it)(`accepts an ArrayBuffer`);
// browserOnly(it)(`accepts a Blob`);
});
describe('#encryptBinaryData()', () => {
it('encrypts binary data', () =>
webex.internal.encryption
.encryptBinaryData(key, FILE)
.then((jwe) => {
assert.isString(jwe);
assert.notEqual(jwe, FILE.toString('base64'));
// JWE format starts with eyJ (base64 encoded header)
assert.match(jwe, /^eyJ/);
}));
it('encrypts binary data with Buffer input', () => {
const binaryData = Buffer.from('test binary data', 'utf8');
return webex.internal.encryption
.encryptBinaryData(key, binaryData)
.then((jwe) => {
assert.isString(jwe);
assert.notEqual(jwe, binaryData.toString());
assert.match(jwe, /^eyJ/);
});
});
it('encrypts binary data with options parameter', () => {
const binaryData = Buffer.from('test binary data with options', 'utf8');
return webex.internal.encryption
.encryptBinaryData(key, binaryData, {})
.then((jwe) => {
assert.isString(jwe);
assert.match(jwe, /^eyJ/);
});
});
it('encrypts binary data with dynamically generated 3000 character string', () => {
// Generate a 3000-character string dynamically using different character sets
const generateLargeString = (length) => {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+-=[]{}|;:,.<>?';
let result = '';
const charsLength = chars.length;
for (let i = 0; i < length; i++) {
// Use modulo to cycle through different patterns for variety
const index = (i * 7 + Math.floor(i / 100) * 3) % charsLength;
result += chars.charAt(index);
}
return result;
};
const largeString = generateLargeString(3000);
const binaryData = Buffer.from(largeString, 'utf8');
// Verify the string is exactly 3000 characters
assert.equal(largeString.length, 3000);
return webex.internal.encryption
.encryptBinaryData(key, binaryData)
.then((jwe) => {
assert.isString(jwe);
assert.notEqual(jwe, binaryData.toString('base64'));
return webex.internal.encryption.decryptBinaryData(key, jwe);
})
.then((decryptedData) => {
assert.isTrue(isBuffer(decryptedData));
assert.equal(decryptedData.toString('utf8'), largeString);
assert.equal(decryptedData.toString('utf8').length, 3000);
});
});
});
describe('#encryptScr()', () => {
it('encrypts an scr', () =>
webex.internal.encryption
.encryptBinary(FILE)
.then(({scr}) => {
scr.loc = 'file:///file.enc';
return webex.internal.encryption.encryptScr(key, scr);
})
.then((cipherScr) => assert.isString(cipherScr)));
});
describe('#encryptText()', () => {
it('encrypts text', () =>
webex.internal.encryption
.encryptText(key, PLAINTEXT)
.then((ciphertext) => assert.notEqual(ciphertext, PLAINTEXT)));
});
describe('#onBehalfOf', () => {
let complianceUser;
before('create compliance officer test user', () =>
testUsers
.create({
count: 1,
config: {
roles: [{name: 'spark.kms_orgagent'}],
},
})
.then((users) => {
complianceUser = users[0];
complianceUser.webex = new WebexCore({
credentials: {
authorization: complianceUser.token,
},
});
assert.isTrue(complianceUser.webex.canAuthorize);
})
);
after(() => complianceUser && complianceUser.webex.internal.mercury.disconnect());
it('decrypt text', () =>
webex.internal.encryption
.encryptText(key, PLAINTEXT)
.then((ciphertext) => {
assert.notEqual(ciphertext, PLAINTEXT);
return complianceUser.webex.internal.encryption.decryptText(key, ciphertext, {
onBehalfOf: user.id,
});
})
.then((plaintext) => assert.equal(plaintext, PLAINTEXT)));
it('decrypt binary data', () => {
const binaryData = Buffer.from('compliance test binary data', 'utf8');
return webex.internal.encryption
.encryptText(key, binaryData.toString('base64'))
.then((ciphertext) => {
assert.notEqual(ciphertext, binaryData.toString('base64'));
return complianceUser.webex.internal.encryption.decryptBinaryData(key, ciphertext, {
onBehalfOf: user.id,
});
})
.then((decryptedData) => {
assert.isTrue(isBuffer(decryptedData));
assert.equal(decryptedData.toString('utf8'), 'compliance test binary data');
});
});
it('encrypt binary data', () => {
const binaryData = Buffer.from('compliance encrypt binary data', 'utf8');
return complianceUser.webex.internal.encryption
.encryptBinaryData(key, binaryData, {onBehalfOf: user.id})
.then((jwe) => {
assert.isString(jwe);
assert.match(jwe, /^eyJ/);
});
});
it('encrypt and decrypt binary data', () => {
const binaryData = Buffer.from('compliance round-trip binary data', 'utf8');
return complianceUser.webex.internal.encryption
.encryptBinaryData(key, binaryData, {onBehalfOf: user.id})
.then((jwe) => {
assert.isString(jwe);
return complianceUser.webex.internal.encryption.decryptBinaryData(key, jwe, {
onBehalfOf: user.id,
});
})
.then((decryptedData) => {
assert.isTrue(isBuffer(decryptedData));
assert.equal(decryptedData.toString('utf8'), 'compliance round-trip binary data');
});
});
it('encrypt and decrypt text', () =>
complianceUser.webex.internal.encryption
.encryptText(key, PLAINTEXT, {onBehalfOf: user.id})
.then((ciphertext) => {
assert.notEqual(ciphertext, PLAINTEXT);
return complianceUser.webex.internal.encryption.decryptText(key, ciphertext, {
onBehalfOf: user.id,
});
})
.then((plaintext) => assert.equal(plaintext, PLAINTEXT)));
it('decrypt scr', () =>
webex.internal.encryption.encryptBinary(FILE).then(({scr}) => {
scr.loc = 'file:///file.enc';
return webex.internal.encryption
.encryptScr(key, scr)
.then((cipherScr) =>
complianceUser.webex.internal.encryption.decryptScr(key, cipherScr, {
onBehalfOf: user.id,
})
)
.then((decryptedScr) => assert.deepEqual(decryptedScr, scr));
}));
it('decrypt scr', () =>
webex.internal.encryption.encryptBinary(FILE).then(({scr}) => {
scr.loc = 'file:///file.enc';
return complianceUser.webex.internal.encryption
.encryptScr(key, scr, {onBehalfOf: user.id})
.then((cipherScr) =>
complianceUser.webex.internal.encryption.decryptScr(key, cipherScr, {
onBehalfOf: user.id,
})
)
.then((decryptedScr) => assert.deepEqual(decryptedScr, scr));
}));
it('getKey', () =>
complianceUser.webex.internal.encryption
.getKey(key.uri, {onBehalfOf: user.id})
.then((key2) => {
assert.property(key2, 'uri');
assert.property(key2, 'jwk');
assert.notEqual(key2, key);
assert.equal(key2.uri, key.uri);
}));
it('getKey forbidden as compliance officer does not have access', () =>
complianceUser.webex.internal.encryption.getKey(key.uri).then(
(value) => expect.fail(`Compliance officer has retrieved key without onBehalfOf: ${value}`),
(error) => expect(error.body.status).to.equal(403)
));
it('getKey forbidden as user does not have access', () =>
complianceUser.webex.internal.encryption
.getKey(key.uri, {onBehalfOf: '7851fe79-7c87-40cc-ac36-8b77b011b399'})
.then(
(value) =>
expect.fail(
`Should not be found as 7851fe79-7c87-40cc-ac36-8b77b011b399 does not have access ${value}`
),
(error) => expect(error.body.status).to.equal(403)
));
it('getKey onBehalfOf and then by compliance officer only', () =>
complianceUser.webex.internal.encryption
.getKey(key.uri, {onBehalfOf: user.id})
.then((key2) => {
assert.property(key2, 'uri');
assert.property(key2, 'jwk');
assert.notEqual(key2, key);
assert.equal(key2.uri, key.uri);
})
.then(() => complianceUser.webex.internal.encryption.getKey(key.uri))
.then(
(value) =>
expect.fail(
`Compliance should no longer be able to retrieve key as onBehalfOf was not set: ${value}`
),
(error) => expect(error.body.status).to.equal(403)
));
});
});