From: Willy Tarreau Date: Mon, 12 Oct 2026 18:31:19 +0000 (+0000) Subject: MINOR: ssl: add alpn_remove_token() to drop a protocol from an ALPN string X-Git-Tag: v3.5-dev13~44 X-Git-Url: https://git.kaiwu.me/web/%22data:,/doc/static/gitweb.js?a=commitdiff_plain;h=94ab3dd9f6f47deb8ff1274178e589b953f2c42c;p=haproxy.git MINOR: ssl: add alpn_remove_token() to drop a protocol from an ALPN string This function removes all occurrences of a given protocol name from an ALPN string in the binary form produced by ssl_sock_parse_alpn(), in place, and updates its length. The string remains zero-terminated so that the existing strstr() checks on it keep working, and it may end up empty, which is then equivalent to "no-alpn". It returns the number of entries removed. It will be used by future patches to globally disable some protocols from frontend ALPN strings at boot. --- diff --git a/include/haproxy/ssl_utils.h b/include/haproxy/ssl_utils.h index 1d1b16966..392436c8f 100644 --- a/include/haproxy/ssl_utils.h +++ b/include/haproxy/ssl_utils.h @@ -47,6 +47,7 @@ const char *x509_v_err_int_to_str(int code); long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d); const char *x509_get_notbefore(X509 *cert); const char *x509_get_notafter(X509 *cert); +int alpn_remove_token(char *alpn, int *len, const struct ist token); #ifdef HAVE_ASN1_TIME_TO_TM time_t ASN1_to_time_t(ASN1_TIME *asn1_time); time_t x509_get_notafter_time_t(X509 *cert); diff --git a/src/ssl_utils.c b/src/ssl_utils.c index 431b5d47c..ef5e6fc0c 100644 --- a/src/ssl_utils.c +++ b/src/ssl_utils.c @@ -842,6 +842,38 @@ end: return NULL; } +/* Removes all occurrences of protocol from the ALPN string + * whose length is <*len>, in the binary format produced by + * ssl_sock_parse_alpn() (each name preceded by its length byte). The string + * is modified in place, remains zero-terminated and <*len> is updated. It may + * end up empty, which is then equivalent to "no-alpn". Returns the number of + * entries removed. + */ +int alpn_remove_token(char *alpn, int *len, const struct ist token) +{ + int pos = 0, removed = 0; + int next; + + while (pos < *len) { + next = pos + 1 + (uchar)alpn[pos]; + + /* truncated entry, should never happen */ + if (next > *len) + break; + + if ((uchar)alpn[pos] == istlen(token) && + memcmp(alpn + pos + 1, istptr(token), istlen(token)) == 0) { + memmove(alpn + pos, alpn + next, *len - next); + *len -= next - pos; + alpn[*len] = '\0'; + removed++; + } + else + pos = next; + } + return removed; +} + #ifdef HAVE_ASN1_TIME_TO_TM /* Takes a ASN1_TIME and converts it into a time_t */ time_t ASN1_to_time_t(ASN1_TIME *asn1_time)