-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PIR: Add database encryption #7010
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
Merged
Merged
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 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
98 changes: 98 additions & 0 deletions
98
...mpl/src/main/java/com/duckduckgo/pir/impl/store/secure/PirSecureStorageDatabaseFactory.kt
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,98 @@ | ||
| /* | ||
| * Copyright (c) 2025 DuckDuckGo | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| package com.duckduckgo.pir.impl.store.secure | ||
|
|
||
| import android.content.Context | ||
| import androidx.room.Room | ||
| import com.duckduckgo.di.scopes.AppScope | ||
| import com.duckduckgo.library.loader.LibraryLoader | ||
| import com.duckduckgo.pir.impl.store.PirDatabase | ||
| import com.squareup.anvil.annotations.ContributesBinding | ||
| import dagger.SingleInstanceIn | ||
| import kotlinx.coroutines.sync.Mutex | ||
| import kotlinx.coroutines.sync.withLock | ||
| import logcat.LogPriority.ERROR | ||
| import logcat.asLog | ||
| import logcat.logcat | ||
| import net.zetetic.database.sqlcipher.SupportOpenHelperFactory | ||
| import javax.inject.Inject | ||
|
|
||
| interface PirSecureStorageDatabaseFactory { | ||
| suspend fun getDatabase(): PirDatabase? | ||
| } | ||
|
|
||
| @SingleInstanceIn(AppScope::class) | ||
| @ContributesBinding( | ||
| scope = AppScope::class, | ||
| boundType = PirSecureStorageDatabaseFactory::class, | ||
| ) | ||
| class RealPirSecureStorageDatabaseFactory @Inject constructor( | ||
| private val context: Context, | ||
| private val keyProvider: PirSecureStorageKeyProvider, | ||
| ) : PirSecureStorageDatabaseFactory { | ||
| private var _database: PirDatabase? = null | ||
|
|
||
| private val mutex = Mutex() | ||
|
|
||
| init { | ||
| logcat { "PIR-DB: Loading the sqlcipher native library" } | ||
| try { | ||
| LibraryLoader.loadLibrary(context, "sqlcipher") | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It should be fine for both Autofill and PIR to call this multiple times |
||
| logcat { "PIR-DB: sqlcipher native library loaded ok" } | ||
| } catch (t: Throwable) { | ||
| // error loading the library | ||
| logcat(ERROR) { "PIR-DB: Error loading sqlcipher library: ${t.asLog()}" } | ||
| } | ||
| } | ||
|
|
||
| override suspend fun getDatabase(): PirDatabase? { | ||
| _database?.let { return it } | ||
| return mutex.withLock { | ||
| getInnerDatabase() | ||
| } | ||
| } | ||
|
|
||
| @OptIn(ExperimentalStdlibApi::class) | ||
| private suspend fun getInnerDatabase(): PirDatabase? { | ||
| // If we have already the DB instance then let's use it | ||
| if (_database != null) { | ||
| return _database | ||
| } | ||
|
|
||
| // If we can't access the keystore, it means that L1Key will be null. We don't want to encrypt the db with a null key. | ||
| return if (keyProvider.canAccessKeyStore()) { | ||
| // At this point, we are guaranteed that if L1key is null, it's because it hasn't been generated yet. Else, we always use the one stored. | ||
| _database = Room.databaseBuilder( | ||
| context, | ||
| PirDatabase::class.java, | ||
| "pir_encrypted.db", | ||
| ) | ||
| .openHelperFactory( | ||
| SupportOpenHelperFactory( | ||
| keyProvider.getl1Key(), | ||
| ), | ||
| ) | ||
| .enableMultiInstanceInvalidation() | ||
| .fallbackToDestructiveMigration() | ||
| .build() | ||
| _database | ||
| } else { | ||
| logcat(ERROR) { "PIR-DB: Cannot access key store!" } | ||
| null | ||
| } | ||
| } | ||
| } | ||
68 changes: 68 additions & 0 deletions
68
...ir-impl/src/main/java/com/duckduckgo/pir/impl/store/secure/PirSecureStorageKeyProvider.kt
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,68 @@ | ||
| /* | ||
| * Copyright (c) 2025 DuckDuckGo | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| package com.duckduckgo.pir.impl.store.secure | ||
|
|
||
| import com.duckduckgo.di.scopes.AppScope | ||
| import com.squareup.anvil.annotations.ContributesBinding | ||
| import kotlinx.coroutines.sync.Mutex | ||
| import kotlinx.coroutines.sync.withLock | ||
| import javax.inject.Inject | ||
|
|
||
| /** | ||
| * This class provides the usable decrypted keys to be used for encryption | ||
| */ | ||
| interface PirSecureStorageKeyProvider { | ||
| suspend fun canAccessKeyStore(): Boolean | ||
|
|
||
| /** | ||
| * Ready to use key for L1 encryption | ||
| */ | ||
| suspend fun getl1Key(): ByteArray | ||
| } | ||
|
|
||
| @ContributesBinding(AppScope::class) | ||
| class RealPirSecureStorageKeyProvider @Inject constructor( | ||
| private val randomBytesGenerator: PirRandomBytesGenerator, | ||
| private val secureStorageKeyRepository: PirSecureStorageKeyRepository, | ||
| ) : PirSecureStorageKeyProvider { | ||
|
|
||
| override suspend fun canAccessKeyStore(): Boolean = | ||
| secureStorageKeyRepository.canUseEncryption() | ||
|
|
||
| private val l1KeyMutex = Mutex() | ||
|
|
||
| override suspend fun getl1Key(): ByteArray { | ||
| l1KeyMutex.withLock { | ||
| return innerGetL1Key() | ||
| } | ||
| } | ||
|
|
||
| private suspend fun innerGetL1Key(): ByteArray { | ||
| // If no key exists in the keystore, we generate a new one and store it | ||
| return if (secureStorageKeyRepository.getL1Key() == null) { | ||
| randomBytesGenerator.generateBytes(L1_PASSPHRASE_SIZE).also { | ||
| secureStorageKeyRepository.setL1Key(it) | ||
| } | ||
| } else { | ||
| secureStorageKeyRepository.getL1Key()!! | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private const val L1_PASSPHRASE_SIZE = 32 | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
...-impl/src/main/java/com/duckduckgo/pir/impl/store/secure/PirSecureStorageKeyRepository.kt
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,57 @@ | ||
| /* | ||
| * Copyright (c) 2025 DuckDuckGo | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| package com.duckduckgo.pir.impl.store.secure | ||
|
|
||
| import com.duckduckgo.di.scopes.AppScope | ||
| import com.squareup.anvil.annotations.ContributesBinding | ||
| import dagger.SingleInstanceIn | ||
| import javax.inject.Inject | ||
|
|
||
| interface PirSecureStorageKeyRepository { | ||
| /** | ||
| * Key used for L1 encryption | ||
| */ | ||
| suspend fun getL1Key(): ByteArray? | ||
| suspend fun setL1Key(value: ByteArray?) | ||
|
|
||
| /** | ||
| * This method can be checked if the keystore has support for encryption | ||
| * | ||
| * @return `true` if keystore encryption is supported and `false` otherwise | ||
| */ | ||
| suspend fun canUseEncryption(): Boolean | ||
| } | ||
|
|
||
| @SingleInstanceIn(AppScope::class) | ||
| @ContributesBinding( | ||
| scope = AppScope::class, | ||
| boundType = PirSecureStorageKeyRepository::class, | ||
| ) | ||
| class RealPirSecureStorageKeyRepository @Inject constructor( | ||
| private val keyStore: PirSecureStorageKeyStore, | ||
| ) : PirSecureStorageKeyRepository { | ||
| override suspend fun getL1Key(): ByteArray? = keyStore.getKey(KEY_L1KEY) | ||
| override suspend fun setL1Key(value: ByteArray?) { | ||
| keyStore.updateKey(KEY_L1KEY, value) | ||
| } | ||
|
|
||
| override suspend fun canUseEncryption(): Boolean = keyStore.canUseEncryption() | ||
|
|
||
| companion object { | ||
| private const val KEY_L1KEY = "KEY_L1KEY" | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.