-
-
Notifications
You must be signed in to change notification settings - Fork 557
/
handshake_worker.rs
352 lines (323 loc) · 12.8 KB
/
handshake_worker.rs
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
use crate::credential::Credential;
use crate::secure_channel::decryptor::DecryptorHandler;
use crate::secure_channel::encryptor::Encryptor;
use crate::secure_channel::encryptor_worker::EncryptorWorker;
use crate::secure_channel::handshake::handshake_state_machine::Action::SendMessage;
use crate::secure_channel::handshake::handshake_state_machine::Event::{
Initialize, ReceivedMessage,
};
use crate::secure_channel::handshake::handshake_state_machine::{
Action, HandshakeResults, StateMachine,
};
use crate::secure_channel::handshake::initiator_state_machine::InitiatorStateMachine;
use crate::secure_channel::handshake::responder_state_machine::ResponderStateMachine;
use crate::secure_channel::{Addresses, Role};
use crate::{
to_xx_initialized, to_xx_vault, IdentityError, IdentityIdentifier, SecureChannelRegistryEntry,
SecureChannels, TrustContext, TrustPolicy,
};
use alloc::sync::Arc;
use core::time::Duration;
use ockam_core::compat::{boxed::Box, vec::Vec};
use ockam_core::errcode::{Kind, Origin};
use ockam_core::{
AllowAll, Any, Decodable, DenyAll, Error, Mailbox, Mailboxes, OutgoingAccessControl, Route,
Routed,
};
use ockam_core::{AllowOnwardAddress, Result, Worker};
use ockam_node::callback::CallbackSender;
use ockam_node::{Context, WorkerBuilder};
use tracing::{debug, info};
/// This struct implements a Worker receiving and sending messages
/// on one side of the secure channel creation as specified with its role: INITIATOR or REPSONDER
pub(crate) struct HandshakeWorker {
secure_channels: Arc<SecureChannels>,
callback_sender: Option<CallbackSender<()>>,
state_machine: Box<dyn StateMachine>,
identifier: IdentityIdentifier,
addresses: Addresses,
role: Role,
remote_route: Option<Route>,
decryptor_handler: Option<DecryptorHandler>,
}
#[ockam_core::worker]
impl Worker for HandshakeWorker {
type Message = Any;
type Context = Context;
/// Initialize the state machine with an `Initialize` event
/// Depending on the state machine role there might be a message to send to the other party
async fn initialize(&mut self, context: &mut Self::Context) -> Result<()> {
match self.state_machine.on_event(Initialize).await? {
SendMessage(message) => {
info!(
"remote route {:?}, decryptor remote {:?}",
self.remote_route.clone(),
self.addresses.decryptor_remote.clone()
);
context
.send_from_address(
self.remote_route()?,
message,
self.addresses.decryptor_remote.clone(),
)
.await
}
Action::NoAction => Ok(()),
}
}
/// Handle a message coming from the other party
/// If the handshake has been fully performed then we can delegate this message to the
/// secure channel Decryptor.
/// Otherwise we unpack the message payload and send it to the state machine to trigger
/// a transition
async fn handle_message(
&mut self,
context: &mut Self::Context,
message: Routed<Self::Message>,
) -> Result<()> {
// Once the decryptor has been initialized, let it handle messages
// Some messages can come from other systems using the remote address
// and some messages can come from the current node when the decryptor
// used to support the decryption of Kafka messages for example
if let Some(decryptor_handler) = self.decryptor_handler.as_mut() {
let msg_addr = message.msg_addr();
let result = if msg_addr == self.addresses.decryptor_remote {
decryptor_handler.handle_decrypt(context, message).await
} else if msg_addr == self.addresses.decryptor_api {
decryptor_handler.handle_decrypt_api(context, message).await
} else {
Err(IdentityError::UnknownChannelMsgDestination.into())
};
return result;
};
let transport_message = message.into_transport_message();
if let SendMessage(message) = self
.state_machine
.on_event(ReceivedMessage(Vec::<u8>::decode(
&transport_message.payload,
)?))
.await?
{
// set the remote route by taking the most up to date message return route
// In the case of the initiator the first return route mentions the secure channel listener
// address so we need to wait for the return route corresponding to the remote handshake worker
// when it has been spawned
self.remote_route = Some(transport_message.return_route);
context
.send_from_address(
self.remote_route()?,
message,
self.addresses.decryptor_remote.clone(),
)
.await?
};
// if we reached the final state we can make a pair of encryptor/decryptor
if let Some(final_state) = self.state_machine.get_handshake_results() {
// start the encryptor worker and return the decryptor
self.decryptor_handler = Some(self.finalize(context, final_state).await?);
if let Some(callback_sender) = self.callback_sender.take() {
callback_sender.send(())?;
}
};
Ok(())
}
async fn shutdown(&mut self, _context: &mut Self::Context) -> Result<()> {
if let Some(handler) = &self.decryptor_handler {
handler.shutdown().await?
}
Ok(())
}
}
impl HandshakeWorker {
/// Create a new HandshakeWorker with a role of either INITIATOR or RESPONDER
#[allow(clippy::too_many_arguments)]
pub(crate) async fn create(
context: &Context,
secure_channels: Arc<SecureChannels>,
addresses: Addresses,
identifier: IdentityIdentifier,
trust_policy: Arc<dyn TrustPolicy>,
decryptor_outgoing_access_control: Arc<dyn OutgoingAccessControl>,
credentials: Vec<Credential>,
trust_context: Option<TrustContext>,
remote_route: Option<Route>,
timeout: Option<Duration>,
role: Role,
) -> Result<()> {
let vault = to_xx_vault(secure_channels.vault());
let identities = secure_channels.identities();
let state_machine: Box<dyn StateMachine> = if role.is_initiator() {
Box::new(
InitiatorStateMachine::new(
vault,
identities,
identifier.clone(),
credentials,
trust_policy,
trust_context,
)
.await?,
)
} else {
Box::new(
ResponderStateMachine::new(
vault,
identities,
identifier.clone(),
credentials,
trust_policy,
trust_context,
)
.await?,
)
};
let (callback_waiter, callback_sender) = if role.is_initiator() {
let callback = ockam_node::callback::new_callback();
(Some(callback.0), Some(callback.1))
} else {
(None, None)
};
let worker = Self {
secure_channels,
callback_sender,
state_machine,
identifier,
role,
remote_route: remote_route.clone(),
addresses: addresses.clone(),
decryptor_handler: None,
};
WorkerBuilder::new(worker)
.with_mailboxes(Self::create_mailboxes(
&addresses,
decryptor_outgoing_access_control,
))
.start(context)
.await?;
let decryptor_remote = addresses.decryptor_remote.clone();
debug!(
"Starting SecureChannel {} at remote: {}",
role, &decryptor_remote
);
// before sending messages make sure that the handshake is finished and
// the encryptor worker is ready
if role.is_initiator() {
if let Some(callback_waiter) = callback_waiter {
// wait until the handshake is finished
if let Some(timeout) = timeout {
callback_waiter.receive_timeout(timeout).await?;
} else {
callback_waiter.receive().await?;
}
}
}
Ok(())
}
/// Return the route for the other party's handshake worker
fn remote_route(&self) -> Result<Route> {
self.remote_route.clone().ok_or_else(|| {
Error::new(
Origin::KeyExchange,
Kind::Invalid,
"a remote route should have been already set",
)
})
}
/// Create mailboxes and access rights for the workers involved in the secure channel creation
pub(crate) fn create_mailboxes(
addresses: &Addresses,
decryptor_outgoing_access_control: Arc<dyn OutgoingAccessControl>,
) -> Mailboxes {
let remote_mailbox = Mailbox::new(
addresses.decryptor_remote.clone(),
// Doesn't matter since we check incoming messages cryptographically,
// but this may be reduced to allowing only from the transport connection that was used
// to create this channel initially
Arc::new(AllowAll),
// Communicate to the other side of the channel during key exchange
Arc::new(AllowAll),
);
let internal_mailbox = Mailbox::new(
addresses.decryptor_internal.clone(),
Arc::new(DenyAll),
decryptor_outgoing_access_control,
);
let api_mailbox = Mailbox::new(
addresses.decryptor_api.clone(),
Arc::new(AllowAll),
Arc::new(AllowAll),
);
Mailboxes::new(remote_mailbox, vec![internal_mailbox, api_mailbox])
}
/// Finalize the handshake by creating a `Decryptor` and an `EncryptorWorker`
/// Note that `EncryptorWorker` is actually started as an independent worker while
/// the `Decryptor` is directly used by this worker to delegate the decryption of messages
async fn finalize(
&self,
context: &Context,
handshake_results: HandshakeResults,
) -> Result<DecryptorHandler> {
// create a decryptor to delegate the processing of all messages after the handshake
let decryptor = DecryptorHandler::new(
self.role.str(),
self.addresses.clone(),
handshake_results.handshake_keys.decryption_key,
to_xx_initialized(self.secure_channels.identities.vault()),
handshake_results.their_identifier.clone(),
);
// create a separate encryptor worker which will be started independently
{
let encryptor = EncryptorWorker::new(
self.role.str(),
self.addresses.clone(),
self.remote_route()?,
Encryptor::new(
handshake_results.handshake_keys.encryption_key,
0,
to_xx_initialized(self.secure_channels.identities.vault()),
),
);
let next_hop = self.remote_route()?.next()?.clone();
let main_mailbox = Mailbox::new(
self.addresses.encryptor.clone(),
Arc::new(AllowAll),
Arc::new(AllowOnwardAddress(next_hop)),
);
let api_mailbox = Mailbox::new(
self.addresses.encryptor_api.clone(),
Arc::new(AllowAll),
Arc::new(AllowAll),
);
WorkerBuilder::new(encryptor)
.with_mailboxes(Mailboxes::new(main_mailbox, vec![api_mailbox]))
.start(context)
.await?;
}
info!(
"Initialized SecureChannel {} at local: {}, remote: {}",
self.role.str(),
&self.addresses.encryptor,
&self.addresses.decryptor_remote
);
let their_decryptor_address = self
.remote_route()?
.iter()
.last()
.expect("the remote route should not be empty")
.clone();
let info = SecureChannelRegistryEntry::new(
self.addresses.encryptor.clone(),
self.addresses.encryptor_api.clone(),
self.addresses.decryptor_remote.clone(),
self.addresses.decryptor_api.clone(),
self.role.is_initiator(),
self.identifier.clone(),
handshake_results.their_identifier,
their_decryptor_address,
);
self.secure_channels
.secure_channel_registry()
.register_channel(info)?;
Ok(decryptor)
}
}