-
Notifications
You must be signed in to change notification settings - Fork 678
feat: database encryption using sqlcipher #539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| * This project is licensed under the open source MPL V2. | ||
| * See https://github.com/openMF/android-client/blob/master/LICENSE.md | ||
| */ | ||
|
|
||
| package com.mifos; | ||
|
|
||
| import android.app.Application; | ||
| import android.content.Context; | ||
| import android.graphics.Typeface; | ||
|
|
||
| import com.crashlytics.android.Crashlytics; | ||
| import com.joanzapata.iconify.Iconify; | ||
| import com.joanzapata.iconify.fonts.MaterialModule; | ||
| import com.mifos.api.local.MifosDatabase; | ||
| import com.mifos.api.local.MifosSQLCipherHelper; | ||
| import com.mifos.mifosxdroid.injection.component.ApplicationComponent; | ||
| import com.mifos.mifosxdroid.injection.component.DaggerApplicationComponent; | ||
| import com.mifos.mifosxdroid.injection.module.ApplicationModule; | ||
| import com.raizlabs.android.dbflow.config.DatabaseConfig; | ||
| import com.raizlabs.android.dbflow.config.DatabaseDefinition; | ||
| import com.raizlabs.android.dbflow.config.FlowConfig; | ||
| import com.raizlabs.android.dbflow.config.FlowManager; | ||
| import com.raizlabs.android.dbflow.structure.database.DatabaseHelperListener; | ||
| import com.raizlabs.android.dbflow.structure.database.OpenHelper; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import io.fabric.sdk.android.Fabric; | ||
|
|
||
| /** | ||
| * Created by ishankhanna on 13/03/15. | ||
| */ | ||
| public class App extends Application { | ||
|
|
||
| public static final Map<Integer, Typeface> typefaceManager = new HashMap<>(); | ||
|
|
||
| private static App instance; | ||
|
|
||
| ApplicationComponent mApplicationComponent; | ||
|
|
||
| public static Context getContext() { | ||
| return instance; | ||
| } | ||
|
|
||
| public static App getInstance() { | ||
| return instance; | ||
| } | ||
|
|
||
| public static App get(Context context) { | ||
| return (App) context.getApplicationContext(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onCreate() { | ||
| super.onCreate(); | ||
| instance = this; | ||
| Fabric.with(this, new Crashlytics()); | ||
|
|
||
| Iconify.with(new MaterialModule()); | ||
| //Initializing the DBFlow and SQL Cipher Encryption | ||
| FlowManager.init(new FlowConfig.Builder(this) | ||
| .addDatabaseConfig( | ||
| new DatabaseConfig.Builder(MifosDatabase.class) | ||
| .openHelper(new DatabaseConfig.OpenHelperCreator() { | ||
| @Override | ||
| public OpenHelper createHelper( | ||
| DatabaseDefinition databaseDefinition, | ||
| DatabaseHelperListener helperListener) { | ||
| return new MifosSQLCipherHelper( | ||
| databaseDefinition, helperListener); | ||
| } | ||
| }) | ||
| .build()) | ||
| .build()); | ||
| } | ||
|
|
||
| public ApplicationComponent getComponent() { | ||
| if (mApplicationComponent == null) { | ||
| mApplicationComponent = DaggerApplicationComponent.builder() | ||
| .applicationModule(new ApplicationModule(this)) | ||
| .build(); | ||
| } | ||
| return mApplicationComponent; | ||
| } | ||
|
|
||
| // Needed to replace the component with a test specific one | ||
| public void setComponent(ApplicationComponent applicationComponent) { | ||
| mApplicationComponent = applicationComponent; | ||
| } | ||
|
|
||
| } |
79 changes: 79 additions & 0 deletions
79
mifosng-android/src/release/java/com/mifos/api/MifosOkHttpClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package com.mifos.api; | ||
|
|
||
| import java.security.cert.CertificateException; | ||
| import java.security.cert.X509Certificate; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import javax.net.ssl.HostnameVerifier; | ||
| import javax.net.ssl.SSLContext; | ||
| import javax.net.ssl.SSLSession; | ||
| import javax.net.ssl.SSLSocketFactory; | ||
| import javax.net.ssl.TrustManager; | ||
| import javax.net.ssl.X509TrustManager; | ||
|
|
||
| import okhttp3.OkHttpClient; | ||
|
|
||
| /** | ||
| * Created by Rajan Maurya on 16/06/16. | ||
| */ | ||
| public class MifosOkHttpClient { | ||
|
|
||
|
|
||
| public OkHttpClient getMifosOkHttpClient() { | ||
|
|
||
| OkHttpClient.Builder builder = new OkHttpClient.Builder(); | ||
|
|
||
| try { | ||
| // Create a trust manager that does not validate certificate chains | ||
| TrustManager[] trustAllCerts = { | ||
| new X509TrustManager() { | ||
| @Override | ||
| public void checkClientTrusted( | ||
| X509Certificate[] chain, | ||
| String authType) throws CertificateException { | ||
| } | ||
|
|
||
| @Override | ||
| public void checkServerTrusted( | ||
| X509Certificate[] chain, | ||
| String authType) throws CertificateException { | ||
| } | ||
|
|
||
| @Override | ||
| public X509Certificate[] getAcceptedIssuers() { | ||
| return new X509Certificate[0]; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // Install the all-trusting trust manager | ||
| SSLContext sslContext = SSLContext.getInstance("SSL"); | ||
| sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); | ||
| // Create an ssl socket factory with our all-trusting manager | ||
| SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); | ||
|
|
||
| //Set SSL certificate to OkHttpClient Builder | ||
|
|
||
| builder.sslSocketFactory(sslSocketFactory); | ||
|
|
||
| builder.hostnameVerifier(new HostnameVerifier() { | ||
| @Override | ||
| public boolean verify(String hostname, SSLSession session) { | ||
| return true; | ||
| } | ||
| }); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
|
|
||
| //Setting Timeout 30 Seconds | ||
| builder.connectTimeout(60, TimeUnit.SECONDS); | ||
| builder.readTimeout(60, TimeUnit.SECONDS); | ||
|
|
||
| //Interceptor :> Full Body Logger and ApiRequest Header | ||
| builder.addInterceptor(new MifosInterceptor()); | ||
|
|
||
| return builder.build(); | ||
|
|
||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
mifosng-android/src/release/java/com/mifos/api/local/MifosSQLCipherHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.mifos.api.local; | ||
|
|
||
| import com.raizlabs.android.dbflow.config.DatabaseDefinition; | ||
| import com.raizlabs.android.dbflow.structure.database.DatabaseHelperListener; | ||
| import com.raizlabs.dbflow.android.sqlcipher.SQLCipherOpenHelper; | ||
|
|
||
| public class MifosSQLCipherHelper extends SQLCipherOpenHelper { | ||
|
|
||
| public MifosSQLCipherHelper(DatabaseDefinition databaseDefinition, | ||
| DatabaseHelperListener listener) { | ||
| super(databaseDefinition, listener); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getCipherSecret() { | ||
| return "dbflow-rules"; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line of code is the problem, Secret is something that people cannot know. We need to find a way to make it secure.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have two ways to implement
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@satyan Suggestion
One of the options could be to use user's password entry to encrypt the key on the client. So you could only decrypt with the help of user.
It adds a layer of entropy to the system. However, could mean that we ask the user for password multiple times..
Android also supports a hardware backed keychain support. You could potentially explore that as well to store the key.
Having a server backed key is a good option too. Except, in offline case, it'd be difficult to obtain. Do not store in shared preferences, as it's stored in a file on the device in clear. Easily obtained.