01: package org.bouncycastle.ocsp;
02:
03: import java.io.ByteArrayOutputStream;
04: import java.util.Enumeration;
05: import java.util.HashSet;
06: import java.util.Set;
07:
08: import org.bouncycastle.asn1.DERObjectIdentifier;
09: import org.bouncycastle.asn1.DEROutputStream;
10: import org.bouncycastle.asn1.ocsp.Request;
11: import org.bouncycastle.asn1.x509.X509Extension;
12: import org.bouncycastle.asn1.x509.X509Extensions;
13:
14: public class Req implements java.security.cert.X509Extension {
15: private Request req;
16:
17: public Req(Request req) {
18: this .req = req;
19: }
20:
21: public CertificateID getCertID() {
22: return new CertificateID(req.getReqCert());
23: }
24:
25: public X509Extensions getSingleRequestExtensions() {
26: return req.getSingleRequestExtensions();
27: }
28:
29: /**
30: * RFC 2650 doesn't specify any critical extensions so we return true
31: * if any are encountered.
32: *
33: * @return true if any critical extensions are present.
34: */
35: public boolean hasUnsupportedCriticalExtension() {
36: Set extns = getCriticalExtensionOIDs();
37: if (extns != null && !extns.isEmpty()) {
38: return true;
39: }
40:
41: return false;
42: }
43:
44: private Set getExtensionOIDs(boolean critical) {
45: Set set = new HashSet();
46: X509Extensions extensions = this .getSingleRequestExtensions();
47:
48: if (extensions != null) {
49: Enumeration e = extensions.oids();
50:
51: while (e.hasMoreElements()) {
52: DERObjectIdentifier oid = (DERObjectIdentifier) e
53: .nextElement();
54: X509Extension ext = extensions.getExtension(oid);
55:
56: if (critical == ext.isCritical()) {
57: set.add(oid.getId());
58: }
59: }
60: }
61:
62: return set;
63: }
64:
65: public Set getCriticalExtensionOIDs() {
66: return getExtensionOIDs(true);
67: }
68:
69: public Set getNonCriticalExtensionOIDs() {
70: return getExtensionOIDs(false);
71: }
72:
73: public byte[] getExtensionValue(String oid) {
74: X509Extensions exts = this .getSingleRequestExtensions();
75:
76: if (exts != null) {
77: X509Extension ext = exts
78: .getExtension(new DERObjectIdentifier(oid));
79:
80: if (ext != null) {
81: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
82: DEROutputStream dOut = new DEROutputStream(bOut);
83:
84: try {
85: dOut.writeObject(ext.getValue());
86:
87: return bOut.toByteArray();
88: } catch (Exception e) {
89: throw new RuntimeException("error encoding "
90: + e.toString());
91: }
92: }
93: }
94:
95: return null;
96: }
97: }
|