Coverage Report

Created: 2025-10-10 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/ctx.c
Line
Count
Source
1
/*
2
 * ctx.c: Context related functions
3
 *
4
 * Copyright (C) 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 <stdio.h>
26
#include <stdlib.h>
27
#include <string.h>
28
#include <assert.h>
29
#include <errno.h>
30
#include <sys/stat.h>
31
#include <limits.h>
32
33
#ifdef _WIN32
34
#include <windows.h>
35
#include <winreg.h>
36
#include <direct.h>
37
#include <io.h>
38
#endif
39
40
#ifdef __APPLE__
41
#include <libproc.h>
42
#endif
43
44
#include "common/libscdl.h"
45
#include "common/compat_strlcpy.h"
46
#include "internal.h"
47
#ifdef ENABLE_OPENSSL
48
#include <openssl/crypto.h>
49
#include "sc-ossl-compat.h"
50
#endif
51
52
53
static int ignored_reader(sc_context_t *ctx, sc_reader_t *reader)
54
0
{
55
0
  if (ctx != NULL && reader != NULL && reader->name != NULL) {
56
0
    size_t i;
57
0
    const scconf_list *list;
58
59
0
    for (i = 0; ctx->conf_blocks[i]; i++) {
60
0
      list = scconf_find_list(ctx->conf_blocks[i], "ignored_readers");
61
0
      while (list != NULL) {
62
0
        if (strstr(reader->name, list->data) != NULL) {
63
0
          sc_log(ctx, "Ignoring reader \'%s\' because of \'%s\'\n",
64
0
              reader->name, list->data);
65
0
          return 1;
66
0
        }
67
0
        list = list->next;
68
0
      }
69
0
    }
70
0
  }
71
72
0
  return 0;
73
0
}
74
75
int _sc_add_reader(sc_context_t *ctx, sc_reader_t *reader)
76
0
{
77
0
  if (reader == NULL || ignored_reader(ctx, reader)) {
78
0
    return SC_ERROR_INVALID_ARGUMENTS;
79
0
  }
80
0
  reader->ctx = ctx;
81
0
  list_append(&ctx->readers, reader);
82
0
  return SC_SUCCESS;
83
0
}
84
85
int _sc_delete_reader(sc_context_t *ctx, sc_reader_t *reader)
86
14
{
87
14
  if (reader == NULL) {
88
0
    return SC_ERROR_INVALID_ARGUMENTS;
89
0
  }
90
14
  if (reader->ops->release)
91
14
      reader->ops->release(reader);
92
14
  free(reader->name);
93
14
  free(reader->vendor);
94
14
  list_delete(&ctx->readers, reader);
95
14
  free(reader);
96
14
  return SC_SUCCESS;
97
14
}
98
99
struct _sc_driver_entry {
100
  const char *name;
101
  void *(*func)(void);
102
};
103
104
// clang-format off
105
static const struct _sc_driver_entry internal_card_drivers[] = {
106
  /* The card handled by skeid shares the ATR with other cards running CardOS 5.4.
107
   * In order to prevent the cardos driver from matching skeid cards, skeid driver
108
   * precedes cardos and matches no other CardOS 5.4 card. */
109
  { "skeid",  (void *(*)(void)) sc_get_skeid_driver },
110
  /* The card handled by dtrust shares the ATR with other cards running CardOS 5.4.
111
   * In order to prevent the cardos driver from matching dtrust cards, dtrust driver
112
   * precedes cardos and matches no other CardOS 5.4 card. */
113
  { "dtrust", (void *(*)(void)) sc_get_dtrust_driver },
114
  { "cardos", (void *(*)(void)) sc_get_cardos_driver },
115
  { "gemsafeV1",  (void *(*)(void)) sc_get_gemsafeV1_driver },
116
  { "starcos",  (void *(*)(void)) sc_get_starcos_driver },
117
  { "tcos", (void *(*)(void)) sc_get_tcos_driver },
118
#ifdef ENABLE_OPENSSL
119
  { "oberthur", (void *(*)(void)) sc_get_oberthur_driver },
120
  { "authentic",  (void *(*)(void)) sc_get_authentic_driver },
121
  { "iasecc", (void *(*)(void)) sc_get_iasecc_driver },
122
#endif
123
  { "belpic", (void *(*)(void)) sc_get_belpic_driver },
124
#ifdef ENABLE_OPENSSL
125
  { "entersafe",(void *(*)(void)) sc_get_entersafe_driver },
126
#ifdef ENABLE_SM
127
  { "epass2003",(void *(*)(void)) sc_get_epass2003_driver },
128
#endif
129
#endif
130
  { "rutoken",  (void *(*)(void)) sc_get_rutoken_driver },
131
  { "rutoken_ecp",(void *(*)(void)) sc_get_rtecp_driver },
132
  { "myeid",      (void *(*)(void)) sc_get_myeid_driver },
133
#if defined(ENABLE_OPENSSL) && defined(ENABLE_SM)
134
  { "dnie",       (void *(*)(void)) sc_get_dnie_driver },
135
#endif
136
  { "masktech", (void *(*)(void)) sc_get_masktech_driver },
137
  { "idprime",  (void *(*)(void)) sc_get_idprime_driver },
138
#if defined(ENABLE_SM) && defined(ENABLE_OPENPACE)
139
  { "edo",        (void *(*)(void)) sc_get_edo_driver },
140
#endif
141
142
/* Here should be placed drivers that need some APDU transactions in the
143
 * driver's `match_card()` function. */
144
  { "esteid2018", (void *(*)(void)) sc_get_esteid2018_driver },
145
  { "esteid2025", (void *(*)(void)) sc_get_esteid2025_driver },
146
  { "coolkey",  (void *(*)(void)) sc_get_coolkey_driver },
147
  /* MUSCLE card applet returns 9000 on whatever AID is selected, see
148
   * https://github.com/JavaCardOS/MuscleCard-Applet/blob/master/musclecard/src/com/musclecard/CardEdge/CardEdge.java#L326
149
   * put the muscle driver first to cope with this bug. */
150
  { "muscle", (void *(*)(void)) sc_get_muscle_driver },
151
  { "sc-hsm", (void *(*)(void)) sc_get_sc_hsm_driver },
152
  { "setcos", (void *(*)(void)) sc_get_setcos_driver },
153
  { "PIV-II", (void *(*)(void)) sc_get_piv_driver },
154
  { "cac",  (void *(*)(void)) sc_get_cac_driver },
155
  { "itacns", (void *(*)(void)) sc_get_itacns_driver },
156
  { "isoApplet",  (void *(*)(void)) sc_get_isoApplet_driver },
