Hans Leidekker : bcrypt: Add support for importing and exporting ECC public keys.
Alexandre Julliard
julliard at winehq.org
Wed Feb 13 15:30:40 CST 2019
Module: wine
Branch: master
Commit: 5c2ac77ab77772e7d588ccef920a6fe91772d8b1
URL: https://source.winehq.org/git/wine.git/?a=commit;h=5c2ac77ab77772e7d588ccef920a6fe91772d8b1
Author: Hans Leidekker <hans at codeweavers.com>
Date: Wed Feb 13 10:21:16 2019 +0100
bcrypt: Add support for importing and exporting ECC public keys.
Signed-off-by: Hans Leidekker <hans at codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard at winehq.org>
---
dlls/bcrypt/bcrypt_main.c | 16 +++++++++++++++-
dlls/bcrypt/tests/bcrypt.c | 22 ++++++++++++++++++++++
2 files changed, 37 insertions(+), 1 deletion(-)
diff --git a/dlls/bcrypt/bcrypt_main.c b/dlls/bcrypt/bcrypt_main.c
index c41c409..0d504b8 100644
--- a/dlls/bcrypt/bcrypt_main.c
+++ b/dlls/bcrypt/bcrypt_main.c
@@ -813,6 +813,14 @@ static NTSTATUS key_export( struct key *key, const WCHAR *type, UCHAR *output, U
memcpy( output + sizeof(len), key->u.s.secret, key->u.s.secret_len );
return STATUS_SUCCESS;
}
+ else if (!strcmpW( type, BCRYPT_ECCPUBLIC_BLOB ))
+ {
+ *size = key->u.a.pubkey_len;
+ if (output_len < key->u.a.pubkey_len) return STATUS_SUCCESS;
+
+ memcpy( output, key->u.a.pubkey, key->u.a.pubkey_len );
+ return STATUS_SUCCESS;
+ }
FIXME( "unsupported key type %s\n", debugstr_w(type) );
return STATUS_NOT_IMPLEMENTED;
@@ -1012,6 +1020,11 @@ static NTSTATUS key_import_pair( struct algorithm *alg, const WCHAR *type, BCRYP
switch (alg->id)
{
+ case ALG_ID_ECDH_P256:
+ key_size = 32;
+ magic = BCRYPT_ECDH_PUBLIC_P256_MAGIC;
+ break;
+
case ALG_ID_ECDSA_P256:
key_size = 32;
magic = BCRYPT_ECDSA_PUBLIC_P256_MAGIC;
@@ -1028,7 +1041,8 @@ static NTSTATUS key_import_pair( struct algorithm *alg, const WCHAR *type, BCRYP
}
if (ecc_blob->dwMagic != magic) return STATUS_NOT_SUPPORTED;
- if (ecc_blob->cbKey != key_size) return STATUS_INVALID_PARAMETER;
+ if (ecc_blob->cbKey != key_size || input_len < sizeof(*ecc_blob) + ecc_blob->cbKey * 2)
+ return STATUS_INVALID_PARAMETER;
if (!(key = heap_alloc_zero( sizeof(*key) ))) return STATUS_NO_MEMORY;
key->hdr.magic = MAGIC_KEY;
diff --git a/dlls/bcrypt/tests/bcrypt.c b/dlls/bcrypt/tests/bcrypt.c
index 7274516..819c1dc 100644
--- a/dlls/bcrypt/tests/bcrypt.c
+++ b/dlls/bcrypt/tests/bcrypt.c
@@ -1659,9 +1659,12 @@ static void test_RSA(void)
static void test_ECDH(void)
{
+ BYTE *buf;
+ BCRYPT_ECCKEY_BLOB *ecckey;
BCRYPT_ALG_HANDLE alg;
BCRYPT_KEY_HANDLE key;
NTSTATUS status;
+ ULONG size;
status = pBCryptOpenAlgorithmProvider(&alg, BCRYPT_ECDH_P256_ALGORITHM, NULL, 0);
if (status)
@@ -1678,6 +1681,25 @@ static void test_ECDH(void)
status = pBCryptFinalizeKeyPair(key, 0);
ok(status == STATUS_SUCCESS, "got %08x\n", status);
+ size = 0;
+ SetLastError(0xdeadbeef);
+ status = pBCryptExportKey(key, NULL, BCRYPT_ECCPUBLIC_BLOB, NULL, 0, &size, 0);
+ ok(status == STATUS_SUCCESS, "got %08x\n", status);
+ ok(size, "size not set\n");
+
+ buf = HeapAlloc(GetProcessHeap(), 0, size);
+ status = pBCryptExportKey(key, NULL, BCRYPT_ECCPUBLIC_BLOB, buf, size, &size, 0);
+ ok(status == STATUS_SUCCESS, "got %08x\n", status);
+ ecckey = (BCRYPT_ECCKEY_BLOB *)buf;
+ ok(ecckey->dwMagic == BCRYPT_ECDH_PUBLIC_P256_MAGIC, "got %08x\n", ecckey->dwMagic);
+ ok(ecckey->cbKey == 32, "got %u\n", ecckey->cbKey);
+ ok(size == sizeof(*ecckey) + ecckey->cbKey * 2, "got %u\n", size);
+ pBCryptDestroyKey(key);
+
+ status = pBCryptImportKeyPair(alg, NULL, BCRYPT_ECCPUBLIC_BLOB, &key, buf, size, 0);
+ ok(status == STATUS_SUCCESS, "got %08x\n", status);
+ HeapFree(GetProcessHeap(), 0, buf);
+
pBCryptDestroyKey(key);
pBCryptCloseAlgorithmProvider(alg, 0);
}
More information about the wine-cvs
mailing list