Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/security/auth/kerberos/KerberosKey.java
38918 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.security.auth.kerberos;2627import java.util.Arrays;28import javax.crypto.SecretKey;29import javax.security.auth.Destroyable;30import javax.security.auth.DestroyFailedException;3132/**33* This class encapsulates a long term secret key for a Kerberos34* principal.<p>35*36* All Kerberos JAAS login modules that obtain a principal's password and37* generate the secret key from it should use this class.38* Sometimes, such as when authenticating a server in39* the absence of user-to-user authentication, the login module will store40* an instance of this class in the private credential set of a41* {@link javax.security.auth.Subject Subject} during the commit phase of the42* authentication process.<p>43*44* A Kerberos service using a keytab to read secret keys should use45* the {@link KeyTab} class, where latest keys can be read when needed.<p>46*47* It might be necessary for the application to be granted a48* {@link javax.security.auth.PrivateCredentialPermission49* PrivateCredentialPermission} if it needs to access the KerberosKey50* instance from a Subject. This permission is not needed when the51* application depends on the default JGSS Kerberos mechanism to access the52* KerberosKey. In that case, however, the application will need an53* appropriate54* {@link javax.security.auth.kerberos.ServicePermission ServicePermission}.55*56* @author Mayank Upadhyay57* @since 1.458*/59public class KerberosKey implements SecretKey, Destroyable {6061private static final long serialVersionUID = -4625402278148246993L;6263/**64* The principal that this secret key belongs to.65*66* @serial67*/68private KerberosPrincipal principal;6970/**71* the version number of this secret key72*73* @serial74*/75private int versionNum;7677/**78* {@code KeyImpl} is serialized by writing out the ASN1 Encoded bytes79* of the encryption key.80* The ASN1 encoding is defined in RFC4120 and as follows:81* <pre>82* EncryptionKey ::= SEQUENCE {83* keytype [0] Int32 -- actually encryption type --,84* keyvalue [1] OCTET STRING85* }86* </pre>87*88* @serial89*/9091private KeyImpl key;92private transient boolean destroyed = false;9394/**95* Constructs a KerberosKey from the given bytes when the key type and96* key version number are known. This can be used when reading the secret97* key information from a Kerberos "keytab".98*99* @param principal the principal that this secret key belongs to100* @param keyBytes the raw bytes for the secret key101* @param keyType the key type for the secret key as defined by the102* Kerberos protocol specification.103* @param versionNum the version number of this secret key104*/105public KerberosKey(KerberosPrincipal principal,106byte[] keyBytes,107int keyType,108int versionNum) {109this.principal = principal;110this.versionNum = versionNum;111key = new KeyImpl(keyBytes, keyType);112}113114/**115* Constructs a KerberosKey from a principal's password.116*117* @param principal the principal that this password belongs to118* @param password the password that should be used to compute the key119* @param algorithm the name for the algorithm that this key will be120* used for. This parameter may be null in which case the default121* algorithm "DES" will be assumed.122* @throws IllegalArgumentException if the name of the123* algorithm passed is unsupported.124*/125public KerberosKey(KerberosPrincipal principal,126char[] password,127String algorithm) {128129this.principal = principal;130// Pass principal in for salt131key = new KeyImpl(principal, password, algorithm);132}133134/**135* Returns the principal that this key belongs to.136*137* @return the principal this key belongs to.138*/139public final KerberosPrincipal getPrincipal() {140if (destroyed)141throw new IllegalStateException("This key is no longer valid");142return principal;143}144145/**146* Returns the key version number.147*148* @return the key version number.149*/150public final int getVersionNumber() {151if (destroyed)152throw new IllegalStateException("This key is no longer valid");153return versionNum;154}155156/**157* Returns the key type for this long-term key.158*159* @return the key type.160*/161public final int getKeyType() {162if (destroyed)163throw new IllegalStateException("This key is no longer valid");164return key.getKeyType();165}166167/*168* Methods from java.security.Key169*/170171/**172* Returns the standard algorithm name for this key. For173* example, "DES" would indicate that this key is a DES key.174* See Appendix A in the <a href=175* "../../../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">176* Java Cryptography Architecture API Specification & Reference177* </a>178* for information about standard algorithm names.179*180* @return the name of the algorithm associated with this key.181*/182public final String getAlgorithm() {183if (destroyed)184throw new IllegalStateException("This key is no longer valid");185return key.getAlgorithm();186}187188/**189* Returns the name of the encoding format for this secret key.190*191* @return the String "RAW"192*/193public final String getFormat() {194if (destroyed)195throw new IllegalStateException("This key is no longer valid");196return key.getFormat();197}198199/**200* Returns the key material of this secret key.201*202* @return the key material203*/204public final byte[] getEncoded() {205if (destroyed)206throw new IllegalStateException("This key is no longer valid");207return key.getEncoded();208}209210/**211* Destroys this key. A call to any of its other methods after this212* will cause an IllegalStateException to be thrown.213*214* @throws DestroyFailedException if some error occurs while destorying215* this key.216*/217public void destroy() throws DestroyFailedException {218if (!destroyed) {219key.destroy();220principal = null;221destroyed = true;222}223}224225226/** Determines if this key has been destroyed.*/227public boolean isDestroyed() {228return destroyed;229}230231public String toString() {232if (destroyed) {233return "Destroyed Principal";234}235return "Kerberos Principal " + principal.toString() +236"Key Version " + versionNum +237"key " + key.toString();238}239240/**241* Returns a hashcode for this KerberosKey.242*243* @return a hashCode() for the {@code KerberosKey}244* @since 1.6245*/246public int hashCode() {247int result = 17;248if (isDestroyed()) {249return result;250}251result = 37 * result + Arrays.hashCode(getEncoded());252result = 37 * result + getKeyType();253if (principal != null) {254result = 37 * result + principal.hashCode();255}256return result * 37 + versionNum;257}258259/**260* Compares the specified Object with this KerberosKey for equality.261* Returns true if the given object is also a262* {@code KerberosKey} and the two263* {@code KerberosKey} instances are equivalent.264*265* @param other the Object to compare to266* @return true if the specified object is equal to this KerberosKey,267* false otherwise. NOTE: Returns false if either of the KerberosKey268* objects has been destroyed.269* @since 1.6270*/271public boolean equals(Object other) {272273if (other == this)274return true;275276if (! (other instanceof KerberosKey)) {277return false;278}279280KerberosKey otherKey = ((KerberosKey) other);281if (isDestroyed() || otherKey.isDestroyed()) {282return false;283}284285if (versionNum != otherKey.getVersionNumber() ||286getKeyType() != otherKey.getKeyType() ||287!Arrays.equals(getEncoded(), otherKey.getEncoded())) {288return false;289}290291if (principal == null) {292if (otherKey.getPrincipal() != null) {293return false;294}295} else {296if (!principal.equals(otherKey.getPrincipal())) {297return false;298}299}300301return true;302}303}304305306