Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.3 KiB

  1. //+-------------------------------------------------------------------------
  2. // File: wvtexample.cpp
  3. //
  4. // Contents: An example calling WinVerifyTrust for Safer
  5. //--------------------------------------------------------------------------
  6. #include <windows.h>
  7. #include <wincrypt.h>
  8. #include <wintrust.h>
  9. #include <softpub.h>
  10. void SaferVerifyFileExample(
  11. IN LPCWSTR pwszFilename
  12. )
  13. {
  14. LONG lStatus;
  15. DWORD dwLastError;
  16. GUID wvtFileActionID = WINTRUST_ACTION_GENERIC_VERIFY_V2;
  17. WINTRUST_FILE_INFO wvtFileInfo;
  18. WINTRUST_DATA wvtData;
  19. //
  20. // Initialize the WinVerifyTrust input data structure
  21. //
  22. memset(&wvtData, 0, sizeof(wvtData)); // default all fields to 0
  23. wvtData.cbStruct = sizeof(wvtData);
  24. // wvtData.pPolicyCallbackData = // use default code signing EKU
  25. // wvtData.pSIPClientData = // no data to pass to SIP
  26. // Display UI if not already trusted or disallowed. Note, admin policy
  27. // may disable UI.
  28. wvtData.dwUIChoice = WTD_UI_ALL;
  29. // wvtData.fdwRevocationChecks = // do revocation checking if
  30. // enabled by admin policy or
  31. // IE advanced user options
  32. wvtData.dwUnionChoice = WTD_CHOICE_FILE;
  33. wvtData.pFile = &wvtFileInfo;
  34. // wvtData.dwStateAction = // default verification
  35. // wvtData.hWVTStateData = // not applicable for default
  36. // wvtData.pwszURLReference = // not used
  37. // Enable safer semantics:
  38. // - if the subject isn't signed, return immediately without UI
  39. // - ignore NO_CHECK revocation errors
  40. // - always search the code hash and publisher databases, even when
  41. // UI has been disabled in dwUIChoice.
  42. wvtData.dwProvFlags = WTD_SAFER_FLAG;
  43. //
  44. // Initialize the WinVerifyTrust file info data structure
  45. //
  46. memset(&wvtFileInfo, 0, sizeof(wvtFileInfo)); // default all fields to 0
  47. wvtFileInfo.cbStruct = sizeof(wvtFileInfo);
  48. wvtFileInfo.pcwszFilePath = pwszFilename;
  49. // wvtFileInfo.hFile = // allow WVT to open
  50. // wvtFileInfo.pgKnownSubject // allow WVT to determine
  51. //
  52. // Call WinVerifyTrust
  53. //
  54. lStatus = WinVerifyTrust(
  55. NULL, // hwnd
  56. &wvtFileActionID,
  57. &wvtData
  58. );
  59. //
  60. // Process the WinVerifyTrust errors
  61. //
  62. switch (lStatus) {
  63. case ERROR_SUCCESS:
  64. // Signed file:
  65. // - Hash representing the subject is trusted.
  66. // - Trusted publisher without any verification errors.
  67. // - UI was disabled in dwUIChoice. No publisher or timestamp
  68. // chain errors.
  69. // - UI was enabled in dwUIChoice and the user clicked "Yes"
  70. // when asked to install and run the signed subject.
  71. break;
  72. case TRUST_E_NOSIGNATURE:
  73. // The file wasn't signed or had an invalid signature
  74. // Get the reason for no signature
  75. dwLastError = GetLastError();
  76. if (TRUST_E_NOSIGNATURE == dwLastError ||
  77. TRUST_E_SUBJECT_FORM_UNKNOWN == dwLastError ||
  78. TRUST_E_PROVIDER_UNKNOWN == dwLastError) {
  79. // The file wasn't signed
  80. } else {
  81. // Invalid signature or error opening the file
  82. }
  83. break;
  84. case TRUST_E_EXPLICIT_DISTRUST:
  85. // The hash representing the subject or the publisher is
  86. // disallowed by the admin or user
  87. break;
  88. case TRUST_E_SUBJECT_NOT_TRUSTED:
  89. // The user clicked "No" when asked to install and run
  90. break;
  91. case CRYPT_E_SECURITY_SETTINGS:
  92. // The hash representing the subject or the publisher wasn't
  93. // explicitly trusted by the admin and admin policy has
  94. // disabled user trust. No signature, publisher or timestamp
  95. // errors.
  96. break;
  97. default:
  98. // UI was disabled in dwUIChoice or admin policy has disabled
  99. // user trust. lStatus contains the publisher or timestamp
  100. // chain error.
  101. break;
  102. }
  103. }