Coverage Report

Created: 2026-11-12 06:34

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