Coverage Report

Created: 2026-11-30 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/card.c
Line
Count
Source
1
/*
2
 * card.c: General smart card functions
3
 *
4
 * Copyright (C) 2001, 2002  Juha Yrjölä <juha.yrjola@iki.fi>
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 <stdlib.h>
26
#ifdef HAVE_UNISTD_H
27
#include <unistd.h>
28
#endif
29
#include <string.h>
30
#include <limits.h>
31
32
#include "reader-tr03119.h"
33
#include "internal.h"
34
#include "asn1.h"
35
#include "common/compat_strlcpy.h"
36
37
#ifdef ENABLE_SM
38
static int sc_card_sm_load(sc_card_t *card, const char *path, const char *module);
39
static int sc_card_sm_unload(sc_card_t *card);
40
static int sc_card_sm_check(sc_card_t *card);
41
#endif
42
43
int sc_check_sw(sc_card_t *card, unsigned int sw1, unsigned int sw2)
44
668k
{
45
668k
  if (card == NULL)
46
0
    return SC_ERROR_INVALID_ARGUMENTS;
47
668k
  if (card->ops->check_sw == NULL)
48
0
    return SC_ERROR_NOT_SUPPORTED;
49
668k
  return card->ops->check_sw(card, sw1, sw2);
50
668k
}
51
52
void sc_format_apdu(sc_card_t *card, sc_apdu_t *apdu,
53
        int cse, int ins, int p1, int p2)
54
873k
{
55
873k
  if (card == NULL || apdu == NULL) {
56
0
    return;
57
0
  }
58
873k
  memset(apdu, 0, sizeof(*apdu));
59
873k
  apdu->cla = (u8) card->cla;
60
873k
  apdu->cse = cse;
61
873k
  apdu->ins = (u8) ins;
62
873k
  apdu->p1 = (u8) p1;
63
873k
  apdu->p2 = (u8) p2;
64
873k
}
65
66
void sc_format_apdu_cse_lc_le(struct sc_apdu *apdu)
67
2.89k
{
68
  /* TODO calculating the APDU case, Lc and Le should actually only be
69
   * done in sc_apdu2bytes, but to gradually change OpenSC we start here. */
70
  /* Let sc_detect_apdu_cse set short or extended  and test for chaining */
71
72
2.89k
  if (!apdu)
73
0
    return;
74
2.89k
  if (apdu->datalen > SC_MAX_APDU_DATA_SIZE
75
2.89k
      || apdu->resplen > SC_MAX_APDU_RESP_SIZE) {
76
    /* extended length  or data chaining and/or get response */
77
0
    if (apdu->datalen <= SC_MAX_EXT_APDU_DATA_SIZE)
78
0
      apdu->lc = apdu->datalen;
79
0
    if (apdu->resplen <= SC_MAX_EXT_APDU_RESP_SIZE)
80
0
      apdu->le = apdu->resplen;
81
0
    if (apdu->resplen && !apdu->datalen)
82
0
      apdu->cse = SC_APDU_CASE_2;
83
0
    if (!apdu->resplen && apdu->datalen)
84
0
      apdu->cse = SC_APDU_CASE_3;
85
0
    if (apdu->resplen && apdu->datalen)
86
0
      apdu->cse = SC_APDU_CASE_4;
87
2.89k
  } else {
88
    /* short length */
89
2.89k
    if (apdu->datalen <= SC_MAX_APDU_DATA_SIZE)
90
2.89k
      apdu->lc = apdu->datalen;
91
2.89k
    if (apdu->resplen <= SC_MAX_APDU_RESP_SIZE)
92
2.89k
      apdu->le = apdu->resplen;
93
2.89k
    if (!apdu->resplen && !apdu->datalen)
94
0
      apdu->cse = SC_APDU_CASE_1;
95
2.89k
    if (apdu->resplen && !apdu->datalen)
96
130
      apdu->cse = SC_APDU_CASE_2_SHORT;
97
2.89k
    if (!apdu->resplen && apdu->datalen)
98
2.61k
      apdu->cse = SC_APDU_CASE_3_SHORT;
99
2.89k
    if (apdu->resplen && apdu->datalen)
100
153
      apdu->cse = SC_APDU_CASE_4_SHORT;
101
2.89k
  }
102
2.89k
}
103
104
void sc_format_apdu_ex(struct sc_apdu *apdu,
105
    u8 cla, u8 ins, u8 p1, u8 p2,
106
    const u8 *data, size_t datalen,
107
    u8 *resp, size_t resplen)
108
2.89k
{
109
2.89k
  if (!apdu) {
110
0
    return;
111
0
  }
112
113
2.89k
  memset(apdu, 0, sizeof(*apdu));
114
2.89k
  apdu->cla = cla;
115
2.89k
  apdu->ins = ins;
116
2.89k
  apdu->p1 = p1;
117
2.89k
  apdu->p2 = p2;
118
2.89k
  apdu->resp = resp;
119
2.89k
  apdu->resplen = resplen;
120
2.89k
  apdu->data = data;
121
2.89k
  apdu->datalen = datalen;
122
2.89k
  sc_format_apdu_cse_lc_le(apdu);
123
2.89k
}
124
125
static sc_card_t * sc_card_new(sc_context_t *ctx)
126
17.5k
{
127
17.5k
  sc_card_t *card;
128
129
17.5k
  if (ctx == NULL)
130
0
    return NULL;
131
132
17.5k
  card = calloc(1, sizeof(struct sc_card));
133
17.5k
  if (card == NULL)
134
0
    return NULL;
135
17.5k
  card->ops = malloc(sizeof(struct sc_card_operations));
136
17.5k
  if (card->ops == NULL) {
137
0
    free(card);
138
0
    return NULL;
139
0
  }
140
141
17.5k
  card->ctx = ctx;
142
17.5k
  if (sc_mutex_create(ctx, &card->mutex) != SC_SUCCESS) {
143
0
    free(card->ops);
144
0
    free(card);
145
0
    return NULL;
146
0
  }
147
148
17.5k
  card->type = -1;
149
17.5k
  card->app_count = -1;
150
151
17.5k
  return card;
152
17.5k
}
153
154
static void sc_card_free(sc_card_t *card)
155
17.5k
{
156
17.5k
  sc_free_apps(card);
157
17.5k
  sc_free_ef_atr(card);
158
159
17.5k
  free(card->ops);
160
161
17.5k
  if (card->algorithms != NULL)   {
162
13.2k
    int i;
163
75.8k
    for (i=0; i<card->algorithm_count; i++)   {
164
62.6k
      struct sc_algorithm_info *info = (card->algorithms + i);
165
62.6k
      if (info->algorithm == SC_ALGORITHM_EC ||
166
50.2k
          info->algorithm == SC_ALGORITHM_EDDSA ||
167
49.9k
          info->algorithm == SC_ALGORITHM_XEDDSA) {
168
14.3k
        sc_clear_ec_params(&info->u._ec.params);
169
14.3k
      }
170
62.6k
    }
171
13.2k
    free(card->algorithms);
172
173
13.2k
    card->algorithms = NULL;
174
13.2k
    card->algorithm_count = 0;
175
13.2k
  }
176
177
17.5k
  if (card->mutex != NULL) {
178
0
    int r = sc_mutex_destroy(card->ctx, card->mutex);
179
0
    if (r != SC_SUCCESS)
180
0
      sc_log(card->ctx, "unable to destroy mutex");
181
0
  }
182
17.5k
  sc_mem_clear(card, sizeof(*card));
183
17.5k
  free(card);
184
17.5k
}
185
186
size_t sc_get_max_recv_size(const sc_card_t *card)
187
386k
{
188
386k
  size_t max_recv_size;
189
386k
  if (card == NULL || card->reader == NULL) {
190
0
    return 0;
191
0
  }
192
386k
  max_recv_size = card->max_recv_size;
193
194
  /* initialize max_recv_size to a meaningful value */
195
386k
  if (card->caps & SC_CARD_CAP_APDU_EXT) {
196
109k
    if (!max_recv_size)
197
2.54k
      max_recv_size = 65536;
198
276k
  } else {
199
276k
    if (!max_recv_size)
200
136k
      max_recv_size = 256;
201
276k
  }
202
203
  /*  Override card limitations with reader limitations. */
204
386k
  if (card->reader->max_recv_size != 0
205
85.0k
      && (card->reader->max_recv_size < max_recv_size))
206
0
    max_recv_size = card->reader->max_recv_size;
207
208
386k
  return max_recv_size;
209
386k
}
210
211
size_t sc_get_max_send_size(const sc_card_t *card)
212
40.4k
{
213
40.4k
  size_t max_send_size;
214
215
40.4k
  if (card == NULL || card->reader == NULL) {
216
0
    return 0;
217
0
  }
218
219
40.4k
  max_send_size = card->max_send_size;
220
221
  /* initialize max_send_size to a meaningful value */
222
40.4k
  if (card->caps & SC_CARD_CAP_APDU_EXT
223
6.41k
      && card->reader->active_protocol != SC_PROTO_T0) {
224
6.41k
    if (!max_send_size)
225
984
      max_send_size = 65535;
226
34.0k
  } else {
227
34.0k
    if (!max_send_size)
228
9.21k
      max_send_size = 255;
229
34.0k
  }
230
231
  /*  Override card limitations with reader limitations. */
232
40.4k
  if (card->reader->max_send_size != 0
233
0
      && (card->reader->max_send_size < max_send_size))
234
0
    max_send_size = card->reader->max_send_size;
235
236
40.4k
  return max_send_size;
237
40.4k
}
238
239
int sc_connect_card(sc_reader_t *reader, sc_card_t **card_out)
240
17.5k
{
241
17.5k
  sc_card_t *card;
242
17.5k
  sc_context_t *ctx;
243
17.5k
  struct sc_card_driver *driver;
244
17.5k
  int i, r = 0, idx, connected = 0;
245
246
17.5k
  if (card_out == NULL || reader == NULL)
247
0
    return SC_ERROR_INVALID_ARGUMENTS;
248
17.5k
  ctx = reader->ctx;
249
17.5k
  SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE);
