Skip to content

Commit

Permalink
Add ALPN validation in the client
Browse files Browse the repository at this point in the history
The ALPN protocol selected by the server must be one that we originally
advertised. We should verify that it is.

Follow on from CVE-2024-5535

(cherry picked from commit 4b375b998798dd516d367036773073e1b88e6433)
  • Loading branch information
dongbeiouba committed Jul 15, 2024
1 parent eef69d9 commit e0564b9
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions ssl/statem/extensions_clnt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,8 @@ int tls_parse_stoc_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
size_t chainidx)
{
size_t len;
PACKET confpkt, protpkt;
int valid = 0;

/* We must have requested it. */
if (!s->s3->alpn_sent) {
Expand All @@ -1771,6 +1773,30 @@ int tls_parse_stoc_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
SSL_R_BAD_EXTENSION);
return 0;
}

/* It must be a protocol that we sent */
if (!PACKET_buf_init(&confpkt, s->ext.alpn, s->ext.alpn_len)) {
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
ERR_R_INTERNAL_ERROR);
return 0;
}
while (PACKET_get_length_prefixed_1(&confpkt, &protpkt)) {
if (PACKET_remaining(&protpkt) != len)
continue;
if (memcmp(PACKET_data(pkt), PACKET_data(&protpkt), len) == 0) {
/* Valid protocol found */
valid = 1;
break;
}
}

if (!valid) {
/* The protocol sent from the server does not match one we advertised */
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
SSL_R_BAD_EXTENSION);
return 0;
}

OPENSSL_free(s->s3->alpn_selected);
s->s3->alpn_selected = OPENSSL_malloc(len);
if (s->s3->alpn_selected == NULL) {
Expand Down

0 comments on commit e0564b9

Please sign in to comment.