KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > der > DERObject


1 /*
2  * Title: Oyster Project
3  * Description: S/MIME email sending capabilities
4  * @Author Vladimir Radisic
5  * @Version 2.1.5
6  */

7
8 package org.enhydra.oyster.der;
9
10 import org.enhydra.oyster.exception.SMIMEException;
11 import org.enhydra.oyster.exception.ErrorStorage;
12
13 /**
14  * Every element of data represented in ASN.1 notation should be encoded
15  * in the process of implementation. It can be done according to BER (Basic
16  * Encoding Rules) or according to DER (Distinguish Encoding Rules). DERObject
17  * class is the root class (super class) of all classes used in DER encodings
18  * of variety elements in data structures used by SMIME.<BR>
19  * <BR>
20  * DER encoding of elements can be performed in two forms: definite form and
21  * indefinite form. DERObject class represents definite form of encoding. Every
22  * encoded element, according to this rules, involves three parts: Identifier
23  * Octet (which represents Tag Type of the encoding data), Length Octets (one
24  * or more octets with the information about number of octets (bytes) in the
25  * content), and Content Octets (they represent data which have to be DER
26  * encoded). <BR>
27  * <BR>
28  * DERObject can be structured or primitive, which depends on his content.
29  */

30 public class DERObject {
31
32 /**
33  * Identifier octet of DER encoding element. It must be in the range 0-255.
34  */

35   private int identifierOctet;
36
37 /**
38  * Length Octets of DER encoding element.
39  */

40   private String JavaDoc lengthOctets = "";
41
42 /**
43  * Content Octets of DER encoding element.
44  */

45   private String JavaDoc contentOctets = "";
46
47 /**
48  * Type of element number (part of Identifier Octet). It must be in the
49  * range 0-30.
50  */

51   private int tagTypeNumber = 0;
52
53 /**
54  * Class Type of element (part of Identifier Octet). It can have values:
55  * 0-Universal, 64-Application, 128-Context, 192-Private (Default 0)
56  */

57   private int tagClassType = 0;
58
59 /**
60  * Complexity type of element. It can have values: 0-Primitive, 32-Structured
61  * (Default 0)
62  */

63   private int tagComplexity = 0;
64
65 /**
66  * Total lenght (number of octets) of DER object (Default 0)
67  */

68   private int totalLength = 0;
69
70 /**
71  * Creates DER Object with defined value for identifier octet.
72  * @param identifierOctet0 must be in the range 0-255 (whithouth 31)
73  * @exception SMIMEException in case of invalid identifierOctet0 parameter
74  */

75   public DERObject (int identifierOctet0) throws SMIMEException
76   {
77     if (identifierOctet0 < 0)
78       throw new SMIMEException(this, 1000);
79     else if (identifierOctet0 > 255)
80       throw new SMIMEException(this, 1001);
81     else if ((identifierOctet0 & 31) == 31)
82       throw new SMIMEException(this, 1002);
83     identifierOctet = identifierOctet0;
84     tagClassType = identifierOctet0 & 192;
85     tagComplexity = identifierOctet0 & 32;
86     tagTypeNumber = identifierOctet0 & 31;
87     totalLength = 1; // Current length of DER object (contain only Identifier octet)
88
}
89
90 /**
91  * Creates DER Object with defined value for identifier octet and values of
92  * content octets
93  * @param identifierOctet0 must be in the range 0-255 (whithouth 31)
94  * @param content0 content of DER Object
95  * @exception SMIMEException in case of invalid identifierOctet0 parameter,
96  * or in case of adding content to DER object of type Null DER object
97  */

98   public DERObject (int identifierOctet0, byte[] content0) throws SMIMEException
99   {
100     this(identifierOctet0);
101     this.addContent(content0);
102   }
103
104 /**
105  * Defines the Lenght Octets which is the part of DEROctet
106  * @param len0 number of octets in the content
107  * @return String representation of Length Octets that correspond to len0 number
108  * @exception SMIMEException caused by non SMIMEException which is:
109  * UnsupportedEncodingException.
110  */

111   private String JavaDoc lengthDERPart (int len0) throws SMIMEException{
112     String JavaDoc returnString = null;
113     if (len0 < 128) {
114       byte[] lenByte = new byte[1];
115       lenByte[0] = (byte)len0;
116       try {
117         returnString = new String JavaDoc(lenByte, "ISO-8859-1");
118       }
119       catch(Exception JavaDoc e) {
120         throw SMIMEException.getInstance(this, e, "lengthDERPart" );
121       }
122       return returnString;
123     }
124     else {
125       int i = 1, a = 1; // i: Number of required bits in length0 number
126
for (; (a*2) <= len0; i++)
127         a = a*2;
128       i = (int)Math.ceil((double)i/8); // Number of required bytes for holding length data
129
byte[] lenByte = new byte[i + 1]; // + 1 for first byte in length string
130
lenByte[0] = (byte)(i); // First byte define number of following bytes used for holding length data
131
lenByte[0] = (byte)((int)lenByte[0] | 128); // Indicate complex length octets with bit #8 set to 1
132
for (; i > 0; i--) {
133         a = 255 << ((lenByte.length - i - 1)*8);
134         lenByte[i] = (byte)((len0 & a) >> ((lenByte.length - i - 1)*8));
135       }
136       try {
137         returnString = new String JavaDoc(lenByte, "ISO-8859-1");
138       }
139       catch(Exception JavaDoc e) {
140         throw SMIMEException.getInstance(this, e, "lengthDERPart" );
141       }
142       return returnString;
143     }
144   }
145
146 /**
147  * Adds content to DER Object. Used only when the content isn't added earlier
148  * via second type of constructor in DERObject.
149  * @param content0 content octets for adding to DERObject
150  * @exception SMIMEException when adding content to DER object is of type
151  * Null DER object. Also, it can be caused by non SMIMEException which is:
152  * UnsupportedEncodingException.
153  */

154   void addContent (byte[] content0) throws SMIMEException {
155     if (identifierOctet == 5)
156       throw new SMIMEException(this, 1003);
157     try {
158       contentOctets = contentOctets.concat(new String JavaDoc(content0, "ISO-8859-1"));
159       lengthOctets = lengthDERPart(contentOctets.length());
160       totalLength = 1 + contentOctets.length() + lengthOctets.length();
161     }
162     catch(Exception JavaDoc e) {
163       throw SMIMEException.getInstance(this, e, "addContent" );
164     }
165   }
166
167 /**
168  * Returns DER encoded object
169  * @return DERObject as byte array
170  * @exception SMIMEException if content of DER object is absent. Also, it can be
171  * caused by non SMIMEException which is: UnsupportedEncodingException.
172  */

173   public byte[] getDEREncoded () throws SMIMEException {
174     if (totalLength == 1 && identifierOctet != 5)
175       throw new SMIMEException(this, 1004);
176     byte[] returnByteArray = null; // Variable for returning DER encoding object represented as byte array
177
try {
178       byte[] temp = {
179         (byte)identifierOctet
180       }; // Import identifier octet in DER object string (first byte of string)
181
String JavaDoc derOctet = new String JavaDoc(temp, "ISO-8859-1"); // String with returning DER value
182
if (identifierOctet == 5) // Special DER format is for Null DER object
183
{
184         temp[0] = (byte)0x00;
185         contentOctets = contentOctets.concat(new String JavaDoc(temp, "ISO-8859-1"));
186         derOctet = derOctet.concat(contentOctets);
187         totalLength = 2;
188         returnByteArray = derOctet.getBytes("ISO-8859-1"); // Null DER value
189
}
190       else {
191         derOctet = derOctet.concat(lengthOctets).concat(contentOctets);
192         totalLength = 1 + contentOctets.length() + lengthOctets.length();
193         returnByteArray = derOctet.getBytes("ISO-8859-1");
194       }
195     }
196     catch(Exception JavaDoc e) {
197       throw SMIMEException.getInstance(this, e, "getDEREncoded" );
198     }
199     return returnByteArray;
200   }
201
202 /**
203  * Returns Identifier Octet
204  * @return Identifier Octet in the range 0-255
205  */

206   public int getIdentifierOctet () {
207     return identifierOctet;
208   }
209
210 /**
211  * Returns Tag Type
212  * @return Tag Type in the range 0-30
213  */

214   public int getTagTypeNumber () {
215     return tagTypeNumber;
216   }
217
218 /**
219  * Returns Class Type
220  * @return 0-Universal, 64-Application, 128-Context, 192-Private (Default 0)
221  */

222   public int getTagClassType () {
223     return tagClassType;
224   }
225
226 /**
227  * Returns Tag Complexity
228  * @return 0-primitive, 32-structured (Default 0)
229  */

230   public int getTagComplexity () {
231     return tagComplexity;
232   }
233
234 /**
235  * Returns size of content part in DER object (Number of content octets).
236  * @return Number of content octets in DER encoded object
237  */

238   public int getContentPartSize () {
239     return contentOctets.length();
240   }
241
242 /**
243  * Returns size of length part in DER object (Number of length octets).
244  * @return Number of length octets in DER encoded object
245  */

246   public int getLengthPartSize () {
247     return lengthOctets.length();
248   }
249
250 /**
251  * Return content octets part from DER object.
252  * @return Number of content octets in DER encoded object
253  * @exception SMIMEException caused by non SMIMEException which is:
254  * UnsupportedEncodingException .
255  */

256   public byte[] getContentOctets() throws SMIMEException {
257     byte[] returnByteArray = null;
258     try {
259       returnByteArray = contentOctets.getBytes("ISO-8859-1");
260     }
261     catch(Exception JavaDoc e) {
262       throw SMIMEException.getInstance(this, e, "getContentOctets" );
263     }
264     return returnByteArray;
265   }
266
267 /**
268  * Return length octets part from DER object.
269  * @return Number of length octets in DER encoded object
270  * @exception SMIMEException caused by non SMIMEException which is:
271  * UnsupportedEncodingException.
272  */

273   public byte[] getLengthOctets() throws SMIMEException {
274     byte[] returnByteArray = null;
275     try {
276       returnByteArray = lengthOctets.getBytes("ISO-8859-1");
277     }
278     catch(Exception JavaDoc e) {
279       throw SMIMEException.getInstance(this, e, "getLengthOctets" );
280     }
281     return returnByteArray;
282   }
283
284 /**
285  * Returns total length of DER object
286  * @return Total length of DER Object (involves identifier octet, length
287  * octets and content octets)
288  */

289   public int getTotalSize () {
290     if (tagTypeNumber == 5)
291       return totalLength + 1;
292     return totalLength;
293   }
294 }
295
296
297
298
Popular Tags