01: package org.bouncycastle.ocsp;
02:
03: import java.io.ByteArrayOutputStream;
04: import java.io.IOException;
05: import java.io.InputStream;
06:
07: import org.bouncycastle.asn1.ASN1InputStream;
08: import org.bouncycastle.asn1.ASN1OutputStream;
09: import org.bouncycastle.asn1.ocsp.BasicOCSPResponse;
10: import org.bouncycastle.asn1.ocsp.OCSPObjectIdentifiers;
11: import org.bouncycastle.asn1.ocsp.OCSPResponse;
12: import org.bouncycastle.asn1.ocsp.ResponseBytes;
13:
14: public class OCSPResp {
15: private OCSPResponse resp;
16:
17: public OCSPResp(OCSPResponse resp) {
18: this .resp = resp;
19: }
20:
21: public OCSPResp(byte[] resp) throws IOException {
22: this (new ASN1InputStream(resp));
23: }
24:
25: public OCSPResp(InputStream in) throws IOException {
26: this (new ASN1InputStream(in));
27: }
28:
29: private OCSPResp(ASN1InputStream aIn) throws IOException {
30: try {
31: this .resp = OCSPResponse.getInstance(aIn.readObject());
32: } catch (IllegalArgumentException e) {
33: throw new IOException("malformed response: "
34: + e.getMessage());
35: } catch (ClassCastException e) {
36: throw new IOException("malformed response: "
37: + e.getMessage());
38: }
39: }
40:
41: public int getStatus() {
42: return this .resp.getResponseStatus().getValue().intValue();
43: }
44:
45: public Object getResponseObject() throws OCSPException {
46: ResponseBytes rb = this .resp.getResponseBytes();
47:
48: if (rb == null) {
49: return null;
50: }
51:
52: if (rb.getResponseType().equals(
53: OCSPObjectIdentifiers.id_pkix_ocsp_basic)) {
54: try {
55: ASN1InputStream aIn = new ASN1InputStream(rb
56: .getResponse().getOctets());
57: return new BasicOCSPResp(BasicOCSPResponse
58: .getInstance(aIn.readObject()));
59: } catch (Exception e) {
60: throw new OCSPException(
61: "problem decoding object: " + e, e);
62: }
63: }
64:
65: return rb.getResponse();
66: }
67:
68: /**
69: * return the ASN.1 encoded representation of this object.
70: */
71: public byte[] getEncoded() throws IOException {
72: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
73: ASN1OutputStream aOut = new ASN1OutputStream(bOut);
74:
75: aOut.writeObject(resp);
76:
77: return bOut.toByteArray();
78: }
79:
80: public boolean equals(Object o) {
81: if (o == this ) {
82: return true;
83: }
84:
85: if (!(o instanceof OCSPResp)) {
86: return false;
87: }
88:
89: OCSPResp r = (OCSPResp) o;
90:
91: return resp.equals(r.resp);
92: }
93:
94: public int hashCode() {
95: return resp.hashCode();
96: }
97: }
|