Skip to content

Commit 83d7ac7

Browse files
committed
esp: report a warning, not an error, for an invalid hex digit in a secret.
We already report a warning if the secret is too long; do so if there's an invalid hex digit in the secret. See #1185 (comment) While we're at it, put the length of the string, in hex-digit pairs, into a size_t rather than an unsigned int, just in case (it's unlikely that it will overflow a 32-bit unsigned integer, but...).
1 parent d5b1a16 commit 83d7ac7

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

print-esp.c

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ static void esp_print_addsa(netdissect_options *ndo,
357357
}
358358

359359

360-
static u_int hexdigit(netdissect_options *ndo, char hex)
360+
static int hexdigit(netdissect_options *ndo, char hex)
361361
{
362362
if (hex >= '0' && hex <= '9')
363363
return (hex - '0');
@@ -366,40 +366,53 @@ static u_int hexdigit(netdissect_options *ndo, char hex)
366366
else if (hex >= 'a' && hex <= 'f')
367367
return (hex - 'a' + 10);
368368
else {
369-
(*ndo->ndo_error)(ndo, S_ERR_ND_ESP_SECRET,
370-
"invalid hex digit %c in espsecret\n", hex);
371-
/* NOTREACHED */
369+
(*ndo->ndo_warning)(ndo,
370+
"invalid hex digit %c in espsecret\n", hex);
371+
return (-1);
372372
}
373373
}
374374

375-
static u_int hex2byte(netdissect_options *ndo, char *hexstring)
376-
{
377-
u_int byte;
378-
379-
byte = (hexdigit(ndo, hexstring[0]) << 4) + hexdigit(ndo, hexstring[1]);
380-
return byte;
381-
}
382-
383375
/*
384376
* returns size of binary, 0 on failure.
385377
*/
386378
static int
387379
espprint_decode_hex(netdissect_options *ndo,
388380
u_char *binbuf, unsigned int binbuf_len, char *hex)
389381
{
390-
unsigned int len;
382+
size_t len;
391383
int i;
392384

385+
/*
386+
* XXX - fail if the string length isn't a multiple of 2?
387+
*/
393388
len = strlen(hex) / 2;
394389

395390
if (len > binbuf_len) {
396-
(*ndo->ndo_warning)(ndo, "secret is too big: %u\n", len);
391+
(*ndo->ndo_warning)(ndo, "secret is too big: %zu\n", len);
397392
return 0;
398393
}
399394

400395
i = 0;
401396
while (hex[0] != '\0' && hex[1]!='\0') {
402-
binbuf[i] = hex2byte(ndo, hex);
397+
int upper_nibble, lower_nibble;
398+
399+
upper_nibble = hexdigit(ndo, hex[0]);
400+
if (upper_nibble < 0) {
401+
/*
402+
* Invalid hex digit; a warning has already been
403+
* printed.
404+
*/
405+
return 0;
406+
}
407+
lower_nibble = hexdigit(ndo, hex[1]);
408+
if (lower_nibble < 0) {
409+
/*
410+
* Invalid hex digit; a warning has already been
411+
* printed.
412+
*/
413+
return 0;
414+
}
415+
binbuf[i] = (((u_int)upper_nibble) << 4) + (((u_int)lower_nibble) << 0);
403416
hex += 2;
404417
i++;
405418
}

0 commit comments

Comments
 (0)