Home | History | Annotate | Download | only in cms
      1 package org.bouncycastle.asn1.cms;
      2 
      3 import org.bouncycastle.asn1.ASN1Encodable;
      4 import org.bouncycastle.asn1.ASN1EncodableVector;
      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.BERSequence;
     11 import org.bouncycastle.asn1.BERTaggedObject;
     12 
     13 /**
     14  * <a href="http://tools.ietf.org/html/rfc5652#section-3">RFC 5652</a> ContentInfo, and
     15  * <a href="http://tools.ietf.org/html/rfc5652#section-5.2">RFC 5652</a> EncapsulatedContentInfo objects.
     16  *
     17  * <pre>
     18  * ContentInfo ::= SEQUENCE {
     19  *     contentType ContentType,
     20  *     content [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL
     21  * }
     22  *
     23  * EncapsulatedContentInfo ::= SEQUENCE {
     24  *     eContentType ContentType,
     25  *     eContent [0] EXPLICIT OCTET STRING OPTIONAL
     26  * }
     27  * </pre>
     28  */
     29 public class ContentInfo
     30     extends ASN1Object
     31     // BEGIN android-removed
     32     // implements CMSObjectIdentifiers
     33     // END android-removed
     34 {
     35     private ASN1ObjectIdentifier contentType;
     36     private ASN1Encodable        content;
     37 
     38     /**
     39      * Return an ContentInfo object from the given object.
     40      * <p>
     41      * Accepted inputs:
     42      * <ul>
     43      * <li> null &rarr; null
     44      * <li> {@link ContentInfo} object
     45      * <li> {@link org.bouncycastle.asn1.ASN1Sequence#getInstance(java.lang.Object) ASN1Sequence} input formats with ContentInfo structure inside
     46      * </ul>
     47      *
     48      * @param obj the object we want converted.
     49      * @exception IllegalArgumentException if the object cannot be converted.
     50      */
     51     public static ContentInfo getInstance(
     52         Object  obj)
     53     {
     54         if (obj instanceof ContentInfo)
     55         {
     56             return (ContentInfo)obj;
     57         }
     58         else if (obj != null)
     59         {
     60             return new ContentInfo(ASN1Sequence.getInstance(obj));
     61         }
     62 
     63         return null;
     64     }
     65 
     66     public static ContentInfo getInstance(
     67         ASN1TaggedObject obj,
     68         boolean explicit)
     69     {
     70         return getInstance(ASN1Sequence.getInstance(obj, explicit));
     71     }
     72 
     73     /**
     74      * @deprecated use getInstance()
     75      */
     76     public ContentInfo(
     77         ASN1Sequence  seq)
     78     {
     79         if (seq.size() < 1 || seq.size() > 2)
     80         {
     81             throw new IllegalArgumentException("Bad sequence size: " + seq.size());
     82         }
     83 
     84         contentType = (ASN1ObjectIdentifier)seq.getObjectAt(0);
     85 
     86         if (seq.size() > 1)
     87         {
     88             ASN1TaggedObject tagged = (ASN1TaggedObject)seq.getObjectAt(1);
     89             if (!tagged.isExplicit() || tagged.getTagNo() != 0)
     90             {
     91                 throw new IllegalArgumentException("Bad tag for 'content'");
     92             }
     93 
     94             content = tagged.getObject();
     95         }
     96     }
     97 
     98     public ContentInfo(
     99         ASN1ObjectIdentifier contentType,
    100         ASN1Encodable        content)
    101     {
    102         this.contentType = contentType;
    103         this.content = content;
    104     }
    105 
    106     public ASN1ObjectIdentifier getContentType()
    107     {
    108         return contentType;
    109     }
    110 
    111     public ASN1Encodable getContent()
    112     {
    113         return content;
    114     }
    115 
    116     /**
    117      * Produce an object suitable for an ASN1OutputStream.
    118      */
    119     public ASN1Primitive toASN1Primitive()
    120     {
    121         ASN1EncodableVector  v = new ASN1EncodableVector();
    122 
    123         v.add(contentType);
    124 
    125         if (content != null)
    126         {
    127             v.add(new BERTaggedObject(0, content));
    128         }
    129 
    130         return new BERSequence(v);
    131     }
    132 }
    133