Source code: gnu/javax/net/ssl/provider/Extension.java
1 /* Extension.java -- A TLS hello extension.
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.EOFException;
42 import java.io.InputStream;
43 import java.io.IOException;
44 import java.io.OutputStream;
45 import java.io.PrintWriter;
46 import java.io.StringWriter;
47
48 final class Extension implements Constructed
49 {
50
51 // Fields.
52 // -------------------------------------------------------------------------
53
54 private final Type type;
55 private final byte[] value;
56
57 // Constructor.
58 // -------------------------------------------------------------------------
59
60 Extension(Type type, byte[] value)
61 {
62 if (type == null || value == null)
63 {
64 throw new NullPointerException();
65 }
66 this.type = type;
67 this.value = value;
68 }
69
70 // Class method.
71 // -------------------------------------------------------------------------
72
73 static Extension read(InputStream in) throws IOException
74 {
75 Type t = Type.read(in);
76 int len = (in.read() & 0xFF) << 8 | (in.read() & 0xFF);
77 byte[] v = new byte[len];
78 int count = 0;
79 while (count < len)
80 {
81 int l = in.read(v, count, len - count);
82 if (l == -1)
83 {
84 throw new EOFException("unexpected end of extension");
85 }
86 count += l;
87 }
88 return new Extension(t, v);
89 }
90
91 // Instance methods.
92 // -------------------------------------------------------------------------
93
94 public void write(OutputStream out) throws IOException
95 {
96 out.write(type.getEncoded());
97 out.write(value.length >>> 8 & 0xFF);
98 out.write(value.length & 0xFF);
99 out.write(value);
100 }
101
102 Type getType()
103 {
104 return type;
105 }
106
107 byte[] getValue()
108 {
109 return value;
110 }
111
112 public String toString()
113 {
114 StringWriter str = new StringWriter();
115 PrintWriter out = new PrintWriter(str);
116 out.println("struct {");
117 out.println(" type = " + type + ";");
118 out.println(" value =");
119 out.println(Util.hexDump(value, " "));
120 out.println("} Extension;");
121 return str.toString();
122 }
123
124 // Inner class.
125 // -------------------------------------------------------------------------
126
127 static final class Type implements Enumerated
128 {
129
130 // Constants and fields.
131 // -----------------------------------------------------------------------
132
133 static final Type SERVER_NAME = new Type(0);
134 static final Type MAX_FRAGMENT_LENGTH = new Type(1);
135 static final Type CLIENT_CERTIFICATE_URL = new Type(2);
136 static final Type TRUSTED_CA_KEYS = new Type(3);
137 static final Type TRUNCATED_HMAC = new Type(4);
138 static final Type STATUS_REQUEST = new Type(5);
139 static final Type SRP = new Type(6);
140 static final Type CERT_TYPE = new Type(7);
141
142 private final int value;
143
144 // Constructor.
145 // -----------------------------------------------------------------------
146
147 private Type(int value)
148 {
149 this.value = value;
150 }
151
152 // Class methods.
153 // -----------------------------------------------------------------------
154
155 static Type read(InputStream in) throws IOException
156 {
157 int i = in.read();
158 if (i == -1)
159 {
160 throw new EOFException("unexpected end of input stream");
161 }
162 int value = (i & 0xFF) << 8;
163 i = in.read();
164 if (i == -1)
165 {
166 throw new EOFException("unexpected end of input stream");
167 }
168 value |= i & 0xFF;
169 switch (value)
170 {
171 case 0: return SERVER_NAME;
172 case 1: return MAX_FRAGMENT_LENGTH;
173 case 2: return CLIENT_CERTIFICATE_URL;
174 case 3: return TRUSTED_CA_KEYS;
175 case 4: return TRUNCATED_HMAC;
176 case 5: return STATUS_REQUEST;
177 case 6: return SRP;
178 case 7: return CERT_TYPE;
179 default: return new Type(value);
180 }
181 }
182
183 // Instance methods.
184 // -----------------------------------------------------------------------
185
186 public byte[] getEncoded()
187 {
188 return new byte[] {
189 (byte) (value >>> 8 & 0xFF), (byte) (value & 0xFF)
190 };
191 }
192
193 public int getValue()
194 {
195 return value;
196 }
197
198 public String toString()
199 {
200 switch (value)
201 {
202 case 0: return "server_name";
203 case 1: return "max_fragment_length";
204 case 2: return "client_certificate_url";
205 case 3: return "trusted_ca_keys";
206 case 4: return "truncated_hmac";
207 case 5: return "status_request";
208 case 6: return "srp";
209 case 7: return "cert_type";
210 default: return "unknown(" + value + ")";
211 }
212 }
213 }
214 }