Package PyKCS11 :: Class Session
[hide private]
[frames] | no frames]

Class Session

source code

object --+
         |
        Session

Manage PyKCS11Lib.openSession objects

Instance Methods [hide private]
 
__init__(self, pykcs11, session)
x.__init__(...) initializes x; see help(type(x)) for signature
source code
 
lib(self)
Get the low level lib of the owning PyKCS11Lib
source code
 
closeSession(self)
C_CloseSession
source code
 
getSessionInfo(self)
C_GetSessionInfo
source code
 
login(self, pin, user_type=CKU_USER)
C_Login
source code
 
logout(self)
C_Logout
source code
 
initPin(self, new_pin)
C_InitPIN
source code
 
setPin(self, old_pin, new_pin)
C_SetPIN
source code
 
createObject(self, template)
C_CreateObject
source code
 
destroyObject(self, obj)
C_DestroyObject
source code
DigestSession
digestSession(self, mecha=MechanismSHA1)
C_DigestInit/C_DigestUpdate/C_DigestKey/C_DigestFinal
source code
list of bytes
digest(self, data, mecha=MechanismSHA1)
C_DigestInit/C_Digest
source code
list of bytes
sign(self, key, data, mecha=MechanismRSAPKCS1)
C_SignInit/C_Sign
source code
bool
verify(self, key, data, signature, mecha=MechanismRSAPKCS1)
C_VerifyInit/C_Verify
source code
list of bytes
encrypt(self, key, data, mecha=MechanismRSAPKCS1)
C_EncryptInit/C_Encrypt
source code
list of bytes
decrypt(self, key, data, mecha=MechanismRSAPKCS1)
C_DecryptInit/C_Decrypt
source code
list of bytes
wrapKey(self, wrappingKey, key, mecha=MechanismRSAPKCS1)
C_WrapKey
source code
integer
unwrapKey(self, unwrappingKey, wrappedKey, template, mecha=MechanismRSAPKCS1)
C_UnwrapKey
source code
bool
isNum(self, type)
is the type a numerical value?
source code
bool
isString(self, type)
is the type a string value?
source code
bool
isBool(self, type)
is the type a boolean value?
source code
bool
isBin(self, type)
is the type a byte array value?
source code
 
_template2ckattrlist(self, template) source code
PyKCS11.LowLevel.CK_OBJECT_HANDLE
generateKey(self, template, mecha=MechanismAESGENERATEKEY)
generate a secret key
source code
tuple
generateKeyPair(self, templatePub, templatePriv, mecha=MechanismRSAGENERATEKEYPAIR)
generate a key pair
source code
list
findObjects(self, template=())
find the objects matching the template pattern
source code
list
getAttributeValue(self, obj_id, attr, allAsBinary=False)
C_GetAttributeValue
source code
 
getAttributeValue_fragmented(self, obj_id, attr, allAsBinary=False)
Same as getAttributeValue except that when some attribute is sensitive or unknown an empty value (None) is returned.
source code
 
seedRandom(self, seed)
C_SeedRandom
source code
 
generateRandom(self, size=16)
C_GenerateRandom
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, pykcs11, session)
(Constructor)

source code 

x.__init__(...) initializes x; see help(type(x)) for signature

Parameters:
  • pykcs11 (PyKCS11Lib) - PyKCS11 library object
  • session (instance of CK_SESSION_HANDLE) - session handle
Overrides: object.__init__

lib(self)

source code 

Get the low level lib of the owning PyKCS11Lib

Decorators:
  • @property

getSessionInfo(self)

source code 

C_GetSessionInfo

Returns:
a CK_SESSION_INFO object

login(self, pin, user_type=CKU_USER)

source code 

C_Login

Parameters:
  • pin (string) - the user's PIN or None for CKF_PROTECTED_AUTHENTICATION_PATH
  • user_type (integer) - the user type. The default value is CKU_USER. You may also use CKU_SO

