From 836801fb1c1739521da38d0a3f49a22c8655bc1e Mon Sep 17 00:00:00 2001 From: Ahmad Vazirna Date: Wed, 24 Jan 2024 23:59:00 +0200 Subject: [PATCH] Throw exception when there is no keyStore facility --- .../commcare/util/EncryptionKeyHelper.java | 5 +- .../java/org/javarosa/core/model/User.java | 71 ++++++++----------- 2 files changed, 35 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/commcare/util/EncryptionKeyHelper.java b/src/main/java/org/commcare/util/EncryptionKeyHelper.java index 23a55582e..829ab1ca1 100644 --- a/src/main/java/org/commcare/util/EncryptionKeyHelper.java +++ b/src/main/java/org/commcare/util/EncryptionKeyHelper.java @@ -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; } @@ -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)) { diff --git a/src/main/java/org/javarosa/core/model/User.java b/src/main/java/org/javarosa/core/model/User.java index 720b29b1a..4b6581189 100644 --- a/src/main/java/org/javarosa/core/model/User.java +++ b/src/main/java/org/javarosa/core/model/User.java @@ -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); } } @@ -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); - } } } @@ -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); } }