Skip to content

Commit c9a6692

Browse files
committed
Make finalize_result take a FidoDevice as input
1 parent d37f5ad commit c9a6692

File tree

6 files changed

+30
-19
lines changed

6 files changed

+30
-19
lines changed

src/ctap2/commands/get_assertion.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl GetAssertion {
191191
}
192192
}
193193

194-
pub fn finalize_result(&self, result: &mut GetAssertionResult) {
194+
pub fn finalize_result<Dev: FidoDevice>(&self, _dev: &Dev, result: &mut GetAssertionResult) {
195195
// Handle extensions whose outputs are not encoded in the authenticator data.
196196
// 1. appId
197197
if let Some(app_id) = &self.extensions.app_id {
@@ -325,8 +325,9 @@ impl RequestCtap1 for GetAssertion {
325325
Ok((apdu, key_handle.clone()))
326326
}
327327

328-
fn handle_response_ctap1(
328+
fn handle_response_ctap1<Dev: FidoDevice>(
329329
&self,
330+
dev: &mut Dev,
330331
status: Result<(), ApduErrorStatus>,
331332
input: &[u8],
332333
add_info: &PublicKeyCredentialDescriptor,
@@ -340,7 +341,7 @@ impl RequestCtap1 for GetAssertion {
340341

341342
let mut result = GetAssertionResult::from_ctap1(input, &self.rp.hash(), add_info)
342343
.map_err(|e| Retryable::Error(HIDError::Command(e)))?;
343-
self.finalize_result(&mut result);
344+
self.finalize_result(dev, &mut result);
344345
// Although there's only one result, we return a vector for consistency with CTAP2.
345346
Ok(vec![result])
346347
}
@@ -351,7 +352,7 @@ impl RequestCtap1 for GetAssertion {
351352
) -> Result<Self::Output, HIDError> {
352353
let mut results = dev.get_assertion(self)?;
353354
for result in results.iter_mut() {
354-
self.finalize_result(result);
355+
self.finalize_result(dev, result);
355356
}
356357
Ok(results)
357358
}
@@ -412,7 +413,7 @@ impl RequestCtap2 for GetAssertion {
412413
}
413414

414415
for result in results.iter_mut() {
415-
self.finalize_result(result);
416+
self.finalize_result(dev, result);
416417
}
417418
Ok(results)
418419
} else {
@@ -427,7 +428,7 @@ impl RequestCtap2 for GetAssertion {
427428
) -> Result<Self::Output, HIDError> {
428429
let mut results = dev.get_assertion(self)?;
429430
for result in results.iter_mut() {
430-
self.finalize_result(result);
431+
self.finalize_result(dev, result);
431432
}
432433
Ok(results)
433434
}

src/ctap2/commands/get_version.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{CommandError, RequestCtap1, Retryable};
22
use crate::consts::U2F_VERSION;
33
use crate::transport::errors::{ApduErrorStatus, HIDError};
4-
use crate::transport::VirtualFidoDevice;
4+
use crate::transport::{FidoDevice, VirtualFidoDevice};
55
use crate::u2ftypes::CTAP1RequestAPDU;
66

77
#[allow(non_camel_case_types)]
@@ -18,8 +18,9 @@ impl RequestCtap1 for GetVersion {
1818
type Output = U2FInfo;
1919
type AdditionalInfo = ();
2020

21-
fn handle_response_ctap1(
21+
fn handle_response_ctap1<Dev: FidoDevice>(
2222
&self,
23+
_dev: &mut Dev,
2324
_status: Result<(), ApduErrorStatus>,
2425
input: &[u8],
2526
_add_info: &(),

src/ctap2/commands/make_credentials.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ impl MakeCredentials {
300300
}
301301
}
302302

303-
pub fn finalize_result(&self, result: &mut MakeCredentialsResult) {
303+
pub fn finalize_result<Dev: FidoDevice>(&self, _dev: &Dev, result: &mut MakeCredentialsResult) {
304304
// Handle extensions whose outputs are not encoded in the authenticator data.
305305
// 1. credProps
306306
// "set clientExtensionResults["credProps"]["rk"] to the value of the
@@ -451,8 +451,9 @@ impl RequestCtap1 for MakeCredentials {
451451
Ok((apdu, ()))
452452
}
453453

454-
fn handle_response_ctap1(
454+
fn handle_response_ctap1<Dev: FidoDevice>(
455455
&self,
456+
dev: &mut Dev,
456457
status: Result<(), ApduErrorStatus>,
457458
input: &[u8],
458459
_add_info: &(),
@@ -466,7 +467,7 @@ impl RequestCtap1 for MakeCredentials {
466467

467468
let mut output = MakeCredentialsResult::from_ctap1(input, &self.rp.hash())
468469
.map_err(|e| Retryable::Error(HIDError::Command(e)))?;
469-
self.finalize_result(&mut output);
470+
self.finalize_result(dev, &mut output);
470471
Ok(output)
471472
}
472473

@@ -475,7 +476,7 @@ impl RequestCtap1 for MakeCredentials {
475476
dev: &mut Dev,
476477
) -> Result<Self::Output, HIDError> {
477478
let mut output = dev.make_credentials(self)?;
478-
self.finalize_result(&mut output);
479+
self.finalize_result(dev, &mut output);
479480
Ok(output)
480481
}
481482
}
@@ -493,7 +494,7 @@ impl RequestCtap2 for MakeCredentials {
493494

494495
fn handle_response_ctap2<Dev: FidoDevice>(
495496
&self,
496-
_dev: &mut Dev,
497+
dev: &mut Dev,
497498
input: &[u8],
498499
) -> Result<Self::Output, HIDError> {
499500
if input.is_empty() {
@@ -512,7 +513,7 @@ impl RequestCtap2 for MakeCredentials {
512513
if status.is_ok() {
513514
let mut output: MakeCredentialsResult =
514515
from_slice(&input[1..]).map_err(CommandError::Deserializing)?;
515-
self.finalize_result(&mut output);
516+
self.finalize_result(dev, &mut output);
516517
Ok(output)
517518
} else {
518519
let data: Value = from_slice(&input[1..]).map_err(CommandError::Deserializing)?;
@@ -528,7 +529,7 @@ impl RequestCtap2 for MakeCredentials {
528529
dev: &mut Dev,
529530
) -> Result<Self::Output, HIDError> {
530531
let mut output = dev.make_credentials(self)?;
531-
self.finalize_result(&mut output);
532+
self.finalize_result(dev, &mut output);
532533
Ok(output)
533534
}
534535
}
@@ -688,8 +689,14 @@ pub mod test {
688689
req_serialized, MAKE_CREDENTIALS_SAMPLE_REQUEST_CTAP1,
689690
"\nGot: {req_serialized:X?}\nExpected: {MAKE_CREDENTIALS_SAMPLE_REQUEST_CTAP1:X?}"
690691
);
692+
let mut device = Device::new("commands/make_credentials").unwrap(); // not really used
691693
let make_cred_result = req
692-
.handle_response_ctap1(Ok(()), &MAKE_CREDENTIALS_SAMPLE_RESPONSE_CTAP1, &())
694+
.handle_response_ctap1(
695+
&mut device,
696+
Ok(()),
697+
&MAKE_CREDENTIALS_SAMPLE_RESPONSE_CTAP1,
698+
&(),
699+
)
693700
.expect("Failed to handle CTAP1 response");
694701

695702
let att_obj = AttestationObject {

src/ctap2/commands/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ pub trait RequestCtap1: fmt::Debug {
6060
fn ctap1_format(&self) -> Result<(Vec<u8>, Self::AdditionalInfo), HIDError>;
6161

6262
/// Deserializes a response from FIDO v1.x / CTAP1 / U2Fv2 format.
63-
fn handle_response_ctap1(
63+
fn handle_response_ctap1<Dev: FidoDevice>(
6464
&self,
65+
dev: &mut Dev,
6566
status: Result<(), ApduErrorStatus>,
6667
input: &[u8],
6768
add_info: &Self::AdditionalInfo,

src/ctap2/preflight.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ impl<'assertion> RequestCtap1 for CheckKeyHandle<'assertion> {
4444
Ok((apdu, ()))
4545
}
4646

47-
fn handle_response_ctap1(
47+
fn handle_response_ctap1<Dev: FidoDevice>(
4848
&self,
49+
_dev: &mut Dev,
4950
status: Result<(), ApduErrorStatus>,
5051
_input: &[u8],
5152
_add_info: &Self::AdditionalInfo,

src/transport/hid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ impl<T: HIDDevice> FidoDeviceIO for T {
215215
// This will bubble up error if status != no error
216216
let status = ApduErrorStatus::from([status[0], status[1]]);
217217

218-
match msg.handle_response_ctap1(status, &data, &add_info) {
218+
match msg.handle_response_ctap1(self, status, &data, &add_info) {
219219
Ok(out) => return Ok(out),
220220
Err(Retryable::Retry) => {
221221
// sleep 100ms then loop again

0 commit comments

Comments
 (0)