#include "remoting/host/win/trust_util.h"
#include <windows.h>
#include <softpub.h>
#include "base/logging.h"
#include "base/win/wintrust_shim.h"
namespace remoting {
bool IsBinaryTrusted(const base::FilePath& binary_path) {
#if defined(OFFICIAL_BUILD)
WINTRUST_FILE_INFO file_info = {0};
file_info.cbStruct = sizeof(file_info);
file_info.pcwszFilePath = binary_path.value().c_str();
file_info.hFile = NULL;
file_info.pgKnownSubject = NULL;
WINTRUST_DATA wintrust_data = {0};
wintrust_data.cbStruct = sizeof(wintrust_data);
wintrust_data.pPolicyCallbackData = NULL;
wintrust_data.pSIPClientData = NULL;
wintrust_data.dwUIChoice = WTD_UI_NONE;
wintrust_data.fdwRevocationChecks = WTD_REVOKE_NONE;
wintrust_data.dwUnionChoice = WTD_CHOICE_FILE;
wintrust_data.dwStateAction = WTD_STATEACTION_VERIFY;
wintrust_data.hWVTStateData = NULL;
wintrust_data.pwszURLReference = NULL;
wintrust_data.dwUIContext = WTD_UICONTEXT_EXECUTE;
wintrust_data.dwProvFlags = WTD_CACHE_ONLY_URL_RETRIEVAL;
wintrust_data.pFile = &file_info;
GUID policy_guid = WINTRUST_ACTION_GENERIC_VERIFY_V2;
LONG trust_status = WinVerifyTrust(static_cast<HWND>(INVALID_HANDLE_VALUE),
&policy_guid, &wintrust_data);
DWORD dwLastError;
switch (trust_status) {
case ERROR_SUCCESS:
LOG(INFO) << "Signature verified for " << binary_path.value();
return true;
case TRUST_E_NOSIGNATURE:
dwLastError = GetLastError();
switch (static_cast<HRESULT>(dwLastError)) {
case TRUST_E_NOSIGNATURE:
LOG(ERROR) << "No signature found for " << binary_path.value()
<< ". ErrorReason: 0x" << std::hex << dwLastError;
break;
case TRUST_E_SUBJECT_FORM_UNKNOWN:
LOG(ERROR) << "The trust provider does not support the form "
<< "specified for the subject for " << binary_path.value()
<< ". ErrorReason: 0x" << std::hex << dwLastError;
break;
case TRUST_E_PROVIDER_UNKNOWN:
LOG(ERROR) << "The trust provider is not recognized on this system "
<< "for " << binary_path.value() << ". ErrorReason: 0x"
<< std::hex << dwLastError;
break;
default:
LOG(ERROR) << "Could not verify signature for " << binary_path.value()
<< ". ErrorReason: " << dwLastError << ", 0x" << std::hex
<< dwLastError;
break;
}
return false;
case TRUST_E_EXPLICIT_DISTRUST:
LOG(ERROR) << "Signature for " << binary_path.value() << " is present, "
<< "but is explicitly distrusted.";
return false;
case TRUST_E_SUBJECT_NOT_TRUSTED:
LOG(ERROR) << "Signature for " << binary_path.value() << " is present, "
<< "but not trusted.";
return false;
case CRYPT_E_SECURITY_SETTINGS:
LOG(ERROR) << "Verification failed for " << binary_path.value() << ". "
<< "The hash representing the subject or the publisher wasn't "
<< "explicitly trusted by the admin and admin policy has "
<< "disabled user trust. No signature, publisher or timestamp "
<< "errors.";
return false;
default:
LOG(ERROR) << "Signature verification error for " << binary_path.value()
<< ": 0x" << std::hex << trust_status;
return false;
}
#else
return true;
#endif
}
}