Coverage Report

Created: 2026-12-03 06:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/card-entersafe.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 Weitao Sun (weitao@ftsafe.com) 2008 */
18
19
#ifdef HAVE_CONFIG_H
20
#include "config.h"
21
#endif
22
#ifdef ENABLE_OPENSSL /* empty file without openssl */
23
24
#include <stdlib.h>
25
#include <string.h>
26
27
#include <openssl/evp.h>
28
29
#include "internal.h"
30
#include "asn1.h"
31
#include "cardctl.h"
32
33
static const struct sc_atr_table entersafe_atrs[] = {
34
  {
35
    "3b:0f:00:65:46:53:05:19:05:71:df:00:00:00:00:00:00",
36
    "ff:ff:ff:ff:ff:ff:ff:00:ff:ff:ff:00:00:00:00:00:00",
37
    "ePass3000", SC_CARD_TYPE_ENTERSAFE_3K, 0, NULL },
38
  {
39
    "3b:9f:95:81:31:fe:9f:00:65:46:53:05:30:06:71:df:00:00:00:80:6a:82:5e",
40
    "FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:00:FF:FF:FF:FF:FF:FF:00:00:00:00",
41
    "FTCOS/PK-01C", SC_CARD_TYPE_ENTERSAFE_FTCOS_PK_01C, 0, NULL },
42
  {
43
    "3b:fc:18:00:00:81:31:80:45:90:67:46:4a:00:64:18:14:00:00:00:00:02",
44
    "ff:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:ff:ff:ff:ff:00",
45
    "EJAVA/PK-01C", SC_CARD_TYPE_ENTERSAFE_EJAVA_PK_01C, 0, NULL },
46
  {
47
    "3b:7c:18:00:00:90:67:46:4a:20:28:8c:58:00:00:00:00",
48
    "ff:00:00:00:00:ff:ff:ff:ff:00:00:00:00:ff:ff:ff:ff",
49
    "EJAVA/PK-01C-T0",SC_CARD_TYPE_ENTERSAFE_EJAVA_PK_01C_T0,0,NULL},
50
  {
51
    "3B:FC:18:00:00:81:31:80:45:90:67:46:4A:21:28:8C:58:00:00:00:00:B7",
52
    "ff:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:ff:ff:ff:ff:00",
53
    "EJAVA/H10CR/PK-01C-T1",SC_CARD_TYPE_ENTERSAFE_EJAVA_H10CR_PK_01C_T1,0,NULL},
54
  {
55
    "3B:FC:18:00:00:81:31:80:45:90:67:46:4A:20:25:c3:30:00:00:00:00",
56
    "ff:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:00:00:00:00",
57
    "EJAVA/D11CR/PK-01C-T1",SC_CARD_TYPE_ENTERSAFE_EJAVA_D11CR_PK_01C_T1,0,NULL},
58
  {
59
    "3B:FC:18:00:00:81:31:80:45:90:67:46:4A:00:6A:04:24:00:00:00:00:20",
60
    "ff:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:ff:ff:ff:ff:00",
61
    "EJAVA/C21C/PK-01C-T1",SC_CARD_TYPE_ENTERSAFE_EJAVA_C21C_PK_01C_T1,0,NULL},
62
  {
63
    "3B:FC:18:00:00:81:31:80:45:90:67:46:4A:00:68:08:04:00:00:00:00:0E",
64
    "ff:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:ff:ff:ff:ff:00",
65
    "EJAVA/A22CR/PK-01C-T1",SC_CARD_TYPE_ENTERSAFE_EJAVA_A22CR_PK_01C_T1,0,NULL},
66
  {
67
    "3B:FC:18:00:00:81:31:80:45:90:67:46:4A:10:27:61:30:00:00:00:00:0C",
68
    "ff:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:ff:ff:ff:ff:00",
69
    "EJAVA/A40CR/PK-01C-T1",SC_CARD_TYPE_ENTERSAFE_EJAVA_A40CR_PK_01C_T1,0,NULL},
70
  {
71
    "3b:fc:18:00:00:81:31:80:45:90:67:46:4a:00:68:08:06:00:00:00:00:0c",
72
    "FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:00:FF:FF:FF:FF:FF:FF:00:00:00",
73
    "FTCOS/PK-01C", SC_CARD_TYPE_ENTERSAFE_FTCOS_PK_01C, 0, NULL },
74
  { NULL, NULL, NULL, 0, 0, NULL }
75
};
76
77
static struct sc_card_operations entersafe_ops;
78
static struct sc_card_operations *iso_ops = NULL;
79
80
static struct sc_card_driver entersafe_drv = {
81
  "entersafe",
82
  "entersafe",
83
  &entersafe_ops,
84
  NULL, 0, NULL
85
};
86
87
static u8 trans_code_3k[] =
88
{
89
  0x01, 0x02, 0x03, 0x04,
90
  0x05, 0x06, 0x07, 0x08,
91
};
92
93
static u8 trans_code_ftcos_pk_01c[] =
94
{
95
  0x92, 0x34, 0x2E, 0xEF,
96
  0x23, 0x40, 0x4F, 0xD1,
97
};
98
99
static u8 init_key[] =
100
{
101
  1,  2,  3,  4,
102
  5,  6,  7,  8,
103
  9,  10, 11, 12,
104
  13, 14, 15, 16,
105
};
106
107
static u8 key_maintain[] =
108
{
109
  0x12, 0x34, 0x56, 0x78,
110
  0x21, 0x43, 0x65, 0x87,
111
  0x11, 0x22, 0xaa, 0xbb,
112
  0x33, 0x44, 0xcd, 0xef
113
};
114
115
static void entersafe_reverse_buffer(u8* buff, size_t size)
116
0
{
117
0
  u8 t;
118
0
  u8 *end = buff + size - 1;
119
120
0
  while (buff < end) {
121
0
    t = *buff;
122
0
    *buff = *end;
123
0
    *end = t;
124
0
    ++buff;
125
0
    --end;
126
0
  }
127
0
}
128
129
static int entersafe_select_file(sc_card_t *card,
130
                 const sc_path_t *in_path,
131
                 sc_file_t **file_out);
132
133
/* the entersafe part */
134
static int entersafe_match_card(sc_card_t *card)
135
10.1k
{
136
10.1k
  int i;
137
10.1k
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
138
139
10.1k
  i = _sc_match_atr(card, entersafe_atrs, &card->type);
140
10.1k
  if (i < 0)
141
9.96k
    return 0;
142
143
153
  return 1;
144
10.1k
}
145
146
static int entersafe_init(sc_card_t *card)
147
153
{
148
153
  unsigned int flags;
149
150
153
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
151
152
153
  card->name = "entersafe";
153
153
  card->cla  = 0x00;
154
153
  card->drv_data = NULL;
155
156
153
  flags = SC_ALGORITHM_ONBOARD_KEY_GEN | SC_ALGORITHM_RSA_RAW | SC_ALGORITHM_RSA_HASH_NONE;
157
158
153
  _sc_card_add_rsa_alg(card, 512, flags, 0);
159
153
  _sc_card_add_rsa_alg(card, 768, flags, 0);
160
153
  _sc_card_add_rsa_alg(card, 1024, flags, 0);
161
153
  _sc_card_add_rsa_alg(card, 2048, flags, 0);
162
163
153
  card->caps = SC_CARD_CAP_RNG;
164
165
  /* we need read_binary&friends with max 224 bytes per read */
166
153
  card->max_send_size = 224;
167
153
  card->max_recv_size = 224;
168
153
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_SUCCESS);
169
153
}
170
171
static int
172
entersafe_finish(sc_card_t *card)
173
153
{
174
153
  if (card->drv_data) {
175
0
    free(card->drv_data);
176
0
    card->drv_data = NULL;
177
0
  }
178
153
  return SC_SUCCESS;
179
153
}
180
181
static int entersafe_gen_random(sc_card_t *card, u8 *buff, size_t size)
182
0
{
183
0
  int r = SC_SUCCESS;
184
0
  u8 rbuf[SC_MAX_APDU_BUFFER_SIZE] = {0};
185
0
  sc_apdu_t apdu;
186
187
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
188
189
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0x84, 0x00, 0x00);
190
0
  apdu.resp = rbuf;
191
0
  apdu.le = size;
192
0
  apdu.resplen = sizeof(rbuf);
193
194
0
  r = sc_transmit_apdu(card, &apdu);
195
0
  LOG_TEST_RET(card->ctx, r, "entersafe gen random failed");
196
197
0
  if (apdu.resplen != size)
198
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
199
0
  memcpy(buff, rbuf, size);
200
201
0
  LOG_FUNC_RETURN(card->ctx, r);
202
0
}
203
204
static int entersafe_cipher_apdu(sc_card_t *card, sc_apdu_t *apdu,
205
                 u8 *key, size_t keylen,
206
                 u8 *buff, size_t buffsize)
