blob: 72424494db8371953335d8c52e24129c001223f1 [file] [log] [blame]
Oliver Smith4e5e5162020-02-21 08:47:36 +01001package org.osmocom.IMSIPseudo;
2
3import javacard.framework.APDU;
4import javacard.framework.Applet;
5import javacard.framework.ISOException;
6
7import sim.toolkit.EnvelopeHandler;
8import sim.toolkit.ProactiveHandler;
9import sim.toolkit.ToolkitConstants;
10import sim.toolkit.ToolkitException;
11import sim.toolkit.ToolkitInterface;
12import sim.toolkit.ToolkitRegistry;
13
14public class IMSIPseudo extends Applet implements ToolkitInterface, ToolkitConstants {
15 // DON'T DECLARE USELESS INSTANCE VARIABLES! They get saved to the EEPROM,
16 // which has a limited number of write cycles.
17 private byte helloMenuItem;
18
Oliver Smithe28705a2020-02-21 10:06:14 +010019 static byte[] welcomeMsg = new byte[] { 'H', 'e', 'l', 'l', 'o', ',', ' ',
20 'W', 'o', 'r', 'l', 'd', '!' };
Oliver Smith8f586422020-02-21 08:55:57 +010021 static byte[] menuItemText = new byte[] { 'I', 'M', 'S', 'I', ' ', 'P', 's', 'e', 'u', 'd', 'o', 'n', 'y', 'm',
22 'i', 'z', 'a', 't', 'i', 'o', 'n'};
Oliver Smith4e5e5162020-02-21 08:47:36 +010023
24 private IMSIPseudo() {
25 // This is the interface to the STK applet registry (which is separate
26 // from the JavaCard applet registry!)
27 ToolkitRegistry reg = ToolkitRegistry.getEntry();
28
29 // Define the applet Menu Entry
30 helloMenuItem = reg.initMenuEntry(menuItemText, (short)0, (short)menuItemText.length,
31 PRO_CMD_SELECT_ITEM, false, (byte)0, (short)0);
Oliver Smithe28705a2020-02-21 10:06:14 +010032 reg.setEvent(EVENT_EVENT_DOWNLOAD_LOCATION_STATUS);
Oliver Smith4e5e5162020-02-21 08:47:36 +010033 }
34
35 // This method is called by the card when the applet is installed. You must
36 // instantiate your applet and register it here.
37 public static void install(byte[] bArray, short bOffset, byte bLength) {
38 IMSIPseudo applet = new IMSIPseudo();
Oliver Smithe28705a2020-02-21 10:06:14 +010039
Oliver Smith4e5e5162020-02-21 08:47:36 +010040 applet.register();
41 }
42
43 // This processes APDUs sent directly to the applet. For STK applets, this
44 // interface isn't really used.
45 public void process(APDU arg0) throws ISOException {
46 // ignore the applet select command dispached to the process
47 if (selectingApplet())
48 return;
49 }
50
51 // This processes STK events.
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 == helloMenuItem) {
59 showHello();
60 }
61 }
Oliver Smithe28705a2020-02-21 10:06:14 +010062
63 if (event == EVENT_EVENT_DOWNLOAD_LOCATION_STATUS) {
64 /* TODO: count the location updates done with the same
65 * pseudo IMSI, and warn the user if it becomes too
66 * high */
67 showHello();
68 }
Oliver Smith4e5e5162020-02-21 08:47:36 +010069 }
70
71 private void showHello() {
72 ProactiveHandler proHdlr = ProactiveHandler.getTheHandler();
73 proHdlr.initDisplayText((byte)0, DCS_8_BIT_DATA, welcomeMsg, (short)0,
74 (short)(welcomeMsg.length));
75 proHdlr.send();
76 return;
77 }
78}