Home | History | Annotate | Download | only in x509
      1 package org.bouncycastle.asn1.x509;
      2 
      3 import org.bouncycastle.asn1.ASN1Choice;
      4 import org.bouncycastle.asn1.ASN1Encodable;
      5 import org.bouncycastle.asn1.ASN1Object;
      6 import org.bouncycastle.asn1.ASN1Primitive;
      7 import org.bouncycastle.asn1.ASN1Set;
      8 import org.bouncycastle.asn1.ASN1TaggedObject;
      9 import org.bouncycastle.asn1.DERTaggedObject;
     10 import org.bouncycastle.util.Strings;
     11 
     12 /**
     13  * The DistributionPointName object.
     14  * <pre>
     15  * DistributionPointName ::= CHOICE {
     16  *     fullName                 [0] GeneralNames,
     17  *     nameRelativeToCRLIssuer  [1] RDN
     18  * }
     19  * </pre>
     20  */
     21 public class DistributionPointName
     22     extends ASN1Object
     23     implements ASN1Choice
     24 {
     25     ASN1Encodable        name;
     26     int                 type;
     27 
     28     public static final int FULL_NAME = 0;
     29     public static final int NAME_RELATIVE_TO_CRL_ISSUER = 1;
     30 
     31     public static DistributionPointName getInstance(
     32         ASN1TaggedObject obj,
     33         boolean          explicit)
     34     {
     35         return getInstance(ASN1TaggedObject.getInstance(obj, true));
     36     }
     37 
     38     public static DistributionPointName getInstance(
     39         Object  obj)
     40     {
     41         if (obj == null || obj instanceof DistributionPointName)
     42         {
     43             return (DistributionPointName)obj;
     44         }
     45         else if (obj instanceof ASN1TaggedObject)
     46         {
     47             return new DistributionPointName((ASN1TaggedObject)obj);
     48         }
     49 
     50         throw new IllegalArgumentException("unknown object in factory: " + obj.getClass().getName());
     51     }
     52 
     53     public DistributionPointName(
     54         int             type,
     55         ASN1Encodable   name)
     56     {
     57         this.type = type;
     58         this.name = name;
     59     }
     60 
     61     public DistributionPointName(
     62         GeneralNames name)
     63     {
     64         this(FULL_NAME, name);
     65     }
     66 
     67     /**
     68      * Return the tag number applying to the underlying choice.
     69      *
     70      * @return the tag number for this point name.
     71      */
     72     public int getType()
     73     {
     74         return this.type;
     75     }
     76 
     77     /**
     78      * Return the tagged object inside the distribution point name.
     79      *
     80      * @return the underlying choice item.
     81      */
     82     public ASN1Encodable getName()
     83     {
     84         return (ASN1Encodable)name;
     85     }
     86 
     87     public DistributionPointName(
     88         ASN1TaggedObject    obj)
     89     {
     90         this.type = obj.getTagNo();
     91 
     92         if (type == 0)
     93         {
     94             this.name = GeneralNames.getInstance(obj, false);
     95         }
     96         else
     97         {
     98             this.name = ASN1Set.getInstance(obj, false);
     99         }
    100     }
    101 
    102     public ASN1Primitive toASN1Primitive()
    103     {
    104         return new DERTaggedObject(false, type, name);
    105     }
    106 
    107     public String toString()
    108     {
    109         String       sep = Strings.lineSeparator();
    110         StringBuffer buf = new StringBuffer();
    111         buf.append("DistributionPointName: [");
    112         buf.append(sep);
    113         if (type == FULL_NAME)
    114         {
    115             appendObject(buf, sep, "fullName", name.toString());
    116         }
    117         else
    118         {
    119             appendObject(buf, sep, "nameRelativeToCRLIssuer", name.toString());
    120         }
    121         buf.append("]");
    122         buf.append(sep);
    123         return buf.toString();
    124     }
    125 
    126     private void appendObject(StringBuffer buf, String sep, String name, String value)
    127     {
    128         String       indent = "    ";
    129 
    130         buf.append(indent);
    131         buf.append(name);
    132         buf.append(":");
    133         buf.append(sep);
    134         buf.append(indent);
    135         buf.append(indent);
    136         buf.append(value);
    137         buf.append(sep);
    138     }
    139 }
    140