首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python无法与TLS1.2客户端连接到TLS1.2服务器

Python无法与TLS1.2客户端连接到TLS1.2服务器
EN

Stack Overflow用户
提问于 2019-01-19 21:05:53
回答 1查看 2.1K关注 0票数 0

我正在开发一个python3套接字+ ssl服务器,用于服务器和客户端之间的数据交换。我制作了一个与Google.com、python.org和我自己的apache2 web服务器一起工作的客户机。

当我启动服务器并尝试通过打开的ssl与openssl s_client -connect myserver.com:8443连接时,它返回:

代码语言:javascript
复制
    CONNECTED(00000003)
    140035580617152:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:../ssl/record/ssl3_record.c:252:
    ---
    no peer certificate available
    ---
    No client certificate CA names sent
    ---
    SSL handshake has read 5 bytes and written 176 bytes
    Verification: OK
    ---
    New, (NONE), Cipher is (NONE)
    Secure Renegotiation IS NOT supported
    Compression: NONE
    Expansion: NONE
    No ALPN negotiated
    SSL-Session:
        Protocol  : TLSv1.2
        Cipher    : 0000
        Session-ID: 
        Session-ID-ctx: 
        Master-Key: 
        PSK identity: None
        PSK identity hint: None
        SRP username: None
        Start Time: 1547929149
        Timeout   : 7200 (sec)
        Verify return code: 0 (ok)
        Extended master secret: no
    ---

我不确定这一点,但它似乎支持TLSv1.2。但是,当我试图与我的客户连接到它时,会出现以下错误

代码语言:javascript
复制
    Traceback (most recent call last):
      File "client-1.py", line 38, in <module>
        sock.connect((HOST, PORT))
      File "/usr/lib/python3.6/ssl.py", line 1109, in connect
        self._real_connect(addr, False)
      File "/usr/lib/python3.6/ssl.py", line 1100, in _real_connect
        self.do_handshake()
      File "/usr/lib/python3.6/ssl.py", line 1077, in do_handshake
        self._sslobj.do_handshake()
      File "/usr/lib/python3.6/ssl.py", line 689, in do_handshake
        self._sslobj.do_handshake()
    ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:847)

奇怪的是,当我使用我的客户端连接到,例如,我自己的apache2服务器(使用ssl)时,它返回TLSv1.2,这让我想知道问题是在我的客户端还是我的服务器上。

我已经尝试在服务器和客户端上使用不同的TLS / SSL版本,但到目前为止,没有一个版本起作用。我尝试过的另一件事是更新OpenSSL,到2019年1月19日,它已经在Ubuntu18.04上的最新版本上了。

我的服务器如下所示

代码语言:javascript
复制
import socket
import sys
from _thread import *
import ssl

context = ssl.SSLContext()
ssl.PROTOCOL_TLS_SERVER
#context.load_cert_chain(certfile="ssl/localhost/localhost.crt", keyfile="ssl/localhost/localhost.key")
context.load_cert_chain(certfile="ssl/certificate.crt", keyfile="ssl/private.key")


host = ''
port = 8443
print(port)

buffer = 134217700 #128 MiB

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)




def log_client_to_file(ip, port, data, file):
    #context manager
    with open(file, 'a') as f:
        f.write("User sent data from:\n\tIP: %s\n\tPort: %s\nData:\n%s\n\n" % (ip, port, data))

    #close file
    f.close()

def conn_process(buffer):
    data_tmp = conn.recv(buffer)
    data = str.encode('')
    while len(data_tmp) > 2:
        data += data_tmp
        data_tmp = conn.recv(buffer
        if len(data_tmp) < 2:
            data += data_tmp
            break
    return data

try:
    s.bind((host,port))
except socket.error as e:
    print(str(e))


s.listen(4)


print('Server is up and waiting for connection')
def client_threaded(conn, ip, port, file):
    conn.send(str.encode('Connected'))

    while True:

        data = conn_process(buffer)

        reply = 'Server output: %s' % data.decode('utf-8')
        if not data:
            break
        conn.sendall(str.encode(reply))
        log_client_to_file(ip, port, data, file)

while True:
    conn, addr = s.accept()
    print('connected to: %s:%s' % (addr[0], str(addr[1])))


    start_new_thread(client_threaded, (conn, addr[0], str(addr[1]), 'connections.log'))

s.close()


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

现在,我的客户就像这样,

代码语言:javascript
复制
import socket
import ssl

HOST = 'myserver.com'
PORT = 8443


args = ssl.SSLContext()
ssl.PROTOCOL_TLS_CLIENT
args.verify_mode = ssl.CERT_NONE
args.check_hostname = False
#ssl.ca_certs="ssl/ca_bundle.crt",

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock = args.wrap_socket(s, server_hostname=HOST)

sock.connect((HOST, PORT))

print(sock.version())

注意:由于我正在为测试目的使用自签名证书,所以我还没有验证它们。

由于客户机和服务器都使用TLS,所以我希望连接不是一个问题,但是我一直得到前面提到的错误,ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:847),这让我感到惊讶,因为我找不到任何错误。也许你们知道我做错了什么

注意,我使用python3.6和OpenSSL 1.1.0g 2017年11月2日

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-20 06:16:17

我不确定这一点,但它似乎支持TLSv1.2。

不,不需要。

代码语言:javascript
复制
... ssl3_get_record:wrong version number:../ssl/record/ssl3_record.c:252:
...
SSL handshake has read 5 bytes and written 176 bytes
...
   Protocol  : TLSv1.2
   Cipher    : 0000

这表明客户端使用ClientHello (176个字节)启动TLS握手,并从服务器获得5个字节,这对于TLS握手内部的预期响应来说太短了。这5个字节不包含TLS版本号,这就是为什么它使用错误的版本号的原因。没有普通密码:Cipher : 0000也表示了这一点。

从服务器的代码来看,在我看来,您在一开始就设置了一些SSL上下文(所显示的代码似乎也被破坏了),但从未真正使用它,也就是说,您的服务器只是简单的TCP。难怪客户对此嗤之以鼻。

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

https://stackoverflow.com/questions/54271387

复制
相关文章

相似问题

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