01: package org.bouncycastle.asn1.x509;
02:
03: import org.bouncycastle.asn1.DEREncodable;
04: import org.bouncycastle.asn1.DERObjectIdentifier;
05: import org.bouncycastle.asn1.DEROctetString;
06: import org.bouncycastle.asn1.DEROutputStream;
07:
08: import java.io.ByteArrayOutputStream;
09: import java.io.IOException;
10: import java.util.Hashtable;
11: import java.util.Vector;
12:
13: /**
14: * Generator for X.509 extensions
15: */
16: public class X509ExtensionsGenerator {
17: private Hashtable extensions = new Hashtable();
18: private Vector extOrdering = new Vector();
19:
20: /**
21: * Reset the generator
22: */
23: public void reset() {
24: extensions = new Hashtable();
25: extOrdering = new Vector();
26: }
27:
28: /**
29: * Add an extension with the given oid and the passed in value to be included
30: * in the OCTET STRING associated with the extension.
31: *
32: * @param oid OID for the extension.
33: * @param critical true if critical, false otherwise.
34: * @param value the ASN.1 object to be included in the extension.
35: */
36: public void addExtension(DERObjectIdentifier oid, boolean critical,
37: DEREncodable value) {
38: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
39: DEROutputStream dOut = new DEROutputStream(bOut);
40:
41: try {
42: dOut.writeObject(value);
43: } catch (IOException e) {
44: throw new IllegalArgumentException("error encoding value: "
45: + e);
46: }
47:
48: this .addExtension(oid, critical, bOut.toByteArray());
49: }
50:
51: /**
52: * Add an extension with the given oid and the passed in byte array to be wrapped in the
53: * OCTET STRING associated with the extension.
54: *
55: * @param oid OID for the extension.
56: * @param critical true if critical, false otherwise.
57: * @param value the byte array to be wrapped.
58: */
59: public void addExtension(DERObjectIdentifier oid, boolean critical,
60: byte[] value) {
61: if (extensions.containsKey(oid)) {
62: throw new IllegalArgumentException("extension " + oid
63: + " already added");
64: }
65:
66: extOrdering.addElement(oid);
67: extensions.put(oid, new X509Extension(critical,
68: new DEROctetString(value)));
69: }
70:
71: /**
72: * Return true if there are no extension present in this generator.
73: *
74: * @return true if empty, false otherwise
75: */
76: public boolean isEmpty() {
77: return extOrdering.isEmpty();
78: }
79:
80: /**
81: * Generate an X509Extensions object based on the current state of the generator.
82: *
83: * @return an X09Extensions object.
84: */
85: public X509Extensions generate() {
86: return new X509Extensions(extOrdering, extensions);
87: }
88: }
|