Coverage Report

Created: 2026-12-03 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/dir.c
Line
Count
Source
1
/*
2
 * dir.c: Stuff for handling EF(DIR)
3
 *
4
 * Copyright (C) 2001, 2002  Juha Yrjölä <juha.yrjola@iki.fi>
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#ifdef HAVE_CONFIG_H
22
#include "config.h"
23
#endif
24
25
#include <stdlib.h>
26
#include <string.h>
27
28
#include "internal.h"
29
#include "asn1.h"
30
31
struct app_entry {
32
  const u8 *aid;
33
  size_t aid_len;
34
  const char *desc;
35
};
36
37
static const struct app_entry apps[] = {
38
  { (const u8 *) "\xA0\x00\x00\x00\x63PKCS-15", 12, "PKCS #15" },
39
  { (const u8 *) "\xA0\x00\x00\x01\x77PKCS-15", 12, "Belgian eID" },
40
  { (const u8 *) "\x44\x46\x20\x69\x73\x73\x75\x65\x72", 9, "Portugal eID" },
41
  { (const u8 *) "\xE8\x28\xBD\x08\x0F\xA0\x00\x00\x01\x67\x45\x53\x49\x47\x4E", 15, "ESIGN"},
42
  { (const u8 *) "\xE8\x28\xBD\x08\x0F\x80\x25\x00\x00\x01\xFF\x00\x10", 13, "CPx IAS"},
43
  { (const u8 *) "\xE8\x28\xBD\x08\x0F\x80\x25\x00\x00\x01\xFF\x00\x20", 13, "CPx IAS CL"},
44
  { (const u8 *) "\xE8\x28\xBD\x08\x0F\xD2\x50\x45\x43\x43\x2D\x65\x49\x44", 14, "ECC eID"},
45
  { (const u8 *) "\xE8\x28\xBD\x08\x0F\xD2\x50\x47\x65\x6E\x65\x72\x69\x63", 14, "ECC Generic PKI"},
46
};
47
48
static const struct sc_asn1_entry c_asn1_dirrecord[] = {
49
  { "aid",   SC_ASN1_OCTET_STRING, SC_ASN1_APP | 15, 0, NULL, NULL },
50
  { "label", SC_ASN1_UTF8STRING,   SC_ASN1_APP | 16, SC_ASN1_OPTIONAL, NULL, NULL },
51
  { "path",  SC_ASN1_OCTET_STRING, SC_ASN1_APP | 17, SC_ASN1_OPTIONAL, NULL, NULL },
52
  { "ddo",   SC_ASN1_OCTET_STRING, SC_ASN1_APP | 19 | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
53
  { NULL, 0, 0, 0, NULL, NULL }
54
};
55
56
static const struct sc_asn1_entry c_asn1_dir[] = {
57
  { "dirRecord", SC_ASN1_STRUCT, SC_ASN1_APP | 1 | SC_ASN1_CONS, 0, NULL, NULL },
58
  { NULL, 0, 0, 0, NULL, NULL }
59
};
60
61
62
static int
63
parse_dir_record(sc_card_t *card, u8 ** buf, size_t *buflen, int rec_nr)
64
0
{
65
0
  struct sc_context *ctx = card->ctx;
66
0
  struct sc_asn1_entry asn1_dirrecord[5], asn1_dir[2];
67
0
  scconf_block *conf_block = NULL;
68
0
  sc_app_info_t *app = NULL;
69
0
  struct sc_aid aid;
70
0
  u8 label[128], path[128], ddo[128];
71
0
  size_t label_len = sizeof(label) - 1, path_len = sizeof(path), ddo_len = sizeof(ddo);
72
0
  int r;
73
74
0
  LOG_FUNC_CALLED(ctx);
75
0
  aid.len = sizeof(aid.value);
76
77
0
  memset(label, 0, sizeof(label));
78
0
  sc_copy_asn1_entry(c_asn1_dirrecord, asn1_dirrecord);
79
0
  sc_copy_asn1_entry(c_asn1_dir, asn1_dir);
80
0
  sc_format_asn1_entry(asn1_dir + 0, asn1_dirrecord, NULL, 0);
81
0
  sc_format_asn1_entry(asn1_dirrecord + 0, aid.value, &aid.len, 0);
82
0
  sc_format_asn1_entry(asn1_dirrecord + 1, label, &label_len, 0);
83
0
  sc_format_asn1_entry(asn1_dirrecord + 2, path, &path_len, 0);
84
0
  sc_format_asn1_entry(asn1_dirrecord + 3, ddo, &ddo_len, 0);
85
86
0
  r = sc_asn1_decode(ctx, asn1_dir, *buf, *buflen, (const u8 **) buf, buflen);
87
0
  if (r == SC_ERROR_ASN1_END_OF_CONTENTS)
88
0
    LOG_FUNC_RETURN(ctx, r);
89
0
  LOG_TEST_RET(ctx, r, "EF(DIR) parsing failed");
90
91
0
  conf_block = sc_get_conf_block(ctx, "framework", "pkcs15", 1);
92
0
  if (conf_block)   {
93
0
    scconf_block **blocks = NULL;
94
0
    char aid_str[SC_MAX_AID_STRING_SIZE];
95
0
    int ignore_app = 0;
96
97
0
    sc_bin_to_hex(aid.value, aid.len, aid_str, sizeof(aid_str), 0);
98
0
    blocks = scconf_find_blocks(card->ctx->conf, conf_block, "application", aid_str);
99
0
    if (blocks)   {
100
0
      ignore_app = (blocks[0] && scconf_get_bool(blocks[0], "disable", 0));
101
0
      free(blocks);
102
0
     }
103
104
0
    if (ignore_app)   {
105
0
      sc_log(ctx, "Application '%s' ignored", aid_str);
106
0
      LOG_FUNC_RETURN(ctx, SC_SUCCESS);
107
0
    }
108
0
  }
109
110
0
  app = calloc(1, sizeof(struct sc_app_info));
111
0
  if (app == NULL)
112
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
113
114
0
  memcpy(&app->aid, &aid, sizeof(struct sc_aid));
115
116
0
  if (asn1_dirrecord[1].flags & SC_ASN1_PRESENT)
117
0
    app->label = strdup((char *) label);
118
0
  else
119
0
    app->label = NULL;
120
121
0
  if (asn1_dirrecord[2].flags & SC_ASN1_PRESENT && path_len > 0) {
122
    /* application path present: ignore AID */
