Skip to content

Commit

Permalink
Fix issue #85
Browse files Browse the repository at this point in the history
Setting or resetting an encryption key (sqlite3_key resp sqlite3_rekey) is not supported for in-memory or temporary databases. This is now explicitly checked, and an error is reported in such a case.
  • Loading branch information
utelle committed Jul 25, 2022
1 parent a3befec commit e1b82c1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
22 changes: 22 additions & 0 deletions src/cipher_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,17 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
{
((char**)pArg)[0] = sqlite3_mprintf("ok");
}
else
{
if (db->pErr)
{
const char* z = (const char*)sqlite3_value_text(db->pErr);
if (z && sqlite3Strlen30(z) > 0)
{
((char**)pArg)[0] = sqlite3_mprintf(z);
}
}
}
}
else if (sqlite3StrICmp(pragmaName, "hexkey") == 0)
{
Expand All @@ -836,6 +847,17 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
{
((char**)pArg)[0] = sqlite3_mprintf("ok");
}
else
{
if (db->pErr)
{
const char* z = (const char*)sqlite3_value_text(db->pErr);
if (z && sqlite3Strlen30(z) > 0)
{
((char**)pArg)[0] = sqlite3_mprintf(z);
}
}
}
}
else
{
Expand Down
22 changes: 16 additions & 6 deletions src/codecext.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@ sqlite3_key_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
{
int dbIndex;
const char* dbFileName = sqlite3_db_filename(db, zDbName);
if (dbFileName == NULL || dbFileName[0] == 0)
{
sqlite3ErrorWithMsg(db, rc, "Setting key not supported for in-memory or temporary databases.");
return rc;
}
/* Configure cipher from URI parameters if requested */
if (sqlite3FindFunction(db, "sqlite3mc_config_table", 0, SQLITE_UTF8, 0) == NULL)
{
Expand All @@ -356,7 +361,7 @@ sqlite3_key_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
else
{
rc = SQLITE_ERROR;
sqlite3ErrorWithMsg(db, rc, "Key failed. Database '%s' not found.", zDbName);
sqlite3ErrorWithMsg(db, rc, "Setting key failed. Database '%s' not found.", zDbName);
}
}
return rc;
Expand All @@ -383,7 +388,12 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
dbIndex = (zDbName) ? sqlite3FindDbName(db, zDbName) : 0;
if (dbIndex < 0)
{
sqlite3ErrorWithMsg(db, rc, "Rekey failed. Database '%s' not found.", zDbName);
sqlite3ErrorWithMsg(db, rc, "Rekeying failed. Database '%s' not found.", zDbName);
return rc;
}
if (dbFileName == NULL || dbFileName[0] == 0)
{
sqlite3ErrorWithMsg(db, rc, "Rekeying not supported for in-memory or temporary databases.");
return rc;
}
pBt = db->aDb[dbIndex].pBt;
Expand All @@ -398,7 +408,7 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)

if (pagerUseWal(pPager))
{
sqlite3ErrorWithMsg(db, rc, "Rekey is not supported in WAL journal mode.");
sqlite3ErrorWithMsg(db, rc, "Rekeying is not supported in WAL journal mode.");
return rc;
}

Expand Down Expand Up @@ -449,7 +459,7 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
{
/* Pagesize cannot be changed for an encrypted database */
rc = SQLITE_ERROR;
sqlite3ErrorWithMsg(db, rc, "Rekey failed. Pagesize cannot be changed for an encrypted database.");
sqlite3ErrorWithMsg(db, rc, "Rekeying failed. Pagesize cannot be changed for an encrypted database.");
goto leave_rekey;
}
}
Expand Down Expand Up @@ -498,14 +508,14 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
{
/* Pagesize cannot be changed for an encrypted database */
rc = SQLITE_ERROR;
sqlite3ErrorWithMsg(db, rc, "Rekey failed. Pagesize cannot be changed for an encrypted database.");
sqlite3ErrorWithMsg(db, rc, "Rekeying failed. Pagesize cannot be changed for an encrypted database.");
goto leave_rekey;
}
}
else
{
/* Setup of write cipher failed */
sqlite3ErrorWithMsg(db, rc, "Rekey failed. Setup of write cipher failed.");
sqlite3ErrorWithMsg(db, rc, "Rekeying failed. Setup of write cipher failed.");
goto leave_rekey;
}
}
Expand Down

0 comments on commit e1b82c1

Please sign in to comment.