Skip to content

Commit 3382819

Browse files
committed
cms: add callbacks to the SignerInfo builder
This is used to add countersignatures/timestamps to a SignerInfo. Countersignatures are attached as unsigned attributes after the signature is emitted (input to the countersignature is the signature itself).
2 parents d275549 + cfacd3e commit 3382819

File tree

1 file changed

+102
-4
lines changed

1 file changed

+102
-4
lines changed

cms/src/builder.rs

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use cipher::{
2525
use const_oid::ObjectIdentifier;
2626
use core::cmp::Ordering;
2727
use core::fmt;
28+
use core::future::ready;
2829
use core::marker::PhantomData;
2930
use der::asn1::{BitString, Null, OctetString, OctetStringRef, SetOfVec};
3031
use der::oid::db::DB;
@@ -410,9 +411,30 @@ impl<'s> SignedDataBuilder<'s> {
410411
S::VerifyingKey: EncodePublicKey,
411412
Signature: SignatureBitStringEncoding,
412413
{
413-
let signer_info = signer_info_builder
414+
self.add_signer_info_cb::<S, Signature, _>(signer_info_builder, signer, |_| Ok(()))
415+
}
416+
417+
/// Add a signer info. The signature will be calculated. Note that the encapsulated content
418+
/// must not be changed after the first signer info was added.
419+
pub fn add_signer_info_cb<S, Signature, CB>(
420+
&mut self,
421+
signer_info_builder: SignerInfoBuilder<'_>,
422+
signer: &S,
423+
callback: CB,
424+
) -> Result<&mut Self>
425+
where
426+
S: Keypair + DynSignatureAlgorithmIdentifier,
427+
S: Signer<Signature>,
428+
S::VerifyingKey: EncodePublicKey,
429+
Signature: SignatureBitStringEncoding,
430+
CB: FnOnce(&mut SignerInfo) -> Result<()>,
431+
{
432+
let mut signer_info = signer_info_builder
414433
.build::<S, Signature>(signer)
415434
.map_err(|_| der::Error::from(ErrorKind::Failed))?;
435+
436+
callback(&mut signer_info)?;
437+
416438
self.signer_infos.push(signer_info);
417439

418440
Ok(self)
@@ -433,9 +455,37 @@ impl<'s> SignedDataBuilder<'s> {
433455
Signature: SignatureBitStringEncoding,
434456
R: CryptoRng + ?Sized,
435457
{
436-
let signer_info = signer_info_builder
458+
self.add_signer_info_with_rng_cb::<S, Signature, _, R>(
459+
signer_info_builder,
460+
signer,
461+
|_| Ok(()),
462+
rng,
463+
)
464+
}
465+
466+
/// Add a signer info. The signature will be calculated. Note that the encapsulated content
467+
/// must not be changed after the first signer info was added.
468+
pub fn add_signer_info_with_rng_cb<S, Signature, CB, R>(
469+
&mut self,
470+
signer_info_builder: SignerInfoBuilder<'_>,
471+
signer: &S,
472+
callback: CB,
473+
rng: &mut R,
474+
) -> Result<&mut Self>
475+
where
476+
S: Keypair + DynSignatureAlgorithmIdentifier,
477+
S: RandomizedSigner<Signature>,
478+
S::VerifyingKey: EncodePublicKey,
479+
Signature: SignatureBitStringEncoding,
480+
CB: FnOnce(&mut SignerInfo) -> Result<()>,
481+
R: CryptoRng + ?Sized,
482+
{
483+
let mut signer_info = signer_info_builder
437484
.build_with_rng::<S, Signature, R>(signer, rng)
438485
.map_err(|_| der::Error::from(ErrorKind::Failed))?;
486+
487+
callback(&mut signer_info)?;
488+
439489
self.signer_infos.push(signer_info);
440490

441491
Ok(self)
@@ -454,10 +504,33 @@ impl<'s> SignedDataBuilder<'s> {
454504
S::VerifyingKey: EncodePublicKey,
455505
Signature: SignatureBitStringEncoding,
456506
{
457-
let signer_info = signer_info_builder
507+
self.add_signer_info_cb_async(signer_info_builder, signer, |_| ready(Ok(())))
508+
.await
509+
}
510+
511+
/// Add a signer info. The signature will be calculated. Note that the encapsulated content
512+
/// must not be changed after the first signer info was added.
513+
pub async fn add_signer_info_cb_async<S, Signature, F, CB>(
514+
&mut self,
515+
signer_info_builder: SignerInfoBuilder<'_>,
516+
signer: &S,
517+
callback: CB,
518+
) -> Result<&mut Self>
519+
where
520+
S: Keypair + DynSignatureAlgorithmIdentifier,
521+
S: AsyncSigner<Signature>,
522+
S::VerifyingKey: EncodePublicKey,
523+
Signature: SignatureBitStringEncoding,
524+
F: Future<Output = Result<()>>,
525+
CB: FnOnce(&mut SignerInfo) -> F,
526+
{
527+
let mut signer_info = signer_info_builder
458528
.build_async::<S, Signature>(signer)
459529
.await
460530
.map_err(|_| der::Error::from(ErrorKind::Failed))?;
531+
532+
callback(&mut signer_info).await?;
533+
461534
self.signer_infos.push(signer_info);
462535

463536
Ok(self)
@@ -478,10 +551,35 @@ impl<'s> SignedDataBuilder<'s> {
478551
Signature: SignatureBitStringEncoding,
479552
R: CryptoRng + ?Sized,
480553
{
481-
let signer_info = signer_info_builder
554+
self.add_signer_info_with_rng_cb_async(signer_info_builder, signer, rng, |_| ready(Ok(())))
555+
.await
556+
}
557+
558+
/// Add a signer info. The signature will be calculated. Note that the encapsulated content
559+
/// must not be changed after the first signer info was added.
560+
pub async fn add_signer_info_with_rng_cb_async<S, Signature, R, F, CB>(
561+
&mut self,
562+
signer_info_builder: SignerInfoBuilder<'_>,
563+
signer: &S,
564+
rng: &mut R,
565+
callback: CB,
566+
) -> Result<&mut Self>
567+
where
568+
S: Keypair + DynSignatureAlgorithmIdentifier,
569+
S: AsyncRandomizedSigner<Signature>,
570+
S::VerifyingKey: EncodePublicKey,
571+
Signature: SignatureBitStringEncoding,
572+
R: CryptoRng + ?Sized,
573+
F: Future<Output = Result<()>>,
574+
CB: FnOnce(&mut SignerInfo) -> F,
575+
{
576+
let mut signer_info = signer_info_builder
482577
.build_with_rng_async::<S, Signature, R>(signer, rng)
483578
.await
484579
.map_err(|_| der::Error::from(ErrorKind::Failed))?;
580+
581+
callback(&mut signer_info).await?;
582+
485583
self.signer_infos.push(signer_info);
486584

487585
Ok(self)

0 commit comments

Comments
 (0)