CMSUtils.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509.Store;
  14. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Cms
  15. {
  16. internal class CmsUtilities
  17. {
  18. // TODO Is there a .NET equivalent to this?
  19. // private static readonly Runtime RUNTIME = Runtime.getRuntime();
  20. internal static int MaximumMemory
  21. {
  22. get
  23. {
  24. // TODO Is there a .NET equivalent to this?
  25. long maxMem = int.MaxValue;//RUNTIME.maxMemory();
  26. if (maxMem > int.MaxValue)
  27. {
  28. return int.MaxValue;
  29. }
  30. return (int)maxMem;
  31. }
  32. }
  33. internal static ContentInfo ReadContentInfo(
  34. byte[] input)
  35. {
  36. // enforce limit checking as from a byte array
  37. return ReadContentInfo(new Asn1InputStream(input));
  38. }
  39. internal static ContentInfo ReadContentInfo(
  40. Stream input)
  41. {
  42. // enforce some limit checking
  43. return ReadContentInfo(new Asn1InputStream(input, MaximumMemory));
  44. }
  45. private static ContentInfo ReadContentInfo(
  46. Asn1InputStream aIn)
  47. {
  48. try
  49. {
  50. return ContentInfo.GetInstance(aIn.ReadObject());
  51. }
  52. catch (IOException e)
  53. {
  54. throw new CmsException("IOException reading content.", e);
  55. }
  56. catch (InvalidCastException e)
  57. {
  58. throw new CmsException("Malformed content.", e);
  59. }
  60. catch (ArgumentException e)
  61. {
  62. throw new CmsException("Malformed content.", e);
  63. }
  64. }
  65. public static byte[] StreamToByteArray(
  66. Stream inStream)
  67. {
  68. return Streams.ReadAll(inStream);
  69. }
  70. public static byte[] StreamToByteArray(
  71. Stream inStream,
  72. int limit)
  73. {
  74. return Streams.ReadAllLimited(inStream, limit);
  75. }
  76. public static IList GetCertificatesFromStore(
  77. IX509Store certStore)
  78. {
  79. try
  80. {
  81. IList certs = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  82. if (certStore != null)
  83. {
  84. foreach (X509Certificate c in certStore.GetMatches(null))
  85. {
  86. certs.Add(
  87. X509CertificateStructure.GetInstance(
  88. Asn1Object.FromByteArray(c.GetEncoded())));
  89. }
  90. }
  91. return certs;
  92. }
  93. catch (CertificateEncodingException e)
  94. {
  95. throw new CmsException("error encoding certs", e);
  96. }
  97. catch (Exception e)
  98. {
  99. throw new CmsException("error processing certs", e);
  100. }
  101. }
  102. public static IList GetCrlsFromStore(
  103. IX509Store crlStore)
  104. {
  105. try
  106. {
  107. IList crls = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  108. if (crlStore != null)
  109. {
  110. foreach (X509Crl c in crlStore.GetMatches(null))
  111. {
  112. crls.Add(
  113. CertificateList.GetInstance(
  114. Asn1Object.FromByteArray(c.GetEncoded())));
  115. }
  116. }
  117. return crls;
  118. }
  119. catch (CrlException e)
  120. {
  121. throw new CmsException("error encoding crls", e);
  122. }
  123. catch (Exception e)
  124. {
  125. throw new CmsException("error processing crls", e);
  126. }
  127. }
  128. public static Asn1Set CreateBerSetFromList(
  129. IList berObjects)
  130. {
  131. Asn1EncodableVector v = new Asn1EncodableVector();
  132. foreach (Asn1Encodable ae in berObjects)
  133. {
  134. v.Add(ae);
  135. }
  136. return new BerSet(v);
  137. }
  138. public static Asn1Set CreateDerSetFromList(
  139. IList derObjects)
  140. {
  141. Asn1EncodableVector v = new Asn1EncodableVector();
  142. foreach (Asn1Encodable ae in derObjects)
  143. {
  144. v.Add(ae);
  145. }
  146. return new DerSet(v);
  147. }
  148. internal static Stream CreateBerOctetOutputStream(Stream s, int tagNo, bool isExplicit, int bufferSize)
  149. {
  150. BerOctetStringGenerator octGen = new BerOctetStringGenerator(s, tagNo, isExplicit);
  151. return octGen.GetOctetOutputStream(bufferSize);
  152. }
  153. internal static TbsCertificateStructure GetTbsCertificateStructure(X509Certificate cert)
  154. {
  155. return TbsCertificateStructure.GetInstance(Asn1Object.FromByteArray(cert.GetTbsCertificate()));
  156. }
  157. internal static IssuerAndSerialNumber GetIssuerAndSerialNumber(X509Certificate cert)
  158. {
  159. TbsCertificateStructure tbsCert = GetTbsCertificateStructure(cert);
  160. return new IssuerAndSerialNumber(tbsCert.Issuer, tbsCert.SerialNumber.Value);
  161. }
  162. }
  163. }
  164. #pragma warning restore
  165. #endif