250
17.5k
  if (reader->ops->connect == NULL)
251
17.5k
    LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED);
252
253
17.5k
  card = sc_card_new(ctx);
254
17.5k
  if (card == NULL)
255
17.5k
    LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
256
17.5k
  r = reader->ops->connect(reader);
257
17.5k
  if (r)
258
0
    goto err;
259
260
17.5k
  connected = 1;
261
17.5k
  card->reader = reader;
262
17.5k
  card->ctx = ctx;
263
264
17.5k
  if (reader->flags & SC_READER_ENABLE_ESCAPE)
265
0
    sc_detect_escape_cmds(reader);
266
267
17.5k
  memcpy(&card->atr, &reader->atr, sizeof(card->atr));
268
17.5k
  memcpy(&card->uid, &reader->uid, sizeof(card->uid));
269
270
17.5k
  _sc_parse_atr(reader);
271
272
  /* See if the ATR matches any ATR specified in the config file */
273
17.5k
  if ((driver = ctx->forced_driver) == NULL) {
274
17.5k
    sc_log(ctx, "matching configured ATRs");
275
822k
    for (i = 0; ctx->card_drivers[i] != NULL; i++) {
276
805k
      driver = ctx->card_drivers[i];
277
278
805k
      if (driver->atr_map == NULL ||
279
805k
          !strcmp(driver->short_name, "default")) {
280
805k
        driver = NULL;
281
805k
        continue;
282
805k
      }
283
0
      sc_log(ctx, "trying driver '%s'", driver->short_name);
284
0
      idx = _sc_match_atr(card, driver->atr_map, NULL);
285
0
      if (idx >= 0) {
286
0
        struct sc_atr_table *src = &driver->atr_map[idx];
287
288
0
        sc_log(ctx, "matched driver '%s'", driver->name);
289
        /* It's up to card driver to notice these correctly */
290
0
        card->name = src->name;
291
0
        card->type = src->type;
292
0
        card->flags = src->flags;
293
0
        break;
294
0
      }
295
0
      driver = NULL;
296
0
    }
297
17.5k
  }
298
299
17.5k
  if (driver != NULL) {
300
    /* Forced driver, or matched via ATR mapping from config file */
301
0
    card->driver = driver;
302
303
0
    memcpy(card->ops, card->driver->ops, sizeof(struct sc_card_operations));
304
0
    if (card->ops->match_card != NULL)
305
0
      if (card->ops->match_card(card) != 1)
306
0
        sc_log(ctx, "driver '%s' match_card() failed: %s (will continue anyway)", card->driver->name, sc_strerror(r));
307
308
0
    if (card->ops->init != NULL) {
309
0
      r = card->ops->init(card);
310
0
      if (r) {
311
0
        sc_log(ctx, "driver '%s' init() failed: %s", card->driver->name, sc_strerror(r));
312
0
        goto err;
313
0
      }
314
0
    }
315
0
  }
316
17.5k
  else {
317
17.5k
    sc_card_t uninitialized = *card;
318
17.5k
    sc_log(ctx, "matching built-in ATRs");
319
459k
    for (i = 0; ctx->card_drivers[i] != NULL; i++) {
320
      /* FIXME If we had a clean API description, we'd probably get a
321
       * cleaner implementation of the driver's match_card and init,
322
       * which should normally *not* modify the card object if
323
       * unsuccessful. However, after years of relentless hacking, reality
324
       * is different: The card object is changed in virtually every card
325
       * driver so in order to prevent unwanted interaction, we reset the
326
       * card object here and hope that the card driver at least doesn't
327
       * allocate any internal resources that need to be freed. If we
328
       * had more time, we should refactor the existing code to not
329
       * modify sc_card_t until complete success (possibly by combining
330
       * `match_card()` and `init()`) */
331
457k
      *card = uninitialized;
332
333
457k
      struct sc_card_driver *drv = ctx->card_drivers[i];
334
457k
      const struct sc_card_operations *ops = drv->ops;
335
336
457k
      sc_log(ctx, "trying driver '%s'", drv->short_name);
337
457k
      if (ops == NULL || ops->match_card == NULL)   {
338
0
        continue;
339
0
      }
340
457k
      else if (!(ctx->flags & SC_CTX_FLAG_ENABLE_DEFAULT_DRIVER)
341
457k
            && !strcmp("default", drv->short_name))   {
342
2.58k
        sc_log(ctx , "ignore 'default' card driver");
343
2.58k
        continue;
344
2.58k
      }
345
346
      /* Needed if match_card() needs to talk with the card (e.g. card-muscle) */
347
454k
      *card->ops = *ops;
348
454k
      if (ops->match_card(card) != 1)
349
438k
        continue;
350
15.8k
      sc_log(ctx, "matched: %s", drv->name);
351
15.8k
      memcpy(card->ops, ops, sizeof(struct sc_card_operations));
352
15.8k
      card->driver = drv;
353
15.8k
      r = ops->init(card);
354
15.8k
      if (r) {
355
1.60k
        sc_log(ctx, "driver '%s' init() failed: %s", drv->name, sc_strerror(r));
356
1.60k
        if (r == SC_ERROR_INVALID_CARD) {
357
959
          card->driver = NULL;
358
959
          continue;
359
959
        }
360
649
        goto err;
361
1.60k
      }
362
14.2k
      break;
363
15.8k
    }
364
17.5k
  }
365
16.8k
  if (card->driver == NULL) {
366
2.58k
    sc_log(ctx, "unable to find driver for inserted card");
367
2.58k
    r = SC_ERROR_INVALID_CARD;
368
2.58k
    goto err;
369
2.58k
  }
370
14.2k
  if (card->name == NULL)
371
2.12k
    card->name = card->driver->name;
372
373
  /* initialize max_send_size/max_recv_size to a meaningful value */
374
14.2k
  card->max_recv_size = sc_get_max_recv_size(card);
375
14.2k
  card->max_send_size = sc_get_max_send_size(card);
376
377
14.2k
  sc_log(ctx, "card info name:'%s', type:%i, flags:0x%lX, max_send/recv_size:%zu/%zu",
378
14.2k
      card->name, card->type, card->flags, card->max_send_size, card->max_recv_size);
379
380
14.2k
#ifdef ENABLE_SM
381
  /* Check, if secure messaging module present. */
382
14.2k
  r = sc_card_sm_check(card);
383
14.2k
  if (r)   {
384
0
    sc_log(ctx, "cannot load secure messaging module");
385
0
    goto err;
386
0
  }
387
14.2k
#endif
388
14.2k
  *card_out = card;
389
390
14.2k
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
391
3.23k
err:
392
3.23k
  if (connected)
393
3.23k
    reader->ops->disconnect(reader);
394
3.23k
  if (card != NULL)
395
3.23k
    sc_card_free(card);
396
3.23k
  LOG_FUNC_RETURN(ctx, r);
