Python:使用 pyOpenSSL.crypto 读取 pkcs12 证书 [英] Python: reading a pkcs12 certificate with pyOpenSSL.crypto

查看:28
本文介绍了Python:使用 pyOpenSSL.crypto 读取 pkcs12 证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有西班牙当局 (FNMT) 颁发的有效证书,我想使用它以了解更多信息.该文件的扩展名为 .p12

I have a valid certificate issued by the spanish authority (FNMT) and I want to play with it to learn more about it. The file has extension .p12

我想阅读其中的信息(名字和姓氏)并检查证书是否有效.可以用 pyOpenSSL 做到这一点吗?我想我必须在 OpenSSL 中使用加密模块.任何帮助或有用的链接?尝试在这里阅读:http://packages.python.org/pyOpenSSL/openssl-crypto.html 但信息不多:-(

I would like to read the information in it (first and last name) and check if the certificate is valid. Is it possible to do that with pyOpenSSL? I guess I have to use the crypto module in OpenSSL. Any help or useful link? Trying reading here: http://packages.python.org/pyOpenSSL/openssl-crypto.html but not much information :-(

推荐答案

使用起来相当简单.这没有经过测试,但应该可以工作:

It's fairly straight-forward to use. This isn't tested, but should work:

# load OpenSSL.crypto
from OpenSSL import crypto

# open it, using password. Supply/read your own from stdin.
p12 = crypto.load_pkcs12(open("/path/to/cert.p12", 'rb').read(), passwd)

# get various properties of said file.
# note these are PyOpenSSL objects, not strings although you
# can convert them to PEM-encoded strings.
p12.get_certificate()     # (signed) certificate object
p12.get_privatekey()      # private key.
p12.get_ca_certificates() # ca chain.

有关更多示例,请查看 pyopenssl 的单元测试代码.几乎所有你想使用图书馆的方式都在那里

For more examples, have a look through the unit test code of pyopenssl. Pretty much every way you might want to use the library is there

另见此处或不带广告这里.

这篇关于Python:使用 pyOpenSSL.crypto 读取 pkcs12 证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