diff --git a/app/src/org/commcare/models/encryption/CipherPool.java b/app/src/org/commcare/models/encryption/CipherPool.java deleted file mode 100755 index 2283bb2574..0000000000 --- a/app/src/org/commcare/models/encryption/CipherPool.java +++ /dev/null @@ -1,59 +0,0 @@ -package org.commcare.models.encryption; - -import android.util.Log; - -import java.util.HashSet; -import java.util.Stack; - -import javax.crypto.Cipher; - -/** - * @author ctsims - */ -public abstract class CipherPool { - private static final String TAG = CipherPool.class.getSimpleName(); - - private static final int GROWTH_FACTOR = 5; - - private final HashSet issued = new HashSet<>(); - private final Stack free = new Stack<>(); - - //TODO: Pass in factory and finalize all API's rather than - //leaving the class to be anonymous? - public CipherPool() { - - } - - public synchronized final void init() { - grow(); - } - - public synchronized final Cipher borrow() { - if (free.isEmpty()) { - grow(); - Log.d(TAG, "Growing cipher pool. Current size is: " + free.size() + issued.size()); - } - Cipher toLend = free.pop(); - issued.add(toLend); - return toLend; - } - - public synchronized final void remit(Cipher cipher) { - issued.remove(cipher); - free.push(cipher); - } - - private synchronized void grow() { - for (int i = 0; i < GROWTH_FACTOR; ++i) { - free.push(generateNewCipher()); - } - } - - protected abstract Cipher generateNewCipher(); - - public synchronized final void expire() { - //do we want to try to destroy the final object here? - issued.clear(); - free.clear(); - } -} diff --git a/app/src/org/commcare/models/legacy/DecryptingCursor.java b/app/src/org/commcare/models/legacy/DecryptingCursor.java deleted file mode 100755 index 0f3d153d28..0000000000 --- a/app/src/org/commcare/models/legacy/DecryptingCursor.java +++ /dev/null @@ -1,113 +0,0 @@ -package org.commcare.models.legacy; - -import android.database.sqlite.SQLiteCursor; -import android.database.sqlite.SQLiteCursorDriver; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteQuery; - -import org.commcare.models.encryption.CipherPool; -import org.commcare.core.encryption.CryptUtil; -import org.commcare.modern.database.DatabaseHelper; -import org.commcare.modern.models.EncryptedModel; - -import javax.crypto.Cipher; - -/** - * @author ctsims - */ -public class DecryptingCursor extends SQLiteCursor { - final Cipher cipher; - final EncryptedModel model; - final CipherPool pool; - - public DecryptingCursor(SQLiteDatabase db, SQLiteCursorDriver driver, String editTable, SQLiteQuery query, EncryptedModel model, CipherPool pool) { - super(db, driver, editTable, query); - this.model = model; - this.pool = pool; - this.cipher = pool.borrow(); - } - - @Override - public byte[] getBlob(int columnIndex) { - if (!isEncrypted(columnIndex)) { - return super.getBlob(columnIndex); - } else { - return decrypt(columnIndex); - } - } - - @Override - public double getDouble(int columnIndex) { - if (!isEncrypted(columnIndex)) { - return super.getDouble(columnIndex); - } else { - return Double.valueOf(new String(decrypt(columnIndex))); - } - } - - @Override - public float getFloat(int columnIndex) { - if (!isEncrypted(columnIndex)) { - return super.getFloat(columnIndex); - } else { - return Float.valueOf(new String(decrypt(columnIndex))); - } - } - - @Override - public int getInt(int columnIndex) { - if (!isEncrypted(columnIndex)) { - return super.getInt(columnIndex); - } else { - return Integer.valueOf(new String(decrypt(columnIndex))); - } - } - - @Override - public long getLong(int columnIndex) { - if (!isEncrypted(columnIndex)) { - return super.getLong(columnIndex); - } else { - return Long.valueOf(new String(decrypt(columnIndex))); - } - } - - @Override - public short getShort(int columnIndex) { - if (!isEncrypted(columnIndex)) { - return super.getShort(columnIndex); - } else { - return Short.valueOf(new String(decrypt(columnIndex))); - } - } - - @Override - public String getString(int columnIndex) { - if (!isEncrypted(columnIndex)) { - return super.getString(columnIndex); - } else { - return new String(decrypt(columnIndex)); - } - } - - private boolean isEncrypted(int columnIndex) { - String column = this.getColumnName(columnIndex); - if (model.isEncrypted(column)) { - return true; - } - return (column.equals(DatabaseHelper.DATA_COL) && - model.isBlobEncrypted()); - } - - private byte[] decrypt(int columnIndex) { - byte[] data = super.getBlob(columnIndex); - return CryptUtil.decrypt(data, cipher); - } - - - @Override - public void close() { - super.close(); - pool.remit(cipher); - } -} diff --git a/app/src/org/commcare/services/CommCareSessionService.java b/app/src/org/commcare/services/CommCareSessionService.java index 1ed06a1e51..ca9da6d2ca 100644 --- a/app/src/org/commcare/services/CommCareSessionService.java +++ b/app/src/org/commcare/services/CommCareSessionService.java @@ -27,7 +27,6 @@ import org.commcare.interfaces.FormSaveCallback; import org.commcare.models.database.user.DatabaseUserOpenHelper; import org.commcare.models.database.user.UserSandboxUtils; -import org.commcare.models.encryption.CipherPool; import org.commcare.preferences.HiddenPreferences; import org.commcare.sync.FormSubmissionHelper; import org.commcare.tasks.DataSubmissionListener; @@ -83,7 +82,6 @@ public class CommCareSessionService extends Service { public static final ReentrantLock sessionAliveLock = new ReentrantLock(); private Timer maintenanceTimer; - private CipherPool pool; private byte[] key = null; @@ -141,7 +139,6 @@ public CommCareSessionService getService() { public void onCreate() { mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); setSessionLength(); - createCipherPool(); } @Override @@ -154,27 +151,6 @@ public void onTaskRemoved(Intent rootIntent) { } } - public void createCipherPool() { - pool = new CipherPool() { - @Override - public Cipher generateNewCipher() { - synchronized (lock) { - try { - SecretKeySpec spec = new SecretKeySpec(key, "AES"); - Cipher decrypter = Cipher.getInstance("AES"); - decrypter.init(Cipher.DECRYPT_MODE, spec); - - return decrypter; - } catch (NoSuchPaddingException | NoSuchAlgorithmException | - InvalidKeyException e) { - e.printStackTrace(); - } - } - return null; - } - }; - } - @Override public int onStartCommand(Intent intent, int flags, int startId) { // We want this service to continue running until it is explicitly @@ -286,7 +262,6 @@ public void prepareStorage(byte[] symetricKey, UserKeyRecord record) { synchronized (lock) { this.userKeyRecordUUID = record.getUuid(); this.key = symetricKey; - pool.init(); if (userDatabase != null && userDatabase.isOpen()) { userDatabase.close(); } @@ -488,8 +463,6 @@ public void closeServiceResources() { maintenanceTimer.cancel(); } logoutStartedAt = -1; - - pool.expire(); endHeartbeatLifecycle(); } }