Path: blob/master/src/java.security.jgss/share/classes/org/ietf/jgss/Oid.java
41155 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package org.ietf.jgss;2627import java.io.InputStream;28import java.io.IOException;29import sun.security.util.DerValue;30import sun.security.util.DerOutputStream;31import sun.security.util.ObjectIdentifier;3233/**34* This class represents Universal Object Identifiers (Oids) and their35* associated operations.<p>36*37* Oids are hierarchically globally-interpretable identifiers used38* within the GSS-API framework to identify mechanisms and name formats.<p>39*40* The structure and encoding of Oids is defined in ISOIEC-8824 and41* ISOIEC-8825. For example the Oid representation of Kerberos V542* mechanism is "1.2.840.113554.1.2.2"<p>43*44* The GSSName name class contains public static Oid objects45* representing the standard name types defined in GSS-API.46*47* @author Mayank Upadhyay48* @since 1.449*/50public class Oid {5152private ObjectIdentifier oid;53private byte[] derEncoding;5455/**56* Constructs an Oid object from a string representation of its57* integer components.58*59* @param strOid the dot separated string representation of the oid.60* For instance, "1.2.840.113554.1.2.2".61* @exception GSSException may be thrown when the string is incorrectly62* formatted63*/64public Oid(String strOid) throws GSSException {6566try {67oid = ObjectIdentifier.of(strOid);68derEncoding = null;69} catch (Exception e) {70throw new GSSException(GSSException.FAILURE,71"Improperly formatted Object Identifier String - "72+ strOid);73}74}7576/**77* Creates an Oid object from its ASN.1 DER encoding. This refers to78* the full encoding including tag and length. The structure and79* encoding of Oids is defined in ISOIEC-8824 and ISOIEC-8825. This80* method is identical in functionality to its byte array counterpart.81*82* @param derOid stream containing the DER encoded oid83* @exception GSSException may be thrown when the DER encoding does not84* follow the prescribed format.85*/86public Oid(InputStream derOid) throws GSSException {87try {88DerValue derVal = new DerValue(derOid);89derEncoding = derVal.toByteArray();90oid = derVal.getOID();91} catch (IOException e) {92throw new GSSException(GSSException.FAILURE,93"Improperly formatted ASN.1 DER encoding for Oid");94}95}969798/**99* Creates an Oid object from its ASN.1 DER encoding. This refers to100* the full encoding including tag and length. The structure and101* encoding of Oids is defined in ISOIEC-8824 and ISOIEC-8825. This102* method is identical in functionality to its InputStream conterpart.103*104* @param data byte array containing the DER encoded oid105* @exception GSSException may be thrown when the DER encoding does not106* follow the prescribed format.107*/108public Oid(byte [] data) throws GSSException {109try {110DerValue derVal = new DerValue(data);111derEncoding = derVal.toByteArray();112oid = derVal.getOID();113} catch (IOException e) {114throw new GSSException(GSSException.FAILURE,115"Improperly formatted ASN.1 DER encoding for Oid");116}117}118119/**120* Only for calling by initializators used with declarations.121*122* @param strOid123*/124static Oid getInstance(String strOid) {125Oid retVal = null;126try {127retVal = new Oid(strOid);128} catch (GSSException e) {129// squelch it!130}131return retVal;132}133134/**135* Returns a string representation of the oid's integer components136* in dot separated notation.137*138* @return string representation in the following format: "1.2.3.4.5"139*/140public String toString() {141return oid.toString();142}143144/**145* Tests if two Oid objects represent the same Object identifier146* value.147*148* @return <code>true</code> if the two Oid objects represent the same149* value, <code>false</code> otherwise.150* @param other the Oid object that has to be compared to this one151*/152public boolean equals(Object other) {153154//check if both reference the same object155if (this == other)156return (true);157158if (other instanceof Oid)159return this.oid.equals(((Oid) other).oid);160else if (other instanceof ObjectIdentifier)161return this.oid.equals(other);162else163return false;164}165166167/**168* Returns the full ASN.1 DER encoding for this oid object, which169* includes the tag and length.170*171* @return byte array containing the DER encoding of this oid object.172* @exception GSSException may be thrown when the oid can't be encoded173*/174public byte[] getDER() throws GSSException {175176if (derEncoding == null) {177DerOutputStream dout = new DerOutputStream();178try {179dout.putOID(oid);180} catch (IOException e) {181throw new GSSException(GSSException.FAILURE, e.getMessage());182}183derEncoding = dout.toByteArray();184}185186return derEncoding.clone();187}188189/**190* A utility method to test if this Oid value is contained within the191* supplied Oid array.192*193* @param oids the array of Oid's to search194* @return true if the array contains this Oid value, false otherwise195*/196public boolean containedIn(Oid[] oids) {197198for (int i = 0; i < oids.length; i++) {199if (oids[i].equals(this))200return (true);201}202203return (false);204}205206207/**208* Returns a hashcode value for this Oid.209*210* @return a hashCode value211*/212public int hashCode() {213return oid.hashCode();214}215}216217218