Last active
May 22, 2023 13:05
-
-
Save joostd/9f9dea983ed6a7b926c5ad09f51cb807 to your computer and use it in GitHub Desktop.
Check CSR with attestation to be generated on a YubiKey
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 characters
import sys | |
from cryptography import x509 | |
from cryptography.hazmat.backends import default_backend | |
from cryptography.hazmat.primitives.asymmetric import padding | |
def verify_signature(parent, child): | |
parent.public_key().verify( | |
child.signature, | |
child.tbs_certificate_bytes, | |
padding.PKCS1v15(), | |
child.signature_hash_algorithm | |
) | |
csr_file = input('Certificate Signing Request Path [csr.pem]: ') | |
if csr_file == '': | |
csr_file = 'csr.pem' | |
attestation_file = input('PIV Attestation Certificate Path [attestation.pem]: ') | |
if attestation_file == '': | |
attestation_file = 'attestation.pem' | |
intermediate_file = input('PIV Intermediate Attestation Certificate Path [intermediate.pem]: ') | |
if intermediate_file == '': | |
intermediate_file = 'intermediate.pem' | |
ca_file = input('PIV Root Attestation Certificate Path [piv-attestation-ca.pem]: ') | |
if ca_file == '': | |
ca_file = 'piv-attestation-ca.pem' | |
try: | |
with open(csr_file, 'rb') as f: | |
csr = x509.load_pem_x509_csr(f.read(), default_backend()) | |
with open(attestation_file, 'rb') as f: | |
attestation_cert = x509.load_pem_x509_certificate(f.read(), default_backend()) | |
with open(intermediate_file, 'rb') as f: | |
intermediate_cert = x509.load_pem_x509_certificate(f.read(), default_backend()) | |
with open(ca_file, 'rb') as f: | |
ca_cert = x509.load_pem_x509_certificate(f.read(), default_backend()) | |
verify_signature(ca_cert, intermediate_cert) | |
verify_signature(intermediate_cert, attestation_cert) | |
if csr.public_key().public_numbers() == attestation_cert.public_key().public_numbers(): | |
print("✅ Public keys match") | |
else: | |
sys.exit("❌ CSR public key does not match attestation public key") | |
print('✅ Signature validation succeeded.') | |
except FileNotFoundError: | |
print('❌ One or more of the supplied files was not found.') | |
except: | |
print('❌ CA path validation failed.') |
On systems where openssl is installed, better use:
openssl req -in csr.pem -noout -pubkey -out pub.pem
openssl x509 -in attestation.pem -noout -pubkey | diff - pub.pem
openssl verify -CAfile piv-attestation-ca.pem -untrusted intermediate.pem attestation.pem
as this will do an actual CA path validation (i.e. check constraints instead of only checking signatures).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Generate keys, csr, attestation crt, retrieve intermediate cert
To verify, retrieve root cert and validate the CSR and attestation: