OutputBuffer.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* OutputBuffer.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 gnu.java.security.util.Util;
  34. import java.io.ByteArrayOutputStream;
  35. import java.io.IOException;
  36. import java.math.BigInteger;
  37. /**
  38. * The implementation of an outgoing SASL buffer.
  39. * <p>
  40. * The data elements this class caters for are described in [1].
  41. * <p>
  42. * References:
  43. * <ol>
  44. * <li><a
  45. * href="http://www.ietf.org/internet-drafts/draft-burdis-cat-srp-sasl-09.txt">
  46. * Secure Remote Password Authentication Mechanism</a>;<br/>
  47. * draft-burdis-cat-srp-sasl-09,<br/> <a
  48. * href="mailto:keith@rucus.ru.ac.za">Keith Burdis</a> and <a
  49. * href="mailto:raif@forge.com.au">Ra&iuml;f S. Naffah</a>.</li>
  50. * </ol>
  51. */
  52. public class OutputBuffer
  53. {
  54. /** The internal output stream. */
  55. private ByteArrayOutputStream out;
  56. public OutputBuffer()
  57. {
  58. super();
  59. out = new ByteArrayOutputStream();
  60. }
  61. /**
  62. * Encodes a SASL scalar quantity, <code>count</code>-octet long, to the
  63. * current buffer.
  64. *
  65. * @param count number of octets to encode <code>b</code> with.
  66. * @param b the scalar quantity.
  67. * @throws SaslEncodingException if an encoding size constraint is violated.
  68. * @throws IOException if any other I/O exception occurs during the operation.
  69. */
  70. public void setScalar(int count, int b) throws IOException
  71. {
  72. if (count < 0 || count > 4)
  73. throw new SaslEncodingException("Invalid SASL scalar octet count: "
  74. + String.valueOf(count));
  75. byte[] element = new byte[count];
  76. for (int i = count; --i >= 0; b >>>= 8)
  77. element[i] = (byte) b;
  78. out.write(element);
  79. }
  80. /**
  81. * Encodes a SASL OS to the current buffer.
  82. *
  83. * @param b the OS element.
  84. * @throws SaslEncodingException if an encoding size constraint is violated.
  85. * @throws IOException if any other I/O exception occurs during the operation.
  86. */
  87. public void setOS(byte[] b) throws IOException
  88. {
  89. final int length = b.length;
  90. if (length > Registry.SASL_ONE_BYTE_MAX_LIMIT)
  91. throw new SaslEncodingException("SASL octet-sequence too long");
  92. out.write(length & 0xFF);
  93. out.write(b);
  94. }
  95. /**
  96. * Encodes a SASL EOS to the current buffer.
  97. *
  98. * @param b the EOS element.
  99. * @throws SaslEncodingException if an encoding size constraint is violated.
  100. * @throws IOException if any other I/O exception occurs during the operation.
  101. */
  102. public void setEOS(byte[] b) throws IOException
  103. {
  104. final int length = b.length;
  105. if (length > Registry.SASL_TWO_BYTE_MAX_LIMIT)
  106. throw new SaslEncodingException("SASL extended octet-sequence too long");
  107. byte[] lengthBytes = { (byte)(length >>> 8), (byte) length };
  108. out.write(lengthBytes);
  109. out.write(b);
  110. }
  111. /**
  112. * Encodes a SASL MPI to the current buffer.
  113. *
  114. * @param val the MPI element.
  115. * @throws SaslEncodingException if an encoding size constraint is violated.
  116. * @throws IOException if any other I/O exception occurs during the operation.
  117. */
  118. public void setMPI(BigInteger val) throws IOException
  119. {
  120. byte[] b = Util.trim(val);
  121. final int length = b.length;
  122. if (length > Registry.SASL_TWO_BYTE_MAX_LIMIT)
  123. throw new SaslEncodingException("SASL multi-precision integer too long");
  124. byte[] lengthBytes = { (byte)(length >>> 8), (byte) length };
  125. out.write(lengthBytes);
  126. out.write(b);
  127. }
  128. /**
  129. * Encodes a SASL Text to the current buffer.
  130. *
  131. * @param str the Text element.
  132. * @throws SaslEncodingException if an encoding size constraint is violated.
  133. * @throws SaslEncodingException if the UTF-8 encoding is not supported on
  134. * this platform.
  135. * @throws IOException if any other I/O exception occurs during the operation.
  136. */
  137. public void setText(String str) throws IOException
  138. {
  139. byte[] b = str.getBytes("UTF8");
  140. final int length = b.length;
  141. if (length > Registry.SASL_TWO_BYTE_MAX_LIMIT)
  142. throw new SaslEncodingException("SASL text too long");
  143. byte[] lengthBytes = { (byte)(length >>> 8), (byte) length };
  144. out.write(lengthBytes);
  145. out.write(b);
  146. }
  147. /**
  148. * Returns the encoded form of the current buffer including the 4-byte length
  149. * header.
  150. *
  151. * @throws SaslEncodingException if an encoding size constraint is violated.
  152. */
  153. public byte[] encode() throws SaslEncodingException
  154. {
  155. byte[] buffer = wrap();
  156. final int length = buffer.length;
  157. byte[] result = new byte[length + 4];
  158. result[0] = (byte)(length >>> 24);
  159. result[1] = (byte)(length >>> 16);
  160. result[2] = (byte)(length >>> 8);
  161. result[3] = (byte) length;
  162. System.arraycopy(buffer, 0, result, 4, length);
  163. return result;
  164. }
  165. /**
  166. * Returns the encoded form of the current buffer excluding the 4-byte length
  167. * header.
  168. *
  169. * @throws SaslEncodingException if an encoding size constraint is violated.
  170. */
  171. public byte[] wrap() throws SaslEncodingException
  172. {
  173. final int length = out.size();
  174. if (length > Registry.SASL_BUFFER_MAX_LIMIT || length < 0)
  175. throw new SaslEncodingException("SASL buffer too long");
  176. return out.toByteArray();
  177. }
  178. }