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