Skip to content

Commit 50376d0

Browse files
committed
Merge branch 'prerelease'
2 parents 87b4a1e + a5374d8 commit 50376d0

14 files changed

Lines changed: 263 additions & 67 deletions

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# SQLCipher Change Log
22
All notable changes to this project will be documented in this file.
33

4+
## [4.4.2] - (November 2020 - [4.4.2 changes])
5+
- Improve error handling to resolve potential corruption if an encryption operation failed while operating in WAL mode
6+
- Changes to OpenSSL library cryptographic provider to reduce initialization complexity
7+
- Adjust cipher_integrity_check to skip locking page to avoid a spurious error report for very large databases
8+
- Miscellaneous code and comment cleanup
9+
410
## [4.4.1] - (October 2020 - [4.4.1 changes])
511
- Updates baseline to upstream SQLite 3.33.0
612
- Fixes double-free bug in cipher_default_plaintext_header_size
@@ -171,7 +177,9 @@ All notable changes to this project will be documented in this file.
171177
### Security
172178
- Change KDF iteration length from 4,000 to 64,000
173179

174-
[unreleased]: https://github.com/sqlcipher/sqlcipher/compare/v4.4.1...prerelease
180+
[unreleased]: https://github.com/sqlcipher/sqlcipher/compare/v4.4.2...prerelease
181+
[4.4.2]: https://github.com/sqlcipher/sqlcipher/tree/v4.4.2
182+
[4.4.2 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.4.0...v4.4.2
175183
[4.4.1]: https://github.com/sqlcipher/sqlcipher/tree/v4.4.1
176184
[4.4.1 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.4.0...v4.4.1
177185
[4.4.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.4.0

SQLCipher.podspec.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
"requires_arc": false,
1616
"source": {
1717
"git": "https://github.com/sqlcipher/sqlcipher.git",
18-
"tag": "v4.4.1"
18+
"tag": "v4.4.2"
1919
},
2020
"summary": "Full Database Encryption for SQLite.",
21-
"version": "4.4.1",
21+
"version": "4.4.2",
2222
"subspecs": [
2323
{
2424
"compiler_flags": [

src/crypto.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
#include "sqlcipher_ext.h"
4040
#endif
4141

42+
#ifdef SQLCIPHER_TEST
43+
static int cipher_fail_next_encrypt = 0;
44+
static int cipher_fail_next_decrypt = 0;
45+
#endif
46+
4247
/* Generate code to return a string value */
4348
static void codec_vdbe_return_string(Parse *pParse, const char *zLabel, const char *value, int value_type){
4449
Vdbe *v = sqlite3GetVdbe(pParse);
@@ -110,6 +115,24 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
110115
codec_vdbe_return_string(pParse, "cipher_license", license_result, P4_DYNAMIC);
111116
}
112117
} else
118+
#endif
119+
#ifdef SQLCIPHER_TEST
120+
if( sqlite3StrICmp(zLeft,"cipher_fail_next_encrypt")==0 ){
121+
if( zRight ) {
122+
cipher_fail_next_encrypt = sqlite3GetBoolean(zRight,1);
123+
} else {
124+
char *fail = sqlite3_mprintf("%d", cipher_fail_next_encrypt);
125+
codec_vdbe_return_string(pParse, "cipher_fail_next_encrypt", fail, P4_DYNAMIC);
126+
}
127+
}else
128+
if( sqlite3StrICmp(zLeft,"cipher_fail_next_decrypt")==0 ){
129+
if( zRight ) {
130+
cipher_fail_next_decrypt = sqlite3GetBoolean(zRight,1);
131+
} else {
132+
char *fail = sqlite3_mprintf("%d", cipher_fail_next_decrypt);
133+
codec_vdbe_return_string(pParse, "cipher_fail_next_decrypt", fail, P4_DYNAMIC);
134+
}
135+
}else
113136
#endif
114137
if( sqlite3StrICmp(zLeft, "cipher_fips_status")== 0 && !zRight ){
115138
if(ctx) {
@@ -685,6 +708,9 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
685708
memcpy(buffer, plaintext_header_sz ? pData : (void *) SQLITE_FILE_HEADER, offset);
686709

687710
rc = sqlcipher_page_cipher(ctx, cctx, pgno, CIPHER_DECRYPT, page_sz - offset, pData + offset, (unsigned char*)buffer + offset);
711+
#ifdef SQLCIPHER_TEST
712+
if(cipher_fail_next_decrypt) rc = SQLITE_ERROR;
713+
#endif
688714
if(rc != SQLITE_OK) { /* clear results of failed cipher operation and set error */
689715
sqlcipher_memset((unsigned char*) buffer+offset, 0, page_sz-offset);
690716
sqlcipher_codec_ctx_set_error(ctx, rc);
@@ -707,9 +733,13 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
707733
memcpy(buffer, plaintext_header_sz ? pData : kdf_salt, offset);
708734
}
709735
rc = sqlcipher_page_cipher(ctx, cctx, pgno, CIPHER_ENCRYPT, page_sz - offset, pData + offset, (unsigned char*)buffer + offset);
736+
#ifdef SQLCIPHER_TEST
737+
if(cipher_fail_next_encrypt) rc = SQLITE_ERROR;
738+
#endif
710739
if(rc != SQLITE_OK) { /* clear results of failed cipher operation and set error */
711740
sqlcipher_memset((unsigned char*)buffer+offset, 0, page_sz-offset);
712741
sqlcipher_codec_ctx_set_error(ctx, rc);
742+
return NULL;
713743
}
714744
return buffer; /* return persistent buffer data, pData remains intact */
715745
break;
@@ -939,8 +969,6 @@ void sqlite3CodecGetKey(sqlite3* db, int nDb, void **zKey, int *nKey) {
939969
}
940970
}
941971

942-
#ifndef OMIT_EXPORT
943-
944972
/*
945973
* Implementation of an "export" function that allows a caller
946974
* to duplicate the main database to an attached database. This is intended
@@ -1146,8 +1174,5 @@ void sqlcipher_exportFunc(sqlite3_context *context, int argc, sqlite3_value **ar
11461174
}
11471175
}
11481176
}
1149-
11501177
#endif
1151-
11521178
/* END SQLCIPHER */
1153-
#endif

src/crypto.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void sqlite3pager_reset(Pager *pPager);
5959
#define CIPHER_STR(s) #s
6060

6161
#ifndef CIPHER_VERSION_NUMBER
62-
#define CIPHER_VERSION_NUMBER 4.4.1
62+
#define CIPHER_VERSION_NUMBER 4.4.2
6363
#endif
6464

6565
#ifndef CIPHER_VERSION_BUILD

src/crypto_impl.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,9 @@ int sqlcipher_codec_ctx_integrity_check(codec_ctx *ctx, Parse *pParse, char *col
12781278
int payload_sz = ctx->page_sz - ctx->reserve_sz + ctx->iv_sz;
12791279
int read_sz = ctx->page_sz;
12801280

1281+
/* skip integrity check on PAGER_MJ_PGNO since it will have no valid content */
1282+
if(sqlite3pager_is_mj_pgno(ctx->pBt->pBt->pPager, page)) continue;
1283+
12811284
if(page==1) {
12821285
int page1_offset = ctx->plaintext_header_sz ? ctx->plaintext_header_sz : FILE_HEADER_SZ;
12831286
read_sz = read_sz - page1_offset;

src/crypto_openssl.c

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@
4040
#include <openssl/hmac.h>
4141
#include <openssl/err.h>
4242

43-
typedef struct {
44-
EVP_CIPHER *evp_cipher;
45-
} openssl_ctx;
46-
47-
static unsigned int openssl_external_init = 0;
4843
static unsigned int openssl_init_count = 0;
4944

5045
#if (defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L) || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L)
@@ -85,7 +80,7 @@ static int sqlcipher_openssl_add_random(void *ctx, void *buffer, int length) {
8580
return SQLITE_OK;
8681
}
8782

88-
#define OPENSSL_CIPHER "aes-256-cbc"
83+
#define OPENSSL_CIPHER EVP_aes_256_cbc()
8984

9085

9186
/* activate and initialize sqlcipher. Most importantly, this will automatically
@@ -103,13 +98,6 @@ static int sqlcipher_openssl_activate(void *ctx) {
10398
sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE));
10499
CODEC_TRACE_MUTEX("sqlcipher_openssl_activate: entered SQLCIPHER_MUTEX_PROVIDER_ACTIVATE\n");
105100

106-
if(openssl_init_count == 0 && EVP_get_cipherbyname(OPENSSL_CIPHER) != NULL) {
107-
/* if openssl has not yet been initialized by this library, but
108-
a call to get_cipherbyname works, then the openssl library
109-
has been initialized externally already. */
110-
openssl_external_init = 1;
111-
}
112-
113101
#ifdef SQLCIPHER_FIPS
114102
if(!FIPS_mode()){
115103
if(!FIPS_mode_set(1)){
@@ -126,13 +114,6 @@ static int sqlcipher_openssl_activate(void *ctx) {
126114
}
127115
#endif
128116

129-
if(openssl_init_count == 0 && openssl_external_init == 0) {
130-
/* if the library was not externally initialized, then should be now */
131-
#if (defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L) || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L)
132-
OpenSSL_add_all_algorithms();
133-
#endif
134-
}
135-
136117
openssl_init_count++;
137118
CODEC_TRACE_MUTEX("sqlcipher_openssl_activate: leaving SQLCIPHER_MUTEX_PROVIDER_ACTIVATE\n");
138119
sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE));
@@ -147,22 +128,8 @@ static int sqlcipher_openssl_deactivate(void *ctx) {
147128
CODEC_TRACE_MUTEX("sqlcipher_openssl_deactivate: entering SQLCIPHER_MUTEX_PROVIDER_ACTIVATE\n");
148129
sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE));
149130
CODEC_TRACE_MUTEX("sqlcipher_openssl_deactivate: entered SQLCIPHER_MUTEX_PROVIDER_ACTIVATE\n");
150-
openssl_init_count--;
151131

152-
if(openssl_init_count == 0) {
153-
if(openssl_external_init == 0) {
154-
/* if OpenSSL hasn't be initialized externally, and the counter reaches zero
155-
after it's decremented, release EVP memory
156-
Note: this code will only be reached if OpensSSL_add_all_algorithms()
157-
is called by SQLCipher internally. This should prevent SQLCipher from
158-
"cleaning up" openssl when it was initialized externally by the program */
159-
#if (defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L) || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L)
160-
EVP_cleanup();
161-
#endif
162-
} else {
163-
openssl_external_init = 0;
164-
}
165-
}
132+
openssl_init_count--;
166133

167134
CODEC_TRACE_MUTEX("sqlcipher_openssl_deactivate: leaving SQLCIPHER_MUTEX_PROVIDER_ACTIVATE\n");
168135
sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE));
@@ -267,7 +234,7 @@ static int sqlcipher_openssl_cipher(void *ctx, int mode, unsigned char *key, int
267234
int tmp_csz, csz, rc = SQLITE_OK;
268235
EVP_CIPHER_CTX* ectx = EVP_CIPHER_CTX_new();
269236
if(ectx == NULL) goto error;
270-
if(!EVP_CipherInit_ex(ectx, ((openssl_ctx *)ctx)->evp_cipher, NULL, NULL, NULL, mode)) goto error;
237+
if(!EVP_CipherInit_ex(ectx, OPENSSL_CIPHER, NULL, NULL, NULL, mode)) goto error;
271238
if(!EVP_CIPHER_CTX_set_padding(ectx, 0)) goto error; /* no padding */
272239
if(!EVP_CipherInit_ex(ectx, NULL, NULL, key, iv, mode)) goto error;
273240
if(!EVP_CipherUpdate(ectx, out, &tmp_csz, in, in_sz)) goto error;
@@ -286,19 +253,19 @@ static int sqlcipher_openssl_cipher(void *ctx, int mode, unsigned char *key, int
286253
}
287254

288255
static const char* sqlcipher_openssl_get_cipher(void *ctx) {
289-
return OBJ_nid2sn(EVP_CIPHER_nid(((openssl_ctx *)ctx)->evp_cipher));
256+
return OBJ_nid2sn(EVP_CIPHER_nid(OPENSSL_CIPHER));
290257
}
291258

292259
static int sqlcipher_openssl_get_key_sz(void *ctx) {
293-
return EVP_CIPHER_key_length(((openssl_ctx *)ctx)->evp_cipher);
260+
return EVP_CIPHER_key_length(OPENSSL_CIPHER);
294261
}
295262

296263
static int sqlcipher_openssl_get_iv_sz(void *ctx) {
297-
return EVP_CIPHER_iv_length(((openssl_ctx *)ctx)->evp_cipher);
264+
return EVP_CIPHER_iv_length(OPENSSL_CIPHER);
298265
}
299266

300267
static int sqlcipher_openssl_get_block_sz(void *ctx) {
301-
return EVP_CIPHER_block_size(((openssl_ctx *)ctx)->evp_cipher);
268+
return EVP_CIPHER_block_size(OPENSSL_CIPHER);
302269
}
303270

304271
static int sqlcipher_openssl_get_hmac_sz(void *ctx, int algorithm) {
@@ -318,21 +285,11 @@ static int sqlcipher_openssl_get_hmac_sz(void *ctx, int algorithm) {
318285
}
319286

320287
static int sqlcipher_openssl_ctx_init(void **ctx) {
321-
openssl_ctx *o_ctx;
322-
323-
*ctx = sqlcipher_malloc(sizeof(openssl_ctx));
324-
if(*ctx == NULL) return SQLITE_NOMEM;
325-
sqlcipher_openssl_activate(*ctx);
326-
327-
o_ctx = (openssl_ctx *)*ctx;
328-
o_ctx->evp_cipher = (EVP_CIPHER *) EVP_get_cipherbyname(OPENSSL_CIPHER);
329-
return o_ctx->evp_cipher != NULL ? SQLITE_OK : SQLITE_ERROR;
288+
return sqlcipher_openssl_activate(*ctx);
330289
}
331290

332291
static int sqlcipher_openssl_ctx_free(void **ctx) {
333-
sqlcipher_openssl_deactivate(*ctx);
334-
sqlcipher_free(*ctx, sizeof(openssl_ctx));
335-
return SQLITE_OK;
292+
return sqlcipher_openssl_deactivate(NULL);
336293
}
337294

338295
static int sqlcipher_openssl_fips_status(void *ctx) {

src/func.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,12 +1824,10 @@ void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){
18241824
}
18251825
/* BEGIN SQLCIPHER */
18261826
#ifdef SQLITE_HAS_CODEC
1827-
#ifndef OMIT_EXPORT
18281827
{
18291828
extern void sqlcipher_exportFunc(sqlite3_context *, int, sqlite3_value **);
18301829
sqlite3CreateFunc(db, "sqlcipher_export", -1, SQLITE_TEXT, 0, sqlcipher_exportFunc, 0, 0, 0, 0, 0);
18311830
}
1832-
#endif
18331831
#ifdef SQLCIPHER_EXT
18341832
#include "sqlcipher_funcs_init.h"
18351833
#endif

src/shell.c.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11118,7 +11118,7 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
1111811118
#else
1111911119
printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
1112011120
#endif
11121-
/* BEGIN SQLCIPHER */
11121+
/* END SQLCIPHER */
1112211122
return 0;
1112311123
}else if( strcmp(z,"-interactive")==0 ){
1112411124
stdin_is_interactive = 1;

src/sqlite.h.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5993,7 +5993,7 @@ void sqlite3_activate_see(
59935993
const char *zPassPhrase /* Activation phrase */
59945994
);
59955995
#endif
5996-
/* BEGIN SQLCIPHER */
5996+
/* END SQLCIPHER */
59975997

59985998
#ifdef SQLITE_ENABLE_CEROD
59995999
/*

src/tclsqlite.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3727,7 +3727,7 @@ static int SQLITE_TCLAPI DbMain(
37273727
void *pKey = 0;
37283728
int nKey = 0;
37293729
#endif
3730-
/* BEGIN SQLCIPHER */
3730+
/* END SQLCIPHER */
37313731
int rc;
37323732

37333733
/* In normal use, each TCL interpreter runs in a single thread. So

0 commit comments

Comments
 (0)