|
|
1.1 root 1: /*
2: Copyright (c) 2008 TrueCrypt Foundation. All rights reserved.
3:
4: Governed by the TrueCrypt License 2.4 the full text of which is contained
5: in the file License.txt included in TrueCrypt binary and source code
6: distribution packages.
7: */
8:
9: #include "System.h"
10: #include "Main/GraphicUserInterface.h"
11: #include "KeyfilesDialog.h"
12: #include "VolumePasswordPanel.h"
13:
14: namespace TrueCrypt
15: {
16: VolumePasswordPanel::VolumePasswordPanel (wxWindow* parent, shared_ptr <VolumePassword> password, shared_ptr <KeyfileList> keyfiles, bool enableCache, bool enablePassword, bool enableKeyfiles, bool enableConfirmation, bool enablePkcs5Prf, const wxString &passwordLabel)
17: : VolumePasswordPanelBase (parent), Keyfiles (new KeyfileList)
18: {
19: if (keyfiles)
20: {
21: *Keyfiles = *keyfiles;
22: UseKeyfilesCheckBox->SetValue (!Keyfiles->empty());
23: }
24: else
25: {
26: *Keyfiles = Gui->GetPreferences().DefaultKeyfiles;
27: UseKeyfilesCheckBox->SetValue (Gui->GetPreferences().UseKeyfiles && !Keyfiles->empty());
28: }
29:
30: PasswordTextCtrl->SetMaxLength (VolumePassword::MaxSize);
31: ConfirmPasswordTextCtrl->SetMaxLength (VolumePassword::MaxSize);
32:
33: if (!passwordLabel.empty())
34: {
35: PasswordStaticText->SetLabel (passwordLabel);
36: GridBagSizer->Detach (PasswordStaticText);
37: GridBagSizer->Add (PasswordStaticText, wxGBPosition (0, 1), wxGBSpan (1, 1), wxALIGN_CENTER_VERTICAL | wxBOTTOM, Gui->GetDefaultBorderSize());
38: }
39:
40: CacheCheckBox->Show (enableCache);
41:
42: if (!enablePassword && enableKeyfiles)
43: {
44: Layout();
45: Fit();
46: PasswordPlaceholderSizer->SetMinSize (wxSize (PasswordTextCtrl->GetSize().GetWidth(), -1));
47: }
48: else if (!enablePkcs5Prf)
49: {
50: GridBagSizer->Remove (PasswordPlaceholderSizer);
51: }
52:
53: PasswordStaticText->Show (enablePassword);
54: PasswordTextCtrl->Show (enablePassword);
55: DisplayPasswordCheckBox->Show (enablePassword);
56:
57: ConfirmPasswordStaticText->Show (enableConfirmation);
58: ConfirmPasswordTextCtrl->Show (enableConfirmation);
59:
60: UseKeyfilesCheckBox->Show (enableKeyfiles);
61: KeyfilesButton->Show (enableKeyfiles);
62:
63: Pkcs5PrfStaticText->Show (enablePkcs5Prf);
64: Pkcs5PrfChoice->Show (enablePkcs5Prf);
65:
66: if (enablePkcs5Prf)
67: {
68: foreach_ref (const Pkcs5Kdf &kdf, Pkcs5Kdf::GetAvailableAlgorithms())
69: {
70: if (!kdf.IsDeprecated())
71: Pkcs5PrfChoice->Append (kdf.GetName());
72: }
73: Pkcs5PrfChoice->Select (0);
74: }
75:
76: if (!enablePkcs5Prf || (!enablePassword && !enableKeyfiles))
77: {
78: GridBagSizer->Remove (Pkcs5PrfSizer);
79: }
80:
81: // Keyfiles drag & drop
82: class FileDropTarget : public wxFileDropTarget
83: {
84: public:
85: FileDropTarget (VolumePasswordPanel *panel) : Panel (panel) { }
86:
87: wxDragResult OnDragOver (wxCoord x, wxCoord y, wxDragResult def)
88: {
89: return wxDragLink;
90: }
91:
92: bool OnDropFiles (wxCoord x, wxCoord y, const wxArrayString &filenames)
93: {
94: foreach (const wxString &f, filenames)
95: Panel->AddKeyfile (make_shared <Keyfile> (wstring (f)));
96:
97: return true;
98: }
99:
100: protected:
101: VolumePasswordPanel *Panel;
102: };
103:
104: if (enableKeyfiles)
105: {
106: SetDropTarget (new FileDropTarget (this));
107: #ifdef TC_MACOSX
108: foreach (wxWindow *c, GetChildren())
109: c->SetDropTarget (new FileDropTarget (this));
110: #endif
111: }
112:
113: Layout();
114: Fit();
115: }
116:
117: VolumePasswordPanel::~VolumePasswordPanel ()
118: {
119: WipeTextCtrl (PasswordTextCtrl);
120: WipeTextCtrl (ConfirmPasswordTextCtrl);
121: }
122:
123: void VolumePasswordPanel::AddKeyfile (shared_ptr <Keyfile> keyfile)
124: {
125: if (!Keyfiles)
126: Keyfiles.reset (new KeyfileList);
127:
128: Keyfiles->push_back (keyfile);
129: UseKeyfilesCheckBox->SetValue (true);
130: }
131:
132: void VolumePasswordPanel::DisplayPassword (bool display, wxTextCtrl **textCtrl, int row)
133: {
134: FreezeScope freeze (this);
135:
136: wxTextCtrl *newTextCtrl = new wxTextCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, display ? 0 : wxTE_PASSWORD);
137: newTextCtrl->SetMaxLength (VolumePassword::MaxSize);
138: newTextCtrl->SetValue ((*textCtrl)->GetValue());
139: newTextCtrl->SetMinSize ((*textCtrl)->GetSize());
140:
141: GridBagSizer->Detach ((*textCtrl));
142: GridBagSizer->Add (newTextCtrl, wxGBPosition (row, 1), wxGBSpan (1, 2), wxEXPAND|wxBOTTOM|wxALIGN_CENTER_VERTICAL, 5);
143: (*textCtrl)->Show (false);
144: WipeTextCtrl (*textCtrl);
145:
146: Fit();
147: Layout();
148: newTextCtrl->SetMinSize ((*textCtrl)->GetMinSize());
149:
150: newTextCtrl->Connect (wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (VolumePasswordPanel::OnTextChanged), nullptr, this);
151: *textCtrl = newTextCtrl;
152: }
153:
154: shared_ptr <VolumePassword> VolumePasswordPanel::GetPassword () const
155: {
156: return GetPassword (PasswordTextCtrl);
157: }
158:
159: shared_ptr <VolumePassword> VolumePasswordPanel::GetPassword (wxTextCtrl *textCtrl) const
160: {
161: shared_ptr <VolumePassword> password;
162: wchar_t passwordBuf[VolumePassword::MaxSize + 1];
163: finally_do_arg (BufferPtr, BufferPtr (reinterpret_cast <byte *> (passwordBuf), sizeof (passwordBuf)), { finally_arg.Erase(); });
164:
165: #ifdef TC_WINDOWS
166: int len = GetWindowText (static_cast <HWND> (textCtrl->GetHandle()), passwordBuf, VolumePassword::MaxSize + 1);
167: password.reset (new VolumePassword (passwordBuf, len));
168: #else
169: const wxString &passwordStr = textCtrl->GetValue();
170: for (size_t i = 0; i < passwordStr.size() && i < VolumePassword::MaxSize; ++i)
171: {
172: passwordBuf[i] = (wchar_t) passwordStr[i];
173: const_cast <wchar_t *> (passwordStr.c_str())[i] = L'X';
174: }
175: password.reset (new VolumePassword (passwordBuf, passwordStr.size()));
176: #endif
177: return password;
178: }
179:
180: shared_ptr <Pkcs5Kdf> VolumePasswordPanel::GetPkcs5Kdf () const
181: {
182: try
183: {
184: return Pkcs5Kdf::GetAlgorithm (wstring (Pkcs5PrfChoice->GetStringSelection()));
185: }
186: catch (ParameterIncorrect&)
187: {
188: return shared_ptr <Pkcs5Kdf> ();
189: }
190: }
191:
192: void VolumePasswordPanel::OnAddKeyfileDirMenuItemSelected (wxCommandEvent& event)
193: {
194: DirectoryPath dir = Gui->SelectDirectory (this, LangString["SELECT_KEYFILE_PATH"]);
195:
196: if (!dir.IsEmpty())
197: {
198: Keyfiles->push_back (make_shared <Keyfile> (dir));
199:
200: UseKeyfilesCheckBox->SetValue (!Keyfiles->empty());
201: OnUpdate();
202: }
203: }
204:
205: void VolumePasswordPanel::OnAddKeyfilesMenuItemSelected (wxCommandEvent& event)
206: {
207: FilePathList files = Gui->SelectFiles (this, LangString["SELECT_KEYFILES"], false, true);
208:
209: if (!files.empty())
210: {
211: foreach_ref (const FilePath &f, files)
212: Keyfiles->push_back (make_shared <Keyfile> (f));
213:
214: UseKeyfilesCheckBox->SetValue (!Keyfiles->empty());
215: OnUpdate();
216: }
217: }
218:
219: void VolumePasswordPanel::OnDisplayPasswordCheckBoxClick (wxCommandEvent& event)
220: {
221: DisplayPassword (event.IsChecked(), &PasswordTextCtrl, 1);
222:
223: if (ConfirmPasswordTextCtrl->IsShown())
224: DisplayPassword (event.IsChecked(), &ConfirmPasswordTextCtrl, 2);
225:
226: OnUpdate();
227: }
228:
229: void VolumePasswordPanel::OnConfigureKeyfilesMenuItemSelected (wxCommandEvent& event)
230: {
231: KeyfilesDialog dialog (GetParent(), Keyfiles);
232:
233: if (dialog.ShowModal() == wxID_OK)
234: {
235: Keyfiles = dialog.GetKeyfiles();
236:
237: UseKeyfilesCheckBox->SetValue (!Keyfiles->empty());
238: OnUpdate();
239: }
240: }
241:
242: void VolumePasswordPanel::OnKeyfilesButtonClick (wxCommandEvent& event)
243: {
244: wxMenu popup;
245: Gui->AppendToMenu (popup, _("&Configure..."), this, wxCommandEventHandler (VolumePasswordPanel::OnConfigureKeyfilesMenuItemSelected));
246:
247: popup.AppendSeparator();
248: Gui->AppendToMenu (popup, _("Add &Files..."), this, wxCommandEventHandler (VolumePasswordPanel::OnAddKeyfilesMenuItemSelected));
249: Gui->AppendToMenu (popup, _("Add &Directory..."), this, wxCommandEventHandler (VolumePasswordPanel::OnAddKeyfileDirMenuItemSelected));
250:
251: PopupMenu (&popup, KeyfilesButton->GetPosition().x + 2, KeyfilesButton->GetPosition().y + 2);
252: }
253:
254: bool VolumePasswordPanel::PasswordsMatch () const
255: {
256: assert (ConfirmPasswordStaticText->IsShown());
257: return *GetPassword (PasswordTextCtrl) == *GetPassword (ConfirmPasswordTextCtrl);
258: }
259:
260: void VolumePasswordPanel::WipeTextCtrl (wxTextCtrl *textCtrl)
261: {
262: textCtrl->SetValue (wxString (L'X', textCtrl->GetLineLength(0)));
263: textCtrl->SetValue (L"");
264: }
265: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.