diff options
author | Alex Klyubin <klyubin@google.com> | 2015-01-14 13:35:32 -0800 |
---|---|---|
committer | Alex Klyubin <klyubin@google.com> | 2015-01-14 13:35:32 -0800 |
commit | 21a76df55cf4b956f4d34f57c7b9e694d0363f54 (patch) | |
tree | 8569f523ecf44e5c723718445306b49ad60c1299 /keystore/java/android/security/KeyPairGeneratorSpec.java | |
parent | 837261cc997cbb188de05e3d1194adb91cb75825 (diff) |
Add KeyPairGenerator.EC backed by Android KeyStore.
The Android KeyStore backed KeyPairGenerator can generate EC key
pairs, but it cannot be instantiated via the standard JCA approach of
KeyPairGenerator.getInstance("EC", "AndroidKeyStore"). Instead, the
user must invoke KeyPairGenerator.getInstance("RSA",
"AndroidKeyStore") and then tell it to generate an EC key pair.
This CL fixes this weirdness.
The fix requires the introduction of late resolution of key algorithm
and default key size. Prior to this CL, these parameters were resolved
prior to KeyPairGenerator initialization, inside KeyPairGeneratorSpec.
In this CL, these parameters are resolved during KeyPairGenerator
initialization. This is fine because KeyPairGeneratorSpec should be as
dumb as possible and all the logic should reside in KeyPairGenerator
and lower layers.
Bug: 19018089
Change-Id: I114502356e6c9691518cf05b6d9eb0920b4fe0b2
Diffstat (limited to 'keystore/java/android/security/KeyPairGeneratorSpec.java')
-rw-r--r-- | keystore/java/android/security/KeyPairGeneratorSpec.java | 69 |
1 files changed, 2 insertions, 67 deletions
diff --git a/keystore/java/android/security/KeyPairGeneratorSpec.java b/keystore/java/android/security/KeyPairGeneratorSpec.java index 6b67f436bdd5..81964344c553 100644 --- a/keystore/java/android/security/KeyPairGeneratorSpec.java +++ b/keystore/java/android/security/KeyPairGeneratorSpec.java @@ -16,8 +16,6 @@ package android.security; -import com.android.org.conscrypt.NativeCrypto; - import android.content.Context; import android.text.TextUtils; @@ -26,7 +24,6 @@ import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.cert.Certificate; import java.security.spec.AlgorithmParameterSpec; -import java.security.spec.RSAKeyGenParameterSpec; import java.util.Date; import javax.security.auth.x500.X500Principal; @@ -54,19 +51,6 @@ import javax.security.auth.x500.X500Principal; * certificate signed by a real Certificate Authority. */ public final class KeyPairGeneratorSpec implements AlgorithmParameterSpec { - /* - * These must be kept in sync with system/security/keystore/defaults.h - */ - - /* EC */ - private static final int EC_DEFAULT_KEY_SIZE = 256; - private static final int EC_MIN_KEY_SIZE = 192; - private static final int EC_MAX_KEY_SIZE = 521; - - /* RSA */ - private static final int RSA_DEFAULT_KEY_SIZE = 2048; - private static final int RSA_MIN_KEY_SIZE = 512; - private static final int RSA_MAX_KEY_SIZE = 8192; private final Context mContext; @@ -139,13 +123,6 @@ public final class KeyPairGeneratorSpec implements AlgorithmParameterSpec { throw new IllegalArgumentException("endDate < startDate"); } - final int keyTypeInt = KeyStore.getKeyTypeForAlgorithm(keyType); - if (keySize == -1) { - keySize = getDefaultKeySizeForType(keyTypeInt); - } - checkCorrectParametersSpec(keyTypeInt, keySize, spec); - checkValidKeySize(keyTypeInt, keySize); - mContext = context; mKeystoreAlias = keyStoreAlias; mKeyType = keyType; @@ -158,46 +135,6 @@ public final class KeyPairGeneratorSpec implements AlgorithmParameterSpec { mFlags = flags; } - private static int getDefaultKeySizeForType(int keyType) { - if (keyType == NativeCrypto.EVP_PKEY_EC) { - return EC_DEFAULT_KEY_SIZE; - } else if (keyType == NativeCrypto.EVP_PKEY_RSA) { - return RSA_DEFAULT_KEY_SIZE; - } - throw new IllegalArgumentException("Invalid key type " + keyType); - } - - private static void checkValidKeySize(int keyType, int keySize) { - if (keyType == NativeCrypto.EVP_PKEY_EC) { - if (keySize < EC_MIN_KEY_SIZE || keySize > EC_MAX_KEY_SIZE) { - throw new IllegalArgumentException("EC keys must be >= " + EC_MIN_KEY_SIZE - + " and <= " + EC_MAX_KEY_SIZE); - } - } else if (keyType == NativeCrypto.EVP_PKEY_RSA) { - if (keySize < RSA_MIN_KEY_SIZE || keySize > RSA_MAX_KEY_SIZE) { - throw new IllegalArgumentException("RSA keys must be >= " + RSA_MIN_KEY_SIZE - + " and <= " + RSA_MAX_KEY_SIZE); - } - } else { - throw new IllegalArgumentException("Invalid key type " + keyType); - } - } - - private static void checkCorrectParametersSpec(int keyType, int keySize, - AlgorithmParameterSpec spec) { - if (keyType == NativeCrypto.EVP_PKEY_RSA && spec != null) { - if (spec instanceof RSAKeyGenParameterSpec) { - RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec) spec; - if (keySize != -1 && keySize != rsaSpec.getKeysize()) { - throw new IllegalArgumentException("RSA key size must match: " + keySize - + " vs " + rsaSpec.getKeysize()); - } - } else { - throw new IllegalArgumentException("RSA may only use RSAKeyGenParameterSpec"); - } - } - } - /** * Gets the Android context used for operations with this instance. */ @@ -311,7 +248,7 @@ public final class KeyPairGeneratorSpec implements AlgorithmParameterSpec { private String mKeystoreAlias; - private String mKeyType = "RSA"; + private String mKeyType; private int mKeySize = -1; @@ -360,9 +297,7 @@ public final class KeyPairGeneratorSpec implements AlgorithmParameterSpec { if (keyType == null) { throw new NullPointerException("keyType == null"); } else { - try { - KeyStore.getKeyTypeForAlgorithm(keyType); - } catch (IllegalArgumentException e) { + if (KeyStore.getKeyTypeForAlgorithm(keyType) == -1) { throw new NoSuchAlgorithmException("Unsupported key type: " + keyType); } } |