Skip to content

Commit 48ae316

Browse files
committed
raise error when trying to rekey unencrypted database, or use an empty key
1 parent e44eb58 commit 48ae316

File tree

4 files changed

+26
-59
lines changed

4 files changed

+26
-59
lines changed

src/crypto.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ int sqlite3_rekey_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey) {
971971
if(ctx == NULL) {
972972
/* there was no codec attached to this database, so this should do nothing! */
973973
sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlite3_rekey_v2: no codec attached to db, exiting");
974-
return SQLITE_OK;
974+
return SQLITE_MISUSE;
975975
}
976976

977977
sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlite3_rekey_v2: entering database mutex %p", db->mutex);

src/pragma.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2621,6 +2621,12 @@ void sqlite3Pragma(
26212621
sqlite3VdbeSetNumCols(v, 1);
26222622
sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "ok", SQLITE_STATIC);
26232623
returnSingleText(v, "ok");
2624+
} else {
2625+
sqlite3ErrorMsg(pParse, "An error occurred with PRAGMA key or rekey. "
2626+
"PRAGMA key requires a key of one or more characters. "
2627+
"PRAGMA rekey can only be run on an existing encrypted database. "
2628+
"Use sqlcipher_export() and ATTACH to convert encrypted/plaintext databases.");
2629+
goto pragma_out;
26242630
}
26252631
}
26262632
break;

test/sqlcipher-core.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,19 @@ do_test test_flags_combo {
877877
} {0 5 0}
878878
db close
879879

880+
# test empty key
881+
# it should raise an error
882+
do_test empty-key {
883+
sqlite_orig db test.db
884+
885+
catchsql {
886+
PRAGMA key = '';
887+
}
888+
889+
} {1 {An error occurred with PRAGMA key or rekey. PRAGMA key requires a key of one or more characters. PRAGMA rekey can only be run on an existing encrypted database. Use sqlcipher_export() and ATTACH to convert encrypted/plaintext databases.}}
890+
db close
891+
file delete -force test.db
892+
880893
# configure URI filename support
881894
# create a new encrypted database with the key via parameter
882895
# close database

test/sqlcipher-rekey.test

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -38,72 +38,20 @@ set testdir [file dirname $argv0]
3838
source $testdir/tester.tcl
3939
source $testdir/sqlcipher.tcl
4040

41-
# Test rekey as first operation on an empty database. should be a no-op
42-
do_test rekey-as-first-op {
41+
# Test rekey as first operation on an empty database
42+
# it should raise an error
43+
do_test rekey-as-first-op-on-empty {
4344
sqlite_orig db test.db
4445

45-
execsql {
46+
catchsql {
4647
PRAGMA rekey = 'testkey';
47-
CREATE table t1(a,b);
48-
BEGIN;
49-
}
50-
51-
for {set i 1} {$i<=100} {incr i} {
52-
set r [expr {int(rand()*500000)}]
53-
execsql "INSERT INTO t1 VALUES($i,'value $r');"
5448
}
5549

56-
execsql {
57-
COMMIT;
58-
}
59-
60-
db close
61-
sqlite_orig db test.db
62-
63-
execsql {
64-
PRAGMA rekey = 'testkey';
65-
SELECT count(*) FROM t1;
66-
}
67-
68-
} {ok 100}
50+
} {1 {An error occurred with PRAGMA key or rekey. PRAGMA key requires a key of one or more characters. PRAGMA rekey can only be run on an existing encrypted database. Use sqlcipher_export() and ATTACH to convert encrypted/plaintext databases.}}
6951
db close
7052
file delete -force test.db
7153

72-
# Test rekey as first operation follwed by key
73-
do_test rekey-then-key-as-first-ops {
74-
sqlite_orig db test.db
75-
76-
execsql {
77-
PRAGMA rekey = '1234';
78-
PRAGMA key = 'testkey';
79-
CREATE table t1(a,b);
80-
BEGIN;
81-
}
82-
83-
for {set i 1} {$i<=100} {incr i} {
84-
set r [expr {int(rand()*500000)}]
85-
execsql "INSERT INTO t1 VALUES($i,'value $r');"
86-
}
87-
88-
execsql {
89-
COMMIT;
90-
}
91-
92-
db close
93-
sqlite_orig db test.db
94-
95-
execsql {
96-
PRAGMA rekey = '4321';
97-
PRAGMA key = 'testkey';
98-
SELECT count(*) FROM t1;
99-
}
100-
101-
} {ok ok 100}
102-
db close
103-
file delete -force test.db
104-
105-
106-
# test a rekey operation as the first op on a database
54+
# test a rekey operation as the first op on an existing database
10755
# then test that now the new key opens the database
10856
# now close database re-open with new key
10957
setup test.db "'testkey'"

0 commit comments

Comments
 (0)