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 */ |
Neels Hofmeyr | 7d7e33f | 2020-02-24 21:24:02 +0100 | [diff] [blame] | 18 | private static final byte[] title = new byte[] { '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] | 19 | 'i', 'z', 'a', 't', 'i', 'o', 'n'}; |
Neels Hofmeyr | 7d7e33f | 2020-02-24 21:24:02 +0100 | [diff] [blame] | 20 | private static final byte[] showLU = new byte[] {'S', 'h', 'o', 'w', ' ', 'L', 'U', ' ', 'c', 'o', 'u', 'n', 't', 'e', 'r'}; |
| 21 | private static final byte[] showIMSI = new byte[] {'S', 'h', 'o', 'w', ' ', 'I', 'M', 'S', 'I'}; |
| 22 | private static final byte[] changeIMSI = new byte[] {'C', 'h', 'a', 'n', 'g', 'e', ' ', 'I', 'M', 'S', 'I', ' '}; |
| 23 | private final Object[] itemListMain = {title, showLU, showIMSI, changeIMSI}; |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 24 | |
| 25 | /* Change IMSI menu */ |
Neels Hofmeyr | 7d7e33f | 2020-02-24 21:24:02 +0100 | [diff] [blame] | 26 | private static final byte[] setDigit1 = new byte[] {'S', 'e', 't', ' ', '1', ' ', 'a', 's', ' ', 'l', 'a', 's', 't', ' ', |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 27 | 'd', 'i', 'g', 'i', 't'}; |
Neels Hofmeyr | 7d7e33f | 2020-02-24 21:24:02 +0100 | [diff] [blame] | 28 | private static final byte[] setDigit2 = new byte[] {'S', 'e', 't', ' ', '2', ' ', 'a', 's', ' ', 'l', 'a', 's', 't', ' ', |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 29 | 'd', 'i', 'g', 'i', 't'}; |
Neels Hofmeyr | 7d7e33f | 2020-02-24 21:24:02 +0100 | [diff] [blame] | 30 | private final Object[] itemListChangeIMSI = {changeIMSI, setDigit1, setDigit2}; |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 31 | |
Neels Hofmeyr | 9846270 | 2020-02-24 21:26:05 +0100 | [diff] [blame] | 32 | private static final byte MI_IMSI = 1; |
| 33 | |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 34 | private IMSIPseudo() { |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 35 | gsmFile = SIMSystem.getTheSIMView(); |
| 36 | |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 37 | /* Register menu and trigger on location updates */ |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 38 | ToolkitRegistry reg = ToolkitRegistry.getEntry(); |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 39 | STKServicesMenuId = reg.initMenuEntry(title, (short)0, (short)title.length, PRO_CMD_SELECT_ITEM, false, |
| 40 | (byte)0, (short)0); |
Oliver Smith | e28705a | 2020-02-21 10:06:14 +0100 | [diff] [blame] | 41 | reg.setEvent(EVENT_EVENT_DOWNLOAD_LOCATION_STATUS); |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 42 | } |
| 43 | |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 44 | public static void install(byte[] bArray, short bOffset, byte bLength) { |
| 45 | IMSIPseudo applet = new IMSIPseudo(); |
| 46 | applet.register(); |
| 47 | } |
| 48 | |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 49 | public void process(APDU arg0) throws ISOException { |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 50 | if (selectingApplet()) |
| 51 | return; |
| 52 | } |
| 53 | |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 54 | public void processToolkit(byte event) throws ToolkitException { |
| 55 | EnvelopeHandler envHdlr = EnvelopeHandler.getTheHandler(); |
| 56 | |
| 57 | if (event == EVENT_MENU_SELECTION) { |
| 58 | byte selectedItemId = envHdlr.getItemIdentifier(); |
| 59 | |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 60 | if (selectedItemId == STKServicesMenuId) { |
| 61 | showMenu(itemListMain, (byte)4); |
| 62 | handleMenuResponseMain(); |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 63 | } |
| 64 | } |
Oliver Smith | e28705a | 2020-02-21 10:06:14 +0100 | [diff] [blame] | 65 | |
| 66 | if (event == EVENT_EVENT_DOWNLOAD_LOCATION_STATUS) { |
Oliver Smith | 1e5cc46 | 2020-02-21 15:39:14 +0100 | [diff] [blame] | 67 | LUCounter[0]++; |
Oliver Smith | 234ab54 | 2020-02-24 08:25:43 +0100 | [diff] [blame] | 68 | showMsg(LUCounter); |
Oliver Smith | e28705a | 2020-02-21 10:06:14 +0100 | [diff] [blame] | 69 | } |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 72 | private void showMenu(Object[] itemList, byte itemCount) { |
| 73 | ProactiveHandler proHdlr = ProactiveHandler.getTheHandler(); |
| 74 | proHdlr.init((byte) PRO_CMD_SELECT_ITEM,(byte)0,DEV_ID_ME); |
| 75 | |
| 76 | for (byte i=(byte)0;i<itemCount;i++) { |
| 77 | if (i == 0) { |
| 78 | /* Title */ |
| 79 | proHdlr.appendTLV((byte)(TAG_ALPHA_IDENTIFIER | TAG_SET_CR), (byte[])itemList[i], |
| 80 | (short)0, (short)((byte[])itemList[i]).length); |
| 81 | |
| 82 | } else { |
| 83 | /* Menu entry */ |
| 84 | proHdlr.appendTLV((byte)(TAG_ITEM | TAG_SET_CR), (byte)i, (byte[])itemList[i], (short)0, |
| 85 | (short)((byte[])itemList[i]).length); |
| 86 | } |
| 87 | } |
| 88 | proHdlr.send(); |
| 89 | } |
| 90 | |
Oliver Smith | cef081c | 2020-02-24 10:02:14 +0100 | [diff] [blame] | 91 | private void showMsg(byte[] msg) { |
| 92 | ProactiveHandler proHdlr = ProactiveHandler.getTheHandler(); |
| 93 | proHdlr.initDisplayText((byte)0, DCS_8_BIT_DATA, msg, (short)0, (short)(msg.length)); |
| 94 | proHdlr.send(); |
Oliver Smith | cef081c | 2020-02-24 10:02:14 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Neels Hofmeyr | ba7a6f2 | 2020-02-24 21:26:37 +0100 | [diff] [blame] | 97 | private byte[] getResponse() |
| 98 | { |
| 99 | ProactiveResponseHandler rspHdlr = ProactiveResponseHandler.getTheHandler(); |
| 100 | byte[] resp = new byte[rspHdlr.getTextStringLength()]; |
| 101 | rspHdlr.copyTextString(resp, (short)0); |
| 102 | return resp; |
| 103 | } |
| 104 | |
| 105 | private byte[] showMsgAndWaitKey(byte[] msg) { |
Neels Hofmeyr | cfb476d | 2020-02-24 19:00:03 +0100 | [diff] [blame] | 106 | ProactiveHandler proHdlr = ProactiveHandler.getTheHandler(); |
| 107 | proHdlr.initGetInkey((byte)0, DCS_8_BIT_DATA, msg, (short)0, (short)(msg.length)); |
| 108 | proHdlr.send(); |
Neels Hofmeyr | ba7a6f2 | 2020-02-24 21:26:37 +0100 | [diff] [blame] | 109 | |
| 110 | return getResponse(); |
| 111 | } |
| 112 | |
| 113 | private byte[] prompt(byte[] msg, short minLen, short maxLen) { |
| 114 | /* if maxLen < 1, the applet crashes */ |
| 115 | if (maxLen < 1) |
| 116 | maxLen = 1; |
| 117 | |
| 118 | ProactiveHandler proHdlr = ProactiveHandler.getTheHandler(); |
| 119 | proHdlr.initGetInput((byte)0, DCS_8_BIT_DATA, msg, (short)0, (short)(msg.length), minLen, maxLen); |
| 120 | proHdlr.send(); |
| 121 | |
| 122 | return getResponse(); |
Neels Hofmeyr | cfb476d | 2020-02-24 19:00:03 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Oliver Smith | d7f1892 | 2020-02-24 12:24:38 +0100 | [diff] [blame] | 125 | private void showError(short code) { |
| 126 | byte[] msg = new byte[] {'E', '?', '?'}; |
| 127 | msg[1] = (byte)('0' + code / 10); |
| 128 | msg[2] = (byte)('0' + code % 10); |
| 129 | showMsg(msg); |
| 130 | } |
| 131 | |
Neels Hofmeyr | b7a20e3 | 2020-02-24 18:58:56 +0100 | [diff] [blame] | 132 | /* Convert BCD-encoded digit into printable character |
| 133 | * \param[in] bcd A single BCD-encoded digit |
| 134 | * \returns single printable character |
| 135 | */ |
| 136 | private byte bcd2char(byte bcd) |
| 137 | { |
| 138 | if (bcd < 0xa) |
| 139 | return (byte)('0' + bcd); |
| 140 | else |
| 141 | return (byte)('A' + (bcd - 0xa)); |
| 142 | } |
| 143 | |
Neels Hofmeyr | 9846270 | 2020-02-24 21:26:05 +0100 | [diff] [blame] | 144 | private byte char2bcd(byte c) |
| 145 | { |
| 146 | if (c >= '0' && c <= '9') |
| 147 | return (byte)(c - '0'); |
| 148 | else if (c >= 'A' && c <= 'F') |
| 149 | return (byte)(0xa + (c - 'A')); |
| 150 | else if (c >= 'a' && c <= 'f') |
| 151 | return (byte)(0xa + (c - 'a')); |
| 152 | else |
| 153 | return 0; |
| 154 | } |
| 155 | |
Neels Hofmeyr | b7a20e3 | 2020-02-24 18:58:56 +0100 | [diff] [blame] | 156 | /* Convert BCD to string. |
| 157 | * The given nibble offsets are interpreted in BCD order, i.e. nibble 0 is bcd[0] & 0xf, nibble 1 is bcd[0] >> 4, nibble |
| 158 | * 3 is bcd[1] & 0xf, etc.. |
| 159 | * \param[out] dst Output byte array. |
| 160 | * \param[in] dst_ofs Where to start writing in dst. |
| 161 | * \param[in] dst_len How many bytes are available at dst_ofs. |
| 162 | * \param[in] bcd Binary coded data buffer. |
| 163 | * \param[in] start_nibble Offset to start from, in nibbles. |
| 164 | * \param[in] end_nibble Offset to stop before, in nibbles. |
| 165 | * \param[in] allow_hex If false, return false if there are digits other than 0-9. |
| 166 | * \returns true on success, false otherwise |
| 167 | */ |
| 168 | private boolean bcd2str(byte dst[], byte dst_ofs, byte dst_len, |
| 169 | byte bcd[], byte start_nibble, byte end_nibble, boolean allow_hex) |
| 170 | { |
| 171 | byte nibble_i; |
| 172 | byte dst_i = dst_ofs; |
| 173 | byte dst_end = (byte)(dst_ofs + dst_len); |
| 174 | boolean rc = true; |
| 175 | |
| 176 | for (nibble_i = start_nibble; nibble_i < end_nibble && dst_i < dst_end; nibble_i++, dst_i++) { |
| 177 | byte nibble = bcd[(byte)nibble_i >> 1]; |
| 178 | if ((nibble_i & 1) != 0) |
| 179 | nibble >>= 4; |
| 180 | nibble &= 0xf; |
| 181 | |
| 182 | if (!allow_hex && nibble > 9) |
| 183 | rc = false; |
| 184 | |
| 185 | dst[dst_i] = bcd2char(nibble); |
| 186 | } |
| 187 | |
| 188 | return rc; |
| 189 | } |
| 190 | |
Neels Hofmeyr | 9846270 | 2020-02-24 21:26:05 +0100 | [diff] [blame] | 191 | private byte mi2str(byte dst[], byte dst_ofs, byte dst_len, |
| 192 | byte mi[], boolean allow_hex) |
Neels Hofmeyr | b7a20e3 | 2020-02-24 18:58:56 +0100 | [diff] [blame] | 193 | { |
| 194 | /* The IMSI byte array by example: |
| 195 | * 08 99 10 07 00 00 10 74 90 |
| 196 | * |
| 197 | * This is encoded according to 3GPP TS 24.008 10.5.1.4 Mobile |
| 198 | * Identity, short the Mobile Identity IEI: |
| 199 | * |
| 200 | * 08 length for the following MI, in bytes. |
| 201 | * 9 = 0b1001 |
| 202 | * 1 = odd nr of digits |
| 203 | * 001 = MI type = IMSI |
| 204 | * 9 first IMSI digit (BCD) |
| 205 | * 0 second digit |
| 206 | * 1 third |
| 207 | * ... |
| 208 | * 0 14th digit |
| 209 | * 9 15th and last digit |
| 210 | * |
| 211 | * If the IMSI had an even number of digits: |
| 212 | * |
| 213 | * 08 98 10 07 00 00 10 74 f0 |
| 214 | * |
| 215 | * 08 length for the following MI, in bytes. |
| 216 | * 8 = 0b0001 |
| 217 | * 0 = even nr of digits |
| 218 | * 001 = MI type = IMSI |
| 219 | * 9 first IMSI digit |
| 220 | * 0 second digit |
| 221 | * 1 third |
| 222 | * ... |
| 223 | * 0 14th and last digit |
| 224 | * f filler |
| 225 | */ |
| 226 | byte bytelen = mi[0]; |
| 227 | byte mi_type = (byte)(mi[1] & 0xf); |
| 228 | boolean odd_nr_of_digits = ((mi_type & 0x08) != 0); |
| 229 | byte start_nibble = 2 + 1; // 2 to skip the bytelen, 1 to skip the mi_type |
| 230 | byte end_nibble = (byte)(2 + bytelen * 2 - (odd_nr_of_digits ? 0 : 1)); |
Neels Hofmeyr | 9846270 | 2020-02-24 21:26:05 +0100 | [diff] [blame] | 231 | bcd2str(dst, dst_ofs, dst_len, mi, start_nibble, end_nibble, allow_hex); |
| 232 | return (byte)(end_nibble - start_nibble); |
| 233 | } |
| 234 | |
| 235 | private byte[] str2mi(byte str[], byte mi_type) |
| 236 | { |
| 237 | /* 1 byte of MI length. |
| 238 | * 1 nibble of mi_type. |
| 239 | * str.length nibbles of MI BCD. |
| 240 | * The first MI digit is in the high-nibble of the mi_type, so an odd amount of digits becomes |
| 241 | * (1 + str.length)/2 bytes; an even amount of digits has same amount of bytes with the last |
| 242 | * nibble unused (0xf0). */ |
| 243 | byte len = (byte)(1 + (byte)(1 + str.length)/2); |
| 244 | byte mi[] = new byte[1 + len]; |
| 245 | mi[0] = len; |
| 246 | |
| 247 | boolean odd_digits = ((str.length & 1) != 0); |
| 248 | mi_type = (byte)(mi_type & 0x07); |
| 249 | if (odd_digits) |
| 250 | mi_type |= 0x08; |
| 251 | mi[1] = (byte)((char2bcd(str[0]) << 4) + mi_type); |
| 252 | byte str_i = 1; |
| 253 | for (byte bcd_i = 1; bcd_i < len; bcd_i++) { |
| 254 | byte data = char2bcd(str[str_i]); |
| 255 | str_i++; |
| 256 | if (str_i < str.length) { |
| 257 | data |= char2bcd(str[str_i]) << 4; |
| 258 | str_i++; |
| 259 | } else |
| 260 | data |= 0xf0; |
| 261 | mi[1 + bcd_i] = data; |
| 262 | } |
| 263 | return mi; |
Neels Hofmeyr | b7a20e3 | 2020-02-24 18:58:56 +0100 | [diff] [blame] | 264 | } |
| 265 | |
Neels Hofmeyr | c0c9562 | 2020-02-24 21:29:23 +0100 | [diff] [blame^] | 266 | private byte nibble2hex(byte nibble) |
| 267 | { |
| 268 | nibble = (byte)(nibble & 0xf); |
| 269 | if (nibble < 0xa) |
| 270 | return (byte)('0' + nibble); |
| 271 | else |
| 272 | return (byte)('a' + nibble - 0xa); |
| 273 | } |
| 274 | |
| 275 | private byte[] hexdump(byte data[]) |
| 276 | { |
| 277 | byte res[] = new byte[(byte)(data.length*2)]; |
| 278 | for (byte i = 0; i < data.length; i++) { |
| 279 | res[(byte)(i*2)] = nibble2hex((byte)(data[i] >> 4)); |
| 280 | res[(byte)(i*2 + 1)] = nibble2hex(data[i]); |
| 281 | } |
| 282 | return res; |
| 283 | } |
| 284 | |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 285 | private void showIMSI() { |
| 286 | /* 3GPP TS 31.102 4.2.2: IMSI */ |
| 287 | byte[] IMSI = new byte[9]; |
| 288 | 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] | 289 | ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}; |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 290 | |
| 291 | gsmFile.select((short) SIMView.FID_DF_GSM); |
| 292 | gsmFile.select((short) SIMView.FID_EF_IMSI); |
Oliver Smith | d7f1892 | 2020-02-24 12:24:38 +0100 | [diff] [blame] | 293 | |
| 294 | try { |
| 295 | gsmFile.readBinary((short)0, IMSI, (short)0, (short)9); |
| 296 | } catch (SIMViewException e) { |
| 297 | showError(e.getReason()); |
Oliver Smith | 89d18bc | 2020-02-24 15:24:43 +0100 | [diff] [blame] | 298 | return; |
Oliver Smith | d7f1892 | 2020-02-24 12:24:38 +0100 | [diff] [blame] | 299 | } |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 300 | |
Neels Hofmeyr | b7a20e3 | 2020-02-24 18:58:56 +0100 | [diff] [blame] | 301 | mi2str(msg, (byte)14, (byte)16, IMSI, false); |
Neels Hofmeyr | cfb476d | 2020-02-24 19:00:03 +0100 | [diff] [blame] | 302 | |
| 303 | showMsgAndWaitKey(msg); |
Oliver Smith | 2259cb9 | 2020-02-24 11:36:31 +0100 | [diff] [blame] | 304 | } |
| 305 | |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 306 | private void handleMenuResponseMain() { |
| 307 | ProactiveResponseHandler rspHdlr = ProactiveResponseHandler.getTheHandler(); |
| 308 | |
| 309 | switch (rspHdlr.getItemIdentifier()) { |
Neels Hofmeyr | c8e9641 | 2020-02-24 21:29:46 +0100 | [diff] [blame] | 310 | case 1: /* Show LU counter */ |
| 311 | showMsg(LUCounter); |
| 312 | break; |
| 313 | case 2: /* Show IMSI */ |
| 314 | showIMSI(); |
| 315 | break; |
| 316 | case 3: /* Change IMSI */ |
| 317 | showMenu(itemListChangeIMSI, (byte)3); |
| 318 | handleMenuResponseChangeIMSI(); |
| 319 | break; |
Oliver Smith | ca866fe | 2020-02-24 09:56:30 +0100 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | private void handleMenuResponseChangeIMSI() { |
| 324 | /* TODO */ |
| 325 | } |
Oliver Smith | 4e5e516 | 2020-02-21 08:47:36 +0100 | [diff] [blame] | 326 | } |