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