Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ServerName.java « tls « crypto « bouncycastle « org « java « main « src « core - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: df9a43952adb4b18cb8c334f8d95fd70e78a8a61 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package org.bouncycastle.crypto.tls;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.bouncycastle.util.Strings;

public class ServerName
{
    protected short nameType;
    protected Object name;

    public ServerName(short nameType, Object name)
    {
        if (!isCorrectType(nameType, name))
        {
            throw new IllegalArgumentException("'name' is not an instance of the correct type");
        }

        this.nameType = nameType;
        this.name = name;
    }

    public short getNameType()
    {
        return nameType;
    }

    public Object getName()
    {
        return name;
    }

    public String getHostName()
    {
        if (!isCorrectType(NameType.host_name, name))
        {
            throw new IllegalStateException("'name' is not a HostName string");
        }
        return (String)name;
    }

    /**
     * Encode this {@link ServerName} to an {@link OutputStream}.
     * 
     * @param output
     *            the {@link OutputStream} to encode to.
     * @throws IOException
     */
    public void encode(OutputStream output) throws IOException
    {
        TlsUtils.writeUint8(nameType, output);

        switch (nameType)
        {
        case NameType.host_name:
            byte[] utf8Encoding = Strings.toUTF8ByteArray((String)name);
            if (utf8Encoding.length < 1)
            {
                throw new TlsFatalAlert(AlertDescription.internal_error);
            }
            TlsUtils.writeOpaque16(utf8Encoding, output);
            break;
        default:
            throw new TlsFatalAlert(AlertDescription.internal_error);
        }
    }

    /**
     * Parse a {@link ServerName} from an {@link InputStream}.
     * 
     * @param input
     *            the {@link InputStream} to parse from.
     * @return a {@link ServerName} object.
     * @throws IOException
     */
    public static ServerName parse(InputStream input) throws IOException
    {
        short name_type = TlsUtils.readUint8(input);
        Object name;

        switch (name_type)
        {
        case NameType.host_name:
        {
            byte[] utf8Encoding = TlsUtils.readOpaque16(input);
            if (utf8Encoding.length < 1)
            {
                throw new TlsFatalAlert(AlertDescription.decode_error);
            }
            name = Strings.fromUTF8ByteArray(utf8Encoding);
            break;
        }
        default:
            throw new TlsFatalAlert(AlertDescription.decode_error);
        }

        return new ServerName(name_type, name);
    }

    protected static boolean isCorrectType(short nameType, Object name)
    {
        switch (nameType)
        {
        case NameType.host_name:
            return name instanceof String;
        default:
            throw new IllegalArgumentException("'name' is an unsupported value");
        }
    }
}