Oliver Smith | 05b1332 | 2020-02-24 14:18:20 +0100 | [diff] [blame] | 1 | /* Copyright 2020 sysmocom s.f.m.c. GmbH |
| 2 | * SPDX-License-Identifier: Apache-2.0 */ |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 3 | package org.osmocom.IMSIPseudo; |
| 4 | |
Oliver Smith | 4eee13d | 2020-02-24 11:28:39 +0100 | [diff] [blame] | 5 | import sim.access.*; |
| 6 | import sim.toolkit.*; |
| 7 | import javacard.framework.*; |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 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. |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 12 | |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 13 | private byte STKServicesMenuId; |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 14 | private SIMView gsmFile; |
Oliver Smith | 1e5cc46 | 2020-02-21 15:39:14 +0100 | [diff] [blame] | 15 | static byte[] LUCounter = new byte[] { '0', 'x', ' ', 'L', 'U' }; |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 16 | |
| 17 | /* Main menu */ |
Oliver Smith | 2dcbfab | 2020-02-21 15:40:21 +0100 | [diff] [blame] | 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'}; |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 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}; |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 31 | |
| 32 | private IMSIPseudo() { |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 33 | gsmFile = SIMSystem.getTheSIMView(); |
| 34 | |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 35 | /* Register menu and trigger on location updates */ |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 36 | ToolkitRegistry reg = ToolkitRegistry.getEntry(); |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 37 | STKServicesMenuId = reg.initMenuEntry(title, (short)0, (short)title.length, PRO_CMD_SELECT_ITEM, false, |
| 38 | (byte)0, (short)0); |
Oliver Smith | e28705a | 2020-02-21 10:06:14 +0100 | [diff] [blame] | 39 | reg.setEvent(EVENT_EVENT_DOWNLOAD_LOCATION_STATUS); |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 42 | public static void install(byte[] bArray, short bOffset, byte bLength) { |
| 43 | IMSIPseudo applet = new IMSIPseudo(); |
| 44 | applet.register(); |
| 45 | } |
| 46 | |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 47 | public void process(APDU arg0) throws ISOException { |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 48 | if (selectingApplet()) |
| 49 | return; |
| 50 | } |
| 51 | |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 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 | |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 58 | if (selectedItemId == STKServicesMenuId) { |
| 59 | showMenu(itemListMain, (byte)4); |
| 60 | handleMenuResponseMain(); |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 61 | } |
| 62 | } |
Oliver Smith | e28705a | 2020-02-21 10:06:14 +0100 | [diff] [blame] | 63 | |
| 64 | if (event == EVENT_EVENT_DOWNLOAD_LOCATION_STATUS) { |
Oliver Smith | 1e5cc46 | 2020-02-21 15:39:14 +0100 | [diff] [blame] | 65 | LUCounter[0]++; |
Oliver Smith | 234ab54 | 2020-02-24 08:25:43 +0100 | [diff] [blame] | 66 | showMsg(LUCounter); |
Oliver Smith | e28705a | 2020-02-21 10:06:14 +0100 | [diff] [blame] | 67 | } |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 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 | |
Oliver Smith | cef081c | 2020-02-24 10:02:14 +0100 | [diff] [blame] | 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(); |
Oliver Smith | cef081c | 2020-02-24 10:02:14 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Oliver Smith | d7f1892 | 2020-02-24 12:24:38 +0100 | [diff] [blame] | 95 | 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 | |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 102 | 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', ':', ' ', |
Oliver Smith | 89879a0 | 2020-02-24 15:44:56 +0100 | [diff] [blame^] | 106 | '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_'}; |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 107 | |
| 108 | gsmFile.select((short) SIMView.FID_DF_GSM); |
| 109 | gsmFile.select((short) SIMView.FID_EF_IMSI); |
Oliver Smith | d7f1892 | 2020-02-24 12:24:38 +0100 | [diff] [blame] | 110 | |
| 111 | try { |
| 112 | gsmFile.readBinary((short)0, IMSI, (short)0, (short)9); |
| 113 | } catch (SIMViewException e) { |
| 114 | showError(e.getReason()); |
Oliver Smith | 89d18bc | 2020-02-24 15:24:43 +0100 | [diff] [blame] | 115 | return; |
Oliver Smith | d7f1892 | 2020-02-24 12:24:38 +0100 | [diff] [blame] | 116 | } |
Oliver Smith | 89879a0 | 2020-02-24 15:44:56 +0100 | [diff] [blame^] | 117 | byte len = (byte) (IMSI[0] * 2); |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 118 | |
Oliver Smith | 89879a0 | 2020-02-24 15:44:56 +0100 | [diff] [blame^] | 119 | for (byte i = (byte)0; i < (byte)18; i++) { |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 120 | byte msg_i = (byte)(14 + i); |
Oliver Smith | 89879a0 | 2020-02-24 15:44:56 +0100 | [diff] [blame^] | 121 | if (i >= len) { |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 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 | } |
Oliver Smith | d7f1892 | 2020-02-24 12:24:38 +0100 | [diff] [blame] | 128 | showMsg(msg); /* DEBUG */ |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 129 | } |
| 130 | showMsg(msg); |
| 131 | } |
| 132 | |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 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 */ |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 141 | showIMSI(); |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 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 | } |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 153 | } |