<PackageReference Include="BouncyCastle.Cryptography" Version="2.5.1" />

CmsUtilities

static class CmsUtilities
using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Cms; using Org.BouncyCastle.Asn1.CryptoPro; using Org.BouncyCastle.Asn1.Ocsp; using Org.BouncyCastle.Asn1.Rosstandart; using Org.BouncyCastle.Asn1.Sec; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Operators.Utilities; using Org.BouncyCastle.Utilities.Collections; using Org.BouncyCastle.Utilities.IO; using Org.BouncyCastle.X509; using System; using System.Collections.Generic; using System.IO; namespace Org.BouncyCastle.Cms { internal static class CmsUtilities { private static readonly HashSet<DerObjectIdentifier> ECAlgorithms; private static readonly HashSet<DerObjectIdentifier> GostAlgorithms; private static readonly HashSet<DerObjectIdentifier> MqvAlgorithms; internal static int MaximumMemory { get { long num = 2147483647; if (num > 2147483647) return 2147483647; return (int)num; } } static CmsUtilities() { ECAlgorithms = new HashSet<DerObjectIdentifier>(); GostAlgorithms = new HashSet<DerObjectIdentifier>(); MqvAlgorithms = new HashSet<DerObjectIdentifier>(); ECAlgorithms.Add(X9ObjectIdentifiers.DHSinglePassStdDHSha1KdfScheme); ECAlgorithms.Add(SecObjectIdentifiers.dhSinglePass_stdDH_sha224kdf_scheme); ECAlgorithms.Add(SecObjectIdentifiers.dhSinglePass_stdDH_sha256kdf_scheme); ECAlgorithms.Add(SecObjectIdentifiers.dhSinglePass_stdDH_sha384kdf_scheme); ECAlgorithms.Add(SecObjectIdentifiers.dhSinglePass_stdDH_sha512kdf_scheme); ECAlgorithms.Add(X9ObjectIdentifiers.DHSinglePassCofactorDHSha1KdfScheme); ECAlgorithms.Add(SecObjectIdentifiers.dhSinglePass_cofactorDH_sha224kdf_scheme); ECAlgorithms.Add(SecObjectIdentifiers.dhSinglePass_cofactorDH_sha256kdf_scheme); ECAlgorithms.Add(SecObjectIdentifiers.dhSinglePass_cofactorDH_sha384kdf_scheme); ECAlgorithms.Add(SecObjectIdentifiers.dhSinglePass_cofactorDH_sha512kdf_scheme); GostAlgorithms.Add(CryptoProObjectIdentifiers.GostR3410x2001CryptoProESDH); GostAlgorithms.Add(RosstandartObjectIdentifiers.id_tc26_agreement_gost_3410_12_256); GostAlgorithms.Add(RosstandartObjectIdentifiers.id_tc26_agreement_gost_3410_12_512); MqvAlgorithms.Add(X9ObjectIdentifiers.MqvSinglePassSha1KdfScheme); MqvAlgorithms.Add(SecObjectIdentifiers.mqvSinglePass_sha224kdf_scheme); MqvAlgorithms.Add(SecObjectIdentifiers.mqvSinglePass_sha256kdf_scheme); MqvAlgorithms.Add(SecObjectIdentifiers.mqvSinglePass_sha384kdf_scheme); MqvAlgorithms.Add(SecObjectIdentifiers.mqvSinglePass_sha512kdf_scheme); } internal static bool IsEC(DerObjectIdentifier oid) { return ECAlgorithms.Contains(oid); } internal static bool IsGost(DerObjectIdentifier oid) { return GostAlgorithms.Contains(oid); } internal static bool IsMqv(DerObjectIdentifier oid) { return MqvAlgorithms.Contains(oid); } internal static ContentInfo ReadContentInfo(byte[] input) { using (Asn1InputStream asn1In = new Asn1InputStream(input)) return ReadContentInfo(asn1In); } internal static ContentInfo ReadContentInfo(Stream input) { using (Asn1InputStream asn1In = new Asn1InputStream(input, MaximumMemory, true)) return ReadContentInfo(asn1In); } private static ContentInfo ReadContentInfo(Asn1InputStream asn1In) { try { return ContentInfo.GetInstance(asn1In.ReadObject()); } catch (IOException innerException) { throw new CmsException("IOException reading content.", innerException); } catch (InvalidCastException innerException2) { throw new CmsException("Malformed content.", innerException2); } catch (ArgumentException innerException3) { throw new CmsException("Malformed content.", innerException3); } } internal static byte[] StreamToByteArray(Stream inStream) { return Streams.ReadAll(inStream); } internal static byte[] StreamToByteArray(Stream inStream, int limit) { return Streams.ReadAllLimited(inStream, limit); } internal static void AddDigestAlgs(Asn1EncodableVector digestAlgs, SignerInformation signer, IDigestAlgorithmFinder digestAlgorithmFinder) { digestAlgs.Add(CmsSignedHelper.FixDigestAlgID(signer.DigestAlgorithmID, digestAlgorithmFinder)); foreach (SignerInformation counterSignature in signer.GetCounterSignatures()) { digestAlgs.Add(CmsSignedHelper.FixDigestAlgID(counterSignature.DigestAlgorithmID, digestAlgorithmFinder)); } } internal static void AddDigestAlgs(ISet<AlgorithmIdentifier> digestAlgs, SignerInformation signer, IDigestAlgorithmFinder digestAlgorithmFinder) { digestAlgs.Add(CmsSignedHelper.FixDigestAlgID(signer.DigestAlgorithmID, digestAlgorithmFinder)); foreach (SignerInformation counterSignature in signer.GetCounterSignatures()) { digestAlgs.Add(CmsSignedHelper.FixDigestAlgID(counterSignature.DigestAlgorithmID, digestAlgorithmFinder)); } } internal static Asn1Set ConvertToDLSet(ISet<AlgorithmIdentifier> digestAlgs) { Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(digestAlgs.Count); foreach (AlgorithmIdentifier digestAlg in digestAlgs) { asn1EncodableVector.Add(digestAlg); } return DLSet.FromVector(asn1EncodableVector); } internal static Asn1Set CreateBerSetFromList(IEnumerable<Asn1Encodable> elements) { Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(); foreach (Asn1Encodable element in elements) { asn1EncodableVector.Add(element); } return BerSet.FromVector(asn1EncodableVector); } internal static Asn1Set CreateDerSetFromList(IEnumerable<Asn1Encodable> elements) { Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(); foreach (Asn1Encodable element in elements) { asn1EncodableVector.Add(element); } return DerSet.FromVector(asn1EncodableVector); } internal static IssuerAndSerialNumber GetIssuerAndSerialNumber(X509Certificate cert) { TbsCertificateStructure tbsCertificate = cert.TbsCertificate; return new IssuerAndSerialNumber(tbsCertificate.Issuer, tbsCertificate.SerialNumber); } internal static Org.BouncyCastle.Asn1.Cms.AttributeTable ParseAttributeTable(Asn1SetParser parser) { Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(); IAsn1Convertible asn1Convertible; while ((asn1Convertible = parser.ReadObject()) != null) { Asn1SequenceParser asn1SequenceParser = (Asn1SequenceParser)asn1Convertible; asn1EncodableVector.Add(asn1SequenceParser.ToAsn1Object()); } return new Org.BouncyCastle.Asn1.Cms.AttributeTable(DerSet.FromVector(asn1EncodableVector)); } internal static void CollectAttributeCertificate(List<Asn1Encodable> result, X509V2AttributeCertificate attrCert) { result.Add(new DerTaggedObject(false, 2, attrCert.AttributeCertificate)); } internal static void CollectAttributeCertificates(List<Asn1Encodable> result, IStore<X509V2AttributeCertificate> attrCertStore) { if (attrCertStore != null) { foreach (X509V2AttributeCertificate item in attrCertStore.EnumerateMatches(null)) { CollectAttributeCertificate(result, item); } } } internal static void CollectCertificate(List<Asn1Encodable> result, X509Certificate cert) { result.Add(cert.CertificateStructure); } internal static void CollectCertificates(List<Asn1Encodable> result, IStore<X509Certificate> certStore) { if (certStore != null) { foreach (X509Certificate item in certStore.EnumerateMatches(null)) { CollectCertificate(result, item); } } } internal static void CollectCrl(List<Asn1Encodable> result, X509Crl crl) { result.Add(crl.CertificateList); } internal static void CollectCrls(List<Asn1Encodable> result, IStore<X509Crl> crlStore) { if (crlStore != null) { foreach (X509Crl item in crlStore.EnumerateMatches(null)) { CollectCrl(result, item); } } } internal static void CollectOtherRevocationInfo(List<Asn1Encodable> result, OtherRevocationInfoFormat otherRevocationInfo) { ValidateOtherRevocationInfo(otherRevocationInfo); result.Add(new DerTaggedObject(false, 1, otherRevocationInfo)); } internal static void CollectOtherRevocationInfo(List<Asn1Encodable> result, DerObjectIdentifier otherRevInfoFormat, Asn1Encodable otherRevInfo) { CollectOtherRevocationInfo(result, new OtherRevocationInfoFormat(otherRevInfoFormat, otherRevInfo)); } internal static void CollectOtherRevocationInfos(List<Asn1Encodable> result, IStore<OtherRevocationInfoFormat> otherRevocationInfoStore) { if (otherRevocationInfoStore != null) { foreach (OtherRevocationInfoFormat item in otherRevocationInfoStore.EnumerateMatches(null)) { CollectOtherRevocationInfo(result, item); } } } internal static void CollectOtherRevocationInfos(List<Asn1Encodable> result, DerObjectIdentifier otherRevInfoFormat, IStore<Asn1Encodable> otherRevInfoStore) { if (otherRevInfoStore != null && otherRevInfoFormat != null) { foreach (Asn1Encodable item in otherRevInfoStore.EnumerateMatches(null)) { CollectOtherRevocationInfo(result, otherRevInfoFormat, item); } } } internal static void ValidateOtherRevocationInfo(OtherRevocationInfoFormat otherRevocationInfo) { if (CmsObjectIdentifiers.id_ri_ocsp_response.Equals(otherRevocationInfo.InfoFormat)) { OcspResponse instance = OcspResponse.GetInstance(otherRevocationInfo.Info); if (instance.ResponseStatus.IntValueExact != 0) throw new ArgumentException("cannot add unsuccessful OCSP response to CMS SignedData"); } } } }