157
#ifdef ENABLE_ZLIB
158
  { "gids", (void *(*)(void)) sc_get_gids_driver },
159
#endif
160
  { "openpgp",  (void *(*)(void)) sc_get_openpgp_driver },
161
  { "jpki", (void *(*)(void)) sc_get_jpki_driver },
162
  { "npa",  (void *(*)(void)) sc_get_npa_driver },
163
  { "cac1", (void *(*)(void)) sc_get_cac1_driver },
164
  { "nqapplet", (void *(*)(void)) sc_get_nqApplet_driver },
165
#if defined(ENABLE_SM) && defined(ENABLE_OPENPACE)
166
  { "eOI",  (void *(*)(void)) sc_get_eoi_driver },
167
#endif
168
  /* The default driver should be last, as it handles all the
169
   * unrecognized cards. */
170
  { "default",  (void *(*)(void)) sc_get_default_driver },
171
  { NULL, NULL }
172
};
173
174
static const struct _sc_driver_entry old_card_drivers[] = {
175
  { "asepcos",    (void *(*)(void)) sc_get_asepcos_driver },
176
  { "atrust-acos",(void *(*)(void)) sc_get_atrust_acos_driver },
177
  { "cyberflex",  (void *(*)(void)) sc_get_cyberflex_driver },
178
  { "flex",       (void *(*)(void)) sc_get_cryptoflex_driver },
179
  { "mcrd",       (void *(*)(void)) sc_get_mcrd_driver },
180
  { NULL, NULL }
181
};
182
// clang-format on
183
184
struct _sc_ctx_options {
185
  struct _sc_driver_entry cdrv[SC_MAX_CARD_DRIVERS];
186
  int ccount;
187
};
188
189
190
int
191
sc_ctx_win32_get_config_value(const char *name_env,
192
      const char *name_reg, const char *name_key,
193
    void *out, size_t *out_len)
194
0
{
195
#ifdef _WIN32
196
  long rc;
197
  HKEY hKey;
198
199
  if (!out || !out_len)
200
    return SC_ERROR_INVALID_ARGUMENTS;
201
202
  if (name_env)   {
203
    char *value = getenv(name_env);
204
    if (value) {
205
      if (strlen(value) > *out_len)
206
        return SC_ERROR_NOT_ENOUGH_MEMORY;
207
      memcpy(out, value, strlen(value));
208
      *out_len = strlen(value);
209
      return SC_SUCCESS;
210
    }
211
  }
212
213
  if (!name_reg)
214
    return SC_ERROR_INVALID_ARGUMENTS;
215
216
  if (!name_key)
217
    name_key = "Software\\" OPENSC_VS_FF_COMPANY_NAME "\\OpenSC" OPENSC_ARCH_SUFFIX;
218
219
  rc = RegOpenKeyExA(HKEY_CURRENT_USER, name_key, 0, KEY_QUERY_VALUE, &hKey);
220
  if (rc == ERROR_SUCCESS) {
221
    DWORD len = *out_len;
222
    rc = RegQueryValueEx(hKey, name_reg, NULL, NULL, out, &len);
223
    RegCloseKey(hKey);
224
    if (rc == ERROR_SUCCESS) {
225
      *out_len = len;
226
      return SC_SUCCESS;
227
    }
228
  }
229
230
  rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, name_key, 0, KEY_QUERY_VALUE, &hKey);
231
  if (rc == ERROR_SUCCESS) {
232
    DWORD len = *out_len;
233
    rc = RegQueryValueEx(hKey, name_reg, NULL, NULL, out, &len);
234
    RegCloseKey(hKey);
235
    if (rc == ERROR_SUCCESS) {
236
      *out_len = len;
237
      return SC_SUCCESS;
238
    }
239
  }
240
241
  return SC_ERROR_OBJECT_NOT_FOUND;
242
#else
243
0
  return SC_ERROR_NOT_SUPPORTED;
244
0
#endif
245
0
}
246
247
248
/* Simclist helper to locate readers by name */
249
0
static int reader_list_seeker(const void *el, const void *key) {
250
0
  const struct sc_reader *reader = (struct sc_reader *)el;
251
0
  if ((el == NULL) || (key == NULL))
252
0
    return 0;
253
0
  if (strcmp(reader->name, (char*)key) == 0)
254
0
    return 1;
255
0
  return 0;
256
0
}
257
258
static void del_drvs(struct _sc_ctx_options *opts)
259
546
{
260
546
  struct _sc_driver_entry *lst;
261
546
  int *cp, i;
262
263
546
  lst = opts->cdrv;
264
546
  cp = &opts->ccount;
265
266
22.1k
  for (i = 0; i < *cp; i++) {
267
21.5k
    free((void *)lst[i].name);
268
21.5k
  }
269
546
  *cp = 0;
270
546
}
271
272
static void add_drv(struct _sc_ctx_options *opts, const char *name)
273
21.5k
{
274
21.5k
  struct _sc_driver_entry *lst;
275
21.5k
  int *cp, max, i;
276
277
21.5k
  lst = opts->cdrv;
278
21.5k
  cp = &opts->ccount;
279
21.5k
  max = SC_MAX_CARD_DRIVERS;
280
21.5k
  if (*cp == max) /* No space for more drivers... */
281
0
    return;
282
438k
  for (i = 0; i < *cp; i++)
283
416k
    if (strcmp(name, lst[i].name) == 0)
284
0
      return;
285
21.5k
  lst[*cp].name = strdup(name);
286
287
21.5k
  *cp = *cp + 1;
288
21.5k
}
289
290
static void add_internal_drvs(struct _sc_ctx_options *opts)
291
546
{
292
546
  const struct _sc_driver_entry *lst;
293
546
  int i;
294
295
546
  lst = internal_card_drivers;
296
546
  i = 0;
297
20.7k
  while (lst[i].name != NULL) {
298
20.2k
    add_drv(opts, lst[i].name);
299
20.2k
    i++;
300
20.2k
  }
301
546
}
302
303
static void add_old_drvs(struct _sc_ctx_options *opts)
304
273
{
305
273
  const struct _sc_driver_entry *lst;
306
273
  int i;
307
308
273
  lst = old_card_drivers;
309
273
  i = 0;
310
1.63k
  while (lst[i].name != NULL) {
311
1.36k
    add_drv(opts, lst[i].name);
312
1.36k
    i++;
313
1.36k
  }
314
273
}
315
316
static void set_defaults(sc_context_t *ctx, struct _sc_ctx_options *opts)
317
273
{
318
273
  ctx->debug = 0;
319
273
  if (ctx->debug_file && (ctx->debug_file != stderr && ctx->debug_file != stdout))
320
0
    fclose(ctx->debug_file);
321
273
  ctx->debug_file = stderr;
322
273
  ctx->flags = 0;
323
273
  ctx->forced_driver = NULL;
324
273
  add_internal_drvs(opts);
325
273
}
326
327
/* In Windows, file handles can not be shared between DLL-s,
328
 * each DLL has a separate file handle table. Thus tools and utilities
329
 * can not set the file handle themselves when -v is specified on command line.
330
 */
