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
|
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'};
|
20
|
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};
|
31
|
|
32
|
private IMSIPseudo() {
|
33
|
gsmFile = SIMSystem.getTheSIMView();
|
34
|
|
35
|
/* Register menu and trigger on location updates */
|
36
|
ToolkitRegistry reg = ToolkitRegistry.getEntry();
|
37
|
STKServicesMenuId = reg.initMenuEntry(title, (short)0, (short)title.length, PRO_CMD_SELECT_ITEM, false,
|
38
|
(byte)0, (short)0);
|
39
|
reg.setEvent(EVENT_EVENT_DOWNLOAD_LOCATION_STATUS);
|
40
|
}
|
41
|
|
42
|
public static void install(byte[] bArray, short bOffset, byte bLength) {
|
43
|
IMSIPseudo applet = new IMSIPseudo();
|
44
|
applet.register();
|
45
|
}
|
46
|
|
47
|
public void process(APDU arg0) throws ISOException {
|
48
|
if (selectingApplet())
|
49
|
return;
|
50
|
}
|
51
|
|
52
|
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
|
|
58
|
if (selectedItemId == STKServicesMenuId) {
|
59
|
showMenu(itemListMain, (byte)4);
|
60
|
handleMenuResponseMain();
|
61
|
}
|
62
|
}
|
63
|
|
64
|
if (event == EVENT_EVENT_DOWNLOAD_LOCATION_STATUS) {
|
65
|
LUCounter[0]++;
|
66
|
showMsg(LUCounter);
|
67
|
}
|
68
|
}
|
69
|
|
70
|
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
|
|
89
|
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();
|
93
|
return;
|
94
|
}
|
95
|
|
96
|
private void showError(short code) {
|
97
|
byte[] msg = new byte[] {'E', '?', '?'};
|
98
|
msg[1] = (byte)('0' + code / 10);
|
99
|
msg[2] = (byte)('0' + code % 10);
|
100
|
showMsg(msg);
|
101
|
}
|
102
|
|
103
|
private void showIMSI() {
|
104
|
/* 3GPP TS 31.102 4.2.2: IMSI */
|
105
|
byte[] IMSI = new byte[9];
|
106
|
byte[] msg = {'C', 'u', 'r', 'r', 'e', 'n', 't', ' ', 'I', 'M', 'S', 'I', ':', ' ',
|
107
|
'_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_'};
|
108
|
|
109
|
gsmFile.select((short) SIMView.FID_DF_GSM);
|
110
|
gsmFile.select((short) SIMView.FID_EF_IMSI);
|
111
|
|
112
|
try {
|
113
|
gsmFile.readBinary((short)0, IMSI, (short)0, (short)9);
|
114
|
} catch (SIMViewException e) {
|
115
|
showError(e.getReason());
|
116
|
return;
|
117
|
}
|
118
|
|
119
|
for (byte i = (byte)0; i < (byte)15; i++) {
|
120
|
byte msg_i = (byte)(14 + i);
|
121
|
if (i >= IMSI[0]) {
|
122
|
msg[msg_i] = ' ';
|
123
|
} else if (i % (byte)2 == (byte)0) {
|
124
|
msg[msg_i] = (byte)('0' + (IMSI[i / (byte)2] & 0x0f));
|
125
|
} else {
|
126
|
msg[msg_i] = (byte)('0' + (IMSI[i / (byte)2] >>> 4));
|
127
|
}
|
128
|
showMsg(msg); /* DEBUG */
|
129
|
}
|
130
|
showMsg(msg);
|
131
|
}
|
132
|
|
133
|
private void handleMenuResponseMain() {
|
134
|
ProactiveResponseHandler rspHdlr = ProactiveResponseHandler.getTheHandler();
|
135
|
|
136
|
switch (rspHdlr.getItemIdentifier()) {
|
137
|
case 1: /* Show LU counter */
|
138
|
showMsg(LUCounter);
|
139
|
break;
|
140
|
case 2: /* Show IMSI */
|
141
|
showIMSI();
|
142
|
break;
|
143
|
case 3: /* Change IMSI */
|
144
|
showMenu(itemListChangeIMSI, (byte)3);
|
145
|
handleMenuResponseChangeIMSI();
|
146
|
break;
|
147
|
}
|
148
|
}
|
149
|
|
150
|
private void handleMenuResponseChangeIMSI() {
|
151
|
/* TODO */
|
152
|
}
|
153
|
}
|