diff options
author | Simon Rozman <simon@rozman.si> | 2020-10-14 08:29:01 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-10-30 16:51:00 +0100 |
commit | 84113bd294141742fbc5f10e645e74c395e35217 (patch) | |
tree | 36d3b90ba2391290655600c675575f693dea09a3 | |
parent | api: unify hardware ID string (diff) | |
download | wintun-84113bd294141742fbc5f10e645e74c395e35217.tar.xz wintun-84113bd294141742fbc5f10e645e74c395e35217.zip |
api: unify and document resource loading
Signed-off-by: Simon Rozman <simon@rozman.si>
-rw-r--r-- | api/driver.c | 29 | ||||
-rw-r--r-- | api/resource.c | 50 | ||||
-rw-r--r-- | api/resource.h | 5 |
3 files changed, 54 insertions, 30 deletions
diff --git a/api/driver.c b/api/driver.c index 0a4c2ca..a65c0a9 100644 --- a/api/driver.c +++ b/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"); diff --git a/api/resource.c b/api/resource.c index b25b96d..4c5be64 100644 --- a/api/resource.c +++ b/api/resource.c @@ -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) diff --git a/api/resource.h b/api/resource.h index 3693bbf..2ee2547 100644 --- a/api/resource.h +++ b/api/resource.h @@ -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); |