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