Coverage Report

Created: 2025-10-10 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/card-gemsafeV1.c
Line
Count
Source
1
/*
2
 * This library is free software; you can redistribute it and/or
3
 * modify it under the terms of the GNU Lesser General Public
4
 * License as published by the Free Software Foundation; either
5
 * version 2.1 of the License, or (at your option) any later version.
6
 *
7
 * This library is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10
 * Lesser General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU Lesser General Public
13
 * License along with this library; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
*/
16
17
/* Initially written by David Mattes (david.mattes@boeing.com) */
18
/* Portuguese eID card support by Joao Poupino (joao.poupino@ist.utl.pt) */
19
20
#ifdef HAVE_CONFIG_H
21
#include "config.h"
22
#endif
23
24
#include <stdlib.h>
25
#include <string.h>
26
27
#include "internal.h"
28
#include "asn1.h"
29
#include "cardctl.h"
30
31
0
#define GEMSAFEV1_ALG_REF_FREEFORM  0x12
32
0
#define GEMSAFEV3_ALG_REF_FREEFORM  0x02
33
#define GEMSAFEV3_ALG_REF_SHA1    0x12
34
0
#define GEMSAFEV3_ALG_REF_SHA256  0x42
35
#define MAX_RESP_BUFFER_SIZE 2048
36
37
static struct sc_card_operations gemsafe_ops;
38
static struct sc_card_operations *iso_ops = NULL;
39
40
static struct sc_card_driver gemsafe_drv = {
41
  "Gemalto GemSafe V1 applet",
42
  "gemsafeV1",
43
  &gemsafe_ops,
44
  NULL, 0, NULL
45
};
46
47
/* Known ATRs */
48
static const struct sc_atr_table gemsafe_atrs[] = {
49
  /* standard version */
50
    {"3B:7B:94:00:00:80:65:B0:83:01:01:74:83:00:90:00", NULL, NULL, SC_CARD_TYPE_GEMSAFEV1_GENERIC, 0, NULL},
51
    {"3B:6B:00:00:80:65:B0:83:01:01:74:83:00:90:00", NULL, NULL, SC_CARD_TYPE_GEMSAFEV1_GENERIC, 0, NULL},
52
    /* GemSafeXpresso 32K */
53
    {"3b:6d:00:00:80:31:80:65:b0:83:01:02:90:83:00:90:00", NULL, NULL, SC_CARD_TYPE_GEMSAFEV1_GENERIC, 0, NULL},
54
    /* fips 140 version */
55
    {"3B:6B:00:00:80:65:B0:83:01:03:74:83:00:90:00", NULL, NULL, SC_CARD_TYPE_GEMSAFEV1_GENERIC, 0, NULL},
56
    /* Undefined */
57
    {"3B:7A:94:00:00:80:65:A2:01:01:01:3D:72:D6:43", NULL, NULL, SC_CARD_TYPE_GEMSAFEV1_GENERIC, 0, NULL},
58
    {"3B:7D:94:00:00:80:31:80:65:B0:83:01:01:90:83:00:90:00", NULL, NULL, SC_CARD_TYPE_GEMSAFEV1_GENERIC, 0, NULL},
59
    {"3B:7D:96:00:00:80:31:80:65:B0:83:11:48:C8:83:00:90:00", NULL, NULL, SC_CARD_TYPE_GEMSAFEV1_GENERIC, 0, NULL},
60
    /* Portuguese eID cards */
61
    {"3B:7D:95:00:00:80:31:80:65:B0:83:11:C0:A9:83:00", NULL, NULL, SC_CARD_TYPE_GEMSAFEV1_PTEID, 0, NULL},
62
    {"3B:7D:95:00:00:80:31:80:65:B0:83:11:C0:A9:83:00:90:00", NULL, NULL, SC_CARD_TYPE_GEMSAFEV1_PTEID, 0, NULL},
63
    {"3B:7D:95:00:00:80:31:80:65:B0:83:11:00:C8:83:00", NULL, NULL, SC_CARD_TYPE_GEMSAFEV1_PTEID, 0, NULL},
64
    {"3B:7D:95:00:00:80:31:80:65:B0:83:11:00:C8:83:00:90:00", NULL, NULL, SC_CARD_TYPE_GEMSAFEV1_PTEID, 0, NULL},
65
    {"3B:FF:96:00:00:81:31:80:43:80:31:80:65:B0:85:03:00:EF:12:0F:FF:82:90:00:67", NULL, NULL, SC_CARD_TYPE_GEMSAFEV1_PTEID, 0, NULL},
66
    {"3B:FF:96:00:00:81:31:FE:43:80:31:80:65:B0:85:04:01:20:12:0F:FF:82:90:00:D0", NULL, NULL, SC_CARD_TYPE_GEMSAFEV1_PTEID, 0, NULL},
67
    /* Swedish eID card */
68
    {"3B:7D:96:00:00:80:31:80:65:B0:83:11:00:C8:83:00:90:00", NULL, NULL, SC_CARD_TYPE_GEMSAFEV1_SEEID, 0, NULL},
69
    /* European Patent Office epoline card*/
70
    {"3b:7d:96:00:00:80:31:80:65:b0:83:02:01:f3:83:00:90:00", NULL, NULL, SC_CARD_TYPE_GEMSAFEV1_SEEID, 0, NULL},
71
    {NULL, NULL, NULL, 0, 0, NULL}
72
};
73
74
static const u8 gemsafe_def_aid[] = {0xA0, 0x00, 0x00, 0x00, 0x18, 0x0A,
75
  0x00, 0x00, 0x01, 0x63, 0x42, 0x00};
