Skip to content

Commit 1f17e15

Browse files
Implement sqlite3_key_v2 & sqlite3_rekey_v2.
1 parent 432585f commit 1f17e15

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

src/crypto.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,16 +341,39 @@ void sqlite3_activate_see(const char* in) {
341341
/* do nothing, security enhancements are always active */
342342
}
343343

344+
int sqlcipher_find_db_index(sqlite3 *db, const char *zDb) {
345+
if(zDb == NULL){
346+
return 0;
347+
}
348+
int db_index;
349+
for(db_index = 0; db_index < db->nDb - 1; db_index++) {
350+
struct Db *pDb = &db->aDb[db_index];
351+
if(strcmp(pDb->zName, zDb) == 0) {
352+
return db_index;
353+
}
354+
}
355+
return 0;
356+
}
357+
344358
int sqlite3_key(sqlite3 *db, const void *pKey, int nKey) {
359+
return sqlite3_key_v2(db, "main", pKey, nKey);
360+
}
361+
362+
int sqlite3_key_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey) {
345363
CODEC_TRACE(("sqlite3_key: entered db=%p pKey=%s nKey=%d\n", db, (char *)pKey, nKey));
346364
/* attach key if db and pKey are not null and nKey is > 0 */
347365
if(db && pKey && nKey) {
348-
return sqlite3CodecAttach(db, 0, pKey, nKey); // operate only on the main db
366+
int db_index = sqlcipher_find_db_index(db, zDb);
367+
return sqlite3CodecAttach(db, db_index, pKey, nKey);
349368
}
350369
return SQLITE_ERROR;
351370
}
352371

353-
/* sqlite3_rekey
372+
int sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey) {
373+
return sqlite3_rekey_v2(db, "main", pKey, nKey);
374+
}
375+
376+
/* sqlite3_rekey_v2
354377
** Given a database, this will reencrypt the database using a new key.
355378
** There is only one possible modes of operation - to encrypt a database
356379
** that is already encrpyted. If the database is not already encrypted
@@ -360,10 +383,11 @@ int sqlite3_key(sqlite3 *db, const void *pKey, int nKey) {
360383
** 2. If there is NOT already a key present do nothing
361384
** 3. If there is a key present, re-encrypt the database with the new key
362385
*/
363-
int sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey) {
386+
int sqlite3_rekey_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey) {
364387
CODEC_TRACE(("sqlite3_rekey: entered db=%p pKey=%s, nKey=%d\n", db, (char *)pKey, nKey));
365388
if(db && pKey && nKey) {
366-
struct Db *pDb = &db->aDb[0];
389+
int db_index = sqlcipher_find_db_index(db, zDb);
390+
struct Db *pDb = &db->aDb[db_index];
367391
CODEC_TRACE(("sqlite3_rekey: database pDb=%p\n", pDb));
368392
if(pDb->pBt) {
369393
codec_ctx *ctx;

0 commit comments

Comments
 (0)