View Javadoc
1   /*
2    * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package org.ietf.jgss;
27  
28  import java.io.InputStream;
29  import java.io.IOException;
30  import sun.security.util.DerValue;
31  import sun.security.util.DerOutputStream;
32  import sun.security.util.ObjectIdentifier;
33  
34  /**
35   * This class represents Universal Object Identifiers (Oids) and their
36   * associated operations.<p>
37   *
38   * Oids are hierarchically globally-interpretable identifiers used
39   * within the GSS-API framework to identify mechanisms and name formats.<p>
40   *
41   * The structure and encoding of Oids is defined in ISOIEC-8824 and
42   * ISOIEC-8825.  For example the Oid representation of Kerberos V5
43   * mechanism is "1.2.840.113554.1.2.2"<p>
44   *
45   * The GSSName name class contains public static Oid objects
46   * representing the standard name types defined in GSS-API.
47   *
48   * @author Mayank Upadhyay
49   * @since 1.4
50   */
51  public class Oid {
52  
53      private ObjectIdentifier oid;
54      private byte[] derEncoding;
55  
56      /**
57       * Constructs an Oid object from a string representation of its
58       * integer components.
59       *
60       * @param strOid the dot separated string representation of the oid.
61       * For instance, "1.2.840.113554.1.2.2".
62       * @exception GSSException may be thrown when the string is incorrectly
63       *     formatted
64       */
65      public Oid(String strOid) throws GSSException {
66  
67          try {
68              oid = new ObjectIdentifier(strOid);
69              derEncoding = null;
70          } catch (Exception e) {
71              throw new GSSException(GSSException.FAILURE,
72                            "Improperly formatted Object Identifier String - "
73                            + strOid);
74          }
75      }
76  
77      /**
78       * Creates an Oid object from its ASN.1 DER encoding.  This refers to
79       * the full encoding including tag and length.  The structure and
80       * encoding of Oids is defined in ISOIEC-8824 and ISOIEC-8825.  This
81       * method is identical in functionality to its byte array counterpart.
82       *
83       * @param derOid stream containing the DER encoded oid
84       * @exception GSSException may be thrown when the DER encoding does not
85       *  follow the prescribed format.
86       */
87      public Oid(InputStream derOid) throws GSSException {
88          try {
89              DerValue derVal = new DerValue(derOid);
90              derEncoding = derVal.toByteArray();
91              oid = derVal.getOID();
92          } catch (IOException e) {
93              throw new GSSException(GSSException.FAILURE,
94                            "Improperly formatted ASN.1 DER encoding for Oid");
95          }
96      }
97  
98  
99      /**
100      * Creates an Oid object from its ASN.1 DER encoding.  This refers to
101      * the full encoding including tag and length.  The structure and
102      * encoding of Oids is defined in ISOIEC-8824 and ISOIEC-8825.  This
103      * method is identical in functionality to its InputStream conterpart.
104      *
105      * @param data byte array containing the DER encoded oid
106      * @exception GSSException may be thrown when the DER encoding does not
107      *     follow the prescribed format.
108      */
109     public Oid(byte [] data) throws GSSException {
110         try {
111             DerValue derVal = new DerValue(data);
112             derEncoding = derVal.toByteArray();
113             oid = derVal.getOID();
114         } catch (IOException e) {
115             throw new GSSException(GSSException.FAILURE,
116                           "Improperly formatted ASN.1 DER encoding for Oid");
117         }
118     }
119 
120     /**
121      * Only for calling by initializators used with declarations.
122      *
123      * @param strOid
124      */
125     static Oid getInstance(String strOid) {
126         Oid retVal = null;
127         try {
128             retVal =  new Oid(strOid);
129         } catch (GSSException e) {
130             // squelch it!
131         }
132         return retVal;
133     }
134 
135     /**
136      * Returns a string representation of the oid's integer components
137      * in dot separated notation.
138      *
139      * @return string representation in the following format: "1.2.3.4.5"
140      */
141     public String toString() {
142         return oid.toString();
143     }
144 
145     /**
146      * Tests if two Oid objects represent the same Object identifier
147      * value.
148      *
149      * @return <code>true</code> if the two Oid objects represent the same
150      * value, <code>false</code> otherwise.
151      * @param other the Oid object that has to be compared to this one
152      */
153     public boolean equals(Object other) {
154 
155         //check if both reference the same object
156         if (this == other)
157             return (true);
158 
159         if (other instanceof Oid)
160             return this.oid.equals((Object)((Oid) other).oid);
161         else if (other instanceof ObjectIdentifier)
162             return this.oid.equals(other);
163         else
164             return false;
165     }
166 
167 
168     /**
169      * Returns the full ASN.1 DER encoding for this oid object, which
170      * includes the tag and length.
171      *
172      * @return byte array containing the DER encoding of this oid object.
173      * @exception GSSException may be thrown when the oid can't be encoded
174      */
175     public byte[] getDER() throws GSSException {
176 
177         if (derEncoding == null) {
178             DerOutputStream dout = new DerOutputStream();
179             try {
180                 dout.putOID(oid);
181             } catch (IOException e) {
182                 throw new GSSException(GSSException.FAILURE, e.getMessage());
183             }
184             derEncoding = dout.toByteArray();
185         }
186 
187         return derEncoding.clone();
188     }
189 
190     /**
191      * A utility method to test if this Oid value is contained within the
192      * supplied Oid array.
193      *
194      * @param oids the array of Oid's to search
195      * @return true if the array contains this Oid value, false otherwise
196      */
197     public boolean containedIn(Oid[] oids) {
198 
199         for (int i = 0; i < oids.length; i++) {
200             if (oids[i].equals(this))
201                 return (true);
202         }
203 
204         return (false);
205     }
206 
207 
208     /**
209      * Returns a hashcode value for this Oid.
210      *
211      * @return a hashCode value
212      */
213     public int hashCode() {
214         return oid.hashCode();
215     }
216 }