43 static GUID WinTrustActionGenericVerifyV2 = WINTRUST_ACTION_GENERIC_VERIFY_V2;
44 static GUID DriverActionVerify = DRIVER_ACTION_VERIFY;
46 static VOID PhpVerifyInitialization(
53 wintrust = LoadLibrary(L
"wintrust.dll");
54 crypt32 = LoadLibrary(L
"crypt32.dll");
80 case TRUST_E_NOSIGNATURE:
86 case TRUST_E_EXPLICIT_DISTRUST:
88 case CRYPT_E_SECURITY_SETTINGS:
90 case TRUST_E_BAD_DIGEST:
98 _In_ HANDLE StateData,
99 _Out_ PCERT_CONTEXT **Signatures,
100 _Out_ PULONG NumberOfSignatures
103 PCRYPT_PROVIDER_DATA provData;
104 PCRYPT_PROVIDER_SGNR sgnr;
105 PCERT_CONTEXT *signatures;
107 ULONG numberOfSignatures;
115 *NumberOfSignatures = 0;
120 numberOfSignatures = 0;
124 if (sgnr->csCertChain != 0)
125 numberOfSignatures++;
130 if (numberOfSignatures != 0)
132 signatures = PhAllocate(numberOfSignatures *
sizeof(PCERT_CONTEXT));
138 if (sgnr->csCertChain != 0)
149 *Signatures = signatures;
150 *NumberOfSignatures = numberOfSignatures;
157 _In_ HANDLE StateData
165 HMODULE cryptui = LoadLibrary(L
"cryptui.dll");
167 cryptUIDlgViewSignerInfo = (PVOID)GetProcAddress(cryptui,
"CryptUIDlgViewSignerInfoW");
171 if (cryptUIDlgViewSignerInfo)
174 PCRYPT_PROVIDER_DATA provData;
175 PCRYPT_PROVIDER_SGNR sgnr;
182 viewSignerInfo.
hwndParent = Information->hWnd;
184 viewSignerInfo.
hMsg = provData->hMsg;
185 viewSignerInfo.
pszOID = szOID_PKIX_KP_CODE_SIGNING;
186 cryptUIDlgViewSignerInfo(&viewSignerInfo);
192 _In_ HANDLE FileHandle,
193 _In_ ULONG UnionChoice,
194 _In_ PVOID UnionData,
196 _In_opt_ PVOID PolicyCallbackData,
197 _Out_ PCERT_CONTEXT **Signatures,
198 _Out_ PULONG NumberOfSignatures
202 WINTRUST_DATA trustData = { 0 };
204 trustData.cbStruct =
sizeof(WINTRUST_DATA);
205 trustData.pPolicyCallbackData = PolicyCallbackData;
206 trustData.dwUIChoice = WTD_UI_NONE;
207 trustData.fdwRevocationChecks = WTD_REVOKE_WHOLECHAIN;
208 trustData.dwUnionChoice = UnionChoice;
209 trustData.dwStateAction = WTD_STATEACTION_VERIFY;
210 trustData.dwProvFlags = WTD_SAFER_FLAG;
212 trustData.pFile = UnionData;
214 if (UnionChoice == WTD_CHOICE_CATALOG)
215 trustData.pCatalog = UnionData;
219 trustData.fdwRevocationChecks = WTD_REVOKE_NONE;
222 trustData.dwProvFlags |= WTD_CACHE_ONLY_URL_RETRIEVAL;
224 trustData.dwProvFlags |= WTD_REVOCATION_CHECK_NONE;
234 trustData.dwStateAction = WTD_STATEACTION_CLOSE;
241 _In_ HANDLE FileHandle,
242 _In_ PWSTR HashAlgorithm,
243 _Out_ PUCHAR *FileHash,
244 _Out_ PULONG FileHashLength,
245 _Out_ HANDLE *CatAdminHandle
248 HANDLE catAdminHandle;
250 ULONG fileHashLength;
264 fileHash = PhAllocate(fileHashLength);
271 fileHash = PhAllocate(fileHashLength);
286 fileHash = PhAllocate(fileHashLength);
297 *FileHash = fileHash;
298 *FileHashLength = fileHashLength;
299 *CatAdminHandle = catAdminHandle;
306 _In_ HANDLE FileHandle,
307 _In_opt_ PWSTR HashAlgorithm,
308 _Out_ PCERT_CONTEXT **Signatures,
309 _Out_ PULONG NumberOfSignatures
313 PCERT_CONTEXT *signatures;
314 ULONG numberOfSignatures;
315 WINTRUST_CATALOG_INFO catalogInfo = { 0 };
316 LARGE_INTEGER fileSize;
319 ULONG fileHashLength;
321 HANDLE catAdminHandle;
322 HANDLE catInfoHandle;
326 *NumberOfSignatures = 0;
332 numberOfSignatures = 0;
334 if (Information->FileSizeLimitForHash != -1)
338 if (Information->FileSizeLimitForHash != 0)
339 fileSizeLimit = Information->FileSizeLimitForHash;
341 if (fileSize.QuadPart > fileSizeLimit)
345 if (
PhpCalculateFileHash(FileHandle, HashAlgorithm, &fileHash, &fileHashLength, &catAdminHandle))
362 DRIVER_VER_INFO verInfo = { 0 };
367 verInfo.
cbStruct =
sizeof(DRIVER_VER_INFO);
369 catalogInfo.cbStruct =
sizeof(catalogInfo);
371 catalogInfo.pcwszMemberFilePath = Information->FileName;
372 catalogInfo.pcwszMemberTag = fileHashTag->
Buffer;
373 catalogInfo.pbCalculatedFileHash = fileHash;
374 catalogInfo.cbCalculatedFileHash = fileHashLength;
375 catalogInfo.hCatAdmin = catAdminHandle;
376 verifyResult =
PhpVerifyFile(Information, FileHandle, WTD_CHOICE_CATALOG, &catalogInfo, &DriverActionVerify, &verInfo, &signatures, &numberOfSignatures);
378 if (verInfo.pcSignerCertContext)
388 for (i = 0; i < Information->NumberOfCatalogFileNames; i++)
392 catalogInfo.cbStruct =
sizeof(catalogInfo);
393 catalogInfo.pcwszCatalogFilePath = Information->CatalogFileNames[i];
394 catalogInfo.pcwszMemberFilePath = Information->FileName;
395 catalogInfo.pcwszMemberTag = fileHashTag->
Buffer;
396 catalogInfo.pbCalculatedFileHash = fileHash;
397 catalogInfo.cbCalculatedFileHash = fileHashLength;
398 catalogInfo.hCatAdmin = catAdminHandle;
399 verifyResult =
PhpVerifyFile(Information, FileHandle, WTD_CHOICE_CATALOG, &catalogInfo, &WinTrustActionGenericVerifyV2, NULL, &signatures, &numberOfSignatures);
411 *Signatures = signatures;
412 *NumberOfSignatures = numberOfSignatures;
420 _Out_opt_ PCERT_CONTEXT **Signatures,
421 _Out_opt_ PULONG NumberOfSignatures
427 PCERT_CONTEXT *signatures;
428 ULONG numberOfSignatures;
429 WINTRUST_FILE_INFO fileInfo = { 0 };
433 PhpVerifyInitialization();
453 return STATUS_NOT_SUPPORTED;
457 Information->FileName,
460 FILE_SHARE_READ | FILE_SHARE_DELETE,
466 fileInfo.cbStruct =
sizeof(WINTRUST_FILE_INFO);
467 fileInfo.pcwszFilePath = Information->FileName;
468 fileInfo.hFile = fileHandle;
470 verifyResult =
PhpVerifyFile(Information, fileHandle, WTD_CHOICE_FILE, &fileInfo, &WinTrustActionGenericVerifyV2, NULL, &signatures, &numberOfSignatures);
477 verifyResult =
PhpVerifyFileFromCatalog(Information, fileHandle, BCRYPT_SHA256_ALGORITHM, &signatures, &numberOfSignatures);
487 *VerifyResult = verifyResult;
490 *Signatures = signatures;
494 if (NumberOfSignatures)
495 *NumberOfSignatures = numberOfSignatures;
499 return STATUS_SUCCESS;
503 _In_ PCERT_CONTEXT *Signatures,
504 _In_ ULONG NumberOfSignatures
511 for (i = 0; i < NumberOfSignatures; i++)
519 _In_ PCERT_NAME_BLOB Blob
554 WCHAR keyNamePlusEqualsBuffer[10];
556 SIZE_T keyNameLength;
560 keyNameLength = KeyName->
Length /
sizeof(WCHAR);
561 assert(!(keyNameLength >
sizeof(keyNamePlusEquals) /
sizeof(WCHAR) - 1));
562 keyNamePlusEquals.
Buffer = keyNamePlusEqualsBuffer;
563 keyNamePlusEquals.
Length = (keyNameLength + 1) *
sizeof(WCHAR);
565 memcpy(keyNamePlusEquals.
Buffer, KeyName->Buffer, KeyName->Length);
566 keyNamePlusEquals.
Buffer[keyNameLength] =
'=';
572 if (remainingPart.
Length == 0)
576 if (remainingPart.
Buffer[0] ==
'"')
594 _In_ PCERT_CONTEXT Certificate
604 certInfo = Certificate->pCertInfo;
648 PCERT_CONTEXT *signatures;
649 ULONG numberOfSignatures;
660 if (numberOfSignatures != 0)