Create embedded-5_0-branch branch for development on ARM embedded cores.
[official-gcc.git] / embedded-5_0-branch / libjava / classpath / org / ietf / jgss / Oid.java
blobbd01f9cfe9b3d5744ba95babe1b52e0d80dddeef
1 /* Oid.java -- Object identifier class.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version.
38 The documentation comments of this class are derived from the text
39 of RFC 2853: Generic Security Service API Version 2: Java Bindings.
40 That document is covered under the following license notice:
42 Copyright (C) The Internet Society (2000). All Rights Reserved.
44 This document and translations of it may be copied and furnished to
45 others, and derivative works that comment on or otherwise explain it
46 or assist in its implementation may be prepared, copied, published and
47 distributed, in whole or in part, without restriction of any kind,
48 provided that the above copyright notice and this paragraph are
49 included on all such copies and derivative works. However, this
50 document itself may not be modified in any way, such as by removing
51 the copyright notice or references to the Internet Society or other
52 Internet organizations, except as needed for the purpose of developing
53 Internet standards in which case the procedures for copyrights defined
54 in the Internet Standards process must be followed, or as required to
55 translate it into languages other than English.
57 The limited permissions granted above are perpetual and will not be
58 revoked by the Internet Society or its successors or assigns.
60 This document and the information contained herein is provided on an
61 "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
62 TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT
63 NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN
64 WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
65 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. */
68 package org.ietf.jgss;
70 import gnu.java.lang.CPStringBuilder;
72 import java.io.ByteArrayInputStream;
73 import java.io.ByteArrayOutputStream;
74 import java.io.DataInputStream;
75 import java.io.InputStream;
76 import java.io.IOException;
77 import java.io.OutputStream;
79 import java.math.BigInteger;
81 import java.util.Arrays;
82 import java.util.StringTokenizer;
84 /**
85 * <p>This class represents Universal Object Identifiers (Oids) and their
86 * associated operations.</p>
88 * <p>Oids are hierarchically globally-interpretable identifiers used
89 * within the GSS-API framework to identify mechanisms and name formats.</p>
91 * <p>The structure and encoding of Oids is defined in ISOIEC-8824 and
92 * ISOIEC-8825. For example the Oid representation of Kerberos V5
93 * mechanism is "1.2.840.113554.1.2.2".</p>
95 * <p>The {@link GSSName} name class contains <code>public static Oid</code>
96 * objects representing the standard name types defined in GSS-API.</p>
98 public class Oid
101 // Constants and fields.
102 // -------------------------------------------------------------------------
104 private static final int OBJECT_IDENTIFIER = 0x06;
105 private static final int RELATIVE_OID = 0x0d;
107 private final int[] components;
108 private byte[] derOid;
109 private String strOid;
110 private boolean relative;
112 // Constructors.
113 // -------------------------------------------------------------------------
116 * Creates an Oid object from a string representation of its integer
117 * components (e.g. "1.2.840.113554.1.2.2").
119 * @param strOid The string representation for the oid.
120 * @throws GSSException If the argument is badly formed.
122 public Oid(String strOid) throws GSSException
124 if (strOid == null)
125 throw new NullPointerException();
126 this.strOid = strOid;
129 StringTokenizer tok = new StringTokenizer(strOid, ".");
130 components = new int[tok.countTokens()];
131 int i = 0;
132 while (tok.hasMoreTokens() && i < components.length)
134 components[i++] = Integer.parseInt(tok.nextToken());
137 catch (Exception x)
139 throw new GSSException(GSSException.FAILURE);
141 relative = false;
145 * Creates an Oid object from its DER encoding. This refers to the full
146 * encoding including tag and length. The structure and encoding of
147 * Oids is defined in ISOIEC-8824 and ISOIEC-8825. This method is
148 * identical in functionality to its byte array counterpart.
150 * @param derOid Stream containing the DER encoded oid.
151 * @throws GSSException If the DER stream is badly formed, or if the
152 * input stream throws an exception.
154 public Oid(InputStream derOid) throws GSSException
156 DataInputStream in = new DataInputStream(derOid);
159 int tag = in.read() & 0x1F;
160 if (tag != OBJECT_IDENTIFIER && tag != RELATIVE_OID)
161 throw new IOException();
162 int len = in.read();
163 if ((len & ~0x7F) != 0)
165 byte[] buf = new byte[len & 0x7F];
166 in.readFully(buf);
167 len = new BigInteger(1, buf).intValue();
169 if (len < 0)
170 throw new IOException();
171 byte[] enc = new byte[len];
172 in.readFully(enc);
173 int[] comp = new int[len + 1];
174 int count = 0;
175 int i = 0;
176 relative = tag == RELATIVE_OID;
177 if (!relative && i < len)
179 int j = (enc[i] & 0xFF);
180 comp[count++] = j / 40;
181 comp[count++] = j % 40;
182 i++;
184 while (i < len)
186 int j = 0;
189 j = enc[i++] & 0xFF;
190 comp[count] <<= 7;
191 comp[count] |= j & 0x7F;
192 if (i >= len && (j & 0x80) != 0)
193 throw new IOException();
195 while ((j & 0x80) != 0);
196 count++;
198 if (count == len)
199 this.components = comp;
200 else
202 this.components = new int[count];
203 System.arraycopy(comp, 0, components, 0, count);
206 catch (IOException ioe)
208 throw new GSSException(GSSException.FAILURE);
213 * Creates an Oid object from its DER encoding. This refers to the full
214 * encoding including tag and length. The structure and encoding of
215 * Oids is defined in ISOIEC-8824 and ISOIEC-8825. This method is
216 * identical in functionality to its streaming counterpart.
218 * @param derOid Byte array storing a DER encoded oid.
219 * @throws GSSException If the DER bytes are badly formed.
221 public Oid(byte[] derOid) throws GSSException
223 this(new ByteArrayInputStream(derOid));
224 this.derOid = (byte[]) derOid.clone();
227 Oid(int[] components)
229 this.components = components;
230 relative = false;
233 // Instance methods.
234 // -------------------------------------------------------------------------
237 * Returns a string representation of the oid's integer components in
238 * dot separated notation (e.g. "1.2.840.113554.1.2.2").
240 * @return The string representation of this oid.
242 public String toString()
244 if (strOid == null)
246 CPStringBuilder buf = new CPStringBuilder();
247 for (int i = 0; i < components.length; i++)
249 buf.append(components[i]);
250 if (i < components.length - 1)
251 buf.append('.');
253 strOid = buf.toString();
255 return strOid;
259 * Returns the full ASN.1 DER encoding for this oid object, which
260 * includes the tag and length.
262 * @return The ASN.1 DER encoding for this oid.
263 * @throws GSSException If encoding fails.
265 public byte[] getDER() throws GSSException
267 if (derOid == null)
269 ByteArrayOutputStream out = new ByteArrayOutputStream(256);
272 int i = 0;
273 if (!relative)
275 int b = components[i++] * 40 + (components.length > 1
276 ? components[i++] : 0);
277 encodeSubId(out, b);
279 for ( ; i < components.length; i++)
280 encodeSubId(out, components[i]);
281 byte[] oid = out.toByteArray();
282 out.reset();
283 if (relative)
284 out.write(RELATIVE_OID);
285 else
286 out.write(OBJECT_IDENTIFIER);
287 if (oid.length < 128)
288 out.write(oid.length);
289 else if (oid.length < 256)
291 out.write(0x81);
292 out.write(oid.length);
294 else if (oid.length < 65536)
296 out.write(0x82);
297 out.write((oid.length >>> 8) & 0xFF);
298 out.write(oid.length & 0xFF);
300 else if (oid.length < 16777216)
302 out.write(0x83);
303 out.write((oid.length >>> 16) & 0xFF);
304 out.write((oid.length >>> 8) & 0xFF);
305 out.write(oid.length & 0xFF);
307 else
309 out.write(0x84);
310 out.write((oid.length >>> 24) & 0xFF);
311 out.write((oid.length >>> 16) & 0xFF);
312 out.write((oid.length >>> 8) & 0xFF);
313 out.write(oid.length & 0xFF);
315 out.write(oid);
317 catch (IOException ioe)
319 throw new GSSException(GSSException.FAILURE);
321 derOid = out.toByteArray();
323 return (byte[]) derOid.clone();
327 * A utility method to test if an Oid object is contained within the
328 * supplied Oid object array.
330 * @param oids An array of oids to search.
331 * @return True if this oid is contained in the given array.
333 public boolean containedIn(Oid[] oids)
335 for (int i = 0; i < oids.length; i++)
337 if (equals(oids[i]))
338 return true;
340 return false;
343 public boolean equals(Object o)
345 if (!(o instanceof Oid))
346 return false;
347 Oid that = (Oid) o;
348 return Arrays.equals(components, that.components);
351 public int hashCode()
353 int code = 0;
354 for (int i = 0; i < components.length; i++)
355 code += components[i];
356 return code;
359 // Own methods.
360 // -------------------------------------------------------------------------
362 private static void encodeSubId(OutputStream out, int id) throws IOException
364 if (id < 128)
366 out.write(id);
368 else if (id < 16384)
370 out.write((id >>> 7) | 0x80);
371 out.write(id & 0x7F);
373 else if (id < 2097152)
375 out.write((id >>> 14) | 0x80);
376 out.write(((id >>> 7) | 0x80) & 0xFF);
377 out.write(id & 0x7F);
379 else if (id < 268435456)
381 out.write( (id >>> 21) | 0x80);
382 out.write(((id >>> 14) | 0x80) & 0xFF);
383 out.write(((id >>> 7) | 0x80) & 0xFF);
384 out.write(id & 0x7F);