Skip to content

Instantly share code, notes, and snippets.

@erikbern
Last active April 10, 2025 07:17

Revisions

  1. erikbern revised this gist May 10, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion use_pfx_with_requests.py
    Original file line number Diff line number Diff line change
    @@ -22,5 +22,5 @@ def pfx_to_pem(pfx_path, pfx_password):
    yield t_pem.name

    # HOW TO USE:
    # with get_cert() as cert:
    # with pfx_to_pem('foo.pem', 'bar') as cert:
    # requests.post(url, cert=cert, data=payload)
  2. erikbern revised this gist Feb 10, 2017. 1 changed file with 6 additions and 14 deletions.
    20 changes: 6 additions & 14 deletions use_pfx_with_requests.py
    Original file line number Diff line number Diff line change
    @@ -5,30 +5,22 @@
    import ssl
    import tempfile

    print(ssl.OPENSSL_VERSION)

    @contextlib.contextmanager
    def get_cert():
    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')

    p12 = OpenSSL.crypto.load_pkcs12(open(os.environ['PFXPATH'], 'rb').read(), os.environ['PFXPASSWORD'])

    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()))
    ca = p12.get_ca_certificates()
    if ca is not None:
    for cert in ca:
    f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert))

    f_pem.close()

    yield t_pem.name


    def get_report(url, payload):
    with get_cert() as cert:
    return requests.post(url,
    cert=cert,
    data=payload)
    # HOW TO USE:
    # with get_cert() as cert:
    # requests.post(url, cert=cert, data=payload)
  3. erikbern revised this gist Feb 9, 2017. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions use_pfx_with_requests.py
    Original file line number Diff line number Diff line change
    @@ -16,9 +16,11 @@ def get_cert():
    p12 = OpenSSL.crypto.load_pkcs12(open(os.environ['PFXPATH'], 'rb').read(), os.environ['PFXPASSWORD'])

    f_pem.write(OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, p12.get_privatekey()))
    certs = (p12.get_certificate(),) + p12.get_ca_certificates()
    for cert in certs:
    f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert))
    f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, p12.get_certificate()))
    ca = p12.get_ca_certificates()
    if ca is not None:
    for cert in ca:
    f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert))

    f_pem.close()

  4. erikbern revised this gist May 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion use_pfx_with_requests.py
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ def get_cert():
    with tempfile.NamedTemporaryFile(suffix='.pem') as t_pem:
    f_pem = open(t_pem.name, 'wb')

    p12 = OpenSSL.crypto.load_pkcs12(open(config['PFXPATH'], 'rb').read(), config['PFXPASSWORD'])
    p12 = OpenSSL.crypto.load_pkcs12(open(os.environ['PFXPATH'], 'rb').read(), os.environ['PFXPASSWORD'])

    f_pem.write(OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, p12.get_privatekey()))
    certs = (p12.get_certificate(),) + p12.get_ca_certificates()
  5. erikbern revised this gist May 16, 2016. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions use_pfx_with_requests.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,12 @@
    import contextlib
    import OpenSSL.crypto
    import os
    import requests
    import ssl
    import tempfile

    print(ssl.OPENSSL_VERSION)

    @contextlib.contextmanager
    def get_cert():
    ''' Decrypts the .pfx file to be used with requests. '''
  6. erikbern revised this gist May 16, 2016. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions use_pfx_with_requests.py
    Original file line number Diff line number Diff line change
    @@ -18,8 +18,6 @@ def get_cert():

    def get_report(url, payload):
    with get_cert() as cert:
    session = requests.Session()

    return session.post(url,
    return requests.post(url,
    cert=cert,
    data=payload)
  7. erikbern created this gist May 16, 2016.
    25 changes: 25 additions & 0 deletions use_pfx_with_requests.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    @contextlib.contextmanager
    def get_cert():
    ''' Decrypts the .pfx file to be used with requests. '''
    with tempfile.NamedTemporaryFile(suffix='.pem') as t_pem:
    f_pem = open(t_pem.name, 'wb')

    p12 = OpenSSL.crypto.load_pkcs12(open(config['PFXPATH'], 'rb').read(), config['PFXPASSWORD'])

    f_pem.write(OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, p12.get_privatekey()))
    certs = (p12.get_certificate(),) + p12.get_ca_certificates()
    for cert in certs:
    f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert))

    f_pem.close()

    yield t_pem.name


    def get_report(url, payload):
    with get_cert() as cert:
    session = requests.Session()

    return session.post(url,
    cert=cert,
    data=payload)