aboutsummaryrefslogtreecommitdiffstats
path: root/src/sim/card_fs_sim.c
blob: df6e4e639c8d7bc934f3a7e035efc5f0261cfe34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* classic SIM card specific structures/routines */
/*
 * (C) 2012 by Harald Welte <laforge@gnumonks.org>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <errno.h>
#include <string.h>

#include <osmocom/sim/sim.h>
#include <osmocom/core/talloc.h>
#include <osmocom/gsm/gsm48.h>

#include "sim_int.h"

/* TS 11.11 / Chapter 9.4 */
static const struct osim_card_sw ts11_11_sw[] = {
	{
		0x9000, 0xffff, SW_TYPE_STR, SW_CLS_OK,
		.u.str = "Normal ending of the command",
	}, {
		0x9100, 0xff00, SW_TYPE_STR, SW_CLS_OK,
		.u.str = "Normal ending of the command - proactive command from SIM pending",
	}, {
		0x9e00, 0xff00, SW_TYPE_STR, SW_CLS_OK,
		.u.str = "Normal ending of the command - response data for SIM data download",
	}, {
		0x9f00, 0xff00, SW_TYPE_STR, SW_CLS_OK,
		.u.str = "Normal ending of the command - response data available",
	}, {
		0x9300, 0xffff, SW_TYPE_STR, SW_CLS_POSTP,
		.u.str = "SIM Application Toolkit is busy, command cannot be executed at present",
	}, {
		0x9200, 0xfff0, SW_TYPE_STR, SW_CLS_WARN,
		.u.str = "Memory management - Command successful but after using an internal updat retry X times",
	}, {
		0x9240, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
		.u.str = "Memory management - Memory problem",
	}, {
		0x9400, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
		.u.str = "Referencing management - no EF selected",
	}, {
		0x9402, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
		.u.str = "Referencing management - out of range (invalid address)",
	}, {
		0x9404, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
		.u.str = "Referencing management - file ID not found / pattern not found",
	}, {
		0x9802, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
		.u.str = "Security management - no CHV initialized",
	}, {
		0x9804, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
		.u.str = "Security management - access condition not fulfilled",
	}, {
		0x9808, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
		.u.str = "Security management - in contradiction with CHV status",
	}, {
		0x9810, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
		.u.str = "Security management - in contradiction with invalidation status",
	}, {
		0x9840, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
		.u.str = "Security management - unsuccessful CHV verification, no attempt left",
	}, {
		0x9850, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
		.u.str = "Security management - increase cannot be performed, max value reached",
	}, {
		0x6700, 0xff00, SW_TYPE_STR, SW_CLS_ERROR,
		.u.str = "Application independent - incorrect parameter P3",
	}, {
		0x6b00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR,
		.u.str = "Application independent - incorrect parameter P1 or P2",
	}, {
		0x6d00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR,
		.u.str = "Application independent - unknown instruction code",
	}, {
		0x6e00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR,
		.u.str = "Application independent - wrong instruction class",
	}, {
		0x6f00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR,
		.u.str = "Application independent - technical problem with no diagnostic given",
	},
	OSIM_CARD_SW_LAST
};

static const struct osim_card_sw *sim_card_sws[] = {
	ts11_11_sw,
	NULL
};

static int iccid_decode(struct osim_decoded_data *dd,
			const struct osim_file_desc *desc,
	     		int len, uint8_t *data)
{
	struct osim_decoded_element *elem;

	elem = element_alloc(dd, "ICCID", ELEM_T_BCD, ELEM_REPR_DEC);
	elem->length = len;
	elem->u.buf = talloc_memdup(elem, data, len);

	return 0;
}

static int elp_decode(struct osim_decoded_data *dd,
		      const struct osim_file_desc *desc,
		      int len, uint8_t *data)
{
	int i, num_lp = len / 2;

	for (i = 0; i < num_lp; i++) {
		uint8_t *cur = data + i*2;
		struct osim_decoded_element *elem;
		elem = element_alloc(dd, "Language Code", ELEM_T_STRING, ELEM_REPR_NONE);
		elem->u.buf = (uint8_t *) talloc_strndup(elem, (const char *) cur, 2);
	}

	return 0;
}

static int default_decode(struct osim_decoded_data *dd,
			  const struct osim_file_desc *desc,
			  int len, uint8_t *data)
{
	struct osim_decoded_element *elem;

	elem = element_alloc(dd, "Unknown Payload", ELEM_T_BYTES, ELEM_REPR_HEX);
	elem->u.buf = talloc_memdup(elem, data, len);

	return 0;
}


