Coverage Report

Created: 2025-12-14 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/apdu.c
Line
Count
Source
1
/*
2
 * apdu.c: basic APDU handling functions
3
 *
4
 * Copyright (C) 2005 Nils Larsch <nils@larsch.net>
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#ifdef HAVE_CONFIG_H
22
#include "config.h"
23
#endif
24
25
#include <stdio.h>
26
#include <stdlib.h>
27
#include <assert.h>
28
#include <string.h>
29
30
#include "internal.h"
31
#include "asn1.h"
32
33
/*********************************************************************/
34
/*   low level APDU handling functions                               */
35
/*********************************************************************/
36
37
/** Calculates the length of the encoded APDU in octets.
38
 *  @param  apdu   the APDU
39
 *  @param  proto  the desired protocol
40
 *  @return length of the encoded APDU
41
 */
42
size_t sc_apdu_get_length(const sc_apdu_t *apdu, unsigned int proto)
43
7.96k
{
44
7.96k
  size_t ret = 4;
45
46
7.96k
  switch (apdu->cse) {
47
0
  case SC_APDU_CASE_1:
48
0
    if (proto == SC_PROTO_T0)
49
0
      ret++;
50
0
    break;
51
7.96k
  case SC_APDU_CASE_2_SHORT:
52
7.96k
    ret++;
53
7.96k
    break;
54
0
  case SC_APDU_CASE_2_EXT:
55
0
    ret += (proto == SC_PROTO_T0 ? 1 : 3);
56
0
    break;
57
0
  case SC_APDU_CASE_3_SHORT:
58
0
    ret += 1 + apdu->lc;
59
0
    break;
60
0
  case SC_APDU_CASE_3_EXT:
61
0
    ret += apdu->lc + (proto == SC_PROTO_T0 ? 1 : 3);
62
0
    break;
63
0
  case SC_APDU_CASE_4_SHORT:
64
0
    ret += apdu->lc + (proto != SC_PROTO_T0 ? 2 : 1);
65
0
    break;
66
0
  case SC_APDU_CASE_4_EXT:
67
0
    ret += apdu->lc + (proto == SC_PROTO_T0 ? 1 : 5);
68
0
    break;
69
0
  default:
70
0
    return 0;
71
7.96k
  }
72
7.96k
  return ret;
73
7.96k
}
74
75
/** Encodes a APDU as an octet string
76
 *  @param  ctx     sc_context_t object (used for logging)
77
 *  @param  apdu    APDU to be encoded as an octet string
78
 *  @param  proto   protocol version to be used
79
 *  @param  out     output buffer of size outlen.
80
 *  @param  outlen  size of the output buffer
81
 *  @return SC_SUCCESS on success and an error code otherwise
82
 */
83
int sc_apdu2bytes(sc_context_t *ctx, const sc_apdu_t *apdu,
84
  unsigned int proto, u8 *out, size_t outlen)
