Coverage Report

Created: 2026-11-30 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/card-muscle.c
Line
Count
Source
1
/*
2
 * card-muscle.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 <stdlib.h>
26
#include <string.h>
27
28
#include "internal.h"
29
#include "cardctl.h"
30
#include "muscle.h"
31
#include "muscle-filesystem.h"
32
#include "types.h"
33
#include "opensc.h"
34
35
static struct sc_card_operations muscle_ops;
36
static const struct sc_card_operations *iso_ops = NULL;
37
38
static struct sc_card_driver muscle_drv = {
39
  "MuscleApplet",
40
  "muscle",
41
  &muscle_ops,
42
  NULL, 0, NULL
43
};
44
45
static const struct sc_atr_table muscle_atrs[] = {
46
  /* Tyfone JCOP 242R2 cards */
47
  { "3b:6d:00:00:ff:54:79:66:6f:6e:65:20:32:34:32:52:32", NULL, NULL, SC_CARD_TYPE_MUSCLE_JCOP242R2_NO_EXT_APDU, 0, NULL },
48
  /* Aladdin eToken PRO USB 72K Java */
49
  { "3b:d5:18:00:81:31:3a:7d:80:73:c8:21:10:30", NULL, NULL, SC_CARD_TYPE_MUSCLE_ETOKEN_72K, 0, NULL },
50
  /* JCOP31 v2.4.1 contact interface */
51
  { "3b:f8:13:00:00:81:31:fe:45:4a:43:4f:50:76:32:34:31:b7", NULL, NULL, SC_CARD_TYPE_MUSCLE_JCOP241, 0, NULL },
52
  /* JCOP31 v2.4.1 RF interface */
53
  { "3b:88:80:01:4a:43:4f:50:76:32:34:31:5e", NULL, NULL, SC_CARD_TYPE_MUSCLE_JCOP241, 0, NULL },
54
  { NULL, NULL, NULL, 0, 0, NULL }
55
};
56
57
926
#define MUSCLE_DATA(card) ( (muscle_private_t*)card->drv_data )
58
2.58k
#define MUSCLE_FS(card) ( ((muscle_private_t*)card->drv_data)->fs )
59
typedef struct muscle_private {
60
  sc_security_env_t env;
61
  unsigned short verifiedPins;
62
  mscfs_t *fs;
63
  int rsa_key_ref;
64
65
} muscle_private_t;
66
67
static int muscle_finish(sc_card_t *card)
68
463
{
69
463
  muscle_private_t *priv = MUSCLE_DATA(card);
70
463
  if (priv) {
71
463
    mscfs_free(priv->fs);
72
463
    free(priv);
73
463
    card->drv_data = NULL;
74
463
  }
75
463
  return 0;
76
463
}
77
78
79
static u8 muscleAppletId[] = { 0xA0, 0x00,0x00,0x00, 0x01, 0x01 };
80
81
static int muscle_match_card(sc_card_t *card)
82
6.01k
{
83
6.01k
  sc_apdu_t apdu;
84
6.01k
  u8 response[64];
85
6.01k
  int r;
86
87
6.01k
  if (msc_select_applet(card, muscleAppletId, sizeof muscleAppletId) == 1) {
88
    /* Muscle applet is present, check the protocol version to be sure */
89
463
    sc_format_apdu(card, &apdu, SC_APDU_CASE_2, 0x3C, 0x00, 0x00);
90
463
    apdu.cla = 0xB0;
91
463
    apdu.le = 64;
92
463
    apdu.resplen = 64;
93
463
    apdu.resp = response;
94
463
    r = sc_transmit_apdu(card, &apdu);
95
463
    if (r == SC_SUCCESS && apdu.resplen > 1 && response[0] == 0x01) {
96
1
      card->type = SC_CARD_TYPE_MUSCLE_V1;
97
462
    } else {
98
462
      card->type = SC_CARD_TYPE_MUSCLE_GENERIC;
99
462
    }
100
463
    return 1;
101
463
  }
102
5.55k
  return 0;
103
6.01k
}
104
105
/* Since Musclecard has a different ACL system then PKCS15
106
 * objects need to have their READ/UPDATE/DELETE permissions mapped for files
107
 * and directory ACLS need to be set
108
 * For keys.. they have different ACLS, but are accessed in different locations, so it shouldn't be an issue here
109
 */
