Adjustments to cipher_migrate

- And format flags when checking for exit condition
- Pass key size to sqlcipher_check_connection
- Trace version upgrade logic
This commit is contained in:
Nick Parker 2013-08-13 14:53:08 -05:00
parent c3f6cf1e02
commit 6f4a0f2d8d
2 changed files with 18 additions and 23 deletions

View File

@ -896,7 +896,6 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
BTREE_USER_VERSION, 0, /* Preserve the user version */
BTREE_APPLICATION_ID, 0, /* Preserve the application id */
};
key_sz = ctx->read_ctx->pass_sz + 1;
key = sqlcipher_malloc(key_sz);
memset(key, 0, key_sz);
@ -907,35 +906,34 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
char *attach_command = sqlite3_mprintf("ATTACH DATABASE '%s-migrated' as migrate KEY '%s';",
db_filename, key);
int rc = sqlcipher_check_connection(db_filename, key, "");
int rc = sqlcipher_check_connection(db_filename, key, key_sz, "");
if(rc == SQLITE_OK){
// no upgrade required
CODEC_TRACE(("No upgrade required - exiting\n"));
goto exit;
}
// check for 1x format
//rc = sqlcipher_check_connection(db_filename, key, pragma_hmac_off);
//if(rc == SQLITE_OK) {
// upgrade_1x_format = 1;
//}
// Version 2 - check for 4k with hmac format
rc = sqlcipher_check_connection(db_filename, key, pragma_4k_kdf_iter);
rc = sqlcipher_check_connection(db_filename, key, key_sz, pragma_4k_kdf_iter);
if(rc == SQLITE_OK) {
CODEC_TRACE(("Version 2 format found\n"));
upgrade_4k_format = 1;
}
// Version 1 - check both no hmac and 4k together
char *pragma_1x_and_4k = sqlite3_mprintf("%s%s", pragma_hmac_off,
pragma_4k_kdf_iter);
rc = sqlcipher_check_connection(db_filename, key, pragma_1x_and_4k);
rc = sqlcipher_check_connection(db_filename, key, key_sz, pragma_1x_and_4k);
sqlite3_free(pragma_1x_and_4k);
if(rc == SQLITE_OK) {
CODEC_TRACE(("Version 1 format found\n"));
upgrade_1x_format = 1;
upgrade_4k_format = 1;
}
if(upgrade_1x_format == 0 || upgrade_4k_format == 0) goto handle_error;
if(upgrade_1x_format == 0 && upgrade_4k_format == 0) {
CODEC_TRACE(("Upgrade format not determined\n"));
goto handle_error;
}
const char *commands[] = {
upgrade_4k_format == 1 ? pragma_4k_kdf_iter : "",
@ -1016,31 +1014,33 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
sqlite3ResetAllSchemasOfConnection(db);
remove(migrated_db_filename);
sqlite3_free(migrated_db_filename);
} else {
CODEC_TRACE(("*** migration failure** \n\n"));
}
}
goto exit;
handle_error:
CODEC_TRACE(("an error occurred\n"));
CODEC_TRACE(("An error occurred attempting to migrate the database\n"));
rc = SQLITE_ERROR;
exit:
return rc;
}
int sqlcipher_check_connection(char *filename, char *key, char *sql) {
int sqlcipher_check_connection(char *filename, char *key, int key_sz, char *sql) {
int rc;
sqlite3 *db;
char *errMsg;
sqlite3_stmt *statement;
int status = SQLITE_ERROR;
char *query_sqlite_master = "SELECT count(*) FROM sqlite_master;";
rc = sqlite3_open(filename, &db);
if(rc != SQLITE_OK){
goto cleanup;
}
rc = sqlite3_key(db, key, (int)strlen(key));
rc = sqlite3_key(db, key, key_sz);
if(rc != SQLITE_OK){
goto cleanup;
}
@ -1053,7 +1053,7 @@ int sqlcipher_check_connection(char *filename, char *key, char *sql) {
goto cleanup;
}
if(sqlite3_step(statement) == SQLITE_ROW){
status = SQLITE_OK;
rc = SQLITE_OK;
}
goto cleanup;
@ -1066,7 +1066,7 @@ cleanup:
}
exit:
return status;
return rc;
}

View File

@ -1960,8 +1960,6 @@ db close
file delete -force test.db
do_test migrate-1.1.8-database-to-3x-format {
file delete -force test.db-migrated
file delete -force test.db
file copy -force sqlcipher-1.1.8-testkey.db test.db
sqlite_orig db test.db
execsql {
@ -1978,10 +1976,8 @@ do_test migrate-1.1.8-database-to-3x-format {
} {1}
db close
file delete -force test.db
file delete -force test.db-migrated
do_test migrate-2-0-le-database-to-3x-format {
file delete -force test.db
file copy -force sqlcipher-2.0-le-testkey.db test.db
sqlite_orig db test.db
execsql {
@ -1998,6 +1994,5 @@ do_test migrate-2-0-le-database-to-3x-format {
} {1}
db close
file delete -force test.db
file delete -force test.db-migrated
finish_test