Skip to content

Commit 16a7d68

Browse files
Add documentation
1 parent 2eb93b7 commit 16a7d68

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
<overview>
7+
<p>
8+
Biometric authentication such as fingerprint recognition can be used alongside cryptographic keys stored in the Android <code>KeyStore</code> to protect sensitive parts of the application. However,
9+
when a key generated for this purpose has certain parameters set insecurely, it can allow an attacker with physical access to bypass the
10+
authentication check, using application hooking tools such as Frida.
11+
</p>
12+
</overview>
13+
14+
<recommendation>
15+
<p>
16+
When generating a key for use with biometric authentication, ensure that the following parameters of <code>KeyGenParameterSpec.Builder</code> are set:
17+
</p>
18+
<ul>
19+
<li><code>setUserAuthenticationRequired</code> should be set to <code>true</code>; otherwise the key can be used without user authentication.</li>
20+
<li><code>setInvalidatedByBiometricEnrollment</code> should be set to <code>true</code> (the default); otherwise an attacker can use the key by enrolling additional biometrics on the device.</li>
21+
<li><code>setUserAuthenticationValidityDurationSeconds</code>, if used, should be set to <code>-1</code>; otherwise non-biometric (less secure) credentials can be used to access the key. <code>setUserAuthenticationParameters</code> is instead recommended to explicitly set both the timeout and the types of credentials that may be used.</li>
22+
</ul>
23+
24+
</recommendation>
25+
26+
<example>
27+
<p>The following example demonstrates a key that is configured with secure paramaters:</p>
28+
<sample src="AndroidInsecureKeysGood.java"/>
29+
</example>
30+
31+
<references>
32+
<li>
33+
WithSecure: <a href="https://labs.withsecure.com/publications/how-secure-is-your-android-keystore-authentication">How Secure is your Android Keystore Authentication?</a>
34+
</li>
35+
<li>
36+
Android Developers: <a href="https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec.Builder">KeyGenParameterSpec.Builder</a>
37+
</li>
38+
39+
</references>
40+
</qhelp>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
// GOOD: Secure parameters are used to generate a key for biometric authentication.
8+
.setUserAuthenticationRequired(true)
9+
.setInvalidatedByBiometricEnrollment(true)
10+
.setUserAuthenticationParamters(0, KeyProperties.AUTH_BIOMETRIC_STRONG)
11+
.build();
12+
KeyGenerator keyGenerator = KeyGenerator.getInstance(
13+
KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
14+
keyGenerator.init(keyGenParameterSpec);
15+
keyGenerator.generateKey();
16+
}

0 commit comments

Comments
 (0)