OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / main / java / org / apache / harmony / xnet / provider / jsse / ServerHello.java
1 /*
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.apache.harmony.xnet.provider.jsse;
19
20 import java.io.IOException;
21 import java.security.SecureRandom;
22
23 /**
24  *
25  * Represents server hello message.
26  * @see <a href="http://www.ietf.org/rfc/rfc2246.txt">TLS 1.0 spec., 7.4.1.3.
27  * Server hello.</a>
28  */
29 public class ServerHello extends Message {
30
31     /**
32      * Server version
33      */
34     byte[] server_version = new byte[2];
35
36     /**
37      * Random bytes
38      */
39     byte[] random = new byte[32];
40
41     /**
42      * Session id
43      */
44     byte[] session_id;
45
46     /**
47      * Selected cipher suite
48      */
49     CipherSuite cipher_suite;
50
51     /**
52      * Selected compression method
53      */
54     byte compression_method;
55
56     /**
57      * Creates outbound message
58      * @param sr
59      * @param server_version
60      * @param session_id
61      * @param cipher_suite
62      * @param compression_method
63      */
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;
67         sr.nextBytes(random);
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;
77     }
78
79     /**
80      * Creates inbound message
81      * @param in
82      * @param length
83      * @throws IOException
84      */
85     public ServerHello(HandshakeIODataStream in, int length) throws IOException {
86
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");
100         }
101
102     }
103
104     /**
105      * Sends message
106      * @param out
107      */
108     @Override
109     public void send(HandshakeIODataStream out) {
110         out.write(server_version);
111         out.write(random);
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;
117     }
118
119     /**
120      * Returns server random
121      * @return
122      */
123     public byte[] getRandom() {
124         return random;
125     }
126
127     /**
128      * Returns message type
129      * @return
130      */
131     @Override
132     public int getType() {
133         return Handshake.SERVER_HELLO;
134     }
135 }