blob: e7169b5adb7c2c853d13d53b0239067e3e3009ab [file] [log] [blame]
Harald Weltead418632012-09-10 10:49:59 +02001/* Card reader abstraction for libosmosim */
2/*
3 * (C) 2012 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23
Harald Welted54c2ee2012-01-17 18:25:50 +010024#include <errno.h>
25#include <stdint.h>
26#include <stdlib.h>
27#include <string.h>
28#include <stdio.h>
29
Harald Weltea5c92552012-09-10 21:05:42 +020030#include <netinet/in.h>
31
32#include <osmocom/core/msgb.h>
Harald Welted54c2ee2012-01-17 18:25:50 +010033#include <osmocom/sim/sim.h>
34
35
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
43 if (!msgb_apdu_de(resp) || msgb_apdu_le(resp) < 2)
44 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);
122 printf("sw = 0x%04x\n", sw);
123 msgb_apdu_sw(amsg) = sw;
124
125 switch (msgb_apdu_case(amsg)) {
126 case APDU_CASE_1:
Kevin Redone07967f2012-09-11 11:44:18 +0200127 case APDU_CASE_3S:
Harald Welted54c2ee2012-01-17 18:25:50 +0100128 /* just copy SW */
129 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200130 case APDU_CASE_2S:
Harald Welted54c2ee2012-01-17 18:25:50 +0100131case_2s:
132 switch (sw >> 8) {
133 case 0x67: /* Case 2S.2: Le definitely not accepted */
134 break;
135 case 0x6c: /* Case 2S.3: Le not accepted, La indicated */
136 tpduh->p3 = sw & 0xff;
137 /* re-issue the command with La as */
138 goto transceive_again;
139 break;
140 case 0x90:
141 /* Case 2S.1, fall-through */
142 case 0x91: case 0x92: case 0x93: case 0x94: case 0x95:
143 case 0x96: case 0x97: case 0x98: case 0x99: case 0x9a:
144 case 0x9b: case 0x9c: case 0x9d: case 0x9e: case 0x9f:
145 /* Case 2S.4 */
146 /* copy response data over */
147 cur = msgb_put(amsg, msgb_l3len(tmsg));
148 memcpy(cur, tmsg->l3h, msgb_l3len(tmsg));
149 }
150 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200151 case APDU_CASE_4S:
Harald Welted54c2ee2012-01-17 18:25:50 +0100152 /* FIXME: this is 4S.2 only for 2nd... response: */
153 if (num_resp >= 2)
154 goto case_2s;
155
156 switch (sw >> 8) {
157 case 0x60: case 0x62: case 0x63: case 0x64: case 0x65:
158 case 0x66: case 0x67: case 0x68: case 0x69: case 0x6a:
159 case 0x6b: case 0x6c: case 0x6d: case 0x6e: case 0x6f:
160 /* Case 4S.1: Command not accepted: just copy SW */
161 break;
162 case 0x90:
163 /* case 4S.2: Command accepted */
164 tpduh->ins = 0xC0;
165 tpduh->p1 = tpduh->p2 = 0;
166 tpduh->p3 = msgb_apdu_le(amsg);
167 /* strip off current result */
168 msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh));
169 goto transceive_again;
170 break;
171 case 0x61: /* Case 4S.3: command accepted with info added */
Harald Welted83d2962013-03-04 17:52:33 +0000172 case 0x9F: /* FIXME: This is specific to SIM cards */
Harald Welted54c2ee2012-01-17 18:25:50 +0100173 tpduh->ins = 0xC0;
174 tpduh->p1 = tpduh->p2 = 0;
175 tpduh->p3 = OSMO_MIN(msgb_apdu_le(amsg), sw & 0xff);
176 /* strip off current result */
177 msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh));
178 goto transceive_again;
179 break;
180 }
181 /* Case 4S.2: Command accepted: just copy SW */
182 /* Case 4S.4: Just copy SW */
183 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200184 case APDU_CASE_2E:
Harald Welted54c2ee2012-01-17 18:25:50 +0100185 if (msgb_apdu_le(amsg) <= 256) {
186 /* Case 2E.1: Le <= 256 */
187 goto case_2s;
188 }
189 switch (sw >> 8) {
190 case 0x67:
191 /* Case 2E.2a: wrong length, abort */
192 break;
193 case 0x6c:
194 /* Case 2E.2b: wrong length, La given */
195 tpduh->p3 = sw & 0xff;
196 /* re-issue the command with La as given */
197 goto transceive_again;
198 break;
199 case 0x90:
200 /* Case 2E.2c: */
201 break;
202 case 0x61:
203 /* Case 2E.2d: more data available */
204 /* FIXME: issue yet another GET RESPONSE */
205 break;
206 }
207 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200208 case APDU_CASE_3E:
Harald Welted54c2ee2012-01-17 18:25:50 +0100209 /* FIXME: handling for ENVELOPE splitting */
210 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200211 case APDU_CASE_4E:
Harald Welted54c2ee2012-01-17 18:25:50 +0100212 break;
213 }
214
215 msgb_free(tmsg);
216
217 /* compute total length of response data */
218 msgb_apdu_le(amsg) = amsg->tail - msgb_apdu_de(amsg);
219
220 return sw;
221}
222
Harald Welte55790aa2014-10-26 18:46:50 +0100223/* FIXME: T=1 According to ISO7816-4 Annex B */
Harald Welted54c2ee2012-01-17 18:25:50 +0100224
225int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg)
226{
Harald Welte55790aa2014-10-26 18:46:50 +0100227 switch (st->card->proto) {
228 case OSIM_PROTO_T0:
229 return transceive_apdu_t0(st->card, amsg);
230 default:
231 return -ENOTSUP;
232 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100233}
234
Harald Welte55790aa2014-10-26 18:46:50 +0100235struct osim_reader_hdl *osim_reader_open(enum osim_reader_driver driver, int idx,
236 const char *name, void *ctx)
Harald Welted54c2ee2012-01-17 18:25:50 +0100237{
Harald Welte55790aa2014-10-26 18:46:50 +0100238 const struct osim_reader_ops *ops;
Harald Welted54c2ee2012-01-17 18:25:50 +0100239 struct osim_reader_hdl *rh;
240
Harald Welte55790aa2014-10-26 18:46:50 +0100241 switch (driver) {
242 case OSIM_READER_DRV_PCSC:
243 ops = &pcsc_reader_ops;
244 break;
245 default:
246 return NULL;
247 }
248
Harald Welted83d2962013-03-04 17:52:33 +0000249 rh = ops->reader_open(idx, name, ctx);
Harald Welted54c2ee2012-01-17 18:25:50 +0100250 if (!rh)
251 return NULL;
252 rh->ops = ops;
253
Harald Welte55790aa2014-10-26 18:46:50 +0100254 /* FIXME: for now we only do T=0 on all readers */
255 rh->proto_supported = (1 << OSIM_PROTO_T0);
256
Harald Welted54c2ee2012-01-17 18:25:50 +0100257 return rh;
258}
259
Harald Welte55790aa2014-10-26 18:46:50 +0100260struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh, enum osim_proto proto)
Harald Welted54c2ee2012-01-17 18:25:50 +0100261{
Harald Welte55790aa2014-10-26 18:46:50 +0100262 struct osim_card_hdl *ch;
263
264 if (!(rh->proto_supported & (1 << proto)))
265 return NULL;
266
267 ch = rh->ops->card_open(rh, proto);
268 if (!ch)
269 return NULL;
270
271 ch->proto = proto;
272
273 return ch;
Harald Welted54c2ee2012-01-17 18:25:50 +0100274}