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

feat: Make AsyncRawSignatureValidator available on all platforms #800

Merged
merged 2 commits into from
Dec 24, 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
4 changes: 2 additions & 2 deletions internal/crypto/src/raw_signature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ pub(crate) mod oids;

mod validator;
pub use validator::{
validator_for_sig_and_hash_algs, validator_for_signing_alg, RawSignatureValidationError,
RawSignatureValidator,
async_validator_for_signing_alg, validator_for_sig_and_hash_algs, validator_for_signing_alg,
AsyncRawSignatureValidator, RawSignatureValidationError, RawSignatureValidator,
};
58 changes: 58 additions & 0 deletions internal/crypto/src/raw_signature/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// specific language governing permissions and limitations under
// each license.

use async_trait::async_trait;
use bcder::Oid;
use thiserror::Error;

Expand All @@ -34,6 +35,31 @@
) -> Result<(), RawSignatureValidationError>;
}

/// An `AsyncRawSignatureValidator` implementation checks a signature encoded
/// using a specific signature algorithm and a private/public key pair.
///
/// IMPORTANT: This signature is typically embedded in a wrapper provided by
/// another signature mechanism. In the C2PA ecosystem, this wrapper is
/// typically COSE, but `AsyncRawSignatureValidator` does not implement COSE.
///
/// The WASM implementation of `c2pa-crypto` also implements
/// [`RawSignatureValidator`] (the synchronous version), but some encryption
/// algorithms are not fully supported. When possible, it's preferable to use
/// this implementation.
///
/// [`RawSignatureValidator`]: crate::raw_signature::RawSignatureValidator
#[async_trait(?Send)]
pub trait AsyncRawSignatureValidator {
/// Return `true` if the signature `sig` is valid for the raw content `data`
/// and the public key `public_key`.
async fn validate_async(
&self,
sig: &[u8],
data: &[u8],
public_key: &[u8],
) -> Result<(), RawSignatureValidationError>;
}

/// Return a built-in signature validator for the requested signature
/// algorithm.
///
Expand All @@ -54,6 +80,24 @@
None
}

/// Return a built-in signature validator for the requested signature
/// algorithm.
///
/// Which validators are available may vary depending on the platform and
/// which crate features were enabled.
pub fn async_validator_for_signing_alg(
alg: SigningAlg,
) -> Option<Box<dyn AsyncRawSignatureValidator>> {
#[cfg(target_arch = "wasm32")]
if let Some(validator) = crate::webcrypto::async_validator_for_signing_alg(alg) {
return Some(validator);
}

Some(Box::new(AsyncValidatorAdapter(validator_for_signing_alg(
alg,
)?)))
}

Check warning on line 99 in internal/crypto/src/raw_signature/validator.rs

View check run for this annotation

Codecov / codecov/patch

internal/crypto/src/raw_signature/validator.rs#L88-L99

Added lines #L88 - L99 were not covered by tests

/// Return a built-in signature validator for the requested signature
/// algorithm as identified by OID.
///
Expand Down Expand Up @@ -165,3 +209,17 @@
}
}
}

struct AsyncValidatorAdapter(Box<dyn RawSignatureValidator>);

#[async_trait(?Send)]
impl AsyncRawSignatureValidator for AsyncValidatorAdapter {
async fn validate_async(
&self,
sig: &[u8],
data: &[u8],
public_key: &[u8],
) -> Result<(), RawSignatureValidationError> {
self.0.validate(sig, data, public_key)
}

Check warning on line 224 in internal/crypto/src/raw_signature/validator.rs

View check run for this annotation

Codecov / codecov/patch

internal/crypto/src/raw_signature/validator.rs#L222-L224

Added lines #L222 - L224 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use wasm_bindgen_futures::JsFuture;
use web_sys::CryptoKey;

use crate::{
raw_signature::RawSignatureValidationError,
webcrypto::{AsyncRawSignatureValidator, WindowOrWorker},
raw_signature::{AsyncRawSignatureValidator, RawSignatureValidationError},
webcrypto::WindowOrWorker,
};

/// An `EcdsaValidator` can validate raw signatures with one of the ECDSA
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@

use async_trait::async_trait;

use crate::{
raw_signature::{RawSignatureValidationError, RawSignatureValidator},
webcrypto::AsyncRawSignatureValidator,
use crate::raw_signature::{
AsyncRawSignatureValidator, RawSignatureValidationError, RawSignatureValidator,
};

/// An `Ed25519Validator` can validate raw signatures with the Ed25519 signature
Expand Down
28 changes: 1 addition & 27 deletions internal/crypto/src/webcrypto/async_validators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,13 @@
// specific language governing permissions and limitations under
// each license.

use async_trait::async_trait;
use bcder::Oid;

use crate::{
raw_signature::{oids::*, RawSignatureValidationError},
raw_signature::{oids::*, AsyncRawSignatureValidator},
SigningAlg,
};

/// An `AsyncRawSignatureValidator` implementation checks a signature encoded
/// using a specific signature algorithm and a private/public key pair.
///
/// IMPORTANT: This signature is typically embedded in a wrapper provided by
/// another signature mechanism. In the C2PA ecosystem, this wrapper is
/// typically COSE, but `AsyncRawSignatureValidator` does not implement COSE.
///
/// The WASM implementation of `c2pa-crypto` also implements
/// [`RawSignatureValidator`] (the synchronous version), but some encryption
/// algorithms are not fully supported. When possible, it's preferable to use
/// this implementation.
///
/// [`RawSignatureValidator`]: crate::raw_signature::RawSignatureValidator
#[async_trait(?Send)]
pub trait AsyncRawSignatureValidator {
/// Return `true` if the signature `sig` is valid for the raw content `data`
/// and the public key `public_key`.
async fn validate_async(
&self,
sig: &[u8],
data: &[u8],
public_key: &[u8],
) -> Result<(), RawSignatureValidationError>;
}

/// Return an async validator for the given signing algorithm.
pub fn async_validator_for_signing_alg(
alg: SigningAlg,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@

use async_trait::async_trait;

use crate::{
raw_signature::{RawSignatureValidationError, RawSignatureValidator},
webcrypto::AsyncRawSignatureValidator,
use crate::raw_signature::{
AsyncRawSignatureValidator, RawSignatureValidationError, RawSignatureValidator,
};

/// An `RsaLegacyValidator` can validate raw signatures with an RSA signature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@

use async_trait::async_trait;

use crate::{
raw_signature::{RawSignatureValidationError, RawSignatureValidator},
webcrypto::AsyncRawSignatureValidator,
use crate::raw_signature::{
AsyncRawSignatureValidator, RawSignatureValidationError, RawSignatureValidator,
};

/// An `RsaValidator` can validate raw signatures with one of the RSA-PSS
Expand Down
1 change: 0 additions & 1 deletion internal/crypto/src/webcrypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
pub(crate) mod async_validators;
pub use async_validators::{
async_validator_for_sig_and_hash_algs, async_validator_for_signing_alg,
AsyncRawSignatureValidator,
};

pub(crate) mod check_certificate_trust;
Expand Down
Loading