Home | History | Annotate | Download | only in pkcs
      1 package org.bouncycastle.asn1.pkcs;
      2 
      3 import java.util.Enumeration;
      4 
      5 import org.bouncycastle.asn1.ASN1Encodable;
      6 import org.bouncycastle.asn1.ASN1EncodableVector;
      7 import org.bouncycastle.asn1.ASN1Sequence;
      8 import org.bouncycastle.asn1.BERSequence;
      9 import org.bouncycastle.asn1.BERTaggedObject;
     10 import org.bouncycastle.asn1.DEREncodable;
     11 import org.bouncycastle.asn1.DERObject;
     12 import org.bouncycastle.asn1.DERObjectIdentifier;
     13 import org.bouncycastle.asn1.DERTaggedObject;
     14 
     15 public class ContentInfo
     16     extends ASN1Encodable
     17     implements PKCSObjectIdentifiers
     18 {
     19     private DERObjectIdentifier contentType;
     20     private DEREncodable        content;
     21 
     22     public static ContentInfo getInstance(
     23         Object  obj)
     24     {
     25         if (obj instanceof ContentInfo)
     26         {
     27             return (ContentInfo)obj;
     28         }
     29         else if (obj instanceof ASN1Sequence)
     30         {
     31             return new ContentInfo((ASN1Sequence)obj);
     32         }
     33 
     34         throw new IllegalArgumentException("unknown object in factory: " + obj.getClass().getName());
     35     }
     36 
     37     public ContentInfo(
     38         ASN1Sequence  seq)
     39     {
     40         Enumeration   e = seq.getObjects();
     41 
     42         contentType = (DERObjectIdentifier)e.nextElement();
     43 
     44         if (e.hasMoreElements())
     45         {
     46             content = ((DERTaggedObject)e.nextElement()).getObject();
     47         }
     48     }
     49 
     50     public ContentInfo(
     51         DERObjectIdentifier contentType,
     52         DEREncodable        content)
     53     {
     54         this.contentType = contentType;
     55         this.content = content;
     56     }
     57 
     58     public DERObjectIdentifier getContentType()
     59     {
     60         return contentType;
     61     }
     62 
     63     public DEREncodable getContent()
     64     {
     65         return content;
     66     }
     67 
     68     /**
     69      * Produce an object suitable for an ASN1OutputStream.
     70      * <pre>
     71      * ContentInfo ::= SEQUENCE {
     72      *          contentType ContentType,
     73      *          content
     74      *          [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL }
     75      * </pre>
     76      */
     77     public DERObject toASN1Object()
     78     {
     79         ASN1EncodableVector  v = new ASN1EncodableVector();
     80 
     81         v.add(contentType);
     82 
     83         if (content != null)
     84         {
     85             v.add(new BERTaggedObject(0, content));
     86         }
     87 
     88         return new BERSequence(v);
     89     }
     90 }
     91