19 #include <winpr/error.h>
20 #include <winpr/ncrypt.h>
21 #include <winpr/string.h>
22 #include <winpr/wlog.h>
23 #include <winpr/smartcard.h>
25 #include <openssl/bio.h>
26 #include <openssl/x509.h>
28 #define TAG "testNCrypt"
30 static void crypto_print_name(
const BYTE* b, DWORD sz)
34 BIO* bio = BIO_new_mem_buf(b, (
int)sz);
38 X509* x509 = d2i_X509_bio(bio, NULL);
42 X509_NAME* name = X509_get_subject_name(x509);
46 char* ret = calloc(1024,
sizeof(
char));
50 char* ret2 = X509_NAME_oneline(name, ret, 1024);
52 printf(
"\t%s\n", ret2);
61 int TestNCryptSmartcard(
int argc,
char* argv[])
64 DWORD providerCount = 0;
70 SECURITY_STATUS status = NCryptEnumStorageProviders(&providerCount, &names, NCRYPT_SILENT_FLAG);
71 if (status != ERROR_SUCCESS)
74 for (
size_t j = 0; j < providerCount; j++)
77 NCRYPT_PROV_HANDLE provider = 0;
78 char providerNameStr[256] = { 0 };
79 PVOID enumState = NULL;
83 if (ConvertWCharToUtf8(name->pszName, providerNameStr, ARRAYSIZE(providerNameStr)) < 0)
85 printf(
"provider %" PRIuz
": %s\n", j, providerNameStr);
87 status = NCryptOpenStorageProvider(&provider, name->pszName, 0);
88 if (status != ERROR_SUCCESS)
91 while ((status = NCryptEnumKeys(provider, NULL, &keyName, &enumState,
92 NCRYPT_SILENT_FLAG)) == ERROR_SUCCESS)
94 NCRYPT_KEY_HANDLE phKey = 0;
97 char keyNameStr[256] = { 0 };
98 WCHAR reader[1024] = { 0 };
99 PBYTE certBytes = NULL;
101 if (ConvertWCharToUtf8(keyName->pszName, keyNameStr, ARRAYSIZE(keyNameStr)) < 0)
104 printf(
"\tkey %" PRIuz
": %s\n", i, keyNameStr);
105 status = NCryptOpenKey(provider, &phKey, keyName->pszName, keyName->dwLegacyKeySpec,
107 if (status != ERROR_SUCCESS)
109 WLog_ERR(TAG,
"unable to open key %s", keyNameStr);
113 status = NCryptGetProperty(phKey, NCRYPT_READER_PROPERTY, (PBYTE)reader,
sizeof(reader),
115 if (status == ERROR_SUCCESS)
117 char readerStr[1024] = { 0 };
119 (void)ConvertWCharNToUtf8(reader, cbOutput, readerStr, ARRAYSIZE(readerStr));
120 printf(
"\treader: %s\n", readerStr);
125 NCryptGetProperty(phKey, NCRYPT_CERTIFICATE_PROPERTY, NULL, 0, &cbOutput, dwFlags);
126 if (status != ERROR_SUCCESS)
128 WLog_ERR(TAG,
"unable to retrieve certificate len for key '%s'", keyNameStr);
132 certBytes = calloc(1, cbOutput);
133 status = NCryptGetProperty(phKey, NCRYPT_CERTIFICATE_PROPERTY, certBytes, cbOutput,
135 if (status != ERROR_SUCCESS)
137 WLog_ERR(TAG,
"unable to retrieve certificate for key %s", keyNameStr);
141 crypto_print_name(certBytes, cbOutput);
145 NCryptFreeBuffer(keyName);
146 NCryptFreeObject((NCRYPT_HANDLE)phKey);
150 NCryptFreeBuffer(enumState);
151 NCryptFreeObject((NCRYPT_HANDLE)provider);
153 if (status != NTE_NO_MORE_ITEMS)
155 (void)fprintf(stderr,
"NCryptEnumKeys returned %s [0x%08" PRIx32
"]\n",
156 Win32ErrorCode2Tag(status), status);
162 NCryptFreeBuffer(names);
a provider name descriptor