- // Commit the new blob to securityd, reencode the db blob, release the
- // cloned db handle and commit the new blob to the db.
- mClientSession.commitDbForSync(mSSDbHandle, clonedDbHandle,
- dbb, allocator());
- dbBlobId->modify(DBBlobRelationID, NULL, &dbb,
- CSSM_DB_MODIFY_ATTRIBUTE_NONE);
-
- // Commit the transaction to the db
- passThrough(CSSM_APPLEFILEDL_COMMIT, NULL);
- passThrough(CSSM_APPLEFILEDL_TOGGLE_AUTOCOMMIT,
- reinterpret_cast<const void *>(1));
- }
- catch (...)
- {
- // Something went wrong rollback the transaction
- passThrough(CSSM_APPLEFILEDL_ROLLBACK, NULL);
- passThrough(CSSM_APPLEFILEDL_TOGGLE_AUTOCOMMIT,
- reinterpret_cast<const void *>(1));
- throw;
- }
+uint32 SSDatabaseImpl::recodeHelper(SecurityServer::DbHandle clonedDbHandle, CssmClient::DbUniqueRecord& dbBlobId) {
+ // Recode all keys
+ DbCursor cursor(SSDatabase(this));
+ cursor->recordType(CSSM_DL_DB_RECORD_ALL_KEYS);
+ CssmDataContainer keyBlob(allocator());
+ CssmClient::DbUniqueRecord keyBlobId;
+ DbAttributes attributes;
+ while (cursor->next(&attributes, &keyBlob, keyBlobId))
+ {
+ KeyHandle keyHandle = 0;
+ try {
+ // Decode the old key
+ CssmKey::Header header;
+ keyHandle = mClientSession.decodeKey(mSSDbHandle, keyBlob, header);
+ // Recode the key
+ CssmDataContainer newKeyBlob(mClientSession.returnAllocator);
+ mClientSession.recodeKey(mSSDbHandle, keyHandle, clonedDbHandle, newKeyBlob);
+ mClientSession.releaseKey(keyHandle);
+ keyHandle = 0;
+ // Write the recoded key blob to the database
+ keyBlobId->modify(attributes.recordType(), NULL, &newKeyBlob,
+ CSSM_DB_MODIFY_ATTRIBUTE_NONE);
+ } catch (CssmError cssme) {
+ const char* errStr = cssmErrorString(cssme.error);
+ secdebugfunc("integrity", "corrupt item while recoding: %d %s", (int) cssme.error, errStr);
+ secdebugfunc("integrity", "deleting corrupt item");
+
+
+ keyBlobId->deleteRecord();
+
+ if(keyHandle != 0) {
+ // tell securityd not to worry about this key again
+ try {
+ secdebugfunc("integrity", "releasing corrupt key");
+ mClientSession.releaseKey(keyHandle);
+ } catch(CssmError cssme) {
+ // swallow the error
+ const char* errStr = cssmErrorString(cssme.error);
+ secdebugfunc("integrity", "couldn't release corrupt key: %d %s", (int) cssme.error, errStr);
+ }
+ }
+ }
+ }
+
+ // Commit the new blob to securityd, reencode the db blob, release the
+ // cloned db handle and commit the new blob to the db.
+ CssmDataContainer dbb(allocator());
+ secdebugfunc("integrity", "committing %d", clonedDbHandle);
+ mClientSession.commitDbForSync(mSSDbHandle, clonedDbHandle,
+ dbb, allocator());
+ dbBlobId->modify(DBBlobRelationID, NULL, &dbb,
+ CSSM_DB_MODIFY_ATTRIBUTE_NONE);
+ return getDbVersionFromBlob(dbb);