python - 使用Python检测SSL哈希算法

标签 python ssl sha

既然 SHA-1 将于明年被主要浏览器禁止,我想检测哪些网站仍在使用它。有没有办法使用Python获取这些信息(例如 Python's ssl library )?我可以使用 openssl s_client ,但我更喜欢 Pythonic 解决方案(与我的异步框架兼容)。

s_client 示例:

$ openssl s_client -connect winkel.vpro.nl:443 < /dev/null 2>/dev/null | openssl x509 -text -in /dev/stdin | grep -i sha
Signature Algorithm: sha1WithRSAEncryption

我翻阅了 ssl 文档,但找不到哈希算法引用。我不知道如何从 ssl 上下文中获取此信息。 TIA!

最佳答案

签名哈希算法不是 SSL 连接的属性,而是证书的属性。获得证书后,您可以使用 get_signature_algorithm 获取算法来自 OpenSSL.crypto:

import ssl, socket, OpenSSL

# connect to server and get certificate as binary (DER)
sock = socket.socket()
sock.connect(('www.google.com',443))
sslsock = ssl.wrap_socket(sock)
cert_der = sslsock.getpeercert(True)

# load binary certificate and get signature hash algorithm
cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_ASN1, cert_der)
print cert.get_signature_algorithm()
# -> 'sha256WithRSAEncryption'

使用Python3还可以使用signature_hash_algorithm来自加密包:

from cryptography import x509
from cryptography.hazmat.backends import default_backend

... get cert_der the same way as before ...
cert = x509.load_der_x509_certificate(cert_der, default_backend())
print(cert.signature_hash_algorithm.name)
#  -> 'sha256'

关于python - 使用Python检测SSL哈希算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40704540/

相关文章:

ruby - 将 mechanize(甚至 Net::HTTP)与 ruby​​ 1.9 一起使用会返回 'OpenSSL::SSL::SSLError' - 如何强制使用 SSLv3?

security - 更改圆 table 中的哈希函数值

firefox - 抑制 Firefox/Firebug SHA-1 警告

python - 如何使用 alembic --autogenerate 忽略某些模式

wordpress - 为什么 Chrome 将 https 添加到我的网站 Assets 、safari 和 firefox 工作正常

python - 如何在Python中使用C API获取对象ID?

java - 通过自己的 Java 客户端通过 HTTPS/SSL 连接的问题

java - SHA-256 自定义长度摘要

python - 文件上传 - 错误请求 (400)

Python 二进制 EOF