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