From a563a6836b06e61d168d3ac16151ca4c730dea1f Mon Sep 17 00:00:00 2001 From: David Fort Date: Thu, 27 Jan 2022 12:04:52 +0100 Subject: [PATCH] winpr: ncrypt add a property to retrieve the PKCS11 slotId --- winpr/include/winpr/ncrypt.h | 32 ++++++++++++++------------- winpr/libwinpr/ncrypt/ncrypt.c | 9 +++++--- winpr/libwinpr/ncrypt/ncrypt.h | 1 + winpr/libwinpr/ncrypt/ncrypt_pkcs11.c | 31 ++++++++++++++++++++------ 4 files changed, 48 insertions(+), 25 deletions(-) diff --git a/winpr/include/winpr/ncrypt.h b/winpr/include/winpr/ncrypt.h index ce062c2e4..1b243756c 100644 --- a/winpr/include/winpr/ncrypt.h +++ b/winpr/include/winpr/ncrypt.h @@ -114,22 +114,24 @@ typedef ULONG_PTR NCRYPT_KEY_HANDLE; "c\x00" \ "a\x00t\x00" \ "e\x00\x00" -#define NCRYPT_UNIQUE_NAME_PROPERTY \ - (const WCHAR*)"U\x00n\x00i\x00q\x00u\x00" \ - "e\x00 \x00N\x00" \ - "a\x00m\x00" \ - "e\x00\x00" -#define NCRYPT_READER_PROPERTY \ - (const WCHAR *)"S\x00m\x00" \ - "a\x00r\x00t\x00" \ - "C\x00" \ - "a\x00r\x00" \ - "d\x00R\x00" \ - "e\x00" \ - "a\x00" \ - "d\x00" \ - "e\x00r\x00\x00" +#define NCRYPT_UNIQUE_NAME_PROPERTY \ + (const WCHAR*)"U\x00n\x00i\x00q\x00u\x00" \ + "e\x00 \x00N\x00" \ + "a\x00m\x00" \ + "e\x00\x00" +#define NCRYPT_READER_PROPERTY \ + (const WCHAR*)"S\x00m\x00" \ + "a\x00r\x00t\x00" \ + "C\x00" \ + "a\x00r\x00" \ + "d\x00R\x00" \ + "e\x00" \ + "a\x00" \ + "d\x00" \ + "e\x00r\x00\x00" +/* winpr specific properties */ +#define NCRYPT_WINPR_SLOTID (const WCHAR*)"S\x00l\x00o\x00t\x00\x00" #define NCRYPT_MACHINE_KEY_FLAG 0x20 #define NCRYPT_SILENT_FLAG 0x40 diff --git a/winpr/libwinpr/ncrypt/ncrypt.c b/winpr/libwinpr/ncrypt/ncrypt.c index e770b81a7..8fc1ad5c4 100644 --- a/winpr/libwinpr/ncrypt/ncrypt.c +++ b/winpr/libwinpr/ncrypt/ncrypt.c @@ -124,14 +124,13 @@ SECURITY_STATUS NCryptEnumStorageProviders(DWORD* wProviderCount, return ERROR_SUCCESS; } - SECURITY_STATUS NCryptOpenStorageProvider(NCRYPT_PROV_HANDLE* phProvider, LPCWSTR pszProviderName, DWORD dwFlags) { #ifdef WITH_PKCS11 if (_wcscmp(pszProviderName, MS_SMART_CARD_KEY_STORAGE_PROVIDER) == 0 || - _wcscmp(pszProviderName, MS_SCARD_PROV) == 0) + _wcscmp(pszProviderName, MS_SCARD_PROV) == 0) { return winpr_NCryptOpenStorageProviderEx(phProvider, pszProviderName, dwFlags, NULL); } @@ -188,10 +187,14 @@ static NCryptKeyGetPropertyEnum propertyStringToEnum(LPCWSTR pszProperty) { return NCRYPT_PROPERTY_CERTIFICATE; } - else if(_wcscmp(pszProperty, NCRYPT_READER_PROPERTY) == 0) + else if (_wcscmp(pszProperty, NCRYPT_READER_PROPERTY) == 0) { return NCRYPT_PROPERTY_READER; } + else if (_wcscmp(pszProperty, NCRYPT_WINPR_SLOTID) == 0) + { + return NCRYPT_PROPERTY_SLOTID; + } return NCRYPT_PROPERTY_UNKNOWN; } diff --git a/winpr/libwinpr/ncrypt/ncrypt.h b/winpr/libwinpr/ncrypt/ncrypt.h index 67f77bd58..01b10b025 100644 --- a/winpr/libwinpr/ncrypt/ncrypt.h +++ b/winpr/libwinpr/ncrypt/ncrypt.h @@ -46,6 +46,7 @@ typedef enum { NCRYPT_PROPERTY_CERTIFICATE, NCRYPT_PROPERTY_READER, + NCRYPT_PROPERTY_SLOTID, NCRYPT_PROPERTY_UNKNOWN } NCryptKeyGetPropertyEnum; diff --git a/winpr/libwinpr/ncrypt/ncrypt_pkcs11.c b/winpr/libwinpr/ncrypt/ncrypt_pkcs11.c index 190953159..5543803bb 100644 --- a/winpr/libwinpr/ncrypt/ncrypt_pkcs11.c +++ b/winpr/libwinpr/ncrypt/ncrypt_pkcs11.c @@ -102,7 +102,7 @@ static SECURITY_STATUS NCryptP11StorageProvider_dtor(NCRYPT_HANDLE handle) static void fix_padded_string(char *str, size_t maxlen) { - char *ptr = str + maxlen-1; + char* ptr = str + maxlen - 1; while (ptr > str && *ptr == ' ') ptr--; @@ -429,7 +429,7 @@ static SECURITY_STATUS parseKeyName(LPCWSTR pszKeyName, CK_SLOT_ID* slotId, CK_B char* pos; if (WideCharToMultiByte(CP_UTF8, 0, pszKeyName, _wcslen(pszKeyName) + 1, asciiKeyName, - sizeof(asciiKeyName)-1, "?", FALSE) <= 0) + sizeof(asciiKeyName) - 1, "?", FALSE) <= 0) return NTE_BAD_KEY; if (*asciiKeyName != '\\') @@ -481,7 +481,7 @@ static SECURITY_STATUS NCryptP11EnumKeys(NCRYPT_PROV_HANDLE hProvider, LPCWSTR p int asciiScopeLen; if (WideCharToMultiByte(CP_UTF8, 0, pszScope, _wcslen(pszScope) + 1, asciiScope, - sizeof(asciiScope)-1, "?", NULL) <= 0) + sizeof(asciiScope) - 1, "?", NULL) <= 0) return NTE_INVALID_PARAMETER; if (strstr(asciiScope, "\\\\.\\") != asciiScope) @@ -645,10 +645,12 @@ static SECURITY_STATUS NCryptP11KeyGetProperties(NCryptP11KeyHandle* keyHandle, WINPR_ASSERT(provider); switch (property) + { case NCRYPT_PROPERTY_CERTIFICATE: break; - case NCRYPT_PROPERTY_READER: { + case NCRYPT_PROPERTY_READER: + { CK_SLOT_INFO slotInfo; WINPR_ASSERT(provider->p11->C_GetSlotInfo); @@ -656,19 +658,34 @@ static SECURITY_STATUS NCryptP11KeyGetProperties(NCryptP11KeyHandle* keyHandle, if (rv != CKR_OK) return NTE_BAD_KEY; - #define SLOT_DESC_SZ sizeof(slotInfo.slotDescription) +#define SLOT_DESC_SZ sizeof(slotInfo.slotDescription) fix_padded_string((char*)slotInfo.slotDescription, SLOT_DESC_SZ); *pcbResult = 2 * (strnlen((char*)slotInfo.slotDescription, SLOT_DESC_SZ) + 1); if (pbOutput) { - if(cbOutput < *pcbResult) + if (cbOutput < *pcbResult) return NTE_NO_MEMORY; - if (MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)slotInfo.slotDescription, -1, (LPWSTR)pbOutput, cbOutput) <= 0) + if (MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)slotInfo.slotDescription, -1, + (LPWSTR)pbOutput, cbOutput) <= 0) return NTE_NO_MEMORY; } return ERROR_SUCCESS; } + case NCRYPT_PROPERTY_SLOTID: + { + *pcbResult = 4; + if (pbOutput) + { + UINT32* ptr = (UINT32*)pbOutput; + + if (cbOutput < 4) + return NTE_NO_MEMORY; + + *ptr = keyHandle->slotId; + } + return ERROR_SUCCESS; + } case NCRYPT_PROPERTY_UNKNOWN: default: return NTE_NOT_SUPPORTED;