Skip to content

Commit

Permalink
Add flag about use of keystore for encryption keys
Browse files Browse the repository at this point in the history
  • Loading branch information
avazirna committed Jan 12, 2024
1 parent 4caf14b commit d26e90f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
20 changes: 9 additions & 11 deletions src/main/java/org/commcare/util/EncryptionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ public class EncryptionHelper {

public enum CryptographicOperation {Encryption, Decryption}

private IEncryptionKeyProvider encryptionKeyProvider = EncryptionKeyServiceProvider.getInstance().serviceImpl();
private IEncryptionKeyProvider encryptionKeyProvider;
private boolean useKeyStoreIfAvailable;

public EncryptionHelper(boolean useKeyStoreIfAvailable) {
encryptionKeyProvider = EncryptionKeyServiceProvider.getInstance().serviceImpl(useKeyStoreIfAvailable);
this.useKeyStoreIfAvailable = useKeyStoreIfAvailable;
}

public IEncryptionKeyProvider getEncryptionKeyProvider() {
return encryptionKeyProvider;
Expand All @@ -33,18 +39,10 @@ public void setEncryptionKeyProvider(IEncryptionKeyProvider newEncryptionKeyProv
encryptionKeyProvider = newEncryptionKeyProvider;
}

public void reloadEncryptionKeyProvider() {
encryptionKeyProvider = EncryptionKeyServiceProvider.getInstance().serviceImpl();
public void reloadDefaultEncryptionKeyProvider() {
encryptionKeyProvider = EncryptionKeyServiceProvider.getInstance().serviceImpl(useKeyStoreIfAvailable);
}









/**
* Encrypts a message using the AES or RAS algorithms and produces a base64 encoded payload
* containing the ciphertext, and when applicable, a random IV which was used to encrypt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ public static EncryptionKeyServiceProvider getInstance() {
return serviceProvider;
}

public IEncryptionKeyProvider serviceImpl() {
public IEncryptionKeyProvider serviceImpl(boolean useKeyStoreIfAvailable) {
IEncryptionKeyProvider service = null;
if (loader.iterator().hasNext()) {
service = loader.iterator().next();
if (service.isKeyStoreAvailable() && useKeyStoreIfAvailable) {
return service;
}
}

// In case the preference is not available, this will default to the last provider, if any
if (service != null) {
return service;
} else {
throw new NoSuchElementException(
"No implementation for IEncryptionKeyProvider");
throw new NoSuchElementException("No implementation for IEncryptionKeyProvider");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/javarosa/core/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class User implements Persistable, Restorable, IMetaData {
private byte[] wrappedKey;

public Hashtable<String, String> properties = new Hashtable<>();
private EncryptionHelper encryptionHelper = new EncryptionHelper();
private EncryptionHelper encryptionHelper = new EncryptionHelper(true);

// plaintextCachedPwd and encryptedCachedPwd are used to store the password in memory, should
// not to be persisted. For aspects related to persisting the password, refer to passwordHash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class XPathDecryptStringFunc extends XPathFuncExpr {

public static final String NAME = "decrypt-string";
private static final int EXPECTED_ARG_COUNT = 3;
private EncryptionHelper encryptionHelper = new EncryptionHelper();
private EncryptionHelper encryptionHelper = new EncryptionHelper(false);

public XPathDecryptStringFunc() {
name = NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class XPathEncryptStringFunc extends XPathFuncExpr {
public static final String NAME = "encrypt-string";
private static final int EXPECTED_ARG_COUNT = 3;
private EncryptionHelper encryptionHelper = new EncryptionHelper();
private EncryptionHelper encryptionHelper = new EncryptionHelper(false);

public XPathEncryptStringFunc() {
name = NAME;
Expand Down

0 comments on commit d26e90f

Please sign in to comment.