DerEnumerated.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.Math;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1
  8. {
  9. public class DerEnumerated
  10. : Asn1Object
  11. {
  12. internal class Meta : Asn1UniversalType
  13. {
  14. internal static readonly Asn1UniversalType Instance = new Meta();
  15. private Meta() : base(typeof(DerEnumerated), Asn1Tags.Enumerated) {}
  16. internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
  17. {
  18. return CreatePrimitive(octetString.GetOctets(), false);
  19. }
  20. }
  21. /**
  22. * return an integer from the passed in object
  23. *
  24. * @exception ArgumentException if the object cannot be converted.
  25. */
  26. public static DerEnumerated GetInstance(object obj)
  27. {
  28. if (obj == null)
  29. return null;
  30. if (obj is DerEnumerated derEnumerated)
  31. return derEnumerated;
  32. if (obj is IAsn1Convertible asn1Convertible)
  33. {
  34. Asn1Object asn1Object = asn1Convertible.ToAsn1Object();
  35. if (asn1Object is DerEnumerated converted)
  36. return converted;
  37. }
  38. else if (obj is byte[] bytes)
  39. {
  40. try
  41. {
  42. return (DerEnumerated)Meta.Instance.FromByteArray(bytes);
  43. }
  44. catch (IOException e)
  45. {
  46. throw new ArgumentException("failed to construct enumerated from byte[]: " + e.Message);
  47. }
  48. }
  49. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  50. }
  51. /**
  52. * return an Enumerated from a tagged object.
  53. *
  54. * @param taggedObject the tagged object holding the object we want
  55. * @param declaredExplicit true if the object is meant to be explicitly tagged false otherwise.
  56. * @exception ArgumentException if the tagged object cannot be converted.
  57. */
  58. public static DerEnumerated GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
  59. {
  60. return (DerEnumerated)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
  61. }
  62. private readonly byte[] contents;
  63. private readonly int start;
  64. public DerEnumerated(int val)
  65. {
  66. if (val < 0)
  67. throw new ArgumentException("enumerated must be non-negative", "val");
  68. this.contents = BigInteger.ValueOf(val).ToByteArray();
  69. this.start = 0;
  70. }
  71. public DerEnumerated(long val)
  72. {
  73. if (val < 0L)
  74. throw new ArgumentException("enumerated must be non-negative", "val");
  75. this.contents = BigInteger.ValueOf(val).ToByteArray();
  76. this.start = 0;
  77. }
  78. public DerEnumerated(BigInteger val)
  79. {
  80. if (val.SignValue < 0)
  81. throw new ArgumentException("enumerated must be non-negative", "val");
  82. this.contents = val.ToByteArray();
  83. this.start = 0;
  84. }
  85. public DerEnumerated(byte[] contents)
  86. : this(contents, true)
  87. {
  88. }
  89. internal DerEnumerated(byte[] contents, bool clone)
  90. {
  91. if (DerInteger.IsMalformed(contents))
  92. throw new ArgumentException("malformed enumerated", "contents");
  93. if (0 != (contents[0] & 0x80))
  94. throw new ArgumentException("enumerated must be non-negative", "contents");
  95. this.contents = clone ? Arrays.Clone(contents) : contents;
  96. this.start = DerInteger.SignBytesToSkip(this.contents);
  97. }
  98. public BigInteger Value
  99. {
  100. get { return new BigInteger(contents); }
  101. }
  102. public bool HasValue(int x)
  103. {
  104. return (contents.Length - start) <= 4
  105. && DerInteger.IntValue(contents, start, DerInteger.SignExtSigned) == x;
  106. }
  107. public bool HasValue(BigInteger x)
  108. {
  109. return null != x
  110. // Fast check to avoid allocation
  111. && DerInteger.IntValue(contents, start, DerInteger.SignExtSigned) == x.IntValue
  112. && Value.Equals(x);
  113. }
  114. public int IntValueExact
  115. {
  116. get
  117. {
  118. int count = contents.Length - start;
  119. if (count > 4)
  120. throw new ArithmeticException("ASN.1 Enumerated out of int range");
  121. return DerInteger.IntValue(contents, start, DerInteger.SignExtSigned);
  122. }
  123. }
  124. internal override IAsn1Encoding GetEncoding(int encoding)
  125. {
  126. return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.Enumerated, contents);
  127. }
  128. internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
  129. {
  130. return new PrimitiveEncoding(tagClass, tagNo, contents);
  131. }
  132. protected override bool Asn1Equals(Asn1Object asn1Object)
  133. {
  134. DerEnumerated other = asn1Object as DerEnumerated;
  135. if (other == null)
  136. return false;
  137. return Arrays.AreEqual(this.contents, other.contents);
  138. }
  139. protected override int Asn1GetHashCode()
  140. {
  141. return Arrays.GetHashCode(contents);
  142. }
  143. private static readonly DerEnumerated[] cache = new DerEnumerated[12];
  144. internal static DerEnumerated CreatePrimitive(byte[] contents, bool clone)
  145. {
  146. if (contents.Length > 1)
  147. return new DerEnumerated(contents, clone);
  148. if (contents.Length == 0)
  149. throw new ArgumentException("ENUMERATED has zero length", "contents");
  150. int value = contents[0];
  151. if (value >= cache.Length)
  152. return new DerEnumerated(contents, clone);
  153. DerEnumerated possibleMatch = cache[value];
  154. if (possibleMatch == null)
  155. {
  156. cache[value] = possibleMatch = new DerEnumerated(contents, clone);
  157. }
  158. return possibleMatch;
  159. }
  160. }
  161. }
  162. #pragma warning restore
  163. #endif