Skip to content

Commit e581101

Browse files
committed
Uses CtapResult instead of explicit Result
Generated with ``` find . -type f -name "*.rs" -exec sed -i 's/Result<\([^,]*\), Ctap2StatusCode>/CtapResult<\1>/g' {} \; cd libraries/opensk find . -type f -name "*.rs" -exec grep -q 'CtapResult' {} \; -exec sed -i '15 i\ use crate::ctap::status_code::CtapResult; ' {} + ``` Then we fix the last few compiler errors and run `cargo fmt`. Next step is to move away from custom error types in the API to only use CtapResult everywhere.
1 parent d624558 commit e581101

13 files changed

+182
-216
lines changed

libraries/opensk/src/api/private_key.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::api::crypto::ecdsa::{SecretKey as _, Signature};
1616
use crate::ctap::crypto_wrapper::{aes256_cbc_decrypt, aes256_cbc_encrypt};
1717
use crate::ctap::data_formats::{extract_array, extract_byte_string, CoseKey, SignatureAlgorithm};
1818
use crate::ctap::secret::Secret;
19-
use crate::ctap::status_code::Ctap2StatusCode;
19+
use crate::ctap::status_code::{Ctap2StatusCode, CtapResult};
2020
use crate::env::{AesKey, EcdsaSk, Env};
2121
use alloc::vec;
2222
use alloc::vec::Vec;
@@ -89,7 +89,7 @@ impl PrivateKey {
8989
}
9090

9191
/// Returns the ECDSA private key.
92-
pub fn ecdsa_key<E: Env>(&self) -> Result<EcdsaSk<E>, Ctap2StatusCode> {
92+
pub fn ecdsa_key<E: Env>(&self) -> CtapResult<EcdsaSk<E>> {
9393
match self {
9494
PrivateKey::Ecdsa(bytes) => ecdsa_key_from_bytes::<E>(bytes),
9595
#[allow(unreachable_patterns)]
@@ -98,7 +98,7 @@ impl PrivateKey {
9898
}
9999

100100
/// Returns the corresponding public key.
101-
pub fn get_pub_key<E: Env>(&self) -> Result<CoseKey, Ctap2StatusCode> {
101+
pub fn get_pub_key<E: Env>(&self) -> CtapResult<CoseKey> {
102102
Ok(match self {
103103
PrivateKey::Ecdsa(bytes) => {
104104
CoseKey::from_ecdsa_public_key(ecdsa_key_from_bytes::<E>(bytes)?.public_key())
@@ -109,7 +109,7 @@ impl PrivateKey {
109109
}
110110

111111
/// Returns the encoded signature for a given message.
112-
pub fn sign_and_encode<E: Env>(&self, message: &[u8]) -> Result<Vec<u8>, Ctap2StatusCode> {
112+
pub fn sign_and_encode<E: Env>(&self, message: &[u8]) -> CtapResult<Vec<u8>> {
113113
Ok(match self {
114114
PrivateKey::Ecdsa(bytes) => ecdsa_key_from_bytes::<E>(bytes)?.sign(message).to_der(),
115115
#[cfg(feature = "ed25519")]
@@ -141,7 +141,7 @@ impl PrivateKey {
141141
&self,
142142
rng: &mut E::Rng,
143143
wrap_key: &AesKey<E>,
144-
) -> Result<cbor::Value, Ctap2StatusCode> {
144+
) -> CtapResult<cbor::Value> {
145145
let bytes = self.to_bytes();
146146
let wrapped_bytes = aes256_cbc_encrypt::<E>(rng, wrap_key, &bytes, true)?;
147147
Ok(cbor_array![
@@ -150,10 +150,7 @@ impl PrivateKey {
150150
])
151151
}
152152

153-
pub fn from_cbor<E: Env>(
154-
wrap_key: &AesKey<E>,
155-
cbor_value: cbor::Value,
156-
) -> Result<Self, Ctap2StatusCode> {
153+
pub fn from_cbor<E: Env>(wrap_key: &AesKey<E>, cbor_value: cbor::Value) -> CtapResult<Self> {
157154
let mut array = extract_array(cbor_value)?;
158155
if array.len() != 2 {
159156
return Err(Ctap2StatusCode::CTAP2_ERR_INVALID_CBOR);
@@ -171,7 +168,7 @@ impl PrivateKey {
171168
}
172169
}
173170

174-
fn ecdsa_key_from_bytes<E: Env>(bytes: &[u8; 32]) -> Result<EcdsaSk<E>, Ctap2StatusCode> {
171+
fn ecdsa_key_from_bytes<E: Env>(bytes: &[u8; 32]) -> CtapResult<EcdsaSk<E>> {
175172
EcdsaSk::<E>::from_slice(bytes).ok_or(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR)
176173
}
177174

libraries/opensk/src/ctap/client_pin.rs

+23-28
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::api::crypto::sha256::Sha256;
2828
use crate::api::customization::Customization;
2929
use crate::api::key_store::KeyStore;
3030
use crate::api::persist::Persist;
31+
use crate::ctap::status_code::CtapResult;
3132
use crate::ctap::storage;
3233
#[cfg(test)]
3334
use crate::env::EcdhSk;
@@ -65,7 +66,7 @@ const PIN_PADDED_LENGTH: usize = 64;
6566
fn decrypt_pin<E: Env>(
6667
shared_secret: &SharedSecret<E>,
6768
new_pin_enc: Vec<u8>,
68-
) -> Result<Secret<[u8]>, Ctap2StatusCode> {
69+
) -> CtapResult<Secret<[u8]>> {
6970
let decrypted_pin = shared_secret.decrypt(&new_pin_enc)?;
7071
if decrypted_pin.len() != PIN_PADDED_LENGTH {
7172
return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER);
@@ -91,7 +92,7 @@ fn check_and_store_new_pin<E: Env>(
9192
env: &mut E,
9293
shared_secret: &SharedSecret<E>,
9394
new_pin_enc: Vec<u8>,
94-
) -> Result<(), Ctap2StatusCode> {
95+
) -> CtapResult<()> {
9596
let pin = decrypt_pin(shared_secret, new_pin_enc)?;
9697
let min_pin_length = storage::min_pin_length(env)? as usize;
9798
let pin_length = str::from_utf8(&pin).unwrap_or("").chars().count();
@@ -168,7 +169,7 @@ impl<E: Env> ClientPin<E> {
168169
&self,
169170
pin_uv_auth_protocol: PinUvAuthProtocol,
170171
key_agreement: CoseKey,
171-
) -> Result<SharedSecret<E>, Ctap2StatusCode> {
172+
) -> CtapResult<SharedSecret<E>> {
172173
self.get_pin_protocol(pin_uv_auth_protocol)
173174
.decapsulate(key_agreement, pin_uv_auth_protocol)
174175
}
@@ -184,7 +185,7 @@ impl<E: Env> ClientPin<E> {
184185
pin_uv_auth_protocol: PinUvAuthProtocol,
185186
shared_secret: &SharedSecret<E>,
186187
pin_hash_enc: Vec<u8>,
187-
) -> Result<(), Ctap2StatusCode> {
188+
) -> CtapResult<()> {
188189
match env.persist().pin_hash()? {
189190
Some(pin_hash) => {
190191
if self.consecutive_pin_mismatches >= 3 {
@@ -217,10 +218,7 @@ impl<E: Env> ClientPin<E> {
217218
Ok(())
218219
}
219220

220-
fn process_get_pin_retries(
221-
&self,
222-
env: &mut E,
223-
) -> Result<AuthenticatorClientPinResponse, Ctap2StatusCode> {
221+
fn process_get_pin_retries(&self, env: &mut E) -> CtapResult<AuthenticatorClientPinResponse> {
224222
Ok(AuthenticatorClientPinResponse {
225223
key_agreement: None,
226224
pin_uv_auth_token: None,
@@ -232,7 +230,7 @@ impl<E: Env> ClientPin<E> {
232230
fn process_get_key_agreement(
233231
&self,
234232
client_pin_params: AuthenticatorClientPinParameters,
235-
) -> Result<AuthenticatorClientPinResponse, Ctap2StatusCode> {
233+
) -> CtapResult<AuthenticatorClientPinResponse> {
236234
let key_agreement = Some(
237235
self.get_pin_protocol(client_pin_params.pin_uv_auth_protocol)
238236
.get_public_key(),
@@ -249,7 +247,7 @@ impl<E: Env> ClientPin<E> {
249247
&mut self,
250248
env: &mut E,
251249
client_pin_params: AuthenticatorClientPinParameters,
252-
) -> Result<(), Ctap2StatusCode> {
250+
) -> CtapResult<()> {
253251
let AuthenticatorClientPinParameters {
254252
pin_uv_auth_protocol,
255253
key_agreement,
@@ -276,7 +274,7 @@ impl<E: Env> ClientPin<E> {
276274
&mut self,
277275
env: &mut E,
278276
client_pin_params: AuthenticatorClientPinParameters,
279-
) -> Result<(), Ctap2StatusCode> {
277+
) -> CtapResult<()> {
280278
let AuthenticatorClientPinParameters {
281279
pin_uv_auth_protocol,
282280
key_agreement,
@@ -309,7 +307,7 @@ impl<E: Env> ClientPin<E> {
309307
&mut self,
310308
env: &mut E,
311309
client_pin_params: AuthenticatorClientPinParameters,
312-
) -> Result<AuthenticatorClientPinResponse, Ctap2StatusCode> {
310+
) -> CtapResult<AuthenticatorClientPinResponse> {
313311
let AuthenticatorClientPinParameters {
314312
pin_uv_auth_protocol,
315313
key_agreement,
@@ -357,12 +355,12 @@ impl<E: Env> ClientPin<E> {
357355
// If you want to support local user verification, implement this function.
358356
// Lacking a fingerprint reader, this subcommand is currently unsupported.
359357
_client_pin_params: AuthenticatorClientPinParameters,
360-
) -> Result<AuthenticatorClientPinResponse, Ctap2StatusCode> {
358+
) -> CtapResult<AuthenticatorClientPinResponse> {
361359
// User verification is only supported through PIN currently.
362360
Err(Ctap2StatusCode::CTAP2_ERR_INVALID_SUBCOMMAND)
363361
}
364362

365-
fn process_get_uv_retries(&self) -> Result<AuthenticatorClientPinResponse, Ctap2StatusCode> {
363+
fn process_get_uv_retries(&self) -> CtapResult<AuthenticatorClientPinResponse> {
366364
// User verification is only supported through PIN currently.
367365
Err(Ctap2StatusCode::CTAP2_ERR_INVALID_SUBCOMMAND)
368366
}
@@ -371,7 +369,7 @@ impl<E: Env> ClientPin<E> {
371369
&mut self,
372370
env: &mut E,
373371
mut client_pin_params: AuthenticatorClientPinParameters,
374-
) -> Result<AuthenticatorClientPinResponse, Ctap2StatusCode> {
372+
) -> CtapResult<AuthenticatorClientPinResponse> {
375373
// Mutating client_pin_params is just an optimization to move it into
376374
// process_get_pin_token, without cloning permissions_rp_id here.
377375
// getPinToken requires permissions* to be None.
@@ -399,7 +397,7 @@ impl<E: Env> ClientPin<E> {
399397
&mut self,
400398
env: &mut E,
401399
client_pin_params: AuthenticatorClientPinParameters,
402-
) -> Result<ResponseData, Ctap2StatusCode> {
400+
) -> CtapResult<ResponseData> {
403401
if !env.customization().allows_pin_protocol_v1()
404402
&& client_pin_params.pin_uv_auth_protocol == PinUvAuthProtocol::V1
405403
{
@@ -441,7 +439,7 @@ impl<E: Env> ClientPin<E> {
441439
hmac_contents: &[u8],
442440
pin_uv_auth_param: &[u8],
443441
pin_uv_auth_protocol: PinUvAuthProtocol,
444-
) -> Result<(), Ctap2StatusCode> {
442+
) -> CtapResult<()> {
445443
if !self.pin_uv_auth_token_state.is_in_use() {
446444
return Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID);
447445
}
@@ -477,7 +475,7 @@ impl<E: Env> ClientPin<E> {
477475
env: &mut E,
478476
hmac_secret_input: GetAssertionHmacSecretInput,
479477
cred_random: &[u8; 32],
480-
) -> Result<Vec<u8>, Ctap2StatusCode> {
478+
) -> CtapResult<Vec<u8>> {
481479
let GetAssertionHmacSecretInput {
482480
key_agreement,
483481
salt_enc,
@@ -523,7 +521,7 @@ impl<E: Env> ClientPin<E> {
523521
}
524522

525523
/// Checks if user verification is cached for use of the pinUvAuthToken.
526-
pub fn check_user_verified_flag(&mut self) -> Result<(), Ctap2StatusCode> {
524+
pub fn check_user_verified_flag(&mut self) -> CtapResult<()> {
527525
if self.pin_uv_auth_token_state.get_user_verified_flag_value() {
528526
Ok(())
529527
} else {
@@ -532,27 +530,24 @@ impl<E: Env> ClientPin<E> {
532530
}
533531

534532
/// Check if the required command's token permission is granted.
535-
pub fn has_permission(&self, permission: PinPermission) -> Result<(), Ctap2StatusCode> {
533+
pub fn has_permission(&self, permission: PinPermission) -> CtapResult<()> {
536534
self.pin_uv_auth_token_state.has_permission(permission)
537535
}
538536

539537
/// Check if no RP ID is associated with the token permission.
540-
pub fn has_no_rp_id_permission(&self) -> Result<(), Ctap2StatusCode> {
538+
pub fn has_no_rp_id_permission(&self) -> CtapResult<()> {
541539
self.pin_uv_auth_token_state.has_no_permissions_rp_id()
542540
}
543541

544542
/// Check if no or the passed RP ID is associated with the token permission.
545-
pub fn has_no_or_rp_id_permission(&mut self, rp_id: &str) -> Result<(), Ctap2StatusCode> {
543+
pub fn has_no_or_rp_id_permission(&mut self, rp_id: &str) -> CtapResult<()> {
546544
self.pin_uv_auth_token_state
547545
.has_no_permissions_rp_id()
548546
.or_else(|_| self.pin_uv_auth_token_state.has_permissions_rp_id(rp_id))
549547
}
550548

551549
/// Check if no RP ID is associated with the token permission, or it matches the hash.
552-
pub fn has_no_or_rp_id_hash_permission(
553-
&self,
554-
rp_id_hash: &[u8],
555-
) -> Result<(), Ctap2StatusCode> {
550+
pub fn has_no_or_rp_id_hash_permission(&self, rp_id_hash: &[u8]) -> CtapResult<()> {
556551
self.pin_uv_auth_token_state
557552
.has_no_permissions_rp_id()
558553
.or_else(|_| {
@@ -564,7 +559,7 @@ impl<E: Env> ClientPin<E> {
564559
/// Check if the passed RP ID is associated with the token permission.
565560
///
566561
/// If no RP ID is associated, associate the passed RP ID as a side effect.
567-
pub fn ensure_rp_id_permission(&mut self, rp_id: &str) -> Result<(), Ctap2StatusCode> {
562+
pub fn ensure_rp_id_permission(&mut self, rp_id: &str) -> CtapResult<()> {
568563
if self
569564
.pin_uv_auth_token_state
570565
.has_no_permissions_rp_id()
@@ -1277,7 +1272,7 @@ mod test {
12771272
pin_uv_auth_protocol: PinUvAuthProtocol,
12781273
cred_random: &[u8; 32],
12791274
salt: Vec<u8>,
1280-
) -> Result<Vec<u8>, Ctap2StatusCode> {
1275+
) -> CtapResult<Vec<u8>> {
12811276
let mut env = TestEnv::default();
12821277
let (client_pin, shared_secret) = create_client_pin_and_shared_secret(pin_uv_auth_protocol);
12831278

libraries/opensk/src/ctap/command.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use super::data_formats::{
2424
#[cfg(feature = "config_command")]
2525
use super::data_formats::{ConfigSubCommand, ConfigSubCommandParams, SetMinPinLengthParams};
2626
use super::status_code::Ctap2StatusCode;
27+
use crate::ctap::status_code::CtapResult;
2728
use alloc::string::String;
2829
use alloc::vec::Vec;
2930
#[cfg(feature = "fuzz")]
@@ -70,7 +71,7 @@ impl Command {
7071
const AUTHENTICATOR_VENDOR_CREDENTIAL_MANAGEMENT: u8 = 0x41;
7172
const _AUTHENTICATOR_VENDOR_LAST: u8 = 0xBF;
7273

73-
pub fn deserialize(bytes: &[u8]) -> Result<Command, Ctap2StatusCode> {
74+
pub fn deserialize(bytes: &[u8]) -> CtapResult<Command> {
7475
if bytes.is_empty() {
7576
// The error to return is not specified, missing parameter seems to fit best.
7677
return Err(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER);
@@ -157,7 +158,7 @@ pub struct AuthenticatorMakeCredentialParameters {
157158
impl TryFrom<cbor::Value> for AuthenticatorMakeCredentialParameters {
158159
type Error = Ctap2StatusCode;
159160

160-
fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> {
161+
fn try_from(cbor_value: cbor::Value) -> CtapResult<Self> {
161162
destructure_cbor_map! {
162163
let {
163164
0x01 => client_data_hash,
@@ -181,15 +182,15 @@ impl TryFrom<cbor::Value> for AuthenticatorMakeCredentialParameters {
181182
let pub_key_cred_params = cred_param_vec
182183
.into_iter()
183184
.map(PublicKeyCredentialParameter::try_from)
184-
.collect::<Result<Vec<PublicKeyCredentialParameter>, Ctap2StatusCode>>()?;
185+
.collect::<CtapResult<Vec<PublicKeyCredentialParameter>>>()?;
185186

186187
let exclude_list = match exclude_list {
187188
Some(entry) => {
188189
let exclude_list_vec = extract_array(entry)?;
189190
let exclude_list = exclude_list_vec
190191
.into_iter()
191192
.map(PublicKeyCredentialDescriptor::try_from)
192-
.collect::<Result<Vec<PublicKeyCredentialDescriptor>, Ctap2StatusCode>>()?;
193+
.collect::<CtapResult<Vec<PublicKeyCredentialDescriptor>>>()?;
193194
Some(exclude_list)
194195
}
195196
None => None,
@@ -244,7 +245,7 @@ pub struct AuthenticatorGetAssertionParameters {
244245
impl TryFrom<cbor::Value> for AuthenticatorGetAssertionParameters {
245246
type Error = Ctap2StatusCode;
246247

247-
fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> {
248+
fn try_from(cbor_value: cbor::Value) -> CtapResult<Self> {
248249
destructure_cbor_map! {
249250
let {
250251
0x01 => rp_id,
@@ -266,7 +267,7 @@ impl TryFrom<cbor::Value> for AuthenticatorGetAssertionParameters {
266267
let allow_list = allow_list_vec
267268
.into_iter()
268269
.map(PublicKeyCredentialDescriptor::try_from)
269-
.collect::<Result<Vec<PublicKeyCredentialDescriptor>, Ctap2StatusCode>>()?;
270+
.collect::<CtapResult<Vec<PublicKeyCredentialDescriptor>>>()?;
270271
Some(allow_list)
271272
}
272273
None => None,
@@ -315,7 +316,7 @@ pub struct AuthenticatorClientPinParameters {
315316
impl TryFrom<cbor::Value> for AuthenticatorClientPinParameters {
316317
type Error = Ctap2StatusCode;
317318

318-
fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> {
319+
fn try_from(cbor_value: cbor::Value) -> CtapResult<Self> {
319320
destructure_cbor_map! {
320321
let {
321322
0x01 => pin_uv_auth_protocol,
@@ -370,7 +371,7 @@ pub struct AuthenticatorLargeBlobsParameters {
370371
impl TryFrom<cbor::Value> for AuthenticatorLargeBlobsParameters {
371372
type Error = Ctap2StatusCode;
372373

373-
fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> {
374+
fn try_from(cbor_value: cbor::Value) -> CtapResult<Self> {
374375
destructure_cbor_map! {
375376
let {
376377
0x01 => get,
@@ -432,7 +433,7 @@ pub struct AuthenticatorConfigParameters {
432433
impl TryFrom<cbor::Value> for AuthenticatorConfigParameters {
433434
type Error = Ctap2StatusCode;
434435

435-
fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> {
436+
fn try_from(cbor_value: cbor::Value) -> CtapResult<Self> {
436437
destructure_cbor_map! {
437438
let {
438439
0x01 => sub_command,
@@ -474,7 +475,7 @@ pub struct AuthenticatorCredentialManagementParameters {
474475
impl TryFrom<cbor::Value> for AuthenticatorCredentialManagementParameters {
475476
type Error = Ctap2StatusCode;
476477

477-
fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> {
478+
fn try_from(cbor_value: cbor::Value) -> CtapResult<Self> {
478479
destructure_cbor_map! {
479480
let {
480481
0x01 => sub_command,

0 commit comments

Comments
 (0)