Skip to content

Commit a5c9112

Browse files
miss-islingtontiran
andcommitted
[2.7] bpo-32185: Don't send IP in SNI TLS extension (GH-5865) (#5871)
The SSL module no longer sends IP addresses in SNI TLS extension on platforms with OpenSSL 1.0.2+ or inet_pton. Signed-off-by: Christian Heimes <christian@python.org> (cherry picked from commit e9370a4) Co-authored-by: Christian Heimes <christian@python.org>
1 parent 6e8f395 commit a5c9112

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The SSL module no longer sends IP addresses in SNI TLS extension on
2+
platforms with OpenSSL 1.0.2+ or inet_pton.

Modules/_ssl.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@
5252
#include <sys/poll.h>
5353
#endif
5454

55+
#ifndef MS_WINDOWS
56+
/* inet_pton */
57+
#include <arpa/inet.h>
58+
#endif
59+
5560
/* Don't warn about deprecated functions */
5661
#ifdef __GNUC__
5762
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
@@ -575,8 +580,41 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
575580
SSL_set_mode(self->ssl, mode);
576581

577582
#if HAVE_SNI
578-
if (server_hostname != NULL)
579-
SSL_set_tlsext_host_name(self->ssl, server_hostname);
583+
if (server_hostname != NULL) {
584+
/* Don't send SNI for IP addresses. We cannot simply use inet_aton() and
585+
* inet_pton() here. inet_aton() may be linked weakly and inet_pton() isn't
586+
* available on all platforms. Use OpenSSL's IP address parser. It's
587+
* available since 1.0.2 and LibreSSL since at least 2.3.0. */
588+
int send_sni = 1;
589+
#if OPENSSL_VERSION_NUMBER >= 0x10200000L
590+
ASN1_OCTET_STRING *ip = a2i_IPADDRESS(server_hostname);
591+
if (ip == NULL) {
592+
send_sni = 1;
593+
ERR_clear_error();
594+
} else {
595+
send_sni = 0;
596+
ASN1_OCTET_STRING_free(ip);
597+
}
598+
#elif defined(HAVE_INET_PTON)
599+
#ifdef ENABLE_IPV6
600+
char packed[Py_MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
601+
#else
602+
char packed[sizeof(struct in_addr)];
603+
#endif /* ENABLE_IPV6 */
604+
if (inet_pton(AF_INET, server_hostname, packed)) {
605+
send_sni = 0;
606+
#ifdef ENABLE_IPV6
607+
} else if(inet_pton(AF_INET6, server_hostname, packed)) {
608+
send_sni = 0;
609+
#endif /* ENABLE_IPV6 */
610+
} else {
611+
send_sni = 1;
612+
}
613+
#endif /* HAVE_INET_PTON */
614+
if (send_sni) {
615+
SSL_set_tlsext_host_name(self->ssl, server_hostname);
616+
}
617+
}
580618
#endif
581619

582620
/* If the socket is in non-blocking mode or timeout mode, set the BIO

0 commit comments

Comments
 (0)