Path: blob/master/src/java.security.jgss/share/classes/javax/security/auth/kerberos/EncryptionKey.java
41161 views
/*1* Copyright (c) 2014, 2015, 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 java.util.Objects;29import javax.crypto.SecretKey;30import javax.security.auth.DestroyFailedException;3132/**33* This class encapsulates an EncryptionKey used in Kerberos.<p>34*35* An EncryptionKey is defined in Section 4.2.9 of the Kerberos Protocol36* Specification (<a href=http://www.ietf.org/rfc/rfc4120.txt>RFC 4120</a>) as:37* <pre>38* EncryptionKey ::= SEQUENCE {39* keytype [0] Int32 -- actually encryption type --,40* keyvalue [1] OCTET STRING41* }42* </pre>43* The key material of an {@code EncryptionKey} is defined as the value44* of the {@code keyValue} above.45*46* @since 947*/48public final class EncryptionKey implements SecretKey {4950private static final long serialVersionUID = 9L;5152/**53* {@code KeyImpl} is serialized by writing out the ASN.1 encoded bytes54* of the encryption key.55*56* @serial57*/58private final KeyImpl key;5960private transient boolean destroyed = false;6162/**63* Constructs an {@code EncryptionKey} from the given bytes and64* the key type.65* <p>66* The contents of the byte array are copied; subsequent modification of67* the byte array does not affect the newly created key.68*69* @param keyBytes the key material for the key70* @param keyType the key type for the key as defined by the71* Kerberos protocol specification.72* @throws NullPointerException if keyBytes is null73*/74public EncryptionKey(byte[] keyBytes, int keyType) {75key = new KeyImpl(Objects.requireNonNull(keyBytes), keyType);76}7778/**79* Returns the key type for this key.80*81* @return the key type.82* @throws IllegalStateException if the key is destroyed83*/84public int getKeyType() {85// KeyImpl already checked if destroyed86return key.getKeyType();87}8889/*90* Methods from java.security.Key91*/9293/**94* Returns the standard algorithm name for this key. The algorithm names95* are the encryption type string defined on the IANA96* <a href="https://www.iana.org/assignments/kerberos-parameters/kerberos-parameters.xhtml#kerberos-parameters-1">Kerberos Encryption Type Numbers</a>97* page.98* <p>99* This method can return the following value not defined on the IANA page:100* <ol>101* <li>none: for etype equal to 0</li>102* <li>unknown: for etype greater than 0 but unsupported by103* the implementation</li>104* <li>private: for etype smaller than 0</li>105* </ol>106*107* @return the name of the algorithm associated with this key.108* @throws IllegalStateException if the key is destroyed109*/110@Override111public String getAlgorithm() {112// KeyImpl already checked if destroyed113return key.getAlgorithm();114}115116/**117* Returns the name of the encoding format for this key.118*119* @return the String "RAW"120* @throws IllegalStateException if the key is destroyed121*/122@Override123public String getFormat() {124// KeyImpl already checked if destroyed125return key.getFormat();126}127128/**129* Returns the key material of this key.130*131* @return a newly allocated byte array that contains the key material132* @throws IllegalStateException if the key is destroyed133*/134@Override135public byte[] getEncoded() {136// KeyImpl already checked if destroyed137return key.getEncoded();138}139140/**141* Destroys this key by clearing out the key material of this key.142*143* @throws DestroyFailedException if some error occurs while destorying144* this key.145*/146@Override147public void destroy() throws DestroyFailedException {148if (!destroyed) {149key.destroy();150destroyed = true;151}152}153154155@Override156public boolean isDestroyed() {157return destroyed;158}159160/**161* Returns an informative textual representation of this {@code EncryptionKey}.162*163* @return an informative textual representation of this {@code EncryptionKey}.164*/165@Override166public String toString() {167if (destroyed) {168return "Destroyed EncryptionKey";169}170return "key " + key.toString();171}172173/**174* Returns a hash code for this {@code EncryptionKey}.175*176* @return a hash code for this {@code EncryptionKey}.177*/178@Override179public int hashCode() {180int result = 17;181if (isDestroyed()) {182return result;183}184result = 37 * result + Arrays.hashCode(getEncoded());185return 37 * result + getKeyType();186}187188/**189* Compares the specified object with this key for equality.190* Returns true if the given object is also an191* {@code EncryptionKey} and the two192* {@code EncryptionKey} instances are equivalent. More formally two193* {@code EncryptionKey} instances are equal if they have equal key types194* and key material.195* A destroyed {@code EncryptionKey} object is only equal to itself.196*197* @param other the object to compare to198* @return true if the specified object is equal to this199* {@code EncryptionKey}, false otherwise.200*/201@Override202public boolean equals(Object other) {203204if (other == this)205return true;206207if (! (other instanceof EncryptionKey)) {208return false;209}210211EncryptionKey otherKey = ((EncryptionKey) other);212if (isDestroyed() || otherKey.isDestroyed()) {213return false;214}215216return getKeyType() == otherKey.getKeyType()217&& Arrays.equals(getEncoded(), otherKey.getEncoded());218}219}220221222