85
3.98k
{
86
3.98k
  u8     *p = out;
87
88
3.98k
  size_t len = sc_apdu_get_length(apdu, proto);
89
90
3.98k
  if (out == NULL || outlen < len)
91
0
    return SC_ERROR_INVALID_ARGUMENTS;
92
  /* CLA, INS, P1 and P2 */
93
3.98k
  *p++ = apdu->cla;
94
3.98k
  *p++ = apdu->ins;
95
3.98k
  *p++ = apdu->p1;
96
3.98k
  *p++ = apdu->p2;
97
  /* case depend part */
98
3.98k
  switch (apdu->cse) {
99
0
  case SC_APDU_CASE_1:
100
    /* T0 needs an additional 0x00 byte */
101
0
    if (proto == SC_PROTO_T0)
102
0
      *p = (u8)0x00;
103
0
    break;
104
3.98k
  case SC_APDU_CASE_2_SHORT:
105
3.98k
    *p = (u8)apdu->le;
106
3.98k
    break;
107
0
  case SC_APDU_CASE_2_EXT:
108
0
    if (proto == SC_PROTO_T0)
109
      /* T0 extended APDUs look just like short APDUs */
110
0
      *p = (u8)apdu->le;
111
0
    else {
112
      /* in case of T1 always use 3 bytes for length */
113
0
      *p++ = (u8)0x00;
114
0
      *p++ = (u8)(apdu->le >> 8);
115
0
      *p = (u8)apdu->le;
116
0
    }
117
0
    break;
118
0
  case SC_APDU_CASE_3_SHORT:
119
0
    *p++ = (u8)apdu->lc;
120
0
    memcpy(p, apdu->data, apdu->lc);
121
0
    break;
122
0
  case SC_APDU_CASE_3_EXT:
123
0
    if (proto == SC_PROTO_T0) {
124
      /* in case of T0 the command is transmitted in chunks
125
       * < 255 using the ENVELOPE command ... */
126
0
      if (apdu->lc > 255) {
127
        /* ... so if Lc is greater than 255 bytes
128
         * an error has occurred on a higher level */
129
0
        sc_log(ctx, "invalid Lc length for CASE 3 extended APDU (need ENVELOPE)");
130
0
        return SC_ERROR_INVALID_ARGUMENTS;
131
0
      }
132
0
    }
133
0
    else {
134
      /* in case of T1 always use 3 bytes for length */
135
0
      *p++ = (u8)0x00;
136
0
      *p++ = (u8)(apdu->lc >> 8);
137
0
      *p++ = (u8)apdu->lc;
138
0
    }
139
0
    memcpy(p, apdu->data, apdu->lc);
140
0
    break;
141
0
  case SC_APDU_CASE_4_SHORT:
142
0
    *p++ = (u8)apdu->lc;
143
0
    memcpy(p, apdu->data, apdu->lc);
144
0
    p += apdu->lc;
145
    /* in case of T0 no Le byte is added */
146
0
    if (proto != SC_PROTO_T0)
147
0
      *p = (u8)apdu->le;
148
0
    break;
149
0
  case SC_APDU_CASE_4_EXT:
150
0
    if (proto == SC_PROTO_T0) {
151
      /* again a T0 extended case 4 APDU looks just
152
       * like a short APDU, the additional data is
153
       * transferred using ENVELOPE and GET RESPONSE */
154
0
      *p++ = (u8)apdu->lc;
155
0
      memcpy(p, apdu->data, apdu->lc);
156
0
    }
157
0
    else {
158
0
      *p++ = (u8)0x00;
159
0
      *p++ = (u8)(apdu->lc >> 8);
160
0
      *p++ = (u8)apdu->lc;
161
0
      memcpy(p, apdu->data, apdu->lc);
162
0
      p += apdu->lc;
163
      /* only 2 bytes are use to specify the length of the
164
       * expected data */
165
0
      *p++ = (u8)(apdu->le >> 8);
166
0
      *p = (u8)apdu->le;
167
0
    }
168
0
    break;
169
3.98k
  }
170
171
3.98k
  return SC_SUCCESS;
172
3.98k
}
173
174
int sc_apdu_get_octets(sc_context_t *ctx, const sc_apdu_t *apdu, u8 **buf,
175
  size_t *len, unsigned int proto)
176
3.98k
{
177
3.98k
  size_t  nlen;
178
3.98k
  u8  *nbuf;
179
180
3.98k
  if (apdu == NULL || buf == NULL || len == NULL)
181
0
    return SC_ERROR_INVALID_ARGUMENTS;
182
183
  /* get the estimated length of encoded APDU */
184
3.98k
  nlen = sc_apdu_get_length(apdu, proto);
185
3.98k
  if (nlen == 0)
186
0
    return SC_ERROR_INTERNAL;
187
3.98k
  nbuf = malloc(nlen);
188
3.98k
  if (nbuf == NULL)
189
0
    return SC_ERROR_OUT_OF_MEMORY;
190
  /* encode the APDU in the buffer */
191
3.98k
  if (sc_apdu2bytes(ctx, apdu, proto, nbuf, nlen) != SC_SUCCESS) {
192
0
    free(nbuf);
193
0
    return SC_ERROR_INTERNAL;
194
0
  }
195
3.98k
  *buf = nbuf;
196
3.98k
  *len = nlen;
197
198
3.98k
  return SC_SUCCESS;
199
3.98k
}
200
201
int sc_apdu_set_resp(sc_context_t *ctx, sc_apdu_t *apdu, const u8 *buf,
202
  size_t len)
