証明書がカスタムCAによって署名されていることを確認する必要があります。OpenSSLコマンドラインユーティリティを使用すると、これを行うのは簡単です。
# 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
しかし、Pythonでも同じことをする必要があり、コマンドラインユーティリティを呼び出したくありません。私の知る限り、M2CryptoはOpenSSLの「最も完全な」Pythonラッパーですが、コマンドラインユーティリティの機能を実現する方法がわかりません。
Cコードでこれと同じタスクを実行する方法についてこの質問を参照すると、私は約半分を得ることができました。 私が選択した変数名は、opensslverifyコマンドラインユーティリティのソースコードで使用されているものと同じです。を参照してください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
ですから、途中ですが、実際に検証を行うことができないようです。私は何かが足りないのですか?M2Cryptoから使用する必要のある他の関数はありますか?OpenSSLの完全に異なるPythonラッパーを探す必要がありますか?Pythonでこのタスクを実行するにはどうすればよいですか!?!?
証明書を使用してファイルを暗号化/復号化していることに注意してください。SSL接続が確立されていないため、SSL接続ベースのピア証明書検証(すでに回答済み)の使用には関心がありません。