RequestedCertificate.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.IsisMtt.Ocsp
  8. {
  9. /**
  10. * ISIS-MTT-Optional: The certificate requested by the client by inserting the
  11. * RetrieveIfAllowed extension in the request, will be returned in this
  12. * extension.
  13. * <p/>
  14. * ISIS-MTT-SigG: The signature act allows publishing certificates only then,
  15. * when the certificate owner gives his isExplicit permission. Accordingly, there
  16. * may be �nondownloadable� certificates, about which the responder must provide
  17. * status information, but MUST NOT include them in the response. Clients may
  18. * get therefore the following three kind of answers on a single request
  19. * including the RetrieveIfAllowed extension:
  20. * <ul>
  21. * <li> a) the responder supports the extension and is allowed to publish the
  22. * certificate: RequestedCertificate returned including the requested
  23. * certificate</li>
  24. * <li>b) the responder supports the extension but is NOT allowed to publish
  25. * the certificate: RequestedCertificate returned including an empty OCTET
  26. * STRING</li>
  27. * <li>c) the responder does not support the extension: RequestedCertificate is
  28. * not included in the response</li>
  29. * </ul>
  30. * Clients requesting RetrieveIfAllowed MUST be able to handle these cases. If
  31. * any of the OCTET STRING options is used, it MUST contain the DER encoding of
  32. * the requested certificate.
  33. * <p/>
  34. * <pre>
  35. * RequestedCertificate ::= CHOICE {
  36. * Certificate Certificate,
  37. * publicKeyCertificate [0] EXPLICIT OCTET STRING,
  38. * attributeCertificate [1] EXPLICIT OCTET STRING
  39. * }
  40. * </pre>
  41. */
  42. public class RequestedCertificate
  43. : Asn1Encodable, IAsn1Choice
  44. {
  45. public enum Choice
  46. {
  47. Certificate = -1,
  48. PublicKeyCertificate = 0,
  49. AttributeCertificate = 1
  50. }
  51. private readonly X509CertificateStructure cert;
  52. private readonly byte[] publicKeyCert;
  53. private readonly byte[] attributeCert;
  54. public static RequestedCertificate GetInstance(
  55. object obj)
  56. {
  57. if (obj == null || obj is RequestedCertificate)
  58. {
  59. return (RequestedCertificate) obj;
  60. }
  61. if (obj is Asn1Sequence)
  62. {
  63. return new RequestedCertificate(X509CertificateStructure.GetInstance(obj));
  64. }
  65. if (obj is Asn1TaggedObject)
  66. {
  67. return new RequestedCertificate((Asn1TaggedObject) obj);
  68. }
  69. throw new ArgumentException("unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  70. }
  71. public static RequestedCertificate GetInstance(
  72. Asn1TaggedObject obj,
  73. bool isExplicit)
  74. {
  75. if (!isExplicit)
  76. throw new ArgumentException("choice item must be explicitly tagged");
  77. return GetInstance(obj.GetObject());
  78. }
  79. private RequestedCertificate(
  80. Asn1TaggedObject tagged)
  81. {
  82. switch ((Choice) tagged.TagNo)
  83. {
  84. case Choice.AttributeCertificate:
  85. this.attributeCert = Asn1OctetString.GetInstance(tagged, true).GetOctets();
  86. break;
  87. case Choice.PublicKeyCertificate:
  88. this.publicKeyCert = Asn1OctetString.GetInstance(tagged, true).GetOctets();
  89. break;
  90. default:
  91. throw new ArgumentException("unknown tag number: " + tagged.TagNo);
  92. }
  93. }
  94. /**
  95. * Constructor from a given details.
  96. * <p/>
  97. * Only one parameter can be given. All other must be <code>null</code>.
  98. *
  99. * @param certificate Given as Certificate
  100. */
  101. public RequestedCertificate(
  102. X509CertificateStructure certificate)
  103. {
  104. this.cert = certificate;
  105. }
  106. public RequestedCertificate(
  107. Choice type,
  108. byte[] certificateOctets)
  109. : this(new DerTaggedObject((int) type, new DerOctetString(certificateOctets)))
  110. {
  111. }
  112. public Choice Type
  113. {
  114. get
  115. {
  116. if (cert != null)
  117. return Choice.Certificate;
  118. if (publicKeyCert != null)
  119. return Choice.PublicKeyCertificate;
  120. return Choice.AttributeCertificate;
  121. }
  122. }
  123. public byte[] GetCertificateBytes()
  124. {
  125. if (cert != null)
  126. {
  127. try
  128. {
  129. return cert.GetEncoded();
  130. }
  131. catch (IOException e)
  132. {
  133. throw new InvalidOperationException("can't decode certificate: " + e);
  134. }
  135. }
  136. if (publicKeyCert != null)
  137. return publicKeyCert;
  138. return attributeCert;
  139. }
  140. /**
  141. * Produce an object suitable for an Asn1OutputStream.
  142. * <p/>
  143. * Returns:
  144. * <p/>
  145. * <pre>
  146. * RequestedCertificate ::= CHOICE {
  147. * Certificate Certificate,
  148. * publicKeyCertificate [0] EXPLICIT OCTET STRING,
  149. * attributeCertificate [1] EXPLICIT OCTET STRING
  150. * }
  151. * </pre>
  152. *
  153. * @return an Asn1Object
  154. */
  155. public override Asn1Object ToAsn1Object()
  156. {
  157. if (publicKeyCert != null)
  158. {
  159. return new DerTaggedObject(0, new DerOctetString(publicKeyCert));
  160. }
  161. if (attributeCert != null)
  162. {
  163. return new DerTaggedObject(1, new DerOctetString(attributeCert));
  164. }
  165. return cert.ToAsn1Object();
  166. }
  167. }
  168. }
  169. #pragma warning restore
  170. #endif