203
0
{
204
0
  if (len < 2) {
205
    /* no SW1 SW2 ... something went terrible wrong */
206
0
    sc_log(ctx, "invalid response: SW1 SW2 missing");
207
0
    return SC_ERROR_INTERNAL;
208
0
  }
209
  /* set the SW1 and SW2 status bytes (the last two bytes of
210
   * the response */
211
0
  apdu->sw1 = (unsigned int)buf[len - 2];
212
0
  apdu->sw2 = (unsigned int)buf[len - 1];
213
0
  len -= 2;
214
  /* set output length and copy the returned data if necessary */
215
0
  if (len <= apdu->resplen)
216
0
    apdu->resplen = len;
217
218
0
  if (apdu->resplen != 0)
219
0
    memcpy(apdu->resp, buf, apdu->resplen);
220
221
0
  return SC_SUCCESS;
222
0
}
223
224
225
/*********************************************************************/
226
/*   higher level APDU transfer handling functions                   */
227
/*********************************************************************/
228
/*   +------------------+
229
 *   | sc_transmit_apdu |
230
 *   +------------------+
231
 *         |  |  |
232
 *         |  |  |     detect APDU cse               +--------------------+
233
 *         |  |  +---------------------------------> | sc_detect_apdu_cse |
234
 *         |  |                                      +--------------------+
235
 *         |  |        check consistency of APDU     +--------------------+
236
 *         |  +------------------------------------> | sc_check_apdu      |
237
 *         |                                         +--------------------+
238
 *         |           send single APDU              +--------------------+
239
 *         +---------------------------------------> | sc_transmit        |
240
 *                        ^                          +--------------------+
241
 *                        |                               |
242
 *                        |  re-transmit if wrong length  |
243
 *                        |       or GET RESPONSE         |
244
 *                        +-------------------------------+
245
 *                                                        |
246
 *                                                        v
247
 *                                               card->reader->ops->transmit
248
 */
249
250
/** basic consistency check of the sc_apdu_t object
251
 *  @param  ctx   sc_context_t object for error messages
252
 *  @param  apdu  sc_apdu_t object to check
253
 *  @return SC_SUCCESS on success and an error code otherwise
254
 */
255
int
256
sc_check_apdu(sc_card_t *card, const sc_apdu_t *apdu)
257
517k
{
258
517k
  if ((apdu->cse & ~SC_APDU_SHORT_MASK) == 0) {
259
    /* length check for short APDU */
260
515k
    if (apdu->le > 256 || (apdu->lc > 255 && (apdu->flags & SC_APDU_FLAGS_CHAINING) == 0))   {
261
14
      sc_log(card->ctx, "failed length check for short APDU");
262
14
      goto error;
263
14
    }
264
515k
  }
265
1.22k
  else if ((apdu->cse & SC_APDU_EXT) != 0) {
266
    /* check if the card supports extended APDUs */
267
1.22k
    if ((card->caps & SC_CARD_CAP_APDU_EXT) == 0) {
268
0
      sc_log(card->ctx, "card doesn't support extended APDUs");
269
0
      goto error;
270
0
    }
271
    /* length check for extended APDU */
272
1.22k
    if (apdu->le > 65536 || apdu->lc > 65535)   {
273
0
      sc_log(card->ctx, "failed length check for extended APDU");
274
0
      goto error;
275
0
    }
276
1.22k
  }
277
0
  else   {
278
0
    goto error;
279
0
  }
280
281
517k
  switch (apdu->cse & SC_APDU_SHORT_MASK) {
282
2.20k
  case SC_APDU_CASE_1:
283
    /* no data is sent or received */
284
2.20k
    if (apdu->datalen != 0 || apdu->lc != 0 || apdu->le != 0)
285
0
      goto error;
286
2.20k
    break;
287
165k
  case SC_APDU_CASE_2_SHORT:
288
    /* no data is sent */
289
165k
    if (apdu->datalen != 0 || apdu->lc != 0)
290
0
      goto error;
291
    /* data is expected       */
292
165k
    if (apdu->resplen == 0 || apdu->resp == NULL)
293
1
      goto error;
294
165k
    break;
295
165k
  case SC_APDU_CASE_3_SHORT:
296
    /* data is sent */
297
62.4k
    if (apdu->datalen == 0 || apdu->data == NULL || apdu->lc == 0)
298
1
      goto error;
299
    /* no data is expected    */
300
62.4k
    if (apdu->le != 0)
301
0
      goto error;
302
    /* inconsistent datalen   */
303
62.4k
    if (apdu->datalen != apdu->lc)
304
0
      goto error;
305
62.4k
    break;
306
287k
  case SC_APDU_CASE_4_SHORT:
307
    /* data is sent */
308
287k
    if (apdu->datalen == 0 || apdu->data == NULL || apdu->lc == 0)
309
8
      goto error;
310
    /* data is expected       */
311
287k
    if (apdu->resplen == 0 || apdu->resp == NULL)
312
0
      goto error;
313
    /* inconsistent datalen   */
314
287k
    if (apdu->datalen != apdu->lc)
315
0
      goto error;
316
287k
    break;
317
287k
  default:
318
0
    sc_log(card->ctx, "Invalid APDU case %d", apdu->cse);
319
0
    return SC_ERROR_INVALID_ARGUMENTS;
320
517k
  }
321
517k
  return SC_SUCCESS;
322
24
error:
323
24
  sc_log(card->ctx, "Invalid Case %d %s APDU:\n"
324
24
    "cse=%02x cla=%02x ins=%02x p1=%02x p2=%02x lc=%lu le=%lu\n"
325
24
    "resp=%p resplen=%lu data=%p datalen=%lu flags=0x%8.8lx",
326
48
    apdu->cse & SC_APDU_SHORT_MASK,
327
48
    (apdu->cse & SC_APDU_EXT) != 0 ? "extended" : "short",
328
48
    apdu->cse, apdu->cla, apdu->ins, apdu->p1, apdu->p2,
329
48
    (unsigned long) apdu->lc, (unsigned long) apdu->le,
330
48
    apdu->resp, (unsigned long) apdu->resplen,
331
48
    apdu->data, (unsigned long) apdu->datalen, apdu->flags);
332
24
  return SC_ERROR_INVALID_ARGUMENTS;
333
517k
}
334
335
/** Tries to determine the APDU type (short or extended) of the supplied
336
 *  APDU if one of the SC_APDU_CASE_? types is used.
337
 *  @param  apdu  APDU object
338
 */
