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; |
Neels Hofmeyr | d20f93a | 2020-02-24 22:42:22 +0100 | [diff] [blame] | 4 | import org.osmocom.IMSIPseudo.MobileIdentity; |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 5 | |
Oliver Smith | 4eee13d | 2020-02-24 11:28:39 +0100 | [diff] [blame] | 6 | import sim.access.*; |
| 7 | import sim.toolkit.*; |
| 8 | import javacard.framework.*; |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 9 | |
| 10 | public class IMSIPseudo extends Applet implements ToolkitInterface, ToolkitConstants { |
| 11 | // DON'T DECLARE USELESS INSTANCE VARIABLES! They get saved to the EEPROM, |
| 12 | // which has a limited number of write cycles. |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 13 | |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 14 | private byte STKServicesMenuId; |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 15 | private SIMView gsmFile; |
Neels Hofmeyr | cf04db0 | 2020-02-25 03:23:03 +0100 | [diff] [blame] | 16 | static byte[] LUCounter = { '0', 'x', ' ', 'L', 'U' }; |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 17 | |
| 18 | /* Main menu */ |
Neels Hofmeyr | cf04db0 | 2020-02-25 03:23:03 +0100 | [diff] [blame] | 19 | private static final byte[] title = { 'I', 'M', 'S', 'I', ' ', 'P', 's', 'e', 'u', 'd', 'o', 'n', 'y', 'm', |
Oliver Smith | 2dcbfab | 2020-02-21 15:40:21 +0100 | [diff] [blame] | 20 | 'i', 'z', 'a', 't', 'i', 'o', 'n'}; |
Neels Hofmeyr | cf04db0 | 2020-02-25 03:23:03 +0100 | [diff] [blame] | 21 | private static final byte[] showLU = {'S', 'h', 'o', 'w', ' ', 'L', 'U', ' ', 'c', 'o', 'u', 'n', 't', 'e', 'r'}; |
Neels Hofmeyr | 483f5a4 | 2020-02-25 03:06:12 +0100 | [diff] [blame^] | 22 | private static final byte[] changeIMSI = {'C', 'h', 'a', 'n', 'g', 'e', ' ', 'I', 'M', 'S', 'I'}; |
| 23 | private static final byte[] invalidIMSI = {'I', 'n', 'v', 'a', 'l', 'i', 'd', ' ', 'I', 'M', 'S', 'I'}; |
| 24 | private static final byte[] noChange = {'N', 'o', ' ', 'c', 'h', 'a', 'n', 'g', 'e'}; |
| 25 | private static final byte[] changed = {'I', 'M', 'S', 'I', ' ', 'c', 'h', 'a', 'n', 'g', 'e', 'd', '!'}; |
| 26 | private static final byte error[] = {'E', 'R', 'R', 'O', 'R' }; |
| 27 | private final Object[] itemListMain = {title, showLU, changeIMSI}; |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 28 | |
| 29 | private IMSIPseudo() { |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 30 | gsmFile = SIMSystem.getTheSIMView(); |
| 31 | |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 32 | /* Register menu and trigger on location updates */ |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 33 | ToolkitRegistry reg = ToolkitRegistry.getEntry(); |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 34 | STKServicesMenuId = reg.initMenuEntry(title, (short)0, (short)title.length, PRO_CMD_SELECT_ITEM, false, |
| 35 | (byte)0, (short)0); |
Oliver Smith | e28705a | 2020-02-21 10:06:14 +0100 | [diff] [blame] | 36 | reg.setEvent(EVENT_EVENT_DOWNLOAD_LOCATION_STATUS); |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 37 | } |
| 38 | |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 39 | public static void install(byte[] bArray, short bOffset, byte bLength) { |
| 40 | IMSIPseudo applet = new IMSIPseudo(); |
| 41 | applet.register(); |
| 42 | } |
| 43 | |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 44 | public void process(APDU arg0) throws ISOException { |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 45 | if (selectingApplet()) |
| 46 | return; |
| 47 | } |
| 48 | |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 49 | public void processToolkit(byte event) throws ToolkitException { |
| 50 | EnvelopeHandler envHdlr = EnvelopeHandler.getTheHandler(); |
| 51 | |
| 52 | if (event == EVENT_MENU_SELECTION) { |
| 53 | byte selectedItemId = envHdlr.getItemIdentifier(); |
| 54 | |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 55 | if (selectedItemId == STKServicesMenuId) { |
Neels Hofmeyr | 583bfec | 2020-02-25 03:10:38 +0100 | [diff] [blame] | 56 | showMenu(itemListMain); |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 57 | handleMenuResponseMain(); |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 58 | } |
| 59 | } |
Oliver Smith | e28705a | 2020-02-21 10:06:14 +0100 | [diff] [blame] | 60 | |
| 61 | if (event == EVENT_EVENT_DOWNLOAD_LOCATION_STATUS) { |
Oliver Smith | 1e5cc46 | 2020-02-21 15:39:14 +0100 | [diff] [blame] | 62 | LUCounter[0]++; |
Oliver Smith | 234ab54 | 2020-02-24 08:25:43 +0100 | [diff] [blame] | 63 | showMsg(LUCounter); |
Oliver Smith | e28705a | 2020-02-21 10:06:14 +0100 | [diff] [blame] | 64 | } |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Neels Hofmeyr | 583bfec | 2020-02-25 03:10:38 +0100 | [diff] [blame] | 67 | private void showMenu(Object[] itemList) { |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 68 | ProactiveHandler proHdlr = ProactiveHandler.getTheHandler(); |
| 69 | proHdlr.init((byte) PRO_CMD_SELECT_ITEM,(byte)0,DEV_ID_ME); |
| 70 | |
Neels Hofmeyr | 583bfec | 2020-02-25 03:10:38 +0100 | [diff] [blame] | 71 | for (byte i=(byte)0; i < itemList.length; i++) { |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 72 | if (i == 0) { |
| 73 | /* Title */ |
| 74 | proHdlr.appendTLV((byte)(TAG_ALPHA_IDENTIFIER | TAG_SET_CR), (byte[])itemList[i], |
| 75 | (short)0, (short)((byte[])itemList[i]).length); |
| 76 | |
| 77 | } else { |
| 78 | /* Menu entry */ |
| 79 | proHdlr.appendTLV((byte)(TAG_ITEM | TAG_SET_CR), (byte)i, (byte[])itemList[i], (short)0, |
| 80 | (short)((byte[])itemList[i]).length); |
| 81 | } |
| 82 | } |
| 83 | proHdlr.send(); |
| 84 | } |
| 85 | |
Oliver Smith | cef081c | 2020-02-24 10:02:14 +0100 | [diff] [blame] | 86 | private void showMsg(byte[] msg) { |
| 87 | ProactiveHandler proHdlr = ProactiveHandler.getTheHandler(); |
| 88 | proHdlr.initDisplayText((byte)0, DCS_8_BIT_DATA, msg, (short)0, (short)(msg.length)); |
| 89 | proHdlr.send(); |
Oliver Smith | cef081c | 2020-02-24 10:02:14 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Neels Hofmeyr | ba7a6f2 | 2020-02-24 21:26:37 +0100 | [diff] [blame] | 92 | private byte[] getResponse() |
| 93 | { |
| 94 | ProactiveResponseHandler rspHdlr = ProactiveResponseHandler.getTheHandler(); |
| 95 | byte[] resp = new byte[rspHdlr.getTextStringLength()]; |
| 96 | rspHdlr.copyTextString(resp, (short)0); |
| 97 | return resp; |
| 98 | } |
| 99 | |
Neels Hofmeyr | 9a3428e | 2020-02-25 03:21:12 +0100 | [diff] [blame] | 100 | /* |
| 101 | This was used to find out that the first byte of a text field seems to be 4. |
| 102 | private byte[] getResponseDBG() |
| 103 | { |
| 104 | ProactiveResponseHandler rspHdlr; |
| 105 | byte resp[]; |
| 106 | byte strlen = -1; |
| 107 | rspHdlr = ProactiveResponseHandler.getTheHandler(); |
| 108 | |
| 109 | for (byte occurence = 1; occurence <= 3; occurence++) { |
| 110 | short len; |
| 111 | try { |
| 112 | if (rspHdlr.findTLV(TAG_TEXT_STRING, (byte)occurence) != TLV_NOT_FOUND) { |
| 113 | if ((len = rspHdlr.getValueLength()) > 1) { |
| 114 | len = 3; |
| 115 | resp = new byte[len]; |
| 116 | rspHdlr.copyValue((short)0, resp, (short)0, (short)(len)); |
| 117 | showMsg(resp); |
| 118 | showMsgAndWaitKey(Bytes.hexdump(resp)); |
| 119 | return resp; |
| 120 | } |
| 121 | } |
| 122 | } catch (Exception e) { |
| 123 | showError((short)(30 + occurence)); |
| 124 | return null; |
| 125 | } |
| 126 | } |
| 127 | showError((short)(39)); |
| 128 | return null; |
| 129 | } |
| 130 | */ |
| 131 | |
Neels Hofmeyr | ba7a6f2 | 2020-02-24 21:26:37 +0100 | [diff] [blame] | 132 | private byte[] showMsgAndWaitKey(byte[] msg) { |
Neels Hofmeyr | cfb476d | 2020-02-24 19:00:03 +0100 | [diff] [blame] | 133 | ProactiveHandler proHdlr = ProactiveHandler.getTheHandler(); |
| 134 | proHdlr.initGetInkey((byte)0, DCS_8_BIT_DATA, msg, (short)0, (short)(msg.length)); |
| 135 | proHdlr.send(); |
Neels Hofmeyr | ba7a6f2 | 2020-02-24 21:26:37 +0100 | [diff] [blame] | 136 | |
| 137 | return getResponse(); |
| 138 | } |
| 139 | |
Neels Hofmeyr | 9a3428e | 2020-02-25 03:21:12 +0100 | [diff] [blame] | 140 | private byte[] prompt(byte[] msg, byte[] prefillVal, short minLen, short maxLen) { |
Neels Hofmeyr | ba7a6f2 | 2020-02-24 21:26:37 +0100 | [diff] [blame] | 141 | /* if maxLen < 1, the applet crashes */ |
| 142 | if (maxLen < 1) |
| 143 | maxLen = 1; |
| 144 | |
| 145 | ProactiveHandler proHdlr = ProactiveHandler.getTheHandler(); |
| 146 | proHdlr.initGetInput((byte)0, DCS_8_BIT_DATA, msg, (short)0, (short)(msg.length), minLen, maxLen); |
Neels Hofmeyr | 9a3428e | 2020-02-25 03:21:12 +0100 | [diff] [blame] | 147 | if (prefillVal != null && prefillVal.length > 0) { |
| 148 | /* appendTLV() expects the first byte to be some header before the actual text. |
| 149 | * At first I thought it was the value's length, but turned out to only work for lengths under 8... |
| 150 | * In the end I reversed the value 4 from the first byte read by rspHdlr.copyValue() for |
| 151 | * TAG_TEXT_STRING fields. As long as we write 4 into the first byte, things just work out, |
| 152 | * apparently. |
| 153 | * Fucking well could have said so in the API docs, too; oh the brain damage, oh the hours wasted. |
| 154 | * This is the appendTLV() variant that writes one byte ahead of writing an array: */ |
| 155 | proHdlr.appendTLV((byte)(TAG_DEFAULT_TEXT), (byte)4, prefillVal, (short)0, |
| 156 | (short)(prefillVal.length)); |
| 157 | } |
Neels Hofmeyr | ba7a6f2 | 2020-02-24 21:26:37 +0100 | [diff] [blame] | 158 | proHdlr.send(); |
| 159 | |
| 160 | return getResponse(); |
Neels Hofmeyr | cfb476d | 2020-02-24 19:00:03 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Oliver Smith | d7f1892 | 2020-02-24 12:24:38 +0100 | [diff] [blame] | 163 | private void showError(short code) { |
Neels Hofmeyr | cf04db0 | 2020-02-25 03:23:03 +0100 | [diff] [blame] | 164 | byte[] msg = {'E', '?', '?'}; |
Oliver Smith | d7f1892 | 2020-02-24 12:24:38 +0100 | [diff] [blame] | 165 | msg[1] = (byte)('0' + code / 10); |
| 166 | msg[2] = (byte)('0' + code % 10); |
| 167 | showMsg(msg); |
| 168 | } |
| 169 | |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 170 | private void handleMenuResponseMain() { |
| 171 | ProactiveResponseHandler rspHdlr = ProactiveResponseHandler.getTheHandler(); |
| 172 | |
| 173 | switch (rspHdlr.getItemIdentifier()) { |
Neels Hofmeyr | c8e9641 | 2020-02-24 21:29:46 +0100 | [diff] [blame] | 174 | case 1: /* Show LU counter */ |
| 175 | showMsg(LUCounter); |
| 176 | break; |
Neels Hofmeyr | 483f5a4 | 2020-02-25 03:06:12 +0100 | [diff] [blame^] | 177 | case 2: /* Change IMSI */ |
| 178 | byte prevIMSI_mi[] = readIMSI(); |
| 179 | byte prevIMSI_str[] = MobileIdentity.mi2str(prevIMSI_mi); |
| 180 | promptIMSI(prevIMSI_str); |
Neels Hofmeyr | c8e9641 | 2020-02-24 21:29:46 +0100 | [diff] [blame] | 181 | break; |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Neels Hofmeyr | 483f5a4 | 2020-02-25 03:06:12 +0100 | [diff] [blame^] | 185 | private void promptIMSI(byte prevIMSI_str[]) |
Neels Hofmeyr | 0866f3a | 2020-02-24 21:30:42 +0100 | [diff] [blame] | 186 | { |
Neels Hofmeyr | 483f5a4 | 2020-02-25 03:06:12 +0100 | [diff] [blame^] | 187 | byte newIMSI_str[] = prevIMSI_str; |
| 188 | |
| 189 | try { |
| 190 | newIMSI_str = prompt(changeIMSI, newIMSI_str, (short)0, (short)15); |
| 191 | } catch (Exception e) { |
| 192 | showError((short)40); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | if (newIMSI_str.length < 6 || newIMSI_str.length > 15 |
| 197 | || !Bytes.isDigit(newIMSI_str)) { |
| 198 | showMsg(invalidIMSI); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | if (Bytes.equals(newIMSI_str, prevIMSI_str)) { |
| 203 | showMsg(noChange); |
| 204 | return; |
| 205 | } |
| 206 | |
Neels Hofmeyr | d20f93a | 2020-02-24 22:42:22 +0100 | [diff] [blame] | 207 | byte mi[]; |
| 208 | try { |
Neels Hofmeyr | 483f5a4 | 2020-02-25 03:06:12 +0100 | [diff] [blame^] | 209 | /* The IMSI file should be 9 bytes long, even if the IMSI is shorter */ |
| 210 | mi = MobileIdentity.str2mi(newIMSI_str, MobileIdentity.MI_IMSI, (byte)9); |
Neels Hofmeyr | 41b6f54 | 2020-02-24 23:00:30 +0100 | [diff] [blame] | 211 | writeIMSI(mi); |
Neels Hofmeyr | 483f5a4 | 2020-02-25 03:06:12 +0100 | [diff] [blame^] | 212 | showMsg(changed); |
Neels Hofmeyr | d20f93a | 2020-02-24 22:42:22 +0100 | [diff] [blame] | 213 | } catch (Exception e) { |
Neels Hofmeyr | 483f5a4 | 2020-02-25 03:06:12 +0100 | [diff] [blame^] | 214 | showError((short)42); |
Neels Hofmeyr | d20f93a | 2020-02-24 22:42:22 +0100 | [diff] [blame] | 215 | } |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 216 | } |
Neels Hofmeyr | c24fdd1 | 2020-02-24 21:31:01 +0100 | [diff] [blame] | 217 | |
| 218 | private byte[] readIMSI() |
| 219 | { |
| 220 | gsmFile.select((short) SIMView.FID_DF_GSM); |
| 221 | gsmFile.select((short) SIMView.FID_EF_IMSI); |
| 222 | byte[] IMSI = new byte[9]; |
| 223 | gsmFile.readBinary((short)0, IMSI, (short)0, (short)9); |
| 224 | return IMSI; |
| 225 | } |
| 226 | |
Neels Hofmeyr | 2625694 | 2020-02-25 03:23:53 +0100 | [diff] [blame] | 227 | private void writeIMSI(byte mi[]) throws Exception |
Neels Hofmeyr | c24fdd1 | 2020-02-24 21:31:01 +0100 | [diff] [blame] | 228 | { |
Neels Hofmeyr | 2625694 | 2020-02-25 03:23:53 +0100 | [diff] [blame] | 229 | if (mi.length != 9) |
| 230 | throw new Exception(); |
Neels Hofmeyr | c24fdd1 | 2020-02-24 21:31:01 +0100 | [diff] [blame] | 231 | gsmFile.select((short) SIMView.FID_DF_GSM); |
| 232 | gsmFile.select((short) SIMView.FID_EF_IMSI); |
| 233 | gsmFile.updateBinary((short)0, mi, (short)0, (short)mi.length); |
| 234 | } |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 235 | } |