summaryrefslogtreecommitdiffstats
path: root/bcprov/src/main/java/org/bouncycastle/crypto/tls/CertificateStatusRequest.java
blob: b947c48808d4df30862213f2f928fa4b670333f6 (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
package org.bouncycastle.crypto.tls;

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

public class CertificateStatusRequest
{
    protected short statusType;
    protected Object request;

    public CertificateStatusRequest(short statusType, Object request)
    {
        if (!isCorrectType(statusType, request))
        {
            throw new IllegalArgumentException("'request' is not an instance of the correct type");
        }
        
        this.statusType = statusType;
        this.request = request;
    }

    public short getStatusType()
    {
        return statusType;
    }

    public Object getRequest()
    {
        return request;
    }

    public OCSPStatusRequest getOCSPStatusRequest()
    {
        if (!isCorrectType(CertificateStatusType.ocsp, request))
        {
            throw new IllegalStateException("'request' is not an OCSPStatusRequest");
        }
        return (OCSPStatusRequest)request;
    }

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

        switch (statusType)
        {
        case CertificateStatusType.ocsp:
            ((OCSPStatusRequest) request).encode(output);
            break;
        default:
            throw new TlsFatalAlert(AlertDescription.internal_error);
        }
    }

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

        switch (status_type)
        {
        case CertificateStatusType.ocsp:
            result = OCSPStatusRequest.parse(input);
            break;
        default:
            throw new TlsFatalAlert(AlertDescription.decode_error);
        }

        return new CertificateStatusRequest(status_type, result);
    }

    protected static boolean isCorrectType(short statusType, Object request)
    {
        switch (statusType)
        {
        case CertificateStatusType.ocsp:
            return request instanceof OCSPStatusRequest;
        default:
            throw new IllegalArgumentException("'statusType' is an unsupported value");
        }
    }
}