【问题标题】:Pythons M2Crypto raises exception in ssl_ctx_load_verify_locations when loading certificates加载证书时,Pythons M2Crypto 在 ssl_ctx_load_verify_locations 中引发异常
【发布时间】:2010-09-29 23:44:42
【问题描述】:

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')

【问题讨论】:

    标签: python django ssl-certificate m2crypto


    【解决方案1】:

    刚刚解决了!

    load_verify_locations() 函数需要 string 对象,而不是 unicode 对象。

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

    ctx.load_verify_locations(str(m.ca_file))
    

    【讨论】:

    • 非常感谢您抽出宝贵时间回答您自己的问题。我在使用 M2Crypto 时遇到了另一个问题,但解决方案完全相同。你刚刚拯救了我的一天! +1。
    • @ereOn 我们需要在load_verify_locations 中提供服务器端证书(ssl_ca)还是客户端证书?
    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 2012-01-09
    • 2010-12-07
    • 2015-09-26
    • 1970-01-01
    相关资源
    最近更新 更多