110
static unsigned short muscle_parse_singleAcl(const sc_acl_entry_t* acl)
111
0
{
112
0
  unsigned short acl_entry = 0;
113
0
  while(acl) {
114
0
    int key = acl->key_ref;
115
0
    int method = acl->method;
116
0
    switch(method) {
117
0
    case SC_AC_NEVER:
118
0
      return 0xFFFF;
119
    /* Ignore... other items overwrite these */
120
0
    case SC_AC_NONE:
121
0
    case SC_AC_UNKNOWN:
122
0
      break;
123
0
    case SC_AC_CHV:
124
      /* Ignore shift with more bits that acl_entry has */
125
0
      if ((size_t) key < sizeof(acl_entry) * 8)
126
0
        acl_entry |= (1u << key); /* Assuming key 0 == SO */
127
0
      break;
128
0
    case SC_AC_AUT:
129
0
    case SC_AC_TERM:
130
0
    case SC_AC_PRO:
131
0
    default:
132
      /* Ignored */
133
0
      break;
134
0
    }
135
0
    acl = acl->next;
136
0
  }
137
0
  return acl_entry;
138
0
}
139
140
static void muscle_parse_acls(const sc_file_t* file, unsigned short* read_perm, unsigned short* write_perm, unsigned short* delete_perm)
141
0
{
142
0
  if (read_perm == NULL || write_perm == NULL || delete_perm == NULL)
143
0
    return;
144
0
  *read_perm =  muscle_parse_singleAcl(sc_file_get_acl_entry(file, SC_AC_OP_READ));
145
0
  *write_perm =  muscle_parse_singleAcl(sc_file_get_acl_entry(file, SC_AC_OP_UPDATE));
146
0
  *delete_perm =  muscle_parse_singleAcl(sc_file_get_acl_entry(file, SC_AC_OP_DELETE));
147
0
}
148
149
static int muscle_create_directory(sc_card_t *card, sc_file_t *file)
150
0
{
151
0
  mscfs_t *fs = MUSCLE_FS(card);
152
0
  msc_id objectId;
153
0
  u8* oid = objectId.id;
154
0
  unsigned id = file->id;
155
0
  unsigned short read_perm = 0, write_perm = 0, delete_perm = 0;
156
0
  size_t objectSize;
157
0
  int r;
158
0
  if(id == 0) /* No null name files */
159
0
    return SC_ERROR_INVALID_ARGUMENTS;
160
161
  /* No nesting directories */
162
0
  if(fs->currentPath[0] != 0x3F || fs->currentPath[1] != 0x00)
163
0
    return SC_ERROR_NOT_SUPPORTED;
164
0
  oid[0] = ((id & 0xFF00) >> 8) & 0xFF;
165
0
  oid[1] = id & 0xFF;
166
0
  oid[2] = oid[3] = 0;
167
168
0
  objectSize = file->size;
169
170
0
  muscle_parse_acls(file, &read_perm, &write_perm, &delete_perm);
171
0
  r = msc_create_object(card, objectId, objectSize, read_perm, write_perm, delete_perm);
172
0
  mscfs_clear_cache(fs);
173
0
  if(r >= 0) return 0;
174
0
  return r;
175
0
}
176
177
178
static int muscle_create_file(sc_card_t *card, sc_file_t *file)
179
0
{
180
0
  mscfs_t *fs = MUSCLE_FS(card);
181
0
  size_t objectSize = file->size;
182
0
  unsigned short read_perm = 0, write_perm = 0, delete_perm = 0;
183
0
  msc_id objectId;
184
0
  int r;
185
0
  if(file->type == SC_FILE_TYPE_DF)
186
0
    return muscle_create_directory(card, file);
187
0
  if(file->type != SC_FILE_TYPE_WORKING_EF)
188
0
    return SC_ERROR_NOT_SUPPORTED;
189
0
  if(file->id == 0) /* No null name files */
190
0
    return SC_ERROR_INVALID_ARGUMENTS;
191
192
0
  muscle_parse_acls(file, &read_perm, &write_perm, &delete_perm);
193
194
0
  mscfs_lookup_local(fs, file->id, &objectId);
195
0
  r = msc_create_object(card, objectId, objectSize, read_perm, write_perm, delete_perm);
196
0
  mscfs_clear_cache(fs);
197
0
  if(r >= 0) return 0;
198
0
  return r;
199
0
}
200
201
static int muscle_read_binary(sc_card_t *card, unsigned int idx, u8* buf, size_t count, unsigned long *flags)
202
291
{
203
291
  mscfs_t *fs = MUSCLE_FS(card);
204
291
  int r;
205
291
  msc_id objectId;
206
291
  u8* oid = objectId.id;
207
291
  mscfs_file_t *file;
208
209
291
  r = mscfs_check_selection(fs, -1);
210
291
  if(r < 0) LOG_FUNC_RETURN(card->ctx, r);
211
291
  file = &fs->cache.array[fs->currentFileIndex];
212
291
  objectId = file->objectId;
213
  /* memcpy(objectId.id, file->objectId.id, 4); */
214
291
  if(!file->ef) {
215
67
    oid[0] = oid[2];
216
67
    oid[1] = oid[3];
217
67
    oid[2] = oid[3] = 0;
218
67
  }
219
291
  r = msc_read_object(card, objectId, idx, buf, count);
220
291
  LOG_FUNC_RETURN(card->ctx, r);
221
291
}
222
223
static int muscle_update_binary(sc_card_t *card, unsigned int idx, const u8* buf, size_t count, unsigned long flags)
224
0
{
225
0
  mscfs_t *fs = MUSCLE_FS(card);
226
0
  int r;
227
0
  mscfs_file_t *file;
228
0
  msc_id objectId;
229
0
  u8* oid = objectId.id;
230
231
0
  r = mscfs_check_selection(fs, -1);
232
0
  if(r < 0) LOG_FUNC_RETURN(card->ctx, r);
233
0
  file = &fs->cache.array[fs->currentFileIndex];
234
235
0
  objectId = file->objectId;
236
  /* memcpy(objectId.id, file->objectId.id, 4); */
237
0
  if(!file->ef) {
238
0
    oid[0] = oid[2];
239
0
    oid[1] = oid[3];
240
0
    oid[2] = oid[3] = 0;
241
0
  }
242
0
  if(file->size < idx + count) {
243
0
    size_t newFileSize = idx + count;
244
0
    u8* buffer = malloc(newFileSize);
245
0
    if(buffer == NULL) LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
246
247
0
    r = msc_read_object(card, objectId, 0, buffer, file->size);
248
    /* TODO: RETRIEVE ACLS */
249
0
    if(r < 0) goto update_bin_free_buffer;
250
0
    r = msc_delete_object(card, objectId, 0);
251
0
    if(r < 0) goto update_bin_free_buffer;
252
0
    r = msc_create_object(card, objectId, newFileSize, 0,0,0);
253
0
    if(r < 0) goto update_bin_free_buffer;
254
0
    memcpy(buffer + idx, buf, count);
255
0
    r = msc_update_object(card, objectId, 0, buffer, newFileSize);
256
0
    if(r < 0) goto update_bin_free_buffer;
257
0
    file->size = newFileSize;
258
0
update_bin_free_buffer:
259
0
    free(buffer);
260
0
    LOG_FUNC_RETURN(card->ctx, r);
261
0
  } else {
262
0
    r = msc_update_object(card, objectId, idx, buf, count);
263
0
  }
264
  /* mscfs_clear_cache(fs); */
265
0
  return r;
266
0
}
267
268
/* TODO: Evaluate correctness */
269
static int muscle_delete_mscfs_file(sc_card_t *card, mscfs_file_t *file_data)
270
0
{
271
0
  mscfs_t *fs = MUSCLE_FS(card);
272
0
  msc_id id = file_data->objectId;
273
0
  u8* oid = id.id;
274
0
  int r;
275
0
  file_data->deleteFile = 1;
276
277
0
  if(!file_data->ef) {
278
0
    int x;
279
0
    mscfs_file_t *childFile;
280
    /* Delete children */
281
0
    r = mscfs_check_cache(fs);
282
0
    if(r < 0) SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r);
283
284
0
    sc_log(card->ctx,
285
0
      "DELETING Children of: %02X%02X%02X%02X\n",
286
0
      oid[0],oid[1],oid[2],oid[3]);
287
0
    for(x = 0; x < fs->cache.size; x++) {
288
0
      msc_id objectId;
289
0
      childFile = &fs->cache.array[x];
290
0
      objectId = childFile->objectId;
291
292
0
      if(0 == memcmp(oid + 2, objectId.id, 2) && !childFile->deleteFile) {
293
0
        sc_log(card->ctx,
294
0
          "DELETING: %02X%02X%02X%02X\n",
295
0
          objectId.id[0],objectId.id[1],
296
0
          objectId.id[2],objectId.id[3]);
297
0
        r = muscle_delete_mscfs_file(card, childFile);
298
0
        if(r < 0) SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE,r);
299
300
0
      }
301
0
    }
