已更新:問題底部的固定/工作代碼。重新實施以使用wrap_socket()
而不是SSL.Context()
。協商的密碼似乎被ssl.PROTOCOL_
爲什麼這個Python代碼不能通過SSL連接?
不受影響我有它偵聽端口8388.服務有它自己的CA內置的,將發出有關其配置的任何客戶端P12文件的XML服務的管理界面(我需要包括在我的連接中有一個CA證書)。
我配置了一個客戶端並下載了P12文件;還從P12導出PEM文件(以便於調試)併爲服務器CA(這是一個Java密鑰庫)導出PEM文件。
如果我使用openssl s_client
並將其提供給客戶端的證書/密鑰和CA文件,事情似乎正常工作;即:驗證返回:1在下面的截斷輸出中。 openssl進程不會在服務器上生成任何證書錯誤(就像我的Python腳本一樣)。從我讀過的內容來看,這應該意味着證書都可以正確有效。
# openssl s_client -connect srv.domain.net:8388 -CAfile server_ca.pem -cert client_cert.pem -key client_key.pem
CONNECTED(00000003)
depth=1 ... emailAddress = [email protected]
verify return:1
depth=0 ...CN = srv.domain.net
verify return:1
---
Certificate chain
0 s:/[email protected]=srv.domain.net
i:/C=US/[email protected]
---
Server certificate
-----BEGIN CERTIFICATE-----
MIIJRDCCByygAwIBAgIGAVGIopaoMA0GCSqGSIb3DQEBDQUAMIG...rV
-----END CERTIFICATE-----
subject=/[email protected]=srv.domain.net
issuer=/C=US/[email protected]
---
Acceptable client certificate CA names
/C=US/[email protected]
/.../CN=srv.domain.net
---
SSL handshake has read 3369 bytes and written 5705 bytes
---
New, TLSv1/SSLv3, Cipher is ECDHE-RSA-AES256-GCM-SHA384
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
SSL-Session:
Protocol : TLSv1.2
Cipher : ECDHE-RSA-AES256-GCM-SHA384
Session-ID: ...DF6572FA00D62D83CF0B1A82F
Session-ID-ctx:
Master-Key: ...7F685C0B163A7739C271E9722FC0554108175C4
Key-Arg : None
PSK identity: None
PSK identity hint: None
SRP username: None
Start Time: 1461337153
Timeout : 300 (sec)
Verify return code: 0 (ok)
---
我現在試圖鉤住一個Python(2.7.9)腳本使用相同的證書,密鑰和CA文件的XML服務,但我不能讓它工作。 Python抱怨SSLv3錯誤,服務器說它無法驗證客戶端。所以,連接工作,但握手,證書或密碼或不正確的東西。
我已經搜索了很多例子和實現,這一個似乎是最簡單的,所以我開始使用它的模板。 SSL3並不是我會堅持使用的協議(POODLE),但它應該來自一個工作示例,所以我想盡可能地做少許更改,以便能夠正常工作,然後從中調整。任何人都知道我在這裏有什麼問題嗎?輸出/錯誤/日誌發佈在最底部。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import socket
import OpenSSL
from OpenSSL import *
import sys
serverName = sys.argv[1]
print "Using server : " + serverName
ctx = SSL.Context(SSL.SSLv3_METHOD)
ctx.use_privatekey_file('client_key.pem')
ctx.use_certificate_file('client_cert.pem')
ctx.load_verify_locations(cafile='server_ca.pem')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((serverName, 8388))
sslSocket = socket.ssl(s)
print repr(sslSocket.server())
print repr(sslSocket.issuer())
print ("writing socket..")
sslSocket.write('<transaction><data>14</data></transaction>\n')
s.close()
Python的輸出:上述連接後
Using server : localhost
Traceback (most recent call last):
File "./test3.py", line 29, in <module>
sslSocket = socket.ssl(s)
File "/usr/lib/python2.7/socket.py", line 64, in ssl
return _realssl.sslwrap_simple(sock, keyfile, certfile)
File "/usr/lib/python2.7/ssl.py", line 993, in sslwrap_simple
ssl_sock.do_handshake()
ssl.SSLError: [SSL: SSLV3_ALERT_BAD_CERTIFICATE] sslv3 alert bad certificate (_ssl.c:581)
服務器日誌:
Apr 22 10:39:56 srv.domain.net ERROR server.auth [Thread-183,run:233] Couldn't validate the client certificate. Verify the validity and dates of the client cert.
Apr 22 10:39:56 srv.domain.net javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
Apr 22 10:39:56 srv.domain.net at sun.security.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:431)
Apr 22 10:39:56 srv.domain.net at com.xml.server.auth.run(auth.java:226)
Apr 22 10:39:56 srv.domain.net at java.lang.Thread.run(Thread.java:745)
工作代碼:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import socket
import ssl
import sys
import os
serverName = sys.argv[1]
print "Using server : " + serverName
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# ssl.PROTOCOL_xxx does not seem to affect negotiated cipher??
wrapper = ssl.wrap_socket(s,
ca_certs = "server_ca.pem",
certfile = "client_cert.pem",
keyfile = "client_key.pem",
cert_reqs=ssl.CERT_REQUIRED,
ssl_version=ssl.PROTOCOL_TLSv1)
wrapper.connect((serverName, 8388))
# some info on the connnection/cipher in use
print repr(wrapper.getpeername())
print repr(wrapper.cipher())
print repr(wrapper.getpeercert())
# send server command
print ("writing socket..")
wrapper.send ("<transaction><data>14</data></transaction>\n")
# read server reply
print "server reply: " + wrapper.recv(4096)
wrapper.close()
s.close()
也許這不是問題的原因,但您將非工作python代碼限制爲SSL 3.0,而openssl成功使用TLS 1.2。請刪除此限制後再試。 –
你指的是'ctx = SSL.Context(SSL.SSLv3_METHOD)'?無論我設置了什麼協議(SSL.SSLv23_METHOD,TLSv1_METHOD,TLSv1_2_METHOD),我仍然會得到相同的'sslv3警報錯誤證書'錯誤。我感到困惑..服務器是否支配這一點? – Koko
從我的理解中,服務器不想接受您的客戶端證書。所以至少你現在知道這個錯誤不是特定於SSL/TLS版本的。 –