blob: f91abb83f3cdc84f210c0cdad09365f082dd229d [file] [log] [blame]
Harald Welte63653742019-01-03 16:54:16 +01001#include <stdlib.h>
2#include <stdio.h>
3#include <unistd.h>
4#include <assert.h>
Harald Welte92e7c0b2019-05-14 09:32:10 +02005#include <string.h>
Harald Welte63653742019-01-03 16:54:16 +01006
7#include <osmocom/core/msgb.h>
8#include <osmocom/core/utils.h>
Harald Weltee73a1df2019-05-15 22:27:02 +02009#include <osmocom/core/logging.h>
Harald Welte63653742019-01-03 16:54:16 +010010
11#include "ccid_proto.h"
Harald Welte8d186ad2019-05-15 19:40:29 +020012#include "ccid_device.h"
Harald Welte63653742019-01-03 16:54:16 +010013
Harald Welte8579ce52019-05-15 12:22:24 +020014/* decode on-the-wire T0 parameters into their parsed form */
15static int decode_ccid_pars_t0(struct ccid_pars_decoded *out, const struct ccid_proto_data_t0 *in)
16{
17 /* input validation: only 0x00 and 0x02 permitted for bmTCCKST0 */
18 if (in->bmTCCKST0 & 0xFD)
19 return -11;
20 /* input validation: only 0x00 to 0x03 permitted for bClockSTop */
21 if (in->bClockStop & 0xFC)
22 return -14;
23
24 out->fi = in->bmFindexDindex >> 4;
25 out->di = in->bmFindexDindex & 0xF;
26 if (in->bmTCCKST0 & 2)
27 out->inverse_convention = true;
28 else
29 out->inverse_convention = false;
30 if (in->bGuardTimeT0 == 0xff)
31 out->t0.guard_time_etu = 0;
32 else
33 out->t0.guard_time_etu = in->bGuardTimeT0;
34 out->t0.waiting_integer = in->bWaitingIntegerT0;
35 out->clock_stop = in->bClockStop & 0x03;
36
37 return 0;
38}
39
40/* encode T0 parameters from parsed form into on-the-wire encoding */
41static void encode_ccid_pars_t0(struct ccid_proto_data_t0 *out, const struct ccid_pars_decoded *in)
42{
43 out->bmFindexDindex = ((in->fi << 4) & 0xF0) | (in->di & 0x0F);
44 if (in->inverse_convention)
45 out->bmTCCKST0 = 0x02;
46 else
47 out->bmTCCKST0 = 0x00;
48 out->bGuardTimeT0 = in->t0.guard_time_etu;
49 out->bWaitingIntegerT0 = in->t0.waiting_integer;
50 out->bClockStop = in->clock_stop & 0x03;
51}
52
53/* decode on-the-wire T1 parameters into their parsed form */
54static int decode_ccid_pars_t1(struct ccid_pars_decoded *out, const struct ccid_proto_data_t1 *in)
55{
56 /* input validation: only some values permitted for bmTCCKST0 */
57 if (in->bmTCCKST1 & 0xE8)
58 return -11;
59 /* input validation: only 0x00 to 0x9F permitted for bmWaitingIntegersT1 */
60 if (in->bWaitingIntegersT1 > 0x9F)
61 return -13;
62 /* input validation: only 0x00 to 0x03 permitted for bClockSTop */
63 if (in->bClockStop & 0xFC)
64 return -14;
65 /* input validation: only 0x00 to 0xFE permitted for bIFSC */
66 if (in->bIFSC > 0xFE)
67 return -15;
68
69 out->fi = in->bmFindexDindex >> 4;
70 out->di = in->bmFindexDindex & 0xF;
71 if (in->bmTCCKST1 & 1)
72 out->t1.csum_type = CCID_CSUM_TYPE_CRC;
73 else
74 out->t1.csum_type = CCID_CSUM_TYPE_LRC;
75 if (in->bmTCCKST1 & 2)
76 out->inverse_convention = true;
77 else
78 out->inverse_convention = false;
79 out->t1.guard_time_t1 = in->bGuardTimeT1;
80 out->t1.bwi = in->bWaitingIntegersT1 >> 4;
81 out->t1.cwi = in->bWaitingIntegersT1 & 0xF;
82 out->clock_stop = in->bClockStop & 0x03;
83 out->t1.ifsc = in->bIFSC;
84 out->t1.nad = in->bNadValue;
85
86 return 0;
87}
88
89/* encode T1 parameters from parsed form into on-the-wire encoding */
90static void encode_ccid_pars_t1(struct ccid_proto_data_t1 *out, const struct ccid_pars_decoded *in)
91{
92 out->bmFindexDindex = ((in->fi << 4) & 0xF0) | (in->di & 0x0F);
93 out->bmTCCKST1 = 0x10;
94 if (in->t1.csum_type == CCID_CSUM_TYPE_CRC)
95 out->bmTCCKST1 |= 0x01;
96 if (in->inverse_convention)
97 out->bmTCCKST1 |= 0x02;
98 out->bGuardTimeT1 = in->t1.guard_time_t1;
99 out->bWaitingIntegersT1 = ((in->t1.bwi << 4) & 0xF0) | (in->t1.cwi & 0x0F);
100 out->bClockStop = in->clock_stop & 0x03;
101 out->bIFSC = in->t1.ifsc;
102 out->bNadValue = in->t1.nad;
103}
104
Harald Welte63653742019-01-03 16:54:16 +0100105#define msgb_ccid_out(x) (union ccid_pc_to_rdr *)msgb_data(x)
106#define msgb_ccid_in(x) (union ccid_rdr_to_pc *)msgb_data(x)
107
108static struct ccid_slot *get_ccid_slot(struct ccid_instance *ci, uint8_t slot_nr)
109{
110 if (slot_nr >= sizeof(ci->slot))
111 return NULL;
112 else
Harald Welte92e7c0b2019-05-14 09:32:10 +0200113 return &ci->slot[slot_nr];
Harald Welte63653742019-01-03 16:54:16 +0100114}
115
116static uint8_t get_icc_status(const struct ccid_slot *cs)
117{
118 if (cs->icc_present && cs->icc_powered && !cs->icc_in_reset)
119 return CCID_ICC_STATUS_PRES_ACT;
120 else if (!cs->icc_present)
121 return CCID_ICC_STATUS_NO_ICC;
122 else
123 return CCID_ICC_STATUS_PRES_INACT;
124}
125
126#define SET_HDR(x, msg_type, slot, seq) do { \
127 (x)->hdr.bMessageType = msg_type; \
128 (x)->hdr.dwLength = 0; \
129 (x)->hdr.bSlot = slot; \
130 (x)->hdr.bSeq = seq; \
131 } while (0)
132
133#define SET_HDR_IN(x, msg_type, slot, seq, status, error) do { \
134 SET_HDR(&(x)->hdr, msg_type, slot, seq); \
135 (x)->hdr.bStatus = status; \
136 (x)->hdr.bError = error; \
137 } while (0)
138
Harald Welte8772f582019-05-14 16:34:47 +0200139#if 0
140static uint8_t ccid_pc_to_rdr_get_seq(const struct ccid_pc_to_rdr *u)
141{
142 const struct ccid_header *ch = (const struct ccid_header *) u;
143 return ch->bSeq;
144}
145#endif
146
Harald Welte63653742019-01-03 16:54:16 +0100147/***********************************************************************
148 * Message generation / sending
149 ***********************************************************************/
150
151static struct msgb *ccid_msgb_alloc(void)
152{
Harald Welte92e7c0b2019-05-14 09:32:10 +0200153 struct msgb *msg = msgb_alloc(512, "ccid");
Harald Welte63653742019-01-03 16:54:16 +0100154 OSMO_ASSERT(msg);
155 return msg;
156}
157
Harald Welte8772f582019-05-14 16:34:47 +0200158/* Send given CCID message */
Harald Welte63653742019-01-03 16:54:16 +0100159static int ccid_send(struct ccid_instance *ci, struct msgb *msg)
160{
Harald Weltee73a1df2019-05-15 22:27:02 +0200161 struct ccid_header *ch = (struct ccid_header *) msgb_ccid_in(msg);
162 struct ccid_slot *cs = get_ccid_slot(ci, ch->bSlot);
163 if (cs) {
164 LOGPCS(cs, LOGL_DEBUG, "Tx CCID(IN) %s %s\n",
165 get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg));
166 } else {
167 LOGPCI(ci, LOGL_DEBUG, "Tx CCID(IN) %s %s\n",
168 get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg));
169 }
Harald Weltebcbc1972019-05-15 21:57:32 +0200170 return ci->ops->send_in(ci, msg);
Harald Welte63653742019-01-03 16:54:16 +0100171}
172
Harald Welte8772f582019-05-14 16:34:47 +0200173/* Send given CCID message for given slot; patch bSlot into message */
Harald Welte63653742019-01-03 16:54:16 +0100174static int ccid_slot_send(struct ccid_slot *cs, struct msgb *msg)
175{
Harald Welte92e7c0b2019-05-14 09:32:10 +0200176 struct ccid_header *ch = (struct ccid_header *) msgb_ccid_in(msg);
Harald Welte63653742019-01-03 16:54:16 +0100177
178 /* patch bSlotNr into message */
Harald Welte92e7c0b2019-05-14 09:32:10 +0200179 ch->bSlot = cs->slot_nr;
Harald Welte63653742019-01-03 16:54:16 +0100180 return ccid_send(cs->ci, msg);
181}
182
Harald Welte8772f582019-05-14 16:34:47 +0200183/* Send given CCID message and mark slot as un-busy */
184static int ccid_slot_send_unbusy(struct ccid_slot *cs, struct msgb *msg)
185{
186 cs->cmd_busy = false;
187 return ccid_slot_send(cs, msg);
188}
Harald Welte63653742019-01-03 16:54:16 +0100189
190/* Section 6.2.1 */
Harald Welte8772f582019-05-14 16:34:47 +0200191static struct msgb *ccid_gen_data_block_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq,
192 uint8_t cmd_sts, enum ccid_error_code err,
193 const uint8_t *data, uint32_t data_len)
Harald Welte63653742019-01-03 16:54:16 +0100194{
195 struct msgb *msg = ccid_msgb_alloc();
Harald Welte8772f582019-05-14 16:34:47 +0200196 struct ccid_rdr_to_pc_data_block *db =
Harald Welte92e7c0b2019-05-14 09:32:10 +0200197 (struct ccid_rdr_to_pc_data_block *) msgb_put(msg, sizeof(*db) + data_len);
Harald Welte8772f582019-05-14 16:34:47 +0200198 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
Harald Welte63653742019-01-03 16:54:16 +0100199
Harald Welte8772f582019-05-14 16:34:47 +0200200 SET_HDR_IN(db, RDR_to_PC_DataBlock, slot_nr, seq, sts, err);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200201 osmo_store32le(data_len, &db->hdr.hdr.dwLength);
Harald Welte63653742019-01-03 16:54:16 +0100202 memcpy(db->abData, data, data_len);
203 return msg;
204}
Harald Welte8772f582019-05-14 16:34:47 +0200205static struct msgb *ccid_gen_data_block(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
206 enum ccid_error_code err, const uint8_t *data,
207 uint32_t data_len)
208{
209 return ccid_gen_data_block_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, data, data_len);
210}
Harald Welte63653742019-01-03 16:54:16 +0100211
212/* Section 6.2.2 */
Harald Welte8772f582019-05-14 16:34:47 +0200213static struct msgb *ccid_gen_slot_status_nr(uint8_t slot_nr, uint8_t icc_status,
214 uint8_t seq, uint8_t cmd_sts,
215 enum ccid_error_code err)
Harald Welte63653742019-01-03 16:54:16 +0100216{
217 struct msgb *msg = ccid_msgb_alloc();
Harald Welte92e7c0b2019-05-14 09:32:10 +0200218 struct ccid_rdr_to_pc_slot_status *ss =
219 (struct ccid_rdr_to_pc_slot_status *) msgb_put(msg, sizeof(*ss));
Harald Welte8772f582019-05-14 16:34:47 +0200220 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
Harald Welte63653742019-01-03 16:54:16 +0100221
Harald Welte8772f582019-05-14 16:34:47 +0200222 SET_HDR_IN(ss, RDR_to_PC_SlotStatus, slot_nr, seq, sts, err);
Harald Welte63653742019-01-03 16:54:16 +0100223 return msg;
224}
Harald Welte8772f582019-05-14 16:34:47 +0200225static struct msgb *ccid_gen_slot_status(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
226 enum ccid_error_code err)
227{
228 return ccid_gen_slot_status_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err);
229}
Harald Welte63653742019-01-03 16:54:16 +0100230
231/* Section 6.2.3 */
Harald Welte8772f582019-05-14 16:34:47 +0200232static struct msgb *ccid_gen_parameters_t0_nr(uint8_t slot_nr, uint8_t icc_status,
233 uint8_t seq, uint8_t cmd_sts, enum ccid_error_code err,
Harald Welte8579ce52019-05-15 12:22:24 +0200234 const struct ccid_pars_decoded *dec_par)
Harald Welte8772f582019-05-14 16:34:47 +0200235{
236 struct msgb *msg = ccid_msgb_alloc();
237 struct ccid_rdr_to_pc_parameters *par =
Harald Welte8579ce52019-05-15 12:22:24 +0200238 (struct ccid_rdr_to_pc_parameters *) msgb_put(msg, sizeof(par->hdr)+sizeof(par->abProtocolData.t0));
Harald Welte8772f582019-05-14 16:34:47 +0200239 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
240
241 SET_HDR_IN(par, RDR_to_PC_Parameters, slot_nr, seq, sts, err);
Harald Welte8579ce52019-05-15 12:22:24 +0200242 if (dec_par) {
243 osmo_store32le(sizeof(par->abProtocolData.t0), &par->hdr.hdr.dwLength);
244 encode_ccid_pars_t0(&par->abProtocolData.t0, dec_par);
Harald Welte8772f582019-05-14 16:34:47 +0200245 }
246 return msg;
247}
248static struct msgb *ccid_gen_parameters_t0(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
Harald Welte8579ce52019-05-15 12:22:24 +0200249 enum ccid_error_code err)
Harald Welte8772f582019-05-14 16:34:47 +0200250{
Harald Welte8579ce52019-05-15 12:22:24 +0200251 return ccid_gen_parameters_t0_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, &cs->pars);
Harald Welte8772f582019-05-14 16:34:47 +0200252}
253
254static struct msgb *ccid_gen_parameters_t1_nr(uint8_t slot_nr, uint8_t icc_status,
255 uint8_t seq, uint8_t cmd_sts, enum ccid_error_code err,
Harald Welte8579ce52019-05-15 12:22:24 +0200256 const struct ccid_pars_decoded *dec_par)
Harald Welte8772f582019-05-14 16:34:47 +0200257{
258 struct msgb *msg = ccid_msgb_alloc();
259 struct ccid_rdr_to_pc_parameters *par =
Harald Welte8579ce52019-05-15 12:22:24 +0200260 (struct ccid_rdr_to_pc_parameters *) msgb_put(msg, sizeof(par->hdr)+sizeof(par->abProtocolData.t1));
Harald Welte8772f582019-05-14 16:34:47 +0200261 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
262
263 SET_HDR_IN(par, RDR_to_PC_Parameters, slot_nr, seq, sts, err);
Harald Welte8579ce52019-05-15 12:22:24 +0200264 if (dec_par) {
265 osmo_store32le(sizeof(par->abProtocolData.t1), &par->hdr.hdr.dwLength);
266 encode_ccid_pars_t1(&par->abProtocolData.t1, dec_par);
Harald Welte8772f582019-05-14 16:34:47 +0200267 }
268 return msg;
269}
270static struct msgb *ccid_gen_parameters_t1(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
Harald Welte8579ce52019-05-15 12:22:24 +0200271 enum ccid_error_code err)
Harald Welte8772f582019-05-14 16:34:47 +0200272{
Harald Welte8579ce52019-05-15 12:22:24 +0200273 return ccid_gen_parameters_t1_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, &cs->pars);
Harald Welte8772f582019-05-14 16:34:47 +0200274}
275
Harald Welte63653742019-01-03 16:54:16 +0100276
277/* Section 6.2.4 */
Harald Welte8772f582019-05-14 16:34:47 +0200278static struct msgb *ccid_gen_escape_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq, uint8_t cmd_sts,
279 enum ccid_error_code err, const uint8_t *data, uint32_t data_len)
Harald Welte63653742019-01-03 16:54:16 +0100280{
281 struct msgb *msg = ccid_msgb_alloc();
Harald Welte92e7c0b2019-05-14 09:32:10 +0200282 struct ccid_rdr_to_pc_escape *esc =
283 (struct ccid_rdr_to_pc_escape *) msgb_put(msg, sizeof(*esc) + data_len);
Harald Welte8772f582019-05-14 16:34:47 +0200284 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
Harald Welte63653742019-01-03 16:54:16 +0100285
Harald Welte8772f582019-05-14 16:34:47 +0200286 SET_HDR_IN(esc, RDR_to_PC_Escape, slot_nr, seq, sts, err);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200287 osmo_store32le(data_len, &esc->hdr.hdr.dwLength);
Harald Welte63653742019-01-03 16:54:16 +0100288 memcpy(esc->abData, data, data_len);
289 return msg;
290}
Harald Welte8772f582019-05-14 16:34:47 +0200291static struct msgb *ccid_gen_escape(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
292 enum ccid_error_code err, const uint8_t *data,
293 uint32_t data_len)
294{
295 return ccid_gen_escape_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, data, data_len);
296}
Harald Welte63653742019-01-03 16:54:16 +0100297
298/* Section 6.2.5 */
Harald Welte8772f582019-05-14 16:34:47 +0200299static struct msgb *ccid_gen_clock_and_rate_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq,
300 uint8_t cmd_sts, enum ccid_error_code err,
301 uint32_t clock_khz, uint32_t rate_bps)
Harald Welte63653742019-01-03 16:54:16 +0100302{
303 struct msgb *msg = ccid_msgb_alloc();
Harald Welte92e7c0b2019-05-14 09:32:10 +0200304 struct ccid_rdr_to_pc_data_rate_and_clock *drc =
305 (struct ccid_rdr_to_pc_data_rate_and_clock *) msgb_put(msg, sizeof(*drc));
Harald Welte8772f582019-05-14 16:34:47 +0200306 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
Harald Welte63653742019-01-03 16:54:16 +0100307
Harald Welte8772f582019-05-14 16:34:47 +0200308 SET_HDR_IN(drc, RDR_to_PC_DataRateAndClockFrequency, slot_nr, seq, sts, err);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200309 osmo_store32le(8, &drc->hdr.hdr.dwLength); /* Message-specific data length (wtf?) */
310 osmo_store32le(clock_khz, &drc->dwClockFrequency); /* kHz */
311 osmo_store32le(rate_bps, &drc->dwDataRate); /* bps */
Harald Welte63653742019-01-03 16:54:16 +0100312 return msg;
313}
Harald Welte8772f582019-05-14 16:34:47 +0200314static struct msgb *ccid_gen_clock_and_rate(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
315 enum ccid_error_code err, uint32_t clock_khz,
316 uint32_t rate_bps)
Harald Welte63653742019-01-03 16:54:16 +0100317{
Harald Welte8772f582019-05-14 16:34:47 +0200318 return ccid_gen_clock_and_rate_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err,
319 clock_khz, rate_bps);
Harald Welte63653742019-01-03 16:54:16 +0100320}
Harald Welte8772f582019-05-14 16:34:47 +0200321
322/*! generate an error response for given input message_type/slot_nr/seq
323 * \param[in] msg_type CCID Message Type against which response is to be created
324 * \param[in] slot_nr CCID Slot Number
325 * \param[in] icc_status ICC Status of the slot
326 * \param[in] seq CCID Sequence number
327 * \param[in] err_code CCID Error Code to send
328 * \returns dynamically-allocated message buffer containing error response */
329static struct msgb *gen_err_resp(enum ccid_msg_type msg_type, uint8_t slot_nr, uint8_t icc_status,
330 uint8_t seq, enum ccid_error_code err_code)
331{
332 struct msgb *resp = NULL;
333
334 switch (msg_type) {
335 case PC_to_RDR_IccPowerOn:
336 case PC_to_RDR_XfrBlock:
337 case PC_to_RDR_Secure:
338 /* Return RDR_to_PC_DataBlock */
339 resp = ccid_gen_data_block_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
340 err_code, NULL, 0);
341 break;
342
343 case PC_to_RDR_IccPowerOff:
344 case PC_to_RDR_GetSlotStatus:
345 case PC_to_RDR_IccClock:
346 case PC_to_RDR_T0APDU:
347 case PC_to_RDR_Mechanical:
348 case PC_to_RDR_Abort:
349 /* Return RDR_to_PC_SlotStatus */
350 resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
351 err_code);
352 break;
353
354 case PC_to_RDR_GetParameters:
355 case PC_to_RDR_ResetParameters:
356 case PC_to_RDR_SetParameters:
357 /* Return RDR_to_PC_Parameters */
358 resp = ccid_gen_parameters_t0_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
359 err_code, NULL); /* FIXME: parameters? */
360 break;
361
362 case PC_to_RDR_Escape:
363 /* Return RDR_to_PC_Escape */
364 resp = ccid_gen_escape_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
365 err_code, NULL, 0);
366 break;
367
368 case PC_to_RDR_SetDataRateAndClockFrequency:
369 /* Return RDR_to_PC_SlotStatus */
370 resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
371 err_code);
372 break;
373
374 default:
375 /* generate general error */
376 resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
377 CCID_ERR_CMD_NOT_SUPPORTED);
378 break;
379 }
380 return resp;
381}
Harald Welte63653742019-01-03 16:54:16 +0100382
383/***********************************************************************
384 * Message reception / parsing
385 ***********************************************************************/
386
387/* Section 6.1.3 */
388static int ccid_handle_get_slot_status(struct ccid_slot *cs, struct msgb *msg)
389{
390 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200391 const struct ccid_header *ch = (const struct ccid_header *) u;
392 uint8_t seq = u->get_slot_status.hdr.bSeq;
Harald Welte63653742019-01-03 16:54:16 +0100393 struct msgb *resp;
394
Harald Welte8772f582019-05-14 16:34:47 +0200395 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
Harald Welte63653742019-01-03 16:54:16 +0100396
Harald Welte8772f582019-05-14 16:34:47 +0200397 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100398}
399
400
401/* Section 6.1.1 */
402static int ccid_handle_icc_power_on(struct ccid_slot *cs, struct msgb *msg)
403{
404 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200405 const struct ccid_header *ch = (const struct ccid_header *) u;
406 uint8_t seq = u->icc_power_on.hdr.bSeq;
Harald Welte63653742019-01-03 16:54:16 +0100407 struct msgb *resp;
408
409 /* TODO: send actual ATR; handle error cases */
410 /* TODO: handle this asynchronously */
Harald Welte8772f582019-05-14 16:34:47 +0200411 resp = ccid_gen_data_block(cs, seq, CCID_CMD_STATUS_OK, 0, NULL, 0);
Harald Welte63653742019-01-03 16:54:16 +0100412
Harald Welte8772f582019-05-14 16:34:47 +0200413 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100414}
415
416/* Section 6.1.2 */
417static int ccid_handle_icc_power_off(struct ccid_slot *cs, struct msgb *msg)
418{
419 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200420 const struct ccid_header *ch = (const struct ccid_header *) u;
421 uint8_t seq = u->icc_power_off.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200422 struct msgb *resp;
423
Harald Welte8772f582019-05-14 16:34:47 +0200424 /* FIXME */
425 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
426 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100427}
428
429/* Section 6.1.4 */
430static int ccid_handle_xfr_block(struct ccid_slot *cs, struct msgb *msg)
431{
432 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200433 const struct ccid_header *ch = (const struct ccid_header *) u;
434 uint8_t seq = u->xfr_block.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200435 struct msgb *resp;
436
Harald Welte8772f582019-05-14 16:34:47 +0200437 /* FIXME */
438 resp = ccid_gen_data_block(cs, seq, CCID_CMD_STATUS_OK, 0, NULL, 0);
439 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100440}
441
442/* Section 6.1.5 */
443static int ccid_handle_get_parameters(struct ccid_slot *cs, struct msgb *msg)
444{
445 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200446 const struct ccid_header *ch = (const struct ccid_header *) u;
447 uint8_t seq = u->get_parameters.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200448 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200449
Harald Welte8579ce52019-05-15 12:22:24 +0200450 /* FIXME: T=1 */
451 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0);
Harald Welte8772f582019-05-14 16:34:47 +0200452 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100453}
454
455/* Section 6.1.6 */
456static int ccid_handle_reset_parameters(struct ccid_slot *cs, struct msgb *msg)
457{
458 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200459 const struct ccid_header *ch = (const struct ccid_header *) u;
460 uint8_t seq = u->reset_parameters.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200461 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200462
Harald Welte8579ce52019-05-15 12:22:24 +0200463 /* FIXME: copy default parameters from somewhere */
464 /* FIXME: T=1 */
465 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0);
Harald Welte8772f582019-05-14 16:34:47 +0200466 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100467}
468
469/* Section 6.1.7 */
470static int ccid_handle_set_parameters(struct ccid_slot *cs, struct msgb *msg)
471{
472 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8579ce52019-05-15 12:22:24 +0200473 const struct ccid_pc_to_rdr_set_parameters *spar = &u->set_parameters;
Harald Welte8772f582019-05-14 16:34:47 +0200474 const struct ccid_header *ch = (const struct ccid_header *) u;
475 uint8_t seq = u->set_parameters.hdr.bSeq;
Harald Welte8579ce52019-05-15 12:22:24 +0200476 struct ccid_pars_decoded pars_dec;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200477 struct msgb *resp;
Harald Welte8579ce52019-05-15 12:22:24 +0200478 int rc;
Harald Welte8772f582019-05-14 16:34:47 +0200479
Harald Welte8579ce52019-05-15 12:22:24 +0200480 switch (spar->bProtocolNum) {
481 case CCID_PROTOCOL_NUM_T0:
482 rc = decode_ccid_pars_t0(&pars_dec, &spar->abProtocolData.t0);
Harald Weltee73a1df2019-05-15 22:27:02 +0200483 if (rc < 0) {
484 LOGP(DCCID, LOGL_ERROR, "SetParameters: Unable to parse T0: %d\n", rc);
Harald Welte8579ce52019-05-15 12:22:24 +0200485 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
Harald Weltee73a1df2019-05-15 22:27:02 +0200486 goto out;
487 }
Harald Welte8579ce52019-05-15 12:22:24 +0200488 /* FIXME: validate parameters; abort if they are not supported */
489 cs->pars = pars_dec;
490 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0);
491 break;
492 case CCID_PROTOCOL_NUM_T1:
493 rc = decode_ccid_pars_t1(&pars_dec, &spar->abProtocolData.t1);
Harald Weltee73a1df2019-05-15 22:27:02 +0200494 if (rc < 0) {
495 LOGP(DCCID, LOGL_ERROR, "SetParameters: Unable to parse T1: %d\n", rc);
Harald Welte8579ce52019-05-15 12:22:24 +0200496 resp = ccid_gen_parameters_t1(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
Harald Weltee73a1df2019-05-15 22:27:02 +0200497 goto out;
498 }
Harald Welte8579ce52019-05-15 12:22:24 +0200499 /* FIXME: validate parameters; abort if they are not supported */
500 cs->pars = pars_dec;
501 resp = ccid_gen_parameters_t1(cs, seq, CCID_CMD_STATUS_OK, 0);
502 break;
503 default:
Harald Weltee73a1df2019-05-15 22:27:02 +0200504 LOGP(DCCID, LOGL_ERROR, "SetParameters: Invalid Protocol 0x%02x\n",spar->bProtocolNum);
Harald Welte8579ce52019-05-15 12:22:24 +0200505 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, 0);
506 break;
507 }
Harald Weltee73a1df2019-05-15 22:27:02 +0200508out:
Harald Welte8772f582019-05-14 16:34:47 +0200509 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100510}
511
512/* Section 6.1.8 */
513static int ccid_handle_escape(struct ccid_slot *cs, struct msgb *msg)
514{
515 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200516 const struct ccid_header *ch = (const struct ccid_header *) u;
517 uint8_t seq = u->escape.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200518 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200519
520 resp = ccid_gen_escape(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED, NULL, 0);
521 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100522}
523
524/* Section 6.1.9 */
525static int ccid_handle_icc_clock(struct ccid_slot *cs, struct msgb *msg)
526{
527 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200528 const struct ccid_header *ch = (const struct ccid_header *) u;
529 uint8_t seq = u->icc_clock.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200530 struct msgb *resp;
531
Harald Welte8772f582019-05-14 16:34:47 +0200532 /* FIXME: Actually Stop/Start the clock */
533 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
534 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100535}
536
537/* Section 6.1.10 */
538static int ccid_handle_t0apdu(struct ccid_slot *cs, struct msgb *msg)
539{
540 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200541 const struct ccid_header *ch = (const struct ccid_header *) u;
542 uint8_t seq = u->t0apdu.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200543 struct msgb *resp;
544
Harald Welte8772f582019-05-14 16:34:47 +0200545 /* FIXME */
546 //resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
547 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
548 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100549}
550
551/* Section 6.1.11 */
552static int ccid_handle_secure(struct ccid_slot *cs, struct msgb *msg)
553{
554 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200555 const struct ccid_header *ch = (const struct ccid_header *) u;
556 uint8_t seq = u->secure.hdr.bSeq;
557 struct msgb *resp;
558
559 /* FIXME */
560 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
561 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100562}
563
564/* Section 6.1.12 */
565static int ccid_handle_mechanical(struct ccid_slot *cs, struct msgb *msg)
566{
567 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200568 const struct ccid_header *ch = (const struct ccid_header *) u;
569 uint8_t seq = u->mechanical.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200570 struct msgb *resp;
571
Harald Welte8772f582019-05-14 16:34:47 +0200572 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
573 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100574}
575
576/* Section 6.1.13 */
577static int ccid_handle_abort(struct ccid_slot *cs, struct msgb *msg)
578{
579 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200580 const struct ccid_header *ch = (const struct ccid_header *) u;
581 uint8_t seq = u->abort.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200582 struct msgb *resp;
583
Harald Welte8772f582019-05-14 16:34:47 +0200584 /* Check if the currently in-progress message is Abortable */
585 switch (0/* FIXME */) {
586 case PC_to_RDR_IccPowerOn:
587 case PC_to_RDR_XfrBlock:
588 case PC_to_RDR_Escape:
589 case PC_to_RDR_Secure:
590 case PC_to_RDR_Mechanical:
591 //case PC_to_RDR_Abort: /* seriously? WTF! */
592 break;
593 default:
Harald Weltee73a1df2019-05-15 22:27:02 +0200594 LOGP(DCCID, LOGL_ERROR, "Abort for non-Abortable Message Type\n");
Harald Welte8772f582019-05-14 16:34:47 +0200595 /* CCID spec lists CMD_NOT_ABORTED, but gives no numberic value ?!? */
596 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
597 return ccid_slot_send_unbusy(cs, resp);
598 }
599
600 /* FIXME */
601 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
602 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100603}
604
605/* Section 6.1.14 */
606static int ccid_handle_set_rate_and_clock(struct ccid_slot *cs, struct msgb *msg)
607{
Harald Welte92e7c0b2019-05-14 09:32:10 +0200608 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200609 const struct ccid_header *ch = (const struct ccid_header *) u;
610 uint8_t seq = u->set_rate_and_clock.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200611 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200612
613 /* FIXME */
614 resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_OK, 0, 9600, 2500000);
615 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100616}
617
Harald Welte8772f582019-05-14 16:34:47 +0200618/*! Handle data arriving from the host on the OUT endpoint.
619 * \param[in] cs CCID Instance on which to operate
Harald Weltee6ac4162019-05-16 11:18:00 +0200620 * \param[in] msgb received message buffer containing one CCID OUT EP message from the host.
621 * Ownership of message buffer is transferred, i.e. it's our job to msgb_free()
622 * it eventually, after we're done with it (could be asynchronously).
Harald Welte8772f582019-05-14 16:34:47 +0200623 * \returns 0 on success; negative on error */
Harald Welte63653742019-01-03 16:54:16 +0100624int ccid_handle_out(struct ccid_instance *ci, struct msgb *msg)
625{
626 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
627 const struct ccid_header *ch = (const struct ccid_header *) u;
628 unsigned int len = msgb_length(msg);
629 struct ccid_slot *cs;
Harald Welte8772f582019-05-14 16:34:47 +0200630 struct msgb *resp;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200631 int rc;
Harald Welte63653742019-01-03 16:54:16 +0100632
633 if (len < sizeof(*ch)) {
634 /* FIXME */
635 return -1;
636 }
637
Harald Welte8772f582019-05-14 16:34:47 +0200638 /* Check for invalid slot number */
Harald Welte92e7c0b2019-05-14 09:32:10 +0200639 cs = get_ccid_slot(ci, ch->bSlot);
Harald Welte63653742019-01-03 16:54:16 +0100640 if (!cs) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200641 LOGPCI(ci, LOGL_ERROR, "Invalid bSlot %u\n", ch->bSlot);
Harald Welte8772f582019-05-14 16:34:47 +0200642 resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq, 5);
643 return ccid_send(ci, resp);
Harald Welte63653742019-01-03 16:54:16 +0100644 }
645
Harald Welte8772f582019-05-14 16:34:47 +0200646 /* Check if slot is already busy; Reject any additional commands meanwhile */
647 if (cs->cmd_busy) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200648 LOGPCS(cs, LOGL_ERROR, "Slot Busy, but another cmd received\n");
Harald Welte8772f582019-05-14 16:34:47 +0200649 /* FIXME: ABORT logic as per section 5.3.1 of CCID Spec v1.1 */
650 resp = gen_err_resp(ch->bMessageType, ch->bSlot, get_icc_status(cs), ch->bSeq,
651 CCID_ERR_CMD_SLOT_BUSY);
652 return ccid_send(ci, resp);
653 }
654
Harald Weltee73a1df2019-05-15 22:27:02 +0200655 LOGPCS(cs, LOGL_DEBUG, "Rx CCID(OUT) %s %s\n",
656 get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg));
657
Harald Welte8772f582019-05-14 16:34:47 +0200658 /* we're now processing a command for the slot; mark slot as busy */
659 cs->cmd_busy = true;
660
661 /* TODO: enqueue into the per-slot specific input queue */
662
Harald Welte63653742019-01-03 16:54:16 +0100663 switch (ch->bMessageType) {
664 case PC_to_RDR_GetSlotStatus:
665 if (len != sizeof(u->get_slot_status))
666 goto short_msg;
667 rc = ccid_handle_get_slot_status(cs, msg);
668 break;
669 case PC_to_RDR_IccPowerOn:
670 if (len != sizeof(u->icc_power_on))
671 goto short_msg;
672 rc = ccid_handle_icc_power_on(cs, msg);
673 break;
674 case PC_to_RDR_IccPowerOff:
675 if (len != sizeof(u->icc_power_off))
676 goto short_msg;
677 rc = ccid_handle_icc_power_off(cs, msg);
678 break;
679 case PC_to_RDR_XfrBlock:
680 if (len < sizeof(u->xfr_block))
681 goto short_msg;
682 rc = ccid_handle_xfr_block(cs, msg);
683 break;
684 case PC_to_RDR_GetParameters:
685 if (len != sizeof(u->get_parameters))
686 goto short_msg;
687 rc = ccid_handle_get_parameters(cs, msg);
688 break;
689 case PC_to_RDR_ResetParameters:
690 if (len != sizeof(u->reset_parameters))
691 goto short_msg;
692 rc = ccid_handle_reset_parameters(cs, msg);
693 break;
694 case PC_to_RDR_SetParameters:
695 if (len != sizeof(u->set_parameters))
696 goto short_msg;
697 rc = ccid_handle_set_parameters(cs, msg);
698 break;
699 case PC_to_RDR_Escape:
700 if (len < sizeof(u->escape))
701 goto short_msg;
702 rc = ccid_handle_escape(cs, msg);
703 break;
704 case PC_to_RDR_IccClock:
705 if (len != sizeof(u->icc_clock))
706 goto short_msg;
707 rc = ccid_handle_icc_clock(cs, msg);
708 break;
709 case PC_to_RDR_T0APDU:
710 if (len != /*FIXME*/ sizeof(u->t0apdu))
711 goto short_msg;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200712 rc = ccid_handle_t0apdu(cs, msg);
Harald Welte63653742019-01-03 16:54:16 +0100713 break;
714 case PC_to_RDR_Secure:
715 if (len < sizeof(u->secure))
716 goto short_msg;
717 rc = ccid_handle_secure(cs, msg);
718 break;
719 case PC_to_RDR_Mechanical:
720 if (len != sizeof(u->mechanical))
721 goto short_msg;
722 rc = ccid_handle_mechanical(cs, msg);
723 break;
724 case PC_to_RDR_Abort:
725 if (len != sizeof(u->abort))
726 goto short_msg;
727 rc = ccid_handle_abort(cs, msg);
728 break;
729 case PC_to_RDR_SetDataRateAndClockFrequency:
730 if (len != sizeof(u->set_rate_and_clock))
731 goto short_msg;
732 rc = ccid_handle_set_rate_and_clock(cs, msg);
733 break;
734 default:
Harald Welte8772f582019-05-14 16:34:47 +0200735 /* generic response with bERror = 0 (command not supported) */
Harald Weltee73a1df2019-05-15 22:27:02 +0200736 LOGP(DCCID, LOGL_NOTICE, "Unknown CCID Message received: 0x%02x\n", ch->bMessageType);
Harald Welte8772f582019-05-14 16:34:47 +0200737 resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq,
738 CCID_ERR_CMD_NOT_SUPPORTED);
739 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100740 }
Harald Welte92e7c0b2019-05-14 09:32:10 +0200741 return 0;
742
Harald Welte63653742019-01-03 16:54:16 +0100743short_msg:
Harald Weltee73a1df2019-05-15 22:27:02 +0200744 LOGP(DCCID, LOGL_ERROR, "Short CCID message received: %s; ignoring\n", msgb_hexdump(msg));
Harald Welte92e7c0b2019-05-14 09:32:10 +0200745 return -1;
Harald Welte63653742019-01-03 16:54:16 +0100746}
Harald Weltebcbc1972019-05-15 21:57:32 +0200747
748void ccid_instance_init(struct ccid_instance *ci, const struct ccid_ops *ops, const char *name,
749 void *priv)
750{
751 int i;
752
753 for (i = 0; i < ARRAY_SIZE(ci->slot); i++) {
754 struct ccid_slot *cs = &ci->slot[i];
755 cs->slot_nr = i;
756 cs->ci = ci;
757 }
758 ci->ops= ops;
759 ci->name = name;
760 ci->priv = priv;
761}