add to simple wisp client

This commit is contained in:
Toshit Chawda 2024-09-14 18:14:10 -07:00
parent 577ce71b89
commit 24ccd8d393
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
7 changed files with 82 additions and 9 deletions

View file

@ -74,7 +74,10 @@ pub struct VerifyKey {
impl VerifyKey {
/// Create a new ED25519 verification key.
pub fn new_ed25519(verifier: Arc<dyn Verifier<Signature> + Sync + Send>, hash: [u8; 64]) -> Self {
pub fn new_ed25519(
verifier: Arc<dyn Verifier<Signature> + Sync + Send>,
hash: [u8; 64],
) -> Self {
Self {
cert_type: SupportedCertificateTypes::Ed25519,
hash,
@ -314,9 +317,9 @@ impl ProtocolExtensionBuilder for CertAuthProtocolExtensionBuilder {
fn build_to_extension(&mut self, _: Role) -> Result<AnyProtocolExtension, WispError> {
match self {
Self::ServerBeforeChallenge { verifiers } => {
let mut challenge = BytesMut::with_capacity(64);
let mut challenge = [0u8; 64];
getrandom::getrandom(&mut challenge).map_err(CertAuthError::from)?;
let challenge = challenge.freeze();
let challenge = Bytes::from(challenge.to_vec());
*self = Self::ServerAfterChallenge {
verifiers: verifiers.to_vec(),

View file

@ -32,6 +32,16 @@ impl AnyProtocolExtension {
pub fn downcast<T: ProtocolExtension>(self) -> Result<Box<T>, Self> {
self.0.__downcast().map_err(Self)
}
/// Downcast the protocol extension.
pub fn downcast_ref<T: ProtocolExtension>(&self) -> Option<&T> {
self.0.__downcast_ref()
}
/// Downcast the protocol extension.
pub fn downcast_mut<T: ProtocolExtension>(&mut self) -> Option<&mut T> {
self.0.__downcast_mut()
}
}
impl Deref for AnyProtocolExtension {
@ -126,6 +136,22 @@ impl dyn ProtocolExtension {
Err(self)
}
}
fn __downcast_ref<T: ProtocolExtension>(&self) -> Option<&T> {
if self.__is::<T>() {
unsafe { Some(&*(self as *const dyn ProtocolExtension as *const T)) }
} else {
None
}
}
fn __downcast_mut<T: ProtocolExtension>(&mut self) -> Option<&mut T> {
if self.__is::<T>() {
unsafe { Some(&mut *(self as *mut dyn ProtocolExtension as *mut T)) }
} else {
None
}
}
}
/// Trait to build a Wisp protocol extension from a payload.