Skip to content

cms: add callbacks to the SignerInfo builder #1991

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
106 changes: 102 additions & 4 deletions cms/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use cipher::{
use const_oid::ObjectIdentifier;
use core::cmp::Ordering;
use core::fmt;
use core::future::ready;
use core::marker::PhantomData;
use der::asn1::{BitString, Null, OctetString, OctetStringRef, SetOfVec};
use der::oid::db::DB;
Expand Down Expand Up @@ -410,9 +411,30 @@ impl<'s> SignedDataBuilder<'s> {
S::VerifyingKey: EncodePublicKey,
Signature: SignatureBitStringEncoding,
{
let signer_info = signer_info_builder
self.add_signer_info_cb::<S, Signature, _>(signer_info_builder, signer, |_| Ok(()))
}

/// Add a signer info. The signature will be calculated. Note that the encapsulated content
/// must not be changed after the first signer info was added.
pub fn add_signer_info_cb<S, Signature, CB>(
&mut self,
signer_info_builder: SignerInfoBuilder<'_>,
signer: &S,
callback: CB,
) -> Result<&mut Self>
where
S: Keypair + DynSignatureAlgorithmIdentifier,
S: Signer<Signature>,
S::VerifyingKey: EncodePublicKey,
Signature: SignatureBitStringEncoding,
CB: FnOnce(&mut SignerInfo) -> Result<()>,
{
let mut signer_info = signer_info_builder
.build::<S, Signature>(signer)
.map_err(|_| der::Error::from(ErrorKind::Failed))?;

callback(&mut signer_info)?;

self.signer_infos.push(signer_info);

Ok(self)
Expand All @@ -433,9 +455,37 @@ impl<'s> SignedDataBuilder<'s> {
Signature: SignatureBitStringEncoding,
R: CryptoRng + ?Sized,
{
let signer_info = signer_info_builder
self.add_signer_info_with_rng_cb::<S, Signature, _, R>(
signer_info_builder,
signer,
|_| Ok(()),
rng,
)
}

/// Add a signer info. The signature will be calculated. Note that the encapsulated content
/// must not be changed after the first signer info was added.
pub fn add_signer_info_with_rng_cb<S, Signature, CB, R>(
&mut self,
signer_info_builder: SignerInfoBuilder<'_>,
signer: &S,
callback: CB,
rng: &mut R,
) -> Result<&mut Self>
where
S: Keypair + DynSignatureAlgorithmIdentifier,
S: RandomizedSigner<Signature>,
S::VerifyingKey: EncodePublicKey,
Signature: SignatureBitStringEncoding,
CB: FnOnce(&mut SignerInfo) -> Result<()>,
R: CryptoRng + ?Sized,
{
let mut signer_info = signer_info_builder
.build_with_rng::<S, Signature, R>(signer, rng)
.map_err(|_| der::Error::from(ErrorKind::Failed))?;

callback(&mut signer_info)?;

self.signer_infos.push(signer_info);

Ok(self)
Expand All @@ -454,10 +504,33 @@ impl<'s> SignedDataBuilder<'s> {
S::VerifyingKey: EncodePublicKey,
Signature: SignatureBitStringEncoding,
{
let signer_info = signer_info_builder
self.add_signer_info_cb_async(signer_info_builder, signer, |_| ready(Ok(())))
.await
}

/// Add a signer info. The signature will be calculated. Note that the encapsulated content
/// must not be changed after the first signer info was added.
pub async fn add_signer_info_cb_async<S, Signature, F, CB>(
&mut self,
signer_info_builder: SignerInfoBuilder<'_>,
signer: &S,
callback: CB,
) -> Result<&mut Self>
where
S: Keypair + DynSignatureAlgorithmIdentifier,
S: AsyncSigner<Signature>,
S::VerifyingKey: EncodePublicKey,
Signature: SignatureBitStringEncoding,
F: Future<Output = Result<()>>,
CB: FnOnce(&mut SignerInfo) -> F,
{
let mut signer_info = signer_info_builder
.build_async::<S, Signature>(signer)
.await
.map_err(|_| der::Error::from(ErrorKind::Failed))?;

callback(&mut signer_info).await?;

self.signer_infos.push(signer_info);

Ok(self)
Expand All @@ -478,10 +551,35 @@ impl<'s> SignedDataBuilder<'s> {
Signature: SignatureBitStringEncoding,
R: CryptoRng + ?Sized,
{
let signer_info = signer_info_builder
self.add_signer_info_with_rng_cb_async(signer_info_builder, signer, rng, |_| ready(Ok(())))
.await
}

/// Add a signer info. The signature will be calculated. Note that the encapsulated content
/// must not be changed after the first signer info was added.
pub async fn add_signer_info_with_rng_cb_async<S, Signature, R, F, CB>(
&mut self,
signer_info_builder: SignerInfoBuilder<'_>,
signer: &S,
rng: &mut R,
callback: CB,
) -> Result<&mut Self>
where
S: Keypair + DynSignatureAlgorithmIdentifier,
S: AsyncRandomizedSigner<Signature>,
S::VerifyingKey: EncodePublicKey,
Signature: SignatureBitStringEncoding,
R: CryptoRng + ?Sized,
F: Future<Output = Result<()>>,
CB: FnOnce(&mut SignerInfo) -> F,
{
let mut signer_info = signer_info_builder
.build_with_rng_async::<S, Signature, R>(signer, rng)
.await
.map_err(|_| der::Error::from(ErrorKind::Failed))?;

callback(&mut signer_info).await?;

self.signer_infos.push(signer_info);

Ok(self)
Expand Down