Blame SOURCES/00298-do-not-send-IP-in-SNI-TLS-extension.patch

28b261
diff --git a/Modules/_ssl.c b/Modules/_ssl.c
28b261
index d0a3830..51b192c 100644
28b261
--- a/Modules/_ssl.c
28b261
+++ b/Modules/_ssl.c
28b261
@@ -50,6 +50,11 @@
28b261
 #include <sys/poll.h>
28b261
 #endif
28b261
 
28b261
+#ifndef MS_WINDOWS
28b261
+/* inet_pton */
28b261
+#include <arpa/inet.h>
28b261
+#endif
28b261
+
28b261
 /* Include OpenSSL header files */
28b261
 #include "openssl/rsa.h"
28b261
 #include "openssl/crypto.h"
28b261
@@ -493,8 +498,41 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
28b261
     SSL_set_mode(self->ssl, mode);
28b261
 
28b261
 #if HAVE_SNI
28b261
-    if (server_hostname != NULL)
28b261
-        SSL_set_tlsext_host_name(self->ssl, server_hostname);
28b261
+    if (server_hostname != NULL) {
28b261
+/* Don't send SNI for IP addresses. We cannot simply use inet_aton() and
28b261
+ * inet_pton() here. inet_aton() may be linked weakly and inet_pton() isn't
28b261
+ * available on all platforms. Use OpenSSL's IP address parser. It's
28b261
+ * available since 1.0.2 and LibreSSL since at least 2.3.0. */
28b261
+        int send_sni = 1;
28b261
+#if OPENSSL_VERSION_NUMBER >= 0x10200000L
28b261
+        ASN1_OCTET_STRING *ip = a2i_IPADDRESS(server_hostname);
28b261
+        if (ip == NULL) {
28b261
+            send_sni = 1;
28b261
+            ERR_clear_error();
28b261
+        } else {
28b261
+            send_sni = 0;
28b261
+            ASN1_OCTET_STRING_free(ip);
28b261
+        }
28b261
+#elif defined(HAVE_INET_PTON)
28b261
+#ifdef ENABLE_IPV6
28b261
+        char packed[Py_MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
28b261
+#else
28b261
+        char packed[sizeof(struct in_addr)];
28b261
+#endif /* ENABLE_IPV6 */
28b261
+        if (inet_pton(AF_INET, server_hostname, packed)) {
28b261
+            send_sni = 0;
28b261
+#ifdef ENABLE_IPV6
28b261
+        } else if(inet_pton(AF_INET6, server_hostname, packed)) {
28b261
+            send_sni = 0;
28b261
+#endif /* ENABLE_IPV6 */
28b261
+        } else {
28b261
+            send_sni = 1;
28b261
+        }
28b261
+#endif /* HAVE_INET_PTON */
28b261
+        if (send_sni) {
28b261
+            SSL_set_tlsext_host_name(self->ssl, server_hostname);
28b261
+        }
28b261
+    }
28b261
 #endif
28b261
 
28b261
     /* If the socket is in non-blocking mode or timeout mode, set the BIO