DerBitString.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. /**
  16. * return a Bit string from the passed in object
  17. *
  18. * @exception ArgumentException if the object cannot be converted.
  19. */
  20. public static DerBitString GetInstance(object obj)
  21. {
  22. if (obj == null || obj is DerBitString)
  23. {
  24. return (DerBitString) obj;
  25. }
  26. if (obj is byte[])
  27. {
  28. try
  29. {
  30. return (DerBitString)FromByteArray((byte[])obj);
  31. }
  32. catch (Exception e)
  33. {
  34. throw new ArgumentException("encoding error in GetInstance: " + e.ToString());
  35. }
  36. }
  37. throw new ArgumentException("illegal object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  38. }
  39. /**
  40. * return a Bit string from a tagged object.
  41. *
  42. * @param obj the tagged object holding the object we want
  43. * @param explicitly true if the object is meant to be explicitly
  44. * tagged false otherwise.
  45. * @exception ArgumentException if the tagged object cannot
  46. * be converted.
  47. */
  48. public static DerBitString GetInstance(Asn1TaggedObject obj, bool isExplicit)
  49. {
  50. Asn1Object o = obj.GetObject();
  51. if (isExplicit || o is DerBitString)
  52. {
  53. return GetInstance(o);
  54. }
  55. // Not copied because assumed to be a tagged implicit primitive from the parser
  56. return CreatePrimitive(((Asn1OctetString)o).GetOctets());
  57. }
  58. internal readonly byte[] contents;
  59. public DerBitString(byte data, int padBits)
  60. {
  61. if (padBits > 7 || padBits < 0)
  62. throw new ArgumentException("pad bits cannot be greater than 7 or less than 0", "padBits");
  63. this.contents = new byte[] { (byte)padBits, data };
  64. }
  65. public DerBitString(byte[] data)
  66. : this(data, 0)
  67. {
  68. }
  69. /**
  70. * @param data the octets making up the bit string.
  71. * @param padBits the number of extra bits at the end of the string.
  72. */
  73. public DerBitString(byte[] data, int padBits)
  74. {
  75. if (data == null)
  76. throw new ArgumentNullException("data");
  77. if (padBits < 0 || padBits > 7)
  78. throw new ArgumentException("must be in the range 0 to 7", "padBits");
  79. if (data.Length == 0 && padBits != 0)
  80. throw new ArgumentException("if 'data' is empty, 'padBits' must be 0");
  81. this.contents = Arrays.Prepend(data, (byte)padBits);
  82. }
  83. public DerBitString(int namedBits)
  84. {
  85. if (namedBits == 0)
  86. {
  87. this.contents = new byte[]{ 0 };
  88. return;
  89. }
  90. int bits = BigInteger.BitLen(namedBits);
  91. int bytes = (bits + 7) / 8;
  92. Debug.Assert(0 < bytes && bytes <= 4);
  93. byte[] data = new byte[1 + bytes];
  94. for (int i = 1; i < bytes; i++)
  95. {
  96. data[i] = (byte)namedBits;
  97. namedBits >>= 8;
  98. }
  99. Debug.Assert((namedBits & 0xFF) != 0);
  100. data[bytes] = (byte)namedBits;
  101. int padBits = 0;
  102. while ((namedBits & (1 << padBits)) == 0)
  103. {
  104. ++padBits;
  105. }
  106. Debug.Assert(padBits < 8);
  107. data[0] = (byte)padBits;
  108. this.contents = data;
  109. }
  110. public DerBitString(Asn1Encodable obj)
  111. : this(obj.GetDerEncoded())
  112. {
  113. }
  114. internal DerBitString(byte[] contents, bool check)
  115. {
  116. if (check)
  117. {
  118. if (null == contents)
  119. throw new ArgumentNullException("contents");
  120. if (contents.Length < 1)
  121. throw new ArgumentException("cannot be empty", "contents");
  122. int padBits = contents[0];
  123. if (padBits > 0)
  124. {
  125. if (contents.Length < 2)
  126. throw new ArgumentException("zero length data with non-zero pad bits", "contents");
  127. if (padBits > 7)
  128. throw new ArgumentException("pad bits cannot be greater than 7 or less than 0", "contents");
  129. }
  130. }
  131. this.contents = contents;
  132. }
  133. /**
  134. * Return the octets contained in this BIT STRING, checking that this BIT STRING really
  135. * does represent an octet aligned string. Only use this method when the standard you are
  136. * following dictates that the BIT STRING will be octet aligned.
  137. *
  138. * @return a copy of the octet aligned data.
  139. */
  140. public virtual byte[] GetOctets()
  141. {
  142. if (contents[0] != 0)
  143. throw new InvalidOperationException("attempt to get non-octet aligned data from BIT STRING");
  144. return Arrays.CopyOfRange(contents, 1, contents.Length);
  145. }
  146. public virtual byte[] GetBytes()
  147. {
  148. if (contents.Length == 1)
  149. return Asn1OctetString.EmptyOctets;
  150. int padBits = contents[0];
  151. byte[] rv = Arrays.CopyOfRange(contents, 1, contents.Length);
  152. // DER requires pad bits be zero
  153. rv[rv.Length - 1] &= (byte)(0xFF << padBits);
  154. return rv;
  155. }
  156. public virtual int PadBits
  157. {
  158. get { return contents[0]; }
  159. }
  160. /**
  161. * @return the value of the bit string as an int (truncating if necessary)
  162. */
  163. public virtual int IntValue
  164. {
  165. get
  166. {
  167. int value = 0, end = System.Math.Min(5, contents.Length - 1);
  168. for (int i = 1; i < end; ++i)
  169. {
  170. value |= (int)contents[i] << (8 * (i - 1));
  171. }
  172. if (1 <= end && end < 5)
  173. {
  174. int padBits = contents[0];
  175. byte der = (byte)(contents[end] & (0xFF << padBits));
  176. value |= (int)der << (8 * (end - 1));
  177. }
  178. return value;
  179. }
  180. }
  181. internal override int EncodedLength(bool withID)
  182. {
  183. return Asn1OutputStream.GetLengthOfEncodingDL(withID, contents.Length);
  184. }
  185. internal override void Encode(Asn1OutputStream asn1Out, bool withID)
  186. {
  187. int padBits = contents[0];
  188. int length = contents.Length;
  189. int last = length - 1;
  190. byte lastOctet = contents[last];
  191. byte lastOctetDer = (byte)(contents[last] & (0xFF << padBits));
  192. if (lastOctet == lastOctetDer)
  193. {
  194. asn1Out.WriteEncodingDL(withID, Asn1Tags.BitString, contents);
  195. }
  196. else
  197. {
  198. asn1Out.WriteEncodingDL(withID, Asn1Tags.BitString, contents, 0, last, lastOctetDer);
  199. }
  200. }
  201. protected override int Asn1GetHashCode()
  202. {
  203. if (contents.Length < 2)
  204. return 1;
  205. int padBits = contents[0];
  206. int last = contents.Length - 1;
  207. byte lastOctetDer = (byte)(contents[last] & (0xFF << padBits));
  208. int hc = Arrays.GetHashCode(contents, 0, last);
  209. hc *= 257;
  210. hc ^= lastOctetDer;
  211. return hc;
  212. }
  213. protected override bool Asn1Equals(Asn1Object asn1Object)
  214. {
  215. DerBitString that = asn1Object as DerBitString;
  216. if (null == that)
  217. return false;
  218. byte[] thisContents = this.contents, thatContents = that.contents;
  219. int length = thisContents.Length;
  220. if (thatContents.Length != length)
  221. return false;
  222. if (length == 1)
  223. return true;
  224. int last = length - 1;
  225. for (int i = 0; i < last; ++i)
  226. {
  227. if (thisContents[i] != thatContents[i])
  228. return false;
  229. }
  230. int padBits = thisContents[0];
  231. byte thisLastOctetDer = (byte)(thisContents[last] & (0xFF << padBits));
  232. byte thatLastOctetDer = (byte)(thatContents[last] & (0xFF << padBits));
  233. return thisLastOctetDer == thatLastOctetDer;
  234. }
  235. public override string GetString()
  236. {
  237. StringBuilder buffer = new StringBuilder("#");
  238. byte[] str = GetDerEncoded();
  239. for (int i = 0; i != str.Length; i++)
  240. {
  241. uint ubyte = str[i];
  242. buffer.Append(table[(ubyte >> 4) & 0xf]);
  243. buffer.Append(table[str[i] & 0xf]);
  244. }
  245. return buffer.ToString();
  246. }
  247. internal static int EncodedLength(bool withID, int contentsLength)
  248. {
  249. return Asn1OutputStream.GetLengthOfEncodingDL(withID, contentsLength);
  250. }
  251. internal static void Encode(Asn1OutputStream asn1Out, bool withID, byte[] buf, int off, int len)
  252. {
  253. asn1Out.WriteEncodingDL(withID, Asn1Tags.BitString, buf, off, len);
  254. }
  255. internal static void Encode(Asn1OutputStream asn1Out, bool withID, byte pad, byte[] buf, int off, int len)
  256. {
  257. asn1Out.WriteEncodingDL(withID, Asn1Tags.BitString, pad, buf, off, len);
  258. }
  259. internal static DerBitString CreatePrimitive(byte[] contents)
  260. {
  261. int length = contents.Length;
  262. if (length < 1)
  263. throw new ArgumentException("truncated BIT STRING detected", "contents");
  264. int padBits = contents[0];
  265. if (padBits > 0)
  266. {
  267. if (padBits > 7 || length < 2)
  268. throw new ArgumentException("invalid pad bits detected", "contents");
  269. byte finalOctet = contents[length - 1];
  270. if (finalOctet != (byte)(finalOctet & (0xFF << padBits)))
  271. {
  272. return new BerBitString(contents, false);
  273. }
  274. }
  275. return new DerBitString(contents, false);
  276. }
  277. }
  278. }
  279. #pragma warning restore
  280. #endif