-
Notifications
You must be signed in to change notification settings - Fork 11
/
MLSActionExecutor.swift
299 lines (253 loc) · 12 KB
/
MLSActionExecutor.swift
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
//
// Wire
// Copyright (C) 2022 Wire Swiss GmbH
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//
import Foundation
import WireCoreCrypto
import Combine
public protocol MLSActionExecutorProtocol {
func addMembers(_ invitees: [KeyPackage], to groupID: MLSGroupID) async throws -> [ZMUpdateEvent]
func removeClients(_ clients: [ClientId], from groupID: MLSGroupID) async throws -> [ZMUpdateEvent]
func updateKeyMaterial(for groupID: MLSGroupID) async throws -> [ZMUpdateEvent]
func commitPendingProposals(in groupID: MLSGroupID) async throws -> [ZMUpdateEvent]
func joinGroup(_ groupID: MLSGroupID, groupInfo: Data) async throws -> [ZMUpdateEvent]
func decryptMessage(_ message: Data, in groupID: MLSGroupID) async throws -> DecryptedMessage
func onEpochChanged() -> AnyPublisher<MLSGroupID, Never>
}
public actor MLSActionExecutor: MLSActionExecutorProtocol {
// MARK: - Types
enum Action {
case addMembers([KeyPackage])
case removeClients([ClientId])
case updateKeyMaterial
case proposal
case joinGroup(Data)
}
// MARK: - Properties
private let coreCryptoProvider: CoreCryptoProviderProtocol
private let commitSender: CommitSending
private var continuationsByGroupID: [MLSGroupID: [CheckedContinuation<Void, Never>]] = [:]
private var coreCrypto: SafeCoreCryptoProtocol {
get async throws {
try await coreCryptoProvider.coreCrypto(requireMLS: true)
}
}
// MARK: - Life cycle
public init(
coreCryptoProvider: CoreCryptoProviderProtocol,
commitSender: CommitSending
) {
self.coreCryptoProvider = coreCryptoProvider
self.commitSender = commitSender
}
// MARK: - Non-reentrant
/// Perform an non-rentrant operation on an MLS group.
///
/// That is only one operation is allowed execute concurrently, if multiple operations for the same group is scheduled
/// they will be queued and executed in sequence.
///
/// This is used for operations where ordering is important. For example when sending a commit to add client to a group, this is a two-step operations:
///
/// 1. Create pending commit and send to distribution server
/// 2. Merge pending commit when accepted by distribution server
///
/// Here's it's critical that no other operation like `decryptMessage` is performed
/// between step 1 and 2. We enforce this by wrapping all `decrypt` and `commit` operations
/// inside `performNonReentrant`
///
func performNonReentrant<T>(groupID: MLSGroupID, operation: () async throws -> T) async rethrows -> T {
if continuationsByGroupID.keys.contains(groupID) {
await withCheckedContinuation { continuation in
continuationsByGroupID[groupID]?.append(continuation)
}
}
if !continuationsByGroupID.keys.contains(groupID) {
// an empty entry means an operation is currently executing, a non-empty
// entry are queued operations.
continuationsByGroupID[groupID] = []
}
defer {
if var continuations = continuationsByGroupID[groupID] {
if continuations.isNonEmpty {
continuations.removeFirst().resume()
continuationsByGroupID[groupID] = continuations
}
if continuations.isEmpty {
continuationsByGroupID.removeValue(forKey: groupID)
}
}
}
return try await operation()
}
// MARK: - Actions
public func addMembers(_ invitees: [KeyPackage], to groupID: MLSGroupID) async throws -> [ZMUpdateEvent] {
try await performNonReentrant(groupID: groupID) {
do {
WireLogger.mls.info("adding members to group (\(groupID.safeForLoggingDescription))...")
let bundle = try await commitBundle(for: .addMembers(invitees), in: groupID)
let result = try await commitSender.sendCommitBundle(bundle, for: groupID)
WireLogger.mls.info("success: adding members to group (\(groupID.safeForLoggingDescription))")
return result
} catch {
WireLogger.mls.info("failed: adding members to group (\(groupID.safeForLoggingDescription)): \(String(describing: error))")
throw error
}
}
}
public func removeClients(_ clients: [ClientId], from groupID: MLSGroupID) async throws -> [ZMUpdateEvent] {
try await performNonReentrant(groupID: groupID) {
do {
WireLogger.mls.info("removing clients from group (\(groupID.safeForLoggingDescription))...")
let bundle = try await commitBundle(for: .removeClients(clients), in: groupID)
let result = try await commitSender.sendCommitBundle(bundle, for: groupID)
WireLogger.mls.info("success: removing clients from group (\(groupID.safeForLoggingDescription))")
return result
} catch {
WireLogger.mls.info("error: removing clients from group (\(groupID.safeForLoggingDescription)): \(String(describing: error))")
throw error
}
}
}
public func updateKeyMaterial(for groupID: MLSGroupID) async throws -> [ZMUpdateEvent] {
try await performNonReentrant(groupID: groupID) {
do {
WireLogger.mls.info("updating key material for group (\(groupID.safeForLoggingDescription))...")
let bundle = try await commitBundle(for: .updateKeyMaterial, in: groupID)
let result = try await commitSender.sendCommitBundle(bundle, for: groupID)
WireLogger.mls.info("success: updating key material for group (\(groupID.safeForLoggingDescription))")
return result
} catch {
WireLogger.mls.info("error: updating key material for group (\(groupID.safeForLoggingDescription)): \(String(describing: error))")
throw error
}
}
}
public func commitPendingProposals(in groupID: MLSGroupID) async throws -> [ZMUpdateEvent] {
try await performNonReentrant(groupID: groupID) {
do {
WireLogger.mls.info("committing pending proposals for group (\(groupID.safeForLoggingDescription))...")
let bundle = try await commitBundle(for: .proposal, in: groupID)
let result = try await commitSender.sendCommitBundle(bundle, for: groupID)
WireLogger.mls.info("success: committing pending proposals for group (\(groupID.safeForLoggingDescription))")
return result
} catch CommitError.noPendingProposals {
throw CommitError.noPendingProposals
} catch {
WireLogger.mls.info("error: committing pending proposals for group (\(groupID.safeForLoggingDescription)): \(String(describing: error))")
throw error
}
}
}
public func joinGroup(_ groupID: MLSGroupID, groupInfo: Data) async throws -> [ZMUpdateEvent] {
try await performNonReentrant(groupID: groupID) {
do {
WireLogger.mls.info("joining group (\(groupID.safeForLoggingDescription)) via external commit")
let bundle = try await commitBundle(for: .joinGroup(groupInfo), in: groupID)
let result = try await commitSender.sendExternalCommitBundle(bundle, for: groupID)
WireLogger.mls.info("success: joining group (\(groupID.safeForLoggingDescription)) via external commit")
return result
} catch {
WireLogger.mls.info("error: joining group (\(groupID.safeForLoggingDescription)) via external commit: \(String(describing: error))")
throw error
}
}
}
// MARK: - Decryption
public func decryptMessage(_ message: Data, in groupID: MLSGroupID) async throws -> DecryptedMessage {
try await performNonReentrant(groupID: groupID) {
try await coreCrypto.perform {
try await $0.decryptMessage(conversationId: groupID.data, payload: message)
}
}
}
// MARK: - Commit generation
private func commitBundle(for action: Action, in groupID: MLSGroupID) async throws -> CommitBundle {
do {
WireLogger.mls.info("generating commit for action (\(String(describing: action))) for group (\(groupID.safeForLoggingDescription))...")
switch action {
case .addMembers(let clients):
let memberAddMessages = try await coreCrypto.perform {
try await $0.addClientsToConversation(
conversationId: groupID.data,
keyPackages: clients.compactMap(\.keyPackage.base64DecodedData)
)
}
return CommitBundle(
welcome: memberAddMessages.welcome,
commit: memberAddMessages.commit,
groupInfo: memberAddMessages.groupInfo
)
case .removeClients(let clients):
return try await coreCrypto.perform {
try await $0.removeClientsFromConversation(
conversationId: groupID.data,
clients: clients
)
}
case .updateKeyMaterial:
return try await coreCrypto.perform {
try await $0.updateKeyingMaterial(conversationId: groupID.data)
}
case .proposal:
guard let bundle = try await coreCrypto.perform({
try await $0.commitPendingProposals(conversationId: groupID.data)
}) else {
throw CommitError.noPendingProposals
}
return bundle
case .joinGroup(let groupInfo):
let conversationInitBundle = try await coreCrypto.perform {
try await $0.joinByExternalCommit(
groupInfo: groupInfo,
customConfiguration: .init(keyRotationSpan: nil, wirePolicy: nil),
credentialType: .basic
)
}
return CommitBundle(
welcome: nil,
commit: conversationInitBundle.commit,
groupInfo: conversationInitBundle.groupInfo
)
}
} catch CommitError.noPendingProposals {
throw CommitError.noPendingProposals
} catch {
WireLogger.mls.warn("failed: generating commit for action (\(String(describing: action))) for group (\(groupID.safeForLoggingDescription)): \(String(describing: error))")
throw CommitError.failedToGenerateCommit
}
}
// MARK: - Epoch publisher
nonisolated
public func onEpochChanged() -> AnyPublisher<MLSGroupID, Never> {
commitSender.onEpochChanged()
}
}
extension MLSActionExecutor.Action: CustomDebugStringConvertible {
var debugDescription: String {
switch self {
case .addMembers:
return "addMembers"
case .removeClients:
return "removeClients"
case .updateKeyMaterial:
return "updateKeyMaterial"
case .proposal:
return "proposal"
case .joinGroup:
return "joinGroup"
}
}
}