8000 Prevent writing database with cipher set to invalid value · magic-coder/sqlcipher@c1f7fe7 · GitHub
[go: up one dir, main page]

Skip to content

Commit c1f7fe7

Browse files
Prevent writing database with cipher set to invalid value
1 parent 92ce336 commit c1f7fe7

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

src/crypto.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
143143
if( sqlite3StrICmp(zLeft, "cipher")==0 ){
144144
if(ctx) {
145145
if( zRight ) {
146-
sqlcipher_codec_ctx_set_cipher(ctx, zRight, 2); // change cipher for both
146+
rc = sqlcipher_codec_ctx_set_cipher(ctx, zRight, 2); // change cipher for both
147147
char *pragma_cipher_deprecated_msg = "PRAGMA cipher command is deprecated, please remove from usage.";
148148
codec_vdbe_return_static_string(pParse, "cipher", pragma_cipher_deprecated_msg);
149149
sqlite3_log(SQLITE_WARNING, pragma_cipher_deprecated_msg);
150-
return SQLITE_ERROR;
150+
return rc;
151151
}else {
152152
codec_vdbe_return_static_string(pParse, "cipher",
153153
sqlcipher_codec_ctx_get_cipher(ctx, 2));

src/crypto_impl.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,11 @@ int sqlcipher_codec_ctx_set_cipher(codec_ctx *ctx, const char *cipher_name, int
494494
cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
495495
int rc;
496496

497-
c_ctx->provider->set_cipher(c_ctx->provider_ctx, cipher_name);
498-
497+
rc = c_ctx->provider->set_cipher(c_ctx->provider_ctx, cipher_name);
498+
if(rc != SQLITE_OK){
499+
sqlcipher_codec_ctx_set_error(ctx, rc);
500+
return rc;
501+
}
499502
c_ctx->key_sz = c_ctx->provider->get_key_sz(c_ctx->provider_ctx);
500503
c_ctx->iv_sz = c_ctx->provider->get_iv_sz(c_ctx->provider_ctx);
501504
c_ctx->block_sz = c_ctx->provider->get_block_sz(c_ctx->provider_ctx);

test/crypto.test

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ do_test non-standard-kdf-and-ciphers {
614614
SELECT count(*) FROM t1;
615615
}
616616

617-
} {2}
617+
} {{PRAGMA cipher command is deprecated, please remove from usage.} 2}
618618
db close
619619
file delete -force test.db
620620

@@ -649,7 +649,7 @@ do_test rekey-from-cbc-to-ecb-no-iv {
649649
SELECT count(*) FROM t1;
650650
}
651651

652-
} {1001}
652+
} {{PRAGMA cipher command is deprecated, please remove from usage.} 1001}
653653
db close
654654
file delete -force test.db
655655

@@ -1069,7 +1069,7 @@ do_test attached-database-pragmas {
10691069
PRAGMA cipher_use_hmac = OFF;
10701070
SELECT count(*) FROM t1;
10711071
}
1072-
} {1000}
1072+
} {{PRAGMA cipher command is deprecated, please remove from usage.} 1000}
10731073
db close
10741074
file delete -force test.db
10751075
file delete -force test2.db
@@ -1541,7 +1541,7 @@ do_test verify-pragma-cipher-version {
15411541
execsql {
15421542
PRAGMA cipher_version;
15431543
}
1544-
} {3.3.1}
1544+
} {3.4.0}
15451545
db close
15461546
file delete -force test.db
15471547

@@ -1922,18 +1922,6 @@ do_test verify-pragma-cipher-page-size-changed {
19221922
db close
19231923
file delete -force test.db
19241924

1925-
# verify invalid cipher does not cause segfault
1926-
if_built_with_openssl verify-invalid-cipher-does-not-segfault {
1927-
sqlite_orig db test.db
1928-
execsql {
1929-
PRAGMA key = 'test';
1930-
PRAGMA cipher = 'junk';
1931-
PRAGMA cipher;
1932-
}
1933-
} {AES-256-CBC}
1934-
db close
1935-
file delete -force test.db
1936-
19371925
# verify setting cipher_store_pass before key
19381926
# does not cause segfault
19391927
do_test verify-cipher-store-pass-before-key-does-not-segfault {
@@ -1967,7 +1955,7 @@ if_built_with_openssl verify-pragma-cipher-changed {
19671955
PRAGMA cipher = 'AES-256-ECB';
19681956
PRAGMA cipher;
19691957
}
1970-
} {AES-256-ECB}
1958+
} {{PRAGMA cipher command is deprecated, please remove from usage.} AES-256-ECB}
19711959
db close
19721960
file delete -force test.db
19731961

@@ -2302,5 +2290,26 @@ do_test attach_database_with_non_default_page_size {
23022290
db close
23032291
file delete -force test.db test2.db
23042292

2293+
if_built_with_openssl wont-write-database-with-invalid-cipher {
2294+
sqlite_orig db test.db
2295+
catchsql {
2296+
PRAGMA key = 'test';
2297+
PRAGMA cipher = 'foobar';
2298+
CREATE TABLE t1(a,b);
2299+
}
2300+
} {1 {SQL logic error or missing database}}
2301+
db close
2302+
file delete -force test.db
2303+
2304+
if_built_with_openssl wont-write-database-with-invalid-cipher-2 {
2305+
sqlite_orig db test.db
2306+
execsql {
2307+
PRAGMA key = 'test';
2308+
PRAGMA cipher = 'foobar';
2309+
}
2310+
} {{PRAGMA cipher command is deprecated, please remove from usage.}}
2311+
db close
2312+
file delete -force test.db
2313+
23052314
sqlite3_test_control_pending_byte $old_pending_byte
23062315
finish_test

0 commit comments

Comments
 (0)
0