207
0
{
208
0
  EVP_CIPHER_CTX *ctx = NULL;
209
0
  EVP_CIPHER *alg = NULL;
210
211
0
  u8 iv[8] = {0};
212
0
  int len;
213
214
0
  if (card == NULL || apdu == NULL || key == NULL || buff == NULL)
215
0
    return SC_ERROR_INTERNAL;
216
217
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
218
219
  /* padding as 0x80 0x00 0x00...... */
220
0
  memset(buff, 0, buffsize);
221
0
  buff[0] = apdu->lc;
222
0
  memcpy(buff + 1, apdu->data, apdu->lc);
223
0
  buff[apdu->lc + 1] = 0x80;
224
225
0
  ctx = EVP_CIPHER_CTX_new();
226
0
  if (ctx == NULL) {
227
0
    sc_log_openssl(card->ctx);
228
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
229
0
  }
230
0
  EVP_CIPHER_CTX_set_padding(ctx, 0);
231
232
0
  if (keylen == 8) {
233
0
    alg = sc_evp_cipher(card->ctx, "DES-ECB");
234
0
  } else if (keylen == 16) {
235
0
    alg = sc_evp_cipher(card->ctx, "DES-EDE");
236
0
  } else {
237
0
    EVP_CIPHER_CTX_free(ctx);
238
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
239
0
  }
240
241
0
  if (EVP_EncryptInit_ex(ctx, alg, NULL, key, iv) != 1) {
242
0
    sc_log_openssl(card->ctx);
243
0
    sc_evp_cipher_free(alg);
244
0
    EVP_CIPHER_CTX_free(ctx);
245
0
    sc_log(card->ctx, "entersafe encryption error.");
246
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
247
0
  }
248
249
0
  len = (int)apdu->lc;
250
0
  if (!EVP_EncryptUpdate(ctx, buff, &len, buff, (int)buffsize)) {
251
0
    sc_log_openssl(card->ctx);
252
0
    sc_evp_cipher_free(alg);
253
0
    EVP_CIPHER_CTX_free(ctx);
254
0
    sc_log(card->ctx, "entersafe encryption error.");
255
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
256
0
  }
257
0
  apdu->lc = len;
258
259
0
  sc_evp_cipher_free(alg);
260
0
  EVP_CIPHER_CTX_free(ctx);
261
262
0
  if (apdu->lc != buffsize) {
263
0
    sc_log(card->ctx, "entersafe build cipher apdu failed.");
264
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INTERNAL);
265
0
  }
266
267
0
  apdu->data = buff;
268
0
  apdu->datalen = apdu->lc;
269
270
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_SUCCESS);
271
0
}
272
273
static int entersafe_mac_apdu(sc_card_t *card, sc_apdu_t *apdu,
274
                u8 * key,size_t keylen,
275
                u8 * buff,size_t buffsize)
276
0
{
277
0
  int r;
278
0
  u8 iv[8];
279
0
  u8 *tmp = NULL, *tmp_rounded = NULL;
280
0
  size_t tmpsize = 0, tmpsize_rounded = 0;
281
0
  int outl = 0;
282
0
  EVP_CIPHER_CTX *ctx = NULL;
283
0
  EVP_CIPHER *alg = NULL;
284
285
0
  if (card == NULL || apdu == NULL || key == NULL || buff == NULL)
286
0
    return SC_ERROR_INTERNAL;
287
288
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
289
290
0
  if (apdu->cse != SC_APDU_CASE_3_SHORT)
291
0
    return SC_ERROR_INTERNAL;
292
0
  if (keylen != 8 && keylen != 16)
293
0
    return SC_ERROR_INTERNAL;
294
295
0
  r = entersafe_gen_random(card, iv, sizeof(iv));
296
0
  LOG_TEST_RET(card->ctx, r, "entersafe gen random failed");
297
298
  /* encode the APDU in the buffer */
299
0
  if ((r = sc_apdu_get_octets(card->ctx, apdu, &tmp, &tmpsize, SC_PROTO_RAW)) != SC_SUCCESS)
300
0
    goto out;
301
302
  /* round to 8 */
303
0
  tmpsize_rounded = (tmpsize / 8 + 1) * 8;
304
305
0
  tmp_rounded = malloc(tmpsize_rounded);
306
0
  if (tmp_rounded == NULL) {
307
0
    r = SC_ERROR_OUT_OF_MEMORY;
308
0
    goto out;
309
0
  }
310
311
  /* build content and padded buffer by 0x80 0x00 0x00..... */
312
0
  memset(tmp_rounded, 0, tmpsize_rounded);
313
0
  memcpy(tmp_rounded, tmp, tmpsize);
314
0
  tmp_rounded[4] += 4;
315
0
  tmp_rounded[tmpsize] = 0x80;
316
317
  /* block_size-1 blocks*/
318
0
  ctx = EVP_CIPHER_CTX_new();
319
0
  if (ctx == NULL) {
320
0
    r = SC_ERROR_OUT_OF_MEMORY;
321
0
    sc_log_openssl(card->ctx);
322
0
    goto out;
323
0
  }
324
0
  EVP_CIPHER_CTX_set_padding(ctx, 0);
325
0
  alg = sc_evp_cipher(card->ctx, "DES-CBC");
326
0
  if (!alg ||
327
0
      EVP_EncryptInit_ex(ctx, alg, NULL, key, iv) != 1) {
328
0
    r = SC_ERROR_INTERNAL;
329
0
    sc_log_openssl(card->ctx);
330
0
    goto out;
331
0
  }
332
333
0
  if (tmpsize_rounded > 8) {
334
0
    if (!EVP_EncryptUpdate(ctx, tmp_rounded, &outl, tmp_rounded, (int)tmpsize_rounded - 8)) {
335
0
      r = SC_ERROR_INTERNAL;
336
0
      sc_log_openssl(card->ctx);
337
0
      goto out;
338
0
    }
339
0
  }
340
  /* last block */
341
0
  if (keylen == 8) {
342
0
    if (!EVP_EncryptUpdate(ctx, tmp_rounded + outl, &outl, tmp_rounded + outl, 8)) {
343
0
      r = SC_ERROR_INTERNAL;
344
0
      sc_log_openssl(card->ctx);
345
0
      goto out;
346
0
    }
347
0
  } else {
348
0
    if (EVP_EncryptInit_ex(ctx, EVP_des_ede_cbc(), NULL, key, tmp_rounded + outl - 8) != 1 ||
349
0
        !EVP_EncryptUpdate(ctx, tmp_rounded + outl, &outl, tmp_rounded + outl, 8)) {
350
0
      r = SC_ERROR_INTERNAL;
351
0
      sc_log_openssl(card->ctx);
352
0
      goto out;
353
0
    }
354
0
  }
355
356
0
  if (apdu->lc + 4 > buffsize) {
357
0
    r = SC_ERROR_INVALID_DATA;
358
0
    goto out;
359
0
  }
360
361
0
  memcpy(buff, apdu->data, apdu->lc);
362
  /* use first 4 bytes of last block as mac value */
363
0
  memcpy(buff + apdu->lc, tmp_rounded + tmpsize_rounded - 8, 4);
364
0
  apdu->data = buff;
365
0
  apdu->lc += 4;
366
0
  apdu->datalen = apdu->lc;
367
368
0
out:
369
0
  free(tmp);
370
0
  free(tmp_rounded);
371
0
  sc_evp_cipher_free(alg);
372
0
  EVP_CIPHER_CTX_free(ctx);
373
374
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r);
375
0
}
376
377
static int entersafe_transmit_apdu(sc_card_t *card, sc_apdu_t *apdu,
378
                   u8 * key, size_t keylen,
379
                   int cipher, int mac)
380
3.59k
{
381
3.59k
  u8 *cipher_data = NULL, *mac_data = NULL;
382
3.59k
  size_t cipher_data_size, mac_data_size, blocks;
383
3.59k
  int r = SC_SUCCESS;
384
3.59k
  u8 *sbuf = NULL;
385
3.59k
  size_t ssize = 0;
386
387
3.59k
  if (card == NULL || apdu == NULL)
388
0
    return SC_ERROR_INTERNAL;
389
390
3.59k
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
391
392
3.59k
  if ((cipher || mac) && (!key || (keylen != 8 && keylen != 16)))
393
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_ARGUMENTS);
394
395
3.59k
  r = sc_apdu_get_octets(card->ctx, apdu, &sbuf, &ssize, SC_PROTO_RAW);
396
3.59k
  if (r == SC_SUCCESS)
397
3.59k
    sc_apdu_log(card->ctx, sbuf, ssize, 1);
398
3.59k
  if (sbuf)
399
3.59k
    free(sbuf);
400
401
3.59k
  if (cipher) {
402
0
    blocks = (apdu->lc + 2) / 8 + 1;
403
0
    cipher_data_size = blocks * 8;
404
0
    cipher_data = malloc(cipher_data_size);
405
0
    if (!cipher_data) {
406
0
      r = SC_ERROR_OUT_OF_MEMORY;
407
0
      goto out;
408
0
    }
409
410
0
    if ((r = entersafe_cipher_apdu(card, apdu, key, keylen, cipher_data, cipher_data_size)) < 0)
411
0
      goto out;
412
0
  }
