首页 > 解决方案 > 在 FreeBSD 上使用旧 python 版本 (2.7.3) 的 SSL

问题描述

我将旧版本的 Python (2.7.3) 从源代码安装到目录中(不是系统的默认 python),以便偶尔运行旧应用程序。我想用 pip 或 easy_install 配置这个环境,但这不起作用,因为需要 SSL,我不能用 ssl 构建这个版本。构建工作,但我得到了消息

Failed to build these modules:
_ctypes            _hashlib           _ssl

已安装 OpenSSL(它是一台 FreeBSD 机器)。从源代码构建 Python 2.7.15 并安装这些模块。

我的猜测是 2.7.3 可能需要旧版本的 OpenSSL(我安装了 1.1.1a-freebsd)。

如何在启用 SSL 的情况下构建这个 Python 版本?(我设法安装了 setuptools,但没有 SSL 就无法使用它们)

标签: pythonpython-2.7opensslsetuptoolsfreebsd

解决方案


使用更新的 OpenSSL 构建 Python 2.7 的补丁:

--- Modules/_ssl.c.orig 2018-03-05 01:25:37.803984781 +0300
+++ Modules/_ssl.c  2018-03-05 01:25:04.499198913 +0300
@@ -300,8 +300,10 @@
     PySSL_BEGIN_ALLOW_THREADS
     if (proto_version == PY_SSL_VERSION_TLS1)
         self->ctx = SSL_CTX_new(TLSv1_method()); /* Set up context */
+#ifndef OPENSSL_NO_SSL3
     else if (proto_version == PY_SSL_VERSION_SSL3)
         self->ctx = SSL_CTX_new(SSLv3_method()); /* Set up context */
+#endif
 #ifndef OPENSSL_NO_SSL2
     else if (proto_version == PY_SSL_VERSION_SSL2)
         self->ctx = SSL_CTX_new(SSLv2_method()); /* Set up context */

--- Lib/ssl.py.orig 2017-09-19 10:32:02.000000000 +0300
+++ Lib/ssl.py  2018-03-05 01:38:26.358119752 +0300
@@ -91,14 +91,13 @@
     SSL_ERROR_INVALID_ERROR_CODE,
     )
 from _ssl import HAS_SNI, HAS_ECDH, HAS_NPN
-from _ssl import (PROTOCOL_SSLv3, PROTOCOL_SSLv23,
+from _ssl import (PROTOCOL_SSLv23,
                   PROTOCOL_TLSv1)
 from _ssl import _OPENSSL_API_VERSION

 _PROTOCOL_NAMES = {
     PROTOCOL_TLSv1: "TLSv1",
     PROTOCOL_SSLv23: "SSLv23",
-    PROTOCOL_SSLv3: "SSLv3",
 }
 try:
     from _ssl import PROTOCOL_SSLv2
@@ -664,7 +663,7 @@
     d = pem_cert_string.strip()[len(PEM_HEADER):-len(PEM_FOOTER)]
     return base64.decodebytes(d.encode('ASCII', 'strict'))

-def get_server_certificate(addr, ssl_version=PROTOCOL_SSLv3, ca_certs=None):
+def get_server_certificate(addr, ssl_version=PROTOCOL_TLSv1, ca_certs=None):
     """Retrieve the certificate from the server at the specified address,
     and return it as a PEM-encoded string.
     If 'ca_certs' is specified, validate the server cert against it.

我在 Debian 9 下使用 OpenSSL 1.1.0j 编译了 Python 2.7.10。


推荐阅读