blob: ac298a53920b72b1478809a353ad112cd7c50a15 [file] [log] [blame]
Oliver Smith05b13322020-02-24 14:18:20 +01001/* Copyright 2020 sysmocom s.f.m.c. GmbH
2 * SPDX-License-Identifier: Apache-2.0 */
Oliver Smith4e5e5162020-02-21 08:47:36 +01003package org.osmocom.IMSIPseudo;
4
Oliver Smith4eee13d2020-02-24 11:28:39 +01005import sim.access.*;
6import sim.toolkit.*;
7import javacard.framework.*;
Oliver Smith4e5e5162020-02-21 08:47:36 +01008
9public class IMSIPseudo extends Applet implements ToolkitInterface, ToolkitConstants {
10 // DON'T DECLARE USELESS INSTANCE VARIABLES! They get saved to the EEPROM,
11 // which has a limited number of write cycles.
Oliver Smith4e5e5162020-02-21 08:47:36 +010012
Oliver Smithca866fe2020-02-24 09:56:30 +010013 private byte STKServicesMenuId;
Oliver Smith2259cb92020-02-24 11:36:31 +010014 private SIMView gsmFile;
Oliver Smith1e5cc462020-02-21 15:39:14 +010015 static byte[] LUCounter = new byte[] { '0', 'x', ' ', 'L', 'U' };
Oliver Smithca866fe2020-02-24 09:56:30 +010016
17 /* Main menu */
Neels Hofmeyr7d7e33f2020-02-24 21:24:02 +010018 private static final byte[] title = new byte[] { 'I', 'M', 'S', 'I', ' ', 'P', 's', 'e', 'u', 'd', 'o', 'n', 'y', 'm',
Oliver Smith2dcbfab2020-02-21 15:40:21 +010019 'i', 'z', 'a', 't', 'i', 'o', 'n'};
Neels Hofmeyr7d7e33f2020-02-24 21:24:02 +010020 private static final byte[] showLU = new byte[] {'S', 'h', 'o', 'w', ' ', 'L', 'U', ' ', 'c', 'o', 'u', 'n', 't', 'e', 'r'};
21 private static final byte[] showIMSI = new byte[] {'S', 'h', 'o', 'w', ' ', 'I', 'M', 'S', 'I'};
22 private static final byte[] changeIMSI = new byte[] {'C', 'h', 'a', 'n', 'g', 'e', ' ', 'I', 'M', 'S', 'I', ' '};
23 private final Object[] itemListMain = {title, showLU, showIMSI, changeIMSI};
Oliver Smithca866fe2020-02-24 09:56:30 +010024
25 /* Change IMSI menu */
Neels Hofmeyr7d7e33f2020-02-24 21:24:02 +010026 private static final byte[] setDigit1 = new byte[] {'S', 'e', 't', ' ', '1', ' ', 'a', 's', ' ', 'l', 'a', 's', 't', ' ',
Oliver Smithca866fe2020-02-24 09:56:30 +010027 'd', 'i', 'g', 'i', 't'};
Neels Hofmeyr7d7e33f2020-02-24 21:24:02 +010028 private static final byte[] setDigit2 = new byte[] {'S', 'e', 't', ' ', '2', ' ', 'a', 's', ' ', 'l', 'a', 's', 't', ' ',
Oliver Smithca866fe2020-02-24 09:56:30 +010029 'd', 'i', 'g', 'i', 't'};
Neels Hofmeyr7d7e33f2020-02-24 21:24:02 +010030 private final Object[] itemListChangeIMSI = {changeIMSI, setDigit1, setDigit2};
Oliver Smith4e5e5162020-02-21 08:47:36 +010031
Neels Hofmeyr98462702020-02-24 21:26:05 +010032 private static final byte MI_IMSI = 1;
33
Oliver Smith4e5e5162020-02-21 08:47:36 +010034 private IMSIPseudo() {
Oliver Smith2259cb92020-02-24 11:36:31 +010035 gsmFile = SIMSystem.getTheSIMView();
36
Oliver Smithca866fe2020-02-24 09:56:30 +010037 /* Register menu and trigger on location updates */
Oliver Smith4e5e5162020-02-21 08:47:36 +010038 ToolkitRegistry reg = ToolkitRegistry.getEntry();
Oliver Smithca866fe2020-02-24 09:56:30 +010039 STKServicesMenuId = reg.initMenuEntry(title, (short)0, (short)title.length, PRO_CMD_SELECT_ITEM, false,
40 (byte)0, (short)0);
Oliver Smithe28705a2020-02-21 10:06:14 +010041 reg.setEvent(EVENT_EVENT_DOWNLOAD_LOCATION_STATUS);
Oliver Smith4e5e5162020-02-21 08:47:36 +010042 }
43
Oliver Smith4e5e5162020-02-21 08:47:36 +010044 public static void install(byte[] bArray, short bOffset, byte bLength) {
45 IMSIPseudo applet = new IMSIPseudo();
46 applet.register();
47 }
48
Oliver Smith4e5e5162020-02-21 08:47:36 +010049 public void process(APDU arg0) throws ISOException {
Oliver Smith4e5e5162020-02-21 08:47:36 +010050 if (selectingApplet())
51 return;
52 }
53
Oliver Smith4e5e5162020-02-21 08:47:36 +010054 public void processToolkit(byte event) throws ToolkitException {
55 EnvelopeHandler envHdlr = EnvelopeHandler.getTheHandler();
56
57 if (event == EVENT_MENU_SELECTION) {
58 byte selectedItemId = envHdlr.getItemIdentifier();
59
Oliver Smithca866fe2020-02-24 09:56:30 +010060 if (selectedItemId == STKServicesMenuId) {
61 showMenu(itemListMain, (byte)4);
62 handleMenuResponseMain();
Oliver Smith4e5e5162020-02-21 08:47:36 +010063 }
64 }
Oliver Smithe28705a2020-02-21 10:06:14 +010065
66 if (event == EVENT_EVENT_DOWNLOAD_LOCATION_STATUS) {
Oliver Smith1e5cc462020-02-21 15:39:14 +010067 LUCounter[0]++;
Oliver Smith234ab542020-02-24 08:25:43 +010068 showMsg(LUCounter);
Oliver Smithe28705a2020-02-21 10:06:14 +010069 }
Oliver Smith4e5e5162020-02-21 08:47:36 +010070 }
71
Oliver Smithca866fe2020-02-24 09:56:30 +010072 private void showMenu(Object[] itemList, byte itemCount) {
73 ProactiveHandler proHdlr = ProactiveHandler.getTheHandler();
74 proHdlr.init((byte) PRO_CMD_SELECT_ITEM,(byte)0,DEV_ID_ME);
75
76 for (byte i=(byte)0;i<itemCount;i++) {
77 if (i == 0) {
78 /* Title */
79 proHdlr.appendTLV((byte)(TAG_ALPHA_IDENTIFIER | TAG_SET_CR), (byte[])itemList[i],
80 (short)0, (short)((byte[])itemList[i]).length);
81
82 } else {
83 /* Menu entry */
84 proHdlr.appendTLV((byte)(TAG_ITEM | TAG_SET_CR), (byte)i, (byte[])itemList[i], (short)0,
85 (short)((byte[])itemList[i]).length);
86 }
87 }
88 proHdlr.send();
89 }
90
Oliver Smithcef081c2020-02-24 10:02:14 +010091 private void showMsg(byte[] msg) {
92 ProactiveHandler proHdlr = ProactiveHandler.getTheHandler();
93 proHdlr.initDisplayText((byte)0, DCS_8_BIT_DATA, msg, (short)0, (short)(msg.length));
94 proHdlr.send();
Oliver Smithcef081c2020-02-24 10:02:14 +010095 }
96
Neels Hofmeyrba7a6f22020-02-24 21:26:37 +010097 private byte[] getResponse()
98 {
99 ProactiveResponseHandler rspHdlr = ProactiveResponseHandler.getTheHandler();
100 byte[] resp = new byte[rspHdlr.getTextStringLength()];
101 rspHdlr.copyTextString(resp, (short)0);
102 return resp;
103 }
104
105 private byte[] showMsgAndWaitKey(byte[] msg) {
Neels Hofmeyrcfb476d2020-02-24 19:00:03 +0100106 ProactiveHandler proHdlr = ProactiveHandler.getTheHandler();
107 proHdlr.initGetInkey((byte)0, DCS_8_BIT_DATA, msg, (short)0, (short)(msg.length));
108 proHdlr.send();
Neels Hofmeyrba7a6f22020-02-24 21:26:37 +0100109
110 return getResponse();
111 }
112
113 private byte[] prompt(byte[] msg, short minLen, short maxLen) {
114 /* if maxLen < 1, the applet crashes */
115 if (maxLen < 1)
116 maxLen = 1;
117
118 ProactiveHandler proHdlr = ProactiveHandler.getTheHandler();
119 proHdlr.initGetInput((byte)0, DCS_8_BIT_DATA, msg, (short)0, (short)(msg.length), minLen, maxLen);
120 proHdlr.send();
121
122 return getResponse();
Neels Hofmeyrcfb476d2020-02-24 19:00:03 +0100123 }
124
Oliver Smithd7f18922020-02-24 12:24:38 +0100125 private void showError(short code) {
126 byte[] msg = new byte[] {'E', '?', '?'};
127 msg[1] = (byte)('0' + code / 10);
128 msg[2] = (byte)('0' + code % 10);
129 showMsg(msg);
130 }
131
Neels Hofmeyrb7a20e32020-02-24 18:58:56 +0100132 /* Convert BCD-encoded digit into printable character
133 * \param[in] bcd A single BCD-encoded digit
134 * \returns single printable character
135 */
136 private byte bcd2char(byte bcd)
137 {
138 if (bcd < 0xa)
139 return (byte)('0' + bcd);
140 else
141 return (byte)('A' + (bcd - 0xa));
142 }
143
Neels Hofmeyr98462702020-02-24 21:26:05 +0100144 private byte char2bcd(byte c)
145 {
146 if (c >= '0' && c <= '9')
147 return (byte)(c - '0');
148 else if (c >= 'A' && c <= 'F')
149 return (byte)(0xa + (c - 'A'));
150 else if (c >= 'a' && c <= 'f')
151 return (byte)(0xa + (c - 'a'));
152 else
153 return 0;
154 }
155
Neels Hofmeyrb7a20e32020-02-24 18:58:56 +0100156 /* Convert BCD to string.
157 * The given nibble offsets are interpreted in BCD order, i.e. nibble 0 is bcd[0] & 0xf, nibble 1 is bcd[0] >> 4, nibble
158 * 3 is bcd[1] & 0xf, etc..
159 * \param[out] dst Output byte array.
160 * \param[in] dst_ofs Where to start writing in dst.
161 * \param[in] dst_len How many bytes are available at dst_ofs.
162 * \param[in] bcd Binary coded data buffer.
163 * \param[in] start_nibble Offset to start from, in nibbles.
164 * \param[in] end_nibble Offset to stop before, in nibbles.
165 * \param[in] allow_hex If false, return false if there are digits other than 0-9.
166 * \returns true on success, false otherwise
167 */
168 private boolean bcd2str(byte dst[], byte dst_ofs, byte dst_len,
169 byte bcd[], byte start_nibble, byte end_nibble, boolean allow_hex)
170 {
171 byte nibble_i;
172 byte dst_i = dst_ofs;
173 byte dst_end = (byte)(dst_ofs + dst_len);
174 boolean rc = true;
175
176 for (nibble_i = start_nibble; nibble_i < end_nibble && dst_i < dst_end; nibble_i++, dst_i++) {
177 byte nibble = bcd[(byte)nibble_i >> 1];
178 if ((nibble_i & 1) != 0)
179 nibble >>= 4;
180 nibble &= 0xf;
181
182 if (!allow_hex && nibble > 9)
183 rc = false;
184
185 dst[dst_i] = bcd2char(nibble);
186 }
187
188 return rc;
189 }
190
Neels Hofmeyr98462702020-02-24 21:26:05 +0100191 private byte mi2str(byte dst[], byte dst_ofs, byte dst_len,
192 byte mi[], boolean allow_hex)
Neels Hofmeyrb7a20e32020-02-24 18:58:56 +0100193 {
194 /* The IMSI byte array by example:
195 * 08 99 10 07 00 00 10 74 90
196 *
197 * This is encoded according to 3GPP TS 24.008 10.5.1.4 Mobile
198 * Identity, short the Mobile Identity IEI:
199 *
200 * 08 length for the following MI, in bytes.
201 * 9 = 0b1001
202 * 1 = odd nr of digits
203 * 001 = MI type = IMSI
204 * 9 first IMSI digit (BCD)
205 * 0 second digit
206 * 1 third
207 * ...
208 * 0 14th digit
209 * 9 15th and last digit
210 *
211 * If the IMSI had an even number of digits:
212 *
213 * 08 98 10 07 00 00 10 74 f0
214 *
215 * 08 length for the following MI, in bytes.
216 * 8 = 0b0001
217 * 0 = even nr of digits
218 * 001 = MI type = IMSI
219 * 9 first IMSI digit
220 * 0 second digit
221 * 1 third
222 * ...
223 * 0 14th and last digit
224 * f filler
225 */
226 byte bytelen = mi[0];
227 byte mi_type = (byte)(mi[1] & 0xf);
228 boolean odd_nr_of_digits = ((mi_type & 0x08) != 0);
229 byte start_nibble = 2 + 1; // 2 to skip the bytelen, 1 to skip the mi_type
230 byte end_nibble = (byte)(2 + bytelen * 2 - (odd_nr_of_digits ? 0 : 1));
Neels Hofmeyr98462702020-02-24 21:26:05 +0100231 bcd2str(dst, dst_ofs, dst_len, mi, start_nibble, end_nibble, allow_hex);
232 return (byte)(end_nibble - start_nibble);
233 }
234
235 private byte[] str2mi(byte str[], byte mi_type)
236 {
237 /* 1 byte of MI length.
238 * 1 nibble of mi_type.
239 * str.length nibbles of MI BCD.
240 * The first MI digit is in the high-nibble of the mi_type, so an odd amount of digits becomes
241 * (1 + str.length)/2 bytes; an even amount of digits has same amount of bytes with the last
242 * nibble unused (0xf0). */
243 byte len = (byte)(1 + (byte)(1 + str.length)/2);
244 byte mi[] = new byte[1 + len];
245 mi[0] = len;
246
247 boolean odd_digits = ((str.length & 1) != 0);
248 mi_type = (byte)(mi_type & 0x07);
249 if (odd_digits)
250 mi_type |= 0x08;
251 mi[1] = (byte)((char2bcd(str[0]) << 4) + mi_type);
252 byte str_i = 1;
253 for (byte bcd_i = 1; bcd_i < len; bcd_i++) {
254 byte data = char2bcd(str[str_i]);
255 str_i++;
256 if (str_i < str.length) {
257 data |= char2bcd(str[str_i]) << 4;
258 str_i++;
259 } else
260 data |= 0xf0;
261 mi[1 + bcd_i] = data;
262 }
263 return mi;
Neels Hofmeyrb7a20e32020-02-24 18:58:56 +0100264 }
265
Neels Hofmeyrc0c95622020-02-24 21:29:23 +0100266 private byte nibble2hex(byte nibble)
267 {
268 nibble = (byte)(nibble & 0xf);
269 if (nibble < 0xa)
270 return (byte)('0' + nibble);
271 else
272 return (byte)('a' + nibble - 0xa);
273 }
274
275 private byte[] hexdump(byte data[])
276 {
277 byte res[] = new byte[(byte)(data.length*2)];
278 for (byte i = 0; i < data.length; i++) {
279 res[(byte)(i*2)] = nibble2hex((byte)(data[i] >> 4));
280 res[(byte)(i*2 + 1)] = nibble2hex(data[i]);
281 }
282 return res;
283 }
284
Oliver Smith2259cb92020-02-24 11:36:31 +0100285 private void showIMSI() {
286 /* 3GPP TS 31.102 4.2.2: IMSI */
287 byte[] IMSI = new byte[9];
288 byte[] msg = {'C', 'u', 'r', 'r', 'e', 'n', 't', ' ', 'I', 'M', 'S', 'I', ':', ' ',
Neels Hofmeyrb7a20e32020-02-24 18:58:56 +0100289 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
Oliver Smith2259cb92020-02-24 11:36:31 +0100290
291 gsmFile.select((short) SIMView.FID_DF_GSM);
292 gsmFile.select((short) SIMView.FID_EF_IMSI);
Oliver Smithd7f18922020-02-24 12:24:38 +0100293
294 try {
295 gsmFile.readBinary((short)0, IMSI, (short)0, (short)9);
296 } catch (SIMViewException e) {
297 showError(e.getReason());
Oliver Smith89d18bc2020-02-24 15:24:43 +0100298 return;
Oliver Smithd7f18922020-02-24 12:24:38 +0100299 }
Oliver Smith2259cb92020-02-24 11:36:31 +0100300
Neels Hofmeyrb7a20e32020-02-24 18:58:56 +0100301 mi2str(msg, (byte)14, (byte)16, IMSI, false);
Neels Hofmeyrcfb476d2020-02-24 19:00:03 +0100302
303 showMsgAndWaitKey(msg);
Oliver Smith2259cb92020-02-24 11:36:31 +0100304 }
305
Oliver Smithca866fe2020-02-24 09:56:30 +0100306 private void handleMenuResponseMain() {
307 ProactiveResponseHandler rspHdlr = ProactiveResponseHandler.getTheHandler();
308
309 switch (rspHdlr.getItemIdentifier()) {
Neels Hofmeyrc8e96412020-02-24 21:29:46 +0100310 case 1: /* Show LU counter */
311 showMsg(LUCounter);
312 break;
313 case 2: /* Show IMSI */
314 showIMSI();
315 break;
316 case 3: /* Change IMSI */
317 showMenu(itemListChangeIMSI, (byte)3);
318 handleMenuResponseChangeIMSI();
319 break;
Oliver Smithca866fe2020-02-24 09:56:30 +0100320 }
321 }
322
323 private void handleMenuResponseChangeIMSI() {
324 /* TODO */
325 }
Oliver Smith4e5e5162020-02-21 08:47:36 +0100326}