Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example for dealing with DATA objects to the readme #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,51 @@ Elliptic-Curve Diffie-Hellman
KeyType.AES, 128,
mechanism_param=(KDF.NULL, None, other_value))

Raw Data Objects
~~~~~~~~~~~~~~~~~~

This can be useful for storing symmetric encryption keys and the like. Be sure to set the Attribute PRIVATE to True, otherwise the Objects will be readable even without Pin login.

import os, pkcs11

lib = pkcs11.lib(os.environ["PKCS11_MODULE"])
token = lib.get_token(token_label="DEMO")

demoapp = "python-pkcs11 demo"
demolabel = "testobject"
demodata = "Hello World!".encode("ascii")

with token.open(user_pin="1111") as session:

#write data into an object
session.create_object(
attrs={
pkcs11.Attribute.CLASS: pkcs11.ObjectClass.DATA,
pkcs11.Attribute.APPLICATION: demoapp,
pkcs11.Attribute.LABEL: demolabel,
pkcs11.Attribute.VALUE: demodata,
pkcs11.Attribute.TOKEN: True
})

#retrieve an object
objectfilter = {
pkcs11.Attribute.CLASS: pkcs11.ObjectClass.DATA,
pkcs11.Attribute.LABEL: demolabel
}

#objects are not uniquely identified by their attributes
#the result might be a list of multiple objects with "demolabel"
objects = list(session.get_objects(attrs=objectfilter))
print(objects)

#extract information from an object
print(objects[-1][pkcs11.Attribute.VALUE])

#change an object
objects[-1][pkcs11.Attribute.VALUE] = "testdata".encode("ascii")
print(objects[-1][pkcs11.Attribute.VALUE])


Tested Compatibility
--------------------

Expand Down