1 package org.bouncycastle.asn1;
2 
3 import java.io.IOException;
4 
5 /**
6  * DER TaggedObject - in ASN.1 notation this is any object preceded by
7  * a [n] where n is some number - these are assumed to follow the construction
8  * rules (as with sequences).
9  */
10 public class DERTaggedObject
11     extends ASN1TaggedObject
12 {
13     /**
14      * @param explicit true if an explicitly tagged object.
15      * @param tagNo the tag number for this object.
16      * @param obj the tagged object.
17      */
DERTaggedObject( boolean explicit, int tagNo, ASN1Encodable obj)18     public DERTaggedObject(
19         boolean       explicit,
20         int           tagNo,
21         ASN1Encodable obj)
22     {
23         super(explicit, tagNo, obj);
24     }
25 
DERTaggedObject(int tagNo, ASN1Encodable encodable)26     public DERTaggedObject(int tagNo, ASN1Encodable encodable)
27     {
28         super(true, tagNo, encodable);
29     }
30 
isConstructed()31     boolean isConstructed()
32     {
33         return explicit || obj.toASN1Primitive().toDERObject().isConstructed();
34     }
35 
encodedLength()36     int encodedLength()
37         throws IOException
38     {
39         ASN1Primitive primitive = obj.toASN1Primitive().toDERObject();
40         int length = primitive.encodedLength();
41 
42         if (explicit)
43         {
44             return StreamUtil.calculateTagLength(tagNo) + StreamUtil.calculateBodyLength(length) + length;
45         }
46         else
47         {
48             // header length already in calculation
49             length = length - 1;
50 
51             return StreamUtil.calculateTagLength(tagNo) + length;
52         }
53     }
54 
encode(ASN1OutputStream out, boolean withTag)55     void encode(ASN1OutputStream out, boolean withTag) throws IOException
56     {
57         ASN1Primitive primitive = obj.toASN1Primitive().toDERObject();
58 
59         int flags = BERTags.TAGGED;
60         if (explicit || primitive.isConstructed())
61         {
62             flags |= BERTags.CONSTRUCTED;
63         }
64 
65         out.writeTag(withTag, flags, tagNo);
66 
67         if (explicit)
68         {
69             out.writeLength(primitive.encodedLength());
70         }
71 
72         primitive.encode(out.getDERSubStream(), explicit);
73     }
74 
toDERObject()75     ASN1Primitive toDERObject()
76     {
77         return this;
78     }
79 
toDLObject()80     ASN1Primitive toDLObject()
81     {
82         return this;
83     }
84 }
85