From efe19088809f88c0ae04c32ed1874b088435ecfc Mon Sep 17 00:00:00 2001 From: Ahmad Vazirna Date: Thu, 7 Dec 2023 10:56:49 +0200 Subject: [PATCH] Lint --- app/build.gradle | 39 +++++++++---------- .../commcare/utils/EncryptionKeyProvider.java | 24 +++++++++--- .../utils/EncryptCredentialsInMemoryTest.kt | 8 ++-- .../utils/MockAndroidKeyStoreProvider.java | 3 +- .../org/commcare/utils/MockKeyGenerator.java | 3 +- .../commcare/utils/MockKeyGeneratorSpi.java | 6 +-- .../src/org/commcare/utils/MockKeyStore.java | 7 ++-- 7 files changed, 49 insertions(+), 41 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 1208a97204..a16098cc00 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -136,35 +136,34 @@ dependencies { } ext { - // Obtained from ~/.gradle/gradle.properties on build server (mobile agent), or your local // Obtained from ~/.gradle/gradle.properties on build server (mobile agent), or your local // ~/.gradle/gradle.properties file, or loads default empty strings if neither is present - MAPBOX_SDK_API_KEY = project.properties['MAPBOX_SDK_API_KEY'] ?: "" - ANALYTICS_TRACKING_ID_DEV = project.properties['ANALYTICS_TRACKING_ID_DEV'] ?: "" - ANALYTICS_TRACKING_ID_LIVE = project.properties['ANALYTICS_TRACKING_ID_LIVE'] ?: "" - GOOGLE_PLAY_MAPS_API_KEY = project.properties['GOOGLE_PLAY_MAPS_API_KEY'] ?: "" - RELEASE_STORE_FILE = project.properties['RELEASE_STORE_FILE'] ?: "." - RELEASE_STORE_PASSWORD = project.properties['RELEASE_STORE_PASSWORD'] ?: "" - RELEASE_KEY_ALIAS = project.properties['RELEASE_KEY_ALIAS'] ?: "" - RELEASE_KEY_PASSWORD = project.properties['RELEASE_KEY_PASSWORD'] ?: "" + MAPBOX_SDK_API_KEY = project.properties['MAPBOX_SDK_API_KEY'] ?: '' + ANALYTICS_TRACKING_ID_DEV = project.properties['ANALYTICS_TRACKING_ID_DEV'] ?: '' + ANALYTICS_TRACKING_ID_LIVE = project.properties['ANALYTICS_TRACKING_ID_LIVE'] ?: '' + GOOGLE_PLAY_MAPS_API_KEY = project.properties['GOOGLE_PLAY_MAPS_API_KEY'] ?: '' + RELEASE_STORE_FILE = project.properties['RELEASE_STORE_FILE'] ?: '.' + RELEASE_STORE_PASSWORD = project.properties['RELEASE_STORE_PASSWORD'] ?: '' + RELEASE_KEY_ALIAS = project.properties['RELEASE_KEY_ALIAS'] ?: '' + RELEASE_KEY_PASSWORD = project.properties['RELEASE_KEY_PASSWORD'] ?: '' TRUSTED_SOURCE_PUBLIC_KEY = project.properties['TRUSTED_SOURCE_PUBLIC_KEY'] ?: "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHiuy2ULV4pobkuQN2TEjmR1tn" + "HJ+F335hm/lVdaFQzvBmeq64MUMbumheVLDJaSUiAVzqSHDKJWH01ZQRowqBYjwo" + "ycVSQSeO2glc6XZZ+CJudAPXe8iFWLQp3kBBnBmVcBXCOQFO7aLgQMv4nqKZsLW0" + "HaAJkjpnc165Os+aYwIDAQAB" - GOOGLE_SERVICES_API_KEY = project.properties['GOOGLE_SERVICES_API_KEY'] ?: "" - QA_BETA_APP_ID = "" - STANDALONE_APP_ID = "" - LTS_APP_ID = "" - COMMCARE_APP_ID = "" - HQ_API_USERNAME = project.properties['HQ_API_USERNAME'] ?: "" - HQ_API_PASSWORD = project.properties['HQ_API_PASSWORD'] ?: "" - TEST_BUILD_TYPE = project.properties['TEST_BUILD_TYPE'] ?: "debug" - FIREBASE_DATABASE_URL = project.properties['FIREBASE_DATABASE_URL'] ?: "" + GOOGLE_SERVICES_API_KEY = project.properties['GOOGLE_SERVICES_API_KEY'] ?: '' + QA_BETA_APP_ID = '' + STANDALONE_APP_ID = '' + LTS_APP_ID = '' + COMMCARE_APP_ID = '' + HQ_API_USERNAME = project.properties['HQ_API_USERNAME'] ?: '' + HQ_API_PASSWORD = project.properties['HQ_API_PASSWORD'] ?: '' + TEST_BUILD_TYPE = project.properties['TEST_BUILD_TYPE'] ?: 'debug' + FIREBASE_DATABASE_URL = project.properties['FIREBASE_DATABASE_URL'] ?: '' // properties related to Service providers - SERVICE_PROVIDERS = ["org.commcare.util.IEncryptionKeyProvider" : "org.commcare.utils.EncryptionKeyProvider"] - SERVICE_PROVIDERS_REL_DIR = "META-INF/services" + SERVICE_PROVIDERS = ['org.commcare.util.IEncryptionKeyProvider' : 'org.commcare.utils.EncryptionKeyProvider'] + SERVICE_PROVIDERS_REL_DIR = 'META-INF/services' } afterEvaluate { diff --git a/app/src/org/commcare/utils/EncryptionKeyProvider.java b/app/src/org/commcare/utils/EncryptionKeyProvider.java index 7a2e8e578e..6309ce7119 100644 --- a/app/src/org/commcare/utils/EncryptionKeyProvider.java +++ b/app/src/org/commcare/utils/EncryptionKeyProvider.java @@ -27,12 +27,22 @@ import javax.crypto.KeyGenerator; import javax.security.auth.x500.X500Principal; +import androidx.annotation.RequiresApi; + import static org.commcare.utils.GlobalConstants.KEYSTORE_NAME; +/** + * Class for providing encryption keys backed by Android Keystore + * + * @author dviggiano + */ public class EncryptionKeyProvider implements IEncryptionKeyProvider { + @RequiresApi(api = Build.VERSION_CODES.M) + private static final String ALGORITHM = KeyProperties.KEY_ALGORITHM_AES; + @RequiresApi(api = Build.VERSION_CODES.M) private static final String BLOCK_MODE = KeyProperties.BLOCK_MODE_GCM; - + @RequiresApi(api = Build.VERSION_CODES.M) private static final String PADDING = KeyProperties.ENCRYPTION_PADDING_NONE; private static KeyStore keystoreSingleton = null; @@ -48,7 +58,8 @@ private static KeyStore getKeyStore() throws KeyStoreException, CertificateExcep @Override public EncryptionKeyAndTransformation retrieveKeyFromKeyStore(String keyAlias, EncryptionUtils.CryptographicOperation operation) - throws KeyStoreException, UnrecoverableEntryException, NoSuchAlgorithmException, CertificateException, IOException { + throws KeyStoreException, UnrecoverableEntryException, NoSuchAlgorithmException, + CertificateException, IOException { Key key; if (getKeyStore().containsAlias(keyAlias)) { KeyStore.Entry keyEntry = getKeyStore().getEntry(keyAlias, null); @@ -64,10 +75,11 @@ public EncryptionKeyAndTransformation retrieveKeyFromKeyStore(String keyAlias, } else { throw new KeyStoreException("Key not found in KeyStore"); } - if (key != null) + if (key != null) { return new EncryptionKeyAndTransformation(key, getTransformationString(key.getAlgorithm())); - else + } else { return null; + } } // Generates a cryptrographic key and adds it to the Android KeyStore @@ -128,12 +140,12 @@ public boolean isKeyStoreAvailable() { @Override public String getAESKeyAlgorithmRepresentation() { - return KeyProperties.KEY_ALGORITHM_AES; + return ALGORITHM; } @Override public String getRSAKeyAlgorithmRepresentation() { - return KeyProperties.KEY_ALGORITHM_RSA; + return "RSA"; } @Override diff --git a/app/unit-tests/src/org/commcare/utils/EncryptCredentialsInMemoryTest.kt b/app/unit-tests/src/org/commcare/utils/EncryptCredentialsInMemoryTest.kt index a4c291c349..46f67bebd0 100644 --- a/app/unit-tests/src/org/commcare/utils/EncryptCredentialsInMemoryTest.kt +++ b/app/unit-tests/src/org/commcare/utils/EncryptCredentialsInMemoryTest.kt @@ -1,4 +1,4 @@ -package org.commcare.utils; +package org.commcare.utils import android.security.keystore.KeyGenParameterSpec import androidx.test.ext.junit.runners.AndroidJUnit4 @@ -59,7 +59,7 @@ public class EncryptCredentialsInMemoryTest { User::class.java, CommCareApplication.instance().userDbHandle ).write(user) - var username = user.username + val username = user.username // close the user session CommCareApplication.instance().closeUserSession() @@ -86,11 +86,11 @@ public class EncryptCredentialsInMemoryTest { } private fun generateUserCredentialKey() { - var mockKeyGenParameterSpec = mockk() + val mockKeyGenParameterSpec = mockk() every { mockKeyGenParameterSpec.keystoreAlias } returns EncryptionUtils.USER_CREDENTIALS_KEY_ALIAS // generate key using mock key generator - var mockKeyGenerator = MockKeyGenerator() + val mockKeyGenerator = MockKeyGenerator() mockKeyGenerator.init(mockKeyGenParameterSpec) mockKeyGenerator.generateKey() } diff --git a/app/unit-tests/src/org/commcare/utils/MockAndroidKeyStoreProvider.java b/app/unit-tests/src/org/commcare/utils/MockAndroidKeyStoreProvider.java index 6a2adb97f2..8478e3aa4e 100644 --- a/app/unit-tests/src/org/commcare/utils/MockAndroidKeyStoreProvider.java +++ b/app/unit-tests/src/org/commcare/utils/MockAndroidKeyStoreProvider.java @@ -1,6 +1,5 @@ package org.commcare.utils; -import java.security.NoSuchAlgorithmException; import java.security.Provider; import java.security.Security; @@ -14,7 +13,7 @@ protected MockAndroidKeyStoreProvider() { super(GlobalConstants.KEYSTORE_NAME, 1.0, "Mock AndroidKeyStore provider"); } - public static void registerProvider() throws NoSuchAlgorithmException { + public static void registerProvider() { Security.addProvider(new MockAndroidKeyStoreProvider()); } diff --git a/app/unit-tests/src/org/commcare/utils/MockKeyGenerator.java b/app/unit-tests/src/org/commcare/utils/MockKeyGenerator.java index 47ee908a74..4322c8fc76 100644 --- a/app/unit-tests/src/org/commcare/utils/MockKeyGenerator.java +++ b/app/unit-tests/src/org/commcare/utils/MockKeyGenerator.java @@ -1,13 +1,12 @@ package org.commcare.utils; -import java.security.NoSuchAlgorithmException; import java.security.Security; import javax.crypto.KeyGenerator; public class MockKeyGenerator extends KeyGenerator { - public MockKeyGenerator() throws NoSuchAlgorithmException { + public MockKeyGenerator() { super(new MockKeyGeneratorSpi() , Security.getProvider(GlobalConstants.KEYSTORE_NAME), "AES"); } } diff --git a/app/unit-tests/src/org/commcare/utils/MockKeyGeneratorSpi.java b/app/unit-tests/src/org/commcare/utils/MockKeyGeneratorSpi.java index 5d16b991e3..224bf53cce 100644 --- a/app/unit-tests/src/org/commcare/utils/MockKeyGeneratorSpi.java +++ b/app/unit-tests/src/org/commcare/utils/MockKeyGeneratorSpi.java @@ -17,8 +17,8 @@ import javax.crypto.SecretKey; public class MockKeyGeneratorSpi extends KeyGeneratorSpi { - private KeyGenerator wrappedKeyGenerator; - private KeyStore keyStore; + private final KeyGenerator wrappedKeyGenerator; + private final KeyStore keyStore; private KeyGenParameterSpec spec = null; { @@ -35,7 +35,7 @@ public class MockKeyGeneratorSpi extends KeyGeneratorSpi { @Override protected void engineInit(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException { - if (params == null || !(params instanceof KeyGenParameterSpec)) { + if (!(params instanceof KeyGenParameterSpec)) { throw new InvalidAlgorithmParameterException( String.format("Cannot initialize without a %s parameter", KeyGenParameterSpec.class.getName())); } diff --git a/app/unit-tests/src/org/commcare/utils/MockKeyStore.java b/app/unit-tests/src/org/commcare/utils/MockKeyStore.java index 8aa247417f..cc54157796 100644 --- a/app/unit-tests/src/org/commcare/utils/MockKeyStore.java +++ b/app/unit-tests/src/org/commcare/utils/MockKeyStore.java @@ -4,7 +4,6 @@ import java.io.OutputStream; import java.security.Key; import java.security.KeyStore; -import java.security.KeyStoreException; import java.security.KeyStoreSpi; import java.security.PrivateKey; import java.security.cert.Certificate; @@ -18,8 +17,8 @@ public class MockKeyStore extends KeyStoreSpi { - private static HashMap keys = new HashMap<>(); - private static HashMap certs = new HashMap<>(); + private static final HashMap keys = new HashMap<>(); + private static final HashMap certs = new HashMap<>(); @Override public void engineSetKeyEntry(String alias, Key key, char[] password, Certificate[] chain) { @@ -27,7 +26,7 @@ public void engineSetKeyEntry(String alias, Key key, char[] password, Certificat } @Override - public void engineDeleteEntry(String alias) throws KeyStoreException { + public void engineDeleteEntry(String alias) { keys.remove(alias); certs.remove(alias); }