-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscasc.py
127 lines (110 loc) · 4.07 KB
/
scasc.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
from smartcard.System import readers
from smartcard.util import toHexString
from config import *
class scasc:
def __init__(self):
self.set_reader = False
self.set_connect = False
def set_sc_reader(self):
r = readers()
r2 = ['%2d : %s' % (i,j) for i,j in zip(range(len(r)),r)]
print('sc readers:')
for i in r2:
print(i)
rn = int(input('Enter number of reader for connection : '))
if rn < 0 or rn > len(r)-1 :
print('Error : inccorect reader number')
return
self.reader = r[rn].createConnection()
self.set_reader = True
def connect(self):
if self.set_reader:
if self.set_connect == False:
self.reader.connect()
self.set_connect = True
else:
print('Error : Already connected')
else:
print('Error : need te set sc reader')
def disconnect(self):
if self.set_reader:
if self.set_connect == True:
self.reader.disconnect()
self.set_connect = False
else:
print('Error : not connected')
else:
print('Error : need te set sc reader')
def print_ATR(self):
atr = self.reader.getATR()
print('ATR : ', ['%02X' % i for i in atr])
def print_APDU(self, apdustr, apdu):
apdu1 = apdu[0:5]
apdu2 = apdu[5:]
print(apdustr, ':', end=' ')
print(''.join(['%02X' % i for i in apdu1]), end=' ')
print(''.join(['%02X' % i for i in apdu2]))
def print_APDU2(self, apdu):
apdu1 = apdu[0:5]
apdu2 = apdu[5:]
print(''.join(['%02X' % i for i in apdu1]), end=' ')
print(''.join(['%02X' % i for i in apdu2]))
def print_DATAwithSW(self, datastr, data, sw1, sw2):
print(datastr, ':', end=' ')
if len(data) > 0:
print(''.join(['%02X' % i for i in data]), end=' ')
print('%02X%02X' % (sw1, sw2))
def print_DATAwithSW2(self, data, sw1, sw2):
if len(data) > 0:
print(''.join(['%02X' % i for i in data]), end=' ')
print('%02X%02X' % (sw1, sw2))
def send_APDU_lc(self, apdu):
print('>> ', end='')
self.print_APDU2(apdu)
ins = apdu[0:5]
data = apdu[5:]
l = int(ins[-1])
if l != len(data):
print('Error : inccorect APDU')
print('\t -> ', end='')
self.print_APDU('APDU', apdu)
return
data, sw1, sw2 = self.reader.transmit(apdu)
print('>> ', end='')
self.print_DATAwithSW2(data, sw1, sw2)
return data, sw1, sw2
def send_APDU_le(self, apdu):
print('>> ', end='')
self.print_APDU2(apdu)
apdu = apdu[0:5]
data, sw1, sw2 = self.reader.transmit(apdu)
print('>> ', end='')
self.print_DATAwithSW2(data, sw1, sw2)
return data, sw1, sw2
'''
def select_DF(self):
print('>> ', end='')
self.print_APDU('selectDF', SELECTDF)
data, sw1, sw2 = self.send_APDU_lc(SELECTDF)
print('<< ', end='')
# self.print_DATAwithSW('response', data, sw1, sw2)
self.print_DATAwithSW2(data, sw1, sw2)
def get_ENCIPHER_APDU(self, text):
GE = GETENCIPHER + [len(text)] + text
print('>> ', end='')
self.print_APDU('getEncipher', GE)
data, sw1, sw2 = self.send_APDU_lc(GE)
print('<< ', end='')
# self.print_DATAwithSW('response', data, sw1, sw2)
self.print_DATAwithSW2(data, sw1, sw2)
return data, sw1, sw2
def get_RESPONSE_APDU(self, length):
GR = GETRESPONSE + [length]
print('>> ', end='')
self.print_APDU('getResponse', GR)
data, sw1, sw2 = self.send_APDU_le(GR)
print('<< ', end='')
# self.print_DATAwithSW('response', data, sw1, sw2)
self.print_DATAwithSW2(data, sw1, sw2)
return data, sw1, sw2
'''