Python asn.1 DER 编码序列命名类型只能强制转换标量值



我正在尝试构建一个包含整数可变大小列表的 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列表,或者我的类型定义有问题?

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

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()

最新更新