Skip to content

Commit ef9d08b

Browse files
committed
api: incorporate new win7 code signing technique
https://git.zx2c4.com/downlevel-driver-enabler/about/ Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent 8967516 commit ef9d08b

File tree

7 files changed

+160
-4
lines changed

7 files changed

+160
-4
lines changed

api/adapter.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,8 @@ SelectDriver(
11261126
WCHAR CatPath[MAX_PATH] = { 0 };
11271127
WCHAR SysPath[MAX_PATH] = { 0 };
11281128
WCHAR InfPath[MAX_PATH] = { 0 };
1129+
WCHAR DownlevelShimPath[MAX_PATH] = { 0 };
1130+
11291131
if (!PathCombineW(CatPath, RandomTempSubDirectory, L"wintun.cat") ||
11301132
!PathCombineW(SysPath, RandomTempSubDirectory, L"wintun.sys") ||
11311133
!PathCombineW(InfPath, RandomTempSubDirectory, L"wintun.inf"))
@@ -1141,6 +1143,53 @@ SelectDriver(
11411143
LastError = LOG_LAST_ERROR(L"Failed to extract driver");
11421144
goto cleanupDelete;
11431145
}
1146+
1147+
WCHAR *WintrustKeyOriginalValue = NULL;
1148+
HKEY WintrustKey = NULL;
1149+
if (!IsWindows10)
1150+
{
1151+
LOG(WINTUN_LOG_INFO, L"Shimming downlevel driver loader");
1152+
if (!PathCombineW(DownlevelShimPath, RandomTempSubDirectory, L"downlevelshim.dll"))
1153+
{
1154+
DownlevelShimPath[0] = L'\0';
1155+
LastError = ERROR_BUFFER_OVERFLOW;
1156+
goto cleanupDelete;
1157+
}
1158+
if (!ResourceCopyToFile(DownlevelShimPath, L"downlevelshim.dll"))
1159+
{
1160+
LastError = LOG_LAST_ERROR(L"Failed to extract downlevel shim");
1161+
goto cleanupDelete;
1162+
}
1163+
LastError = RegOpenKeyExW(
1164+
HKEY_LOCAL_MACHINE,
1165+
L"SOFTWARE\\Microsoft\\Cryptography\\Providers\\Trust\\FinalPolicy\\{F750E6C3-38EE-11D1-85E5-00C04FC295EE}",
1166+
0,
1167+
KEY_QUERY_VALUE | KEY_SET_VALUE,
1168+
&WintrustKey);
1169+
if (LastError != ERROR_SUCCESS)
1170+
{
1171+
LOG_ERROR(LastError, L"Failed to open Wintrust FinalPolicy key");
1172+
goto cleanupDelete;
1173+
}
1174+
WintrustKeyOriginalValue = RegistryQueryString(WintrustKey, L"$DLL", TRUE);
1175+
if (!WintrustKeyOriginalValue)
1176+
{
1177+
LastError = LOG_LAST_ERROR(L"Failed to read current Wintrust FinalPolicy key");
1178+
goto cleanupWintrustKey;
1179+
}
1180+
LastError = RegSetValueExW(
1181+
WintrustKey,
1182+
L"$DLL",
1183+
0,
1184+
REG_SZ,
1185+
(BYTE *)DownlevelShimPath,
1186+
(DWORD)((wcslen(DownlevelShimPath) + 1) * sizeof(DownlevelShimPath[0])));
1187+
if (LastError != ERROR_SUCCESS)
1188+
{
1189+
LOG_ERROR(LastError, L"Failed to set Wintrust FinalPolicy key");
1190+
goto cleanupWintrustChangedKey;
1191+
}
1192+
}
11441193
LOG(WINTUN_LOG_INFO, L"Installing driver");
11451194
WCHAR InfStorePath[MAX_PATH];
11461195
if (!SetupCopyOEMInfW(InfPath, NULL, SPOST_NONE, 0, InfStorePath, MAX_PATH, NULL, NULL))
@@ -1185,10 +1234,26 @@ SelectDriver(
11851234
LastError = ERROR_SUCCESS;
11861235
DestroyDriverInfoListOnCleanup = FALSE;
11871236

1237+
cleanupWintrustChangedKey:
1238+
if (WintrustKeyOriginalValue)
1239+
RegSetValueExW(
1240+
WintrustKey,
1241+
L"$DLL",
1242+
0,
1243+
REG_SZ,
1244+
(BYTE *)WintrustKeyOriginalValue,
1245+
(DWORD)((wcslen(WintrustKeyOriginalValue + 1) * sizeof(WintrustKeyOriginalValue[0]))));
1246+
cleanupWintrustKey:
1247+
if (WintrustKey)
1248+
RegCloseKey(WintrustKey);
1249+
if (WintrustKeyOriginalValue)
1250+
Free(WintrustKeyOriginalValue);
11881251
cleanupDelete:
11891252
DeleteFileW(CatPath);
11901253
DeleteFileW(SysPath);
11911254
DeleteFileW(InfPath);
1255+
if (DownlevelShimPath[0])
1256+
DeleteFileW(DownlevelShimPath);
11921257
cleanupDirectory:
11931258
RemoveDirectoryW(RandomTempSubDirectory);
11941259
cleanupExistingAdapters:

api/resources.rc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ wintun.cat RCDATA "driver\\wintun.cat"
1212
wintun.inf RCDATA "driver\\wintun.inf"
1313
wintun.sys RCDATA "driver\\wintun.sys"
1414

15+
downlevelshim.dll RCDATA "downlevelshim.dll"
16+
1517
#if defined(WANT_AMD64_WOW64)
1618
# if defined(BUILT_AMD64_WOW64)
1719
wintun-amd64.dll RCDATA "amd64\\wintun.dll"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<ProjectGuid>{6E8213E6-5046-4DE8-A760-0932C7D6E33E}</ProjectGuid>
5+
<RootNamespace>downlevelshim</RootNamespace>
6+
<ProjectName>downlevelshim</ProjectName>
7+
</PropertyGroup>
8+
<PropertyGroup Label="Configuration">
9+
<ConfigurationType>DynamicLibrary</ConfigurationType>
10+
<PlatformToolset>WindowsApplicationForDrivers10.0</PlatformToolset>
11+
</PropertyGroup>
12+
<Import Project="..\wintun.props" />
13+
<PropertyGroup>
14+
<TargetName>downlevelshim</TargetName>
15+
</PropertyGroup>
16+
<ItemDefinitionGroup>
17+
<ClCompile>
18+
<PreprocessorDefinitions>_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
19+
<AdditionalOptions>/volatile:iso %(AdditionalOptions)</AdditionalOptions>
20+
</ClCompile>
21+
<Link>
22+
<ModuleDefinitionFile>exports.def</ModuleDefinitionFile>
23+
<SubSystem>Windows</SubSystem>
24+
</Link>
25+
</ItemDefinitionGroup>
26+
<ItemGroup>
27+
<None Include="exports.def" />
28+
</ItemGroup>
29+
<ItemGroup>
30+
<ClCompile Include="shim.c" />
31+
</ItemGroup>
32+
<Import Project="..\wintun.props.user" Condition="exists('..\wintun.props.user')" />
33+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
34+
<ImportGroup Label="ExtensionTargets" />
35+
</Project>

downlevelshim/exports.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
LIBRARY downlevelshim.dll
2+
EXPORTS
3+
DriverFinalPolicy

downlevelshim/shim.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* SPDX-License-Identifier: GPL-2.0
2+
*
3+
* Copyright (C) 2018-2021 WireGuard LLC. All Rights Reserved.
4+
*/
5+
6+
#include <windows.h>
7+
#include <wintrust.h>
8+
9+
typedef DWORD(DRIVER_FINAL_POLICY_FN)(CRYPT_PROVIDER_DATA *);
10+
typedef DRIVER_FINAL_POLICY_FN *PDRIVER_FINAL_POLICY_FN;
11+
12+
DRIVER_FINAL_POLICY_FN DriverFinalPolicy;
13+
14+
DWORD
15+
DriverFinalPolicy(CRYPT_PROVIDER_DATA *ProvData)
16+
{
17+
DWORD OriginalLastError = GetLastError();
18+
HMODULE WintrustModule = GetModuleHandleA("WINTRUST.DLL");
19+
if (!WintrustModule)
20+
return ERROR_INVALID_LIBRARY;
21+
PDRIVER_FINAL_POLICY_FN RealDriverFinalPolicy =
22+
(PDRIVER_FINAL_POLICY_FN)GetProcAddress(WintrustModule, "DriverFinalPolicy");
23+
if (!RealDriverFinalPolicy)
24+
return ERROR_INVALID_FUNCTION;
25+
DWORD Ret = RealDriverFinalPolicy(ProvData);
26+
if (Ret == ERROR_APP_WRONG_OS)
27+
{
28+
Ret = ERROR_SUCCESS;
29+
SetLastError(OriginalLastError);
30+
}
31+
return Ret;
32+
}

wintun.proj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,21 @@
8080
<Target Name="Dll-x86"
8181
Outputs="$(Configuration)\x86\wintun.dll"
8282
DependsOnTargets="Dll-amd64;Dll-arm64">
83-
<MSBuild Projects="api\api.vcxproj" Targets="Build" Properties="Configuration=$(Configuration);Platform=Win32" />
83+
<MSBuild Projects="downlevelshim\downlevelshim.vcxproj;api\api.vcxproj" Targets="Build" Properties="Configuration=$(Configuration);Platform=Win32" />
8484
</Target>
8585
<Target Name="Dll-amd64"
8686
Outputs="$(Configuration)\amd64\wintun.dll"
8787
DependsOnTargets="Dll-arm64">
88-
<MSBuild Projects="api\api.vcxproj" Targets="Build" Properties="Configuration=$(Configuration);Platform=x64" />
88+
<MSBuild Projects="downlevelshim\downlevelshim.vcxproj;api\api.vcxproj" Targets="Build" Properties="Configuration=$(Configuration);Platform=x64" />
8989
</Target>
9090
<Target Name="Dll-arm"
9191
Outputs="$(Configuration)\arm\wintun.dll"
9292
DependsOnTargets="Dll-arm64">
93-
<MSBuild Projects="api\api.vcxproj" Targets="Build" Properties="Configuration=$(Configuration);Platform=ARM" />
93+
<MSBuild Projects="downlevelshim\downlevelshim.vcxproj;api\api.vcxproj" Targets="Build" Properties="Configuration=$(Configuration);Platform=ARM" />
9494
</Target>
9595
<Target Name="Dll-arm64"
9696
Outputs="$(Configuration)\arm64\wintun.dll">
97-
<MSBuild Projects="api\api.vcxproj" Targets="Build" Properties="Configuration=$(Configuration);Platform=ARM64" />
97+
<MSBuild Projects="downlevelshim\downlevelshim.vcxproj;api\api.vcxproj" Targets="Build" Properties="Configuration=$(Configuration);Platform=ARM64" />
9898
</Target>
9999

100100
<!--

wintun.sln

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ EndProject
77
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "api", "api\api.vcxproj", "{897F02E3-3EAA-40AF-A6DC-17EB2376EDAF}"
88
ProjectSection(ProjectDependencies) = postProject
99
{F7679B65-2FEC-469A-8BAC-B07BF4439422} = {F7679B65-2FEC-469A-8BAC-B07BF4439422}
10+
{6E8213E6-5046-4DE8-A760-0932C7D6E33E} = {6E8213E6-5046-4DE8-A760-0932C7D6E33E}
1011
EndProjectSection
1112
EndProject
1213
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "driver", "driver\driver.vcxproj", "{F7679B65-2FEC-469A-8BAC-B07BF4439422}"
1314
EndProject
15+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "downlevelshim", "downlevelshim\downlevelshim.vcxproj", "{6E8213E6-5046-4DE8-A760-0932C7D6E33E}"
16+
EndProject
1417
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3A98F138-EE02-4488-B856-B3C48500BEA8}"
1518
ProjectSection(SolutionItems) = preProject
1619
README.md = README.md
@@ -78,6 +81,22 @@ Global
7881
{F7679B65-2FEC-469A-8BAC-B07BF4439422}.Release|arm64.Build.0 = Release|ARM64
7982
{F7679B65-2FEC-469A-8BAC-B07BF4439422}.Release|x86.ActiveCfg = Release|Win32
8083
{F7679B65-2FEC-469A-8BAC-B07BF4439422}.Release|x86.Build.0 = Release|Win32
84+
{6E8213E6-5046-4DE8-A760-0932C7D6E33E}.Debug|amd64.ActiveCfg = Debug|x64
85+
{6E8213E6-5046-4DE8-A760-0932C7D6E33E}.Debug|amd64.Build.0 = Debug|x64
86+
{6E8213E6-5046-4DE8-A760-0932C7D6E33E}.Debug|arm.ActiveCfg = Debug|ARM
87+
{6E8213E6-5046-4DE8-A760-0932C7D6E33E}.Debug|arm.Build.0 = Debug|ARM
88+
{6E8213E6-5046-4DE8-A760-0932C7D6E33E}.Debug|arm64.ActiveCfg = Debug|ARM64
89+
{6E8213E6-5046-4DE8-A760-0932C7D6E33E}.Debug|arm64.Build.0 = Debug|ARM64
90+
{6E8213E6-5046-4DE8-A760-0932C7D6E33E}.Debug|x86.ActiveCfg = Debug|Win32
91+
{6E8213E6-5046-4DE8-A760-0932C7D6E33E}.Debug|x86.Build.0 = Debug|Win32
92+
{6E8213E6-5046-4DE8-A760-0932C7D6E33E}.Release|amd64.ActiveCfg = Release|x64
93+
{6E8213E6-5046-4DE8-A760-0932C7D6E33E}.Release|amd64.Build.0 = Release|x64
94+
{6E8213E6-5046-4DE8-A760-0932C7D6E33E}.Release|arm.ActiveCfg = Release|ARM
95+
{6E8213E6-5046-4DE8-A760-0932C7D6E33E}.Release|arm.Build.0 = Release|ARM
96+
{6E8213E6-5046-4DE8-A760-0932C7D6E33E}.Release|arm64.ActiveCfg = Release|ARM64
97+
{6E8213E6-5046-4DE8-A760-0932C7D6E33E}.Release|arm64.Build.0 = Release|ARM64
98+
{6E8213E6-5046-4DE8-A760-0932C7D6E33E}.Release|x86.ActiveCfg = Release|Win32
99+
{6E8213E6-5046-4DE8-A760-0932C7D6E33E}.Release|x86.Build.0 = Release|Win32
81100
EndGlobalSection
82101
GlobalSection(SolutionProperties) = preSolution
83102
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)