Skip to content

Commit

Permalink
Add pragma hexkey/hexrekey to resolve issue #70
Browse files Browse the repository at this point in the history
  • Loading branch information
utelle committed Mar 22, 2022
1 parent b0c82a6 commit 2ecb37e
Showing 1 changed file with 57 additions and 4 deletions.
61 changes: 57 additions & 4 deletions src/cipher_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,26 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
((char**)pArg)[0] = sqlite3_mprintf("ok");
}
}
else if (sqlite3StrICmp(pragmaName, "hexkey") == 0)
{
int nValue = sqlite3Strlen30(pragmaValue);
if (((nValue & 1) == 0) && (sqlite3mcIsHexKey(pragmaValue, nValue) != 0))
{
char* zHexKey = sqlite3_malloc(nValue/2);
sqlite3mcConvertHex2Bin(pragmaValue, nValue, zHexKey);
rc = sqlite3_key_v2(db, zDbName, zHexKey, nValue/2);
sqlite3_free(zHexKey);
if (rc == SQLITE_OK)
{
((char**)pArg)[0] = sqlite3_mprintf("ok");
}
}
else
{
rc = SQLITE_ERROR;
((char**)pArg)[0] = sqlite3_mprintf("Malformed hex string");
}
}
else if (sqlite3StrICmp(pragmaName, "rekey") == 0)
{
rc = sqlite3_rekey_v2(db, zDbName, pragmaValue, -1);
Expand All @@ -842,6 +862,37 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
}
}
}
else if (sqlite3StrICmp(pragmaName, "hexrekey") == 0)
{
int nValue = sqlite3Strlen30(pragmaValue);
if (((nValue & 1) == 0) && (sqlite3mcIsHexKey(pragmaValue, nValue) != 0))
{
char* zHexKey = sqlite3_malloc(nValue/2);
sqlite3mcConvertHex2Bin(pragmaValue, nValue, zHexKey);
rc = sqlite3_rekey_v2(db, zDbName, zHexKey, nValue/2);
sqlite3_free(zHexKey);
if (rc == SQLITE_OK)
{
((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
{
rc = SQLITE_ERROR;
((char**)pArg)[0] = sqlite3_mprintf("Malformed hex string");
}
}
else
{
int j;
Expand Down Expand Up @@ -906,13 +957,15 @@ sqlite3mcCodecQueryParameters(sqlite3* db, const char* zDb, const char* zUri)
{
u8 iByte;
int i;
char zDecoded[40];
for (i = 0, iByte = 0; i < sizeof(zDecoded) * 2 && sqlite3Isxdigit(zKey[i]); i++)
int nKey = sqlite3Strlen30(zKey);
char* zDecoded = sqlite3_malloc(nKey);
for (i = 0, iByte = 0; i < nKey && sqlite3Isxdigit(zKey[i]); i++)
{
iByte = (iByte << 4) + sqlite3HexToInt(zKey[i]);
if ((i & 1) != 0) zDecoded[i / 2] = iByte;
if ((i & 1) != 0) zDecoded[i/2] = iByte;
}
sqlite3_key_v2(db, zDb, zDecoded, i / 2);
sqlite3_key_v2(db, zDb, zDecoded, i/2);
sqlite3_free(zDecoded);
}
else if ((zKey = sqlite3_uri_parameter(zUri, "key")) != 0)
{
Expand Down

0 comments on commit 2ecb37e

Please sign in to comment.