Skip to content

Commit 518e0dd

Browse files
committed
Create status channel for MacOS
1 parent c270f85 commit 518e0dd

File tree

5 files changed

+150
-29
lines changed

5 files changed

+150
-29
lines changed

examples/main.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
extern crate authenticator;
66
extern crate base64;
77
extern crate sha2;
8-
use authenticator::{AuthenticatorTransports, KeyHandle, RegisterFlags, SignFlags, U2FManager};
8+
use authenticator::{
9+
AuthenticatorTransports, KeyHandle, RegisterFlags, SignFlags, StatusUpdate, U2FManager,
10+
};
911
use sha2::{Digest, Sha256};
10-
use std::io;
11-
use std::sync::mpsc::channel;
12+
use std::sync::mpsc::{channel, RecvError};
13+
use std::{io, thread};
1214

1315
extern crate env_logger;
1416
extern crate log;
@@ -60,21 +62,41 @@ fn main() {
6062
let manager = U2FManager::new().unwrap();
6163
let flags = RegisterFlags::empty();
6264

63-
let (tx, rx) = channel();
65+
let (status_tx, status_rx) = channel::<StatusUpdate>();
66+
thread::spawn(move || loop {
67+
match status_rx.recv() {
68+
Ok(StatusUpdate::DeviceAvailable { dev_info }) => {
69+
println!("STATUS: device available: {}", dev_info)
70+
}
71+
Ok(StatusUpdate::DeviceUnavailable { dev_info }) => {
72+
println!("STATUS: device unavailable: {}", dev_info)
73+
}
74+
Ok(StatusUpdate::Success { dev_info }) => {
75+
println!("STATUS: success using device: {}", dev_info);
76+
}
77+
Err(RecvError) => {
78+
println!("STATUS: end");
79+
return;
80+
}
81+
}
82+
});
83+
84+
let (register_tx, register_rx) = channel();
6485
manager
6586
.register(
6687
flags,
6788
15_000,
6889
chall_bytes.clone(),
6990
app_bytes.clone(),
7091
vec![],
92+
status_tx.clone(),
7193
move |rv| {
72-
tx.send(rv).unwrap();
94+
register_tx.send(rv).unwrap();
7395
},
7496
)
7597
.unwrap();
7698

77-
let register_result = try_or!(rx.recv(), |_| {
99+
let register_result = try_or!(register_rx.recv(), |_| {
78100
panic!("Problem receiving, unable to continue");
79101
});
80102
let (register_data, device_info) =
@@ -90,21 +112,22 @@ fn main() {
90112
};
91113

92114
let flags = SignFlags::empty();
93-
let (tx, rx) = channel();
115+
let (sign_tx, sign_rx) = channel();
94116
manager
95117
.sign(
96118
flags,
97119
15_000,
98120
chall_bytes,
99121
vec![app_bytes],
100122
vec![key_handle],
123+
status_tx,
101124
move |rv| {
102-
tx.send(rv).unwrap();
125+
sign_tx.send(rv).unwrap();
103126
},
104127
)
105128
.unwrap();
106129

107-
let sign_result = try_or!(rx.recv(), |_| {
130+
let sign_result = try_or!(sign_rx.recv(), |_| {
108131
panic!("Problem receiving, unable to continue");
109132
});
110133
let (_, handle_used, sign_data, device_info) =

src/capi.rs

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use libc::size_t;
66
use rand::{thread_rng, Rng};
77
use std::collections::HashMap;
8+
use std::sync::mpsc::channel;
89
use std::{ptr, slice};
910

1011
use crate::U2FManager;
@@ -229,13 +230,16 @@ pub unsafe extern "C" fn rust_u2f_mgr_register(
229230
let application = from_raw(application_ptr, application_len);
230231
let key_handles = (*khs).clone();
231232

233+
let (tx, _rx) = channel::<crate::StatusUpdate>();
234+
232235
let tid = new_tid();
233236
let res = (*mgr).register(
234237
flags,
235238
timeout,
236239
challenge,
237240
application,
238241
key_handles,
242+
tx,
239243
move |rv| {
240244
let result = match rv {
241245
Ok((registration, dev_info)) => {
@@ -295,26 +299,36 @@ pub unsafe extern "C" fn rust_u2f_mgr_sign(
295299
let app_ids = (*app_ids).clone();
296300
let key_handles = (*khs).clone();
297301

302+
let (tx, _rx) = channel::<crate::StatusUpdate>();
303+
298304
let tid = new_tid();
299-
let res = (*mgr).sign(flags, timeout, challenge, app_ids, key_handles, move |rv| {
300-
let result = match rv {
301-
Ok((app_id, key_handle, signature, dev_info)) => {
302-
let mut bufs = HashMap::new();
303-
bufs.insert(RESBUF_ID_KEYHANDLE, key_handle);
304-
bufs.insert(RESBUF_ID_SIGNATURE, signature);
305-
bufs.insert(RESBUF_ID_APPID, app_id);
306-
bufs.insert(RESBUF_ID_VENDOR_NAME, dev_info.vendor_name);
307-
bufs.insert(RESBUF_ID_DEVICE_NAME, dev_info.device_name);
308-
bufs.insert(RESBUF_ID_FIRMWARE_MAJOR, vec![dev_info.version_major]);
309-
bufs.insert(RESBUF_ID_FIRMWARE_MINOR, vec![dev_info.version_minor]);
310-
bufs.insert(RESBUF_ID_FIRMWARE_BUILD, vec![dev_info.version_build]);
311-
U2FResult::Success(bufs)
312-
}
313-
Err(e) => U2FResult::Error(e),
314-
};
315-
316-
callback(tid, Box::into_raw(Box::new(result)));
317-
});
305+
let res = (*mgr).sign(
306+
flags,
307+
timeout,
308+
challenge,
309+
app_ids,
310+
key_handles,
311+
tx,
312+
move |rv| {
313+
let result = match rv {
314+
Ok((app_id, key_handle, signature, dev_info)) => {
315+
let mut bufs = HashMap::new();
316+
bufs.insert(RESBUF_ID_KEYHANDLE, key_handle);
317+
bufs.insert(RESBUF_ID_SIGNATURE, signature);
318+
bufs.insert(RESBUF_ID_APPID, app_id);
319+
bufs.insert(RESBUF_ID_VENDOR_NAME, dev_info.vendor_name);
320+
bufs.insert(RESBUF_ID_DEVICE_NAME, dev_info.device_name);
321+
bufs.insert(RESBUF_ID_FIRMWARE_MAJOR, vec![dev_info.version_major]);
322+
bufs.insert(RESBUF_ID_FIRMWARE_MINOR, vec![dev_info.version_minor]);
323+
bufs.insert(RESBUF_ID_FIRMWARE_BUILD, vec![dev_info.version_build]);
324+
U2FResult::Success(bufs)
325+
}
326+
Err(e) => U2FResult::Error(e),
327+
};
328+
329+
callback(tid, Box::into_raw(Box::new(result)));
330+
},
331+
);
318332

319333
if res.is_ok() {
320334
tid

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ pub enum Error {
112112
NotAllowed = 5,
113113
}
114114

115+
#[derive(Debug, Clone)]
116+
pub enum StatusUpdate {
117+
DeviceAvailable { dev_info: u2ftypes::U2FDeviceInfo },
118+
DeviceUnavailable { dev_info: u2ftypes::U2FDeviceInfo },
119+
Success { dev_info: u2ftypes::U2FDeviceInfo },
120+
}
121+
115122
#[cfg(fuzzing)]
116123
pub use consts::*;
117124
#[cfg(fuzzing)]

src/manager.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ enum QueueAction {
1818
challenge: Vec<u8>,
1919
application: crate::AppId,
2020
key_handles: Vec<crate::KeyHandle>,
21+
status: Sender<crate::StatusUpdate>,
2122
callback: StateCallback<Result<crate::RegisterResult, crate::Error>>,
2223
},
2324
Sign {
@@ -26,6 +27,7 @@ enum QueueAction {
2627
challenge: Vec<u8>,
2728
app_ids: Vec<crate::AppId>,
2829
key_handles: Vec<crate::KeyHandle>,
30+
status: Sender<crate::StatusUpdate>,
2931
callback: StateCallback<Result<crate::SignResult, crate::Error>>,
3032
},
3133
Cancel,
@@ -52,6 +54,7 @@ impl U2FManager {
5254
challenge,
5355
application,
5456
key_handles,
57+
status,
5558
callback,
5659
}) => {
5760
// This must not block, otherwise we can't cancel.
@@ -61,6 +64,7 @@ impl U2FManager {
6164
challenge,
6265
application,
6366
key_handles,
67+
status,
6468
callback,
6569
);
6670
}
@@ -70,10 +74,19 @@ impl U2FManager {
7074
challenge,
7175
app_ids,
7276
key_handles,
77+
status,
7378
callback,
7479
}) => {
7580
// This must not block, otherwise we can't cancel.
76-
sm.sign(flags, timeout, challenge, app_ids, key_handles, callback);
81+
sm.sign(
82+
flags,
83+
timeout,
84+
challenge,
85+
app_ids,
86+
key_handles,
87+
status,
88+
callback,
89+
);
7790
}
7891
Ok(QueueAction::Cancel) => {
7992
// Cancelling must block so that we don't start a new
@@ -101,6 +114,7 @@ impl U2FManager {
101114
challenge: Vec<u8>,
102115
application: crate::AppId,
103116
key_handles: Vec<crate::KeyHandle>,
117+
status: Sender<crate::StatusUpdate>,
104118
callback: F,
105119
) -> Result<(), crate::Error>
106120
where
@@ -124,6 +138,7 @@ impl U2FManager {
124138
challenge,
125139
application,
126140
key_handles,
141+
status,
127142
callback,
128143
};
129144
self.tx.send(action).map_err(|_| crate::Error::Unknown)
@@ -136,6 +151,7 @@ impl U2FManager {
136151
challenge: Vec<u8>,
137152
app_ids: Vec<crate::AppId>,
138153
key_handles: Vec<crate::KeyHandle>,
154+
status: Sender<crate::StatusUpdate>,
139155
callback: F,
140156
) -> Result<(), crate::Error>
141157
where
@@ -169,6 +185,7 @@ impl U2FManager {
169185
challenge,
170186
app_ids,
171187
key_handles,
188+
status,
172189
callback,
173190
};
174191
self.tx.send(action).map_err(|_| crate::Error::Unknown)

0 commit comments

Comments
 (0)