无法在 CherryPy 3.8.0 上使用 SSL

Unable to use SSL on CherryPy 3.8.0

我正在尝试在 CherryPy 3.8.0 上使用 SSL。我的基本示例在 SSL 上实现了 ping 响应。

我这样设置 SSL 的配置:

# start Web Service with some configuration
global_conf = {
       "global":    { "server.environment": "production",
                      "engine.autoreload.on": True,
                      "engine.autoreload.frequency": 5,
                      "server.socket_host": "0.0.0.0",
                      "server.socket_port": 443,
                      "cherrypy.server.ssl_module": "builtin",
                      "cherrypy.server.ssl_certificate": "cert.pem",
                      "cherrypy.server.ssl_private_key": "privkey.pem",
                      "environment": "production",
                      "log.error_file": "site.log"}
}
cherrypy.config.update(global_conf)
conf = {
    "/": {
        "request.dispatch": cherrypy.dispatch.MethodDispatcher(),
        "tools.encode.debug": True,
    }
}

但是,当我调用 Web 服务时出现错误。随后是 Httpie、cURL 和 openssl 日志。

Httpie 日志:

> http GET https://<host>:443/ping
http: error: SSLError: [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:600)

cURL 日志:

> curl -v https://<host>:443/ping
* Connected to <host> (<host>) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
* error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
* Closing connection 0
curl: (35) error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

OpenSSL 日志:

> openssl s_client -host <host> -port 443
CONNECTED(00000003)
140197694400160:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:s23_clnt.c:795:
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 7 bytes and written 295 bytes
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
---

据我所知,不同版本的 CherryPy 中的 SSL 存在一些问题。问题之一: Adding support for client certificate verification in SSLAdapter (patch included)

简单示例:

import cherrypy

class RootServer:
    @cherrypy.expose
    def index(self, **keywords):
        return "it works!"

if __name__ == '__main__':
    server_config={
        'server.socket_host': '0.0.0.0',
        'server.socket_port':443,
        'server.ssl_module':'builtin',
        'server.ssl_certificate':'cert.pem',
        'server.ssl_private_key':'privkey.pem'
    }

    cherrypy.config.update(server_config)
    cherrypy.quickstart(RootServer())

有效。

可能的问题:

配置无效

从配置中删除 cherrypy. 前缀:

"server.ssl_module": "builtin",
"server.ssl_certificate": "cert.pem",
"server.ssl_private_key": "privkey.pem",

当我有 cherrypy 前缀的配置时,我有完全相同的异常。当我修复它时,一切正常。

Python 不支持 SSL

尝试安装 pyOpenSSL 并将 server.ssl_module 替换为 pyopenssl

证书无效

你确定你的证书是正确的吗?

看看http://docs.cherrypy.org/en/latest/deploy.html#ssl-support