Connect to IIS server using .pfx certificate - python

Hi I am new to SSL and I am trying to connect to an IIS ASP.NET web server which has issued to me a unique .pfx certificate to verify me to the server.
Using a browser where I have installed the certificate I am able to connect the site where I have to upload a file on a daily basis.
I am trying to write a python script to do the same task. I have tried to use the Python Mechanize library.
While adding certificate I converted the .pfx file to .key and .cer PEM file so that it could be attached to add_client_certificate method, but later, I found out that IIS server accepts only .pfx certificate and there is no way to attach a .pfx certificate directly to the Mechanize browser instance.
Is there a way or another library where I can do this task?

You can use requests library
import requests
requests.get("https://your_server", cert=('client01.cer', 'client01.key'))

Related

How do I update an SSL certificate in Python?

I am running python 3.9.1 I have some Django Admin Actions which create and then download some PDFs. When running this on my local machine (Windows 10) I have recently started getting the following error message:
SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1123)
Any ideas on how I can deal with this? The Django app works perfectly on the server, the problem is only on my local host.
In my case, I was interacting with IoT (Internet of Things) device APIs and had a LetsEncrypt certificate that expired. I downloaded the new LetsEncrypt cert at: https://valid-isrgrootx1.letsencrypt.org/
More explanation:
My error occurred on a Windows Python client requesting API information from an IoT web server. I determined which client certificate was expired by viewing existing certificates dates in Windows:
Open Powershell as admin, then: Get-Childitem cert:\LocalMachine\root |format-list
The expired cert was owned by LetsEncrypt. More information about the expired cert: https://letsencrypt.org/docs/dst-root-ca-x3-expiration-september-2021/.
In my case, the LetsEncrypt root certificate expired at the end of September and was replaced with the newer cert going forward. To implement the new cert, visit any webpage that uses that certificate from your failing client. In the case of LetsEncrypt, visit their cert demo page at: https://valid-isrgrootx1.letsencrypt.org/, and your certificate store will update automatically.

Python - Requests HTTP Library SSL Key

I am using requests library to complete communication with https websites. This works great, my only problem is that wireshark no longer captures plain text information in the "Decrypted SSL Data" tab as it does after following this instructional :
https://jimshaver.net/2015/02/11/decrypting-tls-browser-traffic-with-wireshark-the-easy-way/
Setup enviromental variable that allows chrome and firefox to store ssl keys in file, wireshark uses this file in real time.
Is their a way I can modify a simple https request script such as this :
import requests
resp = requests.get("https://www.google.com", allow_redirects=True)
to also store the ssl key into file as chrome and firefox do?
From what I understand about OpenSSL implementations that would do similar, you'd have to find the master secret and session key in memory - is this doable when running from cmd or practical?
This appears to be possible now with Requests.
I have set SSLKEYLOGFILE=secrets.log and then ran a request via requests.get() and secrets.log is now populated with TLS secrets. I am using requests v2.25.1 and urllib3 v1.26.3.
Apparently, it took a while for OpenSSL to provide APIs necessary to extract keying information, and then time for bindings to be created in pyOpenSSL to utilize those APIs and then for that to bubble up to urllib3.
See this issue for more details: https://github.com/psf/requests/issues/3674
openssl s_client -connect www.google.com:443 -showcerts
you will see all certs that google site uses.

Self-Signed Certificates and Urllib with Python

I have a self-signed certificate file, and I need to make requests to a REST endpoint that requires the certificate. How do I pass this information using the standard python 2.7.x libraries?
Is there a way I can check if the current user has the self-signed certificate installed in the certificate store on Windows? If so, can I grab the certificate?
How do you just use urllib/urllib2 to pass a self-signed certificate?
Thank you

m2crypto generated certificate file "This file is invalid for use as the following: security certificate" error

My python application is using M2Crypto to generate CSR in client, sends it to server and retrieves signed certificate in PEM format and saves as file.
Signed certificate file can be verified by M2Crypto. It can be read by OpenSSL successfully both in client and server.
However Windows clients report "This file is invalid for use as the following: security certificate" error when it is tried to be used. Like wise, in Ubuntu clients, "Could not display <filename>" is reported when it is tried to be viewed.
Platform Information for both client and server:
Python 2.7.x
M2Crypto 0.21.1
OpenSSL 1.0.1e
Do you have any idea, what am I missing?

How to add client certificate using python mechanize

I am a client to a secured HTTPS server who has issued to me a password protected .pfx certificate to identify me to its system every-time. Using browser where I have installed the certificate (apparently only IE6 and previous versions works) i am able to log in and upload a file which i need to do on a daily basis. Right now am trying to write a Python script to do the same task and I am not sure if it is doable this way.
I am trying to use python mechanize library to connect to the server.
Using OpenSSL I have broken down the .pfx file into .key and .cer PEM files.
And I am using them as certificate for my python script.
Here is my code snippet
br = mechanize.Browser()
br.add_client_certificate(host,"I:/Key.key","I:/certificate.crt:")
whatever I tried it throws various SSL exception .. namely
SSL_CTX_use_certificate_chain_file
SSL_CTX_use_PrivateKey_file
Can anyone please tell me what possibly I could be doing wrong or if at all this is not the right approach.
I was able to get rid of errors by ways described in this link.
It didn't solve my purpose though because the server I was trying to connect is a IIS based server and it only accepts .pfx certificates.

Categories

Resources