397
3.23k
}
398
399
int sc_disconnect_card(sc_card_t *card)
400
14.2k
{
401
14.2k
  sc_context_t *ctx;
402
403
14.2k
  if (!card)
404
0
    return SC_ERROR_INVALID_ARGUMENTS;
405
406
14.2k
  ctx = card->ctx;
407
14.2k
  LOG_FUNC_CALLED(ctx);
408
409
14.2k
  if (card->ops->finish) {
410
13.5k
    int r = card->ops->finish(card);
411
13.5k
    if (r)
412
10
      sc_log(ctx, "card driver finish() failed: %s", sc_strerror(r));
413
13.5k
  }
414
415
14.2k
  if (card->reader->ops->disconnect) {
416
14.2k
    int r = card->reader->ops->disconnect(card->reader);
417
14.2k
    if (r)
418
0
      sc_log(ctx, "disconnect() failed: %s", sc_strerror(r));
419
14.2k
  }
420
421
14.2k
#ifdef ENABLE_SM
422
  /* release SM related resources */
423
14.2k
  sc_card_sm_unload(card);
424
14.2k
#endif
425
426
14.2k
  sc_card_free(card);
427
14.2k
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
428
14.2k
}
429
430
int sc_reset(sc_card_t *card, int do_cold_reset)
431
0
{
432
0
  int r, r2;
433
434
0
  if (card == NULL)
435
0
    return SC_ERROR_INVALID_ARGUMENTS;
436
0
  if (card->reader->ops->reset == NULL)
437
0
    return SC_ERROR_NOT_SUPPORTED;
438
439
0
  r = sc_mutex_lock(card->ctx, card->mutex);
440
0
  if (r != SC_SUCCESS)
441
0
    return r;
442
443
0
  r = card->reader->ops->reset(card->reader, do_cold_reset);
444
445
0
  r2 = sc_mutex_unlock(card->ctx, card->mutex);
446
0
  if (r2 != SC_SUCCESS) {
447
0
    sc_log(card->ctx, "unable to release lock");
448
0
    r = r != SC_SUCCESS ? r : r2;
449
0
  }
450
451
0
  return r;
452
0
}
453
454
int sc_lock(sc_card_t *card)
455
1.10M
{
456
1.10M
  int r = 0, r2 = 0;
457
1.10M
  int was_reset = 0;
458
1.10M
  int reader_lock_obtained  = 0;
459
460
1.10M
  if (card == NULL)
461
0
    return SC_ERROR_INVALID_ARGUMENTS;
462
463
1.10M
  LOG_FUNC_CALLED(card->ctx);
464
465
1.10M
  r = sc_mutex_lock(card->ctx, card->mutex);
466
1.10M
  if (r != SC_SUCCESS)
467
0
    return r;
468
1.10M
  if (card->lock_count == 0) {
469
752k
    if (card->reader->ops->lock != NULL) {
470
752k
      r = card->reader->ops->lock(card->reader);
471
752k
      while (r == SC_ERROR_CARD_RESET || r == SC_ERROR_READER_REATTACHED) {
472
0
        if (was_reset++ > 4) /* TODO retry a few times */
473
0
          break;
474
0
        r = card->reader->ops->lock(card->reader);
475
0
      }
476
752k
      if (r == 0)
477
752k
        reader_lock_obtained = 1;
478
752k
    }
479
752k
  }
480
1.10M
  if (r == 0)
481
1.10M
    card->lock_count++;
482
483
1.10M
  r2 = sc_mutex_unlock(card->ctx, card->mutex);
484
1.10M
  if (r2 != SC_SUCCESS) {
485
0
    sc_log(card->ctx, "unable to release card->mutex lock");
486
0
    r = r != SC_SUCCESS ? r : r2;
487
0
  }
488
489
1.10M
  if (r == 0 && was_reset > 0) {
490
0
#ifdef ENABLE_SM
491
0
    if (card->sm_ctx.ops.open)
492
0
      card->sm_ctx.ops.open(card);
493
0
#endif
494
0
  }
495
496
  /* give card driver a chance to do something when reader lock first obtained */
497
1.10M
  if (r == 0 && reader_lock_obtained == 1  && card->ops->card_reader_lock_obtained) {
498
393k
    if (SC_SUCCESS != card->ops->card_reader_lock_obtained(card, was_reset))
499
0
      sc_log(card->ctx, "card_reader_lock_obtained failed");
500
393k
  }
501
502
1.10M
  LOG_FUNC_RETURN(card->ctx, r);
503
1.10M
}
504
505
int sc_unlock(sc_card_t *card)
506
1.10M
{
507
1.10M
  int r, r2;
508
509
1.10M
  if (!card)
510
0
    return SC_ERROR_INVALID_ARGUMENTS;
511
512
1.10M
  LOG_FUNC_CALLED(card->ctx);
513
514
1.10M
  r = sc_mutex_lock(card->ctx, card->mutex);
515
1.10M
  if (r != SC_SUCCESS)
516
1.10M
    LOG_FUNC_RETURN(card->ctx, r);
517
518
1.10M
  if (card->lock_count < 1) {
519
0
    sc_mutex_unlock(card->ctx, card->mutex);
520
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
521
0
  }
522
1.10M
  if (--card->lock_count == 0) {
523
    /* release reader lock */
524
752k
    if (card->reader->ops->unlock != NULL)
525
752k
      r = card->reader->ops->unlock(card->reader);
526
752k
  }
527
1.10M
  r2 = sc_mutex_unlock(card->ctx, card->mutex);
528
1.10M
  if (r2 != SC_SUCCESS) {
529
0
    sc_log(card->ctx, "unable to release lock");
530
0
    r = (r == SC_SUCCESS) ? r2 : r;
531
0
  }
532
533
1.10M
  return r;
534
1.10M
}
535
536
int sc_list_files(sc_card_t *card, u8 *buf, size_t buflen)
537
1.15k
{
538
1.15k
  int r;
539
540
1.15k
  if (card == NULL) {
541
0
    return SC_ERROR_INVALID_ARGUMENTS;
542
0
  }
543
1.15k
  LOG_FUNC_CALLED(card->ctx);
544
545
1.15k
  if (card->ops->list_files == NULL)
546
1.15k
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
547
1.15k
  r = card->ops->list_files(card, buf, buflen);
548
549
1.15k
  LOG_FUNC_RETURN(card->ctx, r);
550
1.15k
}
551
552
int sc_create_file(sc_card_t *card, sc_file_t *file)
553
3.12k
{
554
3.12k
  int r;
555
3.12k
  char pbuf[SC_MAX_PATH_STRING_SIZE];
556
3.12k
  const sc_path_t *in_path;
557
558
3.12k
  if (card == NULL || file == NULL) {
559
0
    return SC_ERROR_INVALID_ARGUMENTS;
560
0
  }
561
562
3.12k
  in_path = &file->path;
563
3.12k
  r = sc_path_print(pbuf, sizeof(pbuf), in_path);
564
3.12k
  if (r != SC_SUCCESS)
565
3
    pbuf[0] = '\0';
566
567
3.12k
  sc_log(card->ctx, "called; type=%d, path=%s, id=%04i, size=%zu",
568
3.12k
      in_path->type, pbuf, file->id, file->size);
569
  /* ISO 7816-4: "Number of data bytes in the file, including structural information if any"
570
   * can not be bigger than two bytes */
571
3.12k
  if (file->size > 0xFFFF)
572
3.12k
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
573
574
3.11k
  if (card->ops->create_file == NULL)
575
3.11k
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
576
577
3.11k
  r = card->ops->create_file(card, file);
578
3.11k
  LOG_FUNC_RETURN(card->ctx, r);
579
3.11k
}
580
581
int sc_delete_file(sc_card_t *card, const sc_path_t *path)
582
957
{
583
957
  int r;
584
957
  char pbuf[SC_MAX_PATH_STRING_SIZE];
585
586
957
  if (card == NULL) {
587
0
    return SC_ERROR_INVALID_ARGUMENTS;
588
0
  }
589
590
957
  r = sc_path_print(pbuf, sizeof(pbuf), path);
591
957
  if (r != SC_SUCCESS)
592
2
    pbuf[0] = '\0';
593
594
957
  sc_log(card->ctx, "called; type=%d, path=%s", path->type, pbuf);
595
957
  if (card->ops->delete_file == NULL)
596
957
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
597
954
  r = card->ops->delete_file(card, path);
598
599
954
  LOG_FUNC_RETURN(card->ctx, r);
600
954
}
601
602
int sc_read_binary(sc_card_t *card, unsigned int idx,
603
       unsigned char *buf, size_t count, unsigned long *flags)
604
12.9k
{
605
12.9k
  size_t max_le = sc_get_max_recv_size(card);
606
12.9k
  size_t todo = count;
607
12.9k
  int r;
608
609
12.9k
  if (card == NULL || card->ops == NULL || buf == NULL) {
610
0
    return SC_ERROR_INVALID_ARGUMENTS;
611
0
  }
612
12.9k
  sc_log(card->ctx, "called; %zu bytes at index %d", count, idx);
613
12.9k
  if (count == 0)
614
12.9k
    LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
615
616
12.0k
#ifdef ENABLE_SM
617
12.0k
  if (card->sm_ctx.ops.read_binary)   {
618
110
    r = card->sm_ctx.ops.read_binary(card, idx, buf, count);
619
110
    if (r)
620
110
      LOG_FUNC_RETURN(card->ctx, r);
621
110
  }
622
12.0k
#endif
623
624
12.0k
  if (card->ops->read_binary == NULL)
625
12.0k
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
626
627
  /* lock the card now to avoid deselection of the file */
628
12.0k
  r = sc_lock(card);
629
12.0k
  LOG_TEST_RET(card->ctx, r, "sc_lock() failed");
630
631
29.0k
  while (todo > 0) {
632
28.3k
    size_t chunk = MIN(todo, max_le);
633
634
28.3k
    r = card->ops->read_binary(card, idx, buf, chunk, flags);
635
28.3k
    if (r == 0 || r == SC_ERROR_FILE_END_REACHED)
636
930
      break;
637
27.3k
    if (r < 0 && todo != count) {
638
      /* the last command failed, but previous ones succeeded.
639
       * Let's just return what we've successfully read. */
640
1.96k
      sc_log(card->ctx, "Subsequent read failed with %d, returning what was read successfully.", r);
641
1.96k
      break;
642
1.96k
    }
643
25.4k
    if (r < 0) {
644
8.38k
      sc_unlock(card);
645
8.38k
      LOG_FUNC_RETURN(card->ctx, r);
646
8.38k
    }
647
17.0k
    if ((idx > SIZE_MAX - (size_t) r) || (size_t) r > todo) {
648
      /* `idx + r` or `todo - r` would overflow */
649
0
      sc_unlock(card);
650
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OFFSET_TOO_LARGE);
651
0
    }
652
653
17.0k
    todo -= (size_t) r;
654
17.0k
    buf  += (size_t) r;
655
17.0k
    idx  += (size_t) r;
656
17.0k
  }
657
658
3.62k
  sc_unlock(card);
659
660
3.62k
  LOG_FUNC_RETURN(card->ctx, (int)(count - todo));
661
3.62k
}
662
663
int sc_write_binary(sc_card_t *card, unsigned int idx,
664
        const u8 *buf, size_t count, unsigned long flags)
