-
Notifications
You must be signed in to change notification settings - Fork 7
/
sle4442.py
executable file
·238 lines (207 loc) · 10.9 KB
/
sle4442.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#! /usr/bin/env python
import sys
from PyQt4 import QtCore, QtGui
from card_ui import Ui_MainWindow
from smartcard.scard import *
import smartcard.util
SELECT = [0xFF, 0xA4, 0x00, 0x00, 0x01, 0x06]
LEGGI = [0xFF, 0xB0, 0x00] # aggiungere indirizzo e lunghezza
SCRIVI = [0xFF, 0xD0, 0x00] # aggiungere indirizzo, lunghezza e bytes
PROTEGGI = [0xFF, 0xD1, 0x00] # aggiungere indirizzo, lunghezza e bytes
SBLOCCA_SCRITTURA = [0xFF, 0x20, 0x00, 0x00, 0x03] # aggiungi PIN
CAMBIA_PIN = [0xFF, 0xD2, 0x00, 0x01, 0x03] # aggiungi PIN
LEGGI_PROT = [0xFF, 0xB2, 0x00, 0x00, 0x04]
PIN = ''
class MyUi(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.hcard = None
self.dwActiveProtocol = None
self.hcontext = None
self.reader = None
self.setWindowTitle('sle4442 manager')
self.PIN_PROT = [self.ui.bit_8, self.ui.bit_7, self.ui.bit_6, self.ui.bit_5, self.ui.bit_4, self.ui.bit_3, self.ui.bit_2, self.ui.bit_1,
self.ui.bit_16, self.ui.bit_15, self.ui.bit_14, self.ui.bit_13, self.ui.bit_12, self.ui.bit_11, self.ui.bit_10, self.ui.bit_9,
self.ui.bit_24, self.ui.bit_23, self.ui.bit_22, self.ui.bit_21, self.ui.bit_20, self.ui.bit_19, self.ui.bit_18, self.ui.bit_17,
self.ui.bit_32, self.ui.bit_31, self.ui.bit_30, self.ui.bit_29, self.ui.bit_28, self.ui.bit_27, self.ui.bit_26, self.ui.bit_25 ]
# segnali
QtCore.QObject.connect(self.ui.leggi,QtCore.SIGNAL("clicked()"), self.leggi_tutto)
QtCore.QObject.connect(self.ui.scrivi,QtCore.SIGNAL("clicked()"), self.scrivi_tutto)
QtCore.QObject.connect(self.ui.connetti,QtCore.SIGNAL("clicked()"), self.connetti)
QtCore.QObject.connect(self.ui.disconnetti,QtCore.SIGNAL("clicked()"), self.disconnetti)
QtCore.QObject.connect(self.ui.sblocca,QtCore.SIGNAL("clicked()"), self.sblocca)
QtCore.QObject.connect(self.ui.change_pin,QtCore.SIGNAL("clicked()"), self.cambia_pin)
QtCore.QObject.connect(self.ui.proteggi,QtCore.SIGNAL("clicked()"), self.proteggi_byte)
def connetti(self):
try:
hresult, self.hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
if hresult != SCARD_S_SUCCESS:
raise Exception('Failed to establish context : ' + SCardGetErrorMessage(hresult))
print 'Context established!'
try:
hresult, readers = SCardListReaders(self.hcontext, [])
if hresult != SCARD_S_SUCCESS:
raise Exception('Failed to list readers: ' + SCardGetErrorMessage(hresult))
print 'PCSC Readers:', readers
if len(readers) < 1:
raise Exception('No smart card readers')
self.reader = readers[0]
print "Using reader:", self.reader
try:
hresult, self.hcard, self.dwActiveProtocol = SCardConnect(self.hcontext, self.reader, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1)
if hresult != SCARD_S_SUCCESS:
raise Exception('Unable to connect: ' + SCardGetErrorMessage(hresult))
print 'Connected with active protocol', self.dwActiveProtocol
try:
hresult, response = SCardTransmit(self.hcard, self.dwActiveProtocol, SELECT)
if hresult != SCARD_S_SUCCESS:
raise Exception('Failed to transmit: ' + SCardGetErrorMessage(hresult))
self.ui.stato_carta.setStyleSheet('color: black')
self.ui.stato_carta.setText('carta connessa in lettura')
self.ui.scrivi.setEnabled(False)
self.ui.change_pin.setEnabled(False)
self.ui.proteggi.setEnabled(False)
self.ui.proteggi_n.setEnabled(False)
print 'Carta correttamente inizializzata'
except Exception, message:
print "Exception:", message
except Exception, message:
print "Exception:", message
except Exception, message:
print "Exception:", message
except Exception, message:
print "Exception:", message
def proteggi_byte(self):
try:
byte = self.ui.proteggi_n.value()-1
hresult, response = SCardTransmit(self.hcard, self.dwActiveProtocol, LEGGI + [byte, 1])
if hresult != SCARD_S_SUCCESS:
raise Exception('Failed to transmit: ' + SCardGetErrorMessage(hresult))
risultato = response[0]
try:
hresult, response = SCardTransmit(self.hcard, self.dwActiveProtocol, PROTEGGI + [byte, 1, risultato])
if hresult != SCARD_S_SUCCESS:
raise Exception('Failed to transmit: ' + SCardGetErrorMessage(hresult))
if (response[-2] == 144):
self.ui.statusbar.showMessage('protetto byte ' + str(byte+1), 4000)
except Exception, message:
print "Exception:", message
except Exception, message:
print "Exception:", message
def leggi_tutto(self):
try:
hresult, response = SCardTransmit(self.hcard, self.dwActiveProtocol, LEGGI + [0, 255])
if hresult != SCARD_S_SUCCESS:
raise Exception('Failed to transmit: ' + SCardGetErrorMessage(hresult))
if (response[-2] == 144):
risultato = response[0:-2]
for i in range(255):
self.ui.dati.setItem(i/8,i%8,QtGui.QTableWidgetItem(chr(risultato[i])))
self.ui.statusbar.showMessage('lettura effettuata', 4000)
except Exception, message:
print "Exception:", message
try:
hresult, response = SCardTransmit(self.hcard, self.dwActiveProtocol,LEGGI_PROT)
if hresult != SCARD_S_SUCCESS:
raise Exception('Failed to transmit: ' + SCardGetErrorMessage(hresult))
if (response[-2] == 144):
risultato = bin(response[0])[2:]+bin(response[1])[2:]+bin(response[2])[2:]+bin(response[3])[2:]
for i in range(32):
if risultato[i] == "1":
self.PIN_PROT[i].setChecked(False)
byte = (i/8)*8+8-(i%8)-1
self.ui.dati.item(byte/8,byte%8).setBackground(QtGui.QColor(240,240,240))
elif risultato[i] == "0":
self.PIN_PROT[i].setChecked(True)
byte = (i/8)*8+8-(i%8)-1
self.ui.dati.item(byte/8,byte%8).setBackground(QtGui.QColor(150,100,100))
else:
raise Exception('Errore nella lettura dello stato di protezione dei byte')
except Exception, message:
print "Exception:", message
def scrivi_tutto(self):
risultato = []
for i in range(255):
c = self.ui.dati.item(i/8,i%8).text()
if c == '':
v = 0x00
else:
v = ord(unicode(c))
risultato.append(v)
try:
hresult, response = SCardTransmit(self.hcard, self.dwActiveProtocol, SCRIVI + [0, 255] + risultato)
if hresult != SCARD_S_SUCCESS:
raise Exception('Failed to transmit: ' + SCardGetErrorMessage(hresult))
if (response[-2] == 144):
self.ui.statusbar.showMessage('scrittura corretta', 4000)
except Exception, message:
print "Exception:", message
def sblocca(self):
try:
PIN = unicode(self.ui.pin.text())
if len(PIN) != 3:
self.ui.stato_carta.setStyleSheet('color: red')
self.ui.stato_carta.setText('pin di tre caratteri')
else:
hresult, response = SCardTransmit(self.hcard, self.dwActiveProtocol, SBLOCCA_SCRITTURA + smartcard.util.toASCIIBytes(PIN))
if hresult != SCARD_S_SUCCESS:
raise Exception('Failed to transmit: ' + SCardGetErrorMessage(hresult))
if response[-1] == 7:
self.ui.stato_carta.setStyleSheet('color: green')
self.ui.stato_carta.setText('carta sbloccata')
self.ui.scrivi.setEnabled(True)
self.ui.change_pin.setEnabled(True)
self.ui.proteggi.setEnabled(True)
self.ui.proteggi_n.setEnabled(True)
print "carta sbloccata e pronta per la scrittura"
elif response[-1] == 0:
print "carta bloccata"
self.ui.stato_carta.setStyleSheet('color: red')
self.ui.stato_carta.setText('carta bloccata')
else:
print "pin errato"
self.ui.stato_carta.setStyleSheet('color: red')
self.ui.stato_carta.setText('pin errato')
except Exception, message:
print "Exception:", message
def cambia_pin(self):
try:
PIN = unicode(self.ui.pin.text())
if len(PIN) != 3:
self.ui.stato_carta.setText('pin di tre caratteri')
else:
hresult, response = SCardTransmit(self.hcard, self.dwActiveProtocol, CAMBIA_PIN + smartcard.util.toASCIIBytes(PIN))
if hresult != SCARD_S_SUCCESS:
raise Exception('Failed to transmit: ' + SCardGetErrorMessage(hresult))
if (response[-2] == 144):
self.ui.statusbar.showMessage('PIN modificato', 4000)
except Exception, message:
print "Exception:", message
def disconnetti(self):
try:
hresult = SCardDisconnect(self.hcard, SCARD_UNPOWER_CARD)
if hresult != SCARD_S_SUCCESS:
raise Exception('Failed to disconnect: ' + SCardGetErrorMessage(hresult))
print 'Disconnected'
self.ui.stato_carta.setStyleSheet('color: black')
self.ui.stato_carta.setText('carta disconnessa')
self.ui.scrivi.setEnabled(False)
self.ui.change_pin.setEnabled(False)
self.ui.proteggi.setEnabled(False)
self.ui.proteggi_n.setEnabled(False)
try:
hresult = SCardReleaseContext(self.hcontext)
if hresult != SCARD_S_SUCCESS:
raise Exception('Failed to release context: ' + SCardGetErrorMessage(hresult))
print 'Released context.'
except Exception, message:
print "Exception:", message
except Exception, message:
print "Exception:", message
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyUi()
myapp.show()
sys.exit(app.exec_())