413
3.59k
  if (mac) {
414
0
    mac_data_size = apdu->lc + 4;
415
0
    mac_data = malloc(mac_data_size);
416
0
    if (!mac_data) {
417
0
      r = SC_ERROR_OUT_OF_MEMORY;
418
0
      goto out;
419
0
    }
420
0
    r = entersafe_mac_apdu(card, apdu, key, keylen, mac_data, mac_data_size);
421
0
    if (r < 0)
422
0
      goto out;
423
0
  }
424
425
3.59k
  r = sc_transmit_apdu(card, apdu);
426
427
3.59k
out:
428
3.59k
  free(cipher_data);
429
3.59k
  free(mac_data);
430
431
3.59k
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r);
432
3.59k
}
433
434
static int entersafe_read_binary(sc_card_t *card,
435
                 unsigned int idx, u8 *buf, size_t count,
436
                 unsigned long *flags)
437
3.43k
{
438
3.43k
  sc_apdu_t apdu;
439
3.43k
  u8 recvbuf[SC_MAX_APDU_BUFFER_SIZE];
440
3.43k
  int r;
441
442
3.43k
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
443
444
3.43k
  if (count > card->max_recv_size)
445
0
    return SC_ERROR_INTERNAL;
446
3.43k
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xB0, (idx >> 8) & 0xFF, idx & 0xFF);
447
448
3.43k
  apdu.cla = idx > 0x7fff ? 0x80 : 0x00;
449
3.43k
  apdu.le = count;
450
3.43k
  apdu.resplen = count;
451
3.43k
  apdu.resp = recvbuf;
452
453
3.43k
  r = entersafe_transmit_apdu(card, &apdu, 0, 0, 0, 0);
454
3.43k
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
455
3.43k
  if (apdu.resplen == 0)
456
89
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, sc_check_sw(card, apdu.sw1, apdu.sw2));
457
3.34k
  if (apdu.resplen > count)
458
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_WRONG_LENGTH);
459
3.34k
  memcpy(buf, recvbuf, apdu.resplen);
460
461
3.34k
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, (int)apdu.resplen);
462
3.34k
}
463
464
static int entersafe_update_binary(sc_card_t *card,
465
                   unsigned int idx, const u8 *buf,
466
                   size_t count, unsigned long flags)
467
0
{
468
0
  sc_apdu_t apdu;
469
0
  int r;
470
471
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
472
473
0
  if (count > card->max_send_size)
474
0
    return SC_ERROR_INTERNAL;
475
476
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xD6, (idx >> 8) & 0xFF, idx & 0xFF);
477
0
  apdu.cla = idx > 0x7fff ? 0x80 : 0x00;
478
0
  apdu.lc = count;
479
0
  apdu.datalen = count;
480
0
  apdu.data = buf;
481
482
0
  r = entersafe_transmit_apdu(card, &apdu, 0, 0, 0, 0);
483
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
484
0
  LOG_TEST_RET(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2),
485
0
        "Card returned error");
486
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, (int)count);
487
0
}
488
489
static int entersafe_process_fci(struct sc_card *card, struct sc_file *file,
490
                 const u8 *buf, size_t buflen)
491
182
{
492
182
  int r;
493
494
182
  if (file == NULL)
495
0
    return SC_ERROR_INTERNAL;
496
182
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
497
498
182
  r = iso_ops->process_fci(card, file, buf, buflen);
499
182
  LOG_TEST_RET(card->ctx, r, "Process fci failed");
500
501
182
  if (file->namelen) {
502
19
    file->type = SC_FILE_TYPE_DF;
503
19
    file->ef_structure = SC_FILE_EF_UNKNOWN;
504
163
  } else {
505
163
    file->type = SC_FILE_TYPE_WORKING_EF;
506
163
    file->ef_structure = SC_FILE_EF_TRANSPARENT;
507
163
  }
508
509
182
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r);
510
182
}
511
512
static int entersafe_select_fid(sc_card_t *card,
513
                unsigned int id_hi, unsigned int id_lo,
514
                sc_file_t **file_out)
515
538
{
516
538
  int r;
517
538
  sc_file_t *file = NULL;
518
538
  sc_path_t path;
519
520
538
  memset(&path, 0, sizeof(sc_path_t));
521
522
538
  path.type = SC_PATH_TYPE_FILE_ID;
523
538
  path.value[0] = id_hi;
524
538
  path.value[1] = id_lo;
525
538
  path.len = 2;
526
527
538
  r = iso_ops->select_file(card, &path, &file);
528
538
  if (r < 0)
529
236
    sc_file_free(file);
530
538
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
531
532
302
  if (file_out)
533
124
    *file_out = file;
534
178
  else
535
178
    sc_file_free(file);
536
537
302
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_SUCCESS);
538
302
}
539
540
static int entersafe_select_aid(sc_card_t *card,
541
                const sc_path_t *in_path,
542
                sc_file_t **file_out)
543
333
{
544
333
  int r;
545
546
333
  r = iso_ops->select_file(card, in_path, file_out);
547
333
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
548
549
26
  if (file_out) {
550
0
    sc_file_t *file = *file_out;
551
0
    if (file == NULL)
552
0
      return SC_ERROR_INTERNAL;
553
0
    if (in_path->len > sizeof(file->name))
554
0
      return SC_ERROR_INVALID_ARGUMENTS;
555
556
0
    file->type = SC_FILE_TYPE_DF;
557
0
    file->ef_structure = SC_FILE_EF_UNKNOWN;
558
0
    file->path.len = 0;
559
0
    file->size = 0;
560
    /* AID */
561
0
    memcpy(file->name, in_path->value, in_path->len);
562
0
    file->namelen = in_path->len;
563
0
    file->id = 0x0000;
564
0
  }
565
26
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r);
566
26
}
567
568
static int entersafe_select_path(sc_card_t *card,
569
                const u8 pathbuf[16], const size_t len,
570
                sc_file_t **file_out)
571
378
{
572
378
  u8 n_pathbuf[SC_MAX_PATH_SIZE];
573
378
  const u8 *path = pathbuf;
574
378
  size_t pathlen = len;
575
378
  unsigned int i;
576
378
  int r;
577
578
378
  if (pathlen % 2 != 0 || pathlen > 6 || pathlen <= 0)
579
3
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_ARGUMENTS);
580
581
  /* if pathlen == 6 then the first FID must be MF (== 3F00) */
582
375
  if (pathlen == 6 && (path[0] != 0x3f || path[1] != 0x00))
583
2
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_ARGUMENTS);
584
585
  /* unify path (the first FID should be MF) */
586
373
  if (path[0] != 0x3f || path[1] != 0x00) {
587
15
    n_pathbuf[0] = 0x3f;
588
15
    n_pathbuf[1] = 0x00;
589
15
    memcpy(n_pathbuf + 2, path, pathlen);
590
15
    path = n_pathbuf;
591
15
    pathlen += 2;
592
15
  }
593
594
538
  for (i = 0; i < pathlen - 2; i += 2) {
595
383
    r = entersafe_select_fid(card, path[i], path[i + 1], NULL);
596
383
    LOG_TEST_RET(card->ctx, r, "SELECT FILE (DF-ID) failed");
597
383
  }
598
155
  return entersafe_select_fid(card, path[pathlen - 2], path[pathlen - 1], file_out);
599
373
}
600
601
static int entersafe_select_file(sc_card_t *card,
602
                const sc_path_t *in_path,
603
                sc_file_t **file_out)
604
711
{
605
711
  if (card == NULL || in_path == NULL)
606
0
    return SC_ERROR_INTERNAL;
607
711
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
608
609
711
  switch (in_path->type) {
610
0
    case SC_PATH_TYPE_FILE_ID:
611
0
      if (in_path->len != 2)
612
0
        SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_ARGUMENTS);
613
0
      return entersafe_select_fid(card,in_path->value[0], in_path->value[1], file_out);
614
333
    case SC_PATH_TYPE_DF_NAME:
615
333
      return entersafe_select_aid(card, in_path, file_out);
616
378
    case SC_PATH_TYPE_PATH:
617
378
      return entersafe_select_path(card, in_path->value, in_path->len, file_out);
618
0
    default:
619
0
      SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_ARGUMENTS);
620
711
  }
621
711
}
622
623
static int entersafe_create_mf(sc_card_t *card, sc_entersafe_create_data *data)
624
0
{
625
0
  int r;
626
0
  sc_apdu_t apdu;
627
628
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
629
630
0
  memcpy(data->data.df.init_key, init_key, sizeof(init_key));
631
632
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE0, 0x00, 0x00);
633
0
  apdu.cla = 0x84;
634
0
  apdu.data = (u8 *)&data->data.df;
635
0
  apdu.datalen = apdu.lc = sizeof(data->data.df);
