CmsSignedGenerator
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.Rosstandart;
using Org.BouncyCastle.Asn1.TeleTrust;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.Collections;
using Org.BouncyCastle.X509;
using System;
using System.Collections.Generic;
namespace Org.BouncyCastle.Cms
{
public abstract class CmsSignedGenerator
{
public static readonly string Data = CmsObjectIdentifiers.Data.Id;
public static readonly string DigestSha1 = OiwObjectIdentifiers.IdSha1.Id;
public static readonly string DigestSha224 = NistObjectIdentifiers.IdSha224.Id;
public static readonly string DigestSha256 = NistObjectIdentifiers.IdSha256.Id;
public static readonly string DigestSha384 = NistObjectIdentifiers.IdSha384.Id;
public static readonly string DigestSha512 = NistObjectIdentifiers.IdSha512.Id;
public static readonly string DigestSha512_224 = NistObjectIdentifiers.IdSha512_224.Id;
public static readonly string DigestSha512_256 = NistObjectIdentifiers.IdSha512_256.Id;
public static readonly string DigestMD5 = PkcsObjectIdentifiers.MD5.Id;
public static readonly string DigestGost3411 = CryptoProObjectIdentifiers.GostR3411.Id;
public static readonly string DigestRipeMD128 = TeleTrusTObjectIdentifiers.RipeMD128.Id;
public static readonly string DigestRipeMD160 = TeleTrusTObjectIdentifiers.RipeMD160.Id;
public static readonly string DigestRipeMD256 = TeleTrusTObjectIdentifiers.RipeMD256.Id;
public static readonly string EncryptionRsa = PkcsObjectIdentifiers.RsaEncryption.Id;
public static readonly string EncryptionDsa = X9ObjectIdentifiers.IdDsaWithSha1.Id;
public static readonly string EncryptionECDsa = X9ObjectIdentifiers.ECDsaWithSha1.Id;
public static readonly string EncryptionRsaPss = PkcsObjectIdentifiers.IdRsassaPss.Id;
public static readonly string EncryptionGost3410 = CryptoProObjectIdentifiers.GostR3410x94.Id;
public static readonly string EncryptionECGost3410 = CryptoProObjectIdentifiers.GostR3410x2001.Id;
public static readonly string EncryptionECGost3410_2012_256 = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256.Id;
public static readonly string EncryptionECGost3410_2012_512 = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512.Id;
internal List<Asn1Encodable> _certs = new List<Asn1Encodable>();
internal List<Asn1Encodable> _crls = new List<Asn1Encodable>();
internal IList<SignerInformation> _signers = new List<SignerInformation>();
internal IDictionary<DerObjectIdentifier, byte[]> m_digests = new Dictionary<DerObjectIdentifier, byte[]>();
internal bool _useDerForCerts;
internal bool _useDerForCrls;
protected readonly SecureRandom m_random;
public bool UseDerForCerts {
get {
return _useDerForCerts;
}
set {
_useDerForCerts = value;
}
}
public bool UseDerForCrls {
get {
return _useDerForCrls;
}
set {
_useDerForCrls = value;
}
}
protected CmsSignedGenerator()
: this(CryptoServicesRegistrar.GetSecureRandom())
{
}
protected CmsSignedGenerator(SecureRandom random)
{
if (random == null)
throw new ArgumentNullException("random");
m_random = random;
}
protected internal virtual IDictionary<CmsAttributeTableParameter, object> GetBaseParameters(DerObjectIdentifier contentType, AlgorithmIdentifier digAlgId, byte[] hash)
{
Dictionary<CmsAttributeTableParameter, object> dictionary = new Dictionary<CmsAttributeTableParameter, object>();
if (contentType != null)
dictionary[CmsAttributeTableParameter.ContentType] = contentType;
dictionary[CmsAttributeTableParameter.DigestAlgorithmIdentifier] = digAlgId;
dictionary[CmsAttributeTableParameter.Digest] = hash.Clone();
return dictionary;
}
protected internal virtual Asn1Set GetAttributeSet(Org.BouncyCastle.Asn1.Cms.AttributeTable attr)
{
if (attr != null)
return DerSet.FromVector(attr.ToAsn1EncodableVector());
return null;
}
public void AddAttributeCertificate(X509V2AttributeCertificate attrCert)
{
CmsUtilities.CollectAttributeCertificate(_certs, attrCert);
}
public void AddAttributeCertificates(IStore<X509V2AttributeCertificate> attrCertStore)
{
CmsUtilities.CollectAttributeCertificates(_certs, attrCertStore);
}
public void AddCertificate(X509Certificate cert)
{
CmsUtilities.CollectCertificate(_certs, cert);
}
public void AddCertificates(IStore<X509Certificate> certStore)
{
CmsUtilities.CollectCertificates(_certs, certStore);
}
public void AddCrl(X509Crl crl)
{
CmsUtilities.CollectCrl(_crls, crl);
}
public void AddCrls(IStore<X509Crl> crlStore)
{
CmsUtilities.CollectCrls(_crls, crlStore);
}
public void AddOtherRevocationInfo(OtherRevocationInfoFormat otherRevocationInfo)
{
CmsUtilities.CollectOtherRevocationInfo(_crls, otherRevocationInfo);
}
public void AddOtherRevocationInfo(DerObjectIdentifier otherRevInfoFormat, Asn1Encodable otherRevInfo)
{
CmsUtilities.CollectOtherRevocationInfo(_crls, otherRevInfoFormat, otherRevInfo);
}
public void AddOtherRevocationInfos(IStore<OtherRevocationInfoFormat> otherRevocationInfoStore)
{
CmsUtilities.CollectOtherRevocationInfos(_crls, otherRevocationInfoStore);
}
public void AddOtherRevocationInfos(DerObjectIdentifier otherRevInfoFormat, IStore<Asn1Encodable> otherRevInfoStore)
{
CmsUtilities.CollectOtherRevocationInfos(_crls, otherRevInfoFormat, otherRevInfoStore);
}
public void AddSigners(SignerInformationStore signerStore)
{
foreach (SignerInformation signer in signerStore.GetSigners()) {
_signers.Add(signer);
AddSignerCallback(signer);
}
}
public IDictionary<string, byte[]> GetGeneratedDigests()
{
Dictionary<string, byte[]> dictionary = new Dictionary<string, byte[]>(StringComparer.OrdinalIgnoreCase);
foreach (KeyValuePair<DerObjectIdentifier, byte[]> digest in m_digests) {
dictionary.Add(digest.Key.GetID(), digest.Value);
}
return dictionary;
}
internal virtual void AddSignerCallback(SignerInformation si)
{
}
internal static SignerIdentifier GetSignerIdentifier(X509Certificate cert)
{
return new SignerIdentifier(CmsUtilities.GetIssuerAndSerialNumber(cert));
}
internal static SignerIdentifier GetSignerIdentifier(byte[] subjectKeyIdentifier)
{
return new SignerIdentifier(new DerOctetString(subjectKeyIdentifier));
}
}
}