blob: 982b2eef5a5bc6baef6109a1be2803b2a821d920 [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 *
Harald Weltead418632012-09-10 10:49:59 +020020 */
21
22
Harald Welted54c2ee2012-01-17 18:25:50 +010023#include <errno.h>
24#include <stdint.h>
25#include <stdlib.h>
26#include <string.h>
27#include <stdio.h>
28
Harald Weltea5c92552012-09-10 21:05:42 +020029#include <netinet/in.h>
30
31#include <osmocom/core/msgb.h>
Harald Welted54c2ee2012-01-17 18:25:50 +010032#include <osmocom/sim/sim.h>
33
Harald Welte3c44a642020-03-15 22:50:06 +010034#include "config.h"
Harald Welted54c2ee2012-01-17 18:25:50 +010035
36#include "sim_int.h"
37
Harald Weltea5c92552012-09-10 21:05:42 +020038/* remove the SW from end of the message */
Harald Welted54c2ee2012-01-17 18:25:50 +010039static int get_sw(struct msgb *resp)
40{
41 int ret;
42
Philipp Maier9802c142022-05-24 17:50:17 +020043 if (!resp->l2h || msgb_apdu_le(resp) < 2)
Harald Welted54c2ee2012-01-17 18:25:50 +010044 return -EIO;
45
Harald Weltea5c92552012-09-10 21:05:42 +020046 ret = msgb_get_u16(resp);
Harald Welted54c2ee2012-01-17 18:25:50 +010047
48 return ret;
49}
50
51/* According to ISO7816-4 Annex A */
52static int transceive_apdu_t0(struct osim_card_hdl *st, struct msgb *amsg)
53{
54 struct osim_reader_hdl *rh = st->reader;
55 struct msgb *tmsg = msgb_alloc(1024, "TPDU");
56 struct osim_apdu_cmd_hdr *tpduh;
57 uint8_t *cur;
58 uint16_t sw;
59 int rc, num_resp = 0;
60
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +020061 if (!tmsg)
62 return -ENOMEM;
63
Harald Welted54c2ee2012-01-17 18:25:50 +010064 /* create TPDU header from APDU header */
65 tpduh = (struct osim_apdu_cmd_hdr *) msgb_put(tmsg, sizeof(*tpduh));
66 memcpy(tpduh, msgb_apdu_h(amsg), sizeof(*tpduh));
67
68 switch (msgb_apdu_case(amsg)) {
69 case APDU_CASE_1:
70 tpduh->p3 = 0x00;
71 break;
Kevin Redone07967f2012-09-11 11:44:18 +020072 case APDU_CASE_2S:
Harald Welted54c2ee2012-01-17 18:25:50 +010073 tpduh->p3 = msgb_apdu_le(amsg);
74 break;
Kevin Redone07967f2012-09-11 11:44:18 +020075 case APDU_CASE_2E:
Harald Welted54c2ee2012-01-17 18:25:50 +010076 if (msgb_apdu_le(amsg) <= 256) {
77 /* case 2E.1 */
78 tpduh->p3 = msgb_apdu_le(amsg) & 0xff;
79 } else {
80 /* case 2E.2 */
81 tpduh->p3 = 0;
82 msgb_put_u16(tmsg, msgb_apdu_le(amsg));
83 }
84 break;
Kevin Redone07967f2012-09-11 11:44:18 +020085 case APDU_CASE_3S:
86 case APDU_CASE_4S:
Harald Welted54c2ee2012-01-17 18:25:50 +010087 tpduh->p3 = msgb_apdu_lc(amsg);
88 cur = msgb_put(tmsg, tpduh->p3);
89 memcpy(cur, msgb_apdu_dc(amsg), tpduh->p3);
90 break;
Kevin Redone07967f2012-09-11 11:44:18 +020091 case APDU_CASE_3E:
92 case APDU_CASE_4E:
Harald Welted54c2ee2012-01-17 18:25:50 +010093 if (msgb_apdu_lc(amsg) < 256) {
94 /* Case 3E.1 */
95 tpduh->p3 = msgb_apdu_lc(amsg);
96 } else {
97 /* Case 3E.2 */
98 /* FXIME: Split using ENVELOPE! */
99 return -1;
100 }
101 break;
102 }
103
104transceive_again:
105
106 /* store pointer to start of response */
107 tmsg->l3h = tmsg->tail;
108
109 /* transceive */
110 rc = rh->ops->transceive(st->reader, tmsg);
111 if (rc < 0) {
112 msgb_free(tmsg);
113 return rc;
114 }
115 msgb_apdu_sw(tmsg) = get_sw(tmsg);
116
117 /* increase number of responsese received */
118 num_resp++;
119
120 /* save SW */
121 sw = msgb_apdu_sw(tmsg);
Harald Welted54c2ee2012-01-17 18:25:50 +0100122 msgb_apdu_sw(amsg) = sw;
123
124 switch (msgb_apdu_case(amsg)) {
125 case APDU_CASE_1:
Kevin Redone07967f2012-09-11 11:44:18 +0200126 case APDU_CASE_3S:
Harald Welted54c2ee2012-01-17 18:25:50 +0100127 /* just copy SW */
128 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200129 case APDU_CASE_2S:
Harald Welted54c2ee2012-01-17 18:25:50 +0100130case_2s:
131 switch (sw >> 8) {
132 case 0x67: /* Case 2S.2: Le definitely not accepted */
133 break;
134 case 0x6c: /* Case 2S.3: Le not accepted, La indicated */
135 tpduh->p3 = sw & 0xff;
136 /* re-issue the command with La as */
137 goto transceive_again;
138 break;
139 case 0x90:
140 /* Case 2S.1, fall-through */
141 case 0x91: case 0x92: case 0x93: case 0x94: case 0x95:
142 case 0x96: case 0x97: case 0x98: case 0x99: case 0x9a:
143 case 0x9b: case 0x9c: case 0x9d: case 0x9e: case 0x9f:
144 /* Case 2S.4 */
145 /* copy response data over */
146 cur = msgb_put(amsg, msgb_l3len(tmsg));
147 memcpy(cur, tmsg->l3h, msgb_l3len(tmsg));
148 }
149 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200150 case APDU_CASE_4S:
Harald Welted54c2ee2012-01-17 18:25:50 +0100151 /* FIXME: this is 4S.2 only for 2nd... response: */
152 if (num_resp >= 2)
153 goto case_2s;
154
155 switch (sw >> 8) {
156 case 0x60: case 0x62: case 0x63: case 0x64: case 0x65:
157 case 0x66: case 0x67: case 0x68: case 0x69: case 0x6a:
158 case 0x6b: case 0x6c: case 0x6d: case 0x6e: case 0x6f:
159 /* Case 4S.1: Command not accepted: just copy SW */
160 break;
161 case 0x90:
162 /* case 4S.2: Command accepted */
163 tpduh->ins = 0xC0;
164 tpduh->p1 = tpduh->p2 = 0;
165 tpduh->p3 = msgb_apdu_le(amsg);
166 /* strip off current result */
167 msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh));
168 goto transceive_again;
169 break;
170 case 0x61: /* Case 4S.3: command accepted with info added */
Harald Welted83d2962013-03-04 17:52:33 +0000171 case 0x9F: /* FIXME: This is specific to SIM cards */
Harald Welted54c2ee2012-01-17 18:25:50 +0100172 tpduh->ins = 0xC0;
173 tpduh->p1 = tpduh->p2 = 0;
174 tpduh->p3 = OSMO_MIN(msgb_apdu_le(amsg), sw & 0xff);
175 /* strip off current result */
176 msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh));
177 goto transceive_again;
178 break;
179 }
180 /* Case 4S.2: Command accepted: just copy SW */
181 /* Case 4S.4: Just copy SW */
182 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200183 case APDU_CASE_2E:
Harald Welted54c2ee2012-01-17 18:25:50 +0100184 if (msgb_apdu_le(amsg) <= 256) {
185 /* Case 2E.1: Le <= 256 */
186 goto case_2s;
187 }
188 switch (sw >> 8) {
189 case 0x67:
190 /* Case 2E.2a: wrong length, abort */
191 break;
192 case 0x6c:
193 /* Case 2E.2b: wrong length, La given */
194 tpduh->p3 = sw & 0xff;
195 /* re-issue the command with La as given */
196 goto transceive_again;
197 break;
198 case 0x90:
199 /* Case 2E.2c: */
200 break;
201 case 0x61:
202 /* Case 2E.2d: more data available */
203 /* FIXME: issue yet another GET RESPONSE */
204 break;
205 }
206 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200207 case APDU_CASE_3E:
Harald Welted54c2ee2012-01-17 18:25:50 +0100208 /* FIXME: handling for ENVELOPE splitting */
209 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200210 case APDU_CASE_4E:
Harald Welted54c2ee2012-01-17 18:25:50 +0100211 break;
212 }
213
214 msgb_free(tmsg);
215
216 /* compute total length of response data */
217 msgb_apdu_le(amsg) = amsg->tail - msgb_apdu_de(amsg);
218
219 return sw;
220}
221
Harald Welte55790aa2014-10-26 18:46:50 +0100222/* FIXME: T=1 According to ISO7816-4 Annex B */
Harald Welted54c2ee2012-01-17 18:25:50 +0100223
224int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg)
225{
Harald Welte55790aa2014-10-26 18:46:50 +0100226 switch (st->card->proto) {
227 case OSIM_PROTO_T0:
228 return transceive_apdu_t0(st->card, amsg);
229 default:
230 return -ENOTSUP;
231 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100232}
233
Harald Welte55790aa2014-10-26 18:46:50 +0100234struct osim_reader_hdl *osim_reader_open(enum osim_reader_driver driver, int idx,
235 const char *name, void *ctx)
Harald Welted54c2ee2012-01-17 18:25:50 +0100236{
Harald Welte55790aa2014-10-26 18:46:50 +0100237 const struct osim_reader_ops *ops;
Harald Welted54c2ee2012-01-17 18:25:50 +0100238 struct osim_reader_hdl *rh;
239
Harald Welte55790aa2014-10-26 18:46:50 +0100240 switch (driver) {
Harald Welte3c44a642020-03-15 22:50:06 +0100241#ifdef HAVE_PCSC
Harald Welte55790aa2014-10-26 18:46:50 +0100242 case OSIM_READER_DRV_PCSC:
243 ops = &pcsc_reader_ops;
244 break;
Harald Welte3c44a642020-03-15 22:50:06 +0100245#endif
Harald Welte55790aa2014-10-26 18:46:50 +0100246 default:
247 return NULL;
248 }
249
Harald Welted83d2962013-03-04 17:52:33 +0000250 rh = ops->reader_open(idx, name, ctx);
Harald Welted54c2ee2012-01-17 18:25:50 +0100251 if (!rh)
252 return NULL;
253 rh->ops = ops;
254
Harald Welte55790aa2014-10-26 18:46:50 +0100255 /* FIXME: for now we only do T=0 on all readers */
256 rh->proto_supported = (1 << OSIM_PROTO_T0);
257
Harald Welted54c2ee2012-01-17 18:25:50 +0100258 return rh;
259}
260
Harald Welte55790aa2014-10-26 18:46:50 +0100261struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh, enum osim_proto proto)
Harald Welted54c2ee2012-01-17 18:25:50 +0100262{
Harald Welte55790aa2014-10-26 18:46:50 +0100263 struct osim_card_hdl *ch;
264
265 if (!(rh->proto_supported & (1 << proto)))
266 return NULL;
267
268 ch = rh->ops->card_open(rh, proto);
269 if (!ch)
270 return NULL;
271
272 ch->proto = proto;
273
274 return ch;
Harald Welted54c2ee2012-01-17 18:25:50 +0100275}
Harald Welte20199da2021-06-01 20:11:19 +0200276
277int osim_card_reset(struct osim_card_hdl *card, bool cold_reset)
278{
279 struct osim_reader_hdl *rh = card->reader;
280
281 return rh->ops->card_reset(card, cold_reset);
282}
283
284int osim_card_close(struct osim_card_hdl *card)
285{
286 struct osim_reader_hdl *rh = card->reader;
287 int rc;
288
289 rc = rh->ops->card_close(card);
290
291 card->reader = NULL;
292 talloc_free(card);
293 rh->card = NULL;
294
295 return rc;
296}