1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.io.IOException;
4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam/**
6c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * DER TaggedObject - in ASN.1 notation this is any object preceded by
7c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * a [n] where n is some number - these are assumed to follow the construction
8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * rules (as with sequences).
9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class DERTaggedObject
11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    extends ASN1TaggedObject
12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
13c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private static final byte[] ZERO_BYTES = new byte[0];
14c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param tagNo the tag number for this object.
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param obj the tagged object.
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DERTaggedObject(
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        int             tagNo,
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        DEREncodable    obj)
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        super(tagNo, obj);
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param explicit true if an explicitly tagged object.
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param tagNo the tag number for this object.
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param obj the tagged object.
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DERTaggedObject(
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        boolean         explicit,
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        int             tagNo,
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        DEREncodable    obj)
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        super(explicit, tagNo, obj);
37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * create an implicitly tagged object that contains a zero
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * length sequence.
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DERTaggedObject(
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        int             tagNo)
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        super(false, tagNo, new DERSequence());
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    void encode(
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        DEROutputStream  out)
51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throws IOException
52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (!empty)
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
55c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            byte[] bytes = obj.getDERObject().getEncoded(DER);
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            if (explicit)
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            {
59c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                out.writeEncoded(CONSTRUCTED | TAGGED, tagNo, bytes);
60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            }
61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            else
62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            {
63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                //
64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                // need to mark constructed types...
65b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                //
66c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                int flags;
67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                if ((bytes[0] & CONSTRUCTED) != 0)
68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                {
69c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    flags = CONSTRUCTED | TAGGED;
70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                }
71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                else
72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                {
73c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    flags = TAGGED;
74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                }
75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
76c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                out.writeTag(flags, tagNo);
77c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                out.write(bytes, 1, bytes.length - 1);
78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            }
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else
81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
82c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            out.writeEncoded(CONSTRUCTED | TAGGED, tagNo, ZERO_BYTES);
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
86