001/* KerberosKey.java -- kerberos key
002   Copyright (C) 2006 Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package javax.security.auth.kerberos;
040
041import gnu.classpath.NotImplementedException;
042
043import java.io.Serializable;
044
045import javax.crypto.SecretKey;
046import javax.security.auth.DestroyFailedException;
047import 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 */
056public 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}