blob: 34f22dd056a0e7fb982afe07890c1e3e15ddbb01 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file class_tables.c
2 * simtrace - tables determining APDU case for card emulation. */
3/*
Harald Welte4acaa132016-03-14 15:35:50 +01004 * (C) 2016 by Harald Welte <laforge@gnumonks.org>
5 *
Harald Weltee08da972017-11-13 01:00:26 +09006 * SPDX-License-Identifier: GPL-2.0+
7 *
Harald Welte4acaa132016-03-14 15:35:50 +01008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2, or
10 * any later version as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Harald Welte4acaa132016-03-14 15:35:50 +010016 */
17
18#include <stdint.h>
19#include <osmocom/core/utils.h>
20#include <osmocom/sim/class_tables.h>
21
22static const uint8_t iso7816_ins_tbl[] = {
23 [0xB0] = 2, /* READ BIN */
24 [0xD0] = 3, /* WRITE BIN */
25 [0xD6] = 3, /* UPDATE BIN */
26 [0x0E] = 3, /* ERASE BIN */
27 [0xB2] = 2, /* READ REC */
28 [0xD2] = 3, /* WRITE REC */
29 [0xE2] = 3, /* APPEND REC */
30 [0xDC] = 3, /* UPDATE REC */
31 [0xCA] = 2, /* GET DATA */
32 [0xDA] = 3, /* PUT DATA */
33 [0xA4] = 4, /* SELECT FILE */
34 [0x20] = 3, /* VERIFY */
35 [0x88] = 4, /* INT AUTH */
36 [0x82] = 3, /* EXT AUTH */
37 [0x84] = 2, /* GET CHALLENGE */
38 [0x70] = 2, /* MANAGE CHANNEL */
39};
40
41static const struct osim_cla_ins_case iso7816_4_ins_case[] = {
42 {
43 .cla = 0x00,
44 .cla_mask = 0xF0,
45 .ins_tbl = iso7816_ins_tbl,
46 }, {
47 .cla = 0x80, /* 0x80/0x90 */
48 .cla_mask = 0xE0,
49 .ins_tbl = iso7816_ins_tbl,
50 }, {
51 .cla = 0xB0,
52 .cla_mask = 0xF0,
53 .ins_tbl = iso7816_ins_tbl,
54 }, {
55 .cla = 0xC0,
56 .cla_mask = 0xF0,
57 .ins_tbl = iso7816_ins_tbl,
58 },
59};
60
61const struct osim_cla_ins_card_profile osim_iso7816_cic_profile = {
62 .name = "ISO 7816-4",
63 .description = "ISO 7816-4",
64 .cic_arr = iso7816_4_ins_case,
65 .cic_arr_size = ARRAY_SIZE(iso7816_4_ins_case),
66};
67
68static const uint8_t gsm1111_ins_tbl[256] = {
69 [0xA4] = 4, /* SELECT FILE */
70 [0xF2] = 2, /* STATUS */
71 [0xB0] = 2, /* READ BINARY */
72 [0xD6] = 3, /* UPDATE BINARY */
73 [0xB2] = 2, /* READ RECORD */
74 [0xDC] = 3, /* UPDATE RECORD */
75 [0xA2] = 4, /* SEEK */
76 [0x32] = 4, /* INCREASE */
77 [0x20] = 3, /* VERIFY CHV */
78 [0x24] = 3, /* CHANGE CHV */
79 [0x26] = 3, /* DISABLE CHV */
80 [0x28] = 3, /* ENABLE CHV */
81 [0x2C] = 3, /* UNBLOCK CHV */
82 [0x04] = 1, /* INVALIDATE */
83 [0x44] = 1, /* REHABILITATE */
84 [0x88] = 4, /* RUN GSM ALGO */
85 [0xFA] = 1, /* SLEEP */
86 [0xC0] = 2, /* GET RESPONSE */
87 [0x10] = 3, /* TERMINAL PROFILE */
88 [0xC2] = 4, /* ENVELOPE */
89 [0x12] = 2, /* FETCH */
90 [0x14] = 3, /* TERMINAL RESPONSE */
91};
92
93/* According to Table 9 / Section 9.2 of TS 11.11 */
94static const struct osim_cla_ins_case gsm1111_ins_case[] = {
95 {
96 .cla = 0xA0,
97 .cla_mask = 0xFF,
98 .ins_tbl = gsm1111_ins_tbl,
99 },
100};
101
102const struct osim_cla_ins_card_profile osim_gsm1111_cic_profile = {
103 .name = "GSM SIM",
104 .description = "GSM/3GPP TS 11.11",
105 .cic_arr = gsm1111_ins_case,
106 .cic_arr_size = ARRAY_SIZE(gsm1111_ins_case),
107};
108
109/* ETSI TS 102 221, Table 10.5, CLA = 0x0x, 0x4x or 0x6x */
110static const uint8_t uicc_ins_tbl_046[256] = {
Harald Welteca18c9d2019-09-25 17:46:50 +0200111 [0xA4] = 4, /* SELECT FILE */
Harald Welte4acaa132016-03-14 15:35:50 +0100112 [0xB0] = 2, /* READ BINARY */
113 [0xD6] = 3, /* UPDATE BINARY */
114 [0xB2] = 2, /* READ RECORD */
115 [0xDC] = 3, /* UPDATE RECORD */
116 [0xA2] = 4, /* SEEK */
117 [0x20] = 3, /* VERIFY PIN */
118 [0x24] = 3, /* CHANGE PIN */
119 [0x26] = 3, /* DISABLE PIN */
120 [0x28] = 3, /* ENABLE PIN */
121 [0x2C] = 3, /* UNBLOCK PIN */
122 [0x04] = 1, /* DEACTIVATE FILE */
123 [0x44] = 1, /* ACTIVATE FILE */
124 [0x88] = 4, /* AUTHENTICATE */
125 [0x89] = 4, /* AUTHENTICATE */
126 [0x84] = 2, /* GET CHALLENGE */
127 [0x70] = 2, /* MANAGE CHANNEL */
128 [0x73] = 0x80, /* MANAGE SECURE CHANNEL */
129 [0x75] = 0x80, /* TRANSACT DATA */
130 [0xC0] = 2, /* GET RESPONSE */
131};
132
133static int uicc046_cla_ins_helper(const struct osim_cla_ins_case *cic,
134 const uint8_t *hdr)
135{
136 uint8_t ins = hdr[1];
137 uint8_t p1 = hdr[2];
138 uint8_t p2 = hdr[3];
139 uint8_t p2_cmd;
140
141 switch (ins) {
142 case 0x73: /* MANAGE SECURE CHANNEL */
143 if (p1 == 0x00) /* Retrieve UICC Endpoints */
144 return 2;
145 switch (p1 & 0x07) {
146 case 1: /* Establish SA - Master SA */
147 case 2: /* Establish SA - Conn. SA */
148 case 3: /* Start secure channel SA */
149 p2_cmd = p2 >> 5;
150 if (p2 == 0x80 || p2_cmd == 0) {
151 /* command data */
152 return 3;
153 }
154 if (p2_cmd == 5 || p2_cmd == 1) {
155 /* response data */
156 return 2;
157 }
158 return 0;
159 break;
160 case 4: /* Terminate secure chan SA */
161 return 3;
162 break;
163 }
164 break;
165 case 0x75: /* TRANSACT DATA */
166 if (p1 & 0x04)
167 return 3;
168 else
169 return 2;
170 break;
171 }
172
173 return 0;
174}
175
Karsten Ohme6772dd72022-06-09 02:56:53 +0200176static int gp_cla_ins_helper(const struct osim_cla_ins_case *cic,
177 const uint8_t *hdr)
178{
179 uint8_t ins = hdr[1];
180 uint8_t p1 = hdr[2];
181
182 switch (ins) {
183 case 0xE2: /* STORE DATA */
184 switch (p1 & 0x01) {
185 case 1:
186 return 4;
187 default:
188 return 3;
189 }
190 }
191 return 0;
192}
193
Harald Welte4acaa132016-03-14 15:35:50 +0100194/* ETSI TS 102 221, Table 10.5, CLA = 0x8x, 0xCx or 0xEx */
195static const uint8_t uicc_ins_tbl_8ce[256] = {
196 [0xF2] = 2, /* STATUS */
197 [0x32] = 4, /* INCREASE */
198 [0xCB] = 4, /* RETRIEVE DATA */
199 [0xDB] = 3, /* SET DATA */
200 [0xAA] = 3, /* TERMINAL CAPABILITY */
201};
202
203/* ETSI TS 102 221, Table 10.5, CLA = 0x80 */
204static const uint8_t uicc_ins_tbl_80[256] = {
205 [0x10] = 3, /* TERMINAL PROFILE */
206 [0xC2] = 4, /* ENVELOPE */
207 [0x12] = 2, /* FETCH */
208 [0x14] = 3, /* TERMINAL RESPONSE */
209};
210
Karsten Ohme6772dd72022-06-09 02:56:53 +0200211/* Card Specification v2.3.1*/
212static const uint8_t gp_ins_tbl_8ce[256] = {
213 [0xE4] = 4, /* DELETE */
214 [0xE2] = 0x80, /* STORE DATA */
215 [0xCA] = 4, /* GET DATA */
216 [0xCB] = 4, /* GET DATA */
217 [0xF2] = 4, /* GET STATUS */
218 [0xE6] = 4, /* INSTALL */
219 [0xE8] = 4, /* LOAD */
220 [0xD8] = 4, /* PUT KEY */
221 [0xF0] = 3, /* SET STATUS */
222};
223
Harald Welte4acaa132016-03-14 15:35:50 +0100224static const struct osim_cla_ins_case uicc_ins_case[] = {
225 {
226 .cla = 0x80,
227 .cla_mask = 0xFF,
228 .ins_tbl = uicc_ins_tbl_80,
229 }, {
230 .cla = 0x00,
231 .cla_mask = 0xF0,
232 .helper = uicc046_cla_ins_helper,
233 .ins_tbl = uicc_ins_tbl_046,
234 }, {
235 .cla = 0x40,
236 .cla_mask = 0xF0,
237 .helper = uicc046_cla_ins_helper,
238 .ins_tbl = uicc_ins_tbl_046,
239 }, {
240 .cla = 0x60,
241 .cla_mask = 0xF0,
242 .helper = uicc046_cla_ins_helper,
243 .ins_tbl = uicc_ins_tbl_046,
244 }, {
245 .cla = 0x80,
246 .cla_mask = 0xF0,
247 .ins_tbl = uicc_ins_tbl_8ce,
248 }, {
249 .cla = 0xC0,
250 .cla_mask = 0xF0,
251 .ins_tbl = uicc_ins_tbl_8ce,
252 }, {
253 .cla = 0xE0,
254 .cla_mask = 0xF0,
255 .ins_tbl = uicc_ins_tbl_8ce,
Karsten Ohme6772dd72022-06-09 02:56:53 +0200256 }, {
257 .cla = 0x80,
258 .cla_mask = 0xF0,
259 .helper = gp_cla_ins_helper,
260 .ins_tbl = gp_ins_tbl_8ce,
261 }, {
262 .cla = 0xC0,
263 .cla_mask = 0xF0,
264 .helper = gp_cla_ins_helper,
265 .ins_tbl = gp_ins_tbl_8ce,
266 }, {
267 .cla = 0xE0,
268 .cla_mask = 0xF0,
269 .helper = gp_cla_ins_helper,
270 .ins_tbl = gp_ins_tbl_8ce,
Harald Welte4acaa132016-03-14 15:35:50 +0100271 },
272};
273
274const struct osim_cla_ins_card_profile osim_uicc_cic_profile = {
275 .name = "UICC",
276 .description = "TS 102 221 / 3GPP TS 31.102",
277 .cic_arr = uicc_ins_case,
278 .cic_arr_size = ARRAY_SIZE(uicc_ins_case),
279};
280
281
282static const struct osim_cla_ins_case uicc_sim_ins_case[] = {
283 {
284 .cla = 0xA0,
285 .cla_mask = 0xFF,
286 .ins_tbl = gsm1111_ins_tbl,
287 }, {
288 .cla = 0x80,
289 .cla_mask = 0xFF,
290 .ins_tbl = uicc_ins_tbl_80,
291 }, {
292 .cla = 0x00,
293 .cla_mask = 0xF0,
294 .helper = uicc046_cla_ins_helper,
295 .ins_tbl = uicc_ins_tbl_046,
296 }, {
297 .cla = 0x40,
298 .cla_mask = 0xF0,
299 .helper = uicc046_cla_ins_helper,
300 .ins_tbl = uicc_ins_tbl_046,
301 }, {
302 .cla = 0x60,
303 .cla_mask = 0xF0,
304 .helper = uicc046_cla_ins_helper,
305 .ins_tbl = uicc_ins_tbl_046,
306 }, {
307 .cla = 0x80,
308 .cla_mask = 0xF0,
309 .ins_tbl = uicc_ins_tbl_8ce,
310 }, {
311 .cla = 0xC0,
312 .cla_mask = 0xF0,
313 .ins_tbl = uicc_ins_tbl_8ce,
314 }, {
315 .cla = 0xE0,
316 .cla_mask = 0xF0,
317 .ins_tbl = uicc_ins_tbl_8ce,
Karsten Ohme6772dd72022-06-09 02:56:53 +0200318 }, {
319 .cla = 0x80,
320 .cla_mask = 0xF0,
321 .helper = gp_cla_ins_helper,
322 .ins_tbl = gp_ins_tbl_8ce,
323 }, {
324 .cla = 0xC0,
325 .cla_mask = 0xF0,
326 .helper = gp_cla_ins_helper,
327 .ins_tbl = gp_ins_tbl_8ce,
328 }, {
329 .cla = 0xE0,
330 .cla_mask = 0xF0,
331 .helper = gp_cla_ins_helper,
332 .ins_tbl = gp_ins_tbl_8ce,
Harald Welte4acaa132016-03-14 15:35:50 +0100333 },
Karsten Ohme6772dd72022-06-09 02:56:53 +0200334
Harald Welte4acaa132016-03-14 15:35:50 +0100335};
336
337const struct osim_cla_ins_card_profile osim_uicc_sim_cic_profile = {
338 .name = "UICC+SIM",
339 .description = "TS 102 221 / 3GPP TS 31.102 + GSM TS 11.11",
340 .cic_arr = uicc_sim_ins_case,
341 .cic_arr_size = ARRAY_SIZE(uicc_sim_ins_case),
342};
343
344/* 3GPP TS 31.102 */
345const uint8_t usim_ins_case[256] = {
346 [0x88] = 4, /* AUTHENTICATE */
347};
348
349int osim_determine_apdu_case(const struct osim_cla_ins_card_profile *prof,
350 const uint8_t *hdr)
351{
352 uint8_t cla = hdr[0];
353 uint8_t ins = hdr[1];
354 int i;
355 int rc;
356
357 for (i = 0; i < prof->cic_arr_size; i++) {
358 const struct osim_cla_ins_case *cic = &prof->cic_arr[i];
359 if ((cla & cic->cla_mask) != cic->cla)
360 continue;
361 rc = cic->ins_tbl[ins];
362 switch (rc) {
363 case 0x80:
364 return cic->helper(cic, hdr);
365 case 0x00:
Karsten Ohme6772dd72022-06-09 02:56:53 +0200366 /* continue with further cic, rather than abort
Harald Welte4acaa132016-03-14 15:35:50 +0100367 * now */
368 continue;
369 default:
370 return rc;
371 }
372 }
373 return 0;
374}