|
7 | 7 | import android.security.keystore.KeyGenParameterSpec; |
8 | 8 | import android.security.keystore.KeyProperties; |
9 | 9 | import android.support.annotation.NonNull; |
| 10 | +import android.text.TextUtils; |
10 | 11 |
|
11 | 12 | import java.io.File; |
12 | 13 | import java.io.FileInputStream; |
@@ -187,7 +188,7 @@ public SecretKey getSymmetricKey(@NonNull String alias, char[] password) { |
187 | 188 | if (Utils.lowerThenMarshmallow()) { |
188 | 189 | result = getSymmetricKeyFromDefaultKeyStore(alias, password); |
189 | 190 | } else { |
190 | | - result = getSymmetricKeyFromAndroidtKeyStore(alias); |
| 191 | + result = getSymmetricKeyFromAndroidKeyStore(alias, password); |
191 | 192 | } |
192 | 193 | return result; |
193 | 194 | } |
@@ -272,8 +273,8 @@ private KeyPair generateDefaultAsymmetricKey(KeyProps keyProps) { |
272 | 273 | try { |
273 | 274 | KeyPair keyPair = createAsymmetricKey(keyProps); |
274 | 275 | PrivateKey key = keyPair.getPrivate(); |
275 | | - X509Certificate certificate = keyToCertificateReflection(keyPair, keyProps); |
276 | 276 | KeyStore keyStore = createDefaultKeyStore(); |
| 277 | + X509Certificate certificate = keyToCertificateReflection(keyPair, keyProps); |
277 | 278 |
|
278 | 279 | keyStore.setKeyEntry(keyProps.mAlias, key, keyProps.mPassword, new Certificate[]{certificate}); |
279 | 280 | keyStore.store(new FileOutputStream(mKeystoreFile), mKeystorePassword); |
@@ -428,6 +429,8 @@ private KeyPairGeneratorSpec keyPropsToKeyPairGeneratorSpec(KeyProps keyProps) t |
428 | 429 | .setStartDate(keyProps.mStartDate) |
429 | 430 | .setEndDate(keyProps.mEndDate); |
430 | 431 |
|
| 432 | + if (!TextUtils.isEmpty(new String(keyProps.mPassword))) |
| 433 | + builder.setEncryptionRequired(); |
431 | 434 | if (Utils.biggerThenJellyBean()) { |
432 | 435 | builder.setKeySize(keyProps.mKeySize); |
433 | 436 | } |
@@ -508,11 +511,11 @@ private SecretKey getSymmetricKeyFromDefaultKeyStore(@NonNull String alias, char |
508 | 511 | return result; |
509 | 512 | } |
510 | 513 |
|
511 | | - private SecretKey getSymmetricKeyFromAndroidtKeyStore(@NonNull String alias) { |
| 514 | + private SecretKey getSymmetricKeyFromAndroidKeyStore(@NonNull String alias, char[] password) { |
512 | 515 | SecretKey result = null; |
513 | 516 | try { |
514 | 517 | KeyStore keyStore = createAndroidKeystore(); |
515 | | - result = (SecretKey) keyStore.getKey(alias, null); |
| 518 | + result = (SecretKey) keyStore.getKey(alias, password); |
516 | 519 | } catch (KeyStoreException | CertificateException | IOException | NoSuchAlgorithmException | UnrecoverableEntryException e) { |
517 | 520 | onException(e); |
518 | 521 | } |
|
0 commit comments