331
int sc_ctx_log_to_file(sc_context_t *ctx, const char* filename)
332
0
{
333
  /* Close any existing handles */
334
0
  if (ctx->debug_file && (ctx->debug_file != stderr && ctx->debug_file != stdout))   {
335
0
    fclose(ctx->debug_file);
336
0
    ctx->debug_file = NULL;
337
0
  }
338
339
0
  if (!ctx->debug_filename)   {
340
0
    if (!filename)
341
0
      filename = "stderr";
342
0
    ctx->debug_filename = strdup(filename);
343
0
  }
344
345
0
  if (!filename)
346
0
    return SC_SUCCESS;
347
348
  /* Handle special names */
349
0
  if (!strcmp(filename, "stdout"))
350
0
    ctx->debug_file = stdout;
351
0
  else if (!strcmp(filename, "stderr"))
352
0
    ctx->debug_file = stderr;
353
0
  else {
354
0
    ctx->debug_file = fopen(filename, "a");
355
0
    if (ctx->debug_file == NULL)
356
0
      return SC_ERROR_INTERNAL;
357
0
  }
358
0
  return SC_SUCCESS;
359
0
}
360
361
static void
362
set_drivers(struct _sc_ctx_options *opts, const scconf_list *list)
363
273
{
364
273
  const char *s_internal = "internal", *s_old = "old";
365
273
  if (list != NULL)
366
273
    del_drvs(opts);
367
819
  while (list != NULL) {
368
546
    if (strcmp(list->data, s_internal) == 0)
369
273
      add_internal_drvs(opts);
370
273
    else if (strcmp(list->data, s_old) == 0)
371
273
      add_old_drvs(opts);
372
0
    else
373
0
      add_drv(opts, list->data);
374
546
    list = list->next;
375
546
  }
376
273
}
377
378
static int
379
load_parameters(sc_context_t *ctx, scconf_block *block, struct _sc_ctx_options *opts)
380
273
{
381
273
  int err = 0;
382
273
  const scconf_list *list;
383
273
  const char *val;
384
273
  int debug;
385
273
  const char *disable_hw_pkcs1_padding;
386
#ifdef _WIN32
387
  char expanded_val[PATH_MAX];
388
  DWORD expanded_len;
389
#endif
390
391
273
  debug = scconf_get_int(block, "debug", ctx->debug);
392
273
  if (debug > ctx->debug)
393
0
    ctx->debug = debug;
394
395
273
  val = scconf_get_str(block, "debug_file", NULL);
396
273
  if (val)   {
397
#ifdef _WIN32
398
    expanded_len = PATH_MAX;
399
    expanded_len = ExpandEnvironmentStringsA(val, expanded_val, expanded_len);
400
    if (0 < expanded_len && expanded_len < sizeof expanded_val)
401
      val = expanded_val;
402
#endif
403
0
    sc_ctx_log_to_file(ctx, val);
404
0
  }
405
273
  else if (ctx->debug)   {
406
0
    sc_ctx_log_to_file(ctx, NULL);
407
0
  }
408
409
273
  if (scconf_get_bool (block, "disable_popups",
410
273
        ctx->flags & SC_CTX_FLAG_DISABLE_POPUPS))
411
0
    ctx->flags |= SC_CTX_FLAG_DISABLE_POPUPS;
412
413
273
  if (scconf_get_bool (block, "disable_colors",
414
273
        ctx->flags & SC_CTX_FLAG_DISABLE_COLORS))
415
0
    ctx->flags |= SC_CTX_FLAG_DISABLE_COLORS;
416
417
273
  if (scconf_get_bool (block, "enable_default_driver",
418
273
        ctx->flags & SC_CTX_FLAG_ENABLE_DEFAULT_DRIVER))
419
0
    ctx->flags |= SC_CTX_FLAG_ENABLE_DEFAULT_DRIVER;
420
421
273
  list = scconf_find_list(block, "card_drivers");
422
273
  set_drivers(opts, list);
423
424
  /* Disable PKCS#1 v1.5 type 2 (for decryption) depadding on card by default */
425
273
  disable_hw_pkcs1_padding = "decipher";
426
273
  ctx->disable_hw_pkcs1_padding = SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02;
427
273
  disable_hw_pkcs1_padding = scconf_get_str(block, "disable_hw_pkcs1_padding", disable_hw_pkcs1_padding);
428
273
  if (0 == strcmp(disable_hw_pkcs1_padding, "no")) {
429
0
    ctx->disable_hw_pkcs1_padding = 0;
430
273
  } else if (0 == strcmp(disable_hw_pkcs1_padding, "sign")) {
431
0
    ctx->disable_hw_pkcs1_padding = SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01;
432
273
  } else if (0 == strcmp(disable_hw_pkcs1_padding, "decipher")) {
433
273
    ctx->disable_hw_pkcs1_padding = SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02;
434
273
  } else if (0 == strcmp(disable_hw_pkcs1_padding, "both")) {
435
0
    ctx->disable_hw_pkcs1_padding = SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01 | SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02;
436
0
  }
437
438
273
  return err;
439
273
}
440
441
442
/**
443
 * find library module for provided driver in configuration file
444
 * if not found assume library name equals to module name
445
 */
446
static const char *find_library(sc_context_t *ctx, const char *name)
447
0
{
448
0
  int i, log_warning;
449
0
  const char *libname = NULL;
450
0
  scconf_block **blocks, *blk;
451
452
0
  for (i = 0; ctx->conf_blocks[i]; i++) {
453
0
    blocks = scconf_find_blocks(ctx->conf, ctx->conf_blocks[i], "card_driver", name);
454
0
    if (!blocks)
455
0
      continue;
456
0
    blk = blocks[0];
457
0
    free(blocks);
458
0
    if (blk == NULL)
459
0
      continue;
460
0
    libname = scconf_get_str(blk, "module", name);
461
#ifdef _WIN32
462
    log_warning = libname && libname[0] != '\\';
463
#else
464
0
    log_warning = libname && libname[0] != '/';
465
0
#endif
466
0
    if (log_warning)
467
0
      sc_log(ctx, "warning: relative path to driver '%s' used", libname);
468
0
    break;
469
0
  }
470
471
0
  return libname;
472
0
}
473
474
/**
475
 * load card/reader driver modules
476
 * Every module should contain a function " void * sc_module_init(char *) "
477
 * that returns a pointer to the function _sc_get_xxxx_driver()
478
 * used to initialize static modules
479
 * Also, an exported "char *sc_module_version" variable should exist in module
480
 */
