MCPcopy Create free account
hub / github.com/Tripwire/tripwire-open-source / ChangeEncryption

Method ChangeEncryption

src/tw/filemanipulator.cpp:229–322  ·  view source on GitHub ↗

Change the encryption on a file. If pNewKey is NULL, then encryption on the file will be removed. If pOldKey is NULL and the file is currently encrypted, then the function will throw eFileManip; If pOldKey is not NULL and the file is encrypted, then the fuction will throw eFileManip; If pOldKey is NULL and pNewKey is NULL and the file is not currently encrypted, the function will not do anything a

Source from the content-addressed store, hash-verified

227// return.
228// Returns on successful encryption change.
229void cFileManipulator::ChangeEncryption(const cElGamalSigPublicKey* pOldKey,
230 const cElGamalSigPrivateKey* pNewKey,
231 bool backup)
232{
233 ASSERT(mbInit);
234 if (!mbInit)
235 {
236 Init();
237 }
238
239 // check the pOldKey matches the current encryption state
240 if (mFileHeader.GetEncoding() != cFileHeader::ASYM_ENCRYPTION)
241 {
242 if (pOldKey != NULL)
243 throw eFileManipNotEncrypted(mFileName);
244 if (pNewKey == NULL)
245 return; // NOOP
246 }
247 else if (pOldKey == NULL)
248 throw eFileManipMissingKey();
249
250 if (!cFileUtil::FileWritable(mFileName))
251 throw eFileManipNotWritable(mFileName);
252
253 if (mFileHeader.GetID() == cFCODatabaseFile::GetFileHeaderID())
254 {
255 cFCODatabaseFile db;
256 bool encrypted;
257
258 cTWUtil::ReadDatabase(mFileName.c_str(), db, pOldKey, encrypted);
259 if (backup)
260 {
261 TSTRING backname = mFileName + iFSServices::GetInstance()->GetStandardBackupExtension();
262 iFSServices::GetInstance()->Rename(mFileName.c_str(), backname.c_str());
263 }
264
265 cTWUtil::WriteDatabase(mFileName.c_str(), db, pNewKey != NULL, pNewKey);
266 return;
267 }
268 else if (mFileHeader.GetID() == cFCOReport::GetFileHeaderID())
269 {
270 cFCOReport rep;
271 cFCOReportHeader reph;
272 bool encrypted;
273 cTWUtil::ReadReport(mFileName.c_str(), reph, rep, pOldKey, false, encrypted);
274 if (backup)
275 {
276 TSTRING backname = mFileName + iFSServices::GetInstance()->GetStandardBackupExtension();
277 iFSServices::GetInstance()->Rename(mFileName.c_str(), backname.c_str());
278 }
279 cTWUtil::WriteReport(mFileName.c_str(), reph, rep, pNewKey != NULL, pNewKey);
280 return;
281 }
282 else if (mFileHeader.GetID() == cConfigFile::GetFileHeaderID())
283 {
284 TSTRING configText;
285
286 iUserNotify::GetInstance()->Notify(iUserNotify::V_VERBOSE,

Callers 1

ExecuteMethod · 0.80

Calls 6

GetIDMethod · 0.80
c_strMethod · 0.80
RenameMethod · 0.80
NotifyMethod · 0.80
GetEncodingMethod · 0.45

Tested by

no test coverage detected