blob: 4843c77a3a5d9242e7da9855a35817585776afda [file] [log] [blame]
Harald Welte3c9b7842021-10-19 21:44:24 +02001#!/usr/bin/env python3
2
3# Interactive shell for working with SIM / UICC / USIM / ISIM cards
4#
5# (C) 2022 by Harald Welte <laforge@osmocom.org>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20from typing import List
21
22import cmd2
Harald Welte3c9b7842021-10-19 21:44:24 +020023from cmd2 import CommandSet, with_default_category, with_argparser
24import argparse
25
26from pySim.ts_31_102 import EF_UST_map, EF_USIM_ADF_map
27from pySim.ts_31_103 import EF_IST_map, EF_ISIM_ADF_map
28
29from pySim.exceptions import *
30from pySim.utils import h2b, swap_nibbles, b2h, JsonEncoder
31
32from pySim.ts_102_221 import *
33
34@with_default_category('TS 102 222 Administrative Commands')
35class Ts102222Commands(CommandSet):
36 """Administrative commands for telecommunication applications."""
37
38 def __init__(self):
39 super().__init__()
40
41 delfile_parser = argparse.ArgumentParser()
42 delfile_parser.add_argument('--force-delete', action='store_true',
43 help='I really want to permanently delete the file. I know pySim cannot re-create it yet!')
44 delfile_parser.add_argument('NAME', type=str, help='File name or FID to delete')
45
46 @cmd2.with_argparser(delfile_parser)
47 def do_delete_file(self, opts):
48 """Delete the specified file. DANGEROUS! See TS 102 222 Section 6.4.
49 This will permanently delete the specified file from the card.
50 pySim has no support to re-create files yet, and even if it did, your card may not allow it!"""
51 if not opts.force_delete:
52 self._cmd.perror("Refusing to permanently delete the file, please read the help text.")
53 return
Harald Weltea6c0f882022-07-17 14:23:17 +020054 f = self._cmd.lchan.get_file_for_selectable(opts.NAME)
Harald Welte3c9b7842021-10-19 21:44:24 +020055 (data, sw) = self._cmd.card._scc.delete_file(f.fid)
56
57 def complete_delete_file(self, text, line, begidx, endidx) -> List[str]:
58 """Command Line tab completion for DELETE FILE"""
Harald Weltea6c0f882022-07-17 14:23:17 +020059 index_dict = {1: self._cmd.lchan.selected_file.get_selectable_names()}
Harald Welte3c9b7842021-10-19 21:44:24 +020060 return self._cmd.index_based_complete(text, line, begidx, endidx, index_dict=index_dict)
61
62 termdf_parser = argparse.ArgumentParser()
63 termdf_parser.add_argument('--force', action='store_true',
64 help='I really want to terminate the file. I know I can not recover from it!')
65 termdf_parser.add_argument('NAME', type=str, help='File name or FID')
66
67 @cmd2.with_argparser(termdf_parser)
68 def do_terminate_df(self, opts):
69 """Terminate the specified DF. DANGEROUS! See TS 102 222 6.7.
70 This is a permanent, one-way operation on the card. There is no undo, you can not recover
71 a terminated DF. The only permitted command for a terminated DF is the DLETE FILE command."""
72 if not opts.force:
73 self._cmd.perror("Refusing to terminate the file, please read the help text.")
74 return
Harald Weltea6c0f882022-07-17 14:23:17 +020075 f = self._cmd.lchan.get_file_for_selectable(opts.NAME)
Harald Welte3c9b7842021-10-19 21:44:24 +020076 (data, sw) = self._cmd.card._scc.terminate_df(f.fid)
77
78 def complete_terminate_df(self, text, line, begidx, endidx) -> List[str]:
79 """Command Line tab completion for TERMINATE DF"""
Harald Weltea6c0f882022-07-17 14:23:17 +020080 index_dict = {1: self._cmd.lchan.selected_file.get_selectable_names()}
Harald Welte3c9b7842021-10-19 21:44:24 +020081 return self._cmd.index_based_complete(text, line, begidx, endidx, index_dict=index_dict)
82
83 @cmd2.with_argparser(termdf_parser)
84 def do_terminate_ef(self, opts):
85 """Terminate the specified EF. DANGEROUS! See TS 102 222 6.8.
86 This is a permanent, one-way operation on the card. There is no undo, you can not recover
87 a terminated EF. The only permitted command for a terminated EF is the DLETE FILE command."""
88 if not opts.force:
89 self._cmd.perror("Refusing to terminate the file, please read the help text.")
90 return
Harald Weltea6c0f882022-07-17 14:23:17 +020091 f = self._cmd.lchan.get_file_for_selectable(opts.NAME)
Harald Welte3c9b7842021-10-19 21:44:24 +020092 (data, sw) = self._cmd.card._scc.terminate_ef(f.fid)
93
94 def complete_terminate_ef(self, text, line, begidx, endidx) -> List[str]:
95 """Command Line tab completion for TERMINATE EF"""
Harald Weltea6c0f882022-07-17 14:23:17 +020096 index_dict = {1: self._cmd.lchan.selected_file.get_selectable_names()}
Harald Welte3c9b7842021-10-19 21:44:24 +020097 return self._cmd.index_based_complete(text, line, begidx, endidx, index_dict=index_dict)
98
99 tcard_parser = argparse.ArgumentParser()
100 tcard_parser.add_argument('--force-terminate-card', action='store_true',
101 help='I really want to permanently terminate the card. It will not be usable afterwards!')
102
103 @cmd2.with_argparser(tcard_parser)
104 def do_terminate_card_usage(self, opts):
105 """Terminate the Card. SUPER DANGEROUS! See TS 102 222 Section 6.9.
106 This will permanently brick the card and can NOT be recovered from!"""
107 if not opts.force_terminate_card:
108 self._cmd.perror("Refusing to permanently terminate the card, please read the help text.")
109 return
110 (data, sw) = self._cmd.card._scc.terminate_card_usage()
111
112 create_parser = argparse.ArgumentParser()
113 create_parser.add_argument('FILE_ID', type=str, help='File Identifier as 4-character hex string')
114 create_parser._action_groups.pop()
115 create_required = create_parser.add_argument_group('required arguments')
116 create_optional = create_parser.add_argument_group('optional arguments')
117 create_required.add_argument('--ef-arr-file-id', required=True, type=str, help='Referenced Security: File Identifier of EF.ARR')
118 create_required.add_argument('--ef-arr-record-nr', required=True, type=int, help='Referenced Security: Record Number within EF.ARR')
119 create_required.add_argument('--file-size', required=True, type=int, help='Size of file in octets')
120 create_required.add_argument('--structure', required=True, type=str, choices=['transparent', 'linear_fixed', 'ber_tlv'],
121 help='Structure of the to-be-created EF')
122 create_optional.add_argument('--short-file-id', type=str, help='Short File Identifier as 2-digit hex string')
123 create_optional.add_argument('--shareable', action='store_true', help='Should the file be shareable?')
124 create_optional.add_argument('--record-length', type=int, help='Length of each record in octets')
125
126 @cmd2.with_argparser(create_parser)
127 def do_create_ef(self, opts):
128 """Create a new EF below the currently selected DF. Requires related privileges."""
129 file_descriptor = {
130 'file_descriptor_byte': {
131 'shareable': opts.shareable,
132 'file_type': 'working_ef',
133 'structure': opts.structure,
134 }
135 }
136 if opts.structure == 'linear_fixed':
137 if not opts.record_length:
138 self._cmd.perror("you must specify the --record-length for linear fixed EF")
139 return
140 file_descriptor['record_len'] = opts.record_length
Christian Amsüsse17e2772022-04-19 10:29:09 +0200141 file_descriptor['num_of_rec'] = opts.file_size // opts.record_length
142 if file_descriptor['num_of_rec'] * file_descriptor['record_len'] != opts.file_size:
143 raise ValueError("File size not evenly divisible by record length")
Harald Welte3c9b7842021-10-19 21:44:24 +0200144 elif opts.structure == 'ber_tlv':
145 self._cmd.perror("BER-TLV creation not yet fully supported, sorry")
146 return
147 ies = [FileDescriptor(decoded=file_descriptor), FileIdentifier(decoded=opts.FILE_ID),
148 LifeCycleStatusInteger(decoded='operational_activated'),
149 SecurityAttribReferenced(decoded={'ef_arr_file_id': opts.ef_arr_file_id,
150 'ef_arr_record_nr': opts.ef_arr_record_nr }),
151 FileSize(decoded=opts.file_size),
152 ShortFileIdentifier(decoded=opts.short_file_id),
153 ]
154 fcp = FcpTemplate(children=ies)
155 (data, sw) = self._cmd.card._scc.create_file(b2h(fcp.to_tlv()))
156 # the newly-created file is automatically selected but our runtime state knows nothing of it
Harald Weltea6c0f882022-07-17 14:23:17 +0200157 self._cmd.lchan.select_file(self._cmd.lchan.selected_file)
Harald Welte3c9b7842021-10-19 21:44:24 +0200158
159 createdf_parser = argparse.ArgumentParser()
160 createdf_parser.add_argument('FILE_ID', type=str, help='File Identifier as 4-character hex string')
161 createdf_parser._action_groups.pop()
162 createdf_required = createdf_parser.add_argument_group('required arguments')
163 createdf_optional = createdf_parser.add_argument_group('optional arguments')
164 createdf_sja_optional = createdf_parser.add_argument_group('sysmoISIM-SJA optional arguments')
165 createdf_required.add_argument('--ef-arr-file-id', required=True, type=str, help='Referenced Security: File Identifier of EF.ARR')
166 createdf_required.add_argument('--ef-arr-record-nr', required=True, type=int, help='Referenced Security: Record Number within EF.ARR')
167 createdf_optional.add_argument('--shareable', action='store_true', help='Should the file be shareable?')
168 createdf_optional.add_argument('--aid', type=str, help='Application ID (creates an ADF, instead of a DF)')
169 # mandatory by spec, but ignored by several OS, so don't force the user
170 createdf_optional.add_argument('--total-file-size', type=int, help='Physical memory allocated for DF/ADi in octets')
171 createdf_sja_optional.add_argument('--permit-rfm-create', action='store_true')
172 createdf_sja_optional.add_argument('--permit-rfm-delete-terminate', action='store_true')
173 createdf_sja_optional.add_argument('--permit-other-applet-create', action='store_true')
174 createdf_sja_optional.add_argument('--permit-other-applet-delete-terminate', action='store_true')
175
176 @cmd2.with_argparser(createdf_parser)
177 def do_create_df(self, opts):
178 """Create a new DF below the currently selected DF. Requires related privileges."""
179 file_descriptor = {
180 'file_descriptor_byte': {
181 'shareable': opts.shareable,
182 'file_type': 'df',
183 'structure': 'no_info_given',
184 }
185 }
186 ies = []
187 ies.append(FileDescriptor(decoded=file_descriptor))
188 ies.append(FileIdentifier(decoded=opts.FILE_ID))
189 if opts.aid:
190 ies.append(DfName(decoded=opts.aid))
191 ies.append(LifeCycleStatusInteger(decoded='operational_activated'))
192 ies.append(SecurityAttribReferenced(decoded={'ef_arr_file_id': opts.ef_arr_file_id,
193 'ef_arr_record_nr': opts.ef_arr_record_nr }))
194 if opts.total_file_size:
195 ies.append(TotalFileSize(decoded=opts.total_file_size))
196 # TODO: Spec states PIN Status Template DO is mandatory
197 if opts.permit_rfm_create or opts.permit_rfm_delete_terminate or opts.permit_other_applet_create or opts.permit_other_applet_delete_terminate:
198 toolkit_ac = {
199 'rfm_create': opts.permit_rfm_create,
200 'rfm_delete_terminate': opts.permit_rfm_delete_terminate,
201 'other_applet_create': opts.permit_other_applet_create,
202 'other_applet_delete_terminate': opts.permit_other_applet_delete_terminate,
203 }
204 ies.append(ProprietaryInformation(children=[ToolkitAccessConditions(decoded=toolkit_ac)]))
205 fcp = FcpTemplate(children=ies)
206 (data, sw) = self._cmd.card._scc.create_file(b2h(fcp.to_tlv()))
207 # the newly-created file is automatically selected but our runtime state knows nothing of it
Harald Weltea6c0f882022-07-17 14:23:17 +0200208 self._cmd.lchan.select_file(self._cmd.lchan.selected_file)