m-click / requests_pkcs12

Add PKCS#12 support to the Python requests library in a clean way, without monkey patching or temporary files
ISC License
121 stars 33 forks source link

SSLV3_ALERT_HANDSHAKE_FAILURE #57

Open morindo opened 1 month ago

morindo commented 1 month ago

Hi, I'm getting a SSLV3_ALERT_HANDSHAKE_FAILURE when trying to use my pfx certificate and make a request to a site. I tried a couple of ssl_protocol without success.

This is my code:

def download_a_file(date: pendulum.DateTime):
    # URL to download the PDF file.
    url = f"https://fakesite.com/portalDownload/invoices/file?date={date.format("YYYYMMDD")}&format=pdf"

    pfx_path = "C:/mycertificate.pfx"
    pfx_password = "FakePass123"

    with Session() as s:
        s.mount(
            "https://fakesite.com",
            Pkcs12Adapter(pkcs12_filename=pfx_path, pkcs12_password=pfx_password),
        )
        response = s.get(url)

    if response.status_code == 200:
        context.log.info("Successfully downloaded the PDF file.")
        return response.content
    else:
        context.log.error("Failed to download the PDF file.")
requests.exceptions.SSLError: HTTPSConnectionPool(host='fakesite.com', port=443): Max retries exceeded with url: /marketportal/ (Caused by SSLError(SSLError(1, '[SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:1000)')))

I'm on Windows 11, python 3.12 using requests_pkcs12 version 1.25.

Using my certificate in Chrome, Edge or Firefox is working when login in the site.

This is what I'm getting from Chrome Developer Tools->Security tab.

image

Am I doing something wrong?

Thank you,

morindo commented 1 month ago

Still searching for a fix. I think it's probably related to this requests issue https://github.com/psf/requests/issues/6715#issuecomment-2137782.

xli1205 commented 1 month ago

If you use Python 3.9 or below, this issue will not occur.

vog commented 3 weeks ago

Up to Python 3.11 I can't see any issues, either. I'll test with Python 3.12 later.