DerBitString.cs 7.3 KB

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