📄 tcformat.c
字号:
try
{
phase = BootEncObj->GetHiddenOSCreationPhase();
}
catch (Exception &e)
{
e.Show (MainDlg);
AbortProcess("ERR_GETTING_SYSTEM_ENCRYPTION_STATUS");
}
return phase;
}
// IMPORTANT: This function may be called only by Format (other modules can only _read_ the status).
// Returns TRUE if successful (otherwise FALSE)
static BOOL ChangeHiddenOSCreationPhase (int newPhase)
{
if (!CreateSysEncMutex ())
{
Error ("SYSTEM_ENCRYPTION_IN_PROGRESS_ELSEWHERE");
return FALSE;
}
try
{
BootEncObj->SetHiddenOSCreationPhase (newPhase);
}
catch (Exception &e)
{
e.Show (MainDlg);
return FALSE;
}
//// The contents of the following items might be inappropriate after a change of the phase
//szFileName[0] = 0;
//szDiskFile[0] = 0;
//nUIVolumeSize = 0;
//nVolumeSize = 0;
return TRUE;
}
// IMPORTANT: This function may be called only by Format (other modules can only _read_ the system encryption status).
// Returns TRUE if successful (otherwise FALSE)
static BOOL ChangeSystemEncryptionStatus (int newStatus)
{
if (!CreateSysEncMutex ())
{
Error ("SYSTEM_ENCRYPTION_IN_PROGRESS_ELSEWHERE");
return FALSE; // Only one instance that has the mutex can modify the system encryption settings
}
SystemEncryptionStatus = newStatus;
bSystemEncryptionStatusChanged = TRUE;
if (newStatus == SYSENC_STATUS_ENCRYPTING)
{
// If the user has created a hidden OS and now is creating a decoy OS, we must wipe the hidden OS
// config area in the MBR.
WipeHiddenOSCreationConfig();
}
if (newStatus == SYSENC_STATUS_NONE && !IsHiddenOSRunning())
{
if (DetermineHiddenOSCreationPhase() != TC_HIDDEN_OS_CREATION_PHASE_NONE
&& !ChangeHiddenOSCreationPhase (TC_HIDDEN_OS_CREATION_PHASE_NONE))
return FALSE;
WipeHiddenOSCreationConfig();
}
if (!SaveSysEncSettings (MainDlg))
{
return FALSE;
}
return TRUE;
}
// If the return code of this function is ignored and newWizardMode == WIZARD_MODE_SYS_DEVICE, then this function
// may be called only after CreateSysEncMutex() returns TRUE. It returns TRUE if successful (otherwise FALSE).
static BOOL ChangeWizardMode (int newWizardMode)
{
if (WizardMode != newWizardMode)
{
if (WizardMode == WIZARD_MODE_SYS_DEVICE || newWizardMode == WIZARD_MODE_SYS_DEVICE)
{
if (newWizardMode == WIZARD_MODE_SYS_DEVICE)
{
if (!CreateSysEncMutex ())
{
Error ("SYSTEM_ENCRYPTION_IN_PROGRESS_ELSEWHERE");
return FALSE;
}
}
// If the previous mode was different, the password may have been typed using a different
// keyboard layout (which might confuse the user and cause other problems if system encryption
// was or will be involved).
WipePasswordsAndKeyfiles();
}
if (newWizardMode != WIZARD_MODE_NONSYS_DEVICE)
bInPlaceEncNonSys = FALSE;
if (newWizardMode == WIZARD_MODE_NONSYS_DEVICE && !IsAdmin() && IsUacSupported())
{
if (!ElevateWholeWizardProcess ("/e"))
return FALSE;
}
// The contents of the following items may be inappropriate after a change of mode
szFileName[0] = 0;
szDiskFile[0] = 0;
nUIVolumeSize = 0;
nVolumeSize = 0;
WizardMode = newWizardMode;
}
bDevice = (WizardMode != WIZARD_MODE_FILE_CONTAINER);
if (newWizardMode != WIZARD_MODE_SYS_DEVICE
&& !bHiddenOS)
{
CloseSysEncMutex ();
}
return TRUE;
}
// Determines whether the wizard directly affects system encryption in any way.
// Note, for example, that when the user enters a password for a hidden volume that is to host a hidden OS,
// WizardMode is NOT set to WIZARD_MODE_SYS_DEVICE. The keyboard layout, however, has to be US. That's why
// this function has to be called instead of checking the value of WizardMode.
static BOOL SysEncInEffect (void)
{
return (WizardMode == WIZARD_MODE_SYS_DEVICE
|| CreatingHiddenSysVol());
}
static BOOL CreatingHiddenSysVol (void)
{
return (bHiddenOS
&& bHiddenVol && !bHiddenVolHost);
}
static void LoadSettings (HWND hwndDlg)
{
WipeAlgorithmId savedWipeAlgorithm = TC_WIPE_NONE;
LoadSysEncSettings (hwndDlg);
if (LoadNonSysInPlaceEncSettings (&savedWipeAlgorithm) != 0)
bInPlaceEncNonSysPending = TRUE;
defaultKeyFilesParam.EnableKeyFiles = FALSE;
bStartOnLogon = ConfigReadInt ("StartOnLogon", FALSE);
HiddenSectorDetectionStatus = ConfigReadInt ("HiddenSectorDetectionStatus", 0);
bHistory = ConfigReadInt ("SaveVolumeHistory", FALSE);
ConfigReadString ("SecurityTokenLibrary", "", SecurityTokenLibraryPath, sizeof (SecurityTokenLibraryPath) - 1);
if (SecurityTokenLibraryPath[0])
InitSecurityTokenLibrary();
if (hwndDlg != NULL)
{
LoadCombo (GetDlgItem (hwndDlg, IDC_COMBO_BOX));
return;
}
if (bHistoryCmdLine)
return;
}
static void SaveSettings (HWND hwndDlg)
{
WaitCursor ();
if (hwndDlg != NULL)
DumpCombo (GetDlgItem (hwndDlg, IDC_COMBO_BOX), !bHistory);
ConfigWriteBegin ();
ConfigWriteInt ("StartOnLogon", bStartOnLogon);
ConfigWriteInt ("HiddenSectorDetectionStatus", HiddenSectorDetectionStatus);
ConfigWriteInt ("SaveVolumeHistory", bHistory);
ConfigWriteString ("SecurityTokenLibrary", SecurityTokenLibraryPath[0] ? SecurityTokenLibraryPath : "");
if (GetPreferredLangId () != NULL)
ConfigWriteString ("Language", GetPreferredLangId ());
ConfigWriteEnd ();
NormalCursor ();
}
// WARNING: This function does NOT cause immediate application exit (use e.g. return 1 after calling it
// from a DialogProc function).
static void EndMainDlg (HWND hwndDlg)
{
if (nCurPageNo == VOLUME_LOCATION_PAGE)
{
if (IsWindow(GetDlgItem(hCurPage, IDC_NO_HISTORY)))
bHistory = !IsButtonChecked (GetDlgItem (hCurPage, IDC_NO_HISTORY));
MoveEditToCombo (GetDlgItem (hCurPage, IDC_COMBO_BOX), bHistory);
SaveSettings (hCurPage);
}
else
{
SaveSettings (NULL);
}
SaveSysEncSettings (hwndDlg);
if (!bHistory)
CleanLastVisitedMRU ();
EndDialog (hwndDlg, 0);
}
// Returns TRUE if system encryption or decryption had been or is in progress and has not been completed
static BOOL SysEncryptionOrDecryptionRequired (void)
{
/* If you update this function, revise SysEncryptionOrDecryptionRequired() in Mount.c as well. */
static BootEncryptionStatus locBootEncStatus;
try
{
locBootEncStatus = BootEncObj->GetStatus();
}
catch (Exception &e)
{
e.Show (MainDlg);
}
return (SystemEncryptionStatus == SYSENC_STATUS_ENCRYPTING
|| SystemEncryptionStatus == SYSENC_STATUS_DECRYPTING
||
(
locBootEncStatus.DriveMounted
&&
(
locBootEncStatus.ConfiguredEncryptedAreaStart != locBootEncStatus.EncryptedAreaStart
|| locBootEncStatus.ConfiguredEncryptedAreaEnd != locBootEncStatus.EncryptedAreaEnd
)
)
);
}
// Returns TRUE if the system partition/drive is completely encrypted
static BOOL SysDriveOrPartitionFullyEncrypted (BOOL bSilent)
{
/* If you update this function, revise SysDriveOrPartitionFullyEncrypted() in Mount.c as well. */
static BootEncryptionStatus locBootEncStatus;
try
{
locBootEncStatus = BootEncObj->GetStatus();
}
catch (Exception &e)
{
if (!bSilent)
e.Show (MainDlg);
}
return (!locBootEncStatus.SetupInProgress
&& locBootEncStatus.ConfiguredEncryptedAreaEnd != 0
&& locBootEncStatus.ConfiguredEncryptedAreaEnd != -1
&& locBootEncStatus.ConfiguredEncryptedAreaStart == locBootEncStatus.EncryptedAreaStart
&& locBootEncStatus.ConfiguredEncryptedAreaEnd == locBootEncStatus.EncryptedAreaEnd);
}
// Adds or removes the wizard to/from the system startup sequence
void ManageStartupSeqWiz (BOOL bRemove, const char *arg)
{
char regk [64];
// Split the string in order to prevent some antivirus packages from falsely reporting
// TrueCrypt Format.exe to contain a possible Trojan horse because of this string (heuristic scan).
sprintf (regk, "%s%s", "Software\\Microsoft\\Windows\\Curren", "tVersion\\Run");
if (!bRemove)
{
char exe[MAX_PATH * 2] = { '"' };
GetModuleFileName (NULL, exe + 1, sizeof (exe) - 1);
if (strlen (arg) > 0)
{
strcat (exe, "\" ");
strcat (exe, arg);
}
WriteRegistryString (regk, "TrueCrypt Format", exe);
}
else
DeleteRegistryValue (regk, "TrueCrypt Format");
}
// This functions is to be used when the wizard mode needs to be changed to WIZARD_MODE_SYS_DEVICE.
// If the function fails to switch the mode, it returns FALSE (otherwise TRUE).
BOOL SwitchWizardToSysEncMode (void)
{
WaitCursor ();
try
{
BootEncStatus = BootEncObj->GetStatus();
bWholeSysDrive = BootEncObj->SystemPartitionCoversWholeDrive();
}
catch (Exception &e)
{
e.Show (MainDlg);
Error ("ERR_GETTING_SYSTEM_ENCRYPTION_STATUS");
NormalCursor ();
return FALSE;
}
// From now on, we should be the only instance of the TC wizard allowed to deal with system encryption
if (!CreateSysEncMutex ())
{
Warning ("SYSTEM_ENCRYPTION_IN_PROGRESS_ELSEWHERE");
NormalCursor ();
return FALSE;
}
// User-mode app may have crashed and its mutex may have gotten lost, so we need to check the driver status too
if (BootEncStatus.SetupInProgress)
{
if (AskWarnYesNo ("SYSTEM_ENCRYPTION_RESUME_PROMPT") == IDYES)
{
if (SystemEncryptionStatus != SYSENC_STATUS_ENCRYPTING
&& SystemEncryptionStatus != SYSENC_STATUS_DECRYPTING)
{
// The config file with status was lost or not written correctly
if (!ResolveUnknownSysEncDirection ())
{
CloseSysEncMutex ();
NormalCursor ();
return FALSE;
}
}
bDirectSysEncMode = TRUE;
ChangeWizardMode (WIZARD_MODE_SYS_DEVICE);
LoadPage (MainDlg, SYSENC_ENCRYPTION_PAGE);
NormalCursor ();
return TRUE;
}
else
{
CloseSysEncMutex ();
Error ("SYS_ENCRYPTION_OR_DECRYPTION_IN_PROGRESS");
NormalCursor ();
return FALSE;
}
}
if (BootEncStatus.DriveMounted
|| BootEncStatus.DriveEncrypted
|| SysEncryptionOrDecryptionRequired ())
{
if (!SysDriveOrPartitionFullyEncrypted (FALSE)
&& AskWarnYesNo ("SYSTEM_ENCRYPTION_RESUME_PROMPT") == IDYES)
{
if (SystemEncryptionStatus == SYSENC_STATUS_NONE)
{
// If the config file with status was lost or not written correctly, we
// don't know whether to encrypt or decrypt (but we know that encryption or
// decryption is required). Ask the user to select encryption, decryption,
// or cancel
if (!ResolveUnknownSysEncDirection ())
{
CloseSysEncMutex ();
NormalCursor ();
return FALSE;
}
}
bDirectSysEncMode = TRUE;
ChangeWizardMode (WIZARD_MODE_SYS_DEVICE);
LoadPage (MainDlg, SYSENC_ENCRYPTION_PAGE);
NormalCursor ();
return TRUE;
}
else
{
CloseSysEncMutex ();
Error ("SETUP_FAILED_BOOT_DRIVE_ENCRYPTED");
NormalCursor ();
return FALSE;
}
}
else
{
// Check compliance with requirements for boot encryption
if (!IsAdmin())
{
if (!IsUacSupported())
{
Warning ("ADMIN_PRIVILEGES_WARN_DEVICES");
}
}
try
{
BootEncObj->CheckRequirements ();
}
catch (Exception &e)
{
CloseSysEncMutex ();
e.Show (MainDlg);
NormalCursor ();
return FALSE;
}
if (!ChangeWizardMode (WIZARD_MODE_SYS_DEVICE))
{
NormalCursor ();
return FALSE;
}
if (bSysDriveSelected || bSysPartitionSelected)
{
// The user selected the non-sys-device wizard mode but then selected a system device
bWholeSysDrive = (bSysDriveSelected && !bSysPartitionSelected);
bSysDriveSelected = FALSE;
bSysPartitionSelected = FALSE;
try
{
if (!bHiddenVol)
{
if (bWholeSysDrive && !BootEncObj->SystemPartitionCoversWholeDrive())
{
if (nCurrentOS != WIN_VISTA_OR_LATER)
{
if (BootEncObj->SystemDriveContainsExtendedPartition())
{
bWholeSysDrive = FALSE;
Error ("WDE_UNSUPPORTED_FOR_EXTENDED_PARTITIONS");
if (AskYesNo ("ASK_ENCRYPT_PARTITION_INSTEAD_OF_DRIVE") == IDNO)
{
ChangeWizardMode (WIZARD_MODE_NONSYS_DEVICE);
return FALSE;
}
}
else
Warning ("WDE_EXTENDED_PARTITIONS_WARNING");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -