Last active
April 10, 2025 07:17
-
Star
(137)
You must be signed in to star a gist -
Fork
(24)
You must be signed in to fork a gist
Revisions
-
erikbern revised this gist
May 10, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 pfx_to_pem('foo.pem', 'bar') as cert: # requests.post(url, cert=cert, data=payload) -
erikbern revised this gist
Feb 10, 2017 . 1 changed file with 6 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,30 +5,22 @@ import ssl import tempfile @contextlib.contextmanager 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) 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 # HOW TO USE: # with get_cert() as cert: # requests.post(url, cert=cert, data=payload) -
erikbern revised this gist
Feb 9, 2017 . 1 changed file with 5 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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())) 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() -
erikbern revised this gist
May 16, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(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() -
erikbern revised this gist
May 16, 2016 . 1 changed file with 9 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. ''' -
erikbern revised this gist
May 16, 2016 . 1 changed file with 1 addition and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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: return requests.post(url, cert=cert, data=payload) -
erikbern created this gist
May 16, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)