VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Main/Application.cpp
blob: cb118babf8906cf4654df9b1d7f95a1f83a07a53 (plain)
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
/*
 Derived from source code of TrueCrypt 7.1a, which is
 Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
 by the TrueCrypt License 3.0.

 Modifications and additions to the original source code (contained in this file)
 and all other portions of this file are Copyright (c) 2013-2017 IDRIX
 and are governed by the Apache License 2.0 the full text of which is
 contained in the file License.txt included in VeraCrypt binary and source
 code distribution packages.
*/

#include "System.h"
#include <wx/stdpaths.h>
#include "Main.h"
#include "Application.h"
#include "CommandLineInterface.h"
#ifndef TC_NO_GUI
#include "GraphicUserInterface.h"
#endif
#include "TextUserInterface.h"

namespace VeraCrypt
{
	namespace
	{
		void EnsureEndsWithPathSeparator( wxString &s )
		{
			const wxUniChar pathSeparator = wxFileName::GetPathSeparator();
			if (s[s.size() - 1] != pathSeparator)
				s.append(pathSeparator);
		}

		wxString *GetXdgConfigPath ()
		{
			const wxChar *xdgConfig = wxGetenv(wxT("XDG_CONFIG_HOME"));
			wxString *configDir;

			if (!wxIsEmpty(xdgConfig))
			{
				configDir = new wxString (xdgConfig);
				//wcerr << L"XDG_CONFIG_HOME=" << *configDir << endl;
				EnsureEndsWithPathSeparator(*configDir);
				configDir->append(Application::GetName());
			}
			else
			{
				#if !defined(TC_UNIX) || defined(TC_MACOSX) // Windows, OS X:
					configDir =
						new wxString (wxStandardPaths::Get().GetUserDataDir());
				#else // Linux, FreeBSD, Solaris:
					configDir = new wxString (wxFileName::GetHomeDir());
					configDir->append(wxT("/.config/"));
					configDir->append(Application::GetName());

					if (!wxDirExists(*configDir))
					{
						wxString legacyConfigDir = wxStandardPaths::Get().GetUserDataDir();
						//wcerr << L"Legacy config dir: " << legacyConfigDir << endl;
						if (wxDirExists(legacyConfigDir))
						{
							configDir->swap(legacyConfigDir);
						}
					}
				#endif
			}

			//wcerr << L"Config dir: " << *configDir << endl;
			return configDir;
		}
	}

	wxApp* Application::CreateConsoleApp ()
	{
		mUserInterface = new TextUserInterface;
		mUserInterfaceType = UserInterfaceType::Text;
		return mUserInterface;
	}

#ifndef TC_NO_GUI
	wxApp* Application::CreateGuiApp ()
	{
		mUserInterface = new GraphicUserInterface;
		mUserInterfaceType = UserInterfaceType::Graphic;
		return mUserInterface;
	}
#endif

	FilePath Application::GetConfigFilePath (const wxString &configFileName, bool createConfigDir)
	{
		static wxScopedPtr<const wxString> configDirC;
		static bool configDirExists = false;

		if (!configDirExists)
		{
			if (!configDirC)
			{
				wxString *configDir;

				if (Core->IsInPortableMode())
				{
					configDir = new wxString (
						wxPathOnly(wxStandardPaths::Get().GetExecutablePath()));
				}
				else
				{
					configDir = GetXdgConfigPath();
				}

				EnsureEndsWithPathSeparator(*configDir);
				configDirC.reset(configDir);
			}

			if (createConfigDir)
			{
				if (!wxDirExists(*configDirC))
				{
					//wcerr << L"Creating config dir »" << *configDirC << L"« ..." << endl;
					throw_sys_sub_if(
						!wxMkdir(*configDirC, wxS_IRUSR | wxS_IWUSR | wxS_IXUSR),
						configDirC->ToStdWstring());
				}
				configDirExists = true;
				//wcerr << L"Config directory »" << *configDirC << L"« exists now" << endl;
			}
		}

		return FilePath((*configDirC + configFileName).ToStdWstring());
	}

	DirectoryPath Application::GetExecutableDirectory ()
	{
		return wstring (wxFileName (wxStandardPaths::Get().GetExecutablePath()).GetPath());
	}

	FilePath Application::GetExecutablePath ()
	{
		return wstring (wxStandardPaths::Get().GetExecutablePath());
	}

