-
-
Save avoidik/db5b36756a5191c5666b46526fa176d3 to your computer and use it in GitHub Desktop.
x509Adapter example with pyca/cryptography
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 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