blob: 9c513879f1a53d1a28dd2fceb555be61c149b5da [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 */
Harald Welted6a49ce2022-07-20 18:32:19 +0200116 [0xA2] = 4, /* SEARCH RECORD */
Harald Welte4acaa132016-03-14 15:35:50 +0100117 [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 */
Harald Welted6a49ce2022-07-20 18:32:19 +0200201 [0x78] = 4, /* GET IDENTITY */
Harald Welte4acaa132016-03-14 15:35:50 +0100202};
203
204/* ETSI TS 102 221, Table 10.5, CLA = 0x80 */
205static const uint8_t uicc_ins_tbl_80[256] = {
206 [0x10] = 3, /* TERMINAL PROFILE */
207 [0xC2] = 4, /* ENVELOPE */
208 [0x12] = 2, /* FETCH */
209 [0x14] = 3, /* TERMINAL RESPONSE */
Harald Welted6a49ce2022-07-20 18:32:19 +0200210 [0x76] = 4, /* SUSPEND UICC */
211 [0x7A] = 4, /* EXCHANGE CAPABILITIES */
Harald Welte4acaa132016-03-14 15:35:50 +0100212};
213
Karsten Ohme6772dd72022-06-09 02:56:53 +0200214/* Card Specification v2.3.1*/
215static const uint8_t gp_ins_tbl_8ce[256] = {
216 [0xE4] = 4, /* DELETE */
217 [0xE2] = 0x80, /* STORE DATA */
218 [0xCA] = 4, /* GET DATA */
219 [0xCB] = 4, /* GET DATA */
220 [0xF2] = 4, /* GET STATUS */
221 [0xE6] = 4, /* INSTALL */
222 [0xE8] = 4, /* LOAD */
223 [0xD8] = 4, /* PUT KEY */
224 [0xF0] = 3, /* SET STATUS */
225};
226
Harald Welte4acaa132016-03-14 15:35:50 +0100227static const struct osim_cla_ins_case uicc_ins_case[] = {
228 {
229 .cla = 0x80,
230 .cla_mask = 0xFF,
231 .ins_tbl = uicc_ins_tbl_80,
232 }, {
233 .cla = 0x00,
234 .cla_mask = 0xF0,
235 .helper = uicc046_cla_ins_helper,
236 .ins_tbl = uicc_ins_tbl_046,
237 }, {
238 .cla = 0x40,
239 .cla_mask = 0xF0,
240 .helper = uicc046_cla_ins_helper,
241 .ins_tbl = uicc_ins_tbl_046,
242 }, {
243 .cla = 0x60,
244 .cla_mask = 0xF0,
245 .helper = uicc046_cla_ins_helper,
246 .ins_tbl = uicc_ins_tbl_046,
247 }, {
248 .cla = 0x80,
249 .cla_mask = 0xF0,
250 .ins_tbl = uicc_ins_tbl_8ce,
251 }, {
252 .cla = 0xC0,
253 .cla_mask = 0xF0,
254 .ins_tbl = uicc_ins_tbl_8ce,
255 }, {
256 .cla = 0xE0,
257 .cla_mask = 0xF0,
258 .ins_tbl = uicc_ins_tbl_8ce,
Karsten Ohme6772dd72022-06-09 02:56:53 +0200259 }, {
260 .cla = 0x80,
261 .cla_mask = 0xF0,
262 .helper = gp_cla_ins_helper,
263 .ins_tbl = gp_ins_tbl_8ce,
264 }, {
265 .cla = 0xC0,
266 .cla_mask = 0xF0,
267 .helper = gp_cla_ins_helper,
268 .ins_tbl = gp_ins_tbl_8ce,
269 }, {
270 .cla = 0xE0,
271 .cla_mask = 0xF0,
272 .helper = gp_cla_ins_helper,
273 .ins_tbl = gp_ins_tbl_8ce,
Harald Welte4acaa132016-03-14 15:35:50 +0100274 },
275};
276
277const struct osim_cla_ins_card_profile osim_uicc_cic_profile = {
278 .name = "UICC",
279 .description = "TS 102 221 / 3GPP TS 31.102",
280 .cic_arr = uicc_ins_case,
281 .cic_arr_size = ARRAY_SIZE(uicc_ins_case),
282};
283
284
285static const struct osim_cla_ins_case uicc_sim_ins_case[] = {
286 {
287 .cla = 0xA0,
288 .cla_mask = 0xFF,
289 .ins_tbl = gsm1111_ins_tbl,
290 }, {
291 .cla = 0x80,
292 .cla_mask = 0xFF,
293 .ins_tbl = uicc_ins_tbl_80,
294 }, {
295 .cla = 0x00,
296 .cla_mask = 0xF0,
297 .helper = uicc046_cla_ins_helper,
298 .ins_tbl = uicc_ins_tbl_046,
299 }, {
300 .cla = 0x40,
301 .cla_mask = 0xF0,
302 .helper = uicc046_cla_ins_helper,
303 .ins_tbl = uicc_ins_tbl_046,
304 }, {
305 .cla = 0x60,
306 .cla_mask = 0xF0,
307 .helper = uicc046_cla_ins_helper,
308 .ins_tbl = uicc_ins_tbl_046,
309 }, {
310 .cla = 0x80,
311 .cla_mask = 0xF0,
312 .ins_tbl = uicc_ins_tbl_8ce,
313 }, {
314 .cla = 0xC0,
315 .cla_mask = 0xF0,
316 .ins_tbl = uicc_ins_tbl_8ce,
317 }, {
318 .cla = 0xE0,
319 .cla_mask = 0xF0,
320 .ins_tbl = uicc_ins_tbl_8ce,
Karsten Ohme6772dd72022-06-09 02:56:53 +0200321 }, {
322 .cla = 0x80,
323 .cla_mask = 0xF0,
324 .helper = gp_cla_ins_helper,
325 .ins_tbl = gp_ins_tbl_8ce,
326 }, {
327 .cla = 0xC0,
328 .cla_mask = 0xF0,
329 .helper = gp_cla_ins_helper,
330 .ins_tbl = gp_ins_tbl_8ce,
331 }, {
332 .cla = 0xE0,
333 .cla_mask = 0xF0,
334 .helper = gp_cla_ins_helper,
335 .ins_tbl = gp_ins_tbl_8ce,
Harald Welte4acaa132016-03-14 15:35:50 +0100336 },
Karsten Ohme6772dd72022-06-09 02:56:53 +0200337
Harald Welte4acaa132016-03-14 15:35:50 +0100338};
339
340const struct osim_cla_ins_card_profile osim_uicc_sim_cic_profile = {
341 .name = "UICC+SIM",
342 .description = "TS 102 221 / 3GPP TS 31.102 + GSM TS 11.11",
343 .cic_arr = uicc_sim_ins_case,
344 .cic_arr_size = ARRAY_SIZE(uicc_sim_ins_case),
345};
346
347/* 3GPP TS 31.102 */
348const uint8_t usim_ins_case[256] = {
349 [0x88] = 4, /* AUTHENTICATE */
350};
351
352int osim_determine_apdu_case(const struct osim_cla_ins_card_profile *prof,
353 const uint8_t *hdr)
354{
355 uint8_t cla = hdr[0];
356 uint8_t ins = hdr[1];
357 int i;
358 int rc;
359
360 for (i = 0; i < prof->cic_arr_size; i++) {
361 const struct osim_cla_ins_case *cic = &prof->cic_arr[i];
362 if ((cla & cic->cla_mask) != cic->cla)
363 continue;
364 rc = cic->ins_tbl[ins];
365 switch (rc) {
366 case 0x80:
367 return cic->helper(cic, hdr);
368 case 0x00:
Karsten Ohme6772dd72022-06-09 02:56:53 +0200369 /* continue with further cic, rather than abort
Harald Welte4acaa132016-03-14 15:35:50 +0100370 * now */
371 continue;
372 default:
373 return rc;
374 }
375 }
376 return 0;
377}