8000 Proper format for key on attach, migrate user_version · magic-coder/sqlcipher@9d84aca · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d84aca

Browse files
Proper format for key on attach, migrate user_version
1 parent 018c0fb commit 9d84aca

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/crypto_impl.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -906,11 +906,11 @@ const char* sqlcipher_codec_get_cipher_provider(codec_ctx *ctx) {
906906
}
907907

908908

909-
static int sqlcipher_check_connection(const char *filename, char *key, int key_sz, char *sql) {
909+
static int sqlcipher_check_connection(const char *filename, char *key, int key_sz, char *sql, int *user_version) {
910910
int rc;
911911
sqlite3 *db = NULL;
912912
sqlite3_stmt *statement = NULL;
913-
char *query_sqlite_master = "SELECT count(*) FROM sqlite_master;";
913+
char *query_user_version = "PRAGMA user_version;";
914914

915915
rc = sqlite3_open(filename, &db);
916916
if(rc != SQLITE_OK){
@@ -924,11 +924,13 @@ static int sqlcipher_check_connection(const char *filename, char *key, int key_s
924924
if(rc != SQLITE_OK){
925925
goto cleanup;
926926
}
927-
rc = sqlite3_prepare(db, query_sqlite_master, -1, &statement, NULL);
927+
rc = sqlite3_prepare(db, query_user_version, -1, &statement, NULL);
928928
if(rc != SQLITE_OK){
929929
goto cleanup;
930930
}
931-
if(sqlite3_step(statement) == SQLITE_ROW){
931+
rc = sqlite3_step(statement);
932+
if(rc == SQLITE_ROW){
933+
*user_version = sqlite3_column_int(statement, 0);
932934
rc = SQLITE_OK;
933935
}
934936

@@ -958,8 +960,10 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
958960
char *pragma_hmac_off = "PRAGMA cipher_use_hmac = OFF;";
959961
char *pragma_4k_kdf_iter = "PRAGMA kdf_iter = 4000;";
960962
char *pragma_1x_and_4k;
963+
char *set_user_version;
961964
char *key;
962965
int key_sz;
966+
int user_version = 0;
963967
int upgrade_1x_format = 0;
964968
int upgrade_4k_format = 0;
965969
static const unsigned char aCopy[] = {
@@ -977,18 +981,18 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
977981
memcpy(key, ctx->read_ctx->pass, ctx->read_ctx->pass_sz);
978982

979983
if(db_filename){
980-
const char* commands[4];
981-
char *attach_command = sqlite3_mprintf("ATTACH DATABASE '%s-migrated' as migrate KEY '%s';",
984+
const char* commands[5];
985+
char *attach_command = sqlite3_mprintf("ATTACH DATABASE '%s-migrated' as migrate KEY '%q';",
982986
db_filename, key);
983987

984-
int rc = sqlcipher_check_connection(db_filename, key, key_sz, "");
988+
int rc = sqlcipher_check_connection(db_filename, key, key_sz, "", &user_version);
985989
if(rc == SQLITE_OK){
986990
CODEC_TRACE(("No upgrade required - exiting\n"));
987991
goto exit;
988992
}
989993

990994
// Version 2 - check for 4k with hmac format
991-
rc = sqlcipher_check_connection(db_filename, key, key_sz, pragma_4k_kdf_iter);
995+
rc = sqlcipher_check_connection(db_filename, key, key_sz, pragma_4k_kdf_iter, &user_version);
992996
if(rc == SQLITE_OK) {
993997
CODEC_TRACE(("Version 2 format found\n"));
994998
upgrade_4k_format = 1;
@@ -997,7 +1001,7 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
9971001
// Version 1 - check both no hmac and 4k together
9981002
pragma_1x_and_4k = sqlite3_mprintf("%s%s", pragma_hmac_off,
9991003
pragma_4k_kdf_iter);
1000-
rc = sqlcipher_check_connection(db_filename, key, key_sz, pragma_1x_and_4k);
1004+
rc = sqlcipher_check_connection(db_filename, key, key_sz, pragma_1x_and_4k, &user_version);
10011005
sqlite3_free(pragma_1x_and_4k);
10021006
if(rc == SQLITE_OK) {
10031007
CODEC_TRACE(("Version 1 format found\n"));
@@ -1010,10 +1014,12 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
10101014
goto handle_error;
10111015
}
10121016

1017+
set_user_version = sqlite3_mprintf("PRAGMA migrate.user_version = %d;", user_version);
10131018
commands[0] = upgrade_4k_format == 1 ? pragma_4k_kdf_iter : "";
10141019
commands[1] = upgrade_1x_format == 1 ? pragma_hmac_off : "";
10151020
commands[2] = attach_command;
10161021
commands[3] = "SELECT sqlcipher_export('migrate');";
1022+
commands[4] = set_user_version;
10171023

10181024
for(command_idx = 0; command_idx < ArraySize(commands); command_idx++){
10191025
const char *command = commands[command_idx];
@@ -1026,6 +1032,7 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
10261032
}
10271033
}
10281034
sqlite3_free(attach_command);
1035+
sqlite3_free(set_user_version);
10291036
sqlcipher_free(key, key_sz);
10301037

10311038
if(rc == SQLITE_OK){

0 commit comments

Comments
 (0)
0