001: package org.bouncycastle.ocsp;
002:
003: import org.bouncycastle.asn1.DERObjectIdentifier;
004: import org.bouncycastle.asn1.DEROutputStream;
005: import org.bouncycastle.asn1.ocsp.CertStatus;
006: import org.bouncycastle.asn1.ocsp.RevokedInfo;
007: import org.bouncycastle.asn1.ocsp.SingleResponse;
008: import org.bouncycastle.asn1.x509.X509Extension;
009: import org.bouncycastle.asn1.x509.X509Extensions;
010:
011: import java.io.ByteArrayOutputStream;
012: import java.text.ParseException;
013: import java.util.Date;
014: import java.util.Enumeration;
015: import java.util.HashSet;
016: import java.util.Set;
017:
018: public class SingleResp implements java.security.cert.X509Extension {
019: SingleResponse resp;
020:
021: public SingleResp(SingleResponse resp) {
022: this .resp = resp;
023: }
024:
025: public CertificateID getCertID() {
026: return new CertificateID(resp.getCertID());
027: }
028:
029: /**
030: * Return the status object for the response - null indicates good.
031: *
032: * @return the status object for the response, null if it is good.
033: */
034: public Object getCertStatus() {
035: CertStatus s = resp.getCertStatus();
036:
037: if (s.getTagNo() == 0) {
038: return null; // good
039: } else if (s.getTagNo() == 1) {
040: return new RevokedStatus(RevokedInfo.getInstance(s
041: .getStatus()));
042: }
043:
044: return new UnknownStatus();
045: }
046:
047: public Date getThisUpdate() {
048: try {
049: return resp.getThisUpdate().getDate();
050: } catch (ParseException e) {
051: throw new IllegalStateException("ParseException: "
052: + e.getMessage());
053: }
054: }
055:
056: /**
057: * return the NextUpdate value - note: this is an optional field so may
058: * be returned as null.
059: *
060: * @return nextUpdate, or null if not present.
061: */
062: public Date getNextUpdate() {
063: if (resp.getNextUpdate() == null) {
064: return null;
065: }
066:
067: try {
068: return resp.getNextUpdate().getDate();
069: } catch (ParseException e) {
070: throw new IllegalStateException("ParseException: "
071: + e.getMessage());
072: }
073: }
074:
075: public X509Extensions getSingleExtensions() {
076: return resp.getSingleExtensions();
077: }
078:
079: /**
080: * RFC 2650 doesn't specify any critical extensions so we return true
081: * if any are encountered.
082: *
083: * @return true if any critical extensions are present.
084: */
085: public boolean hasUnsupportedCriticalExtension() {
086: Set extns = getCriticalExtensionOIDs();
087:
088: return extns != null && !extns.isEmpty();
089: }
090:
091: private Set getExtensionOIDs(boolean critical) {
092: Set set = new HashSet();
093: X509Extensions extensions = this .getSingleExtensions();
094:
095: if (extensions != null) {
096: Enumeration e = extensions.oids();
097:
098: while (e.hasMoreElements()) {
099: DERObjectIdentifier oid = (DERObjectIdentifier) e
100: .nextElement();
101: X509Extension ext = extensions.getExtension(oid);
102:
103: if (critical == ext.isCritical()) {
104: set.add(oid.getId());
105: }
106: }
107: }
108:
109: return set;
110: }
111:
112: public Set getCriticalExtensionOIDs() {
113: return getExtensionOIDs(true);
114: }
115:
116: public Set getNonCriticalExtensionOIDs() {
117: return getExtensionOIDs(false);
118: }
119:
120: public byte[] getExtensionValue(String oid) {
121: X509Extensions exts = this .getSingleExtensions();
122:
123: if (exts != null) {
124: X509Extension ext = exts
125: .getExtension(new DERObjectIdentifier(oid));
126:
127: if (ext != null) {
128: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
129: DEROutputStream dOut = new DEROutputStream(bOut);
130:
131: try {
132: dOut.writeObject(ext.getValue());
133:
134: return bOut.toByteArray();
135: } catch (Exception e) {
136: throw new RuntimeException("error encoding "
137: + e.toString());
138: }
139: }
140: }
141:
142: return null;
143: }
144: }
|