CMSUtils.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Ocsp;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  11. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  12. using Best.HTTP.SecureProtocol.Org.BouncyCastle.X509;
  13. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Cms
  14. {
  15. internal class CmsUtilities
  16. {
  17. // TODO Is there a .NET equivalent to this?
  18. // private static readonly Runtime RUNTIME = Runtime.getRuntime();
  19. internal static int MaximumMemory
  20. {
  21. get
  22. {
  23. // TODO Is there a .NET equivalent to this?
  24. long maxMem = int.MaxValue;//RUNTIME.maxMemory();
  25. if (maxMem > int.MaxValue)
  26. {
  27. return int.MaxValue;
  28. }
  29. return (int)maxMem;
  30. }
  31. }
  32. internal static ContentInfo ReadContentInfo(
  33. byte[] input)
  34. {
  35. // enforce limit checking as from a byte array
  36. return ReadContentInfo(new Asn1InputStream(input));
  37. }
  38. internal static ContentInfo ReadContentInfo(
  39. Stream input)
  40. {
  41. // enforce some limit checking
  42. return ReadContentInfo(new Asn1InputStream(input, MaximumMemory));
  43. }
  44. private static ContentInfo ReadContentInfo(
  45. Asn1InputStream aIn)
  46. {
  47. try
  48. {
  49. return ContentInfo.GetInstance(aIn.ReadObject());
  50. }
  51. catch (IOException e)
  52. {
  53. throw new CmsException("IOException reading content.", e);
  54. }
  55. catch (InvalidCastException e)
  56. {
  57. throw new CmsException("Malformed content.", e);
  58. }
  59. catch (ArgumentException e)
  60. {
  61. throw new CmsException("Malformed content.", e);
  62. }
  63. }
  64. internal static byte[] StreamToByteArray(Stream inStream)
  65. {
  66. return Streams.ReadAll(inStream);
  67. }
  68. internal static byte[] StreamToByteArray(Stream inStream, int limit)
  69. {
  70. return Streams.ReadAllLimited(inStream, limit);
  71. }
  72. internal static List<Asn1TaggedObject> GetAttributeCertificatesFromStore(
  73. IStore<X509V2AttributeCertificate> attrCertStore)
  74. {
  75. var result = new List<Asn1TaggedObject>();
  76. if (attrCertStore != null)
  77. {
  78. foreach (var attrCert in attrCertStore.EnumerateMatches(null))
  79. {
  80. result.Add(new DerTaggedObject(false, 2, attrCert.AttributeCertificate));
  81. }
  82. }
  83. return result;
  84. }
  85. internal static List<X509CertificateStructure> GetCertificatesFromStore(IStore<X509Certificate> certStore)
  86. {
  87. var result = new List<X509CertificateStructure>();
  88. if (certStore != null)
  89. {
  90. foreach (var cert in certStore.EnumerateMatches(null))
  91. {
  92. result.Add(cert.CertificateStructure);
  93. }
  94. }
  95. return result;
  96. }
  97. internal static List<CertificateList> GetCrlsFromStore(IStore<X509Crl> crlStore)
  98. {
  99. var result = new List<CertificateList>();
  100. if (crlStore != null)
  101. {
  102. foreach (var crl in crlStore.EnumerateMatches(null))
  103. {
  104. result.Add(crl.CertificateList);
  105. }
  106. }
  107. return result;
  108. }
  109. internal static List<Asn1TaggedObject> GetOtherRevocationInfosFromStore(
  110. IStore<OtherRevocationInfoFormat> otherRevocationInfoStore)
  111. {
  112. var result = new List<Asn1TaggedObject>();
  113. if (otherRevocationInfoStore != null)
  114. {
  115. foreach (var otherRevocationInfo in otherRevocationInfoStore.EnumerateMatches(null))
  116. {
  117. ValidateOtherRevocationInfo(otherRevocationInfo);
  118. result.Add(new DerTaggedObject(false, 1, otherRevocationInfo));
  119. }
  120. }
  121. return result;
  122. }
  123. internal static List<DerTaggedObject> GetOtherRevocationInfosFromStore(IStore<Asn1Encodable> otherRevInfoStore,
  124. DerObjectIdentifier otherRevInfoFormat)
  125. {
  126. var result = new List<DerTaggedObject>();
  127. if (otherRevInfoStore != null && otherRevInfoFormat != null)
  128. {
  129. foreach (var otherRevInfo in otherRevInfoStore.EnumerateMatches(null))
  130. {
  131. var otherRevocationInfo = new OtherRevocationInfoFormat(otherRevInfoFormat, otherRevInfo);
  132. ValidateOtherRevocationInfo(otherRevocationInfo);
  133. result.Add(new DerTaggedObject(false, 1, otherRevocationInfo));
  134. }
  135. }
  136. return result;
  137. }
  138. internal static Asn1Set CreateBerSetFromList(IEnumerable<Asn1Encodable> elements)
  139. {
  140. Asn1EncodableVector v = new Asn1EncodableVector();
  141. foreach (Asn1Encodable element in elements)
  142. {
  143. v.Add(element);
  144. }
  145. return new BerSet(v);
  146. }
  147. internal static Asn1Set CreateDerSetFromList(IEnumerable<Asn1Encodable> elements)
  148. {
  149. Asn1EncodableVector v = new Asn1EncodableVector();
  150. foreach (Asn1Encodable element in elements)
  151. {
  152. v.Add(element);
  153. }
  154. return new DerSet(v);
  155. }
  156. internal static Stream CreateBerOctetOutputStream(Stream s, int tagNo, bool isExplicit, int bufferSize)
  157. {
  158. BerOctetStringGenerator octGen = new BerOctetStringGenerator(s, tagNo, isExplicit);
  159. return octGen.GetOctetOutputStream(bufferSize);
  160. }
  161. internal static TbsCertificateStructure GetTbsCertificateStructure(X509Certificate cert)
  162. {
  163. return TbsCertificateStructure.GetInstance(Asn1Object.FromByteArray(cert.GetTbsCertificate()));
  164. }
  165. internal static IssuerAndSerialNumber GetIssuerAndSerialNumber(X509Certificate cert)
  166. {
  167. TbsCertificateStructure tbsCert = GetTbsCertificateStructure(cert);
  168. return new IssuerAndSerialNumber(tbsCert.Issuer, tbsCert.SerialNumber.Value);
  169. }
  170. internal static void ValidateOtherRevocationInfo(OtherRevocationInfoFormat otherRevocationInfo)
  171. {
  172. if (CmsObjectIdentifiers.id_ri_ocsp_response.Equals(otherRevocationInfo.InfoFormat))
  173. {
  174. OcspResponse ocspResponse = OcspResponse.GetInstance(otherRevocationInfo.Info);
  175. if (OcspResponseStatus.Successful != ocspResponse.ResponseStatus.IntValueExact)
  176. throw new ArgumentException("cannot add unsuccessful OCSP response to CMS SignedData");
  177. }
  178. }
  179. }
  180. }
  181. #pragma warning restore
  182. #endif