cargo fix, move to edition 2018

This commit is contained in:
J.C. Jones 2020-08-05 17:15:39 -07:00 коммит произвёл J.C. Jones
Родитель 9c65976d8a
Коммит df2d127278
11 изменённых файлов: 91 добавлений и 87 удалений

Просмотреть файл

@ -5,6 +5,7 @@ authors = ["J.C. Jones <jc@mozilla.com>", "Tim Taubert <ttaubert@mozilla.com>",
repository = "https://github.com/mozilla/authenticator-rs/" repository = "https://github.com/mozilla/authenticator-rs/"
license = "MPL-2.0" license = "MPL-2.0"
description = "Library for interacting with CTAP1/2 security keys for Web Authentication. Used by Firefox." description = "Library for interacting with CTAP1/2 security keys for Web Authentication. Used by Firefox."
edition = "2018"
[features] [features]
binding-recompile = ["bindgen"] binding-recompile = ["bindgen"]

Просмотреть файл

@ -7,15 +7,15 @@ use rand::{thread_rng, Rng};
use std::collections::HashMap; use std::collections::HashMap;
use std::{ptr, slice}; use std::{ptr, slice};
use U2FManager; use crate::U2FManager;
type U2FAppIds = Vec<::AppId>; type U2FAppIds = Vec<crate::AppId>;
type U2FKeyHandles = Vec<::KeyHandle>; type U2FKeyHandles = Vec<crate::KeyHandle>;
type U2FCallback = extern "C" fn(u64, *mut U2FResult); type U2FCallback = extern "C" fn(u64, *mut U2FResult);
pub enum U2FResult { pub enum U2FResult {
Success(HashMap<u8, Vec<u8>>), Success(HashMap<u8, Vec<u8>>),
Error(::Error), Error(crate::Error),
} }
const RESBUF_ID_REGISTRATION: u8 = 0; const RESBUF_ID_REGISTRATION: u8 = 0;
@ -104,9 +104,9 @@ pub unsafe extern "C" fn rust_u2f_khs_add(
key_handle_len: usize, key_handle_len: usize,
transports: u8, transports: u8,
) { ) {
(*khs).push(::KeyHandle { (*khs).push(crate::KeyHandle {
credential: from_raw(key_handle_ptr, key_handle_len), credential: from_raw(key_handle_ptr, key_handle_len),
transports: ::AuthenticatorTransports::from_bits_truncate(transports), transports: crate::AuthenticatorTransports::from_bits_truncate(transports),
}); });
} }
@ -127,7 +127,7 @@ pub unsafe extern "C" fn rust_u2f_khs_free(khs: *mut U2FKeyHandles) {
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn rust_u2f_result_error(res: *const U2FResult) -> u8 { pub unsafe extern "C" fn rust_u2f_result_error(res: *const U2FResult) -> u8 {
if res.is_null() { if res.is_null() {
return ::Error::Unknown as u8; return crate::Error::Unknown as u8;
} }
if let U2FResult::Error(ref err) = *res { if let U2FResult::Error(ref err) = *res {
@ -219,7 +219,7 @@ pub unsafe extern "C" fn rust_u2f_mgr_register(
return 0; return 0;
} }
let flags = ::RegisterFlags::from_bits_truncate(flags); let flags = crate::RegisterFlags::from_bits_truncate(flags);
let challenge = from_raw(challenge_ptr, challenge_len); let challenge = from_raw(challenge_ptr, challenge_len);
let application = from_raw(application_ptr, application_len); let application = from_raw(application_ptr, application_len);
let key_handles = (*khs).clone(); let key_handles = (*khs).clone();
@ -280,7 +280,7 @@ pub unsafe extern "C" fn rust_u2f_mgr_sign(
return 0; return 0;
} }
let flags = ::SignFlags::from_bits_truncate(flags); let flags = crate::SignFlags::from_bits_truncate(flags);
let challenge = from_raw(challenge_ptr, challenge_len); let challenge = from_raw(challenge_ptr, challenge_len);
let app_ids = (*app_ids).clone(); let app_ids = (*app_ids).clone();
let key_handles = (*khs).clone(); let key_handles = (*khs).clone();

Просмотреть файл

@ -67,10 +67,10 @@ mod u2fprotocol;
mod u2ftypes; mod u2ftypes;
mod manager; mod manager;
pub use manager::U2FManager; pub use crate::manager::U2FManager;
mod capi; mod capi;
pub use capi::*; pub use crate::capi::*;
// Keep this in sync with the constants in u2fhid-capi.h. // Keep this in sync with the constants in u2fhid-capi.h.
bitflags! { bitflags! {

Просмотреть файл

@ -4,15 +4,15 @@
extern crate log; extern crate log;
use consts::{CID_BROADCAST, MAX_HID_RPT_SIZE}; use crate::consts::{CID_BROADCAST, MAX_HID_RPT_SIZE};
use crate::platform::iokit::*;
use crate::u2ftypes::U2FDevice;
use core_foundation::base::*; use core_foundation::base::*;
use platform::iokit::*;
use std::convert::TryInto; use std::convert::TryInto;
use std::io; use std::io;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::sync::mpsc::{Receiver, RecvTimeoutError}; use std::sync::mpsc::{Receiver, RecvTimeoutError};
use std::time::Duration; use std::time::Duration;
use u2ftypes::U2FDevice;
const READ_TIMEOUT: u64 = 15; const READ_TIMEOUT: u64 = 15;

Просмотреть файл

@ -6,7 +6,7 @@
extern crate libc; extern crate libc;
use consts::{FIDO_USAGE_PAGE, FIDO_USAGE_U2FHID}; use crate::consts::{FIDO_USAGE_PAGE, FIDO_USAGE_U2FHID};
use core_foundation::array::*; use core_foundation::array::*;
use core_foundation::base::*; use core_foundation::base::*;
use core_foundation::dictionary::*; use core_foundation::dictionary::*;

Просмотреть файл

@ -5,15 +5,15 @@
extern crate libc; extern crate libc;
extern crate log; extern crate log;
use crate::platform::iokit::*;
use crate::util::io_err;
use core_foundation::base::*; use core_foundation::base::*;
use core_foundation::runloop::*; use core_foundation::runloop::*;
use platform::iokit::*;
use runloop::RunLoop; use runloop::RunLoop;
use std::collections::HashMap; use std::collections::HashMap;
use std::os::raw::c_void; use std::os::raw::c_void;
use std::sync::mpsc::{channel, Receiver, Sender}; use std::sync::mpsc::{channel, Receiver, Sender};
use std::{io, slice}; use std::{io, slice};
use util::io_err;
struct DeviceData { struct DeviceData {
tx: Sender<Vec<u8>>, tx: Sender<Vec<u8>>,

Просмотреть файл

@ -4,13 +4,13 @@
extern crate libc; extern crate libc;
use crate::platform::iokit::{CFRunLoopEntryObserver, IOHIDDeviceRef, SendableRunLoop};
use crate::platform::monitor::Monitor;
use crate::util::StateCallback;
use core_foundation::runloop::*; use core_foundation::runloop::*;
use platform::iokit::{CFRunLoopEntryObserver, IOHIDDeviceRef, SendableRunLoop};
use platform::monitor::Monitor;
use std::os::raw::c_void; use std::os::raw::c_void;
use std::sync::mpsc::{channel, Receiver, Sender}; use std::sync::mpsc::{channel, Receiver, Sender};
use std::thread; use std::thread;
use util::StateCallback;
// A transaction will run the given closure in a new thread, thereby using a // A transaction will run the given closure in a new thread, thereby using a
// separate per-thread state machine for each HID. It will either complete or // separate per-thread state machine for each HID. It will either complete or
@ -24,9 +24,9 @@ pub struct Transaction {
impl Transaction { impl Transaction {
pub fn new<F, T>( pub fn new<F, T>(
timeout: u64, timeout: u64,
callback: StateCallback<Result<T, ::Error>>, callback: StateCallback<Result<T, crate::Error>>,
new_device_cb: F, new_device_cb: F,
) -> Result<Self, ::Error> ) -> Result<Self, crate::Error>
where where
F: Fn((IOHIDDeviceRef, Receiver<Vec<u8>>), &dyn Fn() -> bool) + Sync + Send + 'static, F: Fn((IOHIDDeviceRef, Receiver<Vec<u8>>), &dyn Fn() -> bool) + Sync + Send + 'static,
T: 'static, T: 'static,
@ -47,7 +47,8 @@ impl Transaction {
// Create a new HID device monitor and start polling. // Create a new HID device monitor and start polling.
let mut monitor = Monitor::new(new_device_cb); let mut monitor = Monitor::new(new_device_cb);
try_or!(monitor.start(), |_| callback.call(Err(::Error::Unknown))); try_or!(monitor.start(), |_| callback
.call(Err(crate::Error::Unknown)));
// This will block until completion, abortion, or timeout. // This will block until completion, abortion, or timeout.
unsafe { CFRunLoopRunInMode(kCFRunLoopDefaultMode, timeout, 0) }; unsafe { CFRunLoopRunInMode(kCFRunLoopDefaultMode, timeout, 0) };
@ -56,12 +57,12 @@ impl Transaction {
monitor.stop(); monitor.stop();
// Send an error, if the callback wasn't called already. // Send an error, if the callback wasn't called already.
callback.call(Err(::Error::NotAllowed)); callback.call(Err(crate::Error::NotAllowed));
}) })
.map_err(|_| ::Error::Unknown)?; .map_err(|_| crate::Error::Unknown)?;
// Block until we enter the CFRunLoop. // Block until we enter the CFRunLoop.
let runloop = rx.recv().map_err(|_| ::Error::Unknown)?; let runloop = rx.recv().map_err(|_| crate::Error::Unknown)?;
Ok(Self { Ok(Self {
runloop: Some(runloop), runloop: Some(runloop),

Просмотреть файл

@ -6,27 +6,27 @@ use std::io;
use std::sync::mpsc::{channel, RecvTimeoutError, Sender}; use std::sync::mpsc::{channel, RecvTimeoutError, Sender};
use std::time::Duration; use std::time::Duration;
use consts::PARAMETER_SIZE; use crate::consts::PARAMETER_SIZE;
use crate::statemachine::StateMachine;
use crate::util::StateCallback;
use runloop::RunLoop; use runloop::RunLoop;
use statemachine::StateMachine;
use util::StateCallback;
enum QueueAction { enum QueueAction {
Register { Register {
flags: ::RegisterFlags, flags: crate::RegisterFlags,
timeout: u64, timeout: u64,
challenge: Vec<u8>, challenge: Vec<u8>,
application: ::AppId, application: crate::AppId,
key_handles: Vec<::KeyHandle>, key_handles: Vec<crate::KeyHandle>,
callback: StateCallback<Result<::RegisterResult, ::Error>>, callback: StateCallback<Result<crate::RegisterResult, crate::Error>>,
}, },
Sign { Sign {
flags: ::SignFlags, flags: crate::SignFlags,
timeout: u64, timeout: u64,
challenge: Vec<u8>, challenge: Vec<u8>,
app_ids: Vec<::AppId>, app_ids: Vec<crate::AppId>,
key_handles: Vec<::KeyHandle>, key_handles: Vec<crate::KeyHandle>,
callback: StateCallback<Result<::SignResult, ::Error>>, callback: StateCallback<Result<crate::SignResult, crate::Error>>,
}, },
Cancel, Cancel,
} }
@ -96,24 +96,24 @@ impl U2FManager {
pub fn register<F>( pub fn register<F>(
&self, &self,
flags: ::RegisterFlags, flags: crate::RegisterFlags,
timeout: u64, timeout: u64,
challenge: Vec<u8>, challenge: Vec<u8>,
application: ::AppId, application: crate::AppId,
key_handles: Vec<::KeyHandle>, key_handles: Vec<crate::KeyHandle>,
callback: F, callback: F,
) -> Result<(), ::Error> ) -> Result<(), crate::Error>
where where
F: Fn(Result<::RegisterResult, ::Error>), F: Fn(Result<crate::RegisterResult, crate::Error>),
F: Send + 'static, F: Send + 'static,
{ {
if challenge.len() != PARAMETER_SIZE || application.len() != PARAMETER_SIZE { if challenge.len() != PARAMETER_SIZE || application.len() != PARAMETER_SIZE {
return Err(::Error::Unknown); return Err(crate::Error::Unknown);
} }
for key_handle in &key_handles { for key_handle in &key_handles {
if key_handle.credential.len() > 256 { if key_handle.credential.len() > 256 {
return Err(::Error::Unknown); return Err(crate::Error::Unknown);
} }
} }
@ -126,39 +126,39 @@ impl U2FManager {
key_handles, key_handles,
callback, callback,
}; };
self.tx.send(action).map_err(|_| ::Error::Unknown) self.tx.send(action).map_err(|_| crate::Error::Unknown)
} }
pub fn sign<F>( pub fn sign<F>(
&self, &self,
flags: ::SignFlags, flags: crate::SignFlags,
timeout: u64, timeout: u64,
challenge: Vec<u8>, challenge: Vec<u8>,
app_ids: Vec<::AppId>, app_ids: Vec<crate::AppId>,
key_handles: Vec<::KeyHandle>, key_handles: Vec<crate::KeyHandle>,
callback: F, callback: F,
) -> Result<(), ::Error> ) -> Result<(), crate::Error>
where where
F: Fn(Result<::SignResult, ::Error>), F: Fn(Result<crate::SignResult, crate::Error>),
F: Send + 'static, F: Send + 'static,
{ {
if challenge.len() != PARAMETER_SIZE { if challenge.len() != PARAMETER_SIZE {
return Err(::Error::Unknown); return Err(crate::Error::Unknown);
} }
if app_ids.is_empty() { if app_ids.is_empty() {
return Err(::Error::Unknown); return Err(crate::Error::Unknown);
} }
for app_id in &app_ids { for app_id in &app_ids {
if app_id.len() != PARAMETER_SIZE { if app_id.len() != PARAMETER_SIZE {
return Err(::Error::Unknown); return Err(crate::Error::Unknown);
} }
} }
for key_handle in &key_handles { for key_handle in &key_handles {
if key_handle.credential.len() > 256 { if key_handle.credential.len() > 256 {
return Err(::Error::Unknown); return Err(crate::Error::Unknown);
} }
} }
@ -171,13 +171,13 @@ impl U2FManager {
key_handles, key_handles,
callback, callback,
}; };
self.tx.send(action).map_err(|_| ::Error::Unknown) self.tx.send(action).map_err(|_| crate::Error::Unknown)
} }
pub fn cancel(&self) -> Result<(), ::Error> { pub fn cancel(&self) -> Result<(), crate::Error> {
self.tx self.tx
.send(QueueAction::Cancel) .send(QueueAction::Cancel)
.map_err(|_| ::Error::Unknown) .map_err(|_| crate::Error::Unknown)
} }
} }

Просмотреть файл

@ -2,25 +2,25 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use consts::PARAMETER_SIZE; use crate::consts::PARAMETER_SIZE;
use platform::device::Device; use crate::platform::device::Device;
use platform::transaction::Transaction; use crate::platform::transaction::Transaction;
use crate::u2fprotocol::{u2f_init_device, u2f_is_keyhandle_valid, u2f_register, u2f_sign};
use crate::util::StateCallback;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
use u2fprotocol::{u2f_init_device, u2f_is_keyhandle_valid, u2f_register, u2f_sign};
use util::StateCallback;
fn is_valid_transport(transports: ::AuthenticatorTransports) -> bool { fn is_valid_transport(transports: crate::AuthenticatorTransports) -> bool {
transports.is_empty() || transports.contains(::AuthenticatorTransports::USB) transports.is_empty() || transports.contains(crate::AuthenticatorTransports::USB)
} }
fn find_valid_key_handles<'a, F>( fn find_valid_key_handles<'a, F>(
app_ids: &'a [::AppId], app_ids: &'a [crate::AppId],
key_handles: &'a [::KeyHandle], key_handles: &'a [crate::KeyHandle],
mut is_valid: F, mut is_valid: F,
) -> (&'a ::AppId, Vec<&'a ::KeyHandle>) ) -> (&'a crate::AppId, Vec<&'a crate::KeyHandle>)
where where
F: FnMut(&Vec<u8>, &::KeyHandle) -> bool, F: FnMut(&Vec<u8>, &crate::KeyHandle) -> bool,
{ {
// Try all given app_ids in order. // Try all given app_ids in order.
for app_id in app_ids { for app_id in app_ids {
@ -51,12 +51,12 @@ impl StateMachine {
pub fn register( pub fn register(
&mut self, &mut self,
flags: ::RegisterFlags, flags: crate::RegisterFlags,
timeout: u64, timeout: u64,
challenge: Vec<u8>, challenge: Vec<u8>,
application: ::AppId, application: crate::AppId,
key_handles: Vec<::KeyHandle>, key_handles: Vec<crate::KeyHandle>,
callback: StateCallback<Result<::RegisterResult, ::Error>>, callback: StateCallback<Result<crate::RegisterResult, crate::Error>>,
) { ) {
// Abort any prior register/sign calls. // Abort any prior register/sign calls.
self.cancel(); self.cancel();
@ -99,7 +99,7 @@ impl StateMachine {
if excluded { if excluded {
let blank = vec![0u8; PARAMETER_SIZE]; let blank = vec![0u8; PARAMETER_SIZE];
if u2f_register(dev, &blank, &blank).is_ok() { if u2f_register(dev, &blank, &blank).is_ok() {
callback.call(Err(::Error::InvalidState)); callback.call(Err(crate::Error::InvalidState));
break; break;
} }
} else if let Ok(bytes) = u2f_register(dev, &challenge, &application) { } else if let Ok(bytes) = u2f_register(dev, &challenge, &application) {
@ -117,12 +117,12 @@ impl StateMachine {
pub fn sign( pub fn sign(
&mut self, &mut self,
flags: ::SignFlags, flags: crate::SignFlags,
timeout: u64, timeout: u64,
challenge: Vec<u8>, challenge: Vec<u8>,
app_ids: Vec<::AppId>, app_ids: Vec<crate::AppId>,
key_handles: Vec<::KeyHandle>, key_handles: Vec<crate::KeyHandle>,
callback: StateCallback<Result<::SignResult, ::Error>>, callback: StateCallback<Result<crate::SignResult, crate::Error>>,
) { ) {
// Abort any prior register/sign calls. // Abort any prior register/sign calls.
self.cancel(); self.cancel();
@ -163,7 +163,9 @@ impl StateMachine {
// Aggregate distinct transports from all given credentials. // Aggregate distinct transports from all given credentials.
let transports = key_handles let transports = key_handles
.iter() .iter()
.fold(::AuthenticatorTransports::empty(), |t, k| t | k.transports); .fold(crate::AuthenticatorTransports::empty(), |t, k| {
t | k.transports
});
// We currently only support USB. If the RP specifies transports // We currently only support USB. If the RP specifies transports
// and doesn't include USB it's probably lying. // and doesn't include USB it's probably lying.
@ -177,7 +179,7 @@ impl StateMachine {
if valid_handles.is_empty() { if valid_handles.is_empty() {
let blank = vec![0u8; PARAMETER_SIZE]; let blank = vec![0u8; PARAMETER_SIZE];
if u2f_register(dev, &blank, &blank).is_ok() { if u2f_register(dev, &blank, &blank).is_ok() {
callback.call(Err(::Error::InvalidState)); callback.call(Err(crate::Error::InvalidState));
break; break;
} }
} else { } else {

Просмотреть файл

@ -11,9 +11,9 @@ use std::ffi::CString;
use std::io; use std::io;
use std::io::{Read, Write}; use std::io::{Read, Write};
use consts::*; use crate::consts::*;
use u2ftypes::*; use crate::u2ftypes::*;
use util::io_err; use crate::util::io_err;
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// Device Commands // Device Commands
@ -217,14 +217,14 @@ mod tests {
use rand::{thread_rng, RngCore}; use rand::{thread_rng, RngCore};
use super::{init_device, send_apdu, sendrecv, U2FDevice}; use super::{init_device, send_apdu, sendrecv, U2FDevice};
use consts::{CID_BROADCAST, SW_NO_ERROR, U2FHID_INIT, U2FHID_MSG, U2FHID_PING}; use crate::consts::{CID_BROADCAST, SW_NO_ERROR, U2FHID_INIT, U2FHID_MSG, U2FHID_PING};
mod platform { mod platform {
use std::io; use std::io;
use std::io::{Read, Write}; use std::io::{Read, Write};
use consts::CID_BROADCAST; use crate::consts::CID_BROADCAST;
use u2ftypes::U2FDevice; use crate::u2ftypes::U2FDevice;
const IN_HID_RPT_SIZE: usize = 64; const IN_HID_RPT_SIZE: usize = 64;
const OUT_HID_RPT_SIZE: usize = 64; const OUT_HID_RPT_SIZE: usize = 64;

Просмотреть файл

@ -4,8 +4,8 @@
use std::{cmp, io}; use std::{cmp, io};
use consts::*; use crate::consts::*;
use util::io_err; use crate::util::io_err;
use log; use log;