76
77
static const u8 gemsafe_pteid_aid[] = {0x60, 0x46, 0x32, 0xFF, 0x00, 0x00, 0x02};
78
79
static const u8 gemsafe_seeid_aid[] = {0xA0, 0x00, 0x00, 0x00, 0x18, 0x0C,
80
                                       0x00, 0x00, 0x01, 0x63, 0x42, 0x00};
81
82
/*
83
static const u8 gemsafe_def_aid[] = {0xA0, 0x00, 0x00, 0x00, 0x63, 0x50,
84
  0x4B, 0x43, 0x53, 0x2D, 0x31, 0x35};
85
*/
86
87
typedef struct gemsafe_exdata_st {
88
  u8  aid[16];
89
  size_t  aid_len;
90
} gemsafe_exdata;
91
92
static int get_conf_aid(sc_card_t *card, u8 *aid, size_t *len)
93
0
{
94
0
  sc_context_t    *ctx = card->ctx;
95
0
  scconf_block    *conf_block, **blocks;
96
0
  int     i;
97
0
  const char    *str_aid;
98
99
0
  SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE);
100
101
0
  conf_block = NULL;
102
0
  for (i = 0; ctx->conf_blocks[i] != NULL; i++) {
103
0
    blocks = scconf_find_blocks(ctx->conf, ctx->conf_blocks[i],
104
0
            "card", "gemsafeV1");
105
0
    if (blocks != NULL && blocks[0] != NULL)
106
0
      conf_block = blocks[0];
107
0
    free(blocks);
108
0
  }
109
110
0
  if (!conf_block) {
111
0
    sc_log(ctx,  "no card specific options configured, trying default AID\n");
112
0
    return SC_ERROR_INTERNAL;
113
0
  }
114
115
0
  str_aid = scconf_get_str(conf_block, "aid", NULL);
116
0
  if (!str_aid) {
117
0
    sc_log(ctx,  "no aid configured, trying default AID\n");
118
0
    return SC_ERROR_INTERNAL;
119
0
  }
120
0
  return sc_hex_to_bin(str_aid, aid, len);