481
static void *load_dynamic_driver(sc_context_t *ctx, void **dll, const char *name)
482
0
{
483
0
  const char *version, *libname;
484
0
  void *handle;
485
0
  void *(*modinit)(const char *) = NULL;
486
0
  void *(**tmodi)(const char *) = &modinit;
487
0
  const char *(*modversion)(void) = NULL;
488
0
  const char *(**tmodv)(void) = &modversion;
489
490
0
  if (dll == NULL) {
491
0
    sc_log(ctx, "No dll parameter specified");
492
0
    return NULL;
493
0
  }
494
0
  if (name == NULL) { /* should not occur, but... */
495
0
    sc_log(ctx, "No module specified");
496
0
    return NULL;
497
0
  }
498
0
  libname = find_library(ctx, name);
499
0
  if (libname == NULL)
500
0
    return NULL;
501
0
  handle = sc_dlopen(libname);
502
0
  if (handle == NULL) {
503
0
    sc_log(ctx, "Module %s: cannot load %s library: %s", name, libname, sc_dlerror());
504
0
    return NULL;
505
0
  }
506
507
  /* verify correctness of module */
508
0
  *(void **)tmodi = sc_dlsym(handle, "sc_module_init");
509
0
  *(void **)tmodv = sc_dlsym(handle, "sc_driver_version");
510
0
  if (modinit == NULL || modversion == NULL) {
511
0
    sc_log(ctx, "dynamic library '%s' is not a OpenSC module",libname);
512
0
    sc_dlclose(handle);
513
0
    return NULL;
514
0
  }
515
  /* verify module version */
516
0
  version = modversion();
517
  /* XXX: We really need to have ABI version for each interface */
518
0
  if (version == NULL || strncmp(version, PACKAGE_VERSION, strlen(PACKAGE_VERSION)) != 0) {
519
0
    sc_log(ctx, "dynamic library '%s': invalid module version", libname);
520
0
    sc_dlclose(handle);
521
0
    return NULL;
522
0
  }
523
524
0
  *dll = handle;
525
0
  sc_log(ctx, "successfully loaded card driver '%s'", name);
526
0
  return modinit(name);
527
0
}
528
529
static int load_card_driver_options(sc_context_t *ctx,
530
    struct sc_card_driver *driver)
