X509CrlEntry.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Text;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Utilities;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  11. using Best.HTTP.SecureProtocol.Org.BouncyCastle.X509.Extension;
  12. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.X509
  13. {
  14. /**
  15. * The following extensions are listed in RFC 2459 as relevant to CRL Entries
  16. *
  17. * ReasonCode Hode Instruction Code Invalidity Date Certificate Issuer
  18. * (critical)
  19. */
  20. public class X509CrlEntry
  21. : X509ExtensionBase
  22. {
  23. private CrlEntry c;
  24. private bool isIndirect;
  25. private X509Name previousCertificateIssuer;
  26. private X509Name certificateIssuer;
  27. private volatile bool hashValueSet;
  28. private volatile int hashValue;
  29. public X509CrlEntry(
  30. CrlEntry c)
  31. {
  32. this.c = c;
  33. this.certificateIssuer = loadCertificateIssuer();
  34. }
  35. /**
  36. * Constructor for CRLEntries of indirect CRLs. If <code>isIndirect</code>
  37. * is <code>false</code> {@link #getCertificateIssuer()} will always
  38. * return <code>null</code>, <code>previousCertificateIssuer</code> is
  39. * ignored. If this <code>isIndirect</code> is specified and this CrlEntry
  40. * has no certificate issuer CRL entry extension
  41. * <code>previousCertificateIssuer</code> is returned by
  42. * {@link #getCertificateIssuer()}.
  43. *
  44. * @param c
  45. * TbsCertificateList.CrlEntry object.
  46. * @param isIndirect
  47. * <code>true</code> if the corresponding CRL is a indirect
  48. * CRL.
  49. * @param previousCertificateIssuer
  50. * Certificate issuer of the previous CrlEntry.
  51. */
  52. public X509CrlEntry(
  53. CrlEntry c,
  54. bool isIndirect,
  55. X509Name previousCertificateIssuer)
  56. {
  57. this.c = c;
  58. this.isIndirect = isIndirect;
  59. this.previousCertificateIssuer = previousCertificateIssuer;
  60. this.certificateIssuer = loadCertificateIssuer();
  61. }
  62. private X509Name loadCertificateIssuer()
  63. {
  64. if (!isIndirect)
  65. {
  66. return null;
  67. }
  68. Asn1OctetString ext = GetExtensionValue(X509Extensions.CertificateIssuer);
  69. if (ext == null)
  70. {
  71. return previousCertificateIssuer;
  72. }
  73. try
  74. {
  75. GeneralName[] names = GeneralNames.GetInstance(
  76. X509ExtensionUtilities.FromExtensionValue(ext)).GetNames();
  77. for (int i = 0; i < names.Length; i++)
  78. {
  79. if (names[i].TagNo == GeneralName.DirectoryName)
  80. {
  81. return X509Name.GetInstance(names[i].Name);
  82. }
  83. }
  84. }
  85. catch (Exception)
  86. {
  87. }
  88. return null;
  89. }
  90. public X509Name GetCertificateIssuer()
  91. {
  92. return certificateIssuer;
  93. }
  94. protected override X509Extensions GetX509Extensions()
  95. {
  96. return c.Extensions;
  97. }
  98. public byte[] GetEncoded()
  99. {
  100. try
  101. {
  102. return c.GetDerEncoded();
  103. }
  104. catch (Exception e)
  105. {
  106. throw new CrlException(e.ToString());
  107. }
  108. }
  109. public BigInteger SerialNumber
  110. {
  111. get { return c.UserCertificate.Value; }
  112. }
  113. public DateTime RevocationDate
  114. {
  115. get { return c.RevocationDate.ToDateTime(); }
  116. }
  117. public bool HasExtensions
  118. {
  119. get { return c.Extensions != null; }
  120. }
  121. public override bool Equals(object other)
  122. {
  123. if (this == other)
  124. return true;
  125. X509CrlEntry that = other as X509CrlEntry;
  126. if (null == that)
  127. return false;
  128. if (this.hashValueSet && that.hashValueSet)
  129. {
  130. if (this.hashValue != that.hashValue)
  131. return false;
  132. }
  133. return this.c.Equals(that.c);
  134. }
  135. public override int GetHashCode()
  136. {
  137. if (!hashValueSet)
  138. {
  139. hashValue = this.c.GetHashCode();
  140. hashValueSet = true;
  141. }
  142. return hashValue;
  143. }
  144. public override string ToString()
  145. {
  146. StringBuilder buf = new StringBuilder();
  147. buf.Append(" userCertificate: ").Append(this.SerialNumber).AppendLine();
  148. buf.Append(" revocationDate: ").Append(this.RevocationDate).AppendLine();
  149. buf.Append(" certificateIssuer: ").Append(this.GetCertificateIssuer()).AppendLine();
  150. X509Extensions extensions = c.Extensions;
  151. if (extensions != null)
  152. {
  153. var e = extensions.ExtensionOids.GetEnumerator();
  154. if (e.MoveNext())
  155. {
  156. buf.Append(" crlEntryExtensions:").AppendLine();
  157. do
  158. {
  159. DerObjectIdentifier oid = e.Current;
  160. X509Extension ext = extensions.GetExtension(oid);
  161. if (ext.Value != null)
  162. {
  163. Asn1Object obj = X509ExtensionUtilities.FromExtensionValue(ext.Value);
  164. buf.Append(" critical(")
  165. .Append(ext.IsCritical)
  166. .Append(") ");
  167. try
  168. {
  169. if (oid.Equals(X509Extensions.ReasonCode))
  170. {
  171. buf.Append(new CrlReason(DerEnumerated.GetInstance(obj)));
  172. }
  173. else if (oid.Equals(X509Extensions.CertificateIssuer))
  174. {
  175. buf.Append("Certificate issuer: ").Append(
  176. GeneralNames.GetInstance((Asn1Sequence)obj));
  177. }
  178. else
  179. {
  180. buf.Append(oid.Id);
  181. buf.Append(" value = ").Append(Asn1Dump.DumpAsString(obj));
  182. }
  183. buf.AppendLine();
  184. }
  185. catch (Exception)
  186. {
  187. buf.Append(oid.Id);
  188. buf.Append(" value = ").Append("*****").AppendLine();
  189. }
  190. }
  191. else
  192. {
  193. buf.AppendLine();
  194. }
  195. }
  196. while (e.MoveNext());
  197. }
  198. }
  199. return buf.ToString();
  200. }
  201. }
  202. }
  203. #pragma warning restore
  204. #endif