1 /* KerberosKey.java -- kerberos key 2 Copyright (C) 2006 Free Software Foundation, Inc. 3 4 This file is part of GNU Classpath. 5 6 GNU Classpath is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2, or (at your option) 9 any later version. 10 11 GNU Classpath is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GNU Classpath; see the file COPYING. If not, write to the 18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 02110-1301 USA. 20 21 Linking this library statically or dynamically with other modules is 22 making a combined work based on this library. Thus, the terms and 23 conditions of the GNU General Public License cover the whole 24 combination. 25 26 As a special exception, the copyright holders of this library give you 27 permission to link this library with independent modules to produce an 28 executable, regardless of the license terms of these independent 29 modules, and to copy and distribute the resulting executable under 30 terms of your choice, provided that you also meet, for each linked 31 independent module, the terms and conditions of the license of that 32 module. An independent module is a module which is not derived from 33 or based on this library. If you modify this library, you may extend 34 this exception to your version of the library, but you are not 35 obligated to do so. If you do not wish to do so, delete this 36 exception statement from your version. */ 37 38 39 package javax.security.auth.kerberos; 40 41 import gnu.classpath.NotImplementedException; 42 43 import java.io.Serializable; 44 45 import javax.crypto.SecretKey; 46 import javax.security.auth.DestroyFailedException; 47 import javax.security.auth.Destroyable; 48 49 /** 50 * This class represents a Kerberos key. See the Kerberos 51 * authentication RFC for more information: 52 * <a href="http://www.ietf.org/rfc/rfc1510.txt">RFC 1510</a>. 53 * 54 * @since 1.4 55 */ 56 public class KerberosKey 57 implements Serializable, SecretKey, Destroyable 58 { 59 private static final long serialVersionUID = -4625402278148246993L; 60 61 private KerberosPrincipal principal; 62 private int versionNum; 63 private KeyImpl key; 64 65 /** 66 * Construct a new key with the indicated principal and key. 67 * @param principal the principal 68 * @param key the key's data 69 * @param type the key's type 70 * @param version the key's version number 71 */ KerberosKey(KerberosPrincipal principal, byte[] key, int type, int version)72 public KerberosKey(KerberosPrincipal principal, byte[] key, int type, 73 int version) 74 { 75 this.principal = principal; 76 this.versionNum = version; 77 this.key = new KeyImpl(key, type); 78 } 79 80 /** 81 * Construct a new key with the indicated principal and a password. 82 * @param principal the principal 83 * @param passwd the password to use 84 * @param algo the algorithm; if null the "DES" algorithm is used 85 */ KerberosKey(KerberosPrincipal principal, char[] passwd, String algo)86 public KerberosKey(KerberosPrincipal principal, char[] passwd, String algo) 87 // Not implemented because KeyImpl really does nothing here. 88 throws NotImplementedException 89 { 90 this.principal = principal; 91 this.versionNum = 0; // FIXME: correct? 92 this.key = new KeyImpl(passwd, algo); 93 } 94 95 /** 96 * Return the name of the algorithm used to create this key. 97 */ getAlgorithm()98 public final String getAlgorithm() 99 { 100 checkDestroyed(); 101 return key.algorithm; 102 } 103 104 /** 105 * Return the format of this key. This implementation always returns "RAW". 106 */ getFormat()107 public final String getFormat() 108 { 109 checkDestroyed(); 110 // Silly, but specified. 111 return "RAW"; 112 } 113 114 /** 115 * Return the principal associated with this key. 116 */ getPrincipal()117 public final KerberosPrincipal getPrincipal() 118 { 119 checkDestroyed(); 120 return principal; 121 } 122 123 /** 124 * Return the type of this key. 125 */ getKeyType()126 public final int getKeyType() 127 { 128 checkDestroyed(); 129 return key.type; 130 } 131 132 /** 133 * Return the version number of this key. 134 */ getVersionNumber()135 public final int getVersionNumber() 136 { 137 checkDestroyed(); 138 return versionNum; 139 } 140 141 /** 142 * Return the encoded form of this key. 143 */ getEncoded()144 public final byte[] getEncoded() 145 { 146 checkDestroyed(); 147 return (byte[]) key.key.clone(); 148 } 149 150 /** 151 * Destroy this key. 152 */ destroy()153 public void destroy() throws DestroyFailedException 154 { 155 if (key == null) 156 throw new DestroyFailedException("already destroyed"); 157 key = null; 158 } 159 160 /** 161 * Return true if this key has been destroyed. After this has been 162 * called, other methods on this object will throw IllegalStateException. 163 */ isDestroyed()164 public boolean isDestroyed() 165 { 166 return key == null; 167 } 168 checkDestroyed()169 private void checkDestroyed() 170 { 171 if (key == null) 172 throw new IllegalStateException("key is destroyed"); 173 } 174 toString()175 public String toString() 176 { 177 // FIXME: random choice here. 178 return principal + ":" + versionNum; 179 } 180 } 181