initPin(self, new_pin)

source code 

C_InitPIN

Parameters:
  • new_pin - new PIN

setPin(self, old_pin, new_pin)

source code 

C_SetPIN

Parameters:
  • old_pin - old PIN
  • new_pin - new PIN

createObject(self, template)

source code 

C_CreateObject

Parameters:
  • template - object template

destroyObject(self, obj)

source code 

C_DestroyObject

Parameters:
  • obj - object ID

digestSession(self, mecha=MechanismSHA1)

source code 

C_DigestInit/C_DigestUpdate/C_DigestKey/C_DigestFinal

Parameters:
Returns: DigestSession
A DigestSession object

digest(self, data, mecha=MechanismSHA1)

source code 

C_DigestInit/C_Digest

Parameters:
  • data ((binary) sring or list/tuple of bytes) - the data to be digested
  • mecha (Mechanism instance or MechanismSHA1 for CKM_SHA_1) - the digesting mechanism to be used
Returns: list of bytes
the computed digest

Note: the returned value is an istance of ckbytelist. You can easly convert it to a binary string with:

   bytes(ckbytelistDigest)

or, for Python 2:

   ''.join(chr(i) for i in ckbytelistDigest)

sign(self, key, data, mecha=MechanismRSAPKCS1)

source code 

C_SignInit/C_Sign

Parameters:
  • key (integer) - a key handle, obtained calling findObjects.
  • data ((binary) string or list/tuple of bytes) - the data to be signed
  • mecha (Mechanism instance or MechanismRSAPKCS1 for CKM_RSA_PKCS) - the signing mechanism to be used
Returns: list of bytes
the computed signature

Note: the returned value is an instance of ckbytelist. You can easly convert it to a binary string with:

   bytes(ckbytelistSignature)

or, for Python 2:

   ''.join(chr(i) for i in ckbytelistSignature)

verify(self, key, data, signature, mecha=MechanismRSAPKCS1)

source code 

C_VerifyInit/C_Verify

Parameters:
  • key (integer) - a key handle, obtained calling findObjects.
  • data ((binary) string or list/tuple of bytes) - the data that was signed
  • signature ((binary) string or list/tuple of bytes) - the signature to be verified
  • mecha (Mechanism instance or MechanismRSAPKCS1 for CKM_RSA_PKCS) - the signing mechanism to be used
Returns: bool
True if signature is valid, False otherwise

encrypt(self, key, data, mecha=MechanismRSAPKCS1)

source code 

C_EncryptInit/C_Encrypt

Parameters:
  • key (integer) - a key handle, obtained calling findObjects.
  • data ((binary) string or list/tuple of bytes) - the data to be encrypted
  • mecha (Mechanism instance or MechanismRSAPKCS1 for CKM_RSA_PKCS) - the encryption mechanism to be used
Returns: list of bytes
the encrypted data

Note: the returned value is an instance of ckbytelist. You can easly convert it to a binary string with:

   bytes(ckbytelistEncrypted)

or, for Python 2:

   ''.join(chr(i) for i in ckbytelistEncrypted)

decrypt(self, key, data, mecha=MechanismRSAPKCS1)

source code 

C_DecryptInit/C_Decrypt

Parameters:
  • key (integer) - a key handle, obtained calling findObjects.
  • data ((binary) string or list/tuple of bytes) - the data to be decrypted
  • mecha (Mechanism instance or MechanismRSAPKCS1 for CKM_RSA_PKCS) - the decrypt mechanism to be used
Returns: list of bytes
the decrypted data

Note: the returned value is an instance of ckbytelist. You can easly convert it to a binary string with:

   bytes(ckbytelistData)

or, for Python 2:

   ''.join(chr(i) for i in ckbytelistData)

wrapKey(self, wrappingKey, key, mecha=MechanismRSAPKCS1)

source code 

C_WrapKey

