From d2280cf2397520332ed11829cc3e77f4d2962efc Mon Sep 17 00:00:00 2001 From: arfa k Date: Sat, 21 Nov 2026 12:48:09 +0530 Subject: [PATCH 1/2] Generate ice_lite credentials with RAND_bytes() instead of random() ice_lite_data_ctor() builds the local ICE ufrag and pwd with generate_random_string(), which takes each character from random() % 64. random() is seeded once per process from a 32-bit value and is an additive lagged generator (r[i] = r[i-3] + r[i-31]), so the low bits of its output are fully predictable from earlier output. The local pwd is the only key for MESSAGE-INTEGRITY on incoming binding requests, and a check that passes makes the module force-latch the stream to its source. A caller who sends "A" with long ufrag/pwd gets local credentials of the same length back in their own answer, which is enough random() % 64 output to recover the generator's low bits and compute the ufrag/pwd of the sessions created after it, regardless of how many random() calls the port table makes in between. RFC 8445 5.3 asks for at least 24 bits of randomness in the ufrag and 128 bits in the pwd. Move the helper into the module, its only user, and draw the bytes from RAND_bytes(). The module is only built with libcrypto, and dtls_gw uses RAND_bytes() the same way. Fail the allocation if RAND_bytes() fails. --- modules/ice_lite/rtpp_ice_lite.c | 29 +++++++++++++++++++++++++++-- src/rtpp_util.c | 16 ---------------- src/rtpp_util.h | 1 - 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/modules/ice_lite/rtpp_ice_lite.c b/modules/ice_lite/rtpp_ice_lite.c index 948dd19fd..0556012c5 100644 --- a/modules/ice_lite/rtpp_ice_lite.c +++ b/modules/ice_lite/rtpp_ice_lite.c @@ -38,6 +38,8 @@ #include #include +#include + #include "config_pp.h" #include "rtpp_types.h" @@ -291,6 +293,27 @@ udp_local_get(const struct udp_sock *us, struct sa *local) return (0); } +/* + * RFC 8445 section 5.3: ufrag needs at least 24 and pwd at least 128 bits + * of randomness. The charset has 64 entries, so each byte gives 6 bits. + */ +static int +ice_random_string(char *buffer, int length) +{ + static const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789" + "+/"; + unsigned char *rb = (unsigned char *)buffer; + + if (RAND_bytes(rb, length) != 1) + return (-1); + for (int i = 0; i < length; i++) + buffer[i] = charset[rb[i] % (sizeof(charset) - 1)]; + buffer[length] = '\0'; + return (0); +} + static struct ice_lite_agent_cfg * ice_lite_data_ctor(int lufrag_len, int lpwd_len, struct rtpp_minfo *mself) { @@ -307,9 +330,11 @@ ice_lite_data_ctor(int lufrag_len, int lpwd_len, struct rtpp_minfo *mself) if (lufrag.s == NULL || lpwd.s == NULL) goto e1; lufrag.len = lufrag_len; - generate_random_string(lufrag.s, lufrag.len); + if (ice_random_string(lufrag.s, lufrag.len) != 0) + goto e1; lpwd.len = lpwd_len; - generate_random_string(lpwd.s, lpwd.len); + if (ice_random_string(lpwd.s, lpwd.len) != 0) + goto e1; if (icem_alloc(&ila_c->icem, ICE_MODE_LITE, ICE_ROLE_CONTROLLED, IPPROTO_UDP, 0, tiebrk, lufrag.s, lpwd.s, NULL, NULL) != 0) goto e1; diff --git a/src/rtpp_util.c b/src/rtpp_util.c index 1b123f3c0..02d822a10 100644 --- a/src/rtpp_util.c +++ b/src/rtpp_util.c @@ -597,19 +597,3 @@ rtpp_strsplit(char *ibuf, char *mbuf, size_t dlen, size_t blen) cp += 1; } } - -void -generate_random_string(char *buffer, int length) -{ - const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789" - "+/"; - int charset_size = sizeof(charset) - 1; - - for (int i = 0; i < length; i++) { - int key = random() % charset_size; - buffer[i] = charset[key]; - } - buffer[length] = '\0'; -} diff --git a/src/rtpp_util.h b/src/rtpp_util.h index c8d5b6113..bded73c3d 100644 --- a/src/rtpp_util.h +++ b/src/rtpp_util.h @@ -65,7 +65,6 @@ enum atoi_rval rtpp_parse_rtp_rtcp_val(const char *, int *, int *, int); enum atoi_rval strtol_saferange(const char *, long *, long, long, const char **) RTPP_EXPORT; void rtpp_strsplit(char *, char *, size_t, size_t); -void generate_random_string(char *, int) RTPP_EXPORT; /* Some handy/compat macros */ #if !defined(INFTIM) From fc2ff9cdf0ce441c8a23a61519f4cc249c351908 Mon Sep 17 00:00:00 2001 From: Maksym Sobolyev Date: Thu, 26 Nov 2026 14:13:36 -0700 Subject: [PATCH 2/2] Map ICE credential bytes with encode6() instead of a lookup table Replace the charset[] table lookup in ice_random_string() with a branch-light arithmetic mapping of 6 random bits onto 64 distinct ice-chars (+, /, A-Z, a-z, 0-9), see RFC 8445 section 15.1. --- modules/ice_lite/rtpp_ice_lite.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/modules/ice_lite/rtpp_ice_lite.c b/modules/ice_lite/rtpp_ice_lite.c index 0556012c5..5934b2aac 100644 --- a/modules/ice_lite/rtpp_ice_lite.c +++ b/modules/ice_lite/rtpp_ice_lite.c @@ -293,23 +293,34 @@ udp_local_get(const struct udp_sock *us, struct sa *local) return (0); } +static inline uint8_t +encode6(unsigned x) +{ + x &= 63u; + unsigned k = x & 31u; + + if (k == 0) + return x ? '+' : '/'; + + if (k <= 26) + return x | ('A' - 1); + + return 0x30u | ((~x & 7u) << 1) | (x >> 5); +} + /* * RFC 8445 section 5.3: ufrag needs at least 24 and pwd at least 128 bits - * of randomness. The charset has 64 entries, so each byte gives 6 bits. + * of randomness. Each byte gives 6 bits via encode6(). */ static int ice_random_string(char *buffer, int length) { - static const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789" - "+/"; unsigned char *rb = (unsigned char *)buffer; if (RAND_bytes(rb, length) != 1) return (-1); for (int i = 0; i < length; i++) - buffer[i] = charset[rb[i] % (sizeof(charset) - 1)]; + buffer[i] = encode6(rb[i]); buffer[length] = '\0'; return (0); }