blob: 913f394d91e20a4925c92b0cd7edcfb5caa023a0 [file] [log] [blame]
Sylvain Munaut76504e02010-12-07 00:24:32 +01001#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4""" pySim: SIM Card commands according to ISO 7816-4 and TS 11.11
5"""
6
7#
8# Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com>
9# Copyright (C) 2010 Harald Welte <laforge@gnumonks.org>
10#
11# This program is free software: you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation, either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program. If not, see <http://www.gnu.org/licenses/>.
23#
24
Sylvain Munaut530076e2010-12-23 20:28:58 +010025from pySim.utils import rpad, b2h
Sylvain Munaut76504e02010-12-07 00:24:32 +010026
27class SimCardCommands(object):
28 def __init__(self, transport):
29 self._tp = transport;
Jan Balke14b350f2015-01-26 11:15:25 +010030 self._cla_byte = "a0"
Philipp Maier41460862017-03-21 12:05:30 +010031 self.sel_ctrl = "0000"
Jan Balke14b350f2015-01-26 11:15:25 +010032
Philipp Maier0e3fcaa2018-06-13 12:34:03 +020033 # Get file size from FCP
34 def __get_len_from_tlv(self, fcp):
35 # see also: ETSI TS 102 221, chapter 11.1.1.3.1 Response for MF,
36 # DF or ADF
Philipp Maierfb98dd62018-07-19 12:00:44 +020037 from pytlv.TLV import TLV
Philipp Maier91f26d72019-03-20 12:12:51 +010038 tlvparser = TLV(['82', '83', '84', 'a5', '8a', '8b', '8c', '80', 'ab', 'c6', '81', '88'])
Philipp Maier0e3fcaa2018-06-13 12:34:03 +020039
40 # pytlv is case sensitive!
41 fcp = fcp.lower()
42
43 if fcp[0:2] != '62':
44 raise ValueError('Tag of the FCP template does not match, expected 62 but got %s'%fcp[0:2])
45
46 # Unfortunately the spec is not very clear if the FCP length is
47 # coded as one or two byte vale, so we have to try it out by
48 # checking if the length of the remaining TLV string matches
49 # what we get in the length field.
50 # See also ETSI TS 102 221, chapter 11.1.1.3.0 Base coding.
51 exp_tlv_len = int(fcp[2:4], 16)
52 if len(fcp[4:])/2 == exp_tlv_len:
53 skip = 4
54 else:
55 exp_tlv_len = int(fcp[2:6], 16)
56 if len(fcp[4:])/2 == exp_tlv_len:
57 skip = 6
58
59 # Skip FCP tag and length
60 tlv = fcp[skip:]
61 tlv_parsed = tlvparser.parse(tlv)
62
63 return int(tlv_parsed['80'], 16)
64
65 # Tell the length of a record by the card response
66 # USIMs respond with an FCP template, which is different
67 # from what SIMs responds. See also:
68 # USIM: ETSI TS 102 221, chapter 11.1.1.3 Response Data
69 # SIM: GSM 11.11, chapter 9.2.1 SELECT
70 def __record_len(self, r):
71 if self.sel_ctrl == "0004":
72 return self.__get_len_from_tlv(r[-1])
73 else:
74 return int(r[-1][28:30], 16)
75
76 # Tell the length of a binary file. See also comment
77 # above.
78 def __len(self, r):
79 if self.sel_ctrl == "0004":
80 return self.__get_len_from_tlv(r[-1])
81 else:
82 return int(r[-1][4:8], 16)
83
Alexander Chemerisd2d660a2017-07-18 16:52:25 +030084 def get_atr(self):
85 return self._tp.get_atr()
86
Jan Balke14b350f2015-01-26 11:15:25 +010087 @property
88 def cla_byte(self):
89 return self._cla_byte
90 @cla_byte.setter
91 def cla_byte(self, value):
92 self._cla_byte = value
93
Philipp Maier41460862017-03-21 12:05:30 +010094 @property
95 def sel_ctrl(self):
96 return self._sel_ctrl
97 @sel_ctrl.setter
98 def sel_ctrl(self, value):
99 self._sel_ctrl = value
Sylvain Munaut76504e02010-12-07 00:24:32 +0100100
101 def select_file(self, dir_list):
102 rv = []
103 for i in dir_list:
Philipp Maier41460862017-03-21 12:05:30 +0100104 data, sw = self._tp.send_apdu_checksw(self.cla_byte + "a4" + self.sel_ctrl + "02" + i)
Sylvain Munaut76504e02010-12-07 00:24:32 +0100105 rv.append(data)
106 return rv
107
108 def read_binary(self, ef, length=None, offset=0):
109 if not hasattr(type(ef), '__iter__'):
110 ef = [ef]
111 r = self.select_file(ef)
112 if length is None:
Philipp Maier0e3fcaa2018-06-13 12:34:03 +0200113 length = self.__len(r) - offset
Jan Balke14b350f2015-01-26 11:15:25 +0100114 pdu = self.cla_byte + 'b0%04x%02x' % (offset, (min(256, length) & 0xff))
Sylvain Munaut76504e02010-12-07 00:24:32 +0100115 return self._tp.send_apdu(pdu)
116
117 def update_binary(self, ef, data, offset=0):
118 if not hasattr(type(ef), '__iter__'):
119 ef = [ef]
120 self.select_file(ef)
Jan Balke14b350f2015-01-26 11:15:25 +0100121 pdu = self.cla_byte + 'd6%04x%02x' % (offset, len(data)/2) + data
Harald Welte982a3072011-03-22 21:47:20 +0100122 return self._tp.send_apdu_checksw(pdu)
Sylvain Munaut76504e02010-12-07 00:24:32 +0100123
124 def read_record(self, ef, rec_no):
125 if not hasattr(type(ef), '__iter__'):
126 ef = [ef]
127 r = self.select_file(ef)
Philipp Maier0e3fcaa2018-06-13 12:34:03 +0200128 rec_length = self.__record_len(r)
Jan Balke14b350f2015-01-26 11:15:25 +0100129 pdu = self.cla_byte + 'b2%02x04%02x' % (rec_no, rec_length)
Sylvain Munaut76504e02010-12-07 00:24:32 +0100130 return self._tp.send_apdu(pdu)
131
132 def update_record(self, ef, rec_no, data, force_len=False):
133 if not hasattr(type(ef), '__iter__'):
134 ef = [ef]
135 r = self.select_file(ef)
136 if not force_len:
Philipp Maier0e3fcaa2018-06-13 12:34:03 +0200137 rec_length = self.__record_len(r)
Sylvain Munaut76504e02010-12-07 00:24:32 +0100138 if (len(data)/2 != rec_length):
139 raise ValueError('Invalid data length (expected %d, got %d)' % (rec_length, len(data)/2))
140 else:
141 rec_length = len(data)/2
Jan Balke14b350f2015-01-26 11:15:25 +0100142 pdu = (self.cla_byte + 'dc%02x04%02x' % (rec_no, rec_length)) + data
Harald Welte982a3072011-03-22 21:47:20 +0100143 return self._tp.send_apdu_checksw(pdu)
Sylvain Munaut76504e02010-12-07 00:24:32 +0100144
145 def record_size(self, ef):
146 r = self.select_file(ef)
Philipp Maier0e3fcaa2018-06-13 12:34:03 +0200147 return self.__record_len(r)
Sylvain Munaut76504e02010-12-07 00:24:32 +0100148
149 def record_count(self, ef):
150 r = self.select_file(ef)
Philipp Maier0e3fcaa2018-06-13 12:34:03 +0200151 return self.__len(r) // self.__record_len(r)
Sylvain Munaut76504e02010-12-07 00:24:32 +0100152
153 def run_gsm(self, rand):
154 if len(rand) != 32:
155 raise ValueError('Invalid rand')
156 self.select_file(['3f00', '7f20'])
Jan Balke14b350f2015-01-26 11:15:25 +0100157 return self._tp.send_apdu(self.cla_byte + '88000010' + rand)
Sylvain Munaut76504e02010-12-07 00:24:32 +0100158
159 def reset_card(self):
160 return self._tp.reset_card()
161
162 def verify_chv(self, chv_no, code):
163 fc = rpad(b2h(code), 16)
Neels Hofmeyr3ce84d92017-03-21 13:07:46 +0100164 return self._tp.send_apdu_checksw(self.cla_byte + '2000' + ('%02X' % chv_no) + '08' + fc)