Parameters:
  • wrappingKey (integer) - a wrapping key handle
  • key (integer) - a handle of the key to be wrapped
  • mecha (Mechanism instance or MechanismRSAPKCS1 for CKM_RSA_PKCS) - the encrypt mechanism to be used
Returns: list of bytes
the wrapped key bytes

Note: the returned value is an instance of ckbytelist. You can easily convert it to a binary string with:

   bytes(ckbytelistData)

or, for Python 2:

   ''.join(chr(i) for i in ckbytelistData)

unwrapKey(self, unwrappingKey, wrappedKey, template, mecha=MechanismRSAPKCS1)

source code 

C_UnwrapKey

Parameters:
  • unwrappingKey (integer) - the unwrapping key handle
  • wrappedKey ((binary) string or list/tuple of bytes) - the bytes of the wrapped key
  • template - template for the unwrapped key
  • mecha (Mechanism instance or MechanismRSAPKCS1 for CKM_RSA_PKCS) - the decrypt mechanism to be used
Returns: integer
the unwrapped key object

isNum(self, type)

source code 

is the type a numerical value?

Parameters:
  • type - PKCS#11 type like CKA_CERTIFICATE_TYPE
Returns: bool

isString(self, type)

source code 

is the type a string value?

Parameters:
  • type - PKCS#11 type like CKA_LABEL
Returns: bool

isBool(self, type)

source code 

is the type a boolean value?

Parameters:
  • type - PKCS#11 type like CKA_ALWAYS_SENSITIVE
Returns: bool

isBin(self, type)

source code 

is the type a byte array value?

Parameters:
  • type - PKCS#11 type like CKA_MODULUS
Returns: bool

generateKey(self, template, mecha=MechanismAESGENERATEKEY)

source code 

generate a secret key

Parameters:
  • template - template for the secret key
  • mecha - mechanism to use
Returns: PyKCS11.LowLevel.CK_OBJECT_HANDLE
handle of the generated key

generateKeyPair(self, templatePub, templatePriv, mecha=MechanismRSAGENERATEKEYPAIR)

source code 

generate a key pair

Parameters:
  • templatePub - template for the public key
  • templatePriv - template for the private key
  • mecha - mechanism to use
Returns: tuple
a tuple of handles (pub, priv)

findObjects(self, template=())

source code 

find the objects matching the template pattern

Parameters:
  • template (list) - list of attributes tuples (attribute,value). The default value is () and all the objects are returned
Returns: list
a list of object ids

getAttributeValue(self, obj_id, attr, allAsBinary=False)

source code 

C_GetAttributeValue

Parameters:
  • obj_id (integer) - object ID returned by findObjects
  • attr (list) - list of attributes
  • allAsBinary (Boolean) - return all values as binary data; default is False.
Returns: list
a list of values corresponding to the list of attributes

See Also: getAttributeValue_fragmented

Note: if allAsBinary is True the function do not convert results to Python types (i.e.: CKA_TOKEN to Bool, CKA_CLASS to int, ...).

Binary data is returned as ckbytelist type, usable as a list containing only bytes. You can easly convert it to a binary string with:

   bytes(ckbytelistVariable)

or, for Python 2:

   ''.join(chr(i) for i in ckbytelistVariable)

getAttributeValue_fragmented(self, obj_id, attr, allAsBinary=False)

source code 

Same as getAttributeValue except that when some attribute is sensitive or unknown an empty value (None) is returned.

Note: this is achived by getting attributes one by one.

See Also: getAttributeValue

seedRandom(self, seed)

source code 

C_SeedRandom

Parameters:
  • seed (iterable) - seed material

generateRandom(self, size=16)

source code 

C_GenerateRandom

Parameters:
  • size (integer) - number of random bytes to get

Note: the returned value is an instance of ckbytelist. You can easly convert it to a binary string with:

   bytes(random)

or, for Python 2:

   ''.join(chr(i) for i in random)