Im trying to authenticate with a p12 certicate given by a provider and want to use it to get the html body of the site.
i currently have the following code:
import contextlib
import OpenSSL.crypto
import os
import requests
import ssl
import tempfile
import http.client
import shutil
from OpenSSL import crypto
url = "https://website.com/"
p12_cert = "cert.p12"
password = "password"
@contextlib.contextmanager
def pfx_to_pem(pfx_path, pfx_password):
''' Decrypts the .pfx file to be used with requests. '''
with tempfile.NamedTemporaryFile(suffix='.pem') as t_pem:
f_pem = open(t_pem.name, 'wb')
pfx = open(pfx_path, 'rb').read()
p12 = OpenSSL.crypto.load_pkcs12(pfx, pfx_password)
f_pem.write(OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, p12.get_privatekey()))
f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, p12.get_certificate()))
f_pem.close()
yield t_pem.name
with pfx_to_pem(p12_cert, password) as cert:
requests.get(url, verify=cert)
ANy guidance is much apprecaited.
Thanks
verify=
is for CA certs used to verify the server cert. Usecert=
for client cert+key. docs.python-requests.org/en/master/user/advanced/…>>> with pfx_to_pem('C:\\Users\\ukhare\\Desktop\\gos\\trial_tata.p12', 'trialtata') as cert: ... requests.get('https://IP:8080/siteapi/availabletests', verify=cert) ... Traceback (most recent call last): File "<stdin>", line 1, in <module> File "D:\m\Python34\lib\contextlib.py", line 59, in __enter__ return next(self.gen) File "<stdin>", line 6, in pfx_to_pem PermissionError: [Errno 13] Permission denied: 'C:\\Users\\ukhare\\AppData\\Local\\Temp\\tmpbx5ru9gv.pem'