001package org.apache.commons.ssl.org.bouncycastle.asn1.x509;
002
003import java.io.IOException;
004import java.util.Hashtable;
005import java.util.Vector;
006
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Encodable;
008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Encoding;
009import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1ObjectIdentifier;
010import org.apache.commons.ssl.org.bouncycastle.asn1.DEROctetString;
011
012/**
013 * Generator for X.509 extensions
014 */
015public class ExtensionsGenerator
016{
017    private Hashtable extensions = new Hashtable();
018    private Vector extOrdering = new Vector();
019
020    /**
021     * Reset the generator
022     */
023    public void reset()
024    {
025        extensions = new Hashtable();
026        extOrdering = new Vector();
027    }
028
029    /**
030     * Add an extension with the given oid and the passed in value to be included
031     * in the OCTET STRING associated with the extension.
032     *
033     * @param oid  OID for the extension.
034     * @param critical  true if critical, false otherwise.
035     * @param value the ASN.1 object to be included in the extension.
036     */
037    public void addExtension(
038        ASN1ObjectIdentifier oid,
039        boolean              critical,
040        ASN1Encodable        value)
041        throws IOException
042    {
043        this.addExtension(oid, critical, value.toASN1Primitive().getEncoded(ASN1Encoding.DER));
044    }
045
046    /**
047     * Add an extension with the given oid and the passed in byte array to be wrapped in the
048     * OCTET STRING associated with the extension.
049     *
050     * @param oid OID for the extension.
051     * @param critical true if critical, false otherwise.
052     * @param value the byte array to be wrapped.
053     */
054    public void addExtension(
055        ASN1ObjectIdentifier oid,
056        boolean             critical,
057        byte[]              value)
058    {
059        if (extensions.containsKey(oid))
060        {
061            throw new IllegalArgumentException("extension " + oid + " already added");
062        }
063
064        extOrdering.addElement(oid);
065        extensions.put(oid, new Extension(oid, critical, new DEROctetString(value)));
066    }
067
068    /**
069     * Return true if there are no extension present in this generator.
070     *
071     * @return true if empty, false otherwise
072     */
073    public boolean isEmpty()
074    {
075        return extOrdering.isEmpty();
076    }
077
078    /**
079     * Generate an Extensions object based on the current state of the generator.
080     *
081     * @return  an X09Extensions object.
082     */
083    public Extensions generate()
084    {
085        Extension[] exts = new Extension[extOrdering.size()];
086
087        for (int i = 0; i != extOrdering.size(); i++)
088        {
089            exts[i] = (Extension)extensions.get(extOrdering.elementAt(i));
090        }
091
092        return new Extensions(exts);
093    }
094}