Last active
January 20, 2021 19:14
-
-
Save rashley-iqt/35368709f21fec64b33b787ebbc609e2 to your computer and use it in GitHub Desktop.
x509Adapter example using OpenSSL
This file contains 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 characters
import requests | |
from OpenSSL.crypto import load_pkcs12 | |
from cryptography.hazmat.primitives.serialization import load_pem_private_key, load_der_private_key | |
from cryptography.hazmat.primitives.serialization import Encoding, PrivateFormat, NoEncryption | |
from requests_toolbelt.adapters.x509 import X509Adapter | |
with open('test_cert.p12', 'rb') as pkcs12_file: | |
pkcs12_data = pkcs12_file.read() | |
pkcs12_password_bytes = "test".encode('utf8') | |
PyoP12 = load_pkcs12(pkcs12_data, pkcs12_password_bytes) | |
cert_bytes = PyoP12.get_certificate().to_cryptography().public_bytes(Encoding.DER) | |
pk_bytes = PyoP12.get_privatekey().to_cryptography_key().private_bytes(Encoding.DER, PrivateFormat.PKCS8, NoEncryption()) | |
adapter = X509Adapter(max_retries=3, cert_bytes=cert_bytes, pk_bytes=pk_bytes, encoding=Encoding.DER) | |
session = requests.Session() | |
session.mount('https://', adapter) | |
r = session.get('https://pkiprojecttest01.dev.labs.internal/', verify=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment