0N/A/*
2362N/A * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage org.ietf.jgss;
0N/A
0N/Aimport java.io.InputStream;
0N/Aimport java.io.IOException;
0N/Aimport sun.security.util.DerValue;
0N/Aimport sun.security.util.DerOutputStream;
0N/Aimport sun.security.util.ObjectIdentifier;
0N/A
0N/A/**
0N/A * This class represents Universal Object Identifiers (Oids) and their
0N/A * associated operations.<p>
0N/A *
0N/A * Oids are hierarchically globally-interpretable identifiers used
0N/A * within the GSS-API framework to identify mechanisms and name formats.<p>
0N/A *
0N/A * The structure and encoding of Oids is defined in ISOIEC-8824 and
0N/A * ISOIEC-8825. For example the Oid representation of Kerberos V5
0N/A * mechanism is "1.2.840.113554.1.2.2"<p>
0N/A *
0N/A * The GSSName name class contains public static Oid objects
0N/A * representing the standard name types defined in GSS-API.
0N/A *
0N/A * @author Mayank Upadhyay
0N/A * @since 1.4
0N/A */
0N/Apublic class Oid {
0N/A
0N/A private ObjectIdentifier oid;
0N/A private byte[] derEncoding;
0N/A
0N/A /**
0N/A * Constructs an Oid object from a string representation of its
0N/A * integer components.
0N/A *
0N/A * @param strOid the dot separated string representation of the oid.
0N/A * For instance, "1.2.840.113554.1.2.2".
0N/A * @exception GSSException may be thrown when the string is incorrectly
0N/A * formatted
0N/A */
0N/A public Oid(String strOid) throws GSSException {
0N/A
0N/A try {
0N/A oid = new ObjectIdentifier(strOid);
0N/A derEncoding = null;
0N/A } catch (Exception e) {
0N/A throw new GSSException(GSSException.FAILURE,
0N/A "Improperly formatted Object Identifier String - "
0N/A + strOid);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Creates an Oid object from its ASN.1 DER encoding. This refers to
0N/A * the full encoding including tag and length. The structure and
0N/A * encoding of Oids is defined in ISOIEC-8824 and ISOIEC-8825. This
0N/A * method is identical in functionality to its byte array counterpart.
0N/A *
0N/A * @param derOid stream containing the DER encoded oid
0N/A * @exception GSSException may be thrown when the DER encoding does not
0N/A * follow the prescribed format.
0N/A */
0N/A public Oid(InputStream derOid) throws GSSException {
0N/A try {
0N/A DerValue derVal = new DerValue(derOid);
0N/A derEncoding = derVal.toByteArray();
0N/A oid = derVal.getOID();
0N/A } catch (IOException e) {
0N/A throw new GSSException(GSSException.FAILURE,
0N/A "Improperly formatted ASN.1 DER encoding for Oid");
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Creates an Oid object from its ASN.1 DER encoding. This refers to
0N/A * the full encoding including tag and length. The structure and
0N/A * encoding of Oids is defined in ISOIEC-8824 and ISOIEC-8825. This
0N/A * method is identical in functionality to its InputStream conterpart.
0N/A *
0N/A * @param data byte array containing the DER encoded oid
0N/A * @exception GSSException may be thrown when the DER encoding does not
0N/A * follow the prescribed format.
0N/A */
0N/A public Oid(byte [] data) throws GSSException {
0N/A try {
0N/A DerValue derVal = new DerValue(data);
0N/A derEncoding = derVal.toByteArray();
0N/A oid = derVal.getOID();
0N/A } catch (IOException e) {
0N/A throw new GSSException(GSSException.FAILURE,
0N/A "Improperly formatted ASN.1 DER encoding for Oid");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Only for calling by initializators used with declarations.
0N/A *
0N/A * @param strOid
0N/A */
0N/A static Oid getInstance(String strOid) {
0N/A Oid retVal = null;
0N/A try {
0N/A retVal = new Oid(strOid);
0N/A } catch (GSSException e) {
0N/A // squelch it!
0N/A }
0N/A return retVal;
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of the oid's integer components
0N/A * in dot separated notation.
0N/A *
0N/A * @return string representation in the following format: "1.2.3.4.5"
0N/A */
0N/A public String toString() {
0N/A return oid.toString();
0N/A }
0N/A
0N/A /**
0N/A * Tests if two Oid objects represent the same Object identifier
0N/A * value.
0N/A *
0N/A * @return <code>true</code> if the two Oid objects represent the same
0N/A * value, <code>false</code> otherwise.
0N/A * @param other the Oid object that has to be compared to this one
0N/A */
0N/A public boolean equals(Object other) {
0N/A
0N/A //check if both reference the same object
0N/A if (this == other)
0N/A return (true);
0N/A
0N/A if (other instanceof Oid)
0N/A return this.oid.equals(((Oid) other).oid);
0N/A else if (other instanceof ObjectIdentifier)
0N/A return this.oid.equals(other);
0N/A else
0N/A return false;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the full ASN.1 DER encoding for this oid object, which
0N/A * includes the tag and length.
0N/A *
0N/A * @return byte array containing the DER encoding of this oid object.
0N/A * @exception GSSException may be thrown when the oid can't be encoded
0N/A */
0N/A public byte[] getDER() throws GSSException {
0N/A
0N/A if (derEncoding == null) {
0N/A DerOutputStream dout = new DerOutputStream();
0N/A try {
0N/A dout.putOID(oid);
0N/A } catch (IOException e) {
0N/A throw new GSSException(GSSException.FAILURE, e.getMessage());
0N/A }
0N/A derEncoding = dout.toByteArray();
0N/A }
0N/A
0N/A return derEncoding.clone();
0N/A }
0N/A
0N/A /**
0N/A * A utility method to test if this Oid value is contained within the
0N/A * supplied Oid array.
0N/A *
0N/A * @param oids the array of Oid's to search
0N/A * @return true if the array contains this Oid value, false otherwise
0N/A */
0N/A public boolean containedIn(Oid[] oids) {
0N/A
0N/A for (int i = 0; i < oids.length; i++) {
0N/A if (oids[i].equals(this))
0N/A return (true);
0N/A }
0N/A
0N/A return (false);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a hashcode value for this Oid.
0N/A *
0N/A * @return a hashCode value
0N/A */
0N/A public int hashCode() {
0N/A return oid.hashCode();
0N/A }
0N/A}