|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package com.sun.crypto.provider; |
|
|
|
import java.security.InvalidKeyException; |
|
import java.security.spec.KeySpec; |
|
import java.security.spec.InvalidKeySpecException; |
|
import javax.crypto.SecretKey; |
|
import javax.crypto.SecretKeyFactorySpi; |
|
import javax.crypto.spec.PBEKeySpec; |
|
import java.util.HashSet; |
|
import java.util.Locale; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
abstract class PBEKeyFactory extends SecretKeyFactorySpi { |
|
|
|
private String type; |
|
private static HashSet<String> validTypes; |
|
|
|
|
|
|
|
*/ |
|
private PBEKeyFactory(String keytype) { |
|
type = keytype; |
|
} |
|
|
|
static { |
|
validTypes = new HashSet<>(17); |
|
validTypes.add("PBEWithMD5AndDES".toUpperCase(Locale.ENGLISH)); |
|
validTypes.add("PBEWithSHA1AndDESede".toUpperCase(Locale.ENGLISH)); |
|
validTypes.add("PBEWithSHA1AndRC2_40".toUpperCase(Locale.ENGLISH)); |
|
validTypes.add("PBEWithSHA1AndRC2_128".toUpperCase(Locale.ENGLISH)); |
|
validTypes.add("PBEWithSHA1AndRC4_40".toUpperCase(Locale.ENGLISH)); |
|
validTypes.add("PBEWithSHA1AndRC4_128".toUpperCase(Locale.ENGLISH)); |
|
|
|
validTypes.add("PBEWithMD5AndTripleDES".toUpperCase(Locale.ENGLISH)); |
|
validTypes.add("PBEWithHmacSHA1AndAES_128".toUpperCase(Locale.ENGLISH)); |
|
validTypes.add("PBEWithHmacSHA224AndAES_128".toUpperCase(Locale.ENGLISH)); |
|
validTypes.add("PBEWithHmacSHA256AndAES_128".toUpperCase(Locale.ENGLISH)); |
|
validTypes.add("PBEWithHmacSHA384AndAES_128".toUpperCase(Locale.ENGLISH)); |
|
validTypes.add("PBEWithHmacSHA512AndAES_128".toUpperCase(Locale.ENGLISH)); |
|
validTypes.add("PBEWithHmacSHA1AndAES_256".toUpperCase(Locale.ENGLISH)); |
|
validTypes.add("PBEWithHmacSHA224AndAES_256".toUpperCase(Locale.ENGLISH)); |
|
validTypes.add("PBEWithHmacSHA256AndAES_256".toUpperCase(Locale.ENGLISH)); |
|
validTypes.add("PBEWithHmacSHA384AndAES_256".toUpperCase(Locale.ENGLISH)); |
|
validTypes.add("PBEWithHmacSHA512AndAES_256".toUpperCase(Locale.ENGLISH)); |
|
} |
|
|
|
public static final class PBEWithMD5AndDES |
|
extends PBEKeyFactory { |
|
public PBEWithMD5AndDES() { |
|
super("PBEWithMD5AndDES"); |
|
} |
|
} |
|
|
|
public static final class PBEWithSHA1AndDESede |
|
extends PBEKeyFactory { |
|
public PBEWithSHA1AndDESede() { |
|
super("PBEWithSHA1AndDESede"); |
|
} |
|
} |
|
|
|
public static final class PBEWithSHA1AndRC2_40 |
|
extends PBEKeyFactory { |
|
public PBEWithSHA1AndRC2_40() { |
|
super("PBEWithSHA1AndRC2_40"); |
|
} |
|
} |
|
|
|
public static final class PBEWithSHA1AndRC2_128 |
|
extends PBEKeyFactory { |
|
public PBEWithSHA1AndRC2_128() { |
|
super("PBEWithSHA1AndRC2_128"); |
|
} |
|
} |
|
|
|
public static final class PBEWithSHA1AndRC4_40 |
|
extends PBEKeyFactory { |
|
public PBEWithSHA1AndRC4_40() { |
|
super("PBEWithSHA1AndRC4_40"); |
|
} |
|
} |
|
|
|
public static final class PBEWithSHA1AndRC4_128 |
|
extends PBEKeyFactory { |
|
public PBEWithSHA1AndRC4_128() { |
|
super("PBEWithSHA1AndRC4_128"); |
|
} |
|
} |
|
|
|
|
|
|
|
*/ |
|
public static final class PBEWithMD5AndTripleDES |
|
extends PBEKeyFactory { |
|
public PBEWithMD5AndTripleDES() { |
|
super("PBEWithMD5AndTripleDES"); |
|
} |
|
} |
|
|
|
public static final class PBEWithHmacSHA1AndAES_128 |
|
extends PBEKeyFactory { |
|
public PBEWithHmacSHA1AndAES_128() { |
|
super("PBEWithHmacSHA1AndAES_128"); |
|
} |
|
} |
|
|
|
public static final class PBEWithHmacSHA224AndAES_128 |
|
extends PBEKeyFactory { |
|
public PBEWithHmacSHA224AndAES_128() { |
|
super("PBEWithHmacSHA224AndAES_128"); |
|
} |
|
} |
|
|
|
public static final class PBEWithHmacSHA256AndAES_128 |
|
extends PBEKeyFactory { |
|
public PBEWithHmacSHA256AndAES_128() { |
|
super("PBEWithHmacSHA256AndAES_128"); |
|
} |
|
} |
|
|
|
public static final class PBEWithHmacSHA384AndAES_128 |
|
extends PBEKeyFactory { |
|
public PBEWithHmacSHA384AndAES_128() { |
|
super("PBEWithHmacSHA384AndAES_128"); |
|
} |
|
} |
|
|
|
public static final class PBEWithHmacSHA512AndAES_128 |
|
extends PBEKeyFactory { |
|
public PBEWithHmacSHA512AndAES_128() { |
|
super("PBEWithHmacSHA512AndAES_128"); |
|
} |
|
} |
|
|
|
public static final class PBEWithHmacSHA1AndAES_256 |
|
extends PBEKeyFactory { |
|
public PBEWithHmacSHA1AndAES_256() { |
|
super("PBEWithHmacSHA1AndAES_256"); |
|
} |
|
} |
|
|
|
public static final class PBEWithHmacSHA224AndAES_256 |
|
extends PBEKeyFactory { |
|
public PBEWithHmacSHA224AndAES_256() { |
|
super("PBEWithHmacSHA224AndAES_256"); |
|
} |
|
} |
|
|
|
public static final class PBEWithHmacSHA256AndAES_256 |
|
extends PBEKeyFactory { |
|
public PBEWithHmacSHA256AndAES_256() { |
|
super("PBEWithHmacSHA256AndAES_256"); |
|
} |
|
} |
|
|
|
public static final class PBEWithHmacSHA384AndAES_256 |
|
extends PBEKeyFactory { |
|
public PBEWithHmacSHA384AndAES_256() { |
|
super("PBEWithHmacSHA384AndAES_256"); |
|
} |
|
} |
|
|
|
public static final class PBEWithHmacSHA512AndAES_256 |
|
extends PBEKeyFactory { |
|
public PBEWithHmacSHA512AndAES_256() { |
|
super("PBEWithHmacSHA512AndAES_256"); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected SecretKey engineGenerateSecret(KeySpec keySpec) |
|
throws InvalidKeySpecException |
|
{ |
|
if (!(keySpec instanceof PBEKeySpec)) { |
|
throw new InvalidKeySpecException("Invalid key spec"); |
|
} |
|
return new PBEKey((PBEKeySpec)keySpec, type, true); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpecCl) |
|
throws InvalidKeySpecException { |
|
if ((key instanceof SecretKey) |
|
&& (validTypes.contains(key.getAlgorithm().toUpperCase(Locale.ENGLISH))) |
|
&& (key.getFormat().equalsIgnoreCase("RAW"))) { |
|
|
|
|
|
if ((keySpecCl != null) |
|
&& PBEKeySpec.class.isAssignableFrom(keySpecCl)) { |
|
byte[] passwdBytes = key.getEncoded(); |
|
char[] passwdChars = new char[passwdBytes.length]; |
|
for (int i=0; i<passwdChars.length; i++) |
|
passwdChars[i] = (char) (passwdBytes[i] & 0x7f); |
|
PBEKeySpec ret = new PBEKeySpec(passwdChars); |
|
// password char[] was cloned in PBEKeySpec constructor, |
|
|
|
java.util.Arrays.fill(passwdChars, ' '); |
|
java.util.Arrays.fill(passwdBytes, (byte)0x00); |
|
return ret; |
|
} else { |
|
throw new InvalidKeySpecException("Invalid key spec"); |
|
} |
|
} else { |
|
throw new InvalidKeySpecException("Invalid key " |
|
+ "format/algorithm"); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected SecretKey engineTranslateKey(SecretKey key) |
|
throws InvalidKeyException |
|
{ |
|
try { |
|
if ((key != null) && |
|
(validTypes.contains(key.getAlgorithm().toUpperCase(Locale.ENGLISH))) && |
|
(key.getFormat().equalsIgnoreCase("RAW"))) { |
|
|
|
|
|
if (key instanceof com.sun.crypto.provider.PBEKey) { |
|
return key; |
|
} |
|
|
|
|
|
PBEKeySpec pbeKeySpec = (PBEKeySpec)engineGetKeySpec |
|
(key, PBEKeySpec.class); |
|
|
|
try { |
|
|
|
return engineGenerateSecret(pbeKeySpec); |
|
} finally { |
|
pbeKeySpec.clearPassword(); |
|
} |
|
} else { |
|
throw new InvalidKeyException("Invalid key format/algorithm"); |
|
} |
|
|
|
} catch (InvalidKeySpecException ikse) { |
|
throw new InvalidKeyException("Cannot translate key: " |
|
+ ikse.getMessage()); |
|
} |
|
} |
|
} |