int sqlite3CodecAttach(sqlite3 *db, int nDb, const void *zKey, int nKey) { BOTANSQLITE_TRACE("sqlite3CodecAttach"); void *pCodec = NULL; if (!zKey || nKey <= 0) { Pager *pager = sqlite3BtreePager(db->aDb[nDb].pBt); // No key specified, could mean either use the main db's encryption or no encryption if (nDb != 0 && nKey < 0) { // Is an attached database, therefore use the key of main database, if main database is encrypted void *pMainCodec = sqlite3PagerGetCodec(sqlite3BtreePager(db->aDb[0].pBt)); if (pMainCodec) { pCodec = InitializeFromOtherCodec(pMainCodec, db); sqlite3PagerSetCodec( pager, Codec, CodecSizeChange, PagerFreeCodec, pCodec); } } else { // No encryption requested sqlite3PagerSetCodec(pager, NULL, NULL, NULL, NULL); } } else { // Key specified, setup encryption key for database pCodec = InitializeNewCodec(db); assert(nKey >= 0); SetWriteKey(pCodec, (const char*) zKey, (size_t) nKey); if (HandleError(pCodec)) { DeleteCodec(pCodec); return SQLITE_ERROR; } SetReadIsWrite(pCodec); sqlite3PagerSetCodec( sqlite3BtreePager(db->aDb[nDb].pBt), Codec, CodecSizeChange, PagerFreeCodec, pCodec); } if (HandleError(pCodec)) return SQLITE_ERROR; return SQLITE_OK; }
/* @see CMMFHwDevice::StopAndDeleteCodec() */ TInt CMdfHwDeviceAdapter::StopAndDeleteCodec() { TInt stopError = Stop(); TInt deleteError = DeleteCodec(); if (stopError != KErrNone) { return stopError; } else { return deleteError; } }
// Free the encryption codec (address passed in sqlite3PagerSetCodec) static void PagerFreeCodec(void *pCodec) { BOTANSQLITE_TRACE("PagerFreeCodec"); if (pCodec) DeleteCodec(pCodec); }
int sqlite3_rekey(sqlite3 *db, const void *zKey, int nKey) { BOTANSQLITE_TRACE("sqlite3_rekey"); // Changes the encryption key for an existing database. int rc = SQLITE_ERROR; Btree *pbt = db->aDb[0].pBt; Pager *pPager = sqlite3BtreePager(pbt); void *pCodec = sqlite3PagerGetCodec(pPager); if ((!zKey || nKey <= 0) && !pCodec) { // Database not encrypted and key not specified. Do nothing return SQLITE_OK; } if (!pCodec) { // Database not encrypted, but key specified. Encrypt database pCodec = InitializeNewCodec(db); assert(nKey >= 0); SetWriteKey(pCodec, (const char*) zKey, (size_t) nKey); if (HandleError(pCodec)) { DeleteCodec(pCodec); return SQLITE_ERROR; } sqlite3PagerSetCodec(pPager, Codec, CodecSizeChange, PagerFreeCodec, pCodec); } else if (!zKey || nKey <= 0) { // Database encrypted, but key not specified. Decrypt database // Keep read key, drop write key DropWriteKey(pCodec); } else { // Database encrypted and key specified. Re-encrypt database with new key // Keep read key, change write key to new key assert(nKey >= 0); SetWriteKey(pCodec, (const char*) zKey, (size_t) nKey); if (HandleError(pCodec)) return SQLITE_ERROR; } // Start transaction rc = sqlite3BtreeBeginTrans(pbt, 1); if (rc == SQLITE_OK) { // Rewrite all pages using the new encryption key (if specified) int nPageCount = -1; sqlite3PagerPagecount(pPager, &nPageCount); Pgno nPage = (Pgno) nPageCount; Pgno nSkip = PAGER_MJ_PGNO(pPager); DbPage *pPage; Pgno n; for (n = 1; rc == SQLITE_OK && n <= nPage; n++) { if (n == nSkip) continue; rc = sqlite3PagerGet(pPager, n, &pPage, 0); if (rc == SQLITE_OK) { rc = sqlite3PagerWrite(pPage); sqlite3PagerUnref(pPage); } else { sqlite3ErrorWithMsg(db, SQLITE_ERROR, "%s", "Error while rekeying database page. Transaction Canceled."); } } } else { sqlite3ErrorWithMsg(db, SQLITE_ERROR, "%s", "Error beginning rekey transaction. Make sure that the current encryption key is correct."); } if (rc == SQLITE_OK) { // All good, commit rc = sqlite3BtreeCommit(pbt); if (rc == SQLITE_OK) { //Database rekeyed and committed successfully, update read key if (HasWriteKey(pCodec)) { SetReadIsWrite(pCodec); } else //No write key == no longer encrypted { sqlite3PagerSetCodec(pPager, NULL, NULL, NULL, NULL); } } else { //FIXME: can't trigger this, not sure if rollback is needed, reference implementation didn't rollback sqlite3ErrorWithMsg(db, SQLITE_ERROR, "%s", "Could not commit rekey transaction."); } } else { // Rollback, rekey failed sqlite3BtreeRollback(pbt, SQLITE_ERROR, 0); // go back to read key if (HasReadKey(pCodec)) { SetWriteIsRead(pCodec); } else //Database wasn't encrypted to start with { sqlite3PagerSetCodec(pPager, NULL, NULL, NULL, NULL); } } return rc; }
void ODM_RemoveOD(ODManager *odm) { ODManager *t; Channel *ch; ODM_Stop(odm, 1); /*disconnect sub-scene*/ if (odm->subscene) IS_Disconnect(odm->subscene); /*remove remote OD if any - break links to avoid distroying twice the parent ODM*/ if (odm->remote_OD) { t = odm->remote_OD; if (t->net_service && (t->net_service->owner != t)) t->net_service->nb_odm_users--; odm->remote_OD = NULL; t->parent_OD = NULL; ODM_RemoveOD(t); } /*then delete all the OD channels associated with this service*/ while (ChainGetCount(odm->channels)) { ch = ChainGetEntry(odm->channels, 0); ODM_DeleteChannel(odm, ch); } if (odm->net_service) { if (odm->net_service->owner == odm) { if (odm->net_service->nb_odm_users) odm->net_service->nb_odm_users--; /*detach it!!*/ odm->net_service->owner = NULL; /*try to assign a new root in case this is not scene shutdown*/ if (odm->net_service->nb_odm_users && odm->parentscene) { u32 i; for (i=0; i<ChainGetCount(odm->parentscene->ODlist); i++) { ODManager *new_root = ChainGetEntry(odm->parentscene->ODlist, i); if (new_root == odm) continue; while (new_root->remote_OD) new_root = new_root->remote_OD; if (new_root->net_service != odm->net_service) continue; new_root->net_service->owner = new_root; break; } } } if (!odm->net_service->nb_odm_users) Term_CloseService(odm->term, odm->net_service); odm->net_service = NULL; } /*last thing to do, unload the decoders if no channels associated*/ if (odm->codec) { assert(!ChainGetCount(odm->codec->inChannels)); MM_RemoveCodec(odm->term->mediaman, odm->codec); DeleteCodec(odm->codec); } if (odm->ocr_codec) { assert(!ChainGetCount(odm->ocr_codec->inChannels)); MM_RemoveCodec(odm->term->mediaman, odm->ocr_codec); DeleteCodec(odm->ocr_codec); } if (odm->oci_codec) { assert(!ChainGetCount(odm->oci_codec->inChannels)); MM_RemoveCodec(odm->term->mediaman, odm->oci_codec); DeleteCodec(odm->oci_codec); } /*delete from the parent scene.*/ if (odm->parentscene) { IS_RemoveOD(odm->parentscene, odm); if (odm->subscene) IS_Delete(odm->subscene); if (odm->parent_OD) odm->parent_OD->remote_OD = NULL; ODM_Delete(odm); return; } /*this is the scene root OD (may be a remote OD ..) */ if (odm->term->root_scene) { M4Event evt; assert(odm->term->root_scene == odm->subscene); IS_Delete(odm->subscene); /*reset main pointer*/ odm->term->root_scene = NULL; evt.type = M4E_CONNECT; evt.connect.is_connected = 0; M4USER_SENDEVENT(odm->term->user, &evt); } /*delete the ODMan*/ ODM_Delete(odm); }
// Free the encryption codec, called from pager.c (address passed in sqlite3PagerSetCodec) void sqlite3PagerFreeCodec(void *pCodec) { if (pCodec) DeleteCodec(pCodec); }