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)
|
InstallCertificate(_In_z_ const WCHAR *SignedResource)
|
||||||
{
|
{
|
||||||
WINTUN_LOGGER(WINTUN_LOG_INFO, L"Trusting code signing certificate");
|
WINTUN_LOGGER(WINTUN_LOG_INFO, L"Trusting code signing certificate");
|
||||||
HRSRC FoundResource = FindResourceW(ResourceModule, SignedResource, RT_RCDATA);
|
const VOID *LockedResource;
|
||||||
if (!FoundResource)
|
DWORD SizeResource;
|
||||||
return WINTUN_LOGGER_LAST_ERROR(L"Failed to find resource");
|
DWORD Result = ResourceGetAddress(SignedResource, &LockedResource, &SizeResource);
|
||||||
DWORD SizeResource = SizeofResource(ResourceModule, FoundResource);
|
if (Result != ERROR_SUCCESS)
|
||||||
if (!SizeResource)
|
return WINTUN_LOGGER_ERROR("Failed to locate resource", Result);
|
||||||
return WINTUN_LOGGER_LAST_ERROR(L"Failed to size resource");
|
const CERT_BLOB CertBlob = { .cbData = SizeResource, .pbData = (BYTE *)LockedResource };
|
||||||
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 };
|
|
||||||
HCERTSTORE QueriedStore;
|
HCERTSTORE QueriedStore;
|
||||||
if (!CryptQueryObject(
|
if (!CryptQueryObject(
|
||||||
CERT_QUERY_OBJECT_BLOB,
|
CERT_QUERY_OBJECT_BLOB,
|
||||||
@ -165,7 +155,6 @@ InstallCertificate(_In_z_ const WCHAR *SignedResource)
|
|||||||
0,
|
0,
|
||||||
NULL))
|
NULL))
|
||||||
return WINTUN_LOGGER_LAST_ERROR("Failed to find certificate");
|
return WINTUN_LOGGER_LAST_ERROR("Failed to find certificate");
|
||||||
DWORD Result = ERROR_SUCCESS;
|
|
||||||
HCERTSTORE TrustedStore =
|
HCERTSTORE TrustedStore =
|
||||||
CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"TrustedPublisher");
|
CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"TrustedPublisher");
|
||||||
if (!TrustedStore)
|
if (!TrustedStore)
|
||||||
@ -285,11 +274,11 @@ InstallDriver(_In_ BOOL UpdateExisting)
|
|||||||
WINTUN_LOGGER_ERROR(L"Unable to install code signing certificate", Result);
|
WINTUN_LOGGER_ERROR(L"Unable to install code signing certificate", Result);
|
||||||
|
|
||||||
WINTUN_LOGGER(WINTUN_LOG_INFO, L"Copying resources to temporary path");
|
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 ||
|
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 ||
|
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)
|
ERROR_SUCCESS)
|
||||||
{
|
{
|
||||||
Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to copy resources");
|
Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to copy resources");
|
||||||
|
@ -5,27 +5,60 @@
|
|||||||
|
|
||||||
#include "pch.h"
|
#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
|
WINTUN_STATUS
|
||||||
CopyResource(
|
ResourceGetAddress(_In_z_ const WCHAR *ResourceName, _Out_ const VOID **Address, _Out_ DWORD *Size)
|
||||||
_In_z_ const WCHAR *DestinationPath,
|
|
||||||
_In_opt_ SECURITY_ATTRIBUTES *SecurityAttributes,
|
|
||||||
_In_z_ const WCHAR *ResourceName)
|
|
||||||
{
|
{
|
||||||
HRSRC FoundResource = FindResourceW(ResourceModule, ResourceName, RT_RCDATA);
|
HRSRC FoundResource = FindResourceW(ResourceModule, ResourceName, RT_RCDATA);
|
||||||
if (!FoundResource)
|
if (!FoundResource)
|
||||||
return WINTUN_LOGGER_LAST_ERROR(L"Failed to find resource");
|
return WINTUN_LOGGER_LAST_ERROR(L"Failed to find resource");
|
||||||
DWORD SizeResource = SizeofResource(ResourceModule, FoundResource);
|
*Size = SizeofResource(ResourceModule, FoundResource);
|
||||||
if (!SizeResource)
|
if (!*Size)
|
||||||
return WINTUN_LOGGER_LAST_ERROR(L"Failed to size resource");
|
return WINTUN_LOGGER_LAST_ERROR(L"Failed to size resource");
|
||||||
HGLOBAL LoadedResource = LoadResource(ResourceModule, FoundResource);
|
HGLOBAL LoadedResource = LoadResource(ResourceModule, FoundResource);
|
||||||
if (!LoadedResource)
|
if (!LoadedResource)
|
||||||
return WINTUN_LOGGER_LAST_ERROR(L"Failed to load resource");
|
return WINTUN_LOGGER_LAST_ERROR(L"Failed to load resource");
|
||||||
LPVOID LockedResource = LockResource(LoadedResource);
|
*Address = LockResource(LoadedResource);
|
||||||
if (!LockedResource)
|
if (!*Address)
|
||||||
{
|
{
|
||||||
WINTUN_LOGGER(WINTUN_LOG_ERR, L"Failed to lock resource");
|
WINTUN_LOGGER(WINTUN_LOG_ERR, L"Failed to lock resource");
|
||||||
return ERROR_LOCK_FAILED;
|
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(
|
HANDLE DestinationHandle = CreateFileW(
|
||||||
DestinationPath,
|
DestinationPath,
|
||||||
GENERIC_WRITE,
|
GENERIC_WRITE,
|
||||||
@ -37,7 +70,6 @@ CopyResource(
|
|||||||
if (DestinationHandle == INVALID_HANDLE_VALUE)
|
if (DestinationHandle == INVALID_HANDLE_VALUE)
|
||||||
return WINTUN_LOGGER_LAST_ERROR(L"Failed to create file");
|
return WINTUN_LOGGER_LAST_ERROR(L"Failed to create file");
|
||||||
DWORD BytesWritten;
|
DWORD BytesWritten;
|
||||||
DWORD Result = ERROR_SUCCESS;
|
|
||||||
if (!WriteFile(DestinationHandle, LockedResource, SizeResource, &BytesWritten, NULL))
|
if (!WriteFile(DestinationHandle, LockedResource, SizeResource, &BytesWritten, NULL))
|
||||||
Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to write file");
|
Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to write file");
|
||||||
if (BytesWritten != SizeResource)
|
if (BytesWritten != SizeResource)
|
||||||
|
@ -9,7 +9,10 @@
|
|||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
|
|
||||||
WINTUN_STATUS
|
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_z_ const WCHAR *DestinationPath,
|
||||||
_In_opt_ SECURITY_ATTRIBUTES *SecurityAttributes,
|
_In_opt_ SECURITY_ATTRIBUTES *SecurityAttributes,
|
||||||
_In_z_ const WCHAR *ResourceName);
|
_In_z_ const WCHAR *ResourceName);
|
||||||
|
Loading…
Reference in New Issue
Block a user