1 7 8 package javax.security.auth.kerberos; 9 10 import java.io.*; 11 import java.util.Arrays ; 12 import javax.crypto.SecretKey; 13 import javax.security.auth.Destroyable ; 14 import javax.security.auth.DestroyFailedException ; 15 import sun.misc.HexDumpEncoder; 16 import sun.security.krb5.Asn1Exception; 17 import sun.security.krb5.PrincipalName; 18 import sun.security.krb5.EncryptionKey; 19 import sun.security.krb5.EncryptedData; 20 import sun.security.krb5.KrbException; 21 import sun.security.krb5.KrbCryptoException; 22 import sun.security.util.DerValue; 23 24 34 class KeyImpl implements SecretKey, Destroyable , Serializable { 35 36 private static final long serialVersionUID = -7889313790214321193L; 37 38 private transient byte[] keyBytes; 39 private transient int keyType; 40 private transient boolean destroyed = false; 41 42 43 50 public KeyImpl(byte[] keyBytes, 51 int keyType) { 52 this.keyBytes = (byte[]) keyBytes.clone(); 53 this.keyType = keyType; 54 } 55 56 66 public KeyImpl(KerberosPrincipal principal, 67 char[] password, 68 String algorithm) { 69 70 try { 71 PrincipalName princ = new PrincipalName(principal.getName()); 72 EncryptionKey key = 73 new EncryptionKey(password, princ.getSalt(),algorithm); 74 this.keyBytes = key.getBytes(); 75 this.keyType = key.getEType(); 76 } catch (KrbException e) { 77 throw new IllegalArgumentException (e.getMessage()); 78 } 79 } 80 81 84 public final int getKeyType() { 85 if (destroyed) 86 throw new IllegalStateException ("This key is no longer valid"); 87 return keyType; 88 } 89 90 93 94 public final String getAlgorithm() { 95 return getAlgorithmName(keyType); 96 } 97 98 private String getAlgorithmName(int eType) { 99 if (destroyed) 100 throw new IllegalStateException ("This key is no longer valid"); 101 102 switch (eType) { 103 case EncryptedData.ETYPE_DES_CBC_CRC: 104 case EncryptedData.ETYPE_DES_CBC_MD5: 105 return "DES"; 106 107 case EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD: 108 return "DESede"; 109 110 case EncryptedData.ETYPE_ARCFOUR_HMAC: 111 return "ArcFourHmac"; 112 113 case EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96: 114 return "AES128"; 115 116 case EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96: 117 return "AES256"; 118 119 case EncryptedData.ETYPE_NULL: 120 return "NULL"; 121 122 default: 123 throw new IllegalArgumentException ( 124 "Unsupported encryption type: " + eType); 125 } 126 } 127 128 public final String getFormat() { 129 if (destroyed) 130 throw new IllegalStateException ("This key is no longer valid"); 131 return "RAW"; 132 } 133 134 public final byte[] getEncoded() { 135 if (destroyed) 136 throw new IllegalStateException ("This key is no longer valid"); 137 return (byte[])keyBytes.clone(); 138 } 139 140 public void destroy() throws DestroyFailedException { 141 if (!destroyed) { 142 Arrays.fill(keyBytes, (byte) 0); 143 destroyed = true; 144 } 145 } 146 147 public boolean isDestroyed() { 148 return destroyed; 149 } 150 151 161 162 private synchronized void writeObject(ObjectOutputStream ois) 163 throws IOException { 164 if (destroyed) { 165 throw new IOException ("This key is no longer valid"); 166 } 167 168 try { 169 ois.writeObject((new EncryptionKey(keyType,keyBytes)).asn1Encode()); 170 } catch (Asn1Exception ae) { 171 throw new IOException(ae.getMessage()); 172 } 173 } 174 175 private synchronized void readObject(ObjectInputStream ois) 176 throws IOException , ClassNotFoundException { 177 try { 178 EncryptionKey encKey = new EncryptionKey(new 179 DerValue((byte[])ois.readObject())); 180 keyType = encKey.getEType(); 181 keyBytes = encKey.getBytes(); 182 } catch (Asn1Exception ae) { 183 throw new IOException (ae.getMessage()); 184 } 185 } 186 187 public String toString() { 188 HexDumpEncoder hd = new HexDumpEncoder(); 189 return new String ("EncryptionKey: keyType=" + keyType 190 + " keyBytes (hex dump)=" 191 + (keyBytes == null || keyBytes.length == 0 ? 192 " Empty Key" : 193 '\n' + hd.encode(keyBytes) 194 + '\n')); 195 196 197 } 198 } 199 | Popular Tags |