diff options
author | weidai <weidai11@users.noreply.github.com> | 2006-09-06 05:10:49 +0000 |
---|---|---|
committer | weidai <weidai11@users.noreply.github.com> | 2006-09-06 05:10:49 +0000 |
commit | b3826418aa13e9788f46d39ef2084e9498f5cfa2 (patch) | |
tree | 80434a7461db02e230d2d3c8cca5983890de9dd0 /fipstest.cpp | |
parent | f23729d664010ba9f7f4f90889755bf15d0c4203 (diff) | |
download | cryptopp-git-b3826418aa13e9788f46d39ef2084e9498f5cfa2.tar.gz |
fix self-test when installed into unicode paths
Diffstat (limited to 'fipstest.cpp')
-rw-r--r-- | fipstest.cpp | 67 |
1 files changed, 53 insertions, 14 deletions
diff --git a/fipstest.cpp b/fipstest.cpp index 78d3867d..58f561a2 100644 --- a/fipstest.cpp +++ b/fipstest.cpp @@ -27,6 +27,14 @@ extern PowerUpSelfTestStatus g_powerUpSelfTestStatus; SecByteBlock g_actualMac; unsigned long g_macFileLocation = 0; +// use a random dummy string here, to be searched/replaced later with the real MAC +static const byte s_moduleMac[CryptoPP::HMAC<CryptoPP::SHA1>::DIGESTSIZE] = CRYPTOPP_DUMMY_DLL_MAC; +CRYPTOPP_COMPILE_ASSERT(sizeof(s_moduleMac) == CryptoPP::SHA1::DIGESTSIZE); + +#ifdef CRYPTOPP_WIN32_AVAILABLE +static HMODULE s_hModule = NULL; +#endif + const byte * CRYPTOPP_API GetActualMacAndLocation(unsigned int &macSize, unsigned int &fileLocation) { macSize = (unsigned int)g_actualMac.size(); @@ -261,11 +269,48 @@ bool IntegrityCheckModule(const char *moduleFilename, const byte *expectedModule MeterFilter verifier(new HashFilter(*mac, new ArraySink(actualMac, actualMac.size()))); // MeterFilter verifier(new FileSink("c:\\dt.tmp")); - FileStore file(moduleFilename); + std::ifstream moduleStream; + +#ifdef CRYPTOPP_WIN32_AVAILABLE + HMODULE h; + { + char moduleFilenameBuf[MAX_PATH] = ""; + if (moduleFilename == NULL) + { + wchar_t wideModuleFilename[MAX_PATH]; + if (GetModuleFileNameW(s_hModule, wideModuleFilename, MAX_PATH) > 0) + { + moduleStream.open(wideModuleFilename, std::ios::in | std::ios::binary); + h = GetModuleHandleW(wideModuleFilename); + } + else + { + GetModuleFileNameA(s_hModule, moduleFilenameBuf, MAX_PATH); + moduleFilename = moduleFilenameBuf; + } + } +#endif + if (moduleFilename != NULL) + { + moduleStream.open(moduleFilename, std::ios::in | std::ios::binary); +#ifdef CRYPTOPP_WIN32_AVAILABLE + h = GetModuleHandleA(moduleFilename); + moduleFilename = NULL; + } +#endif + } + + if (!moduleStream) + { +#ifdef CRYPTOPP_WIN32_AVAILABLE + OutputDebugString("Crypto++ DLL integrity check failed. Cannot open file for reading."); +#endif + return false; + } + FileStore file(moduleStream); #ifdef CRYPTOPP_WIN32_AVAILABLE // try to hash from memory first - HMODULE h = GetModuleHandle(moduleFilename); const byte *memBase = (const byte *)h; const IMAGE_DOS_HEADER *ph = (IMAGE_DOS_HEADER *)memBase; const IMAGE_NT_HEADERS *phnt = (IMAGE_NT_HEADERS *)(memBase + ph->e_lfanew); @@ -353,14 +398,15 @@ bool IntegrityCheckModule(const char *moduleFilename, const byte *expectedModule if (memcmp(expectedModuleMac, actualMac, macSize) != 0) { OutputDebugString("In memory integrity check failed. This may be caused by debug breakpoints or DLL relocation.\n"); - file.Initialize(MakeParameters(Name::InputFileName(), moduleFilename)); + moduleStream.clear(); + moduleStream.seekg(0); verifier.Initialize(MakeParameters(Name::OutputBuffer(), ByteArrayParameter(actualMac, (unsigned int)actualMac.size()))); // verifier.Initialize(MakeParameters(Name::OutputFileName(), (const char *)"c:\\dt2.tmp")); verifier.AddRangeToSkip(0, checksumPos, checksumSize); verifier.AddRangeToSkip(0, certificateTableDirectoryPos, certificateTableDirectorySize); verifier.AddRangeToSkip(0, certificateTablePos, certificateTableSize); verifier.AddRangeToSkip(0, macFileLocation, macSize); - file.TransferAllTo(verifier); + FileStore(moduleStream).TransferAllTo(verifier); } #endif @@ -370,7 +416,7 @@ bool IntegrityCheckModule(const char *moduleFilename, const byte *expectedModule #ifdef CRYPTOPP_WIN32_AVAILABLE std::string hexMac; HexEncoder(new StringSink(hexMac)).PutMessageEnd(actualMac, actualMac.size()); - OutputDebugString((moduleFilename + (" integrity check failed. Actual MAC is: " + hexMac) + "\n").c_str()); + OutputDebugString((("Crypto++ DLL integrity check failed. Actual MAC is: " + hexMac) + "\n").c_str()); #endif return false; } @@ -382,7 +428,7 @@ void DoPowerUpSelfTest(const char *moduleFilename, const byte *expectedModuleMac try { - if (FIPS_140_2_ComplianceEnabled() || moduleFilename != NULL) + if (FIPS_140_2_ComplianceEnabled() || expectedModuleMac != NULL) { if (!IntegrityCheckModule(moduleFilename, expectedModuleMac, &g_actualMac, &g_macFileLocation)) throw 0; // throw here so we break in the debugger, this will be caught right away @@ -528,16 +574,9 @@ done: #ifdef CRYPTOPP_WIN32_AVAILABLE -// use a random dummy string here, to be searched/replaced later with the real MAC -static const byte s_moduleMac[CryptoPP::HMAC<CryptoPP::SHA1>::DIGESTSIZE] = CRYPTOPP_DUMMY_DLL_MAC; -CRYPTOPP_COMPILE_ASSERT(sizeof(s_moduleMac) == CryptoPP::SHA1::DIGESTSIZE); -static HMODULE s_hModule = NULL; - void DoDllPowerUpSelfTest() { - char moduleFileName[MAX_PATH]; - GetModuleFileNameA(s_hModule, moduleFileName, sizeof(moduleFileName)); - CryptoPP::DoPowerUpSelfTest(moduleFileName, s_moduleMac); + CryptoPP::DoPowerUpSelfTest(NULL, s_moduleMac); } #else |