如何在python中验证SSL证书?

问题描述:

我需要验证证书是否由我的自定义CA签署。使用OpenSSL命令行工具,这是很容易做到:如何在python中验证SSL证书?

# 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包装,但我无法弄清楚如何完成命令行工具的功能!

参考this question对于如何在C代码中完成相同的任务,我已经能够获得大约一半的时间。 我选择的变量名称与openssl verify命令行实用程序的源代码中使用的名称相同,请参阅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连接的对等证书验证(其中有already been answered)没有兴趣,因为我没有任何SSL连接去。

+1

是否有任何理由你不想调用命令行工具?似乎你可以为自己节省一大笔头痛...... – katrielalex 2010-12-09 21:08:12

+2

是的,这是针对将被部署在许多不同的操作系统上的软件,无论新老,大小,都包括潜在的嵌入式系统(足够大以包含python) )。我想要最大的便携性和最高性能。如果我可以避免的话,我不想让潜在的成千上万个呼叫进入命令行。涉及大量证书。 – Nathan 2010-12-09 22:20:35

+0

相关http://*.com/q/2626792/4279 – jfs 2010-12-16 05:34:23

就像你说的, OpenSSL的需要连接

M2Crypto不具有良好的验证

这样如何巧妙的构思:

import os 
os.system('openssl verify -CAfile ../ca-cert.pem bob.cert') 

它丑陋,但它的工程!

您可以使用遗憾的未公开的X509.verify方法来检查证书是否使用CA的私钥签名。作为这一调用OpenSSL的x509_verify的背景下,我敢肯定,这也是正确的检查所有参数(如过期):

from M2Crypto X509 

cert = X509.load_cert("certificate-filename") 

caCertificate = X509.load_cert("trusted-ca-filename") 
caPublic = caCertificate.get_pubkey() 

if cert.verify(caPublic) == 1: 
    # Certificate is okay! 
else: 
    # not okay 

你不能用普通M2Crypto做到这一点,因为它不换一些必要的功能。好消息是,如果你安装了SWIG,你可以自己打包并使用M2Crypto代码。我前段时间为自己制作了一些额外功能的模块,并决定现在发布它,因为它进行了这种验证。你可以在这里查看:https://github.com/abbot/m2ext。这是一个例子,如何使用此模块验证证书:

import sys 
from m2ext import SSL 
from M2Crypto import X509 

print "Validating certificate %s using CApath %s" % (sys.argv[1], sys.argv[2]) 
cert = X509.load_cert(sys.argv[1]) 
ctx = SSL.Context() 
ctx.load_verify_locations(capath=sys.argv[2]) 
if ctx.validate_certificate(cert): 
    print "valid" 
else: 
    print "invalid" 

不幸的是M2Crypto发展似乎是停滞(在bug跟踪系统没有闭合问题在过去的两年中)和维护者无视我的错误和电子邮件与这些和其他一些补丁...