|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 DuckDuckGo |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.duckduckgo.pir.internal.settings.store.secure |
| 18 | + |
| 19 | +import android.content.Context |
| 20 | +import com.duckduckgo.common.utils.DispatcherProvider |
| 21 | +import com.duckduckgo.di.scopes.AppScope |
| 22 | +import com.duckduckgo.pir.impl.store.PirDatabase |
| 23 | +import com.squareup.anvil.annotations.ContributesBinding |
| 24 | +import dagger.SingleInstanceIn |
| 25 | +import kotlinx.coroutines.withContext |
| 26 | +import logcat.LogPriority.ERROR |
| 27 | +import logcat.asLog |
| 28 | +import logcat.logcat |
| 29 | +import java.io.File |
| 30 | +import javax.inject.Inject |
| 31 | + |
| 32 | +/** |
| 33 | + * Utility interface for exporting the encrypted PIR database to a plaintext format. |
| 34 | + * Useful for debugging and data inspection. |
| 35 | + */ |
| 36 | +interface PirDatabaseExporter { |
| 37 | + /** |
| 38 | + * Exports the encrypted database to a plaintext (unencrypted) SQLite database |
| 39 | + * on the device's external storage. |
| 40 | + * |
| 41 | + * You can pull it using "adb pull /storage/emulated/0/Android/data/com.duckduckgo.mobile.android.debug/files/pir_decrypted.db ~/Desktop/" |
| 42 | + * and open it in a SQLite viewer for inspection. |
| 43 | + */ |
| 44 | + suspend fun exportToPlaintext() |
| 45 | +} |
| 46 | + |
| 47 | +@SingleInstanceIn(AppScope::class) |
| 48 | +@ContributesBinding( |
| 49 | + scope = AppScope::class, |
| 50 | + boundType = PirDatabaseExporter::class, |
| 51 | +) |
| 52 | +class PirRealDatabaseExporter @Inject constructor( |
| 53 | + private val pirDatabase: PirDatabase, |
| 54 | + private val dispatcherProvider: DispatcherProvider, |
| 55 | + private val context: Context, |
| 56 | +) : PirDatabaseExporter { |
| 57 | + |
| 58 | + override suspend fun exportToPlaintext() = withContext(dispatcherProvider.io()) { |
| 59 | + exportDecryptedDatabase() |
| 60 | + } |
| 61 | + |
| 62 | + private fun exportDecryptedDatabase() { |
| 63 | + try { |
| 64 | + // Get the writable database instance |
| 65 | + val db = pirDatabase.openHelper.writableDatabase |
| 66 | + |
| 67 | + // Define output path - using external files directory for accessibility |
| 68 | + val outputFile = File(context.getExternalFilesDir(null), "pir_decrypted.db") |
| 69 | + |
| 70 | + // Delete existing file if present |
| 71 | + if (outputFile.exists()) { |
| 72 | + outputFile.delete() |
| 73 | + logcat { "PIR-DB: Deleted existing decrypted database file" } |
| 74 | + } |
| 75 | + |
| 76 | + val outputPath = outputFile.absolutePath |
| 77 | + logcat { "PIR-DB: Exporting decrypted database to: $outputPath" } |
| 78 | + |
| 79 | + // Attach a plaintext database (no encryption key) |
| 80 | + db.query("ATTACH DATABASE ? AS plaintext KEY ''", arrayOf(outputPath)).use { cursor -> |
| 81 | + cursor.moveToFirst() |
| 82 | + } |
| 83 | + |
| 84 | + logcat { "PIR-DB: Attached plaintext database" } |
| 85 | + |
| 86 | + // Export the encrypted database to the plaintext one |
| 87 | + db.query("SELECT sqlcipher_export('plaintext')").use { cursor -> |
| 88 | + if (cursor.moveToFirst()) { |
| 89 | + val result = cursor.getString(0) |
| 90 | + logcat { "PIR-DB: Export result: $result" } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + logcat { "PIR-DB: Database exported successfully" } |
| 95 | + |
| 96 | + // Detach the plaintext database |
| 97 | + db.query("DETACH DATABASE plaintext").use { cursor -> |
| 98 | + cursor.moveToFirst() |
| 99 | + } |
| 100 | + |
| 101 | + logcat { "PIR-DB: Detached plaintext database" } |
| 102 | + |
| 103 | + // Verify the file was created and has content |
| 104 | + if (outputFile.exists() && outputFile.length() > 0) { |
| 105 | + logcat { "PIR-DB: Successfully exported decrypted database (${outputFile.length()} bytes)" } |
| 106 | + logcat { "PIR-DB: File location: ${outputFile.absolutePath}" } |
| 107 | + } else { |
| 108 | + logcat(ERROR) { "PIR-DB: Export file was not created or is empty" } |
| 109 | + } |
| 110 | + } catch (e: Exception) { |
| 111 | + logcat(ERROR) { "PIR-DB: Error exporting database: ${e.asLog()}" } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments