加载证书时,Python M2Crypto 在 ssl_ctx_load_verify_locations 中引发异常
加载 SSL CA 证书时,M2Crypto 会引发类型错误。我从 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')
M2Crypto raises a TypeError when loading SSL CA certificates. I'm getting the path of an SSL certificate from an instance of a Django model. My code worked perfectly because I was pulling the path of the certificate from a Django model
My code:
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)
Raises:
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 *'
However this code works fine
from M2Crypto import SSL
ctx = SSL.Context()
ctx.load_verify_locations('/path/to/my/certificates/certificate.cer')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
刚刚解决了!
load_verify_locations() 函数需要一个 string 对象,而不是 unicode 对象。
Django 默认使用 unicode,因此在传递给 load_verify_locations() 之前需要将证书路径转换为字符串。所以:
Just worked it out!
The load_verify_locations() function is expecting a string object, not a unicode object.
Django uses unicode by default, so the certificate path needs to be converted to a string before passed into load_verify_locations(). So: