Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android wrapper error handling #82

Merged
merged 6 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ u2f = ["auth-base", "untrusted", "serde_repr", ]
webauthn-server = [ "webauthn", "webpki" ]
webauthn = [ "auth-base", "bytes", "serde_cbor", "uuid", "http", "ed25519-dalek", "p256", "indexmap" ]
auth-base = ["base64", "byteorder", "ring", "serde", "serde_derive", "serde_json", "serde_bytes"]
android = ["jni"]

[dependencies]
sha2 = {version = "0.10" , features = ["oid"]}
Expand Down Expand Up @@ -54,7 +55,7 @@ p256 = { version = "0.13.2", optional = true }
indexmap = { version = "2.2.6", features = ["serde"], optional = true }

[target.'cfg(target_os = "android")'.dependencies]
jni = { version = "0.21.1" }
jni = { version = "0.21.1", optional = true }

[target.'cfg(target_arch="wasm32")'.dependencies]
wasm-bindgen = { version = "0.2.91" }
Expand Down
2 changes: 1 addition & 1 deletion src/oath/hotp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl OtpAuth for HOTPContext {
"otpauth://hotp/{}?secret={}&algorithm={}&digits={}&counter={}",
label.unwrap_or("slauth"),
base32::encode(base32::Alphabet::RFC4648 { padding: false }, self.secret.as_slice()),
self.alg.to_string(),
self.alg,
self.digits,
self.counter
);
Expand Down
10 changes: 6 additions & 4 deletions src/oath/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use hmac::{
};
use sha1::Sha1;
use sha2::{Sha256, Sha512};
use std::fmt::Display;

pub mod hotp;
pub mod totp;
Expand Down Expand Up @@ -71,13 +72,14 @@ impl HashesAlgorithm {
}
}

impl ToString for HashesAlgorithm {
fn to_string(&self) -> String {
match self {
impl Display for HashesAlgorithm {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let str = match self {
HashesAlgorithm::SHA1 => "SHA1".to_string(),
HashesAlgorithm::SHA256 => "SHA256".to_string(),
HashesAlgorithm::SHA512 => "SHA512".to_string(),
}
};
write!(f, "{}", str)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/oath/totp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl OtpAuth for TOTPContext {
"otpauth://totp/{}?secret={}&algorithm={}&digits={}&period={}",
label.unwrap_or("slauth"),
base32::encode(base32::Alphabet::RFC4648 { padding: false }, self.secret.as_slice()),
self.alg.to_string(),
self.alg,
self.digits,
self.period
);
Expand Down
8 changes: 0 additions & 8 deletions src/u2f/proto/web_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub struct Registration {
pub attestation_cert: Vec<u8>,
}

///
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RegisterRequest {
Expand All @@ -43,7 +42,6 @@ pub struct RegisterRequest {
pub challenge: String,
}

///
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RegisteredKey {
Expand Down Expand Up @@ -97,7 +95,6 @@ impl<'a> From<&'a U2fRequestType> for U2fResponseType {
}
}

///
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct U2fRequest {
Expand All @@ -121,17 +118,14 @@ pub struct U2fRequest {
pub data: Request,
}

///
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct U2fRegisterRequest {
///
pub register_requests: Vec<RegisterRequest>,
/// An array of RegisteredKeys representing the U2F tokens registered to this user.
pub registered_keys: Vec<RegisteredKey>,
}

///
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct U2fSignRequest {
Expand All @@ -141,7 +135,6 @@ pub struct U2fSignRequest {
pub registered_keys: Vec<RegisteredKey>,
}

///
#[derive(Serialize, Deserialize)]
#[serde(untagged)]
pub enum Request {
Expand Down Expand Up @@ -234,7 +227,6 @@ pub struct U2fSignResponse {
pub client_data: String,
}

///
#[derive(Serialize, Deserialize)]
#[serde(untagged)]
pub enum Response {
Expand Down
1 change: 0 additions & 1 deletion src/u2f/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ impl Registration {
}

impl U2fSignResponse {
///
pub fn validate_signature(&self, public_key: &[u8]) -> Result<bool, Error> {
let U2fSignResponse {
signature_data,
Expand Down
15 changes: 8 additions & 7 deletions src/webauthn/authenticator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl WebauthnAuthenticator {
pub fn generate_attestation_object(
alg: CoseAlgorithmIdentifier,
aaguid: Uuid,
credential_id: &Vec<u8>,
credential_id: &[u8],
rp_id: &str,
attestation_flags: u8,
) -> Result<(AttestationObject, String, Vec<u8>), WebauthnCredentialRequestError> {
Expand Down Expand Up @@ -250,7 +250,7 @@ impl WebauthnAuthenticator {
let attested_credential_data = if attestation_flags & AttestationFlags::AttestedCredentialDataIncluded as u8 != 0 {
Some(AttestedCredentialData {
aaguid: aaguid.into_bytes(),
credential_id: credential_id.clone(),
credential_id: credential_id.to_owned(),
credential_public_key: CredentialPublicKey {
key_type: key_info.key_type(),
alg: alg.into(),
Expand Down Expand Up @@ -302,7 +302,8 @@ impl WebauthnAuthenticator {

let auth_data_bytes = Self::generate_authenticator_data(rp_id, attestation_flags, None)?.to_vec()?;

let challenge = base64::decode(credential_request_options.challenge)?;
let challenge = base64::decode(credential_request_options.challenge.as_str())
.or(base64::decode_config(credential_request_options.challenge, URL_SAFE_NO_PAD))?;
let collected_client_data = CollectedClientData {
request_type: WEBAUTHN_REQUEST_TYPE_GET.to_owned(),
challenge: base64::encode_config(challenge, URL_SAFE_NO_PAD),
Expand Down Expand Up @@ -374,7 +375,7 @@ impl WebauthnAuthenticator {
let signing_key = rsa::pkcs1v15::SigningKey::<Sha256>::new(key);
Ok(signing_key.sign([auth_data_bytes, client_data_hash].concat().as_slice()).to_vec())
}
_ => return Err(WebauthnCredentialRequestError::AlgorithmNotSupported),
_ => Err(WebauthnCredentialRequestError::AlgorithmNotSupported),
}
}

Expand Down Expand Up @@ -422,9 +423,9 @@ fn test_best_alg() {
assert_eq!(alg, CoseAlgorithmIdentifier::Ed25519);

let params2 = vec![
CoseAlgorithmIdentifier::ES256.into(),
CoseAlgorithmIdentifier::RS1.into(),
CoseAlgorithmIdentifier::RSA.into(),
CoseAlgorithmIdentifier::ES256,
CoseAlgorithmIdentifier::RS1,
CoseAlgorithmIdentifier::RSA,
];

let alg = WebauthnAuthenticator::find_best_supported_algorithm(&params2).unwrap();
Expand Down
Loading