我必须合并并可能重写一堆 bash 脚本,以验证传入的 smime 消息是否有效(即用公司的私钥加密并签署一组特定的公钥)
这一堆 bash 将被一个小型应用程序取代,可能在 M2Crypto 的帮助下用 Python 编写。
到目前为止,解密部分真的很顺利,但是我在签名验证方面遇到了问题。
我需要编写 python 代码来替换这个单一的 bash 行
openssl smime -verify -in to_verify.txt -CAfile signer_pubkey.pem -out verified.txt
to_verify.txt 的内容是“通常的”multipart/signed p7,可以附加或不附加签名。
验证成功时,前面的命令以 0 退出,并从 smime 信封中提取内容。
现在,回到 python,取自 m2crypto 示例:
import os
from M2Crypto import BIO, Rand, SMIME, X509
cert_dir = '/home/niphlod/certs'
doc_dir = '/home/niphlod/datastore'
signer = os.path.join(cert_dir, 'signer_pubkey.pem')
letter = os.path.join(doc_dir,'out_decrypt.txt')
# Instantiate an SMIME object.
s = SMIME.SMIME()
# Load the signer's cert.
x509 = X509.load_cert(signer)
sk = X509.X509_Stack()
sk.push(x509)
s.set_x509_stack(sk)
# Load the signer's CA cert. They're all self-signed, hence the following
st = X509.X509_Store()
st.load_info(signer)
s.set_x509_store(st)
# Load the data, verify it.
p7, data = SMIME.smime_load_pkcs7(letter)
v = s.verify(p7)
print v
print data
print data.read()
嗯....惊喜,我明白了
Traceback (most recent call last):
File "m2crypto_verify.py", line 28, in <module>
v = s.verify(p7)
File "/usr/lib/pymodules/python2.6/M2Crypto/SMIME.py", line 215, in verify
blob = m2.pkcs7_verify0(p7, self.x509_stack._ptr(), self.x509_store._ptr(), flags)
M2Crypto.SMIME.PKCS7_Error: no content
Openssl 正在正确读取、提取和验证这些文件,但 m2crypto 如何报告没有内容?
BUMP:没有人对此感兴趣?