Coverage Report

Created: 2025-10-13 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/muscle-filesystem.c
Line
Count
Source
1
/*
2
 * muscle-filesystem.c: Support for MuscleCard Applet from musclecard.com
3
 *
4
 * Copyright (C) 2006, Identity Alliance, Thomas Harning <support@identityalliance.com>
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 <memory.h>
26
#include <stdio.h>
27
#include <assert.h>
28
29
#include "libopensc/muscle-filesystem.h"
30
#include "libopensc/errors.h"
31
32
0
#define MSCFS_NO_MEMORY   SC_ERROR_OUT_OF_MEMORY
33
4.19k
#define MSCFS_INVALID_ARGS  SC_ERROR_INVALID_ARGUMENTS
34
4.51k
#define MSCFS_FILE_NOT_FOUND  SC_ERROR_FILE_NOT_FOUND
35
7.26k
#define MSCFS_CACHE_INCREMENT 128
36
37
static msc_id rootId = { { 0x3F, 0x00, 0x3F, 0x00 } };
38
39
static const u8* ignoredFiles[] = {
40
  (const u8*)"l0\0\0",
41
  (const u8*)"L0\0\0",
42
  NULL
43
};
44
45
9.17k
mscfs_t *mscfs_new(void) {
46
9.17k
  mscfs_t *fs = malloc(sizeof(mscfs_t));
47
9.17k
  if (!fs)
48
0
    return NULL;
49
9.17k
  memset(fs, 0, sizeof(mscfs_t));
50
9.17k
  memcpy(fs->currentPath, "\x3F\x00", 2);
51
9.17k
  return fs;
52
9.17k
}
53
54
9.17k
void mscfs_free(mscfs_t *fs) {
55
9.17k
  mscfs_clear_cache(fs);
56
9.17k
  free(fs);
57
9.17k
}
58
59
26.0k
void mscfs_clear_cache(mscfs_t* fs) {
60
26.0k
  if(!fs->cache.array) {
61
21.8k
    return;
62
21.8k
  }
63
4.17k
  free(fs->cache.array);
64
4.17k
  fs->cache.array = NULL;
65
4.17k
  fs->cache.totalSize = 0;
66
4.17k
  fs->cache.size = 0;
67
4.17k
}
68
69
static int mscfs_is_ignored(mscfs_t* fs, msc_id objectId)
70
417k
{
71
417k
  int ignored = 0;
72
417k
  const u8** ptr = ignoredFiles;
73
1.25M
  while(ptr && *ptr && !ignored) {
74
834k
    if(0 == memcmp(objectId.id, *ptr, 4))
75
918
      ignored = 1;
76
834k
    ptr++;
77
834k
  }
78
417k
  return ignored;
79
417k
}
80
81
416k
#define MAX_FILES 10000
82
int mscfs_push_file(mscfs_t* fs, mscfs_file_t *file)
83
416k
{
84
416k
  mscfs_cache_t *cache = &fs->cache;
85
416k
  if (cache->size >= MAX_FILES)
86
6
    return SC_ERROR_INTERNAL;
87
416k
  if(!cache->array || cache->size == cache->totalSize) {
88
7.26k
    int length = cache->totalSize + MSCFS_CACHE_INCREMENT;
89
7.26k
    mscfs_file_t *oldArray;
90
7.26k
    cache->totalSize = length;
91
7.26k
    oldArray = cache->array;
92
7.26k
    cache->array = malloc(sizeof(mscfs_file_t) * length);
93
7.26k
    if(!cache->array)
94
0
      return MSCFS_NO_MEMORY;
95
7.26k
    if(oldArray) {
96
3.09k
      memcpy(cache->array, oldArray, sizeof(mscfs_file_t) * cache->size);
97
3.09k
      free(oldArray);
98
3.09k
    }
99
7.26k
  }
100
416k
  cache->array[cache->size] = *file;
101
416k
  cache->size++;
102
416k
  return SC_SUCCESS;
103
416k
}
104
105
16.4k
int mscfs_update_cache(mscfs_t* fs) {
106
16.4k
  mscfs_file_t file;
107
16.4k
  int r;
108
16.4k
  mscfs_clear_cache(fs);
109
16.4k
  r = fs->listFile(&file, 1, fs->udata);
110
16.4k
  if(r == 0)
111
526
    return 0;
112
15.9k
  else if(r < 0)
113
11.5k
    return r;
114
417k
  while(1) {
115
417k
    if(!mscfs_is_ignored(fs, file.objectId)) {
116
      /* Check if its a directory in the root */
117
416k
      u8* oid = file.objectId.id;
118
416k
      if(oid[2] == 0 && oid[3] == 0) {
119
173k
        oid[2] = oid[0];
120
173k
        oid[3] = oid[1];
121
173k
        oid[0] = 0x3F;
122
173k
        oid[1] = 0x00;
123
173k
        file.ef = 0;
124
242k
      } else  {
125
242k
        file.ef = 1; /* File is a working elementary file */
126
242k
      }
127
128
416k
      r = mscfs_push_file(fs, &file);
129
416k
      if (r != SC_SUCCESS)
130
6
        return r;
131
416k
    }
132
417k
    r = fs->listFile(&file, 0, fs->udata);
133
417k
    if(r == 0)
134
1.16k
      break;
135
416k
    else if(r < 0)
136
3.17k
      return r;
137
417k
  }
138
1.16k
  return fs->cache.size;
139
4.34k
}
140
141
int mscfs_check_cache(mscfs_t* fs)
142
164k
{
143
164k
  int r = SC_SUCCESS;
144
164k
  if(!fs->cache.array) {
145
16.4k
    r = mscfs_update_cache(fs);
146
16.4k
  }
147
164k
  return r;
148
164k
}
149
150
int mscfs_lookup_path(mscfs_t* fs, const u8 *path, size_t pathlen, msc_id* objectId, int isDirectory)
151
20.2k
{
152
20.2k
  u8* oid = objectId->id;
153
20.2k
  if ((pathlen & 1) != 0) /* not divisible by 2 */
154
4.06k
    return MSCFS_INVALID_ARGS;
155
16.2k
  if(isDirectory) {
156
    /* Directory must be right next to root */
157
0
    if ((pathlen == 4 && 0 == memcmp(path, "\x3F\x00", 2)) ||
158
0
        (pathlen == 2 && 0 == memcmp(fs->currentPath, "\x3F\x00", 2))) {
159
0
      oid[0] = path[pathlen - 2];
160
0
      oid[1] = path[pathlen - 1];
161
0
      oid[2] = oid[3] = 0;
162
0
    } else {
163
0
      return MSCFS_INVALID_ARGS;
164
0
    }
165
0
  }
166
16.2k
  oid[0] = fs->currentPath[0];
167
16.2k
  oid[1] = fs->currentPath[1];
168
  /* Chop off the root in the path */
169
16.2k
  if(pathlen > 2 && memcmp(path, "\x3F\x00", 2) == 0) {
170
11.6k
    path += 2;
171
11.6k
    pathlen -= 2;
172
11.6k
    oid[0] = 0x3F;
173
11.6k
    oid[1] = 0x00;
174
11.6k
  }
175
  /* Limit to a single directory */
176
16.2k
  if(pathlen > 4)
177
126
    return MSCFS_INVALID_ARGS;
178
  /* Reset to root */
179
16.0k
  if(pathlen == 2 && 0 == memcmp(path, "\x3F\x00", 2)) {
180
1.78k
    oid[0] = oid[2] = path[0];
181
1.78k
    oid[1] = oid[3] = path[1];
182
14.2k
  } else if(pathlen == 2) { /* Path preserved for current-path */
183
8.50k
    oid[2] = path[0];
184
8.50k
    oid[3] = path[1];
185
8.50k
  } else if(pathlen == 4) {
186
4.88k
    oid[0] = path[0];
187
4.88k
    oid[1] = path[1];
188
4.88k
    oid[2] = path[2];
189
4.88k
    oid[3] = path[3];
190
4.88k
  }
191
192
16.0k
  return 0;
193
16.2k
}
194
195
int mscfs_lookup_local(mscfs_t* fs, const int id, msc_id* objectId)
196
7
{
197
7
  u8* oid = objectId->id;
198
7
  oid[0] = fs->currentPath[0];
199
7
  oid[1] = fs->currentPath[1];
200
7
  oid[2] = (id >> 8) & 0xFF;
201
7
  oid[3] = id & 0xFF;
202
7
  return 0;
203
7
}
204
205
/* -1 any, 0 DF, 1 EF */
206
int mscfs_check_selection(mscfs_t *fs, int requiredItem)
207
9.41k
{
208
9.41k
  if(fs->currentPath[0] == 0 && fs->currentPath[1] == 0)
209
0
    return MSCFS_INVALID_ARGS;
210
9.41k
  if(requiredItem == 1 && fs->currentFile[0] == 0 && fs->currentFile[1] == 0)
211
0
    return MSCFS_INVALID_ARGS;
212
9.41k
  return 0;
213
9.41k
}
214
215
int mscfs_loadFileInfo(mscfs_t* fs, const u8 *path, size_t pathlen, mscfs_file_t **file_data, int* idx)
216
20.2k
{
217
20.2k
  msc_id fullPath = {{0, 0, 0, 0}};
218
20.2k
  int x, rc;
219
20.2k
  assert(fs != NULL && path != NULL && file_data != NULL);
220
20.2k
  rc = mscfs_lookup_path(fs, path, pathlen, &fullPath, 0);
221
20.2k
  if (rc != SC_SUCCESS) {
222
4.19k
    return rc;
223
4.19k
  }
224
225
  /* Obtain file information while checking if it exists */
226
16.0k
  rc = mscfs_check_cache(fs);
227
16.0k
  if (rc < 0)
228
192
    return rc;
229
15.8k
  if(idx) *idx = -1;
230
1.25M
  for(x = 0; x < fs->cache.size; x++) {
231
1.24M
    *file_data = &fs->cache.array[x];
232
1.24M
    if (*file_data) {
233
1.24M
      msc_id objectId;
234
1.24M
      objectId = (*file_data)->objectId;
235
1.24M
      if(0 == memcmp(objectId.id, fullPath.id, 4)) {
236
5.96k
        if (idx)
237
5.72k
          *idx = x;
238
5.96k
        break;
239
5.96k
      }
240
1.24M
      *file_data = NULL;
241
1.24M
    }
242
1.24M
  }
243
15.8k
  if(*file_data == NULL && (0 == memcmp("\x3F\x00\x00\x00", fullPath.id, 4) || 0 == memcmp("\x3F\x00\x50\x15", fullPath.id, 4 ) || 0 == memcmp("\x3F\x00\x3F\x00", fullPath.id, 4))) {
244
5.40k
    static mscfs_file_t ROOT_FILE;
245
5.40k
    ROOT_FILE.ef = 0;
246
5.40k
    ROOT_FILE.size = 0;
247
    /* Faked Root ID */
248
5.40k
    ROOT_FILE.objectId = rootId;
249
250
5.40k
    ROOT_FILE.read = 0;
251
5.40k
    ROOT_FILE.write = 0x02; /* User Pin access */
252
5.40k
    ROOT_FILE.delete = 0x02;
253
254
5.40k
    *file_data = &ROOT_FILE;
255
5.40k
    if(idx) *idx = -2;
256
10.4k
  } else if(*file_data == NULL) {
257
4.51k
    return MSCFS_FILE_NOT_FOUND;
258
4.51k
  }
259
260
11.3k
  return 0;
261
15.8k
}