665
0
{
666
0
  size_t max_lc = sc_get_max_send_size(card);
667
0
  size_t todo = count;
668
0
  int r;
669
670
0
  if (card == NULL || card->ops == NULL || buf == NULL) {
671
0
    return SC_ERROR_INVALID_ARGUMENTS;
672
0
  }
673
0
  sc_log(card->ctx, "called; %zu bytes at index %d", count, idx);
674
0
  if (count == 0)
675
0
    LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
676
677
0
  if (card->ops->write_binary == NULL)
678
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
679
680
  /* lock the card now to avoid deselection of the file */
681
0
  r = sc_lock(card);
682
0
  LOG_TEST_RET(card->ctx, r, "sc_lock() failed");
683
684
0
  while (todo > 0) {
685
0
    size_t chunk = MIN(todo, max_lc);
686
687
0
    r = card->ops->write_binary(card, idx, buf, chunk, flags);
688
0
    if (r == 0 || r == SC_ERROR_FILE_END_REACHED)
689
0
      break;
690
0
    if (r < 0) {
691
0
      sc_unlock(card);
692
0
      LOG_FUNC_RETURN(card->ctx, r);
693
0
    }
694
0
    if ((idx > SIZE_MAX - (size_t) r) || (size_t) r > todo) {
695
      /* `idx + r` or `todo - r` would overflow */
696
0
      sc_unlock(card);
697
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OFFSET_TOO_LARGE);
698
0
    }
699
700
0
    todo -= (size_t) r;
701
0
    buf  += (size_t) r;
702
0
    idx  += (size_t) r;
703
0
  }
704
705
0
  sc_unlock(card);
706
707
0
  LOG_FUNC_RETURN(card->ctx, (int)(count - todo));
708
0
}
709
710
int sc_update_binary(sc_card_t *card, unsigned int idx,
711
         const u8 *buf, size_t count, unsigned long flags)
712
1.91k
{
713
1.91k
  size_t max_lc = sc_get_max_send_size(card);
714
1.91k
  size_t todo = count;
715
1.91k
  int r;
716
717
1.91k
  if (card == NULL || card->ops == NULL || buf == NULL) {
718
0
    return SC_ERROR_INVALID_ARGUMENTS;
719
0
  }
720
1.91k
  sc_log(card->ctx, "called; %zu bytes at index %d", count, idx);
721
1.91k
  if (count == 0)
722
1.91k
    LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
723
724
1.90k
#ifdef ENABLE_SM
725
1.90k
  if (card->sm_ctx.ops.update_binary)   {
726
4
    r = card->sm_ctx.ops.update_binary(card, idx, buf, count);
727
4
    if (r)
728
4
      LOG_FUNC_RETURN(card->ctx, r);
729
4
  }
730
1.90k
#endif
731
732
1.90k
  if (card->ops->update_binary == NULL)
733
1.90k
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
734
735
  /* lock the card now to avoid deselection of the file */
736
1.89k
  r = sc_lock(card);
737
1.89k
  LOG_TEST_RET(card->ctx, r, "sc_lock() failed");
738
739
11.0k
  while (todo > 0) {
740
9.51k
    size_t chunk = MIN(todo, max_lc);
741
742
9.51k
    r = card->ops->update_binary(card, idx, buf, chunk, flags);
743
9.51k
    if (r == 0 || r == SC_ERROR_FILE_END_REACHED)
744
4
      break;
745
9.50k
    if (r < 0) {
746
333
      sc_unlock(card);
747
333
      LOG_FUNC_RETURN(card->ctx, r);
748
333
    }
749
9.17k
    if ((idx > SIZE_MAX - (size_t) r) || (size_t) r > todo) {
750
      /* `idx + r` or `todo - r` would overflow */
751
0
      sc_unlock(card);
752
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OFFSET_TOO_LARGE);
753
0
    }
754
755
9.17k
    todo -= (size_t) r;
756
9.17k
    buf  += (size_t) r;
757
9.17k
    idx  += (size_t) r;
758
9.17k
  }
759
760
1.56k
  sc_unlock(card);
761
762
1.56k
  LOG_FUNC_RETURN(card->ctx, (int)(count - todo));
763
1.56k
}
764
765
766
int sc_erase_binary(struct sc_card *card, unsigned int idx, size_t count,  unsigned long flags)
767
47
{
768
47
  int r;
769
47
  size_t todo = count;
770
771
47
  if (card == NULL || card->ops == NULL) {
772
0
    return SC_ERROR_INVALID_ARGUMENTS;
773
0
  }
774
47
  sc_log(card->ctx, "called; erase %zu bytes from offset %d", count, idx);
775
47
  if (count == 0)
776
47
    LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
777
778
31
  if (card->ops->erase_binary == NULL)
779
31
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
780
781
  /* lock the card now to avoid deselection of the file */
782
31
  r = sc_lock(card);
783
31
  LOG_TEST_RET(card->ctx, r, "sc_lock() failed");
784
785
40
  while (todo > 0) {
786
31
    r = card->ops->erase_binary(card, idx, todo, flags);
787
31
    if (r == 0 || r == SC_ERROR_FILE_END_REACHED)
788
1
      break;
789
30
    if (r < 0) {
790
21
      sc_unlock(card);
791
21
      LOG_FUNC_RETURN(card->ctx, r);
792
21
    }
793
9
    if ((idx > SIZE_MAX - (size_t) r) || (size_t) r > todo) {
794
      /* `idx + r` or `todo - r` would overflow */
795
0
      sc_unlock(card);
796
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OFFSET_TOO_LARGE);
797
0
    }
798
799
9
    todo -= (size_t) r;
800
9
    idx  += (size_t) r;
801
9
  }
802
803
10
  sc_unlock(card);
804
805
10
  LOG_FUNC_RETURN(card->ctx, (int)(count - todo));
806
10
}
807
808
809
int sc_select_file(sc_card_t *card, const sc_path_t *in_path,  sc_file_t **file)
810
307k
{
811
307k
  int r;
812
307k
  char pbuf[SC_MAX_PATH_STRING_SIZE];
813
814
307k
  if (card == NULL || in_path == NULL) {
815
0
    return SC_ERROR_INVALID_ARGUMENTS;
816
0
  }
817
818
307k
  r = sc_path_print(pbuf, sizeof(pbuf), in_path);
819
307k
  if (r != SC_SUCCESS)
820
411
    pbuf[0] = '\0';
821
822
  /* FIXME We should be a bit less strict and let the upper layers do
823
   * the initialization (including reuse of existing file objects). We
824
   * implemented this here because we are lazy. */
825
307k
  if (file != NULL)
826
256k
    *file = NULL;
827
828
307k
  sc_log(card->ctx, "called; type=%d, path=%s", in_path->type, pbuf);
829
307k
  if (in_path->len > SC_MAX_PATH_SIZE)
830
307k
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
831
832
307k
  if (in_path->type == SC_PATH_TYPE_PATH) {
833
    /* Perform a sanity check */
834
272k
    size_t i;
835
836
272k
    if ((in_path->len & 1) != 0)
837
272k
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
838
839
1.03M
    for (i = 0; i < in_path->len/2; i++) {
840
767k
      u8 p1 = in_path->value[2*i],
841
767k
         p2 = in_path->value[2*i+1];
842
843
767k
      if ((p1 == 0x3F && p2 == 0x00) && i != 0)
844
767k
        LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
845
767k
    }
846
264k
  }
847
298k
  if (card->ops->select_file == NULL)
848
298k
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
849
298k
  r = card->ops->select_file(card, in_path, file);
850
298k
  LOG_TEST_RET(card->ctx, r, "'SELECT' error");
851
852
45.5k
  if (file) {
853
30.3k
    if (*file)
854
      /* Remember file path */
855
29.3k
      (*file)->path = *in_path;
856
1.02k
    else
857
      /* FIXME We should be a bit less strict and let the upper layers do
858
       * the error checking. We implemented this here because we are
859
       * lazy.  */
860
1.02k
      r = SC_ERROR_INVALID_DATA;
861
30.3k
  }
862
863
45.5k
  LOG_FUNC_RETURN(card->ctx, r);
864
45.5k
}
865
866
867
int sc_get_data(sc_card_t *card, unsigned int tag, u8 *buf, size_t len)
868
7.94k
{
869
7.94k
  int r;
870
871
7.94k
  sc_log(card->ctx, "called, tag=%04x", tag);
872
7.94k
  if (card->ops->get_data == NULL)
873
7.94k
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
874
7.94k
  r = card->ops->get_data(card, tag, buf, len);
875
876
7.94k
  LOG_FUNC_RETURN(card->ctx, r);
877
7.94k
}
878
879
int sc_put_data(sc_card_t *card, unsigned int tag, const u8 *buf, size_t len)
880
0
{
881
0
  int r;
882
883
0
  sc_log(card->ctx,"called, tag=%04x", tag);
884
885
0
  if (card->ops->put_data == NULL)
886
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
887
0
  r = card->ops->put_data(card, tag, buf, len);
888
889
0
  LOG_FUNC_RETURN(card->ctx, r);
890
0
}
891
892
int sc_get_challenge(sc_card_t *card, u8 *rnd, size_t len)
893
88
{
894
88
  int r;
895
896
88
  if (len == 0)
897
0
    return SC_SUCCESS;
898
899
88
  if (card == NULL || rnd == NULL) {
900
0
    return SC_ERROR_INVALID_ARGUMENTS;
901
0
  }
902
903
88
  LOG_FUNC_CALLED(card->ctx);
904
905
88
  if (card->ops == NULL || card->ops->get_challenge == NULL)
906
88
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
907
908
88
  r = sc_lock(card);
909
88
  if (r != SC_SUCCESS)
910
88
    LOG_FUNC_RETURN(card->ctx, r);
911
912
143
  while (len > 0) {
913
116
    r = card->ops->get_challenge(card, rnd, len);
914
116
    if (r == 0)
915
15
      r = SC_ERROR_INVALID_DATA;
916
116
    if (r < 0) {
917
61
      sc_unlock(card);
918
61
      LOG_FUNC_RETURN(card->ctx, r);
919
61
    }
920
921
55
    rnd += (size_t) r;
922
55
    len -= (size_t) r;
923
55
  }
924
925
27
  sc_unlock(card);
926
927
27
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
928
27
}
929
930
int sc_read_record(sc_card_t *card, unsigned int rec_nr, unsigned int idx,
931
       u8 *buf ,size_t count, unsigned long flags)
