Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / security / auth / kerberos / KerberosKey.java
blob3372a162f93c82510156225bed1e7cd29a7bb220
1 /* KerberosKey.java -- kerberos key
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
39 package javax.security.auth.kerberos;
41 import gnu.classpath.NotImplementedException;
43 import java.io.Serializable;
45 import javax.crypto.SecretKey;
46 import javax.security.auth.DestroyFailedException;
47 import javax.security.auth.Destroyable;
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>.
54 * @since 1.4
56 public class KerberosKey
57 implements Serializable, SecretKey, Destroyable
59 private static final long serialVersionUID = -4625402278148246993L;
61 private KerberosPrincipal principal;
62 private int versionNum;
63 private KeyImpl key;
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
72 public KerberosKey(KerberosPrincipal principal, byte[] key, int type,
73 int version)
75 this.principal = principal;
76 this.versionNum = version;
77 this.key = new KeyImpl(key, type);
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
86 public KerberosKey(KerberosPrincipal principal, char[] passwd, String algo)
87 // Not implemented because KeyImpl really does nothing here.
88 throws NotImplementedException
90 this.principal = principal;
91 this.versionNum = 0; // FIXME: correct?
92 this.key = new KeyImpl(passwd, algo);
95 /**
96 * Return the name of the algorithm used to create this key.
98 public final String getAlgorithm()
100 checkDestroyed();
101 return key.algorithm;
105 * Return the format of this key. This implementation always returns "RAW".
107 public final String getFormat()
109 checkDestroyed();
110 // Silly, but specified.
111 return "RAW";
115 * Return the principal associated with this key.
117 public final KerberosPrincipal getPrincipal()
119 checkDestroyed();
120 return principal;
124 * Return the type of this key.
126 public final int getKeyType()
128 checkDestroyed();
129 return key.type;
133 * Return the version number of this key.
135 public final int getVersionNumber()
137 checkDestroyed();
138 return versionNum;
142 * Return the encoded form of this key.
144 public final byte[] getEncoded()
146 checkDestroyed();
147 return (byte[]) key.key.clone();
151 * Destroy this key.
153 public void destroy() throws DestroyFailedException
155 if (key == null)
156 throw new DestroyFailedException("already destroyed");
157 key = null;
161 * Return true if this key has been destroyed. After this has been
162 * called, other methods on this object will throw IllegalStateException.
164 public boolean isDestroyed()
166 return key == null;
169 private void checkDestroyed()
171 if (key == null)
172 throw new IllegalStateException("key is destroyed");
175 public String toString()
177 // FIXME: random choice here.
178 return principal + ":" + versionNum;