"Fossies" - the Fresh Open Source Software Archive

Member "src/Main/Forms/SecurityTokenKeyfilesDialog.cpp" (10 Oct 2018, 5928 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 "SecurityTokenKeyfilesDialog.cpp" see the Fossies "Dox" file reference documentation.

    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/GraphicUserInterface.h"
   15 #include "Common/SecurityToken.h"
   16 #include "NewSecurityTokenKeyfileDialog.h"
   17 #include "SecurityTokenKeyfilesDialog.h"
   18 
   19 namespace VeraCrypt
   20 {
   21     SecurityTokenKeyfilesDialog::SecurityTokenKeyfilesDialog (wxWindow* parent, bool selectionMode)
   22         : SecurityTokenKeyfilesDialogBase (parent)
   23     {
   24         if (selectionMode)
   25             SetTitle (LangString["SELECT_TOKEN_KEYFILES"]);
   26 
   27         list <int> colPermilles;
   28 
   29         SecurityTokenKeyfileListCtrl->InsertColumn (ColumnSecurityTokenSlotId, LangString["TOKEN_SLOT_ID"], wxLIST_FORMAT_CENTER, 1);
   30         colPermilles.push_back (102);
   31         SecurityTokenKeyfileListCtrl->InsertColumn (ColumnSecurityTokenLabel, LangString["TOKEN_NAME"], wxLIST_FORMAT_LEFT, 1);
   32         colPermilles.push_back (368);
   33         SecurityTokenKeyfileListCtrl->InsertColumn (ColumnSecurityTokenKeyfileLabel, LangString["TOKEN_DATA_OBJECT_LABEL"], wxLIST_FORMAT_LEFT, 1);
   34         colPermilles.push_back (529);
   35 
   36         FillSecurityTokenKeyfileListCtrl();
   37 
   38         Gui->SetListCtrlWidth (SecurityTokenKeyfileListCtrl, 65);
   39         Gui->SetListCtrlHeight (SecurityTokenKeyfileListCtrl, 16);
   40         Gui->SetListCtrlColumnWidths (SecurityTokenKeyfileListCtrl, colPermilles);
   41 
   42         Fit();
   43         Layout();
   44         Center();
   45 
   46         DeleteButton->Disable();
   47         ExportButton->Disable();
   48         OKButton->Disable();
   49         OKButton->SetDefault();
   50     }
   51 
   52     void SecurityTokenKeyfilesDialog::FillSecurityTokenKeyfileListCtrl ()
   53     {
   54         wxBusyCursor busy;
   55 
   56         SecurityTokenKeyfileListCtrl->DeleteAllItems();
   57         SecurityTokenKeyfileList = SecurityToken::GetAvailableKeyfiles();
   58 
   59         size_t i = 0;
   60         foreach (const SecurityTokenKeyfile &key, SecurityTokenKeyfileList)
   61         {
   62             vector <wstring> fields (SecurityTokenKeyfileListCtrl->GetColumnCount());
   63 
   64             fields[ColumnSecurityTokenSlotId] = StringConverter::ToWide ((uint64) key.SlotId);
   65             fields[ColumnSecurityTokenLabel] = key.Token.Label;
   66             fields[ColumnSecurityTokenKeyfileLabel] = key.Id;
   67 
   68             Gui->AppendToListCtrl (SecurityTokenKeyfileListCtrl, fields, 0, &SecurityTokenKeyfileList[i++]);
   69         }
   70     }
   71 
   72     void SecurityTokenKeyfilesDialog::OnDeleteButtonClick (wxCommandEvent& event)
   73     {
   74         try
   75         {
   76             if (!Gui->AskYesNo (LangString["CONFIRM_SEL_FILES_DELETE"]))
   77                 return;
   78 
   79             wxBusyCursor busy;
   80 
   81             foreach (long item, Gui->GetListCtrlSelectedItems (SecurityTokenKeyfileListCtrl))
   82             {
   83                 SecurityToken::DeleteKeyfile (*reinterpret_cast <SecurityTokenKeyfile *> (SecurityTokenKeyfileListCtrl->GetItemData (item)));
   84             }
   85 
   86             FillSecurityTokenKeyfileListCtrl();
   87         }
   88         catch (exception &e)
   89         {
   90             Gui->ShowError (e);
   91         }
   92     }
   93 
   94     void SecurityTokenKeyfilesDialog::OnExportButtonClick (wxCommandEvent& event)
   95     {
   96         try
   97         {
   98             foreach (long item, Gui->GetListCtrlSelectedItems (SecurityTokenKeyfileListCtrl))
   99             {
  100                 SecurityTokenKeyfile *keyfile = reinterpret_cast <SecurityTokenKeyfile *> (SecurityTokenKeyfileListCtrl->GetItemData (item));
  101 
  102                 FilePathList files = Gui->SelectFiles (this, wxEmptyString, true);
  103 
  104                 if (!files.empty())
  105                 {
  106                     wxBusyCursor busy;
  107 
  108                     vector <byte> keyfileData;
  109                     SecurityToken::GetKeyfileData (*keyfile, keyfileData);
  110 
  111                     BufferPtr keyfileDataBuf (&keyfileData.front(), keyfileData.size());
  112                     finally_do_arg (BufferPtr, keyfileDataBuf, { finally_arg.Erase(); });
  113 
  114                     File keyfile;
  115                     keyfile.Open (*files.front(), File::CreateWrite);
  116                     keyfile.Write (keyfileDataBuf);
  117                 }
  118                 else
  119                     break;
  120 
  121                 Gui->ShowInfo ("KEYFILE_EXPORTED");
  122             }
  123         }
  124         catch (exception &e)
  125         {
  126             Gui->ShowError (e);
  127         }
  128     }
  129 
  130     void SecurityTokenKeyfilesDialog::OnImportButtonClick (wxCommandEvent& event)
  131     {
  132         try
  133         {
  134             FilePathList keyfilePaths = Gui->SelectFiles (this, LangString["SELECT_KEYFILES"], false);
  135 
  136             if (keyfilePaths.empty())
  137                 return;
  138 
  139             FilePath keyfilePath = *keyfilePaths.front();
  140 
  141             File keyfile;
  142             keyfile.Open (keyfilePath, File::OpenRead, File::ShareReadWrite, File::PreserveTimestamps);
  143 
  144             if (keyfile.Length() > 0)
  145             {
  146                 vector <byte> keyfileData (keyfile.Length());
  147                 BufferPtr keyfileDataBuf (&keyfileData.front(), keyfileData.size());
  148 
  149                 keyfile.ReadCompleteBuffer (keyfileDataBuf);
  150                 finally_do_arg (BufferPtr, keyfileDataBuf, { finally_arg.Erase(); });
  151 
  152                 NewSecurityTokenKeyfileDialog newKeyfileDialog (this, keyfilePath.ToBaseName());
  153 
  154                 if (newKeyfileDialog.ShowModal() == wxID_OK)
  155                 {
  156                     wxBusyCursor busy;
  157                     SecurityToken::CreateKeyfile (newKeyfileDialog.GetSelectedSlotId(), keyfileData, StringConverter::ToSingle (newKeyfileDialog.GetKeyfileName()));
  158 
  159                     FillSecurityTokenKeyfileListCtrl();
  160                 }
  161             }
  162             else
  163                 throw InsufficientData (SRC_POS, keyfilePath);
  164         }
  165         catch (exception &e)
  166         {
  167             Gui->ShowError (e);
  168         }
  169     }
  170 
  171     void SecurityTokenKeyfilesDialog::OnListItemDeselected (wxListEvent& event)
  172     {
  173         if (SecurityTokenKeyfileListCtrl->GetSelectedItemCount() == 0)
  174         {
  175             DeleteButton->Disable();
  176             ExportButton->Disable();
  177             OKButton->Disable();
  178         }
  179     }
  180 
  181     void SecurityTokenKeyfilesDialog::OnListItemSelected (wxListEvent& event)
  182     {
  183         if (event.GetItem().GetData() != (wxUIntPtr) nullptr)
  184         {
  185             DeleteButton->Enable();
  186             ExportButton->Enable();
  187             OKButton->Enable();
  188         }
  189     }
  190 
  191     void SecurityTokenKeyfilesDialog::OnOKButtonClick ()
  192     {
  193         foreach (long item, Gui->GetListCtrlSelectedItems (SecurityTokenKeyfileListCtrl))
  194         {
  195             SecurityTokenKeyfile *key = reinterpret_cast <SecurityTokenKeyfile *> (SecurityTokenKeyfileListCtrl->GetItemData (item));
  196             SelectedSecurityTokenKeyfilePaths.push_back (*key);
  197         }
  198 
  199         EndModal (wxID_OK);
  200     }
  201 }