Skip to content

Instantly share code, notes, and snippets.

@pior
Last active February 11, 2018 18:59
Show Gist options
  • Save pior/7bce30cd4083e0d824b205a0fe2df64f to your computer and use it in GitHub Desktop.
Save pior/7bce30cd4083e0d824b205a0fe2df64f to your computer and use it in GitHub Desktop.
Fetch a remote SSL certificate without hostname check
import socket
import ssl
def fetch_ssl_certificate(hostname, port=443):
ctx = ssl.create_default_context()
ctx.check_hostname = False
sock = ctx.wrap_socket(socket.socket())
sock.connect((hostname, port))
return sock.getpeercert()
fetch_ssl_certificate('pbastida.net')
@pior
Copy link
Author

pior commented Feb 11, 2018

In [1]: import socket
   ...: import ssl
   ...:
   ...: def fetch_ssl_certificate(hostname, port=443):
   ...:     ctx = ssl.create_default_context()
   ...:     ctx.check_hostname = False
   ...:     sock = ctx.wrap_socket(socket.socket())
   ...:     sock.connect((hostname, port))
   ...:     return sock.getpeercert()
   ...:
   ...: fetch_ssl_certificate('pbastida.net')
   ...:
Out[1]:
{'OCSP': ('http://ocsp.digicert.com',),
 'caIssuers': ('http://cacerts.digicert.com/DigiCertSHA2HighAssuranceServerCA.crt',),
 'crlDistributionPoints': ('http://crl3.digicert.com/sha2-ha-server-g5.crl',
  'http://crl4.digicert.com/sha2-ha-server-g5.crl'),
 'issuer': ((('countryName', 'US'),),
  (('organizationName', 'DigiCert Inc'),),
  (('organizationalUnitName', 'www.digicert.com'),),
  (('commonName', 'DigiCert SHA2 High Assurance Server CA'),)),
 'notAfter': 'Apr  7 12:00:00 2020 GMT',
 'notBefore': 'Mar 20 00:00:00 2017 GMT',
 'serialNumber': '09975234E83242499BF5D10B72CA951E',
 'subject': ((('countryName', 'US'),),
  (('stateOrProvinceName', 'California'),),
  (('localityName', 'San Francisco'),),
  (('organizationName', 'GitHub, Inc.'),),
  (('commonName', '*.github.com'),)),
 'subjectAltName': (('DNS', '*.github.com'),
  ('DNS', 'github.com'),
  ('DNS', '*.github.io'),
  ('DNS', 'github.io')),
 'version': 3}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment