DerBoolean.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 int EncodedLength(bool withID)
  73. {
  74. return Asn1OutputStream.GetLengthOfEncodingDL(withID, 1);
  75. }
  76. internal override void Encode(Asn1OutputStream asn1Out, bool withID)
  77. {
  78. // TODO Should we make sure the byte value is one of '0' or '0xff' here?
  79. asn1Out.WriteEncodingDL(withID, Asn1Tags.Boolean, value);
  80. }
  81. protected override bool Asn1Equals(
  82. Asn1Object asn1Object)
  83. {
  84. DerBoolean other = asn1Object as DerBoolean;
  85. if (other == null)
  86. return false;
  87. return IsTrue == other.IsTrue;
  88. }
  89. protected override int Asn1GetHashCode()
  90. {
  91. return IsTrue.GetHashCode();
  92. }
  93. public override string ToString()
  94. {
  95. return IsTrue ? "TRUE" : "FALSE";
  96. }
  97. internal static DerBoolean FromOctetString(byte[] value)
  98. {
  99. if (value.Length != 1)
  100. {
  101. throw new ArgumentException("BOOLEAN value should have 1 byte in it", "value");
  102. }
  103. byte b = value[0];
  104. return b == 0 ? False : b == 0xFF ? True : new DerBoolean(value);
  105. }
  106. }
  107. }
  108. #pragma warning restore
  109. #endif