Changeset 182
- Timestamp:
- 11/16/09 11:18:22 (9 months ago)
- Files:
-
- pykcs11/trunk/PyKCS11/__init__.py (modified) (31 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
pykcs11/trunk/PyKCS11/__init__.py
r172 r182 25 25 CK_INVALID_HANDLE = PyKCS11.LowLevel.CK_INVALID_HANDLE 26 26 27 CKM = {} ;28 CKR = {} ;29 CKA = {} ;30 CKO = {} ;31 CKU = {} ;32 CKK = {} ;33 CKC = {} ;34 CKF = {} ;35 CKS = {} ;27 CKM = {} 28 CKR = {} 29 CKA = {} 30 CKO = {} 31 CKU = {} 32 CKK = {} 33 CKC = {} 34 CKF = {} 35 CKS = {} 36 36 37 37 # redefine PKCS#11 constants using well known prefixes … … 45 45 or x[:4] == 'CKC_' \ 46 46 or x[:4] == 'CKF_' \ 47 or x[:4] == 'CKS_' \ 48 : 49 a = "%s=PyKCS11.LowLevel.%s" % (x, x) 47 or x[:4] == 'CKS_': 48 a = "%s=PyKCS11.LowLevel.%s" % (x, x) 50 49 exec(a) 51 50 if x[3:] != "_VENDOR_DEFINED": … … 56 55 CKR[-2] = "Unkown PKCS#11 type" 57 56 CKR[-1] = "Load" 57 58 58 59 59 class CK_SLOT_INFO(object): … … 76 76 CKF_TOKEN_PRESENT: "CKF_TOKEN_PRESENT", 77 77 CKF_REMOVABLE_DEVICE: "CKF_REMOVABLE_DEVICE", 78 CKF_HW_SLOT: "CKF_HW_SLOT" 79 } 78 CKF_HW_SLOT: "CKF_HW_SLOT"} 80 79 81 80 def flags2text(self): … … 92 91 r.append(CK_SLOT_INFO.flags_dict[v]) 93 92 return r 93 94 94 95 95 class CK_INFO(object): … … 109 109 """ 110 110 111 111 112 class CK_SESSION_INFO(object): 112 113 """ … … 151 152 """ 152 153 return CKS[self.state] 154 153 155 154 156 class CK_TOKEN_INFO(object): … … 229 231 return r 230 232 233 231 234 class CK_MECHANISM_INFO(object): 232 235 """ … … 272 275 return r 273 276 277 274 278 class PyKCS11Error(Exception): 275 279 """ define the possible PKCS#11 error codes """ 276 280 277 def __init__(self, value, text =""):281 def __init__(self, value, text=""): 278 282 self.value = value 279 283 self.text = text … … 289 293 return CKR[self.value] + " (0x%08X)" % self.value 290 294 295 291 296 class PyKCS11Lib(object): 292 297 """ high level PKCS#11 binding """ … … 298 303 self.lib.Unload() 299 304 300 def load(self, pkcs11dll_filename =None, *init_string):305 def load(self, pkcs11dll_filename=None, *init_string): 301 306 """ 302 307 load a PKCS#11 library … … 382 387 @return: a L{CK_TOKEN_INFO} object 383 388 """ 384 tokeninfo = PyKCS11.LowLevel.CK_TOKEN_INFO()389 tokeninfo = PyKCS11.LowLevel.CK_TOKEN_INFO() 385 390 rv = self.lib.C_GetTokenInfo(slot, tokeninfo) 386 391 if rv != CKR_OK: … … 428 433 return t 429 434 430 def openSession(self, slot, flags =0):435 def openSession(self, slot, flags=0): 431 436 """ 432 437 C_OpenSession … … 486 491 return i 487 492 488 def waitForSlotEvent(self, flags =0):493 def waitForSlotEvent(self, flags=0): 489 494 """ 490 495 C_WaitForSlotEvent … … 502 507 return slot 503 508 509 504 510 class Mechanism(object): 505 511 """Wraps CK_MECHANISM""" 512 506 513 def __init__(self, mechanism, param): 507 514 """ … … 511 518 (i.e. the IV for some agorithms) 512 519 @type param: string or list/tuple of bytes 513 520 514 521 @see: L{Session.decrypt}, L{Session.sign} 515 522 """ … … 518 525 519 526 MechanismRSAPKCS1 = Mechanism(CKM_RSA_PKCS, None) 527 520 528 521 529 class Session(object): … … 556 564 return s 557 565 558 def login(self, pin, user_type =CKU_USER):566 def login(self, pin, user_type=CKU_USER): 559 567 """ 560 568 C_Login … … 603 611 if rv != CKR_OK: 604 612 raise PyKCS11Error(rv) 605 613 606 614 def sign(self, key, data, mecha=MechanismRSAPKCS1): 607 615 """ 608 616 C_SignInit/C_Sign 609 617 610 618 @param key: a key handle, obtained calling L{findObjects}. 611 619 @type key: integer … … 613 621 @type data: (binary) sring or list/tuple of bytes 614 622 @param mecha: the signing mechanism to be used 615 @type mecha: L{Mechanism} instance or L{MechanismRSAPKCS1} 623 @type mecha: L{Mechanism} instance or L{MechanismRSAPKCS1} 616 624 for CKM_RSA_PKCS 617 625 @return: the computed signature 618 626 @rtype: list of bytes 619 627 620 628 @note: the returned value is an istance of L{LowLevel.ckbytelist}. 621 629 You can easly convert it to a binary string with:: 622 630 ''.join(chr(i) for i in ckbytelistSignature) 623 631 624 632 """ 625 633 m = PyKCS11.LowLevel.CK_MECHANISM() … … 651 659 raise PyKCS11Error(rv) 652 660 #first call get signature size 653 rv = self.lib.C_Sign(self.session, data1, signature) ;661 rv = self.lib.C_Sign(self.session, data1, signature) 654 662 if (rv != 0): 655 663 raise PyKCS11Error(rv) 656 664 #second call get actual signature data 657 rv = self.lib.C_Sign(self.session, data1, signature) ;665 rv = self.lib.C_Sign(self.session, data1, signature) 658 666 if (rv != 0): 659 667 raise PyKCS11Error(rv) 660 668 return signature 661 669 662 670 def decrypt(self, key, data, mecha=MechanismRSAPKCS1): 663 671 """ 664 672 C_DecryptInit/C_Decrypt 665 673 666 674 @param key: a key handle, obtained calling L{findObjects}. 667 675 @type key: integer … … 673 681 @return: the decrypted data 674 682 @rtype: list of bytes 675 683 676 684 @note: the returned value is an istance of L{LowLevel.ckbytelist}. 677 685 You can easly convert it to a binary string with:: 678 686 ''.join(chr(i) for i in ckbytelistData) 679 687 680 688 """ 681 689 m = PyKCS11.LowLevel.CK_MECHANISM() … … 707 715 raise PyKCS11Error(rv) 708 716 #first call get decrypted size 709 rv = self.lib.C_Decrypt(self.session, data1, decrypted) ;717 rv = self.lib.C_Decrypt(self.session, data1, decrypted) 710 718 if (rv != 0): 711 719 raise PyKCS11Error(rv) 712 720 #second call get actual decrypted data 713 rv = self.lib.C_Decrypt(self.session, data1, decrypted) ;721 rv = self.lib.C_Decrypt(self.session, data1, decrypted) 714 722 if (rv != 0): 715 723 raise PyKCS11Error(rv) 716 724 return decrypted 717 725 718 726 def isNum(self, type): 719 727 if type in (CKA_CERTIFICATE_TYPE, … … 759 767 return (not self.isBool(type)) and (not self.isString(type)) and (not self.isNum(type)) 760 768 761 def findObjects(self, template =()):769 def findObjects(self, template=()): 762 770 """ 763 771 find the objects matching the template pattern … … 802 810 return res 803 811 804 def getAttributeValue(self, obj_id, attr, allAsBinary =False):812 def getAttributeValue(self, obj_id, attr, allAsBinary=False): 805 813 """ 806 814 C_GetAttributeValue … … 814 822 @return: a list of values corresponding to the list of attributes 815 823 @rtype: list 816 824 817 825 @see: L{getAttributeValue_fragmented} 818 826 819 827 @note: if allAsBinary is True the function do not convert results to 820 828 Python types (i.e.: CKA_TOKEN to Bool, CKA_CLASS to int, ...). … … 823 831 You can easly convert it to a binary string with:: 824 832 ''.join(chr(i) for i in ckbytelistVariable) 825 833 826 834 """ 827 835 valTemplate = PyKCS11.LowLevel.ckattrlist(len(attr)) … … 858 866 return res 859 867 860 def getAttributeValue_fragmented(self, obj_id, attr, allAsBinary =False):868 def getAttributeValue_fragmented(self, obj_id, attr, allAsBinary=False): 861 869 """ 862 870 Same as L{getAttributeValue} except that when some attribute 863 871 is sensitive or unknown an empty value (None) is retruned. 864 872 865 873 Note: this is achived getting attributes one by one. 866 874 867 875 @see: L{getAttributeValue} 868 876 """ … … 888 896 if rv != CKR_OK: 889 897 raise PyKCS11Error(rv) 890 898 891 899 if (allAsBinary): 892 900 res.append(valTemplate[0].GetBin()) … … 971 979 print 972 980 print "login" 973 se.login(pin ="12345678")981 se.login(pin="12345678") 974 982 975 983 print … … 1001 1009 print "closeSession" 1002 1010 se.closeSession() 1003