121
0
}
122
123
static int gemsafe_match_card(sc_card_t *card)
124
14
{
125
14
  int i;
126
127
14
  i = _sc_match_atr(card, gemsafe_atrs, &card->type);
128
14
  if (i < 0)
129
14
    return 0;
130
131
0
  return 1;
132
14
}
133
134
static int gemsafe_init(struct sc_card *card)
135
0
{
136
0
  int r;
137
0
  gemsafe_exdata *exdata = NULL;
138
139
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
140
141
0
  card->name = "GemSAFE V1";
142
0
  card->cla  = 0x00;
143
144
0
  exdata = (gemsafe_exdata *)calloc(1, sizeof(gemsafe_exdata));
145
0
  if (!exdata)
146
0
    return SC_ERROR_OUT_OF_MEMORY;
147
0
  exdata->aid_len = sizeof(exdata->aid);
148
0
  if(card->type == SC_CARD_TYPE_GEMSAFEV1_GENERIC) {
149
    /* try to get a AID from the config file */
150
0
    r = get_conf_aid(card, exdata->aid, &exdata->aid_len);
151
0
    if (r < 0) {
152
      /* failed, use default value */
153
0
      memcpy(exdata->aid, gemsafe_def_aid, sizeof(gemsafe_def_aid));
154
0
      exdata->aid_len = sizeof(gemsafe_def_aid);
155
0
    }
156
0
  } else if (card->type == SC_CARD_TYPE_GEMSAFEV1_PTEID) {
157
0
    memcpy(exdata->aid, gemsafe_pteid_aid, sizeof(gemsafe_pteid_aid));
158
0
    exdata->aid_len = sizeof(gemsafe_pteid_aid);
159
0
  } else if (card->type == SC_CARD_TYPE_GEMSAFEV1_SEEID) {
160
0
    memcpy(exdata->aid, gemsafe_seeid_aid, sizeof(gemsafe_seeid_aid));
161
0
    exdata->aid_len = sizeof(gemsafe_seeid_aid);
162
0
  }
163
164
  /* increase lock_count here to prevent sc_unlock to select
165
   * applet twice in gp_select_applet */
166
0
  card->lock_count++;
167
  /* SELECT applet */
168
0
  r = iso7816_select_aid(card, exdata->aid, exdata->aid_len, NULL, NULL);
169
0
  if (r < 0) {
170
0
    free(exdata);
171
0
    sc_log(card->ctx,  "applet selection failed\n");
172
0
    return SC_ERROR_INVALID_CARD;
173
0
  }
174
0
  card->lock_count--;
175
176
  /* set the supported algorithm */
177
0
  unsigned long flags;
178
179
0
  flags  = SC_ALGORITHM_RSA_PAD_PKCS1;
180
0
  flags |= SC_ALGORITHM_RSA_PAD_ISO9796;
181
0
  flags |= SC_ALGORITHM_ONBOARD_KEY_GEN;
182
0
  flags |= SC_ALGORITHM_RSA_HASH_NONE;
183
184
  /* GemSAFE V3 cards support SHA256 */
185
0
  if (card->type == SC_CARD_TYPE_GEMSAFEV1_PTEID ||
186
0
      card->type == SC_CARD_TYPE_GEMSAFEV1_SEEID)
187
0
    flags |= SC_ALGORITHM_RSA_HASH_SHA256;
188
189
0
  _sc_card_add_rsa_alg(card,  512, flags, 0);
190
0
  _sc_card_add_rsa_alg(card,  768, flags, 0);
191
0
  _sc_card_add_rsa_alg(card, 1024, flags, 0);
192
0
  _sc_card_add_rsa_alg(card, 2048, flags, 0);
193
0
  _sc_card_add_rsa_alg(card, 3072, flags, 0);
194
0
  _sc_card_add_rsa_alg(card, 4096, flags, 0);
195
196
  /* fake algorithm to persuade register_mechanisms()
197
   * to register these hashes */
198
0
  if (card->type == SC_CARD_TYPE_GEMSAFEV1_PTEID ||
199
0
      card->type == SC_CARD_TYPE_GEMSAFEV1_SEEID) {
200
0
    flags  = SC_ALGORITHM_RSA_HASH_SHA1;
201
0
    flags |= SC_ALGORITHM_RSA_HASH_MD5;
202
0
    flags |= SC_ALGORITHM_RSA_HASH_MD5_SHA1;
203
0
    flags |= SC_ALGORITHM_RSA_HASH_RIPEMD160;
204
205
0
    _sc_card_add_rsa_alg(card,  512, flags, 0);
206
0
  }
207
208
0
  card->caps |= SC_CARD_CAP_ISO7816_PIN_INFO;
209
0
  card->drv_data = exdata;
210
211
0
  return SC_SUCCESS;
212
0
}
213
214
static int gemsafe_finish(sc_card_t *card)
215
0
{
216
0
  gemsafe_exdata *exdata = (gemsafe_exdata *)card->drv_data;
217
218
0
  if (exdata)
219
0
    free(exdata);
220
0
  return SC_SUCCESS;
221
0
}
222
223
static int gemsafe_select_file(struct sc_card *card, const struct sc_path *path,
224
     struct sc_file **file_out)