531
11.4k
{
532
11.4k
  scconf_block **blocks, *blk;
533
11.4k
  int i;
534
535
22.9k
  for (i = 0; ctx->conf_blocks[i]; i++) {
536
11.4k
    blocks = scconf_find_blocks(ctx->conf, ctx->conf_blocks[i],
537
11.4k
        "card_driver", driver->short_name);
538
11.4k
    if (!blocks)
539
0
      continue;
540
11.4k
    blk = blocks[0];
541
11.4k
    free(blocks);
542
543
11.4k
    if (blk == NULL)
544
11.4k
      continue;
545
546
    /* no options at the moment */
547
11.4k
  }
548
11.4k
  return SC_SUCCESS;
549
11.4k
}
550
551
static int load_card_drivers(sc_context_t *ctx, struct _sc_ctx_options *opts)
552
273
{
553
273
  const struct _sc_driver_entry *ent;
554
273
  int drv_count;
555
273
  int i;
556
557
273
  for (drv_count = 0; ctx->card_drivers[drv_count] != NULL; drv_count++)
558
0
    ;
559
560
11.7k
  for (i = 0; i < opts->ccount; i++) {
561
11.4k
    struct sc_card_driver *(*func)(void) = NULL;
562
11.4k
    struct sc_card_driver *(**tfunc)(void) = &func;
563
11.4k
    void *dll = NULL;
564
11.4k
    int  j;
565
566
11.4k
    if (drv_count >= SC_MAX_CARD_DRIVERS - 1)   {
567
0
      sc_log(ctx, "Not more then %i card drivers allowed.", SC_MAX_CARD_DRIVERS);
568
0
      break;
569
0
    }
570
571
11.4k
    ent = &opts->cdrv[i];
572
243k
    for (j = 0; internal_card_drivers[j].name != NULL; j++) {
573
242k
      if (strcmp(ent->name, internal_card_drivers[j].name) == 0) {
574
10.1k
        func = (struct sc_card_driver *(*)(void)) internal_card_drivers[j].func;
575
10.1k
        break;
576
10.1k
      }
577
242k
    }
578
11.4k
    if (func == NULL) {
579
4.09k
      for (j = 0; old_card_drivers[j].name != NULL; j++) {
580
4.09k
        if (strcmp(ent->name, old_card_drivers[j].name) == 0) {
581
1.36k
          func = (struct sc_card_driver *(*)(void)) old_card_drivers[j].func;
582
1.36k
          break;
583
1.36k
        }
584
4.09k
      }
585
1.36k
    }
586
    /* if not initialized assume external module */
587
11.4k
    if (func == NULL)
588
0
      *(void **)(tfunc) = load_dynamic_driver(ctx, &dll, ent->name);
589
    /* if still null, assume driver not found */
590
11.4k
    if (func == NULL) {
591
0
      sc_log(ctx, "Unable to load '%s'.", ent->name);
592
0
      if (dll)
593
0
        sc_dlclose(dll);
594
0
      continue;
595
0
    }
596
597
11.4k
    ctx->card_drivers[drv_count] = func();
598
11.4k
    if (ctx->card_drivers[drv_count] == NULL) {
599
0
      sc_log(ctx, "Driver '%s' not available.", ent->name);
600
0
      continue;
601
0
    }
602
603
11.4k
    ctx->card_drivers[drv_count]->dll = dll;
604
11.4k
    ctx->card_drivers[drv_count]->atr_map = NULL;
605
11.4k
    ctx->card_drivers[drv_count]->natrs = 0;
606
607
11.4k
    load_card_driver_options(ctx, ctx->card_drivers[drv_count]);
608
609
    /* Ensure that the list is always terminated by NULL */
610
11.4k
    ctx->card_drivers[drv_count + 1] = NULL;
611
612
11.4k
    drv_count++;
613
11.4k
  }
614
273
  return SC_SUCCESS;
615
273
}
616
617
static int load_card_atrs(sc_context_t *ctx)
618
273
{
619
273
  struct sc_card_driver *driver;
620
273
  scconf_block **blocks;
621
273
  int i, j, k;
622
623
546
  for (i = 0; ctx->conf_blocks[i] != NULL; i++) {
624
273
    blocks = scconf_find_blocks(ctx->conf, ctx->conf_blocks[i], "card_atr", NULL);
625
273
    if (!blocks)
626
0
      continue;
627
273
    for (j = 0; blocks[j] != NULL; j++) {
628
0
      scconf_block *b = blocks[j];
629
0
      char *atr = b->name->data;
630
0
      const scconf_list *list;
631
0
      struct sc_atr_table t;
632
0
      const char *dname;
633
634
0
      driver = NULL;
635
636
0
      if (strlen(atr) < 4)
637
0
        continue;
638
639
      /* The interesting part. If there's no card
640
       * driver assigned for the ATR, add it to
641
       * the default driver. This will reduce the
642
       * amount of code required to process things
643
       * related to card_atr blocks in situations,
644
       * where the code is not exactly related to
645
       * card driver settings, but for example
646
       * forcing a protocol at the reader driver.
647
       */
648
0
      dname = scconf_get_str(b, "driver", "default");
649
650
      /* Find the card driver structure according to dname */
651
0
      for (k = 0; ctx->card_drivers[k] != NULL; k++) {
652
0
        driver = ctx->card_drivers[k];
653
0
        if (!strcmp(dname, driver->short_name))
654
0
          break;
655
0
        driver = NULL;
656
0
      }
657
658
0
      if (!driver)
659
0
        continue;
660
661
0
      memset(&t, 0, sizeof(struct sc_atr_table));
662
0
      t.atr = atr;
663
0
      t.atrmask = (char *) scconf_get_str(b, "atrmask", NULL);
664
0
      t.name = (char *) scconf_get_str(b, "name", NULL);
665
0
      t.type = scconf_get_int(b, "type", SC_CARD_TYPE_UNKNOWN);
666
0
      list = scconf_find_list(b, "flags");
667
0
      while (list != NULL) {
668
0
        unsigned int flags = 0;
669
670
0
        if (!list->data) {
671
0
          list = list->next;
672
0
          continue;
673
0
        }
674
675
0
        if (!strcmp(list->data, "rng"))
676
0
          flags = SC_CARD_FLAG_RNG;
677
0
        else if (!strcmp(list->data, "keep_alive"))
678
0
          flags = SC_CARD_FLAG_KEEP_ALIVE;
679
0
        else if (sscanf(list->data, "%x", &flags) != 1)
680
0
          flags = 0;
681
682
0
        t.flags |= flags;
683
0
        list = list->next;
684
0
      }
685
0
      t.card_atr = b;
686
0
      _sc_add_atr(ctx, driver, &t);
687
0
    }
688
273
    free(blocks);
689
273
  }
690
273
  return SC_SUCCESS;
691
273
}
692
693
static void process_config_file(sc_context_t *ctx, struct _sc_ctx_options *opts)
694
273
{
695
273
  int i, r, count = 0;
696
273
  scconf_block **blocks;
697
273
  const char *conf_path = NULL;
698
273
  const char *debug = NULL;
699
#ifdef _WIN32
700
  char temp_path[PATH_MAX];
701
  size_t temp_len;
702
#endif
703
704
  /* Takes effect even when no config around */
705
273
  debug = getenv("OPENSC_DEBUG");
706
273
  if (debug)
707
0
    ctx->debug = atoi(debug);
708
709
273
  memset(ctx->conf_blocks, 0, sizeof(ctx->conf_blocks));
710
#ifdef _WIN32
711
  temp_len = PATH_MAX-1;
712
  r = sc_ctx_win32_get_config_value("OPENSC_CONF", "ConfigFile", "Software\\" OPENSC_VS_FF_COMPANY_NAME "\\OpenSC" OPENSC_ARCH_SUFFIX,
713
      temp_path, &temp_len);
714
  if (r)   {
715
    sc_log(ctx, "process_config_file doesn't find opensc config file. Please set the registry key.");
716
    return;
717
  }
718
  temp_path[temp_len] = '\0';
719
  conf_path = temp_path;
720
#else
721
273
  conf_path = getenv("OPENSC_CONF");
722
273
  if (!conf_path)
723
273
    conf_path = OPENSC_CONF_PATH;
724
273
#endif
725
273
  ctx->conf = scconf_new(conf_path);
726
273
  if (ctx->conf == NULL)
727
0
    return;
728
273
  r = scconf_parse(ctx->conf);
729
273
#define OPENSC_CONFIG_STRING "app default { card_drivers = old, internal; }"
730
273
#ifdef OPENSC_CONFIG_STRING
731
  /* Parse the string if config file didn't exist */
732
273
  if (r < 0)
733
273
    r = scconf_parse_string(ctx->conf, OPENSC_CONFIG_STRING);
734
273
#endif
735
273
  if (r < 1) {
736
    /* A negative return value means the config file isn't
737
     * there, which is not an error. Nevertheless log this
738
     * fact. */
739
0
    if (r < 0)
740
0
      sc_log(ctx, "scconf_parse failed: %s", ctx->conf->errmsg);
741
0
    else
742
0
      sc_log(ctx, "scconf_parse failed: %s", ctx->conf->errmsg);
743
0
    scconf_free(ctx->conf);
744
0
    ctx->conf = NULL;
745
0
    return;
746
0
  }
747
  /* needs to be after the log file is known */
748
273
  sc_log(ctx, "Used configuration file '%s'", conf_path);
749
273
  blocks = scconf_find_blocks(ctx->conf, NULL, "app", ctx->exe_path);
750
273
  if (blocks && blocks[0])
751
0
    ctx->conf_blocks[count++] = blocks[0];
752
273
  free(blocks);
753
273
  blocks = scconf_find_blocks(ctx->conf, NULL, "app", ctx->app_name);
754
273
  if (blocks && blocks[0])
755
0
    ctx->conf_blocks[count++] = blocks[0];
756
273
  free(blocks);
757
273
  if (strcmp(ctx->app_name, "default") != 0) {
758
273
    blocks = scconf_find_blocks(ctx->conf, NULL, "app", "default");
759
273
    if (blocks && blocks[0])
760
273
      ctx->conf_blocks[count] = blocks[0];
761
273
    free(blocks);
762
273
  }
763
  /* Above we add 3 blocks at most, but conf_blocks has 4 elements,
764
   * so at least one is NULL */
765
546
  for (i = 0; ctx->conf_blocks[i]; i++)
766
273
    load_parameters(ctx, ctx->conf_blocks[i], opts);
767
273
}
768
769
int sc_ctx_detect_readers(sc_context_t *ctx)
770
273
{
771
273
  int r = 0;
772
273
  const struct sc_reader_driver *drv = ctx->reader_driver;
773
774
273
  sc_mutex_lock(ctx, ctx->mutex);
775
776
273
  if (drv->ops->detect_readers != NULL)
777
0
    r = drv->ops->detect_readers(ctx);
778
779
273
  sc_mutex_unlock(ctx, ctx->mutex);
780
781
273
  return r;
782
273
}
783
784
sc_reader_t *sc_ctx_get_reader(sc_context_t *ctx, unsigned int i)
785
14
{
786
14
  return list_get_at(&ctx->readers, i);
787
14
}
788
789
sc_reader_t *sc_ctx_get_reader_by_id(sc_context_t *ctx, unsigned int id)
790
0
{
791
0
  return list_get_at(&ctx->readers, id);
792
0
}
793
794
sc_reader_t *sc_ctx_get_reader_by_name(sc_context_t *ctx, const char * name)
795
0
{
796
0
  return list_seek(&ctx->readers, name);
797
0
}
798
799
unsigned int sc_ctx_get_reader_count(sc_context_t *ctx)
800
0
{
801
0
  return list_size(&ctx->readers);
802
0
}
803
804
int sc_establish_context(sc_context_t **ctx_out, const char *app_name)
805
0
{
806
0
  sc_context_param_t ctx_param;
807
808
0
  memset(&ctx_param, 0, sizeof(sc_context_param_t));
809
0
  ctx_param.ver      = 0;
810
0
  ctx_param.app_name = app_name;
811
0
  return sc_context_create(ctx_out, &ctx_param);
812
0
}
813
814
/* For multithreaded issues */
815
int sc_context_repair(sc_context_t **ctx_out)
816
0
{
817
  /* Must already exist */
818
0
  if ((ctx_out == NULL) || (*ctx_out == NULL) ||
819
0
      ((*ctx_out)->app_name == NULL))
820
0
    return SC_ERROR_INVALID_ARGUMENTS;
821
822
  /* The only thing that should be shared across different contexts are the
823
   * card drivers - so rebuild the ATR's
824
   */
825
0
  load_card_atrs(*ctx_out);
826
827
  /* TODO: May need to re-open any card driver DLL's */
828
829
0
  return SC_SUCCESS;
830
0
}
831
832
#ifdef USE_OPENSSL3_LIBCTX
833
static int sc_openssl3_init(sc_context_t *ctx)
834
{
835
  ctx->ossl3ctx = calloc(1, sizeof(ossl3ctx_t));
836
  if (ctx->ossl3ctx == NULL)
837
    return SC_ERROR_OUT_OF_MEMORY;
838
  ctx->ossl3ctx->libctx = OSSL_LIB_CTX_new();
839
  if (ctx->ossl3ctx->libctx == NULL) {
840
    return SC_ERROR_INTERNAL;
841
  }
842
  ctx->ossl3ctx->defprov = OSSL_PROVIDER_load(ctx->ossl3ctx->libctx,
843
                "default");
844
  if (ctx->ossl3ctx->defprov == NULL) {
845
    OSSL_LIB_CTX_free(ctx->ossl3ctx->libctx);
846
    free(ctx->ossl3ctx);
847
    ctx->ossl3ctx = NULL;
848
    return SC_ERROR_INTERNAL;
849
  }
850
  ctx->ossl3ctx->legacyprov = OSSL_PROVIDER_load(ctx->ossl3ctx->libctx,
851
                   "legacy");
852
  if (ctx->ossl3ctx->legacyprov == NULL) {
853
    sc_log(ctx, "Failed to load OpenSSL Legacy provider");
854
  }
855
  return SC_SUCCESS;
856
}
857
858
static void sc_openssl3_deinit(sc_context_t *ctx)
859
{
860
  if (ctx->ossl3ctx == NULL)
861
    return;
862
  if (ctx->ossl3ctx->legacyprov)
863
    OSSL_PROVIDER_unload(ctx->ossl3ctx->legacyprov);
864
  if (ctx->ossl3ctx->defprov)
865
    OSSL_PROVIDER_unload(ctx->ossl3ctx->defprov);
866
  if (ctx->ossl3ctx->libctx)
867
    OSSL_LIB_CTX_free(ctx->ossl3ctx->libctx);
868
  free(ctx->ossl3ctx);
869
  ctx->ossl3ctx = NULL;
870
}
871
#endif
872
873
static char *get_exe_path()
874
273
{
875
  /* Find the executable's path which runs this code.
876
   * See https://github.com/gpakosz/whereami/ for
877
   * potentially more platforms */
878
273
  char exe_path[PATH_MAX] = "unknown executable path";
879
273
  int path_found = 0;
880
881
#if   defined(_WIN32)
882
  if (0 < GetModuleFileNameA(NULL, exe_path, sizeof exe_path))
883
    path_found = 1;
884
#elif defined(__APPLE__)
885
  if (0 < proc_pidpath(getpid(), exe_path, sizeof exe_path))
886
    path_found = 1;
887
#elif defined(__linux__) || defined(__CYGWIN__)
888
273
  if (NULL != realpath("/proc/self/exe", exe_path))
889
273
    path_found = 1;
890
273
#endif
891
892
#if defined(HAVE_GETPROGNAME)
893
  if (!path_found) {
894
    /* getprogname is unreliable and typically only returns the basename.
895
     * However, this should be enough for our purposes */
896
    const char *prog = getprogname();
897
    if (prog)
898
      strlcpy(exe_path, prog, sizeof exe_path);
899
  }
900
#else
901
  /* avoid warning "set but not used" */
902
273
  (void) path_found;
903
273
#endif
904
905
273
  return strdup(exe_path);
906
273
}
907
908
int sc_context_create(sc_context_t **ctx_out, const sc_context_param_t *parm)
909
273
{
910
273
  sc_context_t    *ctx;
911
273
  struct _sc_ctx_options  opts;
912
273
  int     r;
913
273
  char      *driver;
914
915
273
  if (ctx_out == NULL || parm == NULL)
916
0
    return SC_ERROR_INVALID_ARGUMENTS;
917
918
273
  ctx = calloc(1, sizeof(sc_context_t));
919
273
  if (ctx == NULL)
920
0
    return SC_ERROR_OUT_OF_MEMORY;
921
273
  memset(&opts, 0, sizeof(opts));
922
923
  /* set the application name if set in the parameter options */
924
273
  if (parm->app_name != NULL)
925
273
    ctx->app_name = strdup(parm->app_name);
926
0
  else
927
0
    ctx->app_name = strdup("default");
928
273
  if (ctx->app_name == NULL) {
929
0
    sc_release_context(ctx);
930
0
    return SC_ERROR_OUT_OF_MEMORY;
931
0
  }
932
933
273
  ctx->exe_path = get_exe_path();
934
273
  if (ctx->exe_path == NULL) {
935
0
    sc_release_context(ctx);
936
0
    return SC_ERROR_OUT_OF_MEMORY;
937
0
  }
938
939
273
  ctx->flags = parm->flags;
940
273
  set_defaults(ctx, &opts);
941
942
273
  if (0 != list_init(&ctx->readers)) {
943
0
    del_drvs(&opts);
944
0
    sc_release_context(ctx);
945
0
    return SC_ERROR_OUT_OF_MEMORY;
946
0
  }
947
273
  list_attributes_seeker(&ctx->readers, reader_list_seeker);
948
  /* set thread context and create mutex object (if specified) */
949
273
  if (parm->thread_ctx != NULL)
950
0
    ctx->thread_ctx = parm->thread_ctx;
951
273
  r = sc_mutex_create(ctx, &ctx->mutex);
952
273
  if (r != SC_SUCCESS) {
953
0
    del_drvs(&opts);
954
0
    sc_release_context(ctx);
955
0
    return r;
956
0
  }
957
958
#if defined(ENABLE_OPENSSL) && defined(OPENSSL_SECURE_MALLOC_SIZE) && !defined(LIBRESSL_VERSION_NUMBER)
959
  if (!CRYPTO_secure_malloc_initialized()) {
960
    CRYPTO_secure_malloc_init(OPENSSL_SECURE_MALLOC_SIZE, OPENSSL_SECURE_MALLOC_SIZE/8);
961
  }
962
#endif
963
964
273
  process_config_file(ctx, &opts);
965
966
  /* overwrite with caller's parameters if explicitly given */
967
273
  if (parm->debug) {
968
268
    ctx->debug = parm->debug;
969
268
  }
970
273
  if (parm->debug_file) {
971
268
    if (ctx->debug_file && (ctx->debug_file != stderr && ctx->debug_file != stdout))
972
0
      fclose(ctx->debug_file);
973
268
    ctx->debug_file = parm->debug_file;
974
268
  }
975
976
273
  sc_log(ctx, "==================================="); /* first thing in the log */
977
273
  sc_log(ctx, "OpenSC version: %s", sc_get_version());
978
273
  sc_log(ctx, "Configured for %s (%s)", ctx->app_name, ctx->exe_path);
979
980
#ifdef USE_OPENSSL3_LIBCTX
981
  r = sc_openssl3_init(ctx);
982
  if (r != SC_SUCCESS) {
983
    del_drvs(&opts);
984
    sc_release_context(ctx);
985
    return r;
986
  }
987
#endif
988
989
#ifdef ENABLE_PCSC
990
  ctx->reader_driver = sc_get_pcsc_driver();
991
#elif defined(ENABLE_CRYPTOTOKENKIT)
992
  ctx->reader_driver = sc_get_cryptotokenkit_driver();
993
#elif defined(ENABLE_CTAPI)
994
  ctx->reader_driver = sc_get_ctapi_driver();
995
#elif defined(ENABLE_OPENCT)
996
  ctx->reader_driver = sc_get_openct_driver();
997
#endif
998
999
273
  r = ctx->reader_driver->ops->init(ctx);
1000
273
  if (r != SC_SUCCESS)   {
1001
0
    del_drvs(&opts);
1002
0
    sc_release_context(ctx);
1003
0
    return r;
1004
0
  }
1005
1006
273
  driver = getenv("OPENSC_DRIVER");
1007
273
  if (driver) {
1008
0
    scconf_list *list = NULL;
1009
0
    scconf_list_add(&list, driver);
1010
0
    set_drivers(&opts, list);
1011
0
    scconf_list_destroy(list);
1012
0
  }
1013
1014
273
  load_card_drivers(ctx, &opts);
1015
273
  load_card_atrs(ctx);
1016
1017
273
  del_drvs(&opts);
1018
273
  sc_ctx_detect_readers(ctx);
1019
273
  *ctx_out = ctx;
1020
1021
273
  return SC_SUCCESS;
1022
273
}
1023
1024
/* Used by minidriver to pass in provided handles to reader-pcsc */
1025
int sc_ctx_use_reader(sc_context_t *ctx, void *pcsc_context_handle, void *pcsc_card_handle)
1026
0
{
1027
0
  LOG_FUNC_CALLED(ctx);
1028
0
  if (ctx->reader_driver->ops->use_reader != NULL)
1029
0
    return ctx->reader_driver->ops->use_reader(ctx, pcsc_context_handle, pcsc_card_handle);
1030
1031
0
  return SC_ERROR_NOT_SUPPORTED;
1032
0
}
1033
1034
/* Following two are only implemented with internal PC/SC and don't consume a reader object */
1035
int sc_cancel(sc_context_t *ctx)
1036
0
{
1037
0
  LOG_FUNC_CALLED(ctx);
1038
0
  if (ctx->reader_driver->ops->cancel != NULL)
1039
0
    return ctx->reader_driver->ops->cancel(ctx);
1040
1041
0
  return SC_ERROR_NOT_SUPPORTED;
1042
0
}
1043
1044
1045
int sc_wait_for_event(sc_context_t *ctx, unsigned int event_mask, sc_reader_t **event_reader, unsigned int *event, int timeout, void **reader_states)
1046
0
{
1047
0
  LOG_FUNC_CALLED(ctx);
1048
0
  if (ctx->reader_driver->ops->wait_for_event != NULL)
1049
0
    return ctx->reader_driver->ops->wait_for_event(ctx, event_mask, event_reader, event, timeout, reader_states);
1050
1051
0
  return SC_ERROR_NOT_SUPPORTED;
1052
0
}
1053
1054
int sc_release_context(sc_context_t *ctx)
1055
273
{
1056
273
  unsigned int i;
1057
1058
273
  if (ctx == NULL) {
1059
0
    return SC_ERROR_INVALID_ARGUMENTS;
1060
0
  }
1061
273
  SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE);
