Skip to content

Commit

Permalink
Added static helper class to lazy load KeyStore singleton.
Browse files Browse the repository at this point in the history
  • Loading branch information
OrangeAndGreen committed Dec 4, 2024
1 parent b2aa121 commit ea2db93
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import okhttp3.MediaType;
import okhttp3.RequestBody;
Expand All @@ -57,9 +52,9 @@ public class ConnectNetworkHelper {
public static class PostResult {
public final int responseCode;
public final InputStream responseStream;
public final IOException e;
public final Exception e;

public PostResult(int responseCode, InputStream responseStream, IOException e) {
public PostResult(int responseCode, InputStream responseStream, Exception e) {
this.responseCode = responseCode;
this.responseStream = responseStream;
this.e = e;
Expand Down
31 changes: 20 additions & 11 deletions app/src/org/commcare/utils/EncryptionKeyProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;

import org.javarosa.core.services.Logger;

import androidx.annotation.RequiresApi;

import java.io.IOException;
Expand Down Expand Up @@ -42,16 +44,24 @@ public class EncryptionKeyProvider {
private static final String BLOCK_MODE = KeyProperties.BLOCK_MODE_CBC;
@RequiresApi(api = Build.VERSION_CODES.M)
private static final String PADDING = KeyProperties.ENCRYPTION_PADDING_PKCS7;
private static KeyStore keystoreSingleton = null;

private static KeyStore getKeystore() throws KeyStoreException, CertificateException,
IOException, NoSuchAlgorithmException {
if (keystoreSingleton == null) {
keystoreSingleton = KeyStore.getInstance(KEYSTORE_NAME);
keystoreSingleton.load(null);
private static class KeyStoreLoader {
static final KeyStore INSTANCE;

static {
try {
INSTANCE = KeyStore.getInstance(KEYSTORE_NAME);
INSTANCE.load(null);
} catch (KeyStoreException | CertificateException | IOException |
NoSuchAlgorithmException e) {
Logger.exception("Initiating KeyStore", e);
throw new RuntimeException(e);
}
}
}

return keystoreSingleton;
private static KeyStore getKeystore() {
return KeyStoreLoader.INSTANCE;
}

public EncryptionKeyAndTransform getKey(Context context, boolean trueForEncrypt)
Expand All @@ -62,8 +72,8 @@ public EncryptionKeyAndTransform getKey(Context context, boolean trueForEncrypt)

//Gets the SecretKey from the Android KeyStore (creates a new one the first time)
private static EncryptionKeyAndTransform getKey(Context context, KeyStore keystore, boolean trueForEncrypt)
throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException,
UnrecoverableEntryException, InvalidAlgorithmParameterException, NoSuchProviderException {
throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableEntryException,
InvalidAlgorithmParameterException, NoSuchProviderException {

if (doesKeystoreContainEncryptionKey()) {
KeyStore.Entry existingKey = keystore.getEntry(SECRET_NAME, null);
Expand All @@ -81,8 +91,7 @@ private static EncryptionKeyAndTransform getKey(Context context, KeyStore keysto
}
}

private static boolean doesKeystoreContainEncryptionKey() throws CertificateException,
KeyStoreException, IOException, NoSuchAlgorithmException {
private static boolean doesKeystoreContainEncryptionKey() throws KeyStoreException {
KeyStore keystore = getKeystore();

return keystore.containsAlias(SECRET_NAME);
Expand Down

0 comments on commit ea2db93

Please sign in to comment.