From: Santino Mazza mazzasantino1206@gmail.com
v2: Initialize test variables and change title to better reflect the patch. Squash patch 5 into this one.
Signed-off-by: Santino Mazza mazzasantino1206@gmail.com Signed-off-by: Hans Leidekker hans@codeweavers.com --- dlls/ncrypt/main.c | 100 ++++++++++++++++++---------------- dlls/ncrypt/ncrypt_internal.h | 1 + dlls/ncrypt/tests/ncrypt.c | 22 ++++++-- include/ncrypt.h | 34 ++++++++++++ 4 files changed, 104 insertions(+), 53 deletions(-)
diff --git a/dlls/ncrypt/main.c b/dlls/ncrypt/main.c index ca70b2d92c3..82ce7a0b4dc 100644 --- a/dlls/ncrypt/main.c +++ b/dlls/ncrypt/main.c @@ -184,6 +184,53 @@ static struct object *allocate_object(enum object_type type) return ret; }
+static SECURITY_STATUS set_object_property(struct object *object, const WCHAR *name, BYTE *value, DWORD value_size) +{ + struct object_property *property; + + FIXME("check duplicates\n"); + if (!object->num_properties) + { + if (!(object->properties = malloc(sizeof(*property)))) + { + ERR("Error allocating memory."); + return NTE_NO_MEMORY; + } + property = &object->properties[object->num_properties++]; + } + else + { + struct object_property *tmp; + if (!(tmp = realloc(object->properties, sizeof(*property) * (object->num_properties + 1)))) + { + ERR("Error allocating memory."); + return NTE_NO_MEMORY; + } + object->properties = tmp; + property = &object->properties[object->num_properties++]; + } + + memset(property, 0, sizeof(*property)); + if (!(property->key = malloc((lstrlenW(name) + 1) * sizeof(WCHAR)))) + { + ERR("Error allocating memory."); + return NTE_NO_MEMORY; + } + + lstrcpyW(property->key, name); + property->value_size = value_size; + if (!(property->value = malloc(value_size))) + { + ERR("Error allocating memory."); + free(property->key); + property->key = NULL; + return NTE_NO_MEMORY; + } + + memcpy(property->value, value, value_size); + return ERROR_SUCCESS; +} + SECURITY_STATUS WINAPI NCryptImportKey(NCRYPT_PROV_HANDLE provider, NCRYPT_KEY_HANDLE decrypt_key, const WCHAR *type, NCryptBufferDesc *params, NCRYPT_KEY_HANDLE *handle, BYTE *data, DWORD datasize, DWORD flags) @@ -244,6 +291,7 @@ SECURITY_STATUS WINAPI NCryptImportKey(NCRYPT_PROV_HANDLE provider, NCRYPT_KEY_H
key = &object->key; key->alg = RSA; + key->rsa.bit_length = rsaheader->BitLength; key->rsa.public_exp_size = rsaheader->cbPublicExp; key->rsa.modulus_size = rsaheader->cbModulus; if (!(key->rsa.public_exp = malloc(rsaheader->cbPublicExp))) @@ -261,10 +309,13 @@ SECURITY_STATUS WINAPI NCryptImportKey(NCRYPT_PROV_HANDLE provider, NCRYPT_KEY_H }
public_exp = &data[sizeof(*rsaheader)]; /* The public exp is after the header. */ - modulus = &public_exp[rsaheader->cbPublicExp]; /* The modulus is after the public exp. */ + modulus = &public_exp[rsaheader->cbPublicExp]; /* The modulus is after the public exponent. */ memcpy(key->rsa.public_exp, public_exp, rsaheader->cbPublicExp); memcpy(key->rsa.modulus, modulus, rsaheader->cbModulus);
+ set_object_property(object, NCRYPT_ALGORITHM_GROUP_PROPERTY, (BYTE *)L"RSA", sizeof(L"RSA")); + set_object_property(object, NCRYPT_LENGTH_PROPERTY, (BYTE *)&key->rsa.bit_length, sizeof(key->rsa.bit_length)); + set_object_property(object, NCRYPT_PROVIDER_HANDLE_PROPERTY, (BYTE *)&provider, sizeof(provider)); *handle = (NCRYPT_KEY_HANDLE)object; break; } @@ -310,53 +361,6 @@ SECURITY_STATUS WINAPI NCryptOpenStorageProvider(NCRYPT_PROV_HANDLE *provider, c return ERROR_SUCCESS; }
-static SECURITY_STATUS set_object_property(struct object *object, const WCHAR *name, BYTE *value, DWORD value_size) -{ - struct object_property *property; - - FIXME("check duplicates\n"); - if (!object->num_properties) - { - if (!(object->properties = malloc(sizeof(*property)))) - { - ERR("Error allocating memory."); - return NTE_NO_MEMORY; - } - property = &object->properties[object->num_properties++]; - } - else - { - struct object_property *tmp; - if (!(tmp = realloc(object->properties, sizeof(*property) * (object->num_properties + 1)))) - { - ERR("Error allocating memory."); - return NTE_NO_MEMORY; - } - object->properties = tmp; - property = &object->properties[object->num_properties++]; - } - - memset(property, 0, sizeof(*property)); - if (!(property->key = malloc((lstrlenW(name) + 1) * sizeof(WCHAR)))) - { - ERR("Error allocating memory."); - return NTE_NO_MEMORY; - } - - lstrcpyW(property->key, name); - property->value_size = value_size; - if (!(property->value = malloc(value_size))) - { - ERR("Error allocating memory."); - free(property->key); - property->key = NULL; - return NTE_NO_MEMORY; - } - - memcpy(property->value, value, value_size); - return ERROR_SUCCESS; -} - SECURITY_STATUS WINAPI NCryptSetProperty(NCRYPT_HANDLE handle, const WCHAR *name, BYTE *input, DWORD insize, DWORD flags) { struct object *object = (struct object *)handle; diff --git a/dlls/ncrypt/ncrypt_internal.h b/dlls/ncrypt/ncrypt_internal.h index fe5f27ea1eb..3966dd73ed6 100644 --- a/dlls/ncrypt/ncrypt_internal.h +++ b/dlls/ncrypt/ncrypt_internal.h @@ -26,6 +26,7 @@ enum key_algorithm
struct rsa_key { + DWORD bit_length; DWORD public_exp_size; BYTE *public_exp; DWORD modulus_size; diff --git a/dlls/ncrypt/tests/ncrypt.c b/dlls/ncrypt/tests/ncrypt.c index 23205d989f0..070b0c3f13e 100644 --- a/dlls/ncrypt/tests/ncrypt.c +++ b/dlls/ncrypt/tests/ncrypt.c @@ -169,7 +169,7 @@ static void test_get_property(void) NCRYPT_KEY_HANDLE key; SECURITY_STATUS ret; WCHAR value[4]; - DWORD size; + DWORD keylength, size;
ret = NCryptOpenStorageProvider(&prov, NULL, 0); ok(ret == ERROR_SUCCESS, "got %#lx\n", ret); @@ -177,17 +177,29 @@ static void test_get_property(void) ret = NCryptImportKey(prov, 0, BCRYPT_RSAPUBLIC_BLOB, NULL, &key, rsa_key_blob, sizeof(rsa_key_blob), 0); ok(ret == ERROR_SUCCESS, "got %#lx\n", ret);
- todo_wine { - ret = NCryptGetProperty(key, L"Algorithm Group", NULL, 0, &size, 0); + size = 0; + ret = NCryptGetProperty(key, NCRYPT_ALGORITHM_GROUP_PROPERTY, NULL, 0, &size, 0); ok(ret == ERROR_SUCCESS, "got %#lx\n", ret); ok(size == 8, "got %lu\n", size);
size = 0; - ret = NCryptGetProperty(key, L"Algorithm Group", (BYTE *)value, sizeof(value), &size, 0); + value[0] = 0; + ret = NCryptGetProperty(key, NCRYPT_ALGORITHM_GROUP_PROPERTY, (BYTE *)value, sizeof(value), &size, 0); ok(ret == ERROR_SUCCESS, "got %#lx\n", ret); + todo_wine { ok(size == 8, "got %lu\n", size); - ok(!lstrcmpW(value, L"RSA"), "The string doesn't match with 'RSA'\n"); } + ok(!lstrcmpW(value, L"RSA"), "The string doesn't match with 'RSA'\n"); + + size = 0; + ret = NCryptGetProperty(key, NCRYPT_LENGTH_PROPERTY, NULL, 0, &size, 0); + ok(ret == ERROR_SUCCESS, "got %#lx\n", ret); + ok(size == sizeof(DWORD), "got %lu\n", size); + + keylength = 0; + ret = NCryptGetProperty(key, NCRYPT_LENGTH_PROPERTY, (BYTE *)&keylength, size, &size, 0); + ok(ret == ERROR_SUCCESS, "got %#lx\n", ret); + ok(keylength == 1024, "got %lu\n", keylength);
NCryptFreeObject(prov); } diff --git a/include/ncrypt.h b/include/ncrypt.h index 5a8ba7e88fc..f7372b30621 100644 --- a/include/ncrypt.h +++ b/include/ncrypt.h @@ -71,6 +71,40 @@ typedef ULONG_PTR NCRYPT_SECRET_HANDLE;
#define NCRYPT_SILENT_FLAG 0x00000040
+#define NCRYPT_NAME_PROPERTY L"Name" +#define NCRYPT_UNIQUE_NAME_PROPERTY L"Unique Name" +#define NCRYPT_ALGORITHM_PROPERTY L"Algorithm Name" +#define NCRYPT_LENGTH_PROPERTY L"Length" +#define NCRYPT_LENGTHS_PROPERTY L"Lengths" +#define NCRYPT_BLOCK_LENGTH_PROPERTY L"Block Length" +#define NCRYPT_UI_POLICY_PROPERTY L"UI Policy" +#define NCRYPT_EXPORT_POLICY_PROPERTY L"Export Policy" +#define NCRYPT_WINDOW_HANDLE_PROPERTY L"HWND Handle" +#define NCRYPT_USE_CONTEXT_PROPERTY L"Use Context" +#define NCRYPT_IMPL_TYPE_PROPERTY L"Impl Type" +#define NCRYPT_KEY_USAGE_PROPERTY L"Key Usage" +#define NCRYPT_KEY_TYPE_PROPERTY L"Key Type" +#define NCRYPT_VERSION_PROPERTY L"Version" +#define NCRYPT_SECURITY_DESCR_SUPPORT_PROPERTY L"Security Descr Support" +#define NCRYPT_SECURITY_DESCR_PROPERTY L"Security Descr" +#define NCRYPT_USE_COUNT_ENABLED_PROPERTY L"Enabled Use Count" +#define NCRYPT_USE_COUNT_PROPERTY L"Use Count" +#define NCRYPT_LAST_MODIFIED_PROPERTY L"Modified" +#define NCRYPT_MAX_NAME_LENGTH_PROPERTY L"Max Name Length" +#define NCRYPT_ALGORITHM_GROUP_PROPERTY L"Algorithm Group" +#define NCRYPT_PROVIDER_HANDLE_PROPERTY L"Provider Handle" +#define NCRYPT_PIN_PROPERTY L"SmartCardPin" +#define NCRYPT_READER_PROPERTY L"SmartCardReader" +#define NCRYPT_SMARTCARD_GUID_PROPERTY L"SmartCardGuid" +#define NCRYPT_CERTIFICATE_PROPERTY L"SmartCardKeyCertificate" +#define NCRYPT_PIN_PROMPT_PROPERTY L"SmartCardPinPrompt" +#define NCRYPT_USER_CERTSTORE_PROPERTY L"SmartCardUserCertStore" +#define NCRYPT_ROOT_CERTSTORE_PROPERTY L"SmartcardRootCertStore" +#define NCRYPT_SECURE_PIN_PROPERTY L"SmartCardSecurePin" +#define NCRYPT_ASSOCIATED_ECDH_KEY L"SmartCardAssociatedECDHKey" +#define NCRYPT_SCARD_PIN_ID L"SmartCardPinId" +#define NCRYPT_SCARD_PIN_INFO L"SmartCardPinInfo" + SECURITY_STATUS WINAPI NCryptCreatePersistedKey(NCRYPT_PROV_HANDLE, NCRYPT_KEY_HANDLE *, const WCHAR *, const WCHAR *, DWORD, DWORD); SECURITY_STATUS WINAPI NCryptDecrypt(NCRYPT_KEY_HANDLE, BYTE *, DWORD, void *, BYTE *, DWORD, DWORD *, DWORD); SECURITY_STATUS WINAPI NCryptEncrypt(NCRYPT_KEY_HANDLE, BYTE *, DWORD, void *, BYTE *, DWORD, DWORD *, DWORD);