Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
avazirna committed Dec 7, 2023
1 parent 6c5caf0 commit efe1908
Showing 7 changed files with 49 additions and 41 deletions.
39 changes: 19 additions & 20 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -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 {
24 changes: 18 additions & 6 deletions app/src/org/commcare/utils/EncryptionKeyProvider.java
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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<KeyGenParameterSpec>()
val mockKeyGenParameterSpec = mockk<KeyGenParameterSpec>()
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()
}
Original file line number Diff line number Diff line change
@@ -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());
}

3 changes: 1 addition & 2 deletions app/unit-tests/src/org/commcare/utils/MockKeyGenerator.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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()));
}
7 changes: 3 additions & 4 deletions app/unit-tests/src/org/commcare/utils/MockKeyStore.java
Original file line number Diff line number Diff line change
@@ -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,16 +17,16 @@

public class MockKeyStore extends KeyStoreSpi {

private static HashMap<String, Key> keys = new HashMap<>();
private static HashMap<String, Certificate> certs = new HashMap<>();
private static final HashMap<String, Key> keys = new HashMap<>();
private static final HashMap<String, Certificate> certs = new HashMap<>();

@Override
public void engineSetKeyEntry(String alias, Key key, char[] password, Certificate[] chain) {
keys.put(alias, key);
}

@Override
public void engineDeleteEntry(String alias) throws KeyStoreException {
public void engineDeleteEntry(String alias) {
keys.remove(alias);
certs.remove(alias);
}

0 comments on commit efe1908

Please sign in to comment.