Verify the embedded signature

 

So i had this week I had to figure out how to determine if a driver is signed for a little project of mine. This involves being signed by a code certificate OR by Microsoft via WHQL (which includes checking cat files). Documentation on MSDN for this was horrible at best. Very little explained, and very few examples. And these functions calls have a tons of paramenters, some to which I don't even really understand what they do. I got my code working, thought I'm not sure I understand 100% of how everything works. I got little snippets of codes from a couple of places, did some of my own, and together came up with this. If anyone has improvementes or find any errors, please let me know!! thanks, hope this helps!

 

  1. #include <windows.h> 
  2. #include <Softpub.h> 
  3. #include <wincrypt.h> 
  4. #include <wintrust.h> 
  5. #include <Mscat.h> 
  6.  
  7. BOOL VerifyEmbeddedSignature(LPCWSTR pwszSourceFile) 
  8. LONG lStatus; 
  9. GUID WintrustVerifyGuid = DRIVER_ACTION_VERIFY; 
  10. GUID DriverActionGuid = DRIVER_ACTION_VERIFY; 
  11. HANDLE hFile; 
  12. DWORD dwHash; 
  13. BYTE bHash[100]; 
  14. HCATINFO hCatInfo; 
  15. HCATADMIN hCatAdmin; 
  16. WINTRUST_DATA wd = { 0 }; 
  17. WINTRUST_FILE_INFO wfi = { 0 }; 
  18. WINTRUST_CATALOG_INFO wci = { 0 }; 
  19. DRIVER_VER_INFO dvi = {0}; 
  20.  
  21. dvi.cbStruct = sizeof(dvi); 
  22.  
  23. ////set up structs to verify files with cert signatures 
  24. memset(&wfi, 0, sizeof(wfi)); 
  25. wfi.cbStruct = sizeof( WINTRUST_FILE_INFO ); 
  26. wfi.pcwszFilePath = pwszSourceFile; 
  27. wfi.hFile = NULL; 
  28. wfi.pgKnownSubject = NULL; 
  29.  
  30. memset(&wd, 0, sizeof(wd)); 
  31. wd.cbStruct = sizeof( WINTRUST_DATA ); 
  32. wd.dwUnionChoice = WTD_CHOICE_FILE; 
  33. wd.pFile = &wfi; 
  34. wd.dwUIChoice = WTD_UI_NONE; 
  35. wd.fdwRevocationChecks = WTD_REVOKE_NONE; 
  36. wd.dwStateAction = 0; 
  37. wd.dwProvFlags = WTD_SAFER_FLAG; 
  38. wd.hWVTStateData = NULL; 
  39. wd.pwszURLReference = NULL; 
  40. wd.pPolicyCallbackData = &dvi; 
  41. wd.pSIPClientData = NULL; 
  42. wd.dwUIContext = 0; 
  43.  
  44. lStatus = WinVerifyTrust( NULL, &WintrustVerifyGuid, &wd ); 
  45.  
  46. ////if failed, try to verify using catalog files 
  47. if (lStatus != ERROR_SUCCESS) 
  48. //open the file 
  49. hFile = CreateFileW(pwszSourceFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 
  50. if (hFile == INVALID_HANDLE_VALUE) 
  51. return FALSE; 
  52.  
  53. dwHash = sizeof(bHash); 
  54. if (!CryptCATAdminCalcHashFromFileHandle(hFile, &dwHash, bHash, 0)) 
  55. CloseHandle(hFile); 
  56. return FALSE; 
  57.  
  58. //Create a string form of the hash (used later in pszMemberTag) 
  59. LPWSTR pszMemberTag = new WCHAR[dwHash * 2 + 1]; 
  60. for ( DWORD dw = 0; dw < dwHash; ++dw ) 
  61. wsprintfW( &pszMemberTag[dw * 2], L"%02X", bHash[dw] ); 
  62.  
  63. if (!CryptCATAdminAcquireContext(&hCatAdmin, &DriverActionGuid, 0)) 
  64. CloseHandle(hFile); 
  65. return FALSE; 
  66.  
  67. //find the catalog which contains the hash 
  68. hCatInfo = CryptCATAdminEnumCatalogFromHash(hCatAdmin, bHash, dwHash, 0, NULL); 
  69.  
  70. if ( hCatInfo ) 
  71. CATALOG_INFO ci = { 0 }; 
  72. CryptCATCatalogInfoFromContext( hCatInfo, &ci, 0 ); 
  73.  
  74. memset(&wci, 0, sizeof(wci)); 
  75. wci.cbStruct = sizeof( WINTRUST_CATALOG_INFO ); 
  76. wci.pcwszCatalogFilePath = ci.wszCatalogFile; 
  77. wci.pcwszMemberFilePath = pwszSourceFile; 
  78. wci.pcwszMemberTag = pszMemberTag; 
  79.  
  80. memset(&wd, 0, sizeof(wd)); 
  81. wd.cbStruct = sizeof( WINTRUST_DATA ); 
  82. wd.dwUnionChoice = WTD_CHOICE_CATALOG; 
  83. wd.pCatalog = &wci; 
  84. wd.dwUIChoice = WTD_UI_NONE; 
  85. wd.fdwRevocationChecks = WTD_STATEACTION_VERIFY; 
  86. wd.dwProvFlags = 0; 
  87. wd.hWVTStateData = NULL; 
  88. wd.pwszURLReference = NULL; 
  89. wd.pPolicyCallbackData = &dvi; 
  90. wd.pSIPClientData = NULL; 
  91. wd.dwUIContext = 0; 
  92.  
  93. lStatus = WinVerifyTrust( NULL, &WintrustVerifyGuid, &wd ); 
  94.  
  95. CryptCATAdminReleaseCatalogContext( hCatAdmin, hCatInfo, 0 ); 
  96.  
  97. CryptCATAdminReleaseContext( hCatAdmin, 0 ); 
  98. delete[] pszMemberTag; 
  99. CloseHandle(hFile); 
  100.  
  101. printf( "version:%S/nsigner:%S", dvi.wszVersion, dvi.wszSignedBy ); 
  102.  
  103. //I believe we have to clean up our cert context 
  104. CertFreeCertificateContext(dvi.pcSignerCertContext); 
  105.  
  106. if (lStatus != ERROR_SUCCESS) 
  107. return false
  108. else  
  109. return true
  110.  
  111. int main() 
  112.  
  113. VerifyEmbeddedSignature( L"C://windows//system32//drivers//i8042prt.sys" ); 

你可能感兴趣的:(EMBED)