blob: 7f3f18d951500934113516012f6fbd6b7c4c821b [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);
Harald Welted54c2ee2012-01-17 18:25:50 +0100126 msgb_apdu_sw(amsg) = sw;
127
128 switch (msgb_apdu_case(amsg)) {
129 case APDU_CASE_1:
Kevin Redone07967f2012-09-11 11:44:18 +0200130 case APDU_CASE_3S:
Harald Welted54c2ee2012-01-17 18:25:50 +0100131 /* just copy SW */
132 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200133 case APDU_CASE_2S:
Harald Welted54c2ee2012-01-17 18:25:50 +0100134case_2s:
135 switch (sw >> 8) {
136 case 0x67: /* Case 2S.2: Le definitely not accepted */
137 break;
138 case 0x6c: /* Case 2S.3: Le not accepted, La indicated */
139 tpduh->p3 = sw & 0xff;
140 /* re-issue the command with La as */
141 goto transceive_again;
142 break;
143 case 0x90:
144 /* Case 2S.1, fall-through */
145 case 0x91: case 0x92: case 0x93: case 0x94: case 0x95:
146 case 0x96: case 0x97: case 0x98: case 0x99: case 0x9a:
147 case 0x9b: case 0x9c: case 0x9d: case 0x9e: case 0x9f:
148 /* Case 2S.4 */
149 /* copy response data over */
150 cur = msgb_put(amsg, msgb_l3len(tmsg));
151 memcpy(cur, tmsg->l3h, msgb_l3len(tmsg));
152 }
153 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200154 case APDU_CASE_4S:
Harald Welted54c2ee2012-01-17 18:25:50 +0100155 /* FIXME: this is 4S.2 only for 2nd... response: */
156 if (num_resp >= 2)
157 goto case_2s;
158
159 switch (sw >> 8) {
160 case 0x60: case 0x62: case 0x63: case 0x64: case 0x65:
161 case 0x66: case 0x67: case 0x68: case 0x69: case 0x6a:
162 case 0x6b: case 0x6c: case 0x6d: case 0x6e: case 0x6f:
163 /* Case 4S.1: Command not accepted: just copy SW */
164 break;
165 case 0x90:
166 /* case 4S.2: Command accepted */
167 tpduh->ins = 0xC0;
168 tpduh->p1 = tpduh->p2 = 0;
169 tpduh->p3 = msgb_apdu_le(amsg);
170 /* strip off current result */
171 msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh));
172 goto transceive_again;
173 break;
174 case 0x61: /* Case 4S.3: command accepted with info added */
Harald Welted83d2962013-03-04 17:52:33 +0000175 case 0x9F: /* FIXME: This is specific to SIM cards */
Harald Welted54c2ee2012-01-17 18:25:50 +0100176 tpduh->ins = 0xC0;
177 tpduh->p1 = tpduh->p2 = 0;
178 tpduh->p3 = OSMO_MIN(msgb_apdu_le(amsg), sw & 0xff);
179 /* strip off current result */
180 msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh));
181 goto transceive_again;
182 break;
183 }
184 /* Case 4S.2: Command accepted: just copy SW */
185 /* Case 4S.4: Just copy SW */
186 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200187 case APDU_CASE_2E:
Harald Welted54c2ee2012-01-17 18:25:50 +0100188 if (msgb_apdu_le(amsg) <= 256) {
189 /* Case 2E.1: Le <= 256 */
190 goto case_2s;
191 }
192 switch (sw >> 8) {
193 case 0x67:
194 /* Case 2E.2a: wrong length, abort */
195 break;
196 case 0x6c:
197 /* Case 2E.2b: wrong length, La given */
198 tpduh->p3 = sw & 0xff;
199 /* re-issue the command with La as given */
200 goto transceive_again;
201 break;
202 case 0x90:
203 /* Case 2E.2c: */
204 break;
205 case 0x61:
206 /* Case 2E.2d: more data available */
207 /* FIXME: issue yet another GET RESPONSE */
208 break;
209 }
210 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200211 case APDU_CASE_3E:
Harald Welted54c2ee2012-01-17 18:25:50 +0100212 /* FIXME: handling for ENVELOPE splitting */
213 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200214 case APDU_CASE_4E:
Harald Welted54c2ee2012-01-17 18:25:50 +0100215 break;
216 }
217
218 msgb_free(tmsg);
219
220 /* compute total length of response data */
221 msgb_apdu_le(amsg) = amsg->tail - msgb_apdu_de(amsg);
222
223 return sw;
224}
225
Harald Welte55790aa2014-10-26 18:46:50 +0100226/* FIXME: T=1 According to ISO7816-4 Annex B */
Harald Welted54c2ee2012-01-17 18:25:50 +0100227
228int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg)
229{
Harald Welte55790aa2014-10-26 18:46:50 +0100230 switch (st->card->proto) {
231 case OSIM_PROTO_T0:
232 return transceive_apdu_t0(st->card, amsg);
233 default:
234 return -ENOTSUP;
235 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100236}
237
Harald Welte55790aa2014-10-26 18:46:50 +0100238struct osim_reader_hdl *osim_reader_open(enum osim_reader_driver driver, int idx,
239 const char *name, void *ctx)
Harald Welted54c2ee2012-01-17 18:25:50 +0100240{
Harald Welte55790aa2014-10-26 18:46:50 +0100241 const struct osim_reader_ops *ops;
Harald Welted54c2ee2012-01-17 18:25:50 +0100242 struct osim_reader_hdl *rh;
243
Harald Welte55790aa2014-10-26 18:46:50 +0100244 switch (driver) {
Harald Welte3c44a642020-03-15 22:50:06 +0100245#ifdef HAVE_PCSC
Harald Welte55790aa2014-10-26 18:46:50 +0100246 case OSIM_READER_DRV_PCSC:
247 ops = &pcsc_reader_ops;
248 break;
Harald Welte3c44a642020-03-15 22:50:06 +0100249#endif
Harald Welte55790aa2014-10-26 18:46:50 +0100250 default:
251 return NULL;
252 }
253
Harald Welted83d2962013-03-04 17:52:33 +0000254 rh = ops->reader_open(idx, name, ctx);
Harald Welted54c2ee2012-01-17 18:25:50 +0100255 if (!rh)
256 return NULL;
257 rh->ops = ops;
258
Harald Welte55790aa2014-10-26 18:46:50 +0100259 /* FIXME: for now we only do T=0 on all readers */
260 rh->proto_supported = (1 << OSIM_PROTO_T0);
261
Harald Welted54c2ee2012-01-17 18:25:50 +0100262 return rh;
263}
264
Harald Welte55790aa2014-10-26 18:46:50 +0100265struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh, enum osim_proto proto)
Harald Welted54c2ee2012-01-17 18:25:50 +0100266{
Harald Welte55790aa2014-10-26 18:46:50 +0100267 struct osim_card_hdl *ch;
268
269 if (!(rh->proto_supported & (1 << proto)))
270 return NULL;
271
272 ch = rh->ops->card_open(rh, proto);
273 if (!ch)
274 return NULL;
275
276 ch->proto = proto;
277
278 return ch;
Harald Welted54c2ee2012-01-17 18:25:50 +0100279}
Harald Welte20199da2021-06-01 20:11:19 +0200280
281int osim_card_reset(struct osim_card_hdl *card, bool cold_reset)
282{
283 struct osim_reader_hdl *rh = card->reader;
284
285 return rh->ops->card_reset(card, cold_reset);
286}
287
288int osim_card_close(struct osim_card_hdl *card)
289{
290 struct osim_reader_hdl *rh = card->reader;
291 int rc;
292
293 rc = rh->ops->card_close(card);
294
295 card->reader = NULL;
296 talloc_free(card);
297 rh->card = NULL;
298
299 return rc;
300}