1. /*
  2. * @(#)KerberosKey.java 1.17 04/01/13
  3. *
  4. * Copyright 2004 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.17, 01/13/04
  35. * @since 1.4
  36. */
  37. public class KerberosKey implements SecretKey, Destroyable {
  38. private static final long serialVersionUID = -4625402278148246993L;
  39. /**
  40. * The principal that this secret key belongs to.
  41. *
  42. * @serial
  43. */
  44. private KerberosPrincipal principal;
  45. /**
  46. * the version number of this secret key
  47. *
  48. * @serial
  49. */
  50. private int versionNum;
  51. /**
  52. * <code>KeyImpl</code> is serialized by writing out the ASN1 Encoded bytes
  53. * of the encryption key. The ASN1 encoding is defined in
  54. * RFC1510 and as follows:
  55. * <pre>
  56. * EncryptionKey ::= SEQUENCE {
  57. * keytype[0] INTEGER,
  58. * keyvalue[1] OCTET STRING
  59. * }
  60. * </pre>
  61. *
  62. * @serial
  63. */
  64. private KeyImpl key;
  65. private transient boolean destroyed = false;
  66. /**
  67. * Constructs a KerberosKey from the given bytes when the key type and
  68. * key version number are known. This can be used when reading the secret
  69. * key information from a Kerberos "keytab".
  70. *
  71. * @param principal the principal that this secret key belongs to
  72. * @param keyBytes the raw bytes for the secret key
  73. * @param keyType the key type for the secret key as defined by the
  74. * Kerberos protocol specification.
  75. * @param versionNum the version number of this secret key
  76. */
  77. public KerberosKey(KerberosPrincipal principal,
  78. byte[] keyBytes,
  79. int keyType,
  80. int versionNum) {
  81. this.principal = principal;
  82. this.versionNum = versionNum;
  83. key = new KeyImpl(keyBytes, keyType);
  84. }
  85. /**
  86. * Constructs a KerberosKey from a principal's password.
  87. *
  88. * @param principal the principal that this password belongs to
  89. * @param password the password that should be used to compute the key
  90. * @param algorithm the name for the algorithm that this key will be
  91. * used for. This parameter may be null in which case the default
  92. * algorithm "DES" will be assumed.
  93. * @throws IllegalArgumentException if the name of the
  94. * algorithm passed is unsupported.
  95. */
  96. public KerberosKey(KerberosPrincipal principal,
  97. char[] password,
  98. String algorithm) {
  99. this.principal = principal;
  100. // Pass principal in for salt
  101. key = new KeyImpl(principal, password, algorithm);
  102. }
  103. /**
  104. * Returns the principal that this key belongs to.
  105. *
  106. * @return the principal this key belongs to.
  107. */
  108. public final KerberosPrincipal getPrincipal() {
  109. if (destroyed)
  110. throw new IllegalStateException("This key is no longer valid");
  111. return principal;
  112. }
  113. /**
  114. * Returns the key version number.
  115. *
  116. * @return the key version number.
  117. */
  118. public final int getVersionNumber() {
  119. if (destroyed)
  120. throw new IllegalStateException("This key is no longer valid");
  121. return versionNum;
  122. }
  123. /**
  124. * Returns the key type for this long-term key.
  125. *
  126. * @return the key type.
  127. */
  128. public final int getKeyType() {
  129. if (destroyed)
  130. throw new IllegalStateException("This key is no longer valid");
  131. return key.getKeyType();
  132. }
  133. /*
  134. * Methods from java.security.Key
  135. */
  136. /**
  137. * Returns the standard algorithm name for this key. For
  138. * example, "DES" would indicate that this key is a DES key.
  139. * See Appendix A in the <a href=
  140. * "../../../../../guide/security/CryptoSpec.html#AppA">
  141. * Java Cryptography Architecture API Specification & Reference
  142. * </a>
  143. * for information about standard algorithm names.
  144. *
  145. * @return the name of the algorithm associated with this key.
  146. */
  147. public final String getAlgorithm() {
  148. if (destroyed)
  149. throw new IllegalStateException("This key is no longer valid");
  150. return key.getAlgorithm();
  151. }
  152. /**
  153. * Returns the name of the encoding format for this secret key.
  154. *
  155. * @return the String "RAW"
  156. */
  157. public final String getFormat() {
  158. if (destroyed)
  159. throw new IllegalStateException("This key is no longer valid");
  160. return key.getFormat();
  161. }
  162. /**
  163. * Returns the key material of this secret key.
  164. *
  165. * @return the key material
  166. */
  167. public final byte[] getEncoded() {
  168. if (destroyed)
  169. throw new IllegalStateException("This key is no longer valid");
  170. return key.getEncoded();
  171. }
  172. /**
  173. * Destroys this key. A call to any of its other methods after this
  174. * will cause an IllegalStateException to be thrown.
  175. *
  176. * @throws DestroyFailedException if some error occurs while destorying
  177. * this key.
  178. */
  179. public void destroy() throws DestroyFailedException {
  180. if (!destroyed) {
  181. key.destroy();
  182. principal = null;
  183. destroyed = true;
  184. }
  185. }
  186. /** Determines if this key has been destroyed.*/
  187. public boolean isDestroyed() {
  188. return destroyed;
  189. }
  190. public String toString() {
  191. return "Kerberos Principal " + principal.toString() +
  192. "Key Version " + versionNum +
  193. "key " + key.toString();
  194. }
  195. }