Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: gnu/javax/crypto/sasl/OutputBuffer.java


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