import ssl
ssl.get_server_certificate(("www.sefaz.ce.gov.br",443))
cert = ssl.get_server_certificate(("www.google.com", 443)) #Retrieve SSL server certificate
cert = ssl.PEM_cert_to_DER_cert(cert) #Convert certificate to DER format
begin = cert.rfind('\x06\x03\x55\x04\x03') + 7 #Find the last occurence of this byte string indicating the CN, add 7 bytes to startpoint to account for length of byte string and padding
end = begin + ord(cert[begin - 1]) #Set endpoint to startpoint + the length of the CN
print (cert[begin:end]) #Retrieve the CN from the DER encoded certificate
"""Retrieve the certificate from the server at the specified address,
and return it as a PEM-encoded string.
If 'ca_certs' is specified, validate the server cert against it.
If 'ssl_version' is specified, use it in the connection attempt."""
def get_commonname(host,port=443):
oid='\x06\x03U\x04\x03' # Object Identifier 2.5.4.3 (COMMON NAME)
pem=ssl.get_server_certificate((host,port))
der=ssl.PEM_cert_to_DER_cert(pem)
i=der.find(oid) # find first common name (certificate authority)
if i!=-1:
i=der.find(oid,i+1) # skip and find second common name
if i!=-1:
begin=i+len(oid)+2
end=begin+ord(der[begin-1])
return der[begin:end]
return None
Hi there,
did python change its convention from print to print()
Traceback (most recent call last):
File "httplib.py", line 6, in <module>
begin = cert.rfind('\x06\x03\x55\x04\x03') + 7 #Find the last occurence of t
his byte string indicating the CN, add 7 bytes to startpoint to account for leng
th of byte string and padding
TypeError: Type str doesn't support the buffer API
and how come type str doesn't support it?