Skip to content

Commit 750f5e3

Browse files
committed
Merge branch 'prerelease'
2 parents 3778346 + b7c53c8 commit 750f5e3

File tree

3 files changed

+39
-45
lines changed

3 files changed

+39
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44
## [4.2.0] - (May 2019 - [4.2.0 changes])
55
- Adds PRAGMA cipher_integrity_check to perform independent verification of page HMACs
66
- Updates baseline to upstream SQLite 3.28.0
7+
- Improves PRAGMA cipher_migrate to handle keys containing non-terminating zero bytes
78

89
## [4.1.0] - (March 2019 - [4.1.0 changes])
910
- Defer reading salt from header until key derivation is triggered

src/crypto_impl.c

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ static int sqlcipher_check_connection(const char *filename, char *key, int key_s
12561256

12571257
int sqlcipher_codec_ctx_integrity_check(codec_ctx *ctx, Parse *pParse, char *column) {
12581258
Pgno page = 1;
1259-
int i, trans_rc, rc = 0;
1259+
int i, rc = 0;
12601260
char *result;
12611261
unsigned char *hmac_out = NULL;
12621262
sqlite3_file *fd = sqlite3PagerFile(ctx->pBt->pBt->pPager);
@@ -1284,13 +1284,6 @@ int sqlcipher_codec_ctx_integrity_check(codec_ctx *ctx, Parse *pParse, char *col
12841284
goto cleanup;
12851285
}
12861286

1287-
/* establish an exclusive lock on the database */
1288-
if((trans_rc = sqlite3BtreeBeginTrans(ctx->pBt, 2, 0)) != SQLITE_OK) {
1289-
sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, "unable to lock database", P4_TRANSIENT);
1290-
sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1291-
goto cleanup;
1292-
}
1293-
12941287
sqlite3OsFileSize(fd, &file_sz);
12951288
hmac_out = sqlcipher_malloc(ctx->hmac_sz);
12961289

@@ -1330,7 +1323,6 @@ int sqlcipher_codec_ctx_integrity_check(codec_ctx *ctx, Parse *pParse, char *col
13301323
}
13311324

13321325
cleanup:
1333-
if(trans_rc == SQLITE_OK) sqlite3BtreeRollback(ctx->pBt, SQLITE_OK, 0);
13341326
if(hmac_out != NULL) sqlcipher_free(hmac_out, ctx->hmac_sz);
13351327
return SQLITE_OK;
13361328
}
@@ -1342,7 +1334,6 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
13421334
const char *db_filename = sqlite3_db_filename(db, "main");
13431335
char *set_user_version = NULL, *pass = NULL, *attach_command = NULL, *migrated_db_filename = NULL, *keyspec = NULL, *temp = NULL, *journal_mode = NULL, *set_journal_mode = NULL, *pragma_compat = NULL;
13441336
Btree *pDest = NULL, *pSrc = NULL;
1345-
const char* commands[5];
13461337
sqlite3_file *srcfile, *destfile;
13471338
#if defined(_WIN32) || defined(SQLITE_OS_WINRT)
13481339
LPWSTR w_db_filename = NULL, w_migrated_db_filename = NULL;
@@ -1389,23 +1380,46 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
13891380
memcpy(migrated_db_filename, temp, sqlite3Strlen30(temp));
13901381
sqlcipher_free(temp, sqlite3Strlen30(temp));
13911382

1392-
attach_command = sqlite3_mprintf("ATTACH DATABASE '%s' as migrate KEY '%q';", migrated_db_filename, pass);
1383+
attach_command = sqlite3_mprintf("ATTACH DATABASE '%s' as migrate;", migrated_db_filename, pass);
13931384
set_user_version = sqlite3_mprintf("PRAGMA migrate.user_version = %d;", user_version);
13941385

1395-
commands[0] = pragma_compat;
1396-
commands[1] = "PRAGMA journal_mode = delete;"; /* force journal mode to DELETE, we will set it back later if different */
1397-
commands[2] = attach_command;
1398-
commands[3] = "SELECT sqlcipher_export('migrate');";
1399-
commands[4] = set_user_version;
1400-
1401-
for(i = 0; i < ArraySize(commands); i++){
1402-
rc = sqlite3_exec(db, commands[i], NULL, NULL, NULL);
1403-
if(rc != SQLITE_OK){
1404-
CODEC_TRACE("migration step %d failed error code %d\n", i, rc);
1405-
goto handle_error;
1406-
}
1386+
rc = sqlite3_exec(db, pragma_compat, NULL, NULL, NULL);
1387+
if(rc != SQLITE_OK){
1388+
CODEC_TRACE("set compatibility mode failed, error code %d\n", rc);
1389+
goto handle_error;
14071390
}
1408-
1391+
1392+
/* force journal mode to DELETE, we will set it back later if different */
1393+
rc = sqlite3_exec(db, "PRAGMA journal_mode = delete;", NULL, NULL, NULL);
1394+
if(rc != SQLITE_OK){
1395+
CODEC_TRACE("force journal mode DELETE failed, error code %d\n", rc);
1396+
goto handle_error;
1397+
}
1398+
1399+
rc = sqlite3_exec(db, attach_command, NULL, NULL, NULL);
1400+
if(rc != SQLITE_OK){
1401+
CODEC_TRACE("attach failed, error code %d\n", rc);
1402+
goto handle_error;
1403+
}
1404+
1405+
rc = sqlite3_key_v2(db, "migrate", pass, pass_sz);
1406+
if(rc != SQLITE_OK){
1407+
CODEC_TRACE("keying attached database failed, error code %d\n", rc);
1408+
goto handle_error;
1409+
}
1410+
1411+
rc = sqlite3_exec(db, "SELECT sqlcipher_export('migrate');", NULL, NULL, NULL);
1412+
if(rc != SQLITE_OK){
1413+
CODEC_TRACE("sqlcipher_export failed, error code %d\n", rc);
1414+
goto handle_error;
1415+
}
1416+
1417+
rc = sqlite3_exec(db, set_user_version, NULL, NULL, NULL);
1418+
if(rc != SQLITE_OK){
1419+
CODEC_TRACE("set user version failed, error code %d\n", rc);
1420+
goto handle_error;
1421+
}
1422+
14091423
if( !db->autoCommit ){
14101424
CODEC_TRACE("cannot migrate from within a transaction");
14111425
goto handle_error;

test/sqlcipher-integrity.test

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -308,25 +308,4 @@ do_test integrity-check-plaintext-header {
308308
} {{} 1 {{HMAC verification failed for page 1} {HMAC verification failed for page 2}}}
309309
file delete -force test.db
310310

311-
# verify database locking for cipher_integrity_check
312-
do_test integrity-check-locking {
313-
sqlite_orig db test.db
314-
sqlite_orig db2 test.db
315-
316-
execsql {
317-
PRAGMA key = 'test';
318-
CREATE TABLE t1(a,b);
319-
BEGIN EXCLUSIVE;
320-
INSERT INTO t1(a,b) VALUES (1,2);
321-
}
322-
323-
execsql {
324-
PRAGMA key = 'test';
325-
PRAGMA cipher_integrity_check;
326-
} db2
327-
} {{unable to lock database}}
328-
sqlite_orig db test.db
329-
sqlite_orig db2 test.db
330-
file delete -force test.db
331-
332311
finish_test

0 commit comments

Comments
 (0)