-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
202 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,12 @@ | ||
use p256::{ | ||
ecdsa::{signature::Signer, Signature, SigningKey, VerifyingKey}, | ||
elliptic_curve::sec1::Coordinates, | ||
}; | ||
use rand_core::OsRng; | ||
use starknet::{core::types::FieldElement, macros::felt}; | ||
|
||
use crate::webauthn_signer::credential::{AuthenticatorData, CliendData}; | ||
|
||
use self::credential::AuthenticatorAssertionResponse; | ||
|
||
pub mod account; | ||
pub mod cairo_args; | ||
pub mod credential; | ||
pub mod signers; | ||
|
||
pub type U256 = (FieldElement, FieldElement); | ||
pub type Secp256r1Point = (U256, U256); | ||
|
||
// "Webauthn v1" | ||
pub const WEBAUTHN_SIGNATURE_TYPE: FieldElement = felt!("0x576562617574686e207631"); | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct P256r1Signer { | ||
pub signing_key: SigningKey, | ||
rp_id: String, | ||
} | ||
|
||
impl P256r1Signer { | ||
pub fn random(rp_id: String) -> Self { | ||
let signing_key = SigningKey::random(&mut OsRng); | ||
Self::new(signing_key, rp_id) | ||
} | ||
pub fn new(signing_key: SigningKey, rp_id: String) -> Self { | ||
Self { signing_key, rp_id } | ||
} | ||
pub fn public_key_bytes(&self) -> ([u8; 32], [u8; 32]) { | ||
let verifying_key: VerifyingKey = VerifyingKey::from(&self.signing_key); | ||
let encoded = &verifying_key.to_encoded_point(false); | ||
let (x, y) = match encoded.coordinates() { | ||
Coordinates::Uncompressed { x, y } => (x, y), | ||
_ => panic!("unexpected compression"), | ||
}; | ||
( | ||
x.as_slice().try_into().unwrap(), | ||
y.as_slice().try_into().unwrap(), | ||
) | ||
} | ||
pub fn sign(&self, challenge: &[u8]) -> AuthenticatorAssertionResponse { | ||
use sha2::{digest::Update, Digest, Sha256}; | ||
|
||
let authenticator_data = AuthenticatorData { | ||
rp_id_hash: [0; 32], | ||
flags: 0b00000101, | ||
sign_count: 0, | ||
}; | ||
let client_data_json = CliendData::new(challenge, self.rp_id.clone()).to_json(); | ||
let client_data_hash = Sha256::new().chain(client_data_json.clone()).finalize(); | ||
|
||
let mut to_sign = Into::<Vec<u8>>::into(authenticator_data.clone()); | ||
to_sign.append(&mut client_data_hash.to_vec()); | ||
let signature: Signature = self.signing_key.try_sign(&to_sign).unwrap(); | ||
let signature = signature.to_bytes().to_vec(); | ||
|
||
AuthenticatorAssertionResponse { | ||
authenticator_data, | ||
client_data_json, | ||
signature, | ||
user_handle: None, | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_signer() { | ||
let rp_id = "https://localhost:8080".to_string(); | ||
let signer = P256r1Signer::random(rp_id); | ||
let calldata = signer.sign("842903840923".as_bytes()); | ||
dbg!(&calldata); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use async_trait::async_trait; | ||
use coset::CoseKey; | ||
use futures::channel::oneshot; | ||
use wasm_bindgen_futures::spawn_local; | ||
use wasm_webauthn::*; | ||
|
||
use crate::webauthn_signer::credential::{self, AuthenticatorAssertionResponse, AuthenticatorData}; | ||
|
||
use super::Signer; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct DeviceSigner { | ||
rp_id: String, | ||
credential_id: Vec<u8>, | ||
pub_key: CoseKey, | ||
} | ||
|
||
impl DeviceSigner { | ||
pub fn new(rp_id: String, credential_id: Vec<u8>, pub_key: CoseKey) -> Self { | ||
Self { | ||
rp_id, | ||
credential_id, | ||
pub_key, | ||
} | ||
} | ||
} | ||
|
||
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] | ||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] | ||
impl Signer for DeviceSigner { | ||
async fn sign(&self, challenge: &[u8]) -> AuthenticatorAssertionResponse { | ||
let (sender, receiver) = oneshot::channel(); | ||
|
||
let mut credential = Credential::from(CredentialID(self.credential_id.clone())); | ||
credential.public_key = Some(self.pub_key.clone()); | ||
|
||
let rp_id = self.rp_id.to_owned(); | ||
let challenge = challenge.to_vec(); | ||
|
||
spawn_local(async move { | ||
let results = GetAssertionArgsBuilder::default() | ||
.rp_id(Some(rp_id)) | ||
.credentials(Some(vec![credential])) | ||
.challenge(challenge.to_vec()) | ||
.build() | ||
.expect("invalid args") | ||
.get_assertion() | ||
.await | ||
.expect("get assertion"); | ||
|
||
sender.send(results).expect("receiver dropped"); | ||
}); | ||
|
||
let GetAssertionResponse { | ||
signature, | ||
client_data_json, | ||
flags, | ||
counter, | ||
} = receiver.await.expect("receiver dropped"); | ||
|
||
AuthenticatorAssertionResponse { | ||
authenticator_data: AuthenticatorData { | ||
rp_id_hash: [0; 32], | ||
flags, | ||
sign_count: counter, | ||
}, | ||
client_data_json, | ||
signature, | ||
user_handle: None, | ||
} | ||
} | ||
|
||
fn public_key_bytes(&self) -> ([u8; 32], [u8; 32]) { | ||
unimplemented!("unimplemented public_key_bytes") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use super::credential::AuthenticatorAssertionResponse; | ||
use async_trait::async_trait; | ||
|
||
pub mod device; | ||
pub mod p256r1; | ||
|
||
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] | ||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] | ||
pub trait Signer { | ||
async fn sign(&self, challenge: &[u8]) -> AuthenticatorAssertionResponse; | ||
fn public_key_bytes(&self) -> ([u8; 32], [u8; 32]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use crate::webauthn_signer::credential::{AuthenticatorData, CliendData}; | ||
use async_trait::async_trait; | ||
use p256::{ | ||
ecdsa::{signature::Signer as P256Signer, Signature, SigningKey, VerifyingKey}, | ||
elliptic_curve::sec1::Coordinates, | ||
}; | ||
use rand_core::OsRng; | ||
|
||
use crate::webauthn_signer::credential::AuthenticatorAssertionResponse; | ||
|
||
use super::Signer; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct P256r1Signer { | ||
pub signing_key: SigningKey, | ||
rp_id: String, | ||
} | ||
|
||
impl P256r1Signer { | ||
pub fn new(rp_id: String, signing_key: SigningKey) -> Self { | ||
Self { rp_id, signing_key } | ||
} | ||
|
||
pub fn random(rp_id: String) -> Self { | ||
let signing_key = SigningKey::random(&mut OsRng); | ||
Self::new(rp_id, signing_key) | ||
} | ||
} | ||
|
||
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] | ||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] | ||
impl Signer for P256r1Signer { | ||
fn public_key_bytes(&self) -> ([u8; 32], [u8; 32]) { | ||
P256VerifyingKeyConverter::new(*self.signing_key.verifying_key()).to_bytes() | ||
} | ||
|
||
async fn sign(&self, challenge: &[u8]) -> AuthenticatorAssertionResponse { | ||
use sha2::{digest::Update, Digest, Sha256}; | ||
|
||
let authenticator_data = AuthenticatorData { | ||
rp_id_hash: [0; 32], | ||
flags: 0b00000101, | ||
sign_count: 0, | ||
}; | ||
let client_data_json = CliendData::new(challenge, self.rp_id.clone()).to_json(); | ||
let client_data_hash = Sha256::new().chain(client_data_json.clone()).finalize(); | ||
|
||
let mut to_sign = Into::<Vec<u8>>::into(authenticator_data.clone()); | ||
to_sign.append(&mut client_data_hash.to_vec()); | ||
let signature: Signature = self.signing_key.try_sign(&to_sign).unwrap(); | ||
let signature = signature.to_bytes().to_vec(); | ||
|
||
AuthenticatorAssertionResponse { | ||
authenticator_data, | ||
client_data_json, | ||
signature, | ||
user_handle: None, | ||
} | ||
} | ||
} | ||
|
||
pub struct P256VerifyingKeyConverter { | ||
pub verifying_key: VerifyingKey, | ||
} | ||
|
||
impl P256VerifyingKeyConverter { | ||
pub fn new(verifying_key: VerifyingKey) -> Self { | ||
Self { verifying_key } | ||
} | ||
pub fn to_bytes(&self) -> ([u8; 32], [u8; 32]) { | ||
let encoded = &self.verifying_key.to_encoded_point(false); | ||
let (x, y) = match encoded.coordinates() { | ||
Coordinates::Uncompressed { x, y } => (x, y), | ||
_ => panic!("unexpected compression"), | ||
}; | ||
( | ||
x.as_slice().try_into().unwrap(), | ||
y.as_slice().try_into().unwrap(), | ||
) | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_signer() { | ||
let rp_id = "https://localhost:8080".to_string(); | ||
let signer = P256r1Signer::random(rp_id); | ||
let calldata = signer.sign("842903840923".as_bytes()).await; | ||
dbg!(&calldata); | ||
} |