import ssl, socket
hostname = ' '
ctx = ssl.create_default_context()
with ctx.wrap_socket(socket.socket(), server_hostname=hostname) as s:
s.connect((hostname, 443))
cert = s.getpeercert()
subject = dict(x[0] for x in cert['subject'])
issued_to = subject['commonName']
issuer = dict(x[0] for x in cert['issuer'])
issued_by = issuer['commonName']
serialNumber = dict(x[0] for x in cert['serialNumber'])
print(cert)
print(issued_to)
print(issued_by)
s153152@MB20OXDHQ60774 Documents % python3 test3.py {'subject': ((('countryName', 'US'),), (('stateOrProvinceName', 'California'),), (('localityName', 'Eureka'),), (('organizationName', 'departmentName'),), (('organizationalUnitName', 'hostname1'),), (('commonName', 'hostname.ca.gov'),)), 'issuer': ((('domainComponent', 'gov'),), (('domainComponent', 'ca'),), (('domainComponent', 'dot'),), (('domainComponent', 'ct'),), (('commonName', '-CA'),)), 'version': 3, 'serialNumber': '646ss8AA2A000100000F0C', 'notBefore': 'Apr 9 21:26:20 2020 GMT', 'notAfter': 'Dec 28 19:45:58 2021 GMT', 'subjectAltName': (('DNS', 'hostname.ca.gov'), ('DNS', ''), ('DNS', 'hostname'), ('DNS', 'hostname.ca.gov'), ('DNS', 'hostname.ca.gov')), 'caIssuers': ('ldap:///CN=-CA,CN=AIA,CN=Public Key Services,CN=Services,CN=Configuration,DC=ca,DC=gov?cACertificate?base?objectClass=certificationAuthority',), 'crlDistributionPoints': ('ldap:///CN=-CA(1),CN=,CN=CDP,CN=Public Key Services,CN=Services,CN=Configuration,DC=ca,DC=gov?certificateRevocationList?base?objectClass=cRLDistributionPoint',)}.ca.gov output1 s153152@MB20OXDHQ60774 Documents %
want this output 'serialNumber': '6468AA2A000100000F0C'
CodePudding user response:
serialNumber = dict(x[0] for x in cert['serialNumber'])
The serial number is a single value, not a dict. Therefore
serialNumber = cert['serialNumber']