636
637
0
  switch(card->type) {
638
0
    case SC_CARD_TYPE_ENTERSAFE_3K:
639
0
      r = entersafe_transmit_apdu(card, &apdu, trans_code_3k, sizeof(trans_code_3k), 0, 1);
640
0
      break;
641
0
    case SC_CARD_TYPE_ENTERSAFE_FTCOS_PK_01C:
642
0
    case SC_CARD_TYPE_ENTERSAFE_EJAVA_PK_01C:
643
0
    case SC_CARD_TYPE_ENTERSAFE_EJAVA_PK_01C_T0:
644
0
    case SC_CARD_TYPE_ENTERSAFE_EJAVA_H10CR_PK_01C_T1:
645
0
    case SC_CARD_TYPE_ENTERSAFE_EJAVA_D11CR_PK_01C_T1:
646
0
    case SC_CARD_TYPE_ENTERSAFE_EJAVA_C21C_PK_01C_T1:
647
0
    case SC_CARD_TYPE_ENTERSAFE_EJAVA_A22CR_PK_01C_T1:
648
0
    case SC_CARD_TYPE_ENTERSAFE_EJAVA_A40CR_PK_01C_T1:
649
0
      r = entersafe_transmit_apdu(card, &apdu, trans_code_ftcos_pk_01c, sizeof(trans_code_ftcos_pk_01c), 0, 1);
650
0
      break;
651
0
    default:
652
0
      r = SC_ERROR_INTERNAL;
653
0
      break;
654
0
  }
655
656
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
657
0
  return sc_check_sw(card, apdu.sw1, apdu.sw2);
658
0
}
659
660
static int entersafe_create_df(sc_card_t *card, sc_entersafe_create_data *data)
661
0
{
662
0
  int r;
663
0
  sc_apdu_t apdu;
664
665
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
666
667
0
  memcpy(data->data.df.init_key, init_key, sizeof(init_key));
668
669
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE0, 0x01, 0x00);
670
0
  apdu.cla = 0x84;
671
0
  apdu.data = (u8 *)&data->data.df;
672
0
  apdu.lc = apdu.datalen = sizeof(data->data.df);
673
674
0
  r = entersafe_transmit_apdu(card, &apdu,init_key,sizeof(init_key),0,1);
675
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
676
0
  return sc_check_sw(card, apdu.sw1, apdu.sw2);
677
0
}
678
679
static int entersafe_create_ef(sc_card_t *card, sc_entersafe_create_data *data)
680
0
{
681
0
  int r;
682
0
  sc_apdu_t apdu;
683
684
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
685
686
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE0, 0x02, 0x00);
687
0
  apdu.cla = 0x84;
688
0
  apdu.data = (u8*)&data->data.ef;
689
0
  apdu.lc = apdu.datalen = sizeof(data->data.ef);
690
691
0
  r = entersafe_transmit_apdu(card, &apdu, init_key, sizeof(init_key), 0, 1);
692
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
693
0
  return sc_check_sw(card, apdu.sw1, apdu.sw2);
694
0
}
695
696
static u8 process_acl_entry(sc_file_t *in, unsigned int method, unsigned int in_def)
697
0
{
698
0
  u8 def = (u8)in_def;
699
0
  const sc_acl_entry_t *entry = sc_file_get_acl_entry(in, method);
700
0
  if (!entry) {
701
0
    return def;
702
0
  } else if (entry->method & SC_AC_CHV) {
703
0
    unsigned int key_ref = entry->key_ref;
704
0
    if (key_ref == SC_AC_KEY_REF_NONE)
705
0
      return def;
706
0
    else
707
0
      return ENTERSAFE_AC_ALWAYS&0x04;
708
0
  } else if (entry->method & SC_AC_NEVER) {
709
0
    return ENTERSAFE_AC_NEVER;
710
0
  }
711
0
  return def;
712
0
}
713
714
static int entersafe_create_file(sc_card_t *card, sc_file_t *file)
715
0
{
716
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
717
718
0
  if (file->type == SC_FILE_TYPE_WORKING_EF) {
719
0
    sc_entersafe_create_data data;
720
0
    memset(&data, 0, sizeof(data));
721
722
0
    data.data.ef.file_id[0] = (file->id >> 8) & 0xFF;
723
0
    data.data.ef.file_id[1] = file->id & 0xFF;
724
0
    data.data.ef.size[0] = (file->size >> 8) & 0xFF;
725
0
    data.data.ef.size[1] = file->size & 0xFF;
726
0
    memset(data.data.ef.ac, ENTERSAFE_AC_ALWAYS, sizeof(data.data.ef.ac));
727
0
    data.data.ef.ac[0] = process_acl_entry(file, SC_AC_OP_READ, ENTERSAFE_AC_ALWAYS);
728
0
    data.data.ef.ac[1] = process_acl_entry(file, SC_AC_OP_UPDATE, ENTERSAFE_AC_ALWAYS);
729
730
0
    return entersafe_create_ef(card, &data);
731
0
  } else {
732
0
    return SC_ERROR_INVALID_ARGUMENTS;
733
0
  }
734
0
}
735
736
static int entersafe_internal_set_security_env(sc_card_t *card,
737
                         const sc_security_env_t *env,
738
                         u8 ** data,size_t* size)
739
0
{
740
0
  sc_apdu_t apdu;
741
0
  u8 sbuf[SC_MAX_APDU_BUFFER_SIZE];
742
0
  u8 *p = sbuf;
743
0
  int r;
744
745
0
  if (card == NULL || env == NULL)
746
0
    return SC_ERROR_INTERNAL;
747
748
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
749
750
0
  switch (env->operation) {
751
0
    case SC_SEC_OPERATION_DECIPHER:
752
0
    case SC_SEC_OPERATION_SIGN:
753
0
      sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0, 0);
754
0
      apdu.p1 = 0x41;
755
0
      apdu.p2 = 0xB8;
756
0
      *p++ = 0x80;
757
0
      *p++ = 0x01;
758
0
      *p++ = 0x80;
759
0
      *p++ = 0x83;
760
0
      *p++ = 0x02;
761
0
      *p++ = env->key_ref[0];
762
0
      *p++ = 0x22;
763
0
      if (*size > 1024 / 8) {
764
0
        if (*size == 2048 / 8) {
765
0
          *p++ = 0x89;
766
0
          *p++ = 0x40;
767
0
          memcpy(p, *data, 0x40);
768
0
          p += 0x40;
769
0
          *data += 0x40;
770
0
          *size -= 0x40;
771
0
        } else {
772
0
          SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_ARGUMENTS);
773
0
        }
774
0
      }
775
0
      break;
776
0
    default:
777
0
      SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_ARGUMENTS);
778
0
  }
779
780
0
  apdu.le = 0;
781
0
  apdu.lc = apdu.datalen = p - sbuf;
782
0
  apdu.data = sbuf;
783
0
  apdu.resplen = 0;
784
785
0
  r = sc_transmit_apdu(card, &apdu);
786
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
787
0
  return sc_check_sw(card, apdu.sw1, apdu.sw2);
788
0
}
789
790
/**
791
 * We don't really set the security environment,but cache it.It will be set when
792
 * security operation is performed later.Because we may transport partial of
793
 * the sign/decipher data within the security environment apdu.
794
 */
795
static int entersafe_set_security_env(sc_card_t *card,
796
                    const sc_security_env_t *env,
797
                    int se_num)
798
0
{
799
0
  if (card == NULL || env == NULL)
800
0
    return SC_ERROR_INTERNAL;
801
802
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
803
804
0
  if (card->drv_data) {
805
0
    free(card->drv_data);
806
0
    card->drv_data = 0;
807
0
  }
808
809
0
  card->drv_data = calloc(1, sizeof(*env));
810
0
  if (!card->drv_data)
811
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_OUT_OF_MEMORY);
812
813
0
  memcpy(card->drv_data, env, sizeof(*env));
814
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_SUCCESS);
815
0
}
816
817
static int entersafe_restore_security_env(sc_card_t *card, int se_num)
818
0
{
819
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
820
0
  return SC_SUCCESS;
821
0
}
822
823
824
static int entersafe_compute_with_prkey(sc_card_t *card,
825
                    const u8 *data, size_t datalen,
826
                    u8 *out, size_t outlen)
827
0
{
828
0
  int r;
829
0
  sc_apdu_t apdu;
830
0
  u8 sbuf[SC_MAX_APDU_BUFFER_SIZE];
831
0
  u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
832
0
  u8 *p = sbuf;
833
0
  size_t size = datalen;
834
835
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
836
837
0
  if (!data || !out || datalen > SC_MAX_APDU_BUFFER_SIZE)
838
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_ARGUMENTS);
839
840
0
  memcpy(p,data,size);
841
842
0
  if (!card->drv_data)
843
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INTERNAL);
844
845
0
  r = entersafe_internal_set_security_env(card, card->drv_data, &p, &size);
846
0
  LOG_TEST_RET(card->ctx, r, "internal set security env failed");
847
848
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x2A, 0x86, 0x80);
849
0
  apdu.data = p;
850
0
  apdu.lc = size;
851
0
  apdu.datalen = size;
852
0
  apdu.resp = rbuf;
853
0
  apdu.resplen = sizeof(rbuf);
854
0
  apdu.le = 256;
855
856
0
  r = entersafe_transmit_apdu(card, &apdu, 0, 0, 0, 0);
857
0
  LOG_TEST_GOTO_ERR(card->ctx, r, "APDU transmit failed");
