2016-08-29 2 views
1

Est-il possible d'extraire le subject key identificator d'un certificat existant avec python?Obtenir l'identificateur de clé de sujet du certificat

J'ai essayé someting comme:

from OpenSSL.crypto import load_certificate, FILETYPE_PEM 

cert_string='-----BEGIN CERTIFICATE--...' 
certificate=load_certificate(FILETYPE_PEM, plain_cert) 
subject=certificate.get_subject() 

Mais il redonne l'objet du certificat. Il semble que l'objet certificat n'offre pas de fonctions pour l'identificateur de clé sujet. Y a-t-il d'autres options?

Répondre

1
subject=certificate.get_extension(0) 

fait le travail. Avec

certificate.get_extension_count() 

vous pouvez vérifier combien les extensions du certificat a.

2

Le code qui extraira sujet principal identificateur:

from cryptography import x509 
from cryptography.hazmat.backends import default_backend 

cert = x509.load_pem_x509_certificate(pem_data, default_backend()) 
ski = cert.extensions.get_extension_for_oid(x509.oid.ExtensionOID.SUBJECT_KEY_IDENTIFIER) 
print(ski.value.digest)