8000 Adjustments to cipher_migrate · githubzhaoliang/sqlcipher@6f4a0f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6f4a0f2

Browse files
Adjustments to cipher_migrate
- And format flags when checking for exit condition - Pass key size to sqlcipher_check_connection - Trace version upgrade logic
1 parent c3f6cf1 commit 6f4a0f2

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

src/crypto_impl.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,6 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
896896
BTREE_USER_VERSION, 0, /* Preserve the user version */
897897
BTREE_APPLICATION_ID, 0, /* Preserve the application id */
898898
};
899-
900899
key_sz = ctx->read_ctx->pass_sz + 1;
901900
key = sqlcipher_malloc(key_sz);
902901
memset(key, 0, key_sz);
@@ -907,35 +906,34 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
907906
char *attach_command = sqlite3_mprintf("ATTACH DATABASE '%s-migrated' as migrate KEY '%s';",
908907
db_filename, key);
909908

910-
int rc = sqlcipher_check_connection(db_filename, key, "");
909+
int rc = sqlcipher_check_connection(db_filename, key, key_sz, "");
911910
if(rc == SQLITE_OK){
912-
// no upgrade required
911+
CODEC_TRACE(("No upgrade required - exiting\n"));
913912
goto exit;
914913
}
915914

916-
// check for 1x format
917-
//rc = sqlcipher_check_connection(db_filename, key, pragma_hmac_off);
918-
//if(rc == SQLITE_OK) {
919-
// upgrade_1x_format = 1;
920-
//}
921-
922915
// Version 2 - check for 4k with hmac format
923-
rc = sqlcipher_check_connection(db_filename, key, pragma_4k_kdf_iter);
916+
rc = sqlcipher_check_connection(db_filename, key, key_sz, pragma_4k_kdf_iter);
924917
if(rc == SQLITE_OK) {
918+
CODEC_TRACE(("Version 2 format found\n"));
925919
upgrade_4k_format = 1;
926920
}
927921

928922
// Version 1 - check both no hmac and 4k together
929923
char *pragma_1x_and_4k = sqlite3_mprintf("%s%s", pragma_hmac_off,
930924
pragma_4k_kdf_iter);
931-
rc = sqlcipher_check_connection(db_filename, key, pragma_1x_and_4k);
925+
rc = sqlcipher_check_connection(db_filename, key, key_sz, pragma_1x_and_4k);
932926
sqlite3_free(pragma_1x_and_4k);
933927
if(rc == SQLITE_OK) {
928+
CODEC_TRACE(("Version 1 format found\n"));
934929
upgrade_1x_format = 1;
935930
upgrade_4k_format = 1;
936931
}
937932

938-
if(upgrade_1x_format == 0 || upgrade_4k_format == 0) goto handle_error;
933+
if(upgrade_1x_format == 0 && upgrade_4k_format == 0) {
934+
CODEC_TRACE(("Upgrade format not determined\n"));
935+
goto handle_error;
936+
}
939937

940938
const char *commands[] = {
941939
upgrade_4k_format == 1 ? pragma_4k_kdf_iter : "",
@@ -1016,31 +1014,33 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
10161014
sqlite3ResetAllSchemasOfConnection(db);
10171015
remove(migrated_db_filename);
10181016
sqlite3_free(migrated_db_filename);
1017+
} else {
1018+
CODEC_TRACE(("*** migration failure** \n\n"));
10191019
}
1020+
10201021
}
10211022
goto exit;
10221023

10231024
handle_error:
1024-
CODEC_TRACE(("an error occurred\n"));
1025+
CODEC_TRACE(("An error occurred attempting to migrate the database\n"));
10251026
rc = SQLITE_ERROR;
10261027

10271028
exit:
10281029
return rc;
10291030
}
10301031

1031-
int sqlcipher_check_connection(char *filename, char *key, char *sql) {
1032+
int sqlcipher_check_connection(char *filename, char *key, int key_sz, char *sql) {
10321033
int rc;
10331034
sqlite3 *db;
10341035
char *errMsg;
10351036
sqlite3_stmt *statement;
1036-
int status = SQLITE_ERROR;
10371037
char *query_sqlite_master = "SELECT count(*) FROM sqlite_master;";
10381038

10391039
rc = sqlite3_open(filename, &db);
10401040
if(rc != SQLITE_OK){
10411041
goto cleanup;
10421042
}
1043-
rc = sqlite3_key(db, key, (int)strlen(key));
1043+
rc = sqlite3_key(db, key, key_sz);
10441044
if(rc != SQLITE_OK){
10451045
goto cleanup;
10461046
}
@@ -1053,7 +1053,7 @@ int sqlcipher_check_connection(char *filename, char *key, char *sql) {
10531053
goto cleanup;
10541054
}
10551055
if(sqlite3_step(statement) == SQLITE_ROW){
1056-
status = SQLITE_OK;
1056+
rc = SQLITE_OK;
10571057
}
10581058
goto cleanup;
10591059

@@ -1066,7 +1066,7 @@ int sqlcipher_check_connection(char *filename, char *key, char *sql) {
10661066
}
10671067

10681068
exit:
1069-
return status;
1069+
return rc;
10701070

10711071
}
10721072

test/crypto.test

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,8 +1960,6 @@ db close
19601960
file delete -force test.db
19611961

19621962
do_test migrate-1.1.8-database-to-3x-format {
1963-
file delete -force test.db-migrated
1964-
file delete -force test.db
19651963
file copy -force sqlcipher-1.1.8-testkey.db test.db
19661964
sqlite_orig db test.db
19671965
execsql {
@@ -1978,10 +1976,8 @@ do_test migrate-1.1.8-database-to-3x-format {
19781976
} {1}
19791977
db close
19801978
file delete -force test.db
1981-
file delete -force test.db-migrated
19821979

19831980
do_test migrate-2-0-le-database-to-3x-format {
1984-
file delete -force test.db
19851981
file copy -force sqlcipher-2.0-le-testkey.db test.db
19861982
sqlite_orig db test.db
19871983
execsql {
@@ -1998,6 +1994,5 @@ do_test migrate-2-0-le-database-to-3x-format {
19981994
} {1}
19991995
db close
20001996
file delete -force test.db
2001-
file delete -force test.db-migrated
20021997

20031998
finish_test

0 commit comments

Comments
 (0)
0