302
0
    oid[0] = oid[2];
303
0
    oid[1] = oid[3];
304
0
    oid[2] = oid[3] = 0;
305
    /* ??? objectId = objectId >> 16; */
306
0
  }
307
0
  r = msc_delete_object(card, id, 1);
308
  /* Check if its the root... this file generally is virtual
309
   * So don't return an error if it fails */
310
0
  if((0 == memcmp(oid, "\x3F\x00\x00\x00", 4))
311
0
    || (0 == memcmp(oid, "\x3F\x00\x3F\x00", 4)))
312
0
    return 0;
313
314
0
  if(r < 0) {
315
0
    printf("ID: %02X%02X%02X%02X\n",
316
0
          oid[0],oid[1],oid[2],oid[3]);
317
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE,r);
318
0
  }
319
0
  return 0;
320
0
}
321
322
static int muscle_delete_file(sc_card_t *card, const sc_path_t *path_in)
323
0
{
324
0
  mscfs_t *fs = MUSCLE_FS(card);
325
0
  mscfs_file_t *file_data = NULL;
326
0
  int r = 0;
327
0
  int x;
328
329
0
  if (!path_in)
330
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_ARGUMENTS);
331
332
0
  r = mscfs_loadFileInfo(fs, path_in->value, path_in->len, &file_data, NULL);
333
0
  if(r < 0) SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE,r);
334
0
  for (x = 0; x < fs->cache.size; x++) {
335
0
    mscfs_file_t *file = &fs->cache.array[x];
336
0
    file->deleteFile = 0;
337
0
  }
338
0
  r = muscle_delete_mscfs_file(card, file_data);
339
0
  mscfs_clear_cache(fs);
340
0
  if(r < 0) SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE,r);
341
0
  return 0;