1062
287
  while (list_size(&ctx->readers)) {
1063
14
    sc_reader_t *rdr = (sc_reader_t *) list_get_at(&ctx->readers, 0);
1064
14
    _sc_delete_reader(ctx, rdr);
1065
14
  }
1066
1067
273
  if (ctx->reader_driver->ops->finish != NULL)
1068
259
    ctx->reader_driver->ops->finish(ctx);
1069
1070
11.7k
  for (i = 0; ctx->card_drivers[i]; i++) {
1071
11.4k
    struct sc_card_driver *drv = ctx->card_drivers[i];
1072
1073
11.4k
    if (drv->atr_map)
1074
0
      _sc_free_atr(ctx, drv);
1075
11.4k
    if (drv->dll)
1076
0
      sc_dlclose(drv->dll);
1077
11.4k
  }
1078
#ifdef USE_OPENSSL3_LIBCTX
1079
  sc_openssl3_deinit(ctx);
1080
#endif
1081
273
  if (ctx->preferred_language != NULL)
1082
0
    free(ctx->preferred_language);
1083
273
  if (ctx->mutex != NULL) {
1084
0
    int r = sc_mutex_destroy(ctx, ctx->mutex);
1085
0
    if (r != SC_SUCCESS) {
1086
0
      sc_log(ctx, "unable to destroy mutex");
1087
0
      return r;
1088
0
    }
1089
0
  }
