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

URLAndHash.java « tls « crypto « spongycastle « org « java « main « src « core - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 05407842eaf874232cd67be1f848a6e2eb98ff95 (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
package org.spongycastle.crypto.tls;

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

import org.spongycastle.util.Strings;

/**
 * RFC 6066 5.
 */
public class URLAndHash
{
    protected String url;
    protected byte[] sha1Hash;

    public URLAndHash(String url, byte[] sha1Hash)
    {
        if (url == null || url.length() < 1 || url.length() >= (1 << 16))
        {
            throw new IllegalArgumentException("'url' must have length from 1 to (2^16 - 1)");
        }
        if (sha1Hash != null && sha1Hash.length != 20)
        {
            throw new IllegalArgumentException("'sha1Hash' must have length == 20, if present");
        }

        this.url = url;
        this.sha1Hash = sha1Hash;
    }

    public String getURL()
    {
        return url;
    }

    public byte[] getSHA1Hash()
    {
        return sha1Hash;
    }

    /**
     * Encode this {@link URLAndHash} to an {@link OutputStream}.
     *
     * @param output the {@link OutputStream} to encode to.
     * @throws IOException
     */
    public void encode(OutputStream output)
        throws IOException
    {
        byte[] urlEncoding = Strings.toByteArray(this.url);
        TlsUtils.writeOpaque16(urlEncoding, output);

        if (this.sha1Hash == null)
        {
            TlsUtils.writeUint8(0, output);
        }
        else
        {
            TlsUtils.writeUint8(1, output);
            output.write(this.sha1Hash);
        }
    }

    /**
     * Parse a {@link URLAndHash} from an {@link InputStream}.
     * 
     * @param context
     *            the {@link TlsContext} of the current connection.
     * @param input
     *            the {@link InputStream} to parse from.
     * @return a {@link URLAndHash} object.
     * @throws IOException
     */
    public static URLAndHash parse(TlsContext context, InputStream input)
        throws IOException
    {
        byte[] urlEncoding = TlsUtils.readOpaque16(input);
        if (urlEncoding.length < 1)
        {
            throw new TlsFatalAlert(AlertDescription.illegal_parameter);
        }
        String url = Strings.fromByteArray(urlEncoding);

        byte[] sha1Hash = null;
        short padding = TlsUtils.readUint8(input);
        switch (padding)
        {
        case 0:
            if (TlsUtils.isTLSv12(context))
            {
                throw new TlsFatalAlert(AlertDescription.illegal_parameter);
            }
            break;
        case 1:
            sha1Hash = TlsUtils.readFully(20, input);
            break;
        default:
            throw new TlsFatalAlert(AlertDescription.illegal_parameter);
        }

        return new URLAndHash(url, sha1Hash);
    }
}