| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | #include "precomp.h" |
| 4 | |
| 5 | // https://gist.github.com/navossoc/7572c7d82243e9f818989e2765e7793a |
| 6 | HRESULT DetectSHA2CodeSigning( |
| 7 | __out BOOL* pfSupported |
| 8 | ) |
| 9 | { |
| 10 | HRESULT hr = S_OK; |
| 11 | HMODULE hModule = NULL; |
| 12 | FARPROC pfn = NULL; |
| 13 | DWORD er = ERROR_SUCCESS; |
| 14 | |
| 15 | hr = LoadSystemLibrary(L"wintrust.dll", &hModule); |
| 16 | BextExitOnFailure(hr, "Failed to load wintrust.dll"); |
| 17 | |
| 18 | pfn = ::GetProcAddress(hModule, "CryptCATAdminAcquireContext2"); |
| 19 | if (pfn) |
| 20 | { |
| 21 | *pfSupported = TRUE; |
| 22 | ExitFunction1(hr = S_OK); |
| 23 | } |
| 24 | |
| 25 | er = ::GetLastError(); |
| 26 | if (er == ERROR_PROC_NOT_FOUND) |
| 27 | { |
| 28 | *pfSupported = FALSE; |
| 29 | ExitFunction1(hr = S_OK); |
| 30 | } |
| 31 | |
| 32 | hr = HRESULT_FROM_WIN32(er); |
| 33 | BextExitOnFailure(hr, "Failed to probe for CryptCATAdminAcquireContext2 in wintrust.dll"); |
| 34 | |
| 35 | LExit: |
| 36 | ::FreeLibrary(hModule); |
| 37 | |
| 38 | return hr; |
| 39 | } |
| 40 | |
| 41 | HRESULT UtilPerformDetectSHA2CodeSigning( |
| 42 | __in LPCWSTR wzVariable, |
| 43 | __in UTIL_SEARCH* /*pSearch*/, |
| 44 | __in IBootstrapperExtensionEngine* pEngine |
| 45 | ) |
| 46 | { |
| 47 | HRESULT hr = S_OK; |
| 48 | BOOL fSupported = FALSE; |
| 49 | |
| 50 | hr = DetectSHA2CodeSigning(&fSupported); |
| 51 | BextExitOnFailure(hr, "DetectSHA2CodeSigning failed."); |
| 52 | |
| 53 | hr = pEngine->SetVariableNumeric(wzVariable, fSupported ? 1 : 0); |
| 54 | BextExitOnFailure(hr, "Failed to set variable '%ls'", wzVariable); |
| 55 | |
| 56 | LExit: |
| 57 | return hr; |
| 58 | } |