342
0
}
343
344
static void muscle_load_single_acl(sc_file_t* file, int operation, unsigned short acl)
345
1.11k
{
346
1.11k
  int key;
347
  /* Everybody by default.... */
348
1.11k
  sc_file_add_acl_entry(file, operation, SC_AC_NONE, 0);
349
1.11k
  if(acl == 0xFFFF) {
350
108
    sc_file_add_acl_entry(file, operation, SC_AC_NEVER, 0);
351
108
    return;
352
108
  }
353
17.1k
  for(key = 0; key < 16; key++) {
354
16.1k
    if(acl >> key & 1) {
355
5.54k
      sc_file_add_acl_entry(file, operation, SC_AC_CHV, key);
356
5.54k
    }
357
16.1k
  }
358
1.00k
}
359
static void muscle_load_file_acls(sc_file_t* file, mscfs_file_t *file_data)
360
268
{
361
268
  muscle_load_single_acl(file, SC_AC_OP_READ, file_data->read);
362
268
  muscle_load_single_acl(file, SC_AC_OP_WRITE, file_data->write);
363
268
  muscle_load_single_acl(file, SC_AC_OP_UPDATE, file_data->write);
364
268
  muscle_load_single_acl(file, SC_AC_OP_DELETE, file_data->delete);
365
268
}
366
static void muscle_load_dir_acls(sc_file_t* file, mscfs_file_t *file_data)
367
9
{
368
9
  muscle_load_single_acl(file, SC_AC_OP_SELECT, 0);
369
9
  muscle_load_single_acl(file, SC_AC_OP_LIST_FILES, 0);
370
9
  muscle_load_single_acl(file, SC_AC_OP_LOCK, 0xFFFF);
371
9
  muscle_load_single_acl(file, SC_AC_OP_DELETE, file_data->delete);
372
9
  muscle_load_single_acl(file, SC_AC_OP_CREATE, file_data->write);
373
9
}
374
375
/* Required type = -1 for don't care, 1 for EF, 0 for DF */
376
static int select_item(sc_card_t *card, const sc_path_t *path_in, sc_file_t ** file_out, int requiredType)
377
2.29k
{
378
2.29k
  mscfs_t *fs = MUSCLE_FS(card);
379
2.29k
  mscfs_file_t *file_data = NULL;
380
2.29k
  size_t pathlen = path_in->len;
381
2.29k
  int r = 0;
382
2.29k
  int objectIndex;
383
2.29k
  u8* oid;
384
385
2.29k
  r = mscfs_check_cache(fs);
386
2.29k
  if(r < 0) SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r);
387
1.33k
  r = mscfs_loadFileInfo(fs, path_in->value, path_in->len, &file_data, &objectIndex);
388
1.33k
  if(r < 0) SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE,r);
389
390
  /* Check if its the right type */
391
528
  if(requiredType >= 0 && requiredType != file_data->ef) {
392
1
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
393
1
  }
394
527
  oid = file_data->objectId.id;
395
  /* Is it a file or directory */
396
527
  if(file_data->ef) {
397
269
    fs->currentPath[0] = oid[0];
398
269
    fs->currentPath[1] = oid[1];
399
269
    fs->currentFile[0] = oid[2];
400
269
    fs->currentFile[1] = oid[3];
401
269
  } else {
402
258
    if(pathlen < 2) {
403
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
404
0
    }
405
258
    fs->currentPath[0] = oid[pathlen - 2];
406
258
    fs->currentPath[1] = oid[pathlen - 1];
407
258
    fs->currentFile[0] = 0;
408
258
    fs->currentFile[1] = 0;
409
258
  }
410
411
527
  fs->currentFileIndex = objectIndex;
412
527
  if(file_out) {
413
277
    sc_file_t *file;
414
277
    file = sc_file_new();
415
277
    if (!file)
416
0
      SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_OUT_OF_MEMORY);
417
277
    file->path = *path_in;
418
277
    file->size = file_data->size;
419
277
    file->id = (oid[2] << 8) | oid[3];
420
277
    if(!file_data->ef) {
421
9
      file->type = SC_FILE_TYPE_DF;
422
268
    } else {
423
268
      file->type = SC_FILE_TYPE_WORKING_EF;
424
268
      file->ef_structure = SC_FILE_EF_TRANSPARENT;
425
268
    }
426
427
    /* Setup ACLS */
428
277
    if(file_data->ef) {
429
268
      muscle_load_file_acls(file, file_data);
430
268
    } else {
431
9
      muscle_load_dir_acls(file, file_data);
432
      /* Setup directory acls... */
433
9
    }
434
435
277
    file->magic = SC_FILE_MAGIC;
436
277
    *file_out = file;
437
277
  }
438
527
  return 0;
439
527
}
440
441
static int muscle_select_file(sc_card_t *card, const sc_path_t *path_in,
442
           sc_file_t **file_out)
443
2.29k
{
444
2.29k
  int r;
445
446
2.29k
  if (card == NULL || path_in == NULL)
447
0
    return SC_ERROR_INTERNAL;
448
449
2.29k
  switch (path_in->type) {
450
0
  case SC_PATH_TYPE_FILE_ID:
451
0
    r = select_item(card, path_in, file_out, 1);
452
0
    break;
453
899
  case SC_PATH_TYPE_DF_NAME:
454
899
    r = select_item(card, path_in, file_out, 0);
455
899
    break;
456
1.39k
  case SC_PATH_TYPE_PATH:
457
1.39k
    r = select_item(card, path_in, file_out, -1);
458
1.39k
    break;
459
0
  default:
460
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_ARGUMENTS);
461
2.29k
  }
462
2.29k
  if(r > 0) r = 0;
