"Fossies" - the Fresh Open Source Software Archive

Member "src/Main/Forms/ChangePasswordDialog.cpp" (10 Oct 2018, 8825 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ChangePasswordDialog.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.22_Source_vs_1.23_Source.

    1 /*
    2  Derived from source code of TrueCrypt 7.1a, which is
    3  Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
    4  by the TrueCrypt License 3.0.
    5 
    6  Modifications and additions to the original source code (contained in this file)
    7  and all other portions of this file are Copyright (c) 2013-2017 IDRIX
    8  and are governed by the Apache License 2.0 the full text of which is
    9  contained in the file License.txt included in VeraCrypt binary and source
   10  code distribution packages.
   11 */
   12 
   13 #include "System.h"
   14 #include "Main/Main.h"
   15 #include "Main/GraphicUserInterface.h"
   16 #include "ChangePasswordDialog.h"
   17 #include "WaitDialog.h"
   18 
   19 namespace VeraCrypt
   20 {
   21 #ifdef TC_MACOSX
   22 
   23     bool ChangePasswordDialog::ProcessEvent(wxEvent& event)
   24     {
   25         if(GraphicUserInterface::HandlePasswordEntryCustomEvent (event))
   26             return true;
   27         else
   28             return ChangePasswordDialogBase::ProcessEvent(event);
   29     }
   30 #endif
   31 
   32     ChangePasswordDialog::ChangePasswordDialog (wxWindow* parent, shared_ptr <VolumePath> volumePath, Mode::Enum mode, shared_ptr <VolumePassword> password, shared_ptr <KeyfileList> keyfiles, shared_ptr <VolumePassword> newPassword, shared_ptr <KeyfileList> newKeyfiles)
   33         : ChangePasswordDialogBase (parent), DialogMode (mode), Path (volumePath)
   34     {
   35         bool enableNewPassword = false;
   36         bool enableNewKeyfiles = false;
   37         bool enablePkcs5Prf = false;
   38         bool isTrueCryptFile = false;
   39         
   40         if (volumePath && volumePath->HasTrueCryptExtension ())
   41         {
   42             isTrueCryptFile = true;
   43         }
   44 
   45         switch (mode)
   46         {
   47         case Mode::ChangePasswordAndKeyfiles:
   48             enableNewPassword = true;
   49             enableNewKeyfiles = true;
   50             enablePkcs5Prf = true;
   51             SetTitle (_("Change Volume Password and Keyfiles"));
   52             break;
   53 
   54         case Mode::ChangeKeyfiles:
   55             enableNewKeyfiles = true;
   56             SetTitle (_("Add/Remove Keyfiles to/from Volume"));
   57             break;
   58 
   59         case Mode::RemoveAllKeyfiles:
   60             SetTitle (_("Remove All Keyfiles from Volume"));
   61             break;
   62 
   63         case Mode::ChangePkcs5Prf:
   64             enablePkcs5Prf = true;
   65             SetTitle (_("Change Header Key Derivation Algorithm"));
   66             break;
   67 
   68         default:
   69             throw ParameterIncorrect (SRC_POS);
   70         }
   71         
   72 #ifdef TC_MACOSX
   73         GraphicUserInterface::InstallPasswordEntryCustomKeyboardShortcuts (this);
   74 #endif
   75 
   76         CurrentPasswordPanel = new VolumePasswordPanel (this, NULL, password, false, keyfiles, false, true, true, false, true, true);
   77         CurrentPasswordPanel->UpdateEvent.Connect (EventConnector <ChangePasswordDialog> (this, &ChangePasswordDialog::OnPasswordPanelUpdate));
   78         CurrentPasswordPanel->SetTrueCryptMode (isTrueCryptFile);
   79         CurrentPasswordPanelSizer->Add (CurrentPasswordPanel, 1, wxALL | wxEXPAND);
   80 
   81         NewPasswordPanel = new VolumePasswordPanel (this, NULL, newPassword, true, newKeyfiles, false, enableNewPassword, enableNewKeyfiles, enableNewPassword, enablePkcs5Prf);
   82         NewPasswordPanel->UpdateEvent.Connect (EventConnector <ChangePasswordDialog> (this, &ChangePasswordDialog::OnPasswordPanelUpdate));
   83         NewPasswordPanelSizer->Add (NewPasswordPanel, 1, wxALL | wxEXPAND);
   84 
   85         if (mode == Mode::RemoveAllKeyfiles)
   86             NewSizer->Show (false);
   87 
   88         Layout();
   89         Fit();
   90         Center();
   91 
   92         OnPasswordPanelUpdate();
   93         CurrentPasswordPanel->SetFocusToPasswordTextCtrl();
   94     }
   95 
   96     ChangePasswordDialog::~ChangePasswordDialog ()
   97     {
   98         CurrentPasswordPanel->UpdateEvent.Disconnect (this);
   99         NewPasswordPanel->UpdateEvent.Disconnect (this);
  100     }
  101 
  102     void ChangePasswordDialog::OnOKButtonClick (wxCommandEvent& event)
  103     {
  104         // Avoid a GTK bug
  105         if (!OKButton->IsEnabled())
  106             return;
  107 
  108         try
  109         {
  110             bool bUnsupportedKdf = false;
  111             shared_ptr <Pkcs5Kdf> currentKdf = CurrentPasswordPanel->GetPkcs5Kdf(bUnsupportedKdf);
  112             if (bUnsupportedKdf)
  113             {
  114                 Gui->ShowWarning (LangString ["ALGO_NOT_SUPPORTED_FOR_TRUECRYPT_MODE"]);
  115                 return;
  116             }
  117             int currentPim = CurrentPasswordPanel->GetVolumePim();
  118             if (-1 == currentPim)
  119             {
  120                 CurrentPasswordPanel->SetFocusToPimTextCtrl();
  121                 return;
  122             }
  123 
  124             shared_ptr <VolumePassword> newPassword;
  125             int newPim = 0;
  126             if (DialogMode == Mode::ChangePasswordAndKeyfiles)
  127             {
  128                 try
  129                 {
  130                     newPassword = NewPasswordPanel->GetPassword();
  131                 }
  132                 catch (PasswordException& e)
  133                 {
  134                     Gui->ShowWarning (e);
  135                     NewPasswordPanel->SetFocusToPasswordTextCtrl();
  136                     return;
  137                 }
  138                 newPim = NewPasswordPanel->GetVolumePim();
  139                 if (-1 == newPim)
  140                 {
  141                     NewPasswordPanel->SetFocusToPimTextCtrl();
  142                     return;
  143                 }
  144 
  145                 if (newPassword->Size() > 0)
  146                 {
  147                     if (newPassword->Size() < VolumePassword::WarningSizeThreshold)
  148                     {
  149                         if (newPim > 0 && newPim < 485)
  150                         {
  151                             Gui->ShowError ("PIM_REQUIRE_LONG_PASSWORD");
  152                             return;
  153                         }
  154 
  155                         if (!Gui->AskYesNo (LangString ["PASSWORD_LENGTH_WARNING"], false, true))
  156                         {
  157                             NewPasswordPanel->SetFocusToPasswordTextCtrl();
  158                             return;
  159                         }
  160                     }
  161                     else if (newPim > 0 && newPim < 485)
  162                     {
  163                         if (!Gui->AskYesNo (LangString ["PIM_SMALL_WARNING"], false, true))
  164                         {
  165                             NewPasswordPanel->SetFocusToPimTextCtrl();
  166                             return;
  167                         }
  168                     }
  169                 }
  170             }
  171             else
  172             {
  173                 newPassword = CurrentPasswordPanel->GetPassword();
  174                 newPim = CurrentPasswordPanel->GetVolumePim();
  175             }
  176 
  177             shared_ptr <KeyfileList> newKeyfiles;
  178             if (DialogMode == Mode::ChangePasswordAndKeyfiles || DialogMode == Mode::ChangeKeyfiles)
  179                 newKeyfiles = NewPasswordPanel->GetKeyfiles();
  180             else if (DialogMode != Mode::RemoveAllKeyfiles)
  181                 newKeyfiles = CurrentPasswordPanel->GetKeyfiles();
  182 
  183             /* force the display of the random enriching interface */
  184             RandomNumberGenerator::SetEnrichedByUserStatus (false);
  185             Gui->UserEnrichRandomPool (this, NewPasswordPanel->GetPkcs5Kdf(bUnsupportedKdf) ? NewPasswordPanel->GetPkcs5Kdf(bUnsupportedKdf)->GetHash() : shared_ptr <Hash>());
  186 
  187             {
  188 #ifdef TC_UNIX
  189                 // Temporarily take ownership of a device if the user is not an administrator
  190                 UserId origDeviceOwner ((uid_t) -1);
  191 
  192                 if (!Core->HasAdminPrivileges() && Path->IsDevice())
  193                 {
  194                     origDeviceOwner = FilesystemPath (wstring (*Path)).GetOwner();
  195                     Core->SetFileOwner (*Path, UserId (getuid()));
  196                 }
  197 
  198                 finally_do_arg2 (FilesystemPath, *Path, UserId, origDeviceOwner,
  199                 {
  200                     if (finally_arg2.SystemId != (uid_t) -1)
  201                         Core->SetFileOwner (finally_arg, finally_arg2);
  202                 });
  203 #endif
  204                 wxBusyCursor busy;
  205                 ChangePasswordThreadRoutine routine(Path,   Gui->GetPreferences().DefaultMountOptions.PreserveTimestamps,
  206                     CurrentPasswordPanel->GetPassword(), CurrentPasswordPanel->GetVolumePim(), CurrentPasswordPanel->GetPkcs5Kdf(bUnsupportedKdf), CurrentPasswordPanel->GetTrueCryptMode(),CurrentPasswordPanel->GetKeyfiles(),
  207                     newPassword, newPim, newKeyfiles, NewPasswordPanel->GetPkcs5Kdf(bUnsupportedKdf), NewPasswordPanel->GetHeaderWipeCount());
  208                 Gui->ExecuteWaitThreadRoutine (this, &routine);
  209             }
  210 
  211             switch (DialogMode)
  212             {
  213             case Mode::ChangePasswordAndKeyfiles:
  214                 Gui->ShowInfo ("PASSWORD_CHANGED");
  215                 break;
  216 
  217             case Mode::ChangeKeyfiles:
  218             case Mode::RemoveAllKeyfiles:
  219                 Gui->ShowInfo ("KEYFILE_CHANGED");
  220                 break;
  221 
  222             case Mode::ChangePkcs5Prf:
  223                 Gui->ShowInfo ("PKCS5_PRF_CHANGED");
  224                 break;
  225 
  226             default:
  227                 throw ParameterIncorrect (SRC_POS);
  228             }
  229 
  230             EndModal (wxID_OK);
  231         }
  232         catch (UnportablePassword &e)
  233         {
  234             Gui->ShowError (e);
  235             NewPasswordPanel->SetFocusToPasswordTextCtrl();
  236         }
  237         catch (PasswordException &e)
  238         {
  239             Gui->ShowWarning (e);
  240             CurrentPasswordPanel->SetFocusToPasswordTextCtrl();
  241         }
  242         catch (exception &e)
  243         {
  244             Gui->ShowError (e);
  245         }
  246     }
  247 
  248     void ChangePasswordDialog::OnPasswordPanelUpdate ()
  249     {
  250         bool ok = true;
  251 
  252         try
  253         {
  254 
  255             bool passwordEmpty = CurrentPasswordPanel->GetPassword()->IsEmpty();
  256             bool keyfilesEmpty = !CurrentPasswordPanel->GetKeyfiles() || CurrentPasswordPanel->GetKeyfiles()->empty();
  257 
  258             if (passwordEmpty && keyfilesEmpty)
  259                 ok = false;
  260 
  261             if (CurrentPasswordPanel->GetVolumePim () == -1)
  262                 ok = false;
  263 
  264             if (DialogMode == Mode::RemoveAllKeyfiles && (passwordEmpty || keyfilesEmpty))
  265                 ok = false;
  266 
  267             if (DialogMode == Mode::ChangePasswordAndKeyfiles || DialogMode == Mode::ChangeKeyfiles)
  268             {
  269                 bool newKeyfilesEmpty = !NewPasswordPanel->GetKeyfiles() || NewPasswordPanel->GetKeyfiles()->empty();
  270 
  271                 if (DialogMode == Mode::ChangeKeyfiles
  272                     && ((passwordEmpty && newKeyfilesEmpty) || (keyfilesEmpty && newKeyfilesEmpty)))
  273                     ok = false;
  274 
  275                 if (DialogMode == Mode::ChangePasswordAndKeyfiles
  276                     && (    (NewPasswordPanel->GetPassword()->IsEmpty() && newKeyfilesEmpty)
  277                         ||  !NewPasswordPanel->PasswordsMatch()
  278                         ||  (NewPasswordPanel->GetVolumePim() == -1)
  279                         )
  280                     )
  281                     ok = false;
  282             }
  283         }
  284         catch (PasswordException&)
  285         {
  286             ok = false;
  287         }
  288 
  289         OKButton->Enable (ok);
  290 
  291         if (DialogMode == Mode::ChangePasswordAndKeyfiles)
  292         {
  293             bool pimChanged = (CurrentPasswordPanel->GetVolumePim() != NewPasswordPanel->GetVolumePim());
  294             NewPasswordPanel->UpdatePimHelpText(pimChanged);
  295         }
  296 
  297     }
  298 }