blob: d5292baa6091e3f525b9f7abce6c0b7aae5e08e8 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file reader.c
2 * Card reader abstraction for libosmosim. */
Harald Weltead418632012-09-10 10:49:59 +02003/*
4 * (C) 2012 by Harald Welte <laforge@gnumonks.org>
5 *
6 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Harald Weltead418632012-09-10 10:49:59 +020010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 */
25
26
Harald Welted54c2ee2012-01-17 18:25:50 +010027#include <errno.h>
28#include <stdint.h>
29#include <stdlib.h>
30#include <string.h>
31#include <stdio.h>
32
Harald Weltea5c92552012-09-10 21:05:42 +020033#include <netinet/in.h>
34
35#include <osmocom/core/msgb.h>
Harald Welted54c2ee2012-01-17 18:25:50 +010036#include <osmocom/sim/sim.h>
37
Harald Welte3c44a642020-03-15 22:50:06 +010038#include "config.h"
Harald Welted54c2ee2012-01-17 18:25:50 +010039
40#include "sim_int.h"
41
Harald Weltea5c92552012-09-10 21:05:42 +020042/* remove the SW from end of the message */
Harald Welted54c2ee2012-01-17 18:25:50 +010043static int get_sw(struct msgb *resp)
44{
45 int ret;
46
47 if (!msgb_apdu_de(resp) || msgb_apdu_le(resp) < 2)
48 return -EIO;
49
Harald Weltea5c92552012-09-10 21:05:42 +020050 ret = msgb_get_u16(resp);
Harald Welted54c2ee2012-01-17 18:25:50 +010051
52 return ret;
53}
54
55/* According to ISO7816-4 Annex A */
56static int transceive_apdu_t0(struct osim_card_hdl *st, struct msgb *amsg)
57{
58 struct osim_reader_hdl *rh = st->reader;
59 struct msgb *tmsg = msgb_alloc(1024, "TPDU");
60 struct osim_apdu_cmd_hdr *tpduh;
61 uint8_t *cur;
62 uint16_t sw;
63 int rc, num_resp = 0;
64
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +020065 if (!tmsg)
66 return -ENOMEM;
67
Harald Welted54c2ee2012-01-17 18:25:50 +010068 /* create TPDU header from APDU header */
69 tpduh = (struct osim_apdu_cmd_hdr *) msgb_put(tmsg, sizeof(*tpduh));
70 memcpy(tpduh, msgb_apdu_h(amsg), sizeof(*tpduh));
71
72 switch (msgb_apdu_case(amsg)) {
73 case APDU_CASE_1:
74 tpduh->p3 = 0x00;
75 break;
Kevin Redone07967f2012-09-11 11:44:18 +020076 case APDU_CASE_2S:
Harald Welted54c2ee2012-01-17 18:25:50 +010077 tpduh->p3 = msgb_apdu_le(amsg);
78 break;
Kevin Redone07967f2012-09-11 11:44:18 +020079 case APDU_CASE_2E:
Harald Welted54c2ee2012-01-17 18:25:50 +010080 if (msgb_apdu_le(amsg) <= 256) {
81 /* case 2E.1 */
82 tpduh->p3 = msgb_apdu_le(amsg) & 0xff;
83 } else {
84 /* case 2E.2 */
85 tpduh->p3 = 0;
86 msgb_put_u16(tmsg, msgb_apdu_le(amsg));
87 }
88 break;
Kevin Redone07967f2012-09-11 11:44:18 +020089 case APDU_CASE_3S:
90 case APDU_CASE_4S:
Harald Welted54c2ee2012-01-17 18:25:50 +010091 tpduh->p3 = msgb_apdu_lc(amsg);
92 cur = msgb_put(tmsg, tpduh->p3);
93 memcpy(cur, msgb_apdu_dc(amsg), tpduh->p3);
94 break;
Kevin Redone07967f2012-09-11 11:44:18 +020095 case APDU_CASE_3E:
96 case APDU_CASE_4E:
Harald Welted54c2ee2012-01-17 18:25:50 +010097 if (msgb_apdu_lc(amsg) < 256) {
98 /* Case 3E.1 */
99 tpduh->p3 = msgb_apdu_lc(amsg);
100 } else {
101 /* Case 3E.2 */
102 /* FXIME: Split using ENVELOPE! */
103 return -1;
104 }
105 break;
106 }
107
108transceive_again:
109
110 /* store pointer to start of response */
111 tmsg->l3h = tmsg->tail;
112
113 /* transceive */
114 rc = rh->ops->transceive(st->reader, tmsg);
115 if (rc < 0) {
116 msgb_free(tmsg);
117 return rc;
118 }
119 msgb_apdu_sw(tmsg) = get_sw(tmsg);
120
121 /* increase number of responsese received */
122 num_resp++;
123
124 /* save SW */
125 sw = msgb_apdu_sw(tmsg);
126 printf("sw = 0x%04x\n", sw);
127 msgb_apdu_sw(amsg) = sw;
128
129 switch (msgb_apdu_case(amsg)) {
130 case APDU_CASE_1:
Kevin Redone07967f2012-09-11 11:44:18 +0200131 case APDU_CASE_3S:
Harald Welted54c2ee2012-01-17 18:25:50 +0100132 /* just copy SW */
133 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200134 case APDU_CASE_2S:
Harald Welted54c2ee2012-01-17 18:25:50 +0100135case_2s:
136 switch (sw >> 8) {
137 case 0x67: /* Case 2S.2: Le definitely not accepted */
138 break;
139 case 0x6c: /* Case 2S.3: Le not accepted, La indicated */
140 tpduh->p3 = sw & 0xff;
141 /* re-issue the command with La as */
142 goto transceive_again;
143 break;
144 case 0x90:
145 /* Case 2S.1, fall-through */
146 case 0x91: case 0x92: case 0x93: case 0x94: case 0x95:
147 case 0x96: case 0x97: case 0x98: case 0x99: case 0x9a:
148 case 0x9b: case 0x9c: case 0x9d: case 0x9e: case 0x9f:
149 /* Case 2S.4 */
150 /* copy response data over */
151 cur = msgb_put(amsg, msgb_l3len(tmsg));
152 memcpy(cur, tmsg->l3h, msgb_l3len(tmsg));
153 }
154 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200155 case APDU_CASE_4S:
Harald Welted54c2ee2012-01-17 18:25:50 +0100156 /* FIXME: this is 4S.2 only for 2nd... response: */
157 if (num_resp >= 2)
158 goto case_2s;
159
160 switch (sw >> 8) {
161 case 0x60: case 0x62: case 0x63: case 0x64: case 0x65:
162 case 0x66: case 0x67: case 0x68: case 0x69: case 0x6a:
163 case 0x6b: case 0x6c: case 0x6d: case 0x6e: case 0x6f:
164 /* Case 4S.1: Command not accepted: just copy SW */
165 break;
166 case 0x90:
167 /* case 4S.2: Command accepted */
168 tpduh->ins = 0xC0;
169 tpduh->p1 = tpduh->p2 = 0;
170 tpduh->p3 = msgb_apdu_le(amsg);
171 /* strip off current result */
172 msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh));
173 goto transceive_again;
174 break;
175 case 0x61: /* Case 4S.3: command accepted with info added */
Harald Welted83d2962013-03-04 17:52:33 +0000176 case 0x9F: /* FIXME: This is specific to SIM cards */
Harald Welted54c2ee2012-01-17 18:25:50 +0100177 tpduh->ins = 0xC0;
178 tpduh->p1 = tpduh->p2 = 0;
179 tpduh->p3 = OSMO_MIN(msgb_apdu_le(amsg), sw & 0xff);
180 /* strip off current result */
181 msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh));
182 goto transceive_again;
183 break;
184 }
185 /* Case 4S.2: Command accepted: just copy SW */
186 /* Case 4S.4: Just copy SW */
187 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200188 case APDU_CASE_2E:
Harald Welted54c2ee2012-01-17 18:25:50 +0100189 if (msgb_apdu_le(amsg) <= 256) {
190 /* Case 2E.1: Le <= 256 */
191 goto case_2s;
192 }
193 switch (sw >> 8) {
194 case 0x67:
195 /* Case 2E.2a: wrong length, abort */
196 break;
197 case 0x6c:
198 /* Case 2E.2b: wrong length, La given */
199 tpduh->p3 = sw & 0xff;
200 /* re-issue the command with La as given */
201 goto transceive_again;
202 break;
203 case 0x90:
204 /* Case 2E.2c: */
205 break;
206 case 0x61:
207 /* Case 2E.2d: more data available */
208 /* FIXME: issue yet another GET RESPONSE */
209 break;
210 }
211 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200212 case APDU_CASE_3E:
Harald Welted54c2ee2012-01-17 18:25:50 +0100213 /* FIXME: handling for ENVELOPE splitting */
214 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200215 case APDU_CASE_4E:
Harald Welted54c2ee2012-01-17 18:25:50 +0100216 break;
217 }
218
219 msgb_free(tmsg);
220
221 /* compute total length of response data */
222 msgb_apdu_le(amsg) = amsg->tail - msgb_apdu_de(amsg);
223
224 return sw;
225}
226
Harald Welte55790aa2014-10-26 18:46:50 +0100227/* FIXME: T=1 According to ISO7816-4 Annex B */
Harald Welted54c2ee2012-01-17 18:25:50 +0100228
229int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg)
230{
Harald Welte55790aa2014-10-26 18:46:50 +0100231 switch (st->card->proto) {
232 case OSIM_PROTO_T0:
233 return transceive_apdu_t0(st->card, amsg);
234 default:
235 return -ENOTSUP;
236 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100237}
238
Harald Welte55790aa2014-10-26 18:46:50 +0100239struct osim_reader_hdl *osim_reader_open(enum osim_reader_driver driver, int idx,
240 const char *name, void *ctx)
Harald Welted54c2ee2012-01-17 18:25:50 +0100241{
Harald Welte55790aa2014-10-26 18:46:50 +0100242 const struct osim_reader_ops *ops;
Harald Welted54c2ee2012-01-17 18:25:50 +0100243 struct osim_reader_hdl *rh;
244
Harald Welte55790aa2014-10-26 18:46:50 +0100245 switch (driver) {
Harald Welte3c44a642020-03-15 22:50:06 +0100246#ifdef HAVE_PCSC
Harald Welte55790aa2014-10-26 18:46:50 +0100247 case OSIM_READER_DRV_PCSC:
248 ops = &pcsc_reader_ops;
249 break;
Harald Welte3c44a642020-03-15 22:50:06 +0100250#endif
Harald Welte55790aa2014-10-26 18:46:50 +0100251 default:
252 return NULL;
253 }
254
Harald Welted83d2962013-03-04 17:52:33 +0000255 rh = ops->reader_open(idx, name, ctx);
Harald Welted54c2ee2012-01-17 18:25:50 +0100256 if (!rh)
257 return NULL;
258 rh->ops = ops;
259
Harald Welte55790aa2014-10-26 18:46:50 +0100260 /* FIXME: for now we only do T=0 on all readers */
261 rh->proto_supported = (1 << OSIM_PROTO_T0);
262
Harald Welted54c2ee2012-01-17 18:25:50 +0100263 return rh;
264}
265
Harald Welte55790aa2014-10-26 18:46:50 +0100266struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh, enum osim_proto proto)
Harald Welted54c2ee2012-01-17 18:25:50 +0100267{
Harald Welte55790aa2014-10-26 18:46:50 +0100268 struct osim_card_hdl *ch;
269
270 if (!(rh->proto_supported & (1 << proto)))
271 return NULL;
272
273 ch = rh->ops->card_open(rh, proto);
274 if (!ch)
275 return NULL;
276
277 ch->proto = proto;
278
279 return ch;
Harald Welted54c2ee2012-01-17 18:25:50 +0100280}