463
2.29k
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE,r);
464
2.29k
}
465
466
static int _listFile(mscfs_file_t *file, int reset, void *udata)
467
27.1k
{
468
27.1k
  int next = reset ? 0x00 : 0x01;
469
27.1k
  return msc_list_objects( (sc_card_t*)udata, next, file);
470
27.1k
}
471
472
static int muscle_init(sc_card_t *card)
473
463
{
474
463
  muscle_private_t *priv;
475
463
  int r;
476
477
463
  card->name = "MuscleApplet";
478
463
  card->drv_data = malloc(sizeof(muscle_private_t));
479
463
  if(!card->drv_data) {
480
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
481
0
  }
482
463
  memset(card->drv_data, 0, sizeof(muscle_private_t));
483
463
  priv = MUSCLE_DATA(card);
484
463
  priv->verifiedPins = 0;
485
463
  priv->fs = mscfs_new();
486
463
  if(!priv->fs) {
487
0
    free(card->drv_data);
488
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
489
0
  }
490
463
  priv->fs->udata = card;
491
463
  priv->fs->listFile = _listFile;
492
493
463
  card->cla = 0xB0;
494
495
463
  card->flags |= SC_CARD_FLAG_RNG;
496
463
  card->caps |= SC_CARD_CAP_RNG;
497
498
  /* Card type detection */
499
463
  r = _sc_match_atr(card, muscle_atrs, &card->type);
500
463
  if (r < 0) {
501
461
    sc_log(card->ctx, "Failed to match the ATRs");
502
461
  }
503
463
  if(card->type == SC_CARD_TYPE_MUSCLE_ETOKEN_72K) {
504
0
    card->caps |= SC_CARD_CAP_APDU_EXT;
505
0
  }
506
463
  if(card->type == SC_CARD_TYPE_MUSCLE_JCOP241) {
507
2
    card->caps |= SC_CARD_CAP_APDU_EXT;
508
2
  }
509
463
  if (!(card->caps & SC_CARD_CAP_APDU_EXT)) {
510
461
    card->max_recv_size = 255;
511
461
    card->max_send_size = 255;
512
461
  }
513
463
  if(card->type == SC_CARD_TYPE_MUSCLE_JCOP242R2_NO_EXT_APDU) {
514
          /* Tyfone JCOP v242R2 card that doesn't support extended APDUs */
515
0
  }
516
517
518
  /* FIXME: Card type detection */
519
463
  if (1) {
520
463
    unsigned long flags;
521
522
463
    flags = SC_ALGORITHM_RSA_RAW;
523
463
    flags |= SC_ALGORITHM_RSA_HASH_NONE;
524
463
    flags |= SC_ALGORITHM_ONBOARD_KEY_GEN;
525
526
463
    _sc_card_add_rsa_alg(card, 1024, flags, 0);
527
463
    _sc_card_add_rsa_alg(card, 2048, flags, 0);
528
463
  }
529
463
  return SC_SUCCESS;
530
463
}
531
532
static int muscle_list_files(sc_card_t *card, u8 *buf, size_t bufLen)
533
0
{
534
0
  muscle_private_t* priv = MUSCLE_DATA(card);
535
0
  mscfs_t *fs = priv->fs;
536
0
  int x, r;
537
0
  int count = 0;
538
539
0
  r = mscfs_check_cache(priv->fs);
540
0
  if(r < 0) SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r);
541
542
0
  for(x = 0; x < fs->cache.size; x++) {
543
0
    u8* oid = fs->cache.array[x].objectId.id;
544
0
    if (bufLen < 2)
545
0
      break;
546
0
    sc_log(card->ctx,
547
0
      "FILE: %02X%02X%02X%02X\n",
548
0
      oid[0],oid[1],oid[2],oid[3]);
549
0
    if(0 == memcmp(fs->currentPath, oid, 2)) {
550
0
      buf[0] = oid[2];
551
0
      buf[1] = oid[3];
552
0
      if(buf[0] == 0x00 && buf[1] == 0x00) continue; /* No directories/null names outside of root */
553
0
      buf += 2;
554
0
      count += 2;
555
0
      bufLen -= 2;
556
0
    }
557
0
  }
558
0
  return count;
