X509CrlEntry.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.Utilities;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509.Extension;
  14. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.X509
  15. {
  16. /**
  17. * The following extensions are listed in RFC 2459 as relevant to CRL Entries
  18. *
  19. * ReasonCode Hode Instruction Code Invalidity Date Certificate Issuer
  20. * (critical)
  21. */
  22. public class X509CrlEntry
  23. : X509ExtensionBase
  24. {
  25. private CrlEntry c;
  26. private bool isIndirect;
  27. private X509Name previousCertificateIssuer;
  28. private X509Name certificateIssuer;
  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 string ToString()
  122. {
  123. StringBuilder buf = new StringBuilder();
  124. string nl = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.NewLine;
  125. buf.Append(" userCertificate: ").Append(this.SerialNumber).Append(nl);
  126. buf.Append(" revocationDate: ").Append(this.RevocationDate).Append(nl);
  127. buf.Append(" certificateIssuer: ").Append(this.GetCertificateIssuer()).Append(nl);
  128. X509Extensions extensions = c.Extensions;
  129. if (extensions != null)
  130. {
  131. IEnumerator e = extensions.ExtensionOids.GetEnumerator();
  132. if (e.MoveNext())
  133. {
  134. buf.Append(" crlEntryExtensions:").Append(nl);
  135. do
  136. {
  137. DerObjectIdentifier oid = (DerObjectIdentifier)e.Current;
  138. X509Extension ext = extensions.GetExtension(oid);
  139. if (ext.Value != null)
  140. {
  141. Asn1Object obj = Asn1Object.FromByteArray(ext.Value.GetOctets());
  142. buf.Append(" critical(")
  143. .Append(ext.IsCritical)
  144. .Append(") ");
  145. try
  146. {
  147. if (oid.Equals(X509Extensions.ReasonCode))
  148. {
  149. buf.Append(new CrlReason(DerEnumerated.GetInstance(obj)));
  150. }
  151. else if (oid.Equals(X509Extensions.CertificateIssuer))
  152. {
  153. buf.Append("Certificate issuer: ").Append(
  154. GeneralNames.GetInstance((Asn1Sequence)obj));
  155. }
  156. else
  157. {
  158. buf.Append(oid.Id);
  159. buf.Append(" value = ").Append(Asn1Dump.DumpAsString(obj));
  160. }
  161. buf.Append(nl);
  162. }
  163. catch (Exception)
  164. {
  165. buf.Append(oid.Id);
  166. buf.Append(" value = ").Append("*****").Append(nl);
  167. }
  168. }
  169. else
  170. {
  171. buf.Append(nl);
  172. }
  173. }
  174. while (e.MoveNext());
  175. }
  176. }
  177. return buf.ToString();
  178. }
  179. }
  180. }
  181. #pragma warning restore
  182. #endif