TBSCertList.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  7. {
  8. public class CrlEntry
  9. : Asn1Encodable
  10. {
  11. internal Asn1Sequence seq;
  12. internal DerInteger userCertificate;
  13. internal Time revocationDate;
  14. internal X509Extensions crlEntryExtensions;
  15. public CrlEntry(Asn1Sequence seq)
  16. {
  17. if (seq.Count < 2 || seq.Count > 3)
  18. throw new ArgumentException("Bad sequence size: " + seq.Count);
  19. this.seq = seq;
  20. userCertificate = DerInteger.GetInstance(seq[0]);
  21. revocationDate = Time.GetInstance(seq[1]);
  22. }
  23. public DerInteger UserCertificate
  24. {
  25. get { return userCertificate; }
  26. }
  27. public Time RevocationDate
  28. {
  29. get { return revocationDate; }
  30. }
  31. public X509Extensions Extensions
  32. {
  33. get
  34. {
  35. if (crlEntryExtensions == null && seq.Count == 3)
  36. {
  37. crlEntryExtensions = X509Extensions.GetInstance(seq[2]);
  38. }
  39. return crlEntryExtensions;
  40. }
  41. }
  42. public override Asn1Object ToAsn1Object()
  43. {
  44. return seq;
  45. }
  46. }
  47. /**
  48. * PKIX RFC-2459 - TbsCertList object.
  49. * <pre>
  50. * TbsCertList ::= Sequence {
  51. * version Version OPTIONAL,
  52. * -- if present, shall be v2
  53. * signature AlgorithmIdentifier,
  54. * issuer Name,
  55. * thisUpdate Time,
  56. * nextUpdate Time OPTIONAL,
  57. * revokedCertificates Sequence OF Sequence {
  58. * userCertificate CertificateSerialNumber,
  59. * revocationDate Time,
  60. * crlEntryExtensions Extensions OPTIONAL
  61. * -- if present, shall be v2
  62. * } OPTIONAL,
  63. * crlExtensions [0] EXPLICIT Extensions OPTIONAL
  64. * -- if present, shall be v2
  65. * }
  66. * </pre>
  67. */
  68. public class TbsCertificateList
  69. : Asn1Encodable
  70. {
  71. private class RevokedCertificatesEnumeration
  72. : IEnumerable<CrlEntry>
  73. {
  74. private readonly IEnumerable<Asn1Encodable> en;
  75. internal RevokedCertificatesEnumeration(IEnumerable<Asn1Encodable> en)
  76. {
  77. this.en = en;
  78. }
  79. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  80. {
  81. return GetEnumerator();
  82. }
  83. public IEnumerator<CrlEntry> GetEnumerator()
  84. {
  85. return new RevokedCertificatesEnumerator(en.GetEnumerator());
  86. }
  87. private class RevokedCertificatesEnumerator
  88. : IEnumerator<CrlEntry>
  89. {
  90. private readonly IEnumerator<Asn1Encodable> e;
  91. internal RevokedCertificatesEnumerator(IEnumerator<Asn1Encodable> e)
  92. {
  93. this.e = e;
  94. }
  95. public virtual void Dispose()
  96. {
  97. }
  98. public bool MoveNext()
  99. {
  100. return e.MoveNext();
  101. }
  102. public void Reset()
  103. {
  104. e.Reset();
  105. }
  106. object System.Collections.IEnumerator.Current
  107. {
  108. get { return Current; }
  109. }
  110. public CrlEntry Current
  111. {
  112. get { return new CrlEntry(Asn1Sequence.GetInstance(e.Current)); }
  113. }
  114. }
  115. }
  116. internal Asn1Sequence seq;
  117. internal DerInteger version;
  118. internal AlgorithmIdentifier signature;
  119. internal X509Name issuer;
  120. internal Time thisUpdate;
  121. internal Time nextUpdate;
  122. internal Asn1Sequence revokedCertificates;
  123. internal X509Extensions crlExtensions;
  124. public static TbsCertificateList GetInstance(
  125. Asn1TaggedObject obj,
  126. bool explicitly)
  127. {
  128. return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
  129. }
  130. public static TbsCertificateList GetInstance(
  131. object obj)
  132. {
  133. TbsCertificateList list = obj as TbsCertificateList;
  134. if (obj == null || list != null)
  135. {
  136. return list;
  137. }
  138. if (obj is Asn1Sequence)
  139. {
  140. return new TbsCertificateList((Asn1Sequence) obj);
  141. }
  142. throw new ArgumentException("unknown object in factory: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  143. }
  144. internal TbsCertificateList(
  145. Asn1Sequence seq)
  146. {
  147. if (seq.Count < 3 || seq.Count > 7)
  148. {
  149. throw new ArgumentException("Bad sequence size: " + seq.Count);
  150. }
  151. int seqPos = 0;
  152. this.seq = seq;
  153. if (seq[seqPos] is DerInteger)
  154. {
  155. version = DerInteger.GetInstance(seq[seqPos++]);
  156. }
  157. else
  158. {
  159. version = new DerInteger(0);
  160. }
  161. signature = AlgorithmIdentifier.GetInstance(seq[seqPos++]);
  162. issuer = X509Name.GetInstance(seq[seqPos++]);
  163. thisUpdate = Time.GetInstance(seq[seqPos++]);
  164. if (seqPos < seq.Count
  165. && (seq[seqPos] is Asn1UtcTime
  166. || seq[seqPos] is Asn1GeneralizedTime
  167. || seq[seqPos] is Time))
  168. {
  169. nextUpdate = Time.GetInstance(seq[seqPos++]);
  170. }
  171. if (seqPos < seq.Count
  172. && !(seq[seqPos] is Asn1TaggedObject))
  173. {
  174. revokedCertificates = Asn1Sequence.GetInstance(seq[seqPos++]);
  175. }
  176. if (seqPos < seq.Count
  177. && seq[seqPos] is Asn1TaggedObject)
  178. {
  179. crlExtensions = X509Extensions.GetInstance(seq[seqPos]);
  180. }
  181. }
  182. public int Version
  183. {
  184. get { return version.IntValueExact + 1; }
  185. }
  186. public DerInteger VersionNumber
  187. {
  188. get { return version; }
  189. }
  190. public AlgorithmIdentifier Signature
  191. {
  192. get { return signature; }
  193. }
  194. public X509Name Issuer
  195. {
  196. get { return issuer; }
  197. }
  198. public Time ThisUpdate
  199. {
  200. get { return thisUpdate; }
  201. }
  202. public Time NextUpdate
  203. {
  204. get { return nextUpdate; }
  205. }
  206. public CrlEntry[] GetRevokedCertificates()
  207. {
  208. if (revokedCertificates == null)
  209. {
  210. return new CrlEntry[0];
  211. }
  212. CrlEntry[] entries = new CrlEntry[revokedCertificates.Count];
  213. for (int i = 0; i < entries.Length; i++)
  214. {
  215. entries[i] = new CrlEntry(Asn1Sequence.GetInstance(revokedCertificates[i]));
  216. }
  217. return entries;
  218. }
  219. public IEnumerable<CrlEntry> GetRevokedCertificateEnumeration()
  220. {
  221. if (revokedCertificates == null)
  222. return new List<CrlEntry>(0);
  223. return new RevokedCertificatesEnumeration(revokedCertificates);
  224. }
  225. public X509Extensions Extensions
  226. {
  227. get { return crlExtensions; }
  228. }
  229. public override Asn1Object ToAsn1Object()
  230. {
  231. return seq;
  232. }
  233. }
  234. }
  235. #pragma warning restore
  236. #endif