如何在给定参数和键值的情况下创建M2Crypto DSA对象?

时间:2010-06-12 02:07:28

标签: python m2crypto

使用M2Crypto我想创建一个DSA_pub对象来验证DSA签名。我知道q,p,g和公钥,但我知道实例化DSA对象的唯一方法是使用:

dsa = DSA.set_params(q,p,g)
dsa.gen_key()

如何分配已知的公钥?

3 个答案:

答案 0 :(得分:2)

我刚刚遇到了这个挑战,我有P,Q,G和Y参数(在我的例子中来自XML文档),但M2Crypto没有办法让我从他们那里创建一个有效的公钥

我使用pyasn1生成PEM公钥字符串,然后使用M2Crypto.DSA.load_pub_key_bio工厂函数加载该PEM公钥。

我的粗略代码如下,以防将来对某人有用。

import sys
import M2Crypto

if sys.version_info[0] >= 3:
    bin = "{0:#0b}".format
    from functools import reduce

def _a2bits(chars):
    """Convert a string to its bits representation as a tuple of 0's and 1's"""
    return tuple(c == '1' and 1 or 0 for c in (bin(reduce(lambda x, y : (x<<8)+y, (ord(c) for c in chars), 1))[3:]))

def _make_dsa_pubkey_pem(p, q, g, y):
    from pyasn1.type import univ, namedtype
    from pyasn1.codec.der import encoder
    import base64

    class DSSParameters(univ.Sequence):
        componentType = namedtype.NamedTypes(
            namedtype.NamedType('p', univ.Integer()),
            namedtype.NamedType('q', univ.Integer()),
            namedtype.NamedType('g', univ.Integer())
        )

    class AlgorithmIdentifier(univ.Sequence):
        componentType = namedtype.NamedTypes(
            namedtype.NamedType('algorithm', univ.ObjectIdentifier()),
            namedtype.OptionalNamedType('parameters', DSSParameters())
        )

    class SubjectPublicKeyInfo(univ.Sequence):
        componentType = namedtype.NamedTypes(
            namedtype.NamedType('algorithm', AlgorithmIdentifier()),
            namedtype.NamedType('subjectPublicKey', univ.BitString()),
        )

    class DSAPublicKey(univ.Integer):
        pass


    dss_parameters = DSSParameters()
    dss_parameters.setComponentByName('p', p)
    dss_parameters.setComponentByName('q', q)
    dss_parameters.setComponentByName('g', g)

    algorithm_identifier = AlgorithmIdentifier()
    algorithm_identifier.setComponentByName('algorithm', univ.ObjectIdentifier((1, 2, 840, 10040, 4, 1)))
    algorithm_identifier.setComponentByName('parameters', dss_parameters)

    subject_public_key_info = SubjectPublicKeyInfo()
    subject_public_key_info.setComponentByName('algorithm', algorithm_identifier)
    subject_public_key_info.setComponentByName('subjectPublicKey', _a2bits(encoder.encode(DSAPublicKey(y))))

    der = encoder.encode(subject_public_key_info)
    return '-----BEGIN PUBLIC KEY-----\n' + base64.encodestring(der) + '-----END PUBLIC KEY-----\n'


p = 8652574980431835801046702501319893323628737876463029580298337449414347224525946403948627650414713523236662848134622261400464992784181209952478362597409469
q = 1102869237300951505579173947124947290564874845679
g = 4112516799587510153843416910187202701228216851472313407150913894984801048587575223178182928872781591943506026197710239402382269043796703824161282824797865
y = 2998329614411012012383616762831086330705701157164243056626309777500058049666595469116052965199021788182564677073758748878456479902088304265763443201269078
pem = _make_dsa_pubkey_pem(p, q, g, y)
bio = M2Crypto.BIO.MemoryBuffer(pem)
dsapub = M2Crypto.DSA.load_pub_key_bio(bio)

答案 1 :(得分:1)

我最终创建了一个补丁,为M2Crypto添加了pub_key_from_params工厂方法,并且除了功能之外还包括回归测试。在此帖子时,功能请求的状态仍为“新”:https://bugzilla.osafoundation.org/show_bug.cgi?id=12981。在过去的几个月里,它对我有用。如果开发人员觉得它很有用,也许它会包括在内。

答案 2 :(得分:0)

模块DSA中有许多工厂函数超出get_params,我认为你想要load_pub_key(如果你在PEM文件中有公钥和参数)或{{1} (如果你在BIO对象中有它们)。另请参阅BIO模块了解各种BIO对象。