1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
11 #include "certificatecheck.hxx"
12 #include "servicebase.hxx"
14 #pragma comment(lib, "wintrust.lib")
15 #pragma comment(lib, "crypt32.lib")
17 static const int ENCODING
= X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
;
20 * Checks to see if a file stored at filePath matches the specified info.
22 * @param filePath The PE file path to check
23 * @param infoToMatch The acceptable information to match
24 * @return ERROR_SUCCESS if successful, ERROR_NOT_FOUND if the info
25 * does not match, or the last error otherwise.
28 CheckCertificateForPEFile(LPCWSTR filePath
,
29 CertificateCheckInfo
&infoToMatch
)
31 HCERTSTORE certStore
= nullptr;
32 HCRYPTMSG cryptMsg
= nullptr;
33 PCCERT_CONTEXT certContext
= nullptr;
34 PCMSG_SIGNER_INFO signerInfo
= nullptr;
35 DWORD lastError
= ERROR_SUCCESS
;
37 // Get the HCERTSTORE and HCRYPTMSG from the signed file.
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
));
52 // Pass in nullptr to get the needed signer information size.
54 result
= CryptMsgGetParam(cryptMsg
, CMSG_SIGNER_INFO_PARAM
, 0,
55 nullptr, &signerInfoSize
);
58 lastError
= GetLastError();
59 LOG_WARN(("CryptMsgGetParam failed. (%d)", lastError
));
63 // Allocate the needed size for the signer information.
64 signerInfo
= (PCMSG_SIGNER_INFO
)LocalAlloc(LPTR
, signerInfoSize
);
67 lastError
= GetLastError();
68 LOG_WARN(("Unable to allocate memory for Signer Info. (%d)", lastError
));
72 // Get the signer information (PCMSG_SIGNER_INFO).
73 // In particular we want the issuer and serial number.
74 result
= CryptMsgGetParam(cryptMsg
, CMSG_SIGNER_INFO_PARAM
, 0,
75 (PVOID
)signerInfo
, &signerInfoSize
);
78 lastError
= GetLastError();
79 LOG_WARN(("CryptMsgGetParam failed. (%d)", lastError
));
83 // Search for the signer certificate in the certificate store.
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
));
97 if (!DoCertificateAttributesMatch(certContext
, infoToMatch
))
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
);
125 * Checks to see if a file stored at filePath matches the specified info.
127 * @param certContext The certificate context of the file
128 * @param infoToMatch The acceptable information to match
129 * @return FALSE if the info does not match or if any error occurs in the check
132 DoCertificateAttributesMatch(PCCERT_CONTEXT certContext
,
133 CertificateCheckInfo
&infoToMatch
)
136 LPTSTR szName
= nullptr;
138 if (infoToMatch
.issuer
)
140 // Pass in nullptr to get the needed size of the issuer buffer.
141 dwData
= CertGetNameString(certContext
,
142 CERT_NAME_SIMPLE_DISPLAY_TYPE
,
143 CERT_NAME_ISSUER_FLAG
, nullptr,
148 LOG_WARN(("CertGetNameString failed. (%d)", GetLastError()));
152 // Allocate memory for Issuer name buffer.
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()));
170 // If the issuer does not match, return a failure.
171 if (!infoToMatch
.issuer
||
172 wcscmp(szName
, infoToMatch
.issuer
))
182 if (infoToMatch
.name
)
184 // Pass in nullptr to get the needed size of the name buffer.
185 dwData
= CertGetNameString(certContext
, CERT_NAME_SIMPLE_DISPLAY_TYPE
,
186 0, nullptr, nullptr, 0);
189 LOG_WARN(("CertGetNameString failed. (%d)", GetLastError()));
193 // Allocate memory for the name buffer.
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()));
211 // If the issuer does not match, return a failure.
212 if (!infoToMatch
.name
||
213 wcscmp(szName
, infoToMatch
.name
))
223 // If there were any errors we would have aborted by now.
228 * Duplicates the specified string
230 * @param inputString The string to duplicate
231 * @return The duplicated string which should be freed by the caller.
234 AllocateAndCopyWideString(LPCWSTR inputString
)
236 LPWSTR outputString
=
237 (LPWSTR
)LocalAlloc(LPTR
, (wcslen(inputString
) + 1) * sizeof(WCHAR
));
240 lstrcpyW(outputString
, inputString
);
246 * Verifies the trust of the specified file path.
248 * @param filePath The file path to check.
249 * @return ERROR_SUCCESS if successful, or the last error code otherwise.
252 VerifyCertificateTrustForFile(LPCWSTR filePath
)
254 // Setup the file to check.
255 WINTRUST_FILE_INFO fileToCheck
;
256 ZeroMemory(&fileToCheck
, sizeof(fileToCheck
));
257 fileToCheck
.cbStruct
= sizeof(WINTRUST_FILE_INFO
);
258 fileToCheck
.pcwszFilePath
= filePath
;
260 // Setup what to check, we want to check it is signed and trusted.
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
;
277 // Check if the file is signed by something that is trusted.
278 LONG ret
= WinVerifyTrust(nullptr, &policyGUID
, &trustData
);
279 if (ERROR_SUCCESS
== ret
)
281 // The hash that represents the subject is trusted and there were no
282 // verification errors. No publisher nor time stamp chain errors.
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
));