Compare commits
4 Commits
serde_cbor
...
2.1
Author | SHA1 | Date | |
---|---|---|---|
03d55f3ebd
|
|||
e86953077b
|
|||
5da5ac54f0
|
|||
bb202c8a54
|
24
.drone.yml
24
.drone.yml
@@ -1,24 +0,0 @@
|
|||||||
kind: pipeline
|
|
||||||
name: default
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: fmt
|
|
||||||
image: rust:1.43.0
|
|
||||||
commands:
|
|
||||||
- rustup component add rustfmt
|
|
||||||
- cargo fmt --all -- --check
|
|
||||||
- name: test
|
|
||||||
image: rust:1.43.0
|
|
||||||
commands:
|
|
||||||
- cargo test --all-features
|
|
||||||
- name: publish
|
|
||||||
image: rust:1.43.0
|
|
||||||
environment:
|
|
||||||
CARGO_REGISTRY_TOKEN:
|
|
||||||
from_secret: cargo_tkn
|
|
||||||
commands:
|
|
||||||
- grep -E 'version ?= ?"${DRONE_TAG}"' -i Cargo.toml || (printf "incorrect crate/tag version" && exit 1)
|
|
||||||
- cargo package --all-features
|
|
||||||
- cargo publish --all-features
|
|
||||||
when:
|
|
||||||
event: tag
|
|
@@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "ctap_hmac"
|
name = "ctap_hmac"
|
||||||
description = "A Rust implementation of the FIDO2 CTAP protocol, including the HMAC extension"
|
description = "A Rust implementation of the FIDO2 CTAP protocol, including the HMAC extension"
|
||||||
version = "0.4.0"
|
version = "0.4.2"
|
||||||
license = "Apache-2.0/MIT"
|
license = "Apache-2.0/MIT"
|
||||||
homepage = "https://github.com/shimunn/ctap"
|
homepage = "https://github.com/shimunn/ctap"
|
||||||
repository = "https://github.com/shimunn/ctap"
|
repository = "https://github.com/shimunn/ctap"
|
||||||
@@ -23,10 +23,6 @@ rust-crypto = "0.2"
|
|||||||
csv-core = "0.1.6"
|
csv-core = "0.1.6"
|
||||||
derive_builder = "0.9.0"
|
derive_builder = "0.9.0"
|
||||||
crossbeam = { version = "0.7.3", optional = true }
|
crossbeam = { version = "0.7.3", optional = true }
|
||||||
serde_derive = "1.0.106"
|
|
||||||
serde = "1.0.106"
|
|
||||||
serde_cbor = "0.11.1"
|
|
||||||
serde_bytes = "0.11.3"
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
crossbeam = "0.7.3"
|
crossbeam = "0.7.3"
|
||||||
hex = "0.4.0"
|
hex = "0.4.0"
|
||||||
|
@@ -2,7 +2,7 @@ extern crate ctap_hmac as ctap;
|
|||||||
|
|
||||||
use crypto::digest::Digest;
|
use crypto::digest::Digest;
|
||||||
use crypto::sha2::Sha256;
|
use crypto::sha2::Sha256;
|
||||||
use ctap::extensions::hmac::HmacExtension;
|
use ctap::extensions::{self, FidoExtensionResponseParserExt};
|
||||||
use ctap::{FidoAssertionRequestBuilder, FidoCredential, FidoCredentialRequestBuilder};
|
use ctap::{FidoAssertionRequestBuilder, FidoCredential, FidoCredentialRequestBuilder};
|
||||||
use hex;
|
use hex;
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
@@ -16,14 +16,13 @@ fn main() -> ctap::FidoResult<()> {
|
|||||||
let mut devices = ctap::get_devices()?;
|
let mut devices = ctap::get_devices()?;
|
||||||
let device_info = &mut devices.next().expect("No authenticator found");
|
let device_info = &mut devices.next().expect("No authenticator found");
|
||||||
let mut device = ctap::FidoDevice::new(device_info)?;
|
let mut device = ctap::FidoDevice::new(device_info)?;
|
||||||
|
let credential = match args().nth(1).map(|h| FidoCredential {
|
||||||
let credential = match args().skip(1).next().map(|h| FidoCredential {
|
|
||||||
id: hex::decode(&h).expect("Invalid credential"),
|
id: hex::decode(&h).expect("Invalid credential"),
|
||||||
public_key: None,
|
public_key: None,
|
||||||
}) {
|
}) {
|
||||||
Some(cred) => cred,
|
Some(cred) => cred,
|
||||||
_ => {
|
_ => {
|
||||||
let req = FidoCredentialRequestBuilder::default()
|
let mut req = FidoCredentialRequestBuilder::default()
|
||||||
.rp_id(RP_ID)
|
.rp_id(RP_ID)
|
||||||
.rp_name("ctap_hmac crate")
|
.rp_name("ctap_hmac crate")
|
||||||
.user_name("example")
|
.user_name("example")
|
||||||
@@ -31,9 +30,16 @@ fn main() -> ctap::FidoResult<()> {
|
|||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
&device.supports_extension::<extensions::HmacSecret>(),
|
||||||
|
"Your device does not support the hmac extension"
|
||||||
|
);
|
||||||
|
let hmac = extensions::HmacSecret::new();
|
||||||
|
req.with_extension(&hmac)?;
|
||||||
|
dbg!(&req);
|
||||||
println!("Authorize using your device");
|
println!("Authorize using your device");
|
||||||
let cred = device
|
let cred = req
|
||||||
.make_hmac_credential(&req)
|
.make_credential(&mut device)
|
||||||
.expect("Failed to request credential");
|
.expect("Failed to request credential");
|
||||||
println!("Credential: {}\nNote: You can pass this credential as first argument in order to reproduce results", hex::encode(&cred.id));
|
println!("Credential: {}\nNote: You can pass this credential as first argument in order to reproduce results", hex::encode(&cred.id));
|
||||||
cred
|
cred
|
||||||
@@ -52,12 +58,15 @@ fn main() -> ctap::FidoResult<()> {
|
|||||||
digest.input(&message.as_bytes());
|
digest.input(&message.as_bytes());
|
||||||
digest.result(&mut salt);
|
digest.result(&mut salt);
|
||||||
let credential = &&credential;
|
let credential = &&credential;
|
||||||
let request = FidoAssertionRequestBuilder::default()
|
let hmac = extensions::HmacSecret::new().for_device(&mut device, &salt, None)?;
|
||||||
|
let mut request = FidoAssertionRequestBuilder::default()
|
||||||
.rp_id(RP_ID)
|
.rp_id(RP_ID)
|
||||||
.credential(credential)
|
.credential(credential)
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let (_cred, (hash1, _hash2)) = device.get_hmac_assertion(&request, &salt, None)?;
|
request.with_extension(&hmac)?;
|
||||||
|
let (_cred, auth_data) = device.get_assertion(&request)?;
|
||||||
|
let (hash1, _hash2) = auth_data.parse_extension_data(&hmac)?;
|
||||||
println!("Hash: {}", hex::encode(&hash1));
|
println!("Hash: {}", hex::encode(&hash1));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@@ -24,7 +24,6 @@ fn main() -> ctap::FidoResult<()> {
|
|||||||
FidoDevice::new(&h).and_then(|mut dev| {
|
FidoDevice::new(&h).and_then(|mut dev| {
|
||||||
FidoCredentialRequestBuilder::default()
|
FidoCredentialRequestBuilder::default()
|
||||||
.rp_id(RP_ID)
|
.rp_id(RP_ID)
|
||||||
.user_name("test")
|
|
||||||
.build()
|
.build()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.make_credential(&mut dev)
|
.make_credential(&mut dev)
|
||||||
|
80
src/cbor.rs
80
src/cbor.rs
@@ -274,6 +274,12 @@ pub struct GetInfoResponse {
|
|||||||
pub options: OptionsInfo,
|
pub options: OptionsInfo,
|
||||||
pub max_msg_size: u16,
|
pub max_msg_size: u16,
|
||||||
pub pin_protocols: Vec<u8>,
|
pub pin_protocols: Vec<u8>,
|
||||||
|
pub max_credential_count_in_list: Option<u32>,
|
||||||
|
pub max_credential_id_len: Option<u32>,
|
||||||
|
pub transports: Vec<String>,
|
||||||
|
pub algorithms: Vec<CoseKey>,
|
||||||
|
pub max_authenticator_config_len: Option<u32>,
|
||||||
|
pub default_cred_protect: Option<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GetInfoResponse {
|
impl GetInfoResponse {
|
||||||
@@ -282,28 +288,49 @@ impl GetInfoResponse {
|
|||||||
if status != 0 {
|
if status != 0 {
|
||||||
Err(FidoErrorKind::CborError(CborErrorCode::from(status)))?
|
Err(FidoErrorKind::CborError(CborErrorCode::from(status)))?
|
||||||
}
|
}
|
||||||
let mut decoder = Decoder::new(Config::default(), reader);
|
let mut generic = GenericDecoder::new(Config::default(), reader);
|
||||||
let mut response = GetInfoResponse::default();
|
let mut response = GetInfoResponse::default();
|
||||||
for _ in 0..decoder.object()? {
|
for _ in 0..generic.borrow_mut().object()? {
|
||||||
match decoder.u8()? {
|
match generic.borrow_mut().u8()? {
|
||||||
0x01 => {
|
0x01 => {
|
||||||
|
let decoder = generic.borrow_mut();
|
||||||
for _ in 0..decoder.array()? {
|
for _ in 0..decoder.array()? {
|
||||||
response.versions.push(decoder.text()?);
|
response.versions.push(decoder.text()?);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
0x02 => {
|
0x02 => {
|
||||||
|
let decoder = generic.borrow_mut();
|
||||||
for _ in 0..decoder.array()? {
|
for _ in 0..decoder.array()? {
|
||||||
response.extensions.push(decoder.text()?);
|
response.extensions.push(decoder.text()?);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
0x03 => response.aaguid.copy_from_slice(&decoder.bytes()?[..]),
|
0x03 => response
|
||||||
0x04 => response.options = OptionsInfo::decode(&mut decoder)?,
|
.aaguid
|
||||||
0x05 => response.max_msg_size = decoder.u16()?,
|
.copy_from_slice(&generic.borrow_mut().bytes()?[..]),
|
||||||
|
0x04 => response.options = OptionsInfo::decode(&mut generic.borrow_mut())?,
|
||||||
|
0x05 => response.max_msg_size = generic.borrow_mut().u16()?,
|
||||||
0x06 => {
|
0x06 => {
|
||||||
|
let decoder = generic.borrow_mut();
|
||||||
for _ in 0..decoder.array()? {
|
for _ in 0..decoder.array()? {
|
||||||
response.pin_protocols.push(decoder.u8()?);
|
response.pin_protocols.push(decoder.u8()?);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
0x07 => response.max_credential_count_in_list = Some(generic.borrow_mut().u32()?),
|
||||||
|
0x08 => response.max_credential_id_len = Some(generic.borrow_mut().u32()?),
|
||||||
|
0x09 => {
|
||||||
|
let decoder = generic.borrow_mut();
|
||||||
|
for _ in 0..decoder.array()? {
|
||||||
|
response.transports.push(decoder.text()?);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
0x0A => {
|
||||||
|
for _ in 0..generic.borrow_mut().array()? {
|
||||||
|
response.algorithms.push(CoseKey::decode(&mut generic)?);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
0x0B => response.max_authenticator_config_len = Some(generic.borrow_mut().u32()?),
|
||||||
|
|
||||||
|
0x0C => response.default_cred_protect = Some(generic.borrow_mut().u8()?),
|
||||||
_ => continue,
|
_ => continue,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -565,7 +592,7 @@ impl P256Key {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default, Clone)]
|
||||||
pub struct CoseKey {
|
pub struct CoseKey {
|
||||||
key_type: u16,
|
key_type: u16,
|
||||||
algorithm: i32,
|
algorithm: i32,
|
||||||
@@ -597,45 +624,14 @@ impl CoseKey {
|
|||||||
let mut cose_key = CoseKey::default();
|
let mut cose_key = CoseKey::default();
|
||||||
cose_key.algorithm = -7;
|
cose_key.algorithm = -7;
|
||||||
for _ in 0..items {
|
for _ in 0..items {
|
||||||
match generic.value()? {
|
match generic.borrow_mut().i16()? {
|
||||||
Value::Text(value::Text::Text(text)) => match &text[..] {
|
|
||||||
"type" => {
|
|
||||||
cose_key.key_type = match generic.value()? {
|
|
||||||
Value::Text(value::Text::Text(type_)) if &type_ == "public-key" => 0u16,
|
|
||||||
Value::U16(i) => i,
|
|
||||||
Value::U8(i) => i.into(),
|
|
||||||
_ => {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"alg" => cose_key.algorithm = generic.borrow_mut().i32()?,
|
|
||||||
_ => continue,
|
|
||||||
},
|
|
||||||
val @ Value::I8(_)
|
|
||||||
| val @ Value::I16(_)
|
|
||||||
| val @ Value::U16(_)
|
|
||||||
| val @ Value::U8(_) => {
|
|
||||||
let int_val = match val {
|
|
||||||
Value::I8(i) => i as i32,
|
|
||||||
Value::I16(i) => i as i32,
|
|
||||||
Value::U8(i) => i as i32,
|
|
||||||
Value::U16(i) => i as i32,
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
match int_val {
|
|
||||||
0x01 => cose_key.key_type = generic.borrow_mut().u16()?,
|
0x01 => cose_key.key_type = generic.borrow_mut().u16()?,
|
||||||
0x02 => cose_key.algorithm = generic.borrow_mut().i32()?,
|
0x02 => cose_key.algorithm = generic.borrow_mut().i32()?,
|
||||||
key if key < 0 => {
|
key if key < 0 => {
|
||||||
cose_key.parameters.insert(key as i16, generic.value()?);
|
cose_key.parameters.insert(key, generic.value()?);
|
||||||
}
|
}
|
||||||
unknown => {
|
_ => {
|
||||||
(unknown, generic.value()?); // skip unknown parameter
|
generic.value()?; // skip unknown parameter
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
unknown => {
|
|
||||||
(unknown, generic.value()?); // skip unknown parameter
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -15,7 +15,7 @@ use rust_crypto::buffer::{RefReadBuffer, RefWriteBuffer};
|
|||||||
use rust_crypto::symmetriccipher::{Decryptor, Encryptor};
|
use rust_crypto::symmetriccipher::{Decryptor, Encryptor};
|
||||||
use untrusted::Input;
|
use untrusted::Input;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct SharedSecret {
|
pub struct SharedSecret {
|
||||||
pub public_key: CoseKey,
|
pub public_key: CoseKey,
|
||||||
pub shared_secret: [u8; 32],
|
pub shared_secret: [u8; 32],
|
||||||
|
57
src/extensions/cred_protect.rs
Normal file
57
src/extensions/cred_protect.rs
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
use crate::extensions::FidoExtension;
|
||||||
|
use crate::FidoResult;
|
||||||
|
use crate::{FidoAssertionRequest, FidoCredentialRequest};
|
||||||
|
use cbor_codec::value::Value;
|
||||||
|
use num_traits::ToPrimitive;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, FromPrimitive, ToPrimitive, PartialEq)]
|
||||||
|
pub enum CredProtectLevel {
|
||||||
|
UvOptional = 0x01,
|
||||||
|
UvOptionalWithCredentialIDList = 0x02,
|
||||||
|
UvRequired = 0x03,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CredProtectLevel {
|
||||||
|
fn extension_input(self) -> Value {
|
||||||
|
Value::U8(self.to_u8().unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CredProtect(Value);
|
||||||
|
|
||||||
|
impl CredProtect {
|
||||||
|
pub fn level(level: CredProtectLevel) -> Self {
|
||||||
|
Self(level.extension_input())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extension_name() -> &'static str {
|
||||||
|
"credProtect"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extension_input(&self) -> &Value {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FidoExtension for CredProtect {
|
||||||
|
fn extension_name() -> &'static str {
|
||||||
|
CredProtect::extension_name()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn patch_assertion_request<'a, 'b>(
|
||||||
|
&'b self,
|
||||||
|
_request: &mut FidoAssertionRequest<'a, 'b>,
|
||||||
|
) -> FidoResult<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn patch_credential_request<'a>(
|
||||||
|
&'a self,
|
||||||
|
request: &mut FidoCredentialRequest<'a>,
|
||||||
|
) -> FidoResult<()> {
|
||||||
|
request
|
||||||
|
.extension_data
|
||||||
|
.insert(CredProtect::extension_name(), self.extension_input());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
@@ -1,8 +1,14 @@
|
|||||||
|
use crate::cbor::AuthenticatorData;
|
||||||
|
use crate::crypto::SharedSecret;
|
||||||
|
use crate::extensions::{
|
||||||
|
FidoExtension, FidoExtensionResponseParser, FidoExtensionResponseParserExt,
|
||||||
|
};
|
||||||
use crate::{FidoAssertionRequest, FidoAssertionRequestBuilder, FidoCredentialRequest};
|
use crate::{FidoAssertionRequest, FidoAssertionRequestBuilder, FidoCredentialRequest};
|
||||||
use crate::{FidoCredential, FidoDevice, FidoErrorKind, FidoResult};
|
use crate::{FidoCredential, FidoDevice, FidoErrorKind, FidoResult};
|
||||||
use cbor_codec::value::{Bytes, Int, Key, Text, Value};
|
use cbor_codec::value::{Bytes, Int, Key, Text, Value};
|
||||||
use cbor_codec::Encoder;
|
use cbor_codec::Encoder;
|
||||||
use cbor_codec::{Config, GenericDecoder};
|
use cbor_codec::{Config, GenericDecoder};
|
||||||
|
use failure::ResultExt;
|
||||||
use rust_crypto::buffer::{RefReadBuffer, RefWriteBuffer};
|
use rust_crypto::buffer::{RefReadBuffer, RefWriteBuffer};
|
||||||
use rust_crypto::digest::Digest;
|
use rust_crypto::digest::Digest;
|
||||||
use rust_crypto::hmac::Hmac;
|
use rust_crypto::hmac::Hmac;
|
||||||
@@ -11,6 +17,10 @@ use rust_crypto::sha2::Sha256;
|
|||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
|
|
||||||
|
#[deprecated(
|
||||||
|
since = "0.4.2",
|
||||||
|
note = "Please use FidoAssertionRequest::with_extension(HmacSecret) instead"
|
||||||
|
)]
|
||||||
pub trait HmacExtension {
|
pub trait HmacExtension {
|
||||||
fn extension_name() -> &'static str {
|
fn extension_name() -> &'static str {
|
||||||
"hmac-secret"
|
"hmac-secret"
|
||||||
@@ -83,9 +93,75 @@ pub trait HmacExtension {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(deprecated)]
|
||||||
impl HmacExtension for FidoDevice {
|
impl HmacExtension for FidoDevice {
|
||||||
fn get_data(&mut self, salt: &[u8; 32], salt2: Option<&[u8; 32]>) -> FidoResult<Value> {
|
fn get_data(&mut self, salt: &[u8; 32], salt2: Option<&[u8; 32]>) -> FidoResult<Value> {
|
||||||
let shared_secret = self.shared_secret.as_ref().unwrap();
|
Ok(HmacSecret::extension_input(self, salt, salt2)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make_hmac_credential(
|
||||||
|
&mut self,
|
||||||
|
request: &FidoCredentialRequest,
|
||||||
|
) -> FidoResult<FidoCredential> {
|
||||||
|
let mut request = request.clone();
|
||||||
|
request.rk = true;
|
||||||
|
request.extension_data.insert(
|
||||||
|
<Self as HmacExtension>::extension_name(),
|
||||||
|
<Self as HmacExtension>::extension_input(),
|
||||||
|
);
|
||||||
|
self.make_credential(&request)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_hmac_assertion<'a: 'b, 'b>(
|
||||||
|
&mut self,
|
||||||
|
request: &FidoAssertionRequest<'a, 'b>,
|
||||||
|
salt: &[u8; 32],
|
||||||
|
salt2: Option<&[u8; 32]>,
|
||||||
|
) -> FidoResult<(&'a FidoCredential, ([u8; 32], Option<[u8; 32]>))> {
|
||||||
|
while self.shared_secret.is_none() {
|
||||||
|
self.init_shared_secret()?;
|
||||||
|
}
|
||||||
|
let mut request = request.clone();
|
||||||
|
let ext = HmacSecret::new().for_device(self, salt, salt2)?;
|
||||||
|
request.with_extension(&ext)?;
|
||||||
|
|
||||||
|
let (cred, auth_data) = self.get_assertion(&request)?;
|
||||||
|
|
||||||
|
let cred = request
|
||||||
|
.credentials
|
||||||
|
.iter()
|
||||||
|
.find(|c| c.id == cred.id)
|
||||||
|
.unwrap();
|
||||||
|
Ok((cred, auth_data.parse_extension_data(&ext)?))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum HmacSecret {
|
||||||
|
Assertion {
|
||||||
|
extension_data: Value,
|
||||||
|
shared_secret: SharedSecret,
|
||||||
|
salt2: bool,
|
||||||
|
},
|
||||||
|
Credential,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HmacSecret {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self::Credential
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extension_input(
|
||||||
|
device: &mut FidoDevice,
|
||||||
|
salt: &[u8; 32],
|
||||||
|
salt2: Option<&[u8; 32]>,
|
||||||
|
) -> FidoResult<Value> {
|
||||||
|
let shared_secret = loop {
|
||||||
|
if let Some(ref secret) = device.shared_secret {
|
||||||
|
break secret;
|
||||||
|
}
|
||||||
|
device.init_shared_secret()?;
|
||||||
|
};
|
||||||
let mut encryptor = shared_secret.encryptor();
|
let mut encryptor = shared_secret.encryptor();
|
||||||
let mut salt_enc = [0u8; 64];
|
let mut salt_enc = [0u8; 64];
|
||||||
let mut output = RefWriteBuffer::new(&mut salt_enc);
|
let mut output = RefWriteBuffer::new(&mut salt_enc);
|
||||||
@@ -113,7 +189,7 @@ impl HmacExtension for FidoDevice {
|
|||||||
let mut map = BTreeMap::new();
|
let mut map = BTreeMap::new();
|
||||||
map.insert(
|
map.insert(
|
||||||
Key::Int(Int::from_i64(0x01)),
|
Key::Int(Int::from_i64(0x01)),
|
||||||
key_agreement().map_err(|_| FidoErrorKind::Io)?,
|
key_agreement().context(FidoErrorKind::Io)?,
|
||||||
);
|
);
|
||||||
map.insert(
|
map.insert(
|
||||||
Key::Int(Int::from_i64(0x02)),
|
Key::Int(Int::from_i64(0x02)),
|
||||||
@@ -136,42 +212,67 @@ impl HmacExtension for FidoDevice {
|
|||||||
Ok(Value::Map(map))
|
Ok(Value::Map(map))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_hmac_credential(
|
pub fn for_device(
|
||||||
&mut self,
|
&mut self,
|
||||||
request: &FidoCredentialRequest,
|
device: &mut FidoDevice,
|
||||||
) -> FidoResult<FidoCredential> {
|
|
||||||
let mut request = request.clone();
|
|
||||||
request.rk = true;
|
|
||||||
request.extension_data.insert(
|
|
||||||
<Self as HmacExtension>::extension_name(),
|
|
||||||
<Self as HmacExtension>::extension_input(),
|
|
||||||
);
|
|
||||||
self.make_credential(&request)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_hmac_assertion<'a: 'b, 'b>(
|
|
||||||
&mut self,
|
|
||||||
request: &FidoAssertionRequest<'a, 'b>,
|
|
||||||
salt: &[u8; 32],
|
salt: &[u8; 32],
|
||||||
salt2: Option<&[u8; 32]>,
|
salt2: Option<&[u8; 32]>,
|
||||||
) -> FidoResult<(&'a FidoCredential, ([u8; 32], Option<[u8; 32]>))> {
|
) -> FidoResult<Self> {
|
||||||
while self.shared_secret.is_none() {
|
Ok(Self::Assertion {
|
||||||
self.init_shared_secret()?;
|
extension_data: Self::extension_input(device, salt, salt2)?,
|
||||||
|
shared_secret: device.shared_secret.as_ref().unwrap().clone(),
|
||||||
|
salt2: salt2.is_some(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
let ext_data: Value = self.get_data(salt, salt2)?;
|
}
|
||||||
let mut request = request.clone();
|
|
||||||
|
impl FidoExtension for HmacSecret {
|
||||||
|
fn extension_name() -> &'static str {
|
||||||
|
"hmac-secret"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn patch_assertion_request<'a, 'b>(
|
||||||
|
&'b self,
|
||||||
|
request: &mut FidoAssertionRequest<'a, 'b>,
|
||||||
|
) -> FidoResult<()> {
|
||||||
|
match self {
|
||||||
|
Self::Assertion { extension_data, .. } => request
|
||||||
|
.extension_data
|
||||||
|
.insert(Self::extension_name(), extension_data),
|
||||||
|
_ => return Err(FidoErrorKind::DeviceUnsupported.into()),
|
||||||
|
};
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn patch_credential_request<'a>(
|
||||||
|
&'a self,
|
||||||
|
request: &mut FidoCredentialRequest<'a>,
|
||||||
|
) -> FidoResult<()> {
|
||||||
request
|
request
|
||||||
.extension_data
|
.extension_data
|
||||||
.insert(<Self as HmacExtension>::extension_name(), &ext_data);
|
.insert(Self::extension_name(), &Value::Bool(true));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let (cred, auth_data) = self.get_assertion(&request)?;
|
impl FidoExtensionResponseParser for HmacSecret {
|
||||||
let shared_secret = self.shared_secret.as_ref().unwrap();
|
type Output = ([u8; 32], Option<[u8; 32]>);
|
||||||
|
|
||||||
|
fn parse_response(&self, response: &AuthenticatorData) -> FidoResult<Self::Output> {
|
||||||
|
let (shared_secret, salt2) = match self {
|
||||||
|
Self::Assertion {
|
||||||
|
shared_secret,
|
||||||
|
salt2,
|
||||||
|
..
|
||||||
|
} => (shared_secret, salt2),
|
||||||
|
_ => return Err(FidoErrorKind::DeviceUnsupported.into()),
|
||||||
|
};
|
||||||
let mut decryptor = shared_secret.decryptor();
|
let mut decryptor = shared_secret.decryptor();
|
||||||
let mut hmac_secret_combined = [0u8; 64];
|
let mut hmac_secret_combined = [0u8; 64];
|
||||||
let _output = RefWriteBuffer::new(&mut hmac_secret_combined);
|
let _output = RefWriteBuffer::new(&mut hmac_secret_combined);
|
||||||
let hmac_secret_enc = match auth_data
|
let hmac_secret_enc = match response
|
||||||
.extensions
|
.extensions
|
||||||
.get(<Self as HmacExtension>::extension_name())
|
.get(Self::extension_name())
|
||||||
.ok_or(FidoErrorKind::CborDecode)?
|
.ok_or(FidoErrorKind::CborDecode)?
|
||||||
{
|
{
|
||||||
Value::Bytes(hmac_ciphered) => Ok(match hmac_ciphered {
|
Value::Bytes(hmac_ciphered) => Ok(match hmac_ciphered {
|
||||||
@@ -196,11 +297,6 @@ impl HmacExtension for FidoDevice {
|
|||||||
let mut hmac_secret_1 = [0u8; 32];
|
let mut hmac_secret_1 = [0u8; 32];
|
||||||
hmac_secret_0.copy_from_slice(&hmac_secret[0..32]);
|
hmac_secret_0.copy_from_slice(&hmac_secret[0..32]);
|
||||||
hmac_secret_1.copy_from_slice(&hmac_secret[32..]);
|
hmac_secret_1.copy_from_slice(&hmac_secret[32..]);
|
||||||
let cred = request
|
Ok((hmac_secret_0, Some(hmac_secret_1).filter(|_| *salt2)))
|
||||||
.credentials
|
|
||||||
.into_iter()
|
|
||||||
.find(|c| c.id == cred.id)
|
|
||||||
.unwrap();
|
|
||||||
Ok((cred, (hmac_secret_0, salt2.and(Some(hmac_secret_1)))))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,2 +1,36 @@
|
|||||||
pub mod hmac;
|
pub mod hmac;
|
||||||
pub use hmac::*;
|
pub use hmac::*;
|
||||||
|
mod cred_protect;
|
||||||
|
use crate::cbor::AuthenticatorData;
|
||||||
|
use crate::{FidoAssertionRequest, FidoCredentialRequest, FidoResult};
|
||||||
|
pub use cred_protect::*;
|
||||||
|
|
||||||
|
pub trait FidoExtension {
|
||||||
|
fn extension_name() -> &'static str;
|
||||||
|
fn patch_assertion_request<'a, 'b>(
|
||||||
|
&'b self,
|
||||||
|
request: &mut FidoAssertionRequest<'a, 'b>,
|
||||||
|
) -> FidoResult<()>;
|
||||||
|
fn patch_credential_request<'a>(
|
||||||
|
&'a self,
|
||||||
|
request: &mut FidoCredentialRequest<'a>,
|
||||||
|
) -> FidoResult<()>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait FidoExtensionResponseParser {
|
||||||
|
type Output;
|
||||||
|
fn parse_response(&self, response: &AuthenticatorData) -> FidoResult<Self::Output>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait FidoExtensionResponseParserExt<Ext: FidoExtensionResponseParser> {
|
||||||
|
fn parse_extension_data(&self, extension: &Ext) -> FidoResult<Ext::Output>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Ext: FidoExtensionResponseParser> FidoExtensionResponseParserExt<Ext> for AuthenticatorData {
|
||||||
|
fn parse_extension_data(
|
||||||
|
&self,
|
||||||
|
extension: &Ext,
|
||||||
|
) -> FidoResult<<Ext as FidoExtensionResponseParser>::Output> {
|
||||||
|
extension.parse_response(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
43
src/lib.rs
43
src/lib.rs
@@ -74,7 +74,9 @@ pub use self::error::*;
|
|||||||
use self::hid_linux as hid;
|
use self::hid_linux as hid;
|
||||||
use self::packet::CtapCommand;
|
use self::packet::CtapCommand;
|
||||||
pub use self::util::*;
|
pub use self::util::*;
|
||||||
use crate::cbor::{AuthenticatorData, GetAssertionRequest};
|
use crate::cbor::{AuthenticatorData, GetAssertionRequest, GetInfoResponse};
|
||||||
|
use crate::packet::CtapStatus;
|
||||||
|
use crate::extensions::FidoExtension;
|
||||||
use failure::{Fail, ResultExt};
|
use failure::{Fail, ResultExt};
|
||||||
use num_traits::FromPrimitive;
|
use num_traits::FromPrimitive;
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
@@ -107,6 +109,7 @@ pub struct FidoDevice {
|
|||||||
shared_secret: Option<crypto::SharedSecret>,
|
shared_secret: Option<crypto::SharedSecret>,
|
||||||
pin_token: Option<crypto::PinToken>,
|
pin_token: Option<crypto::PinToken>,
|
||||||
aaguid: [u8; 16],
|
aaguid: [u8; 16],
|
||||||
|
info: GetInfoResponse,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FidoCancelHandle {
|
pub struct FidoCancelHandle {
|
||||||
@@ -117,7 +120,7 @@ pub struct FidoCancelHandle {
|
|||||||
|
|
||||||
impl FidoCancelHandle {
|
impl FidoCancelHandle {
|
||||||
pub fn cancel(&mut self) -> FidoResult<()> {
|
pub fn cancel(&mut self) -> FidoResult<()> {
|
||||||
let payload = &[1u8];
|
let payload = &[];
|
||||||
let to_send = payload.len() as u16;
|
let to_send = payload.len() as u16;
|
||||||
let max_payload = (self.packet_size - 7) as usize;
|
let max_payload = (self.packet_size - 7) as usize;
|
||||||
let (frame, payload) = payload.split_at(cmp::min(payload.len(), max_payload));
|
let (frame, payload) = payload.split_at(cmp::min(payload.len(), max_payload));
|
||||||
@@ -204,6 +207,10 @@ impl<'a> FidoCredentialRequest<'a> {
|
|||||||
pub fn make_credential(&self, device: &mut FidoDevice) -> FidoResult<FidoCredential> {
|
pub fn make_credential(&self, device: &mut FidoDevice) -> FidoResult<FidoCredential> {
|
||||||
device.make_credential(&self)
|
device.make_credential(&self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn with_extension<Ext: FidoExtension>(&mut self, extension: &'a Ext) -> FidoResult<()> {
|
||||||
|
extension.patch_credential_request(self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Request an assertion from the authenticator for a given credential.
|
/// Request an assertion from the authenticator for a given credential.
|
||||||
@@ -239,6 +246,10 @@ impl<'a, 'b> FidoAssertionRequest<'a, 'b> {
|
|||||||
pub fn get_assertion(&self, device: &mut FidoDevice) -> FidoResult<&'a FidoCredential> {
|
pub fn get_assertion(&self, device: &mut FidoDevice) -> FidoResult<&'a FidoCredential> {
|
||||||
device.get_assertion(self).map(|res| res.0)
|
device.get_assertion(self).map(|res| res.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn with_extension<Ext: FidoExtension>(&mut self, extension: &'b Ext) -> FidoResult<()> {
|
||||||
|
extension.patch_assertion_request(self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> FidoAssertionRequestBuilder<'a, 'b> {
|
impl<'a, 'b> FidoAssertionRequestBuilder<'a, 'b> {
|
||||||
@@ -266,6 +277,7 @@ impl FidoDevice {
|
|||||||
shared_secret: None,
|
shared_secret: None,
|
||||||
pin_token: None,
|
pin_token: None,
|
||||||
aaguid: [0; 16],
|
aaguid: [0; 16],
|
||||||
|
info: GetInfoResponse::default(),
|
||||||
};
|
};
|
||||||
dev.init()?;
|
dev.init()?;
|
||||||
Ok(dev)
|
Ok(dev)
|
||||||
@@ -300,6 +312,7 @@ impl FidoDevice {
|
|||||||
}
|
}
|
||||||
self.needs_pin = response.options.client_pin == Some(true);
|
self.needs_pin = response.options.client_pin == Some(true);
|
||||||
self.aaguid = response.aaguid;
|
self.aaguid = response.aaguid;
|
||||||
|
self.info = response;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -369,6 +382,10 @@ impl FidoDevice {
|
|||||||
.context(FidoErrorKind::Io)?)
|
.context(FidoErrorKind::Io)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn supports_extension<Ext: FidoExtension>(&self) -> bool {
|
||||||
|
self.info.extensions.iter().any(|ext| ext == Ext::extension_name())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn make_credential(
|
pub fn make_credential(
|
||||||
&mut self,
|
&mut self,
|
||||||
request: &FidoCredentialRequest<'_>,
|
request: &FidoCredentialRequest<'_>,
|
||||||
@@ -538,6 +555,26 @@ impl FidoDevice {
|
|||||||
.map(|cred| (cred, response.auth_data))
|
.map(|cred| (cred, response.auth_data))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn ping(&mut self, data: &[u8]) -> FidoResult<Vec<u8>> {
|
||||||
|
self.exchange(CtapCommand::Ping, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn wink(&mut self) -> FidoResult<()> {
|
||||||
|
self.send(&CtapCommand::Wink, &[]).map(|_| ())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lock(&mut self, time_sec: u8) -> FidoResult<()> {
|
||||||
|
self.exchange(CtapCommand::Lock, &[time_sec]).map(|_| ())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn keepalive(&mut self) -> FidoResult<CtapStatus> {
|
||||||
|
self.exchange(CtapCommand::Keepalive, &[])?
|
||||||
|
.first()
|
||||||
|
.cloned()
|
||||||
|
.and_then(CtapStatus::from_u8)
|
||||||
|
.ok_or(FidoError::from(FidoErrorKind::CborDecode))
|
||||||
|
}
|
||||||
|
|
||||||
fn cbor(&mut self, request: cbor::Request) -> FidoResult<cbor::Response> {
|
fn cbor(&mut self, request: cbor::Request) -> FidoResult<cbor::Response> {
|
||||||
let mut buf = Cursor::new(Vec::new());
|
let mut buf = Cursor::new(Vec::new());
|
||||||
request
|
request
|
||||||
@@ -556,7 +593,7 @@ impl FidoDevice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn send(&mut self, cmd: &CtapCommand, payload: &[u8]) -> FidoResult<()> {
|
fn send(&mut self, cmd: &CtapCommand, payload: &[u8]) -> FidoResult<()> {
|
||||||
if payload.is_empty() || payload.len() > u16::MAX as usize {
|
if payload.len() > u16::MAX as usize {
|
||||||
Err(FidoErrorKind::WritePacket)?
|
Err(FidoErrorKind::WritePacket)?
|
||||||
}
|
}
|
||||||
let to_send = payload.len() as u16;
|
let to_send = payload.len() as u16;
|
||||||
|
@@ -13,7 +13,7 @@ use std::io::{Read, Write};
|
|||||||
static FRAME_INIT: u8 = 0x80;
|
static FRAME_INIT: u8 = 0x80;
|
||||||
|
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
#[derive(FromPrimitive, ToPrimitive, PartialEq)]
|
#[derive(Debug, FromPrimitive, ToPrimitive, PartialEq)]
|
||||||
pub enum CtapCommand {
|
pub enum CtapCommand {
|
||||||
Invalid = 0x00,
|
Invalid = 0x00,
|
||||||
Ping = 0x01,
|
Ping = 0x01,
|
||||||
@@ -36,6 +36,13 @@ impl CtapCommand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[repr(u8)]
|
||||||
|
#[derive(Debug, FromPrimitive, ToPrimitive, PartialEq)]
|
||||||
|
pub enum CtapStatus {
|
||||||
|
Processing = 0x01,
|
||||||
|
AwaitingUserPresence = 0x02,
|
||||||
|
}
|
||||||
|
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
#[derive(FromPrimitive, Fail, Debug)]
|
#[derive(FromPrimitive, Fail, Debug)]
|
||||||
pub enum CtapError {
|
pub enum CtapError {
|
||||||
|
1279
src/protocol/cbor.rs
1279
src/protocol/cbor.rs
File diff suppressed because it is too large
Load Diff
@@ -1,7 +0,0 @@
|
|||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
||||||
pub enum ProtocolError {
|
|
||||||
CborEncode,
|
|
||||||
CborDecode { data: Vec<u8> },
|
|
||||||
}
|
|
@@ -1,4 +0,0 @@
|
|||||||
mod cbor;
|
|
||||||
mod error;
|
|
||||||
pub use self::cbor::*;
|
|
||||||
pub use self::error::*;
|
|
@@ -1,9 +0,0 @@
|
|||||||
use crate::protocol::{CborRequest, CborResponse};
|
|
||||||
|
|
||||||
pub trait CtapTransport {
|
|
||||||
type Error;
|
|
||||||
|
|
||||||
fn cbor<'a>(&mut self, command: &CborRequest<'a>) -> Result<CborResponse, Self::Error>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum CtapCommand {}
|
|
Reference in New Issue
Block a user