DerEnumerated.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. private readonly int start;
  13. /**
  14. * return an integer from the passed in object
  15. *
  16. * @exception ArgumentException if the object cannot be converted.
  17. */
  18. public static DerEnumerated GetInstance(
  19. object obj)
  20. {
  21. if (obj == null || obj is DerEnumerated)
  22. {
  23. return (DerEnumerated)obj;
  24. }
  25. throw new ArgumentException("illegal object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  26. }
  27. /**
  28. * return an Enumerated from a tagged object.
  29. *
  30. * @param obj the tagged object holding the object we want
  31. * @param explicitly true if the object is meant to be explicitly
  32. * tagged false otherwise.
  33. * @exception ArgumentException if the tagged object cannot
  34. * be converted.
  35. */
  36. public static DerEnumerated GetInstance(
  37. Asn1TaggedObject obj,
  38. bool isExplicit)
  39. {
  40. Asn1Object o = obj.GetObject();
  41. if (isExplicit || o is DerEnumerated)
  42. {
  43. return GetInstance(o);
  44. }
  45. return FromOctetString(((Asn1OctetString)o).GetOctets());
  46. }
  47. public DerEnumerated(int val)
  48. {
  49. if (val < 0)
  50. throw new ArgumentException("enumerated must be non-negative", "val");
  51. this.bytes = BigInteger.ValueOf(val).ToByteArray();
  52. this.start = 0;
  53. }
  54. public DerEnumerated(long val)
  55. {
  56. if (val < 0L)
  57. throw new ArgumentException("enumerated must be non-negative", "val");
  58. this.bytes = BigInteger.ValueOf(val).ToByteArray();
  59. this.start = 0;
  60. }
  61. public DerEnumerated(BigInteger val)
  62. {
  63. if (val.SignValue < 0)
  64. throw new ArgumentException("enumerated must be non-negative", "val");
  65. this.bytes = val.ToByteArray();
  66. this.start = 0;
  67. }
  68. public DerEnumerated(byte[] bytes)
  69. {
  70. if (DerInteger.IsMalformed(bytes))
  71. throw new ArgumentException("malformed enumerated", "bytes");
  72. if (0 != (bytes[0] & 0x80))
  73. throw new ArgumentException("enumerated must be non-negative", "bytes");
  74. this.bytes = Arrays.Clone(bytes);
  75. this.start = DerInteger.SignBytesToSkip(bytes);
  76. }
  77. public BigInteger Value
  78. {
  79. get { return new BigInteger(bytes); }
  80. }
  81. public bool HasValue(int x)
  82. {
  83. return (bytes.Length - start) <= 4
  84. && DerInteger.IntValue(bytes, start, DerInteger.SignExtSigned) == x;
  85. }
  86. public bool HasValue(BigInteger x)
  87. {
  88. return null != x
  89. // Fast check to avoid allocation
  90. && DerInteger.IntValue(bytes, start, DerInteger.SignExtSigned) == x.IntValue
  91. && Value.Equals(x);
  92. }
  93. public int IntValueExact
  94. {
  95. get
  96. {
  97. int count = bytes.Length - start;
  98. if (count > 4)
  99. throw new ArithmeticException("ASN.1 Enumerated out of int range");
  100. return DerInteger.IntValue(bytes, start, DerInteger.SignExtSigned);
  101. }
  102. }
  103. internal override int EncodedLength(bool withID)
  104. {
  105. return Asn1OutputStream.GetLengthOfEncodingDL(withID, bytes.Length);
  106. }
  107. internal override void Encode(Asn1OutputStream asn1Out, bool withID)
  108. {
  109. asn1Out.WriteEncodingDL(withID, Asn1Tags.Enumerated, bytes);
  110. }
  111. protected override bool Asn1Equals(Asn1Object asn1Object)
  112. {
  113. DerEnumerated other = asn1Object as DerEnumerated;
  114. if (other == null)
  115. return false;
  116. return Arrays.AreEqual(this.bytes, other.bytes);
  117. }
  118. protected override int Asn1GetHashCode()
  119. {
  120. return Arrays.GetHashCode(bytes);
  121. }
  122. private static readonly DerEnumerated[] cache = new DerEnumerated[12];
  123. internal static DerEnumerated FromOctetString(byte[] enc)
  124. {
  125. if (enc.Length > 1)
  126. return new DerEnumerated(enc);
  127. if (enc.Length == 0)
  128. throw new ArgumentException("ENUMERATED has zero length", "enc");
  129. int value = enc[0];
  130. if (value >= cache.Length)
  131. return new DerEnumerated(enc);
  132. DerEnumerated possibleMatch = cache[value];
  133. if (possibleMatch == null)
  134. {
  135. cache[value] = possibleMatch = new DerEnumerated(enc);
  136. }
  137. return possibleMatch;
  138. }
  139. }
  140. }
  141. #pragma warning restore
  142. #endif