Skip to content

Commit 26c2527

Browse files
committed
Support generating and using ECDSA keys
Add support for generating ECDSA keys when generating keys directly in KeyChain (the only option used to be RSA keys). Additionally, when testing use of a key, invoke the right algorithm based on key type so that EC keys would be supported. Bug: 132435000 Test: Manual, generated EC key then tested its use. Change-Id: Id8fc69909d00396f16d8ce41f93db630eac43b7a
1 parent 475b250 commit 26c2527

File tree

5 files changed

+45
-10
lines changed

5 files changed

+45
-10
lines changed

app/src/main/java/com/afwsamples/testdpc/policy/PolicyManagementFragment.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ public void onPositiveButtonClicked(String[] lockTaskArray) {
987987
choosePrivateKeyForRemoval();
988988
return true;
989989
case GENERATE_KEY_CERTIFICATE_KEY:
990-
showPromptForGeneratedKeyAlias("generated-rsa-testdpc-1");
990+
showPromptForGeneratedKeyAlias("generated-key-testdpc-1");
991991
return true;
992992
case TEST_KEY_USABILITY_KEY:
993993
testKeyCanBeUsedForSigning();
@@ -1440,10 +1440,11 @@ private boolean installKeyPair(final PrivateKey key, final Certificate cert, fin
14401440
private void generateKeyPair(final String alias, boolean isUserSelectable,
14411441
byte[] attestationChallenge,
14421442
int idAttestationFlags,
1443-
boolean useStrongBox) {
1443+
boolean useStrongBox,
1444+
boolean generateEcKey) {
14441445
new GenerateKeyAndCertificateTask(
14451446
alias, isUserSelectable, attestationChallenge, idAttestationFlags,
1446-
useStrongBox, getActivity(), mAdminComponentName).execute();
1447+
useStrongBox, generateEcKey, getActivity(), mAdminComponentName).execute();
14471448
}
14481449

14491450
/**
@@ -2477,6 +2478,9 @@ private void showPromptForGeneratedKeyAlias(String alias) {
24772478
R.id.alias_user_selectable);
24782479
userSelectableCheckbox.setChecked(!BuildCompat.isAtLeastP());
24792480

2481+
final CheckBox ecKeyCheckbox = aliasNamingView.findViewById(
2482+
R.id.generate_ec_key);
2483+
24802484
// Attestation check-boxes
24812485
final CheckBox includeAttestationChallengeCheckbox = aliasNamingView.findViewById(
24822486
R.id.include_key_attestation_challenge);
@@ -2520,7 +2524,8 @@ public void onClick(DialogInterface dialog, int which) {
25202524
}
25212525

25222526
generateKeyPair(alias, isUserSelectable, attestationChallenge,
2523-
idAttestationFlags, useStrongBoxCheckbox.isChecked());
2527+
idAttestationFlags, useStrongBoxCheckbox.isChecked(),
2528+
ecKeyCheckbox.isChecked());
25242529
}
25252530
})
25262531
.setNegativeButton(android.R.string.cancel, null)

app/src/main/java/com/afwsamples/testdpc/policy/keymanagement/GenerateKeyAndCertificateTask.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class GenerateKeyAndCertificateTask extends AsyncTask<Void, Integer, Atte
5858
private final byte[] mAttestationChallenge;
5959
private final int mIdAttestationFlags;
6060
private final boolean mUseStrongBox;
61+
private final boolean mGenerateEcKey;
6162
private final ComponentName mAdminComponentName;
6263
private final DevicePolicyManager mDevicePolicyManager;
6364
private final Activity mActivity;
@@ -68,13 +69,15 @@ public GenerateKeyAndCertificateTask(
6869
byte[] attestationChallenge,
6970
int idAttestationFlags,
7071
boolean useStrongBox,
72+
boolean generateEcKey,
7173
Activity activity,
7274
ComponentName admin) {
7375
mAlias = alias;
7476
mIsUserSelectable = isUserSelectable;
7577
mAttestationChallenge = attestationChallenge;
7678
mIdAttestationFlags = idAttestationFlags;
7779
mUseStrongBox = useStrongBox;
80+
mGenerateEcKey = generateEcKey;
7881
mActivity = activity;
7982
mAdminComponentName = admin;
8083
mDevicePolicyManager =
@@ -89,21 +92,34 @@ protected AttestedKeyPair doInBackground(Void... voids) {
8992
new KeyGenParameterSpec.Builder(
9093
mAlias,
9194
KeyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY)
92-
.setKeySize(2048)
9395
.setDigests(KeyProperties.DIGEST_SHA256)
94-
.setSignaturePaddings(
95-
KeyProperties.SIGNATURE_PADDING_RSA_PSS,
96-
KeyProperties.SIGNATURE_PADDING_RSA_PKCS1)
9796
.setIsStrongBoxBacked(mUseStrongBox);
9897

9998
if (mAttestationChallenge != null) {
10099
keySpecBuilder.setAttestationChallenge(mAttestationChallenge);
101100
}
102101

102+
if (mGenerateEcKey) {
103+
keySpecBuilder.setKeySize(256);
104+
} else {
105+
// RSA key
106+
keySpecBuilder.setSignaturePaddings(
107+
KeyProperties.SIGNATURE_PADDING_RSA_PSS,
108+
KeyProperties.SIGNATURE_PADDING_RSA_PKCS1)
109+
.setKeySize(2048);
110+
}
111+
112+
String keyAlgorithm;
113+
if (mGenerateEcKey) {
114+
keyAlgorithm = KeyProperties.KEY_ALGORITHM_EC;
115+
} else {
116+
keyAlgorithm = KeyProperties.KEY_ALGORITHM_RSA;
117+
}
118+
103119
KeyGenParameterSpec keySpec = keySpecBuilder.build();
104120
AttestedKeyPair keyPair =
105121
mDevicePolicyManager.generateKeyPair(
106-
mAdminComponentName, "RSA", keySpec, mIdAttestationFlags);
122+
mAdminComponentName, keyAlgorithm, keySpec, mIdAttestationFlags);
107123

108124
if (keyPair == null) {
109125
return null;

app/src/main/java/com/afwsamples/testdpc/policy/keymanagement/SignAndVerifyTask.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.os.AsyncTask;
2121
import android.security.KeyChain;
2222
import android.security.KeyChainException;
23+
import android.security.keystore.KeyProperties;
2324
import android.util.Log;
2425

2526
import com.afwsamples.testdpc.R;
@@ -46,9 +47,15 @@ public SignAndVerifyTask(Context context, ShowToastCallback callback) {
4647
protected String doInBackground(String... aliases) {
4748
String alias = aliases[0];
4849
try {
49-
final String algorithmIdentifier = "SHA256withRSA";
5050
PrivateKey privateKey = KeyChain.getPrivateKey(mContext, alias);
5151

52+
final String algorithmIdentifier;
53+
if (privateKey.getAlgorithm().equals(KeyProperties.KEY_ALGORITHM_RSA)) {
54+
algorithmIdentifier = "SHA256withRSA";
55+
} else {
56+
algorithmIdentifier = "SHA256withECDSA";
57+
}
58+
5259
byte[] data = new String("hello").getBytes();
5360
Signature signer = Signature.getInstance(algorithmIdentifier);
5461
signer.initSign(privateKey);

app/src/main/res/layout/key_generation_prompt.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
android:layout_height="wrap_content"
4848
android:text="@string/user_selectability_checkbox"/>
4949

50+
<CheckBox
51+
android:id="@+id/generate_ec_key"
52+
android:layout_width="match_parent"
53+
android:layout_height="wrap_content"
54+
android:text="@string/ec_key_checkbox"/>
55+
5056
<TextView
5157
android:layout_width="fill_parent"
5258
android:layout_height="wrap_content"

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@
529529
<string name="all_ca_certificates_removed">All CA certificates have been removed.</string>
530530
<string name="user_selectability_description">User-selectability</string>
531531
<string name="user_selectability_checkbox">Make user-selectable</string>
532+
<string name="ec_key_checkbox">Generate EC key (Default: RSA)</string>
532533
<string name="key_attestation_description">Key Attestation options</string>
533534
<string name="key_attestation_checkbox">Include key attestation challenge</string>
534535
<string name="device_id_attestation_description">Device ID attestation options</string>

0 commit comments

Comments
 (0)