1

M2Crypto 在加载 SSL CA 证书时引发 TypeError。我从 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')
4

1 回答 1

2

刚刚解决了!

load_verify_locations ()函数需要一个字符串对象,而不是unicode对象。

Django 默认使用 unicode,因此证书路径需要转换为字符串才能传递给load_verify_locations()。所以:

ctx.load_verify_locations(str(m.ca_file))
于 2010-09-29T23:49:55.167 回答