Coverage Report

Created: 2026-11-12 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/pkcs15init/profile.c
Line
Count
Source
1
/*
2
 * Initialize Cards according to PKCS#15
3
 *
4
 * Copyright (C) 2002 Olaf Kirch <okir@suse.de>
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
 * Random notes
21
 *  - the "key" command should go away, it's obsolete
22
 */
23
24
#include "config.h"
25
26
#include <stdio.h>
27
#include <ctype.h>
28
#include <stdarg.h>
29
#include <string.h>
30
#include <limits.h>
31
#ifdef HAVE_STRINGS_H
32
#include <strings.h>
33
#endif
34
#ifdef HAVE_UNISTD_H
35
#include <unistd.h>
36
#endif
37
#include <stdlib.h>
38
39
#ifdef _WIN32
40
#include <windows.h>
41
#include <winreg.h>
42
#endif
43
44
#include "common/compat_strlcpy.h"
45
#include "common/libscdl.h"
46
#include "libopensc/log.h"
47
#include "libopensc/pkcs15.h"
48
#include "pkcs15-init.h"
49
#include "profile.h"
50
#include "scconf/scconf.h"
51
52
13.1k
#define DEF_PRKEY_RSA_ACCESS  0x1D
53
#define DEF_PUBKEY_ACCESS 0x12
54
55
14.8k
#define TEMPLATE_FILEID_MIN_DIFF  0x20
56
57
8.23k
#define WORD_SIZE 64
58
59
/*
60
#define DEBUG_PROFILE
61
*/
62
63
/*
64
 * Parser state
65
 */
66
struct state {
67
  struct state *    frame;
68
  const char *    filename;
69
  struct sc_profile * profile;
70
  struct file_info *  file;
71
  struct pin_info * pin;
72
  struct auth_info *  key;
73
};
74
75
76
struct command {
77
  const char *    name;
78
  int     min_args, max_args;
79
  int     (*func)(struct state *, int, char **);
80
};
81
82
struct block {
83
  const char *    name;
84
  int     (*handler)(struct state *,
85
          struct block *,
86
          const char *,
87
          scconf_block *);
88
  struct command *  cmd_info;
89
  struct block *    blk_info;
90
};
91
92
struct map {
93
  const char *    name;
94
  unsigned int    val;
95
};
96
97
static struct map   aclNames[] = {
98
  { "NONE", SC_AC_NONE  },
99
  { "NEVER",  SC_AC_NEVER },
100
  { "CHV",  SC_AC_CHV },
101
  { "TERM", SC_AC_TERM  },
102
  { "PRO",  SC_AC_PRO },
103
  { "AUT",  SC_AC_AUT },
104
  { "KEY",  SC_AC_AUT },
105
  { "SEN",  SC_AC_SEN },
106
  { "IDA",  SC_AC_IDA },
107
  { "SCB",  SC_AC_SCB },
108
  { NULL, 0 }
109
};
110
static struct map   fileOpNames[] = {
111
  { "SELECT", SC_AC_OP_SELECT },
112
  { "LOCK", SC_AC_OP_LOCK },
113
  { "DELETE", SC_AC_OP_DELETE },
114
  { "DELETE-SELF",SC_AC_OP_DELETE_SELF },
115
  { "CREATE", SC_AC_OP_CREATE },
116
  { "CREATE-EF",  SC_AC_OP_CREATE_EF  },
117
  { "CREATE-DF",  SC_AC_OP_CREATE_DF  },
118
  { "REHABILITATE",SC_AC_OP_REHABILITATE  },
119
  { "INVALIDATE", SC_AC_OP_INVALIDATE },
120
  { "FILES",  SC_AC_OP_LIST_FILES },
121
  { "READ", SC_AC_OP_READ },
122
  { "UPDATE", SC_AC_OP_UPDATE },
123
  { "WRITE",  SC_AC_OP_WRITE  },
124
  { "ERASE",  SC_AC_OP_ERASE  },
125
  { "CRYPTO",     SC_AC_OP_CRYPTO },
126
        { "PIN-DEFINE", SC_AC_OP_PIN_DEFINE },
127
        { "PIN-CHANGE", SC_AC_OP_PIN_CHANGE },
128
        { "PIN-RESET",  SC_AC_OP_PIN_RESET },
129
        { "PIN-USE",  SC_AC_OP_PIN_USE },
130
  { "GENERATE", SC_AC_OP_GENERATE },
131
  { "PSO-COMPUTE-SIGNATURE",  SC_AC_OP_PSO_COMPUTE_SIGNATURE },
132
  { "INTERNAL-AUTHENTICATE",  SC_AC_OP_INTERNAL_AUTHENTICATE },
133
  { "PSO-DECRYPT",    SC_AC_OP_PSO_DECRYPT },
134
  { "RESIZE", SC_AC_OP_RESIZE },
135
  { "ADMIN",  SC_AC_OP_ADMIN  },
136
  { "ACTIVATE", SC_AC_OP_ACTIVATE },
137
  { "DEACTIVATE", SC_AC_OP_DEACTIVATE },
138
  { NULL, 0 }
139
};
140
static struct map   fileTypeNames[] = {
141
  { "EF",   SC_FILE_TYPE_WORKING_EF   },
142
  { "INTERNAL-EF",SC_FILE_TYPE_INTERNAL_EF  },
143
  { "DF",   SC_FILE_TYPE_DF     },
144
  { "BSO",  SC_FILE_TYPE_BSO    },
145
  { NULL, 0 }
146
};
147
static struct map   fileStructureNames[] = {
148
  { "TRANSPARENT",  SC_FILE_EF_TRANSPARENT  },
149
  { "LINEAR-FIXED", SC_FILE_EF_LINEAR_FIXED },
150
  { "LINEAR-FIXED-TLV", SC_FILE_EF_LINEAR_FIXED_TLV },
151
  { "LINEAR-VARIABLE",  SC_FILE_EF_LINEAR_VARIABLE  },
152
  { "LINEAR-VARIABLE-TLV",SC_FILE_EF_LINEAR_VARIABLE_TLV  },
153
  { "CYCLIC",   SC_FILE_EF_CYCLIC },
154
  { "CYCLIC-TLV",   SC_FILE_EF_CYCLIC_TLV },
155
  { NULL, 0 }
156
};
157
static struct map   pkcs15DfNames[] = {
158
  { "PRKDF",    SC_PKCS15_PRKDF   },
159
  { "PUKDF",    SC_PKCS15_PUKDF   },
160
  { "PUKDF-TRUSTED",  SC_PKCS15_PUKDF_TRUSTED },
161
  { "SKDF",   SC_PKCS15_SKDF    },
162
  { "CDF",    SC_PKCS15_CDF   },
163
  { "CDF-TRUSTED",  SC_PKCS15_CDF_TRUSTED },
164
  { "CDF-USEFUL",   SC_PKCS15_CDF_USEFUL  },
165
  { "DODF",   SC_PKCS15_DODF    },
166
  { "AODF",   SC_PKCS15_AODF    },
167
  { NULL, 0 }
168
};
169
static struct map   pinTypeNames[] = {
170
  { "BCD",    SC_PKCS15_PIN_TYPE_BCD  },
171
  { "ascii-numeric",  SC_PKCS15_PIN_TYPE_ASCII_NUMERIC  },
172
  { "utf8",   SC_PKCS15_PIN_TYPE_UTF8 },
173
  { "half-nibble-bcd",  SC_PKCS15_PIN_TYPE_HALFNIBBLE_BCD },
174
  { "iso9564-1",    SC_PKCS15_PIN_TYPE_ISO9564_1  },
175
  { NULL, 0 }
176
};
177
static struct map   pinIdNames[] = {
178
  { "pin",    SC_PKCS15INIT_USER_PIN  },
179
  { "puk",    SC_PKCS15INIT_USER_PUK  },
180
  { "user-pin",   SC_PKCS15INIT_USER_PIN  },
181
  { "user-puk",   SC_PKCS15INIT_USER_PUK  },
182
  { "sopin",    SC_PKCS15INIT_SO_PIN  },
183
  { "sopuk",    SC_PKCS15INIT_SO_PUK  },
184
  { "so-pin",   SC_PKCS15INIT_SO_PIN  },
185
  { "so-puk",   SC_PKCS15INIT_SO_PUK  },
186
  { NULL, 0 }
187
};
188
static struct map   pinFlagNames[] = {
189
  { "case-sensitive",   SC_PKCS15_PIN_FLAG_CASE_SENSITIVE   },
190
  { "local",      SC_PKCS15_PIN_FLAG_LOCAL      },
191
  { "change-disabled",    SC_PKCS15_PIN_FLAG_CHANGE_DISABLED    },
192
  { "unblock-disabled",   SC_PKCS15_PIN_FLAG_UNBLOCK_DISABLED   },
193
  { "initialized",    SC_PKCS15_PIN_FLAG_INITIALIZED      },
194
  { "needs-padding",    SC_PKCS15_PIN_FLAG_NEEDS_PADDING    },
195
  { "unblockingPin",    SC_PKCS15_PIN_FLAG_UNBLOCKING_PIN   },
196
  { "soPin",      SC_PKCS15_PIN_FLAG_SO_PIN     },
197
  { "disable-allowed",    SC_PKCS15_PIN_FLAG_DISABLE_ALLOW    },
198
  { "integrity-protected",  SC_PKCS15_PIN_FLAG_INTEGRITY_PROTECTED    },
199
  { "confidentiality-protected",  SC_PKCS15_PIN_FLAG_CONFIDENTIALITY_PROTECTED  },
200
  { "exchangeRefData",    SC_PKCS15_PIN_FLAG_EXCHANGE_REF_DATA    },
201
  { NULL, 0 }
202
};
203
static struct map   idStyleNames[] = {
204
  { "native",   SC_PKCS15INIT_ID_STYLE_NATIVE },
205
  { "mozilla",    SC_PKCS15INIT_ID_STYLE_MOZILLA },
206
  { "rfc2459",    SC_PKCS15INIT_ID_STYLE_RFC2459 },
207
  { NULL, 0 }
208
};
209
static struct map              mdStyleNames[] = {
210
  { "none",               SC_PKCS15INIT_MD_STYLE_NONE },
211
  { "gemalto",            SC_PKCS15INIT_MD_STYLE_GEMALTO },
212
  { NULL, 0 }
213
};
214
static struct {
215
  const char *    name;
216
  struct map *    addr;
217
} mapNames[] = {
218
  { "file ACL",   aclNames  },
219
  { "file operation", fileOpNames },
220
  { "file type",    fileTypeNames },
221
  { "file structure", fileStructureNames},
222
  { "PKCS#15 file name",  pkcs15DfNames },
223
  { "pin encoding", pinTypeNames  },
224
  { "pin name",   pinIdNames  },
225
  { "pin flag",   pinFlagNames  },
226
  { NULL, NULL }
227
};
228
229
static int    process_conf(struct sc_profile *, scconf_context *);
230
static int    process_block(struct state *, struct block *,
231
        const char *, scconf_block *);
232
static void   init_state(struct state *, struct state *);
233
static int    get_authid(struct state *, const char *,
234
        unsigned int *, unsigned int *);
235
static int    get_uint(struct state *, const char *, unsigned int *);
236
static int    get_bool(struct state *, const char *, unsigned int *);
237
static int    get_uint_eval(struct state *, int, char **,
238
        unsigned int *);
239
static int    map_str2int(struct state *, const char *,
240
        unsigned int *, struct map *);
241
static int    setstr(char **strp, const char *value);
242
static void   parse_error(struct state *, const char *, ...);
243
244
static struct file_info * sc_profile_instantiate_file(sc_profile_t *,
245
        struct file_info *, struct file_info *,
246
        unsigned int);
247
static struct file_info * sc_profile_find_file(struct sc_profile *,
248
        const sc_path_t *, const char *);
249
static struct file_info * sc_profile_find_file_by_path(
250
        struct sc_profile *,
251
        const sc_path_t *);
252
253
static struct pin_info *  new_pin(struct sc_profile *, int);
254
static struct file_info * new_file(struct state *, const char *,
255
        unsigned int);
256
static struct file_info * add_file(sc_profile_t *, const char *,
257
        sc_file_t *, struct file_info *);
258
static void   free_file_list(struct file_info **);
259
static void   append_file(sc_profile_t *, struct file_info *);
260
static struct auth_info * new_key(struct sc_profile *,
261
        unsigned int, unsigned int);
262
static void   set_pin_defaults(struct sc_profile *,
263
        struct pin_info *);
264
static int    new_macro(sc_profile_t *, const char *, scconf_list *);
265
static sc_macro_t * find_macro(sc_profile_t *, const char *);
266
static int    is_macro_character(char c);
267
268
static sc_file_t *
269
init_file(unsigned int type)
270
65.4k
{
271
65.4k
  struct sc_file  *file;
272
65.4k
  unsigned int  op;
273
274
65.4k
  file = sc_file_new();
275
2.09M
  for (op = 0; op < SC_MAX_AC_OPS; op++) {
276
2.02M
    sc_file_add_acl_entry(file, op, SC_AC_NONE, 0);
277
2.02M
  }
278
65.4k
  file->type = type;
279
65.4k
  file->status = SC_FILE_STATUS_ACTIVATED;
280
65.4k
  if (file->type != SC_FILE_TYPE_DF && file->type != SC_FILE_TYPE_BSO)
281
46.8k
    file->ef_structure = SC_FILE_EF_TRANSPARENT;
282
65.4k
  return file;
283
65.4k
}
284
285
/*
286
 * Initialize profile
287
 */
