我最近在使用pyOpenSSL,但是我遇到了一些使用SNI为同一IP地址提供多个证书的urls。下面是我的代码:
from OpenSSL import SSL
from socket import socket
from sys import argv, stdout
import re
from urlparse import urlparse
def callback(conn, cert, errno, depth, result):
if depth == 0 and (errno == 9 or errno == 10):
return False # or raise Exception("Certificate not yet valid or expired")
return True
def main():
if len(argv) < 2:
print 'Usage: %s <hostname>' % (argv[0],)
return 1
o = urlparse(argv[1])
host_name = o.netloc
context = SSL.Context(SSL.TLSv1_METHOD) # Use TLS Method
context.set_options(SSL.OP_NO_SSLv2) # Don't accept SSLv2
context.set_verify(SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
callback)
# context.load_verify_locations(ca_file, ca_path)
sock = socket()
ssl_sock = SSL.Connection(context, sock)
ssl_sock.connect((host_name, 443))
ssl_sock.do_handshake()
cert = ssl_sock.get_peer_certificate()
common_name = cert.get_subject().commonName.decode()
print "Common Name: ", common_name
print "Cert number: ", cert.get_serial_number()
regex = common_name.replace('.', r'\.').replace('*',r'.*') + '$'
if re.match(regex, host_name):
print "matches"
else:
print "invalid"
if __name__ == "__main__":
main()例如,假设我有以下url:
https://example.com当我得到以下输出时:
python sni.py https://example.com/
Common Name: *.example.com
Cert number: 63694395280496902491340707875731768741
invalid哪个证书与https://another.example.com的证书相同
python sni.py https://another.example.com/
Common Name: *.example.com
Cert number: 63694395280496902491340707875731768741
matches但是,假设https://another.example.com的证书已过期,连接无论如何都会被接受,因为它使用的是有效的*.example.com证书。但是,我希望能够使用https://another.example.com/,如果它无效,则直接拒绝连接。我如何才能做到这一点?
发布于 2016-03-06 04:43:29
您需要使用set_tlsext_host_name。来自the documentation
Connection.set_tlsext_host_name(name)
Specify the byte string to send as the server name in the client hello message.
New in version 0.13.除此之外,您的主机名验证是错误的,因为它只与CN而不是主题替代名称进行比较。此外,它允许在任何地方使用通配符,这违反了通配符应该只允许在最左边的标签中使用的规则:*.example.com很好,而www.*.com甚至*.*.*是不允许的,但您的代码可以接受。
https://stackoverflow.com/questions/35819525
复制相似问题