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