PyKCS11不易腐烂的lis

2024-10-05 12:23:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我设计了一个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]

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

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

^{pr2}$

尝试将属性转储到以下块时,我得到一个不可损坏的类型:“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)

我理解您不能使用列表作为dict中的键,因为dict键需要是不可变的。在这种情况下,如何使用元组?在


Tags: formatif属性sessiontypeattributeslonginc
1条回答
网友
1楼 · 发布于 2024-10-05 12:23:57

(这个答案是在你其他问题的背景下提出的)

要读取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))

一些附加(随机)注释:

  • Session.getAttributeValue() method方法需要属性ID的列表。您正在构建一个“包含属性名称(字符串)属性id(int)的列表”的列表,而不进行任何转换这将无法工作

  • CKA_PRIVATE_EXPONENT属性对RSA私钥敏感。除非CKA_SENSITIVE属性设置为False(参见here

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

  • 上面的代码片段没有使用PyKCS11.前缀来引用PyKCS11对象成员,因为它假设这些成员是用from PyKCS11 import *指令导入的(我还不足以告诉您哪种方法是好的)

  • attribute id<;->;attribute name映射基于这样一个事实,PKCS11.CKA字典既包含带int值的字符串键,也包含带字符串键的int键(您可以自己转储此字典或检查source code

  • 使用print(o)

    转储属性可能要容易得多
  • 我建议阅读PKCS#11 standard

  • (如果引用the origins of your thoughts,您可能会更快地得到答案)

祝你好运!在

相关问题 更多 >

    热门问题