FileDocCategorySizeDatePackage
KeyImpl.javaAPI DocJava SE 5 API5204Fri Aug 26 14:57:48 BST 2005javax.security.auth.kerberos

KeyImpl

public class KeyImpl extends Object implements SecretKey, Destroyable, Serializable
This class encapsulates a Kerberos encryption key. It is not associated with a principal and may represent an ephemeral session key.
author
Mayank Upadhyay
version
1.13, 04/01/04
since
1.4
serial
include

Fields Summary
private static final long
serialVersionUID
private transient byte[]
keyBytes
private transient int
keyType
private transient boolean
destroyed
Constructors Summary
public KeyImpl(byte[] keyBytes, int keyType)
Constructs a KeyImpl from the given bytes.

param
keyBytes the raw bytes for the secret key
param
keyType the key type for the secret key as defined by the Kerberos protocol specification.



                                          
       
		         
	this.keyBytes = (byte[]) keyBytes.clone();
	this.keyType = keyType;
    
public KeyImpl(KerberosPrincipal principal, char[] password, String algorithm)
Constructs a KeyImpl from a password.

param
principal the principal from which to derive the salt
param
password the password that should be used to compute the key.
param
algorithm the name for the algorithm that this key wil be used for. This parameter may be null in which case "DES" will be assumed.


	try {
	    PrincipalName princ = new PrincipalName(principal.getName());
	    EncryptionKey key = 
		new EncryptionKey(password, princ.getSalt(),algorithm);
	    this.keyBytes = key.getBytes();
	    this.keyType = key.getEType();
	} catch (KrbException e) {
	    throw new IllegalArgumentException(e.getMessage());
	}
    
Methods Summary
public voiddestroy()

	if (!destroyed) {
	    Arrays.fill(keyBytes, (byte) 0);
	    destroyed = true;
	}
    
public final java.lang.StringgetAlgorithm()

	return getAlgorithmName(keyType);
    
private java.lang.StringgetAlgorithmName(int eType)

	if (destroyed)
	    throw new IllegalStateException("This key is no longer valid");

	switch (eType) {
	case EncryptedData.ETYPE_DES_CBC_CRC:
	case EncryptedData.ETYPE_DES_CBC_MD5:
	    return "DES";

	case EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD:
	    return "DESede";

	case EncryptedData.ETYPE_NULL:
	    return "NULL";

	default:
	    throw new IllegalArgumentException(
		"Unsupported encryption type: " + eType);
	}
    
public final byte[]getEncoded()

	if (destroyed)
	    throw new IllegalStateException("This key is no longer valid");
	return (byte[])keyBytes.clone();
    
public final java.lang.StringgetFormat()

	if (destroyed)
	    throw new IllegalStateException("This key is no longer valid");
	return "RAW";
    
public final intgetKeyType()
Returns the keyType for this key as defined in the Kerberos Spec.

	if (destroyed)
	    throw new IllegalStateException("This key is no longer valid");
	return keyType;
    
public booleanisDestroyed()

	return destroyed;
    
private synchronized voidreadObject(java.io.ObjectInputStream ois)

	try {
	    EncryptionKey encKey = new EncryptionKey(new 
				     DerValue((byte[])ois.readObject()));
	    keyType = encKey.getEType();
	    keyBytes = encKey.getBytes();
	} catch (Asn1Exception ae) {
	    throw new IOException (ae.getMessage());
	}
    
public java.lang.StringtoString()

	HexDumpEncoder hd = new HexDumpEncoder();	
	return new String("EncryptionKey: keyType=" + keyType
                          + " keyBytes (hex dump)="
                          + (keyBytes == null || keyBytes.length == 0 ?
                             " Empty Key" :
                             '\n" + hd.encode(keyBytes)
                          + '\n"));

	
    
private synchronized voidwriteObject(java.io.ObjectOutputStream ois)

serialData
this KeyImpl is serialized by writing out the ASN1 Encoded bytes of the encryption key. The ASN1 encoding is defined in RFC1510 and as follows: EncryptionKey ::= SEQUENCE { keytype[0] INTEGER, keyvalue[1] OCTET STRING }

	if (destroyed) {
	   throw new IOException ("This key is no longer valid");
	}

	try {
	   ois.writeObject((new EncryptionKey(keyType,keyBytes)).asn1Encode());
	} catch (Asn1Exception ae) {
	   throw new IOException(ae.getMessage());
	}