DerBoolean.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1
  7. {
  8. public class DerBoolean
  9. : Asn1Object
  10. {
  11. internal class Meta : Asn1UniversalType
  12. {
  13. internal static readonly Asn1UniversalType Instance = new Meta();
  14. private Meta() : base(typeof(DerBoolean), Asn1Tags.Boolean) {}
  15. internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
  16. {
  17. return CreatePrimitive(octetString.GetOctets());
  18. }
  19. }
  20. public static readonly DerBoolean False = new DerBoolean(false);
  21. public static readonly DerBoolean True = new DerBoolean(true);
  22. /**
  23. * return a bool from the passed in object.
  24. *
  25. * @exception ArgumentException if the object cannot be converted.
  26. */
  27. public static DerBoolean GetInstance(object obj)
  28. {
  29. if (obj == null)
  30. return null;
  31. if (obj is DerBoolean derBoolean)
  32. return derBoolean;
  33. if (obj is IAsn1Convertible asn1Convertible)
  34. {
  35. Asn1Object asn1Object = asn1Convertible.ToAsn1Object();
  36. if (asn1Object is DerBoolean converted)
  37. return converted;
  38. }
  39. else if (obj is byte[] bytes)
  40. {
  41. try
  42. {
  43. return (DerBoolean)Meta.Instance.FromByteArray(bytes);
  44. }
  45. catch (IOException e)
  46. {
  47. throw new ArgumentException("failed to construct boolean from byte[]: " + e.Message);
  48. }
  49. }
  50. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  51. }
  52. public static DerBoolean GetInstance(bool value)
  53. {
  54. return value ? True : False;
  55. }
  56. public static DerBoolean GetInstance(int value)
  57. {
  58. return value != 0 ? True : False;
  59. }
  60. /**
  61. * return a Boolean from a tagged object.
  62. *
  63. * @param taggedObject the tagged object holding the object we want
  64. * @param declaredExplicit true if the object is meant to be explicitly tagged false otherwise.
  65. * @exception ArgumentException if the tagged object cannot be converted.
  66. */
  67. public static DerBoolean GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
  68. {
  69. return (DerBoolean)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
  70. }
  71. private readonly byte value;
  72. public DerBoolean(
  73. byte[] val)
  74. {
  75. if (val.Length != 1)
  76. throw new ArgumentException("byte value should have 1 byte in it", "val");
  77. // TODO Are there any constraints on the possible byte values?
  78. this.value = val[0];
  79. }
  80. private DerBoolean(
  81. bool value)
  82. {
  83. this.value = value ? (byte)0xff : (byte)0;
  84. }
  85. public bool IsTrue
  86. {
  87. get { return value != 0; }
  88. }
  89. internal override IAsn1Encoding GetEncoding(int encoding)
  90. {
  91. return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.Boolean, GetContents(encoding));
  92. }
  93. internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
  94. {
  95. return new PrimitiveEncoding(tagClass, tagNo, GetContents(encoding));
  96. }
  97. protected override bool Asn1Equals(
  98. Asn1Object asn1Object)
  99. {
  100. DerBoolean other = asn1Object as DerBoolean;
  101. if (other == null)
  102. return false;
  103. return IsTrue == other.IsTrue;
  104. }
  105. protected override int Asn1GetHashCode()
  106. {
  107. return IsTrue.GetHashCode();
  108. }
  109. public override string ToString()
  110. {
  111. return IsTrue ? "TRUE" : "FALSE";
  112. }
  113. internal static DerBoolean CreatePrimitive(byte[] contents)
  114. {
  115. if (contents.Length != 1)
  116. throw new ArgumentException("BOOLEAN value should have 1 byte in it", "contents");
  117. byte b = contents[0];
  118. return b == 0 ? False : b == 0xFF ? True : new DerBoolean(contents);
  119. }
  120. private byte[] GetContents(int encoding)
  121. {
  122. byte contents = value;
  123. if (Asn1OutputStream.EncodingDer == encoding && IsTrue)
  124. {
  125. contents = 0xFF;
  126. }
  127. return new byte[]{ contents };
  128. }
  129. }
  130. }
  131. #pragma warning restore
  132. #endif