c2pa_crypto/raw_signature/validator.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
// Copyright 2024 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.
// Unless required by applicable law or agreed to in writing,
// this software is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
// specific language governing permissions and limitations under
// each license.
use async_trait::async_trait;
use bcder::Oid;
use thiserror::Error;
use crate::raw_signature::SigningAlg;
/// A `RawSignatureValidator` 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 `RawSignatureValidator` does not implement COSE.
pub trait RawSignatureValidator {
/// Return `Ok(())` if the signature `sig` is valid for the raw content
/// `data` and the public key `public_key`.
fn validate(
&self,
sig: &[u8],
data: &[u8],
public_key: &[u8],
) -> 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 supported. For that reason, it's preferable to use this
/// implementation on WASM.
///
/// [`RawSignatureValidator`]: crate::raw_signature::RawSignatureValidator
#[async_trait(?Send)]
pub trait AsyncRawSignatureValidator {
/// Return `Ok(())` 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.
///
/// Which validators are available may vary depending on the platform and
/// which crate features were enabled.
pub fn validator_for_signing_alg(alg: SigningAlg) -> Option<Box<dyn RawSignatureValidator>> {
#[cfg(any(target_arch = "wasm32", feature = "rust_native_crypto"))]
{
if let Some(validator) =
crate::raw_signature::rust_native::validators::validator_for_signing_alg(alg)
{
return Some(validator);
}
}
#[cfg(not(target_arch = "wasm32"))]
if let Some(validator) =
crate::raw_signature::openssl::validators::validator_for_signing_alg(alg)
{
return Some(validator);
}
let _ = alg; // this value will be unused in this case
None
}
/// Return a built-in signature validator for the requested signature
/// algorithm as identified by OID.
///
/// Which validators are available may vary depending on the platform and
/// which crate features were enabled.
pub(crate) fn validator_for_sig_and_hash_algs(
sig_alg: &Oid,
hash_alg: &Oid,
) -> Option<Box<dyn RawSignatureValidator>> {
// TO REVIEW: Do we need any of the RSA-PSS algorithms for this use case?
#[cfg(any(target_arch = "wasm32", feature = "rust_native_crypto"))]
{
if let Some(validator) =
crate::raw_signature::rust_native::validators::validator_for_sig_and_hash_algs(
sig_alg, hash_alg,
)
{
return Some(validator);
}
}
#[cfg(not(target_arch = "wasm32"))]
if let Some(validator) =
crate::raw_signature::openssl::validators::validator_for_sig_and_hash_algs(
sig_alg, hash_alg,
)
{
return Some(validator);
}
let _ = sig_alg; // this value will be unused in this case
let _ = hash_alg; // this value will be unused in this case
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.
///
/// IMPORTANT: Only available on WASM builds. There are no built-in async
/// validators for other platforms.
#[cfg(target_arch = "wasm32")]
pub fn async_validator_for_signing_alg(
alg: SigningAlg,
) -> Option<Box<dyn AsyncRawSignatureValidator>> {
crate::raw_signature::rust_native::validators::async_validator_for_signing_alg(alg)
}
#[cfg(target_arch = "wasm32")]
pub(crate) fn async_validator_for_sig_and_hash_algs(
sig_alg: &Oid,
hash_alg: &Oid,
) -> Option<Box<dyn AsyncRawSignatureValidator>> {
crate::raw_signature::rust_native::validators::async_validator_for_sig_and_hash_algs(
sig_alg, hash_alg,
)
}
/// Describes errors that can be identified when validating a raw signature.
#[derive(Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum RawSignatureValidationError {
/// The signature does not match the provided data or public key.
#[error("the signature does not match the provided data or public key")]
SignatureMismatch,
/// An error was reported by the underlying cryptography implementation.
#[error("an error was reported by the cryptography library: {0}")]
CryptoLibraryError(String),
/// An invalid public key was provided.
#[error("invalid public key")]
InvalidPublicKey,
/// An invalid signature value was provided.
#[error("invalid signature value")]
InvalidSignature,
/// The time stamp uses an unsupported signing or hash algorithm.
#[error("signature uses an unsupported algorithm")]
UnsupportedAlgorithm,
/// An unexpected internal error occured while requesting the time stamp
/// response.
#[error("internal error ({0})")]
InternalError(String),
}
#[cfg(not(target_arch = "wasm32"))]
impl From<openssl::error::ErrorStack> for RawSignatureValidationError {
fn from(err: openssl::error::ErrorStack) -> Self {
Self::CryptoLibraryError(err.to_string())
}
}
#[cfg(not(target_arch = "wasm32"))]
impl From<crate::raw_signature::openssl::OpenSslMutexUnavailable> for RawSignatureValidationError {
fn from(err: crate::raw_signature::openssl::OpenSslMutexUnavailable) -> Self {
Self::InternalError(err.to_string())
}
}