Project

General

Profile

« Previous | Next » 

Revision b7a20e3b

Added by Neels Hofmeyr about 4 years ago

properly read IMSI as Mobile Identity (3GPP TS 24.008)

View differences:

sim-applet/src/org/osmocom/IMSIPseudo/IMSIPseudo.java
99 99
		showMsg(msg);
100 100
	}
101 101

  
102
	/* Convert BCD-encoded digit into printable character
103
	 *  \param[in] bcd A single BCD-encoded digit
104
	 *  \returns single printable character
105
	 */
106
	private byte bcd2char(byte bcd)
107
	{
108
		if (bcd < 0xa)
109
			return (byte)('0' + bcd);
110
		else
111
			return (byte)('A' + (bcd - 0xa));
112
	}
113

  
114
	/* Convert BCD to string.
115
	 * The given nibble offsets are interpreted in BCD order, i.e. nibble 0 is bcd[0] & 0xf, nibble 1 is bcd[0] >> 4, nibble
116
	 * 3 is bcd[1] & 0xf, etc..
117
	 *  \param[out] dst  Output byte array.
118
	 *  \param[in] dst_ofs  Where to start writing in dst.
119
	 *  \param[in] dst_len  How many bytes are available at dst_ofs.
120
	 *  \param[in] bcd  Binary coded data buffer.
121
	 *  \param[in] start_nibble  Offset to start from, in nibbles.
122
	 *  \param[in] end_nibble  Offset to stop before, in nibbles.
123
	 *  \param[in] allow_hex  If false, return false if there are digits other than 0-9.
124
	 *  \returns true on success, false otherwise
125
	 */
126
	private boolean bcd2str(byte dst[], byte dst_ofs, byte dst_len,
127
				byte bcd[], byte start_nibble, byte end_nibble, boolean allow_hex)
128
	{
129
		byte nibble_i;
130
		byte dst_i = dst_ofs;
131
		byte dst_end = (byte)(dst_ofs + dst_len);
132
		boolean rc = true;
133

  
134
		for (nibble_i = start_nibble; nibble_i < end_nibble && dst_i < dst_end; nibble_i++, dst_i++) {
135
			byte nibble = bcd[(byte)nibble_i >> 1];
136
			if ((nibble_i & 1) != 0)
137
				nibble >>= 4;
138
			nibble &= 0xf;
139

  
140
			if (!allow_hex && nibble > 9)
141
				rc = false;
142

  
143
			dst[dst_i] = bcd2char(nibble);
144
		}
145

  
146
		return rc;
147
	}
148

  
149
	private boolean mi2str(byte dst[], byte dst_ofs, byte dst_len,
150
			       byte mi[], boolean allow_hex)
151
	{
152
		/* The IMSI byte array by example:
153
		 * 08 99 10 07 00 00 10 74 90
154
		 *
155
		 * This is encoded according to 3GPP TS 24.008 10.5.1.4 Mobile
156
		 * Identity, short the Mobile Identity IEI:
157
		 *
158
		 * 08 length for the following MI, in bytes.
159
		 *  9 = 0b1001
160
		 *	1 = odd nr of digits
161
		 *	 001 = MI type = IMSI
162
		 * 9  first IMSI digit (BCD)
163
		 *  0 second digit
164
		 * 1  third
165
		 * ...
166
		 *  0 14th digit
167
		 * 9  15th and last digit
168
		 *
169
		 * If the IMSI had an even number of digits:
170
		 *
171
		 * 08 98 10 07 00 00 10 74 f0
172
		 *
173
		 * 08 length for the following MI, in bytes.
174
		 *  8 = 0b0001
175
		 *	0 = even nr of digits
176
		 *	 001 = MI type = IMSI
177
		 * 9  first IMSI digit
178
		 *  0 second digit
179
		 * 1  third
180
		 * ...
181
		 *  0 14th and last digit
182
		 * f  filler
183
		 */
184
		byte bytelen = mi[0];
185
		byte mi_type = (byte)(mi[1] & 0xf);
186
		boolean odd_nr_of_digits = ((mi_type & 0x08) != 0);
187
		byte start_nibble = 2 + 1; // 2 to skip the bytelen, 1 to skip the mi_type
188
		byte end_nibble = (byte)(2 + bytelen * 2 - (odd_nr_of_digits ? 0 : 1));
189
		return bcd2str(dst, dst_ofs, dst_len, mi, start_nibble, end_nibble, allow_hex);
190
	}
191

  
102 192
	private void showIMSI() {
103 193
		/* 3GPP TS 31.102 4.2.2: IMSI */
104 194
		byte[] IMSI = new byte[9];
105 195
		byte[] msg = {'C', 'u', 'r', 'r', 'e', 'n', 't', ' ', 'I', 'M', 'S', 'I', ':', ' ',
106
			      '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_'};
196
			      ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
107 197

  
108 198
		gsmFile.select((short) SIMView.FID_DF_GSM);
109 199
		gsmFile.select((short) SIMView.FID_EF_IMSI);
......
114 204
			showError(e.getReason());
115 205
			return;
116 206
		}
117
		byte len = (byte) (IMSI[0] * 2);
118

  
119
		for (byte i = (byte)0; i < (byte)18; i++) {
120
			byte msg_i = (byte)(14 + i);
121
			if (i >= len) {
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
			}
128
			showMsg(msg); /* DEBUG */
129
		}
207

  
208
		mi2str(msg, (byte)14, (byte)16, IMSI, false);
130 209
		showMsg(msg);
131 210
	}
132 211

  

Also available in: Unified diff

Add picture from clipboard (Maximum size: 48.8 MB)