225
0
{
226
  /* so far just call the iso select file (but this will change) */
227
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
228
229
0
  return iso_ops->select_file(card, path, file_out);
230
0
}
231
232
static int gemsafe_sc2acl(sc_file_t *file, unsigned ops, u8 sc_byte)
233
0
{
234
0
  int r;
235
0
  unsigned int meth = 0;
236
237
0
  if (sc_byte == 0xff) {
238
0
    r = sc_file_add_acl_entry(file, ops, SC_AC_NEVER, 0);
239
0
    return r;
240
0
  }
241
0
  if (sc_byte == 0x00) {
242
0
    r = sc_file_add_acl_entry(file, ops, SC_AC_NONE, 0);
243
0
    return r;
244
0
  }
245
246
  /* XXX: OR combination of access rights are currently not supported
247
   * hence ignored */
248
0
  if (sc_byte & 0x40)
249
0
    meth |= SC_AC_PRO;
250
0
  if (sc_byte & 0x20)
251
0
    meth |= SC_AC_AUT | SC_AC_TERM;
252
0
  if (sc_byte & 0x10)
253
0
    meth |= SC_AC_CHV;
254
255
0
  return sc_file_add_acl_entry(file, ops, meth, sc_byte & 0x0f);
256
0
}
257
258
static int gemsafe_setacl(sc_card_t *card, sc_file_t *file, const u8 *data,
259
  int is_df)
260
0
{
261
0
  int       r;
262
0
  u8        cond;
263
0
  const u8 *p = data + 1;
264
0
  struct sc_context *ctx = card->ctx;
265
266
0
  if (is_df) {
267
0
    if (*data & 0x04) /* CREATE DF */
268
0
      cond = *p++;
269
0
    else
270
0
      cond = 0xff;
271
0
    sc_log(ctx,
272
0
      "DF security byte CREATE DF: %02x\n", cond);
273
0
    r = gemsafe_sc2acl(file, SC_AC_OP_CREATE, cond);
274
0
    if (r < 0)
275
0
      return r;
276
0
    if (*data & 0x02) /* CREATE EF */
277
0
      cond = *p;
278
0
    else
279
0
      cond = 0xff;
280
0
    sc_log(ctx,
281
0
      "DF security byte CREATE EF: %02x\n", cond);
282
    /* XXX: opensc doesn't currently separate access conditions for
283
     * CREATE EF and CREATE DF, this should be changed */
284
0
    r = gemsafe_sc2acl(file, SC_AC_OP_CREATE, cond);
285
0
    if (r < 0)
286
0
      return r;
287
0
  } else {
288
    /* XXX: ACTIVATE FILE and DEACTIVATE FILE ac are currently not
289
     * supported => ignore them */
290
0
    if (*data & 0x02) /* UPDATE BINARY, ERASE BINARY */
291
0
      cond = *p++;
292
0
    else
293
0
      cond = 0xff;
294
0
    sc_log(ctx,
295
0
      "EF security byte UPDATE/ERASE BINARY: %02x\n", cond);
296
0
    r = gemsafe_sc2acl(file, SC_AC_OP_UPDATE, cond);
297
0
    if (r < 0)
298
0
      return r;
299
0
    r = gemsafe_sc2acl(file, SC_AC_OP_WRITE, cond);
300
0
    if (r < 0)
301
0
      return r;
302
0
    r = gemsafe_sc2acl(file, SC_AC_OP_ERASE, cond);
303
0
    if (r < 0)
304
0
      return r;
305
0
    if (*data & 0x01) /* READ BINARY */
306
0
      cond = *p;
307
0
    else
308
0
      cond = 0xff;
309
0
    sc_log(ctx,
310
0
      "EF security byte READ BINARY: %02x\n", cond);
311
0
    r = gemsafe_sc2acl(file, SC_AC_OP_READ, cond);
312
0
    if (r < 0)
313
0
      return r;
314
0
  }
315
316
0
  return SC_SUCCESS;
317
0
}
318
319
static int gemsafe_process_fci(struct sc_card *card, struct sc_file *file,
320
  const u8 *buf, size_t len)
