Skip to content

Commit d33d491

Browse files
committed
feat: use new API for rotating credentials in groups after enrolling into e2ei
1 parent 4e5f5db commit d33d491

File tree

2 files changed

+70
-74
lines changed

2 files changed

+70
-74
lines changed

wire-ios-request-strategy/Sources/E2EIdentity/E2EIKeyPackageRotator.swift

Lines changed: 70 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public class E2EIKeyPackageRotator: E2EIKeyPackageRotating {
4848
private let coreCryptoProvider: CoreCryptoProviderProtocol
4949
private let conversationEventProcessor: ConversationEventProcessorProtocol
5050
private let context: NSManagedObjectContext
51-
private let commitSender: CommitSending
5251
private let newKeyPackageCount: UInt32 = 100
5352
private let featureRepository: FeatureRepositoryInterface
5453
private let onNewCRLsDistributionPointsSubject: PassthroughSubject<CRLsDistributionPoints, Never>
@@ -66,17 +65,12 @@ public class E2EIKeyPackageRotator: E2EIKeyPackageRotating {
6665
conversationEventProcessor: ConversationEventProcessorProtocol,
6766
context: NSManagedObjectContext,
6867
onNewCRLsDistributionPointsSubject: PassthroughSubject<CRLsDistributionPoints, Never>,
69-
commitSender: CommitSending? = nil,
7068
featureRepository: FeatureRepositoryInterface
7169
) {
7270
self.coreCryptoProvider = coreCryptoProvider
7371
self.conversationEventProcessor = conversationEventProcessor
7472
self.context = context
7573
self.onNewCRLsDistributionPointsSubject = onNewCRLsDistributionPointsSubject
76-
self.commitSender = commitSender ?? CommitSender(
77-
coreCryptoProvider: coreCryptoProvider,
78-
notificationContext: context.notificationContext
79-
)
8074
self.featureRepository = featureRepository
8175
}
8276

@@ -93,76 +87,81 @@ public class E2EIKeyPackageRotator: E2EIKeyPackageRotating {
9387
guard let enrollment = enrollment as? E2eiEnrollment else {
9488
throw Error.invalidIdentity
9589
}
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()
96100

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+
}
128105
}
129106

130107
// 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+
}
157131
}
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)
166132
}
167133

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+
168167
}

wire-ios-request-strategy/Sources/E2EIdentity/E2EIKeyPackageRotatorTests.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class E2EIKeyPackageRotatorTests: MessagingTestBase {
2727

2828
private var mockCoreCrypto: MockCoreCryptoProtocol!
2929
private var mockCoreCryptoProvider: MockCoreCryptoProviderProtocol!
30-
private var mockCommitSender: MockCommitSending!
3130
private var mockConversationEventProcessor: MockConversationEventProcessorProtocol!
3231
private var mockFeatureRepository: MockFeatureRepositoryInterface!
3332
private var sut: E2EIKeyPackageRotator!
@@ -36,7 +35,6 @@ class E2EIKeyPackageRotatorTests: MessagingTestBase {
3635
super.setUp()
3736

3837
mockCoreCrypto = MockCoreCryptoProtocol()
39-
mockCommitSender = MockCommitSending()
4038
mockConversationEventProcessor = MockConversationEventProcessorProtocol()
4139
mockCoreCryptoProvider = MockCoreCryptoProviderProtocol()
4240
mockCoreCryptoProvider.coreCrypto_MockValue = MockSafeCoreCrypto(coreCrypto: mockCoreCrypto)
@@ -47,7 +45,6 @@ class E2EIKeyPackageRotatorTests: MessagingTestBase {
4745
conversationEventProcessor: mockConversationEventProcessor,
4846
context: syncMOC,
4947
onNewCRLsDistributionPointsSubject: .init(),
50-
commitSender: mockCommitSender,
5148
featureRepository: mockFeatureRepository
5249
)
5350
}

0 commit comments

Comments
 (0)