001: package org.bouncycastle.ocsp;
002:
003: import org.bouncycastle.asn1.ASN1Sequence;
004: import org.bouncycastle.asn1.DERObjectIdentifier;
005: import org.bouncycastle.asn1.DEROutputStream;
006: import org.bouncycastle.asn1.ocsp.ResponseData;
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 RespData implements java.security.cert.X509Extension {
019: ResponseData data;
020:
021: public RespData(ResponseData data) {
022: this .data = data;
023: }
024:
025: public int getVersion() {
026: return data.getVersion().getValue().intValue() + 1;
027: }
028:
029: public RespID getResponderId() {
030: return new RespID(data.getResponderID());
031: }
032:
033: public Date getProducedAt() {
034: try {
035: return data.getProducedAt().getDate();
036: } catch (ParseException e) {
037: throw new IllegalStateException("ParseException:"
038: + e.getMessage());
039: }
040: }
041:
042: public SingleResp[] getResponses() {
043: ASN1Sequence s = data.getResponses();
044: SingleResp[] rs = new SingleResp[s.size()];
045:
046: for (int i = 0; i != rs.length; i++) {
047: rs[i] = new SingleResp(SingleResponse.getInstance(s
048: .getObjectAt(i)));
049: }
050:
051: return rs;
052: }
053:
054: public X509Extensions getResponseExtensions() {
055: return data.getResponseExtensions();
056: }
057:
058: /**
059: * RFC 2650 doesn't specify any critical extensions so we return true
060: * if any are encountered.
061: *
062: * @return true if any critical extensions are present.
063: */
064: public boolean hasUnsupportedCriticalExtension() {
065: Set extns = getCriticalExtensionOIDs();
066: if (extns != null && !extns.isEmpty()) {
067: return true;
068: }
069:
070: return false;
071: }
072:
073: private Set getExtensionOIDs(boolean critical) {
074: Set set = new HashSet();
075: X509Extensions extensions = this .getResponseExtensions();
076:
077: if (extensions != null) {
078: Enumeration e = extensions.oids();
079:
080: while (e.hasMoreElements()) {
081: DERObjectIdentifier oid = (DERObjectIdentifier) e
082: .nextElement();
083: X509Extension ext = extensions.getExtension(oid);
084:
085: if (critical == ext.isCritical()) {
086: set.add(oid.getId());
087: }
088: }
089: }
090:
091: return set;
092: }
093:
094: public Set getCriticalExtensionOIDs() {
095: return getExtensionOIDs(true);
096: }
097:
098: public Set getNonCriticalExtensionOIDs() {
099: return getExtensionOIDs(false);
100: }
101:
102: public byte[] getExtensionValue(String oid) {
103: X509Extensions exts = this .getResponseExtensions();
104:
105: if (exts != null) {
106: X509Extension ext = exts
107: .getExtension(new DERObjectIdentifier(oid));
108:
109: if (ext != null) {
110: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
111: DEROutputStream dOut = new DEROutputStream(bOut);
112:
113: try {
114: dOut.writeObject(ext.getValue());
115:
116: return bOut.toByteArray();
117: } catch (Exception e) {
118: throw new RuntimeException("error encoding "
119: + e.toString());
120: }
121: }
122: }
123:
124: return null;
125: }
126: }
|