20#include <winpr/config.h>
21#include <winpr/wlog.h>
22#include <winpr/crypto.h>
141#include <winpr/crt.h>
142#include <winpr/collections.h>
144static wListDictionary* g_ProtectedMemoryBlocks =
nullptr;
146BOOL CryptProtectMemory(LPVOID pData, DWORD cbData, DWORD dwFlags)
148 BYTE* pCipherText =
nullptr;
151 WINPR_CIPHER_CTX* enc =
nullptr;
152 BYTE randomKey[256] = WINPR_C_ARRAY_INIT;
155 if (dwFlags != CRYPTPROTECTMEMORY_SAME_PROCESS)
158 if (winpr_RAND(randomKey,
sizeof(randomKey)) < 0)
161 if (!g_ProtectedMemoryBlocks)
163 g_ProtectedMemoryBlocks = ListDictionary_New(TRUE);
165 if (!g_ProtectedMemoryBlocks)
174 pMemBlock->pData = pData;
175 pMemBlock->cbData = cbData;
176 pMemBlock->dwFlags = dwFlags;
178 if (winpr_RAND(pMemBlock->salt, 8) < 0)
181 if (winpr_Cipher_BytesToKey(WINPR_CIPHER_AES_256_CBC, WINPR_MD_SHA1, pMemBlock->salt, randomKey,
182 sizeof(randomKey), 4, pMemBlock->key, pMemBlock->iv) <= 0)
185 SecureZeroMemory(randomKey,
sizeof(randomKey));
187 cbOut = pMemBlock->cbData + 16 - 1;
188 pCipherText = (BYTE*)calloc(1, cbOut);
193 if ((enc = winpr_Cipher_NewEx(WINPR_CIPHER_AES_256_CBC, WINPR_ENCRYPT, pMemBlock->key,
194 sizeof(pMemBlock->key), pMemBlock->iv,
sizeof(pMemBlock->iv))) ==
197 if (!winpr_Cipher_Update(enc, pMemBlock->pData, pMemBlock->cbData, pCipherText, &cbOut))
199 if (!winpr_Cipher_Final(enc, pCipherText + cbOut, &cbFinal))
201 winpr_Cipher_Free(enc);
203 CopyMemory(pMemBlock->pData, pCipherText, pMemBlock->cbData);
206 return ListDictionary_Add(g_ProtectedMemoryBlocks, pData, pMemBlock);
210 winpr_Cipher_Free(enc);
215BOOL CryptUnprotectMemory(LPVOID pData, WINPR_ATTR_UNUSED DWORD cbData, DWORD dwFlags)
217 BYTE* pPlainText =
nullptr;
220 WINPR_CIPHER_CTX* dec =
nullptr;
223 if (dwFlags != CRYPTPROTECTMEMORY_SAME_PROCESS)
226 if (!g_ProtectedMemoryBlocks)
235 cbOut = pMemBlock->cbData + 16 - 1;
237 pPlainText = (BYTE*)malloc(cbOut);
242 if ((dec = winpr_Cipher_NewEx(WINPR_CIPHER_AES_256_CBC, WINPR_DECRYPT, pMemBlock->key,
243 sizeof(pMemBlock->key), pMemBlock->iv,
sizeof(pMemBlock->iv))) ==
246 if (!winpr_Cipher_Update(dec, pMemBlock->pData, pMemBlock->cbData, pPlainText, &cbOut))
248 if (!winpr_Cipher_Final(dec, pPlainText + cbOut, &cbFinal))
250 winpr_Cipher_Free(dec);
252 CopyMemory(pMemBlock->pData, pPlainText, pMemBlock->cbData);
253 SecureZeroMemory(pPlainText, pMemBlock->cbData);
256 ListDictionary_Remove(g_ProtectedMemoryBlocks, pData);
265 winpr_Cipher_Free(dec);
269BOOL CryptProtectData(WINPR_ATTR_UNUSED
DATA_BLOB* pDataIn, WINPR_ATTR_UNUSED LPCWSTR szDataDescr,
270 WINPR_ATTR_UNUSED
DATA_BLOB* pOptionalEntropy,
271 WINPR_ATTR_UNUSED PVOID pvReserved,
273 WINPR_ATTR_UNUSED DWORD dwFlags, WINPR_ATTR_UNUSED
DATA_BLOB* pDataOut)
275 WLog_ERR(
"TODO",
"TODO: Implement");
279BOOL CryptUnprotectData(WINPR_ATTR_UNUSED
DATA_BLOB* pDataIn,
280 WINPR_ATTR_UNUSED LPWSTR* ppszDataDescr,
281 WINPR_ATTR_UNUSED
DATA_BLOB* pOptionalEntropy,
282 WINPR_ATTR_UNUSED PVOID pvReserved,
284 WINPR_ATTR_UNUSED DWORD dwFlags, WINPR_ATTR_UNUSED
DATA_BLOB* pDataOut)
286 WLog_ERR(
"TODO",
"TODO: Implement");
290BOOL CryptStringToBinaryW(WINPR_ATTR_UNUSED LPCWSTR pszString, WINPR_ATTR_UNUSED DWORD cchString,
291 WINPR_ATTR_UNUSED DWORD dwFlags, WINPR_ATTR_UNUSED BYTE* pbBinary,
292 WINPR_ATTR_UNUSED DWORD* pcbBinary, WINPR_ATTR_UNUSED DWORD* pdwSkip,
293 WINPR_ATTR_UNUSED DWORD* pdwFlags)
295 WLog_ERR(
"TODO",
"TODO: Implement");
299BOOL CryptStringToBinaryA(WINPR_ATTR_UNUSED LPCSTR pszString, WINPR_ATTR_UNUSED DWORD cchString,
300 WINPR_ATTR_UNUSED DWORD dwFlags, WINPR_ATTR_UNUSED BYTE* pbBinary,
301 WINPR_ATTR_UNUSED DWORD* pcbBinary, WINPR_ATTR_UNUSED DWORD* pdwSkip,
302 WINPR_ATTR_UNUSED DWORD* pdwFlags)
304 WLog_ERR(
"TODO",
"TODO: Implement");
308BOOL CryptBinaryToStringW(WINPR_ATTR_UNUSED CONST BYTE* pbBinary, WINPR_ATTR_UNUSED DWORD cbBinary,
309 WINPR_ATTR_UNUSED DWORD dwFlags, WINPR_ATTR_UNUSED LPWSTR pszString,
310 WINPR_ATTR_UNUSED DWORD* pcchString)
312 WLog_ERR(
"TODO",
"TODO: Implement");
316BOOL CryptBinaryToStringA(WINPR_ATTR_UNUSED CONST BYTE* pbBinary, WINPR_ATTR_UNUSED DWORD cbBinary,
317 WINPR_ATTR_UNUSED DWORD dwFlags, WINPR_ATTR_UNUSED LPSTR pszString,
318 WINPR_ATTR_UNUSED DWORD* pcchString)
320 WLog_ERR(
"TODO",
"TODO: Implement");