X509CrlParser.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 System.Text;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  14. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.X509
  15. {
  16. public class X509CrlParser
  17. {
  18. private static readonly PemParser PemCrlParser = new PemParser("CRL");
  19. private readonly bool lazyAsn1;
  20. private Asn1Set sCrlData;
  21. private int sCrlDataObjectCount;
  22. private Stream currentCrlStream;
  23. public X509CrlParser()
  24. : this(false)
  25. {
  26. }
  27. public X509CrlParser(
  28. bool lazyAsn1)
  29. {
  30. this.lazyAsn1 = lazyAsn1;
  31. }
  32. private X509Crl ReadPemCrl(
  33. Stream inStream)
  34. {
  35. Asn1Sequence seq = PemCrlParser.ReadPemObject(inStream);
  36. return seq == null
  37. ? null
  38. : CreateX509Crl(CertificateList.GetInstance(seq));
  39. }
  40. private X509Crl ReadDerCrl(
  41. Asn1InputStream dIn)
  42. {
  43. Asn1Sequence seq = (Asn1Sequence)dIn.ReadObject();
  44. if (seq.Count > 1 && seq[0] is DerObjectIdentifier)
  45. {
  46. if (seq[0].Equals(PkcsObjectIdentifiers.SignedData))
  47. {
  48. sCrlData = SignedData.GetInstance(
  49. Asn1Sequence.GetInstance((Asn1TaggedObject) seq[1], true)).Crls;
  50. return GetCrl();
  51. }
  52. }
  53. return CreateX509Crl(CertificateList.GetInstance(seq));
  54. }
  55. private X509Crl GetCrl()
  56. {
  57. if (sCrlData == null || sCrlDataObjectCount >= sCrlData.Count)
  58. {
  59. return null;
  60. }
  61. return CreateX509Crl(
  62. CertificateList.GetInstance(
  63. sCrlData[sCrlDataObjectCount++]));
  64. }
  65. protected virtual X509Crl CreateX509Crl(
  66. CertificateList c)
  67. {
  68. return new X509Crl(c);
  69. }
  70. /// <summary>
  71. /// Create loading data from byte array.
  72. /// </summary>
  73. /// <param name="input"></param>
  74. public X509Crl ReadCrl(
  75. byte[] input)
  76. {
  77. return ReadCrl(new MemoryStream(input, false));
  78. }
  79. /// <summary>
  80. /// Create loading data from byte array.
  81. /// </summary>
  82. /// <param name="input"></param>
  83. public ICollection ReadCrls(
  84. byte[] input)
  85. {
  86. return ReadCrls(new MemoryStream(input, false));
  87. }
  88. /**
  89. * Generates a certificate revocation list (CRL) object and initializes
  90. * it with the data read from the input stream inStream.
  91. */
  92. public X509Crl ReadCrl(
  93. Stream inStream)
  94. {
  95. if (inStream == null)
  96. throw new ArgumentNullException("inStream");
  97. if (!inStream.CanRead)
  98. throw new ArgumentException("inStream must be read-able", "inStream");
  99. if (currentCrlStream == null)
  100. {
  101. currentCrlStream = inStream;
  102. sCrlData = null;
  103. sCrlDataObjectCount = 0;
  104. }
  105. else if (currentCrlStream != inStream) // reset if input stream has changed
  106. {
  107. currentCrlStream = inStream;
  108. sCrlData = null;
  109. sCrlDataObjectCount = 0;
  110. }
  111. try
  112. {
  113. if (sCrlData != null)
  114. {
  115. if (sCrlDataObjectCount != sCrlData.Count)
  116. {
  117. return GetCrl();
  118. }
  119. sCrlData = null;
  120. sCrlDataObjectCount = 0;
  121. return null;
  122. }
  123. PushbackStream pis = new PushbackStream(inStream);
  124. int tag = pis.ReadByte();
  125. if (tag < 0)
  126. return null;
  127. pis.Unread(tag);
  128. if (tag != 0x30) // assume ascii PEM encoded.
  129. {
  130. return ReadPemCrl(pis);
  131. }
  132. Asn1InputStream asn1 = lazyAsn1
  133. ? new LazyAsn1InputStream(pis)
  134. : new Asn1InputStream(pis);
  135. return ReadDerCrl(asn1);
  136. }
  137. catch (CrlException e)
  138. {
  139. throw e;
  140. }
  141. catch (Exception e)
  142. {
  143. throw new CrlException(e.ToString());
  144. }
  145. }
  146. /**
  147. * Returns a (possibly empty) collection view of the CRLs read from
  148. * the given input stream inStream.
  149. *
  150. * The inStream may contain a sequence of DER-encoded CRLs, or
  151. * a PKCS#7 CRL set. This is a PKCS#7 SignedData object, with the
  152. * only significant field being crls. In particular the signature
  153. * and the contents are ignored.
  154. */
  155. public ICollection ReadCrls(
  156. Stream inStream)
  157. {
  158. X509Crl crl;
  159. IList crls = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  160. while ((crl = ReadCrl(inStream)) != null)
  161. {
  162. crls.Add(crl);
  163. }
  164. return crls;
  165. }
  166. }
  167. }
  168. #pragma warning restore
  169. #endif