Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
avazirna committed Oct 16, 2023
1 parent eff308b commit 99e70fd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
23 changes: 12 additions & 11 deletions src/main/java/org/commcare/util/EncryptionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,27 @@
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import static org.commcare.util.CommCarePlatform.getPlatformKeyStoreName;

public class EncryptionUtils {
private static KeyStore androidKeyStore;

public static final String USER_CREDENTIALS_KEY_ALIAS = "user-credentials-key-alias";

public static final String ANDROID_KEYSTORE_PROVIDER_NAME = "AndroidKeyStore";
private static KeyStore platformKeyStore;

private enum CryptographicOperation {Encryption, Decryption}

public static KeyStore getAndroidKeyStore() {
if (androidKeyStore == null) {
public static KeyStore getPlatformKeyStore() {
if (platformKeyStore == null) {
try {
androidKeyStore = KeyStore.getInstance(ANDROID_KEYSTORE_PROVIDER_NAME);
androidKeyStore.load(null);
platformKeyStore = KeyStore.getInstance(getPlatformKeyStoreName());
platformKeyStore.load(null);
} catch (KeyStoreException | IOException | NoSuchAlgorithmException |
CertificateException e) {
throw new RuntimeException(e);
}
}
return androidKeyStore;
return platformKeyStore;
}

public static String encryptUsingKeyFromKeyStore(String message, String alias) throws EncryptionException {
Expand Down Expand Up @@ -163,8 +164,8 @@ private static String getCryptographicTransformation(String algorithm) {
}

private static Key retrieveKeyFromKeyStore(String keyAlias, CryptographicOperation operation) throws KeyStoreException, UnrecoverableEntryException, NoSuchAlgorithmException {
if (getAndroidKeyStore().containsAlias(keyAlias)) {
KeyStore.Entry keyEntry = getAndroidKeyStore().getEntry(keyAlias, null);
if (getPlatformKeyStore().containsAlias(keyAlias)) {
KeyStore.Entry keyEntry = getPlatformKeyStore().getEntry(keyAlias, null);
if (keyEntry instanceof KeyStore.PrivateKeyEntry) {
if (operation == CryptographicOperation.Encryption) {
return ((KeyStore.PrivateKeyEntry)keyEntry).getCertificate().getPublicKey();
Expand Down Expand Up @@ -235,8 +236,8 @@ private static String decrypt(String algorithm, String message, Key key) throws
}
}

public static boolean isAndroidKeyStoreSupported() {
return Security.getProvider("AndroidKeyStore") != null;
public static boolean isPlatformKeyStoreAvailable() {
return Security.getProvider(getPlatformKeyStoreName()) != null;
}

public static class EncryptionException extends Exception {
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/org/javarosa/core/model/User.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.javarosa.core.model;

import org.commcare.util.CommCarePlatform;
import org.commcare.util.EncryptionUtils;
import org.javarosa.core.model.instance.FormInstance;
import org.javarosa.core.model.instance.TreeReference;
Expand All @@ -18,7 +19,7 @@
import java.util.Hashtable;

import static org.commcare.util.EncryptionUtils.USER_CREDENTIALS_KEY_ALIAS;
import static org.commcare.util.EncryptionUtils.isAndroidKeyStoreSupported;
import static org.commcare.util.EncryptionUtils.isPlatformKeyStoreAvailable;

/**
* Peristable object representing a CommCare mobile user.
Expand Down Expand Up @@ -92,7 +93,7 @@ public void writeExternal(DataOutputStream out) throws IOException {
}

public String getUsername() {
if (!isAndroidKeyStoreSupported()) {
if (!isPlatformKeyStoreAvailable()) {
return this.username;
} else {
try {
Expand Down Expand Up @@ -130,7 +131,7 @@ public void setUserType(String userType) {
}

public void setUsername(String username) {
if (!isAndroidKeyStoreSupported()) {
if (!isPlatformKeyStoreAvailable()) {
this.username = username;
} else {
try {
Expand Down Expand Up @@ -204,7 +205,7 @@ public String[] getMetaDataFields() {
//Don't ever save!
private String cachedPwd;
public void setCachedPwd(String password) {
if (!isAndroidKeyStoreSupported()) {
if (!isPlatformKeyStoreAvailable()) {
this.cachedPwd = password;
} else {
try {
Expand All @@ -216,7 +217,7 @@ public void setCachedPwd(String password) {
}

public String getCachedPwd() {
if (!isAndroidKeyStoreSupported()) {
if (!isPlatformKeyStoreAvailable()) {
return this.cachedPwd;
} else {
try {
Expand Down

0 comments on commit 99e70fd

Please sign in to comment.