DerInteger.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 DerInteger
  9. : Asn1Object
  10. {
  11. public const string AllowUnsafeProperty = "BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.AllowUnsafeInteger";
  12. internal static bool AllowUnsafe()
  13. {
  14. string allowUnsafeValue = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetEnvironmentVariable(AllowUnsafeProperty);
  15. return allowUnsafeValue != null && BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase("true", allowUnsafeValue);
  16. }
  17. private readonly byte[] bytes;
  18. /**
  19. * return an integer from the passed in object
  20. *
  21. * @exception ArgumentException if the object cannot be converted.
  22. */
  23. public static DerInteger GetInstance(
  24. object obj)
  25. {
  26. if (obj == null || obj is DerInteger)
  27. {
  28. return (DerInteger)obj;
  29. }
  30. throw new ArgumentException("illegal object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  31. }
  32. /**
  33. * return an Integer from a tagged object.
  34. *
  35. * @param obj the tagged object holding the object we want
  36. * @param isExplicit true if the object is meant to be explicitly
  37. * tagged false otherwise.
  38. * @exception ArgumentException if the tagged object cannot
  39. * be converted.
  40. */
  41. public static DerInteger GetInstance(
  42. Asn1TaggedObject obj,
  43. bool isExplicit)
  44. {
  45. if (obj == null)
  46. throw new ArgumentNullException("obj");
  47. Asn1Object o = obj.GetObject();
  48. if (isExplicit || o is DerInteger)
  49. {
  50. return GetInstance(o);
  51. }
  52. return new DerInteger(Asn1OctetString.GetInstance(o).GetOctets());
  53. }
  54. public DerInteger(
  55. int value)
  56. {
  57. bytes = BigInteger.ValueOf(value).ToByteArray();
  58. }
  59. public DerInteger(
  60. BigInteger value)
  61. {
  62. if (value == null)
  63. throw new ArgumentNullException("value");
  64. bytes = value.ToByteArray();
  65. }
  66. public DerInteger(
  67. byte[] bytes)
  68. {
  69. if (bytes.Length > 1)
  70. {
  71. if ((bytes[0] == 0 && (bytes[1] & 0x80) == 0)
  72. || (bytes[0] == (byte)0xff && (bytes[1] & 0x80) != 0))
  73. {
  74. if (!AllowUnsafe())
  75. throw new ArgumentException("malformed integer");
  76. }
  77. }
  78. this.bytes = Arrays.Clone(bytes);
  79. }
  80. public BigInteger Value
  81. {
  82. get { return new BigInteger(bytes); }
  83. }
  84. /**
  85. * in some cases positive values Get crammed into a space,
  86. * that's not quite big enough...
  87. */
  88. public BigInteger PositiveValue
  89. {
  90. get { return new BigInteger(1, bytes); }
  91. }
  92. internal override void Encode(
  93. DerOutputStream derOut)
  94. {
  95. derOut.WriteEncoded(Asn1Tags.Integer, bytes);
  96. }
  97. protected override int Asn1GetHashCode()
  98. {
  99. return Arrays.GetHashCode(bytes);
  100. }
  101. protected override bool Asn1Equals(
  102. Asn1Object asn1Object)
  103. {
  104. DerInteger other = asn1Object as DerInteger;
  105. if (other == null)
  106. return false;
  107. return Arrays.AreEqual(this.bytes, other.bytes);
  108. }
  109. public override string ToString()
  110. {
  111. return Value.ToString();
  112. }
  113. }
  114. }
  115. #pragma warning restore
  116. #endif