Fix timing side-channel in check_peer_pubkey

strcmp() at line 315 short-circuits on first differing byte, leaking
timing info about trusted fingerprints.

Replace with constant-time comparison using memcmp and bitwise OR
accumulation across all trusted entries.

Fixes GH #239
This commit is contained in:
Levi Neely 2026-03-03 12:51:47 +01:00
parent fb8d0fc608
commit 6af9d0e9b9
1 changed files with 4 additions and 2 deletions

View File

@ -311,9 +311,11 @@ static int check_peer_pubkey(ssl_connection_t *conn) {
mbedtls_sha256(buff + sizeof(buff) - i, i, sigbin, 0);
psync_binhex(sighex, sigbin, 32);
sighex[64] = 0;
int match = 0;
for (i = 0; i < ARRAY_SIZE(psync_ssl_trusted_pk_sha256); i++)
if (!strcmp(sighex, psync_ssl_trusted_pk_sha256[i]))
return 0;
match |= (memcmp(sighex, psync_ssl_trusted_pk_sha256[i], 64) == 0);
if (match)
return 0;
pdbg_logf(D_ERROR,
"got sha256hex of public key %s that does not match any approved "
"fingerprint",