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.ASN1Sequence;
      6 import org.bouncycastle.asn1.ASN1TaggedObject;
      7 import org.bouncycastle.asn1.DERObject;
      8 import org.bouncycastle.asn1.DERSequence;
      9 import org.bouncycastle.asn1.DERTaggedObject;
     10 
     11 /**
     12  * The Holder object.
     13  * <p>
     14  * For an v2 attribute certificate this is:
     15  *
     16  * <pre>
     17  *            Holder ::= SEQUENCE {
     18  *                  baseCertificateID   [0] IssuerSerial OPTIONAL,
     19  *                           -- the issuer and serial number of
     20  *                           -- the holder's Public Key Certificate
     21  *                  entityName          [1] GeneralNames OPTIONAL,
     22  *                           -- the name of the claimant or role
     23  *                  objectDigestInfo    [2] ObjectDigestInfo OPTIONAL
     24  *                           -- used to directly authenticate the holder,
     25  *                           -- for example, an executable
     26  *            }
     27  * </pre>
     28  *
     29  * <p>
     30  * For an v1 attribute certificate this is:
     31  *
     32  * <pre>
     33  *         subject CHOICE {
     34  *          baseCertificateID [0] IssuerSerial,
     35  *          -- associated with a Public Key Certificate
     36  *          subjectName [1] GeneralNames },
     37  *          -- associated with a name
     38  * </pre>
     39  */
     40 public class Holder
     41     extends ASN1Encodable
     42 {
     43     IssuerSerial baseCertificateID;
     44 
     45     GeneralNames entityName;
     46 
     47     ObjectDigestInfo objectDigestInfo;
     48 
     49     private int version = 1;
     50 
     51     public static Holder getInstance(Object obj)
     52     {
     53         if (obj instanceof Holder)
     54         {
     55             return (Holder)obj;
     56         }
     57         else if (obj instanceof ASN1Sequence)
     58         {
     59             return new Holder((ASN1Sequence)obj);
     60         }
     61         else if (obj instanceof ASN1TaggedObject)
     62         {
     63             return new Holder((ASN1TaggedObject)obj);
     64         }
     65 
     66         throw new IllegalArgumentException("unknown object in factory: " + obj.getClass().getName());
     67     }
     68 
     69     /**
     70      * Constructor for a holder for an v1 attribute certificate.
     71      *
     72      * @param tagObj The ASN.1 tagged holder object.
     73      */
     74     public Holder(ASN1TaggedObject tagObj)
     75     {
     76         switch (tagObj.getTagNo())
     77         {
     78         case 0:
     79             baseCertificateID = IssuerSerial.getInstance(tagObj, false);
     80             break;
     81         case 1:
     82             entityName = GeneralNames.getInstance(tagObj, false);
     83             break;
     84         default:
     85             throw new IllegalArgumentException("unknown tag in Holder");
     86         }
     87         version = 0;
     88     }
     89 
     90     /**
     91      * Constructor for a holder for an v2 attribute certificate. *
     92      *
     93      * @param seq The ASN.1 sequence.
     94      */
     95     public Holder(ASN1Sequence seq)
     96     {
     97         if (seq.size() > 3)
     98         {
     99             throw new IllegalArgumentException("Bad sequence size: "
    100                 + seq.size());
    101         }
    102 
    103         for (int i = 0; i != seq.size(); i++)
    104         {
    105             ASN1TaggedObject tObj = ASN1TaggedObject.getInstance(seq
    106                 .getObjectAt(i));
    107 
    108             switch (tObj.getTagNo())
    109             {
    110             case 0:
    111                 baseCertificateID = IssuerSerial.getInstance(tObj, false);
    112                 break;
    113             case 1:
    114                 entityName = GeneralNames.getInstance(tObj, false);
    115                 break;
    116             case 2:
    117                 objectDigestInfo = ObjectDigestInfo.getInstance(tObj, false);
    118                 break;
    119             default:
    120                 throw new IllegalArgumentException("unknown tag in Holder");
    121             }
    122         }
    123         version = 1;
    124     }
    125 
    126     public Holder(IssuerSerial baseCertificateID)
    127     {
    128         this.baseCertificateID = baseCertificateID;
    129     }
    130 
    131     /**
    132      * Constructs a holder from a IssuerSerial.
    133      * @param baseCertificateID The IssuerSerial.
    134      * @param version The version of the attribute certificate.
    135      */
    136     public Holder(IssuerSerial baseCertificateID, int version)
    137     {
    138         this.baseCertificateID = baseCertificateID;
    139         this.version = version;
    140     }
    141 
    142     /**
    143      * Returns 1 for v2 attribute certificates or 0 for v1 attribute
    144      * certificates.
    145      * @return The version of the attribute certificate.
    146      */
    147     public int getVersion()
    148     {
    149         return version;
    150     }
    151 
    152     /**
    153      * Constructs a holder with an entityName for v2 attribute certificates or
    154      * with a subjectName for v1 attribute certificates.
    155      *
    156      * @param entityName The entity or subject name.
    157      */
    158     public Holder(GeneralNames entityName)
    159     {
    160         this.entityName = entityName;
    161     }
    162 
    163     /**
    164      * Constructs a holder with an entityName for v2 attribute certificates or
    165      * with a subjectName for v1 attribute certificates.
    166      *
    167      * @param entityName The entity or subject name.
    168      * @param version The version of the attribute certificate.
    169      */
    170     public Holder(GeneralNames entityName, int version)
    171     {
    172         this.entityName = entityName;
    173         this.version = version;
    174     }
    175 
    176     /**
    177      * Constructs a holder from an object digest info.
    178      *
    179      * @param objectDigestInfo The object digest info object.
    180      */
    181     public Holder(ObjectDigestInfo objectDigestInfo)
    182     {
    183         this.objectDigestInfo = objectDigestInfo;
    184     }
    185 
    186     public IssuerSerial getBaseCertificateID()
    187     {
    188         return baseCertificateID;
    189     }
    190 
    191     /**
    192      * Returns the entityName for an v2 attribute certificate or the subjectName
    193      * for an v1 attribute certificate.
    194      *
    195      * @return The entityname or subjectname.
    196      */
    197     public GeneralNames getEntityName()
    198     {
    199         return entityName;
    200     }
    201 
    202     public ObjectDigestInfo getObjectDigestInfo()
    203     {
    204         return objectDigestInfo;
    205     }
    206 
    207     public DERObject toASN1Object()
    208     {
    209         if (version == 1)
    210         {
    211             ASN1EncodableVector v = new ASN1EncodableVector();
    212 
    213             if (baseCertificateID != null)
    214             {
    215                 v.add(new DERTaggedObject(false, 0, baseCertificateID));
    216             }
    217 
    218             if (entityName != null)
    219             {
    220                 v.add(new DERTaggedObject(false, 1, entityName));
    221             }
    222 
    223             if (objectDigestInfo != null)
    224             {
    225                 v.add(new DERTaggedObject(false, 2, objectDigestInfo));
    226             }
    227 
    228             return new DERSequence(v);
    229         }
    230         else
    231         {
    232             if (entityName != null)
    233             {
    234                 return new DERTaggedObject(false, 1, entityName);
    235             }
    236             else
    237             {
    238                 return new DERTaggedObject(false, 0, baseCertificateID);
    239             }
    240         }
    241     }
    242 }
    243