【问题标题】:Python asn.1 DER encoding Sequence NamedTypes can cast only scalar valuesPython asn.1 DER 编码序列 NamedTypes 只能转换标量值
【发布时间】:2019-11-22 06:34:59
【问题描述】:

我正在尝试构建一个包含可变大小整数列表的 ASN.1 类型,并像这样尝试过

class ASNBigInteger(Integer):
    """
    A subtype of the pyasn1 Integer type to support
    bigger numbers
    """
    subtypeSpec = ValueRangeConstraint(0x1, 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141)

class ANSBigIntegerList(univ.SequenceOf):
    """
    A subytpe of Sequenceof for variable length
    list of BigIntegers
    """
    componentType = ASNBigInteger()
    subtypeSpec = ValueSizeConstraint(1, 9223372036854775807)

class ASNMLSAGSignature(Sequence):
    """
    ASN.1 type specification for MLSAG
    Ring Signature
    """
    componentType = NamedTypes(
        NamedType('Ix', ASNBigInteger()),
        NamedType('Iy', ASNBigInteger()),
        NamedType('c0', ASNBigInteger()),
        NamedType('sl', ANSBigIntegerList())
    )

尝试使用这种类型:

def get_asn1_encoded(self) -> str:
    """
    Get the ring signature as der encoded
    :return: asn encoded signature
    """
    asn = ASNMLSAGSignature()
    asn["Ix"] = self.I().x()
    asn["Iy"] = self.I().y()
    asn["c0"] = self.c0()
    asn["sl"] = self.sl()
    serialized = encode(asn)
    return serialized.hex()

(注意 self.sl() 返回一个整数列表) 在我设置 sl 值的那一行,我得到了这个错误:

KeyError: PyAsn1Error('NamedTypes can cast only scalar values',)

我需要如何将 python 列表转换为 ASN.1 列表,或者我的类型定义有什么问题吗?

【问题讨论】:

    标签: python asn.1 der


    【解决方案1】:

    设法通过将列表类型更改为:(删除不必要的约束)来解决它

    class ANSIntegerList(univ.SequenceOf):
        """
        A subytpe of Sequenceof for variable length
        list of BigIntegers
        """
        componentType = ASNBigInteger()
    

    和编码部分:(创建列表对象并使用扩展函数添加值)

    def get_asn1_encoded(self) -> str:
        """
        Get the ring signature as der encoded
        :return: asn encoded signature
        """
        asn = ASNMLSAGSignature()
        asn["Ix"] = self.I().x()
        asn["Iy"] = self.I().y()
        asn["c0"] = self.c0()
        asnlist = ANSIntegerList()
        asnlist.extend(self.sl())
        asn["sl"] = asnlist
        serialized = encode(asn)
        return serialized.hex()
    

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 1970-01-01
      • 2015-12-10
      • 2012-10-23
      • 2017-08-20
      • 2010-11-19
      • 2011-07-20
      • 2011-11-11
      • 2011-04-07
      相关资源
      最近更新 更多