339
static void
340
sc_detect_apdu_cse(const sc_card_t *card, sc_apdu_t *apdu)
341
508k
{
342
508k
  if (apdu->cse == SC_APDU_CASE_2 || apdu->cse == SC_APDU_CASE_3 ||
343
382k
      apdu->cse == SC_APDU_CASE_4) {
344
137k
    int btype = apdu->cse & SC_APDU_SHORT_MASK;
345
    /* if either Lc or Le is bigger than the maximum for
346
     * short APDUs and the card supports extended APDUs
347
     * use extended APDUs (unless Lc is greater than
348
     * 255 and command chaining is activated) */
349
137k
    if ((apdu->le > 256 || (apdu->lc > 255 && (apdu->flags & SC_APDU_FLAGS_CHAINING) == 0)) &&
350
1.17k
        (card->caps & SC_CARD_CAP_APDU_EXT) != 0)
351
1.16k
      btype |= SC_APDU_EXT;
352
137k
    apdu->cse = btype;
353
137k
  }
354
508k
}
355
356
357
static int
358
sc_single_transmit(struct sc_card *card, struct sc_apdu *apdu)
359
509k
{
360
509k
  struct sc_context *ctx  = card->ctx;
361
509k
  int rv;
362
363
509k
  LOG_FUNC_CALLED(ctx);
364
509k
  if (card->reader->ops->transmit == NULL)
365
509k
    LOG_TEST_RET(card->ctx, SC_ERROR_NOT_SUPPORTED, "cannot transmit APDU");
366
367
509k
  sc_log(ctx,
368
509k
         "CLA:%X, INS:%X, P1:%X, P2:%X, data(%"SC_FORMAT_LEN_SIZE_T"u) %p",
369
509k
         apdu->cla, apdu->ins, apdu->p1, apdu->p2, apdu->datalen,
370
509k
         apdu->data);
371
509k
#ifdef ENABLE_SM
372
509k
  if (card->sm_ctx.sm_mode == SM_MODE_TRANSMIT
373
3.15k
        && (apdu->flags & SC_APDU_FLAGS_NO_SM) == 0) {
374
1.57k
    LOG_FUNC_RETURN(ctx, sc_sm_single_transmit(card, apdu));
375
1.57k
  }
376
507k
#endif
377
378
  /* send APDU to the reader driver */
379
507k
  rv = card->reader->ops->transmit(card->reader, apdu);
380
507k
  LOG_TEST_RET(ctx, rv, "unable to transmit APDU");
381
382
507k
  LOG_FUNC_RETURN(ctx, rv);
383
507k
}
384
385
386
static int
387
sc_set_le_and_transmit(struct sc_card *card, struct sc_apdu *apdu, size_t olen)
388
1.30k
{
389
1.30k
  struct sc_context *ctx  = card->ctx;
390
1.30k
  size_t nlen = apdu->sw2 ? (size_t)apdu->sw2 : 256;
391
1.30k
  int rv;
392
393
1.30k
  LOG_FUNC_CALLED(ctx);
394
  /* we cannot re-transmit the APDU with the demanded Le value
395
   * as the buffer is too small => error */
396
1.30k
  if (olen < nlen)
397
1.30k
    LOG_TEST_RET(ctx, SC_ERROR_WRONG_LENGTH, "wrong length: required length exceeds resplen");
398
399
  /* don't try again if it doesn't work this time */
400
482
  apdu->flags  |= SC_APDU_FLAGS_NO_RETRY_WL;
401
  /* set the new expected length */
402
482
  apdu->resplen = olen;
403
482
  apdu->le      = nlen;
404
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
405
  /* Belpic V1 applets have a problem: if the card sends a 6C XX (only XX bytes available),
406
   * and we resend the command too soon (i.e. the reader is too fast), the card doesn't respond.
407
   * So we build in a delay. */
408
  if (card->type == SC_CARD_TYPE_BELPIC_EID)
409
    msleep(40);
410
#endif
411
412
  /* re-transmit the APDU with new Le length */
413
482
  rv = sc_single_transmit(card, apdu);
414
482
  LOG_TEST_RET(ctx, rv, "cannot re-transmit APDU");
415
416
479
  LOG_FUNC_RETURN(ctx, rv);
417
479
}
418
419
420
static int
421
sc_get_response(struct sc_card *card, struct sc_apdu *apdu, size_t olen)
422
58.4k
{
423
58.4k
  struct sc_context *ctx  = card->ctx;
424
58.4k
  size_t le, minlen, buflen;
425
58.4k
  unsigned char *buf;
426
58.4k
  int rv;
427
428
58.4k
  LOG_FUNC_CALLED(ctx);
429
58.4k
  if (apdu->le == 0) {
430
    /* no data is requested => change return value to 0x9000 and ignore the remaining data */
431
8.04k
    apdu->sw1 = 0x90;
432
8.04k
    apdu->sw2 = 0x00;
433
8.04k
    return SC_SUCCESS;
434
8.04k
  }
435
436
  /* this should _never_ happen */
437
50.4k
  if (!card->ops->get_response)
438
50.4k
    LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "no GET RESPONSE command");
439
440
  /* call GET RESPONSE until we have read all data requested or until the card returns 0x9000,
441
   * whatever happens first. */
442
443
  /* if there are already data in response append a new data to the end of the buffer */
444
50.3k
  buf = apdu->resp + apdu->resplen;
445
446
  /* read as much data as fits in apdu->resp (i.e. min(apdu->resplen, amount of data available)). */
447
50.3k
  buflen = olen - apdu->resplen;
448
449
  /* 0x6100 means at least 256 more bytes to read */
450
50.3k
  le = apdu->sw2 != 0 ? (size_t)apdu->sw2 : 256;
451
  /* we try to read at least as much as bytes as promised in the response bytes */
452
50.3k
  minlen = le;
453
454
60.4k
  do {
455
60.4k
    unsigned char resp[256];
456
60.4k
    size_t resp_len = le;
457
458
    /* we have all the data the caller requested even if the card has more data */
459
60.4k
    if (buflen == 0)
460
17.6k
      break;
461
462
    /* call GET RESPONSE to get more date from the card;
463
     * note: GET RESPONSE returns the left amount of data (== SW2) */
464
42.8k
    memset(resp, 0, sizeof(resp));
465
42.8k
    rv = card->ops->get_response(card, &resp_len, resp);
466
42.8k
    if (rv < 0)   {
467
5.44k
#ifdef ENABLE_SM
468
5.44k
      if (resp_len)   {
469
2.09k
        sc_log_hex(ctx, "SM response data", resp, resp_len);
470
2.09k
        sc_sm_update_apdu_response(card, resp, resp_len, rv, apdu);
471
2.09k
      }
472
5.44k
#endif
473
5.44k
      LOG_TEST_RET(ctx, rv, "GET RESPONSE error");
474
5.44k
    }
475
476
37.4k
    le = resp_len;
477
    /* copy as much as will fit in requested buffer */
478
37.4k
    if (buflen < le)
479
1.12k
      le = buflen;
480
481
37.4k
    memcpy(buf, resp, le);
482
37.4k
    buf    += le;
483
37.4k
    buflen -= le;
484
485
37.4k
    minlen -= le;
486
37.4k
    if (rv != 0)
487
10.1k
      le = minlen = (size_t)rv;
488
27.2k
    else
489
      /* if the card has returned 0x9000 but we still expect data ask for more
490
       * until we have read enough bytes */
491
27.2k
      le = minlen;
492
37.4k
  } while (rv != 0 && minlen != 0);
