pythonpython-requestssudszeep

Soap call using pfx or p12 authentication in Zeep or suds library Python


I want to use SOAP call service using .p12 certificate file. I tried below code. But it throws path error.

Is there any solution for this, in suds python3?

*TypeError: stat: path should be string, bytes, os.PathLike or integer, not X509*

from requests import Session
from zeep.transports import Transport
from zeep import Client
from OpenSSL import crypto

pkcs12 = crypto.load_pkcs12(open(CertPath, 'rb').read(), Password)
session = Session()
session.cert = (pkcs12.get_certificate(),pkcs12.get_privatekey())
transport = Transport(session=session)
client = Client(
    'http://my.own.sslhost.local/service?WSDL',
    transport=transport) 

Solution

  • You'll need to convert the p12 cert into the pem format before adding into session.cert, e.g. something like below:

    pkcs12 = crypto.load_pkcs12(open(CertPath, 'rb').read(), Password)
    
    cert = crypto.dump_certificate(crypto.FILETYPE_PEM, pkcs12.get_certificate())
    key = crypto.dump_privatekey(crypto.FILETYPE_PEM, pkcs12.get_privatekey())
    
    with open('cert.pem', 'wb') as f:
        f.write(cert)    
    with open('key.pem', 'wb') as f:
        f.write(key)
    
    session.cert = ('cert.pem', 'key.pem')