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.ASN1TaggedObject;
     10 import org.bouncycastle.asn1.DERSequence;
     11 import org.bouncycastle.asn1.DERTaggedObject;
     12 
     13 /**
     14  * The OtherName object.
     15  * <pre>
     16  * OtherName ::= SEQUENCE {
     17  *      type-id    OBJECT IDENTIFIER,
     18  *      value      [0] EXPLICIT ANY DEFINED BY type-id }
     19  * </pre>
     20  */
     21 public class OtherName
     22     extends ASN1Object
     23 {
     24     private final ASN1ObjectIdentifier typeID;
     25     private final ASN1Encodable value;
     26 
     27     /**
     28      * OtherName factory method.
     29      * @param obj the object used to construct an instance of <code>
     30      * OtherName</code>. It must be an instance of <code>OtherName
     31      * </code> or <code>ASN1Sequence</code>.
     32      * @return the instance of <code>OtherName</code> built from the
     33      * supplied object.
     34      * @throws java.lang.IllegalArgumentException if the object passed
     35      * to the factory is not an instance of <code>OtherName</code> or something that
     36      * can be converted into an appropriate <code>ASN1Sequence</code>.
     37      */
     38     public static OtherName getInstance(
     39         Object obj)
     40     {
     41 
     42         if (obj instanceof OtherName)
     43         {
     44             return (OtherName)obj;
     45         }
     46         else if (obj != null)
     47         {
     48             return new OtherName(ASN1Sequence.getInstance(obj));
     49         }
     50 
     51         return null;
     52     }
     53 
     54     /**
     55      * Base constructor.
     56      * @param typeID the type of the other name.
     57      * @param value the ANY object that represents the value.
     58      */
     59     public OtherName(
     60         ASN1ObjectIdentifier typeID,
     61         ASN1Encodable value)
     62     {
     63         this.typeID = typeID;
     64         this.value  = value;
     65     }
     66 
     67     private OtherName(ASN1Sequence seq)
     68     {
     69         this.typeID = ASN1ObjectIdentifier.getInstance(seq.getObjectAt(0));
     70         this.value = ASN1TaggedObject.getInstance(seq.getObjectAt(1)).getObject(); // explicitly tagged
     71     }
     72 
     73     public ASN1ObjectIdentifier getTypeID()
     74     {
     75         return typeID;
     76     }
     77 
     78     public ASN1Encodable getValue()
     79     {
     80         return value;
     81     }
     82 
     83     public ASN1Primitive toASN1Primitive()
     84     {
     85         ASN1EncodableVector v = new ASN1EncodableVector();
     86 
     87         v.add(typeID);
     88         v.add(new DERTaggedObject(true, 0, value));
     89 
     90         return new DERSequence(v);
     91     }
     92 }
     93