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
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
.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
61 // -------------------------------------------------------------------------
63 private ProtocolVersion version
;
64 private Random random
;
65 private byte[] sessionId
;
68 private List extensions
;
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
;
84 this.sessionId
= sessionId
;
87 this.extensions
= extensions
;
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];
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
));
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
117 if (in
.available() > 0) // then we have extensions.
119 ext
= new LinkedList();
120 len
= (in
.read() & 0xFF) << 8 | (in
.read() & 0xFF);
124 Extension e
= Extension
.read(in
);
126 count
+= e
.getValue().length
+ 4;
129 return new ClientHello(vers
, rand
, id
, suites
, comp
, ext
);
133 // -------------------------------------------------------------------------
135 public void write(OutputStream out
) throws IOException
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);
165 ProtocolVersion
getVersion()
175 byte[] getSessionId()
180 List
getCipherSuites()
185 List
getCompressionMethods()
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()));
205 while ((s
= r
.readLine()) != null)
211 catch (IOException ignored
)
214 out
.println(" sessionId = " + Util
.toHexString(sessionId
, ':') + ";");
215 out
.println(" cipherSuites = {");
216 for (Iterator i
= suites
.iterator(); i
.hasNext(); )
219 out
.println(i
.next());
222 out
.print(" compressionMethods = { ");
223 for (Iterator i
= comp
.iterator(); i
.hasNext(); )
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)
244 catch (IOException ignored
)
250 out
.println("} ClientHello;");
251 return str
.toString();