[英]How to accept self-signed certificate from e-mail server via smtplib (TSL)?
[英]Login via starttls method from smtplib to old e-mail server
我使用:Python 2.7.15,OpenSSL 1.1.0h(2018年3月27日),MS Exchange 2007。
我的MS交換僅允許在STARTTLS之后發送登錄名/密碼。
在python中,我嘗試連接到服務器,例如:
from stmplib import SMTP
conn = SMTP(server,port)
conn.set_debuglevel(1)
conn.starttls()
conn.login(username, password)
conn.quit()
最后我在starttls
遇到錯誤:
/python2.7/ssl.py”,行847,在do_handshake self._sslobj.do_handshake()中
問題是跟隨python嘗試與TLS v1.2建立連接,但Exchange僅支持TLS v.1.0。 我嘗試使用端口25和587。
當我嘗試通過控制台openssl
應用程序連接並登錄服務器時,使用TLS v.1.0的兩個端口都可以正常工作: openssl s_client -connect sever:587 -starttls smtp -no_tls1_2 -no_tls1_1 -crlf
服務器回答:
SSL handshake has read 1481 bytes and written 530 bytes
--- New, TLSv1/SSLv3, Cipher is RC4-MD5 Server public key is 2048 bit Secure Renegotiation IS supported Compression: NONE Expansion: NONE No ALPN negotiated SSL-Session:
Protocol : TLSv1
Cipher : RC4-MD5
Session-ID: xxxx
Session-ID-ctx:
Master-Key: xxxx
Key-Arg : None
PSK identity: None
PSK identity hint: None
SRP username: None
Start Time: 1533874470
Timeout : 300 (sec)
Verify return code: 21 (unable to verify the first certificate)
--- 250 CHUNKING ehlo 250-xxxxxxx Hello [xxxx] 250-SIZE 26214400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-AUTH GSSAPI NTLM LOGIN 250-8BITMIME 250-BINARYMIME 250 CHUNKING ^C
我嘗試繼承標准SMTP類以重載starttls
方法以使用context
選項,例如:
# show only changes to standard `starttls` method
def starttls(self, keyfile=None, certfile=None, context=None):
...
if context is None:
context = ssl._create_stdlib_context(certfile=certfile, keyfile=keyfile)
self.sock = context.wrap_socket(self.sock, server_hostname=self._hostname)
...
並在我的腳本中使用此類:
context = ssl.SSLContext(ssl.PROTOCOL_TLS)
context.options |= ssl.OP_NO_TLSv1_2 | ssl.OP_NO_TLSv1_1
conn = mySMTP(server,port)
conn.set_debuglevel(1)
conn.starttls(context = context)
conn.login(username, password)
conn.quit()
但是錯誤仍然相同。
我做錯了什么? 可能是上下文選項必須是其他選項,還是我可能想念某些東西?
在這種情況下,如何設置smtplib和starttls方法以強制僅使用TLS v.1.0?
過去使用不推薦使用的SSL版本連接到舊電子郵件源時,對我來說成功的事情是使用stunnel或類似方法設置本地隧道並通過localhost連接到本地主機,您也可以將本地sendmail實例設置為代理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.