Skip to content

Commit

Permalink
Disallow creating uninit Credential, update example
Browse files Browse the repository at this point in the history
  • Loading branch information
PvdBerg1998 committed Jun 29, 2019
1 parent 064852b commit d55ab2c
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 39 deletions.
21 changes: 0 additions & 21 deletions examples/detect.rs

This file was deleted.

53 changes: 53 additions & 0 deletions examples/run.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use libfido2::*;
use std::ffi::CString;

// Source: https://github.com/Yubico/libfido2/blob/master/examples/cred.c
const CLIENT_DATA_HASH: [u8; 32] = [
0xf9, 0x64, 0x57, 0xe7, 0x2d, 0x97, 0xf6, 0xbb, 0xdd, 0xd7, 0xfb, 0x06, 0x37, 0x62, 0xea, 0x26,
0x20, 0x44, 0x8e, 0x69, 0x7c, 0x03, 0xf2, 0x31, 0x2f, 0x99, 0xdc, 0xaf, 0x3e, 0x8a, 0x91, 0x6b,
];
const USER_ID: [u8; 32] = [
0x78, 0x1c, 0x78, 0x60, 0xad, 0x88, 0xd2, 0x63, 0x32, 0x62, 0x2a, 0xf1, 0x74, 0x5d, 0xed, 0xb2,
0xe7, 0xa4, 0x2b, 0x44, 0x89, 0x29, 0x39, 0xc5, 0x56, 0x64, 0x01, 0x27, 0x0d, 0xbb, 0xc4, 0x49,
];

const USER_NAME: &'static str = "John Doe";
const RELYING_PARTY_ID: &'static str = "localhost";
const RELYING_PARTY_NAME: &'static str = "Oost West, Thuis Best";

pub fn main() {
match _main() {
Ok(()) => {}
Err(e) => eprintln!("{}", e),
}
}

pub fn _main() -> Result<(), FidoError> {
let fido = Fido::new();
let detected_devices = fido.detect_devices(1);
let info = detected_devices.iter().next().expect("No device found");
println!("Found device: {:#?}", info);
let mut device = fido.new_device(info.path).expect("Unable to open device");
println!("Mode: {:?}", device.mode());
println!("CTAPHID info: {:#?}", device.ctap_hid_info());
println!(
"CBOR info: {:#?}",
device
.request_cbor_data()
.expect("Unable to request CBOR info")
.as_ref()
);

let mut creator = fido.new_credential_creator();
creator.set_type(CredentialType::ES256)?;
creator.set_client_data_hash(&CLIENT_DATA_HASH)?;
creator.set_relying_party(
&CString::new(RELYING_PARTY_ID).unwrap(),
&CString::new(RELYING_PARTY_NAME).unwrap(),
)?;
creator.set_user(&USER_ID, &CString::new(USER_NAME).unwrap(), None, None)?;

let credential = device.request_credential_creation(creator, None)?;
println!("Created credential: {:?}", credential.as_ref());
Ok(())
}
18 changes: 5 additions & 13 deletions src/credential.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{ffi::NonNull, FidoError, Result, FIDO_OK};
use bitflags::bitflags;
use libfido2_sys::*;
use std::{error, ffi::CStr, fmt, os::raw, slice, str::FromStr};
use std::{error, ffi::CStr, fmt, os::raw, ptr, slice, str::FromStr};

// @TODO: Create types for getters/setters instead of using byte slices
// This is out of scope for now
Expand Down Expand Up @@ -75,17 +75,17 @@ impl CredentialCreator {
&mut self,
user_id: &[u8],
name: &CStr,
display_name: &CStr,
icon: &CStr,
display_name: Option<&CStr>,
account_image_uri: Option<&CStr>,
) -> Result<()> {
unsafe {
match fido_cred_set_user(
self.0.raw.as_ptr_mut(),
user_id as *const _ as *const _,
user_id.len(),
name.as_ptr(),
display_name.as_ptr(),
icon.as_ptr(),
display_name.map(CStr::as_ptr).unwrap_or(ptr::null()),
account_image_uri.map(CStr::as_ptr).unwrap_or(ptr::null()),
) {
FIDO_OK => Ok(()),
err => Err(FidoError(err)),
Expand Down Expand Up @@ -205,14 +205,6 @@ impl Credential {
}
}

pub fn into_creator(self) -> CredentialCreator {
CredentialCreator(self)
}

pub fn into_verifier(self) -> CredentialVerifier {
CredentialVerifier(self)
}

fn set_options(&mut self, options: CredentialOptions) -> Result<()> {
unsafe {
match fido_cred_set_options(
Expand Down
13 changes: 10 additions & 3 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ pub struct Device {
}

impl Device {
/// Returns whether the device supports FIDO2.
pub fn is_fido2(&self) -> bool {
unsafe { fido_dev_is_fido2(self.raw.as_ptr()) }
/// Returns the latest mode the device supports.
pub fn mode(&self) -> DeviceMode {
unsafe {
if fido_dev_is_fido2(self.raw.as_ptr()) {
DeviceMode::Fido2
} else {
DeviceMode::FidoU2F
}
}
}

/// Forces the communication to follow the chosen standard.
pub fn force_mode(&mut self, mode: DeviceMode) {
unsafe {
match mode {
Expand Down
18 changes: 16 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Fido {
pub fn new() -> Self {
LIB_INITIALIZED.call_once(|| unsafe {
// Argument can be 0 for no debugging, or FIDO_DEBUG for debugging
fido_init(0);
fido_init(FIDO_DEBUG);
});

Fido { _private: () }
Expand All @@ -58,7 +58,21 @@ impl Fido {
}
}

pub fn new_credential(&self) -> Credential {
/// Creates a new [`CredentialCreator`].
///
/// [`CredentialCreator`]: struct.CredentialCreator.html
pub fn new_credential_creator(&self) -> CredentialCreator {
CredentialCreator(self.allocate_credential())
}

/// Creates a new [`CredentialVerifier`].
///
/// [`CredentialVerifier`]: struct.CredentialVerifier.html
pub fn new_credential_verifier(&self) -> CredentialVerifier {
CredentialVerifier(self.allocate_credential())
}

fn allocate_credential(&self) -> Credential {
unsafe {
Credential {
raw: NonNull::new(fido_cred_new()).unwrap(),
Expand Down

0 comments on commit d55ab2c

Please sign in to comment.