TBSCertList.cs 6.5 KB

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