blob: 5d6033bfc6d62551097d8c285c2c527dd58f24e0 [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;
69 case APDU_CASE_2:
70 tpduh->p3 = msgb_apdu_le(amsg);
71 break;
72 case APDU_CASE_2_EXT:
73 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;
82 case APDU_CASE_3:
83 case APDU_CASE_4:
84 tpduh->p3 = msgb_apdu_lc(amsg);
85 cur = msgb_put(tmsg, tpduh->p3);
86 memcpy(cur, msgb_apdu_dc(amsg), tpduh->p3);
87 break;
88 case APDU_CASE_3_EXT:
89 case APDU_CASE_4_EXT:
90 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:
124 case APDU_CASE_3:
125 /* just copy SW */
126 break;
127 case APDU_CASE_2:
128case_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;
148 case APDU_CASE_4:
149 /* 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 */
169 tpduh->ins = 0xC0;
170 tpduh->p1 = tpduh->p2 = 0;
171 tpduh->p3 = OSMO_MIN(msgb_apdu_le(amsg), sw & 0xff);
172 /* strip off current result */
173 msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh));
174 goto transceive_again;
175 break;
176 }
177 /* Case 4S.2: Command accepted: just copy SW */
178 /* Case 4S.4: Just copy SW */
179 break;
180 case APDU_CASE_2_EXT:
181 if (msgb_apdu_le(amsg) <= 256) {
182 /* Case 2E.1: Le <= 256 */
183 goto case_2s;
184 }
185 switch (sw >> 8) {
186 case 0x67:
187 /* Case 2E.2a: wrong length, abort */
188 break;
189 case 0x6c:
190 /* Case 2E.2b: wrong length, La given */
191 tpduh->p3 = sw & 0xff;
192 /* re-issue the command with La as given */
193 goto transceive_again;
194 break;
195 case 0x90:
196 /* Case 2E.2c: */
197 break;
198 case 0x61:
199 /* Case 2E.2d: more data available */
200 /* FIXME: issue yet another GET RESPONSE */
201 break;
202 }
203 break;
204 case APDU_CASE_3_EXT:
205 /* FIXME: handling for ENVELOPE splitting */
206 break;
207 case APDU_CASE_4_EXT:
208 break;
209 }
210
211 msgb_free(tmsg);
212
213 /* compute total length of response data */
214 msgb_apdu_le(amsg) = amsg->tail - msgb_apdu_de(amsg);
215
216 return sw;
217}
218
219/* According to ISO7816-4 Annex B */
220static int transceive_apdu_t1(struct osim_card_hdl *st, struct msgb *amsg)
221{
222 return -1;
223}
224
225int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg)
226{
227 /* FIXME: check for protocol */
228 return transceive_apdu_t0(st->card, amsg);
229}
230
231
232
233struct osim_reader_hdl *osim_reader_open(int idx, const char *name)
234{
235 /* FIXME: support multiple drivers */
236 const struct osim_reader_ops *ops = &pcsc_reader_ops;
237 struct osim_reader_hdl *rh;
238
239 rh = ops->reader_open(idx, name);
240 if (!rh)
241 return NULL;
242 rh->ops = ops;
243
244 return rh;
245}
246
247struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh)
248{
249 return rh->ops->card_open(rh);
250}