1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#!/usr/bin/env python3
"""
# Copyright (C) 2004 Midori (midori -- a-t -- paipai dot net)
# Copyright (C) 2018 Ludovic Rousseau (ludovic.rousseau@free.fr)
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
"""
import os
import PyKCS11.LowLevel
a = PyKCS11.LowLevel.CPKCS11Lib()
info = PyKCS11.LowLevel.CK_INFO()
slotInfo = PyKCS11.LowLevel.CK_SLOT_INFO()
lib = os.getenv("PYKCS11LIB")
if lib is None:
raise ValueError("Define PYKCS11LIB")
session = PyKCS11.LowLevel.CK_SESSION_HANDLE()
sessionInfo = PyKCS11.LowLevel.CK_SESSION_INFO()
tokenInfo = PyKCS11.LowLevel.CK_TOKEN_INFO()
slotList = PyKCS11.LowLevel.ckulonglist()
pin = PyKCS11.LowLevel.ckbytelist(b"123456")
puk = PyKCS11.LowLevel.ckbytelist(b"12345678")
Label = "PyKCS#11 Initialized Token "
print("Load of " + lib + ": " + str(a.Load(lib)))
print("C_GetInfo: " + hex(a.C_GetInfo(info)))
print("Library manufacturerID: " + info.GetManufacturerID())
del info
print("C_GetSlotList(NULL): " + hex(a.C_GetSlotList(1, slotList)))
print("\tAvailable Slots: " + str(len(slotList)))
if len(slotList) != 0:
print("\tC_SlotInfo(): " + hex(a.C_GetSlotInfo(slotList[0], slotInfo)))
print("\tC_GetTokenInfo(): " + hex(a.C_GetTokenInfo(slotList[0], tokenInfo)))
print(
"\t\tTokenInfo: Label="
+ tokenInfo.GetLabel()
+ ", ManufacturerID="
+ tokenInfo.GetManufacturerID()
)
print(
"\t\tTokenInfo: flags="
+ hex(tokenInfo.flags)
+ ", Model="
+ tokenInfo.GetModel()
)
print("\tC_InitToken(): " + hex(a.C_InitToken(slotList[0], puk, Label)))
print("\tC_GetTokenInfo(): " + hex(a.C_GetTokenInfo(slotList[0], tokenInfo)))
print(
"\t\tTokenInfo: Label="
+ tokenInfo.GetLabel()
+ ", ManufacturerID="
+ tokenInfo.GetManufacturerID()
)
print(
"\t\tTokenInfo: flags="
+ hex(tokenInfo.flags)
+ ", Model="
+ tokenInfo.GetModel()
)
print(
"\tC_OpenSession(): "
+ hex(
a.C_OpenSession(
slotList[0],
PyKCS11.LowLevel.CKF_SERIAL_SESSION | PyKCS11.LowLevel.CKF_RW_SESSION,
session,
)
)
)
print("\t\tSession:" + str(session))
print("\tC_GetSessionInfo(): " + hex(a.C_GetSessionInfo(session, sessionInfo)))
print(
"\t\tSessionInfo: state="
+ hex(sessionInfo.state)
+ ", flags="
+ hex(sessionInfo.flags)
)
print("\tC_Login(SO): " + hex(a.C_Login(session, PyKCS11.LowLevel.CKU_SO, puk)))
print("\tC_InitPIN(): " + hex(a.C_InitPIN(session, pin)))
print("\tC_Logout(SO): " + hex(a.C_Logout(session)))
print("\tC_GetTokenInfo(): " + hex(a.C_GetTokenInfo(slotList[0], tokenInfo)))
print(
"\t\tTokenInfo: Label="
+ tokenInfo.GetLabel()
+ ", ManufacturerID="
+ tokenInfo.GetManufacturerID()
)
print(
"\t\tTokenInfo: flags="
+ hex(tokenInfo.flags)
+ ", Model="
+ tokenInfo.GetModel()
)
print("C_Login(USER): " + hex(a.C_Login(session, PyKCS11.LowLevel.CKU_USER, pin)))
print("C_Logout(USER): " + hex(a.C_Logout(session)))
print("C_CloseSession(): " + hex(a.C_CloseSession(session)))
print("C_Finalize(): " + hex(a.C_Finalize()))
print(a.Unload())
|