KerberosKey.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package javax.security.auth.kerberos;
  18. import javax.crypto.SecretKey;
  19. import javax.security.auth.DestroyFailedException;
  20. import javax.security.auth.Destroyable;
  21. import org.apache.harmony.auth.internal.nls.Messages;
  22. /**
  23. * See <a href="http://www.ietf.org/rfc/rfc3961.txt">RFC3961</a>
  24. */
  25. public class KerberosKey implements SecretKey, Destroyable {
  26. private static final long serialVersionUID = -4625402278148246993L;
  27. //principal
  28. private KerberosPrincipal principal;
  29. //key version number
  30. private int versionNum;
  31. //raw bytes for the secret key
  32. private KeyImpl key;
  33. // indicates the ticket state
  34. private transient boolean destroyed;
  35. public KerberosKey(KerberosPrincipal principal, byte[] keyBytes, int keyType,
  36. int versionNumber) {
  37. if (keyBytes == null) {
  38. throw new NullPointerException(Messages.getString("auth.47")); //$NON-NLS-1$
  39. }
  40. this.principal = principal;
  41. this.versionNum = versionNumber;
  42. this.key = new KeyImpl(keyBytes, keyType);
  43. }
  44. public KerberosKey(KerberosPrincipal principal, char[] password, String algorithm) {
  45. this.principal = principal;
  46. this.key = new KeyImpl(principal, password, algorithm);
  47. }
  48. public final KerberosPrincipal getPrincipal() {
  49. checkState();
  50. return principal;
  51. }
  52. public final String getAlgorithm() {
  53. return key.getAlgorithm();
  54. }
  55. public final String getFormat() {
  56. return key.getFormat();
  57. }
  58. public final int getKeyType() {
  59. return key.getKeyType();
  60. }
  61. public final byte[] getEncoded() {
  62. return key.getEncoded();
  63. }
  64. public final int getVersionNumber() {
  65. checkState();
  66. return versionNum;
  67. }
  68. public void destroy() throws DestroyFailedException {
  69. if (!destroyed) {
  70. this.principal = null;
  71. key.destroy();
  72. this.destroyed = true;
  73. }
  74. }
  75. public boolean isDestroyed() {
  76. return destroyed;
  77. }
  78. @Override
  79. public String toString() {
  80. checkState();
  81. StringBuilder sb = new StringBuilder();
  82. sb.append("KerberosPrincipal ").append(principal.getName()).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
  83. sb.append("KeyVersion ").append(versionNum).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
  84. sb.append(key.toString());
  85. return sb.toString();
  86. }
  87. // if a key is destroyed then IllegalStateException must be thrown
  88. private void checkState() {
  89. if (destroyed) {
  90. throw new IllegalStateException(Messages.getString("auth.48")); //$NON-NLS-1$
  91. }
  92. }
  93. }