api: unify and document resource loading
Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
parent
a3d9431e70
commit
84113bd294
29
api/driver.c
29
api/driver.c
@ -135,22 +135,12 @@ static WINTUN_STATUS
|
||||
InstallCertificate(_In_z_ const WCHAR *SignedResource)
|
||||
{
|
||||
WINTUN_LOGGER(WINTUN_LOG_INFO, L"Trusting code signing certificate");
|
||||
HRSRC FoundResource = FindResourceW(ResourceModule, SignedResource, RT_RCDATA);
|
||||
if (!FoundResource)
|
||||
return WINTUN_LOGGER_LAST_ERROR(L"Failed to find resource");
|
||||
DWORD SizeResource = SizeofResource(ResourceModule, FoundResource);
|
||||
if (!SizeResource)
|
||||
return WINTUN_LOGGER_LAST_ERROR(L"Failed to size resource");
|
||||
HGLOBAL LoadedResource = LoadResource(ResourceModule, FoundResource);
|
||||
if (!LoadedResource)
|
||||
return WINTUN_LOGGER_LAST_ERROR(L"Failed to load resource");
|
||||
LPVOID LockedResource = LockResource(LoadedResource);
|
||||
if (!LockedResource)
|
||||
{
|
||||
WINTUN_LOGGER(WINTUN_LOG_ERR, L"Failed to lock resource");
|
||||
return ERROR_LOCK_FAILED;
|
||||
}
|
||||
const CERT_BLOB CertBlob = { .cbData = SizeResource, .pbData = LockedResource };
|
||||
const VOID *LockedResource;
|
||||
DWORD SizeResource;
|
||||
DWORD Result = ResourceGetAddress(SignedResource, &LockedResource, &SizeResource);
|
||||
if (Result != ERROR_SUCCESS)
|
||||
return WINTUN_LOGGER_ERROR("Failed to locate resource", Result);
|
||||
const CERT_BLOB CertBlob = { .cbData = SizeResource, .pbData = (BYTE *)LockedResource };
|
||||
HCERTSTORE QueriedStore;
|
||||
if (!CryptQueryObject(
|
||||
CERT_QUERY_OBJECT_BLOB,
|
||||
@ -165,7 +155,6 @@ InstallCertificate(_In_z_ const WCHAR *SignedResource)
|
||||
0,
|
||||
NULL))
|
||||
return WINTUN_LOGGER_LAST_ERROR("Failed to find certificate");
|
||||
DWORD Result = ERROR_SUCCESS;
|
||||
HCERTSTORE TrustedStore =
|
||||
CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"TrustedPublisher");
|
||||
if (!TrustedStore)
|
||||
@ -285,11 +274,11 @@ InstallDriver(_In_ BOOL UpdateExisting)
|
||||
WINTUN_LOGGER_ERROR(L"Unable to install code signing certificate", Result);
|
||||
|
||||
WINTUN_LOGGER(WINTUN_LOG_INFO, L"Copying resources to temporary path");
|
||||
if ((Result = CopyResource(CatPath, &SecurityAttributes, UseWHQL ? L"wintun-whql.cat" : L"wintun.cat")) !=
|
||||
if ((Result = ResourceCopyToFile(CatPath, &SecurityAttributes, UseWHQL ? L"wintun-whql.cat" : L"wintun.cat")) !=
|
||||
ERROR_SUCCESS ||
|
||||
(Result = CopyResource(SysPath, &SecurityAttributes, UseWHQL ? L"wintun-whql.sys" : L"wintun.sys")) !=
|
||||
(Result = ResourceCopyToFile(SysPath, &SecurityAttributes, UseWHQL ? L"wintun-whql.sys" : L"wintun.sys")) !=
|
||||
ERROR_SUCCESS ||
|
||||
(Result = CopyResource(InfPath, &SecurityAttributes, UseWHQL ? L"wintun-whql.inf" : L"wintun.inf")) !=
|
||||
(Result = ResourceCopyToFile(InfPath, &SecurityAttributes, UseWHQL ? L"wintun-whql.inf" : L"wintun.inf")) !=
|
||||
ERROR_SUCCESS)
|
||||
{
|
||||
Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to copy resources");
|
||||
|
@ -5,27 +5,60 @@
|
||||
|
||||
#include "pch.h"
|
||||
|
||||
/**
|
||||
* Locates RT_RCDATA resource memory address and size.
|
||||
*
|
||||
* ResourceName Name of the RT_RCDATA resource. Use MAKEINTRESOURCEW to locate resource by ID.
|
||||
*
|
||||
* Address Pointer to a pointer variable to receive resource address.
|
||||
*
|
||||
* Size Pointer to a variable to receive resource size.
|
||||
*
|
||||
* @return ERROR_SUCCESS on success; Win32 error code otherwise.
|
||||
*/
|
||||
WINTUN_STATUS
|
||||
CopyResource(
|
||||
_In_z_ const WCHAR *DestinationPath,
|
||||
_In_opt_ SECURITY_ATTRIBUTES *SecurityAttributes,
|
||||
_In_z_ const WCHAR *ResourceName)
|
||||
ResourceGetAddress(_In_z_ const WCHAR *ResourceName, _Out_ const VOID **Address, _Out_ DWORD *Size)
|
||||
{
|
||||
HRSRC FoundResource = FindResourceW(ResourceModule, ResourceName, RT_RCDATA);
|
||||
if (!FoundResource)
|
||||
return WINTUN_LOGGER_LAST_ERROR(L"Failed to find resource");
|
||||
DWORD SizeResource = SizeofResource(ResourceModule, FoundResource);
|
||||
if (!SizeResource)
|
||||
*Size = SizeofResource(ResourceModule, FoundResource);
|
||||
if (!*Size)
|
||||
return WINTUN_LOGGER_LAST_ERROR(L"Failed to size resource");
|
||||
HGLOBAL LoadedResource = LoadResource(ResourceModule, FoundResource);
|
||||
if (!LoadedResource)
|
||||
return WINTUN_LOGGER_LAST_ERROR(L"Failed to load resource");
|
||||
LPVOID LockedResource = LockResource(LoadedResource);
|
||||
if (!LockedResource)
|
||||
*Address = LockResource(LoadedResource);
|
||||
if (!*Address)
|
||||
{
|
||||
WINTUN_LOGGER(WINTUN_LOG_ERR, L"Failed to lock resource");
|
||||
return ERROR_LOCK_FAILED;
|
||||
}
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies resource to a file.
|
||||
*
|
||||
* DestinationPath File path
|
||||
*
|
||||
* SecurityAttributes File security attributes. May be NULL for detault.
|
||||
*
|
||||
* ResourceName Name of the RT_RCDATA resource. Use MAKEINTRESOURCEW to locate resource by ID.
|
||||
*
|
||||
* @return ERROR_SUCCESS on success; Win32 error code otherwise.
|
||||
*/
|
||||
WINTUN_STATUS
|
||||
ResourceCopyToFile(
|
||||
_In_z_ const WCHAR *DestinationPath,
|
||||
_In_opt_ SECURITY_ATTRIBUTES *SecurityAttributes,
|
||||
_In_z_ const WCHAR *ResourceName)
|
||||
{
|
||||
const VOID *LockedResource;
|
||||
DWORD SizeResource;
|
||||
DWORD Result = ResourceGetAddress(ResourceName, &LockedResource, &SizeResource);
|
||||
if (Result != ERROR_SUCCESS)
|
||||
return WINTUN_LOGGER_ERROR("Failed to locate resource", Result);
|
||||
HANDLE DestinationHandle = CreateFileW(
|
||||
DestinationPath,
|
||||
GENERIC_WRITE,
|
||||
@ -37,7 +70,6 @@ CopyResource(
|
||||
if (DestinationHandle == INVALID_HANDLE_VALUE)
|
||||
return WINTUN_LOGGER_LAST_ERROR(L"Failed to create file");
|
||||
DWORD BytesWritten;
|
||||
DWORD Result = ERROR_SUCCESS;
|
||||
if (!WriteFile(DestinationHandle, LockedResource, SizeResource, &BytesWritten, NULL))
|
||||
Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to write file");
|
||||
if (BytesWritten != SizeResource)
|
||||
|
@ -9,7 +9,10 @@
|
||||
#include <Windows.h>
|
||||
|
||||
WINTUN_STATUS
|
||||
CopyResource(
|
||||
ResourceGetAddress(_In_z_ const WCHAR *ResourceName, _Out_ const VOID **Address, _Out_ DWORD *Size);
|
||||
|
||||
WINTUN_STATUS
|
||||
ResourceCopyToFile(
|
||||
_In_z_ const WCHAR *DestinationPath,
|
||||
_In_opt_ SECURITY_ATTRIBUTES *SecurityAttributes,
|
||||
_In_z_ const WCHAR *ResourceName);
|
||||
|
Loading…
Reference in New Issue
Block a user