@@ -62,22 +62,24 @@ void SecurityGuard::writeSecurityError(const std::wstring& prefix, const std::ws
6262 writeLog (expandedLogFileName.c_str (), prefix.c_str (), log2write.c_str ());
6363}
6464
65- bool SecurityGuard::verifySignedBinary ( const std::wstring& filepath )
65+ bool SecurityGuard::initFromSelfCertif ( )
6666{
67- wstring display_name;
68- wstring key_id_hex;
69- wstring subject;
70- wstring authority_key_id_hex;
67+ wchar_t codeSigedBinPath[MAX_PATH]{};
68+ ::GetModuleFileName (NULL , codeSigedBinPath, MAX_PATH);
69+
70+ return verifySignatureAndGetInfo (codeSigedBinPath, _signer_display_name, _signer_key_id, _signer_subject, _authority_key_id);
71+ }
7172
73+ bool SecurityGuard::verifySignatureAndGetInfo (const std::wstring& codeSigedBinPath, wstring& display_name, wstring& key_id_hex, wstring& subject, wstring& authority_key_id_hex)
74+ {
7275 //
7376 // Signature verification
7477 //
7578
7679 // Initialize the WINTRUST_FILE_INFO structure.
77- LPCWSTR pwszfilepath = filepath.c_str ();
7880 WINTRUST_FILE_INFO file_data = {};
7981 file_data.cbStruct = sizeof (WINTRUST_FILE_INFO);
80- file_data.pcwszFilePath = pwszfilepath ;
82+ file_data.pcwszFilePath = codeSigedBinPath. c_str () ;
8183
8284 // Initialise WinTrust data
8385 WINTRUST_DATA winTEXTrust_data = {};
@@ -121,13 +123,13 @@ bool SecurityGuard::verifySignedBinary(const std::wstring& filepath)
121123
122124 if (vtrust)
123125 {
124- writeSecurityError (filepath. c_str () , L" : chain of trust verification failed" );
126+ writeSecurityError (codeSigedBinPath , L" : chain of trust verification failed" );
125127 return false ;
126128 }
127129
128130 if (t2)
129131 {
130- writeSecurityError (filepath. c_str () , L" : error encountered while cleaning up after WinVerifyTrust" );
132+ writeSecurityError (codeSigedBinPath , L" : error encountered while cleaning up after WinVerifyTrust" );
131133 return false ;
132134 }
133135 }
@@ -143,14 +145,14 @@ bool SecurityGuard::verifySignedBinary(const std::wstring& filepath)
143145 bool status = true ;
144146
145147 try {
146- BOOL result = ::CryptQueryObject (CERT_QUERY_OBJECT_FILE, filepath .c_str (),
148+ BOOL result = ::CryptQueryObject (CERT_QUERY_OBJECT_FILE, codeSigedBinPath .c_str (),
147149 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED, CERT_QUERY_FORMAT_FLAG_BINARY, 0 ,
148150 &dwEncoding, &dwContentType, &dwFormatType,
149151 &hStore, &hMsg, NULL );
150152
151153 if (!result)
152154 {
153- throw string (" Checking certificate of " ) + ws2s (filepath ) + " : " + ws2s (GetLastErrorAsString (GetLastError ()));
155+ throw string (" Checking certificate of " ) + ws2s (codeSigedBinPath ) + " : " + ws2s (GetLastErrorAsString (GetLastError ()));
154156 }
155157
156158 // Get signer information size.
@@ -235,13 +237,12 @@ bool SecurityGuard::verifySignedBinary(const std::wstring& filepath)
235237
236238
237239 // --- Retrieve Authority Key Identifier (AKI) ---
238-
239- PCERT_EXTENSION pExtension = ::CertFindExtension (szOID_AUTHORITY_KEY_IDENTIFIER2, // OID for Authority Key Identifier (2.5.29.35)
240- context->pCertInfo ->cExtension , context->pCertInfo ->rgExtension );
240+ // OID for Authority Key Identifier (2.5.29.35)
241+ PCERT_EXTENSION pExtension = ::CertFindExtension (szOID_AUTHORITY_KEY_IDENTIFIER2, context->pCertInfo ->cExtension , context->pCertInfo ->rgExtension );
241242
242243 if (!pExtension)
243- pExtension = :: CertFindExtension (szOID_AUTHORITY_KEY_IDENTIFIER, // OID for Authority Key Identifier (2.5.29.1)
244- context->pCertInfo ->cExtension , context->pCertInfo ->rgExtension );
244+ // OID for Authority Key Identifier (2.5.29.1)
245+ pExtension = :: CertFindExtension (szOID_AUTHORITY_KEY_IDENTIFIER, context->pCertInfo ->cExtension , context->pCertInfo ->rgExtension );
245246
246247 if (pExtension)
247248 {
@@ -278,15 +279,35 @@ bool SecurityGuard::verifySignedBinary(const std::wstring& filepath)
278279
279280 }
280281 catch (const string& s) {
281- writeSecurityError ((filepath + L" - error while getting certificate information: " ).c_str (), s2ws (s).c_str ());
282+ wstring msg = codeSigedBinPath;
283+ msg += L" - error while getting certificate information: " ;
284+ writeSecurityError (msg, s2ws (s));
282285 status = false ;
283286 }
284287 catch (...) {
285288 // Unknown error
286- writeSecurityError (filepath. c_str () , L" : Unknow error while getting certificate information" );
289+ writeSecurityError (codeSigedBinPath , L" : Unknow error while getting certificate information" );
287290 status = false ;
288291 }
289292
293+ // Clean up.
294+
295+ if (hStore != NULL ) CertCloseStore (hStore, 0 );
296+ if (hMsg != NULL ) CryptMsgClose (hMsg);
297+ if (pSignerInfo != NULL ) LocalFree (pSignerInfo);
298+
299+ return status;
300+ }
301+
302+ bool SecurityGuard::verifySignedBinary (const std::wstring& filepath)
303+ {
304+ wstring display_name;
305+ wstring key_id_hex;
306+ wstring subject;
307+ wstring authority_key_id_hex;
308+
309+ bool status = verifySignatureAndGetInfo (filepath, display_name, key_id_hex, subject, authority_key_id_hex);
310+
290311 //
291312 // fields verifications - if status is true, and demaded parameter string to compare (from the parameter) is not empty, then do compare
292313 //
@@ -298,7 +319,7 @@ bool SecurityGuard::verifySignedBinary(const std::wstring& filepath)
298319 errMsg += _signer_display_name;
299320 errMsg += L" vs unexpected " ;
300321 errMsg += display_name;
301- writeSecurityError (filepath. c_str () , errMsg);
322+ writeSecurityError (filepath, errMsg);
302323 }
303324
304325 if (status && (!_signer_subject.empty () && _signer_subject != subject))
@@ -309,7 +330,7 @@ bool SecurityGuard::verifySignedBinary(const std::wstring& filepath)
309330 errMsg += _signer_subject;
310331 errMsg += L" vs unexpected " ;
311332 errMsg += subject;
312- writeSecurityError (filepath. c_str () , errMsg);
333+ writeSecurityError (filepath, errMsg);
313334 }
314335
315336 if (status && (!_signer_key_id.empty () && stringToUpper (_signer_key_id) != key_id_hex))
@@ -320,7 +341,7 @@ bool SecurityGuard::verifySignedBinary(const std::wstring& filepath)
320341 errMsg += _signer_key_id;
321342 errMsg += L" vs unexpected " ;
322343 errMsg += key_id_hex;
323- writeSecurityError (filepath. c_str () , errMsg);
344+ writeSecurityError (filepath, errMsg);
324345 }
325346
326347 if (status && (!_authority_key_id.empty () && stringToUpper (_authority_key_id) != authority_key_id_hex))
@@ -331,14 +352,8 @@ bool SecurityGuard::verifySignedBinary(const std::wstring& filepath)
331352 errMsg += _authority_key_id;
332353 errMsg += L" vs unexpected " ;
333354 errMsg += authority_key_id_hex;
334- writeSecurityError (filepath. c_str () , errMsg);
355+ writeSecurityError (filepath, errMsg);
335356 }
336357
337- // Clean up.
338-
339- if (hStore != NULL ) CertCloseStore (hStore, 0 );
340- if (hMsg != NULL ) CryptMsgClose (hMsg);
341- if (pSignerInfo != NULL ) LocalFree (pSignerInfo);
342-
343358 return status;
344359}
0 commit comments