Comment puis-je vérifier un certificat SSL en python?

J'ai besoin de vérifier qu'un certificat a été signé par mon custom CA. À l'aide d'OpenSSL utilitaires de ligne de commande c'est facile à faire:

# Custom CA file: ca-cert.pem
# Cert signed by above CA: bob.cert
$ openssl verify -CAfile test-ca-cert.pem bob.cert
bob.cert: OK

Mais j'ai besoin de faire la même chose en Python, et je ne veux vraiment pas à faire appel à des utilitaires de ligne de commande. Pour autant que je suis au courant, M2Crypto "la plus complète" wrapper python pour OpenSSL, mais je ne peux pas comprendre comment accomplir ce que l'utilitaire de ligne de commande ne!

Référencement cette question pour savoir comment accomplir la même tâche dans le code C, j'ai été en mesure d'obtenir environ à mi-chemin. Les noms de variable, j'ai choisi sont les mêmes que ceux utilisés dans le code source d'openssl vérifier l'utilitaire de ligne de commande, voir openssl-xxx/apps/verify.c.

import M2Crypto as m2
# Load the certificates
cacert = m2.X509.load_cert('test-ca-cert.pem')   # Create cert object from CA cert file
bobcert = m2.X509.load_cert('bob.cert')     # Create cert object from Bob's cert file
cert_ctx = m2.X509.X509_Store()             # Step 1 from referenced C code steps
csc = m2.X509.X509_Store_Context(cert_ctx)  # Step 2 & 5
cert_ctx.add_cert(cacert)                   # Step 3
cert_ctx.add_cert(bobcert)                  # ditto
# Skip step 4 (no CRLs to add)
# Step 5 is combined with step 2...I think. (X509_STORE_CTX_init: Python creates and 
#   initialises an object in the same step)
# Skip step 6? (can't find anything corresponding to 
#   X509_STORE_CTX_set_purpose, not sure if we need to anyway???)
# 
# It all falls apart at this point, as steps 7 and 8 don't have any corresponding
# functions in M2Crypto -- I even grepped the entire source code of M2Crypto, and
# neither of the following functions are present in it:
# Step 7: X509_STORE_CTX_set_cert - Tell the context which certificate to validate.
# Step 8: X509_verify_cert - Finally, validate it

Donc je suis à mi-chemin, mais je n'arrive pas à réellement obtenir la validation! Ai-je raté quelque chose? Est-il une autre fonction que je devrais être à l'aide de M2Crypto? Dois-je être à la recherche pour un de complètement différent wrapper python de OpenSSL? Comment puis-je accomplir cette tâche en python!?!?

Remarque que je suis à l'aide de certificats pour crypter/décrypter des FICHIERS, donc je ne suis pas intéressé à l'aide du protocole SSL pour les pairs de la vérification du certificat (qui a déjà répondu), parce que je n'ai pas de connexions SSL va.

source d'informationauteur Nathan