Skip to content

Instantly share code, notes, and snippets.

@avoidik
Forked from rashley-iqt/cert_test_pyca.py
Created February 12, 2019 09:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avoidik/db5b36756a5191c5666b46526fa176d3 to your computer and use it in GitHub Desktop.
Save avoidik/db5b36756a5191c5666b46526fa176d3 to your computer and use it in GitHub Desktop.
x509Adapter example with pyca/cryptography
import requests
from cryptography.hazmat.primitives.serialization.pkcs12 import load_key_and_certificates
from cryptography.hazmat.primitives.serialization import Encoding, PrivateFormat, NoEncryption
from cryptography.hazmat.backends import default_backend
from requests_toolbelt.adapters.x509 import X509Adapter
backend = default_backend()
with open('test_cert.p12', 'rb') as pkcs12_file:
pkcs12_data = pkcs12_file.read()
pkcs12_password_bytes = "test".encode('utf8')
pycaP12 = load_key_and_certificates(pkcs12_data, pkcs12_password_bytes, backend)
cert_bytes = pycaP12[1].public_bytes(Encoding.DER)
pk_bytes = pycaP12[0].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