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
use crate::{
KeyExchangeCompleted, SecureChannelListener, SecureChannelNewKeyExchanger, SecureChannelVault,
SecureChannelWorker,
};
use ockam_core::{Address, Result, Route};
use ockam_node::Context;
use rand::random;
use serde::{Deserialize, Serialize};
use tracing::{debug, info};
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct SecureChannelInfo {
worker_address: Address,
auth_hash: [u8; 32],
}
impl SecureChannelInfo {
pub fn address(&self) -> Address {
self.worker_address.clone()
}
pub fn auth_hash(&self) -> [u8; 32] {
self.auth_hash
}
}
pub struct SecureChannel;
impl SecureChannel {
#[cfg(all(feature = "software_vault", feature = "noise_xx"))]
pub async fn create_listener<A: Into<Address>>(
ctx: &Context,
address: A,
vault: &Address,
) -> Result<()> {
use ockam_key_exchange_xx::XXNewKeyExchanger;
use ockam_vault_sync_core::VaultSync;
let vault = VaultSync::create_with_worker(ctx, vault)?;
let new_key_exchanger = XXNewKeyExchanger::new(vault.clone());
Self::create_listener_extended(ctx, address, new_key_exchanger, vault).await
}
pub async fn create_listener_extended<
A: Into<Address>,
N: SecureChannelNewKeyExchanger,
V: SecureChannelVault,
>(
ctx: &Context,
address: A,
new_key_exchanger: N,
vault: V,
) -> Result<()> {
let address = address.into();
let channel_listener = SecureChannelListener::new(new_key_exchanger, vault);
info!("Starting SecureChannel listener at {}", &address);
ctx.start_worker(address, channel_listener).await?;
Ok(())
}
#[cfg(all(feature = "software_vault", feature = "noise_xx"))]
pub async fn create<R: Into<Route>>(
ctx: &Context,
route: R,
vault: &Address,
) -> Result<SecureChannelInfo> {
use ockam_key_exchange_xx::XXNewKeyExchanger;
use ockam_vault_sync_core::VaultSync;
let vault = VaultSync::create_with_worker(ctx, vault)?;
let new_key_exchanger = XXNewKeyExchanger::new(vault.clone());
Self::create_extended(ctx, route, None, &new_key_exchanger, vault).await
}
pub async fn create_extended<
R: Into<Route>,
N: SecureChannelNewKeyExchanger,
V: SecureChannelVault,
>(
ctx: &Context,
route: R,
first_responder_address: Option<Address>,
new_key_exchanger: &N,
vault: V,
) -> Result<SecureChannelInfo> {
let address_remote: Address = random();
let address_local: Address = random();
debug!(
"Starting SecureChannel initiator at local: {}, remote: {}",
&address_local, &address_remote
);
let address: Address = random();
let mut child_ctx = ctx.new_context(address).await?;
let channel = SecureChannelWorker::new(
true,
route.into(),
address_remote.clone(),
address_local.clone(),
Some(child_ctx.address()),
first_responder_address,
new_key_exchanger.initiator()?,
vault,
)?;
ctx.start_worker(vec![address_remote.clone(), address_local.clone()], channel)
.await?;
let resp = child_ctx
.receive_match(|m: &KeyExchangeCompleted| m.address() == &address_local)
.await?
.take()
.body();
let info = SecureChannelInfo {
worker_address: address_local,
auth_hash: resp.auth_hash(),
};
Ok(info)
}
}