288
struct sc_profile *
289
sc_profile_new(void)
290
13.1k
{
291
13.1k
  struct sc_pkcs15_card *p15card;
292
13.1k
  struct sc_profile *pro;
293
294
13.1k
  pro = calloc(1, sizeof(*pro));
295
13.1k
  if (pro == NULL)
296
0
    return NULL;
297
13.1k
  pro->p15_spec = p15card = sc_pkcs15_card_new();
298
299
13.1k
  pro->pkcs15.do_last_update = 1;
300
301
13.1k
  if (p15card) {
302
13.1k
    p15card->tokeninfo->label = strdup("OpenSC Card");
303
13.1k
    p15card->tokeninfo->manufacturer_id = strdup("OpenSC Project");
304
13.1k
    p15card->tokeninfo->serial_number = strdup("0000");
305
13.1k
    p15card->tokeninfo->flags = SC_PKCS15_TOKEN_EID_COMPLIANT;
306
13.1k
    p15card->tokeninfo->version = 0;
307
308
    /* Set up EF(TokenInfo) and EF(ODF) */
309
13.1k
    p15card->file_tokeninfo = init_file(SC_FILE_TYPE_WORKING_EF);
310
13.1k
    p15card->file_odf = init_file(SC_FILE_TYPE_WORKING_EF);
311
13.1k
    p15card->file_unusedspace = init_file(SC_FILE_TYPE_WORKING_EF);
312
13.1k
  }
313
314
  /* Assume card does RSA natively */
315
13.1k
  pro->rsa_access_flags = DEF_PRKEY_RSA_ACCESS;
316
13.1k
  pro->pin_encoding = SC_PKCS15_PIN_TYPE_ASCII_NUMERIC;
317
13.1k
  pro->pin_minlen = 4;
318
13.1k
  pro->pin_maxlen = 8;
319
13.1k
  pro->id_style = SC_PKCS15INIT_ID_STYLE_NATIVE;
320
321
13.1k
  return pro;
322
13.1k
}
323
324
int
325
sc_profile_load(struct sc_profile *profile, const char *filename)
326
0
{
327
0
  struct sc_context *ctx = profile->card->ctx;
328
0
  scconf_context  *conf;
329
0
  const char *profile_dir = NULL;
330
0
  char path[PATH_MAX];
331
0
  int res = 0, i;
332
#ifdef _WIN32
333
  char temp_path[PATH_MAX];
334
  size_t temp_len;
335
#endif
336
337
0
  LOG_FUNC_CALLED(ctx);
338
0
  for (i = 0; ctx->conf_blocks[i]; i++) {
339
0
    profile_dir = scconf_get_str(ctx->conf_blocks[i], "profile_dir", NULL);
340
0
    if (profile_dir)
341
0
      break;
342
0
  }
343
344
0
  if (!profile_dir) {
345
#ifdef _WIN32
346
    temp_len = PATH_MAX - 1;
347
    res = sc_ctx_win32_get_config_value(NULL, "ProfileDir", "Software\\" OPENSC_VS_FF_COMPANY_NAME "\\OpenSC" OPENSC_ARCH_SUFFIX,
348
        temp_path, &temp_len);
349
    if (res)
350
      LOG_FUNC_RETURN(ctx, res);
351
    temp_path[temp_len] = '\0';
352
    profile_dir = temp_path;
353
#else
354
0
    profile_dir = SC_PKCS15_PROFILE_DIRECTORY;
355
0
#endif
356
0
  }
357
0
  sc_log(ctx, "Using profile directory '%s'.", profile_dir);
358
359
#ifdef _WIN32
360
  res = snprintf(path, sizeof(path), "%s\\%s.%s", profile_dir, filename, SC_PKCS15_PROFILE_SUFFIX);
361
#else /* _WIN32 */
362
0
  res = snprintf(path, sizeof(path), "%s/%s.%s", profile_dir, filename, SC_PKCS15_PROFILE_SUFFIX);
363
0
#endif /* _WIN32 */
364
0
  if (res < 0 || (size_t)res >= sizeof(path))
365
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_BUFFER_TOO_SMALL);
366
367
0
  sc_log(ctx, "Trying profile file %s", path);
368
369
0
  conf = scconf_new(path);
370
0
  res = scconf_parse(conf);
371
372
0
  sc_log(ctx, "profile %s loaded ok", path);
373
374
0
  if (res < 0) {
375
0
    scconf_free(conf);
376
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_FILE_NOT_FOUND);
377
0
  }
378
379
0
  if (res == 0) {
380
0
    scconf_free(conf);
381
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_SYNTAX_ERROR);
382
0
  }
383
384
0
  res = process_conf(profile, conf);
385
0
  scconf_free(conf);
386
0
  LOG_FUNC_RETURN(ctx, res);
387
0
}
388
389
390
int
391
sc_profile_finish(struct sc_profile *profile, const struct sc_app_info *app_info)
392
9.99k
{
393
9.99k
  struct sc_context *ctx = profile->card->ctx;
394
9.99k
  struct file_info *fi;
395
9.99k
  struct pin_info *pi;
396
9.99k
  char    reason[64];
397
398
9.99k
  LOG_FUNC_CALLED(ctx);
399
9.99k
  profile->mf_info = sc_profile_find_file(profile, NULL, "MF");
400
9.99k
  if (!profile->mf_info)
401
9.99k
    LOG_TEST_RET(ctx, SC_ERROR_INCONSISTENT_PROFILE, "Profile doesn't define a MF");
402
403
6.70k
  if (app_info && app_info->aid.len)   {
404
0
    struct sc_path path;
405
406
0
    sc_log(ctx, "finish profile with '%s' application profile", app_info->label);
407
0
    memset(&path, 0, sizeof(struct sc_path));
408
0
    path.type = SC_PATH_TYPE_DF_NAME;
409
0
    path.aid = app_info->aid;
410
411
0
    sc_log(ctx, "Look for file by path '%s'", sc_print_path(&path));
412
0
    profile->df_info = sc_profile_find_file_by_path(profile, &path);
413
0
    sc_log(ctx, "returned DF info %p", profile->df_info);
414
0
    if (profile->df_info && profile->df_info->profile_extension)   {
415
0
      sc_log(ctx, "application profile extension '%s'", profile->df_info->profile_extension);
416
0
      if (sc_profile_load(profile, profile->df_info->profile_extension))
417
0
        LOG_TEST_RET(ctx, SC_ERROR_INCONSISTENT_PROFILE, "Cannot load application profile extension");
418
0
    }
419
0
  }
420
421
6.70k
  profile->df_info = sc_profile_find_file(profile, NULL, "PKCS15-AppDF");
422
6.70k
  if (!profile->df_info)
423
6.70k
    LOG_TEST_RET(ctx, SC_ERROR_INCONSISTENT_PROFILE, "Profile doesn't define a PKCS15-AppDF");
424
425
6.60k
  profile->p15_spec->file_app = profile->df_info->file;
426
6.60k
  profile->df_info->dont_free = 1;
427
428
10.2k
  for (pi = profile->pin_list; pi; pi = pi->next) {
429
3.68k
    const char  *name;
430
431
3.68k
    set_pin_defaults(profile, pi);
432
3.68k
    if (!(name = pi->file_name))
433
3.68k
      continue;
434
1
    if (!(fi = sc_profile_find_file(profile, NULL, name))) {
435
1
      snprintf(reason, sizeof(reason), "unknown PIN file \"%s\"\n", name);
436
1
      goto whine;
437
1
    }
438
439
0
    pi->file = fi;
440
0
  }
441
6.60k
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
442
443
1
whine:
444
1
  sc_log(ctx, "%s", reason);
445
1
  LOG_FUNC_RETURN(ctx, SC_ERROR_INCONSISTENT_PROFILE);
446
1
}
447
448
void
449
sc_profile_free(struct sc_profile *profile)
450
15.3k
{
451
15.3k
  struct auth_info *ai;
452
15.3k
  struct pin_info *pi;
453
15.3k
  sc_macro_t  *mi;
454
15.3k
  sc_template_t *ti;
455
456
15.3k
  if (profile->name)
457
12.0k
    free(profile->name);
458
15.3k
  if (profile->driver)
459
8
    free(profile->driver);
460
461
15.3k
  free_file_list(&profile->ef_list);
462
463
15.3k
  while ((ai = profile->auth_list) != NULL) {
464
3
    profile->auth_list = ai->next;
465
3
    free(ai);
466
3
  }
467
468
17.4k
  while ((ti = profile->template_list) != NULL) {
469
2.18k
    profile->template_list = ti->next;
470
2.18k
    if (ti->data)
471
2.18k
      sc_profile_free(ti->data);
472
2.18k
    if (ti->name)
473
2.18k
      free(ti->name);
474
2.18k
    free(ti);
475
2.18k
  }
476
477
18.3k
  while ((mi = profile->macro_list) != NULL) {
478
3.09k
    profile->macro_list = mi->next;
479
3.09k
    if (mi->name)
480
3.09k
      free(mi->name);
481
3.09k
    free(mi);
482
3.09k
  }
483
484
33.4k
  while ((pi = profile->pin_list) != NULL) {
485
18.0k
    profile->pin_list = pi->next;
486
18.0k
    if (pi->file_name)
487
24
      free(pi->file_name);
488
18.0k
    free(pi);
489
18.0k
  }
490
491
15.5k
  for (int i = 0; profile->options[i]; i++) {
492
249
    free(profile->options[i]);
493
249
  }
494
495
15.3k
  if (profile->p15_spec)
496
13.1k
    sc_pkcs15_card_free(profile->p15_spec);
497
498
15.3k
  if (profile->dll)
499
0
    sc_dlclose(profile->dll);
500
501
15.3k
  free(profile);
502
15.3k
}
503
504
void
505
sc_profile_get_pin_info(struct sc_profile *profile,
506
    int id, struct sc_pkcs15_auth_info *info)
507
27.3k
{
508
27.3k
  struct pin_info *pi;
509
510
27.3k
  pi = new_pin(profile, id);
511
27.3k
  if (pi == NULL)
512
0
    return;
513
514
27.3k
  pi->pin.max_tries = pi->pin.tries_left;
515
27.3k
  *info = pi->pin;
516
27.3k
}
517
518
int
519
sc_profile_get_pin_retries(sc_profile_t *profile, int id)
520
68
{
521
68
  struct pin_info *pi;
522
523
68
  pi = new_pin(profile, id);
524
68
  if (pi == NULL)
525
0
    return SC_ERROR_OUT_OF_MEMORY;
526
68
  return pi->pin.tries_left;
527
68
}
528
529
int
530
sc_profile_get_pin_id(struct sc_profile *profile,
531
    unsigned int reference, int *id)
