DerEnumerated.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  7. {
  8. public class DerEnumerated
  9. : Asn1Object
  10. {
  11. private readonly byte[] bytes;
  12. /**
  13. * return an integer from the passed in object
  14. *
  15. * @exception ArgumentException if the object cannot be converted.
  16. */
  17. public static DerEnumerated GetInstance(
  18. object obj)
  19. {
  20. if (obj == null || obj is DerEnumerated)
  21. {
  22. return (DerEnumerated)obj;
  23. }
  24. throw new ArgumentException("illegal object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  25. }
  26. /**
  27. * return an Enumerated from a tagged object.
  28. *
  29. * @param obj the tagged object holding the object we want
  30. * @param explicitly true if the object is meant to be explicitly
  31. * tagged false otherwise.
  32. * @exception ArgumentException if the tagged object cannot
  33. * be converted.
  34. */
  35. public static DerEnumerated GetInstance(
  36. Asn1TaggedObject obj,
  37. bool isExplicit)
  38. {
  39. Asn1Object o = obj.GetObject();
  40. if (isExplicit || o is DerEnumerated)
  41. {
  42. return GetInstance(o);
  43. }
  44. return FromOctetString(((Asn1OctetString)o).GetOctets());
  45. }
  46. public DerEnumerated(
  47. int val)
  48. {
  49. bytes = BigInteger.ValueOf(val).ToByteArray();
  50. }
  51. public DerEnumerated(
  52. BigInteger val)
  53. {
  54. bytes = val.ToByteArray();
  55. }
  56. public DerEnumerated(
  57. byte[] bytes)
  58. {
  59. if (bytes.Length > 1)
  60. {
  61. if ((bytes[0] == 0 && (bytes[1] & 0x80) == 0)
  62. || (bytes[0] == (byte)0xff && (bytes[1] & 0x80) != 0))
  63. {
  64. if (!DerInteger.AllowUnsafe())
  65. throw new ArgumentException("malformed enumerated");
  66. }
  67. }
  68. this.bytes = Arrays.Clone(bytes);
  69. }
  70. public BigInteger Value
  71. {
  72. get { return new BigInteger(bytes); }
  73. }
  74. internal override void Encode(
  75. DerOutputStream derOut)
  76. {
  77. derOut.WriteEncoded(Asn1Tags.Enumerated, bytes);
  78. }
  79. protected override bool Asn1Equals(
  80. Asn1Object asn1Object)
  81. {
  82. DerEnumerated other = asn1Object as DerEnumerated;
  83. if (other == null)
  84. return false;
  85. return Arrays.AreEqual(this.bytes, other.bytes);
  86. }
  87. protected override int Asn1GetHashCode()
  88. {
  89. return Arrays.GetHashCode(bytes);
  90. }
  91. private static readonly DerEnumerated[] cache = new DerEnumerated[12];
  92. internal static DerEnumerated FromOctetString(byte[] enc)
  93. {
  94. if (enc.Length == 0)
  95. {
  96. throw new ArgumentException("ENUMERATED has zero length", "enc");
  97. }
  98. if (enc.Length == 1)
  99. {
  100. int value = enc[0];
  101. if (value < cache.Length)
  102. {
  103. DerEnumerated cached = cache[value];
  104. if (cached != null)
  105. {
  106. return cached;
  107. }
  108. return cache[value] = new DerEnumerated(Arrays.Clone(enc));
  109. }
  110. }
  111. return new DerEnumerated(Arrays.Clone(enc));
  112. }
  113. }
  114. }
  115. #pragma warning restore
  116. #endif