blob: 777dd2482bb1576790b49bf61c95c327007f789b [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
26
Sylvain Munaut76504e02010-12-07 00:24:32 +010027
28class SimCardCommands(object):
29 def __init__(self, transport):
30 self._tp = transport;
Jan Balke14b350f2015-01-26 11:15:25 +010031 self._cla_byte = "a0"
Philipp Maier41460862017-03-21 12:05:30 +010032 self.sel_ctrl = "0000"
Jan Balke14b350f2015-01-26 11:15:25 +010033
34 @property
35 def cla_byte(self):
36 return self._cla_byte
37 @cla_byte.setter
38 def cla_byte(self, value):
39 self._cla_byte = value
40
Philipp Maier41460862017-03-21 12:05:30 +010041 @property
42 def sel_ctrl(self):
43 return self._sel_ctrl
44 @sel_ctrl.setter
45 def sel_ctrl(self, value):
46 self._sel_ctrl = value
Sylvain Munaut76504e02010-12-07 00:24:32 +010047
48 def select_file(self, dir_list):
49 rv = []
50 for i in dir_list:
Philipp Maier41460862017-03-21 12:05:30 +010051 data, sw = self._tp.send_apdu_checksw(self.cla_byte + "a4" + self.sel_ctrl + "02" + i)
Sylvain Munaut76504e02010-12-07 00:24:32 +010052 rv.append(data)
53 return rv
54
55 def read_binary(self, ef, length=None, offset=0):
56 if not hasattr(type(ef), '__iter__'):
57 ef = [ef]
58 r = self.select_file(ef)
59 if length is None:
60 length = int(r[-1][4:8], 16) - offset
Jan Balke14b350f2015-01-26 11:15:25 +010061 pdu = self.cla_byte + 'b0%04x%02x' % (offset, (min(256, length) & 0xff))
Sylvain Munaut76504e02010-12-07 00:24:32 +010062 return self._tp.send_apdu(pdu)
63
64 def update_binary(self, ef, data, offset=0):
65 if not hasattr(type(ef), '__iter__'):
66 ef = [ef]
67 self.select_file(ef)
Jan Balke14b350f2015-01-26 11:15:25 +010068 pdu = self.cla_byte + 'd6%04x%02x' % (offset, len(data)/2) + data
Harald Welte982a3072011-03-22 21:47:20 +010069 return self._tp.send_apdu_checksw(pdu)
Sylvain Munaut76504e02010-12-07 00:24:32 +010070
71 def read_record(self, ef, rec_no):
72 if not hasattr(type(ef), '__iter__'):
73 ef = [ef]
74 r = self.select_file(ef)
75 rec_length = int(r[-1][28:30], 16)
Jan Balke14b350f2015-01-26 11:15:25 +010076 pdu = self.cla_byte + 'b2%02x04%02x' % (rec_no, rec_length)
Sylvain Munaut76504e02010-12-07 00:24:32 +010077 return self._tp.send_apdu(pdu)
78
79 def update_record(self, ef, rec_no, data, force_len=False):
80 if not hasattr(type(ef), '__iter__'):
81 ef = [ef]
82 r = self.select_file(ef)
83 if not force_len:
84 rec_length = int(r[-1][28:30], 16)
85 if (len(data)/2 != rec_length):
86 raise ValueError('Invalid data length (expected %d, got %d)' % (rec_length, len(data)/2))
87 else:
88 rec_length = len(data)/2
Jan Balke14b350f2015-01-26 11:15:25 +010089 pdu = (self.cla_byte + 'dc%02x04%02x' % (rec_no, rec_length)) + data
Harald Welte982a3072011-03-22 21:47:20 +010090 return self._tp.send_apdu_checksw(pdu)
Sylvain Munaut76504e02010-12-07 00:24:32 +010091
92 def record_size(self, ef):
93 r = self.select_file(ef)
94 return int(r[-1][28:30], 16)
95
96 def record_count(self, ef):
97 r = self.select_file(ef)
98 return int(r[-1][4:8], 16) // int(r[-1][28:30], 16)
99
100 def run_gsm(self, rand):
101 if len(rand) != 32:
102 raise ValueError('Invalid rand')
103 self.select_file(['3f00', '7f20'])
Jan Balke14b350f2015-01-26 11:15:25 +0100104 return self._tp.send_apdu(self.cla_byte + '88000010' + rand)
Sylvain Munaut76504e02010-12-07 00:24:32 +0100105
106 def reset_card(self):
107 return self._tp.reset_card()
108
109 def verify_chv(self, chv_no, code):
110 fc = rpad(b2h(code), 16)
Neels Hofmeyr3ce84d92017-03-21 13:07:46 +0100111 return self._tp.send_apdu_checksw(self.cla_byte + '2000' + ('%02X' % chv_no) + '08' + fc)