Home | History | Annotate | Download | only in x509
      1 package org.bouncycastle.asn1.x509;
      2 
      3 import java.util.Enumeration;
      4 import java.util.Vector;
      5 
      6 import org.bouncycastle.asn1.ASN1Encodable;
      7 import org.bouncycastle.asn1.ASN1EncodableVector;
      8 import org.bouncycastle.asn1.ASN1Sequence;
      9 import org.bouncycastle.asn1.DERObject;
     10 import org.bouncycastle.asn1.DERSequence;
     11 
     12 /**
     13  * This extension may contain further X.500 attributes of the subject. See also
     14  * RFC 3039.
     15  *
     16  * <pre>
     17  *     SubjectDirectoryAttributes ::= Attributes
     18  *     Attributes ::= SEQUENCE SIZE (1..MAX) OF Attribute
     19  *     Attribute ::= SEQUENCE
     20  *     {
     21  *       type AttributeType
     22  *       values SET OF AttributeValue
     23  *     }
     24  *
     25  *     AttributeType ::= OBJECT IDENTIFIER
     26  *     AttributeValue ::= ANY DEFINED BY AttributeType
     27  * </pre>
     28  *
     29  * @see org.bouncycastle.asn1.x509.X509Name for AttributeType ObjectIdentifiers.
     30  */
     31 public class SubjectDirectoryAttributes
     32     extends ASN1Encodable
     33 {
     34     private Vector attributes = new Vector();
     35 
     36     public static SubjectDirectoryAttributes getInstance(
     37         Object obj)
     38     {
     39         if (obj == null || obj instanceof SubjectDirectoryAttributes)
     40         {
     41             return (SubjectDirectoryAttributes)obj;
     42         }
     43 
     44         if (obj instanceof ASN1Sequence)
     45         {
     46             return new SubjectDirectoryAttributes((ASN1Sequence)obj);
     47         }
     48 
     49         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
     50     }
     51 
     52     /**
     53      * Constructor from ASN1Sequence.
     54      *
     55      * The sequence is of type SubjectDirectoryAttributes:
     56      *
     57      * <pre>
     58      *      SubjectDirectoryAttributes ::= Attributes
     59      *      Attributes ::= SEQUENCE SIZE (1..MAX) OF Attribute
     60      *      Attribute ::= SEQUENCE
     61      *      {
     62      *        type AttributeType
     63      *        values SET OF AttributeValue
     64      *      }
     65      *
     66      *      AttributeType ::= OBJECT IDENTIFIER
     67      *      AttributeValue ::= ANY DEFINED BY AttributeType
     68      * </pre>
     69      *
     70      * @param seq
     71      *            The ASN.1 sequence.
     72      */
     73     public SubjectDirectoryAttributes(ASN1Sequence seq)
     74     {
     75         Enumeration e = seq.getObjects();
     76 
     77         while (e.hasMoreElements())
     78         {
     79             ASN1Sequence s = ASN1Sequence.getInstance(e.nextElement());
     80             attributes.addElement(new Attribute(s));
     81         }
     82     }
     83 
     84     /**
     85      * Constructor from a vector of attributes.
     86      *
     87      * The vector consists of attributes of type {@link Attribute Attribute}
     88      *
     89      * @param attributes
     90      *            The attributes.
     91      *
     92      */
     93     public SubjectDirectoryAttributes(Vector attributes)
     94     {
     95         Enumeration e = attributes.elements();
     96 
     97         while (e.hasMoreElements())
     98         {
     99             this.attributes.addElement(e.nextElement());
    100         }
    101     }
    102 
    103     /**
    104      * Produce an object suitable for an ASN1OutputStream.
    105      *
    106      * Returns:
    107      *
    108      * <pre>
    109      *      SubjectDirectoryAttributes ::= Attributes
    110      *      Attributes ::= SEQUENCE SIZE (1..MAX) OF Attribute
    111      *      Attribute ::= SEQUENCE
    112      *      {
    113      *        type AttributeType
    114      *        values SET OF AttributeValue
    115      *      }
    116      *
    117      *      AttributeType ::= OBJECT IDENTIFIER
    118      *      AttributeValue ::= ANY DEFINED BY AttributeType
    119      * </pre>
    120      *
    121      * @return a DERObject
    122      */
    123     public DERObject toASN1Object()
    124     {
    125         ASN1EncodableVector vec = new ASN1EncodableVector();
    126         Enumeration e = attributes.elements();
    127 
    128         while (e.hasMoreElements())
    129         {
    130 
    131             vec.add((Attribute)e.nextElement());
    132         }
    133 
    134         return new DERSequence(vec);
    135     }
    136 
    137     /**
    138      * @return Returns the attributes.
    139      */
    140     public Vector getAttributes()
    141     {
    142         return attributes;
    143     }
    144 }
    145