Skip to content

Commit c14afcf

Browse files
Prevent segfault when invalid cipher name provided, identified by Nicholas Starke
1 parent 388970a commit c14afcf

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/crypto_openssl.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,11 @@ static int sqlcipher_openssl_cipher(void *ctx, int mode, unsigned char *key, int
186186

187187
static int sqlcipher_openssl_set_cipher(void *ctx, const char *cipher_name) {
188188
openssl_ctx *o_ctx = (openssl_ctx *)ctx;
189-
o_ctx->evp_cipher = (EVP_CIPHER *) EVP_get_cipherbyname(cipher_name);
190-
return SQLITE_OK;
189+
EVP_CIPHER* cipher = (EVP_CIPHER *) EVP_get_cipherbyname(cipher_name);
190+
if(cipher != NULL) {
191+
o_ctx->evp_cipher = cipher;
192+
}
193+
return cipher != NULL ? SQLITE_OK : SQLITE_ERROR;
191194
}
192195

193196
static const char* sqlcipher_openssl_get_cipher(void *ctx) {

test/crypto.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,6 +1922,18 @@ 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+
19251937
# verify the pragma cipher
19261938
# reports the default value
19271939
if_built_with_openssl verify-pragma-cipher-default {

0 commit comments

Comments
 (0)