@@ -48,7 +48,6 @@ public class E2EIKeyPackageRotator: E2EIKeyPackageRotating {
48
48
private let coreCryptoProvider : CoreCryptoProviderProtocol
49
49
private let conversationEventProcessor : ConversationEventProcessorProtocol
50
50
private let context : NSManagedObjectContext
51
- private let commitSender : CommitSending
52
51
private let newKeyPackageCount : UInt32 = 100
53
52
private let featureRepository : FeatureRepositoryInterface
54
53
private let onNewCRLsDistributionPointsSubject : PassthroughSubject < CRLsDistributionPoints , Never >
@@ -66,17 +65,12 @@ public class E2EIKeyPackageRotator: E2EIKeyPackageRotating {
66
65
conversationEventProcessor: ConversationEventProcessorProtocol ,
67
66
context: NSManagedObjectContext ,
68
67
onNewCRLsDistributionPointsSubject: PassthroughSubject < CRLsDistributionPoints , Never > ,
69
- commitSender: CommitSending ? = nil ,
70
68
featureRepository: FeatureRepositoryInterface
71
69
) {
72
70
self . coreCryptoProvider = coreCryptoProvider
73
71
self . conversationEventProcessor = conversationEventProcessor
74
72
self . context = context
75
73
self . onNewCRLsDistributionPointsSubject = onNewCRLsDistributionPointsSubject
76
- self . commitSender = commitSender ?? CommitSender (
77
- coreCryptoProvider: coreCryptoProvider,
78
- notificationContext: context. notificationContext
79
- )
80
74
self . featureRepository = featureRepository
81
75
}
82
76
@@ -93,76 +87,81 @@ public class E2EIKeyPackageRotator: E2EIKeyPackageRotating {
93
87
guard let enrollment = enrollment as? E2eiEnrollment else {
94
88
throw Error . invalidIdentity
95
89
}
90
+
91
+ let crlNewDistributionPoints = try await coreCrypto. perform { context in
92
+ try await context. saveX509Credential (
93
+ enrollment: enrollment,
94
+ certificateChain: certificateChain
95
+ )
96
+ }
97
+
98
+ try await replaceKeyPackages ( )
99
+ try await replaceCredentialsInExistingConversations ( )
96
100
97
- // TODO: jacob update iterate over all conversation with e2eiRotate
98
- // // Get the rotate bundle from core crypto
99
- // let rotateBundle = try await coreCrypto.perform {
100
- // try await $0.e2eiRotateAll(
101
- // enrollment: enrollment,
102
- // certificateChain: certificateChain,
103
- // newKeyPackagesCount: newKeyPackageCount
104
- // )
105
- // }
106
- //
107
- // guard !rotateBundle.commits.isEmpty else {
108
- // // TODO: [WPB-6281] [jacob] remove this guard when implementing
109
- // return
110
- // }
111
- //
112
- // // Replace the key packages with the ones including the certificate
113
- // try await replaceKeyPackages(rotateBundle: rotateBundle)
114
- //
115
- // // Send migration commits after key packages rotations
116
- // for (groupID, commit) in rotateBundle.commits {
117
- // do {
118
- // try await migrateConversation(with: groupID, commit: commit)
119
- // } catch {
120
- // WireLogger.e2ei.warn("failed to rotate keys for group: \(String(describing: error))")
121
- // }
122
- // }
123
- //
124
- // // Publish new certificate revocation lists (CRLs) distribution points
125
- // if let newDistributionPoints = CRLsDistributionPoints(from: rotateBundle.crlNewDistributionPoints) {
126
- // onNewCRLsDistributionPointsSubject.send(newDistributionPoints)
127
- // }
101
+ // Publish new certificate revocation lists (CRLs) distribution points
102
+ if let newDistributionPoints = CRLsDistributionPoints ( from: crlNewDistributionPoints) {
103
+ onNewCRLsDistributionPointsSubject. send ( newDistributionPoints)
104
+ }
128
105
}
129
106
130
107
// MARK: - Helpers
131
-
132
- // TODO: jacob update iterate over all conversation with e2eiRotate
133
- // private func replaceKeyPackages(rotateBundle: RotateBundle) async throws {
134
- //
135
- // guard let clientID = await context.perform({ [self] in
136
- // ZMUser.selfUser(in: context).selfClient()?.remoteIdentifier
137
- // }) else {
138
- // throw Error.noSelfClient
139
- // }
140
- //
141
- // let newKeyPackages = rotateBundle.newKeyPackages.map { $0.base64String() }
142
- // let mlsConfig = await featureRepository.fetchMLS().config
143
- // guard let ciphersuite = MLSCipherSuite(rawValue: mlsConfig.defaultCipherSuite.rawValue) else {
144
- // throw Error.invalidCiphersuite
145
- // }
146
- // var action = ReplaceSelfMLSKeyPackagesAction(
147
- // clientID: clientID,
148
- // keyPackages: newKeyPackages,
149
- // ciphersuite: ciphersuite
150
- // )
151
- // try await action.perform(in: context.notificationContext)
152
- // }
153
-
154
- private func migrateConversation( with groupID: String , commit: CommitBundle ) async throws {
155
- guard let groupData = groupID. zmHexDecodedData ( ) else {
156
- throw Error . invalidGroupID
108
+
109
+ private func replaceCredentialsInExistingConversations( ) async throws {
110
+ let mlsConversationsToMigrate = try await context. perform ( {
111
+ var mlsGroupIDs = try ZMConversation . fetchConversationsWithMLSGroupStatus (
112
+ mlsGroupStatus: . ready,
113
+ in: self . context
114
+ ) . compactMap { $0. mlsGroupID }
115
+
116
+ if let selfMLSGroupID = ZMConversation . fetchSelfMLSConversation ( in: self . context) ? . mlsGroupID {
117
+ mlsGroupIDs. append ( selfMLSGroupID)
118
+ }
119
+
120
+ return mlsGroupIDs
121
+ } )
122
+
123
+ try await coreCrypto. perform { context in
124
+ for groupID in mlsConversationsToMigrate {
125
+ do {
126
+ try await context. e2eiRotate ( conversationId: groupID. data)
127
+ } catch {
128
+ WireLogger . e2ei. warn ( " failed to rotate keys for group \( groupID. safeForLoggingDescription) : \( String ( describing: error) ) " )
129
+ }
130
+ }
157
131
}
158
-
159
- let groupID = MLSGroupID ( groupData)
160
- let events = try await commitSender. sendCommitBundle (
161
- commit,
162
- for: groupID
163
- )
164
-
165
- await conversationEventProcessor. processConversationEvents ( events)
166
132
}
167
133
134
+ private func replaceKeyPackages( ) async throws {
135
+ let mlsConfig = await featureRepository. fetchMLS ( ) . config
136
+
137
+ guard let clientID = await context. perform ( { [ self ] in
138
+ ZMUser . selfUser ( in: context) . selfClient ( ) ? . remoteIdentifier
139
+ } ) else {
140
+ throw Error . noSelfClient
141
+ }
142
+
143
+ guard let ciphersuite = MLSCipherSuite ( rawValue: mlsConfig. defaultCipherSuite. rawValue) else {
144
+ throw Error . invalidCiphersuite
145
+ }
146
+
147
+ try await coreCrypto. perform { coreCryptoContext in
148
+ let rawCiphersuite = UInt16 ( ciphersuite. rawValue)
149
+ let newKeyPackages = try await coreCryptoContext. clientKeypackages (
150
+ ciphersuite: rawCiphersuite,
151
+ credentialType: . x509,
152
+ amountRequested: self . newKeyPackageCount
153
+ ) . map ( { $0. base64String ( ) } )
154
+
155
+ var action = ReplaceSelfMLSKeyPackagesAction (
156
+ clientID: clientID,
157
+ keyPackages: newKeyPackages,
158
+ ciphersuite: ciphersuite
159
+ )
160
+ try await action. perform ( in: self . context. notificationContext)
161
+ try await coreCryptoContext. deleteStaleKeyPackages (
162
+ ciphersuite: rawCiphersuite
163
+ )
164
+ }
165
+ }
166
+
168
167
}
0 commit comments