2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.harmony.xnet.provider.jsse;
20 import java.io.IOException;
21 import java.security.SecureRandom;
25 * Represents server hello message.
26 * @see <a href="http://www.ietf.org/rfc/rfc2246.txt">TLS 1.0 spec., 7.4.1.3.
29 public class ServerHello extends Message {
34 byte[] server_version = new byte[2];
39 byte[] random = new byte[32];
47 * Selected cipher suite
49 CipherSuite cipher_suite;
52 * Selected compression method
54 byte compression_method;
57 * Creates outbound message
59 * @param server_version
62 * @param compression_method
64 public ServerHello(SecureRandom sr, byte[] server_version,
65 byte[] session_id, CipherSuite cipher_suite, byte compression_method) {
66 long gmt_unix_time = new java.util.Date().getTime() / 1000;
68 random[0] = (byte) ((gmt_unix_time & 0xFF000000) >>> 24);
69 random[1] = (byte) ((gmt_unix_time & 0xFF0000) >>> 16);
70 random[2] = (byte) ((gmt_unix_time & 0xFF00) >>> 8);
71 random[3] = (byte) (gmt_unix_time & 0xFF);
72 this.session_id = session_id;
73 this.cipher_suite = cipher_suite;
74 this.compression_method = compression_method;
75 this.server_version = server_version;
76 length = 38 + session_id.length;
80 * Creates inbound message
85 public ServerHello(HandshakeIODataStream in, int length) throws IOException {
87 server_version[0] = (byte) in.read();
88 server_version[1] = (byte) in.read();
89 in.read(random, 0, 32);
90 int size = in.readUint8();
91 session_id = new byte[size];
92 in.read(session_id, 0, size);
93 byte b0 = (byte) in.read();
94 byte b1 = (byte) in.read();
95 cipher_suite = CipherSuite.getByCode(b0, b1);
96 compression_method = (byte) in.read();
97 this.length = 38 + session_id.length;
98 if (this.length != length) {
99 fatalAlert(AlertProtocol.DECODE_ERROR, "DECODE ERROR: incorrect ServerHello");
109 public void send(HandshakeIODataStream out) {
110 out.write(server_version);
112 out.writeUint8(session_id.length);
113 out.write(session_id);
114 out.write(cipher_suite.toBytes());
115 out.write(compression_method);
116 length = 38 + session_id.length;
120 * Returns server random
123 public byte[] getRandom() {
128 * Returns message type
132 public int getType() {
133 return Handshake.SERVER_HELLO;