2626#include < sensapi.h>
2727#include < iomanip>
2828#include < sstream>
29+ #include < shlwapi.h>
30+ #include < shlobj_core.h>
2931#include " verifySignedfile.h"
30- # include " Common.h "
32+
3133
3234using namespace std ;
3335
3436
3537// Debug use
3638bool doLogCertifError = false ;
3739
40+ void SecurityGuard::writeSecurityError (const std::wstring& prefix, const std::wstring& log2write) const
41+ {
42+ // Expand the environment variable
43+ wstring expandedLogFileName = _errLogPath;
44+ expandEnv (expandedLogFileName);
45+
46+ // Create the folder & sub-folders for the log file
47+ wchar_t logDir[MAX_PATH];
48+ lstrcpy (logDir, expandedLogFileName.c_str ());
49+ ::PathRemoveFileSpec (logDir);
50+ int result = SHCreateDirectoryEx (NULL , logDir, NULL );
51+
52+ // If folder doesn't exit or folder creation failed
53+ if (result != ERROR_SUCCESS && result != ERROR_ALREADY_EXISTS)
54+ {
55+ // process %TEMP% treatment
56+ wchar_t * fileName = ::PathFindFileName (expandedLogFileName.c_str ());
57+ expandedLogFileName = L" %TEMP%\\ " ;
58+ expandedLogFileName += fileName;
59+ expandEnv (expandedLogFileName);
60+ }
61+
62+ writeLog (expandedLogFileName.c_str (), prefix.c_str (), log2write.c_str ());
63+ }
64+
3865bool SecurityGuard::verifySignedBinary (const std::wstring& filepath)
3966{
4067 wstring display_name;
4168 wstring key_id_hex;
4269 wstring subject;
4370 wstring authority_key_id_hex;
4471
45- if (doLogCertifError)
46- {
47- writeLog (L" c:\\ tmp\\ certifError.log" , L" verifySignedBinary: " , filepath.c_str ());
48- }
49-
5072 //
5173 // Signature verification
5274 //
@@ -69,9 +91,6 @@ bool SecurityGuard::verifySignedBinary(const std::wstring& filepath)
6991 if (!_doCheckRevocation)
7092 {
7193 winTEXTrust_data.fdwRevocationChecks = WTD_REVOKE_NONE;
72-
73- if (doLogCertifError)
74- writeLog (L" c:\\ tmp\\ certifError.log" , L" verifySignedBinary: " , L" certificate revocation checking is disabled" );
7594 }
7695 else
7796 {
@@ -87,9 +106,6 @@ bool SecurityGuard::verifySignedBinary(const std::wstring& filepath)
87106 if (!online)
88107 {
89108 winTEXTrust_data.fdwRevocationChecks = WTD_REVOKE_NONE;
90-
91- if (doLogCertifError)
92- writeLog (L" c:\\ tmp\\ certifError.log" , L" verifySignedBinary: " , L" system is offline - certificate revocation won't be checked" );
93109 }
94110 }
95111
@@ -105,17 +121,13 @@ bool SecurityGuard::verifySignedBinary(const std::wstring& filepath)
105121
106122 if (vtrust)
107123 {
108- if (doLogCertifError)
109- writeLog (L" c:\\ tmp\\ certifError.log" , L" verifySignedBinary: " , L" trust verification failed" );
110-
124+ writeSecurityError (filepath.c_str (), L" : chain of trust verification failed" );
111125 return false ;
112126 }
113127
114128 if (t2)
115129 {
116- if (doLogCertifError)
117- writeLog (L" c:\\ tmp\\ certifError.log" , L" verifySignedBinary: " , L" error encountered while cleaning up after WinVerifyTrust" );
118-
130+ writeSecurityError (filepath.c_str (), L" : error encountered while cleaning up after WinVerifyTrust" );
119131 return false ;
120132 }
121133 }
@@ -206,9 +218,6 @@ bool SecurityGuard::verifySignedBinary(const std::wstring& filepath)
206218 }
207219 key_id_hex = ss.str ();
208220
209- if (doLogCertifError)
210- writeLog (L" c:\\ tmp\\ certifError.log" , L" verifySignedBinary: " , key_id_hex.c_str ());
211-
212221 // Getting the display name
213222 auto sze = ::CertGetNameString (context, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0 , NULL , NULL , 0 );
214223 if (sze <= 1 )
@@ -265,63 +274,44 @@ bool SecurityGuard::verifySignedBinary(const std::wstring& filepath)
265274 LocalFree (pAuthKeyIdInfo);
266275 }
267276 }
268- else
269- {
270- // Authority Key Identifier extension not found
271- if (doLogCertifError)
272- writeLog (L" c:\\ tmp\\ certifError.log" , L" Authority Key ID: " , L" Extension not found" );
273- }
274277 // --- End AKI Retrieval ---
275278
276279 }
277280 catch (const string& s) {
278- if (doLogCertifError)
279- {
280- writeLog (L" c:\\ tmp\\ certifError.log" , L" verifySignedBinary: error while getting certificate information: " , s2ws (s).c_str ());
281- }
281+ writeSecurityError ((filepath + L" - error while getting certificate information: " ).c_str (), s2ws (s).c_str ());
282282 status = false ;
283283 }
284284 catch (...) {
285285 // Unknown error
286- if (doLogCertifError)
287- writeLog (L" c:\\ tmp\\ certifError.log" , L" verifySignedBinary: " , L" error while getting certificate information" );
288-
286+ writeSecurityError (filepath.c_str (), L" : Unknow error while getting certificate information" );
289287 status = false ;
290288 }
291289
292290 //
293- // fields verifications - if status is true, and string to compare (from the parameter) is not empty, then do compare
291+ // fields verifications - if status is true, and demaded parameter string to compare (from the parameter) is not empty, then do compare
294292 //
295293 if (status && (!_signer_display_name.empty () && _signer_display_name != display_name))
296294 {
297295 status = false ;
298-
299- if (doLogCertifError)
300- writeLog (L" c:\\ tmp\\ certifError.log" , L" verifySignedBinary: " , L" Invalid certificate display name" );
296+ writeSecurityError (filepath.c_str (), display_name + L" : Invalid certificate display name" );
301297 }
302298
303299 if (status && (!_signer_subject.empty () && _signer_subject != subject))
304300 {
305301 status = false ;
306-
307- if (doLogCertifError)
308- writeLog (L" c:\\ tmp\\ certifError.log" , L" verifySignedBinary: " , L" Invalid certificate subject" );
302+ writeSecurityError (filepath.c_str (), subject + L" : Invalid certificate subject" );
309303 }
310304
311305 if (status && (!_signer_key_id.empty () && stringToUpper (_signer_key_id) != key_id_hex))
312306 {
313307 status = false ;
314-
315- if (doLogCertifError)
316- writeLog (L" c:\\ tmp\\ certifError.log" , L" verifySignedBinary: " , L" Invalid certificate key id" );
308+ writeSecurityError (filepath.c_str (), key_id_hex + L" : Invalid certificate key id" );
317309 }
318310
319311 if (status && (!_authority_key_id.empty () && stringToUpper (_authority_key_id) != authority_key_id_hex))
320312 {
321313 status = false ;
322-
323- if (doLogCertifError)
324- writeLog (L" c:\\ tmp\\ certifError.log" , L" verifySignedBinary: " , L" Invalid authority key id" );
314+ writeSecurityError (filepath.c_str (), authority_key_id_hex + L" : Invalid authority key id" );
325315 }
326316
327317 // Clean up.
0 commit comments