Skip to content

Commit 9ad05fe

Browse files
Address reveiws - Add BAD example to doc, add doc example to tests and fix typo.
1 parent 3a4a841 commit 9ad05fe

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

java/ql/src/Security/CWE/CWE-287/AndroidInsecureKeys.qhelp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ When generating a key for use with biometric authentication, ensure that the fol
2626
<example>
2727
<p>The following example demonstrates a key that is configured with secure paramaters:</p>
2828
<sample src="AndroidInsecureKeysGood.java"/>
29+
30+
<p>In each of the following cases, a parameter is set insecurely:</p>
31+
<sample src="AndroidInsecureKeysBad.java"/>
2932
</example>
3033

3134
<references>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
private void generateSecretKey() {
2+
KeyGenParameterSpec keyGenParameterSpec = new KeyGenParameterSpec.Builder(
3+
"MySecretKey",
4+
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
5+
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
6+
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
7+
// BAD: User authentication is not required to use this key.
8+
.setUserAuthenticationRequired(false)
9+
.build();
10+
KeyGenerator keyGenerator = KeyGenerator.getInstance(
11+
KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
12+
keyGenerator.init(keyGenParameterSpec);
13+
keyGenerator.generateKey();
14+
}
15+
16+
private void generateSecretKey() {
17+
KeyGenParameterSpec keyGenParameterSpec = new KeyGenParameterSpec.Builder(
18+
"MySecretKey",
19+
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
20+
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
21+
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
22+
.setUserAuthenticationRequired(true)
23+
// BAD: An attacker can access this key by enrolling additional biometrics.
24+
.setInvalidatedByBiometricEnrollment(false)
25+
.build();
26+
KeyGenerator keyGenerator = KeyGenerator.getInstance(
27+
KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
28+
keyGenerator.init(keyGenParameterSpec);
29+
keyGenerator.generateKey();
30+
}
31+
32+
private void generateSecretKey() {
33+
KeyGenParameterSpec keyGenParameterSpec = new KeyGenParameterSpec.Builder(
34+
"MySecretKey",
35+
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
36+
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
37+
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
38+
.setUserAuthenticationRequired(true)
39+
.setInvalidatedByBiometricEnrollment(true)
40+
// BAD: This key can be accessed using non-biometric credentials.
41+
.setUserAuthenticationValidityDurationSeconds(30)
42+
.build();
43+
KeyGenerator keyGenerator = KeyGenerator.getInstance(
44+
KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
45+
keyGenerator.init(keyGenParameterSpec);
46+
keyGenerator.generateKey();
47+
}

java/ql/src/Security/CWE/CWE-287/AndroidInsecureKeysGood.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ private void generateSecretKey() {
77
// GOOD: Secure parameters are used to generate a key for biometric authentication.
88
.setUserAuthenticationRequired(true)
99
.setInvalidatedByBiometricEnrollment(true)
10-
.setUserAuthenticationParamters(0, KeyProperties.AUTH_BIOMETRIC_STRONG)
10+
.setUserAuthenticationParameters(0, KeyProperties.AUTH_BIOMETRIC_STRONG)
1111
.build();
1212
KeyGenerator keyGenerator = KeyGenerator.getInstance(
1313
KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");

java/ql/test/query-tests/security/CWE-287/InsecureKeys/Test1/Test.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import android.security.keystore.KeyGenParameterSpec;
22
import android.hardware.biometrics.BiometricPrompt;
33
import android.security.keystore.KeyProperties;
4+
import javax.crypto.KeyGenerator;
45

56
class Test {
67
void test() {
@@ -9,6 +10,23 @@ void test() {
910
builder.setInvalidatedByBiometricEnrollment(false); // $insecure-key
1011
builder.setUserAuthenticationValidityDurationSeconds(30); // $insecure-key
1112
}
13+
14+
private void generateSecretKey() throws Exception {
15+
KeyGenParameterSpec keyGenParameterSpec = new KeyGenParameterSpec.Builder(
16+
"MySecretKey",
17+
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
18+
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
19+
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
20+
// GOOD: Secure parameters are used to generate a key for biometric authentication.
21+
.setUserAuthenticationRequired(true)
22+
.setInvalidatedByBiometricEnrollment(true)
23+
.setUserAuthenticationParameters(0, KeyProperties.AUTH_BIOMETRIC_STRONG)
24+
.build();
25+
KeyGenerator keyGenerator = KeyGenerator.getInstance(
26+
KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
27+
keyGenerator.init(keyGenParameterSpec);
28+
keyGenerator.generateKey();
29+
}
1230
}
1331

1432
class Callback extends BiometricPrompt.AuthenticationCallback {

0 commit comments

Comments
 (0)