InputBuffer.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* InputBuffer.java --
  2. Copyright (C) 2003, 2006 Free Software Foundation, Inc.
  3. This file is a part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or (at
  7. your option) any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. USA
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.crypto.sasl;
  32. import gnu.java.security.Registry;
  33. import java.io.ByteArrayInputStream;
  34. import java.io.IOException;
  35. import java.math.BigInteger;
  36. /**
  37. * The implementation of an incoming SASL buffer.
  38. * <p>
  39. * The data elements this class caters for are described in [1].
  40. * <p>
  41. * References:
  42. * <ol>
  43. * <li><a
  44. * href="http://www.ietf.org/internet-drafts/draft-burdis-cat-srp-sasl-09.txt">
  45. * Secure Remote Password Authentication Mechanism</a>;<br/>
  46. * draft-burdis-cat-srp-sasl-09,<br/> <a
  47. * href="mailto:keith@rucus.ru.ac.za">Keith Burdis</a> and <a
  48. * href="mailto:raif@forge.com.au">Ra&iuml;f S. Naffah</a>.</li>
  49. * </ol>
  50. */
  51. public class InputBuffer
  52. {
  53. /** The internal buffer stream containing the buffer's contents. */
  54. protected ByteArrayInputStream in;
  55. /** The length of the buffer, according to its header. */
  56. protected int length;
  57. /**
  58. * Constructs a SASL buffer given the buffer's encoded form, including its
  59. * header bytes.
  60. *
  61. * @param frame the encoded form, including the header bytes, of a SASL
  62. * buffer.
  63. * @throws SaslEncodingException if the buffer is malformed.
  64. */
  65. public InputBuffer(byte[] frame) throws SaslEncodingException
  66. {
  67. this();
  68. if (frame.length < 4)
  69. throw new SaslEncodingException("SASL buffer header too short");
  70. length = (frame[0] & 0xFF) << 24
  71. | (frame[1] & 0xFF) << 16
  72. | (frame[2] & 0xFF) << 8
  73. | (frame[3] & 0xFF);
  74. if (length > Registry.SASL_BUFFER_MAX_LIMIT || length < 0)
  75. throw new SaslEncodingException("SASL buffer size limit exceeded");
  76. in = new ByteArrayInputStream(frame, 4, length);
  77. }
  78. /** Trivial private constructor for use by the class method. */
  79. private InputBuffer()
  80. {
  81. super();
  82. }
  83. /**
  84. * Returns an instance of a SASL buffer given the buffer's encoded contents,
  85. * excluding the buffer's header bytes.
  86. * <p>
  87. * Calls the method with the same name and three arguments as:
  88. * <code>getInstance(raw, 0, raw.length)</code>.
  89. *
  90. * @param raw the encoded form, excluding the header bytes, of a SASL buffer.
  91. * @return a new instance of {@link InputBuffer}.
  92. */
  93. public static InputBuffer getInstance(byte[] raw)
  94. {
  95. return getInstance(raw, 0, raw.length);
  96. }
  97. /**
  98. * Returns an instance of a SASL buffer given the buffer's encoded contents,
  99. * excluding the buffer's header bytes.
  100. *
  101. * @param raw the encoded form, excluding the header bytes, of a SASL buffer.
  102. * @param offset offset where to start using raw bytes from.
  103. * @param len number of bytes to use.
  104. * @return a new instance of {@link InputBuffer}.
  105. */
  106. public static InputBuffer getInstance(byte[] raw, int offset, int len)
  107. {
  108. InputBuffer result = new InputBuffer();
  109. result.in = new ByteArrayInputStream(raw, offset, len);
  110. return result;
  111. }
  112. /**
  113. * Converts two octets into the number that they represent.
  114. *
  115. * @param b the two octets.
  116. * @return the length.
  117. */
  118. public static int twoBytesToLength(byte[] b) throws SaslEncodingException
  119. {
  120. final int result = (b[0] & 0xFF) << 8 | (b[1] & 0xFF);
  121. if (result > Registry.SASL_TWO_BYTE_MAX_LIMIT)
  122. throw new SaslEncodingException("SASL MPI/Text size limit exceeded");
  123. return result;
  124. }
  125. public boolean hasMoreElements()
  126. {
  127. return (in.available() > 0);
  128. }
  129. /**
  130. * Decodes a SASL scalar quantity, <code>count</code>-octet long, from the
  131. * current buffer.
  132. *
  133. * @param count the number of octets of this scalar quantity.
  134. * @return a native representation of a SASL scalar (unsigned integer)
  135. * quantity.
  136. * @throws SaslEncodingException if an encoding exception occurs during the
  137. * operation.
  138. * @throws IOException if any other I/O exception occurs during the operation.
  139. */
  140. public long getScalar(int count) throws IOException
  141. {
  142. if (count < 0 || count > 4)
  143. throw new SaslEncodingException("Invalid SASL scalar octet count: "
  144. + String.valueOf(count));
  145. if (! hasMoreElements())
  146. throw new SaslEncodingException("Not enough bytes for a scalar in buffer");
  147. if (in.available() < count)
  148. throw new SaslEncodingException("Illegal SASL scalar encoding");
  149. byte[] element = new byte[count];
  150. in.read(element);
  151. long result = 0L;
  152. for (int i = 0; i < count; i++)
  153. {
  154. result <<= 8;
  155. result |= element[i] & 0xFFL;
  156. }
  157. return result;
  158. }
  159. /**
  160. * Decodes a SASL OS from the current buffer.
  161. *
  162. * @return a native representation of a SASL OS.
  163. * @throws SaslEncodingException if an encoding exception occurs during the
  164. * operation.
  165. * @throws IOException if any other I/O exception occurs during the operation.
  166. */
  167. public byte[] getOS() throws IOException
  168. {
  169. if (! hasMoreElements())
  170. throw new SaslEncodingException(
  171. "Not enough bytes for an octet-sequence in buffer");
  172. final int elementLength = in.read();
  173. if (elementLength > Registry.SASL_ONE_BYTE_MAX_LIMIT)
  174. throw new SaslEncodingException("SASL octet-sequence size limit exceeded");
  175. if (in.available() < elementLength)
  176. throw new SaslEncodingException("Illegal SASL octet-sequence encoding");
  177. byte[] result = new byte[elementLength];
  178. in.read(result);
  179. return result;
  180. }
  181. /**
  182. * Decodes a SASL EOS from the current buffer.
  183. *
  184. * @return a native representation of a SASL EOS.
  185. * @throws SaslEncodingException if an encoding exception occurs during the
  186. * operation.
  187. * @throws IOException if any other I/O exception occurs during the operation.
  188. */
  189. public byte[] getEOS() throws IOException
  190. {
  191. if (in.available() < 2)
  192. throw new SaslEncodingException(
  193. "Not enough bytes for an extended octet-sequence in buffer");
  194. byte[] elementLengthBytes = new byte[2];
  195. in.read(elementLengthBytes);
  196. final int elementLength = twoBytesToLength(elementLengthBytes);
  197. if (in.available() < elementLength)
  198. throw new SaslEncodingException(
  199. "Illegal SASL extended octet-sequence encoding");
  200. byte[] result = new byte[elementLength];
  201. in.read(result);
  202. return result;
  203. }
  204. /**
  205. * Decodes a SASL MPI from the current buffer.
  206. *
  207. * @return a native representation of a SASL MPI.
  208. * @throws SaslEncodingException if an encoding exception occurs during the
  209. * operation.
  210. * @throws IOException if any other I/O exception occurs during the operation.
  211. */
  212. public BigInteger getMPI() throws IOException
  213. {
  214. if (in.available() < 2)
  215. throw new SaslEncodingException("Not enough bytes for an MPI in buffer");
  216. byte[] elementLengthBytes = new byte[2];
  217. in.read(elementLengthBytes);
  218. final int elementLength = twoBytesToLength(elementLengthBytes);
  219. if (in.available() < elementLength)
  220. throw new SaslEncodingException(
  221. "Illegal SASL multi-precision integer encoding");
  222. byte[] element = new byte[elementLength];
  223. in.read(element);
  224. return new BigInteger(1, element);
  225. }
  226. /**
  227. * Decodes a SASL Text from the current buffer.
  228. *
  229. * @return a native representation of a SASL Text.
  230. * @throws SaslEncodingException if an encoding exception occurs during the
  231. * operation.
  232. * @throws SaslEncodingException if the UTF-8 character encoding is not
  233. * supported on this platform.
  234. * @throws IOException if any other I/O exception occurs during the operation.
  235. */
  236. public String getText() throws IOException
  237. {
  238. if (in.available() < 2)
  239. throw new SaslEncodingException("Not enough bytes for a text in buffer");
  240. byte[] elementLengthBytes = new byte[2];
  241. in.read(elementLengthBytes);
  242. final int elementLength = twoBytesToLength(elementLengthBytes);
  243. if (in.available() < elementLength)
  244. throw new SaslEncodingException("Illegal SASL text encoding");
  245. byte[] element = new byte[elementLength];
  246. in.read(element);
  247. return new String(element, "UTF8");
  248. }
  249. }