932
7.63k
{
933
7.63k
  size_t max_le = sc_get_max_recv_size(card);
934
7.63k
  size_t todo = count;
935
7.63k
  int r;
936
937
7.63k
  if (card == NULL || card->ops == NULL || buf == NULL) {
938
0
    return SC_ERROR_INVALID_ARGUMENTS;
939
0
  }
940
7.63k
  LOG_FUNC_CALLED(card->ctx);
941
7.63k
  if (count == 0)
942
7.63k
    LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
943
944
5.55k
  if (card->ops->read_record == NULL)
945
5.55k
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
946
947
  /* lock the card now to avoid deselection of the file */
948
4.11k
  r = sc_lock(card);
949
4.11k
  LOG_TEST_RET(card->ctx, r, "sc_lock() failed");
950
951
6.78k
  while (todo > 0) {
952
6.66k
    size_t chunk = MIN(todo, max_le);
953
954
6.66k
    r = card->ops->read_record(card, rec_nr, idx, buf, chunk, flags);
955
6.66k
    if (r == 0 || r == SC_ERROR_FILE_END_REACHED)
956
1.22k
      break;
957
5.44k
    if (r < 0 && todo != count) {
958
      /* the last command failed, but previous ones succeeded.
959
       * Let's just return what we've successfully read. */
960
2.33k
      sc_log(card->ctx, "Subsequent read failed with %d, returning what was read successfully.", r);
961
2.33k
      break;
962
2.33k
    }
963
3.10k
    if (r < 0) {
964
428
      sc_unlock(card);
965
428
      LOG_FUNC_RETURN(card->ctx, r);
966
428
    }
967
2.67k
    if ((idx > SIZE_MAX - (size_t) r) || (size_t) r > todo) {
968
      /* `idx + r` or `todo - r` would overflow */
969
0
      sc_unlock(card);
970
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OFFSET_TOO_LARGE);
971
0
    }
972
973
2.67k
    todo -= (size_t) r;
974
2.67k
    buf  += (size_t) r;
975
2.67k
    idx  += (size_t) r;
976
2.67k
  }
977
978
3.68k
  sc_unlock(card);
979
980
3.68k
  LOG_FUNC_RETURN(card->ctx, (int)(count - todo));
981
3.68k
}
982
983
int sc_write_record(sc_card_t *card, unsigned int rec_nr, const u8 * buf,
984
        size_t count, unsigned long flags)
985
0
{
986
0
  int r;
987
988
0
  if (card == NULL) {
989
0
    return SC_ERROR_INVALID_ARGUMENTS;
990
0
  }
991
0
  LOG_FUNC_CALLED(card->ctx);
992
993
0
  if (card->ops->write_record == NULL)
994
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
995
996
0
  r = card->ops->write_record(card, rec_nr, buf, count, flags);
997
0
  if (r == SC_SUCCESS) {
998
0
    r = (int)count;
999
0
  }
1000
1001
0
  LOG_FUNC_RETURN(card->ctx, r);
1002
0
}
1003
1004
int sc_append_record(sc_card_t *card, const u8 * buf, size_t count,
1005
         unsigned long flags)
1006
1
{
1007
1
  int r;
1008
1009
1
  if (card == NULL) {
1010
0
    return SC_ERROR_INVALID_ARGUMENTS;
1011
0
  }
1012
1
  LOG_FUNC_CALLED(card->ctx);
1013
1014
1
  if (card->ops->append_record == NULL)
1015
1
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
1016
1017
0
  r = card->ops->append_record(card, buf, count, flags);
1018
0
  if (r == SC_SUCCESS) {
1019
0
    r = (int)count;
1020
0
  }
1021
1022
0
  LOG_FUNC_RETURN(card->ctx, r);
1023
0
}
1024
1025
int sc_update_record(sc_card_t *card, unsigned int rec_nr, unsigned int idx,
1026
         const u8 * buf, size_t count, unsigned long flags)
1027
1
{
1028
1
  size_t max_lc = sc_get_max_send_size(card);
1029
1
  size_t todo = count;
1030
1
  int r;
1031
1032
1
  if (card == NULL || card->ops == NULL || buf == NULL) {
1033
0
    return SC_ERROR_INVALID_ARGUMENTS;
1034
0
  }
1035
1
  LOG_FUNC_CALLED(card->ctx);
1036
1
  if (count == 0)
1037
1
    LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
1038
1039
1
  if (card->ops->update_record == NULL)
1040
1
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
1041
1042
  /* lock the card now to avoid deselection of the file */
1043
0
  r = sc_lock(card);
1044
0
  LOG_TEST_RET(card->ctx, r, "sc_lock() failed");
1045
1046
0
  while (todo > 0) {
1047
0
    size_t chunk = MIN(todo, max_lc);
1048
1049
0
    r = card->ops->update_record(card, rec_nr, idx, buf, chunk, flags);
1050
0
    if (r == 0 || r == SC_ERROR_FILE_END_REACHED)
1051
0
      break;
1052
0
    if (r < 0) {
1053
0
      sc_unlock(card);
1054
0
      LOG_FUNC_RETURN(card->ctx, r);
1055
0
    }
1056
0
    if ((idx > SIZE_MAX - (size_t) r) || (size_t) r > todo) {
1057
      /* `idx + r` or `todo - r` would overflow */
1058
0
      sc_unlock(card);
1059
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OFFSET_TOO_LARGE);
1060
0
    }
1061
1062
0
    todo -= (size_t) r;
1063
0
    buf  += (size_t) r;
1064
0
    idx  += (size_t) r;
1065
0
  }
1066
1067
0
  sc_unlock(card);
1068
1069
0
  LOG_FUNC_RETURN(card->ctx, (int)(count - todo));
1070
0
}
1071
1072
int sc_delete_record(sc_card_t *card, unsigned int rec_nr)
1073
0
{
1074
0
  int r;
1075
1076
0
  if (card == NULL) {
1077
0
    return SC_ERROR_INVALID_ARGUMENTS;
1078
0
  }
1079
0
  LOG_FUNC_CALLED(card->ctx);
1080
1081
0
  if (card->ops->delete_record == NULL)
1082
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
1083
1084
0
  r = card->ops->delete_record(card, rec_nr);
1085
1086
0
  LOG_FUNC_RETURN(card->ctx, r);
1087
0
}
1088
1089
int
1090
sc_card_ctl(sc_card_t *card, unsigned long cmd, void *args)
1091
26.3k
{
1092
26.3k
  int r = SC_ERROR_NOT_SUPPORTED;
1093
1094
26.3k
  if (card == NULL) {
1095
0
    return SC_ERROR_INVALID_ARGUMENTS;
1096
0
  }
1097
26.3k
  sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "called with cmd=%lu\n", cmd);
1098
1099
26.3k
  if (card->ops->card_ctl != NULL)
1100
26.2k
    r = card->ops->card_ctl(card, cmd, args);
1101
1102
  /* suppress "not supported" error messages */
1103
26.3k
  if (r == SC_ERROR_NOT_SUPPORTED) {
1104
17.0k
    sc_log(card->ctx, "card_ctl(%lu) not supported", cmd);
1105
17.0k
    return r;
1106
17.0k
  }
1107
9.35k
  LOG_FUNC_RETURN(card->ctx, r);
1108
9.35k
}
1109
1110
int _sc_card_add_algorithm(sc_card_t *card, const sc_algorithm_info_t *info)
1111
62.6k
{
1112
62.6k
  sc_algorithm_info_t *p;
1113
1114
62.6k
  if (info == NULL) {
1115
0
    return SC_ERROR_INVALID_ARGUMENTS;
1116
0
  }
1117
62.6k
  p = (sc_algorithm_info_t *) realloc(card->algorithms, (card->algorithm_count + 1) * sizeof(*info));
1118
62.6k
  if (!p) {
1119
0
    return SC_ERROR_OUT_OF_MEMORY;
1120
0
  }
1121
62.6k
  card->algorithms = p;
1122
62.6k
  p += card->algorithm_count;
1123
62.6k
  card->algorithm_count++;
1124
62.6k
  *p = *info;
1125
62.6k
  return SC_SUCCESS;
1126
62.6k
}
1127
1128
int _sc_card_add_symmetric_alg(sc_card_t *card, unsigned int algorithm,
1129
             unsigned int key_length, unsigned long flags)
