DerBoolean.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  6. {
  7. public class DerBoolean
  8. : Asn1Object
  9. {
  10. private readonly byte value;
  11. public static readonly DerBoolean False = new DerBoolean(false);
  12. public static readonly DerBoolean True = new DerBoolean(true);
  13. /**
  14. * return a bool from the passed in object.
  15. *
  16. * @exception ArgumentException if the object cannot be converted.
  17. */
  18. public static DerBoolean GetInstance(
  19. object obj)
  20. {
  21. if (obj == null || obj is DerBoolean)
  22. {
  23. return (DerBoolean) obj;
  24. }
  25. throw new ArgumentException("illegal object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  26. }
  27. /**
  28. * return a DerBoolean from the passed in bool.
  29. */
  30. public static DerBoolean GetInstance(
  31. bool value)
  32. {
  33. return value ? True : False;
  34. }
  35. /**
  36. * return a Boolean from a tagged object.
  37. *
  38. * @param obj the tagged object holding the object we want
  39. * @param explicitly true if the object is meant to be explicitly
  40. * tagged false otherwise.
  41. * @exception ArgumentException if the tagged object cannot
  42. * be converted.
  43. */
  44. public static DerBoolean GetInstance(
  45. Asn1TaggedObject obj,
  46. bool isExplicit)
  47. {
  48. Asn1Object o = obj.GetObject();
  49. if (isExplicit || o is DerBoolean)
  50. {
  51. return GetInstance(o);
  52. }
  53. return FromOctetString(((Asn1OctetString)o).GetOctets());
  54. }
  55. public DerBoolean(
  56. byte[] val)
  57. {
  58. if (val.Length != 1)
  59. throw new ArgumentException("byte value should have 1 byte in it", "val");
  60. // TODO Are there any constraints on the possible byte values?
  61. this.value = val[0];
  62. }
  63. private DerBoolean(
  64. bool value)
  65. {
  66. this.value = value ? (byte)0xff : (byte)0;
  67. }
  68. public bool IsTrue
  69. {
  70. get { return value != 0; }
  71. }
  72. internal override void Encode(
  73. DerOutputStream derOut)
  74. {
  75. // TODO Should we make sure the byte value is one of '0' or '0xff' here?
  76. derOut.WriteEncoded(Asn1Tags.Boolean, new byte[]{ value });
  77. }
  78. protected override bool Asn1Equals(
  79. Asn1Object asn1Object)
  80. {
  81. DerBoolean other = asn1Object as DerBoolean;
  82. if (other == null)
  83. return false;
  84. return IsTrue == other.IsTrue;
  85. }
  86. protected override int Asn1GetHashCode()
  87. {
  88. return IsTrue.GetHashCode();
  89. }
  90. public override string ToString()
  91. {
  92. return IsTrue ? "TRUE" : "FALSE";
  93. }
  94. internal static DerBoolean FromOctetString(byte[] value)
  95. {
  96. if (value.Length != 1)
  97. {
  98. throw new ArgumentException("BOOLEAN value should have 1 byte in it", "value");
  99. }
  100. byte b = value[0];
  101. return b == 0 ? False : b == 0xFF ? True : new DerBoolean(value);
  102. }
  103. }
  104. }
  105. #pragma warning restore
  106. #endif