Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / net / ssl / provider / ClientHello.java
blob259051df129781343d8bf16e77748cdc325936a0
1 /* ClientHello.java -- SSL ClientHello message.
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
19 USA
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.
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.BufferedReader;
42 import java.io.ByteArrayOutputStream;
43 import java.io.InputStream;
44 import java.io.IOException;
45 import java.io.OutputStream;
46 import java.io.PrintWriter;
47 import java.io.StringReader;
48 import java.io.StringWriter;
50 import java.util.ArrayList;
51 import java.util.Iterator;
52 import java.util.LinkedList;
53 import java.util.List;
55 import javax.net.ssl.SSLProtocolException;
57 final class ClientHello implements Handshake.Body
60 // Fields.
61 // -------------------------------------------------------------------------
63 private ProtocolVersion version;
64 private Random random;
65 private byte[] sessionId;
66 private List suites;
67 private List comp;
68 private List extensions;
70 // Constructor.
71 // -------------------------------------------------------------------------
73 ClientHello(ProtocolVersion version, Random random,
74 byte[] sessionId, List suites, List comp)
76 this(version, random, sessionId, suites, comp, null);
79 ClientHello(ProtocolVersion version, Random random,
80 byte[] sessionId, List suites, List comp, List extensions)
82 this.version = version;
83 this.random = random;
84 this.sessionId = sessionId;
85 this.suites = suites;
86 this.comp = comp;
87 this.extensions = extensions;
90 // Class methods.
91 // -------------------------------------------------------------------------
93 static ClientHello read(InputStream in) throws IOException
95 ProtocolVersion vers = ProtocolVersion.read(in);
96 Random rand = Random.read(in);
97 byte[] id = new byte[in.read() & 0xFF];
98 in.read(id);
99 int len = (in.read() & 0xFF) << 8 | (in.read() & 0xFF);
100 ArrayList suites = new ArrayList(len / 2);
101 for (int i = 0; i < len; i += 2)
103 suites.add(CipherSuite.read(in).resolve(vers));
105 len = in.read() & 0xFF;
106 ArrayList comp = new ArrayList(len);
107 for (int i = 0; i < len; i++)
109 comp.add(CompressionMethod.read(in));
112 List ext = null;
113 // Since parsing MAY need to continue into the extensions fields, or it
114 // may end here, the specified input stream MUST be a ByteArrayInputStream
115 // over all the data this hello contains. Otherwise this will mess up
116 // the data stream.
117 if (in.available() > 0) // then we have extensions.
119 ext = new LinkedList();
120 len = (in.read() & 0xFF) << 8 | (in.read() & 0xFF);
121 int count = 0;
122 while (count < len)
124 Extension e = Extension.read(in);
125 ext.add(e);
126 count += e.getValue().length + 4;
129 return new ClientHello(vers, rand, id, suites, comp, ext);
132 // Instance methods.
133 // -------------------------------------------------------------------------
135 public void write(OutputStream out) throws IOException
137 version.write(out);
138 random.write(out);
139 out.write(sessionId.length);
140 out.write(sessionId);
141 out.write((suites.size() << 1) >>> 8 & 0xFF);
142 out.write((suites.size() << 1) & 0xFF);
143 for (Iterator i = suites.iterator(); i.hasNext(); )
145 ((CipherSuite) i.next()).write(out);
147 out.write(comp.size());
148 for (Iterator i = comp.iterator(); i.hasNext(); )
150 out.write(((CompressionMethod) i.next()).getValue());
152 if (extensions != null)
154 ByteArrayOutputStream out2 = new ByteArrayOutputStream();
155 for (Iterator i = extensions.iterator(); i.hasNext(); )
157 ((Extension) i.next()).write(out2);
159 out.write(out2.size() >>> 8 & 0xFF);
160 out.write(out2.size() & 0xFF);
161 out2.writeTo(out);
165 ProtocolVersion getVersion()
167 return version;
170 Random getRandom()
172 return random;
175 byte[] getSessionId()
177 return sessionId;
180 List getCipherSuites()
182 return suites;
185 List getCompressionMethods()
187 return comp;
190 List getExtensions()
192 return extensions;
195 public String toString()
197 StringWriter str = new StringWriter();
198 PrintWriter out = new PrintWriter(str);
199 out.println("struct {");
200 out.println(" version = " + version + ";");
201 BufferedReader r = new BufferedReader(new StringReader(random.toString()));
202 String s;
205 while ((s = r.readLine()) != null)
207 out.print(" ");
208 out.println(s);
211 catch (IOException ignored)
214 out.println(" sessionId = " + Util.toHexString(sessionId, ':') + ";");
215 out.println(" cipherSuites = {");
216 for (Iterator i = suites.iterator(); i.hasNext(); )
218 out.print(" ");
219 out.println(i.next());
221 out.println(" };");
222 out.print(" compressionMethods = { ");
223 for (Iterator i = comp.iterator(); i.hasNext(); )
225 out.print(i.next());
226 if (i.hasNext())
227 out.print(", ");
229 out.println(" };");
230 if (extensions != null)
232 out.println(" extensions = {");
233 for (Iterator i = extensions.iterator(); i.hasNext(); )
235 r = new BufferedReader(new StringReader(i.next().toString()));
238 while ((s = r.readLine()) != null)
240 out.print(" ");
241 out.println(s);
244 catch (IOException ignored)
248 out.println(" };");
250 out.println("} ClientHello;");
251 return str.toString();