-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
keys.test.js
821 lines (699 loc) · 33.5 KB
/
keys.test.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
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/* eslint-disable no-unused-expressions */
/* eslint-disable no-undef */
import * as Factory from './lib/factory.js'
import * as Utils from './lib/Utils.js'
chai.use(chaiAsPromised)
const expect = chai.expect
describe('keys', function () {
this.timeout(Factory.TwentySecondTimeout)
beforeEach(async function () {
localStorage.clear()
this.context = await Factory.createAppContext()
await this.context.launch()
this.application = this.context.application
this.email = UuidGenerator.GenerateUuid()
this.password = UuidGenerator.GenerateUuid()
})
afterEach(async function () {
if (!this.application.dealloced) {
await Factory.safeDeinit(this.application)
}
this.application = undefined
localStorage.clear()
})
it('should not have root key by default', async function () {
expect(await this.application.encryption.getRootKey()).to.not.be.ok
})
it('validates content types requiring root encryption', function () {
expect(ContentTypeUsesRootKeyEncryption(ContentType.TYPES.ItemsKey)).to.equal(true)
expect(ContentTypeUsesRootKeyEncryption(ContentType.TYPES.EncryptedStorage)).to.equal(true)
expect(ContentTypeUsesRootKeyEncryption(ContentType.TYPES.Item)).to.equal(false)
expect(ContentTypeUsesRootKeyEncryption(ContentType.TYPES.Note)).to.equal(false)
})
it('generating export params with no account or passcode should produce encrypted payload', async function () {
/** Items key available by default */
const payload = Factory.createNotePayload()
const processedPayload = CreateEncryptedLocalStorageContextPayload(
await this.application.encryption.encryptSplitSingle({
usesItemsKeyWithKeyLookup: {
items: [payload],
},
}),
)
expect(isEncryptedPayload(processedPayload)).to.equal(true)
})
it('has root key and one items key after registering user', async function () {
await Factory.registerUserToApplication({ application: this.application })
expect(this.application.encryption.getRootKey()).to.be.ok
expect(this.application.items.getDisplayableItemsKeys().length).to.equal(1)
})
it('changing root key with passcode should re-wrap root key', async function () {
const email = 'foo'
const password = 'bar'
const key = await this.application.encryption.createRootKey(email, password, KeyParamsOrigination.Registration)
await this.application.encryption.setRootKey(key)
Factory.handlePasswordChallenges(this.application, password)
await this.application.addPasscode(password)
/** We should be able to decrypt wrapped root key with passcode */
const wrappingKeyParams = await this.application.encryption.rootKeyManager.getRootKeyWrapperKeyParams()
const wrappingKey = await this.application.encryption.computeRootKey(password, wrappingKeyParams)
await this.application.encryption.unwrapRootKey(wrappingKey).catch((error) => {
expect(error).to.not.be.ok
})
const newPassword = 'bar'
const newKey = await this.application.encryption.createRootKey(
email,
newPassword,
KeyParamsOrigination.Registration,
)
await this.application.encryption.setRootKey(newKey, wrappingKey)
await this.application.encryption.unwrapRootKey(wrappingKey).catch((error) => {
expect(error).to.not.be.ok
})
})
it('items key should be encrypted with root key', async function () {
await Factory.registerUserToApplication({ application: this.application })
const itemsKey = await this.application.encryption.getSureDefaultItemsKey()
const rootKey = await this.application.encryption.getRootKey()
/** Encrypt items key */
const encryptedPayload = await this.application.encryption.encryptSplitSingle({
usesRootKey: {
items: [itemsKey.payloadRepresentation()],
key: rootKey,
},
})
/** Should not have an items_key_id */
expect(encryptedPayload.items_key_id).to.not.be.ok
/** Attempt to decrypt with root key. Should succeed. */
const decryptedPayload = await this.application.encryption.decryptSplitSingle({
usesRootKey: {
items: [encryptedPayload],
key: rootKey,
},
})
expect(decryptedPayload.errorDecrypting).to.not.be.ok
expect(decryptedPayload.content.itemsKey).to.equal(itemsKey.content.itemsKey)
})
it('should create random items key if no account and no passcode', async function () {
const itemsKeys = this.application.items.getDisplayableItemsKeys()
expect(itemsKeys.length).to.equal(1)
const notePayload = Factory.createNotePayload()
const dirtied = notePayload.copy({
dirty: true,
dirtyIndex: getIncrementedDirtyIndex(),
})
await this.application.payloads.emitPayload(dirtied, PayloadEmitSource.LocalChanged)
await this.application.sync.sync()
const rawPayloads = await this.application.storage.getAllRawPayloads()
const rawNotePayload = rawPayloads.find((r) => r.content_type === ContentType.TYPES.Note)
expect(typeof rawNotePayload.content).to.equal('string')
})
it('should keep offline created items key upon registration', async function () {
expect(this.application.items.getDisplayableItemsKeys().length).to.equal(1)
const originalItemsKey = this.application.items.getDisplayableItemsKeys()[0]
await this.application.register(this.email, this.password)
expect(this.application.items.getDisplayableItemsKeys().length).to.equal(1)
const newestItemsKey = this.application.items.getDisplayableItemsKeys()[0]
expect(newestItemsKey.uuid).to.equal(originalItemsKey.uuid)
})
it('should use items key for encryption of note', async function () {
const notePayload = Factory.createNotePayload()
const keyToUse = await this.application.encryption.itemsEncryption.keyToUseForItemEncryption(notePayload)
expect(keyToUse.content_type).to.equal(ContentType.TYPES.ItemsKey)
})
it('encrypting an item should associate an items key to it', async function () {
const note = Factory.createNotePayload()
const encryptedPayload = await this.application.encryption.encryptSplitSingle({
usesItemsKeyWithKeyLookup: {
items: [note],
},
})
const itemsKey = this.application.encryption.itemsKeyForEncryptedPayload(encryptedPayload)
expect(itemsKey).to.be.ok
})
it('decrypt encrypted item with associated key', async function () {
const note = Factory.createNotePayload()
const title = note.content.title
const encryptedPayload = await this.application.encryption.encryptSplitSingle({
usesItemsKeyWithKeyLookup: {
items: [note],
},
})
const itemsKey = this.application.encryption.itemsKeyForEncryptedPayload(encryptedPayload)
expect(itemsKey).to.be.ok
const decryptedPayload = await this.application.encryption.decryptSplitSingle({
usesItemsKeyWithKeyLookup: {
items: [encryptedPayload],
},
})
expect(decryptedPayload.content.title).to.equal(title)
})
it('decrypts items waiting for keys', async function () {
const notePayload = Factory.createNotePayload()
const title = notePayload.content.title
const encryptedPayload = await this.application.encryption.encryptSplitSingle({
usesItemsKeyWithKeyLookup: {
items: [notePayload],
},
})
const itemsKey = this.application.encryption.itemsKeyForEncryptedPayload(encryptedPayload)
await this.application.items.removeItemFromMemory(itemsKey)
const erroredPayload = await this.application.encryption.decryptSplitSingle({
usesItemsKeyWithKeyLookup: {
items: [encryptedPayload],
},
})
await this.application.mutator.emitItemsFromPayloads([erroredPayload], PayloadEmitSource.LocalChanged)
const note = this.application.items.findAnyItem(notePayload.uuid)
expect(note.errorDecrypting).to.equal(true)
expect(note.waitingForKey).to.equal(true)
const keyPayload = new DecryptedPayload(itemsKey.payload.ejected())
await this.application.mutator.emitItemsFromPayloads([keyPayload], PayloadEmitSource.LocalChanged)
/**
* Sleeping is required to trigger asyncronous encryptionService.decryptItemsWaitingForKeys,
* which occurs after keys are mapped above.
*/
await Factory.sleep(0.2)
const updatedNote = this.application.items.findItem(note.uuid)
expect(updatedNote.errorDecrypting).to.not.be.ok
expect(updatedNote.waitingForKey).to.not.be.ok
expect(updatedNote.content.title).to.equal(title)
})
it('attempting to emit errored items key for which there exists a non errored master copy should ignore it', async function () {
await Factory.registerUserToApplication({ application: this.application })
const itemsKey = await this.application.encryption.getSureDefaultItemsKey()
expect(itemsKey.errorDecrypting).to.not.be.ok
const errored = new EncryptedPayload({
...itemsKey.payload,
content: '004:...',
errorDecrypting: true,
})
const response = new ServerSyncResponse({
data: {
retrieved_items: [errored.ejected()],
},
})
await this.application.sync.handleSuccessServerResponse({ payloadsSavedOrSaving: [], options: {} }, response)
const refreshedKey = this.application.payloads.findOne(itemsKey.uuid)
expect(refreshedKey.errorDecrypting).to.not.be.ok
expect(refreshedKey.content.itemsKey).to.be.ok
})
it('generating export params with logged in account should produce encrypted payload', async function () {
await Factory.registerUserToApplication({ application: this.application })
const payload = Factory.createNotePayload()
const encryptedPayload = await this.application.encryption.encryptSplitSingle({
usesItemsKeyWithKeyLookup: {
items: [payload],
},
})
expect(typeof encryptedPayload.content).to.equal('string')
expect(encryptedPayload.content.substring(0, 3)).to.equal(this.application.encryption.getLatestVersion())
})
it('When setting passcode, should encrypt items keys', async function () {
await this.application.addPasscode('foo')
const itemsKey = this.application.items.getDisplayableItemsKeys()[0]
const rawPayloads = await this.application.storage.getAllRawPayloads()
const itemsKeyRawPayload = rawPayloads.find((p) => p.uuid === itemsKey.uuid)
const itemsKeyPayload = new EncryptedPayload(itemsKeyRawPayload)
expect(itemsKeyPayload.enc_item_key).to.be.ok
})
it('items key encrypted payload should contain root key params', async function () {
await this.application.addPasscode('foo')
const itemsKey = this.application.items.getDisplayableItemsKeys()[0]
const rawPayloads = await this.application.storage.getAllRawPayloads()
const itemsKeyRawPayload = rawPayloads.find((p) => p.uuid === itemsKey.uuid)
const itemsKeyPayload = new EncryptedPayload(itemsKeyRawPayload)
const authenticatedData = this.context.encryption.getEmbeddedPayloadAuthenticatedData(itemsKeyPayload)
const rootKeyParams = await this.application.encryption.getRootKeyParams()
expect(authenticatedData.kp).to.be.ok
expect(authenticatedData.kp).to.eql(rootKeyParams.getPortableValue())
expect(authenticatedData.kp.origination).to.equal(KeyParamsOrigination.PasscodeCreate)
})
it('correctly validates local passcode', async function () {
const passcode = 'foo'
await this.application.addPasscode('foo')
expect((await this.application.encryption.validatePasscode('wrong')).valid).to.equal(false)
expect((await this.application.encryption.validatePasscode(passcode)).valid).to.equal(true)
})
it('signing into 003 account should delete latest offline items key and create 003 items key', async function () {
/**
* When starting the application it will create an items key with the latest protocol version (004).
* Upon signing into an 003 account, the application should delete any neverSynced items keys,
* and create a new default items key that is the default for a given protocol version.
*/
const defaultItemsKey = await this.application.encryption.getSureDefaultItemsKey()
const latestVersion = this.application.encryption.getLatestVersion()
expect(defaultItemsKey.keyVersion).to.equal(latestVersion)
/** Register with 003 version */
await Factory.registerOldUser({
application: this.application,
email: this.email,
password: this.password,
version: ProtocolVersion.V003,
})
const itemsKeys = this.application.items.getDisplayableItemsKeys()
expect(itemsKeys.length).to.equal(1)
const newestItemsKey = itemsKeys[0]
expect(newestItemsKey.keyVersion).to.equal(ProtocolVersion.V003)
const rootKey = await this.application.encryption.getRootKey()
expect(newestItemsKey.itemsKey).to.equal(rootKey.masterKey)
expect(newestItemsKey.dataAuthenticationKey).to.equal(rootKey.dataAuthenticationKey)
})
it('reencrypts existing notes when logging into an 003 account', async function () {
await Factory.createManyMappedNotes(this.application, 10)
await Factory.registerOldUser({
application: this.application,
email: this.email,
password: this.password,
version: ProtocolVersion.V003,
})
expect(this.application.payloads.invalidPayloads.length).to.equal(0)
expect(this.application.items.getDisplayableItemsKeys().length).to.equal(1)
expect(this.application.items.getDisplayableItemsKeys()[0].dirty).to.equal(false)
/** Sign out and back in */
this.application = await Factory.signOutApplicationAndReturnNew(this.application)
await this.application.signIn(this.email, this.password, undefined, undefined, undefined, true)
expect(this.application.items.getDisplayableItemsKeys().length).to.equal(1)
expect(this.application.items.getDisplayableNotes().length).to.equal(10)
expect(this.application.payloads.invalidPayloads.length).to.equal(0)
})
it('When root key changes, all items keys must be re-encrypted', async function () {
const passcode = 'foo'
await this.application.addPasscode(passcode)
await Factory.createSyncedNote(this.application)
const itemsKeys = this.application.items.getDisplayableItemsKeys()
expect(itemsKeys.length).to.equal(1)
const originalItemsKey = itemsKeys[0]
const originalRootKey = await this.application.encryption.getRootKey()
/** Expect that we can decrypt raw payload with current root key */
const rawPayloads = await this.application.storage.getAllRawPayloads()
const itemsKeyRawPayload = rawPayloads.find((p) => p.uuid === originalItemsKey.uuid)
const itemsKeyPayload = new EncryptedPayload(itemsKeyRawPayload)
const decrypted = await this.application.encryption.decryptSplitSingle({
usesRootKey: {
items: [itemsKeyPayload],
key: originalRootKey,
},
})
expect(decrypted.errorDecrypting).to.not.be.ok
expect(decrypted.content).to.eql(originalItemsKey.content)
/** Change passcode */
Factory.handlePasswordChallenges(this.application, passcode)
await this.application.changePasscode('bar')
const newRootKey = await this.application.encryption.getRootKey()
expect(newRootKey).to.not.equal(originalRootKey)
expect(newRootKey.masterKey).to.not.equal(originalRootKey.masterKey)
/**
* Expect that originalRootKey can no longer decrypt originalItemsKey
* as items key has been re-encrypted with new root key
*/
const rawPayloads2 = await this.application.storage.getAllRawPayloads()
const itemsKeyRawPayload2 = rawPayloads2.find((p) => p.uuid === originalItemsKey.uuid)
expect(itemsKeyRawPayload2.content).to.not.equal(itemsKeyRawPayload.content)
const itemsKeyPayload2 = new EncryptedPayload(itemsKeyRawPayload2)
const decrypted2 = await this.application.encryption.decryptSplitSingle({
usesRootKey: {
items: [itemsKeyPayload2],
key: originalRootKey,
},
})
expect(decrypted2.errorDecrypting).to.equal(true)
/** Should be able to decrypt with new root key */
const decrypted3 = await this.application.encryption.decryptSplitSingle({
usesRootKey: {
items: [itemsKeyPayload2],
key: newRootKey,
},
})
expect(decrypted3.errorDecrypting).to.not.be.ok
})
it('changing account password should create new items key and encrypt items with that key', async function () {
await Factory.registerUserToApplication({
application: this.application,
email: this.email,
password: this.password,
})
const itemsKeys = this.application.items.getDisplayableItemsKeys()
expect(itemsKeys.length).to.equal(1)
const defaultItemsKey = await this.application.encryption.getSureDefaultItemsKey()
const result = await this.application.changePassword(this.password, 'foobarfoo')
expect(result.error).to.not.be.ok
expect(this.application.items.getDisplayableItemsKeys().length).to.equal(2)
const newDefaultItemsKey = await this.application.encryption.getSureDefaultItemsKey()
expect(newDefaultItemsKey.uuid).to.not.equal(defaultItemsKey.uuid)
const note = await Factory.createSyncedNote(this.application)
const payload = await this.application.encryption.encryptSplitSingle({
usesItemsKeyWithKeyLookup: {
items: [note.payload],
},
})
expect(payload.items_key_id).to.equal(newDefaultItemsKey.uuid)
})
it('changing account email should create new items key and encrypt items with that key', async function () {
const { application, email, password } = await Factory.createAndInitSimpleAppContext()
await Factory.registerUserToApplication({
application: application,
email: email,
password: password,
})
const itemsKeys = application.items.getDisplayableItemsKeys()
expect(itemsKeys.length).to.equal(1)
const defaultItemsKey = application.encryption.getSureDefaultItemsKey()
const newEmail = UuidGenerator.GenerateUuid()
const result = await application.changeEmail(newEmail, password)
expect(result.error).to.not.be.ok
expect(application.items.getDisplayableItemsKeys().length).to.equal(2)
const newDefaultItemsKey = application.encryption.getSureDefaultItemsKey()
expect(newDefaultItemsKey.uuid).to.not.equal(defaultItemsKey.uuid)
const note = await Factory.createSyncedNote(application)
const payload = await application.encryption.encryptSplitSingle({
usesItemsKeyWithKeyLookup: {
items: [note.payload],
},
})
expect(payload.items_key_id).to.equal(newDefaultItemsKey.uuid)
await Factory.safeDeinit(application)
})
it('compares root keys', async function () {
const keyParams = {}
const a1 = await CreateNewRootKey({
version: ProtocolVersion.V004,
masterKey: '2C26B46B68FFC68FF99B453C1D30413413422D706483BFA0F98A5E886266E7AE',
serverPassword: 'FCDE2B2EDBA56BF408601FB721FE9B5C338D10EE429EA04FAE5511B68FBF8FB9',
keyParams,
})
const a2 = await CreateNewRootKey({
version: ProtocolVersion.V004,
masterKey: '2C26B46B68FFC68FF99B453C1D30413413422D706483BFA0F98A5E886266E7AE',
serverPassword: 'FCDE2B2EDBA56BF408601FB721FE9B5C338D10EE429EA04FAE5511B68FBF8FB9',
keyParams,
})
const b = await CreateNewRootKey({
version: ProtocolVersion.V004,
masterKey: '2CF24DBA5FB0A30E26E83B2AC5B9E29E1B161E5C1FA7425E73043362938B9824',
serverPassword: '486EA46224D1BB4FB680F34F7C9AD96A8F24EC88BE73EA8E5A6C65260E9CB8A7',
keyParams,
})
expect(a1.compare(a2)).to.equal(true)
expect(a2.compare(a1)).to.equal(true)
expect(a1.compare(b)).to.equal(false)
expect(b.compare(a1)).to.equal(false)
})
it('loading the keychain root key should also load its key params', async function () {
await Factory.registerUserToApplication({ application: this.application })
const rootKey = await this.application.encryption.rootKeyManager.getRootKeyFromKeychain()
expect(rootKey.keyParams).to.be.ok
})
it('key params should be persisted separately and not as part of root key', async function () {
await Factory.registerUserToApplication({ application: this.application })
const rawKey = await this.application.device.getNamespacedKeychainValue(this.application.identifier)
expect(rawKey.keyParams).to.not.be.ok
const rawKeyParams = await this.application.storage.getValue(StorageKey.RootKeyParams, StorageValueModes.Nonwrapped)
expect(rawKeyParams).to.be.ok
})
it('persisted key params should exactly equal in memory rootKey.keyParams', async function () {
await Factory.registerUserToApplication({ application: this.application })
const rootKey = await this.application.encryption.getRootKey()
const rawKeyParams = await this.application.storage.getValue(StorageKey.RootKeyParams, StorageValueModes.Nonwrapped)
expect(rootKey.keyParams.content).to.eql(rawKeyParams)
})
it('key params should have expected values', async function () {
await Factory.registerUserToApplication({ application: this.application })
const keyParamsObject = await this.application.encryption.getRootKeyParams()
const keyParams = keyParamsObject.content
expect(keyParams.identifier).to.be.ok
expect(keyParams.pw_nonce).to.be.ok
expect(keyParams.version).to.equal(ProtocolVersion.V004)
expect(keyParams.created).to.be.ok
expect(keyParams.origination).to.equal(KeyParamsOrigination.Registration)
expect(keyParams.email).to.not.be.ok
expect(keyParams.pw_cost).to.not.be.ok
expect(keyParams.pw_salt).to.not.be.ok
})
it('key params obtained when signing in should have created and origination', async function () {
const email = this.email
const password = this.password
await Factory.registerUserToApplication({
application: this.application,
email,
password,
})
this.application = await Factory.signOutApplicationAndReturnNew(this.application)
await Factory.loginToApplication({
application: this.application,
email,
password,
})
const keyParamsObject = await this.application.encryption.getRootKeyParams()
const keyParams = keyParamsObject.content
expect(keyParams.created).to.be.ok
expect(keyParams.origination).to.equal(KeyParamsOrigination.Registration)
})
it('key params for 003 account should still have origination and created', async function () {
/** origination and created are new properties since 004, but they can be added retroactively
* to previous versions. They are not essential to <= 003, but are for >= 004 */
/** Register with 003 version */
await Factory.registerOldUser({
application: this.application,
email: this.email,
password: this.password,
version: ProtocolVersion.V003,
})
const keyParamsObject = await this.application.encryption.getRootKeyParams()
const keyParams = keyParamsObject.content
expect(keyParams.created).to.be.ok
expect(keyParams.origination).to.equal(KeyParamsOrigination.Registration)
})
it('encryption name should be dependent on key params version', async function () {
/** Register with 003 account */
await Factory.registerOldUser({
application: this.application,
email: this.email,
password: this.password,
version: ProtocolVersion.V003,
})
expect(await this.application.encryption.getEncryptionDisplayName()).to.equal('AES-256')
this.application = await Factory.signOutApplicationAndReturnNew(this.application)
/** Register with 004 account */
await this.application.register(this.email + 'new', this.password)
expect(await this.application.encryption.getEncryptionDisplayName()).to.equal('XChaCha20-Poly1305')
})
it('when launching app with no keychain but data, should present account recovery challenge', async function () {
/**
* On iOS (and perhaps other platforms where keychains are not included in device backups),
* when setting up a new device from restore, the keychain is deleted, but the data persists.
* We want to make sure we're prompting the user to re-authenticate their account.
*/
const id = this.application.identifier
await Factory.registerUserToApplication({
application: this.application,
email: this.email,
password: this.password,
})
/** Simulate empty keychain */
await this.application.device.clearRawKeychainValue()
const recreatedApp = await Factory.createApplicationWithFakeCrypto(id)
let totalChallenges = 0
const expectedChallenges = 1
const receiveChallenge = (challenge) => {
totalChallenges++
recreatedApp.submitValuesForChallenge(challenge, [CreateChallengeValue(challenge.prompts[0], this.password)])
}
await recreatedApp.prepareForLaunch({ receiveChallenge })
await recreatedApp.launch(true)
expect(recreatedApp.encryption.getRootKey()).to.be.ok
expect(totalChallenges).to.equal(expectedChallenges)
await Factory.safeDeinit(recreatedApp)
})
describe('changing password on 003 account while signed into 004 client', function () {
/**
* When an 004 client signs into 003 account, it creates a root key based items key.
* Then, if the 003 client changes its account password, and the 004 client
* re-authenticates, incorrect behavior (2.0.13) would be not to create a new root key based
* items key based on the new root key. The result would be that when the modified 003
* items sync to the 004 client, it can't decrypt them with its existing items key
* because its based on the old root key.
*/
it.skip('should add new items key', async function () {
this.timeout(Factory.TwentySecondTimeout * 3)
let oldClient = this.application
/** Register an 003 account */
await Factory.registerOldUser({
application: oldClient,
email: this.email,
password: this.password,
version: ProtocolVersion.V003,
})
/** Sign into account from another app */
const newClient = await Factory.createAppWithRandNamespace()
await newClient.prepareForLaunch({
receiveChallenge: (challenge) => {
/** Reauth session challenge */
newClient.submitValuesForChallenge(challenge, [
CreateChallengeValue(challenge.prompts[0], this.email),
CreateChallengeValue(challenge.prompts[1], this.password),
])
},
})
await newClient.launch()
/** Change password through session manager directly instead of application,
* as not to create any items key (to simulate 003 client behavior) */
const currentRootKey = await oldClient.encryption.computeRootKey(
this.password,
await oldClient.encryption.getRootKeyParams(),
)
const operator = this.context.operators.operatorForVersion(ProtocolVersion.V003)
const newRootKey = await operator.createRootKey(this.email, this.password)
Object.defineProperty(oldClient.legacyApi, 'apiVersion', {
get: function () {
return '20190520'
},
})
/**
* Sign in as late as possible on new client to prevent session timeouts
*/
await newClient.signIn(this.email, this.password)
await oldClient.sessions.changeCredentials({
currentServerPassword: currentRootKey.serverPassword,
newRootKey,
})
/** Re-authenticate on other app; allow challenge to complete */
await newClient.sync.sync()
await Factory.sleep(1)
/** Expect a new items key to be created based on the new root key */
expect(newClient.items.getDisplayableItemsKeys().length).to.equal(2)
await Factory.safeDeinit(newClient)
await Factory.safeDeinit(oldClient)
})
it('should add new items key from migration if pw change already happened', async function () {
this.context.anticipateConsoleError('Shared vault network errors due to not accepting JWT-based token')
this.context.anticipateConsoleError(
'Cannot find items key to use for encryption',
'No items keys being created in this test',
)
/** Register an 003 account */
await Factory.registerOldUser({
application: this.application,
email: this.email,
password: this.password,
version: ProtocolVersion.V003,
})
/** Change password through session manager directly instead of application,
* as not to create any items key (to simulate 003 client behavior) */
const currentRootKey = await this.application.encryption.computeRootKey(
this.password,
await this.application.encryption.getRootKeyParams(),
)
const operator = this.context.operators.operatorForVersion(ProtocolVersion.V003)
const newRootKeyTemplate = await operator.createRootKey(this.email, this.password)
const newRootKey = CreateNewRootKey({
...newRootKeyTemplate.content,
...{
encryptionKeyPair: {},
signingKeyPair: {},
},
})
Object.defineProperty(this.application.legacyApi, 'apiVersion', {
get: function () {
return '20190520'
},
})
/** Renew session to prevent timeouts */
this.application = await Factory.signOutAndBackIn(this.application, this.email, this.password)
await this.application.sessions.changeCredentials({
currentServerPassword: currentRootKey.serverPassword,
newRootKey,
})
await this.application.dependencies.get(TYPES.ReencryptTypeAItems).execute()
/** Note: this may result in a deadlock if features_service syncs and results in an error */
await this.application.sync.sync({ awaitAll: true })
/** Relaunch application and expect new items key to be created */
const identifier = this.application.identifier
/** Set to pre 2.0.15 version so migration runs */
await this.application.device.setRawStorageValue(`${identifier}-snjs_version`, '2.0.14')
await Factory.safeDeinit(this.application)
const refreshedApp = await Factory.createApplicationWithFakeCrypto(identifier)
await Factory.initializeApplication(refreshedApp)
/** Expect a new items key to be created based on the new root key */
expect(refreshedApp.items.getDisplayableItemsKeys().length).to.equal(2)
await Factory.safeDeinit(refreshedApp)
})
})
it('importing 003 account backup, then registering for account, should properly reconcile keys', async function () {
/**
* When importing a backup of an 003 account into an offline state, ItemsKeys imported
* will have an updated_at value, which tell our protocol service that this key has been
* synced before, which sort of "lies" to the protocol service because now it thinks it doesnt
* need to create a new items key because one has already been synced with the account.
* The corrective action was to do a final check in encryptionService.handleDownloadFirstSyncCompletion
* to ensure there exists an items key corresponding to the user's account version.
*/
const promise = this.context.awaitNextSucessfulSync()
await this.context.sync()
await promise
await this.application.items.removeAllItemsFromMemory()
expect(this.application.encryption.getSureDefaultItemsKey()).to.not.be.ok
const protocol003 = new SNProtocolOperator003(new SNWebCrypto())
const key = await protocol003.createItemsKey()
await this.application.mutator.emitItemFromPayload(
key.payload.copy({
content: {
...key.payload.content,
isDefault: true,
},
dirty: true,
/** Important to indicate that the key has been synced with a server */
updated_at: Date.now(),
}),
)
const defaultKey = this.application.encryption.getSureDefaultItemsKey()
expect(defaultKey.keyVersion).to.equal(ProtocolVersion.V003)
expect(defaultKey.uuid).to.equal(key.uuid)
await Factory.registerUserToApplication({ application: this.application })
const notePayload = Factory.createNotePayload()
expect(await this.application.encryption.itemsEncryption.keyToUseForItemEncryption(notePayload)).to.be.ok
})
it('having unsynced items keys should resync them upon download first sync completion', async function () {
await Factory.registerUserToApplication({ application: this.application })
const itemsKey = this.application.items.getDisplayableItemsKeys()[0]
await this.application.mutator.emitItemFromPayload(
itemsKey.payload.copy({
dirty: false,
updated_at: new Date(0),
deleted: false,
}),
)
await this.application.sync.sync({
mode: SyncMode.DownloadFirst,
})
const updatedKey = this.application.items.findItem(itemsKey.uuid)
expect(updatedKey.neverSynced).to.equal(false)
})
it('having key while offline then signing into account with key should only have 1 default items key', async function () {
const otherClient = await Factory.createInitAppWithFakeCrypto()
/** Invert order of keys */
otherClient.items.itemsKeyDisplayController.setDisplayOptions({ sortBy: 'dsc' })
/** On client A, create account and note */
await Factory.registerUserToApplication({
application: this.application,
email: this.email,
password: this.password,
})
await Factory.createSyncedNote(this.application)
const itemsKey = this.application.items.getItems(ContentType.TYPES.ItemsKey)[0]
/** Create another client and sign into account */
await Factory.loginToApplication({
application: otherClient,
email: this.email,
password: this.password,
})
const defaultKeys = otherClient.encryption.itemsEncryption.getItemsKeys().filter((key) => {
return key.isDefault
})
expect(defaultKeys.length).to.equal(1)
const rawPayloads = await otherClient.storage.getAllRawPayloads()
const notePayload = rawPayloads.find((p) => p.content_type === ContentType.TYPES.Note)
expect(notePayload.items_key_id).to.equal(itemsKey.uuid)
await otherClient.deinit()
})
})