src/java.security.jgss/share/classes/javax/security/auth/kerberos/KerberosPrincipal.java
author mbalao
Tue, 12 Nov 2019 12:36:16 -0300
changeset 59036 642d7d7ed426
parent 55258 d65d3c37232c
permissions -rw-r--r--
8233946: Add @since 13 annotation to KerberosPrincipal.KRB_NT_ENTERPRISE field Reviewed-by: clanger
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55258
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
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 javax.security.auth.kerberos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.security.krb5.KrbException;
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.Realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class encapsulates a Kerberos principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author Mayank Upadhyay
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public final class KerberosPrincipal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    implements java.security.Principal, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final long serialVersionUID = -7374788026156829911L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    //name types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * unknown name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static final int KRB_NT_UNKNOWN =   0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * user principal name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static final int KRB_NT_PRINCIPAL = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * service and other unique instance (krbtgt) name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static final int KRB_NT_SRV_INST =  2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * service with host name as instance (telnet, rcommands) name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static final int KRB_NT_SRV_HST =   3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * service with host as remaining components name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static final int KRB_NT_SRV_XHST =  4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * unique ID name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public static final int KRB_NT_UID = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
55258
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 47216
diff changeset
    83
    /**
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 47216
diff changeset
    84
     * Enterprise name (alias)
59036
642d7d7ed426 8233946: Add @since 13 annotation to KerberosPrincipal.KRB_NT_ENTERPRISE field
mbalao
parents: 55258
diff changeset
    85
     *
642d7d7ed426 8233946: Add @since 13 annotation to KerberosPrincipal.KRB_NT_ENTERPRISE field
mbalao
parents: 55258
diff changeset
    86
     * @since 13
55258
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 47216
diff changeset
    87
     */
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 47216
diff changeset
    88
    public static final int KRB_NT_ENTERPRISE = 10;
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 47216
diff changeset
    89
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private transient String fullName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private transient String realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private transient int nameType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
    98
     * Constructs a {@code KerberosPrincipal} from the provided string input.
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
    99
     * The name type for this principal defaults to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * {@link #KRB_NT_PRINCIPAL KRB_NT_PRINCIPAL}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * This string is assumed to contain a name in the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * that is specified in Section 2.1.1. (Kerberos Principal Name Form) of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <a href=http://www.ietf.org/rfc/rfc1964.txt> RFC 1964 </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * (for example, <i>duke@FOO.COM</i>, where <i>duke</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * represents a principal, and <i>FOO.COM</i> represents a realm).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <p>If the input name does not contain a realm, the default realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * is used. The default realm can be specified either in a Kerberos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * configuration file or via the java.security.krb5.realm
45665
6f21cd7ec80e 8178114: Fix guide links in security APIs
wetmore
parents: 33282
diff changeset
   110
     * system property. For more information, see the
6f21cd7ec80e 8178114: Fix guide links in security APIs
wetmore
parents: 33282
diff changeset
   111
     * {@extLink security_guide_jgss_tutorial Kerberos Requirements}.
6f21cd7ec80e 8178114: Fix guide links in security APIs
wetmore
parents: 33282
diff changeset
   112
     * Additionally, if a security manager is
33282
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   113
     * installed, a {@link ServicePermission} must be granted and the service
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   114
     * principal of the permission must minimally be inside the
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   115
     * {@code KerberosPrincipal}'s realm. For example, if the result of
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   116
     * {@code new KerberosPrincipal("user")} is {@code user@EXAMPLE.COM},
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   117
     * then a {@code ServicePermission} with service principal
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   118
     * {@code host/www.example.com@EXAMPLE.COM} (and any action)
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   119
     * must be granted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param name the principal name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @throws IllegalArgumentException if name is improperly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * formatted, if name is null, or if name does not contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * the realm to use and the default realm is not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * in either a Kerberos configuration file or via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * java.security.krb5.realm system property.
33282
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   127
     * @throws SecurityException if a security manager is installed and
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   128
     * {@code name} does not contain the realm to use, and a proper
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   129
     * {@link ServicePermission} as described above is not granted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public KerberosPrincipal(String name) {
33282
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   132
        this(name, KRB_NT_PRINCIPAL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   136
     * Constructs a {@code KerberosPrincipal} from the provided string and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * name type input.  The string is assumed to contain a name in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * format that is specified in Section 2.1 (Mandatory Name Forms) of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <a href=http://www.ietf.org/rfc/rfc1964.txt>RFC 1964</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Valid name types are specified in Section 6.2 (Principal Names) of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * <a href=http://www.ietf.org/rfc/rfc4120.txt>RFC 4120</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * The input name must be consistent with the provided name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * (for example, <i>duke@FOO.COM</i>, is a valid input string for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * name type, KRB_NT_PRINCIPAL where <i>duke</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * represents a principal, and <i>FOO.COM</i> represents a realm).
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   146
     *
33282
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   147
     * <p>If the input name does not contain a realm, the default realm
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * is used. The default realm can be specified either in a Kerberos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * configuration file or via the java.security.krb5.realm
45665
6f21cd7ec80e 8178114: Fix guide links in security APIs
wetmore
parents: 33282
diff changeset
   150
     * system property. For more information, see the
6f21cd7ec80e 8178114: Fix guide links in security APIs
wetmore
parents: 33282
diff changeset
   151
     * {@extLink security_guide_jgss_tutorial Kerberos Requirements}.
6f21cd7ec80e 8178114: Fix guide links in security APIs
wetmore
parents: 33282
diff changeset
   152
     * Additionally, if a security manager is
33282
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   153
     * installed, a {@link ServicePermission} must be granted and the service
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   154
     * principal of the permission must minimally be inside the
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   155
     * {@code KerberosPrincipal}'s realm. For example, if the result of
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   156
     * {@code new KerberosPrincipal("user")} is {@code user@EXAMPLE.COM},
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   157
     * then a {@code ServicePermission} with service principal
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   158
     * {@code host/www.example.com@EXAMPLE.COM} (and any action)
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   159
     * must be granted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param name the principal name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param nameType the name type of the principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @throws IllegalArgumentException if name is improperly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * formatted, if name is null, if the nameType is not supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * or if name does not contain the realm to use and the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * realm is not specified in either a Kerberos configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * file or via the java.security.krb5.realm system property.
33282
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   168
     * @throws SecurityException if a security manager is installed and
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   169
     * {@code name} does not contain the realm to use, and a proper
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   170
     * {@link ServicePermission} as described above is not granted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public KerberosPrincipal(String name, int nameType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        PrincipalName krb5Principal = null;
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
            // Appends the default realm if it is missing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            krb5Principal  = new PrincipalName(name,nameType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new IllegalArgumentException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
33282
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   184
        if (krb5Principal.isRealmDeduced() && !Realm.AUTODEDUCEREALM) {
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   185
            SecurityManager sm = System.getSecurityManager();
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   186
            if (sm != null) {
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   187
                try {
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   188
                    sm.checkPermission(new ServicePermission(
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   189
                            "@" + krb5Principal.getRealmAsString(), "-"));
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   190
                } catch (SecurityException se) {
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   191
                    // Swallow the actual exception to hide info
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   192
                    throw new SecurityException("Cannot read realm info");
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   193
                }
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   194
            }
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 27771
diff changeset
   195
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        this.nameType = nameType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        fullName = krb5Principal.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        realm = krb5Principal.getRealmString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Returns the realm component of this Kerberos principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @return the realm component of this Kerberos principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public String getRealm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   210
     * Returns a hash code for this {@code KerberosPrincipal}. The hash code
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   211
     * is defined to be the result of the following calculation:
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
   212
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *  hashCode = getName().hashCode();
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
   214
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   216
     * @return a hash code for this {@code KerberosPrincipal}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   223
     * Compares the specified object with this principal for equality.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Returns true if the given object is also a
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
   225
     * {@code KerberosPrincipal} and the two
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
   226
     * {@code KerberosPrincipal} instances are equivalent.
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
   227
     * More formally two {@code KerberosPrincipal} instances are equal
19597
7a3478d94601 8015669: KerberosPrincipal::equals should ignore name-type
weijun
parents: 18830
diff changeset
   228
     * if the values returned by {@code getName()} are equal.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   230
     * @param other the object to compare to
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   231
     * @return true if the object passed in represents the same principal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * as this one, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (other == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (! (other instanceof KerberosPrincipal)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
19597
7a3478d94601 8015669: KerberosPrincipal::equals should ignore name-type
weijun
parents: 18830
diff changeset
   242
        String myFullName = getName();
7a3478d94601 8015669: KerberosPrincipal::equals should ignore name-type
weijun
parents: 18830
diff changeset
   243
        String otherFullName = ((KerberosPrincipal) other).getName();
7a3478d94601 8015669: KerberosPrincipal::equals should ignore name-type
weijun
parents: 18830
diff changeset
   244
        return myFullName.equals(otherFullName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   248
     * Save the {@code KerberosPrincipal} object to a stream
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
   250
     * @serialData this {@code KerberosPrincipal} is serialized
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *          by writing out the PrincipalName and the
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   252
     *          Realm in their DER-encoded form as specified in Section 5.2.2 of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *          <a href=http://www.ietf.org/rfc/rfc4120.txt> RFC4120</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    private void writeObject(ObjectOutputStream oos)
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   256
            throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   258
        PrincipalName krb5Principal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        try {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   260
            krb5Principal  = new PrincipalName(fullName, nameType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            oos.writeObject(krb5Principal.asn1Encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            oos.writeObject(krb5Principal.getRealm().asn1Encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        } catch (Exception e) {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   264
            throw new IOException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
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
     * Reads this object from a stream (i.e., deserializes it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    private void readObject(ObjectInputStream ois)
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   272
            throws IOException, ClassNotFoundException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        byte[] asn1EncPrincipal = (byte [])ois.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        byte[] encRealm = (byte [])ois.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        try {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   276
           Realm realmObject = new Realm(new DerValue(encRealm));
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   277
           PrincipalName krb5Principal = new PrincipalName(
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   278
                   new DerValue(asn1EncPrincipal), realmObject);
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   279
           realm = realmObject.toString();
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   280
           fullName = krb5Principal.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
           nameType = krb5Principal.getNameType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        } catch (Exception e) {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   283
            throw new IOException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * The returned string corresponds to the single-string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * representation of a Kerberos Principal name as specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Section 2.1 of <a href=http://www.ietf.org/rfc/rfc1964.txt>RFC 1964</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @return the principal name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return fullName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   299
     * Returns the name type of the {@code KerberosPrincipal}. Valid name types
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * are specified in Section 6.2 of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * <a href=http://www.ietf.org/rfc/rfc4120.txt> RFC4120</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @return the name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public int getNameType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return nameType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   309
    /**
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   310
     * Returns an informative textual representation of this {@code KerberosPrincipal}.
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   311
     *
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   312
     * @return an informative textual representation of this {@code KerberosPrincipal}.
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   313
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
}