| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | #include <stdio.h> |
|---|
| 22 | #include <stdlib.h> |
|---|
| 23 | #include <assert.h> |
|---|
| 24 | #include <string.h> |
|---|
| 25 | |
|---|
| 26 | #include "internal.h" |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | static size_t sc_apdu_get_length(const sc_apdu_t *apdu, unsigned int proto) |
|---|
| 38 | { |
|---|
| 39 | size_t ret = 4; |
|---|
| 40 | |
|---|
| 41 | switch (apdu->cse) { |
|---|
| 42 | case SC_APDU_CASE_1: |
|---|
| 43 | if (proto == SC_PROTO_T0) |
|---|
| 44 | ret++; |
|---|
| 45 | break; |
|---|
| 46 | case SC_APDU_CASE_2_SHORT: |
|---|
| 47 | ret++; |
|---|
| 48 | break; |
|---|
| 49 | case SC_APDU_CASE_2_EXT: |
|---|
| 50 | ret += (proto == SC_PROTO_T0 ? 1 : 3); |
|---|
| 51 | break; |
|---|
| 52 | case SC_APDU_CASE_3_SHORT: |
|---|
| 53 | ret += 1 + apdu->lc; |
|---|
| 54 | break; |
|---|
| 55 | case SC_APDU_CASE_3_EXT: |
|---|
| 56 | ret += apdu->lc + (proto == SC_PROTO_T0 ? 1 : 3); |
|---|
| 57 | break; |
|---|
| 58 | case SC_APDU_CASE_4_SHORT: |
|---|
| 59 | ret += apdu->lc + (proto != SC_PROTO_T0 ? 2 : 1); |
|---|
| 60 | break; |
|---|
| 61 | case SC_APDU_CASE_4_EXT: |
|---|
| 62 | ret += apdu->lc + (proto == SC_PROTO_T0 ? 1 : 5); |
|---|
| 63 | break; |
|---|
| 64 | default: |
|---|
| 65 | return 0; |
|---|
| 66 | } |
|---|
| 67 | return ret; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | static int sc_apdu2bytes(sc_context_t *ctx, const sc_apdu_t *apdu, |
|---|
| 79 | unsigned int proto, u8 *out, size_t outlen) |
|---|
| 80 | { |
|---|
| 81 | u8 *p = out; |
|---|
| 82 | |
|---|
| 83 | size_t len = sc_apdu_get_length(apdu, proto); |
|---|
| 84 | |
|---|
| 85 | if (out == NULL || outlen < len) |
|---|
| 86 | return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 87 | |
|---|
| 88 | *p++ = apdu->cla; |
|---|
| 89 | *p++ = apdu->ins; |
|---|
| 90 | *p++ = apdu->p1; |
|---|
| 91 | *p++ = apdu->p2; |
|---|
| 92 | |
|---|
| 93 | switch (apdu->cse) { |
|---|
| 94 | case SC_APDU_CASE_1: |
|---|
| 95 | |
|---|
| 96 | if (proto == SC_PROTO_T0) |
|---|
| 97 | *p++ = (u8)0x00; |
|---|
| 98 | break; |
|---|
| 99 | case SC_APDU_CASE_2_SHORT: |
|---|
| 100 | *p++ = (u8)apdu->le; |
|---|
| 101 | break; |
|---|
| 102 | case SC_APDU_CASE_2_EXT: |
|---|
| 103 | if (proto == SC_PROTO_T0) |
|---|
| 104 | |
|---|
| 105 | *p++ = (u8)apdu->le; |
|---|
| 106 | else { |
|---|
| 107 | |
|---|
| 108 | *p++ = (u8)0x00; |
|---|
| 109 | *p++ = (u8)(apdu->le >> 8); |
|---|
| 110 | *p++ = (u8)apdu->le; |
|---|
| 111 | } |
|---|
| 112 | break; |
|---|
| 113 | case SC_APDU_CASE_3_SHORT: |
|---|
| 114 | *p++ = (u8)apdu->lc; |
|---|
| 115 | memcpy(p, apdu->data, apdu->lc); |
|---|
| 116 | p += apdu->lc; |
|---|
| 117 | break; |
|---|
| 118 | case SC_APDU_CASE_3_EXT: |
|---|
| 119 | if (proto == SC_PROTO_T0) { |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | if (apdu->lc > 255) { |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | sc_error(ctx, "invalid Lc length for CASE 3 " |
|---|
| 126 | "extended APDU (need ENVELOPE)"); |
|---|
| 127 | return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 128 | } |
|---|
| 129 | } else { |
|---|
| 130 | |
|---|
| 131 | *p++ = (u8)0x00; |
|---|
| 132 | *p++ = (u8)(apdu->lc >> 8); |
|---|
| 133 | *p++ = (u8)apdu->lc; |
|---|
| 134 | } |
|---|
| 135 | memcpy(p, apdu->data, apdu->lc); |
|---|
| 136 | p += apdu->lc; |
|---|
| 137 | break; |
|---|
| 138 | case SC_APDU_CASE_4_SHORT: |
|---|
| 139 | *p++ = (u8)apdu->lc; |
|---|
| 140 | memcpy(p, apdu->data, apdu->lc); |
|---|
| 141 | p += apdu->lc; |
|---|
| 142 | |
|---|
| 143 | if (proto != SC_PROTO_T0) |
|---|
| 144 | *p++ = (u8)apdu->le; |
|---|
| 145 | break; |
|---|
| 146 | case SC_APDU_CASE_4_EXT: |
|---|
| 147 | if (proto == SC_PROTO_T0) { |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | *p++ = (u8)apdu->lc; |
|---|
| 152 | memcpy(p, apdu->data, apdu->lc); |
|---|
| 153 | p += apdu->lc & 0xff; |
|---|
| 154 | } else { |
|---|
| 155 | *p++ = (u8)0x00; |
|---|
| 156 | *p++ = (u8)(apdu->lc >> 8); |
|---|
| 157 | *p++ = (u8)apdu->lc; |
|---|
| 158 | memcpy(p, apdu->data, apdu->lc); |
|---|
| 159 | p += apdu->lc; |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | *p++ = (u8)(apdu->le >> 8); |
|---|
| 163 | *p++ = (u8)apdu->le; |
|---|
| 164 | } |
|---|
| 165 | break; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | return SC_SUCCESS; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | void sc_apdu_log(sc_context_t *ctx, const u8 *data, size_t len, int is_out) |
|---|
| 172 | { |
|---|
| 173 | size_t blen = len * 5 + 128; |
|---|
| 174 | char *buf = malloc(blen); |
|---|
| 175 | if (buf == NULL) |
|---|
| 176 | return; |
|---|
| 177 | |
|---|
| 178 | sc_hex_dump(ctx, data, len, buf, blen); |
|---|
| 179 | |
|---|
| 180 | sc_debug(ctx, "\n%s APDU data [%5u bytes] =====================================\n" |
|---|
| 181 | "%s" |
|---|
| 182 | "======================================================================\n", |
|---|
| 183 | is_out != 0 ? "Outgoing" : "Incoming", len, |
|---|
| 184 | buf); |
|---|
| 185 | free(buf); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | int sc_apdu_get_octets(sc_context_t *ctx, const sc_apdu_t *apdu, u8 **buf, |
|---|
| 189 | size_t *len, unsigned int proto) |
|---|
| 190 | { |
|---|
| 191 | size_t nlen; |
|---|
| 192 | u8 *nbuf; |
|---|
| 193 | |
|---|
| 194 | if (apdu == NULL || buf == NULL || len == NULL) |
|---|
| 195 | return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | nlen = sc_apdu_get_length(apdu, proto); |
|---|
| 199 | if (nlen == 0) |
|---|
| 200 | return SC_ERROR_INTERNAL; |
|---|
| 201 | nbuf = malloc(nlen); |
|---|
| 202 | if (nbuf == NULL) |
|---|
| 203 | return SC_ERROR_MEMORY_FAILURE; |
|---|
| 204 | |
|---|
| 205 | if (sc_apdu2bytes(ctx, apdu, proto, nbuf, nlen) != SC_SUCCESS) |
|---|
| 206 | return SC_ERROR_INTERNAL; |
|---|
| 207 | *buf = nbuf; |
|---|
| 208 | *len = nlen; |
|---|
| 209 | |
|---|
| 210 | return SC_SUCCESS; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | int sc_apdu_set_resp(sc_context_t *ctx, sc_apdu_t *apdu, const u8 *buf, |
|---|
| 214 | size_t len) |
|---|
| 215 | { |
|---|
| 216 | if (len < 2) { |
|---|
| 217 | |
|---|
| 218 | sc_error(ctx, "invalid response: SW1 SW2 missing"); |
|---|
| 219 | return SC_ERROR_INTERNAL; |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | apdu->sw1 = (unsigned int)buf[len - 2]; |
|---|
| 224 | apdu->sw2 = (unsigned int)buf[len - 1]; |
|---|
| 225 | len -= 2; |
|---|
| 226 | |
|---|
| 227 | if (len <= apdu->resplen) |
|---|
| 228 | apdu->resplen = len; |
|---|
| 229 | |
|---|
| 230 | if (apdu->resplen != 0) |
|---|
| 231 | memcpy(apdu->resp, buf, apdu->resplen); |
|---|
| 232 | |
|---|
| 233 | return SC_SUCCESS; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | static int sc_check_apdu(sc_card_t *card, const sc_apdu_t *apdu) |
|---|
| 267 | { |
|---|
| 268 | if ((apdu->cse & ~SC_APDU_SHORT_MASK) == 0) { |
|---|
| 269 | |
|---|
| 270 | if (apdu->le > 256 || (apdu->lc > 255 && |
|---|
| 271 | (apdu->flags & SC_APDU_FLAGS_CHAINING) == 0)) |
|---|
| 272 | goto error; |
|---|
| 273 | } else if ((apdu->cse & SC_APDU_EXT) != 0) { |
|---|
| 274 | |
|---|
| 275 | if ((card->caps & SC_CARD_CAP_APDU_EXT) == 0) { |
|---|
| 276 | sc_error(card->ctx, "card doesn't support extended APDUs"); |
|---|
| 277 | goto error; |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | if (apdu->le > 65536 || apdu->lc > 65535) |
|---|
| 281 | goto error; |
|---|
| 282 | } else |
|---|
| 283 | goto error; |
|---|
| 284 | |
|---|
| 285 | switch (apdu->cse & SC_APDU_SHORT_MASK) { |
|---|
| 286 | case SC_APDU_CASE_1: |
|---|
| 287 | |
|---|
| 288 | if (apdu->datalen != 0 || apdu->lc != 0 || apdu->le != 0) |
|---|
| 289 | goto error; |
|---|
| 290 | break; |
|---|
| 291 | case SC_APDU_CASE_2_SHORT: |
|---|
| 292 | |
|---|
| 293 | if (apdu->datalen != 0 || apdu->lc != 0) |
|---|
| 294 | goto error; |
|---|
| 295 | |
|---|
| 296 | if (apdu->le == 0 || apdu->resplen == 0 || apdu->resp == NULL) |
|---|
| 297 | goto error; |
|---|
| 298 | |
|---|
| 299 | if (apdu->resplen < apdu->le) |
|---|
| 300 | goto error; |
|---|
| 301 | break; |
|---|
| 302 | case SC_APDU_CASE_3_SHORT: |
|---|
| 303 | |
|---|
| 304 | if (apdu->datalen == 0 || apdu->data == NULL || apdu->lc == 0) |
|---|
| 305 | goto error; |
|---|
| 306 | |
|---|
| 307 | if (apdu->le != 0) |
|---|
| 308 | goto error; |
|---|
| 309 | |
|---|
| 310 | if (apdu->datalen != apdu->lc) |
|---|
| 311 | goto error; |
|---|
| 312 | break; |
|---|
| 313 | case SC_APDU_CASE_4_SHORT: |
|---|
| 314 | |
|---|
| 315 | if (apdu->datalen == 0 || apdu->data == NULL || apdu->lc == 0) |
|---|
| 316 | goto error; |
|---|
| 317 | |
|---|
| 318 | if (apdu->le == 0 || apdu->resplen == 0 || apdu->resp == NULL) |
|---|
| 319 | goto error; |
|---|
| 320 | |
|---|
| 321 | if (apdu->resplen < apdu->le) |
|---|
| 322 | goto error; |
|---|
| 323 | |
|---|
| 324 | if (apdu->datalen != apdu->lc) |
|---|
| 325 | goto error; |
|---|
| 326 | break; |
|---|
| 327 | default: |
|---|
| 328 | sc_error(card->ctx, "Invalid APDU case %d\n", apdu->cse); |
|---|
| 329 | return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 330 | } |
|---|
| 331 | return SC_SUCCESS; |
|---|
| 332 | error: |
|---|
| 333 | sc_error(card->ctx, "Invalid Case %d %s APDU:\n" |
|---|
| 334 | "cse=%02x cla=%02x ins=%02x p1=%02x p2=%02x lc=%lu le=%lu\n" |
|---|
| 335 | "resp=%p resplen=%lu data=%p datalen=%lu", |
|---|
| 336 | apdu->cse & SC_APDU_SHORT_MASK, |
|---|
| 337 | (apdu->cse & SC_APDU_EXT) != 0 ? "extended" : "short", |
|---|
| 338 | apdu->cse, apdu->cla, apdu->ins, apdu->p1, apdu->p2, |
|---|
| 339 | (unsigned long) apdu->lc, (unsigned long) apdu->le, |
|---|
| 340 | apdu->resp, (unsigned long) apdu->resplen, |
|---|
| 341 | apdu->data, (unsigned long) apdu->datalen); |
|---|
| 342 | return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | static void sc_detect_apdu_cse(const sc_card_t *card, sc_apdu_t *apdu) |
|---|
| 350 | { |
|---|
| 351 | if (apdu->cse == SC_APDU_CASE_2 || apdu->cse == SC_APDU_CASE_3 || |
|---|
| 352 | apdu->cse == SC_APDU_CASE_4) { |
|---|
| 353 | int btype = apdu->cse & SC_APDU_SHORT_MASK; |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | if ((apdu->le > 256 || (apdu->lc > 255 && (apdu->flags & SC_APDU_FLAGS_CHAINING) == 0)) && |
|---|
| 359 | (card->caps & SC_CARD_CAP_APDU_EXT) != 0) |
|---|
| 360 | btype |= SC_APDU_EXT; |
|---|
| 361 | apdu->cse = btype; |
|---|
| 362 | } |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | static int do_single_transmit(sc_card_t *card, sc_apdu_t *apdu) |
|---|
| 373 | { |
|---|
| 374 | int r; |
|---|
| 375 | size_t olen = apdu->resplen; |
|---|
| 376 | sc_context_t *ctx = card->ctx; |
|---|
| 377 | |
|---|
| 378 | |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | |
|---|
| 389 | |
|---|
| 390 | if (card->reader->ops->transmit == NULL) |
|---|
| 391 | return SC_ERROR_NOT_SUPPORTED; |
|---|
| 392 | r = card->reader->ops->transmit(card->reader, card->slot, apdu); |
|---|
| 393 | if (r != 0) { |
|---|
| 394 | sc_error(ctx, "unable to transmit APDU"); |
|---|
| 395 | return r; |
|---|
| 396 | } |
|---|
| 397 | |
|---|
| 398 | |
|---|
| 399 | |
|---|
| 400 | |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | if (apdu->sw1 == 0x6C && (apdu->flags & SC_APDU_FLAGS_NO_RETRY_WL) == 0) { |
|---|
| 404 | size_t nlen = apdu->sw2 != 0 ? (size_t)apdu->sw2 : 256; |
|---|
| 405 | if (olen >= nlen) { |
|---|
| 406 | |
|---|
| 407 | apdu->flags |= SC_APDU_FLAGS_NO_GET_RESP; |
|---|
| 408 | |
|---|
| 409 | apdu->resplen = olen; |
|---|
| 410 | apdu->le = nlen; |
|---|
| 411 | |
|---|
| 412 | |
|---|
| 413 | |
|---|
| 414 | if (card->wait_resend_apdu != 0) |
|---|
| 415 | msleep(card->wait_resend_apdu); |
|---|
| 416 | |
|---|
| 417 | r = card->reader->ops->transmit(card->reader, card->slot, apdu); |
|---|
| 418 | if (r != SC_SUCCESS) { |
|---|
| 419 | sc_error(ctx, "unable to transmit APDU"); |
|---|
| 420 | return r; |
|---|
| 421 | } |
|---|
| 422 | } else { |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | sc_debug(ctx, "wrong length: required length exceeds resplen"); |
|---|
| 426 | return SC_ERROR_WRONG_LENGTH; |
|---|
| 427 | } |
|---|
| 428 | } |
|---|
| 429 | |
|---|
| 430 | |
|---|
| 431 | |
|---|
| 432 | |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | if (apdu->sw1 == 0x61 && (apdu->flags & SC_APDU_FLAGS_NO_GET_RESP) == 0) { |
|---|
| 436 | if (apdu->le == 0) { |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | apdu->sw1 = 0x90; |
|---|
| 442 | apdu->sw2 = 0x00; |
|---|
| 443 | |
|---|
| 444 | } else { |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | |
|---|
| 448 | |
|---|
| 449 | size_t le, minlen, buflen; |
|---|
| 450 | u8 *buf; |
|---|
| 451 | |
|---|
| 452 | if (card->ops->get_response == NULL) { |
|---|
| 453 | |
|---|
| 454 | sc_error(ctx, "no GET RESPONSE command\n"); |
|---|
| 455 | return SC_ERROR_NOT_SUPPORTED; |
|---|
| 456 | } |
|---|
| 457 | |
|---|
| 458 | |
|---|
| 459 | |
|---|
| 460 | |
|---|
| 461 | buf = apdu->resp + apdu->resplen; |
|---|
| 462 | |
|---|
| 463 | |
|---|
| 464 | |
|---|
| 465 | |
|---|
| 466 | buflen = olen - apdu->resplen; |
|---|
| 467 | |
|---|
| 468 | |
|---|
| 469 | le = apdu->sw2 != 0 ? (size_t)apdu->sw2 : 256; |
|---|
| 470 | |
|---|
| 471 | |
|---|
| 472 | minlen = le; |
|---|
| 473 | |
|---|
| 474 | do { |
|---|
| 475 | u8 tbuf[256]; |
|---|
| 476 | |
|---|
| 477 | |
|---|
| 478 | |
|---|
| 479 | r = card->ops->get_response(card, &le, tbuf); |
|---|
| 480 | if (r < 0) |
|---|
| 481 | SC_FUNC_RETURN(ctx, 2, r); |
|---|
| 482 | |
|---|
| 483 | if (buflen < le) |
|---|
| 484 | return SC_ERROR_WRONG_LENGTH; |
|---|
| 485 | |
|---|
| 486 | memcpy(buf, tbuf, le); |
|---|
| 487 | buf += le; |
|---|
| 488 | buflen -= le; |
|---|
| 489 | |
|---|
| 490 | minlen -= le; |
|---|
| 491 | if (r != 0) |
|---|
| 492 | le = minlen = (size_t)r; |
|---|
| 493 | else |
|---|
| 494 | |
|---|
| 495 | |
|---|
| 496 | |
|---|
| 497 | le = minlen; |
|---|
| 498 | } while (r != 0 || minlen != 0); |
|---|
| 499 | |
|---|
| 500 | apdu->resplen = buf - apdu->resp; |
|---|
| 501 | apdu->sw1 = 0x90; |
|---|
| 502 | apdu->sw2 = 0x00; |
|---|
| 503 | } |
|---|
| 504 | } |
|---|
| 505 | |
|---|
| 506 | return SC_SUCCESS; |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | int sc_transmit_apdu(sc_card_t *card, sc_apdu_t *apdu) |
|---|
| 510 | { |
|---|
| 511 | int r = SC_SUCCESS; |
|---|
| 512 | |
|---|
| 513 | if (card == NULL || apdu == NULL) |
|---|
| 514 | return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 515 | |
|---|
| 516 | SC_FUNC_CALLED(card->ctx, 4); |
|---|
| 517 | |
|---|
| 518 | |
|---|
| 519 | |
|---|
| 520 | sc_detect_apdu_cse(card, apdu); |
|---|
| 521 | |
|---|
| 522 | r = sc_check_apdu(card, apdu); |
|---|
| 523 | if (r != SC_SUCCESS) |
|---|
| 524 | return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 525 | |
|---|
| 526 | r = sc_lock(card); |
|---|
| 527 | if (r != SC_SUCCESS) { |
|---|
| 528 | sc_error(card->ctx, "unable to acquire lock"); |
|---|
| 529 | return r; |
|---|
| 530 | } |
|---|
| 531 | |
|---|
| 532 | if ((apdu->flags & SC_APDU_FLAGS_CHAINING) != 0) { |
|---|
| 533 | |
|---|
| 534 | |
|---|
| 535 | size_t len = apdu->datalen; |
|---|
| 536 | const u8 *buf = apdu->data; |
|---|
| 537 | |
|---|
| 538 | while (len != 0) { |
|---|
| 539 | size_t plen; |
|---|
| 540 | sc_apdu_t tapdu; |
|---|
| 541 | int last = 0; |
|---|
| 542 | |
|---|
| 543 | tapdu = *apdu; |
|---|
| 544 | |
|---|
| 545 | tapdu.flags &= ~SC_APDU_FLAGS_CHAINING; |
|---|
| 546 | if (len > 255) { |
|---|
| 547 | |
|---|
| 548 | |
|---|
| 549 | if ((tapdu.cse & SC_APDU_SHORT_MASK) == SC_APDU_CASE_4_SHORT) |
|---|
| 550 | tapdu.cse--; |
|---|
| 551 | |
|---|
| 552 | |
|---|
| 553 | plen = 255; |
|---|
| 554 | tapdu.cla |= 0x10; |
|---|
| 555 | tapdu.le = 0; |
|---|
| 556 | |
|---|
| 557 | tapdu.lc = 0; |
|---|
| 558 | tapdu.resplen = 0; |
|---|
| 559 | tapdu.resp = NULL; |
|---|
| 560 | } else { |
|---|
| 561 | plen = len; |
|---|
| 562 | last = 1; |
|---|
| 563 | } |
|---|
| 564 | tapdu.data = buf; |
|---|
| 565 | tapdu.datalen = tapdu.lc = plen; |
|---|
| 566 | |
|---|
| 567 | r = sc_check_apdu(card, &tapdu); |
|---|
| 568 | if (r != SC_SUCCESS) { |
|---|
| 569 | sc_error(card->ctx, "inconsistent APDU while chaining"); |
|---|
| 570 | break; |
|---|
| 571 | } |
|---|
| 572 | |
|---|
| 573 | r = do_single_transmit(card, &tapdu); |
|---|
| 574 | if (r != SC_SUCCESS) |
|---|
| 575 | break; |
|---|
| 576 | if (last != 0) { |
|---|
| 577 | |
|---|
| 578 | |
|---|
| 579 | apdu->sw1 = tapdu.sw1; |
|---|
| 580 | apdu->sw2 = tapdu.sw2; |
|---|
| 581 | apdu->resplen = tapdu.resplen; |
|---|
| 582 | } else { |
|---|
| 583 | |
|---|
| 584 | r = sc_check_sw(card, tapdu.sw1, tapdu.sw2); |
|---|
| 585 | if (r != SC_SUCCESS) |
|---|
| 586 | break; |
|---|
| 587 | } |
|---|
| 588 | len -= plen; |
|---|
| 589 | buf += plen; |
|---|
| 590 | } |
|---|
| 591 | } else |
|---|
| 592 | |
|---|
| 593 | r = do_single_transmit(card, apdu); |
|---|
| 594 | |
|---|
| 595 | if (sc_unlock(card) != SC_SUCCESS) |
|---|
| 596 | sc_error(card->ctx, "sc_unlock failed"); |
|---|
| 597 | |
|---|
| 598 | return r; |
|---|
| 599 | } |
|---|