blob: 71fb763d92ce8f7991654cad99a378ef5f055a74 [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
30#include <osmocom/sim/sim.h>
31
32
33#include "sim_int.h"
34
35static int get_sw(struct msgb *resp)
36{
37 int ret;
38
39 if (!msgb_apdu_de(resp) || msgb_apdu_le(resp) < 2)
40 return -EIO;
41
42 ret = resp->data[resp->len-2] << 8;
43 ret |= resp->data[resp->len-1];
44
45 return ret;
46}
47
48/* According to ISO7816-4 Annex A */
49static int transceive_apdu_t0(struct osim_card_hdl *st, struct msgb *amsg)
50{
51 struct osim_reader_hdl *rh = st->reader;
52 struct msgb *tmsg = msgb_alloc(1024, "TPDU");
53 struct osim_apdu_cmd_hdr *tpduh;
54 uint8_t *cur;
55 uint16_t sw;
56 int rc, num_resp = 0;
57
58 /* create TPDU header from APDU header */
59 tpduh = (struct osim_apdu_cmd_hdr *) msgb_put(tmsg, sizeof(*tpduh));
60 memcpy(tpduh, msgb_apdu_h(amsg), sizeof(*tpduh));
61
62 switch (msgb_apdu_case(amsg)) {
63 case APDU_CASE_1:
64 tpduh->p3 = 0x00;
65 break;
66 case APDU_CASE_2:
67 tpduh->p3 = msgb_apdu_le(amsg);
68 break;
69 case APDU_CASE_2_EXT:
70 if (msgb_apdu_le(amsg) <= 256) {
71 /* case 2E.1 */
72 tpduh->p3 = msgb_apdu_le(amsg) & 0xff;
73 } else {
74 /* case 2E.2 */
75 tpduh->p3 = 0;
76 msgb_put_u16(tmsg, msgb_apdu_le(amsg));
77 }
78 break;
79 case APDU_CASE_3:
80 case APDU_CASE_4:
81 tpduh->p3 = msgb_apdu_lc(amsg);
82 cur = msgb_put(tmsg, tpduh->p3);
83 memcpy(cur, msgb_apdu_dc(amsg), tpduh->p3);
84 break;
85 case APDU_CASE_3_EXT:
86 case APDU_CASE_4_EXT:
87 if (msgb_apdu_lc(amsg) < 256) {
88 /* Case 3E.1 */
89 tpduh->p3 = msgb_apdu_lc(amsg);
90 } else {
91 /* Case 3E.2 */
92 /* FXIME: Split using ENVELOPE! */
93 return -1;
94 }
95 break;
96 }
97
98transceive_again:
99
100 /* store pointer to start of response */
101 tmsg->l3h = tmsg->tail;
102
103 /* transceive */
104 rc = rh->ops->transceive(st->reader, tmsg);
105 if (rc < 0) {
106 msgb_free(tmsg);
107 return rc;
108 }
109 msgb_apdu_sw(tmsg) = get_sw(tmsg);
110
111 /* increase number of responsese received */
112 num_resp++;
113
114 /* save SW */
115 sw = msgb_apdu_sw(tmsg);
116 printf("sw = 0x%04x\n", sw);
117 msgb_apdu_sw(amsg) = sw;
118
119 switch (msgb_apdu_case(amsg)) {
120 case APDU_CASE_1:
121 case APDU_CASE_3:
122 /* just copy SW */
123 break;
124 case APDU_CASE_2:
125case_2s:
126 switch (sw >> 8) {
127 case 0x67: /* Case 2S.2: Le definitely not accepted */
128 break;
129 case 0x6c: /* Case 2S.3: Le not accepted, La indicated */
130 tpduh->p3 = sw & 0xff;
131 /* re-issue the command with La as */
132 goto transceive_again;
133 break;
134 case 0x90:
135 /* Case 2S.1, fall-through */
136 case 0x91: case 0x92: case 0x93: case 0x94: case 0x95:
137 case 0x96: case 0x97: case 0x98: case 0x99: case 0x9a:
138 case 0x9b: case 0x9c: case 0x9d: case 0x9e: case 0x9f:
139 /* Case 2S.4 */
140 /* copy response data over */
141 cur = msgb_put(amsg, msgb_l3len(tmsg));
142 memcpy(cur, tmsg->l3h, msgb_l3len(tmsg));
143 }
144 break;
145 case APDU_CASE_4:
146 /* FIXME: this is 4S.2 only for 2nd... response: */
147 if (num_resp >= 2)
148 goto case_2s;
149
150 switch (sw >> 8) {
151 case 0x60: case 0x62: case 0x63: case 0x64: case 0x65:
152 case 0x66: case 0x67: case 0x68: case 0x69: case 0x6a:
153 case 0x6b: case 0x6c: case 0x6d: case 0x6e: case 0x6f:
154 /* Case 4S.1: Command not accepted: just copy SW */
155 break;
156 case 0x90:
157 /* case 4S.2: Command accepted */
158 tpduh->ins = 0xC0;
159 tpduh->p1 = tpduh->p2 = 0;
160 tpduh->p3 = msgb_apdu_le(amsg);
161 /* strip off current result */
162 msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh));
163 goto transceive_again;
164 break;
165 case 0x61: /* Case 4S.3: command accepted with info added */
166 tpduh->ins = 0xC0;
167 tpduh->p1 = tpduh->p2 = 0;
168 tpduh->p3 = OSMO_MIN(msgb_apdu_le(amsg), sw & 0xff);
169 /* strip off current result */
170 msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh));
171 goto transceive_again;
172 break;
173 }
174 /* Case 4S.2: Command accepted: just copy SW */
175 /* Case 4S.4: Just copy SW */
176 break;
177 case APDU_CASE_2_EXT:
178 if (msgb_apdu_le(amsg) <= 256) {
179 /* Case 2E.1: Le <= 256 */
180 goto case_2s;
181 }
182 switch (sw >> 8) {
183 case 0x67:
184 /* Case 2E.2a: wrong length, abort */
185 break;
186 case 0x6c:
187 /* Case 2E.2b: wrong length, La given */
188 tpduh->p3 = sw & 0xff;
189 /* re-issue the command with La as given */
190 goto transceive_again;
191 break;
192 case 0x90:
193 /* Case 2E.2c: */
194 break;
195 case 0x61:
196 /* Case 2E.2d: more data available */
197 /* FIXME: issue yet another GET RESPONSE */
198 break;
199 }
200 break;
201 case APDU_CASE_3_EXT:
202 /* FIXME: handling for ENVELOPE splitting */
203 break;
204 case APDU_CASE_4_EXT:
205 break;
206 }
207
208 msgb_free(tmsg);
209
210 /* compute total length of response data */
211 msgb_apdu_le(amsg) = amsg->tail - msgb_apdu_de(amsg);
212
213 return sw;
214}
215
216/* According to ISO7816-4 Annex B */
217static int transceive_apdu_t1(struct osim_card_hdl *st, struct msgb *amsg)
218{
219 return -1;
220}
221
222int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg)
223{
224 /* FIXME: check for protocol */
225 return transceive_apdu_t0(st->card, amsg);
226}
227
228
229
230struct osim_reader_hdl *osim_reader_open(int idx, const char *name)
231{
232 /* FIXME: support multiple drivers */
233 const struct osim_reader_ops *ops = &pcsc_reader_ops;
234 struct osim_reader_hdl *rh;
235
236 rh = ops->reader_open(idx, name);
237 if (!rh)
238 return NULL;
239 rh->ops = ops;
240
241 return rh;
242}
243
244struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh)
245{
246 return rh->ops->card_open(rh);
247}