Skip to content

Commit

Permalink
Throw exception when there is no keyStore facility
Browse files Browse the repository at this point in the history
  • Loading branch information
avazirna committed Jan 24, 2024
1 parent bd512d6 commit 836801f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 41 deletions.
5 changes: 4 additions & 1 deletion src/main/java/org/commcare/util/EncryptionKeyHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static EncryptionKeyAndTransformation retrieveKeyFromEncodedKey(String ba
"AES/GCM/NoPadding");
}

public static boolean isKeyStoreAvailable() {
private static boolean isKeyStoreAvailable() {
return keyStoreEncryptionKeyProvider != null &&
Security.getProvider(keyStoreEncryptionKeyProvider.getKeyStoreName()) != null;
}
Expand All @@ -78,6 +78,9 @@ public static boolean isKeyStoreAvailable() {
public static EncryptionKeyAndTransformation retrieveKeyFromKeyStore(String keyAlias,
EncryptionHelper.CryptographicOperation cryptographicOperation)
throws EncryptionKeyException {
if (!isKeyStoreAvailable()) {
throw new EncryptionKeyException("No KeyStore facility available!");
}
Key key;
try {
if (getKeyStore().containsAlias(keyAlias)) {
Expand Down
71 changes: 31 additions & 40 deletions src/main/java/org/javarosa/core/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,16 @@ public void writeExternal(DataOutputStream out) throws IOException {
}

public String getUsername() {
if (!EncryptionKeyHelper.isKeyStoreAvailable()) {
if (this.plaintextUsername != null) {
return this.plaintextUsername;
} else {
try {
return EncryptionHelper.decryptWithKeyStore(this.encryptedUsername, CC_IN_MEMORY_ENCRYPTION_KEY_ALIAS);

} catch (EncryptionKeyHelper.EncryptionKeyException e) {
throw new RuntimeException("Error encountered while retrieving key from keyStore ", e);
} catch (EncryptionHelper.EncryptionException e) {
throw new RuntimeException("Error encountered while decrypting the username ", e);
}
}

try {
return EncryptionHelper.decryptWithKeyStore(this.encryptedUsername, CC_IN_MEMORY_ENCRYPTION_KEY_ALIAS);
} catch (EncryptionKeyHelper.EncryptionKeyException e) {
throw new RuntimeException("Error encountered while retrieving key from keyStore", e);
} catch (EncryptionHelper.EncryptionException e) {
throw new RuntimeException("Error encountered while decrypting the username", e);
}
}

Expand Down Expand Up @@ -143,17 +142,13 @@ public void setUserType(String userType) {
}

public void setUsername(String username) {
if (!EncryptionKeyHelper.isKeyStoreAvailable()) {
try {
this.encryptedUsername =
EncryptionHelper.encryptWithKeyStore(username, CC_IN_MEMORY_ENCRYPTION_KEY_ALIAS);
} catch (EncryptionKeyHelper.EncryptionKeyException
| EncryptionHelper.EncryptionException e) {
e.printStackTrace();
this.plaintextUsername = username;
} else {
try {
this.encryptedUsername = EncryptionHelper.encryptWithKeyStore(username, CC_IN_MEMORY_ENCRYPTION_KEY_ALIAS);

} catch (EncryptionKeyHelper.EncryptionKeyException e) {
throw new RuntimeException("Error encountered while retrieving key from keyStore ", e);
} catch (EncryptionHelper.EncryptionException e) {
throw new RuntimeException("Error encountered while encrypting the username ", e);
}
}
}

Expand Down Expand Up @@ -218,32 +213,28 @@ public String[] getMetaDataFields() {
}

public void setCachedPwd(String password) {
if (!EncryptionKeyHelper.isKeyStoreAvailable()) {
try {
this.encryptedCachedPwd =
EncryptionHelper.encryptWithKeyStore(password, CC_IN_MEMORY_ENCRYPTION_KEY_ALIAS);

} catch (EncryptionKeyHelper.EncryptionKeyException
| EncryptionHelper.EncryptionException e) {
e.printStackTrace();
this.plaintextCachedPwd = password;
} else {
try {
this.encryptedCachedPwd = EncryptionHelper.encryptWithKeyStore(password, CC_IN_MEMORY_ENCRYPTION_KEY_ALIAS);

} catch (EncryptionKeyHelper.EncryptionKeyException e) {
throw new RuntimeException("Error encountered while retrieving key from keyStore ", e);
} catch (EncryptionHelper.EncryptionException e) {
throw new RuntimeException("Error encountered while encrypting the password ", e);
}
}
}

public String getCachedPwd() {
if (!EncryptionKeyHelper.isKeyStoreAvailable()) {
if (this.plaintextCachedPwd != null) {
return this.plaintextCachedPwd;
} else {
try {
return EncryptionHelper.decryptWithKeyStore(this.encryptedCachedPwd, CC_IN_MEMORY_ENCRYPTION_KEY_ALIAS);

} catch (EncryptionKeyHelper.EncryptionKeyException e) {
throw new RuntimeException("Error encountered while retrieving key from keyStore ", e);
} catch (EncryptionHelper.EncryptionException e) {
throw new RuntimeException("Error encountered while decrypting the username ", e);
}
}

try {
return EncryptionHelper.decryptWithKeyStore(this.encryptedCachedPwd, CC_IN_MEMORY_ENCRYPTION_KEY_ALIAS);
} catch (EncryptionKeyHelper.EncryptionKeyException e) {
throw new RuntimeException("Error encountered while retrieving key from keyStore ", e);
} catch (EncryptionHelper.EncryptionException e) {
throw new RuntimeException("Error encountered while decrypting the password ", e);
}
}

Expand Down

0 comments on commit 836801f

Please sign in to comment.