1

I have code to walk through my wincertstore and find a certificate by name and/or thumbprint.

if os.name == 'nt':
    for storename in ["MY"]:  # "ROOT", "CA",
        with wincertstore.CertSystemStore(storename) as store:
            for cert in store.itercerts(usage=wincertstore.CLIENT_AUTH):
                print(cert.get_name())
                print(cert.cert_type)
                print(cert.enhanced_keyusage_names())
                # pem = cert.get_pem()
                # encodedDer = ''.join(pem.split("\n")[1:-2])
                # cert_bytes = base64.b64decode(encodedDer)
                cert_pem = ssl.DER_cert_to_PEM_cert(cert.get_encoded())
                cert_details = x509.load_pem_x509_certificate(
                    cert_pem.encode('utf-8'), default_backend()
                )
                serial_number = hex(cert_details.serial_number).replace("0x", "")
                cert_details.fingerprint
                if cert.get_name().lower() == find_name.lower():
                    pem_data = cert.get_pem()
                    break
if pem_data:
   f = open('./mycert.pem', 'w')
   f.write(pem_data)
   f.close()
   del f

import requests 
resp = requests.get(<some url>, cert='./mycert.pem')

This gives an SSL Error:

urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='*****.e***.com', port=443): Max retries exceeded with url: /gis/sharing/rest/portals/self/servers?f=json (Caused by SSLError(SSLError(9, '[SSL] PEM lib (_ssl.c:3932)')))

So what else do I need to pull from the window's certificate store to pass the client certificate?

JabberJabber
  • 341
  • 2
  • 17

1 Answers1

0

I think you might be running into a bug on urllib3 Try updating it to the latest version:

https://pypi.org/project/urllib3/#changes

Technoob1984
  • 172
  • 9
  • Can you explain what you are pointing out a bit more? – JabberJabber Jul 08 '21 at 10:32
  • 1
    Hey JabberJabber, Sorry been AFK for a bit. When I researched the error I found that link I shared. It talks about urllib3 is being deprecated. I just took a fresh look and I actually think you are feeding the wrong information from your pem_data variable. Here is another overflow that talks about it: https://stackoverflow.com/questions/24160244/why-httpsconnectionpool-doesnt-work-when-poolmanager-does Can you confirm pem_data is a hostname or IP and not a URL of some sort? Thanks – Technoob1984 Aug 12 '21 at 14:42