forked from steganos-oss/locknote2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocknote.cpp
More file actions
443 lines (383 loc) · 10.8 KB
/
Copy pathlocknote.cpp
File metadata and controls
443 lines (383 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Steganos LockNote - self-modifying encrypted notepad
// Copyright (C) 2006-2023 Steganos GmbH
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "stdafx.h"
#include <atlframe.h>
#include <atlctrls.h>
#include <atldlgs.h>
#include <process.h>
#include <algorithm>
#include <array>
#include <atlfile.h>
#include "resource.h"
#include "locknoteView.h"
#include "AboutDlg.h"
#include "PasswordDlg.h"
#include "MainFrm.h"
#include "aeslayer.cpp" // prevents having to include precompiled header in aeslayer.cpp
CAppModule _Module;
namespace
{
constexpr int kFileRetryCount = 300;
constexpr DWORD kFileRetrySleepMs = 100;
bool RetryCopyFile(const TCHAR* source, const TCHAR* target)
{
for (int attempt = 0; attempt < kFileRetryCount; ++attempt)
{
if (::CopyFile(source, target, FALSE))
{
return true;
}
const DWORD lastError = ::GetLastError();
if (lastError != ERROR_SHARING_VIOLATION && lastError != ERROR_ACCESS_DENIED)
{
return false;
}
::Sleep(kFileRetrySleepMs);
}
return false;
}
bool RetryDeleteFile(const TCHAR* target)
{
for (int attempt = 0; attempt < kFileRetryCount; ++attempt)
{
if (!::PathFileExists(target))
{
return true;
}
if (::DeleteFile(target))
{
return true;
}
const DWORD lastError = ::GetLastError();
if (lastError != ERROR_SHARING_VIOLATION && lastError != ERROR_ACCESS_DENIED)
{
return false;
}
::Sleep(kFileRetrySleepMs);
}
return !::PathFileExists(target);
}
bool StageWritebackFromMainFrame(const CMainFrame& wndMain, std::string password)
{
std::string encryptedData;
if (!wndMain.m_text.empty())
{
Utils::EncryptString(
wndMain.m_text,
password,
encryptedData,
Utils::ParseKdfModeValue(wndMain.GetKdfMode()));
}
else
{
Utils::MessageBox(nullptr, WSTR(IDS_TEXT_IS_ENCRYPTED), MB_OK | MB_ICONINFORMATION);
}
std::array<wchar_t, MAX_PATH> modulePath{};
std::array<wchar_t, MAX_PATH> tempPath{};
std::array<wchar_t, MAX_PATH> fileName{};
const DWORD modulePathLength = ::GetModuleFileNameW(Utils::GetModuleHandle(), modulePath.data(), static_cast<DWORD>(modulePath.size()));
if (modulePathLength == 0 || modulePathLength >= modulePath.size())
{
return false;
}
const DWORD tempPathLength = ::GetTempPathW(static_cast<DWORD>(tempPath.size()), tempPath.data());
if (tempPathLength == 0 || tempPathLength >= tempPath.size())
{
return false;
}
if (::GetTempFileNameW(tempPath.data(), L"STG", 0, fileName.data()) == 0)
{
return false;
}
if (!::CopyFileW(modulePath.data(), fileName.data(), FALSE))
{
::DeleteFileW(fileName.data());
return false;
}
const std::string fileNameUtf8 = wstring_to_utf8(fileName.data());
if (fileNameUtf8.empty())
{
::DeleteFileW(fileName.data());
return false;
}
bool writeResult = Utils::UpdateResource(fileNameUtf8, "CONTENT", "PAYLOAD", encryptedData);
LOCKNOTEWINTRAITS traits{};
traits.m_nWindowSizeX = wndMain.m_nWindowSizeX;
traits.m_nWindowSizeY = wndMain.m_nWindowSizeY;
traits.m_nFontSize = wndMain.m_nFontSize;
traits.m_nLangId = wndMain.GetLanguage();
traits.m_nKdfMode = wndMain.GetKdfMode();
traits.m_nThemeMode = wndMain.GetThemeMode();
traits.m_strFontName = wndMain.m_strFontName;
writeResult &= Utils::WriteWinTraitsResources(fileNameUtf8, traits);
if (!writeResult)
{
::DeleteFileW(fileName.data());
return false;
}
const intptr_t spawnResult = _tspawnl(
_P_NOWAIT,
fileName.data(),
fileName.data(),
_T("-writeback"),
modulePath.data(),
nullptr);
if (spawnResult == -1)
{
::DeleteFileW(fileName.data());
return false;
}
return true;
}
}
int Run(LPTSTR /*lpstrCmdLine*/ = NULL, int nCmdShow = SW_SHOWDEFAULT)
{
TCHAR szModulePath[MAX_PATH] = {'\0'};
const DWORD modulePathLength = ::GetModuleFileName(Utils::GetModuleHandle(), szModulePath, MAX_PATH);
if (modulePathLength == 0 || modulePathLength >= MAX_PATH)
{
return -1;
}
CMessageLoop theLoop;
_Module.AddMessageLoop(&theLoop);
if (__argc == 3)
{
#ifdef _UNICODE
TCHAR* lpszCommand = __wargv[1];
TCHAR* lpszPath = __wargv[2];
#else
char* lpszCommand = __argv[1];
char* lpszPath = __argv[2];
#endif
if (!_tcscmp(lpszCommand, _T("-writeback")))
{
if (!RetryCopyFile(szModulePath, lpszPath))
{
return -1;
}
const intptr_t spawnResult = _tspawnl(_P_NOWAIT, lpszPath, lpszPath, _T("-erase"), szModulePath, nullptr);
if (spawnResult == -1)
{
return -1;
}
return 0;
}
else if (!_tcscmp(lpszCommand, _T("-erase")))
{
if (!RetryDeleteFile(lpszPath))
{
return -1;
}
return 0;
}
}
if (__argc > 1)
{
int nResult = Utils::MessageBox(NULL, WSTR(IDS_ASK_CONVERT_FILES), MB_YESNO | MB_ICONQUESTION);
if (nResult == IDYES)
{
int nConverted = 0;
std::string encryptPassword;
for (int nIndex = 1; nIndex < __argc; nIndex++)
{
#ifdef _UNICODE
const std::string filename = wstring_to_utf8(__wargv[nIndex]);
#else
const std::string filename = __argv[nIndex];
#endif
if (HasExtension(filename, ".txt"))
{
std::string newfilename = filename.substr(0, filename.size() - 4);
newfilename += ".exe";
std::string text;
std::string password;
if (LoadTextFromFile(filename, text, password))
{
if (SaveTextToFile(newfilename, text, encryptPassword))
{
nConverted += 1;
}
}
}
else
{
std::array<wchar_t, 512> message{};
const std::wstring wideFilename = utf8_to_wstring(filename);
swprintf_s(message.data(), message.size(), WSTR(IDS_CANT_CONVERT).c_str(), wideFilename.c_str());
Utils::MessageBox(NULL, message.data(), MB_OK | MB_ICONERROR);
}
}
if (nConverted)
{
std::array<wchar_t, 256> message{};
swprintf_s(message.data(), message.size(), WSTR(IDS_CONVERT_DONE).c_str(), nConverted);
Utils::MessageBox(NULL, message.data(), MB_OK | MB_ICONINFORMATION);
}
}
return 0;
}
std::string szFileMappingName = wstring_to_utf8(szModulePath);
std::replace_if(
szFileMappingName.begin(),
szFileMappingName.end(),
[](const char ch)
{
return ch == '\\' || ch == ':';
},
'_');
CAtlFileMapping<HWND> fmSingleInstanceHWND;
BOOL bAlreadyExisted = FALSE;
fmSingleInstanceHWND.MapSharedMem(sizeof(HWND), utf8_to_wstring(szFileMappingName).c_str(), &bAlreadyExisted);
if (bAlreadyExisted)
{
HWND hWndMain = *fmSingleInstanceHWND;
::SetForegroundWindow(hWndMain);
return 0;
}
std::string text;
std::string data;
std::string password;
Utils::LoadResource("CONTENT", "PAYLOAD", data);
if (!data.empty())
{
password = GetPasswordDlg();
if (password.empty())
{
return -1;
}
if (!Utils::DecryptString(data, password, text))
{
MessageBox(NULL, WSTR(IDS_INVALID_PASSWORD), MB_OK | MB_ICONERROR);
return -1;
}
}
else
{
text = STR(IDS_WELCOME);
}
CMainFrame wndMain;
wndMain.m_password = password;
wndMain.m_text = text;
// get window sizes from resource
std::string strSizeX;
std::string strSizeY;
Utils::LoadResource("SIZEX", "INFORMATION", strSizeX);
Utils::LoadResource("SIZEY", "INFORMATION", strSizeY);
int parsedValue = 0;
if (Utils::TryParseInt(strSizeX, parsedValue))
{
wndMain.m_nWindowSizeX = parsedValue;
}
if (Utils::TryParseInt(strSizeY, parsedValue))
{
wndMain.m_nWindowSizeY = parsedValue;
}
// get font size
std::string fontsize;
Utils::LoadResource("FONTSIZE", "INFORMATION", fontsize);
if (Utils::TryParseInt(fontsize, parsedValue))
{
wndMain.m_nFontSize = parsedValue;
}
else
{
wndMain.m_nFontSize = DEFAULT_FONT_SIZE;
}
// get font set
std::string fontface;
Utils::LoadResource("TYPEFACE", "INFORMATION", fontface);
if (!fontface.empty())
{
// font typeface format: "Lucida Console" (example)
wndMain.m_strFontName = fontface;
}
else
{
wndMain.m_strFontName = DEFAULT_FONT_NAME;
}
// get language id
std::string language;
Utils::LoadResource("LANGID", "INFORMATION", language);
if (Utils::TryParseInt(language, parsedValue))
{
wndMain.SetLanguage(parsedValue);
}
else
{
wndMain.SetLanguage(0);
}
std::string kdfMode;
Utils::LoadResource("KDFMODE", "INFORMATION", kdfMode);
if (Utils::TryParseInt(kdfMode, parsedValue))
{
wndMain.SetKdfMode(parsedValue);
}
else
{
wndMain.SetKdfMode(static_cast<int>(AESLayer::KdfMode::Scrypt));
}
std::string themeMode;
Utils::LoadResource("THEMEMODE", "INFORMATION", themeMode);
if (Utils::TryParseInt(themeMode, parsedValue))
{
wndMain.SetThemeMode(parsedValue);
}
else
{
wndMain.SetThemeMode(static_cast<int>(ThemeMode::System));
}
wndMain.PrepareInitialWindowSizeForCreate();
RECT initialWindowRect{ 0, 0, wndMain.m_nWindowSizeX, wndMain.m_nWindowSizeY };
// create window
if (wndMain.CreateEx(nullptr, &initialWindowRect) == nullptr)
{
ATLTRACE(_T("Main window creation failed!\n"));
return 0;
}
wndMain.CheckFontSize();
wndMain.CheckFontTypeFace();
*fmSingleInstanceHWND = wndMain.m_hWnd;
wndMain.ShowWindow(nCmdShow);
int nRet = theLoop.Run();
_Module.RemoveMessageLoop();
if (((wndMain.m_text != text) || (wndMain.m_password != password) || wndMain.m_bTraitsChanged) && (wndMain.m_password.size()))
{
password = wndMain.m_password;
if (!StageWritebackFromMainFrame(wndMain, password))
{
Utils::MessageBox(nullptr, L"Saving changes failed.", MB_OK | MB_ICONERROR);
}
}
return nRet;
}
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{
HRESULT hRes = ::CoInitialize(NULL);
// If you are running on NT 4.0 or higher you can use the following call instead to
// make the EXE free threaded. This means that calls come in on a random RPC thread.
// HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
ATLASSERT(SUCCEEDED(hRes));
// this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used
::DefWindowProc(NULL, 0, 0, 0L);
AtlInitCommonControls(ICC_BAR_CLASSES); // add flags to support other controls
hRes = _Module.Init(NULL, hInstance);
ATLASSERT(SUCCEEDED(hRes));
int nRet = Run(lpstrCmdLine, nCmdShow);
_Module.Term();
::CoUninitialize();
return nRet;
}