Project

General

Profile

Download (9.59 KB) Statistics
| Branch: | Revision:
1
/* Copyright 2020 sysmocom s.f.m.c. GmbH
2
 * SPDX-License-Identifier: Apache-2.0 */
3
package org.osmocom.IMSIPseudo;
4

    
5
import sim.access.*;
6
import sim.toolkit.*;
7
import javacard.framework.*;
8

    
9
public 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.
12

    
13
	private byte STKServicesMenuId;
14
	private SIMView gsmFile;
15
	static byte[] LUCounter = new byte[] { '0', 'x', ' ', 'L', 'U' };
16

    
17
	/* Main menu */
18
	private static final 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'};
20
	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};
24

    
25
	/* Change IMSI menu */
26
	private static final byte[] setDigit1 = new byte[] {'S', 'e', 't', ' ', '1', ' ', 'a', 's', ' ', 'l', 'a', 's', 't', ' ',
27
						  'd', 'i', 'g', 'i', 't'};
28
	private static final byte[] setDigit2 = new byte[] {'S', 'e', 't', ' ', '2', ' ', 'a', 's', ' ', 'l', 'a', 's', 't', ' ',
29
						  'd', 'i', 'g', 'i', 't'};
30
	private final Object[] itemListChangeIMSI = {changeIMSI, setDigit1, setDigit2};
31

    
32
	private static final byte MI_IMSI = 1;
33

    
34
	private IMSIPseudo() {
35
		gsmFile = SIMSystem.getTheSIMView();
36

    
37
		/* Register menu and trigger on location updates */
38
		ToolkitRegistry reg = ToolkitRegistry.getEntry();
39
		STKServicesMenuId = reg.initMenuEntry(title, (short)0, (short)title.length, PRO_CMD_SELECT_ITEM, false,
40
						 (byte)0, (short)0);
41
		reg.setEvent(EVENT_EVENT_DOWNLOAD_LOCATION_STATUS);
42
	}
43

    
44
	public static void install(byte[] bArray, short bOffset, byte bLength) {
45
		IMSIPseudo applet = new IMSIPseudo();
46
		applet.register();
47
	}
48

    
49
	public void process(APDU arg0) throws ISOException {
50
		if (selectingApplet())
51
			return;
52
	}
53

    
54
	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

    
60
			if (selectedItemId == STKServicesMenuId) {
61
				showMenu(itemListMain, (byte)4);
62
				handleMenuResponseMain();
63
			}
64
		}
65

    
66
		if (event == EVENT_EVENT_DOWNLOAD_LOCATION_STATUS) {
67
			LUCounter[0]++;
68
			showMsg(LUCounter);
69
		}
70
	}
71

    
72
	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

    
91
	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();
95
	}
96

    
97
	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) {
106
		ProactiveHandler proHdlr = ProactiveHandler.getTheHandler();
107
		proHdlr.initGetInkey((byte)0, DCS_8_BIT_DATA, msg, (short)0, (short)(msg.length));
108
		proHdlr.send();
109

    
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();
123
	}
124

    
125
	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

    
132
	/* 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

    
144
	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

    
156
	/* 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

    
191
	private byte mi2str(byte dst[], byte dst_ofs, byte dst_len,
192
			    byte mi[], boolean allow_hex)
193
	{
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));
231
		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;
264
	}
265

    
266
	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

    
285
	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', ':', ' ',
289
			      ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
290

    
291
		gsmFile.select((short) SIMView.FID_DF_GSM);
292
		gsmFile.select((short) SIMView.FID_EF_IMSI);
293

    
294
		try {
295
			gsmFile.readBinary((short)0, IMSI, (short)0, (short)9);
296
		} catch (SIMViewException e) {
297
			showError(e.getReason());
298
			return;
299
		}
300

    
301
		mi2str(msg, (byte)14, (byte)16, IMSI, false);
302

    
303
		showMsgAndWaitKey(msg);
304
	}
305

    
306
	private void handleMenuResponseMain() {
307
		ProactiveResponseHandler rspHdlr = ProactiveResponseHandler.getTheHandler();
308

    
309
		switch (rspHdlr.getItemIdentifier()) {
310
		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;
320
		}
321
	}
322

    
323
	private void handleMenuResponseChangeIMSI() {
324
		/* TODO */
325
	}
326
}
    (1-1/1)
    Add picture from clipboard (Maximum size: 48.8 MB)