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.ASN1Enumerated;
      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.DERBitString;
     11 import org.bouncycastle.asn1.DERSequence;
     12 
     13 /**
     14  * ObjectDigestInfo ASN.1 structure used in v2 attribute certificates.
     15  *
     16  * <pre>
     17  *
     18  *    ObjectDigestInfo ::= SEQUENCE {
     19  *         digestedObjectType  ENUMERATED {
     20  *                 publicKey            (0),
     21  *                 publicKeyCert        (1),
     22  *                 otherObjectTypes     (2) },
     23  *                         -- otherObjectTypes MUST NOT
     24  *                         -- be used in this profile
     25  *         otherObjectTypeID   OBJECT IDENTIFIER OPTIONAL,
     26  *         digestAlgorithm     AlgorithmIdentifier,
     27  *         objectDigest        BIT STRING
     28  *    }
     29  *
     30  * </pre>
     31  *
     32  */
     33 public class ObjectDigestInfo
     34     extends ASN1Object
     35 {
     36     /**
     37      * The public key is hashed.
     38      */
     39     public final static int publicKey = 0;
     40 
     41     /**
     42      * The public key certificate is hashed.
     43      */
     44     public final static int publicKeyCert = 1;
     45 
     46     /**
     47      * An other object is hashed.
     48      */
     49     public final static int otherObjectDigest = 2;
     50 
     51     ASN1Enumerated digestedObjectType;
     52 
     53     ASN1ObjectIdentifier otherObjectTypeID;
     54 
     55     AlgorithmIdentifier digestAlgorithm;
     56 
     57     DERBitString objectDigest;
     58 
     59     public static ObjectDigestInfo getInstance(
     60         Object obj)
     61     {
     62         if (obj instanceof ObjectDigestInfo)
     63         {
     64             return (ObjectDigestInfo)obj;
     65         }
     66 
     67         if (obj != null)
     68         {
     69             return new ObjectDigestInfo(ASN1Sequence.getInstance(obj));
     70         }
     71 
     72         return null;
     73     }
     74 
     75     public static ObjectDigestInfo getInstance(
     76         ASN1TaggedObject obj,
     77         boolean          explicit)
     78     {
     79         return getInstance(ASN1Sequence.getInstance(obj, explicit));
     80     }
     81 
     82     /**
     83      * Constructor from given details.
     84      * <p>
     85      * If <code>digestedObjectType</code> is not {@link #publicKeyCert} or
     86      * {@link #publicKey} <code>otherObjectTypeID</code> must be given,
     87      * otherwise it is ignored.
     88      *
     89      * @param digestedObjectType The digest object type.
     90      * @param otherObjectTypeID The object type ID for
     91      *            <code>otherObjectDigest</code>.
     92      * @param digestAlgorithm The algorithm identifier for the hash.
     93      * @param objectDigest The hash value.
     94      */
     95     public ObjectDigestInfo(
     96         int digestedObjectType,
     97         ASN1ObjectIdentifier otherObjectTypeID,
     98         AlgorithmIdentifier digestAlgorithm,
     99         byte[] objectDigest)
    100     {
    101         this.digestedObjectType = new ASN1Enumerated(digestedObjectType);
    102         if (digestedObjectType == otherObjectDigest)
    103         {
    104             this.otherObjectTypeID = otherObjectTypeID;
    105         }
    106 
    107         this.digestAlgorithm = digestAlgorithm;
    108         this.objectDigest = new DERBitString(objectDigest);
    109     }
    110 
    111     private ObjectDigestInfo(
    112         ASN1Sequence seq)
    113     {
    114         if (seq.size() > 4 || seq.size() < 3)
    115         {
    116             throw new IllegalArgumentException("Bad sequence size: "
    117                 + seq.size());
    118         }
    119 
    120         digestedObjectType = ASN1Enumerated.getInstance(seq.getObjectAt(0));
    121 
    122         int offset = 0;
    123 
    124         if (seq.size() == 4)
    125         {
    126             otherObjectTypeID = ASN1ObjectIdentifier.getInstance(seq.getObjectAt(1));
    127             offset++;
    128         }
    129 
    130         digestAlgorithm = AlgorithmIdentifier.getInstance(seq.getObjectAt(1 + offset));
    131 
    132         objectDigest = DERBitString.getInstance(seq.getObjectAt(2 + offset));
    133     }
    134 
    135     public ASN1Enumerated getDigestedObjectType()
    136     {
    137         return digestedObjectType;
    138     }
    139 
    140     public ASN1ObjectIdentifier getOtherObjectTypeID()
    141     {
    142         return otherObjectTypeID;
    143     }
    144 
    145     public AlgorithmIdentifier getDigestAlgorithm()
    146     {
    147         return digestAlgorithm;
    148     }
    149 
    150     public DERBitString getObjectDigest()
    151     {
    152         return objectDigest;
    153     }
    154 
    155     /**
    156      * Produce an object suitable for an ASN1OutputStream.
    157      *
    158      * <pre>
    159      *
    160      *    ObjectDigestInfo ::= SEQUENCE {
    161      *         digestedObjectType  ENUMERATED {
    162      *                 publicKey            (0),
    163      *                 publicKeyCert        (1),
    164      *                 otherObjectTypes     (2) },
    165      *                         -- otherObjectTypes MUST NOT
    166      *                         -- be used in this profile
    167      *         otherObjectTypeID   OBJECT IDENTIFIER OPTIONAL,
    168      *         digestAlgorithm     AlgorithmIdentifier,
    169      *         objectDigest        BIT STRING
    170      *    }
    171      *
    172      * </pre>
    173      */
    174     public ASN1Primitive toASN1Primitive()
    175     {
    176         ASN1EncodableVector v = new ASN1EncodableVector();
    177 
    178         v.add(digestedObjectType);
    179 
    180         if (otherObjectTypeID != null)
    181         {
    182             v.add(otherObjectTypeID);
    183         }
    184 
    185         v.add(digestAlgorithm);
    186         v.add(objectDigest);
    187 
    188         return new DERSequence(v);
    189     }
    190 }
    191