1130
2.34k
{
1131
2.34k
  sc_algorithm_info_t info;
1132
1133
2.34k
  memset(&info, 0, sizeof(info));
1134
2.34k
  info.algorithm = algorithm;
1135
2.34k
  info.key_length = key_length;
1136
2.34k
  info.flags = flags;
1137
1138
2.34k
  return _sc_card_add_algorithm(card, &info);
1139
2.34k
}
1140
1141
static int
1142
_sc_card_add_ec_alg_int(sc_card_t *card, size_t key_length,
1143
      unsigned long flags, unsigned long ext_flags,
1144
      struct sc_object_id *curve_oid,
1145
      int algorithm)
1146
14.3k
{
1147
14.3k
  sc_algorithm_info_t info;
1148
14.3k
  int r;
1149
1150
14.3k
  memset(&info, 0, sizeof(info));
1151
14.3k
  sc_init_oid(&info.u._ec.params.id);
1152
1153
14.3k
  info.algorithm = algorithm;
1154
14.3k
  info.key_length = key_length;
1155
14.3k
  info.flags = flags;
1156
1157
14.3k
  info.u._ec.ext_flags = ext_flags;
1158
14.3k
  if (curve_oid) {
1159
8.17k
    info.u._ec.params.id = *curve_oid;
1160
8.17k
    r = sc_encode_oid(card->ctx, &info.u._ec.params.id, &info.u._ec.params.der.value, &info.u._ec.params.der.len);
1161
8.17k
    LOG_TEST_GOTO_ERR(card->ctx, r, "sc_encode_oid failed");
1162
8.17k
    r = sc_pkcs15_fix_ec_parameters(card->ctx, &info.u._ec.params);
1163
8.17k
    LOG_TEST_GOTO_ERR(card->ctx, r, "sc_pkcs15_fix_ec_parameters failed");
1164
8.17k
  }
1165
1166
14.3k
  r = _sc_card_add_algorithm(card, &info);
1167
14.3k
  return r;
1168
0
err:
1169
0
  sc_clear_ec_params(&info.u._ec.params);
1170
0
  return r;
1171
14.3k
}
1172
1173
int  _sc_card_add_ec_alg(sc_card_t *card, size_t key_length,
1174
      unsigned long flags, unsigned long ext_flags,
1175
      struct sc_object_id *curve_oid)
1176
12.3k
{
1177
12.3k
  return _sc_card_add_ec_alg_int(card, key_length, flags, ext_flags,
1178
12.3k
    curve_oid, SC_ALGORITHM_EC);
1179
12.3k
}
1180
1181
int  _sc_card_add_eddsa_alg(sc_card_t *card, size_t key_length,
1182
      unsigned long flags, unsigned long ext_flags,
1183
      struct sc_object_id *curve_oid)
1184
305
{
1185
  /* For simplicity, share the ec union with the curve information */
1186
305
  return _sc_card_add_ec_alg_int(card, key_length, flags, ext_flags,
1187
305
    curve_oid, SC_ALGORITHM_EDDSA);
1188
305
}
1189
1190
int  _sc_card_add_xeddsa_alg(sc_card_t *card, size_t key_length,
1191
      unsigned long flags, unsigned long ext_flags,
1192
      struct sc_object_id *curve_oid)
1193
1.68k
{
1194
  /* For simplicity, share the ec union with the curve information */
1195
1.68k
  return _sc_card_add_ec_alg_int(card, key_length, flags, ext_flags,
1196
1.68k
    curve_oid, SC_ALGORITHM_XEDDSA);
1197
1.68k
}
1198
1199
sc_algorithm_info_t *sc_card_find_alg(sc_card_t *card,
1200
    unsigned int algorithm, size_t key_length, void *param)
1201
223
{
1202
223
  int i;
1203
1204
1.15k
  for (i = 0; i < card->algorithm_count; i++) {
1205
1.06k
    sc_algorithm_info_t *info = &card->algorithms[i];
1206
1207
1.06k
    if (param && (info->algorithm == SC_ALGORITHM_EC ||
1208
0
      info->algorithm == SC_ALGORITHM_EDDSA ||
1209
0
      info->algorithm == SC_ALGORITHM_XEDDSA)) {
1210
0
      if (sc_compare_oid((struct sc_object_id *)param, &info->u._ec.params.id))
1211
0
        return info;
1212
1.06k
    } else if (info->algorithm != algorithm) {
1213
388
      continue;
1214
673
    } else if (info->key_length == key_length)
1215
131
      return info;
1216
1.06k
  }
1217
92
  return NULL;
1218
223
}
1219
1220
sc_algorithm_info_t *sc_card_find_ec_alg(sc_card_t *card,
1221
    size_t key_length, struct sc_object_id *curve_name)
1222
60
{
1223
60
  return sc_card_find_alg(card, SC_ALGORITHM_EC, key_length, curve_name);
1224
60
}
1225
1226
sc_algorithm_info_t *sc_card_find_eddsa_alg(sc_card_t *card,
1227
    size_t key_length, struct sc_object_id *curve_name)
1228
0
{
1229
0
  return sc_card_find_alg(card, SC_ALGORITHM_EDDSA, key_length, curve_name);
1230
0
}
1231
1232
sc_algorithm_info_t *sc_card_find_xeddsa_alg(sc_card_t *card,
1233
    size_t key_length, struct sc_object_id *curve_name)
1234
0
{
1235
0
  return sc_card_find_alg(card, SC_ALGORITHM_XEDDSA, key_length, curve_name);
1236
0
}
1237
1238
int _sc_card_add_rsa_alg(sc_card_t *card, size_t key_length,
1239
       unsigned long flags, unsigned long exponent)