/* 10.3.1 */
int gsm_lp_decode(struct osim_decoded_data *dd,
		 const struct osim_file_desc *desc,
		 int len, uint8_t *data)
{
	int i;

	for (i = 0; i < len; i++) {
		struct osim_decoded_element *elem;
		elem = element_alloc(dd, "Language Code", ELEM_T_UINT8, ELEM_REPR_DEC);
		elem->u.u8 = data[i];
	}

	return 0;
}

/* 10.3.2 */
int gsm_imsi_decode(struct osim_decoded_data *dd,
		   const struct osim_file_desc *desc,
		   int len, uint8_t *data)
{
	struct osim_decoded_element *elem;

	if (len < 2)
		return -EINVAL;

	elem = element_alloc(dd, "IMSI", ELEM_T_BCD, ELEM_REPR_DEC);
	elem->length = data[0];
	elem->u.buf = talloc_memdup(elem, data+1, len-1);

	return 0;
}

/* 10.3.3 */
static int gsm_kc_decode(struct osim_decoded_data *dd,
			 const struct osim_file_desc *desc,
			 int len, uint8_t *data)
{
	struct osim_decoded_element *kc, *cksn;

	if (len < 9)
		return -EINVAL;

	kc = element_alloc(dd, "Kc", ELEM_T_BYTES, ELEM_REPR_HEX);
	kc->u.buf = talloc_memdup(kc, data, 8);
	cksn = element_alloc(dd, "CKSN", ELEM_T_UINT8, ELEM_REPR_DEC);
	cksn->u.u8 = data[8];

	return 0;
}

/* 10.3.4 */
static int gsm_plmnsel_decode(struct osim_decoded_data *dd,
			      const struct osim_file_desc *desc,
			      int len, uint8_t *data)
{
	int i, n_plmn = len / 3;

	if (n_plmn < 1)
		return -EINVAL;

	for (i = 0; i < n_plmn; i++) {
		uint8_t *cur = data + 3*i;
		struct osim_decoded_element *elem, *mcc, *mnc;
		uint8_t ra_buf[6];
		struct gprs_ra_id ra_id;

		memset(ra_buf, 0, sizeof(ra_buf));
		memcpy(ra_buf, cur, 3);
		gsm48_parse_ra(&ra_id, ra_buf);

		elem = element_alloc(dd, "PLMN", ELEM_T_GROUP, ELEM_REPR_NONE);

		mcc = element_alloc_sub(elem, "MCC", ELEM_T_UINT16, ELEM_REPR_DEC);
		mcc->u.u16 = ra_id.mcc;

		mnc = element_alloc_sub(elem, "MNC", ELEM_T_UINT16, ELEM_REPR_DEC);
		mnc->u.u16 = ra_id.mnc;
	}

	return 0;
}

/* 10.3.5 */
int gsm_hpplmn_decode(struct osim_decoded_data *dd,
		     const struct osim_file_desc *desc,
		     int len, uint8_t *data)
{
	struct osim_decoded_element *elem;

	elem = element_alloc(dd, "Time interval", ELEM_T_UINT8, ELEM_REPR_DEC);
	elem->u.u8 = *data;

	return 0;
}

/* Chapter 10.2.x */
static const struct osim_file_desc sim_ef_in_mf[] = {
	EF_TRANSP(0x2FE2, "EF.ICCID", 0,
		  "ICC Identification", &iccid_decode, NULL),
	EF_TRANSP(0x2F05, "EF.ELP", F_OPTIONAL,
		  "Extended language preference", &elp_decode, NULL),
};

