-
Notifications
You must be signed in to change notification settings - Fork 3
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
25 changed files
with
798 additions
and
442 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use anyhow::Result; | ||
#[cfg(feature = "p256")] | ||
use anyhow::{bail, Error}; | ||
#[cfg(feature = "p256")] | ||
use p256::ecdsa::signature::Verifier as _; | ||
use x509_cert::spki::SubjectPublicKeyInfoRef; | ||
|
||
pub trait Verifier: Sized { | ||
/// Construct a [Verifier] from [SubjectPublicKeyInfoRef]. | ||
/// | ||
/// ## Params | ||
/// * `spki` - the public key information necessary to construct a [Verifier]. | ||
/// * `algorithm` - the value taken from the `alg` header of the request, to hint at what curve should be used by the [Verifier]. | ||
fn from_spki(spki: SubjectPublicKeyInfoRef<'_>, algorithm: String) -> Result<Self>; | ||
fn verify(&self, payload: &[u8], signature: &[u8]) -> Result<()>; | ||
} | ||
|
||
#[cfg(feature = "p256")] | ||
#[derive(Debug, Clone)] | ||
pub struct P256Verifier(p256::ecdsa::VerifyingKey); | ||
|
||
#[cfg(feature = "p256")] | ||
impl Verifier for P256Verifier { | ||
fn from_spki(spki: SubjectPublicKeyInfoRef<'_>, algorithm: String) -> Result<Self> { | ||
if algorithm != "ES256" { | ||
bail!("P256Verifier cannot verify requests signed with '{algorithm}'") | ||
} | ||
spki.try_into().map(Self).map_err(Error::from) | ||
} | ||
|
||
fn verify(&self, payload: &[u8], signature: &[u8]) -> Result<()> { | ||
let signature = p256::ecdsa::Signature::from_slice(signature)?; | ||
self.0.verify(payload, &signature).map_err(Error::from) | ||
} | ||
} |
Oops, something went wrong.