KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.OutputStream JavaDoc;
25
26 public class DERObjectIdentifier
27     extends DERObject
28 {
29     String JavaDoc identifier;
30
31     /**
32      * return an OID from the passed in object
33      *
34      * @exception IllegalArgumentException if the object cannot be converted.
35      */

36     public static DERObjectIdentifier getInstance(
37         Object JavaDoc obj)
38     {
39         if (obj == null || obj instanceof DERObjectIdentifier)
40         {
41             return (DERObjectIdentifier)obj;
42         }
43
44         if (obj instanceof ASN1OctetString)
45         {
46             return new DERObjectIdentifier(((ASN1OctetString)obj).getOctets());
47         }
48
49         if (obj instanceof ASN1TaggedObject)
50         {
51             return getInstance(((ASN1TaggedObject)obj).getObject());
52         }
53
54         throw new IllegalArgumentException JavaDoc("illegal object in getInstance: " + obj.getClass().getName());
55     }
56
57     /**
58      * return an Object Identifier from a tagged object.
59      *
60      * @param obj the tagged object holding the object we want
61      * @param explicit true if the object is meant to be explicitly
62      * tagged false otherwise.
63      * @exception IllegalArgumentException if the tagged object cannot
64      * be converted.
65      */

66     public static DERObjectIdentifier getInstance(
67         ASN1TaggedObject obj,
68         boolean explicit)
69     {
70         return getInstance(obj.getObject());
71     }
72
73
74     DERObjectIdentifier(
75         byte[] bytes)
76     {
77         StringBuffer JavaDoc objId = new StringBuffer JavaDoc();
78         int value = 0;
79         boolean first = true;
80
81         for (int i = 0; i != bytes.length; i++)
82         {
83             int b = bytes[i] & 0xff;
84
85             value = value * 128 + (b & 0x7f);
86             if ((b & 0x80) == 0) // end of number reached
87
{
88                 if (first)
89                 {
90                     switch (value / 40)
91                     {
92                     case 0:
93                         objId.append('0');
94                         break;
95                     case 1:
96                         objId.append('1');
97                         value -= 40;
98                         break;
99                     default:
100                         objId.append('2');
101                         value -= 80;
102                     }
103                     first = false;
104                 }
105
106                 objId.append('.');
107                 objId.append(Integer.toString(value));
108                 value = 0;
109             }
110         }
111
112         this.identifier = objId.toString();
113     }
114
115     public DERObjectIdentifier(
116         String JavaDoc identifier)
117     {
118         this.identifier = identifier;
119     }
120
121     public String JavaDoc getId()
122     {
123         return identifier;
124     }
125
126     private void writeField(
127         OutputStream JavaDoc out,
128         int fieldValue)
129         throws IOException JavaDoc
130     {
131         if (fieldValue >= (1 << 7))
132         {
133             if (fieldValue >= (1 << 14))
134             {
135                 if (fieldValue >= (1 << 21))
136                 {
137                     if (fieldValue >= (1 << 28))
138                     {
139                         out.write((fieldValue >> 28) | 0x80);
140                     }
141                     out.write((fieldValue >> 21) | 0x80);
142                 }
143                 out.write((fieldValue >> 14) | 0x80);
144             }
145             out.write((fieldValue >> 7) | 0x80);
146         }
147         out.write(fieldValue & 0x7f);
148     }
149
150     void encode(
151         DEROutputStream out)
152         throws IOException JavaDoc
153     {
154         OIDTokenizer tok = new OIDTokenizer(identifier);
155         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
156         DEROutputStream dOut = new DEROutputStream(bOut);
157
158         writeField(bOut,
159                     Integer.parseInt(tok.nextToken()) * 40
160                     + Integer.parseInt(tok.nextToken()));
161
162         while (tok.hasMoreTokens())
163         {
164             writeField(bOut, Integer.parseInt(tok.nextToken()));
165         }
166
167         dOut.close();
168
169         byte[] bytes = bOut.toByteArray();
170
171         out.writeEncoded(OBJECT_IDENTIFIER, bytes);
172     }
173
174     public int hashCode()
175     {
176         return identifier.hashCode();
177     }
178
179     public boolean equals(
180         Object JavaDoc o)
181     {
182         if ((o == null) || !(o instanceof DERObjectIdentifier))
183         {
184             return false;
185         }
186
187         return identifier.equals(((DERObjectIdentifier)o).identifier);
188     }
189 }
190
Popular Tags