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 * @deprecated use org.bouncycastle.asn1.x509.ExtensionsGenerator
015 */
016public class X509ExtensionsGenerator
017{
018    private Hashtable extensions = new Hashtable();
019    private Vector extOrdering = new Vector();
020
021    /**
022     * Reset the generator
023     */
024    public void reset()
025    {
026        extensions = new Hashtable();
027        extOrdering = new Vector();
028    }
029
030    /**
031     * Add an extension with the given oid and the passed in value to be included
032     * in the OCTET STRING associated with the extension.
033     *
034     * @param oid  OID for the extension.
035     * @param critical  true if critical, false otherwise.
036     * @param value the ASN.1 object to be included in the extension.
037     */
038    public void addExtension(
039        ASN1ObjectIdentifier oid,
040        boolean             critical,
041        ASN1Encodable       value)
042    {
043        try
044        {
045            this.addExtension(oid, critical, value.toASN1Primitive().getEncoded(ASN1Encoding.DER));
046        }
047        catch (IOException e)
048        {
049            throw new IllegalArgumentException("error encoding value: " + e);
050        }
051    }
052
053    /**
054     * Add an extension with the given oid and the passed in byte array to be wrapped in the
055     * OCTET STRING associated with the extension.
056     *
057     * @param oid OID for the extension.
058     * @param critical true if critical, false otherwise.
059     * @param value the byte array to be wrapped.
060     */
061    public void addExtension(
062        ASN1ObjectIdentifier oid,
063        boolean             critical,
064        byte[]              value)
065    {
066        if (extensions.containsKey(oid))
067        {
068            throw new IllegalArgumentException("extension " + oid + " already added");
069        }
070
071        extOrdering.addElement(oid);
072        extensions.put(oid, new X509Extension(critical, new DEROctetString(value)));
073    }
074
075    /**
076     * Return true if there are no extension present in this generator.
077     *
078     * @return true if empty, false otherwise
079     */
080    public boolean isEmpty()
081    {
082        return extOrdering.isEmpty();
083    }
084
085    /**
086     * Generate an X509Extensions object based on the current state of the generator.
087     *
088     * @return  an X09Extensions object.
089     */
090    public X509Extensions generate()
091    {
092        return new X509Extensions(extOrdering, extensions);
093    }
094}