winpr: ncrypt add a property to retrieve the PKCS11 slotId

This commit is contained in:
David Fort 2022-01-27 12:04:52 +01:00 committed by akallabeth
parent 62ac25ef54
commit a563a6836b
4 changed files with 48 additions and 25 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -46,6 +46,7 @@ typedef enum
{
NCRYPT_PROPERTY_CERTIFICATE,
NCRYPT_PROPERTY_READER,
NCRYPT_PROPERTY_SLOTID,
NCRYPT_PROPERTY_UNKNOWN
} NCryptKeyGetPropertyEnum;

View File

@ -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;