532
48
{
533
48
  struct pin_info *pi;
534
535
151
  for (pi = profile->pin_list; pi; pi = pi->next) {
536
137
    if (pi->pin.auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
537
0
      continue;
538
137
    if (pi->pin.attrs.pin.reference == (int)reference) {
539
34
      *id = pi->id;
540
34
      return 0;
541
34
    }
542
543
137
  }
544
14
  return SC_ERROR_OBJECT_NOT_FOUND;
545
48
}
546
547
int
548
sc_profile_get_file_in(sc_profile_t *profile,
549
    const sc_path_t *path, const char *name, sc_file_t **ret)
550
19
{
551
19
  struct file_info *fi;
552
553
19
  if ((fi = sc_profile_find_file(profile, path, name)) == NULL)
554
14
    return SC_ERROR_FILE_NOT_FOUND;
555
5
  sc_file_dup(ret, fi->file);
556
5
  if (*ret == NULL)
557
0
    return SC_ERROR_OUT_OF_MEMORY;
558
5
  return 0;
559
5
}
560
561
int
562
sc_profile_get_file(struct sc_profile *profile,
563
    const char *name, sc_file_t **ret)
564
7.91k
{
565
7.91k
  struct file_info *fi;
566
567
7.91k
  if ((fi = sc_profile_find_file(profile, NULL, name)) == NULL)
568
4.29k
    return SC_ERROR_FILE_NOT_FOUND;
569
3.61k
  sc_file_dup(ret, fi->file);
570
3.61k
  if (*ret == NULL)
571
0
    return SC_ERROR_OUT_OF_MEMORY;
572
3.61k
  return 0;
573
3.61k
}
574
575
int
576
sc_profile_get_file_instance(struct sc_profile *profile, const char *name,
577
    int index, sc_file_t **ret)
578
23.0k
{
579
23.0k
  struct sc_context *ctx = profile->card->ctx;
580
23.0k
  struct file_info *fi;
581
23.0k
  struct sc_file *file;
582
23.0k
  int r;
583
584
23.0k
  LOG_FUNC_CALLED(ctx);
585
23.0k
  sc_log(ctx, "try to get '%s' file instance", name);
586
587
23.0k
  if ((fi = sc_profile_find_file(profile, NULL, name)) == NULL)
588
23.0k
    LOG_FUNC_RETURN(ctx, SC_ERROR_FILE_NOT_FOUND);
589
4.32k
  sc_file_dup(&file, fi->file);
590
4.32k
  sc_log(ctx, "ident '%s'; parent '%s'", fi->ident, fi->parent ? fi->parent->ident : "(null)");
591
4.32k
  if (file == NULL)
592
4.32k
    LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
593
4.32k
  sc_log(ctx, "file (type:%X, path:'%s')", file->type, sc_print_path(&file->path));
594
595
4.32k
  file->id += index;
596
4.32k
        if(file->type == SC_FILE_TYPE_BSO) {
597
7
    r = sc_profile_add_file(profile, name, file);
598
7
    if (r < 0)
599
2
      sc_file_free(file);
600
7
    LOG_TEST_RET(ctx, r, "Profile error: cannot add BSO file");
601
7
  }
602
4.31k
  else if (file->path.len)   {
603
600
    file->path.value[file->path.len - 2] = (file->id >> 8) & 0xFF;
604
600
    file->path.value[file->path.len - 1] = file->id & 0xFF;
605
606
600
    r = sc_profile_add_file(profile, name, file);
607
600
    if (r < 0)
608
58
      sc_file_free(file);
609
600
    LOG_TEST_RET(ctx, r, "Profile error: cannot add file");
610
600
  }
611
612
4.26k
  if (ret)
613
4.26k
    *ret = file;
614
0
  else
615
0
    sc_file_free(file);
616
617
4.26k
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
618
4.26k
}
619
620
int
621
sc_profile_get_path(struct sc_profile *profile,
622
    const char *name, sc_path_t *ret)
623
0
{
624
0
  struct file_info *fi;
625
626
0
  if ((fi = sc_profile_find_file(profile, NULL, name)) == NULL)
627
0
    return SC_ERROR_FILE_NOT_FOUND;
628
0
  *ret = fi->file->path;
629
0
  return 0;
630
0
}
631
632
int
633
sc_profile_get_file_by_path(struct sc_profile *profile,
634
    const sc_path_t *path, sc_file_t **ret)
635
5.41k
{
636
5.41k
  struct sc_context *ctx = profile->card->ctx;
637
5.41k
  struct file_info *fi;
638
639
5.41k
  LOG_FUNC_CALLED(ctx);
640
5.41k
  if ((fi = sc_profile_find_file_by_path(profile, path)) == NULL)
641
5.41k
    LOG_FUNC_RETURN(ctx, SC_ERROR_FILE_NOT_FOUND);
642
3.31k
  sc_file_dup(ret, fi->file);
643
3.31k
  LOG_FUNC_RETURN(ctx, *ret ? SC_SUCCESS : SC_ERROR_OUT_OF_MEMORY);
644
3.31k
}
645
646
int
647
sc_profile_add_file(sc_profile_t *profile, const char *name, sc_file_t *file)
648
616
{
649
616
  struct sc_context *ctx = profile->card->ctx;
650
616
  sc_path_t path = file->path;
651
616
  struct file_info  *parent;
652
653
616
  LOG_FUNC_CALLED(ctx);
654
616
  if (!path.len)   {
655
5
    parent = profile->df_info;
656
611
  } else {
657
611
    path.len -= 2;
658
611
    parent = sc_profile_find_file_by_path(profile, &path);
659
611
  }
660
616
  if (!parent)
661
616
    LOG_FUNC_RETURN(ctx, SC_ERROR_FILE_NOT_FOUND);
662
547
  sc_log(ctx, "Parent path:%s", sc_print_path(&parent->file->path));
663
664
547
  sc_file_dup(&file, file);
665
547
  if (file == NULL)
666
547
    LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
667
668
547
  add_file(profile, name, file, parent);
669
547
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
670
547
}
671
672
/*
673
 * Instantiate template
674
 */
675
int
676
sc_profile_instantiate_template(sc_profile_t *profile,
677
    const char *template_name, const sc_path_t *base_path,
678
    const char *file_name, const sc_pkcs15_id_t *id,
679
    sc_file_t **ret)
680
25.1k
{
681
25.1k
  struct sc_context *ctx = profile->card->ctx;
682
25.1k
  struct sc_profile *tmpl;
683
25.1k
  struct sc_template  *info;
684
25.1k
  unsigned int  idx;
685
25.1k
  struct file_info *fi, *base_file, *match = NULL;
686
687
#ifdef DEBUG_PROFILE
688
  printf("Instantiate %s in template %s\n", file_name, template_name);
689
  sc_profile_find_file_by_path(profile, base_path);
690
#endif
691
25.8k
  for (info = profile->template_list; info; info = info->next)
692
2.56k
    if (!strcmp(info->name, template_name))
693
1.89k
      break;
694
25.1k
  if (info == NULL)   {
695
23.2k
    sc_log(ctx, "Template %s not found", template_name);
696
23.2k
    return SC_ERROR_TEMPLATE_NOT_FOUND;
697
23.2k
  }
698
699
1.89k
  tmpl = info->data;
700
1.89k
  idx = id->value[id->len-1];
701
141k
  for (fi = profile->ef_list; fi; fi = fi->next) {
702
140k
    if (fi->base_template == tmpl
703
134k
     && fi->inst_index == idx
704
3.41k
     && sc_compare_path(&fi->inst_path, base_path)
705
3.40k
     && !strcmp(fi->ident, file_name)) {
706
564
      sc_file_dup(ret, fi->file);
707
564
      if (*ret == NULL)
708
0
        return SC_ERROR_OUT_OF_MEMORY;
709
564
      return 0;
710
564
    }
711
140k
  }
712
713
1.32k
  sc_log(ctx, "Instantiating template %s at %s", template_name, sc_print_path(base_path));
714
715
1.32k
  base_file = sc_profile_find_file_by_path(profile, base_path);
716
1.32k
  if (base_file == NULL) {
717
32
    sc_log(ctx, "Directory %s not defined in profile", sc_print_path(base_path));
718
32
    return SC_ERROR_OBJECT_NOT_FOUND;
719
32
  }
720
721
  /* This loop relies on the fact that new files are always
722
   * appended to the list, after the parent files they refer to
723
   */
724
1.29k
  if (base_file->instance == NULL)
725
0
    return SC_ERROR_INTERNAL;
726
3.19k
  for (fi = tmpl->ef_list; fi; fi = fi->next) {
727
1.90k
    struct file_info  *parent, *instance;
728
1.90k
    unsigned int  skew = 0;
729
730
1.90k
    fi->instance = NULL;
731
1.90k
    if ((parent = fi->parent) == NULL) {
732
1.25k
      parent = base_file;
733
1.25k
      skew = idx;
734
1.25k
    }
735
1.90k
    parent = parent->instance;
736
737
1.90k
    instance = sc_profile_instantiate_file(profile, fi, parent, skew);
738
1.90k
    if (instance == NULL)
739
0
      return SC_ERROR_OUT_OF_MEMORY;
740
1.90k
    instance->base_template = tmpl;
741
1.90k
    instance->inst_index = idx;
742
1.90k
    instance->inst_path = *base_path;
743
744
1.90k
    if (!strcmp(instance->ident, file_name))
745
513
      match = instance;
746
1.90k
  }
747
748
1.29k
  if (match == NULL) {
749
782
    sc_log(ctx, "No file named \"%s\" in template \"%s\"",
750
782
        file_name, template_name);
751
782
    return SC_ERROR_OBJECT_NOT_FOUND;
752
782
  }
753
513
  sc_file_dup(ret, match->file);
754
513
  if (*ret == NULL)
755
0
    return SC_ERROR_OUT_OF_MEMORY;
756
#ifdef DEBUG_PROFILE
757
  printf("Template instantiated\n");
758
#endif
759
513
  return 0;
760
513
}
761
762
static struct file_info *
763
sc_profile_instantiate_file(sc_profile_t *profile, struct file_info *ft,
764
    struct file_info *parent, unsigned int skew)
765
1.90k
{
766
1.90k
  struct sc_context *ctx = profile->card->ctx;
767
1.90k
  struct file_info *fi;
768
769
1.90k
  fi = calloc(1, sizeof(*fi));
770
1.90k
  if (fi == NULL)
771
0
    return NULL;
772
1.90k
  fi->instance = fi;
773
1.90k
  fi->parent = parent;
774
1.90k
  fi->ident = strdup(ft->ident);
775
1.90k
  if (fi->ident == NULL) {
776
0
    free(fi);
777
0
    return NULL;
778
0
  }
779
1.90k
  sc_file_dup(&fi->file, ft->file);
780
1.90k
  if (fi->file == NULL) {
781
0
    free(fi->ident);
782
0
    free(fi);
783
0
    return NULL;
784
0
  }
785
1.90k
  fi->file->path = parent->file->path;
786
1.90k
  fi->file->id += skew;
787
788
1.90k
  if (fi->file->type == SC_FILE_TYPE_INTERNAL_EF
789
1.82k
      || fi->file->type == SC_FILE_TYPE_WORKING_EF
790
1.45k
      || (fi->file->type == SC_FILE_TYPE_DF && fi->file->id))
791
667
    sc_append_file_id(&fi->file->path, fi->file->id);
792
793
1.90k
  append_file(profile, fi);
794
795
1.90k
  ft->instance = fi;
796
797
1.90k
  sc_log(ctx, "Instantiated %s at %s", ft->ident, sc_print_path(&fi->file->path));
798
1.90k
  sc_log(ctx, "  parent=%s@%s", parent->ident, sc_print_path(&parent->file->path));
799
800
1.90k
  return fi;
801
1.90k
}
802
803
int
804
sc_profile_get_pin_id_by_reference(struct sc_profile *profile,
805
    unsigned auth_method, int reference,
806
    struct sc_pkcs15_auth_info *auth_info)
807
2.37k
{
808
2.37k
  struct pin_info *pinfo;
809
810
4.90k
  for (pinfo = profile->pin_list; pinfo; pinfo = pinfo->next)  {
811
4.22k
    if (auth_method == SC_AC_SYMBOLIC)   {
812
2.69k
      if (pinfo->id != reference)
813
1.01k
        continue;
814
2.69k
    }
815
1.53k
    else   {
816
1.53k
      if (pinfo->pin.auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
817
0
        continue;
818
1.53k
      if (pinfo->pin.auth_method != auth_method)
819
537
        continue;
820
995
      if (pinfo->pin.attrs.pin.reference != reference)
821
972
        continue;
822
995
    }
823
824
1.70k
    if (auth_info)
825
1.70k
      *auth_info = pinfo->pin;
826
1.70k
    return pinfo->id;
827
4.22k
  }
828
829
676
  return -1;
830
2.37k
}
831
832
/*
833
 * Configuration file parser
834
 */
835
static void
836
init_state(struct state *cur_state, struct state *new_state)
837
40.3k
{
838
40.3k
  memset(new_state, 0, sizeof(*new_state));
839
40.3k
  new_state->filename = cur_state->filename;
840
40.3k
  new_state->profile = cur_state->profile;
841
40.3k
  new_state->frame = cur_state;
842
40.3k
}
843
844
static int
845
do_card_driver(struct state *cur, int argc, char **argv)
846
22
{
847
22
  free(cur->profile->driver);
848
22
  cur->profile->driver = strdup(argv[0]);
849
22
  return 0;
850
22
}
851
852
static int
853
do_maxpinlength(struct state *cur, int argc, char **argv)
854
177
{
855
177
  return get_uint(cur, argv[0], &cur->profile->pin_maxlen);
856
177
}
857
858
static int
859
do_minpinlength(struct state *cur, int argc, char **argv)
860
689
{
861
689
  return get_uint(cur, argv[0], &cur->profile->pin_minlen);
862
689
}
863
864
static int
865
do_default_pin_type(struct state *cur, int argc, char **argv)
866
1.95k
{
867
1.95k
  return map_str2int(cur, argv[0],
868
1.95k
            &cur->profile->pin_encoding, pinTypeNames);
869
1.95k
}
870
871
static int
872
do_pin_pad_char(struct state *cur, int argc, char **argv)
873
1.69k
{
874
1.69k
  return get_uint(cur, argv[0], &cur->profile->pin_pad_char);
875
1.69k
}
876
877
static int
878
do_pin_domains(struct state *cur, int argc, char **argv)
879
258
{
880
258
  return get_bool(cur, argv[0], &cur->profile->pin_domains);
881
258
}
882
883
static int
884
do_card_label(struct state *cur, int argc, char **argv)
885
1.45k
{
886
1.45k
  struct sc_pkcs15_card *p15card = cur->profile->p15_spec;
887
888
1.45k
  return setstr(&p15card->tokeninfo->label, argv[0]);
889
1.45k
}
890
891
static int
892
do_card_manufacturer(struct state *cur, int argc, char **argv)
893
434
{
894
434
  struct sc_pkcs15_card *p15card = cur->profile->p15_spec;
895
896
434
  return setstr(&p15card->tokeninfo->manufacturer_id, argv[0]);
897
434
}
898
899
/*
900
 * Command related to the pkcs15 we generate
901
 */
902
static int
903
do_direct_certificates(struct state *cur, int argc, char **argv)
904
957
{
905
957
  return get_bool(cur, argv[0], &cur->profile->pkcs15.direct_certificates);
906
957
}
907
908
static int
909
do_encode_df_length(struct state *cur, int argc, char **argv)
910
929
{
911
929
  return get_bool(cur, argv[0], &cur->profile->pkcs15.encode_df_length);
912
929
}
913
914
static int
915
do_encode_update_field(struct state *cur, int argc, char **argv)
916
396
{
917
396
  return get_bool(cur, argv[0], &cur->profile->pkcs15.do_last_update);
918
396
}
919
920
static int
921
do_pkcs15_id_style(struct state *cur, int argc, char **argv)
922
291
{
923
291
  return map_str2int(cur, argv[0], &cur->profile->id_style, idStyleNames);
924
291
}
925
926
static int
927
do_minidriver_support_style(struct state *cur, int argc, char **argv)
928
1
{
929
1
  return map_str2int(cur, argv[0], &cur->profile->md_style, mdStyleNames);
930
1
}
931
932
/*
933
 * Process an option block
934
 */
935
static int
936
process_option(struct state *cur, struct block *info,
937
    const char *name, scconf_block *blk)
938
1.04k
{
939
1.04k
  sc_profile_t  *profile = cur->profile;
940
1.04k
  int   match = 0, i;
941
942
1.41k
  for (i = 0; profile->options[i]; i++)
943
371
    match |= !strcmp(profile->options[i], name);
944
1.04k
  if (!match && strcmp("default", name))
945
644
    return 0;
946
404
  return process_block(cur, info, name, blk);
947
1.04k
}
948
949
/*
950
 * Process a key block
951
 */
952
static int
953
process_key(struct state *cur, struct block *info,
954
    const char *name, scconf_block *blk)
955
10
{
956
10
  unsigned int  type, id;
957
10
  struct state  state;
958
959
10
  if (get_authid(cur, name, &type, &id))
960
7
    return 1;
961
962
3
  init_state(cur, &state);
963
3
  state.key = new_key(cur->profile, type, id);
964
3
  return process_block(&state, info, name, blk);
965
10
}
966
967
static struct auth_info *
968
new_key(struct sc_profile *profile, unsigned int type, unsigned int ref)
969
3
{
970
3
  struct auth_info *ai, **aip;
971
972
3
  for (aip = &profile->auth_list; (ai = *aip); aip = &ai->next) {
973
0
    if (ai->type == type && ai->ref == ref)
974
0
      return ai;
975
0
  }
976
977
3
  ai = calloc(1, sizeof(*ai));
978
3
  if (ai == NULL)
979
0
    return NULL;
980
3
  ai->type = type;
981
3
  ai->ref = ref;
982
3
  *aip = ai;
983
3
  return ai;
984
3
}
985
986
static int
987
do_key_value(struct state *cur, int argc, char **argv)
988
0
{
989
0
  struct auth_info *ai = cur->key;
990
0
  const char  *key = argv[0];
991
0
  size_t    key_len;
992
0
  unsigned char keybuf[32];
993
994
0
  if (key[0] == '=') {
995
0
    ++key;
996
0
    key_len = strlen(key);
997
0
    if (key_len > sizeof(keybuf)) {
998
0
        parse_error(cur, "Key value too long (%zu > %zu)\n", key_len, sizeof(keybuf));
999
0
        return 1;
1000
0
    }
1001
0
    memcpy(keybuf, key, key_len);
1002
0
  } else {
1003
0
    key_len = sizeof(keybuf);
1004
0
    if (sc_hex_to_bin(key, keybuf, &key_len)) {
1005
0
      parse_error(cur, "Error parsing PIN/key \"%s\"\n", key);
1006
0
      return 1;
1007
0
    }
1008
0
  }
1009
1010
0
  memcpy(ai->key, keybuf, key_len);
1011
0
  ai->key_len = key_len;
1012
0
  return 0;
1013
0
}
1014
1015
/*
1016
 * This function is called when the parser finds a block with an unknown
1017
 * name in the filesystem block. This will create a new filesystem
1018
 * object as the child of the current object.
1019
 */
1020
static int
1021
process_df(struct state *cur, struct block *info,
1022
    const char *name, scconf_block *blk)
1023
19.4k
{
1024
19.4k
  struct state  state;
1025
1026
19.4k
  init_state(cur, &state);
1027
19.4k
  if (name == NULL) {
1028
0
    parse_error(cur, "No name given for DF object.");
1029
0
    return 1;
1030
0
  }
1031
19.4k
  if (!(state.file = new_file(cur, name, SC_FILE_TYPE_DF)))
1032
227
    return 1;
1033
19.1k
  return process_block(&state, info, name, blk);
1034
19.4k
}
1035
1036
static int
1037
process_ef(struct state *cur, struct block *info,
1038
    const char *name, scconf_block *blk)
1039
9.50k
{
1040
9.50k
  struct state  state;
1041
1042
9.50k
  init_state(cur, &state);
1043
9.50k
  if (name == NULL) {
1044
0
    parse_error(cur, "No name given for EF object.");
1045
0
    return 1;
1046
0
  }
1047
9.50k
  if (!(state.file = new_file(cur, name, SC_FILE_TYPE_WORKING_EF)))
1048
13
    return 1;
1049
9.49k
  return process_block(&state, info, name, blk);
1050
9.50k
}
1051
1052
1053
static int
1054
process_bso(struct state *cur, struct block *info,
1055
    const char *name, scconf_block *blk)
1056
1.51k
{
1057
1.51k
  struct state  state;
1058
1059
1.51k
  init_state(cur, &state);
1060
1.51k
  if (name == NULL) {
1061
0
    parse_error(cur, "No name given for BSO object.");
1062
0
    return 1;
1063
0
  }
1064
1.51k
  if (!(state.file = new_file(cur, name, SC_FILE_TYPE_BSO)))
1065
1
    return 1;
1066
1.51k
  return process_block(&state, info, name, blk);
1067
1.51k
}
1068
1069
/*
1070
 * In the template the difference between any two file-ids
1071
 * should be greater then TEMPLATE_FILEID_MIN_DIFF.
1072
 */
1073
static int
1074
template_sanity_check(struct state *cur, struct sc_profile *templ)
1075
2.06k
{
1076
2.06k
  struct file_info *fi, *ffi;
1077
1078
8.02k
  for (fi = templ->ef_list; fi; fi = fi->next) {
1079
6.00k
    struct sc_path fi_path =  fi->file->path;
1080
6.00k
    int fi_id;
1081
1082
6.00k
    if (fi->file->type == SC_FILE_TYPE_BSO)
1083
1.02k
      continue;
1084
1085
4.97k
    if (fi_path.len < 2) {
1086
23
      parse_error(cur, "Template insane: file-path length should not be less than 2 bytes");
1087
23
      return 1;
1088
23
    }
1089
1090
4.95k
    fi_id = fi_path.value[fi_path.len - 2] * 0x100
1091
4.95k
        + fi_path.value[fi_path.len - 1];
1092
1093
25.2k
    for (ffi = templ->ef_list; ffi; ffi = ffi->next) {
1094
20.2k
      struct sc_path ffi_path =  ffi->file->path;
1095
20.2k
      int dlt, ffi_id;
1096
1097
20.2k
      if (ffi->file->type == SC_FILE_TYPE_BSO)
1098
421
        continue;
1099
1100
19.8k
      if (ffi_path.len < 2) {
1101
11
        parse_error(cur, "Template insane: file-path length should not be less than 2 bytes");
1102
11
        return 1;
1103
11
      }
1104
1105
19.8k
      ffi_id = ffi_path.value[ffi_path.len - 2] * 0x100
1106
19.8k
          + ffi_path.value[ffi_path.len - 1];
1107
1108
19.8k
      dlt = fi_id > ffi_id ? fi_id - ffi_id : ffi_id - fi_id;
1109
19.8k
      if (strcmp(ffi->ident, fi->ident))   {
1110
14.8k
        if (dlt >= TEMPLATE_FILEID_MIN_DIFF)
1111
14.8k
          continue;
1112
1113
10
        parse_error(cur, "Template insane: file-ids should be substantially different");
1114
10
        return 1;
1115
14.8k
      }
1116
19.8k
    }
1117
4.95k
  }
1118
1119
2.02k
  return SC_SUCCESS;
1120
2.06k
}
1121
1122
1123
static int
1124
process_tmpl(struct state *cur, struct block *info,
1125
    const char *name, scconf_block *blk)
1126
2.18k
{
1127
2.18k
  struct state  state;
1128
2.18k
  sc_template_t *tinfo;
1129
2.18k
  sc_profile_t  *templ;
1130
2.18k
  int r;
1131
1132
#ifdef DEBUG_PROFILE
1133
  printf("Process template:%s; block:%s\n", name, info->name);
1134
#endif
1135
2.18k
  if (name == NULL) {
1136
0
    parse_error(cur, "No name given for template.");
1137
0
    return 1;
1138
0
  }
1139
1140
2.18k
  templ = calloc(1, sizeof(*templ));
1141
2.18k
  if (templ == NULL) {
1142
0
    parse_error(cur, "memory allocation failed");
1143
0
    return 1;
1144
0
  }
1145
1146
2.18k
  tinfo = calloc(1, sizeof(*tinfo));
1147
2.18k
  if (tinfo == NULL) {
1148
0
    parse_error(cur, "memory allocation failed");
1149
0
    free(templ);
1150
0
    return 1;
1151
0
  }
1152
2.18k
  tinfo->name = strdup(name);
1153
2.18k
  tinfo->data = templ;
1154
1155
2.18k
  tinfo->next = cur->profile->template_list;
1156
2.18k
  cur->profile->template_list = tinfo;
1157
1158
2.18k
  init_state(cur, &state);
1159
2.18k
  state.profile = tinfo->data;
1160
2.18k
  state.file = NULL;
1161
1162
2.18k
  r = process_block(&state, info, name, blk);
1163
2.18k
  if (!r)
1164
2.06k
    r = template_sanity_check(cur, templ);
1165
1166
#ifdef DEBUG_PROFILE
1167
  printf("Template %s processed; returns %i\n", name, r);
1168
#endif
1169
2.18k
  return r;
1170
2.18k
}
1171
1172
/*
1173
 * Append new file at the end of the ef_list.
1174
 * This is crucial; the profile instantiation code relies on it
1175
 */
1176
static void append_file(sc_profile_t *profile, struct file_info *nfile)
1177
28.9k
{
1178
28.9k
  struct file_info  **list, *fi;
1179
1180
28.9k
  list = &profile->ef_list;
1181
172k
  while ((fi = *list) != NULL)
1182
143k
    list = &fi->next;
1183
28.9k
  *list = nfile;
1184
28.9k
}
1185
1186
/*
1187
 * Add a new file to the profile.
1188
 * This function is called by sc_profile_add_file.
1189
 */
1190
static struct file_info *
1191
add_file(sc_profile_t *profile, const char *name,
1192
    sc_file_t *file, struct file_info *parent)
1193
27.0k
{
1194
27.0k
  struct file_info  *info;
1195
1196
27.0k
  info = calloc(1, sizeof(*info));
1197
27.0k
  if (info == NULL)
1198
0
    return NULL;
1199
27.0k
  info->instance = info;
1200
27.0k
  info->ident = strdup(name);
1201
1202
27.0k
  info->parent = parent;
1203
27.0k
  info->file = file;
1204
1205
27.0k
  append_file(profile, info);
1206
27.0k
  return info;
1207
27.0k
}
1208
1209
/*
1210
 * Free file_info list
1211
 */
1212
static void
1213
free_file_list(struct file_info **list)
1214
15.3k
{
1215
15.3k
  struct file_info  *fi;
1216
1217
44.2k
  while ((fi = *list) != NULL) {
1218
28.9k
    *list = fi->next;
1219
1220
28.9k
    if (fi->dont_free == 0)
1221
21.9k
      sc_file_free(fi->file);
1222
28.9k
    free(fi->profile_extension);
1223
28.9k
    free(fi->ident);
1224
28.9k
    free(fi);
1225
28.9k
  }
1226
15.3k
}
1227
1228
/*
1229
 * Create a new file info object.
1230
 * This function is called by the profile parser.
1231
 */
1232
static struct file_info *
1233
new_file(struct state *cur, const char *name, unsigned int type)
1234
30.4k
{
1235
30.4k
  sc_profile_t  *profile = cur->profile;
1236
30.4k
  struct file_info  *info;
1237
30.4k
  sc_file_t *file;
1238
30.4k
  unsigned int  df_type = 0, dont_free = 0;
1239
30.4k
  int free_file = 0;
1240
1241
30.4k
  if ((info = sc_profile_find_file(profile, NULL, name)) != NULL)
1242
3.67k
    return info;
1243
1244
  /* Special cases for those EFs handled separately
1245
   * by the PKCS15 logic */
1246
26.7k
  if (strncasecmp(name, "PKCS15-", 7)) {
1247
16.5k
    file = init_file(type);
1248
16.5k
    free_file = 1;
1249
16.5k
  } else if (!strcasecmp(name+7, "TokenInfo")) {
1250
109
    if (!profile->p15_spec) {
1251
1
      parse_error(cur, "no pkcs15 spec in profile");
1252
1
      return NULL;
1253
1
    }
1254
108
    file = profile->p15_spec->file_tokeninfo;
1255
108
    dont_free = 1;
1256
10.0k
  } else if (!strcasecmp(name+7, "ODF")) {
1257
261
    if (!profile->p15_spec) {
1258
1
      parse_error(cur, "no pkcs15 spec in profile");
1259
1
      return NULL;
1260
1
    }
1261
260
    file = profile->p15_spec->file_odf;
1262
260
    dont_free = 1;
1263
9.82k
  } else if (!strcasecmp(name+7, "UnusedSpace")) {
1264
84
    if (!profile->p15_spec) {
1265
1
      parse_error(cur, "no pkcs15 spec in profile");
1266
1
      return NULL;
1267
1
    }
1268
83
    file = profile->p15_spec->file_unusedspace;
1269
83
    dont_free = 1;
1270
9.74k
  } else if (!strcasecmp(name+7, "AppDF")) {
1271
7.95k
    file = init_file(SC_FILE_TYPE_DF);
1272
7.95k
    free_file = 1;
1273
7.95k
  } else {
1274
1.78k
    if (map_str2int(cur, name+7, &df_type, pkcs15DfNames)
1275
1.58k
        || df_type >= SC_PKCS15_DF_TYPE_COUNT)
1276
229
      return NULL;
1277
1278
1.55k
    file = init_file(SC_FILE_TYPE_WORKING_EF);
1279
1.55k
    profile->df[df_type] = file;
1280
1.55k
    free_file = 1;
1281
1.55k
  }
1282
26.5k
  if (!file)
1283
0
    return NULL;
1284
26.5k
  if (file->type != type) {
1285
9
    parse_error(cur, "inconsistent file type (should be %s)",
1286
9
      file->type == SC_FILE_TYPE_DF
1287
9
        ? "DF" : file->type == SC_FILE_TYPE_BSO
1288
8
          ? "BS0" : "EF");
1289
9
    if (free_file)
1290
7
      sc_file_free(file);
1291
9
    return NULL;
1292
9
  }
1293
1294
26.5k
  info = add_file(profile, name, file, cur->file);
1295
26.5k
  if (info == NULL) {
1296
0
    parse_error(cur, "memory allocation failed");
1297
0
    return NULL;
1298
0
  }
1299
26.5k
  info->dont_free = dont_free;
1300
26.5k
  return info;
1301
26.5k
}
1302
1303
static int
1304
do_file_type(struct state *cur, int argc, char **argv)
1305
3.78k
{
1306
3.78k
  unsigned int  type;
1307
1308
3.78k
  if (!cur->file) {
1309
1
    parse_error(cur, "Invalid state\n");
1310
1
    return 1;
1311
1
  }
1312
1313
3.78k
  if (map_str2int(cur, argv[0], &type, fileTypeNames))
1314
4
    return 1;
1315
3.77k
  cur->file->file->type = type;
1316
3.77k
  return 0;
1317
3.78k
}
1318
1319
static int
1320
do_file_path(struct state *cur, int argc, char **argv)
1321
3.79k
{
1322
3.79k
  struct sc_file  *file = NULL;
1323
3.79k
  struct sc_path  *path = NULL;
1324
1325
3.79k
  if (!cur->file) {
1326
1
    parse_error(cur, "Invalid state\n");
1327
1
    return 1;
1328
1
  }
1329
3.79k
  file = cur->file->file;
1330
3.79k
  path = &file->path;
1331
1332
  /* sc_format_path doesn't return an error indication
1333
   * when it's unable to parse the path */
1334
3.79k
  sc_format_path(argv[0], path);
1335
3.79k
  if (!path->len || (path->len & 1)) {
1336
42
    parse_error(cur, "Invalid path length\n");
1337
42
    return 1;
1338
42
  }
1339
3.75k
  file->id = (path->value[path->len-2] << 8) | path->value[path->len-1];
1340
3.75k
  return 0;
1341
3.79k
}
1342
1343
static int
1344
do_fileid(struct state *cur, int argc, char **argv)
1345
10.0k
{
1346
10.0k
  struct file_info *fi;
1347
10.0k
  struct sc_file  *df, *file = NULL;
1348
10.0k
  struct sc_path  temp, *path = NULL;
1349
1350
10.0k
  if (!cur->file) {
1351
1
    parse_error(cur, "Invalid state\n");
1352
1
    return 1;
1353
1
  }
1354
10.0k
  file = cur->file->file;
1355
10.0k
  path = &file->path;
1356
1357
  /* sc_format_path doesn't return an error indication
1358
   * when it's unable to parse the path */
1359
10.0k
  sc_format_path(argv[0], &temp);
1360
10.0k
  if (temp.len != 2) {
1361
21
    parse_error(cur, "Invalid file ID length\n");
1362
21
    return 1;
1363
21
  }
1364
1365
  /* Get the DF, if any */
1366
10.0k
  if ((fi = cur->file->parent) && (df = fi->file)) {
1367
7.21k
    if (!df->path.len && !df->path.aid.len) {
1368
1
      parse_error(cur, "No path/fileid set for parent DF\n");
1369
1
      return 1;
1370
1
    }
1371
7.21k
    if (df->path.len + 2 > sizeof(df->path.value)) {
1372
1
      parse_error(cur, "File path too long\n");
1373
1
      return 1;
1374
1
    }
1375
7.21k
    *path = df->path;
1376
7.21k
  }
1377
10.0k
  if (path->len + 2 > sizeof(path->value)) {
1378
4
    parse_error(cur, "File path too long\n");
1379
4
    return 1;
1380
4
  }
1381
10.0k
  memcpy(path->value + path->len, temp.value, 2);
1382
10.0k
  path->len += 2;
1383
1384
10.0k
  file->id = (temp.value[0] << 8) | temp.value[1];
1385
10.0k
  return 0;
1386
10.0k
}
1387
1388
static int
1389
do_structure(struct state *cur, int argc, char **argv)
1390
2.07k
{
1391
2.07k
  unsigned int  ef_structure;
1392
1393
2.07k
  if (!cur->file) {
1394
1
    parse_error(cur, "Invalid state\n");
1395
1
    return 1;
1396
1
  }
1397
1398
2.07k
  if (map_str2int(cur, argv[0], &ef_structure, fileStructureNames))
1399
4
    return 1;
1400
2.06k
  cur->file->file->ef_structure = ef_structure;
1401
2.06k
  return 0;
1402
2.07k
}
1403
1404
static int
1405
do_size(struct state *cur, int argc, char **argv)
1406
2.96k
{
1407
2.96k
  unsigned int  size;
1408
1409
2.96k
  if (!cur->file) {
1410
1
    parse_error(cur, "Invalid state\n");
1411
1
    return 1;
1412
1
  }
1413
1414
2.96k
  if (get_uint_eval(cur, argc, argv, &size))
1415
219
    return 1;
1416
2.74k
  cur->file->file->size = size;
1417
2.74k
  return 0;
1418
2.96k
}
1419
1420
static int
1421
do_reclength(struct state *cur, int argc, char **argv)
1422
12
{
1423
12
  unsigned int  reclength;
1424
1425
12
  if (!cur->file) {
1426
1
    parse_error(cur, "Invalid state\n");
1427
1
    return 1;
1428
1
  }
1429
1430
11
  if (get_uint(cur, argv[0], &reclength))
1431
1
    return 1;
1432
10
  cur->file->file->record_length = reclength;
1433
10
  return 0;
1434
11
}
1435
1436
static int
1437
do_content(struct state *cur, int argc, char **argv)
1438
173
{
1439
173
  struct sc_file *file = NULL;
1440
173
  size_t len = (strlen(argv[0]) + 1) / 2;
1441
173
  int rv = 0;
1442
1443
173
  if (!cur->file) {
1444
1
    parse_error(cur, "Invalid state\n");
1445
1
    return 1;
1446
1
  }
1447
172
  file = cur->file->file;
1448
1449
172
  free(file->encoded_content);
1450
1451
172
  file->encoded_content = malloc(len);
1452
172
  if (!file->encoded_content)
1453
0
    return 1;
1454
172
  rv = sc_hex_to_bin(argv[0], file->encoded_content, &len);
1455
172
  file->encoded_content_len = len;
1456
172
  return rv;
1457
172
}
1458
1459
static int
1460
do_prop_attr(struct state *cur, int argc, char **argv)
1461
36
{
1462
36
  struct sc_file *file = NULL;
1463
36
  size_t len = (strlen(argv[0]) + 1) / 2;
1464
36
  int rv = 0;
1465
1466
36
  if (!cur->file) {
1467
1
    parse_error(cur, "Invalid state\n");
1468
1
    return 1;
1469
1
  }
1470
35
  file = cur->file->file;
1471
1472
35
  free(file->prop_attr);
1473
35
  file->prop_attr = malloc(len);
1474
35
  if (!file->prop_attr)
1475
0
    return 1;
1476
35
  rv = sc_hex_to_bin(argv[0], file->prop_attr, &len);
1477
35
  file->prop_attr_len = len;
1478
35
  return rv;
1479
35
}
1480
1481
static int
1482
do_aid(struct state *cur, int argc, char **argv)
1483
379
{
1484
379
  struct sc_file  *file = NULL;
1485
379
  const char  *name = argv[0];
1486
379
  size_t len;
1487
379
  int   res = 0;
1488
1489
379
  if (!cur->file) {
1490
1
    parse_error(cur, "Invalid state\n");
1491
1
    return 1;
1492
1
  }
1493
378
  file = cur->file->file;
1494
1495
378
  if (*name == '=') {
1496
24
    len = strlen(++name);
1497
24
    if (len > sizeof(file->name)) {
1498
1
      parse_error(cur, "AID \"%s\" too long\n", name);
1499
1
      return 1;
1500
1
    }
1501
23
    memcpy(file->name, name, len);
1502
23
    file->namelen = len;
1503
23
  }
1504
354
  else {
1505
354
    file->namelen = sizeof(file->name);
1506
354
    res = sc_hex_to_bin(name, file->name, &file->namelen);
1507
354
  }
1508
377
  return res;
1509
378
}
1510
1511
static int
1512
do_exclusive_aid(struct state *cur, int argc, char **argv)
1513
1.07k
{
1514
1.07k
  struct sc_file  *file = NULL;
1515
1.07k
  const char  *name = argv[0];
1516
1.07k
  size_t len;
1517
1.07k
  int   res = 0;
1518
1519
1.07k
  if (!cur->file) {
1520
1
    parse_error(cur, "Invalid state\n");
1521
1
    return 1;
1522
1
  }
1523
1.07k
  file = cur->file->file;
1524
1525
#ifdef DEBUG_PROFILE
1526
  printf("do_exclusive_aid(): exclusive-aid '%s'\n", name);
1527
  printf("do_exclusive_aid(): current file '%s' (path:%s)\n", cur->file->ident, sc_print_path(&file->path));
1528
#endif
1529
1.07k
  sc_format_path(name, &file->path);
1530
1.07k
  if (file->path.len > SC_MAX_AID_SIZE)   {
1531
0
    parse_error(cur, "Path length is too big\n");
1532
0
    return 1;
1533
0
  }
1534
1535
1.07k
  memcpy(file->path.aid.value, file->path.value, file->path.len);
1536
1.07k
  file->path.aid.len = file->path.len;
1537
1538
1.07k
  file->path.len = 0;
1539
1.07k
  file->path.type = SC_PATH_TYPE_DF_NAME;
1540
1541
#ifdef DEBUG_PROFILE
1542
  printf("do_exclusive_aid(): '%s' exclusive-aid path %s\n", cur->file->ident, sc_print_path(&file->path));
1543
#endif
1544
1.07k
  if (*name == '=') {
1545
21
    len = strlen(++name);
1546
21
    if (len > sizeof(file->name)) {
1547
1
      parse_error(cur, "AID \"%s\" too long\n", name);
1548
1
      return 1;
1549
1
    }
1550
20
    memcpy(file->name, name, len);
1551
20
    file->namelen = len;
1552
20
  }
1553
1.04k
  else {
1554
1.04k
    file->namelen = sizeof(file->name);
1555
1.04k
    res = sc_hex_to_bin(name, file->name, &file->namelen);
1556
1.04k
  }
1557
1.06k
  return res;
1558
1.07k
}
1559
1560
static int
1561
do_profile_extension(struct state *cur, int argc, char **argv)
1562
18
{
1563
18
  if (!cur->file) {
1564
2
    parse_error(cur, "Invalid state\n");
1565
2
    return 1;
1566
2
  }
1567
16
  return setstr(&cur->file->profile_extension, argv[0]);
1568
18
}
1569
1570
/*
1571
 * Parse ACL list.
1572
 * The way we do this is we first split things like CHV1
1573
 * into a method (SC_AC_CHV) and a reference (1).
1574
 * When we're finished parsing the profile, the fake references
1575
 * are replaced by the real references given in KEY or PIN
1576
 * commands
1577
 */
1578
static int
1579
do_acl(struct state *cur, int argc, char **argv)
1580
3.99k
{
1581
3.99k
  struct sc_file  *file = NULL;
1582
3.99k
  char    oper[64], *what = NULL;
1583
3.99k
  memset(oper, 0, sizeof(oper));
1584
1585
3.99k
  if (!cur->file)
1586
1
    goto bad;
1587
3.99k
  file = cur->file->file;
1588
1589
18.2k
  while (argc--) {
1590
14.3k
    unsigned int  op, method, id;
1591
1592
14.3k
    if (strlen(*argv) >= sizeof(oper))
1593
3
      goto bad;
1594
14.3k
    strlcpy(oper, *argv++, sizeof(oper));
1595
1596
14.3k
    if ((what = strchr(oper, '=')) == NULL)
1597
15
      goto bad;
1598
14.3k
    *what++ = '\0';
1599
1600
14.3k
    if (*what == '$') {
1601
6.30k
      method = SC_AC_SYMBOLIC;
1602
6.30k
      if (map_str2int(cur, what+1, &id, pinIdNames))
1603
3
        return 1;
1604
6.30k
    }
1605
8.00k
    else if (get_authid(cur, what, &method, &id))
1606
52
      goto bad;
1607
1608
1609
14.2k
    if (!strcmp(oper, "*")) {
1610
97.1k
      for (op = 0; op < SC_MAX_AC_OPS; op++) {
1611
94.1k
        sc_file_clear_acl_entries(file, op);
1612
94.1k
        sc_file_add_acl_entry(file, op, method, id);
1613
94.1k
      }
1614
11.2k
    } else {
1615
11.2k
      const sc_acl_entry_t *acl;
1616
1617
11.2k
      if (map_str2int(cur, oper, &op, fileOpNames))
1618
7
        goto bad;
1619
11.2k
      if (!(acl = sc_file_get_acl_entry(file, op)))
1620
21
        goto bad;
1621
11.1k
      if (acl->method == SC_AC_NEVER
1622
6.94k
       || acl->method == SC_AC_NONE
1623
2.49k
       || acl->method == SC_AC_UNKNOWN)
1624
8.69k
        sc_file_clear_acl_entries(file, op);
1625
1626
11.1k
      sc_file_add_acl_entry(file, op, method, id);
1627
11.1k
    }
1628
14.2k
  }
1629
3.88k
  return 0;
1630
1631
99
bad:  parse_error(cur,
1632
99
    "Invalid ACL \"%s%s%s\"\n",
1633
99
    oper, what? "=" : "", what? what : "");
1634
99
  return 1;
1635
3.99k
}
1636
1637
static int
1638
process_pin(struct state *cur, struct block *info,
1639
    const char *name, scconf_block *blk)
1640
7.83k
{
1641
7.83k
  struct state  state;
1642
7.83k
  unsigned int  id;
1643
1644
7.83k
  if (map_str2int(cur, name, &id, pinIdNames))
1645
111
    return 1;
1646
1647
7.72k
  init_state(cur, &state);
1648
7.72k
  state.pin = new_pin(cur->profile, (int)id);
1649
1650
7.72k
  return process_block(&state, info, name, blk);
1651
7.83k
}
1652
1653
static struct pin_info *
1654
new_pin(struct sc_profile *profile, int id)
1655
35.1k
{
1656
35.1k
  struct pin_info *pi, **tail;
1657
1658
59.7k
  for (tail = &profile->pin_list; (pi = *tail); tail = &pi->next) {
1659
41.6k
    if (pi->id == id)
1660
17.0k
      return pi;
1661
41.6k
  }
1662
1663
  /* Create pin info object. Most values are
1664
   * set to their defaults in set_pin_defaults later
1665
   * We can't do this here because these pin info objects
1666
   * are usually created before we've read the card specific
1667
   * profile
1668
   */
1669
18.0k
  pi = calloc(1, sizeof(*pi));
1670
18.0k
  if (pi == NULL)
1671
0
    return NULL;
1672
18.0k
  pi->id = id;
1673
18.0k
  pi->pin.auth_type = SC_PKCS15_PIN_AUTH_TYPE_PIN;
1674
18.0k
  pi->pin.auth_method = SC_AC_CHV;
1675
18.0k
  pi->pin.attrs.pin.type = (unsigned int)-1;
1676
18.0k
  pi->pin.attrs.pin.flags = 0x32;
1677
18.0k
  pi->pin.attrs.pin.max_length = 0;
1678
18.0k
  pi->pin.attrs.pin.min_length = 0;
1679
18.0k
  pi->pin.attrs.pin.stored_length = 0;
1680
18.0k
  pi->pin.attrs.pin.pad_char = 0xA5;
1681
18.0k
  pi->pin.attrs.pin.reference = -1;
1682
18.0k
  pi->pin.tries_left = 3;
1683
1684
18.0k
  *tail = pi;
1685
18.0k
  return pi;
1686
18.0k
}
1687
1688
static void set_pin_defaults(struct sc_profile *profile, struct pin_info *pi)
1689
3.68k
{
1690
3.68k
  struct sc_pkcs15_auth_info *info = &pi->pin;
1691
3.68k
  struct sc_pkcs15_pin_attributes *pin_attrs = &info->attrs.pin;
1692
1693
3.68k
  info->auth_type = SC_PKCS15_PIN_AUTH_TYPE_PIN;
1694
1695
3.68k
  if (pin_attrs->type == (unsigned int) -1)
1696
3.66k
    pin_attrs->type = profile->pin_encoding;
1697
3.68k
  if (pin_attrs->max_length == 0)
1698
3.24k
    pin_attrs->max_length = profile->pin_maxlen;
1699
3.68k
  if (pin_attrs->min_length == 0)
1700
3.18k
    pin_attrs->min_length = profile->pin_minlen;
1701
3.68k
  if (pin_attrs->stored_length == 0) {
1702
3.67k
    pin_attrs->stored_length = profile->pin_maxlen;
1703
    /* BCD encoded PIN takes half the space */
1704
3.67k
    if (pin_attrs->type == SC_PKCS15_PIN_TYPE_BCD)
1705
6
      pin_attrs->stored_length = (pin_attrs->stored_length + 1) / 2;
1706
3.67k
  }
1707
3.68k
  if (pin_attrs->pad_char == 0xA5)
1708
3.68k
    pin_attrs->pad_char = profile->pin_pad_char;
1709
3.68k
}
1710
1711
static int
1712
do_pin_file(struct state *cur, int argc, char **argv)
1713
513
{
1714
513
  free(cur->pin->file_name);
1715
513
  cur->pin->file_name = strdup(argv[0]);
1716
513
  return 0;
1717
513
}
1718
1719
static int
1720
do_pin_offset(struct state *cur, int argc, char **argv)
1721
41
{
1722
41
  return get_uint(cur, argv[0], &cur->pin->file_offset);
1723
41
}
1724
1725
static int
1726
do_pin_attempts(struct state *cur, int argc, char **argv)
1727
970
{
1728
970
  struct pin_info *pi = cur->pin;
1729
970
  unsigned int  count;
1730
1731
970
  if (get_uint(cur, argv[0], &count))
1732
1
    return 1;
1733
969
  pi->pin.tries_left = count;
1734
969
  return 0;
1735
970
}
1736
1737
static int
1738
do_pin_maxunlocks(struct state *cur, int argc, char **argv)
1739
67
{
1740
67
  struct pin_info *pi = cur->pin;
1741
67
  unsigned int  count;
1742
1743
67
  if (get_uint(cur, argv[0], &count))
1744
1
    return 1;
1745
66
  pi->pin.max_unlocks = count;
1746
66
  return 0;
1747
67
}
1748
1749
static int
1750
do_pin_type(struct state *cur, int argc, char **argv)
1751
221
{
1752
221
  unsigned int  type;
1753
1754
221
  if (map_str2int(cur, argv[0], &type, pinTypeNames))
1755
3
    return 1;
1756
218
  if (cur->pin->pin.auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
1757
0
    return 1;
1758
218
  cur->pin->pin.attrs.pin.type = type;
1759
218
  return 0;
1760
218
}
1761
1762
static int
1763
do_pin_reference(struct state *cur, int argc, char **argv)
1764
1.94k
{
1765
1.94k
  unsigned int  reference;
1766
1767
1.94k
  if (get_uint(cur, argv[0], &reference))
1768
4
    return 1;
1769
1.94k
  if (cur->pin->pin.auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
1770
0
    return 1;
1771
1.94k
  cur->pin->pin.attrs.pin.reference = reference;
1772
1.94k
  return 0;
1773
1.94k
}
1774
1775
static int
1776
do_pin_authid(struct state *cur, int argc, char **argv)
1777
1.00k
{
1778
1.00k
  sc_pkcs15_format_id(argv[0], &cur->pin->pin.auth_id);
1779
1.00k
  return 0;
1780
1.00k
}
1781
1782
static int
1783
do_pin_minlength(struct state *cur, int argc, char **argv)
1784
1.05k
{
1785
1.05k
  unsigned int  len;
1786
1787
1.05k
  if (get_uint(cur, argv[0], &len))
1788
1
    return 1;
1789
1.05k
  if (cur->pin->pin.auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
1790
0
    return 1;
1791
1.05k
  cur->pin->pin.attrs.pin.min_length = len;
1792
1.05k
  return 0;
1793
1.05k
}
1794
1795
static int
1796
do_pin_maxlength(struct state *cur, int argc, char **argv)
1797
1.00k
{
1798
1.00k
  unsigned int  len;
1799
1800
1.00k
  if (get_uint(cur, argv[0], &len))
1801
2
    return 1;
1802
1.00k
  if (cur->pin->pin.auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
1803
0
    return 1;
1804
1.00k
  cur->pin->pin.attrs.pin.max_length = len;
1805
1.00k
  return 0;
1806
1.00k
}
1807
1808
static int
1809
do_pin_storedlength(struct state *cur, int argc, char **argv)
1810
415
{
1811
415
  unsigned int  len;
1812
1813
415
  if (get_uint(cur, argv[0], &len))
1814
1
    return 1;
1815
414
  if (cur->pin->pin.auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
1816
0
    return 1;
1817
414
  cur->pin->pin.attrs.pin.stored_length = len;
1818
414
  return 0;
1819
414
}
1820
1821
static int
1822
do_pin_flags(struct state *cur, int argc, char **argv)
1823
1.75k
{
1824
1.75k
  unsigned int flags = 0;
1825
1.75k
  int   i, r;
1826
1827
1.75k
  if (cur->pin->pin.auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
1828
0
    return -1;
1829
1830
1.75k
  cur->pin->pin.attrs.pin.flags = 0;
1831
4.35k
  for (i = 0; i < argc; i++) {
1832
2.65k
    if ((r = map_str2int(cur, argv[i], &flags, pinFlagNames)) < 0)
1833
52
      return r;
1834
2.60k
    cur->pin->pin.attrs.pin.flags |= flags;
1835
2.60k
  }
1836
1837
1.70k
  return 0;
1838
1.75k
}
1839
1840
static int
1841
process_macros(struct state *cur, struct block *info,
1842
    const char *dummy, scconf_block *blk)
1843
1.18k
{
1844
1.18k
  scconf_item *item;
1845
1.18k
  const char  *name;
1846
1.18k
  int    r;
1847
1848
6.74k
  for (item = blk->items; item; item = item->next) {
1849
5.61k
    char *s = item->key;
1850
5.61k
    name = item->key;
1851
5.61k
    if (item->type != SCCONF_ITEM_TYPE_VALUE || !name)
1852
783
      continue;
1853
1854
    /* make sure the macro name consist only of allowed characters.
1855
     * This is not guaranteed by the tokenizer */
1856
53.2k
    while (is_macro_character(*s)) {
1857
48.3k
      s++;
1858
48.3k
    }
1859
4.83k
    if (*s != '\0') {
1860
#ifdef DEBUG_PROFILE
1861
      printf("Invalid macro name %s\n", name);
1862
#endif
1863
39
      return SC_ERROR_SYNTAX_ERROR;
1864
39
    }
1865
#ifdef DEBUG_PROFILE
1866
    printf("Defining %s\n", name);
1867
#endif
1868
4.79k
    r = new_macro(cur->profile, name, item->value.list);
1869
4.79k
    if (r != SC_SUCCESS)
1870
21
      return r;
1871
4.79k
  }
1872
1873
1.12k
  return SC_SUCCESS;
1874
1.18k
}
1875
1876
static int
1877
new_macro(sc_profile_t *profile, const char *name, scconf_list *value)
1878
4.79k
{
1879
4.79k
  sc_macro_t  *mac;
1880
1881
4.79k
  if (!profile || !name || !value)
1882
21
    return SC_ERROR_INVALID_ARGUMENTS;
1883
1884
4.77k
  if ((mac = find_macro(profile, name)) == NULL) {
1885
3.09k
    mac = calloc(1, sizeof(*mac));
1886
3.09k
    if (mac == NULL)
1887
0
      return SC_ERROR_OUT_OF_MEMORY;
1888
3.09k
    mac->name = strdup(name);
1889
3.09k
    mac->next = profile->macro_list;
1890
3.09k
    profile->macro_list = mac;
1891
3.09k
  }
1892
1893
4.77k
  mac->value = value;
1894
4.77k
  return SC_SUCCESS;
1895
4.77k
}
1896
1897
static sc_macro_t *
1898
find_macro(sc_profile_t *profile, const char *name)
1899
14.9k
{
1900
14.9k
  sc_macro_t  *mac;
1901
1902
135k
  for (mac = profile->macro_list; mac; mac = mac->next) {
1903
124k
    if (!strcmp(mac->name, name))
1904
4.45k
      return mac;
1905
124k
  }
1906
10.4k
  return NULL;
1907
14.9k
}
1908
1909
/*
1910
 * Key section
1911
 */
1912
static struct command key_commands[] = {
1913
 { "value",   1,  1,  do_key_value  },
1914
 { NULL, 0, 0, NULL }
1915
};
1916
1917
/*
1918
 * Cardinfo section
1919
 */
1920
static struct command ci_commands[] = {
1921
 { "driver",    1,  1,  do_card_driver  },
1922
 { "max-pin-length",  1,  1,  do_maxpinlength },
1923
 { "min-pin-length",  1,  1,  do_minpinlength },
1924
 { "pin-encoding",  1,  1,  do_default_pin_type },
1925
 { "pin-pad-char",  1,  1,  do_pin_pad_char },
1926
 { "pin-domains", 1,  1,  do_pin_domains  },
1927
 { "label",   1,  1,  do_card_label },
1928
 { "manufacturer",  1,  1,  do_card_manufacturer},
1929
1930
 { NULL, 0, 0, NULL }
1931
};
1932
1933
static struct block ci_blocks[] = {
1934
 { "key",   process_key,  key_commands, NULL  },
1935
1936
 { NULL, NULL, NULL, NULL }
1937
};
1938
1939
/*
1940
 * Filesystem section
1941
 */
1942
static struct command fs_commands[] = {
1943
 { "type",    1,  1,  do_file_type  },
1944
 { "path",    1,  1,  do_file_path  },
1945
 { "file-id",   1,  1,  do_fileid },
1946
 { "structure",   1,  1,  do_structure  },
1947
 { "size",    1,  -1, do_size   },
1948
 { "record-length", 1,  1,  do_reclength  },
1949
 { "AID",   1,  1,  do_aid    },
1950
 { "ACL",   1,  -1, do_acl    },
1951
/* AID dependent sub-profile */
1952
 { "profile-extension", 1,  1,  do_profile_extension  },
1953
/* AID of the DFs without file-id */
1954
 { "exclusive-aid", 1,  1,  do_exclusive_aid  },
1955
 { "content",   1,  1,  do_content  },
1956
 { "prop-attr",   1,  1,  do_prop_attr  },
1957
1958
 { NULL, 0, 0, NULL }
1959
};
1960
1961
static struct block fs_blocks[] = {
1962
 { "DF",    process_df, fs_commands,  fs_blocks },
1963
 { "EF",    process_ef, fs_commands,  fs_blocks },
1964
 { "BSO",   process_bso,  fs_commands,  fs_blocks },
1965
 { "template",    process_tmpl, fs_commands,  fs_blocks },
1966
1967
 { NULL, NULL, NULL, NULL }
1968
};
1969
1970
/*
1971
 * Pin section
1972
 */
1973
static struct command pi_commands[] = {
1974
 { "file",    1,  1,  do_pin_file   },
1975
 { "offset",    1,  1,  do_pin_offset   },
1976
 { "attempts",    1,  2,  do_pin_attempts   },
1977
 { "encoding",    1,  1,  do_pin_type   },
1978
 { "reference",   1,  1,  do_pin_reference  },
1979
 { "auth-id",   1,  1,  do_pin_authid   },
1980
 { "max-length",  1,  1,  do_pin_maxlength  },
1981
 { "min-length",  1,  1,  do_pin_minlength  },
1982
 { "stored-length", 1,  1,  do_pin_storedlength },
1983
 { "max-unlocks", 1,  1,  do_pin_maxunlocks },
1984
 { "flags",   1,  -1, do_pin_flags    },
1985
 { NULL, 0, 0, NULL }
1986
};
1987
1988
/*
1989
 * pkcs15 dialect section
1990
 */
1991
static struct command p15_commands[] = {
1992
 { "direct-certificates", 1,  1,  do_direct_certificates },
1993
 { "encode-df-length",    1,  1,  do_encode_df_length },
1994
 { "do-last-update",    1,  1,  do_encode_update_field },
1995
 { "pkcs15-id-style",   1,  1,  do_pkcs15_id_style },
1996
 { "minidriver-support-style",  1,  1,  do_minidriver_support_style },
1997
 { NULL, 0, 0, NULL }
1998
};
1999
2000
static struct block root_blocks[] = {
2001
 { "filesystem",  process_block,  NULL,   fs_blocks },
2002
 { "cardinfo",    process_block,  ci_commands,  ci_blocks },
2003
 { "pin",   process_pin,  pi_commands,  NULL  },
2004
 { "option",    process_option, NULL,   root_blocks },
2005
 { "macros",    process_macros, NULL,   NULL  },
2006
 { "pkcs15",    process_block,  p15_commands, NULL  },
2007
2008
 { NULL, NULL, NULL, NULL }
2009
};
2010
2011
static struct block root_ops = {
2012
   "root",    process_block,  NULL,   root_blocks
2013
};
2014
2015
static int
2016
is_macro_character(char c)
2017
95.3k
{
2018
95.3k
  if (isalnum(c) || c == '-' || c == '_')
2019
82.2k
    return 1;
2020
13.1k
  return 0;
2021
95.3k
}
2022
2023
static int
2024
get_inner_word(const char *str, char word[WORD_SIZE])
2025
8.23k
{
2026
8.23k
  const char *inner = NULL;
2027
8.23k
  size_t len = 0;
2028
2029
8.23k
  inner = str;
2030
2031
41.4k
  while (is_macro_character(*inner)) {
2032
33.2k
    inner++;
2033
33.2k
    len++;
2034
33.2k
  }
2035
8.23k
  if (len >= WORD_SIZE)
2036
6
    return 1;
2037
8.22k
  memcpy(word, str, len);
2038
8.22k
  word[len] = '\0';
2039
8.22k
  return 0;
2040
8.23k
}
2041
2042
/*
2043
 * Checks for a reference loop for macro named start_name in macro definitions.
2044
 * Function returns 1 if a reference loop is detected, 0 otherwise.
2045
 */
2046
static int
2047
check_macro_reference_loop(const char *start_name, sc_macro_t *macro, sc_profile_t *profile, int depth)
2048
2.71k
{
2049
2.71k
  scconf_list *value;
2050
2.71k
  const char *name = NULL;
2051
2.71k
  sc_macro_t  *m;
2052
2.71k
  char word[WORD_SIZE];
2053
2054
2.71k
  if (!start_name || !macro || !profile || depth == 16)
2055
11
    return 1;
2056
2057
  /* For some reason, the macro value is a list where we need to check for references */
2058
6.31k
  for (value = macro->value; value != NULL; value = value->next) {
2059
    /* Find name in macro value */
2060
3.80k
    char *macro_value = value->data;
2061
3.80k
    if (!(name = strchr(macro_value, '$')))
2062
2.51k
      continue;
2063
    /* Extract the macro name from the string */
2064
1.28k
    if (get_inner_word(name + 1, word))
2065
3
      return 1;
2066
    /* Find whether name corresponds to some other macro */
2067
1.28k
    if (!(m = find_macro(profile, word)))
2068
526
      continue;
2069
    /* Check for loop */
2070
760
    if (!strcmp(m->name, start_name))
2071
8
      return 1;
2072
    /* Reference loop was found to the original macro name */
2073
752
    if (check_macro_reference_loop(start_name, m, profile, depth + 1) == 1) {
2074
184
      return 1;
2075
184
    }
2076
752
  }
2077
2.50k
  return 0;
2078
2.70k
}
2079
2080
static int
2081
build_argv(struct state *cur, const char *cmdname,
2082
    scconf_list *list, char **argv, unsigned int max)
2083
48.5k
{
2084
48.5k
  unsigned int  argc;
2085
48.5k
  const char  *str;
2086
48.5k
  sc_macro_t  *macro;
2087
48.5k
  int   r;
2088
2089
108k
  for (argc = 0; list; list = list->next) {
2090
60.2k
    if (argc >= max) {
2091
10
      parse_error(cur, "%s: too many arguments", cmdname);
2092
10
      return SC_ERROR_INVALID_ARGUMENTS;
2093
10
    }
2094
2095
60.2k
    str = list->data;
2096
60.2k
    if (str[0] != '$') {
2097
      /* When str contains macro inside, macro reference loop needs to be checked */
2098
58.4k
      const char *macro_name = NULL;
2099
58.4k
      if ((macro_name = strchr(str, '$'))) {
2100
        /* Macro does not have to start at the first position */
2101
6.94k
        char word[WORD_SIZE];
2102
6.94k
        if (get_inner_word(macro_name + 1, word) != 0) {
2103
3
          return SC_ERROR_SYNTAX_ERROR;
2104
3
        }
2105
6.94k
        if ((macro = find_macro(cur->profile, word))
2106
121
            && check_macro_reference_loop(macro->name, macro, cur->profile, 0)) {
2107
2
          return SC_ERROR_SYNTAX_ERROR;
2108
2
        }
2109
6.94k
      }
2110
2111
58.4k
      argv[argc++] = list->data;
2112
58.4k
      continue;
2113
58.4k
    }
2114
2115
    /* Expand macro reference */
2116
1.86k
    if (!(macro = find_macro(cur->profile, str + 1))) {
2117
22
      parse_error(cur, "%s: unknown macro \"%s\"",
2118
22
          cmdname, str);
2119
22
      return SC_ERROR_SYNTAX_ERROR;
2120
22
    }
2121
2122
1.83k
    if (list == macro->value) {
2123
0
      return SC_ERROR_SYNTAX_ERROR;
2124
0
    }
2125
1.83k
    if (check_macro_reference_loop(macro->name, macro, cur->profile, 0)) {
2126
20
      return SC_ERROR_SYNTAX_ERROR;
2127
20
    }
2128
#ifdef DEBUG_PROFILE
2129
    {
2130
      scconf_list *list;
2131
2132
      printf("Expanding macro %s:", macro->name);
2133
      for (list = macro->value; list; list = list->next)
2134
        printf(" %s", list->data);
2135
      printf("\n");
2136
    }
2137
#endif
2138
1.81k
    r = build_argv(cur, cmdname, macro->value,
2139
1.81k
        argv + argc, max - argc);
2140
1.81k
    if (r < 0)
2141
41
      return r;
2142
2143
1.77k
    argc += r;
2144
1.77k
  }
2145
2146
48.4k
  return argc;
2147
48.5k
}
2148
2149
static int
2150
process_command(struct state *cur, struct command *cmd_info, scconf_list *list)
2151
46.6k
{
2152
46.6k
  const char  *cmd = cmd_info->name;
2153
46.6k
  char    *argv[32];
2154
46.6k
  int   argc, max = 32;
2155
2156
46.6k
  if (cmd_info->max_args >= 0 && max > cmd_info->max_args)
2157
37.9k
    max = cmd_info->max_args;
2158
2159
46.6k
  if ((argc = build_argv(cur, cmd, list, argv, max)) < 0)
2160
56
    return argc;
2161
2162
46.6k
  if (argc < cmd_info->min_args) {
2163
4
    parse_error(cur, "%s: not enough arguments\n", cmd);
2164
4
    return 1;
2165
4
  }
2166
46.6k
  return cmd_info->func(cur, argc, argv);
2167
46.6k
}
2168
2169
static struct block *
2170
find_block_handler(struct block *bp, const char *name)
2171
56.3k
{
2172
56.3k
  if (bp == NULL)
2173
13
    return NULL;
2174
113k
  for (; bp->name; bp++) {
2175
113k
    if (!strcasecmp(bp->name, name))
2176
56.0k
      return bp;
2177
113k
  }
2178
262
  return NULL;
2179
56.3k
}
2180
2181
static struct command *
2182
find_cmd_handler(struct command *cp, const char *name)
2183
46.9k
{
2184
46.9k
  if (cp == NULL)
2185
113
    return NULL;
2186
212k
  for (; cp->name; cp++) {
2187
212k
    if (!strcasecmp(cp->name, name))
2188
46.6k
      return cp;
2189
212k
  }
2190
169
  return NULL;
2191
46.8k
}
2192
2193
static int
2194
process_block(struct state *cur, struct block *info,
2195
    const char *name, scconf_block *blk)
2196
65.6k
{
2197
65.6k
  scconf_item *item;
2198
65.6k
  struct command  *cp;
2199
65.6k
  struct block  *bp;
2200
65.6k
  const char  *cmd, *ident;
2201
65.6k
  int   res = 0;
2202
2203
220k
  for (item = blk->items; res == 0 && item; item = item->next) {
2204
155k
    cmd = item->key;
2205
155k
    if (item->type == SCCONF_ITEM_TYPE_COMMENT)
2206
52.4k
      continue;
2207
103k
    if (!cmd) {
2208
22
      parse_error(cur, "Command can not be processed.");
2209
22
      return SC_ERROR_SYNTAX_ERROR;
2210
22
    }
2211
103k
    if (item->type == SCCONF_ITEM_TYPE_BLOCK) {
2212
56.3k
      scconf_list *nlist;
2213
2214
56.3k
      ident = NULL;
2215
56.3k
      if ((nlist = item->value.block->name) != NULL) {
2216
56.3k
        if (nlist->next) {
2217
3
          parse_error(cur, "Too many name components in block name.");
2218
3
          return SC_ERROR_SYNTAX_ERROR;
2219
3
        }
2220
56.3k
        ident = nlist->data;
2221
56.3k
      }
2222
#ifdef DEBUG_PROFILE
2223
      printf("Processing %s %s\n", cmd, ident? ident : "");
2224
#endif
2225
56.3k
      if ((bp = find_block_handler(info->blk_info, cmd))) {
2226
56.0k
        res = bp->handler(cur, bp, ident, item->value.block);
2227
56.0k
        continue;
2228
56.0k
      }
2229
56.3k
    }
2230
46.9k
    else if (item->type == SCCONF_ITEM_TYPE_VALUE) {
2231
#ifdef DEBUG_PROFILE
2232
      printf("Processing %s\n", cmd);
2233
#endif
2234
46.9k
      if ((cp = find_cmd_handler(info->cmd_info, cmd))) {
2235
46.6k
        res = process_command(cur, cp, item->value.list);
2236
46.6k
        continue;
2237
46.6k
      }
2238
46.9k
    }
2239
557
    parse_error(cur, "Command \"%s\" not understood in this context.", cmd);
2240
557
    return SC_ERROR_SYNTAX_ERROR;
2241
103k
  }
2242
2243
65.0k
  if (res > 0)
2244
975
    res = SC_ERROR_SYNTAX_ERROR;
2245
65.0k
  return res;
2246
65.6k
}
2247
2248
static int
2249
process_conf(struct sc_profile *profile, scconf_context *conf)
2250
11.7k
{
2251
11.7k
  struct state  state;
2252
2253
11.7k
  memset(&state, 0, sizeof(state));
2254
11.7k
  state.filename = conf->filename;
2255
11.7k
  state.profile = profile;
2256
11.7k
  return process_block(&state, &root_ops, "root", conf->root);
2257
11.7k
}
2258
2259
static struct file_info *
2260
sc_profile_find_file(struct sc_profile *pro,
2261
    const sc_path_t *path, const char *name)
2262
78.1k
{
2263
78.1k
  struct file_info  *fi;
2264
78.1k
  size_t        len;
2265
78.1k
  const u8      *value;
2266
2267
78.1k
  value = path ? path->value : (const u8*) "";
2268
78.1k
  len = path ? path->len : 0;
2269
246k
  for (fi = pro->ef_list; fi; fi = fi->next) {
2270
193k
    sc_path_t *fpath = &fi->file->path;
2271
2272
193k
    if (!strcasecmp(fi->ident, name) && fpath->len >= len && !memcmp(fpath->value, value, len))
2273
24.9k
      return fi;
2274
193k
  }
2275
53.1k
  return NULL;
2276
78.1k
}
2277
2278
2279
static struct file_info *
2280
sc_profile_find_file_by_path(struct sc_profile *pro, const sc_path_t *path)
2281
7.34k
{
2282
7.34k
  struct file_info *fi, *out = NULL;
2283
7.34k
  struct sc_path *fp_path, *fpp_path;
2284
2285
#ifdef DEBUG_PROFILE
2286
  struct sc_context *ctx = pro->card->ctx;
2287
2288
  sc_log(ctx, "profile's EF list:");
2289
  for (fi = pro->ef_list; fi; fi = fi->next)   {
2290
    sc_log(ctx, "'%s' (path:%s)",  fi->ident, sc_print_path(&fi->file->path));
2291
    sc_log(ctx, "fi parent %p", fi->parent);
2292
    if (fi->parent && fi->parent->file)
2293
      sc_log(ctx, "fi parent path %s", sc_print_path(&fi->parent->file->path));
2294
  }
2295
  sc_log(ctx, "find profile file by path:%s", sc_print_path(path));
2296
#endif
2297
2298
7.34k
  if (!path || (!path->len && !path->aid.len))
2299
2.08k
    return NULL;
2300
2301
107k
  for (fi = pro->ef_list; fi; fi = fi->next) {
2302
102k
    fp_path = &fi->file->path;
2303
102k
    fpp_path = fi->parent ? &fi->parent->file->path : NULL;
2304
2305
102k
    if (fp_path->len != path->len)
2306
17.6k
      continue;
2307
84.5k
    if (fp_path->len && memcmp(fp_path->value, path->value, path->len))
2308
9.29k
      continue;
2309
2310
75.2k
    if (path->aid.len && fp_path->aid.len)   {
2311
34.6k
      if (memcmp(fp_path->aid.value, path->aid.value, path->aid.len))
2312
167
        continue;
2313
34.6k
    }
2314
40.6k
    else if (path->aid.len && !fp_path->aid.len && fpp_path)   {
2315
34.7k
      if (fpp_path->type == SC_PATH_TYPE_DF_NAME && fpp_path->len)   {
2316
109
        if (fpp_path->len != path->aid.len)
2317
40
          continue;
2318
69
        if (memcmp(fpp_path->value, path->aid.value, path->aid.len))
2319
62
          continue;
2320
69
      }
2321
34.6k
      else if (fpp_path->aid.len)   {
2322
662
        if (fpp_path->aid.len != path->aid.len)
2323
104
          continue;
2324
558
        if (memcmp(fpp_path->aid.value, path->aid.value, path->aid.len))
2325
119
          continue;
2326
558
      }
2327
34.7k
    }
2328
2329
74.7k
    out = fi;
2330
74.7k
  }
2331
2332
#ifdef DEBUG_PROFILE
2333
  sc_log(ctx, "returns (%s)", out ? out->ident: "<null>");
2334
#endif
2335
5.26k
  return out;
2336
7.34k
}
2337
2338
int
2339
sc_profile_get_parent(struct sc_profile *profile,
2340
    const char *name, sc_file_t **ret)
2341
67
{
2342
67
  struct file_info *fi = NULL;
2343
2344
67
  if ((fi = sc_profile_find_file(profile, NULL, name)) == NULL)
2345
0
    return SC_ERROR_FILE_NOT_FOUND;
2346
2347
67
  if (!fi->parent)
2348
1
    return SC_ERROR_FILE_NOT_FOUND;
2349
2350
66
  sc_file_dup(ret, fi->parent->file);
2351
66
  if (*ret == NULL)
2352
0
    return SC_ERROR_OUT_OF_MEMORY;
2353
66
  return 0;
2354
66
}
2355
2356
/*
2357
 * Split up KEY0 or CHV1 into SC_AC_XXX and a number
2358
 */
2359
static int
2360
get_authid(struct state *cur, const char *value,
2361
    unsigned int *type, unsigned int *num)
2362
8.01k
{
2363
8.01k
  char  temp[16];
2364
8.01k
  size_t  n;
2365
2366
8.01k
  if (isdigit((unsigned char) *value)) {
2367
1.86k
    *num = 0;
2368
1.86k
    return get_uint(cur, value, type);
2369
1.86k
  }
2370
2371
6.15k
  if (strlen(value) >= sizeof(temp))
2372
7
    return 1;
2373
2374
6.14k
  n = strcspn(value, "0123456789x");
2375
6.14k
  strlcpy(temp, value, (sizeof(temp) > n) ? n + 1 : sizeof(temp));
2376
2377
6.14k
  if (map_str2int(cur, temp, type, aclNames))
2378
45
    return 1;
2379
6.09k
  if (value[n])
2380
1.66k
    return get_uint(cur, value + n, num);
2381
4.43k
  *num = 0;
2382
4.43k
  return 0;
2383
6.09k
}
2384
2385
static int
2386
get_uint(struct state *cur, const char *value, unsigned int *vp)
2387
16.1k
{
2388
16.1k
  char  *ep;
2389
16.1k
  unsigned long tmp;
2390
2391
16.1k
  if (strstr(value, "0x") == value)
2392
972
    tmp = strtoul(value + 2, &ep, 16);
2393
15.1k
  else if (strstr(value, "x") == value)
2394
969
    tmp = strtoul(value + 1, &ep, 16);
2395
14.2k
  else
2396
14.2k
    tmp = strtoul(value, &ep, 0);
2397
16.1k
  if (*ep != '\0') {
2398
163
    parse_error(cur, "invalid integer argument \"%s\"\n", value);
2399
163
    return 1;
2400
163
  }
2401
16.0k
  if (tmp > INT_MAX) {
2402
456
    parse_error(cur, "the number \"%s\" is too large\n", value);
2403
456
    return 1;
2404
456
  }
2405
15.5k
  *vp = (int)tmp;
2406
15.5k
  return 0;
2407
16.0k
}
2408
2409
static int
2410
get_bool(struct state *cur, const char *value, unsigned int *vp)
2411
2.54k
{
2412
2.54k
  if (!strcasecmp(value, "on")
2413
2.13k
   || !strcasecmp(value, "yes")
2414
1.92k
   || !strcasecmp(value, "true")) {
2415
1.14k
    *vp = 1;
2416
1.14k
  } else
2417
1.39k
  if (!strcasecmp(value, "off")
2418
1.38k
   || !strcasecmp(value, "no")
2419
1.27k
   || !strcasecmp(value, "false")) {
2420
1.27k
    *vp = 0;
2421
1.27k
  } else {
2422
121
    parse_error(cur, "invalid boolean argument \"%s\"\n", value);
2423
121
    return 1;
2424
121
  }
2425
2.41k
  return 0;
2426
2.54k
}
2427
2428
static int
2429
map_str2int(struct state *cur, const char *value,
2430
    unsigned int *vp, struct map *map)
2431
44.2k
{
2432
44.2k
  unsigned int  n;
2433
44.2k
  const char  *what;
2434
2435
44.2k
  if (isdigit((unsigned char) *value))
2436
4.56k
    return get_uint(cur, value, vp);
2437
186k
  for (n = 0; map[n].name; n++) {
2438
186k
    if (!strcasecmp(value, map[n].name)) {
2439
39.3k
      *vp = map[n].val;
2440
39.3k
      return 0;
2441
39.3k
    }
2442
186k
  }
2443
2444
  /* Try to print a meaningful error message */
2445
392
  what = "argument";
2446
2.05k
  for (n = 0; mapNames[n].name; n++) {
2447
2.05k
    if (mapNames[n].addr == map) {
2448
391
      what = mapNames[n].name;
2449
391
      break;
2450
391
    }
2451
2.05k
  }
2452
2453
392
  parse_error(cur, "invalid %s \"%s\"\n", what, value);
2454
392
  return SC_ERROR_SYNTAX_ERROR;
2455
39.7k
}
2456
2457
static int
2458
setstr(char **strp, const char *value)
2459
1.90k
{
2460
1.90k
  if (*strp)
2461
1.89k
    free(*strp);
2462
1.90k
  *strp = strdup(value);
2463
1.90k
  return 0;
2464
1.90k
}
2465
2466
/*
2467
 * Evaluate numeric expressions
2468
 */
2469
#include <setjmp.h>
2470
2471
struct num_exp_ctx {
2472
  struct state *  state;
2473
  jmp_buf   error;
2474
2475
  int   j;
2476
  char    word[WORD_SIZE];
2477
2478
  char *    unget;
2479
  char *    str;
2480
  int   argc;
2481
  char **   argv;
2482
};
2483
2484
static void expr_eval(struct num_exp_ctx *, unsigned int *, unsigned int, int);
2485
2486
static void
2487
expr_fail(struct num_exp_ctx *ctx)
2488
222
{
2489
222
  longjmp(ctx->error, 1);
2490
222
}
2491
2492
static void
2493
expr_put(struct num_exp_ctx *ctx, int c)
2494
26.9k
{
2495
26.9k
  if (ctx->j >= (int)sizeof(ctx->word))
2496
1
    expr_fail(ctx);
2497
26.9k
  ctx->word[ctx->j++] = (char)c;
2498
26.9k
}
2499
2500
static char *
2501
__expr_get(struct num_exp_ctx *ctx, int eof_okay)
2502
14.2k
{
2503
14.2k
  char  *s;
2504
2505
14.2k
  if ((s = ctx->unget) != NULL) {
2506
2.15k
    ctx->unget = NULL;
2507
2.15k
    return s;
2508
2.15k
  }
2509
2510
12.1k
  ctx->j = 0;
2511
12.1k
  s = ctx->str;
2512
12.2k
  do {
2513
12.2k
    if (s == NULL || *s == '\0') {
2514
6.29k
      if (ctx->argc == 0) {
2515
3.19k
        if (eof_okay)
2516
3.14k
          return NULL;
2517
54
        expr_fail(ctx);
2518
54
      }
2519
3.15k
      ctx->str = s = *(ctx->argv++);
2520
3.15k
      ctx->argc--;
2521
3.15k
    }
2522
2523
9.07k
    while (isspace((unsigned char)*s))
2524
293
      s++;
2525
9.07k
  } while (*s == '\0');
2526
2527
8.98k
  if (isdigit((unsigned char)*s)) {
2528
5.48k
    while (isdigit((unsigned char)*s))
2529
14.0k
      expr_put(ctx, *s++);
2530
5.48k
  }
2531
3.49k
  else if (*s == '$') {
2532
71
    expr_put(ctx, *s++);
2533
721
    while (is_macro_character(*s))
2534
650
      expr_put(ctx, *s++);
2535
71
  }
2536
3.42k
  else if (strchr("*/+-()|&", *s)) {
2537
3.32k
    expr_put(ctx, *s++);
2538
3.32k
  }
2539
99
  else {
2540
99
    expr_fail(ctx);
2541
99
  }
2542
8.98k
  ctx->str = s;
2543
2544
8.98k
  expr_put(ctx, '\0');
2545
8.98k
  return ctx->word;
2546
12.1k
}
2547
2548
static char *
2549
expr_get(struct num_exp_ctx *ctx)
2550
6.31k
{
2551
6.31k
  return __expr_get(ctx, 0);
2552
6.31k
}
2553
2554
static void
2555
expr_unget(struct num_exp_ctx *ctx, char *s)
2556
2.18k
{
2557
2.18k
  if (ctx->unget)
2558
0
    expr_fail(ctx);
2559
2.18k
  ctx->unget = s;
2560
2.18k
}
2561
2562
static void
2563
expr_expect(struct num_exp_ctx *ctx, int c)
2564
132
{
2565
132
  char  *tok;
2566
2567
132
  tok = expr_get(ctx);
2568
132
  if (tok[0] != (char)c || tok[1])
2569
3
    expr_fail(ctx);
2570
132
}
2571
2572
553
#define MAX_BRACKETS 32
2573
static void
2574
expr_term(struct num_exp_ctx *ctx, unsigned int *vp, int opening_brackets)
2575
6.17k
{
2576
6.17k
  char  *tok;
2577
2578
6.17k
  tok = expr_get(ctx);
2579
6.17k
  if (*tok == '(') {
2580
553
    if (opening_brackets + 1 > MAX_BRACKETS) {
2581
2
      parse_error(ctx->state, "Too many \"%s\" in expression", tok);
2582
2
      expr_fail(ctx);
2583
2
    }
2584
553
    expr_eval(ctx, vp, 1, opening_brackets + 1);
2585
553
    expr_expect(ctx, ')');
2586
553
  }
2587
5.62k
  else if (isdigit((unsigned char)*tok)) {
2588
5.48k
    char  *ep;
2589
5.48k
    unsigned long tmp;
2590
2591
5.48k
    tmp = strtoul(tok, &ep, 0);
2592
5.48k
    if (*ep)
2593
3
      expr_fail(ctx);
2594
5.48k
    if (tmp > UINT_MAX)
2595
53
      expr_fail(ctx);
2596
5.48k
    *vp = (unsigned int)tmp;
2597
5.48k
  }
2598
143
  else if (*tok == '$') {
2599
62
    sc_macro_t  *mac;
2600
62
    char    *argv[32];
2601
62
    int   argc;
2602
2603
62
    if (!(mac = find_macro(ctx->state->profile, tok + 1)))
2604
10
      expr_fail(ctx);
2605
62
    argc = build_argv(ctx->state, "<expr>", mac->value, argv, 32);
2606
62
    if (argc < 0 || get_uint_eval(ctx->state, argc, argv, vp) < 0)
2607
4
      expr_fail(ctx);
2608
62
  }
2609
81
  else {
2610
81
    parse_error(ctx->state, "Unexpected token \"%s\" in expression", tok);
2611
81
    expr_fail(ctx);
2612
81
  }
2613
6.17k
}
2614
2615
static void
2616
expr_eval(struct num_exp_ctx *ctx, unsigned int *vp, unsigned int pri, int opening_brackets)
2617
6.17k
{
2618
6.17k
  unsigned int  left, right, new_pri;
2619
6.17k
  char    *tok, op;
2620
2621
6.17k
  expr_term(ctx, &left, opening_brackets);
2622
2623
8.54k
  while (1) {
2624
7.96k
    tok = __expr_get(ctx, 1);
2625
7.96k
    if (tok == NULL)
2626
3.14k
      break;
2627
2628
4.82k
    op = tok[0];
2629
2630
4.82k
    new_pri = 0;
2631
4.82k
    switch (op) {
2632
611
    case '*':
2633
1.50k
    case '/':
2634
1.50k
      new_pri++;
2635
      /* fall through */
2636
2.47k
    case '+':
2637
3.44k
    case '-':
2638
3.44k
      new_pri++;
2639
      /* fall through */
2640
4.01k
    case '&':
2641
4.01k
      new_pri++;
2642
      /* fall through */
2643
4.61k
    case '|':
2644
4.61k
      new_pri++;
2645
      /* fall through */
2646
4.79k
    case ')':
2647
4.79k
      break;
2648
10
    default:
2649
10
      expr_fail(ctx);
2650
4.82k
    }
2651
2652
4.79k
    if (new_pri < pri) {
2653
2.18k
      expr_unget(ctx, tok);
2654
2.18k
      break;
2655
2.18k
    }
2656
2.61k
    pri = new_pri;
2657
2658
2.61k
    expr_eval(ctx, &right, new_pri + 1, opening_brackets);
2659
2.61k
    switch (op) {
2660
331
    case '*': left *= right; break;
2661
459
    case '/':
2662
459
      if (right == 0)
2663
6
        expr_fail(ctx);
2664
459
      left /= right; break;
2665
550
    case '+': left += right; break;
2666
462
    case '-': left -= right; break;
2667
287
    case '&': left &= right; break;
2668
286
    case '|': left |= right; break;
2669
2
    default: expr_fail(ctx);
2670
2.61k
    }
2671
2.61k
  }
2672
2673
5.90k
  *vp = left;
2674
5.90k
}
2675
2676
static int
2677
get_uint_eval(struct state *cur, int argc, char **argv, unsigned int *vp)
2678
3.01k
{
2679
3.01k
  struct num_exp_ctx  ctx;
2680
2681
3.01k
  memset(&ctx, 0, sizeof(ctx));
2682
3.01k
  ctx.state = cur;
2683
3.01k
  ctx.argc  = argc;
2684
3.01k
  ctx.argv  = argv;
2685
2686
3.01k
  if (setjmp(ctx.error)) {
2687
222
    parse_error(cur, "invalid numeric expression\n");
2688
222
    return SC_ERROR_SYNTAX_ERROR;
2689
222
  }
2690
2691
2.79k
  expr_eval(&ctx, vp, 0, 0);
2692
2.79k
  if (ctx.str[0] || ctx.argc)
2693
22
    expr_fail(&ctx);
2694
2695
2.79k
  return 0;
2696
3.01k
}
2697
2698
static void
2699
parse_error(struct state *cur, const char *fmt, ...)
2700
2.21k
{
2701
2.21k
  char  buffer[1024], *sp;
2702
2.21k
  va_list ap;
2703
2704
2.21k
  va_start(ap, fmt);
2705
2.21k
  vsnprintf(buffer, sizeof(buffer), fmt, ap);
2706
2.21k
  va_end(ap);
2707
2708
2.21k
  if ((sp = strchr(buffer, '\n')) != NULL)
2709
1.51k
    *sp = '\0';
2710
2711
2.21k
  if (cur->profile->card && cur->profile->card->ctx)
2712
2.17k
    sc_log(cur->profile->card->ctx, "%s: %s", cur->filename, buffer);
2713
49
  else
2714
49
    fprintf(stdout, "%s: %s\n", cur->filename, buffer);
2715
2.21k
}