Skip to content

Commit 1be5782

Browse files
committed
Fix retrieval of config parameter table (#90)
If some of the builtin cipher schemes are omitted, the retrieval of the cipher configuration parameter table can return an invalid pointer, leading to a crash on activating encryption for a database connection.
1 parent 4a1f4f8 commit 1be5782

File tree

10 files changed

+87
-66
lines changed

10 files changed

+87
-66
lines changed

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ dnl Copyright (C) 2019-2022 Ulrich Telle <[email protected]>
44
dnl
55
dnl This file is covered by the same licence as the entire SQLite3 Multiple Ciphers package.
66

7-
AC_INIT([sqlite3mc], [1.5.1], [[email protected]])
7+
AC_INIT([sqlite3mc], [1.5.2], [[email protected]])
88

99
dnl This is the version tested with, might work with earlier ones.
1010
AC_PREREQ([2.69])

readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ The code was mainly developed under Windows, but was tested under Linux as well.
1010

1111
## Version history
1212

13+
* 1.5.2 - *September 2022*
14+
- Based on SQLite version 3.39.3
15+
- Fixed retrieval of configuration parameter table
1316
* 1.5.1 - *September 2022*
1417
- Based on SQLite version 3.39.3
1518
- Fixed a bug in shutdown code

src/cipher_chacha20.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
/* --- ChaCha20-Poly1305 cipher (plus sqleet variant) --- */
1313
#if HAVE_CIPHER_CHACHA20
1414

15+
#define CIPHER_NAME_CHACHA20 "chacha20"
16+
1517
/*
1618
** Configuration parameters for "chacha20"
1719
**
@@ -68,7 +70,7 @@ AllocateChaCha20Cipher(sqlite3* db)
6870
}
6971
if (chacha20Cipher != NULL)
7072
{
71-
CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CODEC_TYPE_CHACHA20);
73+
CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CIPHER_NAME_CHACHA20);
7274
chacha20Cipher->m_legacy = sqlite3mcGetCipherParameter(cipherParams, "legacy");
7375
chacha20Cipher->m_legacyPageSize = sqlite3mcGetCipherParameter(cipherParams, "legacy_page_size");
7476
chacha20Cipher->m_kdfIter = sqlite3mcGetCipherParameter(cipherParams, "kdf_iter");
@@ -364,15 +366,16 @@ DecryptPageChaCha20Cipher(void* cipher, int page, unsigned char* data, int len,
364366

365367
SQLITE_PRIVATE const CipherDescriptor mcChaCha20Descriptor =
366368
{
367-
"chacha20", AllocateChaCha20Cipher,
368-
FreeChaCha20Cipher,
369-
CloneChaCha20Cipher,
370-
GetLegacyChaCha20Cipher,
371-
GetPageSizeChaCha20Cipher,
372-
GetReservedChaCha20Cipher,
373-
GetSaltChaCha20Cipher,
374-
GenerateKeyChaCha20Cipher,
375-
EncryptPageChaCha20Cipher,
376-
DecryptPageChaCha20Cipher
369+
CIPHER_NAME_CHACHA20,
370+
AllocateChaCha20Cipher,
371+
FreeChaCha20Cipher,
372+
CloneChaCha20Cipher,
373+
GetLegacyChaCha20Cipher,
374+
GetPageSizeChaCha20Cipher,
375+
GetReservedChaCha20Cipher,
376+
GetSaltChaCha20Cipher,
377+
GenerateKeyChaCha20Cipher,
378+
EncryptPageChaCha20Cipher,
379+
DecryptPageChaCha20Cipher
377380
};
378381
#endif

src/cipher_common.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ static CipherParams commonParams[] =
2929
CIPHER_PARAMS_SENTINEL
3030
};
3131

32-
static CodecParameter globalCommonParams = { "global", CODEC_TYPE_UNKNOWN, commonParams };
33-
static CodecParameter globalSentinelParams = { "", CODEC_TYPE_UNKNOWN, NULL };
32+
#define CIPHER_NAME_GLOBAL "global"
33+
34+
static CodecParameter globalCommonParams = { CIPHER_NAME_GLOBAL, CODEC_TYPE_UNKNOWN, commonParams };
35+
static CodecParameter globalSentinelParams = { "", CODEC_TYPE_UNKNOWN, NULL };
3436

3537
SQLITE_PRIVATE int
3638
sqlite3mcGetCipherParameter(CipherParams* cipherParams, const char* paramName)
@@ -157,19 +159,20 @@ sqlite3mcGetCipherType(sqlite3* db)
157159
}
158160

159161
SQLITE_PRIVATE CipherParams*
160-
sqlite3mcGetCipherParams(sqlite3* db, int cypherType)
162+
sqlite3mcGetCipherParams(sqlite3* db, const char* cipherName)
161163
{
162164
int j = 0;
165+
int cipherType = sqlite3mc_cipher_index(cipherName);
163166
CodecParameter* codecParams = (db != NULL) ? sqlite3mcGetCodecParams(db) : globalCodecParameterTable;
164167
if (codecParams == NULL)
165168
{
166169
codecParams = globalCodecParameterTable;
167170
}
168-
if (cypherType > 0)
171+
if (cipherType > 0)
169172
{
170173
for (j = 1; codecParams[j].m_id > 0; ++j)
171174
{
172-
if (cypherType == codecParams[j].m_id) break;
175+
if (cipherType == codecParams[j].m_id) break;
173176
}
174177
}
175178
CipherParams* cipherParamTable = codecParams[j].m_params;
@@ -241,7 +244,7 @@ SQLITE_PRIVATE int
241244
sqlite3mcCodecSetup(Codec* codec, int cipherType, char* userPassword, int passwordLength)
242245
{
243246
int rc = SQLITE_OK;
244-
CipherParams* globalParams = sqlite3mcGetCipherParams(codec->m_db, 0);
247+
CipherParams* globalParams = sqlite3mcGetCipherParams(codec->m_db, CIPHER_NAME_GLOBAL);
245248
codec->m_isEncrypted = 1;
246249
codec->m_hmacCheck = sqlite3mcGetCipherParameter(globalParams, "hmac_check");
247250
codec->m_walLegacy = sqlite3mcGetCipherParameter(globalParams, "mc_legacy_wal");
@@ -266,7 +269,7 @@ SQLITE_PRIVATE int
266269
sqlite3mcSetupWriteCipher(Codec* codec, int cipherType, char* userPassword, int passwordLength)
267270
{
268271
int rc = SQLITE_OK;
269-
CipherParams* globalParams = sqlite3mcGetCipherParams(codec->m_db, 0);
272+
CipherParams* globalParams = sqlite3mcGetCipherParams(codec->m_db, CIPHER_NAME_GLOBAL);
270273
if (codec->m_writeCipher != NULL)
271274
{
272275
globalCodecDescriptorTable[codec->m_writeCipherType-1].m_freeCipher(codec->m_writeCipher);

src/cipher_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ SQLITE_PRIVATE int sqlite3mcGetCipherParameter(CipherParams* cipherParams, const
9898

9999
SQLITE_PRIVATE int sqlite3mcGetCipherType(sqlite3* db);
100100

101-
SQLITE_PRIVATE CipherParams* sqlite3mcGetCipherParams(sqlite3* db, int cypherType);
101+
SQLITE_PRIVATE CipherParams* sqlite3mcGetCipherParams(sqlite3* db, const char* cipherName);
102102

103103
SQLITE_PRIVATE int sqlite3mcCodecInit(Codec* codec);
104104

src/cipher_sds_rc4.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
/* --- RC4 cipher (System.Data.SQLite) --- */
1313
#if HAVE_CIPHER_RC4
1414

15+
#define CIPHER_NAME_RC4 "rc4"
16+
1517
/*
1618
** Configuration parameters for "rc4"
1719
**
@@ -52,7 +54,7 @@ AllocateRC4Cipher(sqlite3* db)
5254
}
5355
if (rc4Cipher != NULL)
5456
{
55-
CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CODEC_TYPE_RC4);
57+
CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CIPHER_NAME_RC4);
5658
rc4Cipher->m_legacy = sqlite3mcGetCipherParameter(cipherParams, "legacy");
5759
rc4Cipher->m_legacyPageSize = sqlite3mcGetCipherParameter(cipherParams, "legacy_page_size");
5860
}
@@ -155,15 +157,16 @@ DecryptPageRC4Cipher(void* cipher, int page, unsigned char* data, int len, int r
155157

156158
SQLITE_PRIVATE const CipherDescriptor mcRC4Descriptor =
157159
{
158-
"rc4", AllocateRC4Cipher,
159-
FreeRC4Cipher,
160-
CloneRC4Cipher,
161-
GetLegacyRC4Cipher,
162-
GetPageSizeRC4Cipher,
163-
GetReservedRC4Cipher,
164-
GetSaltRC4Cipher,
165-
GenerateKeyRC4Cipher,
166-
EncryptPageRC4Cipher,
167-
DecryptPageRC4Cipher
160+
CIPHER_NAME_RC4,
161+
AllocateRC4Cipher,
162+
FreeRC4Cipher,
163+
CloneRC4Cipher,
164+
GetLegacyRC4Cipher,
165+
GetPageSizeRC4Cipher,
166+
GetReservedRC4Cipher,
167+
GetSaltRC4Cipher,
168+
GenerateKeyRC4Cipher,
169+
EncryptPageRC4Cipher,
170+
DecryptPageRC4Cipher
168171
};
169172
#endif

src/cipher_sqlcipher.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
/* --- SQLCipher AES256CBC-HMAC cipher --- */
1313
#if HAVE_CIPHER_SQLCIPHER
1414

15+
#define CIPHER_NAME_SQLCIPHER "sqlcipher"
16+
1517
/*
1618
** Configuration parameters for "sqlcipher"
1719
**
@@ -127,7 +129,7 @@ AllocateSQLCipherCipher(sqlite3* db)
127129
}
128130
if (sqlCipherCipher != NULL)
129131
{
130-
CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CODEC_TYPE_SQLCIPHER);
132+
CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CIPHER_NAME_SQLCIPHER);
131133
sqlCipherCipher->m_legacy = sqlite3mcGetCipherParameter(cipherParams, "legacy");
132134
sqlCipherCipher->m_legacyPageSize = sqlite3mcGetCipherParameter(cipherParams, "legacy_page_size");
133135
sqlCipherCipher->m_kdfIter = sqlite3mcGetCipherParameter(cipherParams, "kdf_iter");
@@ -500,15 +502,16 @@ DecryptPageSQLCipherCipher(void* cipher, int page, unsigned char* data, int len,
500502
}
501503
SQLITE_PRIVATE const CipherDescriptor mcSQLCipherDescriptor =
502504
{
503-
"sqlcipher", AllocateSQLCipherCipher,
504-
FreeSQLCipherCipher,
505-
CloneSQLCipherCipher,
506-
GetLegacySQLCipherCipher,
507-
GetPageSizeSQLCipherCipher,
508-
GetReservedSQLCipherCipher,
509-
GetSaltSQLCipherCipher,
510-
GenerateKeySQLCipherCipher,
511-
EncryptPageSQLCipherCipher,
512-
DecryptPageSQLCipherCipher
505+
CIPHER_NAME_SQLCIPHER,
506+
AllocateSQLCipherCipher,
507+
FreeSQLCipherCipher,
508+
CloneSQLCipherCipher,
509+
GetLegacySQLCipherCipher,
510+
GetPageSizeSQLCipherCipher,
511+
GetReservedSQLCipherCipher,
512+
GetSaltSQLCipherCipher,
513+
GenerateKeySQLCipherCipher,
514+
EncryptPageSQLCipherCipher,
515+
DecryptPageSQLCipherCipher
513516
};
514517
#endif

src/cipher_wxaes128.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
/* --- AES 128-bit cipher (wxSQLite3) --- */
1313
#if HAVE_CIPHER_AES_128_CBC
1414

15+
#define CIPHER_NAME_AES128 "aes128cbc"
16+
1517
/*
1618
** Configuration parameters for "aes128cbc"
1719
**
@@ -62,7 +64,7 @@ AllocateAES128Cipher(sqlite3* db)
6264
}
6365
if (aesCipher != NULL)
6466
{
65-
CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CODEC_TYPE_AES128);
67+
CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CIPHER_NAME_AES128);
6668
aesCipher->m_legacy = sqlite3mcGetCipherParameter(cipherParams, "legacy");
6769
aesCipher->m_legacyPageSize = sqlite3mcGetCipherParameter(cipherParams, "legacy_page_size");
6870
}
@@ -266,15 +268,16 @@ DecryptPageAES128Cipher(void* cipher, int page, unsigned char* data, int len, in
266268

267269
SQLITE_PRIVATE const CipherDescriptor mcAES128Descriptor =
268270
{
269-
"aes128cbc", AllocateAES128Cipher,
270-
FreeAES128Cipher,
271-
CloneAES128Cipher,
272-
GetLegacyAES128Cipher,
273-
GetPageSizeAES128Cipher,
274-
GetReservedAES128Cipher,
275-
GetSaltAES128Cipher,
276-
GenerateKeyAES128Cipher,
277-
EncryptPageAES128Cipher,
278-
DecryptPageAES128Cipher
271+
CIPHER_NAME_AES128,
272+
AllocateAES128Cipher,
273+
FreeAES128Cipher,
274+
CloneAES128Cipher,
275+
GetLegacyAES128Cipher,
276+
GetPageSizeAES128Cipher,
277+
GetReservedAES128Cipher,
278+
GetSaltAES128Cipher,
279+
GenerateKeyAES128Cipher,
280+
EncryptPageAES128Cipher,
281+
DecryptPageAES128Cipher
279282
};
280283
#endif

src/cipher_wxaes256.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
/* --- AES 256-bit cipher (wxSQLite3) --- */
1313
#if HAVE_CIPHER_AES_256_CBC
1414

15+
#define CIPHER_NAME_AES256 "aes256cbc"
16+
1517
/*
1618
** Configuration parameters for "aes256cbc"
1719
**
@@ -66,7 +68,7 @@ AllocateAES256Cipher(sqlite3* db)
6668
}
6769
if (aesCipher != NULL)
6870
{
69-
CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CODEC_TYPE_AES256);
71+
CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CIPHER_NAME_AES256);
7072
aesCipher->m_legacy = sqlite3mcGetCipherParameter(cipherParams, "legacy");
7173
aesCipher->m_legacyPageSize = sqlite3mcGetCipherParameter(cipherParams, "legacy_page_size");
7274
aesCipher->m_kdfIter = sqlite3mcGetCipherParameter(cipherParams, "kdf_iter");
@@ -234,15 +236,16 @@ DecryptPageAES256Cipher(void* cipher, int page, unsigned char* data, int len, in
234236

235237
SQLITE_PRIVATE const CipherDescriptor mcAES256Descriptor =
236238
{
237-
"aes256cbc", AllocateAES256Cipher,
238-
FreeAES256Cipher,
239-
CloneAES256Cipher,
240-
GetLegacyAES256Cipher,
241-
GetPageSizeAES256Cipher,
242-
GetReservedAES256Cipher,
243-
GetSaltAES256Cipher,
244-
GenerateKeyAES256Cipher,
245-
EncryptPageAES256Cipher,
246-
DecryptPageAES256Cipher
239+
CIPHER_NAME_AES256,
240+
AllocateAES256Cipher,
241+
FreeAES256Cipher,
242+
CloneAES256Cipher,
243+
GetLegacyAES256Cipher,
244+
GetPageSizeAES256Cipher,
245+
GetReservedAES256Cipher,
246+
GetSaltAES256Cipher,
247+
GenerateKeyAES256Cipher,
248+
EncryptPageAES256Cipher,
249+
DecryptPageAES256Cipher
247250
};
248251
#endif

src/sqlite3mc_version.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
#define SQLITE3MC_VERSION_MAJOR 1
1616
#define SQLITE3MC_VERSION_MINOR 5
17-
#define SQLITE3MC_VERSION_RELEASE 1
17+
#define SQLITE3MC_VERSION_RELEASE 2
1818
#define SQLITE3MC_VERSION_SUBRELEASE 0
19-
#define SQLITE3MC_VERSION_STRING "SQLite3 Multiple Ciphers 1.5.1"
19+
#define SQLITE3MC_VERSION_STRING "SQLite3 Multiple Ciphers 1.5.2"
2020

2121
#endif /* SQLITE3MC_VERSION_H_ */

0 commit comments

Comments
 (0)