1 /* ServerHello.java -- SSL ServerHello 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 class ServerHello
implements Handshake
.Body
61 // -------------------------------------------------------------------------
63 private final ProtocolVersion version
;
64 private final Random random
;
65 private final byte[] sessionId
;
66 private final CipherSuite suite
;
67 private final CompressionMethod comp
;
68 private final List extensions
;
71 // -------------------------------------------------------------------------
73 ServerHello(ProtocolVersion version
, Random random
,
74 byte[] sessionId
, CipherSuite suite
,
75 CompressionMethod comp
)
77 this(version
, random
, sessionId
, suite
, comp
, null);
80 ServerHello(ProtocolVersion version
, Random random
,
81 byte[] sessionId
, CipherSuite suite
,
82 CompressionMethod comp
, List extensions
)
84 this.version
= version
;
86 this.sessionId
= sessionId
;
89 this.extensions
= extensions
;
93 // -------------------------------------------------------------------------
95 static ServerHello
read(InputStream in
) throws IOException
97 ProtocolVersion vers
= ProtocolVersion
.read(in
);
98 Random rand
= Random
.read(in
);
99 byte[] id
= new byte[in
.read() & 0xFF];
101 CipherSuite suite
= CipherSuite
.read(in
).resolve(vers
);
102 CompressionMethod comp
= CompressionMethod
.read(in
);
104 if (in
.available() > 0)
106 ext
= new LinkedList();
107 int len
= (in
.read() >>> 8 & 0xFF) | (in
.read() & 0xFF);
111 Extension e
= Extension
.read(in
);
113 count
+= e
.getValue().length
+ 4;
116 return new ServerHello(vers
, rand
, id
, suite
, comp
, ext
);
120 // -------------------------------------------------------------------------
122 public void write(OutputStream out
) throws IOException
126 out
.write(sessionId
.length
);
127 out
.write(sessionId
);
129 out
.write(comp
.getValue());
130 if (extensions
!= null)
132 ByteArrayOutputStream out2
= new ByteArrayOutputStream();
133 for (Iterator i
= extensions
.iterator(); i
.hasNext(); )
134 ((Extension
) i
.next()).write(out2
);
135 out
.write(out2
.size() >>> 8 & 0xFF);
136 out
.write(out2
.size() & 0xFF);
141 ProtocolVersion
getVersion()
151 byte[] getSessionId()
153 return (byte[]) sessionId
.clone();
156 CipherSuite
getCipherSuite()
161 CompressionMethod
getCompressionMethod()
171 public String
toString()
173 StringWriter str
= new StringWriter();
174 PrintWriter out
= new PrintWriter(str
);
175 out
.println("struct {");
176 out
.println(" version = " + version
+ ";");
177 BufferedReader r
= new BufferedReader(new StringReader(random
.toString()));
181 while ((s
= r
.readLine()) != null)
187 catch (IOException ignored
)
190 out
.println(" sessionId = " + Util
.toHexString(sessionId
, ':') + ";");
191 out
.println(" cipherSuite = " + suite
+ ";");
192 out
.println(" compressionMethod = " + comp
+ ";");
193 if (extensions
!= null)
195 out
.println(" extensions = {");
196 for (Iterator i
= extensions
.iterator(); i
.hasNext(); )
198 r
= new BufferedReader(new StringReader(i
.next().toString()));
201 while ((s
= r
.readLine()) != null)
207 catch (IOException ignored
)
213 out
.println("} ServerHello;");
214 return str
.toString();