KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > asn1 > DERTaggedObject


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.asn1;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 /**
26  * DER TaggedObject - in ASN.1 nottation this is any object proceeded by
27  * a [n] where n is some number - these are assume to follow the construction
28  * rules (as with sequences).
29  */

30 public class DERTaggedObject
31     extends ASN1TaggedObject
32 {
33     /**
34      * @param tagNo the tag number for this object.
35      * @param obj the tagged object.
36      */

37     public DERTaggedObject(
38         int tagNo,
39         DEREncodable obj)
40     {
41         super(tagNo, obj);
42     }
43
44     /**
45      * @param explicit true if an explicitly tagged object.
46      * @param tagNo the tag number for this object.
47      * @param obj the tagged object.
48      */

49     public DERTaggedObject(
50         boolean explicit,
51         int tagNo,
52         DEREncodable obj)
53     {
54         super(explicit, tagNo, obj);
55     }
56
57     /**
58      * create an implicitly tagged object that contains a zero
59      * length sequence.
60      */

61     public DERTaggedObject(
62         int tagNo)
63     {
64         super(false, tagNo, new DERSequence());
65     }
66
67     void encode(
68         DEROutputStream out)
69         throws IOException JavaDoc
70     {
71         if (!empty)
72         {
73             ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
74             DEROutputStream dOut = new DEROutputStream(bOut);
75
76             dOut.writeObject(obj);
77             dOut.close();
78
79             byte[] bytes = bOut.toByteArray();
80
81             if (explicit)
82             {
83                 out.writeEncoded(CONSTRUCTED | TAGGED | tagNo, bytes);
84             }
85             else
86             {
87                 //
88
// need to mark constructed types...
89
//
90
if ((bytes[0] & CONSTRUCTED) != 0)
91                 {
92                     bytes[0] = (byte)(CONSTRUCTED | TAGGED | tagNo);
93                 }
94                 else
95                 {
96                     bytes[0] = (byte)(TAGGED | tagNo);
97                 }
98
99                 out.write(bytes);
100             }
101         }
102         else
103         {
104             out.writeEncoded(CONSTRUCTED | TAGGED | tagNo, new byte[0]);
105         }
106     }
107 }
108
Popular Tags