001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.security.auth.kerberos;
019:
020: import javax.crypto.SecretKey;
021: import javax.security.auth.DestroyFailedException;
022: import javax.security.auth.Destroyable;
023:
024: import org.apache.harmony.auth.internal.nls.Messages;
025:
026: /**
027: * See <a href="http://www.ietf.org/rfc/rfc3961.txt">RFC3961</a>
028: */
029: public class KerberosKey implements SecretKey, Destroyable {
030:
031: private static final long serialVersionUID = -4625402278148246993L;
032:
033: //principal
034: private KerberosPrincipal principal;
035:
036: //key version number
037: private int versionNum;
038:
039: //raw bytes for the secret key
040: private KeyImpl key;
041:
042: // indicates the ticket state
043: private transient boolean destroyed;
044:
045: public KerberosKey(KerberosPrincipal principal, byte[] keyBytes,
046: int keyType, int versionNumber) {
047:
048: if (keyBytes == null) {
049: throw new NullPointerException(Messages
050: .getString("auth.47")); //$NON-NLS-1$
051: }
052:
053: this .principal = principal;
054: this .versionNum = versionNumber;
055:
056: this .key = new KeyImpl(keyBytes, keyType);
057:
058: }
059:
060: public KerberosKey(KerberosPrincipal principal, char[] password,
061: String algorithm) {
062:
063: this .principal = principal;
064:
065: this .key = new KeyImpl(principal, password, algorithm);
066: }
067:
068: public final KerberosPrincipal getPrincipal() {
069: checkState();
070: return principal;
071: }
072:
073: public final String getAlgorithm() {
074: return key.getAlgorithm();
075: }
076:
077: public final String getFormat() {
078: return key.getFormat();
079: }
080:
081: public final int getKeyType() {
082: return key.getKeyType();
083: }
084:
085: public final byte[] getEncoded() {
086: return key.getEncoded();
087: }
088:
089: public final int getVersionNumber() {
090: checkState();
091: return versionNum;
092: }
093:
094: public void destroy() throws DestroyFailedException {
095: if (!destroyed) {
096: this .principal = null;
097: key.destroy();
098: this .destroyed = true;
099: }
100: }
101:
102: public boolean isDestroyed() {
103: return destroyed;
104: }
105:
106: @Override
107: public String toString() {
108: checkState();
109: StringBuilder sb = new StringBuilder();
110: sb
111: .append("KerberosPrincipal ").append(principal.getName()).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
112: sb.append("KeyVersion ").append(versionNum).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
113: sb.append(key.toString());
114: return sb.toString();
115: }
116:
117: // if a key is destroyed then IllegalStateException must be thrown
118: private void checkState() {
119: if (destroyed) {
120: throw new IllegalStateException(Messages
121: .getString("auth.48")); //$NON-NLS-1$
122: }
123: }
124: }
|