321
0
{
322
0
  int        r;
323
0
  size_t     tlen;
324
0
  const u8   *tag = NULL, *p = buf;
325
0
  const char *type;
326
0
  struct sc_context *ctx = card->ctx;
327
328
0
  SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE);
329
330
0
  r = iso_ops->process_fci(card, file, buf, len);
331
0
  if (r < 0)
332
0
    return r;
333
0
  sc_log(ctx,
334
0
    "processing GemSAFE V1 specific FCI information\n");
335
336
337
0
  tag = sc_asn1_find_tag(ctx, p, len, 0x82, &tlen);
338
0
  if (!tag) {
339
    /* no FDB => we have a DF */
340
0
    type = "DF";
341
0
    file->type = SC_FILE_TYPE_DF;
342
0
  } else {
343
0
    type = "EF";
344
0
    file->type = SC_FILE_TYPE_WORKING_EF;
345
0
  }
346
347
0
  sc_log(ctx,  "file type: %s\n", type);
348
349
0
  tag = sc_asn1_find_tag(ctx, p, len, 0x8C, &tlen);
350
0
  if (tag) {
351
0
    r = gemsafe_setacl(card, file, tag, strcmp(type, "DF") ? 0 : 1);
352
0
    if (r < 0) {
353
0
      sc_log(ctx,  "unable to set ACL\n");
354
0
      return SC_ERROR_INTERNAL;
355
0
    }
356
0
  } else
357
0
    sc_log(ctx,  "error: AM and SC bytes missing\n");
358
359
0
  return SC_SUCCESS;
360
0
}
361
362
static u8 gemsafe_flags2algref(struct sc_card *card, const struct sc_security_env *env)
363
0
{
364
0
  u8 ret = 0;
365
366
0
  if (env->operation == SC_SEC_OPERATION_SIGN) {
367
0
    if (env->algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA256)
368
0
      ret = GEMSAFEV3_ALG_REF_SHA256;
369
0
    else if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01)
370
0
      ret = (card->type == SC_CARD_TYPE_GEMSAFEV1_PTEID ||
371
0
             card->type == SC_CARD_TYPE_GEMSAFEV1_SEEID) ?
372
0
            GEMSAFEV3_ALG_REF_FREEFORM :
373
0
            GEMSAFEV1_ALG_REF_FREEFORM;
374
0
    else if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_ISO9796)
375
0
      ret = 0x11;
376
0
  } else if (env->operation == SC_SEC_OPERATION_DECIPHER) {
377
0
    if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02)
378
0
      ret = (card->type == SC_CARD_TYPE_GEMSAFEV1_PTEID ||
379
0
             card->type == SC_CARD_TYPE_GEMSAFEV1_SEEID) ?
380
0
            GEMSAFEV3_ALG_REF_FREEFORM :
381
0
            GEMSAFEV1_ALG_REF_FREEFORM;
382
0
  }
383
384
0
  return ret;
385
0
}
386
387
static int gemsafe_restore_security_env(struct sc_card *card, int se_num)
388
0
{
389
0
  int r;
390
0
  struct sc_apdu apdu;
391
392
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
393
394
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x22, 0x73, (u8) se_num);
395
396
0
  r = sc_transmit_apdu(card, &apdu);
397
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
398
399
0
  return sc_check_sw(card, apdu.sw1, apdu.sw2);
400
0
}
401
402
403
static int gemsafe_set_security_env(struct sc_card *card,
404
            const struct sc_security_env *env,
405
            int se_num)
406
0
{
407
0
  u8 alg_ref;
408
0
  struct sc_security_env se_env = *env;
409
0
  struct sc_context *ctx = card->ctx;
410
411
0
  SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE);
412
413
0
  if (!(se_env.flags & SC_SEC_ENV_ALG_REF_PRESENT)) {
414
    /* set the algorithm reference */
415
0
    alg_ref = gemsafe_flags2algref(card, &se_env);
416
0
    if (alg_ref) {
417
0
      se_env.algorithm_ref = alg_ref;
418
0
      se_env.flags |= SC_SEC_ENV_ALG_REF_PRESENT;
419
0
    }
420
0
  }
