libopensc: sc_hex_to_bin() accepts hex string with length 1

fix issue #751
This commit is contained in:
Viktor Tarasov 2016-05-06 10:33:35 +02:00
parent e9786bfb34
commit ca2c1c56c8
2 changed files with 14 additions and 5 deletions

View File

@ -2424,9 +2424,15 @@ sc_pkcs15_compare_id(const struct sc_pkcs15_id *id1, const struct sc_pkcs15_id *
void
sc_pkcs15_format_id(const char *str, struct sc_pkcs15_id *id)
{
size_t len = sizeof(id->value);
size_t len;
if (sc_hex_to_bin(str, id->value, &len) >= 0)
if (!id)
return;
len = sizeof(id->value);
if (sc_hex_to_bin(str, id->value, &len) != SC_SUCCESS)
id->len = 0;
else
id->len = len;
}

View File

@ -50,10 +50,11 @@ const char *sc_get_version(void)
int sc_hex_to_bin(const char *in, u8 *out, size_t *outlen)
{
int err = SC_SUCCESS;
size_t left, count = 0;
size_t left, count = 0, in_len;
assert(in != NULL && out != NULL && outlen != NULL);
left = *outlen;
in_len = strlen(in);
while (*in != '\0') {
int byte = 0, nybbles = 2;
@ -76,11 +77,13 @@ int sc_hex_to_bin(const char *in, u8 *out, size_t *outlen)
}
byte |= c;
}
// Detect premature end of string before byte is complete
if (!*in && nybbles >= 0) {
/* Detect premature end of string before byte is complete */
if (in_len > 1 && *in == '\0' && nybbles >= 0) {
err = SC_ERROR_INVALID_ARGUMENTS;
break;
}
if (*in == ':' || *in == ' ')
in++;
if (left <= 0) {