8000 fix initialization of OpenSSL so an encrypted database can be attache… · devstator82/sqlcipher@b38a9d2 · GitHub
[go: up one dir, main page]

Skip to content

Commit b38a9d2

Browse files
committed
fix initialization of OpenSSL so an encrypted database can be attached to a non-encrypted main database; add test for the same
1 parent 982f416 commit b38a9d2

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/crypto.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ typedef struct {
7070
cipher_ctx *write_ctx;
7171
} codec_ctx;
7272

73+
static void activate_openssl() {
74+
if(EVP_get_cipherbyname(CIPHER) == NULL) {
75+
OpenSSL_add_all_algorithms();
76+
}
77+
}
7378

7479
/*
7580
** Simple routines for converting hex char strings to binary data
@@ -80,7 +85,7 @@ static int cipher_hex2int(char c) {
8085
(c>='a' && c<='f') ? (c)-'a'+10 : 0;
8186
}
8287

83-
void cipher_hex2bin(const char *hex, int sz, unsigned char *out){
88+
static void cipher_hex2bin(const char *hex, int sz, unsigned char *out){
8489
int i;
8590
for(i = 0; i < sz; i += 2){
8691
out[i/2] = (cipher_hex2int(hex[i])<<4) | cipher_hex2int(hex[i+1]);
@@ -410,6 +415,7 @@ int sqlite3CodecAttach(sqlite3* db, int nDb, const void *zKey, int nKey) {
410415
struct Db *pDb = &db->aDb[nDb];
411416

412417
CODEC_TRACE(("sqlite3CodecAttach: entered nDb=%d zKey=%s, nKey=%d\n", nDb, zKey, nKey));
418+
activate_openssl();
413419

414420
if(nKey && zKey && pDb->pBt) {
415421
codec_ctx *ctx;
@@ -471,7 +477,6 @@ void sqlite3_activate_see(const char* in) {
471477

472478
int sqlite3_key(sqlite3 *db, const void *pKey, int nKey) {
473479
CODEC_TRACE(("sqlite3_key: entered db=%d pKey=%s nKey=%d\n", db, pKey, nKey));
474-
OpenSSL_add_all_algorithms();
475480
/* attach key if db and pKey are not null and nKey is > 0 */
476481
if(db && pKey && nKey) {
477482
sqlite3CodecAttach(db, 0, pKey, nKey); // operate only on the main db
@@ -496,7 +501,7 @@ int sqlite3_key(sqlite3 *db, const void *pKey, int nKey) {
496501
*/
497502
int sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey) {
498503
CODEC_TRACE(("sqlite3_rekey: entered db=%d pKey=%s, nKey=%d\n", db, pKey, nKey));
499-
OpenSSL_add_all_algorithms();
504+
activate_openssl();
500505
if(db && pKey && nKey) {
501506
struct Db *pDb = &db->aDb[0];
502507
CODEC_TRACE(("sqlite3_rekey: database pDb=%d\n", pDb));

test/crypto.test

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,37 @@ do_test codec-1.16 {
444444
db close
445445
file delete -force test.db
446446

447+
# create an unencrypted database, attach a new encrypted volume
448+
# copy data between, verify the encypted database is good afterwards
449+
do_test unencryped-attach {
450+
sqlite_orig db test.db
451+
452+
execsql {
453+
CREATE TABLE t1(a,b);
454+
BEGIN;
455+
}
456+
457+
for {set i 1} {$i<=1000} {incr i} {
458+
set r [expr {int(rand()*500000)}]
459+
execsql "INSERT INTO t1 VALUES($i,$r);"
460+
}
461+
462+
execsql {
463+
COMMIT;
464+
ATTACH DATABASE 'test2.db' AS db2 KEY 'testkey';
465+
CREATE TABLE db2.t1(a,b);
466+
INSERT INTO db2.t1 SELECT * FROM t1;
467+
DETACH DATABASE db2;
468+
}
469+
470+
sqlite_orig db2 test2.db
471+
execsql {
472+
PRAGMA key='testkey';
473+
SELECT count(*) FROM t1;
474+
} db2
475+
} {1000}
476+
db2 close
477+
file delete -force test.db
478+
file delete -force test2.db
479+
447480
finish_test

0 commit comments

Comments
 (0)
0