-
-
Notifications
You must be signed in to change notification settings - Fork 154
Migrate vault passwords to GCM #547
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
8 commits
Select commit
Hold shift + click to select a range
bd7d324
Support GCM as credentials cryptor
SailReal ac5f3ff
Migrate access tokens to GCM
SailReal 5af0cdb
WIP vault password gcm migration
SailReal 89b8aa6
Add workaround to show multiple biometric auth promots from within th…
SailReal d4a279d
Merge branch 'develop' into feature/migrate-to-gcm
SailReal 827f35d
Run fluidattacks lane during dryRun as well
SailReal 8b0a6dc
Apply code review suggestions
SailReal 7a7f477
Store local storage URL in URL property of the cloud entity
SailReal 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,7 +90,7 @@ android { | |
} | ||
|
||
greendao { | ||
schemaVersion 12 | ||
schemaVersion 13 | ||
} | ||
|
||
configurations.all { | ||
|
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
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
142 changes: 142 additions & 0 deletions
142
data/src/main/java/org/cryptomator/data/db/Upgrade12To13.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,142 @@ | ||
package org.cryptomator.data.db | ||
|
||
import android.content.Context | ||
import org.cryptomator.util.crypto.CredentialCryptor | ||
import org.cryptomator.util.crypto.CryptoMode | ||
import org.greenrobot.greendao.database.Database | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
import timber.log.Timber | ||
|
||
@Singleton | ||
internal class Upgrade12To13 @Inject constructor(private val context: Context) : DatabaseUpgrade(12, 13) { | ||
|
||
override fun internalApplyTo(db: Database, origin: Int) { | ||
db.beginTransaction() | ||
try { | ||
moveLocalStorageUrlToUrlProperty(db) | ||
addCryptoModeToDbEntities(db) | ||
applyVaultPasswordCryptoModeToDb(db) | ||
upgradeCloudCryptoModeToGCM(db) | ||
db.setTransactionSuccessful() | ||
} finally { | ||
db.endTransaction() | ||
} | ||
} | ||
|
||
private fun moveLocalStorageUrlToUrlProperty(db: Database) { | ||
Sql.query("CLOUD_ENTITY").where("TYPE", Sql.eq("LOCAL")).executeOn(db).use { | ||
while (it.moveToNext()) { | ||
Sql.update("CLOUD_ENTITY") // | ||
.where("_id", Sql.eq(it.getLong(it.getColumnIndex("_id")))) // | ||
.set("URL", Sql.toString(it.getString(it.getColumnIndex("ACCESS_TOKEN")))) // | ||
.set("ACCESS_TOKEN", Sql.toNull()) // | ||
.executeOn(db) | ||
} | ||
} | ||
} | ||
|
||
private fun addCryptoModeToDbEntities(db: Database) { | ||
Sql.alterTable("CLOUD_ENTITY").renameTo("CLOUD_ENTITY_OLD").executeOn(db) | ||
|
||
Sql.createTable("CLOUD_ENTITY") // | ||
.id() // | ||
.requiredText("TYPE") // | ||
.optionalText("ACCESS_TOKEN") // | ||
.optionalText("ACCESS_TOKEN_CRYPTO_MODE") // | ||
.optionalText("URL") // | ||
.optionalText("USERNAME") // | ||
.optionalText("WEBDAV_CERTIFICATE") // | ||
.optionalText("S3_BUCKET") // | ||
.optionalText("S3_REGION") // | ||
.optionalText("S3_SECRET_KEY") // | ||
.optionalText("S3_SECRET_KEY_CRYPTO_MODE") // | ||
.executeOn(db) | ||
|
||
Sql.insertInto("CLOUD_ENTITY") // | ||
.select("_id", "TYPE", "ACCESS_TOKEN", "URL", "USERNAME", "WEBDAV_CERTIFICATE", "S3_BUCKET", "S3_REGION", "S3_SECRET_KEY") // | ||
.columns("_id", "TYPE", "ACCESS_TOKEN", "URL", "USERNAME", "WEBDAV_CERTIFICATE", "S3_BUCKET", "S3_REGION", "S3_SECRET_KEY") // | ||
.from("CLOUD_ENTITY_OLD") // | ||
.executeOn(db) | ||
|
||
// use this to recreate the index but also add the new column as well | ||
addPasswordCryptoModeToVaultDbEntity(db) | ||
|
||
Sql.dropTable("CLOUD_ENTITY_OLD").executeOn(db) | ||
} | ||
|
||
private fun addPasswordCryptoModeToVaultDbEntity(db: Database) { | ||
Sql.alterTable("VAULT_ENTITY").renameTo("VAULT_ENTITY_OLD").executeOn(db) | ||
Sql.createTable("VAULT_ENTITY") // | ||
.id() // | ||
.optionalInt("FOLDER_CLOUD_ID") // | ||
.optionalText("FOLDER_PATH") // | ||
.optionalText("FOLDER_NAME") // | ||
.optionalInt("FORMAT") // | ||
.requiredText("CLOUD_TYPE") // | ||
.optionalText("PASSWORD") // | ||
.optionalText("PASSWORD_CRYPTO_MODE") // | ||
.optionalInt("POSITION") // | ||
.optionalInt("SHORTENING_THRESHOLD") // | ||
.foreignKey("FOLDER_CLOUD_ID", "CLOUD_ENTITY", Sql.SqlCreateTableBuilder.ForeignKeyBehaviour.ON_DELETE_SET_NULL) // | ||
.executeOn(db) | ||
|
||
Sql.insertInto("VAULT_ENTITY") // | ||
.select("_id", "FOLDER_CLOUD_ID", "FOLDER_PATH", "FOLDER_NAME", "FORMAT", "PASSWORD", "POSITION", "SHORTENING_THRESHOLD", "CLOUD_ENTITY.TYPE") // | ||
.columns("_id", "FOLDER_CLOUD_ID", "FOLDER_PATH", "FOLDER_NAME", "FORMAT", "PASSWORD", "POSITION", "SHORTENING_THRESHOLD", "CLOUD_TYPE") // | ||
.from("VAULT_ENTITY_OLD") // | ||
.join("CLOUD_ENTITY", "VAULT_ENTITY_OLD.FOLDER_CLOUD_ID") // | ||
.executeOn(db) | ||
|
||
Sql.dropIndex("IDX_VAULT_ENTITY_FOLDER_PATH_FOLDER_CLOUD_ID").executeOn(db) | ||
|
||
Sql.createUniqueIndex("IDX_VAULT_ENTITY_FOLDER_PATH_FOLDER_CLOUD_ID") // | ||
.on("VAULT_ENTITY") // | ||
.asc("FOLDER_PATH") // | ||
.asc("FOLDER_CLOUD_ID") // | ||
.executeOn(db) | ||
|
||
Sql.dropTable("VAULT_ENTITY_OLD").executeOn(db) | ||
} | ||
|
||
private fun applyVaultPasswordCryptoModeToDb(db: Database) { | ||
Sql.query("VAULT_ENTITY").where("PASSWORD", Sql.isNotNull()).executeOn(db).use { | ||
while (it.moveToNext()) { | ||
Sql.update("VAULT_ENTITY") // | ||
.where("_id", Sql.eq(it.getLong(it.getColumnIndex("_id")))) // | ||
.set("PASSWORD_CRYPTO_MODE", Sql.toString(CryptoMode.CBC.toString())) // | ||
.executeOn(db) | ||
} | ||
} | ||
SailReal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private fun upgradeCloudCryptoModeToGCM(db: Database) { | ||
val gcmCryptor = CredentialCryptor.getInstance(context, CryptoMode.GCM) | ||
val cbcCryptor = CredentialCryptor.getInstance(context, CryptoMode.CBC) | ||
|
||
Sql.query("CLOUD_ENTITY").where("ACCESS_TOKEN", Sql.isNotNull()).executeOn(db).use { | ||
while (it.moveToNext()) { | ||
Sql.update("CLOUD_ENTITY") // | ||
.where("_id", Sql.eq(it.getLong(it.getColumnIndex("_id")))) // | ||
.set("ACCESS_TOKEN", Sql.toString(reEncrypt(it.getString(it.getColumnIndex("ACCESS_TOKEN")), gcmCryptor, cbcCryptor))) // | ||
.set("ACCESS_TOKEN_CRYPTO_MODE", Sql.toString(CryptoMode.GCM.toString())) // | ||
.executeOn(db) | ||
} | ||
} | ||
Sql.query("CLOUD_ENTITY").where("S3_SECRET_KEY", Sql.isNotNull()).executeOn(db).use { | ||
while (it.moveToNext()) { | ||
Sql.update("CLOUD_ENTITY") // | ||
.where("_id", Sql.eq(it.getLong(it.getColumnIndex("_id")))) // | ||
.set("S3_SECRET_KEY", Sql.toString(reEncrypt(it.getString(it.getColumnIndex("S3_SECRET_KEY")), gcmCryptor, cbcCryptor))) // | ||
.set("S3_SECRET_KEY_CRYPTO_MODE", Sql.toString(CryptoMode.GCM.toString())) // | ||
.executeOn(db) | ||
} | ||
} | ||
} | ||
|
||
private fun reEncrypt(ciphertext: String?, gcmCryptor: CredentialCryptor, cbcCryptor: CredentialCryptor): String? { | ||
if (ciphertext == null) return null | ||
val accessToken = cbcCryptor.decrypt(ciphertext) | ||
return gcmCryptor.encrypt(accessToken) | ||
} | ||
} |
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
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.