You've already forked android_translation_layer
mirror of
https://gitlab.com/android_translation_layer/android_translation_layer.git
synced 2025-10-27 11:48:10 -07:00
add KeyGenerator for AndroidKeyStore
This commit is contained in:
@@ -130,6 +130,7 @@ public class Context extends Object {
|
||||
|
||||
Provider provider = new Provider("AndroidKeyStore", 1.0, "Android KeyStore provider") {};
|
||||
provider.put("KeyStore.AndroidKeyStore", "android.security.keystore.AndroidKeyStore");
|
||||
provider.put("KeyGenerator.AES", "android.security.keystore.KeyGenerator");
|
||||
Security.addProvider(provider);
|
||||
|
||||
r.applyPackageQuirks(application_info.packageName);
|
||||
|
||||
@@ -10,6 +10,7 @@ import java.security.NoSuchAlgorithmException;
|
||||
import java.security.UnrecoverableKeyException;
|
||||
import java.security.cert.Certificate;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.Enumeration;
|
||||
@@ -19,12 +20,12 @@ import android.util.Slog;
|
||||
|
||||
public class AndroidKeyStore extends KeyStoreSpi {
|
||||
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
static HashMap<String, Key> map = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public Key engineGetKey(String alias, char[] password) throws NoSuchAlgorithmException, UnrecoverableKeyException {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'engineGetKey'");
|
||||
System.out.println("engineGetKey alias=" + alias + " password=" + Arrays.toString(password));
|
||||
return map.get(alias);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package android.security.keystore;
|
||||
|
||||
public class KeyGenParameterSpec {
|
||||
|
||||
private String keystoreAlias;
|
||||
private int purposes;
|
||||
private int keySize;
|
||||
private String[] blockModes;
|
||||
private String[] encryptionPaddings;
|
||||
private boolean userAuthenticationRequired;
|
||||
|
||||
public static class Builder {
|
||||
private KeyGenParameterSpec spec = new KeyGenParameterSpec();
|
||||
|
||||
public Builder(String keystoreAlias, int purposes) {
|
||||
spec.keystoreAlias = keystoreAlias;
|
||||
spec.purposes = purposes;
|
||||
}
|
||||
|
||||
public Builder setKeySize(int keySize) {
|
||||
spec.keySize = keySize;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setBlockModes(String[] blockModes) {
|
||||
spec.blockModes = blockModes;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setEncryptionPaddings(String[] encryptionPaddings) {
|
||||
spec.encryptionPaddings = encryptionPaddings;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setUserAuthenticationRequired(boolean userAuthenticationRequired) {
|
||||
spec.userAuthenticationRequired = userAuthenticationRequired;
|
||||
return this;
|
||||
}
|
||||
|
||||
public KeyGenParameterSpec build() {
|
||||
return spec;
|
||||
}
|
||||
}
|
||||
|
||||
public int getKeySize() {
|
||||
return keySize;
|
||||
}
|
||||
|
||||
public String[] getBlockModes() {
|
||||
return blockModes;
|
||||
}
|
||||
|
||||
public int getPurposes() {
|
||||
return purposes;
|
||||
}
|
||||
|
||||
public String[] getEncryptionPaddings() {
|
||||
return encryptionPaddings;
|
||||
}
|
||||
|
||||
public boolean isUserAuthenticationRequired() {
|
||||
return userAuthenticationRequired;
|
||||
}
|
||||
|
||||
public String getKeystoreAlias() {
|
||||
return keystoreAlias;
|
||||
}
|
||||
}
|
||||
50
src/api-impl/android/security/keystore/KeyGenerator.java
Normal file
50
src/api-impl/android/security/keystore/KeyGenerator.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package android.security.keystore;
|
||||
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
|
||||
import javax.crypto.KeyGeneratorSpi;
|
||||
import javax.crypto.SecretKey;
|
||||
|
||||
public class KeyGenerator extends KeyGeneratorSpi {
|
||||
|
||||
private javax.crypto.KeyGenerator keyGenerator;
|
||||
private AlgorithmParameterSpec params;
|
||||
|
||||
@Override
|
||||
protected SecretKey engineGenerateKey() {
|
||||
System.out.println("generating key with alias " + ((KeyGenParameterSpec)params).getKeystoreAlias());
|
||||
SecretKey key = keyGenerator.generateKey();
|
||||
AndroidKeyStore.map.put(((KeyGenParameterSpec)params).getKeystoreAlias(), key);
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void engineInit(SecureRandom random) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'engineInit'");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void engineInit(AlgorithmParameterSpec params, SecureRandom random)
|
||||
throws InvalidAlgorithmParameterException {
|
||||
try {
|
||||
keyGenerator = javax.crypto.KeyGenerator.getInstance("AES", "BC");
|
||||
this.params = params;
|
||||
keyGenerator.init(random);
|
||||
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
|
||||
e.printStackTrace();
|
||||
throw new UnsupportedOperationException("Unimplemented method 'engineInit'");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void engineInit(int keysize, SecureRandom random) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'engineInit'");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -373,6 +373,8 @@ srcs = [
|
||||
'android/provider/Settings.java',
|
||||
'android/provider/UserDictionary.java',
|
||||
'android/security/keystore/AndroidKeyStore.java',
|
||||
'android/security/keystore/KeyGenerator.java',
|
||||
'android/security/keystore/KeyGenParameterSpec.java',
|
||||
'android/service/media/MediaBrowserService.java',
|
||||
'android/speech/tts/TextToSpeech.java',
|
||||
'android/telecom/ConnectionService.java',
|
||||
|
||||
Reference in New Issue
Block a user