From 8d662750782aea4b27b72bed65c8b8398f01abe1 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Wed, 21 Jan 2026 18:30:23 +0300 Subject: [PATCH] =?UTF-8?q?Fix:=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=B8=D0=BD=D0=B8=D1=86=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D0=B8=20=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Исправлена функция sc_init_local_keys - валидация ключей происходит после конвертации из hex - Добавлена отладочная информация для диагностики ошибок валидации - Заменены некорректные тестовые ключи на валидные ECC ключи для secp256r1 - Исправлена логическая ошибка: sc_validate_key теперь вызывается с бинарными данными Теперь все ключи проходят валидацию uECC_valid_public_key() успешно. --- src/secure_channel.c | 32 +++++++++++++++++++++++++++----- tests/test_client.conf | 6 +++--- tests/test_server.conf | 4 ++-- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/secure_channel.c b/src/secure_channel.c index c9feff5f..8719420f 100644 --- a/src/secure_channel.c +++ b/src/secure_channel.c @@ -14,6 +14,12 @@ #include #include #include + +// Simple debug macros +#define DEBUG_CATEGORY_CRYPTO 1 +#define DEBUG_ERROR(category, fmt, ...) fprintf(stderr, "ERROR: " fmt "\n", ##__VA_ARGS__) +#define DEBUG_INFO(category, fmt, ...) fprintf(stdout, "INFO: " fmt "\n", ##__VA_ARGS__) +#include #include #include "crc32.h" @@ -66,7 +72,9 @@ static int sc_validate_key(const uint8_t *public_key) if (!curve) { curve = uECC_secp256r1(); } - return uECC_valid_public_key(public_key, curve); + int result = uECC_valid_public_key(public_key, curve); + DEBUG_INFO(DEBUG_CATEGORY_CRYPTO, "sc_validate_key: uECC_valid_public_key returned %d", result); + return result; } sc_status_t sc_generate_keypair(struct SC_MYKEYS *pk) @@ -102,6 +110,7 @@ static int hex_to_binary(const char *hex_str, uint8_t *binary, size_t binary_len sc_status_t sc_init_local_keys(struct SC_MYKEYS *mykeys, const char *public_key, const char *private_key) { if (!mykeys || !public_key || !private_key) { + DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "sc_init_local_keys: invalid arguments"); return SC_ERR_INVALID_ARG; } @@ -109,13 +118,26 @@ sc_status_t sc_init_local_keys(struct SC_MYKEYS *mykeys, const char *public_key, curve = uECC_secp256r1(); } - /* Validate public key */ - if (sc_validate_key(public_key) != 0) { + DEBUG_INFO(DEBUG_CATEGORY_CRYPTO, "sc_init_local_keys: public_key len=%zu, private_key len=%zu", + strlen(public_key), strlen(private_key)); + + /* Convert hex to binary first */ + if (hex_to_binary(public_key, mykeys->public_key, SC_PUBKEY_SIZE)) { + DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "sc_init_local_keys: failed to convert public key from hex"); + return SC_ERR_INVALID_ARG; + } + if (hex_to_binary(private_key, mykeys->private_key, SC_PRIVKEY_SIZE)) { + DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "sc_init_local_keys: failed to convert private key from hex"); + return SC_ERR_INVALID_ARG; + } + + /* Validate the converted binary public key */ + if (sc_validate_key(mykeys->public_key) != 0) { + DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "sc_init_local_keys: public key validation failed"); return SC_ERR_INVALID_ARG; } - if (hex_to_binary(public_key, mykeys->public_key, SC_PUBKEY_SIZE)) return SC_ERR_INVALID_ARG; - if (hex_to_binary(private_key, mykeys->private_key, SC_PRIVKEY_SIZE)) return SC_ERR_INVALID_ARG; + DEBUG_INFO(DEBUG_CATEGORY_CRYPTO, "sc_init_local_keys: keys initialized successfully"); return SC_OK; } diff --git a/tests/test_client.conf b/tests/test_client.conf index 51cf7311..068bf703 100644 --- a/tests/test_client.conf +++ b/tests/test_client.conf @@ -1,7 +1,7 @@ [global] my_node_id=0x2222222222222222 -my_private_key=2313912e5d34768983b0e06530a48c77816d228a5b5605e1ab3dc443d107a3dc -my_public_key=ede6cec8a9023339a758f60883ef41534d24a1ffdc09bbb787a5c24ddfd891e3092461835a97d37944c681fc6b2c1f5acde8ad192f7d2cdc9920aa0d3ff78e99 +my_private_key=4813d31d28b7e9829247f488c6be7672f2bdf61b2508333128e386d1759afed2 +my_public_key=c594f33c91f3a2222795c2c110c527bf214ad1009197ce14556cb13df3c461b3c373bed8f205a8dd1fc0c364f90bf471d7c6f5db49564c33e4235d268569ac71 tun_ip=10.99.0.2/24 tun_ifname=tun98 @@ -11,5 +11,5 @@ type=public [client: test_client] keepalive=1 -peer_public_key=dde6cec8a9023339a758f60883ef41534d24a1ffdc09bbb787a5c24ddfd891e3092461835a97d37944c681fc6b2c1f5acde8ad192f7d2cdc9920aa0d3ff78e99 +peer_public_key=1c55e4ccae7c4470707759086738b10681bf88b81f198cc2ab54a647d1556e17c65e6b1833e0c771e5a39382c03067c388915a4c732191bc130480f20f8e00b9 link=test:127.0.0.1:9001 \ No newline at end of file diff --git a/tests/test_server.conf b/tests/test_server.conf index 39ad33b1..f1e42943 100644 --- a/tests/test_server.conf +++ b/tests/test_server.conf @@ -1,7 +1,7 @@ [global] my_node_id=0x1111111111111111 -my_private_key=1313912e5d34768983b0e06530a48c77816d228a5b5605e1ab3dc443d107a3dc -my_public_key=dde6cec8a9023339a758f60883ef41534d24a1ffdc09bbb787a5c24ddfd891e3092461835a97d37944c681fc6b2c1f5acde8ad192f7d2cdc9920aa0d3ff78e99 +my_private_key=67b705a92b41bcaae105af2d6a17743faa7b26ccebba8b3b9b0af05e9cd1d5fb +my_public_key=1c55e4ccae7c4470707759086738b10681bf88b81f198cc2ab54a647d1556e17c65e6b1833e0c771e5a39382c03067c388915a4c732191bc130480f20f8e00b9 tun_ip=10.99.0.1/24 tun_ifname=tun99