001 /* KerberosKey.java -- kerberos key
002 Copyright (C) 2006 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package javax.security.auth.kerberos;
040
041 import gnu.classpath.NotImplementedException;
042
043 import java.io.Serializable;
044
045 import javax.crypto.SecretKey;
046 import javax.security.auth.DestroyFailedException;
047 import javax.security.auth.Destroyable;
048
049 /**
050 * This class represents a Kerberos key. See the Kerberos
051 * authentication RFC for more information:
052 * <a href="http://www.ietf.org/rfc/rfc1510.txt">RFC 1510</a>.
053 *
054 * @since 1.4
055 */
056 public class KerberosKey
057 implements Serializable, SecretKey, Destroyable
058 {
059 private static final long serialVersionUID = -4625402278148246993L;
060
061 private KerberosPrincipal principal;
062 private int versionNum;
063 private KeyImpl key;
064
065 /**
066 * Construct a new key with the indicated principal and key.
067 * @param principal the principal
068 * @param key the key's data
069 * @param type the key's type
070 * @param version the key's version number
071 */
072 public KerberosKey(KerberosPrincipal principal, byte[] key, int type,
073 int version)
074 {
075 this.principal = principal;
076 this.versionNum = version;
077 this.key = new KeyImpl(key, type);
078 }
079
080 /**
081 * Construct a new key with the indicated principal and a password.
082 * @param principal the principal
083 * @param passwd the password to use
084 * @param algo the algorithm; if null the "DES" algorithm is used
085 */
086 public KerberosKey(KerberosPrincipal principal, char[] passwd, String algo)
087 // Not implemented because KeyImpl really does nothing here.
088 throws NotImplementedException
089 {
090 this.principal = principal;
091 this.versionNum = 0; // FIXME: correct?
092 this.key = new KeyImpl(passwd, algo);
093 }
094
095 /**
096 * Return the name of the algorithm used to create this key.
097 */
098 public final String getAlgorithm()
099 {
100 checkDestroyed();
101 return key.algorithm;
102 }
103
104 /**
105 * Return the format of this key. This implementation always returns "RAW".
106 */
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 */
117 public final KerberosPrincipal getPrincipal()
118 {
119 checkDestroyed();
120 return principal;
121 }
122
123 /**
124 * Return the type of this key.
125 */
126 public final int getKeyType()
127 {
128 checkDestroyed();
129 return key.type;
130 }
131
132 /**
133 * Return the version number of this key.
134 */
135 public final int getVersionNumber()
136 {
137 checkDestroyed();
138 return versionNum;
139 }
140
141 /**
142 * Return the encoded form of this key.
143 */
144 public final byte[] getEncoded()
145 {
146 checkDestroyed();
147 return (byte[]) key.key.clone();
148 }
149
150 /**
151 * Destroy this key.
152 */
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 */
164 public boolean isDestroyed()
165 {
166 return key == null;
167 }
168
169 private void checkDestroyed()
170 {
171 if (key == null)
172 throw new IllegalStateException("key is destroyed");
173 }
174
175 public String toString()
176 {
177 // FIXME: random choice here.
178 return principal + ":" + versionNum;
179 }
180 }