Home | History | Annotate | Download | only in x509
      1 package org.bouncycastle.asn1.x509;
      2 
      3 import org.bouncycastle.asn1.ASN1EncodableVector;
      4 import org.bouncycastle.asn1.ASN1Object;
      5 import org.bouncycastle.asn1.ASN1Primitive;
      6 import org.bouncycastle.asn1.ASN1Sequence;
      7 import org.bouncycastle.asn1.ASN1TaggedObject;
      8 import org.bouncycastle.asn1.DERBitString;
      9 import org.bouncycastle.asn1.DERSequence;
     10 import org.bouncycastle.asn1.DERTaggedObject;
     11 import org.bouncycastle.util.Strings;
     12 
     13 /**
     14  * The DistributionPoint object.
     15  * <pre>
     16  * DistributionPoint ::= SEQUENCE {
     17  *      distributionPoint [0] DistributionPointName OPTIONAL,
     18  *      reasons           [1] ReasonFlags OPTIONAL,
     19  *      cRLIssuer         [2] GeneralNames OPTIONAL
     20  * }
     21  * </pre>
     22  */
     23 public class DistributionPoint
     24     extends ASN1Object
     25 {
     26     DistributionPointName       distributionPoint;
     27     ReasonFlags                 reasons;
     28     GeneralNames                cRLIssuer;
     29 
     30     public static DistributionPoint getInstance(
     31         ASN1TaggedObject obj,
     32         boolean          explicit)
     33     {
     34         return getInstance(ASN1Sequence.getInstance(obj, explicit));
     35     }
     36 
     37     public static DistributionPoint getInstance(
     38         Object obj)
     39     {
     40         if(obj == null || obj instanceof DistributionPoint)
     41         {
     42             return (DistributionPoint)obj;
     43         }
     44 
     45         if(obj instanceof ASN1Sequence)
     46         {
     47             return new DistributionPoint((ASN1Sequence)obj);
     48         }
     49 
     50         throw new IllegalArgumentException("Invalid DistributionPoint: " + obj.getClass().getName());
     51     }
     52 
     53     public DistributionPoint(
     54         ASN1Sequence seq)
     55     {
     56         for (int i = 0; i != seq.size(); i++)
     57         {
     58             ASN1TaggedObject    t = ASN1TaggedObject.getInstance(seq.getObjectAt(i));
     59             switch (t.getTagNo())
     60             {
     61             case 0:
     62                 distributionPoint = DistributionPointName.getInstance(t, true);
     63                 break;
     64             case 1:
     65                 reasons = new ReasonFlags(DERBitString.getInstance(t, false));
     66                 break;
     67             case 2:
     68                 cRLIssuer = GeneralNames.getInstance(t, false);
     69                 break;
     70             default:
     71                 throw new IllegalArgumentException("Unknown tag encountered in structure: " + t.getTagNo());
     72             }
     73         }
     74     }
     75 
     76     public DistributionPoint(
     77         DistributionPointName distributionPoint,
     78         ReasonFlags                 reasons,
     79         GeneralNames            cRLIssuer)
     80     {
     81         this.distributionPoint = distributionPoint;
     82         this.reasons = reasons;
     83         this.cRLIssuer = cRLIssuer;
     84     }
     85 
     86     public DistributionPointName getDistributionPoint()
     87     {
     88         return distributionPoint;
     89     }
     90 
     91     public ReasonFlags getReasons()
     92     {
     93         return reasons;
     94     }
     95 
     96     public GeneralNames getCRLIssuer()
     97     {
     98         return cRLIssuer;
     99     }
    100 
    101     public ASN1Primitive toASN1Primitive()
    102     {
    103         ASN1EncodableVector  v = new ASN1EncodableVector();
    104 
    105         if (distributionPoint != null)
    106         {
    107             //
    108             // as this is a CHOICE it must be explicitly tagged
    109             //
    110             v.add(new DERTaggedObject(0, distributionPoint));
    111         }
    112 
    113         if (reasons != null)
    114         {
    115             v.add(new DERTaggedObject(false, 1, reasons));
    116         }
    117 
    118         if (cRLIssuer != null)
    119         {
    120             v.add(new DERTaggedObject(false, 2, cRLIssuer));
    121         }
    122 
    123         return new DERSequence(v);
    124     }
    125 
    126     public String toString()
    127     {
    128         String       sep = Strings.lineSeparator();
    129         StringBuffer buf = new StringBuffer();
    130         buf.append("DistributionPoint: [");
    131         buf.append(sep);
    132         if (distributionPoint != null)
    133         {
    134             appendObject(buf, sep, "distributionPoint", distributionPoint.toString());
    135         }
    136         if (reasons != null)
    137         {
    138             appendObject(buf, sep, "reasons", reasons.toString());
    139         }
    140         if (cRLIssuer != null)
    141         {
    142             appendObject(buf, sep, "cRLIssuer", cRLIssuer.toString());
    143         }
    144         buf.append("]");
    145         buf.append(sep);
    146         return buf.toString();
    147     }
    148 
    149     private void appendObject(StringBuffer buf, String sep, String name, String value)
    150     {
    151         String       indent = "    ";
    152 
    153         buf.append(indent);
    154         buf.append(name);
    155         buf.append(":");
    156         buf.append(sep);
    157         buf.append(indent);
    158         buf.append(indent);
    159         buf.append(value);
    160         buf.append(sep);
    161     }
    162 }
    163