Skip to content

Commit cdfd2a3

Browse files
committed
ensure that pragmas don't do anything unless a context is initialized on the db
1 parent 75f060e commit cdfd2a3

2 files changed

Lines changed: 45 additions & 15 deletions

File tree

src/crypto.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ int codec_set_kdf_iter(sqlite3* db, int nDb, int kdf_iter, int for_ctx) {
4545
if(pDb->pBt) {
4646
codec_ctx *ctx;
4747
sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx);
48-
return sqlcipher_codec_ctx_set_kdf_iter(ctx, kdf_iter, for_ctx);
48+
if(ctx) return sqlcipher_codec_ctx_set_kdf_iter(ctx, kdf_iter, for_ctx);
4949
}
5050
return SQLITE_ERROR;
5151
}
@@ -57,7 +57,7 @@ int codec_set_fast_kdf_iter(sqlite3* db, int nDb, int kdf_iter, int for_ctx) {
5757
if(pDb->pBt) {
5858
codec_ctx *ctx;
5959
sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx);
60-
return sqlcipher_codec_ctx_set_fast_kdf_iter(ctx, kdf_iter, for_ctx);
60+
if(ctx) return sqlcipher_codec_ctx_set_fast_kdf_iter(ctx, kdf_iter, for_ctx);
6161
}
6262
return SQLITE_ERROR;
6363
}
@@ -86,14 +86,14 @@ int codec_set_use_hmac(sqlite3* db, int nDb, int use) {
8686
int rc;
8787
codec_ctx *ctx;
8888
sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx);
89-
90-
rc = sqlcipher_codec_ctx_set_use_hmac(ctx, use);
91-
if(rc != SQLITE_OK) return rc;
92-
93-
/* since the use of hmac has changed, the page size may also change */
94-
/* Note: before forcing the page size we need to force pageSizeFixed to 0, else
89+
if(ctx) {
90+
rc = sqlcipher_codec_ctx_set_use_hmac(ctx, use);
91+
if(rc != SQLITE_OK) return rc;
92+
/* since the use of hmac has changed, the page size may also change */
93+
/* Note: before forcing the page size we need to force pageSizeFixed to 0, else
9594
sqliteBtreeSetPageSize will block the change */
96-
return codec_set_btree_to_codec_pagesize(db, pDb, ctx);
95+
return codec_set_btree_to_codec_pagesize(db, pDb, ctx);
96+
}
9797
}
9898
return SQLITE_ERROR;
9999
}
@@ -107,10 +107,11 @@ int codec_set_page_size(sqlite3* db, int nDb, int size) {
107107
codec_ctx *ctx;
108108
sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx);
109109

110-
rc = sqlcipher_codec_ctx_set_pagesize(ctx, size);
111-
if(rc != SQLITE_OK) return rc;
112-
113-
return codec_set_btree_to_codec_pagesize(db, pDb, ctx);
110+
if(ctx) {
111+
rc = sqlcipher_codec_ctx_set_pagesize(ctx, size);
112+
if(rc != SQLITE_OK) return rc;
113+
return codec_set_btree_to_codec_pagesize(db, pDb, ctx);
114+
}
114115
}
115116
return SQLITE_ERROR;
116117
}
@@ -128,7 +129,7 @@ int codec_set_cipher_name(sqlite3* db, int nDb, const char *cipher_name, int for
128129
if(pDb->pBt) {
129130
codec_ctx *ctx;
130131
sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx);
131-
return sqlcipher_codec_ctx_set_cipher(ctx, cipher_name, for_ctx);
132+
if(ctx) return sqlcipher_codec_ctx_set_cipher(ctx, cipher_name, for_ctx);
132133
}
133134
return SQLITE_ERROR;
134135
}
@@ -139,7 +140,7 @@ int codec_set_pass_key(sqlite3* db, int nDb, const void *zKey, int nKey, int for
139140
if(pDb->pBt) {
140141
codec_ctx *ctx;
141142
sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx);
142-
return sqlcipher_codec_ctx_set_pass(ctx, zKey, nKey, for_ctx);
143+
if(ctx) return sqlcipher_codec_ctx_set_pass(ctx, zKey, nKey, for_ctx);
143144
}
144145
return SQLITE_ERROR;
145146
}

test/crypto.test

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,5 +1115,34 @@ do_test vacuum {
11151115

11161116
} {10000 true 5000}
11171117
db close
1118+
file delete -force test.db
1119+
1120+
# test kdf_iter and other pragmas
1121+
# before a key is set. Verify that they
1122+
# are no-ops
1123+
do_test cipher-options-before-keys {
1124+
sqlite_orig db test.db
1125+
1126+
execsql {
1127+
PRAGMA kdf_iter = 1000;
1128+
PRAGMA cipher_page_size = 4096;
1129+
PRAGMA cipher = 'aes-128-cbc';
1130+
PRAGMA cipher_use_hmac = OFF;
1131+
PRAGMA key = 'testkey';
1132+
CREATE table t1(a,b);
1133+
INSERT INTO t1 VALUES(1,2);
1134+
}
1135+
db close
1136+
1137+
sqlite_orig db test.db
1138+
1139+
execsql {
1140+
PRAGMA key = 'testkey';
1141+
SELECT count(*) FROM t1;
1142+
}
1143+
1144+
} {1}
1145+
db close
1146+
file delete -force test.db
11181147

11191148
finish_test

0 commit comments

Comments
 (0)