	void Application::Initialize (UserInterfaceType::Enum type)
	{
		switch (type)
		{
		case UserInterfaceType::Text:
			{
				wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) CreateConsoleApp);
				break;
			}

#ifndef TC_NO_GUI
		case UserInterfaceType::Graphic:
			{
				wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) CreateGuiApp);
				break;
			}
#endif

		default:
			throw ParameterIncorrect (SRC_POS);
		}
	}

	int Application::ExitCode = 0;
	UserInterface *Application::mUserInterface = nullptr;
	UserInterfaceType::Enum Application::mUserInterfaceType;
}
pan> #define HAS_BCRYPTDERIVEKEYPBKDF2 #endif #ifdef HAS_BCRYPTDERIVEKEYPBKDF2 bool _zip_crypto_pbkdf2(const zip_uint8_t *key, zip_uint64_t key_length, const zip_uint8_t *salt, zip_uint16_t salt_length, zip_uint16_t iterations, zip_uint8_t *output, zip_uint16_t output_length) { BCRYPT_ALG_HANDLE hAlgorithm = NULL; bool result; if (!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&hAlgorithm, BCRYPT_SHA1_ALGORITHM, NULL, BCRYPT_ALG_HANDLE_HMAC_FLAG))) { return false; } result = BCRYPT_SUCCESS(BCryptDeriveKeyPBKDF2(hAlgorithm, (PUCHAR)key, (ULONG)key_length, (PUCHAR)salt, salt_length, iterations, output, output_length, 0)); BCryptCloseAlgorithmProvider(hAlgorithm, 0); return result; } #else #include <math.h> #define DIGEST_SIZE 20 #define BLOCK_SIZE 64 typedef struct { BCRYPT_ALG_HANDLE hAlgorithm; BCRYPT_HASH_HANDLE hInnerHash; BCRYPT_HASH_HANDLE hOuterHash; ULONG cbHashObject; PUCHAR pbInnerHash; PUCHAR pbOuterHash; } PRF_CTX; static void hmacFree(PRF_CTX *pContext) { if (pContext->hOuterHash) BCryptDestroyHash(pContext->hOuterHash); if (pContext->hInnerHash) BCryptDestroyHash(pContext->hInnerHash); free(pContext->pbOuterHash); free(pContext->pbInnerHash); if (pContext->hAlgorithm) BCryptCloseAlgorithmProvider(pContext->hAlgorithm, 0); } static BOOL hmacPrecomputeDigest(BCRYPT_HASH_HANDLE hHash, PUCHAR pbPassword, DWORD cbPassword, BYTE mask) { BYTE buffer[BLOCK_SIZE]; DWORD i; if (cbPassword > BLOCK_SIZE) { return FALSE; } memset(buffer, mask, sizeof(buffer)); for (i = 0; i < cbPassword; ++i) { buffer[i] = (char)(pbPassword[i] ^ mask); } return BCRYPT_SUCCESS(BCryptHashData(hHash, buffer, sizeof(buffer), 0)); } static BOOL hmacInit(PRF_CTX *pContext, PUCHAR pbPassword, DWORD cbPassword) { BOOL bStatus = FALSE; ULONG cbResult; BYTE key[DIGEST_SIZE]; if (!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&pContext->hAlgorithm, BCRYPT_SHA1_ALGORITHM, NULL, 0)) || !BCRYPT_SUCCESS(BCryptGetProperty(pContext->hAlgorithm, BCRYPT_OBJECT_LENGTH, (PUCHAR)&pContext->cbHashObject, sizeof(pContext->cbHashObject), &cbResult, 0)) || ((pContext->pbInnerHash = malloc(pContext->cbHashObject)) == NULL) || ((pContext->pbOuterHash = malloc(pContext->cbHashObject)) == NULL) || !BCRYPT_SUCCESS(BCryptCreateHash(pContext->hAlgorithm, &pContext->hInnerHash, pContext->pbInnerHash, pContext->cbHashObject, NULL, 0, 0)) || !BCRYPT_SUCCESS(BCryptCreateHash(pContext->hAlgorithm, &pContext->hOuterHash, pContext->pbOuterHash, pContext->cbHashObject, NULL, 0, 0))) { goto hmacInit_end; } if (cbPassword > BLOCK_SIZE) { BCRYPT_HASH_HANDLE hHash = NULL; PUCHAR pbHashObject = malloc(pContext->cbHashObject); if (pbHashObject == NULL) { goto hmacInit_end; } bStatus = BCRYPT_SUCCESS(BCryptCreateHash(pContext->hAlgorithm, &hHash, pbHashObject, pContext->cbHashObject, NULL, 0, 0)) && BCRYPT_SUCCESS(BCryptHashData(hHash, pbPassword, cbPassword, 0)) && BCRYPT_SUCCESS(BCryptGetProperty(hHash, BCRYPT_HASH_LENGTH, (PUCHAR)&cbPassword, sizeof(cbPassword), &cbResult, 0)) && BCRYPT_SUCCESS(BCryptFinishHash(hHash, key, cbPassword, 0)); if (hHash) BCryptDestroyHash(hHash); free(pbHashObject); if (!bStatus) { goto hmacInit_end; } pbPassword = key; } bStatus = hmacPrecomputeDigest(pContext->hInnerHash, pbPassword, cbPassword, 0x36) && hmacPrecomputeDigest(pContext->hOuterHash, pbPassword, cbPassword, 0x5C); hmacInit_end: if (bStatus == FALSE) hmacFree(pContext); return bStatus; } static BOOL hmacCalculateInternal(BCRYPT_HASH_HANDLE hHashTemplate, PUCHAR pbData, DWORD cbData, PUCHAR pbOutput, DWORD cbOutput, DWORD cbHashObject) { BOOL success = FALSE; BCRYPT_HASH_HANDLE hHash = NULL; PUCHAR pbHashObject = malloc(cbHashObject); if (pbHashObject == NULL) { return FALSE; } if (BCRYPT_SUCCESS(BCryptDuplicateHash(hHashTemplate, &hHash, pbHashObject, cbHashObject, 0))) { success = BCRYPT_SUCCESS(BCryptHashData(hHash, pbData, cbData, 0)) && BCRYPT_SUCCESS(BCryptFinishHash(hHash, pbOutput, cbOutput, 0)); BCryptDestroyHash(hHash); } free(pbHashObject); return success; } static BOOL hmacCalculate(PRF_CTX *pContext, PUCHAR pbData, DWORD cbData, PUCHAR pbDigest) { DWORD cbResult; DWORD cbHashObject; return BCRYPT_SUCCESS(BCryptGetProperty(pContext->hAlgorithm, BCRYPT_OBJECT_LENGTH, (PUCHAR)&cbHashObject, sizeof(cbHashObject), &cbResult, 0)) && hmacCalculateInternal(pContext->hInnerHash, pbData, cbData, pbDigest, DIGEST_SIZE, cbHashObject) && hmacCalculateInternal(pContext->hOuterHash, pbDigest, DIGEST_SIZE, pbDigest, DIGEST_SIZE, cbHashObject); } static void myxor(LPBYTE ptr1, LPBYTE ptr2, DWORD dwLen) { while (dwLen--) *ptr1++ ^= *ptr2++; } BOOL pbkdf2(PUCHAR pbPassword, ULONG cbPassword, PUCHAR pbSalt, ULONG cbSalt, DWORD cIterations, PUCHAR pbDerivedKey, ULONG cbDerivedKey) { BOOL bStatus = FALSE; DWORD l, r, dwULen, i, j; BYTE Ti[DIGEST_SIZE]; BYTE V[DIGEST_SIZE]; LPBYTE U = malloc(max((cbSalt + 4), DIGEST_SIZE)); PRF_CTX prfCtx = {0}; if (U == NULL) { return FALSE; } if (pbPassword == NULL || cbPassword == 0 || pbSalt == NULL || cbSalt == 0 || cIterations == 0 || pbDerivedKey == NULL || cbDerivedKey == 0) { free(U); return FALSE; } if (!hmacInit(&prfCtx, pbPassword, cbPassword)) { goto PBKDF2_end; } l = (DWORD)ceil((double)cbDerivedKey / (double)DIGEST_SIZE); r = cbDerivedKey - (l - 1) * DIGEST_SIZE; for (i = 1; i <= l; i++) { ZeroMemory(Ti, DIGEST_SIZE); for (j = 0; j < cIterations; j++) { if (j == 0) { /* construct first input for PRF */ (void)memcpy_s(U, cbSalt, pbSalt, cbSalt); U[cbSalt] = (BYTE)((i & 0xFF000000) >> 24); U[cbSalt + 1] = (BYTE)((i & 0x00FF0000) >> 16); U[cbSalt + 2] = (BYTE)((i & 0x0000FF00) >> 8); U[cbSalt + 3] = (BYTE)((i & 0x000000FF)); dwULen = cbSalt + 4; } else { (void)memcpy_s(U, DIGEST_SIZE, V, DIGEST_SIZE); dwULen = DIGEST_SIZE; } if (!hmacCalculate(&prfCtx, U, dwULen, V)) { goto PBKDF2_end; } myxor(Ti, V, DIGEST_SIZE); } if (i != l) { (void)memcpy_s(&pbDerivedKey[(i - 1) * DIGEST_SIZE], cbDerivedKey - (i - 1) * DIGEST_SIZE, Ti, DIGEST_SIZE); } else { /* Take only the first r bytes */ (void)memcpy_s(&pbDerivedKey[(i - 1) * DIGEST_SIZE], cbDerivedKey - (i - 1) * DIGEST_SIZE, Ti, r); } } bStatus = TRUE; PBKDF2_end: hmacFree(&prfCtx); free(U); return bStatus; } bool _zip_crypto_pbkdf2(const zip_uint8_t *key, zip_uint64_t key_length, const zip_uint8_t *salt, zip_uint16_t salt_length, zip_uint16_t iterations, zip_uint8_t *output, zip_uint16_t output_length) { return (key_length <= ZIP_UINT32_MAX) && pbkdf2((PUCHAR)key, (ULONG)key_length, (PUCHAR)salt, salt_length, iterations, output, output_length); } #endif struct _zip_crypto_aes_s { BCRYPT_ALG_HANDLE hAlgorithm; BCRYPT_KEY_HANDLE hKey; ULONG cbKeyObject; PUCHAR pbKeyObject; }; _zip_crypto_aes_t * _zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error) { _zip_crypto_aes_t *aes = (_zip_crypto_aes_t *)calloc(1, sizeof(*aes)); ULONG cbResult; ULONG key_length = key_size / 8; if (aes == NULL) { zip_error_set(error, ZIP_ER_MEMORY, 0); return NULL; } if (!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&aes->hAlgorithm, BCRYPT_AES_ALGORITHM, NULL, 0))) { _zip_crypto_aes_free(aes); return NULL; } if (!BCRYPT_SUCCESS(BCryptSetProperty(aes->hAlgorithm, BCRYPT_CHAINING_MODE, (PUCHAR)BCRYPT_CHAIN_MODE_ECB, sizeof(BCRYPT_CHAIN_MODE_ECB), 0))) { _zip_crypto_aes_free(aes); return NULL; } if (!BCRYPT_SUCCESS(BCryptGetProperty(aes->hAlgorithm, BCRYPT_OBJECT_LENGTH, (PUCHAR)&aes->cbKeyObject, sizeof(aes->cbKeyObject), &cbResult, 0))) { _zip_crypto_aes_free(aes); return NULL; } aes->pbKeyObject = malloc(aes->cbKeyObject); if (aes->pbKeyObject == NULL) { _zip_crypto_aes_free(aes); zip_error_set(error, ZIP_ER_MEMORY, 0); return NULL; } if (!BCRYPT_SUCCESS(BCryptGenerateSymmetricKey(aes->hAlgorithm, &aes->hKey, aes->pbKeyObject, aes->cbKeyObject, (PUCHAR)key, key_length, 0))) { _zip_crypto_aes_free(aes); return NULL; } return aes; } void _zip_crypto_aes_free(_zip_crypto_aes_t *aes) { if (aes == NULL) { return; } if (aes->hKey != NULL) { BCryptDestroyKey(aes->hKey); } if (aes->pbKeyObject != NULL) { free(aes->pbKeyObject); } if (aes->hAlgorithm != NULL) { BCryptCloseAlgorithmProvider(aes->hAlgorithm, 0); } free(aes); } bool _zip_crypto_aes_encrypt_block(_zip_crypto_aes_t *aes, const zip_uint8_t *in, zip_uint8_t *out) { ULONG cbResult; NTSTATUS status = BCryptEncrypt(aes->hKey, (PUCHAR)in, ZIP_CRYPTO_AES_BLOCK_LENGTH, NULL, NULL, 0, (PUCHAR)out, ZIP_CRYPTO_AES_BLOCK_LENGTH, &cbResult, 0); return BCRYPT_SUCCESS(status); } struct _zip_crypto_hmac_s { BCRYPT_ALG_HANDLE hAlgorithm; BCRYPT_HASH_HANDLE hHash; DWORD cbHashObject; PUCHAR pbHashObject; DWORD cbHash; PUCHAR pbHash; }; /* https://code.msdn.microsoft.com/windowsdesktop/Hmac-Computation-Sample-11fe8ec1/sourcecode?fileId=42820&pathId=283874677 */ _zip_crypto_hmac_t * _zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error) { NTSTATUS status; ULONG cbResult; _zip_crypto_hmac_t *hmac; if (secret_length > INT_MAX) { zip_error_set(error, ZIP_ER_INVAL, 0); return NULL; } hmac = (_zip_crypto_hmac_t *)calloc(1, sizeof(*hmac)); if (hmac == NULL) { zip_error_set(error, ZIP_ER_MEMORY, 0); return NULL; } status = BCryptOpenAlgorithmProvider(&hmac->hAlgorithm, BCRYPT_SHA1_ALGORITHM, NULL, BCRYPT_ALG_HANDLE_HMAC_FLAG); if (!BCRYPT_SUCCESS(status)) { _zip_crypto_hmac_free(hmac); return NULL; } status = BCryptGetProperty(hmac->hAlgorithm, BCRYPT_OBJECT_LENGTH, (PUCHAR)&hmac->cbHashObject, sizeof(hmac->cbHashObject), &cbResult, 0); if (!BCRYPT_SUCCESS(status)) { _zip_crypto_hmac_free(hmac); return NULL; } hmac->pbHashObject = malloc(hmac->cbHashObject); if (hmac->pbHashObject == NULL) { _zip_crypto_hmac_free(hmac); zip_error_set(error, ZIP_ER_MEMORY, 0); return NULL; } status = BCryptGetProperty(hmac->hAlgorithm, BCRYPT_HASH_LENGTH, (PUCHAR)&hmac->cbHash, sizeof(hmac->cbHash), &cbResult, 0); if (!BCRYPT_SUCCESS(status)) { _zip_crypto_hmac_free(hmac); return NULL; } hmac->pbHash = malloc(hmac->cbHash); if (hmac->pbHash == NULL) { _zip_crypto_hmac_free(hmac); zip_error_set(error, ZIP_ER_MEMORY, 0); return NULL; } status = BCryptCreateHash(hmac->hAlgorithm, &hmac->hHash, hmac->pbHashObject, hmac->cbHashObject, (PUCHAR)secret, (ULONG)secret_length, 0); if (!BCRYPT_SUCCESS(status)) { _zip_crypto_hmac_free(hmac); return NULL; } return hmac; } void _zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac) { if (hmac == NULL) { return; } if (hmac->hHash != NULL) { BCryptDestroyHash(hmac->hHash); } if (hmac->pbHash != NULL) { free(hmac->pbHash); } if (hmac->pbHashObject != NULL) { free(hmac->pbHashObject); } if (hmac->hAlgorithm) { BCryptCloseAlgorithmProvider(hmac->hAlgorithm, 0); } free(hmac); } bool _zip_crypto_hmac(_zip_crypto_hmac_t *hmac, zip_uint8_t *data, zip_uint64_t length) { if (hmac == NULL || length > ULONG_MAX) { return false; } return BCRYPT_SUCCESS(BCryptHashData(hmac->hHash, data, (ULONG)length, 0)); } bool _zip_crypto_hmac_output(_zip_crypto_hmac_t *hmac, zip_uint8_t *data) { if (hmac == NULL) { return false; } return BCRYPT_SUCCESS(BCryptFinishHash(hmac->hHash, data, hmac->cbHash, 0)); } ZIP_EXTERN bool zip_secure_random(zip_uint8_t *buffer, zip_uint16_t length) { return BCRYPT_SUCCESS(BCryptGenRandom(NULL, buffer, length, BCRYPT_USE_SYSTEM_PREFERRED_RNG)); }