What would be a nice way to get SSL's Subject DN with python ?
This includes things like:
- Country name
- State name
- Locality name
- Org name
- Common name
What would be a nice way to get SSL's Subject DN with python ?
This includes things like:
Something like:
import ssl
import socket
from pprint import pprint
hostname = 'www.example.com'
context = ssl.create_default_context()
with socket.create_connection((hostname, 443)) as sock:
with context.wrap_socket(sock, server_hostname=hostname) as ssock:
pprint(ssock.getpeercert()['subject'])
Outputs:
((('countryName', 'US'),),
(('stateOrProvinceName', 'California'),),
(('localityName', 'Los Angeles'),),
(('organizationName', 'Internet Corporation for Assigned Names and Numbers'),),
(('commonName', 'www.example.org'),))
Read more about getpeercert()
here: https://docs.python.org/3/library/ssl.html#ssl.SSLSocket.getpeercert