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 | |
Neels Hofmeyr | cfb476d | 2020-02-24 19:00:03 +0100 | [diff] [blame^] | 95 | private void showMsgAndWaitKey(byte[] msg) { |
| 96 | ProactiveHandler proHdlr = ProactiveHandler.getTheHandler(); |
| 97 | proHdlr.initGetInkey((byte)0, DCS_8_BIT_DATA, msg, (short)0, (short)(msg.length)); |
| 98 | proHdlr.send(); |
| 99 | } |
| 100 | |
Oliver Smith | d7f1892 | 2020-02-24 12:24:38 +0100 | [diff] [blame] | 101 | private void showError(short code) { |
| 102 | byte[] msg = new byte[] {'E', '?', '?'}; |
| 103 | msg[1] = (byte)('0' + code / 10); |
| 104 | msg[2] = (byte)('0' + code % 10); |
| 105 | showMsg(msg); |
| 106 | } |
| 107 | |
Neels Hofmeyr | b7a20e3 | 2020-02-24 18:58:56 +0100 | [diff] [blame] | 108 | /* Convert BCD-encoded digit into printable character |
| 109 | * \param[in] bcd A single BCD-encoded digit |
| 110 | * \returns single printable character |
| 111 | */ |
| 112 | private byte bcd2char(byte bcd) |
| 113 | { |
| 114 | if (bcd < 0xa) |
| 115 | return (byte)('0' + bcd); |
| 116 | else |
| 117 | return (byte)('A' + (bcd - 0xa)); |
| 118 | } |
| 119 | |
| 120 | /* Convert BCD to string. |
| 121 | * The given nibble offsets are interpreted in BCD order, i.e. nibble 0 is bcd[0] & 0xf, nibble 1 is bcd[0] >> 4, nibble |
| 122 | * 3 is bcd[1] & 0xf, etc.. |
| 123 | * \param[out] dst Output byte array. |
| 124 | * \param[in] dst_ofs Where to start writing in dst. |
| 125 | * \param[in] dst_len How many bytes are available at dst_ofs. |
| 126 | * \param[in] bcd Binary coded data buffer. |
| 127 | * \param[in] start_nibble Offset to start from, in nibbles. |
| 128 | * \param[in] end_nibble Offset to stop before, in nibbles. |
| 129 | * \param[in] allow_hex If false, return false if there are digits other than 0-9. |
| 130 | * \returns true on success, false otherwise |
| 131 | */ |
| 132 | private boolean bcd2str(byte dst[], byte dst_ofs, byte dst_len, |
| 133 | byte bcd[], byte start_nibble, byte end_nibble, boolean allow_hex) |
| 134 | { |
| 135 | byte nibble_i; |
| 136 | byte dst_i = dst_ofs; |
| 137 | byte dst_end = (byte)(dst_ofs + dst_len); |
| 138 | boolean rc = true; |
| 139 | |
| 140 | for (nibble_i = start_nibble; nibble_i < end_nibble && dst_i < dst_end; nibble_i++, dst_i++) { |
| 141 | byte nibble = bcd[(byte)nibble_i >> 1]; |
| 142 | if ((nibble_i & 1) != 0) |
| 143 | nibble >>= 4; |
| 144 | nibble &= 0xf; |
| 145 | |
| 146 | if (!allow_hex && nibble > 9) |
| 147 | rc = false; |
| 148 | |
| 149 | dst[dst_i] = bcd2char(nibble); |
| 150 | } |
| 151 | |
| 152 | return rc; |
| 153 | } |
| 154 | |
| 155 | private boolean mi2str(byte dst[], byte dst_ofs, byte dst_len, |
| 156 | byte mi[], boolean allow_hex) |
| 157 | { |
| 158 | /* The IMSI byte array by example: |
| 159 | * 08 99 10 07 00 00 10 74 90 |
| 160 | * |
| 161 | * This is encoded according to 3GPP TS 24.008 10.5.1.4 Mobile |
| 162 | * Identity, short the Mobile Identity IEI: |
| 163 | * |
| 164 | * 08 length for the following MI, in bytes. |
| 165 | * 9 = 0b1001 |
| 166 | * 1 = odd nr of digits |
| 167 | * 001 = MI type = IMSI |
| 168 | * 9 first IMSI digit (BCD) |
| 169 | * 0 second digit |
| 170 | * 1 third |
| 171 | * ... |
| 172 | * 0 14th digit |
| 173 | * 9 15th and last digit |
| 174 | * |
| 175 | * If the IMSI had an even number of digits: |
| 176 | * |
| 177 | * 08 98 10 07 00 00 10 74 f0 |
| 178 | * |
| 179 | * 08 length for the following MI, in bytes. |
| 180 | * 8 = 0b0001 |
| 181 | * 0 = even nr of digits |
| 182 | * 001 = MI type = IMSI |
| 183 | * 9 first IMSI digit |
| 184 | * 0 second digit |
| 185 | * 1 third |
| 186 | * ... |
| 187 | * 0 14th and last digit |
| 188 | * f filler |
| 189 | */ |
| 190 | byte bytelen = mi[0]; |
| 191 | byte mi_type = (byte)(mi[1] & 0xf); |
| 192 | boolean odd_nr_of_digits = ((mi_type & 0x08) != 0); |
| 193 | byte start_nibble = 2 + 1; // 2 to skip the bytelen, 1 to skip the mi_type |
| 194 | byte end_nibble = (byte)(2 + bytelen * 2 - (odd_nr_of_digits ? 0 : 1)); |
| 195 | return bcd2str(dst, dst_ofs, dst_len, mi, start_nibble, end_nibble, allow_hex); |
| 196 | } |
| 197 | |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 198 | private void showIMSI() { |
| 199 | /* 3GPP TS 31.102 4.2.2: IMSI */ |
| 200 | byte[] IMSI = new byte[9]; |
| 201 | byte[] msg = {'C', 'u', 'r', 'r', 'e', 'n', 't', ' ', 'I', 'M', 'S', 'I', ':', ' ', |
Neels Hofmeyr | b7a20e3 | 2020-02-24 18:58:56 +0100 | [diff] [blame] | 202 | ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}; |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 203 | |
| 204 | gsmFile.select((short) SIMView.FID_DF_GSM); |
| 205 | gsmFile.select((short) SIMView.FID_EF_IMSI); |
Oliver Smith | d7f1892 | 2020-02-24 12:24:38 +0100 | [diff] [blame] | 206 | |
| 207 | try { |
| 208 | gsmFile.readBinary((short)0, IMSI, (short)0, (short)9); |
| 209 | } catch (SIMViewException e) { |
| 210 | showError(e.getReason()); |
Oliver Smith | 89d18bc | 2020-02-24 15:24:43 +0100 | [diff] [blame] | 211 | return; |
Oliver Smith | d7f1892 | 2020-02-24 12:24:38 +0100 | [diff] [blame] | 212 | } |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 213 | |
Neels Hofmeyr | b7a20e3 | 2020-02-24 18:58:56 +0100 | [diff] [blame] | 214 | mi2str(msg, (byte)14, (byte)16, IMSI, false); |
Neels Hofmeyr | cfb476d | 2020-02-24 19:00:03 +0100 | [diff] [blame^] | 215 | |
| 216 | showMsgAndWaitKey(msg); |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 219 | private void handleMenuResponseMain() { |
| 220 | ProactiveResponseHandler rspHdlr = ProactiveResponseHandler.getTheHandler(); |
| 221 | |
| 222 | switch (rspHdlr.getItemIdentifier()) { |
| 223 | case 1: /* Show LU counter */ |
| 224 | showMsg(LUCounter); |
| 225 | break; |
| 226 | case 2: /* Show IMSI */ |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 227 | showIMSI(); |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 228 | break; |
| 229 | case 3: /* Change IMSI */ |
| 230 | showMenu(itemListChangeIMSI, (byte)3); |
| 231 | handleMenuResponseChangeIMSI(); |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | private void handleMenuResponseChangeIMSI() { |
| 237 | /* TODO */ |
| 238 | } |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 239 | } |