blob: 160f1758a3c7ccfe06235669a728f21a91eec9c9 [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
61 /* create TPDU header from APDU header */
62 tpduh = (struct osim_apdu_cmd_hdr *) msgb_put(tmsg, sizeof(*tpduh));
63 memcpy(tpduh, msgb_apdu_h(amsg), sizeof(*tpduh));
64
65 switch (msgb_apdu_case(amsg)) {
66 case APDU_CASE_1:
67 tpduh->p3 = 0x00;
68 break;
Kevin Redone07967f2012-09-11 11:44:18 +020069 case APDU_CASE_2S:
Harald Welted54c2ee2012-01-17 18:25:50 +010070 tpduh->p3 = msgb_apdu_le(amsg);
71 break;
Kevin Redone07967f2012-09-11 11:44:18 +020072 case APDU_CASE_2E:
Harald Welted54c2ee2012-01-17 18:25:50 +010073 if (msgb_apdu_le(amsg) <= 256) {
74 /* case 2E.1 */
75 tpduh->p3 = msgb_apdu_le(amsg) & 0xff;
76 } else {
77 /* case 2E.2 */
78 tpduh->p3 = 0;
79 msgb_put_u16(tmsg, msgb_apdu_le(amsg));
80 }
81 break;
Kevin Redone07967f2012-09-11 11:44:18 +020082 case APDU_CASE_3S:
83 case APDU_CASE_4S:
Harald Welted54c2ee2012-01-17 18:25:50 +010084 tpduh->p3 = msgb_apdu_lc(amsg);
85 cur = msgb_put(tmsg, tpduh->p3);
86 memcpy(cur, msgb_apdu_dc(amsg), tpduh->p3);
87 break;
Kevin Redone07967f2012-09-11 11:44:18 +020088 case APDU_CASE_3E:
89 case APDU_CASE_4E:
Harald Welted54c2ee2012-01-17 18:25:50 +010090 if (msgb_apdu_lc(amsg) < 256) {
91 /* Case 3E.1 */
92 tpduh->p3 = msgb_apdu_lc(amsg);
93 } else {
94 /* Case 3E.2 */
95 /* FXIME: Split using ENVELOPE! */
96 return -1;
97 }
98 break;
99 }
100
101transceive_again:
102
103 /* store pointer to start of response */
104 tmsg->l3h = tmsg->tail;
105
106 /* transceive */
107 rc = rh->ops->transceive(st->reader, tmsg);
108 if (rc < 0) {
109 msgb_free(tmsg);
110 return rc;
111 }
112 msgb_apdu_sw(tmsg) = get_sw(tmsg);
113
114 /* increase number of responsese received */
115 num_resp++;
116
117 /* save SW */
118 sw = msgb_apdu_sw(tmsg);
119 printf("sw = 0x%04x\n", sw);
120 msgb_apdu_sw(amsg) = sw;
121
122 switch (msgb_apdu_case(amsg)) {
123 case APDU_CASE_1:
Kevin Redone07967f2012-09-11 11:44:18 +0200124 case APDU_CASE_3S:
Harald Welted54c2ee2012-01-17 18:25:50 +0100125 /* just copy SW */
126 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200127 case APDU_CASE_2S:
Harald Welted54c2ee2012-01-17 18:25:50 +0100128case_2s:
129 switch (sw >> 8) {
130 case 0x67: /* Case 2S.2: Le definitely not accepted */
131 break;
132 case 0x6c: /* Case 2S.3: Le not accepted, La indicated */
133 tpduh->p3 = sw & 0xff;
134 /* re-issue the command with La as */
135 goto transceive_again;
136 break;
137 case 0x90:
138 /* Case 2S.1, fall-through */
139 case 0x91: case 0x92: case 0x93: case 0x94: case 0x95:
140 case 0x96: case 0x97: case 0x98: case 0x99: case 0x9a:
141 case 0x9b: case 0x9c: case 0x9d: case 0x9e: case 0x9f:
142 /* Case 2S.4 */
143 /* copy response data over */
144 cur = msgb_put(amsg, msgb_l3len(tmsg));
145 memcpy(cur, tmsg->l3h, msgb_l3len(tmsg));
146 }
147 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200148 case APDU_CASE_4S:
Harald Welted54c2ee2012-01-17 18:25:50 +0100149 /* FIXME: this is 4S.2 only for 2nd... response: */
150 if (num_resp >= 2)
151 goto case_2s;
152
153 switch (sw >> 8) {
154 case 0x60: case 0x62: case 0x63: case 0x64: case 0x65:
155 case 0x66: case 0x67: case 0x68: case 0x69: case 0x6a:
156 case 0x6b: case 0x6c: case 0x6d: case 0x6e: case 0x6f:
157 /* Case 4S.1: Command not accepted: just copy SW */
158 break;
159 case 0x90:
160 /* case 4S.2: Command accepted */
161 tpduh->ins = 0xC0;
162 tpduh->p1 = tpduh->p2 = 0;
163 tpduh->p3 = msgb_apdu_le(amsg);
164 /* strip off current result */
165 msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh));
166 goto transceive_again;
167 break;
168 case 0x61: /* Case 4S.3: command accepted with info added */
Harald Welted83d2962013-03-04 17:52:33 +0000169 case 0x9F: /* FIXME: This is specific to SIM cards */
Harald Welted54c2ee2012-01-17 18:25:50 +0100170 tpduh->ins = 0xC0;
171 tpduh->p1 = tpduh->p2 = 0;
172 tpduh->p3 = OSMO_MIN(msgb_apdu_le(amsg), sw & 0xff);
173 /* strip off current result */
174 msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh));
175 goto transceive_again;
176 break;
177 }
178 /* Case 4S.2: Command accepted: just copy SW */
179 /* Case 4S.4: Just copy SW */
180 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200181 case APDU_CASE_2E:
Harald Welted54c2ee2012-01-17 18:25:50 +0100182 if (msgb_apdu_le(amsg) <= 256) {
183 /* Case 2E.1: Le <= 256 */
184 goto case_2s;
185 }
186 switch (sw >> 8) {
187 case 0x67:
188 /* Case 2E.2a: wrong length, abort */
189 break;
190 case 0x6c:
191 /* Case 2E.2b: wrong length, La given */
192 tpduh->p3 = sw & 0xff;
193 /* re-issue the command with La as given */
194 goto transceive_again;
195 break;
196 case 0x90:
197 /* Case 2E.2c: */
198 break;
199 case 0x61:
200 /* Case 2E.2d: more data available */
201 /* FIXME: issue yet another GET RESPONSE */
202 break;
203 }
204 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200205 case APDU_CASE_3E:
Harald Welted54c2ee2012-01-17 18:25:50 +0100206 /* FIXME: handling for ENVELOPE splitting */
207 break;
Kevin Redone07967f2012-09-11 11:44:18 +0200208 case APDU_CASE_4E:
Harald Welted54c2ee2012-01-17 18:25:50 +0100209 break;
210 }
211
212 msgb_free(tmsg);
213
214 /* compute total length of response data */
215 msgb_apdu_le(amsg) = amsg->tail - msgb_apdu_de(amsg);
216
217 return sw;
218}
219
Harald Welte55790aa2014-10-26 18:46:50 +0100220/* FIXME: T=1 According to ISO7816-4 Annex B */
Harald Welted54c2ee2012-01-17 18:25:50 +0100221
222int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg)
223{
Harald Welte55790aa2014-10-26 18:46:50 +0100224 switch (st->card->proto) {
225 case OSIM_PROTO_T0:
226 return transceive_apdu_t0(st->card, amsg);
227 default:
228 return -ENOTSUP;
229 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100230}
231
Harald Welte55790aa2014-10-26 18:46:50 +0100232struct osim_reader_hdl *osim_reader_open(enum osim_reader_driver driver, int idx,
233 const char *name, void *ctx)
Harald Welted54c2ee2012-01-17 18:25:50 +0100234{
Harald Welte55790aa2014-10-26 18:46:50 +0100235 const struct osim_reader_ops *ops;
Harald Welted54c2ee2012-01-17 18:25:50 +0100236 struct osim_reader_hdl *rh;
237
Harald Welte55790aa2014-10-26 18:46:50 +0100238 switch (driver) {
239 case OSIM_READER_DRV_PCSC:
240 ops = &pcsc_reader_ops;
241 break;
242 default:
243 return NULL;
244 }
245
Harald Welted83d2962013-03-04 17:52:33 +0000246 rh = ops->reader_open(idx, name, ctx);
Harald Welted54c2ee2012-01-17 18:25:50 +0100247 if (!rh)
248 return NULL;
249 rh->ops = ops;
250
Harald Welte55790aa2014-10-26 18:46:50 +0100251 /* FIXME: for now we only do T=0 on all readers */
252 rh->proto_supported = (1 << OSIM_PROTO_T0);
253
Harald Welted54c2ee2012-01-17 18:25:50 +0100254 return rh;
255}
256
Harald Welte55790aa2014-10-26 18:46:50 +0100257struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh, enum osim_proto proto)
Harald Welted54c2ee2012-01-17 18:25:50 +0100258{
Harald Welte55790aa2014-10-26 18:46:50 +0100259 struct osim_card_hdl *ch;
260
261 if (!(rh->proto_supported & (1 << proto)))
262 return NULL;
263
264 ch = rh->ops->card_open(rh, proto);
265 if (!ch)
266 return NULL;
267
268 ch->proto = proto;
269
270 return ch;
Harald Welted54c2ee2012-01-17 18:25:50 +0100271}