From 7e7c2805df3819eb6add328377d99cafa86606b4 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Wed, 17 Jun 2026 18:42:06 +0200 Subject: [PATCH] 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: 324528700d98 ("Added identification constructor using a chunk of data, guessing id type") --- .../tests/suites/test_identification.c | 9 +++++++++ src/libstrongswan/utils/identification.c | 18 +++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/libstrongswan/tests/suites/test_identification.c b/src/libstrongswan/tests/suites/test_identification.c index b318fc659..1fc98b4a7 100644 --- a/src/libstrongswan/tests/suites/test_identification.c +++ b/src/libstrongswan/tests/suites/test_identification.c @@ -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); diff --git a/src/libstrongswan/utils/identification.c b/src/libstrongswan/utils/identification.c index 35837237c..009b1b724 100644 --- a/src/libstrongswan/utils/identification.c +++ b/src/libstrongswan/utils/identification.c @@ -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; } /*