KickJava   Java API By Example, From Geeks To Geeks.

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


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

7
8
9 package org.enhydra.oyster.der;
10
11 import org.enhydra.oyster.exception.SMIMEException;
12 import org.enhydra.oyster.exception.ErrorStorage;
13
14
15 /**
16  * DERObjectIdentifier is primitive type of DER encoded object which represents
17  * Object Identifier type in ASN.1 notation. A value (distinguishable from all
18  * other such values) which is associated with an information object. For
19  * example object identifier for RSA algorithm is 1.2.840.113549.1.1.1.
20  * Implemented object identifiers are stored in class IdentifierStorage.
21  */

22 public class DERObjectIdentifier extends DERObject {
23
24 /**
25  * This constructor has two different forms, depend on parameter typeConstruction0,
26  * which can be: DOT_SEPARATED_ARRAY or NAME_STRING. If typeConstruction0 parameter
27  * is DOT_SEPARATED_ARRAY then id0 definition is represented by numbers separated
28  * with dots (example: "1.2.840.113549.1.1.1"). In the case of NAME_STRING, id0
29  * definition is name of object identifier (example: "RSA").
30  * @param id0 defines Object Identifier in representation determined by the
31  * second parameter - typeConstruction0.
32  * @param typeConstruction0 can take values DOT_SEPARATED_ARRAY and NAME_STRING
33  * @exception SMIMEException if wrong type of parameters are passed to the
34  * constructor. Also, exception could be thrown in super class constructor or
35  * in super class addContent method.
36  */

37   public DERObjectIdentifier (String JavaDoc id0, String JavaDoc typeConstruction0) throws SMIMEException
38   {
39     super(6);
40     byte[] contentID; // For storing content of ID identifiers byte string
41
if (typeConstruction0.equalsIgnoreCase("DOT_SEPARATED_ARRAY")) // Construction with dot separated string of numbers
42
{
43       int[] temp;
44       int[] dotPosition;
45       int j = -1, i = 0;
46       do // Counting number of dots to find out how many integers will contain Object Identificator string
47
{
48         j = id0.indexOf('.', j + 1);
49         i++;
50       } while (j != -1);
51       if (i == 1)
52         throw new SMIMEException(this, 1008);
53       temp = new int[i];
54       dotPosition = new int[i - 1];
55       i = 0;
56       j = -1;
57       do // Filling dot position
58
{
59         j = id0.indexOf('.', j + 1);
60         if (j != -1)
61           dotPosition[i] = j;
62         i++;
63       } while (j != -1);
64       temp[0] = Integer.decode(id0.substring(0, dotPosition[0])).intValue(); // First number in Identificator object string
65
temp[temp.length - 1] = Integer.decode(id0.substring(dotPosition[dotPosition.length - 1] + 1)).intValue(); // Last number in Identificator object string
66
for (i = 1; i != temp.length - 1; i++)
67         temp[i] = Integer.decode(id0.substring(dotPosition[i - 1] + 1, dotPosition[i])).intValue(); // Numbers between first and last number in Identificator object string
68
contentID = formatID(temp);
69       super.addContent(contentID); // Formating DER value of Object identificator
70
}
71     else if (typeConstruction0.equalsIgnoreCase("NAME_STRING")) {
72       contentID = formatID(IdentifierStorage.getID(id0.toUpperCase()));
73       super.addContent(contentID); // Formating DER value of Object identificator
74
}
75     else
76       throw new SMIMEException(this, 1009);
77   }
78
79 /**
80  * Array of numbers is used for construction of DERObjectIdentifier. Every number in
81  * array represents one number between dots in the object identifier string.
82  * @param arrayID0 array of given numbers (example: for RSA algorithm those
83  * numbers are 1, 2, 840, 113549, 1, 1, and 1).
84  * @exception SMIMEException if wrong type of parameters are passed to the
85  * constructor. Also, exception could be thrown in super class constructor or
86  * in super class addContent method.
87  */

88   public DERObjectIdentifier (int[] arrayID0) throws SMIMEException
89   {
90     super(6);
91     super.addContent(formatID(arrayID0));
92   }
93
94 /**
95  * Creats Object Identifier octet string from discret number identifiers
96  * @param id0 array of defined numbers for defined Object Identifier
97  * @return Byte array representation of the DER encoded content of the Object
98  * Identifier
99  * @exception SMIMEException in case that unknown type of object identifier is
100  * submited to constructors dealing with DOT_SEPARATED_ARRAY and NAME_STRING.
101  * Also, it can be caused by non SMIMEException which is:
102  * UnsupportedEncodingException.
103  */

104   private byte[] formatID (int[] id0) throws SMIMEException {
105     int[] temp = new int[id0.length - 1];
106     String JavaDoc s = new String JavaDoc();
107     byte[] returnByteArray = null;
108     if (id0[0] == -1)
109       throw new SMIMEException(this, 1010);
110     temp[0] = 40*id0[0] + id0[1]; // First byte is constructed from the first and second discret numbers (that`s a rule)
111
for (int i = 2; i != id0.length; i++)
112       temp[i - 1] = id0[i];
113     try {
114       for (int i = 0; i != temp.length; i++) {
115         int j = 1; // j: Number of required bits for particular element of array "temp"
116
for (int a = 1; (a*2) <= temp[i]; j++) // Counting number of required bits
117
a = a*2;
118         j = (int)Math.ceil((double)j/7); // j: Number of required bytes for particular element of array "temp"
119
byte[] tempElement = new byte[j];
120         for (j = tempElement.length - 1; j >= 0; j--) {
121           tempElement[j] = (byte)((temp[i] >> (7*(tempElement.length - 1 - j))) & 0x7F);
122           if (j != (tempElement.length - 1))
123             tempElement[j] = (byte)(tempElement[j] | (0x80));
124         }
125         s = s.concat(new String JavaDoc(tempElement, "ISO-8859-1"));
126       }
127       returnByteArray = s.getBytes("ISO-8859-1");
128     }
129     catch(Exception JavaDoc e) {
130       throw SMIMEException.getInstance(this, e, "formatID" );
131     }
132     return returnByteArray;
133   }
134 }
135
136
137
138
Popular Tags