858
859
0
  if (apdu.sw1 == 0x90 && apdu.sw2 == 0x00) {
860
0
    if (outlen < apdu.resplen) {
861
0
      r = SC_ERROR_BUFFER_TOO_SMALL;
862
0
      goto err;
863
0
    }
864
0
    memcpy(out, apdu.resp, apdu.resplen);
865
0
    sc_mem_clear(rbuf, sizeof(rbuf));
866
0
    r = (int)apdu.resplen;
867
0
  } else {
868
0
    r = sc_check_sw(card, apdu.sw1, apdu.sw2);
869
0
  }
870
0
err:
871
0
  sc_mem_clear(rbuf, sizeof(rbuf));
872
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r);
873
0
}
874
875
static int entersafe_compute_signature(sc_card_t *card,
876
                     const u8 * data, size_t datalen,
877
                     u8 * out, size_t outlen)
878
0
{
879
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
880
0
  return entersafe_compute_with_prkey(card, data, datalen, out, outlen);
881
0
}
882
883
static int entersafe_decipher(sc_card_t *card,
884
                const u8 *crgram, size_t crgram_len,
885
                u8 *out, size_t outlen)
886
0
{
887
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
888
0
  return entersafe_compute_with_prkey(card, crgram, crgram_len, out, outlen);
889
0
}
890
891
static void entersafe_init_pin_info(struct sc_pin_cmd_pin *pin, unsigned int num)
892
12
{
893
12
  pin->encoding   = SC_PIN_ENCODING_ASCII;
894
12
  pin->min_length = 4;
895
12
  pin->max_length = 16;
896
12
  pin->pad_length = 16;
897
12
  pin->offset     = 5 + num * 16;
898
12
  pin->pad_char   = 0x00;
899
12
}
900
901
static int entersafe_pin_cmd(sc_card_t *card, struct sc_pin_cmd_data *data)
902
6
{
903
6
  int r;
904
6
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
905
6
  entersafe_init_pin_info(&data->pin1, 0);
906
6
  entersafe_init_pin_info(&data->pin2, 1);
907
6
  data->flags |= SC_PIN_CMD_NEED_PADDING;
908
909
6
  if (data->cmd != SC_PIN_CMD_UNBLOCK) {
910
6
    r = iso_ops->pin_cmd(card, data);
911
6
    sc_log(card->ctx, "Verify rv:%i", r);
912
6
  } else {
913
0
    { /*verify*/
914
0
      sc_apdu_t apdu;
915
0
      u8 sbuf[0x10] = {0};
916
917
0
      if (data->pin1.len > sizeof(sbuf)) {
918
0
        return SC_ERROR_INVALID_DATA;
919
0
      }
920
921
0
      memcpy(sbuf, data->pin1.data, data->pin1.len);
922
0
      sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x20, 0x00, data->pin_reference + 1);
923
0
      apdu.lc = apdu.datalen = sizeof(sbuf);
924
0
      apdu.data = sbuf;
925
926
0
      r = entersafe_transmit_apdu(card, &apdu, 0, 0, 0, 0);
927
0
      LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
928
0
    }
929
930
0
    { /*change*/
931
0
      sc_apdu_t apdu;
932
0
      u8 sbuf[0x12] = {0};
933
934
0
      if (data->pin2.len + 2 > sizeof(sbuf)) {
935
0
        return SC_ERROR_INVALID_DATA;
936
0
      }
937
938
0
      sbuf[0] = 0x33;
939
0
      sbuf[1] = 0x00;
940
0
      memcpy(sbuf + 2, data->pin2.data, data->pin2.len);
941
0
      sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xF4, 0x0B, data->pin_reference);
942
0
      apdu.cla = 0x84;
943
0
      apdu.lc = apdu.datalen = sizeof(sbuf);
944
0
      apdu.data = sbuf;
945
946
0
      r = entersafe_transmit_apdu(card, &apdu, key_maintain, sizeof(key_maintain), 1, 1);
947
0
      LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
948
0
    }
949
0
  }
950
6
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r);
951
6
}
952
953
static int entersafe_erase_card(sc_card_t *card)
954
0
{
955
0
  int r;
956
0
  u8  sbuf[2];
957
0
  sc_apdu_t apdu;
958
959
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
960
961
0
  sbuf[0] = 0x3f;
962
0
  sbuf[1] = 0x00;
963
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xA4, 0x00, 0x00);
964
0
  apdu.lc = 2;
965
0
  apdu.datalen = 2;
966
0
  apdu.data = sbuf;
967
968
0
  r = entersafe_transmit_apdu(card, &apdu, 0, 0, 0, 0);
969
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
970
971
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xEE, 0x00, 0x00);
972
0
  apdu.cla = 0x84;
973
0
  apdu.lc = 2;
974
0
  apdu.datalen = 2;
975
0
  apdu.data = sbuf;
976
977
0
  switch(card->type) {
978
0
    case SC_CARD_TYPE_ENTERSAFE_3K:
979
0
      r = entersafe_transmit_apdu(card, &apdu, trans_code_3k, sizeof(trans_code_3k), 0, 1);
980
0
      break;
981
0
    case SC_CARD_TYPE_ENTERSAFE_FTCOS_PK_01C:
982
0
    case SC_CARD_TYPE_ENTERSAFE_EJAVA_PK_01C:
983
0
    case SC_CARD_TYPE_ENTERSAFE_EJAVA_PK_01C_T0:
984
0
    case SC_CARD_TYPE_ENTERSAFE_EJAVA_H10CR_PK_01C_T1:
985
0
    case SC_CARD_TYPE_ENTERSAFE_EJAVA_D11CR_PK_01C_T1:
986
0
    case SC_CARD_TYPE_ENTERSAFE_EJAVA_C21C_PK_01C_T1:
987
0
    case SC_CARD_TYPE_ENTERSAFE_EJAVA_A22CR_PK_01C_T1:
988
0
    case SC_CARD_TYPE_ENTERSAFE_EJAVA_A40CR_PK_01C_T1:
989
0
      r = entersafe_transmit_apdu(card, &apdu, trans_code_ftcos_pk_01c, sizeof(trans_code_ftcos_pk_01c), 0, 1);
990
0
      break;
991
0
    default:
992
0
      r = SC_ERROR_INTERNAL;
993
0
      break;
994
0
  }
995
996
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
997
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, sc_check_sw(card, apdu.sw1, apdu.sw2));
998
0
}
999
1000
static int
1001
entersafe_encode_bignum(u8 tag, sc_pkcs15_bignum_t bignum, u8 **ptr, size_t *ptrlen)
1002
0
{
1003
0
  u8 *p = *ptr;
1004
1005
  /* lower bound for 1B length encoding */
1006
0
  if (*ptrlen < bignum.len + 2) {
1007
0
    return SC_ERROR_INVALID_DATA;
1008
0
  }
1009
1010
0
  *p++ = tag;
1011
0
  *ptrlen -= 1;
1012
0
  if (bignum.len < 128) {
1013
0
    *p++ = (u8)bignum.len;
1014
0
    *ptrlen -= 1;
1015
0
  } else {
1016
0
    u8 bytes = 1;
1017
0
    size_t len = bignum.len;
1018
0
    while (len) {
1019
0
      len = len >> 8;
1020
0
      ++bytes;
1021
0
    }
1022
0
    bytes &= 0x0F;
1023
0
    *p++ = 0x80 | bytes;
1024
0
    *ptrlen -= 1;
1025
0
    while (bytes) {
1026
0
      *p++ = bignum.len >> ((bytes - 1) * 8);
1027
0
      *ptrlen -= 1;
1028
0
      --bytes;
1029
0
    }
1030
0
  }
1031
  /* Tag and length bytes already counted and checked */
1032
0
  if (*ptrlen < bignum.len) {
1033
0
    return SC_ERROR_INVALID_DATA;
1034
0
  }
1035
1036
0
  memcpy(p, bignum.data, bignum.len);
1037
0
  entersafe_reverse_buffer(p, bignum.len);
1038
0
  p += bignum.len;
1039
0
  *ptr = p;
1040
0
  *ptrlen -= bignum.len;
1041
1042
0
  return SC_SUCCESS;
1043
0
}
1044
1045
static int entersafe_write_small_rsa_key(sc_card_t *card, u8 key_id, struct sc_pkcs15_prkey_rsa *rsa)
1046
0
{
1047
0
  sc_apdu_t apdu;
1048
0
  u8 sbuff[SC_MAX_APDU_BUFFER_SIZE];
1049
0
  int r = SC_ERROR_INTERNAL;
1050
0
  u8 *p = sbuff;
1051
0
  size_t buflen = sizeof(sbuff);
1052
1053
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1054
1055
0
  { /* write prkey */
1056
0
    *p++ = 0x00;      /* EC */
1057
0
    *p++ = 0x00;      /* ver */
1058
0
    r = entersafe_encode_bignum('E', rsa->exponent, &p, &buflen);
1059
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "Failed to encode bignum. Buffer too small?");
1060
0
    r = entersafe_encode_bignum('D', rsa->d, &p, &buflen);
1061
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "Failed to encode bignum. Buffer too small?");
1062
1063
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xF4, 0x22, key_id);
1064
0
    apdu.cla = 0x84;
1065
0
    apdu.data = sbuff;
