Skip to content

Commit

Permalink
null termination of key value
Browse files Browse the repository at this point in the history
  • Loading branch information
sjlombardo committed Aug 13, 2013
1 parent dce806a commit c3f6cf1
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/crypto_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,8 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
char *query_sqlite_master = "SELECT count(*) from sqlite_master;";
char *pragma_hmac_off = "PRAGMA cipher_use_hmac = OFF;";
char *pragma_4k_kdf_iter = "PRAGMA kdf_iter = 4000;";
char *key = ctx->read_ctx->pass;
char *key;
int key_sz;
int upgrade_1x_format = 0;
int upgrade_4k_format = 0;
sqlite3 *test;
Expand All @@ -896,6 +897,11 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
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);
memcpy(key, ctx->read_ctx->pass, ctx->read_ctx->pass_sz);

if(db_filename){

char *attach_command = sqlite3_mprintf("ATTACH DATABASE '%s-migrated' as migrate KEY '%s';",
Expand All @@ -908,18 +914,18 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
}

// check for 1x format
rc = sqlcipher_check_connection(db_filename, key, pragma_hmac_off);
if(rc == SQLITE_OK) {
upgrade_1x_format = 1;
}
//rc = sqlcipher_check_connection(db_filename, key, pragma_hmac_off);
//if(rc == SQLITE_OK) {
// upgrade_1x_format = 1;
//}

// check for 4k format
// Version 2 - check for 4k with hmac format
rc = sqlcipher_check_connection(db_filename, key, pragma_4k_kdf_iter);
if(rc == SQLITE_OK) {
upgrade_4k_format = 1;
}

// check both 1x and 4k together
// 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);
Expand All @@ -929,6 +935,8 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
upgrade_4k_format = 1;
}

if(upgrade_1x_format == 0 || upgrade_4k_format == 0) goto handle_error;

const char *commands[] = {
upgrade_4k_format == 1 ? pragma_4k_kdf_iter : "",
upgrade_1x_format == 1 ? pragma_hmac_off : "",
Expand All @@ -946,6 +954,7 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
}
}
sqlite3_free(attach_command);
sqlcipher_free(key, key_sz);

if(rc == SQLITE_OK){
if( !db->autoCommit ){
Expand Down

0 comments on commit c3f6cf1

Please sign in to comment.