| 1 |
|
| 2 |
#include <winpr/crt.h> |
| 3 |
#include <winpr/print.h> |
| 4 |
#include <winpr/crypto.h> |
| 5 |
#include <winpr/ssl.h> |
| 6 |
#include <winpr/wlog.h> |
| 7 |
|
| 8 |
static const char* SECRET_PASSWORD_TEST = "MySecretPassword123!"; |
| 9 |
|
| 10 |
int TestCryptoProtectMemory(int argc, char* argv[]) |
| 11 |
{ |
| 12 |
int rc = -1; |
| 13 |
UINT32 cbPlainText = 0; |
| 14 |
UINT32 cbCipherText = 0; |
| 15 |
const char* pPlainText = nullptr; |
| 16 |
BYTE* pCipherText = nullptr; |
| 17 |
|
| 18 |
WINPR_UNUSED(argc); |
| 19 |
WINPR_UNUSED(argv); |
| 20 |
|
| 21 |
pPlainText = SECRET_PASSWORD_TEST; |
| 22 |
cbPlainText = strlen(pPlainText) + 1; |
| 23 |
cbCipherText = cbPlainText + |
| 24 |
(CRYPTPROTECTMEMORY_BLOCK_SIZE - (cbPlainText % CRYPTPROTECTMEMORY_BLOCK_SIZE)); |
| 25 |
printf("cbPlainText: %" PRIu32 " cbCipherText: %" PRIu32 "\n", cbPlainText, cbCipherText); |
| 26 |
pCipherText = (BYTE*)malloc(cbCipherText); |
| 27 |
if (!pCipherText) |
| 28 |
{ |
| 29 |
printf("Unable to allocate memory\n"); |
| 30 |
return -1; |
| 31 |
} |
| 32 |
CopyMemory(pCipherText, pPlainText, cbPlainText); |
| 33 |
ZeroMemory(&pCipherText[cbPlainText], (cbCipherText - cbPlainText)); |
| 34 |
if (!winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT)) |
| 35 |
goto fail; |
| 36 |
|
| 37 |
if (!CryptProtectMemory(pCipherText, cbCipherText, CRYPTPROTECTMEMORY_SAME_PROCESS)) |
| 38 |
{ |
| 39 |
printf("CryptProtectMemory failure\n"); |
| 40 |
goto fail; |
| 41 |
} |
| 42 |
|
| 43 |
printf("PlainText: %s (cbPlainText = %" PRIu32 ", cbCipherText = %" PRIu32 ")\n", pPlainText, |
| 44 |
cbPlainText, cbCipherText); |
| 45 |
winpr_HexDump("crypto.test", WLOG_DEBUG, pCipherText, cbCipherText); |
| 46 |
|
| 47 |
if (!CryptUnprotectMemory(pCipherText, cbCipherText, CRYPTPROTECTMEMORY_SAME_PROCESS)) |
| 48 |
{ |
| 49 |
printf("CryptUnprotectMemory failure\n"); |
| 50 |
goto fail; |
| 51 |
} |
| 52 |
|
| 53 |
printf("Decrypted CipherText: %s\n", pCipherText); |
| 54 |
SecureZeroMemory(pCipherText, cbCipherText); |
| 55 |
|
| 56 |
rc = 0; |
| 57 |
fail: |
| 58 |
free(pCipherText); |
| 59 |
return rc; |
| 60 |
} |