blob: f39829b4ea886e93877b67277059e683b2083b00 [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 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24
Harald Welted54c2ee2012-01-17 18:25:50 +010025#include <errno.h>
26#include <stdint.h>
27#include <stdlib.h>
28#include <string.h>
29#include <stdio.h>
30
Harald Weltea5c92552012-09-10 21:05:42 +020031#include <netinet/in.h>
32
33#include <osmocom/core/msgb.h>
Harald Welted54c2ee2012-01-17 18:25:50 +010034#include <osmocom/sim/sim.h>
35
36
37#include "sim_int.h"
38
Harald Weltea5c92552012-09-10 21:05:42 +020039/* remove the SW from end of the message */
Harald Welted54c2ee2012-01-17 18:25:50 +010040static int get_sw(struct msgb *resp)
41{
42 int ret;
43
44 if (!msgb_apdu_de(resp) || msgb_apdu_le(resp) < 2)
45 return -EIO;
46
Harald Weltea5c92552012-09-10 21:05:42 +020047 ret = msgb_get_u16(resp);
Harald Welted54c2ee2012-01-17 18:25:50 +010048
49 return ret;
50}
51
52/* According to ISO7816-4 Annex A */
53static int transceive_apdu_t0(struct osim_card_hdl *st, struct msgb *amsg)
54{
55 struct osim_reader_hdl *rh = st->reader;
56 struct msgb *tmsg = msgb_alloc(1024, "TPDU");
57 struct osim_apdu_cmd_hdr *tpduh;
58 uint8_t *cur;
59 uint16_t sw;
60 int rc, num_resp = 0;
61
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +020062 if (!tmsg)
63 return -ENOMEM;
64
Harald Welted54c2ee2012-01-17 18:25:50 +010065 /* create TPDU header from APDU header */
66 tpduh = (struct osim_apdu_cmd_hdr *) msgb_put(tmsg, sizeof(*tpduh));
67 memcpy(tpduh, msgb_apdu_h(amsg), sizeof(*tpduh));
68
69 switch (msgb_apdu_case(amsg)) {
70 case APDU_CASE_1:
71 tpduh->p3 = 0x00;
72 break;
Kevin Redone07967f2012-09-11 11:44:18 +020073 case APDU_CASE_2S:
Harald Welted54c2ee2012-01-17 18:25:50 +010074 tpduh->p3 = msgb_apdu_le(amsg);
75 break;
Kevin Redone07967f2012-09-11 11:44:18 +020076 case APDU_CASE_2E:
Harald Welted54c2ee2012-01-17 18:25:50 +010077 if (msgb_apdu_le(amsg) <= 256) {
78 /* case 2E.1 */
79 tpduh->p3 = msgb_apdu_le(amsg) & 0xff;
80 } else {
81 /* case 2E.2 */
82 tpduh->p3 = 0;
83 msgb_put_u16(tmsg, msgb_apdu_le(amsg));
84 }
85 break;
Kevin Redone07967f2012-09-11 11:44:18 +020086 case APDU_CASE_3S:
87 case APDU_CASE_4S:
Harald Welted54c2ee2012-01-17 18:25:50 +010088 tpduh->p3 = msgb_apdu_lc(amsg);
89 cur = msgb_put(tmsg, tpduh->p3);
90 memcpy(cur, msgb_apdu_dc(amsg), tpduh->p3);
91 break;
Kevin Redone07967f2012-09-11 11:44:18 +020092 case APDU_CASE_3E:
93 case APDU_CASE_4E:
Harald Welted54c2ee2012-01-17 18:25:50 +010094 if (msgb_apdu_lc(amsg) < 256) {
95 /* Case 3E.1 */
96 tpduh->p3 = msgb_apdu_lc(amsg);
97 } else {
98 /* Case 3E.2 */
99 /* FXIME: Split using ENVELOPE! */
100 return -1;
101 }
102 break;
103 }
104
105transceive_again:
106
107 /* store pointer to start of response */
108 tmsg->l3h = tmsg->tail;
109
110 /* transceive */
111 rc = rh->ops->transceive(st->reader, tmsg);
112 if (rc < 0) {
113 msgb_free(tmsg);
114 return rc;
115 }
116 msgb_apdu_sw(tmsg) = get_sw(tmsg);
117
118 /* increase number of responsese received */
119 num_resp++;
120
121 /* save SW */
122 sw = msgb_apdu_sw(tmsg);
123 printf("sw = 0x%04x\n", sw);
124 msgb_apdu_sw(amsg) = sw;
125
126 switch (msgb_apdu_case(amsg)) {
127 case APDU_CASE_1:
Kevin Redone07967f2012-09-11 11:44:18 +0200128 case APDU_CASE_3S:
Harald Welted54c2ee2012-01-17 18:25:50 +0100129 /* just copy SW */
130 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200131 case APDU_CASE_2S:
Harald Welted54c2ee2012-01-17 18:25:50 +0100132case_2s:
133 switch (sw >> 8) {
134 case 0x67: /* Case 2S.2: Le definitely not accepted */
135 break;
136 case 0x6c: /* Case 2S.3: Le not accepted, La indicated */
137 tpduh->p3 = sw & 0xff;
138 /* re-issue the command with La as */
139 goto transceive_again;
140 break;
141 case 0x90:
142 /* Case 2S.1, fall-through */
143 case 0x91: case 0x92: case 0x93: case 0x94: case 0x95:
144 case 0x96: case 0x97: case 0x98: case 0x99: case 0x9a:
145 case 0x9b: case 0x9c: case 0x9d: case 0x9e: case 0x9f:
146 /* Case 2S.4 */
147 /* copy response data over */
148 cur = msgb_put(amsg, msgb_l3len(tmsg));
149 memcpy(cur, tmsg->l3h, msgb_l3len(tmsg));
150 }
151 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200152 case APDU_CASE_4S:
Harald Welted54c2ee2012-01-17 18:25:50 +0100153 /* FIXME: this is 4S.2 only for 2nd... response: */
154 if (num_resp >= 2)
155 goto case_2s;
156
157 switch (sw >> 8) {
158 case 0x60: case 0x62: case 0x63: case 0x64: case 0x65:
159 case 0x66: case 0x67: case 0x68: case 0x69: case 0x6a:
160 case 0x6b: case 0x6c: case 0x6d: case 0x6e: case 0x6f:
161 /* Case 4S.1: Command not accepted: just copy SW */
162 break;
163 case 0x90:
164 /* case 4S.2: Command accepted */
165 tpduh->ins = 0xC0;
166 tpduh->p1 = tpduh->p2 = 0;
167 tpduh->p3 = msgb_apdu_le(amsg);
168 /* strip off current result */
169 msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh));
170 goto transceive_again;
171 break;
172 case 0x61: /* Case 4S.3: command accepted with info added */
Harald Welted83d2962013-03-04 17:52:33 +0000173 case 0x9F: /* FIXME: This is specific to SIM cards */
Harald Welted54c2ee2012-01-17 18:25:50 +0100174 tpduh->ins = 0xC0;
175 tpduh->p1 = tpduh->p2 = 0;
176 tpduh->p3 = OSMO_MIN(msgb_apdu_le(amsg), sw & 0xff);
177 /* strip off current result */
178 msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh));
179 goto transceive_again;
180 break;
181 }
182 /* Case 4S.2: Command accepted: just copy SW */
183 /* Case 4S.4: Just copy SW */
184 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200185 case APDU_CASE_2E:
Harald Welted54c2ee2012-01-17 18:25:50 +0100186 if (msgb_apdu_le(amsg) <= 256) {
187 /* Case 2E.1: Le <= 256 */
188 goto case_2s;
189 }
190 switch (sw >> 8) {
191 case 0x67:
192 /* Case 2E.2a: wrong length, abort */
193 break;
194 case 0x6c:
195 /* Case 2E.2b: wrong length, La given */
196 tpduh->p3 = sw & 0xff;
197 /* re-issue the command with La as given */
198 goto transceive_again;
199 break;
200 case 0x90:
201 /* Case 2E.2c: */
202 break;
203 case 0x61:
204 /* Case 2E.2d: more data available */
205 /* FIXME: issue yet another GET RESPONSE */
206 break;
207 }
208 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200209 case APDU_CASE_3E:
Harald Welted54c2ee2012-01-17 18:25:50 +0100210 /* FIXME: handling for ENVELOPE splitting */
211 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200212 case APDU_CASE_4E:
Harald Welted54c2ee2012-01-17 18:25:50 +0100213 break;
214 }
215
216 msgb_free(tmsg);
217
218 /* compute total length of response data */
219 msgb_apdu_le(amsg) = amsg->tail - msgb_apdu_de(amsg);
220
221 return sw;
222}
223
Harald Welte55790aa2014-10-26 18:46:50 +0100224/* FIXME: T=1 According to ISO7816-4 Annex B */
Harald Welted54c2ee2012-01-17 18:25:50 +0100225
226int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg)
227{
Harald Welte55790aa2014-10-26 18:46:50 +0100228 switch (st->card->proto) {
229 case OSIM_PROTO_T0:
230 return transceive_apdu_t0(st->card, amsg);
231 default:
232 return -ENOTSUP;
233 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100234}
235
Harald Welte55790aa2014-10-26 18:46:50 +0100236struct osim_reader_hdl *osim_reader_open(enum osim_reader_driver driver, int idx,
237 const char *name, void *ctx)
Harald Welted54c2ee2012-01-17 18:25:50 +0100238{
Harald Welte55790aa2014-10-26 18:46:50 +0100239 const struct osim_reader_ops *ops;
Harald Welted54c2ee2012-01-17 18:25:50 +0100240 struct osim_reader_hdl *rh;
241
Harald Welte55790aa2014-10-26 18:46:50 +0100242 switch (driver) {
243 case OSIM_READER_DRV_PCSC:
244 ops = &pcsc_reader_ops;
245 break;
246 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}