/* Chapter 10.3.x */
static const struct osim_file_desc sim_ef_in_gsm[] = {
	EF_TRANSP(0x6F05, "EF.LP", 0,
		  "Language preference", &gsm_lp_decode, NULL),
	EF_TRANSP(0x6F07, "EF.IMSI", 0,
		  "IMSI", &gsm_imsi_decode, NULL),
	EF_TRANSP(0x6F20, "EF.Kc", 0,
		  "Ciphering key Kc", &gsm_kc_decode, NULL),
	EF_TRANSP(0x6F30, "EF.PLMNsel", F_OPTIONAL,
		  "PLMN selector", &gsm_plmnsel_decode, NULL),
	EF_TRANSP(0x6F31, "EF.HPPLMN", 0,
		  "Higher Priority PLMN search period", &gsm_hpplmn_decode, NULL),
	EF_TRANSP_N(0x6F37, "EF.ACMmax", F_OPTIONAL,
		  "ACM maximum value"),
	EF_TRANSP_N(0x6F38, "EF.SST", 0,
		  "SIM service table"),
	EF_CYCLIC_N(0x6F39, "EF.ACM", F_OPTIONAL,
		  "Accumulated call meter"),
	EF_TRANSP_N(0x6F3E, "EF.GID1", F_OPTIONAL,
		  "Group Identifier Level 1"),
	EF_TRANSP_N(0x6F3F, "EF.GID2", F_OPTIONAL,
		  "Group Identifier Level 2"),
	EF_TRANSP_N(0x6F46, "EF.SPN", F_OPTIONAL,
		  "Service Provider Name"),
	EF_TRANSP_N(0x6F41, "EF.PUCT", F_OPTIONAL,
		  "Price per unit and currency table"),
	EF_TRANSP_N(0x6F45, "EF.CBMI", F_OPTIONAL,
		  "Cell broadcast massage identifier selection"),
	EF_TRANSP_N(0x6F74, "EF.BCCH", 0,
		  "Broadcast control channels"),
	EF_TRANSP_N(0x6F78, "EF.ACC", 0,
		  "Access control class"),
	EF_TRANSP_N(0x6F7B, "EF.FPLMN", 0,
		  "Forbidden PLMNs"),
	EF_TRANSP_N(0x6F7E, "EF.LOCI", 0,
		  "Location information"),
	EF_TRANSP_N(0x6FAD, "EF.AD", 0,
		  "Administrative data"),
	EF_TRANSP_N(0x6FAE, "EF.Phase", 0,
		  "Phase identification"),
	EF_TRANSP_N(0x6FB1, "EF.VGCS", F_OPTIONAL,
		  "Voice Group Call Service"),
	EF_TRANSP_N(0x6FB2, "EF.VGCSS", F_OPTIONAL,
		  "Voice Group Call Service Status"),
	EF_TRANSP_N(0x6FB3, "EF.VBS", F_OPTIONAL,
		  "Voice Broadcast Service"),
	EF_TRANSP_N(0x6FB4, "EF.VBSS", F_OPTIONAL,
		  "Voice Broadcast Service Status"),
	EF_TRANSP_N(0x6FB5, "EF.eMLPP", F_OPTIONAL,
		  "enhanced Mult Level Pre-emption and Priority"),
	EF_TRANSP_N(0x6FB6, "EF.AAeM", F_OPTIONAL,
		  "Automatic Answer for eMLPP Service"),
	EF_TRANSP_N(0x6F48, "EF.CBMID", F_OPTIONAL,
		  "Cell Broadcast Message Identifier for Data Download"),
	EF_TRANSP_N(0x6FB7, "EF.ECC", F_OPTIONAL,
		  "Emergency Call Code"),
	EF_TRANSP_N(0x6F50, "EF.CBMIR", F_OPTIONAL,
		  "Cell broadcast message identifier range selection"),
	EF_TRANSP_N(0x6F2C, "EF.DCK", F_OPTIONAL,
		  "De-personalization Control Keys"),
	EF_TRANSP_N(0x6F32, "EF.CNL", F_OPTIONAL,
		  "Co-operative Network List"),
	EF_LIN_FIX_N(0x6F51, "EF.NIA", F_OPTIONAL,
		   "Network's Indication of Alerting"),
	EF_TRANSP_N(0x6F52, "EF.KcGPRS", F_OPTIONAL,
		  "GPRS Ciphering key KcGPRS"),
	EF_TRANSP_N(0x6F53, "EF.LOCIGPRS", F_OPTIONAL,
		  "GPRS location information"),
	EF_TRANSP_N(0x6F54, "EF.SUME", F_OPTIONAL,
		  "SetUpMenu Elements"),
	EF_TRANSP_N(0x6F60, "EF.PLMNwAcT", F_OPTIONAL,
		  "User controlled PLMN Selector with Access Technology"),
	EF_TRANSP_N(0x6F61, "EF.OPLMNwAcT", F_OPTIONAL,
		  "Operator controlled PLMN Selector with Access Technology"),
	EF_TRANSP_N(0x6F62, "EF.HPLMNwAcT", F_OPTIONAL,
		  "HPLMN Selector with Access Technology"),
	EF_TRANSP_N(0x6F63, "EF.CPBCCH", F_OPTIONAL,
		  "CPBCCH Information"),
	EF_TRANSP_N(0x6F64, "EF.InvScan", F_OPTIONAL,
		  "Investigation Scan"),
};

