preference updates. fix KeyStore ops for Android 9++.

This commit is contained in:
Akinwale Ariwodola 2019-10-15 10:24:55 +01:00
parent c711fa070a
commit fa2cc27085
2 changed files with 44 additions and 16 deletions

2
app

@ -1 +1 @@
Subproject commit dcbb7f62d6e8b857781acf7e15a25bcd0b883cc0
Subproject commit 194e29356f24527f7d17c6adbaf24c0165b54a67

View file

@ -2,6 +2,7 @@ package io.lbry.browser;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.security.KeyPairGeneratorSpec;
import android.util.Base64;
import android.util.Log;
@ -25,6 +26,8 @@ import java.security.Key;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
@ -153,6 +156,7 @@ public final class Utils {
try {
SharedPreferences pref = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
String encryptedValue = pref.getString(key, null);
if (encryptedValue == null || encryptedValue.trim().length() == 0) {
return null;
}
@ -356,11 +360,24 @@ public final class Utils {
}
private static byte[] rsaEncrypt(byte[] secret, KeyStore keyStore) throws Exception {
PrivateKey privateKey = null;
PublicKey publicKey = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
privateKey = (PrivateKey) keyStore.getKey(KEY_ALIAS, null);
publicKey = keyStore.getCertificate(KEY_ALIAS).getPublicKey();
} else {
KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(KEY_ALIAS, null);
privateKey = privateKeyEntry.getPrivateKey();
publicKey = privateKeyEntry.getCertificate().getPublicKey();
}
if (publicKey == null) {
throw new Exception("Could not obtain public key for encryption.");
}
// Encrypt the text
Cipher inputCipher = Cipher.getInstance(RSA_MODE);
inputCipher.init(Cipher.ENCRYPT_MODE, privateKeyEntry.getCertificate().getPublicKey());
inputCipher.init(Cipher.ENCRYPT_MODE, publicKey);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, inputCipher);
@ -371,9 +388,20 @@ public final class Utils {
}
private static byte[] rsaDecrypt(byte[] encrypted, KeyStore keyStore) throws Exception {
PrivateKey privateKey = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
privateKey = (PrivateKey) keyStore.getKey(KEY_ALIAS, null);
} else {
KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(KEY_ALIAS, null);
privateKey = privateKeyEntry.getPrivateKey();
}
if (privateKey == null) {
throw new Exception("Could not obtain private key for decryption");
}
Cipher output = Cipher.getInstance(RSA_MODE);
output.init(Cipher.DECRYPT_MODE, privateKeyEntry.getPrivateKey());
output.init(Cipher.DECRYPT_MODE, privateKey);
CipherInputStream cipherInputStream = new CipherInputStream(new ByteArrayInputStream(encrypted), output);
ArrayList<Byte> values = new ArrayList<Byte>();
int nextByte;