123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- #pragma warning disable
- using System;
- using System.Collections.Generic;
- using System.IO;
- using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
- using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
- using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Ocsp;
- using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
- using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
- using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
- using Best.HTTP.SecureProtocol.Org.BouncyCastle.X509;
- namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Cms
- {
- internal class CmsUtilities
- {
- // TODO Is there a .NET equivalent to this?
- // private static readonly Runtime RUNTIME = Runtime.getRuntime();
- internal static int MaximumMemory
- {
- get
- {
- // TODO Is there a .NET equivalent to this?
- long maxMem = int.MaxValue;//RUNTIME.maxMemory();
- if (maxMem > int.MaxValue)
- {
- return int.MaxValue;
- }
- return (int)maxMem;
- }
- }
- internal static ContentInfo ReadContentInfo(
- byte[] input)
- {
- // enforce limit checking as from a byte array
- return ReadContentInfo(new Asn1InputStream(input));
- }
- internal static ContentInfo ReadContentInfo(
- Stream input)
- {
- // enforce some limit checking
- return ReadContentInfo(new Asn1InputStream(input, MaximumMemory));
- }
- private static ContentInfo ReadContentInfo(
- Asn1InputStream aIn)
- {
- try
- {
- return ContentInfo.GetInstance(aIn.ReadObject());
- }
- catch (IOException e)
- {
- throw new CmsException("IOException reading content.", e);
- }
- catch (InvalidCastException e)
- {
- throw new CmsException("Malformed content.", e);
- }
- catch (ArgumentException e)
- {
- throw new CmsException("Malformed content.", e);
- }
- }
- 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 List<Asn1TaggedObject> GetAttributeCertificatesFromStore(
- IStore<X509V2AttributeCertificate> attrCertStore)
- {
- var result = new List<Asn1TaggedObject>();
- if (attrCertStore != null)
- {
- foreach (var attrCert in attrCertStore.EnumerateMatches(null))
- {
- result.Add(new DerTaggedObject(false, 2, attrCert.AttributeCertificate));
- }
- }
- return result;
- }
- internal static List<X509CertificateStructure> GetCertificatesFromStore(IStore<X509Certificate> certStore)
- {
- var result = new List<X509CertificateStructure>();
- if (certStore != null)
- {
- foreach (var cert in certStore.EnumerateMatches(null))
- {
- result.Add(cert.CertificateStructure);
- }
- }
- return result;
- }
- internal static List<CertificateList> GetCrlsFromStore(IStore<X509Crl> crlStore)
- {
- var result = new List<CertificateList>();
- if (crlStore != null)
- {
- foreach (var crl in crlStore.EnumerateMatches(null))
- {
- result.Add(crl.CertificateList);
- }
- }
- return result;
- }
- internal static List<Asn1TaggedObject> GetOtherRevocationInfosFromStore(
- IStore<OtherRevocationInfoFormat> otherRevocationInfoStore)
- {
- var result = new List<Asn1TaggedObject>();
- if (otherRevocationInfoStore != null)
- {
- foreach (var otherRevocationInfo in otherRevocationInfoStore.EnumerateMatches(null))
- {
- ValidateOtherRevocationInfo(otherRevocationInfo);
- result.Add(new DerTaggedObject(false, 1, otherRevocationInfo));
- }
- }
- return result;
- }
- internal static List<DerTaggedObject> GetOtherRevocationInfosFromStore(IStore<Asn1Encodable> otherRevInfoStore,
- DerObjectIdentifier otherRevInfoFormat)
- {
- var result = new List<DerTaggedObject>();
- if (otherRevInfoStore != null && otherRevInfoFormat != null)
- {
- foreach (var otherRevInfo in otherRevInfoStore.EnumerateMatches(null))
- {
- var otherRevocationInfo = new OtherRevocationInfoFormat(otherRevInfoFormat, otherRevInfo);
- ValidateOtherRevocationInfo(otherRevocationInfo);
- result.Add(new DerTaggedObject(false, 1, otherRevocationInfo));
- }
- }
- return result;
- }
- internal static Asn1Set CreateBerSetFromList(IEnumerable<Asn1Encodable> elements)
- {
- Asn1EncodableVector v = new Asn1EncodableVector();
- foreach (Asn1Encodable element in elements)
- {
- v.Add(element);
- }
- return new BerSet(v);
- }
- internal static Asn1Set CreateDerSetFromList(IEnumerable<Asn1Encodable> elements)
- {
- Asn1EncodableVector v = new Asn1EncodableVector();
- foreach (Asn1Encodable element in elements)
- {
- v.Add(element);
- }
- return new DerSet(v);
- }
- internal static Stream CreateBerOctetOutputStream(Stream s, int tagNo, bool isExplicit, int bufferSize)
- {
- BerOctetStringGenerator octGen = new BerOctetStringGenerator(s, tagNo, isExplicit);
- return octGen.GetOctetOutputStream(bufferSize);
- }
- internal static TbsCertificateStructure GetTbsCertificateStructure(X509Certificate cert)
- {
- return TbsCertificateStructure.GetInstance(Asn1Object.FromByteArray(cert.GetTbsCertificate()));
- }
- internal static IssuerAndSerialNumber GetIssuerAndSerialNumber(X509Certificate cert)
- {
- TbsCertificateStructure tbsCert = GetTbsCertificateStructure(cert);
- return new IssuerAndSerialNumber(tbsCert.Issuer, tbsCert.SerialNumber.Value);
- }
- internal static void ValidateOtherRevocationInfo(OtherRevocationInfoFormat otherRevocationInfo)
- {
- if (CmsObjectIdentifiers.id_ri_ocsp_response.Equals(otherRevocationInfo.InfoFormat))
- {
- OcspResponse ocspResponse = OcspResponse.GetInstance(otherRevocationInfo.Info);
- if (OcspResponseStatus.Successful != ocspResponse.ResponseStatus.IntValueExact)
- throw new ArgumentException("cannot add unsuccessful OCSP response to CMS SignedData");
- }
- }
- }
- }
- #pragma warning restore
- #endif
|