normalize attach behavior when key is not yet derived

This commit is contained in:
Stephen Lombardo 2019-01-15 13:15:51 -05:00
parent e72b34b24b
commit 42e655bf35
2 changed files with 67 additions and 3 deletions

View File

@ -813,10 +813,12 @@ void sqlite3CodecGetKey(sqlite3* db, int nDb, void **zKey, int *nKey) {
codec_ctx *ctx = (codec_ctx*) sqlite3PagerGetCodec(pDb->pBt->pBt->pPager);
if(ctx) {
if(sqlcipher_codec_get_store_pass(ctx) == 1) {
/* pass back the keyspec from the codec, unless PRAGMA cipher_store_pass
is set or keyspec has not yet been derived, in which case pass
back the password key material */
sqlcipher_codec_get_keyspec(ctx, zKey, nKey);
if(sqlcipher_codec_get_store_pass(ctx) == 1 || *zKey == NULL) {
sqlcipher_codec_get_pass(ctx, zKey, nKey);
} else {
sqlcipher_codec_get_keyspec(ctx, zKey, nKey);
}
} else {
*zKey = NULL;

View File

@ -225,6 +225,68 @@ db2 close
file delete -force test.db
file delete -force test2.db
# attach an empty encrypted database as the first op
# on a keyed database and verify different
# salts but same keys (because derivation of the key spec
# has not occured yet)
setup test.db "'testkey'"
do_test attach-empty-database-with-default-key-first-op {
sqlite_orig db test.db
set rc {}
execsql {
PRAGMA key='testkey';
ATTACH DATABASE 'test2.db' AS test;
CREATE TABLE test.t1(a,b);
INSERT INTO test.t1 SELECT * FROM t1;
DETACH DATABASE test;
}
sqlite_orig db2 test2.db
lappend rc [execsql {
PRAGMA key='testkey';
SELECT count(*) FROM t1;
} db2]
lappend rc [string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16]]
} {1 0}
db close
db2 close
file delete -force test.db
file delete -force test2.db
# attach an empty encrypted database
# on a keyed database when PRAGMA cipher_store_pass = 1
# and verify different salts
setup test.db "'testkey'"
do_test attach-empty-database-with-cipher-store-pass {
sqlite_orig db test.db
set rc {}
execsql {
PRAGMA key='testkey';
PRAGMA cipher_store_pass = 1;
INSERT INTO t1(a,b) VALUES (1,2);
ATTACH DATABASE 'test2.db' AS test;
CREATE TABLE test.t1(a,b);
INSERT INTO test.t1 SELECT * FROM t1;
DETACH DATABASE test;
}
sqlite_orig db2 test2.db
lappend rc [execsql {
PRAGMA key='testkey';
SELECT count(*) FROM t1;
} db2]
lappend rc [string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16]]
} {2 0}
db close
db2 close
file delete -force test.db
file delete -force test2.db
# attach an encrypted database
# without specifying key, verify it attaches
# correctly when PRAGMA cipher_store_pass = 1