1240
45.7k
{
1241
45.7k
  sc_algorithm_info_t info;
1242
1243
45.7k
  memset(&info, 0, sizeof(info));
1244
45.7k
  info.algorithm = SC_ALGORITHM_RSA;
1245
45.7k
  info.key_length = key_length;
1246
45.7k
  info.flags = flags;
1247
  /* disable particular PKCS1 v1.5 padding type if also RAW is supported on card */
1248
45.7k
  if ((info.flags & (SC_ALGORITHM_RSA_PAD_PKCS1 | SC_ALGORITHM_RSA_RAW)) == (SC_ALGORITHM_RSA_PAD_PKCS1 | SC_ALGORITHM_RSA_RAW)) {
1249
8.11k
    if (card->ctx->disable_hw_pkcs1_padding & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01)
1250
0
      info.flags &= ~SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01;
1251
8.11k
    if (card->ctx->disable_hw_pkcs1_padding & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02)
1252
8.11k
      info.flags &= ~SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02;
1253
8.11k
  }
1254
45.7k
  info.u._rsa.exponent = exponent;
1255
1256
45.7k
  return _sc_card_add_algorithm(card, &info);
1257
45.7k
}
1258
1259
sc_algorithm_info_t *sc_card_find_rsa_alg(sc_card_t *card, size_t key_length)
1260
163
{
1261
163
  return sc_card_find_alg(card, SC_ALGORITHM_RSA, key_length, NULL);
1262
163
}
1263
1264
sc_algorithm_info_t *sc_card_find_gostr3410_alg(sc_card_t *card, size_t key_length)
1265
0
{
1266
0
  return sc_card_find_alg(card, SC_ALGORITHM_GOSTR3410, key_length, NULL);
1267
0
}
1268
1269
static int match_atr_table(sc_context_t *ctx, const struct sc_atr_table *table, struct sc_atr *atr)
1270
407k
{
1271
407k
  u8 *card_atr_bin;
1272
407k
  size_t card_atr_bin_len;
1273
407k
  char card_atr_hex[3 * SC_MAX_ATR_SIZE];
1274
407k
  size_t card_atr_hex_len;
1275
407k
  unsigned int i = 0;
1276
1277
407k
  if (ctx == NULL || table == NULL || atr == NULL)
1278
14.2k
    return -1;
1279
393k
  card_atr_bin = atr->value;
1280
393k
  card_atr_bin_len = atr->len;
1281
393k
  sc_bin_to_hex(card_atr_bin, card_atr_bin_len, card_atr_hex, sizeof(card_atr_hex), ':');
1282
393k
  card_atr_hex_len = strlen(card_atr_hex);
1283
1284
393k
  sc_debug(ctx, SC_LOG_DEBUG_MATCH, "ATR     : %s", card_atr_hex);
1285
1286
3.30M
  for (i = 0; table[i].atr != NULL; i++) {
1287
2.91M
    const char *tatr = table[i].atr;
1288
2.91M
    const char *matr = table[i].atrmask;
1289
2.91M
    size_t tatr_len = strlen(tatr);
1290
2.91M
    u8 mbin[SC_MAX_ATR_SIZE], tbin[SC_MAX_ATR_SIZE];
1291
2.91M
    size_t mbin_len, tbin_len, s, matr_len;
1292
2.91M
    size_t fix_hex_len = card_atr_hex_len;
1293
2.91M
    size_t fix_bin_len = card_atr_bin_len;
1294
1295
2.91M
    sc_debug(ctx, SC_LOG_DEBUG_MATCH, "ATR try : %s", tatr);
1296
1297
2.91M
    if (tatr_len != fix_hex_len) {
1298
2.84M
      sc_debug(ctx, SC_LOG_DEBUG_MATCH, "ignored - wrong length");
1299
2.84M
      continue;
1300
2.84M
    }
1301
75.4k
    if (matr != NULL) {
1302
23.7k
      sc_debug(ctx, SC_LOG_DEBUG_MATCH, "ATR mask: %s", matr);
1303
1304
23.7k
      matr_len = strlen(matr);
1305
23.7k
      if (tatr_len != matr_len)
1306
0
        continue;
1307
23.7k
      tbin_len = sizeof(tbin);
1308
23.7k
      sc_hex_to_bin(tatr, tbin, &tbin_len);
1309
23.7k
      mbin_len = sizeof(mbin);
1310
23.7k
      sc_hex_to_bin(matr, mbin, &mbin_len);
1311
23.7k
      if (mbin_len != fix_bin_len) {
1312
0
        sc_debug(ctx, SC_LOG_DEBUG_MATCH, "length of atr and atr mask do not match - ignored: %s - %s", tatr, matr);
1313
0
        continue;
1314
0
      }
1315
483k
      for (s = 0; s < tbin_len; s++) {
1316
        /* reduce tatr with mask */
1317
459k
        tbin[s] = (tbin[s] & mbin[s]);
1318
        /* create copy of card_atr_bin masked) */
1319
459k
        mbin[s] = (card_atr_bin[s] & mbin[s]);
1320
459k
      }
1321
23.7k
      if (memcmp(tbin, mbin, tbin_len) != 0)
1322
20.1k
        continue;
1323
51.7k
    } else {
1324
51.7k
      if (strncasecmp(tatr, card_atr_hex, tatr_len) != 0)
1325
46.7k
        continue;
1326
51.7k
    }
1327
8.53k
    return i;
1328
75.4k
  }
1329
384k
  return -1;
1330
393k
}
1331
1332
int _sc_match_atr(sc_card_t *card, const struct sc_atr_table *table, int *type_out)
1333
393k
{
1334
393k
  int res;
1335
1336
393k
  if (card == NULL)
1337
0
    return -1;
1338
393k
  res = match_atr_table(card->ctx, table, &card->atr);
1339
393k
  if (res < 0)
1340
384k
    return res;
1341
8.53k
  if (type_out != NULL)
1342
8.02k
    *type_out = table[res].type;
1343
8.53k
  return res;
1344
393k
}
1345
1346
scconf_block *_sc_match_atr_block(sc_context_t *ctx, struct sc_card_driver *driver, struct sc_atr *atr)
1347
14.2k
{
1348
14.2k
  struct sc_card_driver *drv;
1349
14.2k
  struct sc_atr_table *table;
1350
14.2k
  int res;
1351
1352
14.2k
  if (ctx == NULL)
1353
0
    return NULL;
1354
14.2k
  if (driver) {
1355
14.2k
    drv = driver;
1356
14.2k
    table = drv->atr_map;
1357
14.2k
    res = match_atr_table(ctx, table, atr);
1358
14.2k
    if (res < 0)
1359
14.2k
      return NULL;
1360
0
    return table[res].card_atr;
1361
14.2k
  } else {
1362
0
    unsigned int i;
1363
1364
0
    for (i = 0; ctx->card_drivers[i] != NULL; i++) {
1365
0
      drv = ctx->card_drivers[i];
1366
0
      table = drv->atr_map;
1367
0
      res = match_atr_table(ctx, table, atr);
1368
0
      if (res < 0)
1369
0
        continue;
1370
0
      return table[res].card_atr;
1371
0
    }
1372
0
  }
1373
0
  return NULL;
1374
14.2k
}
1375
1376
int _sc_add_atr(sc_context_t *ctx, struct sc_card_driver *driver, struct sc_atr_table *src)
1377
0
{
1378
0
  struct sc_atr_table *map, *dst;
1379
1380
0
  map = (struct sc_atr_table *) realloc(driver->atr_map,
1381
0
      (driver->natrs + 2) * sizeof(struct sc_atr_table));
1382
0
  if (!map)
1383
0
    return SC_ERROR_OUT_OF_MEMORY;
1384
0
  driver->atr_map = map;
1385
1386
0
  dst = &driver->atr_map[driver->natrs++];
1387
0
  memset(dst, 0, sizeof(*dst));
1388
0
  memset(&driver->atr_map[driver->natrs], 0, sizeof(struct sc_atr_table));
1389
0
  dst->atr = strdup(src->atr);
1390
0
  if (!dst->atr)
1391
0
    return SC_ERROR_OUT_OF_MEMORY;
1392
1393
0
  if (src->atrmask) {
1394
0
    dst->atrmask = strdup(src->atrmask);
1395
0
    if (!dst->atrmask)
1396
0
      return SC_ERROR_OUT_OF_MEMORY;
1397
0
  }
1398
0
  else {
1399
0
    dst->atrmask = NULL;
1400
0
  }
1401
1402
0
  if (src->name) {
1403
0
    dst->name = strdup(src->name);
1404
0
    if (!dst->name)
1405
0
      return SC_ERROR_OUT_OF_MEMORY;
1406
0
  }
1407
0
  else {
1408
0
    dst->name = NULL;
1409
0
  }
1410
1411
0
  dst->type = src->type;
1412
0
  dst->flags = src->flags;
1413
0
  dst->card_atr = src->card_atr;
1414
1415
0
  return SC_SUCCESS;
1416
0
}
1417
1418
1419
int _sc_free_atr(sc_context_t *ctx, struct sc_card_driver *driver)
1420
0
{
1421
0
  unsigned int i;
1422
1423
0
  for (i = 0; i < driver->natrs; i++) {
1424
0
    struct sc_atr_table *src = &driver->atr_map[i];
1425
1426
0
    if (src->atr)
1427
0
      free((void *)src->atr);
1428
0
    if (src->atrmask)
1429
0
      free((void *)src->atrmask);
1430
0
    if (src->name)
1431
0
      free((void *)src->name);
1432
0
    src->card_atr = NULL;
1433
0
    src = NULL;
1434
0
  }
1435
0
  if (driver->atr_map)
1436
0
    free(driver->atr_map);
1437
0
  driver->atr_map = NULL;
1438
0
  driver->natrs = 0;
1439
1440
0
  return SC_SUCCESS;
1441
0
}
1442
1443
1444
scconf_block *sc_get_conf_block(sc_context_t *ctx, const char *name1, const char *name2, int priority)
1445
39.6k
{
1446
39.6k
  int i;
1447
39.6k
  scconf_block *conf_block = NULL;
1448
1449
79.3k
  for (i = 0; ctx->conf_blocks[i] != NULL; i++) {
1450
39.6k
    scconf_block **blocks;
1451
1452
39.6k
    blocks = scconf_find_blocks(ctx->conf, ctx->conf_blocks[i], name1, name2);
1453
39.6k
    if (blocks != NULL) {
1454
39.6k
      conf_block = blocks[0];
1455
39.6k
      free(blocks);
1456
39.6k
    }
1457
39.6k
    if (conf_block != NULL && priority)
1458
0
      break;
1459
39.6k
  }
1460
39.6k
  return conf_block;
1461
39.6k
}
1462
1463
void
1464
sc_clear_ec_params(struct sc_ec_parameters *ecp)
1465
20.4k
{
1466
20.4k
  if (ecp) {
1467
20.4k
    free(ecp->named_curve);
1468
20.4k
    free(ecp->der.value);
1469
20.4k
    memset(ecp, 0, sizeof(struct sc_ec_parameters));
1470
20.4k
  }
1471
20.4k
  return;
1472
20.4k
}
1473
1474
int sc_copy_ec_params(struct sc_ec_parameters *dst, struct sc_ec_parameters *src)
1475
2.65k
{
1476
2.65k
  if (!dst || !src)
1477
0
    return SC_ERROR_INVALID_ARGUMENTS;
1478
1479
2.65k
  memset(dst, 0, sizeof(*dst));
1480
2.65k
  if (src->named_curve)   {
1481
2.65k
    dst->named_curve = strdup(src->named_curve);
1482
2.65k
    if (!dst->named_curve)
1483
0
      return SC_ERROR_OUT_OF_MEMORY;
1484
2.65k
  }
1485
2.65k
  dst->id = src->id;
1486
2.65k
  if (src->der.value && src->der.len)   {
1487
2.65k
    dst->der.value = malloc(src->der.len);
1488
2.65k
    if (!dst->der.value) {
1489
0
      free(dst->named_curve);
1490
0
      return SC_ERROR_OUT_OF_MEMORY;
1491
0
    }
1492
2.65k
    memcpy(dst->der.value, src->der.value, src->der.len);
1493
2.65k
    dst->der.len = src->der.len;
1494
2.65k
  }
1495
2.65k
  dst->type = src->type;
1496
2.65k
  dst->field_length = src->field_length;
1497
2.65k
  dst->key_type = src->key_type;
1498
1499
2.65k
  return SC_SUCCESS;
1500
2.65k
}
1501
1502
scconf_block *
1503
sc_match_atr_block(sc_context_t *ctx, struct sc_card_driver *driver, struct sc_atr *atr)
1504
0
{
1505
0
  return _sc_match_atr_block(ctx, driver, atr);
1506
0
}
1507
1508
#ifdef ENABLE_SM
1509
static int
1510
sc_card_sm_unload(struct sc_card *card)
1511
14.2k
{
1512
14.2k
  if (card->sm_ctx.module.ops.module_cleanup)
1513
0
    card->sm_ctx.module.ops.module_cleanup(card->ctx);
1514
1515
14.2k
  if (card->sm_ctx.module.handle)
1516
0
    sc_dlclose(card->sm_ctx.module.handle);
1517
14.2k
  card->sm_ctx.module.handle = NULL;
1518
14.2k
  return 0;
1519
14.2k
}
1520
1521
1522
static int
1523
sc_card_sm_load(struct sc_card *card, const char *module_path, const char *in_module)
1524
0
{
1525
0
  struct sc_context *ctx = NULL;
1526
0
  int rv = SC_ERROR_INTERNAL;
1527
0
  char *module = NULL;
1528
#ifdef _WIN32
1529
  const char path_delim = '\\';
1530
#else
1531
0
  const char path_delim = '/';
1532
0
#endif
1533
1534
0
  if (card == NULL) {
1535
0
    return SC_ERROR_INVALID_ARGUMENTS;
1536
0
  }
1537
0
  ctx = card->ctx;
1538
0
  LOG_FUNC_CALLED(ctx);
1539
0
  if (!in_module)
1540
0
    return sc_card_sm_unload(card);
1541
1542
0
  sc_log(ctx, "SM module '%s' located in '%s'", in_module, module_path);
1543
0
  if (module_path && strlen(module_path) > 0)   {
1544
0
    size_t sz = strlen(in_module) + strlen(module_path) + 3;
1545
0
    module = malloc(sz);
1546
0
    if (module)
1547
0
      snprintf(module, sz, "%s%c%s", module_path, path_delim, in_module);
1548
0
  }
1549
0
  else   {
1550
0
    module = strdup(in_module);
1551
0
  }
1552
1553
0
  if (!module)
1554
0
    return SC_ERROR_OUT_OF_MEMORY;
1555
1556
0
  sc_log(ctx, "try to load SM module '%s'", module);
1557
0
  do  {
1558
0
    struct sm_module_operations *mod_ops = &card->sm_ctx.module.ops;
1559
0
    void *mod_handle;
1560
1561
0
    card->sm_ctx.module.handle = sc_dlopen(module);
1562
0
    if (!card->sm_ctx.module.handle)   {
1563
0
      sc_log(ctx, "cannot open dynamic library '%s': %s", module, sc_dlerror());
1564
0
      break;
1565
0
    }
1566
0
    mod_handle = card->sm_ctx.module.handle;
1567
1568
0
    mod_ops->initialize = sc_dlsym(mod_handle, "initialize");
1569
0
    if (!mod_ops->initialize)   {
1570
0
      sc_log(ctx, "SM handler 'initialize' not exported: %s", sc_dlerror());
1571
0
      break;
1572
0
    }
1573
1574
0
    mod_ops->get_apdus  = sc_dlsym(mod_handle, "get_apdus");
1575
0
    if (!mod_ops->get_apdus)   {
1576
0
      sc_log(ctx, "SM handler 'get_apdus' not exported: %s", sc_dlerror());
1577
0
      break;
1578
0
    }
1579
1580
0
    mod_ops->finalize  = sc_dlsym(mod_handle, "finalize");
1581
0
    if (!mod_ops->finalize)
1582
0
      sc_log(ctx, "SM handler 'finalize' not exported -- ignored");
1583
1584
0
    mod_ops->module_init  = sc_dlsym(mod_handle, "module_init");
1585
0
    if (!mod_ops->module_init)
1586
0
      sc_log(ctx, "SM handler 'module_init' not exported -- ignored");
1587
1588
0
    mod_ops->module_cleanup  = sc_dlsym(mod_handle, "module_cleanup");
1589
0
    if (!mod_ops->module_cleanup)
1590
0
      sc_log(ctx, "SM handler 'module_cleanup' not exported -- ignored");
1591
1592
0
    mod_ops->test  = sc_dlsym(mod_handle, "test");
1593
0
    if (mod_ops->test)
1594
0
      sc_log(ctx, "SM handler 'test' not exported -- ignored");
1595
1596
0
    rv = 0;
1597
0
    break;
1598
0
  } while(0);
1599
1600
0
  if (rv)
1601
0
    sc_card_sm_unload(card);
1602
1603
0
  card->sm_ctx.sm_mode = SM_MODE_ACL;
1604
0
  if (module)
1605
0
    free(module);
1606
1607
0
  SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_VERBOSE, rv);