559
0
}
560
561
static int muscle_pin_cmd(sc_card_t *card, struct sc_pin_cmd_data *cmd)
562
0
{
563
0
  muscle_private_t* priv = MUSCLE_DATA(card);
564
0
  const int bufferLength = MSC_MAX_PIN_COMMAND_LENGTH;
565
0
  u8 buffer[MSC_MAX_PIN_COMMAND_LENGTH];
566
567
0
  if (!priv || !cmd)
568
0
    return SC_ERROR_INVALID_ARGUMENTS;
569
570
0
  switch(cmd->cmd) {
571
0
  case SC_PIN_CMD_VERIFY:
572
0
    switch(cmd->pin_type) {
573
0
    case SC_AC_CHV: {
574
0
      sc_apdu_t apdu;
575
0
      int r;
576
0
      r = msc_verify_pin_apdu(card, &apdu, buffer, bufferLength, cmd->pin_reference, cmd->pin1.data, cmd->pin1.len);
577
0
      if (r < 0)
578
0
        return r;
579
0
      cmd->apdu = &apdu;
580
0
      cmd->pin1.offset = 5;
581
0
      r = iso_ops->pin_cmd(card, cmd);
582
0
      if (r >= 0 && cmd->pin_reference < 16)
583
0
        priv->verifiedPins |= (1 << cmd->pin_reference);
584
0
      return r;
585
0
    }
586
0
    case SC_AC_TERM:
587
0
    case SC_AC_PRO:
588
0
    case SC_AC_AUT:
589
0
    case SC_AC_NONE:
590
0
    default:
591
0
      sc_log(card->ctx,  "Unsupported authentication method\n");
592
0
      return SC_ERROR_NOT_SUPPORTED;
593
0
    }
594
0
  case SC_PIN_CMD_CHANGE:
595
0
    switch(cmd->pin_type) {
596
0
    case SC_AC_CHV: {
597
0
      sc_apdu_t apdu;
598
0
      int r;
599
0
      r = msc_change_pin_apdu(card, &apdu, buffer, bufferLength, cmd->pin_reference, cmd->pin1.data, cmd->pin1.len, cmd->pin2.data, cmd->pin2.len);
600
0
      if (r < 0)
601
0
        return r;
602
0
      cmd->apdu = &apdu;
603
0
      return iso_ops->pin_cmd(card, cmd);
604
0
    }
605
0
    case SC_AC_TERM:
606
0
    case SC_AC_PRO:
607
0
    case SC_AC_AUT:
608
0
    case SC_AC_NONE:
609
0
    default:
610
0
      sc_log(card->ctx,  "Unsupported authentication method\n");
611
0
      return SC_ERROR_NOT_SUPPORTED;
612
0
    }
613
0
  case SC_PIN_CMD_UNBLOCK:
614
0
  switch(cmd->pin_type) {
615
0
    case SC_AC_CHV: {
616
0
      sc_apdu_t apdu;
617
0
      int r;
618
0
      r = msc_unblock_pin_apdu(card, &apdu, buffer, bufferLength, cmd->pin_reference, cmd->pin1.data, cmd->pin1.len);
619
0
      if (r < 0)
620
0
        return r;
621
0
      cmd->apdu = &apdu;
622
0
      return iso_ops->pin_cmd(card, cmd);
623
0
    }
624
0
    case SC_AC_TERM:
625
0
    case SC_AC_PRO:
626
0
    case SC_AC_AUT:
627
0
    case SC_AC_NONE:
628
0
    default:
629
0
      sc_log(card->ctx,  "Unsupported authentication method\n");
630
0
      return SC_ERROR_NOT_SUPPORTED;
631
0
    }
632
0
  default:
633
0
    sc_log(card->ctx,  "Unsupported command\n");
634
0
    return SC_ERROR_NOT_SUPPORTED;
635
636
0
  }
637
638
0
}
639
640
static int muscle_card_extract_key(sc_card_t *card, sc_cardctl_muscle_key_info_t *info)
641
0
{
642
  /* CURRENTLY DONT SUPPORT EXTRACTING PRIVATE KEYS... */
643
0
  switch(info->keyType) {
644
0
  case 1: /* RSA */
645
0
    return msc_extract_rsa_public_key(card,
646
0
      info->keyLocation,
647
0
      &info->modLength,
648
0
      &info->modValue,
649
0
      &info->expLength,
650
0
      &info->expValue);
651
0
  default:
652
0
    return SC_ERROR_NOT_SUPPORTED;
653
0
  }
654
0
}
655
656
static int muscle_card_import_key(sc_card_t *card, sc_cardctl_muscle_key_info_t *info)
657
0
{
658
  /* CURRENTLY DONT SUPPORT EXTRACTING PRIVATE KEYS... */
659
0
  switch(info->keyType) {
660
0
  case 0x02: /* RSA_PRIVATE */
661
0
  case 0x03: /* RSA_PRIVATE_CRT */
662
0
    return msc_import_key(card,
663
0
      info->keyLocation,
664
0
      info);
665
0
  default:
666
0
    return SC_ERROR_NOT_SUPPORTED;
667
0
  }
668
0
}
669
670
static int muscle_card_generate_key(sc_card_t *card, sc_cardctl_muscle_gen_key_info_t *info)
671
0
{
672
0
  return msc_generate_keypair(card,
673
0
    info->privateKeyLocation,
674
0
    info->publicKeyLocation,
675
0
    info->keyType,
676
0
    info->keySize,
677
0
    0);
678
0
}
679
680
static int muscle_card_verified_pins(sc_card_t *card, sc_cardctl_muscle_verified_pins_info_t *info)
681
0
{
682
0
  muscle_private_t* priv = MUSCLE_DATA(card);
683
0
  info->verifiedPins = priv->verifiedPins;
684
0
  return 0;
685
0
}
686
static int muscle_card_ctl(sc_card_t *card, unsigned long request, void *data)
687
1
{
688
1
  switch(request) {
689
0
  case SC_CARDCTL_MUSCLE_GENERATE_KEY:
690
0
    return muscle_card_generate_key(card, (sc_cardctl_muscle_gen_key_info_t*) data);
691
0
  case SC_CARDCTL_MUSCLE_EXTRACT_KEY:
692
0
    return muscle_card_extract_key(card, (sc_cardctl_muscle_key_info_t*) data);
693
0
  case SC_CARDCTL_MUSCLE_IMPORT_KEY:
694
0
    return muscle_card_import_key(card, (sc_cardctl_muscle_key_info_t*) data);
695
0
  case SC_CARDCTL_MUSCLE_VERIFIED_PINS:
696
0
    return muscle_card_verified_pins(card, (sc_cardctl_muscle_verified_pins_info_t*) data);
697
1
  default:
698
1
    return SC_ERROR_NOT_SUPPORTED; /* Unsupported.. whatever it is */
699
1
  }
700
1
}
701
702
static int muscle_set_security_env(sc_card_t *card,
703
         const sc_security_env_t *env,
704
         int se_num)
