PyKCS11 不可哈希列表

2024-05-02

我的 python 脚本旨在获取特定 .so 库中插槽/令牌的详细信息。输出如下所示:

Library manufacturerID: Safenet, Inc.                   
Available Slots: 4
Slot no: 0
slotDescription: ProtectServer K5E:00045
manufacturerID: SafeNet Inc.
TokenInfo
label: CKM
manufacturerID: SafeNet Inc.
model: K5E:PL25
Opened session 0x00000002

Found 38 objects: [5021, 5022, 5014, 5016, 4, 5, 6, 7, 8, 9, 16, 18, 23, 24, 26, 27, 29, 30, 32, 33, 35, 36, 38, 39, 5313, 5314, 4982, 5325, 5326, 5328, 5329, 5331, 5332, 5335, 5018, 4962, 5020, 4963]

我能够打开会话并获取信息。我遇到可疑问题的地方是检索库中所述键的属性。

我为我的规范所需的所需属性创建了自己的模板,如下:

    all_attributes = PyKCS11.CKA.keys()
    # only use the integer values and not the strings like 'CKM_RSA_PKCS'
    all_attributes = [e for e in all_attributes if isinstance(e, int)]
    attributes = [
            ["CKA_ENCRYPT", PyKCS11.CKA_ENCRYPT],
            ["CKA_CLASS", PyKCS11.CKA_CLASS],
            ["CKA_DECRYPT", PyKCS11.CKA_DECRYPT],
            ["CKA_SIGN", PyKCS11.CKA_SIGN],
            ["CKA_VERIFY", PyKCS11.CKA_VERIFY],
            ["CKA_ID", PyKCS11.CKA_ID],
            ["CKA_MODULUS", PyKCS11.CKA_MODULUS],
            ["CKA_MODULUS", PyKCS11.CKA_MODULUS],
            ["CKA_MODULUS_BITS", PyKCS11.CKA_MODULUS_BITS],
            ["CKA_PUBLIC_EXPONENT", PyKCS11.CKA_PUBLIC_EXPONENT],
            ["CKA_PRIVATE_EXPONENT", PyKCS11.CKA_PRIVATE_EXPONENT],
            ]

当尝试将属性转储到以下块时,我收到不可散列的类型:'list' TypeError:

print "Dumping attributes:"
        for q, a in zip(all_attributes, attributes):
            if a == None:
                # undefined (CKR_ATTRIBUTE_TYPE_INVALID) attribute
                continue
            if q == PyKCS11.CKA_CLASS:
                print format_long % (PyKCS11.CKA[q], PyKCS11.CKO[a], a)
            elif q == PyKCS11.CKA_CERTIFICATE_TYPE:
                print format_long % (PyKCS11.CKA[q], PyKCS11.CKC[a], a)
            elif q == PyKCS11.CKA_KEY_TYPE:
                print format_long % (PyKCS11.CKA[q], PyKCS11.CKK[a], a)
            elif session.isBin(q):
                print format_binary % (PyKCS11.CKA[q], len(a))
                if a:
                    print dump(''.join(map(chr, a)), 16),
            elif q == PyKCS11.CKA_SERIAL_NUMBER:
                print format_binary % (PyKCS11.CKA[q], len(a))
                if a:
                    print hexdump(a, 16),
            else:
                print format_normal % (PyKCS11.CKA[q], a)

该行具体生成了错误:

if q == PyKCS11.CKA_CLASS:
            print format_long % (PyKCS11.CKA[q], PyKCS11.CKO[a], a)

我知道您不能使用列表作为字典中的键,因为字典键必须是不可变的。在这种情况下我将如何使用元组?


(这个答案是在你的其他问题的背景下综合起来的)

读取 PKCS#11 对象的属性o您可以使用以下代码:

# List which attributes you want to read
attributeIds = [
    CKA_ENCRYPT,
    CKA_CLASS,
    CKA_DECRYPT,
    CKA_SIGN,
    CKA_VERIFY,
    CKA_ID,
    CKA_MODULUS,
    CKA_MODULUS_BITS,
    CKA_PUBLIC_EXPONENT,
    CKA_PRIVATE_EXPONENT
]

# Read them
attributeValues = session.getAttributeValue(o, attributeIds)

# Print them (variant 1 -- more readable)
for i in range(0,len(attributeIds)):
    attributeName = CKA[attributeIds[i]]
    print("Attribute %s: %s" % (attributeName, attributeValues[i]))

# Print them (variant 2 -- more consise)
for curAttrId, currAttrVale in zip(attributeIds,attributeValues):
    attributeName = CKA[curAttrId]
    print("Attribute %s: %s" % (attributeName, currAttrVale))

一些附加(随机)注释:

  • the Session.getAttributeValue() 方法 http://pkcs11wrap.sourceforge.net/api/PyKCS11.Session-class.html#getAttributeValue方法需要属性 ID 列表。您正在构建一个“列表包含属性名称(字符串) and 属性 ID(整数)“——没有任何转换——这是行不通的

  • the CKA_PRIVATE_EXPONENT属性对 RSA 私钥敏感。您可能无法阅读它,除非CKA_SENSITIVE属性设置为False(参见例如here https://stackoverflow.com/a/14933297/5128464)

  • 确保只读取特定对象的有效属性(基于类型、机制、敏感性......)

  • 上面的代码片段没有使用PyKCS11.引用 PyKCS11 对象成员的前缀,因为它假设它们是通过导入的from PyKCS11 import *指令(我对 python 的了解还不够,无法告诉你哪种方式是好的)

  • 属性 id 属性名称映射基于以下事实:PKCS11.CKA字典包含带有 int 值的字符串键和带有字符串键的 int 键(您可以自己转储此字典或检查)

  • 转储属性可能会更容易print(o)

  • 我建议阅读相关部分PKCS#11标准 https://www.emc.com/emc-plus/rsa-labs/standards-initiatives/pkcs-11-cryptographic-token-interface-standard.htm

  • (如果您引用了,您可能会更快地得到答案)

祝你好运!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PyKCS11 不可哈希列表 的相关文章

随机推荐