14#pragma comment(lib, "wintrust.lib")
15#pragma comment(lib, "crypt32.lib")
17static const int ENCODING = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
31 HCERTSTORE certStore =
nullptr;
32 HCRYPTMSG cryptMsg =
nullptr;
33 PCCERT_CONTEXT certContext =
nullptr;
34 PCMSG_SIGNER_INFO signerInfo =
nullptr;
35 DWORD lastError = ERROR_SUCCESS;
38 DWORD encoding, contentType, formatType;
39 BOOL result = CryptQueryObject(CERT_QUERY_OBJECT_FILE,
41 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED,
42 CERT_QUERY_CONTENT_FLAG_ALL,
43 0, &encoding, &contentType,
44 &formatType, &certStore, &cryptMsg,
nullptr);
47 lastError = GetLastError();
48 LOG_WARN((
"CryptQueryObject failed. (%d)", lastError));
54 result = CryptMsgGetParam(cryptMsg, CMSG_SIGNER_INFO_PARAM, 0,
55 nullptr, &signerInfoSize);
58 lastError = GetLastError();
59 LOG_WARN((
"CryptMsgGetParam failed. (%d)", lastError));
64 signerInfo = (PCMSG_SIGNER_INFO)LocalAlloc(LPTR, signerInfoSize);
67 lastError = GetLastError();
68 LOG_WARN((
"Unable to allocate memory for Signer Info. (%d)", lastError));
74 result = CryptMsgGetParam(cryptMsg, CMSG_SIGNER_INFO_PARAM, 0,
75 (PVOID)signerInfo, &signerInfoSize);
78 lastError = GetLastError();
79 LOG_WARN((
"CryptMsgGetParam failed. (%d)", lastError));
85 certInfo.Issuer = signerInfo->Issuer;
86 certInfo.SerialNumber = signerInfo->SerialNumber;
87 certContext = CertFindCertificateInStore(certStore,
ENCODING, 0,
88 CERT_FIND_SUBJECT_CERT,
89 (PVOID)&certInfo,
nullptr);
92 lastError = GetLastError();
93 LOG_WARN((
"CertFindCertificateInStore failed. (%d)", lastError));
99 lastError = ERROR_NOT_FOUND;
100 LOG_WARN((
"Certificate did not match issuer or name. (%d)", lastError));
107 LocalFree(signerInfo);
111 CertFreeCertificateContext(certContext);
115 CertCloseStore(certStore, 0);
119 CryptMsgClose(cryptMsg);
136 LPTSTR szName =
nullptr;
141 dwData = CertGetNameString(certContext,
142 CERT_NAME_SIMPLE_DISPLAY_TYPE,
143 CERT_NAME_ISSUER_FLAG,
nullptr,
148 LOG_WARN((
"CertGetNameString failed. (%d)", GetLastError()));
153 LPTSTR szName = (LPTSTR)LocalAlloc(LPTR, dwData *
sizeof(WCHAR));
156 LOG_WARN((
"Unable to allocate memory for issuer name. (%d)",
162 if (!CertGetNameString(certContext, CERT_NAME_SIMPLE_DISPLAY_TYPE,
163 CERT_NAME_ISSUER_FLAG,
nullptr, szName, dwData))
165 LOG_WARN((
"CertGetNameString failed. (%d)", GetLastError()));
171 if (!infoToMatch.
issuer ||
172 wcscmp(szName, infoToMatch.
issuer))
182 if (infoToMatch.
name)
185 dwData = CertGetNameString(certContext, CERT_NAME_SIMPLE_DISPLAY_TYPE,
186 0,
nullptr,
nullptr, 0);
189 LOG_WARN((
"CertGetNameString failed. (%d)", GetLastError()));
194 szName = (LPTSTR)LocalAlloc(LPTR, dwData *
sizeof(WCHAR));
197 LOG_WARN((
"Unable to allocate memory for subject name. (%d)",
203 if (!(CertGetNameString(certContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0,
204 nullptr, szName, dwData)))
206 LOG_WARN((
"CertGetNameString failed. (%d)", GetLastError()));
212 if (!infoToMatch.
name ||
213 wcscmp(szName, infoToMatch.
name))
236 LPWSTR outputString =
237 (LPWSTR)LocalAlloc(LPTR, (wcslen(inputString) + 1) *
sizeof(WCHAR));
240 lstrcpyW(outputString, inputString);
255 WINTRUST_FILE_INFO fileToCheck;
256 ZeroMemory(&fileToCheck,
sizeof(fileToCheck));
257 fileToCheck.cbStruct =
sizeof(WINTRUST_FILE_INFO);
258 fileToCheck.pcwszFilePath =
filePath;
261 WINTRUST_DATA trustData;
262 ZeroMemory(&trustData,
sizeof(trustData));
263 trustData.cbStruct =
sizeof(trustData);
264 trustData.pPolicyCallbackData =
nullptr;
265 trustData.pSIPClientData =
nullptr;
266 trustData.dwUIChoice = WTD_UI_NONE;
267 trustData.fdwRevocationChecks = WTD_REVOKE_NONE;
268 trustData.dwUnionChoice = WTD_CHOICE_FILE;
269 trustData.dwStateAction = 0;
270 trustData.hWVTStateData =
nullptr;
271 trustData.pwszURLReference =
nullptr;
273 trustData.dwUIContext = 0;
274 trustData.pFile = &fileToCheck;
276 GUID policyGUID = WINTRUST_ACTION_GENERIC_VERIFY_V2;
278 LONG ret = WinVerifyTrust(
nullptr, &policyGUID, &trustData);
279 if (ERROR_SUCCESS == ret)
283 LOG((
"The file \"%ls\" is signed and the signature was verified.",
285 return ERROR_SUCCESS;
288 DWORD lastError = GetLastError();
289 LOG_WARN((
"There was an error validating trust of the certificate for file"
290 " \"%ls\". Returned: %d. (%d)",
filePath, ret, lastError));
DWORD CheckCertificateForPEFile(LPCWSTR filePath, CertificateCheckInfo &infoToMatch)
Checks to see if a file stored at filePath matches the specified info.
LPWSTR AllocateAndCopyWideString(LPCWSTR inputString)
Duplicates the specified string.
BOOL DoCertificateAttributesMatch(PCCERT_CONTEXT certContext, CertificateCheckInfo &infoToMatch)
Checks to see if a file stored at filePath matches the specified info.
static const int ENCODING
DWORD VerifyCertificateTrustForFile(LPCWSTR filePath)
Verifies the trust of the specified file path.
tDoubleVectorPair cleanup(const css::uno::Sequence< double > &rXValues, const css::uno::Sequence< double > &rYValues, Pred aPred)
const wchar_t *typedef BOOL