Skip to content

Commit 7d3f1c0

Browse files
Migrate user_version during cipher_migrate
1 parent 6f4a0f2 commit 7d3f1c0

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

src/crypto_impl.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,7 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
889889
int upgrade_4k_format = 0;
890890
sqlite3 *test;
891891
char *err = 0;
892+
int user_version = 0;
892893
static const unsigned char aCopy[] = {
893894
BTREE_SCHEMA_VERSION, 1, /* Add one to the old schema cookie */
894895
BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */
@@ -905,15 +906,14 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
905906

906907
char *attach_command = sqlite3_mprintf("ATTACH DATABASE '%s-migrated' as migrate KEY '%s';",
907908
db_filename, key);
908-
909-
int rc = sqlcipher_check_connection(db_filename, key, key_sz, "");
909+
int rc = sqlcipher_check_connection(db_filename, key, key_sz, "", &user_version);
910910
if(rc == SQLITE_OK){
911911
CODEC_TRACE(("No upgrade required - exiting\n"));
912912
goto exit;
913913
}
914914

915915
// Version 2 - check for 4k with hmac format
916-
rc = sqlcipher_check_connection(db_filename, key, key_sz, pragma_4k_kdf_iter);
916+
rc = sqlcipher_check_connection(db_filename, key, key_sz, pragma_4k_kdf_iter, &user_version);
917917
if(rc == SQLITE_OK) {
918918
CODEC_TRACE(("Version 2 format found\n"));
919919
upgrade_4k_format = 1;
@@ -922,7 +922,7 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
922922
// Version 1 - check both no hmac and 4k together
923923
char *pragma_1x_and_4k = sqlite3_mprintf("%s%s", pragma_hmac_off,
924924
pragma_4k_kdf_iter);
925-
rc = sqlcipher_check_connection(db_filename, key, key_sz, pragma_1x_and_4k);
925+
rc = sqlcipher_check_connection(db_filename, key, key_sz, pragma_1x_and_4k, &user_version);
926926
sqlite3_free(pragma_1x_and_4k);
927927
if(rc == SQLITE_OK) {
928928
CODEC_TRACE(("Version 1 format found\n"));
@@ -935,11 +935,13 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
935935
goto handle_error;
936936
}
937937

938+
char *set_user_version = sqlite3_mprintf("PRAGMA migrate.user_version = %d;", user_version);
938939
const char *commands[] = {
939940
upgrade_4k_format == 1 ? pragma_4k_kdf_iter : "",
940941
upgrade_1x_format == 1 ? pragma_hmac_off : "",
941942
attach_command,
942943
"SELECT sqlcipher_export('migrate');",
944+
set_user_version,
943945
};
944946
for(command_idx = 0; command_idx < ArraySize(commands); command_idx++){
945947
const char *command = commands[command_idx];
@@ -952,6 +954,7 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
952954
}
953955
}
954956
sqlite3_free(attach_command);
957+
sqlite3_free(set_user_version);
955958
sqlcipher_free(key, key_sz);
956959

957960
if(rc == SQLITE_OK){
@@ -1029,12 +1032,12 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
10291032
return rc;
10301033
}
10311034

1032-
int sqlcipher_check_connection(char *filename, char *key, int key_sz, char *sql) {
1035+
int sqlcipher_check_connection(char *filename, char *key, int key_sz, char *sql, int *user_version) {
10331036
int rc;
10341037
sqlite3 *db;
10351038
char *errMsg;
10361039
sqlite3_stmt *statement;
1037-
char *query_sqlite_master = "SELECT count(*) FROM sqlite_master;";
1040+
char *query_user_version = "PRAGMA user_version;";
10381041

10391042
rc = sqlite3_open(filename, &db);
10401043
if(rc != SQLITE_OK){
@@ -1048,11 +1051,13 @@ int sqlcipher_check_connection(char *filename, char *key, int key_sz, char *sql)
10481051
if(rc != SQLITE_OK){
10491052
goto cleanup;
10501053
}
1051-
rc = sqlite3_prepare(db, query_sqlite_master, -1, &statement, NULL);
1054+
rc = sqlite3_prepare(db, query_user_version, -1, &statement, NULL);
10521055
if(rc != SQLITE_OK){
10531056
goto cleanup;
10541057
}
1055-
if(sqlite3_step(statement) == SQLITE_ROW){
1058+
rc = sqlite3_step(statement);
1059+
if(rc == SQLITE_ROW){
1060+
*user_version = sqlite3_column_int(statement, 0);
10561061
rc = SQLITE_OK;
10571062
}
10581063
goto cleanup;

0 commit comments

Comments
 (0)