验证符号中的M2Crypto分段错误

时间:2016-08-07 09:40:17

标签: python cryptography digital-signature m2crypto

我尝试在提交之前向json请求添加一个sign字段,并在响应中验证它。我所做的是在初始化时加载一次密钥对然后(根据this gist)在我的类中使用以下方法:

import M2Crypto

class MyRequestHandler:

    SIGN_PRIVATE = M2Crypto.EVP.load_key('/path/to/signing/key')
    SIGN_PUBLIC = M2Crypto.RSA.load_pub_key('/path/to/verification/key')

    def __init__(self):
        pass

    # signing
    @classmethod
    def sign(cls, data):
        cls.SIGN_PRIVATE.sign_init()
        cls.SIGN_PRIVATE.sign_update(data)
        signed_data = cls.SIGN_PRIVATE.sign_final()
        signed_data_base64 = signed_data.encode('base64')
        return signed_data_base64

    # verifying
    @classmethod
    def verify(cls, response):
        signature = response['sign'].decode('base64')
        data = response['data']
        verify_evp = M2Crypto.EVP.PKey()
        verify_evp.assign_rsa(cls.SIGN_PUBLIC)
        verify_evp.verify_init()
        verify_evp.verify_update(data)
        return verify_evp.verify_final(signature) == 1

问题是verify在调用一次时工作正常,但无论数据和签名是什么,再次调用它都会导致分段错误。为什么会这样?一般来说,我是以正确的方式完成这个过程吗?

编辑:使用从签名中使用的私钥中提取的公钥来重现python shell中的问题(考虑我们要验证自己的符号):

data = "test message"
signature = MeyRequestHandler.sign(data)
res = {"data": data, "sign": signature}
MyRequestHandler.verify(res)  # works fine
MyRequestHandler.verify(res)  # segmentation fault in *verify_final*

M2Crypto版本为0.25.1

0 个答案:

没有答案