123
0
    if (path_len > SC_MAX_PATH_SIZE) {
124
0
      free(app->label);
125
0
      free(app);
126
0
      LOG_TEST_RET(ctx, SC_ERROR_INVALID_ASN1_OBJECT, "Application path is too long.");
127
0
    }
128
0
    memcpy(app->path.value, path, path_len);
129
0
    app->path.len = path_len;
130
0
    app->path.type = SC_PATH_TYPE_PATH;
131
0
  }
132
0
  else {
133
    /* application path not present: use AID as application path */
134
0
    memcpy(app->path.value, aid.value, aid.len);
135
0
    app->path.len = aid.len;
136
0
    app->path.type = SC_PATH_TYPE_DF_NAME;
137
0
  }
138
139
0
  if (asn1_dirrecord[3].flags & SC_ASN1_PRESENT) {
140
0
    app->ddo.value = malloc(ddo_len);
141
0
    if (app->ddo.value == NULL) {
142
0
      free(app->label);
143
0
      free(app);
144
0
      LOG_TEST_RET(ctx, SC_ERROR_OUT_OF_MEMORY, "Cannot allocate DDO value");
145
0
    }
146
0
    memcpy(app->ddo.value, ddo, ddo_len);
147
0
    app->ddo.len = ddo_len;
148
0
  } else {
149
0
    app->ddo.value = NULL;
150
0
    app->ddo.len = 0;
151
0
  }
152
153
0
  app->rec_nr = rec_nr;
154
0
  card->app[card->app_count] = app;
155
0
  card->app_count++;
156
157
0
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
158
0
}
159
160
161
int sc_enum_apps(sc_card_t *card)
162
0
{
163
0
  struct sc_context *ctx = card->ctx;
164
0
  sc_path_t path;
165
0
  int ef_structure;
166
0
  size_t file_size, jj;
167
0
  int r, ii, idx;
168
0
  struct sc_file *ef_dir = NULL;
169
170
0
  LOG_FUNC_CALLED(ctx);
171
172
0
  sc_free_apps(card);
173
0
  card->app_count = 0;
174
175
0
  sc_format_path("3F002F00", &path);
176
0
  r = sc_select_file(card, &path, &ef_dir);
177
0
  if (r < 0)
178
0
    sc_file_free(ef_dir);
179
0
  LOG_TEST_RET(ctx, r, "Cannot select EF.DIR file");
180
181
0
  if (ef_dir->type != SC_FILE_TYPE_WORKING_EF) {
182
0
    sc_file_free(ef_dir);
183
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_CARD, "EF(DIR) is not a working EF.");
184
0
  }
185
186
0
  ef_structure = ef_dir->ef_structure;
187
0
  file_size = ef_dir->size;
188
0
  sc_file_free(ef_dir);
189
0
  if (ef_structure == SC_FILE_EF_TRANSPARENT) {
190
0
    u8 *buf = NULL, *p;
191
0
    size_t bufsize;
192
193
0
    if (file_size == 0)
194
0
      LOG_FUNC_RETURN(ctx, 0);
195
0
    if (file_size > MAX_FILE_SIZE)
196
0
      LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_DATA);
