1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.tlswire.handshake;
18
19import libcore.tlswire.util.IoUtils;
20import java.io.ByteArrayInputStream;
21import java.io.DataInput;
22import java.io.DataInputStream;
23import java.io.IOException;
24
25/**
26 * Handshake Protocol message from TLS 1.2 RFC 5246.
27 */
28public class HandshakeMessage {
29    public static final int TYPE_CLIENT_HELLO = 1;
30
31    public int type;
32    public byte[] body;
33
34    /**
35     * Parses the provided TLS record as a handshake message.
36     */
37    public static HandshakeMessage read(DataInput in) throws IOException {
38        int type = in.readUnsignedByte();
39        HandshakeMessage result;
40        switch (type) {
41            case TYPE_CLIENT_HELLO:
42                result = new ClientHello();
43                break;
44            default:
45                result = new HandshakeMessage();
46                break;
47        }
48        result.type = type;
49        int bodyLength = IoUtils.readUnsignedInt24(in);
50        result.body = new byte[bodyLength];
51        in.readFully(result.body);
52        result.parseBody(new DataInputStream(new ByteArrayInputStream(result.body)));
53        return result;
54    }
55
56    /**
57     * Parses the provided body. The default implementation does nothing.
58     *
59     * @throws IOException if an I/O error occurs.
60     */
61    protected void parseBody(@SuppressWarnings("unused") DataInput in) throws IOException {}
62}
63