1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45:
46:
65: public class ServerHello implements Handshake.Body
66: {
67:
68:
69:
70:
71: protected static final int RANDOM_OFFSET = 2;
72: protected static final int SESSID_OFFSET = 32 + RANDOM_OFFSET;
73: protected static final int SESSID_OFFSET2 = SESSID_OFFSET + 1;
74:
75: protected ByteBuffer buffer;
76: protected boolean disableExtensions;
77:
78:
79:
80:
81: public ServerHello (final ByteBuffer buffer)
82: {
83: this.buffer = buffer;
84: disableExtensions = false;
85: }
86:
87: public int length ()
88: {
89: int sessionLen = buffer.get(SESSID_OFFSET) & 0xFF;
90: int len = SESSID_OFFSET2 + sessionLen + 3;
91: int elen = 0;
92: if (!disableExtensions && len + 1 < buffer.limit()
93: && (elen = buffer.getShort(len)) != 0)
94: len += 2 + elen;
95: return len;
96: }
97:
98:
107: public ProtocolVersion version()
108: {
109: return ProtocolVersion.getInstance (buffer.getShort (0));
110: }
111:
112:
120: public Random random()
121: {
122: ByteBuffer randomBuf =
123: ((ByteBuffer) buffer.duplicate ().position (RANDOM_OFFSET)
124: .limit (SESSID_OFFSET)).slice ();
125: return new Random (randomBuf);
126: }
127:
128:
134: public byte[] sessionId()
135: {
136: int idlen = buffer.get (SESSID_OFFSET) & 0xFF;
137: byte[] sessionId = new byte[idlen];
138: buffer.position (SESSID_OFFSET2);
139: buffer.get (sessionId);
140: return sessionId;
141: }
142:
143:
149: public CipherSuite cipherSuite()
150: {
151: int offset = SESSID_OFFSET2 + (buffer.get(SESSID_OFFSET) & 0xFF);
152: return CipherSuite.forValue(buffer.getShort(offset)).resolve();
153: }
154:
155:
160: public CompressionMethod compressionMethod()
161: {
162: int offset = SESSID_OFFSET2 + (buffer.get(SESSID_OFFSET) & 0xFF) + 2;
163: return CompressionMethod.getInstance(buffer.get(offset) & 0xFF);
164: }
165:
166: public int extensionsLength()
167: {
168: int offset = SESSID_OFFSET2 + (buffer.get (SESSID_OFFSET) & 0xFF) + 3;
169: if (offset + 1 >= buffer.limit())
170: return 0;
171: return buffer.getShort(offset) & 0xFFFF;
172: }
173:
174: public ExtensionList extensions ()
175: {
176: int offset = SESSID_OFFSET2 + (buffer.get (SESSID_OFFSET) & 0xFF) + 3;
177: if (offset + 1 >= buffer.limit())
178: return null;
179: int len = buffer.getShort(offset) & 0xFFFF;
180: if (len == 0)
181: len = buffer.limit() - offset - 2;
182: ByteBuffer ebuf = ((ByteBuffer) buffer.duplicate().position(offset)
183: .limit(offset + len + 2)).slice();
184: return new ExtensionList(ebuf);
185: }
186:
187: public String toString()
188: {
189: return toString(null);
190: }
191:
192: public String toString (final String prefix)
193: {
194: StringWriter str = new StringWriter();
195: PrintWriter out = new PrintWriter(str);
196: if (prefix != null)
197: out.print (prefix);
198: out.println ("struct {");
199: String subprefix = " ";
200: if (prefix != null)
201: subprefix += prefix;
202: out.print (subprefix);
203: out.print ("version: ");
204: out.print (version ());
205: out.println (";");
206: out.print (subprefix);
207: out.println ("random:");
208: out.println (random ().toString (subprefix));
209: out.print (subprefix);
210: out.print ("sessionId: ");
211: out.print (Util.toHexString(sessionId (), ':'));
212: out.println (";");
213: out.print (subprefix);
214: out.print ("cipherSuite: ");
215: out.print (cipherSuite ());
216: out.println (";");
217: out.print (subprefix);
218: out.print ("compressionMethod: ");
219: out.print (compressionMethod ());
220: out.println (";");
221: ExtensionList exts = extensions ();
222: out.print (subprefix);
223: out.println ("extensions:");
224: out.println (exts != null ? exts.toString (subprefix+" ")
225: : subprefix + " (nil)");
226: if (prefix != null)
227: out.print (prefix);
228: out.print ("} ServerHello;");
229: return str.toString();
230: }
231: }