1066
0
    apdu.lc = apdu.datalen = p - sbuff;
1067
1068
0
    r = entersafe_transmit_apdu(card, &apdu, key_maintain, sizeof(key_maintain), 1, 1);
1069
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "APDU transmit failed");
1070
0
    r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1071
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "Write prkey failed");
1072
0
  }
1073
1074
0
  p = sbuff;
1075
0
  { /* write pukey */
1076
0
    *p++ = 0x00;      /* EC */
1077
0
    *p++ = 0x00;      /* ver */
1078
0
    r = entersafe_encode_bignum('E', rsa->exponent, &p, &buflen);
1079
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "Failed to encode bignum. Buffer too small?");
1080
0
    r = entersafe_encode_bignum('N', rsa->modulus, &p, &buflen);
1081
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "Failed to encode bignum. Buffer too small?");
1082
1083
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xF4, 0x2A, key_id);
1084
0
    apdu.cla = 0x84;
1085
0
    apdu.data = sbuff;
1086
0
    apdu.lc = apdu.datalen = p - sbuff;
1087
1088
0
    r = entersafe_transmit_apdu(card, &apdu, key_maintain, sizeof(key_maintain), 1, 1);
1089
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "APDU transmit failed");
1090
0
    r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1091
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "Write pukey failed");
1092
0
  }
1093
1094
0
err:
1095
0
  sc_mem_clear(sbuff, sizeof(sbuff));
1096
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r);
1097
0
}
1098
1099
static int entersafe_write_rsa_key_factor(sc_card_t *card,
1100
                      u8 key_id,u8 usage,
1101
                      u8 factor,
1102
                      sc_pkcs15_bignum_t data)
1103
0
{
1104
0
  int r;
1105
0
  sc_apdu_t apdu;
1106
1107
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1108
1109
0
  { /* MSE */
1110
0
    u8 sbuff[4];
1111
0
    sbuff[0] = 0x84;
1112
0
    sbuff[1] = 0x02;
1113
0
    sbuff[2] = key_id;
1114
0
    sbuff[3] = usage;
1115
1116
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0x01, 0xB8);
1117
0
    apdu.data = sbuff;
1118
0
    apdu.lc = apdu.datalen = 4;
1119
1120
0
    r = entersafe_transmit_apdu(card, &apdu, 0, 0, 0, 0);
1121
0
    LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1122
0
    LOG_TEST_RET(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2), "Write prkey factor failed(MSE)");
1123
0
  }
1124
1125
0
  { /* Write 'x'; */
1126
0
    u8 sbuff[SC_MAX_APDU_BUFFER_SIZE];
1127
1128
0
    if (!data.data || data.len == 0 || data.len > sizeof(sbuff))
1129
0
      return SC_ERROR_INVALID_ARGUMENTS;
1130
1131
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x46, factor, 0x00);
1132
1133
0
    memcpy(sbuff, data.data, data.len);
1134
0
    entersafe_reverse_buffer(sbuff, data.len);
1135
/*
1136
 *  PK01C and PK13C smart card only support 1024 or 2048bit key .
1137
 *  Size of exponent1 exponent2 coefficient of RSA private key keep the same as size of prime1
1138
 *  So check factor is padded with zero or not
1139
 */
1140
0
    switch(factor) {
1141
0
      case 0x3:
1142
0
      case 0x4:
1143
0
      case 0x5: {
1144
0
          size_t i;
1145
0
          if (data.len > 32 && data.len < 64) {
1146
0
            for (i = data.len; i < 64; i++)
1147
0
              sbuff[i] = 0;
1148
0
            data.len = 64;
1149
0
          } else if( data.len > 64 && data.len < 128 ) {
1150
0
            for (i = data.len; i < 128; i++)
1151
0
              sbuff[i] = 0;
1152
0
            data.len = 128;
1153
0
          }
1154
0
        }
1155
0
        break;
1156
0
      default:
1157
0
        break;
1158
0
    }
1159
1160
0
    apdu.data = sbuff;
1161
0
    apdu.lc = apdu.datalen = data.len;
1162
1163
0
    r = entersafe_transmit_apdu(card, &apdu, 0, 0, 0, 0);
1164
0
    sc_mem_clear(sbuff, sizeof(sbuff));
1165
0
    LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1166
0
    LOG_TEST_RET(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2), "Write prkey factor failed");
1167
0
  }
1168
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_SUCCESS);
1169
0
}
1170
1171
static int entersafe_write_large_rsa_key(sc_card_t *card,u8 key_id,struct sc_pkcs15_prkey_rsa *rsa)
1172
0
{
1173
0
  int r;
1174
1175
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1176
1177
0
  { /* write prkey */
1178
0
    r = entersafe_write_rsa_key_factor(card, key_id, 0x22, 0x01, rsa->p);
1179
0
    LOG_TEST_RET(card->ctx, r, "write p failed");
1180
0
    r = entersafe_write_rsa_key_factor(card, key_id, 0x22, 0x02, rsa->q);
1181
0
    LOG_TEST_RET(card->ctx, r, "write q failed");
1182
0
    r = entersafe_write_rsa_key_factor(card, key_id, 0x22, 0x03, rsa->dmp1);
1183
0
    LOG_TEST_RET(card->ctx, r, "write dmp1 failed");
1184
0
    r = entersafe_write_rsa_key_factor(card, key_id, 0x22, 0x04, rsa->dmq1);
1185
0
    LOG_TEST_RET(card->ctx, r, "write dmq1 failed");
1186
0
    r = entersafe_write_rsa_key_factor(card, key_id, 0x22, 0x05, rsa->iqmp);
1187
0
    LOG_TEST_RET(card->ctx, r, "write iqmp failed");
1188
0
  }
1189
1190
0
  { /* write pukey */
1191
0
    u8 sbuff[SC_MAX_APDU_BUFFER_SIZE];
1192
0
    sc_apdu_t apdu;
1193
1194
0
    if (!rsa->modulus.data || rsa->modulus.len < 0x100)
1195
0
      return SC_ERROR_INVALID_ARGUMENTS;
1196
1197
    /* first 64(0x40) bytes of N */
1198
0
    sbuff[0] = 0x83;
1199
0
    sbuff[1] = 0x02;
1200
0
    sbuff[2] = key_id;
1201
0
    sbuff[3] = 0x2A;
1202
0
    sbuff[4] = 0x89;
1203
0
    sbuff[5] = 0x40;
1204
0
    memcpy(sbuff + 6, rsa->modulus.data, 0x40);
1205
1206
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0x01, 0xB8);
1207
0
    apdu.data = sbuff;
1208
0
    apdu.lc = apdu.datalen = 0x46;
1209
1210
0
    r = entersafe_transmit_apdu(card, &apdu, 0, 0, 0, 0);
1211
0
    sc_mem_clear(sbuff, sizeof(sbuff));
1212
0
    LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1213
0
    LOG_TEST_RET(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2), "Write pukey N(1) failed");
1214
1215
    /* left 192(0xC0) bytes of N */
1216
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x46, 0x0B, 0x00);
1217
0
    apdu.data = rsa->modulus.data + 0x40;
1218
0
    apdu.lc = apdu.datalen = 0xC0;
1219
1220
0
    r = entersafe_transmit_apdu(card, &apdu, 0, 0, 0, 0);
1221
0
    LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1222
0
    LOG_TEST_RET(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2), "Write pukey N(2) failed");
1223
1224
    /* E */
1225
0
    r = entersafe_write_rsa_key_factor(card, key_id, 0x2A, 0x0D, rsa->exponent);
1226
0
    LOG_TEST_RET(card->ctx, r, "write exponent failed");
1227
0
  }
1228
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_SUCCESS);
1229
0
}
1230
1231
static int entersafe_write_symmetric_key(sc_card_t *card,
1232
                     u8 key_id, u8 usage,
1233
                     u8 EC, u8 ver,
1234
                     u8 *data, size_t len)
1235
0
{
1236
0
  sc_apdu_t apdu;
1237
0
  u8 sbuff[SC_MAX_APDU_BUFFER_SIZE] = {0};
1238
0
  int r;
1239
1240
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1241
1242
0
  if (len > 240)
1243
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INCORRECT_PARAMETERS);
1244
1245
0
  sbuff[0] = EC;
1246
0
  sbuff[1] = ver;
1247
0
  memcpy(&sbuff[2], data, len);
1248
1249
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xF4, usage, key_id);
1250
0
  apdu.cla = 0x84;
1251
0
  apdu.data = sbuff;
1252
0
  apdu.lc = apdu.datalen = len + 2;
1253
1254
0
  r = entersafe_transmit_apdu(card, &apdu, key_maintain, sizeof(key_maintain), 1, 1);
1255
0
  sc_mem_clear(sbuff, sizeof(sbuff));
1256
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1257
0
  LOG_TEST_RET(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2), "Write prkey failed");
