-
-
Notifications
You must be signed in to change notification settings - Fork 734
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
feat: Implementing encrypted local storage for user sessions #1191
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
113 changes: 113 additions & 0 deletions
113
parse/src/main/java/com/parse/EncryptedFileObjectStore.java
This file contains 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,113 @@ | ||
package com.parse; | ||
|
||
import android.content.Context; | ||
|
||
import androidx.security.crypto.EncryptedFile; | ||
import androidx.security.crypto.MasterKey; | ||
|
||
import com.parse.boltsinternal.Task; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.security.GeneralSecurityException; | ||
import java.util.concurrent.Callable; | ||
|
||
/** | ||
* a file based {@link ParseObjectStore} using Jetpack's {@link EncryptedFile} class to protect files from a malicious copy. | ||
*/ | ||
class EncryptedFileObjectStore<T extends ParseObject> implements ParseObjectStore<T> { | ||
|
||
private final String className; | ||
private final File file; | ||
private final EncryptedFile encryptedFile; | ||
private final ParseObjectCurrentCoder coder; | ||
|
||
public EncryptedFileObjectStore(Class<T> clazz, File file, ParseObjectCurrentCoder coder) { | ||
this(getSubclassingController().getClassName(clazz), file, coder); | ||
} | ||
|
||
public EncryptedFileObjectStore(String className, File file, ParseObjectCurrentCoder coder) { | ||
this.className = className; | ||
this.file = file; | ||
this.coder = coder; | ||
Context context = ParsePlugins.get().applicationContext(); | ||
try { | ||
encryptedFile = new EncryptedFile.Builder(context, file, new MasterKey.Builder(context).setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build(), EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB).build(); | ||
} catch (GeneralSecurityException | IOException e) { | ||
throw new RuntimeException(e.getMessage()); | ||
} | ||
} | ||
|
||
private static ParseObjectSubclassingController getSubclassingController() { | ||
return ParseCorePlugins.getInstance().getSubclassingController(); | ||
} | ||
|
||
/** | ||
* Saves the {@code ParseObject} to the a file on disk as JSON in /2/ format. | ||
* | ||
* @param current ParseObject which needs to be saved to disk. | ||
* @throws IOException thrown if an error occurred during writing of the file | ||
* @throws GeneralSecurityException thrown if there is an error with encryption keys or during the encryption of the file | ||
*/ | ||
private void saveToDisk(ParseObject current) throws IOException, GeneralSecurityException { | ||
JSONObject json = coder.encode(current.getState(), null, PointerEncoder.get()); | ||
ParseFileUtils.writeJSONObjectToFile(encryptedFile, json); | ||
} | ||
|
||
/** | ||
* Retrieves a {@code ParseObject} from a file on disk in /2/ format. | ||
* | ||
* @return The {@code ParseObject} that was retrieved. If the file wasn't found, or the contents | ||
* of the file is an invalid {@code ParseObject}, returns {@code null}. | ||
* @throws GeneralSecurityException thrown if there is an error with encryption keys or during the encryption of the file | ||
* @throws JSONException thrown if an error occurred during the decoding process of the ParseObject to a JSONObject | ||
* @throws IOException thrown if an error occurred during writing of the file | ||
*/ | ||
private T getFromDisk() throws GeneralSecurityException, JSONException, IOException { | ||
return ParseObject.from(coder.decode(ParseObject.State.newBuilder(className), ParseFileUtils.readFileToJSONObject(encryptedFile), ParseDecoder.get()).isComplete(true).build()); | ||
} | ||
|
||
@Override | ||
public Task<T> getAsync() { | ||
return Task.call(new Callable<T>() { | ||
@Override | ||
public T call() throws Exception { | ||
if (!file.exists()) return null; | ||
try { | ||
return getFromDisk(); | ||
} catch (GeneralSecurityException e) { | ||
throw new RuntimeException(e.getMessage()); | ||
} | ||
} | ||
}, ParseExecutors.io()); | ||
} | ||
|
||
@Override | ||
public Task<Void> setAsync(T object) { | ||
return Task.call(() -> { | ||
if (file.exists() && !ParseFileUtils.deleteQuietly(file)) throw new RuntimeException("Unable to delete"); | ||
try { | ||
saveToDisk(object); | ||
} catch (GeneralSecurityException e) { | ||
throw new RuntimeException(e.getMessage()); | ||
} | ||
return null; | ||
}, ParseExecutors.io()); | ||
} | ||
|
||
@Override | ||
public Task<Boolean> existsAsync() { | ||
return Task.call(file::exists, ParseExecutors.io()); | ||
} | ||
|
||
@Override | ||
public Task<Void> deleteAsync() { | ||
return Task.call(() -> { | ||
if (file.exists() && !ParseFileUtils.deleteQuietly(file)) throw new RuntimeException("Unable to delete"); | ||
return null; | ||
}, ParseExecutors.io()); | ||
} | ||
} |
This file contains 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 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
69 changes: 69 additions & 0 deletions
69
parse/src/main/java/com/parse/ParseObjectStoreMigrator.java
This file contains 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,69 @@ | ||
package com.parse; | ||
|
||
import com.parse.boltsinternal.Continuation; | ||
import com.parse.boltsinternal.Task; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* Use this utility class to migrate from one {@link ParseObjectStore} to another | ||
*/ | ||
class ParseObjectStoreMigrator<T extends ParseObject> implements ParseObjectStore<T> { | ||
|
||
private final ParseObjectStore<T> store; | ||
private final ParseObjectStore<T> legacy; | ||
|
||
/** | ||
* @param store the new {@link ParseObjectStore} to migrate to | ||
* @param legacy the old {@link ParseObjectStore} to migrate from | ||
*/ | ||
public ParseObjectStoreMigrator(ParseObjectStore<T> store, ParseObjectStore<T> legacy) { | ||
this.store = store; | ||
this.legacy = legacy; | ||
} | ||
|
||
@Override | ||
public Task<T> getAsync() { | ||
return store.getAsync().continueWithTask(new Continuation<T, Task<T>>() { | ||
@Override | ||
public Task<T> then(Task<T> task) throws Exception { | ||
if (task.getResult() != null) return task; | ||
return legacy.getAsync().continueWithTask(new Continuation<T, Task<T>>() { | ||
@Override | ||
public Task<T> then(Task<T> task) throws Exception { | ||
T object = task.getResult(); | ||
if (object == null) return task; | ||
return legacy.deleteAsync().continueWith(task1 -> ParseTaskUtils.wait(store.setAsync(object))).onSuccess(task1 -> object); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public Task<Void> setAsync(T object) { | ||
return store.setAsync(object); | ||
} | ||
|
||
@Override | ||
public Task<Boolean> existsAsync() { | ||
return store.existsAsync().continueWithTask(new Continuation<Boolean, Task<Boolean>>() { | ||
@Override | ||
public Task<Boolean> then(Task<Boolean> task) throws Exception { | ||
if (task.getResult()) return Task.forResult(true); | ||
return legacy.existsAsync(); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public Task<Void> deleteAsync() { | ||
Task<Void> storeTask = store.deleteAsync(); | ||
return Task.whenAll(Arrays.asList(legacy.deleteAsync(), storeTask)).continueWithTask(new Continuation<Void, Task<Void>>() { | ||
@Override | ||
public Task<Void> then(Task<Void> task1) throws Exception { | ||
return storeTask; | ||
} | ||
}); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
parse/src/test/java/com/parse/AlgorithmParameterSpecExt.kt
This file contains 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,22 @@ | ||
package com.parse | ||
|
||
/* | ||
* Copyright 2020 Appmattus Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import java.security.spec.AlgorithmParameterSpec | ||
|
||
internal val AlgorithmParameterSpec.keystoreAlias: String | ||
get() = this::class.java.getDeclaredMethod("getKeystoreAlias").invoke(this) as String |
Oops, something went wrong.
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.
We need to make sure to consider the license implications. This repository is planned to be migrated to Apache 2, we should do that before merging this, otherwise we need to add this as a second license.