/* 10.5. */
static const struct osim_file_desc sim_ef_in_telecom[] = {
	EF_LIN_FIX_N(0x6F3A, "EF.ADN", F_OPTIONAL,
		"Abbreviated dialling numbers"),
	EF_LIN_FIX_N(0x6F3B, "EF.FDN", F_OPTIONAL,
		"Fixed dialling numbers"),
	EF_LIN_FIX_N(0x6F3C, "EF.SMS", F_OPTIONAL,
		"Short messages"),
	EF_LIN_FIX_N(0x6F3D, "EF.CCP", F_OPTIONAL,
		"Capability configuration parameters"),
	EF_LIN_FIX_N(0x6F4F, "EF.ECCP", F_OPTIONAL,
		"Extended Capability configuration parameters"),
	EF_LIN_FIX_N(0x6F40, "EF.MSISDN", F_OPTIONAL,
		"MSISDN"),
	EF_LIN_FIX_N(0x6F42, "EF.SMSP", F_OPTIONAL,
		"Short message service parameters"),
	EF_TRANSP_N(0x6F43, "EF.SMSS", F_OPTIONAL,
		"SMS Status"),
	EF_CYCLIC_N(0x6F44, "EF.LND", F_OPTIONAL,
		"Last number dialled"),
	EF_LIN_FIX_N(0x6F4A, "EF.EXT1", F_OPTIONAL,
		"Extension 1"),
	EF_LIN_FIX_N(0x6F4B, "EF.EXT2", F_OPTIONAL,
		"Extension 2"),
	EF_LIN_FIX_N(0x6F4C, "EF.EXT3", F_OPTIONAL,
		"Extension 3"),
	EF_LIN_FIX_N(0x6F4D, "EF.BDN", F_OPTIONAL,
		"Barred dialling numbers"),
	EF_LIN_FIX_N(0x6F4E, "EF.EXT4", F_OPTIONAL,
		"Extension 4"),
	EF_LIN_FIX_N(0x6F47, "EF.SMSR", F_OPTIONAL,
		"Short message status reports"),
	EF_LIN_FIX_N(0x6F58, "EF.CMI", F_OPTIONAL,
		"Comparison Method Information"),
};


/* 10.6. */
static const struct osim_file_desc sim_ef_in_graphics[] = {
	EF_LIN_FIX_N(0x4F20, "EF.IMG", F_OPTIONAL,
		"Image"),
};

struct osim_card_profile *osim_cprof_sim(void *ctx)
{
	struct osim_card_profile *cprof;
	struct osim_file_desc *mf, *gsm, *tc;

	cprof = talloc_zero(ctx, struct osim_card_profile);
	cprof->name = "GSM SIM";
	cprof->sws = sim_card_sws;

	mf = alloc_df(cprof, 0x3f00, "MF");

	cprof->mf = mf;

	add_filedesc(mf, sim_ef_in_mf, ARRAY_SIZE(sim_ef_in_mf));
	gsm = add_df_with_ef(mf, 0x7F20, "DF.GSM", sim_ef_in_gsm,
			ARRAY_SIZE(sim_ef_in_gsm));
	add_df_with_ef(gsm, 0x5F30, "DF.IRIDIUM", NULL, 0);
	add_df_with_ef(gsm, 0x5F31, "DF.GLOBST", NULL, 0);
	add_df_with_ef(gsm, 0x5F32, "DF.ICO", NULL, 0);
	add_df_with_ef(gsm, 0x5F33, "DF.ACeS", NULL, 0);
	add_df_with_ef(gsm, 0x5F40, "DF.ACeS", NULL, 0);
	add_df_with_ef(gsm, 0x5F60, "DF.CTS", NULL, 0);
	add_df_with_ef(gsm, 0x5F70, "DF.SoLSA", NULL, 0);
	tc = add_df_with_ef(mf, 0x7F10, "DF.TELECOM", sim_ef_in_telecom,
			ARRAY_SIZE(sim_ef_in_telecom));
	add_df_with_ef(tc, 0x5F50, "DF.GRAPHICS", sim_ef_in_graphics,
			ARRAY_SIZE(sim_ef_in_graphics));

	return cprof;
}