421
0
  if (!(se_env.flags & SC_SEC_ENV_ALG_REF_PRESENT))
422
0
    sc_log(ctx, "unknown algorithm flags '%lx'\n", se_env.algorithm_flags);
423
424
0
  se_env.flags &= ~SC_SEC_ENV_FILE_REF_PRESENT;
425
0
  return iso_ops->set_security_env(card, &se_env, se_num);
426
0
}
427
428
static int gemsafe_compute_signature(struct sc_card *card, const u8 * data,
429
  size_t data_len, u8 * out, size_t outlen)
430
0
{
431
0
  int r;
432
0
  size_t len;
433
0
  struct sc_apdu apdu;
434
0
  u8 rbuf[MAX_RESP_BUFFER_SIZE];
435
0
  u8 sbuf[MAX_RESP_BUFFER_SIZE];
436
0
  sc_context_t *ctx = card->ctx;
437
438
0
  SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE);
439
440
  /* the card can sign 36 bytes of free form data */
441
0
  if (data_len > 36) {
442
0
    sc_log(ctx,
443
0
       "error: input data too long: %"SC_FORMAT_LEN_SIZE_T"u bytes\n",
444
0
       data_len);
445
0
    return SC_ERROR_INVALID_ARGUMENTS;
446
0
  }
447
448
  /* the Portuguese eID card requires a two-phase exchange */
449
  /* and so does the Swedish one */
450
0
  if(card->type == SC_CARD_TYPE_GEMSAFEV1_PTEID ||
451
0
     card->type == SC_CARD_TYPE_GEMSAFEV1_SEEID) {
452
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x2A, 0x90, 0xA0);
453
0
  } else {
454
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x2A, 0x9E, 0xAC);
455
0
    apdu.cla |= 0x80;
456
0
    apdu.resp = rbuf;
457
0
    apdu.resplen = sizeof(rbuf);
458
0
    apdu.le      = 256;
459
0
  }
460
  /* we sign a digestInfo object => tag 0x90 */
461
0
  sbuf[0] = 0x90;
462
0
  sbuf[1] = (u8)data_len;
463
0
  memcpy(sbuf + 2, data, data_len);
464
0
  apdu.data = sbuf;
465
0
  apdu.lc   = data_len + 2;
466
0
  apdu.datalen = data_len + 2;
467
468
0
  r = sc_transmit_apdu(card, &apdu);
469
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
470
0
  if (apdu.sw1 == 0x90 && apdu.sw2 == 0x00) {
471
0
    if(card->type == SC_CARD_TYPE_GEMSAFEV1_PTEID ||
472
0
       card->type == SC_CARD_TYPE_GEMSAFEV1_SEEID) {
473
      /* finalize the exchange */
474
0
      sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0x2A, 0x9E, 0x9A);
475
0
      apdu.le = 128; /* 1024 bit keys */
476
0
      apdu.resp = rbuf;
477
0
      apdu.resplen = sizeof(rbuf);
478
0
      if(card->type == SC_CARD_TYPE_GEMSAFEV1_SEEID) {
479
        /* cla 0x80 not supported */
480
0
        apdu.cla = 0x00;
481
0
      }
482
0
      r = sc_transmit_apdu(card, &apdu);
483
0
      LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
484
0
      if(apdu.sw1 != 0x90 || apdu.sw2 != 0x00)
485
0
        SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, sc_check_sw(card, apdu.sw1, apdu.sw2));
486
0
    }
487
0
    len = apdu.resplen > outlen ? outlen : apdu.resplen;
488
489
0
    memcpy(out, apdu.resp, len);
490
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, (int)len);
491
0
  }
492
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, sc_check_sw(card, apdu.sw1, apdu.sw2));
493
0
}
494
495
static int gemsafe_decipher(struct sc_card *card, const u8 * crgram,
496
  size_t crgram_len, u8 *out, size_t outlen)
497
0
{
498
0
  int r;
499
0
  struct sc_apdu apdu;
500
0
  u8 rbuf[MAX_RESP_BUFFER_SIZE];
501
0
  sc_context_t *ctx = card->ctx;
502
503
0
  SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE);
