forked from tutao/tutanota
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCryptoFacade.ts
849 lines (780 loc) · 37.6 KB
/
CryptoFacade.ts
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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
import {
assertNotNull,
base64ToUint8Array,
downcast,
isSameTypeRef,
isSameTypeRefByAttr,
neverNull,
ofClass,
promiseMap,
stringToUtf8Uint8Array,
TypeRef,
uint8ArrayToBase64,
} from "@tutao/tutanota-utils"
import {
AccountType,
asCryptoProtoocolVersion,
assertEnumValue,
BucketPermissionType,
CryptoProtocolVersion,
EncryptionAuthStatus,
GroupType,
PermissionType,
PublicKeyIdentifierType,
SYSTEM_GROUP_MAIL_ADDRESS,
} from "../../common/TutanotaConstants"
import { HttpMethod, resolveTypeReference } from "../../common/EntityFunctions"
import type { BucketKey, BucketPermission, GroupMembership, InstanceSessionKey, Permission, PublicKeyGetOut } from "../../entities/sys/TypeRefs.js"
import {
BucketKeyTypeRef,
BucketPermissionTypeRef,
createInstanceSessionKey,
createPublicKeyGetIn,
createUpdatePermissionKeyData,
GroupInfoTypeRef,
GroupTypeRef,
PermissionTypeRef,
PushIdentifierTypeRef,
} from "../../entities/sys/TypeRefs.js"
import {
Contact,
ContactTypeRef,
createEncryptTutanotaPropertiesData,
createInternalRecipientKeyData,
createSymEncInternalRecipientKeyData,
File,
FileTypeRef,
InternalRecipientKeyData,
Mail,
MailTypeRef,
SymEncInternalRecipientKeyData,
TutanotaPropertiesTypeRef,
} from "../../entities/tutanota/TypeRefs.js"
import { typeRefToPath } from "../rest/EntityRestClient"
import { LockedError, NotFoundError, PayloadTooLargeError, TooManyRequestsError } from "../../common/error/RestError"
import { SessionKeyNotFoundError } from "../../common/error/SessionKeyNotFoundError"
import { birthdayToIsoDate, oldBirthdayToBirthday } from "../../common/utils/BirthdayUtils"
import type { Entity, Instance, SomeEntity, TypeModel } from "../../common/EntityTypes"
import { assertWorkerOrNode } from "../../common/Env"
import type { EntityClient } from "../../common/EntityClient"
import { RestClient } from "../rest/RestClient"
import { Aes256Key, aes256RandomKey, aesEncrypt, AesKey, bitArrayToUint8Array, decryptKey, EccPublicKey, encryptKey, sha256Hash } from "@tutao/tutanota-crypto"
import { RecipientNotResolvedError } from "../../common/error/RecipientNotResolvedError"
import { IServiceExecutor } from "../../common/ServiceRequest"
import { EncryptTutanotaPropertiesService } from "../../entities/tutanota/Services"
import { PublicKeyService, UpdatePermissionKeyService } from "../../entities/sys/Services"
import { UserFacade } from "../facades/UserFacade"
import { elementIdPart, getElementId, getListId } from "../../common/utils/EntityUtils.js"
import { InstanceMapper } from "./InstanceMapper.js"
import { OwnerEncSessionKeysUpdateQueue } from "./OwnerEncSessionKeysUpdateQueue.js"
import { DefaultEntityRestCache } from "../rest/DefaultEntityRestCache.js"
import { CryptoError } from "@tutao/tutanota-crypto/error.js"
import { KeyLoaderFacade } from "../facades/KeyLoaderFacade.js"
import { encryptKeyWithVersionedKey, VersionedEncryptedKey, VersionedKey } from "./CryptoWrapper.js"
import { AsymmetricCryptoFacade, convertToVersionedPublicKeys } from "./AsymmetricCryptoFacade.js"
assertWorkerOrNode()
// Unmapped encrypted owner group instance
type UnmappedOwnerGroupInstance = {
_ownerEncSessionKey: string
_ownerKeyVersion: NumberString
_ownerGroup: Id
}
type ResolvedSessionKeys = {
resolvedSessionKeyForInstance: AesKey
instanceSessionKeys: Array<InstanceSessionKey>
}
export class CryptoFacade {
constructor(
private readonly userFacade: UserFacade,
private readonly entityClient: EntityClient,
private readonly restClient: RestClient,
private readonly serviceExecutor: IServiceExecutor,
private readonly instanceMapper: InstanceMapper,
private readonly ownerEncSessionKeysUpdateQueue: OwnerEncSessionKeysUpdateQueue,
private readonly cache: DefaultEntityRestCache | null,
private readonly keyLoaderFacade: KeyLoaderFacade,
private readonly asymmetricCryptoFacade: AsymmetricCryptoFacade,
) {}
async applyMigrationsForInstance<T>(decryptedInstance: T): Promise<T> {
const instanceType = downcast<Entity>(decryptedInstance)._type
if (isSameTypeRef(instanceType, ContactTypeRef)) {
const contact = downcast<Contact>(decryptedInstance)
try {
if (!contact.birthdayIso && contact.oldBirthdayAggregate) {
contact.birthdayIso = birthdayToIsoDate(contact.oldBirthdayAggregate)
contact.oldBirthdayAggregate = null
contact.oldBirthdayDate = null
await this.entityClient.update(contact)
} else if (!contact.birthdayIso && contact.oldBirthdayDate) {
contact.birthdayIso = birthdayToIsoDate(oldBirthdayToBirthday(contact.oldBirthdayDate))
contact.oldBirthdayDate = null
await this.entityClient.update(contact)
} else if (contact.birthdayIso && (contact.oldBirthdayAggregate || contact.oldBirthdayDate)) {
contact.oldBirthdayAggregate = null
contact.oldBirthdayDate = null
await this.entityClient.update(contact)
}
} catch (e) {
if (!(e instanceof LockedError)) {
throw e
}
}
}
return decryptedInstance
}
async resolveSessionKeyForInstance(instance: SomeEntity): Promise<AesKey | null> {
const typeModel = await resolveTypeReference(instance._type)
return this.resolveSessionKey(typeModel, instance)
}
/** Helper for the rare cases when we needed it on the client side. */
async resolveSessionKeyForInstanceBinary(instance: SomeEntity): Promise<Uint8Array | null> {
const key = await this.resolveSessionKeyForInstance(instance)
return key == null ? null : bitArrayToUint8Array(key)
}
/** Resolve a session key an {@param instance} using an already known {@param ownerKey}. */
resolveSessionKeyWithOwnerKey(instance: Record<string, any>, ownerKey: AesKey): AesKey {
let key: Uint8Array | string = instance._ownerEncSessionKey
if (typeof key === "string") {
key = base64ToUint8Array(key)
}
return decryptKey(ownerKey, key)
}
async decryptSessionKey(instance: Record<string, any>, ownerEncSessionKey: VersionedEncryptedKey): Promise<AesKey> {
const gk = await this.keyLoaderFacade.loadSymGroupKey(instance._ownerGroup, ownerEncSessionKey.encryptingKeyVersion)
return decryptKey(gk, ownerEncSessionKey.key)
}
/**
* Returns the session key for the provided type/instance:
* * null, if the instance is unencrypted
* * the decrypted _ownerEncSessionKey, if it is available
* * the public decrypted session key, otherwise
*
* @param typeModel the type model of the instance
* @param instance The unencrypted (client-side) instance or encrypted (server-side) object literal
*/
async resolveSessionKey(typeModel: TypeModel, instance: Record<string, any>): Promise<AesKey | null> {
try {
if (!typeModel.encrypted) {
return null
}
if (instance.bucketKey) {
// if we have a bucket key, then we need to cache the session keys stored in the bucket key for details, files, etc.
// we need to do this BEFORE we check the owner enc session key
const bucketKey = await this.convertBucketKeyToInstanceIfNecessary(instance.bucketKey)
const resolvedSessionKeys = await this.resolveWithBucketKey(bucketKey, instance, typeModel)
return resolvedSessionKeys.resolvedSessionKeyForInstance
} else if (instance._ownerEncSessionKey && this.userFacade.isFullyLoggedIn() && this.userFacade.hasGroup(instance._ownerGroup)) {
const gk = await this.keyLoaderFacade.loadSymGroupKey(instance._ownerGroup, Number(instance._ownerKeyVersion ?? 0))
return this.resolveSessionKeyWithOwnerKey(instance, gk)
} else if (instance.ownerEncSessionKey) {
// Likely a DataTransferType, so this is a service.
const gk = await this.keyLoaderFacade.loadSymGroupKey(this.userFacade.getGroupId(GroupType.Mail), Number(instance.ownerKeyVersion ?? 0))
return this.resolveSessionKeyWithOwnerKey(instance, gk)
} else {
// See PermissionType jsdoc for more info on permissions
const permissions = await this.entityClient.loadAll(PermissionTypeRef, instance._permissions)
return (await this.trySymmetricPermission(permissions)) ?? (await this.resolveWithPublicOrExternalPermission(permissions, instance, typeModel))
}
} catch (e) {
if (e instanceof CryptoError) {
console.log("failed to resolve session key", e)
throw new SessionKeyNotFoundError("Crypto error while resolving session key for instance " + instance._id)
} else {
throw e
}
}
}
/**
* Takes a freshly JSON-parsed, unmapped object and apply migrations as necessary
* @param typeRef
* @param data
* @return the unmapped and still encrypted instance
*/
async applyMigrations<T extends SomeEntity>(typeRef: TypeRef<T>, data: Record<string, any>): Promise<Record<string, any>> {
if (isSameTypeRef(typeRef, GroupInfoTypeRef) && data._ownerGroup == null) {
return this.applyCustomerGroupOwnershipToGroupInfo(data)
} else if (isSameTypeRef(typeRef, TutanotaPropertiesTypeRef) && data._ownerEncSessionKey == null) {
return this.encryptTutanotaProperties(data)
} else if (isSameTypeRef(typeRef, PushIdentifierTypeRef) && data._ownerEncSessionKey == null) {
return this.addSessionKeyToPushIdentifier(data)
} else {
return data
}
}
/**
* In case the given bucketKey is a literal the literal will be converted to an instance and return. In case the BucketKey is already an instance the
* instance is returned.
* @param bucketKeyInstanceOrLiteral The bucket key as literal or instance
*/
async convertBucketKeyToInstanceIfNecessary(bucketKeyInstanceOrLiteral: Record<string, any>): Promise<BucketKey> {
if (this.isLiteralInstance(bucketKeyInstanceOrLiteral)) {
// decryptAndMapToInstance is misleading here (it's not going to be decrypted), but we want to map the BucketKey aggregate and its session key from
// a literal to an instance to have the encrypted keys in binary format and not as base 64. There is actually no decryption ongoing, just
// mapToInstance.
const bucketKeyTypeModel = await resolveTypeReference(BucketKeyTypeRef)
return (await this.instanceMapper.decryptAndMapToInstance(bucketKeyTypeModel, bucketKeyInstanceOrLiteral, null)) as BucketKey
} else {
// bucket key was already decoded
return bucketKeyInstanceOrLiteral as BucketKey
}
}
public async resolveWithBucketKey(bucketKey: BucketKey, instance: Record<string, any>, typeModel: TypeModel): Promise<ResolvedSessionKeys> {
const instanceElementId = this.getElementIdFromInstance(instance)
let decryptedBucketKey: AesKey
let unencryptedSenderAuthStatus: EncryptionAuthStatus | null = null
let pqMessageSenderKey: EccPublicKey | null = null
if (bucketKey.keyGroup && bucketKey.pubEncBucketKey) {
// bucket key is encrypted with public key for internal recipient
const { decryptedAesKey, senderIdentityPubKey } = await this.asymmetricCryptoFacade.loadKeyPairAndDecryptSymKey(
bucketKey.keyGroup,
Number(bucketKey.recipientKeyVersion),
asCryptoProtoocolVersion(bucketKey.protocolVersion),
bucketKey.pubEncBucketKey,
)
decryptedBucketKey = decryptedAesKey
pqMessageSenderKey = senderIdentityPubKey
} else if (bucketKey.groupEncBucketKey) {
// received as secure external recipient or reply from secure external sender
let keyGroup
const groupKeyVersion = Number(bucketKey.recipientKeyVersion)
if (bucketKey.keyGroup) {
// 1. Uses when receiving confidential replies from external users.
// 2. legacy code path for old external clients that used to encrypt bucket keys with user group keys.
keyGroup = bucketKey.keyGroup
} else {
// by default, we try to decrypt the bucket key with the ownerGroupKey (e.g. secure external recipient)
keyGroup = neverNull(instance._ownerGroup)
}
decryptedBucketKey = await this.resolveWithGroupReference(keyGroup, groupKeyVersion, bucketKey.groupEncBucketKey)
unencryptedSenderAuthStatus = EncryptionAuthStatus.AES_NO_AUTHENTICATION
} else {
throw new SessionKeyNotFoundError(`encrypted bucket key not set on instance ${typeModel.name}`)
}
const resolvedSessionKeys = await this.collectAllInstanceSessionKeysAndAuthenticate(
bucketKey,
decryptedBucketKey,
instanceElementId,
instance,
typeModel,
unencryptedSenderAuthStatus,
pqMessageSenderKey,
)
await this.ownerEncSessionKeysUpdateQueue.updateInstanceSessionKeys(resolvedSessionKeys.instanceSessionKeys, typeModel)
// for symmetrically encrypted instances _ownerEncSessionKey is sent from the server.
// in this case it is not yet and we need to set it because the rest of the app expects it.
const groupKey = await this.keyLoaderFacade.getCurrentSymGroupKey(instance._ownerGroup) // get current key for encrypting
this.setOwnerEncSessionKeyUnmapped(
instance as UnmappedOwnerGroupInstance,
encryptKeyWithVersionedKey(groupKey, resolvedSessionKeys.resolvedSessionKeyForInstance),
)
return resolvedSessionKeys
}
/**
* Calculates the SHA-256 checksum of a string value as UTF-8 bytes and returns it as a base64-encoded string
*/
public async sha256(value: string): Promise<string> {
return uint8ArrayToBase64(sha256Hash(stringToUtf8Uint8Array(value)))
}
/**
* Decrypts the given encrypted bucket key with the group key of the given group. In case the current user is not
* member of the key group the function tries to resolve the group key using the adminEncGroupKey.
* This is necessary for resolving the BucketKey when receiving a reply from an external Mailbox.
* @param keyGroup The group that holds the encryption key.
* @param groupKeyVersion the version of the key from the keyGroup
* @param groupEncBucketKey The group key encrypted bucket key.
*/
private async resolveWithGroupReference(keyGroup: Id, groupKeyVersion: number, groupEncBucketKey: Uint8Array): Promise<AesKey> {
if (this.userFacade.hasGroup(keyGroup)) {
// the logged-in user (most likely external) is a member of that group. Then we have the group key from the memberships
const groupKey = await this.keyLoaderFacade.loadSymGroupKey(keyGroup, groupKeyVersion)
return decryptKey(groupKey, groupEncBucketKey)
} else {
// internal user receiving a mail from secure external:
// internal user group key -> external user group key -> external mail group key -> bucket key
const externalMailGroupId = keyGroup
const externalMailGroupKeyVersion = groupKeyVersion
const externalMailGroup = await this.entityClient.load(GroupTypeRef, externalMailGroupId)
const externalUserGroupdId = externalMailGroup.admin
if (!externalUserGroupdId) {
throw new SessionKeyNotFoundError("no admin group on key group: " + externalMailGroupId)
}
const externalUserGroupKeyVersion = Number(externalMailGroup.adminGroupKeyVersion ?? 0)
const externalUserGroup = await this.entityClient.load(GroupTypeRef, externalUserGroupdId)
const internalUserGroupId = externalUserGroup.admin
const internalUserGroupKeyVersion = Number(externalUserGroup.adminGroupKeyVersion ?? 0)
if (!(internalUserGroupId && this.userFacade.hasGroup(internalUserGroupId))) {
throw new SessionKeyNotFoundError("no admin group or no membership of admin group: " + internalUserGroupId)
}
const internalUserGroupKey = await this.keyLoaderFacade.loadSymGroupKey(internalUserGroupId, internalUserGroupKeyVersion)
const currentExternalUserGroupKey = decryptKey(internalUserGroupKey, assertNotNull(externalUserGroup.adminGroupEncGKey))
const externalUserGroupKey = await this.keyLoaderFacade.loadSymGroupKey(externalUserGroupdId, externalUserGroupKeyVersion, {
object: currentExternalUserGroupKey,
version: Number(externalUserGroup.groupKeyVersion),
})
const currentExternalMailGroupKey = decryptKey(externalUserGroupKey, assertNotNull(externalMailGroup.adminGroupEncGKey))
const externalMailGroupKey = await this.keyLoaderFacade.loadSymGroupKey(externalMailGroupId, externalMailGroupKeyVersion, {
object: currentExternalMailGroupKey,
version: Number(externalMailGroup.groupKeyVersion),
})
return decryptKey(externalMailGroupKey, groupEncBucketKey)
}
}
private async addSessionKeyToPushIdentifier(data: Record<string, any>): Promise<Record<string, any>> {
const userGroupKey = this.userFacade.getCurrentUserGroupKey()
// set sessionKey for allowing encryption when old instance (< v43) is updated
const typeModel = await resolveTypeReference(PushIdentifierTypeRef)
await this.updateOwnerEncSessionKey(typeModel, data, userGroupKey, aes256RandomKey())
return data
}
private async encryptTutanotaProperties(data: Record<string, any>): Promise<Record<string, any>> {
const userGroupKey = this.userFacade.getCurrentUserGroupKey()
// EncryptTutanotaPropertiesService could be removed and replaced with a Migration that writes the key
const groupEncSessionKey = encryptKeyWithVersionedKey(userGroupKey, aes256RandomKey())
this.setOwnerEncSessionKeyUnmapped(data as UnmappedOwnerGroupInstance, groupEncSessionKey, this.userFacade.getUserGroupId())
const migrationData = createEncryptTutanotaPropertiesData({
properties: data._id,
symKeyVersion: String(groupEncSessionKey.encryptingKeyVersion),
symEncSessionKey: groupEncSessionKey.key,
})
await this.serviceExecutor.post(EncryptTutanotaPropertiesService, migrationData)
return data
}
private async applyCustomerGroupOwnershipToGroupInfo(data: Record<string, any>): Promise<Record<string, any>> {
const customerGroupMembership = assertNotNull(
this.userFacade.getLoggedInUser().memberships.find((g: GroupMembership) => g.groupType === GroupType.Customer),
)
const listPermissions = await this.entityClient.loadAll(PermissionTypeRef, data._id[0])
const customerGroupPermission = listPermissions.find((p) => p.group === customerGroupMembership.group)
if (!customerGroupPermission) throw new SessionKeyNotFoundError("Permission not found, could not apply OwnerGroup migration")
const customerGroupKeyVersion = Number(customerGroupPermission.symKeyVersion ?? 0)
const customerGroupKey = await this.keyLoaderFacade.loadSymGroupKey(customerGroupMembership.group, customerGroupKeyVersion)
const versionedCustomerGroupKey = { object: customerGroupKey, version: customerGroupKeyVersion }
const listKey = decryptKey(customerGroupKey, assertNotNull(customerGroupPermission.symEncSessionKey))
const groupInfoSk = decryptKey(listKey, base64ToUint8Array(data._listEncSessionKey))
this.setOwnerEncSessionKeyUnmapped(
data as UnmappedOwnerGroupInstance,
encryptKeyWithVersionedKey(versionedCustomerGroupKey, groupInfoSk),
customerGroupMembership.group,
)
return data
}
private setOwnerEncSessionKeyUnmapped(unmappedInstance: UnmappedOwnerGroupInstance, key: VersionedEncryptedKey, ownerGroup?: Id) {
unmappedInstance._ownerEncSessionKey = uint8ArrayToBase64(key.key)
unmappedInstance._ownerKeyVersion = key.encryptingKeyVersion.toString()
if (ownerGroup) {
unmappedInstance._ownerGroup = ownerGroup
}
}
private setOwnerEncSessionKey(instance: Instance, key: VersionedEncryptedKey) {
instance._ownerEncSessionKey = key.key
instance._ownerKeyVersion = key.encryptingKeyVersion.toString()
}
/**
* @return Whether the {@param elementOrLiteral} is a unmapped type, as used in JSON for transport or if it's a runtime representation of a type.
*/
private isLiteralInstance(elementOrLiteral: Record<string, any>): boolean {
return typeof elementOrLiteral._type === "undefined"
}
private async trySymmetricPermission(listPermissions: Permission[]): Promise<AesKey | null> {
const symmetricPermission: Permission | null =
listPermissions.find(
(p) =>
(p.type === PermissionType.Public_Symmetric || p.type === PermissionType.Symmetric) &&
p._ownerGroup &&
this.userFacade.hasGroup(p._ownerGroup),
) ?? null
if (symmetricPermission) {
const gk = await this.keyLoaderFacade.loadSymGroupKey(
assertNotNull(symmetricPermission._ownerGroup),
Number(symmetricPermission._ownerKeyVersion ?? 0),
)
return decryptKey(gk, assertNotNull(symmetricPermission._ownerEncSessionKey))
} else {
return null
}
}
/**
* Resolves the session key for the provided instance and collects all other instances'
* session keys in order to update them.
*/
private async collectAllInstanceSessionKeysAndAuthenticate(
bucketKey: BucketKey,
decBucketKey: number[],
instanceElementId: string,
instance: Record<string, any>,
typeModel: TypeModel,
encryptionAuthStatus: EncryptionAuthStatus | null,
pqMessageSenderKey: EccPublicKey | null,
): Promise<ResolvedSessionKeys> {
let resolvedSessionKeyForInstance: AesKey | undefined = undefined
const instanceSessionKeys = await promiseMap(bucketKey.bucketEncSessionKeys, async (instanceSessionKey) => {
const decryptedSessionKey = decryptKey(decBucketKey, instanceSessionKey.symEncSessionKey)
const groupKey = await this.keyLoaderFacade.getCurrentSymGroupKey(instance._ownerGroup)
const ownerEncSessionKey = encryptKeyWithVersionedKey(groupKey, decryptedSessionKey)
const instanceSessionKeyWithOwnerEncSessionKey = createInstanceSessionKey(instanceSessionKey)
if (instanceElementId == instanceSessionKey.instanceId) {
resolvedSessionKeyForInstance = decryptedSessionKey
// we can only authenticate once we have the session key
// because we need to check if the confidential flag is set, which is encrypted still
// we need to do it here at the latest because we must write the flag when updating the session key on the instance
await this.authenticateMainInstance(
typeModel,
encryptionAuthStatus,
pqMessageSenderKey,
bucketKey.protocolVersion === CryptoProtocolVersion.TUTA_CRYPT ? Number(bucketKey.senderKeyVersion ?? 0) : null,
instance,
resolvedSessionKeyForInstance,
instanceSessionKeyWithOwnerEncSessionKey,
decryptedSessionKey,
)
}
instanceSessionKeyWithOwnerEncSessionKey.symEncSessionKey = ownerEncSessionKey.key
instanceSessionKeyWithOwnerEncSessionKey.symKeyVersion = String(ownerEncSessionKey.encryptingKeyVersion)
return instanceSessionKeyWithOwnerEncSessionKey
})
if (resolvedSessionKeyForInstance) {
return { resolvedSessionKeyForInstance, instanceSessionKeys }
} else {
throw new SessionKeyNotFoundError("no session key for instance " + instance._id)
}
}
private async authenticateMainInstance(
typeModel: TypeModel,
encryptionAuthStatus:
| EncryptionAuthStatus
| null
| EncryptionAuthStatus.RSA_NO_AUTHENTICATION
| EncryptionAuthStatus.TUTACRYPT_AUTHENTICATION_SUCCEEDED
| EncryptionAuthStatus.TUTACRYPT_AUTHENTICATION_FAILED
| EncryptionAuthStatus.AES_NO_AUTHENTICATION,
pqMessageSenderKey: Uint8Array | null,
pqMessageSenderKeyVersion: number | null,
instance: Record<string, any>,
resolvedSessionKeyForInstance: number[],
instanceSessionKeyWithOwnerEncSessionKey: InstanceSessionKey,
decryptedSessionKey: number[],
) {
// we only authenticate mail instances
const isMailInstance = isSameTypeRefByAttr(MailTypeRef, typeModel.app, typeModel.name)
if (isMailInstance) {
if (!encryptionAuthStatus) {
if (!pqMessageSenderKey) {
encryptionAuthStatus = EncryptionAuthStatus.RSA_NO_AUTHENTICATION
} else {
const mail = this.isLiteralInstance(instance)
? ((await this.instanceMapper.decryptAndMapToInstance(typeModel, instance, resolvedSessionKeyForInstance)) as Mail)
: (instance as Mail)
const senderMailAddress = mail.confidential ? mail.sender.address : SYSTEM_GROUP_MAIL_ADDRESS
encryptionAuthStatus = await this.tryAuthenticateSenderOfMainInstance(senderMailAddress, pqMessageSenderKey, pqMessageSenderKeyVersion)
}
}
instanceSessionKeyWithOwnerEncSessionKey.encryptionAuthStatus = aesEncrypt(decryptedSessionKey, stringToUtf8Uint8Array(encryptionAuthStatus))
}
}
private async tryAuthenticateSenderOfMainInstance(senderMailAddress: string, pqMessageSenderKey: Uint8Array, pqMessageSenderKeyVersion: number | null) {
try {
return await this.asymmetricCryptoFacade.authenticateSender(
{
identifier: senderMailAddress,
identifierType: PublicKeyIdentifierType.MAIL_ADDRESS,
},
pqMessageSenderKey,
assertNotNull(pqMessageSenderKeyVersion),
)
} catch (e) {
// we do not want to fail mail decryption here, e.g. in case an alias was removed we would get a permanent NotFoundError.
// in those cases we will just show a warning banner but still want to display the mail
console.error("Could not authenticate sender", e)
return EncryptionAuthStatus.TUTACRYPT_AUTHENTICATION_FAILED
}
}
private async resolveWithPublicOrExternalPermission(listPermissions: Permission[], instance: Record<string, any>, typeModel: TypeModel): Promise<AesKey> {
const pubOrExtPermission = listPermissions.find((p) => p.type === PermissionType.Public || p.type === PermissionType.External) ?? null
if (pubOrExtPermission == null) {
const typeName = `${typeModel.app}/${typeModel.name}`
throw new SessionKeyNotFoundError(`could not find permission for instance of type ${typeName} with id ${this.getElementIdFromInstance(instance)}`)
}
const bucketPermissions = await this.entityClient.loadAll(BucketPermissionTypeRef, assertNotNull(pubOrExtPermission.bucket).bucketPermissions)
const bucketPermission = bucketPermissions.find(
(bp) => (bp.type === BucketPermissionType.Public || bp.type === BucketPermissionType.External) && pubOrExtPermission._ownerGroup === bp._ownerGroup,
)
// find the bucket permission with the same group as the permission and public type
if (bucketPermission == null) {
throw new SessionKeyNotFoundError("no corresponding bucket permission found")
}
if (bucketPermission.type === BucketPermissionType.External) {
return this.decryptWithExternalBucket(bucketPermission, pubOrExtPermission, instance)
} else {
return this.decryptWithPublicBucketWithoutAuthentication(bucketPermission, instance, pubOrExtPermission, typeModel)
}
}
private async decryptWithExternalBucket(
bucketPermission: BucketPermission,
pubOrExtPermission: Permission,
instance: Record<string, any>,
): Promise<AesKey> {
let bucketKey
if (bucketPermission.ownerEncBucketKey != null) {
const ownerGroupKey = await this.keyLoaderFacade.loadSymGroupKey(
neverNull(bucketPermission._ownerGroup),
Number(bucketPermission.ownerKeyVersion ?? 0),
)
bucketKey = decryptKey(ownerGroupKey, bucketPermission.ownerEncBucketKey)
} else if (bucketPermission.symEncBucketKey) {
// legacy case: for very old email sent to external user we used symEncBucketKey on the bucket permission.
// The bucket key is encrypted with the user group key of the external user.
// We maintain this code as we still have some old BucketKeys in some external mailboxes.
// Can be removed if we finished mail details migration or when we do cleanup of external mailboxes.
const userGroupKey = await this.keyLoaderFacade.loadSymUserGroupKey(Number(bucketPermission.symKeyVersion ?? 0))
bucketKey = decryptKey(userGroupKey, bucketPermission.symEncBucketKey)
} else {
throw new SessionKeyNotFoundError(
`BucketEncSessionKey is not defined for Permission ${pubOrExtPermission._id.toString()} (Instance: ${JSON.stringify(instance)})`,
)
}
return decryptKey(bucketKey, neverNull(pubOrExtPermission.bucketEncSessionKey))
}
private async decryptWithPublicBucketWithoutAuthentication(
bucketPermission: BucketPermission,
instance: Record<string, any>,
pubOrExtPermission: Permission,
typeModel: TypeModel,
): Promise<AesKey> {
const pubEncBucketKey = bucketPermission.pubEncBucketKey
if (pubEncBucketKey == null) {
throw new SessionKeyNotFoundError(
`PubEncBucketKey is not defined for BucketPermission ${bucketPermission._id.toString()} (Instance: ${JSON.stringify(instance)})`,
)
}
const bucketEncSessionKey = pubOrExtPermission.bucketEncSessionKey
if (bucketEncSessionKey == null) {
throw new SessionKeyNotFoundError(
`BucketEncSessionKey is not defined for Permission ${pubOrExtPermission._id.toString()} (Instance: ${JSON.stringify(instance)})`,
)
}
const { decryptedAesKey } = await this.asymmetricCryptoFacade.loadKeyPairAndDecryptSymKey(
bucketPermission.group,
Number(bucketPermission.pubKeyVersion ?? 0),
asCryptoProtoocolVersion(bucketPermission.protocolVersion),
pubEncBucketKey,
)
const sk = decryptKey(decryptedAesKey, bucketEncSessionKey)
if (bucketPermission._ownerGroup) {
// is not defined for some old AccountingInfos
let bucketPermissionOwnerGroupKey = await this.keyLoaderFacade.getCurrentSymGroupKey(neverNull(bucketPermission._ownerGroup)) // get current key for encrypting
await this.updateWithSymPermissionKey(typeModel, instance, pubOrExtPermission, bucketPermission, bucketPermissionOwnerGroupKey, sk).catch(
ofClass(NotFoundError, () => {
console.log("w> could not find instance to update permission")
}),
)
}
return sk
}
/**
* Returns the session key for the provided service response:
* * null, if the instance is unencrypted
* * the decrypted _ownerPublicEncSessionKey, if it is available
* @param instance The unencrypted (client-side) or encrypted (server-side) instance
*
*/
async resolveServiceSessionKey(instance: Record<string, any>): Promise<Aes256Key | null> {
if (instance._ownerPublicEncSessionKey) {
// we assume the server uses the current key pair of the recipient
const keypair = await this.keyLoaderFacade.loadCurrentKeyPair(instance._ownerGroup)
// we do not authenticate as we could remove data transfer type encryption altogether and only rely on tls
return (
await this.asymmetricCryptoFacade.decryptSymKeyWithKeyPair(
keypair.object,
assertEnumValue(CryptoProtocolVersion, instance._publicCryptoProtocolVersion),
base64ToUint8Array(instance._ownerPublicEncSessionKey),
)
).decryptedAesKey
}
return null
}
/**
* Creates a new _ownerEncSessionKey and assigns it to the provided entity
* the entity must already have an _ownerGroup
* @returns the generated key
*/
async setNewOwnerEncSessionKey(model: TypeModel, entity: Record<string, any>, keyToEncryptSessionKey?: VersionedKey): Promise<AesKey | null> {
if (!entity._ownerGroup) {
throw new Error(`no owner group set ${JSON.stringify(entity)}`)
}
if (model.encrypted) {
if (entity._ownerEncSessionKey) {
throw new Error(`ownerEncSessionKey already set ${JSON.stringify(entity)}`)
}
const sessionKey = aes256RandomKey()
const effectiveKeyToEncryptSessionKey = keyToEncryptSessionKey ?? (await this.keyLoaderFacade.getCurrentSymGroupKey(entity._ownerGroup))
const encryptedSessionKey = encryptKeyWithVersionedKey(effectiveKeyToEncryptSessionKey, sessionKey)
this.setOwnerEncSessionKey(entity as Instance, encryptedSessionKey)
return sessionKey
} else {
return null
}
}
async encryptBucketKeyForInternalRecipient(
senderUserGroupId: Id,
bucketKey: AesKey,
recipientMailAddress: string,
notFoundRecipients: Array<string>,
): Promise<InternalRecipientKeyData | SymEncInternalRecipientKeyData | null> {
const keyData = createPublicKeyGetIn({
identifier: recipientMailAddress,
identifierType: PublicKeyIdentifierType.MAIL_ADDRESS,
version: null,
})
try {
const publicKeyGetOut = await this.serviceExecutor.get(PublicKeyService, keyData)
// We do not create any key data in case there is one not found recipient, but we want to
// collect ALL not found recipients when iterating a recipient list.
if (notFoundRecipients.length !== 0) {
return null
}
const isExternalSender = this.userFacade.getUser()?.accountType === AccountType.EXTERNAL
// we only encrypt symmetric as external sender if the recipient supports tuta-crypt.
// Clients need to support symmetric decryption from external users. We can always encrypt symmetricly when old clients are deactivated that don't support tuta-crypt.
if (publicKeyGetOut.pubKyberKey && isExternalSender) {
return this.createSymEncInternalRecipientKeyData(recipientMailAddress, bucketKey)
} else {
return this.createPubEncInternalRecipientKeyData(bucketKey, recipientMailAddress, publicKeyGetOut, senderUserGroupId)
}
} catch (e) {
if (e instanceof NotFoundError) {
notFoundRecipients.push(recipientMailAddress)
return null
} else if (e instanceof TooManyRequestsError) {
throw new RecipientNotResolvedError("")
} else {
throw e
}
}
}
private async createPubEncInternalRecipientKeyData(bucketKey: AesKey, recipientMailAddress: string, publicKeyGetOut: PublicKeyGetOut, senderGroupId: Id) {
const recipientPublicKeys = convertToVersionedPublicKeys(publicKeyGetOut)
const pubEncBucketKey = await this.asymmetricCryptoFacade.asymEncryptSymKey(bucketKey, recipientPublicKeys, senderGroupId)
return createInternalRecipientKeyData({
mailAddress: recipientMailAddress,
pubEncBucketKey: pubEncBucketKey.pubEncSymKeyBytes,
recipientKeyVersion: pubEncBucketKey.recipientKeyVersion.toString(),
senderKeyVersion: pubEncBucketKey.senderKeyVersion != null ? pubEncBucketKey.senderKeyVersion.toString() : null,
protocolVersion: pubEncBucketKey.cryptoProtocolVersion,
})
}
private async createSymEncInternalRecipientKeyData(recipientMailAddress: string, bucketKey: AesKey) {
const keyGroup = this.userFacade.getGroupId(GroupType.Mail)
const externalMailGroupKey = await this.keyLoaderFacade.getCurrentSymGroupKey(keyGroup)
return createSymEncInternalRecipientKeyData({
mailAddress: recipientMailAddress,
symEncBucketKey: encryptKey(externalMailGroupKey.object, bucketKey),
keyGroup,
symKeyVersion: String(externalMailGroupKey.version),
})
}
/**
* Updates the given public permission with the given symmetric key for faster access if the client is the leader and otherwise does nothing.
* @param typeModel The type model of the instance
* @param instance The unencrypted (client-side) or encrypted (server-side) instance
* @param permission The permission.
* @param bucketPermission The bucket permission.
* @param permissionOwnerGroupKey The symmetric group key for the owner group on the permission.
* @param sessionKey The symmetric session key.
*/
private async updateWithSymPermissionKey(
typeModel: TypeModel,
instance: Record<string, any>,
permission: Permission,
bucketPermission: BucketPermission,
permissionOwnerGroupKey: VersionedKey,
sessionKey: AesKey,
): Promise<void> {
if (!this.isLiteralInstance(instance) || !this.userFacade.isLeader()) {
// do not update the session key in case of an unencrypted (client-side) instance
// or in case we are not the leader client
return
}
if (!instance._ownerEncSessionKey && permission._ownerGroup === instance._ownerGroup) {
return this.updateOwnerEncSessionKey(typeModel, instance, permissionOwnerGroupKey, sessionKey)
} else {
// instances shared via permissions (e.g. body)
const encryptedKey = encryptKeyWithVersionedKey(permissionOwnerGroupKey, sessionKey)
let updateService = createUpdatePermissionKeyData({
ownerKeyVersion: String(encryptedKey.encryptingKeyVersion),
ownerEncSessionKey: encryptedKey.key,
permission: permission._id,
bucketPermission: bucketPermission._id,
})
await this.serviceExecutor.post(UpdatePermissionKeyService, updateService)
}
}
/**
* Resolves the ownerEncSessionKey of a mail. This might be needed if it wasn't updated yet
* by the OwnerEncSessionKeysUpdateQueue but the file is already downloaded.
* @param mainInstance the instance that has the bucketKey
* @param childInstances the files that belong to the mainInstance
*/
async enforceSessionKeyUpdateIfNeeded(mainInstance: Record<string, any>, childInstances: File[]): Promise<File[]> {
if (!childInstances.some((f) => f._ownerEncSessionKey == null)) {
return childInstances
}
const typeModel = await resolveTypeReference(mainInstance._type)
const outOfSyncInstances = childInstances.filter((f) => f._ownerEncSessionKey == null)
if (mainInstance.bucketKey) {
// invoke updateSessionKeys service in case a bucket key is still available
const bucketKey = await this.convertBucketKeyToInstanceIfNecessary(mainInstance.bucketKey)
const resolvedSessionKeys = await this.resolveWithBucketKey(bucketKey, mainInstance, typeModel)
await this.ownerEncSessionKeysUpdateQueue.postUpdateSessionKeysService(resolvedSessionKeys.instanceSessionKeys)
} else {
console.warn("files are out of sync refreshing", outOfSyncInstances.map((f) => f._id).join(", "))
}
for (const childInstance of outOfSyncInstances) {
await this.cache?.deleteFromCacheIfExists(FileTypeRef, getListId(childInstance), getElementId(childInstance))
}
// we have a caching entity client, so this re-inserts the deleted instances
return await this.entityClient.loadMultiple(
FileTypeRef,
getListId(childInstances[0]),
childInstances.map((childInstance) => getElementId(childInstance)),
)
}
private updateOwnerEncSessionKey(typeModel: TypeModel, instance: Record<string, any>, ownerGroupKey: VersionedKey, sessionKey: AesKey): Promise<void> {
this.setOwnerEncSessionKeyUnmapped(instance as UnmappedOwnerGroupInstance, encryptKeyWithVersionedKey(ownerGroupKey, sessionKey))
// we have to call the rest client directly because instance is still the encrypted server-side version
const path = typeRefToPath(new TypeRef(typeModel.app, typeModel.name)) + "/" + (instance._id instanceof Array ? instance._id.join("/") : instance._id)
const headers = this.userFacade.createAuthHeaders()
headers.v = typeModel.version
return this.restClient
.request(path, HttpMethod.PUT, {
headers,
body: JSON.stringify(instance),
queryParams: { updateOwnerEncSessionKey: "true" },
})
.catch(
ofClass(PayloadTooLargeError, (e) => {
console.log("Could not update owner enc session key - PayloadTooLargeError", e)
}),
)
}
private getElementIdFromInstance(instance: Record<string, any>): Id {
if (typeof instance._id === "string") {
return instance._id
} else {
const idTuple = instance._id as IdTuple
return elementIdPart(idTuple)
}
}
}
if (!("toJSON" in Error.prototype)) {
Object.defineProperty(Error.prototype as any, "toJSON", {
value: function () {
const alt: Record<string, any> = {}
for (let key of Object.getOwnPropertyNames(this)) {
alt[key] = this[key]
}
return alt
},
configurable: true,
writable: true,
})
}