identification: Avoid truncating identities created from data blobs
This is not necessarily an issue, but we should avoid not using the
full identity data as best as possible. The change also avoids the
dynamically sized buffer on the stack.
Fixes: 324528700d ("Added identification constructor using a chunk of data, guessing id type")
This commit is contained in:
1 parent
45b2f8d91f
commit
7e7c2805df
2 files changed
+22
-5
No files matched your search
@@ -80,6 +80,15 @@ START_TEST(test_from_data)
|
||||
ck_assert(chunk_equals(expected, encoding));
|
||||
a->destroy(a);
|
||||
|
||||
/* data that contains 0 characters is not handled with the string parser */
|
||||
expected = chunk_from_chars('f', 'o', 'o', '\0', 'b', 'a', 'r');
|
||||
a = identification_create_from_data(expected);
|
||||
ck_assert(ID_KEY_ID == a->get_type(a));
|
||||
encoding = a->get_encoding(a);
|
||||
ck_assert(expected.ptr != encoding.ptr);
|
||||
ck_assert(chunk_equals(expected, encoding));
|
||||
a->destroy(a);
|
||||
|
||||
/* everything else is handled by the string parser */
|
||||
expected = chunk_from_str("moon@strongswan.org");
|
||||
a = identification_create_from_data(expected);
|
||||
|
||||
@@ -2196,18 +2196,26 @@ identification_t *identification_create_from_string_with_regex(char *string)
|
||||
*/
|
||||
identification_t *identification_create_from_data(chunk_t data)
|
||||
{
|
||||
char buf[data.len + 1];
|
||||
identification_t *id;
|
||||
|
||||
if (is_asn1(data) && is_valid_dn(data))
|
||||
{
|
||||
return identification_create_from_encoding(ID_DER_ASN1_DN, data);
|
||||
id = identification_create_from_encoding(ID_DER_ASN1_DN, data);
|
||||
}
|
||||
else if (data.len && memchr(data.ptr, '\0', data.len))
|
||||
{
|
||||
/* treat identities that would get truncated below as opaque blobs */
|
||||
id = identification_create_from_encoding(ID_KEY_ID, data);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* use string constructor */
|
||||
snprintf(buf, sizeof(buf), "%.*s", (int)data.len, data.ptr);
|
||||
return identification_create_from_string(buf);
|
||||
char *str;
|
||||
|
||||
str = strndup(data.ptr, data.len);
|
||||
id = identification_create_from_string(str);
|
||||
free(str);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in new issue
Block a user