[英]Python ssl unable to connect to TLS1.2 server with TLS1.2 client
我正在使用python3 socket + ssl服務器,用於服務器和客戶端之間的數據交換。 我已經建立了一個可與Google.com,python.org和我自己的apache2網絡服務器一起使用的客戶端。
當我啟動服務器並嘗試通過openssl s_client -connect myserver.com:8443
與openssl s_client -connect myserver.com:8443
它將返回:
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。 當我嘗試與客戶端連接時,雖然出現以下錯誤
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日,它位於Ubuntu 18.04上可用的最新版本上
我的服務器如下
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)
現在,我的客戶就是這樣建立的,
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)
,這讓我感到驚訝我找不到任何錯誤。 也許你們知道我在做什么錯以及如何解決
注意,我正在使用python 3.6和OpenSSL 1.1.0g 2017年11月2日
我對此不確定,但看起來好像支持TLSv1.2。
不,不是。
... 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。 難怪客戶對此之以鼻。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.