1package org.bouncycastle.cert.ocsp;
2
3import java.io.ByteArrayInputStream;
4import java.io.IOException;
5import java.io.InputStream;
6
7import org.bouncycastle.asn1.ASN1Exception;
8import org.bouncycastle.asn1.ASN1InputStream;
9import org.bouncycastle.asn1.ASN1Primitive;
10import org.bouncycastle.asn1.ocsp.BasicOCSPResponse;
11import org.bouncycastle.asn1.ocsp.OCSPObjectIdentifiers;
12import org.bouncycastle.asn1.ocsp.OCSPResponse;
13import org.bouncycastle.asn1.ocsp.ResponseBytes;
14import org.bouncycastle.cert.CertIOException;
15
16public class OCSPResp
17{
18    public static final int SUCCESSFUL = 0;  // Response has valid confirmations
19    public static final int MALFORMED_REQUEST = 1;  // Illegal confirmation request
20    public static final int INTERNAL_ERROR = 2;  // Internal error in issuer
21    public static final int TRY_LATER = 3;  // Try again later
22    // (4) is not used
23    public static final int SIG_REQUIRED = 5;  // Must sign the request
24    public static final int UNAUTHORIZED = 6;  // Request unauthorized
25
26    private OCSPResponse    resp;
27
28    public OCSPResp(
29        OCSPResponse    resp)
30    {
31        this.resp = resp;
32    }
33
34    public OCSPResp(
35        byte[]          resp)
36        throws IOException
37    {
38        this(new ByteArrayInputStream(resp));
39    }
40
41    public OCSPResp(
42        InputStream resp)
43        throws IOException
44    {
45        this(new ASN1InputStream(resp));
46    }
47
48    private OCSPResp(
49        ASN1InputStream aIn)
50        throws IOException
51    {
52        try
53        {
54            this.resp = OCSPResponse.getInstance(aIn.readObject());
55        }
56        catch (IllegalArgumentException e)
57        {
58            throw new CertIOException("malformed response: " + e.getMessage(), e);
59        }
60        catch (ClassCastException e)
61        {
62            throw new CertIOException("malformed response: " + e.getMessage(), e);
63        }
64        catch (ASN1Exception e)
65        {
66            throw new CertIOException("malformed response: " + e.getMessage(), e);
67        }
68
69        if (resp == null)
70        {
71            throw new CertIOException("malformed response: no response data found");
72        }
73    }
74
75    public int getStatus()
76    {
77        return this.resp.getResponseStatus().getValue().intValue();
78    }
79
80    public Object getResponseObject()
81        throws OCSPException
82    {
83        ResponseBytes   rb = this.resp.getResponseBytes();
84
85        if (rb == null)
86        {
87            return null;
88        }
89
90        if (rb.getResponseType().equals(OCSPObjectIdentifiers.id_pkix_ocsp_basic))
91        {
92            try
93            {
94                ASN1Primitive obj = ASN1Primitive.fromByteArray(rb.getResponse().getOctets());
95                return new BasicOCSPResp(BasicOCSPResponse.getInstance(obj));
96            }
97            catch (Exception e)
98            {
99                throw new OCSPException("problem decoding object: " + e, e);
100            }
101        }
102
103        return rb.getResponse();
104    }
105
106    /**
107     * return the ASN.1 encoded representation of this object.
108     */
109    public byte[] getEncoded()
110        throws IOException
111    {
112        return resp.getEncoded();
113    }
114
115    public boolean equals(Object o)
116    {
117        if (o == this)
118        {
119            return true;
120        }
121
122        if (!(o instanceof OCSPResp))
123        {
124            return false;
125        }
126
127        OCSPResp r = (OCSPResp)o;
128
129        return resp.equals(r.resp);
130    }
131
132    public int hashCode()
133    {
134        return resp.hashCode();
135    }
136
137    public OCSPResponse toASN1Structure()
138    {
139        return resp;
140    }
141}
142