-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsimcard.py
207 lines (169 loc) · 5.66 KB
/
simcard.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Simcard IO Class
(C) 2017 by Sysmocom s.f.m.c. GmbH
All Rights Reserved
Author: Philipp Maier
This program 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, see <http://www.gnu.org/licenses/>.
"""
from card.USIM import USIM
from card.SIM import SIM
from card.utils import *
from utils import *
# Files
GSM_SIM_MF = [0x3F, 0x00]
GSM_SIM_DF_TELECOM = [0x7F, 0x10]
GSM_SIM_DF_GSM = [0x7F, 0x20]
GSM_SIM_EF_ADN = [0x6f,0x3A]
GSM_SIM_EF_IMSI = [0x6F, 0x07]
GSM_SIM_EF_AD = [0x6f, 0xAD]
GSM_SIM_EF_ICCID = [0x2F, 0xE2]
GSM_USIM_EF_DIR = [0x2F, 0x00] # See also: 3GPP TS 31.102 Table 105
# Card types
GSM_SIM = 0
GSM_USIM = 1
# CHV Types
GSM_CHV1 = 0x01
GSM_CHV2 = 0x02
# Record oriented read modes
GSM_SIM_INS_READ_RECORD_NEXT = 0x02
GSM_SIM_INS_READ_RECORD_PREV = 0x03
GSM_SIM_INS_READ_RECORD_ABS = 0x04
# Record oriented write modes
GSM_SIM_INS_UPDATE_RECORD_NEXT = 0x02
GSM_SIM_INS_UPDATE_RECORD_PREV = 0x03
GSM_SIM_INS_UPDATE_RECORD_ABS = 0x04
class Card_res_apdu():
apdu = None
sw = None
# convert Benoit Michau style result to sysmocom style result
def from_mich(self, mich):
self.apdu = mich[3]
self.sw = [ mich[2][0], mich[2][1] ]
def __str__(self):
dump = ""
if len(self.apdu) > 0:
dump = "APDU: " + hexdump(self.apdu)
else:
dump = "APDU: (no data)"
dump += ", SW: " + hexdump(self.sw)
return dump
# A class to abstract a simcard.
class Simcard():
card = None
filelen = 0 #length of the currently selected file
has_isim = False
has_usim = False
# Constructor: Create a new simcard object
def __init__(self, cardtype = GSM_USIM, atr = None):
if cardtype == GSM_USIM:
self.card = USIM(atr)
self.usim = True
# Detect ISIM / USIM applications
self.card.get_AID()
AID = self.card.AID
for a in AID:
if a[0:7] == [0xA0, 0x00, 0x00, 0x00, 0x87, 0x10, 0x04]:
self.has_isim = True
elif a[0:7] == [0xA0, 0x00, 0x00, 0x00, 0x87, 0x10, 0x02]:
self.has_usim = True
else:
self.card = SIM(atr)
self.usim = False
# Find the right class byte, depending on the simcard type
def __get_cla(self, usim):
return self.card.CLA
# Get file size from FCP
def __get_len_from_tlv(self, fcp):
# Note: This has been taken from http://git.osmocom.org/pysim/tree/pySim/commands.py,
# but pySim uses ascii-hex strings for its internal data representation. We use
# regular lists with integers, so we must convert to an ascii-hex string first:
fcp = ''.join('{:02x}'.format(x) for x in fcp)
# see also: ETSI TS 102 221, chapter 11.1.1.3.1 Response for MF,
# DF or ADF
from pytlv.TLV import TLV
tlvparser = TLV(['82', '83', '84', 'a5', '8a', '8b', '8c', '80', 'ab', 'c6', '81', '88'])
# pytlv is case sensitive!
fcp = fcp.lower()
if fcp[0:2] != '62':
raise ValueError('Tag of the FCP template does not match, expected 62 but got %s'%fcp[0:2])
# Unfortunately the spec is not very clear if the FCP length is
# coded as one or two byte vale, so we have to try it out by
# checking if the length of the remaining TLV string matches
# what we get in the length field.
# See also ETSI TS 102 221, chapter 11.1.1.3.0 Base coding.
exp_tlv_len = int(fcp[2:4], 16)
if len(fcp[4:])/2 == exp_tlv_len:
skip = 4
else:
exp_tlv_len = int(fcp[2:6], 16)
if len(fcp[4:])/2 == exp_tlv_len:
skip = 6
# Skip FCP tag and length
tlv = fcp[skip:]
tlv_parsed = tlvparser.parse(tlv)
if '80' in tlv_parsed:
return int(tlv_parsed['80'], 16)
else:
return 0
# Get the file length from a response (select)
def __len(self, res, p2):
if p2 == 0x04:
return self.__get_len_from_tlv(res)
else:
return int(res[-1][4:8], 16)
# Select a file and retrieve its length
def select(self, fid):
self.filelen = 0
p2 = 0x04
res = Card_res_apdu()
res.from_mich(self.card.SELECT_FILE(P2 = p2, Data = fid))
# Stop here, on failure
if res.sw[0] != 0x61:
return res
res.from_mich(self.card.GET_RESPONSE(res.sw[1]))
self.filelen = self.__len(res.apdu, p2)
return res
# Perform card holder verification
def verify_chv(self, chv, chv_no):
res = Card_res_apdu()
res.from_mich(self.card.VERIFY(P2 = chv_no, Data = chv))
return res
# Read CHV retry counter
def chv_retrys(self, chv_no):
res = self.card.VERIFY(P2 = chv_no)
return res[2][1] & 0x0F
# Perform file operation (Write)
def update_binary(self, data, offset = 0):
offs_high = (offset >> 8) & 0xFF
offs_low = offset & 0xFF
res = Card_res_apdu()
res.from_mich(self.card.UPDATE_BINARY(offs_high, offs_low, data))
return res
# Perform file operation (Read, byte oriented)
def read_binary(self, length, offset = 0):
offs_high = (offset >> 8) & 0xFF
offs_low = offset & 0xFF
res = Card_res_apdu()
res.from_mich(self.card.READ_BINARY(offs_high, offs_low, length))
return res
# Perform file operation (Read, record oriented)
def read_record(self, length, rec_no = 0):
res = Card_res_apdu()
res.from_mich(self.card.READ_RECORD(rec_no, GSM_SIM_INS_READ_RECORD_ABS, length))
return res
# Perform file operation (Read, record oriented)
def update_record(self, data, rec_no = 0):
res = Card_res_apdu()
res.from_mich(self.card.UPDATE_RECORD(rec_no, GSM_SIM_INS_UPDATE_RECORD_ABS, data))
return res