Milestone 5: deliver embedded RDP sessions and lifecycle hardening
This commit is contained in:
29
third_party/FreeRDP/winpr/libwinpr/crypto/test/CMakeLists.txt
vendored
Normal file
29
third_party/FreeRDP/winpr/libwinpr/crypto/test/CMakeLists.txt
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
set(MODULE_NAME "TestCrypto")
|
||||
set(MODULE_PREFIX "TEST_CRYPTO")
|
||||
|
||||
disable_warnings_for_directory(${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
set(${MODULE_PREFIX}_DRIVER ${MODULE_NAME}.c)
|
||||
|
||||
set(${MODULE_PREFIX}_TESTS TestCryptoHash.c TestCryptoRand.c TestCryptoCipher.c TestCryptoProtectData.c
|
||||
TestCryptoProtectMemory.c TestCryptoCertEnumCertificatesInStore.c
|
||||
)
|
||||
|
||||
create_test_sourcelist(${MODULE_PREFIX}_SRCS ${${MODULE_PREFIX}_DRIVER} ${${MODULE_PREFIX}_TESTS})
|
||||
|
||||
add_executable(${MODULE_NAME} ${${MODULE_PREFIX}_SRCS})
|
||||
|
||||
if(WIN32)
|
||||
set(${MODULE_PREFIX}_LIBS ${${MODULE_PREFIX}_LIBS} secur32 crypt32 cryptui)
|
||||
endif()
|
||||
|
||||
target_link_libraries(${MODULE_NAME} winpr)
|
||||
|
||||
set_target_properties(${MODULE_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${TESTING_OUTPUT_DIRECTORY}")
|
||||
|
||||
foreach(test ${${MODULE_PREFIX}_TESTS})
|
||||
get_filename_component(TestName ${test} NAME_WE)
|
||||
add_test(${TestName} ${TESTING_OUTPUT_DIRECTORY}/${MODULE_NAME} ${TestName})
|
||||
endforeach()
|
||||
|
||||
set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "WinPR/Test")
|
||||
88
third_party/FreeRDP/winpr/libwinpr/crypto/test/TestCryptoCertEnumCertificatesInStore.c
vendored
Normal file
88
third_party/FreeRDP/winpr/libwinpr/crypto/test/TestCryptoCertEnumCertificatesInStore.c
vendored
Normal file
@@ -0,0 +1,88 @@
|
||||
|
||||
|
||||
#include <winpr/crt.h>
|
||||
#include <winpr/tchar.h>
|
||||
#include <winpr/crypto.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
//#define WITH_CRYPTUI 1
|
||||
#endif
|
||||
|
||||
#ifdef WITH_CRYPTUI
|
||||
#include <cryptuiapi.h>
|
||||
#endif
|
||||
|
||||
int TestCryptoCertEnumCertificatesInStore(int argc, char* argv[])
|
||||
{
|
||||
int index = 0;
|
||||
DWORD status = 0;
|
||||
LPTSTR pszNameString = nullptr;
|
||||
HCERTSTORE hCertStore = nullptr;
|
||||
PCCERT_CONTEXT pCertContext = nullptr;
|
||||
|
||||
WINPR_UNUSED(argc);
|
||||
WINPR_UNUSED(argv);
|
||||
|
||||
/**
|
||||
* System Store Locations:
|
||||
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa388136/
|
||||
*/
|
||||
|
||||
/**
|
||||
* Requires elevated rights:
|
||||
* hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
|
||||
* CERT_SYSTEM_STORE_LOCAL_MACHINE, _T("Remote Desktop"));
|
||||
*/
|
||||
|
||||
hCertStore = CertOpenSystemStore(0, _T("MY"));
|
||||
// hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
|
||||
// CERT_SYSTEM_STORE_CURRENT_USER, _T("MY"));
|
||||
|
||||
if (!hCertStore)
|
||||
{
|
||||
printf("Failed to open system store\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
index = 0;
|
||||
|
||||
while ((pCertContext = CertEnumCertificatesInStore(hCertStore, pCertContext)))
|
||||
{
|
||||
status =
|
||||
CertGetNameString(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, nullptr, nullptr, 0);
|
||||
if (status == 0)
|
||||
return -1;
|
||||
|
||||
pszNameString = (LPTSTR)calloc(status, sizeof(TCHAR));
|
||||
if (!pszNameString)
|
||||
{
|
||||
printf("Unable to allocate memory\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
status = CertGetNameString(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, nullptr,
|
||||
pszNameString, status);
|
||||
if (status == 0)
|
||||
{
|
||||
free(pszNameString);
|
||||
return -1;
|
||||
}
|
||||
|
||||
_tprintf(_T("Certificate #%d: %s\n"), index++, pszNameString);
|
||||
|
||||
free(pszNameString);
|
||||
|
||||
#ifdef WITH_CRYPTUI
|
||||
CryptUIDlgViewContext(CERT_STORE_CERTIFICATE_CONTEXT, pCertContext, nullptr, nullptr, 0,
|
||||
nullptr);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (!CertCloseStore(hCertStore, 0))
|
||||
{
|
||||
printf("Failed to close system store\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
251
third_party/FreeRDP/winpr/libwinpr/crypto/test/TestCryptoCipher.c
vendored
Normal file
251
third_party/FreeRDP/winpr/libwinpr/crypto/test/TestCryptoCipher.c
vendored
Normal file
@@ -0,0 +1,251 @@
|
||||
|
||||
#include <winpr/crt.h>
|
||||
#include <winpr/print.h>
|
||||
#include <winpr/crypto.h>
|
||||
#include <winpr/ssl.h>
|
||||
|
||||
static BOOL test_crypto_cipher_aes_128_cbc(BOOL ex)
|
||||
{
|
||||
BOOL result = FALSE;
|
||||
BYTE key[16] = "0123456789abcdeF";
|
||||
BYTE iv[16] = "1234567887654321";
|
||||
BYTE ibuf[1024] = WINPR_C_ARRAY_INIT;
|
||||
BYTE obuf[1024] = WINPR_C_ARRAY_INIT;
|
||||
size_t ilen = 0;
|
||||
size_t olen = 0;
|
||||
size_t xlen = 0;
|
||||
const char plaintext[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
|
||||
"eiusmod tempor incididunt ut labore et dolore magna aliqua.";
|
||||
|
||||
/* encrypt */
|
||||
|
||||
WINPR_CIPHER_CTX* ctx = nullptr;
|
||||
if (ex)
|
||||
ctx = winpr_Cipher_NewEx(WINPR_CIPHER_AES_128_CBC, WINPR_ENCRYPT, key, sizeof(key), iv,
|
||||
sizeof(iv));
|
||||
#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
|
||||
else
|
||||
ctx = winpr_Cipher_New(WINPR_CIPHER_AES_128_CBC, WINPR_ENCRYPT, key, iv);
|
||||
#endif
|
||||
if (!ctx)
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_Cipher_New (encrypt) failed\n", __func__);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ilen = strnlen(plaintext, sizeof(plaintext)) + 1;
|
||||
memcpy(ibuf, plaintext, ilen);
|
||||
|
||||
ilen = ((ilen + 15) / 16) * 16;
|
||||
olen = 0;
|
||||
xlen = 0;
|
||||
|
||||
if (!winpr_Cipher_Update(ctx, ibuf, ilen, obuf, &olen))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_Cipher_New (encrypt) failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
xlen += olen;
|
||||
|
||||
if (!winpr_Cipher_Final(ctx, obuf + xlen, &olen))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_Cipher_Final (encrypt) failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
xlen += olen;
|
||||
|
||||
if (xlen != ilen)
|
||||
{
|
||||
(void)fprintf(stderr, "%s: error, xlen (%" PRIuz ") != ilen (%" PRIuz ") (encrypt)\n",
|
||||
__func__, xlen, ilen);
|
||||
goto out;
|
||||
}
|
||||
|
||||
winpr_Cipher_Free(ctx);
|
||||
ctx = nullptr;
|
||||
|
||||
/* decrypt */
|
||||
|
||||
if (ex)
|
||||
ctx = winpr_Cipher_NewEx(WINPR_CIPHER_AES_128_CBC, WINPR_DECRYPT, key, sizeof(key), iv,
|
||||
sizeof(iv));
|
||||
#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
|
||||
else
|
||||
ctx = winpr_Cipher_New(WINPR_CIPHER_AES_128_CBC, WINPR_DECRYPT, key, iv);
|
||||
|
||||
#endif
|
||||
if (!ctx)
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_Cipher_New (decrypt) failed\n", __func__);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
memset(ibuf, 0, sizeof(ibuf));
|
||||
memcpy(ibuf, obuf, xlen);
|
||||
memset(obuf, 0, sizeof(obuf));
|
||||
|
||||
ilen = xlen;
|
||||
olen = 0;
|
||||
xlen = 0;
|
||||
|
||||
if (!winpr_Cipher_Update(ctx, ibuf, ilen, obuf, &olen))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_Cipher_New (decrypt) failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
xlen += olen;
|
||||
|
||||
if (!winpr_Cipher_Final(ctx, obuf + xlen, &olen))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_Cipher_Final (decrypt) failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
xlen += olen;
|
||||
|
||||
if (xlen != ilen)
|
||||
{
|
||||
(void)fprintf(stderr, "%s: error, xlen (%" PRIuz ") != ilen (%" PRIuz ") (decrypt)\n",
|
||||
__func__, xlen, ilen);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (strcmp((const char*)obuf, plaintext) != 0)
|
||||
{
|
||||
(void)fprintf(stderr, "%s: error, decrypted data does not match plaintext\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
|
||||
result = TRUE;
|
||||
|
||||
out:
|
||||
winpr_Cipher_Free(ctx);
|
||||
return result;
|
||||
}
|
||||
|
||||
static const char TEST_RC4_KEY[] = "Key";
|
||||
static const char TEST_RC4_PLAINTEXT[] = "Plaintext";
|
||||
static const char TEST_RC4_CIPHERTEXT[] = "\xBB\xF3\x16\xE8\xD9\x40\xAF\x0A\xD3";
|
||||
|
||||
static BOOL test_crypto_cipher_rc4(void)
|
||||
{
|
||||
BOOL rc = FALSE;
|
||||
WINPR_RC4_CTX* ctx = nullptr;
|
||||
|
||||
const size_t len = strnlen(TEST_RC4_PLAINTEXT, sizeof(TEST_RC4_PLAINTEXT));
|
||||
BYTE* text = (BYTE*)calloc(1, len);
|
||||
if (!text)
|
||||
{
|
||||
(void)fprintf(stderr, "%s: failed to allocate text buffer (len=%" PRIuz ")\n", __func__,
|
||||
len);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if ((ctx = winpr_RC4_New(TEST_RC4_KEY, strnlen(TEST_RC4_KEY, sizeof(TEST_RC4_KEY)))) == nullptr)
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_RC4_New failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
rc = winpr_RC4_Update(ctx, len, (const BYTE*)TEST_RC4_PLAINTEXT, text);
|
||||
winpr_RC4_Free(ctx);
|
||||
if (!rc)
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_RC4_Update failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (memcmp(text, TEST_RC4_CIPHERTEXT, len) != 0)
|
||||
{
|
||||
char* actual = nullptr;
|
||||
char* expected = nullptr;
|
||||
|
||||
actual = winpr_BinToHexString(text, len, FALSE);
|
||||
expected = winpr_BinToHexString(TEST_RC4_CIPHERTEXT, len, FALSE);
|
||||
|
||||
(void)fprintf(stderr, "%s: unexpected RC4 ciphertext: Actual: %s Expected: %s\n", __func__,
|
||||
actual, expected);
|
||||
|
||||
free(actual);
|
||||
free(expected);
|
||||
goto out;
|
||||
}
|
||||
|
||||
rc = TRUE;
|
||||
|
||||
out:
|
||||
free(text);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static const BYTE* TEST_RAND_DATA =
|
||||
(BYTE*)"\x1F\xC2\xEE\x4C\xA3\x66\x80\xA2\xCE\xFE\x56\xB4\x9E\x08\x30\x96"
|
||||
"\x33\x6A\xA9\x6D\x36\xFD\x3C\xB7\x83\x04\x4E\x5E\xDC\x22\xCD\xF3"
|
||||
"\x48\xDF\x3A\x2A\x61\xF1\xA8\xFA\x1F\xC6\xC7\x1B\x81\xB4\xE1\x0E"
|
||||
"\xCB\xA2\xEF\xA1\x12\x4A\x83\xE5\x1D\x72\x1D\x2D\x26\xA8\x6B\xC0";
|
||||
|
||||
static const BYTE* TEST_CIPHER_KEY =
|
||||
(BYTE*)"\x9D\x7C\xC0\xA1\x94\x3B\x07\x67\x2F\xD3\x83\x10\x51\x83\x38\x0E"
|
||||
"\x1C\x74\x8C\x4E\x15\x79\xD6\xFF\xE2\xF0\x37\x7F\x8C\xD7\xD2\x13";
|
||||
|
||||
static const BYTE* TEST_CIPHER_IV =
|
||||
(BYTE*)"\xFE\xE3\x9F\xF0\xD1\x5E\x37\x0C\xAB\xAB\x9B\x04\xF3\xDB\x99\x15";
|
||||
|
||||
static BOOL test_crypto_cipher_key(void)
|
||||
{
|
||||
int status = 0;
|
||||
BYTE key[32] = WINPR_C_ARRAY_INIT;
|
||||
BYTE iv[16] = WINPR_C_ARRAY_INIT;
|
||||
BYTE salt[8] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
|
||||
|
||||
status = winpr_Cipher_BytesToKey(WINPR_CIPHER_AES_256_CBC, WINPR_MD_SHA1, salt, TEST_RAND_DATA,
|
||||
64, 4, key, iv);
|
||||
|
||||
if (status != 32 || memcmp(key, TEST_CIPHER_KEY, 32) != 0 ||
|
||||
memcmp(iv, TEST_CIPHER_IV, 16) != 0)
|
||||
{
|
||||
char* akstr = nullptr;
|
||||
char* ekstr = nullptr;
|
||||
char* aivstr = nullptr;
|
||||
char* eivstr = nullptr;
|
||||
|
||||
akstr = winpr_BinToHexString(key, 32, 0);
|
||||
ekstr = winpr_BinToHexString(TEST_CIPHER_KEY, 32, 0);
|
||||
|
||||
aivstr = winpr_BinToHexString(iv, 16, 0);
|
||||
eivstr = winpr_BinToHexString(TEST_CIPHER_IV, 16, 0);
|
||||
|
||||
(void)fprintf(stderr, "Unexpected EVP_BytesToKey Key: Actual: %s, Expected: %s\n", akstr,
|
||||
ekstr);
|
||||
(void)fprintf(stderr, "Unexpected EVP_BytesToKey IV : Actual: %s, Expected: %s\n", aivstr,
|
||||
eivstr);
|
||||
|
||||
free(akstr);
|
||||
free(ekstr);
|
||||
free(aivstr);
|
||||
free(eivstr);
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int TestCryptoCipher(int argc, char* argv[])
|
||||
{
|
||||
|
||||
WINPR_UNUSED(argc);
|
||||
WINPR_UNUSED(argv);
|
||||
|
||||
winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT);
|
||||
|
||||
if (!test_crypto_cipher_aes_128_cbc(TRUE))
|
||||
return -1;
|
||||
|
||||
if (!test_crypto_cipher_aes_128_cbc(FALSE))
|
||||
return -1;
|
||||
|
||||
if (!test_crypto_cipher_rc4())
|
||||
return -1;
|
||||
|
||||
if (!test_crypto_cipher_key())
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
318
third_party/FreeRDP/winpr/libwinpr/crypto/test/TestCryptoHash.c
vendored
Normal file
318
third_party/FreeRDP/winpr/libwinpr/crypto/test/TestCryptoHash.c
vendored
Normal file
@@ -0,0 +1,318 @@
|
||||
|
||||
#include <winpr/crt.h>
|
||||
#include <winpr/print.h>
|
||||
#include <winpr/crypto.h>
|
||||
#include <winpr/ssl.h>
|
||||
|
||||
static const char TEST_MD5_DATA[] = "test";
|
||||
static const BYTE TEST_MD5_HASH[] =
|
||||
"\x09\x8f\x6b\xcd\x46\x21\xd3\x73\xca\xde\x4e\x83\x26\x27\xb4\xf6";
|
||||
|
||||
static BOOL test_crypto_hash_md5(void)
|
||||
{
|
||||
BOOL result = FALSE;
|
||||
BYTE hash[WINPR_MD5_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
|
||||
WINPR_DIGEST_CTX* ctx = nullptr;
|
||||
|
||||
if (!(ctx = winpr_Digest_New()))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_Digest_New failed\n", __func__);
|
||||
return FALSE;
|
||||
}
|
||||
if (!winpr_Digest_Init(ctx, WINPR_MD_MD5))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_Digest_Init failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
if (!winpr_Digest_Update(ctx, (const BYTE*)TEST_MD5_DATA,
|
||||
strnlen(TEST_MD5_DATA, sizeof(TEST_MD5_DATA))))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_Digest_Update failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
if (!winpr_Digest_Final(ctx, hash, sizeof(hash)))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_Digest_Final failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
if (memcmp(hash, TEST_MD5_HASH, WINPR_MD5_DIGEST_LENGTH) != 0)
|
||||
{
|
||||
char* actual = nullptr;
|
||||
char* expected = nullptr;
|
||||
|
||||
actual = winpr_BinToHexString(hash, WINPR_MD5_DIGEST_LENGTH, FALSE);
|
||||
expected = winpr_BinToHexString(TEST_MD5_HASH, WINPR_MD5_DIGEST_LENGTH, FALSE);
|
||||
|
||||
(void)fprintf(stderr, "unexpected MD5 hash: Actual: %s Expected: %s\n", actual, expected);
|
||||
|
||||
free(actual);
|
||||
free(expected);
|
||||
|
||||
goto out;
|
||||
}
|
||||
|
||||
result = TRUE;
|
||||
out:
|
||||
winpr_Digest_Free(ctx);
|
||||
return result;
|
||||
}
|
||||
|
||||
static const char TEST_MD4_DATA[] = "test";
|
||||
static const BYTE TEST_MD4_HASH[] =
|
||||
"\xdb\x34\x6d\x69\x1d\x7a\xcc\x4d\xc2\x62\x5d\xb1\x9f\x9e\x3f\x52";
|
||||
|
||||
static BOOL test_crypto_hash_md4(void)
|
||||
{
|
||||
BOOL result = FALSE;
|
||||
BYTE hash[WINPR_MD4_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
|
||||
WINPR_DIGEST_CTX* ctx = nullptr;
|
||||
|
||||
if (!(ctx = winpr_Digest_New()))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_Digest_New failed\n", __func__);
|
||||
return FALSE;
|
||||
}
|
||||
if (!winpr_Digest_Init(ctx, WINPR_MD_MD4))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_Digest_Init failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
if (!winpr_Digest_Update(ctx, (const BYTE*)TEST_MD4_DATA,
|
||||
strnlen(TEST_MD4_DATA, sizeof(TEST_MD4_DATA))))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_Digest_Update failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
if (!winpr_Digest_Final(ctx, hash, sizeof(hash)))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_Digest_Final failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
if (memcmp(hash, TEST_MD4_HASH, WINPR_MD4_DIGEST_LENGTH) != 0)
|
||||
{
|
||||
char* actual = nullptr;
|
||||
char* expected = nullptr;
|
||||
|
||||
actual = winpr_BinToHexString(hash, WINPR_MD4_DIGEST_LENGTH, FALSE);
|
||||
expected = winpr_BinToHexString(TEST_MD4_HASH, WINPR_MD4_DIGEST_LENGTH, FALSE);
|
||||
|
||||
(void)fprintf(stderr, "unexpected MD4 hash: Actual: %s Expected: %s\n", actual, expected);
|
||||
|
||||
free(actual);
|
||||
free(expected);
|
||||
|
||||
goto out;
|
||||
}
|
||||
|
||||
result = TRUE;
|
||||
out:
|
||||
winpr_Digest_Free(ctx);
|
||||
return result;
|
||||
}
|
||||
|
||||
static const char TEST_SHA1_DATA[] = "test";
|
||||
static const BYTE TEST_SHA1_HASH[] =
|
||||
"\xa9\x4a\x8f\xe5\xcc\xb1\x9b\xa6\x1c\x4c\x08\x73\xd3\x91\xe9\x87\x98\x2f\xbb\xd3";
|
||||
|
||||
static BOOL test_crypto_hash_sha1(void)
|
||||
{
|
||||
BOOL result = FALSE;
|
||||
BYTE hash[WINPR_SHA1_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
|
||||
WINPR_DIGEST_CTX* ctx = nullptr;
|
||||
|
||||
if (!(ctx = winpr_Digest_New()))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_Digest_New failed\n", __func__);
|
||||
return FALSE;
|
||||
}
|
||||
if (!winpr_Digest_Init(ctx, WINPR_MD_SHA1))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_Digest_Init failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
if (!winpr_Digest_Update(ctx, (const BYTE*)TEST_SHA1_DATA,
|
||||
strnlen(TEST_SHA1_DATA, sizeof(TEST_SHA1_DATA))))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_Digest_Update failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
if (!winpr_Digest_Final(ctx, hash, sizeof(hash)))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_Digest_Final failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (memcmp(hash, TEST_SHA1_HASH, WINPR_MD5_DIGEST_LENGTH) != 0)
|
||||
{
|
||||
char* actual = nullptr;
|
||||
char* expected = nullptr;
|
||||
|
||||
actual = winpr_BinToHexString(hash, WINPR_SHA1_DIGEST_LENGTH, FALSE);
|
||||
expected = winpr_BinToHexString(TEST_SHA1_HASH, WINPR_SHA1_DIGEST_LENGTH, FALSE);
|
||||
|
||||
(void)fprintf(stderr, "unexpected SHA1 hash: Actual: %s Expected: %s\n", actual, expected);
|
||||
|
||||
free(actual);
|
||||
free(expected);
|
||||
|
||||
goto out;
|
||||
}
|
||||
|
||||
result = TRUE;
|
||||
out:
|
||||
winpr_Digest_Free(ctx);
|
||||
return result;
|
||||
}
|
||||
|
||||
static const char TEST_HMAC_MD5_DATA[] = "Hi There";
|
||||
static const BYTE TEST_HMAC_MD5_KEY[] =
|
||||
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
|
||||
static const BYTE TEST_HMAC_MD5_HASH[] =
|
||||
"\xb5\x79\x91\xa2\x20\x3d\x49\x2d\x73\xfb\x71\x43\xdf\xc5\x08\x28";
|
||||
|
||||
static BOOL test_crypto_hash_hmac_md5(void)
|
||||
{
|
||||
BYTE hash[WINPR_MD5_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
|
||||
WINPR_HMAC_CTX* ctx = nullptr;
|
||||
BOOL result = FALSE;
|
||||
|
||||
if (!(ctx = winpr_HMAC_New()))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_HMAC_New failed\n", __func__);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!winpr_HMAC_Init(ctx, WINPR_MD_MD5, TEST_HMAC_MD5_KEY, WINPR_MD5_DIGEST_LENGTH))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_HMAC_Init failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
if (!winpr_HMAC_Update(ctx, (const BYTE*)TEST_HMAC_MD5_DATA,
|
||||
strnlen(TEST_HMAC_MD5_DATA, sizeof(TEST_HMAC_MD5_DATA))))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_HMAC_Update failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
if (!winpr_HMAC_Update(ctx, (const BYTE*)TEST_HMAC_MD5_DATA,
|
||||
strnlen(TEST_HMAC_MD5_DATA, sizeof(TEST_HMAC_MD5_DATA))))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_HMAC_Update failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
if (!winpr_HMAC_Final(ctx, hash, sizeof(hash)))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_HMAC_Final failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (memcmp(hash, TEST_HMAC_MD5_HASH, WINPR_MD5_DIGEST_LENGTH) != 0)
|
||||
{
|
||||
char* actual = nullptr;
|
||||
char* expected = nullptr;
|
||||
|
||||
actual = winpr_BinToHexString(hash, WINPR_MD5_DIGEST_LENGTH, FALSE);
|
||||
expected = winpr_BinToHexString(TEST_HMAC_MD5_HASH, WINPR_MD5_DIGEST_LENGTH, FALSE);
|
||||
|
||||
(void)fprintf(stderr, "unexpected HMAC-MD5 hash: Actual: %s Expected: %s\n", actual,
|
||||
expected);
|
||||
|
||||
free(actual);
|
||||
free(expected);
|
||||
|
||||
goto out;
|
||||
}
|
||||
|
||||
result = TRUE;
|
||||
out:
|
||||
winpr_HMAC_Free(ctx);
|
||||
return result;
|
||||
}
|
||||
|
||||
static const char TEST_HMAC_SHA1_DATA[] = "Hi There";
|
||||
static const BYTE TEST_HMAC_SHA1_KEY[] =
|
||||
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
|
||||
static const BYTE TEST_HMAC_SHA1_HASH[] =
|
||||
"\xab\x23\x08\x2d\xca\x0c\x75\xea\xca\x60\x09\xc0\xb8\x8c\x2d\xf4\xf4\xbf\x88\xee";
|
||||
|
||||
static BOOL test_crypto_hash_hmac_sha1(void)
|
||||
{
|
||||
BYTE hash[WINPR_SHA1_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
|
||||
WINPR_HMAC_CTX* ctx = nullptr;
|
||||
BOOL result = FALSE;
|
||||
|
||||
if (!(ctx = winpr_HMAC_New()))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_HMAC_New failed\n", __func__);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!winpr_HMAC_Init(ctx, WINPR_MD_SHA1, TEST_HMAC_SHA1_KEY, WINPR_SHA1_DIGEST_LENGTH))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_HMAC_Init failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
if (!winpr_HMAC_Update(ctx, (const BYTE*)TEST_HMAC_SHA1_DATA,
|
||||
strnlen(TEST_HMAC_SHA1_DATA, sizeof(TEST_HMAC_SHA1_DATA))))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_HMAC_Update failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
if (!winpr_HMAC_Update(ctx, (const BYTE*)TEST_HMAC_SHA1_DATA,
|
||||
strnlen(TEST_HMAC_SHA1_DATA, sizeof(TEST_HMAC_SHA1_DATA))))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_HMAC_Update failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
if (!winpr_HMAC_Final(ctx, hash, sizeof(hash)))
|
||||
{
|
||||
(void)fprintf(stderr, "%s: winpr_HMAC_Final failed\n", __func__);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (memcmp(hash, TEST_HMAC_SHA1_HASH, WINPR_SHA1_DIGEST_LENGTH) != 0)
|
||||
{
|
||||
char* actual = nullptr;
|
||||
char* expected = nullptr;
|
||||
|
||||
actual = winpr_BinToHexString(hash, WINPR_SHA1_DIGEST_LENGTH, FALSE);
|
||||
expected = winpr_BinToHexString(TEST_HMAC_SHA1_HASH, WINPR_SHA1_DIGEST_LENGTH, FALSE);
|
||||
|
||||
(void)fprintf(stderr, "unexpected HMAC-SHA1 hash: Actual: %s Expected: %s\n", actual,
|
||||
expected);
|
||||
|
||||
free(actual);
|
||||
free(expected);
|
||||
|
||||
goto out;
|
||||
}
|
||||
|
||||
result = TRUE;
|
||||
out:
|
||||
winpr_HMAC_Free(ctx);
|
||||
return result;
|
||||
}
|
||||
|
||||
int TestCryptoHash(int argc, char* argv[])
|
||||
{
|
||||
WINPR_UNUSED(argc);
|
||||
WINPR_UNUSED(argv);
|
||||
|
||||
winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT);
|
||||
|
||||
if (!test_crypto_hash_md5())
|
||||
return -1;
|
||||
|
||||
if (!test_crypto_hash_md4())
|
||||
return -1;
|
||||
|
||||
if (!test_crypto_hash_sha1())
|
||||
return -1;
|
||||
|
||||
if (!test_crypto_hash_hmac_md5())
|
||||
return -1;
|
||||
|
||||
if (!test_crypto_hash_hmac_sha1())
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
10
third_party/FreeRDP/winpr/libwinpr/crypto/test/TestCryptoProtectData.c
vendored
Normal file
10
third_party/FreeRDP/winpr/libwinpr/crypto/test/TestCryptoProtectData.c
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
|
||||
#include <winpr/crt.h>
|
||||
#include <winpr/tchar.h>
|
||||
#include <winpr/crypto.h>
|
||||
|
||||
int TestCryptoProtectData(int argc, char* argv[])
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
55
third_party/FreeRDP/winpr/libwinpr/crypto/test/TestCryptoProtectMemory.c
vendored
Normal file
55
third_party/FreeRDP/winpr/libwinpr/crypto/test/TestCryptoProtectMemory.c
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
|
||||
#include <winpr/crt.h>
|
||||
#include <winpr/print.h>
|
||||
#include <winpr/crypto.h>
|
||||
#include <winpr/ssl.h>
|
||||
#include <winpr/wlog.h>
|
||||
|
||||
static const char* SECRET_PASSWORD_TEST = "MySecretPassword123!";
|
||||
|
||||
int TestCryptoProtectMemory(int argc, char* argv[])
|
||||
{
|
||||
UINT32 cbPlainText = 0;
|
||||
UINT32 cbCipherText = 0;
|
||||
const char* pPlainText = nullptr;
|
||||
BYTE* pCipherText = nullptr;
|
||||
|
||||
WINPR_UNUSED(argc);
|
||||
WINPR_UNUSED(argv);
|
||||
|
||||
pPlainText = SECRET_PASSWORD_TEST;
|
||||
cbPlainText = strlen(pPlainText) + 1;
|
||||
cbCipherText = cbPlainText +
|
||||
(CRYPTPROTECTMEMORY_BLOCK_SIZE - (cbPlainText % CRYPTPROTECTMEMORY_BLOCK_SIZE));
|
||||
printf("cbPlainText: %" PRIu32 " cbCipherText: %" PRIu32 "\n", cbPlainText, cbCipherText);
|
||||
pCipherText = (BYTE*)malloc(cbCipherText);
|
||||
if (!pCipherText)
|
||||
{
|
||||
printf("Unable to allocate memory\n");
|
||||
return -1;
|
||||
}
|
||||
CopyMemory(pCipherText, pPlainText, cbPlainText);
|
||||
ZeroMemory(&pCipherText[cbPlainText], (cbCipherText - cbPlainText));
|
||||
winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT);
|
||||
|
||||
if (!CryptProtectMemory(pCipherText, cbCipherText, CRYPTPROTECTMEMORY_SAME_PROCESS))
|
||||
{
|
||||
printf("CryptProtectMemory failure\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("PlainText: %s (cbPlainText = %" PRIu32 ", cbCipherText = %" PRIu32 ")\n", pPlainText,
|
||||
cbPlainText, cbCipherText);
|
||||
winpr_HexDump("crypto.test", WLOG_DEBUG, pCipherText, cbCipherText);
|
||||
|
||||
if (!CryptUnprotectMemory(pCipherText, cbCipherText, CRYPTPROTECTMEMORY_SAME_PROCESS))
|
||||
{
|
||||
printf("CryptUnprotectMemory failure\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("Decrypted CipherText: %s\n", pCipherText);
|
||||
SecureZeroMemory(pCipherText, cbCipherText);
|
||||
free(pCipherText);
|
||||
return 0;
|
||||
}
|
||||
27
third_party/FreeRDP/winpr/libwinpr/crypto/test/TestCryptoRand.c
vendored
Normal file
27
third_party/FreeRDP/winpr/libwinpr/crypto/test/TestCryptoRand.c
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
#include <winpr/crt.h>
|
||||
#include <winpr/print.h>
|
||||
#include <winpr/crypto.h>
|
||||
|
||||
int TestCryptoRand(int argc, char* argv[])
|
||||
{
|
||||
char* str = nullptr;
|
||||
BYTE rnd[16] = WINPR_C_ARRAY_INIT;
|
||||
|
||||
WINPR_UNUSED(argc);
|
||||
WINPR_UNUSED(argv);
|
||||
|
||||
if (winpr_RAND(rnd, sizeof(rnd)) < 0)
|
||||
return -1;
|
||||
|
||||
str = winpr_BinToHexString(rnd, sizeof(rnd), FALSE);
|
||||
// (void)fprintf(stderr, "Rand: %s\n", str);
|
||||
free(str);
|
||||
|
||||
if (memcmp(rnd, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16) == 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user