File: test.py

package info (click to toggle)
pykcs11 1.5.18-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 808 kB
  • sloc: python: 6,520; cpp: 1,424; ansic: 1,360; makefile: 77; sh: 18
file content (137 lines) | stat: -rwxr-xr-x 4,612 bytes parent folder | download
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/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
from PyKCS11 import ckbytelist

# pylint: disable=duplicate-code

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 = ckbytelist("1234")

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(0, slotList)))
print("\tAvailable Slots: " + str(len(slotList)))

for index, slot in enumerate(slotList):
    print("\tC_SlotInfo(): " + hex(a.C_GetSlotInfo(slot, slotInfo)))
    print(
        "\t\tSlot N."
        + str(index)
        + ": ID="
        + str(slot)
        + ", name='"
        + slotInfo.GetSlotDescription()
        + "'"
    )
    print(
        "\tC_OpenSession(): "
        + hex(
            a.C_OpenSession(
                slot,
                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_GetTokenInfo(): " + hex(a.C_GetTokenInfo(slot, tokenInfo)))
    print(
        "\t\tTokenInfo: Label="
        + tokenInfo.GetLabel()
        + ", ManufacturerID="
        + tokenInfo.GetManufacturerID()
    )
    print(
        "\t\tTokenInfo: flags="
        + hex(tokenInfo.flags)
        + ", Model="
        + tokenInfo.GetModel()
    )

    print("\tC_Login(): " + hex(a.C_Login(session, PyKCS11.LowLevel.CKU_USER, pin)))
    print("\tC_Logout(): " + hex(a.C_Logout(session)))
    print("\tC_CloseSession(): " + hex(a.C_CloseSession(session)))

print(
    "C_OpenSession(): "
    + hex(a.C_OpenSession(slotList[0], PyKCS11.LowLevel.CKF_SERIAL_SESSION, session))
)
print("C_Login(): " + hex(a.C_Login(session, PyKCS11.LowLevel.CKU_USER, pin)))

SearchResult = PyKCS11.LowLevel.ckulonglist(10)
SearchTemplate = PyKCS11.LowLevel.ckattrlist(2)
SearchTemplate[0].SetNum(PyKCS11.LowLevel.CKA_CLASS, PyKCS11.LowLevel.CKO_CERTIFICATE)
SearchTemplate[1].SetBool(PyKCS11.LowLevel.CKA_TOKEN, True)

print("C_FindObjectsInit: " + hex(a.C_FindObjectsInit(session, SearchTemplate)))
print("C_FindObjects: " + hex(a.C_FindObjects(session, SearchResult)))
print("C_FindObjectsFinal: " + hex(a.C_FindObjectsFinal(session)))

for x in SearchResult:
    print("object: " + hex(x.value()))
    valTemplate = PyKCS11.LowLevel.ckattrlist(2)
    valTemplate[0].SetType(PyKCS11.LowLevel.CKA_LABEL)
    # valTemplate[0].Reserve(128)
    valTemplate[1].SetType(PyKCS11.LowLevel.CKA_CLASS)
    # valTemplate[1].Reserve(4)
    print(
        "C_GetAttributeValue(): " + hex(a.C_GetAttributeValue(session, x, valTemplate))
    )
    print(
        "CKA_LABEL Len: ",
        valTemplate[0].GetLen(),
        " CKA_CLASS Len: ",
        valTemplate[1].GetLen(),
    )
    print(
        "C_GetAttributeValue(): " + hex(a.C_GetAttributeValue(session, x, valTemplate))
    )
    print("\tCKO_CERTIFICATE: " + valTemplate[0].GetString())
    print("\tCKA_TOKEN: " + str(valTemplate[1].GetNum()))

print("C_Logout(): " + hex(a.C_Logout(session)))
print("C_CloseSession(): " + hex(a.C_CloseSession(session)))
print("C_Finalize(): " + hex(a.C_Finalize()))
print(a.Unload())