1 /* Extension.java -- A TLS hello extension.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
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.
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.
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
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
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. */
39 package gnu.javax.net.ssl.provider;
41 import java.io.PrintWriter;
42 import java.io.StringWriter;
44 import java.nio.ByteBuffer;
45 import java.nio.ByteOrder;
48 * An SSL hello extension.
52 * ExtensionType extension_type;
53 * opaque extension_data<0..2^16-1>;
58 public final class Extension implements Builder, Constructed
62 // -------------------------------------------------------------------------
64 private ByteBuffer buffer;
67 // -------------------------------------------------------------------------
69 public Extension(final ByteBuffer buffer)
71 this.buffer = buffer.duplicate().order(ByteOrder.BIG_ENDIAN);
74 public Extension(final Type type, final Value value)
76 ByteBuffer valueBuffer = value.buffer();
77 int length = 2 + 2 + valueBuffer.remaining();
78 buffer = ByteBuffer.allocate(length);
79 buffer.putShort((short) type.getValue());
80 buffer.putShort((short) valueBuffer.remaining());
81 buffer.put(valueBuffer);
86 // -------------------------------------------------------------------------
90 return (buffer.getShort (2) & 0xFFFF) + 4;
93 public ByteBuffer buffer()
95 return (ByteBuffer) buffer.duplicate().limit(length());
100 return Type.forValue (buffer.getShort (0) & 0xFFFF);
103 public byte[] valueBytes()
105 int len = buffer.getShort (2) & 0xFFFF;
106 byte[] value = new byte[len];
107 ((ByteBuffer) buffer.duplicate ().position (4)).get (value);
111 public ByteBuffer valueBuffer()
113 int len = buffer.getShort(2) & 0xFFFF;
114 return ((ByteBuffer) buffer.duplicate().position(4).limit(len+4)).slice();
122 return new ServerNameList(valueBuffer());
124 case MAX_FRAGMENT_LENGTH:
125 switch (valueBuffer().get() & 0xFF)
127 case 1: return MaxFragmentLength.LEN_2_9;
128 case 2: return MaxFragmentLength.LEN_2_10;
129 case 3: return MaxFragmentLength.LEN_2_11;
130 case 4: return MaxFragmentLength.LEN_2_12;
132 throw new IllegalArgumentException("invalid max_fragment_len");
136 return new TruncatedHMAC();
138 case CLIENT_CERTIFICATE_URL:
139 return new CertificateURL(valueBuffer());
141 case TRUSTED_CA_KEYS:
142 return new TrustedAuthorities(valueBuffer());
145 return new CertificateStatusRequest(valueBuffer());
150 return new UnresolvedExtensionValue(valueBuffer());
153 public void setLength (final int newLength)
155 if (newLength < 0 || newLength > 65535)
156 throw new IllegalArgumentException ("length is out of bounds");
157 buffer.putShort (2, (short) newLength);
160 public void setType (final Type type)
162 buffer.putShort(0, (short) type.getValue());
165 public void setValue (byte[] value)
167 setValue (value, 0, value.length);
170 public void setValue (final byte[] value, final int offset, final int length)
172 if (length != length ())
173 throw new IllegalArgumentException ("length is different than claimed length");
174 ((ByteBuffer) buffer.duplicate().position(4)).put(value, offset, length);
177 public String toString()
179 return toString(null);
182 public String toString(String prefix)
184 StringWriter str = new StringWriter();
185 PrintWriter out = new PrintWriter(str);
186 if (prefix != null) out.print (prefix);
187 out.println("struct {");
188 if (prefix != null) out.print (prefix);
189 out.println(" type = " + type () + ";");
190 if (prefix != null) out.print (prefix);
191 String subprefix = " ";
192 if (prefix != null) subprefix = prefix + subprefix;
193 out.println(" value =");
194 out.println(value().toString(subprefix));
195 if (prefix != null) out.print (prefix);
196 out.print("} Extension;");
197 return str.toString();
201 // -------------------------------------------------------------------------
203 public static enum Type
206 MAX_FRAGMENT_LENGTH (1),
207 CLIENT_CERTIFICATE_URL (2),
214 private final int value;
216 private Type(int value)
221 public static Type forValue (final int value)
223 switch (value & 0xFFFF)
225 case 0: return SERVER_NAME;
226 case 1: return MAX_FRAGMENT_LENGTH;
227 case 2: return CLIENT_CERTIFICATE_URL;
228 case 3: return TRUSTED_CA_KEYS;
229 case 4: return TRUNCATED_HMAC;
230 case 5: return STATUS_REQUEST;
232 case 7: return CERT_TYPE;
233 default: return null;
237 public int getValue()
243 public static abstract class Value implements Builder, Constructed