jdk/src/share/classes/sun/security/jgss/krb5/Krb5NameElement.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 10369 e9d2e59e53f0
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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 sun.security.jgss.krb5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import org.ietf.jgss.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.security.jgss.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.security.auth.kerberos.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.krb5.PrincipalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.krb5.KrbException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.krb5.ServiceName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Implements the GSSNameSpi for the krb5 mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Mayank Upadhyay
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class Krb5NameElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    implements GSSNameSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private PrincipalName krb5PrincipalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private String gssNameStr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private Oid gssNameType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // XXX Move this concept into PrincipalName's asn1Encode() sometime
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static String CHAR_ENCODING = "UTF-8";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private Krb5NameElement(PrincipalName principalName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                            String gssNameStr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                            Oid gssNameType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.krb5PrincipalName = principalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.gssNameStr = gssNameStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.gssNameType = gssNameType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Instantiates a new Krb5NameElement object. Internally it stores the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * information provided by the input parameters so that they may later
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * be used for output when a printable representaion of this name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * needed in GSS-API format rather than in Kerberos format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static Krb5NameElement getInstance(String gssNameStr, Oid gssNameType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
         * A null gssNameType implies that the mechanism default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
         * Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (gssNameType == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            gssNameType = Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (!gssNameType.equals(GSSName.NT_USER_NAME) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                !gssNameType.equals(GSSName.NT_HOSTBASED_SERVICE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                !gssNameType.equals(Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                !gssNameType.equals(GSSName.NT_EXPORT_NAME))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                throw new GSSException(GSSException.BAD_NAMETYPE, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                       gssNameType.toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                       +" is an unsupported nametype");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        PrincipalName principalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (gssNameType.equals(GSSName.NT_EXPORT_NAME) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                gssNameType.equals(Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                principalName = new PrincipalName(gssNameStr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                  PrincipalName.KRB_NT_PRINCIPAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                String[] components = getComponents(gssNameStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                 * We have forms of GSS name strings that can come in:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                 * 1. names of the form "foo" with just one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                 * component. (This might include a "@" but only in escaped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                 * form like "\@")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                 * 2. names of the form "foo@bar" with two components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                 * The nametypes that are accepted are NT_USER_NAME, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                 * NT_HOSTBASED_SERVICE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                if (gssNameType.equals(GSSName.NT_USER_NAME))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    principalName = new PrincipalName(gssNameStr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                    PrincipalName.KRB_NT_PRINCIPAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    String hostName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    String service = components[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    if (components.length >= 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        hostName = components[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    String principal = getHostBasedInstance(service, hostName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    principalName = new ServiceName(principal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                            PrincipalName.KRB_NT_SRV_HST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new GSSException(GSSException.BAD_NAME, -1, e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return new Krb5NameElement(principalName, gssNameStr, gssNameType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    static Krb5NameElement getInstance(PrincipalName principalName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return new Krb5NameElement(principalName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                                   principalName.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                   Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private static String[] getComponents(String gssNameStr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        String[] retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // XXX Perhaps provide this parsing code in PrincipalName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // Look for @ as in service@host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // Assumes host name will not have an escaped '@'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        int separatorPos = gssNameStr.lastIndexOf('@', gssNameStr.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // Not really a separator if it is escaped. Then this is just part
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // of the principal name or service name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if ((separatorPos > 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                (gssNameStr.charAt(separatorPos-1) == '\\')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            // Is the `\` character escaped itself?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if ((separatorPos - 2 < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                (gssNameStr.charAt(separatorPos-2) != '\\'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                separatorPos = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (separatorPos > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            String serviceName = gssNameStr.substring(0, separatorPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            String hostName = gssNameStr.substring(separatorPos+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            retVal = new String[] { serviceName, hostName};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            retVal = new String[] {gssNameStr};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private static String getHostBasedInstance(String serviceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                               String hostName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            StringBuffer temp = new StringBuffer(serviceName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                // A lack of "@" defaults to the service being on the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                // host as per RFC 2743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                // XXX Move this part into JGSS framework
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                if (hostName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    hostName = InetAddress.getLocalHost().getHostName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                // use hostname as it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            hostName = hostName.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            temp = temp.append('/').append(hostName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return temp.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public final PrincipalName getKrb5PrincipalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return krb5PrincipalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Equal method for the GSSNameSpi objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * If either name denotes an anonymous principal, the call should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param name to be compared with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @returns true if they both refer to the same entity, else false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @exception GSSException with major codes of BAD_NAMETYPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *  BAD_NAME, FAILURE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public boolean equals(GSSNameSpi other) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (other == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (other instanceof Krb5NameElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                Krb5NameElement that = (Krb5NameElement) other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                return (this.krb5PrincipalName.getName().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                            that.krb5PrincipalName.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Compares this <code>GSSNameSpi</code> object to another Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * that might be a <code>GSSNameSpi</code>. The behaviour is exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * the same as in {@link #equals(GSSNameSpi) equals} except that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * no GSSException is thrown; instead, false will be returned in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * situation where an error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @param another the object to be compared to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @returns true if they both refer to the same entity, else false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @see #equals(GSSNameSpi)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public boolean equals(Object another) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (this == another) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (another instanceof Krb5NameElement)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                 return equals((Krb5NameElement) another);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        } catch (GSSException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            // ignore exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Returns a hashcode value for this GSSNameSpi.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @return a hashCode value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return 37 * 17 + krb5PrincipalName.getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Returns the principal name in the form user@REALM or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * host/service@REALM but with the following contraints that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * imposed by RFC 1964:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *  (1) all occurrences of the characters `@`,  `/`, and `\` within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *   principal components or realm names shall be quoted with an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *   immediately-preceding `\`.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *   (2) all occurrences of the null, backspace, tab, or newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *   characters within principal components or realm names will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *   represented, respectively, with `\0`, `\b`, `\t`, or `\n`.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *   (3) the `\` quoting character shall not be emitted within an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *   exported name except to accomodate cases (1) and (2).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public byte[] export() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        // XXX Apply the above constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        byte[] retVal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            retVal = krb5PrincipalName.getName().getBytes(CHAR_ENCODING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            // Can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Get the mechanism type that this NameElement corresponds to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @return the Oid of the mechanism type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public Oid getMechanism() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return (Krb5MechFactory.GSS_KRB5_MECH_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * Returns a string representation for this name. The printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * name type can be obtained by calling getStringNameType().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @return string form of this name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @see #getStringNameType()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @overrides Object#toString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return (gssNameStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // For testing: return (super.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Returns the name type oid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public Oid getGSSNameType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return (gssNameType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Returns the oid describing the format of the printable name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @return the Oid for the format of the printed name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public Oid getStringNameType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        // XXX For NT_EXPORT_NAME return a different name type. Infact,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        // don't even store NT_EXPORT_NAME in the cons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return (gssNameType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Indicates if this name object represents an Anonymous name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public boolean isAnonymousName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return (gssNameType.equals(GSSName.NT_ANONYMOUS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return Krb5MechFactory.PROVIDER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
}