1608
0
}
1609
1610
1611
/* get SM related configuration settings and initialize SM session, SM module, ... */
1612
static int
1613
sc_card_sm_check(struct sc_card *card)
1614
14.2k
{
1615
14.2k
  const char *sm = NULL, *module_name = NULL, *module_path = NULL, *module_data = NULL, *sm_mode = NULL;
1616
14.2k
  struct sc_context *ctx = card->ctx;
1617
14.2k
  scconf_block *atrblock = NULL, *sm_conf_block = NULL;
1618
14.2k
  int rv, ii;
1619
#ifdef _WIN32
1620
  char temp_path[PATH_MAX];
1621
  size_t temp_len = PATH_MAX - 1;
1622
  char expanded_val[PATH_MAX];
1623
  DWORD expanded_len = PATH_MAX;
1624
#endif
1625
1626
14.2k
  LOG_FUNC_CALLED(ctx);
1627
1628
  /* get the name of card specific SM configuration section */
1629
14.2k
  atrblock = _sc_match_atr_block(ctx, card->driver, &card->atr);
1630
14.2k
  if (atrblock == NULL)
1631
14.2k
    LOG_FUNC_RETURN(ctx, SC_SUCCESS);
1632
0
  sm = scconf_get_str(atrblock, "secure_messaging", NULL);
1633
0
  if (!sm)
1634
0
    LOG_FUNC_RETURN(ctx, SC_SUCCESS);
1635
1636
  /* get SM configuration section by the name */
1637
0
  sc_log(ctx, "secure_messaging configuration block '%s'", sm);
1638
0
        for (ii = 0; ctx->conf_blocks[ii]; ii++) {
1639
0
    scconf_block **blocks;
1640
1641
0
    blocks = scconf_find_blocks(ctx->conf, ctx->conf_blocks[ii], "secure_messaging", sm);
1642
0
    if (blocks) {
1643
0
      sm_conf_block = blocks[0];
1644
0
      free(blocks);
1645
0
    }
1646
0
    if (sm_conf_block != NULL)
1647
0
      break;
1648
0
  }
1649
1650
0
  if (!sm_conf_block)
1651
0
    LOG_TEST_RET(ctx, SC_ERROR_INCONSISTENT_CONFIGURATION, "SM configuration block not preset");
1652
1653
  /* check if an external SM module has to be used */
1654
#ifdef _WIN32
1655
  rv = sc_ctx_win32_get_config_value(NULL, "SmDir", "Software\\" OPENSC_VS_FF_COMPANY_NAME "\\OpenSC" OPENSC_ARCH_SUFFIX,
1656
      temp_path, &temp_len);
1657
  if (rv == SC_SUCCESS) {
1658
    temp_path[temp_len] = '\0';
1659
    module_path = temp_path;
1660
  }
1661
  expanded_len = ExpandEnvironmentStringsA(module_path, expanded_val, expanded_len);
1662
  if (0 < expanded_len && expanded_len < sizeof expanded_val)
1663
    module_path = expanded_val;
1664
#else
1665
0
  module_path = scconf_get_str(sm_conf_block, "module_path", DEFAULT_SM_MODULE_PATH);
1666
0
#endif
1667
0
  module_name = scconf_get_str(sm_conf_block, "module_name", DEFAULT_SM_MODULE);
1668
0
  sc_log(ctx, "SM module '%s' in  '%s'", module_name, module_path);
1669
0
  if (!module_name)
1670
0
    LOG_TEST_RET(ctx, SC_ERROR_INCONSISTENT_CONFIGURATION, "Invalid SM configuration: module not defined");
1671
1672
0
  rv = sc_card_sm_load(card, module_path, module_name);
1673
0
  LOG_TEST_RET(ctx, rv, "Failed to load SM module");
1674
1675
0
  strlcpy(card->sm_ctx.module.filename, module_name, sizeof(card->sm_ctx.module.filename));
1676
0
  strlcpy(card->sm_ctx.config_section, sm, sizeof(card->sm_ctx.config_section));
1677
1678
  /* allocate resources for the external SM module */
1679
0
  if (card->sm_ctx.module.ops.module_init)   {
1680
0
    module_data = scconf_get_str(sm_conf_block, "module_data", NULL);
1681
1682
0
    rv = card->sm_ctx.module.ops.module_init(ctx, module_data);
1683
0
    LOG_TEST_RET(ctx, rv, "Cannot initialize SM module");
1684
0
  }
1685
1686
  /* initialize SM session in the case of 'APDU TRANSMIT' SM mode */
1687
0
  sm_mode = scconf_get_str(sm_conf_block, "mode", NULL);
1688
0
  if (sm_mode && !strcasecmp("Transmit", sm_mode))   {
1689
0
    if (!card->sm_ctx.ops.open || !card->sm_ctx.ops.get_sm_apdu || !card->sm_ctx.ops.free_sm_apdu)
1690
0
      LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "'Transmit' SM asked but not supported by card driver");
1691
1692
0
    card->sm_ctx.sm_mode = SM_MODE_TRANSMIT;
1693
0
    rv = card->sm_ctx.ops.open(card);
1694
0
    LOG_TEST_RET(ctx, rv, "Cannot initialize SM");
1695
0
  }
1696
1697
0
  sc_log(ctx, "SM mode:%X", card->sm_ctx.sm_mode);
1698
0
  SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_VERBOSE, rv);
1699
0
}
1700
#endif