c2pa_crypto/raw_signature/signer.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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
// Copyright 2022 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 thiserror::Error;
use crate::{
raw_signature::SigningAlg,
time_stamp::{AsyncTimeStampProvider, TimeStampError, TimeStampProvider},
};
/// Implementations of the `RawSigner` trait generate a cryptographic signature
/// over an arbitrary byte array.
///
/// If an implementation _can_ be asynchronous, that is preferred.
pub trait RawSigner: TimeStampProvider {
/// Return a raw signature over the original byte slice.
fn sign(&self, data: &[u8]) -> Result<Vec<u8>, RawSignerError>;
/// Return the algorithm implemented by this signer.
fn alg(&self) -> SigningAlg;
/// Return the signing certificate chain.
///
/// Each certificate should be encoded in DER format and sequenced from
/// end-entity certificate to the outermost certificate authority.
fn cert_chain(&self) -> Result<Vec<Vec<u8>>, RawSignerError>;
/// Return the size in bytes of the largest possible expected signature.
/// Signing will fail if the result of the [`sign`] function is larger
/// than this value.
///
/// [`sign`]: Self::sign
fn reserve_size(&self) -> usize;
/// Return an OCSP response for the signing certificate if available.
///
/// By pre-querying the value for the signing certificate, the value can be
/// cached which will reduce load on the certificate authority, as
/// recommended by the C2PA spec.
fn ocsp_response(&self) -> Option<Vec<u8>> {
None
}
}
/// Implementations of the `AsyncRawSigner` trait generate a cryptographic
/// signature over an arbitrary byte array.
///
/// Use this trait only when the implementation must be asynchronous.
#[cfg(not(target_arch = "wasm32"))]
#[async_trait]
pub trait AsyncRawSigner: AsyncTimeStampProvider + Sync {
/// Return a raw signature over the original byte slice.
async fn sign(&self, data: Vec<u8>) -> Result<Vec<u8>, RawSignerError>;
/// Return the algorithm implemented by this signer.
fn alg(&self) -> SigningAlg;
/// Return the signing certificate chain.
///
/// Each certificate should be encoded in DER format and sequenced from
/// end-entity certificate to the outermost certificate authority.
fn cert_chain(&self) -> Result<Vec<Vec<u8>>, RawSignerError>;
/// Return the size in bytes of the largest possible expected signature.
/// Signing will fail if the result of the [`sign`] function is larger
/// than this value.
///
/// [`sign`]: Self::sign
fn reserve_size(&self) -> usize;
/// Return an OCSP response for the signing certificate if available.
///
/// By pre-querying the value for the signing certificate, the value can be
/// cached which will reduce load on the certificate authority, as
/// recommended by the C2PA spec.
async fn ocsp_response(&self) -> Option<Vec<u8>> {
None
}
}
/// Implementations of the `AsyncRawSigner` trait generate a cryptographic
/// signature over an arbitrary byte array.
///
/// Use this trait only when the implementation must be asynchronous.
#[cfg(target_arch = "wasm32")]
#[async_trait(?Send)]
pub trait AsyncRawSigner: AsyncTimeStampProvider {
/// Return a raw signature over the original byte slice.
async fn sign(&self, data: Vec<u8>) -> Result<Vec<u8>, RawSignerError>;
/// Return the algorithm implemented by this signer.
fn alg(&self) -> SigningAlg;
/// Return the signing certificate chain.
///
/// Each certificate should be encoded in DER format and sequenced from
/// end-entity certificate to the outermost certificate authority.
fn cert_chain(&self) -> Result<Vec<Vec<u8>>, RawSignerError>;
/// Return the size in bytes of the largest possible expected signature.
/// Signing will fail if the result of the [`sign`] function is larger
/// than this value.
///
/// [`sign`]: Self::sign
fn reserve_size(&self) -> usize;
/// Return an OCSP response for the signing certificate if available.
///
/// By pre-querying the value for the signing certificate, the value can be
/// cached which will reduce load on the certificate authority, as
/// recommended by the C2PA spec.
async fn ocsp_response(&self) -> Option<Vec<u8>> {
None
}
}
/// Describes errors that can be identified when generating a raw signature.
#[derive(Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum RawSignerError {
/// The signing credentials are invalid.
#[error("invalid signing credentials ({0})")]
InvalidSigningCredentials(String),
/// An I/O error occurred. This typically happens when loading
/// public/private key material from files.
///
/// NOTE: We do not directly capture the I/O error itself because it
/// lacks an `Eq` implementation. Instead we capture the error description.
#[error("I/O error ({0})")]
IoError(String),
/// An error was reported by the underlying cryptography implementation.
#[error("an error was reported by the cryptography library: {0}")]
CryptoLibraryError(String),
/// An unexpected internal error occured while requesting the time stamp
/// response.
#[error("internal error ({0})")]
InternalError(String),
}
impl From<std::io::Error> for RawSignerError {
fn from(err: std::io::Error) -> Self {
Self::IoError(err.to_string())
}
}
#[cfg(not(target_arch = "wasm32"))]
impl From<openssl::error::ErrorStack> for RawSignerError {
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 RawSignerError {
fn from(err: crate::raw_signature::openssl::OpenSslMutexUnavailable) -> Self {
Self::InternalError(err.to_string())
}
}
/// Return a built-in [`RawSigner`] instance using the provided signing
/// certificate and private key.
///
/// Which signers are available may vary depending on the platform and which
/// crate features were enabled. If the desired signing algorithm is
/// unavailable, will respond with `Err(RawSignerError::InternalError)`.
///
/// May return an `Err` response if the certificate chain or private key are
/// invalid.
#[allow(unused)] // arguments may or may not be used depending on crate features
pub fn signer_from_cert_chain_and_private_key(
cert_chain: &[u8],
private_key: &[u8],
alg: SigningAlg,
time_stamp_service_url: Option<String>,
) -> Result<Box<dyn RawSigner + Send + Sync>, RawSignerError> {
#[cfg(any(target_arch = "wasm32", feature = "rust_native_crypto"))]
{
match crate::raw_signature::rust_native::signers::signer_from_cert_chain_and_private_key(
cert_chain,
private_key,
alg,
time_stamp_service_url.clone(),
) {
Ok(signer) => return Ok(signer),
Err(RawSignerError::InternalError(_)) => (),
Err(err) => return Err(err),
}
}
#[cfg(not(target_arch = "wasm32"))]
{
return crate::raw_signature::openssl::signers::signer_from_cert_chain_and_private_key(
cert_chain,
private_key,
alg,
time_stamp_service_url,
);
}
Err(RawSignerError::InternalError(format!(
"unsupported algorithm: {alg}"
)))
}
/// Return a built-in [`AsyncRawSigner`] instance using the provided signing
/// certificate and private key.
///
/// Which signers are available may vary depending on the platform and which
/// crate features were enabled. If the desired signing algorithm is
/// unavailable, will respond with `Err(RawSignerError::InternalError)`.
///
/// May return an `Err` response if the certificate chain or private key are
/// invalid.
pub fn async_signer_from_cert_chain_and_private_key(
cert_chain: &[u8],
private_key: &[u8],
alg: SigningAlg,
time_stamp_service_url: Option<String>,
) -> Result<Box<dyn AsyncRawSigner + Send + Sync>, RawSignerError> {
let sync_signer = signer_from_cert_chain_and_private_key(
cert_chain,
private_key,
alg,
time_stamp_service_url,
)?;
Ok(Box::new(AsyncRawSignerWrapper(sync_signer)))
}
struct AsyncRawSignerWrapper(Box<dyn RawSigner + Send + Sync>);
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl AsyncRawSigner for AsyncRawSignerWrapper {
async fn sign(&self, data: Vec<u8>) -> Result<Vec<u8>, RawSignerError> {
self.0.sign(&data)
}
fn alg(&self) -> SigningAlg {
self.0.alg()
}
fn cert_chain(&self) -> Result<Vec<Vec<u8>>, RawSignerError> {
self.0.cert_chain()
}
fn reserve_size(&self) -> usize {
self.0.reserve_size()
}
async fn ocsp_response(&self) -> Option<Vec<u8>> {
self.0.ocsp_response()
}
}
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl AsyncTimeStampProvider for AsyncRawSignerWrapper {
fn time_stamp_service_url(&self) -> Option<String> {
self.0.time_stamp_service_url()
}
fn time_stamp_request_headers(&self) -> Option<Vec<(String, String)>> {
self.0.time_stamp_request_headers()
}
fn time_stamp_request_body(&self, message: &[u8]) -> Result<Vec<u8>, TimeStampError> {
self.0.time_stamp_request_body(message)
}
async fn send_time_stamp_request(
&self,
message: &[u8],
) -> Option<Result<Vec<u8>, TimeStampError>> {
self.0.send_time_stamp_request(message)
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
pub(crate) fn test_signer(alg: SigningAlg) -> Box<dyn RawSigner> {
let (cert_chain, private_key) = match alg {
SigningAlg::Ed25519 => (
include_bytes!("../tests/fixtures/raw_signature/ed25519.pub").as_slice(),
include_bytes!("../tests/fixtures/raw_signature/ed25519.priv").as_slice(),
),
SigningAlg::Es256 => (
include_bytes!("../tests/fixtures/raw_signature/es256.pub").as_slice(),
include_bytes!("../tests/fixtures/raw_signature/es256.priv").as_slice(),
),
SigningAlg::Es384 => (
include_bytes!("../tests/fixtures/raw_signature/es384.pub").as_slice(),
include_bytes!("../tests/fixtures/raw_signature/es384.priv").as_slice(),
),
SigningAlg::Es512 => (
include_bytes!("../tests/fixtures/raw_signature/es512.pub").as_slice(),
include_bytes!("../tests/fixtures/raw_signature/es512.priv").as_slice(),
),
SigningAlg::Ps256 => (
include_bytes!("../tests/fixtures/raw_signature/ps256.pub").as_slice(),
include_bytes!("../tests/fixtures/raw_signature/ps256.priv").as_slice(),
),
SigningAlg::Ps384 => (
include_bytes!("../tests/fixtures/raw_signature/ps384.pub").as_slice(),
include_bytes!("../tests/fixtures/raw_signature/ps384.priv").as_slice(),
),
SigningAlg::Ps512 => (
include_bytes!("../tests/fixtures/raw_signature/ps512.pub").as_slice(),
include_bytes!("../tests/fixtures/raw_signature/ps512.priv").as_slice(),
),
};
signer_from_cert_chain_and_private_key(cert_chain, private_key, alg, None).unwrap()
}