加载证书时,Python M2Crypto 在 ssl_ctx_load_verify_locations 中引发异常

发布于 2024-09-25 15:26:51 字数 1140 浏览 11 评论 0原文

加载 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离鸿 2024-10-02 15:26:51

刚刚解决了!

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

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

ctx.load_verify_locations(str(m.ca_file))

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:

ctx.load_verify_locations(str(m.ca_file))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文