jdk/src/java.security.jgss/share/classes/javax/security/auth/kerberos/KerberosKey.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:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 9499
diff changeset
     2
 * Copyright (c) 2000, 2013, 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.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.security.auth.DestroyFailedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This class encapsulates a long term secret key for a Kerberos
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * principal.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
    36
 * A {@code KerberosKey} object includes an EncryptionKey, a
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
    37
 * {@link KerberosPrincipal} as its owner, and the version number
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
    38
 * of the key.<p>
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
    39
 *
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
    40
 * An EncryptionKey is defined in Section 4.2.9 of the Kerberos Protocol
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
    41
 * Specification (<a href=http://www.ietf.org/rfc/rfc4120.txt>RFC 4120</a>) as:
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
    42
 * <pre>
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
    43
 *     EncryptionKey   ::= SEQUENCE {
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
    44
 *             keytype         [0] Int32 -- actually encryption type --,
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
    45
 *             keyvalue        [1] OCTET STRING
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
    46
 *     }
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
    47
 * </pre>
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
    48
 * The key material of a {@code KerberosKey} is defined as the value
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
    49
 * of the {@code keyValue} above.<p>
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
    50
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * All Kerberos JAAS login modules that obtain a principal's password and
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
    52
 * generate the secret key from it should use this class.
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
    53
 * Sometimes, such as when authenticating a server in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * the absence of user-to-user authentication, the login module will store
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * an instance of this class in the private credential set of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * {@link javax.security.auth.Subject Subject} during the commit phase of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * authentication process.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
    59
 * A Kerberos service using a keytab to read secret keys should use
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
    60
 * the {@link KeyTab} class, where latest keys can be read when needed.<p>
f3115698a012 6894072: always refresh keytab
weijun
parents: 5506
diff changeset
    61
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * It might be necessary for the application to be granted a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * {@link javax.security.auth.PrivateCredentialPermission
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
    64
 * PrivateCredentialPermission} if it needs to access the {@code KerberosKey}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * instance from a Subject. This permission is not needed when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * 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
    67
 * {@code KerberosKey}. In that case, however, the application will need an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * appropriate
23732
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    69
 * {@link javax.security.auth.kerberos.ServicePermission ServicePermission}.<p>
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    70
 *
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    71
 * When creating a {@code KerberosKey} using the
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    72
 * {@link #KerberosKey(KerberosPrincipal, char[], String)} constructor,
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    73
 * an implementation may accept non-IANA algorithm names (For example,
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    74
 * "ArcFourMac" for "rc4-hmac"), but the {@link #getAlgorithm} method
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    75
 * must always return the IANA algorithm name.<p>
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    76
 *
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    77
 * @implNote Old algorithm names used before JDK 9 are supported in the
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    78
 * {@link #KerberosKey(KerberosPrincipal, char[], String)} constructor in this
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    79
 * implementation for compatibility reasons, which are "DES" (and null) for
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    80
 * "des-cbc-md5", "DESede" for "des3-cbc-sha1-kd", "ArcFourHmac" for "rc4-hmac",
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    81
 * "AES128" for "aes128-cts-hmac-sha1-96", and "AES256" for
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    82
 * "aes256-cts-hmac-sha1-96".
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @author Mayank Upadhyay
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 */
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
    87
public class KerberosKey implements SecretKey {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static final long serialVersionUID = -4625402278148246993L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * The principal that this secret key belongs to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private KerberosPrincipal principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * the version number of this secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
23732
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   103
    private final int versionNum;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
   /**
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   106
    * {@code KeyImpl} is serialized by writing out the ASN.1 encoded bytes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    * of the encryption key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private KeyImpl key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private transient boolean destroyed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   116
     * Constructs a {@code KerberosKey} from the given bytes when the key type
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   117
     * and key version number are known. This can be used when reading the
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   118
     * secret key information from a Kerberos "keytab".
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param principal the principal that this secret key belongs to
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   121
     * @param keyBytes the key material for the secret key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param keyType the key type for the secret key as defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Kerberos protocol specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param versionNum the version number of this secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public KerberosKey(KerberosPrincipal principal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                       byte[] keyBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                       int keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                       int versionNum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this.principal = principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this.versionNum = versionNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        key = new KeyImpl(keyBytes, keyType);
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 KerberosKey} from a principal's password using the
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   137
     * specified algorithm name. The algorithm name (case insensitive) should
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   138
     * be provided as the encryption type string defined on the IANA
23732
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   139
     * <a href="https://www.iana.org/assignments/kerberos-parameters/kerberos-parameters.xhtml#kerberos-parameters-1">Kerberos Encryption Type Numbers</a>
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   140
     * page. The version number of the key generated will be 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param principal the principal that this password belongs to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param password the password that should be used to compute the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param algorithm the name for the algorithm that this key will be
23732
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   145
     * used for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @throws IllegalArgumentException if the name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * algorithm passed is unsupported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public KerberosKey(KerberosPrincipal principal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                       char[] password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                       String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        this.principal = principal;
23732
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   154
        this.versionNum = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // Pass principal in for salt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        key = new KeyImpl(principal, password, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Returns the principal that this key belongs to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return the principal this key belongs to.
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   163
     * @throws IllegalStateException if the key is destroyed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public final KerberosPrincipal getPrincipal() {
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   166
        if (destroyed) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            throw new IllegalStateException("This key is no longer valid");
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   168
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Returns the key version number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @return the key version number.
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   176
     * @throws IllegalStateException if the key is destroyed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public final int getVersionNumber() {
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   179
        if (destroyed) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new IllegalStateException("This key is no longer valid");
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   181
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return versionNum;
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
     * Returns the key type for this long-term key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @return the key type.
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   189
     * @throws IllegalStateException if the key is destroyed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public final int getKeyType() {
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   192
        // KeyImpl already checked if destroyed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return key.getKeyType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Methods from java.security.Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
23732
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   201
     * Returns the standard algorithm name for this key. The algorithm names
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   202
     * are the encryption type string defined on the IANA
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   203
     * <a href="https://www.iana.org/assignments/kerberos-parameters/kerberos-parameters.xhtml#kerberos-parameters-1">Kerberos Encryption Type Numbers</a>
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   204
     * page.
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   205
     * <p>
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   206
     * This method can return the following value not defined on the IANA page:
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   207
     * <ol>
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   208
     *     <li>none: for etype equal to 0</li>
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   209
     *     <li>unknown: for etype greater than 0 but unsupported by
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   210
     *         the implementation</li>
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   211
     *     <li>private: for etype smaller than 0</li>
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   212
     * </ol>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @return the name of the algorithm associated with this key.
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   215
     * @throws IllegalStateException if the key is destroyed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public final String getAlgorithm() {
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   218
        // KeyImpl already checked if destroyed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return key.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Returns the name of the encoding format for this secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @return the String "RAW"
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   226
     * @throws IllegalStateException if the key is destroyed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public final String getFormat() {
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   229
        // KeyImpl already checked if destroyed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return key.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Returns the key material of this secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @return the key material
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   237
     * @throws IllegalStateException if the key is destroyed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public final byte[] getEncoded() {
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   240
        // KeyImpl already checked if destroyed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   245
     * Destroys this key by clearing out the key material of this secret key.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @throws DestroyFailedException if some error occurs while destorying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * this key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public void destroy() throws DestroyFailedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (!destroyed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            key.destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            principal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            destroyed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /** Determines if this key has been destroyed.*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public boolean isDestroyed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return destroyed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   264
    /**
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   265
     * Returns an informative textual representation of this {@code KerberosKey}.
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   266
     *
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   267
     * @return an informative textual representation of this {@code KerberosKey}.
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   268
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (destroyed) {
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   271
            return "Destroyed KerberosKey";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   273
        return "Kerberos Principal " + principal +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                "Key Version " + versionNum +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                "key "  + key.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   279
     * Returns a hash code for this {@code KerberosKey}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   281
     * @return a hash code for this {@code KerberosKey}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        int result = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (isDestroyed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        result = 37 * result + Arrays.hashCode(getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        result = 37 * result + getKeyType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (principal != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            result = 37 * result + principal.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return result * 37 + versionNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   298
     * Compares the specified object with this {@code KerberosKey} for
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   299
     * equality. Returns true if the given object is also a
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 9499
diff changeset
   300
     * {@code KerberosKey} and the two
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 9499
diff changeset
   301
     * {@code KerberosKey} instances are equivalent.
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   302
     * A destroyed {@code KerberosKey} object is only equal to itself.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
27771
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   304
     * @param other the object to compare to
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   305
     * @return true if the specified object is equal to this {@code KerberosKey},
360714d431ab 8061253: Spec cleanup for some security-related classes
weijun
parents: 25859
diff changeset
   306
     * false otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   311
        if (other == this) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            return true;
25661
929c829a8400 8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents: 23732
diff changeset
   313
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (! (other instanceof KerberosKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        KerberosKey otherKey = ((KerberosKey) other);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (isDestroyed() || otherKey.isDestroyed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (versionNum != otherKey.getVersionNumber() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                getKeyType() != otherKey.getKeyType() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                !Arrays.equals(getEncoded(), otherKey.getEncoded())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if (principal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            if (otherKey.getPrincipal() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (!principal.equals(otherKey.getPrincipal())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
}