1 |
05b1332d
|
Oliver Smith
|
/* Copyright 2020 sysmocom s.f.m.c. GmbH
|
2 |
|
|
* SPDX-License-Identifier: Apache-2.0 */
|
3 |
4e5e516a
|
Oliver Smith
|
package org.osmocom.IMSIPseudo;
|
4 |
|
|
|
5 |
4eee13d7
|
Oliver Smith
|
import sim.access.*;
|
6 |
|
|
import sim.toolkit.*;
|
7 |
|
|
import javacard.framework.*;
|
8 |
4e5e516a
|
Oliver Smith
|
|
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 |
ca866fe7
|
Oliver Smith
|
private byte STKServicesMenuId;
|
14 |
2259cb9f
|
Oliver Smith
|
private SIMView gsmFile;
|
15 |
1e5cc46d
|
Oliver Smith
|
static byte[] LUCounter = new byte[] { '0', 'x', ' ', 'L', 'U' };
|
16 |
ca866fe7
|
Oliver Smith
|
|
17 |
|
|
/* Main menu */
|
18 |
2dcbfabb
|
Oliver Smith
|
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 |
ca866fe7
|
Oliver Smith
|
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 |
4e5e516a
|
Oliver Smith
|
|
32 |
|
|
private IMSIPseudo() {
|
33 |
2259cb9f
|
Oliver Smith
|
gsmFile = SIMSystem.getTheSIMView();
|
34 |
|
|
|
35 |
ca866fe7
|
Oliver Smith
|
/* Register menu and trigger on location updates */
|
36 |
4e5e516a
|
Oliver Smith
|
ToolkitRegistry reg = ToolkitRegistry.getEntry();
|
37 |
ca866fe7
|
Oliver Smith
|
STKServicesMenuId = reg.initMenuEntry(title, (short)0, (short)title.length, PRO_CMD_SELECT_ITEM, false,
|
38 |
|
|
(byte)0, (short)0);
|
39 |
e28705af
|
Oliver Smith
|
reg.setEvent(EVENT_EVENT_DOWNLOAD_LOCATION_STATUS);
|
40 |
4e5e516a
|
Oliver Smith
|
}
|
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 |
ca866fe7
|
Oliver Smith
|
if (selectedItemId == STKServicesMenuId) {
|
59 |
|
|
showMenu(itemListMain, (byte)4);
|
60 |
|
|
handleMenuResponseMain();
|
61 |
4e5e516a
|
Oliver Smith
|
}
|
62 |
|
|
}
|
63 |
e28705af
|
Oliver Smith
|
|
64 |
|
|
if (event == EVENT_EVENT_DOWNLOAD_LOCATION_STATUS) {
|
65 |
1e5cc46d
|
Oliver Smith
|
LUCounter[0]++;
|
66 |
234ab54b
|
Oliver Smith
|
showMsg(LUCounter);
|
67 |
e28705af
|
Oliver Smith
|
}
|
68 |
4e5e516a
|
Oliver Smith
|
}
|
69 |
|
|
|
70 |
ca866fe7
|
Oliver Smith
|
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 |
cef081c1
|
Oliver Smith
|
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 |
|
|
}
|
94 |
|
|
|
95 |
d7f18920
|
Oliver Smith
|
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 |
|
|
|
102 |
2259cb9f
|
Oliver Smith
|
private void showIMSI() {
|
103 |
|
|
/* 3GPP TS 31.102 4.2.2: IMSI */
|
104 |
|
|
byte[] IMSI = new byte[9];
|
105 |
|
|
byte[] msg = {'C', 'u', 'r', 'r', 'e', 'n', 't', ' ', 'I', 'M', 'S', 'I', ':', ' ',
|
106 |
89879a09
|
Oliver Smith
|
'_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_'};
|
107 |
2259cb9f
|
Oliver Smith
|
|
108 |
|
|
gsmFile.select((short) SIMView.FID_DF_GSM);
|
109 |
|
|
gsmFile.select((short) SIMView.FID_EF_IMSI);
|
110 |
d7f18920
|
Oliver Smith
|
|
111 |
|
|
try {
|
112 |
|
|
gsmFile.readBinary((short)0, IMSI, (short)0, (short)9);
|
113 |
|
|
} catch (SIMViewException e) {
|
114 |
|
|
showError(e.getReason());
|
115 |
89d18bc6
|
Oliver Smith
|
return;
|
116 |
d7f18920
|
Oliver Smith
|
}
|
117 |
89879a09
|
Oliver Smith
|
byte len = (byte) (IMSI[0] * 2);
|
118 |
2259cb9f
|
Oliver Smith
|
|
119 |
89879a09
|
Oliver Smith
|
for (byte i = (byte)0; i < (byte)18; i++) {
|
120 |
2259cb9f
|
Oliver Smith
|
byte msg_i = (byte)(14 + i);
|
121 |
89879a09
|
Oliver Smith
|
if (i >= len) {
|
122 |
2259cb9f
|
Oliver Smith
|
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 |
d7f18920
|
Oliver Smith
|
showMsg(msg); /* DEBUG */
|
129 |
2259cb9f
|
Oliver Smith
|
}
|
130 |
|
|
showMsg(msg);
|
131 |
|
|
}
|
132 |
|
|
|
133 |
ca866fe7
|
Oliver Smith
|
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 |
2259cb9f
|
Oliver Smith
|
showIMSI();
|
142 |
ca866fe7
|
Oliver Smith
|
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 |
4e5e516a
|
Oliver Smith
|
}
|