X509ExtensionsGenerator.java revision e6bf3e8dfa2804891a82075cb469b736321b4827
1package org.bouncycastle.asn1.x509;
2
3import java.io.IOException;
4import java.util.Hashtable;
5import java.util.Vector;
6
7import org.bouncycastle.asn1.ASN1Encodable;
8import org.bouncycastle.asn1.ASN1Encoding;
9import org.bouncycastle.asn1.ASN1ObjectIdentifier;
10import org.bouncycastle.asn1.DERObjectIdentifier;
11import org.bouncycastle.asn1.DEROctetString;
12
13/**
14 * Generator for X.509 extensions
15 * @deprecated use org.bouncycastle.asn1.x509.ExtensionsGenerator
16 */
17public class X509ExtensionsGenerator
18{
19    private Hashtable extensions = new Hashtable();
20    private Vector extOrdering = new Vector();
21
22    /**
23     * Reset the generator
24     */
25    public void reset()
26    {
27        extensions = new Hashtable();
28        extOrdering = new Vector();
29    }
30
31    /**
32     * @deprecated use ASN1ObjectIdentifier
33     */
34    public void addExtension(
35        DERObjectIdentifier oid,
36        boolean             critical,
37        ASN1Encodable       value)
38    {
39        addExtension(new ASN1ObjectIdentifier(oid.getId()), critical, value);
40    }
41
42    /**
43     * @deprecated use ASN1ObjectIdentifier
44     */
45    public void addExtension(
46        DERObjectIdentifier oid,
47        boolean             critical,
48        byte[]              value)
49    {
50        addExtension(new ASN1ObjectIdentifier(oid.getId()), critical, value);
51    }
52
53    /**
54     * Add an extension with the given oid and the passed in value to be included
55     * in the OCTET STRING associated with the extension.
56     *
57     * @param oid  OID for the extension.
58     * @param critical  true if critical, false otherwise.
59     * @param value the ASN.1 object to be included in the extension.
60     */
61    public void addExtension(
62        ASN1ObjectIdentifier oid,
63        boolean             critical,
64        ASN1Encodable       value)
65    {
66        try
67        {
68            this.addExtension(oid, critical, value.toASN1Primitive().getEncoded(ASN1Encoding.DER));
69        }
70        catch (IOException e)
71        {
72            throw new IllegalArgumentException("error encoding value: " + e);
73        }
74    }
75
76    /**
77     * Add an extension with the given oid and the passed in byte array to be wrapped in the
78     * OCTET STRING associated with the extension.
79     *
80     * @param oid OID for the extension.
81     * @param critical true if critical, false otherwise.
82     * @param value the byte array to be wrapped.
83     */
84    public void addExtension(
85        ASN1ObjectIdentifier oid,
86        boolean             critical,
87        byte[]              value)
88    {
89        if (extensions.containsKey(oid))
90        {
91            throw new IllegalArgumentException("extension " + oid + " already added");
92        }
93
94        extOrdering.addElement(oid);
95        extensions.put(oid, new X509Extension(critical, new DEROctetString(value)));
96    }
97
98    /**
99     * Return true if there are no extension present in this generator.
100     *
101     * @return true if empty, false otherwise
102     */
103    public boolean isEmpty()
104    {
105        return extOrdering.isEmpty();
106    }
107
108    /**
109     * Generate an X509Extensions object based on the current state of the generator.
110     *
111     * @return  an X09Extensions object.
112     */
113    public X509Extensions generate()
114    {
115        return new X509Extensions(extOrdering, extensions);
116    }
117}
118