705
0
{
706
0
  muscle_private_t* priv = MUSCLE_DATA(card);
707
708
0
  if (!priv || !env)
709
0
    return SC_ERROR_INVALID_ARGUMENTS;
710
711
0
  if (env->operation != SC_SEC_OPERATION_SIGN &&
712
0
      env->operation != SC_SEC_OPERATION_DECIPHER) {
713
0
    sc_log(card->ctx,  "Invalid crypto operation supplied.\n");
714
0
    return SC_ERROR_NOT_SUPPORTED;
715
0
  }
716
0
  if (env->algorithm != SC_ALGORITHM_RSA) {
717
0
    sc_log(card->ctx,  "Invalid crypto algorithm supplied.\n");
718
0
    return SC_ERROR_NOT_SUPPORTED;
719
0
  }
720
  /* ADJUST FOR PKCS1 padding support for decryption only */
721
0
  if ((env->algorithm_flags & SC_ALGORITHM_RSA_PADS) ||
722
0
      (env->algorithm_flags & SC_ALGORITHM_RSA_HASHES)) {
723
0
    sc_log(card->ctx,  "Card supports only raw RSA.\n");
724
0
    return SC_ERROR_NOT_SUPPORTED;
725
0
  }
726
0
  if (env->flags & SC_SEC_ENV_KEY_REF_PRESENT) {
727
0
    if (env->key_ref_len != 1 ||
728
0
        (env->key_ref[0] > 0x0F)) {
729
0
      sc_log(card->ctx,  "Invalid key reference supplied.\n");
730
0
      return SC_ERROR_NOT_SUPPORTED;
731
0
    }
732
0
    priv->rsa_key_ref = env->key_ref[0];
733
0
  }
734
0
  if (env->flags & SC_SEC_ENV_ALG_REF_PRESENT) {
735
0
    sc_log(card->ctx,  "Algorithm reference not supported.\n");
736
0
    return SC_ERROR_NOT_SUPPORTED;
737
0
  }
738
  /* if (env->flags & SC_SEC_ENV_FILE_REF_PRESENT)
739
    if (memcmp(env->file_ref.value, "\x00\x12", 2) != 0) {
740
      sc_log(card->ctx,  "File reference is not 0012.\n");
741
      return SC_ERROR_NOT_SUPPORTED;
742
    } */
743
0
  priv->env = *env;
744
0
  return 0;
745
0
}
746
747
static int muscle_restore_security_env(sc_card_t *card, int se_num)
748
0
{
749
0
  muscle_private_t* priv = MUSCLE_DATA(card);
750
0
  memset(&priv->env, 0, sizeof(priv->env));
751
0
  return 0;
752
0
}
753
754
755
static int muscle_decipher(sc_card_t * card,
756
       const u8 * crgram, size_t crgram_len, u8 * out,
757
       size_t out_len)
758
0
{
759
0
  muscle_private_t* priv = MUSCLE_DATA(card);
760
761
0
  u8 key_id;
762
0
  int r;
763
764
0
  if (!priv || !crgram || !out)
765
0
    return SC_ERROR_INVALID_ARGUMENTS;
766
767
  /* sanity check */
768
0
  if (priv->env.operation != SC_SEC_OPERATION_DECIPHER)
769
0
    return SC_ERROR_INVALID_ARGUMENTS;
770
771
0
  key_id = priv->rsa_key_ref * 2; /* Private key */
772
773
0
  if (out_len < crgram_len) {
774
0
    sc_log(card->ctx,  "Output buffer too small");
775
0
    return SC_ERROR_BUFFER_TOO_SMALL;
776
0
  }
777
778
0
  r = msc_compute_crypt(card,
779
0
    key_id,
780
0
    0x00, /* RSA NO PADDING */
781
0
    0x04, /* decrypt */
782
0
    crgram,
783
0
    out,
784
0
    crgram_len,
785
0
    out_len);
786
0
  LOG_TEST_RET(card->ctx, r, "Card signature failed");
787
0
  return r;
788
0
}
789
790
static int muscle_compute_signature(sc_card_t *card, const u8 *data,
791
    size_t data_len, u8 * out, size_t outlen)
