2

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
Martin Gergov
  • 1,556
  • 4
  • 20
  • 29

1 Answers1

0

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

Martin Gergov
  • 1,556
  • 4
  • 20
  • 29