|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)|
|ca = p12.get_ca_certificates()|
|if ca is not None:|
|for cert in ca:|
|# HOW TO USE:|
|# with pfx_to_pem('foo.pem', 'bar') as cert:|
|# requests.post(url, cert=cert, data=payload)|
@talespadua lol I actually googled "python requests p12" right now, found my own gist (which I had forgotten about), and saw your comment. pretty weird coincidence given that you commented on it 40 minutes ago. i'll let you know if i figure out how to make it work
@erikbern Yeah, I just saw you commenting in a issue on similar subject. The code now appears to work, although I am having bad handshake for some reason. I can acess the website with selenium + firefox, but having a hard time with requests or selenium + phantomjs. Thanks you for the answer!
@erikbern I am new to python and am trying to send a request using the client cert. You code helps me understand how to attach the pfx. One thing I am confused is what get_cert() is used for? and how do I call the method pfx_to_pem.
Your quick response will help me.
Nice! Exactly what I'm looking for, thanks a lot man!
If anyone's getting Permission denied or file not found error, and you're on Windows, you need to
So I got through the [Errno 13] issue (thanks @d1t69 for the heads up about delete=False), but it seems my credentials are being rejected. They work fine in Postman (Make the request, it asks me to pick my cert, then returns the info I need), so I assume something's going wrong with how it's translating the cert. I checked the contents of the generated .pem file, and the Certificate matches the .pem I used to make the .pfx file but the Key doesn't match the .key file (and trying to substitute that out for the original key gives an SSL Error about asn1 encoding issues).
I also tried this method to no avail. Any insight would be greatly appreciated!
@daultonoryan On Windows, all I had to do was change Line 11 to
The file remains in the folder after the script runs if you leave it like this, so I guess either manually delete it or add a line to delete it after the loop wraps up to clean it out.
I combined this with a hider based on: (https://benkurtovic.com/2014/06/01/obfuscating-hello-world.html)
that way at least the password is not there when you read the code
With this technique, for me anyway, I'm able to decrypt a .pfx file without error, but then it fails authentication when submitting to the server per the example, even though the same .pfx file with same password submits fine via Postman. Don't know why it won't work, but I used these instructions to "decompose" the .pfx into a pair of .pem files, and those can be submitted directly from python requests. And with that technique, you only have to do it once, not on every request.