-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify_ts_cns_pin.py
executable file
·198 lines (145 loc) · 5 KB
/
verify_ts_cns_pin.py
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This Python script verify_ts_cns_pin.py check if the the PIN code that inserted with the key pad
it's correct (equals to the PIN stored on TS-CNS).
MIT License
Raspberry Pi - Access via Smart Card TS-CNS
Copyright (c) 2020 Antonio Musarra's Blog - https://www.dontesta.it
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in the
Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
__author__ = "Antonio Musarra"
__copyright__ = "Copyright 2020 Antonio Musarra's Blog"
__credits__ = ["Antonio Musarra"]
__version__ = "1.0.0"
__license__ = "MIT"
__maintainer__ = "Antonio Musarra"
__email__ = "[email protected]"
__status__ = "Development"
from pad4pi import rpi_gpio
from modules.PCF8574 import PCF8574_GPIO
from modules.Adafruit_LCD1602 import Adafruit_CharLCD
import time
import sys
import subprocess
# Check I2C address via command i2cdetect -y 1
PCF8574_address = 0x27 # I2C address of the PCF8574 chip.
PCF8574A_address = 0x3F # I2C address of the PCF8574A chip.
# Create PCF8574 GPIO adapter.
try:
mcp = PCF8574_GPIO(PCF8574_address)
except:
try:
mcp = PCF8574_GPIO(PCF8574A_address)
except:
print("I2C Address Error !")
exit(1)
# Create LCD, passing in MCP GPIO adapter.
lcd = Adafruit_CharLCD(pin_rs=0, pin_e=2, pins_db=[4, 5, 6, 7], GPIO=mcp)
KEYPAD = [
[1, 2, 3, "A"],
[4, 5, 6, "B"],
[7, 8, 9, "C"],
["*", 0, "#", "D"]
]
ROW_PINS = [18, 12, 20, 21] # BCM numbering
COL_PINS = [10, 22, 27, 17] # BCM numbering
entered_pin = ""
# CleanUp the resources
def cleanup():
global keypad
lcd.clear()
lcd.message("Goodbye...\n")
lcd.backlight = False
keypad.cleanup()
# Check entered PIN code
def check_pin(key):
global entered_pin
command_to_check_pin_cns = "pkcs11-tool --login --test --verbose --pin " + entered_pin
if len(entered_pin) >= 8 or key == "#":
p = subprocess.Popen(command_to_check_pin_cns, shell=True, stdout=subprocess.PIPE)
(output, err) = p.communicate()
p_status = p.wait()
print("Check PIN: ", output)
if p_status == 0:
correct_pin_entered()
else:
incorrect_pin_entered()
# Display info on corrected PIN code and exit
def correct_pin_entered():
lcd.clear()
lcd.message("Access granted\n")
lcd.message("Accepted PIN\n")
print("PIN accepted. Access granted.")
time.sleep(5)
cleanup()
sys.exit()
# Construct the entered PIN code
def digit_entered(key):
global entered_pin
entered_pin += str(key)
print(entered_pin)
lcd.clear()
lcd.message("PIN: " + entered_pin + "\n")
lcd.message("# to confirm")
check_pin(key)
# Display info on in-corrected PIN code and exit
def incorrect_pin_entered():
lcd.clear()
lcd.message("Access denied\n")
lcd.message("Incorrect PIN\n")
print("Incorrect PIN. Access denied.")
time.sleep(5)
cleanup()
sys.exit()
# Initialize the I2C LCD 1602 Display
def initialize_lcd():
mcp.output(3, 1) # turn on LCD backlight
lcd.begin(16, 2) # set number of LCD lines and columns
lcd.message("Enter your PIN\n")
lcd.message("Press * to clear")
# Manage no PIN code key
def non_digit_entered(key):
global entered_pin
if key == "*" and len(entered_pin) > 0:
entered_pin = entered_pin[:-1]
lcd.clear()
lcd.message("PIN: " + entered_pin + "\n")
lcd.message("# to confirm")
print(entered_pin)
if key == "#" and len(entered_pin) > 0:
check_pin(key)
def key_pressed(key):
try:
int_key = int(key)
if 0 <= int_key <= 9:
digit_entered(key)
except ValueError:
non_digit_entered(key)
try:
factory = rpi_gpio.KeypadFactory()
keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS)
keypad.registerKeyPressHandler(key_pressed)
initialize_lcd()
print("Enter your PIN:")
print("Press * to clear previous digit.")
print("Press # to confirm.")
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Goodbye")
finally:
cleanup()