| 1 | 769 | jeremybenn | /* Record.java -- A single SSL Record.
 | 
      
         | 2 |  |  |    Copyright (C) 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.net.ssl.provider;
 | 
      
         | 40 |  |  |  
 | 
      
         | 41 |  |  | import java.io.PrintWriter;
 | 
      
         | 42 |  |  | import java.io.StringWriter;
 | 
      
         | 43 |  |  | import java.nio.ByteBuffer;
 | 
      
         | 44 |  |  | import java.nio.ByteOrder;
 | 
      
         | 45 |  |  |  
 | 
      
         | 46 |  |  | /**
 | 
      
         | 47 |  |  |  * A SSL/TLS record structure. An SSL record is defined to be:
 | 
      
         | 48 |  |  |  *
 | 
      
         | 49 |  |  |  * <pre>
 | 
      
         | 50 |  |  | struct
 | 
      
         | 51 |  |  | {
 | 
      
         | 52 |  |  |   {@link ContentType}     type;
 | 
      
         | 53 |  |  |   {@link ProtocolVersion} version;
 | 
      
         | 54 |  |  |   uint16          length;
 | 
      
         | 55 |  |  |   opaque          fragment[TLSPlaintext.length];
 | 
      
         | 56 |  |  | } TLSPlaintext;
 | 
      
         | 57 |  |  | </pre>
 | 
      
         | 58 |  |  |  */
 | 
      
         | 59 |  |  | public class Record
 | 
      
         | 60 |  |  | {
 | 
      
         | 61 |  |  |   private final ByteBuffer buffer;
 | 
      
         | 62 |  |  |  
 | 
      
         | 63 |  |  |   public Record (final ByteBuffer buffer)
 | 
      
         | 64 |  |  |   {
 | 
      
         | 65 |  |  |     this.buffer = buffer.duplicate().order(ByteOrder.BIG_ENDIAN);
 | 
      
         | 66 |  |  |   }
 | 
      
         | 67 |  |  |  
 | 
      
         | 68 |  |  |   // XXX remove
 | 
      
         | 69 |  |  |   public ContentType getContentType ()
 | 
      
         | 70 |  |  |   {
 | 
      
         | 71 |  |  |     return contentType ();
 | 
      
         | 72 |  |  |   }
 | 
      
         | 73 |  |  |  
 | 
      
         | 74 |  |  |   /**
 | 
      
         | 75 |  |  |    * Gets the content type field.
 | 
      
         | 76 |  |  |    *
 | 
      
         | 77 |  |  |    * @return The content type field.
 | 
      
         | 78 |  |  |    */
 | 
      
         | 79 |  |  |   public ContentType contentType ()
 | 
      
         | 80 |  |  |   {
 | 
      
         | 81 |  |  |     return ContentType.forInteger (buffer.get (0) & 0xFF);
 | 
      
         | 82 |  |  |   }
 | 
      
         | 83 |  |  |  
 | 
      
         | 84 |  |  |   /**
 | 
      
         | 85 |  |  |    * Get the fragment content, storing it into <code>sink</code>.
 | 
      
         | 86 |  |  |    *
 | 
      
         | 87 |  |  |    * @param sink The sink for the fragment bytes.
 | 
      
         | 88 |  |  |    * @return The number of bytes put into <code>sink</code>
 | 
      
         | 89 |  |  |    */
 | 
      
         | 90 |  |  |   public int fragment (final ByteBuffer sink)
 | 
      
         | 91 |  |  |   {
 | 
      
         | 92 |  |  |     int length = length ();
 | 
      
         | 93 |  |  |     sink.put (((ByteBuffer) buffer.limit (5 + length).position (5)).slice ());
 | 
      
         | 94 |  |  |     return length;
 | 
      
         | 95 |  |  |   }
 | 
      
         | 96 |  |  |  
 | 
      
         | 97 |  |  |   /**
 | 
      
         | 98 |  |  |    * Returns the fragment field as a ByteBuffer. The returned buffer
 | 
      
         | 99 |  |  |    * is shared with this object's underlying buffer, so it will share
 | 
      
         | 100 |  |  |    * its attributes. For example, if the underlying buffer is
 | 
      
         | 101 |  |  |    * read-only, the returned buffer will be read-only.
 | 
      
         | 102 |  |  |    *
 | 
      
         | 103 |  |  |    * @return The fragment buffer.
 | 
      
         | 104 |  |  |    */
 | 
      
         | 105 |  |  |   public ByteBuffer fragment ()
 | 
      
         | 106 |  |  |   {
 | 
      
         | 107 |  |  |     int length = length ();
 | 
      
         | 108 |  |  |     return ((ByteBuffer) buffer.limit (5 + length).position (5)).slice ();
 | 
      
         | 109 |  |  |   }
 | 
      
         | 110 |  |  |  
 | 
      
         | 111 |  |  |   /**
 | 
      
         | 112 |  |  |    * Gets the fragment length.
 | 
      
         | 113 |  |  |    *
 | 
      
         | 114 |  |  |    * @return The fragment length.
 | 
      
         | 115 |  |  |    */
 | 
      
         | 116 |  |  |   public int length ()
 | 
      
         | 117 |  |  |   {
 | 
      
         | 118 |  |  |     // XXX this is different behavior than we usually want: we return the
 | 
      
         | 119 |  |  |     // length field, not the total length. We should consider changing this.
 | 
      
         | 120 |  |  |     return buffer.getShort (3) & 0xFFFF;
 | 
      
         | 121 |  |  |   }
 | 
      
         | 122 |  |  |  
 | 
      
         | 123 |  |  |   /**
 | 
      
         | 124 |  |  |    * Gets the protocol version field.
 | 
      
         | 125 |  |  |    *
 | 
      
         | 126 |  |  |    * @return The protocol version field.
 | 
      
         | 127 |  |  |    */
 | 
      
         | 128 |  |  |   public ProtocolVersion version ()
 | 
      
         | 129 |  |  |   {
 | 
      
         | 130 |  |  |     int major = buffer.get (1) & 0xFF;
 | 
      
         | 131 |  |  |     int minor = buffer.get (2) & 0xFF;
 | 
      
         | 132 |  |  |     return ProtocolVersion.getInstance (major, minor);
 | 
      
         | 133 |  |  |   }
 | 
      
         | 134 |  |  |  
 | 
      
         | 135 |  |  |   /**
 | 
      
         | 136 |  |  |    * Sets the content type field.
 | 
      
         | 137 |  |  |    *
 | 
      
         | 138 |  |  |    * @param type The content type.
 | 
      
         | 139 |  |  |    * @throws java.nio.ReadOnlyBufferException If the underlying buffer
 | 
      
         | 140 |  |  |    * is not writeable.
 | 
      
         | 141 |  |  |    * @throws NullPointerException If <i>type</i> is <code>null</code>.
 | 
      
         | 142 |  |  |    */
 | 
      
         | 143 |  |  |   public void setContentType (final ContentType type)
 | 
      
         | 144 |  |  |   {
 | 
      
         | 145 |  |  |     buffer.put (0, (byte) type.getValue ());
 | 
      
         | 146 |  |  |   }
 | 
      
         | 147 |  |  |  
 | 
      
         | 148 |  |  |   /**
 | 
      
         | 149 |  |  |    * Sets the fragment length.
 | 
      
         | 150 |  |  |    *
 | 
      
         | 151 |  |  |    * @param length The fragment length.
 | 
      
         | 152 |  |  |    * @throws java.nio.ReadOnlyBufferException If the underlying buffer
 | 
      
         | 153 |  |  |    * is not writeable.
 | 
      
         | 154 |  |  |    * @throws IllegalArgumentException If the length is not between 0
 | 
      
         | 155 |  |  |    * and 16384, inclusive.
 | 
      
         | 156 |  |  |    */
 | 
      
         | 157 |  |  |   public void setLength (final int length)
 | 
      
         | 158 |  |  |   {
 | 
      
         | 159 |  |  |     if (length < 0 || length > 16384)
 | 
      
         | 160 |  |  |       throw new IllegalArgumentException ("length " + length + " out of range; "
 | 
      
         | 161 |  |  |                                           + "must be between 0 and 16384");
 | 
      
         | 162 |  |  |     buffer.putShort (3, (short) length);
 | 
      
         | 163 |  |  |   }
 | 
      
         | 164 |  |  |  
 | 
      
         | 165 |  |  |   /**
 | 
      
         | 166 |  |  |    * Sets the protocol version field.
 | 
      
         | 167 |  |  |    *
 | 
      
         | 168 |  |  |    * @param version The protocol version.
 | 
      
         | 169 |  |  |    * @throws java.nio.ReadOnlyBufferException If the underlying buffer
 | 
      
         | 170 |  |  |    * is not writeable.
 | 
      
         | 171 |  |  |    * @throws NullPointerException If <i>version</i> is <code>null</code>.
 | 
      
         | 172 |  |  |    */
 | 
      
         | 173 |  |  |   public void setVersion (final ProtocolVersion version)
 | 
      
         | 174 |  |  |   {
 | 
      
         | 175 |  |  |     buffer.put (1, (byte) version.major ()).put (2, (byte) version.minor ());
 | 
      
         | 176 |  |  |   }
 | 
      
         | 177 |  |  |  
 | 
      
         | 178 |  |  |   public String toString ()
 | 
      
         | 179 |  |  |   {
 | 
      
         | 180 |  |  |     StringWriter str = new StringWriter ();
 | 
      
         | 181 |  |  |     PrintWriter out = new PrintWriter (str);
 | 
      
         | 182 |  |  |     out.println ("struct {");
 | 
      
         | 183 |  |  |     out.print ("  type:    ");
 | 
      
         | 184 |  |  |     out.print (contentType ());
 | 
      
         | 185 |  |  |     out.println (";");
 | 
      
         | 186 |  |  |     out.print ("  version: ");
 | 
      
         | 187 |  |  |     out.print (version ());
 | 
      
         | 188 |  |  |     out.println (";");
 | 
      
         | 189 |  |  |     out.print("  length: ");
 | 
      
         | 190 |  |  |     out.print(length());
 | 
      
         | 191 |  |  |     out.println(";");
 | 
      
         | 192 |  |  |     out.println ("  fragment {");
 | 
      
         | 193 |  |  |     out.print (Util.hexDump (fragment (), "    "));
 | 
      
         | 194 |  |  |     out.println ("  };");
 | 
      
         | 195 |  |  |     out.print ("} Record;");
 | 
      
         | 196 |  |  |     return str.toString ();
 | 
      
         | 197 |  |  |   }
 | 
      
         | 198 |  |  | }
 |