504
0
  if (crgram_len > 255)
505
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_ARGUMENTS);
506
507
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x2A, 0x80, 0x84);
508
0
  apdu.cla |= 0x80;
509
0
  apdu.resp = rbuf;
510
0
  apdu.resplen = sizeof(rbuf);
511
0
  apdu.le      = crgram_len;
512
513
0
  apdu.data = crgram;
514
0
  apdu.lc   = crgram_len;
515
0
  apdu.datalen = crgram_len;
516
0
  r = sc_transmit_apdu(card, &apdu);
517
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
518
0
  if (apdu.sw1 == 0x90 && apdu.sw2 == 0x00) {
519
0
    size_t len = apdu.resplen > outlen ? outlen : apdu.resplen;
520
521
0
    memcpy(out, apdu.resp, len);
522
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, (int)len);
523
0
  }
524
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, sc_check_sw(card, apdu.sw1, apdu.sw2));
525
0
}
526
527
static int gemsafe_get_challenge(sc_card_t *card, u8 *rnd, size_t len)
528
0
{
529
0
  int prev_cla, r;
530
531
0
  prev_cla = card->cla;
532
0
  if(card->type == SC_CARD_TYPE_GEMSAFEV1_PTEID) {
533
    /* Warning: this depends on iso7816_get_challenge not
534
     * changing the value of the card's CLA
535
     */
536
0
    card->cla = 0x80;
537
0
  }
538
0
  r = iso_ops->get_challenge(card, rnd, len);
539
  /* Restore the CLA value if needed */
540
0
  if(card->cla != prev_cla)
541
0
    card->cla = prev_cla;
542
543
0
  return r;
544
0
}
545
546
static int gemsafe_card_reader_lock_obtained(sc_card_t *card, int was_reset)
547
0
{
548
0
  int r = SC_SUCCESS;
549
0
  gemsafe_exdata *exdata = (gemsafe_exdata *)card->drv_data;
550
551
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
552
553
0
  if (was_reset > 0 && exdata) {
554
0
    r = iso7816_select_aid(card, exdata->aid, exdata->aid_len, NULL, NULL);
555
0
  }
556
557
0
  LOG_FUNC_RETURN(card->ctx, r);
558
0
}
559
560
static int gemsafe_logout(sc_card_t *card)
561
0
{
562
0
  gemsafe_exdata *exdata = (gemsafe_exdata *)card->drv_data;
563
564
0
  return iso7816_select_aid(card, exdata->aid, exdata->aid_len, NULL, NULL);
565
0
}
566
567
static struct sc_card_driver *sc_get_driver(void)
568
273
{
569
273
  struct sc_card_driver *iso_drv = sc_get_iso7816_driver();
570
273
  if (!iso_ops)
571
1
    iso_ops = iso_drv->ops;
572
  /* use the standard iso operations as default */
573
273
  gemsafe_ops = *iso_drv->ops;
574
  /* gemsafe specific functions */
575
273
  gemsafe_ops.match_card  = gemsafe_match_card;
576
273
  gemsafe_ops.init  = gemsafe_init;
577
273
  gemsafe_ops.finish  = gemsafe_finish;
578
273
  gemsafe_ops.select_file = gemsafe_select_file;
579
273
  gemsafe_ops.restore_security_env = gemsafe_restore_security_env;
580
273
  gemsafe_ops.set_security_env     = gemsafe_set_security_env;
581
273
  gemsafe_ops.decipher             = gemsafe_decipher;
582
273
  gemsafe_ops.compute_signature    = gemsafe_compute_signature;
583
273
  gemsafe_ops.get_challenge      = gemsafe_get_challenge;
584
273
  gemsafe_ops.process_fci = gemsafe_process_fci;
585
273
  gemsafe_ops.pin_cmd    = iso_ops->pin_cmd;
586
273
  gemsafe_ops.card_reader_lock_obtained = gemsafe_card_reader_lock_obtained;
587
273
  gemsafe_ops.logout = gemsafe_logout;
588
589
273
  return &gemsafe_drv;
590
273
}
591
592
struct sc_card_driver *sc_get_gemsafeV1_driver(void)
593
273
{
594
273
  return sc_get_driver();
595
273
}
596