使用DHE + AESGCM密码套件时,Python 3.7(OpenSSL 1.1.0i)客户端和服务器无法建立SSL / TLS连接。
我正在尝试使用DHE-RSA-AES128-GCM-SHA256
创建SSL / TLS连接,但未建立连接,并且服务器向我显示“无共享密码”异常:
INFO:root:Listening on 127.0.0.1:8765
INFO:root:Cipher suites:
TLSv1.2 DHE-DSS-AES256-GCM-SHA384
TLSv1.2 DHE-RSA-AES256-GCM-SHA384
TLSv1.2 DHE-DSS-AES128-GCM-SHA256
TLSv1.2 DHE-RSA-AES128-GCM-SHA256
INFO:root:Connection from 127.0.0.1:20938
ERROR:root:[SSL: NO_SHARED_CIPHER] no shared cipher (_ssl.c:1051)
在客户端,它向我显示“ sslv3警报握手失败”:
INFO:root:Connecting to 127.0.0.1:8765
ERROR:root:[SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:1051)
此问题并非特定于DHE-RSA-AES128-GCM-SHA256
,而是所有DHE + AESGCM密码。
服务器:
#!python
# -*- coding: utf-8 -*-
import logging
import sys
import ssl
import socket
SERVER_ADDRESS = ('127.0.0.1', 8765)
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
def main():
ssl_context = ssl.SSLContext()
ssl_context.set_ciphers('DHE+AESGCM')
ssl_context.load_cert_chain(certfile='D:/tls/server-cer.pem', keyfile='D:/tls/server-prk.pem')
server_host, server_port = SERVER_ADDRESS
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
server_socket.bind((server_host, server_port))
server_socket.listen(1)
logging.info(f'Listening on {server_host}:{server_port}')
# noinspection PyUnresolvedReferences
ciphers = "\n\t".join(f'{i["protocol"]} {i["name"]}' for i in ssl_context.get_ciphers())
logging.info(f'Cipher suites:\n\t{ciphers}')
while True:
try:
client_socket, client_address = server_socket.accept()
client_host, client_port = client_address
logging.info(f'Connection from {client_host}:{client_port}')
with client_socket:
with ssl_context.wrap_socket(client_socket, server_side=True) as client_ssl_socket:
tls_cipher, tls_version, _ = client_ssl_socket.cipher()
logging.info(f'Cipher suite: {tls_version} {tls_cipher}')
except ssl.SSLError as ssl_error:
logging.error(ssl_error)
if __name__ == '__main__':
main()
服务器证书:
-----BEGIN CERTIFICATE-----
MIICyDCCAbACCQDcFqxqUss0ezANBgkqhkiG9w0BAQsFADAaMQswCQYDVQQGEwJV
UzELMAkGA1UECgwCQ0EwHhcNMTkwMTI0MTAxMjQzWhcNMjAwMTI0MTAxMjQzWjAy
MQswCQYDVQQGEwJVUzEPMA0GA1UECgwGU2VydmVyMRIwEAYDVQQDDAkxMjcuMC4w
LjEwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDQWqIp1mFuhZJPr/E/
8RWPiu+jPygmoSqNy+60Gq8hTduZFZyWbQqLWtS41Dg2zUzgatIJjJviYpLW6mGb
xlUyUFmwn4vJHS4aNE15KIEpHwkM8EikZtGDfUI6Y98yCz+soFy2J4z8VEYUoXKg
VD1Pri3i/G+EwTL9RB3hGeSjX1mYB0NwKqxcgBJI6oodCYjocT7gMjN2KMjnvtyC
Dhk09HUDpPqi/DPwFU1irkXF5OrbvG4le0tFViEnQ0frdW+dHNfUZxx139/mBk8l
HGdelf0DiOiIgJRYIgunVFO1Dnwqx1Wx0/pDBlwiA3Whwc2DP6/19EAynNh9gkNA
wmFJAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAHZWRARJf/MlqKqkSQC8IumTujKA
pzF8JFti3jrTj1U4JERsu18pgXxm1Fuz94jjvVysNyDF4s6uiJQqgkruGk48pXNW
3o6/OtDRSy29QLIAwpwbBGTgD90FhgxnpKkq0S+lZym9Jug0wprSeviaUu3yzarx
onMdYndi/NNsGqc3zp4WalP0eGD8+VZqr6NgXaaqxqYdyFcdzV4DuC/rouQsJKTq
7nMqmE45fk80QlnGa1X3qeRrzOl070q5ZN2lgb4UKpVrD2utPr8ZnfStQmR62jKv
lwm0hQulsL0JzJEv74NYjwR7ewBe1D1rNuItJFQRErQMRl2gLyFHoeLI0ps=
-----END C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R+GqerVktWo0QfouBO
/Bxs6TcwqevoX3pmZ6o1AmJiLUy2gyOzWpkdQkcCgYB0g2jU7vMqgO8gYZ7duP9/
3/TrEvGpd/LPjSZAXWpPwUXxyIBaRIFd+fBzd8gEApWBur2uFxBcc9ipcK7VEyi1
aMOB3ey3jhzmcpuSwTRXOcClauFG+g0LhLnKDWexP3IjaWNYy5ciDSkDjXKMZOk7
7hRcCrUClUyFeMlDvTBC3wKBgQCJpIJwLyZrlFW8yhf/+gs2eR3HsGb0bBCHpGyI
MNKnZHH51Gqk3p4afKVkFh0kHW7blxb/CZv07fF38mgA4btYFt7G3MbsfU+U/xRC
8cKAJfOdPoN5hYUU5Yq89AUDAUsFXWjt5/rB2rFnazEOODNjzwWQ+wg8WMTKcoQJ
S8XvbQKBgFZxoVPDNN/C1dYoLAFs3dmrUkMorbSblqhTwgsXk5T79XrKvEP0FPV1
bqDaX4kUXkcCy0IGutaAKvShZNx3qA/uc2COL0Z6oew8WjOLgmJz8nMxsiGscwtI
Y3tKp4MJP8eBKQwtz82fgIoh2l7NITkFyQM1iFdodk4XAPyu6Ndw
-----END RSA PRIVATE KEY-----
客户端:
#!python
# -*- coding: utf-8 -*-
import logging
import sys
import ssl
import socket
SERVER_ADDRESS = ('127.0.0.1', 8765)
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
def main():
ssl_context = ssl.SSLContext()
ssl_context.set_ciphers('DHE+AESGCM')
ssl_context.load_verify_locations(cafile='D:/tls/ca-cer.pem')
server_host, server_port = SERVER_ADDRESS
try:
with socket.create_connection((server_host, server_port)) as client_socket:
logging.info(f'Connecting to {server_host}:{server_port}')
with ssl_context.wrap_socket(client_socket, server_hostname=server_host) as client_ssl_socket:
logging.info(f'Connection from {client_ssl_socket.version()} {client_ssl_socket.cipher()})')
except ssl.SSLError as ssl_error:
logging.error(ssl_error)
except ConnectionRefusedError as connection_refused_error:
logging.error(connection_refused_error)
if __name__ == '__main__':
main()
CA证书:
-----BEGIN CERTIFICATE-----
MIICsDCCAZgCCQDYzJy3j6+xezANBgkqhkiG9w0BAQsFADAaMQswCQYDVQQGEwJV
UzELMAkGA1UECgwCQ0EwHhcNMTkwMTI0MTAxMjAwWhcNMjkwMTIxMTAxMjAwWjAa
MQswCQYDVQQGEwJVUzELMAkGA1UECgwCQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IB
DwAwggEKAoIBAQCeJHozhpcJ+ySesan+ohHCgScmdy29cDf1P7vksfyxe1oU0bI7
1DEq9OhL4F0Z+hAtWPPx25LzMG5TpuhxoZ1zJbyW5g7EXIp5R5cI3jWb8lxRB8kL
CsrB0HmRVZA8uYyBiGxfNYIRpzbpMZf/9knpH3bD8S/c+5Ru6wrp+0fCQr3w2n8g
7ivyYWa81jDeuUAn7SOZZNzLn5niJv8f1mZtl3fpPTnvlajzeO6m60KHwxnZIC58
RwIf8qPfu1fdfyVwaeoYRCmoU8RKtq+e0CZ+kaSoGyWZRpuJX+Tis7YEzt9g9+Sq
1NmcLUM+B5DvWPF0pFixq34yRDAQ+QL4oO6lAgMBAAEwDQYJKoZIhvcNAQELBQAD
ggEBAA3O0VAtSYLtLqIkfIhvoF4qYuci+yLETXQ77EThHlvkMEqGNncdWBw4is5g
5L/atbnj12EzA9SEfxvzWe3quoRJHLSlY5uwCWLMqyOJ2PrViUTVx2BKYz4oEQjT
qzINTrtNIrw4lEveVR4rb/5X1OcUVneZbQNPLXzJVHigmNOKyCcPqN5XQZwoJMFM
4BKbvOtTNHPGBy2QZuLlqnmw43ldWTQU0N4dxW3OLRU44OwzUHbNdTxsqtbF8HcE
EEd8j1qae4xAiXjzT/NjlY73qJSaiSE9HWKR3mwWziAe/uNUa036WROgzAQLzKj2
LCG/Y/5ik0vREZNmz1VAmsCooiE=
-----END CERTIFICATE-----
我找不到未建立连接的原因。 OpenSSL支持DHE + AESGCM密码,并且客户端和服务器都运行相同版本的Python和OpenSSL。