blob: d1a9ae60bf691c6713d5d46a4d1e466a986aff7d [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
38
39#include "sim_int.h"
40
Harald Weltea5c92552012-09-10 21:05:42 +020041/* remove the SW from end of the message */
Harald Welted54c2ee2012-01-17 18:25:50 +010042static int get_sw(struct msgb *resp)
43{
44 int ret;
45
46 if (!msgb_apdu_de(resp) || msgb_apdu_le(resp) < 2)
47 return -EIO;
48
Harald Weltea5c92552012-09-10 21:05:42 +020049 ret = msgb_get_u16(resp);
Harald Welted54c2ee2012-01-17 18:25:50 +010050
51 return ret;
52}
53
54/* According to ISO7816-4 Annex A */
55static int transceive_apdu_t0(struct osim_card_hdl *st, struct msgb *amsg)
56{
57 struct osim_reader_hdl *rh = st->reader;
58 struct msgb *tmsg = msgb_alloc(1024, "TPDU");
59 struct osim_apdu_cmd_hdr *tpduh;
60 uint8_t *cur;
61 uint16_t sw;
62 int rc, num_resp = 0;
63
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +020064 if (!tmsg)
65 return -ENOMEM;
66
Harald Welted54c2ee2012-01-17 18:25:50 +010067 /* create TPDU header from APDU header */
68 tpduh = (struct osim_apdu_cmd_hdr *) msgb_put(tmsg, sizeof(*tpduh));
69 memcpy(tpduh, msgb_apdu_h(amsg), sizeof(*tpduh));
70
71 switch (msgb_apdu_case(amsg)) {
72 case APDU_CASE_1:
73 tpduh->p3 = 0x00;
74 break;
Kevin Redone07967f2012-09-11 11:44:18 +020075 case APDU_CASE_2S:
Harald Welted54c2ee2012-01-17 18:25:50 +010076 tpduh->p3 = msgb_apdu_le(amsg);
77 break;
Kevin Redone07967f2012-09-11 11:44:18 +020078 case APDU_CASE_2E:
Harald Welted54c2ee2012-01-17 18:25:50 +010079 if (msgb_apdu_le(amsg) <= 256) {
80 /* case 2E.1 */
81 tpduh->p3 = msgb_apdu_le(amsg) & 0xff;
82 } else {
83 /* case 2E.2 */
84 tpduh->p3 = 0;
85 msgb_put_u16(tmsg, msgb_apdu_le(amsg));
86 }
87 break;
Kevin Redone07967f2012-09-11 11:44:18 +020088 case APDU_CASE_3S:
89 case APDU_CASE_4S:
Harald Welted54c2ee2012-01-17 18:25:50 +010090 tpduh->p3 = msgb_apdu_lc(amsg);
91 cur = msgb_put(tmsg, tpduh->p3);
92 memcpy(cur, msgb_apdu_dc(amsg), tpduh->p3);
93 break;
Kevin Redone07967f2012-09-11 11:44:18 +020094 case APDU_CASE_3E:
95 case APDU_CASE_4E:
Harald Welted54c2ee2012-01-17 18:25:50 +010096 if (msgb_apdu_lc(amsg) < 256) {
97 /* Case 3E.1 */
98 tpduh->p3 = msgb_apdu_lc(amsg);
99 } else {
100 /* Case 3E.2 */
101 /* FXIME: Split using ENVELOPE! */
102 return -1;
103 }
104 break;
105 }
106
107transceive_again:
108
109 /* store pointer to start of response */
110 tmsg->l3h = tmsg->tail;
111
112 /* transceive */
113 rc = rh->ops->transceive(st->reader, tmsg);
114 if (rc < 0) {
115 msgb_free(tmsg);
116 return rc;
117 }
118 msgb_apdu_sw(tmsg) = get_sw(tmsg);
119
120 /* increase number of responsese received */
121 num_resp++;
122
123 /* save SW */
124 sw = msgb_apdu_sw(tmsg);
125 printf("sw = 0x%04x\n", sw);
126 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) {
245 case OSIM_READER_DRV_PCSC:
246 ops = &pcsc_reader_ops;
247 break;
248 default:
249 return NULL;
250 }
251
Harald Welted83d2962013-03-04 17:52:33 +0000252 rh = ops->reader_open(idx, name, ctx);
Harald Welted54c2ee2012-01-17 18:25:50 +0100253 if (!rh)
254 return NULL;
255 rh->ops = ops;
256
Harald Welte55790aa2014-10-26 18:46:50 +0100257 /* FIXME: for now we only do T=0 on all readers */
258 rh->proto_supported = (1 << OSIM_PROTO_T0);
259
Harald Welted54c2ee2012-01-17 18:25:50 +0100260 return rh;
261}
262
Harald Welte55790aa2014-10-26 18:46:50 +0100263struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh, enum osim_proto proto)
Harald Welted54c2ee2012-01-17 18:25:50 +0100264{
Harald Welte55790aa2014-10-26 18:46:50 +0100265 struct osim_card_hdl *ch;
266
267 if (!(rh->proto_supported & (1 << proto)))
268 return NULL;
269
270 ch = rh->ops->card_open(rh, proto);
271 if (!ch)
272 return NULL;
273
274 ch->proto = proto;
275
276 return ch;
Harald Welted54c2ee2012-01-17 18:25:50 +0100277}