Skip to content
This repository has been archived by the owner on Feb 28, 2019. It is now read-only.

Improve buildscript and fix Windows 10 Creator update problems #6

Open
wants to merge 2 commits into
base: jov-pull-requests
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions u2f-tests/BLE/BleApi/BleApiWinRT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <collection.h>
#include <comdef.h>

#include <sddl.h>

using namespace Platform;
using namespace Platform::Collections;
using namespace Concurrency;
Expand Down Expand Up @@ -56,10 +58,150 @@ inline std::runtime_error hresult_exception(std::string file, int line, HRESULT
#define STRING_RUNTIME_EXCEPTION(x) std::runtime_error( __FILE__ ":" + std::to_string(__LINE__) + ": " + x)
#define CX_EXCEPTION(x) HRESULT_RUNTIME_EXCEPTION(x->HResult)

// From: https://github.com/pauldotknopf/WindowsSDK7-Samples/blob/master/com/fundamentals/dcom/dcomperm/SDMgmt.Cpp
DWORD MakeSDAbsolute(
PSECURITY_DESCRIPTOR psidOld,
PSECURITY_DESCRIPTOR *psidNew
)
{
PSECURITY_DESCRIPTOR pSid = NULL;
DWORD cbDescriptor = 0;
DWORD cbDacl = 0;
DWORD cbSacl = 0;
DWORD cbOwnerSID = 0;
DWORD cbGroupSID = 0;
PACL pDacl = NULL;
PACL pSacl = NULL;
PSID psidOwner = NULL;
PSID psidGroup = NULL;
BOOL fPresent = FALSE;
BOOL fSystemDefault = FALSE;
DWORD dwReturnValue = ERROR_SUCCESS;

// Get SACL
if (!GetSecurityDescriptorSacl(psidOld, &fPresent, &pSacl, &fSystemDefault))
{
dwReturnValue = GetLastError();
goto CLEANUP;
}

if (pSacl && fPresent)
{
cbSacl = pSacl->AclSize;
}

// Get DACL
if (!GetSecurityDescriptorDacl(psidOld, &fPresent, &pDacl, &fSystemDefault))
{
dwReturnValue = GetLastError();
goto CLEANUP;
}

if (pDacl && fPresent)
{
cbDacl = pDacl->AclSize;
}

// Get Owner
if (!GetSecurityDescriptorOwner(psidOld, &psidOwner, &fSystemDefault))
{
dwReturnValue = GetLastError();
goto CLEANUP;
}

cbOwnerSID = GetLengthSid(psidOwner);

// Get Group
if (!GetSecurityDescriptorGroup(psidOld, &psidGroup, &fSystemDefault))
{
dwReturnValue = GetLastError();
goto CLEANUP;
}

cbGroupSID = GetLengthSid(psidGroup);

// Do the conversion
cbDescriptor = 0;

MakeAbsoluteSD(psidOld, pSid, &cbDescriptor, pDacl, &cbDacl, pSacl,
&cbSacl, psidOwner, &cbOwnerSID, psidGroup,
&cbGroupSID);

pSid = (PSECURITY_DESCRIPTOR)malloc(cbDescriptor);
if (!pSid)
{
dwReturnValue = ERROR_OUTOFMEMORY;
goto CLEANUP;
}

ZeroMemory(pSid, cbDescriptor);

if (!InitializeSecurityDescriptor(pSid, SECURITY_DESCRIPTOR_REVISION))
{
dwReturnValue = GetLastError();
goto CLEANUP;
}

if (!MakeAbsoluteSD(psidOld, pSid, &cbDescriptor, pDacl, &cbDacl, pSacl,
&cbSacl, psidOwner, &cbOwnerSID, psidGroup,
&cbGroupSID))
{
dwReturnValue = GetLastError();
goto CLEANUP;
}

CLEANUP:

if (dwReturnValue != ERROR_SUCCESS && pSid)
{
free(pSid);
pSid = NULL;
}

*psidNew = pSid;

return dwReturnValue;
}


BleApiWinRT::BleApiWinRT(BleApiConfiguration &configuration)
: BleApi(configuration)
{
RoInitialize(RO_INIT_TYPE::RO_INIT_MULTITHREADED);

const char* security = "O:BAG:BAD:(A;;0x7;;;PS)(A;;0x3;;;SY)(A;;0x7;;;BA)(A;;0x3;;;AC)(A;;0x3;;;LS)(A;;0x3;;;NS)";

PSECURITY_DESCRIPTOR pSecurityDescriptor;
ULONG securityDescriptorSize;

if (!ConvertStringSecurityDescriptorToSecurityDescriptor(
security,
SDDL_REVISION_1,
&pSecurityDescriptor,
&securityDescriptorSize))
{
throw STRING_RUNTIME_EXCEPTION("ConvertStringSecurityDescriptorToSecurityDescriptor failed.");
}

// MakeSDAbsolute as defined in
// https://github.com/pauldotknopf/WindowsSDK7-Samples/blob/master/com/fundamentals/dcom/dcomperm/SDMgmt.Cpp
PSECURITY_DESCRIPTOR pAbsoluteSecurityDescriptor = NULL;
MakeSDAbsolute(pSecurityDescriptor, &pAbsoluteSecurityDescriptor);

HRESULT hResult = CoInitializeSecurity(
pAbsoluteSecurityDescriptor, // Converted from the above string.
-1,
nullptr,
nullptr,
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IDENTIFY,
NULL,
EOAC_NONE,
nullptr);
if (FAILED(hResult))
{
throw HRESULT_RUNTIME_EXCEPTION(hResult);
}
}

BleApiWinRT::~BleApiWinRT(void)
Expand Down
4 changes: 4 additions & 0 deletions u2f-tests/BLE/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

U2F V1.1:

1.2.1:
* Workaround for Windows 10 Creators update bug.
* Improve detection of Windows 10 Kits directories.

1.2.0:
* add 1.2 version bit.

Expand Down
19 changes: 14 additions & 5 deletions u2f-tests/BLE/Makefile.win
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ ARCHIVENAME=BLECertTool
7ZIP=C:\Program Files\7-Zip\7z.exe
FEATURE_WINRT=1

!IF [WindowsKit.bat >WindowsKit.inc]
!ELSE
!ERROR WindowsKit.bat failed.
!ENDIF
!INCLUDE WindowsKit.inc

VS_PATH=C:/Program Files (x86)/Microsoft Visual Studio 14.0
WINSDK_PATH=C:/Program Files (x86)/Windows Kits/10
!IFNDEF WINDOWS_SDK
WINDOWS_SDK = C:/Program Files (x86)/Windows Kits/10
WINDOWS_SDK_PATHS = $(WINDOWS_SDK_PATHS) -AI"C:/Program Files (x86)/Windows Kits/10/References/" -AI"C:/Program Files (x86)/Windows Kits/10/UnionMetaData/"
!ENDIF

MSSDK=$(WINSDK_PATH)
MSTOOLS=$(WINSDK_PATH)
MSSDK=$(WINDOWS_SDK)
MSTOOLS=$(WINDOWS_SDK)

!INCLUDE VERSION

Expand All @@ -31,12 +40,12 @@ CFLAGS = $(CFLAGS) -Gz
#
!IFDEF FEATURE_WINRT
CFLAGS = $(CFLAGS) -DFEATURE_WINRT
CFLAGS_WINRT = -ZW -Gm- -AI"C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/vcpackages" -AI"C:/Program Files (x86)/Windows Kits/10/References/" -AI"C:/Program Files (x86)/Windows Kits/10/UnionMetaData/"
CFLAGS_WINRT = -ZW -Gm- -AI"C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/vcpackages" $(WINDOWS_SDK_PATHS)
!ELSE
CFLAGS_WINRT =
!ENDIF

LDFLAGS = setupapi.lib ws2_32.lib
LDFLAGS = setupapi.lib ws2_32.lib Advapi32.lib

#
## Crypto code.
Expand Down
2 changes: 1 addition & 1 deletion u2f-tests/BLE/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION=1.2.0
VERSION=1.2.1
35 changes: 35 additions & 0 deletions u2f-tests/BLE/WindowsKit.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@echo OFF

::
:: This script extract the Windows Kit path from the registry.
::

setlocal ENABLEEXTENSIONS
set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Kits\Installed Roots"
set VALUE_NAME=KitsRoot10

FOR /F "usebackq skip=2 tokens=1-2*" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
set ValueName=%%A
set ValueType=%%B
set ValueValue=%%C
)

@echo WINDOWS_SDK = %ValueValue:\=/%
set found=0
IF EXIST "%ValueValue%UnionMetaData\Windows.winmd" (
set RefPath=-AI"%ValueValue:\=/%References/"
set UMDPath=-AI"%ValueValue:\=/%UnionMetaData/"
set found=1
) ELSE (
FOR /F "usebackq skip=1 tokens=6 delims=\" %%A IN (`REG QUERY %KEY_NAME% /f "10.*" /k 2^>nul`) DO (
IF %found% == 0 IF EXIST "%ValueValue%UnionMetaData\%%A\Windows.winmd" (
set RefPath=-AI"%ValueValue:\=/%References/%%A"
set UMDPath=-AI"%ValueValue:\=/%UnionMetaData/%%A"
set found=1
)
)
)
if found==0 exit /b 0

@echo WINDOWS_SDK_PATHS = $(WINDOWS_SDK_PATHS) %RefPath% %UMDPath%
exit /b 1