DerInteger.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 DerInteger
  10. : Asn1Object
  11. {
  12. internal class Meta : Asn1UniversalType
  13. {
  14. internal static readonly Asn1UniversalType Instance = new Meta();
  15. private Meta() : base(typeof(DerInteger), Asn1Tags.Integer) {}
  16. internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
  17. {
  18. return CreatePrimitive(octetString.GetOctets());
  19. }
  20. }
  21. public const string AllowUnsafeProperty = "Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.AllowUnsafeInteger";
  22. internal static bool AllowUnsafe()
  23. {
  24. string allowUnsafeValue = Org.BouncyCastle.Utilities.Platform.GetEnvironmentVariable(AllowUnsafeProperty);
  25. return allowUnsafeValue != null && Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase("true", allowUnsafeValue);
  26. }
  27. internal const int SignExtSigned = -1;
  28. internal const int SignExtUnsigned = 0xFF;
  29. private readonly byte[] bytes;
  30. private readonly int start;
  31. /**
  32. * return an integer from the passed in object
  33. *
  34. * @exception ArgumentException if the object cannot be converted.
  35. */
  36. public static DerInteger GetInstance(object obj)
  37. {
  38. if (obj == null)
  39. return null;
  40. if (obj is DerInteger derInteger)
  41. return derInteger;
  42. if (obj is IAsn1Convertible asn1Convertible)
  43. {
  44. Asn1Object asn1Object = asn1Convertible.ToAsn1Object();
  45. if (asn1Object is DerInteger converted)
  46. return converted;
  47. }
  48. else if (obj is byte[] bytes)
  49. {
  50. try
  51. {
  52. return (DerInteger)Meta.Instance.FromByteArray(bytes);
  53. }
  54. catch (IOException e)
  55. {
  56. throw new ArgumentException("failed to construct integer from byte[]: " + e.Message);
  57. }
  58. }
  59. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  60. }
  61. /**
  62. * return an Integer from a tagged object.
  63. *
  64. * @param taggedObject the tagged object holding the object we want
  65. * @param declaredExplicit true if the object is meant to be explicitly tagged false otherwise.
  66. * @exception ArgumentException if the tagged object cannot be converted.
  67. */
  68. public static DerInteger GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
  69. {
  70. return (DerInteger)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
  71. }
  72. public DerInteger(int value)
  73. {
  74. this.bytes = BigInteger.ValueOf(value).ToByteArray();
  75. this.start = 0;
  76. }
  77. public DerInteger(long value)
  78. {
  79. this.bytes = BigInteger.ValueOf(value).ToByteArray();
  80. this.start = 0;
  81. }
  82. public DerInteger(BigInteger value)
  83. {
  84. if (value == null)
  85. throw new ArgumentNullException("value");
  86. this.bytes = value.ToByteArray();
  87. this.start = 0;
  88. }
  89. public DerInteger(byte[] bytes)
  90. : this(bytes, true)
  91. {
  92. }
  93. internal DerInteger(byte[] bytes, bool clone)
  94. {
  95. if (IsMalformed(bytes))
  96. throw new ArgumentException("malformed integer", "bytes");
  97. this.bytes = clone ? Arrays.Clone(bytes) : bytes;
  98. this.start = SignBytesToSkip(bytes);
  99. }
  100. /**
  101. * in some cases positive values Get crammed into a space,
  102. * that's not quite big enough...
  103. */
  104. public BigInteger PositiveValue
  105. {
  106. get { return new BigInteger(1, bytes); }
  107. }
  108. public BigInteger Value
  109. {
  110. get { return new BigInteger(bytes); }
  111. }
  112. public bool HasValue(int x)
  113. {
  114. return (bytes.Length - start) <= 4
  115. && IntValue(bytes, start, SignExtSigned) == x;
  116. }
  117. public bool HasValue(long x)
  118. {
  119. return (bytes.Length - start) <= 8
  120. && LongValue(bytes, start, SignExtSigned) == x;
  121. }
  122. public bool HasValue(BigInteger x)
  123. {
  124. return null != x
  125. // Fast check to avoid allocation
  126. && IntValue(bytes, start, SignExtSigned) == x.IntValue
  127. && Value.Equals(x);
  128. }
  129. public int IntPositiveValueExact
  130. {
  131. get
  132. {
  133. int count = bytes.Length - start;
  134. if (count > 4 || (count == 4 && 0 != (bytes[start] & 0x80)))
  135. throw new ArithmeticException("ASN.1 Integer out of positive int range");
  136. return IntValue(bytes, start, SignExtUnsigned);
  137. }
  138. }
  139. public int IntValueExact
  140. {
  141. get
  142. {
  143. int count = bytes.Length - start;
  144. if (count > 4)
  145. throw new ArithmeticException("ASN.1 Integer out of int range");
  146. return IntValue(bytes, start, SignExtSigned);
  147. }
  148. }
  149. public long LongValueExact
  150. {
  151. get
  152. {
  153. int count = bytes.Length - start;
  154. if (count > 8)
  155. throw new ArithmeticException("ASN.1 Integer out of long range");
  156. return LongValue(bytes, start, SignExtSigned);
  157. }
  158. }
  159. internal override IAsn1Encoding GetEncoding(int encoding)
  160. {
  161. return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.Integer, bytes);
  162. }
  163. internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
  164. {
  165. return new PrimitiveEncoding(tagClass, tagNo, bytes);
  166. }
  167. protected override int Asn1GetHashCode()
  168. {
  169. return Arrays.GetHashCode(bytes);
  170. }
  171. protected override bool Asn1Equals(Asn1Object asn1Object)
  172. {
  173. DerInteger other = asn1Object as DerInteger;
  174. if (other == null)
  175. return false;
  176. return Arrays.AreEqual(this.bytes, other.bytes);
  177. }
  178. public override string ToString()
  179. {
  180. return Value.ToString();
  181. }
  182. internal static DerInteger CreatePrimitive(byte[] contents)
  183. {
  184. return new DerInteger(contents, false);
  185. }
  186. internal static int IntValue(byte[] bytes, int start, int signExt)
  187. {
  188. int length = bytes.Length;
  189. int pos = System.Math.Max(start, length - 4);
  190. int val = (sbyte)bytes[pos] & signExt;
  191. while (++pos < length)
  192. {
  193. val = (val << 8) | bytes[pos];
  194. }
  195. return val;
  196. }
  197. internal static long LongValue(byte[] bytes, int start, int signExt)
  198. {
  199. int length = bytes.Length;
  200. int pos = System.Math.Max(start, length - 8);
  201. long val = (sbyte)bytes[pos] & signExt;
  202. while (++pos < length)
  203. {
  204. val = (val << 8) | bytes[pos];
  205. }
  206. return val;
  207. }
  208. /**
  209. * Apply the correct validation for an INTEGER primitive following the BER rules.
  210. *
  211. * @param bytes The raw encoding of the integer.
  212. * @return true if the (in)put fails this validation.
  213. */
  214. internal static bool IsMalformed(byte[] bytes)
  215. {
  216. switch (bytes.Length)
  217. {
  218. case 0:
  219. return true;
  220. case 1:
  221. return false;
  222. default:
  223. return (sbyte)bytes[0] == ((sbyte)bytes[1] >> 7) && !AllowUnsafe();
  224. }
  225. }
  226. internal static int SignBytesToSkip(byte[] bytes)
  227. {
  228. int pos = 0, last = bytes.Length - 1;
  229. while (pos < last
  230. && (sbyte)bytes[pos] == ((sbyte)bytes[pos + 1] >> 7))
  231. {
  232. ++pos;
  233. }
  234. return pos;
  235. }
  236. }
  237. }
  238. #pragma warning restore
  239. #endif