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.DERBitString;
      8 import org.bouncycastle.asn1.DEREnumerated;
      9 import org.bouncycastle.asn1.DERObject;
     10 import org.bouncycastle.asn1.DERObjectIdentifier;
     11 import org.bouncycastle.asn1.DERSequence;
     12 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
     13 
     14 
     15 public class ObjectDigestInfo
     16     extends ASN1Encodable
     17 {
     18     DEREnumerated digestedObjectType;
     19 
     20     DERObjectIdentifier otherObjectTypeID;
     21 
     22     AlgorithmIdentifier digestAlgorithm;
     23 
     24     DERBitString objectDigest;
     25 
     26     public static ObjectDigestInfo getInstance(
     27             Object  obj)
     28     {
     29         if (obj == null || obj instanceof ObjectDigestInfo)
     30         {
     31             return (ObjectDigestInfo)obj;
     32         }
     33 
     34         if (obj instanceof ASN1Sequence)
     35         {
     36             return new ObjectDigestInfo((ASN1Sequence)obj);
     37         }
     38 
     39         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
     40     }
     41 
     42     public static ObjectDigestInfo getInstance(
     43         ASN1TaggedObject obj,
     44         boolean          explicit)
     45     {
     46         return getInstance(ASN1Sequence.getInstance(obj, explicit));
     47     }
     48 
     49     public ObjectDigestInfo(ASN1Sequence seq)
     50     {
     51         if (seq.size() > 4 || seq.size() < 3)
     52         {
     53             throw new IllegalArgumentException("Bad sequence size: "
     54                     + seq.size());
     55         }
     56 
     57         digestedObjectType = DEREnumerated.getInstance(seq.getObjectAt(0));
     58 
     59         int offset = 0;
     60 
     61         if (seq.size() == 4)
     62         {
     63             otherObjectTypeID = DERObjectIdentifier.getInstance(seq.getObjectAt(1));
     64             offset++;
     65         }
     66 
     67         digestAlgorithm = AlgorithmIdentifier.getInstance(seq.getObjectAt(1 + offset));
     68 
     69         objectDigest = DERBitString.getInstance(seq.getObjectAt(2 + offset));
     70     }
     71 
     72     public DEREnumerated getDigestedObjectType()
     73     {
     74         return digestedObjectType;
     75     }
     76 
     77     public DERObjectIdentifier getOtherObjectTypeID()
     78     {
     79         return otherObjectTypeID;
     80     }
     81 
     82     public AlgorithmIdentifier getDigestAlgorithm()
     83     {
     84         return digestAlgorithm;
     85     }
     86 
     87     public DERBitString getObjectDigest()
     88     {
     89         return objectDigest;
     90     }
     91 
     92     /**
     93      * Produce an object suitable for an ASN1OutputStream.
     94      *
     95      * <pre>
     96      *
     97      *   ObjectDigestInfo ::= SEQUENCE {
     98      *        digestedObjectType  ENUMERATED {
     99      *                publicKey            (0),
    100      *                publicKeyCert        (1),
    101      *                otherObjectTypes     (2) },
    102      *                        -- otherObjectTypes MUST NOT
    103      *                        -- be used in this profile
    104      *        otherObjectTypeID   OBJECT IDENTIFIER OPTIONAL,
    105      *        digestAlgorithm     AlgorithmIdentifier,
    106      *        objectDigest        BIT STRING
    107      *   }
    108      *
    109      * </pre>
    110      */
    111     public DERObject toASN1Object()
    112     {
    113         ASN1EncodableVector v = new ASN1EncodableVector();
    114 
    115         v.add(digestedObjectType);
    116 
    117         if (otherObjectTypeID != null)
    118         {
    119             v.add(otherObjectTypeID);
    120         }
    121 
    122         v.add(digestAlgorithm);
    123         v.add(objectDigest);
    124 
    125         return new DERSequence(v);
    126     }
    127 }
    128