493
494
  /* we've read all data, let's return 0x9000 */
495
44.8k
  apdu->resplen = buf - apdu->resp;
496
44.8k
  apdu->sw1 = 0x90;
497
44.8k
  apdu->sw2 = 0x00;
498
499
44.8k
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
500
44.8k
}
501
502
503
/** Sends a single APDU to the card reader and calls GET RESPONSE to get the return data if necessary.
504
 *  @param  card  sc_card_t object for the smartcard
505
 *  @param  apdu  APDU to be sent
506
 *  @return SC_SUCCESS on success and an error value otherwise
507
 */
508
static int
509
sc_transmit(sc_card_t *card, sc_apdu_t *apdu)
510
508k
{
511
508k
  struct sc_context *ctx  = card->ctx;
512
508k
  size_t       olen  = apdu->resplen;
513
508k
  int          r;
514
515
508k
  LOG_FUNC_CALLED(ctx);
516
517
508k
  r = sc_single_transmit(card, apdu);
518
508k
  LOG_TEST_RET(ctx, r, "transmit APDU failed");
519
520
  /* ok, the APDU was successfully transmitted. Now we have two special cases:
521
   * 1. the card returned 0x6Cxx: in this case APDU will be re-transmitted with Le set to SW2
522
   * (possible only if response buffer size is larger than new Le = SW2)
523
   */
524
508k
  if (apdu->sw1 == 0x6C && (apdu->flags & SC_APDU_FLAGS_NO_RETRY_WL) == 0) {
525
1.30k
    r = sc_set_le_and_transmit(card, apdu, olen);
526
1.30k
    LOG_TEST_RET(ctx, r, "cannot re-transmit APDU ");
527
1.30k
  }
528
529
  /* 2. the card returned 0x61xx: more data can be read from the card
530
   *    using the GET RESPONSE command (mostly used in the T0 protocol).
531
   *    Unless the SC_APDU_FLAGS_NO_GET_RESP is set we try to read as
532
   *    much data as possible using GET RESPONSE.
533
   */
534
507k
  if (apdu->sw1 == 0x61 && (apdu->flags & SC_APDU_FLAGS_NO_GET_RESP) == 0) {
535
58.4k
    r = sc_get_response(card, apdu, olen);
536
58.4k
    LOG_TEST_RET(ctx, r, "cannot get all data with 'GET RESPONSE'");
537
58.4k
  }
538
539
501k
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
540
501k
}
541
542
543
int sc_transmit_apdu(sc_card_t *card, sc_apdu_t *apdu)
544
508k
{
545
508k
  int r = SC_SUCCESS;
546
547
508k
  if (card == NULL || apdu == NULL)
548
0
    return SC_ERROR_INVALID_ARGUMENTS;
549
550
508k
  LOG_FUNC_CALLED(card->ctx);
551
552
  /* determine the APDU type if necessary, i.e. to use
553
   * short or extended APDUs  */
554
508k
  sc_detect_apdu_cse(card, apdu);
555
  /* basic APDU consistency check */
556
508k
  r = sc_check_apdu(card, apdu);
557
508k
  if (r != SC_SUCCESS)
558
24
    return SC_ERROR_INVALID_ARGUMENTS;
559
560
508k
  r = sc_lock(card);  /* acquire card lock*/
561
508k
  if (r != SC_SUCCESS) {
562
0
    sc_log(card->ctx, "unable to acquire lock");
563
0
    return r;
564
0
  }
565
566
508k
#if ENABLE_SM
567
508k
  if (card->sm_ctx.sm_mode == SM_MODE_TRANSMIT
568
3.13k
      && (apdu->flags & SC_APDU_FLAGS_CHAINING) != 0
569
0
      && (apdu->flags & SC_APDU_FLAGS_SM_CHAINING) != 0) {
570
0
    sc_log(card->ctx,"Let SM do the chaining");
571
0
    r = sc_transmit(card, apdu);
572
0
  } else
573
508k
#endif
574
508k
  if ((apdu->flags & SC_APDU_FLAGS_CHAINING) != 0) {
575
    /* divide et impera: transmit APDU in chunks with Lc <= max_send_size
576
     * bytes using command chaining */
577
6.80k
    size_t    len  = apdu->datalen;
578
6.80k
    const u8  *buf = apdu->data;
579
6.80k
    size_t    max_send_size = sc_get_max_send_size(card);
580
581
13.7k
    while (len != 0) {
582
7.30k
      size_t    plen;
583
7.30k
      sc_apdu_t tapdu;
584
7.30k
      int       last = 0;
585
586
7.30k
      tapdu = *apdu;
587
      /* clear chaining flag */
588
7.30k
      tapdu.flags &= ~SC_APDU_FLAGS_CHAINING;
589
7.30k
      if (len > max_send_size) {
590
        /* adjust APDU case: in case of CASE 4 APDU
591
         * the intermediate APDU are of CASE 3 */
592
646
        if ((tapdu.cse & SC_APDU_SHORT_MASK) == SC_APDU_CASE_4_SHORT)
593
646
          tapdu.cse--;
594
        /* XXX: the chunk size must be adjusted when
595
         *      secure messaging is used */
596
646
        plen          = max_send_size;
597
646
        tapdu.cla    |= 0x10;
598
        /* the intermediate APDU don't expect response data */
599
646
        tapdu.le      = 0;
600
646
        tapdu.resplen = 0;
601
646
        tapdu.resp    = NULL;
602
6.66k
      } else {
603
6.66k
        plen = len;
604
6.66k
        last = 1;
605
6.66k
      }
606
7.30k
      tapdu.data    = buf;
607
7.30k
      tapdu.datalen = tapdu.lc = plen;
608
609
7.30k
      r = sc_check_apdu(card, &tapdu);
610
7.30k
      if (r != SC_SUCCESS) {
611
0
        sc_log(card->ctx, "inconsistent APDU while chaining");
612
0
        break;
613
0
      }
614
615
7.30k
      r = sc_transmit(card, &tapdu);
616
7.30k
      if (r != SC_SUCCESS)
617
199
        break;
618
7.10k
      if (last != 0) {
619
        /* in case of the last APDU set the SW1
620
         * and SW2 bytes in the original APDU */
621
6.46k
        apdu->sw1 = tapdu.sw1;
622
6.46k
        apdu->sw2 = tapdu.sw2;
623
6.46k
        apdu->resplen = tapdu.resplen;
624
6.46k
      } else {
625
        /* otherwise check the status bytes */
626
646
        r = sc_check_sw(card, tapdu.sw1, tapdu.sw2);
627
646
        if (r != SC_SUCCESS)
628
142
          break;
629
646
      }
630
6.96k
      len -= plen;
631
6.96k
      buf += plen;
632
6.96k
    }
633
501k
  } else {
634
    /* transmit single APDU */
635
501k
    r = sc_transmit(card, apdu);
636
501k
  }
637
638
508k
  if (r == SC_ERROR_CARD_RESET || r == SC_ERROR_READER_REATTACHED) {
639
    /* give card driver a chance to react on resets */
640
0
    if (card->ops->card_reader_lock_obtained)
641
0
      card->ops->card_reader_lock_obtained(card, 1);
642
0
  }
643
644
  /* all done => release lock */
645
508k
  if (sc_unlock(card) != SC_SUCCESS)
646
0
    sc_log(card->ctx, "sc_unlock failed");
647
648
508k
  return r;
649
508k
}
650
651
652
int
653
sc_bytes2apdu(sc_context_t *ctx, const u8 *buf, size_t len, sc_apdu_t *apdu)
654
82
{
655
82
  const unsigned char *p;
656
82
  size_t len0;
657
658
82
  if (!buf || !apdu)
659
0
    return SC_ERROR_INVALID_ARGUMENTS;
660
661
82
  len0 = len;
662
82
  if (len < 4) {
663
0
    sc_log(ctx, "APDU too short (must be at least 4 bytes)");
664
0
    return SC_ERROR_INVALID_DATA;
665
0
  }
666
667
82
  memset(apdu, 0, sizeof *apdu);
668
82
  p = buf;
669
82
  apdu->cla = *p++;
670
82
  apdu->ins = *p++;
671
82
  apdu->p1 = *p++;
672
82
  apdu->p2 = *p++;
673
82
  len -= 4;
674
675
82
  if (!len) {
676
0
    apdu->cse = SC_APDU_CASE_1;
677
0
    sc_log(ctx,
678
0
           "CASE_1 APDU: %"SC_FORMAT_LEN_SIZE_T"u bytes:\tins=%02x p1=%02x p2=%02x lc=%04"SC_FORMAT_LEN_SIZE_T"x le=%04"SC_FORMAT_LEN_SIZE_T"x",
679
0
           len0, apdu->ins, apdu->p1, apdu->p2, apdu->lc, apdu->le);
680
0
    return SC_SUCCESS;
681
0
  }
682
683
82
  if (*p == 0 && len >= 3) {
684
    /* ...must be an extended APDU */
685
0
    p++;
686
0
    if (len == 3) {
687
0
      apdu->le = (*p++)<<8;
688
0
      apdu->le += *p++;
689
0
      if (apdu->le == 0)
690
0
        apdu->le = 0xffff+1;
691
0
      len -= 3;
692
0
      apdu->cse = SC_APDU_CASE_2_EXT;
693
0
    }
694
0
    else {
695
      /* len > 3 */
696
0
      apdu->lc = (*p++)<<8;
697
0
      apdu->lc += *p++;
698
0
      len -= 3;
699
0
      if (len < apdu->lc) {
700
0
        sc_log(ctx,
701
0
               "APDU too short (need %"SC_FORMAT_LEN_SIZE_T"u more bytes)",
702
0
               apdu->lc - len);
703
0
        return SC_ERROR_INVALID_DATA;
704
0
      }
705
0
      apdu->data = p;
706
0
      apdu->datalen = apdu->lc;
707
0
      len -= apdu->lc;
708
0
      p += apdu->lc;
709
0
      if (!len) {
710
0
        apdu->cse = SC_APDU_CASE_3_EXT;
711
0
      }
712
0
      else {
713
        /* at this point the apdu has a Lc, so Le is on 2 bytes */
714
0
        if (len < 2) {
715
0
          sc_debug(ctx, SC_LOG_DEBUG_VERBOSE, "APDU too short (need 2 more bytes)\n");
716
0
          return SC_ERROR_INVALID_DATA;
717
0
        }
718
0
        apdu->le = (*p++)<<8;
719
0
        apdu->le += *p++;
720
0
        if (apdu->le == 0)
721
0
          apdu->le = 0xffff+1;
722
0
        len -= 2;
723
0
        apdu->cse = SC_APDU_CASE_4_EXT;
724
0
      }
725
0
    }
726
0
  }
727
82
  else {
728
    /* ...must be a short APDU */
729
82
    if (len == 1) {
730
82
      apdu->le = *p++;
731
82
      if (apdu->le == 0)
732
0
        apdu->le = 0xff+1;
733
82
      len--;
734
82
      apdu->cse = SC_APDU_CASE_2_SHORT;
735
82
    }
736
0
    else {
737
0
      apdu->lc = *p++;
738
0
      len--;
739
0
      if (len < apdu->lc) {
740
0
        sc_log(ctx,
741
0
               "APDU too short (need %"SC_FORMAT_LEN_SIZE_T"u more bytes)",
742
0
               apdu->lc - len);
743
0
        return SC_ERROR_INVALID_DATA;
744
0
      }
745
0
      apdu->data = p;
746
0
      apdu->datalen = apdu->lc;
747
0
      len -= apdu->lc;
748
0
      p += apdu->lc;
749
0
      if (!len) {
750
0
        apdu->cse = SC_APDU_CASE_3_SHORT;
751
0
      }
752
0
      else {
753
0
        apdu->le = *p++;
754
0
        if (apdu->le == 0)
755
0
          apdu->le = 0xff+1;
756
0
        len--;
757
0
        apdu->cse = SC_APDU_CASE_4_SHORT;
758
0
      }
759
0
    }
760
82
  }
761
82
  if (len) {
762
0
    sc_log(ctx, "APDU too long (%lu bytes extra)",(unsigned long) len);
763
0
    return SC_ERROR_INVALID_DATA;
764
0
  }
765
766
82
  sc_log(ctx,
767
82
         "Case %d %s APDU, %"SC_FORMAT_LEN_SIZE_T"u bytes:\tins=%02x p1=%02x p2=%02x lc=%04"SC_FORMAT_LEN_SIZE_T"x le=%04"SC_FORMAT_LEN_SIZE_T"x",
768
164
         apdu->cse & SC_APDU_SHORT_MASK,
769
164
         (apdu->cse & SC_APDU_EXT) != 0 ? "extended" : "short",
770
164
         len0, apdu->ins, apdu->p1, apdu->p2, apdu->lc,
771
164
         apdu->le);
772
773
82
  return SC_SUCCESS;
774
82
}