DerBitString.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. using System.Text;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  9. {
  10. public class DerBitString
  11. : DerStringBase
  12. {
  13. private static readonly char[] table
  14. = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  15. protected readonly byte[] mData;
  16. protected readonly int mPadBits;
  17. /**
  18. * return a Bit string from the passed in object
  19. *
  20. * @exception ArgumentException if the object cannot be converted.
  21. */
  22. public static DerBitString GetInstance(
  23. object obj)
  24. {
  25. if (obj == null || obj is DerBitString)
  26. {
  27. return (DerBitString) obj;
  28. }
  29. if (obj is byte[])
  30. {
  31. try
  32. {
  33. return (DerBitString)FromByteArray((byte[])obj);
  34. }
  35. catch (Exception e)
  36. {
  37. throw new ArgumentException("encoding error in GetInstance: " + e.ToString());
  38. }
  39. }
  40. throw new ArgumentException("illegal object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  41. }
  42. /**
  43. * return a Bit string from a tagged object.
  44. *
  45. * @param obj the tagged object holding the object we want
  46. * @param explicitly true if the object is meant to be explicitly
  47. * tagged false otherwise.
  48. * @exception ArgumentException if the tagged object cannot
  49. * be converted.
  50. */
  51. public static DerBitString GetInstance(
  52. Asn1TaggedObject obj,
  53. bool isExplicit)
  54. {
  55. Asn1Object o = obj.GetObject();
  56. if (isExplicit || o is DerBitString)
  57. {
  58. return GetInstance(o);
  59. }
  60. return FromAsn1Octets(((Asn1OctetString)o).GetOctets());
  61. }
  62. /**
  63. * @param data the octets making up the bit string.
  64. * @param padBits the number of extra bits at the end of the string.
  65. */
  66. public DerBitString(
  67. byte[] data,
  68. int padBits)
  69. {
  70. if (data == null)
  71. throw new ArgumentNullException("data");
  72. if (padBits < 0 || padBits > 7)
  73. throw new ArgumentException("must be in the range 0 to 7", "padBits");
  74. if (data.Length == 0 && padBits != 0)
  75. throw new ArgumentException("if 'data' is empty, 'padBits' must be 0");
  76. this.mData = Arrays.Clone(data);
  77. this.mPadBits = padBits;
  78. }
  79. public DerBitString(
  80. byte[] data)
  81. : this(data, 0)
  82. {
  83. }
  84. public DerBitString(
  85. int namedBits)
  86. {
  87. if (namedBits == 0)
  88. {
  89. this.mData = new byte[0];
  90. this.mPadBits = 0;
  91. return;
  92. }
  93. int bits = BigInteger.BitLen(namedBits);
  94. int bytes = (bits + 7) / 8;
  95. Debug.Assert(0 < bytes && bytes <= 4);
  96. byte[] data = new byte[bytes];
  97. --bytes;
  98. for (int i = 0; i < bytes; i++)
  99. {
  100. data[i] = (byte)namedBits;
  101. namedBits >>= 8;
  102. }
  103. Debug.Assert((namedBits & 0xFF) != 0);
  104. data[bytes] = (byte)namedBits;
  105. int padBits = 0;
  106. while ((namedBits & (1 << padBits)) == 0)
  107. {
  108. ++padBits;
  109. }
  110. Debug.Assert(padBits < 8);
  111. this.mData = data;
  112. this.mPadBits = padBits;
  113. }
  114. public DerBitString(
  115. Asn1Encodable obj)
  116. : this(obj.GetDerEncoded())
  117. {
  118. }
  119. /**
  120. * Return the octets contained in this BIT STRING, checking that this BIT STRING really
  121. * does represent an octet aligned string. Only use this method when the standard you are
  122. * following dictates that the BIT STRING will be octet aligned.
  123. *
  124. * @return a copy of the octet aligned data.
  125. */
  126. public virtual byte[] GetOctets()
  127. {
  128. if (mPadBits != 0)
  129. throw new InvalidOperationException("attempt to get non-octet aligned data from BIT STRING");
  130. return Arrays.Clone(mData);
  131. }
  132. public virtual byte[] GetBytes()
  133. {
  134. byte[] data = Arrays.Clone(mData);
  135. // DER requires pad bits be zero
  136. if (mPadBits > 0)
  137. {
  138. data[data.Length - 1] &= (byte)(0xFF << mPadBits);
  139. }
  140. return data;
  141. }
  142. public virtual int PadBits
  143. {
  144. get { return mPadBits; }
  145. }
  146. /**
  147. * @return the value of the bit string as an int (truncating if necessary)
  148. */
  149. public virtual int IntValue
  150. {
  151. get
  152. {
  153. int value = 0, length = System.Math.Min(4, mData.Length);
  154. for (int i = 0; i < length; ++i)
  155. {
  156. value |= (int)mData[i] << (8 * i);
  157. }
  158. if (mPadBits > 0 && length == mData.Length)
  159. {
  160. int mask = (1 << mPadBits) - 1;
  161. value &= ~(mask << (8 * (length - 1)));
  162. }
  163. return value;
  164. }
  165. }
  166. internal override void Encode(
  167. DerOutputStream derOut)
  168. {
  169. if (mPadBits > 0)
  170. {
  171. int last = mData[mData.Length - 1];
  172. int mask = (1 << mPadBits) - 1;
  173. int unusedBits = last & mask;
  174. if (unusedBits != 0)
  175. {
  176. byte[] contents = Arrays.Prepend(mData, (byte)mPadBits);
  177. /*
  178. * X.690-0207 11.2.1: Each unused bit in the final octet of the encoding of a bit string value shall be set to zero.
  179. */
  180. contents[contents.Length - 1] = (byte)(last ^ unusedBits);
  181. derOut.WriteEncoded(Asn1Tags.BitString, contents);
  182. return;
  183. }
  184. }
  185. derOut.WriteEncoded(Asn1Tags.BitString, (byte)mPadBits, mData);
  186. }
  187. protected override int Asn1GetHashCode()
  188. {
  189. return mPadBits.GetHashCode() ^ Arrays.GetHashCode(mData);
  190. }
  191. protected override bool Asn1Equals(
  192. Asn1Object asn1Object)
  193. {
  194. DerBitString other = asn1Object as DerBitString;
  195. if (other == null)
  196. return false;
  197. return this.mPadBits == other.mPadBits
  198. && Arrays.AreEqual(this.mData, other.mData);
  199. }
  200. public override string GetString()
  201. {
  202. StringBuilder buffer = new StringBuilder("#");
  203. byte[] str = GetDerEncoded();
  204. for (int i = 0; i != str.Length; i++)
  205. {
  206. uint ubyte = str[i];
  207. buffer.Append(table[(ubyte >> 4) & 0xf]);
  208. buffer.Append(table[str[i] & 0xf]);
  209. }
  210. return buffer.ToString();
  211. }
  212. internal static DerBitString FromAsn1Octets(byte[] octets)
  213. {
  214. if (octets.Length < 1)
  215. throw new ArgumentException("truncated BIT STRING detected", "octets");
  216. int padBits = octets[0];
  217. byte[] data = Arrays.CopyOfRange(octets, 1, octets.Length);
  218. if (padBits > 0 && padBits < 8 && data.Length > 0)
  219. {
  220. int last = data[data.Length - 1];
  221. int mask = (1 << padBits) - 1;
  222. if ((last & mask) != 0)
  223. {
  224. return new BerBitString(data, padBits);
  225. }
  226. }
  227. return new DerBitString(data, padBits);
  228. }
  229. }
  230. }
  231. #pragma warning restore
  232. #endif