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
//! An in-process keystore that manages the entire lair server life-cycle
//! without needing to call out to an external process.
use crate::*;
use std::future::Future;
/// An in-process keystore that manages the entire lair server life-cycle
/// without needing to call out to an external process.
#[derive(Clone)]
pub struct InProcKeystore {
config: LairServerConfig,
passphrase: SharedLockedArray,
srv_hnd: crate::lair_server::LairServer,
}
impl InProcKeystore {
/// Construct a new InProcKeystore instance.
/// The internal server will already be "interactively" unlocked.
pub fn new(
config: LairServerConfig,
store_factory: LairStoreFactory,
passphrase: SharedLockedArray,
) -> impl Future<Output = LairResult<Self>> + 'static + Send {
async move {
// set up our server handler
let srv_hnd = crate::lair_server::spawn_lair_server_task(
config.clone(),
"lair-keystore-in-proc".into(),
LAIR_VER.into(),
store_factory,
passphrase.clone(),
)
.await?;
Ok(Self {
config,
passphrase,
srv_hnd,
})
}
}
/// Get a handle to the LairStore instantiated by this server,
/// may error if a store has not yet been created.
pub fn store(
&self,
) -> impl Future<Output = LairResult<LairStore>> + 'static + Send {
self.srv_hnd.store()
}
/// Get the config used by the LairServer held by this InProcKeystore.
pub fn get_config(&self) -> LairServerConfig {
self.config.clone()
}
/// Get a new LairClient connection to this InProcKeystore server.
/// This new connection will already have verified the server identity
/// via "hello" request as well as unlocked the connection.
pub fn new_client(
&self,
) -> impl Future<Output = LairResult<LairClient>> + 'static + Send {
let srv_pub_key = self.config.get_server_pub_key();
let passphrase = self.passphrase.clone();
let srv_hnd = self.srv_hnd.clone();
async move {
let srv_pub_key = srv_pub_key?;
// note, for now it greatly simplifies the implementation
// to just have the single server implementation expecting
// to process async read/write channels. This increases
// overhead for the in-process implementation, so, someday
// we could make a shortcut for this use-case.
// create a new duplex to simulate networking code
let (srv, cli) = tokio::io::duplex(4096);
// split into read/write halves
let (srv_recv, srv_send) = tokio::io::split(srv);
let (cli_recv, cli_send) = tokio::io::split(cli);
// get the server accept future
let srv_fut = srv_hnd.accept(srv_send, srv_recv);
// get the client wrap future
let cli_fut =
crate::lair_client::async_io::new_async_io_lair_client(
cli_send,
cli_recv,
srv_pub_key.cloned_inner().into(),
);
// await both futures at the same time so they can
// exchange information
let (_, cli_hnd) =
futures::future::try_join(srv_fut, cli_fut).await?;
// verify server identity
cli_hnd.hello(srv_pub_key).await?;
// unlock the connection
cli_hnd.unlock(passphrase).await?;
Ok(cli_hnd)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::{Arc, Mutex};
#[tokio::test(flavor = "multi_thread")]
async fn in_proc_happy_path() {
// set up a passphrase
let passphrase = Arc::new(Mutex::new(sodoken::LockedArray::from(
b"passphrase".to_vec(),
)));
// create the config for the test server
// the path is immaterial since we'll be using an in-memory store
let config = Arc::new(
hc_seed_bundle::PwHashLimits::Interactive
.with_exec(|| {
LairServerConfigInner::new("/", passphrase.clone())
})
.await
.unwrap(),
);
// create an in-process keystore with an in-memory store
let keystore = InProcKeystore::new(
config,
crate::mem_store::create_mem_store_factory(),
passphrase.clone(),
)
.await
.unwrap();
let config = keystore.get_config();
println!("{config}");
// create a client connection to the keystore
let client = keystore.new_client().await.unwrap();
// create a new seed
let seed_info_ref = client
.new_seed("test-tag".into(), None, false)
.await
.unwrap();
// list keystore contents
let mut entry_list = client.list_entries().await.unwrap();
assert_eq!(1, entry_list.len());
match entry_list.remove(0) {
LairEntryInfo::Seed { tag, seed_info } => {
assert_eq!("test-tag", &*tag);
assert_eq!(seed_info, seed_info_ref);
}
oth => panic!("unexpected: {:?}", oth),
}
let tag = "test-tag-deep";
let passphrase =
Arc::new(Mutex::new(sodoken::LockedArray::from(b"deep".to_vec())));
// create a new deep-locked seed
let seed_info_ref_deep = hc_seed_bundle::PwHashLimits::Interactive
.with_exec(|| {
client.new_seed(tag.into(), Some(passphrase.clone()), false)
})
.await
.unwrap();
println!("{:#?}", client.list_entries().await.unwrap());
let seed_info_ref2 = client
.new_seed("test-tag-2".into(), None, false)
.await
.unwrap();
let (nonce, cipher) = client
.crypto_box_xsalsa_by_pub_key(
seed_info_ref.x25519_pub_key.clone(),
seed_info_ref2.x25519_pub_key.clone(),
None,
b"hello"[..].into(),
)
.await
.unwrap();
let msg = client
.crypto_box_xsalsa_open_by_pub_key(
seed_info_ref.x25519_pub_key,
seed_info_ref2.x25519_pub_key,
None,
nonce,
cipher,
)
.await
.unwrap();
assert_eq!(b"hello", &*msg);
let (nonce, cipher) = client
.crypto_box_xsalsa_by_sign_pub_key(
seed_info_ref.ed25519_pub_key.clone(),
seed_info_ref2.ed25519_pub_key.clone(),
None,
b"world"[..].into(),
)
.await
.unwrap();
let msg = client
.crypto_box_xsalsa_open_by_sign_pub_key(
seed_info_ref.ed25519_pub_key,
seed_info_ref2.ed25519_pub_key,
None,
nonce,
cipher,
)
.await
.unwrap();
assert_eq!(b"world", &*msg);
let data = Arc::new([1, 2, 3_u8]);
let signature = client
.sign_by_pub_key(
seed_info_ref_deep.ed25519_pub_key.clone(),
Some(passphrase),
data.clone(),
)
.await
.unwrap();
assert!(
seed_info_ref_deep
.ed25519_pub_key
.verify_detached(signature, data)
.await
);
}
#[tokio::test(flavor = "multi_thread")]
async fn in_proc_derive_seed_happy_path() {
// set up a passphrase
let passphrase1 = Arc::new(Mutex::new(sodoken::LockedArray::from(
b"passphrase1".to_vec(),
)));
let passphrase2 = Arc::new(Mutex::new(sodoken::LockedArray::from(
b"passphrase2".to_vec(),
)));
// create the config for the test server
// the path is immaterial since we'll be using an in-memory store
let config = Arc::new(
hc_seed_bundle::PwHashLimits::Minimum
.with_exec(|| {
LairServerConfigInner::new("/", passphrase1.clone())
})
.await
.unwrap(),
);
// create an in-process keystore with an in-memory store
let keystore = InProcKeystore::new(
config,
crate::mem_store::create_mem_store_factory(),
passphrase1.clone(),
)
.await
.unwrap();
// create a client connection to the keystore
let client = keystore.new_client().await.unwrap();
// create a new seed
let _ = client.new_seed("seed-0".into(), None, false).await.unwrap();
// create a new seed
let _ = client
.new_seed("deepseed-0".into(), Some(passphrase1.clone()), false)
.await
.unwrap();
client
.derive_seed(
"seed-0".into(),
None,
"deepseed-1".into(),
Some(passphrase2),
Box::new([1, 1]),
)
.await
.unwrap();
client
.derive_seed(
"deepseed-0".into(),
Some(passphrase1),
"seed-1".into(),
None,
Box::new([1, 2, 3, 5, 8]),
)
.await
.unwrap();
let seed = client.get_entry("seed-1".into()).await.unwrap();
let deepseed = client.get_entry("deepseed-1".into()).await.unwrap();
assert!(matches!(seed, LairEntryInfo::Seed { .. }));
assert!(matches!(deepseed, LairEntryInfo::DeepLockedSeed { .. }));
}
}