jdk/src/java.security.jgss/share/classes/javax/security/auth/kerberos/KeyTab.java
author weijun
Wed, 26 Nov 2014 15:28:46 +0800
changeset 27771 360714d431ab
parent 25859 3317bb8137f4
child 29492 a4bf9a570035
permissions -rw-r--r--
8061253: Spec cleanup for some security-related classes Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     1
/*
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 15649
diff changeset
     2
 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     4
 *
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    10
 *
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    15
 * accompanied this code).
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    16
 *
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    20
 *
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    23
 * questions.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    24
 */
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    25
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    26
package javax.security.auth.kerberos;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    27
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    28
import java.io.File;
20844
3f51de06462f 8022931: Enhance Kerberos exceptions
weijun
parents: 18830
diff changeset
    29
import java.security.AccessControlException;
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    30
import java.util.Objects;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    31
import sun.security.krb5.EncryptionKey;
10044
413c0f8ca341 7048466: Move sun.misc.JavaxSecurityAuthKerberosAccess to sun.security.krb5 package
weijun
parents: 9499
diff changeset
    32
import sun.security.krb5.KerberosSecrets;
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    33
import sun.security.krb5.PrincipalName;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    34
import sun.security.krb5.RealmException;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    35
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    36
/**
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    37
 * This class encapsulates a keytab file.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    38
 * <p>
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    39
 * A Kerberos JAAS login module that obtains long term secret keys from a
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    40
 * keytab file should use this class. The login module will store
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    41
 * an instance of this class in the private credential set of a
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    42
 * {@link javax.security.auth.Subject Subject} during the commit phase of the
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    43
 * authentication process.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    44
 * <p>
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    45
 * If a {@code KeyTab} object is obtained from {@link #getUnboundInstance()}
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    46
 * or {@link #getUnboundInstance(java.io.File)}, it is unbound and thus can be
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    47
 * used by any service principal. Otherwise, if it's obtained from
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    48
 * {@link #getInstance(KerberosPrincipal)} or
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    49
 * {@link #getInstance(KerberosPrincipal, java.io.File)}, it is bound to the
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    50
 * specific service principal and can only be used by it.
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    51
 * <p>
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    52
 * Please note the constructors {@link #getInstance()} and
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    53
 * {@link #getInstance(java.io.File)} were created when there was no support
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    54
 * for unbound keytabs. These methods should not be used anymore. An object
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    55
 * created with either of these methods are considered to be bound to an
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    56
 * unknown principal, which means, its {@link #isBound()} returns true and
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    57
 * {@link #getPrincipal()} returns null.
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    58
 * <p>
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    59
 * It might be necessary for the application to be granted a
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    60
 * {@link javax.security.auth.PrivateCredentialPermission
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
    61
 * PrivateCredentialPermission} if it needs to access the {@code KeyTab}
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
    62
 * instance from a {@code Subject}. This permission is not needed when the
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    63
 * application depends on the default JGSS Kerberos mechanism to access the
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
    64
 * {@code KeyTab}. In that case, however, the application will need an appropriate
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    65
 * {@link javax.security.auth.kerberos.ServicePermission ServicePermission}.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    66
 * <p>
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    67
 * The keytab file format is described at
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    68
 * <a href="http://www.ioplex.com/utilities/keytab.txt">
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    69
 * http://www.ioplex.com/utilities/keytab.txt</a>.
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    70
 * <p>
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    71
 * @since 1.7
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    72
 */
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    73
public final class KeyTab {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    74
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    75
    /*
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    76
     * Impl notes:
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    77
     *
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    78
     * This class is only a name, a permanent link to the keytab source
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    79
     * (can be missing). Itself has no content. In order to read content,
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    80
     * take a snapshot and read from it.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    81
     *
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    82
     * The snapshot is of type sun.security.krb5.internal.ktab.KeyTab, which
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    83
     * contains the content of the keytab file when the snapshot is taken.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    84
     * Itself has no refresh function and mostly an immutable class (except
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    85
     * for the create/add/save methods only used by the ktab command).
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    86
     */
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    87
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    88
    // Source, null if using the default one. Note that the default name
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    89
    // is maintained in snapshot, this field is never "resolved".
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    90
    private final File file;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    91
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    92
    // Bound user: normally from the "principal" value in a JAAS krb5
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    93
    // login conf. Will be null if it's "*".
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    94
    private final KerberosPrincipal princ;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    95
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    96
    private final boolean bound;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
    97
10044
413c0f8ca341 7048466: Move sun.misc.JavaxSecurityAuthKerberosAccess to sun.security.krb5 package
weijun
parents: 9499
diff changeset
    98
    // Set up JavaxSecurityAuthKerberosAccess in KerberosSecrets
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    99
    static {
10044
413c0f8ca341 7048466: Move sun.misc.JavaxSecurityAuthKerberosAccess to sun.security.krb5 package
weijun
parents: 9499
diff changeset
   100
        KerberosSecrets.setJavaxSecurityAuthKerberosAccess(
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   101
                new JavaxSecurityAuthKerberosAccessImpl());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   102
    }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   103
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   104
    private KeyTab(KerberosPrincipal princ, File file, boolean bound) {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   105
        this.princ = princ;
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   106
        this.file = file;
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   107
        this.bound = bound;
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   108
    }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   109
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   110
    /**
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   111
     * Returns a {@code KeyTab} instance from a {@code File} object
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   112
     * that is bound to an unknown service principal.
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   113
     * <p>
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   114
     * The result of this method is never null. This method only associates
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   115
     * the returned {@code KeyTab} object with the file and does not read it.
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   116
     * <p>
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   117
     * Developers should call {@link #getInstance(KerberosPrincipal,File)}
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   118
     * when the bound service principal is known.
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   119
     * @param file the keytab {@code File} object, must not be null
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   120
     * @return the keytab instance
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   121
     * @throws NullPointerException if the {@code file} argument is null
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   122
     */
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   123
    public static KeyTab getInstance(File file) {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   124
        if (file == null) {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   125
            throw new NullPointerException("file must be non null");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   126
        }
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   127
        return new KeyTab(null, file, true);
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   128
    }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   129
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   130
    /**
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   131
     * Returns an unbound {@code KeyTab} instance from a {@code File}
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   132
     * object.
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   133
     * <p>
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   134
     * The result of this method is never null. This method only associates
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   135
     * the returned {@code KeyTab} object with the file and does not read it.
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   136
     * @param file the keytab {@code File} object, must not be null
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   137
     * @return the keytab instance
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   138
     * @throws NullPointerException if the file argument is null
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   139
     * @since 1.8
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   140
     */
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   141
    public static KeyTab getUnboundInstance(File file) {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   142
        if (file == null) {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   143
            throw new NullPointerException("file must be non null");
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   144
        }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   145
        return new KeyTab(null, file, false);
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   146
    }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   147
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   148
    /**
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   149
     * Returns a {@code KeyTab} instance from a {@code File} object
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   150
     * that is bound to the specified service principal.
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   151
     * <p>
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   152
     * The result of this method is never null. This method only associates
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   153
     * the returned {@code KeyTab} object with the file and does not read it.
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   154
     * @param princ the bound service principal, must not be null
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   155
     * @param file the keytab {@code File} object, must not be null
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   156
     * @return the keytab instance
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   157
     * @throws NullPointerException if either of the arguments is null
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   158
     * @since 1.8
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   159
     */
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   160
    public static KeyTab getInstance(KerberosPrincipal princ, File file) {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   161
        if (princ == null) {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   162
            throw new NullPointerException("princ must be non null");
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   163
        }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   164
        if (file == null) {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   165
            throw new NullPointerException("file must be non null");
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   166
        }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   167
        return new KeyTab(princ, file, true);
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   168
    }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   169
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   170
    /**
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   171
     * Returns the default {@code KeyTab} instance that is bound
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   172
     * to an unknown service principal.
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   173
     * <p>
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   174
     * The result of this method is never null. This method only associates
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   175
     * the returned {@code KeyTab} object with the default keytab file and
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   176
     * does not read it.
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   177
     * <p>
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   178
     * Developers should call {@link #getInstance(KerberosPrincipal)}
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   179
     * when the bound service principal is known.
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   180
     * @return the default keytab instance.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   181
     */
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   182
    public static KeyTab getInstance() {
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   183
        return new KeyTab(null, null, true);
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   184
    }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   185
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   186
    /**
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   187
     * Returns the default unbound {@code KeyTab} instance.
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   188
     * <p>
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   189
     * The result of this method is never null. This method only associates
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   190
     * the returned {@code KeyTab} object with the default keytab file and
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   191
     * does not read it.
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   192
     * @return the default keytab instance
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   193
     * @since 1.8
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   194
     */
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   195
    public static KeyTab getUnboundInstance() {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   196
        return new KeyTab(null, null, false);
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   197
    }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   198
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   199
    /**
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   200
     * Returns the default {@code KeyTab} instance that is bound
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   201
     * to the specified service principal.
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   202
     * <p>
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   203
     * The result of this method is never null. This method only associates
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   204
     * the returned {@code KeyTab} object with the default keytab file and
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   205
     * does not read it.
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   206
     * @param princ the bound service principal, must not be null
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   207
     * @return the default keytab instance
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   208
     * @throws NullPointerException if {@code princ} is null
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   209
     * @since 1.8
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   210
     */
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   211
    public static KeyTab getInstance(KerberosPrincipal princ) {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   212
        if (princ == null) {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   213
            throw new NullPointerException("princ must be non null");
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   214
        }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   215
        return new KeyTab(princ, null, true);
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   216
    }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   217
20844
3f51de06462f 8022931: Enhance Kerberos exceptions
weijun
parents: 18830
diff changeset
   218
    // Takes a snapshot of the keytab content. This method is called by
3f51de06462f 8022931: Enhance Kerberos exceptions
weijun
parents: 18830
diff changeset
   219
    // JavaxSecurityAuthKerberosAccessImpl so no more private
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   220
    sun.security.krb5.internal.ktab.KeyTab takeSnapshot() {
20844
3f51de06462f 8022931: Enhance Kerberos exceptions
weijun
parents: 18830
diff changeset
   221
        try {
3f51de06462f 8022931: Enhance Kerberos exceptions
weijun
parents: 18830
diff changeset
   222
            return sun.security.krb5.internal.ktab.KeyTab.getInstance(file);
3f51de06462f 8022931: Enhance Kerberos exceptions
weijun
parents: 18830
diff changeset
   223
        } catch (AccessControlException ace) {
3f51de06462f 8022931: Enhance Kerberos exceptions
weijun
parents: 18830
diff changeset
   224
            if (file != null) {
3f51de06462f 8022931: Enhance Kerberos exceptions
weijun
parents: 18830
diff changeset
   225
                // It's OK to show the name if caller specified it
3f51de06462f 8022931: Enhance Kerberos exceptions
weijun
parents: 18830
diff changeset
   226
                throw ace;
3f51de06462f 8022931: Enhance Kerberos exceptions
weijun
parents: 18830
diff changeset
   227
            } else {
3f51de06462f 8022931: Enhance Kerberos exceptions
weijun
parents: 18830
diff changeset
   228
                AccessControlException ace2 = new AccessControlException(
3f51de06462f 8022931: Enhance Kerberos exceptions
weijun
parents: 18830
diff changeset
   229
                        "Access to default keytab denied (modified exception)");
3f51de06462f 8022931: Enhance Kerberos exceptions
weijun
parents: 18830
diff changeset
   230
                ace2.setStackTrace(ace.getStackTrace());
3f51de06462f 8022931: Enhance Kerberos exceptions
weijun
parents: 18830
diff changeset
   231
                throw ace2;
3f51de06462f 8022931: Enhance Kerberos exceptions
weijun
parents: 18830
diff changeset
   232
            }
3f51de06462f 8022931: Enhance Kerberos exceptions
weijun
parents: 18830
diff changeset
   233
        }
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   234
    }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   235
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   236
    /**
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   237
     * Returns fresh keys for the given Kerberos principal.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   238
     * <p>
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   239
     * Implementation of this method should make sure the returned keys match
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   240
     * the latest content of the keytab file. The result is a newly created
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   241
     * copy that can be modified by the caller without modifying the keytab
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   242
     * object. The caller should {@link KerberosKey#destroy() destroy} the
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   243
     * result keys after they are used.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   244
     * <p>
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   245
     * Please note that the keytab file can be created after the
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   246
     * {@code KeyTab} object is instantiated and its content may change over
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   247
     * time. Therefore, an application should call this method only when it
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   248
     * needs to use the keys. Any previous result from an earlier invocation
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   249
     * could potentially be expired.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   250
     * <p>
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   251
     * If there is any error (say, I/O error or format error)
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   252
     * during the reading process of the keytab file, a saved result should be
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   253
     * returned. If there is no saved result (say, this is the first time this
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   254
     * method is called, or, all previous read attempts failed), an empty array
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   255
     * should be returned. This can make sure the result is not drastically
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   256
     * changed during the (probably slow) update of the keytab file.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   257
     * <p>
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   258
     * Each time this method is called and the reading of the file succeeds
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   259
     * with no exception (say, I/O error or file format error),
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   260
     * the result should be saved for {@code principal}. The implementation can
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   261
     * also save keys for other principals having keys in the same keytab object
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   262
     * if convenient.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   263
     * <p>
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   264
     * Any unsupported key read from the keytab is ignored and not included
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   265
     * in the result.
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   266
     * <p>
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   267
     * If this keytab is bound to a specific principal, calling this method on
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   268
     * another principal will return an empty array.
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   269
     *
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   270
     * @param principal the Kerberos principal, must not be null.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   271
     * @return the keys (never null, may be empty)
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   272
     * @throws NullPointerException if the {@code principal}
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   273
     * argument is null
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   274
     * @throws SecurityException if a security manager exists and the read
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   275
     * access to the keytab file is not permitted
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   276
     */
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   277
    public KerberosKey[] getKeys(KerberosPrincipal principal) {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   278
        try {
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   279
            if (princ != null && !principal.equals(princ)) {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   280
                return new KerberosKey[0];
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   281
            }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   282
            PrincipalName pn = new PrincipalName(principal.getName());
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   283
            EncryptionKey[] keys = takeSnapshot().readServiceKeys(pn);
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   284
            KerberosKey[] kks = new KerberosKey[keys.length];
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   285
            for (int i=0; i<kks.length; i++) {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   286
                Integer tmp = keys[i].getKeyVersionNumber();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   287
                kks[i] = new KerberosKey(
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   288
                        principal,
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   289
                        keys[i].getBytes(),
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   290
                        keys[i].getEType(),
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   291
                        tmp == null ? 0 : tmp.intValue());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   292
                keys[i].destroy();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   293
            }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   294
            return kks;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   295
        } catch (RealmException re) {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   296
            return new KerberosKey[0];
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   297
        }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   298
    }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   299
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   300
    EncryptionKey[] getEncryptionKeys(PrincipalName principal) {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   301
        return takeSnapshot().readServiceKeys(principal);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   302
    }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   303
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   304
    /**
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   305
     * Checks if the keytab file exists. Implementation of this method
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   306
     * should make sure that the result matches the latest status of the
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   307
     * keytab file.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   308
     * <p>
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   309
     * The caller can use the result to determine if it should fallback to
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   310
     * another mechanism to read the keys.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   311
     * @return true if the keytab file exists; false otherwise.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   312
     * @throws SecurityException if a security manager exists and the read
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   313
     * access to the keytab file is not permitted
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   314
     */
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   315
    public boolean exists() {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   316
        return !takeSnapshot().isMissing();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   317
    }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   318
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   319
    /**
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   320
     * Returns an informative textual representation of this {@code KeyTab}.
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   321
     *
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   322
     * @return an informative textual representation of this {@code KeyTab}.
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   323
     */
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   324
    public String toString() {
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   325
        String s = (file == null) ? "Default keytab" : file.toString();
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   326
        if (!bound) return s;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   327
        else if (princ == null) return s + " for someone";
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   328
        else return s + " for " + princ;
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   329
    }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   330
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   331
    /**
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   332
     * Returns a hash code for this {@code KeyTab}.
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   333
     *
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   334
     * @return a hash code for this {@code KeyTab}.
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   335
     */
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   336
    public int hashCode() {
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   337
        return Objects.hash(file, princ, bound);
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   338
    }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   339
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   340
    /**
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   341
     * Compares the specified object with this {@code KeyTab} for equality.
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   342
     * Returns true if the given object is also a
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 15649
diff changeset
   343
     * {@code KeyTab} and the two
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 15649
diff changeset
   344
     * {@code KeyTab} instances are equivalent.
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   345
     *
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   346
     * @param other the object to compare to
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   347
     * @return true if the specified object is equal to this {@code KeyTab}
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   348
     */
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   349
    public boolean equals(Object other) {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   350
        if (other == this)
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   351
            return true;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   352
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   353
        if (! (other instanceof KeyTab)) {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   354
            return false;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   355
        }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   356
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   357
        KeyTab otherKtab = (KeyTab) other;
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   358
        return Objects.equals(otherKtab.princ, princ) &&
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   359
                Objects.equals(otherKtab.file, file) &&
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   360
                bound == otherKtab.bound;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   361
    }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   362
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   363
    /**
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   364
     * Returns the service principal this {@code KeyTab} object
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   365
     * is bound to. Returns {@code null} if it's not bound.
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   366
     * <p>
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   367
     * Please note the deprecated constructors create a {@code KeyTab} object
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   368
     * bound for some unknown principal. In this case, this method also returns
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   369
     * null. User can call {@link #isBound()} to verify this case.
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   370
     * @return the service principal
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   371
     * @since 1.8
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   372
     */
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   373
    public KerberosPrincipal getPrincipal() {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   374
        return princ;
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   375
    }
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   376
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   377
    /**
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   378
     * Returns if the keytab is bound to a principal
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   379
     * @return if the keytab is bound to a principal
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   380
     * @since 1.8
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   381
     */
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   382
    public boolean isBound() {
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 10044
diff changeset
   383
        return bound;
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   384
    }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   385
}