1090
273
  if (ctx->conf != NULL)
1091
273
    scconf_free(ctx->conf);
1092
273
  if (ctx->debug_file && (ctx->debug_file != stdout && ctx->debug_file != stderr))
1093
0
    fclose(ctx->debug_file);
1094
273
  free(ctx->debug_filename);
1095
273
  free(ctx->app_name);
1096
273
  free(ctx->exe_path);
1097
273
  list_destroy(&ctx->readers);
1098
273
  sc_mem_clear(ctx, sizeof(*ctx));
1099
273
  free(ctx);
1100
273
  return SC_SUCCESS;
1101
273
}
1102
1103
int sc_set_card_driver(sc_context_t *ctx, const char *short_name)
1104
0
{
1105
0
  int i = 0, match = 0;
1106
1107
0
  sc_mutex_lock(ctx, ctx->mutex);
1108
0
  if (short_name == NULL) {
1109
0
    ctx->forced_driver = NULL;
1110
0
    match = 1;
1111
0
  } else while (i < SC_MAX_CARD_DRIVERS && ctx->card_drivers[i] != NULL) {
1112
0
    struct sc_card_driver *drv = ctx->card_drivers[i];
1113
1114
0
    if (strcmp(short_name, drv->short_name) == 0) {
1115
0
      ctx->forced_driver = drv;
1116
0
      match = 1;
1117
0
      break;
1118
0
    }
1119
0
    i++;
1120
0
  }
1121
0
  sc_mutex_unlock(ctx, ctx->mutex);
1122
0
  if (match == 0)
1123
0
    return SC_ERROR_OBJECT_NOT_FOUND; /* FIXME: invent error */
1124
0
  return SC_SUCCESS;
1125
0
}
1126
1127
int sc_get_cache_dir(sc_context_t *ctx, char *buf, size_t bufsize)
1128
259
{
1129
259
  char *homedir;
1130
259
  const char *cache_dir;
1131
259
        scconf_block *conf_block = NULL;
1132
#ifdef _WIN32
1133
  char temp_path[PATH_MAX];
1134
#endif
1135
259
  conf_block = sc_get_conf_block(ctx, "framework", "pkcs15", 1);
1136
259
  cache_dir = scconf_get_str(conf_block, "file_cache_dir", NULL);
1137
259
  if (cache_dir != NULL) {
1138
0
    strlcpy(buf, cache_dir, bufsize);
1139
0
    return SC_SUCCESS;
1140
0
  }
1141
1142
259
#ifndef _WIN32
1143
#ifdef __APPLE__
1144
  cache_dir = getenv("Caches");
1145
#else
1146
259
  cache_dir = getenv("XDG_CACHE_HOME");
1147
259
#endif
1148
259
  if (cache_dir != NULL && cache_dir[0] != '\0') {
1149
0
    snprintf(buf, bufsize, "%s/%s", cache_dir, "opensc");
1150
0
    return SC_SUCCESS;
1151
0
  }
1152
259
  cache_dir = ".cache/opensc";
1153
259
  homedir = getenv("HOME");
1154
#else
1155
  cache_dir = "eid-cache";
1156
  homedir = getenv("USERPROFILE");
1157
  /* If USERPROFILE isn't defined, assume it's a single-user OS
1158
   * and put the cache dir in the Windows dir (usually C:\\WINDOWS) */
1159
  if (homedir == NULL || homedir[0] == '\0') {
1160
    GetWindowsDirectoryA(temp_path, sizeof(temp_path));
1161
    homedir = temp_path;
1162
  }
1163
#endif
1164
259
  if (homedir == NULL || homedir[0] == '\0')
1165
0
    return SC_ERROR_INTERNAL;
1166
259
  if (snprintf(buf, bufsize, "%s/%s", homedir, cache_dir) < 0)
1167
0
    return SC_ERROR_BUFFER_TOO_SMALL;
1168
259
  return SC_SUCCESS;
1169
259
}
1170
1171
int sc_make_cache_dir(sc_context_t *ctx)
1172
0
{
1173
0
  char dirname[PATH_MAX], *sp;
1174
0
  int    r, mkdir_checker;
1175
0
  size_t j, namelen;
1176
1177
0
  if ((r = sc_get_cache_dir(ctx, dirname, sizeof(dirname))) < 0)
1178
0
    return r;
1179
0
  namelen = strlen(dirname);
1180
1181
0
  while (1) {
1182
#ifdef _WIN32
1183
    mkdir_checker = mkdir(dirname) >= 0;
1184
#else
1185
0
    mkdir_checker = mkdir(dirname, 0700) >= 0;
1186
0
#endif
1187
0
    if (mkdir_checker)
1188
0
      break;
1189
1190
0
    if (errno != ENOENT || (sp = strrchr(dirname, '/')) == NULL
1191
0
        || sp == dirname)
1192
0
      goto failed;
1193
0
    *sp = '\0';
1194
0
  }
1195
1196
  /* We may have stripped one or more path components from
1197
   * the directory name. Restore them */
1198
0
  while (1) {
1199
0
    j = strlen(dirname);
1200
0
    if (j >= namelen)
1201
0
      break;
1202
0
    dirname[j] = '/';
1203
#ifdef _WIN32
1204
    mkdir_checker = mkdir(dirname) < 0;
1205
#else
1206
0
    mkdir_checker = mkdir(dirname, 0700) < 0;
1207
0
#endif
1208
0
    if (mkdir_checker)
1209
0
      goto failed;
1210
0
  }
1211
0
  return SC_SUCCESS;
1212
1213
  /* for lack of a better return code */
1214
0
failed:
1215
0
  sc_log(ctx, "failed to create cache directory");
1216
0
  return SC_ERROR_INTERNAL;
1217
0
}