TBSCertList.cs 6.4 KB

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