792
0
{
793
0
  muscle_private_t* priv = MUSCLE_DATA(card);
794
0
  u8 key_id;
795
0
  int r;
796
797
0
  if (!priv || !data || !out)
798
0
    return SC_ERROR_INVALID_ARGUMENTS;
799
800
0
  key_id = priv->rsa_key_ref * 2; /* Private key */
801
802
0
  if (outlen < data_len) {
803
0
    sc_log(card->ctx,  "Output buffer too small");
804
0
    return SC_ERROR_BUFFER_TOO_SMALL;
805
0
  }
806
807
0
  r = msc_compute_crypt(card,
808
0
    key_id,
809
0
    0x00, /* RSA NO PADDING */
810
0
    0x04, /* -- decrypt raw... will do what we need since signing isn't yet supported */
811
0
    data,
812
0
    out,
813
0
    data_len,
814
0
    outlen);
815
0
  LOG_TEST_RET(card->ctx, r, "Card signature failed");
816
0
  return r;
817
0
}
818
819
static int muscle_get_challenge(sc_card_t *card, u8 *rnd, size_t len)
820
0
{
821
0
  if (len == 0)
822
0
    return SC_SUCCESS;
823
0
  else {
824
0
    LOG_TEST_RET(card->ctx,
825
0
        msc_get_challenge(card, len, 0, NULL, rnd),
826
0
        "GET CHALLENGE cmd failed");
827
0
    return (int) len;
828
0
  }
829
0
}
830
831
27.0k
static int muscle_check_sw(sc_card_t * card, unsigned int sw1, unsigned int sw2) {
832
27.0k
  if(sw1 == 0x9C) {
833
152
    switch(sw2) {
834
13
      case 0x01: /* SW_NO_MEMORY_LEFT */
835
13
        return SC_ERROR_NOT_ENOUGH_MEMORY;
836
11
      case 0x02: /* SW_AUTH_FAILED */
837
11
        return SC_ERROR_PIN_CODE_INCORRECT;
838
10
      case 0x03: /* SW_OPERATION_NOT_ALLOWED */
839
10
        return SC_ERROR_NOT_ALLOWED;
840
10
      case 0x05: /* SW_UNSUPPORTED_FEATURE */
841
10
        return SC_ERROR_NO_CARD_SUPPORT;
842
10
      case 0x06: /* SW_UNAUTHORIZED */
843
10
        return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED;
844
12
      case 0x07: /* SW_OBJECT_NOT_FOUND */
845
12
        return SC_ERROR_FILE_NOT_FOUND;
846
10
      case 0x08: /* SW_OBJECT_EXISTS */
847
10
        return SC_ERROR_FILE_ALREADY_EXISTS;
848
10
      case 0x09: /* SW_INCORRECT_ALG */
849
10
        return SC_ERROR_INCORRECT_PARAMETERS;
850
10
      case 0x0B: /* SW_SIGNATURE_INVALID */
851
10
        return SC_ERROR_CARD_CMD_FAILED;
852
12
      case 0x0C: /* SW_IDENTITY_BLOCKED */
853
12
        return SC_ERROR_AUTH_METHOD_BLOCKED;
854
11
      case 0x0F: /* SW_INVALID_PARAMETER */
855
21
      case 0x10: /* SW_INCORRECT_P1 */
856
34
      case 0x11: /* SW_INCORRECT_P2 */
857
34
        return SC_ERROR_INCORRECT_PARAMETERS;
858
152
    }
859
152
  }
860
26.8k
  return iso_ops->check_sw(card, sw1, sw2);
861
27.0k
}
862
863
static int muscle_card_reader_lock_obtained(sc_card_t *card, int was_reset)
864
6.94k
{
865
6.94k
  int r = SC_SUCCESS;
866
867
6.94k
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
868
869
6.94k
  if (was_reset > 0) {
870
0
    if (msc_select_applet(card, muscleAppletId, sizeof muscleAppletId) != 1) {
871
0
      r = SC_ERROR_INVALID_CARD;
872
0
    }
873
0
  }
874
875
6.94k
  LOG_FUNC_RETURN(card->ctx, r);
876
6.94k
}
877
878
static int muscle_logout(sc_card_t *card)
879
0
{
880
0
  int r = SC_ERROR_NOT_SUPPORTED;
881
882
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
883
884
0
  if (msc_select_applet(card, muscleAppletId, sizeof muscleAppletId) == 1) {
885
0
    r = SC_SUCCESS;
886
0
  }
887
888
0
  LOG_FUNC_RETURN(card->ctx, r);
889
0
}
890
891
892
static struct sc_card_driver * sc_get_driver(void)
893
14.4k
{
894
14.4k
  struct sc_card_driver *iso_drv = sc_get_iso7816_driver();
895
14.4k
  if (iso_ops == NULL)
896
1
    iso_ops = iso_drv->ops;
897
898
14.4k
  muscle_ops = *iso_drv->ops;
899
14.4k
  muscle_ops.check_sw = muscle_check_sw;
900
14.4k
  muscle_ops.pin_cmd = muscle_pin_cmd;
901
14.4k
  muscle_ops.match_card = muscle_match_card;
902
14.4k
  muscle_ops.init = muscle_init;
903
14.4k
  muscle_ops.finish = muscle_finish;
904
905
14.4k
  muscle_ops.get_challenge = muscle_get_challenge;
906
907
14.4k
  muscle_ops.set_security_env = muscle_set_security_env;
908
14.4k
  muscle_ops.restore_security_env = muscle_restore_security_env;
909
14.4k
  muscle_ops.compute_signature = muscle_compute_signature;
910
14.4k
  muscle_ops.decipher = muscle_decipher;
911
14.4k
  muscle_ops.card_ctl = muscle_card_ctl;
912
14.4k
  muscle_ops.read_binary = muscle_read_binary;
913
14.4k
  muscle_ops.update_binary = muscle_update_binary;
914
14.4k
  muscle_ops.create_file = muscle_create_file;
915
14.4k
  muscle_ops.select_file = muscle_select_file;
916
14.4k
  muscle_ops.delete_file = muscle_delete_file;
917
14.4k
  muscle_ops.list_files = muscle_list_files;
918
14.4k
  muscle_ops.card_reader_lock_obtained = muscle_card_reader_lock_obtained;
919
14.4k
  muscle_ops.logout = muscle_logout;
920
921
14.4k
  return &muscle_drv;
922
14.4k
}
923
924
struct sc_card_driver * sc_get_muscle_driver(void)
925
14.4k
{
926
14.4k
  return sc_get_driver();
927
14.4k
}