Path: blob/master/src/java.security.jgss/share/classes/javax/security/auth/kerberos/KerberosCredMessage.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 javax.security.auth.Destroyable;28import java.util.Arrays;29import java.util.Base64;30import java.util.Objects;3132/**33* This class encapsulates a Kerberos 5 KRB_CRED message which can be used to34* send Kerberos credentials from one principal to another.<p>35*36* A KRB_CRED message is defined in Section 5.8.1 of the Kerberos Protocol37* Specification (<a href=http://www.ietf.org/rfc/rfc4120.txt>RFC 4120</a>) as:38* <pre>39* KRB-CRED ::= [APPLICATION 22] SEQUENCE {40* pvno [0] INTEGER (5),41* msg-type [1] INTEGER (22),42* tickets [2] SEQUENCE OF Ticket,43* enc-part [3] EncryptedData -- EncKrbCredPart44* }45* </pre>46*47* @since 948*/49public final class KerberosCredMessage implements Destroyable {5051private final KerberosPrincipal sender;52private final KerberosPrincipal recipient;53private final byte[] message;5455private boolean destroyed = false;5657/**58* Constructs a {@code KerberosCredMessage} object.59* <p>60* The contents of the {@code message} argument are copied; subsequent61* modification of the byte array does not affect the newly created object.62*63* @param sender the sender of the message64* @param recipient the recipient of the message65* @param message the DER encoded KRB_CRED message66* @throws NullPointerException if any of sender, recipient67* or message is null68*/69public KerberosCredMessage(KerberosPrincipal sender,70KerberosPrincipal recipient,71byte[] message) {72this.sender = Objects.requireNonNull(sender);73this.recipient = Objects.requireNonNull(recipient);74this.message = Objects.requireNonNull(message).clone();75}7677/**78* Returns the DER encoded form of the KRB_CRED message.79*80* @return a newly allocated byte array that contains the encoded form81* @throws IllegalStateException if the object is destroyed82*/83public byte[] getEncoded() {84if (destroyed) {85throw new IllegalStateException("This object is no longer valid");86}87return message.clone();88}8990/**91* Returns the sender of this message.92*93* @return the sender94* @throws IllegalStateException if the object is destroyed95*/96public KerberosPrincipal getSender() {97if (destroyed) {98throw new IllegalStateException("This object is no longer valid");99}100return sender;101}102103/**104* Returns the recipient of this message.105*106* @return the recipient107* @throws IllegalStateException if the object is destroyed108*/109public KerberosPrincipal getRecipient() {110if (destroyed) {111throw new IllegalStateException("This object is no longer valid");112}113return recipient;114}115116/**117* Destroys this object by clearing out the message.118*/119@Override120public void destroy() {121if (!destroyed) {122Arrays.fill(message, (byte)0);123destroyed = true;124}125}126127@Override128public boolean isDestroyed() {129return destroyed;130}131132/**133* Returns an informative textual representation of this {@code KerberosCredMessage}.134*135* @return an informative textual representation of this {@code KerberosCredMessage}.136*/137@Override138public String toString() {139if (destroyed) {140return "Destroyed KerberosCredMessage";141} else {142return "KRB_CRED from " + sender + " to " + recipient + ":\n"143+ Base64.getUrlEncoder().encodeToString(message);144}145}146147/**148* Returns a hash code for this {@code KerberosCredMessage}.149*150* @return a hash code for this {@code KerberosCredMessage}.151*/152@Override153public int hashCode() {154if (isDestroyed()) {155return -1;156} else {157return Objects.hash(sender, recipient, Arrays.hashCode(message));158}159}160161/**162* Compares the specified object with this {@code KerberosCredMessage}163* for equality. Returns true if the given object is also a164* {@code KerberosCredMessage} and the two {@code KerberosCredMessage}165* instances are equivalent. More formally two {@code KerberosCredMessage}166* instances are equal if they have equal sender, recipient, and encoded167* KRB_CRED messages.168* A destroyed {@code KerberosCredMessage} object is only equal to itself.169*170* @param other the object to compare to171* @return true if the specified object is equal to this172* {@code KerberosCredMessage}, false otherwise.173*/174@Override175public boolean equals(Object other) {176if (other == this) {177return true;178}179180if (! (other instanceof KerberosCredMessage)) {181return false;182}183184KerberosCredMessage otherMessage = ((KerberosCredMessage) other);185if (isDestroyed() || otherMessage.isDestroyed()) {186return false;187}188189return Objects.equals(sender, otherMessage.sender)190&& Objects.equals(recipient, otherMessage.recipient)191&& Arrays.equals(message, otherMessage.message);192}193}194195196