ResponderID.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Asn1;
  4. using Org.BouncyCastle.Asn1.X509;
  5. namespace Org.BouncyCastle.Asn1.Ocsp
  6. {
  7. public class ResponderID
  8. : Asn1Encodable, IAsn1Choice
  9. {
  10. private readonly Asn1Encodable id;
  11. public static ResponderID GetInstance(
  12. object obj)
  13. {
  14. if (obj == null || obj is ResponderID)
  15. {
  16. return (ResponderID)obj;
  17. }
  18. if (obj is DerOctetString)
  19. {
  20. return new ResponderID((DerOctetString)obj);
  21. }
  22. if (obj is Asn1TaggedObject)
  23. {
  24. Asn1TaggedObject o = (Asn1TaggedObject)obj;
  25. if (o.TagNo == 1)
  26. {
  27. return new ResponderID(X509Name.GetInstance(o, true));
  28. }
  29. return new ResponderID(Asn1OctetString.GetInstance(o, true));
  30. }
  31. return new ResponderID(X509Name.GetInstance(obj));
  32. }
  33. public ResponderID(
  34. Asn1OctetString id)
  35. {
  36. if (id == null)
  37. throw new ArgumentNullException("id");
  38. this.id = id;
  39. }
  40. public ResponderID(
  41. X509Name id)
  42. {
  43. if (id == null)
  44. throw new ArgumentNullException("id");
  45. this.id = id;
  46. }
  47. public static ResponderID GetInstance(
  48. Asn1TaggedObject obj,
  49. bool isExplicit)
  50. {
  51. return GetInstance(obj.GetObject()); // must be explicitly tagged
  52. }
  53. public virtual byte[] GetKeyHash()
  54. {
  55. if (id is Asn1OctetString)
  56. {
  57. return ((Asn1OctetString)id).GetOctets();
  58. }
  59. return null;
  60. }
  61. public virtual X509Name Name
  62. {
  63. get
  64. {
  65. if (id is Asn1OctetString)
  66. {
  67. return null;
  68. }
  69. return X509Name.GetInstance(id);
  70. }
  71. }
  72. /**
  73. * Produce an object suitable for an Asn1OutputStream.
  74. * <pre>
  75. * ResponderID ::= CHOICE {
  76. * byName [1] Name,
  77. * byKey [2] KeyHash }
  78. * </pre>
  79. */
  80. public override Asn1Object ToAsn1Object()
  81. {
  82. if (id is Asn1OctetString)
  83. {
  84. return new DerTaggedObject(true, 2, id);
  85. }
  86. return new DerTaggedObject(true, 1, id);
  87. }
  88. }
  89. }
  90. #endif