Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <stdio.h> |
| 3 | #include <unistd.h> |
| 4 | #include <assert.h> |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 5 | #include <string.h> |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 6 | |
| 7 | #include <osmocom/core/msgb.h> |
| 8 | #include <osmocom/core/utils.h> |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 9 | #include <osmocom/core/logging.h> |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 10 | |
| 11 | #include "ccid_proto.h" |
Harald Welte | 8d186ad | 2019-05-15 19:40:29 +0200 | [diff] [blame] | 12 | #include "ccid_device.h" |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 13 | |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 14 | /* local, stand-alone definition of a USB control request */ |
| 15 | struct _usb_ctrl_req { |
| 16 | uint8_t bRequestType; |
| 17 | uint8_t bRequest; |
| 18 | uint16_t wValue; |
| 19 | uint16_t wIndex; |
| 20 | uint16_t wLength; |
| 21 | } __attribute__ ((packed));; |
| 22 | |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 23 | /* decode on-the-wire T0 parameters into their parsed form */ |
| 24 | static int decode_ccid_pars_t0(struct ccid_pars_decoded *out, const struct ccid_proto_data_t0 *in) |
| 25 | { |
| 26 | /* input validation: only 0x00 and 0x02 permitted for bmTCCKST0 */ |
| 27 | if (in->bmTCCKST0 & 0xFD) |
| 28 | return -11; |
| 29 | /* input validation: only 0x00 to 0x03 permitted for bClockSTop */ |
| 30 | if (in->bClockStop & 0xFC) |
| 31 | return -14; |
| 32 | |
| 33 | out->fi = in->bmFindexDindex >> 4; |
| 34 | out->di = in->bmFindexDindex & 0xF; |
| 35 | if (in->bmTCCKST0 & 2) |
| 36 | out->inverse_convention = true; |
| 37 | else |
| 38 | out->inverse_convention = false; |
| 39 | if (in->bGuardTimeT0 == 0xff) |
| 40 | out->t0.guard_time_etu = 0; |
| 41 | else |
| 42 | out->t0.guard_time_etu = in->bGuardTimeT0; |
| 43 | out->t0.waiting_integer = in->bWaitingIntegerT0; |
| 44 | out->clock_stop = in->bClockStop & 0x03; |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | /* encode T0 parameters from parsed form into on-the-wire encoding */ |
| 50 | static void encode_ccid_pars_t0(struct ccid_proto_data_t0 *out, const struct ccid_pars_decoded *in) |
| 51 | { |
| 52 | out->bmFindexDindex = ((in->fi << 4) & 0xF0) | (in->di & 0x0F); |
| 53 | if (in->inverse_convention) |
| 54 | out->bmTCCKST0 = 0x02; |
| 55 | else |
| 56 | out->bmTCCKST0 = 0x00; |
| 57 | out->bGuardTimeT0 = in->t0.guard_time_etu; |
| 58 | out->bWaitingIntegerT0 = in->t0.waiting_integer; |
| 59 | out->bClockStop = in->clock_stop & 0x03; |
| 60 | } |
| 61 | |
| 62 | /* decode on-the-wire T1 parameters into their parsed form */ |
| 63 | static int decode_ccid_pars_t1(struct ccid_pars_decoded *out, const struct ccid_proto_data_t1 *in) |
| 64 | { |
| 65 | /* input validation: only some values permitted for bmTCCKST0 */ |
| 66 | if (in->bmTCCKST1 & 0xE8) |
| 67 | return -11; |
| 68 | /* input validation: only 0x00 to 0x9F permitted for bmWaitingIntegersT1 */ |
| 69 | if (in->bWaitingIntegersT1 > 0x9F) |
| 70 | return -13; |
| 71 | /* input validation: only 0x00 to 0x03 permitted for bClockSTop */ |
| 72 | if (in->bClockStop & 0xFC) |
| 73 | return -14; |
| 74 | /* input validation: only 0x00 to 0xFE permitted for bIFSC */ |
| 75 | if (in->bIFSC > 0xFE) |
| 76 | return -15; |
| 77 | |
| 78 | out->fi = in->bmFindexDindex >> 4; |
| 79 | out->di = in->bmFindexDindex & 0xF; |
| 80 | if (in->bmTCCKST1 & 1) |
| 81 | out->t1.csum_type = CCID_CSUM_TYPE_CRC; |
| 82 | else |
| 83 | out->t1.csum_type = CCID_CSUM_TYPE_LRC; |
| 84 | if (in->bmTCCKST1 & 2) |
| 85 | out->inverse_convention = true; |
| 86 | else |
| 87 | out->inverse_convention = false; |
| 88 | out->t1.guard_time_t1 = in->bGuardTimeT1; |
| 89 | out->t1.bwi = in->bWaitingIntegersT1 >> 4; |
| 90 | out->t1.cwi = in->bWaitingIntegersT1 & 0xF; |
| 91 | out->clock_stop = in->bClockStop & 0x03; |
| 92 | out->t1.ifsc = in->bIFSC; |
| 93 | out->t1.nad = in->bNadValue; |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | /* encode T1 parameters from parsed form into on-the-wire encoding */ |
| 99 | static void encode_ccid_pars_t1(struct ccid_proto_data_t1 *out, const struct ccid_pars_decoded *in) |
| 100 | { |
| 101 | out->bmFindexDindex = ((in->fi << 4) & 0xF0) | (in->di & 0x0F); |
| 102 | out->bmTCCKST1 = 0x10; |
| 103 | if (in->t1.csum_type == CCID_CSUM_TYPE_CRC) |
| 104 | out->bmTCCKST1 |= 0x01; |
| 105 | if (in->inverse_convention) |
| 106 | out->bmTCCKST1 |= 0x02; |
| 107 | out->bGuardTimeT1 = in->t1.guard_time_t1; |
| 108 | out->bWaitingIntegersT1 = ((in->t1.bwi << 4) & 0xF0) | (in->t1.cwi & 0x0F); |
| 109 | out->bClockStop = in->clock_stop & 0x03; |
| 110 | out->bIFSC = in->t1.ifsc; |
| 111 | out->bNadValue = in->t1.nad; |
| 112 | } |
| 113 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 114 | #define msgb_ccid_out(x) (union ccid_pc_to_rdr *)msgb_data(x) |
| 115 | #define msgb_ccid_in(x) (union ccid_rdr_to_pc *)msgb_data(x) |
| 116 | |
| 117 | static struct ccid_slot *get_ccid_slot(struct ccid_instance *ci, uint8_t slot_nr) |
| 118 | { |
| 119 | if (slot_nr >= sizeof(ci->slot)) |
| 120 | return NULL; |
| 121 | else |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 122 | return &ci->slot[slot_nr]; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static uint8_t get_icc_status(const struct ccid_slot *cs) |
| 126 | { |
| 127 | if (cs->icc_present && cs->icc_powered && !cs->icc_in_reset) |
| 128 | return CCID_ICC_STATUS_PRES_ACT; |
| 129 | else if (!cs->icc_present) |
| 130 | return CCID_ICC_STATUS_NO_ICC; |
| 131 | else |
| 132 | return CCID_ICC_STATUS_PRES_INACT; |
| 133 | } |
| 134 | |
| 135 | #define SET_HDR(x, msg_type, slot, seq) do { \ |
| 136 | (x)->hdr.bMessageType = msg_type; \ |
| 137 | (x)->hdr.dwLength = 0; \ |
| 138 | (x)->hdr.bSlot = slot; \ |
| 139 | (x)->hdr.bSeq = seq; \ |
| 140 | } while (0) |
| 141 | |
| 142 | #define SET_HDR_IN(x, msg_type, slot, seq, status, error) do { \ |
| 143 | SET_HDR(&(x)->hdr, msg_type, slot, seq); \ |
| 144 | (x)->hdr.bStatus = status; \ |
| 145 | (x)->hdr.bError = error; \ |
| 146 | } while (0) |
| 147 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 148 | #if 0 |
| 149 | static uint8_t ccid_pc_to_rdr_get_seq(const struct ccid_pc_to_rdr *u) |
| 150 | { |
| 151 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 152 | return ch->bSeq; |
| 153 | } |
| 154 | #endif |
| 155 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 156 | /*********************************************************************** |
| 157 | * Message generation / sending |
| 158 | ***********************************************************************/ |
| 159 | |
| 160 | static struct msgb *ccid_msgb_alloc(void) |
| 161 | { |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 162 | struct msgb *msg = msgb_alloc(512, "ccid"); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 163 | OSMO_ASSERT(msg); |
| 164 | return msg; |
| 165 | } |
| 166 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 167 | /* Send given CCID message */ |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 168 | static int ccid_send(struct ccid_instance *ci, struct msgb *msg) |
| 169 | { |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 170 | struct ccid_header *ch = (struct ccid_header *) msgb_ccid_in(msg); |
| 171 | struct ccid_slot *cs = get_ccid_slot(ci, ch->bSlot); |
| 172 | if (cs) { |
| 173 | LOGPCS(cs, LOGL_DEBUG, "Tx CCID(IN) %s %s\n", |
| 174 | get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg)); |
| 175 | } else { |
| 176 | LOGPCI(ci, LOGL_DEBUG, "Tx CCID(IN) %s %s\n", |
| 177 | get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg)); |
| 178 | } |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 179 | return ci->ops->send_in(ci, msg); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 182 | /* Send given CCID message for given slot; patch bSlot into message */ |
Harald Welte | 005b09d | 2019-05-16 17:24:29 +0200 | [diff] [blame] | 183 | int ccid_slot_send(struct ccid_slot *cs, struct msgb *msg) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 184 | { |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 185 | struct ccid_header *ch = (struct ccid_header *) msgb_ccid_in(msg); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 186 | |
| 187 | /* patch bSlotNr into message */ |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 188 | ch->bSlot = cs->slot_nr; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 189 | return ccid_send(cs->ci, msg); |
| 190 | } |
| 191 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 192 | /* Send given CCID message and mark slot as un-busy */ |
Harald Welte | 005b09d | 2019-05-16 17:24:29 +0200 | [diff] [blame] | 193 | int ccid_slot_send_unbusy(struct ccid_slot *cs, struct msgb *msg) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 194 | { |
| 195 | cs->cmd_busy = false; |
| 196 | return ccid_slot_send(cs, msg); |
| 197 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 198 | |
| 199 | /* Section 6.2.1 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 200 | static struct msgb *ccid_gen_data_block_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq, |
| 201 | uint8_t cmd_sts, enum ccid_error_code err, |
| 202 | const uint8_t *data, uint32_t data_len) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 203 | { |
| 204 | struct msgb *msg = ccid_msgb_alloc(); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 205 | struct ccid_rdr_to_pc_data_block *db = |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 206 | (struct ccid_rdr_to_pc_data_block *) msgb_put(msg, sizeof(*db) + data_len); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 207 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 208 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 209 | SET_HDR_IN(db, RDR_to_PC_DataBlock, slot_nr, seq, sts, err); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 210 | osmo_store32le(data_len, &db->hdr.hdr.dwLength); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 211 | memcpy(db->abData, data, data_len); |
| 212 | return msg; |
| 213 | } |
Harald Welte | 005b09d | 2019-05-16 17:24:29 +0200 | [diff] [blame] | 214 | struct msgb *ccid_gen_data_block(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 215 | enum ccid_error_code err, const uint8_t *data, |
| 216 | uint32_t data_len) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 217 | { |
| 218 | return ccid_gen_data_block_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, data, data_len); |
| 219 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 220 | |
| 221 | /* Section 6.2.2 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 222 | static struct msgb *ccid_gen_slot_status_nr(uint8_t slot_nr, uint8_t icc_status, |
| 223 | uint8_t seq, uint8_t cmd_sts, |
| 224 | enum ccid_error_code err) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 225 | { |
| 226 | struct msgb *msg = ccid_msgb_alloc(); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 227 | struct ccid_rdr_to_pc_slot_status *ss = |
| 228 | (struct ccid_rdr_to_pc_slot_status *) msgb_put(msg, sizeof(*ss)); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 229 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 230 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 231 | SET_HDR_IN(ss, RDR_to_PC_SlotStatus, slot_nr, seq, sts, err); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 232 | return msg; |
| 233 | } |
Harald Welte | 005b09d | 2019-05-16 17:24:29 +0200 | [diff] [blame] | 234 | struct msgb *ccid_gen_slot_status(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 235 | enum ccid_error_code err) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 236 | { |
| 237 | return ccid_gen_slot_status_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err); |
| 238 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 239 | |
| 240 | /* Section 6.2.3 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 241 | static struct msgb *ccid_gen_parameters_t0_nr(uint8_t slot_nr, uint8_t icc_status, |
| 242 | uint8_t seq, uint8_t cmd_sts, enum ccid_error_code err, |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 243 | const struct ccid_pars_decoded *dec_par) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 244 | { |
| 245 | struct msgb *msg = ccid_msgb_alloc(); |
| 246 | struct ccid_rdr_to_pc_parameters *par = |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 247 | (struct ccid_rdr_to_pc_parameters *) msgb_put(msg, sizeof(par->hdr)+sizeof(par->abProtocolData.t0)); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 248 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
| 249 | |
| 250 | SET_HDR_IN(par, RDR_to_PC_Parameters, slot_nr, seq, sts, err); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 251 | if (dec_par) { |
| 252 | osmo_store32le(sizeof(par->abProtocolData.t0), &par->hdr.hdr.dwLength); |
| 253 | encode_ccid_pars_t0(&par->abProtocolData.t0, dec_par); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 254 | } |
| 255 | return msg; |
| 256 | } |
| 257 | static struct msgb *ccid_gen_parameters_t0(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 258 | enum ccid_error_code err) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 259 | { |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 260 | return ccid_gen_parameters_t0_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, &cs->pars); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | static struct msgb *ccid_gen_parameters_t1_nr(uint8_t slot_nr, uint8_t icc_status, |
| 264 | uint8_t seq, uint8_t cmd_sts, enum ccid_error_code err, |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 265 | const struct ccid_pars_decoded *dec_par) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 266 | { |
| 267 | struct msgb *msg = ccid_msgb_alloc(); |
| 268 | struct ccid_rdr_to_pc_parameters *par = |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 269 | (struct ccid_rdr_to_pc_parameters *) msgb_put(msg, sizeof(par->hdr)+sizeof(par->abProtocolData.t1)); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 270 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
| 271 | |
| 272 | SET_HDR_IN(par, RDR_to_PC_Parameters, slot_nr, seq, sts, err); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 273 | if (dec_par) { |
| 274 | osmo_store32le(sizeof(par->abProtocolData.t1), &par->hdr.hdr.dwLength); |
| 275 | encode_ccid_pars_t1(&par->abProtocolData.t1, dec_par); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 276 | } |
| 277 | return msg; |
| 278 | } |
| 279 | static struct msgb *ccid_gen_parameters_t1(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 280 | enum ccid_error_code err) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 281 | { |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 282 | return ccid_gen_parameters_t1_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, &cs->pars); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 283 | } |
| 284 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 285 | |
| 286 | /* Section 6.2.4 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 287 | static struct msgb *ccid_gen_escape_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq, uint8_t cmd_sts, |
| 288 | enum ccid_error_code err, const uint8_t *data, uint32_t data_len) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 289 | { |
| 290 | struct msgb *msg = ccid_msgb_alloc(); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 291 | struct ccid_rdr_to_pc_escape *esc = |
| 292 | (struct ccid_rdr_to_pc_escape *) msgb_put(msg, sizeof(*esc) + data_len); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 293 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 294 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 295 | SET_HDR_IN(esc, RDR_to_PC_Escape, slot_nr, seq, sts, err); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 296 | osmo_store32le(data_len, &esc->hdr.hdr.dwLength); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 297 | memcpy(esc->abData, data, data_len); |
| 298 | return msg; |
| 299 | } |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 300 | static struct msgb *ccid_gen_escape(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 301 | enum ccid_error_code err, const uint8_t *data, |
| 302 | uint32_t data_len) |
| 303 | { |
| 304 | return ccid_gen_escape_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, data, data_len); |
| 305 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 306 | |
| 307 | /* Section 6.2.5 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 308 | static struct msgb *ccid_gen_clock_and_rate_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq, |
| 309 | uint8_t cmd_sts, enum ccid_error_code err, |
| 310 | uint32_t clock_khz, uint32_t rate_bps) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 311 | { |
| 312 | struct msgb *msg = ccid_msgb_alloc(); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 313 | struct ccid_rdr_to_pc_data_rate_and_clock *drc = |
| 314 | (struct ccid_rdr_to_pc_data_rate_and_clock *) msgb_put(msg, sizeof(*drc)); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 315 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 316 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 317 | SET_HDR_IN(drc, RDR_to_PC_DataRateAndClockFrequency, slot_nr, seq, sts, err); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 318 | osmo_store32le(8, &drc->hdr.hdr.dwLength); /* Message-specific data length (wtf?) */ |
| 319 | osmo_store32le(clock_khz, &drc->dwClockFrequency); /* kHz */ |
| 320 | osmo_store32le(rate_bps, &drc->dwDataRate); /* bps */ |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 321 | return msg; |
| 322 | } |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 323 | static struct msgb *ccid_gen_clock_and_rate(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 324 | enum ccid_error_code err, uint32_t clock_khz, |
| 325 | uint32_t rate_bps) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 326 | { |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 327 | return ccid_gen_clock_and_rate_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, |
| 328 | clock_khz, rate_bps); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 329 | } |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 330 | |
| 331 | /*! generate an error response for given input message_type/slot_nr/seq |
| 332 | * \param[in] msg_type CCID Message Type against which response is to be created |
| 333 | * \param[in] slot_nr CCID Slot Number |
| 334 | * \param[in] icc_status ICC Status of the slot |
| 335 | * \param[in] seq CCID Sequence number |
| 336 | * \param[in] err_code CCID Error Code to send |
| 337 | * \returns dynamically-allocated message buffer containing error response */ |
| 338 | static struct msgb *gen_err_resp(enum ccid_msg_type msg_type, uint8_t slot_nr, uint8_t icc_status, |
| 339 | uint8_t seq, enum ccid_error_code err_code) |
| 340 | { |
| 341 | struct msgb *resp = NULL; |
| 342 | |
| 343 | switch (msg_type) { |
| 344 | case PC_to_RDR_IccPowerOn: |
| 345 | case PC_to_RDR_XfrBlock: |
| 346 | case PC_to_RDR_Secure: |
| 347 | /* Return RDR_to_PC_DataBlock */ |
| 348 | resp = ccid_gen_data_block_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 349 | err_code, NULL, 0); |
| 350 | break; |
| 351 | |
| 352 | case PC_to_RDR_IccPowerOff: |
| 353 | case PC_to_RDR_GetSlotStatus: |
| 354 | case PC_to_RDR_IccClock: |
| 355 | case PC_to_RDR_T0APDU: |
| 356 | case PC_to_RDR_Mechanical: |
| 357 | case PC_to_RDR_Abort: |
| 358 | /* Return RDR_to_PC_SlotStatus */ |
| 359 | resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 360 | err_code); |
| 361 | break; |
| 362 | |
| 363 | case PC_to_RDR_GetParameters: |
| 364 | case PC_to_RDR_ResetParameters: |
| 365 | case PC_to_RDR_SetParameters: |
| 366 | /* Return RDR_to_PC_Parameters */ |
| 367 | resp = ccid_gen_parameters_t0_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 368 | err_code, NULL); /* FIXME: parameters? */ |
| 369 | break; |
| 370 | |
| 371 | case PC_to_RDR_Escape: |
| 372 | /* Return RDR_to_PC_Escape */ |
| 373 | resp = ccid_gen_escape_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 374 | err_code, NULL, 0); |
| 375 | break; |
| 376 | |
| 377 | case PC_to_RDR_SetDataRateAndClockFrequency: |
| 378 | /* Return RDR_to_PC_SlotStatus */ |
| 379 | resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 380 | err_code); |
| 381 | break; |
| 382 | |
| 383 | default: |
| 384 | /* generate general error */ |
| 385 | resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 386 | CCID_ERR_CMD_NOT_SUPPORTED); |
| 387 | break; |
| 388 | } |
| 389 | return resp; |
| 390 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 391 | |
| 392 | /*********************************************************************** |
| 393 | * Message reception / parsing |
| 394 | ***********************************************************************/ |
| 395 | |
| 396 | /* Section 6.1.3 */ |
| 397 | static int ccid_handle_get_slot_status(struct ccid_slot *cs, struct msgb *msg) |
| 398 | { |
| 399 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 400 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 401 | uint8_t seq = u->get_slot_status.hdr.bSeq; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 402 | struct msgb *resp; |
| 403 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 404 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 405 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 406 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | |
| 410 | /* Section 6.1.1 */ |
| 411 | static int ccid_handle_icc_power_on(struct ccid_slot *cs, struct msgb *msg) |
| 412 | { |
| 413 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 414 | const struct ccid_header *ch = (const struct ccid_header *) u; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 415 | |
Harald Welte | 505d441 | 2019-05-16 17:26:09 +0200 | [diff] [blame] | 416 | /* handle this asynchronously */ |
| 417 | cs->ci->slot_ops->icc_power_on_async(cs, msg, &u->icc_power_on); |
| 418 | return 1; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /* Section 6.1.2 */ |
| 422 | static int ccid_handle_icc_power_off(struct ccid_slot *cs, struct msgb *msg) |
| 423 | { |
| 424 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 425 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 426 | uint8_t seq = u->icc_power_off.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 427 | struct msgb *resp; |
| 428 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 429 | cs->ci->slot_ops->set_power(cs, false); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 430 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
| 431 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* Section 6.1.4 */ |
| 435 | static int ccid_handle_xfr_block(struct ccid_slot *cs, struct msgb *msg) |
| 436 | { |
| 437 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 438 | const struct ccid_header *ch = (const struct ccid_header *) u; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 439 | |
Harald Welte | 505d441 | 2019-05-16 17:26:09 +0200 | [diff] [blame] | 440 | /* handle this asynchronously */ |
| 441 | cs->ci->slot_ops->xfr_block_async(cs, msg, &u->xfr_block); |
| 442 | return 1; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* Section 6.1.5 */ |
| 446 | static int ccid_handle_get_parameters(struct ccid_slot *cs, struct msgb *msg) |
| 447 | { |
| 448 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 449 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 450 | uint8_t seq = u->get_parameters.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 451 | struct msgb *resp; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 452 | |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 453 | /* FIXME: T=1 */ |
| 454 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 455 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /* Section 6.1.6 */ |
| 459 | static int ccid_handle_reset_parameters(struct ccid_slot *cs, struct msgb *msg) |
| 460 | { |
| 461 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 462 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 463 | uint8_t seq = u->reset_parameters.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 464 | struct msgb *resp; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 465 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 466 | /* copy default parameters from somewhere */ |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 467 | /* FIXME: T=1 */ |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 468 | cs->ci->slot_ops->set_params(cs, CCID_PROTOCOL_NUM_T0, cs->default_pars); |
| 469 | cs->pars = *cs->default_pars; |
| 470 | |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 471 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 472 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /* Section 6.1.7 */ |
| 476 | static int ccid_handle_set_parameters(struct ccid_slot *cs, struct msgb *msg) |
| 477 | { |
| 478 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 479 | const struct ccid_pc_to_rdr_set_parameters *spar = &u->set_parameters; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 480 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 481 | uint8_t seq = u->set_parameters.hdr.bSeq; |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 482 | struct ccid_pars_decoded pars_dec; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 483 | struct msgb *resp; |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 484 | int rc; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 485 | |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 486 | switch (spar->bProtocolNum) { |
| 487 | case CCID_PROTOCOL_NUM_T0: |
| 488 | rc = decode_ccid_pars_t0(&pars_dec, &spar->abProtocolData.t0); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 489 | break; |
| 490 | case CCID_PROTOCOL_NUM_T1: |
| 491 | rc = decode_ccid_pars_t1(&pars_dec, &spar->abProtocolData.t1); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 492 | break; |
| 493 | default: |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 494 | LOGP(DCCID, LOGL_ERROR, "SetParameters: Invalid Protocol 0x%02x\n",spar->bProtocolNum); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 495 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, 0); |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 496 | goto out; |
| 497 | } |
| 498 | |
| 499 | if (rc < 0) { |
| 500 | LOGP(DCCID, LOGL_ERROR, "SetParameters: Unable to parse: %d\n", rc); |
| 501 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc); |
| 502 | goto out; |
| 503 | } |
| 504 | |
| 505 | /* validate parameters; abort if they are not supported */ |
| 506 | rc = cs->ci->slot_ops->set_params(cs, spar->bProtocolNum, &pars_dec); |
| 507 | if (rc < 0) { |
| 508 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc); |
| 509 | } else { |
| 510 | cs->pars = pars_dec; |
| 511 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 512 | } |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 513 | out: |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 514 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | /* Section 6.1.8 */ |
| 518 | static int ccid_handle_escape(struct ccid_slot *cs, struct msgb *msg) |
| 519 | { |
| 520 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 521 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 522 | uint8_t seq = u->escape.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 523 | struct msgb *resp; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 524 | |
| 525 | resp = ccid_gen_escape(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED, NULL, 0); |
| 526 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /* Section 6.1.9 */ |
| 530 | static int ccid_handle_icc_clock(struct ccid_slot *cs, struct msgb *msg) |
| 531 | { |
| 532 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 533 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 534 | uint8_t seq = u->icc_clock.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 535 | struct msgb *resp; |
| 536 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 537 | cs->ci->slot_ops->set_clock(cs, u->icc_clock.bClockCommand); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 538 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
| 539 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /* Section 6.1.10 */ |
| 543 | static int ccid_handle_t0apdu(struct ccid_slot *cs, struct msgb *msg) |
| 544 | { |
| 545 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 546 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 547 | uint8_t seq = u->t0apdu.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 548 | struct msgb *resp; |
| 549 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 550 | /* FIXME: Required for APDU level exchange */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 551 | //resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
| 552 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED); |
| 553 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /* Section 6.1.11 */ |
| 557 | static int ccid_handle_secure(struct ccid_slot *cs, struct msgb *msg) |
| 558 | { |
| 559 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 560 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 561 | uint8_t seq = u->secure.hdr.bSeq; |
| 562 | struct msgb *resp; |
| 563 | |
| 564 | /* FIXME */ |
| 565 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED); |
| 566 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | /* Section 6.1.12 */ |
| 570 | static int ccid_handle_mechanical(struct ccid_slot *cs, struct msgb *msg) |
| 571 | { |
| 572 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 573 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 574 | uint8_t seq = u->mechanical.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 575 | struct msgb *resp; |
| 576 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 577 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED); |
| 578 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /* Section 6.1.13 */ |
| 582 | static int ccid_handle_abort(struct ccid_slot *cs, struct msgb *msg) |
| 583 | { |
| 584 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 585 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 586 | uint8_t seq = u->abort.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 587 | struct msgb *resp; |
| 588 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 589 | /* Check if the currently in-progress message is Abortable */ |
| 590 | switch (0/* FIXME */) { |
| 591 | case PC_to_RDR_IccPowerOn: |
| 592 | case PC_to_RDR_XfrBlock: |
| 593 | case PC_to_RDR_Escape: |
| 594 | case PC_to_RDR_Secure: |
| 595 | case PC_to_RDR_Mechanical: |
| 596 | //case PC_to_RDR_Abort: /* seriously? WTF! */ |
| 597 | break; |
| 598 | default: |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 599 | LOGP(DCCID, LOGL_ERROR, "Abort for non-Abortable Message Type\n"); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 600 | /* CCID spec lists CMD_NOT_ABORTED, but gives no numberic value ?!? */ |
| 601 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED); |
| 602 | return ccid_slot_send_unbusy(cs, resp); |
| 603 | } |
| 604 | |
| 605 | /* FIXME */ |
| 606 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
| 607 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /* Section 6.1.14 */ |
| 611 | static int ccid_handle_set_rate_and_clock(struct ccid_slot *cs, struct msgb *msg) |
| 612 | { |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 613 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 614 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 615 | uint8_t seq = u->set_rate_and_clock.hdr.bSeq; |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 616 | uint32_t freq_hz = osmo_load32le(&u->set_rate_and_clock.dwClockFrequency); |
| 617 | uint32_t rate_bps = osmo_load32le(&u->set_rate_and_clock.dwDataRate); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 618 | struct msgb *resp; |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 619 | int rc; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 620 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 621 | /* FIXME: which rate to return in failure case? */ |
| 622 | rc = cs->ci->slot_ops->set_rate_and_clock(cs, freq_hz, rate_bps); |
| 623 | if (rc < 0) |
| 624 | resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_FAILED, -rc, 9600, 2500000); |
| 625 | else |
| 626 | resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_OK, 0, rate_bps, freq_hz); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 627 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 628 | } |
| 629 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 630 | /*! Handle data arriving from the host on the OUT endpoint. |
| 631 | * \param[in] cs CCID Instance on which to operate |
Harald Welte | e6ac416 | 2019-05-16 11:18:00 +0200 | [diff] [blame] | 632 | * \param[in] msgb received message buffer containing one CCID OUT EP message from the host. |
| 633 | * Ownership of message buffer is transferred, i.e. it's our job to msgb_free() |
| 634 | * it eventually, after we're done with it (could be asynchronously). |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 635 | * \returns 0 on success; negative on error */ |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 636 | int ccid_handle_out(struct ccid_instance *ci, struct msgb *msg) |
| 637 | { |
| 638 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
| 639 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 640 | unsigned int len = msgb_length(msg); |
| 641 | struct ccid_slot *cs; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 642 | struct msgb *resp; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 643 | int rc; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 644 | |
| 645 | if (len < sizeof(*ch)) { |
| 646 | /* FIXME */ |
| 647 | return -1; |
| 648 | } |
| 649 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 650 | /* Check for invalid slot number */ |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 651 | cs = get_ccid_slot(ci, ch->bSlot); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 652 | if (!cs) { |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 653 | LOGPCI(ci, LOGL_ERROR, "Invalid bSlot %u\n", ch->bSlot); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 654 | resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq, 5); |
| 655 | return ccid_send(ci, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 656 | } |
| 657 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 658 | /* Check if slot is already busy; Reject any additional commands meanwhile */ |
| 659 | if (cs->cmd_busy) { |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 660 | LOGPCS(cs, LOGL_ERROR, "Slot Busy, but another cmd received\n"); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 661 | /* FIXME: ABORT logic as per section 5.3.1 of CCID Spec v1.1 */ |
| 662 | resp = gen_err_resp(ch->bMessageType, ch->bSlot, get_icc_status(cs), ch->bSeq, |
| 663 | CCID_ERR_CMD_SLOT_BUSY); |
| 664 | return ccid_send(ci, resp); |
| 665 | } |
| 666 | |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 667 | LOGPCS(cs, LOGL_DEBUG, "Rx CCID(OUT) %s %s\n", |
| 668 | get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg)); |
| 669 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 670 | /* we're now processing a command for the slot; mark slot as busy */ |
| 671 | cs->cmd_busy = true; |
| 672 | |
| 673 | /* TODO: enqueue into the per-slot specific input queue */ |
| 674 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 675 | /* call pre-processing call-back function; allows reader to update state */ |
| 676 | if (ci->slot_ops->pre_proc_cb) |
| 677 | ci->slot_ops->pre_proc_cb(cs, msg); |
| 678 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 679 | switch (ch->bMessageType) { |
| 680 | case PC_to_RDR_GetSlotStatus: |
Eric Wild | 33403af | 2019-10-01 15:01:58 +0200 | [diff] [blame] | 681 | if (len < sizeof(u->get_slot_status)) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 682 | goto short_msg; |
| 683 | rc = ccid_handle_get_slot_status(cs, msg); |
| 684 | break; |
| 685 | case PC_to_RDR_IccPowerOn: |
| 686 | if (len != sizeof(u->icc_power_on)) |
| 687 | goto short_msg; |
| 688 | rc = ccid_handle_icc_power_on(cs, msg); |
| 689 | break; |
| 690 | case PC_to_RDR_IccPowerOff: |
| 691 | if (len != sizeof(u->icc_power_off)) |
| 692 | goto short_msg; |
| 693 | rc = ccid_handle_icc_power_off(cs, msg); |
| 694 | break; |
| 695 | case PC_to_RDR_XfrBlock: |
| 696 | if (len < sizeof(u->xfr_block)) |
| 697 | goto short_msg; |
| 698 | rc = ccid_handle_xfr_block(cs, msg); |
| 699 | break; |
| 700 | case PC_to_RDR_GetParameters: |
| 701 | if (len != sizeof(u->get_parameters)) |
| 702 | goto short_msg; |
| 703 | rc = ccid_handle_get_parameters(cs, msg); |
| 704 | break; |
| 705 | case PC_to_RDR_ResetParameters: |
| 706 | if (len != sizeof(u->reset_parameters)) |
| 707 | goto short_msg; |
| 708 | rc = ccid_handle_reset_parameters(cs, msg); |
| 709 | break; |
| 710 | case PC_to_RDR_SetParameters: |
Eric Wild | 33403af | 2019-10-01 15:01:58 +0200 | [diff] [blame] | 711 | // smallest union member |
| 712 | if (len < (sizeof(u->set_parameters.abProtocolData.t0)+10)) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 713 | goto short_msg; |
| 714 | rc = ccid_handle_set_parameters(cs, msg); |
| 715 | break; |
| 716 | case PC_to_RDR_Escape: |
| 717 | if (len < sizeof(u->escape)) |
| 718 | goto short_msg; |
| 719 | rc = ccid_handle_escape(cs, msg); |
| 720 | break; |
| 721 | case PC_to_RDR_IccClock: |
| 722 | if (len != sizeof(u->icc_clock)) |
| 723 | goto short_msg; |
| 724 | rc = ccid_handle_icc_clock(cs, msg); |
| 725 | break; |
| 726 | case PC_to_RDR_T0APDU: |
| 727 | if (len != /*FIXME*/ sizeof(u->t0apdu)) |
| 728 | goto short_msg; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 729 | rc = ccid_handle_t0apdu(cs, msg); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 730 | break; |
| 731 | case PC_to_RDR_Secure: |
| 732 | if (len < sizeof(u->secure)) |
| 733 | goto short_msg; |
| 734 | rc = ccid_handle_secure(cs, msg); |
| 735 | break; |
| 736 | case PC_to_RDR_Mechanical: |
| 737 | if (len != sizeof(u->mechanical)) |
| 738 | goto short_msg; |
| 739 | rc = ccid_handle_mechanical(cs, msg); |
| 740 | break; |
| 741 | case PC_to_RDR_Abort: |
| 742 | if (len != sizeof(u->abort)) |
| 743 | goto short_msg; |
| 744 | rc = ccid_handle_abort(cs, msg); |
| 745 | break; |
| 746 | case PC_to_RDR_SetDataRateAndClockFrequency: |
| 747 | if (len != sizeof(u->set_rate_and_clock)) |
| 748 | goto short_msg; |
| 749 | rc = ccid_handle_set_rate_and_clock(cs, msg); |
| 750 | break; |
| 751 | default: |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 752 | /* generic response with bERror = 0 (command not supported) */ |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 753 | LOGP(DCCID, LOGL_NOTICE, "Unknown CCID Message received: 0x%02x\n", ch->bMessageType); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 754 | resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq, |
| 755 | CCID_ERR_CMD_NOT_SUPPORTED); |
Harald Welte | 922ff93 | 2019-05-16 11:21:51 +0200 | [diff] [blame] | 756 | msgb_free(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 757 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 758 | } |
Harald Welte | 922ff93 | 2019-05-16 11:21:51 +0200 | [diff] [blame] | 759 | /* the various ccid_handle_* functions can return '1' to tell us that they took ownership |
| 760 | * of the msgb */ |
| 761 | if (rc != 1) |
| 762 | msgb_free(msg); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 763 | return 0; |
| 764 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 765 | short_msg: |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 766 | LOGP(DCCID, LOGL_ERROR, "Short CCID message received: %s; ignoring\n", msgb_hexdump(msg)); |
Harald Welte | 922ff93 | 2019-05-16 11:21:51 +0200 | [diff] [blame] | 767 | msgb_free(msg); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 768 | return -1; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 769 | } |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 770 | |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 771 | /* Section 5.3.1 ABORT */ |
| 772 | static int ccid_handle_ctrl_abort(struct ccid_instance *ci, const struct _usb_ctrl_req *req) |
| 773 | { |
| 774 | uint16_t w_value = osmo_load16le(&req->wValue); |
| 775 | uint8_t slot_nr = w_value & 0xff; |
| 776 | uint8_t seq = w_value >> 8; |
| 777 | struct ccid_slot *cs; |
| 778 | |
| 779 | if (slot_nr >= ARRAY_SIZE(ci->slot)) |
| 780 | return CCID_CTRL_RET_INVALID; |
| 781 | |
| 782 | cs = &ci->slot[slot_nr]; |
| 783 | |
| 784 | LOGP(DCCID, LOGL_NOTICE, "Not handling PC_to_RDR_Abort; please implement it\n"); |
| 785 | /* Upon receiving the Control pipe ABORT request the CCID should check |
| 786 | * the state of the requested slot. */ |
| 787 | |
| 788 | /* If the last Bulk-OUT message received by the CCID was a |
| 789 | * PC_to_RDR_Abort command with the same bSlot and bSeq as the ABORT |
| 790 | * request, then the CCID will respond to the Bulk-OUT message with |
| 791 | * the RDR_to_PC_SlotStatus response. */ |
| 792 | |
| 793 | /* FIXME */ |
| 794 | |
| 795 | /* If the previous Bulk-OUT message received by the CCID was not a |
| 796 | * PC_to_RDR_Abort command with the same bSlot and bSeq as the ABORT |
| 797 | * request, then the CCID will fail all Bulk-Out commands to that slot |
| 798 | * until the PC_to_RDR_Abort command with the same bSlot and bSeq is |
| 799 | * received. Bulk-OUT commands will be failed by sending a response |
| 800 | * with bmCommandStatus=Failed and bError=CMD_ABORTED. */ |
| 801 | |
| 802 | /* FIXME */ |
| 803 | return CCID_CTRL_RET_OK; |
| 804 | } |
| 805 | |
| 806 | /* Section 5.3.2 GET_CLOCK_FREQUENCIES */ |
| 807 | static int ccid_handle_ctrl_get_clock_freq(struct ccid_instance *ci, const struct _usb_ctrl_req *req, |
| 808 | const uint8_t **data_in) |
| 809 | { |
| 810 | uint16_t len = osmo_load16le(&req->wLength); |
| 811 | |
| 812 | if (len != sizeof(uint32_t) * ci->class_desc->bNumClockSupported) |
| 813 | return CCID_CTRL_RET_INVALID; |
| 814 | |
| 815 | *data_in = (const uint8_t *) ci->clock_freqs; |
| 816 | return CCID_CTRL_RET_OK; |
| 817 | } |
| 818 | |
| 819 | /* Section 5.3.3 GET_DATA_RATES */ |
| 820 | static int ccid_handle_ctrl_get_data_rates(struct ccid_instance *ci, const struct _usb_ctrl_req *req, |
| 821 | const uint8_t **data_in) |
| 822 | { |
| 823 | uint16_t len = osmo_load16le(&req->wLength); |
| 824 | |
| 825 | if (len != sizeof(uint32_t) * ci->class_desc->bNumClockSupported) |
| 826 | return CCID_CTRL_RET_INVALID; |
| 827 | |
| 828 | *data_in = (const uint8_t *) ci->data_rates; |
| 829 | return CCID_CTRL_RET_OK; |
| 830 | } |
| 831 | |
| 832 | /*! Handle [class specific] CTRL request. We assume the caller has already verified that the |
| 833 | * request was made to the correct interface as well as it is a class-specific request. |
| 834 | * \param[in] ci CCID Instance for which CTRL request was received |
| 835 | * \param[in] ctrl_req buffer holding the 8 bytes CTRL transfer header |
| 836 | * \param[out] data_in data to be returned to the host in the IN transaction (if any) |
| 837 | * \returns CCID_CTRL_RET_OK, CCID_CTRL_RET_INVALID or CCID_CTRL_RET_UNKNOWN |
| 838 | */ |
| 839 | int ccid_handle_ctrl(struct ccid_instance *ci, const uint8_t *ctrl_req, const uint8_t **data_in) |
| 840 | { |
| 841 | const struct _usb_ctrl_req *req = (const struct _usb_ctrl_req *) ctrl_req; |
| 842 | int rc; |
| 843 | |
| 844 | LOGPCI(ci, LOGL_DEBUG, "CTRL bmReqT=0x%02X bRequest=%s, wValue=0x%04X, wIndex=0x%04X, wLength=%d\n", |
| 845 | req->bRequestType, get_value_string(ccid_class_spec_req_vals, req->bRequest), |
| 846 | req->wValue, req->wIndex, req->wLength); |
| 847 | |
| 848 | switch (req->bRequest) { |
| 849 | case CLASS_SPEC_CCID_ABORT: |
| 850 | rc = ccid_handle_ctrl_abort(ci, req); |
| 851 | break; |
| 852 | case CLASS_SPEC_CCID_GET_CLOCK_FREQ: |
| 853 | rc = ccid_handle_ctrl_get_clock_freq(ci, req, data_in); |
| 854 | break; |
| 855 | case CLASS_SPEC_CCID_GET_DATA_RATES: |
| 856 | rc = ccid_handle_ctrl_get_data_rates(ci, req, data_in); |
| 857 | break; |
| 858 | default: |
| 859 | return CCID_CTRL_RET_UNKNOWN; |
| 860 | } |
| 861 | return rc; |
| 862 | } |
| 863 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 864 | void ccid_instance_init(struct ccid_instance *ci, const struct ccid_ops *ops, |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 865 | const struct ccid_slot_ops *slot_ops, |
| 866 | const struct usb_ccid_class_descriptor *class_desc, |
| 867 | const uint32_t *data_rates, const uint32_t *clock_freqs, |
| 868 | const char *name, void *priv) |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 869 | { |
| 870 | int i; |
| 871 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 872 | ci->ops = ops; |
| 873 | ci->slot_ops = slot_ops; |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 874 | ci->class_desc = class_desc; |
| 875 | ci->clock_freqs = clock_freqs; |
| 876 | ci->data_rates = data_rates; |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 877 | ci->name = name; |
| 878 | ci->priv = priv; |
Harald Welte | d338507 | 2019-05-16 17:38:23 +0200 | [diff] [blame] | 879 | |
| 880 | for (i = 0; i < ARRAY_SIZE(ci->slot); i++) { |
| 881 | struct ccid_slot *cs = &ci->slot[i]; |
| 882 | cs->slot_nr = i; |
| 883 | cs->ci = ci; |
| 884 | |
| 885 | slot_ops->init(cs); |
| 886 | } |
| 887 | |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 888 | } |