blob: 3107030fc079015e3370b157fffae58480f5d745 [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 */
Oliver Smith2dcbfab2020-02-21 15:40:21 +010018 static byte[] title = new byte[] { 'I', 'M', 'S', 'I', ' ', 'P', 's', 'e', 'u', 'd', 'o', 'n', 'y', 'm',
19 'i', 'z', 'a', 't', 'i', 'o', 'n'};
Oliver Smithca866fe2020-02-24 09:56:30 +010020 static byte[] showLU = new byte[] {'S', 'h', 'o', 'w', ' ', 'L', 'U', ' ', 'c', 'o', 'u', 'n', 't', 'e', 'r'};
21 static byte[] showIMSI = new byte[] {'S', 'h', 'o', 'w', ' ', 'I', 'M', 'S', 'I'};
22 static byte[] changeIMSI = new byte[] {'C', 'h', 'a', 'n', 'g', 'e', ' ', 'I', 'M', 'S', 'I', ' '};
23 private Object[] itemListMain = {title, showLU, showIMSI, changeIMSI};
24
25 /* Change IMSI menu */
26 static byte[] setDigit1 = new byte[] {'S', 'e', 't', ' ', '1', ' ', 'a', 's', ' ', 'l', 'a', 's', 't', ' ',
27 'd', 'i', 'g', 'i', 't'};
28 static byte[] setDigit2 = new byte[] {'S', 'e', 't', ' ', '2', ' ', 'a', 's', ' ', 'l', 'a', 's', 't', ' ',
29 'd', 'i', 'g', 'i', 't'};
30 private Object[] itemListChangeIMSI = {changeIMSI, setDigit1, setDigit2};
Oliver Smith4e5e5162020-02-21 08:47:36 +010031
32 private IMSIPseudo() {
Oliver Smith2259cb92020-02-24 11:36:31 +010033 gsmFile = SIMSystem.getTheSIMView();
34
Oliver Smithca866fe2020-02-24 09:56:30 +010035 /* Register menu and trigger on location updates */
Oliver Smith4e5e5162020-02-21 08:47:36 +010036 ToolkitRegistry reg = ToolkitRegistry.getEntry();
Oliver Smithca866fe2020-02-24 09:56:30 +010037 STKServicesMenuId = reg.initMenuEntry(title, (short)0, (short)title.length, PRO_CMD_SELECT_ITEM, false,
38 (byte)0, (short)0);
Oliver Smithe28705a2020-02-21 10:06:14 +010039 reg.setEvent(EVENT_EVENT_DOWNLOAD_LOCATION_STATUS);
Oliver Smith4e5e5162020-02-21 08:47:36 +010040 }
41
Oliver Smith4e5e5162020-02-21 08:47:36 +010042 public static void install(byte[] bArray, short bOffset, byte bLength) {
43 IMSIPseudo applet = new IMSIPseudo();
44 applet.register();
45 }
46
Oliver Smith4e5e5162020-02-21 08:47:36 +010047 public void process(APDU arg0) throws ISOException {
Oliver Smith4e5e5162020-02-21 08:47:36 +010048 if (selectingApplet())
49 return;
50 }
51
Oliver Smith4e5e5162020-02-21 08:47:36 +010052 public void processToolkit(byte event) throws ToolkitException {
53 EnvelopeHandler envHdlr = EnvelopeHandler.getTheHandler();
54
55 if (event == EVENT_MENU_SELECTION) {
56 byte selectedItemId = envHdlr.getItemIdentifier();
57
Oliver Smithca866fe2020-02-24 09:56:30 +010058 if (selectedItemId == STKServicesMenuId) {
59 showMenu(itemListMain, (byte)4);
60 handleMenuResponseMain();
Oliver Smith4e5e5162020-02-21 08:47:36 +010061 }
62 }
Oliver Smithe28705a2020-02-21 10:06:14 +010063
64 if (event == EVENT_EVENT_DOWNLOAD_LOCATION_STATUS) {
Oliver Smith1e5cc462020-02-21 15:39:14 +010065 LUCounter[0]++;
Oliver Smith234ab542020-02-24 08:25:43 +010066 showMsg(LUCounter);
Oliver Smithe28705a2020-02-21 10:06:14 +010067 }
Oliver Smith4e5e5162020-02-21 08:47:36 +010068 }
69
Oliver Smithca866fe2020-02-24 09:56:30 +010070 private void showMenu(Object[] itemList, byte itemCount) {
71 ProactiveHandler proHdlr = ProactiveHandler.getTheHandler();
72 proHdlr.init((byte) PRO_CMD_SELECT_ITEM,(byte)0,DEV_ID_ME);
73
74 for (byte i=(byte)0;i<itemCount;i++) {
75 if (i == 0) {
76 /* Title */
77 proHdlr.appendTLV((byte)(TAG_ALPHA_IDENTIFIER | TAG_SET_CR), (byte[])itemList[i],
78 (short)0, (short)((byte[])itemList[i]).length);
79
80 } else {
81 /* Menu entry */
82 proHdlr.appendTLV((byte)(TAG_ITEM | TAG_SET_CR), (byte)i, (byte[])itemList[i], (short)0,
83 (short)((byte[])itemList[i]).length);
84 }
85 }
86 proHdlr.send();
87 }
88
Oliver Smithcef081c2020-02-24 10:02:14 +010089 private void showMsg(byte[] msg) {
90 ProactiveHandler proHdlr = ProactiveHandler.getTheHandler();
91 proHdlr.initDisplayText((byte)0, DCS_8_BIT_DATA, msg, (short)0, (short)(msg.length));
92 proHdlr.send();
Oliver Smithcef081c2020-02-24 10:02:14 +010093 }
94
Oliver Smithd7f18922020-02-24 12:24:38 +010095 private void showError(short code) {
96 byte[] msg = new byte[] {'E', '?', '?'};
97 msg[1] = (byte)('0' + code / 10);
98 msg[2] = (byte)('0' + code % 10);
99 showMsg(msg);
100 }
101
Neels Hofmeyrb7a20e32020-02-24 18:58:56 +0100102 /* Convert BCD-encoded digit into printable character
103 * \param[in] bcd A single BCD-encoded digit
104 * \returns single printable character
105 */
106 private byte bcd2char(byte bcd)
107 {
108 if (bcd < 0xa)
109 return (byte)('0' + bcd);
110 else
111 return (byte)('A' + (bcd - 0xa));
112 }
113
114 /* Convert BCD to string.
115 * The given nibble offsets are interpreted in BCD order, i.e. nibble 0 is bcd[0] & 0xf, nibble 1 is bcd[0] >> 4, nibble
116 * 3 is bcd[1] & 0xf, etc..
117 * \param[out] dst Output byte array.
118 * \param[in] dst_ofs Where to start writing in dst.
119 * \param[in] dst_len How many bytes are available at dst_ofs.
120 * \param[in] bcd Binary coded data buffer.
121 * \param[in] start_nibble Offset to start from, in nibbles.
122 * \param[in] end_nibble Offset to stop before, in nibbles.
123 * \param[in] allow_hex If false, return false if there are digits other than 0-9.
124 * \returns true on success, false otherwise
125 */
126 private boolean bcd2str(byte dst[], byte dst_ofs, byte dst_len,
127 byte bcd[], byte start_nibble, byte end_nibble, boolean allow_hex)
128 {
129 byte nibble_i;
130 byte dst_i = dst_ofs;
131 byte dst_end = (byte)(dst_ofs + dst_len);
132 boolean rc = true;
133
134 for (nibble_i = start_nibble; nibble_i < end_nibble && dst_i < dst_end; nibble_i++, dst_i++) {
135 byte nibble = bcd[(byte)nibble_i >> 1];
136 if ((nibble_i & 1) != 0)
137 nibble >>= 4;
138 nibble &= 0xf;
139
140 if (!allow_hex && nibble > 9)
141 rc = false;
142
143 dst[dst_i] = bcd2char(nibble);
144 }
145
146 return rc;
147 }
148
149 private boolean mi2str(byte dst[], byte dst_ofs, byte dst_len,
150 byte mi[], boolean allow_hex)
151 {
152 /* The IMSI byte array by example:
153 * 08 99 10 07 00 00 10 74 90
154 *
155 * This is encoded according to 3GPP TS 24.008 10.5.1.4 Mobile
156 * Identity, short the Mobile Identity IEI:
157 *
158 * 08 length for the following MI, in bytes.
159 * 9 = 0b1001
160 * 1 = odd nr of digits
161 * 001 = MI type = IMSI
162 * 9 first IMSI digit (BCD)
163 * 0 second digit
164 * 1 third
165 * ...
166 * 0 14th digit
167 * 9 15th and last digit
168 *
169 * If the IMSI had an even number of digits:
170 *
171 * 08 98 10 07 00 00 10 74 f0
172 *
173 * 08 length for the following MI, in bytes.
174 * 8 = 0b0001
175 * 0 = even nr of digits
176 * 001 = MI type = IMSI
177 * 9 first IMSI digit
178 * 0 second digit
179 * 1 third
180 * ...
181 * 0 14th and last digit
182 * f filler
183 */
184 byte bytelen = mi[0];
185 byte mi_type = (byte)(mi[1] & 0xf);
186 boolean odd_nr_of_digits = ((mi_type & 0x08) != 0);
187 byte start_nibble = 2 + 1; // 2 to skip the bytelen, 1 to skip the mi_type
188 byte end_nibble = (byte)(2 + bytelen * 2 - (odd_nr_of_digits ? 0 : 1));
189 return bcd2str(dst, dst_ofs, dst_len, mi, start_nibble, end_nibble, allow_hex);
190 }
191
Oliver Smith2259cb92020-02-24 11:36:31 +0100192 private void showIMSI() {
193 /* 3GPP TS 31.102 4.2.2: IMSI */
194 byte[] IMSI = new byte[9];
195 byte[] msg = {'C', 'u', 'r', 'r', 'e', 'n', 't', ' ', 'I', 'M', 'S', 'I', ':', ' ',
Neels Hofmeyrb7a20e32020-02-24 18:58:56 +0100196 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
Oliver Smith2259cb92020-02-24 11:36:31 +0100197
198 gsmFile.select((short) SIMView.FID_DF_GSM);
199 gsmFile.select((short) SIMView.FID_EF_IMSI);
Oliver Smithd7f18922020-02-24 12:24:38 +0100200
201 try {
202 gsmFile.readBinary((short)0, IMSI, (short)0, (short)9);
203 } catch (SIMViewException e) {
204 showError(e.getReason());
Oliver Smith89d18bc2020-02-24 15:24:43 +0100205 return;
Oliver Smithd7f18922020-02-24 12:24:38 +0100206 }
Oliver Smith2259cb92020-02-24 11:36:31 +0100207
Neels Hofmeyrb7a20e32020-02-24 18:58:56 +0100208 mi2str(msg, (byte)14, (byte)16, IMSI, false);
Oliver Smith2259cb92020-02-24 11:36:31 +0100209 showMsg(msg);
210 }
211
Oliver Smithca866fe2020-02-24 09:56:30 +0100212 private void handleMenuResponseMain() {
213 ProactiveResponseHandler rspHdlr = ProactiveResponseHandler.getTheHandler();
214
215 switch (rspHdlr.getItemIdentifier()) {
216 case 1: /* Show LU counter */
217 showMsg(LUCounter);
218 break;
219 case 2: /* Show IMSI */
Oliver Smith2259cb92020-02-24 11:36:31 +0100220 showIMSI();
Oliver Smithca866fe2020-02-24 09:56:30 +0100221 break;
222 case 3: /* Change IMSI */
223 showMenu(itemListChangeIMSI, (byte)3);
224 handleMenuResponseChangeIMSI();
225 break;
226 }
227 }
228
229 private void handleMenuResponseChangeIMSI() {
230 /* TODO */
231 }
Oliver Smith4e5e5162020-02-21 08:47:36 +0100232}