Browse Source

fix: replace deprecated SHA256 API with EVP, conditional dl linkage for Windows

chatgui
Evgeny 3 months ago
parent
commit
cca3511503
  1. 27
      tests/test_member_sync.c
  2. 6
      tools/chatgui/CMakeLists.txt
  3. 37
      tools/chatgui/transport/member_sync.c

27
tests/test_member_sync.c

@ -4,7 +4,7 @@
#include <stdlib.h>
#include <assert.h>
#include <sqlite3.h>
#include <openssl/sha.h>
#include <openssl/evp.h>
#define HASH_SZ 32
#define BUCKETS 32
@ -39,12 +39,14 @@ static void _create_peers(sqlite3* db, const char* ch_id) {
static void _member_hash(uint64_t nid, const uint8_t* x25, const uint8_t* ed,
const uint8_t* sig, uint8_t out[HASH_SZ]) {
SHA256_CTX ctx; SHA256_Init(&ctx);
SHA256_Update(&ctx, &nid, 8);
SHA256_Update(&ctx, x25, 32);
SHA256_Update(&ctx, ed, 32);
SHA256_Update(&ctx, sig, 64);
SHA256_Final(out, &ctx);
EVP_MD_CTX* ctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
EVP_DigestUpdate(ctx, &nid, 8);
EVP_DigestUpdate(ctx, x25, 32);
EVP_DigestUpdate(ctx, ed, 32);
EVP_DigestUpdate(ctx, sig, 64);
EVP_DigestFinal_ex(ctx, out, NULL);
EVP_MD_CTX_free(ctx);
}
static uint64_t _mask(int lvl) { int s=63-lvl*5; return s>=0 ? ~0ULL<<s : UINT64_MAX; }
@ -58,16 +60,19 @@ static void _tree_hash(sqlite3* db, const char* ch_id, int lvl, uint64_t pref, u
san, (long long)_mask(lvl), (long long)pref);
sqlite3_stmt* st=NULL;
if (sqlite3_prepare_v2(db, sql, -1, &st, NULL)!=SQLITE_OK) { memset(out,0,HASH_SZ); return; }
SHA256_CTX ctx; SHA256_Init(&ctx); int cnt=0;
EVP_MD_CTX* ctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
int cnt=0;
while (sqlite3_step(st)==SQLITE_ROW) {
uint64_t nid=(uint64_t)sqlite3_column_int64(st,0);
const uint8_t* x=sqlite3_column_blob(st,1),*e=sqlite3_column_blob(st,2),*s=sqlite3_column_blob(st,3);
if(!x||!e||!s)continue;
uint8_t mh[HASH_SZ]; _member_hash(nid,x,e,s,mh); SHA256_Update(&ctx,mh,HASH_SZ); cnt++;
uint8_t mh[HASH_SZ]; _member_hash(nid,x,e,s,mh); EVP_DigestUpdate(ctx,mh,HASH_SZ); cnt++;
}
sqlite3_finalize(st);
if(cnt==0){memset(out,0,HASH_SZ);return;}
SHA256_Final(out,&ctx);
if(cnt==0){EVP_MD_CTX_free(ctx);memset(out,0,HASH_SZ);return;}
EVP_DigestFinal_ex(ctx,out,NULL);
EVP_MD_CTX_free(ctx);
}
static void _insert(sqlite3* db, const char* ch_id, uint64_t nid,

6
tools/chatgui/CMakeLists.txt

@ -105,7 +105,11 @@ target_include_directories(test_member_sync PRIVATE
)
target_compile_definitions(test_member_sync PRIVATE USE_SQLITE SQLITE_THREADSAFE=1)
set_source_files_properties(db/sqlite3.c ../../tests/test_member_sync.c PROPERTIES LANGUAGE C)
target_link_libraries(test_member_sync PRIVATE OpenSSL::Crypto pthread dl)
if(WIN32)
target_link_libraries(test_member_sync PRIVATE OpenSSL::Crypto pthread)
else()
target_link_libraries(test_member_sync PRIVATE OpenSSL::Crypto pthread dl)
endif()
# ============================================================================
# Tests

37
tools/chatgui/transport/member_sync.c

@ -12,7 +12,7 @@
#include <string.h>
#include <stdlib.h>
#include <openssl/sha.h>
#include <openssl/evp.h>
#include <sqlite3.h>
#define MS_ID "member_sync"
@ -89,14 +89,14 @@ static void _compute_member_hash(uint64_t node_id, const uint8_t* x25519,
const uint8_t* ed25519, const uint8_t* join_sig,
const uint8_t* addrs_data, int addr_count, int online,
uint8_t hash_out[MS_HASH_SIZE]) {
SHA256_CTX ctx;
SHA256_Init(&ctx);
SHA256_Update(&ctx, &node_id, 8);
SHA256_Update(&ctx, x25519, 32);
SHA256_Update(&ctx, ed25519, 32);
SHA256_Update(&ctx, join_sig, 64);
EVP_MD_CTX* ctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
EVP_DigestUpdate(ctx, &node_id, 8);
EVP_DigestUpdate(ctx, x25519, 32);
EVP_DigestUpdate(ctx, ed25519, 32);
EVP_DigestUpdate(ctx, join_sig, 64);
uint8_t ac = (uint8_t)addr_count;
SHA256_Update(&ctx, &ac, 1);
EVP_DigestUpdate(ctx, &ac, 1);
if (addr_count > 0 && addrs_data) {
struct addr_item items[256]; int n = 0;
@ -111,16 +111,17 @@ static void _compute_member_hash(uint64_t node_id, const uint8_t* x25519,
}
qsort(items, (size_t)n, sizeof(struct addr_item), _addr_cmp);
for (int i = 0; i < n; i++) {
SHA256_Update(&ctx, &items[i].family, 1);
SHA256_Update(&ctx, items[i].addr, (size_t)(items[i].family == 4 ? 4 : 16));
EVP_DigestUpdate(ctx, &items[i].family, 1);
EVP_DigestUpdate(ctx, items[i].addr, (size_t)(items[i].family == 4 ? 4 : 16));
uint8_t port_be[2] = { (uint8_t)(items[i].port >> 8), (uint8_t)(items[i].port & 0xFF) };
SHA256_Update(&ctx, port_be, 2);
EVP_DigestUpdate(ctx, port_be, 2);
}
}
uint8_t ol = (uint8_t)(online ? 1 : 0);
SHA256_Update(&ctx, &ol, 1);
SHA256_Final(hash_out, &ctx);
EVP_DigestUpdate(ctx, &ol, 1);
EVP_DigestFinal_ex(ctx, hash_out, NULL);
EVP_MD_CTX_free(ctx);
}
static void _peers_table(const char* ch_id, char* buf, size_t sz) {
@ -153,8 +154,8 @@ static int _recompute_bucket(struct UTUN_INSTANCE* inst, const char* ch_id,
sqlite3_stmt* stmt = NULL;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK) return -1;
SHA256_CTX ctx;
SHA256_Init(&ctx);
EVP_MD_CTX* ctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
int count = 0;
while (sqlite3_step(stmt) == SQLITE_ROW) {
@ -167,12 +168,13 @@ static int _recompute_bucket(struct UTUN_INSTANCE* inst, const char* ch_id,
uint8_t mh[MS_HASH_SIZE];
_compute_member_hash(nid, x25, ed, sig, NULL, 0, online, mh);
SHA256_Update(&ctx, mh, MS_HASH_SIZE);
EVP_DigestUpdate(ctx, mh, MS_HASH_SIZE);
count++;
}
sqlite3_finalize(stmt);
if (count == 0) {
EVP_MD_CTX_free(ctx);
snprintf(sql, sizeof(sql),
"DELETE FROM member_tree_hash WHERE channel_id=? AND level=? AND prefix64=?");
sqlite3_stmt* ds = NULL;
@ -186,7 +188,8 @@ static int _recompute_bucket(struct UTUN_INSTANCE* inst, const char* ch_id,
}
uint8_t hash[MS_HASH_SIZE];
SHA256_Final(hash, &ctx);
EVP_DigestFinal_ex(ctx, hash, NULL);
EVP_MD_CTX_free(ctx);
snprintf(sql, sizeof(sql),
"INSERT OR REPLACE INTO member_tree_hash(channel_id, level, prefix64, hash, member_count)"

Loading…
Cancel
Save