Skip to content

Commit 3703f36

Browse files
committed
rework attach to require explicit key
1 parent ed1e161 commit 3703f36

2 files changed

Lines changed: 31 additions & 19 deletions

File tree

src/crypto.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,9 @@ int sqlite3CodecAttach(sqlite3* db, int nDb, const void *zKey, int nKey) {
314314
sqlite3BtreeSetAutoVacuum(pDb->pBt, SQLITE_DEFAULT_AUTOVACUUM);
315315
}
316316
sqlite3_mutex_leave(db->mutex);
317+
return SQLITE_OK;
317318
}
318-
return SQLITE_OK;
319+
return SQLITE_ERROR;
319320
}
320321

321322
void sqlite3_activate_see(const char* in) {
@@ -409,18 +410,8 @@ int sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey) {
409410
void sqlite3CodecGetKey(sqlite3* db, int nDb, void **zKey, int *nKey) {
410411
struct Db *pDb = &db->aDb[nDb];
411412
CODEC_TRACE(("sqlite3CodecGetKey: entered db=%p, nDb=%d\n", db, nDb));
412-
413-
if( pDb->pBt ) {
414-
codec_ctx *ctx;
415-
sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx);
416-
417-
if(ctx) { /* if the codec has an attached codec_context user the raw key data */
418-
sqlcipher_codec_get_pass(ctx, zKey, nKey);
419-
} else {
420-
*zKey = NULL;
421-
*nKey = 0;
422-
}
423-
}
413+
*zKey = NULL;
414+
*nKey = 0;
424415
}
425416

426417

test/crypto.test

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,30 @@ do_test rekey-delete-and-query-wal-3 {
381381
db close
382382
file delete -force test.db
383383

384+
# attach an encrypted database
385+
# without specifying key, verify it fails
386+
setup test.db "'testkey'"
387+
do_test attach-database-with-default-key {
388+
sqlite_orig db2 test2.db
389+
390+
execsql {
391+
PRAGMA key = 'testkey';
392+
CREATE TABLE t2(a,b);
393+
INSERT INTO t2 VALUES ('test1', 'test2');
394+
} db2
395+
396+
catchsql {
397+
ATTACH 'test.db' AS db;
398+
} db2
399+
400+
} {1 {unable to open database: test.db}}
401+
db2 close
402+
file delete -force test.db
403+
file delete -force test2.db
404+
384405
# attach an encrypted database
385406
# where both database have the same
386-
# key
407+
# key explicitly
387408
setup test.db "'testkey'"
388409
do_test attach-database-with-same-key {
389410
sqlite_orig db2 test2.db
@@ -396,7 +417,7 @@ do_test attach-database-with-same-key {
396417

397418
execsql {
398419
SELECT count(*) FROM t2;
399-
ATTACH 'test.db' AS db;
420+
ATTACH 'test.db' AS db KEY 'testkey';
400421
SELECT count(*) FROM db.t1;
401422
} db2
402423

@@ -581,7 +602,7 @@ file delete -force test.db
581602

582603
# create an unencrypted database, attach a new encrypted volume
583604
# copy data between, verify the encypted database is good afterwards
584-
do_test unencryped-attach {
605+
do_test unencrypted-attach {
585606
sqlite_orig db test.db
586607

587608
execsql {
@@ -1335,7 +1356,7 @@ do_test default-use-hmac-attach {
13351356
PRAGMA cipher_default_use_hmac = OFF;
13361357
PRAGMA key = 'testkey';
13371358
SELECT count(*) FROM t1;
1338-
ATTACH 'sqlcipher-1.1.8-testkey.db' AS db2;
1359+
ATTACH 'sqlcipher-1.1.8-testkey.db' AS db2 KEY 'testkey';
13391360
SELECT count(*) from db2.t1;
13401361
PRAGMA cipher_default_use_hmac = ON;
13411362
}
@@ -1352,7 +1373,7 @@ do_test attach-1.1.8-database-from-2.0-fails {
13521373
catchsql {
13531374
PRAGMA key = 'testkey';
13541375
CREATE table t1(a,b);
1355-
ATTACH 'sqlcipher-1.1.8-testkey.db' AS db2;
1376+
ATTACH 'sqlcipher-1.1.8-testkey.db' AS db2 KEY 'testkey';
13561377
}
13571378
} {1 {file is encrypted or is not a database}}
13581379
db close
@@ -1376,7 +1397,7 @@ do_test change-default-use-hmac-attach {
13761397
PRAGMA key = 'testkey';
13771398
SELECT count(*) FROM t1;
13781399
PRAGMA cipher_default_use_hmac = OFF;
1379-
ATTACH 'sqlcipher-1.1.8-testkey.db' AS db2;
1400+
ATTACH 'sqlcipher-1.1.8-testkey.db' AS db2 KEY 'testkey';
13801401
SELECT count(*) from db2.t1;
13811402
PRAGMA cipher_default_use_hmac = ON;
13821403
}

0 commit comments

Comments
 (0)