Pythons M2Crypto在加载证书时ssl_ctx_load_verify_locations引发异常
M2Crypto在加载SSL CA证书时会引发一个类型错误。我是从一个Django模型的实例中获取SSL证书的路径。我的代码之前运行得很好,因为我正是从Django模型中提取证书的路径。
我的代码:
from M2Crypto import SSL
from django.db import models
class MyModel(models.Model):
ca_file = models.FilePathField(path='/path/to/my/certificates/')
m = MyModel(ca_file='/path/to/my/certificates/certificate.cer')
m.save()
ctx = SSL.Context()
ctx.load_verify_locations(m.ca_file)
引发的错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/django/lib/datalivelib/utils/https.py", line 51, in do_https
if ctx.load_verify_locations(ca_file) != 1:
File "/usr/lib/pymodules/python2.6/M2Crypto/SSL/Context.py", line 131, in load_verify_locations
return m2.ssl_ctx_load_verify_locations(self.ctx, cafile, capath)
TypeError: in method 'ssl_ctx_load_verify_locations', argument 2 of type 'char const *'
不过,这段代码运行得很好
from M2Crypto import SSL
ctx = SSL.Context()
ctx.load_verify_locations('/path/to/my/certificates/certificate.cer')
1 个回答
2
我刚刚搞明白了!
load_verify_locations() 这个函数需要的是一个 字符串 对象,而不是 unicode 对象。
Django 默认使用的是 unicode,所以在把证书路径传给 load_verify_locations() 之前,需要把它转换成字符串。这样做:
ctx.load_verify_locations(str(m.ca_file))