blob: 534ef53d3cc87578264e2c011ea87e8d35da6de9 [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
176/* ETSI TS 102 221, Table 10.5, CLA = 0x8x, 0xCx or 0xEx */
177static const uint8_t uicc_ins_tbl_8ce[256] = {
178 [0xF2] = 2, /* STATUS */
179 [0x32] = 4, /* INCREASE */
180 [0xCB] = 4, /* RETRIEVE DATA */
181 [0xDB] = 3, /* SET DATA */
182 [0xAA] = 3, /* TERMINAL CAPABILITY */
183};
184
185/* ETSI TS 102 221, Table 10.5, CLA = 0x80 */
186static const uint8_t uicc_ins_tbl_80[256] = {
187 [0x10] = 3, /* TERMINAL PROFILE */
188 [0xC2] = 4, /* ENVELOPE */
189 [0x12] = 2, /* FETCH */
190 [0x14] = 3, /* TERMINAL RESPONSE */
191};
192
193static const struct osim_cla_ins_case uicc_ins_case[] = {
194 {
195 .cla = 0x80,
196 .cla_mask = 0xFF,
197 .ins_tbl = uicc_ins_tbl_80,
198 }, {
199 .cla = 0x00,
200 .cla_mask = 0xF0,
201 .helper = uicc046_cla_ins_helper,
202 .ins_tbl = uicc_ins_tbl_046,
203 }, {
204 .cla = 0x40,
205 .cla_mask = 0xF0,
206 .helper = uicc046_cla_ins_helper,
207 .ins_tbl = uicc_ins_tbl_046,
208 }, {
209 .cla = 0x60,
210 .cla_mask = 0xF0,
211 .helper = uicc046_cla_ins_helper,
212 .ins_tbl = uicc_ins_tbl_046,
213 }, {
214 .cla = 0x80,
215 .cla_mask = 0xF0,
216 .ins_tbl = uicc_ins_tbl_8ce,
217 }, {
218 .cla = 0xC0,
219 .cla_mask = 0xF0,
220 .ins_tbl = uicc_ins_tbl_8ce,
221 }, {
222 .cla = 0xE0,
223 .cla_mask = 0xF0,
224 .ins_tbl = uicc_ins_tbl_8ce,
225 },
226};
227
228const struct osim_cla_ins_card_profile osim_uicc_cic_profile = {
229 .name = "UICC",
230 .description = "TS 102 221 / 3GPP TS 31.102",
231 .cic_arr = uicc_ins_case,
232 .cic_arr_size = ARRAY_SIZE(uicc_ins_case),
233};
234
235
236static const struct osim_cla_ins_case uicc_sim_ins_case[] = {
237 {
238 .cla = 0xA0,
239 .cla_mask = 0xFF,
240 .ins_tbl = gsm1111_ins_tbl,
241 }, {
242 .cla = 0x80,
243 .cla_mask = 0xFF,
244 .ins_tbl = uicc_ins_tbl_80,
245 }, {
246 .cla = 0x00,
247 .cla_mask = 0xF0,
248 .helper = uicc046_cla_ins_helper,
249 .ins_tbl = uicc_ins_tbl_046,
250 }, {
251 .cla = 0x40,
252 .cla_mask = 0xF0,
253 .helper = uicc046_cla_ins_helper,
254 .ins_tbl = uicc_ins_tbl_046,
255 }, {
256 .cla = 0x60,
257 .cla_mask = 0xF0,
258 .helper = uicc046_cla_ins_helper,
259 .ins_tbl = uicc_ins_tbl_046,
260 }, {
261 .cla = 0x80,
262 .cla_mask = 0xF0,
263 .ins_tbl = uicc_ins_tbl_8ce,
264 }, {
265 .cla = 0xC0,
266 .cla_mask = 0xF0,
267 .ins_tbl = uicc_ins_tbl_8ce,
268 }, {
269 .cla = 0xE0,
270 .cla_mask = 0xF0,
271 .ins_tbl = uicc_ins_tbl_8ce,
272 },
273};
274
275const struct osim_cla_ins_card_profile osim_uicc_sim_cic_profile = {
276 .name = "UICC+SIM",
277 .description = "TS 102 221 / 3GPP TS 31.102 + GSM TS 11.11",
278 .cic_arr = uicc_sim_ins_case,
279 .cic_arr_size = ARRAY_SIZE(uicc_sim_ins_case),
280};
281
282/* 3GPP TS 31.102 */
283const uint8_t usim_ins_case[256] = {
284 [0x88] = 4, /* AUTHENTICATE */
285};
286
287int osim_determine_apdu_case(const struct osim_cla_ins_card_profile *prof,
288 const uint8_t *hdr)
289{
290 uint8_t cla = hdr[0];
291 uint8_t ins = hdr[1];
292 int i;
293 int rc;
294
295 for (i = 0; i < prof->cic_arr_size; i++) {
296 const struct osim_cla_ins_case *cic = &prof->cic_arr[i];
297 if ((cla & cic->cla_mask) != cic->cla)
298 continue;
299 rc = cic->ins_tbl[ins];
300 switch (rc) {
301 case 0x80:
302 return cic->helper(cic, hdr);
303 case 0x00:
304 /* continue with fruther cic, rather than abort
305 * now */
306 continue;
307 default:
308 return rc;
309 }
310 }
311 return 0;
312}