summaryrefslogtreecommitdiffstats
path: root/bcprov/src/main/java/org/bouncycastle/crypto/tls/ServerNameList.java
blob: 1dc81f011d4b605c48a2b8d3073ba2df0395c8c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package org.bouncycastle.crypto.tls;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;

public class ServerNameList
{
    protected Vector serverNameList;

    /**
     * @param serverNameList a {@link Vector} of {@link ServerName}.
     */
    public ServerNameList(Vector serverNameList)
    {
        if (serverNameList == null || serverNameList.isEmpty())
        {
            throw new IllegalArgumentException("'serverNameList' must not be null or empty");
        }

        this.serverNameList = serverNameList;
    }

    /**
     * @return a {@link Vector} of {@link ServerName}.
     */
    public Vector getServerNameList()
    {
        return serverNameList;
    }

    /**
     * Encode this {@link ServerNameList} to an {@link OutputStream}.
     * 
     * @param output
     *            the {@link OutputStream} to encode to.
     * @throws IOException
     */
    public void encode(OutputStream output) throws IOException
    {
        ByteArrayOutputStream buf = new ByteArrayOutputStream();

        for (int i = 0; i < serverNameList.size(); ++i)
        {
            ServerName entry = (ServerName)serverNameList.elementAt(i);
            entry.encode(buf);
        }

        TlsUtils.checkUint16(buf.size());
        TlsUtils.writeUint16(buf.size(), output);
        buf.writeTo(output);
    }

    /**
     * Parse a {@link ServerNameList} from an {@link InputStream}.
     * 
     * @param input
     *            the {@link InputStream} to parse from.
     * @return a {@link ServerNameList} object.
     * @throws IOException
     */
    public static ServerNameList parse(InputStream input) throws IOException
    {
        int length = TlsUtils.readUint16(input);
        if (length < 1)
        {
            throw new TlsFatalAlert(AlertDescription.decode_error);
        }

        byte[] data = TlsUtils.readFully(length, input);

        ByteArrayInputStream buf = new ByteArrayInputStream(data);

        Vector server_name_list = new Vector();
        while (buf.available() > 0)
        {
            ServerName entry = ServerName.parse(buf);
            server_name_list.addElement(entry);
        }

        return new ServerNameList(server_name_list);
    }
}