KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
23 import java.util.Enumeration JavaDoc;
24
25 /**
26  * BER 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 BERTaggedObject
31     extends DERTaggedObject
32 {
33     /**
34      * @param tagNo the tag number for this object.
35      * @param obj the tagged object.
36      */

37     public BERTaggedObject(
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 BERTaggedObject(
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 BERTaggedObject(
62         int tagNo)
63     {
64         super(false, tagNo, new BERConstructedSequence());
65     }
66
67     void encode(
68         DEROutputStream out)
69         throws IOException JavaDoc
70     {
71         if (out instanceof ASN1OutputStream || out instanceof BEROutputStream)
72         {
73             out.write(CONSTRUCTED | TAGGED | tagNo);
74             out.write(0x80);
75
76             if (!empty)
77             {
78                 if (!explicit)
79                 {
80                     if (obj instanceof ASN1OctetString)
81                     {
82                         Enumeration JavaDoc e;
83
84                         if (obj instanceof BERConstructedOctetString)
85                         {
86                             e = ((BERConstructedOctetString)obj).getObjects();
87                         }
88                         else
89                         {
90                             ASN1OctetString octs = (ASN1OctetString)obj;
91                             BERConstructedOctetString berO = new BERConstructedOctetString(octs.getOctets());
92
93                             e = berO.getObjects();
94                         }
95
96                         while (e.hasMoreElements())
97                         {
98                             out.writeObject(e.nextElement());
99                         }
100                     }
101                     else if (obj instanceof ASN1Sequence)
102                     {
103                         Enumeration JavaDoc e = ((ASN1Sequence)obj).getObjects();
104
105                         while (e.hasMoreElements())
106                         {
107                             out.writeObject(e.nextElement());
108                         }
109                     }
110                     else if (obj instanceof ASN1Set)
111                     {
112                         Enumeration JavaDoc e = ((ASN1Set)obj).getObjects();
113
114                         while (e.hasMoreElements())
115                         {
116                             out.writeObject(e.nextElement());
117                         }
118                     }
119                     else
120                     {
121                         throw new RuntimeException JavaDoc("not implemented: " + obj.getClass().getName());
122                     }
123                 }
124                 else
125                 {
126                     out.writeObject(obj);
127                 }
128             }
129
130             out.write(0x00);
131             out.write(0x00);
132         }
133         else
134         {
135             super.encode(out);
136         }
137     }
138 }
139
Popular Tags