1258
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r);
1259
0
}
1260
1261
static int entersafe_write_key(sc_card_t *card, sc_entersafe_wkey_data *data)
1262
0
{
1263
0
  struct sc_pkcs15_prkey_rsa *rsa = data->key_data.rsa;
1264
1265
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1266
1267
0
  switch(data->usage) {
1268
0
    case 0x22:
1269
0
      if(rsa->modulus.len < 256)
1270
0
        return entersafe_write_small_rsa_key(card,data->key_id, rsa);
1271
0
      else
1272
0
        return entersafe_write_large_rsa_key(card,data->key_id, rsa);
1273
0
      break;
1274
0
    case 0x2A:
1275
0
      SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_NOT_SUPPORTED);
1276
0
      break;
1277
0
    default:
1278
0
      return entersafe_write_symmetric_key(card,data->key_id,data->usage,
1279
0
                        data->key_data.symmetric.EC,
1280
0
                        data->key_data.symmetric.ver,
1281
0
                        data->key_data.symmetric.key_val,
1282
0
                        data->key_data.symmetric.key_len);
1283
0
      break;
1284
0
  }
1285
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_SUCCESS);
1286
0
}
1287
1288
static int entersafe_gen_key(sc_card_t *card, sc_entersafe_gen_key_data *data)
1289
0
{
1290
0
  int r;
1291
0
  size_t len = data->key_length >> 3;
1292
0
  sc_apdu_t apdu;
1293
0
  u8 rbuf[300] = {0};
1294
0
  u8 sbuf[4], *p;
1295
0
  size_t plen = 0;
1296
1297
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1298
1299
  /* MSE */
1300
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0x01, 0xB8);
1301
0
  apdu.lc = 0x04;
1302
0
  sbuf[0] = 0x83;
1303
0
  sbuf[1] = 0x02;
1304
0
  sbuf[2] = data->key_id;
1305
0
  sbuf[3] = 0x2A;
1306
0
  apdu.data = sbuf;
1307
0
  apdu.datalen = 4;
1308
0
  apdu.lc = 4;
1309
0
  apdu.le = 0;
1310
1311
0
  r = entersafe_transmit_apdu(card, &apdu, 0, 0, 0, 0);
1312
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1313
0
  LOG_TEST_RET(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2), "EnterSafe set MSE failed");
1314
1315
  /* generate key */
1316
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x46, 0x00, 0x00);
1317
0
  apdu.le = 0;
1318
0
  sbuf[0] = (u8)(data->key_length >> 8);
1319
0
  sbuf[1] = (u8)(data->key_length);
1320
0
  apdu.data = sbuf;
1321
0
  apdu.lc = 2;
1322
0
  apdu.datalen = 2;
1323
1324
0
  r = entersafe_transmit_apdu(card, &apdu, 0, 0, 0, 0);
1325
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1326
0
  LOG_TEST_RET(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2), "EnterSafe generate key pair failed");
1327
1328
  /* read public key via READ PUBLIC KEY */
1329
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xE6, 0x2A, data->key_id);
1330
0
  apdu.cla     = 0x80;
1331
0
  apdu.resp    = rbuf;
1332
0
  apdu.resplen = sizeof(rbuf);
1333
0
  apdu.le      = 256;
1334
0
  r = entersafe_transmit_apdu(card, &apdu, 0, 0, 0, 0);
1335
0
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1336
0
  LOG_TEST_RET(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2), "EnterSafe get pukey failed");
1337
1338
0
  p = rbuf;
1339
0
  plen = apdu.resplen;
1340
0
  if (*p != 'E') {
1341
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_DATA);
1342
0
  }
1343
0
  if ((size_t)(p - rbuf) + 2 + p[1] >= plen) {
1344
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_DATA);
1345
0
  }
1346
0
  p += 2 + p[1];
1347
  /* N */
1348
0
  if (*p != 'N') {
1349
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_DATA);
1350
0
  }
1351
0
  if ((size_t)(p - rbuf) + 2 >= plen) {
1352
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_DATA);
1353
0
  }
1354
0
  ++p;
1355
0
  if (*p++ > 0x80) {
1356
0
    u8 len_bytes = (*(p - 1)) & 0x0f;
1357
0
    size_t module_len = 0;
1358
0
    if ((size_t)(p - rbuf) + len_bytes >= plen) {
1359
0
      SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_DATA);
1360
0
    }
1361
0
    while (len_bytes != 0) {
1362
0
      module_len = module_len << 8;
1363
0
      module_len += *p++;
1364
0
      --len_bytes;
1365
0
    }
1366
0
  }
1367
1368
0
  if ((p - rbuf) + len >= plen) {
1369
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_INVALID_DATA);
1370
0
  }
1371
1372
0
  data->modulus = malloc(len);
1373
0
  if (!data->modulus)
1374
0
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_OUT_OF_MEMORY);
1375
0
  entersafe_reverse_buffer(p, len);
1376
0
  memcpy(data->modulus, p, len);
1377
1378
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_SUCCESS);
1379
0
}
1380
1381
static int entersafe_get_serialnr(sc_card_t *card, sc_serial_number_t *serial)
1382
161
{
1383
161
  int r;
1384
161
  sc_apdu_t apdu;
1385
161
  u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
1386
1387
161
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1388
161
  if (serial == NULL)
1389
0
    return SC_ERROR_INTERNAL;
1390
1391
161
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xEA, 0x00, 0x00);
1392
161
  apdu.cla = 0x80;
1393
161
  apdu.resp = rbuf;
1394
161
  apdu.resplen = sizeof(rbuf);
1395
161
  apdu.le = 0x08;
1396
1397
161
  r = entersafe_transmit_apdu(card, &apdu, 0, 0, 0, 0);
1398
161
  LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
1399
157
  LOG_TEST_RET(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2), "EnterSafe get SN failed");
1400
14
  if (apdu.resplen != 8)
1401
14
    LOG_TEST_RET(card->ctx, SC_ERROR_UNKNOWN_DATA_RECEIVED, "Invalid length of SN");
1402
1403
5
  card->serialnr.len = serial->len = 8;
1404
5
  memcpy(card->serialnr.value, rbuf, 8);
1405
5
  memcpy(serial->value, rbuf, 8);
1406
1407
5
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_SUCCESS);
1408
5
}
1409
1410
static int entersafe_preinstall_rsa_2048(sc_card_t *card, u8 key_id)
1411
0
{
1412
0
  u8 sbuf[SC_MAX_APDU_BUFFER_SIZE];
1413
0
  sc_apdu_t apdu;
1414
0
  int ret = 0;
1415
0
  static u8 const rsa_key_e[] =
1416
0
  {
1417
0
    'E', 0x04, 0x01, 0x00, 0x01, 0x00
1418
0
  };
1419
1420
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1421
1422
  /* create rsa item in IKF */
1423
0
  sbuf[0] = 0x04; /* key len extern */
1424
0
  sbuf[1] = 0x0a; /* key len */
1425
0
  sbuf[2] = 0x22; /* USAGE */
1426
0
  sbuf[3] = 0x34; /* user ac */
1427
0
  sbuf[4] = 0x04; /* change ac */
1428
0
  sbuf[5] = 0x34; /* UPDATE AC */
1429
0
  sbuf[6] = 0x40; /* ALGO */
1430
0
  sbuf[7] = 0x00; /* EC */
1431
0
  sbuf[8] = 0x00; /* VER */
1432
0
  memcpy(&sbuf[9], rsa_key_e, sizeof(rsa_key_e));
1433
0
  sbuf[9 + sizeof(rsa_key_e) + 0] = 'C'+'R'+'T';
1434
0
  sbuf[9 + sizeof(rsa_key_e) + 1] = 0x82;
1435
0
  sbuf[9 + sizeof(rsa_key_e) + 2] = 0x04;
1436
0
  sbuf[9 + sizeof(rsa_key_e) + 3] = 0x00;
1437
1438
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xF0, 0x00, key_id);
1439
0
  apdu.cla = 0x84;
1440
0
  apdu.data = sbuf;
1441
0
  apdu.lc = apdu.datalen = 9 + sizeof(rsa_key_e) + 4;
1442
1443
0
  ret = entersafe_transmit_apdu(card, &apdu, init_key, sizeof(init_key), 0, 1);
1444
0
  LOG_TEST_RET(card->ctx, ret, "Preinstall rsa failed");
1445
1446
  /* create rsa item in PKF */
1447
0
  sbuf[0] = 0x01; /* key len extern */
1448
0
  sbuf[1] = 0x0A; /* key len */
1449
0
  sbuf[2] = 0x2A; /* USAGE */
1450
0
  sbuf[3] = ENTERSAFE_AC_ALWAYS; /* user ac */
1451
0
  sbuf[4] = 0x04; /* change ac */
1452
0
  sbuf[5] = ENTERSAFE_AC_ALWAYS; /* UPDATE AC */
1453
0
  sbuf[6] = 0x40; /* ALGO */
1454
0
  sbuf[7] = 0x00; /* EC */
1455
0
  sbuf[8] = 0x00; /* VER */
1456
0
  memcpy(&sbuf[9], rsa_key_e, sizeof(rsa_key_e));
1457
0
  sbuf[9 + sizeof(rsa_key_e) + 0] = 'N';
1458
0
  sbuf[9 + sizeof(rsa_key_e) + 1] = 0x82;
1459
0
  sbuf[9 + sizeof(rsa_key_e) + 2] = 0x01;
