1. /*
  2. * @(#)KerberosKey.java 1.13 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.security.auth.kerberos;
  8. import javax.crypto.SecretKey;
  9. import javax.security.auth.Destroyable;
  10. import javax.security.auth.DestroyFailedException;
  11. /**
  12. * This class encapsulates a long term secret key for a Kerberos
  13. * principal.<p>
  14. *
  15. * All Kerberos JAAS login modules that obtain a principal's password and
  16. * generate the secret key from it should use this class. Where available,
  17. * the login module might even read this secret key directly from a
  18. * Kerberos "keytab". Sometimes, such as when authenticating a server in
  19. * the absence of user-to-user authentication, the login module will store
  20. * an instance of this class in the private credential set of a
  21. * {@link javax.security.auth.Subject Subject} during the commit phase of the
  22. * authentication process.<p>
  23. *
  24. * It might be necessary for the application to be granted a
  25. * {@link javax.security.auth.PrivateCredentialPermission
  26. * PrivateCredentialPermission} if it needs to access the KerberosKey
  27. * instance from a Subject. This permission is not needed when the
  28. * application depends on the default JGSS Kerberos mechanism to access the
  29. * KerberosKey. In that case, however, the application will need an
  30. * appropriate
  31. * {@link javax.security.auth.kerberos.ServicePermission ServicePermission}.
  32. *
  33. * @author Mayank Upadhyay
  34. * @version 1.13, 01/23/03
  35. * @since 1.4
  36. */
  37. public class KerberosKey implements SecretKey, Destroyable {
  38. /**
  39. * The principal that this secret key belongs to.
  40. *
  41. * @serial
  42. */
  43. private KerberosPrincipal principal;
  44. /**
  45. * the version number of this secret key
  46. *
  47. * @serial
  48. */
  49. private int versionNum;
  50. /**
  51. * <code>KeyImpl</code> is serialized by writing out the ASN1 Encoded bytes
  52. * of the encryption key. The ASN1 encoding is defined in
  53. * RFC1510 and as follows:
  54. * <pre>
  55. * EncryptionKey ::= SEQUENCE {
  56. * keytype[0] INTEGER,
  57. * keyvalue[1] OCTET STRING
  58. * }
  59. * </pre>
  60. *
  61. * @serial
  62. */
  63. private KeyImpl key;
  64. private transient boolean destroyed = false;
  65. /**
  66. * Constructs a KerberosKey from the given bytes when the key type and
  67. * key version number are known. This can used when reading the secret
  68. * key information from a Kerberos "keytab".
  69. *
  70. * @param principal the principal that this secret key belongs to
  71. * @param keyBytes the raw bytes for the secret key
  72. * @param keyType the key type for the secret key as defined by the
  73. * Kerberos protocol specification.
  74. * @param versionNum the version number of this secret key
  75. */
  76. public KerberosKey(KerberosPrincipal principal,
  77. byte[] keyBytes,
  78. int keyType,
  79. int versionNum) {
  80. this.principal = principal;
  81. this.versionNum = versionNum;
  82. key = new KeyImpl(keyBytes, keyType);
  83. }
  84. /**
  85. * Constructs a KerberosKey from a principal's password.
  86. *
  87. * @param principal the principal that this password belongs to
  88. * @param password the password that should be used to compute the key
  89. * @param algorithm the name for the algorithm that this key wil be
  90. * used for. This parameter may be null in which case "DES" will be
  91. * assumed.
  92. */
  93. public KerberosKey(KerberosPrincipal principal,
  94. char[] password,
  95. String algorithm) {
  96. this.principal = principal;
  97. // Pass principal in for salt
  98. key = new KeyImpl(principal, password, algorithm);
  99. }
  100. /**
  101. * Returns the principal that this key belongs to.
  102. *
  103. * @return the principal this key belongs to.
  104. */
  105. public final KerberosPrincipal getPrincipal() {
  106. if (destroyed)
  107. throw new IllegalStateException("This key is no longer valid");
  108. return principal;
  109. }
  110. /**
  111. * Returns the key version number.
  112. *
  113. * @return the key version number.
  114. */
  115. public final int getVersionNumber() {
  116. if (destroyed)
  117. throw new IllegalStateException("This key is no longer valid");
  118. return versionNum;
  119. }
  120. /**
  121. * Returns the key type for this long-term key.
  122. *
  123. * @return the key type.
  124. */
  125. public final int getKeyType() {
  126. if (destroyed)
  127. throw new IllegalStateException("This key is no longer valid");
  128. return key.getKeyType();
  129. }
  130. /*
  131. * Methods from java.security.Key
  132. */
  133. /**
  134. * Returns the standard algorithm name for this key. For
  135. * example, "DES" would indicate that this key is a DES key.
  136. * See Appendix A in the <a href=
  137. * "../../../../../guide/security/CryptoSpec.html#AppA">
  138. * Java Cryptography Architecture API Specification & Reference
  139. * </a>
  140. * for information about standard algorithm names.
  141. *
  142. * @return the name of the algorithm associated with this key.
  143. */
  144. public final String getAlgorithm() {
  145. if (destroyed)
  146. throw new IllegalStateException("This key is no longer valid");
  147. return key.getAlgorithm();
  148. }
  149. /**
  150. * Returns the name of the encoding format for this secret key.
  151. *
  152. * @return the String "RAW"
  153. */
  154. public final String getFormat() {
  155. if (destroyed)
  156. throw new IllegalStateException("This key is no longer valid");
  157. return key.getFormat();
  158. }
  159. /**
  160. * Returns the key material of this secret key.
  161. *
  162. * @return the key material
  163. */
  164. public final byte[] getEncoded() {
  165. if (destroyed)
  166. throw new IllegalStateException("This key is no longer valid");
  167. return key.getEncoded();
  168. }
  169. /**
  170. * Destroys this key. A call to any of its other methods after this
  171. * will cause an IllegalStateException to be thrown.
  172. *
  173. * @throws DestroyFailedException if some error occurs while destorying
  174. * this key.
  175. */
  176. public void destroy() throws DestroyFailedException {
  177. if (!destroyed) {
  178. key.destroy();
  179. principal = null;
  180. destroyed = true;
  181. }
  182. }
  183. /** Determines if this key has been destroyed.*/
  184. public boolean isDestroyed() {
  185. return destroyed;
  186. }
  187. public String toString() {
  188. return "Kerberos Principal " + principal.toString() +
  189. "Key Version " + versionNum +
  190. "key " + key.toString();
  191. }
  192. }