197
198
0
    buf = malloc(file_size);
199
0
    if (buf == NULL)
200
0
      LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
201
0
    p = buf;
202
0
    r = sc_read_binary(card, 0, buf, file_size, 0);
203
0
    if (r < 0) {
204
0
      free(buf);
205
0
      LOG_TEST_RET(ctx, r, "sc_read_binary() failed");
206
0
    }
207
0
    bufsize = r;
208
0
    while (bufsize > 0) {
209
0
      if (card->app_count == SC_MAX_CARD_APPS) {
210
0
        sc_log(ctx, "Too many applications on card");
211
0
        break;
212
0
      }
213
0
      r = parse_dir_record(card, &p, &bufsize, -1);
214
0
      if (r)
215
0
        break;
216
0
    }
217
0
    if (buf)
218
0
      free(buf);
219
220
0
  }
221
0
  else { /* record structure */
222
0
    unsigned char buf[256], *p;
223
0
    unsigned int rec_nr;
224
0
    size_t rec_size;
225
226
    /* Arbitrary set '16' as maximal number of records to check out:
227
     * to avoid endless loop because of some incomplete cards/drivers */
228
0
    for (rec_nr = 1; rec_nr < 16; rec_nr++) {
229
0
      r = sc_read_record(card, rec_nr, 0, buf, sizeof(buf), SC_RECORD_BY_REC_NR);
230
0
      if (r == SC_ERROR_RECORD_NOT_FOUND)
231
0
        break;
232
0
      LOG_TEST_RET(ctx, r, "read_record() failed");
233
234
0
      if (card->app_count == SC_MAX_CARD_APPS) {
235
0
        sc_log(ctx, "Too many applications on card");
236
0
        break;
237
0
      }
238
239
0
      rec_size = r;
240
0
      p = buf;
241
0
      parse_dir_record(card, &p, &rec_size, (int)rec_nr);
242
0
    }
243
0
  }
244
245
  /* Move known PKCS#15 applications to the head of the list */
246
0
  for (ii=0, idx=0; ii<card->app_count; ii++)   {
247
0
    for (jj=0; jj < sizeof(apps)/sizeof(apps[0]); jj++) {
248
0
      if (apps[jj].aid_len != card->app[ii]->aid.len)
249
0
        continue;
250
0
      if (memcmp(apps[jj].aid, card->app[ii]->aid.value, apps[jj].aid_len))
251
0
        continue;
252
0
      break;
253
0
    }
254
255
0
    if (ii != idx && jj < sizeof(apps)/sizeof(apps[0]))   {
256
0
      struct sc_app_info *tmp = card->app[idx];
257
258
0
      card->app[idx] = card->app[ii];
259
0
      card->app[ii] = tmp;
260
0
      idx++;
261
0
    }
262
0
  }
263
264
0
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
265
0
}
266
267
void sc_free_apps(sc_card_t *card)
268
3.62k
{
269
3.62k
  int i;
270
271
3.62k
  for (i = 0; i < card->app_count; i++) {
272
0
    free(card->app[i]->label);
273
0
    free(card->app[i]->ddo.value);
274
0
    free(card->app[i]);
275
0
  }
276
3.62k
  card->app_count = -1;
277
3.62k
}
278
279
static int encode_dir_record(sc_context_t *ctx, const sc_app_info_t *app,
280
           u8 **buf, size_t *buflen)
