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 (!g_ProtectedMemoryBlocks)
160 g_ProtectedMemoryBlocks = ListDictionary_New(TRUE);
162 if (!g_ProtectedMemoryBlocks)
171 pMemBlock->pData = pData;
172 pMemBlock->cbData = cbData;
173 pMemBlock->dwFlags = dwFlags;
175 if (winpr_RAND(pMemBlock->salt, 8) < 0)
177 if (winpr_RAND(randomKey,
sizeof(randomKey)) < 0)
180 if (winpr_Cipher_BytesToKey(WINPR_CIPHER_AES_256_CBC, WINPR_MD_SHA1, pMemBlock->salt, randomKey,
181 sizeof(randomKey), 4, pMemBlock->key, pMemBlock->iv) <= 0)
184 SecureZeroMemory(randomKey,
sizeof(randomKey));
186 cbOut = pMemBlock->cbData + 16 - 1;
187 pCipherText = (BYTE*)calloc(1, cbOut);
192 if ((enc = winpr_Cipher_NewEx(WINPR_CIPHER_AES_256_CBC, WINPR_ENCRYPT, pMemBlock->key,
193 sizeof(pMemBlock->key), pMemBlock->iv,
sizeof(pMemBlock->iv))) ==
196 if (!winpr_Cipher_Update(enc, pMemBlock->pData, pMemBlock->cbData, pCipherText, &cbOut))
198 if (!winpr_Cipher_Final(enc, pCipherText + cbOut, &cbFinal))
200 winpr_Cipher_Free(enc);
202 CopyMemory(pMemBlock->pData, pCipherText, pMemBlock->cbData);
205 return ListDictionary_Add(g_ProtectedMemoryBlocks, pData, pMemBlock);
209 winpr_Cipher_Free(enc);
214BOOL CryptUnprotectMemory(LPVOID pData, WINPR_ATTR_UNUSED DWORD cbData, DWORD dwFlags)
216 BYTE* pPlainText =
nullptr;
219 WINPR_CIPHER_CTX* dec =
nullptr;
222 if (dwFlags != CRYPTPROTECTMEMORY_SAME_PROCESS)
225 if (!g_ProtectedMemoryBlocks)
234 cbOut = pMemBlock->cbData + 16 - 1;
236 pPlainText = (BYTE*)malloc(cbOut);
241 if ((dec = winpr_Cipher_NewEx(WINPR_CIPHER_AES_256_CBC, WINPR_DECRYPT, pMemBlock->key,
242 sizeof(pMemBlock->key), pMemBlock->iv,
sizeof(pMemBlock->iv))) ==
245 if (!winpr_Cipher_Update(dec, pMemBlock->pData, pMemBlock->cbData, pPlainText, &cbOut))
247 if (!winpr_Cipher_Final(dec, pPlainText + cbOut, &cbFinal))
249 winpr_Cipher_Free(dec);
251 CopyMemory(pMemBlock->pData, pPlainText, pMemBlock->cbData);
252 SecureZeroMemory(pPlainText, pMemBlock->cbData);
255 ListDictionary_Remove(g_ProtectedMemoryBlocks, pData);
264 winpr_Cipher_Free(dec);
268BOOL CryptProtectData(WINPR_ATTR_UNUSED
DATA_BLOB* pDataIn, WINPR_ATTR_UNUSED LPCWSTR szDataDescr,
269 WINPR_ATTR_UNUSED
DATA_BLOB* pOptionalEntropy,
270 WINPR_ATTR_UNUSED PVOID pvReserved,
272 WINPR_ATTR_UNUSED DWORD dwFlags, WINPR_ATTR_UNUSED
DATA_BLOB* pDataOut)
274 WLog_ERR(
"TODO",
"TODO: Implement");
278BOOL CryptUnprotectData(WINPR_ATTR_UNUSED
DATA_BLOB* pDataIn,
279 WINPR_ATTR_UNUSED LPWSTR* ppszDataDescr,
280 WINPR_ATTR_UNUSED
DATA_BLOB* pOptionalEntropy,
281 WINPR_ATTR_UNUSED PVOID pvReserved,
283 WINPR_ATTR_UNUSED DWORD dwFlags, WINPR_ATTR_UNUSED
DATA_BLOB* pDataOut)
285 WLog_ERR(
"TODO",
"TODO: Implement");
289BOOL CryptStringToBinaryW(WINPR_ATTR_UNUSED LPCWSTR pszString, WINPR_ATTR_UNUSED DWORD cchString,
290 WINPR_ATTR_UNUSED DWORD dwFlags, WINPR_ATTR_UNUSED BYTE* pbBinary,
291 WINPR_ATTR_UNUSED DWORD* pcbBinary, WINPR_ATTR_UNUSED DWORD* pdwSkip,
292 WINPR_ATTR_UNUSED DWORD* pdwFlags)
294 WLog_ERR(
"TODO",
"TODO: Implement");
298BOOL CryptStringToBinaryA(WINPR_ATTR_UNUSED LPCSTR pszString, WINPR_ATTR_UNUSED DWORD cchString,
299 WINPR_ATTR_UNUSED DWORD dwFlags, WINPR_ATTR_UNUSED BYTE* pbBinary,
300 WINPR_ATTR_UNUSED DWORD* pcbBinary, WINPR_ATTR_UNUSED DWORD* pdwSkip,
301 WINPR_ATTR_UNUSED DWORD* pdwFlags)
303 WLog_ERR(
"TODO",
"TODO: Implement");
307BOOL CryptBinaryToStringW(WINPR_ATTR_UNUSED CONST BYTE* pbBinary, WINPR_ATTR_UNUSED DWORD cbBinary,
308 WINPR_ATTR_UNUSED DWORD dwFlags, WINPR_ATTR_UNUSED LPWSTR pszString,
309 WINPR_ATTR_UNUSED DWORD* pcchString)
311 WLog_ERR(
"TODO",
"TODO: Implement");
315BOOL CryptBinaryToStringA(WINPR_ATTR_UNUSED CONST BYTE* pbBinary, WINPR_ATTR_UNUSED DWORD cbBinary,
316 WINPR_ATTR_UNUSED DWORD dwFlags, WINPR_ATTR_UNUSED LPSTR pszString,
317 WINPR_ATTR_UNUSED DWORD* pcchString)
319 WLog_ERR(
"TODO",
"TODO: Implement");