首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何阻止SSL协议以支持TLS?

如何阻止SSL协议以支持TLS?
EN

Stack Overflow用户
提问于 2015-03-25 16:15:29
回答 2查看 3.5K关注 0票数 4

如何在PyOpenSSL中阻止SSL协议以支持TLS?我正在使用CentOS 7并拥有以下版本:

代码语言:javascript
复制
pyOpenSSL-0.13.1-3.el7.x86_64
openssl-1.0.1e-34.el7_0.7.x86_64

在我的配置文件(如果用于CherryPy应用程序)中,我有:

代码语言:javascript
复制
'server.ssl_module': 'pyopenssl',
EN

回答 2

Stack Overflow用户

发布于 2015-03-27 15:12:32

对于今天的CherryPy来说,这是一个很好的问题。本月,我们开始讨论SSL问题以及CherryPy的包装器在py2.6+、ssl和pyOpenSSL ( CherryPy用户组 )上的总体可维护性。我正在计划一个关于SSL问题的主题,以便您可以订阅这个组,以便稍后获得更多的详细信息。

就目前而言,这是可能的。我有Debian,Python2.7.3-4+Debian 7u 1,OpenSSL 1.0.1e-2+Debian 7u 16。我已经安装了回购程序中的CherryPy (3.6ssl)和pyOpenSSL 0.14。我试图覆盖这两个CherryPy SSL适配器,以便在Qualys实验室测试中获得一些积分。它非常有用,我强烈建议您用它测试您的部署(不管您的前端是什么,CherryPy与否)。

因此,ssl-based适配器仍然存在漏洞,在py2 < 2.7.9 (大规模SSL )和py3 < 3.3中无法解决这些漏洞。因为CherryPy ssl适配器早在这些更改之前就已经编写好了,所以它需要重写以支持新旧方式(主要是SSL上下文)。另一方面,对子类pyOpenSSL进行了改编,它基本上是很好的,除了:

这是密码。

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os
import sys
import ssl

import cherrypy
from cherrypy.wsgiserver.ssl_builtin import BuiltinSSLAdapter
from cherrypy.wsgiserver.ssl_pyopenssl import pyOpenSSLAdapter

from cherrypy import wsgiserver
if sys.version_info < (3, 0):
  from cherrypy.wsgiserver.wsgiserver2 import ssl_adapters  
else:
  from cherrypy.wsgiserver.wsgiserver3 import ssl_adapters

try:
  from OpenSSL import SSL
except ImportError:
  pass


ciphers = (
  'ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+HIGH:'
  'DH+HIGH:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+HIGH:RSA+3DES:!aNULL:'
  '!eNULL:!MD5:!DSS:!RC4:!SSLv2'
)

bundle = os.path.join(os.path.dirname(cherrypy.__file__), 'test', 'test.pem')

config = {
  'global' : {
    'server.socket_host' : '127.0.0.1',
    'server.socket_port' : 8443,
    'server.thread_pool' : 8,

    'server.ssl_module'      : 'custom-pyopenssl',
    'server.ssl_certificate' : bundle,
    'server.ssl_private_key' : bundle,
  }
}


class BuiltinSsl(BuiltinSSLAdapter):
  '''Vulnerable, on py2 < 2.7.9, py3 < 3.3:
    * POODLE (SSLv3), adding ``!SSLv3`` to cipher list makes it very incompatible
    * can't disable TLS compression (CRIME)
    * supports Secure Client-Initiated Renegotiation (DOS)
    * no Forward Secrecy
  Also session caching doesn't work. Some tweaks are posslbe, but don't really 
  change much. For example, it's possible to use ssl.PROTOCOL_TLSv1 instead of 
  ssl.PROTOCOL_SSLv23 with little worse compatiblity.
  '''

  def wrap(self, sock):
    """Wrap and return the given socket, plus WSGI environ entries."""
    try:
      s = ssl.wrap_socket(
        sock, 
        ciphers = ciphers, # the override is for this line
        do_handshake_on_connect = True,
        server_side = True, 
        certfile = self.certificate,
        keyfile = self.private_key,
        ssl_version = ssl.PROTOCOL_SSLv23
      )
    except ssl.SSLError:
      e = sys.exc_info()[1]
      if e.errno == ssl.SSL_ERROR_EOF:
        # This is almost certainly due to the cherrypy engine
        # 'pinging' the socket to assert it's connectable;
        # the 'ping' isn't SSL.
        return None, {}
      elif e.errno == ssl.SSL_ERROR_SSL:
        if e.args[1].endswith('http request'):
          # The client is speaking HTTP to an HTTPS server.
          raise wsgiserver.NoSSLError
        elif e.args[1].endswith('unknown protocol'):
          # The client is speaking some non-HTTP protocol.
          # Drop the conn.
          return None, {}
      raise

    return s, self.get_environ(s)

ssl_adapters['custom-ssl'] = BuiltinSsl


class Pyopenssl(pyOpenSSLAdapter):
  '''Mostly fine, except:
    * Secure Client-Initiated Renegotiation
    * no Forward Secrecy, SSL.OP_SINGLE_DH_USE could have helped but it didn't
  '''

  def get_context(self):
    """Return an SSL.Context from self attributes."""
    c = SSL.Context(SSL.SSLv23_METHOD)

    # override:
    c.set_options(SSL.OP_NO_COMPRESSION | SSL.OP_SINGLE_DH_USE | SSL.OP_NO_SSLv2 | SSL.OP_NO_SSLv3)
    c.set_cipher_list(ciphers)

    c.use_privatekey_file(self.private_key)
    if self.certificate_chain:
        c.load_verify_locations(self.certificate_chain)
    c.use_certificate_file(self.certificate)
    return c

ssl_adapters['custom-pyopenssl'] = Pyopenssl


class App:

  @cherrypy.expose
  def index(self):
    return '<em>Is this secure?</em>'


if __name__ == '__main__':
  cherrypy.quickstart(App(), '/', config)

更新

这里是文章讨论,应该决定CherryPy的SSL支持的未来。

票数 1
EN

Stack Overflow用户

发布于 2015-03-27 00:26:56

我知道有两种方法可以做到这一点。一个是配置选项,另一个是运行时选项。

配置选项

在构建OpenSSL时使用配置选项。它适用于所有应用程序,因为它应用了您的管理策略,并解决了与SSL/TLS相关的问题不关心的应用程序。

对于此选项,只需使用OpenSSL配置no-ssl2 no-ssl3即可。no-comp也经常被使用,因为压缩会泄漏信息。

代码语言:javascript
复制
./Configure no-ssl2 no-ssl3 <other opts>

还有其他OpenSSL选项可用,您可能希望访问OpenSSL的wiki上的编译和安装

运行时选项

在C中,您必须(1)使用2/3方法获得SSL2/3和更高版本;然后(2)调用SSL_CTX_set_options (或SSL_set_options)和(3)删除SSL协议。剩下的是TLS协议:

代码语言:javascript
复制
SSL_CTX* ctx = SSL_CTX_new(SSLv23_method());
const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
SSL_CTX_set_options(ctx, flags);

在Python中,您可以使用OpenSSL.SSL.Context.set_options来完成它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29260947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档