Skip to content

Commit

Permalink
Add check for encryption support (fix issue utelle#168)
Browse files Browse the repository at this point in the history
Functions sqlite3_key_v2 and sqlite3_rekey_v2 now check whether the associated VFS supports encryption. If not, SQLITE_ERROR is returned.
  • Loading branch information
utelle committed Jun 11, 2024
1 parent 4856e93 commit 5208c16
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cipher_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ sqlite3mcGetLegacyWriteCipher(Codec* codec)
SQLITE_PRIVATE int
sqlite3mcGetPageSizeReadCipher(Codec* codec)
{
int pageSize = (codec->m_hasReadCipher && codec->m_readCipher != NULL) ? globalCodecDescriptorTable[codec->m_readCipherType - 1].m_getPageSize(codec->m_readCipher) : 0;
int pageSize = (codec->m_hasReadCipher && codec->m_readCipher != NULL) ? globalCodecDescriptorTable[codec->m_readCipherType - 1].m_getPageSize(codec->m_readCipher) : -1;
return pageSize;
}

Expand Down
13 changes: 13 additions & 0 deletions src/codecext.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ sqlite3mcGetMainCodec(sqlite3* db);
SQLITE_PRIVATE void
sqlite3mcSetCodec(sqlite3* db, const char* zDbName, const char* zFileName, Codec* codec);

SQLITE_PRIVATE int
sqlite3mcIsEncryptionSupported(sqlite3* db, const char* zDbName);

static int
mcAdjustBtree(Btree* pBt, int nPageSize, int nReserved, int isLegacy)
{
Expand Down Expand Up @@ -336,6 +339,11 @@ SQLITE_API int
sqlite3_key_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
{
int rc = SQLITE_ERROR;
if (!sqlite3mcIsEncryptionSupported(db, zDbName))
{
sqlite3ErrorWithMsg(db, rc, "Setting key failed. Encryption is not supported by the VFS.");
return rc;
}
if (zKey != NULL && nKey < 0)
{
/* Key is zero-terminated string */
Expand Down Expand Up @@ -392,6 +400,11 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
int rc = SQLITE_ERROR;
char* err = NULL;

if (!sqlite3mcIsEncryptionSupported(db, zDbName))
{
sqlite3ErrorWithMsg(db, rc, "Rekeying failed. Encryption is not supported by the VFS.");
return rc;
}
if (zKey != NULL && nKey < 0)
{
/* Key is zero-terminated string */
Expand Down
10 changes: 10 additions & 0 deletions src/sqlite3mc_vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,16 @@ static sqlite3mc_vfs* mcFindVfs(sqlite3* db, const char* zDbName)
return pVfsMC;
}

/*
** Check whether the VFS of the database file corresponding
** to the database schema name supports encryption.
*/
SQLITE_PRIVATE int sqlite3mcIsEncryptionSupported(sqlite3* db, const char* zDbName)
{
sqlite3mc_vfs* pVfsMC = mcFindVfs(db, zDbName);
return (pVfsMC != NULL);
}

/*
** Find the codec of the database file
** corresponding to the database schema name.
Expand Down

0 comments on commit 5208c16

Please sign in to comment.