281
0
{
282
0
  struct sc_asn1_entry asn1_dirrecord[5], asn1_dir[2];
283
0
  sc_app_info_t   tapp = *app;
284
0
  int r;
285
0
  size_t label_len;
286
287
0
  sc_copy_asn1_entry(c_asn1_dirrecord, asn1_dirrecord);
288
0
  sc_copy_asn1_entry(c_asn1_dir, asn1_dir);
289
0
  sc_format_asn1_entry(asn1_dir + 0, asn1_dirrecord, NULL, 1);
290
0
  sc_format_asn1_entry(asn1_dirrecord + 0, (void *) tapp.aid.value, (void *) &tapp.aid.len, 1);
291
0
  if (tapp.label != NULL) {
292
0
    label_len = strlen(tapp.label);
293
0
    sc_format_asn1_entry(asn1_dirrecord + 1, tapp.label, &label_len, 1);
294
0
  }
295
0
  if (tapp.path.len)
296
0
    sc_format_asn1_entry(asn1_dirrecord + 2, (void *) tapp.path.value,
297
0
             (void *) &tapp.path.len, 1);
298
0
  if (tapp.ddo.value != NULL && tapp.ddo.len)
299
0
    sc_format_asn1_entry(asn1_dirrecord + 3, (void *) tapp.ddo.value,
300
0
             (void *) &tapp.ddo.len, 1);
301
0
  r = sc_asn1_encode(ctx, asn1_dir, buf, buflen);
302
0
  LOG_TEST_RET(ctx, r, "Encode DIR record error");
303
304
0
  return SC_SUCCESS;
305
0
}
306
307
static int update_transparent(sc_card_t *card, sc_file_t *file)
308
0
{
309
0
  u8 *rec, *buf = NULL, *tmp;
310
0
  size_t rec_size, buf_size = 0;
311
0
  int i, r;
312
313
0
  for (i = 0; i < card->app_count; i++) {
314
0
    r = encode_dir_record(card->ctx, card->app[i], &rec, &rec_size);
315
0
    if (r) {
316
0
      if (buf)
317
0
        free(buf);
318
0
      return r;
319
0
    }
320
0
    if (!rec_size)
321
0
      continue;
322
0
    tmp = (u8 *) realloc(buf, buf_size + rec_size);
323
0
    if (!tmp) {
324
0
      if (rec)
325
0
        free(rec);
326
0
      if (buf)
327
0
        free(buf);
328
0
      return SC_ERROR_OUT_OF_MEMORY;
329
0
    }
330
0
    buf = tmp;
331
0
    memcpy(buf + buf_size, rec, rec_size);
332
0
    buf_size += rec_size;
333
0
    free(rec);
334
0
    rec=NULL;
335
0
  }
336
0
  if (file->size > buf_size) {
337
0
    tmp = (u8 *) realloc(buf, file->size);
338
0
    if (!tmp) {
339
0
      free(buf);
340
0
      return SC_ERROR_OUT_OF_MEMORY;
341
0
    }
342
0
    buf = tmp;
343
0
    memset(buf + buf_size, 0, file->size - buf_size);
344
0
    buf_size = file->size;
345
0
  }
346
0
  r = sc_update_binary(card, 0, buf, buf_size, 0);
347
0
  free(buf);
348
0
  LOG_TEST_RET(card->ctx, r, "Unable to update EF(DIR)");
349
350
0
  return SC_SUCCESS;
351
0
}
352
353
static int update_single_record(sc_card_t *card, sc_app_info_t *app)
354
0
{
355
0
  u8 *rec;
356
0
  size_t rec_size;
357
0
  int r;
358
359
0
  r = encode_dir_record(card->ctx, app, &rec, &rec_size);
360
0
  if (r)
361
0
    return r;
362
0
  if (app->rec_nr > 0)
363
0
    r = sc_update_record(card, (unsigned int)app->rec_nr, 0, rec, rec_size, SC_RECORD_BY_REC_NR);
364
0
  else if (app->rec_nr == 0) {
365
    /* create new record entry */
366
0
    r = sc_append_record(card, rec, rec_size, 0);
367
0
    if (r == SC_ERROR_NOT_SUPPORTED) {
368
      /* if the card doesn't support APPEND RECORD we try a
369
       * UPDATE RECORD on the next unused record (and hope
370
       * that there is a record with this index).
371
       */
372
0
      int rec_nr = 0, i;
373
0
      for(i = 0; i < card->app_count; i++)
374
0
        if (card->app[i]->rec_nr > rec_nr)
375
0
          rec_nr = card->app[i]->rec_nr;
376
0
      rec_nr++;
377
0
      r = sc_update_record(card, (unsigned int)rec_nr, 0, rec, rec_size, SC_RECORD_BY_REC_NR);
378
0
    }
379
0
  } else {
380
0
    sc_log(card->ctx, "invalid record number\n");
381
0
    r = SC_ERROR_INTERNAL;
382
0
  }
383
0
  free(rec);
384
0
  LOG_TEST_RET(card->ctx, r, "Unable to update EF(DIR) record");
385
0
  return 0;
386
0
}
387
388
static int update_records(sc_card_t *card)
389
0
{
390
0
  int i, r;
391
392
0
  for (i = 0; i < card->app_count; i++) {
393
0
    r = update_single_record(card, card->app[i]);
394
0
    if (r)
395
0
      return r;
396
0
  }
397
0
  return 0;
398
0
}
399
400
int sc_update_dir(sc_card_t *card, sc_app_info_t *app)
401
0
{
402
0
  sc_path_t path;
403
0
  sc_file_t *file;
404
0
  int r;
405
406
0
  sc_format_path("3F002F00", &path);
407
408
0
  r = sc_select_file(card, &path, &file);
409
0
  LOG_TEST_RET(card->ctx, r, "unable to select EF(DIR)");
410
411
0
  if (file->ef_structure == SC_FILE_EF_TRANSPARENT)
412
0
    r = update_transparent(card, file);
413
0
  else if (app == NULL)
414
0
    r = update_records(card);
415
0
  else
416
0
    r = update_single_record(card, app);
417
0
  sc_file_free(file);
418
0
  return r;
419
0
}