X509CrlEntry.cs 4.8 KB

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