1460
0
  sbuf[9 + sizeof(rsa_key_e) + 3] = 0x00;
1461
1462
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xF0, 0x00, key_id);
1463
0
  apdu.cla = 0x84;
1464
0
  apdu.data = sbuf;
1465
0
  apdu.lc = apdu.datalen = 9 + sizeof(rsa_key_e) + 4;
1466
1467
0
  ret = entersafe_transmit_apdu(card, &apdu, init_key, sizeof(init_key), 0, 1);
1468
0
  LOG_TEST_RET(card->ctx, ret, "Preinstall rsa failed");
1469
1470
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_SUCCESS);
1471
0
}
1472
1473
static int entersafe_preinstall_keys(sc_card_t *card, int (*install_rsa)(sc_card_t *, u8))
1474
0
{
1475
0
  int r;
1476
0
  u8 sbuf[SC_MAX_APDU_BUFFER_SIZE];
1477
0
  sc_apdu_t apdu;
1478
1479
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1480
1481
0
  { /* RSA */
1482
0
    u8 rsa_index;
1483
0
    for (rsa_index = ENTERSAFE_MIN_KEY_ID; rsa_index <= ENTERSAFE_MAX_KEY_ID; ++rsa_index) {
1484
0
      r = install_rsa(card, rsa_index);
1485
0
      LOG_TEST_RET(card->ctx, r, "Preinstall rsa key failed");
1486
0
    }
1487
0
  }
1488
1489
0
  { /* key maintain */
1490
    /* create key maintain*/
1491
0
    sbuf[0] = 0;  /* key len extern */
1492
0
    sbuf[1] = sizeof(key_maintain); /* key len */
1493
0
    sbuf[2] = 0x03; /* USAGE */
1494
0
    sbuf[3] = ENTERSAFE_AC_ALWAYS; /* use AC */
1495
0
    sbuf[4] = ENTERSAFE_AC_ALWAYS; /* CHANGE AC */
1496
0
    sbuf[5] = ENTERSAFE_AC_NEVER; /* UPDATE AC */
1497
0
    sbuf[6] = 0x01; /* ALGO */
1498
0
    sbuf[7] = 0x00; /* EC */
1499
0
    sbuf[8] = 0x00; /* VER */
1500
0
    memcpy(&sbuf[9], key_maintain, sizeof(key_maintain));
1501
1502
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xF0, 0x00, 0x00);
1503
0
    apdu.cla = 0x84;
1504
0
    apdu.data = sbuf;
1505
0
    apdu.lc = apdu.datalen = 0x19;
1506
1507
0
    r = entersafe_transmit_apdu(card, &apdu, init_key, sizeof(init_key), 0, 1);
1508
0
    LOG_TEST_RET(card->ctx, r, "Preinstall key maintain failed");
1509
0
  }
1510
1511
0
  { /* user PIN */
1512
0
    memset(sbuf, 0, sizeof(sbuf));
1513
0
    sbuf[0] = 0;  /* key len extern */
1514
0
    sbuf[1] = 16; /* key len */
1515
0
    sbuf[2] = 0x0B; /* USAGE */
1516
0
    sbuf[3] = ENTERSAFE_AC_ALWAYS; /* use AC */
1517
0
    sbuf[4] = 0X04; /* CHANGE AC */
1518
0
    sbuf[5] = 0x38; /* UPDATE AC */
1519
0
    sbuf[6] = 0x01; /* ALGO */
1520
0
    sbuf[7] = 0xFF; /* EC */
1521
0
    sbuf[8] = 0x00; /* VER */
1522
1523
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xF0, 0x00, ENTERSAFE_USER_PIN_ID);
1524
0
    apdu.cla = 0x84;
1525
0
    apdu.data = sbuf;
1526
0
    apdu.lc = apdu.datalen = 0x19;
1527
1528
0
    r = entersafe_transmit_apdu(card, &apdu, init_key, sizeof(init_key), 0, 1);
1529
0
    LOG_TEST_RET(card->ctx, r, "Preinstall user PIN failed");
1530
0
  }
1531
1532
0
  { /* user PUK */
1533
0
    memset(sbuf, 0, sizeof(sbuf));
1534
0
    sbuf[0] = 0;  /* key len extern */
1535
0
    sbuf[1] = 16; /* key len */
1536
0
    sbuf[2] = 0x0B; /* USAGE */
1537
0
    sbuf[3] = ENTERSAFE_AC_ALWAYS; /* use AC */
1538
0
    sbuf[4] = 0X08; /* CHANGE AC */
1539
0
    sbuf[5] = 0xC0; /* UPDATE AC */
1540
0
    sbuf[6] = 0x01; /* ALGO */
1541
0
    sbuf[7] = 0xFF; /* EC */
1542
0
    sbuf[8] = 0x00; /* VER */
1543
1544
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xF0, 0x00, ENTERSAFE_USER_PIN_ID + 1);
1545
0
    apdu.cla = 0x84;
1546
0
    apdu.data = sbuf;
1547
0
    apdu.lc = apdu.datalen = 0x19;
1548
1549
0
    r = entersafe_transmit_apdu(card, &apdu, init_key, sizeof(init_key), 0, 1);
1550
0
    LOG_TEST_RET(card->ctx, r, "Preinstall user PUK failed");
1551
0
  }
1552
1553
0
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_SUCCESS);
1554
0
}
1555
1556
static int entersafe_card_ctl_2048(sc_card_t *card, unsigned long cmd, void *ptr)
1557
161
{
1558
161
  sc_entersafe_create_data *tmp = (sc_entersafe_create_data *)ptr;
1559
1560
161
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1561
1562
161
  switch (cmd) {
1563
0
    case SC_CARDCTL_ENTERSAFE_CREATE_FILE:
1564
0
      if (tmp->type == SC_ENTERSAFE_MF_DATA)
1565
0
        return entersafe_create_mf(card, tmp);
1566
0
      else if (tmp->type == SC_ENTERSAFE_DF_DATA)
1567
0
        return entersafe_create_df(card, tmp);
1568
0
      else if (tmp->type == SC_ENTERSAFE_EF_DATA)
1569
0
        return entersafe_create_ef(card, tmp);
1570
0
      else
1571
0
        return SC_ERROR_INTERNAL;
1572
0
    case SC_CARDCTL_ENTERSAFE_WRITE_KEY:
1573
0
      return entersafe_write_key(card, (sc_entersafe_wkey_data *)ptr);
1574
0
    case SC_CARDCTL_ENTERSAFE_GENERATE_KEY:
1575
0
      return entersafe_gen_key(card, (sc_entersafe_gen_key_data *)ptr);
1576
0
    case SC_CARDCTL_ERASE_CARD:
1577
0
      return entersafe_erase_card(card);
1578
161
    case SC_CARDCTL_GET_SERIALNR:
1579
161
      return entersafe_get_serialnr(card, (sc_serial_number_t *)ptr);
1580
0
    case SC_CARDCTL_ENTERSAFE_PREINSTALL_KEYS:
1581
0
      return entersafe_preinstall_keys(card, entersafe_preinstall_rsa_2048);
1582
0
    default:
1583
0
      return SC_ERROR_NOT_SUPPORTED;
1584
161
  }
1585
161
}
1586
1587
static struct sc_card_driver * sc_get_driver(void)
1588
16.8k
{
1589
16.8k
  struct sc_card_driver *iso_drv = sc_get_iso7816_driver();
1590
1591
16.8k
  if (iso_ops == NULL)
1592
1
    iso_ops = iso_drv->ops;
1593
1594
16.8k
  entersafe_ops = *iso_drv->ops;
1595
16.8k
  entersafe_ops.match_card = entersafe_match_card;
1596
16.8k
  entersafe_ops.init = entersafe_init;
1597
16.8k
  entersafe_ops.finish = entersafe_finish;
1598
16.8k
  entersafe_ops.read_binary = entersafe_read_binary;
1599
16.8k
  entersafe_ops.write_binary = NULL;
1600
16.8k
  entersafe_ops.update_binary = entersafe_update_binary;
1601
16.8k
  entersafe_ops.select_file = entersafe_select_file;
1602
16.8k
  entersafe_ops.restore_security_env = entersafe_restore_security_env;
1603
16.8k
  entersafe_ops.set_security_env = entersafe_set_security_env;
1604
16.8k
  entersafe_ops.decipher = entersafe_decipher;
1605
16.8k
  entersafe_ops.compute_signature = entersafe_compute_signature;
1606
16.8k
  entersafe_ops.create_file = entersafe_create_file;
1607
16.8k
  entersafe_ops.delete_file = NULL;
1608
16.8k
  entersafe_ops.pin_cmd = entersafe_pin_cmd;
1609
16.8k
  entersafe_ops.card_ctl = entersafe_card_ctl_2048;
1610
16.8k
  entersafe_ops.process_fci = entersafe_process_fci;
1611
16.8k
  return &entersafe_drv;
1612
16.8k
}
1613
1614
struct sc_card_driver * sc_get_entersafe_driver(void)
1615
16.8k
{
1616
16.8k
  return sc_get_driver();
1617
16.8k
}
1618
#endif