PyKCS11 unhashable list
<p>A python script of mine is designed to get detailed information of slots/tokens in a particular .so library. The output looks like this:</p>

<pre><code>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]
</code></pre>

<p>I am able to open the session and get the information. Where I run into dubious problems is retrieving the attributes of said keys in the library.</p>

<p>I created my own template for desired attributes needed for my specifications, the following:</p>

<pre><code>    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],
            ]
</code></pre>

<p>I'm getting an unhashable type: 'list' TypeError when trying to dump the attributes on the following block: </p>

<pre><code>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)
</code></pre>

<p>This line specifically is generating the error: </p>

<pre><code>if q == PyKCS11.CKA_CLASS:
            print format_long % (PyKCS11.CKA[q], PyKCS11.CKO[a], a)
</code></pre>

<p>I understand that you can't use a list as the key in a dict, since dict keys need to be immutable. How would I use a tuple in this situation?</p>

<p>(This answer was put together in the context of your other questions)</p>

<p>To read attributes of a PKCS#11 object <code>o</code> you can use the following code:</p>

<pre><code># 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))
</code></pre>

<p>Some additional (random) notes:</p>

<ul>
<li><p>the <a href="http://pkcs11wrap.sourceforge.net/api/PyKCS11.Session-class.html#getAttributeValue" rel="nofollow">Session.getAttributeValue() method</a> method requires a list of attribute ids. You are constructing a list of "lists containing <em>Attribute name (string)</em> and <em>Attribute id (int)</em>" -- without any conversion -- this can't work</p></li>
<li><p>the <code>CKA_PRIVATE_EXPONENT</code> attribute is sensitive for RSA private keys. You probably won't be able to read it unless the <code>CKA_SENSITIVE</code> attribute is set to <code>False</code> (see e.g. <a href="http://stackoverflow.com/a/14933297/5128464">here</a>)</p></li>
<li><p>be sure to read only valid attributes for specific object (based on type, mechanism, sensitivity...)</p></li>
<li><p>the snippet above does not use the <code>PyKCS11.</code> prefix to reference PyKCS11 object members as it assumes they are imported with <code>from PyKCS11 import *</code> directive (I am not enough into python to tell you which way is the good one)</p></li>
<li><p>the attribute id &lt;-> attribute name mapping is based on fact, that the <code>PKCS11.CKA</code> dictionary contains both string keys with int values and int keys with string keys (you can dump this dictionary yourself or check the <a href="https://bitbucket.org/PyKCS11/pykcs11/src/856b967cefb0e946d4b03614f71ddcaa9cbd8153/PyKCS11/__init__.py?at=1.3.2&amp;fileviewer=file-view-default#__init__.py-77" rel="nofollow">source code</a>)</p></li>
<li><p>it might be much easier to dump the attributes with <code>print(o)</code></p></li>
<li><p>I would recommend reading relevant parts of the <a href="https://www.emc.com/emc-plus/rsa-labs/standards-initiatives/pkcs-11-cryptographic-token-interface-standard.htm" rel="nofollow">PKCS#11 standard</a></p></li>
<li><p>(you might get your answer faster if you referenced <a href="https://bitbucket.org/PyKCS11/pykcs11/src/856b967cefb0e946d4b03614f71ddcaa9cbd8153/samples/dumpit.py?at=1.3.2&amp;fileviewer=file-view-default#dumpit.py-224" rel="nofollow">the origins of your thoughts</a>)</p></li>
</ul>

<p>Good luck!</p>

python list dictionary typeerror pkcs#11