Home | History | Annotate | Download | only in x509
      1 package org.bouncycastle.asn1.x509;
      2 
      3 import org.bouncycastle.asn1.ASN1Encodable;
      4 import org.bouncycastle.asn1.ASN1EncodableVector;
      5 import org.bouncycastle.asn1.ASN1Object;
      6 import org.bouncycastle.asn1.ASN1ObjectIdentifier;
      7 import org.bouncycastle.asn1.ASN1Primitive;
      8 import org.bouncycastle.asn1.ASN1Sequence;
      9 import org.bouncycastle.asn1.ASN1Set;
     10 import org.bouncycastle.asn1.DERSequence;
     11 
     12 public class Attribute
     13     extends ASN1Object
     14 {
     15     private ASN1ObjectIdentifier attrType;
     16     private ASN1Set             attrValues;
     17 
     18     /**
     19      * return an Attribute object from the given object.
     20      *
     21      * @param o the object we want converted.
     22      * @exception IllegalArgumentException if the object cannot be converted.
     23      */
     24     public static Attribute getInstance(
     25         Object o)
     26     {
     27         if (o instanceof Attribute)
     28         {
     29             return (Attribute)o;
     30         }
     31 
     32         if (o != null)
     33         {
     34             return new Attribute(ASN1Sequence.getInstance(o));
     35         }
     36 
     37         return null;
     38     }
     39 
     40     private Attribute(
     41         ASN1Sequence seq)
     42     {
     43         if (seq.size() != 2)
     44         {
     45             throw new IllegalArgumentException("Bad sequence size: " + seq.size());
     46         }
     47 
     48         attrType = ASN1ObjectIdentifier.getInstance(seq.getObjectAt(0));
     49         attrValues = ASN1Set.getInstance(seq.getObjectAt(1));
     50     }
     51 
     52     public Attribute(
     53         ASN1ObjectIdentifier attrType,
     54         ASN1Set             attrValues)
     55     {
     56         this.attrType = attrType;
     57         this.attrValues = attrValues;
     58     }
     59 
     60     public ASN1ObjectIdentifier getAttrType()
     61     {
     62         return new ASN1ObjectIdentifier(attrType.getId());
     63     }
     64 
     65     public ASN1Encodable[] getAttributeValues()
     66     {
     67         return attrValues.toArray();
     68     }
     69 
     70     public ASN1Set getAttrValues()
     71     {
     72         return attrValues;
     73     }
     74 
     75     /**
     76      * Produce an object suitable for an ASN1OutputStream.
     77      * <pre>
     78      * Attribute ::= SEQUENCE {
     79      *     attrType OBJECT IDENTIFIER,
     80      *     attrValues SET OF AttributeValue
     81      * }
     82      * </pre>
     83      */
     84     public ASN1Primitive toASN1Primitive()
     85     {
     86         ASN1EncodableVector v = new ASN1EncodableVector();
     87 
     88         v.add(attrType);
     89         v.add(attrValues);
     90 
     91         return new DERSequence(v);
     92     }
     93 }
     94