如何连接Python IMAP4峎SSL和自签名服务器SSL证书?

2024-04-25 15:22:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python3和imaplib连接到Exchange服务器。Exchange服务器使用为主机名“”创建的自签名证书我的.server.fqdn'. 最近在我的工作站(Ubuntu14.04)上升级了操作系统之后,我的python脚本不再工作了。在

我使用的基本代码片段如下:

#!/usr/bin/python3

import imaplib
import socket
import ssl
import getpass

passwd = getpass.getpass()
mail =  imaplib.IMAP4_SSL('my.server.fqdn', 1143)  
mail.login('mike', passwd)
mail.list()
mail.select("INBOX")

在升级之前,代码运行良好。现在,该代码产生以下错误:

^{pr2}$

我不清楚python是否在自签名证书、SSL版本上有问题,或者python3有什么新东西不能用我的旧代码工作。在

我确实有来自Exchange服务器的PEM文件,它允许我使用curl连接到服务器。身份验证失败,但是在SSL握手期间,curl所做的检查似乎工作正常。我确实需要提供curl选项和指向PEM文件的路径,才能正常工作。如果这正是我需要用python来做的事情,那么我该如何向python提供相同的信息呢?在

curl会话在这里:

$ curl -vvv --sslv3 https://my.server.fqdn:1143 --cacert ~/my_server_fqdn.pem 
    * Rebuilt URL to: https://my.server.fqdn:1143/
    * Hostname was NOT found in DNS cache
    *   Trying 10.10.10.10...
    * Connected to my.server.fqdn (10.10.10.10) port 1143 (#0)
    * successfully set certificate verify locations:
    *   CAfile: /home/username/my_server_fqdn.pem
      CApath: /etc/ssl/certs
    * SSLv3, TLS handshake, Client hello (1):
    * SSLv3, TLS handshake, Server hello (2):
    * SSLv3, TLS handshake, CERT (11):
    * SSLv3, TLS handshake, Server key exchange (12):
    * SSLv3, TLS handshake, Server finished (14):
    * SSLv3, TLS handshake, Client key exchange (16):
    * SSLv3, TLS change cipher, Client hello (1):
    * SSLv3, TLS handshake, Finished (20):
    * SSLv3, TLS change cipher, Client hello (1):
    * SSLv3, TLS handshake, Finished (20):
    * SSL connection using ECDHE-RSA-AES256-SHA
    * Server certificate:
    *        subject: O=fqdn; O=SERVER; OU=M; CN=my.server.fqdn
    *        start date: 2013-07-29 20:59:04 GMT
    *        expire date: 2023-07-27 20:59:04 GMT
    *        common name: my.server.fqdn (matched)
    *        issuer: O=fqdn; O=SERVER; OU=M; CN=my.server.fqdn
    *        SSL certificate verify ok.
    > GET / HTTP/1.1
    > User-Agent: curl/7.35.0
    > Host: my.server.fqdn:1143
    > Accept: */*
    > 
    * OK [CAPABILITY IMAP4REV1 AUTH=LOGIN MOVE] IMAP4rev1 server ready
    GET BAD command authentication required
    User-Agent: BAD command authentication required
    Host: BAD command authentication required
    Accept: BAD command authentication required
    * SSLv3, TLS alert, Client hello (1):
    * Connection #0 to host my.server.fqdn left intact

Tags: import服务器clientsslhelloservermytls
1条回答
网友
1楼 · 发布于 2024-04-25 15:22:37

显然,这个问题是由于ubuntu12.04和14.04上openssl包中的一个或多个versions of TLS protocol being disabled造成的。最后我在python3中使用了ssl上下文,并强制使用sslversion3进行连接。现在看来这是可行的。真的希望这能帮助任何人解决同样的问题。在

#!/usr/bin/python3

import imaplib
import socket
import ssl
import getpass

ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv3)  
passwd = getpass.getpass()
mail =  imaplib.IMAP4_SSL('my.server.fqdn', 1143, ssl_context=ctx)
mail.login('mike', passwd)
mail.select('INBOX', 1)
typ, uids = mail.uid("SEARCH", None, "ALL")

uids = uids[0].split()
for u in uids:
   print ("found uid " , u)

mail.close()
mail.logout()
del mail

相关问题 更多 >