Cyr*_* N. 3 python ssl certificate
我知道可以使用 OpenSSL 或 M2Crypto 在 Python 中读取证书,如如何使用 python 检索远程主机的 TLS/SSL 对等证书?,但我希望能够使用不受信任的证书来做同样的事情。
不可信,我的意思是我(例如)刚刚生成的证书并将 csr 输出粘贴到我的 python 函数中。
这是我生成 csr 的方式:
openssl req -new -newkey rsa:2048 -nodes -keyout domain.key -out domain.csr
Run Code Online (Sandbox Code Playgroud)
并将 domain.csr 的输出用于之前 SO 问题中的代码。
正如预期的那样,这行不通。为了更快,使用 Openssl 执行相同操作会导致相同的错误:
openssl x509 -text -in Domain.csr -noout
Run Code Online (Sandbox Code Playgroud)
无法加载证书 139698977908608:error:0906D06C:PEMroutines:PEM_read_bio:no start >line:pem_lib.c:703:Expecting: TRUSTED CERTIFICATE
期望:受信任的证书是这里的关键问题。
我能怎么做 ?
感谢您的帮助 !
更新:通过搜索更多,我读到如果私钥在此 SO 答案/sf/answers/861909611/ 中的证书(又名 pem)内,这将起作用,但我正在尝试实施是一个可以从来自其他人而不是我的证书中提取信息的系统。而且我很确定“其他人”不会想要分享他们的私钥,所以我仍然被卡住了。有任何想法吗 ?
更新 2: x509 和 req 证书之间似乎存在差异。如前所述,使用之前的命令生成的是一个请求。
所以现在我现在可以使用第一个 SO 链接读取 x509 证书,但是如何在 python 中读取 req 证书?
好的,我找到了我的答案,我相信它会帮助其他人!
因此,“req”证书和 x509 证书之间存在重要区别!
在 x509 中,私钥也包含在文件中!
因此,在下面,我将展示如何使用 REQ 和 x509 类型的证书生成证书并在 Python 中读取它。
使用“req”证书:
生成它:
openssl req -new -newkey rsa:2048 -nodes -keyout domain.key -out domain.csr
Run Code Online (Sandbox Code Playgroud)
并阅读它:
import OpenSSL
# cert contains the content of domain.csr
OpenSSL.crypto.load_certificate_request(OpenSSL.crypto.FILETYPE_PEM, cert).get_subject().get_components()
Run Code Online (Sandbox Code Playgroud)
使用“x509”证书:
生成它:
openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem
Run Code Online (Sandbox Code Playgroud)
并阅读它:
import OpenSSL
# cert contains the content of cert.pem
OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert).get_subject().get_components()
Run Code Online (Sandbox Code Playgroud)
就是这样 !
不同之处在于您如何加载证书OpenSSL.crypto
:
load_certificate_request()
load_certificate()
就是这么简单;)
归档时间: |
|
查看次数: |
1833 次 |
最近记录: |