cawg_identity/builder/
identity_assertion_signer.rsuse std::cell::RefCell;
use c2pa::{DynamicAssertion, Result, Signer};
use c2pa_crypto::raw_signature::{RawSigner, SigningAlg};
use crate::builder::IdentityAssertionBuilder;
pub struct IdentityAssertionSigner {
signer: Box<dyn RawSigner>,
identity_assertions: RefCell<Vec<IdentityAssertionBuilder>>,
}
impl IdentityAssertionSigner {
pub fn new(signer: Box<dyn RawSigner>) -> Self {
Self {
signer,
identity_assertions: RefCell::new(vec![]),
}
}
#[cfg(test)]
pub(crate) fn from_test_credentials(alg: SigningAlg) -> Self {
use c2pa_crypto::raw_signature::signer_from_cert_chain_and_private_key;
use crate::tests::fixtures::cert_chain_and_private_key_for_alg;
let (cert_chain, private_key) = cert_chain_and_private_key_for_alg(alg);
#[allow(clippy::unwrap_used)]
Self {
signer: signer_from_cert_chain_and_private_key(&cert_chain, &private_key, alg, None)
.unwrap(),
identity_assertions: RefCell::new(vec![]),
}
}
pub fn add_identity_assertion(&mut self, iab: IdentityAssertionBuilder) {
#[allow(clippy::unwrap_used)]
let mut identity_assertions = self.identity_assertions.try_borrow_mut().unwrap();
identity_assertions.push(iab);
}
}
impl Signer for IdentityAssertionSigner {
fn sign(&self, data: &[u8]) -> Result<Vec<u8>> {
self.signer.sign(data).map_err(|e| e.into())
}
fn alg(&self) -> SigningAlg {
self.signer.alg()
}
fn certs(&self) -> Result<Vec<Vec<u8>>> {
self.signer.cert_chain().map_err(|e| e.into())
}
fn reserve_size(&self) -> usize {
self.signer.reserve_size()
}
fn ocsp_val(&self) -> Option<Vec<u8>> {
self.signer.ocsp_response()
}
fn time_authority_url(&self) -> Option<String> {
self.signer.time_stamp_service_url()
}
fn timestamp_request_headers(&self) -> Option<Vec<(String, String)>> {
self.signer.time_stamp_request_headers()
}
fn timestamp_request_body(&self, message: &[u8]) -> Result<Vec<u8>> {
self.signer
.time_stamp_request_body(message)
.map_err(|e| e.into())
}
fn send_timestamp_request(&self, message: &[u8]) -> Option<Result<Vec<u8>>> {
self.signer
.send_time_stamp_request(message)
.map(|r| r.map_err(|e| e.into()))
}
fn raw_signer(&self) -> Option<Box<&dyn RawSigner>> {
Some(Box::new(&*self.signer))
}
fn dynamic_assertions(&self) -> Vec<Box<dyn DynamicAssertion>> {
#[allow(clippy::unwrap_used)]
let mut identity_assertions = self.identity_assertions.try_borrow_mut().unwrap();
let ia_clone = identity_assertions.split_off(0);
let mut dynamic_assertions: Vec<Box<dyn DynamicAssertion>> = vec![];
for ia in ia_clone.into_iter() {
dynamic_assertions.push(Box::new(ia));
}
dynamic_assertions
}
}